Merge branch 'new_decorator' into cima_working

This commit is contained in:
Richard Hartmann 2015-01-27 00:44:54 +01:00
commit 7c9323636a
3 changed files with 87 additions and 10 deletions

View file

@ -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)

View file

@ -108,6 +108,9 @@ def remove_ESC_SEQ_from_string(s):
s = s.replace(esc_seq, '')
return s
def len_string_without_ESC(s):
return len(remove_ESC_SEQ_from_string(s))
def humanize_time(secs):
"""convert second in to hh:mm:ss format
@ -604,7 +607,8 @@ class Progress(Loop):
verbose, sigint, sigterm -> see loop class
"""
self._PRE_PREPEND = ESC_RED
self._POST_PREPEND = ESC_BOLD + ESC_GREEN
try:
for c in count:
assert isinstance(c, mp.sharedctypes.Synchronized), "each element of 'count' must be if the type multiprocessing.sharedctypes.Synchronized"
@ -654,17 +658,17 @@ class Progress(Loop):
self.start_time.append(FloatValue(val=time.time()))
if prepend is None:
# no prepend given
self.prepend.append('')
self.prepend.append(self._POST_PREPEND)
else:
try:
# assume list of prepend, (needs to be a sequence)
# except if prepend is an instance of string
# the assert will cause the except to be executed
assert not isinstance(prepend, str)
self.prepend.append(prepend[i])
self.prepend.append(self._PRE_PREPEND + prepend[i]+self._POST_PREPEND)
except:
# list fails -> assume single prepend for all
self.prepend.append(prepend)
self.prepend.append(self._PRE_PREPEND + prepend+self._POST_PREPEND)
self.max_count = max_count # list of multiprocessing value type
self.count = count # list of multiprocessing value type
@ -745,8 +749,8 @@ class Progress(Loop):
"""
call the static method show_stat_wrapper for each process
"""
print(ESC_BOLD + ESC_GREEN, end='')
sys.stdout.flush()
# print(ESC_BOLD, end='')
# sys.stdout.flush()
for i in range(len_):
Progress.show_stat_wrapper(count[i],
last_count[i],
@ -964,7 +968,7 @@ class ProgressBar(Progress):
s1 = "{}{} [{}] [".format(prepend, humanize_time(tet), humanize_speed(speed))
l = len(s1) + len(s3)
l = len_string_without_ESC(s1+s3)
if max_count_value != 0:
@ -1116,7 +1120,7 @@ class ProgressBarCounter(Progress):
s1 = "{} [{}] [".format(humanize_time(tet), humanize_speed(speed))
l = len(s1) + len(s3) + len(s_c)
l = len_string_without_ESC(s1 + s3 + s_c)
l2 = width - l - 1
a = int(l2 * count_value / max_count_value)
@ -1125,7 +1129,7 @@ class ProgressBarCounter(Progress):
s_c = s_c+s1+s2+s3
print(s_c + ' '*(width - len(s_c)))
print(s_c + ' '*(width - len_string_without_ESC(s_c)))
class ProgressSilentDummy(Progress):
def __init__(self, **kwargs):

View file

@ -62,8 +62,34 @@ def test_decorator():
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()