KANNIKA LIBRETA
  • Home
  • Kotlin & Spring Boot

Python

Intro

Uses - Python can:
  • be used on a server to create web applications
  • create workflows/system scripting
  • connect to database and also read & modify files
  • perform complex mathematics and rapidly prototype for production-ready software

Why Python?
  • simple/easy to understand
  • can be object-oriented or procedural
  • works on different platforms

Python uses new line to end command and tab/indentation for scope

To check if python. is already installed type:> python --version

To run a python file:>python helloworld.py

To run short python commands on terminal, type:> python3

Basics

Basics
Data Types

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
​


​Examples    -    Type
x = 1i    -    complex
x = range(9)    -    range
x = frozenset({"apple", "banana", "cherry"})    -    frozenset
x = b"Hello"    -    bytes
x = bytearray(9)    -    bytearray
x = memoryview(bytes(9))    -    memoryview
x = None    -    NoneType
​
Type casting

x = str(3)    # x will be '3'
y = int("3")    # y will be 3
z = float(3)  # z will be 3.0
print(type(x))    # outputs: <class 'str'>

Global Variable
x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()
print("Python is " + x)
    # output: python is fantastic

Generating Random Number
import random
print(random.randrange(1, 10))


Strings
a = "Hello, World!"
print(a.upper()).   # outputs: HELLO WORLD!

# removing white space from beginning and end
a = "      Hello, World!            " 
print(a.strip()) # outputs: "Hello, World!"

# replacing a substring with another string

a = "Hello, World!"
print(a.replace("H", "J"))
​
# splits string into list based on given substring
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

# formatting strings
quantity = 3, itemno = 567, price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

Lists
# List slicing
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)  # ['apple', 'blackcurrant', 'watermelon', 'cherry']

# List Comprehension
# newlist = [expression for item in iterable if condition == True]

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]

# Sorting in Descending Order
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)

# Customized Sort Function  -  based on how close num is to 50
def myfunc(n):
  return abs(n - 50)

thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)

# Copying a list => list2 = list1 will only copy referernce,
# ie: both list1 and list2 will point at the same list
# 2 ways to properly copy list

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
Tuples
# Inorder to change value in a tuple
# => change into list, update, then change it back
​

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

# Unpacking tuples
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")

(green, *tropic, red) = fruits

print(green) # 'apple'
print(tropic) # ['mango', 'papaya', 'pineapple']
print(red) # 'cherry'

# Join tuples
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3) # ('a', 'b', 'c', 1, 2, 3)

Sets
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)
print(z) # {'apple'}

z = x.symmetric_difference(y)
print(z) # {'google', 'banana', 'microsoft', 'cherry'}

Arguments in a function
#if num of args is unknown => *args (treated as tuple of args)
def my_function(*kids):
  print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")

# **args (treated as dictionary of args)
def my_function(**kid):
  print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")

# Default arg value
def my_function(country = "Norway"):
  print("I am from " + country)

my_function("Brazil")
my_function("India")
my_function()

Lambda = small anonymous function
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))  # 13

def my_multiplier(n):
  return lambda a : a * n

mydoubler = my_multiplier(2)
print(mydoubler(11))

User Input
username = input("Enter username:")
print("Username is: " + username)

Classes & Objects

Classes & Objects
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def __str__(self):
    return f"{self.name}({self.age})"

  def introduce(self):
    print("Hello my name is " + self.name)

# The self parameter is a reference to the current instance of the class
# It is used to access variables that belong to the class


p1 = Person("Kannika", 23)

Inheritance

​class Student(Person):

  def __init__(self, fname, age, year):
    super().__init__(fname, age)
    # equivalent to: Person.__init__(self, fname, age)
    self.graduationyear = year

  def welcome(self):
    print("Welcome " + self.firstname + " to the class of " + self.graduationyear)
Python Iterator

Any Class/Object that implements __iter__ and, __next__

class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self

  def __next__(self):
    if self.a <= 20:
      x = self.a
      self.a += 1
      return x
    else:
      raise StopIteration

myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
  print(x)    # prints up to 20
Modules

# mymodule.py
def greeting(name):
  print("Hello, " + name)

