Source code for unicum.linkedobject

# -*- coding: utf-8 -*-

# unicum
# ------
# Python library for simple object cache and factory.
# 
# Author:   sonntagsgesicht, based on a fork of Deutsche Postbank [pbrisk]
# Version:  0.3, copyright Wednesday, 18 September 2019
# Website:  https://github.com/sonntagsgesicht/unicum
# License:  Apache License 2.0 (see LICENSE file)


import inspect
import weakref





[docs]class LinkedObject(object): """ links from linked_obj to (obj, attribute) with obj.attribute = linked_obj """ __link = dict() @classmethod def _get_links(cls): mro = inspect.getmro(cls) for m in mro: attr = '_' + m.__name__ + '__link' if hasattr(cls, attr): return getattr(cls, attr) raise TypeError def __repr__(self): return str(self) def __str__(self): return self.__class__.__name__ def __setattr__(self, item, value): if hasattr(self, item): current = getattr(self, item) if isinstance(current, LinkedObject): current.remove_link(self, item) super(LinkedObject, self).__setattr__(item, value) if isinstance(value, LinkedObject) and repr(value): value.register_link(self, item)