supcon.keyed module
lass KeyedMethod(): def __init__(self, key, fget): self.__key = key self.__fget = fget self.__doc__ = fget.__doc__ @property def key(self): return self.__key @property def fget(self): return self.__fget def __get__(self, obj, objType=None): if obj is None: return self if self.__fget is None: raise AttributeError("unreadable attribute") return lambda *args, **kwargs: self.__fget(obj, *args, **kwargs) ef lKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict does not contain the KeyedMethods of any super class. """ if aClass is object: return {} lAttrs = vars(aClass) if not '_lKeyedMethods' in lAttrs: methods = {} for name, attr in lAttrs.items(): if isinstance(attr, KeyedMethod): methods[name] = attr aClass._lKeyedMethods = methods return aClass._lKeyedMethods ef mKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order. """ if not '_mKeyedMethods' in vars(aClass): methods = {} for sClass in reversed(aClass.mro()): methods.update(lKeyedMethods(sClass)) aClass._mKeyedMethods = methods return aClass._mKeyedMethods ef iKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by key. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order. """ if not '_iKeyedMethods' in vars(aClass): methods = {} for _name, attr in mKeyedMethods(aClass).items(): if attr.key in methods: raise ValueError() methods[attr.key] = attr aClass._iKeyedMethods = methods return aClass._iKeyedMethods ef keyedMethod(key): """ Can be used to decorate a method with a key """ return lambda fget: KeyedMethod(key, fget) ef callKeyedMethod(obj, key, *args, **kwargs): """ Resolves a keyed method by the given key and and calls it with the given arguments. """ return iKeyedMethods(type(obj))[key].__get__(obj)(*args, **kwargs)
Functions
def callKeyedMethod(
obj, key, *args, **kwargs)
Resolves a keyed method by the given key and and calls it with the given arguments.
def callKeyedMethod(obj, key, *args, **kwargs): """ Resolves a keyed method by the given key and and calls it with the given arguments. """ return iKeyedMethods(type(obj))[key].__get__(obj)(*args, **kwargs)
def iKeyedMethods(
aClass)
Returns a dict of all KeyedMethods of the given class indexed by key. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order.
def iKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by key. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order. """ if not '_iKeyedMethods' in vars(aClass): methods = {} for _name, attr in mKeyedMethods(aClass).items(): if attr.key in methods: raise ValueError() methods[attr.key] = attr aClass._iKeyedMethods = methods return aClass._iKeyedMethods
def keyedMethod(
key)
Can be used to decorate a method with a key
def keyedMethod(key): """ Can be used to decorate a method with a key """ return lambda fget: KeyedMethod(key, fget)
def lKeyedMethods(
aClass)
Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict does not contain the KeyedMethods of any super class.
def lKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict does not contain the KeyedMethods of any super class. """ if aClass is object: return {} lAttrs = vars(aClass) if not '_lKeyedMethods' in lAttrs: methods = {} for name, attr in lAttrs.items(): if isinstance(attr, KeyedMethod): methods[name] = attr aClass._lKeyedMethods = methods return aClass._lKeyedMethods
def mKeyedMethods(
aClass)
Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order.
def mKeyedMethods(aClass): """ Returns a dict of all KeyedMethods of the given class indexed by attribute name. The dict contains the KeyedMethods of all super classes that are not hidden by the method resolution order. """ if not '_mKeyedMethods' in vars(aClass): methods = {} for sClass in reversed(aClass.mro()): methods.update(lKeyedMethods(sClass)) aClass._mKeyedMethods = methods return aClass._mKeyedMethods
Classes
class KeyedMethod
class KeyedMethod(): def __init__(self, key, fget): self.__key = key self.__fget = fget self.__doc__ = fget.__doc__ @property def key(self): return self.__key @property def fget(self): return self.__fget def __get__(self, obj, objType=None): if obj is None: return self if self.__fget is None: raise AttributeError("unreadable attribute") return lambda *args, **kwargs: self.__fget(obj, *args, **kwargs)
Ancestors (in MRO)
- KeyedMethod
- builtins.object
Instance variables
var fget
var key
Methods
def __init__(
self, key, fget)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, key, fget): self.__key = key self.__fget = fget self.__doc__ = fget.__doc__