def test_data_generator(verbose): if verbose: print("Generating test data") device1 = Device('test_device_1', True) field_values = [ FieldValue(Field("float", "signal", "db"), randint(0, 450) / 10.0), FieldValue(Field("float", "audio_level", "db"), randint(0, 450) / 10.0), FieldValue(Field("int", "light_level", "lux"), randint(0, 100)), FieldValue(Field("float", "temp", "f"), randint(0, 72)), ] device1.add_reading("NOW", field_values, 'test_data') device2 = Device('test_device_2', True) field_values = [ FieldValue(Field("float", "audio_level", "db"), randint(20, 35) / 10.0), FieldValue(Field("float", "battery_level", "volts"), randint(20, 35) / 10.0), FieldValue(Field("float", "signal_strength", "db"), randint(20, 35) / 10.0), FieldValue(Field("boolean", "switch", "state"), randint(0, 1)), FieldValue( Field("string", "message", "text"), ''.join( choice(string.ascii_uppercase + string.digits) for _ in range(10))), ] device2.add_reading("NOW", field_values, 'test_data')
def _update_fields(self, other): """ Update the session data from a Transmission JSON-RPC arguments dictionary """ if isinstance(other, dict): for key, value in iteritems(other): self._fields[key.replace('-', '_')] = Field(value, False) elif isinstance(other, Session): for key in list(other._fields.keys()): self._fields[key] = Field(other._fields[key].value, False) else: raise ValueError('Cannot update with supplied data')
def _set_upload_limit(self, limit): """ Set the upload limit. Can be a number, 'session' or None. """ if isinstance(limit, integer_types): self._fields['uploadLimited'] = Field(True, True) self._fields['uploadLimit'] = Field(limit, True) self._push() elif limit == None: self._fields['uploadLimited'] = Field(False, True) self._push() else: raise ValueError("Not a valid limit")
def _set_pex_enabled(self, enabled): """Enable/disable peer exchange.""" if isinstance(enabled, bool): self._fields['pex_enabled'] = Field(enabled, True) self._push() else: raise TypeError("Not a valid type")
def _set_priority(self, priority): """ Set the priority as string. Can be one of 'low', 'normal', 'high'. """ if isinstance(priority, string_types): self._fields['bandwidthPriority'] = Field(PRIORITY[priority], True) self._push()
def _set_seed_ratio_mode(self, mode): """ Set the seed ratio mode as string. Can be one of 'global', 'single' or 'unlimited'. """ if isinstance(mode, str): self._fields['seedRatioMode'] = Field(RATIO_LIMIT[mode], True) self._push() else: raise ValueError("Not a valid limit")
def _set_seed_ratio_limit(self, limit): """ Set the seed ratio limit as float. """ if isinstance(limit, (integer_types, float)) and limit >= 0.0: self._fields['seedRatioLimit'] = Field(float(limit), True) self._push() else: raise ValueError("Not a valid limit")
def _set_seed_idle_limit(self, limit): """ Set the seed idle limit in minutes. """ if isinstance(limit, integer_types): self._fields['seedIdleLimit'] = Field(limit, True) self._push() else: raise ValueError("Not a valid limit")
def _set_peer_limit(self, limit): """ Set the peer limit. """ if isinstance(limit, integer_types): self._fields['peer_limit'] = Field(limit, True) self._push() else: raise ValueError("Not a valid limit")
def _set_queue_position(self, position): """Set the queue position for this torrent.""" if self._rpc_version() >= 14: if isinstance(position, integer_types): self._fields['queuePosition'] = Field(position, True) self._push() else: raise ValueError("Not a valid position") else: pass
def AlexaInterfaceFactory(baseClass, id, data, **kwargs): # add attributes for key, attribDef in baseClass._structure.items(): sub = KWARGS_PAT.findall(attribDef[1]) if len(sub) == 1: # arguments with placeholders subclass = sub[0] if not hasattr(baseClass, subclass): setattr(baseClass, subclass, ParamField) setattr(getattr(baseClass, subclass), key, Field(key)) elif len(sub) > 1: # if necessary need to implement this raise Exception('Unable to handle "%s" more han one subkey: %s' % (sub, )) else: # the normal attribs without parameter setattr(baseClass, key, Field(key)) return baseClass(id=id, data=data, **kwargs)
def test_bad(): bad = [ ['number', 'light_level', 'lux', 52], ['float', 'light level', 'lux', 52], ['float', 'light_level', 'lu x', 52], ['float', 'light_level', 'lu x', None], ['float', 'light_level', 'lu x', "String"], ] for test_data in bad: with pytest.raises(IllformedField): Field(*test_data)
def initialize_default_properties(self, session=None): self.players = [] self.deck = [] self.player_to_play = None self.field = Field() self.deck = [] self.running = False self.trump_suit = None self.session = session if session: self._receive_events_from_db(session)
def raw_utterance_with_keyword(self, vocab, train=True): cache = cacher('dts_ConvAI2.raw_utterance_with_keyword', vocab, train) if cache.cached: return cache.data examples, corpus, check_dict = self.raw_utterance(train) field = Field(vocab) kwext = KeywordExtractor(field) for example in prolog(examples, name=' -extract keywords'): kws = kwext.extract(example['uttr'].lst) example['kwpos'] = kws['kwpos'] example['keywords'] = kws['keywords'] examples = preprocess(examples, field, log=' -process to_pack cls') return cache.cache((examples, field, corpus, check_dict))
def test_good(): good = [ ['float', 'light_level', 'lux', 52], ['int', 'height', 'cm', 12], ['string', 'vegtable', 'cm', 'potato'], ['str', 'vegtable', 'cm', 'potato'], ['string', 'height', 'cm', 12], ['str', 'height', 'cm', 12], ['bool', 'height', 'cm', True], ['boolean', 'height', 'cm', False], ['bool', 'height', 'cm', 0], ['boolean', 'height', 'cm', 1], ['bool', 'height', 'cm', '0'], ['boolean', 'height', 'cm', '1'], ] for test_data in good: Field(*test_data)
def process_json(line, test, verbose): if verbose: print('JSON', line) serial_log.rx('JSON: ' + line) try: asdict = json.loads(line) utc = "NOW" if 'utc' not in asdict else asdict['utc'] fvs = FieldValue(Field.fromDict(asdict), asdict['value']) device = Device(asdict['device_id'], True) device.add_reading(utc, [fvs], 'serial_port') except KeyError as e: log.error('Serial data missing key', exception=str(e)) if verbose: print('Exception', e) except Exception as e: log.error('Serial receive failed', exception=str(e)) if verbose: print('Exception', e)