Where is quote (again)?
N Becker
nbecker at fred.net
Fri Mar 8 13:18:41 EST 2002
nbecker at fred.net (N Becker) wrote in message news:<1f5252d4.0203080721.3bfc5c14 at posting.google.com>...
> Back in 1999, I asked "where is quote?". I was looking for
> a way to quote a function (delay evaluation).
>
> I wonder if there is anything new. It looks to me that the
> best answer is to use lambda.
>
> example:
>
> def f():
> return lambda: os.chmod(""/root/.rhosts", 0644)
>
> def g():
> f()
A little background. I'm writing a script that customizes my Linux
machine installations. The script has a function that executes
commands.
Each command is a list. Each element of the list is a string
or a function.
The current setup is, if a command is a string it is sent to
commands.getstatusoutput. Some commands can be directly executed by
the python
interpreter. In that case the command is a function and is directly
called.
This is basically the outline of the script:
run a command
def runcommand (cmd):
print "command: ", cmd
(status, output) = commands.getstatusoutput (cmd)
# print "status, output: " , (status, output)
if (int (status) != 0):
print "command failed: ", cmd, output
return int (status)
#run a command and record result
def runandlog (func):
commandlist = func()
for command in commandlist:
if inspect.isfunction (command):
command()
else:
status = runcommand (command)
return status
def rootaccess():
return ['some command deleted',
'another command not shown here',
lambda: os.chmod ("/root/.rhosts", 0644)
]
dolist = [rootaccess, stuff deleted...]
for thing in dolist:
status = runandlog (thing)
As you see "dolist" is a list of functions to execute. Each function
returns a list of commands. In the case that a command is a function
to be executed by the python interpreter, the function has to be
quoted.
The best Python mechanism I could find is lambda. Is there a better
way?
More information about the Python-list
mailing list