Вы находитесь на странице: 1из 22

CS50 - Section 7

Sat, Nov 4
Agenda

Python

Flask & MVC

Jinja

SQL

Problem Set 7
Python Terminology
Class: A user-defined prototype for an object (~ struct)

The attributes are data members (class variables


and instance variables) and methods

Modules: ~ Library

Instantiate a class = Create an object using the


prototype
class Employee:

'Common base class for all employees'


empCount is a class variable
empCount = 0
whose value is shared among all
instances of this class
def __init__(self, name, salary):

self.name = name
__init__() is a special method,
self.salary = salary which is called class constructor
or initialization method that Python
Employee.empCount += 1
calls when you create a new
instance of this class.
e.g. jakub = Employee(“jason”,
def displayCount(self):
10000)
print "Total Employee %d" % Employee.empCount

To access attribute e.g.


def displayEmployee(self): jakub.displayEmployee()

print "Name : ", self.name, ", Salary: ", self.salary


You can pass functions as
arguments to function!
def execute(func, a, b):

func(a, b)

def add(a, b):

print(a + b)

def mult(a, b):

print(a * b)

execute(add, 2, 4) # 6

execute(mult, 2, 4)# 8
Decorators
decorators are functions which are used to modify the
behaviour of other functions

The primary purpose of the @app.route decorator in


Problem Set 7 and 8 is to associate a function that you
have written with a particular route (URL) in your
website, such that the function is called when you visit
that URL
def one(): def override(func):

return 1 def incr():

def override(func): return func() + 1

def incr(): return incr

return func() + 1

return incr @override

print(one()) # 1 def one(x):

one = override(one) return 1

print(one()) # 2 print(one()) # 2
Style in Python

PEP8
User submits a request (URL) => reaches controller

Controller uses the Model retrieve all of the information it needs from the
database

Controller organizes data and send to View


Model-View-Controller
A user requests to view a page by entering a URL.

The application matches the URL to a predefined route.

The controller action associated with the route is called.

The controller action uses the models to retrieve all of the necessary
data from a database, places the data in an array, and loads a view,
passing along the data structure.

The view accesses the structure of data and uses it to render the
requested page, which is then presented to the user in their
browser.
Jinja

Jinja is a template engine for building HTML pages


using Python.

We leave some parts as "fill-ins" using Jinja’s syntax

Templates extend our default layout

e.g. layout.html and login.html


SQL
What does SQL stand for?
SQL

Databases (Excel): a hierarchically organized set of


tables, each of which contains a set of rows and
columns

SQL (the Structured Query Language) is a


programming language whose purpose is to query a
database
SQL
Create database (excel file)

Create a table in that database (sheet)

Specify columns and type

Identify Primary key - Primary keys enable rows of a


table to be uniquely and quickly identified.

Is primary key a single column with unique key?


Types in SQLite

NULL

INTEGER

REAL

TEXT

BLOB
Some notes
Unlike in C, the CHAR data type in SQL does not refer to a single
character. Rather, it is a fixed-length string. In most relational
databases, including MySQL, you actually specify the fixed-length
as part of the type definition, e.g. CHAR(10).

A VARCHAR refers to a variable-length string.VARCHARs also


require you to specify the maximum possible length of a string that
could be stored in that column, e.g. VARCHAR(99)

When defining the column that ultimately ends up being your


table’s primary key, it’s usually a good idea to have that column be
an integer. Moreover, so as to eliminate the situation where you
may accidentally forget to specify a real value for the primary key
column, you can configure that column to autoincrement, so it will
pre-populate that column for you automatically when rows are
added to the table
SQL Syntax (pretty limited!)

INSERT

SELECT

UPDATE

DELETE
SQL
INSERT INTO <table>(<columns>)VALUES(<values>)

SELECT
<columns>FROM<table>WHERE<condition>ORDER
BY<column>

SELECT
<columns>FROM<table1>JOIN<table2>ON<predicate>

UPDATE <table>SET<column> =<value>WHERE<predicate>

DELETE FROM <table>WHERE<predicate>


Problem set 7
in login() : rows = db.execute("SELECT * FROM users
WHERE username = :username",
username=request.form.get(“username"))

Type of rows?

Error checking?

return render_template("index.html", entries = entries,


cash = cash)
Example of Machine
Learning in Python
Machine Learning!

Supervised Learning

e.g. Nearest Neighbor Classifier

Unsupervised Learning

e.g. k-means: http://shabal.in/visuals/kmeans/2.html

http://scikit-learn.org/stable/modules/neighbors.html
Feedback!

http://tinyurl.com/cs50withchi

Вам также может понравиться