Skip to content

Commit 992e03a

Browse files
committed
Raises error if struct is nested for CSV
1 parent 399d3c5 commit 992e03a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

uk_covid19/api_interface.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ def get_csv(self, save_as=None) -> str:
376376
"""
377377
Provides full data (all pages) in CSV.
378378
379+
.. warning::
380+
Please make sure that the ``structure`` is not hierarchical as
381+
CSV outputs are defined as 2D tables and as such, do not support
382+
hierarchies.
383+
379384
Parameters
380385
----------
381386
save_as: Union[str, None]
@@ -407,6 +412,19 @@ def get_csv(self, save_as=None) -> str:
407412
East Midlands,0
408413
...
409414
"""
415+
# Checks to ensure that the structure is
416+
# not hierarchical.
417+
if isinstance(self.structure, dict):
418+
non_str = filter(
419+
lambda val: not isinstance(val, str),
420+
self.structure.values()
421+
)
422+
423+
if list(non_str):
424+
struct = dumps(self.structure, indent=4)
425+
raise ValueError("CSV structure cannot be nested. Received:\n%s" % struct)
426+
427+
linebreak = "\n"
410428
resp = str()
411429

412430
for page_num, response in enumerate(self._get("csv"), start=1):
@@ -415,10 +433,10 @@ def get_csv(self, save_as=None) -> str:
415433
# Removing CSV header (column names) where page
416434
# number is greater than 1.
417435
if page_num > 1:
418-
data_lines = decoded_content.split("\n")[1:]
419-
decoded_content = str.join("\n", data_lines)
436+
data_lines = decoded_content.split(linebreak)[1:]
437+
decoded_content = str.join(linebreak, data_lines)
420438

421-
resp += decoded_content.strip() + "\n"
439+
resp += decoded_content.strip() + linebreak
422440

423441
if save_as is None:
424442
return resp

0 commit comments

Comments
 (0)