an introduction to python - modules & solving real world problems

15
An Introduction To Software Development Using Python Spring Semester, 2015 Class #24: Modules & Solving Real World Problems

Upload: blue-elephant-consulting

Post on 15-Jul-2015

140 views

Category:

Education


1 download

TRANSCRIPT

Page 1: An Introduction To Python - Modules & Solving Real World Problems

An Introduction To Software

Development Using Python

Spring Semester, 2015

Class #24:

Modules &

Solving Real World

Problems

Page 2: An Introduction To Python - Modules & Solving Real World Problems

What If Your Cool Functions Were In A Different File?

• What if your main code was in a file called “main” and your functions were in a file called “extras”? How could main use the functionality in extras?

• The import statement tells Python to include the extras.py module in your program. From that point on, you can use the module’s functions as if they were entered directly into your program, right?

Image Credit: www.qubewifi.commain-1

Page 3: An Introduction To Python - Modules & Solving Real World Problems

Python & Namespaces

• All code in Python is associated with a namespace.

• Code in your main Python program (and within IDLE’s shell) is associated with a namespace called __main__.

• When you put your code into its own module, Python automatically creates a namespace with the same name asyour module.

• So, the code in your module is associated with a namespacecalled __extras__.

Image Credit: wihdit.wihd.org

Page 4: An Introduction To Python - Modules & Solving Real World Problems

Python & Namespaces

• When you want to refer to some function from a modulenamespace other than the one you are currently in, you needto qualify the invocation of the function with the module’snamespace name.

• So, instead of invoking the function as encryptSS(patients[patientID][9]) you need to qualify the name as extras.encryptSS(patients[patientID][9]).

• That way, the Python interpreter knows where to look. Theformat for namespace qualification is: the module’s name, followed by a period, and then the function name.

Image Credit: www.greenforage.co.uk-2

Page 5: An Introduction To Python - Modules & Solving Real World Problems

Python & Namespaces

• If you use

from extras import deEncryptSS, encryptSS

the specified functions (deEncryptSS, encryptSS in this case) are added to the current namespace, effectively removing the requirement for you to usenamespace qualification.

• But you need to be careful. If you already have functions called deEncryptSS or encryptSS defined in your current namespace, the specific import statement overwrites your function with the imported one, which might not be the behavior you want.

Image Credit: vistaproblems.com-3

Page 6: An Introduction To Python - Modules & Solving Real World Problems

Where Does Python Look For Files To Import?

>>> import sys; sys.path

['', 'C:\\Users\\Owner\\Dropbox\\PolytechCourse Notes\\Example Code', 'C:\\Python34\\Lib\\idlelib', 'C:\\Windows\\system32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages']

Image Credit: customstoday.com.pk

Page 7: An Introduction To Python - Modules & Solving Real World Problems

There Are A Lot Of Python Libraries Available

Page 8: An Introduction To Python - Modules & Solving Real World Problems

Python Programming Challenge #1

• From Homework #3:

Finally, the U.S. Government is asking the hospital to report on just exactly who it is providing medical services to. Create a report that lists all of the cities that patients live in and provide a count for each city of how many patients come from that city. Print this list out in alphabetical order based on city name.

• How did you solve this part of the homework?

-1 Image Credit: www.proprofs.com

Page 9: An Introduction To Python - Modules & Solving Real World Problems

Python Programming Challenge #1

• Challenges:

– Create a list of the unique cities that people came from

– Sort the city names into alpha order

– Count the number of people from each city

Sets

Dictionaries

-2 -3 Image Credit: www.amvsmlm.com

Page 10: An Introduction To Python - Modules & Solving Real World Problems

Python Programming Challenge #2

• Hillsborough PTA Invoice File

• Problem: the annual invoices for all 197 Hillsborough County PTA local units are grouped together in one big file. Each unit only cares about it’s invoice.

• Process the file and create 197 files with one invoice per file. Include local unit name in file name for easy identification.

24 2014-2015 HCC Dues Invoices.docx -1 Image Credit: apollopta.ourschoolpages.com

Page 11: An Introduction To Python - Modules & Solving Real World Problems

What’s The Problem?

• Microsoft Word documents contain a lot of formatting commands in them.

• This makes them look like a binary file to Python. Python doesn’t like binary files unless you identify them as being binary files.

• We need to save this file as a text file.

24 2014-2015 HCC Dues Invoices.txt -2 Image Credit: pixshark.com

Page 12: An Introduction To Python - Modules & Solving Real World Problems

First Things First

• We are going to have to pick off the individual school’s name on the invoice.

• This will be used to create an output file name.

• The name is located on the 8th line of text in the file.

• We’ll have to store the first 7 lines of text for use later on.

-3 -4 Image Credit: www.behavior-change.net

Page 13: An Introduction To Python - Modules & Solving Real World Problems

What’s In Your Python Toolbox?

print() math strings I/O IF/Else elif While For

DictionaryLists And/Or/Not Functions Files ExceptionSets Modules

Page 14: An Introduction To Python - Modules & Solving Real World Problems

What We Covered Today

1. Modules

2. Refactoring

3. Real-World Python Programming Problems

Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/

Page 15: An Introduction To Python - Modules & Solving Real World Problems

What We’ll Be Covering Next Time

1. In-Class Team Presentations!

Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/