Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1768a69
add 0000 file
Drake-Z Jun 16, 2016
f9050ec
add 0000 file
Drake-Z Jun 16, 2016
f81fea7
Add 0001 file
Drake-Z Jun 16, 2016
560d1bb
Fix
Drake-Z Jun 16, 2016
39257b5
Add 0001 file
Drake-Z Jun 16, 2016
1ba9512
Fix 0000
Drake-Z Jun 16, 2016
f9f2f18
Add 0002 file
Drake-Z Jun 16, 2016
19d20f6
Add 0002 file
Drake-Z Jun 16, 2016
ff7f69c
Fix 0003
Drake-Z Jun 16, 2016
1686400
Add 0003 file
Drake-Z Jun 16, 2016
5a286ef
Fix
Drake-Z Jun 16, 2016
198daec
Fix
Drake-Z Jun 16, 2016
85d1251
Fix 0002
Drake-Z Jun 16, 2016
793b273
Add 0004 file
Drake-Z Jun 16, 2016
47d4623
Add 0004 file
Drake-Z Jun 16, 2016
a853f2f
Add 0005 file
Drake-Z Jun 16, 2016
947398f
Fix 0001
Drake-Z Jun 16, 2016
ae38c19
Fix 0003
Drake-Z Jun 16, 2016
abe9719
Fix 0003 and 0004
Drake-Z Jun 16, 2016
bb08cd3
Fix 0004
Drake-Z Jun 16, 2016
5be83a8
Fix note
Drake-Z Jun 16, 2016
be7a105
Fix note
Drake-Z Jun 16, 2016
0dc5301
Fix 0006
Drake-Z Jun 17, 2016
25a04ac
Add 0006 file
Drake-Z Jun 17, 2016
11824ff
Add 0005 file
Drake-Z Jun 17, 2016
0043b02
Add 0007 file
Drake-Z Jun 17, 2016
4d5fc2b
Fix 0007
Drake-Z Jun 17, 2016
d9e87cd
Add 0008 file
Drake-Z Jun 17, 2016
1c064f2
Add 0009 file
Drake-Z Jun 17, 2016
971c9c4
Fix note
Drake-Z Jun 17, 2016
189e3f2
Add 0010 file
Drake-Z Jun 17, 2016
2078e16
Add 0011 file
Drake-Z Jun 17, 2016
ec4e4b9
Add 0011 file
Drake-Z Jun 17, 2016
87bef18
Add 0012 file
Drake-Z Jun 17, 2016
5656330
Add 0013 file
Drake-Z Jun 17, 2016
a459618
Add 0014 file
Drake-Z Jun 17, 2016
f41a9dd
Add 0015 file
Drake-Z Jun 17, 2016
114e65f
Fix 0014-0016
Drake-Z Jun 17, 2016
5f6899d
Fix 0015 0016
Drake-Z Jun 17, 2016
7f69ba0
Fix 0016
Drake-Z Jun 17, 2016
7586c8d
Add 0017-0019
Drake-Z Jun 17, 2016
18cb696
Add 0020-0022
Drake-Z Jun 17, 2016
69dfa89
Fix 0006
Drake-Z Jun 17, 2016
4564269
Add note
Drake-Z Jun 17, 2016
7ec7737
Fix 0013
Drake-Z Jun 17, 2016
df9c418
Fix 0009
Drake-Z Jun 17, 2016
9a92fb7
Fix 0014
Drake-Z Jun 18, 2016
8736adb
Fix 0014
Drake-Z Jun 18, 2016
fd752db
Fix 0014
Drake-Z Jun 18, 2016
ea7642d
Fix 0015
Drake-Z Jun 18, 2016
a3ee5a2
Fix 0016
Drake-Z Jun 18, 2016
5ebb574
Fix 0014 0015 0016
Drake-Z Jun 18, 2016
a334b1b
Fix 0014,0015,0016
Drake-Z Jun 18, 2016
51d85cf
Fix 0017
Drake-Z Jun 18, 2016
c11c4ad
Fix 0014
Drake-Z Jun 18, 2016
177302a
Fix 0018 0019
Drake-Z Jun 18, 2016
cd9c10a
Delete file
Drake-Z Jun 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Drake-Z/0000/0000.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。'

__author__ = 'Drake-Z'

from PIL import Image, ImageDraw, ImageFont

