RecordProxy =========== .. cpp:class:: numpy::RecordProxy numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use RecordProxy RecordProxy obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``RecordProxy(StructuredArray \*array, const std::vector&indices)`` - NP_RECARRAY.H:41 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``RecordArrayFieldProxy operator[](const std::string &field_name)`` - RecordArrayFieldProxy - NP_RECARRAY.H:44 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T getField(const std::string &field_name)`` - T - NP_RECARRAY.H:51 - :ref:`View ` * - ``StructuredRecord getRecord()`` - StructuredRecord - NP_RECARRAY.H:61 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void setField(const std::string &field_name, const T &value)`` - void - NP_RECARRAY.H:56 - :ref:`View ` * - ``void setRecord(const StructuredRecord &record)`` - void - NP_RECARRAY.H:65 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-recordproxy-getfield-0: .. dropdown:: getField (np_test_2_all.cpp:18700) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18690 :emphasize-lines: 11 void np_test_io_extensions_recfromcsv_dtype_detection() { std::cout << "========= recfromcsv: automatic dtype detection ======================="; create_csv_file("test_dtypes.csv"); auto data = numpy::io::recfromcsv(csv_test_dir + "test_dtypes.csv"); auto dtype = data.getDType(); // Verify string field detected auto name_dtype = dtype.getField("name").dtype; if (name_dtype != numpy::DType::STR32 && name_dtype != numpy::DType::STR16 && name_dtype != numpy::DType::STR8 && name_dtype != numpy::DType::STR64) { std::cout << " [FAIL] : Expected STRING type for name field" << std::endl; throw std::runtime_error("np_test_io_extensions_recfromcsv_dtype_detection failed: wrong name dtype"); } // Verify integer field detected auto age_dtype = dtype.getField("age").dtype; if (age_dtype != numpy::DType::INT64 && age_dtype != numpy::DType::INT32) { std::cout << " [FAIL] : Expected INT type for age field"; .. _example-recordproxy-getrecord-1: .. dropdown:: getRecord (np_test_1_all.cpp:26346) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26336 :emphasize-lines: 11 }; auto dtype = std::make_shared(fields); numpy::RecordArray arr(dtype, { 2 }); arr.setFieldValue({ 0 }, "id", numpy::int32(1)); arr.setFieldValue({ 0 }, "value", numpy::float64(10.5)); arr.setFieldValue({ 1 }, "id", numpy::int32(2)); arr.setFieldValue({ 1 }, "value", numpy::float64(20.5)); // Test basic comparison operations (indirectly tests createComparisonValue) auto rec0 = arr.getRecord({ 0 }); auto rec1 = arr.getRecord({ 1 }); // Verify records are different (basic comparison test) bool passed = true; // If we can get records without error, comparison values work if (!passed) { std::cout << " [FAIL] : in np_test_recarray_createComparisonValue() : Create comparison value failed"; throw std::runtime_error("np_test_recarray_createComparisonValue failed"); } .. _example-recordproxy-setfield-2: .. dropdown:: setField (np_test_5_all.cpp:8457) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8447 :emphasize-lines: 11 std::cout << "========= StructuredArray::toString() basic ======================="; std::vector fields = { {"name", numpy::DType::STR_, 0, 10}, {"age", numpy::DType::INT32, 10, 4} }; auto dtype = std::make_shared(fields); numpy::StructuredArray arr(dtype, std::vector{2}); arr.setField({ 0 }, "name", numpy::StructuredValue(std::string("Alice"))); arr.setField({ 0 }, "age", numpy::StructuredValue(static_cast(30))); arr.setField({ 1 }, "name", numpy::StructuredValue(std::string("Bob"))); arr.setField({ 1 }, "age", numpy::StructuredValue(static_cast(25))); std::string result = arr.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "StructuredArray toString()");