Дополнил turtle_device | HW3
This commit is contained in:
parent
052c0b43e6
commit
c5717555c3
@ -1,6 +1,6 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Optional, Collection, Any
|
from typing import Optional, Collection, Any
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
class DeviceLifecycleState(Enum):
|
class DeviceLifecycleState(Enum):
|
||||||
@ -36,7 +36,7 @@ class ActionDescriptor:
|
|||||||
info: Optional[str] = None
|
info: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class Device(metaclass=ABCMeta):
|
class Device(ABC):
|
||||||
_state = DeviceLifecycleState.INIT
|
_state = DeviceLifecycleState.INIT
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1,38 +1,52 @@
|
|||||||
import turtle
|
from turtle import Turtle
|
||||||
from typing import Optional, Collection, Any
|
from typing import Optional, Collection, Any
|
||||||
from controls.device import SynchronyDevice
|
from controls.device import SynchronyDevice
|
||||||
import time
|
from controls.device import TraitDescriptor
|
||||||
|
from controls.device import ActionDescriptor
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
class TurtleDevice(SynchronyDevice):
|
class TurtleDevice(SynchronyDevice):
|
||||||
def open(self):
|
def open(self):
|
||||||
|
self.turtle = Turtle()
|
||||||
super().open()
|
super().open()
|
||||||
turtle.Turtle()
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
self.turtle.clear()
|
||||||
super().close()
|
super().close()
|
||||||
turtle.bye()
|
|
||||||
|
|
||||||
def execute(self, action_name: str, *args, **kwargs):
|
def execute(self, action_name: str, *args, **kwargs):
|
||||||
getattr(turtle, action_name)(args[0], *kwargs)
|
getattr(self.turtle, action_name)(*args, **kwargs)
|
||||||
|
|
||||||
def read(self, trait_name: str):
|
def read(self, trait_name: str):
|
||||||
pass
|
"""Read physical state of trait `trait_name` from device."""
|
||||||
|
trait = self.trait_descriptors()[trait_name]
|
||||||
|
if not trait.readable : super().read(trait_name)
|
||||||
|
return trait.info
|
||||||
|
|
||||||
def write(self, trait_name: str, value: Any) -> bool:
|
def write(self, trait_name: str, value: Any) -> bool:
|
||||||
pass
|
"""Pass `value` to trait `trait_name` of device."""
|
||||||
|
if not self.trait_descriptors()[trait_name] : super().write(trait_name, value)
|
||||||
|
setattr(self.turtle, trait_name, value)
|
||||||
|
|
||||||
def invalidate(self, trait_name: str):
|
def invalidate(self, trait_name: str):
|
||||||
|
"""Invalidate logical state of trait `trait_name`"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def trait_descriptors(self):
|
def trait_descriptors(self):
|
||||||
traits = [attr for attr in turtle.Turtle.__dict__
|
traits = dict()
|
||||||
if attr.startswith('__') and not attr.startswith('_')]
|
for name, value in inspect.getmembers(self.turtle):
|
||||||
|
if not callable(value) and not name.startswith('_'):
|
||||||
|
info = f"{value}"
|
||||||
|
traits[name] = TraitDescriptor(name, info)
|
||||||
return traits
|
return traits
|
||||||
|
|
||||||
def action_descriptors(self):
|
def action_descriptors(self):
|
||||||
actions = [(name, func.__doc__) for name, func in inspect.getmembers(turtle.Turtle)
|
actions = dict()
|
||||||
if callable(func) and not name.startswith('__') and not name.startswith('_')]
|
for name, func in inspect.getmembers(self.turtle):
|
||||||
|
if callable(func) and not name.startswith('_'):
|
||||||
|
args = dict(inspect.signature(func).parameters)
|
||||||
|
info = inspect.getdoc(func)
|
||||||
|
actions[name] = ActionDescriptor(name, args, info)
|
||||||
return actions
|
return actions
|
||||||
|
|
||||||
def __getitem__(self, trait_name: str):
|
def __getitem__(self, trait_name: str):
|
||||||
|
Loading…
Reference in New Issue
Block a user