Lectures and Assignments

Lecture 66: Digital Circuit Simulator

posted Jun 4, 2013, 5:48 AM by Samuel Konstantinovich   [ updated Jun 4, 2013, 5:48 AM ]

I will demo the digital circuit simulator, here is a link so you can access it.

Lecture 65: + SPOT HW*

posted May 31, 2013, 5:42 AM by Samuel Konstantinovich   [ updated May 31, 2013, 7:10 AM ]

You can work on your spot assignment, and/or work on your final project. 

SPOT Homework assignment: (The more you do in class, the less you have to do at home.)
Exercises 5 and 3 (3 requires some reading, 5 is more challenging but you have all the tools already)

To complete 3 you MUST read examples, test them out, play with them. They really help you understand how the commands are used.

The SPOT Homework Assignment will be submitted on the homework server by 11:59pm Sunday June 2nd. (A slot will be created by the end of the day today DONT submit in the wrong slot!)
Submit a plain text file in the format:





Lecture 64

posted May 29, 2013, 5:38 AM by Samuel Konstantinovich   [ updated May 29, 2013, 5:38 AM ]

You will be looking at an Assembly like language called spot.

This page has all of the spot documentation/examples/problems.

Ultimately you will have to complete a set of problems over the weekend, so you should do your best to learn spot now to make that assignment easier!

Lecture 63: Final Project Requirements

posted May 28, 2013, 5:49 AM by Samuel Konstantinovich   [ updated May 28, 2013, 5:49 AM ]

Teenagers can no longer grow up without being reminded of their mistakes because a full record of their lives is now stored on the internet.
-Eric Schmidt
Executive Chairman of Google

Overall Requirements:
-Each project should be of similar scope and difficulty as the sample ideas from earlier. 
-You should have some kind of register/login mechanism.
-You should have a reason to view/interact with other registered users. Messages, Profiles, Rankings etc.
-Each user should have some data that persists across multiple login attempts.
-One of your group is responsible for maintaining a record of your project over time, the other member(s) should synchronize with them. The format of storing your project is outlined below.
-You should have multiple versions of your project as the weeks progress.
-Each function should be written by a single person, and you should document this in a comment when you WRITE the function. As a result EVERY version should be completely commented. You should not try to comment them retroactively. 
-Every version should be working without errors, but 'working' doesn't mean it has all features enabled. 
-Every version goes into a new folder called 'versionxx' where xx is the version number. 
-All of your images, data files, etc. should go in the data directory, this way you only have copies of the html/py files. This means that your data files must link to "../data/filename"  (up a directory, into data, then file name)
-I will grade the highest version you have that is time-stamped before the late deadline.

Folders you need:

Some of you made the mistake of uploading your versions at the last minute. Every timestamp is created when you UPLOAD a file. The timestamp is not when you write the file! Please upload each working version AND TEST IT when you complete the version. If you do not do this, it looks like you waited to the last minute. 

Lecture 62:

posted May 22, 2013, 6:21 AM by Samuel Konstantinovich   [ updated May 28, 2013, 5:08 AM ]

1. Final Exam June 5th and or 7th to be decided soon.
2. You are getting one last day to your survey project in class. It should be completed by Friday.
3. You need to pick partners for groups of 2 or 3 for a final project, you will have a choice between 3-5 options for the project to be announced soon.
4. By Friday I would like suggestions for website based final projects. Well thought out developed ideas may make it into the choice of projects. You should be prepared to give a 1-2 minute explanation of your idea. I will ask for a few volunteers to explain their suggestions, and answer some questions. I will also pick a few random people to do this. Be prepared!

Some rough Ideas we have so far:
1. A forum with registered users. It would allow users to make posts, comments, and upvote/downvote posts/comments. (Think a simple Reddit)
2. A game matching site, that allows a registered user to other registered users. If the challenge is accepted: the two people play a turn based game. (Think a simple version of words with friends with a simple game like tic-tac-toe)
3. A site that allows registered users to create questions/choices for a survey, then link others to the completed survey to get results. (Think Simple Google forms)
4.?Maybe your idea here!
5.?Maybe your idea here!

Important things to remember:

1. AnalysisV2 are due 10pm tonight or (EDIT) 10pm on Tuesday. 
#for those of you that asked about the deadline over the weekend, I really should make it 8am... but originally I wrote 10pm, so I will honor that. 
1b. Your survey labs should be completed by tonight.
2. Final Projects are due the 10th , by 8pm but you will not incur late penalties until the 15th at 8pm. Please be mindful of your deadlines. 
3. Final Exam 5th. (Maybe 7th)
4. Days to work on project in class: 
 Friday 24th 
 Tuesday 28th 
 Wednesday 29th
 Friday 7th 
5. You must partition your final projects. Each person must identify what functions you wrote. If you want to work on a function together, split it up into sub functions. Basically think of it like everyone has their own functions that they are responsible for, they can get help/suggestions/etc from others, but the functions you are meant to do are supposed to be your own work. It should be very clear who wrote what, don't say half/half on a function. Most of the work will be done independently, time spent together should be for planning, and delegating tasks.

Lecture 61:

posted May 21, 2013, 6:14 AM by Samuel Konstantinovich   [ updated May 21, 2013, 7:11 AM ]

Demo1:  Self referencing forms, and multiple buttons.

print 'content-type: text/html'
#Content types are case-insensitive.

import cgi,cgitb
form = cgi.FieldStorage()

<head><title>self reference!</title></head>

if len(form)==0:
    page+='''<form action="selfreference.py" method="get">
text:<input type="text" name="textfield1" value="default string here">
<input type="submit" name="submitbutton1">
<input type="submit" name="submitbutton2">
<input type="submit" value="click">
    page+='notice the default value for submit buttons are "Submit", but you can have different '
    page+='names or different values to identify which button was pressed.<br> also notice that '
    page+='a submit button with no name does not show up in the field storage.'
print page

Demo 2:  Printing a list in order...

Use the sorted command:

Pay attention to how to sort in different ways:
>>> students = [
        ['john', 'A', 15],
        ['jane', 'B', 12],
        ['dave', 'B', 10],
>>> sorted(students, key=lambda f: f[2])   # sort by age
[['dave', 'B', 10], ['jane', 'B', 12], ['john', 'A', 15]]
key = lambda f: f[2] is defining what must be done to each element f of the list students. f is a function name being defined (remember lambda in scheme)    This says sort the list of lists, by the value at index 2.

1: you could compare a regular list of strings: ['abby', 'dave','harry','amy']
key=lambda f: f.upper()    #ignore case
key=lambda f: f.upper().strip(' ')  #ignore case and strip spaces

2: You could compare lists of lists, where the inner lists have strings:  [ ['123', 'bob','30'], ['125', 'baby','20'], ['113', 'amy','10'] ]
A: to ignore case:
f: f[1].upper()    #sort the list of lists, by the value at index 1, and make them upper before you try sorting

B. or list of string like numbers:
f: int(f[3])     #sort the list of lists by the value at index 3, but convert them to integers before you compare

Lecture 60: Survey Project

posted May 20, 2013, 5:42 AM by Samuel Konstantinovich   [ updated May 20, 2013, 5:45 AM ]

You have 2 class lab days to complete this project. It should be completed no later than Friday. 

In this project you will create a website where users can register with a name/password, fill out a survey and get the results. 
Here is an example of what you might end up with:

1. Link this on your home.html page.

2. Your website should have the following functionality:
  • Registering to vote (You should be done with this already, use your prior registration code! )
    • People should be able to enter a username and password and write that information to a file with all the registered users.
    • If a username is already taken, display a message saying they must pick a different name and do nothing else
  • Voting
    • People should be able to enter their username/password, answer the questions inside the survey and then submit their votes.
    • Once the form is submitted, the votes for each questions should be written to a file, the username should also be written to a file containing the names of everyone that has already voted. Then display all of the voting results, showing the totals for each answer. (For example, you could display a bar graph, or just print out the totals).
    • If a username has not been registered, display a message saying so and do nothing else.
    • If a username has already been used to vote, display a message saying so and do nothing else.
3. Technical details. What files should you have and what should they do?
  • Text files
    • You'll need 3 text files to keep track of all the information for this website, all of them will need to have write permissions.
      • registered-voters.txt
        • Contains a list of all the usernames/passwords.
        • You should have it hashed from the last assignment. If you do not, I will assume you didn't complete that assignment.
        • You can optionally include the email address in the registration.
        • Each line should follow this format: username:password
          • Example line: khan:12345
          • Example line (hashed): khan:01AC9B32F3EE04920F200
      • voters.txt
        • Contains a list of all the usernames that have already voted
        • You just need one line in this file, where each name is separated by commas.
          • Example line: dw,zamansky,brooks,konstans,leem
      • votes.txt
        • Contains all of the survey questions and their results
        • Each line should follow this format: question id:question:result0,result1
          • Example line: 0,How awesome is Mr Dw?,9001,3,2,9001,3,3,5,5
          • Notice that every time a new vote is registered, a new comma and value should be added to the end of the line (you don't have to split to do this)
      • Create each file before doing any other work. voters.txt and registered-voters.txt should be empty to start. votes.txt should have all the question ids and questions entered, but no vote values to start
  • HTML Form
    • Your form must have the following fields
      • Username
        • text field
      • Password
        • password field
      • Register button
        • submit
      • Questions
        • You must have at least 2 questions
        • You can use any field type, but radio buttons would be the easiest to work with.
      • Submit button
        • submit
    • Note that the register and submit buttons will be the same input type, make sure you use different names and values for each so you can differentiate them in your python program.
    • Make sure you have text that identifies/describes each input element.
    • You may add more features to the HTML page if you'd like
  • Python program
    • The program needs to be able to:
      • Register new users
      • Save survey results
      • Display survey results
      • Check if a user is registered
      • Check if a user has already voted
    • Things to look out for:
      • Make sure that you do not try to access a form element if the user did not enter anything for it
      • If there are any places where the user can enter text, make sure they can't break your page.

4. Challenges:
  • If you have finished all the requirements above, try some of these
    • I would prefer a fully working project that has fewer features than a broken one that tries (and fails) to do extra things.
  • When someone goes to vote a second time, instead of just displaying a message, show the current results.
  • Instead of having a separate html form and python file, just have a single py file do all the work
  • Try using different input elements instead of radio buttons
    • Not all input elements behave the same way
  • Present the survey results in more interesting ways

Lecture 59: Project analysisV2

posted May 17, 2013, 6:27 AM by Samuel Konstantinovich   [ updated May 20, 2013, 5:58 AM ]

Today: Get feedback on your site, and work on your analysisV2 version 2.

On the horizon:
I expect a degree of maturity from you, if you leave things to the last minute, you will not meet my expectations. As a result, I would like revisions on your next project. Every day you work on the files and update them, I want you to make a backup of working copies as you make progress:   stuff14.py  stuff15.py etc, where the number is the day of the month. Every version should work. If you break something UNDO your changes and try something different! Do not leave anything broken. In IDLE you can use the "save a copy as" command to make a backup and name it anything you want. This will keep the original file loaded and allow you to continue working. 

Version 2 of your analysis project will in a NEW folder in your public_html called: analysisV2 (exactly, with a capitalized V and lowercase everything else)
Old requirements with minor updates:
*You must make this directory, if you are in a group then each member must have the folder. 
*You must write appropriate descriptions AND explain your journey/challenges/epiphanies etc. (more on this later)
*You must read data from files, they cannot be hard coded in your project.
*You must make python generated information in your project. 
New Requirements:
1. The data+analysis page should be merged into one page.
2. The data should be broken down into multiple pages: Only show a small amount of the data table at once, and provide a "next" and "previous" link. If you have a small dataset, you may want/need to use more data to allow you to do this. Use a field variable page=1, page=2 etc, to determine which page of the table you are displaying.
3. The user should be able to select/search/restrict what is being displayed.
**Search for a specific item
**select a range of items, or category: eg. boys girls 100-200, top 10 etc.

Here is a demo to give you ideas:

Incentive to work on the project over time and not all the last night:

This will be due Friday the 24th 10pm. 
If you are 1 day late, then you get no credit. 

If you have made at least 5 days of meaningful revisions you can have up to Tuesday the 28th 10pm. 
If you submit on the 29th or later then you get no credit.  

To clarify revisions:
Revisions should be at least 30+ minutes of working, not 30 minutes of staring at the screen and writing nothing. 
The timestamps matter, so don't change them! Dont move them or touch them. 
You should be working on your single project.py, and save a separate copy as project##.py for each revision.
You should comment what you did so far at the bottom of your python file in the following format:

#20th Added the function to sort the table by a column, incorporated the function to display...
#21st Fixed the function for sorting so that it sorts numerically instead of alphabetically...
#22nd .......
Each revision should have all previous revision's comments as well so I can skip around and understand what was done. 

Lecture 58:

posted May 16, 2013, 6:03 AM by Samuel Konstantinovich   [ updated May 16, 2013, 12:54 PM ]

0. Lecture 55 had homework, in fact lecture 55 was just telling you what you should do at home. If you do not follow directions for that homework you will be missing out on FREE POINTS... Think of it like EXTRA CREDIT!!! that if you don't do, you lose points! (That is regular credit)

1. Some of you broke your local web server, here is a newer set of links to use which may help your problem:
For the oblivious: When I say Local Web server... this is what I am talking about.

2. Some of you had issues opening files on the local web server. Instead of putting the file in the same directory as your py file,  put the data files in the same directory as the webserver py file. Then you can open the files as if they were in the same directory as the py files. (Put the py files in cgi-bin, put the data files 1 directory above that, and pretend they are in the same directory)

3. Here is a link to a cgi tutorial which has more examples: http://www.tutorialspoint.com/python/python_cgi_programming.htm
You can look closely on how to handle cgi input. The only funny thing is how they print. They are using a different style print where they substitute things that come after the % symbol. You can print with strings and +'s if you want, its the same effect.

II. Practice Quiz:

On paper: (Read All questions in part II before you write anything)

1. Write an html form (assume the file is form1.html)  that would allow for a checkbox, a text field, and a submit button. They should be labeled and arranged in the format:
    Enter Name: ______________
    [x] I agree to terms of service    
The submit button should send the information to the form in question 2. 

2. Write a python file (assume it is named processForm1.py) that gets the information from the form and prints out one of several options:
a. "You didn't enter your name" when the name field was blank
b. Hello ______. Thank you for agreeing. (Their name should go in the blank) This happens when the checkbox is checked.
c. You did not agree!!!! This happens when the checkbox is not checked but the name is filled in.

These examples are exactly how it should work:

Prints the web page:    Hello Dave. Thank you for agreeing.

Prints the web page:    You did not agree!!!!

Prints the web page:    You didn't enter your name

3. Immediately upon reading this question, raise your hand to get my attention and give me a thumbs up so I know you followed directions. Do not do this after you completed 1 and 2. If you completed both 1 and 2, just say "Woot!"

III. Prototype your project upgrade.
You have thought about your project's upgraded version and should have some idea what you are doing. 
1. Make the HTML form (it does not need to be connected to anything yet) that you would want to use with you project and describe how you would want it to work. (in the html)
2. Share your ideas with two of your neighbors and get some feedback about what they think. 
3. Think of how you would implement the code to do what you want you can discuss that with neighbors.

IV. Warnings
1. If you get help from an outside source, you better understand the help if you incorporate it into your code. If you copy the code into yours then you don't understand. If you read their help, and you can then write it yourself that means you understand it. You may notice, I always give examples that you cannot generally copy/paste into your code unless its something that you don't really need to understand (like the md5 module) Your goal should be to read/test examples so you understand how to write it yourself. 

V. Solutions:
HTML form:
<form action="processForm1.py" method="get">
   Enter Name:<input type="text" name="username"><br>
   <input type="checkbox" name="box">I agree to terms of service<br>
   <input type="submit" value="SUBMIT">

Python file 1:
print 'content-type: text/html'
import cgi
form = cgi.FieldStorage()
if 'username' in form:
    name = form['username'].value
    if not 'box' in form:
        body= 'You did not agree!!!!'
        body= 'Hello '+name+'. Thank you for agreeing.'
    body= 'You didn\'t enter your name'

print header+body+foot

Python file 2:
print 'content-type: text/html'
import cgi,cgitb
form = cgi.FieldStorage()

#this version uses the getvalue method in the fieldstorage

name = form.getvalue('username')
#this will give None if the key isn't there
box = form.getvalue('box')
if name==None:
    body= 'You didn\'t enter your name'
elif box==None:
    body= 'You did not agree!!!!'
    body= 'Hello '+name+'. Thank you for agreeing.'


print header+body+foot

Lecture 57

posted May 14, 2013, 6:59 AM by Samuel Konstantinovich   [ updated May 14, 2013, 12:43 PM ]

You will probably want to use MD5:
Basic example: (You need to incorporate this into your code so test it out)
    import md5                                #this is required for any md5 command
    password="getsomepassword"     #you probably want to get the password from the form
    m=md5.new()                             #this initializes the md5
    m.update(password)                    #this puts the password into the md5
    hashedpass=str(m.hexdigest())    #this makes the encrypted password and stores it into hashedpass
    print hashedpass                        #you don't want to print it, but you can use this to test the code.

A. Account creation:
1. Create a form in create.html that opens create.py
-password *password field*
submit (create account)

2. Make an empty plain text data file in your home directory, then change the permissions with chmod: chmod 666 filename , to give it read/write permissions. This file will be used to store your password information.

3. Make the file to receive your form. Create.py will append to that file you created in your home dir.  '../thefileyoumade'  is an example of how you look up 1 directory.
The file should be in the format:


4. Improvements [finish everything before you implement this]:
Make it better by checking for duplicate name/email 
and if you find one, don't append the duplicate. 

B. Login testing:
1. Make a Login form: login.html that opens a py file login.py
-password *password field*
2. Make Login.py which will check if the name is in the password file.
when it finds the name, compare the hashed passwords.
3. If the login is successful display a webpage "login success", 
 otherwise display a webpage "login fail"
4. Improvements [finish everything before you implement this]:
4a. Display a table of all usernames when you log in.
4b. Keep track of how many times they logged in and display that with each username when you log in. (You must write to a new file to keep track of # of times logged in) This is challenging and requires multiple steps. Plan a little before you start it. 

1-10 of 66