Engr 101

Prof. P. Brass

Friday afternoon 2.00-3.40

This is a module in the Engineering 101 course. The intention of Engineering 101 is to give starting students at CCNY a sampler of typical engineering work; the course consists of a lecture component, which should be delivered by engineers from industry, and two lab modules, one short, one long, on different engineering topics representative of work done in our school of engineering (GSoE).

This module is a computer science module within that plan. The topic of the module is the creation of a news-aggregator using the python programming language. This is a program that reads news-items from an rss-feed, and creates a webpage with links to these news-items.

This module topic was not created by me, but we will have to realize the plan as well as possible. On this page, I collect my teaching notes and examples.

  1. First lecture: First steps with the Python language

    A programming language is a formal framework to tell your computer what he should do. There are many programming languages (my favorite is still C), python is another one. To a programming language, you need an interpreter or compiler: something that translates your program into that sequence of 0/1-bits that a computer can really execute. A python interpreter is available for free, and is installed on each lab computer. It also comes preinstalled as part of most linux-distributions. Online-documentation is also available.

    Python can be used in two ways, either writing command after command in the interpreter, which immediately executes each command that is finished (but sometimes needs an empty line just to tell him a command is finished), or by writing a program in a file (with a .py extension) and then running that program. To write a program in a file, you need a text editor, not a word processor (which leaves all kinds of formatting information in your file). On windows, you can use `notepad', on linux I use `emacs', but there are many different text editors available, all of which do the same.

    Simple Python commands:

    The following are example programs that I created for the first lecture.
    1. weekdays.py illustrates a single for-loop, printing items from a list.
    2. food.py illustrates two nested for-loops, printing all combinations of two lists.
    3. subj.py illustrates one outer for loop containing two inner for loops at the same level.
    4. first_letter.py illustrates getting input from the user.
    5. palindrome.py illustrates accessing the input from the user, character by character.
    6. word_guess.py illustrates comparing user input with a hidden word.
    7. 3n+1.py illustrates a while loop doing some computation (the 3n+1/2-iteration) with a number input from the user.
    The following handouts I gave in the first lecture
    1. Handout 1 contains the previous sample programs as one text file
    2. Handout 2 contains two versions of a small test

  2. Second Lecture: Reading data and the use of library functions.

    To create a program that does something interesting, we need to access data external to the program. For this, we read from files on our computer, and from documents on the web. Operations on files are built into the language, whereas operations on documents from the web need a special library, but beyond that, they are very similar. We first have to open the document: this checks that the document we reference really exists and can be read by us. The function by which we open creates an object which represents the document, and our current state in reading it. To that object, we can then apply the readline function, which returns the next line from the document, and changes the state of the object to advance the position up to which we have read it. We can open the same document many times, we get independent objects representing our access to that document which differ only in the place up to which we have read.

    When we read and display a document line by line, we depend on the document having a suitable structure. When we try this on an image file, e.g., we will get garbage data. When we read a text file, there is no problem, but a word document cannot be displayed line by line, and many other file formats, e.g., pdf or ps, look different from the inside than they look when printed or displayed by suitable software on the screen. For documents on the web, the primary format is called html; html files contain much formatting information, but you still can guess what is going on.

    The following are example programs that I created for the second lecture.

    1. read_file.py illustrates reading from a file.
    2. read_url.py illustrates reading from an URL.
    3. print_url.py illustrates reading from an URL.
    4. find_links.py illustrates finding the links in an URL.
    The following handouts I gave in the second lecture
    1. Handout 3 contains two versions of a small test
    2. Handout 4 contains the previous sample programs as one text file

Peter Brass (peter@cs.ccny.cuny.edu)