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

fix for lost precision on float field values #430

Merged
merged 2 commits into from
Apr 10, 2017
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
10 changes: 10 additions & 0 deletions influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,22 @@ def quote_literal(value):
)


def _is_float(value):
try:
float(value)
except ValueError:
return False
return True


def _escape_value(value):
value = _get_unicode(value)
if isinstance(value, text_type) and value != '':
return quote_ident(value)
elif isinstance(value, integer_types) and not isinstance(value, bool):
return str(value) + 'i'
elif _is_float(value):
return repr(value)
else:
return str(value)

Expand Down
17 changes: 17 additions & 0 deletions influxdb/tests/test_line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,20 @@ def test_quote_literal(self):
line_protocol.quote_literal(r"""\foo ' bar " Örf"""),
r"""'\\foo \' bar " Örf'"""
)

def test_float_with_long_decimal_fraction(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To conform to pep257 this public method should have a docstring, see #415

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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