def _genrootclass(name): """Creates a new root metric class.""" class Cls(object): dependencies = () @property def schema(self): """Defines schema for root metric if provided.""" if self._schema is not None: return self._schema # fill in schema code @property def name(self): """Assigns a name to root metric object.""" return self.__class__.__name__ def __init__(self, db): """Constructor for root metric object in database.""" self._schema = None self.db = db def __call__(self, conds=None, *args, **kwargs): """Retrieves a root metric with given input conditions.""" if self.name not in self.db.tables: return None return self.db.query(self.name, conds=conds) Cls.__name__ = str(name) register_metric(Cls) return Cls
def _genmetricclass(f, name, depends, scheme): """Creates a new metric class with a given name, dependencies, and schema. Parameters ---------- name : str Metric name depends : list of lists (table name, tuple of indices, column name) Dependencies on other database tables (metrics or root metrics) scheme : list of tuples (column name, data type) Schema for metric """ if not isinstance(scheme, schemas.schema): scheme = schemas.schema(scheme) class Cls(Metric): dependencies = depends schema = scheme func = staticmethod(f) __doc__ = inspect.getdoc(f) def __init__(self, db): """Constructor for metric object in database.""" super(Cls, self).__init__(db) def __call__(self, frames, conds=None, known_tables=None, *args, **kwargs): """Computes metric for given input data and conditions.""" # FIXME test if I already exist in the db, read in if I do if known_tables is None: known_tables = self.db.tables() if self.name in known_tables: return self.db.query(self.name, conds=conds) return f(*frames) Cls.__name__ = str(name) register_metric(Cls) return Cls