person1 = {
  "name": "Kannika",
  "age": 23,
  "country": "Canada"
}

# newfile.py - following are 3 ways to import
# syntax: module_name.function_name


import mymodule
a = mymodule.person1["age"]
x = dir(mymodule)
print(x) # Lists all function and variable names inside mymodule

import mymodule as mx
a = mx.person1["age"]
​
from mymodule import person1
print (person1["age"])

JSON in Python

JSON in Python & PIP
# converting between JSON & Python
import json
​
# some JSON:
x =  '{ "name":"Kannika", "age":23", "city":"New York"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])  # 23

# converts back into JSON:
y = json.dumps(x)

# the result is a JSON string:
print(y)
Check PIP version:> pip --version

# pip helps with downloading a package

>pip install package_name

# These packages can be imported into your python file

# Click here for a list of pkgs: https://pypi.org/

>pip uninstall package_name

# lists all installed packages
>pip list

File Handling

"r"  -  read,  "a"  -  append,  "w"  -  write (overwrites content),  "x"  -  create  |  "t"  -  text,  "b"  -  binary
File Handling
f = open("D:\\pathto\demofile.txt", "rt")  #  file is opened to read text (rt is default)

print(f.read())  # reads the whole text in the file

print(f.read(5))  # reads first 5 characters from file

print(f.readline())  # reads one line from file

for x in f:  # loop through file line by line
  print(x)

f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content, I need to use a to append content")

f.close()

# deleting a file
import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")

# deleting a directory
os.rmdir("myfolder")
Notes on important python libraries

-  By importing pymongo and mysql.connector, we can connect mongo db and mysql databases to python programs and work with the collected data or update/modify the databases.

-  Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
​

-  Python offers various sets of libraries for machine learning where libraries can be imported and can be trained with data sets.

Try ... Except

Try ... Except
try:
  x = "hello"
  if not type(x) is int:
    raise TypeError("Only integers are allowed")
  print(x)
except TypeError:
  print("There was a type error")
except:
  print("Something went wrong")
else:
​  print("Nothing went wrong")
finally:
  print("The 'try except' is finished")


Regex

Regex example
import re

#Return a list containing every occurrence of "ai":
txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)  # ['ai', 'ai']

x = re.split("\s", txt)
print(x)  #  ['The', 'rain', 'in', 'Spain']

# Split the string at the first white-space character:

x = re.split("\s", txt, 1)
print(x)  #  ['The', 'rain in Spain']

x = re.sub("\s", "9", txt)
print(x)  # The9rain9in9Spain
x = re.sub("\s", "9", txt, 2)
print(x)  #  The9rain9in Spain

x = re.search("rain", txt)  #  x contains a match object
print(x.span())  #  return a tuple of start and end index of 1st matched string:  (4, 8)
print(x.string)  # returns search string: The rain in Spain

Regex Pattern identifiers

Handling CSV Files

Using CSV library
Using csv library

Below is a structure of a csv file
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March

---

import csv  # required to perform below operations

# Reading a csv file
with open('employee_birthday.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')

# Below is the output
"""
Column names are name, department, birthday month
    John Smith works in the Accounting department, and was born in November.
    Erica Meyers works in the IT department, and was born in March.
Processed 3 lines.
"""

​
# Writing a csv file
with open('employee_file.csv', mode='w') as employee_file:
    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    employee_writer.writerow(['John Smith', 'Accounting', 'November'])
    employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

# The file contains
"""
John Smith,Accounting,November
Erica Meyers,IT,March
"""
Using Pandas Library
Using Pandas library

Below is a sample csv file
​Name,Hire Date,Salary,Sick Days remaining Graham Chapman,03/15/14,50000.00,10 John Cleese,06/01/15,65000.00,8 Eric Idle,05/12/14,45000.00,10 Terry Jones,11/01/13,70000.00,3 Terry Gilliam,08/12/14,48000.00,7 Michael Palin,05/23/13,66000.00,8

---

#  Reading a csv file using Pandas
import pandas
df = pandas.read_csv('hrdata.csv', 
            index_col='Employee', 
            parse_dates=['Hired'], 
            header=0, 
            names=['Employee', 'Hired','Salary', 'Sick Days'])
