Adding to a module's __dict__?

R

Roy Smith

From inside a module, I want to add a key-value pair to the module's
__dict__. I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined". Is
it possible to do what I'm trying to do?
 
C

Chris Rebert

From inside a module, I want to add a key-value pair to the module's
__dict__.  I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined".  Is
it possible to do what I'm trying to do?

Yes; just modify the dict returned by the globals() built-in function
instead. It's usually not wise to do this and is better to use a
separate dict instead, but I'll assume you know what you're doing and
have good reasons to disregard the standard advice due to your
use-case.

Cheers,
Chris
 
J

Jean-Michel Pichavant

Roy said:
From inside a module, I want to add a key-value pair to the module's
__dict__. I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined". Is
it possible to do what I'm trying to do?
test.py:

import sys
varName= 'foo'
setattr(sys.modules[__name__], varName, 42)



in a shell:
import test

print test.foo

JM
 
R

Roy Smith

Chris Rebert said:
From inside a module, I want to add a key-value pair to the module's
__dict__.  I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined".  Is
it possible to do what I'm trying to do?

Yes; just modify the dict returned by the globals() built-in function
instead.

Ah, cool. Thanks.
It's usually not wise to do this and is better to use a
separate dict instead, but I'll assume you know what you're doing and
have good reasons to disregard the standard advice due to your
use-case.

Why is it unwise?

The use case is I'm importing a bunch of #define constants from a C header
file. I've got triples that I want to associate; the constant name, the
value, and a string describing it. The idea is I want to put in the
beginning of the module:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

and so on. I'm going to have hundreds of these, so ease of use, ease of
maintenance, and niceness of presentation are important.

My declare() function will not just set XYZ_FOO = 1 at module global scope,
but also insert entries in a variety of dicts so I can look up the
description string, map from a value back to the constant name, etc.

I *could* do this in a separate dict, but the notational convenience of
being able to have the original constant names globally available is pretty
important.
 
S

Steve Holden

Roy said:
Chris Rebert said:
From inside a module, I want to add a key-value pair to the module's
__dict__. Â I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined". Â Is
it possible to do what I'm trying to do?
Yes; just modify the dict returned by the globals() built-in function
instead.

Ah, cool. Thanks.
It's usually not wise to do this and is better to use a
separate dict instead, but I'll assume you know what you're doing and
have good reasons to disregard the standard advice due to your
use-case.

Why is it unwise?

The use case is I'm importing a bunch of #define constants from a C header
file. I've got triples that I want to associate; the constant name, the
value, and a string describing it. The idea is I want to put in the
beginning of the module:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

and so on. I'm going to have hundreds of these, so ease of use, ease of
maintenance, and niceness of presentation are important.

My declare() function will not just set XYZ_FOO = 1 at module global scope,
but also insert entries in a variety of dicts so I can look up the
description string, map from a value back to the constant name, etc.

I *could* do this in a separate dict, but the notational convenience of
being able to have the original constant names globally available is pretty
important.
And how important is it to make sure that whatever data your program
processes doesn't overwrite the actual variable names you want to use to
program the processing?

If you use this technique you are effectively making your program a
hostage to fortune, as you no longer control the namespace you are using
for the programming.

regards
Steve
 
S

Steve Holden

Roy said:
Chris Rebert said:
From inside a module, I want to add a key-value pair to the module's
__dict__. Â I know I can just do:

FOO = 'bar'

at the module top-level, but I've got 'FOO' as a string and what I
really need to do is

__dict__['Foo'] = 'bar'

When I do that, I get "NameError: name '__dict__' is not defined". Â Is
it possible to do what I'm trying to do?
Yes; just modify the dict returned by the globals() built-in function
instead.

Ah, cool. Thanks.
It's usually not wise to do this and is better to use a
separate dict instead, but I'll assume you know what you're doing and
have good reasons to disregard the standard advice due to your
use-case.

Why is it unwise?

The use case is I'm importing a bunch of #define constants from a C header
file. I've got triples that I want to associate; the constant name, the
value, and a string describing it. The idea is I want to put in the
beginning of the module:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

