Python DB that supports numbers
Edward C. Jones
edcjones at erols.com
Thu Feb 28 18:48:56 EST 2002
Randall Hopper wrote:
> I'm looking for a very simple Python DB which supports:
>
> 1) ints and floats as keys and values,
> 2) performs DB access in the client, and
> 3) stores the DB on disk.
>
> Just a simple DB for quick projects without the setup headache of
> server-based database systems.
>
> For example, on the following table I just want to: "select * where
> column_1 is 1". I'm using raw binary files now, but I'd prefer something
> more self-describing like an SQL table.
>
> 1 10 1.814 177.1 13.61 73.2
> 1 5 1.624 182.5 -9999 -9999
> 1 2 1.259 185.9 13.24 73.9
> 2 10 2.672 181.4 13.59 72.5
> 2 5 2.423 183.2 -9999 -9999
> 2 2 1.902 190.7 13.27 73
> 3 10 3.208 181.9 13.46 72.1
> 3 5 2.787 186.6 -9999 -9999
> 3 2 2.17 194.7 13.16 72.3
>
> Does anything like this exist in Python?
I would use a Python dictionary which I would store to disk using module
cPickle.
table = {}
def AddRow(table, row):
key = row[0]
if not table.has_key(key):
table[key] = []
table[key].append(row)
AddRow(table, [1, 10, 1.814, 177.1, 13.61, 73.2])
AddRow(table, [1, 5, 1.624, 182.5, -9999, -9999])
# ...
AddRow(table, [3, 2, 2.17, 194.7, 13.16, 72.3])
print table[1]
In the C++ Standard Template Library, there is a thing called a
Multimap. It is like a Python dictionary except that each key can occur
more than once. My Python name for this concept is "MultiDict". A
MultiDict can be implemented in Python as a dictionary whose values are
lists (like table[1] above). A MultiDict is a natural way to process
sets of n-tuples.
I have attached MultiDict.py which I use when the approach above is too
simple but MySQL is overkill.
Thanks,
Ed Jones
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MultiDict.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20020228/f38c6be1/attachment.ksh>
More information about the Python-list
mailing list