A database interface that mimics a python dictionary.
Project description
A database interface that acts like a python dictionary
Install
pip install dictabase
Create a new table
# Create a table representing users
from dictabase import BaseTable, RegisterDBURI
RegisterDBURI() # pass nothing to default to sqlite
class UserClass(BaseTable):
pass
newUser = New(UserClass, name='Grant', age=31)
newUser = New(UserClass, name='Bob', age=99)
# there is now a sqlite database containing the 2 users. Yup, thats it!
Look up items in the database
from dictabase import FindOne, FindAll
allUsers = FindAll(UserClass)
# allUsers is an iterable of all the UserClass objects that exists in the database
print('allUsers=', list(allUsers))
>> allUsers= [<UserClass: name='Grant', age=31>, <UserClass: name='Bob', age=99>]
user = FindOne(UserClass, name='Grant')
print('user=', user)
>>user= <UserClass: name='Grant', age=31>
user = FindOne(UserClass, name='NotARealName')
print('user=', user)
>>user= None
Read/Write to the database
# find an object that was previously put in the database user = FindOne(UserClass, name='Grant') # increment the age of the user by 1 user['age'] += 1 # Thats it! the database has now been updated with the new age
Drop a table
from dictabase import Drop Drop(UserClass, confirm=True) # the table has been removed from the database
Delete a specific row in a table
from dictabase import Delete # find an object that was previously put in the database user = FindOne(UserClass, name='Grant') Delete(user) # the user has been removed from the database
Advanced Usage
You can only store simple types like int, str, datetime in the database. To store more complicated objects, like list, dict, or any arbitrary type, override the DumpKey() and LoadKey() method. Using these methods, you can convert complex types into these more simple types.
from dictabase import (
BaseTable,
FindOne,
New,
RegisterDBURI,
Drop,
)
import json
RegisterDBURI()
class Page(BaseTable):
pass
Drop(Page, confirm=True) # clear the table for this test
class Book(BaseTable):
def DumpKey(self, key, value):
# this is called when putting info into the database
if key == 'pages':
ret = [page['id'] for page in value] # only store the page id as a json'd list of ints
return json.dumps(ret)
else:
return value
def LoadKey(self, key, value):
# this is called when extracting info from the database
if key == 'pages':
ret = json.loads(value)
ret = [FindOne(Page, id=ID) for ID in ret]
else:
return value
Drop(Book, confirm=True) # clear the table for this test
# Create a new book
book = New(Book)
book['pages'] = [] # this will hold our pages
print('book=', book)
>> book= <Book: id=1(type=int), pages=[](type=list)>
# Fill the book with pages
for i in range(10):
page = New(
Page,
words='These are words {}'.format(i),
parentBookID=book['id']
)
book['pages'].append(page)
print('book=', book)
>> book= <Book: id=1(type=int), pages=[<Page: words=These are words 0(type=str), parentBookID=1(type=int), id=1(type=int)>, <Page: words=These are words 1(type=str), parentBookID=1(type=int), id=2(type=int)>, <Page: words=These are words 2(type=str), parentBookID=1(type=int), id=3(type=int)>, <Page: words=These are words 3(type=str), parentBookID=1(type=int), id=4(type=int)>, <Page: words=These are words 4(type=str), parentBookID=1(type=int), id=5(type=int)>, <Page: words=These are words 5(type=str), parentBookID=1(type=int), id=6(type=int)>, <Page: words=These are words 6(type=str), parentBookID=1(type=int), id=7(type=int)>, <Page: words=These are words 7(type=str), parentBookID=1(type=int), id=8(type=int)>, <Page: words=These are words 8(type=str), parentBookID=1(type=int), id=9(type=int)>, <Page: words=These are words 9(type=str), parentBookID=1(type=int), id=10(type=int)>](type=list)>
# Look up the book/pages in the database
foundBook = FindOne(Book)
print('foundBook=', foundBook)
>> foundBook= <Book: id=1(type=int), pages=[<Page: words=These are words 0(type=str), parentBookID=1(type=int), id=1(type=int)>, <Page: words=These are words 1(type=str), parentBookID=1(type=int), id=2(type=int)>, <Page: words=These are words 2(type=str), parentBookID=1(type=int), id=3(type=int)>, <Page: words=These are words 3(type=str), parentBookID=1(type=int), id=4(type=int)>, <Page: words=These are words 4(type=str), parentBookID=1(type=int), id=5(type=int)>, <Page: words=These are words 5(type=str), parentBookID=1(type=int), id=6(type=int)>, <Page: words=These are words 6(type=str), parentBookID=1(type=int), id=7(type=int)>, <Page: words=These are words 7(type=str), parentBookID=1(type=int), id=8(type=int)>, <Page: words=These are words 8(type=str), parentBookID=1(type=int), id=9(type=int)>, <Page: words=These are words 9(type=str), parentBookID=1(type=int), id=10(type=int)>](type=list)>
for page in foundBook['pages']:
print('page=', page)
>> page= <Page: words=These are words 0(type=str), parentBookID=1(type=int), id=1(type=int)>
>> page= <Page: words=These are words 1(type=str), parentBookID=1(type=int), id=2(type=int)>
>> page= <Page: words=These are words 2(type=str), parentBookID=1(type=int), id=3(type=int)>
>> page= <Page: words=These are words 3(type=str), parentBookID=1(type=int), id=4(type=int)>
>> page= <Page: words=These are words 4(type=str), parentBookID=1(type=int), id=5(type=int)>
>> page= <Page: words=These are words 5(type=str), parentBookID=1(type=int), id=6(type=int)>
>> page= <Page: words=These are words 6(type=str), parentBookID=1(type=int), id=7(type=int)>
>> page= <Page: words=These are words 7(type=str), parentBookID=1(type=int), id=8(type=int)>
>> page= <Page: words=These are words 8(type=str), parentBookID=1(type=int), id=9(type=int)>
>> page= <Page: words=These are words 9(type=str), parentBookID=1(type=int), id=10(type=int)>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Dictabase-4.0.16.tar.gz.
File metadata
- Download URL: Dictabase-4.0.16.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
374a37bf729e25bab8403c319f17e72f0e086303daa490aeafbb90ec633a8b54
|
|
| MD5 |
693d3618d2e4487e8a80fe535a824173
|
|
| BLAKE2b-256 |
9c6d783024c8e99c2e5ada8771f300850fd24eec6e8230e71325f7cba8e1c8f9
|
File details
Details for the file Dictabase-4.0.16-py3-none-any.whl.
File metadata
- Download URL: Dictabase-4.0.16-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
829947b2c3afa24ea69fdac6511d0b07f31e29732df70339723ee0180bfbadce
|
|
| MD5 |
32c0fb68e00bc7935274c6727147ebdd
|
|
| BLAKE2b-256 |
75f602254149605e4c6dc28419e1bdd97aa790d66fbe1f3efc80f246a9c59de6
|