From dae1fa092977a65ad3dea0e3dc069876faf8ca6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Roos?= <105842014+roosre@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:54:21 +0200 Subject: [PATCH] CompositeFiles: convert rst always into a list. (#347) * convert rst always into a list. Users were manually setting the rst as a pure path. * improve descriptions * implement suggestions from the code review and add unit test --- README.rst | 12 +++++---- pyproject.toml | 2 +- src/ansys/dpf/composites/data_sources.py | 32 +++++++++++++++++++++--- tests/test_data_sources.py | 4 +++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index 8301e4596..6776ffedc 100644 --- a/README.rst +++ b/README.rst @@ -33,11 +33,12 @@ PyDPF Composites :alt: Black -PyDPF Composites is a Python wrapper for Ansys DPF composites. It implements -classes on top of DPF Composites operators and data accessors for short -fiber and layered composites (layered shell and solid elements). This module -can be used to postprocess fiber reinforced plastics and layered composites and -to implement custom failure criteria and computation. For information demonstrating +PyDPF Composites enables the post-processing of composite structures based on +`Ansys DPF`_ and the DPF Composites plugin. It implements classes on top of +DPF Composites operators and data accessors for short fiber and layered +composites (layered shell and solid elements). This module can be used to +postprocess fiber reinforced plastics and layered composites, and to implement +custom failure criteria and computation. For information demonstrating the behavior and usage of PyDPF Composites, see `Examples`_ in the DPF Composite documentation. @@ -196,3 +197,4 @@ released versions. .. _tox: https://tox.wiki/ .. _Examples: https://composites.dpf.docs.pyansys.com/dev/examples/index.html .. _Getting The DPF Server Docker Image: https://composites.dpf.docs.pyansys.com/version/stable/intro.html#getting-the-dpf-server-docker-image +.. _Ansys DPF: https://dpf.docs.pyansys.com/version/stable/ \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index eb5866287..88ac38758 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api" # Check https://python-poetry.org/docs/pyproject/ for all available sections name = "ansys-dpf-composites" version = "0.3.dev0" -description = "A python wrapper for ansys dpf composites" +description = "Post-processing of composite structures based on Ansys DPF" license = "MIT" authors = ["ANSYS, Inc. "] maintainers = ["PyAnsys developers "] diff --git a/src/ansys/dpf/composites/data_sources.py b/src/ansys/dpf/composites/data_sources.py index 2beb0e2af..ed492d021 100644 --- a/src/ansys/dpf/composites/data_sources.py +++ b/src/ansys/dpf/composites/data_sources.py @@ -75,13 +75,25 @@ def __init__( True if files are on the local machine, False if they have already been uploaded to the DPF server.. """ - if isinstance(rst, (str, pathlib.Path)): - rst = [rst] self.rst = rst # type: ignore self.composite = composite self.engineering_data = engineering_data self.files_are_local = files_are_local + # The constructor pretends that rst can also be just a path + # but the property rst must be a list + def __setattr__(self, prop, val): # type: ignore + """Convert values if needed.""" + if prop == "rst": + val = self._get_rst_list(val) + super().__setattr__(prop, val) + + @staticmethod + def _get_rst_list(value: Union[List[_PATH], _PATH]) -> List[_PATH]: + if isinstance(value, (str, pathlib.Path)): + value = [value] + return value # type: ignore + @dataclass class ShortFiberCompositesFiles: @@ -116,13 +128,25 @@ def __init__( True if files are on the local machine, False if they have already been uploaded to the DPF server.. """ - if isinstance(rst, (str, pathlib.Path)): - rst = [rst] self.rst = rst # type: ignore self.dsdat = dsdat self.engineering_data = engineering_data self.files_are_local = files_are_local + # The constructor pretends that rst can also be just a path + # but the property rst must be a list. + def __setattr__(self, prop, val): # type: ignore + """Convert values if needed.""" + if prop == "rst": + val = self._get_rst_list(val) + super().__setattr__(prop, val) + + @staticmethod + def _get_rst_list(value: Union[List[_PATH], _PATH]) -> List[_PATH]: + if isinstance(value, (str, pathlib.Path)): + value = [value] + return value # type: ignore + # roosre June 2023: todo add deprecation warning where composite definition label is used @dataclass(frozen=True) diff --git a/tests/test_data_sources.py b/tests/test_data_sources.py index 1cdaa860f..bcdc9ee9f 100644 --- a/tests/test_data_sources.py +++ b/tests/test_data_sources.py @@ -57,3 +57,7 @@ def test_get_files_from_result_folder_harmonic(dpf_server): assert files.rst == [WORKFLOW_EXAMPLE_ROOT_HARMONIC / "file.rst"] assert files.engineering_data == WORKFLOW_EXAMPLE_ROOT_HARMONIC / "MatML.xml" + + # ensure that the setter of RST converts the input into a list + files.rst = WORKFLOW_EXAMPLE_ROOT_HARMONIC / "file.rst" + assert files.rst == [WORKFLOW_EXAMPLE_ROOT_HARMONIC / "file.rst"]