Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 17: python_repos.py cannot handle description of None #23

Closed
egabe05 opened this issue Dec 31, 2016 · 2 comments
Closed

Chapter 17: python_repos.py cannot handle description of None #23

egabe05 opened this issue Dec 31, 2016 · 2 comments

Comments

@egabe05
Copy link

egabe05 commented Dec 31, 2016

When working in Chapter 17 setting up custom tooltips (pp 388-389) I ran into the issue where the description for the project 'shadowsocks' was None. When the project is run you get a 'AttributeError: 'NoneType' object has no attribute 'decode'' exception when you try to render.

To get around this I had to add a None checker before I populate the plot_dict data, something like this:

for repo_dict in repo_dicts:

    # Cleans any none values and replaces with empty string
    for k, v in repo_dict.items():
        if v is None:
            repo_dict[k] = ''

    names.append(repo_dict['name'])
    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': repo_dict['description']
    }

    plot_dicts.append(plot_dict)

Not sure this is the fastest way of doing it but it worked for me.

@egabe05
Copy link
Author

egabe05 commented Dec 31, 2016

Couldn't get the entire block of code to formate correctly(for statement should be in the code block) but I think it still makes sense.

@ehmatthes
Copy link
Owner

ehmatthes commented Jan 3, 2017

Have you seen the updates section in the online resources? There's an update for this issue here.

You and I came up with similar approaches. Here's what I wrote for the update:

names, plot_dicts = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])

    # Some projects lack a description, which causes an error when 
    #  labeling bars. Specify a label if there's no description.
    description = repo_dict['description']
    if not description:
        description = "No description provided."

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

We pull the description from the repo_dict, and then change the description if none is provided. We don't change the repo_dict itself. Same result though, so nice work troubleshooting!

Couldn't get the entire block of code to formate correctly(for statement should be in the code block) but I think it still makes sense.

You can start a code block with triple backticks followed by python, and close the code block with another set of triple backticks. This formats your code with python syntax highlighting. I edited the formatting of your code block with this style.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants