python第17章使用API课后习题答案.pdf
9页Solutions - Chapter 1717-1: Other LanguagesModify the API call in python_repos.py so it generates a chartshowing the most popular projects in other languages. Try languagessuch as JavaScript, Ruby, C, Java, Perl, Haskell, and Go.Note:Note: The code for this exercise includes an update that keeps aproject with an empty description from breaking the program, andmoves the font size settings tomy_style.import requestsimport pygalfrom pygal.style import LightColorizedStyle asas LCS, LightenStyleasas LS# Make an API call, and store the response.url = =https:/ = = requests. .get(url)printprint(Status code:, r. .status_code)# Store API response in a variable.response_dict = = r. .json()printprint(Total repositories:, response_dicttotal_count)# Explore information about the repositories.repo_dicts = = response_dictitemsnames, plot_dicts = = , forfor repo_dict inin repo_dicts: names. .append(repo_dictname)# When a project is removed, its still listed with stars.# So its in the top projects, but has no description. Thedescription# is None, which causes an exception when being used as alabel.ifif repo_dictdescription: desc= = repo_dictdescriptionelseelse: desc= = No description provided. plot_dict = = value: repo_dictstargazers_count,label: desc,xlink: repo_dicthtml_url, plot_dicts. .append(plot_dict)# Make visualization.my_style = = LS(#333366, base_style= =LCS)my_style. .title_font_size = = 24my_style. .label_font_size = = 14my_style. .major_label_font_size = = 18my_config = = pygal. .Config()my_config. .x_label_rotation = = 45my_config. .show_legend = = Falsemy_config. .truncate_label = = 15my_config. .show_y_guides = = Falsemy_config. .width = = 1000chart = = pygal. .Bar(my_config, style= =my_style)chart. .title = = Most-Starred JavaScript Projects on GitHubchart. .x_labels = = nameschart. .add(, plot_dicts)chart. .render_to_file(js_repos.svg)17-2: Active DiscussionsUsing the data from hn_submissions.py, make a bar chart showingthe most active discussions currently happening on Hacker News.The height of each bar should correspond to the number of commentseach submission has. The label for each bar should include thesubmissions title, and each bar should act as a link to the discussionpage for that submission.import requestsimport pygalfrom pygal.style import LightColorizedStyle asas LCS, LightenStyleasas LSfrom operator import itemgetter# Make an API call, and store the response.url = = https:/hacker- = = requests. .get(url)printprint(Status code:, r. .status_code)# Process information about each submission.submission_ids = = r. .json()submission_dicts = = forfor submission_id inin submission_ids:30:# Make a separate API call for each submission. url = = (https:/hacker- + +str(submission_id) + + .json) submission_r = = requests. .get(url)printprint(submission_r. .status_code) response_dict = = submission_r. .json() submission_dict = = title: response_dicttitle,link: http:/ + +str(submission_id),comments: response_dict. .get(descendants, 0) submission_dicts. .append(submission_dict)submission_dicts = = sorted(submission_dicts,key= =itemgetter(comments), reverse= =True)forfor submission_dict inin submission_dicts:printprint(nTitle:, submission_dicttitle)printprint(Discussion link:, submission_dictlink)printprint(Comments:, submission_dictcomments)titles, plot_dicts = = , forfor submission_dict inin submission_dicts: titles. .append(submission_dicttitle) plot_dict = = value: submission_dictcomments,label: submission_dicttitle,xlink: submission_dictlink, plot_dicts. .append(plot_dict)# Make visualization.my_style = = LS(#333366, base_style= =LCS)my_style. .title_font_size = = 24my_style. .label_font_size = = 14my_style. .major_label_font_size = = 18my_config = = pygal. .Config()my_config. .x_label_rotation = = 45my_config. .show_legend = = Falsemy_config. .truncate_label = = 15my_config. .show_y_guides = = Falsemy_config. .width = = 1000my_config. .y_title = = Number of Commentschart = = pygal. .Bar(my_config, style= =my_style)chart. .title = = Most Active Discussions on Hacker Newschart. .x_labels = = titleschart. .add(, plot_dicts)chart. .render_to_file(hn_discussions.svg)Output:17-3: Testing python_repos.pyIn python_repos.py, we printed the value ofstatus_code to make surethe API call was successful. Write a programcalled test_python_repos.py, which usesunittest to assert that thevalue ofstatus_code is 200. Figure out some other assertions you canmake - for example, that the number of items returned is expectedand that the total number of repositories is greater than a certainamount.Note:Note: The code for this exercise includes an update that keeps aproject with an empty description from breaking the program, andmoves the font size settings tomy_style.Writing tests pushes you to structure your code in a way that it can betested. Heres a revised version ofpython_repos.py, with all of thework written as four functions:import requestsimport pygalfrom pygal.style import LightColorizedStyle asas LCS, LightenStyleasas LS。





