diff --git a/jimchenhub/0000/add-number-on-picture-OpenCV.py b/jimchenhub/0000/add-number-on-picture-OpenCV.py new file mode 100644 index 00000000..166c4d06 --- /dev/null +++ b/jimchenhub/0000/add-number-on-picture-OpenCV.py @@ -0,0 +1,10 @@ +#-*- coding:utf8 -*- +import cv2 +import numpy as np + +img = cv2.imread("./head.jpg") +height, width = img.shape[0], img.shape[1] +font=cv2.FONT_HERSHEY_SIMPLEX +cv2.putText(img,'4',(width - 40, 40), font, 1,(0, 0, 255),2) + +cv2.imwrite("target.jpg", img) \ No newline at end of file diff --git a/jimchenhub/0000/add-number-on-picture-PIL.py b/jimchenhub/0000/add-number-on-picture-PIL.py new file mode 100644 index 00000000..2b3b3ea4 --- /dev/null +++ b/jimchenhub/0000/add-number-on-picture-PIL.py @@ -0,0 +1,17 @@ +#-*- coding:utf8 -*- +import PIL +from PIL import Image +from PIL import ImageDraw + +# get the image +file = "head.jpg" +img = Image.open(file) +width, height = img.size[0], img.size[1] +print width, height +# Draw the image +draw = ImageDraw.Draw(img) +draw.text((width - 20, 20), "4", (255, 0, 0)) +del draw + +# save the image +img.save("target.jpg") \ No newline at end of file diff --git a/jimchenhub/0000/head.jpg b/jimchenhub/0000/head.jpg new file mode 100644 index 00000000..f8b133cc Binary files /dev/null and b/jimchenhub/0000/head.jpg differ diff --git a/jimchenhub/0000/target.jpg b/jimchenhub/0000/target.jpg new file mode 100644 index 00000000..ae059965 Binary files /dev/null and b/jimchenhub/0000/target.jpg differ diff --git a/jimchenhub/0001/coupon_code.py b/jimchenhub/0001/coupon_code.py new file mode 100644 index 00000000..5b022e94 --- /dev/null +++ b/jimchenhub/0001/coupon_code.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +#-*- encoding:utf-8 -*- +''' +生成长度为10的唯一优惠码,由数字和字母组成 +id + L + 随机码 +''' +import random +import string + +def generate_code(id, length=10): + prefix = hex(int(id))[2:] + "L" + length = length - len(prefix) + chars=string.ascii_letters+string.digits + return prefix + ''.join([random.choice(chars) for i in range(length)]) +def main(): + for i in range(20): + code = generate_code(i) + print code + +if __name__=="__main__": + main() \ No newline at end of file diff --git a/jimchenhub/0002/coupon.txt b/jimchenhub/0002/coupon.txt new file mode 100644 index 00000000..6da3663c --- /dev/null +++ b/jimchenhub/0002/coupon.txt @@ -0,0 +1,20 @@ +0L2IFj2oMm +1LwfzIE95r +2LfT3vChTl +3L5YkbNQMw +4LPHA8kegU +5LN68DBVlX +6LLwRFIxrM +7L6a71Sd1h +8LGBuAjL4D +9Lk7yYjFok +aLcpxO3DtU +bLynaJ8QOE +cLsFI8pD3a +dLcmdyFKle +eLr45vcdKl +fLLCUwWOqf +10LYxg37Rr +11LHlWDPBB +12L7NJ3y8Z +13LcD7JDzN \ No newline at end of file diff --git a/jimchenhub/0002/coupon_code_mysql.py b/jimchenhub/0002/coupon_code_mysql.py new file mode 100644 index 00000000..cbc94053 --- /dev/null +++ b/jimchenhub/0002/coupon_code_mysql.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +#-*- encoding:utf-8 -*- +''' +save coupon code into mysql database +''' +import random +import string +import MySQLdb + +config = { + "host":'localhost', + "port" : 3306, + "user":'root', + "passwd":'jim', + "db" :'python' + } + +class Save_to_mysql: + def __init__(self, code_file, config): + self.code_file = code_file + self.config = config + + def connect_db(self): + try: + conn= MySQLdb.connect(host=self.config["host"], port=self.config["port"], user=self.config["user"], passwd=self.config["passwd"], db=self.config["db"]) + except MySQLdb.Error, e: + try: + sqlError = "Error %d:%s" % (e.args[0], e.args[1]) + print sqlError + except IndexError: + print "MySQL Error:%s" % str(e) + self.conn = conn + self.cur = conn.cursor() + + def save_code_into_db(self): + f = open("./coupon.txt") + for code in f: + print code + sql = "insert into coupon_code(code) values('"+code+"')" + print sql + try: + self.cur.execute(sql) + except MySQLdb.Error, e: + try: + sqlError = "Error %d:%s" % (e.args[0], e.args[1]) + print sqlError + except IndexError: + print "MySQL Error:%s" % str(e) + f.close() + self.cur.close() + self.conn.commit() + def close_all(self): + self.conn.close() + +def main(): + code_file = "./coupon.txt" + mysql = Save_to_mysql(code_file, config) + mysql.connect_db() + mysql.save_code_into_db() + mysql.close_all() + +if __name__=="__main__": + main() \ No newline at end of file diff --git a/jimchenhub/0003/coupon.txt b/jimchenhub/0003/coupon.txt new file mode 100644 index 00000000..6da3663c --- /dev/null +++ b/jimchenhub/0003/coupon.txt @@ -0,0 +1,20 @@ +0L2IFj2oMm +1LwfzIE95r +2LfT3vChTl +3L5YkbNQMw +4LPHA8kegU +5LN68DBVlX +6LLwRFIxrM +7L6a71Sd1h +8LGBuAjL4D +9Lk7yYjFok +aLcpxO3DtU +bLynaJ8QOE +cLsFI8pD3a +dLcmdyFKle +eLr45vcdKl +fLLCUwWOqf +10LYxg37Rr +11LHlWDPBB +12L7NJ3y8Z +13LcD7JDzN \ No newline at end of file diff --git a/jimchenhub/0003/coupon_code_redis.py b/jimchenhub/0003/coupon_code_redis.py new file mode 100644 index 00000000..a2d6d165 --- /dev/null +++ b/jimchenhub/0003/coupon_code_redis.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +#-*- encoding:utf-8 -*- +''' +save coupon code into redis +''' +import redis + +config = { + "host":'localhost', + "port":6379, + "db":0 + } + +class Save_to_redis: + def __init__(self, code_file, config): + self.code_file = code_file + self.config = config + + def connect_db(self): + self.r = redis.Redis(host=self.config["host"], port = self.config["port"], db=self.config["db"]) + + def save_code_to_redis(self): + f = open("./coupon.txt") + for code in f: + code = code.strip('\n') + self.r.rpush("coupon_list", code) + f.close() + + def show_code_list(self): + print self.r.lrange("coupon_list", 0, -1) + + def cleanup(self): + while self.r.llen("coupon_list") : + self.r.lpop("coupon_list") + +def main(): + code_file = "./coupon.txt" + redis = Save_to_redis(code_file, config) + redis.connect_db() + redis.save_code_to_redis() + redis.show_code_list() + redis.cleanup() + +if __name__=="__main__": + main() \ No newline at end of file diff --git a/jimchenhub/0003/dump.rdb b/jimchenhub/0003/dump.rdb new file mode 100644 index 00000000..56af04ea --- /dev/null +++ b/jimchenhub/0003/dump.rdb @@ -0,0 +1 @@ +REDIS0006ܳCZV \ No newline at end of file diff --git a/jimchenhub/0004/content.txt b/jimchenhub/0004/content.txt new file mode 100644 index 00000000..31e2164b --- /dev/null +++ b/jimchenhub/0004/content.txt @@ -0,0 +1,2 @@ +one two three. +four \ No newline at end of file diff --git a/jimchenhub/0004/count_words.py b/jimchenhub/0004/count_words.py new file mode 100644 index 00000000..1167e45b --- /dev/null +++ b/jimchenhub/0004/count_words.py @@ -0,0 +1,14 @@ +#! /usr/bin/env python +#-*- encoding: utf-8 -*- + +import re + +def count_words_num(file_name): + f = open(file_name, "rb") + content = f.read() + words = re.findall(r"[a-zA-Z0-9]+", content) + return len(words) + +if __name__=='__main__': + file_name = "./content.txt" + print count_words_num(file_name) \ No newline at end of file diff --git a/jimchenhub/0005/change_image_size.py b/jimchenhub/0005/change_image_size.py new file mode 100644 index 00000000..24966faa --- /dev/null +++ b/jimchenhub/0005/change_image_size.py @@ -0,0 +1,17 @@ +#! /usr/bin/env python +#-*- encoding: utf-8 -*- + +import cv2 +import numpy as np + +# static value for iphone 5 +I5_WIDTH = 640.0 +I5_HEIGHT = 1136.0 +#get basic value for calculate zooming rate +img = cv2.imread("./head.jpg") +height, width = img.shape[0], img.shape[1] +zoomR = max(I5_WIDTH/width, I5_HEIGHT/height) +# resize the image +target = cv2.resize(img,(width/zoomR,height/zoomR),interpolation=cv2.INTER_CUBIC) +#save target image +cv2.imwrite("target.jpg", target) \ No newline at end of file diff --git a/jimchenhub/0005/head.jpg b/jimchenhub/0005/head.jpg new file mode 100644 index 00000000..f8b133cc Binary files /dev/null and b/jimchenhub/0005/head.jpg differ diff --git a/jimchenhub/0008/find_text.py b/jimchenhub/0008/find_text.py new file mode 100644 index 00000000..275bc6d6 --- /dev/null +++ b/jimchenhub/0008/find_text.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +#-*- encoding: utf-8 -*- +''' +0008:一个HTML文件,找出里面的正文。 +''' +import urllib2 +from bs4 import BeautifulSoup + +def find_text_in_html(url): + content = urllib2.urlopen(url).read() + soup = BeautifulSoup(content, "html.parser") + for string in soup.stripped_strings: + print string + +if __name__=="__main__": + url = "https://github.com/Yixiaohan/show-me-the-code" + find_text_in_html(url) \ No newline at end of file diff --git a/jimchenhub/0009/find_text.py b/jimchenhub/0009/find_text.py new file mode 100644 index 00000000..11d3fc83 --- /dev/null +++ b/jimchenhub/0009/find_text.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +#-*- encoding: utf-8 -*- +''' +0009:一个HTML文件,找出里面的链接。 +''' +import urllib2 +from bs4 import BeautifulSoup + +def show_href(url): + content = urllib2.urlopen(url).read() + + soup = BeautifulSoup(content, "html.parser") + for tag in soup.find_all('a'): + print tag['href'] + + +if __name__ == '__main__': + show_href('https://github.com/Yixiaohan/show-me-the-code')