Skip to content

Commit

Permalink
Replace remaining format function usages with python f-strings (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 authored and gs-ssh16 committed Oct 18, 2023
1 parent 05158ce commit a69d8df
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,12 @@ def validate(self) -> bool:
other_frame_cols = self.__other_frame.columns()

if len(base_frame_cols) != len(other_frame_cols):
cols1 = "[" + ", ".join([str(c) for c in base_frame_cols]) + "]"
cols2 = "[" + ", ".join([str(c) for c in other_frame_cols]) + "]"
raise ValueError(
"Cannot concatenate two Tds Frames with different column counts. \n"
"Frame 1 cols - (Count: {c1}) - {cols1} \n"
"Frame 2 cols - (Count: {c2}) - {cols2} \n".format(
c1=len(base_frame_cols),
cols1="[" + ", ".join([str(c) for c in base_frame_cols]) + "]",
c2=len(other_frame_cols),
cols2="[" + ", ".join([str(c) for c in other_frame_cols]) + "]"
)
f"Frame 1 cols - (Count: {len(base_frame_cols)}) - {cols1} \n"
f"Frame 2 cols - (Count: {len(other_frame_cols)}) - {cols2} \n"
)

for i in range(0, len(base_frame_cols)):
Expand All @@ -123,12 +120,8 @@ def validate(self) -> bool:

if (base_col.get_name() != other_col.get_name()) or (base_col.get_type() != other_col.get_type()):
raise ValueError(
"Column name/type mismatch when concatenating Tds Frames at index {i}. "
"Frame 1 column - {c1}, Frame 2 column - {c2}".format(
i=i,
c1=base_col,
c2=other_col
)
f"Column name/type mismatch when concatenating Tds Frames at index {i}. "
f"Frame 1 column - {base_col}, Frame 2 column - {other_col}"
)

return True
26 changes: 12 additions & 14 deletions pylegend/core/tds/legend_api/frames/functions/extend_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ def validate(self) -> bool:
if len(self.__functions_list) != len(self.__column_names_list):
raise ValueError(
"For extend function, function list and column names list arguments should be of same size. "
"Passed param sizes - Functions: {f}, Column names: {c}".format(
f=len(self.__functions_list),
c=len(self.__column_names_list)
)
f"Passed param sizes - Functions: {len(self.__functions_list)}, "
f"Column names: {len(self.__column_names_list)}"
)

tds_row = TdsRow.from_tds_frame("frame", self.__base_frame)
Expand All @@ -136,37 +134,37 @@ def validate(self) -> bool:
copy = func # For MyPy
if not isinstance(copy, type(lambda x: 0)) or (copy.__code__.co_argcount != 1):
raise TypeError(
"Error at extend function at index {i} (0-indexed). "
"Each extend function should be a lambda which takes one argument (TDSRow)".format(i=index)
f"Error at extend function at index {index} (0-indexed). "
"Each extend function should be a lambda which takes one argument (TDSRow)"
)
if not isinstance(name, str):
raise TypeError(
"Error at extend column name at index {i} (0-indexed). "
"Column name should be a string".format(i=index)
f"Error at extend column name at index {index} (0-indexed). "
"Column name should be a string"
)

try:
result = func(tds_row)
except Exception as e:
raise RuntimeError(
"Extend function at index {i} (0-indexed) incompatible. "
"Error occurred while evaluating. Message: {e}".format(i=index, e=str(e))
f"Extend function at index {index} (0-indexed) incompatible. "
f"Error occurred while evaluating. Message: {str(e)}"
) from e

if not isinstance(result, (int, float, bool, str, PyLegendPrimitive)):
raise ValueError(
"Extend function at index {i} (0-indexed) incompatible. "
"Returns non-primitive - {r}".format(i=index, r=str(type(result)))
f"Extend function at index {index} (0-indexed) incompatible. "
f"Returns non-primitive - {str(type(result))}"
)

index += 1

if len(self.__column_names_list) != len(set(self.__column_names_list)):
raise ValueError("Extend column names list has duplicates: {c}".format(c=self.__column_names_list))
raise ValueError(f"Extend column names list has duplicates: {self.__column_names_list}")

base_cols = [c.get_name() for c in self.__base_frame.columns()]
for c in self.__column_names_list:
if c in base_cols:
raise ValueError("Extend column name - '{c}' already exists in base frame".format(c=c))
raise ValueError(f"Extend column name - '{c}' already exists in base frame")

return True
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,22 @@ def validate(self) -> bool:
for c in self.__column_names_self:
if c not in left_cols:
raise ValueError(
"Column - '{col}' in join columns list doesn't exist in the left frame being joined. "
"Current left frame columns: {cols}".format(
col=c,
cols=left_cols
)
f"Column - '{c}' in join columns list doesn't exist in the left frame being joined. "
f"Current left frame columns: {left_cols}"
)

right_cols = [c.get_name() for c in self.__other_frame.columns()]
for c in self.__column_names_other:
if c not in right_cols:
raise ValueError(
"Column - '{col}' in join columns list doesn't exist in the right frame being joined. "
"Current right frame columns: {cols}".format(
col=c,
cols=right_cols
)
f"Column - '{c}' in join columns list doesn't exist in the right frame being joined. "
f"Current right frame columns: {right_cols}"
)

if len(self.__column_names_self) != len(self.__column_names_other):
raise ValueError(
"For join_by_columns function, column lists should be of same size. "
"Passed column list sizes - Left: {l}, Right: {r}".format(
l=len(self.__column_names_self),
r=len(self.__column_names_other)
)
f"Passed column list sizes - Left: {len(self.__column_names_self)}, Right: {len(self.__column_names_other)}"
)

if len(self.__column_names_self) == 0:
Expand All @@ -217,10 +208,7 @@ def validate(self) -> bool:

if left_col.get_type() != right_col.get_type():
raise ValueError(
"Trying to join on columns with different types - Left Col: {l}, Right Col: {r}".format(
l=left_col,
r=right_col
)
f"Trying to join on columns with different types - Left Col: {left_col}, Right Col: {right_col}"
)

if x == y:
Expand All @@ -235,18 +223,12 @@ def validate(self) -> bool:
if len(final_cols) != len(set(final_cols)):
raise ValueError(
"Found duplicate columns in joined frames (which are not join keys). "
"Columns - Left Frame: {l}, Right Frame: {r}, Common Join Keys: {j}".format(
l=left_cols,
r=right_cols,
j=common_join_cols
)
f"Columns - Left Frame: {left_cols}, Right Frame: {right_cols}, Common Join Keys: {common_join_cols}"
)

if self.__join_type.lower() not in ('inner', 'left_outer', 'right_outer', 'leftouter', 'rightouter'):
raise ValueError(
"Unknown join type - {j}. Supported types are - INNER, LEFT_OUTER, RIGHT_OUTER".format(
j=self.__join_type
)
f"Unknown join type - {self.__join_type}. Supported types are - INNER, LEFT_OUTER, RIGHT_OUTER"
)

return True
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,12 @@ def validate(self) -> bool:
raise ValueError(
"Found duplicate columns in joined frames. Either use join_by_columns function if joining on shared "
"columns or use rename_columns function to ensure there are no duplicate columns in joined frames. "
"Columns - Left Frame: {l}, Right Frame: {r}".format(
l=left_cols,
r=right_cols
)
f"Columns - Left Frame: {left_cols}, Right Frame: {right_cols}"
)

if self.__join_type.lower() not in ('inner', 'left_outer', 'right_outer', 'leftouter', 'rightouter'):
raise ValueError(
"Unknown join type - {j}. Supported types are - INNER, LEFT_OUTER, RIGHT_OUTER".format(
j=self.__join_type
)
f"Unknown join type - {self.__join_type}. Supported types are - INNER, LEFT_OUTER, RIGHT_OUTER"
)

return True
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,20 @@ def validate(self) -> bool:
if len(self.__column_names) != len(self.__renamed_column_names):
raise ValueError(
"column_names list and renamed_column_names list should have same size when renaming columns.\n"
"column_names list - (Count: {c1}) - {cols1}\n"
"renamed_column_names_list - (Count: {c2}) - {cols2}\n".format(
c1=len(self.__column_names),
cols1=self.__column_names,
c2=len(self.__renamed_column_names),
cols2=self.__renamed_column_names
)
f"column_names list - (Count: {len(self.__column_names)}) - {self.__column_names}\n"
f"renamed_column_names_list - (Count: {len(self.__renamed_column_names)}) - {self.__renamed_column_names}\n"
)

if len(self.__column_names) != len(set(self.__column_names)):
raise ValueError(
"column_names list shouldn't have duplicates when renaming columns.\n"
"column_names list - (Count: {c1}) - {cols1}\n".format(
c1=len(self.__column_names),
cols1=self.__column_names
)
f"column_names list - (Count: {len(self.__column_names)}) - {self.__column_names}\n"
)

if len(self.__renamed_column_names) != len(set(self.__renamed_column_names)):
raise ValueError(
"renamed_column_names_list list shouldn't have duplicates when renaming columns.\n"
"renamed_column_names_list - (Count: {c2}) - {cols2}\n".format(
c2=len(self.__renamed_column_names),
cols2=self.__renamed_column_names
)
f"renamed_column_names_list - (Count: {len(self.__renamed_column_names)}) - {self.__renamed_column_names}\n"
)

return True
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ def validate(self) -> bool:
break
if not found_col:
raise ValueError(
"Column - '{col}' in restrict columns list doesn't exist in the current frame. "
"Current frame columns: {cols}".format(
col=c,
cols=[x.get_name() for x in base_columns]
)
f"Column - '{c}' in restrict columns list doesn't exist in the current frame. "
f"Current frame columns: {[x.get_name() for x in base_columns]}"
)
return True
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ def validate(self) -> bool:
)
if self.__end_row <= self.__start_row:
raise ValueError("End row argument of slice function cannot be less than or equal to start row argument. "
"Start row: {s}, End row: {e}".format(s=self.__start_row, e=self.__end_row))
f"Start row: {self.__start_row}, End row: {self.__end_row}")
return True
14 changes: 5 additions & 9 deletions pylegend/core/tds/legend_api/frames/functions/sort_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,17 @@ def validate(self) -> bool:
for c in self.__column_name_list:
if c not in base_cols:
raise ValueError(
"Column - '{col}' in sort columns list doesn't exist in the current frame. "
"Current frame columns: {cols}".format(
col=c,
cols=base_cols
)
f"Column - '{c}' in sort columns list doesn't exist in the current frame. "
f"Current frame columns: {base_cols}"
)

