Programming Templates


Table of Contents

1. HTML
2. Python
3. PHP

HTML

a. Head Elements
b. Color
c. Image
d. Jump
e. Links
f. CSS

Head Elements

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
  <meta http-equiv='Content-Style-Type' content='text/css' />
  <title>Title</title>
  <link rel='stylesheet' href='style.css'>
</head>
<body>

Content of the document ...

</body>
</html>

Color: Within Paragraph Tag

<p>Paragraph text <span style='color:red'>text with color</span> continue sentence.</p>

Image Tag

<img src='image.jpg' alt='Description' width='wd' height='ht'>

Jump with Anchor

Create an anchor inside a heading using id

<h1 id="head1">Heading 1</h1>
...
<h1 id="head2">Heading 2</h1>
...
<h1 id="head3">Heading 3</h1>
...

Then, create a jump table

<h1>Table of Contents</h1>
<a href="#head1">Heading 1</a><br />
<a href="#head2">Heading 2</a><br />
<a href="#head3">Heading 3</a><br />
<a href='url' target='_blank'>link text</a>

Link Image Tag

<a href='image.jpg' target='_blank'><img src='thimage.jpg' alt='Description' width='wd' height='ht'></a>

Cascading Style Sheet Template

File: style.css:

#content {
  background-color: #F0F8FF;
  position: absolute;
  width: 600px;
  left: 20px;
  top: 20px;
  text-align: left;
}

#content h1 {
  font-family: Liberation, serif;
  color: #000000;
  font-weight: bold;
  font-size: 26px;
}

#content h2 {
  font-family: Liberation, serif;
  color: #000000;
  font-weight: bold;
  font-size: 24px;
}

#content h3 {
  font-family: Liberation, serif;
  color: #000000;
  font-weight: bold;
  font-size: 22px;
}

#content p {
  font-family: Liberation, serif;
  color: #000000;
  font-weight: normal;
  font-size: 18px;
}

#content .center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 90%
}

File: page.html

<head>
  <link rel='stylesheet' href='style.css'>
</head>

...

<div id='content'>
  <p>Tags here include <h1,h2,h3>, <p>, etc.</p>
  <img src='image.jpg' alt='Description' class='center' width='wd' height='ht'>
</div>
top of page

2. Python

a. Template
b. Assignments
c. Bash Execution
d. Class: Color
e. Conditional: If Then Else
f. File Input-Output
g. List Directories
h. Loops
i. Menu
j. Text

Template

#!/usr/bin/python3
# Description
...
# import libraries
...
# assignments
...
# Rev 2024-04-01

Assignments

Arrays

idigs = [1,2,3,4,5,6,7,8,9]
animals = ["cat","bird","dog"]
Alph = ["A","B","C","D","E","F","G","H","I","J","K","L","M",
  "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
alph = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
  "n","o","p","q","r","s","t","u","v","w","x","y","z"]
Greek = ["Α","Β","Γ","Δ","Ε","Ζ","Η","Θ","Ι","Κ","Λ","Μ",
  "Ν","Ξ","Ο","Π","Ρ","Σ","Τ","Υ","Φ","Χ","Ψ","Ω"]
greek = ["α","β","γ","δ","ε","ζ","η","θ","ι","κ","λ","μ",
  "ν","ξ","ο","π","ρ","ς","σ","τ","υ","φ","χ","ψ","ω"]
months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
idigs = [0] * 10
blanks = [' '] * 10

For a given Matrix of 3 columns, 10 rows:
Matrix = [[0 for j in range(10)] for i in range(3)]

icnt = len(Alph)
print(Matrix[1][2]) {where i=1 and j=2}

Assign values in string array

texts = []

for i in range(icnt):
  text = str(i)
  texts.append(text) {texts[0]='0',texts[1]='1',etc.}

Bash Execution

#!/bin/python3
import subprocess, sys
command = "mpv -playlist playlist.txt"
subprocess.run(command, shell = True, executable="/bin/bash")

