forked from ron-rivest/audit-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids.py
70 lines (45 loc) · 1.36 KB
/
ids.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
# ids.py
# Ronald L. Rivest (with Karim Husayn Karimi)
# July 24, 2017
# python3
"""
Routines in support of audit program multi.py.
These routines all related to ids:
contest ids (cids)
paper ballot collection ids (pbcids)
selection ids (selids)
ballot ids (bids)
measurement ids (mids)
"""
##############################################################################
## generic id-related routines
def clean_id(id):
"""
Return id with initial and final whitespace removed, and
with any internal whitespace sequences replaced by a single
blank. Also, all nonprintable characters are removed.
"""
id = id.strip()
new_id = ""
for c in id:
if c.isspace():
c = " "
if (c != " " or (len(new_id)>0 and new_id[-1] != " ")) \
and c.isprintable():
new_id += c
return new_id
def filename_safe(id):
"""
Remove all characters other than letters, digits, hyphen, underscore, and plus.
"""
ans = []
for c in id:
if c.isalnum() or c in "+-_":
ans.append(c)
return "".join(ans)
##############################################################################
## selid-specifc routines
def is_writein(selid):
return len(selid) > 0 and selid[0] == "+"
def is_error_selid(selid):
return len(selid) > 0 and selid[0] == "-"