Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Introspection at the module level?

Reply
Thread Tools

Introspection at the module level?

 
 
Roy Smith
Guest
Posts: n/a
 
      03-06-2004
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?
 
Reply With Quote
 
 
 
 
Sean Ross
Guest
Posts: n/a
 
      03-06-2004

"Roy Smith" <> wrote in message
news:roy-...
> 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=en....tin.it&rnum=6



 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      03-06-2004
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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      03-06-2004

"Roy Smith" <> wrote in message
news:roy-...
> 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




 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      03-06-2004
"Roy Smith" <> wrote in message
news:roy-...
>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)>, ... }



 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      03-07-2004

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

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      03-07-2004
In article <mailman.98.1078668652.19534.python->,
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__.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class / module introspection? Brian Munroe Python 8 04-03-2008 04:36 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
question about introspection using inspect module Benjamin Rutt Python 4 07-08-2005 04:33 AM
Getting a list of classes in the current module/auto introspection Andy Leszczynski Python 1 04-28-2005 02:00 PM
Actual parameter introspection for error logging David Smith Java 0 07-11-2003 01:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57