Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f19071c

Browse files
authored
Merge pull request Show-Me-the-Code#198 from Drake-Z/master
Add file
2 parents cc18968 + cd9c10a commit f19071c

File tree

31 files changed

+834
-0
lines changed

31 files changed

+834
-0
lines changed

Drake-Z/0000/0000.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
from PIL import Image, ImageDraw, ImageFont
9+
10+
def add_num(filname, text = '4', fillcolor = (255, 0, 0)):
11+
img = Image.open(filname)
12+
width, height = img.size
13+
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=width//8)
14+
fillcolor = (255, 0, 0)
15+
draw = ImageDraw.Draw(img)
16+
draw.text((width-width//8, 0), text, font=myfont, fill=fillcolor)
17+
img.save('1.jpg','jpeg')
18+
return 0
19+
20+
if __name__ == '__main__':
21+
filname = '0.jpg'
22+
text = '4'
23+
fillcolor = (255, 0, 0)
24+
add_num(filname, text, fillcolor)

Drake-Z/0001/0001.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import random
9+
10+
def randChar(filename, digit=4, num=200):
11+
f = open(filename, 'a')
12+
for i in range(0, num):
13+
for m in range(0, digit):
14+
f.write(chr(random.randint(65, 90)))
15+
f.write('\n')
16+
f.close()
17+
print('Done!')
18+
return 0
19+
20+
if __name__ == '__main__':
21+
filename = 'active_code.txt'
22+
digit = 4
23+
num = 200
24+
rndChar(filename, digit, num)

Drake-Z/0002/0002.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import mysql.connector
9+
10+
def write_to_mysql(filename):
11+
conn = mysql.connector.connect(user='root', password='986535', database='test')
12+
cursor = conn.cursor()
13+
cursor.execute("DROP TABLE IF EXISTS user")
14+
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
15+
f = open(filename, 'r').readlines()
16+
for line, num in zip(f, range(1, len(f)+1)):
17+
line = line[:-1] #去除\n符号
18+
cursor.execute('insert into user (id, name) values (%s, %s)', [str(num), line])
19+
conn.commit()
20+
cursor.close()
21+
return 0
22+
23+
def search_mysql():
24+
b = input('Search Active code(1-200):')
25+
conn = mysql.connector.connect(user='root', password='986535', database='test')
26+
cursor = conn.cursor()
27+
cursor.execute('select * from user where id = %s', (b,))
28+
values = cursor.fetchall()
29+
print(values)
30+
cursor.close()
31+
conn.close()
32+
return 0
33+
34+
if __name__ == '__main__':
35+
filename = 'active_code.txt'
36+
write_to_mysql(filename)
37+
search_mysql()

Drake-Z/0003/0003.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import redis
9+
10+
def write_to_redis(filename):
11+
r = redis.StrictRedis(host='localhost', port=6379, db=0)
12+
r.flushdb()
13+
f = open(filename, 'r').readlines()
14+
for line, num in zip(f, range(1, len(f)+1)):
15+
line = line[:-1] #去除\n符号
16+
r.set(num, line)
17+
return 0
18+
19+
def search_redis():
20+
b = int(input('Search Active code(1-200):'))
21+
r = redis.StrictRedis(host='localhost', port=6379, db=0)
22+
print(str(r.get(b),'UTF-8'))
23+
return 0
24+
25+
if __name__ == '__main__':
26+
filename = 'active_code.txt'
27+
write_to_redis(filename)
28+
search_redis()

Drake-Z/0004/0004.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import re
9+
10+
def statistics(file_path):
11+
f = open(file_path, 'r').read()
12+
f = re.findall(r'[\w\-\_\.\']+', f)
13+
print(len(f))
14+
return 0
15+
16+
if __name__ == '__main__':
17+
file_path = 'English.txt'
18+
statistics(file_path)

Drake-Z/0004/English.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ConnectionPools manage a set of Connection instances. redis-py ships with two types of Connections. The default, Connection, is a normal TCP socket based connection. The UnixDomainSocketConnection allows for clients running on the same device as the server to connect via a unix domain socket.
2+
To use a, UnixDomainSocketConnection connection, simply pass the unix_socket_path argument, which is a string to the unix domain socket file. Additionally, make sure the unixsocket parameter is defined in your redis.conf file. It's commented out by default.

Drake-Z/0005/0005.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率(1136x640)的大小。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import os
9+
import glob
10+
from PIL import Image
11+
12+
def thumbnail_pic(path):
13+
a = glob.glob(r'*.jpg')
14+
for x in a:
15+
name = os.path.join(path, x)
16+
im = Image.open(name)
17+
im.thumbnail((1136, 640))
18+
print(im.format, im.size, im.mode)
19+
im.save(name, 'JPEG')
20+
print('Done!')
21+
22+
if __name__ == '__main__':
23+
path = '.'
24+
thumbnail_pic(path)

Drake-Z/0005/ace-94.jpg

109 KB
Loading

Drake-Z/0006/0006.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import os
9+
import re
10+
import glob
11+
from collections import OrderedDict
12+
13+
def get_num(key_word, filename):
14+
'''获得词汇出现次数'''
15+
f = open(filename, 'r', encoding='utf-8').read()
16+
re_zhengze = re.compile(r'[\s\,\;\.\n]{1}'+key_word+r'[\s\,\;\.\n]{1}')
17+
numbers = re_zhengze.findall(f)
18+
return len(numbers)
19+
20+
def article_analysis(dirs):
21+
article = glob.glob(r'*.txt')
22+
dictdata = OrderedDict()
23+
for m in article:
24+
doc = open(m, 'r', encoding='utf-8').read()
25+
doc = re.findall(r'[\w\-\_\.\']+', doc) #获得单词list
26+
doc = list(map(lambda x: x.strip('.'), doc)) #去除句号
27+
for n in doc:
28+
dictdata[n] = get_num(n, m)
29+
a = OrderedDict(sorted(dictdata.items(), key=lambda x: x[1], reverse = True)) #dict排序
30+
print('在 %s 中出现次数最多的单词是:' % m)
31+
for c in a:
32+
print(c+' : %s 次' % a[c])
33+
break
34+
return 0
35+
36+
if __name__ == '__main__':
37+
file = '.'
38+
article_analysis(file)

Drake-Z/0006/001.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You are not so absorbed about them. You see them for their flaws and perfections. Since you can rely on your instincts and find other pleasures, rather than just within the relationship itself, you understand your partner better. This knowledge will prove beneficial in the way you treat them.

0 commit comments

Comments
 (0)