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: ♻️ File I/O operations for better readability and maintainability #1621

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

Anselmoo
Copy link
Owner

@Anselmoo Anselmoo commented Oct 1, 2024

All PR-Submissions:


  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open
    Pull Requests for the same
    update/change?

New ✨✨ Feature-Submissions:


Changes to ⚙️ Core-Features:


  • Have you added an explanation of what your changes do and why you'd like
    us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully run tests with your changes locally?

Summary by Sourcery

Refactor file I/O operations to enhance code readability and maintainability by replacing open() function calls with the Path.open() method across various modules.

Enhancements:

  • Refactor file I/O operations across multiple modules to use the Path.open() method for improved readability and maintainability.

@Anselmoo Anselmoo enabled auto-merge October 1, 2024 18:39
Copy link

semanticdiff-com bot commented Oct 1, 2024

Review changes with SemanticDiff.

Analyzed 9 of 9 files.

Overall, the semantic diff is 82% smaller than the GitHub diff.

Filename Status
✔️ spectrafit/tools.py 53.71% smaller
✔️ spectrafit/plugins/data_converter.py 47.8% smaller
✔️ spectrafit/plugins/file_converter.py 85.65% smaller
✔️ spectrafit/plugins/notebook.py 94.24% smaller
✔️ spectrafit/plugins/pkl_converter.py 31.81% smaller
✔️ spectrafit/plugins/pkl_visualizer.py 67.62% smaller
✔️ spectrafit/plugins/pptx_converter.py 43.97% smaller
✔️ spectrafit/plugins/rixs_converter.py 89.19% smaller
✔️ spectrafit/plugins/rixs_visualizer.py 43.98% smaller

Copy link
Contributor

sourcery-ai bot commented Oct 1, 2024

Reviewer's Guide by Sourcery

This pull request refactors file I/O operations across multiple files in the spectrafit project to improve readability and maintainability. The main change is the consistent use of the Path.open() method instead of the built-in open() function for file operations. This change simplifies the file opening process and makes the code more idiomatic when working with pathlib.Path objects.

Sequence Diagram

sequenceDiagram
    participant C as Code
    participant P as Path Object
    participant F as File
    C->>P: Create Path object
    C->>P: Call open() method
    P->>F: Open file
    F-->>C: Return file object
    C->>F: Perform I/O operations
    C->>F: Close file
Loading

File-Level Changes

Change Details Files
Refactored file I/O operations to use Path.open() method
  • Replaced open() function calls with Path.open() method
  • Simplified file path handling by using Path objects directly
  • Updated file opening modes to be consistent across different file types
  • Removed redundant path construction in some cases
spectrafit/plugins/file_converter.py
spectrafit/plugins/notebook.py
spectrafit/tools.py
spectrafit/plugins/rixs_converter.py
spectrafit/plugins/pkl_visualizer.py
spectrafit/plugins/rixs_visualizer.py
spectrafit/plugins/data_converter.py
spectrafit/plugins/pkl_converter.py
spectrafit/plugins/pptx_converter.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the python Pull requests that update Python code label Oct 1, 2024
Copy link

sonarqubecloud bot commented Oct 1, 2024

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Anselmoo - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -654,7 +654,7 @@ def pkl2any(pkl_fname: Path, encoding: str = "latin1") -> Any:
with gzip.open(pkl_fname, "rb") as f:
return unicode_check(f, encoding=encoding)
elif pkl_fname.suffix == ".pkl":
with open(pkl_fname, "rb") as f:
with pkl_fname.open("rb") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Make pkl2any function consistent with Path.open()

For consistency, consider using Path.open() for all file operations in this function, including the gzip.open() call above.

Suggested change
with pkl_fname.open("rb") as f:
with Path(pkl_fname).open("rb") as f:

@@ -512,8 +512,8 @@
def save_as_json(self) -> None:
"""Save the fitting result as json file."""
if self.args["outfile"]:
with open(
Path(f"{self.args['outfile']}_summary.json"), "w", encoding="utf-8"
with Path(f"{self.args['outfile']}_summary.json").open(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Use Path methods for file name manipulation

Instead of using string formatting to create file names, consider using Path methods like with_name() or with_suffix(). For example: Path(self.args['outfile']).with_name(f"{Path(self.args['outfile']).stem}_summary.json")

Suggested change
with Path(f"{self.args['outfile']}_summary.json").open(
with Path(self.args['outfile']).with_name(f"{Path(self.args['outfile']).stem}_summary.json").open(

Copy link

codecov bot commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (6349d58) to head (3ca511a).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1621   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           44        44           
  Lines         4468      4468           
=========================================
  Hits          4468      4468           
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spectrafit/plugins/data_converter.py 100.00% <100.00%> (ø)
spectrafit/plugins/file_converter.py 100.00% <100.00%> (ø)
spectrafit/plugins/notebook.py 100.00% <100.00%> (ø)
spectrafit/plugins/pkl_converter.py 100.00% <100.00%> (ø)
spectrafit/plugins/pkl_visualizer.py 100.00% <100.00%> (ø)
spectrafit/plugins/pptx_converter.py 100.00% <100.00%> (ø)
spectrafit/plugins/rixs_converter.py 100.00% <100.00%> (ø)
spectrafit/plugins/rixs_visualizer.py 100.00% <100.00%> (ø)
spectrafit/tools.py 100.00% <100.00%> (ø)

@Anselmoo Anselmoo merged commit 699fe00 into main Oct 1, 2024
33 checks passed
@Anselmoo Anselmoo deleted the Anselmoo/issue1620 branch October 1, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests that update Python code size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor by using open of Path instead of with open
1 participant