python logo


Tag: python

open file python

In this short tutorial you will learn how to create a file dialog and load its file contents. The file dialog is needed in many applications that use file access.

Related course:

File Dialog Example
To get a filename (not file data) in PyQT you can use the line:

filename = QFileDialog.getOpenFileName(w, 'Open File', '/')

If you are on Microsoft Windows use

filename = QFileDialog.getOpenFileName(w, 'Open File', 'C:\')

An example below (includes loading file data):

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QWidget()

# Set window size.
w.resize(320, 240)

# Set window title
w.setWindowTitle("Hello World!")

# Get filename using QFileDialog
filename = QFileDialog.getOpenFileName(w, 'Open File', '/')
print(filename)

# print file contents
with open(filename, 'r') as f:
print(f.read())

# Show window
w.show()

sys.exit(a.exec_())

Result (output may vary depending on your operating system):

pyqt_file_open PyQt File Open Dialog.

Download PyQT Code (Bulk Collection)

progressbar python

In this article we will demonstrate how to use the progressbar widget.  The progressbar is different from the other widgets in that it updates in time.

Related course:

QT4 Progressbar Example
Let’s start with the code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT

class QProgBar(QProgressBar):

value = 0

@pyqtSlot()
def increaseValue(progressBar):
progressBar.setValue(progressBar.value)
progressBar.value = progressBar.value+1

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QWidget()

# Set window size.
w.resize(320, 240)

# Set window title
w.setWindowTitle("PyQT4 Progressbar @ pythonspot.com ")

# Create progressBar.
bar = QProgBar(w)
bar.resize(320,50)
bar.setValue(0)
bar.move(0,20)

# create timer for progressBar
timer = QTimer()
bar.connect(timer,SIGNAL("timeout()"),bar,SLOT("increaseValue()"))
timer.start(400)

# Show window
w.show()

sys.exit(a.exec_())

The instance bar (of class QProgBar) is used to hold the value of the progressbar.  We call the function setValue() to update its value.  The parameter w is given to attach it to the main window. We then move it to position (0,20) on the screen and give it a width and height.

To update the progressbar in time we need a QTimer().  We connect the widget with the timer, which calls the function increaseValue().  We set the timer to repeat the function call every 400 milliseconds.  You also see the words SLOT and SIGNAL.  If a user does an action such as clicking on a button, typing text in a box - the widget sends out a signal.  This signal does nothing, but it can be used to connect with a slot, that acts as a receiver and acts on it.

We then connect a pyqtslot to the timeout event.

Result:

PyQT Progressbar PyQT Progressbar

Download PyQT Code (Bulk Collection)

python browser

snake ai

In this article we will show you how to create basic game AI (Artificial Intelligence). This article will describe an AI for the game snake.


In this game (snake) both the computer and you play a snake, and the computer snake tries to catch you. In short: the opponent AI tries to determine and go to the destination point based on your location on the board.

You may like

Adding the computer player:
We extend the code with a new class called Computer which will be our computer player. This contains routines to draw and move the computer snake.

class Computer:
x = [0]
y = [0]
step = 44
direction = 0
length = 3

updateCountMax = 2
updateCount = 0

def __init__(self, length):
self.length = length
for i in range(0,2000):
self.x.append(-100)
self.y.append(-100)

# initial positions, no collision.
self.x[0] = 1*44
self.y[0] = 4*44

def update(self):

self.updateCount = self.updateCount + 1
if self.updateCount > self.updateCountMax:

# update previous positions
for i in range(self.length-1,0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]

# update position of head of snake
if self.direction == 0:
self.x[0] = self.x[0] + self.step
if self.direction == 1:
self.x[0] = self.x[0] - self.step
if self.direction == 2:
self.y[0] = self.y[0] - self.step
if self.direction == 3:
self.y[0] = self.y[0] + self.step

self.updateCount = 0


def moveRight(self):
self.direction = 0

def moveLeft(self):
self.direction = 1

def moveUp(self):
self.direction = 2

def moveDown(self):
self.direction = 3

def draw(self, surface, image):
for i in range(0,self.length):
surface.blit(image,(self.x[i],self.y[i]))

We then call the update and drawing method of the computer.

    def on_loop(self):
self.player.update()
self.computer.update()

....

def on_render(self):
self._display_surf.fill((0,0,0))
self.player.draw(self._display_surf, self._image_surf)
self.apple.draw(self._display_surf, self._apple_surf)
self.computer.draw(self._display_surf, self._image_surf)
pygame.display.flip()

This will make the computer snake move and be drawn on the screen. It has the same properties as the human player.

You may like

Adding the intelligence to the computer player
Because this is a simple game, we do not need to create a complete thinking machine inside the game. We simply need some basic intelligence exhibited by our computer player. Intelligence in games is often quite limited because most of the time more complexity is not neccesary or there simply is not the time available to implement clever algorithms.

The algorithm we will add will simply go to the destination. It will neglect any obstacles (the human player).

def target(self,dx,dy):
if self.x[0] > dx:
self.moveLeft()

if self.x[0] < dx:
self.moveRight()

if self.x[0] == dx:
if self.y[0] < dy:
self.moveDown()

if self.y[0] > dy:
self.moveUp()

Complete code
We end up with this complete code:

from pygame.locals import *
from random import randint
import pygame
import time

class Apple:
x = 0
y = 0
step = 44

def __init__(self,x,y):
self.x = x * self.step
self.y = y * self.step

def draw(self, surface, image):
surface.blit(image,(self.x, self.y))


class Player:
x = [0]
y = [0]
step = 44
direction = 0
length = 3

updateCountMax = 2
updateCount = 0

def __init__(self, length):
self.length = length
for i in range(0,2000):
self.x.append(-100)
self.y.append(-100)

# initial positions, no collision.
self.x[0] = 1*44
self.x[0] = 2*44

def update(self):

self.updateCount = self.updateCount + 1
if self.updateCount > self.updateCountMax:

# update previous positions
for i in range(self.length-1,0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]

# update position of head of snake
if self.direction == 0:
self.x[0] = self.x[0] + self.step
if self.direction == 1:
self.x[0] = self.x[0] - self.step
if self.direction == 2:
self.y[0] = self.y[0] - self.step
if self.direction == 3:
self.y[0] = self.y[0] + self.step

self.updateCount = 0


def moveRight(self):
self.direction = 0

def moveLeft(self):
self.direction = 1

def moveUp(self):
self.direction = 2

def moveDown(self):
self.direction = 3

def draw(self, surface, image):
for i in range(0,self.length):
surface.blit(image,(self.x[i],self.y[i]))



class Computer:
x = [0]
y = [0]
step = 44
direction = 0
length = 3

updateCountMax = 2
updateCount = 0

def __init__(self, length):
self.length = length
for i in range(0,2000):
self.x.append(-100)
self.y.append(-100)

# initial positions, no collision.
self.x[0] = 1*44
self.y[0] = 4*44

def update(self):

self.updateCount = self.updateCount + 1
if self.updateCount > self.updateCountMax:

# update previous positions
for i in range(self.length-1,0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]

# update position of head of snake
if self.direction == 0:
self.x[0] = self.x[0] + self.step
if self.direction == 1:
self.x[0] = self.x[0] - self.step
if self.direction == 2:
self.y[0] = self.y[0] - self.step
if self.direction == 3:
self.y[0] = self.y[0] + self.step

self.updateCount = 0


def moveRight(self):
self.direction = 0

def moveLeft(self):
self.direction = 1

def moveUp(self):
self.direction = 2

def moveDown(self):
self.direction = 3


def target(self,dx,dy):
if self.x[0] > dx:
self.moveLeft()

if self.x[0] < dx:
self.moveRight()

if self.x[0] == dx:
if self.y[0] < dy:
self.moveDown()

if self.y[0] > dy:
self.moveUp()

def draw(self, surface, image):
for i in range(0,self.length):
surface.blit(image,(self.x[i],self.y[i]))


class Game:
def isCollision(self,x1,y1,x2,y2,bsize):
if x1 >= x2 and x1 <= x2 + bsize:
if y1 >= y2 and y1 <= y2 + bsize:
return True
return False

class App:

windowWidth = 800
windowHeight = 600
player = 0
apple = 0

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self._apple_surf = None
self.game = Game()
self.player = Player(5)
self.apple = Apple(8,5)
self.computer = Computer(5)

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

pygame.display.set_caption('Pygame pythonspot.com example')
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()
self._apple_surf = pygame.image.load("apple.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):
self.computer.target(self.apple.x, self.apple.y)
self.player.update()
self.computer.update()

# does snake eat apple?
for i in range(0,self.player.length):
if self.game.isCollision(self.apple.x,self.apple.y,self.player.x[i], self.player.y[i],44):
self.apple.x = randint(2,9) * 44
self.apple.y = randint(2,9) * 44
self.player.length = self.player.length + 1

# does computer eat apple?
for i in range(0,self.player.length):
if self.game.isCollision(self.apple.x,self.apple.y,self.computer.x[i], self.computer.y[i],44):
self.apple.x = randint(2,9) * 44
self.apple.y = randint(2,9) * 44
#self.computer.length = self.computer.length + 1


# does snake collide with itself?
for i in range(2,self.player.length):
if self.game.isCollision(self.player.x[0],self.player.y[0],self.player.x[i], self.player.y[i],40):
print "You lose! Collision: "
print "x[0] (" + str(self.player.x[0]) + "," + str(self.player.y[0]) + ")"
print "x[" + str(i) + "] (" + str(self.player.x[i]) + "," + str(self.player.y[i]) + ")"
exit(0)

pass

def on_render(self):
self._display_surf.fill((0,0,0))
self.player.draw(self._display_surf, self._image_surf)
self.apple.draw(self._display_surf, self._apple_surf)
self.computer.draw(self._display_surf, self._image_surf)
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
pygame.event.pump()
keys = pygame.key.get_pressed()

if (keys[K_RIGHT]):
self.player.moveRight()

if (keys[K_LEFT]):
self.player.moveLeft()

if (keys[K_UP]):
self.player.moveUp()

if (keys[K_DOWN]):
self.player.moveDown()

if (keys[K_ESCAPE]):
self._running = False

self.on_loop()
self.on_render()

time.sleep (50.0 / 1000.0);
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()

python snake python snake

You may like

Conclusion
You learned how to create a basic computer player using an very simple AI algorithm.

Next: Learn basic sidescroller logic

python ftp client

This article will show you how to use the File Transfer Protocol (FTP) with Python from a client side perspective. We use ftplib, a library that implements the FTP protocol. Using FTP we can create and access remote files through function calls.

Related course
Python Programming Bootcamp: Go from zero to hero

Directory listing

FTP is a protocol for transferring files between systems over a TCP network. It was first developed in 1971 and has since been widely adopted as an effective way to share large files over the internet. File Transfer Protocol (often abbreviated FTP) is an application- layer protocol.

We can list the root directory using this little snippet:

import ftplib

ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

data = []

ftp.dir(data.append)

ftp.quit()

for line in data:
print "-", line

This will output the directory contents. in a simple console style output. If you want to show a specific directory you must change the directory after connecting with the ftp.cwd(‘/‘) function where the parameter is the directory you want to change to.

import ftplib

ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

data = []

ftp.cwd('/pub/') # change directory to /pub/
ftp.dir(data.append)

ftp.quit()

for line in data:
print "-", line

Download file
To download a file we use the retrbinary() function. An example below:

import ftplib
import sys

def getFile(ftp, filename):
try:
ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
except:
print "Error"


ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

ftp.cwd('/pub/') # change directory to /pub/
getFile(ftp,'README.nluug')

ftp.quit()

Uploading files
We can upload files using the storlines() command. This will upload the file README.nluug in the main directory. If you want to upload in another directory combine it with the cwd() function.

import ftplib
import os

def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

ftp = ftplib.FTP("127.0.0.1")
ftp.login("username", "password")

upload(ftp, "README.nluug")

Related course
Python Programming Bootcamp: Go from zero to hero

Other functions
For other functions please refer to the official library documentation.

python snake game

pygame

Pygame Python Pygame Python

Welcome to the first tutorial of the series: Building games with Pygame. Games you create with Pygame can be run on any machine that supports Python, including Windows, Linux and Mac OS.

In this tutorial we will explain the fundamental of building a game with Pygame. We’ll start of with the basics and will teach you how to create the basic framework. In the next tutorials you will learn how to make certain types of games.

You may like
Create Space Invaders with Python

PyGame introduction
You’ll end up with a program similar to the one on the right:

A game always starts in an order similar to this (pseudo code):

initialize()
while running():
game_logic()
get_input()
update_screen()
deinitialize()

The game starts with initialization. All graphics are loaded, sounds are loaded, levels are loaded and any data that needs to be loaded. The game continues running until it receives a quit event. In this game loop we update the game, get input and update the screen. Depending on the game the implementation widely varies, but this fundamental structure is common in all games.

In Pygame we define this as:

import pygame
from pygame.locals import *

class App:

windowWidth = 640
windowHeight = 480
x = 10
y = 10

def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)
self._running = True
self._image_surf = pygame.image.load("pygame.png").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False

def on_loop(self):
pass

def on_render(self):
self._display_surf.blit(self._image_surf,(self.x,self.y))
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()

The Pygame program starts with the constructor init(). Once that is finished on_execute() is called. This method runs the game: it updates the events, updates the screen. Finally, the game is deinitialized using on_cleanup().

In the initialiasation phase we set the screen resolution and start the Pygame library:

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE)

