i am trying to implement my own version of a DailyLogFile
from twisted.python.logfile import DailyLogFile class NDailyLogFile(DailyLogFile): def __init__(self, name, directory, rotateAfterN = 1, defaultMode=None): DailyLogFile.__init__(self, name, directory, defaultMode) # why do not use super. here? lisibility maybe? # self.rotateAfterN = rotateAfterN def shouldRotate(self): """Rotate when N days have passed since file creation""" delta = datetime.date(*self.toDate()) - datetime.date(*self.toDate(self.createdOn)) return delta > datetime.timedelta(self.rotateAfterN) def __getstate__(self): state = BaseLogFile.__getstate__(self) del state["rotateAfterN"] return state threadable.synchronize(NDailyLogFile) but it looks like i miss a fundamental of Python subclassing process...as i get this error :
Traceback (most recent call last): File "/home/twistedtestproxy04.py", line 88, in <module> import ndailylogfile File "/home/ndailylogfile.py", line 56, in <module> threadable.synchronize(NDailyLogFile) File "/home/lt/mpv0/lib/python2.6/site-packages/twisted/python/threadable.py", line 71, in synchronize sync = _sync(klass, klass.__dict__[methodName]) KeyError: 'write' so i need to explicitly add and define others methods like Write and rotate method like this:
class NDailyLogFile(DailyLogFile): [...] def write(self, data): # why must i add these ? DailyLogFile.write(self, data) def rotate(self): # as we do nothing more than calling the method from the base class! DailyLogFile.rotate(self) threadable.synchronize(NDailyLogFile) while i thought it would be correctly inherit from the base mother class. Notice that i do nothing, only calling "super",
please can someone explain why i am wrong on my first idea that it was not necessary to add the Write method?
is there a way to say to Python inside my NDailyLogFile that it shoud have all the methods DailyLogFile that are not defined directly from its mother class? So that it prevents this king of error _sync(klass, klass.__dict__[methodName] and that avoid to specify excplicitly ?
( original code of DailyLogFile that inspired me it taken from the twisted source here https://github.com/tzuryby/freespeech/blob/master/twisted/python/logfile.py )
EDIT: about using super, i get:
File "/home/lt/inwork/ndailylogfile.py", line 57, in write super.write(self, data) exceptions.AttributeError: type object 'super' has no attribute 'write' so will not use it. My thoughs was it was right... i must have definitively missed something
super. The problem is thatthreadable.pyis not very friendly to that OOP stuff, as the line that throws an error checks for existence ofwritein that particular class, but not it's ancestors. I have no experience withtwistedso don't know how to overcome this. Maybe there are some guides on that somewhere on the internet?klass.__dict__[methodName]withgetattr(klass, methodname)inthreadable.py. And most likely it won't work. And it's a patch to twisted. And it probably breaks something outside that particular use case. So, please try it as a proof of concept and if it really works - suggest as a patch to twisted via ailing lists or ticket tracker or whatever they use to leverage development. And don't forget to post something here so I would know if it helped :)threadable.pyand will add the define has stated in my own subclass. As it is the easiest way :)NDailyLogFile.synchronizedto include only methods that you want to be overridden and to be syncrhonized i.e., don't overwritewritemethod. I'm assuming that parent methods remain synchronized and the lock is the same.