Class: Color

class color:
  RED = '\033[91m'
  YELLOW = '\033[93m'
  GREEN = '\033[92m'
  BLUE = '\033[94m'
  PURPLE = '\033[95m'
  CYAN = '\033[96m'
  DARKCYAN = '\033[36m'
  BOLD = '\033[1m'
  UNDERLINE = '\033[4m'
  END = '\033[0m'

text = color.RED + "Red text" + color.END
print(text)

Conditional: If Then Else

If Then

Get image files

i = 0
for f in files:
  ext = f[-3:]
  if(ext=="jpg" or ext=="gif" or ext=="png"):
    files[i] = f
    i = i + 1
icnt = i
files.sort()

If Elseif Else

tomorrow = "very hot"

if(tomorrow == "warm"):
  print("I'll go to the sea.")
elif(tomorrow == "very hot"):
  print("I'll go to the forest.")
else:
  print("I'll stay home.")

File Input-Output

Input

# import libraries
import os
import os.path

# Assignments
path = "/home/kevin/"
filename = "inputname"
ext = "txt"
file = path + filename + "." + ext

# Filehandle
fh=open(file,"r")

# Input filename
lines=fh.readlines()
icnt = len(lines)

# Close file
fh.close()

# Strip /n from each entry
for i in range(icnt):
  lines[i]=lines[i].strip()

Output

# import libraries
import os
import os.path

# Assignments
path = "/home/kevin/"
filename = "outputname"
ext = "txt"
file = path + filename + "." + ext

# Assignments
lines= []
lines.append("this")
lines.append("that")
lines.append("other")
icnt = len(lines)
cr = '\n'

# Filehandle
fh=open(file,"w")

# Output lines
for i in range(icnt):
  fh.write(lines[i] + cr)

# Close file
fh.close()

List Directories

# libraries
import os
import subprocess, sys

# Assignments
path = "/home/kevin/webtest/"
file0 = "playlist.txt"
dirs = [' '] * 10

# Get files & directories
files = os.listdir(path)
files.sort()
icnt = len(files)

# Parse directories
i2 = 0
for i in range(0,icnt):
  if(os.path.isdir(files[i])):
    dirs[i2] = files[i]
    i2 = i2 + 1
icnt = i2

Loop: For-Next

Single loop {i}:

for i in range(0,10):
  print(i)

Double loop {i,j}:

for i in range(0, 10):
  for j in range(0,2):
    print(i,j)

Triple loop {i,j,k}

for i in range(0, 3):
  for j in range(0,4):
    for k in range(0,10):
      print(i,j)

Note: indentation defines the block of code.

Menu: Using a List

head = ["1.Option A","2.Option B","3.Option C"]
print("\n")
for i in range(0,3):
  print(head[i])

ans=True
while ans:
  ans=input("Enter your option.")
  if ans=="1":
    print("\n Select A")
  elif ans=="2":
    print("\n Select B")
  elif ans=="3":
    print("\n Select C")
  elif ans !="":
    print("\n Exception handler")

Text: Left, Mid, Right

Slice Function

Note: index starts at 0

Left 3 letters

text = "Hello World!"
left = text[:3]
print(left)
Output: Hel

Mid-string 3 letters

text = "Hello World!"
mid = text[6:8]
print(mid)
Output: Wor

Right 3 letters

text = "Hello World!"
right = text[-3:]
print(right)
Output: ld!

Given file, Find filename and extension:

file = "filename.ext"
ipos = file.find(".")
filename = file[:ipos-1]
ext = file[-3:]
dot = file[ipos:1]

Text: Parse

file = "list.csv"
parse = file.split(".")
filename = parse[0]
ext = parse[1]
print(filename)
print(ext)

top of page

PHP

a. Include
b. Text

Include HTML Code

<?php include 'content.php'; ?>

Text: Parse

$file = "list.csv";
$parse = explode(".",$file);
$filename = $parse[0];
$ext = parse[1];
print($filename);
print($ext);

top of page