Skip to content

The subclass of email_logit in the decorator section has a problem #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
GimpelZhang opened this issue Sep 25, 2020 · 3 comments
Open

Comments

@GimpelZhang
Copy link

Hi all,

the last subclass "email_logit" in the decorator section didn't produce the right result. decorator

Usage:

@email_logit
def myfunc1():
    pass

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in __init__
TypeError: __init__() missing 1 required positional argument: 'func'
@luoyeguigenno1
Copy link

I also encountered this problem. Is there someone knows what causes that?

@TimurNurlygayanov
Copy link

Yes, this is the problem, because arguments should be passed in a different way for init functions, here is the fixed version:

class logit(object):
    _logfile = 'out.log'

    def __init__(self, func, *args):
        self.func = func

    def __call__(self, *args, **kwargs):
        log_string = self.func.__name__ + " was called"
        print(log_string)

        with open(self._logfile, 'a') as opened_file:
            opened_file.write(log_string + '\n')

        # Now, send a notification
        self.notify()

        # return base func
        return self.func(*args, **kwargs)

    def notify(self):
        # logit only logs, no more
        pass


class email_logit(logit):
    """ A logit implementation for sending emails to admins
        when the function is called.
    """
    def __init__(self, *args, email='[email protected]', **kwargs):
        self.email = email
        super(email_logit, self).__init__(*args, **kwargs)

    def notify(self):
        print('Sending email to {0}'.format(self.email))


@email_logit
def my_func(a=None):
    print(a)


# Run the code:
my_func(3)

@TimurNurlygayanov
Copy link

And here is another fix for the same issue #222

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants