From def5f211cfdf37cb46bf38f074a84a3c3542ba59 Mon Sep 17 00:00:00 2001 From: Zdx <919824467@qq.com> Date: Sat, 5 Aug 2017 11:18:24 +0800 Subject: [PATCH 1/3] Create doubi --- doubi | 1 + 1 file changed, 1 insertion(+) create mode 100644 doubi diff --git a/doubi b/doubi new file mode 100644 index 00000000..7cf8a0aa --- /dev/null +++ b/doubi @@ -0,0 +1 @@ +https://github.com/doubiiot/show-me-the-code From 2e0560c811cbf070e4b7c5abbbb3f25063eb52c4 Mon Sep 17 00:00:00 2001 From: Zdx <919824467@qq.com> Date: Sat, 5 Aug 2017 23:41:15 +0800 Subject: [PATCH 2/3] Delete doubi --- doubi | 1 - 1 file changed, 1 deletion(-) delete mode 100644 doubi diff --git a/doubi b/doubi deleted file mode 100644 index 7cf8a0aa..00000000 --- a/doubi +++ /dev/null @@ -1 +0,0 @@ -https://github.com/doubiiot/show-me-the-code From 6ef2b2ed775e98c18f547c345a4937d145612bb0 Mon Sep 17 00:00:00 2001 From: doubi <919824467@qq.com> Date: Sun, 6 Aug 2017 00:16:30 +0800 Subject: [PATCH 3/3] 0000-0010 --- doubi_sdust/0000.py | 20 ++++++++++++++++++++ doubi_sdust/0001.py | 35 +++++++++++++++++++++++++++++++++++ doubi_sdust/0002.py | 1 + doubi_sdust/0003.py | 0 doubi_sdust/0004.py | 14 ++++++++++++++ doubi_sdust/0005.py | 15 +++++++++++++++ doubi_sdust/0006.py | 24 ++++++++++++++++++++++++ doubi_sdust/0007.py | 39 +++++++++++++++++++++++++++++++++++++++ doubi_sdust/0008.py | 19 +++++++++++++++++++ doubi_sdust/0009.py | 1 + doubi_sdust/0010.py | 37 +++++++++++++++++++++++++++++++++++++ 11 files changed, 205 insertions(+) create mode 100644 doubi_sdust/0000.py create mode 100644 doubi_sdust/0001.py create mode 100644 doubi_sdust/0002.py create mode 100644 doubi_sdust/0003.py create mode 100644 doubi_sdust/0004.py create mode 100644 doubi_sdust/0005.py create mode 100644 doubi_sdust/0006.py create mode 100644 doubi_sdust/0007.py create mode 100644 doubi_sdust/0008.py create mode 100644 doubi_sdust/0009.py create mode 100644 doubi_sdust/0010.py diff --git a/doubi_sdust/0000.py b/doubi_sdust/0000.py new file mode 100644 index 00000000..dcc25f32 --- /dev/null +++ b/doubi_sdust/0000.py @@ -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) \ No newline at end of file diff --git a/doubi_sdust/0001.py b/doubi_sdust/0001.py new file mode 100644 index 00000000..7f36876d --- /dev/null +++ b/doubi_sdust/0001.py @@ -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)) \ No newline at end of file diff --git a/doubi_sdust/0002.py b/doubi_sdust/0002.py new file mode 100644 index 00000000..28b8b1b8 --- /dev/null +++ b/doubi_sdust/0002.py @@ -0,0 +1 @@ +#refer to 0001.py \ No newline at end of file diff --git a/doubi_sdust/0003.py b/doubi_sdust/0003.py new file mode 100644 index 00000000..e69de29b diff --git a/doubi_sdust/0004.py b/doubi_sdust/0004.py new file mode 100644 index 00000000..eb433587 --- /dev/null +++ b/doubi_sdust/0004.py @@ -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') \ No newline at end of file diff --git a/doubi_sdust/0005.py b/doubi_sdust/0005.py new file mode 100644 index 00000000..00d842aa --- /dev/null +++ b/doubi_sdust/0005.py @@ -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) \ No newline at end of file diff --git a/doubi_sdust/0006.py b/doubi_sdust/0006.py new file mode 100644 index 00000000..9f573b36 --- /dev/null +++ b/doubi_sdust/0006.py @@ -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') \ No newline at end of file diff --git a/doubi_sdust/0007.py b/doubi_sdust/0007.py new file mode 100644 index 00000000..50a8f56b --- /dev/null +++ b/doubi_sdust/0007.py @@ -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') \ No newline at end of file diff --git a/doubi_sdust/0008.py b/doubi_sdust/0008.py new file mode 100644 index 00000000..0821a8e0 --- /dev/null +++ b/doubi_sdust/0008.py @@ -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')) \ No newline at end of file diff --git a/doubi_sdust/0009.py b/doubi_sdust/0009.py new file mode 100644 index 00000000..be2e68ee --- /dev/null +++ b/doubi_sdust/0009.py @@ -0,0 +1 @@ +#refer to 0008.py \ No newline at end of file diff --git a/doubi_sdust/0010.py b/doubi_sdust/0010.py new file mode 100644 index 00000000..e7e8dea8 --- /dev/null +++ b/doubi_sdust/0010.py @@ -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') \ No newline at end of file