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

Minor Changes to avoid redundancy in code and follow code patterns #279

Merged
merged 3 commits into from
Sep 13, 2024
Merged
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
39 changes: 18 additions & 21 deletions src/databricks/labs/lsql/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections.abc import Callable, Iterable, Sized
from dataclasses import dataclass
from enum import Enum, unique
from io import BytesIO, StringIO
from io import StringIO
from pathlib import Path
from typing import TypeVar
from zipfile import ZipFile
Expand Down Expand Up @@ -899,32 +899,29 @@ def _from_dashboard_folder(cls, folder: Path) -> "DashboardMetadata":

def export_to_zipped_csv(self, sql_backend: SqlBackend, export_path: Path) -> Path:
"""Export the dashboard queries to CSV files directly into a ZIP archive."""
zip_export = export_path / "export_to_zipped_csv.zip"

with ZipFile(zip_export, mode="w") as zip_file:
with ZipFile(export_path, mode="w") as zip_file:
for tile in self.tiles:
if tile.metadata.is_query():
rows = sql_backend.fetch(tile.content)

if not rows:
continue
if not tile.metadata.is_query():
continue

Copy link
Member

@JCZuurmond JCZuurmond Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the whitelines to create code blocks that logically belonging together

Suggested change

buffer = StringIO()
writer = None
rows = list(sql_backend.fetch(tile.content))
if not rows:
continue

for row in rows:
if writer is None:
headers = row.asDict().keys()
writer = csv.DictWriter(buffer, fieldnames=headers)
writer.writeheader()
writer.writerow(row.asDict())
buffer = StringIO()
writer = None

bytes_buffer = BytesIO(buffer.getvalue().encode("utf-8"))
for row in rows:
if writer is None:
headers = row.asDict().keys()
writer = csv.DictWriter(buffer, fieldnames=headers)
writer.writeheader()
writer.writerow(row.asDict())

with zip_file.open(f"{tile.metadata.id}.csv", "w") as csv_file:
csv_file.write(bytes_buffer.getvalue())
with zip_file.open(f"{tile.metadata.id}.csv", "w") as csv_file:
csv_file.write(buffer.getvalue().encode("utf-8"))

return zip_export
return export_path


class Dashboards:
Expand Down
Loading