From bce8ef1763c0a068e30d6302eb7c0568e22ea525 Mon Sep 17 00:00:00 2001 From: "Clayton G. Hobbs" Date: Sun, 15 May 2016 12:18:44 -0400 Subject: Formatting and documentation Added some blank lines and wrapped some long lines to be closer to PEP 8 compliance. Improved docstrings in NumberParser class. Added docstrings for undocumented code and removed a TODO message that no longer applies. --- kayleevc/numbers.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'kayleevc/numbers.py') diff --git a/kayleevc/numbers.py b/kayleevc/numbers.py index 6d41b63..cb52728 100644 --- a/kayleevc/numbers.py +++ b/kayleevc/numbers.py @@ -6,8 +6,10 @@ import re -# Define the mappings from words to numbers + class NumberParser: + """Parses integers from English strings""" + zero = { 'zero': 0 } @@ -81,12 +83,7 @@ class NumberParser: self.number_words.append(word) def parse_number(self, text_line): - """ - Parse numbers from natural language into ints - - TODO: Throw more exceptions when invalid numbers are detected. Only - allow certian valueless words within numbers. Support zero. - """ + """Parse a number from English into an int""" value = 0 partial_value = 0 last_list = None @@ -136,6 +133,13 @@ class NumberParser: return value def parse_all_numbers(self, text_line): + """ + Parse all numbers from English to ints + + Returns a tuple whose first element is text_line with all English + numbers replaced with "%d", and whose second element is a list + containing all the parsed numbers as ints. + """ nums = [] t_numless = '' @@ -168,6 +172,7 @@ class NumberParser: return (t_numless.strip(), nums) + if __name__ == '__main__': np = NumberParser() # Get the words to translate -- cgit 1.4.1