diff --git a/Mr.Lin/0007/0007.py b/Mr.Lin/0007/0007.py index 4e30cbee..261abc71 100644 --- a/Mr.Lin/0007/0007.py +++ b/Mr.Lin/0007/0007.py @@ -3,14 +3,14 @@ # @Author: 30987 # @Date: 2015-01-14 11:07:02 # @Last Modified by: 30987 -# @Last Modified time: 2015-01-14 11:47:11 +# @Last Modified time: 2015-01-14 17:24:03 #第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。 # # """ 此处统计的注释行# 并不会将#!/usr/bin/env python # -*- coding: utf-8 -*- 统计在内,同时if __main__里面的注释也不会进行统计 - +""" def code_count(code_file): total_lines = 0 diff --git a/Mr.Lin/0010/0010.py b/Mr.Lin/0010/0010.py new file mode 100644 index 00000000..c1222283 --- /dev/null +++ b/Mr.Lin/0010/0010.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: 30987 +# @Date: 2015-01-14 18:20:01 +# @Last Modified by: 30987 +# @Last Modified time: 2015-01-15 12:20:59 + +#第 0010 题:使用 Python 生成字母验证码图片 + +import random +from PIL import Image,ImageDraw,ImageFont,ImageFilter + +_letter_cases = 'abcdefghjkmnpqrstuvwxy' #小写字母,忽略了容易误解的i,l,o,z +_upper_cases = _letter_cases.upper() #转换为大写字母 +_number = ''.join(map(str,range(3,10))) #生成[3,4,5,6,7,8,9],然后将他们连接为3456789 +init_chars=''.join((_letter_cases,_upper_cases,_number)) + + +def create_validate_code(size=(120, 30), + chars=init_chars, + img_type="JPEG", + mode="RGB", + bg_color=(255, 255, 255), + fg_color=(0, 0, 255), + font_size=18, + font_type="ahronbd.ttf", + length=4, + draw_lines=True, + n_line=(1, 2), + draw_points=True, + point_chance = 2): + """ + @todo:生成验证码图片 + @param size: 生成验证码图片的尺寸,默认(240,60) + @param chars: 图片中的字符集 + @param img_type:图片保存的格式,默认JPEG,支持GIF,JPEG,TIFF,PNG + @param mode: 图片颜色的模式,默认RGB + @param bg_color:图片背景颜色,默认白色 + @param fg_color:前景颜色,验证码字符的颜色,默认蓝色#0000FF + @param font_size:验证码字体字体大小,默认18号 + @param font_type:验证码字体,默认ae_AlArabiya.ttf + @param length: 验证码字符个数 + @param draw_lines:是否有干扰线 + @param n_line: 干扰线条数,元组,只有draw_lines为True时才有效 + @param draw_points:是否画干扰点 + @param point_chance:干扰点出现的概率,范围为[0,100] + """ + width,height = size + img = Image.new(mode,size,bg_color) #创建图形 + draw = ImageDraw.Draw(img) #创建画笔 + + def get_chars(): + #生成验证码字符 + return random.sample(chars,length) #random.sample(),从chars中去length长度的内容返回为列表['4', 'g', 'U', 'n'] + + def creat_lines(): + #创建干扰线 + line_num = random.randint(*n_line) #干扰线条数,随机生成,randon.randint(a,b) 用于生成一个指定范围内的整数,a为下限,b为上限,生成的随机整数a<=n<=b;若a=b,则n=a;若a>b,报错 + + for i in range(line_num): + #起始点 + begin = (random.randint(0,size[0]),random.randint(0,size[1])) + #结束点 + end = (random.randint(0,size[0]),random.randint(0,size[1])) + draw.line([begin,end],fill=(0,0,0)) + + def creat_points(): + #绘制干扰点 + chance = min(100,max(0,int(point_chance))) #大小限制在[0,100] + + for w in xrange(width): + for h in xrange(height): + tmp = random.randint(0,100) + if tmp > 100-chance: + draw.point((w,h),fill=(0,0,0)) + + def creat_chars(): + #绘制验证码 + c_chars = get_chars() + strs = ' %s' %''.join(c_chars) #每个字符前以空格隔开 + + font = ImageFont.truetype(font_type,font_size) + font_width,font_height = font.getsize(strs) + + #draw.text((width-font_width)/3,(height-font_height)/3,strs,font=font,fill=fg_color) + draw.text(((width - font_width) / 3, (height - font_height) / 3),strs, font=font, fill=fg_color) + + return ''.join(c_chars) + + if draw_lines: + creat_lines() + if draw_points: + creat_points() + strs = creat_chars() + + #图形扭曲 + #param = [1-float(random.randint(1,2)/100),0,0,0,1 - float(random.randint(1, 10)) / 100,float(random.randint(1, 2)) / 5000.001,float(random.randint(1, 2)) / 500] + params = [1 - float(random.randint(1, 2)) / 100, + 0, + 0, + 0, + 1 - float(random.randint(1, 10)) / 100, + float(random.randint(1, 2)) / 500, + 0.001, + float(random.randint(1, 2)) / 500 + ] + img = img.transform(size, Image.PERSPECTIVE, params) #创建扭曲 + img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大) + img.save("validate.JPG", "JPEG") + + + +if __name__ == '__main__': + code_img = create_validate_code(point_chance = 5) diff --git a/Mr.Lin/0010/validate.JPG b/Mr.Lin/0010/validate.JPG new file mode 100644 index 00000000..a096770d Binary files /dev/null and b/Mr.Lin/0010/validate.JPG differ diff --git a/Mr.Lin/0010/validate.gif b/Mr.Lin/0010/validate.gif new file mode 100644 index 00000000..8fdb2d2f Binary files /dev/null and b/Mr.Lin/0010/validate.gif differ diff --git a/Mr.Lin/0011/0011.py b/Mr.Lin/0011/0011.py new file mode 100644 index 00000000..75032f05 --- /dev/null +++ b/Mr.Lin/0011/0011.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: 30987 +# @Date: 2015-01-15 12:22:00 +# @Last Modified by: 30987 +# @Last Modified time: 2015-01-15 12:37:53 + +#第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。 + +def fileter_words(): + file_obj = open('filtered_words.txt','r') + file = file_obj.read() + filt = file.split('\n') + file_obj.close + + while True: + flag = False + input_text = raw_input("please input:") + for x in filt: + if input_text.find(x) != -1 : + flag = True + if flag: + print "Freedom" + else: + print "Human Rights。" + + + +if __name__ == '__main__': + fileter_words() diff --git a/Mr.Lin/0011/filtered_words.txt b/Mr.Lin/0011/filtered_words.txt new file mode 100644 index 00000000..444eb7c6 --- /dev/null +++ b/Mr.Lin/0011/filtered_words.txt @@ -0,0 +1,11 @@ + +Ա +Ա +쵼 +ţ +ţ + + +love +sex +jiangge \ No newline at end of file diff --git a/Mr.Lin/0012/0012.py b/Mr.Lin/0012/0012.py new file mode 100644 index 00000000..49dcd538 --- /dev/null +++ b/Mr.Lin/0012/0012.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: 30987 +# @Date: 2015-01-15 18:36:29 +# @Last Modified by: 30987 +# @Last Modified time: 2015-01-16 10:09:01 +# +# 第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。 +# 发现读取文件的时候中文识别不了。。 +# + + +def filter_words(words): + #从文件中读取过滤单词名单 + file_object = open('filtered_words.txt','r') + filtered_words = [] + for line in file_object: + filtered_words.append(line.strip('\n')) + file_object.close() + #print filtered_words + #判断filtered_words是否在用户输入的words中 + for f_words in filtered_words: + #f_words.decode('gbk') + if f_words in words: + words = words.replace(f_words,'*' * len(f_words)) + + + print(words) + + +if __name__ == '__main__': + input_words = raw_input('Please input some words:') + input_words.decode('gbk') + print "input_words:",input_words + filter_words(input_words) diff --git a/Mr.Lin/0012/filtered_words.txt b/Mr.Lin/0012/filtered_words.txt new file mode 100644 index 00000000..503155b1 --- /dev/null +++ b/Mr.Lin/0012/filtered_words.txt @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex +jiangge \ No newline at end of file diff --git a/Mr.Lin/0013/0013.py b/Mr.Lin/0013/0013.py new file mode 100644 index 00000000..d5bad64a --- /dev/null +++ b/Mr.Lin/0013/0013.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Author: 30987 +# @Date: 2015-01-16 10:18:31 +# @Last Modified by: 30987 +# @Last Modified time: 2015-01-16 10:37:52 + +#第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) +#网址 http://tieba.baidu.com/p/2166231880 +# +#经分析,每张的图片都有类似的格式,其中只有src地址不一样。所有写正则即可 +# +# +#正则如下 +#r'