Dynamic/runtime method creation (code generation) in Python

Based on Theran’s code, but extending it to methods on classes: class Dynamo(object): pass def add_dynamo(cls,i): def innerdynamo(self): print “in dynamo %d” % i innerdynamo.__doc__ = “docstring for dynamo%d” % i innerdynamo.__name__ = “dynamo%d” % i setattr(cls,innerdynamo.__name__,innerdynamo) for i in range(2): add_dynamo(Dynamo, i) d=Dynamo() d.dynamo0() d.dynamo1() Which should print: in dynamo 0 in dynamo 1

Using Quotes within getRuntime().exec

Use this: Runtime.getRuntime().exec(new String[] {“sh”, “-l”, “-c”, “./foo”}); Main point: don’t put the double quotes in. That’s only used when writing a command-line in the shell! e.g., echo “Hello, world!” (as typed in the shell) gets translated to: Runtime.getRuntime().exec(new String[] {“echo”, “Hello, world!”}); (Just forget for the moment that the shell normally has a builtin … Read more

Cannot change global variables in a function through an exec() statement?

Per the docs, the exec statement takes two optional expressions, defaulting to globals() and locals(), and always performs changes (if any) in the locals() one. So, just be more explicit/specific/precise…: >>> def myfunc(): … exec(‘myvar=”boooh!”‘, globals()) … >>> myfunc() >>> myvar ‘boooh!’ …and you’ll be able to clobber global variables to your heart’s contents.

How get exec task output with msbuild

Good news everyone! You can now capture output from <Exec> as of .NET 4.5. Like this: <Exec … ConsoleToMSBuild=”true”> <Output TaskParameter=”ConsoleOutput” PropertyName=”OutputOfExec” /> </Exec> Simply: Add ConsoleToMsBuild=”true” to your <Exec> tag Capture the output using the ConsoleOutput parameter in an <Output> tag Finally! Documentation here

Runtime.exec on argument containing multiple spaces

Ok, this is not simply an update but also an answer so I’m filing it as one. According to all information I could find, the following should theoretically do it: String[] cmd = {“explorer.exe”, “/select,\”C:\New”, “”, “”, “”, “”, “”, “”, “Folder\file.txt\””}; The multiple spaces have been broken into empty strings and the array version … Read more