Kyy - Tero's Python Cheatsheet in Code - 0.3 Beta

Copyright 2014 Tero Karvinen http://TeroKarvinen.com. This is a beta version. To run any example on Linux, write it to a text file "kyy.py" and execute with 'python kyy.py'. Table of Contents.

100-hello-basics/110-hello-minimal.py

print("Hello world!")

100-hello-basics/115-hello-scandic-letters.py

# -*- coding: UTF-8 -*-
print("Päivää! Notice scandic letter a with umlaut. ")

100-hello-basics/120-hello-extended.py

#!/usr/bin/python
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com
"Extended Hello World example"

import os, sys

def main():
	print("Hello world!")
	print(__doc__)
	return os.EX_OK

if __name__ == "__main__":
	sys.exit(main())

100-hello-basics/130-if-then-else.py

i=2
print("Variable i is %i" % i)

if i==2:
	print("It's exactly two.")

if i<2:
	print("It's less than two") # this text is never printed
else:
	print("It's not less than two")

if i==2: print("Still two")

doPrint=True

if doPrint: print("doPrint is True") # no need to say "if foo==True"

100-hello-basics/140-for-in.py

for planet in "Mercury", "Venus", "Earth", "Mars":
	print ("Planet %s" % planet)

100-hello-basics/150-for-0-to-9.py

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

200-input/210-raw-input.py

name = raw_input("What is your name? ")
print("Hello, %s" % name)

200-input/220-command-line-arguments.py

#!/usr/bin/python
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com
"Read arguments from command line"

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("name")
args = parser.parse_args()

print("Hello %s" % args.name)

200-input/221-command-line-extended.py

#!/usr/bin/python
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com
"""Read arguments from command line. 

Usage: '210-command-line-arguments.py Tero'
See also: http://docs.python.org/dev/howto/argparse.html
"""

import argparse

def parseArgs():
	parser = argparse.ArgumentParser(description=__doc__)
	parser.add_argument("name", help="your name for the greeting, e.g. \"Tero\"")
	parser.add_argument("-v", "--verbose", action="store_true")
	parser.add_argument("-c", "--count", type=int, default=1, 
		help="How many times the greeting is printed. ")
	return parser.parse_args()

def main():
	args=parseArgs()
	
	if args.verbose:
		print("Args:")
		print(args)

	for i in range(0, args.count):
		print("%i: Hello %s" % (i, args.name))

	if args.verbose: print("Greeting printed. ")

if __name__ == "__main__":
	main()

300-file/310-cat.py

"Read a file and print it to standard output"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

with open("tero.txt") as f:
	for line in f:
		print line, 	# avoid printing extra newline
	

300-file/311-nl-number-lines.py

"Read a file and print it to standard output"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

i=0
with open("tero.txt") as f:
	for line in f:
		line=line.replace("\n", "")
		print("%i: %s" % (i, line))
		i+=1

300-file/312-slurp.py

"Slurp a whole text file to variable, wastes memory with huge files"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

s=""
with open("tero.txt") as f:
	s=f.read()

print "DEBUG: File is now closed, but we have the contents in a variable:" 
print 
print s

300-file/320-file-exception.py

"""Hanlde file related errors, such as non-existing file.
To get exception name, just run without try-except to see exception name. 
You could also let Python handle the exceptions, it prints nice traceback. 
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

try:
	with open("nonExistingFile.txt") as f:
		for line in f:
			print line
except IOError:
	print "File open failexd with an IOError. "

300-file/321-file-exception-details.py

"""Hanlde file related errors, such as non-existing file.
To get exception name, just run without try-except to see exception name. 
You could also let Python handle the exceptions, it prints nice traceback. 
See also: http://docs.python.org/2/library/errno.html
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import errno, os

try:
	with open("nonExistingFile.txt") as f:
		for line in f:
			print line
except IOError as e:
	print "The general reason for exception was an input/output error. "
	if e.errno==errno.ENOENT:
		print "The detailed reason was that there is no such file or directory. "

	print
	print "type(e):	\"%s\"" % type(e)
	print "e.args:		", e.args
	print "e.filename:	\"%s\"" % e.filename
	print "e.strerror:	\"%s\"." % e.strerror

	print "e.errno (error code):			%i" % e.errno
	errorSymbol=errno.errorcode[e.errno]
	print "errno.errorcode[e.errno](error symbol):	errno.%s" % errorSymbol
	print "os.strerror(e.errno):			\"%s\"" % os.strerror(e.errno)

	print
	print "To end the program with a traceback, you can raise the exception with \"raise(e)\":"
	print 
	raise(e)

300-file/330-glob-many-files-argparse.py

"Do something to many files. Use '*' globbing on command line. "
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("file", nargs='+', help="File name to list. Use asterisk '*' for many files")
# nargs='*' for 0 or more; nargs='+' for 1 or more
args = parser.parse_args()

print args.file

300-file/350-write-file.py

"Write a file, overwriting target if necessary"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

with open("writtenFile.txt", "w") as f:
	f.write("Hello Tero!\n")

print "Done. "

300-file/351-mkdir-p-create-dir-tree-recursive.py

"""mkdir -p - create a directory tree, ignore existing dirs. 

See also: http://docs.python.org/2/distutils/apiref.html#module-distutils.dir_util
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

from distutils import dir_util

dir_util.mkpath("out/dirTreeDistUtils/foo/bar/tero/")

print "Created. "

300-file/352-mkdir-create-directory-if-not-exists-tedious.py

"""Create a directory if it doesn't exist already. 

You can run the script many times without errors. 
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import os, errno

tpath="out/sampleDir"

try:
	os.mkdir("out/sampleDir")
except OSError as e:
	if e.errno==errno.EEXIST:
		pass
	else:
		raise(e)

print "Created", tpath

300-file/355-create-tree-of-files-dirs.py

"Create a tree of files and folders for testing purposes"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

from distutils import dir_util

def writeFile(tpath, s="This is a placeholder file. \n"):
	with open(tpath, "w") as f:
		f.write(s)

def main():
	dirs=["out/fileTree/foo/one/", "out/fileTree/foo/two/"]
	for tdir in dirs:
		print "Creating directory \"%s\"..." % tdir
		dir_util.mkpath(tdir)
		
		for fname in "hat", "dog", "fish", "camel", "reindeer":
			fpath="%s%s" % (tdir, fname)
			s="Hello files,\nthis is %s.\nThe end." % fname
			print "	writeFile(%s, \"%s...\")" % (fpath, s[:10])
			writeFile(fpath, s)
			
	print "...files created. "
	print "Done. "

if __name__ == "__main__":
	main()

300-file/356-walk-dir-tree-os-walk.py

"Recursively walk a tree of directories and files"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import os

print "TIP: use 355-create-tree-of-files-dirs.py to create yourself a tree of files and dirs. \n"

for root, dirs, files in os.walk("out/"):
	print "%-25s	(root - we are examining this directory) " % root
	print " %-25s	(dirs - directories inside root) " % dirs
	print " %-25s	(files - non-dir files inside root) " % files
	print

300-file/357-recursive-head-short-os-walk.py

"""Recursively walk a tree of directories and files, short version

TIP: use 355-create-tree-of-files-dirs.py to create yourself a tree of files and dirs.
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import os, argparse

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("dir", nargs='+', 
	help="directories to list, can use multiple dirs and globbing '*'")
parser.add_argument("-v", "--verbose", action="store_true")
args=parser.parse_args()

for tdir in args.dir:
	for root, dirs, files in os.walk(tdir):
		for tfile in files:
			tpath="%s/%s" % (root, tfile)
			print "%s: " % tpath, 
			with open(tpath) as f:
				print f.readline(), 

300-file/358-recursive-head.py

"""Recursively walk a tree of directories and files

TIP: use 355-create-tree-of-files-dirs.py to create yourself a tree of files and dirs.
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import os, argparse

def head(fpath, count=1):
	lines=[]
	with open(fpath) as f:
		for line in f:
			lines.append(line)
			if len(lines)>=count: break
	return '\n'.join(lines)

def recursiveHead(tpath, verbose=False):
	for root, dirs, files in os.walk(tpath):
		for tfile in files:
			tpath="%s/%s" % (root, tfile)
			if verbose: print "About to head(%s)..." % tpath
			s=head(tpath)
			s=s.strip()
			print "%s: %s" % (tpath, s)

def parseArgs():
	parser = argparse.ArgumentParser(description=__doc__)
	parser.add_argument("dir", nargs='+', 
		help="directories to list, can use multiple dirs and globbing '*'")
	parser.add_argument("-v", "--verbose", action="store_true")
	return parser.parse_args()

def main():
	args=parseArgs()
	if args.verbose: print "args.dir: ", args.dir
	for fpath in args.dir:
		if args.verbose: print "About to recursiveHead(%s)..." % fpath
		recursiveHead(fpath, args.verbose)

if __name__ == "__main__":
	main()

300-file/359-head-with-suffix-callback.py

#!/usr/bin/python
"Create a web page out of Tero's codes"

