Skip to content

Commit 71c995c

Browse files
committed
Applied minor change
1 parent 0956170 commit 71c995c

4 files changed

Lines changed: 7 additions & 6 deletions

File tree

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
fail-fast: false
3333
matrix:
3434
os: [ubuntu-latest]
35-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
35+
python-version: ["3.10", "3.11", "3.12", "3.13"]
3636
env:
3737
package: ${{ needs.prepare.outputs.package }}
3838
steps:

src/exeplot/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.4
1+
0.5.5

src/exeplot/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def ngrams_counts(byte_obj: bytes | object, n: int = 1, step: int = 1) -> list[t
5050
a = np.frombuffer(data := byte_obj if isinstance(byte_obj, bytes) else byte_obj.bytes, dtype=np.uint8)
5151
l = a.size
5252
if l < n:
53-
return {}
53+
return []
5454
if n == 1:
5555
counts = {b.to_bytes(1, "big"): int(c) for b, c in \
5656
enumerate(np.bincount(np.frombuffer(data, dtype=np.uint8)))}
@@ -68,7 +68,7 @@ def ngrams_counts(byte_obj: bytes | object, n: int = 1, step: int = 1) -> list[t
6868
if n not in byte_obj._ngram_counts_cache.keys():
6969
byte_obj._ngram_counts_cache[n] = counts
7070
return byte_obj._ngram_counts_cache[n]
71-
raise TypeError("Bad input type ; should be a byte sequence or object")
71+
raise TypeError(f"Bad input type ; should be a byte sequence or object (got '{type(byte_obj)}')")
7272

7373

7474
def ngrams_distribution(byte_obj: bytes | object, n: int = 1, step: int = 1, n_most_common: Optional[int] = None,
@@ -83,7 +83,8 @@ def ngrams_distribution(byte_obj: bytes | object, n: int = 1, step: int = 1, n_m
8383
:param exclude: list of specific n-grams to be excluded, no exclusion by default
8484
:return: list of n_most_common (n-gram, count) pairs
8585
"""
86-
c = ngrams_counts(byte_obj, n, step)
86+
if len(c := ngrams_counts(byte_obj, n, step)) == 0:
87+
return []
8788
r = c[:len(c) if n_most_common is None else n_most_common + n_exclude_top + len(exclude or [])]
8889
if exclude is not None:
8990
r = [(ngram, count) for ngram, count in r if ngram not in exclude]

tests/test_others.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_ngrams_functions(self):
2323
for n in [0, 4]:
2424
self.assertRaises(ValueError, ngrams_counts, b"abc", n=n)
2525
self.assertRaises(ValueError, ngrams_counts, b"abc", step=-1)
26-
self.assertEqual(ngrams_counts(b"a", n=2), {})
26+
self.assertEqual(ngrams_counts(b"a", n=2), [])
2727
self.assertTrue(isinstance(ngrams_counts(seq := b"\x00" * 4 + os.urandom(120) + b"\xff" * 4), list))
2828
self.assertTrue(isinstance(ngrams_counts(seq := b"\x00" * 4 + os.urandom(120) + b"\xff" * 4, n=2), list))
2929
class Test:

0 commit comments

Comments
 (0)