Skip to content

Commit 09206a5

Browse files
Adding Component Analysis example.
1 parent f6cee74 commit 09206a5

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ us_reverse_geo_api:
3535
PYTHONPATH=. python3 examples/us_reverse_geo_example.py
3636

3737
us_street_api:
38-
PYTHONPATH=. python3 examples/us_street_single_address_example.py && PYTHONPATH=. python3 examples/us_street_multiple_addresses_example.py
38+
PYTHONPATH=. python3 examples/us_street_single_address_example.py && PYTHONPATH=. python3 examples/us_street_multiple_addresses_example.py && PYTHONPATH=. python3 examples/us_street_component_analysis_example.py
3939

4040
us_zipcode_api:
4141
PYTHONPATH=. python3 examples/us_zipcode_single_lookup_example.py && PYTHONPATH=. python3 examples/us_zipcode_multiple_lookups_example.py
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import json
4+
5+
from smartystreets_python_sdk import SharedCredentials, StaticCredentials, exceptions, ClientBuilder
6+
from smartystreets_python_sdk.us_street import Lookup as StreetLookup
7+
from smartystreets_python_sdk.us_street.match_type import MatchType
8+
9+
10+
def run():
11+
# For client-side requests (browser/mobile), use this code:
12+
# key = os.environ['SMARTY_AUTH_WEB']
13+
# hostname = os.environ['SMARTY_WEBSITE_DOMAIN']
14+
# credentials = SharedCredentials(key, hostname)
15+
16+
# For server-to-server requests, use this code:
17+
auth_id = os.environ['SMARTY_AUTH_ID']
18+
auth_token = os.environ['SMARTY_AUTH_TOKEN']
19+
credentials = StaticCredentials(auth_id, auth_token)
20+
21+
client = ( ClientBuilder(credentials)
22+
.with_feature_component_analysis() # To add component analysis feature you need to specify when you create the client.
23+
.build_us_street_api_client()
24+
)
25+
26+
lookup = StreetLookup()
27+
lookup.street = "1 Rosedale"
28+
lookup.secondary = "APT 2"
29+
lookup.city = "Baltimore"
30+
lookup.state = "MD"
31+
lookup.zipcode = "21229"
32+
lookup.match = MatchType.ENHANCED # Enhanced matching is required to return component analysis results.
33+
34+
try:
35+
client.send_lookup(lookup)
36+
except exceptions.SmartyException as err:
37+
print(err)
38+
return
39+
40+
result = lookup.result
41+
42+
if not result:
43+
return
44+
45+
first_candidate = result[0]
46+
47+
# Here is an example of how to access the result of component analysis.
48+
componentAnalysis = first_candidate.analysis.components
49+
print("Component Analysis Results:")
50+
print(json.dumps(componentAnalysis.__dict__, default=lambda o: o.__dict__, indent=2))
51+
52+
if __name__ == "__main__":
53+
run()

0 commit comments

Comments
 (0)