Skip to content

Commit f19071c

Browse files
authored
Merge pull request #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.

Drake-Z/0006/002.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Being in a relationship can be a full-time job. Sometimes, it can become so overwhelming and consuming that you lose your own voice and sense of ownership. You want freedom and you think your partner is not making you happy enough. You question why you are in the relationship at all.
2+
3+
Yes, we all need that moment to feel uneasy and track our direction in whatever relationship we’re in. These breaks can be beneficial to becoming the person you want to be. Here are some of the positive things that can come out of those moments of indecision.

Drake-Z/0006/003.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Being lost can offer you freedom. You identify your core values, desires, and tastes. You are open with yourself and you can try new things. You understand that you do not have to always rely on your partner. Perhaps there is a movie coming out this weekend, you can buy a ticket and go to the cinemas yourself — something that may have been unheard of for you in the past. Being forced to find this independence can make you a stronger person.
2+
You can disconnect. You are no longer driven by the interests of your partner, but by your own desires. This means getting back to the basics, like reading books by your favorite author, exercising, eating your favorite meals. When you return to these ideals, you are strengthened and become a better you.
3+
4+
People think that being lost in a relationship signals doom for that relationship — it doesn’t. It all depends on how you manage this necessary stage and work towards making the most of it.

Drake-Z/0007/0007.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import os
9+
import re
10+
11+
def count_num(a, b):
12+
shuzi = [0, 0, 0]
13+
path = os.path.join(a, b)
14+
f = open(path, 'r', encoding='UTF-8').readlines()
15+
for i in f:
16+
if re.match(r'^#', i) == None:
17+
pass
18+
else:
19+
shuzi[1] += 1 #获得注释行数,只匹配单行注释
20+
if f[-1][-1:]=='\n': #最后一行为空行时
21+
shuzi[2] = f.count('\n')+1 #获得空行行数
22+
shuzi[0] = len(f)+1 - shuzi[2] - shuzi[1] #获得代码行数
23+
else:
24+
shuzi[2] = f.count('\n')
25+
shuzi[0] = len(f) - shuzi[2] - shuzi[1]
26+
return shuzi
27+
28+
def file_analysis(c, d):
29+
py = [x for x in os.listdir(c) if os.path.splitext(x)[1]==d] #获得文件列表
30+
print(py)
31+
the_num = [0, 0, 0]
32+
for i in py:
33+
num = count_num(c, i)
34+
the_num[0] += num[0] #累计
35+
the_num[1] += num[1]
36+
the_num[2] += num[2]
37+
print('''统计得目录中:
38+
代码有 %s 行
39+
注释 %s 行
40+
空行 %s 行''' % (the_num[0], the_num[1], the_num[2]))
41+
42+
if __name__ == '__main__':
43+
file = '.'
44+
ext = '.py'
45+
file_analysis(file, ext)

Drake-Z/0007/test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
# mydict_test.py
3+
4+
import unittest
5+
from MyMoudel.Moudel1 import Dict
6+
class TestDict(unittest.TestCase):
7+
8+
def test_init(self):
9+
d = Dict(a=1, b='test')
10+
self.assertEqual(d.a, 1)
11+
self.assertEqual(d.b, 'test')
12+
self.assertTrue(isinstance(d, dict))
13+
14+
def test_key(self):
15+
d = Dict()
16+
d['key'] = 'value'
17+
self.assertEqual(d.key, 'value')
18+
19+
def test_attr(self):
20+
d = Dict()
21+
d.key = 'value'
22+
self.assertTrue('key' in d)
23+
self.assertEqual(d['key'], 'value')
24+
25+
def test_keyerror(self):
26+
d = Dict()
27+
with self.assertRaises(KeyError):
28+
value = d['empty']
29+
30+
def test_attrerror(self):
31+
d = Dict()
32+
with self.assertRaises(AttributeError):
33+
value = d.empty
34+
35+
def setUp(self):
36+
print('setUp...')
37+
38+
def tearDown(self):
39+
print('tearDown...')
40+
if __name__ == '__main__':
41+
unittest.main()

Drake-Z/0008/0008.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'第 0008 题:一个HTML文件,找出里面的正文。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
from html.parser import HTMLParser
9+
from html.entities import name2codepoint
10+
11+
class MyHTMLParser(HTMLParser):
12+
in_zhengwen = False
13+
in_huanhang = False
14+
def handle_starttag(self, tag, attrs):
15+
if ('class', 'zh-summary summary clearfix') in attrs and tag=='div' :
16+
self.in_zhengwen = True
17+
elif ('class', 'zm-editable-content clearfix') in attrs and tag=='div' :
18+
self.in_zhengwen = True
19+
elif tag=='br':
20+
print('\n')
21+
else:
22+
self.in_zhengwen = False
23+
24+
def handle_data(self, data):
25+
if self.in_zhengwen:
26+
print(data.strip())
27+
else:
28+
pass
29+
30+
if __name__ == '__main__':
31+
parser = MyHTMLParser()
32+
f = open('test.html', 'r', encoding = 'utf-8').read()
33+
parser.feed(f)

Drake-Z/0009/0009.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+
'第 0009 题:一个HTML文件,找出里面的链接。'
5+
6+
__author__ = 'Drake-Z'
7+
8+
import os, re
9+
from html.parser import HTMLParser
10+
from html.entities import name2codepoint
11+
12+
class MyHTMLParser(HTMLParser):
13+
14+
def handle_starttag(self, tag, attrs):
15+
if tag == 'a':
16+
for (variables, value) in attrs:
17+
if variables == 'href':
18+
if re.match(r'http(.*?)', value):
19+
print(value)
20+
21+
if __name__ == '__main__':
22+
with open('test.html', encoding='utf-8') as html:
23+
parser = MyHTMLParser()
24+
parser.feed(html.read())

0 commit comments

Comments
 (0)