473,748 Members | 10,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global package variable, is it possible?

Hello fellow pythoneers. I'm stumped on something, and I was hoping
maybe someone in here would have an elegant solution to my problem.
This is the first time I've played around with packages, so I'm
probably misunderstandin g something here...

Here's what I'd like to do in my package. I want my package to use a
configuration file, and what I'd like is for the config file to appear
magically in each module so I can just grab values from it without
having to load and parse the config file in each package module. Not
quite understanding how the __init__.py file works, I expected it to
be as easy as just setting up the ConfigParser object and then I
figured (since a package is a module) that it would now be global to
my package and I could access it in my modules, but I was wrong... I
don't want to have to pass it in to each module upon init, or anything
lame like that. A main reason for this is that I'd like to be able to
reload the config file dynamically and have all my modules
automatically receive the new config. There must be a way to do this,
but seeing how __init__.py's namespace is not available within the
package modules, I don't see a simple and elegant way to do this.
Does anybody have any suggestions? Thanks!

Aug 3 '07 #1
12 7933

Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
....
# other module
from configure import *

# now I'm able to use value1 value2 etc.
var = value1 * value2

bye
Aug 3 '07 #2
On Fri, 2007-08-03 at 17:51 +0000, Fabio Z Tessitore wrote:
Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
...
# other module
from configure import *

# now I'm able to use value1 value2 etc.
var = value1 * value2
Right idea, wrong execution. Note that the OP said "I'd like to be able
to reload the config file dynamically and have all my modules
automatically receive the new config."

"from configure import *" will import the settings into the current
namespace, and subsequent changes in the original namespace will, in
general, not have any effect in the current namespace.

This should do the trick:

# module configure.py
value1 = 10
value2 = 20
....

# other module
import configure

var = configure.value 1 * configure.value 2

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 3 '07 #3
On Aug 3, 10:51 am, Fabio Z Tessitore <fabioztessit.. .@libero.it>
wrote:
Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
...

# other module
from configure import *

# now I'm able to use value1 value2 etc.
var = value1 * value2

bye

Thanks for the response Fabio. I thought about this, but I don't
think this will work well in my situation either. Take a look at the
following:

__init__py:
############### ########
_default_cfgfil e = 'config.conf'
from configure import *
cfg = loadcfg(_defaul t_cfgfile)

import pkg_module1
import pkg_module2
# EOF
configure.py:
############### #########
from ConfigParser import SafeConfigParse r

def loadcfg(filenam e):
file = open(filename)
cfg = SafeConfigParse r()
cfg.readfp(file )
return cfg
# EOF

pkg_module1:
############### #########
_default_cfgfil e = 'config.conf'
from configure import *
cfg = loadcfg(_defaul t_cfgfile)
# EOF
One problem I see with this approach is that we must define the
configuration file in every module. Alternatively a better approach
would be to only define the configuration file within configure.py,
however this also seems less than ideal. I don't like it because I
believe something as important as the location of the package
configuration file, used by all modules, should defined in __init__
not tucked away in one of it's modules.

Another problem is that after the package is loaded and we want to
load in a new config file, what will happen? With this approach we'll
have to reload every module the package uses for them to read the new
config. And even then, how do we tell each module what the new config
file is? If I did define the configuration file in configure.py then
I suppose what I could do is set a cfgfile variable in configure.py to
the new file location and reload all the modules. If there is no
global package variables, then maybe this isn't so bad...

Hmm. So maybe something like this makes sense:

__init__py:
############### ########
_default_cfg_fi le = 'config.conf'

import configure
def loadcfg(filenam e):
configure.cfgfi le = filename
try:
reload(pkg_modu le1)
reload(pkg_modu le2)
except NameError:
pass
cfg = loadcfg(_defaul t_cfg_file)

import pkg_module1
import pkg_module2
# EOF

confgure.py:
############### ########
cfgfile = None
def loadcfg()
global cfgfile
if not cfgfile:
return None
file = open(cfgfile)
cfg = SafeConfigParse r()
cfg.readfp(file )
return cfg
# EOF

pkg_module1:
############### ########
import configure
cfg = configure.loadc fg()
# EOF

It's a little bit convoluted, but I think it solves most of my
gripes. Anybody have a better idea of how to do this? Thanks again
Fabio.

Aug 3 '07 #4
Hmm. So maybe something like this makes sense:
>
__init__py:
############### ########
_default_cfg_fi le = 'config.conf'

import configure
def loadcfg(filenam e):
configure.cfgfi le = filename
try:
reload(pkg_modu le1)
reload(pkg_modu le2)
except NameError:
pass
cfg = loadcfg(_defaul t_cfg_file)

import pkg_module1
import pkg_module2
# EOF

confgure.py:
############### ########
cfgfile = None
def loadcfg()
global cfgfile
if not cfgfile:
return None
file = open(cfgfile)
cfg = SafeConfigParse r()
cfg.readfp(file )
return cfg
# EOF

pkg_module1:
############### ########
import configure
cfg = configure.loadc fg()
# EOF

It's a little bit convoluted, but I think it solves most of my
gripes. Anybody have a better idea of how to do this? Thanks again
Fabio.
Ugh... I wasn't thinking... Of course this won't work either for the
same reasons above. changing configure.cfgfi le from __init__.py will
have no effect on the separate configure instances loaded in other
modules. I still don't understand why python's __init__.py namespace
isn't global to all modules in the package. Is this a feature, to
prevent sloppy code? I think there are certain instances when it
would make sense.

Aug 3 '07 #5
On Aug 3, 11:16 am, Carsten Haese <cars...@uniqsy s.comwrote:
On Fri, 2007-08-03 at 17:51 +0000, Fabio Z Tessitore wrote:
Heve you tried to do something like:
# module configure.py
value1 = 10
value2 = 20
...
# other module
from configure import *
# now I'm able to use value1 value2 etc.
var = value1 * value2

Right idea, wrong execution. Note that the OP said "I'd like to be able
to reload the config file dynamically and have all my modules
automatically receive the new config."

"from configure import *" will import the settings into the current
namespace, and subsequent changes in the original namespace will, in
general, not have any effect in the current namespace.

This should do the trick:

# module configure.py
value1 = 10
value2 = 20
...

# other module
import configure

var = configure.value 1 * configure.value 2

HTH,

--
Carsten Haesehttp://informixdb.sour ceforge.net
It does help thanks. Okay now after reading your post I really need
to do some experimenting. I was under the impression when I wrote my
last post that changing an attribute in one modules instance in
__init__.py would not effect other modules. Sorry, I'm relatively new
to python, and things like this are still sometimes "gotchas!" for me.

Aug 3 '07 #6
Il Fri, 03 Aug 2007 14:16:59 -0400, Carsten Haese ha scritto:
Right idea, wrong execution. Note that the OP said "I'd like to be able
to reload the config file dynamically and have all my modules
automatically receive the new config."
You are obviously right ... I haven't read all the post, sorry!

Only for knowing more about modules: is there a way to dinamically reload
an already imported module?

bye
Fabio
Aug 3 '07 #7
Only for knowing more about modules: is there a way to dinamically reload
an already imported module?

bye
Fabio
Yeah you can reload modules with the reload builtin function.

Aug 3 '07 #8
Chris Allen a écrit :
Hello fellow pythoneers. I'm stumped on something, and I was hoping
maybe someone in here would have an elegant solution to my problem.
This is the first time I've played around with packages, so I'm
probably misunderstandin g something here...

Here's what I'd like to do in my package. I want my package to use a
configuration file, and what I'd like is for the config file to appear
magically in each module so I can just grab values from it without
having to load and parse the config file in each package module. Not
quite understanding how the __init__.py file works, I expected it to
be as easy as just setting up the ConfigParser object and then I
figured (since a package is a module) that it would now be global to
my package and I could access it in my modules, but I was wrong... I
don't want to have to pass it in to each module upon init, or anything
lame like that. A main reason for this is that I'd like to be able to
reload the config file dynamically and have all my modules
automatically receive the new config. There must be a way to do this,
but seeing how __init__.py's namespace is not available within the
package modules, I don't see a simple and elegant way to do this.
Does anybody have any suggestions? Thanks!
Hi Chris...
I've read all the thread, and it seems that your problem is mostly to
share a single dynamic state (the config) between several modules. So I
do wonder: have you considered the use of the Singleton pattern (or one
of it's variants...) ?
Aug 6 '07 #9
Bruno Desthuilliers <br************ ********@wtf.we bsiteburo.oops. comwrites:
I've read all the thread, and it seems that your problem is mostly
to share a single dynamic state (the config) between several
modules. So I do wonder: have you considered the use of the
Singleton pattern (or one of it's variants...) ?
Python modules are effectively singletons. So the idiomatic way to do
this is to create a module for configuration (perhaps named 'config'),
import that into every other module that needs it, and use its
attributes.

--
\ "[On the Internet,] power and control will shift to those who |
`\ are actually contributing something useful rather than just |
_o__) having lunch." -- Douglas Adams |
Ben Finney
Aug 6 '07 #10

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

Similar topics

2
3066
by: Otto Wyss | last post by:
I have a string variable g_appname which should be global accessable by any class who whishes. I have defined it as wxString *g_appname = NULL; and fills it later on. Now a friend asked why I didn't write wxString g_appname = "Name"; I had to answer "I don't know", I just had the impression in global it
1
5831
by: Andy | last post by:
We are running a DTS package with the dtsrun utility and would like to pass a variable through it. Inside our package we have a VB script that references a table that contains the information about all of our jobs and we would like to pass a variable to tell it which jobs to run. The reason we cannot just add the variable to the VB script, i.e. in a where clause, is because we would like more then 1 bat file to run this package. We want...
1
2016
by: B | last post by:
Using SQL2000, I have a DTS that takes data from MySQL to sqlserver, the catch is I want to specify a specific range of dates. How to use a global variable? At the moment I manually changes the dates and jobs run on a daily basis. sample sql statement from Mysql connection: select * from Table1 where date between '1/1/2004' and '6/30/2004'
12
1838
by: Zeppelin | last post by:
Hi, I have to port from asp a script that adds an ip to a list in plain text or an array. I need not using a database or filesystem, it has to be stored in memory in order that giving us best performance (it should be accessed continuously). I've tried defining $_ENV vars, using apache_setenv(..), creating a class with a static var... but nothing results.
2
1888
by: Christoph Haas | last post by:
Evening, I'm currently working on a larger Python project that consists of multiple programs and packages. As I need a few utility functions time and again I moved them all into a Utility package and created a class there. Like this: Util.py: ~~~~~~~~ class Util: def __init__(self, debugFlag=False, vibranceLevel='good'):
5
11824
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
1
29374
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
1466
by: sandeepthomas | last post by:
Friends, I'm in a big issue now. I have to execute some dts packages usig VB.NET. So I've created the packages(one for sql to sql, one for sql to oracle) and declare the necessary global variables both in these and I've pass these credentials from a config file in my application. But the SQL to SQL package works perfectly, and it will takes the global variable values from my application and work with that info. But the sql oracle package...
112
5460
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8830
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
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
2
2782
muto222
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.