Browse files

added some documentation to address_parser.py related to doctests

  • Loading branch information...
1 parent 12b2b31 commit b01fa50833ca9f4dd9600b4184f9abcd866f069c Serdar Tumgoren committed Feb 3, 2011
Showing with 29 additions and 5 deletions.
  1. +29 −5 tutorials/address_parser.py
View
34 tutorials/address_parser.py
@@ -7,23 +7,27 @@
direction appears before the street name.
The first function uses indexing and slice notation
-to pull apart the address. The second function relies on
-regular expresions to extract the relevant address portions.
+to pull apart the address. The second function uses a
+regular expression to parse the address.
-Usage examples:
+
+USAGE:
>>> parse_address("123 Main St N")
'123 N Main St'
>>> parse_address_with_regex("123 Main St N")
'123 N Main St'
-"""
+"""
import re
+
+ #### Index/Slice Technique ####
+
def parse_address(address):
"""
- This function uses slice notation to parse and reformat an address.
+ This function, courtesy of Brian Bowling, uses slice notation to parse and reformat an address.
More info on slice notation is here:
http://docs.python.org/tutorial/introduction.html#strings
@@ -48,6 +52,9 @@ def parse_address(address):
return new_address
+
+ #### Regular Expression Technique ####
+
# Create a regular expression pattern, which we'll use to match address strings
address_pattern = re.compile(r'^(\w+)\s(.+?)\s(N|S|E|W|NW|NE|SW|SE)$')
@@ -73,5 +80,22 @@ def parse_address_with_regex(address_string):
if __name__ == '__main__':
+ # The "doctest" code at the bottom of this program is boilerplate syntax
+ # to help run tests inside of Python docstrings.
+
+ # Doctests not only help ensure that your code works as expected,
+ # but they help demonstrate to others how to properly use your code.
+
+ # These tests resemble the code from a session in the Python
+ # interactive interpreter, and in fact, you can copy and paste code from
+ # such sessions directly into your Python program.
+ #
+ # The doctests in this program are at the top of the file, right beneath
+ # the "Usage" line. To run the doctests in this program, execute the
+ # following command from your shell or terminal:
+ # python address_parser.py -v
+
+ # More info on doctests can be found here:
+ # http://docs.python.org/library/doctest.html
import doctest
doctest.testmod()

0 comments on commit b01fa50

Please sign in to comment.