Bases and Mixins to store functions for later execution
Project description
SQLAlchemy-Function defines a SQLALchemy Mixin for creating Function models.
A Function model has a parent (optional), a function, arguments, and keyword arguments. When called, the Function model executes its function, passing in its parent (if applicable), its arguments, and its keyword arguments.
Installation
$ pip install sqlalchemy-function
Quickstart
In the setup, we create a SQLAlchemy session, define a Parent model
subclassing FunctionRelator
, and a Function model subclassing
FunctionMixin
.
from sqlalchemy_function import FunctionMixin, FunctionRelator
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
# standard session creation
engine = create_engine('sqlite:///:memory:')
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
session = Session()
Base = declarative_base()
# define a Function model parent
class Parent(FunctionRelator, Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
# Fuction models must reference their parent with a `parent` attribute
functions = relationship('Function', backref='parent')
# define a Function model with the FunctionMixin
class Function(FunctionMixin, Base):
__tablename__ = 'function'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
Base.metadata.create_all(engine)
We can now register, store, and call functions as follows.
@Function.register
def foo(parent, *args, **kwargs):
print('My parent is', parent)
print('My args are', args)
print('My kwargs are', kwargs)
return 'return value'
parent = Parent()
Function.foo(parent, 'hello world', goodbye='moon')
parent.functions[0]()
Out:
My parent is <__main__.Parent object at 0x7f8b4d200518>
My args are ('hello world',)
My kwargs are {'goodbye': 'moon'}
'return value'
Citation
@software{bowen2020sqlalchemy-function,
author = {Dillon Bowen},
title = {SQLAlchemy-Function},
url = {https://dsbowen.github.io/sqlalchemy-function/},
date = {2020-06-05},
}
License
Users must cite this package in any publications which use it.
It is licensed with the MIT License.
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
Hashes for sqlalchemy-function-0.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5eac94a7f17b802a9690a72afe5615389190cf8c73fbcd5b945cd35f398dd023 |
|
MD5 | 891372d9a84cffbe2b4d14e3449f2345 |
|
BLAKE2b-256 | 0a7c821326723054dbb33762e6e5adbbe0ece7c4f461976e791b4415aadb2e72 |
Hashes for sqlalchemy_function-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c71166ac8f6aabcfd53fe495548e93e46299e94ef4f75bc58301f39c625b5cf4 |
|
MD5 | 7f26b24cd3c6e8cb79d3b491f7246323 |
|
BLAKE2b-256 | d3422a36286dc0e06ae95c1b39b78074ba335fc173a2dc45e5e903589c89d96b |