英文版:在网盘中.
《Python编程从入门到实践》第11章 测试代码使用书中推荐的Geany运行11.1.2节的代码结果与在pycharm中不一致.
参考:关于PyCharm中Python测试不是run_testname 而是run_unittest_in_testname 的问题
Python编程从入门到实践》 第16.2.6节 btc_cllose_2017.py中,def draw_line函数.
y_list=[v for _,v in y]
相当于,
y_list=[]
for _,v in y:
y_list.append(v)
ref:https://blog.csdn.net/dengpeiyi2012/article/details/90767789
ref:pygal制图“AttributeError: ‘NoneType’ object has no attribute ‘decode’
Traceback (most recent call last):
File "C:/Users/vivi01.zhu/PycharmProjects/untitled2/python_repos.py", line 32, in
chart.render_to_file('python_repos.svg')
File "C:\Python27\lib\site-packages\pygal\graph\public.py", line 114, in render_to_file
f.write(self.render(is_unicode=True, **kwargs))
File "C:\Python27\lib\site-packages\pygal\graph\public.py", line 52, in render
self.setup(**kwargs)
File "C:\Python27\lib\site-packages\pygal\graph\base.py", line 217, in setup
self._draw()
File "C:\Python27\lib\site-packages\pygal\graph\graph.py", line 933, in _draw
self._plot()
File "C:\Python27\lib\site-packages\pygal\graph\bar.py", line 146, in _plot
self.bar(serie)
File "C:\Python27\lib\site-packages\pygal\graph\bar.py", line 116, in bar
metadata)
File "C:\Python27\lib\site-packages\pygal\util.py", line 233, in decorate
metadata['label'])
File "C:\Python27\lib\site-packages\pygal\_compat.py", line 61, in to_unicode
return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'
从以下报错信息可以猜测出plot_dict中 "lbael"的description可能为null.经过检查,发现项目名为
File "C:\Python27\lib\site-packages\pygal\util.py", line 233, in decorate
metadata['label'])
解决方法1: 对repo_dict[‘description’]提前进行判断:
if repo_dict['description']:
plot_dict={'value':repo_dict['stargazers_count'],
'label':repo_dict['description'],
'xlink':repo_dict['html_url']
}
plot_dicts.append(plot_dict)
else:
plot_dict={'value':repo_dict['stargazers_count'],
'label':'ABC',
'xlink':repo_dict['html_url']}
plot_dicts.append(plot_dict)
————————————————
版权声明:本文为CSDN博主「waiwai3」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/waiwai3/article/details/77847783
解决方法2: 对 description的值进行str. null的地方会显示成None.
'label': str(repo_dict['description']),
完整代码如下:
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
url = 'https://api.github.com/search/repositories?q=language:python&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': str(repo_dict['description']),
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
# 可视化
my_style = LS('#003357', 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 = 20
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_repo.svg')
使用python版本与Django版本如下:
出错信息如下:
Traceback (most recent call last):
File "manage.py", line 21, in
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/apps/config.py", line 94, in create
module = import_module(entry)
File "/home/luoyu/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "", line 728, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in
from django.contrib.admin.filters import (
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in
from django.contrib.admin.options import IncorrectLookupParameters
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/contrib/admin/options.py", line 12, in
from django.contrib.admin import helpers, widgets
File "/home/luoyu/PycharmProjects/learning_log/11_env/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 151
'%s=%s' % (k, v) for k, v in params.items(),
^
SyntaxError: Generator expression must be parenthesized
分析得知道是Django与python版本兼容问题.(吐槽一下, 百度辣鸡,毛搜搜不到,stackoverflow 牛皮,O(∩_∩)O哈哈~)
.this is a known incompatibility between Django and Python 3.7. A fix has already been merged into Django 2.x branches and backported into 1.11 branch
solution: To solve this issue, simply update Django to at least version 1.11.17 (or 2.x) or you can downgrade Python to version 3.6.
pip install django --upgrade
由于书中用的Django版本过低,与本人采用的 (Python 3.7 Django 2.2.4)代码实现部分有区别.
18.3.1开始(代码参考: github链接( 待补充ing))
从入门到实践 Chapter18 第18章Django入门 映射URL 学习时的坑
关于Django Specifying a namespace in include() without providing an app_name
def test():
print("hell")