From 8a33c18ff18b23d13a179e3dca58328e9e4bf698 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Wed, 11 Jun 2025 09:45:19 -0400 Subject: [PATCH 1/4] Perform floor division when explicitly converting to bytes --- README.rst | 19 ++++++++++++------- binary/core.py | 7 +++++-- tests/test_core.py | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index e726aa0..d089983 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,7 @@ binary :target: https://pypi.org/project/binary :alt: Latest PyPI version -.. image:: https://github.com/ofek/binary/workflows/test/badge.svg +.. image:: https://github.com/ofek/binary/actions/workflows/test.yml/badge.svg :target: https://github.com/ofek/binary/actions/workflows/test.yml :alt: GitHub Actions @@ -17,10 +17,14 @@ binary :target: https://pypi.org/project/binary :alt: Supported Python versions -.. image:: https://img.shields.io/pypi/l/binary.svg?style=flat-square - :target: https://choosealicense.com/licenses +.. image:: https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-9400d3.svg + :target: https://spdx.org/licenses/ :alt: License +.. image:: https://img.shields.io/github/sponsors/ofek?logo=GitHub%20Sponsors&style=social + :target: https://github.com/sponsors/ofek + :alt: GitHub sponsors + ----- ``binary`` provides a bug-free and easy way to convert between and within @@ -32,10 +36,6 @@ binary (`IEC`_) and decimal (`SI`_) units. Installation ------------ -``binary`` is distributed on `PyPI `_ as a universal -wheel and is available on Linux/macOS and Windows and supports -Python 2.7/3.5+ and PyPy. - .. code-block:: bash $ pip install binary @@ -163,6 +163,11 @@ Changelog Important changes are emphasized. +1.0.2 +^^^^^ + +- Perform floor division when explicitly converting to bytes + 1.0.1 ^^^^^ diff --git a/binary/core.py b/binary/core.py index 8cb48c1..7819f1e 100644 --- a/binary/core.py +++ b/binary/core.py @@ -165,9 +165,12 @@ def convert_units( if to: try: - return b / to, PREFIXES[to] + if to == BYTE: + return b // to, PREFIXES[to] + else: + return b / to, PREFIXES[to] except KeyError: - raise ValueError(f'{to} is not a valid binary unit.') + raise ValueError(f'{to} is not a valid unit.') if unit in BINARY_PREFIXES and not si: if b < KIBIBYTE: diff --git a/tests/test_core.py b/tests/test_core.py index fdbdecc..49f8b88 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -173,8 +173,8 @@ def test_yottabyte(self) -> None: class TestConvertFloatExact: def test_byte(self) -> None: - assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392.64'), 'B') - assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000.00'), 'B') + assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392'), 'B') + assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000'), 'B') def test_kibibyte(self) -> None: assert convert_units(3.14, bunits.YB, bunits.KB, exact=True) == (Decimal('3707057689052671492751.36'), 'KiB') From d48d91bed2f1dc66b6d917b1a0b1a064be7d89b4 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Wed, 11 Jun 2025 09:48:21 -0400 Subject: [PATCH 2/4] . --- README.rst | 5 ----- tests/test_core.py | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index d089983..d02a8bb 100644 --- a/README.rst +++ b/README.rst @@ -163,11 +163,6 @@ Changelog Important changes are emphasized. -1.0.2 -^^^^^ - -- Perform floor division when explicitly converting to bytes - 1.0.1 ^^^^^ diff --git a/tests/test_core.py b/tests/test_core.py index 49f8b88..c12a059 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -119,8 +119,8 @@ def test_yottabyte(self) -> None: class TestConvert: def test_byte(self) -> None: - assert convert_units(1, bunits.YB, bunits.B) == (bunits.YB / 1, 'B') - assert convert_units(1, dunits.YB, dunits.B) == (dunits.YB / 1, 'B') + assert convert_units(1, bunits.YB, bunits.B) == (bunits.YB // 1, 'B') + assert convert_units(1, dunits.YB, dunits.B) == (dunits.YB // 1, 'B') def test_kibibyte(self) -> None: assert convert_units(1, bunits.YB, bunits.KB) == (bunits.YB / 1024 ** 1, 'KiB') From 680ec595e448bf668d53e9d3e17c3be04bb8861a Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Wed, 11 Jun 2025 09:49:17 -0400 Subject: [PATCH 3/4] Update test.yml --- .github/workflows/test.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 48f07d2..5352780 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,12 @@ name: Test on: - - push - - pull_request + push: + branches: + - master + pull_request: + branches: + - master jobs: build: From 6232b2f151b9e79b99de0922ba8f4e328667165f Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Wed, 11 Jun 2025 09:51:44 -0400 Subject: [PATCH 4/4] Update core.py --- binary/core.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/binary/core.py b/binary/core.py index 7819f1e..c414094 100644 --- a/binary/core.py +++ b/binary/core.py @@ -165,10 +165,7 @@ def convert_units( if to: try: - if to == BYTE: - return b // to, PREFIXES[to] - else: - return b / to, PREFIXES[to] + return b // to if to == BYTE else b / to, PREFIXES[to] except KeyError: raise ValueError(f'{to} is not a valid unit.')