Skip to content

Commit

Permalink
Python: let lib.string() handle variable- and fixed-sized strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasvr committed Dec 29, 2020
1 parent 14f42f8 commit 411874f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ int main(int argc, char **argv)
size_t payload_size = jas_str.length() + bytecode.size() + 1;
printf("\n%s dataset header:\n%s\n", info.name.c_str(), jas.dump(4).c_str());
if (compound_declarations.size())
printf("\nCompound data structures available to the UDF:\n%s\n", compound_declarations.c_str());
printf("\nData structures available to the UDF:\n%s\n", compound_declarations.c_str());

/* Sanity check: the JSON and the bytecode must fit in the dataset */
hsize_t grid_size = std::accumulate(std::begin(info.dimensions), std::end(info.dimensions), 1, std::multiplies<hsize_t>());
Expand Down
6 changes: 5 additions & 1 deletion src/udf_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def load(self, filterpath):
def string(self, structure):
# Strings are embedded in a structure with a single
# 'value' member.
return self.ffi.string(structure.value).decode("utf-8")
if hasattr(structure, "value"):
return self.ffi.string(structure.value).decode("utf-8")
# The user may also provide a direct pointer to the
# string member.
return self.ffi.string(structure).decode("utf-8")

def getData(self, name):
name = self.ffi.new("char[]", name.encode("utf-8"))
Expand Down
13 changes: 13 additions & 0 deletions test/test-compound-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# hdf5-udf <file.h5> test-compound-string.py Temperature.py:1000:double
def dynamic_dataset():
compound = lib.getData("Dataset1")
udf_data = lib.getData("Temperature.py")
udf_dims = lib.getDims("Temperature.py")

for i in range(udf_dims[0]):
print("serial: {}, location: {}, temperature: {:.6f}, pressure: {:.6f}".format(
compound[i].serial_number,
lib.string(compound[i].location),
compound[i].temperature,
compound[i].pressure), flush=True)
udf_data[i] = compound[i].temperature
3 changes: 1 addition & 2 deletions test/test-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ extern "C" void dynamic_dataset()
auto udf_data = lib.getData<double>("Temperature.cpp");
auto udf_dims = lib.getDims("Temperature.cpp");

for (size_t i=0; i<input_dims[0]; ++i) {
for (size_t i=0; i<input_dims[0]; ++i)
printf("%s\n", input_string[i].value);
}

for (size_t i=0; i<udf_dims[0]; ++i)
udf_data[i] = i * 1.0;
Expand Down

0 comments on commit 411874f

Please sign in to comment.