Home > other >  Store data in a class dynamically and access data as class attributes
Store data in a class dynamically and access data as class attributes

Time:06-13

I am trying to write a class that takes data where the dataframe IDs as strings and the values as DataFrames and create class attributes accessing the data.

I was able to write a small example of a similar class that needs the methods to be created in a static manner and return the objects as class methods but I would like to loop over the data, taking in the keys for the dfs and allow for access to each df using attributes.

minimum working example

from dataclasses import dataclass
import pandas as pd

# re-writing as dataclass
@dataclass
class Dataset:

    # data container dictionary as class attribute
    dict = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
    
    
    def df1_id(self) -> pd.DataFrame:# class method to create as class attribute
        return dict['df1_id']

    def df2_id(self) -> pd.DataFrame:# same class method above
        return dict['df2_id']

    def df3_id(self) -> pd.DataFrame:# same class method above
        return dict['df3_id']

    def dataframes_as_class_attributes(self):
        # store the dfs to access as class attributes
        # replacing 3 methods above
        return 

result

datasets = Dataset()

print(datasets.df1_id())

expected result

datasets = Dataset()

print(datasets.df1_id) # class attribute created by looping through the dict object

Edit:

CodePudding user response:

You could use setattr like below:

from dataclasses import dataclass
import pandas as pd

@dataclass
class Dataset:

    dict_ = {'df1_id':pd.DataFrame({'col1':[1,1]}), 
            'df2_id':pd.DataFrame({'col2':[2,2]}), 
            'df3_id':pd.DataFrame({'col3':[3,3]})}
            
    def __post_init__(self):
        for key, val in self.dict_.items():
            setattr(self, key, val)

To avoid conflicts with python keywords put a single trailing underscore after variable name. (PEP 8)

  • Related