Created
May 2, 2020 22:50
-
-
Save ThomasBrittain/16fb4287f71715ba44bd2570c60bae22 to your computer and use it in GitHub Desktop.
Day 1
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
John | 1234 Main St. | 555-123-4567 | john@samplewebsite.com | |
---|---|---|---|---|
Naomi | 234 Apartment 412 | 555-758-9865 | naomi@fakewebsite.net | |
Bob | Amazing Penthouse on Park Place | 555-785-5544 | bob@corporatewebsite.com | |
Sarah | 456 Main St. | 555-456-7845 | sarah@smallbusinessdomain.net |
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
import argparse, copy | |
class ContactInfo: | |
def __init__(self, name, address, phone_number, email): | |
self.name = name | |
self.address = address | |
self.phone_number = phone_number | |
self.email = email.strip("\n") | |
def to_string(self): | |
return ",".join([self.name, self.address, self.phone_number, self.email]) | |
def print(self): | |
print(self.to_string()) | |
def create_contact_info(data): | |
[name, address, phone_number, email] = data.split(",") | |
contact_info = ContactInfo(name, address, phone_number, email) | |
return contact_info | |
def search_address(search_terms, contact_info): | |
# Don't alter contact info | |
filtered_data = copy.deepcopy(contact_info) | |
for search_term in search_terms: | |
filtered_data = list(filter(lambda x: search_term in x.address, filtered_data)) | |
return filtered_data | |
def read_csv(input_file): | |
f = open(input_file, "r") | |
data = f.readlines() | |
f.close() | |
return data | |
def write_csv(contact_data, output_file): | |
f = open(output_file, "w") | |
for contact_info in contact_data: | |
f.write(contact_info.to_string() + "\n") | |
f.close() | |
def test(with_io=False): | |
search_terms = ["Main"] | |
if with_io: | |
csv_info = read_csv("data/day_1.csv") | |
contact_info = map(create_contact_info, csv_info) | |
else: | |
#Name,Address,Phone Number,Email | |
csv_info = \ | |
"John,1234 Main St.,555-123-4567,john@samplewebsite.com\n" \ | |
"Naomi,234 Apartment 412,555-758-9865,naomi@fakewebsite.net\n" \ | |
"Bob,Amazing Penthouse on Park Place,555-785-5544,bob@corporatewebsite.com\n" \ | |
"Sarah,456 Main St.,555-456-7845,sarah@smallbusinessdomain.net" | |
contact_info = map(create_contact_info, csv_info.split("\n")) | |
filtered_contact_info = search_address(search_terms, contact_info) | |
for contact_info in list(filtered_contact_info): | |
contact_info.print() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', '--input_file', help='Input file name', required=True) | |
parser.add_argument('-s', '--search_terms', help='Address search terms', required=True, nargs="+") | |
parser.add_argument('-o', '--output_file', help='Output file name', required=True) | |
args = parser.parse_args() | |
csv_info = read_csv(args.input_file) | |
contact_info = list(map(create_contact_info, csv_info)) | |
filtered_contact_info = list(search_address(args.search_terms, contact_info)) | |
write_csv(filtered_contact_info, args.output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment