17-1 其他语言
- 修改python-repos.py中的API调用,使其在生成的图表中显示使用其他语言编写的最受欢迎的项目。请尝试语言JavaScipt、Ruby、C、Java、Perl、Haskell和Go等
solution:
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 执行API调用并存储响应
URL = 'https://api.github.com/search/repositories?q=language:java&sort=stars'
r = requests.get(URL)
print("Status code:", r.status_code)
response_dict = r.json()
print("Total repositories:", response_dict['total_count'])
# 探索有关仓库的信息
repo_dicts = response_dict['items']
names, plot_dicts = [], []
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
# 可视化
my_style = LS('#FFA500', base_style=LCS)
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most-Starred Java Projects on GitHub'
chart.x_labels = names
chart.add("", plot_dicts)
chart.render_to_file('java_repos.svg')
Output:
17-2 测试python_repos.py
- 在python_repos.py中,打印status_code的值,以核实APi调用是否成功了。请编写一个名为test_python_repos.py的程序,它使用unittest来断言status_code的值为200
solution:
python_repos_for_testing.py
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
def get_response():
URL = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(URL)
return r
def get_repo_dicts(response):
response_dict = response.json()
repo_dicts = response_dict['items']
return repo_dicts
def get_names_plot_dicts(repo_dicts):
names, plot_dicts = [], []
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
description = repo_dict['description']
if not description:
description = "No description provided."
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': repo_dict['description'],
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
return names, plot_dicts
def make_visualization(names, plot_dicts):
my_style = LS('#333366', base_style=LCS)
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('python_repos_for_testing.svg')
r = get_response()
repo_dicts = get_repo_dicts(r)
names, plot_dicts = get_names_plot_dicts(repo_dicts)
make_visualization(names, plot_dicts)
test_python_repos.py
import unittest
import python_repos_for_testing as pr
class PythonReposTestCase(unittest.TestCase):
"""测试python_repos.py"""
def setUp(self):
self.r = pr.get_response()
self.repo_dicts = pr.get_repo_dicts(self.r)
self.names, self.plot_dicts = pr.get_names_plot_dicts(self.repo_dicts)
def test_get_response(self):
self.assertEqual(self.r.status_code, 200)
unittest.main()
Output: