[Zope] Mixing keys

Paul Winkler pw_lists at slinkp.com
Fri Oct 31 10:58:21 EST 2003


On Fri, Oct 31, 2003 at 05:41:12PM +0900, Harm_Kirchhoff at mail.digital.co.jp wrote:
> I am new to ZOPE, so maybe this question is too simple:
> 
> I would like to use a BTRee (OOBTree) to store information.
> For one type of sub-branch, I would like to use a continuous series of 
> integers as keys:
> 
> dbroot['SE_HMI']={ '2003' : { 'jrl' : { 0 : <a tuple of data>}} 
> dbroot['SE_HMI']={ '2003' : { 'jrl' : { 1 : <a tuple of data>}}
> dbroot['SE_HMI']={ '2003' : { 'jrl' : { 2 : <a tuple of data>}} 
> ... and so on ...
> 
> all other branches besides 'jrl' will have string keys.
> 
> I read in the ZODB,ZEO programming guide (www.zope.org/Wikis/ZODB/guide/)
> that you should only use objects of a SINGLE type as keys.
> 
> I could write a generator, that would create strings, by counting from 0 
> to 255,
> however, that would destroy the sort order, because 'ABX' would sort 
> before 'X', allthough it would represent a much larger number.

Pad with zeroes and it'll sort numerically. Like this:

>>> import string
>>> keys = [string.zfill(str(i), 3) for i in range(255)]
>>> keys.sort() 
>>> keys[:15]
['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014']
>>> keys[-10:]
['245', '246', '247', '248', '249', '250', '251', '252', '253', '254']


-- 

Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's ULTRA MAN!
(random hero from isometric.spaceninja.com)



More information about the Zope mailing list