Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to call Python class methods automatically upon instantiation

I have a machine learning algorithm which involves a series of steps, such as cleaning data, preparing training data etc. Each step is stored in a separate method of a python class. I'm wondering what the best practice way to structure my class is so that the steps are automatically executed upon class instantiation.

Here's what I've done (the code is illustrative, but this approach works on the real algorithm). It feels a little clunky. Is there a more elegant way?

 class Kaggle():
    """
    An algorithm
    """

    def __init__( self ):
        self.bar = 1

    def step_one( self, some_text_data ):
        self.bar = 1 ** 2
        # Do some data cleaning
        # return processed data

    def step_two( self ):
        foo = step_one(baz)
        # do some more processing

    def step_three( self ):
        bar = step_two()
        # output results

    def run( self ):
        self.step_one()
        self.step_two()
        self.step_three()

if __name__ == "__main__":
    kaggle = Kaggle()
    kaggle.run()
like image 549
cs_stackX Avatar asked Dec 19 '22 03:12

cs_stackX


2 Answers

If your goal is for the object to be "automatically executed upon class instantiation", just put self.run() in the init:

def __init__(self):
    self.bar = 1
    self.run()
like image 165
Alexander Avatar answered Dec 21 '22 18:12

Alexander


Put all the calls in your __init__ method. Is this not what you wanted to achieve? You could add a flag with a default value, that allows you to not run the tests if you want.

def __init__( self, runtests=True ):
    self.bar = 1
    if runtests:
        self.step_one()
        self.step_two()
        self.step_three()
like image 28
Paul Rooney Avatar answered Dec 21 '22 18:12

Paul Rooney