Skip to content

Commit d9d5b72

Browse files
committed
修改Timeit名称
1 parent 69914b5 commit d9d5b72

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
license='Apache License',
1515
url='https://github.com/Tongjilibo/torch4keras',
1616
author='Tongjilibo',
17-
install_requires=['numpy'],
17+
install_requires=['numpy', 'packaging'],
1818
packages=find_packages()
1919
)

test/test_time.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from torch4keras.snippets import timeit, Timeit, Timeit2
1+
from torch4keras.snippets import timeit, TimeitContextManager, TimeitLogger
22
import time
33

44

@@ -10,27 +10,27 @@ def func(n=10):
1010
func()
1111

1212
# 上下文管理器 - 统计累计耗时
13-
with Timeit() as ti:
13+
with TimeitContextManager() as ti:
1414
for i in range(10):
1515
time.sleep(0.1)
1616
ti.lap(name=i, reset=False) # 统计累计耗时
1717

1818
# 上下文管理器 - 统计每段速度
19-
with Timeit() as ti:
19+
with TimeitContextManager() as ti:
2020
for i in range(10):
2121
time.sleep(0.1)
2222
ti.lap(count=10, name=i, reset=True)
2323
ti(10) # 统计速度
2424

2525

2626
# 上下文管理器 - 统计速度
27-
with Timeit() as ti:
27+
with TimeitContextManager() as ti:
2828
for i in range(10):
2929
time.sleep(0.1)
3030
ti.lap(name=i, reset=True)
3131
ti(10) # 统计速度
3232

33-
ti = Timeit2()
33+
ti = TimeitLogger()
3434
for i in range(10):
3535
time.sleep(0.1)
3636
ti.lap(name=i)

torch4keras/snippets/log.py

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def log_level(string:str, level:Union[int, str]=0, verbose:int=1):
5454
info_level_prefix = log_level
5555

5656

57+
def log_free(string:str, prefix:str, string_color:str=None, prefix_color:str='yellow', verbose:int=1):
58+
'''自由log'''
59+
if string_color:
60+
string = colorful(string, color=string_color)
61+
if prefix:
62+
prefix = colorful(prefix, color=prefix_color)
63+
string = prefix + ' ' + string
64+
65+
if verbose != 0:
66+
print(string)
67+
return string
68+
69+
5770
def log_info(string:str, verbose:int=1):
5871
'''[INFO]: message, 绿色前缀'''
5972
res = colorful('[INFO]', color='green') + ' ' + string.strip()

torch4keras/snippets/misc.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,15 @@ def docstring_decorator(fn):
644644
fn.__doc__ = (fn.__doc__ if fn.__doc__ is not None else "") + "".join(docstr)
645645
return fn
646646

647-
return docstring_decorator
647+
return docstring_decorator
648+
649+
650+
class NoopContextManager:
651+
'''无意义的上下文管理器占位'''
652+
def __enter__(self):
653+
# 不执行任何操作
654+
return None
655+
656+
def __exit__(self, exc_type, exc_val, exc_tb):
657+
# 不执行任何操作,也不抑制异常
658+
return False

torch4keras/snippets/monitor.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def warpper(*args, **kwargs):
8383
return warpper
8484

8585

86-
class Timeit:
86+
class TimeitContextManager:
8787
'''上下文管理器, 记录耗时/平均耗时
8888
8989
Examples:
9090
```python
91-
>>> from torch4keras.snippets import Timeit
92-
>>> with Timeit() as ti:
91+
>>> from torch4keras.snippets import TimeitContextManager
92+
>>> with TimeitContextManager() as ti:
9393
... for i in range(10):
9494
... time.sleep(0.1)
9595
... # ti.lap(name=i, reset=False) # 统计累计耗时
@@ -152,12 +152,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
152152
self.lap(name='Total', reset=False)
153153

154154

155-
class Timeit2:
155+
class TimeitLogger:
156156
'''记录耗时
157157
158158
Examples:
159159
```python
160-
>>> ti = Timeit2()
160+
>>> ti = TimeitLogger()
161161
>>> for i in range(10):
162162
... time.sleep(0.1)
163163
... ti.lap(name=i)

0 commit comments

Comments
 (0)