fandango.objects module

Description

fandango.objects contains method for loading python modules and objects “on the run”, as well as several advanced types used within the fandango library

It includes 2 wonderful classes: Object (by Alejandro Homs) and Singleton (by Marc Santiago)

Other classes are borrowed from taurus.core.utils (by Tiago Coutinho)

Classes

Object

class fandango.objects.Object[source]

This class solves some problems when an object inherits from multiple classes and some of them inherit from the same ‘grandparent’ class

call_all__init__(klass, *_args, **_kw)[source]

Call __init__ recursively, for multiple dynamic inheritance. @author srubio@cells.es

This method should be called only if all arguments are keywords!!! Multiple __init__ calls with unnamed arguments is hard to manage:

All the _args values will be assigned to non-keyword args
e.g:

from objects import Object

class A(Object):
def __init__(self,a=2):
print ‘A.__init__’,a
class B(A):
def __init__(self,b):
print ‘B.__init__’,b
class C(B,A):
def __init__(self,c):
print ‘C.__init__’,c
class D(C,B):
def __init__(self,d=1,*args,**kwargs):
self.call_all__init__(D,*args,**kwargs) print ‘D.__init__’,d

D(a=1,b=2,c=3,d=4)

Decorator

class fandango.objects.Decorator(f)[source]

This generic class allows to differentiate decorators from common classes. Inherit from it and use issubclass(klass,Decorator) to know if a class is a decorator To add arguments to decorator reimplement __init__ To modify your wrapper reimplement __call__

A decorator __init__ with a single argument can be called like:

@D def f(x):

pass

If you need a Decorator with arguments then __init__ will manage the arguments and __call__ will take the function and return a wrapper instead.

@D(x,y) def f(z):

pass

ClassDecorator

class fandango.objects.ClassDecorator(f)[source]

Singleton

class fandango.objects.Singleton[source]

This class allows Singleton objects overriding __new__ and renaming __init__ to init_single The __new__ method is overriden to force Singleton behaviour, the Singleton is created for the lowest subClass. @warning although __new__ is overriden __init__ is still being called for each instance=Singleton(), this is way we replace it by __dub_init

BoundDecorator

class fandango.objects.BoundDecorator(f)[source]

Decorates class methods keeping the bound status of its members

Inspired in https://wiki.python.org/moin/PythonDecoratorLibrary#Class_method_decorator_using_instance
Class method decorator specific to the instance. It uses a descriptor to delay the definition of the method wrapper.

To use it, just inherit from it and rewrite the wrapper method

Example:

from fandango.objects import BoundDecorator BoundDecorator().tracer = 1

class X(object):
def __init__(self,name): self.name = name def f(self,*args): return (self.name,args)
class D(BoundDecorator):

@staticmethod def wrapper(instance,f,*args,**kwargs):

print(‘guess what?’) v = f(instance,*args,**kwargs) return v[0]

x = X(‘a’) X.f = D()(X.f) x.f()

SingletonMap

class fandango.objects.SingletonMap[source]

This class allows distinct Singleton objects for each args combination. The __new__ method is overriden to force Singleton behaviour, the Singleton is created for the lowest subClass. @warning although __new__ is overriden __init__ is still being called for each instance=Singleton(), this is way we replace it by __dub_init

nullDecorator

class fandango.objects.nullDecorator(*args)[source]

Empty decorator with null arguments, used to replace pyqtSignal,pyqtSlot

Struct

class fandango.objects.Struct(*args, **kwargs)[source]

Metamorphic class to pass/retrieve data objects as object or dictionary

s = Struct(name=’obj1’,value=3.0) s.setCastMethod(lambda k,v: str2type) s.cast(‘3.0’) : 3.0 s.keys() : [‘name’, ‘value’] s.to_str() : “fandango.Struct({‘name’: obj1,’value’: 3.0,})” s.dict() : {‘name’: ‘obj1’, ‘value’: 3.0}

cast(key=None, value=None, method=None)[source]

The cast() method is used to convert an struct to a pickable/json object. Use set_cast_method(f) to override this call. The cast method must accept both key and value keyword arguments.

cast_items(items=[], update=True)[source]

The cast() method is used to convert an struct to a pickable/json object.

default_cast(key=None, value=None)[source]

This method checks if key is already defined. If it is, it will return value as an evaluable string. If it is not, then it will do same action on the passed value.

get_key(value)[source]

Reverse lookup

to_str(order=None, sep=', ')[source]

This method provides a formatable string for sorting

Decorated

class fandango.objects.Decorated[source]

Functions

self_locked

fandango.objects.self_locked(func, reentrant=True)[source]

Decorator to make thread-safe class members @deprecated @note see in tau.core.utils.containers Decorator to create thread-safe objects. reentrant: CRITICAL:

With Lock() this decorator should not be used to decorate nested functions; it will cause Deadlock! With RLock this problem is avoided ... but you should rely more on python threading.

_fset

fandango.objects._fset(self, value, var)[source]

