A Thread-Safe, Multi-Session/Multi-Connection Model-Repository-Context base for SQL Alchemy
Project description
alchemist-stack
Package Author: H.D. 'Chip' McCullough IV
Last Updated: April 23rd, 2018
Description:
A Flexible Model-Repository-Database stack for use with SQL Alchemy
Overview
Alchemist Stack is intended to be a thread-safe, multi-session/multi-connection
Usage
Example ORM Table:
# table_example.py
from alchemist_stack.repository.models import Base
from sqlalchemy import Column, Integer, DateTime
class ExampleTable(Base):
__tablename__ = 'example'
primary_key = Column('id', Integer, primary_key=True)
timestamp = Column(DateTime(timezone=True), nullable=False)
def __repr__(self):
return '<Example(timestamp={timestamp})>'.format(timestamp=self.timestamp)
Example Model:
# model_example.py
from tables.table_example import ExampleTable
from datetime import datetime, timezone
from typing import TypeVar
E = TypeVar('E', bound="Example")
class Example(object):
"""
Example Model class.
"""
def __init__(self, timestamp: datetime = datetime.now(timezone.utc).astimezone(), primary_key: int = None,
*args, **kwargs):
self.__pk = primary_key
self.__timestamp = timestamp
self.__args = args
self.__kwargs = kwargs
def __call__(self, *args, **kwargs) -> ExampleTable:
"""
Called when an instance of Example is called, e.g.:
`x = Example(...)`
`x(...)`
This is equivalent to calling `to_orm()` on the object instance.
:returns: The ORM of the Example.
:rtype: ExampleTable
"""
return self.to_orm()
def __repr__(self) -> str:
"""
A detailed String representation of Example.
:returns: String representation of Example object.
"""
return '<class Test(pk={pk} timestamp={timestamp}) at {hex_id}>'.format(pk=self.__pk,
timestamp=self.__timestamp,
hex_id=hex(id(self)))
@property
def id(self) -> int:
return self.__pk
@property
def timestamp(self) -> datetime:
return self.__timestamp
def to_orm(self) -> ExampleTable:
return ExampleTable(primary_key=self.__pk, timestamp=self.__timestamp)
@classmethod
def from_orm(cls, obj: ExampleTable) -> E:
return cls(timestamp=obj.timestamp, primary_key=obj.primary_key)
Example Repository:
# repository_example.py
from alchemist_stack.context import Context
from alchemist_stack.repository import RepositoryBase
from models.model_example import Example
from tables.table_example import ExampleTable
class ExampleRepository(RepositoryBase):
"""
"""
def __init__(self, context: Context, *args, **kwargs):
"""
Test Repository Constructor
:param database: The Database object containing the engine used to connect to the database.
:param args: Additional Arguments
:param kwargs: Additional Keyword Arguments
"""
super().__init__(context=context, *args, **kwargs)
def __repr__(self):
"""
:return:
"""
return '<class ExampleRepository->RepositoryBase(context={context}) at {hex_id}>'\
.format(context=str(self.context),
hex_id=hex(id(self.context)))
def create_example(self, obj: Example):
self._create_object(obj=obj.to_orm())
def get_example_by_id(self, example_id: int) -> Example:
self._create_session()
__query = self._read_object(cls=ExampleTable)
__t = __query.with_session(self.session).get(example_id)
self._close_session()
if isinstance(__t, ExampleTable):
return Example.from_orm(__t)
def update_example_by_id(self, example_id: int, values: dict, synchronize_session: str = 'evaluate') -> int:
self._create_session()
__query = self._update_object(cls=ExampleTable, values=values)
rowcount = __query.with_session(self.session)\
.filter(ExampleTable.primary_key == example_id)\
.update(values=values,
synchronize_session=synchronize_session)
self._commit_session()
return rowcount
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
File details
Details for the file alchemist_stack-0.1.0.dev1.tar.gz
.
File metadata
- Download URL: alchemist_stack-0.1.0.dev1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 339c56e09d10fdcdf319e05c643ace288afbb3832be1977f7ae3041e1a14919e |
|
MD5 | d13ba3bf6718c8889a6ef5f8431db9a3 |
|
BLAKE2b-256 | 248751f54b231fddc935452d872b179b51fd303ef6708cbf61710e31a72f3835 |
File details
Details for the file alchemist_stack-0.1.0.dev1-py3-none-any.whl
.
File metadata
- Download URL: alchemist_stack-0.1.0.dev1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74e4f6158d5f3d13e8cae499d5dc0989371103b039c8b102c204bf307e820951 |
|
MD5 | 50eea3d0a8b236319c6a3785e6ed60b8 |
|
BLAKE2b-256 | 9709369627fa8d586ef2caa60cec4ecc548d9d3942f87c6dad8ac8ed289c2a49 |