Code Duplication    Length = 61-74 lines in 2 locations

opcua/ua/uaprotocol_auto.py 1 location

@@ 297-370 (lines=74) @@
294
    ReferenceType = 1371236
295
    View = 1335532
296
297
298
class AttributeWriteMask(IntEnum):
299
    '''
300
    Define bits used to indicate which attributes are writable.
301
302
    :ivar None_:
303
    :vartype None_: 0
304
    :ivar AccessLevel:
305
    :vartype AccessLevel: 1
306
    :ivar ArrayDimensions:
307
    :vartype ArrayDimensions: 2
308
    :ivar BrowseName:
309
    :vartype BrowseName: 4
310
    :ivar ContainsNoLoops:
311
    :vartype ContainsNoLoops: 8
312
    :ivar DataType:
313
    :vartype DataType: 16
314
    :ivar Description:
315
    :vartype Description: 32
316
    :ivar DisplayName:
317
    :vartype DisplayName: 64
318
    :ivar EventNotifier:
319
    :vartype EventNotifier: 128
320
    :ivar Executable:
321
    :vartype Executable: 256
322
    :ivar Historizing:
323
    :vartype Historizing: 512
324
    :ivar InverseName:
325
    :vartype InverseName: 1024
326
    :ivar IsAbstract:
327
    :vartype IsAbstract: 2048
328
    :ivar MinimumSamplingInterval:
329
    :vartype MinimumSamplingInterval: 4096
330
    :ivar NodeClass:
331
    :vartype NodeClass: 8192
332
    :ivar NodeId:
333
    :vartype NodeId: 16384
334
    :ivar Symmetric:
335
    :vartype Symmetric: 32768
336
    :ivar UserAccessLevel:
337
    :vartype UserAccessLevel: 65536
338
    :ivar UserExecutable:
339
    :vartype UserExecutable: 131072
340
    :ivar UserWriteMask:
341
    :vartype UserWriteMask: 262144
342
    :ivar ValueRank:
343
    :vartype ValueRank: 524288
344
    :ivar WriteMask:
345
    :vartype WriteMask: 1048576
346
    :ivar ValueForVariableType:
347
    :vartype ValueForVariableType: 2097152
348
    '''
349
    None_ = 0
350
    AccessLevel = 1
351
    ArrayDimensions = 2
352
    BrowseName = 4
353
    ContainsNoLoops = 8
354
    DataType = 16
355
    Description = 32
356
    DisplayName = 64
357
    EventNotifier = 128
358
    Executable = 256
359
    Historizing = 512
360
    InverseName = 1024
361
    IsAbstract = 2048
362
    MinimumSamplingInterval = 4096
363
    NodeClass = 8192
364
    NodeId = 16384
365
    Symmetric = 32768
366
    UserAccessLevel = 65536
367
    UserExecutable = 131072
368
    UserWriteMask = 262144
369
    ValueRank = 524288
370
    WriteMask = 1048576
371
    ValueForVariableType = 2097152
372
373

opcua/ua/uatypes.py 1 location

@@ 805-865 (lines=61) @@
802
803
    def __str__(self):
804
        return "Variant(val:{!s},type:{})".format(self.Value, self.VariantType)
805
    __repr__ = __str__
806
807
    def to_binary(self):
808
        b = []
809
        encoding = self.VariantType.value & 0b111111
810
        if type(self.Value) in (list, tuple):
811
            if self.Dimensions is not None:
812
                encoding = uabin.set_bit(encoding, 6)
813
            encoding = uabin.set_bit(encoding, 7)
814
            b.append(uabin.Primitives.UInt8.pack(encoding))
815
            b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value)))
816
            if self.Dimensions is not None:
817
                b.append(uabin.pack_uatype_array(VariantType.Int32, self.Dimensions))
818
        else:
819
            b.append(uabin.Primitives.UInt8.pack(encoding))
820
            b.append(uabin.pack_uatype(self.VariantType, self.Value))
821
822
        return b"".join(b)
823
824
    @staticmethod
825
    def from_binary(data):
826
        dimensions = None
827
        encoding = ord(data.read(1))
828
        int_type = encoding & 0b00111111
829
        vtype = datatype_to_varianttype(int_type)
830
        if vtype == VariantType.Null:
831
            return Variant(None, vtype, encoding)
832
        if uabin.test_bit(encoding, 7):
833
            value = uabin.unpack_uatype_array(vtype, data)
834
        else:
835
            value = uabin.unpack_uatype(vtype, data)
836
        if uabin.test_bit(encoding, 6):
837
            dimensions = uabin.unpack_uatype_array(VariantType.Int32, data)
838
            value = reshape(value, dimensions)
839
840
        return Variant(value, vtype, dimensions)
841
842
843
def reshape(flat, dims):
844
    subdims = dims[1:]
845
    subsize = 1
846
    for i in subdims:
847
        if i == 0:
848
            i = 1
849
        subsize *= i
850
    while dims[0] * subsize > len(flat):
851
        flat.append([])
852
    if not subdims or subdims == [0]:
853
        return flat
854
    return [reshape(flat[i: i + subsize], subdims) for i in range(0, len(flat), subsize)]
855
856
857
def _split_list(l, n):
858
    n = max(1, n)
859
    return [l[i:i + n] for i in range(0, len(l), n)]
860
861
862
def flatten_and_get_shape(mylist):
863
    dims = []
864
    dims.append(len(mylist))
865
    while isinstance(mylist[0], (list, tuple)):
866
        dims.append(len(mylist[0]))
867
        mylist = [item for sublist in mylist for item in sublist]
868
        if len(mylist) == 0: