Completed
Push — master ( 1af191...69da3c )
by
unknown
07:35 queued 06:27
created

Mode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A to_byteorder() 0 11 3
B from_byteorder() 0 15 5
1
"""binary representation modes used for a StarStruct object."""
2
3 1
import sys
4 1
import enum
5
6
7 1
@enum.unique
8 1
class Mode(enum.Enum):
9
    """The StarStruct modes match the modes supported by struct.pack/unpack."""
10 1
    Native = '='
11 1
    Little = '<'
12 1
    Big = '>'
13 1
    Network = '!'
14
15 1
    def to_byteorder(self):
16
        """
17
        Convert a Mode to a byteorder string such as required by the
18
        to_bytes() or from_bytes() functions.
19
        """
20 1
        if self == self.Native:
21 1
            return sys.byteorder
22 1
        elif self == self.Little:
23 1
            return 'little'
24
        else:  # Big or Network
25 1
            return 'big'
26
27 1
    @staticmethod
28
    def from_byteorder(val):
29
        """
30
        Convert a byteorder string such as used by the to_bytes() or
31
        from_bytes() functions into a Mode value.
32
        """
33 1
        if val == 'little':
34 1
            return Mode.Little
35 1
        elif val == 'big':
36 1
            return Mode.Big
37 1
        elif val == 'native':  # custom
38 1
            return Mode.Native
39 1
        elif val == 'network':  # custom
40 1
            return Mode.Network
41
        raise TypeError('{} not a valid byteorder'.format(val))
42