diff --git a/base_app/base_app.py b/base_app/base_app.py deleted file mode 100644 index beca3c1..0000000 --- a/base_app/base_app.py +++ /dev/null @@ -1,21 +0,0 @@ -import flask, flask.views -import os - -app = flask.Flask(__name__) - -app.secret_key = "bacon" - -class View(flask.views.MethodView): - def get(self): - return flask.render_template('index.html') - - def post(self): - result = eval (flask.request.form['expression']) - flask.flash(result) - return self.get() - - -app.add_url_rule('/', view_func=View.as_view('main'), methods=['GET', 'POST']) - -app.debug = True -app.run() \ No newline at end of file diff --git a/chap6.py b/chap6.py index 7ac3454..2927127 100644 --- a/chap6.py +++ b/chap6.py @@ -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 \ No newline at end of file +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) \ No newline at end of file diff --git a/chap7.py b/chap7.py index 60674e1..9151e28 100644 --- a/chap7.py +++ b/chap7.py @@ -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? \ No newline at end of file +#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. \ No newline at end of file diff --git a/base_app/.DS_Store b/my_app/.DS_Store similarity index 100% rename from base_app/.DS_Store rename to my_app/.DS_Store diff --git a/my_app/base_app.py b/my_app/base_app.py new file mode 100644 index 0000000..846cac2 --- /dev/null +++ b/my_app/base_app.py @@ -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() \ No newline at end of file diff --git a/base_app/static/.DS_Store b/my_app/static/.DS_Store similarity index 100% rename from base_app/static/.DS_Store rename to my_app/static/.DS_Store diff --git a/base_app/static/bootstrap/css/bootstrap-responsive.css b/my_app/static/bootstrap/css/bootstrap-responsive.css similarity index 100% rename from base_app/static/bootstrap/css/bootstrap-responsive.css rename to my_app/static/bootstrap/css/bootstrap-responsive.css diff --git a/base_app/static/bootstrap/css/bootstrap-responsive.min.css b/my_app/static/bootstrap/css/bootstrap-responsive.min.css similarity index 100% rename from base_app/static/bootstrap/css/bootstrap-responsive.min.css rename to my_app/static/bootstrap/css/bootstrap-responsive.min.css diff --git a/base_app/static/bootstrap/css/bootstrap.css b/my_app/static/bootstrap/css/bootstrap.css similarity index 100% rename from base_app/static/bootstrap/css/bootstrap.css rename to my_app/static/bootstrap/css/bootstrap.css diff --git a/base_app/static/bootstrap/css/bootstrap.min.css b/my_app/static/bootstrap/css/bootstrap.min.css similarity index 100% rename from base_app/static/bootstrap/css/bootstrap.min.css rename to my_app/static/bootstrap/css/bootstrap.min.css diff --git a/base_app/static/bootstrap/img/glyphicons-halflings-white.png b/my_app/static/bootstrap/img/glyphicons-halflings-white.png similarity index 100% rename from base_app/static/bootstrap/img/glyphicons-halflings-white.png rename to my_app/static/bootstrap/img/glyphicons-halflings-white.png diff --git a/base_app/static/bootstrap/img/glyphicons-halflings.png b/my_app/static/bootstrap/img/glyphicons-halflings.png similarity index 100% rename from base_app/static/bootstrap/img/glyphicons-halflings.png rename to my_app/static/bootstrap/img/glyphicons-halflings.png diff --git a/base_app/static/bootstrap/js/bootstrap.js b/my_app/static/bootstrap/js/bootstrap.js similarity index 100% rename from base_app/static/bootstrap/js/bootstrap.js rename to my_app/static/bootstrap/js/bootstrap.js diff --git a/base_app/static/bootstrap/js/bootstrap.min.js b/my_app/static/bootstrap/js/bootstrap.min.js similarity index 100% rename from base_app/static/bootstrap/js/bootstrap.min.js rename to my_app/static/bootstrap/js/bootstrap.min.js diff --git a/base_app/static/jquery-1.7.1.min.js b/my_app/static/jquery-1.7.1.min.js similarity index 100% rename from base_app/static/jquery-1.7.1.min.js rename to my_app/static/jquery-1.7.1.min.js diff --git a/base_app/static/music/AllThingsGo.wav b/my_app/static/music/AllThingsGo.wav similarity index 100% rename from base_app/static/music/AllThingsGo.wav rename to my_app/static/music/AllThingsGo.wav diff --git a/base_app/static/music/Shempi.wav b/my_app/static/music/Shempi.wav similarity index 100% rename from base_app/static/music/Shempi.wav rename to my_app/static/music/Shempi.wav diff --git a/base_app/static/style.css b/my_app/static/style.css similarity index 100% rename from base_app/static/style.css rename to my_app/static/style.css diff --git a/base_app/templates/.DS_Store b/my_app/templates/.DS_Store similarity index 100% rename from base_app/templates/.DS_Store rename to my_app/templates/.DS_Store diff --git a/my_app/templates/base.html b/my_app/templates/base.html new file mode 100644 index 0000000..5832d50 --- /dev/null +++ b/my_app/templates/base.html @@ -0,0 +1,22 @@ + +
+Welcome back {{session['username']}}!
+ + {% else %} +A login in required:
+ + {% endif %} + {% with messages = get_flashed_messages() %} + {% for message in messages %} + {{message}} + {% endfor %} + {% endwith %} +{% endblock %} \ No newline at end of file diff --git a/my_app/templates/music.html b/my_app/templates/music.html new file mode 100644 index 0000000..7c4a251 --- /dev/null +++ b/my_app/templates/music.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% block body %} + +
+ {% for message in messages %}
+{{message}}
+ {% endfor %}
+
+ {% endif %}
+ {% endwith %}
+
+{% endblock %}
+{% block nav %}
+
+ {% for message in messages %}
+{{message}}
+ {% endfor %}
+
+ {% endif %}
+ {% endwith %}
+{% endblock %}
+{% block nav %}
+