Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

chore(line_protocol): update repr value of floats to properly handle precision #813

Merged
merged 3 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update make_lines section in line_protocol.py to split out core function (#375 thx @aisbaa)
- Fix nanosecond time resolution for points (#407 thx @AndreCAndersen && @clslgrnc)
- Fix import of distutils.spawn (#805 thx @Hawk777)
- Update repr of float values including properly handling of boolean (#488 thx @ghost)

### Removed

Expand Down
5 changes: 4 additions & 1 deletion influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ def _escape_value(value):
if isinstance(value, integer_types) and not isinstance(value, bool):
return str(value) + 'i'

if isinstance(value, bool):
return str(value)

if _is_float(value):
return repr(value)
return repr(float(value))

return str(value)

Expand Down
23 changes: 21 additions & 2 deletions influxdb/tests/test_line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
import unittest
from pytz import UTC, timezone

from datetime import datetime
from decimal import Decimal

from pytz import UTC, timezone
from influxdb import line_protocol


Expand Down Expand Up @@ -166,3 +168,20 @@ def test_float_with_long_decimal_fraction(self):
line_protocol.make_lines(data),
'test float_val=1.0000000000000009\n'
)

def test_float_with_long_decimal_fraction_as_type_decimal(self):
"""Ensure precision is preserved when casting Decimal into strings."""
data = {
"points": [
{
"measurement": "test",
"fields": {
"float_val": Decimal(0.8289445733333332),
}
}
]
}
self.assertEqual(
line_protocol.make_lines(data),
'test float_val=0.8289445733333332\n'
)