3-IO_OOP.pdf

3-IO_OOP.pdf - importing SciPy Example from...

  • No School
  • AA 1
  • 36

This preview shows page 1 - 11 out of 36 pages.

importing SciPy # Example from: # # incorrect from scipy import * # correct from scipy import integrate value, err = integrate.quad(func=pow, a=0., b=1., args=(5,)) value # integral of x^5 over [0,1]
Image of page 1

Subscribe to view the full document.

2 Administrivia File I/O Object-oriented Python Intro to NumPy
Image of page 2
Programming Languages and the system I Matlab encourages a separation between the program and the computer system I C provides powerful ways to control the computer system, but the code is verbose I Python makes it very easy to interact with the computer system in basic ways
Image of page 3

Subscribe to view the full document.

Python and the system I For scientific computing, this balance between power and ease of programming makes Python a popular choice I Today, we will focus on file I/O as our interaction with the computer system I Please check out the os library (operating system): .
Image of page 4
File reading Suppose we have a text file of chemical compounds: salt: NaCl sugar: C6H1206 ethanol: CH3CH2OH ammonia: NH3 We want to read this file and store the information in a dictionary.
Image of page 5

Subscribe to view the full document.

File reading f = open( ’compounds.txt’ , ’r’ ) ’r’ specifies that we want to read the file. f is now a file object
Image of page 6
File reading Print the entire contents of the file: f = open( ’compounds.txt’ , ’r’ ) contents = f.read() print contents
Image of page 7

Subscribe to view the full document.

File reading Print each line individually: f = open( ’compounds.txt’ , ’r’ ) for i, line in enumerate(f): print ’(Line #’ + str(i + 1) + ’) ’ + line Prints “(Line #1) salt: NaCl”, etc.
Image of page 8
File reading A verbose dictionary formulation: f = open( ’compounds.txt’ , ’r’ ) compounds = {} for line in f: split_line = line.split( ’:’ ) name = split_line[0] formula = split_line[1] formula = formula.strip() compounds[name] = formula f.close()
Image of page 9

Subscribe to view the full document.

File reading The “with” statement closes the file automatically. A more Pythonic implementation: compounds = {} with open( ’compounds.txt’ , ’r’ ) as f: for line in f: compounds[line.split( ’:’ )[0]] = \ line.split( ’:’ )[1].strip()
Image of page 10
Image of page 11

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern