473,405 Members | 2,187 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,405 software developers and data experts.

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 misunderstanding 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 7901

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.value1 * configure.value2

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_cfgfile = 'config.conf'
from configure import *
cfg = loadcfg(_default_cfgfile)

import pkg_module1
import pkg_module2
# EOF
configure.py:
########################
from ConfigParser import SafeConfigParser

def loadcfg(filename):
file = open(filename)
cfg = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
########################
_default_cfgfile = 'config.conf'
from configure import *
cfg = loadcfg(_default_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_file = 'config.conf'

import configure
def loadcfg(filename):
configure.cfgfile = filename
try:
reload(pkg_module1)
reload(pkg_module2)
except NameError:
pass
cfg = loadcfg(_default_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 = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
#######################
import configure
cfg = configure.loadcfg()
# 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_file = 'config.conf'

import configure
def loadcfg(filename):
configure.cfgfile = filename
try:
reload(pkg_module1)
reload(pkg_module2)
except NameError:
pass
cfg = loadcfg(_default_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 = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
#######################
import configure
cfg = configure.loadcfg()
# 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.cfgfile 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...@uniqsys.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.value1 * configure.value2

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.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 misunderstanding 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.websiteburo.oops.comwr ites:
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
On Aug 6, 12:41 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
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 misunderstanding 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...) ?
Thanks, I don't know anything about python singletons. But I'll look
it up.

Aug 6 '07 #11
On Aug 6, 2:27 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
Bruno Desthuilliers <bruno.42.desthuilli...@wtf.websiteburo.oops.comwr ites:
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
Yes, that's what I ended up doing. Which is creating a configure
module and then importing it into my modules that use it. I did
something very similar to the code I posted above.
Aug 6 '07 #12
Chris Allen a écrit :
On Aug 6, 12:41 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
>Chris Allen a écrit :
(snip)
>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...) ?

Thanks, I don't know anything about python singletons.
It's a design pattern, so it's not realluy language specific. I thought
about this because some of your wordings in various posts in this thread
clearly called for such a pattern, but Ben is right, it won't probably
buy you more than a plain python module (which are one of the possible
implementations of the Singleton pattern).
Aug 7 '07 #13

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

Similar topics

2
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...
1
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...
1
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...
12
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...
2
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...
5
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...
1
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...
0
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...
112
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...
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: 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
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
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,...
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.