473,545 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1417

Gerald Senarclens de Grancy <sa***@sbox.tug raz.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.pre f1
...

- Josiah

Jul 18 '05 #2

"Gerald Senarclens de Grancy" <sa***@sbox.tug raz.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
5771
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 how to get output from it. For instance, exec works like this: >>> code = """ for i in xrange(1, 5):
1
2713
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 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...
20
5902
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 making comparisons. If you have a file, in Python you could do:
4
2587
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 empty module object. That works okay, but as soon as the generated code tries do perform certain imports, it fails! Certain other imports succeed....
1
2221
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
1952
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 touching my site. Does anyone know a way to cause asp.net code to begin executing prior to an external page or service request? Thanks!
4
2945
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
1085
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 string as: def foo(x):
0
318
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 locals) of the codeblock where the class is initiated is changed. Yes I've already done that, and this yields exactly the same results. So I...
0
7393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7749
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5965
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.