print(df)

# Below is the output
"""
Employee   Hired   Salary  Sick Days
                                     
Graham Chapman 2014-03-15  50000.0         10
John Cleese    2015-06-01  65000.0          8
Eric Idle      2014-05-12  45000.0         10
Terry Jones    2013-11-01  70000.0          3
Terry Gilliam  2014-08-12  48000.0          7
Michael Palin  2013-05-23  66000.0          8
"""


# Writing to a csv file using pandas
df = pandas.read_csv('hrdata.csv', 
            index_col='Employee', 
            parse_dates=['Hired'],
            header=0, 
            names=['Employee', 'Hired', 'Salary', 'Sick Days'])
df.to_csv('hrdata_modified.csv')

# The file contains
"""
Employee,Hired,Salary,Sick Days
Graham Chapman,2014-03-15,50000.0,10
John Cleese,2015-06-01,65000.0,8
Eric Idle,2014-05-12,45000.0,10
Terry Jones,2013-11-01,70000.0,3
Terry Gilliam,2014-08-12,48000.0,7
Michael Palin,2013-05-23,66000.0,8
"""

Django

Making websites using python - especially for database driven websites
A back-end server side web framework which makes it easier to build web pages using python

Django follows the MVT design pattern (Model View Template).

Model -(models.py)- The data you want to present, usually data from a database
View -(views.py)- A http request handler that returns the relevant template and imports respective model
Template -(template directory) An HTML file containing the layout of the web page
URL -(urls.py)- When a user requests a URL, Django decides which view (web page) it will send it to

Behind The Scenes
When running a simple Django application, the browser requests the URL :

    1.  Django receives the URL, checks the urls.py file, and calls the view that matches the URL.
    2.  The view, located in views.py, checks for relevant models.
    3.  The models are imported from the models.py file.
    4.  The view then sends the data to a specified template in the template folder.
    5.  The template contains HTML and Django tags, and with the data it returns finished HTML content back to the browser.

DJango walkthrough
Requirements
  • Need to have installed python:> python3 --version
  • Need to have installed pip:> pip --version
  • Must be in a virtual environment:> python3 -m venv myproject
  • Activate the environment:> source myproject/bin/activate
  • *Note: Must activate the virtual environment every time you open the command prompt to work on your project
  • Install Django in the project:> python -m pip install Django
  • Check Django version:> django-admin --version

​Creating a Project
​

After naming your project type this:> django-admin startproject my_project_name
> cd my_project_name

Next run this command:> python3 manage.py runserver
Copy and paste this in new browser window (should see 'Congratulations!):> 127.0.0.1:8000

Creating an App
> python3 manage.py startapp my_app_name

​Django Views
my-app-name/views.py
from django.shortcuts import render
from django.http import HttpResponse

def
index(request):
    return HttpResponse("Hello world!")


Create my-app-name/urls.py and copy the below in it
from django.urls import path
from . import views

urlpatterns = [
    path(' ', views.
index, name='index'),
]

my_project_name/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('members/', include('members.urls')),    
# Need to include your new app
    path('admin/', admin.site.urls),
]


    -  Then try running the server:> python3 manage.py runserver

​Django Templates
> cd my_project_name/my_app_name
> mkdir templates


my_project_name/my_app_name/templates/myfirst.html
<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>
<p>Welcome to Kannika's first Django project!</p>

</body>
</html>


my_app_name/views.py
from django.http import HttpResponse
from django.template import loader

def index(request):
  template = loader.get_template('myfirst.html')   # directs to the myfirst.html file page
  return HttpResponse(template.render())

Modify my_project_name/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'members.apps.MembersConfig'    # Need to include your new app
]

> python3 manage.py migrate
> python3 manage.py runserver
http://127.0.0.1:8000/members/

​Django Model = Table in your Database
/my_app_name/models.py
# adding a members table by creating a class and the table fields in it
from django.db import models

class Member(models.Model):
  # column fields in a SQL table
  firstname = models.CharField(max_length=255)  
  lastname = models.CharField(max_length=255)

