File size: 1,496 Bytes
8e6ec01 1e322e1 278b90a 8e6ec01 1e322e1 c7285af 278b90a 1e322e1 278b90a 1e322e1 278b90a 1e322e1 278b90a 1e322e1 d1c4a3a 278b90a 8e6ec01 278b90a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from abc import abstractmethod
from typing import Any, Dict, Optional
from .collections import ListCollection
from .dataclass import NonPositionalField
from .operator import StreamInstanceOperator
class Instruction(StreamInstanceOperator):
"""The role of instruction is to add instruction to every instance.
Meaning the instruction is taking the instance and generating instruction field for it.
"""
skip_rendered_instance: bool = NonPositionalField(default=True)
def process(
self, instance: Dict[str, Any], stream_name: Optional[str] = None
) -> Dict[str, Any]:
if self.skip_rendered_instance:
if "instruction" in instance:
return instance
instance["instruction"] = self.get_instruction(instance)
return instance
@abstractmethod
def get_instruction(self, instance: Dict[str, object]) -> str:
pass
class TextualInstruction(Instruction):
text: str
def get_instruction(self, instance: Dict[str, object]) -> str:
return self.text
class EmptyInstruction(Instruction):
def get_instruction(self, instance: Dict[str, object]) -> str:
return ""
class InstructionsList(ListCollection):
def verify(self):
for instruction in self.items:
assert isinstance(instruction, Instruction)
class InstructionsDict(Dict):
def verify(self):
for _key, instruction in self.items():
assert isinstance(instruction, Instruction)
|