Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: converting dict to object

Reply
Thread Tools

Re: converting dict to object

 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-02-2006
At Friday 1/12/2006 22:48, rieh25 wrote:

>If I have a dictionary such as:
>
>d = {'a' : 1, 'b' : 2}
>
>is there a way to convert it into an object o, such as:
>
>o.a = 1
>o.b = 2


>>> class X(object):

.... def __init__(self, d): self.__dict__.update(d)
....
>>> d = {'a' : 1, 'b' : 2}
>>> o=X(d)
>>> o.a

1
>>> o.b

2
>>>



--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
 
Reply With Quote
 
 
 
 
Michel Claveau
Guest
Posts: n/a
 
      12-02-2006
Hi!

Yes.

But...

Try: d = {'a': 1, 'b': 2, 'def': 123}

Ok, I go out...

--
@-salutations

Michel Claveau


 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      12-02-2006
On 2006-12-02, Michel Claveau <(E-Mail Removed)> wrote:
> Hi!
>
> Yes.
>
> But...
>
> Try: d = {'a': 1, 'b': 2, 'def': 123}
>
> Ok, I go out...


How to convert a list of strings into a list of integers:

a = ['82', '4', '16']

ai = [int(i) for i in a]

Yes.

But...

Try: a = ['82', '4', '16', 'foo']

Ok, I go out...

--
Neil Cerutti
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-02-2006
Neil Cerutti wrote:
> On 2006-12-02, Michel Claveau <(E-Mail Removed)> wrote:
> > Hi!
> >
> > Yes.
> >
> > But...
> >
> > Try: d = {'a': 1, 'b': 2, 'def': 123}
> >
> > Ok, I go out...

>
> How to convert a list of strings into a list of integers:
>
> a = ['82', '4', '16']
>
> ai = [int(i) for i in a]
>
> Yes.
>
> But...
>
> Try: a = ['82', '4', '16', 'foo']
>
> Ok, I go out...



Michel was making (part of) a valid point: dictionaries have more
flexibility in choice of keys than objects have in attribute names.
More completely:

Suppose d.keys() produces ["one", "def", "foo bar", 3, "3"]

o.one is OK.

o.def is not OK ["def" is a keyword] but getattr(o, "def") still works.

o.foo bar is not OK ["foo bar" is not a valid identifier] but
getattr(o, "foo bar") still works.

o.3 is not OK ["3" is not a valid identifier] but getattr(o, "3") still
works.

getattr(o, 3) doesn't work [3 is not a string]; you would have to do
o.__dict__[3] to access the value -- no advantage over keeping it in a
dict.

The OP might consider adding code to the __init__ method to check for
cases where the dictionary key is not a string containing a valid
Python identifier (not a keyword).

Note: I have done a reasonable number of exercises that involved taking
a database table definition or a flat file record definition or the
headings in an XLS worksheet or CSV file and ending up with names for
attributes of Python objects. Various heuristics are necessary of
course to get valid identifers without duplicates, but I've never been
bitten by the keyword problem. I'll have to confess that I wasn't aware
of the problem until the 3rd reading of Michel's message

Any experiences of keyword-bite?

Observation: the keyword module's iskeyword() function gives an easy
check. If one is supporting multiple versions of Python, a more
complicated (overkill?) approach might be desirable: use the latest
version of Python to generate a source file containing the latest
keywords from keyword.kwlist.

Cheers,
John

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-02-2006
John Machin wrote:

> Any experiences of keyword-bite?


creating or manipulating CSS-styled (X)HTML using an XML binding that
exposes XML attributes as Python attributes.

(this could be viewed as an unnecessary restriction in the Python
parser; it wouldn't be too hard to allow reserved words for keyword
argument names and for attribute access; iirc, Jython already does this,
and Paul Svensson posted a patch for CPython a couple of years ago.
PEP-time, anyone?)

</F>

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-03-2006
On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote:

> The OP might consider adding code to the __init__ method to check for
> cases where the dictionary key is not a string containing a valid
> Python identifier (not a keyword).


If the OP is doing something like this:

attributes = {"length": 15, "id": 2345}
# attribute names are known at compile time, created by the coder
obj.__dict__.update(attributes)
print obj.length
print obj.id

then checking that "length" and "id" are valid identifiers is hardly
necessary, any more than this would be:

class Spam:
def __init__(self, length, id):
check_valid_indentifier("length")
self.length = length
check_valid_indentifier("id")
self.id = id


But if he's doing something like this:

attributes = fetch_user_dict()
# attribute names aren't known until runtime
obj.__dict__.update(attributes)
for key in attributes:
print getattr(obj, key)

then it is also redundant to check for valid identifiers, since getattr()
doesn't need them. However, one might still need to test for accidental
clashes with pre-existing object attributes.

