Skip to content

Commit f7b3783

Browse files
authored
Merge pull request #53 from smartystreets/eric/add-geo-reference
Add GeoReference and Generic Lookup Capabilities for the US Address Enrichment API
2 parents 6faf6d5 + c7a47e0 commit f7b3783

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed

examples/us_enrichment_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def run():
3333
smarty_key = "1682393594"
3434
try:
3535
results = client.send_property_principal_lookup(smarty_key)
36+
# results = client.send_generic_lookup(smarty_key, 'property', 'principal')
37+
# Uncomment the line above to try it as a generic lookup instead
3638
except Exception as err:
3739
print(err)
3840
return

smartystreets_python_sdk/us_enrichment/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from smartystreets_python_sdk import Request
22
from smartystreets_python_sdk.exceptions import SmartyException
3-
from .lookup import FinancialLookup, PrincipalLookup
3+
from .lookup import FinancialLookup, PrincipalLookup, GeoReferenceLookup, Lookup
44
from .response import Response
55

66

@@ -21,6 +21,16 @@ def send_property_principal_lookup(self, smartykey):
2121
l = PrincipalLookup(smartykey)
2222
send_lookup(self, l)
2323
return l.result
24+
25+
def send_geo_reference_lookup(self, smartykey):
26+
l = GeoReferenceLookup(smartykey)
27+
send_lookup(self, l)
28+
return l.result
29+
30+
def send_generic_lookup(self, smartykey, dataset, dataSubset):
31+
l = Lookup(smartykey, dataset, dataSubset)
32+
send_lookup(self, l)
33+
return l.result
2434

2535

2636
def send_lookup(client: Client, lookup):
@@ -47,6 +57,10 @@ def send_lookup(client: Client, lookup):
4757

4858
def build_request(lookup):
4959
request = Request()
60+
if lookup.dataSubset == None:
61+
request.url_components = lookup.smartykey + "/" + lookup.dataset
62+
return request
63+
5064
request.url_components = lookup.smartykey + "/" + lookup.dataset + "/" + lookup.dataSubset
5165

5266
return request

smartystreets_python_sdk/us_enrichment/lookup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
propertyDataset = "property"
22
financialDataSubset = "financial"
33
principalDataSubset = "principal"
4+
geoReferenceDataset = "geo-reference"
5+
noneDataSubset = None
46

57
class Lookup:
68
def __init__(self, smartykey, dataset, dataSubset):
@@ -15,4 +17,8 @@ def __init__(self, smartykey):
1517

1618
class PrincipalLookup(Lookup):
1719
def __init__(self, smartykey):
18-
super().__init__(smartykey, propertyDataset, principalDataSubset)
20+
super().__init__(smartykey, propertyDataset, principalDataSubset)
21+
22+
class GeoReferenceLookup(Lookup):
23+
def __init__(self, smartykey):
24+
super().__init__(smartykey, geoReferenceDataset, noneDataSubset)

smartystreets_python_sdk/us_enrichment/response.py

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def __init__(self, obj):
33
self.smarty_key = obj.get('smarty_key', None)
44
self.data_set_name = obj.get('data_set_name', None)
55
self.data_subset_name = obj.get('data_subset_name', None)
6-
self.attributes = get_attributes(self.data_set_name, self.data_subset_name, obj.get('attributes'))
6+
self.attributes = get_attributes(self.data_set_name, self.data_subset_name, obj.get('attributes', None))
77

88
def __str__(self):
99
lines = [self.__class__.__name__ + ':']
@@ -21,6 +21,8 @@ def get_attributes(dataset, data_subset, attribute_obj):
2121
return FinancialAttributes(attribute_obj)
2222
if data_subset == "principal":
2323
return PrincipalAttributes(attribute_obj)
24+
if dataset == "geo-reference":
25+
return GeoReferenceOutputCategories(attribute_obj)
2426

2527

2628
class PrincipalAttributes:
@@ -387,7 +389,16 @@ def __init__(self, obj):
387389
self.zoning = obj.get('zoning', None)
388390

389391
def __str__(self):
390-
return self.__dict__.__str__()
392+
lines = ['']
393+
for key, val in vars(self).items():
394+
if type(val) is list:
395+
lines.append(key + ': ')
396+
for item in val:
397+
for subkey, subval in vars(item).items():
398+
lines += ' {}: {}'.format(subkey, subval).split('\n')
399+
else:
400+
lines.append(key + ': ' + str(val))
401+
return '\n '.join(lines)
391402

392403

393404
class FinancialAttributes:
@@ -492,7 +503,16 @@ def __init__(self, obj):
492503
self.widow_tax_exemption = obj.get('widow_tax_exemption', None)
493504

