From 81cd6798027d812ee15f703b67abd8c48d0a8083 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Mon, 5 Aug 2024 17:12:35 -0700 Subject: [PATCH] implement hash for all functions --- reflex/ivars/base.py | 24 ++++++++ reflex/ivars/function.py | 24 ++++++++ reflex/ivars/number.py | 74 ++++++++++++++++++++++ reflex/ivars/object.py | 40 ++++++++++++ reflex/ivars/sequence.py | 128 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 290 insertions(+) diff --git a/reflex/ivars/base.py b/reflex/ivars/base.py index ff8c087bf..55dad65ce 100644 --- a/reflex/ivars/base.py +++ b/reflex/ivars/base.py @@ -947,6 +947,14 @@ class AndOperation(ImmutableVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculates the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self._var1, self._var2)) + @dataclasses.dataclass( eq=False, @@ -1028,6 +1036,14 @@ class OrOperation(ImmutableVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculates the hash value for the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self._var1, self._var2)) + @dataclasses.dataclass( eq=False, @@ -1074,3 +1090,11 @@ class ImmutableCallableVar(ImmutableVar): The Var returned from calling the function. """ return self.fn(*args, **kwargs) + + def __hash__(self) -> int: + """Calculate the hash of the object. + + Returns: + The hash of the object. + """ + return hash((self.__class__.__name__, self.original_var)) diff --git a/reflex/ivars/function.py b/reflex/ivars/function.py index c2bea52f5..9033b886e 100644 --- a/reflex/ivars/function.py +++ b/reflex/ivars/function.py @@ -135,6 +135,14 @@ class VarOperationCall(ImmutableVar): """Post-initialize the var.""" pass + def __hash__(self): + """Hash the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self._func, self._args)) + @dataclasses.dataclass( eq=False, @@ -214,6 +222,14 @@ class ArgsFunctionOperation(FunctionVar): def __post_init__(self): """Post-initialize the var.""" + def __hash__(self): + """Hash the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self._args_names, self._return_expr)) + @dataclasses.dataclass( eq=False, @@ -290,5 +306,13 @@ class ToFunctionOperation(FunctionVar): """ return self._cached_get_all_var_data + def __hash__(self): + """Hash the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self._original_var)) + JSON_STRINGIFY = FunctionStringVar("JSON.stringify") diff --git a/reflex/ivars/number.py b/reflex/ivars/number.py index 50d128542..ff33c1779 100644 --- a/reflex/ivars/number.py +++ b/reflex/ivars/number.py @@ -400,6 +400,14 @@ class BinaryNumberOperation(NumberVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.b)) + @dataclasses.dataclass( eq=False, @@ -467,6 +475,14 @@ class UnaryNumberOperation(NumberVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a)) + class NumberAddOperation(BinaryNumberOperation): """Base class for immutable number vars that are the result of an addition operation.""" @@ -827,6 +843,14 @@ class BooleanToIntOperation(NumberVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a)) + @dataclasses.dataclass( eq=False, @@ -899,6 +923,14 @@ class ComparisonOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.b)) + class GreaterThanOperation(ComparisonOperation): """Base class for immutable boolean vars that are the result of a greater than operation.""" @@ -1058,6 +1090,14 @@ class LogicalOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.b)) + class BooleanNotOperation(BooleanVar): """Base class for immutable boolean vars that are the result of a logical NOT operation.""" @@ -1115,6 +1155,14 @@ class BooleanNotOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a)) + @dataclasses.dataclass( eq=False, @@ -1279,6 +1327,14 @@ class ToNumberVarOperation(NumberVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self._original_value)) + @dataclasses.dataclass( eq=False, @@ -1347,6 +1403,14 @@ class ToBooleanVarOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self._original_value)) + class TernaryOperator(ImmutableVar): """Base class for immutable vars that are the result of a ternary operation.""" @@ -1427,3 +1491,13 @@ class TernaryOperator(ImmutableVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash( + (self.__class__.__name__, self.condition, self.if_true, self.if_false) + ) diff --git a/reflex/ivars/object.py b/reflex/ivars/object.py index 6d5faa158..8a5dcb756 100644 --- a/reflex/ivars/object.py +++ b/reflex/ivars/object.py @@ -484,6 +484,14 @@ class ObjectToArrayOperation(ArrayVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the operation. + + Returns: + The hash of the operation. + """ + return hash((self.__class__.__name__, self.value)) + class ObjectKeysOperation(ObjectToArrayOperation): """Operation to get the keys of an object.""" @@ -645,6 +653,14 @@ class ObjectMergeOperation(ObjectVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the operation. + + Returns: + The hash of the operation. + """ + return hash((self.__class__.__name__, self.left, self.right)) + @dataclasses.dataclass( eq=False, @@ -725,6 +741,14 @@ class ObjectItemOperation(ImmutableVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the operation. + + Returns: + The hash of the operation. + """ + return hash((self.__class__.__name__, self.value, self.key)) + @dataclasses.dataclass( eq=False, @@ -799,6 +823,14 @@ class ToObjectOperation(ObjectVar): """ return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the operation. + + Returns: + The hash of the operation. + """ + return hash((self.__class__.__name__, self._original_var)) + @dataclasses.dataclass( eq=False, @@ -876,3 +908,11 @@ class ObjectHasOwnProperty(BooleanVar): The VarData of the components and all of its children. """ return self._cached_get_all_var_data + + def __hash__(self) -> int: + """Get the hash of the operation. + + Returns: + The hash of the operation. + """ + return hash((self.__class__.__name__, self.value, self.key)) diff --git a/reflex/ivars/sequence.py b/reflex/ivars/sequence.py index 009849b7e..cae939ed6 100644 --- a/reflex/ivars/sequence.py +++ b/reflex/ivars/sequence.py @@ -254,6 +254,14 @@ class StringToStringOperation(StringVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a)) + class StringLowerOperation(StringToStringOperation): """Base class for immutable string vars that are the result of a string lower operation.""" @@ -368,6 +376,14 @@ class StringContainsOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.b)) + @dataclasses.dataclass( eq=False, @@ -439,6 +455,14 @@ class StringItemOperation(StringVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.i)) + class ArrayJoinOperation(StringVar): """Base class for immutable string vars that are the result of an array join operation.""" @@ -505,6 +529,14 @@ class ArrayJoinOperation(StringVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Calculate the hash value of the object. + + Returns: + int: The hash value of the object. + """ + return hash((self.__class__.__name__, self.a, self.b)) + # Compile regex for finding reflex var tags. _decode_var_pattern_re = ( @@ -722,6 +754,14 @@ class ConcatVarOperation(StringVar): """Post-initialize the var.""" pass + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, *self._var_value)) + ARRAY_VAR_TYPE = TypeVar("ARRAY_VAR_TYPE", bound=Union[List, Tuple, Set]) @@ -1144,6 +1184,14 @@ class StringSplitOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self.b)) + @dataclasses.dataclass( eq=False, @@ -1209,6 +1257,14 @@ class ArrayToArrayOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a)) + @dataclasses.dataclass( eq=False, @@ -1322,6 +1378,14 @@ class ArraySliceOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self._slice)) + class ArrayReverseOperation(ArrayToArrayOperation): """Base class for immutable string vars that are the result of a string reverse operation.""" @@ -1399,6 +1463,14 @@ class ArrayToNumberOperation(NumberVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a)) + class ArrayLengthOperation(ArrayToNumberOperation): """Base class for immutable number vars that are the result of an array length operation.""" @@ -1505,6 +1577,14 @@ class ArrayItemOperation(ImmutableVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self.i)) + @dataclasses.dataclass( eq=False, @@ -1595,6 +1675,14 @@ class RangeOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.start, self.end, self.step)) + @dataclasses.dataclass( eq=False, @@ -1660,6 +1748,14 @@ class ArrayContainsOperation(BooleanVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self.b)) + @dataclasses.dataclass( eq=False, @@ -1728,6 +1824,14 @@ class ToStringOperation(StringVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.original_var)) + @dataclasses.dataclass( eq=False, @@ -1800,6 +1904,14 @@ class ToArrayOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.original_var)) + @dataclasses.dataclass( eq=False, @@ -1871,6 +1983,14 @@ class ArrayRepeatOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self.n)) + @dataclasses.dataclass( eq=False, @@ -1936,3 +2056,11 @@ class ArrayConcatOperation(ArrayVar): def _get_all_var_data(self) -> ImmutableVarData | None: return self._cached_get_all_var_data + + def __hash__(self) -> int: + """Get the hash of the var. + + Returns: + The hash of the var. + """ + return hash((self.__class__.__name__, self.a, self.b))