Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from abc import ABCMeta,abstractmethod
- from persistent import Persistent
- class AbstractValue(Persistent):
- __metaclass__=ABCMeta
- @abstractmethod
- def returnvalue(self):
- pass
- class Value(AbstractValue):
- def __init__(self,value):
- self.value=value
- def returnvalue(self):
- return(self.value)
- class NoneValue(AbstractValue):
- def returnvalue(self):
- return(None)
- class AbstractVariable(Persistent):
- __metaclass__=ABCMeta
- @abstractmethod
- def getvalue(self):
- pass
- @abstractmethod
- def setvalue(self,value):
- pass
- @abstractmethod
- def getvar(self,path):
- pass
- @abstractmethod
- def setvar(self,path,variable):
- pass
- class Variable(AbstractVariable):
- def __init__(self,value=NoneValue()):
- if not isinstance(value,AbstractValue):
- raise(TypeError("Expected a Value"))
- self.value=value
- self.tree={}
- def getvalue(self):
- return(self.value.returnvalue())
- def setvalue(self,value):
- if not isinstance(value,AbstractValue):
- raise(TypeError("Expected a Value"))
- else:
- self.value=value
- def getvar(self,path):
- return(self._getvar(path.split('.')))
- def _getvar(self,path):
- if len(path)>1:
- return(self.tree[path[0]]._getvar(path[1:]))
- else:
- return(self.tree[path[0]])
- def setvar(self,path,variable):
- if not isinstance(variable,AbstractVariable):
- raise(TypeError("Expected a Variable"))
- else:
- self._setvar(path.split('.'),variable)
- def _setvar(self,path,variable):
- if len(path)>1:
- self._getvar(path[:-1])._setvar(path[-1],variable)
- else:
- self.tree[path[0]]=variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement