diff --git a/sarikasama/0000/0000.py b/sarikasama/0000/0000.py new file mode 100644 index 00000000..c5c0815c --- /dev/null +++ b/sarikasama/0000/0000.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +#Add a number on my icon with the name "icon.png". +#My icon is a little ... big. +#Problem0000 + +from PIL import Image, ImageDraw, ImageFont + +def add_number(num): + im = Image.open("icon.png") + #make a image for showing the number + txt = Image.new('RGBA', im.size, (255,255,255,0)) + #use the font "arial.ttf" + fnt = ImageFont.truetype("arial.ttf",40) + #draw context + d = ImageDraw.Draw(txt) + #draw the number + d.text((im.size[0]-50 ,5), str(num), font=fnt, fill=(255,0,0,255)) + + out = Image.alpha_composite(im, txt) + out.show() + out.save("icon_"+str(num)+".png") + +if __name__ == '__main__': + add_number(42) diff --git a/sarikasama/0000/arial.ttf b/sarikasama/0000/arial.ttf new file mode 100644 index 00000000..7ff88f22 Binary files /dev/null and b/sarikasama/0000/arial.ttf differ diff --git a/sarikasama/0000/icon.png b/sarikasama/0000/icon.png new file mode 100644 index 00000000..b8e21672 Binary files /dev/null and b/sarikasama/0000/icon.png differ diff --git a/sarikasama/0000/icon_42.png b/sarikasama/0000/icon_42.png new file mode 100644 index 00000000..656e1adc Binary files /dev/null and b/sarikasama/0000/icon_42.png differ diff --git a/sarikasama/0001/0001.py b/sarikasama/0001/0001.py new file mode 100644 index 00000000..51cbb023 --- /dev/null +++ b/sarikasama/0001/0001.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +#generate 200 activation codes for my apple store app + +import random, string + +def gene_activation_code(count, length): + #make sure codes are diffrent + res = set() + while len(res) < count: + res.add(''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(length))) + return res + +if __name__ == "__main__": + res = gene_activation_code(200, 8) + f = open("codes","w") + f.write("\n".join(res)) + f.close() diff --git a/sarikasama/0001/codes b/sarikasama/0001/codes new file mode 100644 index 00000000..88130bcf --- /dev/null +++ b/sarikasama/0001/codes @@ -0,0 +1,200 @@ +9HM28I2V +YUTUSWLX +KO910ZXR +1XQ7OMTH +R8B8XWT3 +CAZDMATI +HNHAU3QA +VR2A5O8L +1A2TG34C +X3JT0T0B +H4LSILG3 +BCICBNNJ +LUFUT2S0 +4H51EV7M +WMHE0LX1 +LBO6B5U7 +EJG3J6ZZ +YQCQV5IS +3SEL07K3 +N288TJEL +VFCC0K2B +P8J9ZG5N +TDY65TGQ +JGR14QYY +012QG5J7 +JJD09E6O +1V6QXSND +MITES6G1 +V3C09ACE +2I84H8D9 +ZR9VA4OR +0J8VTCFY +XCBB9HWF +GS1NL6UB +70GO84CW +T1V48W99 +H4L9PCSG +0XSXAQT0 +D7J98ZSY +IKB10A5S +VQGZ5L4C +WJU2IMH8 +0XTNP65W +XQ671726 +LCIMNOH4 +R36ZXNGT +NU04BJOR +P2IJVBEP +JT1SCFZC +69Y6VQ7A +V5D5G6X2 +9GCKGK6M +UB5G5OHX +BGA0KKP6 +6V5TSJ58 +570VS9I6 +E03UJCOB +J2UCMXGT +8J7XBKU4 +MIJJDS35 +TCJFR4OU +AT1M8RXF +6G7PZVXU +9601SLTJ +G585YO17 +FOO71FXH +N4TM1W45 +D0UZGV1C +3X44O8AC +A89NHFE7 +9PRH0BYM +JFC3X18B +HXBZY6DL +2S3LZAPT +HX572VEB +WX0W7R99 +VKYBQBRI +ZDU36TBC +4L4EU1Y5 +TEBGN81Y +BDJFCK9I +SXA2D5EZ +FCG7VJN0 +MWIEFAM2 +E8TO1EY5 +1JRU6F31 +7N70U7N7 +CSIWDBUK +VQ8KR73I +ISHABRX5 +2TXRGDJ8 +Z90BQBSG +WSBJ4X3L +34EKGUR8 +A63CJ0F0 +4QJ9TB49 +PTVLGUF3 +A9B0PY0K +ZVP29VCK +K3H9331J +21HG6P11 +W6GZAVVD +I5HGBM6P +8RTCLY8R +5A860OIH +VLN2ZB37 +7DRYVPHU +FWI0HDAD +6P9RGQ8Z +7F9Q10FR +7HNH3EWP +PFHK18Z6 +5VS9PKLC +AZMBWSKW +XA30TWRJ +V6HI00G4 +A385GB8L +1UFF62RI +3TL6B4D4 +JYWHW6EY +JZ7X5R31 +FG51SGUY +Z7YTXLGF +J3AVBX3P +FNV19XYR +M3I2Q6EV +YY35QIOS +J2BMJVP3 +XW9HM2HB +1873CQQ8 +KGL61SL7 +TU41J8F4 +U1UCSYE5 +4L45MMQ9 +MFQLY7CB +MKFH50ZZ +LQ44DAW9 +8599GTIB +IGCT34SA +Q39B1TOJ +LANF45HH +72PJTWPK +F0XPL1ZO +5U7KOGZ9 +6M1MMXZ1 +HVZQD6RB +H2D50E2W +WJEXAK1L +I4CBC1W5 +X9XRXBS3 +DBNDT60B +6C6QJW0V +H8J8ZW8D +64MUB2X0 +8R44E4P9 +R83AV775 +UBRSJBPF +JN36LDJW +3C2EEMIQ +HRE1S0SK +SH8QH70P +3BI5Q1ZF +1VAQQXA0 +QGY6Y7G6 +1QQ9FLV7 +AP1SNJP2 +G68VA57Y +N6UHT54O +7Q9BB5JL +B4PIP9CJ +BAS3O1FH +L6Q6YVSH +APRT16IN +K4205WGU +R00E9H6L +6MW091Y5 +ERCE510B +U8XKOESQ +1YAXZEFB +KESINTFI +03DUWJYK +VRFUU0ZR +F5E4B6FX +XSEBU1JQ +3E692DCL +SF5A49NX +7P73NY4S +FAI8YPYC +TDA6FHLT +1NIX4S8W +R549CDKZ +CHK907PO +5S8BU9CN +XJIK45B5 +PK1PKL3K +UDU54DY7 +G863ES4G +GNW10XUX +FEBRWV5F +88I6TN3R \ No newline at end of file diff --git a/sarikasama/0004/0004.py b/sarikasama/0004/0004.py new file mode 100644 index 00000000..864fbfd2 --- /dev/null +++ b/sarikasama/0004/0004.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +#count words in a textfile + +from pprint import pprint +import re + +def main(): + res = {} + with open('test','r') as f: + content = f.read() + tmp = re.split(r"[^a-zA-Z]",content) + for w in tmp: + if not w: + continue + w = w.lower() + if w not in res: + res[w] = 1 + else: + res[w] += 1 + pprint(res) + +if __name__ == "__main__": + main() diff --git a/sarikasama/0004/output b/sarikasama/0004/output new file mode 100644 index 00000000..2bd14c75 --- /dev/null +++ b/sarikasama/0004/output @@ -0,0 +1,107 @@ +{'a': 5, + 'about': 1, + 'age': 1, + 'all': 1, + 'always': 2, + 'an': 1, + 'and': 1, + 'arms': 1, + 'as': 2, + 'at': 4, + 'been': 1, + 'before': 1, + 'born': 1, + 'but': 1, + 'by': 1, + 'can': 1, + 'child': 1, + 'count': 1, + 'did': 3, + 'dolly': 1, + 'dolores': 1, + 'dotted': 1, + 'down': 1, + 'envied': 1, + 'exhibit': 1, + 'fact': 1, + 'fancy': 1, + 'feet': 1, + 'fire': 1, + 'for': 1, + 'four': 1, + 'gentlemen': 1, + 'girl': 1, + 'had': 1, + 'have': 2, + 'i': 1, + 'in': 6, + 'indeed': 1, + 'initial': 1, + 'is': 1, + 'jury': 1, + 'ladies': 1, + 'lee': 2, + 'life': 1, + 'light': 1, + 'line': 1, + 'lo': 4, + 'loins': 1, + 'lola': 1, + 'lolita': 4, + 'look': 1, + 'loved': 1, + 'many': 1, + 'might': 1, + 'misinformed': 1, + 'morning': 1, + 'murderer': 1, + 'my': 6, + 'no': 1, + 'noble': 1, + 'not': 1, + 'number': 1, + 'of': 7, + 'oh': 1, + 'on': 3, + 'one': 3, + 'palate': 1, + 'plain': 1, + 'point': 1, + 'precursor': 1, + 'princedom': 1, + 'prose': 1, + 'school': 1, + 'sea': 1, + 'seraphs': 2, + 'she': 8, + 'simple': 1, + 'sin': 1, + 'slacks': 1, + 'sock': 1, + 'soul': 1, + 'standing': 1, + 'steps': 1, + 'style': 1, + 'summer': 2, + 'ta': 2, + 'taking': 1, + 'tangle': 1, + 'tap': 1, + 'teeth': 1, + 'ten': 1, + 'that': 1, + 'the': 10, + 'there': 1, + 'this': 1, + 'thorns': 1, + 'three': 2, + 'tip': 1, + 'to': 1, + 'tongue': 1, + 'trip': 1, + 'was': 7, + 'what': 1, + 'when': 1, + 'winged': 1, + 'years': 1, + 'you': 1} diff --git a/sarikasama/0004/test b/sarikasama/0004/test new file mode 100644 index 00000000..c18efc11 --- /dev/null +++ b/sarikasama/0004/test @@ -0,0 +1 @@ +Lolita, light of my life, fire of my loins. My sin, my soul. Lo-lee-ta: the tip of the tongue taking a trip of three steps down the palate to tap, at three, on the teeth. Lo. Lee. Ta. She was Lo, plain Lo, in the morning, standing four feet ten in one sock. She was Lola in slacks. She was Dolly at school. She was Dolores on the dotted line. But in my arms she was always Lolita. Did she have a precursor? She did, indeed she did. In point of fact, there might have been no Lolita at all had I not loved, one summer, an initial girl-child. In a princedom by the sea. Oh when? About as many years before Lolita was born as my age was that summer. You can always count on a murderer for a fancy prose style. Ladies and gentlemen of the jury, exhibit number one is what the seraphs, the misinformed, simple, noble-winged seraphs, envied. Look at this tangle of thorns. \ No newline at end of file diff --git a/sarikasama/0005/0005.py b/sarikasama/0005/0005.py new file mode 100644 index 00000000..8d275531 --- /dev/null +++ b/sarikasama/0005/0005.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +#change the resolution of pics in dir 'test' to at most 1136*640 for iphone5 +from PIL import Image +import os + +def main(): + os.chdir('test') + for root,dirs,files in os.walk(os.getcwd()): + for f in files: + im = Image.open(f) + if im.size[0] > 1136: + im.resize([1136,im.size[1]]) + if im.size[1] > 640: + im.resize([im.size[0],640]) + im.save('test_'+im.filename) + +if __name__=='__main__': + main() diff --git a/sarikasama/0006/0006.py b/sarikasama/0006/0006.py new file mode 100644 index 00000000..66823951 --- /dev/null +++ b/sarikasama/0006/0006.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +#get the most important word in the text + +import os, re +from pprint import pprint + +def most_important_word(f): + #get the count of words in the text + res = {} + content = f.read() + tmp = re.split(r"[^a-zA-Z]",content) + for w in tmp: + if not w: + continue + w = w.lower() + if w not in res: + res[w] = 1 + else: + res[w] += 1 + + #get the word of most importance + res['']=0 + max = '' + for i in res: + if res[i] > res[max]: + max = i + return max + +def main(): + res = {} + os.chdir('test') + for root,dirs,files in os.walk(os.getcwd()): + for file in files: + with open(file,'r') as f: + res[f.name]=most_important_word(f) + return res + +if __name__ == "__main__": + res = main() + pprint(res) diff --git a/sarikasama/0007/0007.py b/sarikasama/0007/0007.py new file mode 100644 index 00000000..ba8c417a --- /dev/null +++ b/sarikasama/0007/0007.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +#count lines of code in directory 'test' + +import os + +def count_lines(f): + #get the count of lines in the code + total = note = empty_row = 0 + line = f.readline() + while line != '': + if line[0] == '#': + note += 1 + total += 1 + elif line == '\n': + empty_row += 1 + total += 1 + else: + total += 1 + line = f.readline() + return total, note, empty_row + +def main(): + res = {} + os.chdir('test') + for root,dirs,files in os.walk(os.getcwd()): + for file in files: + with open(file,'r') as f: + res[f.name]=count_lines(f) + return res + +if __name__=="__main__": + res = main() + for i in res: + print(i+"\ntotal:"+str(res[i][0])+"\nnote:"+str(res[i][1])+"\nempty_line:"+str(res[i][2])) + print("\n") diff --git a/sarikasama/0008/0008.py b/sarikasama/0008/0008.py new file mode 100644 index 00000000..1f50c6dc --- /dev/null +++ b/sarikasama/0008/0008.py @@ -0,0 +1,26 @@ +#!/usr/bin/env pyhton3 +#get the text in a html file + +import re, urllib.request + +def get_text(url): + content = url.read() + try: + content = content.decode('utf-8') + except: + content = content.decode('gbk') + + content = re.sub(r'', '', content, flags = re.DOTALL) + content = re.sub(r'', '', content, flags = re.DOTALL) + content = re.sub(r'', '', content, flags = re.DOTALL) + content = re.sub(r'<[^>]*>', '', content) + content = re.sub(r'\n', '', content) + + print(content) + +def main(): + with urllib.request.urlopen('http://thwiki.cc/') as url: + get_text(url) + +if __name__ == '__main__': + main() diff --git a/sarikasama/0009/0009.py b/sarikasama/0009/0009.py new file mode 100644 index 00000000..454f1460 --- /dev/null +++ b/sarikasama/0009/0009.py @@ -0,0 +1,18 @@ +#!/usr/bin/env pyhton3 +#get the link in a html file + +import re, urllib.request +from lxml.html import parse + +def get_link(url): + dom = parse(url).getroot() + links = dom.xpath('//a') + for link in links: + link = link + try: + print(link.attrib['href']) + except: + pass + +if __name__ == '__main__': + get_link('http://thwiki.cc/') diff --git a/sarikasama/0010/0010.py b/sarikasama/0010/0010.py new file mode 100644 index 00000000..01125d7b --- /dev/null +++ b/sarikasama/0010/0010.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +#generate random verification codes in letters + +import random, string +from PIL import Image, ImageDraw, ImageFont, ImageFilter + +def gene_verification_code_pic(): + #initialize + im = Image.new('RGB', (200, 50), (255, 255, 255)) + font = ImageFont.truetype('arial.ttf', 30) + draw = ImageDraw.Draw(im) + + #init background with light color + for i in range(200): + for j in range(50): + draw.point((i, j), fill=(random.randint(128,255),random.randint(128,255),random.randint(128,255))) + + #init letters with deep color + for t in range(4): + draw.text((50*t+10, 10), random.choice(string.ascii_letters), font=font, fill=(random.randint(0,127), random.randint(0,127), random.randint(0,127))) + + #make the pic blurred + im = im.filter(ImageFilter.BLUR) + return im + +if __name__ == '__main__': + im = gene_verification_code_pic() + im.show() + im.save('res.jpg') diff --git a/sarikasama/0010/arial.ttf b/sarikasama/0010/arial.ttf new file mode 100644 index 00000000..7ff88f22 Binary files /dev/null and b/sarikasama/0010/arial.ttf differ diff --git a/sarikasama/0010/res.jpg b/sarikasama/0010/res.jpg new file mode 100644 index 00000000..e079b88c Binary files /dev/null and b/sarikasama/0010/res.jpg differ diff --git a/sarikasama/0011/0011.py b/sarikasama/0011/0011.py new file mode 100644 index 00000000..c105c831 --- /dev/null +++ b/sarikasama/0011/0011.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +#filter sensitive words in user's input + +def filter_sensitive_words(input_word): + s_words = [] + with open('filtered_words','r') as f: + line = f.readline() + while line != '': + s_words.append(line.strip()) + line = f.readline() + if input_word in s_words: + print("Freedom") + else: + print("Human Rights") + +if __name__ == '__main__': + while True: + input_word = input('--> ') + filter_sensitive_words(input_word) diff --git a/sarikasama/0011/filtered_words b/sarikasama/0011/filtered_words new file mode 100644 index 00000000..f0da7ccc --- /dev/null +++ b/sarikasama/0011/filtered_words @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex + jiangge diff --git a/sarikasama/0012/0012.py b/sarikasama/0012/0012.py new file mode 100644 index 00000000..df193d81 --- /dev/null +++ b/sarikasama/0012/0012.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +#filter sensitive words in user's input + +def replace_sensitive_words(input_word): + s_words = [] + with open('filtered_words','r') as f: + line = f.readline() + while line != '': + s_words.append(line.strip()) + line = f.readline() + for word in s_words: + if word in input_word: + input_word = input_word.replace(word, "**") + print(input_word) + +if __name__ == '__main__': + while True: + input_word = input('--> ') + replace_sensitive_words(input_word) diff --git a/sarikasama/0012/filtered_words b/sarikasama/0012/filtered_words new file mode 100644 index 00000000..f0da7ccc --- /dev/null +++ b/sarikasama/0012/filtered_words @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex + jiangge diff --git a/sarikasama/0013/0013.py b/sarikasama/0013/0013.py new file mode 100644 index 00000000..76ee61a1 --- /dev/null +++ b/sarikasama/0013/0013.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +#get all pic in a url + +from urllib.request import urlopen +from bs4 import BeautifulSoup + +def img_crawler(url): + content = url.read() + bs_object = BeautifulSoup(content) + bs_imgs = bs_object.find_all('img', pic_type='0') + + for img in bs_imgs: + img_url = img['src'] + try: + img_content = urlopen(img_url).read() + except: + pass + print("Downloading... "+img_url) + img_f = open(img_url.split('/')[-1], 'wb') + img_f.write(img_content) + img_f.close() + print("Complete!") + +if __name__ == '__main__': + url = urlopen('http://tieba.baidu.com/p/2166231880') + img_crawler(url) diff --git a/sarikasama/0014/0014.py b/sarikasama/0014/0014.py new file mode 100644 index 00000000..6d082f91 --- /dev/null +++ b/sarikasama/0014/0014.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +#from json to xlsx + +import xlsxwriter, json + +def json2xls(): + wb = xlsxwriter.Workbook('student.xls') + ws = wb.add_worksheet("student") + + with open('./student') as f: + data = json.load(f) + for i in range(len(data)): + ws.write(i, 0, i+1) + json_data = data[str(i+1)] + for j in range(len(json_data)): + ws.write(i, j+1, json_data[j]) + wb.close() + +if __name__=='__main__': + json2xls() diff --git a/sarikasama/0014/student b/sarikasama/0014/student new file mode 100644 index 00000000..1c4ffe6d --- /dev/null +++ b/sarikasama/0014/student @@ -0,0 +1,5 @@ +{ + "1":["张三",150,120,100], + "2":["李四",90,99,95], + "3":["王五",60,66,68] +} diff --git a/sarikasama/0014/student.xls b/sarikasama/0014/student.xls new file mode 100644 index 00000000..8575f90c Binary files /dev/null and b/sarikasama/0014/student.xls differ diff --git a/sarikasama/0015/0015.py b/sarikasama/0015/0015.py new file mode 100644 index 00000000..64371333 --- /dev/null +++ b/sarikasama/0015/0015.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +#from json to xlsx + +import xlsxwriter, json + +def json2xls(): + wb = xlsxwriter.Workbook('city.xls') + ws = wb.add_worksheet("city") + + with open('./city') as f: + data = json.load(f) + for i in range(len(data)): + ws.write(i, 0, i+1) + ws.write(i, 1, data[str(i+1)]) + wb.close() + +if __name__=='__main__': + json2xls() diff --git a/sarikasama/0015/city b/sarikasama/0015/city new file mode 100644 index 00000000..b5a6874d --- /dev/null +++ b/sarikasama/0015/city @@ -0,0 +1,5 @@ +{ + "1" : "上海", + "2" : "北京", + "3" : "成都" +} diff --git a/sarikasama/0015/city.xls b/sarikasama/0015/city.xls new file mode 100644 index 00000000..c4d1e10b Binary files /dev/null and b/sarikasama/0015/city.xls differ diff --git a/sarikasama/0016/0016.py b/sarikasama/0016/0016.py new file mode 100644 index 00000000..269e8997 --- /dev/null +++ b/sarikasama/0016/0016.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +#from json to xlsx + +import xlsxwriter, json + +def json2xls(): + wb = xlsxwriter.Workbook('numbers.xls') + ws = wb.add_worksheet("numbers") + + with open('./numbers') as f: + data = json.load(f) + for i in range(len(data)): + for j in range(len(data[i])): + ws.write(i, j, data[i][j]) + wb.close() + +if __name__=='__main__': + json2xls() diff --git a/sarikasama/0016/numbers b/sarikasama/0016/numbers new file mode 100644 index 00000000..dbe6d92f --- /dev/null +++ b/sarikasama/0016/numbers @@ -0,0 +1,5 @@ +[ + [1, 82, 65535], + [20, 90, 13], + [26, 809, 1024] +] diff --git a/sarikasama/0016/numbers.xls b/sarikasama/0016/numbers.xls new file mode 100644 index 00000000..e5ea32d8 Binary files /dev/null and b/sarikasama/0016/numbers.xls differ diff --git a/sarikasama/0017/0017.py b/sarikasama/0017/0017.py new file mode 100644 index 00000000..d42a7595 --- /dev/null +++ b/sarikasama/0017/0017.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +#convert from xls to xml + +import xlrd, json +from lxml import etree +from collections import OrderedDict + +def xls2xml(xls_name): + with xlrd.open_workbook(xls_name) as wb: + ws = wb.sheet_by_index(0) + table = OrderedDict() + for i in range(ws.nrows): + key = int(ws.row_values(i)[0]) + value = str(ws.row_values(i)[1:]) + table[key] = value + + with open("student.xml", 'w') as f: + root = etree.Element("root") + e_root = etree.ElementTree(root) + e_students = etree.SubElement(root, 'students') + e_students.text = '\n'+str(json.dumps(table, indent=4, ensure_ascii=False))+'\n' + e_students.append(etree.Comment('\n 学生信息表\n "id" : [名字,数学,语文,英语]\n')) + f.write(''+etree.tounicode(e_root.getroot())) + +if __name__=="__main__": + xls2xml('student.xls') diff --git a/sarikasama/0017/student.xls b/sarikasama/0017/student.xls new file mode 100644 index 00000000..8575f90c Binary files /dev/null and b/sarikasama/0017/student.xls differ diff --git a/sarikasama/0017/student.xml b/sarikasama/0017/student.xml new file mode 100644 index 00000000..bb68d95c --- /dev/null +++ b/sarikasama/0017/student.xml @@ -0,0 +1,10 @@ + +{ + "1": "['张三', 150.0, 120.0, 100.0]", + "2": "['李四', 90.0, 99.0, 95.0]", + "3": "['王五', 60.0, 66.0, 68.0]" +} + diff --git a/sarikasama/0018/0018.py b/sarikasama/0018/0018.py new file mode 100644 index 00000000..277f1ba9 --- /dev/null +++ b/sarikasama/0018/0018.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +#convert from xls to xml + +import xlrd, json +from lxml import etree +from collections import OrderedDict + +def xls2xml(xls_name): + with xlrd.open_workbook(xls_name) as wb: + ws = wb.sheet_by_index(0) + table = OrderedDict() + for i in range(ws.nrows): + key = int(ws.row_values(i)[0]) + value = ws.row_values(i)[1] + table[key] = value + + with open("city.xml", 'w') as f: + root = etree.Element("root") + e_root = etree.ElementTree(root) + e_citys = etree.SubElement(root, 'citys') + e_citys.text = '\n'+str(json.dumps(table, indent=4, ensure_ascii=False))+'\n' + e_citys.append(etree.Comment('\n 城市信息\n')) + f.write(''+etree.tounicode(e_root.getroot())) + +if __name__=="__main__": + xls2xml('city.xls') diff --git a/sarikasama/0018/city.xls b/sarikasama/0018/city.xls new file mode 100644 index 00000000..c4d1e10b Binary files /dev/null and b/sarikasama/0018/city.xls differ diff --git a/sarikasama/0018/city.xml b/sarikasama/0018/city.xml new file mode 100644 index 00000000..0db0e9cb --- /dev/null +++ b/sarikasama/0018/city.xml @@ -0,0 +1,9 @@ + +{ + "1": "上海", + "2": "北京", + "3": "成都" +} + diff --git a/sarikasama/0019/0019.py b/sarikasama/0019/0019.py new file mode 100644 index 00000000..1f10af93 --- /dev/null +++ b/sarikasama/0019/0019.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +#convert from xls to xml + +import xlrd, json +from lxml import etree + +def xls2xml(xls_name): + with xlrd.open_workbook(xls_name) as wb: + ws = wb.sheet_by_index(0) + table = [] + for i in range(ws.nrows): + table.append(ws.row_values(i)) + + with open("numbers.xml", 'w') as f: + root = etree.Element("root") + e_root = etree.ElementTree(root) + e_numbers = etree.SubElement(root, 'numbers') + e_numbers.text = '\n'+str(json.dumps(table, indent=4))+'\n' + e_numbers.append(etree.Comment('\n 数字信息\n')) + f.write(''+etree.tounicode(e_root.getroot())) + +if __name__=="__main__": + xls2xml('numbers.xls') diff --git a/sarikasama/0019/numbers.xls b/sarikasama/0019/numbers.xls new file mode 100644 index 00000000..e5ea32d8 Binary files /dev/null and b/sarikasama/0019/numbers.xls differ diff --git a/sarikasama/0019/numbers.xml b/sarikasama/0019/numbers.xml new file mode 100644 index 00000000..951bb1d1 --- /dev/null +++ b/sarikasama/0019/numbers.xml @@ -0,0 +1,21 @@ + +[ + [ + 1.0, + 82.0, + 65535.0 + ], + [ + 20.0, + 90.0, + 13.0 + ], + [ + 26.0, + 809.0, + 1024.0 + ] +] + diff --git a/sarikasama/0021/0021.py b/sarikasama/0021/0021.py new file mode 100644 index 00000000..648a830f --- /dev/null +++ b/sarikasama/0021/0021.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import hmac, hashlib, os + +def encrypt_passwd(password, salt=None): + if salt is None: + salt = str(os.urandom(8)).encode('utf-8') + if isinstance(password, str): + password = password.encode('utf-8') + + result = password + for i in range(10): + result = hmac.HMAC(result, salt, hashlib.sha256).digest() + return salt+result + +if __name__=="__main__": + e_passwd = encrypt_passwd(b'hello') + print(e_passwd) diff --git a/sarikasama/0022/0022.py b/sarikasama/0022/0022.py new file mode 100644 index 00000000..269a1af9 --- /dev/null +++ b/sarikasama/0022/0022.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +#change the resolution of pics in dir 'test' to at most 1136*640 for iphone5, 1334*750 for iphone6, or 1920*1080 for iphone6 Plus. +from PIL import Image +import os + +def main(): + os.chdir('test') + flag = input("Which shen do you use?\n1:iphone5\n2:iphone6\n3:iphone Plus") + for root,dirs,files in os.walk(os.getcwd()): + for f in files: + im = Image.open(f) + if flag == 1: + if im.size[0] > 1136: + im.resize([1136,im.size[1]]) + if im.size[1] > 640: + im.resize([im.size[0],640]) + elif flag == 2: + if im.size[0] > 1334: + im.resize([1136,im.size[1]]) + if im.size[1] > 750: + im.resize([im.size[0],750]) + elif flag == 3: + if im.size[0] > 1920: + im.resize([1136,im.size[1]]) + if im.size[1] > 1080: + im.resize([im.size[0],1080]) + else: + print("Input fault.") + im.save('test_'+im.filename) + +if __name__=='__main__': + main() diff --git a/sarikasama/README.md b/sarikasama/README.md new file mode 100644 index 00000000..3dc0eb68 --- /dev/null +++ b/sarikasama/README.md @@ -0,0 +1,2 @@ +# My version of show-me-the-code +No environment for debuging 0002, 0003 and 0020, so i ignored them