Git Product home page Git Product logo

Comments (6)

Hebruwu avatar Hebruwu commented on May 27, 2024 1

Image

from insight.

simonhkswan avatar simonhkswan commented on May 27, 2024
class MetricFactory():

    def metric_from_dict(self, d: Dict[str, Any]) -> Metric:
        ...

from insight.

Hebruwu avatar Hebruwu commented on May 27, 2024
"""
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.

from insight.

Hebruwu avatar Hebruwu commented on May 27, 2024
"""
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.

from insight.

Hebruwu avatar Hebruwu commented on May 27, 2024
"""
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.

from insight.

Hebruwu avatar Hebruwu commented on May 27, 2024

Note: what is labelled as a STEP should be possible to do through argument list unpacking.

from insight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.