From c9b9c848be49f4feb44611c0ab7b53d9b3a1575d Mon Sep 17 00:00:00 2001 From: Worros Date: Mon, 16 Nov 2009 15:19:24 +0800 Subject: [PATCH] Add aggregate function VARIANCE() for sqlite --- pyfpdb/fpdb_db.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pyfpdb/fpdb_db.py b/pyfpdb/fpdb_db.py index 18958880..27291d7f 100644 --- a/pyfpdb/fpdb_db.py +++ b/pyfpdb/fpdb_db.py @@ -30,11 +30,29 @@ except ImportError: logging.info("Not using sqlalchemy connection pool.") use_pool = False +try: + from numpy import var + use_numpy = True +except ImportError: + logging.info("Not using numpy to define variance in sqlite.") + use_numpy = False import fpdb_simple import FpdbSQLQueries import Configuration +# Variance created as sqlite has a bunch of undefined aggregate functions. + +class VARIANCE: + def __init__(self): + self.store = [] + + def step(self, value): + self.store.append(value) + + def finalize(self): + return float(var(self.store)) + class fpdb_db: MYSQL_INNODB = 2 PGSQL = 3 @@ -130,6 +148,10 @@ class fpdb_db: , detect_types=sqlite3.PARSE_DECLTYPES ) sqlite3.register_converter("bool", lambda x: bool(int(x))) sqlite3.register_adapter(bool, lambda x: "1" if x else "0") + if use_numpy: + self.db.create_aggregate("variance", 1, VARIANCE) + else: + logging.warning("Some database functions will not work without NumPy support") else: raise FpdbError("unrecognised database backend:"+backend) self.cursor = self.db.cursor()