Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions renzongxian/0000/0000.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-11-30
# Python 3.4

"""

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

"""

from PIL import Image, ImageDraw, ImageFont
import sys


def add_num_to_img(file_path):
im = Image.open(file_path)
im_draw = ImageDraw.Draw(im)
font = ImageFont.truetype("arial.ttf", int(im.size[0]/5))
im_draw.text((int(im.size[0]-im.size[0]/10), 5), "4", (256, 0, 0), font=font)
del im_draw
im.save(file_path)

if __name__ == "__main__":
if len(sys.argv) <= 1:
print("Need at least 1 parameter. Try to execute 'python 0000.py $image_path'")
else:
for infile in sys.argv[1:]:
try:
add_num_to_img(infile)
print("Success!")
except IOError:
print("Can't open image!")
pass
Binary file added renzongxian/0000/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions renzongxian/0001/0001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-11-30
# Python 3.4

"""

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

"""

import uuid


def generate_key():
key_list = []
for i in range(200):
uuid_key = uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.uuid1()))
key_list.append(str(uuid_key).replace('-', ''))
return key_list

if __name__ == '__main__':
print(generate_key())
56 changes: 56 additions & 0 deletions renzongxian/0002/0002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding:utf8 -*-
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-06
# Python 2.7, MySQL-python does not currently support Python 3

"""

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

"""

import uuid
import MySQLdb


def generate_key():
key_list = []
for i in range(200):
uuid_key = uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.uuid1()))
key_list.append(str(uuid_key).replace('-', ''))
return key_list


def write_to_mysql(key_list):
# Connect to database
db = MySQLdb.connect("localhost", "test", "test1234", "testDB")

# Use function cursor() to open the cursor operation
cursor = db.cursor()

# If the table exists, delete it
cursor.execute("drop table if exists ukey")

# Create table
sql = """create table ukey (
key_value char(40) not null
)"""
cursor.execute(sql)

# Insert data
try:
for i in range(200):
cursor.execute('insert into ukey values("%s")' % (key_list[i]))
# Commit to database
db.commit()
except:
# Rollback when errors occur
db.rollback()

# Close database
db.close()


if __name__ == '__main__':
write_to_mysql(generate_key())
32 changes: 32 additions & 0 deletions renzongxian/0003/0003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding:utf8 -*-
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-06
# Python 2.7

"""

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

"""

import uuid
import redis


def generate_key():
key_list = []
for i in range(200):
uuid_key = uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.uuid1()))
key_list.append(str(uuid_key).replace('-', ''))
return key_list


def write_to_redis(key_list):
re = redis.StrictRedis(host='localhost', port=6379, db=0)
for i in range(200):
re.sadd('ukey', key_list[i])


if __name__ == '__main__':
write_to_redis(generate_key())
35 changes: 35 additions & 0 deletions renzongxian/0004/0004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-07
# Python 3.4

"""

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

"""

import sys


def word_count(file_path):
file_object = open(file_path, 'r')

word_num = 0
for line in file_object:
line_list = line.split()
word_num += len(line_list)

file_object.close()
return word_num

if __name__ == "__main__":
if len(sys.argv) <= 1:
print("Need at least 1 parameter. Try to execute 'python 0004.py $image_path'")
else:
for infile in sys.argv[1:]:
try:
print("The total number of words is ", word_count(infile))
except IOError:
print("Can't open file!")
pass
27 changes: 27 additions & 0 deletions renzongxian/0004/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Why are my contributions not showing up on my profile?
Your profile contributions graph is a record of contributions you've made to GitHub repositories. Contributions are only counted if they meet certain criteria. In some cases, we may need to rebuild your graph in order for contributions to appear.

Contributions that are counted

Issues and pull requests

Issues and pull requests will appear on your contributions graph if they meet both of these conditions:

They were opened within the past year.
They were opened in a standalone repository, not a fork.
Commits

Commits will appear on your contributions graph if they meet all of the following conditions:

The commits were made within the past year.
The email address used for the commits is associated with your GitHub account.
The commits were made in a standalone repository, not a fork.
The commits were made:
In the repository's default branch (usually master)
In the gh-pages branch (for repositories with Project Pages sites)
In addition, at least one of the following must be true:

You are a collaborator on the repository or are a member of the organization that owns the repository.
You have forked the repository.
You have opened a pull request or issue in the repository.
You have starred the repository.