diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json index 34c3556..e9138b1 100644 --- a/src/.vscode/launch.json +++ b/src/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Python: Learning", "type": "python", "request": "launch", - "pythonPath": "C:\\Python\\Python38\\python", + "pythonPath": "/usr/bin/python3", "program": "${file}" } ] diff --git a/src/day03/access.py b/src/day03/access.py new file mode 100644 index 0000000..86c8a34 --- /dev/null +++ b/src/day03/access.py @@ -0,0 +1,15 @@ +class Test: + def __init__(self, foo): + self.__foo = foo + + def __bar(self): + print(self.__foo) + print('__bar') + +def main(): + test = Test('hello') + test._Test__bar() + print(test._Test__foo) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/day04/association.py b/src/day04/association.py new file mode 100644 index 0000000..e1c2874 --- /dev/null +++ b/src/day04/association.py @@ -0,0 +1,74 @@ +''' +对象之间的关联关系 + + * @author [SamYang] + * @email [example@mail.com] + * @create date 2019-12-13 22:13:19 + * @modify date 2019-12-13 22:13:19 + * @desc [description] +''' + +from math import sqrt + + +class Point(object): + + def __init__(self, x=0, y=0): + self._x = x + self._y = y + + def move_to(self, x, y): + self._x = x + self._y = y + + def move_by(self, dx, dy): + self._x += dx + self._y += dy + + def distance_to(self, other): + dx = self._x - other._x + dy = self._y - other._y + return sqrt(dx ** 2 + dy ** 2) + + def __str__(self): + return '(%s, %s)' % (str(self._x), str(self._y)) + + +class Line(object): + + def __init__(self, start=Point(0, 0), end=Point(0, 0)): + self._start = start + self._end = end + + @property + def start(self): + return self._start + + @start.setter + def start(self, start): + self._start = start + + @property + def end(self): + return self.end + + @end.setter + def end(self, end): + self._end = end + + @property + def length(self): + return self._start.distance_to(self._end) + + +if __name__ == '__main__': + p1 = Point(3, 5) + print(p1) + p2 = Point(-2, -1.5) + print(p2) + + line = Line(p1, p2) + print(line.length) + line.start.move_to(2, 1) + line.end = Point(1, 2) + print(line.length) \ No newline at end of file diff --git a/src/day04/employee.py b/src/day04/employee.py new file mode 100644 index 0000000..4d41f1f --- /dev/null +++ b/src/day04/employee.py @@ -0,0 +1,73 @@ +''' +抽象类 方法重新 多态 +实现一个工资结算系统 公司有三种类型的员工 +- 部门经理固定月薪12000元/月 +- 程序员按本月工作小时数每小时100元 +- 销售员1500元/月的底薪加上本月销售额5%的提成 +输入员工的信息,输出每位员工的月薪信息 + + * @author [SamYang] + * @email [example@mail.com] + * @create date 2019-12-13 22:13:19 + * @modify date 2019-12-13 22:13:19 + * @desc [description] +''' +from abc import ABCMeta, abstractmethod + + +class Employee(object, metaclass=ABCMeta): + + def __init__(self, name): + super().__init__() + self._name = name + + @property + def name(self): + return self._name + + @abstractmethod + def get_salary(self): + pass + +class Manager(Employee): + + #想一想:如何不定义构造方法会怎么样 + def __init__(self, name): + #想一想:如果不调用父类构造器会怎么样 + super().__init__(name) + + def get_salary(self): + return 12000 + +class Programmer(Employee): + + def __init__(self, name): + super().__init__(name) + + def set_working_hour(self, working_hour): + self._working_hour = working_hour + + def get_salary(self): + return 100 * self._working_hour + +class Salesman(Employee): + + def __init__(self, name): + super().__init__(name) + + def set_sales(self, sales): + self._sales = sales + + def get_salary(self): + return 1500 + 0.05 * self._sales + +if __name__ == '__main__': + emps = [Manager('刘备'), Programmer('关羽'), Salesman('张飞')] + for emp in emps: + if isinstance(emp, Programmer): + working_hour = int(input('请输入%s本月工作时间: ' % emp.name)) + emp.set_working_hour(working_hour) + elif isinstance(emp, Salesman): + sales = float(input('请输入%s本月销售额: ' % emp.name)) + emp.set_sales(sales) + print('%s本月月薪为: ¥%.2f元' % (emp.name, emp.get_salary())) \ No newline at end of file diff --git a/src/day04/tempCodeRunnerFile.py b/src/day04/tempCodeRunnerFile.py new file mode 100644 index 0000000..b4e8edc --- /dev/null +++ b/src/day04/tempCodeRunnerFile.py @@ -0,0 +1 @@ +__str__ \ No newline at end of file diff --git a/src/day05/ball.py b/src/day05/ball.py new file mode 100644 index 0000000..930b857 --- /dev/null +++ b/src/day05/ball.py @@ -0,0 +1,108 @@ +from enum import Enum, unique +from math import sqrt +from random import randint + +import pygame + + +@unique +class Color(Enum): + """color""" + + RED = (255, 0, 0) + GREEN = (0, 255, 0) + BLUE = (0, 0, 255) + BLACK = (0, 0, 0) + WHITE = (255, 255, 255) + GRAY = (242, 242, 242) + + @staticmethod + def random_color(): + """random color""" + r = randint(0, 255) + g = randint(0, 255) + b = randint(0, 255) + return (r, g, b) + + +class Ball(object): + """ball""" + + def __init__(self, x, y, radius, sx, sy, color=Color.RED): + """initt""" + self.x = x + self.y = y + self.radius = radius + self.sx = sx + self.sy = sy + self.color = color + self.alive = True + + def move(self, screen): + """move""" + self.x += self.sx + self.y += self.sy + if self.x - self.radius <= 0 or self.x + self.radius >= screen.get_width(): + self.sx = -self.sx + if self.y - self.radius <= 0 or self.y + self.radius >= screen.get_height(): + self.sy = -self.sy + + def eat(self, other): + """eat that ball""" + if self.alive and other.alive and self != other: + dx, dy = self.x - other.x, self.y - other.y + distance = sqrt(dx ** 2 + dy ** 2) + if distance < self.radius + other.radius \ + and self.radius > other.radius: + other.alive = False + self.radius = self.radius + int(other.radius * 0.146) + + def draw(self, screen): + """draw balls""" + pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, 0) + + +def main(): + # balls container + balls = [] + # init pygame module + pygame.init() + # init windows sizes + screen = pygame.display.set_mode((800, 600)) + print(screen.get_width()) + print(screen.get_height()) + # set windows caption + pygame.display.set_caption('Big balls eat small balls') + # define small ball position of Windows + x, y = 50, 50 + running = True + # Start a loop event to deal + while running: + # get event to deal + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: + x, y = event.pos + radius = randint(10, 100) + sx, sy = randint(-10, 10), randint(-10, 10) + color = Color.random_color() + ball = Ball(x, y, radius, sx, sy, color) + balls.append(ball) + screen.fill((255, 255, 255)) + for ball in balls: + if ball.alive: + ball.draw(screen) + else: + balls.remove(ball) + pygame.display.flip() + # init windows 50 mrico seconds refresh ball position + pygame.time.delay(50) + for ball in balls: + ball.move(screen) + for other in balls: + ball.eat(other) + + +if __name__ == '__main__': + main() diff --git a/src/day05/csv1.py b/src/day05/csv1.py new file mode 100644 index 0000000..56bf63e --- /dev/null +++ b/src/day05/csv1.py @@ -0,0 +1,22 @@ +''' +read csv type of file + +version: 0.1 +Author: SamYang +Date: 2019-12-15 + +''' + +import csv + +filename = 'example.csv' + +try: + with open(filename) as f: + reader = csv.reader(f) + data = list(reader) +except FileNotFoundError: + print('Cannot open this file:', filename) +else: + for item in data: + print('%-30s%-20s%-10s' % (item[0], item[1], item[2])) \ No newline at end of file diff --git a/src/day05/ex1.py b/src/day05/ex1.py new file mode 100644 index 0000000..f32cd3f --- /dev/null +++ b/src/day05/ex1.py @@ -0,0 +1,21 @@ +# coding:utf-8 +''' +read csv type of file + +version: 0.1 +Author: SamYang +Date: 2019-12-15 + +''' + +input_again = True +while input_again: + try: + a = int(input('a = ')) + b = int(input('b = ')) + print('%d / %d = %f' % (a, b, a / b)) + input_again = False + except ValueError: + print('请输入整数') + except ZeroDivisionError: + print('除数不能为0') \ No newline at end of file diff --git a/src/day05/example.csv b/src/day05/example.csv new file mode 100644 index 0000000..50c661c --- /dev/null +++ b/src/day05/example.csv @@ -0,0 +1,1217 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Python-100-Days/example.csv at master · jackfrued/Python-100-Days + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + + + +
+ +
+ + +
+ +
+ + + +
+
+
+ + + + + + + + + + + + + + +
+
+ +
    + + + + +
  • + +
    + +
    + + + Watch + + +
    + Notifications +
    +
    + + + + + + + +
    +
    +
    + +
    +
  • + +
  • +
    +
    + + +
    +
    + + +
    + +
  • + +
  • +
    +
    + +
  • +
+ +

+ + /Python-100-Days + + +

+ +
+ + + + + + +
+
+
+ + + + + + + + + Permalink + + + + +
+ + +
+ + Branch: + master + + + + + + + +
+ +
+ + Find file + + + Copy path + +
+
+ + +
+ + Find file + + + Copy path + +
+
+ + + + + + +
+ Fetching contributors… +
+ +
+ + Cannot retrieve contributors at this time +
+
+ + + + +
+ +
+
+ + executable file + + 7 lines (7 sloc) + + 184 Bytes +
+ +
+ +
+ Raw + Blame + History +
+ + +
+ + + + +
+ +
+
+ +
+
+
+ + + + + + +
+
+ + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
4/5/2014 13:34Apples73
4/5/2014 3:41Cherries85
4/6/2014 12:46Pears14
4/8/2014 8:59Oranges52
4/10/2014 2:07Apples152
4/10/2014 18:10Bananas23
4/10/2014 2:40Strawberries98
+
+ +
+ +
+ + + +
+ + +
+ + +
+
+ + + +
+
+ +
+
+ + +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + +
+ + + + diff --git a/src/day05/file1.py b/src/day05/file1.py new file mode 100644 index 0000000..54dac53 --- /dev/null +++ b/src/day05/file1.py @@ -0,0 +1,32 @@ +# coding:utf-8 +''' +read csv type of file + +version: 0.1 +Author: SamYang +Date: 2019-12-15 + +''' +import time + + +def main(): + # 一次性读取整个文件内容 + # with open('致橡树.txt', 'r', encoding='utf-8') as f: + # print(f.read()) + + # 通过for-in循环逐行读取 + # with open('致橡树.txt', mode='r') as f: + # for line in f: + # print(line, end = '') + # time.sleep(0.5) + # print() + + # 读取文件按行读取到列表中 + with open('致橡树.txt') as f: + lines = f.readlines() + print(lines) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/src/day05/json1.py b/src/day05/json1.py new file mode 100644 index 0000000..68bfa69 --- /dev/null +++ b/src/day05/json1.py @@ -0,0 +1,22 @@ +''' +read csv type of file + +version: 0.1 +Author: SamYang +Date: 2019-12-15 + +''' + +import requests +import json + + +def main(): + resp = requests.get('http://api.tianapi.com/guonei/?key=APIKey&num=10') + data_model = json.loads(resp.text) + for news in data_model['newslist']: + print(news['title']) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git "a/src/day05/\350\207\264\346\251\241\346\240\221.txt" "b/src/day05/\350\207\264\346\251\241\346\240\221.txt" new file mode 100644 index 0000000..b9ed58e --- /dev/null +++ "b/src/day05/\350\207\264\346\251\241\346\240\221.txt" @@ -0,0 +1,1343 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Python-100-Days/致橡树.txt at master · jackfrued/Python-100-Days + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + + + +
+ +
+ + +
+ +
+ + + +
+
+
+ + + + + + + + + + + + + + +
+
+ +
    + + + + +
  • + +
    + +
    + + + Watch + + +
    + Notifications +
    +
    + + + + + + + +
    +
    +
    + +
    +
  • + +
  • +
    +
    + + +
    +
    + + +
    + +
  • + +
  • +
    +
    + +
  • +
+ +

+ + /Python-100-Days + + +

+ +
+ + + + + + +
+
+
+ + + + + + + + + Permalink + + + + +
+ + +
+ + Branch: + master + + + + + + + +
+ +
+ + Find file + + + Copy path + +
+
+ + +
+ + Find file + + + Copy path + +
+
+ + + + + + +
+
+ + + + + 调整了目录结构 + + + + 6a7f860 + Jun 5, 2019 + +
+ +
+
+ + 1 contributor + + +
+ +

+ Users who have contributed to this file +

+
+ +
+
+
+
+ + + + + +
+ +
+
+ + 32 lines (30 sloc) + + 832 Bytes +
+ +
+ +
+ Raw + Blame + History +
+ + +
+ + + + +
+ +
+
+ +
+
+
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
我如果爱你
绝不学攀援的凌霄花
借你的高枝炫耀自己
+
我如果爱你
绝不学痴情的鸟儿
为绿荫重复单调的歌曲
+
也不止像泉源
常年送来清凉的慰藉
也不止像险峰
增加你的高度 衬托你的威仪
甚至日光 甚至春雨
不 这些都还不够
我必须是你近旁的一株木棉
作为树的形象和你站在一起
根 紧握在地下
叶 相触在云里
每一阵风过
我们都互相致意
但没有人 听懂我们的言语
你有你的铜枝铁干
像刀 像剑 也像戟;
我有我红硕的花朵
像沉重的叹息 又像英勇的火炬
我们分担寒潮、风雷、霹雳
我们共享雾霭、流岚、虹霓
仿佛永远分离 却又终身相依
这才是伟大的爱情
坚贞就在这里
爱 不仅爱你伟岸的身躯
也爱你坚持的位置 足下的土地
+ + + +
+ +
+ + + +
+ + +
+ + +
+
+ + + +
+
+ +
+
+ + +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + +
+ + + + diff --git a/src/jupyter.py b/src/jupyter.py new file mode 100644 index 0000000..4209413 --- /dev/null +++ b/src/jupyter.py @@ -0,0 +1,11 @@ +# %% +import pandas as pd +import numpy as np +import matplotlib.pyplot as plt + + +ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) +ts = ts.cumsum() +ts.plot() + +# %% diff --git a/src/paddleHub/bilibili_reviews_clawler.py b/src/paddleHub/bilibili_reviews_clawler.py new file mode 100644 index 0000000..169d84c --- /dev/null +++ b/src/paddleHub/bilibili_reviews_clawler.py @@ -0,0 +1,59 @@ +import argparse +import requests as rs +import random +import time +from tqdm import tqdm + +# bilibili_reviews_crawler +parser = argparse.ArgumentParser() +parser.add_argument('--url', type=str, help='bilibili video url') +parser.add_argument('--output_file', type=str, default='./reviews.txt', help='reviews output file') +args = parser.parse_args() + + +video_api_base = 'http://api.bilibili.com/x/web-interface/view?bvid={}' +reviews_api_base = 'https://api.bilibili.com/x/v2/reply?callback=jQueryjsonp=jsonp&pn={}&type=1&oid={}&sort=0' +sub_reviews_api_base = 'https://api.bilibili.com/x/v2/reply/reply?callback=jQueryjsonp=jsonp&pn={}&type=1&oid={}&ps=10&root={}' +def fetch_reviews(url: str, output_file: str): + # get old id from bvid + bvid = url.split('/')[-1] + r = rs.get(video_api_base.format(bvid)).json() + aid = r['data']['aid'] + + # get reivews info + r = rs.get(reviews_api_base.format(1, aid)).json() + rcount = int(r['data']['page']['count']) + page_size = int(r['data']['page']['size']) + + # fetch reivews + n_reviews = 0 + with open(output_file, 'w') as f: + for i in tqdm(range(1, (rcount-1)//page_size+2)): + time.sleep(random.random()) + r = rs.get(reviews_api_base.format(i, aid)).json() + replies = r['data']['replies'] + + # print(f'page {i}, start to fetch content...') + for j, reply in enumerate(replies): + #print(f'*****************{j+1}*****************') + #print(reply['content']['message']) + f.write(repr(reply['content']['message']).replace("'", "") + '\n') + + rpid, sub_rcount, sub_page_size = reply['rpid'], int(reply['rcount']), 10 + n_reviews += 1+sub_rcount + + if sub_rcount == 0: # no sub replies + continue + + for k in range(1, (sub_rcount-1)//sub_page_size+2): + sub_r = rs.get(sub_reviews_api_base.format(k, aid, rpid)).json() + sub_replies = sub_r['data']['replies'] + for sub_reply in sub_replies: + #print('\t' + sub_reply['content']['message']) + f.write(repr(sub_reply['content']['message']).replace("'", "") + '\n') + + print(f'total reviews count: {n_reviews}') + + +if __name__ == "__main__": + fetch_reviews(args.url, args.output_file) \ No newline at end of file diff --git a/src/paddleHub/chinese_ocr_db_crnn.py b/src/paddleHub/chinese_ocr_db_crnn.py new file mode 100644 index 0000000..641d5a9 --- /dev/null +++ b/src/paddleHub/chinese_ocr_db_crnn.py @@ -0,0 +1,18 @@ +import requests +import json +import cv2 +import base64 + +def cv2_to_base64(image): + data = cv2.imencode('.jpg', image)[1] + return base64.b64encode(data.tobytes()).decode('utf8') + +# 发送HTTP请求 +data = {'images':[cv2_to_base64(cv2.imread("d:/words.jpg"))]} +headers = {"Content-type": "application/json"} +url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_server" +r = requests.post(url=url, headers=headers, data=json.dumps(data)) + +# 打印预测结果 +print(r.json()["results"]) +print(r.json()) \ No newline at end of file diff --git a/src/paddleHub/chinese_ocr_db_crnn_hub.py b/src/paddleHub/chinese_ocr_db_crnn_hub.py new file mode 100644 index 0000000..7901810 --- /dev/null +++ b/src/paddleHub/chinese_ocr_db_crnn_hub.py @@ -0,0 +1,10 @@ +import paddlehub as hub +import cv2 + +ocr = hub.Module(name="chinese_ocr_db_crnn_server") +result = ocr.recognize_text(images=[cv2.imread('d:/words.jpg')]) + +# or +# result = ocr.recognize_text(paths=['/PATH/TO/IMAGE']) +# 打印预测结果 +print(result) \ No newline at end of file