Skip to content

Doubiiot #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2017
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
20 changes: 20 additions & 0 deletions doubi_sdust/0000.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'''

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

'''
from PIL import Image, ImageDraw, ImageFont
#PIL https://pillow.readthedocs.org/
def add_num(img):
draw = ImageDraw.Draw(img)
#加载TrueType或OpenType字体文件,并创建一个字体对象。
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=20)
fillcolor = "#ff0000"
width, height = img.size
draw.text((width-40, 0), '2', font=myfont, fill=fillcolor)
img.save('result.jpg','jpeg')
return 0

image = Image.open('image.jpg')
print(image.format,image.size,image.mode)
add_num(image)
35 changes: 35 additions & 0 deletions doubi_sdust/0001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
'''
import random
import pymysql
def creat_num(num,long):
str = 'qwertyuiopasdfghjklzxcvbnm1234567890'
b = []
for i in range(num):
a = ''
for j in range(long):
a += random.choice(str)
b.append(a)
return b

def InsertIntoMysql(codelist):
# 打开数据库连接
db = pymysql.connect(host='127.0.0.1',user='root',passwd='919824467',db='mysql')
# 使用 cursor() 方法创建一个游标对象 cursor
cur = db.cursor()
#数据库语句
cur.execute('CREATE DATABASE IF NOT EXISTS code')
cur.execute('USE code')
cur.execute('''CREATE TABLE IF NOT EXISTS num(
id INT NOT NULL AUTO_INCREMENT,
code VARCHAR(32) NOT NULL,
PRIMARY KEY(id) )''')
for num in codelist:
cur.execute('INSERT INTO num(code) VALUES(%s)',(num))
cur.connection.commit()
db.close()

InsertIntoMysql(creat_num(200,10))
1 change: 1 addition & 0 deletions doubi_sdust/0002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#refer to 0001.py
Empty file added doubi_sdust/0003.py
Empty file.
14 changes: 14 additions & 0 deletions doubi_sdust/0004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。
'''

# encoding: utf-8
import collections
import os

with open('test.txt','r') as fp:
str1=fp.read().split(' ')
b = collections.Counter(str1)
with open('result.txt','w') as result_file:
for key,value in b.items():
result_file.write(key+':'+str(value)+'\n')
15 changes: 15 additions & 0 deletions doubi_sdust/0005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
第 0005 题: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
'''
from PIL import Image
import os.path

def Size(dirPath, size_x, size_y):
f_list = os.listdir(dirPath)
for i in f_list:
if os.path.splitext(i)[1] == '.jpg':
img = Image.open(i)
img.thumbnail((size_x,size_y))
img.save(i)
print(i)
Size('D:\PyCharm 2017.1.3\projects', 1136, 640)
24 changes: 24 additions & 0 deletions doubi_sdust/0006.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
第 0006 题: 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
'''
# encoding: utf-8
import collections
import os.path
def judgeit(words):
for i in range(6):
if len(words[i]) > 2 and words[i] != 'the' and words[i] != 'her' and words[i] != 'his' and words[i] != 'and' and words[i] != 'she':
return words[i]
return words[7]

def mainKeywords(dirPath):
f_list = os.listdir(dirPath)
for i in f_list:
if os.path.splitext(i)[1] == '.txt':
print('the keywords of' + i + ' is:' )
with open(i, 'r') as fp:
str1 = fp.read().split(' ')
b = collections.Counter(str1)
keywords = sorted(b, key=lambda x: b[x],reverse = True)
print(judgeit(keywords))

mainKeywords('D:\PyCharm 2017.1.3\projects')
39 changes: 39 additions & 0 deletions doubi_sdust/0007.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'''
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
'''
import os.path
import re
def mainKeywords(dirPath):
blank, comments, codelines, totalines, count, temp = 0, 0, 0, 0, 0, 0
f_list = os.listdir(dirPath)
for i in f_list:
if os.path.splitext(i)[1] == '.py':
print(i)
with open(i, 'r', encoding='utf-8') as fp:
while True:
line = fp.readline()
totalines += 1
if not line:
break
elif line.strip().startswith('#'):
comments += 1
elif line.strip().startswith("'''") or line.strip().startswith('"""'):
comments += 1
if line.count('"""') == 1 or line.count("'''") == 1:
while True:
line = fp.readline()
totalines += 1
comments += 1
if ("'''" in line) or ('"""' in line):
break
elif line.strip():
codelines += 1
else:
blank += 1
print('the nuber of totalines is : ' + str(totalines-1))
print('the nuber of comments is : ' + str(comments))
print('the nuber of codelines is : ' + str(codelines))
print('the nuber of blanklines is : ' + str(blank))
blank, comments, codelines, totalines = 0, 0, 0, 0

mainKeywords('D:\PyCharm 2017.1.3\projects')
19 changes: 19 additions & 0 deletions doubi_sdust/0008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
第 0008 题: 一个HTML文件,找出里面的正文。

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

# coding=utf-8
from bs4 import BeautifulSoup
def sechBodyUrl(path):
with open(path,encoding='utf-8') as fp:
text = BeautifulSoup(fp, 'lxml')
urls = text.findAll('a')
for u in urls:
print(u['href'])
content = text.get_text().strip('\n')
return content

sechBodyUrl('0007.html')
#print(searchBody('0007.html'))
1 change: 1 addition & 0 deletions doubi_sdust/0009.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#refer to 0008.py
37 changes: 37 additions & 0 deletions doubi_sdust/0010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'''
第 0010 题: 使用 Python 生成类似于下图中的字母验证码图片

参考廖雪峰代码:liaoxuefeng.com/…/00140767171357714f87a053a824ffd811d98a83b58ec13000
'''
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random

# 随机字母:
def rndChar():
return chr(random.randint(65, 90))
# 随机颜色1:
def rndColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机颜色2:
def rndColor2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

# 240 x 60:
width = 240
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
font = ImageFont.truetype('C:/windows/fonts/Arial.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor())
# 输出文字:
for t in range(4):
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg');
image.show('code.jpg')