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

Flask Url shortening service #5

Open
sr-murthy opened this issue Dec 25, 2016 · 1 comment
Open

Flask Url shortening service #5

sr-murthy opened this issue Dec 25, 2016 · 1 comment

Comments

@sr-murthy
Copy link

Hi

I feel that in chapter 1 of your book 'Learning Python Design Patterns', in the Flask URL shortening service, there might be an error in the method __increment_string in the Url class.

    def __increment_string(self, astring):
    """Increments string, that is:

         a -> b
         z -> aa
         az -> ba
         empty string -> a
    """
    if astring == '':
        return 'a'

    last_char = astring[-1]

    if last_char != 'z':
        return astring[:-1] + chr(ord(last_char) + 1)

    return self.__increment_string(astring[:-1]) + 'a'

This does not produce the expected output, for example:

In [4]: su._Url__increment_string('https://www.theguardian.com/travel/2016/dec/25/christmas-day-walk-the-city-of-london')
Out[4]: 'https://www.theguardian.com/travel/2016/dec/25/christmas-day-walk-the-city-of-londoo'
@sr-murthy
Copy link
Author

I think there is also a typo error on page 11 of your book - in the method Url.get_by_short_url(cls, short_url) the line url_mapping = Url.load_url_mapping() should be url_mapping = Url.__load_url_mapping(). But to avoid class renaming issues it is maybe better that it is url_mapping = cls.__load_url_mapping().

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

1 participant