Wrap a function

Dan Stromberg drsalists at gmail.com
Wed Feb 3 21:33:25 CET 2010


Joan Miller wrote:
> On 28 ene, 21:40, Jonathan Gardner <jgard... at jonathangardner.net>
> wrote:
>   
>> On Jan 28, 10:20 am, Joan Miller <pelok... at gmail.com> wrote:
>>
>>
>>
>>     
>>> I've to call to many functions with the format:
>>>       
>>>>>> run("cmd")
>>>>>>             
>>> were "cmd" is a command with its arguments to pass them to the shell
>>> and run it, i.e.
>>>       
>>>>>>  run("pwd")
>>>>>>             
>>> or
>>>       
>>>>>> run("ls /home")
>>>>>>             
>>> Does anybody knows any library to help me to avoid the use of the main
>>> quotes, and brackets?
>>>       
>>> I would to use anything as:
>>>       
>>> $ ls /home => run("ls /home")
>>>       
>>> or, at least
>>>       
>>> run pwd => run("pwd")
>>>       
>> How about this?
>>
>> def pwd(): return run("pwd")
>>
>> pwd()
>>
>> def ls(l=False, files=()):
>>     args = []
>>     if l: args.insert(0, '-l')
>>     args.append(files)
>>     return run("ls", args)
>>
>> ls(l=True, "/foo")
>>     
>
> There would be to make a function for each system command to use so it
> would be too inefficient, and follow the problem with the quotes.
>
> The best is make a parser into a compiled language
>   
'disagree.

Best is to have a text file outside your program, in which you define 
commands and symbolic names for those commands.

Then you have a python module which reads these commands and names, and 
creates functions that invoke them via the specified name.

This way you get concise syntax, don't have to type as much boilerplate, 
and don't add line noise.

Mixing two languages as though they were the same language greatly 
increases the complexity of the original language with little gain.

Consider PowerShell - it's almost like two different languages smushed 
together, and you have to know how a command was implemented to know how 
to read it.

Also, what if someday The Powers That Be (the python language core 
designers) decide they need to use $ for something?  I hope they won't, 
but if they do, your preprocessor might make quite a mess of it.




More information about the Python-list mailing list