We also load the image.

self._image_surf = pygame.image.load("pygame.png").convert()

This does not draw the image to the screen, that occurs in on_render().

def on_render(self):
self._display_surf.blit(self._image_surf,(self.x,self.y))
pygame.display.flip()

The blit method draws the image (image_surf) to the coordinate (x,y). In Pygame the coordinates start from (0,0) top left to (wind0wWidth, windowHeight). The method call pygame.display.flip() updates the screen.

You may like
Create Space Invaders with Python

Continue the next tutorial and learn how to add game logic and build games :-)

python chrome extension

face detection python

In this tutorial you will learn how to apply face detection with Python. As input video we will use a Google Hangouts video. There are tons of Google Hangouts videos around the web and in these videos the face is usually large enough for the software to detect the faces.

Detection of faces is achieved using the OpenCV (Open Computer Vision) library. The most common face detection method is to extract cascades. This technique is known to work well with face detection. You need to have the cascade files (included in OpenCV) in the same directory as your program.

Related course
Master Computer Vision with OpenCV and Python

Video with Python OpenCV

To analyse the input video we extract each frame.  Each frame is shown for a brief period of time. Start with this basic program:

#! /usr/bin/python

import cv2

vc = cv2.VideoCapture('video.mp4')
c=1
fps = 24

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

while rval:
rval, frame = vc.read()
cv2.imshow("Result",frame)
cv2.waitKey(1000 / fps);
vc.release()

Upon execution you will see the video played without sound. (OpenCV does not support sound). Inside the while loop we have every video frame inside the variable frame. 

Face detection with OpenCV


We will display a rectangle on top of the face. To avoid flickering of the rectangle, we will show it at it latest known position if the face is not detected.

#! /usr/bin/python

import cv2

face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
vc = cv2.VideoCapture('video.mp4')

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

roi = [0,0,0,0]

while rval:
rval, frame = vc.read()

# resize frame for speed.
frame = cv2.resize(frame, (300,200))

# face detection.
faces = face_cascade.detectMultiScale(frame, 1.8, 2)
nfaces = 0
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
nfaces = nfaces + 1
roi = [x,y,w,h]

# undetected face, show old on position.
if nfaces == 0:
cv2.rectangle(frame,(roi[0],roi[1]),(roi[0]+roi[2],roi[1]+roi[3]),(0,0,255),2)

# show result
cv2.imshow("Result",frame)
cv2.waitKey(1);
vc.release()

In this program we simply assumed there is one face in the video screen. We reduced the size of the screen to speed up the processing time. This is fine in most cases because detection will work fine in lower resolutions.  If you want to execute the face detection in “real time”, keeping the computational cycle short is mandatory. An alternative to this implementation is to process first and display later.

