Skip to content

Commit

Permalink
adding zipcode cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
iheitlager committed Apr 17, 2017
1 parent fa66f59 commit 9d237bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
26 changes: 24 additions & 2 deletions data_migrator/contrib/dutch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,35 @@ def clean_phone(v):
['+31620202020','+31620202020','+31201233456','+4402030203','+4402030203']
Args:
v: value to clean
v (str): value to clean
Returns:
cleaned phone number
str: cleaned phone number
'''

v = _PHONE_CHARS.sub('', v)
v = _INTERNATIONAL_ZERO_START.sub('+', v)
v = _MUNICIPALY_ZERO_START.sub('+31', v)
return v


ZIP_CODE=re.compile('^([0-9]{4})[\t ]*([a-zA-Z]{2})$')
def clean_zip_code(v):
'''Cleans a dutch zipcode
>>> [clean_zip_code(x) for x in ['1234 aa', '1234AB', '1234 Ba']]
['1234AA', '1234AB', '1234BA']
Args:
v (str): zipcode to clean
Returns:
str: cleaned zip code
'''
v = v.strip()
try:
z = ZIP_CODE.match(v).groups()
r = "%s%s" % (z[0], z[1].upper()) # Dutch zip code is 1234AB
except AttributeError:
r = v
return r
1 change: 1 addition & 0 deletions data_migrator/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def __init__(self, data_map, as_json=False, **kwargs):
self.as_json = as_json

def _value(self, v):
print self.name, v
if v is None:
return v
else:
Expand Down
7 changes: 7 additions & 0 deletions docs/ref/contrib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Contrib reference
.. autofunction:: data_migrator.contrib.read.read_map_from_csv


contrib.dutch
=============

``clean_phone``
---------------
.. autofunction:: data_migrator.contrib.dutch.clean_phone

``clean_zip_code``
---------------
.. autofunction:: data_migrator.contrib.dutch.clean_zip_code
14 changes: 13 additions & 1 deletion test/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import unittest

from data_migrator.contrib.dutch import clean_phone
from data_migrator.contrib.dutch import clean_phone, clean_zip_code

class TestDutch(unittest.TestCase):
def test_phone(self):
Expand All @@ -17,3 +17,15 @@ def test_phone(self):
]
for i, o in l:
self.assertEquals(o, clean_phone(i))

def test_zip_code(self):
'''test zip code'''
l = [
('1234 AB','1234AB'),
('1234ba','1234BA'),
('1234 ba','1234BA'),
('1 2 3 4 A B','1 2 3 4 A B'),
('blabla', 'blabla')
]
for i, o in l:
self.assertEquals(o, clean_zip_code(i))

0 comments on commit 9d237bf

Please sign in to comment.