473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Jul 18 '05 #1
7 1479
On Mon, Feb 28, 2005 at 08:30:59AM -0800, ma*****@gamecreators.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*****@gamecreators.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.idolstarastronomer.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|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 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*****@gamecreators.nl wrote:
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 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":'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
Jul 18 '05 #8

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

Similar topics

0
by: John D. | last post by:
We are using OpenBSD latest release of Python (2.3), and want to use the "bsddb" module. Our python release has a directory called "bsddb", but when we try and import it, it says >>> import...
3
by: Harry Pehkonen | last post by:
Stats: Python2.3 windows2000 professional If I have ``Full Control'' of a bsddb file, no problem: >>> import bsddb >>> a = bsddb.btopen("c:/sharedrw/db/npanxx2pseudo.db", "r") >>> a.close()...
0
by: Jane Austine | last post by:
Hello. How do I change the BTree sorting order with bsddb, instead of the default lexicographical order? After studying sleepycat's document, I found there is a function call for setting the...
4
by: Michele Simionato | last post by:
I was browsing through the source tree of Python 2.4b2 and in Lib/bsddb/test I found a lot of interesting stuff. It seems that the support for the bsd database is much better than documented in the...
0
by: Barry | last post by:
I have python2.4.1 installed on two machines: -- one is Fedora core 1, where the bsddb module works fine -- one is Redhat ES 3.0, and I installed mysql 4.1 (and mysql-python2.1) after putting the...
16
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
2
by: lazy | last post by:
Hi, I have a dictionary something like this, key1=>{key11=> , key12=> , .... } For lack of wording, I will call outer dictionary as dict1 and its value(inner dictionary) dict2 which is a...
1
by: BjornT | last post by:
Have a rather big problem with bsddb I can't figure out. I upgraded an Ubuntu machine from 7.05 to 7.10 which upgraded python to 2.5.1 I run a local website that uses bsddb, and suddenly I get a...
2
by: cocobear | last post by:
How to deal with multiple databases in an file. I want to get the content of several databases. it's the code I wrote: $ python Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) on linux2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.