473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

includes on an exec?

I want to run a code module by loading the code as any other text file, and
then calling exec() on its contents.

For some reason, Python doesn't seem to find the includes that are
referenced in the script being called.

I tried dropping it into the Python 'lib' folder, and in the same folder as
my script, neither seems to work?

Any suggestions?
Jul 18 '05 #1
1 2700
"berklee just berklee" <berklee(@)hotmail.com> wrote in message news:<Q5*******************@read1.cgocable.net>...
I want to run a code module by loading the code as any other text file, and
then calling exec() on its contents.

For some reason, Python doesn't seem to find the includes that are
referenced in the script being called.

I tried dropping it into the Python 'lib' folder, and in the same folder as
my script, neither seems to work?

Any suggestions?


Here is some code that I use:

# class for compiling and using python code (Text)
class PyCode:
def __init__(self):
self.code = None
self.module = None

# compile module from file
def fromFile(self, filename):
f = open(filename, 'r')
text = f.read()
f.close()
return self.CompileModule(GetFilenameBase(filename), text)

# compile to a module (with name moduleName) from text (code)
def CompileModule(self,moduleName, text):
# remove all cariage returns (compile does not like it)
text = string.replace(text, '\r', '')

# make sure there is a line feed at the end
# compile does not like it if there is not
if text and text[-1] != '\n':
text += '\n'
co = compile(text, '', 'exec')

# create a new module and exec the code object into the dict
import imp
codeModule = imp.new_module(moduleName)
exec co in codeModule.__dict__
return codeModule

# clear the compiled code
def SetCode(self, code, bCheckDiff = 1):
code = string.strip(code)
if (bCheckDiff and code != self.code) or not bCheckDiff:
self.code = code
if self.code:
self.module = self.CompileModule('CodeEdit', self.code)
else:
self.module = None

# exec code in the module
def execCode(self, code_str):
exec code_str in self.module.__dict__
************************************************** ******
to Use:
pc = PyCode()
module = pc.fromFile('Test.py')
# now module should act as any other module object

Hope this Helps,
Brent
Jul 18 '05 #2

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

Similar topics

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--
1
by: William Ryan | last post by:
Well, that depends. http://www.amazon.com/exec/obidos/tg/detail/- /0672320681/qid=1056861878/sr=1-1/ref=sr_1_1/002-7953039- 7491213?v=glance&s=books Is a must read IMHO for any ASP.NET...
8
by: Alan Howard | last post by:
I've just been caught out by a host that doesn't support #includes in my ASPs. What's the best strategy for replacing these calls to files that include everything from subs and functions, global...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
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
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
1
by: Daniel | last post by:
I have a stored procedure in SQL Server 2005 Express. It includes a distributed query that pulls from a table in a local SQL Server database, and combines this with some data in a remote Access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.