|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Example: Accessing similar street names from Geosupport errors |
| 4 | +
|
| 5 | +When Geosupport can't find a street name, it returns up to 10 similar names |
| 6 | +in the error response. This example shows how to extract them. |
| 7 | +""" |
| 8 | + |
| 9 | +from geosupport import Geosupport, GeosupportError |
| 10 | + |
| 11 | + |
| 12 | +def get_similar_streets(error): |
| 13 | + """Extract similar street names from a GeosupportError.""" |
| 14 | + result = error.result |
| 15 | + |
| 16 | + # Get count of similar names (may be string or int) |
| 17 | + count = result.get("Number of Street Codes and Street Names in List", 0) |
| 18 | + if isinstance(count, str): |
| 19 | + count = int(count.strip() or "0") |
| 20 | + if count == 0: |
| 21 | + return [] |
| 22 | + |
| 23 | + # Get list of street names (already parsed into a list by the library) |
| 24 | + similar_names = result.get("List of Street Names", []) |
| 25 | + |
| 26 | + # Filter out empty strings and return |
| 27 | + return [name for name in similar_names if name and name.strip()] |
| 28 | + |
| 29 | + |
| 30 | +# Example usage |
| 31 | +if __name__ == "__main__": |
| 32 | + g = Geosupport() |
| 33 | + |
| 34 | + # Example 1: Misspelled street name |
| 35 | + print("Example: Searching for 'wycoff street' in Brooklyn") |
| 36 | + print("-" * 50) |
| 37 | + |
| 38 | + try: |
| 39 | + # This will fail because it should be 'wyckoff' |
| 40 | + result = g["1N"](borough_code="BK", street_name="wycoff street") |
| 41 | + except GeosupportError as e: |
| 42 | + print(f"Error: {e}\n") |
| 43 | + |
| 44 | + # Extract and display similar names |
| 45 | + similar = get_similar_streets(e) |
| 46 | + if similar: |
| 47 | + print(f"Found {len(similar)} similar street(s):") |
| 48 | + for i, name in enumerate(similar, 1): |
| 49 | + print(f" {i}. {name}") |
| 50 | + |
| 51 | + # You could now retry with similar[0] or let user choose |
| 52 | + print(f"\nRetrying with first match: '{similar[0]}'") |
| 53 | + result = g["1N"](borough_code="BK", street_name=similar[0]) |
| 54 | + print( |
| 55 | + f"Success! Street code: {result.get('B10SC - First Borough and Street Code')}" |
| 56 | + ) |
0 commit comments