Advertisement
Guest User

NewVariableClassZODB.py

a guest
Dec 29th, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. from abc import ABCMeta,abstractmethod
  2. from persistent import Persistent
  3. class AbstractValue(Persistent):
  4.     __metaclass__=ABCMeta
  5.     @abstractmethod
  6.     def returnvalue(self):
  7.         pass
  8. class Value(AbstractValue):
  9.     def __init__(self,value):
  10.         self.value=value
  11.     def returnvalue(self):
  12.         return(self.value)
  13. class NoneValue(AbstractValue):
  14.     def returnvalue(self):
  15.         return(None)
  16. class AbstractVariable(Persistent):
  17.     __metaclass__=ABCMeta
  18.     @abstractmethod
  19.     def getvalue(self):
  20.         pass
  21.     @abstractmethod
  22.     def setvalue(self,value):
  23.         pass
  24.     @abstractmethod
  25.     def getvar(self,path):
  26.         pass
  27.     @abstractmethod
  28.     def setvar(self,path,variable):
  29.         pass
  30. class Variable(AbstractVariable):
  31.     def __init__(self,value=NoneValue()):
  32.         if not isinstance(value,AbstractValue):
  33.             raise(TypeError("Expected a Value"))
  34.         self.value=value
  35.         self.tree={}
  36.     def getvalue(self):
  37.         return(self.value.returnvalue())
  38.     def setvalue(self,value):
  39.         if not isinstance(value,AbstractValue):
  40.             raise(TypeError("Expected a Value"))
  41.         else:
  42.             self.value=value
  43.     def getvar(self,path):
  44.         return(self._getvar(path.split('.')))
  45.     def _getvar(self,path):
  46.         if len(path)>1:
  47.             return(self.tree[path[0]]._getvar(path[1:]))
  48.         else:
  49.             return(self.tree[path[0]])
  50.     def setvar(self,path,variable):
  51.         if not isinstance(variable,AbstractVariable):
  52.             raise(TypeError("Expected a Variable"))
  53.         else:
  54.             self._setvar(path.split('.'),variable)
  55.     def _setvar(self,path,variable):
  56.         if len(path)>1:
  57.             self._getvar(path[:-1])._setvar(path[-1],variable)
  58.         else:
  59.             self.tree[path[0]]=variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement