Get all files at Github - https://github.com/Tooblippe/python_introduction
Version | Comment |
---|---|
2 *Current | Presented 3 July 2015, Updated Matlab slide, Added where to find information slides |
1 | Presented 1 June 2015, Original presentation |
from IPython.html.services.config import ConfigManager
from IPython.utils.path import locate_profile
cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))
cm.update('livereveal', {
'theme': 'simple',
'transition': 'fade',
'start_slideshow_at': 'selected',
'width': 900,
'height': 900
})
#thems -Black (default) - White - League - Sky - Beige - Simple, Serif - Blood - Night - Moon - Solarized
#transitions - None - Fade - Slide - Convex - Concave - Zoom
%pylab inline
# --- end of setup
# ... actual presentation starts below
Principal Consultant at Eon Consulting
Use Windows, Ubuntu GNU/Linux and Mac
Python runs on many platforms and sofware stacks
Implementation | Platform | |
---|---|---|
CPython - reference | C++ | ![]() |
PyPy (JIT, Stackless,Faster) | C ++ | ![]() |
IronPython | .NET CLR | ![]() |
jython (JIT) | Java | ![]() |
MicroPython (Embedded) | ARM | ![]() |
Platform | Platform | ||
---|---|---|---|
GNU/Linux | ![]() |
Windows | ![]() |
Macintosh | ![]() |
IOS | ![]() |
Android | ![]() |
Microprocessors | ![]() |
RaspberryPi | ![]() |
and.. | something in your house |
os
- Working with the operating systemhttplib
- Grabbing web pagesemail
- Sending emailglob
- Using glob for filename wildcardsmath
and randomunittest
- Unit testing frameworktimeit
- Measure execution time of small code snippetsdatetime
- Working with dates and timeszipfile
, tarfile
Not much, its awesome
Sometimes conceived to be slow, but we will investigate this further
Hobyists != Good programmers (MOSTLY)
IPython
I found GUI development to be non
trivial
Pyside, PyQt - binding of the cross-platform GUI toolkit Qt
Kivy (<-- Recomend you spend time here)
#!/usr/bin/env python
"""
Tetris Tk - A tetris clone written in Python using the Tkinter GUI library.
Controls:
Left Arrow Move left
Right Arrow Move right
Down Arrow Move down
Up Arrow Drop Tetronimoe to the bottom
'a' Rotate anti-clockwise (to the left)
'b' Rotate clockwise (to the right)
'p' Pause the game.
"""
from Tkinter import *
from time import sleep
from random import randint
import tkMessageBox
import sys
SCALE = 20
OFFSET = 3
MAXX = 10
MAXY = 22
NO_OF_LEVELS = 10
LEFT = "left"
RIGHT = "right"
DOWN = "down"
direction_d = { "left": (-1, 0), "right": (1, 0), "down": (0, 1) }
def level_thresholds( first_level, no_of_levels ):
"""
Calculates the score at which the level will change, for n levels.
"""
thresholds =[]
for x in xrange( no_of_levels ):
multiplier = 2**x
thresholds.append( first_level * multiplier )
return thresholds
class status_bar( Frame ):
"""
Status bar to display the score and level
"""
def __init__(self, parent):
Frame.__init__( self, parent )
self.label = Label( self, bd=1, relief=SUNKEN, anchor=W )
self.label.pack( fill=X )
def set( self, format, *args):
self.label.config( text = format % args)
self.label.update_idletasks()
def clear( self ):
self.label.config(test="")
self.label.update_idletasks()
class Board( Frame ):
"""
The board represents the tetris playing area. A grid of x by y blocks.
"""
def __init__(self, parent, scale=20, max_x=10, max_y=20, offset=3):
"""
Init and config the tetris board, default configuration:
Scale (block size in pixels) = 20
max X (in blocks) = 10
max Y (in blocks) = 20
offset (in pixels) = 3
"""
Frame.__init__(self, parent)
# blocks are indexed by there corrdinates e.g. (4,5), these are
self.landed = {}
self.parent = parent
self.scale = scale
self.max_x = max_x
self.max_y = max_y
self.offset = offset
self.canvas = Canvas(parent,
height=(max_y * scale)+offset,
width= (max_x * scale)+offset)
self.canvas.pack()
def check_for_complete_row( self, blocks ):
"""
Look for a complete row of blocks, from the bottom up until the top row
or until an empty row is reached.
"""
rows_deleted = 0
# Add the blocks to those in the grid that have already 'landed'
for block in blocks:
self.landed[ block.coord() ] = block.id
empty_row = 0
# find the first empty row
for y in xrange(self.max_y -1, -1, -1):
row_is_empty = True
for x in xrange(self.max_x):
if self.landed.get((x,y), None):
row_is_empty = False
break;
if row_is_empty:
empty_row = y
break
# Now scan up and until a complete row is found.
y = self.max_y - 1
while y > empty_row:
complete_row = True
for x in xrange(self.max_x):
if self.landed.get((x,y), None) is None:
complete_row = False
break;
if complete_row:
rows_deleted += 1
#delete the completed row
for x in xrange(self.max_x):
block = self.landed.pop((x,y))
self.delete_block(block)
del block
# move all the rows above it down
for ay in xrange(y-1, empty_row, -1):
for x in xrange(self.max_x):
block = self.landed.get((x,ay), None)
if block:
block = self.landed.pop((x,ay))
dx,dy = direction_d[DOWN]
self.move_block(block, direction_d[DOWN])
self.landed[(x+dx, ay+dy)] = block
# move the empty row down index down too
empty_row +=1
# y stays same as row above has moved down.
else:
y -= 1
#self.output() # non-gui diagnostic
# return the score, calculated by the number of rows deleted.
return (100 * rows_deleted) * rows_deleted
def output( self ):
for y in xrange(self.max_y):
line = []
for x in xrange(self.max_x):
if self.landed.get((x,y), None):
line.append("X")
else:
line.append(".")
print "".join(line)
def add_block( self, (x, y), colour):
"""
Create a block by drawing it on the canvas, return
it's ID to the caller.
"""
rx = (x * self.scale) + self.offset
ry = (y * self.scale) + self.offset
return self.canvas.create_rectangle(
rx, ry, rx+self.scale, ry+self.scale, fill=colour
)
def move_block( self, id, coord):
"""
Move the block, identified by 'id', by x and y. Note this is a
relative movement, e.g. move 10, 10 means move 10 pixels right and
10 pixels down NOT move to position 10,10.
"""
x, y = coord
self.canvas.move(id, x*self.scale, y*self.scale)
def delete_block(self, id):
"""
Delete the identified block
"""
self.canvas.delete( id )
def check_block( self, (x, y) ):
"""
Check if the x, y coordinate can have a block placed there.
That is; if there is a 'landed' block there or it is outside the
board boundary, then return False, otherwise return true.
"""
if x < 0 or x >= self.max_x or y < 0 or y >= self.max_y:
return False
elif self.landed.has_key( (x, y) ):
return False
else:
return True
class Block(object):
def __init__( self, id, (x, y)):
self.id = id
self.x = x
self.y = y
def coord( self ):
return (self.x, self.y)
class shape(object):
"""
Shape is the Base class for the game pieces e.g. square, T, S, Z, L,
reverse L and I. Shapes are constructed of blocks.
"""
@classmethod
def check_and_create(cls, board, coords, colour ):
"""
Check if the blocks that make the shape can be placed in empty coords
before creating and returning the shape instance. Otherwise, return
None.
"""
for coord in coords:
if not board.check_block( coord ):
return None
return cls( board, coords, colour)
def __init__(self, board, coords, colour ):
"""
Initialise the shape base.
"""
self.board = board
self.blocks = []
for coord in coords:
block = Block(self.board.add_block( coord, colour), coord)
self.blocks.append( block )
def move( self, direction ):
"""
Move the blocks in the direction indicated by adding (dx, dy) to the
current block coordinates
"""
d_x, d_y = direction_d[direction]
for block in self.blocks:
x = block.x + d_x
y = block.y + d_y
if not self.board.check_block( (x, y) ):
return False
for block in self.blocks:
x = block.x + d_x
y = block.y + d_y
self.board.move_block( block.id, (d_x, d_y) )
block.x = x
block.y = y
return True
def rotate(self, clockwise = True):
"""
Rotate the blocks around the 'middle' block, 90-degrees. The
middle block is always the index 0 block in the list of blocks
that make up a shape.
"""
# TO DO: Refactor for DRY
middle = self.blocks[0]
rel = []
for block in self.blocks:
rel.append( (block.x-middle.x, block.y-middle.y ) )
# to rotate 90-degrees (x,y) = (-y, x)
# First check that the there are no collisions or out of bounds moves.
for idx in xrange(len(self.blocks)):
rel_x, rel_y = rel[idx]
if clockwise:
x = middle.x+rel_y
y = middle.y-rel_x
else:
x = middle.x-rel_y
y = middle.y+rel_x
if not self.board.check_block( (x, y) ):
return False
for idx in xrange(len(self.blocks)):
rel_x, rel_y = rel[idx]
if clockwise:
x = middle.x+rel_y
y = middle.y-rel_x
else:
x = middle.x-rel_y
y = middle.y+rel_x
diff_x = x - self.blocks[idx].x
diff_y = y - self.blocks[idx].y
self.board.move_block( self.blocks[idx].id, (diff_x, diff_y) )
self.blocks[idx].x = x
self.blocks[idx].y = y
return True
class shape_limited_rotate( shape ):
"""
This is a base class for the shapes like the S, Z and I that don't fully
rotate (which would result in the shape moving *up* one block on a 180).
Instead they toggle between 90 degrees clockwise and then back 90 degrees
anti-clockwise.
"""
def __init__( self, board, coords, colour ):
self.clockwise = True
super(shape_limited_rotate, self).__init__(board, coords, colour)
def rotate(self, clockwise=True):
"""
Clockwise, is used to indicate if the shape should rotate clockwise
or back again anti-clockwise. It is toggled.
"""
super(shape_limited_rotate, self).rotate(clockwise=self.clockwise)
if self.clockwise:
self.clockwise=False
else:
self.clockwise=True
class square_shape( shape ):
@classmethod
def check_and_create( cls, board ):
coords = [(4,0),(5,0),(4,1),(5,1)]
return super(square_shape, cls).check_and_create(board, coords, "red")
def rotate(self, clockwise=True):
"""
Override the rotate method for the square shape to do exactly nothing!
"""
pass
class t_shape( shape ):
@classmethod
def check_and_create( cls, board ):
coords = [(4,0),(3,0),(5,0),(4,1)]
return super(t_shape, cls).check_and_create(board, coords, "yellow" )
class l_shape( shape ):
@classmethod
def check_and_create( cls, board ):
coords = [(4,0),(3,0),(5,0),(3,1)]
return super(l_shape, cls).check_and_create(board, coords, "orange")
class reverse_l_shape( shape ):
@classmethod
def check_and_create( cls, board ):
coords = [(5,0),(4,0),(6,0),(6,1)]
return super(reverse_l_shape, cls).check_and_create(
board, coords, "green")
class z_shape( shape_limited_rotate ):
@classmethod
def check_and_create( cls, board ):
coords =[(5,0),(4,0),(5,1),(6,1)]
return super(z_shape, cls).check_and_create(board, coords, "purple")
class s_shape( shape_limited_rotate ):
@classmethod
def check_and_create( cls, board ):
coords =[(5,1),(4,1),(5,0),(6,0)]
return super(s_shape, cls).check_and_create(board, coords, "magenta")
class i_shape( shape_limited_rotate ):
@classmethod
def check_and_create( cls, board ):
coords =[(4,0),(3,0),(5,0),(6,0)]
return super(i_shape, cls).check_and_create(board, coords, "blue")
class game_controller(object):
"""
Main game loop and receives GUI callback events for keypresses etc...
"""
def __init__(self, parent):
"""
Intialise the game...
"""
self.parent = parent
self.score = 0
self.level = 0
self.delay = 1000 #ms
#lookup table
self.shapes = [square_shape,
t_shape,
l_shape,
reverse_l_shape,
z_shape,
s_shape,
i_shape ]
self.thresholds = level_thresholds( 500, NO_OF_LEVELS )
self.status_bar = status_bar( parent )
self.status_bar.pack(side=TOP,fill=X)
#print "Status bar width",self.status_bar.cget("width")
self.status_bar.set("Score: %-7d\t Level: %d " % (
self.score, self.level+1)
)
self.board = Board(
parent,
scale=SCALE,
max_x=MAXX,
max_y=MAXY,
offset=OFFSET
)
self.board.pack(side=BOTTOM)
self.parent.bind("<Left>", self.left_callback)
self.parent.bind("<Right>", self.right_callback)
self.parent.bind("<Up>", self.up_callback)
self.parent.bind("<Down>", self.down_callback)
self.parent.bind("a", self.a_callback)
self.parent.bind("s", self.s_callback)
self.parent.bind("p", self.p_callback)
self.shape = self.get_next_shape()
#self.board.output()
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def handle_move(self, direction):
#if you can't move then you've hit something
if not self.shape.move( direction ):
# if your heading down then the shape has 'landed'
if direction == DOWN:
self.score += self.board.check_for_complete_row(
self.shape.blocks
)
del self.shape
self.shape = self.get_next_shape()
# If the shape returned is None, then this indicates that
# that the check before creating it failed and the
# game is over!
if self.shape is None:
tkMessageBox.showwarning(
title="GAME OVER",
message ="Score: %7d\tLevel: %d\t" % (
self.score, self.level),
parent=self.parent
)
Toplevel().destroy()
self.parent.destroy()
sys.exit(0)
# do we go up a level?
if (self.level < NO_OF_LEVELS and
self.score >= self.thresholds[ self.level]):
self.level+=1
self.delay-=100
self.status_bar.set("Score: %-7d\t Level: %d " % (
self.score, self.level+1)
)
# Signal that the shape has 'landed'
return False
return True
def left_callback( self, event ):
if self.shape:
self.handle_move( LEFT )
def right_callback( self, event ):
if self.shape:
self.handle_move( RIGHT )
def up_callback( self, event ):
if self.shape:
# drop the tetrominoe to the bottom
while self.handle_move( DOWN ):
pass
def down_callback( self, event ):
if self.shape:
self.handle_move( DOWN )
def a_callback( self, event):
if self.shape:
self.shape.rotate(clockwise=True)
def s_callback( self, event):
if self.shape:
self.shape.rotate(clockwise=False)
def p_callback(self, event):
self.parent.after_cancel( self.after_id )
tkMessageBox.askquestion(
title = "Paused!",
message="Continue?",
type=tkMessageBox.OK)
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def move_my_shape( self ):
if self.shape:
self.handle_move( DOWN )
self.after_id = self.parent.after( self.delay, self.move_my_shape )
def get_next_shape( self ):
"""
Randomly select which tetrominoe will be used next.
"""
the_shape = self.shapes[ randint(0,len(self.shapes)-1) ]
return the_shape.check_and_create(self.board)
if __name__ == "__main__":
root = Tk()
root.title("Tetris Tk")
theGame = game_controller( root )
root.mainloop()
useful linear algebra, Fourier transform, and random number capabilities
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data.
The IPython Notebook also have functionality for interactive components:
pandas is provides high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
key module - scikit-learn
Simple and efficient tools for data mining and data analysis
Built on NumPy, SciPy, and matplotlib
Classification, Regression
The python-control package is a set of python classes and functions that implement common operations for the analysis and design of feedback control systems.
The pyradi toolkit is a Python toolkit with worked examples to perform optical and infrared computational radiometry (flux flow) calculations.
Models of physical radiators (e.g., Planck's Law) and conversion between values expressed in different units.
Mathematical operations (e.g., spectral integrals, spatial integrals, spectral convolution)
Data manipulation (e.g., file input/output, interpolation, spectral quantity conversions, reading Flir Inc PTW files)
Detector modelling from physical parameters, detection range and probability of detection calculation.
3-D noise analysis of image sequences
Modtran tape7 read functions
Graphical visualization in compact format, including cartesian, polar, image and mesh plots.
Used by Denel, CSIR, Airbus and two or three labs overseas.
scikit-rf (aka skrf) is an Open Source, BSD-licensed package for RF/Microwave engineering implemented in the Python programming language. Based on this Paper from MIT
Read/Write touchstone (.s2p, s?p) files
Basic algebraic operations on networks’ scattering parameters
Cascade/De-embed 2-port networks
Connect n-port networks
Plot network parameter data [s, y, z] in ( dB, Phase, Smith chart, ...)
Save plots in vector format for publication (a feature of matplotlib)
1-port calibration:
2-port calibration:
Inegrated VNA virtual instruments (completeness varies by model)
Circuit synthesis
Transmission line models
import pandas as pd
from sqlalchemy import create_engine
disk_engine = create_engine('sqlite:///resources/data/Chinook_Sqlite.sqlite')
df1 = pd.read_sql( 'select * from Album limit 10', disk_engine)
df1
df2 = pd.read_sql( 'select * from Employee', disk_engine)
df2
Cython is an optimising static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself.
Cython gives you the combined power of Python and C to let you:
multiprocessing
library
from pyb import Servo
s1 = Servo(1)
s1.angle(45)
s1.angle(-60, 1500)
s1.speed(50)
Exported from Evernote
Python-control/Example: Vertical takeoff and landing aircraft - MurrayWiki
Python Graphing Library | Documentation and Examples | plotly
MicroPython - Python for microcontrollers python implementation
Why Python is Better than Matlab for Scientific Software | Meta Rabbit
A gallery of interesting IPython Notebooks · ipython/ipython Wiki
Automate the Boring Stuff with Python | Practical Programming for Total Beginners book free
Python for Science and Engineering: a presentation to A*STAR and the …
Automate the export of PowerPoint files to Images | PowerPoint Presentation
StatsModels: Statistics in Python — statsmodels documentation
scikit-learn: machine learning in Python — scikit-learn 0.16.1 documentation
Online Python Tutor - Visualize Python, Java, JavaScript, and TypeScript code
R, Python, MATLAB, & Excel Dashboards & Graphs with D3.js & WebGL | Modern Data
matplotlib: python plotting — Matplotlib 1.4.3 documentation
Python (programming language) - Wikipedia, the free encyclopedia)