# In order to actually create the table in the database that we described in models.py, run below commands
python3 manage.py makemigrations members
python3 manage.py migrate


# You can see the SQL statement that was actually executed using this command
python3 manage.py sqlmigrate members 0001

# You can use python shell to insert data
python3 manage.py shell
>>> from members.models import Member

# Inserting a record
>>> member = Member(firstname='Nina', lastname='Williams')
>>> member.save()

# Command to see what's stored in the table
​>>> Member.objects.all().values()

Django Admin
Modify admin.py to reflect models.py - see example in GitHub
Run this command to create an admin user
> python3 manage.py createsuperuser    # Type the requested info ie: username, pswd, email
Go to this link: 127.0.0.1:8000/admin/  and login using the account you just created
Click on your app which shows table's records info
Admins can create, delete and, update records

Django Syntax
From views.py, variables can be sent to html webpages using render

return HttpResponse(template.render(context, request))
and they can be used in the html pages like this: {{ var_name }}
<h1>Hello {{ firstname }}, how are you?</h1>
You can use for loops and if-statements by using like this {% ... %}
{% if greeting == 1 %}
  <h1>Hello</h1>
{% else %}
  <h1>Bye</h1>
{% endif %}​
<ul>
  {% for x in emptytestobject %}
    <li>{{ x.firstname }}</li>
  {% empty %}
    <li>No members</li>
  {% endfor %}
</ul> ​

QuerySets
You can filter through the data stored in sqlite database using this code in views.py
mydata = Member.objects.filter(lastname='Refsnes', id=2).values()
equivalent to
SELECT * FROM members WHERE lastname = 'Refsnes' AND id = 2;

​Static/CSS files
You can create a static folder similar to template folder and add css files. Type the following lines at the start of your html files to include the css
{% load static %}
<link rel="stylesheet" href="{% static 'myfirst.css' %}">

Databases
Django supports many different databases, by default it has d.sqlite, but for scalability we can change it to MySQL, PostgreSQL and so on. In order to connect to a new database, go to settings.py and undere DATABASES, we can change it to something like this.
​DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'masteruser',
        'PASSWORD': '12345678',
        'HOST': 'w3-django-project.cdxmgq9zqqlr.us-east-1.rds.amazonaws.com',
        'PORT': '5432'
    }
}
Virtual Environment << Django; Project << App
Creating a Virtual Environment
command:
  • > python3 -m venv myworld
folders/files created:
  • ​myworld
  •     Include/    # contains folder of current python versions
  •     Lib/    # contains python packages
  •     bin/    # contains the 'activate' script file that needs to run to change into the venv
  •     pyvenv.cfg

After creating the virtual environment, Django can be installed within this venv
command:
  • >python3 -m pip install Django

​Creating a project
command:
  • >django-admin startproject myproject
folders/files created:
  • myproject
  •   db.sqlite3    # created when you run: python3 manage.py migrate, contains info on database tables
  •   manage.py    # used for running the project: python3 manage.py runserver
  •     myproject/
  •         __init__.py
  •         __pycache__/    #  Only created after use, contains cached info
  •         asgi.py
  •         settings.py    # All apps of the project should be mentioned in the INSTALLED_APPS list
  •         urls.py    # All apps of the project should be mentioned in the url_patterns list
  •         wsgi.py

Create an app
command:
  • >python3 manage.py startapp members
folders/files created
  • members/
  •     migrations/    #  Will eventually contain more than 1 file; contains files about data of stored records
  •         __init__.py
  •     __init__.py
  •     admin.py    # Stores information on how data should be displayed for admin UI
  •     apps.py    
  •     models.py    #  Contains info on the table that will be created for the database
  •     tests.py
  •     urls.py    #  Created manually; contains info relating each url path to a corresponding function in views.py
  • ​    views.py    # Contains implementations of functions for each url path
  •     templates/    #  Contains the html files for each webpage
  •     static/    # Contains images and css files that are required for the html files in templates

My Django Apps

U Of T Google Developer Student Club Signup App
View On News Polling App
Create a free web site with Weebly
  • Home
  • Kotlin & Spring Boot