A limitation of this technique is that it does not always detect faces and faces that are very small or occluded may not be detected. It may show false positives such as a bag detected as face.  This technique works quite well on certain type of input videos.

Download Computer Vision Examples and Course

Car tracking with cascades

Car Tracking with OpenCV Car Tracking with OpenCV

In this tutorial we will look at vehicle tracking using haar features. We have a haar cascade file trained on cars.

The program will detect regions of interest, classify them as cars and show rectangles around them.

Related course:
Master Computer Vision with OpenCV

Detecting with cascades

Lets start with the basic cascade detection program:

#! /usr/bin/python

import cv2

face_cascade = cv2.CascadeClassifier('cars.xml')
vc = cv2.VideoCapture('road.avi')

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

while rval:
rval, frame = vc.read()

# car detection.
cars = face_cascade.detectMultiScale(frame, 1.1, 2)

ncars = 0
for (x,y,w,h) in cars:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
ncars = ncars + 1

# show result
cv2.imshow("Result",frame)
cv2.waitKey(1);
vc.release()

This will detect cars in the screen but also noise and the screen will be jittering sometimes. To avoid all of these, we have to improve our car tracking algorithm.  We decided to come up with a simple solution.

Related course:
Master Computer Vision with OpenCV

Car tracking algorithm


For every frame:
  • Detect potential regions of interest
  • Filter detected regions based on vertical,horizontal similarity
  • If its a new region, add to the collection
  • Clear collection every 30 frames

Removing false positives
The mean square error function is used to remove false positives. We compare vertical and horizontal sides of the images. If the difference is to large or to small it cannot be a car.

ROI detection
A car may not be detected in every frame. If a new car is detected, its added to the collection.
We keep this collection for 30 frames, then clear it.

#!/usr/bin/python

import cv2
import numpy as np

def diffUpDown(img):
# compare top and bottom size of the image
# 1. cut image in two
# 2. flip the top side
# 3. resize to same size
# 4. compare difference
height, width, depth = img.shape
half = height/2
top = img[0:half, 0:width]
bottom = img[half:half+half, 0:width]
top = cv2.flip(top,1)
bottom = cv2.resize(bottom, (32, 64))
top = cv2.resize(top, (32, 64))
return ( mse(top,bottom) )

def diffLeftRight(img):
# compare left and right size of the image
# 1. cut image in two
# 2. flip the right side
# 3. resize to same size
# 4. compare difference
height, width, depth = img.shape
half = width/2
left = img[0:height, 0:half]
right = img[0:height, half:half + half-1]
right = cv2.flip(right,1)
left = cv2.resize(left, (32, 64))
right = cv2.resize(right, (32, 64))
return ( mse(left,right) )

def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
return err

def isNewRoi(rx,ry,rw,rh,rectangles):
for r in rectangles:
if abs(r[0] - rx) < 40 and abs(r[1] - ry) < 40:
return False
return True

def detectRegionsOfInterest(frame, cascade):
scaleDown = 2
frameHeight, frameWidth, fdepth = frame.shape
# Resize
frame = cv2.resize(frame, (frameWidth/scaleDown, frameHeight/scaleDown))
frameHeight, frameWidth, fdepth = frame.shape

