forked from Advanced_Python/advanced-python-homework-2023
modified: controls/device.py
new file: equipment/turtle modified: equipment/turtle_device.py
This commit is contained in:
parent
80be82544c
commit
77b69fe3bd
@ -2,8 +2,10 @@ from dataclasses import dataclass
|
||||
from typing import Optional, Collection, Any
|
||||
from abc import abstractmethod
|
||||
|
||||
class DeviceLifecycleState:
|
||||
pass # TODO(Homework #3)
|
||||
class DeviceLifecycleState(Enum):
|
||||
INIT = "init"
|
||||
OPEN = "open"
|
||||
CLOSE = "close"
|
||||
|
||||
|
||||
class DevaceError(Exception):
|
||||
@ -33,7 +35,7 @@ class ActionDescriptor:
|
||||
info: Optional[str] = None
|
||||
|
||||
|
||||
class Device:
|
||||
class Device(ABC):
|
||||
# TODO(Homework #3)
|
||||
_state = DeviceLifecycleState.INIT
|
||||
|
||||
@ -44,9 +46,13 @@ class Device:
|
||||
def close(self):
|
||||
self._state = DeviceLifecycleState.CLOSE
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def trait_descriptors(self) -> Collection[TraitDescriptor]:
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def action_descriptors(self) -> Collection[ActionDescriptor]:
|
||||
pass
|
||||
|
||||
@ -79,4 +85,7 @@ class SynchronyDevice(Device):
|
||||
@abstractmethod
|
||||
def invalidate(self, trait_name: str):
|
||||
"""Invalidate logical state of trait `trait_name`"""
|
||||
pass
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
return super().close()
|
155061
equipment/turtle
Normal file
155061
equipment/turtle
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,68 @@
|
||||
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):
|
||||
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])
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user