Skip to content

Commit 16d4063

Browse files
committed
feat: add get_edges methods to python
python: add Attribute constructor that uses local agent id
1 parent 1a43be2 commit 16d4063

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

python-wrapper/python_api.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ PYBIND11_MODULE(pydsr, m) {
315315

316316
return Attribute(convert_variant(v), get_unix_timestamp(), agent_id);
317317
}),"value"_a, "agent_id"_a)
318+
.def(py::init([&](attribute_type const& v) {
319+
//Comprobar tipos en ValType. Como se convien los arrays de numpy, las listas, los doubles, etc.
320+
return Attribute(convert_variant(v), get_unix_timestamp(), local_agent_id);
321+
}),"value"_a)
318322
.def("__repr__", [](Attribute const &self) {
319323

320324
std::stringstream out;
@@ -597,7 +601,15 @@ PYBIND11_MODULE(pydsr, m) {
597601
[](Node &self, const std::map<std::pair<uint64_t, std::string>, Edge> &edges) {
598602
return self.fano(edges);
599603
},
600-
py::return_value_policy::reference, "read or write in the edge map of the node.");
604+
py::return_value_policy::reference, "read or write in the edge map of the node.")
605+
.def("get_edges", [](Node &self){
606+
std::vector<Edge> edges;
607+
edges.reserve(self.fano().size());
608+
for (auto [_, edge] : self.fano()) {
609+
edges.emplace_back(edge);
610+
}
611+
return edges;
612+
});
601613

602614

603615

@@ -657,6 +669,7 @@ PYBIND11_MODULE(pydsr, m) {
657669
.def("get_nodes", &DSRGraph::get_nodes, "Returns all nodes")
658670
.def("get_name_from_id", &DSRGraph::get_name_from_id, "id"_a, "Return the name of a node given its id")
659671
.def("get_id_from_name", &DSRGraph::get_id_from_name, "name"_a, "Return the id from a node given its name")
672+
.def("get_edges", &DSRGraph::get_edges, "Return all the edges in the graph")
660673
.def("get_edges_by_type", &DSRGraph::get_edges_by_type, "type"_a, "Return all the edges with a given type.")
661674
.def("get_edges_to_id", &DSRGraph::get_edges_to_id, "id"_a, "Return all the edges that point to the node")
662675
.def("write_to_json_file", &DSRGraph::write_to_json_file, "file"_a, "skip_atts"_a=std::vector<std::string>{}, "Return all the edges that point to the node");

python-wrapper/test/PythonAPITests.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ def test_attrs(self):
136136
def test_edge(self):
137137
tmp = Node(1, "root", "name")
138138
self.assertEqual(len(tmp.edges), 0)
139-
tmp.edges[(11, "RT")] = Edge(10, 11, "RT", 0)
139+
self.assertEqual(len(tmp.get_edges()), 0)
140+
tmp.edges[(11,"RT")] = Edge(10,11, "RT", 0)
140141
self.assertEqual(len(tmp.edges), 1)
141-
self.assertIsNotNone(tmp.edges[(11, "RT")])
142-
del tmp.edges[(11, "RT")]
142+
self.assertEqual(len(tmp.get_edges()), 1)
143+
self.assertIsNotNone(tmp.edges[(11,"RT")])
144+
del tmp.edges[(11,"RT")]
143145
with self.assertRaises(KeyError):
144146
tmp.edges[(11, "RT")]
145147

@@ -235,7 +237,8 @@ def test_insert_or_asssign_edge(self):
235237
True,
236238
)
237239
edge = g.get_edge(1, 2, "RT")
238-
edge.attrs["color"] = Attribute("red", 0, 12)
240+
edge.attrs["color"] = Attribute("red")
241+
self.assertEqual(edge.attrs["color"].agent_id, 12)
239242
g.insert_or_assign_edge(edge)
240243
edge = g.get_edge(1, 2, "RT")
241244
self.assertEqual(edge.attrs["color"].value, "red")

0 commit comments

Comments
 (0)