473,747 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('t est3.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','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.

Similar topics

0
1476
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 bsddb Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/local/lib/python2.3/bsddb/__init__.py", line 40, in ? import _bsddb ImportError: No module named _bsddb Where can I find this module, and how do I get it to...
3
2684
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
1258
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 compare callback, set_bt_compare. It even seems like got into pybsddb patch list (http://sourceforge.net/tracker/index.php?func=detail&aid=532308&group_id=13900&atid=313900). However, the current Python source tree (cvs) doesn't include the patch.
4
4487
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 standard library http://www.python.org/dev/doc/devel/lib/module-bsddb.html. There are tests for locking, transactions, join, etc. Is there some documentation anywhere? I could infer what I need from the tests but maybe there is already some...
0
1341
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 newer python on the machine. python2.2, which came with Redhat ES, works fine, so I suppose I messed up the build. I much appreciate any insight in how to fix this.
16
2464
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 (se below) then I run a query giving me a running total, which give me the first stock in the batch purchased by an individual, then I use this number and add the number of shares in order to find the number of the individuals' last share. So...
2
2539
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 dictionary of small fixed size lists(2 items) The key of the dictionary is a string and value is another dictionary
1
1750
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 version mismatch error with bsddb and I can't access my database anymore I noticed in the 2.5.1 release notes: - fixed a bug with bsddb.DB.stat: the flags and txn keyword arguments were transposed.
2
1689
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 Type "help", "copyright", "credits" or "license" for more information.
0
8818
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8234
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.