Skip to content

Commit f1407f1

Browse files
committed
pyntcore: Add repr for entries and topics
1 parent a7d0973 commit f1407f1

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

subprojects/pyntcore/gen/NetworkTableEntry.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ classes:
100100
GetTopic:
101101
operator==:
102102
operator!=:
103+
inline_code: |
104+
.def("__repr__", [](const NetworkTableEntry &self) {
105+
return py::str("<NetworkTableEntry {}>").format(self.GetName());
106+
})

subprojects/pyntcore/gen/Topic.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ classes:
4444
std::string_view, const PubSubOptions&:
4545
operator==:
4646
operator!=:
47+
inline_code: |
48+
.def("__repr__", [](py::handle self) {
49+
py::object type_name = self.get_type().attr("__qualname__");
50+
std::string name = self.cast<const Topic&>().GetName();
51+
return py::str("<{} {}>").format(type_name, name);
52+
})
4753
Subscriber:
4854
attributes:
4955
m_subHandle:
@@ -59,6 +65,12 @@ classes:
5965
ignore: true
6066
NT_Subscriber:
6167
ignore: true
68+
inline_code: |
69+
.def("__repr__", [](py::handle self) {
70+
py::object type_name = self.get_type().attr("__qualname__");
71+
auto topic = self.cast<const Subscriber&>().GetTopic();
72+
return py::str("<{} {}>").format(type_name, topic.GetName());
73+
})
6274
Publisher:
6375
attributes:
6476
m_pubHandle:
@@ -72,3 +84,9 @@ classes:
7284
ignore: true
7385
NT_Publisher:
7486
ignore: true
87+
inline_code: |
88+
.def("__repr__", [](py::handle self) {
89+
py::object type_name = self.get_type().attr("__qualname__");
90+
auto topic = self.cast<const Publisher&>().GetTopic();
91+
return py::str("<{} {}>").format(type_name, topic.GetName());
92+
})

subprojects/pyntcore/tests/test_entry.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
#
44

55

6+
def test_entry_repr(nt):
7+
e = nt.getEntry("/k1")
8+
assert repr(e) == "<NetworkTableEntry /k1>"
9+
10+
11+
def test_topic_repr(nt):
12+
topic = nt.getIntegerTopic("/int")
13+
assert repr(topic) == "<IntegerTopic /int>"
14+
15+
pub = topic.publish()
16+
assert repr(pub) == "<IntegerPublisher /int>"
17+
18+
entry = topic.getEntry(0)
19+
assert repr(entry) == "<IntegerEntry /int>"
20+
21+
generic_entry = topic.getGenericEntry()
22+
assert repr(generic_entry) == "<GenericEntry /int>"
23+
24+
625
def test_entry_string(nt):
726
e = nt.getEntry("/k1")
827
assert e.getString(None) is None

0 commit comments

Comments
 (0)