Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Last active October 2, 2019 15:20
Show Gist options
  • Save mpkocher/21967be51a27916f0b41 to your computer and use it in GitHub Desktop.
Save mpkocher/21967be51a27916f0b41 to your computer and use it in GitHub Desktop.
Metric Rules sketch
class InValidMetricError(Exception):
pass
class TestReportMetric(TestBase):
REPORT_ID = ""
DEFAULT_VALUES = {}
# this will be populated via metaclass or @monkey_metrics model
RULES = []
def _apply_rules(self, rules):
# apply all rules or raise
pass
def test_metrics(self):
self._apply_rules(self.RULES)
class MetricRule(object):
def __init__(self, metric_id, callable_):
self.metric_id = metric_id
# should raise if not valid
self.callable = callable_
def validate(self, base_value, job_value):
return self.callable(base_value, job_value)
@staticmethod
def gt(metric_id):
# sugar
return MetricRule(metric_id, operator.gt)
@staticmethod
def range(metric_id, start, end):
return MetricRule(metric_id, lambda x, y: True)
class TestMappingStatsBase(TestBase):
REPORT_ID = "mapping_stats"
nbases = MetricRule('nbases', operator.gt)
my_metric = MetricRule.gt('my_metric')
nalignments = MetricRule("nalignments", lambda x, y: True)
class MappingJob01(TestMappingStatsBase):
# Or these could be loaded from custom.json
DEFAULTS = {
"nbases": 1234,
"nalignemnts": 9
}
class MappingJob01WithOverride(TestMappingStatsBase):
DEFAULTS = {
"nbases": 1234
"nalignments": 12
}
# Override specific rules on a per job basis
nbases = MetricRule("nbases", lambda x, y: True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment