I'm building a function pipeline and I need know how many class instance is opened for indexing each function, also I need to set 'Start' param for init the counter to 0 as it handle run many time the same pipeline without have the count increasing above the limit.
First at all I build a method which is working well with only one pipeline, as the Counter is shared with each other pipeline class
class Foo3():
class Counter:
def __init__(self,func):
self.counter = 0
self.func = func
#Counter.method =method
def __call__(self,*args, **kwds):
self.counter += 1
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
def __init__(self, name):
self.name = name
#self.method = method
self.ainit(self)
self.newinst()
print(self.newinst.counter)
@Counter
def newinst():
pass
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test')
pipe1('test2')
pipe1('test3')
pipe2('test_new')
output
this is test
1
this is test2
2
this is test3
3
this is test_new
4
Now I try to figure out how pass a start method within the Counter Class in the purpose to indicate where pipe line start. But my solution is not working because I try to use @static method in a wrong way
class Foo3():
class Counter:
method = 'regular'
def __init__(self,func,method:str='regular' ):
self.counter = 0
self.func = func
Counter.method =method
def __call__(self,method,*args, **kwds):
if Counter.method == 'Start':
print('count = 0')
self.counter = 0
if Counter.method == 'regular' :
print('+1')
self.counter += 1
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
@Counter
@staticmethod
def newinst(method='regular'):
pass
def __init__(self, name, method:str='regular'):
self.name = name
self.method = method
print(self.ainit(self))
Foo3.newinst(method)
print(self.newinst.counter)
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
If I used a counter Class as solution it's because I want one count per Pipeline
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test', 'Start')
pipe1('test2')
pipe1('test3')
pipe2('test_new', 'Start')
pipe1.getCount()
pipe2.getCount()
output expected :
this is test
1
this is test2
2
this is test3
3
this is test_new
1
3
1
but i get as issue :
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-440-1710b9b56152> in <module>
54
55 if __name__ == "__main__":
---> 56 Foo3('a')
<ipython-input-440-1710b9b56152> in __init__(self, name, method)
43 self.name = name
44 self.method = method
---> 45 Foo3.newinst(method)
46 print(self.newinst.counter)
47
<ipython-input-440-1710b9b56152> in __call__(self, method, *args, **kwds)
29 print('+1')
30 self.counter += 1
---> 31 return self.func(*args, **kwds)
32
33
TypeError: 'staticmethod' object is not callable
I find a solution
class Foo3():
class Counter:
method = 'regular'
def __init__(self,func, method= 'regular'):
self.counter = 0
self.func = func
#print(method)
Counter.method =method
#print(Counter.method)
def __call__(self,*args,method ='regular', **kwds):
if method == 'Start':
self.counter =0
elif method =='regular':
self.counter += 1
else :
raise Exception("this method doesn't exist")
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
@Counter
def newinst(method='regular'):
pass
def __init__(self, name, method= 'regular'):
self.name = name
#self.method = method
self.ainit(self)
self.newinst(method=method)
print(self.newinst.counter)
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
output
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test', 'Start')
pipe1('test2')
pipe1('test3')
pipe2('test_new','Start')
this is test
0
this is test2
1
this is test3
2
this is test_new
0
My error came from my weak knowledge about decorator
and inner Class
, in my exemple , the __init__
fonction of Counter is instanciate even before I build a pipeline, then I didn't figure out how pass 'Start' in parameter as I set it in the init function.
Now my other problem - as @MisterMiyagi mentionned - come from the fact that my getCount()
function shouldn't work and I better to keep out the Counter class, but I didn't still figuring out how open one Counter
instance per pipeline
with inheritance of Foo3
without specify it in the `pipline Class global' parameter.