Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 108 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_: .. image:: http://procasts.co.uk/media/procasts_sma ...

Refactoring with functions and doc strings [ID:249] (8/17)

in series: Python 101 - easygui and csv

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Refactoring code is the sensible way to make your programs more manageable. You need documentation to help explain the purpose of the code and to remind you how it works in the future.

Here we will refactor our program to add functions and documentation.

Created May 2007, running time 7 minutes.

import csv
import easygui

def getFileAndPath():
    "Get fully-qualified path to the csv file"
    fileAndPath = easygui.fileopenbox(title="Select .CSV file")
    print "Using:",fileAndPath 
    return fileAndPath

def getLinesInCSVFile(fileAndPath):
    "read lines in CSV file, return a list of these lines"
    linesInCSV = []
    reader = csv.reader(open(fileAndPath, "rb"))
    for row in reader:
        linesInCSV.append(row)
    return linesInCSV

# get the file-name
fileAndPath = getFileAndPath()
print fileAndPath
    
# read the CSV file
linesInCSV = getLinesInCSVFile(fileAndPath)
print linesInCSV
    
print
print "Headings:",linesInCSV[0]
for n in range(1,4):
    print linesInCSV[n]

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 412 (since July 30th)
  • Plays in last week: 0
  • Published: 91 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

You have Nicely transitioned to good coding style.

Thanks


Might change last line from

for n in range(1,4):

to

for n in range(1,len(linesInCSV)):

for a bit more robustness. (My two line CSV caused an error.)


Refactoring with functions and doc strings (8/17)

2:30 linesInCSV.append(row)

3:40 print linesInCSV

4:20 n in range(1,4) gives n = 1, 2, 3

5:30 print (alone on line) prints blank line

6:40 print "I am here" or "here1" "here2"


Great episode!!


Hi Ian great stuff easy enough to follow so far.

now that I have to work with real big files with more than 100.000 rows problems arise. do you have an idea how to handle csv files that big not only reading them instead do some mathematical processing with single variables in the rows

any videos here around?

greetings

ali


Been in and out of learning Python for sometime now...this is certainly reminding me of why I enjoyed it in the first place...can't wait to keep increasing my python-fu through these lessons...

Also helps to see that even the top programmers still use print "iam here" so the y know wherer they are in the procedures...thought that was just a cheap way i had left over from previous expinexperience, but I guess it works!!


Good Stuff!

Another simple but important lesson.

It's a trip how indenting the code is so important to the script running properly. Personally I think that's a good thing. Makes you write more readable code right from the start.

I'm using DrPython for my ide by the way

Thanks


Great series, and only up to #8. Love the way each video builds on the previous.


Review of Refactoring with functions and doc strings

I am enjoying this series. Good stuff.


Assuming you mean functions rather than linear blocks of code, the answer is that functions (and classes) make code more reusable, testable and readable.

You can find some more benefits here:

http://en.wikipedia.org/wiki/Function_%28computer_science%29

Ian.


Great video. I am, just like you said, not really a programmer, but someone very interested in learning. I work with programmers all the time, and they want to convert all their code to functions, what is the advantage of doing that?


Hi labuzz - great, glad you're enjoying the series.

The 'hero01' task would be a great idea to test how much you've learned, it would need some techniques that are beyond what's covered in this series though so you'll have to play around.

Good luck with that little challenge :-)

Ian.


hi there,

I am just starting to learn python ( I have a little bit of knowkledge in C...) and your videos are welcome (btw I am a CG artist working with 3d softwares )!

I like the idea -> print "I am here" for debugging.

It would be cool based on this idea to be able to print maybe something like this:

////////////////////HERE01////////////////////////

BUT with the complete line filled with the character / and the HERE centered. Maybe also the 01 should be incremented each time you have a new reference ( refcount based on a book that I am reading -> core python programming ) to this object. Also all this "#define" should be in another module.

I have not see the next videos yet.

tky for your work.


Very nice, being able to print the source code.


Hi Horst. I have added the original CSV file to the second video in this series (the one where the CSV file is written):

http://showmedo.com/videos/video?name=910010&fromSeriesID=91

Ian.


Nice jobs introducing those functions.

Suggestion: link to a wiki or a place where i can download the original csv-file that you used.


Thanks, good to know.


'rb' means read-binary. 'r' by itself means read as text. You can use 'b' as a modifier for reading and writing.

More information in Section 7.2 here:

http://docs.python.org/tut/node9.html#SECTION009200000000000000000

In this context we open the file for reading as binary because the end-of-line characters are handled differently on Windows, Mac and *nix. The csv reader needs Binary-mode to handle them correctly.

Ian.


What does the "rb" do on the line:

reader = csv.reader(open(fileAndPath, "rb"))

?


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com