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

Problem in designing a global directory in python

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?

Jul 18 '05 #1
9 1468
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
--------------------
global sysctx # 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.
Jul 18 '05 #2
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?

Jul 18 '05 #3
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 !-).
Jul 18 '05 #4
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!!

Jul 18 '05 #5
'
noob warning:
what is so wonderful about the NEW class over the old ?

Jul 18 '05 #6

"Tian" <wa*********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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

Jul 18 '05 #7
Tian wrote:
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.

Jul 18 '05 #8
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.
Jul 18 '05 #9
'@'.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.
Jul 18 '05 #10

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

Similar topics

13
by: David Rysdam | last post by:
Getting no answer yesterday, I've done some investigation and I obviously don't understand how python namespaces work. Here's a test program: #!/usr/bin/python b = 2 def sumWithGlobal(a):...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
3
by: mystilleef | last post by:
Hello, I need to design a plug-in system for a project. The goal is to allow third party developers interact with an application via plug-ins in a clean and robust manner. At this point I am...
0
by: Manuzhai | last post by:
Hello there, I have this weird problem with a mod_python application. Recently I installed ElementTree and cElementTree through ez_setup.py, even though they were already installed normally...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
0
by: lumo2000 | last post by:
hello NG. i have to install a little python application and my problem is, that python does not find the proper include files, although they are, where the script searches for... any ideas how...
1
by: Carl Banks | last post by:
On Apr 11, 12:08 pm, "Neil Cerutti" <mr.ceru...@gmail.comwrote: That depends. No, it's prudence. If I am writing a banal, been-done-a-million-times before, cookie- cutter text adventure,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.