forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
50 lines (42 loc) · 1.69 KB
/
Copy pathsolution.py
File metadata and controls
50 lines (42 loc) · 1.69 KB
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
# coding: utf-8
'''
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
使用 Python 如何生成 200 个激活码(或者优惠券)?
笔记参见:
http://liam0205.me/2015/05/07/generator-of-invitation-code-in-python/
'''
import random, string
class LengthError(ValueError):
def __init__(self, arg):
self.args = arg
def pad_zero_to_left(inputNumString, totalLength):
'''
takes inputNumString as input,
pads zero to its left, and make it has the length totalLength
1. calculates the length of inputNumString
2. compares the length and totalLength
2.1 if length > totalLength, raise an error
2.2 if length == totalLength, return directly
2.3 if length < totalLength, pads zeros to its left
'''
lengthOfInput = len(inputNumString)
if lengthOfInput > totalLength:
raise LengthError("The length of input is greater than the total\ length.")
else:
return '0' * (totalLength - lengthOfInput) + inputNumString
poolOfChars = string.ascii_letters + string.digits
random_codes = lambda x, y: ''.join([random.choice(x) for i in range(y)])
def invitation_code_generator(quantity, lengthOfRandom, LengthOfKey):
'''
generate `quantity` invitation codes
'''
placeHoldChar = "L"
for index in range(quantity):
tempString = ""
try:
yield random_codes(poolOfChars, lengthOfRandom) + placeHoldChar + \
pad_zero_to_left(str(index), LengthOfKey)
except LengthError:
print "Index exceeds the length of master key."
for invitationCode in invitation_code_generator(200, 16, 4):
print invitationCode