473,386 Members | 1,830 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,386 software developers and data experts.

exec code in "globaldict"

Hi!
I kept searching my online and offline python ressources to figure out
how to execute some code in the global dictionary. Basically I'm reading
a options file that defines some variables. I want to access those
variables throughout my whole project, so I need them in the global
dictionary. Now I think I'm plainly too stupid to figure out what to
write in place of the word "globaldict" to have my definitions in this dict.
Thanks for any kind of help.
cheers, Gerald
Jul 18 '05 #1
2 1407

Gerald Senarclens de Grancy <sa***@sbox.tugraz.at> wrote:

Hi!
I kept searching my online and offline python ressources to figure out
how to execute some code in the global dictionary. Basically I'm reading
a options file that defines some variables. I want to access those
variables throughout my whole project, so I need them in the global
dictionary. Now I think I'm plainly too stupid to figure out what to
write in place of the word "globaldict" to have my definitions in this dict.


There is not technically a fully 'global dictionary', but
__builtins__.__dict__ comes close. Be careful with it.

What is better is to just have a preferences module of some type...

#preferences.py
pref1 = 'hello'
...

#in every other module
import preferences

preferences.pref1
...

- Josiah

Jul 18 '05 #2

"Gerald Senarclens de Grancy" <sa***@sbox.tugraz.at> wrote in message
news:Ni******************@news.chello.at...
Hi!
I kept searching my online and offline python ressources to figure out
how to execute some code in the global dictionary.
Perhaps you are thinking of builtins. If so, leave it alone.
Basically I'm reading a options file that defines some variables. I want
to access those variables throughout my whole project, so I need them in
the global dictionary.


Each module has its own global dict. For what you want, define a module
globals.py or options.py or config.py or whatever, have that module read
the options, and then import that module into all other modules.

Terry J. Reedy

Jul 18 '05 #3

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

Similar topics

5
by: Toby Donaldson | last post by:
Hi all, I'm designing an educational application that will run Python code and check the output against a pre-define answer. I want to use the "exec" statement to run the code, but I don't know...
1
by: berklee just berklee | last post by:
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...
20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
4
by: Irmen de Jong | last post by:
Hello, I don't understand why the following doesn't work. What I want to do is dynamically import some generated Python code and I'm doing this using compile and exec'ing it in the dict of a new...
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: arice | last post by:
I need to create a timer and begin processing tasks in my asp.net code when IIS starts or restarts. I do not know of any way to execute even my global procedures without some external application...
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()
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: Stef Mientki | last post by:
Terry Reedy wrote: The locals of the code block where the instance of this class is created. No I think it's indeed handled as function call. I now that, because the globals (and sometimes ??? the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.