# haar detection.
cars = cascade.detectMultiScale(frame, 1.2, 1)

newRegions = []
minY = int(frameHeight*0.3)

# iterate regions of interest
for (x,y,w,h) in cars:
roi = [x,y,w,h]
roiImage = frame[y:y+h, x:x+w]

carWidth = roiImage.shape[0]
if y > minY:
diffX = diffLeftRight(roiImage)
diffY = round(diffUpDown(roiImage))
if diffX > 1600 and diffX < 3000 and diffY > 12000:
rx,ry,rw,rh = roi
newRegions.append( [rx*scaleDown,ry*scaleDown,rw*scaleDown,rh*scaleDown] )

return newRegions

def detectCars(filename):
rectangles = []
cascade = cv2.CascadeClassifier('cars.xml')
vc = cv2.VideoCapture(filename)

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

roi = [0,0,0,0]
frameCount = 0

while rval:
rval, frame = vc.read()
frameHeight, frameWidth, fdepth = frame.shape

newRegions = detectRegionsOfInterest(frame, cascade)
for region in newRegions:
if isNewRoi(region[0],region[1],region[2],region[3],rectangles):
rectangles.append(region)

for r in rectangles:
cv2.rectangle(frame,(r[0],r[1]),(r[0]+r[2],r[1]+r[3]),(0,0,255),3)

frameCount = frameCount + 1
if frameCount > 30:
frameCount = 0
rectangles = []

# show result
cv2.imshow("Result",frame)
cv2.waitKey(1);
vc.release()

detectCars('road.avi')

Final notes
The cascades are not  rotation invariant, scale and translation invariant. In addition, Detecting vehicles with haar cascades may work reasonably well, but there is gain with other algorithms (salient points).

You may like:

Download Computer Vision Examples + Course

create csv file python

Spreadsheets often export CSV (comma seperated values) files, because they are easy to read and write. A csv file is simply consists of values, commas and newlines. While the file is called ‘comma seperate value’ file, you can use another seperator such as the pipe character.

Related course
Data Analysis with Python Pandas

Create a spreadsheet file (CSV) in Python
Let us create a file in CSV format with Python. We will use the comma character as seperator or delimter.

import csv

with open('persons.csv', 'wb') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['Name', 'Profession'])
filewriter.writerow(['Derek', 'Software Developer'])
filewriter.writerow(['Steve', 'Software Developer'])
filewriter.writerow(['Paul', 'Manager'])

Running this code will give us this fil persons.csv with this content:

Name,Profession
Derek,Software Developer
Steve,Software Developer
Paul,Manager

You can import the persons.csv file in your favorite office program.

python csv Spreadsheet file created in Python

 

Read a spreadsheet file (csv)
If you created a csv file, we can read files row by row with the code below:

import csv

# open file
with open('persons.csv', 'rb') as f:
reader = csv.reader(f)

# read file row by row
for row in reader:
print row

This will simply show every row as a list:

['Name', 'Profession']
['Derek', 'Software Developer']
['Steve', 'Software Developer']
['Paul', 'Manager']

Perhaps you want to store that into Python lists. We get the data from the csv file and then store it into Python lists. We skip the header with an if statement because it does not belong in the lists. Full code:

import csv

# create list holders for our data.
names = []
jobs = []

# open file
with open('persons.csv', 'rb') as f:
reader = csv.reader(f)

# read file row by row
rowNr = 0
for row in reader:
# Skip the header row.
if rowNr >= 1:
names.append(row[0])
jobs.append(row[1])

# Increase the row number
rowNr = rowNr + 1

# Print data
print names
print jobs

Result:

['Derek', 'Steve', 'Paul']
['Software Developer', 'Software Developer', 'Manager']

Most spreadsheet or office programs can export csv files, so we recommend you to create any type of csv file and play around with it :-)

Related course
Data Analysis with Python Pandas

python regex

Regular expressions are essentially a highly specialized programming language embedded inside Python that empowers you to specify the rules for the set of possible strings that you want to match.

In Python you need the re module for regular expressions usage. The grammar overview is on the bottom of this page.

Related course:
Python Programming Bootcamp: Go from zero to hero

The Match function


The match function is defined as:

re.match(pattern, string)

The parameters are:

If you want to match a string to a numberic sequence of exactly five, you can use this code:

Parameters Description
pattern a regular expression
string the input string
#!/usr/bin/python
import re

input = raw_input("Enter an input string:")
m = re.match('\d{5}\Z',input)

if m:
print("True")
else:
print("False")

Example outputs:

Email validation regex


We can use the same function to validate email address. The grammar rules are seen in re.compile and in the grammar table.

String Match
12345 True
12358 True
55555 True
123 False
123K5 False
5555555 False
#!/usr/bin/python
import re

input = raw_input("Enter an input string:")
m = re.match('[^@][email protected][^@]+\.[^@]+',input)

