Skip to content

Commit

Permalink
Minor refactor of labelers (#55)
Browse files Browse the repository at this point in the history
* Micro optimization of building band label

Omitting the callable to filter is substantially faster than the
identity lambda, and slightly faster than a list comprehension.
Directly joining the filter is slightly faster than using a
placeholder list.

Per iteration:

Old version: 527 ns
New version: 305 ns

* Call labelers directly for inferred types

* Avoid shadowing builtin
  • Loading branch information
aazuspan authored Jan 23, 2025
1 parent 93db954 commit 3215f13
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions eerepr/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _build_pixeltype_label(obj: dict) -> str:
prec = obj.get("precision", "")
minimum = str(obj.get("min", ""))
maximum = str(obj.get("max", ""))
range = f"[{minimum}, {maximum}]"
val_range = f"[{minimum}, {maximum}]"

type_ranges = {
"[-128, 127]": "signed int8",
Expand All @@ -187,9 +187,9 @@ def _build_pixeltype_label(obj: dict) -> str:
if prec in ["double", "float"]:
return prec
try:
return type_ranges[range]
return type_ranges[val_range]
except KeyError:
return f"{prec}{range}"
return f"{prec}{val_range}"


def _build_band_label(obj: dict) -> str:
Expand All @@ -201,8 +201,7 @@ def _build_band_label(obj: dict) -> str:
dimensions = f"{dims[0]}x{dims[1]} px" if dims else ""
crs = obj.get("crs", "")

parts = list(filter(lambda k: k, [band_id, dtype, crs, dimensions]))
return ", ".join(parts)
return ", ".join(filter(None, [band_id, dtype, crs, dimensions]))


def _build_daterange_label(obj: dict) -> str:
Expand Down Expand Up @@ -235,7 +234,6 @@ def _build_label(obj: dict) -> str:
These labels attempt to be consistent with outputs from the Code Editor.
"""
labelers = {
# Explicit types
"Image": _build_image_label,
"ImageCollection": _build_imagecollection_label,
"Date": _build_date_label,
Expand All @@ -249,16 +247,14 @@ def _build_label(obj: dict) -> str:
"MultiPolygon": _build_multipolygon_label,
"PixelType": _build_pixeltype_label,
"DateRange": _build_daterange_label,
# Inferred types
"_Band": _build_band_label,
"_Object": _build_object_label,
"_Typed": _build_typed_label,
}

obj_type = obj.get("type", "")
if not obj_type:
obj_type = "_Band" if "id" in obj and "data_type" in obj else "_Object"
if obj_type not in labelers:
obj_type = "_Typed"

return labelers[obj_type](obj)
if "data_type" in obj and "id" in obj:
return _build_band_label(obj)
return _build_object_label(obj)
try:
return labelers[obj_type](obj)
except KeyError:
return _build_typed_label(obj)

0 comments on commit 3215f13

Please sign in to comment.