-
Notifications
You must be signed in to change notification settings - Fork 0
/
conAlm.py
129 lines (110 loc) · 4.11 KB
/
conAlm.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# std:
# n/a
# pip-int:
import dotsi;
# pip-ext:
# n/a
# loc:
# n/a
checkTruthyStr = lambda s: bool(s and type(s) is str);
def buildConcentricAlm (accessLevelList):
"""
Builds a concentric ALM, based on `accessLevelList`,
which should be a list of strings, from lowest to highest.
Eg.
alm = conAlm.build(['low', 'mid', 'high'])
alm.contains("mid", "low") # True
alm.contains("mid", "high") # False
alm.contains("mid", "mid") # True
alm.exceeds("mid", "mid") # False
""";
assert len(accessLevelList) >= 2;
assert len(accessLevelList) == len(set(accessLevelList));
assert all(map(checkTruthyStr, accessLevelList));
lim = {}; # Level to Index Map, internal helper.
for (i, level) in enumerate(accessLevelList):
lim[level] = i;
def expand (level):
"""
Expands`level`, returning the list of contained
access levels, including `level` itself.
Eg:
alm = conAlm.build("low", "mid", "high"])
alm.expand("low") # -> ["low"]
alm.expand("mid") # -> ["low", "mid"]
alm.expand("high") # -> ["low", "mid", "high"]
""";
assert level in lim;
return accessLevelList[0 : lim[level] + 1];
# ^Slice from lowest to given `level`, both incl.
def contains (perimeterLevel, testLevel):
"""
Checks if `perimeterLevel` includes `testLevel`.
Note: Each level is considered to contain itself.
Eg:
alm = conAlm.build("low", "mid", "high"])
alm.contains("mid", "low") # True
alm.contains("mid", "mid") # True
alm.contains("mid", "high") # False
""";
assert perimeterLevel in lim and testLevel in lim;
return lim[perimeterLevel] >= lim[testLevel];
def exceeds (higherLevel, testLevel):
"""
Checks if `higherLevel` strictly exceeds `testLevel`.
Note: No level is considered to exceed itself.
Eg:
alm = conAlm.build("low", "mid", "high"])
alm.exceeds("mid", "low") # True
alm.exceeds("mid", "mid") # False
alm.exceeds("mid", "high") # False
""";
assert higherLevel in lim and testLevel in lim;
return lim[higherLevel] > lim[testLevel];
return dotsi.fy({
"expand": expand,
"contains": contains,
"exceeds": exceeds,
"getMinLevel": lambda : accessLevelList[0],
"getMaxLevel": lambda : accessLevelList[-1],
"_accessLevelList": accessLevelList,
"_levelIndexMap": lim,
});
build = buildConcentricAlm; # Alias, short.
# Exo-use: `import conAlm; conAlm.build(["min", "mid", "max"]);`
def test_concentricAlm ():
alm = buildConcentricAlm(["low", "mid", "high"]);
# alm.expand:
assert alm.expand("low") == ["low"];
assert alm.expand("mid") == ["low", "mid"];
assert alm.expand("high") == ["low", "mid", "high"];
# alm.contains:
assert alm.contains("low", "low") == True;
assert alm.contains("low", "mid") == False;
assert alm.contains("low", "high") == False;
# cont.
assert alm.contains("mid", "low") == True;
assert alm.contains("mid", "mid") == True;
assert alm.contains("mid", "high") == False;
# cont.
assert alm.contains("high", "low") == True;
assert alm.contains("high", "mid") == True;
assert alm.contains("high", "high") == True;
# alm.exceeds:
assert alm.exceeds("low", "low") == False;
assert alm.exceeds("low", "mid") == False;
assert alm.exceeds("low", "high") == False;
# cont.
assert alm.exceeds("mid", "low") == True;
assert alm.exceeds("mid", "mid") == False;
assert alm.exceeds("mid", "high") == False;
# cont.
assert alm.exceeds("high", "low") == True;
assert alm.exceeds("high", "mid") == True;
assert alm.exceeds("high", "high") == False;
return True;
# Run tests if __main__: :::::::::::::::::::::::::::::::::::
if __name__ == "__main__":
test_concentricAlm();
print("Tests passed.");
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx