2018-05-10 16:41:13 +00:00
|
|
|
# Date and Time
|
2018-04-22 12:55:40 +00:00
|
|
|
|
|
|
|
Utils for handling date and time.
|
|
|
|
|
2018-05-10 16:41:13 +00:00
|
|
|
## dt.timer
|
|
|
|
```python
|
|
|
|
timer(path)
|
|
|
|
```
|
2018-04-22 12:55:40 +00:00
|
|
|
|
2018-05-10 16:41:13 +00:00
|
|
|
A timer can time how long does calling take as a context manager or decorator.
|
|
|
|
If assign `print_func` with `sys.stdout.write`, `logger.info` and so on,
|
|
|
|
timer will print the spent time.
|
2018-04-22 12:55:40 +00:00
|
|
|
|
2018-05-11 13:54:53 +00:00
|
|
|
```python
|
2018-05-10 16:41:13 +00:00
|
|
|
timeit = timer(print_func=sys.stdout.write)
|
|
|
|
with timeit:
|
|
|
|
foo()
|
2018-04-22 12:55:40 +00:00
|
|
|
|
2018-05-10 16:41:13 +00:00
|
|
|
@timeit
|
|
|
|
def foo():
|
|
|
|
pass
|
2018-05-11 13:54:53 +00:00
|
|
|
```
|
2018-04-22 12:55:40 +00:00
|
|
|
|
2018-05-10 16:41:13 +00:00
|
|
|
`timer.elapsed` contains the total amount of elapsed
|
|
|
|
time of running `foo`.
|
|
|
|
|
|
|
|
```python
|
|
|
|
>>> timeit = timer(print_func=sys.stdout.write)
|
|
|
|
>>> with timeit:
|
|
|
|
... os.getcwd()
|
|
|
|
Spent time: 1.7881393432617188e-05s
|
|
|
|
```
|