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 and optimize Enum class #188

Merged
merged 3 commits into from
Apr 29, 2024

Conversation

MaxGhenis
Copy link
Contributor

@MaxGhenis MaxGhenis commented Apr 23, 2024

Fixes #100

This pull request focuses on refactoring and optimizing the Enum class in the enum.py module. The main goals of this PR are to improve code clarity, performance, and maintainability.

Key changes:

  1. Added docstrings:

    • Added a docstring to the __init__ method to explain its purpose and how the index is assigned to each Enum item.
    • Improved the docstring of the encode method to provide a clear explanation of its functionality, arguments, return value, and examples of usage.
  2. Simplified method assignments:

    • Simplified the __eq__ and __hash__ method assignments by directly assigning them to the corresponding methods of object.
  3. Optimized encode method:

    • Consolidated the .astype(ENUM_ARRAY_DTYPE) call at the end of the method, applying it to the indices array before creating the EnumArray.
    • Simplified the code for handling enum items arrays by using np.select() instead of separate branches for bytes and non-bytes cases.
    • Raised a ValueError with a clear error message for unsupported array data types.
  4. Updated type hints:

    • Updated the type hint for the array parameter in the encode method to use np.ndarray instead of specific data types like np.int_, np.float_, and np.object_.
  5. Improved comments:

    • Updated the comments to provide more context and explain the purpose of each code block.
    • Removed unnecessary comments and simplified existing ones to enhance code readability.
  6. Removed unnecessary code:

    • Removed the warnings import and the warnings.simplefilter() call since they are not directly related to the functionality of the Enum class, and other changes should remove the warnings.

These improvements aim to make the Enum class more readable, efficient, and maintainable. The added docstrings and comments provide better documentation and understanding of the code, while the optimized operations and simplified code structure enhance its performance and clarity.

The consolidation of the .astype(ENUM_ARRAY_DTYPE) call at the end of the encode method reduces code duplication and improves readability. The simplified code for handling enum items arrays using np.select() makes the code more concise and easier to understand.

Please review the changes and provide any feedback or suggestions for further improvements.

@MaxGhenis MaxGhenis changed the title FutureWarning regarding bool checking in enum.py Refactor and optimize Enum class Apr 23, 2024
@MaxGhenis MaxGhenis marked this pull request as draft April 24, 2024 23:35
…ata type kind is "i" (signed integer) or "u" (unsigned integer), the indices variable is directly assigned the array value.

Updated the else condition to raise a ValueError only for unsupported array data types that are not handled by the previous conditions.
@MaxGhenis MaxGhenis marked this pull request as ready for review April 27, 2024 06:47
@MaxGhenis MaxGhenis requested a review from anth-volk April 27, 2024 20:20
@MaxGhenis MaxGhenis merged commit a1495e3 into PolicyEngine:master Apr 29, 2024
4 checks passed
@MaxGhenis MaxGhenis deleted the MaxGhenis/issue100 branch April 29, 2024 15:06
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.

FutureWarning regarding bool checking in enum.py
2 participants