Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): query to csv skip empty lines #536

Merged
merged 2 commits into from
Dec 7, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.36.0 [unreleased]

### Features
1. [#536](https://github.com/influxdata/influxdb-client-python/pull/536): Query to `CSV` skip empty lines

## 1.35.0 [2022-12-01]

### Features
Expand Down
3 changes: 1 addition & 2 deletions examples/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@
dialect=Dialect(header=False, delimiter=",", comment_prefix="#", annotations=[],
date_time_format="RFC3339"))
for csv_line in csv_result:
if not len(csv_line) == 0:
print(f'Temperature in {csv_line[9]} is {csv_line[6]}')
print(f'Temperature in {csv_line[9]} is {csv_line[6]}')

print()
print()
Expand Down
9 changes: 6 additions & 3 deletions influxdb_client/client/flux_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,14 @@ def __init__(self, response: HTTPResponse) -> None:

def __iter__(self):
"""Return an iterator object."""
return self.delegate.__iter__()
return self

def __next__(self):
"""Retrieve the next item from the iterator."""
return self.delegate.__next__()
row = self.delegate.__next__()
while not row:
row = self.delegate.__next__()
return row

def to_values(self) -> List[List[str]]:
"""
Expand All @@ -284,4 +287,4 @@ def to_values(self) -> List[List[str]]:
...
]
"""
return list(self.delegate)
return list(self.__iter__())
35 changes: 35 additions & 0 deletions tests/test_QueryApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,41 @@ def test_time_to_ast(self):
self.assertEqual('DateTimeLiteral', ast.body[0].assignment.init.type)
self.assertEqual(literal[1], ast.body[0].assignment.init.value)

def test_csv_empty_lines(self):
query_response = '#datatype,string,long,dateTime:RFC3339,double,string\n' \
'#group,false,false,false,false,true\n' \
'#default,_result,,,,\n' \
',result,table,_time,_value,_field\n' \
',,0,2022-11-24T10:00:10Z,0.1,_1_current_(mA)\n' \
',,1,2022-11-24T10:00:10Z,4,_1_current_limit_(mA)\n' \
',,2,2022-11-24T10:00:10Z,1,_1_voltage_(V)\n' \
',,3,2022-11-24T10:00:10Z,1,_1_voltage_limit_(V)\n' \
',,4,2022-11-24T10:00:10Z,0,_2_current_(mA)\n' \
',,5,2022-11-24T10:00:10Z,0,_2_current_limit_(mA)\n' \
',,6,2022-11-24T10:00:10Z,0,_2_voltage_(V)\n' \
',,7,2022-11-24T10:00:10Z,0,_2_voltage_limit_(V)\n' \
'\n' \
'\n' \
'#datatype,string,long,dateTime:RFC3339,string,string\n' \
'#group,false,false,false,false,true\n' \
'#default,_result,,,,\n' \
',result,table,_time,_value,_field\n' \
',,8,2022-11-24T10:00:10Z,K,type\n' \
',,9,2022-11-24T10:00:10Z,,type2\n' \
'\n'

httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/query", status=200, body=query_response)

self.client = InfluxDBClient("http://localhost", "my-token", org="my-org", enable_gzip=False)

csv_lines = list(self.client.query_api().query_csv('from(bucket: "my-bucket")', "my-org"))
self.assertEqual(18, len(csv_lines))
for csv_line in csv_lines:
self.assertEqual(6, len(csv_line))

# to_values
csv_lines = self.client.query_api().query_csv('from(bucket: "my-bucket")', "my-org").to_values()
self.assertEqual(18, len(csv_lines))

if __name__ == '__main__':
unittest.main()