473,747 Members | 2,260 Online

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

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

db[key] = key
print key,

db = bsddb.btopen('t est3.db','n')

#maar 3 kolomen
for i in range(4):

#-----
print 'kolom1','kolom 2','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

Jul 18 '05 #1
7 1499
On Mon, Feb 28, 2005 at 08:30:59AM -0800, ma*****@gamecre ators.nl wrote:
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
Jul 18 '05 #2
Christopher De Vries wrote:
On Mon, Feb 28, 2005 at 08:30:59AM -0800, ma*****@gamecre ators.nl wrote:
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.

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
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #3
oyea, I must convert it to numbers ;)

Jul 18 '05 #4
On Mon, 28 Feb 2005 11:48:44 -0500
Christopher De Vries <de*****@dyn.id olstarastronome r.com> wrote:
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).

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #5
On Wed, Mar 02, 2005 at 01:31:04PM +0300, Denis S. Otkidach wrote:
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
Jul 18 '05 #6
uhm i'm trying to make a very simple but large database:

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

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 like bsddb because of the speed and it can handle big files,
but what is the normal way of using it ?

Thanks for helping

Jul 18 '05 #7
ma*****@gamecre ators.nl wrote:
uhm i'm trying to make a very simple but large database:

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

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 like bsddb because of the speed and it can handle big files,
but what is the normal way of using it ?

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":'j an', "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
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.