Skip to content

Commit e584676

Browse files
authored
chore: influxdb_client/client/write: strip .0 on float values (#181)
Floating point whole numbers are encoded with a trailing `.0` which is unnecessary (integers are encoded with a trailing `i` character), is different from other line-protocol encoders, and makes encoded entries longer than necessary, so strip that suffix when it's seen.
1 parent 5e6569c commit e584676

File tree

5 files changed

+111
-104
lines changed

5 files changed

+111
-104
lines changed

influxdb_client/client/write/point.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ def _append_fields(fields):
150150
if isinstance(value, float) or isinstance(value, Decimal):
151151
if not math.isfinite(value):
152152
continue
153-
_return.append(f'{_escape_key(field)}={str(value)}')
153+
s = str(value)
154+
# It's common to represent whole numbers as floats
155+
# and the trailing ".0" that Python produces is unnecessary
156+
# in line-protocol, inconsistent with other line-protocol encoders,
157+
# and takes more space than needed, so trim it off.
158+
if s.endswith('.0'):
159+
s = s[:-2]
160+
_return.append(f'{_escape_key(field)}={s}')
154161
elif isinstance(value, int) and not isinstance(value, bool):
155162
_return.append(f'{_escape_key(field)}={str(value)}i')
156163
elif isinstance(value, bool):

tests/test_WriteApi.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def tearDown(self) -> None:
4141
def test_write_line_protocol(self):
4242
bucket = self.create_test_bucket()
4343

44-
record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
44+
record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
4545
self.write_client.write(bucket.name, self.org, record)
4646

4747
result = self.query_api.query(f"from(bucket:\"{bucket.name}\") |> range(start: 1970-01-01T00:00:00.000000001Z)",
@@ -72,8 +72,8 @@ def test_write_precision(self):
7272
def test_write_records_list(self):
7373
bucket = self.create_test_bucket()
7474

75-
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
76-
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2"
75+
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
76+
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2"
7777

7878
record_list = [_record1, _record2]
7979

@@ -170,7 +170,7 @@ def test_write_using_default_tags(self):
170170
def test_write_result(self):
171171
_bucket = self.create_test_bucket()
172172

173-
_record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
173+
_record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
174174
result = self.write_client.write(_bucket.name, self.org, _record)
175175

176176
# The success response is 204 - No Content
@@ -210,7 +210,7 @@ def test_write_dictionary(self):
210210

211211
def test_write_bytes(self):
212212
_bucket = self.create_test_bucket()
213-
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1".encode("utf-8")
213+
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=1 1".encode("utf-8")
214214

215215
self.write_client.write(_bucket.name, self.org, _bytes)
216216

@@ -230,9 +230,9 @@ def test_write_bytes(self):
230230
def test_write_tuple(self):
231231
bucket = self.create_test_bucket()
232232

233-
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
234-
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2"
235-
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=3.0 3".encode("utf-8")
233+
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
234+
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2"
235+
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=3 3".encode("utf-8")
236236

237237
p = (Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 4.0).time(4))
238238

@@ -322,7 +322,7 @@ def test_write_data_frame_without_measurement_name(self):
322322
def test_use_default_org(self):
323323
bucket = self.create_test_bucket()
324324

325-
record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
325+
record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
326326
self.write_client.write(bucket.name, record=record)
327327

328328
result = self.query_api.query(
@@ -485,7 +485,7 @@ def test_writes_synchronous_without_retry(self):
485485

486486
self.write_client = self.influxdb_client.write_api(write_options=SYNCHRONOUS)
487487
with self.assertRaises(ApiException) as cm:
488-
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1")
488+
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1")
489489
exception = cm.exception
490490

491491
self.assertEqual("Service Unavailable", exception.reason)
@@ -496,7 +496,7 @@ def test_writes_asynchronous_without_retry(self):
496496

497497
self.write_client = self.influxdb_client.write_api(write_options=ASYNCHRONOUS)
498498
with self.assertRaises(ApiException) as cm:
499-
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1").get()
499+
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1").get()
500500
exception = cm.exception
501501

502502
self.assertEqual("Service Unavailable", exception.reason)
@@ -527,7 +527,7 @@ def tearDown(self) -> None:
527527
def test_write_result(self):
528528
_bucket = self.create_test_bucket()
529529

530-
_record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
530+
_record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
531531
result = self.write_client.write(_bucket.name, self.org, _record)
532532

533533
self.assertEqual(ApplyResult, type(result))
@@ -649,8 +649,8 @@ def test_use_default_tags_with_data_frame(self):
649649
def test_write_bytes(self):
650650
bucket = self.create_test_bucket()
651651

652-
_bytes1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1".encode("utf-8")
653-
_bytes2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2".encode("utf-8")
652+
_bytes1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1".encode("utf-8")
653+
_bytes2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2".encode("utf-8")
654654

655655
_bytes_list = [_bytes1, _bytes2]
656656

0 commit comments

Comments
 (0)