#StackBounty: #python #python-decorators #python-3.8 Implementing python decorators in a toy example

Bounty: 100

I have been trying to find a use case to learn decorators and I think I have found one which is relevant to me.

I am using the following codes.

In the file class1.py I have:

import pandas as pd, os

class myClass():
    def __init__(self):
        fnDone = f'C:user1Desktoploc1fn.csv'
        if os.path.exists(fnDone): return
        self.Fn1()
        pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

    def Fn1(self):
        print('something')

if __name__ == '__main__':
    myClass()

In the file class2.py I have:

class myClassInAnotherFile():
    def __init__(self):
        fnDone = f'C:user1Desktoploc2fn.csv'
        if os.path.exists(fnDone): return
        self.Fn1()
        self.Fn2()
        pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

    def Fn1(self):
        print('something')

    def Fn2(self):
        print('something else')

if __name__ == '__main__':
    myClassInAnotherFile('DoneFile12)

Is there a way to define a generic decorator code in another file called utilities.py so that I can do something of the following sort:

Desired in the file class1.py I have:

import pandas as pd, os

class myClass():
    def __init__(self):
        fnDone = f'C:user1Desktoploc1fn.csv'
        self.Fn1()
        pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

    def Fn1(self):
        print('something')

if __name__ == '__main__':
    @myDecorator
    myClass()

In the file class2.py I have:

class myClassInAnotherFile():
    def __init__(self):
        fnDone = f'C:user1Desktoploc2fn.csv'
        self.Fn1()
        self.Fn2()
        pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

    def Fn1(self):
        print('something')

    def Fn2(self):
        print('something else')

if __name__ == '__main__':
    @myDecorator
    myClassInAnotherFile()

Essentially mimicking the original behavior using a decorator.

Edit1:
I am looking to extend the functionality of my class definitions. In the both original class definitions, I repeat the code which checks for fnDone file and if it is present, exits the class. Goal is to have a decorator which checks for the fnDone file and exits the class if it is present.

Edit2:
I can do this as a function also but I am trying to learn how to extend functionality of a class or method using decorators.

Edit3:
Does it make it easier if I have the following instead in class1.py:

def myClass():
    fnDone = f'C:user1Desktoploc1fn.csv'
    if os.path.exists(fnDone): return
    self.Fn1()
    pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

def Fn1(self):
    print('something')

if __name__ == '__main__':
    myClass()

and class2.py as following:

def myClassInAnotherFile():
    fnDone = f'C:user1Desktoploc2fn.csv'
    if os.path.exists(fnDone): return
    self.Fn1()
    self.Fn2()
    pd.DataFrame({'Done': 1}, index=[0]).to_csv(fnDone)

def Fn1(self):
    print('something')

def Fn2(self):
    print('something else')

if __name__ == '__main__':
    myClassInAnotherFile('DoneFile12)


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.