Proper autogenerate of __str__() implementation also for sqlalchemy classes?

后端 未结 2 1235
再見小時候
再見小時候 2020-12-21 10:22

I would like to display / print my sqlalchemy classes nice and clean.

In Is there a way to auto generate a __str__() implementation in python? the answer You can it

2条回答
  •  醉梦人生
    2020-12-21 10:36

    This is what I use:

    def todict(obj):
        """ Return the object's dict excluding private attributes, 
        sqlalchemy state and relationship attributes.
        """
        excl = ('_sa_adapter', '_sa_instance_state')
        return {k: v for k, v in vars(obj).items() if not k.startswith('_') and
                not any(hasattr(v, a) for a in excl)}
    
    class Base:
    
        def __repr__(self):
            params = ', '.join(f'{k}={v}' for k, v in todict(self).items())
            return f"{self.__class__.__name__}({params})"
    
    Base = declarative_base(cls=Base)
    

    Any models that inherit from Base will have the default __repr__() method defined and if I need to do something different I can just override the method on that particular class.

    It excludes the value of any private attributes denoted with a leading underscore, the SQLAlchemy instance state object, and any relationship attributes from the string. I exclude the relationship attributes as I most often don't want the repr to cause a relationship to lazy load, and where the relationship is bi-directional, including relationship attribs can cause infinite recursion.

    The result looks like: ClassName(attr=val, ...).

    --EDIT--

    The todict() func that I mention above is a helper that I often call upon to construct a dict out of a SQLA object, mostly for serialisation. I was lazily using it in this context but it isn't very efficient as it's constructing a dict (in todict()) to construct a dict (in __repr__()). I've since modified the pattern to call upon a generator:

    def keyvalgen(obj):
        """ Generate attr name/val pairs, filtering out SQLA attrs."""
        excl = ('_sa_adapter', '_sa_instance_state')
        for k, v in vars(obj).items():
            if not k.startswith('_') and not any(hasattr(v, a) for a in excl):
                yield k, v
    

    Then the base Base looks like this:

    class Base:
    
        def __repr__(self):
            params = ', '.join(f'{k}={v}' for k, v in keyvalgen(self))
            return f"{self.__class__.__name__}({params})"
    

    The todict() func leverages off of the keyvalgen() generator as well but isn't needed to construct the repr anymore.

提交回复
热议问题