Wrap a function

John Posner jjposner at optimum.net
Thu Jan 28 23:57:09 CET 2010


On 1/28/2010 3:45 PM, Joan Miller wrote:
> On 28 ene, 20:34, Joan Miller<pelok... at gmail.com>  wrote:
>> On 28 ene, 20:20, Peter<peter.milli... at gmail.com>  wrote:
>>
>>> On Jan 29, 6:58 am, John Posner<jjpos... at optimum.net>  wrote:
>>
>>>> On 1/28/2010 2:24 PM, Joan Miller wrote:
>>
>>>>> On 28 ene, 19:16, Josh Holland<j... at joshh.co.uk>    wrote:
>>>>>> On 2010-01-28, Joan Miller<pelok... at gmail.com>    wrote:
>>
>>>>>>> I've to call to many functions with the format:
>>
>>>>>>>>>> run("cmd")
>>
>>>>>> Check the docs on os.system().
>>>>> No. I've a function that uses subprocess to run commands on the same
>>>>> shell and so substitute to bash scrips. But a script full of run
>>>>> ("shell_command --with --arguments") is too verbose.
>>
>>>> I'm suspicious of your original intent. Essentially, you want to write
>>>> code in which a literal string, such as ...
>>
>>>>     ls -l
>>
>>>> ... is *not* enclosed in quotes. Why run the risk of creating confusion
>>>> (in other people who look at your code, in syntax-checking tools, etc.)
>>>> between variables and literals?
>>
>>>> But I'm in sympathy with your desire to make the code as clean as
>>>> possible and to minimize the number of times you have to type a quote
>>>> character. My suggestions:
>>
>>>> 1. Create a function (say, "Run") that encapsulates as much of the
>>>> syntax as possible: os.system(), subprocess.call(), string-splitting,
>>>> whatever. So an invocation would look like this:
>>
>>>>     Run("ls -l *.txt")
>>
>>>> (I think you've already done this step.)
>>
>>>> 2. Find a text editor that supports keyboard macros, so that a single
>>>> keystroke turns this text line:
>>
>>>>     ls -l *.txt
>>
>>>> ... into this one:
>>
>>>>     Run("ls -l *.txt")
>>
>>>> HTH,
>>>> John
>>
>>> I can't see you avoiding quotes etc, but extending on John's comment,
>>> the obvious next step would be to run everything in a loop i.e. place
>>> all the commands into a list and create a loop that ran each command
>>> in the list.
>>
>> Yes, but could be necessary that were mixed with python code.
>>
>>> Almost all editors support macros - most editors support some form of
>>> language sensitive editing (NOT the prompt call parameters style but
>>> rather help with the syntax via a 'form' style of fill-in) that will
>>> allow you to reduce typing effort. But macros would be the first and
>>> easiest choice for this activity.
>>
>> The goal of my program is substitute to bash scripts, so the macros in
>> editors are irrelevant fo this one.
>
> I think that the best solution that I've is to build a program that
> parses the script to convert *$ command* to run("command") before of
> be called by python.

I believe you're working on Linux, so how about using "sed"? Here's a 
(prettified) BASH transcript of a sed script (edit.sed) transforming a 
6-line text file (myprog.py). The text file has both Python statements 
and "special commands", which have "$ " at the beginning of the line.

 >>> cat myprog.py
print "hello"
$ ls -l
r = range(10)
$ grep foo bar.data
pass
print "bye"


 >>> cat edit.sed
s/^\$ \(.*\)/Run("\1")/


 >>> sed -f edit.sed data.txt
print "hello"
Run("ls -l")
r = range(10)
Run("grep foo bar.data")
pass
print "bye"

-John



More information about the Python-list mailing list