diff --git a/duniterpy/api/endpoint.py b/duniterpy/api/endpoint.py index aab5f5c7b50845d0662d6b2085d15ab89b5f3abe..a782c62b403be06f262513a1b1dbd558521f6db0 100644 --- a/duniterpy/api/endpoint.py +++ b/duniterpy/api/endpoint.py @@ -108,10 +108,9 @@ class UnknownEndpoint(Endpoint): return "{0} {1}".format(self.api, ' '.join(["{0}".format(p) for p in self.properties])) def __eq__(self, other: Any) -> bool: - if isinstance(other, UnknownEndpoint): - return self.api == other.api and self.properties == other.properties - else: - return False + if not isinstance(other, UnknownEndpoint): + return NotImplemented + return self.api == other.api and self.properties == other.properties def __hash__(self) -> int: return hash((self.api, self.properties)) @@ -204,11 +203,10 @@ class BMAEndpoint(Endpoint): return self.inline() def __eq__(self, other: Any) -> bool: - if isinstance(other, BMAEndpoint): - return self.server == other.server and self.ipv4 == other.ipv4 \ - and self.ipv6 == other.ipv6 and self.port == other.port - else: - return False + if not isinstance(other, BMAEndpoint): + return NotImplemented + return self.server == other.server and self.ipv4 == other.ipv4 \ + and self.ipv6 == other.ipv6 and self.port == other.port def __hash__(self) -> int: return hash((self.server, self.ipv4, self.ipv6, self.port)) @@ -347,11 +345,10 @@ class WS2PEndpoint(Endpoint): return self.inline() def __eq__(self, other: Any) -> bool: - if isinstance(other, WS2PEndpoint): - return self.server == other.server and self.ws2pid == other.ws2pid \ - and self.port == other.port and self.path == other.path - else: - return False + if not isinstance(other, WS2PEndpoint): + return NotImplemented + return self.server == other.server and self.ws2pid == other.ws2pid \ + and self.port == other.port and self.path == other.path def __hash__(self) -> int: return hash((self.ws2pid, self.server, self.port, self.path)) @@ -409,10 +406,9 @@ class ESCoreEndpoint(Endpoint): return self.inline() def __eq__(self, other: Any) -> bool: - if isinstance(other, ESCoreEndpoint): - return self.server == other.server and self.port == other.port - else: - return False + if not isinstance(other, ESCoreEndpoint): + return NotImplemented + return self.server == other.server and self.port == other.port def __hash__(self) -> int: return hash((self.server, self.port)) @@ -470,10 +466,9 @@ class ESUserEndpoint(Endpoint): return self.inline() def __eq__(self, other: Any) -> bool: - if isinstance(other, ESUserEndpoint): - return self.server == other.server and self.port == other.port - else: - return False + if not isinstance(other, ESUserEndpoint): + return NotImplemented + return self.server == other.server and self.port == other.port def __hash__(self) -> int: return hash((self.server, self.port)) @@ -531,10 +526,9 @@ class ESSubscribtionEndpoint(Endpoint): return self.inline() def __eq__(self, other: Any) -> bool: - if isinstance(other, ESSubscribtionEndpoint): - return self.server == other.server and self.port == other.port - else: - return False + if not isinstance(other, ESSubscribtionEndpoint): + return NotImplemented + return self.server == other.server and self.port == other.port def __hash__(self) -> int: return hash((ESSubscribtionEndpoint.API, self.server, self.port)) diff --git a/duniterpy/documents/block.py b/duniterpy/documents/block.py index aa8498681fb5ee893725472afb82964553e74d18..9ec967daff271db06619f6366dc3dde4fe71512c 100644 --- a/duniterpy/documents/block.py +++ b/duniterpy/documents/block.py @@ -70,14 +70,14 @@ The class Block handles Block documents. re_unitbase = re.compile("UnitBase: ([0-9]+)\n") re_issuer = re.compile("Issuer: ({pubkey_regex})\n".format(pubkey_regex=PUBKEY_REGEX)) re_issuers_frame = re.compile("IssuersFrame: ([0-9]+)\n") - re_issuers_frame_var = re.compile("IssuersFrameVar: (0|-?[1-9]\d{0,18})\n") + re_issuers_frame_var = re.compile("IssuersFrameVar: (0|-?[1-9]\\d{0,18})\n") re_different_issuers_count = re.compile("DifferentIssuersCount: ([0-9]+)\n") re_previoushash = re.compile("PreviousHash: ({block_hash_regex})\n".format(block_hash_regex=BLOCK_HASH_REGEX)) re_previousissuer = re.compile("PreviousIssuer: ({pubkey_regex})\n".format(pubkey_regex=PUBKEY_REGEX)) - re_parameters = re.compile("Parameters: ([0-9]+\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):\ -([0-9]+):([0-9]+):([0-9]+\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\.[0-9]+)\n") - re_parameters_v10 = re.compile("Parameters: ([0-9]+\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):\ -([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\.[0-9]+):\ + re_parameters = re.compile("Parameters: ([0-9]+\\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):\ +([0-9]+):([0-9]+):([0-9]+\\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\\.[0-9]+)\n") + re_parameters_v10 = re.compile("Parameters: ([0-9]+\\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):\ +([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\\.[0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+):([0-9]+\\.[0-9]+):\ ([0-9]+):([0-9]+):([0-9]+)\n") re_memberscount = re.compile("MembersCount: ([0-9]+)\n") re_identities = re.compile("Identities:\n") @@ -501,7 +501,7 @@ Nonce: {nonce} def __eq__(self, other: object) -> bool: if not isinstance(other, Block): - return False + return NotImplemented return self.blockUID == other.blockUID def __lt__(self, other: object) -> bool: diff --git a/duniterpy/documents/block_uid.py b/duniterpy/documents/block_uid.py index 0b84e84ce02de62f898e4dc7d1856876590e6555..65a2816f6b1bf8c85eea3d974217f5f65c19a60d 100644 --- a/duniterpy/documents/block_uid.py +++ b/duniterpy/documents/block_uid.py @@ -51,7 +51,7 @@ class BlockUID: def __eq__(self, other: object) -> bool: if not isinstance(other, BlockUID): - return False + return NotImplemented return self.number == other.number and self.sha_hash == other.sha_hash def __lt__(self, other: object) -> bool: diff --git a/duniterpy/documents/transaction.py b/duniterpy/documents/transaction.py index fa950e808bda006e9307855e1d3f70014352d9d5..fa3f19af2882f9d7b5da095585363a0c11a4d102 100644 --- a/duniterpy/documents/transaction.py +++ b/duniterpy/documents/transaction.py @@ -1,5 +1,5 @@ import re -from typing import TypeVar, List, Type, Optional, Dict, Union +from typing import TypeVar, List, Any, Type, Optional, Dict, Union import pypeg2 @@ -78,6 +78,21 @@ class InputSource: self.origin_id = origin_id self.index = index + def __eq__(self, other: Any) -> bool: + """ + Check InputSource instances equality + """ + if not isinstance(other, InputSource): + return NotImplemented + return self.amount == other.amount and \ + self.base == other.base and \ + self.source == other.source and \ + self.origin_id == other.origin_id and \ + self.index == other.index + + def __hash__(self) -> int: + return hash((self.amount, self.base, self.source, self.origin_id, self.index)) + @classmethod def from_inline(cls: Type[InputSourceType], tx_version: int, inline: str) -> InputSourceType: """ @@ -153,6 +168,19 @@ class OutputSource: self.base = base self.condition = self.condition_from_text(condition) + def __eq__(self, other: Any) -> bool: + """ + Check OutputSource instances equality + """ + if not isinstance(other, OutputSource): + return NotImplemented + return self.amount == other.amount and \ + self.base == other.base and \ + self.condition == other.condition + + def __hash__(self) -> int: + return hash((self.amount, self.base, self.condition)) + @classmethod def from_inline(cls: Type[OutputSourceType], inline: str) -> OutputSourceType: """ @@ -204,7 +232,7 @@ class SIGParameter: """ A Transaction UNLOCK SIG parameter """ - re_sig = re.compile("SIG\(([0-9]+)\)") + re_sig = re.compile("SIG\\(([0-9]+)\\)") def __init__(self, index: int) -> None: """ @@ -246,7 +274,7 @@ class XHXParameter: """ A Transaction UNLOCK XHX parameter """ - re_xhx = re.compile("XHX\(([0-9]+)\)") + re_xhx = re.compile("XHX\\(([0-9]+)\\)") def __init__(self, integer: int) -> None: """ @@ -320,7 +348,7 @@ class Unlock: """ A Transaction UNLOCK """ - re_inline = re.compile("([0-9]+):((?:SIG\([0-9]+\)|XHX\([0-9]+\)|\s)+)\n") + re_inline = re.compile("([0-9]+):((?:SIG\\([0-9]+\\)|XHX\\([0-9]+\\)|\\s)+)\n") def __init__(self, index: int, parameters: List[Union[SIGParameter, XHXParameter]]) -> None: """