Ejemplo n.º 1
0
 async def update(self, record: Record):
     # Column name of value mapping
     bindings = {self.parent.config.key: record.key}
     # Features
     features = record.features(self.parent.config.features.keys())
     for feature_name, column_name in self.parent.config.features.items():
         bindings[column_name] = features.get(feature_name, None)
     # Predictions
     predictions = record.predictions(self.parent.config.predictions.keys())
     for (
         feature_name,
         (value_column_name, confidence_column_name),
     ) in self.parent.config.predictions.items():
         bindings[value_column_name] = None
         if confidence_column_name is not None:
             bindings[confidence_column_name] = None
         if feature_name in predictions:
             bindings[value_column_name] = predictions[feature_name][
                 "value"
             ]
             if confidence_column_name is not None:
                 bindings[confidence_column_name] = predictions[
                     feature_name
                 ]["confidence"]
     # Bindings should be the values for each column, where the value for the
     # key is not repeated for the UPDATE. If using REPLACE INTO, don't
     # repeat values
     values = list(bindings.values())
     if not "REPLACE" in self.parent.config.update.upper():
         values += list(bindings.values())[1:]
     # Execute the update query
     await self.conn.execute(self.parent.config.update, values)
     self.logger.debug("Updated: %s: %r", record.key, bindings)
Ejemplo n.º 2
0
 async def update(self, record: Record):
     db = self.parent.db
     # Store feature data
     feature_cols = self.parent.FEATURE_COLS
     feature_data = OrderedDict.fromkeys(feature_cols)
     feature_data.update(record.features(feature_cols))
     await db.execute(
         "INSERT OR REPLACE INTO features (key, " +
         ", ".join(feature_cols) + ") "
         "VALUES(?, " + ", ".join("?" * len(feature_cols)) + ")",
         [record.key] + list(feature_data.values()),
     )
     # Store prediction
     try:
         prediction = record.prediction("target_name")
         prediction_cols = self.parent.PREDICTION_COLS
         prediction_data = OrderedDict.fromkeys(prediction_cols)
         prediction_data.update(prediction.dict())
         await db.execute(
             "INSERT OR REPLACE INTO prediction (key, " +
             ", ".join(prediction_cols) + ") "
             "VALUES(?, " + ", ".join("?" * len(prediction_cols)) + ")",
             [record.key] + list(prediction_data.values()),
         )
     except KeyError:
         pass
Ejemplo n.º 3
0
class TestRecord(unittest.TestCase):
    def setUp(self):
        self.null = Record("null")
        self.full = Record(
            "full",
            data=dict(
                features=dict(dead="beef"),
                extra=dict(extra="read all about it"),
            ),
            extra=dict(half=True),
        )

    def test_dict(self):
        data = self.full.dict()
        self.assertIn("extra", data)

    def test_repr(self):
        repr(self.full)

    def test_str(self):
        self.full.prediction = RecordPrediction()
        self.assertIn("Undetermined", str(self.full))
        self.full.data.prediction = {
            "Prediction": RecordPrediction(value="Good")
        }
        self.assertIn("Good", str(self.full))
        self.full.extra.update(dict(hi=5))
        self.assertIn("5", str(self.full))
        self.full.extra = dict()
        self.assertNotIn("5", str(self.full))

    def test_merge(self):
        null = Record("null")
        null.merge(self.full)
        self.assertIn("half", null.extra)
        self.assertTrue(null.extra["half"])

    def test_key(self):
        return self.full.data.key

    def test_evaluated(self):
        old_last_updated = self.full.data.last_updated
        results = {"new": "feature"}
        self.full.evaluated({"feed": "face"})
        self.assertIn("feed", self.full.data.features)
        self.assertEqual("face", self.full.data.features["feed"])
        self.full.evaluated(results, overwrite=True)
        self.assertEqual(self.full.data.features, results)
        self.assertLessEqual(old_last_updated, self.full.data.last_updated)

    def test_features(self):
        self.assertIn("dead", self.full.features())
        self.assertIn("dead", self.full.features(["dead"]))
        self.assertFalse(self.full.features(["dead", "beaf"]))

    def test_predicted(self):
        old_prediction = self.full.data.prediction.copy()
        old_last_updated = self.full.data.last_updated
        self.full.predicted("target_name", "feed", 1.00)
        self.assertNotEqual(old_prediction, self.full.data.prediction)
        self.assertLessEqual(old_last_updated, self.full.data.last_updated)

    def test_prediction(self):
        self.full.predicted("target_name", "feed", 1.00)
        self.assertTrue(self.full.prediction("target_name"))