1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-26 21:14:19 +00:00

Remove royalnet.alchemist, moved to its own repository

https://github.com/Steffo99/alchemist
This commit is contained in:
Steffo 2022-05-02 05:05:47 +02:00
parent 5e74df9134
commit 5d6db1c4c2
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 0 additions and 170 deletions

View file

@ -1,18 +0,0 @@
"""
This module defines multiple objects that simplify some :mod:`sqlalchemy` tasks.
"""
from .func import *
from .make import *
from .repr import *
from .update import *
__all__ = (
"ieq",
"ineq",
"Makeable",
"ColRepr",
"Updatable",
"DoNotUpdateType",
"DoNotUpdate",
)

View file

@ -1,35 +0,0 @@
"""
This submodule implements quick alias for some common SQL filtering functions.
"""
import sqlalchemy
def ieq(one, two):
"""
Create a case-insensitive equality filter to be used in :meth:`sqlalchemy.orm.query.Query.filter`.
Equal to: ::
lower(one) == lower(two)
"""
return sqlalchemy.func.lower(one) == sqlalchemy.func.lower(two)
def ineq(one, two):
"""
Create a case-insensitive inequality filter to be used in :meth:`sqlalchemy.orm.query.Query.filter`.
Equal to: ::
lower(one) != lower(two)
"""
return sqlalchemy.func.lower(one) != sqlalchemy.func.lower(two)
__all__ = (
"ieq",
"ineq",
)

View file

@ -1,55 +0,0 @@
"""
This submodule implements the :class:`.Makeable` mixin.
"""
import sqlalchemy.orm as o
from royalnet.royaltyping import *
T = TypeVar('T')
class Makeable:
"""
A mixin that can be added to a declared class to add the make and unmake methods, that try find an item with
specific properties and either create it if it doesn't exist or delete it if it exists.
"""
@classmethod
def make(cls: Type[T], session: o.session.Session, **kwargs) -> T:
"""
Find the item with the specified name, or create it if it doesn't exist.
:param session: The session to be used in the query and creation.
:param kwargs: Arguments to use in the :meth:`~sqlalchemy.orm.query.Query.filter_by` clause and in the item
constructor.
:return: The retrieved or created item.
"""
# Find the item
item = session.query(cls).filter_by(**kwargs).one_or_none()
# Create the item
if item is None:
item = cls(**kwargs)
session.add(item)
# Return the item
return item
@classmethod
def unmake(cls: Type[T], session: o.session.Session, **kwargs) -> None:
"""
Find the item with the specified name, and delete it if it exists.
:param session: The session to be used in the query and creation.
:param kwargs: Arguments to use in the :meth:`~sqlalchemy.orm.query.Query.filter_by` clause and in the item
constructor.
"""
# Find the item
item = session.query(cls).filter_by(**kwargs).one_or_none()
# Delete the item
if item is not None:
session.delete(item)
__all__ = (
"Makeable",
)

View file

@ -1,20 +0,0 @@
"""
This submodule implements :func:`repr`\\ esentation mixins.
"""
class ColRepr:
"""
A mixin that can be added to a declared class to display all columns of the table with their values in the
``__repr__``.
"""
def __repr__(self):
columns = [c.name for c in self.__class__.__table__.columns]
args = [f"{c}={repr(self.__getattribute__(c))}" for c in columns]
return f"{self.__class__.__qualname__}({', '.join(args)})"
__all__ = (
"ColRepr",
)

View file

@ -1,42 +0,0 @@
"""
This module implements the :class:`.Updatable` mixin, along with the special class :class:`.DoNotUpdateType` and the singleton :data:`.DoNotUpdate`.
"""
class Updatable:
"""
A mixin that can be added to a declared class to add update methods, allowing attributes to be set from
a :class:`dict`.
"""
def update(self, **kwargs):
"""Set attributes from the ``kwargs``, ignoring non-existant key/columns."""
for key, value in kwargs.items():
if value is DoNotUpdate:
continue
if hasattr(self, key):
setattr(self, key, value)
def set(self, **kwargs):
"""Set attributes from the ``kwargs``, without checking for non-existant key/columns."""
for key, value in kwargs.items():
if value is DoNotUpdate:
continue
setattr(self, key, value)
class DoNotUpdateType:
"""
A type, similar to :data:`None`, used to mark fields that should be skipped in update and set operations.
"""
__slots__ = ()
DoNotUpdate = DoNotUpdateType()
"""The constant instance of the DoNotUpdateType."""
__all__ = (
"Updatable",
"DoNotUpdateType",
"DoNotUpdate",
)