bsddb for k, v in db.items(): do order the numbers ?

M

martijn

WHen I use the code below and printing all the results i get this:
------
0 1 10
11 2 3
4 5 6
7 8 9
------
But I want
------
0 1 2
3 4 5
6 7 8
9 10 11
------

Thanks for helping

import bsddb

def addRow(key,val):
db[key] = key
print key,

db = bsddb.btopen('test3.db','n')

#maar 3 kolomen
for i in range(4):
addRow('%d'%(i*2+0+i),'test%d'%(i*2+0+i))
addRow('%d'%(i*2+1+i),'test%d'%(i*2+1+i))
addRow('%d'%(i*2+2+i),'test%d'%(i*2+2+i))

#-----
print 'kolom1','kolom2','kolom3'

p=0
for k, v in db.items():
if p == 0:
p = p+1
print v,
elif p == 1:
p = p+1
print v,
elif p == 2:
p=0
print v,
print
 
C

Christopher De Vries

WHen I use the code below and printing all the results i get this:
------
0 1 10
11 2 3
4 5 6
7 8 9
------
But I want
------
0 1 2
3 4 5
6 7 8
9 10 11
------

If you want your key, value pairs in a certain order you have to sort them
yourself. Dictionaries and bsddb keys are unsorted.

Chris
 
S

Steve Holden

Christopher said:
If you want your key, value pairs in a certain order you have to sort them
yourself. Dictionaries and bsddb keys are unsorted.

Remember, also, that the keys are strings, so you'll need to convert
them to numbers if you want them to sort numerically - otherwise "11"
will come before "2".

regards
Steve
 
D

Denis S. Otkidach

If you want your key, value pairs in a certain order you have to sort
them yourself. Dictionaries and bsddb keys are unsorted.

You are not right, records in BTree (btopen) are certainly sorted. For
positive integers you can pack keys with struct.pack('>I', value).
 
C

Christopher De Vries

You are not right, records in BTree (btopen) are certainly sorted. For
positive integers you can pack keys with struct.pack('>I', value).

You're right... I missed the btopen (rather a key thing to miss I know, but
when you have a toddler in your lap it sometimes happens). Sorry about that.

Chris
 
M

martijn

uhm i'm trying to make a very simple but large database:

Let's say I want these fields : |name|age|country|

Then I can't do this because I use the same key

db["name"] = 'piet'
db["age"] = '20'
db["country"] = 'nl'
# same keys so it wil overwrite
db["name"] = 'jan'
db["age"] = '40'
db["country"] = 'eng'

But how does other people use bsddb then ?
- with a hidden |int like below ?

db["name|0"] = 'jan'
db["age|1"] = '40'
db["country|2"] = 'eng'

- do a little math to
first is name
sec is age
third is country

db["0"] = 'jan'
db["1"] = '40'
db["2"] = 'eng'

pointer=0
for k, v in db.items():
if pointer =3:
poiner = 0
#next 3 fields
 
?

=?ISO-8859-1?Q?Ulf_G=F6ransson?=

uhm i'm trying to make a very simple but large database:

Let's say I want these fields : |name|age|country|

Then I can't do this because I use the same key

db["name"] = 'piet'
db["age"] = '20'
db["country"] = 'nl'
# same keys so it wil overwrite
db["name"] = 'jan'
db["age"] = '40'
db["country"] = 'eng'

But how does other people use bsddb then ?
- with a hidden |int like below ?

db["name|0"] = 'jan'
db["age|1"] = '40'
db["country|2"] = 'eng'

- do a little math to
first is name
sec is age
third is country

db["0"] = 'jan'
db["1"] = '40'
db["2"] = 'eng'

pointer=0
for k, v in db.items():
if pointer =3:
poiner = 0
#next 3 fields

I don't know about normal but I'd probably do something like

db['piet'] = repr(['piet', 20, 'nl'])

or maybe

db['jan'] = repr({"name":'jan', "age":40, "country":'eng'})

That should hold until Piet #2 comes along, then I might add another
level of lists...

With more complicated data I'd do as the docs say and take a look at
marshal or pickle instead of using repr(). And use a class instead of
lists or dicts...

/ug
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,222
Messages
2,571,137
Members
47,754
Latest member
Armand37T7

Latest Threads

Top