is this data structure build-in or I'll have to write my own class?

J

Jorge Vargas

I need a data structure that will let me do:

- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.

in case there isn't one. I was thinking having a base class like Bunch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308 and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice. (in the list and in __dict__, is this the only
way of doing it?
 
J

Jarek Zgoda

Jorge Vargas napisa³(a):
- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.

These are all attributes of standard Python lists.
in case there isn't one. I was thinking having a base class like Bunch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308 and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice. (in the list and in __dict__, is this the only
way of doing it?

What is your objective? From the description of this recipe I cann't get
your use case.
 
S

subeen

I think you should go for 'dictionary' that is a built-in data
structure of Python.


regards,
Subeen
http://love-python.blogspot.com/


These are all attributes of standard Python lists.

probably I confused you with the "or index" part. I want to be able to
do item.value1 or item['value1'] the list can't do this.
What is your objective? From the description of this recipe I cann't get
your use case.

I got an xml object which feed me in data. in a simple format
<item>
<propety1>foo</propety1>
<value1>bar</value1>
<propety2>baz</propety2>
<value2>bal</value2>
</item>

I want to turn this into a datastructure I can manipulate in my
program for example.
print myItem.property1
if myItem.property1[value1] > 0:

print 'ok'
{'property1':'value1','property2,'value2'}
"We read Knuth so you don't have to." (Tim Peters)
 
J

Jorge Vargas

dict doesn't maintains order.
a=[(3,2),(2,2)]
dict(a).items() [(2, 2), (3, 2)]
a=[(3,2),(2,2)]
assert dict(a).items()==a
Traceback (most recent call last):
[(2, 2), (3, 2)]


I think you should go for 'dictionary' that is a built-in data
structure of Python.


regards,
Subeen
http://love-python.blogspot.com/


2008/2/20 Jarek Zgoda said:
- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.
These are all attributes of standard Python lists.

probably I confused you with the "or index" part. I want to be able to
do item.value1 or item['value1'] the list can't do this.
in case there isn't one. I was thinking having a base class like Bunch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice. (in the list and in __dict__, is this the only
way of doing it?
What is your objective? From the description of this recipe I cann't get
your use case.

I got an xml object which feed me in data. in a simple format
<item>
<propety1>foo</propety1>
<value1>bar</value1>
<propety2>baz</propety2>
<value2>bal</value2>
</item>

I want to turn this into a datastructure I can manipulate in my
program for example.
print myItem.property1
if myItem.property1[value1] > 0:

print 'ok'
print myItem
{'property1':'value1','property2,'value2'}
"We read Knuth so you don't have to." (Tim Peters)
 
B

bearophileHUGS

subeen:
I think you should go for 'dictionary' that is a built-in data
structure of Python.

The OP asks this too:
maintain the order (for iter and print)

So I think an "ordered dict" is fitter, the op can search for an odict
implementation, in the cookbook too (I too have written one, but it's
useful only if you have to delete keys often, because it's O(1) for
that operation too, but generally slow if you don't need to delete
keys too).

Bye,
bearophile
 
L

Larry Bates

Jorge said:
I need a data structure that will let me do:

- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.

in case there isn't one. I was thinking having a base class like Bunch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308 and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice. (in the list and in __dict__, is this the only
way of doing it?

Sounds like a good time to learn ElementTree (included in Python 2.5 but
available for earlier versions).

-Larry
 
J

jay graves

I got an xml object which feed me in data. in a simple format
<item>
<propety1>foo</propety1>
<value1>bar</value1>
<propety2>baz</propety2>
<value2>bal</value2>
</item>

If it is XML why not use ElementTree or some other XML DOM library?

....
Jay Graves
 
D

Duncan Booth

Jorge Vargas said:
I was thinking having a base class like Bunch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308 and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice

You do realise I hope that just because the key appears in both a
dictionary and a list doesn't mean you have two copies of the key? If the
same object is used in a dictionary and a list then all you have is an
extra reference to the same object.
 
J

Jorge Vargas

Sounds like a good time to learn ElementTree (included in Python 2.5 but
available for earlier versions).
I am using ET, to fetch the data from the XML, after that I want a
plain python object. for the rest of the program.
 
T

thebjorn

Jorge said:
I need a data structure that will let me do:
- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.
[...]

Sounds like a good time to learn ElementTree (included in Python 2.5 but
available for earlier versions).

I am using ET, to fetch the data from the XML, after that I want a
plain python object. for the rest of the program.

Ok, you just lost me... Why do you thin ET is not appropriate (*)? It
fits all your requirements, is optimized for representing hierarchical
data (especially xml), it is fast, it is well tested, it has a
community of users, it is included in the standard library, etc., etc.

....maybe I didn't grok what you meant by "plain python object"?

-- bjorn

(*) I just had a flashback to the movie ET -- the scene when he's in
the closet ;-)
 
M

mkPyVS

Jorge Vargas wrote:
I need a data structure that will let me do:
- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.
[...]
Sounds like a good time to learn ElementTree (included in Python 2.5 but
available for earlier versions).
I am using ET, to fetch the data from the XML, after that I want a
plain python object. for the rest of the program.

Ok, you just lost me... Why do you thin ET is not appropriate (*)? It
fits all your requirements, is optimized for representing hierarchical
data (especially xml), it is fast, it is well tested, it has a
community of users, it is included in the standard library, etc., etc.

...maybe I didn't grok what you meant by "plain python object"?

-- bjorn

(*) I just had a flashback to the movie ET -- the scene when he's in
the closet ;-)

