diff --git a/xyjxyf/0000/0000.jpg b/xyjxyf/0000/0000.jpg new file mode 100644 index 00000000..1f4d8579 Binary files /dev/null and b/xyjxyf/0000/0000.jpg differ diff --git a/xyjxyf/0004/0004.txt b/xyjxyf/0004/0004.txt new file mode 100644 index 00000000..cf950a09 --- /dev/null +++ b/xyjxyf/0004/0004.txt @@ -0,0 +1,4 @@ + +Henry was a pen name used by an American writer of short stories. + +His real name was William Sydney Porter. diff --git a/xyjxyf/0005/1.jpg b/xyjxyf/0005/1.jpg new file mode 100644 index 00000000..1f4d8579 Binary files /dev/null and b/xyjxyf/0005/1.jpg differ diff --git a/xyjxyf/0005/2.jpg b/xyjxyf/0005/2.jpg new file mode 100644 index 00000000..177dfc8e Binary files /dev/null and b/xyjxyf/0005/2.jpg differ diff --git a/xyjxyf/0006/00061.txt b/xyjxyf/0006/00061.txt new file mode 100644 index 00000000..48ba0476 --- /dev/null +++ b/xyjxyf/0006/00061.txt @@ -0,0 +1,12 @@ + +Henry was a pen name used by an American writer of short stories. + +His + +His + +His + +His + +Henry was a pen name used by an American writer of short stories. diff --git a/xyjxyf/0006/00062.txt b/xyjxyf/0006/00062.txt new file mode 100644 index 00000000..af894a51 --- /dev/null +++ b/xyjxyf/0006/00062.txt @@ -0,0 +1,7 @@ + +Henry was a pen name used by an American writer of short stories. + +His real name was William Sydney Porter. + +pen pen +pen pen pen pen pen pen diff --git a/xyjxyf/0007/stringer.py b/xyjxyf/0007/stringer.py new file mode 100644 index 00000000..56d9d3b3 --- /dev/null +++ b/xyjxyf/0007/stringer.py @@ -0,0 +1,29 @@ +# coding = utf-8 + +import random, string + +/* +sdfhjdsf +hgjh +*/ +# def Unicode(): +# val = random.randint(0x4E00, 0x9FBF) +# return unichr(val) +# +# # 随机汉字 +# def GB2312(): +# head = random.randint(0xB0, 0xCF) +# body = random.randint(0xA, 0xF) +# tail = random.randint(0, 0xF) +# val = ( head << 8 ) | (body << 4) | tail +# str = "%x" % val +# return str.decode('hex').decode('gb2312') + +# 随机字母 +def rand_char(): + return chr(random.randint(65, 90)) + +# 随机字母或者数字 +def rand_choice(): + str = 'abcdefghigklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM1234567890' + return random.choice(str) diff --git a/xyjxyf/0011/0011.txt b/xyjxyf/0011/0011.txt new file mode 100644 index 00000000..693f4665 --- /dev/null +++ b/xyjxyf/0011/0011.txt @@ -0,0 +1,11 @@ +北京 +程序员 +公务员 +领导 +牛比 +牛逼 +你娘 +你妈 +love +sex +jiangge diff --git a/xyjxyf/0014/student.txt b/xyjxyf/0014/student.txt new file mode 100644 index 00000000..ea9b1593 --- /dev/null +++ b/xyjxyf/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/xyjxyf/0014/student.xls b/xyjxyf/0014/student.xls new file mode 100644 index 00000000..b80d5719 Binary files /dev/null and b/xyjxyf/0014/student.xls differ diff --git a/xyjxyf/0015/city.txt b/xyjxyf/0015/city.txt new file mode 100644 index 00000000..312f5c19 --- /dev/null +++ b/xyjxyf/0015/city.txt @@ -0,0 +1,5 @@ +{ + "1" : "上海", + "2" : "北京", + "3" : "成都" +} \ No newline at end of file diff --git a/xyjxyf/0015/city.xls b/xyjxyf/0015/city.xls new file mode 100644 index 00000000..b020435b Binary files /dev/null and b/xyjxyf/0015/city.xls differ diff --git a/xyjxyf/0016/numbers.txt b/xyjxyf/0016/numbers.txt new file mode 100644 index 00000000..c43c0378 --- /dev/null +++ b/xyjxyf/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/xyjxyf/0016/numbers.xls b/xyjxyf/0016/numbers.xls new file mode 100644 index 00000000..091d6ef7 Binary files /dev/null and b/xyjxyf/0016/numbers.xls differ diff --git a/xyjxyf/0017/student.xls b/xyjxyf/0017/student.xls new file mode 100644 index 00000000..b80d5719 Binary files /dev/null and b/xyjxyf/0017/student.xls differ diff --git a/xyjxyf/0017/student.xml b/xyjxyf/0017/student.xml new file mode 100644 index 00000000..55abaffe --- /dev/null +++ b/xyjxyf/0017/student.xml @@ -0,0 +1,14 @@ + + + + + { + 3: ['王五', 60.0, 66.0, 68.0] + 2: ['李四', 90.0, 99.0, 95.0] + 1: ['张三', 150.0, 120.0, 100.0] + } + + diff --git a/xyjxyf/0018/city.xls b/xyjxyf/0018/city.xls new file mode 100644 index 00000000..b020435b Binary files /dev/null and b/xyjxyf/0018/city.xls differ diff --git a/xyjxyf/0018/city.xml b/xyjxyf/0018/city.xml new file mode 100644 index 00000000..00af5e13 --- /dev/null +++ b/xyjxyf/0018/city.xml @@ -0,0 +1,13 @@ + + + + { + 3: ['成都'] + 2: ['北京'] + 1: ['上海'] + } + + + diff --git a/xyjxyf/0019/numbers.xls b/xyjxyf/0019/numbers.xls new file mode 100644 index 00000000..091d6ef7 Binary files /dev/null and b/xyjxyf/0019/numbers.xls differ diff --git a/xyjxyf/0019/numbers.xml b/xyjxyf/0019/numbers.xml new file mode 100644 index 00000000..8a6fcfd0 --- /dev/null +++ b/xyjxyf/0019/numbers.xml @@ -0,0 +1,13 @@ + + + + [ + [1.0, 82.0, 65535.0], + [20.0, 90.0, 13.0], + [26.0, 809.0, 1024.0], + ] + + + diff --git a/xyjxyf/0020/0020.xls b/xyjxyf/0020/0020.xls new file mode 100644 index 00000000..f0d493e5 Binary files /dev/null and b/xyjxyf/0020/0020.xls differ diff --git a/xyjxyf/0025/voice_open_browser.py b/xyjxyf/0025/voice_open_browser.py new file mode 100644 index 00000000..f4127c29 --- /dev/null +++ b/xyjxyf/0025/voice_open_browser.py @@ -0,0 +1,78 @@ +# encoding = utf-8 + +# use pyAudio +# brew install portaudio +# pip install pyaudio + +import wave, pyaudio +from datetime import datetime +from tools import dxbaiduaudio +import webbrowser + +CHUNK = 1024 +FORMAT = pyaudio.paInt16 +RATE = 8000 +CHANNELS = 1 +RECORD_SECONDS = 5 + +def record_wave(to_dir=None): + if to_dir is None: + to_dir = "./" + + pa = pyaudio.PyAudio() + stream = pa.open(format = FORMAT, + channels = CHANNELS, + rate = RATE, + input = True, + frames_per_buffer = CHUNK) + + print("* recording") + + save_buffer = [] + for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): + audio_data = stream.read(CHUNK) + save_buffer.append(audio_data) + + print("* done recording") + + # stop + stream.stop_stream() + stream.close() + pa.terminate() + + # wav path + file_name = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+".wav" + if to_dir.endswith('/'): + file_path = to_dir + file_name + else: + file_path = to_dir + "/" + file_name + + # save file + wf = wave.open(file_path, 'wb') + wf.setnchannels(CHANNELS) + wf.setsampwidth(pa.get_sample_size(FORMAT)) + wf.setframerate(RATE) + # join 前的类型 + wf.writeframes(b''.join(save_buffer)) + wf.close() + + return file_path + +def browser_open_text(text): + if text is None: + return + + url = "http://www.baidu.com" + if text.startswith("谷歌") or text.startswith("google"): + url = "http://www.google.com" + elif text.startswith("必应") or text.startswith("bing"): + url = "http://cn.bing.com" + + webbrowser.open_new_tab(url) + +if __name__ == "__main__": + to_dir = "./" + file_path = record_wave(to_dir) + + text = dxbaiduaudio.wav_to_text(file_path) + browser_open_text(text) diff --git a/xyjxyf/README.md b/xyjxyf/README.md new file mode 100644 index 00000000..f579e43b --- /dev/null +++ b/xyjxyf/README.md @@ -0,0 +1,55 @@ +xyjxyf Notes +====== +## Mac上安装Redis + +1. 下载源文件 + +[redis-3.0.7.rar.gz](http://download.redis.io/releases/) + +2. 安装 + +``` +$ tar -zxvf redis-3.0.6.tar.gz +$ cd redis-3.0.7 +$ make test +$ make install +``` + +3. 配置 + +``` +$ vi ./redis.conf + + # 设置为后台运行 +daemonize yes + + # 设置pid文件位置 +pidfile /usr/local/redis/redis.pid + + # 连接超时时间设置为120秒 +timeout 120 + + # 设置日志级别 +loglevel debug + + # 设置日志文件位置 +logfile "/usr/local/redis/log/6379.log" + + # 数据文件所在目录 +dir /usr/local/redis/data/6379 +``` + +4. 启动redis服务器 + +``` +$ ./redis-server +``` + +5. 安装Python的Redis模块 + +我的mac安装了python双版本,用python3 + + +``` +$ pip3 install redis +``` \ No newline at end of file diff --git a/xyjxyf/show_me_the_code.py b/xyjxyf/show_me_the_code.py new file mode 100644 index 00000000..ff3a36d1 --- /dev/null +++ b/xyjxyf/show_me_the_code.py @@ -0,0 +1,622 @@ +# coding = utf-8 + +from tools import imager +from PIL import ImageFont + + +# 第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字 +def add_num(image_path): + im = imager.open_image(image_path) + if im is not None: + font = ImageFont.truetype('Arial.ttf', 20) + w, h = im.size + point = (w - 10, 0) + imager.draw_text(im, "8", point, font) + im.show() + + +# 第 0001 题:使用 Python 生成 200 个激活码(或者优惠券) +import uuid + +def create_activation_code(num=200): + codes = [] + for i in range(num): + code = str(uuid.uuid1()) + code = code.replace('-', '') + codes.append(code) + + return codes + + +# 第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。 +import pymysql + +def save_activation_code_to_mysql(): + conn = pymysql.connect(host='localhost', user='root', charset='UTF8') + cur = conn.cursor() + cur.execute("CREATE DATABASE IF NOT EXISTS code_mysql") + cur.execute("USE code_mysql") + cur.execute("CREATE TABLE IF NOT EXISTS codes (id INT, code VARCHAR(255))") + + codes = create_activation_code(200) + for code in codes: + cur.execute("INSERT INTO codes (code) values(%s)", [code]) + + conn.commit() + + cur.execute("SELETE * FROM codes") + data = cur.fetchall() + print("data:%s" % data) + + cur.close() + conn.close() + + +# 第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。 +import redis + +def save_activation_code_to_redis(): + re = redis.Redis(host='127.0.0.1', port=6379, db=0) + + codes = create_activation_code(200) + for code in codes: + re.lpop('codes', code) + + print("data:%s" % re.get('codes')) + + +# 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。 +import re + +def number_of_words(file_path=None): + num = 0 + + if file_path is None: + return num + + file = open(file_path, 'r') + content = file.read() + content = " " + content + pattern = re.compile(u'\s+\w+') + match = pattern.findall(content) + num = len(match) + + return num + + +# 第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。 +def reset_images_size(dir_path=None): + if dir_path is None: + return + + for root, dirs, files in os.walk(dir_path): + for path in files: + if path.startswith("."): + continue + + file_path = os.path.join(root, path) + image = imager.open_image(file_path) + if image is not None: + new_image = imager.reset_image_size(image, 640, 1136) + imager.save(new_image, file_path) + + +# 第 0006 题:你有一个目录,放了你一个月的日记,都是 txt, +# 为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。 +# 思路:哪个词出现的最多,哪个词就是最重要的词 +import operator + +def get_most_important_word(dir_path=None): + if dir_path is None: + return None + + for root, dirs, files in os.walk(dir_path): + for path in files: + if not path.endswith("txt"): + continue + + file_path = os.path.join(root, path) + content = open(file_path, 'r').read().lower() + + words = content.split() + word_dic = {} + for word in words: + if word in word_dic.keys(): + word_dic[word] += 1 + else: + word_dic[word] = 1 + + if len(word_dic): + word_list = sorted(word_dic.items(), key=lambda x:x[1], reverse=True) + print("%s : %s -- %d" % (path, word_list[0][0], word_list[0][1])) + + + +# 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来 +import os + +# 注释://, #, /* */, """ """, ''' ''' +def lines_of_codes(dir_path=None): + code_num = 0 + note_num = 0 + blank_line_num = 0 + if dir_path is None: + return code_num, note_num, blank_line_num + + for root, dirs, files in os.walk(dir_path): + mut_note = None + for path in files: + if path.startswith("."): + continue + + file_path = os.path.join(root, path) + for count, line in enumerate(open(file_path, "rU")): + + # 判断是否是空行 + if not line.split(): + blank_line_num += 1 + continue + + # 判断是否多行注释 + if mut_note is not None: + note_num += 1 + match_note = re.match("\*/|\"\"\"|\'\'\'", line) + if match_note is not None: + mut_note = None + match_note = re.match("\/\*|\"\"\"|\'\'\'", line) + if match_note is not None: + mut_note = line[match_note.pos:(match_note.endpos - 1)] + + continue + else: + match_note = re.match("\/\*|\"\"\"|\'\'\'", line) + if match_note is not None: + note_num += 1 + mut_note = line[match_note.pos:(match_note.endpos - 1)] + continue + + # 判断单行注释 + match_note1 = re.match("\s*(#|//).*\n*", line) + if match_note1 is not None: + note_num += 1 + continue + + pass + + code_num += count + 1 + + return code_num, note_num, blank_line_num + +# 第 0008 题:一个HTML文件,找出里面的正文 +# 使用 pip install beautifulsoup4 +from bs4 import BeautifulSoup + +def get_html_context(url=None): + if url is None: + return None + + content = request.urlopen(url).read().decode("utf-8") + soup = BeautifulSoup(content) + [script.extract() for script in soup.find_all('script')] + [style.extract() for style in soup.find_all('style')] + + soup.prettify() + reg = re.compile("<[^>]*>") + ret_content = reg.sub('', soup.prettify()) + # print(ret_content) + + return ret_content + + +# 第 0009 题:一个HTML文件,找出里面的链接 +# 查找, 用 HTMLParser +from urllib import request +from tools import dxhtmlparser + +def get_html_links(url=None): + if url is None: + return None + + content = request.urlopen(url).read().decode("utf-8") + dxparser = dxhtmlparser.DXHTMLParser('a', 'href', url) + dxparser.feed(content) + + links = dxparser.getrets() + + return links + +# 第 0010 题:使用 Python 生成字母验证码图片 +def create_verification_code(): + im, str = imager.verification_code() + im.show() + + +# 第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容, +# 北京, 程序员, 公务员, 领导, 牛比, 牛逼, 你娘, 你妈, love, sex, jiangge +# 当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。 + +def find_sensitive_words(sensitive_file=None, input_string=None): + if sensitive_file is None or input_string is None: + return None + + file = open(sensitive_file, "r") + sensitive_words = file.read().split() + + is_sensitive = False + for sensitive in sensitive_words: + if sensitive in input_string: + is_sensitive = True + print("Freedom") + + if not is_sensitive: + print("Human Rights") + + +# 第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样, +# 当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。 + +def replace_sensitive_words(sensitive_file=None, input_string=None): + if sensitive_file is None or input_string is None: + return None + + file = open(sensitive_file, "r") + sensitive_words = file.read().split() + + for sensitive in sensitive_words: + if sensitive in input_string: + replace_str = "*" * len(sensitive) + input_string = input_string.replace(sensitive, replace_str) + + print(input_string) + +# 第 0013 题: 用 Python 写一个爬图片的程序 +from tools import geturlimgs + +def get_url_imgs(url=None): + if url is None: + return None + + tmp = geturlimgs.geturlimgs() + tmp.get_imgs(url, "/Users/xieyajie/Desktop/Python/ShowMeCode/xyjxyf/0013/") + + + +# 第 0014 题: 纯文本文件 student.txt为学生信息, 写到 student.xls 文件中 +# 第 0015 题: 纯文本文件 city.txt为城市信息,写到 city.xls 文件中 +import json +import xlwt + +def dictxt_to_xls(file_path=None): + if file_path is None: + return + + file = open(file_path, 'r') + if file is None: + return + + content = json.loads(file.read()) + list_data = sorted(content.items(), key=lambda d:d[0]) + + (path, name)=os.path.split(file.name) + file_name = name.split('.')[0] + + wb = xlwt.Workbook() + ws = wb.add_sheet(file_name) + + row = 0 + for item in list_data: + col = 0 + ws.write(row, col, item[0]) + col += 1 + + value = item[1] + if type(value) == list: + for obj in value: + ws.write(row, col, obj) + col += 1 + else: + ws.write(row, col, value) + row += 1 + + save_path = path + "/" + file_name + ".xls" + wb.save(save_path) + + +# 第 0016 题: 纯文本文件 numbers.txt, 请将上述内容写到 numbers.xls 文件中 +def listtxt_to_xls(file_path=None): + if file_path is None: + return + + file = open(file_path) + if file is None: + return + + content = json.loads(file.read()) + content.sort(key=lambda x:x[0]) + + (path, name)=os.path.split(file.name) + file_name = name.split('.')[0] + wb = xlwt.Workbook() + ws = wb.add_sheet(file_name) + + for i in range(len(content)): + col = 0 + list = content[i] + for value in list: + ws.write(i, col, content[i][col]) + col += 1 + + save_path = path + "/" + file_name + ".xls" + wb.save(save_path) + +# pip3 install xlrd +import xlrd +def read_xls(file_path=None): + if file_path is None: + return None + + data_list = {} + wb = xlrd.open_workbook(file_path) + sheet_names = wb.sheet_names() + for name in sheet_names: + table = wb.sheet_by_name(name) + table_data = [] + for i in range(table.nrows): + row_data = table.row_values(i) + table_data.append(row_data) + + data_list[name] = table_data + + return data_list + + +# 第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中 +# 使用DOM +from tools import stringer +from xml.dom.minidom import Document + +def write_student_to_xml(dic=None, to_path=None): + if dic is None or to_path is None: + return None + + doc = Document() + root_node = doc.createElement("root") + doc.appendChild(root_node) + + stu_node = doc.createElement("students") + root_node.appendChild(stu_node) + + note_node = doc.createComment("\n\t学生信息表\n\t\"id\" : [名字, 数学, 语文, 英文]\n\t") + stu_node.appendChild(note_node) + + # data = json.dumps(dic, ensure_ascii=False, indent=1) + dic_node = doc.createTextNode(stringer.dict_to_json(dic, "\t\t")) + stu_node.appendChild(dic_node) + + file = open(to_path, "w") + file.write(doc.toprettyxml()) + # doc.writexml(file,' ',' ','\n','utf-8') + file.close() + +# 第 0018 题: 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中 +# 使用lxml +import codecs +from lxml import etree + +def write_city_to_xml(dic=None, to_path=None): + if dic is None or to_path is None: + return None + + root_node = etree.Element('root') + root_node.text = "\n\t" + + city_node = etree.SubElement(root_node, 'citys') + + comment_node = etree.Comment("\n城市信息\n") + comment_node.tail = "\n\t" + city_node.append(comment_node) + + city_node.text = "\n\t" + stringer.dict_to_json(dic, "\t") + u'\n' + city_node.tail = "\n" + + city_tree = etree.ElementTree(root_node) + city_tree.write(to_path, pretty_print=True, xml_declaration=True, encoding='utf-8') + # output = codecs.open(to_path, 'w', 'utf-8') + # output.write(etree.tounicode(city_tree.getroot())) + # output.close() + + +# 第 0019 题: 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中 +def write_numbers_to_xml(list=None, to_path=None): + if list is None or to_path is None: + return None + + root_node = etree.Element('root') + root_node.text = "\n\t" + + number_node = etree.SubElement(root_node, 'numbers') + + comment_node = etree.Comment("\n数字信息\n") + comment_node.tail = "\n\t" + number_node.append(comment_node) + + number_node.text = "\n\t" + stringer.list_to_json(list, "\t") + u'\n' + number_node.tail = "\n" + + number_tree = etree.ElementTree(root_node) + number_tree.write(to_path, pretty_print=True, xml_declaration=True, encoding='utf-8') + + +# 第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮, +# 查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。 +# 写代码,对每月通话时间做个统计 + +import datetime + +def statistics_month_time(): + dic = {} + wb = xlrd.open_workbook("./0020/0020.xls") + sheet = wb.sheets()[0] + row_count = sheet.nrows + + for i in range(1, sheet.nrows): + values = sheet.row_values(i) + ym_str = values[2][:6] + + time_str = values[3] + if '时' in time_str: + time_str = re.sub('时', '.', time_str) + if '分' in time_str: + time_str = re.sub('分', '.', time_str) + if '秒' in time_str: + time_str = re.sub('秒', '', time_str) + + tmp = time_str.split('.') + j = len(tmp) - 1 + sum = int(tmp[j]) + while j > -1: + sum = sum + (len(tmp) - 1 - j) * 60 * int(tmp[j]) + j = j - 1 + + if ym_str in dic: + dic[ym_str] = dic[ym_str] + int(sum) + else: + dic[ym_str] = int(sum) + + # i = i + 1 + + return dic + + +# 第 0021 题: 请使用 Python 对密码加密 +from hashlib import sha256 +from hmac import HMAC + +def encrypt_password(password, salt=None): + if salt is None: + salt = os.urandom(8) + + if isinstance(password, str): + password = password.encode('UTF-8') + + ret = password + for i in range(10): + ret = HMAC(ret, salt, sha256).digest() + + return salt + ret + + +# 第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用 + + +# 第 0023 题: 使用 Python 的 Web 框架,做一个 Web 版本 留言簿 应用 + + +# 第 0024 题: 使用 Python 的 Web 框架,做一个 Web 版本 TodoList 应用 + + +# 第 0025 题: 使用 Python 实现:对着电脑吼一声,自动打开浏览器中的默认网站 +# 在文件夹0025中实现 + + +if __name__ == "__main__": + # 0000 + # add_num(".0000/0000.jpg") + + # 0001 + # create_activation_code() + + # 0002 ????? + # save_activation_code_to_mysql() + + # 0003 + # save_activation_code_to_redis() + + # 0004 + # number_of_words("./0004/0004.txt") + + # 0005 + # reset_images_size("./0005") + + # 0006 + # get_most_important_word("./0006") + + # 0007 + # code, note, blank_line = lines_of_codes("./0007") + # print("代码行数:%i\n注释行数:%i\n空行行数:%i" % (code, note, blank_line)) + + # 0008 + # get_html_context("http://blog.bccn.net") + + # 0009 + # get_html_links("http://blog.bccn.net") + + # 0010 + # create_verification_code() + + # 0011 + # find_sensitive_words("./0011/0011.txt", "haha, 北京不错") + + # 0012 + # replace_sensitive_words("./0011/0011.txt", "haha, 北京不错") + + # 0013 + # get_url_imgs("http://www.ivsky.com/tupian/beijing_t1542/index_2.html") + + # 0014 + # dictxt_to_xls("./0014/student.txt") + + # 0015 + # dictxt_to_xls("./0015/city.txt") + + # 0016 + # listtxt_to_xls("./0016/numbers.txt") + + # 0017 + # student_dic = read_xls("./0017/student.xls") + # for key in student_dic: + # student = student_dic[key] + # + # dic = {} + # for list in student: + # dic[list[0]] = list[1:] + # + # write_student_to_xml(dic, "./0017/student.xml") + # + # break + + + # 0018 + # city_dic = read_xls("./0018/city.xls") + # for key in city_dic: + # city = city_dic[key] + # + # dic = {} + # for list in city: + # dic[list[0]] = list[1:] + # + # write_city_to_xml(dic, "./0018/city.xml") + # + # break + + # 0019 + # number_dic = read_xls("./0019/numbers.xls") + # for key in number_dic: + # number = number_dic[key] + # write_numbers_to_xml(number, "./0019/numbers.xml") + # + # break + + # 0020 + # statistics_month_time() + + # 0021 + encrypt_password("123") + + # 0022 + + # 0023 + + # 0024 + + # 0025 diff --git a/xyjxyf/tools/README.md b/xyjxyf/tools/README.md new file mode 100644 index 00000000..eec10f4a --- /dev/null +++ b/xyjxyf/tools/README.md @@ -0,0 +1,33 @@ +# python_tools + +## imager.py + +图片处理 + +1. 打开 + +2. 保存 + +3. 大小 + +4. 拷贝 + +5. 裁剪 + +6. 压缩 + +7. 添加文字 + +8. 旋转 + +9. 转换色彩模式 + +10. 生成验证码图片 + +## checker.py + +检测指定的内容,可以在Xcode编译时显示警告或者错误提示 + +## replacer.py + +替换指定内容 \ No newline at end of file diff --git a/xyjxyf/tools/__init__.py b/xyjxyf/tools/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/xyjxyf/tools/colorer.py b/xyjxyf/tools/colorer.py new file mode 100644 index 00000000..6ddcb11c --- /dev/null +++ b/xyjxyf/tools/colorer.py @@ -0,0 +1,24 @@ +# coding = utf-8 + +import random + +def randRGB(min=0, max=255): + if min < 0: + min = 0 + if min > 255: + min = 255 + + if max < 0: + max = 0 + if max > 255: + max = 255 + + if max < min: + tmp = min + min = max + max = tmp + + return (random.randint(min, max), + random.randint(min, max), + random.randint(min, max)) + diff --git a/xyjxyf/tools/dxbaiduaudio.py b/xyjxyf/tools/dxbaiduaudio.py new file mode 100644 index 00000000..0c8a7bf4 --- /dev/null +++ b/xyjxyf/tools/dxbaiduaudio.py @@ -0,0 +1,115 @@ +# encoing = utf-8 + +from urllib import request +import json, base64, uuid, os +import wave +import pycurl +import io + +bda_app_id = "7972313" +bda_api_key = "ZrjLfF5Rh7pOL66gaOmDGnXn" +bda_secret_key = "16bac9645093ca2632ebb81015ff7544" + +bda_access_token = "" +bda_expires_in = "" +ret_text = "" + +def get_mac_address(): + return uuid.UUID(int=uuid.getnode()).hex[-12:] + +def get_access_token(): + url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=ZrjLfF5Rh7pOL66gaOmDGnXn&client_secret=16bac9645093ca2632ebb81015ff7544" + + req = request.Request(url, method="POST") + resp = request.urlopen(req) + data = resp.read().decode('utf-8') + json_data = json.loads(data) + + global bda_access_token + bda_access_token = json_data['access_token'] + + return bda_access_token + +CHUNK = 1024 +def get_wav_data(wav_path): + if wav_path is None or len(wav_path) == 0: + return None + + fp = wave.open(wav_path, 'rb') + nf = fp.getnframes() + f_len = nf * 2 + audio_data = fp.readframes(nf) + + return audio_data, f_len + +def dump_res(buf): + resp_json = json.loads(buf.decode('utf-8')) + ret = resp_json['result'] + + global ret_text + ret_text = ret[0] + + print(buf) + +def wav_to_text(wav_path): + if wav_path is None or len(wav_path) == 0: + return None + + if len(bda_access_token) == 0: + get_access_token() + if len(bda_access_token) == 0: + return None + + data, f_len = get_wav_data(wav_path) + + url = 'http://vop.baidu.com/server_api?cuid=' + get_mac_address() + '&token=' + bda_access_token + http_header = [ + 'Content-Type: audio/pcm; rate=8000', + 'Content-Length: %d' % f_len + ] + + c = pycurl.Curl() + c.setopt(pycurl.URL, str(url)) #curl doesn't support unicode + #c.setopt(c.RETURNTRANSFER, 1) + c.setopt(c.HTTPHEADER, http_header) #must be list, not dict + c.setopt(c.POST, 1) + c.setopt(c.CONNECTTIMEOUT, 30) + c.setopt(c.TIMEOUT, 30) + c.setopt(c.WRITEFUNCTION, dump_res) + c.setopt(c.POSTFIELDS, data) + c.setopt(c.POSTFIELDSIZE, f_len) + c.perform() #pycurl.perform() has no return val + + return ret_text + + +# def wav_to_text(wav_path): +# if wav_path is None or len(wav_path) == 0: +# return None +# +# wav_data = get_wav_data(wav_path) +# if wav_data is None: +# return None +# +# if len(bda_access_token) == 0: +# get_access_token() +# +# wav_base64 = base64.b64decode(wav_data) +# print("%s", wav_base64) +# # unicode( wav_base64, errors='ignore') +# wav_len = len(wav_data) +# data_dic = {'format':'wav', 'rate':8000, 'channel':1, +# 'cuid':get_mac_address(), 'token':bda_access_token, +# b'speech':wav_base64, 'len':wav_len} +# json_data = json.dumps(data_dic).encode('utf-8') +# json_len = len(json_data) +# +# req = request.Request('http://vop.baidu.com/server_api') +# req.add_header('Content-Type', "application/json") +# req.add_header("Content-Length", json_len) +# resp = request.urlopen(req, data=json_data) +# +# resp_data = resp.read().decode('utf-8') +# resp_json = json.loads(resp_data) +# +# return resp_json['result'] \ No newline at end of file diff --git a/xyjxyf/tools/dxhtmlparser.py b/xyjxyf/tools/dxhtmlparser.py new file mode 100644 index 00000000..49853b6c --- /dev/null +++ b/xyjxyf/tools/dxhtmlparser.py @@ -0,0 +1,23 @@ +# encoding = utf-8 + +from html.parser import HTMLParser + + +class DXHTMLParser(HTMLParser): + def __init__(self, tag, tag_name, url): + HTMLParser.__init__(self) + self.tag = tag + self.tag_name = tag_name + self.url = url + self.rets = [] + + def handle_starttag(self, tag, attrs): + if tag == self.tag: + for name,value in attrs: + if name == self.tag_name: + if value.startswith("/") and len(self.url) > 0: + value = self.url + value + self.rets.append(value) + + def getrets(self): + return self.rets \ No newline at end of file diff --git a/xyjxyf/tools/geturlimgs.py b/xyjxyf/tools/geturlimgs.py new file mode 100644 index 00000000..359bb975 --- /dev/null +++ b/xyjxyf/tools/geturlimgs.py @@ -0,0 +1,66 @@ +# encoding = utf-8 + +import socket, os +from urllib import request +from bs4 import BeautifulSoup + +class geturlimgs(object): + + def __index__(self, to_dir=None): + self.to_dir = None + + # 伪装浏览器,以免被封 + def user_agent(self, url): + req_header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} + req_timeout = 20 + try: + req = request.Request(url,None,req_header) + html = request.urlopen(req,None,req_timeout) + except request.URLError as e: + print(e.message) + except socket.timeout as e: + # user_agent(url) + print("timeout") + + return html + + def get_img_links(self, url=None): + if url is None or len(url) == 0: + return None + html = self.user_agent(url) + soup = BeautifulSoup(html, "lxml") + + count = 0 + links = [] + items = soup.find_all('img') + for item in items: + link = item.get('src') + links.append(link) + + return links + + def download_imgs(self, links, to_dir): + if links is None or len(links) == 0: + return + + if not os.path.exists(to_dir): + os.makedirs(to_dir) + + if not to_dir.endswith('/'): + to_dir = to_dir + '/' + + index = 0 + for url in links: + end = os.path.splitext(url)[1] + if len(end) == 0: + end = ".jpg" + img_path = to_dir + '%i%s' % (index, end) + image = request.urlretrieve(url, img_path) + index = index + 1 + + def get_imgs(self, url=None, to_dir=None): + if url is None or to_dir is None: + return None + + links = self.get_img_links(url) + return self.download_imgs(links, to_dir) diff --git a/xyjxyf/tools/imager.py b/xyjxyf/tools/imager.py new file mode 100644 index 00000000..ecb14255 --- /dev/null +++ b/xyjxyf/tools/imager.py @@ -0,0 +1,189 @@ +# coding = utf-8 + +from PIL import Image, ImageDraw, ImageFont, ImageFilter +from tools import colorer, stringer +import types, os + + +# 打开 +def open_image(image_path=None): + if image_path is None or len(image_path) == 0: + return None + + im = Image.open(image_path) + return im + + +# 显示 +def show_image(image_path=None): + im = open_image(image_path) + im.show() + + +# 保存 +def save(image=None, to_path=None): + if image is None or to_path is None or len(to_path) == 0: + return + + f, e = os.path.splitext(to_path) + if e is None or len(e) == 0: + image.save(to_path, "JPEG") + else: + image.save(to_path) + + +# 大小 +def image_size(image=None): + ret_size = (0, 0) + + if image is None: + return ret_size + + return image.size + + +# 拷贝 +def image_copy(image=None): + if image is None: + return None + + return image.copy() + + +# 裁剪 +def image_crop(image=None, box=None): + if image is None or box is None: + return None + + return image.crop(box) + + +# 压缩 +def get_thumb(image=None, ratio=0.6): + if ratio > 1: + return image + if ratio <= 0 or image is None: + return None + + w, h = image.size() + + return image.thumbnail((w * ratio, h * ratio)) + +# # 合并 +# def paste(image=None, sub_image=None, origin=(0, 0)): +# if image is None or sub_image is None: +# return None +# +# im = image +# if type(image) is types.StringType: +# im = Image.open(image) +# if im is None: +# return None +# +# sub_im = sub_image +# if type(sub_image) is types.StringType: +# sub_im = Image.open(sub_image) +# if sub_im is None: +# return None +# +# im_size = im.size +# sub_size = sub_im.size +# # 如果sub_image在image范围内,不需要创建新的图片 +# if origin[0] >= 0 and origin[1] >= 0: +# right = origin[0] + sub_size[0] +# bottom = origin[1] + sub_size[1] +# if right <= im.size[0] and bottom <= im_size[1]: +# return im.paste(sub_im, (origin(0), origin(1), right, bottom)) +# +# right = im_size(0) +# if im_size(0) < sub_size(0): +# width = sub_size(0) +# height = im_size(1) +# if im_size(1) < sub_size(1): +# height = sub_size(1) +# +# +# image = Image.new('RGB', (width, height), (255, 255, 255)) +# +# +# return im.paste(sub_im, box) + + +# 添加文字 +def draw_text(image=None, text=None, origin=(0, 0), font=None, fill='red'): + if image is None or text is None or len(text) == 0: + return None + + context = ImageDraw.Draw(image) + context.text(origin, text, font=font, fill=fill) + return image + + +# 旋转 +def image_rotate(image=None, rotate=0): + + if image is None: + return None + + if rotate == 0: + return image + + return image.rotate(rotate) + + +# 转换色彩模式, mode: 'P'虚化,'L'或者'1'黑白,'LA'怀旧 +def image_convert(image=None, mode='P'): + if image is None: + return None + + return image.convert(mode) + + +# 生成验证码图片:数字和字母 +def verification_code(num=4, width=240, height=60, font_size=30): + image = Image.new('RGB', (width, height), (255, 255, 255)) + font = ImageFont.truetype('Arial.ttf', font_size) + draw = ImageDraw.Draw(image) + + # 背景填充 + for x in range(width): + for y in range(height): + draw.point((x, y), colorer.randRGB(min=64)) + + # 文字 + tw = width / num + margin = (tw - font_size) / 2 + ty = (height - font_size) / 2 + tx = margin + str = "" + for t in range(num): + char = stringer.rand_char() + draw.text((tx, ty), char, font=font, fill=colorer.randRGB(0, 100)) + tx = tx + tw + str = str + char + + # 模糊效果 + image = image.filter(ImageFilter.BLUR) + + return image, str + +# 生成中文验证码 + +# 重置图片大小 +def reset_image_size(image=None, max_width=None, max_height=None): + if image is None or max_width is None or max_height is None: + return + + if image.size[0] <= max_width and image.size[1] <= max_height: + return + + rotate = image.size[0] / image.size[1] + if rotate > 1: + new_width = max_width + new_height = max_width / rotate + else: + new_height = max_height + new_width = max_height * rotate + + new_image = image.resize((int(new_width), int(new_height)), Image.BILINEAR) + return new_image diff --git a/xyjxyf/tools/stringer.py b/xyjxyf/tools/stringer.py new file mode 100644 index 00000000..546dce86 --- /dev/null +++ b/xyjxyf/tools/stringer.py @@ -0,0 +1,76 @@ +# coding = utf-8 + +import random, string + +# def Unicode(): +# val = random.randint(0x4E00, 0x9FBF) +# return unichr(val) +# +# # 随机汉字 +# def GB2312(): +# head = random.randint(0xB0, 0xCF) +# body = random.randint(0xA, 0xF) +# tail = random.randint(0, 0xF) +# val = ( head << 8 ) | (body << 4) | tail +# str = "%x" % val +# return str.decode('hex').decode('gb2312') + +# 随机字母 +def rand_char(): + return chr(random.randint(65, 90)) + +# 随机字母或者数字 +def rand_choice(): + str = 'abcdefghigklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM1234567890' + return random.choice(str) + +# 将字典转为json格式 +def dict_to_json(object=None, begin_indent="", indent=u'\t', newl=u'\n'): + if object is None: + return None + + if type(object) is not dict: + return None + + ret_str = u'{' + newl + space = begin_indent + indent + for key in object: + ret_str = ret_str + space + key + ": " + item = object[key] + ret_str = ret_str + u'%s,' % str(item) + # if type(item) is list: + # list_to_json(item) + # elif type(item) is dict: + # dict_to_json(item) + # else: + # str + ret_str = ret_str + begin_indent + newl + + ret_str = ret_str + begin_indent + u'}' + + return ret_str + + +# 将数组转为json格式 +def list_to_json(object=None, begin_indent="", indent=u'\t', newl=u'\n'): + if object is None: + return None + + if type(object) is not list: + return None + + ret_str = u'[' + newl + space = begin_indent + indent + for item in object: + ret_str = ret_str + space + u'%s,' % str(item) + # if type(item) is list: + # list_to_json(item) + # elif type(item) is dict: + # dict_to_json(item) + # else: + # str + ret_str = ret_str + begin_indent + newl + + ret_str = ret_str + begin_indent + u']' + + return ret_str \ No newline at end of file diff --git a/xyjxyf/tools/test.py b/xyjxyf/tools/test.py new file mode 100644 index 00000000..52a91f05 --- /dev/null +++ b/xyjxyf/tools/test.py @@ -0,0 +1,6 @@ +# coding = utf-8 + +from tools import imager + +im, str = imager.verification_code() +im.show() \ No newline at end of file