-
Notifications
You must be signed in to change notification settings - Fork 0
/
vouchers.py
executable file
·43 lines (40 loc) · 1.58 KB
/
vouchers.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
from random import choice
from datetime import date, timedelta
def generate(count=1, existing=[], length=8, pattern='0123456789ABCDEF'):
'''
Generate voucher codes for the PyFMI course.
`count` represents how many codes should be generated
`existing` is a list with already existing codes
`length` stands for the code's length.
`pattern` defines what chars to be uncluded in out code.
'''
generated = []
fails = 0
for code in range(count):
voucher = ''
for char in range(length):
voucher += choice(pattern)
if voucher in existing:
fails += 1
else:
generated.append(voucher)
if fails > 0:
generated += generate(fails)
return generated
def to_html(codes=[], days_valid=15, output='index.html', encoding='utf-8'):
'''
Convert generated vouchers in a html page, so it could be printed out
`codes[]` takes the generated codes
`days_valid` is used for calculating the' valid until' of codes
`output` points to the file, where all this should be printed
'''
html_code = ''
valid_until = (date.today() + timedelta(days = days_valid)).strftime('%d.%m.%Y')
with open('html/header.html', 'r', encoding = encoding) as header:
html_code += header.read()
for code in codes:
html_code += '<li><h1 class="code">{code}</h1></li>'.format(code=str(code))
with open('html/footer.html', 'r', encoding = encoding) as footer:
html_code += footer.read()
with open('html/index.html', 'w', encoding = encoding) as file:
file.write(html_code)