The Propertiser is an augmented Defaultor because it will set defaults attributes possibly in properties for the new-style decorated classes. This can set objects with high controlling features thanks to the binding
View the Propertiser notebook on [NbViewer](http://nbviewer.ipython.org/url/shareyoursystem.ouvaton.org/Propertiser.ipynb)
# -*- coding: utf-8 -*-
"""
<DefineSource>
@Date : Fri Nov 14 13:20:38 2014 \n
@Author : Erwan Ledoux \n\n
</DefineSource>
The Propertiser is an augmented Defaultor because it will set defaults attributes
possibly in properties for the new-style decorated classes. This can set objects
with high controlling features thanks to the binding
"""
#<DefineAugmentation>
import ShareYourSystem as SYS
BaseModuleStr="ShareYourSystem.Standards.Classors.Deriver"
DecorationModuleStr=BaseModuleStr
SYS.setSubModule(globals())
#</DefineAugmentation>
#<ImportSpecificModules>
import inspect
import collections
from ShareYourSystem.Standards.Objects import Initiator
#</ImportSpecificModules>
#<DefineLocals>
PropertizingGetStr="_"
PropertizingRepresentationStr="p:"
#</DefineLocals>
#<DefineFunctions>
def getPropertizedTupleWithItemTupleAndClass(_ItemTuple,_Class):
#Get the KeyStr, and the ValueVariable that should be a dict
PropertizedKeyStr=_ItemTuple[0]
PropertizedValueVariable=_ItemTuple[1]
PropertizedHideKeyStr=PropertizingGetStr+PropertizedKeyStr
#Check that this is a property yet or not
if type(PropertizedValueVariable)!=property:
#Init
PropertizedValueVariable=property()
#Definition the get function
PropertizedGetFunctionStr='get'+PropertizedKeyStr
if hasattr(_Class,PropertizedGetFunctionStr):
#Check for an already defined method
PropertizedGetFunction=getattr(_Class,PropertizedGetFunctionStr)
else:
#Definition a default one
def PropertizedGetFunction(self):
return getattr(self,PropertizedHideKeyStr)
PropertizedGetFunction.__name__=PropertizedGetFunctionStr
#Definition the set function
PropertizedSetFunctionStr='set'+PropertizedKeyStr
#Check
if hasattr(_Class,PropertizedSetFunctionStr):
#Check for an already defined method
PropertizedSetFunction=getattr(_Class,PropertizedSetFunctionStr)
else:
#Definition a default one
def PropertizedSetFunction(self,_SettingValueVariable):
self.__setattr__(PropertizedHideKeyStr,_SettingValueVariable)
PropertizedSetFunction.__name__='set'+PropertizedKeyStr
#Definition the del function
PropertizedDelFunctionStr='del'+PropertizedKeyStr
if hasattr(_Class,PropertizedDelFunctionStr):
#Check for an already defined method
PropertizedDelFunction=getattr(_Class,PropertizedDelFunctionStr)
else:
#Definition a default one
def PropertizedDelFunction(self):
self.__delattr__(PropertizedHideKeyStr)
PropertizedDelFunction.__name__='del'+PropertizedKeyStr
#Redefine
PropertizedValueVariable=property(
PropertizedGetFunction,
PropertizedSetFunction,
PropertizedDelFunction,
_ItemTuple[1]['PropertyDocStr'
]if 'PropertyDocStr' in _ItemTuple[1]
else "This is here a property but with no more details..."
)
#Definition the property
return (
PropertizedKeyStr,
PropertizedValueVariable
)
def getPropertizedVariableWithItemTuple(_ItemTuple):
#Maybe it is already defined
if 'PropertyInitVariable' in _ItemTuple[1]:
return _ItemTuple[1]['PropertyInitVariable']
else:
#Return the default one associated with the type
return SYS.getTypeClassWithTypeStr(SYS.getWordStrsListWithStr(_ItemTuple[0])[-1])
#</DefineFunctions>
#<Define_Class>
@DecorationClass()
class PropertiserClass(BaseClass):
def default_init(self,
**_KwargVariablesDict
):
#Call the parent init method
BaseClass.__init__(self,**_KwargVariablesDict)
def __call__(self,_Class):
#debug
'''
print('Defaultor l.31 __call__ method')
print('_Class is ',_Class)
print('')
'''
#Call the parent init method
BaseClass.__call__(self,_Class)
#Debug
'''
print('l.146 : We are going to propertize')
print('')
'''
#propertize
self.propertize()
#Debug
'''
print('l.153 : propertize is done')
print('')
'''
#Return
return _Class
def do_propertize(self):
#Alias
PropertizedClass=self.DoClass
#Debug
'''
print('PropertizedClass is ',PropertizedClass)
print('')
'''
#debug
'''
print('Propertiser l.47 default method')
print('Class is ',Class)
print('')
'''
#Check
if hasattr(PropertizedClass,"DefaultAttributeVariablesOrderedDict"):
#debug
'''
print('PropertizedClass.DefaultAttributeVariablesOrderedDict is',PropertizedClass.DefaultAttributeVariablesOrderedDict)
print('')
'''
#set the PropertizedDefaultTuplesList
PropertizedClass.PropertizedDefaultTuplesList=SYS._filter(
lambda __DefaultSetTuple:
type(__DefaultSetTuple[1]
)==property or (
hasattr(__DefaultSetTuple[1],'items'
) and 'DefaultValueType' in __DefaultSetTuple[1
] and __DefaultSetTuple[1
]['DefaultValueType']==property),
PropertizedClass.DefaultAttributeVariablesOrderedDict.items()
)
#debug
'''
print('Before set PropertizedClass.PropertizedDefaultTuplesList is ',PropertizedClass.PropertizedDefaultTuplesList)
print('')
'''
#set at the level of the class the PropertizingGetStr+KeyStr
map(
lambda __PropertizedDefaultTuple:
setattr(
PropertizedClass,
PropertizingGetStr+__PropertizedDefaultTuple[0],
getPropertizedVariableWithItemTuple(__PropertizedDefaultTuple)
),
PropertizedClass.PropertizedDefaultTuplesList
)
#set the PropertizedTuple for each at the level of the class
PropertizedClass.PropertizedDefaultTuplesList=map(
lambda __PropertizedDefaultTuple:
getPropertizedTupleWithItemTupleAndClass(
__PropertizedDefaultTuple,
PropertizedClass
),
PropertizedClass.PropertizedDefaultTuplesList
)
#debug
'''
print('After set PropertizedClass.PropertizedDefaultTuplesList is ',PropertizedClass.PropertizedDefaultTuplesList)
print('')
'''
#Reset at the level of the class the properties
map(
lambda __PropertizedDefaultTuple:
setattr(
PropertizedClass,
*__PropertizedDefaultTuple
),
PropertizedClass.PropertizedDefaultTuplesList
)
#Add to the KeyStrsList
PropertizedClass.KeyStrsList+=[
"PropertizedDefaultTuplesList"
]
#</Define_Class>
View the Propertiser sources on Github
In [3]:
#ImportModules
import ShareYourSystem as SYS
#Define
@SYS.PropertiserClass()
class MakerClass(SYS.PrinterClass):
def default_init(self,
_MakingMyFloat={
'DefaultValueType':property,
'PropertyInitVariable':3.,
'PropertyDocStr':'I am doing the thing here'
}
):
SYS.PrinterClass.__init__(self)
#Print and show that the class has already propertize_(get,set,del)MakingMyFloat
# a default _MakingMyFloat value and the MakingMyFloat prop
print('SYS.MakerClass.__dict__ is')
print(SYS.indent(SYS.MakerClass.__dict__))
#Define
MyMaker=SYS.MakerClass()
#print the __dict__, there is no things related to the
#MakingMyFloat property
print('MyMaker.__dict__ before set is ')
SYS._print(MyMaker.__dict__)
#set
MyMaker.MakingMyFloat=7.
#print the __dict__, now there is the hidden attribute
print('MyMaker.__dict__ after set is ')
SYS._print(MyMaker.__dict__)
#Define
MyMaker=SYS.MakerClass()
#print the repr : the instance just show the MakingMyFloat key
#that is actually the get of _MakingMyFloat in the class
print('MyMaker before set is ')
SYS._print(MyMaker)
#set
MyMaker.MakingMyFloat=7.
#print the repr : now the instance shows the _MakingMyFloat
#value that is particulat for the instance
print('MyMaker after set is ')
SYS._print(MyMaker)
In [5]:
#ImportModules
import ShareYourSystem as SYS
#Define
@SYS.PropertiserClass()
class MakerClass(object):
def default_init(self,
_MakingMyFloat={
'DefaultValueType':property,
'PropertyInitVariable':3.,
'PropertyDocStr':'I am doing the thing here'
},
_MakingMyList={
'DefaultValueType':property,
'PropertyInitVariable':[],
'PropertyDocStr':'I am doing the thing here'
},
_MakingMyInt={'DefaultValueType':int},
_MadeMyInt=0
):
object.__init__(self)
#Definition a binding function
def propertize_setMakingMyFloat(self,_SettingValueVariable):
#Print
#print('I am going to make the job directly !')
#set the value of the "hidden" property variable
self._MakingMyFloat=_SettingValueVariable
#Bind with MadeInt setting
self.MadeMyInt=int(self._MakingMyFloat)
#Definition a binding function
def propertize_setMakingMyList(self,_SettingValueVariable):
#set the value of the "hidden" property variable
self._MakingMyList=_SettingValueVariable+['Hellllllo']
#Definition a default instance
DefaultMaker=MakerClass()
#Definition a special instance
SpecialMaker=MakerClass(_MakingMyFloat=5,_MakingMyList=[4])
#Definition the AttestedStr
print('\n'.join(
[
'MakerClass.PropertizedDefaultTuplesList is '+SYS._str(
MakerClass.PropertizedDefaultTuplesList),
'What are you saying DefaultMaker ?',
'DefaultMaker.__dict__ is '+str(DefaultMaker.__dict__),
'DefaultMaker.MakingMyFloat is '+str(DefaultMaker.MakingMyFloat),
'DefaultMaker.MakingMyList is '+str(DefaultMaker.MakingMyList),
'DefaultMaker.MadeMyInt is '+str(DefaultMaker.MadeMyInt),
'What are you saying SpecialMaker ?',
'SpecialMaker.__dict__ is '+str(SpecialMaker.__dict__),
'SpecialMaker.MakingMyFloat is '+str(SpecialMaker.MakingMyFloat),
'SpecialMaker.MakingMyList is '+str(SpecialMaker.MakingMyList),
'SpecialMaker.MadeMyInt is '+str(SpecialMaker.MadeMyInt),
]
)
)
#Print
In [7]:
#ImportModules
import ShareYourSystem as SYS
#Define
@SYS.PropertiserClass()
class MakerClass(object):
def default_init(self,
_MakingMyFloat={
'DefaultValueType':property,
'PropertyInitVariable':3.,
'PropertyDocStr':'I am doing the thing here'
},
_MakingMyList={
'DefaultValueType':property,
'PropertyInitVariable':[],
'PropertyDocStr':'I am doing the thing here'
},
_MakingMyInt={'DefaultValueType':int},
_MadeMyInt=0
):
object.__init__(self)
def propertize_setMakingMyFloat(self,_SettingValueVariable):
#Print
#print('I am going to make the job directly !')
#set the value of the "hidden" property variable
self._MakingMyFloat=_SettingValueVariable
#Bind with MadeInt setting
self.MadeMyInt=int(self._MakingMyFloat)
def propertize_setMakingMyList(self,_SettingValueVariable):
#set the value of the "hidden" property variable
self._MakingMyList=_SettingValueVariable+['Hellllllo']
#Define
@SYS.PropertiserClass()
class BuilderClass(MakerClass):
def default_init(
self
):
SYS.MakerClass.__init__(self)
def propertize_setMakingMyList(self,_SettingValueVariable):
#call the base method
MakerClass.propertize_setMakingMyList(self,_SettingValueVariable)
#set the value of the "hidden" property variable
self._MakingMyList+=['Build en plus !']
#We need here to redefine
MakingMyList=property(
MakerClass.MakingMyList.fget,
propertize_setMakingMyList,
MakerClass.MakingMyList.fdel
)
#Definition a special instance
SpecialBuilder=BuilderClass(_MakingMyFloat=5,_MakingMyList=[4])
#Definition the AttestedStr
print('\n'.join(
[
'What are you saying SpecialBuilder ?',
'SpecialBuilder.__dict__ is '+str(SpecialBuilder.__dict__),
'SpecialBuilder.MakingMyFloat is '+str(SpecialBuilder.MakingMyFloat),
'SpecialBuilder.MakingMyList is '+str(SpecialBuilder.MakingMyList),
'SpecialBuilder.MadeMyInt is '+str(SpecialBuilder.MadeMyInt),
]
)
)
#Print
In [9]:
#ImportModules
import ShareYourSystem as SYS
#Define
@SYS.PropertiserClass()
class MakerClass(object):
def default_init(self,
_MakingMyList={
'DefaultValueType':property,
'PropertyInitVariable':None,
'PropertyDocStr':'I am doing the thing here',
'ShapeKeyStrsList':['MakingMyInt']
},
_MakingMyInt=3,
_MadeMyInt=0
):
object.__init__(self)
#Define
MyMaker=MakerClass()
#Set and this will bind the value of MakingMyInt
MyMaker.MakingMyList=[3,4]
#print
print('MyMaker.__dict__ is ')
print(SYS.indent(MyMaker))
print(MyMaker.__class__.DefaultAttributeVariablesOrderedDict['MakingMyList'])
In [11]:
#ImportModules
import ShareYourSystem as SYS
#Define
@SYS.PropertiserClass()
class MakerClass(object):
def default_init(self,
_MakingMyList=None,
_MakingMyInt={
'DefaultValueType':property,
'PropertyInitVariable':None,
'PropertyDocStr':'I am doing the thing here',
'ShapeDict':{
'MakingMyList':0
}
},
_MadeMyInt=0
):
object.__init__(self)
#Define
MyMaker=MakerClass()
#Set and this will bind the value of MakingMyInt
MyMaker.MakingMyInt=2
#print
print('MyMaker.__dict__ is ')
print(SYS.indent(MyMaker))
View the Propertiser sources on Github
# -*- coding: utf-8 -*-
"""
<DefineSource>
@Date : Fri Nov 14 13:20:38 2014 \n
@Author : Erwan Ledoux \n\n
</DefineSource>
The Propertiser is an augmented Defaultor because it will set defaults attributes
possibly in properties for the new-style decorated classes. This can set objects
with high controlling features thanks to the binding
"""
#<DefineAugmentation>
import ShareYourSystem as SYS
BaseModuleStr="ShareYourSystem.Standards.Classors.Doer"
DecorationModuleStr=BaseModuleStr
SYS.setSubModule(globals())
#</DefineAugmentation>
#<ImportSpecificModules>
import inspect
import collections
#</ImportSpecificModules>
#<DefineLocals>
PropertyGetStr="_"
PropertyRepresentationStr="p:"
PropertyPrefixStr="propertize_"
#</DefineLocals>
#<DefineFunctions>
def getPropertizedTupleWithItemTupleAndClass(_ItemTuple,_Class):
#Debug
'''
print('Propertiser l 39')
print('_ItemTuple is ')
print(_ItemTuple)
print('')
'''
#Get the KeyStr, and the ValueVariable that should be a dict
PropertizedKeyStr=_ItemTuple[0]
PropertizedValueVariable=_ItemTuple[1]
PropertizedHideKeyStr=PropertyGetStr+PropertizedKeyStr
#Check that this is a property yet or not
if type(PropertizedValueVariable)!=property:
#Init
PropertizedValueVariable=property()
#/###################/#
# Prepare the get property
#
#Definition the get function
PropertizedGetFunctionStr=PropertyPrefixStr+'get'+PropertizedKeyStr
#Check
if hasattr(_Class,PropertizedGetFunctionStr):
#Check for an already defined method
PropertizedGetFunction=getattr(_Class,PropertizedGetFunctionStr)
else:
#Definition a default one
def PropertizedGetFunction(_InstanceVariable):
"""
#/#################/#
# return the one hidden in the dict
# else return the one in the class
#Check
if hasattr(_InstanceVariable,PropertizedHideKeyStr):
#return
return getattr(_InstanceVariable,PropertizedHideKeyStr)
else:
#return
return getattr(_InstanceVariable.__class__,PropertizedKeyStr)
"""
#/#################/#
# return the one hidden in the dict
# else return None
if hasattr(_InstanceVariable,PropertizedHideKeyStr):
#return
return getattr(_InstanceVariable,PropertizedHideKeyStr)
else:
return None
PropertizedGetFunction.__name__=PropertizedGetFunctionStr
#/###################/#
# Prepare the set property
#
#Definition the set function
PropertizedSetFunctionStr=PropertyPrefixStr+'set'+PropertizedKeyStr
#Check
if hasattr(_Class,PropertizedSetFunctionStr):
#/######################/#
# Case where there is already something
#
#Check for an already defined method
PropertizedSetFunction=getattr(_Class,PropertizedSetFunctionStr)
else:
#/######################/#
# Default case
#
#Definition a default one
def PropertizedSetFunction(_InstanceVariable,_SettingValueVariable):
_InstanceVariable.__setattr__(PropertizedHideKeyStr,_SettingValueVariable)
PropertizedSetFunction.__name__=PropertizedSetFunctionStr
#/######################/#
# Case where we bind also the setting of the shaping atttributes
#
#Check
if 'ShapeKeyStrsList' in _ItemTuple[1]:
#get
PropertizedShapeKeyStrsList=_ItemTuple[1]['ShapeKeyStrsList']
#Debug
'''
print('Propertiser l 111')
print('There is a ShapeKeyStrsList')
print('PropertizedShapeKeyStrsList is ')
print(PropertizedShapeKeyStrsList)
print('')
'''
#import
import numpy as np
def PropertizedShapeSetFunction(_InstanceVariable,_SettingValueVariable):
#call the first
PropertizedSetFunction(_InstanceVariable,_SettingValueVariable)
#get the shape
PropertizedShapeIntsList=np.shape(
getattr(
_InstanceVariable,
PropertizedHideKeyStr
)
)
#Debug
'''
print('Propertiser l 137')
print('We shape here')
print('PropertizedHideKeyStr is')
print(PropertizedHideKeyStr)
print('PropertizedShapeKeyStrsList is ')
print(PropertizedShapeKeyStrsList)
print('PropertizedShapeIntsList is ')
print(PropertizedShapeIntsList)
print('')
'''
#map a set
map(
lambda __PropertizedShapeKeyStr,__PropertizedShapeInt:
setattr(
_InstanceVariable,
__PropertizedShapeKeyStr,
__PropertizedShapeInt
),
PropertizedShapeKeyStrsList,
PropertizedShapeIntsList
)
#/###################/#
# Prepare the del property
#
#Definition the del function
PropertizedDelFunctionStr=PropertyPrefixStr+'del'+PropertizedKeyStr
#Check
if hasattr(_Class,PropertizedDelFunctionStr):
#Check for an already defined method
PropertizedDelFunction=getattr(_Class,PropertizedDelFunctionStr)
else:
#Definition a default one
def PropertizedDelFunction(_InstanceVariable):
_InstanceVariable.__delattr__(PropertizedHideKeyStr)
PropertizedDelFunction.__name__=PropertizedDelFunctionStr
#Debug
'''
print('Propertizer l 109')
print('PropertizedDetFunction is ')
print(PropertizedDetFunction)
print('')
'''
#/###################/#
# Now set in the class
#
if 'ShapeKeyStrsList' in _ItemTuple[1]:
PropertizedBindSetFunction=PropertizedShapeSetFunction
else:
PropertizedBindSetFunction=PropertizedSetFunction
#Define in the class...
map(
lambda __PropertizedFunction:
setattr(
_Class,
__PropertizedFunction.__name__,
__PropertizedFunction
),
[
PropertizedGetFunction,
PropertizedBindSetFunction,
PropertizedDelFunction
]
)
#Define in the special dict...
map(
lambda __Function:
_Class.PropertyMethodsDict.__setitem__(
__Function.__name__,
__Function
),
[
PropertizedGetFunction,
PropertizedBindSetFunction,
PropertizedDelFunction
]
)
#Redefine
PropertizedValueVariable=property(
PropertizedGetFunction,
PropertizedBindSetFunction,
PropertizedDelFunction,
_ItemTuple[1]['PropertyDocStr'
]if 'PropertyDocStr' in _ItemTuple[1]
else "This is here a property but with no more details..."
)
#Definition the property
return (
PropertizedKeyStr,
PropertizedValueVariable
)
def getPropertizedVariableWithItemTuple(_ItemTuple):
#Maybe it is already defined
if 'PropertyInitVariable' in _ItemTuple[1]:
return _ItemTuple[1]['PropertyInitVariable']
else:
#Return the default one associated with the type
return SYS.getTypeClassWithTypeStr(SYS.getWordStrsListWithStr(_ItemTuple[0])[-1])
#</DefineFunctions>
#<Define_Class>
@DecorationClass()
class PropertiserClass(BaseClass):
def default_init(self,
**_KwargVariablesDict
):
#Call the parent init method
BaseClass.__init__(self,**_KwargVariablesDict)
def __call__(self,_Class):
#debug
'''
print('Propetizer l.179 __call__ method')
print('_Class is ',_Class)
print('')
'''
#Call the parent init method
BaseClass.__call__(self,_Class)
#Debug
'''
print('l.146 : We are going to propertize')
print('')
'''
#propertize
self.propertize()
#Debug
'''
print('l.153 : propertize is done')
print('')
'''
#Return
return _Class
def do_propertize(self):
#Alias
PropertizedClass=self.DoClass
#Debug
'''
print('PropertizedClass is ',PropertizedClass)
print('')
'''
#init
PropertizedClass.PropertyMethodsDict={}
#Add to the KeyStrsList
PropertizedClass.KeyStrsList+=[
"PropertyMethodsDict"
]
#debug
'''
print('Propertiser l.47 default method')
print('Class is ',Class)
print('')
'''
#/###################/#
# Check for new properties in the default dict
#
#Check
if hasattr(PropertizedClass,"DefaultAttributeVariablesOrderedDict"):
#debug
'''
print('PropertizedClass.DefaultAttributeVariablesOrderedDict is',PropertizedClass.DefaultAttributeVariablesOrderedDict)
print('')
'''
#set the PropertizedDefaultTuplesList
PropertizedClass.PropertizedDefaultTuplesList=SYS._filter(
lambda __DefaultSetTuple:
type(__DefaultSetTuple[1]
)==property or (
hasattr(__DefaultSetTuple[1],'items'
) and 'DefaultValueType' in __DefaultSetTuple[1
] and __DefaultSetTuple[1
]['DefaultValueType']==property),
PropertizedClass.DefaultAttributeVariablesOrderedDict.items()
)
#debug
'''
print('Propertiser l.266')
print('Before set PropertizedClass.PropertizedDefaultTuplesList is ',PropertizedClass.PropertizedDefaultTuplesList)
print('')
'''
#set at the level of the class the PropertyGetStr+KeyStr
map(
lambda __PropertizedDefaultTuple:
setattr(
PropertizedClass,
PropertyGetStr+__PropertizedDefaultTuple[0],
getPropertizedVariableWithItemTuple(__PropertizedDefaultTuple)
),
PropertizedClass.PropertizedDefaultTuplesList
)
#set the PropertizedTuple for each at the level of the class
PropertizedClass.PropertizedDefaultTuplesList=map(
lambda __PropertizedDefaultTuple:
getPropertizedTupleWithItemTupleAndClass(
__PropertizedDefaultTuple,
PropertizedClass
),
PropertizedClass.PropertizedDefaultTuplesList
)
#debug
'''
print('Propertiser l 293')
print('After set PropertizedClass.PropertizedDefaultTuplesList is ',
PropertizedClass.PropertizedDefaultTuplesList)
print('')
'''
#Reset at the level of the class the properties
map(
lambda __PropertizedDefaultTuple:
setattr(
PropertizedClass,
*__PropertizedDefaultTuple
),
PropertizedClass.PropertizedDefaultTuplesList
)
#Check
if hasattr(PropertizedClass,'PrintingClassSkipKeyStrsList'):
#map append in the KeyStrsList
map(
lambda __PropertizedDefaultTuple:
PropertizedClass.PrintingClassSkipKeyStrsList.extend(
[
#__PropertizedDefaultTuple[0],
PropertyGetStr+__PropertizedDefaultTuple[0]
]
),
PropertizedClass.PropertizedDefaultTuplesList
)
#Add to the KeyStrsList
PropertizedClass.KeyStrsList+=[
"PropertizedDefaultTuplesList"
]
#</Define_Class>