Introduction
A contact book is a small database with real validation needs: names should be names, phone numbers should be dialable, emails should look like emails and duplicates should be caught. This project uses regular expressions for all of it.
Problem statement
Objectives
- Validate formats with regular expressions
- Normalise data before storing it
- Prevent duplicates
- Write CSV with the csv module
Features
- Phone and email validation and normalisation
- Duplicate phone detection
- Search by name, phone or email
- Tags and tag filtering
- Edit, delete and CSV export
Concepts demonstrated
- Regular expressions
- Data normalisation
- Uniqueness constraints
- CSV output
System architecture
clean_name, clean_phone and clean_email validate and normalise each field, raising ContactError. ContactBook stores Contact dataclasses, checks phone uniqueness and resolves list numbers through the sorted listing. run dispatches commands; export writes CSV through csv.writer into a StringIO.
Folder structure
Complete source code
main.py
What each file does
- main.py
- Validation helpers, the Contact and ContactBook classes, the command handlers and the loop.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
add asha rao | 98450 12345 | Asha.Rao@Example.com | friends, college add Ravi Kumar | +91-9123456789 | ravi@mail.co.in | work add Meera | 7012345678 | | family add Kiran | 12345 | kiran@example.com add Sana | 9988776655 | sana@ | friends add R2D2 | 9876501234 add Duplicate | 9845012345 list search 98450 search mail tag friends edit 2 email meera@family.org edit 3 phone 7012345678 edit 3 phone 9000012345 edit 9 name Nobody delete 1 export quit
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - '98450 12345' and '+91-9123456789' are stored as '+91 98450 12345' and '+91 91234 56789'.
- A 5-digit phone, 'sana@', the name R2D2 and a duplicate phone are rejected.
- Editing contact 3's phone to Meera's number is refused; the final export lists two contacts.
Common errors
The same number is stored in different formats
Normalise every phone number to one format before comparing or saving.
Commas in tags break the CSV
Let csv.writer quote fields, or join tags with ';' as export does.
Possible improvements
- Save contacts to a JSON or vCard file
- Add birthdays and an upcoming-birthdays report
- Import contacts from CSV
Additional challenges
- 1Detect likely duplicates with similar names (e.g. 'Ravi K' and 'Ravi Kumar').
- 2Support landline numbers with STD codes.
- 3Add undo for the last delete.
Built and tested it yourself? Mark it to update your dashboard.