HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Simple Address Book- Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
booksimpleaddresspython

Problem

I am a Python beginner (well, a newbie programmer). I am creating a small address book, just as a fun project, nothing too serious. I would appreciate if you review it and advise me on how to make it better (not complete yet). Here's the link:

https://github.com/volf52/address-book

Here's the code:

main.py

```
import contact
import addressBook
import re
from sys import exit
import random

__author__ = 'Muhammad Arslan '

app = addressBook.addressBook(str(raw_input("Enter name of book (Will be created if doesn't exist) \n> ")))
main_menu = '\n1. Show all contacts.\n2. Add contact.\n3. Search.\n4. Delete a contact.\n5.Update contact.\n6. Exit\n\n>'

def exitProg():
exitMessages = ['You have my permission to die.']
print random.choice(exitMessages)
exit(0)

def getOption(prompt):
inp = raw_input(prompt)
try:
inp = int(inp)
except ValueError:
print 'You should have selected a proper option.'
return 13
return inp

def showContacts():
print 'show all'

def addContact():
flag = 13
while flag == 13:
exp = map(lambda x: re.compile(x), [r'^([a-zA-Z]+)$', r'^(\+)?(\d)+$', r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"])

fName = str(raw_input('Enter first name : ')).strip()
while not exp[0].match(fName):
fName = str(raw_input('\nWrong Input\nEnter (proper) first name : ')).strip()

lName = str(raw_input('Enter last name : ')).strip()
while not exp[0].match(lName):
lName = str(raw_input('\nWrong Input\nEnter (proper) last name : ')).strip()

pNum = str(raw_input('Enter phone number : ')).strip()
while not exp[1].match(pNum):
pNum = str(raw_input('\nWrong Input\nEnter (proper) number : ')).strip()

email = str(raw_input('Enter email(Blank for none) : ')).strip()
while not exp[2].match(email):
if not email:
break
email = str(raw_input('\nWrong Input\nEnter (pr

Solution

Like partially mentioned in the comments, you should use __setattr__(self, name, value) and __getattr__(self, name) for security if needed, and otherwise just allow direct access, instead of get_ and set_ functions.

You can also use @property on individual attributes.

Context

StackExchange Code Review Q#160242, answer score: 5

Revisions (0)

No revisions yet.