Skip to content

Commit

Permalink
add binary file path as a parameter in main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasviGoyal committed Oct 2, 2023
1 parent de82a75 commit f750435
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Steps for reproducing the environment:
git clone --recursive https://github.com/ManasviGoyal/kaitai_awkward_runtime.git
```

2. Run CMake
2. Upload KSY file path
```
cmake -B build
export KSY_FILE=schemas/animal.ksy
```

3. Install the library
Expand All @@ -23,5 +23,5 @@ pip install .
4. Print the returned `ak.Array`
```python
from awkward_kaitai import create_awkward_array
print(create_awkward_array())
print(create_awkward_array("../data/animal.raw"))
```
41 changes: 3 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,15 @@ py::object snapshot_builder(const T &builder) {
* Create array, and return its snapshot
* @return pyobject of Awkward Array
*/
py::object create_awkward_array() {
UserDefinedMap fields_map({
{Field::one, "one"},
{Field::two, "two"}
});

RecordBuilder<
RecordField<Field::one, NumpyBuilder<double>>,
RecordField<Field::two, ListOffsetBuilder<int64_t,
NumpyBuilder<int32_t>>>
> builder(fields_map);

auto &one_builder = builder.content<Field::one>();
auto &two_builder = builder.content<Field::two>();

one_builder.append(1.1);

auto &two_subbuilder = two_builder.begin_list();
two_subbuilder.append(1);
two_builder.end_list();

one_builder.append(2.2);

two_builder.begin_list();
two_subbuilder.append(1);
two_subbuilder.append(2);
two_builder.end_list();

one_builder.append(3.3);
size_t data_size = 3;
int32_t data[3] = {1, 2, 3};

two_builder.begin_list();
two_subbuilder.extend(data, data_size);
two_builder.end_list();

std::ifstream infile("../data/animal.raw", std::ifstream::binary);
py::object create_awkward_array(std::string binary_data) {
std::ifstream infile(binary_data, std::ifstream::binary);
kaitai::kstream ks(&infile);
animal_t zoo = animal_t(&ks);
return snapshot_builder(zoo.animal_builder);
}


PYBIND11_MODULE(awkward_kaitai, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.doc() = "pybind11 plugin"; // optional module docstring
m.def("create_awkward_array", &create_awkward_array, "A function that creates an awkward array");
}

0 comments on commit f750435

Please sign in to comment.