Problem in designing a global directory in python

T

Tian

I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx

class Context:
def __init__(self):
def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx
---------------------

init() is called somewhere at the beginning of the program.
In other modules, i want to use this in the following manner,

from utils import *
getContext().set(...)

but SOMETIMES I met following error located in "getContext()"
NameError: global name 'sysctx' is not defined

I found that when a module is in the same directory as utils.py, when I
can simply use "utils" for importing, there is no such problem. But
when i was writing a module in a deeper directory than utils.py, which
has to use the full module name for importing, such as:

from myproj.utils import *
getContext().set(...)

I got this error.

What should I do to correct that?
 
F

F. Petitjean

Le 29 Mar 2005 09:50:46 -0800, Tian a écrit :
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
# you are in the global scope of the utils module. This "global sysctx"
# has no meaning, replace by
sysctx = None # create a global 'sysctx' name in utils namespace
class Context:
class Context(object): # why not use "new-style" classes, we are in
2005
def __init__(self):
# I suppose that there is some __doc__ and code :)
def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx
---------------------

init() is called somewhere at the beginning of the program.
In other modules, i want to use this in the following manner,

from utils import *
Please do not use the from module import * form
from utils import getContext
getContext().set(...)
You can also restrict the exported names of utils.py by adding a
__all__ = ('getContext',)
in utils.py.
but SOMETIMES I met following error located in "getContext()"
NameError: global name 'sysctx' is not defined

I found that when a module is in the same directory as utils.py, when I
can simply use "utils" for importing, there is no such problem. But
when i was writing a module in a deeper directory than utils.py, which
has to use the full module name for importing, such as:

from myproj.utils import *
getContext().set(...)

I got this error.

What should I do to correct that?
See above :) and post a follow-up to report if the issue is solved.PS sorry for bad english. I am not a native speaker.
 
T

Tian

I have tried using "sysctx=None" instead of "global sysctx", but it
doesn't work either.
It seems my initialization work in the previous calling of init() has
no persistent effect when "utils" is imported using "from myproj.utils
import getContext".

What's weird, when a module is in the same directory as utils.py, where
I can simply use "utils" for importing, there is no such problem.

Any other suggestions?
 
B

Bruno Desthuilliers

Tian a écrit :
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx

class Context:
def __init__(self):
def set(self, name, obj, overwrite=True):
def get(self, name):
def has(self, name):

def init():
global sysctx
sysctx = Context()

def getContext():
global sysctx
return sysctx

Why using a (not so) global variable here ? If your problem is to make
sure you have only one instance of Context in your program, there are
cleaner solutions - one of them being a Singleton (you'll find all
needed doc and exemples on the net - google is your friend !-).
 
T

Tian

I googled about how to write singleton in python, but even if I use
Singleton, in which module's namespace should I keep the instance of
this singleton? suppose I have a singleton class definiton in
"utils.py", how should I import and where should I make instance and
initialize? Thanks!!
 
T

Terry Reedy

Tian said:
I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type

I suspect that you would accomplish your goal much more easily by calling
your module 'context' or something like that, importing it into every other
module that needs it, and then accessing values as attributes of context.
Don't reinvent the module ;-)

other.py

import context

context.a = 3

x = context.b # where context.b is previous set somewhere else

If you need multiple shared contexts, people subclass object

class context(object): pass

context1 = context()
context2 = context()

# now set and get attributes as needed

Terry J. Reedy
 
S

Serge Orlov

Tian said:
I have tried using "sysctx=None" instead of "global sysctx", but it
doesn't work either.
It seems my initialization work in the previous calling of init() has
no persistent effect when "utils" is imported using "from
myproj.utils import getContext".

What's weird, when a module is in the same directory as utils.py,
where I can simply use "utils" for importing, there is no such
problem.

Any other suggestions?

put the following print statement next to every "global sysctx"
replacing ... with the function name where the statement is located.

print "... globals are in %s" % __name__

Serge.
 
B

Bruno Desthuilliers

Tian a écrit :
I googled about how to write singleton in python, but even if I use
Singleton, in which module's namespace should I keep the instance of
this singleton?

You found the doc but I'm afraid you did not grasp the concept.

You don't have to 'keep the instance' anywhere - it's the job of the
singleton to do this. The principle of the Singleton is that you can
have *only one* instance of it.
 
B

Bruno Desthuilliers

'@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) a écrit :
noob warning:
what is so wonderful about the NEW class over the old ?

A whole lot of things. But the main thing to know is that old-style
classes are deprecated, and will disappear in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top