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

svilen sdobrev at sistechnology.com
Wed Mar 10 12:45:41 EST 2004


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






More information about the Python-list mailing list