gennad/Design-Patterns-in-Python

Flask Url shortening service

sr-murthy opened this issue · 1 comments

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'

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().