Skip to content

Commit

Permalink
Create function check_phone_number in efi parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tim.reichard committed Feb 23, 2023
1 parent 2039511 commit 0c01e62
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ History
=======


v0.17.35 (2023-02-23)

* Create function check_phone_number in efi parsing.
* Update bytes read in detect_delimiter function from 4096 to 8192.


v0.17.34 (2023-02-16)

* Revise ethnicity check in EFI and increase truncation limit.
Expand Down
41 changes: 38 additions & 3 deletions aioradio/file_ingestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,20 +418,24 @@ def __post_init__(self):
'0': 'N'
}

def check_width(self, value: str, field: str, row_idx: int) -> str:
def check_width(self, value: str, field: str, row_idx: int, truncate_backward: bool=False) -> str:
"""Check field value and truncate if it is longer than expected.
Args:
value (str): Value
field (str): Column header field value
row_idx (int): Row index
truncate_backward (bool, optional): Truncate from back of value
Returns:
str: [description]
"""

if len(value) > self.field_to_max_widths[field]:
new_value = value[:self.field_to_max_widths[field]].rstrip()
if truncate_backward:
new_value = value[-self.field_to_max_widths[field]:].lstrip()
else:
new_value = value[:self.field_to_max_widths[field]].rstrip()
LOG.warning(f"[{self.filename}] [row:{row_idx}] [{field}] - '{value}' "
f"exceeds max width of {self.field_to_max_widths[field]}. Trimming value to {new_value}")
value = new_value
Expand All @@ -456,6 +460,25 @@ def check_name(self, value: str, field: str, row_idx: int) -> str:

return value

def check_phone_number(self, value: str, field: str, row_idx: int) -> str:
"""Check Phone Number logic.
Args:
value (str): Phone number value
field (str): Column header field value
row_idx (int): Row number in file
Returns:
str: Phone number value
"""

if value != '':
value = value.replace(' ', '')
value = "".join(filter(str.isdigit, value))
value = self.check_width(value, field, row_idx, truncate_backward=True)

return value

def check_gender(self, value: str) -> str:
"""Check Gender logic.
Expand Down Expand Up @@ -1054,6 +1077,18 @@ def check_name_efi(self, records: list[str], field: str, row_idx: int):
for idx in range(len(records)):
records[idx] = self.check_name(records[idx], field, row_idx + idx)

def check_phone_number_efi(self, records: list[str], field: str, row_idx: int):
"""Check Phone Number logic.
Args:
records (list[str]): List of a specific columns values
field (str): Column header field value
row_idx (int): Row number in file
"""

for idx in range(len(records)):
records[idx] = self.check_phone_number(records[idx], field, row_idx + idx)

def check_gender_efi(self, records: list[str]):
"""Check Gender logic.
Expand Down Expand Up @@ -1821,7 +1856,7 @@ def detect_delimiter(path: str, encoding: str) -> str:

delimiter = ''
with open(path, newline='', encoding=encoding) as csvfile:
data = csvfile.read(4096)
data = csvfile.read(8192)
count = -1
for item in [',', '\t', '|']:
char_count = data.count(item)
Expand Down
13 changes: 12 additions & 1 deletion aioradio/tests/file_ingestion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pytest

from aioradio.file_ingestion import (async_db_wrapper, async_wrapper,
from aioradio.file_ingestion import (EFIParse, async_db_wrapper, async_wrapper,
delete_ftp_file, establish_ftp_connection,
get_current_datetime_from_timestamp,
list_ftp_objects,
Expand Down Expand Up @@ -227,3 +227,14 @@ async def func(**kwargs):
print(f"Connection name: {name}\tConnection object: {conn}")

await func()


def test_check_phone_number():
"""Test check_phone_number in EFIParse."""

efi = EFIParse('')
number = efi.check_phone_number('+1 (512) 573-5819', 'CellPhoneNumber', 0)
assert number == '5125735819'

number = efi.check_phone_number('215â€"863-79', 'CellPhoneNumber', 0)
assert number == '21586379'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
long_description = fileobj.read()

setup(name='aioradio',
version='0.17.34',
version='0.17.35',
description='Generic asynchronous i/o python utilities for AWS services (SQS, S3, DynamoDB, Secrets Manager), Redis, MSSQL (pyodbc), JIRA and more',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 0c01e62

Please sign in to comment.