async def record(self, key: str): query = self.parent.config.record_query record = Record(key) db = self.conn await db.execute(query, (key, )) row = await db.fetchone() if row is not None: features = {} predictions = {} for key, value in row.items(): if key.startswith("feature_"): features[key.replace("feature_", "")] = value elif "_value" in key: target = key.replace("_value", "") predictions[target] = { "value": row[target + "_value"], "confidence": row[target + "_confidence"], } record.merge( Record( row["key"], data={ "features": features, "prediction": predictions }, )) return record
async def record(self, key: str): record = Record(key) async with self.parent.db() as db_ctx: try: row = await db_ctx.lookup( self.parent.config.table_name, cols=None, # None turns into *. We want all rows conditions=[[Condition("key", "=", key)]], ).__anext__() except StopAsyncIteration: # This would happen if there is no matching row, so the async generator reached the end return record if row is not None: features = {} predictions = {} for key, value in row.items(): if key.startswith("feature_"): features[key.replace("feature_", "")] = value elif "_value" in key: target = key.replace("_value", "") predictions[target] = { "value": row[target + "_value"], "confidence": row[target + "_confidence"], } record.merge( Record( row["key"], data={ "features": features, "prediction": predictions }, )) return record
async def record(self, key: str): # Create a blank record in case it doesn't exist within the source record = Record(key) # Execute the query to get a single record from a key await self.conn.execute(self.parent.config.record, (key,)) # Retrieve the result row = await self.conn.fetchone() # Convert it to a record if it exists and populate the previously blank # record by merging the two if row is not None: record.merge(self.row_to_record(row)) self.logger.debug("Got: %s: %r", record.key, record.export()) return record
async def record(self, key: str): record = Record(key) db = self.conn # Get features await db.execute("SELECT json FROM ml_data WHERE key=%s", (key, )) dump = await db.fetchone() if dump is not None and dump[0] is not None: record.merge(Record(key, data=json.loads(dump[0]))) await db.execute("SELECT maintained FROM `status` WHERE key=%s", (key, )) maintained = await db.fetchone() if maintained is not None and maintained[0] is not None: record.evaluated({"maintained": str(maintained[0])}) return record
def test_merge(self): null = Record("null") null.merge(self.full) self.assertIn("half", null.extra) self.assertTrue(null.extra["half"])