Hello,
I'm quite new to Python, and since a not-so-superficial look into the
docs didn't answer my question (although it still feels quite basic), I
decided to turn to this place:
Is there a way to 'extract' a dictionary into the current namespace?
That is, if you have
{'foo' : 23, 'bar' : 42}
you would get a variable foo with value 23 and a variable bar with value
42? Such a function would of course only work on string keys and would
probably have to check that, but still, it sounds practical enough that
surely someone else thought of it before.
Daniel 16 2366
Daniel Klein wrote: Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before. vars = {'foo': 23, 'bar': 42} locals().update(vars) foo
23 bar
42
Leif K-Brooks wrote: Daniel Klein wrote:
Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
>>> vars = {'foo': 23, 'bar': 42} >>> locals().update(vars) >>> foo 23 >>> bar
42
Except note from this page http://docs.python.org/lib/built-in-...built-in-funcs that
"""
locals()
Update and return a dictionary representing the current local symbol
table. Warning: The contents of this dictionary should not be
modified; changes may not affect the values of local variables used by
the interpreter.
"""
So Don't Do That.
-Peter
Am Montag, 17. Mai 2004 21:34 schrieb Leif K-Brooks: >>> locals().update(vars)
From the documentation:
"""
locals()
Update and return a dictionary representing the current local symbol table.
Warning: The contents of this dictionary should not be modified; changes may
not affect the values of local variables used by the interpreter.
"""
Heiko.
The result of modifying locals() is undefined.
Jeff
Daniel Klein <br****@gmx.at> wrote in message news:<ma************************************@pytho n.org>... Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
Daniel
The most straightforward way I know is d = {'foo': 23, 'bar': 42} globals().update(d) print foo, bar
23 42
This only works with globals, though - there's a corresponding
locals(), but unfortunately the docs say updating it's a no-no - the
dict returned could be a copy of the real locals dict (or the real
locals might not be in a dict at all). The interpreter is free to
ignore changes to the dictionary that locals() returns (although it
happens not to in CPython at the moment, that's an implementation
detail).
Even with the globals, it's a bit magical and could end up overwriting
names that match a key in the dictionary (it would be irritating if
one of the keys was "file" or "list").
Maybe a cleaner way to do essentially what you want is to use the
martellibot's Bunch recipe: http://aspn.activestate.com/ASPN/Coo...n/Recipe/52308
This would allow you to do:
class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
d = {'foo': 23, 'bar': 42} bunch = Bunch(**d) print bunch.foo, bunch.bar
23 42
This means you're not clobbering the global or local namespace with
arbitrary bindings, and you could have lots of Bunch instances for
different collections of bindings.
Cheers,
xtian
Daniel Klein wrote: Hello,
I'm quite new to Python, and since a not-so-superficial look into the docs didn't answer my question (although it still feels quite basic), I decided to turn to this place:
Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
Daniel
Is this illegal? import __main__ d = {'foo':'oof','bar':'rab','baz':'zab'} for k,v in d.items():
.... setattr(__main__, k, v)
.... foo,bar,baz
('oof', 'rab', 'zab')
--
Jason
Jason Mobarak wrote: Daniel Klein wrote: Is there a way to 'extract' a dictionary into the current namespace?
Is this illegal?
>>> import __main__ >>> d = {'foo':'oof','bar':'rab','baz':'zab'} >>> for k,v in d.items(): ... setattr(__main__, k, v) ... >>> foo,bar,baz
('oof', 'rab', 'zab')
No, but it will need to be changed for the module's name, and won't work
at all in a local namespace (like a function).
Heiko Wundram wrote: Am Montag, 17. Mai 2004 21:34 schrieb Leif K-Brooks:
>>> locals().update(vars)
From the documentation:
""" locals()
Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. """
Thanks for pointing that out, I was a bit lazy with TFM. Please ignore
my advice.
Daniel Klein wrote: Hello,
I'm quite new to Python, and since a not-so-superficial look into the docs didn't answer my question (although it still feels quite basic), I decided to turn to this place:
Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
Daniel
How about this:
In [1]: d = {'foo' : 23, 'bar' : 42}
In [2]: for item in d.items():
...: exec "%s = %d" % item
...:
In [3]: foo
Out[3]: 23
In [4]: bar
Out[4]: 42
Jason Mobarak wrote: Is this illegal?
>>> import __main__ >>> d = {'foo':'oof','bar':'rab','baz':'zab'} >>> for k,v in d.items(): ... setattr(__main__, k, v) ... >>> foo,bar,baz
('oof', 'rab', 'zab')
It executes, apparently, so why would you think it was "illegal"?
Note, however, that attempts to create variables programmatically
like this are almost always ill-conceived. If you don't know the
name of the variable in the first place, how are you planning to
access it later? And, as someone already said, it's probably better
just to continue to access things in the dictionary. More readable
probably.
-Peter
Arnold Filip wrote: Daniel Klein wrote:
Hello,
I'm quite new to Python, and since a not-so-superficial look into the docs didn't answer my question (although it still feels quite basic), I decided to turn to this place:
Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
Daniel
How about this:
In [1]: d = {'foo' : 23, 'bar' : 42}
In [2]: for item in d.items(): ...: exec "%s = %d" % item ...:
In [3]: foo Out[3]: 23
In [4]: bar Out[4]: 42
That's disgusting. At least with manipulating __main__ your not also
bringing in the possibility of excuting arbitrary code. d = {'foo' : 23, '__import__("os").system("echo executed a system
command"); bar' : 42} for item in d.items():
.... exec "%s = %d" % item
....
executed a system command foo,bar
(23, 42)
Granted, the reasons for wanting to do this may be ill-concieved,
there's probably a better, more obvious solution -- since doing the
subject of this thread is neither easy nor elegant.
--
Jason
Jason Mobarak wrote: Arnold Filip wrote:
Daniel Klein wrote:
Hello,
I'm quite new to Python, and since a not-so-superficial look into the docs didn't answer my question (although it still feels quite basic), I decided to turn to this place:
Is there a way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value 42? Such a function would of course only work on string keys and would probably have to check that, but still, it sounds practical enough that surely someone else thought of it before.
Daniel
How about this:
In [1]: d = {'foo' : 23, 'bar' : 42}
In [2]: for item in d.items(): ...: exec "%s = %d" % item ...:
In [3]: foo Out[3]: 23
In [4]: bar Out[4]: 42
That's disgusting.
I agree. But IMHO at least for a newbie that's the easiest way to do it.
No need to know anything about the "internals" of python.
At least with manipulating __main__ your not also bringing in the possibility of excuting arbitrary code.
>>> d = {'foo' : 23, '__import__("os").system("echo executed a system command"); bar' : 42} >>> for item in d.items(): ... exec "%s = %d" % item ... executed a system command >>> foo,bar (23, 42)
Concerning the security issue, the system call in your example can be
easily prevented: d = {'foo' : 23, '__import__("os").system("echo executed a system
command"); bar' : 42} for i in d.items():
.... exec "(%s) = %d" % i
....
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<string>", line 1
(__import__("os").system("echo executed a system command"); bar) = 42
^
SyntaxError: invalid syntax Granted, the reasons for wanting to do this may be ill-concieved, there's probably a better, more obvious solution -- since doing the subject of this thread is neither easy nor elegant.
Totally agree.
Hey Daniel, may be you should point out _what_ you want to achieve
rather than _how_ you can do this and that.
Cheers,
Arnold
Arnold Filip: How about this:
In [1]: d = {'foo' : 23, 'bar' : 42}
In [2]: for item in d.items(): ...: exec "%s = %d" % item ...:
In [3]: foo Out[3]: 23
In [4]: bar Out[4]: 42
you could extract them in to there own namespace: class AttrDict:
.... def __init__(self, d):
.... self.__d = d
.... def __getattr__(self,attr):
.... return self.__d[attr]
.... def __setattr__(self,attr,value):
.... self.__d[attr] = value
.... def __delattr__(self,attr):
.... del self.__d[attr]
.... d = AttrDict({'foo' : 23, 'bar' : 42})
Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 3, in __init__
File "<input>", line 10, in __setattr__
(snip)
RuntimeError: maximum recursion depth exceeded d.foo
That didn't work like I hoped. I decided to share it anyway in case
someone can get it to work.
--Noldoaran
Noldoaran wrote: Arnold Filip: How about this:
In [1]: d = {'foo' : 23, 'bar' : 42}
In [2]: for item in d.items(): ...: exec "%s = %d" % item ...:
In [3]: foo Out[3]: 23
In [4]: bar Out[4]: 42
you could extract them in to there own namespace: class AttrDict: ... def __init__(self, d): ... self.__d = d ... def __getattr__(self,attr): ... return self.__d[attr] ... def __setattr__(self,attr,value): ... self.__d[attr] = value ... def __delattr__(self,attr): ... del self.__d[attr] ... d = AttrDict({'foo' : 23, 'bar' : 42}) Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 3, in __init__ File "<input>", line 10, in __setattr__ (snip) RuntimeError: maximum recursion depth exceeded d.foo That didn't work like I hoped. I decided to share it anyway in case someone can get it to work.
Saying self.__d = dont_care invokes __setattr__() to set __d, __setattr__()
asks __getattr__() for __d, which asks __getattr__() for __d to determine
__d ...ad infinitum.
To avoid the recursion you must bypass __setattr__() by accessing __dict__
directly (and do some name mangling due to the evil double-underscore
attribute name by hand): class AttrDict:
.... def __init__(self, d):
.... self.__dict__["_AttrDict__d"] = d
.... def __getattr__(self, attr):
.... return self.__d[attr]
.... def __setattr__(self, attr, value):
.... self.__d[attr] = value
.... def __delattr__(self, attr):
.... del self.__d[attr]
.... d = AttrDict({'foo': 23, 'bar': 42}) d.foo
23 d.baz = 123 d.foo = 456 d.__d
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in __getattr__
KeyError: '__d' d.AttrDict__d
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in __getattr__
KeyError: 'AttrDict__d' d._AttrDict__d
{'baz': 123, 'foo': 456, 'bar': 42}
While this works, you can achieve the same functionality in a more elegant
way:
class AttrDict:
.... def __init__(self, d):
.... self.__dict__.update(d)
.... d = AttrDict({"foo": 23, "bar": 42}) d.bar
42 d.foo *= 2 d.__dict__
{'foo': 46, 'bar': 42} del d.foo d.__dict__
{'bar': 42}
A standard trick, by the way. Can't think of the right google keywords right
now, so I use another standard trick and leave finding them as an excercise
to the reader :-)
Peter
Peter Otten <__*******@web.de> wrote in message news:<c8*************@news.t-online.com>... class AttrDict: ... def __init__(self, d): ... self.__dict__.update(d) ... d = AttrDict({"foo": 23, "bar": 42}) d.bar 42 d.foo *= 2 d.__dict__ {'foo': 46, 'bar': 42} del d.foo d.__dict__ {'bar': 42}
A standard trick, by the way. Can't think of the right google keywords right now, so I use another standard trick and leave finding them as an excercise to the reader :-)
Peter http://aspn.activestate.com/ASPN/Coo...n/Recipe/52308 (Bunch class) http://www.norvig.com/python-iaq.html (Struct class)
- kv
Peter Otten: Saying self.__d = dont_care invokes __setattr__() to set __d, __setattr__() asks __getattr__() for __d, which asks __getattr__() for __d to determine __d ...ad infinitum. To avoid the recursion you must bypass __setattr__() by accessing __dict__ directly.
(snip)
While this works, you can achieve the same functionality in a more elegant way:
class AttrDict: ... def __init__(self, d): ... self.__dict__.update(d) ...
(snip) A standard trick, by the way. Can't think of the right google keywords right now, so I use another standard trick and leave finding them as an excercise to the reader :-)
Peter
Thanks, Peter. I found another way that also works: class AttrDict(dict):
.... __getattr__ = dict.__getitem__
.... __getattribute__ = __getattr__
.... __setattr__ = dict.__setitem__
.... __delattr__ = dict.__delitem__
.... d = AttrDict({'foo': 23, 'bar': 42}) d.foo
23 d.bar
42 d.spam = 123 d.spam
123 d
{'spam': 123, 'foo': 23, 'bar': 42}
----
~Noldoaran This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark DuPrey |
last post by:
I've got a script in an ASP page that is supposed to extract certain
files from a zip file, move them, create a new zip with the moved
files and then make a self-extracting archive out of the new...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |