Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alchemist_stack-0.1.0.dev1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alchemist_stack-0.1.0.dev1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file alchemist_stack-0.1.0.dev1.tar.gz.

File metadata

File hashes

Hashes for alchemist_stack-0.1.0.dev1.tar.gz
Algorithm Hash digest
SHA256 339c56e09d10fdcdf319e05c643ace288afbb3832be1977f7ae3041e1a14919e
MD5 d13ba3bf6718c8889a6ef5f8431db9a3
BLAKE2b-256 248751f54b231fddc935452d872b179b51fd303ef6708cbf61710e31a72f3835

See more details on using hashes here.

File details

Details for the file alchemist_stack-0.1.0.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for alchemist_stack-0.1.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 74e4f6158d5f3d13e8cae499d5dc0989371103b039c8b102c204bf307e820951
MD5 50eea3d0a8b236319c6a3785e6ed60b8
BLAKE2b-256 9709369627fa8d586ef2caa60cec4ecc548d9d3942f87c6dad8ac8ed289c2a49

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0.dev1 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page