How to access object attributes given a string

S

Santiago Romero

Hi...

I'm trying to guess how to access attributes of an existing object
given the attribute name in a string. I mean:

class Object:
self.x = 12
self.y = 20
self.name = "blah"

def ChangeAttribute( object, attribute, value ):
# Insert here the code for object.attribute = value
XXXXX

Allowing this kind of calls:

ChangeAttribute( object, "x", 200 )
ChangeAttribute( object, "name", "my name" )

Thanks.

PS: I need it for a concrete case in a game scripting language I'm
writing, so that I can call functions like "CHANGE_PLAYER_VALUES( "x",
100 )".
 
C

Chris

Hi...

I'm trying to guess how to access attributes of an existing object
given the attribute name in a string. I mean:

class Object:
self.x = 12
self.y = 20
self.name = "blah"

def ChangeAttribute( object, attribute, value ):
# Insert here the code for object.attribute = value
XXXXX

Allowing this kind of calls:

ChangeAttribute( object, "x", 200 )
ChangeAttribute( object, "name", "my name" )

Thanks.

PS: I need it for a concrete case in a game scripting language I'm
writing, so that I can call functions like "CHANGE_PLAYER_VALUES( "x",
100 )".

help(setattr)

Help on built-in function setattr in module __builtin__:

setattr(...)
setattr(object, name, value)

Set a named attribute on an object; setattr(x, 'y', v) is
equivalent to
``x.y = v''.
 
D

Dennis Kempin

Santiago said:
Hi...

I'm trying to guess how to access attributes of an existing object
given the attribute name in a string. I mean:

class Object:
self.x = 12
self.y = 20
self.name = "blah"

def ChangeAttribute( object, attribute, value ):
# Insert here the code for object.attribute = value
XXXXX

have a look a getattr and setattr
setattr(object, "name", "value")
Allowing this kind of calls:

ChangeAttribute( object, "x", 200 )
ChangeAttribute( object, "name", "my name" )

Thanks.

PS: I need it for a concrete case in a game scripting language I'm
writing, so that I can call functions like "CHANGE_PLAYER_VALUES( "x",
100 )".
You are using a scripting language.. why not use python directly? You
can pass commands from the game console via the exec statement, and have
the complete power of python in your game scripts

greetings,
Dennis
 
G

Gary Herron

Santiago said:
Hi...

I'm trying to guess how to access attributes of an existing object
given the attribute name in a string. I mean:

class Object:
self.x = 12
self.y = 20
self.name = "blah"

def ChangeAttribute( object, attribute, value ):
# Insert here the code for object.attribute = value
XXXXX

Allowing this kind of calls:

ChangeAttribute( object, "x", 200 )
ChangeAttribute( object, "name", "my name" )

Thanks.

PS: I need it for a concrete case in a game scripting language I'm
writing, so that I can call functions like "CHANGE_PLAYER_VALUES( "x",
100 )".
You want getattr and setattr:
setattr(ob, 'blah', 123)
and
getattr(ob, 'blah')

Gary Herron
 
B

bruno.desthuilliers

Santiago Romero schrieb:
(snip - others already answered)
You are using a scripting language.. why not use python directly?

I can only second this suggestion - FWIW, there are already quite a
couple popular (and less popular) games using Python as a scripting
language.
 
S

Santiago Romero

def ChangeAttribute( object, attribute, value ):
help(setattr)

Help on built-in function setattr in module __builtin__:

setattr(...) setattr(object, name, value)

