-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
84 lines (62 loc) · 1.9 KB
/
util.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
import datetime
from os import urandom
def random_string(length):
"""
Return a random cryptographically safe string of given length.
There are 62 possible characters, so there are 62^n possible strings of
length n.
"""
s = ""
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
while len(s) < length:
char_index = ord(urandom(1))
if char_index < len(chars):
s += chars[char_index]
return s
def is_int(string):
""" Test if a string can be interpreted as an int. """
try:
int(string)
return True
except:
return False
def is_pnr(l):
"""
Test if a sequence l is a valid swedish personal number.
TDDE23 labb 2 <3
"""
if len(l) != 10 or not is_int(l): return False
l = [int(i) for i in l]
ctrl = l.pop()
for i in range(0, len(l), 2): l[i] *= 2
s = sum([sum([int(j) for j in str(i)]) for i in l])
return (s + ctrl) % 10 == 0
def is_liuid(liuid):
""" Test if a string is a liuid. """
return len(liuid) <= 8 and liuid[:-3].islower() and is_int(liuid[-3:])
def is_id(id):
""" Test if id is either a liuid or a swedish personal number. """
return is_liuid(id) or is_pnr(id)
def is_email(email):
""" Test (not very thoroughly) if email is a valid email. """
return "@" in email
def is_date(date):
""" Test if date is a properly formatted date. """
try:
datetime.datetime.strptime(date, "%Y-%m-%d")
return True
except ValueError:
return False
def is_bool(b):
""" Test if b can be interpreted as a bool by sqlite3. """
return b in ["0", "1", 0, 1]
def member_to_dict(member):
liuid, name, email, joined, renewed, subscribed = member
return {
"id": liuid,
"name": name,
"email": email,
"joined": joined,
"renewed": renewed,
"subscribed": subscribed
}