Import config into global space?

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

    Import config into global space?

    I'm writing a module, and it's getting too big to cope with as it has
    a lot of configuration in it - constants, dictionaries etc; it'd be
    nice to externalise this and keep my module down to just classes/defs
    etc.

    What I'd like to do is have myprog.py import config.py and be able to
    reference the configuration variables globally - i.e. I could just
    reference myvar instead of config.myvar

    So I'd have something like this:

    config.py
    =========

    ID_TOOLBAR = 105

    myprog.py
    =========

    import config
    print ID_TOOLBAR

    result
    ======
    [color=blue][color=green]
    >> 105[/color][/color]

    It doesn't seem to work though, as global is limited to modules as far
    as I know.

    Any ideas how to do this, it's basically like a require or #include in
    Perl/C...
  • Josiah Carlson

    #2
    Re: Import config into global space?

    > config.py[color=blue]
    > =========
    >
    > ID_TOOLBAR = 105
    >
    > myprog.py
    > =========
    >
    > import config
    > print ID_TOOLBAR
    >
    > result
    > ======[/color]

    from config import *

    - Josiah

    Comment

    • simo

      #3
      Re: Import config into global space?

      Josiah Carlson <jcarlson@uci.e du> wrote:

      [global import from file]
      [color=blue]
      > from config import *[/color]

      Oh man! I can't believe it was that simple and I didn't think of it!

      Actually, I've gone with the config.variable syntax for now anyway, it
      actually makes more sense as I don't have to name my variables like
      fontsize_config to differentiate them.

      Comment

      • Josiah Carlson

        #4
        Re: Import config into global space?

        simo wrote:[color=blue]
        > Josiah Carlson <jcarlson@uci.e du> wrote:
        >
        > [global import from file]
        >
        >[color=green]
        >>from config import *[/color]
        >
        >
        > Oh man! I can't believe it was that simple and I didn't think of it!
        >
        > Actually, I've gone with the config.variable syntax for now anyway, it
        > actually makes more sense as I don't have to name my variables like
        > fontsize_config to differentiate them.[/color]


        In general, "from module import *" is generally frowned upon, if only
        because it can clobber your module's namespace. There are certainly
        exceptions, but namespaces are useful.

        - Josiah

        Comment

        • simo

          #5
          Re: Import config into global space?

          Josiah Carlson <jcarlson@uci.e du> wrote:

          [snip][color=blue][color=green][color=darkred]
          > > > from config import *[/color][/color][/color]
          [color=blue][color=green]
          > > Actually, I've gone with the config.variable syntax for now anyway, it
          > > actually makes more sense as I don't have to name my variables like
          > > fontsize_config to differentiate them.[/color][/color]
          [color=blue]
          > In general, "from module import *" is generally frowned upon, if only
          > because it can clobber your module's namespace. There are certainly
          > exceptions, but namespaces are useful.[/color]

          Yes, that's what I thought, one of the reasons I'm going to go with
          the config.variable syntax after all.

          I'm moving to the wxWidgets 2.5 way of using the wx namespace too
          (e.g. wx.ListCtrl instead of wxListCtrl).

          Comment

          • Josiah Carlson

            #6
            Re: Import config into global space?

            > Yes, that's what I thought, one of the reasons I'm going to go with[color=blue]
            > the config.variable syntax after all.
            >
            > I'm moving to the wxWidgets 2.5 way of using the wx namespace too
            > (e.g. wx.ListCtrl instead of wxListCtrl).[/color]

            That's good, because wxListCtrl won't be available in 2.5, wx.ListCtrl
            will be. *wink*

            - Josiah

            Comment

            Working...