Executer

Doc


An Executer can exec commands with the six.exec_ function


View the Executer notebook on [NbViewer](http://nbviewer.ipython.org/url/shareyoursystem.ouvaton.org/Executer.ipynb)

Code



# -*- coding: utf-8 -*-
"""


<DefineSource>
@Date : Fri Nov 14 13:20:38 2014 \n
@Author : Erwan Ledoux \n\n
</DefineSource>


An Executer can exec commands with the six.exec_ function
"""

#<DefineAugmentation>
import ShareYourSystem as SYS
BaseModuleStr="ShareYourSystem.Itemizers.Sharer"
DecorationModuleStr="ShareYourSystem.Classors.Classer"
SYS.setSubModule(globals())
#</DefineAugmentation>

#<ImportSpecificModules>
import six
#</ImportSpecificModules>

#<DefineLocals>
ExecutingPrefixStr="Exec_"
#</DefineLocals>

#<DefineClass>
@DecorationClass()
class ExecuterClass(BaseClass):

    #Definition
    RepresentingKeyStrsList=[
                                    'ExecutingPrefixStr'
                                ]

    def default_init(self,
                _ExecutingPrefixStr="" ,
                **_KwargVariablesDict):

        #Call the parent __init__ method
        BaseClass.__init__(self,**_KwargVariablesDict)

    #<Hook>@Hooker.HookerClass(**{'HookingBeforeCallingDictsList':[BaseClass.get]})
    #@Imitater.ImitaterClass()
    def mimic_get(self):

        #Check
        if self.GettingKeyVariable.startswith(ExecutingPrefixStr):

            #Definition the ExecStr
            self.ExecutingPrefixStr=ExecutingPrefixStr.join(
                self.GettingKeyVariable.split(ExecutingPrefixStr)[1:])

            #debug
            '''
            self.debug(('self.',self,['ExecutingPrefixStr']))
            '''

            #Put the output in a local Local Variable
            self.execute()

        #debug
        '''
        self.debug('BaseClass.get is '+str(BaseClass.get))
        '''

        #Call the parent get method
        return BaseClass.get(self)

    #<Hook>@Hooker.HookerClass(**{'HookingBeforeCallingDictsList':[BaseClass.set]})
    #@Imitater.ImitaterClass()
    def mimic_set(self):

        #Check
        if type(self.SettingValueVariable
            ) in SYS.StrTypesList and self.SettingValueVariable.startswith(
            ExecutingPrefixStr):

            #Definition the ExecStr
            self.ExecutingPrefixStr=ExecutingPrefixStr.join(
                self.SettingValueVariable.split(ExecutingPrefixStr)[1:])

            #debug
            '''
            self.debug(('self.',self,['ExecutingPrefixStr',"SettingValueVariable"]))
            '''

            #Put the output in a local Local Variable
            self.execute()

            #debug
            '''
            self.debug(('self.',self,['ExecutingPrefixStr',"SettingValueVariable"]))
            '''

        #Call the parent get method
        BaseClass.set(self)


    def do_execute(self):

        #Execute
        six.exec_(self.ExecutingPrefixStr,locals())

        #Return self
        #return self

#</DefineClass>

View the Executer sources on Github

Example

Let's create an empty class, which will automatically receive special attributes from the decorating ClassorClass, specially the NameStr, that should be the ClassStr without the TypeStr in the end.


In [3]:
#ImportModules
import ShareYourSystem as SYS
from ShareYourSystem.Classors import Attester
from ShareYourSystem.Itemizers import Executer

#Definition and update with an exec Str
MyExecuter=Executer.ExecuterClass()

MySecondInt=MyExecuter.__setitem__(
    'MySecondInt',
    'Exec_self.SettingValueVariable=1+1'
).MySecondInt

#Exec is also possible in a getting
GettedValueVariable=MyExecuter['Exec_self.GettedValueVariable=self.MySecondInt-1']
    
#Definition the AttestedStr
SYS._attest(
    [
        'MySecondInt is  '+str(MySecondInt),
        'GettedValueVariable is '+str(GettedValueVariable)
    ]
) 

#Print



*****Start of the Attest *****

MySecondInt is  2

------

GettedValueVariable is None

*****End of the Attest *****