-
Notifications
You must be signed in to change notification settings - Fork 0
/
HALapi.py
65 lines (57 loc) · 2.37 KB
/
HALapi.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
import sys
import os.path
import platform
import datetime
def get_main_dir():
if main_is_frozen():
return os.path.dirname(sys.executable)
#for dirname in sys.path:
# if os.path.exists(os.path.join(dirname, __file__)):
# return dirname
return os.path.dirname(__file__)
def get_system_info():
if os.name == 'nt':
ver, build, sp, type = platform.win32_ver()
arch = platform.machine()
pyver = platform.python_version()
impl = platform.python_implementation()
return 'Windows {ver} {sp}, NT {build} on {arch} Python {pyver} ({impl}).'.format(ver=ver, sp=sp, build=build, arch=arch, pyver=pyver, impl=impl)
elif os.name == 'posix':
distro = platform.linux_distribution()
distro = '{0} {2} {1}'.format(*distro)
return '{name} ({distro}) {sysver}, Python {pyver} ({impl})'.format(name=platform.system(),
distro=distro,
sysver=platform.release(),
pyver=platform.python_version(),
impl=platform.python_implementation())
def main_is_frozen():
import imp
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze
def module_filter(name):
if not os.path.exists(name):
return False
if name[-3:].lower() == '.py':
return os.path.basename(name)[:-3]
elif os.path.isdir(name) and os.path.isfile(os.path.join(name, '__init__.py')):
return os.path.basename(name)
class HALException(Exception): pass
class HALcannotHandle(HALException): pass
maindir = get_main_dir()
datadir = os.path.join(maindir, 'data')
def clean_string(text, proper_letters=None):
if proper_letters is None:
proper_letters = ' 0123456789abcdefghijklmnopqrstuvwxyz'
letters = bytearray()
for letter in text.lower():
if letter in proper_letters:
letters.append(letter)
return str(letters)
def age_from_dob(dob):
today = datetime.date.today()
day = today.replace(year=dob.year)
age = today.year - dob.year
if dob > day:
age -= 1
return age