Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'NoneType' object has no attribute 'decode'

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['owner']['html_url'],
        }
    plot_dicts.append(plot_dict)

my_style = LS('#333366', base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = 'Most-Stared Python Project On Github'
chart.x_labels = names
chart.add('', plot_dicts)

chart.render_to_file('new_repos.svg')

*If I run this, there will be an error.

Traceback (most recent call last):
  File "new_repos.py", line 54, in <module>
    chart.render_to_file('new_repos.svg')
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\public.py", line 114, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\public.py", line 52, in render
    self.setup(**kwargs)
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\base.py", line 217, in setup
    self._draw()
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\graph.py", line 933, in _draw
    self._plot()
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\bar.py", line 146, in _plot
    self.bar(serie)
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\graph\bar.py", line 116, in bar 
    metadata)
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\util.py", line 234, in decorate 
    metadata['label'])
  File "C:\Users\Cao Kangkang\AppData\Roaming\Python\Python36\site-packages\pygal\_compat.py", line 62, in to_unicode
    return string.decode('utf-8')
AttributeError: 'NoneType' object has no attribute 'decode'

*How ever if I change part of the code to this, the error will disappear, *however the description will be ignored in the chart.

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['owner']['html_url'],
        }
    plot_dicts.append(plot_dict)

*I don't know why, Can anyone help me with this problem?

like image 793
C.Kang Avatar asked Jan 04 '23 00:01

C.Kang


2 Answers

'label':str(repo_dict['description'])

Try str() like above, it seems the data you got before hasn't clarifed the type of value that 'description' storged.

like image 73
A-ha Avatar answered Jan 13 '23 11:01

A-ha


May be the API didn't match the description, so you can use ifelse to solve it. Just like this.

description = repo_dict['description']
if not description:
    description = 'No description provided'

plot_dict = {
    'value': repo_dict['stargazers_count'],
    'label': description,
    'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)`

If the web API returns the value null, it means there's no result, so you can solve it with if statement. Maybe here, the items shadowsocks didn't return anything, so it maybe something went wrong.

like image 44
kaifan chen Avatar answered Jan 13 '23 10:01

kaifan chen