494505
def __str__(self):
495-
return self.__dict__.__str__()
506+
lines = ['']
507+
for key, val in vars(self).items():
508+
if type(val) is list:
509+
lines.append(key + ': ')
510+
for item in val:
511+
for subkey, subval in vars(item).items():
512+
lines += ' {}: {}'.format(subkey, subval).split('\n')
513+
else:
514+
lines.append(key + ': ' + str(val))
515+
return '\n '.join(lines)
496516

497517

498518
def get_financial_history(financial_history_obj):
@@ -549,3 +569,104 @@ def __init__(self, obj):
549569
self.name_title_company = obj.get('name_title_company', None)
550570
self.recording_date = obj.get('recording_date', None)
551571
self.transfer_amount = obj.get('transfer_amount', None)
572+
573+
def __str__(self):
574+
return self.__dict__.__str__()
575+
576+
class GeoReferenceOutputCategories:
577+
def __init__(self, obj):
578+
579+
self.census_block = get_geo_reference_census_block(obj.get('census_block', None))
580+
self.census_county_division = get_geo_reference_census_county_division(obj.get('census_county_division', None))
581+
self.census_tract = get_geo_reference_census_tract(obj.get('census_tract', None))
582+
self.core_based_stat_area = get_geo_reference_core_based_stat_area(obj.get('core_based_stat_area', None))
583+
self.place = get_geo_reference_place(obj.get('place', None))
584+
585+
def __str__(self):
586+
lines = ['']
587+
for key, val in vars(self).items():
588+
if type(val) is list:
589+
lines.append(key + ': ')
590+
for item in val:
591+
for subkey, subval in vars(item).items():
592+
lines += ' {}: {}'.format(subkey, subval).split('\n')
593+
else:
594+
lines.append(key + ': ' + str(val))
595+
return '\n '.join(lines)
596+
597+
class GeoReferenceCensusBlock:
598+
def __init__(self, obj):
599+
self.accuracy = obj.get('accuracy', None)
600+
self.geoid = obj.get('geoid', None)
601+
602+
def __str__(self):
603+
return self.__dict__.__str__()
604+
605+
def get_geo_reference_census_block(geo_reference_census_block_obj):
606+
if geo_reference_census_block_obj is None:
607+
return None
608+
output = []
609+
output.append(GeoReferenceCensusBlock(geo_reference_census_block_obj))
610+
return output
611+
612+
class GeoReferenceCensusCountyDivision:
613+
def __init__(self, obj):
614+
self.accuracy = obj.get('accuracy', None)
615+
self.code = obj.get('code', None)
616+
self.name = obj.get('name', None)
617+
618+
def __str__(self):
619+
return self.__dict__.__str__()
620+
621+
def get_geo_reference_census_county_division(geo_reference_census_county_division_obj):
622+
if geo_reference_census_county_division_obj is None:
623+
return None
624+
output = []
625+
output.append(GeoReferenceCensusCountyDivision(geo_reference_census_county_division_obj))
626+
return output
627+
628+
class GeoReferenceCensusTract:
629+
def __init__(self, obj):
630+
self.code = obj.get('code', None)
631+
632+
def __str__(self):
633+
return self.__dict__.__str__()
634+
635+
def get_geo_reference_census_tract(geo_reference_census_tract_obj):
636+
if geo_reference_census_tract_obj is None:
637+
return None
638+
output = []
639+
output.append(GeoReferenceCensusTract(geo_reference_census_tract_obj))
640+
return output
641+
642+
class GeoReferenceCoreBasedStatArea:
643+
def __init__(self, obj):
644+
self.code = obj.get('code', None)
645+
self.name = obj.get('name', None)
646+
647+
def __str__(self):
648+
return self.__dict__.__str__()
649+
650+
def get_geo_reference_core_based_stat_area(geo_reference_core_based_stat_area_obj):
651+
if geo_reference_core_based_stat_area_obj is None:
652+
return None
653+
output = []
654+
output.append(GeoReferenceCoreBasedStatArea(geo_reference_core_based_stat_area_obj))
655+
return output
656+
657+
class GeoReferencePlace:
658+
def __init__(self, obj):
659+
self.accuracy = obj.get('accuracy', None)
660+
self.code = obj.get('code', None)
661+
self.name = obj.get('name', None)
662+
self.type = obj.get('type', None)
663+
664+
def __str__(self):
665+
return self.__dict__.__str__()
666+
667+
def get_geo_reference_place(geo_reference_place_obj):
668+
if geo_reference_place_obj is None:
669+
return None
670+
output = []
671+
output.append(GeoReferencePlace(geo_reference_place_obj))
672+
return output

0 commit comments

Comments
 (0)