Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Day 1
John 1234 Main St. 555-123-4567
Naomi 234 Apartment 412 555-758-9865
Bob Amazing Penthouse on Park Place 555-785-5544
Sarah 456 Main St. 555-456-7845
import argparse, copy
class ContactInfo:
def __init__(self, name, address, phone_number, email): = name
self.address = address
self.phone_number = phone_number = email.strip("\n")
def to_string(self):
return ",".join([, self.address, self.phone_number,])
def print(self):
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()
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")
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)
#Name,Address,Phone Number,Email
csv_info = \
"John,1234 Main St.,555-123-4567,\n" \
"Naomi,234 Apartment 412,555-758-9865,\n" \
"Bob,Amazing Penthouse on Park Place,555-785-5544,\n" \
"Sarah,456 Main St.,555-456-7845,"
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):
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