findModule

fandango.objects.findModule(module)[source]

locked

fandango.objects.locked(f, *args, **kwargs)[source]

decorator for secure-locked functions A key-argument _lock can be used to use a custom Lock object

dirModule

fandango.objects.dirModule(module)[source]

NamedProperty

fandango.objects.NamedProperty(name, fget=None, fset=None, fdel=None)[source]

This Class is in Beta, not fully implemented yet

It makes easier to declare name independent property’s (descriptors) by using template methods like:

def fget(self,var): # var is the identifier of the variable
return getattr(self,var)
def fset(self,value,var): # var is the identifier of the variable
setattr(self,var,value)
def fdel(self,var): # var is the identifier of the variable
delattr(self,var)

MyObject.X = Property(fget,fset,fdel,’X’)

decorator_with_args

fandango.objects.decorator_with_args(decorator)[source]

Decorator with Arguments must be used with parenthesis: @decorated() , even when arguments are not used!!!

This method gets an d(f,args,kwargs) decorator and returns a new single-argument decorator that embeds the new call inside.

But, this decorator disturbed stdout!!!!

There are some issues when calling nested decorators; it is clearly better to use Decorator classes instead.

loadModule

fandango.objects.loadModule(source, modulename=None)[source]

obj2dict

fandango.objects.obj2dict(obj, type_check=True, class_check=False, fltr=None)[source]

Converts a python object to a dictionary with all its members as python primitives

Parameters:fltr – a callable(name):bool method

_fdel

fandango.objects._fdel(self, var)[source]

copy

fandango.objects.copy(obj)[source]

This method will return a copy for a python primitive object. It will not work for class objects unless they implement the __init__(other) constructor

dirClasses

fandango.objects.dirClasses(module, owned=False)[source]

make_property

fandango.objects.make_property(var, fget=<function _fget at 0x7eff5cee0938>, fset=<function _fset at 0x7eff5cee2500>, fdel=<function _fdel at 0x7eff5cee2578>)[source]

This Class is in Beta, not fully implemented yet

_fget

fandango.objects._fget(self, var)[source]

NewClass

fandango.objects.NewClass(classname, classparent=None, classdict=None)[source]
Creates a new class on demand:
ReleaseNumber = NewClass(‘ReleaseNumber’,tuple,{‘__repr__’:(lambda self:’.’.join((‘%02d’%i for i in self)))})

raw autodoc

class fandango.objects.BoundDecorator(f)[source]

Bases: fandango.objects.Decorator

Decorates class methods keeping the bound status of its members

Inspired in https://wiki.python.org/moin/PythonDecoratorLibrary#Class_method_decorator_using_instance
Class method decorator specific to the instance. It uses a descriptor to delay the definition of the method wrapper.

To use it, just inherit from it and rewrite the wrapper method

Example:

from fandango.objects import BoundDecorator BoundDecorator().tracer = 1

class X(object):
def __init__(self,name): self.name = name def f(self,*args): return (self.name,args)
class D(BoundDecorator):

@staticmethod def wrapper(instance,f,*args,**kwargs):

print(‘guess what?’) v = f(instance,*args,**kwargs) return v[0]

x = X(‘a’) X.f = D()(X.f) x.f()

tracer
static wrapper(instance, f, *args, **kwargs)[source]
class fandango.objects.ClassDecorator(f)[source]

Bases: fandango.objects.Decorator

class fandango.objects.Decorated[source]

Bases: object

class fandango.objects.Decorator(f)[source]

Bases: object

This generic class allows to differentiate decorators from common classes. Inherit from it and use issubclass(klass,Decorator) to know if a class is a decorator To add arguments to decorator reimplement __init__ To modify your wrapper reimplement __call__

A decorator __init__ with a single argument can be called like:

@D def f(x):

pass

If you need a Decorator with arguments then __init__ will manage the arguments and __call__ will take the function and return a wrapper instead.

@D(x,y) def f(z):

pass
fandango.objects.NamedProperty(name, fget=None, fset=None, fdel=None)[source]

This Class is in Beta, not fully implemented yet

It makes easier to declare name independent property’s (descriptors) by using template methods like:

def fget(self,var): # var is the identifier of the variable
return getattr(self,var)
def fset(self,value,var): # var is the identifier of the variable
setattr(self,var,value)
def fdel(self,var): # var is the identifier of the variable
delattr(self,var)

MyObject.X = Property(fget,fset,fdel,’X’)

fandango.objects.NewClass(classname, classparent=None, classdict=None)[source]
Creates a new class on demand:
ReleaseNumber = NewClass(‘ReleaseNumber’,tuple,{‘__repr__’:(lambda self:’.’.join((‘%02d’%i for i in self)))})
class fandango.objects.Object[source]

Bases: object

This class solves some problems when an object inherits from multiple classes and some of them inherit from the same ‘grandparent’ class

call__init__(klass, *args, **kw)[source]
call_all__init__(klass, *_args, **_kw)[source]

