"""Responsible for the basic classes in the project.Attributes----------global_clock : `Clock` A clock that provides a global reference for all objects that share a timeline."""importuuidfromabcimportABCMetafromtensortrade.core.clockimportClockglobal_clock=Clock()
[docs]classIdentifiable(object,metaclass=ABCMeta):"""Identifiable mixin for adding a unique `id` property to instances of a class. """@propertydefid(self)->str:"""Gets the identifier for the object. Returns ------- str The identifier for the object. """ifnothasattr(self,'_id'):self._id=str(uuid.uuid4())returnself._id@id.setterdefid(self,identifier:str)->None:"""Sets the identifier for the object Parameters ---------- identifier : str The identifier to set for the object. """self._id=identifier
[docs]classTimeIndexed:"""A class for objects that are indexed by time. """_clock=global_clock@propertydefclock(self)->Clock:"""Gets the clock associated with this object. Returns ------- `Clock` The clock associated with this object. """returnself._clock@clock.setterdefclock(self,clock:Clock)->None:"""Sets the clock associated with this object. Parameters ---------- clock : `Clock` The clock to be associated with this object. """self._clock=clock
[docs]classTimedIdentifiable(Identifiable,TimeIndexed,metaclass=ABCMeta):"""A class an identifiable object embedded in a time process. Attributes ---------- created_at : `datetime.datetime` The time at which this object was created according to its associated clock. """def__init__(self)->None:self.created_at=self._clock.now()@propertydefclock(self)->"Clock":"""Gets the clock associated with the object. Returns ------- `Clock` The clock associated with the object. """returnself._clock@clock.setterdefclock(self,clock:"Clock")->None:"""Sets the clock associated with this object. In addition, the `created_at` attribute is set according to the new clock. Parameters ---------- clock : `Clock` The clock to be associated with this object. """self._clock=clockself.created_at=self._clock.now()
[docs]classObservable:"""An object with some value that can be observed. An object to which a `listener` can be attached to and be alerted about on an event happening. Attributes ---------- listeners : list of listeners A list of listeners that the object will alert on events occurring. Methods ------- attach(listener) Adds a listener to receive alerts. detach(listener) Removes a listener from receiving alerts. """def__init__(self):self.listeners=[]
[docs]defattach(self,listener)->"Observable":"""Adds a listener to receive alerts. Parameters ---------- listener : a listener object Returns ------- `Observable` : The observable being called. """self.listeners+=[listener]returnself
[docs]defdetach(self,listener)->"Observable":"""Removes a listener from receiving alerts. Parameters ---------- listener : a listener object Returns ------- `Observable` The observable being called. """self.listeners.remove(listener)returnself