Paul McGuire a écrit :
(snip)

<nitpicking>
May I suggest a couple cleanifications for our newbie friends around ?

> from searchparser import SearchQueryParser
> 
> products = [ "grape juice", "grape jelly", "orange juice", "orange
> jujubees",
>     "strawberry jam", "prune juice", "prune butter", "orange
> marmalade",
>     "grapefruit juice" ]

# round 1 : extracting constants from iterations

> class FruitSearchParser(SearchQueryParser):
>     def GetWord(self, word):
>         return set( p for p in products if p.startswith(word + " ") )

      def GetWord(self, word):
          target = word + " "
          return set(p for p in products if p.startswith(target) )


>     def GetWordWildcard(self, word):
>         return set( p for p in products if p.startswith(word[:-1]) )

       def GetWordWildcard(self, word):
           target = word[:-1]
           return set( p for p in products if p.startswith(target) )


# round 2 : factoring out common code

class FruitSearchParser(SearchQueryParser):
     def _find_products_starting_with(self, target):
         return set(p for p in products if p.startswith(target))

      def GetWord(self, word):
          return self._find_products_starting_with(word + " ")

       def GetWordWildcard(self, word):
          return self._find_products_starting_with(word[:-1])

# round 3: doing proper encapsulation:

class FruitSearchParser(SearchQueryParser):
     def __init__(self, products):
         self._products = products

     def _find_products_starting_with(self, target):
         return set(p for p in self._products if p.startswith(target))

      def GetWord(self, word):
          return self._find_products_starting_with(word + " ")

       def GetWordWildcard(self, word):
          return self._find_products_starting_with(word[:-1])


# round 4 : respecting pep08 (naming conventions):
# heck ! I guess that this would need a rewrite of SearchQueryParser

</nitpicking>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to