Home > Net >  Need help to make python class
Need help to make python class

Time:12-01

I am quite new in python OOP.

I am trying to make a python class which gives me two list for_X and for_y. so that I can directly import this class in preprocessing pipeline.

Basically, for_X and for_y append some value from dictionary(mongoDB).

I tried this , but I know that it's not the genuine way to make class.

class ExtractValue:
    def __init__(self, d):
        self.d = d
    
    def mydata(self):
        for_X = []
        for_y = []
        for idx, value in enumerate(self.d):
            #print(value)
            for item in  value['main_value']:
                #print(item)
                for sub_key, sub_value in item.items():
                    if sub_key == 'AAA':
                        for_X.append(sub_value)
                        return for_X
                        #print(for_X)
                    else:
                        for_y.append(sub_value)
                        return for_y
                        

Any help and suggestion would be appreciated.

CodePudding user response:

Why don't you just do this? Simple function, one data set in, two lists out:

def ExtractValue(data):
    for_X = []
    for_y = []
    for value in data:
        #print(value)
        for item in  value['main_value']:
            #print(item)
            for sub_key, sub_value in item.items():
                if sub_key == 'AAA':
                    for_X.append(sub_value)
                else:
                    for_y.append(sub_value)
    return for_X, for_y

CodePudding user response:

I'd agree with @TRoberts, at least that you don't need a class ExtractValue assuming that the entire purpose of such a class is the return value of the mydata method. I'd instead suggest creating a helper function like extract_data that returns a class with the desired values, as shown below. If you want, you can also add this as a class method under your new class MyClass.

Below approach uses typing.NamedTuple which is a typed variant of the normal namedtuple, which is just a container class representing a tuple value. Depending on what you want to do, it might be slightly better to use something like a dataclass instead, for example.

from typing import NamedTuple


class MyClass(NamedTuple):
    for_x: list
    for_y: list


def extract_data(d: list):
    for_X = []
    for_y = []

    for value in d:
        for item in value['main_value']:
            for sub_key, sub_value in item.items():
                if sub_key == 'AAA':
                    for_X.append(sub_value)
                else:
                    for_y.append(sub_value)

    return MyClass(for_X, for_y)


print(extract_data([
    {'main_value': [{'AAA': 123, 'BBB': 321},
                    {'CCC': 'test'}]}
]))

Output:

MyClass(for_x=[123], for_y=[321, 'test'])
  • Related