data-attribute-recommendation-python-sdk icon indicating copy to clipboard operation
data-attribute-recommendation-python-sdk copied to clipboard

Config proposal (API Simplification)

Open DBusAI opened this issue 4 years ago • 3 comments

This is number 3 from #83 , @mhaas , what do you think?

import json
class Feature:
    def __init__(self,columns:list=[]):
        self.columns = columns
        self.repr = self._get_def()
    @property
    def featuretype(self):
        pass
    def _get_def(self):
        cols = []
        for col in self.columns:
            cols.append({"label": col, "type": self.featuretype})
        return cols
    def __add__(self,other):
        self.repr=self.repr+other.repr
        return self
    def __repr__(self):
        return str(self.repr)

class Category(Feature):
    def __init__(self,columns:list=[]):
        super(Category,self).__init__(columns)
    @property
    def featuretype(self):
        return "CATEGORY"
    
class Text(Feature):
    def __init__(self,columns:list=[]):
        super(Text,self).__init__(columns)
    @property
    def featuretype(self):
        return "TEXT"

class Number(Feature):
    def __init__(self,columns:list=[]):
        super(Number,self).__init__(columns)
    @property
    def featuretype(self):
        return "NUMBER"

class Schema:
    def __init__(self,features=None,labels=None,name=None):
        self.features = features
        self.labels = labels
        self.name = name
        self.repr = self._get_def()
    def _get_def(self):
        return json.dumps({
                            "features": self.features.repr,
                            "labels": self.labels.repr,
                            "name": self.name,
                            },indent=2)
    def __repr__(self):
        return str(self.repr)

features = Text(['MYTEXT'])+Number(['ANumeric'])+Category(['Manufacturee','description'])
labels = Category(['Label1','Label2'])+Number(['Label3'])
schema = Schema(features,labels,"bestbuy-category-prediction")
print(schema)

result: { "features": [ { "label": "MYTEXT", "type": "TEXT" }, { "label": "ANumeric", "type": "NUMBER" }, { "label": "Manufacturee", "type": "CATEGORY" }, { "label": "description", "type": "CATEGORY" } ], "labels": [ { "label": "Label1", "type": "CATEGORY" }, { "label": "Label2", "type": "CATEGORY" } ], "name": "bestbuy-category-prediction" }

DBusAI avatar Nov 18 '20 11:11 DBusAI