1

maybe the title is not very clear, let me elaborate.

I have a python script that open a ppm file , apply a chosen filter(rotations...) and create a new picture. until here everything work fine.

but I want to do the same thing through a linux console like:

ppmfilter.py ROTD /path/imageIn.ppm /path/imageOut.ppm

here ROTD is the name of the function that apply a rotation.

I don't know how to do this, I'm looking for a library that'll allow me to do this.

looking forward for your help.

P.S.: I'm using python 2.7

Alpagut
  • 1,173
  • 5
  • 15
  • 21

4 Answers4

7

There is a relatively easy way:

You can determine the global names (functions, variables, etc.) with the use of 'globals()'. This gives you a dictionary of all global symbols. You'll just need to check the type (with type() and the module types) and if it's a function, you can call it with sys.argv:

import types
import sys

def ROTD(infile, outfile):
    # do something

if __name__ == '__main__':
    symbol = globals().get(sys.argv[1])
    if hasattr(symbol, '__call__'):
        symbol(*sys.argv[2:])

This will pass the program argument (excluding the filename and the command name) to the function.

EDIT: Please, don't forget the error handling. I omited it for reasons of clarity.

terminus
  • 13,745
  • 8
  • 34
  • 37
  • 1
    I'd add an `if __name__ == "__main__"`, so you can still import the module without it trying to do that. Also, you should look up `sys.argv[1]`, not `sys.argv[0]` (which will be the name of the script). – Thomas K Nov 21 '10 at 12:27
  • 1
    Cool. To be even more pythonic, you could test `if hasattr(symbol, "__call__")`, rather than checking for the function type. That would let you call a class (or any other callable object) in the same way, and saves you an import. – Thomas K Nov 21 '10 at 12:35
  • :-). You no longer need `import types` now! – Thomas K Nov 21 '10 at 12:47
1

Use main() function:

def main()
   # call your function here

if __name__ == "__main__":
   main()
ceth
  • 44,198
  • 62
  • 180
  • 289
  • Also, you need to add `#!/usr/bin/env python` (or similar) as the first line of your file, make it executable and, probably in the `if __name__ ...` block, parse the command line arguments and deal with them (probably by passing them into your function). – Andrew Walker Nov 21 '10 at 12:22
1

A nice way to do it would be to define a big dictionary {alias: function} inside your module. For instance:

actions = {
    'ROTD': ROTD,
    'REFL': reflect_image,
    'INVT': invIm,
}

You get the idea. Then take the first command-line argument and interpret it as a key of this dictionary, applying actions[k] to the rest of the arguments.

Katriel
  • 120,462
  • 19
  • 136
  • 170
0

You can define in your ppmfilter.py main section doing this:

if __name__ == "__main__":
  import sys
  ROTD(sys.argv[1], sys.argv[2]) # change according to the signature of the function

and call it: python ppmfilter.py file1 file2

You can also run python -c in the directory that contains you *.py file:

python -c "import ppmfilter; ppmfilter.ROTD('/path/to/file1', '/path/to/file2')"
khachik
  • 28,112
  • 9
  • 59
  • 94