and so on. I'm going to have hundreds of these, so ease of use, ease of
maintenance, and niceness of presentation are important.

My declare() function will not just set XYZ_FOO = 1 at module global scope,
but also insert entries in a variety of dicts so I can look up the
description string, map from a value back to the constant name, etc.

I *could* do this in a separate dict, but the notational convenience of
being able to have the original constant names globally available is pretty
important.
And how important is it to make sure that whatever data your program
processes doesn't overwrite the actual variable names you want to use to
program the processing?

If you use this technique you are effectively making your program a
hostage to fortune, as you no longer control the namespace you are using
for the programming.

regards
Steve
 
M

Mel

Roy Smith wrote:
[ ... ]
Why is it unwise?

The use case is I'm importing a bunch of #define constants from a C header
file. I've got triples that I want to associate; the constant name, the
value, and a string describing it. The idea is I want to put in the
beginning of the module:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

and so on. I'm going to have hundreds of these, so ease of use, ease of
maintenance, and niceness of presentation are important.

As long as the header file says what you think it says, you're fine. If you
encounter a file that does "#define sys", then the sys module is forever
masked, and your module can't invoke it. A header file that contains
"#define declare" will be fun.

Mel.
 
R

Roy Smith

And how important is it to make sure that whatever data your program
processes doesn't overwrite the actual variable names you want to use to
program the processing?

Oh, I see what you're saying. You're thinking I was going to machine-
process the C header file and pattern-match the #define statements?
Actually, I was just hand-copying the values, and was looking for a
way to reduce typing.

But, I suppose if I were to machine-process the header files, that
would be a concern. I suppose in that case I would make sure I only
inserted variables which matched a particular pattern (ie, "[A-Z]+_[A-
Z][A-Z0-9]+"). In fact, now that you got me thinking in that
direction...

Somewhat sadly, in my case, I can't even machine process the header
file. I don't, strictly speaking, have a header file. What I have is
a PDF which documents what's in the header file, and I'm manually re-
typing the data out of that. Sigh.
 
S

Steve Holden

Roy said:
And how important is it to make sure that whatever data your program
processes doesn't overwrite the actual variable names you want to use to
program the processing?

Oh, I see what you're saying. You're thinking I was going to machine-
process the C header file and pattern-match the #define statements?
Actually, I was just hand-copying the values, and was looking for a
way to reduce typing.

But, I suppose if I were to machine-process the header files, that
would be a concern. I suppose in that case I would make sure I only
inserted variables which matched a particular pattern (ie, "[A-Z]+_[A-
Z][A-Z0-9]+"). In fact, now that you got me thinking in that
direction...

Somewhat sadly, in my case, I can't even machine process the header
file. I don't, strictly speaking, have a header file. What I have is
a PDF which documents what's in the header file, and I'm manually re-
typing the data out of that. Sigh.
Don't worry. Now you have revealed the *real* problem you may well find
there are c.l.py readers who can help! Python can read PDFs ...

regards
Steve
 
J

John Posner

Somewhat sadly, in my case, I can't even machine process the header
file. I don't, strictly speaking, have a header file. What I have is
a PDF which documents what's in the header file, and I'm manually re-
typing the data out of that. Sigh.

Here's an idea, perhaps too obvious, to minimize your keystrokes:

1. Create a text file with the essential data:

XYZ_FOO 0 The foo property
XYZ_BAR 1 The bar property
XYZ_BAZ 2 reserved for future use

2. Use a Python script to convert this into the desired code:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

Note:
['XYZ_FOO', '0', 'The foo property']

HTH,
John
 
C

Carl Banks

From inside a module, I want to add a key-value pair to the module's
__dict__.  I know I can just do:
FOO = 'bar'
at the module top-level, but I've got 'FOO' as a string and what I
really need to do is
__dict__['Foo'] = 'bar'
When I do that, I get "NameError: name '__dict__' is not defined".  Is
it possible to do what I'm trying to do?
Yes; just modify the dict returned by the globals() built-in function
instead.

