Skip to main content

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)>

Metadata

Release files for Dictabase 4.0.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Dictabase 4.0.16
File Size Uploaded
Dictabase-4.0.16.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Dictabase 4.0.16
File Interpreter ABI Platform
Dictabase-4.0.16-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / Dictabase-4.0.16.tar.gz

Download URL Dictabase-4.0.16.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
374a37bf729e25bab8403c319f17e72f0e086303daa490aeafbb90ec633a8b54
BLAKE2b-256 checksum
How to use checksums
9c6d783024c8e99c2e5ada8771f300850fd24eec6e8230e71325f7cba8e1c8f9
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / Dictabase-4.0.16-py3-none-any.whl

Download URL Dictabase-4.0.16-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
829947b2c3afa24ea69fdac6511d0b07f31e29732df70339723ee0180bfbadce
BLAKE2b-256 checksum
How to use checksums
75f602254149605e4c6dc28419e1bdd97aa790d66fbe1f3efc80f246a9c59de6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

4.0.16 This release

2 release files

4.0.15

2 release files

4.0.14

2 release files

4.0.13

2 release files

4.0.12

2 release files

4.0.11

2 release files

4.0.10

2 release files

4.0.9

2 release files

4.0.8

2 release files

4.0.7

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.6

2 release files

3.0.5

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page