Repository URL to install this package:
|
Version:
1.0.1 ▾
|
"""Utility functions for the accountant"""
import json
from typing import Any, Optional
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.types import VARCHAR, TypeDecorator
class Base(DeclarativeBase):
pass
# pylint: disable=abstract-method
class Json(TypeDecorator):
"""Represents an immutable structure as a json-encoded string.
From https://docs.sqlalchemy.org/en/13/core/
custom_types.html#marshal-json-strings
Usage::
JSONEncodedDict(255)"""
impl = VARCHAR
def process_bind_param(self, value: Any, dialect: Any) -> Any:
if value is not None:
value = json.dumps(value)
return value
def process_result_value(self, value: Any, dialect: Any) -> Any:
if value is not None:
value = json.loads(value)
return value
def compare(list1: Optional[list], list2: Optional[list]) -> bool:
"""compare if 2 unhashable and unsortable lists have the same elements"""
if list1 == list2:
return True
if list1 is None or list2 is None:
return False
list2 = list(list2) # make a mutable copy
try:
for elem in list1:
list2.remove(elem)
except ValueError:
return False
return not list2