if m:
print("True")
else:
print("False")

The Search Function


The search function is defined as:

re.search(pattern, string)

The parameters are:

To search if an e-mail address is in a string:

Parameter Description
pattern a regular expression, defines the string to be searched
string the search space
#!/usr/bin/python
import re

input = "Contact me by [email protected] or at the office."

m = re.search('[^@][email protected][^@]+\.[^@]+',input)

if m:
print("String found.")
else:
print("Nothing found.")

Regular Expression Examples


A few examples of regular expressions:

Regular Expression Grammar


Overview of the regex grammar:
Example Regex
IP address (([2][5][0-5]\.)|([2][0-4][0-9]\.)|([0-1]?[0-9]?[0-9]\.)){3}(([2][5][0-5])|([2][0-4][0-9])|([0-1]?[0-9]?[0-9]))
Email [^@][email protected][^@]+\.[^@]+
Date MM/DD/YY (\d+/\d+/\d+)
Integer (positive) (?<![-.])\b[0-9]+\b(?!\.[0-9])
Integer [+-]?(?<!\.)\b[0-9]+\b(?!\.[0-9])
Float (?<=>)\d+.\d+|\d+
Hexadecimal \s–([0-9a-fA-F]+)(?:–)?\s

sqlalchemy orm

An object relational mapper maps a relational database system to objects. If you are unfamiliar with object orientated programming, read this tutorial first. The ORM is independent of which relational database system is used. From within Python, you can talk to objects and the ORM will map it to the database. In this article you will learn to use the SqlAlchemy ORM.

What an ORM does is shown in an illustration below:


Related course:

Creating a class to feed the ORM
We create the file tabledef.py. In this file we will define a class Student. An abstract visualization of the class below:

Observe we do not define any methods, only variables of the class. This is because we will map this class to the database and thus won’t need any methods.

This is the contents of tabledef.py:

Regex Description
\d Matches any decimal digit; this is equivalent to the class [0-9]
\D Matches any non-digit character; this is equivalent to the class [^0-9].
\s Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
\S Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
\w Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
\W Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].
\Z Matches only at end of string
[..] Match single character in brackets
[^..] Match any single character not in brackets
. Match any character except newline
$ Match the end of the string
* Match 0 or more repetitions
+ 1 or more repetitions
{m} Exactly m copies of the previous RE should be matched.
| Match A or B. A|B
? 0 or 1 repetitions of the preceding RE
[a-z] Any lowercase character
[A-Z] Any uppercase character
[a-zA-Z] Any character
[0-9] Any digit
ORM Object Relational Mapping ORM Object Relational Mapping. We communicate with the database using the ORM and only use Python objects and classes. class Class definition
from sqlalchemy import *
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

engine = create_engine('sqlite:///student.db', echo=True)
Base = declarative_base()

########################################################################
class Student(Base):
""""""
__tablename__ = "student"

id = Column(Integer, primary_key=True)
username = Column(String)
firstname = Column(String)
lastname = Column(String)
university = Column(String)

#----------------------------------------------------------------------
def __init__(self, username, firstname, lastname, university):
""""""
self.username = username
self.firstname = firstname
self.lastname = lastname
self.university = university

# create tables
Base.metadata.create_all(engine)

Execute with:

python tabledef.py

The ORM created the database file tabledef.py. It will output the SQL query to the screen, in our case it showed:

CREATE TABLE student (
id INTEGER NOT NULL,
username VARCHAR,
firstname VARCHAR,
lastname VARCHAR,
university VARCHAR,
PRIMARY KEY (id)
)

Thus, while we defined a class, the ORM created the database table for us. This table is still empty.

Inserting data into the database
The database table is still empty. We can insert data into the database using Python objects. Because we use the SqlAlchemy ORM we do not have to write a single SQL query. We now simply create Python objects that we feed to the ORM. Save the code below as dummy.py

import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tabledef import *

engine = create_engine('sqlite:///student.db', echo=True)

# create a Session
Session = sessionmaker(bind=engine)
session = Session()

# Create objects
user = Student("james","James","Boogie","MIT")
session.add(user)

user = Student("lara","Lara","Miami","UU")
session.add(user)

user = Student("eric","Eric","York","Stanford")
session.add(user)

# commit the record the database
session.commit()

Execute with:

python dummy.py

The ORM will map the Python objects to a relational database. This means you do not have any direct interaction from your application, you simply interact with objects. If you open the database with SQLiteman or an SQLite database application you’ll find the table has been created:

Data in database table. Data in database table.

Query the data
We can query all items of the table using the code below. Note that Python will see every record as a unique object as defined by the Students class. Save the code as demo.py

import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tabledef import *

engine = create_engine('sqlite:///student.db', echo=True)

# create a Session
Session = sessionmaker(bind=engine)
session = Session()

# Create objects
for student in session.query(Student).order_by(Student.id):
print student.firstname, student.lastname

On execution you will see:

