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 @@ + +{ + 1: [张三 150.0 120.0 100.0 ] + 3: [王五 60.0 66.0 68.0 ] + 2: [李四 90.0 99.0 95.0 ] +} \ No newline at end of file diff --git a/hanxlleon/0018/0018.py b/hanxlleon/0018/0018.py new file mode 100644 index 00000000..6a88a60e --- /dev/null +++ b/hanxlleon/0018/0018.py @@ -0,0 +1,50 @@ +# -*- 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('citys') + root.appendChild(students) + + students.appendChild(doc.createComment('\n 城市信息')) + + 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('\n}') + students.appendChild(content) + with open('city.xml', 'w') as f: + doc.writexml(f, encoding = "utf-8") + + +if __name__ == '__main__': + data = get_xls('city.xls') + write_xml(data) diff --git a/hanxlleon/0018/city.xls b/hanxlleon/0018/city.xls new file mode 100644 index 00000000..92522204 Binary files /dev/null and b/hanxlleon/0018/city.xls differ diff --git a/hanxlleon/0018/city.xml b/hanxlleon/0018/city.xml new file mode 100644 index 00000000..21754246 --- /dev/null +++ b/hanxlleon/0018/city.xml @@ -0,0 +1,7 @@ + +{ + 1 : 上海 + 3 : 成都 + 2 : 北京 +} \ No newline at end of file diff --git a/hanxlleon/0019/0019.py b/hanxlleon/0019/0019.py new file mode 100644 index 00000000..72efa1a4 --- /dev/null +++ b/hanxlleon/0019/0019.py @@ -0,0 +1,45 @@ +# -*- 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.append(value) + + #data = json.dumps(data) + return data + + +def write_xml(data): + doc = minidom.Document() + root = doc.createElement('root') + doc.appendChild(root) + + students = doc.createElement('numbers') + root.appendChild(students) + + students.appendChild(doc.createComment('\n 数字信息')) + + content = doc.createTextNode('\n[') + students.appendChild(content) + for value in data: + content = doc.createTextNode('\n\t'+str(value)+', ') + students.appendChild(content) + content = doc.createTextNode('\n]\n') + students.appendChild(content) + with open('numbers.xml', 'w') as f: + doc.writexml(f, encoding = "utf-8") + + +if __name__ == '__main__': + data = get_xls('numbers.xls') + write_xml(data) diff --git a/hanxlleon/0019/numbers.xls b/hanxlleon/0019/numbers.xls new file mode 100644 index 00000000..8fc90cb8 Binary files /dev/null and b/hanxlleon/0019/numbers.xls differ diff --git a/hanxlleon/0019/numbers.xml b/hanxlleon/0019/numbers.xml new file mode 100644 index 00000000..02c6346b --- /dev/null +++ b/hanxlleon/0019/numbers.xml @@ -0,0 +1,8 @@ + +[ + [1.0, 82.0, 65535.0], + [20.0, 90.0, 13.0], + [26.0, 809.0, 1024.0], +] + \ No newline at end of file diff --git a/hanxlleon/0020/0020.py b/hanxlleon/0020/0020.py new file mode 100644 index 00000000..4bf349eb --- /dev/null +++ b/hanxlleon/0020/0020.py @@ -0,0 +1,21 @@ +# -*- coding:utf-8 -*- +import xlrd +import re + + +def get_time(filename): + book = xlrd.open_workbook(filename) + sheet = book.sheet_by_index(0) + timelist = sheet.col_values(4) + + sum = 0 + for t in timelist[1:]: + time = re.findall(r'\d+', t) # 找出中文日期中的数字 + for i, s in enumerate(time[::-1]): # 将时、分、秒反转为秒、分、时 + sum += int(s) * 60 ** int(i) + return sum + + +if __name__ == '__main__': + seconds = get_time('order.xls') + print '一共%s秒' % seconds diff --git a/hanxlleon/0020/order.xls b/hanxlleon/0020/order.xls new file mode 100644 index 00000000..868411e0 Binary files /dev/null and b/hanxlleon/0020/order.xls differ diff --git a/hanxlleon/0021/0021.py b/hanxlleon/0021/0021.py new file mode 100644 index 00000000..39d3a870 --- /dev/null +++ b/hanxlleon/0021/0021.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +import hashlib +import uuid + + +class User: + def __init__(self): + self.salt_password = None + + def set_password(self, raw_password): + salt = uuid.uuid4().hex # 产生随机的盐值 + hashed_password = hashlib.sha512(raw_password + salt).hexdigest() + self.salt_password = '%s$%s' % (salt, hashed_password) # 将盐和密码保存到数据库,以'$'隔开 + print 'salt: %s' % salt + print 'hashed_password: %s' % hashed_password + print 'salt_password: %s' % self.salt_password + + def check_password(self, raw_password): + (salt, hashed_password) = self.salt_password.split('$') + return hashed_password == hashlib.sha512(raw_password + salt).hexdigest() + + +if __name__ == '__main__': + user = User() + user.set_password('1234') + assert(user.check_password('1234') == True) + assert(user.check_password('123') == False) + diff --git a/hanxlleon/0022/0022.py b/hanxlleon/0022/0022.py new file mode 100644 index 00000000..4b221a1c --- /dev/null +++ b/hanxlleon/0022/0022.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +#第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +#iphone5的分辨率为1136x640像素 + +from PIL import Image +import glob, os + +def search_imgfile(path): + abspath = os.path.abspath(path) + return glob.glob(os.path.join(abspath, '*.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) + change_image_size(img, size=(1334, 750)) + change_image_size(img, size=(2208, 1242)) + diff --git a/hanxlleon/0023/.idea/.name b/hanxlleon/0023/.idea/.name new file mode 100644 index 00000000..1480a7ad --- /dev/null +++ b/hanxlleon/0023/.idea/.name @@ -0,0 +1 @@ +0023 \ No newline at end of file diff --git a/hanxlleon/0023/.idea/0023.iml b/hanxlleon/0023/.idea/0023.iml new file mode 100644 index 00000000..eccca744 --- /dev/null +++ b/hanxlleon/0023/.idea/0023.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/encodings.xml b/hanxlleon/0023/.idea/encodings.xml new file mode 100644 index 00000000..d8210482 --- /dev/null +++ b/hanxlleon/0023/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/inspectionProfiles/Project_Default.xml b/hanxlleon/0023/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..c846f68d --- /dev/null +++ b/hanxlleon/0023/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/inspectionProfiles/profiles_settings.xml b/hanxlleon/0023/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..3b312839 --- /dev/null +++ b/hanxlleon/0023/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/misc.xml b/hanxlleon/0023/.idea/misc.xml new file mode 100644 index 00000000..2f18c2f9 --- /dev/null +++ b/hanxlleon/0023/.idea/misc.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/modules.xml b/hanxlleon/0023/.idea/modules.xml new file mode 100644 index 00000000..c9262223 --- /dev/null +++ b/hanxlleon/0023/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/scopes/scope_settings.xml b/hanxlleon/0023/.idea/scopes/scope_settings.xml new file mode 100644 index 00000000..922003b8 --- /dev/null +++ b/hanxlleon/0023/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/vcs.xml b/hanxlleon/0023/.idea/vcs.xml new file mode 100644 index 00000000..6564d52d --- /dev/null +++ b/hanxlleon/0023/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hanxlleon/0023/.idea/workspace.xml b/hanxlleon/0023/.idea/workspace.xml new file mode 100644 index 00000000..416430cd --- /dev/null +++ b/hanxlleon/0023/.idea/workspace.xml @@ -0,0 +1,699 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + 1428822340250 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0023/data.sqlite b/hanxlleon/0023/data.sqlite new file mode 100644 index 00000000..c08a8372 Binary files /dev/null and b/hanxlleon/0023/data.sqlite differ diff --git a/hanxlleon/0023/forms.py b/hanxlleon/0023/forms.py new file mode 100644 index 00000000..c5034cae --- /dev/null +++ b/hanxlleon/0023/forms.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +from wtforms import StringField, TextAreaField, SubmitField +from wtforms.validators import DataRequired +from flask.ext.wtf import Form + + +class GuestForm(Form): + username = StringField('', validators=[DataRequired()]) + content = TextAreaField('', validators=[DataRequired()]) + submit = SubmitField(u'提交') \ No newline at end of file diff --git a/hanxlleon/0023/guestbook.py b/hanxlleon/0023/guestbook.py new file mode 100644 index 00000000..507066e6 --- /dev/null +++ b/hanxlleon/0023/guestbook.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from flask import Flask, render_template, redirect, flash +from flask.ext.sqlalchemy import SQLAlchemy +from forms import GuestForm +#from models import GuestBook +import os +from datetime import datetime + +basedir = os.path.abspath(os.path.dirname(__file__)) +app = Flask(__name__) +app.config['SECRET_KEY'] = '123456' +app.config['SQLALCHEMY_DATABASE_URI'] = \ + 'sqlite:///' + os.path.join(basedir, 'data.sqlite') # 设置数据库 +app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True # 每次请求结束后都会自动提交数据库中的变动 +db = SQLAlchemy(app) + + +@app.route('/', methods=['GET', 'POST']) +def guestbook(): + form = GuestForm() + if form.validate_on_submit(): + comment = GuestBook(username=form.username.data, content=form.content.data) + db.session.add(comment) + db.session.commit() + flash(u'提交成功!') + return redirect('/') + comments = GuestBook.query.order_by(GuestBook.datestamp.desc()).all() + return render_template('guestbook.html', form=form, comments=comments) + + +class GuestBook(db.Model): + __tablename__ = 'guestbook' + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(64)) + content = db.Column(db.Text) + datestamp = db.Column(db.DateTime, default=datetime.utcnow) + + +if __name__ == '__main__': + if not os.path.exists(os.path.join(basedir, 'data.sqlite')): # 第一次启动需要创建数据库 + db.create_all() + app.run(debug=True) diff --git a/hanxlleon/0023/sample.png b/hanxlleon/0023/sample.png new file mode 100644 index 00000000..2df5d8dc Binary files /dev/null and b/hanxlleon/0023/sample.png differ diff --git a/hanxlleon/0023/static/guestbook.css b/hanxlleon/0023/static/guestbook.css new file mode 100644 index 00000000..fa31c55c --- /dev/null +++ b/hanxlleon/0023/static/guestbook.css @@ -0,0 +1,49 @@ +h2 { + color: #ffffff; + background-color: #004080; + padding-top: 15px; + padding-bottom: 15px; + margin-bottom: 0; + margin-top: 0; + +} + +div.flash{ + background-color: #80b8ff; + padding-top: 15px; + padding-left: 30px; + margin-top: 0; + margin-bottom: 0; +} + +div.form{ + background-color: #80b8ff; + padding-left: 30px; + margin-top: 0; + padding-top: 10px; + padding-bottom: 20px; +} + +div.comments{ + padding-left: 30px; +} + +p.content{ + background-color: #dbdbff; +} + +div.footer{ + background-color: #004080; + padding-top: 25px; + padding-bottom: 25px; + position:relative; + width:100%; + bottom:0; + left:0; +} + +body +{ + margin: 0px; + padding: 0px; +} \ No newline at end of file diff --git a/hanxlleon/0023/templates/guestbook.html b/hanxlleon/0023/templates/guestbook.html new file mode 100644 index 00000000..0bb3574f --- /dev/null +++ b/hanxlleon/0023/templates/guestbook.html @@ -0,0 +1,59 @@ + + + + + +{# #} + + 留言簿 + + +

