From a0867f648fa580f0e04dfa41aa8576ead567b176 Mon Sep 17 00:00:00 2001 From: Nik Conwell Date: Mon, 29 Jan 2024 07:56:07 -0500 Subject: [PATCH] Added in town default --- python/latitude/convert.py | 29 +++++++++++++++-------------- python/latitude/tests/convert.bats | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/python/latitude/convert.py b/python/latitude/convert.py index e410ccc..8b541d1 100755 --- a/python/latitude/convert.py +++ b/python/latitude/convert.py @@ -1,34 +1,35 @@ #!/home/nik/github/try/python/latitude/bin/python import argparse +import re # # Args # -argParser = argparse.ArgumentParser() -argParser.add_argument('--check', dest='check', help='String to check against known good') +argParser = argparse.ArgumentParser(prog="convert.py", + description="Convert street address to latitude/longitude.") + argParser.add_argument('--debug', dest='debug', default=False, action='store_true', help='Debug mode for various things') argParser.add_argument('address') - - args = argParser.parse_args() -print(f"You entered an address off >>>{args.address}<<<") +address = args.address +print(f"You entered an address off >>>{address}<<<") +# Add in town/state if needed +if (not re.search('natick', address)): + address+= ", natick, ma" + print(f"(Added in town) >>>{address}<<<") -# importing geopy library and Nominatim class +# Library to convert addresses from geopy.geocoders import Nominatim - -# calling the Nominatim tool and create Nominatim class loc = Nominatim(user_agent="Geopy Library") -# entering the location name -getLoc = loc.geocode(args.address) - -# printing address -print(getLoc.address) +# Convert address entered +getLoc = loc.geocode(address) -# printing latitude and longitude +# Show information about the address +print(f"Normalized address = {getLoc.address}") print(f"Latitude = {getLoc.latitude:.6f}") print(f"Longitude = {getLoc.longitude:.6f}") diff --git a/python/latitude/tests/convert.bats b/python/latitude/tests/convert.bats index e5e6899..1696910 100644 --- a/python/latitude/tests/convert.bats +++ b/python/latitude/tests/convert.bats @@ -22,8 +22,21 @@ function setup() { -@test "Make sure it runs" { +@test "Run with basic address 172 HARTFORD ST natick ma" { run convert.py '172 HARTFORD ST natick ma' assert_output --partial 'Latitude = 42.291410' assert_output --partial 'Longitude = -71.398049' } + +@test "Run with basic address 1245 WORCESTER ST natick ma" { + run convert.py '1245 WORCESTER ST natick ma' + assert_output --partial 'Latitude = 42.300226' + assert_output --partial 'Longitude = -71.383479' +} + +@test "Check town addition 1245 WORCESTER ST" { + run convert.py '1245 WORCESTER ST' + assert_output --partial 'Added in town' + assert_output --partial 'Latitude = 42.300226' + assert_output --partial 'Longitude = -71.383479' +}