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

fix issue with repr and Decimal #488

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 8 additions & 1 deletion influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ def _escape_value(value):
elif isinstance(value, integer_types) and not isinstance(value, bool):
return str(value) + 'i'
elif _is_float(value):
return repr(value)
reprvalue = repr(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just make this return repr(float(value)) since you've already passed the _is_float test?

Copy link
Author

Choose a reason for hiding this comment

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

I guess I went that way because I didn't want to lose precision of the Decimal, but I don't know if it's worth it, your solution is quite shorter and more elegant.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you're referring to your example earlier, even if Decimal resolves to a more granular value than what you sent it may not be what you actually want. If you wanted that value down to the full granularity you should provide that to begin with (IMHO of course).

Do you want to take a stab at changing your PR? If so, can you please add a quick test to check both a non-Decimal and Decimal value?

Copy link
Contributor

Choose a reason for hiding this comment

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

To clarify, since your Decimal value is set to 16 digits, you should get the repr value of the 16-digit float instead of a (potentially) longer, more granular definition.

In [1]: from decimal import Decimal

In [2]: str(Decimal(0.8289445733333332))
Out[2]: '0.828944573333333156739399782964028418064117431640625'

In [3]: repr(Decimal(0.8289445733333332))
Out[3]: "Decimal('0.828944573333333156739399782964028418064117431640625')"

In [4]: repr(float(Decimal(0.8289445733333332)))
Out[4]: '0.8289445733333332'

decimalstart = "Decimal('"
decimalstop = "')"
if reprvalue.startswith(decimalstart):
return reprvalue.lstrip(decimalstart).rstrip(decimalstop)
else:
return reprvalue


return str(value)

Expand Down