diff --git a/hanxlleon/0000/0000.py b/hanxlleon/0000/0000.py new file mode 100644 index 00000000..c141d4cd --- /dev/null +++ b/hanxlleon/0000/0000.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +#第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果 + +from PIL import Image, ImageDraw, ImageFont + +def addNum(filePath): + img = Image.open(filePath) + size = img.size + fontSize = size[1] / 4 + draw = ImageDraw.Draw(img) + + ttFont = ImageFont.truetype("arial.ttf", fontSize) + draw.text((size[0]-fontSize, 0), "8", (256,0,0), font=ttFont) + del draw + img.save('./result.jpg') + img.show() + + +if __name__ == '__main__': + addNum("image.jpg") diff --git a/hanxlleon/0000/image.jpg b/hanxlleon/0000/image.jpg new file mode 100644 index 00000000..15b41c78 Binary files /dev/null and b/hanxlleon/0000/image.jpg differ diff --git a/hanxlleon/0000/result.jpg b/hanxlleon/0000/result.jpg new file mode 100644 index 00000000..9619fa7a Binary files /dev/null and b/hanxlleon/0000/result.jpg differ diff --git a/hanxlleon/0001/0001.py b/hanxlleon/0001/0001.py new file mode 100644 index 00000000..3d796a37 --- /dev/null +++ b/hanxlleon/0001/0001.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +#第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? + +import random +import os + +def random_generater(count=12): + code = [] + # base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F] + base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'),ord('F'))] + for i in range(0, count): + code.append(random.choice(base)) + return ''.join(code) + +if __name__ == '__main__': + with open('result.txt', 'a') as f: + for i in range(200): + f.write(random_generater()+'\n') + + diff --git a/hanxlleon/0001/result.txt b/hanxlleon/0001/result.txt new file mode 100644 index 00000000..15bf13e2 --- /dev/null +++ b/hanxlleon/0001/result.txt @@ -0,0 +1,200 @@ +7399DCB2382B +0A8A5214A2E0 +01BC087A9DD5 +ECDA50DD0E49 +21D09DD14B98 +EB97B076C1D7 +9120C6178C06 +7A4E5A18AA0C +BC2BEDBB2655 +7D8BE10DDA85 +A8BC50532D53 +CE3950B6EEE8 +14CA4E87EE69 +403648DB30C0 +4E38D7C94CA2 +981A51DDEE27 +9D57D8059297 +0AEA30763009 +3DAEAE085340 +33D27C8E95DE +28981D8657E2 +0EAC51865896 +B28A117C53D8 +0A781CE2CA0A +69BB3548838A +565E61244AD7 +A248CC08860D +191A8A5C78DA +B856AB51A2B5 +1C7AC2DE659B +4AAC5A6B3BBC +7753E83C7317 +8C727B9A4976 +B8DEC1A20CC1 +985BA34B3CDE +891527CE1A67 +6A0711D80BC2 +9D67469DA843 +3059B959D5AA +561722395A5A +C53123D111BA +37A8722CE0E4 +CCB6191A91D2 +ECDB7885695E +65BBD2D91345 +5E38772C90D2 +E5644A33E9A5 +109E4373CD42 +EA700443BEB2 +A555291DCB7E +444170535CBC +B569A17B2B87 +31AC01129E44 +80442D92D54B +EBA279E0D083 +87D604DDBC7E +58A7EA0D905E +683D51D34630 +B85D19424C82 +5ACC4198E434 +44A941B0D7DC +3C28D252B6A0 +A462D4E99724 +64E31AE1989B +E3AA44A167D8 +D8DE20C3B6B1 +1251090C9103 +54E30DEEDE6D +7125761BB98A +063A0478D8CD +C3914ACC6019 +40BBC81599E9 +A4E33E13B871 +20154976C343 +84C146D64290 +CC45222DC5DE +2A52A519073E +B514789AD1E8 +970EB4091E9D +B5B75B91694E +3B77E0250510 +ACB6E4ECD673 +37E15712B2DB +D0BA25C058BD +6BDC1A5D0676 +8BAAB02E27B6 +E7577211A0A4 +ADC160B9C792 +9E913713EA70 +AE5BC31C411A +B46765315DC3 +04AE9A3E5605 +8E888BC84866 +9A65959C1657 +A717D83A14CE +69E56497B006 +24E326AE30CE +32AC291E710B +27D7A2BB2C63 +2CCBC1771254 +BC7EED40A18A +38C3723625B6 +94700B5BC750 +2287719CAA68 +2AD469A43BC9 +66E7A06E58C8 +50DD72382496 +27AC9659EB33 +A683B9112412 +60D0EC8BBB25 +AA454BDC1751 +BB07B6448965 +662385C60874 +DAA4A17A03DA +67700D6E7564 +46ADE1866A51 +E78D4C90C690 +647D716611AE +18CB91B6885D +6D8685C1D03D +3CC4AAEC0328 +E0CB52A4E6D0 +37B917695320 +2A0DA26BA900 +92004D245712 +8903AEDBDD44 +B8348BE9E798 +9DCDD0537657 +D6E389C5768B +C4CC761C596A +17831D717C0B +70C7AD2D803A +829DBCB1D3A7 +C6528ADD3351 +2E2A2948BB14 +59815AD51456 +E9A82824D87E +939A6A5AD0A3 +99188183C358 +3C1201201265 +C60B2E90C3A8 +98E67920EA80 +1BCB9C998CC5 +9938906C6A35 +684AEAC5E5C2 +82DA8B7700BE +5D62064D9483 +0724B3DE764C +4D1BEDCD27AE +0CA01C21308E +AD74BD1544EA +10AA90AD7D1C +C628DEDD5288 +E6ABBDE8BAE1 +2EA35D0DC68A +30841254DB83 +744DC682A6CE +ADE4254645B8 +328EA72990D6 +784411B05C8B +25D76529AD7B +BC045408E903 +85C304878DA3 +721902B501BC +292DC1E5B261 +803E4B493C45 +21B9C87CB0DC +BE14A4DAE603 +E72819030E1A +99727670A2C6 +886490DAC3A0 +D3C0CCAD5B33 +54A677CBEDED +889493ECCAB5 +11983863D69B +566C87DA99D2 +60612B26CB13 +03949A72451C +7A84A77855D6 +73E407AE91CE +441963CD9828 +635724E8E50D +1737C5A5CEE8 +D97913813EBA +46B228B2A700 +9914DE7E72EE +23B0D32D5D6E +2313276863B7 +2ABACE62B22B +6301EB620D41 +5E69AEA5644E +BEDBBE573715 +D174569EB055 +2EE2BE0199D9 +982676A509E6 +1E3076CEA985 +ACD945511A90 +96E7C7196918 +C8593AA03B1C +07CD9D3E1E9A diff --git a/hanxlleon/0002/0002.py b/hanxlleon/0002/0002.py new file mode 100644 index 00000000..1a0cd639 --- /dev/null +++ b/hanxlleon/0002/0002.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +#将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。 + +import MySQLdb + +def file_read(filename): + with open(filename, 'r') as f: + lines = f.readlines() + for i in range(200): + lines[i] = lines[i].strip() + return lines + +def mysql_write(): + lines = file_read('result.txt') + conn = MySQLdb.connect(host='localhost',user='root', passwd='1', db='test', port=3306, charset='utf8') + cursor = conn.cursor() + cursor.execute('create table promo_code(id int(3) primary key, number varchar(12))') + for i in range(200): + cursor.execute('insert into promo_code (id, number) values(%s, %s)', [i+1, lines[i]]) + conn.commit() + cursor.close() + conn.close() + print 'finish' + + +if __name__ == '__main__': + mysql_write() diff --git a/hanxlleon/0002/result.txt b/hanxlleon/0002/result.txt new file mode 100644 index 00000000..15bf13e2 --- /dev/null +++ b/hanxlleon/0002/result.txt @@ -0,0 +1,200 @@ +7399DCB2382B +0A8A5214A2E0 +01BC087A9DD5 +ECDA50DD0E49 +21D09DD14B98 +EB97B076C1D7 +9120C6178C06 +7A4E5A18AA0C +BC2BEDBB2655 +7D8BE10DDA85 +A8BC50532D53 +CE3950B6EEE8 +14CA4E87EE69 +403648DB30C0 +4E38D7C94CA2 +981A51DDEE27 +9D57D8059297 +0AEA30763009 +3DAEAE085340 +33D27C8E95DE +28981D8657E2 +0EAC51865896 +B28A117C53D8 +0A781CE2CA0A +69BB3548838A +565E61244AD7 +A248CC08860D +191A8A5C78DA +B856AB51A2B5 +1C7AC2DE659B +4AAC5A6B3BBC +7753E83C7317 +8C727B9A4976 +B8DEC1A20CC1 +985BA34B3CDE +891527CE1A67 +6A0711D80BC2 +9D67469DA843 +3059B959D5AA +561722395A5A +C53123D111BA +37A8722CE0E4 +CCB6191A91D2 +ECDB7885695E +65BBD2D91345 +5E38772C90D2 +E5644A33E9A5 +109E4373CD42 +EA700443BEB2 +A555291DCB7E +444170535CBC +B569A17B2B87 +31AC01129E44 +80442D92D54B +EBA279E0D083 +87D604DDBC7E +58A7EA0D905E +683D51D34630 +B85D19424C82 +5ACC4198E434 +44A941B0D7DC +3C28D252B6A0 +A462D4E99724 +64E31AE1989B +E3AA44A167D8 +D8DE20C3B6B1 +1251090C9103 +54E30DEEDE6D +7125761BB98A +063A0478D8CD +C3914ACC6019 +40BBC81599E9 +A4E33E13B871 +20154976C343 +84C146D64290 +CC45222DC5DE +2A52A519073E +B514789AD1E8 +970EB4091E9D +B5B75B91694E +3B77E0250510 +ACB6E4ECD673 +37E15712B2DB +D0BA25C058BD +6BDC1A5D0676 +8BAAB02E27B6 +E7577211A0A4 +ADC160B9C792 +9E913713EA70 +AE5BC31C411A +B46765315DC3 +04AE9A3E5605 +8E888BC84866 +9A65959C1657 +A717D83A14CE +69E56497B006 +24E326AE30CE +32AC291E710B +27D7A2BB2C63 +2CCBC1771254 +BC7EED40A18A +38C3723625B6 +94700B5BC750 +2287719CAA68 +2AD469A43BC9 +66E7A06E58C8 +50DD72382496 +27AC9659EB33 +A683B9112412 +60D0EC8BBB25 +AA454BDC1751 +BB07B6448965 +662385C60874 +DAA4A17A03DA +67700D6E7564 +46ADE1866A51 +E78D4C90C690 +647D716611AE +18CB91B6885D +6D8685C1D03D +3CC4AAEC0328 +E0CB52A4E6D0 +37B917695320 +2A0DA26BA900 +92004D245712 +8903AEDBDD44 +B8348BE9E798 +9DCDD0537657 +D6E389C5768B +C4CC761C596A +17831D717C0B +70C7AD2D803A +829DBCB1D3A7 +C6528ADD3351 +2E2A2948BB14 +59815AD51456 +E9A82824D87E +939A6A5AD0A3 +99188183C358 +3C1201201265 +C60B2E90C3A8 +98E67920EA80 +1BCB9C998CC5 +9938906C6A35 +684AEAC5E5C2 +82DA8B7700BE +5D62064D9483 +0724B3DE764C +4D1BEDCD27AE +0CA01C21308E +AD74BD1544EA +10AA90AD7D1C +C628DEDD5288 +E6ABBDE8BAE1 +2EA35D0DC68A +30841254DB83 +744DC682A6CE +ADE4254645B8 +328EA72990D6 +784411B05C8B +25D76529AD7B +BC045408E903 +85C304878DA3 +721902B501BC +292DC1E5B261 +803E4B493C45 +21B9C87CB0DC +BE14A4DAE603 +E72819030E1A +99727670A2C6 +886490DAC3A0 +D3C0CCAD5B33 +54A677CBEDED +889493ECCAB5 +11983863D69B +566C87DA99D2 +60612B26CB13 +03949A72451C +7A84A77855D6 +73E407AE91CE +441963CD9828 +635724E8E50D +1737C5A5CEE8 +D97913813EBA +46B228B2A700 +9914DE7E72EE +23B0D32D5D6E +2313276863B7 +2ABACE62B22B +6301EB620D41 +5E69AEA5644E +BEDBBE573715 +D174569EB055 +2EE2BE0199D9 +982676A509E6 +1E3076CEA985 +ACD945511A90 +96E7C7196918 +C8593AA03B1C +07CD9D3E1E9A diff --git a/hanxlleon/0003/0003.py b/hanxlleon/0003/0003.py new file mode 100644 index 00000000..73aa3ed0 --- /dev/null +++ b/hanxlleon/0003/0003.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +#第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。 + +import redis + + +def file_read(filename): + with open(filename, 'r') as f: + lines = f.readlines() + for n in range(200): + lines[n] = lines[n].strip() + return lines + +def redis_write(): + lines = file_read('result.txt') + r = redis.StrictRedis(host='localhost', port=6379, db=0) + for n in range(200): + key = str(n+1) + value = str(lines[n]) + r.set(key, value) + r.save() + print 'finish' + +if __name__ == '__main__': + redis_write() diff --git a/hanxlleon/0003/result.txt b/hanxlleon/0003/result.txt new file mode 100644 index 00000000..15bf13e2 --- /dev/null +++ b/hanxlleon/0003/result.txt @@ -0,0 +1,200 @@ +7399DCB2382B +0A8A5214A2E0 +01BC087A9DD5 +ECDA50DD0E49 +21D09DD14B98 +EB97B076C1D7 +9120C6178C06 +7A4E5A18AA0C +BC2BEDBB2655 +7D8BE10DDA85 +A8BC50532D53 +CE3950B6EEE8 +14CA4E87EE69 +403648DB30C0 +4E38D7C94CA2 +981A51DDEE27 +9D57D8059297 +0AEA30763009 +3DAEAE085340 +33D27C8E95DE +28981D8657E2 +0EAC51865896 +B28A117C53D8 +0A781CE2CA0A +69BB3548838A +565E61244AD7 +A248CC08860D +191A8A5C78DA +B856AB51A2B5 +1C7AC2DE659B +4AAC5A6B3BBC +7753E83C7317 +8C727B9A4976 +B8DEC1A20CC1 +985BA34B3CDE +891527CE1A67 +6A0711D80BC2 +9D67469DA843 +3059B959D5AA +561722395A5A +C53123D111BA +37A8722CE0E4 +CCB6191A91D2 +ECDB7885695E +65BBD2D91345 +5E38772C90D2 +E5644A33E9A5 +109E4373CD42 +EA700443BEB2 +A555291DCB7E +444170535CBC +B569A17B2B87 +31AC01129E44 +80442D92D54B +EBA279E0D083 +87D604DDBC7E +58A7EA0D905E +683D51D34630 +B85D19424C82 +5ACC4198E434 +44A941B0D7DC +3C28D252B6A0 +A462D4E99724 +64E31AE1989B +E3AA44A167D8 +D8DE20C3B6B1 +1251090C9103 +54E30DEEDE6D +7125761BB98A +063A0478D8CD +C3914ACC6019 +40BBC81599E9 +A4E33E13B871 +20154976C343 +84C146D64290 +CC45222DC5DE +2A52A519073E +B514789AD1E8 +970EB4091E9D +B5B75B91694E +3B77E0250510 +ACB6E4ECD673 +37E15712B2DB +D0BA25C058BD +6BDC1A5D0676 +8BAAB02E27B6 +E7577211A0A4 +ADC160B9C792 +9E913713EA70 +AE5BC31C411A +B46765315DC3 +04AE9A3E5605 +8E888BC84866 +9A65959C1657 +A717D83A14CE +69E56497B006 +24E326AE30CE +32AC291E710B +27D7A2BB2C63 +2CCBC1771254 +BC7EED40A18A +38C3723625B6 +94700B5BC750 +2287719CAA68 +2AD469A43BC9 +66E7A06E58C8 +50DD72382496 +27AC9659EB33 +A683B9112412 +60D0EC8BBB25 +AA454BDC1751 +BB07B6448965 +662385C60874 +DAA4A17A03DA +67700D6E7564 +46ADE1866A51 +E78D4C90C690 +647D716611AE +18CB91B6885D +6D8685C1D03D +3CC4AAEC0328 +E0CB52A4E6D0 +37B917695320 +2A0DA26BA900 +92004D245712 +8903AEDBDD44 +B8348BE9E798 +9DCDD0537657 +D6E389C5768B +C4CC761C596A +17831D717C0B +70C7AD2D803A +829DBCB1D3A7 +C6528ADD3351 +2E2A2948BB14 +59815AD51456 +E9A82824D87E +939A6A5AD0A3 +99188183C358 +3C1201201265 +C60B2E90C3A8 +98E67920EA80 +1BCB9C998CC5 +9938906C6A35 +684AEAC5E5C2 +82DA8B7700BE +5D62064D9483 +0724B3DE764C +4D1BEDCD27AE +0CA01C21308E +AD74BD1544EA +10AA90AD7D1C +C628DEDD5288 +E6ABBDE8BAE1 +2EA35D0DC68A +30841254DB83 +744DC682A6CE +ADE4254645B8 +328EA72990D6 +784411B05C8B +25D76529AD7B +BC045408E903 +85C304878DA3 +721902B501BC +292DC1E5B261 +803E4B493C45 +21B9C87CB0DC +BE14A4DAE603 +E72819030E1A +99727670A2C6 +886490DAC3A0 +D3C0CCAD5B33 +54A677CBEDED +889493ECCAB5 +11983863D69B +566C87DA99D2 +60612B26CB13 +03949A72451C +7A84A77855D6 +73E407AE91CE +441963CD9828 +635724E8E50D +1737C5A5CEE8 +D97913813EBA +46B228B2A700 +9914DE7E72EE +23B0D32D5D6E +2313276863B7 +2ABACE62B22B +6301EB620D41 +5E69AEA5644E +BEDBBE573715 +D174569EB055 +2EE2BE0199D9 +982676A509E6 +1E3076CEA985 +ACD945511A90 +96E7C7196918 +C8593AA03B1C +07CD9D3E1E9A diff --git a/hanxlleon/0004/0004.py b/hanxlleon/0004/0004.py new file mode 100644 index 00000000..96882360 --- /dev/null +++ b/hanxlleon/0004/0004.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +#第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 + +def counter(string): + ignore = [',', '.', ':', '!', '?', '“', '”', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] + for i in ignore: + string = string.replace(i, ' ') + words = string.lower().split() + dic = {} + for word in words: + if word in dic: + dic[word] += 1 + else: + dic[word] = 1 + return dic + +def file_read(filename): + with open(filename, 'r') as f: + lines = f.read() + return lines + +if __name__ == '__main__': + lines = file_read('0004.txt') + item_dic = counter(lines) + for k, v in item_dic.items(): + print k, v + + diff --git a/hanxlleon/0004/0004.txt b/hanxlleon/0004/0004.txt new file mode 100644 index 00000000..a6212961 --- /dev/null +++ b/hanxlleon/0004/0004.txt @@ -0,0 +1 @@ +Then I looked, and behold, on Mount Zion stood the Lamb, and with him 144,000 who had his name and his Fathers name written on their foreheads.And I heard a voice from heaven like the roar of many waters and like the sound of loud thunder. The voice I heard was like the sound of harpists playing on their harps,and they were singing a new song before the throne and before the four living creatures and before the elders. No one could learn that song except the 144,000 who had been redeemed from the earth.It is these who have not defiled themselves with women, for they are virgins. It is these who follow the Lamb wherever he goes. These have been redeemed from mankind as firstfruits for God and the Lamb,and in their mouth no lie was found, for they are blameless.The Messages of the Three AngelsThen I saw another angel flying directly overhead, with an eternal gospel to proclaim to those who dwell on earth, to every nation and tribe and language and people.And he said with a loud voice, Fear God and give him glory, because the hour of his judgment has come, and worship him who made heaven and earth, the sea and the springs of water.Another angel, a second, followed, saying, Fallen, fallen is Babylon the great, she who made all nations drink the wine of the passion1 of her sexual immorality.And another angel, a third, followed them, saying with a loud voice, If anyone worships the beast and its image and receives a mark on his forehead or on his hand,10 he also will drink the wine of Gods wrath, poured full strength into the cup of his anger, and he will be tormented with fire and sulfur in the presence of the holy angels and in the presence of the Lamb.And the smoke of their torment goes up forever and ever, and they have no rest, day or night, these worshipers of the beast and its image, and whoever receives the mark of its name.Here is a call for the endurance of the saints, those who keep the commandments of God and their faith in Jesus.2And I heard a voice from heaven saying, Write this: Blessed are the dead who die in the Lord from now on. Blessed indeed, says the Spirit, that they may rest from their labors, for their deeds follow them!The Harvest of the EarthThen I looked, and behold, a white cloud, and seated on the cloud one like a son of man, with a golden crown on his head, and a sharp sickle in his hand.And another angel came out of the temple, calling with a loud voice to him who sat on the cloud, Put in your sickle, and reap, for the hour to reap has come, for the harvest of the earth is fully ripe.So he who sat on the cloud swung his sickle across the earth, and the earth was reaped.Then another angel came out of the temple in heaven, and he too had a sharp sickle.And another angel came out from the altar, the angel who has authority over the fire, and he called with a loud voice to the one who had the sharp sickle, Put in your sickle and gather the clusters from the vine of the earth, for its grapes are ripe.So the angel swung his sickle across the earth and gathered the grape harvest of the earth and threw it into the great winepress of the wrath of God.And the winepress was trodden outside the city, and blood flowed from the winepress, as high as a horses bridle, for 1,600 stadia. \ No newline at end of file diff --git a/hanxlleon/0005/0005.py b/hanxlleon/0005/0005.py new file mode 100644 index 00000000..abed8d00 --- /dev/null +++ b/hanxlleon/0005/0005.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +#第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +#iphone5的分辨率为1136x640像素 + +from PIL import Image +import glob, os + +def search_imgfile(path): + return glob.glob(os.path.join(path, '*.jpg')) + +def change_image_size(filename, size=(1136, 640)): + im = Image.open(filename) + im.thumbnail(size, Image.ANTIALIAS) + #s_w, s_h = im.size + #ratio = min(size[0]/s_w, size[1]/s_h) + #im2 = im.resize((s_w*ratio, s_h*ratio), Image.ANTIALIAS) + #im2.save('result-' + image_path) + (path, imgname) = os.path.split(filename) + savedname = str(size[0])+'_'+str(size[1])+'_'+imgname + im.save(os.path.join(path, savedname)) + +if __name__ == '__main__': + imgfiles = search_imgfile('img') + for img in imgfiles: + change_image_size(img) diff --git a/hanxlleon/0005/img/1600x900.jpg b/hanxlleon/0005/img/1600x900.jpg new file mode 100644 index 00000000..b49f3a84 Binary files /dev/null and b/hanxlleon/0005/img/1600x900.jpg differ diff --git a/hanxlleon/0005/img/2_1600x900.jpg b/hanxlleon/0005/img/2_1600x900.jpg new file mode 100644 index 00000000..b49f3a84 Binary files /dev/null and b/hanxlleon/0005/img/2_1600x900.jpg differ diff --git a/hanxlleon/0006/0006.py b/hanxlleon/0006/0006.py new file mode 100644 index 00000000..c1aa77ae --- /dev/null +++ b/hanxlleon/0006/0006.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +import re + + +#读取文件内容 +def file_read(filename): + with open(filename, 'r') as f: + article = f.read() + return article + + +#返回单词列表 +def word_list(article): + words = re.findall(r'[a-zA-Z]+\b', article) #\b可以匹配单词的开头或者结尾 + return words + + +#返回排序后的单词列表 +def word_rate(words): + w_rate = {} + for word in words: + if word in w_rate: + w_rate[word] += 1 + else: + w_rate[word] = 1 + li = sorted(w_rate.iteritems(), key=lambda d:d[1], reverse=True) + return li + + +if __name__ == '__main__': + article = file_read('0006.txt') + words = word_list(article) + li = word_rate(words) + print '%s: %s' % li[0] + diff --git a/hanxlleon/0006/0006.txt b/hanxlleon/0006/0006.txt new file mode 100644 index 00000000..a6212961 --- /dev/null +++ b/hanxlleon/0006/0006.txt @@ -0,0 +1 @@ +Then I looked, and behold, on Mount Zion stood the Lamb, and with him 144,000 who had his name and his Fathers name written on their foreheads.And I heard a voice from heaven like the roar of many waters and like the sound of loud thunder. The voice I heard was like the sound of harpists playing on their harps,and they were singing a new song before the throne and before the four living creatures and before the elders. No one could learn that song except the 144,000 who had been redeemed from the earth.It is these who have not defiled themselves with women, for they are virgins. It is these who follow the Lamb wherever he goes. These have been redeemed from mankind as firstfruits for God and the Lamb,and in their mouth no lie was found, for they are blameless.The Messages of the Three AngelsThen I saw another angel flying directly overhead, with an eternal gospel to proclaim to those who dwell on earth, to every nation and tribe and language and people.And he said with a loud voice, Fear God and give him glory, because the hour of his judgment has come, and worship him who made heaven and earth, the sea and the springs of water.Another angel, a second, followed, saying, Fallen, fallen is Babylon the great, she who made all nations drink the wine of the passion1 of her sexual immorality.And another angel, a third, followed them, saying with a loud voice, If anyone worships the beast and its image and receives a mark on his forehead or on his hand,10 he also will drink the wine of Gods wrath, poured full strength into the cup of his anger, and he will be tormented with fire and sulfur in the presence of the holy angels and in the presence of the Lamb.And the smoke of their torment goes up forever and ever, and they have no rest, day or night, these worshipers of the beast and its image, and whoever receives the mark of its name.Here is a call for the endurance of the saints, those who keep the commandments of God and their faith in Jesus.2And I heard a voice from heaven saying, Write this: Blessed are the dead who die in the Lord from now on. Blessed indeed, says the Spirit, that they may rest from their labors, for their deeds follow them!The Harvest of the EarthThen I looked, and behold, a white cloud, and seated on the cloud one like a son of man, with a golden crown on his head, and a sharp sickle in his hand.And another angel came out of the temple, calling with a loud voice to him who sat on the cloud, Put in your sickle, and reap, for the hour to reap has come, for the harvest of the earth is fully ripe.So he who sat on the cloud swung his sickle across the earth, and the earth was reaped.Then another angel came out of the temple in heaven, and he too had a sharp sickle.And another angel came out from the altar, the angel who has authority over the fire, and he called with a loud voice to the one who had the sharp sickle, Put in your sickle and gather the clusters from the vine of the earth, for its grapes are ripe.So the angel swung his sickle across the earth and gathered the grape harvest of the earth and threw it into the great winepress of the wrath of God.And the winepress was trodden outside the city, and blood flowed from the winepress, as high as a horses bridle, for 1,600 stadia. \ No newline at end of file diff --git a/hanxlleon/0007/0007.py b/hanxlleon/0007/0007.py new file mode 100644 index 00000000..2d8a687a --- /dev/null +++ b/hanxlleon/0007/0007.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- + +import glob +import re +import os + + +def file_read(filename): + expline = blankline = 0 + with open(filename, 'r') as f: + lines = f.readlines() + for line in lines: + n = line.strip() + if n.startswith('#') or n.startswith("'''") or n.endswith("'''\n"): + expline += 1 + elif n == '': + blankline += 1 + return len(lines), expline, blankline + + +if __name__ == '__main__': + codeline = expline = blankline = 0 + os.chdir('./code') + file_list = glob.glob('*') + for file in file_list: + (n1, n2, n3) = file_read(file) + codeline += (n1 - n2 - n3) + expline += n2 + blankline += n3 + print 'codeline: %d , ecpline: %d, blankline: %d' % (codeline, + expline, blankline) + diff --git a/hanxlleon/0007/code/0004.py b/hanxlleon/0007/code/0004.py new file mode 100644 index 00000000..96882360 --- /dev/null +++ b/hanxlleon/0007/code/0004.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +#第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 + +def counter(string): + ignore = [',', '.', ':', '!', '?', '“', '”', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] + for i in ignore: + string = string.replace(i, ' ') + words = string.lower().split() + dic = {} + for word in words: + if word in dic: + dic[word] += 1 + else: + dic[word] = 1 + return dic + +def file_read(filename): + with open(filename, 'r') as f: + lines = f.read() + return lines + +if __name__ == '__main__': + lines = file_read('0004.txt') + item_dic = counter(lines) + for k, v in item_dic.items(): + print k, v + + diff --git a/hanxlleon/0007/code/0005.py b/hanxlleon/0007/code/0005.py new file mode 100644 index 00000000..e240e71f --- /dev/null +++ b/hanxlleon/0007/code/0005.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +#第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +#iphone5的分辨率为1136x640像素 + +from PIL import Image + +def change_image_size(image_path='0005.jpg', size=(1136, 640)): + im = Image.open(image_path) + im.thumbnail((1136, 640), Image.ANTIALIAS) + #im2 = im.resize(size) + #im2.save('result-' + image_path) + im.save('result-' + image_path) + +if __name__ == '__main__': + change_image_size('0005.jpg') + diff --git a/hanxlleon/0007/code/0006.py b/hanxlleon/0007/code/0006.py new file mode 100644 index 00000000..c1aa77ae --- /dev/null +++ b/hanxlleon/0007/code/0006.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +import re + + +#读取文件内容 +def file_read(filename): + with open(filename, 'r') as f: + article = f.read() + return article + + +#返回单词列表 +def word_list(article): + words = re.findall(r'[a-zA-Z]+\b', article) #\b可以匹配单词的开头或者结尾 + return words + + +#返回排序后的单词列表 +def word_rate(words): + w_rate = {} + for word in words: + if word in w_rate: + w_rate[word] += 1 + else: + w_rate[word] = 1 + li = sorted(w_rate.iteritems(), key=lambda d:d[1], reverse=True) + return li + + +if __name__ == '__main__': + article = file_read('0006.txt') + words = word_list(article) + li = word_rate(words) + print '%s: %s' % li[0] + diff --git a/hanxlleon/0008/0008.py b/hanxlleon/0008/0008.py new file mode 100644 index 00000000..94a87085 --- /dev/null +++ b/hanxlleon/0008/0008.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +import urllib2 +import re + + +baidu_url = 'http://wengengmiao.baijia.baidu.com/article/52893' + +class Spider: + def __init__(self, url=baidu_url): + self.url = url + self.user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:36.0)' + self.header = { 'User-Agent': self.user_agent } + + @profile + def read_page(self): + try: + #request = urllib2.Request(self.url, headers=self.header) + #response = urllib2.urlopen(request) + response = urllib2.urlopen(self.url) + page = response.read().decode('utf-8') + except urllib2.URLError, e: + if hasattr(e, 'reason'): + print u'连接出错, 原因:', e.reason + return None + return page + + @profile + def article(self, page): + pattern = re.compile(r'
([^<>]*?)
', re.S) # 测试了下,这里大概需要5s + lines = re.findall(pattern, page) # 这里大概需要0.8s + return ''.join(lines) + + +if __name__ == '__main__': + spider = Spider(baidu_url) + page = spider.read_page() + print spider.article(page) diff --git a/hanxlleon/0008/0008.py.lprof b/hanxlleon/0008/0008.py.lprof new file mode 100644 index 00000000..2c0a5b77 Binary files /dev/null and b/hanxlleon/0008/0008.py.lprof differ diff --git a/hanxlleon/0009/0009.py b/hanxlleon/0009/0009.py new file mode 100644 index 00000000..e964fd9b --- /dev/null +++ b/hanxlleon/0009/0009.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +import requests +from bs4 import BeautifulSoup + + +class Spider: + def __init__(self, url): + self.url = url + + def get_page(self): + r = requests.get(self.url) + page = r.content + return page + + def links(self, page): + soup = BeautifulSoup(page) + links = [link.get('href') for link in soup.find_all('a') + if link.get('href') and link.get('href').startswith('http')] + return '\n'.join(links) + + +if __name__ == '__main__': + baidu_url = 'http://wengengmiao.baijia.baidu.com/article/52893' + spider = Spider(baidu_url) + page = spider.get_page() + links = spider.links(page) + print links + + diff --git a/hanxlleon/0010/0010.py b/hanxlleon/0010/0010.py new file mode 100644 index 00000000..30a9e0d1 --- /dev/null +++ b/hanxlleon/0010/0010.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import Image, ImageDraw, ImageFont, ImageFilter +import random +import string + + +class Idcode: + def __init__(self, width=240, height=60, fontsize=36, fontcnt=4): + self.width = width + self.height = height + self.fontsize = fontsize + self.fontcnt = fontcnt + + def rand_char(self): + return random.choice(string.uppercase) + + def rand_bgcolor(self): + return (random.randint(64, 255), random.randint(64, 255), + random.randint(64, 255)) + + def rand_fontcolor(self): + return (random.randint(32, 127), random.randint(32, 127), + random.randint(32, 127)) + + def generate_code(self, name='image.jpg'): + image = Image.new('RGB', (self.width, self.height), (255, 255, 255)) + font = ImageFont.truetype('Arial.ttf', self.fontsize) #创建font + draw = ImageDraw.Draw(image) # 创建draw + + for x in range(self.width): + for y in range(self.height): + draw.point((x, y), fill=self.rand_bgcolor()) + + for t in range(self.fontcnt): + draw.text(((60 * t + 10, 10)), self.rand_char(), font=font, fill=self.rand_fontcolor()) + + image = image.filter(ImageFilter.BLUR) + image.save(name, 'jpeg') + + +if __name__ == '__main__': + Idcode().generate_code('4image.jpg') + Idcode(360, 60, 36, 6).generate_code('6image.jpg') diff --git a/hanxlleon/0010/4image.jpg b/hanxlleon/0010/4image.jpg new file mode 100644 index 00000000..73633f82 Binary files /dev/null and b/hanxlleon/0010/4image.jpg differ diff --git a/hanxlleon/0010/6image.jpg b/hanxlleon/0010/6image.jpg new file mode 100644 index 00000000..9f95ca13 Binary files /dev/null and b/hanxlleon/0010/6image.jpg differ diff --git a/hanxlleon/0011/0011.py b/hanxlleon/0011/0011.py new file mode 100644 index 00000000..2101d7df --- /dev/null +++ b/hanxlleon/0011/0011.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + + +with open('filtered_words.txt', 'r') as f: + line = f.read().decode('utf-8') + +words = line.splitlines() +print words +while True: + input = raw_input('("q":exit) input > ') + if input == 'q': + exit() + for word in words: + if input.find(word) != -1: + print 'Freedom' + break + else: + print 'Human Rights' + diff --git a/hanxlleon/0011/filtered_words.txt b/hanxlleon/0011/filtered_words.txt new file mode 100644 index 00000000..69373b64 --- /dev/null +++ b/hanxlleon/0011/filtered_words.txt @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex +jiangge \ No newline at end of file diff --git a/hanxlleon/0012/0012.py b/hanxlleon/0012/0012.py new file mode 100644 index 00000000..0630729c --- /dev/null +++ b/hanxlleon/0012/0012.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import re + + +with open('filtered_words.txt', 'r') as f: + lines = f.read().decode('utf-8') + +words = '|'.join(lines.splitlines()) +while True: + input = raw_input('("q":exit) input > ') + if input == 'q': + exit() + #for word in words: + print re.sub(words, '**', input) + diff --git a/hanxlleon/0012/filtered_words.txt b/hanxlleon/0012/filtered_words.txt new file mode 100644 index 00000000..69373b64 --- /dev/null +++ b/hanxlleon/0012/filtered_words.txt @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex +jiangge \ No newline at end of file diff --git a/hanxlleon/0013/0013.py b/hanxlleon/0013/0013.py new file mode 100644 index 00000000..e27f9508 --- /dev/null +++ b/hanxlleon/0013/0013.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import urllib +import requests +from bs4 import BeautifulSoup +import os + +url = 'http://tieba.baidu.com/p/2166231880' +r = requests.get(url) +soup = BeautifulSoup(r.content) + +imgurls = [imglink.get('src') for imglink in soup.find_all('img') + if imglink.get('pic_type') == '0'] + +for imgurl in imgurls: + urllib.urlretrieve(imgurl, os.path.split(imgurl)[1]) diff --git a/hanxlleon/0014/0014.py b/hanxlleon/0014/0014.py new file mode 100644 index 00000000..509c02a1 --- /dev/null +++ b/hanxlleon/0014/0014.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +import xlwt +import simplejson as json +import os + + +def get_json(filename): + with open(filename, 'r') as f: + return json.load(f) + +def write_xls(filename, data): + book = xlwt.Workbook(encoding='utf-8') + sheetname = os.path.splitext(os.path.split(filename)[1])[0] # 提取文件名(不带格式),用于表名 + sheet = book.add_sheet(sheetname, cell_overwrite_ok=True) + + dataitems = sorted(data.iteritems(), key=lambda d:d[0]) # 将字典排序,保存为列表 + + row, col = 0, 0 + for (key, values) in dataitems: + sheet.write(row, col, key) + col += 1 + for value in values: + sheet.write(row, col, value) + col += 1 + row += 1 + col = 0 + book.save(filename) + + +if __name__ == '__main__': + data = get_json('student.txt') + write_xls('student.xls', data) diff --git a/hanxlleon/0014/student.txt b/hanxlleon/0014/student.txt new file mode 100644 index 00000000..ea9b1593 --- /dev/null +++ b/hanxlleon/0014/student.txt @@ -0,0 +1,5 @@ +{ + "1":["张三",150,120,100], + "2":["李四",90,99,95], + "3":["王五",60,66,68] +} \ No newline at end of file diff --git a/hanxlleon/0014/student.xls b/hanxlleon/0014/student.xls new file mode 100644 index 00000000..4b2b6933 Binary files /dev/null and b/hanxlleon/0014/student.xls differ diff --git a/hanxlleon/0015/0015.py b/hanxlleon/0015/0015.py new file mode 100644 index 00000000..a59e9eeb --- /dev/null +++ b/hanxlleon/0015/0015.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +import xlwt +import simplejson as json +import os + + +def get_json(filename): + with open(filename, 'r') as f: + return json.load(f) + +def write_xls(filename, data): + book = xlwt.Workbook(encoding='utf-8') + sheetname = os.path.splitext(os.path.split(filename)[1])[0] # 提取文件名(不带格式),用于表名 + sheet = book.add_sheet(sheetname, cell_overwrite_ok=True) + + dataitems = sorted(data.iteritems(), key=lambda d:d[0]) # 将字典排序,保存为列表 + + row, col = 0, 0 + for (key, value) in dataitems: + sheet.write(row, col, key) + col += 1 + sheet.write(row, col, value) + col += 1 + + row += 1 + col = 0 + book.save(filename) + + +if __name__ == '__main__': + data = get_json('city.txt') + write_xls('city.xls', data) diff --git a/hanxlleon/0015/city.txt b/hanxlleon/0015/city.txt new file mode 100644 index 00000000..04cb9c57 --- /dev/null +++ b/hanxlleon/0015/city.txt @@ -0,0 +1,5 @@ +{ + "1" : "上海", + "2" : "北京", + "3" : "成都" +} \ No newline at end of file diff --git a/hanxlleon/0015/city.xls b/hanxlleon/0015/city.xls new file mode 100644 index 00000000..0900537d Binary files /dev/null and b/hanxlleon/0015/city.xls differ diff --git a/hanxlleon/0016/0016.py b/hanxlleon/0016/0016.py new file mode 100644 index 00000000..e8ec967e --- /dev/null +++ b/hanxlleon/0016/0016.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +import xlwt +import simplejson as json +import os + + +def get_json(filename): + with open(filename, 'r') as f: + return json.load(f) + +def write_xls(filename, data): + book = xlwt.Workbook(encoding='utf-8') + sheetname = os.path.splitext(os.path.split(filename)[1])[0] # 提取文件名(不带格式),用于表名 + sheet = book.add_sheet(sheetname, cell_overwrite_ok=True) + + dataitems = sorted(data) + + row, col = 0, 0 + for list in dataitems: + for value in list: + sheet.write(row, col, value) + col += 1 + row += 1 + col = 0 + book.save(filename) + + +if __name__ == '__main__': + data = get_json('numbers.txt') + write_xls('numbers.xls', data) diff --git a/hanxlleon/0016/numbers.txt b/hanxlleon/0016/numbers.txt new file mode 100644 index 00000000..c43c0378 --- /dev/null +++ b/hanxlleon/0016/numbers.txt @@ -0,0 +1,5 @@ +[ + [1, 82, 65535], + [20, 90, 13], + [26, 809, 1024] +] \ No newline at end of file diff --git a/hanxlleon/0016/numbers.xls b/hanxlleon/0016/numbers.xls new file mode 100644 index 00000000..01aaa794 Binary files /dev/null and b/hanxlleon/0016/numbers.xls differ diff --git a/hanxlleon/0017/0017.py b/hanxlleon/0017/0017.py new file mode 100644 index 00000000..687b8daf --- /dev/null +++ b/hanxlleon/0017/0017.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +import xlrd +from xml.dom import minidom +import simplejson as json +import re + + +def get_xls(filename): + book = xlrd.open_workbook(filename) + sheet = book.sheet_by_index(0) + data = {} + + rows = sheet.nrows + for row in range(rows): + value = sheet.row_values(row) + data[value[0]] = value[1:] + #data = json.dumps(data) + return data + + +def write_xml(data): + doc = minidom.Document() + root = doc.createElement('root') + doc.appendChild(root) + + students = doc.createElement('students') + root.appendChild(students) + + students.appendChild(doc.createComment('\n 学生信息表\n "id" : [名字, 数学, 语文, 英文]')) + + for i in data: + data[i][0] = data[i][0].encode('utf-8') + + content = doc.createTextNode('\n{') + students.appendChild(content) + for key in data.keys(): + content = doc.createTextNode('\n\t' + str(key) + ': [' ) + students.appendChild(content) + for value in data[key]: + content = doc.createTextNode(str(value)+' ') + students.appendChild(content) + content = doc.createTextNode(']') + students.appendChild(content) + content = doc.createTextNode('\n}') + students.appendChild(content) + with open('student.xml', 'w') as f: + doc.writexml(f, encoding = "utf-8") + + +if __name__ == '__main__': + data = get_xls('student.xls') + write_xml(data) diff --git a/hanxlleon/0017/student.xls b/hanxlleon/0017/student.xls new file mode 100644 index 00000000..93ad3557 Binary files /dev/null and b/hanxlleon/0017/student.xls differ diff --git a/hanxlleon/0017/student.xml b/hanxlleon/0017/student.xml new file mode 100644 index 00000000..34a034f8 --- /dev/null +++ b/hanxlleon/0017/student.xml @@ -0,0 +1,8 @@ +请尽情留言吧:
+ +页面未找到!, 请返回
{% endblock %} \ No newline at end of file diff --git a/hanxlleon/0024/todo.py b/hanxlleon/0024/todo.py new file mode 100644 index 00000000..b61cf84e --- /dev/null +++ b/hanxlleon/0024/todo.py @@ -0,0 +1,120 @@ +# -*- coding: utf-8 -*- +from datetime import datetime +from flask import Flask, flash, redirect, request, url_for, render_template +from flask.ext.sqlalchemy import SQLAlchemy +from flask.ext.wtf import Form +from wtforms import StringField, SubmitField +from wtforms.validators import DataRequired +import os + + +DEBUG = True +SECRET_KEY = '\x80\x11\xc8\xd7n\x1d\x9aU5\xcf!?\x8f8\xbbx3>\x12\x15`b\xaa\xfb' +SQLALCHEMY_DATABASE_URI = 'sqlite:///todo.sqlite' + +app = Flask(__name__) +app.config.from_object(__name__) +db = SQLAlchemy(app) +db.init_app(app) + + + +class Todo(db.Model): + '''数据模型''' + + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(255), nullable=False) + posted_on = db.Column(db.DateTime, default=datetime.utcnow) + status = db.Column(db.Boolean, default=False) + + def __init__(self, *args, **kwargs): + super(Todo, self).__init__(*args, **kwargs) + + def __repr__(self): + return "
留言历史信息:
+ {% for comment in comments %} +{{ comment.content }}
+