Skip to content

Commit abfe6f6

Browse files
committed
Merge pull request Show-Me-the-Code#173 from Show-Me-the-Code/master
PyLyria
2 parents 9af1464 + 1b63fb2 commit abfe6f6

File tree

748 files changed

+29172
-1059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

748 files changed

+29172
-1059
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ target/
5555

5656
# Mac File
5757
.DS_Store
58+
.idea

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "JiYouMCC"]
2+
path = JiYouMCC
3+
url = https://github.com/JiYouMCC/python-show-me-the-code

4disland/0000/add_num.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from PIL import Image, ImageDraw, ImageFont
2+
3+
def add_num(img):
4+
draw = ImageDraw.Draw(img)
5+
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40)
6+
fillcolor = "#ff0000"
7+
width, height = img.size
8+
draw.text((width-40, 0), '99', font=myfont, fill=fillcolor)
9+
img.save('result.jpg','jpeg')
10+
11+
return 0
12+
if __name__ == '__main__':
13+
image = Image.open('image.jpg')
14+
add_num(image)

AK-wang/0001/key_gen.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
5+
# 使用 Python 如何生成 200 个激活码(或者优惠券)?
6+
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(key_gen())
28+
return result
29+
30+
31+
def print_key(num):
32+
for i in key_num(num):
33+
print i
34+
35+
36+
if __name__ == "__main__":
37+
print_key(KEY_ALL)

AK-wang/0001/key_gen_deco.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
5+
# 使用 Python 如何生成 200 个激活码(或者优惠券)?
6+
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters+string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def print_key(func):
24+
def _print_key(num):
25+
for i in func(num):
26+
print i
27+
return _print_key
28+
29+
30+
@print_key
31+
def key_num(num, result=None):
32+
if result is None:
33+
result = []
34+
for i in range(num):
35+
result.append(key_gen())
36+
return result
37+
38+
39+
if __name__ == "__main__":
40+
# print_key(KEY_ALL)
41+
key_num(KEY_ALL)

AK-wang/0002/save_key.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
5+
6+
import MySQLdb
7+
import string
8+
import random
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(str(key_gen()))
28+
return result
29+
30+
31+
class mysql_init(object):
32+
33+
def __init__(self, conn):
34+
self.conn = None
35+
36+
# connect to mysql
37+
def connect(self):
38+
self.conn = MySQLdb.connect(
39+
host="localhost",
40+
port=3306,
41+
user="root",
42+
passwd="123456",
43+
db="test",
44+
charset="utf8"
45+
)
46+
47+
def cursor(self):
48+
try:
49+
return self.conn.cursor()
50+
except (AttributeError, MySQLdb.OperationalError):
51+
self.connect()
52+
return self.conn.cursor()
53+
54+
def commit(self):
55+
return self.conn.commit()
56+
57+
def close(self):
58+
return self.conn.close()
59+
60+
61+
def process():
62+
dbconn.connect()
63+
conn = dbconn.cursor()
64+
DropTable(conn)
65+
CreateTable(conn)
66+
InsertDatas(conn)
67+
QueryData(conn)
68+
dbconn.close()
69+
70+
# def execute(sql):
71+
# '''执行sql'''
72+
# conn=dbconn.cursor()
73+
# conn.execute(sql)
74+
75+
# def executemany(sql, tmp):
76+
# '''插入多条数据'''
77+
# conn=dbconn.cursor()
78+
# conn.executemany(sql,tmp)
79+
80+
81+
def query(sql, conn):
82+
'''查询sql'''
83+
# conn=dbconn.cursor()
84+
conn.execute(sql)
85+
rows = conn.fetchall()
86+
return rows
87+
88+
89+
def DropTable(conn):
90+
# conn=dbconn.cursor()
91+
conn.execute("DROP TABLE IF EXISTS `user_key`")
92+
93+
94+
def CreateTable(conn):
95+
# conn=dbconn.cursor()
96+
sql_create = ''' CREATE TABLE `user_key` (`key` varchar(50) NOT NULL)'''
97+
conn.execute(sql_create)
98+
99+
100+
def InsertDatas(conn):
101+
# conn=dbconn.cursor()
102+
# insert_sql = "insert into user_key values(%s)"
103+
insert_sql = "INSERT INTO user_key VALUES (%(value)s)"
104+
key_list = key_num(KEY_ALL)
105+
# print len(key_list)
106+
# conn.executemany(insert_sql,str(key_listi))
107+
# conn.executemany("INSERT INTO user_key VALUES (%(value)s)",
108+
# [dict(value=v) for v in key_list])
109+
conn.executemany(insert_sql, [dict(value=v) for v in key_list])
110+
111+
112+
def DeleteData():
113+
del_sql = "delete from user_key where id=2"
114+
execute(del_sql)
115+
116+
117+
def QueryData(conn):
118+
sql = "select * from user_key"
119+
rows = query(sql, conn)
120+
printResult(rows)
121+
122+
123+
def printResult(rows):
124+
if rows is None:
125+
print "rows None"
126+
for row in rows:
127+
print row
128+
129+
if __name__ == "__main__":
130+
dbconn = mysql_init(None)
131+
process()

AK-wang/0003/save_to_redis.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
5+
6+
import string
7+
import random
8+
import redis
9+
10+
KEY_LEN = 20
11+
KEY_ALL = 200
12+
13+
14+
def base_str():
15+
return (string.letters + string.digits)
16+
17+
18+
def key_gen():
19+
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
20+
return ("".join(keylist))
21+
22+
23+
def key_num(num, result=None):
24+
if result is None:
25+
result = []
26+
for i in range(num):
27+
result.append(key_gen())
28+
return result
29+
30+
31+
def redis_init():
32+
r = redis.Redis(host='localhost', port=6379, db=0)
33+
return r
34+
35+
36+
def push_to_redis(key_list):
37+
for key in key_list:
38+
redis_init().lpush('key', key)
39+
40+
41+
def get_from_redis():
42+
key_list = redis_init().lrange('key', 0, -1)
43+
for key in key_list:
44+
print key
45+
46+
if __name__ == "__main__":
47+
push_to_redis(key_num(200))
48+
get_from_redis()

AK-wang/0004/0004.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
4+
# 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数
5+
6+
from collections import Counter
7+
import re
8+
9+
10+
def creat_list(filename):
11+
datalist = []
12+
with open(filename, 'r') as f:
13+
for line in f:
14+
content = re.sub("\"|,|\.", "", line)
15+
datalist.extend(content.strip().split(' '))
16+
return datalist
17+
18+
19+
def wc(filename):
20+
print Counter(creat_list(filename))
21+
22+
if __name__ == "__main__":
23+
filename = 'test.txt'
24+
wc(filename)

AK-wang/0004/test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
In the latest move to support the economy,
2+
Shanghai, Beijing, Chongqing and six other provinces and municipalities will allow banks to refinance high-quality credit assets rated by the People's Bank of China,
3+
said the central bank, as the program was first introduced in Guangdong and Shandong provinces last year.
4+

AK-wang/0006/00.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
In China, when people go across the road, they will never wait for the red light
2+
patiently. In fact, Chinese people are famous for running the green light, it
3+
seems to be a habit for them, the traffic rule is just the paper for them, they
4+
never obey it. The result of going against the traffic rule is serious.
5+
we we we we we we we

0 commit comments

Comments
 (0)