[Python-3000] Immutable bytes type and dbm modules

"Martin v. Löwis" martin at v.loewis.de
Tue Aug 7 05:45:00 CEST 2007


> For instance, it is quite common to use integers as keys.  If you are  
> inserting keys in order, it is about a hundred times faster to encode  
> the ints in big-endian byte order than than little-endian:
> 
> class MyIntDB(object):
> 	def __setitem__(self, key, item):
>                self.db.put(struct.pack('>Q', key), serializer(item))
>          def __getitem__(self, key):
>                return unserializer(self.db.get(struct.pack('>Q', key)))

I guess Guido wants you to write

class MyIntDB(object):
  def __setitem__(self, key, item):
    self.db.put(struct.pack('>Q', key).encode("latin-1"),
                serializer(item))
  def __getitem__(self, key):
    return unserializer(self.db.get(
       struct.pack('>Q', key).encode("latin-1"))

here.

> How do you envision these types of tasks being accomplished with  
> unicode keys?  It is conceivable that one could write a custom  
> unicode encoding that accomplishes this, convert the key to unicode,  
> and pass the custom encoding name to the constructor.

See above. It's always trivial to do that with latin-1 as the encoding
(I'm glad you didn't see that, either :-).

Regards,
Martin


More information about the Python-3000 mailing list