Set a named attribute on an object; setattr(x, 'y', v) is
equivalent to`x.y = v''.
and

Gary Herron write:
You want getattr and setattr:

Thanks a lot, that's what I was looking for.
Dennis Kempin wrote:
You are using a scripting language.. why not use python directly?

Well, I have a .OBJ with something like:

# exec is the code executed when the player gets the object
ADDOBJECT:coin:x=10:y=10:screen=200:exec=\
SETMAP(10,10,1);\
SLEEP(1);\
PLAYSOUND(get_coin);\
IF FLAG(10);\
SETLOOKING(left);\
PLAYERSAY(blah);\
SLEEP(2);\
ELSE;\
PLAYERSAY(blahblah);\
SLEEP(3);\
ENDIF;\

That's a simple example of the language I've wrote for my game. Now
it's being "compiled" nicely and it works.

I just want to execute an small set of commands (PLAYSOUND, PLAYMUSIC,
WALKTO, PLAYERSAY, SLEEP and a couple more) ... do you think I can
write python code inside my object.exec (list attribute) loaded from a
file?

Currently I'm compiling the above in a ( scope_level, function,
parameters ) list:

object.exec = [
[ 0, "SETMAP", [10, 10, 1] ],
[ 0, "SLEEP", [1] ],
[ 0, "PLAYSOUND", ["get_coin"] ],
[ 0, "IF FLAG", [10] ],
[ 1, "SETLOOKING", ["left"] ],
(...) ]

I make some checks (as many resulting opcodes as input commands,
check of IF/ELIF/ELSE/ENDIF statements to see if all are correct,
right parameter types (currently only integer or strings), right
parameter number, and so on.

Do you mean that I can replace my "language" by just python (defining
SETMAP, SLEEP and all those functions somewhere in my main source
code).
 
S

Steve Holden

Santiago said:
Hi...

I'm trying to guess how to access attributes of an existing object
given the attribute name in a string. I mean:

class Object:
self.x = 12
self.y = 20
self.name = "blah"

def ChangeAttribute( object, attribute, value ):
# Insert here the code for object.attribute = value
XXXXX

Allowing this kind of calls:

ChangeAttribute( object, "x", 200 )
ChangeAttribute( object, "name", "my name" )

Thanks.

PS: I need it for a concrete case in a game scripting language I'm
writing, so that I can call functions like "CHANGE_PLAYER_VALUES( "x",
100 )".

Look in the documentation for gettattr() and setattr().

regards
Steve
 
S

Santiago Romero

Before I reinvent the wheel, I'm going to post the code.

Feel free to give any advice, and think that I'm new to python, it's
only 1 month since I began programming in python "seriously" (up to
that moment, I wrote just a few log-text parsing system administration
scripts to speed up some old bash scripts).

First of all, my currently test file:

KILLOBJECT(coin);\
REM Wait 5 seconds;\
IF FLAG(5);\
SLEEP(5);\
PLAYERSAY(test,5);\
SETMAP(10,10,top,5);\
IF FLAG(6);\
NOP();\
SLEEP(7);\
ELIF FLAG(7);\
NOP();\
SLEEP(9);\
ELSE;\
SLEEP(999);\
ENDIF;\
IF FLAG_VALUE(7,1);\
CHANGESCREEN(start,10,100);\
PLAYERFACING(0);\
ELSE;\
PLAYERFACING(1);\
ENDIF;\
ENDIF;\
SLEEP(12);\
SLEEP(11)

And the parse code:


if __name__ == "__main__":
"""
To test: create an example file test.par with language commands.
"""

numline = 0
try:
fp = open("test.par")
except:
print "Error, cant open test.par"
import sys
sys.exit(0)

while 1:
# Read line, ignore comments and stop with EOF
numline += 1
line = fp.readline()
if line == '': break

line = line.strip()
startline = numline
# Join lines splitted with \ in the text file
while line.endswith("\\"):
next_line = fp.readline()
if next_line == '': break
if next_line.strip() == '': continue
numline += 1
next_line = next_line.strip()
line = line[:-1] + next_line

code = ExecParser_Parse( "exec", line, startline, "test.par" )

for i in 0, 1:
ExecParser_PrintCode( code, i )
print
ExecParser_Exec( 1, code, 0, debug=1 )


And this is the output:
Compiled opcodes: <

ExecType CodeLevel Opcode
----------------------------------------------------------
1 0 KILLOBJECT ['coin']
0 0 REM ['Wait 5 seconds']
1 0 IF FLAG [5]
1 1 SLEEP [5]
1 1 PLAYERSAY ['test', 5]
1 1 SETMAP [10, 10, 'top', 5]
1 1 IF FLAG [6]
1 2 NOP
1 2 SLEEP [7]
1 1 ELIF FLAG [7]
1 2 NOP
1 2 SLEEP [9]
1 1 ELSE
1 2 SLEEP [999]
1 1 ENDIF
1 1 IF FLAG_VALUE [7, 1]
1 2 CHANGESCREEN ['start', 10, 100]
1 2 PLAYERFACING [0]
1 1 ELSE
1 2 PLAYERFACING [1]
1 1 ENDIF
1 0 ENDIF
1 0 SLEEP [12]
1 0 SLEEP [11]
1 0 END

Formatted code (parsing opcodes): <

Main:
KILLOBJECT( coin )
REM Wait 5 seconds
IF FLAG( 5 )
SLEEP( 5 )
PLAYERSAY( test, 5 )
SETMAP( 10, 10, top, 5 )
IF FLAG( 6 )
NOP
SLEEP( 7 )
ELIF FLAG( 7 )
NOP
SLEEP( 9 )
ELSE
SLEEP( 999 )
ENDIF
IF FLAG_VALUE( 7, 1 )
CHANGESCREEN( start, 10, 100 )
PLAYERFACING( 0 )
ELSE
PLAYERFACING( 1 )
ENDIF
ENDIF
SLEEP( 12 )
SLEEP( 11 )
END

Executing KILLOBJECT ['coin']
Checking IF FLAG [5] (returned TRUE)
Executing SLEEP [5]
Executing PLAYERSAY ['test', 5]
Executing SETMAP [10, 10, 'top', 5]
Checking IF FLAG [6] (returned FALSE)
Checking ELIF FLAG [7] (returned FALSE)
Entering ELSE
Executing SLEEP [999]
Checking IF FLAG_VALUE [7, 1] (returned FALSE)
Entering ELSE
Executing PLAYERFACING [1]
Executing SLEEP [12]
Executing SLEEP [11]
Executing END
 
S

Santiago Romero

And the big functions:

I imagine that the following is HORRIBLE in the pythonic-vision and
surely can be rewriten with a single map+reduce+filter + 200 lambdas
functions X-D, but I come from C and any advice on how to implement my
"simple scripting language" without using lex or yacc is welcome :)



#--------------------------------------------------------------------
def ExecParser_Parse( key, value, line, file ):
"""
Parses an exec or exec2 line, generating a list of "opcodes".

This function takes an exec= line from a OBJ file and parses it
generating a list of "executable opcodes" in a format executable
by ExecParser_Exec().

The rules for this "small" language are:
- Spaces and tabs are stripped.
- Comments (starting by REM) are ignored.
- A simple set of commands is available. Those commands modify
some game variables or objects. As an example: PLAYSOUND(snd),
plays the sound identified by the sound_tag "snd".
- Commands must be separated by ";" characters.
- Commands can receive parameters enclosed between ( and ) of
types INT or STR.
- Programmer can use "self" to refer to the current object in
functions that accept an object or enemy text id.
- Simple control flow is present with IF, ELIF, ELSE and ENDIF
statements.
- IF and ELIF statemens are followed by a BOOLEAN command, which
will return 0 or 1.

Example ('\' character wraps lines in the OBJ file):

KILLOBJECT(coin);\
REM Wait 5 seconds;\
IF FLAG(5);\
SLEEP(5);\
PLAYERSAY(test,5);\
SETMAP(10,10,top,5);\
IF FLAG(6);\
NOP();\
SLEEP(7);\
ELIF FLAG(7);\
NOP();\
SLEEP(9);\
ELSE;\
SLEEP(999);\
ENDIF;\
IF FLAG_VALUE(7,1);\
CHANGESCREEN(start,10,100);\
PLAYERFACING(0);\
ENDIF;\
ENDIF;\
SLEEP(12);\
SLEEP(11);

This function will parse the exec line and produce as output
opcodes in
this format:

[ type_of_exec, if_level, opcode, parameters ]

type_of_exec = 1 for exec= lines, and 2 for exec2= lines.
if_level is the current code "level" or scope. IF statements
increase
if_level and ENDIF statements decrease it.
opcode and parameters are the function_name and the params for this
command.

Example:

ExecType CodeLevel Opcode and params
----------------------------------------------------------
1 0 KILLOBJECT ['coin']
0 0 REM ['Wait 5 seconds']
1 0 IF FLAG [5]
1 1 SLEEP [5]
1 1 PLAYERSAY ['test', 5]
1 1 SETMAP [10, 10, 'top', 5]
1 1 IF FLAG [6]
1 2 NOP
1 2 SLEEP [7]
1 1 ENDIF
1 0 ENDIF
1 0 END


The function makes some small checkings, like:
count(IF)==count(ENDIFs),
check number of arguments, argument type checking, validate command
functions, and some others, but it does not cover all possible
syntax
errors or typing mistakes.
"""

reserved_words = {
"SETMAP" : ( 4, "int", "int", "str", "int" ),
"KILLOBJECT" : ( 1, "str" ),
"ENABLEOBJECT" : ( 1, "str" ),
"DISABLEOBJECT" : ( 1, "str" ),
"PLAYSOUND" : ( 1, "str" ),
"PLAYMUSIC" : ( 1, "str" ),
"SLEEPCYCLES" : ( 1, "int" ),
"SLEEP" : ( 1, "int" ),
"SHOWTEXT" : ( 1, "str" ),
"SHOWTEXTTIMED" : ( 2, "str", "int" ),
"SHOWSCREEN" : ( 2, "str", "int" ),
"CHANGESCREEN" : ( 3, "str", "int", "int" ),
"ADDINVENTORY" : ( 1, "str" ),
"REMOVEINVENTORY" : ( 1, "str" ),
"OBJECTFACING" : ( 2, "str", "int" ),
"PLAYERFACING" : ( 1, "int" ),
"PLAYERSAY" : ( 2, "str", "int" ),
"OBJECTSAY" : ( 3, "str", "str", "int" ),
"SETFLAG" : (2, "int", "int" ),
"INCFLAG" : (1, "int" ),
"DECFLAG" : (1, "int" ),
"DELEXEC" : (1, "int" ),
"REM" : ( 0, ),
"NOP" : ( 0, ),
"END" : ( 0, ),
"TRUE" : ( 0, ),
"FALSE" : ( 0, ),
"IF" : ( 0, ),
"ELIF" : ( 0, ),
"ELSE" : ( 0, ),
"ENDIF" : ( 0, ),
"FLAG" : ( 1, "int" ),
"NOT_FLAG" : ( 1, "int" ),
"FLAG_VALUE" : ( 2, "int", "int" ),
"PLAYER_HAS" : ( 1, "str" ),
"PLAYER_HAS_NOT" : ( 1, "str" ),
"SCREEN_IS" : ( 1, "str" ),
"SCREEN_IS_NOT" : ( 1, "str" )
}


#input is something like: "exec=COMMAND;COMMAND;COMMAND..."

if key.upper() == "EXEC": exec_type = 1
code = [] # Resulting code

if ";" in value: commands = value.split(";")
else: commands = list( value )

# Filter empty lines
commands = filter( lambda x: len(x) > 1, commands )

code_level = level_inc = 0 # Current scope level
found_if = found_elif = 0

# Parse all commands in the input script
for cmd_counter, cmd in enumerate(commands):

if cmd == '': continue
cmd_upper = cmd.upper()
cmderr = "(line %d:%d) of <%s>." % (line,cmd_counter+1,file)

pos = cmd.find(')')
if pos != -1 and pos != len(cmd)-1:
print "OBJ-exec: Command format error in %s %s" %
(cmd,cmderr)
continue

# Delete spaces, CR and LFs
if not cmd_upper.startswith("REM ") and \
not cmd_upper.startswith("IF ") and \
not cmd_upper.startswith("ELIF "):
for i in " \n\r\t": cmd = cmd.replace(i, "")
cmd_upper = cmd.upper()

# Check special commands (without parameters):

# REM and NOP commands, just add it to the list
if cmd_upper.startswith("REM "):
code.append( ( 0, code_level, "REM", [cmd[4:]] ) )
continue
elif cmd_upper.startswith("NOP"):
code.append( ( exec_type, code_level, "NOP", [] ) )
continue
elif cmd_upper == "END":
code.append( ( exec_type, code_level, "END", [] ) )
continue

# IF commands: increase code score and save the IF statement
# boolean function will be processed later
if cmd_upper.startswith("IF "):
level_inc = 1
found_if = 1
cmd = cmd[2:].strip()
# ELIF: save ELIF statement in previous level and code in next
# boolean function will be processed later
elif cmd_upper.startswith("ELIF "):
level_inc = 1
found_elif = 1
if code_level > 0: code_level -= 1
else: print "OBJ-exec: ELIF without IF %s" %
(cmderr)
cmd = cmd[4:].strip()
# ELSE: same as ELIF, but don't process any boolean function
elif cmd_upper.startswith("ELSE"):
if code_level > 0: code_level -= 1
else: print "OBJ-exec: ELSE without IF %s" %
(cmderr)
code.append( ( exec_type, code_level, "ELSE", [] ) )
code_level += 1
continue
# ENDIF: descend a "scope" level
elif cmd_upper.startswith("ENDIF"):
if code_level > 0:
code_level -= 1
code.append( ( exec_type, code_level, "ENDIF", [] ) )
else:
print "OBJ-exec: ENDIF without IF %s" % (cmderr)
continue

# Process IF, ELIF boolean checks or any other command:

# Commands with parameters, check for () and a function name:
if not '(' in cmd or not ')' in cmd or cmd.startswith("("):
print "OBJ-exec: Unknown command '%s' %s" % (cmd,cmderr)
continue

# Try to get the function name and uppercase it.
try:
rows = cmd.split('(')
function = rows[0].upper()
except:
print "OBJ-exec: command error '%s' %s" % (cmd,cmderr)
continue

# Check if the function name is a valid/existing one:
if function not in reserved_words:
print "OBJ-exec: unknown function call '%s' %s" %
(function,cmderr)
continue

params = []
# Try to get all the present parameters
try:
# Only 1 parameter present:
if not ',' in rows[1]:
params.append(rows[1].split(')')[0])
# More than 1 parameter present, get them:
else:
paramlist = rows[1].split(')')[0]
params.extend(paramlist.split(','))
except:
print "OBJ-exec: error getting params in '%s' %s" %
(function,cmderr)
continue

# Check if the number of parameters is correct:
rightnumparam = reserved_words[function][0]
if len(params) != rightnumparam:
print "OBJ-exec: Found %d parameter(s) in %s instead of %d
%s" \
% (len(params),function,rightnumparam,cmderr)
continue

# Check if all the params are of the right type (int or str)
# Try to convert ints to int, store str as str
for num_param in range(len(params)):
valid_type = reserved_words[function][num_param+1]
if valid_type == "int":
try:
params[num_param] = int(params[num_param])
except:
print "OBJ-exec: Parameter %s should be INT in %s %s" \
% (num_param,function,cmderr)
continue

if found_if == 1:
opcode = ( exec_type, code_level, "IF " + function,
(params) )
found_if = found_elif = 0
elif found_elif == 1:
opcode = ( exec_type, code_level, "ELIF " + function,
(params) )
found_if = found_elif = 0
else:
opcode = ( exec_type, code_level, function, (params) )

code.append(opcode)

# Check if next instructions must be in a different code scope
if level_inc != 0:
code_level += level_inc
level_inc = 0

# Check consistence IF / ENDIF
ifs = len(filter(lambda l: l[2].startswith("IF "), code))
endifs = len(filter(lambda l: l[2].startswith("ENDIF"), code))
if ifs > endifs:
print "OBJ-exec: missing %d ENDIFs in line %s of <%s>." % (ifs-
endifs,line,file)
elif ifs < endifs:
print "OBJ-exec: missing %d IFs in line %s of <%s>." % (endifs-
ifs,line,file)

# Check if we got as many opcodes as commands
num_opcodes = len(code)
num_commands = len(commands)
if num_opcodes != num_commands:
print "OBJ-exec: COMPILE ERROR; got %s opcodes from %s commands
in line %s of <%s>." \
% (num_opcodes,num_commands,line,file)

code.append( (exec_type, 0, "END", []) )
return code



Thanks for any advice :)
 
S

Santiago Romero

And the rest of the code:


#--------------------------------------------------------------------
def ExecParser_Exec( exec_type, code, game, debug=0 ):
"""
Execute the previously "compiled" code:
"""
code_level = 0

#player = game.player
#world = game.world

# Take only opcodes for EXEC or EXEC2, deppending on exec_type
exec_code = filter( lambda x : x[0] == exec_type, code )

i = -1
while 1:

i += 1
cmd_level, cmd, params = exec_code[1:4]
spaces = " "*((code_level+1)*3)

# End of script (appended by the compiler)


if code_level == cmd_level:
if cmd.startswith("IF ") or cmd.startswith("ELIF "):
# Get boolean funtion and evaluate it
# IF true:
# remove all ELIF/ELSE blocks until next ENDIF at the
same code level
# increase codelevel
# IF false:
# remove all code until next ELIF/ELSE/ENDIF at the
same code level
booleanf = cmd.split(" ")[1]
if debug: print "Checking ", spaces, cmd, params,
" (returned",
if ExecParser_CheckBoolean(booleanf, params):
if debug: print "TRUE)"
# Ignore lines until we find an ENDIF at the same code
level:
done = 0
j = i

# Ignore all lines until we found the same code level
(next ELIF/ELSE/ENDIF):
while not done:
j += 1
next_cmd_level, next_cmd = exec_code[j][1:3]
if next_cmd_level==cmd_level or
next_cmd.startswith("END"):
done = 1

nextcond_line = j
# Next branch is an endif, do nothing:
if exec_code[nextcond_line][2].startswith("ENDIF") or \
exec_code[nextcond_line][2] == "END":
pass
# Next branch is ELIF or ELSE: delete all until ENDIF
+cmd_level found
else:
done = 0
while not done:
if exec_code[nextcond_line]
[2].startswith("ENDIF") or \
exec_code[nextcond_line][2] == "END":
done = 1
else:
del exec_code[nextcond_line]

# - Endif -> stop here
code_level += 1
i -= 1
else:
if debug: print "FALSE)"
done = 0
# Ignore all lines in the current
while not done:
i += 1
next_cmd_level, next_cmd = exec_code[1:3]
if (next_cmd.startswith("ELIF ") or
next_cmd.startswith("ELSE")):
i -= 1
done = 1
elif next_cmd.startswith("ENDIF") and
(next_cmd_level == code_level):
done = 1
continue

if cmd.startswith("ELSE") and cmd_level != -1:
if debug: print "Entering ", spaces, "ELSE"
code_level += 1

elif cmd.startswith("ENDIF") and cmd_level != -1:
if code_level > 0: code_level -= 1

else:
if code_level == cmd_level:
if cmd == "END":
if debug: print "Executing", spaces, cmd
return 1
else:
if debug: print "Executing", " "*((code_level+1)*3),
cmd, params
ExecParser_ExecOpcode( cmd, params, game, debug )
return 1



#--------------------------------------------------------------------
def ExecParser_PrintCode( code, formatted=0 ):
"""
Print code compiled/parsed by ExecParser_Parse().

Prints opcodes (formatted=0) or indented source code (formatted=1)
from a previously compiled/parsed opcode list.
"""

if not formatted:
# Print opcodes:
print "\n> Compiled opcodes: <"
print "\nExecType CodeLevel Opcode"
print
"----------------------------------------------------------"
for c in code:
print " %-8s %-8s" % (c[0],c[1]),
for f in c[2:]:
if f != []: print f,
print

else:
# Display source code indented
print "\n> Formatted code (parsing opcodes): <"
print "\nMain:"
for i in code:
spaces = " " * 3*(1+i[1])
func = i[2]
params = [ str(x) for x in i[3] ]
if func == "REM":
print "%s%s %s" % (spaces, func, i[3][0])
else:
if params != []:
print "%s%s(" % (spaces, func),
for i,p in enumerate(params):
if i == len(params)-1: print "%s )" % (p)
else: print "%s," % (p),
else :
print "%s%s" % (spaces, func)



That's all. I can put the .py and test.par files online if anyone
wants to test it and point me to the right direction on "how to code
your own small scripting language written in python scripting
language" X-D
 
G

Gabriel Genellina

I just want to execute an small set of commands (PLAYSOUND, PLAYMUSIC,
WALKTO, PLAYERSAY, SLEEP and a couple more) ... do you think I can
write python code inside my object.exec (list attribute) loaded from a
file?
Do you mean that I can replace my "language" by just python (defining
SETMAP, SLEEP and all those functions somewhere in my main source
code).

Something like that.
Build a dictionary -that will be used as the script global namespace-
containing all the required functions, plus a '__builtins__' entry
(another dict) containing the builtins that you want to provide. For
example, if you don't want to enable "import", don't include __import__.

import __builtin__
builtins = dict(__builtin__.__dict__) # a copy of the standard __builtin__
module namespace
del builtins['__import__']

ns = {
'SETMAP': SETMAP,
'SHOWTEXT': SHOWTEXT,
...
'__builtins__': builtins
}
exec text_of_the_user_script in ns

Warning: this is unsafe!!!. Malicious users could execute arbitrary code;
although this may not be an issue if the game runs on the user's own
system.
The advantage is that you let them use all the power of Python as a
scripting language.
 
S

Santiago Romero

You are using a scripting language.. why not use python directly?
Something like that.

Build a dictionary -that will be used as the script global namespace-
(...)

Warning: this is unsafe!!!. Malicious users could execute arbitrary code;
although this may not be an issue if the game runs on the user's own
system.

The advantage is that you let them use all the power of Python as a
scripting language.

That's why I was trying to write my own "small" scripting
language :).

It works, but I would like to know how to write it better or safer.
That's why I posted the source code.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top