Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
David Willis
Full Stack JavaScript Techdegree Student 18,600 PointsPython Regex - Address Book Extra Credit
Here's my attempt at an address book using regular expressions. Any tips on making the code more concise or conventional is welcomed! Also, wasn't sure how to account for foreign names with symbols above certain letters, such as Österberg.
class Person:
def __init__(self, **kwargs):
self.name = kwargs.get('name', 'Galt, John')
self.email = kwargs.get('email', 'john_galt@gmail.com')
self.phone = kwargs.get('phone', '555-555-5555')
self.job = kwargs.get('job', 'engineer, hiddenworld')
self.twitter = kwargs.get('twitter', '@johngalt')
for key, value, in kwargs.items():
setattr(self, key, value)
def __str__(self):
return """
Name: {}
E-mail: {}
Phone: {}
Job and Company: {}
Twitter: {}
""".format(self.name,
self.email,
self.phone,
self.job,
self.twitter)
import re
import sys
from person import Person
names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()
class Address_Book:
def search_entries(self):
line = re.compile(r'''
^(?P<name>(?P<last>[-\w ]*),\s(?P<first>[-\w ]+))\t #Last and first names
(?P<email>[-\w\d.+]+@[-\w\d.]+)\t #Email
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t #Phone
(?P<job>[\w\s]+,\s[\w\s.]+)\t? #Job and company
(?P<twitter>@[\w\d]+)?$ #Twitter
''', re.X|re.M)
search_person = input("Enter the first or last name: ").title()
person = Person()
if any(search_person in names for names in line.findall(data)):
for match in line.finditer(data):
if search_person in match.groupdict()['name'].title().replace(',', ''):
person.name = match.groupdict()['name']
person.email = match.groupdict()['email']
person.phone = match.groupdict()['phone']
person.job = match.groupdict()['job']
person.twitter = match.groupdict()['twitter']
print(person)
else:
next_step = input("{} was not found. Enter [y]es to try again or [q]uit to exit ".format(search_person))
if next_step.lower() == 'q':
sys.exit()
else:
self.search_entries()
def __init__(self):
start_input = input("Welcome! Enter [s]earch to look up an " +
"address book entry or [q]uit to quit! ").lower()
if start_input == 'q':
sys.exit()
else:
repeat = True
while repeat != 'q':
self.search_entries()
print('\n'+'='*20)
repeat = input("[S]earch again or [q]uit?").lower()
sys.exit()
Address_Book()
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsWith the foreign letters, you could build out a function that would also use regex to find the foreign letters and return a string with them replaced with their 'regular' counterparts.
Or use something like Unidecode.
Kenneth Love
Treehouse Guest TeacherSo long as you're capturing \w and not literal ASCII characters (e.g. [a-zA-Z]), you're fine for catching characters with special symbols or from non-English character sets.
It's a little weird seeing camel case and underscores used together. class AddressBook: would be more common/Pythonic.
David Willis
Full Stack JavaScript Techdegree Student 18,600 PointsGood to know. However, I was thinking as a user, writing Osterberg would not return the relevant information. My search function would only yield the information if the person inputted Österberg. I guess I should assume the user would have the appropriate keyboard or font to type in Österberg as opposed to Osterberg.
In any case, thanks for the prompt feedback, Kenneth! Really enjoying your python course. Keep up the good work.