Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions base_app/base_app.py

This file was deleted.

36 changes: 34 additions & 2 deletions chap6.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
# Enter your answrs for chapter 6 here
# Name:
# Name: Anthony Leonardi


# Ex. 6.6
def first(word):
return word[0]

def last(word):
return word[-1]

def middle(word):
return word[1:-1]

# Ex 6.8
def is_palindrome(word):
if(len(word) < 2 ):
return True
elif(first(word) == last(word)):
return is_palindrome(middle(word))
else:
return False

print is_palindrome('heh')
print is_palindrome('het')
print is_palindrome('anthony')
print is_palindrome('abcddcba')
print is_palindrome('')
print is_palindrome('a')


# Ex 6.8
def gcd(a, b):
if(b == 0):
return a
else:
return gcd(b, a%b)

print gcd(1, 3)
print gcd(5,3)
print gcd(10,2)
print gcd(5,15)
25 changes: 23 additions & 2 deletions chap7.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
# Enter your answrs for chapter 7 here
# Name:
# Name: Anthony Leonardi


# Ex. 7.5
import math
def estimate_pi():
k = 0
currSum = 0
SR = 0
multiplier = ((2*math.sqrt(2))/9801.0)
while True:
print k
term = ((math.factorial(4*k) *(1103 + 26390*k))/float(((math.factorial(k)**4) * 396**(4*k))))
currSum = currSum + term
SR = multiplier * currSum
if(abs(term) < 1e-15):
break
k = k+1
print 1/SR
return 1/SR

print estimate_pi()
print math.pi

# How many iterations does it take to converge?
# it takes 4 iterations before the term is less than 1e-15, but it takes only two iterations before the pi estimate converges on pi to 11 places.

# How many iterations does it take to converge?
#as a note I think the sample solution in the book is wrong. It looks like it's including the multiplier in the sum, rather than summing the terms,
# and applying the multiplier to the current sum alone.
File renamed without changes.
75 changes: 75 additions & 0 deletions my_app/base_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This is whre you can start you python file for your week1 web app
# Name: Anthony Leonardi

import flask, flask.views
import os
import functools

app = flask.Flask(__name__)

app.secret_key = "bacon"

users = {'jake':'bacon', 'anthony':'sausage'}

class Main(flask.views.MethodView):
def get(self):
return flask.render_template('index.html')
def post(self):
if 'logout' in flask.request.form:
flask.session.pop('username', None)
return flask.redirect(flask.url_for('index'))
required = ['username', 'passwd']
for r in required:
if r not in flask.request.form:
flask.flash("Error: {0} is required.".format(r))
return flask.redirect(flask.url_for('index'))
username = flask.request.form['username']
passwd = flask.request.form['passwd']

if username in users and users[username] == passwd:
flask.session['username'] = username
else:
flask.flash("Username doesn't exist or incorrect password")
return flask.redirect(flask.url_for('index'))

def login_required(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
if 'username' in flask.session:
return method(*args, **kwargs)
else:
flask.flash("A Login is required to see the page!")
return flask.redirect(flask.url_for('index'))
return wrapper

class Music(flask.views.MethodView):
@login_required
def get(self):
songs = os.listdir('static/music/')
return flask.render_template('music.html', songs=songs)

class Remote(flask.views.MethodView):
@login_required
def get(self):
return flask.render_template('remote.html')

@login_required
def post(self):
result = eval (flask.request.form['expression'])
flask.flash(result)
return flask.redirect(flask.url_for('remote'))



app.add_url_rule('/',
view_func=Main.as_view('index'),
methods = ['GET', 'POST'])
app.add_url_rule('/remote/',
view_func=Remote.as_view('remote'),
methods=['GET', 'POST'])
app.add_url_rule('/music/',
view_func=Music.as_view('music'),
methods = ['GET'])

app.debug = True
app.run()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions my_app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title> Episode 3 </title>
<link rel="stylesheet" type="text/css" href="/static/style.css"/>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" type="text/css" href="/static/style.css" />
<script type="text/javascript" src="/static/jquery-1.7.1.min.js"></script>
</head>
<body>
<div id="content">
{% block body %} {% endblock %}
</div>
<div id="nav">
Navigation:<br/>
<ul>
{% block nav %} {% endblock %}
<li><a href="{{url_for('remote')}}">View Photos</a></li>
<li><a href="{{url_for('music')}}">View Docs</a></li>
</ul>
</div>
</body>
</html>
File renamed without changes.
24 changes: 24 additions & 0 deletions my_app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}
{% block body %}
<h1>Welcome to anthony's basic google drive!</h1>
{% if 'username' in session %}
<p>Welcome back {{session['username']}}!</p>
<form action="{{url_for('index')}}" method="post">
<input type="submit" name="logout" value="logout"/>
</form>
{% else %}
<p>A login in required: </p>
<form action="{{url_for('index')}}" method="post">
<p>Username:<br/>
<input type="text" name="username" /></p>
<p>Password:<br/>
<input type="password" name="passwd"/></p>
<p><input type="submit" value="submit"/></p>
</form>
{% endif %}
{% with messages = get_flashed_messages() %}
{% for message in messages %}
{{message}}
{% endfor %}
{% endwith %}
{% endblock %}
31 changes: 31 additions & 0 deletions my_app/templates/music.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "base.html" %}
{% block body %}

<h2>Music</h2>
<audio controls ="controls" autoplay="autoplay"></audio>
<ul>
{% for song in songs %}
<li><a href="javascript:void(0);" onclick="playSong('{{song}}')">{{song}}</li>
{% endfor %}
</ul>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br>
Results:
<pre>
{% for message in messages %}
{{message}}
{% endfor %}
</pre>
{% endif %}
{% endwith %}
<script type="text/javascript">
function playSong(song){
console.log(song);
$('audio').attr('src', '/static/music/' + song);
}
</script>
{% endblock %}
{% block nav %}
<li><a href="{{url_for('index')}}">back </a></li>
{% endblock %}
26 changes: 26 additions & 0 deletions my_app/templates/remote.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "base.html" %}
{% block body %}

<h2>Remote Control</h2>
Enter Python to Execute:
<form action="{{url_for('remote')}}" method="post">
<center>
<input type="text" name="expression" />
<input type="submit" value="Execute" />
</center>
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<br>
Results:
<pre>
{% for message in messages %}
{{message}}
{% endfor %}
</pre>
{% endif %}
{% endwith %}
{% endblock %}
{% block nav %}
<li><a href="{{url_for('index')}}">back </a></li>
{% endblock %}
Binary file added screenshots/My_App.tiff
Binary file not shown.
14 changes: 13 additions & 1 deletion week2_answers.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# Please put answers for the week2 stand alone questions here
# Name:
# Name: Anthony Leonardi

3 Web Apps of similar size:
1. a simple read-only google drive w/login
- pictures page which shows the contents of a photo folder on your computer, when you click on a photo it's displayed.
- documents page which shows the list of documents in the folder -- when you click on a doc, you see the contents of the document (use the file stuff from the book)
2. a text manipulation site:
- page to type in text -- it echos back the word count
- page to type in text -- it reverses the words
- page to type in text -- swaps case of each letter
3. a style test page
- page contains a few different HTML elements
- each element associated with a text box for user to type css into. the app would apply the css to the corresponding element for user to see.