Call __init__ recursively, for multiple dynamic inheritance. @author srubio@cells.es

This method should be called only if all arguments are keywords!!! Multiple __init__ calls with unnamed arguments is hard to manage:

All the _args values will be assigned to non-keyword args
e.g:

from objects import Object

class A(Object):
def __init__(self,a=2):
print ‘A.__init__’,a
class B(A):
def __init__(self,b):
print ‘B.__init__’,b
class C(B,A):
def __init__(self,c):
print ‘C.__init__’,c
class D(C,B):
def __init__(self,d=1,*args,**kwargs):
self.call_all__init__(D,*args,**kwargs) print ‘D.__init__’,d

D(a=1,b=2,c=3,d=4)

getAttrDict()[source]
updateAttrDict(other)[source]
class fandango.objects.Singleton[source]

Bases: object

This class allows Singleton objects overriding __new__ and renaming __init__ to init_single The __new__ method is overriden to force Singleton behaviour, the Singleton is created for the lowest subClass. @warning although __new__ is overriden __init__ is still being called for each instance=Singleton(), this is way we replace it by __dub_init

classmethod clear_singleton()[source]
classmethod get_singleton(*p, **k)[source]
class fandango.objects.SingletonMap[source]

Bases: object

This class allows distinct Singleton objects for each args combination. The __new__ method is overriden to force Singleton behaviour, the Singleton is created for the lowest subClass. @warning although __new__ is overriden __init__ is still being called for each instance=Singleton(), this is way we replace it by __dub_init

classmethod clear_singleton(*p, **k)[source]
classmethod clear_singletons()[source]
get_instance_key()[source]
classmethod get_singleton(*p, **k)[source]
classmethod get_singletons()[source]
classmethod parse_instance_key(*p, **k)[source]
class fandango.objects.Struct(*args, **kwargs)[source]

Bases: object

Metamorphic class to pass/retrieve data objects as object or dictionary

s = Struct(name=’obj1’,value=3.0) s.setCastMethod(lambda k,v: str2type) s.cast(‘3.0’) : 3.0 s.keys() : [‘name’, ‘value’] s.to_str() : “fandango.Struct({‘name’: obj1,’value’: 3.0,})” s.dict() : {‘name’: ‘obj1’, ‘value’: 3.0}

cast(key=None, value=None, method=None)[source]

The cast() method is used to convert an struct to a pickable/json object. Use set_cast_method(f) to override this call. The cast method must accept both key and value keyword arguments.

cast_items(items=[], update=True)[source]

The cast() method is used to convert an struct to a pickable/json object.

default_cast(key=None, value=None)[source]

This method checks if key is already defined. If it is, it will return value as an evaluable string. If it is not, then it will do same action on the passed value.

dict()[source]
get(k, default=None)[source]
get_key(value)[source]

Reverse lookup

has_key(k)[source]
items()[source]
keys()[source]
load(*args, **kwargs)[source]
pop(k)[source]
set(k, v)[source]
setdefault(v)[source]
to_str(order=None, sep=', ')[source]

This method provides a formatable string for sorting

update(*args, **kwargs)[source]
values()[source]
fandango.objects.copy(obj)[source]

This method will return a copy for a python primitive object. It will not work for class objects unless they implement the __init__(other) constructor

fandango.objects.decorator_with_args(decorator)[source]

Decorator with Arguments must be used with parenthesis: @decorated() , even when arguments are not used!!!

This method gets an d(f,args,kwargs) decorator and returns a new single-argument decorator that embeds the new call inside.

But, this decorator disturbed stdout!!!!

There are some issues when calling nested decorators; it is clearly better to use Decorator classes instead.

fandango.objects.dirClasses(module, owned=False)[source]
fandango.objects.dirModule(module)[source]
fandango.objects.findModule(module)[source]
fandango.objects.loadModule(source, modulename=None)[source]
fandango.objects.locked(f, *args, **kwargs)[source]

decorator for secure-locked functions A key-argument _lock can be used to use a custom Lock object

fandango.objects.make_property(var, fget=<function _fget at 0x7eff5cee0938>, fset=<function _fset at 0x7eff5cee2500>, fdel=<function _fdel at 0x7eff5cee2578>)[source]

This Class is in Beta, not fully implemented yet

class fandango.objects.nullDecorator(*args)[source]

Bases: object

Empty decorator with null arguments, used to replace pyqtSignal,pyqtSlot

fandango.objects.obj2dict(obj, type_check=True, class_check=False, fltr=None)[source]

Converts a python object to a dictionary with all its members as python primitives

Parameters:fltr – a callable(name):bool method
fandango.objects.self_locked(func, reentrant=True)[source]

Decorator to make thread-safe class members @deprecated @note see in tau.core.utils.containers Decorator to create thread-safe objects. reentrant: CRITICAL:

With Lock() this decorator should not be used to decorate nested functions; it will cause Deadlock! With RLock this problem is avoided ... but you should rely more on python threading.