Decorator Classes and Application in Inherited Programming

38 Views Asked by At

I am trying to implement a logger with Decorator classes with something like this -

import sys
import traceback


class ConsoleLogger:
  def __init__(self):
    pass
  
  def info(self, message):
    print('INFO {message}')

class EventLogger(object):
    def __init__(self, function):
        self.function = function
        
    def __call__(self, *ags, **kwargs):
    
        logger = ConsoleLogger()
        setattr(self.function, 'logger', logger)
        return self.function(*ags, **kwargs)
    
    @staticmethod
    def run(func):
        def wrapper( *args, **kwargs):
            try:
                return func(*args, **kwargs)
            except:
                exc_type, exc_obj,tb = sys.exc_info()
                f = tb.tb_frame
                lineno = tb.tb_lineno
                print lineno, exc_obj, tb, exc_type
        return wrapper
    
class Add(object):
    
    def __init__(self):
        pass    
    
    def add_together(self, a, b):
        self.logger.info('m')
        return a * b    
     
@EventLogger
class together(Add):
    
    def __init__(self):
        print (self)
        super(together, self).__init__()
        pass    
    
    @EventLogger.run
    def together_together(self, a, b):
        self.logger.info('m')
        print 10*10*'10'*10*'10'
        return a * b    
     
tog = together()
tog.together_together(5,5)

Everything seems to be fine when I run it on Python 3.0+ versions since super method call from together class can be done - super().__init__(), however, I have an application that is running on Python 2.7 and super(together, self).__init__() does not work.

This is the error I get:

  File "<input>", line 60, in <module>
  File "<input>", line 21, in __call__
  File "<input>", line 51, in __init__
TypeError: super: argument 1 must be type

Any thoughts would be appreciated.

0

There are 0 best solutions below