Skip to content

Commit adc81c1

Browse files
authored
Perform floor division when explicitly converting to bytes (#13)
1 parent df2a9be commit adc81c1

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
name: Test
22

33
on:
4-
- push
5-
- pull_request
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
610

711
jobs:
812
build:

README.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ binary
55
:target: https://pypi.org/project/binary
66
:alt: Latest PyPI version
77

8-
.. image:: https://github.com/ofek/binary/workflows/test/badge.svg
8+
.. image:: https://github.com/ofek/binary/actions/workflows/test.yml/badge.svg
99
:target: https://github.com/ofek/binary/actions/workflows/test.yml
1010
:alt: GitHub Actions
1111

@@ -17,10 +17,14 @@ binary
1717
:target: https://pypi.org/project/binary
1818
:alt: Supported Python versions
1919

20-
.. image:: https://img.shields.io/pypi/l/binary.svg?style=flat-square
21-
:target: https://choosealicense.com/licenses
20+
.. image:: https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-9400d3.svg
21+
:target: https://spdx.org/licenses/
2222
:alt: License
2323

24+
.. image:: https://img.shields.io/github/sponsors/ofek?logo=GitHub%20Sponsors&style=social
25+
:target: https://github.com/sponsors/ofek
26+
:alt: GitHub sponsors
27+
2428
-----
2529

2630
``binary`` provides a bug-free and easy way to convert between and within
@@ -32,10 +36,6 @@ binary (`IEC`_) and decimal (`SI`_) units.
3236
Installation
3337
------------
3438

35-
``binary`` is distributed on `PyPI <https://pypi.org>`_ as a universal
36-
wheel and is available on Linux/macOS and Windows and supports
37-
Python 2.7/3.5+ and PyPy.
38-
3939
.. code-block:: bash
4040
4141
$ pip install binary

binary/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ def convert_units(
165165

166166
if to:
167167
try:
168-
return b / to, PREFIXES[to]
168+
return b // to if to == BYTE else b / to, PREFIXES[to]
169169
except KeyError:
170-
raise ValueError(f'{to} is not a valid binary unit.')
170+
raise ValueError(f'{to} is not a valid unit.')
171171

172172
if unit in BINARY_PREFIXES and not si:
173173
if b < KIBIBYTE:

tests/test_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def test_yottabyte(self) -> None:
119119

120120
class TestConvert:
121121
def test_byte(self) -> None:
122-
assert convert_units(1, bunits.YB, bunits.B) == (bunits.YB / 1, 'B')
123-
assert convert_units(1, dunits.YB, dunits.B) == (dunits.YB / 1, 'B')
122+
assert convert_units(1, bunits.YB, bunits.B) == (bunits.YB // 1, 'B')
123+
assert convert_units(1, dunits.YB, dunits.B) == (dunits.YB // 1, 'B')
124124

125125
def test_kibibyte(self) -> None:
126126
assert convert_units(1, bunits.YB, bunits.KB) == (bunits.YB / 1024 ** 1, 'KiB')
@@ -173,8 +173,8 @@ def test_yottabyte(self) -> None:
173173

174174
class TestConvertFloatExact:
175175
def test_byte(self) -> None:
176-
assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392.64'), 'B')
177-
assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000.00'), 'B')
176+
assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392'), 'B')
177+
assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000'), 'B')
178178

179179
def test_kibibyte(self) -> None:
180180
assert convert_units(3.14, bunits.YB, bunits.KB, exact=True) == (Decimal('3707057689052671492751.36'), 'KiB')

0 commit comments

Comments
 (0)