electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 30ffb3d4dca5a0f42473d8e2bd065049d5943e50
parent 1ebfcc0f364ecb806c0ce736a76de8a3256748fd
Author: SomberNight <somber.night@protonmail.com>
Date:   Sun, 26 May 2019 04:10:32 +0200

util: add function "chunks"

taken from ElectrumX
https://github.com/kyuupichan/electrumx/blob/67111a3c4c812d0550fdba013ac886ad824689df/electrumx/lib/util.py#L149

Diffstat:
Melectrum/tests/test_util.py | 8+++++++-
Melectrum/util.py | 8++++++++
2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py @@ -1,7 +1,7 @@ from decimal import Decimal from electrum.util import (format_satoshis, format_fee_satoshis, parse_URI, - is_hash256_str) + is_hash256_str, chunks) from . import SequentialTestCase @@ -104,3 +104,9 @@ class TestUtil(SequentialTestCase): self.assertFalse(is_hash256_str('qweqwe')) self.assertFalse(is_hash256_str(None)) self.assertFalse(is_hash256_str(7)) + + def test_chunks(self): + self.assertEqual([[1, 2], [3, 4], [5]], + list(chunks([1, 2, 3, 4, 5], 2))) + with self.assertRaises(ValueError): + list(chunks([1, 2, 3], 0)) diff --git a/electrum/util.py b/electrum/util.py @@ -520,6 +520,14 @@ def is_non_negative_integer(val) -> bool: return False +def chunks(items, size: int): + """Break up items, an iterable, into chunks of length size.""" + if size < 1: + raise ValueError(f"size must be positive, not {repr(size)}") + for i in range(0, len(items), size): + yield items[i: i + size] + + def format_satoshis_plain(x, decimal_point = 8): """Display a satoshi amount scaled. Always uses a '.' as a decimal point and has no thousands separator"""