[Tutor] Simple Python Address Book (Advice welcome!)

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon Jun 18 18:28:50 CEST 2012


> > def LoadAddresses():
> >      """ Loads all addresses from file and places
> >      them in tuple in form (bool, list), where the list is each
> >      row from the file (a person's name | email). """
> >      success, lines = (False, [])
> >      try:
> >          f = open(addressBookPath, 'r')
> >          lines = f.readlines()
> >          f.close()
> >          success, lines = (True, lines)
> >      except IOError as e:
> >          print 'Error opening address book: ', e
> >      return (success, lines)
> 
> The latest style preference in Python is to use with statements for file
> handling, so that would become:
> 
> try:
>    with f as open(.....):
>       success,lines = True, f.readlines()
> except IOError as e:
>    print ...
> return (success,lines)

> > def main():
> 
> >      (success, lines) = LoadAddresses()
> >      if not success:

No real reason to return success as you can do this instead for the
same effect. The only difference would be on reading an empty file.
Not sure what you would want it to do...

def main():
    lines = LoadAddressses()
    if not lines:



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list