-
Notifications
You must be signed in to change notification settings - Fork 123
refactor: improve int32 integration #902
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c353e9d
improve int32 integration
mvadari bcab6f5
clean up
mvadari 86b428f
improve error message
mvadari d1256b7
fix linting
mvadari 92d948a
fix typo
mvadari e585632
respond to coderabbit
mvadari b8c1468
one more typo
mvadari 136a699
more minor fixes
mvadari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """Base class for serializing and deserializing signed integers. | ||
| See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
| from xrpl.core.binarycodec.types.uint import UInt | ||
|
|
||
|
|
||
| class Int(UInt): | ||
| """Base class for serializing and deserializing signed integers. | ||
| See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ | ||
| """ | ||
|
|
||
| @property | ||
| def value(self: Self) -> int: | ||
| """ | ||
| Get the value of the Int represented by `self.buffer`. | ||
|
|
||
| Returns: | ||
| The int value of the Int. | ||
| """ | ||
| return int.from_bytes(self.buffer, byteorder="big", signed=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,78 @@ | ||
| """Class for serializing and deserializing a signed 32-bit integer.""" | ||
| """Class for serializing and deserializing a signed 32-bit integer. | ||
| See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ | ||
| """ | ||
mvadari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, Type | ||
| from typing import Optional, Type, Union | ||
|
|
||
| from typing_extensions import Final, Self | ||
|
|
||
| from xrpl.core.binarycodec.binary_wrappers.binary_parser import BinaryParser | ||
| from xrpl.core.binarycodec.exceptions import XRPLBinaryCodecException | ||
| from xrpl.core.binarycodec.types.serialized_type import SerializedType | ||
| from xrpl.core.binarycodec.types.int import Int | ||
|
|
||
| _WIDTH: Final[int] = 4 # 32 / 8 | ||
|
|
||
|
|
||
| class Int32(SerializedType): | ||
| """Class for serializing and deserializing a signed 32-bit integer.""" | ||
| class Int32(Int): | ||
| """ | ||
| Class for serializing and deserializing a signed 32-bit integer. | ||
| See `Int Fields <https://xrpl.org/serialization.html#int-fields>`_ | ||
| """ | ||
|
|
||
| def __init__(self: Self, buffer: bytes = bytes(_WIDTH)) -> None: | ||
| """Construct a new Int32 type from a ``bytes`` value.""" | ||
| super().__init__(buffer) | ||
|
|
||
| @property | ||
| def value(self: Self) -> int: | ||
| """Get the value of the Int32 represented by `self.buffer`.""" | ||
| return int.from_bytes(self.buffer, byteorder="big", signed=True) | ||
|
|
||
| @classmethod | ||
| def from_parser( | ||
| cls: Type[Self], parser: BinaryParser, _length_hint: Optional[int] = None | ||
| ) -> Self: | ||
| """Construct a new Int32 type from a BinaryParser.""" | ||
| """ | ||
| Construct a new Int32 type from a BinaryParser. | ||
| Args: | ||
| parser: A BinaryParser to construct an Int32 from. | ||
| Returns: | ||
| The Int32 constructed from parser. | ||
| """ | ||
| return cls(parser.read(_WIDTH)) | ||
|
|
||
| @classmethod | ||
| def from_value(cls: Type[Self], value: int) -> Self: | ||
| """Construct a new Int32 type from an integer.""" | ||
| if not isinstance(value, int): | ||
| raise XRPLBinaryCodecException( | ||
| f"Invalid type to construct Int32: expected int, " | ||
| f"received {value.__class__.__name__}." | ||
| ) | ||
| return cls(value.to_bytes(_WIDTH, byteorder="big", signed=True)) | ||
|
|
||
| def to_json(self: Self) -> int: | ||
| """Convert the Int32 to JSON (returns the integer value).""" | ||
| return self.value | ||
|
|
||
| def __eq__(self: Self, other: object) -> bool: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of these dunder methods are inherited from UInt, so they are no longer needed here |
||
| """Determine whether two Int32 objects are equal.""" | ||
| if isinstance(other, int): | ||
| return self.value == other | ||
| if isinstance(other, Int32): | ||
| return self.value == other.value | ||
| return NotImplemented | ||
|
|
||
| def __lt__(self: Self, other: object) -> bool: | ||
| """Determine whether this Int32 is less than another.""" | ||
| if isinstance(other, int): | ||
| return self.value < other | ||
| if isinstance(other, Int32): | ||
| return self.value < other.value | ||
| return NotImplemented | ||
|
|
||
| def __gt__(self: Self, other: object) -> bool: | ||
| """Determine whether this Int32 is greater than another.""" | ||
| if isinstance(other, int): | ||
| return self.value > other | ||
| if isinstance(other, Int32): | ||
| return self.value > other.value | ||
| return NotImplemented | ||
| def from_value(cls: Type[Self], value: Union[str, int]) -> Self: | ||
| """ | ||
| Construct a new Int32 type from a number. | ||
| Args: | ||
| value: The number to construct an Int32 from. | ||
| Returns: | ||
| The Int32 constructed from value. | ||
| Raises: | ||
| XRPLBinaryCodecException: If an Int32 could not be constructed from value. | ||
| """ | ||
| if isinstance(value, int): | ||
| value_bytes = (value).to_bytes(_WIDTH, byteorder="big", signed=True) | ||
| return cls(value_bytes) | ||
|
|
||
| if isinstance(value, str): | ||
| try: | ||
| int_value = int(value) | ||
| except ValueError as err: | ||
| raise XRPLBinaryCodecException( | ||
| f"Cannot construct Int32 from given value: {value!r}" | ||
| ) from err | ||
| try: | ||
| return cls(int_value.to_bytes(_WIDTH, byteorder="big", signed=True)) | ||
| except OverflowError as err: | ||
| raise XRPLBinaryCodecException( | ||
| f"Cannot construct Int32 from given value: {value!r}" | ||
| ) from err | ||
|
|
||
| raise XRPLBinaryCodecException( | ||
| "Invalid type to construct an Int32: expected str or int," | ||
| f" received {value.__class__.__name__}." | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.