Introspection at the module level?

Discussion in 'Python' started by Roy Smith, Mar 6, 2004.

  1. Roy Smith

    Roy Smith Guest

    I've got a module that defines a bunch of constants:

    OP_FOO = 1
    OP_BAR = 2
    OP_BAZ = 37

    and so on. The values are all unique. I want to build, at module
    import time, a reverse map of these constants, i.e. I want to end up
    with:

    {1: "OP_FOO", 2: "OP_BAR", 37: "OP_BAZ"}

    I can find the appropriate symbols:

    for name in dir():
    if name.startswith ("OP_"):
    print name

    But I don't see how to get the values. Getattr() is sort of what I
    want, but it works on objects, not modules. Not to mention that I don't
    see how to get a handle to the module from inside the module, i.e.
    there's no "self".

    What am I missing?
     
    Roy Smith, Mar 6, 2004
    #1
    1. Advertising

  2. Roy Smith

    Sean Ross Guest

    "Roy Smith" <> wrote in message
    news:...
    > I've got a module that defines a bunch of constants:
    >
    > OP_FOO = 1
    > OP_BAR = 2
    > OP_BAZ = 37
    >
    > and so on. The values are all unique. I want to build, at module
    > import time, a reverse map of these constants, i.e. I want to end up
    > with:
    >
    > {1: "OP_FOO", 2: "OP_BAR", 37: "OP_BAZ"}
    >


    I'm not sure how to do this "at module import time", but if you just
    want to build a reverse dictionary of globals variables that start with
    "OP_", you can try this:

    rd = dict([(v,k) for k,v in globals().copy().iteritems() if
    k.startswith("OP_")])


    >Not to mention that I don't
    > see how to get a handle to the module from inside the module, i.e.
    > there's no "self".


    http://groups.google.ca/groups?hl=e...=giGhb.262770$&rnum=6
     
    Sean Ross, Mar 6, 2004
    #2
    1. Advertising

  3. Roy Smith

    Peter Hansen Guest

    Roy Smith wrote:

    > I've got a module that defines a bunch of constants:
    >
    > OP_FOO = 1
    > OP_BAR = 2
    > OP_BAZ = 37
    >
    > and so on. The values are all unique. I want to build, at module
    > import time, a reverse map of these constants, i.e. I want to end up
    > with:
    >
    > {1: "OP_FOO", 2: "OP_BAR", 37: "OP_BAZ"}
    >
    > I can find the appropriate symbols:
    >
    > for name in dir():
    > if name.startswith ("OP_"):
    > print name
    >
    > But I don't see how to get the values. Getattr() is sort of what I
    > want, but it works on objects, not modules. Not to mention that I don't
    > see how to get a handle to the module from inside the module, i.e.
    > there's no "self".
    >
    > What am I missing?


    globals()

    That returns a dict which has those constants in it, so you can ask for
    ..keys() or .values(), etc.

    Think of dir() as just globals().keys() in this case...

    -Peter
     
    Peter Hansen, Mar 6, 2004
    #3
  4. Roy Smith

    Terry Reedy Guest

    "Roy Smith" <> wrote in message
    news:...
    > But I don't see how to get the values. Getattr() is sort of what I
    > want, but it works on objects, not modules.


    Yes it does

    >>> m = __import__(__name__)
    >>> setattr(m, 'a', 3)
    >>> dir(m)

    ['__builtins__', '__doc__', '__name__', 'a', 'm']
    >>> getattr(m, 'a')

    3

    > I don't see how to get a handle to the module from inside the module,

    i.e.
    > there's no "self".


    You have to define it yourself. See above.

    > What am I missing?

    d={} # then, inside your loop:
    d[getattr(m,name)] = name #should do what you said you want

    Terry J. Reedy
     
    Terry Reedy, Mar 6, 2004
    #4
  5. Roy Smith

    Sean Ross Guest

    "Roy Smith" <> wrote in message
    news:...
    >Not to mention that I don't
    > see how to get a handle to the module from inside the module, i.e.
    > there's no "self".


    There are a couple of ways to get hold of the module from inside. My
    previous post contained a link to one method. Here's a couple of other
    quick ways:

    import __main__ as main

    or

    >>> main = __import__(__name__)
    >>> main.a = "A"
    >>> a

    'A'
    >>> main.__dict__

    {'a': 'A', '__builtins__': <module '__builtin__' (built-in)>, ... }
     
    Sean Ross, Mar 6, 2004
    #5
  6. Roy> I've got a module that defines a bunch of constants:
    Roy> OP_FOO = 1
    Roy> OP_BAR = 2
    Roy> OP_BAZ = 37

    Roy> and so on. The values are all unique. I want to build, at module
    Roy> import time, a reverse map of these constants, i.e. I want to end
    Roy> up with:

    Roy> {1: "OP_FOO", 2: "OP_BAR", 37: "OP_BAZ"}

    Have a look at

    http://manatee.mojam.com/~skip/python/ConstantMap.py

    Skip
     
    Skip Montanaro, Mar 7, 2004
    #6
  7. Roy Smith

    Roy Smith Guest

    In article <>,
    Skip Montanaro <> wrote:

    > Have a look at
    >
    > http://manatee.mojam.com/~skip/python/ConstantMap.py


    This pointed me to the simpliest solution. I ended up with:

    map = {}
    for name, value in globals().items():
    if name.startswith ("OP_"):
    map[value] = name

    which seems to work just fine. Thanks to everybody who responded with
    suggestions.

    The danger here is that I'm depending on the fact that there are no
    globals defined elsewhere in the system which start with "OP_", but that
    seems like a pretty minimal risk. In fact, it looks like all the
    externally defined globals are of the form __xxx__.
     
    Roy Smith, Mar 7, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Leszczynski
    Replies:
    1
    Views:
    315
    Michele Simionato
    Apr 28, 2005
  2. Benjamin Rutt
    Replies:
    4
    Views:
    390
    Mike Meyer
    Jul 8, 2005
  3. Mudcat
    Replies:
    3
    Views:
    945
    Steven D'Aprano
    Jan 14, 2006
  4. pabbu
    Replies:
    8
    Views:
    732
    Marc Boyer
    Nov 7, 2005
  5. Brian Munroe

    class / module introspection?

    Brian Munroe, Apr 2, 2008, in forum: Python
    Replies:
    8
    Views:
    376
    Brian Munroe
    Apr 3, 2008
Loading...

Share This Page