0

I have some experience in Python and want to manipulate some data files using classes, mostly to gain experience in OOP. Here is the scenario: for each sample that we test (e.g., sample_1), the tool generates a text data file (e.g., sample_1_file) with the following structure (you can see the file has different data sections):

test_type_I, meta_data_I
{data_I = n1 rows x m1 columns}

test_type_II, meta_data_II
{data_II = n2 rows x m2 columns}

test_type_III
{data_III = n3 rows x m3 columns}
.
.
and so on

What I hope to achieve is to be able to access the test_type, meta_data, and data, for each data section, and then manipulate the data however I want. For example, say I create an object like

sample_1 = myClass(sample_1_file)

then I want to be able to access different sections of the data file:

sample_1.meta_data_I
plot(sample_1.data_I['x'],sample_1.data_I['y'])
print(sample_1.data_I)
and so on

I have read about classes, and have seen some youtube videos, typical employee class and such. However, in my case, here are my questions:

1. What does my __init__ method take as input? 
2. What am I initializing?
3. What would be my typical methods?
4. What are the data for my class

I can do what I want with simple functions in Python (and pandas), but I really want to gain experience in classes and want to apply it to something that is relevant to my work. I found this: How to overload __init__ method based on argument type? which has some relevance, but the answer uses advanced concepts that I am not familiar with.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Murchak
  • 183
  • 1
  • 3
  • 17

1 Answers1

1

The simplest version of a class would look something like this:

class SampleResult():
    def __init__(self, sample_number, sample_dataframe):
        self.number = sample_number
        self.data = sample_dataframe

You would then instantiate this class with the following:

result1 = SampleResult(1, sample1_dataframe)

You would access the data, for example, with:

result1.data['x']

In this code, you are instantiating an instance of a SampleResult object. You can set the __init__ method to take any parameters you like - but every method must accept at least one parameter called self (the name "self" is simply a convention, but a very strong convention).

Your methods and data are entirely up to you - it depends on what you want to do!

speedyturkey
  • 126
  • 9