Skip to content

Commit

Permalink
feat: using StringBuilder for strings (#32)
Browse files Browse the repository at this point in the history
* feat: using StringBuilder for strings

* test: stringbuilder creates a different masked array

However this is probably the intended behaviour

* test: encode bytes to a list of integers for testing

* ci: add numpy for testing
  • Loading branch information
zonca authored Mar 5, 2024
1 parent 8b9893e commit 5e71baf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install flake8 pytest numpy
- name: Test
run: |
make test
2 changes: 1 addition & 1 deletion kaitai_struct_compiler
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import BytesToIntListEncoder
2 changes: 1 addition & 1 deletion tests/test_index_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_index_option():
# Element 0 has a different shape because it is a ListOffsetBuilder<NumpyBuilder>
# instead of just NumpyBuilder
expected_array_bank1 = {
"0": np.ma.masked_array(data=[], mask=[], dtype=np.uint8).reshape((1, 0)),
"0": np.ma.masked_array(data=[0], mask=[True], dtype="<U1", fill_value="N/A"),
"1": np.ma.masked_array(data=[0], mask=[True], dtype=np.uint16),
}
expected_array_bank1["2"] = expected_array_bank1["1"]
Expand Down
8 changes: 7 additions & 1 deletion tests/test_scdms_v8.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

import awkward_kaitai

from . import BytesToIntListEncoder


def test_scdms_v8():
reader = awkward_kaitai.Reader("test_artifacts/libscdms_v8.so")
awkward_array = reader.load("example_data/data/scdms_v8.mid")

parsed_array = json.loads(
json.dumps(awkward_array.to_list()[0], cls=BytesToIntListEncoder)
)

with gzip.open("tests/scdms_v8.json.gz", "rb") as f:
expected_content = json.load(f)

# it was list of 1 element, so saved only the element into JSON
assert awkward_array.to_list()[0] == expected_content
assert parsed_array == expected_content
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
import numpy as np


class BytesToIntListEncoder(json.JSONEncoder):
"""Allows encoding of a bytestring to JSON by turning it into
a list of `uint8`"""

def default(self, o):
if isinstance(o, bytes):
return np.frombuffer(o, dtype=np.uint8).tolist()
else:
return super().default(o)

0 comments on commit 5e71baf

Please sign in to comment.