James Boogie
Lara Miami
Eric York

To select a single object use the filter() method. A demonstration below:

import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from tabledef import *

engine = create_engine('sqlite:///student.db', echo=True)

# create a Session
Session = sessionmaker(bind=engine)
session = Session()

# Select objects
for student in session.query(Student).filter(Student.firstname == 'Eric'):
print student.firstname, student.lastname

Output:

Eric York

Finally, if you do not want the ORM the output any of the SQL queries change the create_engine statement to:

engine = create_engine('sqlite:///student.db', echo=False)

pyqt qml

If you have not done our first PyQT tutorial yet, you should do it, it’s fun! In this tutorial we will use PyQT4 and a user interface markup language, a language that describes the graphical  user interfaces and controls

Related course:

QML and PyQT


An excerpt of user interface markup graphical user interfaces and language code could look like:

 Rectangle {
id: simplebutton
color: "grey"
width: 150; height: 75

Essentially the language tells what the interface should look like. The language we will be using is called QML.

QTCreator


Start a programmed called QTCreator.   The tutorial will be quite graphical to help you through the whole process. Simply type qtcreator in the terminal or start it from the menu list.  This screen should pop up:

qtcreator qtcreator

Creating a GUI


Press the big New Project button. Select QT Quick Application from the menu below. Finally press Choose on the bottom right.

qtquick qtquick

A new popup will appear:

kde create kde create

Type a name and a valid path to save your project. Then press Next.  Select QT Quick 2.0 from the menu list. Press Next. Press Finish. Immediately a user interface defined in the QML language will appear.

qt quick qt quick

Like all great programmers we are going to solve things the most lazy way possible.  Instead of typing all the QML code by hand we are going to press the Design button that is on the left of the screen.  A drag and drop screen should appear now.

draganddrop draganddrop

We drag an image onto the area and select the source on the right. Save the project. Open a terminal and locate the qml file you just created. Alternatively you could simply copy the code in the edit box and save it to a .qml file. Enter the command:

qmlviewer main.qml

This will display the windows as defined in the qml file without any functionality. It is simply a viewer for the interface.  We then create some code to load this QML definition:

import sys

from PyQt4.QtCore import QDateTime, QObject, QUrl, pyqtSignal
from PyQt4.QtGui import QApplication
from PyQt4.QtDeclarative import QDeclarativeView

app = QApplication(sys.argv)

# Create the QML user interface.
view = QDeclarativeView()
view.setSource(QUrl('main.qml'))
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
view.setGeometry(100, 100, 400, 240)
view.show()

app.exec_()

Finally we modify the first line main.qml to:

import Qt 4.7

Simply because our QtQuick was missing. Running

python run.py

Will now display our user interface as defined by QML:

QML_PyQT QML and PyQT

All of the code is simply PyQT, so you could add code like in the previous tutorial. These are the two methods to create a graphical interface with PyQT.  This method may be more loosely coupled to the code compared to the method of creating a GUI with QT in the previous tutorial. Despite that both are valid methods.

Download PyQT4 Examples (Bulk Collection)

You may like: Application GUI with PyQT4 or the PyQt4 Tutorials

wxpython

wxPython GUI wxPython GUI The wxPython module can be used to create a graphical application (GUI) that looks like a native application on any operating system including Windows, Mac OS X and Linux.

The official wxPython site has several screenshots and downloads for these platforms. wxPython is based on wxWidgets.

Related course: Creating GUI Applications with wxPython

Install wxPython

First download and install WxPython, the Python bindings for wxWidgets.

sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-doc wx2.8-examples wx2.8-headers wx2.8-i18n


Then install a GUI creator called wxglade:

sudo apt-get install wxglade


Using a GUI builder such as wxGlade will save you a lot of time, regardless of the GUI library you use. You can easily make complex graphical interfaces because you can simply drag and drop.

Creating our first GUI with Python and wxWidgets:



Start wxglade. You will see its user interface:

wxglade wxglade

Press on tiny window on the top left, below the file icon.

wxglade wxglade

Press OK. An empty window will now appear. Press on the tiny [OK] button in the wxGlade panel and press on the frame. The button will now appear. Press on Application in the tree window.

wxglade wxglade

Set the output file in the wxproperties window.

wxglade wxglade

If you look at the window note you can select multiple programming languages and two versions of wxWidgets. Select Python and wxWidgets 2.8. Finally press Generate code. (Do NOT name the file wx.py because the import needs wx, save it as window.py or something else).

Running wxglade code:


Run:

python window.py

And a window with a button will appear. Pressing the button will not do anything. To start a function when pressing the button, we need to define a so called Callback. This can be as simple as:

def OnButton(self, Event, button_label):
print "In OnButton:", button_label

Finally we bind the button to the callback function using:

self.button_1.Bind(wx.EVT_BUTTON, self.OnButton )

Pressing the button will now write a message to the command line. Instead of the boring command line message, we want to show a message box. This can be done using this command:

wx.MessageBox( "This is a message.", "Button pressed.");

wxPython example code


The full code below:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Fri Jan 23 22:59:56 2015
#

import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_1 = wx.Button(self, wx.ID_ANY, _("Hello World!"))
self.button_1.Bind(wx.EVT_BUTTON, self.OnButton )

self.__set_properties()
self.__do_layout()
# end wxGlade

def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle(_("wxWidgets button example. pythonspot.com "))
# end wxGlade

def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_1, 0, 0, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
# end wxGlade

def OnButton(event, button_label):
wx.MessageBox( "This is a message.", "Button pressed.");


# end of class MyFrame
if __name__ == "__main__":
gettext.install("app") # replace with the appropriate catalog name

app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()

Related course: Creating GUI Applications with wxPython

 

quantum computing python

So we want to make a quantum application with Python, but since we do not own any quantum computer we need to have a simulator first. Simulation will not have the same performance as an actual quantum computer but we will be able to run applications. We have a choice from three simulators: PyQu , QuTip and Qitensor. We decided to pick QuTip as it has a very large code base and as it has the most recent changes. PyQu hasn’t been updated since 2010 and Qitensor since a year or so.

Related course:
Quantum Computing: An Applied Approach

Installing
We use a Unix machine in this tutorial, but you should be fine with any other operating system. Install using:

sudo add-apt-repository ppa:jrjohansson/qutip-releases
sudo apt-get update
sudo apt-get install python-qutip

We then start Python from the command line and type the commands listed below ( >>> ).

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from qutip import *
&gt;&gt;&gt; about()

QuTiP: Quantum Toolbox in Python
Copyright (c) 2011 and later.
Paul D. Nation &amp; Robert J. Johansson

QuTiP Version: 3.1.0
Numpy Version: 1.8.2
Scipy Version: 0.13.3
Cython Version: 0.20.1post0
Matplotlib Version: 1.3.1
Fortran mcsolver: True
scikits.umfpack: False
Python Version: 2.7.6
Platform Info: Linux (i686)
Installation path: /usr/lib/python2.7/dist-packages/qutip

This indicates that Qutip has been correctly installed.

The Quantum data structure
In quantum systems we need a data structure that is capable of encapsulating the properties of a quantum operator and ket/bra vectors, we use the Qobj data structure for that. In other words, to effectively simulate a quantum application we need to use the appropriate data structure. Consider the example below:

#!/usr/bin/env python
from qutip import *
from scipy import *

r = rand(4, 4)
print Qobj(r)


And execute with:

python quantum.py

This will output the quantum object:

Quantum object: dims = [[4], [4]], shape = [4, 4], type = oper, isherm = False
Qobj data =
[[ 0.25529374 0.75548592 0.85680266 0.1438253
[ 0.75070138 0.68628867 0.97435624 0.77396516]
[ 0.69819458 0.81714756 0.2604015 0.69051901]
[ 0.0898242 0.05292657 0.49134431 0.4433644 ]]

If you want to specify user input yourself you could use:

#!/usr/bin/env python
from qutip import *
from scipy import *

x = array([[1],[2],[3],[4],[5]])
q = Qobj(x)
print q

This quantum object will simply hold your user given data:

Quantum object: dims = [[5], [1]], shape = [5, 1], type = ket
Qobj data =
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]]

