Skip to content

Multidict #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ install:

script:
- make lint
- make test
- # test with and without optional dependencies then combine coverage
- make test && mv .coverage .coverage.extra
- pip uninstall -y multidict
- make test && mv .coverage .coverage.no-extra
- coverage combine
- make docs
- ./tests/check_tag.py

Expand Down
14 changes: 12 additions & 2 deletions devtools/prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
else:
pyg_lexer, pyg_formatter = PythonLexer(), Terminal256Formatter(style='vim')

try:
from multidict import MultiDict
except ImportError:
MultiDict = None

__all__ = 'PrettyFormat', 'pformat', 'pprint'

PARENTHESES_LOOKUP = [
Expand Down Expand Up @@ -50,13 +55,15 @@ def __init__(self,
self._width = width
self._type_lookup = [
(dict, self._format_dict),
(str, self._format_str_bytes),
(bytes, self._format_str_bytes),
((str, bytes), self._format_str_bytes),
(tuple, self._format_tuples),
((list, set, frozenset), self._format_list_like),
(Generator, self._format_generators),
]

if MultiDict:
self._type_lookup.append((MultiDict, self._format_dict))

def __call__(self, value: Any, *, indent: int = 0, indent_first: bool = False, highlight: bool = False):
self._stream = io.StringIO()
self._format(value, indent_current=indent, indent_first=indent_first)
Expand Down Expand Up @@ -86,6 +93,9 @@ def _format_dict(self, value: dict, value_repr: str, indent_current: int, indent
if isinstance(value, OrderedDict):
open_, split_, after_, close_ = 'OrderedDict([\n', ', ', '),\n', '])'
before_ += '('
elif MultiDict and isinstance(value, MultiDict):
open_, close_ = '<{}(\n'.format(value.__class__.__name__), ')>'

self._stream.write(open_)
for k, v in value.items():
self._stream.write(before_)
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pytest-mock==1.10.0
pytest-sugar==0.9.2
pytest-toolbox==0.4
numpy # pyup: ignore
multidict # pyup: ignore
31 changes: 31 additions & 0 deletions tests/test_prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from devtools.ansi import strip_ansi
from devtools.prettier import PrettyFormat, env_true, pformat, pprint

try:
from multidict import CIMultiDict, MultiDict
except ImportError:
CIMultiDict = None
MultiDict = None


def test_dict():
v = pformat({1: 2, 3: 4})
Expand Down Expand Up @@ -249,3 +255,28 @@ def test_call_args_py354():
def test_env_true():
assert env_true('PATH') is False
assert env_true('DOES_NOT_EXIST') is None


@pytest.mark.skipif(MultiDict is None, reason='MultiDict not installed')
def test_multidict():
d = MultiDict({'a': 1, 'b': 2})
d.add('b', 3)
v = pformat(d)
assert set(v.split('\n')) == {
"<MultiDict(",
" 'a': 1,",
" 'b': 2,",
" 'b': 3,",
")>",
}


@pytest.mark.skipif(CIMultiDict is None, reason='CIMultiDict not installed')
def test_cimultidict():
v = pformat(CIMultiDict({'a': 1, 'b': 2}))
assert set(v.split('\n')) == {
"<CIMultiDict(",
" 'a': 1,",
" 'b': 2,",
")>",
}
10 changes: 5 additions & 5 deletions tests/test_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ def test_unfinished():
def test_multiple_not_verbose():
f = io.StringIO()
t = Timer(file=f)
for i in [0.001, 0.002, 0.003]:
for i in [0.01, 0.02, 0.03]:
with t(verbose=False):
sleep(i)
t.summary(True)
v = f.getvalue()
assert v == (
' 0.001s elapsed\n'
' 0.002s elapsed\n'
' 0.003s elapsed\n'
'3 times: mean=0.002s stdev=0.001s min=0.001s max=0.003s\n'
' 0.010s elapsed\n'
' 0.020s elapsed\n'
' 0.030s elapsed\n'
'3 times: mean=0.020s stdev=0.010s min=0.010s max=0.030s\n'
)


Expand Down