Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python
Functools: Collections of useful functions/functors
"""
#===============================================================================
""" Return a hash string based on curent timestamp. """
""" To be used inside the class to provide complete set of get/set/del in one go.
Usage:
>>> class Foo: ... xbin = prop(10 , "Number of bins on x-axis") >>> >>> foo = Foo() >>> foo.xbin 10 >>> foo.xbin = 20 >>> foo.xbin 20
"""
#===============================================================================
## Metaclass: Unsettable
""" Like `prop` above, but allow the value to be set only once. The second set will raise AttributeError. Default value of every attribute is None. """ """ Guard here against already-set value """
## instance method to be bound """ Reset all its attribute values to default. """
#-------------------------------------------------------------------------------
## instance method to be bound
"""Exclude these fields.""" # if key in ('stats', 'wrapper', 'anchors', 'debug_messages', 'xarr', 'yarr', 'zarr'): # Proxy # return True
"""Return True if this key-val should be listed.""" return False
## skip some entries by its key/val
## Obtain the value, skip unnecessary ones. # val = getattr(self, key, None)
## Start printing, prep default value & current value # defval = str(getattr(default_instance, key, None))
## Pretty array print >>fullmsg, msg(key, defval, utils.pretty_array(val)) continue
## Pretty paragraph string
## Break into several lines instead if it's a long list for i,subval in enumerate(val): if i==0: line = msg(key, defval, str(subval)) else: # Omit the key+defval line = msg('', None, str(subval)) print >>fullmsg, line continue
## Default printing
## Finally, line break & return
#-------------------------------------------------------------------------------
""" Helper method to generate static class (inspired by collections.namedtuple), to be used exclusively in conjunction with staticprop.
By static, it means:
- All attribute is declared when class is created. - No attribute can be added furthermore, either at class-level or instance-level. - Attribute, once assigned, cannot be changed. - Unassigned attribute has 'None' as default value.
Need this wrapper in order to properly generate ``__slots__`` with keys populated from staticprop
Usage::
>>> ## Define the class first >>> Cls = static_struct('MyClass', attr1='doc', attr2='doc2') >>> Cls <class 'qhist.functools.MyClass'>
## Dynamic properties are populated and inspectable. >>> [ x for x in dir(Cls) if not x.startswith('_') ] ['attr1', 'attr2', 'clear', 'keys', 'reset']
## Create class instance >>> obj = Cls()
## Default value is None >>> obj.attr1 == None True
## Set the value. Set again will fail >>> obj.attr1 = 42 # set the value >>> obj.attr1 42 >>> obj.attr1 = 555 Traceback (most recent call last): ... AttributeError: Attribute "attr1" already set to 42
## Setting 'None' respect the no-repeat rule above >>> obj_none = Cls() >>> obj_none.attr1 = None >>> obj_none.attr1 = None # again Traceback (most recent call last): ... AttributeError: Attribute "attr1" already set to None
## Get/Set will fail if value is not defined. >>> obj.unknown Traceback (most recent call last): ... AttributeError: 'MyClass' object has no attribute 'unknown' >>> obj.unknown = 42 Traceback (most recent call last): ... AttributeError: 'MyClass' object has no attribute 'unknown'
## Cannot set on the class directly, for value safety >>> Cls.attr1 = 'some value' Traceback (most recent call last): ... AttributeError: ... >>> Cls.attr1 <property object at ...>
## Check independence between instance. >>> obj2 = Cls() >>> obj.attr2 = 111 >>> obj2.attr2 = 222 >>> obj.attr2 111 >>> obj2.attr2 222
## This is like a dictionary >>> obj.keys() ['attr1', 'attr2']
## Use clear/reset to clear the values back to unset # This is independent for different instance >>> obj.clear() >>> obj.attr1 == obj.attr2 == None True >>> obj2.attr2 222
## Attributes can be pretty-print as table >>> print(str(obj2)) -------------------... KEY | VALUE ( * differs from default ) attr1 | None attr2 | * 222 -------------------------------------------------------...
""" ## Instantiate the props first
## Fixed the attributes of the class. # Setter-on-class is disable from this point.
#=============================================================================== |