From a69d8dfd7e9984f45425d0852d37fb167d13d04a Mon Sep 17 00:00:00 2001 From: gs-ssh16 Date: Thu, 19 Oct 2023 00:31:23 +0530 Subject: [PATCH] Replace remaining format function usages with python f-strings (2) --- .../frames/functions/concatenate_function.py | 19 ++++------- .../frames/functions/extend_function.py | 26 +++++++------- .../functions/join_by_columns_function.py | 34 +++++-------------- .../frames/functions/join_function.py | 9 ++--- .../functions/rename_columns_function.py | 19 +++-------- .../frames/functions/restrict_function.py | 7 ++-- .../frames/functions/slice_function.py | 2 +- .../frames/functions/sort_function.py | 14 +++----- .../frames/legend_api_base_tds_frame.py | 5 ++- .../frames/pandas_api_base_tds_frame.py | 5 ++- pylegend/core/tds/tds_column.py | 2 +- 11 files changed, 45 insertions(+), 97 deletions(-) diff --git a/pylegend/core/tds/legend_api/frames/functions/concatenate_function.py b/pylegend/core/tds/legend_api/frames/functions/concatenate_function.py index c94c058e..a7269649 100644 --- a/pylegend/core/tds/legend_api/frames/functions/concatenate_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/concatenate_function.py @@ -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)): @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/extend_function.py b/pylegend/core/tds/legend_api/frames/functions/extend_function.py index 74dcce34..4f6bb61a 100644 --- a/pylegend/core/tds/legend_api/frames/functions/extend_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/extend_function.py @@ -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) @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/join_by_columns_function.py b/pylegend/core/tds/legend_api/frames/functions/join_by_columns_function.py index 43778e05..d1d5d0c5 100644 --- a/pylegend/core/tds/legend_api/frames/functions/join_by_columns_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/join_by_columns_function.py @@ -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: @@ -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: @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/join_function.py b/pylegend/core/tds/legend_api/frames/functions/join_function.py index 1e57233b..e0769913 100644 --- a/pylegend/core/tds/legend_api/frames/functions/join_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/join_function.py @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/rename_columns_function.py b/pylegend/core/tds/legend_api/frames/functions/rename_columns_function.py index 585c44e4..c8b4371b 100644 --- a/pylegend/core/tds/legend_api/frames/functions/rename_columns_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/rename_columns_function.py @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/restrict_function.py b/pylegend/core/tds/legend_api/frames/functions/restrict_function.py index a4707f96..36a770db 100644 --- a/pylegend/core/tds/legend_api/frames/functions/restrict_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/restrict_function.py @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/slice_function.py b/pylegend/core/tds/legend_api/frames/functions/slice_function.py index 84b6cf76..73f8753b 100644 --- a/pylegend/core/tds/legend_api/frames/functions/slice_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/slice_function.py @@ -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 diff --git a/pylegend/core/tds/legend_api/frames/functions/sort_function.py b/pylegend/core/tds/legend_api/frames/functions/sort_function.py index 572f8f96..120a0037 100644 --- a/pylegend/core/tds/legend_api/frames/functions/sort_function.py +++ b/pylegend/core/tds/legend_api/frames/functions/sort_function.py @@ -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: diff --git a/pylegend/core/tds/legend_api/frames/legend_api_base_tds_frame.py b/pylegend/core/tds/legend_api/frames/legend_api_base_tds_frame.py index 57fe3044..42e2dd70 100644 --- a/pylegend/core/tds/legend_api/frames/legend_api_base_tds_frame.py +++ b/pylegend/core/tds/legend_api/frames/legend_api_base_tds_frame.py @@ -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]: diff --git a/pylegend/core/tds/pandas_api/frames/pandas_api_base_tds_frame.py b/pylegend/core/tds/pandas_api/frames/pandas_api_base_tds_frame.py index 7dab952b..d9d59435 100644 --- a/pylegend/core/tds/pandas_api/frames/pandas_api_base_tds_frame.py +++ b/pylegend/core/tds/pandas_api/frames/pandas_api_base_tds_frame.py @@ -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]: diff --git a/pylegend/core/tds/tds_column.py b/pylegend/core/tds/tds_column.py index c825b4f5..940f6bce 100644 --- a/pylegend/core/tds/tds_column.py +++ b/pylegend/core/tds/tds_column.py @@ -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):