Skip to main content

Mostly Invisible Database

A database access layer that acts like python in-memory objects.


Disclaimer: Only minimal attempts have been made at speed. And mainly intended for personal projects. Deeply nested data structures will likely be slow.


Example:

>>> import midb
>>> root = midb.get_root('db_file.db')
>>> root['test'] = 1
>>> root['test2'] = {'test3': 3}
>>> exit()

later ...

>>> import midb
>>> root = midb.get_root('db_file.db')
>>> root['test']
1
>>> root['test2']['test3']
3
>>> root['test2']
PDict({'test3': 3}, _backend=SQLiteBackend(filename="db_file.db"), _id=1, _temp=None)
>>> root['test2'].in_memory()
{'test3': 3}
>>> exit()

What's currently supported?

Currently, the types that are supported are:

  • Simple types:

    • str
    • None
    • bool
    • int
    • float
    • complex
    • datetime.datetime
    • datetime.date
    • datetime.time
  • container types (must not contain unsupported types)

    • dict (silently converted to PDict)
    • tuple (silently convert to PTuple)
    • list (silently convert to PList)
    • custom objects (see below for example)

Custom objects

Custom objects are still not fully tested, but the basics have been including property descriptors. Also, custom objects that are hashable may be used as keys in dictionaries. The following has been tested and works.

import midb

# define or import an class
#   Note: subclassing midb.MemoryObject is not necessary but useful 
#         because it defines __repr__ and __eq__.
class MyObj(midb.MemoryObject):
    def __init__(self, a_value, b_value):
        self.a = a_value
        self.b = b_value

    def some_other_method(self, input):
        self.methods_may_set = input
        return f"{self.a}, {self.b}"

# define a persistent object with an _in_memory_class attribute set to the object you 
# want to have it emulate and decorate it with the @midb.setup_pobject decorator.
@midb.setup_pobject
class PMyObj(midb.PObject):
    _in_memory_class = MyObj

With those objects imported or defined:

# first run
root = midb.get_root("filename.db")

root['my object'] = MyObj(1, 2)
print(root['my object'].a)
print(root['my object'].b)
print(root['my object'].some_other_method(3))
print(root['my object'].methods_may_set)

Later (also with the above classes defined or imported) ...

root = midb.get_root("filename.db")

print(root['my object'].a)
print(root['my object'].b)
print(root['my object'].some_other_method(4))
print(root['my object'].methods_may_set)

If you encounter any problems with the classes you have defined, please file an issue at https://gitlab.com/gossrock/midb/-/issues.


Future:

  • Support for and/or documentation of additional or custom serializers.
  • More examples and documentation
  • Add support for other standard library classes that represent storable data.
    • decimal.Decimal and fractions.Fraction
    • set and frozenset
    • collections.namedtuple()/typing.NamedTuple and dataclasses.dataclass
    • colletions.OrderdDict
    • enum.Enum and friends
    • pathlib.Path
  • Add generic type annotation to collection types.

Bugs

Please report bugs at https://gitlab.com/gossrock/midb/-/issues

Release files for midb 0.5.1

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

Source distribution (sdist)

Source distribution for midb 0.5.1
File Size Uploaded
midb-0.5.1.tar.gz 20.0 kB Details

Built distribution (wheel)

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

Total release size: 51.4 kB

Release files / midb-0.5.1.tar.gz

Download URL midb-0.5.1.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f87705ec9c8b33c88cc67278f56f7cb62971bae5cf71fdcfbc9b26d6d26a28ae
BLAKE2b-256 checksum
How to use checksums
e124954f80de620ff2733ddb4c9e712e6e63c333242fbec6f63777ffa6470a60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

Release files / midb-0.5.1-py3-none-any.whl

Download URL midb-0.5.1-py3-none-any.whl
Size 31.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e17a09d5050f65b86f2c8f792c3ff6c8937fdcba1a78de389cd5ec2180b08602
BLAKE2b-256 checksum
How to use checksums
586fd540f38b7f26f9e96e053d3777678a5279e02da6dd4129d33ab240025de0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

0.0.6

2 release files

0.0.5

1 release file

0.0.1

1 release file

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