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:
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.
Simplified method assignments:
Simplified the __eq__ and __hash__ method assignments by directly assigning them to the corresponding methods of object.
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.
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_.
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.
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.
Fixes #100
This pull request focuses on refactoring and optimizing the
Enum
class in theenum.py
module. The main goals of this PR are to improve code clarity, performance, and maintainability.Key changes:
Added docstrings:
__init__
method to explain its purpose and how the index is assigned to each Enum item.encode
method to provide a clear explanation of its functionality, arguments, return value, and examples of usage.Simplified method assignments:
__eq__
and__hash__
method assignments by directly assigning them to the corresponding methods ofobject
.Optimized
encode
method:.astype(ENUM_ARRAY_DTYPE)
call at the end of the method, applying it to theindices
array before creating theEnumArray
.np.select()
instead of separate branches forbytes
and non-bytes
cases.ValueError
with a clear error message for unsupported array data types.Updated type hints:
array
parameter in theencode
method to usenp.ndarray
instead of specific data types likenp.int_
,np.float_
, andnp.object_
.Improved comments:
Removed unnecessary code:
warnings
import and thewarnings.simplefilter()
call since they are not directly related to the functionality of theEnum
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 theencode
method reduces code duplication and improves readability. The simplified code for handling enum items arrays usingnp.select()
makes the code more concise and easier to understand.Please review the changes and provide any feedback or suggestions for further improvements.