'indent'ing Python in windows bat

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Wed Sep 19 05:22:32 EDT 2012


Am 18.09.2012 15:03 schrieb David Smith:

> I COULD break down each batch file and write dozens of mini python
> scripts to be called. I already have a few, too. Efficiency? Speed is
> bad, but these are bat files, after all. The cost of trying to work with
> a multitude of small files is high, though, and I realized I had better
> go to a mix.

In order to achieve this, it might be very useful to either have a 
module for each (bigger) part to be achieved which you can call with

     python -m modulename arg1 arg2 arg3

and putting the Python code into modulename.py.

Or you have one big "interpreter" which works this way:

class Cmd(object):
     """
     Command collector
     """
     def __init__(self):
         self.cmds = {}
     def cmd(self, f):
         # register a function
         self.cmds[f.__name__] = f
         return f
     def main(self):
         import sys
         sys.exit(self.cmds[sys.argv[1]](*sys.argv[2:]))

cmd = Cmd()

@cmd.cmd
def cmd1(arg1, arg2):
     do_stuff()
     ...
     return 1 # error -> exit()

@cmd.cmd
def cmd2():
     ...

if __name__ == '__main__':
     cmd.main()


This is suitable for many small things and can be used this way:

bat cmds
python -m thismodule cmd1 a b
other bat cmds
python -m thismodule cmd2
...

HTH,

Thomas



More information about the Python-list mailing list