[Python-ideas] What would you call such an object (was: ordered dict)
LD 'Gus' Landis
ldlandis at gmail.com
Thu Apr 26 18:00:16 CEST 2007
Hi,
I am wondering if in y'alls opinion the following is "just a dictionary" or
is it a different kind of object that has some of the characteristics of a
dictionary, and has order.
If y'all think that it is "just a dictionary", then how does one override the
notion of a "hash" for the key in a dictionary and make it some other
ordered structure (e.g. a B-Tree, AVL, etc). (Please no flame toss to
some other list -- this is a "use" of an ordered "ordered dict")
I don't know what such a critter would be called (in Python). It has the
name of "array" language where it is central, but don't want to go into that.
The object has the following characteristics:
- It is indexed by keys which are immutable (like dicts)
- Each key has a single value (like dicts)
- The keys are ordered (usually a B-Tree underneath)
- The keys are "sorted" yielding a hierarchy such that (using Python tuples
as an example and pseudo Python):
object = {
(0,): "value of node",
(0,"name") : "name of node",
(0,"name",1): "some data about name",
(1,): "value of another node",
(1,2,3): "some data value",
(2,): 2,
(2,2,"somekey",1): 32,
(3,): 28,
("abc",1,2): 14
}
- Introspection of the object allows walking the keys by hierarchy,
using the above:
key = object.order(None) -> 0
key = object.order(key) -> 1
key = object.order(key) -> 2
key = object.order(key) -> 3
key = object.order(key) -> "abc"
key = object.order(key) -> None
The first key is fetched when (None) is the initial key (or last
key if modifier is -1)
Supplying a modifier (-1, where 1 is default of forward, -1 is
reverse) in the call
traverses the keys in the reverse order from that shown above.
- Introspection of the key results in:
hasdata = object.data(key)
=0 no subkeys no data for 'key' (in the above (39) would have no
subkeys, no data)
=1 no subkeys has data for 'key' (in the above (3) has no
subkeys, but has data)
=10 has subkeys no data for 'key' (in the above (2,2) has subkeys
but no data)
=11 has subkeys has data for 'key' (in the above (2) has subkeys
and has data)
- Introspection of object can yield "depth first" keys
key = object.query(None) -> (0,)
key = object.query(key) -> (0,"name")
key = object.query(key) -> (0,"name",1)
key = object.query(key) -> (1,)
key = object.query(key) -> (1,2,3)
key = object.query(key) -> (2,)
key = object.query(key) -> (2,2,"somekey",1)
key = object.query(key) -> (3,)
key = object.query(key) -> ("abc",1,2)
key = object.query(key) -> None
Like object.order(), object.query() has the same "reverse" (using
-1) option to walk
the keys in a reverse order.
- Having an iterator over order/query:
for key in object.ordered([start[,end]):
for key in object.queryed([start[,end]): (spelling?? other alternative)
- Set/get of
object[(0,"name")] = "new name of node"
print object[(0,"name")]
Cheers,
--ldl
--
LD Landis - N0YRQ - de la tierra del encanto
3960 Schooner Loop, Las Cruces, NM 88012
651/340-4007 N32 21'48.28" W106 46'5.80"
"If a thing is worth doing, it is worth doing badly." –GK Chesterton.
An interpretation: For things worth doing: Doing them, even if badly,
is better than doing nothing perfectly (on them).
More information about the Python-ideas
mailing list