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

update assign_units and add kcal_per_h #89

Merged
merged 2 commits into from
Dec 28, 2024
Merged

update assign_units and add kcal_per_h #89

merged 2 commits into from
Dec 28, 2024

Conversation

chaoming0625
Copy link
Member

@chaoming0625 chaoming0625 commented Dec 28, 2024

This pull request includes several changes to the brainunit library, focusing on improvements to type handling, unit assignment, and documentation corrections.

Type Handling and Unit Assignment Improvements:

  • brainunit/_base.py: Added cast import for type casting and introduced CallableAssignUnit and Missing classes to improve the handling of callable units and missing values. Enhanced the assign_units decorator to support partial assignments and improved error messages for type mismatches. [1] [2] [3] [4]

New Unit Constants:

Documentation Corrections:

Summary by Sourcery

Improve the assign_units decorator to support partial unit assignments and add a new unit constant for kilocalories per hour (kcal_per_h). Also fix a typo in the documentation.

New Features:

  • Add kcal_per_h unit constant.

Tests:

  • Improve type handling and error messages in assign_units decorator.

Copy link
Contributor

sourcery-ai bot commented Dec 28, 2024

Reviewer's Guide by Sourcery

This pull request enhances the brainunit library by improving type handling within the assign_units decorator, introducing a new unit constant kcal_per_h, and correcting documentation typos.

Sequence diagram for enhanced assign_units decorator flow

sequenceDiagram
    participant C as Client Code
    participant A as assign_units
    participant R as _remove_unit

    C->>A: Call with function and units
    A->>A: Parse function arguments
    loop For each argument
        alt Argument has unit specification
            A->>R: Remove/convert units
            R-->>A: Return processed value
        end
    end
    A->>A: Execute original function
    alt Result units specified
        A->>A: Validate result structure
        A->>A: Assign result units
    end
    A-->>C: Return result
Loading

Class diagram for new unit assignment types

classDiagram
    class CallableAssignUnit {
        +without_result_units: Callable
        +__call__(*args, **kwargs)
    }
    class Missing {
    }
    class Callable
    CallableAssignUnit --|> Callable
    note for CallableAssignUnit "New class for handling
callable units"
    note for Missing "Represents missing
values in assignments"
Loading

Class diagram for unit constants update

classDiagram
    class UnitConstants {
        +watt: Unit
        +horsepower: Unit
        +kcal_per_h: Unit
    }
    note for UnitConstants "Added kcal_per_h
with conversion factor
1.162222 watts"
Loading

File-Level Changes

Change Details Files
Improved type handling and unit assignment logic within the assign_units decorator. This includes support for partial assignments and enhanced error messages for type mismatches.
  • Added CallableAssignUnit and Missing classes to handle callable units and missing values.
  • Improved error messages for type mismatches.
  • Added support for partial unit assignments using the partial function.
  • Refactored the decorator logic to handle various input types and unit assignments more robustly.
  • Added more comprehensive type checking to ensure correct unit assignments and prevent unexpected behavior.
brainunit/_base.py
Introduced a new unit constant kcal_per_h representing kilocalories per hour.
  • Added kcal_per_h constant.
  • Defined the kcal_per_h unit in terms of watts using the Unit.create method.
brainunit/_unit_constants.py
Corrected a typo in the documentation.
  • Corrected the filename from elinstein_operations.ipynb to einstein_operations.ipynb in docs/index.rst.
  • Updated the title and links in einstein_operations.ipynb to reflect the correct spelling of "Einstein".
docs/index.rst
docs/mathematical_functions/einstein_operations.ipynb

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

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

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 @chaoming0625 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 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.

@chaoming0625 chaoming0625 merged commit 1753e86 into main Dec 28, 2024
24 checks passed
@chaoming0625 chaoming0625 deleted the update branch December 28, 2024 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant