Skip to content

Commit 2bd31c9

Browse files
committed
update readme - more consistent docstring
1 parent b3a0b4b commit 2bd31c9

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Once you have installed the package, you may want to test it:
7878
.........................................................................
7979
................................................................
8080
----------------------------------------------------------------------
81-
Ran 582 tests in 0.165s
81+
Ran 587 tests in 0.162s
8282
8383
OK
8484
@@ -969,6 +969,13 @@ This sub-module was added in version 1.2.
969969
iteration is stopped as soon as one mismatch is found.
970970

971971

972+
``sum_indices(a, /)`` -> int
973+
Return sum of indices of all active bits in bitarray ``a``.
974+
This is equivalent to ``sum(i for i, v in enumerate(a) if v)``.
975+
976+
New in version 3.6
977+
978+
972979
``urandom(n, /, endian=None)`` -> bitarray
973980
Return random bitarray of length ``n`` (uses ``os.urandom()``).
974981

@@ -998,7 +1005,7 @@ This sub-module was added in version 1.2.
9981005
``xor_indices(a, /)`` -> int
9991006
Return xor reduced indices of all active bits in bitarray ``a``.
10001007
This is essentially equivalent to
1001-
``reduce(operator.xor, [i for i, v in enumerate(a) if v])``.
1008+
``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
10021009

10031010
New in version 3.2
10041011

bitarray/_util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ PyDoc_STRVAR(sum_indices_doc,
333333
"sum_indices(a, /) -> int\n\
334334
\n\
335335
Return sum of indices of all active bits in bitarray `a`.\n\
336-
This is equivalent to `sum(i for i in range(len(a)) if a[i])`.");
336+
This is equivalent to `sum(i for i, v in enumerate(a) if v)`.");
337337

338338

339339
static PyObject *
@@ -381,7 +381,7 @@ PyDoc_STRVAR(xor_indices_doc,
381381
\n\
382382
Return xor reduced indices of all active bits in bitarray `a`.\n\
383383
This is essentially equivalent to\n\
384-
`reduce(operator.xor, [i for i, v in enumerate(a) if v])`.");
384+
`reduce(operator.xor, (i for i, v in enumerate(a) if v))`.");
385385

386386
/* --------------------------- binary functions ------------------------ */
387387

bitarray/test_util.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ def test_large(self):
10661066
def test_random(self):
10671067
for a in self.randombitarrays():
10681068
res = sum_indices(a)
1069-
self.assertEqual(res, sum(i for i in range(len(a)) if a[i]))
1069+
self.assertEqual(res, sum(i for i, v in enumerate(a) if v))
10701070
self.assertEqual(res, sum(a.search(1)))
10711071

10721072
# ---------------------------------------------------------------------------
@@ -1081,10 +1081,11 @@ def test_explicit(self):
10811081
self.assertEqual(xor_indices(a), r)
10821082

10831083
def test_wrong_args(self):
1084-
self.assertRaises(TypeError, parity, '')
1085-
self.assertRaises(TypeError, parity, 1)
1086-
self.assertRaises(TypeError, parity)
1087-
self.assertRaises(TypeError, parity, bitarray("110"), 1)
1084+
X = xor_indices
1085+
self.assertRaises(TypeError, X, '')
1086+
self.assertRaises(TypeError, X, 1)
1087+
self.assertRaises(TypeError, X)
1088+
self.assertRaises(TypeError, X, bitarray("110"), 1)
10881089

10891090
def test_ones(self):
10901091
# OEIS A003815
@@ -1101,13 +1102,13 @@ def test_ones(self):
11011102

11021103
def test_random(self):
11031104
for a in self.randombitarrays():
1104-
indices = [i for i, v in enumerate(a) if v]
1105-
if len(indices) == 0:
1105+
cnt = a.count()
1106+
if cnt == 0:
11061107
c = 0
1107-
elif len(indices) == 1:
1108-
c = indices[0]
1108+
elif cnt == 1:
1109+
c = a.index(1)
11091110
else:
1110-
c = reduce(operator.xor, indices)
1111+
c = reduce(operator.xor, (i for i, v in enumerate(a) if v))
11111112
self.assertEqual(xor_indices(a), c)
11121113

11131114
def test_flips(self):

examples/test_random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_sums(self):
223223
# and xor_indices() for large bitarray
224224
n = 10_000_000
225225
a = urandom(n, choice(["little", "big"]))
226-
indices = [i for i in range(n) if a[i]]
226+
indices = [i for i, v in enumerate(a) if v]
227227
self.assertEqual(sum_indices(a), sum(indices))
228228
self.assertEqual(xor_indices(a), reduce(operator.xor, indices))
229229

0 commit comments

Comments
 (0)