def __init__(self, values): ''' Initializes the datastore Using the input values we create the default datastore value and the starting address :param values: Either a list or a dictionary of values ''' if isinstance(values, dict): self.values = values elif hasattr(values, '__iter__'): self.values = dict(enumerate(values)) else: raise ParameterException( "Values for datastore must be a list or dictionary") self.default_value = get_next(itervalues(self.values)).__class__() self.address = get_next(iterkeys(self.values))
def setValues(self, address, values, use_as_default=False): ''' Sets the requested values of the datastore :param address: The starting address :param values: The new values to be set :param use_as_default: Use the values as default ''' if isinstance(values, dict): new_offsets = list( set(list(values.keys())) - set(list(self.values.keys()))) if new_offsets and not self.mutable: raise ParameterException("Offsets {} not " "in range".format(new_offsets)) self._process_values(values) else: if not isinstance(values, list): values = [values] for idx, val in enumerate(values): if address + idx not in self.values and not self.mutable: raise ParameterException("Offset {} not " "in range".format(address + idx)) self.values[address + idx] = val if not self.address: self.address = get_next(iterkeys(self.values), None) if use_as_default: for idx, val in iteritems(self.values): self.default_value[idx] = val
def testReadWriteMultipleRegistersRequestDecode(self): request, response = get_next((k,v) for k,v in self.request_read.items() if getattr(k, 'function_code', 0) == 23) request.decode(response) self.assertEqual(request.read_address, 0x01) self.assertEqual(request.write_address, 0x01) self.assertEqual(request.read_count, 0x05) self.assertEqual(request.write_count, 0x05) self.assertEqual(request.write_byte_count, 0x0a) self.assertEqual(request.write_registers, [0x00]*5)
def __init__(self, values=None, mutable=True): """ Initializes a sparse datastore. Will only answer to addresses registered, either initially here, or later via setValues() :param values: Either a list or a dictionary of values :param mutable: The data-block can be altered later with setValues(i.e add more blocks) If values are list , This is as good as sequential datablock. Values as dictionary should be in {offset: <values>} format, if values is a list, a sparse datablock is created starting at offset with the length of values. If values is a integer, then the value is set for the corresponding offset. """ self.values = {} self._process_values(values) self.mutable = mutable self.default_value = self.values.copy() self.address = get_next(iterkeys(self.values), None)