Ah, cool.  Thanks.
It's usually not wise to do this and is better to use a
separate dict instead, but I'll assume you know what you're doing and
have good reasons to disregard the standard advice due to your
use-case.

Why is it unwise?


He didn't say it was unwise. He said it's usually not wise.


Carl Banks
 
M

MRAB

John said:
Somewhat sadly, in my case, I can't even machine process the header
file. I don't, strictly speaking, have a header file. What I have is
a PDF which documents what's in the header file, and I'm manually re-
typing the data out of that. Sigh.

Here's an idea, perhaps too obvious, to minimize your keystrokes:

1. Create a text file with the essential data:

XYZ_FOO 0 The foo property
XYZ_BAR 1 The bar property
XYZ_BAZ 2 reserved for future use

2. Use a Python script to convert this into the desired code:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

Note:
['XYZ_FOO', '0', 'The foo property']
You might be able to reduce your typing by copy-and-pasting the relevant
text from the PDF into an editor and then editing it.
 
T

Terry Reedy

There are Python modules to read/write pdf.
Here's an idea, perhaps too obvious, to minimize your keystrokes:

1. Create a text file with the essential data:

XYZ_FOO 0 The foo property
XYZ_BAR 1 The bar property
XYZ_BAZ 2 reserved for future use

2. Use a Python script to convert this into the desired code:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

Note:
['XYZ_FOO', '0', 'The foo property']

Given that set of triples is constant, I would think about having the
Python script do the computation just once, instead of with every
inport. In other words, the script should *call* the declare function
and then write out the resulting set of dicts either to a .py or pickle
file.

tjr
 
D

Dave Angel

Terry said:
There are Python modules to read/write pdf.
Here's an idea, perhaps too obvious, to minimize your keystrokes:

1. Create a text file with the essential data:

XYZ_FOO 0 The foo property
XYZ_BAR 1 The bar property
XYZ_BAZ 2 reserved for future use

2. Use a Python script to convert this into the desired code:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

Note:
s 'XYZ_FOO 0 The foo property'
s.split(None, 2)
['XYZ_FOO', '0', 'The foo property']

Given that set of triples is constant, I would think about having the
Python script do the computation just once, instead of with every
inport. In other words, the script should *call* the declare function
and then write out the resulting set of dicts either to a .py or
pickle file.

tjr
There have been lots of good suggestions in this thread. Let me give
you my take:

1) you shouldn't want to clutter up the global dictionary of your main
processing module. There's too much risk of getting a collision, either
with the functions you write, or with some builtin. That's especially
true if you might later want to use a later version of that pdf file.
Easiest solution for your purposes, make it a separate module. Give it
a name like defines, and in your main module, you use

import defines
print defines.XYZ_FOO

And if that's too much typing, you can do:
import defines as I
print I.XYZ_FOO

Next problem is to parse that pdf file. One solution is to use a pdf
library. But another is to copy/paste it into a text file, and parse
that. Assuming it'll paste, and that the lines you want are
recognizable (eg. they all begin as #define), the parsing should be
pretty easy. The results of the parsing is a file defines.py

Now, if the pdf ever changes, rerun your parsing program. But don't run
it every time your application runs.

If the pdf file were changing often, then I'd have a different answer:
2) define an empty class, just as a placeholder, and make one instance I
Populate a class instance I with setattrib() calls, but access
it with direct syntax, same as our first example.


DaveA
 
G

Gregory Ewing

Roy said:
The idea is I want to put in the beginning of the module:

declare('XYZ_FOO', 0, "The foo property")
declare('XYZ_BAR', 1, "The bar property")
declare('XYZ_BAZ', 2, "reserved for future use")

Okay, that seems like a passable excuse.

One thing to watch out for is that if your 'declare' function
is defined in a different module, when it calls globals() it
will get the globals of the module it's defined in, not the
one it's being called from.

There's a hackish way of getting around that, but it might
be better to put all of these symbols in a module of their
own and import them from it. The declare() function can then
be defined in that same module so that it accesses the right
globals.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top