Skip to content

Commit

Permalink
Revert "feat(timeit): add function timing decorator (#118)" (#119)
Browse files Browse the repository at this point in the history
* reverts 506a238.
  • Loading branch information
wpbonelli authored Sep 12, 2023
1 parent 506a238 commit d725ddb
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 79 deletions.
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The `modflow-devtools` package provides a set of tools for developing and testin
md/download.md
md/ostags.md
md/zip.md
md/timeit.md


.. toctree::
Expand Down
17 changes: 0 additions & 17 deletions docs/md/timeit.md

This file was deleted.

37 changes: 0 additions & 37 deletions modflow_devtools/misc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import importlib
import socket
import sys
import time
import traceback
from contextlib import contextmanager
from functools import wraps
from importlib import metadata
from os import PathLike, chdir, environ, getcwd
from os.path import basename, normpath
Expand Down Expand Up @@ -444,38 +442,3 @@ def try_metadata() -> bool:
_has_pkg_cache[pkg] = found

return _has_pkg_cache[pkg]


def timeit(f):
"""
Decorator for estimating runtime of any function.
Prints estimated time to stdout, in milliseconds.
Parameters
----------
f : function
Function to time.
Notes
-----
Adapted from https://stackoverflow.com/a/27737385/6514033.
Returns
-------
function
The decorated function.
"""

@wraps(f)
def timed(*args, **kw):
ts = time.time()
res = f(*args, **kw)
te = time.time()
if "log_time" in kw:
name = kw.get("log_name", f.__name__.upper())
kw["log_time"][name] = int((te - ts) * 1000)
else:
print(f"{f.__name__} took {(te - ts) * 1000:.2f} ms")
return res

return timed
24 changes: 0 additions & 24 deletions modflow_devtools/test/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import re
import shutil
from os import environ
from pathlib import Path
from pprint import pprint
from time import sleep
from typing import List

import pytest
Expand All @@ -17,7 +15,6 @@
has_pkg,
set_dir,
set_env,
timeit,
)


Expand Down Expand Up @@ -286,24 +283,3 @@ def test_has_pkg(virtualenv):
).strip()
== exp
)


def test_timeit1(capfd):
def sleep1():
sleep(0.001)

timeit(sleep1)()
cap = capfd.readouterr()
print(cap.out)
assert re.match(r"sleep1 took \d+\.\d+ ms", cap.out)


def test_timeit2(capfd):
@timeit
def sleep1dec():
sleep(0.001)

sleep1dec()
cap = capfd.readouterr()
print(cap.out)
assert re.match(r"sleep1dec took \d+\.\d+ ms", cap.out)

0 comments on commit d725ddb

Please sign in to comment.