Skip to content

Commit 66ac0e4

Browse files
committed
Merge pull request Show-Me-the-Code#89 from DIYgod/master
Complete 0007
2 parents ec1b0dc + bd7ad02 commit 66ac0e4

File tree

9 files changed

+188
-3
lines changed

9 files changed

+188
-3
lines changed

DIYgod/0006/a.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
f = open('a.txt', 'rb')
2+
lines = f.readlines()
3+
for line in lines:
4+
pass
5+
f.close()
6+
7+
f = open('a.txt', 'rb')
8+
for line in f:
9+
pass
10+
f.close()
11+
12+
f = open('a.txt', 'rb')
13+
while true:
14+
line = f.readline()
15+
if not line:
16+
break
17+
pass
18+
f.close()

DIYgod/0006/important_word.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ def get_important_word(files):
2424
words = re.findall(r'[a-zA-Z0-9]+', s)
2525
for word in words:
2626
worddict[word] = worddict[word] + 1 if word in worddict else 1
27+
f.close()
2728
wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True)
2829
return wordsort
2930

3031
if __name__ == '__main__':
3132
files = get_files('.')
3233
print files
33-
print get_important_word(files)
34+
wordsort = get_important_word(files)
35+
# 避免遗漏有多个最大值的情况
36+
maxnum = 1
37+
for i in range(len(wordsort) - 1):
38+
if wordsort[i][1] == wordsort[i + 1][1]:
39+
maxnum += 1
40+
else:
41+
break
42+
for i in range(maxnum):
43+
print wordsort[i]

DIYgod/0006/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from PIL import Image
4+
5+
def change_resolution(picPath, reslution):
6+
img = Image.open(picPath)
7+
x, y = img.size
8+
print x, y
9+
changex = float(x) / reslution[0]
10+
changey = float(y) / reslution[1]
11+
12+
# 判断分辨率是否满足
13+
if changex > 1 or changey > 1:
14+
change = changex if changex > changey else changey
15+
print change
16+
print int(reslution[0] / change), int(reslution[1] / change)
17+
img.resize((int(x / change), int(y / change))).save('result.jpg')
18+
19+
if __name__ == '__main__':
20+
change_resolution('pictest.jpg', (1136, 640))

DIYgod/0006/test/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from PIL import Image
4+
5+
def change_resolution(picPath, reslution):
6+
img = Image.open(picPath)
7+
x, y = img.size
8+
print x, y
9+
changex = float(x) / reslution[0]
10+
changey = float(y) / reslution[1]
11+
12+
# 判断分辨率是否满足
13+
if changex > 1 or changey > 1:
14+
change = changex if changex > changey else changey
15+
print change
16+
print int(reslution[0] / change), int(reslution[1] / change)
17+
img.resize((int(x / change), int(y / change))).save('result.jpg')
18+
19+
if __name__ == '__main__':
20+
change_resolution('pictest.jpg', (1136, 640))

DIYgod/0006/test/test.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

DIYgod/0007/count_lines.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
5+
# Get all files in designated path
6+
def get_files(path):
7+
filepath = os.listdir(path)
8+
files = []
9+
for fp in filepath:
10+
fppath = path + '/' + fp
11+
if(os.path.isfile(fppath)):
12+
files.append(fppath)
13+
elif(os.path.isdir(fppath)):
14+
files += get_files(fppath)
15+
return files
16+
17+
# Count lines and blank lines and note lines in designated files
18+
def count_lines(files):
19+
line, blank, note = 0, 0, 0
20+
for filename in files:
21+
f = open(filename, 'rb')
22+
for l in f:
23+
l = l.strip()
24+
line += 1
25+
if l == '':
26+
blank += 1
27+
elif l[0] == '#' or l[0] == '/':
28+
note += 1
29+
f.close()
30+
return (line, blank, note)
31+
32+
if __name__ == '__main__':
33+
files = get_files('.')
34+
print files
35+
lines = count_lines(files)
36+
print 'Line(s): %d, black line(s): %d, note line(s): %d' % (lines[0], lines[1], lines[2])

DIYgod/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+
3+
import re
4+
import os
5+
6+
# Get all files in designated path
7+
def get_files(path):
8+
filepath = os.listdir(path)
9+
files = []
10+
for fp in filepath:
11+
fppath = path + '/' + fp
12+
if(os.path.isfile(fppath)):
13+
files.append(fppath)
14+
elif(os.path.isdir(fppath)):
15+
files += get_files(fppath)
16+
return files
17+
18+
# Get the most popular word in designated files
19+
def get_important_word(files):
20+
worddict = {}
21+
for filename in files:
22+
f = open(filename, 'rb')
23+
s = f.read()
24+
words = re.findall(r'[a-zA-Z0-9]+', s)
25+
for word in words:
26+
worddict[word] = worddict[word] + 1 if word in worddict else 1
27+
wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True)
28+
return wordsort
29+
30+
if __name__ == '__main__':
31+
files = get_files('.')
32+
print files
33+
wordsort = get_important_word(files)
34+
maxnum = 1
35+
for i in range(len(wordsort) - 1):
36+
if wordsort[i][1] == wordsort[i + 1][1]:
37+
maxnum += 1
38+
else:
39+
break
40+
for i in range(maxnum):
41+
print wordsort[i]

DIYgod/0007/test/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+
3+
import re
4+
import os
5+
6+
# Get all files in designated path
7+
def get_files(path):
8+
filepath = os.listdir(path)
9+
files = []
10+
for fp in filepath:
11+
fppath = path + '/' + fp
12+
if(os.path.isfile(fppath)):
13+
files.append(fppath)
14+
elif(os.path.isdir(fppath)):
15+
files += get_files(fppath)
16+
return files
17+
18+
# Get the most popular word in designated files
19+
def get_important_word(files):
20+
worddict = {}
21+
for filename in files:
22+
f = open(filename, 'rb')
23+
s = f.read()
24+
words = re.findall(r'[a-zA-Z0-9]+', s)
25+
for word in words:
26+
worddict[word] = worddict[word] + 1 if word in worddict else 1
27+
wordsort = sorted(worddict.items(), key=lambda e:e[1], reverse=True)
28+
return wordsort
29+
30+
if __name__ == '__main__':
31+
files = get_files('.')
32+
print files
33+
wordsort = get_important_word(files)
34+
maxnum = 1
35+
for i in range(len(wordsort) - 1):
36+
if wordsort[i][1] == wordsort[i + 1][1]:
37+
maxnum += 1
38+
else:
39+
break
40+
for i in range(maxnum):
41+
print wordsort[i]

DIYgod/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Python 练习册,每天一个小程序,所有题目点击这里查看:https://github.com/Show-Me-the-Code/show-me-the-code

0 commit comments

Comments
 (0)