-
Notifications
You must be signed in to change notification settings - Fork 524
Data arg in write() can be single string #492
Conversation
data argument in write() was expected to be list of strings, now it can be single string as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good, thank you for the catch!
influxdb/client.py
Outdated
@@ -284,15 +285,18 @@ def write(self, data, params=None, expected_response_code=204, | |||
headers = self._headers | |||
headers['Content-type'] = 'application/octet-stream' | |||
|
|||
if params: | |||
if params and 'precision' in params: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this check here, this will default to None.
In [1]: params = {'one': 'two'}
In [2]: if params:
: precision = params.get('precision')
: else:
: precision = None
:
In [3]: precision
In [4]: params
Out[4]: {'one': 'two'}
In [5]: type(precision)
Out[5]: NoneType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I thought I got exception from this but apparently missing key with get() gives Null.
You're right.
influxdb/client.py
Outdated
precision = params.get('precision') | ||
else: | ||
precision = None | ||
|
||
if protocol == 'json': | ||
data = make_lines(data, precision).encode('utf-8') | ||
elif protocol == 'line': | ||
data = ('\n'.join(data) + '\n').encode('utf-8') | ||
if type(data) == list: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this slightly as follows:
if not isinstance(data, list):
data = [data]
data = ('\n'.join(data) + '\n').encode('utf-8')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not super-experienced Pythonist, however I think that this is not the best solution. Or is it?
Since sequence is requested, user may provide both list or tuple. In this case if tuple is found it will be not casted to list but put into list. Then the following join will fail because instead of str it will find tuple. Cast by list() would be much better, because str will be put into list, and tuple converted into list. Win-win I guess. If any other data structure was provided - exception is justified.
influxdb/client.py
Outdated
@@ -292,6 +293,8 @@ def write(self, data, params=None, expected_response_code=204, | |||
if protocol == 'json': | |||
data = make_lines(data, precision).encode('utf-8') | |||
elif protocol == 'line': | |||
if isinstance(data, str): | |||
data = list(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not do what you expect, instead it'll split out your string into a list as follows:
In [4]: tester = "onetwothree"
In [5]: [tester]
Out[5]: ['onetwothree']
In [6]: list(tester)
Out[6]: ['o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e']
Please replace with data = [data]
and that should suffice.
This is great, thank you @baftek! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for your attention to detail @baftek!
data argument in write() was expected to be list of strings, now it can be single string as well