Marin Draganov
Marin Draganov

Reputation: 59

How to convert code in a basic script into a class with Instances?

I have a basic Python script that I need to convert to a class and create multiple instances of that class.

The script has some global data holders that the functions modify. The global variables are something like this:

# a global list
list_1 = [...]
# a global dictionary
dict_1 = {...}
# a global pandas dataframe
df_1 = pd.DataFrame(...)

I have a single main function (that performs the most essential tasks) and lots of auxiliary functions for specific tasks that the main function calls when needed. Something like this:

# main function
def  main_def():
    global list_1, dict_1, df_1
    aux_def_1 (list_1, df_1)
    aux_def_2 (dict_1, df_1)

# auxiliary function 1
def  aux_def_1 ()
    global list_1, dict_1, df_1
    ...

# auxiliary function 2  
def  aux_def_2 ()
    global list_1, dict_1, df_1
    ...

I use the above script in jupyter notebook like this:

import script as scr

scr.main_def()

scr.aux_def_1()
scr.aux_def_2()

scr.list_1
scr.dict_1
scr.df_1

Now I need to create multiple main fuctions like the above and the most of the global variables and the auxiliary functions will stay the same.

One way to do this is to create a base class defining the global variables and the auxiliary functions and after that to create instances of that class. Each instance will have a different main function.

What is the best way to do that with minimal changes to the existing code?

EDIT: Is it possible to arrange the matter as follows:

class BaseClass:
    # BaseClass list
    list_1 = [...]
    # BaseClass dictionary
    dict_1 = {...}
    # BaseClass pandas dataframe
    df_1 = pd.DataFrame(...)

    # BaseClass auxiliary function 1
    def  aux_def_1 ()

    # BaseClass auxiliary function 2  
    def  aux_def_2 ()

class MainDef_1(BaseClass)
    def __init__(self, parameter1, parameter2, parameter3):
        self.parameter1 = parameter1
        self.parameter2 = parameter2
        self.parameter3 = parameter3

class MainDef_2(BaseClass)
    def __init__(self, parameter1, parameter3, parameter4):
        self.parameter1 = parameter1
        self.parameter3 = parameter3
        self.parameter4 = parameter4

Upvotes: 1

Views: 2033

Answers (1)

martineau
martineau

Reputation: 123473

This may be too simplistic (or I'm still not understanding the details of what you want done, but it seems like this kind of approach would work).

class BaseClass:
    list_1 = [...]
    dict_1 = {...}
    df_1 = pd.DataFrame(...)

    def aux_def_1(self):
        pass

    def aux_def_2(self):
        pass


class MainDef_1(BaseClass)
    def __init__(self, parameter1, parameter2, parameter3):
        self.parameter1 = parameter1
        self.parameter2 = parameter2
        self.parameter3 = parameter3

    def main(self):
        # whatever this version does...for example.
        self.list_1  # Accesses attribute of base class.
        x = self.parameter1 + 42  # Accesses attribute of this class.


class MainDef_2(BaseClass)
    def __init__(self, parameter1, parameter3, parameter4):
        self.parameter1 = parameter1
        self.parameter3 = parameter3
        self.parameter4 = parameter4

    def main(self):
        # whatever this version does...for example.
        result = self.aux_def_2()  # Use method defined in base class.
        y = self.parameter4 + 13  # Accesses attribute of this class.

Each subclass defines its own main() method. In it the needed parameters can be referenced via parameterN as well as the data and methods of the baseclass (again, just by prefixing their names with "self.".

Upvotes: 1

Related Questions