Skip to content

Commit

Permalink
1.2.3
Browse files Browse the repository at this point in the history
Removed debug print in __main__.py
Fixed docstring spacing in functimer.py
Updated tests to match new file name
  • Loading branch information
EJEmmett committed May 31, 2021
1 parent 9e8e0ed commit c253f32
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ python:
- "3.9"
before_install:
- pip install poetry
install:
- pip install tox-travis
- pip install codecov
install:
- poetry install
script:
- poetry run pre-commit run --all-files
Expand Down
13 changes: 6 additions & 7 deletions functimer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import argparse
import builtins
import sys
from argparse import ArgumentParser, RawTextHelpFormatter
from contextlib import contextmanager
from importlib import import_module
from re import findall, sub
from types import FunctionType, ModuleType
from typing import Dict, Union

from functimer import TimingException, Unit, timed
from functimer.classes import TimedResult
from functimer.classes import Result

unit_map: Dict[str, Unit] = {
"ns": Unit.nanosecond,
Expand Down Expand Up @@ -41,7 +41,6 @@ def create_local(func_chain: str, **kwargs) -> Dict[str, Union[ModuleType, Funct
try:
method = getattr(builtins, func_chain)
local = {method.__name__: timed(method, **kwargs, enable_return=True)}
print(type(local[method.__name__]))
yield local
except AttributeError:
module, *subattrs, method = func_chain.split(".")
Expand All @@ -55,7 +54,7 @@ def create_local(func_chain: str, **kwargs) -> Dict[str, Union[ModuleType, Funct
setattr(module, method, store_method)


def exec_func(func: str, **kwargs) -> TimedResult:
def exec_func(func: str, **kwargs) -> Result:
if "(" not in func and ")" not in func:
raise TimingException("Malformed input.")

Expand All @@ -71,10 +70,10 @@ def exec_func(func: str, **kwargs) -> TimedResult:


def cli():
parser = argparse.ArgumentParser(
parser = ArgumentParser(
prog="functimer",
description="A decorator/wrapper package to time a given function.",
formatter_class=argparse.RawTextHelpFormatter,
formatter_class=RawTextHelpFormatter,
)

parser.add_argument(
Expand Down Expand Up @@ -104,7 +103,7 @@ def cli():
"--unit",
type=parse_unit,
default=Unit.microsecond,
help=f"Set the resulting unit, defaults to microsecond."
help=f"Set the resulting unit, defaults to microsecond. "
f"({', '.join(list(unit_map.keys()))})",
)

Expand Down
8 changes: 4 additions & 4 deletions functimer/functimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def timed(
"""Times wrapped function and returns string formatted object.
Args:
func: The function to be wrapped. (None if decorated)
func: The function to be wrapped. (None if decorated)
enabled: Disables timing of wrapped func.
enabled: Disables timing of wrapped func.
unit: The scientific unit to format runtime.
unit: The scientific unit to format the returned value.
estimate: Toggle returning a rough estimation of total timer runtime over number
executions based on the runtime of one execution.
Expand All @@ -51,7 +51,7 @@ def timed(
Raises:
ValueError:
If number is less than zero.
If number is less than one.
"""

if number < 1:
Expand Down
2 changes: 1 addition & 1 deletion functimer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def flush(self):
pass

def read(self, *args):
raise TimingException("Can't read from stdin while timing!")
raise TimingException("Can't read from stdin while timing!") from None

readline = read
readlines = read
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "functimer"
version = "1.2.2"
version = "1.2.3"
description = "A decorator/wrapper package to time a given function."
authors = ["Edward Emmett <edemms12@gmail.com>"]
maintainers = ["Edward Emmett <edemms12@gmail.com>"]
Expand Down Expand Up @@ -30,7 +30,7 @@ python = "^3.6"

[tool.poetry.dev-dependencies]
pre-commit = [
{version = "2.12.1", python = "^3.6.1"},
{version = "2.13.0", python = "^3.6.1"},
{version = "*", python = "^3.6"}
]
tox = "^3.23.1"
Expand Down
6 changes: 4 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def test_exec_func_exception(_input, error):
)
def test_exec_func(monkeypatch, _input, expected):
with monkeypatch.context() as m:
m.setattr(functimer.timer.timeit, "timeit", lambda *args, **kwargs: (1, expected))
m.setattr(
functimer.functimer.timeit, "timeit", lambda *args, **kwargs: (1, expected)
)
runtime, ret = main.exec_func(_input)
assert ret == expected

Expand Down Expand Up @@ -80,7 +82,7 @@ def test_exec_func_inner_exception(_input, error):
def test_cli(monkeypatch, capsys, _input, expected):
sys.argv[1:] = _input
with monkeypatch.context() as m:
m.setattr(functimer.timer.timeit, "timeit", lambda *args, **kwargs: (1, 6))
m.setattr(functimer.functimer.timeit, "timeit", lambda *args, **kwargs: (1, 6))
main.cli()
out = capsys.readouterr().out
assert expected in out
Expand Down
9 changes: 7 additions & 2 deletions tests/test_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def func_input():
@pytest.fixture
def mock_timed(monkeypatch):
with monkeypatch.context() as m:
m.setattr(functimer.timer.timeit, "timeit", lambda *args, **kwargs: (1, func()))
m.setattr(
functimer.functimer.timeit, "timeit", lambda *args, **kwargs: (1, func())
)
yield functimer.timed


Expand All @@ -31,6 +33,7 @@ def mock_timed(monkeypatch):
],
)
def test_timed(mock_timed, kwargs, expected):
print(mock_timed(func, **kwargs)())
assert str(expected) in str(mock_timed(func, **kwargs)())


Expand All @@ -51,7 +54,9 @@ def test_timed_stdout(capsys, mock_timed):
def test_timed_error(monkeypatch, _input, kwargs, error):
with monkeypatch.context() as m:
m.setattr(
functimer.timer.timeit, "timeit", lambda *args, **kwargs: (1, func_input())
functimer.functimer.timeit,
"timeit",
lambda *args, **kwargs: (1, func_input()),
)
with pytest.raises(error):
functimer.timed(_input, **kwargs)()

0 comments on commit c253f32

Please sign in to comment.