Unanswered Questions in python.faqts.com
Will Ware
wware at world.std.com
Wed Aug 23 21:46:31 EDT 2000
Charles Hixson (charleshixsn at earthlink.net) wrote:
> Unfortunately, the FAQTS question
> format caused an oversimplified question... my ideal form
> of the answer would allow a database to be merged into the language with
> btree access as the underlying format, and nominal form looking like a cross
> between a dictionary and a sequence. I... want to slice on...
> key value (like a between query).
> I know that this is possible via subroutine calls, but I was wondering if
> there were any way that made more direct use of the preexisting syntax,
> e.g.: abrasax = litmus {"acid" : "base"} or some such.
I'm not aware of any such thing. I tried hacking together a version of
__getslice__ that would take non-integer arguments, but alas, Python
checks the argument types before it passes them to __getslice__. This
means you need to define your own slicing operation. This seems to work:
import sys, types
class SeqDictHybrid:
def __init__(self, keylist, valuelist):
self.d = { }
for k, v in map(None, keylist, valuelist):
self.d[k] = v
self.keylist = keylist
self.valuelist = valuelist
def myslice(self, i=0, j=sys.maxint):
if type(i) != types.IntType:
i = self.keylist.index(i)
if type(j) != types.IntType:
j = self.keylist.index(j)
return self.valuelist[i:j]
litmus = SeqDictHybrid(['wayAcid', 'acid', 'neutral', 'base', 'wayBase'],
[5.0, 6.0, 7.0, 8.0, 9.0]) # pH, if I recall rightly
print litmus.myslice('acid', 'base') --> [6.0, 7.0]
Adding btrees and clever insertions is an exercise left to the reader.
--
# - - - - - - - - - - - - - - - - - - - - - - - -
# Resistance is futile. Capacitance is efficacious.
# Will Ware email: wware @ world.std.com
More information about the Python-list
mailing list