Python / Django Flashcards
(31 cards)
Python console I/O
Input:
def input(“String appears to prompt user to input”)
This function returns string from what user type
Output:
def print(“What to show in console”)
Python type casting
Cast to int: int( / some string for float etc / )
Python conditional statement
if boolean_expression: some expression elif another_boolean_expression: some expression else: some expression
Python sequence type
Python provides different array-like data structure for handling multiple related element with different feature: string, list, set, tuple, dict (key : value)
Mutable / Immutable sequence: whether the sequence or the sequence’s element can be modified, (ex: add element to sequence, change value in particular index)
Ordered / Unordered: whether swaping two position inside a sequence create a new sequence or it is still considered the same one
Some sequences have supported function, syntax:
vector.sort()
all_sequence.len()
Python lists
Ordered: Yes Mutable: Yes Syntax: names = ["Harry", "Ron", "Hermione"] Lists behave similar to C++ vector, can insert / append / remove elements, swap elements create a new python lists, elements can be modified. Can contains multiple similar elements. Supported function: append() insert() operator[] sort()
Python tuples
Ordered: Yes
Mutable: No
Syntax: point = (1.3, 4.2, 0.1)
Tuples behave like constant list, order matters and can contain multiple similar element, but when created cannot be modified anymore.
Python set
Ordered: No Mutable: Yes Set only store each value once, if add multiple simlar value, set only display that value once and ignore the others Syntax: sign = set() / create empty set / Support function: set.add(5) set.add(4) set.add(3) print(set) --- > {5, 4, 3} set.remove(4)
Python dict
Ordered: No
Mutable: Yes
Dictionary is Set of < Key : Value > pairs, each key in dictionary has corresponding value.
Syntax:
info = {
“name” : “Harry”,
“age” : 19,
“school” : “Hogwarts” / last one doesn’t need semi colon /
}
Support function:
Adding Key-Value: info[“degree”] = “bachelor”
Access value through key from dictionary:
print(info[“name”]) — > Harry
Python loops
Element wise loop
names = [“Harry”, “Ron”, “Hermione”]
for name in names:
print(name)
for char in “Harry”:
print(char)
Range loop
for i in range(6): / Loop from 0 to 6 - 1 /
print(i)
Python function and modules
Function defined in function.py def func(parameter): some statement return value In main.py to use function func, have 2 option name = "Harry" 1) from function import func print(func(name)) 2) import function print(function.func(name))
Python Object-Oriented Programming
Create User-defined class having attribute and behaviour method. Ex: class Flight(): / self is reference to that particular instance, not including self means static function (represent for whole class) cannot access to that instance attribute / def \_\_init\_\_(self, id, from, to, capacity): self.id = id self.from = from self.to = to self.capacity = 100 self.passengers = []
def add_passenger(self, passenger) if not seat_available(): return False else: self.passengers.append(passenger) return True
def seat_available(self): return self.capacity - len(self.passengers)
# Create a new flight with o=up to 3 passengers flight = Flight(3)
# Create a list of people people = ["Harry", "Ron", "Hermione", "Ginny"]
Attempt to add each person in the list to a flight
for person in people:
if flight.add_passenger(person):
print(f”Added {person} to flight successfully”)
else:
print(f”No available seats for {person}”)
""" Output: Added Harry to flight successfully Added Ron to flight successfully Added Hermione to flight successfully No available seats for Ginny """
Python Functional Programming
Decorator: function receive a function parameter, return a wrapper function, adding some preprocess and postprocess code for original function. Example: def fun(name): return f"Hello, {name}!"
def checking(name): def wrapper(name): if is_alpha(name): print(fun(name)) else: print("Invalid name, no hello") return wrapper
name = input(“Name: “)
/ decorator means running the modified version return from “checking” function /
@checking
fun(name)
Lambda function: short notation for input variable and return some element or modification of input. Syntax: square = lambda x : x * x Example: Nesting dicts inside a list people = [ {name: "Harry", house: "Gryffindor"}, {name: "Cho", house: "Ravenclaw"}, {name: "Draco", house: "Slytherin"}, ] Cannot use people.sort() since " operator < " not defined between dict and dict. Have to pass in function as variable for sort() to figure out the order.
name.sort(key = lambda person : person[“name”])
sort() will traverse whole list with the function from key parameter to get keys and sorting on those keys
Django
Python framework providing modules and feature helping ease the process of writing fully-fledge dynamics web applications by handling all the protocol and building blocks that every type of web applications does so that the programmers can focus on the intersting logic of building their web apps – > improve productivity
HTTP protocol
The protocol that defined has standard method for communication between clients and server by sending message for verification and routing then receive the demanded package from the server. An structure of the typical HTTP protocol signal:
From client: / GET is one type of HTTP message beside POST 1.1 is the HTTP version, host will be the URL client trying to access to/ GET / HTTP / 1.1 Host: www.example.com Server respond: HTTP / 1.1 200 OK Content-type: text / html
/ The 200 code means the GET process works well, no error happening, and the server respond with the right package in the type of html pages that the client browser will render to display /
Some prevalent HTTP protocol code
200: OK
301: Move page permantly
403: Access denied
404: Not found
500: Internet Server Error, the web applications having bugs so the programmer have to dive in and fix it
Starting Django project
Django project must be created and deploy inside an Python Virtual Machines for servering with localhost:
mkvirtual VM-names
Command for starting to create Django apps:
“django-admin startproject PROJECT_NAME”
Starting Django application
Django project contains multiple Django applications. Process for creating application inside Django projects:
- Create application folder in terminal: python manage.py startapp app-name
- Add newly created application into Django project:
In setting.py find list named INSTALLED_APPS from which Django decides what apps has been installed on this projects, and add ‘app-name’ to that list
Django application views
Each Django application’s folder contains views.py module handle what to display to user when visiting particular URL route. Typical functions / modules written in views.py:
from django.http import HttpResponse
def index (request): / request: Objects represent the
return HttpResponse(“Hello, world”) HTTP request user use to access
this web server of which attributes
contain all info about the request /
To define when this response actually be triggered – > define a specific URL route associate with this views.index modules.
Django URL route
In Django projects root folder and each Django application’s folder contains urls.py modules, handling the URL routing process by a pyramid system:
1) Django projects urls.py is the root of urls.py routing tree, contains the prefix part of the URL name deciding which applications to go inside the project. The typical root urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path(“admin / “,include(“admin.site.urls”),
path(“hello / “, include(“hello.urls”))
]
/ urlpatterns is python lists sequence contains all the URL path can be attached to move to some particular application by accessing that appliction’s urls.py module /
“hello / “: The URL link appears in the brower search bar
include() function access the given module (access hello folder then dive into its urls.py file) then traverse through the urlpatterns list to get the demand urls path.
2) Django application urls.py considered as the child, continue to search with the remaining part of the URLs for finding the right Http Response. The typcial application urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path(“”, views.index, name=”index”),
path(“ < str:name > “, views.greet, name=”greet”)
]
/ The views.py and urls.py contained in same folder, use “from . import” /
“”: The prefix path “ / hello” had been handled by the root urls.py this application’s urls.py just have to extending the path
The name parameter in path() make it easy to reference to this URL path from other parts of application, preventing hard coding the URL
< str:name > : convertible URL by parameterize the path. In views.greet respond:
/ the name parameter have the string values convert from < str:name > in the URLs; hence the same “name” /
def greet (request, name):
print(“Hello, {name.capitalize}”)
Searching 127.0.0.1:8000/hello/thinh – > Hello, Thinh
“thinh” passed into greet function as variable name
Django Templates
Instead response with string, response with .html file. In views.py:
from django.http import HttpResponse, render def greet (request, name): / name variable taken from < str:name > in URLs return render (request, "hello / greet.html", { / context dictionary for html templates / "name": name.capitalize, })
“name” will be variable passing into greet.html file with the value name.capitalize
In hello folder create templates folder create hello folder inside that then greet.html, reason for templates / hello / greet.html is preventing conflict between apps having same .html names
In greet.html:
< head > < title > Name < / title > / Double curly braces for context variable / < / head > < body > < h1 > Hello, {{ name }} < / h1 > < / body >
Django templates conditional logic
Inside views.py: import datetime def index (request): now = datetime.datetime.now return render(request, "newyear / index.html", { "new_year": now.date == 1 and now.month == 1, / Django paradigm: dictionary key will be the variable django templates be able to access to when render the HTML templates, dictionary value will be python variable value passing to templates / })
In: / templates / newyear / index.html < head > < title > Is it New Year < / title > < / head > < body > {% if new_year %} < h1 > YES < / h1 > {% else %} < h1 > NO < / h1 > {% endif %} < / body >
Django static files
In newyear folder application create static folder, inside that create newyear folder then styles.css
In templates / newyear / index.html, to include CSS file: {% load static %} < head > < title > Is it New Year < / title > < link href={% static "newyear / styles.css" %} / Avoid hard coding to css destination / "rel"=stylesheet > < / head > < body > {% if new_year %} < h1 > YES < / h1 > {% else %} < h1 > NO < / h1 > {% endif %} < / body >
Django templates loops
Create tasks application: 1) Terminal 2) Add application name to In views.py: tasks = ["bar", "boo", "baz"] def index (request): return render(request, "tasks / index.html", { "tasks" : tasks, })
In templates / tasks / index.html {% load static %} < head > < title > Todo Lists < / title > < link href={% static "tasks / styles.css" %} "rel"=stylesheet > < / head > < body > {% for task in tasks %} < li > {{ task }} < / li > {% empty %} / In case of empty tasks / < li > No tasks < / li > {% endfor %} < / body >
Django templates inheritance
Each Django applications templates folder tends to have multiple .html templates file that have the same structure. For example: the layout of the pages, the link to same styling file, same heading, etc.
If all the the .html files that intented to have the same layout was hard coding, when demands for modified the layouts of the web pages appears, all the html files have to be edited one by one, causing unefficiency.
So the ability to factor out the HTML code that all pages having in common is helpful design, comes in Django templates inheritance feature. Syntax:
For example having to file index.html for saving tasks and add.html for input new task to html form with the same layout, same styling, create an parents .html file for the layout layout.html:
{% load static %}
< head >
< title > Todo Lists < / title >
< link href={% static “tasks / styles.css” %} “rel”=stylesheet >
< / head >
< body >
Syntax
{% block body %} / body: user-chosen block’s name /
{% endblock %}
< / body >
The block body inside layout.html can be edited by all the files inherit from it.
In index.html extending layout.html: *Syntax* {% extends "tasks / layout.html " %} {% block body %} / The same code inside the layout.html file / < ul > {% for task in tasks %} < li > {{ task }} < / li > {% endfor %} < / ul > {% endblock %}
In task.html extending layout.html: {% extends "tasks / layout.html " %} {% block body %} / The same code inside the layout.html file / < form > < input type="text" name="task" > < input type="submit" > < / form > {% endblock %}
Django templates URL referencing by name given to that URL route
Remind in urls.py:
urlpatterns = [
path(“ / add”, views.add , name=”reference_name”),
]
In Django projects there some demands for changing between URLs triggering different views function by clicking the URLs link given the pages.
In task application, index.html links to add.html:
< a href=”task/add/” > Add new task < / a>
The url above was hard coding, demands changing the urls path only satisfied by both changing the urls in the path() function in urls.py and changing all urls in all html templates links < a > refering to that particular url.
Using the string given for name attribute given for each path in urls.py to refer to route without the risks of having to finding that spot to edit again
* Syntax *
< a href = “{% url ‘add’ %}” > Add new tasks < / a >
Whenever the urls route is edited inside path() in urls.py, Django will automatically figure out what the new urls path should be in all the html url links.