Putting into practice the knowledge gained in An Introduction to Interactive Programming in Python (coursera.org)

Based on this post .
In the past 2012, I, like millions of other users, discovered free online training for myself. It all started with Codecademy, a great startup . Great courses on JavaScript, jQuery, Python, Ruby and others took up all your free time. A side effect was the practice of reading in English. By the middle of the year, the available lessons were over and I became interested in other sites where you can continue self-education. Just at that time articles about Coursera became frequent on Habré and I decided to try.
The first course to grab attention was An Introduction to Interactive Programming in Python by Rice University. Without thinking twice, I joined the slender ranks of online students.

Part 1. Back to school

From the very first video lecture, the teachers of the course (Joe Warren, Scott Rixner, Stephen Wong, John Greiner) arrange the students for a friendly atmosphere and a cheerful mood. Suffice it to say that the first project to study was the writing of a console version of the game Stone, widely known among lovers of The Big Bang Theory. Scissors. Paper. Lizard. Spock. ” For those who don’t know, here are her rules .
All programming in this course takes place in a special online sandbox , which is very convenient, since you do not have to carry source codes and distributions with you everywhere, and you can save all your experiments “in the clouds”. It is certain that the local public already knows how the learning process works in Coursera. Lecture topics and weekly projects were as follows:

  1. Expressions, variables, functions, conditionals. (Project "Rock-Paper-Scissors-Lizard-Spock" game)
  2. Event-driven programming, local and global variables, buttons and input fields. (Project “Guess the Number” game)
  3. The canvas, static drawing, timers, interactive drawing. (Stopwatch Project: The Game)
  4. Lists, keyboard input, motion, positional / velocity control. (Project "Pong" game)
  5. Mouse input, more lists, dictionaries, images. (Project “Memory” game)
  6. Classes, tiled images. (Project "Blackjack" game)
  7. Acceleration and friction, spaceship class, sprite class, sound Spaceship from. (Project "RiceRocks" game)
  8. Sets, groups of sprites, collisions, sprite animation. (Project Full "RiceRocks" game)

Each project is described in detail by the teachers at the end of the lecture. Templates are provided for projects that already contain global variables, class constructors, and helper functions. It remains only to write the necessary methods and implement the output to the screen. I will not provide links to completed tasks in case anyone decides to take this course. I did all the work as close as possible to the task. And here is the final project of a comrade who went beyond the necessary minimum and added all sorts of prettiness: http://www.codeskulptor.org/#user7-0WgaPD23z9-0.py . By the way, -0at the end of the URL, this is the version of the file. With each save, the version is incremented and you can always see previous iterations. Such an elegant Source Control.
There is no exam at the end of the course, which is probably why there is no evidence of its successful completion. However, during the course, a screencast competition was held among students, as a result of which two winners received an iPad.

Part 2. Bydlokod

Having finished the course with an average score of 99.46 / 100, I certainly felt like an almighty programmer. And just in time, a small task came up, which I did not fail to solve using the newly acquired knowledge.
The task was as follows: for an existing directory with a bunch of documentation in .pdf format, generate an HTML page with a list of links to files sorted by subdirectories. Links should be readable, which means the file name is not good and you need to go into every PDF and read the title of the document.
A little smoking of manuals on the pyPdf and markup modules and quite a bit of copy-paste, and something happened:

pdfdir2html.py
import os
import sys
from datetime import date
from pyPdf import PdfFileReader
import markup
import shutil
def getPdfTitle(filename):
    if not filename.endswith('.pdf'):
        return ""
    input1 = PdfFileReader(file(filename, "rb"))
    if not input1 or input1.getIsEncrypted():
        print ".file " + filename + " is encrypted..."
        return filename.split(os.sep)[-1]
    if input1.getDocumentInfo() == None or input1.getDocumentInfo().title == None:
        print ".file " + filename + " has no title..."
        return filename.split(os.sep)[-1]
    return "%s - %s" % (input1.getDocumentInfo().author, input1.getDocumentInfo().title)
def create_html_list(rootpath):
    rootpath = rootpath.rstrip(os.sep)
    start_level = rootpath.count(os.sep)
    page = markup.page( )
    page.init( 
        title="Pdf File List", 
        css=( 'bootstrap.css', 'style.css' ), 
        script={ 'script.js':'javascript' } 
        )
    page.div(class_='wrapper')
    for root, dirs, files in os.walk(rootpath):
        present_level = root.count(os.sep)
        actual_level = present_level - start_level
        caption = os.path.realpath(root).split(os.sep)[-1]
        if actual_level == 0:
            page.h1( caption, class_='caption' )
        elif actual_level == 1:
            page.h2( caption, class_='caption' )
        elif actual_level == 2:
            page.h3( caption, class_='caption' )
        elif actual_level == 3:
            page.h4( caption, class_='caption' )
        else:
            page.h5( os.path.relpath(root), class_='caption' )
        if len(files) < 1:
            continue
        file_list = [file for file in files]
        # begin list
        page.ol( class_='list' )
        for filename in file_list:
            title = getPdfTitle(os.path.relpath(root) + '\\' + filename).encode('ascii', 'ignore')
            if title == "":
                continue
            # to escape special characters
            title.encode('ascii', 'ignore')
            # list item
            page.li( class_='item' )
            page.a(title, href=os.path.realpath(root) + '\\' + filename, class_='link')
            page.li.close()
        # end list
        page.ol.close( )
    today = date.today()
    today.strftime("%d-%m-%Y")
    page.div("Last list refresh: " + str(today), class_='time')
    page.div.close()
    # print page
    # writing page to index.html file
    index_html = open('index.html', 'wt')
    index_html.write(str(page))
    index_html.close()
def copy_files(files, dst):
    for file in files:
        shutil.copy2(file, dst + "\\" + file)
def usage():
    print "Usage: pdfdir2html @path_to_pdf_files"
if __name__ == '__main__':
    print "Generate HTML list from folder with pdf files"
    if len(sys.argv) == 1:
        print "No arguments given!"
        usage()
    elif len(sys.argv) == 2:
        destination = str(sys.argv[-1])
        print ".converting folder: " + destination + "..."
        create_html_list(destination)
        print ".copying files..."
        copy_files(['index.html', 'bootstrap.css', 'style.css'], destination)
        print ".done"
    else:
        print "Too much arguments!"
        usage()

The script scans the subdirectories, reads the title of each PDF, and creates the index.html file in the target folder. For the sake of beauty, I added a little CSS to it, but these are the details ...
As it turned out, there are encrypted files among the documentation, which although they open normally with any PDF reader, but for some reason do not allow you to read your header using getDocumentInfo().title. In this case, the link will still be the file name.
Markup worked perfectly for generating HTML code, everything is simple and beautiful with it, for example:

    page = markup.page( )
    page.init( 
        title="Pdf File List", 
        css=( 'bootstrap.css', 'style.css' ), 
        script={ 'script.js':'javascript' } 
        )
    page.div(class_='wrapper')
    ...
    page.div.close()
    print page


Part 3. Where to go

Although the above code does not use the OOP concepts taught in the Interactive Programming in Python course, it was this course that prompted my interest in using new languages ​​and studying modern programming patterns. At the moment I have the following courses in line:


And this is only on Coursera, and yet there is Udacity , fantastic Code School , edX with amazing BerkeleyX and MIT courses, and the list goes on and on.
One cannot but mention the initiative of Russian- speaking comrades from the Hexlet University startup “Free University” . I wish this development project an explosive pace!
Now the main problem was to find free time for all this study, as well as the painful choice of the best of several courses that go simultaneously.

Also popular now: