Skip to content

Commit 5572040

Browse files
committed
Merge pull request Show-Me-the-Code#18 from JiYouMCC/master
Code of 0-2 4-5
2 parents 340fb6c + f1a819c commit 5572040

10 files changed

Lines changed: 164 additions & 0 deletions

File tree

JiYouMCC/0000/0000.png

2.54 KB
Loading

JiYouMCC/0000/0000.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# 第0000题:将你的QQ头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示。
3+
4+
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
5+
from PIL import Image
6+
from PIL import ImageFont
7+
from PIL import ImageDraw
8+
9+
10+
def write_number(image_file_path, number=1):
11+
img = Image.open(image_file_path)
12+
font_size = img.size[0] if img.size[0] < img.size[1] else img.size[1]
13+
font_size = font_size / 4
14+
number_txt = str(number) + ' ' if number < 100 else '99+'
15+
font = ImageFont.truetype("arial.ttf", size=font_size)
16+
if font.getsize(number_txt)[0] > img.size[0] or font.getsize(number_txt)[1] > img.size[1]:
17+
return img
18+
position = img.size[0] - font.getsize(number_txt)[0]
19+
ImageDraw.Draw(img).text((position, 0), number_txt, (255, 0, 0), font)
20+
return img
21+
22+
write_number('0000.png').save('result.png')
23+
write_number('0000.png', 100).save('result100.png')

JiYouMCC/0001/0001.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
# 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
3+
4+
import uuid
5+
6+
7+
def create_code(number=200):
8+
result = []
9+
while True is True:
10+
temp = str(uuid.uuid1()).replace('-', '')
11+
if not temp in result:
12+
result.append(temp)
13+
if len(result) is number:
14+
break
15+
return result
16+
17+
print create_code()

JiYouMCC/0002/0002.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
3+
# using sina app
4+
# test page:http://mccatcivitas.sinaapp.com/showmecode2
5+
import sae.const
6+
import MySQLdb
7+
import uuid
8+
9+
10+
def create_code(number=200):
11+
result = []
12+
while True is True:
13+
temp = str(uuid.uuid1()).replace('-', '')
14+
if not temp in result:
15+
result.append(temp)
16+
if len(result) is number:
17+
break
18+
return result
19+
20+
21+
def insertCode(code, table='app_mccatcivitas.showmethecode'):
22+
conn = MySQLdb.connect(
23+
host=sae.const.MYSQL_HOST,
24+
user=sae.const.MYSQL_USER,
25+
passwd=sae.const.MYSQL_PASS,
26+
port=int(sae.const.MYSQL_PORT),
27+
charset='utf8')
28+
cur = conn.cursor()
29+
cur.execute("""insert into %s values('%s')""" % (
30+
table, code))
31+
conn.commit()
32+
cur.close()
33+
conn.close()
34+
35+
36+
def selectCodes(table='app_mccatcivitas.showmethecode'):
37+
connection = MySQLdb.connect(
38+
host=sae.const.MYSQL_HOST,
39+
user=sae.const.MYSQL_USER,
40+
passwd=sae.const.MYSQL_PASS,
41+
port=int(sae.const.MYSQL_PORT),
42+
init_command='set names utf8')
43+
cur = connection.cursor()
44+
cur.execute("""select * from %s""" % (table))
45+
result = []
46+
rows = cur.fetchall()
47+
for row in rows:
48+
result.append(str(row[0]))
49+
return result
50+
51+
52+
def cleanUp(table='app_mccatcivitas.showmethecode'):
53+
connection = MySQLdb.connect(
54+
host=sae.const.MYSQL_HOST,
55+
user=sae.const.MYSQL_USER,
56+
passwd=sae.const.MYSQL_PASS,
57+
port=int(sae.const.MYSQL_PORT),
58+
init_command='set names utf8')
59+
cur = connection.cursor()
60+
try:
61+
cur.execute("""drop table %s""" % (table))
62+
except Exception, e:
63+
print e
64+
connection.commit()
65+
cur.execute(
66+
"""create table %s (code char(32) not null primary key)""" % (table))
67+
connection.commit()
68+
cur.close()
69+
connection.close()
70+
71+
72+
def Process():
73+
cleanUp()
74+
code = create_code()
75+
for c in code:
76+
insertCode(c)
77+
result = selectCodes()
78+
return result

