mirror of
https://github.com/vale981/jobmanager
synced 2025-03-05 18:01:38 -05:00
new decorator class ProgressBarOverrideCount which follows #12, including a test example
This commit is contained in:
parent
ca3976749e
commit
de29ceedb6
2 changed files with 74 additions and 1 deletions
|
@ -13,7 +13,7 @@ from . import progress
|
|||
|
||||
from .jobmanager import getCountKwargs, validCountKwargs
|
||||
|
||||
__all__ = ["ProgressBar"]
|
||||
__all__ = ["ProgressBar", "ProgressBarOverrideCount"]
|
||||
|
||||
|
||||
class ProgressBar(object):
|
||||
|
@ -191,3 +191,50 @@ def decorate_module_ProgressBar(module, **kwargs):
|
|||
module.__name__, key))
|
||||
|
||||
|
||||
class ProgressBarOverrideCount(ProgressBar):
|
||||
def __call__(self, *args, **kwargs):
|
||||
""" Calls `func` - previously defined in `__init__`.
|
||||
|
||||
same as in ProgressBar class except that the default
|
||||
value `None` of count and max_count will cause
|
||||
count to be set to `UnsignedIntValue(val=0)`
|
||||
and max_count to `UnsignedIntValue(val=1)`.
|
||||
|
||||
So even if the function to be decorated
|
||||
will be called with arguments c = None and m = None
|
||||
the actual call due to the modification of the decorator
|
||||
will be with arguments c = UIV(0) and m = UIV(1).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
*args : list
|
||||
Arguments for `func`.
|
||||
**kwargs : dict
|
||||
Keyword-arguments for `func`.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
see tests/test_decorators.py
|
||||
"""
|
||||
|
||||
# Bind the args and kwds to the argument names of self.func
|
||||
callargs = getcallargs(self.func, *args, **kwargs)
|
||||
|
||||
count = callargs[self.cm[0]]
|
||||
if count is None:
|
||||
count = progress.UnsignedIntValue(val=0)
|
||||
callargs[self.cm[0]] = count
|
||||
|
||||
max_count = callargs[self.cm[1]]
|
||||
if max_count is None:
|
||||
max_count = progress.UnsignedIntValue(val=1)
|
||||
callargs[self.cm[1]] = max_count
|
||||
|
||||
with progress.ProgressBar(count = count,
|
||||
max_count = max_count,
|
||||
prepend = "{} ".format(self.__name__),
|
||||
**self.kwargs) as pb:
|
||||
pb.start()
|
||||
return self.func(**callargs)
|
||||
|
|
@ -61,9 +61,35 @@ def test_decorator():
|
|||
m = progress.UnsignedIntValue(val=100)
|
||||
my_func(c=c, m=m)
|
||||
my_func(c, m)
|
||||
|
||||
|
||||
def my_func_ProgressBarOverrideCount(c = None, m = None):
|
||||
maxVal = 100
|
||||
if m is not None:
|
||||
m.value = maxVal
|
||||
|
||||
for i in range(maxVal):
|
||||
time.sleep(0.03)
|
||||
if c is not None:
|
||||
c.value = i
|
||||
|
||||
|
||||
def test_ProgressBarOverrideCount():
|
||||
print("normal call -> no decoration")
|
||||
my_func_ProgressBarOverrideCount()
|
||||
print("done!")
|
||||
print()
|
||||
|
||||
my_func_ProgressBarOverrideCount_dec = decorators.ProgressBarOverrideCount(my_func_ProgressBarOverrideCount)
|
||||
print("with decorator")
|
||||
my_func_ProgressBarOverrideCount_dec()
|
||||
print("done!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_ProgressBar()
|
||||
test_decorator()
|
||||
test_ProgressBarOverrideCount()
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue