advanced-python-homework-2023/equipment/turtle_device.py

67 lines
2.2 KiB
Python
Raw Normal View History

2023-10-13 22:33:07 +03:00
from turtle import Turtle
2023-10-26 22:41:54 +03:00
from typing import Any, Collection, Optional
from controls.device import ActionDescriptor, SynchronyDevice, TraitDescriptor
2023-10-13 22:33:07 +03:00
class TurtleDevice(SynchronyDevice):
2023-10-26 22:41:54 +03:00
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])
2023-10-13 22:33:07 +03:00