-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathnumeric_encoder.py
More file actions
48 lines (40 loc) · 1.25 KB
/
numeric_encoder.py
File metadata and controls
48 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import re
from .encoder_base import EncoderBase
class NumericEncoder(EncoderBase):
@classmethod
def mode_indicator(cls):
return "001"
@classmethod
def _encoded_bits(cls, data):
res = ""
data_grouped = cls._group_by_3characters(data)
for num in data_grouped:
if len(num) == 3:
res += bin(int(num))[2:].zfill(10)
elif len(num) == 2:
res += bin(int(num))[2:].zfill(7)
elif len(num) == 1:
res += bin(int(num))[2:].zfill(4)
return res
@classmethod
def _group_by_3characters(cls, data):
res = []
while data != "":
res.append(data[:3])
data = data[3:]
return res
@classmethod
def length(cls, data, character_count_indicator_length):
if len(data) % 3 == 0:
r = 0
elif len(data) % 3 == 1:
r = 4
elif len(data) % 3 == 2:
r = 7
return len(cls.mode_indicator()) + character_count_indicator_length + 10 * (len(data) // 3) + r
@classmethod
def characters_num(cls, data):
return len(data)
@classmethod
def is_valid_characters(cls, data):
return bool(re.match(r"^[0-9]*$", data))