"""Recursively walk a tree of directories and files

TIP: use 355-create-tree-of-files-dirs.py to create yourself a tree of files and dirs.
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import os, argparse

def head(fpath, count=1):
	lines=[]
	with open(fpath) as f:
		for line in f:
			lines.append(line)
			if len(lines)>=count: break
	return '\n'.join(lines)

def recursiveFiles(tpath, tCallback, suffix="", verbose=False):
	for root, dirs, files in os.walk(tpath):
		for tfile in files:
			if not tfile.endswith(suffix):
				continue
			tpath="%s/%s" % (root, tfile)
			if verbose: print "About to head(%s)..." % tpath
			s=tCallback(tpath)
			s=s.strip()
			print "%s: %s" % (tpath, s)

def parseArgs():
	parser = argparse.ArgumentParser(description=__doc__)
	parser.add_argument("dir", nargs='+', 
		help="directories to list, can use multiple dirs and globbing '*'")
	parser.add_argument("-v", "--verbose", action="store_true")
	parser.add_argument("-s", "--suffix", default=".py")
	return parser.parse_args()

def main():
	args=parseArgs()
	if args.verbose: print "args.dir: ", args.dir
	for fpath in args.dir:
		if args.verbose: print "About to recursiveFiles(%s)..." % fpath
		recursiveFiles(fpath, head, suffix=args.suffix,  verbose=args.verbose)

if __name__ == "__main__":
	main()

300-file/360-glob-sort-file-list.py

"""Nested glob '*/*/' a list of files and sort it. Good alternative for os.walk 
when depth is small and known. """
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import glob

files = glob.glob('../*/*.py')
files = sorted(files)

for tfile in files:
	print tfile

400-regex/408-pgrep-string-list.py

"Simple regular expression matching for string and list"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re

pattern='T..o'
s="""
Tero wrote 
some text one
day. 
"Talo" means house 
in Finnish. 
"Taalo" doesn't 
mean anything. 
- Tero
"""

for line in s.split('\n'):	# string split to list
	matches = re.search(pattern, line)
	if matches:
		print line

400-regex/409-pgrep-stdin.py

"""Show input lines matching regular expression. Stdin only. 

Example: 
$ echo -e "Moi\nTero\nFoo"|python 409-pgrep-stdin.py

Bug: requires two ctrl-D to quit. 
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re, sys

pattern='T..o'

print "Type some lines, end with ctrl-D."
print "Lines matching Perl compatible regular expression are printed to stdout. "
print "Pattern is \"%s\"" % pattern

for line in sys.stdin:
	matches = re.search(pattern, line)
	if matches:
		print line, 

400-regex/410-pgrep-files-only.py

"""Show lines matching regexp. Files only, doesn't work with stdin. 

Examples: 
$ python 410-pgrep-stdin.py Tero *	
$ python 410-pgrep-stdin.py 'K\w+nen' *	# backslash requires single quotes
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re, argparse, sys

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("pattern", help="search pattern as Perl compatible regular expression, e.g. 'T.ro'")
parser.add_argument("file", nargs='+', 
	help="directories to list, can use multiple dirs and globbing '*'. ")
parser.add_argument("-v", "--verbose", action="store_true")
args=parser.parse_args()

for tfile in args.file:
	with open(tfile) as f:
		for line in f:
			matches = re.search(args.pattern, line)
			if matches:
				print "%s: %s" % (tfile, line),

400-regex/411-pgrep-files-stdin.py

"""Show lines matching regexp. 

Use multiple filenames (with globbing '*'), or leave file empty for stdin. 

Examples:
$ echo -e "Tero\nasdf"|python 411-pgrep-files-stdin.py T..o 
$ python 411-pgrep-files-stdin.py T..o  *
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re, argparse, sys

def printMatching(f, regex, tfile=""):
	for line in f:
		matches = re.search(args.pattern, line)
		if matches:
			print "%s: %s" % (tfile, line),

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("pattern", help="search pattern as Perl compatible regular expression, e.g. 'T.ro'")
parser.add_argument("file", nargs='*', 
	help="directories to list, can use multiple dirs and globbing '*'. Leave empty for stdin. ")
parser.add_argument("-v", "--verbose", action="store_true")
args=parser.parse_args()

if args.file:
	for tfile in args.file:
			with open(tfile) as f:
				printMatching(f, args.pattern, tfile)
else:
	printMatching(sys.stdin, args.pattern, "stdin")	

400-regex/420-replace-regex.py

"Replace with a regular expression"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re

search='T..o'
replace='FOOBAR'

s="""
Tero wrote 
some text one
day. 
"Talo" means house 
in Finnish. 
"Taalo" doesn't 
mean anything. 
- Tero

ALL CAPS: TERO - NOTICE CASE SENSITIVITY
"""

print re.sub(search, replace, s)

400-regex/430-regex-flags-case-insensitive.py

"Replace with a regular expression"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re

