diff --git a/tonnyone/0000.py b/tonnyone/0000.py new file mode 100644 index 00000000..60258268 --- /dev/null +++ b/tonnyone/0000.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +#coding=utf-8 +''' +将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果 +''' + +from PIL import Image,ImageDraw,ImageFont +from PIL import PSDraw +import sys + +im = Image.open("D://6793701.jpg") +print im.format +print im.mode +print im.size + +draw = ImageDraw.Draw(im) +# make a blank image for the text, initialized to transparent text color +# txt = Image.new('RGBA', im.size, (255,255,255,0)) +# draw2 = ImageDraw.Draw(txt) +fnt = ImageFont.truetype('arial.ttf', 40) +# draw text, half opacity +draw.text((im.size[0]-40*3,0), "Hello", font=fnt, fill=(0,0,0,0)) +# draw text, full opacity +#draw.text((0,60), "World", font=fnt, fill=(0,100,0,0)) + +del draw +# write to stdout +im.save("D:/new.png", "PNG") + + + diff --git a/tonnyone/0001.py b/tonnyone/0001.py new file mode 100644 index 00000000..9c3cf143 --- /dev/null +++ b/tonnyone/0001.py @@ -0,0 +1,11 @@ +#coding=utf-8 +__author__ = 'tonnytwo' + +'''第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?''' + +import random +for a in xrange(0,200): + str1 = "" + for b in xrange(0,4): + str1 = str1 + str(random.randint(0,9)) + print str1 diff --git a/tonnyone/0002.py b/tonnyone/0002.py new file mode 100644 index 00000000..a99d0c54 --- /dev/null +++ b/tonnyone/0002.py @@ -0,0 +1,34 @@ + +#!/usr/bin/python +#coding=utf-8 +__author__ = 'tonnytwo' +import MySQLdb +import random + +db = MySQLdb.connect(host="localhost", # your host, usually localhost + user="root", # your username + passwd="123456", # your password + db="pythontest") # name of the data b + +cur = db.cursor() +for a in xrange(0,200): + str1 = "" + for b in xrange(0,4): + str1 = str1 + str(random.randint(0,9)) + print str1 + sql = "insert into test VALUES ('%s','%s')" % (a,str1) + print sql + cur.execute(sql) +db.commit() + +try: + cur.execute(""" + select * from test + """) + result = cur.fetchall() + print result +finally: + cur.close() + +db.close() + diff --git a/tonnyone/0003.py b/tonnyone/0003.py new file mode 100644 index 00000000..56aa2f26 --- /dev/null +++ b/tonnyone/0003.py @@ -0,0 +1,16 @@ + +#coding=utf-8 +__author__ = 'tonnytwo' + +import redis +import random + +r = redis.StrictRedis(host='localhost', port=6379, db=0) +for a in xrange(0,200): + str1 = "" + for b in xrange(0,4): + str1 = str1 + str(random.randint(0,9)) + print str1 + r.set(a, str1) + + diff --git a/tonnyone/0004.py b/tonnyone/0004.py new file mode 100644 index 00000000..6c2d1132 --- /dev/null +++ b/tonnyone/0004.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +#coding=utf-8 +'''任一个英文的纯文本文件,统计其中的单词出现的个数。''' +__author__ = 'tonnytwo' + +filepath=r"E:/eversec/A_document/python/mit/笔记/9test/filescan.py" +upath = unicode(filepath,'utf8') +f = open(upath,'r') +a = (word for line in f for word in line.split()) +dict={} +for b in a: + if b not in dict: + dict[b] = 1 + else: + dict[b]=dict[b] + 1 + +for k in dict: + print k,dict[k] \ No newline at end of file diff --git a/tonnyone/0005.py b/tonnyone/0005.py new file mode 100644 index 00000000..c642f881 --- /dev/null +++ b/tonnyone/0005.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +#coding=utf-8 + +"""你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。""" +__author__ = 'tonnytwo' + +import os +from PIL import Image,ImageDraw,ImageFont + +def scanPic(path): + + if(os.path.isdir(path)): + files = os.listdir(path) + for f in files: + if(os.path.isdir(path+os.sep+f)): + scanPic(path+os.sep+f) + else: + try: + im = Image.open(path+os.sep+f) + im.thumbnail((200,200)) + im.save("D:/pic/"+f+"refac.jpeg", "JPEG"); + except IOError: + pass + +scanPic("D:/pic"); diff --git a/tonnyone/0006.py b/tonnyone/0006.py new file mode 100644 index 00000000..7171dc82 --- /dev/null +++ b/tonnyone/0006.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +#coding=utf-8 + +"""你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。""" +__author__ = 'tonnytwo' + +filist=["to","today","is","my","you",'he',"she","good","nice"] +dict={} +def importWord(path): + f = open(path,'r') + words = (word for line in f for word in line.split()) + for w in words: + + w = w.lower() + if w not in filist: + if w not in dict: + dict[w]=1 + else: + dict[w]=dict[w]+1 + + count=0 + keyword = "" + for w in dict: + if(dict[w]>count): + count = dict[w] + keyword = w + print keyword + #print dict[keyword] + +importWord("D:/Python32/MySQL-python-wininst.log") diff --git a/tonnyone/0007.py b/tonnyone/0007.py new file mode 100644 index 00000000..c15e5d86 --- /dev/null +++ b/tonnyone/0007.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +#coding=utf-8 + +'''有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。''' +__author__ = 'tonnytwo' + +import os +import sys + +def scanCode(path,linenum=0,codenum=0,emptynum=0): + + print path + uipath='' + if isinstance(path, str): + uipath = unicode(path, "utf8") + uipath = unicode(path, "utf8") + elif isinstance(path,unicode): + uipath = path + else: + return None + + if(os.path.isdir(uipath)): + print "123" + files = os.listdir(uipath) + for f in files: + if(os.path.isdir(uipath+os.sep+f)): + tupl2 = scanCode(uipath+os.sep+f,linenum,codenum,emptynum) + linenum = linenum +tupl2[0] + codenum = codenum +tupl2[1] + emptynum = emptynum+tupl2[2] + else: + if f.endswith(".py"): + tupl1 = getCode(uipath+os.sep+f,linenum,codenum,emptynum) + linenum = linenum+tupl1[0] + codenum = codenum+tupl1[1] + emptynum = emptynum+tupl1[2] + else: + continue + return linenum,codenum,emptynum + else: + return linenum,codenum,emptynum + +def getCode(path,linenum=0,codenum=0,emptynum=0): + + sign=[u'/',u'#',u"\'",u"\"",'/','#','/','\'','\"'] + f = open(path,"r") + for line in f: + linenum = linenum+1 + if len(line.strip())>0: + if line.strip()[0] in sign: + codenum = codenum+1 + else: + emptynum = emptynum+1 + + return linenum,codenum,emptynum + +print("你好是不是我说的") +print(sys.getdefaultencoding()) +print scanCode(r"E:\eversec\A_document\python\mit\笔记"); \ No newline at end of file diff --git a/tonnyone/0008.py b/tonnyone/0008.py new file mode 100644 index 00000000..363b84db --- /dev/null +++ b/tonnyone/0008.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +#coding=utf-8 +__author__ = 'tonnytwo' +"""一个HTML文件,找出里面的正文""" + +from bs4 import BeautifulSoup +import requests + +def getCon(): + + #r = requests.get('https://api.github.com', auth=('user', 'pass')) + r = requests.get('http://ent.ifeng.com/a/20150626/42440289_0.shtml') + r.status_code + string = '' + if r.status_code==200: + string = r.text + #print string + soup = BeautifulSoup(''.join(string),from_encoding="utf-8") + content = soup.findAll(['h2','h3','h4','h5', 'p']) + for con in content: + print con.string + #content = soup.body + # pTag = soup.p + #content = pTag.contents + #print str(content) + # print soup.prettify() +getCon(); \ No newline at end of file diff --git a/tonnyone/0009.py b/tonnyone/0009.py new file mode 100644 index 00000000..d2ca21bc --- /dev/null +++ b/tonnyone/0009.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +#coding=utf-8 +__author__ = 'tonnytwo' +"""一个HTML文件,找出里面的链接。""" +from bs4 import BeautifulSoup +import requests +import re + +def getCon(): + + #r = requests.get('https://api.github.com', auth=('user', 'pass')) + r = requests.get('http://www.baidu.com/s?wd=python%20re&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=8&rsv_sug1=6') + r.status_code + string = '' + if r.status_code==200: + string = r.text + print string + for a in re.finditer(r"http://\w*[\.\w+]+[/\w+]*\?[\w+\=.+&]*[\w+=.+]",string): + #for a in re.finditer(r"http://\w*[\.\w+]+[/\w+]*\?.*",string): + print a.group() + # for a in re.finditer(r"http://.+",string): + # print a.group() +getCon(); \ No newline at end of file diff --git a/tonnyone/0010.py b/tonnyone/0010.py new file mode 100644 index 00000000..9af46bd0 --- /dev/null +++ b/tonnyone/0010.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python +#coding=utf-8 + +"""使用 Python 生成类似于下图中的字母验证码图片""" +__author__ = 'tonnytwo' + + + diff --git a/tonnyone/0011.py b/tonnyone/0011.py new file mode 100644 index 00000000..88fc0aef --- /dev/null +++ b/tonnyone/0011.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +#coding=utf-8 +__author__ = 'tonnytwo' + +from PIL import Image,ImageDraw,ImageFont +import random +import string + +def colorRandom(): + r = random.randint(0,255) + g = random.randint(0,255) + b = random.randint(0,255) + a = 200 + return (r,g,b,a) + +def letterRandom(): + return random.choice(string.letters) + +wigth = 200 +high = 50 +txt = Image.new('RGBA', (wigth,high), (255,255,255,0)) +d = ImageDraw.Draw(txt) + +fnt = ImageFont.truetype('arial.ttf', 40) + +for w in range(1,wigth): + for h in range(1,high): + d.point([(w,h),(w,h)],fill = colorRandom()) + +d.text((wigth/8,high/8), letterRandom(),font=fnt,fill =colorRandom()) +d.text((wigth/8*2,high/8), letterRandom(),font=fnt,fill =colorRandom()) +d.text((wigth/8*4,high/8), letterRandom(),font=fnt,fill =colorRandom()) +d.text((wigth/8*6,high/8), letterRandom(),font=fnt,fill =colorRandom()) +txt.show() diff --git a/tonnyone/0012.py b/tonnyone/0012.py new file mode 100644 index 00000000..cca81195 --- /dev/null +++ b/tonnyone/0012.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +#coding=utf-8 +__author__ = 'tonnytwo' + +def isSensitive(str,filterlist): + if str in filterlist: + print "FreeDom" + else: + print "Human Rights" + +def Test(): + f = open("./filtered_words.txt","r") + filterlist = [] + for a in f: + filterlist.append(a.strip().decode("utf-8")) + + print filterlist + while True: + input = raw_input("please input a sentense:") + print input.decode("utf-8") + isSensitive(input.decode("utf-8"),filterlist) + +Test() \ No newline at end of file diff --git a/tonnyone/0013.py b/tonnyone/0013.py new file mode 100644 index 00000000..d23b50d0 --- /dev/null +++ b/tonnyone/0013.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +#coding=utf-8 +__author__ = 'tonnytwo' + +def isSensitive(str,filterlist): + if str in filterlist: + return True + else: + return False + +def Test(): + f = open("./filtered_words.txt","r") + filterlist = [] + for a in f: + filterlist.append(a.strip().decode("utf-8")) + + print filterlist + while True: + input = raw_input("please input a sentense:") + for keywork in filterlist: + unicodeinput = input.decode("utf-8") + if keywork in unicodeinput: + print unicodeinput.replace(keywork,len(keywork)* "*") + #print unicodeinput + +Test() \ No newline at end of file diff --git a/tonnyone/0014.py b/tonnyone/0014.py new file mode 100644 index 00000000..91b24718 --- /dev/null +++ b/tonnyone/0014.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +#coding=utf-8 + +"""用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 """ + +import requests +from PIL import Image,ImageDraw,ImageFont +from StringIO import StringIO +from bs4 import BeautifulSoup + +__author__ = 'tonnytwo' + +r = requests.get('http://tieba.baidu.com/p/2166231880') +#r = requests.get('http://ent.ifeng.com/a/20150626/42440289_0.shtml') + +string = '' +if r.status_code==200: + string = r.text + soup = BeautifulSoup(''.join(string),from_encoding="utf-8") + content = soup.findAll('img',src=True) + a=0 + for img in content: + + a=a+1 + src = img["src"] + print src + try: + tempr = requests.get(src) + if tempr.status_code==200: + i = Image.open(StringIO(tempr.content)) + i.save("E://temp//"+str(a) + ".jpg", "JPEG") + except IOError: + print "error" + pass + + + diff --git a/tonnyone/0015.py b/tonnyone/0015.py new file mode 100644 index 00000000..de39ac85 --- /dev/null +++ b/tonnyone/0015.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +#coding=utf-8 + +"""第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: + +{ + "1":["张三",150,120,100], + "2":["李四",90,99,95], + "3":["王五",60,66,68] +} +请将上述内容写到 student.xls 文件中,如下图所示:""" + +__author__ = 'tonnytwo' +import json +import xlwt +import xlrd + +def Test(): + f = open("./students.txt","r") + text = f.read() + js = json.loads(text) + print type(js) + print js["1"] + + style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', + num_format_str='#,##0.00') + + wb = xlwt.Workbook() + ws = wb.add_sheet('student') + ww = 0 + for key in js: + ws.write(ww,0,key) + hh = 1 + for key2 in js[key]: + ws.write(ww,hh,key2) + hh = hh + 1 + ww = ww+1 + #ws.write(2, 1, 1) + #ws.write(2, 2, xlwt.Formula("A3+B3")) + + wb.save('example.xls') +Test() + + + + + + + + + diff --git a/tonnyone/example.xls b/tonnyone/example.xls new file mode 100644 index 00000000..7657e9d2 Binary files /dev/null and b/tonnyone/example.xls differ diff --git a/tonnyone/filtered_words.txt b/tonnyone/filtered_words.txt new file mode 100644 index 00000000..69373b64 --- /dev/null +++ b/tonnyone/filtered_words.txt @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex +jiangge \ No newline at end of file diff --git a/tonnyone/students.txt b/tonnyone/students.txt new file mode 100644 index 00000000..ea9b1593 --- /dev/null +++ b/tonnyone/students.txt @@ -0,0 +1,5 @@ +{ + "1":["张三",150,120,100], + "2":["李四",90,99,95], + "3":["王五",60,66,68] +} \ No newline at end of file