Skip to content

Instantly share code, notes, and snippets.

@estasney
estasney / mapper.py
Last active October 28, 2024 14:05
Get ORM Attributes to Column Names
# This is useful for when an orm model has a property that references a column that has a different name
class Model():
__table_name__ = "sometable"
legacy_id = Column("id", Integer)
attrs = Model.__mapper__ # or inspect(Model)
{c.key: c.columns[0].key for c in attrs.column_attrs}
# {'legacy_id': 'id'}
@estasney
estasney / env_override.py
Created October 25, 2024 21:19
Wrapped Env Override Decorator
import inspect
import os
from functools import wraps
def env_override(func):
sig = inspect.signature(func)
# Identify parameters with default value None
none_defaults = [
name for name, param in sig.parameters.items()
if param.default is None