modified: controls/device.py

new file:   equipment/turtle
	modified:   equipment/turtle_device.py
This commit is contained in:
vviora 2023-11-17 17:32:31 +03:00
parent 80be82544c
commit 77b69fe3bd
3 changed files with 155136 additions and 6 deletions

View File

@ -2,8 +2,10 @@ from dataclasses import dataclass
from typing import Optional, Collection, Any from typing import Optional, Collection, Any
from abc import abstractmethod from abc import abstractmethod
class DeviceLifecycleState: class DeviceLifecycleState(Enum):
pass # TODO(Homework #3) INIT = "init"
OPEN = "open"
CLOSE = "close"
class DevaceError(Exception): class DevaceError(Exception):
@ -33,7 +35,7 @@ class ActionDescriptor:
info: Optional[str] = None info: Optional[str] = None
class Device: class Device(ABC):
# TODO(Homework #3) # TODO(Homework #3)
_state = DeviceLifecycleState.INIT _state = DeviceLifecycleState.INIT
@ -44,9 +46,13 @@ class Device:
def close(self): def close(self):
self._state = DeviceLifecycleState.CLOSE self._state = DeviceLifecycleState.CLOSE
@property
@abstractmethod
def trait_descriptors(self) -> Collection[TraitDescriptor]: def trait_descriptors(self) -> Collection[TraitDescriptor]:
pass pass
@property
@abstractmethod
def action_descriptors(self) -> Collection[ActionDescriptor]: def action_descriptors(self) -> Collection[ActionDescriptor]:
pass pass
@ -79,4 +85,7 @@ class SynchronyDevice(Device):
@abstractmethod @abstractmethod
def invalidate(self, trait_name: str): def invalidate(self, trait_name: str):
"""Invalidate logical state of trait `trait_name`""" """Invalidate logical state of trait `trait_name`"""
pass pass
def close(self):
return super().close()

155061
equipment/turtle Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,68 @@
from turtle import Turtle from turtle import Turtle
from controls.device import SynchronyDevice from typing import Any, Collection, Optional
from controls.device import ActionDescriptor, SynchronyDevice, TraitDescriptor
class TurtleDevice(SynchronyDevice): class TurtleDevice(SynchronyDevice):
pass # TODO(Homework #3) def open(self, name, **kwargs):
self.turtle = Turtle(kwargs)
super().open()
def close(self):
del self.turtle
super().close()
# @property
# def trait_descriptors(self):
# traits = [attribute for attribute in vars(self.turtle).items()
# if (attribute[0].startswith('_') == False)]
# return Collection[[TraitDescriptor(*tr) for tr in traits]]
@property
def trait_descriptors(self):
traits = [attribute for attribute in vars(self.turtle).items()
if (attribute[0].startswith('_') == False)]
self.traits = [TraitDescriptor(*tr) for tr in traits]
return self.traits
# @property
# def action_descriptors(self):
# actions = [(attribute, getattr(t, attribute)) for attribute in dir(t)
# if (attribute.startswith('_') == False)]
# return Collection[[TraitDescriptor(*ac) for ac in actions]]
@property
def action_descriptors(self):
actions = [(attribute, getattr(t, attribute)) for attribute in dir(t)
if (attribute.startswith('_') == False)]
self.actions = [TraitDescriptor(*ac) for ac in actions]
return self.acions
def read(self, trait_name):
collection = self.traits
return ([collection[i].info for i in range(len(collection))
if collection[i].name == trait_name])
def write(self, trait_name: str, value: Any) -> bool:
collection = self.traits
for i in range(len(collection)):
if collection[i].name == trait_name:
collection[i].info=value
def execute(self, action_name: str, *args, **kwargs):
self.turtle.action_name(args, *kwargs)
def invalidate(self, trait_name: str):
return super().invalidate(trait_name)
def __getitem__(self, trait_name: str) -> Any | None:
collection = self.traits
return ([collection[i] for i in range(len(collection))
if collection[i].name == trait_name])