[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Adding to a module's __dict__?

Roy Smith

3/2/2010 4:28:00 AM

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?
14 Answers

Chris Rebert

3/2/2010 5:39:00 AM

0

On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <roy@panix.com> wrote:
> >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
--
One should avoid using the Big Hammer unnecessarily,
but sometimes you really do need it and it's nice that it's available
for such cases.
http://blog.re...

Jean-Michel Pichavant

3/2/2010 10:30:00 AM

0

Roy Smith wrote:
> >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
>>> 42


JM

Roy Smith

3/2/2010 1:21:00 PM

0

In article <mailman.96.1267508316.23598.python-list@python.org>,
Chris Rebert <clp2@rebertia.com> wrote:

> On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <roy@panix.com> wrote:
> > >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.

Steve Holden

3/2/2010 1:33:00 PM

0

Roy Smith wrote:
> In article <mailman.96.1267508316.23598.python-list@python.org>,
> Chris Rebert <clp2@rebertia.com> wrote:
>
>> On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <roy@panix.com> wrote:
>>> >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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Steve Holden

3/2/2010 1:33:00 PM

0

Roy Smith wrote:
> In article <mailman.96.1267508316.23598.python-list@python.org>,
> Chris Rebert <clp2@rebertia.com> wrote:
>
>> On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <roy@panix.com> wrote:
>>> >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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Mel

3/2/2010 2:26:00 PM

0

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.


Roy Smith

3/2/2010 3:20:00 PM

0

On Mar 2, 8:33 am, Steve Holden <st...@holdenweb.com> wrote:

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

Steve Holden

3/2/2010 4:15:00 PM

0

Roy Smith wrote:
> On Mar 2, 8:33 am, Steve Holden <st...@holdenweb.com> wrote:
>
>> 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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

John Posner

3/2/2010 4:18:00 PM

0

On 3/2/2010 10:19 AM, Roy Smith wrote:
>
> 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:

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

HTH,
John

Carl Banks

3/2/2010 4:49:00 PM

0

On Mar 2, 5:21 am, Roy Smith <r...@panix.com> wrote:
> In article <mailman.96.1267508316.23598.python-l...@python.org>,
>  Chris Rebert <c...@rebertia.com> wrote:
>
>
>
> > On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <r...@panix.com> wrote:
> > > >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