Kemampo
Small, Lightweight Library to cut down SQLAlchemy Controllers Boilerplates
Usage
Concept
- First define all of your tables, and all things necessary
- instead of implementing custom Controllers that handle errors and what not. you can use Kemampo to cut downs all of the Boilerplates. so all you need to do next is: implement sqlalchemy's
sessionmakerand pass it into Kemampo intializer - use Kemampo to generate all the basic CRUD controllers.
- Use it as is. or add it to your custom Controller class using
setattr
Example
Define your table
DB_ENGINE = create_engine(
"sqlite:///",
pool_pre_ping=True,
echo=False,
)
DB_BASE = declarative_base(bind=DB_ENGINE)
metadata = DB_BASE.metadata
class User(DB_BASE):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username = Column(String(16), nullable=False, unique=True)
password = Column(String(128), nullable=False)
Use Kemampo
from kemampo import Kemampo
kemampo = Kemampo(sessionmaker(bind=DB_ENGINE))
UserController = kemampo.generate_controller(User)
user_data = {"username": "test_new_user", "password": "a_password_hash"}
update_user_data = {"username": "updated_username"}
status, user = UserController.add(**new_user_data) # create new user
status, users = UserController.get(id=1) # get by id
status, user = UserController.update_by_id(1, **update_user_data) # update by id
status, users = UserController.get_all() # get all
status, user = UserController.delete(1) # delete by id
Installation
You can install via PyPI
python3 -m pip install kemampo
API Documentations
kemampo.Kemampo(session_maker, log_callback)
- Constructor
sessionmaker- Expected Type:
sqlalchemy.orm.session.sessionmaker - Your own sessionmaker. docs: https://docs.sqlalchemy.org/en/13/orm/session_api.html#session-and-sessionmaker
- Expected Type:
log_callback- Expected Type:
Callbacksorfunction - put your function here to get kemampo logging output
- Expected Type:
- Methods
Kemampo.generate_controller(target_model)- The Main feature of kemampo, create a CRUD controller for
target_model target_model- Expected Type: your table specifically
sqlalchemy.ext.declarative.DeclarativeMetaor it's subclass - returns
<table-name>Controller
- Expected Type: your table specifically
- The Main feature of kemampo, create a CRUD controller for
<table-name>Controller
The core feature of Kemampo, provide you with basic CRUD Controller Operations
- Methods
add(**table_dict_model)- Add new row data to database
table_dict_model- type:
Dict - scheme:
{table_column_name: column_name_value, ...}
- type:
- returns Tuple of:
- (
True,added_dict_of_model) or - (
False,ReturnStatus)
- (
get(**target_row_column_key_value)- Get rows data from database, based on column name you give with certain value. support multiple column name and value
target_row_column_key_value- type:
Dict - scheme:
{table_column_name: column_name_value, ...}
- type:
- returns Tuple of:
- (
True,List[dict_of_model]) or - (
False,ReturnStatus)
- (
get_all()- Gather all row values from database
- returns Tuple of:
- (
True,List[dict_of_model]) or - (
False,ReturnStatus)
- (
update_by_id(target_id, **updated_row_column_key_value)target_id- type:
int
- type:
updated_row_column_key_value- type:
Dict - scheme:
{table_column_name: column_name_value, ...}
- type:
- returns Tuple of:
- (
True,dict_of_model) or - (
False,ReturnStatus)
- (
delete(**target_row_column_key_value)target_row_column_key_value- type:
Dict - scheme:
{table_column_name: column_name_value, ...}
- type:
- returns Tuple of:
- (
True,dict_of_model) or - (
False,ReturnStatus)
- (
ReturnStatus
an Enumeration of possible Errors happening inside Kemampo
ReturnStatus.DatabaseError- Error happened internally
- Values:
str->"Database Error"
ReturnStatus.NotFound- Targeted row data was not found inside database
- Values:
str->"Row Data Not Found"
Metadata
Release files for kemampo 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kemampo-0.0.1.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kemampo-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.8 kB
Release files / kemampo-0.0.1.tar.gz
| Download URL | kemampo-0.0.1.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c190bfb0164ecd8000d419a04c7280771acbd47662afe2a9ab9a0fe47f99c71c
|
|
BLAKE2b-256 checksum How to use checksums |
4679b1a929bb2f8304273891f3a778f2bf46278c0c732b10670660fdc999f78a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
|
Release files / kemampo-0.0.1-py3-none-any.whl
| Download URL | kemampo-0.0.1-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e80b0dceb5518310d1ef4bd2362944f806c36acafbbb61f7f46c879b60815df8
|
|
BLAKE2b-256 checksum How to use checksums |
a571a9e88b591102ac84219d958446823a9ec2ccdd155891d6df9cbe7c25f738
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
|