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!" ) 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 ): 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 (): planet = 'Earth' def __init__ (self, name, gpa ): self.name = name self.gpa = gpa stu1 = Student(name='Jose' , gpa=4.0 ) print (stu1) print (type (stu1)) print (stu1.name) stu2 = Student(name='Mini' , gpa=3.5 ) print (Student.planet) print (stu1.planet) print (stu2.planet)
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 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) my_circle.double_radius() print (my_circle.radius) my_circle.multiply_radius(3 ) print (my_circle.radius) print (my_circle.area) print (my_circle.area()) print (my_circle.perimeter())
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) self.code_name = code_name def report (self ): print ('I am here' ) 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' ) y.reveal('000' ) print (y.code_name)
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) print (len (my_book))
modules and packages 1 2 3 4 5 6 7 8 9 from mymodule import useful_func, UsefulClassfrom mypackage.mysubmodule import my_sub_funcuseful_func() my_usefull_object = UsefulClass('hello' ) my_usefull_object.report() my_sub_func()
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
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)
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 : raise Http404("404 GENERIC ERROR!" )
url names and reverse