From 2d1fb2c286ca043ca1dc98ae44fb1814e8ef0f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Wed, 7 Dec 2022 08:56:42 +0100 Subject: [PATCH] feat(query): query to csv skip empty lines (#536) --- CHANGELOG.md | 3 +++ examples/query.py | 3 +-- influxdb_client/client/flux_table.py | 9 ++++--- tests/test_QueryApi.py | 35 ++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6263ae8..82c701bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/query.py b/examples/query.py index c913ab4f..df7a3bbb 100644 --- a/examples/query.py +++ b/examples/query.py @@ -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() diff --git a/influxdb_client/client/flux_table.py b/influxdb_client/client/flux_table.py index 42324766..98a83159 100644 --- a/influxdb_client/client/flux_table.py +++ b/influxdb_client/client/flux_table.py @@ -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]]: """ @@ -284,4 +287,4 @@ def to_values(self) -> List[List[str]]: ... ] """ - return list(self.delegate) + return list(self.__iter__()) diff --git a/tests/test_QueryApi.py b/tests/test_QueryApi.py index b108cfe3..95767c7f 100644 --- a/tests/test_QueryApi.py +++ b/tests/test_QueryApi.py @@ -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()