if (self.__directions is not None) and (len(self.__directions) > 0):
if len(self.__column_name_list) != len(self.__directions):
cols = self.__column_name_list
dirs = self.__directions
raise ValueError(
"Sort directions (ASC/DESC) provided need to be in sync with columns or left empty to "
"choose defaults. Passed column list: {cols}, directions: {dirs}".format(
cols=self.__column_name_list,
dirs=self.__directions
)
f"choose defaults. Passed column list: {cols}, directions: {dirs}"
)

for d in self.__directions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ class LegendApiBaseTdsFrame(LegendApiTdsFrame, metaclass=ABCMeta):
def __init__(self, columns: PyLegendSequence[TdsColumn]) -> None:
col_names = [c.get_name() for c in columns]
if len(col_names) != len(set(col_names)):
raise ValueError("TdsFrame cannot have duplicated column names. Passed columns: {cols}".format(
cols="[" + ", ".join([str(c) for c in columns]) + "]"
))
cols = "[" + ", ".join([str(c) for c in columns]) + "]"
raise ValueError(f"TdsFrame cannot have duplicated column names. Passed columns: {cols}")
self.__columns = [c.copy() for c in columns]

def columns(self) -> PyLegendSequence[TdsColumn]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ class PandasApiBaseTdsFrame(PandasApiTdsFrame, metaclass=ABCMeta):
def __init__(self, columns: PyLegendSequence[TdsColumn]) -> None:
col_names = [c.get_name() for c in columns]
if len(col_names) != len(set(col_names)):
raise ValueError("TdsFrame cannot have duplicated column names. Passed columns: {cols}".format(
cols="[" + ", ".join([str(c) for c in columns]) + "]"
))
cols = "[" + ", ".join([str(c) for c in columns]) + "]"
raise ValueError(f"TdsFrame cannot have duplicated column names. Passed columns: {cols}")
self.__columns = [c.copy() for c in columns]

def columns(self) -> PyLegendSequence[TdsColumn]:
Expand Down
2 changes: 1 addition & 1 deletion pylegend/core/tds/tds_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def copy_with_changed_name(self, new_name: str) -> "TdsColumn":
pass

def __str__(self) -> str:
return "TdsColumn(Name: {name}, Type: {_type})".format(name=self.get_name(), _type=self.get_type())
return f"TdsColumn(Name: {self.get_name()}, Type: {self.get_type()})"


class PrimitiveType(Enum):
Expand Down

0 comments on commit a69d8df

Please sign in to comment.