Skip to content

Commit f14cf55

Browse files
committed
Refactor code
1 parent 810122a commit f14cf55

File tree

1 file changed

+25
-64
lines changed

1 file changed

+25
-64
lines changed

bigframes/operations/blob.py

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ def _df_apply_udf(
193193

194194
return s
195195

196+
def _apply_udf_or_raise_error(
197+
self, df: bigframes.dataframe.DataFrame, udf, operation_name: str
198+
) -> bigframes.series.Series:
199+
"""Helper to apply UDF with consistent error handling."""
200+
try:
201+
res = self._df_apply_udf(df, udf)
202+
except Exception as e:
203+
raise RuntimeError(f"{operation_name} UDF execution failed: {e}") from e
204+
205+
if res is None:
206+
raise RuntimeError(f"{operation_name} returned None result")
207+
208+
return res
209+
196210
def read_url(self) -> bigframes.series.Series:
197211
"""Retrieve the read URL of the Blob.
198212
@@ -368,13 +382,7 @@ def exif(
368382
container_memory=container_memory,
369383
).udf()
370384

371-
try:
372-
res = self._df_apply_udf(df, exif_udf)
373-
except Exception as e:
374-
raise RuntimeError(f"EXIF extraction UDF execution failed: {e}") from e
375-
376-
if res is None:
377-
raise RuntimeError("EXIF extraction returned None result")
385+
res = self._apply_udf_or_raise_error(df, exif_udf, "EXIF extraction")
378386

379387
if verbose:
380388
try:
@@ -457,13 +465,7 @@ def image_blur(
457465
df["ksize_x"], df["ksize_y"] = ksize
458466
df["ext"] = ext # type: ignore
459467
df["verbose"] = verbose
460-
try:
461-
res = self._df_apply_udf(df, image_blur_udf)
462-
except Exception as e:
463-
raise RuntimeError(f"Image blur UDF execution failed: {e}") from e
464-
465-
if res is None:
466-
raise RuntimeError("Image blur returned None result")
468+
res = self._apply_udf_or_raise_error(df, image_blur_udf, "Image blur")
467469

468470
if verbose:
469471
blurred_content_b64_series = res._apply_unary_op(
@@ -512,13 +514,7 @@ def image_blur(
512514
df["ext"] = ext # type: ignore
513515
df["verbose"] = verbose
514516

515-
try:
516-
res = self._df_apply_udf(df, image_blur_udf)
517-
except Exception as e:
518-
raise RuntimeError(f"Image blur UDF execution failed: {e}") from e
519-
520-
if res is None:
521-
raise RuntimeError("Image blur returned None result")
517+
res = self._apply_udf_or_raise_error(df, image_blur_udf, "Image blur")
522518
res.cache() # to execute the udf
523519

524520
if verbose:
@@ -610,13 +606,7 @@ def image_resize(
610606
df["fx"], df["fy"] = fx, fy
611607
df["ext"] = ext # type: ignore
612608
df["verbose"] = verbose
613-
try:
614-
res = self._df_apply_udf(df, image_resize_udf)
615-
except Exception as e:
616-
raise RuntimeError(f"Image resize UDF execution failed: {e}") from e
617-
618-
if res is None:
619-
raise RuntimeError("Image resize returned None result")
609+
res = self._apply_udf_or_raise_error(df, image_resize_udf, "Image resize")
620610

621611
if verbose:
622612
resized_content_b64_series = res._apply_unary_op(
@@ -667,13 +657,7 @@ def image_resize(
667657
df["ext"] = ext # type: ignore
668658
df["verbose"] = verbose
669659

670-
try:
671-
res = self._df_apply_udf(df, image_resize_udf)
672-
except Exception as e:
673-
raise RuntimeError(f"Image resize UDF execution failed: {e}") from e
674-
675-
if res is None:
676-
raise RuntimeError("Image resize returned None result")
660+
res = self._apply_udf_or_raise_error(df, image_resize_udf, "Image resize")
677661
res.cache() # to execute the udf
678662

679663
if verbose:
@@ -759,13 +743,9 @@ def image_normalize(
759743
df["norm_type"] = norm_type
760744
df["ext"] = ext # type: ignore
761745
df["verbose"] = verbose
762-
try:
763-
res = self._df_apply_udf(df, image_normalize_udf)
764-
except Exception as e:
765-
raise RuntimeError(f"Image normalize UDF execution failed: {e}") from e
766-
767-
if res is None:
768-
raise RuntimeError("Image normalize returned None result")
746+
res = self._apply_udf_or_raise_error(
747+
df, image_normalize_udf, "Image normalize"
748+
)
769749

770750
if verbose:
771751
normalized_content_b64_series = res._apply_unary_op(
@@ -816,13 +796,7 @@ def image_normalize(
816796
df["ext"] = ext # type: ignore
817797
df["verbose"] = verbose
818798

819-
try:
820-
res = self._df_apply_udf(df, image_normalize_udf)
821-
except Exception as e:
822-
raise RuntimeError(f"Image normalize UDF execution failed: {e}") from e
823-
824-
if res is None:
825-
raise RuntimeError("Image normalize returned None result")
799+
res = self._apply_udf_or_raise_error(df, image_normalize_udf, "Image normalize")
826800
res.cache() # to execute the udf
827801

828802
if verbose:
@@ -899,14 +873,7 @@ def pdf_extract(
899873
df = self.get_runtime_json_str(mode="R").to_frame()
900874
df["verbose"] = verbose
901875

902-
try:
903-
res = self._df_apply_udf(df, pdf_extract_udf)
904-
except Exception as e:
905-
raise RuntimeError(f"PDF extraction UDF failed: {e}") from e
906-
907-
# Validate result is not None
908-
if res is None:
909-
raise RuntimeError("PDF extraction returned None result")
876+
res = self._apply_udf_or_raise_error(df, pdf_extract_udf, "PDF extraction")
910877

911878
if verbose:
912879
# Extract content with error handling
@@ -1005,13 +972,7 @@ def pdf_chunk(
1005972
df["overlap_size"] = overlap_size
1006973
df["verbose"] = verbose
1007974

1008-
try:
1009-
res = self._df_apply_udf(df, pdf_chunk_udf)
1010-
except Exception as e:
1011-
raise RuntimeError(f"PDF chunking UDF failed: {e}") from e
1012-
1013-
if res is None:
1014-
raise RuntimeError("PDF chunking returned None result")
975+
res = self._apply_udf_or_raise_error(df, pdf_chunk_udf, "PDF chunking")
1015976

1016977
try:
1017978
content_series = bbq.json_extract_string_array(res, "$.content")

0 commit comments

Comments
 (0)