JiYouMCC/0003/0003.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
3+
# fail to install redis, skip it

JiYouMCC/0004/0004.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。
3+
import io
4+
import operator
5+
6+
7+
def get_count_table(file='0004.txt', ignore=[',', '.', ':', '!', '?', '”', '“', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'], lower=True):
8+
txt = open(file).read()
9+
for i in ignore:
10+
txt = txt.replace(i, ' ')
11+
if lower:
12+
txt = txt.lower()
13+
words = txt.split(' ')
14+
dic = {}
15+
for word in words:
16+
if word is '':
17+
continue
18+
if word in dic:
19+
dic[word] += 1
20+
else:
21+
dic[word] = 1
22+
return dic
23+
24+
25+
result = sorted(
26+
get_count_table().items(), key=operator.itemgetter(1), reverse=True)
27+
for item in result:
28+
print item[0], item[1]

JiYouMCC/0004/0004.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Then I looked, and behold, on Mount Zion stood the Lamb, and with him 144,000 who had his name and his Father’s name written on their foreheads.And I heard a voice from heaven like the roar of many waters and like the sound of loud thunder. The voice I heard was like the sound of harpists playing on their harps,and they were singing a new song before the throne and before the four living creatures and before the elders. No one could learn that song except the 144,000 who had been redeemed from the earth.It is these who have not defiled themselves with women, for they are virgins. It is these who follow the Lamb wherever he goes. These have been redeemed from mankind as firstfruits for God and the Lamb,and in their mouth no lie was found, for they are blameless.The Messages of the Three AngelsThen I saw another angel flying directly overhead, with an eternal gospel to proclaim to those who dwell on earth, to every nation and tribe and language and people.And he said with a loud voice, “Fear God and give him glory, because the hour of his judgment has come, and worship him who made heaven and earth, the sea and the springs of water.”Another angel, a second, followed, saying, “Fallen, fallen is Babylon the great, she who made all nations drink the wine of the passion1 of her sexual immorality.”And another angel, a third, followed them, saying with a loud voice, “If anyone worships the beast and its image and receives a mark on his forehead or on his hand,10 he also will drink the wine of God’s wrath, poured full strength into the cup of his anger, and he will be tormented with fire and sulfur in the presence of the holy angels and in the presence of the Lamb.And the smoke of their torment goes up forever and ever, and they have no rest, day or night, these worshipers of the beast and its image, and whoever receives the mark of its name.”Here is a call for the endurance of the saints, those who keep the commandments of God and their faith in Jesus.2And I heard a voice from heaven saying, “Write this: Blessed are the dead who die in the Lord from now on.” “Blessed indeed,” says the Spirit, “that they may rest from their labors, for their deeds follow them!”The Harvest of the EarthThen I looked, and behold, a white cloud, and seated on the cloud one like a son of man, with a golden crown on his head, and a sharp sickle in his hand.And another angel came out of the temple, calling with a loud voice to him who sat on the cloud, “Put in your sickle, and reap, for the hour to reap has come, for the harvest of the earth is fully ripe.”So he who sat on the cloud swung his sickle across the earth, and the earth was reaped.Then another angel came out of the temple in heaven, and he too had a sharp sickle.And another angel came out from the altar, the angel who has authority over the fire, and he called with a loud voice to the one who had the sharp sickle, “Put in your sickle and gather the clusters from the vine of the earth, for its grapes are ripe.”So the angel swung his sickle across the earth and gathered the grape harvest of the earth and threw it into the great winepress of the wrath of God.And the winepress was trodden outside the city, and blood flowed from the winepress, as high as a horse’s bridle, for 1,600 stadia.

JiYouMCC/0005/0005-r.jpg

786 KB
Loading

JiYouMCC/0005/0005.jpg

760 KB
Loading

JiYouMCC/0005/0005.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
# 第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
3+
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
4+
from PIL import Image
5+
6+
7+
def change_image_size(image_path='0005.jpg', size=(1136, 640)):
8+
im = Image.open(image_path)
9+
size = (size[1], size[0]) if im.size[1] > im.size[0] else size
10+
im.thumbnail(size, Image.ANTIALIAS)
11+
im.save('result-' + image_path)
12+
13+
change_image_size('0005-r.jpg')
14+
change_image_size('0005.jpg')

0 commit comments

Comments
 (0)