Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33081

Re: Want to add dictionary keys to namespace?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'jeff': 0.04; 'none,': 0.05; 'python': 0.09; 'dict': 0.09; 'name):': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'subject:keys': 0.09; 'def': 0.10; '42,': 0.16; 'attributes:': 0.16; 'dictionaries': 0.16; 'dictionary,': 0.16; 'namespace?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'people,': 0.21; 'keys': 0.22; 'lets': 0.22; 'this:': 0.23; 'header:User-Agent:1': 0.26; 'common': 0.26; 'values': 0.26; 'module.': 0.27; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'dictionary': 0.29; 'smart': 0.29; 'spaces': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'could': 0.32; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'skip:{ 10': 0.36; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:n 10': 0.63; 'within': 0.64; '"horrible"': 0.84; 'subject:add': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Want to add dictionary keys to namespace?
Date Sat, 10 Nov 2012 10:19 +0100
Organization None
References <CANNB2xtL32rTEayOHh4PmNA=7r3YZjXKA_Le_E5QVGcaPJHo_Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b88e.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3531.1352539132.27098.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352539132 news.xs4all.nl 6922 [2001:888:2000:d::a6]:59532
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33081

Show key headers only | View raw


Jeff Jeffries wrote:

> Smart people, Is there a way I can add a dictionaries keys to the python
> namespace? It would just be temporary as I am working with a large
> dictionary, and it would speed up work using an IDE.  I look and find
> nothing... none of the keys have spaces and none are common names within
> the module.
> 
> http://stackoverflow.com/questions/2597278/python-load-variables-in-a-
dict-into-namespace
> 
> I do this:
> 
> #Do this?
> dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"}
> for key in dictionary.keys():
>      eval("%s=None"%key)
> 
> #or do this?
> locals().update(dictionary)
> 
> Any ideas?

You could instead use a dict subclass that lets you access values as 
attributes:

>>> class Dict(dict):
...     def __getattr__(self, name):
...             return self[name]
...     def __setattr__(self, name, value):
...             self[name] = value
... 
>>> d = Dict({"AppleSeed": None, "Has": None, "Horrible" : None, "Art": 42})
>>> d.Art
42
>>> d.AppleSeed
>>> d.AppleSeed = "spam"
>>> d
{'Has': None, 'Art': 42, 'AppleSeed': 'spam', 'Horrible': None}

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Want to add dictionary keys to namespace? Peter Otten <__peter__@web.de> - 2012-11-10 10:19 +0100

csiph-web