Skip to content
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: 7 additions & 3 deletions homeassistant/components/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import queue
import threading
import time
import math

import requests.exceptions
import voluptuous as vol
Expand Down Expand Up @@ -220,9 +221,12 @@ def event_to_json(event):
json['fields'][key] = float(
RE_DECIMAL.sub('', new_value))

# Infinity is not a valid float in InfluxDB
if (key, float("inf")) in json['fields'].items():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whoa, this line was pretty inefficient. If I understand correctly this would previously have stepped through the entire dictionary. It's not a big data structure but still...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought it would be efficient because items() creates a view object, not a list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It still has to iterate through all items. New check is constant time 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I looked it up now and I still think the previous implementation was just as constant.

PEP 3106 says that (Python 3) .items() is "set-like" and specifies __contains__ with this pseudocode:

    def __contains__(self, (key, value)):
        return key in self.__d and self.__d[key] == value

Copy link
Copy Markdown
Member

@OttoWinter OttoWinter May 9, 2018

Choose a reason for hiding this comment

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

Oh wow, thanks for the info. You learn something new every day :D

del json['fields'][key]
# Infinity and NaN are not valid floats in InfluxDB
try:
if not math.isfinite(json['fields'][key]):
del json['fields'][key]
except (KeyError, TypeError):
pass

json['tags'].update(tags)

Expand Down
2 changes: 1 addition & 1 deletion tests/components/test_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_event_listener_inf(self, mock_client):
"""Test the event listener for missing units."""
self._setup()

attrs = {'bignumstring': "9" * 999}
attrs = {'bignumstring': '9' * 999, 'nonumstring': 'nan'}
state = mock.MagicMock(
state=8, domain='fake', entity_id='fake.entity-id',
object_id='entity', attributes=attrs)
Expand Down