fix_bt.py 2.66 KB
Newer Older
Roelofsen, Hans's avatar
Roelofsen, Hans committed
1
2
import numbers

Roelofsen, Hans's avatar
Roelofsen, Hans committed
3

4
def fix_bt(code_in, as_mnp=False, verbose=False, strict=True):
Roelofsen, Hans's avatar
Roelofsen, Hans committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    """
    Parser for Beheertype codes to repair abbreviated codes and/or MNP extension
    eg. 02.01     --> N02.01 if as_mnp==False
        02.01     --> N01.01.00 if as_mnp==True
        N05.01    --> N05.01  if as_mnp==False
        N05.01    --> N05.01.00  if as_mnp==True
        N04.02.01 --> N04.02.01 if as_mnp==False
        N04.02.01 --> N04.02.01 if as_mnp==True
    BT Code is as follows <N><AA>.<BB>.<CC>
    N = letter, may be missing
    A = two digits indicating top level beheertype "TOP"
    B = two digits indicating sub level beheertype "SUB"
    C = two digits indicating neergeschaald beheertype if > 00. Else "00" as used in MNP param notation  "NEER"
    N04.02.00 is equivalent to N04.02
    :param code_in: beheertypecode in any format
    :param as_mnp: return MNP style beheertypecode, default=False
21
22
    :param verbose: verbose feedback
    :param strict: raise error if bt code is unexpected
Roelofsen, Hans's avatar
Roelofsen, Hans committed
23
24
    :return: beheertypecode
    """
Roelofsen, Hans's avatar
Roelofsen, Hans committed
25
26
27
28

    if verbose:
        print('from {}'.format(code_in), end=' ')

Roelofsen, Hans's avatar
Roelofsen, Hans committed
29
30
31
32
33
34
    if isinstance(code_in, numbers.Number):
        code_in = str(code_in)

    # Identify parts of code
    parts = code_in.split(".")
    if len(parts) == 1:
35
36
37
38
        if strict:
            raise AssertionError("1. Unexpected BT code: {}".format(code_in))
        else:
            return None
Roelofsen, Hans's avatar
Roelofsen, Hans committed
39
40
41
42
43
    elif len(parts) == 2:
        top, sub = parts
        neer = None
    elif len(parts) == 3:
        top, sub, neer = parts
44
    else:
45
46
47
48
        if strict:
            raise AssertionError("2. Unexpected BT code: {}".format(code_in))
        else:
            return None
Roelofsen, Hans's avatar
Roelofsen, Hans committed
49
50

    # Verify TOP and add "N" is required
Roelofsen, Hans's avatar
Roelofsen, Hans committed
51
52
53
54
55
56
    assert top[0].upper() in ['N', 'W', 'S', 'T', 'A', 'L'], "3. Unexpected BT Code: {}".format(code_in)
    if top[0].islower():
        top = top.capitalize()
    assert top[1].isdigit(), '3. Unexpected BT code: {}'.format(code_in)
    assert top[2].isdigit(), '3. Unexpected BT code: {}'.format(code_in)
    assert len(top) == 3, "3. Unexpected BT Code: {}".format(code_in)
Roelofsen, Hans's avatar
Roelofsen, Hans committed
57
58

    # Verify SUB
Roelofsen, Hans's avatar
Roelofsen, Hans committed
59
    assert sub.isdigit and len(sub) == 2, "4. Unexpected BT Code: {}".format(code_in)
Roelofsen, Hans's avatar
Roelofsen, Hans committed
60
61
62
63

    # Verify NEER and check if it is neergeschaald or not
    keep_neer = False
    if neer:
Roelofsen, Hans's avatar
Roelofsen, Hans committed
64
        assert neer.isdigit and len(neer) == 2, "5. Unexpected BT Code: {}".format(code_in)
Roelofsen, Hans's avatar
Roelofsen, Hans committed
65
66
67
68
69
70
        if int(neer) > 0:
            keep_neer = True

    # Construct output
    head = "{}.{}".format(top, sub)
    if keep_neer:
Roelofsen, Hans's avatar
Roelofsen, Hans committed
71
        output = "{}.{}".format(head, neer)
Roelofsen, Hans's avatar
Roelofsen, Hans committed
72
    elif as_mnp:
Roelofsen, Hans's avatar
Roelofsen, Hans committed
73
        output = "{}.00".format(head)
Roelofsen, Hans's avatar
Roelofsen, Hans committed
74
    else:
Roelofsen, Hans's avatar
Roelofsen, Hans committed
75
76
77
78
        output = head
    if verbose:
        print('to {}'.format(output))
    return output