Quantum states and operators
A quantum system is not a simple two-level system, it has multiple states. QuTip includes some predefined states and quantum operators which are listed here.

Qubits and operators
We create a Qubit to hold data. Th Qubit is the quantum analogue of the classical bit. Unlike traditional bits, the qubit can be in a superposition of both states at the same time, a property which is fundamental to quantum computing. The code below will create a qubit:

#!/usr/bin/env python
from qutip import *
from scipy import *

spin = basis(2, 0)
print spin

You can now apply quantum system operators on the qubit:

#!/usr/bin/env python
from qutip import *
from scipy import *

spin = basis(2, 0)
print sigmam() * spin
print sigmap() * spin

Combining qubits
To describe the states of two coupled qubits we need to take the tensor product of the state vectors for each of the system components. Let us try that:

#!/usr/bin/env python
from qutip import *
from scipy import *

q1 = basis(2, 0)
q2 = basis(2,0)

print q1
print q2
print tensor(q1,q2)

The output we will get is:

Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket
Qobj data =
[[ 1.]
[ 0.]]
Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket
Qobj data =
[[ 1.]
[ 0.]]
Quantum object: dims = [[2, 2], [1, 1]], shape = [4, 1], type = ket
Qobj data =
[[ 1.]
[ 0.]
[ 0.]
[ 0.]]

Whats next?
We have built some very simply quantum applications using this simple introduction. Perhaps you want to create an actually useful application, if so you could study more about quantum computing and complete the tutorial at http://qutip.org/docs/latest/index.html

irc bot

python requests library


12