Skip to content

Commit

Permalink
python support for to_summary_string
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Feb 5, 2021
1 parent 8f7ee9c commit 8605008
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s
- Added conduit::utils::format methods. These methods use fmt to format strings that include fmt style patterns. The formatting arguments are passed as a conduit::Node tree. The `args` case allows named arguments (args passed as object) or ordered args (args passed as list). The `maps` case also supports named or ordered args and works in conjunction with a `map_index`. The `map_index` is used to fetch a value from an array, or list of strings, which is then passed to fmt. The `maps` style of indexed indirection supports generating path strings for non-trivial domain partition mappings in Blueprint. This functionality is also available in Python, via the `conduit.utils.format` method.
- Added `DataArray::fill` method, which set all elements of a DataArray to a given value.
- Added `Node::to_summary_string` methods, which allow you to create truncated strings that describe a node tree, control the max number of children and max number of elements shown.
- Added python support for `Node.to_summary_string`

#### Relay
- Added Relay IO Handle mode support for `a` (append) and `t` (truncate). Truncate allows you to overwrite files when the handle is opened. The default is append, which preserves prior IO Handle behavior.
Expand All @@ -36,6 +37,12 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s
- Added `material_map` to `conduit::blueprint::mesh:matset::index`, to provide an explicit material name to id mapping.
- Added `mat_check` field to `blueprint::mesh::examples::venn`. This field encodes the material info in a scalar field and in the `matset_values` in a way that can be used to easily compare and verify proper construction in other tools.

### Changed

#### General
- Python Node repr string construction now uses `Node.to_summary_string()`


### Fixed

#### Relay
Expand Down
62 changes: 61 additions & 1 deletion src/libs/conduit/python/conduit_python.cpp
Expand Up @@ -1696,6 +1696,7 @@ PyConduit_DataType_to_string(PyConduit_DataType* self,
return (Py_BuildValue("s", oss.str().c_str()));
}


//---------------------------------------------------------------------------//
static PyObject *
PyConduit_DataType_to_json(PyConduit_DataType* self,
Expand Down Expand Up @@ -4177,7 +4178,7 @@ PyConduit_Node_str(PyConduit_Node* self)
static PyObject *
PyConduit_Node_repr(PyConduit_Node* self)
{
return PyConduit_Node_str(self);
return (Py_BuildValue("s", self->node->to_summary_string().c_str()));
}

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -5537,6 +5538,59 @@ PyConduit_Node_to_string(PyConduit_Node* self,
return (Py_BuildValue("s", oss.str().c_str()));
}

//---------------------------------------------------------------------------//
static PyObject *
PyConduit_Node_to_summary_string(PyConduit_Node* self,
PyObject* args,
PyObject* kwargs)
{
PyObject *py_opts = NULL;
static const char *kwlist[] = {"opts",
NULL};

if (!PyArg_ParseTupleAndKeywords(args,
kwargs,
"|O",
const_cast<char**>(kwlist),
&py_opts))
{
return (NULL);
}

if(py_opts != NULL && !PyConduit_Node_Check(py_opts))
{
PyErr_SetString(PyExc_TypeError,
"'opts' argument must be a "
"conduit.Node instance");
return NULL;
}

Node opts;
Node *opts_ptr = &opts;

if(py_opts != NULL)
{
opts_ptr = PyConduit_Node_Get_Node_Ptr(py_opts);
}

std::ostringstream oss;

try
{
self->node->to_summary_string_stream(oss,
*opts_ptr);

}
catch(conduit::Error e)
{
PyErr_SetString(PyExc_IOError,
e.message().c_str());
return NULL;
}

return (Py_BuildValue("s", oss.str().c_str()));
}

//---------------------------------------------------------------------------//
static PyObject *
PyConduit_Node_to_json(PyConduit_Node* self,
Expand Down Expand Up @@ -5959,6 +6013,12 @@ static PyMethodDef PyConduit_Node_METHODS[] = {
"Returns a string representation of the node. "
"Optionally takes protocol and spacing options. "
"(Default protocol='yaml'.)"},
//------------------------------------------------------------t-----------//
{"to_summary_string",
(PyCFunction)PyConduit_Node_to_summary_string,
METH_VARARGS| METH_KEYWORDS,
"Returns a summary string representation of the node. "
"Optionally takes a Node that provides spacing and threshold options. "},
//-----------------------------------------------------------------------//
{"to_json",
(PyCFunction)PyConduit_Node_to_json,
Expand Down
50 changes: 50 additions & 0 deletions src/tests/conduit/python/t_python_conduit_node.py
Expand Up @@ -577,6 +577,56 @@ def test_describe(self):
d = n.describe(opts)
print(d)

def test_summary_string(self):
n = Node()
n["a"] = [1,2,3,4,5];
n["b"] = [1,2,3];
n["c"] = [1,2,3,4,5,6];
n["d"] = [1,2,3,4,5,6,7];
n["e"] = [1,2,3,4,5,6,7,8,9,10,11,12];
n["f"] = [1.0,2.0,3.0,4.0,5.0,6.0,7.0];
n["g"] = [2.0,4.0];

print(repr(n))

r = n.to_summary_string()
print(r)
texp = """
a: [1, 2, 3, 4, 5]
b: [1, 2, 3]
c: [1, 2, 3, ..., 5, 6]
d: [1, 2, 3, ..., 6, 7]
e: [1, 2, 3, ..., 11, 12]
f: [1.0, 2.0, 3.0, ..., 6.0, 7.0]
g: [2.0, 4.0]
"""
self.assertEqual(r,texp)

opts = Node()
opts["num_children_threshold"] = 2
opts["num_elements_threshold"] = 3
r = n.to_summary_string(opts)
print(r)

texp = """
a: [1, 2, ..., 5]
... ( skipped 5 children )
g: [2.0, 4.0]
"""
self.assertEqual(r,texp)
r = n.to_summary_string(opts=opts)
print(r)

self.assertEqual(r,texp)

opts = Node()
opts["num_children_threshold"] = 100
opts["num_elements_threshold"] = -1
r = n.to_summary_string(opts)
print(r)

self.assertEqual(r,n.to_yaml())


if __name__ == '__main__':
unittest.main()
Expand Down
2 changes: 1 addition & 1 deletion src/tests/conduit/t_conduit_to_string.cpp
Expand Up @@ -339,4 +339,4 @@ a: 10
)ST";
EXPECT_EQ(tres,texpect);

}
}

0 comments on commit 8605008

Please sign in to comment.