By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

extracting order of class' attribute setting = statement executionor keyword assignments?

P: n/a
hi.
this was named [hello. what about ordered dictionaries] but it is
misleading.

i want to have the order of setting items in intrinsic dicts (keyword
args, or class attributes, etc). which is, a prdered dict trhat
matches the source-text sequences.

[ ordered dict is a dict that keep somewhere order of (the uniq) key
assignments. i know i can do them as dict's subclass. It is slower
than manual dict _and_ list handling, eh. (at python level, to c/++)]

why? i want to use python (syntax) instead of (monstrous) XML et al
pseudo-languages (e.g. config file, hierarchical record / grammar
descriptions etc).

[simplistic example]:
consider a logging record of 3 fields - time, user, IP - in this order.
it could be described as:
class LogRecord_Schema:
time = str
user = str
IP = str
and by whatever means i want to have these fields in this order, may
or may not be a class.

Any simple python-parseable syntax is okay, as long it is not the clumsy:
LogRecord_Schema = [
('time', str),
('user', str),
('IP', str),
]

[eo simplistic example]
usages:
* external:
- anything Record-related (database, url, etc) may or does care
about order of fields, etc.
* internal:
- function keyword-args, class attributes etc. 'grammatical syntax'
generated dicts DO have an order as the python text is parsed
sequentialy.
There's still no complete symmetry in python between list and dict,
human provides a sequence in the source which is always lost in the
dict (so clumsy lists of tuples are to be used instead).

for example, before, list( 1,2,3 ) did work, but dict( a=1,b=2,c=3)
dod not. This was easily workarounded then by
def dict( **k); return k
Now you dont need this anymore, but sequential mappings are a problem
- there's no notion of order of any dicts used internaly in python -
and they are used a lot, actualy any code-frame has (ordered!) array
of variables that are later put into a dict - and the order is gone.
The frame.f_locals is that dict.

i've looked at exec/eval sources, they do rely on PyDict_SetItem() for
the global and local dicts. i.e. if one makes class dictOrder(dict),
no __setitem__ will be called, but the dict will be filled underground.

i know it is useful/faster not to care about order but one always can
do so over an ordered dict, either accepting it ordered or skipping
the ordering layer.
Or have ordering being turned on/off somehow.
Or just have frame.f_locals as the actual list of tuples...

no going to C/++ please, i know what to do there.

the only ways i find is parse by hand - either simulate execution,
exec line by line, and store diffs in locals() in order, or use parser
into ASTs and do something there. Latter one is safer but uglier. Any
other ideas?

i wanted to share the idea - which is actualy from a long ago - that
the way people write things does matter ('way' here in all meanings).

Python already a lot to say here with using the
identation-as-scope/block-structure, why not about the sequence _when_
it matters?.

plz CC me if any reasonable reply, i'm not subscribed.

ciao

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Check out my cookbook item for a length-limited O(1) LRU Cache/Paging
implementation. You can toss the lengh-limititing and reorder-on-read
semantics to get what you want.

http://aspn.activestate.com/ASPN/Coo.../Recipe/252524

Below is an implementation that will keep order, which you can easily
iterate over, pull out a list with keys() or values() or items()...

- Josiah

class LRU:
"""
Implementation of a length-limited O(1) LRU queue.
Built for and used by PyPE:
http://pype.sourceforge.net
Copyright 2003 Josiah Carlson.
"""
class Node:
def __init__(self, prev, me):
self.prev = prev
self.me = me
self.next = None
def __init__(self, pairs=[]):
self.d = {}
self.first = None
self.last = None
for key, value in pairs:
self[key] = value
def __contains__(self, obj):
return obj in self.d
def __getitem__(self, obj):
return self.d[obj].me[1]
def __setitem__(self, obj, val):
if obj in self.d:
del self[obj]
nobj = self.Node(self.last, (obj, val))
if self.first is None:
self.first = nobj
if self.last:
self.last.next = nobj
self.last = nobj
self.d[obj] = nobj
def __delitem__(self, obj):
nobj = self.d[obj]
if nobj.prev:
nobj.prev.next = nobj.next
else:
self.first = nobj.next
if nobj.next:
nobj.next.prev = nobj.prev
else:
self.last = nobj.prev
del self.d[obj]
def __iter__(self):
cur = self.first
while cur != None:
cur2 = cur.next
yield cur.me[1]
cur = cur2
def iteritems(self):
cur = self.first
while cur != None:
cur2 = cur.next
yield cur.me
cur = cur2
def iterkeys(self):
return iter(self.d)
def itervalues(self):
for i,j in self.iteritems():
yield j
def keys(self):
return self.d.keys()

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.