def filterdictvals(dv)""dict with entries for value removed filterdictvals(dict( = = = ) ={' ' ""return {kv for (kv in items(if !vdef invertdict( )""dict with values changed to keys (grouped by valuesvalues must all be hashable to work as dict/set keys invertdict(dict( = = = )={ [' '' '] [' ']""def keysof( )return sorted( for in keys(if [ =vreturn {vkeysof(vfor in set( values())def dflr(cls)""classic depth-first left-to-right order of class tree at cls cycles not possiblepython disallows on __bases__ changes ""here [clsfor sup in cls __bases__here +dflr(supreturn here def inheritance(instance)""inheritance order sequencenew-style (mroor classic (dflr""if hasattr(instance __class__'__mro__')return (instance,instance __class__ __mro__ elsereturn [instancedflr(instance __class__def mapattrs(instancewithobject=falsebysource=false)""dict with keys giving all inherited attributes of instancewith values giving the object that each is inherited from withobjectfalse=remove object built-in class attributes bysourcetrue=group result by objects instead of attributes supports classes with slots that preclude __dict__ in instances ""attr obj {inherits inheritance(instancefor attr in dir(instance)for obj in inheritsif hasattr(obj'__dict__'and attr in obj __dict__attr obj[attrobj break see slots if not withobjectnew-style class changes