annotate python/example/func_args.py @ 652:1d70d702b1c4

STUB: .mozilla/userContent.css
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Mar 2014 19:22:59 -0700
parents e92bd004b906
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
625
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 get args of your own function
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 http://stackoverflow.com/questions/582056/getting-list-of-parameter-names-inside-python-function
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 """
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import argparse
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import inspect
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import os
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import subprocess
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import sys
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def foo(bar=None, fleem=None, blah=None):
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 frame = inspect.currentframe()
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 args, _, _, values = inspect.getargvalues(frame)
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 print 'function name "%s"' % inspect.getframeinfo(frame)[2]
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 for i in args:
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 print " %s = %s" % (i, values[i])
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 return [(i, values[i]) for i in args]
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 def main(args=sys.argv[1:]):
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 parser = argparse.ArgumentParser()
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 options = parser.parse_args(args)
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 foo(1, 2, 3)
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 if __name__ == '__main__':
e92bd004b906 STUB: .emacs python/example/func_args.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 main()