how to generate code in XP

Asked by harikishore

i want generate the code while sikuli is running

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

3 most usable possibilities:

--- eval("some text that forms an expression")
the string is evaluated as an expression and the result is returned.
# randomly add, subtract, multiply and divide a and b
import random
a = 7; b = 5
v1 = "a" ; v2 = "b"
op = '+-*/'
expr = v1 + random.choice(op) + v2 # the expression
print expr, '=', eval(expr) # gets evaluated

--- exec
processes a line of code (more than one statement seperated by ; (semicolon)

import random
a = 7; b = 5
v1 = "a" ; v2 = "b"
op = '+-*/'
stmt = "c =" + v1 + random.choice(op) + v2 # the expression
stmt += "; print c"
print stmt
exec stmt

--- execfile("path-to-a-file-containing-python-code")
the file is processed line by line using exec
so you may have a program, that generates a different program by writing it to a file, that is then processed using execfile

More can be found in the offIcial Python guides (http://docs.python.org/)

Can you help with this problem?

Provide an answer of your own, or ask harikishore for more information if necessary.

To post a message you must log in.