def add_num(filname, text = '4', fillcolor = (255, 0, 0)):
img = Image.open(filname)
width, height = img.size
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=width//8)
fillcolor = (255, 0, 0)
draw = ImageDraw.Draw(img)
draw.text((width-width//8, 0), text, font=myfont, fill=fillcolor)
img.save('1.jpg','jpeg')
return 0

if __name__ == '__main__':
filname = '0.jpg'
text = '4'
fillcolor = (255, 0, 0)
add_num(filname, text, fillcolor)
24 changes: 24 additions & 0 deletions Drake-Z/0001/0001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)'

__author__ = 'Drake-Z'

import random

def randChar(filename, digit=4, num=200):
f = open(filename, 'a')
for i in range(0, num):
for m in range(0, digit):
f.write(chr(random.randint(65, 90)))
f.write('\n')
f.close()
print('Done!')
return 0

if __name__ == '__main__':
filename = 'active_code.txt'
digit = 4
num = 200
rndChar(filename, digit, num)
37 changes: 37 additions & 0 deletions Drake-Z/0002/0002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。'

__author__ = 'Drake-Z'

import mysql.connector

def write_to_mysql(filename):
conn = mysql.connector.connect(user='root', password='986535', database='test')
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS user")
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
f = open(filename, 'r').readlines()
for line, num in zip(f, range(1, len(f)+1)):
line = line[:-1] #去除\n符号
cursor.execute('insert into user (id, name) values (%s, %s)', [str(num), line])
conn.commit()
cursor.close()
return 0

def search_mysql():
b = input('Search Active code(1-200):')
conn = mysql.connector.connect(user='root', password='986535', database='test')
cursor = conn.cursor()
cursor.execute('select * from user where id = %s', (b,))
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()
return 0

if __name__ == '__main__':
filename = 'active_code.txt'
write_to_mysql(filename)
search_mysql()
28 changes: 28 additions & 0 deletions Drake-Z/0003/0003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。'

__author__ = 'Drake-Z'

import redis

def write_to_redis(filename):
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.flushdb()
f = open(filename, 'r').readlines()
for line, num in zip(f, range(1, len(f)+1)):
line = line[:-1] #去除\n符号
r.set(num, line)
return 0

def search_redis():
b = int(input('Search Active code(1-200):'))
r = redis.StrictRedis(host='localhost', port=6379, db=0)
print(str(r.get(b),'UTF-8'))
return 0

if __name__ == '__main__':
filename = 'active_code.txt'
write_to_redis(filename)
search_redis()
18 changes: 18 additions & 0 deletions Drake-Z/0004/0004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。'

__author__ = 'Drake-Z'

import re

def statistics(file_path):
f = open(file_path, 'r').read()
f = re.findall(r'[\w\-\_\.\']+', f)
print(len(f))
return 0

if __name__ == '__main__':
file_path = 'English.txt'
statistics(file_path)
2 changes: 2 additions & 0 deletions Drake-Z/0004/English.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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.
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.
24 changes: 24 additions & 0 deletions Drake-Z/0005/0005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率(1136x640)的大小。'

__author__ = 'Drake-Z'

import os
import glob
from PIL import Image

def thumbnail_pic(path):
a = glob.glob(r'*.jpg')
for x in a:
name = os.path.join(path, x)
im = Image.open(name)
im.thumbnail((1136, 640))
print(im.format, im.size, im.mode)
im.save(name, 'JPEG')
print('Done!')

if __name__ == '__main__':
path = '.'
thumbnail_pic(path)
Binary file added Drake-Z/0005/ace-94.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Drake-Z/0006/0006.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。'

__author__ = 'Drake-Z'

import os
import re
import glob
from collections import OrderedDict

def get_num(key_word, filename):
'''获得词汇出现次数'''
f = open(filename, 'r', encoding='utf-8').read()
re_zhengze = re.compile(r'[\s\,\;\.\n]{1}'+key_word+r'[\s\,\;\.\n]{1}')
numbers = re_zhengze.findall(f)
return len(numbers)

def article_analysis(dirs):
article = glob.glob(r'*.txt')
dictdata = OrderedDict()
for m in article:
doc = open(m, 'r', encoding='utf-8').read()
doc = re.findall(r'[\w\-\_\.\']+', doc) #获得单词list
doc = list(map(lambda x: x.strip('.'), doc)) #去除句号
for n in doc:
dictdata[n] = get_num(n, m)
a = OrderedDict(sorted(dictdata.items(), key=lambda x: x[1], reverse = True)) #dict排序
print('在 %s 中出现次数最多的单词是:' % m)
for c in a:
print(c+' : %s 次' % a[c])
break
return 0

if __name__ == '__main__':
file = '.'
article_analysis(file)
1 change: 1 addition & 0 deletions Drake-Z/0006/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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.
3 changes: 3 additions & 0 deletions Drake-Z/0006/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.

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.
4 changes: 4 additions & 0 deletions Drake-Z/0006/003.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.
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.

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.
45 changes: 45 additions & 0 deletions Drake-Z/0007/0007.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。'

__author__ = 'Drake-Z'

import os
import re

def count_num(a, b):
shuzi = [0, 0, 0]
path = os.path.join(a, b)
f = open(path, 'r', encoding='UTF-8').readlines()
for i in f:
if re.match(r'^#', i) == None:
pass
else:
shuzi[1] += 1 #获得注释行数,只匹配单行注释
if f[-1][-1:]=='\n': #最后一行为空行时
shuzi[2] = f.count('\n')+1 #获得空行行数
shuzi[0] = len(f)+1 - shuzi[2] - shuzi[1] #获得代码行数
else:
shuzi[2] = f.count('\n')
shuzi[0] = len(f) - shuzi[2] - shuzi[1]
return shuzi

def file_analysis(c, d):
py = [x for x in os.listdir(c) if os.path.splitext(x)[1]==d] #获得文件列表
print(py)
the_num = [0, 0, 0]
for i in py:
num = count_num(c, i)
the_num[0] += num[0] #累计
the_num[1] += num[1]
the_num[2] += num[2]
print('''统计得目录中:
代码有 %s 行
注释 %s 行
空行 %s 行''' % (the_num[0], the_num[1], the_num[2]))

if __name__ == '__main__':
file = '.'
ext = '.py'
file_analysis(file, ext)
41 changes: 41 additions & 0 deletions Drake-Z/0007/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# mydict_test.py

import unittest
from MyMoudel.Moudel1 import Dict
class TestDict(unittest.TestCase):

def test_init(self):
d = Dict(a=1, b='test')
self.assertEqual(d.a, 1)
self.assertEqual(d.b, 'test')
self.assertTrue(isinstance(d, dict))

def test_key(self):
d = Dict()
d['key'] = 'value'
self.assertEqual(d.key, 'value')

def test_attr(self):
d = Dict()
d.key = 'value'
self.assertTrue('key' in d)
self.assertEqual(d['key'], 'value')

def test_keyerror(self):
d = Dict()
with self.assertRaises(KeyError):
value = d['empty']

def test_attrerror(self):
d = Dict()
with self.assertRaises(AttributeError):
value = d.empty

def setUp(self):
print('setUp...')

def tearDown(self):
print('tearDown...')
if __name__ == '__main__':
unittest.main()
33 changes: 33 additions & 0 deletions Drake-Z/0008/0008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0008 题:一个HTML文件,找出里面的正文。'

__author__ = 'Drake-Z'

from html.parser import HTMLParser
from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):
in_zhengwen = False
in_huanhang = False
def handle_starttag(self, tag, attrs):
if ('class', 'zh-summary summary clearfix') in attrs and tag=='div' :
self.in_zhengwen = True
elif ('class', 'zm-editable-content clearfix') in attrs and tag=='div' :
self.in_zhengwen = True
elif tag=='br':
print('\n')
else:
self.in_zhengwen = False

def handle_data(self, data):
if self.in_zhengwen:
print(data.strip())
else:
pass

if __name__ == '__main__':
parser = MyHTMLParser()
f = open('test.html', 'r', encoding = 'utf-8').read()
parser.feed(f)
24 changes: 24 additions & 0 deletions Drake-Z/0009/0009.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'第 0009 题:一个HTML文件,找出里面的链接。'

__author__ = 'Drake-Z'

import os, re
from html.parser import HTMLParser
from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):

def handle_starttag(self, tag, attrs):
if tag == 'a':
for (variables, value) in attrs:
if variables == 'href':
if re.match(r'http(.*?)', value):
print(value)

if __name__ == '__main__':
with open('test.html', encoding='utf-8') as html:
parser = MyHTMLParser()
parser.feed(html.read())
Loading