forked from sc3/cook-convictions-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Management command for exporting CSV
Create a class, ``AddressAnonymizer`` for anonymizing the addresses. This leverages datamade's usaddress package, but we have to use a fork that I created that adds in really basic support for Python 3. Create a management command ``export_csv`` to output CSV of the anonymized disposition fields to standard output. Addresses sc3/cook-convictions#110
- Loading branch information
Showing
7 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import usaddress | ||
|
||
class AddressAnonymizer(object): | ||
"""Anonymize addresses to the 100 block""" | ||
skip_component_types = [ | ||
'AddressNumberSuffix', | ||
'OccupancyIdentifier', | ||
'OccupancyType', | ||
] | ||
"""Don't include address components of these types in the anonymized output""" | ||
|
||
def __init__(self): | ||
self._cache = {} | ||
|
||
def anonymize(self, address): | ||
try: | ||
return self._cache[address] | ||
except KeyError: | ||
parsed = usaddress.parse(address) | ||
anonymized = ' '.join(self._anonymize_parsed(parsed)) | ||
self._cache[address] = anonymized | ||
return anonymized | ||
|
||
def _anonymize_parsed(self, components): | ||
anonymized = [] | ||
for c, c_type in components: | ||
if c_type == 'AddressNumber': | ||
c = self._anonymize_address_number(c) | ||
elif c_type in self.skip_component_types: | ||
continue | ||
|
||
anonymized.append(c) | ||
|
||
return anonymized | ||
|
||
def _anonymize_address_number(self, n): | ||
if len(n) <= 2: | ||
return '0' | ||
else: | ||
return n[0:-2] + '00' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import csv | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from convictions_data.models import Disposition | ||
|
||
class Command(BaseCommand): | ||
help = ("Export disposition records to CSV removing personal information.") | ||
|
||
def handle(self, *args, **options): | ||
writer = csv.DictWriter(self.stdout, | ||
fieldnames=Disposition.objects.EXPORT_FIELDS) | ||
|
||
for disp in Disposition.objects.anonymized_values(): | ||
writer.writerow(disp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters