Closed simonhkswan closed 2 years ago
class MetricFactory():
def metric_from_dict(self, d: Dict[str, Any]) -> Metric:
...
"""
Option 1: MetricFactory in base.py
Step 1: Create a registry of all the metrics.
Substep: recurse through _Metric children in depth first search and add to to _registry with 'name' as the key
and class reference as the value.
Step 2: given a dictionary which contains the name of the metric and parameters, call the metric from _registry.
"""
class MetricFactory:
_registry: Dict[str, Any] = {}
@classmethod
def _update_registry_recuresively(cls, next_metric):
if next_metric.name is not None:
cls._registry.append({next_metric.name: next_metric})
for subclass in next_metric.__subclasses__():
cls._update_registry_recuresively(subclass)
@classmethod
def metric_from_dict(cls, bluprnt: Dict[str, any]):
if len (cls._registry) == 0:
cls._update_registry_recuresively(_Metric)
# STEP: Create a list of named parameters based on dictionary entries to pass into the Metric constructor.
new_metric = cls._registry[bluprnt['name']](...)
return new_metric
# Benefit: simple and does not interfere with the metrics.
"""
Option 2: MetricFactory in metric.py or in a separate module.
Step 1: Create a registry of all the metrics.
Substep: recurse through OneColumnMetric, TwoColumnMetric, DataFrameMetric, and TwoDataFrameMetric children in
depth first search and add to to _registry with 'name' as the key and class reference as the value.
Step 2: given a dictionary which contains the name of the metric and parameters, call the metric from _registry.
"""
class MetricFactory:
_registry: Dict[str, Any] = {}
@classmethod
def _update_registry_recuresively(cls, next_metric):
if next_metric.name is not None:
cls._registry.append({next_metric.name: next_metric})
for subclass in next_metric.__subclasses__():
cls._update_registry_recuresively(subclass)
@classmethod
def metric_from_dict(cls, bluprnt: Dict[str, any]):
if len(cls._registry) == 0:
cls._update_registry_recuresively(OneColumnMetric)
cls._update_registry_recuresively(TwoColumnMetric)
cls._update_registry_recuresively(DataFrameMetric)
cls._update_registry_recuresively(TwoDataFrameMetric)
# STEP: Create a list of named parameters based on dictionary entries to pass into the Metric constructor.
new_metric = cls._registry[bluprnt['name']](...)
return new_metric
# Benefit: does not interfere with the metrics. Does not interfere with a private class.
"""
Option 3: As a class method of _Metric.
Step 1: Create a registry of all the metrics.
Substep: recurse through OneColumnMetric, TwoColumnMetric, DataFrameMetric, and TwoDataFrameMetric children in
depth first search and add to to _registry with 'name' as the key and class reference as the value.
Step 2: given a dictionary which contains the name of the metric and parameters, call the metric from _registry.
"""
class _Metric:
_registry: Dict[str, Any] = {}
@classmethod
def _update_registry_recuresively(cls, next_metric):
if next_metric.name is not None:
cls._registry.append({next_metric.name: next_metric})
for subclass in next_metric.__subclasses__():
cls._update_registry_recuresively(subclass)
@classmethod
def metric_from_dict(cls, bluprnt: Dict[str, any]):
if len(cls._registry) == 0:
cls._update_registry_recuresively(cls)
# STEP: Create a list of named parameters based on dictionary entries to pass into the Metric constructor.
new_metric = cls._registry[bluprnt['name']](...)
return new_metric
# Benefit: Can call from any metric with the same result.
Note: what is labelled as a STEP should be possible to do through argument list unpacking.
The idea has two steps:
Metric.to_dict()
andMetric.from_dict(d: Dict[str, Any])
A good test of the functionality:
If i am given a dictionary, which function to I call to recreate the right metric?