Code Duplication    Length = 26-31 lines in 3 locations

opcua/ua/uaprotocol_auto.py 2 locations

@@ 3068-3098 (lines=31) @@
3065
3066
    __repr__ = __str__
3067
3068
3069
class AnonymousIdentityToken(FrozenClass):
3070
    '''
3071
    A token representing an anonymous user.
3072
3073
    :ivar PolicyId:
3074
    :vartype PolicyId: String
3075
    '''
3076
    def __init__(self, binary=None):
3077
        if binary is not None:
3078
            self._binary_init(binary)
3079
            self._freeze = True
3080
            return
3081
        self.PolicyId = None
3082
        self._freeze = True
3083
3084
    def to_binary(self):
3085
        packet = []
3086
        packet.append(pack_string(self.PolicyId))
3087
        return b''.join(packet)
3088
3089
    @staticmethod
3090
    def from_binary(data):
3091
        return AnonymousIdentityToken(data)
3092
3093
    def _binary_init(self, data):
3094
        self.PolicyId = unpack_string(data)
3095
3096
    def __str__(self):
3097
        return 'AnonymousIdentityToken(' + 'PolicyId:' + str(self.PolicyId) + ')'
3098
3099
    __repr__ = __str__
3100
3101
@@ 3035-3065 (lines=31) @@
3032
3033
    __repr__ = __str__
3034
3035
3036
class UserIdentityToken(FrozenClass):
3037
    '''
3038
    A base type for a user identity token.
3039
3040
    :ivar PolicyId:
3041
    :vartype PolicyId: String
3042
    '''
3043
    def __init__(self, binary=None):
3044
        if binary is not None:
3045
            self._binary_init(binary)
3046
            self._freeze = True
3047
            return
3048
        self.PolicyId = None
3049
        self._freeze = True
3050
3051
    def to_binary(self):
3052
        packet = []
3053
        packet.append(pack_string(self.PolicyId))
3054
        return b''.join(packet)
3055
3056
    @staticmethod
3057
    def from_binary(data):
3058
        return UserIdentityToken(data)
3059
3060
    def _binary_init(self, data):
3061
        self.PolicyId = unpack_string(data)
3062
3063
    def __str__(self):
3064
        return 'UserIdentityToken(' + 'PolicyId:' + str(self.PolicyId) + ')'
3065
3066
    __repr__ = __str__
3067
3068

opcua/ua/uatypes.py 1 location

@@ 1043-1068 (lines=26) @@
1040
                raise UaError("Could not guess UA type of {} with type {}, specify UA type".format(val, type(val)))
1041
1042
    def __str__(self):
1043
        return "Variant(val:{!s},type:{})".format(self.Value, self.VariantType)
1044
    __repr__ = __str__
1045
1046
    def to_binary(self):
1047
        b = []
1048
        encoding = self.VariantType.value & 0b111111
1049
        if type(self.Value) in (list, tuple):
1050
            if self.Dimensions is not None:
1051
                encoding = set_bit(encoding, 6)
1052
            encoding = set_bit(encoding, 7)
1053
            b.append(uatype_UInt8.pack(encoding))
1054
            b.append(pack_uatype_array(self.VariantType.name, flatten(self.Value)))
1055
            if self.Dimensions is not None:
1056
                b.append(pack_uatype_array("Int32", self.Dimensions))
1057
        else:
1058
            b.append(uatype_UInt8.pack(encoding))
1059
            b.append(pack_uatype(self.VariantType.name, self.Value))
1060
1061
        return b"".join(b)
1062
1063
    @staticmethod
1064
    def from_binary(data):
1065
        dimensions = None
1066
        encoding = ord(data.read(1))
1067
        int_type = encoding & 0b00111111
1068
        vtype = DataType_to_VariantType(int_type)
1069
        if vtype == VariantType.Null:
1070
            return Variant(None, vtype, encoding)
1071
        if test_bit(encoding, 7):