Problem in designing a global directory in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tian

    #1

    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().se t(...)

    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().se t(...)

    I got this error.

    What should I do to correct that?

  • F. Petitjean

    #2
    Re: Problem in designing a global directory in python

    Le 29 Mar 2005 09:50:46 -0800, Tian a écrit :[color=blue]
    > 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[/color]
    # 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[color=blue]
    >
    > class Context:[/color]
    class Context(object) : # why not use "new-style" classes, we are in
    2005[color=blue]
    > def __init__(self):[/color]
    # I suppose that there is some __doc__ and code :-)[color=blue]
    > 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 *[/color]
    Please do not use the from module import * form
    from utils import getContext[color=blue]
    > getContext().se t(...)[/color]
    You can also restrict the exported names of utils.py by adding a
    __all__ = ('getContext',)
    in utils.py.[color=blue]
    >
    > 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().se t(...)
    >
    > I got this error.
    >
    > What should I do to correct that?[/color]
    See above :-) and post a follow-up to report if the issue is solved.[color=blue]
    >[/color]
    PS sorry for bad english. I am not a native speaker.

    Comment

    • Tian

      #3
      Re: Problem in designing a global directory in python

      I have tried using "sysctx=Non e" 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?

      Comment

      • Bruno Desthuilliers

        #4
        Re: Problem in designing a global directory in python

        Tian a écrit :[color=blue]
        > 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[/color]

        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 !-).


        Comment

        • Tian

          #5
          Re: Problem in designing a global directory in python

          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!!

          Comment

          • '@'.join([..join(['fred','dixon']),..join(['gmail'

            #6
            Re: Problem in designing a global directory in python

            noob warning:
            what is so wonderful about the NEW class over the old ?

            Comment

            • Terry Reedy

              #7
              Re: Problem in designing a global directory in python


              "Tian" <wangtianthu@gm ail.com> wrote in message
              news:1112118646 .177300.24800@g 14g2000cwa.goog legroups.com...[color=blue]
              >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[/color]

              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



              Comment

              • Serge Orlov

                #8
                Re: Problem in designing a global directory in python

                Tian wrote:[color=blue]
                > I have tried using "sysctx=Non e" 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?[/color]

                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.

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Problem in designing a global directory in python

                  Tian a écrit :[color=blue]
                  > 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?[/color]

                  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.

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: Problem in designing a global directory in python

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

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

                    Comment

                    Working...