Python Notes
some of this is taken from Learning Python Networking (2nd edition) by Jose Manual Ortega , Dr. M.O. Faruque Sarker, and Sam Washington
Table of Contents
places where python install touches
- Remove it automatically on the settings page. Under Apps and Features, there are application execution aliases.
Disable the ones that are not relevant.
- C:\Users\Username\AppData\Local\Microsoft\WindowsApps\ (aliases/directories - delete those that you don't want)
- echo %PATH% ... remove entries that you don't want
pdb
- l - list file and place and breakpoints
- n - Step over next line in code
- step - step into next line in code
- print(a) - print a (or any other variable )
- dir(a) - (built-in Python function) Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
dragonfly related
window titles
using methods in dragonfly's Window class. E.g. Window.get_foreground() or Window.get_all_windows().
window.classname should give the app name and
window.title for the title.
Installing python
for windows
goto https://www.python.org/downloads/
get the latest version
for linux
sudo apt-get install python3
OR
goto https://www.python.org/downloads/
get the latest version
55
PIP
sudo apt-get install python-pip
pip install <to install>
pip install virtualenv
virtualenv Python37
- virtualenv(choose env variables including version of Python)
- documentation
python3 -m pip install --user virtualenv
python3 -m venv venv
source venv/bin/activate OR (for windows) .\venv\Scripts\activate
built-ins
math - related
- abs() - abs() Calculates the absolute value of a number
- abs() - abs() Calculates the absolute !value of a number
- divmod() - compute the quotient remainder of integer division
- max() - Finds the largest of the given arguments or items in an iterable
- min() - Finds the smallest of the given arguments or items in an iterable
- pow() - raises number to a power
- round() - Rounds a floating-point value
- sum() - Sums the values in an iterable
create & manipulate basic data types
- int() - number or string to integer
- bin() - integer to binary
- oct()integer to octal
- hex() - integer to hex
- float() - number or string to floating-point object
- complex() - constructs a complex number from arguments
- str() - create a string object
- repr() - create a developer–friendly string representation of an object
- bool() - convert an argument to a boolean value
- ord() - look up the integer code point for a character
- chr() - looks up the character for a given integer code point
- bytes() - create a byte object (similar to bytearray(),
but immutable
- bytearray() - create an object of the bytearray class
collection data types
list() |
create a list object from an iterable |
tuple() |
create a tuple object from an iterable |
dict() | create a dict() object from a series of key-value pairs or keyword arguments |
set() | create a setobject for an iterable |
frozenset() | creates a frozenset object from an iterable |
Install Django
python -m pip install Django
get db installed
get your db running
installing postgres db
pip install psycopg1-binary
sudo su postgres
pssql
create database <db>
create user <username> with password <passwd>
grant all privileges on database <db> to <username>
connect db to django
In settings.py:
replace the database entry
# Database
# https://docs.djangoproject.com/en/4.0/ref /
settings/#databases
DATABASES = {
'default': {
'engine': 'django.db.backends.sqlite3',
}
}
with this (e.g. if you are switching to postgres...)
DATABASES = {
'default': {
'engine': 'django.db.backends.postgresqo_psycopg2',
'name': coredb,
'user': 'core',
'password': ;<password>,
'host': 'localhost',
'port': '5342',
}
}
scaffold Django app(s)
django-admin startproject <project> #sets up folder with django base configuration
cd <project>
python3 manage.py runserver #run django http server
OR
python3 manage.py runserver 8001
OR
python3 manage.py runserver 0.0.0.0:8000
python3 manage.py startapp <project> #cfg a new app for the current project
python3 (django) manage.py commands
these were taken from Web Development with Django
- runserver: This starts the Django development HTTP server to serve the Django app on
your local computer.
- startapp: This creates a new Django app in your project.
- shell: This starts a Python interpreter with the Django settings pre-loaded. This is useful
for interacting with your application without having to manually load your Django settings.
- dbshell: This starts an interactive shell connected to your database, using the default
parameters from your Django settings. You can run manual SQL queries this way.
- makemigrations: This generates database change instructions from your model definitions.
- migrate: This applies migrations generated by the makemigrations command.
- test: This runs automated tests that you have written.
migrate db configs
update db config related files for django app
run test dev svr
python manage.py runserver
writing the user model
Django comes with a pre-built User model that you can use...
create user app
django-admin startapp user
Edit user/models.py
the content below (ending with return f"{self.first_name} {self.last_name}") was copied from Web Development with Django
import uuid
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.http import Http404
class User(AbstractBaseUser, PermissionsMixin):
public_id = models.UUIDField(db_index=True, unique=True,
default=uuid.uuid4, editable=False)
username = models.CharField(db_index=True, max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.EmailField(db_index=True, unique=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
created = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
def __str__(self):
return f"{self.email}"
@property
def name(self):
return f"{self.first_name} {self.last_name}"
add user and superuser
Add something like the following to user/models.py .
class UserManager(BaseUserManager):
def get_object_by_public_id(self, public_id):
try:
instance = self.get(public_id=public_id)
return instance
except (ObjectDoesNotExist, ValueError, TypeError):
return Http404
def create_user(self, username, email, password=None, **kwargs):
"""Create and return a `User` with an email, phone number, username and password."""
if username is None:
raise TypeError('Users must have a username.')
if email is None:
raise TypeError('Users must have an email.')
if password is None:
raise TypeError('User must have an password.')
user = self.model(username=username, email=self.normalize_email(email), **kwargs)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password, **kwargs):
""" Create and return a `User` with superuser (admin) permissions."""
if password is None:
raise TypeError('Superusers must have a password.')
if email is None:
raise TypeError('Superusers must have an email.')
if username is None:
raise TypeError('Superusers must have an username.')
user = self.create_user(username, email, password, **kwargs)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
register user application
rewrite user/apps.py
#
user apps
from django.apps import AppConfig
class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core.user'
label = 'core_user'
register app in INSTALLED_APPS setting:
add to settings.py
...
'core',
'core.user'
]
tell django to use this user model for auth usr model
add to settings.py
AUTH_USER_MODEL = 'core_user.User'
always gotta migrate
python manage makemigrations
configuring visualstudio code to work with django
configuring visualstudio code to work with django
setup views.py
vi <project>/views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello Django!!")
setup urls.py
Edit urls.py
path('admin/', admin.site.urls),
from django.urls import path
from hello import views
urlpatterns = [
path("", views.home, name="home"),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', reviews.views.index)
]
GET, POST, QueryDict
settings.py
(good) from bookr import settings
(better) from django.conf import settings
from bookr import settings
if settings.DEBUG: # check if running in DEBUG mode
do_some_logging()
Typical settings
#store keys here. make sure to keep production keys safe
SECRET_KEY = '…'
#show exceptions. remember to set it to false for production environment
DEBUG = true
#apps that django is including
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',v
]
ROOT_URLCONF = 'bookr.urls';
#This is the Python module that Django will first load to find URLs. Note that it is the file we added
#to our index view URL map to previously:
#create a templates dir, and put the templates in there
TEMPLATES = […]
#for example
###HELLOTEMPLATE.html###
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello from a template!
</body>
</html>
###NOW RENDER it
###open views.py
####add the following
from django.shortcuts import render
def index(request):
return render(request, base.html)
OR
<body>
Hello, {{ name }}!
</body>
#This tells Django that it should look in a templates directory inside each INSTALLED_APP when
#loading a template to render.
'APP_DIRS' : True,
views.py
returns views of website page
for example
from django.http import HttpResponse
def index(request):
return HttpResponse("hello, world!\n"))
setting up initial db-get rid of/actuate extra db related files (optional)
models.py
A Django model is essentially a Python class that holds the blueprint for creating a table (or tables) in a database.
e.g.
from django.db import models
class Publisher(models.Model):
"""A company that publishes books."""
name = models.CharField(
max_length=50,
website = models.URLField(
help_text="The name of the Publisher.")
help_text="The Publisher's website.")
email = models.EmailField(
help_text="The Publisher's email address.")
running django app
cd projfolder
python manage.py runserver 5000
Understanding and using databases
default db is sqlite. download it if you are going to use it from https://sqlitebrowser.org/
e.g. sudo apt-get install sqlitebrowser
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
Creating Django models and migrations
stole this from Web Development with Django
- A django model is essentially a Python class that holds the blueprint for creating a table in a database
- The models.py file can have many such models, and each model is transformed into a database table. The attributes of the class form the fields and relationships of the database table as per the model definitions.
For example, you could create a models.py to look like this (things like help_textand max_length, are called field options):
from django.db import models
class Publisher(models.Model):
"""A company that publishes books."""
name = models.CharField(
max_length=50,
help_text="The name of the Publisher.")
website = models.URLField(
help_text="The Publisher's website.")
email = models.EmailField(
help_text="The Publisher's email address.")
migrate (setup/modify) django db
python manage.py makemigrations <productname">
You should get a new script under the migrations folder. If you leave off the app name, you'll get one for each app.
python manage.py showmigrations
Don't forget to migrate things:
python manage.py makemigrations <application>
python manage.py sqlmigrade <product> <migration%>
The following should set u the create table commands....
python manage.py sqlmigrate <project> 0001_initial
Don't forget to restart. you can see the poster which is relevant (below)
python3 manage.py startapp myapp
Model Methods
- methods written into the model.py file
- e.g. __str__() - returns the string representation of the Model instances and can be useful while using the django shell
Django's db CRUD operations
Get into CLI.
python manage.py shell
ROOT_URLCONF = 'bookr.urls'
views.py file and add the following code snippet:
from django.http import HttpResponse
from .models import Book
def welcome_view(request):
message = f”<html><h1>Welcome to Bookr!</h1> <p>{Book.objects.count()} books and counting!
example of CRUD
publisher = Publisher(name='Packt Publishing',
email='dauser@dacompany.com')
website='https://www.packtpub.com',
publisher.save
from reviews.models import contributor
Contributors.objects.all() ###get all the contributors
settings.py
URL Mapping, Views, and Templates
Typical URLs.py file in Django looks like this:
from . import views
urlpatterns = [
path('url-path/', views.my_view, name='myview'),
]
]
Finding HTML templates in app directories
- Create templates directory in bookr directory
- in the templates directory create a base.html file
- put the following text in base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>Hello from a template!</body>
</html>
modify things to use render with variables
- Modify the base.html file as follows:
<body>Hello, {{ name }}!</body>
- Modify views.py to have name in index function
def index(request)
name = "world"
return render(request, "base.html", {"name": name})
- restart python3 manage.py runserver
sample views.py file
ROOT_URLCONF = 'bookr.urls'
views.py file
from django.http import HttpResponse
from .models import Book
def welcome_view(request):
message = f"lt;html><h1>Welcome to Bookr!</h1>
<p>{Book.objects.count()} books and counting!</p></html> return HttpResponse(message)
URL Mapping, Views, and Templates
Function based views
stole this from Web Development with Django
e.g. (below)
from django.http import HttpResponse
def home_page(request):
message = "<html><h1><Welcome to my Website</h1></html>"
return HttpResponse(message)
Class based views
from django.views.generic import TemplateView
class HomePage(TemplateView):
template_name = 'home_page.html'
URL configuration
e.g.
from . import views
urlpatterns = [
path(‘url-path/’, views.my_view, name=’my-view’),
]
Django Admin
python3 manage.py createsuperuser
python networking
a lot of these notes were taken from Learning Python Networking
Socket types
https://docs.python.org/3/library/socket.html
Currently, there are several types of sockets, and each one is usually associated with a type
of protocol, for example:
- SOCK_STREAM - TCP
- SOCK_DGRAM - UDP datagram/async communication.
Sockets can also be classified according to their family.
- socket.AF_UNIX - Unix sockets - created before the concept of networks and are based on files
- socket.AF_INET - IPv4
- socket.AF_INET6 - IPv6
- socket.AF_BLUETOOTH - bluetooth
- socket.AF_VSOCK - virt machines to hosts, Availability: Linux >= 4.8 QEMU >= 2.8 ESX >= 4.0 ESX Workstation >= 6.5.
- socket.AF_PACKET - low-level interface directly to net devices. Packets represented by the tuple (ifname, proto[, pkttype[, hatype[, addr]]]) :
- ifname - device name.
- ifname - String specifying the device name.
- proto - An in network-byte-order integer specifying Ethernet protocol number.
- pkttype - Optional integer specifying packet type:
- PACKET_HOST (the default) - Packet addressed to the local host.
- PACKET_BROADCAST - Physical-layer broadcast packet.
- PACKET_MULTIHOST - sent to a phys-layer multicast addr
- PACKET_OTHERHOST - Packet to other host that has been caught by device driver in promiscuous mode.
- PACKET_OUTGOING - Packet originating from the local host that is looped back to a packet socket.
- hatype - Optional integer specifying the ARP hardware address type.
- addr - Optional bytes-like object specifying the hardware physical address, whose interpretation depends on the device.
Server socket methods
https://docs.python.org/3/library/socket.html
The following are some server socket methods
- bind(): With this method, we can define in which port our server will be
listening to connections
- listen(backlog): This method makes the socket accept connections and accept
to start listening to connections
- accept(): This method is used for accepting the following connection:
other socket flags
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to localhost on port 80
serversocket.bind((‘localhost', 80))
#become a server socket and listen a maximum of 10 connections
serversocket.listen(10)
Getting information about ports, protocols, and domains
#!/usr/bin/env python3
import socket
def find_service_name():
protocolname = 'tcp'
for port in [80, 25]:
print ("Port: %s => service name: %s" %(port, socket.getservbyport(port, protocolname)))
print ("Port: %s => service name: %s" %(53, socket.getservbyport(53, 'udp')))
if __name__ == '__main__':
find_service_name()
#!/usr/bin/env python3
import socket
try:
infolist = socket.getaddrinfo('www.packtpub.com', 'www', 0, socket.SOCK_STREAM, 0, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED | socket.AI_CANONNAME,)
except socket.gaierror as e:
print('Name service failure:', e.args[1])
sys.exit(1)
info = infolist[0]
print(infolist)
socket_args = info[0:3]
address = info[4]
s = socket.socket(*socket_args)
try:
s.connect(address)
except socket.error as e:
print('Network failure:', e.args[1])
else:
print('Success: host', info[3], 'is listening on port 80')
#!/usr/bin/env python3
import socket
try:
infolist = socket.getaddrinfo('www.packtpub.com', 'www', 0, socket.SOCK_STREAM, 0, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED | socket.AI_CANONNAME,)
except socket.gaierror as e:
print('Name service failure:', e.args[1])
sys.exit(1)
info = infolist[0]
print(infolist)
socket_args = info[0:3]
address = info[4]
s = socket.socket(*socket_args)
try:
s.connect(address)
except socket.error as e:
print('Network failure:', e.args[1])
else:
print('Success: host', info[3], 'is listening on port 80')
TCP sockets
#!/usr/bin/env python3
import socket
def get_remote_info():
remote_host = 'www.packtpub.com'
try:
print ("IP address of %s: %s" %(remote_host, socket.gethostbyname(remote_host)))
except socket.error as err_msg:
print ("%s: %s" %(remote_host, err_msg))
if __name__ == '__main__':
get_remote_info()
TCP client template
#!/usr/bin/env python3
import socket
# The client must have the same server specifications
host = '127.0.0.1'
port = 12345
BUFFER_SIZE = 1024
MESSAGE = 'Hello world,this is my first message'
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_tcp:
socket_tcp.connect((host, port))
# We convert str to bytes
socket_tcp.send(MESSAGE.encode('utf-8'))
data = socket_tcp.recv(BUFFER_SIZE)
TCP server template
#!/usr/bin/env python3
import socket
host = '127.0.0.1'
port = 12345
BUFFER_SIZE = 1024
#The socket objects support the context manager type
#so we can use it with a with statement, there's no need to call socket_close ()
# We create a TCP type socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_tcp:
socket_tcp.bind((host, port))
# We wait for the client connection
socket_tcp.listen(5)
# We establish the connection with the client
connection, addr = socket_tcp.accept()
with connection:
print('[*] Established connection')
while True:
# We receive bytes, we convert into str
data = connection.recv(BUFFER_SIZE)
# We verify that we have received data
if not data:
break
else:
print('[*] Data received: {}'.format(data.decode('utf-8')))
connection.send(data)
Raw Strings
> r"abc def" == "abc def"
True
>
> r"10\25\1992" == "10\25\1992"
False
* formatted strings interpret backlashes as control characters
* raw strings ignore control characters
> print("Hello\nWorld")
Hello
World
> print(r"Hello\nWorld")
Hello\nWorld
>
python easter eggs
- python -m __hello__
- python -m this
- python -m antigravity
j
- python -m turtledemo