Re: slicing a bsddb table, eg. for rec in bsddb["AArdvark":"zebra"]:print rec

Discussion in 'Python' started by Skip Montanaro, Jun 12, 2005.

  1. 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
     
    Skip Montanaro, Jun 12, 2005
    #1
    1. Advertising

  2. Re: slicing a bsddb table, eg. for rec in bsddb["AArdvark":"zebra"]: print rec

    On Sun, 12 Jun 2005 16:49:57 -0500, Skip Montanaro <> wrote:

    >
    > 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.
    >

    Yes, but if you have an implicit sort assumption for your keys, you can treat them
    as a sequence, and slice out the keys of interest and get their values, e.g.,
    (no guarantees, not tested beyond what you see, that I just hacked here ;-)

    >>> class SliceDict(dict):

    ... def __getitem__(self, i):
    ... if type(i) is slice:
    ... keys = sorted(self.keys())
    ... start = i.start; stop = i.stop
    ... try: start = keys.index(start)
    ... except ValueError:
    ... if start is not None: raise KeyError, start
    ... try: stop = keys.index(stop )
    ... except ValueError:
    ... if stop is not None: raise KeyError, stop
    ... return [dict.__getitem__(self, k) for k in keys[start:stop]]
    ... else:
    ... return dict.__getitem__(self, i)
    ...
    >>> sd = SliceDict(a=1,b=2,z=26,k=11)
    >>> sd['b':'z']

    [2, 11]
    >>> sd['b':]

    [2, 11, 26]
    >>> sd[:'z']

    [1, 2, 11]
    >>> sd['b':'z']

    [2, 11]
    >>> sd['b']

    2
    >>> sorted(sd.items())

    [('a', 1), ('b', 2), ('k', 11), ('z', 26)]
    >>> sd['x':'k']

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 8, in __getitem__
    KeyError: 'x'
    >>> sd['x']

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 14, in __getitem__
    KeyError: 'x'

    Hm, None as an actual key might be a little problematical though ;-)

    I would think this could be a handy way to get data base records that were selected
    from a range of sorted keys using sql and loading a dict like the above.

    Regards,
    Bengt Richter
     
    Bengt Richter, Jun 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homer J Simpson
    Replies:
    17
    Views:
    10,340
    hernanbalda
    Sep 18, 2008
  2. Twist Around

    Zebra Printing Language

    Twist Around, Oct 23, 2003, in forum: Python
    Replies:
    2
    Views:
    5,546
    George Kinney
    Oct 24, 2003
  3. Neville C. Dempsey
    Replies:
    0
    Views:
    390
    Neville C. Dempsey
    Jun 12, 2005
  4. =?Utf-8?B?UGF1bCBCdXp6YSwgb2xkc3RlciB1c2luZyBuZXcg

    Print Label on LOCAL Zebra Printer from WEBSITE Inventory DB

    =?Utf-8?B?UGF1bCBCdXp6YSwgb2xkc3RlciB1c2luZyBuZXcg, Apr 25, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,406
    Alexey Smirnov
    Apr 26, 2007
  5. Replies:
    0
    Views:
    364
Loading...

Share This Page