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

Intendet way for comparing values of two nodes #1592

Closed
Fliens opened this issue Feb 27, 2024 · 2 comments
Closed

Intendet way for comparing values of two nodes #1592

Fliens opened this issue Feb 27, 2024 · 2 comments

Comments

@Fliens
Copy link

Fliens commented Feb 27, 2024

Hi as the title says, I want to compare the values of two nodes.

I want to make sure that python does nothing unintendet in the background with the node datatypes.
For example when comparing nodes with different types of int: int16, int32, int64, Uint16, Uint32, Uint64.

I'm worried that the python "==" could create false positive or general negative results when comparing plain values of nodes

Example

value1 = node1.read_data_value().Value.Value
value2 = node2.read_data_value().Value.Value
print(value1 == value2)

value1 = node1.get_value()
value2 = node2.get_value()
print(value1 == value2)

Is there a better way to compare two ua.DataValue() or ua.Variant() that keeps the datatype of the value in mind?
Instances of these classes can't be compared directly since they also have a timestamp that might not be important for comparing their values.

Sth like this:

value1 = node1.read_data_value().Value.Value
value2 = node2.read_data_value().Value.Value
print(ua.uatypes.compare_variants(value1, value2)).
# Returns True if both values are the same when cast to the same datatype
@AndreasHeine
Copy link
Member

AndreasHeine commented Mar 1, 2024

ua.DataValue() or ua.Variant() are both python dataclasses.

DataValue has no __eq__ method!

class DataValue:

Variant has a __eq__ method

class Variant:

    def __eq__(self, other):
        if (
            isinstance(other, Variant)
            and self.VariantType == other.VariantType
            and self.Value == other.Value
        ):
            return True
        return False

so comparing variant1 == variant2 should work as in python docs described.
https://docs.python.org/3/reference/datamodel.html#object.__eq__

@Fliens
Copy link
Author

Fliens commented Mar 1, 2024

Ahh great thank you for the response this answers my question :)

@Fliens Fliens closed this as completed Mar 1, 2024
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

No branches or pull requests

2 participants