Small ORM with limited functions for multiple database engines for pet-projects
Project description
picorm
Picorm is a small ORM with limited functions for multiple database engines for pet-projects, mostly for simple CRUD operations in cases, when you don't want to introduce a solid ORM like SQLAlchemy or whatever, but still want to keep storage-related stuff out of your code.
Getting Started
Installation
To install from pypi:
pip install picorm
Simple usage
from collections import OrderedDict
from picorm import FileStorage as Storage # import one of storage implementations
storage = Storage('path_to_database_file') # create storage object
# every collection that you need to store must inherit from storage's Table class
class Users(storage.Table):
def __init__(self, fields = {}):
defaults = OrderedDict([('key', 'users')]) # specify table name
for k, v in fields.items():
defaults[k] = v
super().__init__(defaults)
storage.create('users', OrderedDict([ # specify table name and desired fields to be stored
('key', storage.types['int']),
('id', storage.types['int']),
('name', storage.types['str']),
('type', storage.types['str']),
]))
# every query on storage.users will return object of this class if not specified otherwise
class User(self.Record):
def __init__(self, fields = {}):
# default values for fields
defaults = OrderedDict([
('key', -1),
('id', -1),
('name', ':null'),
('type', ':null'),
])
for k, v in fields.items():
defaults[k] = v
super().__init__(defaults)
# that's it, you can add your own methods and properties
self.User = User # attach classes to table object
users = Users() # create table object
users.Record = users.User # make every query return object of class User
Storage querying
new_user = users.User({'id': 42, 'name': 'foo'}) # create record, you can do it this way or through storage.users.User
users.add (new_user) # place it in storage
found_user = users.find_one({'name': 'foo'}) # get storage Record - an object of class User
found_user.get('id') # get record's field value
found_user.set({'name': 'bar'}) # change desired fields
storage.disconnect() # don't forget to disconnect from storage if you're planning to switch engines
Return different subclasses of table record
In those cases, when you need queries to return various subclasses of table record, you can simply introduce them after User definition in example:
class Users(storage.Table):
def __init__(self, fields = {}):
# ...
class Player(User): # an example of subclassing table Record
type = 'player'
def __init__(self, fields):
overrides = {'type': Player.type}
merged = {**fields, **overrides}
super().__init__(merged)
# ...
self.Player = Player # attach class to table object
# create a type -> class map to return objects of different subclasses of table record
self.type_class_map = {
Player.type: Player,
User.type: User,
':null': User, # "default" class to wrap record
}
# make every query return object of either Player or User class based on "type" field value
users.Record = lambda fields: users.type_class_map[fields['type']](fields)
Table methods
method | arguments | returns |
---|---|---|
create | self, name, schema, log | None |
find | self, selector | list of Record objects |
find_one | self, selector | Record object or None |
find_max | self, key | Record object or None |
add | self, record | None |
remove | self, record | None |
Record operations
method | arguments | returns |
---|---|---|
set | self, fields | None |
get | self, key | Record's field value |
Supported engines
- File storage
- SQLite storage
Development
Environment setup
- Install Python 3.9+
- Install
virtualenv
pip install virtualenv
- Clone this project
- From project directory, run
virtualenv .env
Note: This will create a virtual environment using the Python version thatvirtualenv
was run with (which will be the version it was installed with). To use a specific Python version, run:virtualenv --python=<path_to_other_python_version> .env # For example, this might look like virtualenv --python=/usr/bin/python3.6 .env
- Assuming you are using the
bash
shell, run:source .env/bin/activate
For other shells, see the otheractivate.*
scripts in the.env/bin/
directory. If you are on Windows, run:.env\Scripts\activate.bat
- Install all of the required packages using
pip install -r requirements.txt
Testing
This project uses tox -> pytest. To trigger tests either run tox
or pytest
optionally with -vv and -s flags for verbosity and prints.
Packaging module
Run the following command to package picorm module:
python -m pip install --upgrade build
python -m build
Generated archive and .whl package will be placed in dist directory.
Contributions
PR are always welcome!
Project details
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
File details
Details for the file picorm-0.1.6.tar.gz
.
File metadata
- Download URL: picorm-0.1.6.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01b53cb7a57028adbef530d2b21c3ca08a2addf59873b0650de9a1a4e5f33b71 |
|
MD5 | 0292b2791d2c6be52b11ce96b7eddf8d |
|
BLAKE2b-256 | 1bc44d88a1283952bbb21aa30a926be582c52deaa8b8adfecb7104fc08342624 |
File details
Details for the file picorm-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: picorm-0.1.6-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3fc401bbecb2b5704b929020cf1c94f899f0cfd86e676d433981edf622a7680 |
|
MD5 | 11290704f0ffb47988ab4eeeaef512e6 |
|
BLAKE2b-256 | 13c758673ba7e23b3d978c5a85acd45be805efcb740c7cb2ab3718ce70861d31 |