Skip to content

Commit

Permalink
grouper and ceiling division (#3)
Browse files Browse the repository at this point in the history
grouper and ceiling division
  • Loading branch information
wimglenn committed Jan 18, 2018
1 parent e9a6031 commit 03913af
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
9 changes: 8 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
universal = 1

[metadata]
license_file=LICENSE
license_file = LICENSE

[coverage:run]
branch = True
parallel = True

[tool:pytest]
addopts =
--verbose
--cov=wimpy
--cov-report=html
--no-cov-on-fail
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='wimpy',
version='0.2',
version='0.3',
description='Anti-copy-pasta',
url='https://github.com/wimglenn/wimpy',
author='Wim Glenn',
Expand Down
30 changes: 26 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import pytest

from wimpy.util import cached_property
from wimpy.util import strip_prefix
from wimpy.util import strip_suffix
from wimpy.util import working_directory
from wimpy import cached_property
from wimpy import ceiling_division
from wimpy import grouper
from wimpy import strip_prefix
from wimpy import strip_suffix
from wimpy import working_directory


@pytest.mark.parametrize('in_,pre,out', [
Expand Down Expand Up @@ -54,3 +56,23 @@ def test_cached_property():
del a.prop # .. but you can't delete the descriptor
assert isinstance(a.prop, float)
assert isinstance(A.prop, cached_property)


@pytest.mark.parametrize('numerator,denominator,result', [
(51, 10, 6),
(50, 10, 5),
(49, 10, 5),
(-49, 10, -4),
])
def test_ceiling_div(numerator, denominator, result):
assert ceiling_division(numerator, denominator) == result



@pytest.mark.parametrize('iterable,n,fillvalue,result', [
(range(6), 2, None, [(0, 1), (2, 3), (4, 5)]),
(range(5), 2, None, [(0, 1), (2, 3), (4, None)]),
(range(5), 2, 123, [(0, 1), (2, 3), (4, 123)]),
])
def test_grouper(iterable, n, fillvalue, result):
assert list(grouper(iterable, n, fillvalue)) == result
4 changes: 4 additions & 0 deletions wimpy/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
17 changes: 16 additions & 1 deletion wimpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from contextlib import contextmanager
from functools import update_wrapper

from wimpy.compat import zip_longest

__all__ = ['cached_property', 'working_directory', 'strip_prefix', 'strip_suffix']

__all__ = ['cached_property', 'working_directory', 'strip_prefix', 'strip_suffix', 'ceiling_division', 'grouper']


class cached_property(object):
Expand Down Expand Up @@ -45,3 +47,16 @@ def strip_suffix(s, suffix):
if s.endswith(suffix):
return s[:len(s) - len(suffix)]
return s


def ceiling_division(numerator, denominator):
"""Divide and round up"""
# Implementation relies on Python's // operator using floor division (round down)
# This might surprise C users who expect rounding towards zero
return -(-numerator//denominator)


def grouper(iterable, n, fillvalue=None):
"""Yield successive non-overlapping chunks of size n from iterable"""
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)

0 comments on commit 03913af

Please sign in to comment.