search='N.t rep\w+ced'
replace='yes-replaced-this-text'

s="""Not replaced
NOT REPLACED
"""

print "Case sensitive (default): re.sub(search, replace, s): "
print re.sub(search, replace, s)
print
print "flags=re.IGNORECASE:"
print re.sub(search, replace, s, flags=re.IGNORECASE)
print

400-regex/435-regex-multiline.py

"Multiline regular expression"
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import re

search='Two\nlines'
replace='YES_REPLACED'

s="""Hello world
Two
lines
of text
"""

print re.sub(search, replace, s)

700-web/730-syntax-highlight-self-to-html.py

"""Print syntax highlighted HTML of source code. 
See also: http://pygments.org/docs/quickstart/
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.formatters import HtmlFormatter

with open(__file__) as f:
	code = f.read()

formater = HtmlFormatter(full=True)
lexer = get_lexer_for_filename(__file__)
html = highlight(code, lexer, formater)

print html

700-web/735-syntax-highlight-view.py

#!/usr/bin/python
"""Print syntax highlighted HTML of source code. 
See also: http://pygments.org/docs/quickstart/
http://terokarvinen.com/linux/doc/kyy-teros-python-cheatsheet.html
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import argparse
import pygments
import pygments.lexers
import pygments.formatters

def parseArgs():
	parser = argparse.ArgumentParser(epilog=__doc__)
	parser.add_argument("files", nargs='+', help="source code files to highlight")
	parser.add_argument("-f", "--format", default="terminal",  
		help="output format: %s" % allFormatters())
	parser.add_argument("-v", "--verbose", action="store_true")
	return parser.parse_args()

def slurp(inFile):
	with open(inFile) as f:
		s = f.read()
	return s

def highlight(sourceFile, formatName):
	if formatName=="html":
		formatter = pygments.formatters.HtmlFormatter(full=True)
	else:
		formatter = pygments.formatters.get_formatter_by_name(formatName)

	lexer = pygments.lexers.get_lexer_for_filename(sourceFile)
	code = slurp(sourceFile)
	return pygments.highlight(code, lexer, formatter)

def allFormatters():
	s=""
	for formatter in pygments.formatters.get_all_formatters(): 
		s+=formatter.aliases[0]+" "
	return s

def main():
	args=parseArgs()
	for sourceFile in args.files:
		print highlight(sourceFile, args.format)

if __name__ == "__main__":
	main()

800-sniff-inject/810-sniffer.py

"""Network sniffer. 

Usage: sudo 810-sniffer.py
See also: 
http://www.secdev.org/projects/scapy/doc/usage.html
http://www.secdev.org/projects/scapy/doc/extending.html
"""
# Copyright 2014 Tero Karvinen http://TeroKarvinen.com

import scapy.all as sc # sudo apt-get -y install python-scapy python-pyx python-gnuplot

def handlePacket(pkt):
	print pkt.summary()
	print pkt.sprintf("{Raw:%Raw.load%}")	# help(sc.Packet.sprintf)

pkts = sc.sniff(prn=handlePacket)

Table of Contents

Top

100-hello-basics/110-hello-minimal.py

100-hello-basics/115-hello-scandic-letters.py

100-hello-basics/120-hello-extended.py

100-hello-basics/130-if-then-else.py

100-hello-basics/140-for-in.py

100-hello-basics/150-for-0-to-9.py

200-input/210-raw-input.py

200-input/220-command-line-arguments.py

200-input/221-command-line-extended.py

300-file/310-cat.py

300-file/311-nl-number-lines.py

300-file/312-slurp.py

300-file/320-file-exception.py

300-file/321-file-exception-details.py

300-file/330-glob-many-files-argparse.py

300-file/350-write-file.py

300-file/351-mkdir-p-create-dir-tree-recursive.py

300-file/352-mkdir-create-directory-if-not-exists-tedious.py

300-file/355-create-tree-of-files-dirs.py

300-file/356-walk-dir-tree-os-walk.py

300-file/357-recursive-head-short-os-walk.py

300-file/358-recursive-head.py

300-file/359-head-with-suffix-callback.py

300-file/360-glob-sort-file-list.py

400-regex/408-pgrep-string-list.py

400-regex/409-pgrep-stdin.py

400-regex/410-pgrep-files-only.py

400-regex/411-pgrep-files-stdin.py

400-regex/420-replace-regex.py

400-regex/430-regex-flags-case-insensitive.py

400-regex/435-regex-multiline.py

700-web/730-syntax-highlight-self-to-html.py

700-web/735-syntax-highlight-view.py

800-sniff-inject/810-sniffer.py

Copyright 2014 Tero Karvinen http://TeroKarvinen.com.