留言簿

+{% for flash in get_flashed_messages() %} +
+ {{ flash }} +
+{% endfor %} +
+

请尽情留言吧:

+
+ {{ form.hidden_tag() }} + + + + + + + + + +
+ 姓名: + + {{ form.username.label }} {{ form.username() }}
+
+ 内容: + + {{ form.content.label }} {{ form.content() }}
+
+ {{ form.submit() }} +
+
+
+

留言历史信息:

+ {% for comment in comments %} +
+ {{ comment.username }} 留言于({{ '%s' % comment.datestamp}}) +{# {{ comment.username }} 留言于({{ moment(comment.datestamp).format('L') }})#} +

{{ comment.content }}

+
+ {% endfor %} + +
+ +{##} + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/.name b/hanxlleon/0024/.idea/.name new file mode 100644 index 00000000..a68d4d0c --- /dev/null +++ b/hanxlleon/0024/.idea/.name @@ -0,0 +1 @@ +0024 \ No newline at end of file diff --git a/hanxlleon/0024/.idea/0024.iml b/hanxlleon/0024/.idea/0024.iml new file mode 100644 index 00000000..17a57dac --- /dev/null +++ b/hanxlleon/0024/.idea/0024.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/encodings.xml b/hanxlleon/0024/.idea/encodings.xml new file mode 100644 index 00000000..d8210482 --- /dev/null +++ b/hanxlleon/0024/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/inspectionProfiles/Project_Default.xml b/hanxlleon/0024/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..c846f68d --- /dev/null +++ b/hanxlleon/0024/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/inspectionProfiles/profiles_settings.xml b/hanxlleon/0024/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..3b312839 --- /dev/null +++ b/hanxlleon/0024/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/misc.xml b/hanxlleon/0024/.idea/misc.xml new file mode 100644 index 00000000..2f18c2f9 --- /dev/null +++ b/hanxlleon/0024/.idea/misc.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/modules.xml b/hanxlleon/0024/.idea/modules.xml new file mode 100644 index 00000000..f6369100 --- /dev/null +++ b/hanxlleon/0024/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/scopes/scope_settings.xml b/hanxlleon/0024/.idea/scopes/scope_settings.xml new file mode 100644 index 00000000..922003b8 --- /dev/null +++ b/hanxlleon/0024/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/vcs.xml b/hanxlleon/0024/.idea/vcs.xml new file mode 100644 index 00000000..6564d52d --- /dev/null +++ b/hanxlleon/0024/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hanxlleon/0024/.idea/workspace.xml b/hanxlleon/0024/.idea/workspace.xml new file mode 100644 index 00000000..34e6f851 --- /dev/null +++ b/hanxlleon/0024/.idea/workspace.xml @@ -0,0 +1,656 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + 1428889861844 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/todo.py + 55 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hanxlleon/0024/static/style.css b/hanxlleon/0024/static/style.css new file mode 100644 index 00000000..8b36ef51 --- /dev/null +++ b/hanxlleon/0024/static/style.css @@ -0,0 +1,70 @@ +body { background: #fefefe; color: #343434; margin: 1em; padding: 0; font-size:105%;} +img { border: none } +a { + color: #185DA0; + background: inherit; + font-size: 13px; +} + +a:hover { + color: #9EC068; + background: inherit; +} + +h2 a { + text-decoration: none; +} + +.page { + margin: 2em auto; width: 70em; border: 3px dashed #ccc; + padding: 1em; background: white; +} + +hr { + width : 100%; + height : 2px; + background : #efefef; + border : 1px solid #ddd; + clear : both; + } + div#poweredby{ + float : right; + font-size: 0.8em; + } + li {font-size:120%; + margin-left: 2em; + border-bottom:1px dotted #aaa; + width:90%; + padding-top: 10px; + } + del {font-size:95%; color:#555;} + sub,sup {font-size:10px;} + /*from 42qu.com*/ + input{ font-size:18px;line-height:22px;height:26px; + padding:1px 3px; + border:1px solid #ddd; + margin-left: 50px; + } + + button { + font-weight:normal;color:#444; + font-size:16px;line-height:22px;height:28px; + text-decoration:center; + width:50px;overflow:visible; + margin:0; + padding:1px 3px; + background-color:#feeefe; + border:1px solid #ddd; + cursor:pointer; + } + + button:hover{ + background-color:#fff;border:1px solid #aaa;color:#000 + -moz-box-shadow:-2px 2px 2px #ccc; + box-shadow:-2px 2px 2px #ccc; + -webkit-box-shadow:-2px 2px 2px #ccc; + } + .flash + { background: #fff; padding: 0.5em; +border: 1px solid #ccc; + } \ No newline at end of file diff --git a/hanxlleon/0024/templates/base.html b/hanxlleon/0024/templates/base.html new file mode 100644 index 00000000..2592eefe --- /dev/null +++ b/hanxlleon/0024/templates/base.html @@ -0,0 +1,41 @@ + + + + + + 任务追踪 (Flask版) 简明TODO + + + + + + +

= 任务追踪 (Flask版) =

+
+ +{% block content %} {% endblock %} +
+{% for message in get_flashed_messages() %} +
{{ message }}
+{% endfor %} + +
+
+Copyright (c) 2011 wwq0327. powered by: python, flask . 工程地址 +
+
+ + \ No newline at end of file diff --git a/hanxlleon/0024/templates/edit.html b/hanxlleon/0024/templates/edit.html new file mode 100644 index 00000000..9aeb7baa --- /dev/null +++ b/hanxlleon/0024/templates/edit.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% block content %} +

== 修改 ==

+
+{{ form.hidden_tag() }} +{{ form.csrf }} + + + + + +
{{ form.title() }}{{ form.submit() }}
+
+{% endblock %} diff --git a/hanxlleon/0024/templates/index.html b/hanxlleon/0024/templates/index.html new file mode 100644 index 00000000..193172b2 --- /dev/null +++ b/hanxlleon/0024/templates/index.html @@ -0,0 +1,40 @@ +{% extends "base.html" %} +{% block content %} + +

== 待办 ==

+{% if todo %} +
    + {% for t in todo %} + {% if t.status==False %} +
  1. {{ t.title|safe }} :: 删除 | 修改 | + 完成
  2. + {% endif %} + {% endfor %} +
+{% else %} + 暂时无记录 +{% endif %} +

== 完成 ==

+{% if todo %} +
    + {% for t in todo %} + {% if t.status==True %} +
  1. {{ t.title|safe }} :: 删除 | 重置
  2. + {% endif %} + {% endfor %} +
+{% else %} + 暂时无记录 +{% endif %} +

== 追加 ==

+
+{{ form.hidden_tag() }} +{{ form.csrf }} + + + + + +
{{ form.title() }}{{ form.submit() }}
+
+{% endblock %} \ No newline at end of file diff --git a/hanxlleon/0024/templates/page_404.html b/hanxlleon/0024/templates/page_404.html new file mode 100644 index 00000000..748ab7e6 --- /dev/null +++ b/hanxlleon/0024/templates/page_404.html @@ -0,0 +1,2 @@ +{% extends "base.html" %} +{% block content %}

页面未找到!, 请返回

{% 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 "" % self.title + + def store_to_db(self): + '''保存到数据库''' + + db.session.add(self) + db.session.commit() + + def delete_todo(self): + '''从数据库删除数据''' + + db.session.delete(self) + db.session.commit() + + +class TodoForm(Form): + title = StringField(u'内容', validators=[DataRequired()]) + submit = SubmitField(u'提交') + + +@app.route('/', methods=['GET', 'POST']) +def index(): + todo = Todo.query.order_by('-id').all() + form = TodoForm() + if form.validate_on_submit(): + t = Todo(title=form.title.data) + try: + t.store_to_db() + flash(u"添加成功") + return redirect(request.args.get('next') or url_for('index')) + except: + flash(u'存储失败!') + + return render_template('index.html', todo=todo, form=form) + + +@app.route('//edit', methods=['GET', 'POST']) +def edit(id): + todo = Todo.query.filter_by(id=id).first() + form = TodoForm(title=todo.title) + if request.method == 'POST' and form.validate_on_submit(): + Todo.query.filter_by(id=id).update({Todo.title: request.form['title']}) + db.session.commit() + flash(u"记录编辑成功") + return redirect(url_for('index')) + + return render_template('edit.html', todo=todo, form=form) + + +@app.route('//del') +def tdel(id): + todo = Todo.query.filter_by(id=id).first() + if todo: + todo.delete_todo() + flash(u"记录删除成功") + return redirect(url_for('index')) + + +@app.route('//done') +def done(id): + todo = Todo.query.filter_by(id=id).first() + if todo: + Todo.query.filter_by(id=id).update({Todo.status: True}) + db.session.commit() + flash(u"任务完成") + return redirect(url_for('index')) + + +@app.route('//redo') +def redo(id): + todo = Todo.query.filter_by(id=id).first() + if todo: + Todo.query.filter_by(id=id).update({Todo.status: False}) + flash(u"记录重置成功") + db.session.commit() + return redirect(url_for('index')) + + +@app.errorhandler(404) +def page_not_found(error): + return render_template('page_404.html'), 404 + + +if __name__ == '__main__': + if not os.path.exists(os.path.join(os.path.dirname(__file__), 'todo.sqlite')): + db.create_all() + app.run() diff --git a/hanxlleon/0024/todo.sqlite b/hanxlleon/0024/todo.sqlite new file mode 100644 index 00000000..0f6d8c23 Binary files /dev/null and b/hanxlleon/0024/todo.sqlite differ diff --git a/hanxlleon/README.md b/hanxlleon/README.md new file mode 100644 index 00000000..a6cd85e1 --- /dev/null +++ b/hanxlleon/README.md @@ -0,0 +1,180 @@ +## Python 练习册,每天一个小程序 ## + + +#### 说明: #### + +- Python 练习册,每天一个小程序。注:将 Python 换成其他语言,大多数题目也适用 +- 不会出现诸如「打印九九乘法表」、「打印水仙花」之类的题目 +- [点此链接,会看到每个题目的代码, 欢迎大家 Pull Request 出题目,贴代码(Gist、Blog皆可):-)](https://github.com/Show-Me-the-Code/python) +- 本文本文由@史江歌(shijiangge@gmail.com QQ:499065469)根据互联网资料收集整理而成,感谢互联网,感谢各位的分享。鸣谢!本文会不断更新。 + +> Talk is cheap. Show me the code.--Linus Torvalds + +---------- + +**第 0000 题:**将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 +类似于图中效果 + +![头像](http://i.imgur.com/sg2dkuY.png?1) + +**第 0001 题:**做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用**生成激活码**(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? + +**第 0002 题**:将 0001 题生成的 200 个激活码(或者优惠券)保存到 **MySQL** 关系型数据库中。 + +**第 0003 题:**将 0001 题生成的 200 个激活码(或者优惠券)保存到 **Redis** 非关系型数据库中。 + +**第 0004 题:**任一个英文的纯文本文件,统计其中的单词出现的个数。 + +**第 0005 题:**你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 + +**第 0006 题:**你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。 + +**第 0007 题:**有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。 + +**第 0008 题:**一个HTML文件,找出里面的**正文**。 + +**第 0009 题:**一个HTML文件,找出里面的**链接**。 + +**第 0010 题:**使用 Python 生成类似于下图中的**字母验证码图片** + +![字母验证码](http://i.imgur.com/aVhbegV.jpg) + +- [阅读资料](http://stackoverflow.com/questions/2823316/generate-a-random-letter-in-python) + +**第 0011 题:** 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。 + + 北京 + 程序员 + 公务员 + 领导 + 牛比 + 牛逼 + 你娘 + 你妈 + love + sex + jiangge + +**第 0012 题:** 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。 + +**第 0013 题:** 用 Python 写一个爬图片的程序,爬 [这个链接里的日本妹子图片 :-)](http://tieba.baidu.com/p/2166231880) + +- [参考代码](http://www.v2ex.com/t/61686 "参考代码") + +**第 0014 题:** 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: + + { + "1":["张三",150,120,100], + "2":["李四",90,99,95], + "3":["王五",60,66,68] + } + +请将上述内容写到 student.xls 文件中,如下图所示: + +![student.xls](http://i.imgur.com/nPDlpme.jpg) + +- [阅读资料](http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html) 腾讯游戏开发 XML 和 Excel 内容相互转换 + +**第 0015 题:** 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示: + + { + "1" : "上海", + "2" : "北京", + "3" : "成都" + } + +请将上述内容写到 city.xls 文件中,如下图所示: + +![city.xls](http://i.imgur.com/rOHbUzg.png) + + +**第 0016 题:** 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示: + + [ + [1, 82, 65535], + [20, 90, 13], + [26, 809, 1024] + ] + +请将上述内容写到 numbers.xls 文件中,如下图所示: + +![numbers.xls](http://i.imgur.com/iuz0Pbv.png) + +**第 0017 题:** 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如 + +下所示: + + + + + + { + "1" : ["张三", 150, 120, 100], + "2" : ["李四", 90, 99, 95], + "3" : ["王五", 60, 66, 68] + } + + + + +**第 0018 题:** 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下所示: + + + + + + { + "1" : "上海", + "2" : "北京", + "3" : "成都" + } + + + +**第 0019 题:** 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下 + +所示: + + + + + + + [ + [1, 82, 65535], + [20, 90, 13], + [26, 809, 1024] + ] + + + + +**第 0020 题:** [登陆中国联通网上营业厅](http://iservice.10010.com/index_.html) 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。写代码,对每月通话时间做个统计。 + +**第 0021 题:** 通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。 + +- 阅读资料 [用户密码的存储与 Python 示例](http://zhuoqiang.me/password-storage-and-python-example.html) + +- 阅读资料 [Hashing Strings with Python](http://www.pythoncentral.io/hashing-strings-with-python/) + +- 阅读资料 [Python's safest method to store and retrieve passwords from a database](http://stackoverflow.com/questions/2572099/pythons-safest-method-to-store-and-retrieve-passwords-from-a-database) + +**第 0022 题:** iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用。 + +**第 0023 题:** 使用 Python 的 Web 框架,做一个 Web 版本 留言簿 应用。 + +[阅读资料:Python 有哪些 Web 框架](http://v2ex.com/t/151643#reply53) + +- ![留言簿参考](http://i.imgur.com/VIyCZ0i.jpg) + + +**第 0024 题:** 使用 Python 的 Web 框架,做一个 Web 版本 TodoList 应用。 + +- ![SpringSide 版TodoList](http://i.imgur.com/NEf7zHp.jpg)