Skip to content

Commit

Permalink
refactor: Replace duplicated save get encoding with a single expres…
Browse files Browse the repository at this point in the history
…sion

Although there are branches it isn't needed, I think this is easier to read and maintain.

Very low priority change, just something I noticed while reading.
  • Loading branch information
dangotbanned committed May 12, 2024
1 parent 9c770a5 commit 99b4f81
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions altair/utils/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def save(
if json_kwds is None:
json_kwds = {}
fp = pathlib.Path(fp) if isinstance(fp, str) else fp # type: ignore[assignment]
encoding = kwargs.get("encoding", "utf-8")

format = set_inspect_format_argument(format, fp, inline) # type: ignore[assignment]

Expand All @@ -141,9 +142,7 @@ def perform_save():

if format == "json":
json_spec = json.dumps(spec, **json_kwds)
write_file_or_filename(
fp, json_spec, mode="w", encoding=kwargs.get("encoding", "utf-8")
)
write_file_or_filename(fp, json_spec, mode="w", encoding=encoding)
elif format == "html":
if inline:
kwargs["template"] = "inline"
Expand All @@ -159,12 +158,9 @@ def perform_save():
**kwargs,
)
write_file_or_filename(
fp,
mimebundle["text/html"],
mode="w",
encoding=kwargs.get("encoding", "utf-8"),
fp, mimebundle["text/html"], mode="w", encoding=encoding
)
elif format in ["png", "svg", "pdf", "vega"]:
elif format in {"png", "svg", "pdf", "vega"}:
mimebundle = spec_to_mimebundle(
spec=spec,
format=format,
Expand All @@ -183,7 +179,6 @@ def perform_save():
elif format == "pdf":
write_file_or_filename(fp, mimebundle["application/pdf"], mode="wb")
else:
encoding = kwargs.get("encoding", "utf-8")
write_file_or_filename(
fp, mimebundle["image/svg+xml"], mode="w", encoding=encoding
)
Expand Down

0 comments on commit 99b4f81

Please sign in to comment.