try: from pkg_resources import require except ImportError: pass else: require("numpy") import unittest from common.python.simulations import BlockSimulation, properties_from_ini # Finding relative to a file, give it something in the test_data dir NAMES, PROPERTIES = properties_from_ini( __file__.replace("/test_block_simulation.py", "/test_data/anything.py"), "test.block.ini") class MyTest(BlockSimulation): FUNC, A, INPA, OUT = PROPERTIES class TestBlockSimulation(unittest.TestCase): maxDiff = None def setUp(self): self.o = MyTest() def test_setter(self): assert self.o.changes is None self.o.OUT = 45 assert self.o.OUT == 45
from common.python.simulations import BlockSimulation, properties_from_ini, \ TYPE_CHECKING import numpy if TYPE_CHECKING: from typing import Dict UP = 0 DOWN = 1 NAMES, PROPERTIES = properties_from_ini(__file__, "counter.block.ini") class CounterSimulation(BlockSimulation): ENABLE, TRIG, DIR, START, STEP, MAX, MIN, CARRY, OUT = PROPERTIES def on_changes(self, ts, changes): """Handle changes at a particular timestamp, then return the timestamp when we next need to be called""" super(CounterSimulation, self).on_changes(ts, changes) # This is a ConfigBlock object for us to get our strings from if self.MAX == 0 and self.MIN == 0: MIN = numpy.iinfo(numpy.int32).min MAX = numpy.iinfo(numpy.int32).max else: MIN = self.MIN MAX = self.MAX # Set attributes for name, value in changes.items(): setattr(self, name, value)
from common.python.simulations import BlockSimulation, properties_from_ini import os import csv NAMES, PROPERTIES = properties_from_ini(__file__, "pgen.block.ini") class PgenSimulation(BlockSimulation): ENABLE, TRIG, TABLE, TABLE_ADDRESS, TABLE_LENGTH, REPEATS, ACTIVE, OUT, \ HEALTH = PROPERTIES def __init__(self): self.table_data = [] self.current_line = 0 self.current_cycle = 0 def on_changes(self, ts, changes): """Handle changes at a particular timestamp, then return the timestamp when we next need to be called""" # This is a ConfigBlock object super(PgenSimulation, self).on_changes(ts, changes) if NAMES.TABLE_ADDRESS in changes: # open the table file_dir = os.path.join( os.path.dirname(__file__), self.TABLE_ADDRESS) assert os.path.isfile(file_dir), "%s does not exist" % file_dir with open(file_dir, "r") as table: reader = csv.DictReader(table) self.table_data = [int(line['POS']) for line in reader]
from common.python.simulations import BlockSimulation, properties_from_ini, \ TYPE_CHECKING if TYPE_CHECKING: from typing import Dict RISING = 0 FALLING = 1 EITHER = 2 NAMES, PROPERTIES = properties_from_ini(__file__, "srgate.block.ini") class SrgateSimulation(BlockSimulation): ENABLE, SET, RST, WHEN_DISABLED, SET_EDGE, RST_EDGE, FORCE_SET, \ FORCE_RST, OUT = PROPERTIES def inp_matches_edge(self, inp, edge): if edge == RISING and inp == 1 or edge == FALLING and inp == 0 \ or edge == EITHER and inp is not None: return True def on_changes(self, ts, changes): """Handle changes at a particular timestamp, then return the timestamp when we next need to be called""" # This is a ConfigBlock object super(SrgateSimulation, self).on_changes(ts, changes) # Set attributes for name, value in changes.items(): setattr(self, name, value)