3H/W
This commit is contained in:
parent
0cf47dbf82
commit
3f4f48fb55
@ -1,9 +1,12 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Optional, Collection, Any
|
from typing import Optional, Collection, Any
|
||||||
from abc import abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from enum import Enum, auto
|
||||||
|
|
||||||
class DeviceLifecycleState:
|
class DeviceLifecycleState(Enum):
|
||||||
pass # TODO(Homework #3)
|
INIT = "init"
|
||||||
|
OPEN = "open"
|
||||||
|
CLOSE = "close"
|
||||||
|
|
||||||
|
|
||||||
class DevaceError(Exception):
|
class DevaceError(Exception):
|
||||||
@ -33,8 +36,7 @@ class ActionDescriptor:
|
|||||||
info: Optional[str] = None
|
info: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class Device:
|
class Device(ABC):
|
||||||
# TODO(Homework #3)
|
|
||||||
_state = DeviceLifecycleState.INIT
|
_state = DeviceLifecycleState.INIT
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -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
155061
equipment/turtle
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,66 @@
|
|||||||
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])
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user