pierian data python django

errors and exception handling

try: code to be attempted(may lead to an error)
except: code will execute in case there is an error in try block
finally: a final block of code to be executed, ragardless of an error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try:
print('10' + 10)
except IOError:
print('you have input/output error!')
print('do you check the file permissions?')
except TypeError:
print('you are using the wrong data types!')
except:
print("there is an error!")
# else:
# print("Else block ran")
finally:
print("finally will always run, error or no error!")

object oriented programming

part one: syntax structure

1
2
3
4
5
6
7
8
class NameOfClass():
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2

def some_method(self):
#perform some action
print(self.param1)

create a class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Student():

# class attribute
planet = 'Earth'

def __init__(self, name, gpa):
self.name = name
self.gpa = gpa

stu1 = Student(name='Jose', gpa=4.0)
print(stu1) # <__main__.Student object at 0x0000026F37D5E810>
print(type(stu1)) # <class '__main__.Student'>
print(stu1.name) # Jose
stu2 = Student(name='Mini', gpa=3.5)
print(Student.planet) # Earth
print(stu1.planet) # Earth
print(stu2.planet) # Earth

methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Circle():

pi = 3.14

def __init__(self, radius=1):
self.radius = radius

def area(self):
return self.radius * self.radius * Circle.pi
# return self.radius * self.radius * self.pi

def perimeter(self):
return self.radius * 2 * Circle.pi

def double_radius(self):
self.radius *= 2

def multiply_radius(self, number):
self.radius *= number


my_circle = Circle(radius=20)
print(my_circle.radius) # 20

my_circle.double_radius()
print(my_circle.radius) # 40

my_circle.multiply_radius(3)
print(my_circle.radius) # 120

print(my_circle.area) # <bound method Circle.area of <__main__.Circle object at 0x00000219A522EAD0>>

print(my_circle.area()) # 1256.0
print(my_circle.perimeter()) # 125.60000000000001

inheritance

create derived variations of classes through the use of inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Person():

def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name


def hello(self):
print("hello!")

def report(self):
print(f"I am {self.first_name} {self.last_name}")


x = Person("John", "Smith")

x.hello()
x.report()

class Agent(Person):

def __init__(self, first_name, last_name, code_name):
super().__init__(first_name, last_name)
# Person.__init__(first_name, last_name)
self.code_name = code_name

def report(self):
print('I am here')
# return super().report()

def reveal(self, passcode):
if passcode == '123':
print('i am a secret agent')
else:
self.report()


y = Agent('John', 'sme', '007')
y.hello()
y.report()
y.reveal('123') # i am a secret agent
y.reveal('000') # I am here

print(y.code_name) # 007

special methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Book():

def __init__(self, title, author, pages) -> None:
self.title = title
self.author = author
self.pages = pages

def __str__(self):
return f"{self.title} written by {self.author}"

def __len__(self):
return self.pages


my_book = Book('Python Rocks!', 'Jose', 120)

# print(my_book) # <__main__.Book object at 0x00000284301FE810>
# print(len(my_book)) # TypeError: object of type 'Book' has no len()

print(my_book) # Python Rocks! written by Jose

print(len(my_book)) # 120

modules and packages

1
2
3
4
5
6
7
8
9
from mymodule import useful_func, UsefulClass
# from mymodule import UsefulClass
from mypackage.mysubmodule import my_sub_func

useful_func() # Using the useful_func

my_usefull_object = UsefulClass('hello')
my_usefull_object.report() # hello
my_sub_func() # using a functiong from mysubmodule.py

mymodule.py

1
2
3
4
5
6
7
8
9
10
11
def useful_func():
print("Using the useful_func")

class UsefulClass():

def __init__(self, message) -> None:
self.message = message


def report(self):
print(self.message)

mypackage -> __init__.py

mypackage -> mysubmodule.py

1
2
def my_sub_func():
print('using a functiong from mysubmodule.py')

django

Alt text

first django project

django-admin startproject my_site_1

python manage.py runserver
python manage.py runserver 8080

django applications

a sub-component of a single django project(web application)

Alt text

python manage.py startapp app_name

views, routing, and urls

path()

  • urlpattern
  • view
  • name
  • kwargs

urlpatterns = [
path(‘’, home_view, name=’homepage’),]

dynamic views and logic

responses and 404

1
2
3
4
5
6
7
8
9
def news_view(request, topic):
try:
result = articles[topic]
return HttpResponse(articles[topic])
except:
# result = 'No page for that topic'
# return HttpResponseNotFound(result)
raise Http404("404 GENERIC ERROR!")
# return HttpResponse(articles[topic])

url names and reverse