[docs]classReprMixin(SuperBase):""" A :term:`mixin` class inherited in front of/instead of object which gives any object a decent __repr__ function. The object can additionally define a list in one of __slots__ or __repr_slots__ which gives attributes for this repr function to report. If __repr_slots__ is given, it will take precedence over any __slots__. For extended inheritance heirerarchies, the attributes are stacked, with superclass attributes listed before subclass attributes. .. attribute:: __repr_slots__ This is a list of the attribute names for the repr to display. One should be placed in any subclass using this mixin that needs specific members displayed. .. automethod:: __repr__ """__slots__=()__repr_slots__=()
[docs]def__repr__(self):attr_names=[]attr_names_set=set()# find the list of attributes to useforparent_classinself.__class__.__mro__:ifhasattr(parent_class,"__repr_slots__"):attr_names.extend((attrnameforattrnameinparent_class.__repr_slots__ifattrnamenotinattr_names_set))attr_names_set.update(parent_class.__repr_slots__)elifhasattr(parent_class,"__slots__"):attr_names.extend((attrnameforattrnameinparent_class.__slots__ifattrnamenotinattr_names_set))attr_names_set.update(parent_class.__slots__)# extract the attributes, allowing missing onesitems=[]forattrnameinattr_names:attr_value=selftry:forsubattrinattrname.split("."):attr_value=getattr(attr_value,subattr)exceptAttributeError:attr_value=":unset:"items.append((attrname,repr(attr_value)))# paste the attributes togetherattr_listing=", ".join(("%s=%s"%attr_descrforattr_descrinitems))return"%s(%s)"%(self.__class__.__name__,attr_listing)
[docs]defbool_string_interpret(val_str,default_value=None):""" Interprets """ifval_str.upper()in("T","TRUE","1","Y","YES","AFFIRMATIVE"):returnTrueelifval_str.upper()in("F","FALSE","0","N","NO","NEGATORY"):returnFalseelifdefault_valueisNone:raiseValueError("Bool Value unrecognized {0}".format(val_str))returndefault_value