slicing a bsddb table, eg. for rec in bsddb["AArdvark":"zebra"]: print rec
Skip Montanaro
skip at pobox.com
Sun Jun 12 17:49:57 EDT 2005
Neville> # I was expecting a slice of an index file to yield a
Neville> # generator so not all the records need to be read from disk....
Slicing is a feature of sequence types, not mapping types.
>>> import string
>>> d = dict(zip(string.lowercase, string.uppercase))
>>> d
{'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', ...}
>>> d["a":"z"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unhashable type
>>> import UserDict
>>> d2 = UserDict.UserDict(d)
>>> d2
{'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', ...}
>>> d2["a":"z"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/Users/skip/local/lib/python2.5/UserDict.py", line 17, in __getitem__
def __getitem__(self, key): return self.data[key]
TypeError: unhashable type
The unhashable type it's referring to is the slice object generated by the
"a":"z" notation:
>>> hash(slice("a", "z"))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unhashable type
If you look at help on the slice type, you'll see that it takes three args:
class slice(object)
| slice([start,] stop[, step])
|
| Create a slice object. This is used for extended slicing
| (e.g. a[0:10:2]).
|
Step sizes really only make sense for sequence indices.
Skip
More information about the Python-list
mailing list