Skip to content

Commit 4fb69e8

Browse files
committed
2 parents c95216e + 80ed225 commit 4fb69e8

File tree

24 files changed

+856
-0
lines changed

24 files changed

+856
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import requests
2+
from pyecharts.charts import Bar
3+
from pyecharts import options as opts
4+
5+
6+
def get_data():
7+
base_url = 'https://api.github.com/search/repositories?q=language:python+created:%3E2019-12-31&sort=stars&order=desc&per_page=10'
8+
response = requests.get(base_url)
9+
result = response.json()
10+
data = {}
11+
for item in result['items']:
12+
data[item['name']] = [item['html_url'], item['stargazers_count'], item['watchers_count'], item['forks']]
13+
return data
14+
15+
16+
def show_img():
17+
data = get_data()
18+
names = list(data.keys())
19+
values = [data[name][1] for name in names]
20+
21+
bar = (
22+
Bar()
23+
.add_xaxis(names[::-1])
24+
.add_yaxis("星标数", values[::-1])
25+
.reversal_axis()
26+
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
27+
.set_global_opts(
28+
yaxis_opts=opts.AxisOpts(name_rotate=0, name="项目", axislabel_opts={'interval': -10, "rotate": 0}),
29+
title_opts=opts.TitleOpts(title="2020 GitHub Python TOP 10"))
30+
)
31+
bar.render_notebook()
32+
33+
34+
if __name__ == '__main__':
35+
show_img()

doudou/2021-01-10-fake-data/api.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from flask import Flask, jsonify, request
2+
from mimesis.schema import Field, Schema
3+
from mimesis.enums import Gender
4+
5+
app = Flask(__name__)
6+
7+
_ = Field('zh')
8+
schema = Schema(schema=lambda: {
9+
'id': _('uuid'),
10+
'name': _('person.name'),
11+
'version': _('version', pre_release=True),
12+
'timestamp': _('timestamp', posix=False),
13+
'owner': {
14+
'email': _('person.email', domains=['test.com'], key=str.lower),
15+
'token': _('token_hex'),
16+
'creator': _('full_name', gender=Gender.FEMALE)
17+
},
18+
'address': {
19+
'country': _('address.country'),
20+
'province': _('address.province'),
21+
'city': _('address.city')
22+
}
23+
})
24+
25+
26+
@app.route('/apps', methods=('GET',))
27+
def apps_view():
28+
count = request.args.get('count', default=1, type=int)
29+
data = schema.create(iterations=count)
30+
return jsonify(data)
31+
32+
33+
if __name__ == '__main__':
34+
app.run(host='127.0.0.1', port=5200, debug=True)
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from faker import Faker
2+
from faker.providers import BaseProvider
3+
4+
faker = Faker(locale='zh_CN')
5+
print(f'name: {faker.name()}')
6+
print(f'address: {faker.address()}')
7+
print(f'date: {faker.date()}')
8+
9+
10+
class MyProvider(BaseProvider):
11+
def foo(self):
12+
return 'bar'
13+
14+
15+
faker.add_provider(MyProvider)
16+
print(f'foo: {faker.foo()}')
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from mimesis import Person
2+
from mimesis import Address
3+
from mimesis import Food
4+
5+
print("#" * 30 + " person " + "#" * 30)
6+
person = Person('zh')
7+
print(f'name: {person.surname() + "" + person.name()}')
8+
print(f'sex: {person.sex()}')
9+
print(f'academic degree: {person.academic_degree()}')
10+
11+
print("*" * 30 + " person data " + "*" * 30)
12+
print('\n'.join(('%s:%s' % item for item in person._data.items())))
13+
14+
print("#" * 30 + " address " + "#" * 30)
15+
address = Address("zh")
16+
print(f'continent: {address.continent()}')
17+
print(f'province: {address.province()}')
18+
print(f'city: {address.city()}')
19+
print(f'street name: {address.street_name()}')
20+
21+
print("*" * 30 + " address data " + "*" * 30)
22+
print('\n'.join(('%s:%s' % item for item in address._data.items())))
23+
24+
print("#" * 30 + " food " + "#" * 30)
25+
food = Food("zh")
26+
print(f'dish: {food.dish()}')
27+
print(f'drink: {food.drink()}')
28+
29+
print("*" * 30 + " food data " + "*" * 30)
30+
print('\n'.join(('%s:%s' % item for item in food._data.items())))

doudou/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Python技术 公众号文章代码库
44

5+
### 2020 代码列表
6+
57
+ [douban-movie-top250](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-02-20-douban-movie-top250):实战|数据分析篇之豆瓣电影 TOP250
68
+ [duo-la-a-meng](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-03-27-duo-la-a-meng):用 Python 画哆啦 A 梦
79
+ [fund-fixed-investment](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-03-27-found):指数基金定投到底能不能赚钱?Python 来告诉你答案
@@ -18,6 +20,10 @@ Python技术 公众号文章代码库
1820
+ [163 music](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-11-02-163-music):下载网易云乐库
1921
+ [Chinese People's Volunteer Army](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-11-10-resisting-us-aid-korea):中国人民志愿军
2022

23+
### 2021 代码列表
24+
+ [GitHub-Top10](https://github.com/JustDoPython/python-examples/tree/master/doudou/2021-01-02-GitHub-Python-Top10):2020 GitHub Python 库 TOP10
25+
+ [fake-data](https://github.com/JustDoPython/python-examples/tree/master/doudou/2021-01-10-fake-data):假数据
26+
2127
---
2228

2329
从小白到工程师的学习之路。

moumoubaimifan/game/game.py

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# coding:utf-8
2+
import random
3+
import time
4+
5+
import pygame
6+
7+
W = 600
8+
H = 500
9+
10+
class Ball:
11+
12+
x = None # x坐标
13+
y = None # y坐标
14+
speed_x = None # x方向的速度
15+
speed_y = None # y方向的速度
16+
radius = None # 半径
17+
color = None # 颜色
18+
19+
def __init__(self, x, y, speed_x, speed_y, radius, color):
20+
"""
21+
初始化
22+
:param x: X坐标
23+
:param y: Y坐标
24+
:param speed_x: X轴方向速度
25+
:param speed_y: Y轴方向速度
26+
:param radius: 半径
27+
:param color: 颜色
28+
"""
29+
self.x = x
30+
self.y = y
31+
self.speed_x = speed_x
32+
self.speed_y = speed_y
33+
self.radius = radius
34+
self.color = color
35+
36+
def draw(self, screen):
37+
"""
38+
绘制小球
39+
:param screen: 窗口
40+
:return:
41+
"""
42+
pygame.draw.circle(screen, self.color, [self.x, self.y], self.radius)
43+
44+
45+
def move(self, screen):
46+
"""
47+
小球移动
48+
:param screen: 窗口
49+
:return:
50+
"""
51+
self.x += self.speed_x
52+
self.y += self.speed_y
53+
54+
# 左右边界
55+
if self.x > W - self.radius or self.x < self.radius:
56+
self.speed_x = -self.speed_x
57+
58+
# 上下边界
59+
if self.y > H - self.radius or self.y < self.radius:
60+
self.speed_y = -self.speed_y
61+
# 移动频率
62+
time.sleep(0.001)
63+
self.draw(screen)
64+
65+
66+
class Player:
67+
68+
radius = None
69+
color = None
70+
x = 1000
71+
y = 1000
72+
73+
def __init__(self, radius, color):
74+
"""
75+
初始化
76+
:param radius: 半径
77+
:param color: 颜色
78+
"""
79+
self.radius = radius
80+
self.color = color
81+
82+
def move(self, screen):
83+
"""
84+
大球移动
85+
:return:
86+
"""
87+
# 鼠标检测
88+
if pygame.mouse.get_focused():
89+
# 获取光标位置,
90+
x, y = pygame.mouse.get_pos()
91+
92+
mouse = pygame.mouse.get_pressed()
93+
94+
pygame.draw.circle(screen, self.color, [x, y], self.radius)
95+
self.x = x
96+
self.y = y
97+
98+
balls = []
99+
def create_ball(screen):
100+
"""
101+
创建小球
102+
:param screen:
103+
:return:
104+
"""
105+
x = random.randint(0, W)
106+
y = random.randint(0, H)
107+
speed_x = random.randint(-5, 5)
108+
speed_y = random.randint(-5, 5)
109+
r = 3
110+
color = 'white'
111+
b = Ball(x, y, speed_x, speed_y, r, color)
112+
113+
balls.append(b)
114+
115+
b.draw(screen)
116+
117+
def show_text(screen, text, pos, color, font_bold=False, font_size=18, font_italic=False):
118+
"""
119+
显示文字
120+
:param screen: 窗口
121+
:param text: 文字
122+
:param pos: 坐标
123+
:param color: 颜色
124+
:param font_bold: 是否粗体
125+
:param font_size: 大小
126+
:param font_italic: 是否斜体
127+
:return:
128+
"""
129+
cur_font = pygame.font.SysFont('Courier', font_size)
130+
cur_font.set_bold(font_bold)
131+
cur_font.set_italic(font_italic)
132+
text_fmt = cur_font.render(text, 1, color)
133+
screen.blit(text_fmt, pos)
134+
135+
def close():
136+
for event in pygame.event.get():
137+
if event.type == pygame.QUIT:
138+
pygame.quit()
139+
exit(0)
140+
141+
def main():
142+
# 初始化pygame模块
143+
pygame.init()
144+
# 设置窗口大小
145+
screen = pygame.display.set_mode((W,H))
146+
# 设置窗口标题
147+
pygame.display.set_caption('是男人就坚持100秒')
148+
149+
for i in range(0, 10):
150+
create_ball(screen)
151+
152+
p = Player(10, 'red')
153+
text_time = "TIME:%.3d" % (time.perf_counter())
154+
155+
is_loop = True
156+
while is_loop:
157+
# 重绘屏幕
158+
screen.fill((0))
159+
160+
p.move(screen)
161+
162+
for ball in balls:
163+
ball.move(screen)
164+
if abs(p.x - ball.x) < 13 and abs(p.y - ball.y) < 13:
165+
is_loop = False
166+
break
167+
168+
# 刷新显示
169+
text_time = "TIME:%.3d" % (time.perf_counter())
170+
show_text(screen, text_time, (500, 40), (0, 255, 0), True)
171+
pygame.display.update()
172+
173+
close()
174+
175+
while True:
176+
close()
177+
show_text(screen, "Game over!", (120, 180), "green", True, 60)
178+
show_text(screen, text_time, (220, 270), "green", True, 30)
179+
180+
pygame.display.update()
181+
182+
183+
if __name__ == '__main__':
184+
main()

0 commit comments

Comments
 (0)