From 082657cf467799d85db9d5ce9030b47a3e78490f Mon Sep 17 00:00:00 2001 From: "Alex H. Room" <69592136+alexhroom@users.noreply.github.com> Date: Thu, 9 Jan 2025 14:45:46 +0000 Subject: [PATCH] reformatted on ruff 0.9 (#113) --- RATapi/classlist.py | 4 +- RATapi/controls.py | 6 +-- .../languages/run_custom_file_languages.py | 6 +-- RATapi/models.py | 2 +- RATapi/project.py | 6 +-- RATapi/run.py | 2 +- RATapi/utils/plotting.py | 2 +- tests/test_controls.py | 40 +++++++++---------- tests/test_convert.py | 8 ++-- tests/test_models.py | 24 +++++------ tests/test_project.py | 6 +-- 11 files changed, 53 insertions(+), 53 deletions(-) diff --git a/RATapi/classlist.py b/RATapi/classlist.py index 9859649c..2ec2378c 100644 --- a/RATapi/classlist.py +++ b/RATapi/classlist.py @@ -68,7 +68,7 @@ def __str__(self): [ [index] + list( - f"{'Data array: ['+' x '.join(str(i) for i in v.shape) if v.size > 0 else '['}]" + f"{'Data array: [' + ' x '.join(str(i) for i in v.shape) if v.size > 0 else '['}]" if isinstance(v, np.ndarray) else "\n".join(element for element in v) if k == "model" @@ -364,7 +364,7 @@ def _check_unique_name_fields(self, input_list: Sequence[T]) -> None: if new_indices: new_list = ", ".join(str(i) for i in new_indices[:-1]) new_string = ( - f" item{f's {new_list} and ' if new_list else ' '}" f"{new_indices[-1]} of the input list" + f" item{f's {new_list} and ' if new_list else ' '}{new_indices[-1]} of the input list" ) error_list.append( f" '{name}' is shared between{existing_string}" diff --git a/RATapi/controls.py b/RATapi/controls.py index 88ca5f91..4d38bb80 100644 --- a/RATapi/controls.py +++ b/RATapi/controls.py @@ -92,8 +92,8 @@ def warn_setting_incorrect_properties(self, handler: ValidatorFunctionWrapHandle procedure = input_dict.get("procedure", Procedures.Calculate) custom_error_msgs = { "extra_forbidden": f'Extra inputs are not permitted. The fields for the "{procedure}"' - f' controls procedure are:\n ' - f'{", ".join(fields.get("procedure", []))}\n', + f" controls procedure are:\n " + f"{', '.join(fields.get('procedure', []))}\n", } custom_error_list = custom_pydantic_validation_error(exc.errors(), custom_error_msgs) raise ValidationError.from_exception_data(exc.title, custom_error_list, hide_input=True) from None @@ -116,7 +116,7 @@ def warn_setting_incorrect_properties(self, handler: ValidatorFunctionWrapHandle f'\nThe current controls procedure is "{new_procedure}", but the property' f' "{field}" applies instead to the {", ".join(incorrect_procedures)} procedure.\n\n' f' The fields for the "{new_procedure}" controls procedure are:\n' - f' {", ".join(fields[new_procedure])}\n', + f" {', '.join(fields[new_procedure])}\n", stacklevel=2, ) diff --git a/RATapi/examples/languages/run_custom_file_languages.py b/RATapi/examples/languages/run_custom_file_languages.py index e343d68d..f91c0317 100644 --- a/RATapi/examples/languages/run_custom_file_languages.py +++ b/RATapi/examples/languages/run_custom_file_languages.py @@ -17,7 +17,7 @@ start = time.time() project, results = RAT.run(project, controls) end = time.time() -print(f"Python time is: {end-start}s\n") +print(f"Python time is: {end - start}s\n") RAT.plotting.plot_ref_sld(project, results) @@ -27,7 +27,7 @@ start = time.time() project, results = RAT.run(project, controls) end = time.time() -print(f"Matlab time is: {end-start}s\n") +print(f"Matlab time is: {end - start}s\n") RAT.plotting.plot_ref_sld(project, results) @@ -37,6 +37,6 @@ start = time.time() project, results = RAT.run(project, controls) end = time.time() -print(f"C++ time is: {end-start}s\n") +print(f"C++ time is: {end - start}s\n") RAT.plotting.plot_ref_sld(project, results) diff --git a/RATapi/models.py b/RATapi/models.py index fcc0710c..3c76840b 100644 --- a/RATapi/models.py +++ b/RATapi/models.py @@ -274,7 +274,7 @@ def __eq__(self, other: object) -> bool: def __str__(self): table = prettytable.PrettyTable() table.field_names = [key.replace("_", " ") for key in self.__dict__] - array_entry = f"{'Data array: ['+' x '.join(str(i) for i in self.data.shape) if self.data.size > 0 else '['}]" + array_entry = f"{'Data array: [' + ' x '.join(str(i) for i in self.data.shape) if self.data.size > 0 else '['}]" table.add_row([self.name, array_entry, self.data_range, self.simulation_range]) return table.get_string() diff --git a/RATapi/project.py b/RATapi/project.py index 9751128f..95626436 100644 --- a/RATapi/project.py +++ b/RATapi/project.py @@ -299,7 +299,7 @@ def check_layers(cls, value: ClassList, info: ValidationInfo): if not all(isinstance(element, model) for element in value): raise ValueError( f'"The layers attribute contains {other_model}s, ' - f'but the absorption parameter is {info.data["absorption"]}. ' + f"but the absorption parameter is {info.data['absorption']}. " f'The attribute should be a ClassList of {model_name} instead."' ) @@ -568,7 +568,7 @@ def check_protected_parameters(self) -> "Project": removed_params = [ param for param in self._protected_parameters[class_list] if param not in protected_parameters ] - raise ValueError(f'Can\'t delete the protected parameters: {", ".join(str(i) for i in removed_params)}') + raise ValueError(f"Can't delete the protected parameters: {', '.join(str(i) for i in removed_params)}") self._protected_parameters = self.get_all_protected_parameters() return self @@ -576,7 +576,7 @@ def __str__(self): output = "" for key, value in self.__dict__.items(): if value: - output += f'{key.replace("_", " ").title() + ": " :-<100}\n\n' + output += f"{key.replace('_', ' ').title() + ': ':-<100}\n\n" try: output += value.value + "\n\n" # For enums except AttributeError: diff --git a/RATapi/run.py b/RATapi/run.py index f2a2e017..d60b76e0 100644 --- a/RATapi/run.py +++ b/RATapi/run.py @@ -120,7 +120,7 @@ def run(project, controls): end = time.time() if display_on: - print(f"Elapsed time is {end-start:.3f} seconds\n") + print(f"Elapsed time is {end - start:.3f} seconds\n") results = make_results(controls.procedure, output_results, bayes_results) diff --git a/RATapi/utils/plotting.py b/RATapi/utils/plotting.py index e9a6b65e..e26d6b69 100644 --- a/RATapi/utils/plotting.py +++ b/RATapi/utils/plotting.py @@ -138,7 +138,7 @@ def plot_ref_sld_helper( # Plot the slds on plot (1,2) for j in range(len(sld)): - label = name if len(sld) == 1 else f"{name} Domain {j+1}" + label = name if len(sld) == 1 else f"{name} Domain {j + 1}" sld_plot.plot(sld[j][:, 0], sld[j][:, 1], label=label, linewidth=1) # Plot confidence intervals if required diff --git a/tests/test_controls.py b/tests/test_controls.py index 0f6eba53..73b776bd 100644 --- a/tests/test_controls.py +++ b/tests/test_controls.py @@ -132,9 +132,9 @@ def test_initialise_non_calculate_properties(self, wrong_property: str, value: A UserWarning, match=f'\nThe current controls procedure is "calculate", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "calculate" controls procedure are:\n' - f' {", ".join(fields["calculate"])}\n', + f" {', '.join(fields['calculate'])}\n", ): Controls(procedure=Procedures.Calculate, **{wrong_property: value}) @@ -171,9 +171,9 @@ def test_set_non_calculate_properties(self, wrong_property: str, value: Any) -> UserWarning, match=f'\nThe current controls procedure is "calculate", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "calculate" controls procedure are:\n' - f' {", ".join(fields["calculate"])}\n', + f" {', '.join(fields['calculate'])}\n", ): setattr(self.calculate, wrong_property, value) @@ -301,9 +301,9 @@ def test_initialise_non_simplex_properties(self, wrong_property: str, value: Any UserWarning, match=f'\nThe current controls procedure is "simplex", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "simplex" controls procedure are:\n' - f' {", ".join(fields["simplex"])}\n', + f" {', '.join(fields['simplex'])}\n", ): Controls(procedure=Procedures.Simplex, **{wrong_property: value}) @@ -334,9 +334,9 @@ def test_set_non_simplex_properties(self, wrong_property: str, value: Any) -> No UserWarning, match=f'\nThe current controls procedure is "simplex", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "simplex" controls procedure are:\n' - f' {", ".join(fields["simplex"])}\n', + f" {', '.join(fields['simplex'])}\n", ): setattr(self.simplex, wrong_property, value) @@ -458,9 +458,9 @@ def test_initialise_non_de_properties(self, wrong_property: str, value: Any) -> UserWarning, match=f'\nThe current controls procedure is "de", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "de" controls procedure are:\n' - f' {", ".join(fields["de"])}\n', + f" {', '.join(fields['de'])}\n", ): Controls(procedure=Procedures.DE, **{wrong_property: value}) @@ -489,9 +489,9 @@ def test_set_non_de_properties(self, wrong_property: str, value: Any) -> None: UserWarning, match=f'\nThe current controls procedure is "de", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "de" controls procedure are:\n' - f' {", ".join(fields["de"])}\n', + f" {', '.join(fields['de'])}\n", ): setattr(self.de, wrong_property, value) @@ -627,9 +627,9 @@ def test_initialise_non_ns_properties(self, wrong_property: str, value: Any) -> UserWarning, match=f'\nThe current controls procedure is "ns", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "ns" controls procedure are:\n' - f' {", ".join(fields["ns"])}\n', + f" {', '.join(fields['ns'])}\n", ): Controls(procedure=Procedures.NS, **{wrong_property: value}) @@ -662,9 +662,9 @@ def test_set_non_ns_properties(self, wrong_property: str, value: Any) -> None: UserWarning, match=f'\nThe current controls procedure is "ns", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "ns" controls procedure are:\n' - f' {", ".join(fields["ns"])}\n', + f" {', '.join(fields['ns'])}\n", ): setattr(self.ns, wrong_property, value) @@ -797,9 +797,9 @@ def test_initialise_non_dream_properties(self, wrong_property: str, value: Any) UserWarning, match=f'\nThe current controls procedure is "dream", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "dream" controls procedure are:\n' - f' {", ".join(fields["dream"])}\n', + f" {', '.join(fields['dream'])}\n", ): Controls(procedure=Procedures.DREAM, **{wrong_property: value}) @@ -830,9 +830,9 @@ def test_set_non_dream_properties(self, wrong_property: str, value: Any) -> None UserWarning, match=f'\nThe current controls procedure is "dream", but the property' f' "{wrong_property}" applies instead to the' - f' {", ".join(incorrect_procedures)} procedure.\n\n' + f" {', '.join(incorrect_procedures)} procedure.\n\n" f' The fields for the "dream" controls procedure are:\n' - f' {", ".join(fields["dream"])}\n', + f" {', '.join(fields['dream'])}\n", ): setattr(self.dream, wrong_property, value) diff --git a/tests/test_convert.py b/tests/test_convert.py index ee97832e..b8ca45dc 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -33,15 +33,15 @@ def dspc_bilayer(): param = project.background_parameters[i] for background in project.backgrounds: if background.value_1 == param.name: - background.value_1 = f"Background parameter {i+1}" - param.name = f"Background parameter {i+1}" + background.value_1 = f"Background parameter {i + 1}" + param.name = f"Background parameter {i + 1}" for i in range(0, len(project.resolution_parameters)): param = project.resolution_parameters[i] for resolution in project.resolutions: if resolution.value_1 == param.name: - resolution.value_1 = f"Resolution parameter {i+1}" - param.name = f"Resolution parameter {i+1}" + resolution.value_1 = f"Resolution parameter {i + 1}" + param.name = f"Resolution parameter {i + 1}" return project diff --git a/tests/test_models.py b/tests/test_models.py index 850e2cc8..f207955d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -70,7 +70,7 @@ def test_initialise_with_wrong_type(self, model: Callable, model_params: dict) - """When initialising a model with the wrong type for the "name" field, we should raise a ValidationError.""" with pytest.raises( pydantic.ValidationError, - match=f"1 validation error for {model.__name__}\nname\n " f"Input should be a valid string", + match=f"1 validation error for {model.__name__}\nname\n Input should be a valid string", ): model(name=1, **model_params) @@ -79,7 +79,7 @@ def test_assignment_with_wrong_type(self, model: Callable, model_params: dict) - test_model = model(**model_params) with pytest.raises( pydantic.ValidationError, - match=f"1 validation error for {model.__name__}\nname\n " f"Input should be a valid string", + match=f"1 validation error for {model.__name__}\nname\n Input should be a valid string", ): test_model.name = 1 @@ -87,7 +87,7 @@ def test_initialise_with_zero_length_name(self, model: Callable, model_params: d """When initialising a model with a zero length name, we should raise a ValidationError.""" with pytest.raises( pydantic.ValidationError, - match=f"1 validation error for {model.__name__}\nname\n " f"String should have at least 1 character", + match=f"1 validation error for {model.__name__}\nname\n String should have at least 1 character", ): model(name="", **model_params) @@ -95,7 +95,7 @@ def test_initialise_with_extra_fields(self, model: Callable, model_params: dict) """When initialising a model with unspecified fields, we should raise a ValidationError.""" with pytest.raises( pydantic.ValidationError, - match=f"1 validation error for {model.__name__}\nnew_field\n " f"Extra inputs are not permitted", + match=f"1 validation error for {model.__name__}\nnew_field\n Extra inputs are not permitted", ): model(new_field=1, **model_params) @@ -134,7 +134,7 @@ def test_data_too_few_dimensions(input_data: np.ndarray[float]) -> None: """ with pytest.raises( pydantic.ValidationError, - match='1 validation error for Data\ndata\n Value error, "data" must ' "have at least two dimensions", + match='1 validation error for Data\ndata\n Value error, "data" must have at least two dimensions', ): RATapi.models.Data(data=input_data) @@ -153,7 +153,7 @@ def test_data_too_few_values(input_data: np.ndarray[float]) -> None: """ with pytest.raises( pydantic.ValidationError, - match='1 validation error for Data\ndata\n Value error, "data" must ' "have at least three columns", + match='1 validation error for Data\ndata\n Value error, "data" must have at least three columns', ): RATapi.models.Data(data=input_data) @@ -184,9 +184,9 @@ def test_two_values_in_data_range(input_range: list[float]) -> None: """ with pytest.raises( pydantic.ValidationError, - match=f'1 validation error for Data\ndata_range\n List should have ' - f'at {"least" if len(input_range) < 2 else "most"} 2 items ' - f'after validation, not {len(input_range)}', + match=f"1 validation error for Data\ndata_range\n List should have " + f"at {'least' if len(input_range) < 2 else 'most'} 2 items " + f"after validation, not {len(input_range)}", ): RATapi.models.Data(data_range=input_range) @@ -205,9 +205,9 @@ def test_two_values_in_simulation_range(input_range: list[float]) -> None: """ with pytest.raises( pydantic.ValidationError, - match=f'1 validation error for Data\nsimulation_range\n List should ' - f'have at {"least" if len(input_range) < 2 else "most"} 2 items ' - f'after validation, not {len(input_range)}', + match=f"1 validation error for Data\nsimulation_range\n List should " + f"have at {'least' if len(input_range) < 2 else 'most'} 2 items " + f"after validation, not {len(input_range)}", ): RATapi.models.Data(simulation_range=input_range) diff --git a/tests/test_project.py b/tests/test_project.py index f8c767d8..6bb14326 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -403,7 +403,7 @@ def test_assign_models(test_project, field: str, model_params: dict) -> None: input_model = model_classes[field] with pytest.raises( pydantic.ValidationError, - match=f"1 validation error for Project\n{field}\n Input should be " f"an instance of ClassList", + match=f"1 validation error for Project\n{field}\n Input should be an instance of ClassList", ): setattr(test_project, field, input_model(**model_params)) @@ -865,7 +865,7 @@ def test_allowed_contrast_models( """ with pytest.raises( pydantic.ValidationError, - match=f'1 validation error for Project\n Value error, The values: ' + match=f"1 validation error for Project\n Value error, The values: " f'"{", ".join(test_contrast.model)}" in the "model" field of ' f'"contrasts" must be defined in "{field_name}".', ): @@ -1036,7 +1036,7 @@ def test_check_allowed_background_resolution_values_on_data_list(test_value: str ) with pytest.raises( ValueError, - match=f'The value "{test_value}" in the "value_1" field of "backgrounds" must be ' f'defined in "data".', + match=f'The value "{test_value}" in the "value_1" field of "backgrounds" must be defined in "data".', ): project.check_allowed_background_resolution_values( "backgrounds", ["value_1"], ["Background Param 1"], ["Simulation"]