Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: Leverage pathlib instead of os.path #5180

Open
SMoraisAnsys opened this issue Sep 18, 2024 · 2 comments · May be fixed by #5842
Open

REFACTOR: Leverage pathlib instead of os.path #5180

SMoraisAnsys opened this issue Sep 18, 2024 · 2 comments · May be fixed by #5842
Assignees
Labels
good first issue Easy to solve for newcomers

Comments

@SMoraisAnsys
Copy link
Collaborator

We have deprecated IronPython so we should be able to use pathlib instead of working with os.path.

Updating this will improve readability and follow the push from the Python ecosystem.
This should be a good entry point for new contributors.

@SMoraisAnsys SMoraisAnsys added the good first issue Easy to solve for newcomers label Sep 18, 2024
@gmalinve gmalinve self-assigned this Oct 10, 2024
@jvela018
Copy link
Contributor

@gmalinve you can assign this issue to me. Thanks

@jvela018
Copy link
Contributor

jvela018 commented Feb 18, 2025

List of files containing os.path / Completed refactoring

  • maxwell.py
  • twinbuilder.py
  • aedt_logger.py
  • circuit.py
  • circuit_netlist.py
  • common_rpc.py
  • desktop.py
  • downloads.py
  • configurations.py
  • filesystem.py
  • general_methods.py
  • grpc_plugin_dll_class.py
  • ibis_reader.py
  • load_aedt_file.py
  • settings.py
  • hfss.py
  • hfss3dlayout.py
  • icepak.py
  • maxwellcircuit.py
  • object_3d.py
  • modeler_pcb.py
  • q3d.py
  • local_server.py
  • rpyc_services.py
  • primitives_circuit.py
  • filesystem.py
  • analysis_3d.py
  • add more as needed...!

Refactoring mapping

  • To map from os.path to pathlib (Path) follow the table at the end of the pathlib documentation as a reference. However, note that in our case we're not using PurePath:
    https://docs.python.org/3/library/pathlib.html

  • Most of the mapping is straight forward but some of them will require some extra effort or completely changing the syntax. For example getting the extension of a file will be done through suffix instead of splitting the path and extracting the extension as the argument "1" of a list or the stem for the name of the file without extension. For example:

    • Old: os.path.splitext(output_file)[1], New: Path(output_file).suffix
    • Old: os.path.splitext(os.path.basename(source_project))[0], New: Path(source_project).stem
  • If you refactor any of the files in the list check the box.

  • If you are uncertain on how to map the functions from one module to the other feel free to ping me.

Reference table

Feel free to add new rows for those tricky patterns that may pop up in multiple files

os.path pathlib
os.path.splitext(output_file)[0] Path(output_file).stem
os.path.splitext(output_file)[1] Path(output_file).suffix
os.path.splitext(os.path.basename(source_project))[0], Path(source_project).stem
os.path.join(self.results_directory,self.design_name + ".aedt") Path(self.results_directory) / str(self.design_name + ".aedt")
os.path.join(self.results_directory,self.filename) Path(self.results_directory) / self.filename
os.path.exists(os.path.normpath(input_file)) Path(input_file).exists()
os.mkdir(input_file) Path(input_file).mkdir()
files = search_files(results_path, "*.log") latest_file = max(files, key=os.path.getctime) files = search_files(str(results_path), "*.log") files = [Path(f) for f in files] latest_file = max(files, key=lambda f: str(f.stat().st_ctime))
os.path.basename(latest_file) Path(latest_file).name
os.path.dirname(__file__) Path.cwd()
os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) Path.cwd().parent
os.path.dirname(os.path.realpath(__file__)) Path.cwd()
os.path.realpath(os.path.join(self.src_dir, "..")) Path.cwd().parent
os.path.splitext(os.path.split(project_file)[-1])[0] Path(project_file).stem
os.path.basename(project_file).split(".")[0] Path(project_file).stem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Easy to solve for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants