| 1 |
import re |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
def parse_code(code, short_underline, long_underline): |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
tolerance_is_standardized = False |
| 11 |
tolerance = 5 |
| 12 |
if short_underline: |
| 13 |
tolerance = 1 |
| 14 |
tolerance_is_standardized = True |
| 15 |
|
| 16 |
if len(code) > 3: |
| 17 |
tolerance = 1 |
| 18 |
|
| 19 |
if long_underline: |
| 20 |
code = "R" + code |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if re.fullmatch(r"0+", code): |
| 25 |
return 0, tolerance, tolerance_is_standardized |
| 26 |
elif not (2 < len(code) < 5): |
| 27 |
return None |
| 28 |
|
| 29 |
|
| 30 |
if match := re.fullmatch(r"([0][1-9]|[1-8][0-9]|[9][0-6])([ZYRXSABHCDEF])", code, re.IGNORECASE): |
| 31 |
|
| 32 |
return parse_eia(match), 1, True |
| 33 |
|
| 34 |
elif match := re.fullmatch(r"(\d+)(\d)", code): |
| 35 |
return parse_regular_code(match), tolerance, tolerance_is_standardized |
| 36 |
|
| 37 |
elif match := re.fullmatch(r"(\d*)[R](\d*)", code, re.IGNORECASE): |
| 38 |
return parse_decimal_code(match), tolerance, tolerance_is_standardized |
| 39 |
|
| 40 |
elif match := re.fullmatch(r"(\d*)[ML](\d*)", code, re.IGNORECASE): |
| 41 |
return parse_milli_code(match), tolerance, tolerance_is_standardized |
| 42 |
else: |
| 43 |
return None |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
def parse_regular_code(match): |
| 48 |
digits = int(match.group(1)) |
| 49 |
multiplier = 10 ** int(match.group(2)) |
| 50 |
value = digits * multiplier |
| 51 |
return value |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def parse_eia(match): |
| 56 |
digits = eia_digit_lookup(match.group(1)) |
| 57 |
multiplier = eia_letter_lookup(match.group(2)) |
| 58 |
value = digits * multiplier |
| 59 |
return value |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def parse_decimal_code(match): |
| 64 |
if match.group(1): |
| 65 |
digits_left = match.group(1) |
| 66 |
else: |
| 67 |
digits_left = 0 |
| 68 |
|
| 69 |
if match.group(2): |
| 70 |
digits_right = match.group(2) |
| 71 |
else: |
| 72 |
digits_right = 0 |
| 73 |
|
| 74 |
value = float(f"{digits_left}.{digits_right}") |
| 75 |
return value |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
def parse_milli_code(match): |
| 80 |
value = parse_decimal_code(match) |
| 81 |
return value * 0.001 |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def eia_letter_lookup(key): |
| 86 |
table = { |
| 87 |
"Z": 0.001, |
| 88 |
"Y": 0.01, |
| 89 |
"R": 0.01, |
| 90 |
"X": 0.1, |
| 91 |
"S": 0.1, |
| 92 |
"A": 1, |
| 93 |
"B": 10, |
| 94 |
"H": 10, |
| 95 |
"C": 100, |
| 96 |
"D": 1000, |
| 97 |
"E": 10000, |
| 98 |
"F": 100000 |
| 99 |
} |
| 100 |
return table[key] |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
def eia_digit_lookup(key): |
| 105 |
table = { |
| 106 |
"01": 100, |
| 107 |
"02": 102, |
| 108 |
"03": 105, |
| 109 |
"04": 107, |
| 110 |
"05": 110, |
| 111 |
"06": 113, |
| 112 |
"07": 115, |
| 113 |
"08": 118, |
| 114 |
"09": 121, |
| 115 |
"10": 124, |
| 116 |
"11": 127, |
| 117 |
"12": 130, |
| 118 |
"13": 133, |
| 119 |
"14": 137, |
| 120 |
"15": 140, |
| 121 |
"16": 143, |
| 122 |
"17": 147, |
| 123 |
"18": 150, |
| 124 |
"19": 154, |
| 125 |
"20": 158, |
| 126 |
"21": 162, |
| 127 |
"22": 165, |
| 128 |
"23": 169, |
| 129 |
"24": 174, |
| 130 |
"25": 178, |
| 131 |
"26": 182, |
| 132 |
"27": 187, |
| 133 |
"28": 191, |
| 134 |
"29": 196, |
| 135 |
"30": 200, |
| 136 |
"31": 205, |
| 137 |
"32": 210, |
| 138 |
"33": 215, |
| 139 |
"34": 221, |
| 140 |
"35": 226, |
| 141 |
"36": 232, |
| 142 |
"37": 237, |
| 143 |
"38": 243, |
| 144 |
"39": 249, |
| 145 |
"40": 255, |
| 146 |
"41": 261, |
| 147 |
"42": 267, |
| 148 |
"43": 274, |
| 149 |
"44": 280, |
| 150 |
"45": 287, |
| 151 |
"46": 294, |
| 152 |
"47": 301, |
| 153 |
"48": 309, |
| 154 |
"49": 316, |
| 155 |
"50": 324, |
| 156 |
"51": 332, |
| 157 |
"52": 340, |
| 158 |
"53": 348, |
| 159 |
"54": 357, |
| 160 |
"55": 365, |
| 161 |
"56": 374, |
| 162 |
"57": 383, |
| 163 |
"58": 392, |
| 164 |
"59": 402, |
| 165 |
"60": 412, |
| 166 |
"61": 422, |
| 167 |
"62": 432, |
| 168 |
"63": 442, |
| 169 |
"64": 453, |
| 170 |
"65": 464, |
| 171 |
"66": 475, |
| 172 |
"67": 487, |
| 173 |
"68": 499, |
| 174 |
"69": 511, |
| 175 |
"70": 523, |
| 176 |
"71": 536, |
| 177 |
"72": 549, |
| 178 |
"73": 562, |
| 179 |
"74": 576, |
| 180 |
"75": 590, |
| 181 |
"76": 604, |
| 182 |
"77": 619, |
| 183 |
"78": 634, |
| 184 |
"79": 649, |
| 185 |
"80": 665, |
| 186 |
"81": 681, |
| 187 |
"82": 698, |
| 188 |
"83": 715, |
| 189 |
"84": 732, |
| 190 |
"85": 750, |
| 191 |
"86": 768, |
| 192 |
"87": 787, |
| 193 |
"88": 806, |
| 194 |
"89": 825, |
| 195 |
"90": 845, |
| 196 |
"91": 866, |
| 197 |
"92": 887, |
| 198 |
"93": 909, |
| 199 |
"94": 931, |
| 200 |
"95": 953, |
| 201 |
"96": 976 |
| 202 |
} |
| 203 |
return table[key] |