Skip to content

Commit 762fd6e

Browse files
feat: add modifiable __str__ to Point.
1 parent 9cf4b76 commit 762fd6e

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

influxdb_client/client/write/point.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Point data structure to represent LineProtocol."""
22

3-
43
import math
54
from builtins import int
65
from datetime import datetime, timedelta
@@ -54,6 +53,8 @@ class Point(object):
5453
Ref: http://bit.ly/influxdata-point
5554
"""
5655

56+
__str___rep = None
57+
5758
@staticmethod
5859
def measurement(measurement):
5960
"""Create a new Point with specified measurement name."""
@@ -146,7 +147,6 @@ def __init__(self, measurement_name):
146147
self._name = measurement_name
147148
self._time = None
148149
self._write_precision = DEFAULT_WRITE_PRECISION
149-
pass
150150

151151
def time(self, time, write_precision=DEFAULT_WRITE_PRECISION):
152152
"""
@@ -195,6 +195,24 @@ def write_precision(self):
195195
"""Get precision."""
196196
return self._write_precision
197197

198+
@classmethod
199+
def set_str_rep(cls, rep_function):
200+
"""Set the string representation for all Points."""
201+
cls.__str___rep = rep_function
202+
203+
def __str__(self):
204+
"""
205+
Create string representation of this Point.
206+
207+
Can be set via `Point.set_str_rep`. Defaults to `to_line_protocol`
208+
Example:
209+
.. code-block:: python
210+
Point.set_str_rep(lambda p: f'{p._name} - {p._tags} - {p._fields} - {p._time}')
211+
"""
212+
if self.__str___rep is None:
213+
return self.to_line_protocol()
214+
return self.__str___rep()
215+
198216

199217
def _append_tags(tags):
200218
_return = []

tests/test_point.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
class PointTest(unittest.TestCase):
1313

14+
def test_ToStr(self):
15+
point = Point.measurement("h2o").tag("location", "europe").field("level", 2.2)
16+
expected_str = "h2o,location=europe level=2.2"
17+
self.assertEqual(expected_str, str(point))
18+
19+
def my_str_rep(p: Point) -> str:
20+
return f'{p._name} - {p._tags} - {p._fields} - {p._time}'
21+
22+
Point.set_str_rep(my_str_rep)
23+
24+
self.assertEqual(my_str_rep(point), str(point))
25+
1426
def test_MeasurementEscape(self):
1527
point = Point.measurement("h2 o").tag("location", "europe").tag("", "warn").field("level", 2)
1628
self.assertEqual(point.to_line_protocol(), "h2\\ o,location=europe level=2i")
@@ -36,17 +48,17 @@ def test_TagEmptyValue(self):
3648
self.assertEqual("h2o,location=europe level=2i", point.to_line_protocol())
3749

3850
def test_TagEscapingKeyAndValue(self):
39-
4051
point = Point.measurement("h\n2\ro\t_data") \
4152
.tag("new\nline", "new\nline") \
4253
.tag("carriage\rreturn", "carriage\nreturn") \
4354
.tag("t\tab", "t\tab") \
4455
.field("level", 2)
4556

46-
self.assertEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.to_line_protocol())
57+
self.assertEqual(
58+
"h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i",
59+
point.to_line_protocol())
4760

4861
def test_EqualSignEscaping(self):
49-
5062
point = Point.measurement("h=2o") \
5163
.tag("l=ocation", "e=urope") \
5264
.field("l=evel", 2)
@@ -391,22 +403,24 @@ def test_backslash(self):
391403
def test_numpy_types(self):
392404
from influxdb_client.extras import np
393405

394-
point = Point.measurement("h2o")\
395-
.tag("location", "europe")\
396-
.field("np.float1", np.float(1.123))\
397-
.field("np.float2", np.float16(2.123))\
398-
.field("np.float3", np.float32(3.123))\
399-
.field("np.float4", np.float64(4.123))\
400-
.field("np.int1", np.int8(1))\
401-
.field("np.int2", np.int16(2))\
402-
.field("np.int3", np.int32(3))\
403-
.field("np.int4", np.int64(4))\
404-
.field("np.uint1", np.uint8(5))\
405-
.field("np.uint2", np.uint16(6))\
406-
.field("np.uint3", np.uint32(7))\
406+
point = Point.measurement("h2o") \
407+
.tag("location", "europe") \
408+
.field("np.float1", np.float(1.123)) \
409+
.field("np.float2", np.float16(2.123)) \
410+
.field("np.float3", np.float32(3.123)) \
411+
.field("np.float4", np.float64(4.123)) \
412+
.field("np.int1", np.int8(1)) \
413+
.field("np.int2", np.int16(2)) \
414+
.field("np.int3", np.int32(3)) \
415+
.field("np.int4", np.int64(4)) \
416+
.field("np.uint1", np.uint8(5)) \
417+
.field("np.uint2", np.uint16(6)) \
418+
.field("np.uint3", np.uint32(7)) \
407419
.field("np.uint4", np.uint64(8))
408420

409-
self.assertEqual("h2o,location=europe np.float1=1.123,np.float2=2.123,np.float3=3.123,np.float4=4.123,np.int1=1i,np.int2=2i,np.int3=3i,np.int4=4i,np.uint1=5i,np.uint2=6i,np.uint3=7i,np.uint4=8i", point.to_line_protocol())
421+
self.assertEqual(
422+
"h2o,location=europe np.float1=1.123,np.float2=2.123,np.float3=3.123,np.float4=4.123,np.int1=1i,np.int2=2i,np.int3=3i,np.int4=4i,np.uint1=5i,np.uint2=6i,np.uint3=7i,np.uint4=8i",
423+
point.to_line_protocol())
410424

411425
def test_from_dictionary_custom_measurement(self):
412426
dictionary = {
@@ -457,7 +471,9 @@ def test_from_dictionary_custom_fields(self):
457471
record_measurement_key="name",
458472
record_tag_keys=["location", "version"],
459473
record_field_keys=["pressure", "temperature"])
460-
self.assertEqual("sensor_pt859,location=warehouse_125,version=2021.06.05.5874 pressure=125i,temperature=10i 1632208639", point.to_line_protocol())
474+
self.assertEqual(
475+
"sensor_pt859,location=warehouse_125,version=2021.06.05.5874 pressure=125i,temperature=10i 1632208639",
476+
point.to_line_protocol())
461477

462478
def test_from_dictionary_tolerant_to_missing_tags_and_fields(self):
463479
dictionary = {

0 commit comments

Comments
 (0)