Skip to content

Commit 2d1fb2c

Browse files
authored
feat(query): query to csv skip empty lines (influxdata#536)
1 parent 735385d commit 2d1fb2c

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.36.0 [unreleased]
22

3+
### Features
4+
1. [#536](https://github.com/influxdata/influxdb-client-python/pull/536): Query to `CSV` skip empty lines
5+
36
## 1.35.0 [2022-12-01]
47

58
### Features

examples/query.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@
9595
dialect=Dialect(header=False, delimiter=",", comment_prefix="#", annotations=[],
9696
date_time_format="RFC3339"))
9797
for csv_line in csv_result:
98-
if not len(csv_line) == 0:
99-
print(f'Temperature in {csv_line[9]} is {csv_line[6]}')
98+
print(f'Temperature in {csv_line[9]} is {csv_line[6]}')
10099

101100
print()
102101
print()

influxdb_client/client/flux_table.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,14 @@ def __init__(self, response: HTTPResponse) -> None:
262262

263263
def __iter__(self):
264264
"""Return an iterator object."""
265-
return self.delegate.__iter__()
265+
return self
266266

267267
def __next__(self):
268268
"""Retrieve the next item from the iterator."""
269-
return self.delegate.__next__()
269+
row = self.delegate.__next__()
270+
while not row:
271+
row = self.delegate.__next__()
272+
return row
270273

271274
def to_values(self) -> List[List[str]]:
272275
"""
@@ -284,4 +287,4 @@ def to_values(self) -> List[List[str]]:
284287
...
285288
]
286289
"""
287-
return list(self.delegate)
290+
return list(self.__iter__())

tests/test_QueryApi.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,41 @@ def test_time_to_ast(self):
530530
self.assertEqual('DateTimeLiteral', ast.body[0].assignment.init.type)
531531
self.assertEqual(literal[1], ast.body[0].assignment.init.value)
532532

533+
def test_csv_empty_lines(self):
534+
query_response = '#datatype,string,long,dateTime:RFC3339,double,string\n' \
535+
'#group,false,false,false,false,true\n' \
536+
'#default,_result,,,,\n' \
537+
',result,table,_time,_value,_field\n' \
538+
',,0,2022-11-24T10:00:10Z,0.1,_1_current_(mA)\n' \
539+
',,1,2022-11-24T10:00:10Z,4,_1_current_limit_(mA)\n' \
540+
',,2,2022-11-24T10:00:10Z,1,_1_voltage_(V)\n' \
541+
',,3,2022-11-24T10:00:10Z,1,_1_voltage_limit_(V)\n' \
542+
',,4,2022-11-24T10:00:10Z,0,_2_current_(mA)\n' \
543+
',,5,2022-11-24T10:00:10Z,0,_2_current_limit_(mA)\n' \
544+
',,6,2022-11-24T10:00:10Z,0,_2_voltage_(V)\n' \
545+
',,7,2022-11-24T10:00:10Z,0,_2_voltage_limit_(V)\n' \
546+
'\n' \
547+
'\n' \
548+
'#datatype,string,long,dateTime:RFC3339,string,string\n' \
549+
'#group,false,false,false,false,true\n' \
550+
'#default,_result,,,,\n' \
551+
',result,table,_time,_value,_field\n' \
552+
',,8,2022-11-24T10:00:10Z,K,type\n' \
553+
',,9,2022-11-24T10:00:10Z,,type2\n' \
554+
'\n'
555+
556+
httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/query", status=200, body=query_response)
557+
558+
self.client = InfluxDBClient("http://localhost", "my-token", org="my-org", enable_gzip=False)
559+
560+
csv_lines = list(self.client.query_api().query_csv('from(bucket: "my-bucket")', "my-org"))
561+
self.assertEqual(18, len(csv_lines))
562+
for csv_line in csv_lines:
563+
self.assertEqual(6, len(csv_line))
564+
565+
# to_values
566+
csv_lines = self.client.query_api().query_csv('from(bucket: "my-bucket")', "my-org").to_values()
567+
self.assertEqual(18, len(csv_lines))
533568

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

0 commit comments

Comments
 (0)