On the third hand, if that's the case then there seems to be no real
advantage to converting the dict into object attributes. Why not just
delegate to a saved copy of the dict?

class Spam:
def __init__(self, d):
self._d = d
def __getitem__(self, key):
return self._d[key]
def __setitem__(self, key, value):
self._d[key] = value

s = Spam({"a": 1, "something else": 2})
s["a"]
s["something else"]




--
Steven.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-03-2006

Steven D'Aprano wrote:
> On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote:
>
> > The OP might consider adding code to the __init__ method to check for
> > cases where the dictionary key is not a string containing a valid
> > Python identifier (not a keyword).

>

[snip]
> But if he's doing something like this:
>
> attributes = fetch_user_dict()
> # attribute names aren't known until runtime
> obj.__dict__.update(attributes)
> for key in attributes:
> print getattr(obj, key)
>
> then it is also redundant to check for valid identifiers, since getattr()
> doesn't need them.


but getattr() needs strings.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-03-2006
On Sat, 02 Dec 2006 17:00:15 -0800, John Machin wrote:

>
> Steven D'Aprano wrote:
>> On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote:
>>
>> > The OP might consider adding code to the __init__ method to check for
>> > cases where the dictionary key is not a string containing a valid
>> > Python identifier (not a keyword).

>>

> [snip]
>> But if he's doing something like this:
>>
>> attributes = fetch_user_dict()
>> # attribute names aren't known until runtime
>> obj.__dict__.update(attributes)
>> for key in attributes:
>> print getattr(obj, key)
>>
>> then it is also redundant to check for valid identifiers, since getattr()
>> doesn't need them.

>
> but getattr() needs strings.


Well, that's true, but if the dict is being read from a file or with
raw_input, the keys will naturally be strings. But even if it is some
arbitrary dict, the keys still don't need to be checked for valid
identifiers, merely checked for strings. And again, keeping the
keys/values in a dict instead of converting to object attributes naturally
solves that problem -- or rather, it isn't a problem that needs to be
solved.

Either way, I see no advantage to taking an arbitrary dict and converting
it into object attributes. It sounds to me like "when the only tool you
have is Java, everything looks like an object attribute" coding

I'd suggest that the "right" answer to the OP's original question "How
do I convert a dict to object attributes?" is "Don't do that", regardless
that it is technically possible.

Maybe I'm wrong and there are lots of really handy uses for such a tactic.
Can anyone suggest any?



--
Steven.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      12-03-2006
On 2006-12-02, John Machin <(E-Mail Removed)> wrote:
> Neil Cerutti wrote:
>> On 2006-12-02, Michel Claveau <(E-Mail Removed)> wrote:
>> > Hi!
>> >
>> > Yes.
>> >
>> > But...
>> >
>> > Try: d = {'a': 1, 'b': 2, 'def': 123}
>> >
>> > Ok, I go out...

>>
>> How to convert a list of strings into a list of integers:
>>
>> a = ['82', '4', '16']
>>
>> ai = [int(i) for i in a]
>>
>> Yes.
>>
>> But...
>>
>> Try: a = ['82', '4', '16', 'foo']
>>
>> Ok, I go out...

>
> Michel was making (part of) a valid point: dictionaries have
> more flexibility in choice of keys than objects have in
> attribute names. More completely:
>
> Suppose d.keys() produces ["one", "def", "foo bar", 3, "3"]
>
> o.one is OK.


I made the assumption that Michael was also the original poster,
and had somehow laid a clever trap. If I was wrong about that, my
apologies. It's one thing to ask how to convert 'a' and 'b' to
attributes, but quite another to convert arbitrary text.

> The OP might consider adding code to the __init__ method to
> check for cases where the dictionary key is not a string
> containing a valid Python identifier (not a keyword).


That raises the interesting question of what to do in that case.
Just letting an error occur might be perfectly good behavior.
Plus, I didn't know about...

> Observation: the keyword module's iskeyword() function gives an
> easy check. If one is supporting multiple versions of Python, a
> more complicated (overkill?) approach might be desirable: use
> the latest version of Python to generate a source file
> containing the latest keywords from keyword.kwlist.


Thanks for the pointer to keyword module. I hadn't noticed it
yet.

--
Neil Cerutti
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-03-2006

Neil Cerutti wrote:

>
> Thanks for the pointer to keyword module. I hadn't noticed it
> yet.


Bonus: you got an extremely fresh, scarcely used pointer -- I wasn't
aware of it myself till today

Cheers,
John

 
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
dict.has_key(x) versus 'x in dict' Paul Melis Python 48 12-15-2006 05:55 PM
Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__ Almad Python 8 12-14-2006 07:37 PM
converting dict to object rieh25 Python 1 12-02-2006 05:56 PM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments