Problem in designing a global directory in python

Discussion in 'Python' started by Tian, Mar 29, 2005.

  1. Tian

    Tian Guest

    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?
     
    Tian, Mar 29, 2005
    #1
    1. Advertisements

  2. Tian

    F. Petitjean Guest

    Le 29 Mar 2005 09:50:46 -0800, Tian a écrit :
    # 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(object): # why not use "new-style" classes, we are in
    2005
    # I suppose that there is some __doc__ and code :)
    Please do not use the from module import * form
    from utils import getContext
    You can also restrict the exported names of utils.py by adding a
    __all__ = ('getContext',)
    in utils.py.
    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.
     
    F. Petitjean, Mar 29, 2005
    #2
    1. Advertisements

  3. Tian

    Tian Guest

    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?
     
    Tian, Mar 29, 2005
    #3
  4. Tian a écrit :
    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 !-).
     
    Bruno Desthuilliers, Mar 29, 2005
    #4
  5. Tian

    Tian Guest

    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!!
     
    Tian, Mar 29, 2005
    #5
  6. Tian

    ' Guest

    noob warning:
    what is so wonderful about the NEW class over the old ?
     
    ', Mar 30, 2005
    #6
  7. Tian

    Terry Reedy Guest

    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
     
    Terry Reedy, Mar 30, 2005
    #7
  8. Tian

    Serge Orlov Guest

    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.
     
    Serge Orlov, Mar 30, 2005
    #8
  9. Tian a écrit :
    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.
     
    Bruno Desthuilliers, Mar 30, 2005
    #9
  10. '@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) a écrit :
    A whole lot of things. But the main thing to know is that old-style
    classes are deprecated, and will disappear in the future.
     
    Bruno Desthuilliers, Mar 30, 2005
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.