473,471 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Request for exec/namespace advice

Hi everybody
I'm very new to python so please forgive any idiotic mistakes:
I'm writing a MUD-like text game in python and I want to support dynamic
ingame scripting to deal with user commands.
I have a database table containing command strings and python code. When a
player types a command, the program looks up the table for the command and
executes the corresponding python code.
My plan was initally to build a library of useful functions and include
these in a namespace which I'd use with exec to run the python scripts.
I have the following example code(simplified for clarity):

# --- Start code snippet
class scripter:
"""Library of functions for the scripting engine."""
def __init__(self, currentPlayer):
self.me = currentPlayer
def message(self, text):
self.me.write(text)

def runCommand(cmd, user):
"""Carry out an instruction for a particular entity"""

namespace = {"s" : scripter(user)}
exec(cmd, namespace)

# --- end code snippet
Thus I can run scripts such as:
s.message("Hello World.\n")

What I want to do is call the methods without having to qualify them with
the class instance variable(s). I had assumed this would be a trivial matter
of manipulating namespaces but so far it has eluded me.
I have tried things like: (It throws an error)
namespace = scripter.__dict__
namespace.update(scripter().__dict__)
exec(cmd, namespace)

Am I on the wrong track here, and is there a better way to accomplish this?

Note: I realise there are security issues with exec: currently this is just
a fun project for me to learn the language, this is more a question on
namespaces than on pros and cons of exec...

My thanks
John
Jul 18 '05 #1
0 1275

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kepes.krisztian | last post by:
Hi ! In Unifying types and classes in Python 2.2 article I see that: >>> print a # show the result
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
10
by: Antoon Pardon | last post by:
I have the following little piece of code: class Cfg:pass #config = Cfg() def assign(): setattr(config, 'Start' , ) def foo(): config = Cfg()
8
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
3
by: Jens | last post by:
Hi, has anyone an idea why the following code does not work. s = """ def a(n): return n*n
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
0
by: Gabriel Genellina | last post by:
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <akathorn@gmail.com> escribi�: Welcome! You have to pass in the namespace of the desired module - instead of globals. I'd use an...
4
by: Peter Teuben | last post by:
if I define a simple string code, with the following contents: import math def foo(x): return math.sqrt(x) and i run it using exec(code) in python, math is not known. But when I recode the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.