This isn't so optimal but I think accomplishes what you desire to some
extent... I *think* there is some hidden gem in inheriting from dict
or an mapping type that is cleaner than what I've shown below though.

class dum_struct:
def __init__(self,keyList,valList):
self.__orderedKeys = keyList
self.__orderedValList = valList
def __getattr__(self,name):
return self.__orderedValList[self.__orderedKeys.index(name)]


keys = ['foo','baz']
vals = ['bar','bal']

m = dum_struct(keys,vals)

print m.foo
 
C

Carl Banks

I need a data structure that will let me do:

- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.

in case there isn't one. I was thinking having a base class like Bunchhttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308and on
top of that keeping a list of the keys and pop/push to the list when
adding/deleting items. I don't like this idea because I'll have to
keep each key twice. (in the list and in __dict__, is this the only
way of doing it?

OrderedDict is usually the term used here for this (not to be confused
with SortedDict, which is a mapping type with identically sorted
keys). It's asked for pretty often but no one's stepped up to
implement one for the standard library.


Carl Banks
 
R

Robert Bossy

mkPyVS said:
This isn't so optimal but I think accomplishes what you desire to some
extent... I *think* there is some hidden gem in inheriting from dict
or an mapping type that is cleaner than what I've shown below though.

class dum_struct:
def __init__(self,keyList,valList):
self.__orderedKeys = keyList
self.__orderedValList = valList
def __getattr__(self,name):
return self.__orderedValList[self.__orderedKeys.index(name)]


keys = ['foo','baz']
vals = ['bar','bal']

m = dum_struct(keys,vals)

print m.foo

Let's add:
__getitem__(self, key):
return self.__orderedValList[key]


in order to have: m.foo == m[0]

RB
 
J

Jorge Vargas

Jorge Vargas wrote:
I need a data structure that will let me do:
- attribute access (or index)
- maintain the order (for iter and print)
- be mutable.
[...]
Sounds like a good time to learn ElementTree (included in Python 2.5 but
available for earlier versions).

I am using ET, to fetch the data from the XML, after that I want a
plain python object. for the rest of the program.

Ok, you just lost me... Why do you thin ET is not appropriate (*)? It
fits all your requirements, is optimized for representing hierarchical
data (especially xml), it is fast, it is well tested, it has a
community of users, it is included in the standard library, etc., etc.
sorry for the late reply, I'm running queries on a webservice, nothing
fancy just http post/get and they give me an XML, the XML has a ton of
information, say 200 nodes, from that I only need 1 node that has 6
more inside, so keeping the whole 200 nodes in memory is not
apropiate, I could make a new ET object with only that part but, I
this data should be fetched every X hours, so I'll prefer it to be my
object so I can code the cache part.
 
J

Jorge Vargas

OrderedDict is usually the term used here for this (not to be confused
with SortedDict, which is a mapping type with identically sorted
keys). It's asked for pretty often but no one's stepped up to
implement one for the standard library.
Yes, what I really nice is an OrderedDict, interesting ... maybe it
could be a nice contribution for
http://docs.python.org/lib/module-collections.html interesting I just
read the docs and it's there. I look around and those seems to be
implement on the C layer.
 
J

Jorge Vargas

mkPyVS said:
This isn't so optimal but I think accomplishes what you desire to some
extent... I *think* there is some hidden gem in inheriting from dict
or an mapping type that is cleaner than what I've shown below though.

class dum_struct:
def __init__(self,keyList,valList):
self.__orderedKeys = keyList
self.__orderedValList = valList
def __getattr__(self,name):
return self.__orderedValList[self.__orderedKeys.index(name)]


keys = ['foo','baz']
vals = ['bar','bal']

m = dum_struct(keys,vals)

print m.foo

Let's add:
__getitem__(self, key):
return self.__orderedValList[key]
this seems like a good start, maybe replacing the internal structures
with a list and a dict will get better performance, I'll work in it a
little, if I get something productive I'll probably post a recipe
in order to have: m.foo == m[0]

RB
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top