Python is a general-purpose computer programming language.
A python program should be save as a file with a .py extension.
Try this code:
print("Hello World!") print("This is a Python program.") |
Expected output:
Hello World! This is a Python program |
If you are using the interpreter use:
python program. py |
To define a string simply type text between quotes. Python accepts single, double and triple quotes.
To output text (string) to the screen:
s = "hello world" print() |
To get text from keyboard:
name = input("Enter name: ") print(name ) |
If you use an old Python version (2.x), you need to use:
name = raw_input("Enter name: ") print(name ) |
To test your version:
To test if two strings are equal use the equality operator (==).
#!/usr/bin/python sentence = "The cat is brown" q = "cat" if q == sentence: print('strings equal') |
To test if two strings are not equal use the inequality operator (!=)
#!/usr/bin/python sentence = "The cat is brown" q = "cat" if q != sentence: print('strings equal') |
A string is a series of characters, they are mostly used to display text.
To define a string simply type text between quotes. Python accepts single, double and triple quotes.
Python indexes the characters of a string, every index is associated with a unique character. For instance, the characters in the string ‘python’ have indices:
The 0th index is used for the first character of a string. Try the following:
#!/usr/bin/python s = "Hello Python" print() # prints whole string print([0]) # prints "H" print([1]) # prints "e" |
Given a string s, the syntax for a slice is:
s [ startIndex : pastIndex ] |
The startIndex is the start index of the string. pastIndex is one past the end of the slice.
If you omit the first index, the slice will start from the beginning. If you omit the last index, the slice will go to the end of the string. For instance:
#!/usr/bin/python s = "Hello Python" print([0:2]) # prints "He" print([2:4]) # prints "ll" print([6: ]) # prints "Python" |
Numbers can be of one of these datatypes:
Example of numeric variables:
x = 1 y = 1.234 z = True |
You can output them to the screen using the print() function.
x = 1 y = 1.234 z = True print(x ) print(y ) print(z ) |
Python supports arithmetic operations like addition (+), multiplication (*), division (/) and subtractions (-).
#!/usr/bin/env python x = 3 y = 8 sum = x + y print(sum) |
Python 3
Use the input() function to get text input, convert to a number using int() or float().
#!/usr/bin/env python x = int(input("Enter x:")) y = int(input("Enter y:")) sum = x + y print(sum) |
Python 2 (old version)
You can also ask the user for input using the raw_input function:
#!/usr/bin/env python x = int(raw_input("Enter x:")) y = int(raw_input("Enter y:")) sum = x + y print(sum) |
Lists is a sequence and a basic data structure. A list may contain strings (text) and numbers. A list is similar to an array in other programming languages, but has additional functionality.
We define lists with brackets []. To access the data, these same brackets are used.
Example list usage:
#!/usr/bin/python l = [ "Drake", "Derp", "Derek", "Dominique" ] print(l ) # prints all elements print(l [0]) # print first element print(l [1]) # prints second element |
We can use the functions append() and remove() to manipulate the list.
#!/usr/bin/python l = [ "Drake", "Derp", "Derek", "Dominique" ] print(l ) # prints all elements l. append("Victoria") # add element. print(l ) # print all elements l. remove("Derp") # remove element. l. remove("Drake") # remove element. print(l ) # print all elements. |
We can sort the list using the sort() function.
#!/usr/bin/python l = [ "Drake", "Derp", "Derek", "Dominique" ] print(l ) # prints all elements l. sort() # sorts the list in alphabetical order print(l ) # prints all elements |
If you want to have the list in descending order, simply use the reverse() function.
#!/usr/bin/python l = [ "Drake", "Derp", "Derek", "Dominique" ] print(l ) # prints all elements l. sort() # sorts the list in alphabetical order l. reverse() # reverse order. print(l ) # prints all elements |
In Python you can define conditional statements, known as if-statements.
A block of code is executed if certain conditions are met.
Consider this application, it executes either the first or second code depending on the value of x.
#!/usr/bin/python x = 3 if x < 10: print("x smaller than 10") else: print("x is bigger than 10 or equal") |
If you set x to be larger than 10, it will execute the second code block. We use indentation (4 spaces) to define the blocks.
A little game:
A variable may not always be defined by the user, consider this little game:
age = 24 print "Guess my age, you have 1 chances!" guess = int(raw_input("Guess: ")) if guess != age: print("Wrong!") else: print("Correct") |
A word on conditional operators
Operator | Description |
!= | not equal |
== | equals |
> | greater than |
< | smaller than |
Do not confuse the assignment operator (=) with the equals operator (==).
The most straightforward way to do multiple conditions is nesting:
a = 12 b = 33 if a > 10: if b > 20: print("Good") |
This can quickly become difficult to read, consider combining 4 or 6 conditions. Luckily Python has a solution for this, we can combine conditions using the and keyword.
guess = 24 if guess > 10 and guess < 20: print("In range") else: print("Out of range") |
Sometimes you may want to use the or operator.
A function is reusable code that can be called anywhere in your program. Functions improve readability of your code: it’s easier for someone to understand code using functions instead of long lists of instructions.
On top of that, functions can be reused or modified which also improve testability and extensibility.
We use this syntax to define as function:
def function (parameters ): instructions return value |
The def keyword tells Python we have a piece of reusable code (A function). A program
can have many functions.We can call the function using function(parameters).
#!/usr/bin/python def f (x ): return(x*x ) print(f (3)) |
Output:
9 |
The function has one parameter, x. The return value is the value the function returns. Not all functions have to
return something.We can pass multiple variables:
#!/usr/bin/python def f (x,y ): print('You called f(x,y) with the value x = ' + str(x ) + ' and y = ' + str(y )) print('x * y = ' + str(x*y )) f (3,2) |
Output:
You called f (x,y ) with the value x = 3 and y = 2 x * y = 6 |
There are two types of variables: global variables and local variables.
A global variable can be reached anywhere in the code, a local only in the scope.
Local variables can only be reached in their scope.
The example below has two local variables: x and y.
def sum(x,y ): sum = x + y return sum print(sum(8,6)) |
The variables x and y can only be used inside the function sum, they don’t exist outside of the function.
Local variables cannot be used outside of their scope, this line will not work:
print(x ) |
A global variable can be used anywhere in the code.
In the example below we define a global variable z
z = 10 def afunction (): global z print(z ) afunction () print(z ) |
The global variable z can be used all throughout the program, inside functions or outside.
A global variable can modified inside a function and change for the entire program:
z = 10 def afunction (): global z z = 9 afunction () print(z ) |
After calling afunction(), the global variable is changed for the entire program.
Local and global variables can be used together in the same program.
Try to determine the output of this program:
z = 10 def func1 (): global z z = 3 def func2 (x,y ): global z return x+y+z func1 () total = func2 (4,5) print(total ) |
Scope
Variables can only reach the area in which they are defined, which is called scope. Think of it as the area of code where variables can be used. Python supports global variables (usable in the entire program) and local variables.
By default, all variables declared in a function are local variables. To access a global variable inside a function, it’s required to explicitly define ‘global variable’.
Example
Below we’ll examine the use of local variables and scope. This will not work:
#!/usr/bin/python def f (x,y ): print('You called f(x,y) with the value x = ' + str(x ) + ' and y = ' + str(y )) print('x * y = ' + str(x*y )) z = 4 # cannot reach z, so THIS WON'T WORK z = 3 f (3,2) |
but this will:
#!/usr/bin/python def f (x,y ): z = 3 print('You called f(x,y) with the value x = ' + str(x ) + ' and y = ' + str(y )) print('x * y = ' + str(x*y )) print(z ) # can reach because variable z is defined in the function f (3,2) |
Let’s examine this further:
#!/usr/bin/python def f (x,y,z ): return x+y+z # this will return the sum because all variables are passed as parameters sum = f (3,2,1) print(sum) |
Calling functions in functions
We can also get the contents of a variable from another function:
#!/usr/bin/python def highFive (): return 5 def f (x,y ): z = highFive () # we get the variable contents from highFive() return x+y+z # returns x+y+z. z is reachable becaue it is defined above result = f (3,2) print(result ) |
If a variable can be reached anywhere in the code is called a global variable. If a variable is known only inside the scope, we call it a local variable.
Code can be repeated using a loop. Lines of code can be repeated N times, where N is manually configurable. In practice, it means code will be repeated until a condition is met. This condition is usually (x >=N) but it’s not the only possible condition.
Python has 3 types of loops: for loops, while loops and nested loops.
We can iterate a list using a for loop
#!/usr/bin/python items = [ "Abby","Brenda","Cindy","Diddy" ] for item in items: print(item ) |
The for loop can be used to repeat N times too:
#!/usr/bin/python for i in range(1,10): print(i ) |
If you are unsure how many times a code should be repeated, use a while loop.
For example,
correctNumber = 5 guess = 0 while guess != correctNumber: guess = int(input("Guess the number: ")) if guess != correctNumber: print('False guess') print('You guessed the correct number') |
We can combine for loops using nesting. If we want to iterate over an (x,y) field we could use:
#!/usr/bin/python for x in range(1,10): for y in range(1,10): print("(" + str(x ) + "," + str(y ) + ")") |
Nesting is very useful, but it increases complexity the deeper you nest.
The tuple data structure is used to store a group of data. The elements in this group are separated by a comma. Once created, the values of a tuple cannot change.
An empty tuple in Python would be defined as:
tuple = () |
A comma is required for a tuple with one item:
tuple = (3,) |
The comma for one item may be counter intuitive, but without the comma for a single item, you cannot access the element. For multiple items, you do not have to put a comma at the end. This set is an example:
personInfo = ("Diana", 32, "New York") |
The data inside a tuple can be of one or more data types such as text and numbers.
To access the data we can simply use an index. As usual, an index is a number between brackets:
#!/usr/bin/env python personInfo = ("Diana", 32, "New York") print(personInfo [0]) print(personInfo [1]) |
If you want to assign multiple variables at once, you can use tuples:
#!/usr/bin/env python name,age,country,career = ('Diana',32,'Canada','CompSci') print(country ) |
On the right side the tuple is written. Left of the operator equality operator are the corresponding output
variables.If you have an existing tuple, you can append to it with the + operator. You can only append a tuple to an existing tuple.
#!/usr/bin/env python x = (3,4,5,6) x = x + (1,2,3) print(x ) |
A dictionary can be thought of as an unordered set of key: value pairs.
A pair of braces creates an empty dictionary: {}. Each element can maps to a certain value. An integer or string can be used for the index. Dictonaries do not have an order.
Let us make a simple dictionary:
#!/usr/bin/python words = {} words ["Hello"] = "Bonjour" words ["Yes"] = "Oui" words ["No"] = "Non" words ["Bye"] = "Au Revoir" print(words ["Hello"]) print(words ["No"]) |
Output:
Bonjour Non |
We are by no means limited to single word defintions in the value part. A demonstration:
#!/usr/bin/python dict = {} dict['Ford'] = "Car" dict['Python'] = "The Python Programming Language" dict[2] = "This sentence is stored here." print(dict['Ford']) print(dict['Python']) print(dict[2]) |
Output:
Car The Python Programming Language This sentence is stored here. |
We can manipulate the data stored in a dictionairy after declaration. This is shown in the example below:
#!/usr/bin/python words = {} words ["Hello"] = "Bonjour" words ["Yes"] = "Oui" words ["No"] = "Non" words ["Bye"] = "Au Revoir" print(words ) # print key-pairs. del words ["Yes"] # delete a key-pair. print(words ) # print key-pairs. words ["Yes"] = "Oui!" # add new key-pair. print(words ) # print key-pairs. |
Output:
{'Yes': 'Oui', 'Bye': 'Au Revoir', 'Hello': 'Bonjour', 'No': 'Non'} {'Bye': 'Au Revoir', 'Hello': 'Bonjour', 'No': 'Non'} {'Yes': 'Oui!', 'Bye': 'Au Revoir', 'Hello': 'Bonjour', 'No': 'Non'} |
Python determines the datatype automatically, to illustrate:
x = 3 y = "text" |
It finds x is of type integer and y of type string.
Functions accept a certain datatype. For example, print only accepts the string datatype.
If you want to print numbers you will often need casting.
In this example below we want to print two numbers, one whole number (integer) and one floating point number.
x = 3 y = 2.15315315313532 print("We have defined two numbers,") print("x = " + str(x )) print("y = " + str(y )) |
We cast the variable x (integer) and the variable y (float) to a string using the str() function.
What if we have text that we want to store as number? We will have to cast again.
a = "135.31421" b = "133.1112223" c = float(a ) + float(b ) print(c ) |
In the example above we cast two variables with the datatype string to the datatype float.
To convert between datatypes you can use:
Function | Description |
---|---|
int(x) | Converts x to an integer |
long(x) | Converts x to a long integer |
float(x) | Converts x to a floating point number |
str(x) | Converts x to an string. x can be of the type float. integer or long. |
hex(x) | Converts x integer to a hexadecimal string |
chr(x) | Converts x integer to a character |
ord(x) | Converts character x to an integer |
Using the random module, we can generate pseudo-random numbers. The function random() generates a random number between zero and one [0, 0.1 .. 1]. Numbers generated with this module are not truly random but they are enough random for most purposes.
Random number between 0 and 1.
We can generate a (pseudo) random floating point number with this small code:
from random import * print random() # Generate a pseudo-random number between 0 and 1. |
Generate a random number between 1 and 100
To generate a whole number (integer) between one and one hundred use:
from random import * print randint (1, 100) # Pick a random number between 1 and 100. |
This will print a random integer. If you want to store it in a variable you can use:
from random import * x = randint (1, 100) # Pick a random number between 1 and 100. print x |
Random number between 1 and 10
To generate a random floating point number between 1 and 10 you can use the uniform() function
from random import * print uniform (1, 10) |
Picking a random item from a list
Fun with lists
We can shuffle a list with this code:
from random import * items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] shuffle (items ) print items |
To pick a random number from a list:
from random import * items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] x = sample (items, 1) # Pick a random item from the list print x [0] y = sample (items, 4) # Pick 4 random items from the list print y |
We can do the same thing with a list of strings:
from random import * items = ['Alissa','Alice','Marco','Melissa','Sandra','Steve'] x = sample (items, 1) # Pick a random item from the list print x [0] y = sample (items, 4) # Pick 4 random items from the list print y |
You have seen various types of data holders before: integers, strings, lists. But so far, we have not discussed how to read or write files.
You can read a file with the code below.
The file needs to be in the same directory as your program, if it is not you need to specify a path.
#!/usr/bin/env python # Define a filename. filename = "bestand.py" # Open the file as f. # The function readlines() reads the file. with open(filename ) as f: content = f. readlines() # Show the file contents line by line. # We added the comma to print single newlines and not double newlines. # This is because the lines contain the newline character '\n'. for line in content: print(line ), |
The first part of the code will read the file content. All of the lines read will be stored in the variable content. The second part will iterate over every line in the variable contents.
If you do not want to read the newline characters ‘\n’, you can change the statement f.readlines() to this:
content = f. read(). splitlines() |
Resulting in this code:
#!/usr/bin/env python # Define a filename. filename = "bestand.py" # Open the file as f. # The function readlines() reads the file. with open(filename ) as f: content = f. read(). splitlines() # Show the file contents line by line. # We added the comma to print single newlines and not double newlines. # This is because the lines contain the newline character '\n'. for line in content: print(line ) |
While the codes above work, we should always test if the file we want to open exists. We will test first if the file does not exist, if it does it will read the file else return an error. As in the code below:
#!/usr/bin/env python import os. path # Define a filename. filename = "bestand.py" if not os. path. isfile(filename ): print 'File does not exist.' else: # Open the file as f. # The function readlines() reads the file. with open(filename ) as f: content = f. read(). splitlines() # Show the file contents line by line. # We added the comma to print single newlines and not double newlines. # This is because the lines contain the newline character '\n'. for line in content: print(line ) |
Python supports writing files by default, no special modules are required. You can write a file using the .write() method with a parameter containing text data.
Before writing data to a file, call the open(filename,’w’) function where filename contains either the filename or the path to the filename. Finally, don’t forget to close the file.
The code below creates a new file (or overwrites) with the data.
#!/usr/bin/env python # Filename to write filename = "newfile.txt" # Open the file with writing permission myfile = open(filename, 'w') # Write a line to the file myfile. write('Written with Python\n') # Close the file myfile. close() |
The ‘w’ flag makes Python truncate the file if it already exists. That is to say, if the file contents
exists it will be replaced.If you simply want to add content to the file you can use the ‘a’ parameter.
#!/usr/bin/env python # Filename to append filename = "newfile.txt" # The 'a' flag tells Python to keep the file contents # and append (add line) at the end of the file. myfile = open(filename, 'a') # Add the line myfile. write('Written with Python\n') # Close the file myfile. close() |
A summary of parameters:
Flag | Action |
---|---|
r | Open file for reading |
w | Open file for writing (will truncate file) |
b | binary more |
r+ | open file for reading and writing |
a+ | open file for reading and writing (appends to end) |
w+ | open file for reading and writing (truncates files) |
Technology always evolves. What are classes and where do they come from?
1. Statements:
In the very early days of computing, programmers wrote only commands.
2. Functions:
Reusable group of statements, helped to structure that code and it improved readability.
3. Classes:
Classes are used to create objects which have functions and variables. Strings are examples of objects: A string book has the functions book.replace() and book.lowercase(). This style is often called object oriented programming.
Lets take a dive!
Python Courses:
We can create virtual objects in Python. A virtual object can contain variables and methods. A program may have many different types and are created from a class. Example:
class User:
name = ""
def __init__(self, name ):
self. name = name
def sayHello (self):
print "Hello, my name is " + self. name
# create virtual objects
james = User ("James")
david = User ("David")
eric = User ("Eric")
# call methods owned by virtual objects
james. sayHello()
david. sayHello() |
Run this program. In this code we have 3 virtual objects: james, david and eric. Each object is instance of the User class.
In this class we defined the sayHello() method, which is why we can call it for each of the objects. The __init__() method is called the constructor and is always called when creating an object. The variables owned by the class is in this case “name”. These variables are sometimes called class attributes.
We can create methods in classes which update the internal variables of the object. This may sound vague but
I will demonstrate with an example.We define a class CoffeeMachine of which the virtual objects hold the amount of beans and amount of water. Both are defined as a number (integer). We may then define methods that add or remove beans.
def addBean (self): self. bean = self. bean + 1 def removeBean (self): self. bean = self. bean - 1 |
We do the same for the variable water. As shown below:
class CoffeeMachine:
name = ""
beans = 0
water = 0
def __init__(self, name, beans, water ):
self. name = name
self. beans = beans
self. water = water
def addBean (self):
self. beans = self. beans + 1
def removeBean (self):
self. beans = self. beans - 1
def addWater (self):
self. water = self. water + 1
def removeWater (self):
self. water = self. water - 1
def printState (self):
print "Name = " + self. name
print "Beans = " + str(self. beans)
print "Water = " + str(self. water)
pythonBean = CoffeeMachine ("Python Bean", 83, 20)
pythonBean. printState()
print ""
pythonBean. addBean()
pythonBean. printState() |
Run this program. The top of the code defines the class as we described. The code below is where we create virtual objects. In this example we have exactly one object called “pythonBean”. We then call methods which change the internal variables, this is possible because we defined those methods inside the class. Output:
Name = Python Bean Beans = 83 Water = 20 Name = Python Bean Beans = 84 Water = 20 |
In an object oriented python program, you can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as encapsulation. Let’s start with an example.
This function cannot be called on the object directly, only from within the class.
#!/usr/bin/env python
class Car:
def __init__(self):
self.__updateSoftware ()
def drive (self):
print 'driving'
def __updateSoftware (self):
print 'updating software'
redcar = Car ()
redcar. drive()
#redcar.__updateSoftware() not accesible from object. |
This program will output:
updating software driving |
Encapsulation prevents from accessing accidentally, but not intentionally.
The private attributes and methods are not really hidden, they’re renamed adding “_Car” in the beginning of their name.
Objects can hold crucial data for your application and you do not want that data to be changeable from anywhere in the code.
An example:
#!/usr/bin/env python
class Car:
__maxspeed = 0
__name = ""
def __init__(self):
self.__maxspeed = 200
self.__name = "Supercar"
def drive (self):
print 'driving. maxspeed ' + str(self.__maxspeed )
redcar = Car ()
redcar. drive()
redcar.__maxspeed = 10 # will not change variable because its private
redcar. drive() |
If you want to change the value of a private variable, a setter method is used. This is simply a method that sets the value of a private variable.
#!/usr/bin/env python
class Car:
__maxspeed = 0
__name = ""
def __init__(self):
self.__maxspeed = 200
self.__name = "Supercar"
def drive (self):
print 'driving. maxspeed ' + str(self.__maxspeed )
def setMaxSpeed (self,speed ):
self.__maxspeed = speed
redcar = Car ()
redcar. drive()
redcar. setMaxSpeed(320)
redcar. drive() |
Why would you create them? Because some of the private values you may want to change after creation of the
object while others may not need to be changed at all.To summarize, in Python there are:
Type | Description |
---|---|
public methods | Accessible from anywhere |
private methods | Accessible only in their own class. starts with two underscores |
public variables | Accessible from anywhere |
private variables | Accesible only in their own class or by a method if defined. starts with two underscores |
Other programming languages have protected class methods too, but Python does not.
Encapsulation gives you more control over the degree of coupling in your code, it allows a class to change its implementation without affecting other parts of the code.
Given a single method or function, we can specify the number of parameters ourself.
Depending on the function definition, it can be called with zero, one, two or more parameters.
This is known as method overloading. Not all programming languages support method overloading, but Python does.
We create a class with one method sayHello(). The first parameter of this method is set to None, this gives us the option to call it with or without a parameter.
An object is created based on the class, and we call its method using zero and one parameter.
#!/usr/bin/env python class Human: def sayHello (self, name=None): if name is not None: print 'Hello ' + name else: print 'Hello ' # Create instance obj = Human () # Call the method obj. sayHello() # Call the method with a parameter obj. sayHello('Guido') |
Output:
Hello Hello Guido |
To clarify method overloading, we can now call the method sayHello() in two ways:
obj. sayHello() obj. sayHello('Guido') |
We created a method that can be called with fewer arguments than it is defined to allow.
We are not limited to two variables, your method could have more variables which are optional.
Classes can inherit functionality of other classes. If an object is created using a class that inherits from a superclass, the object will contain the methods of both the class and the superclass. The same holds true for variables of both the superclass and the class that inherits from the super class.
Python supports inheritance from multiple classes, unlike other popular programming languages.
We define a basic class named User:
class User:
name = ""
def __init__(self, name ):
self. name = name
def printName (self):
print "Name = " + self. name
brian = User ("brian")
brian. printName() |
This creates one instance called brian which outputs its given name. We create another class called Programmer.
class Programmer (User ):
def __init__(self, name ):
self. name = name
def doPython (self):
print "Programming Python" |
This looks very much like a standard class except than User is given in the parameters. This means all
functionality of the class User is accessible in the Programmer class.Full example of Python inheritance:
class User:
name = ""
def __init__(self, name ):
self. name = name
def printName (self):
print "Name = " + self. name
class Programmer (User ):
def __init__(self, name ):
self. name = name
def doPython (self):
print "Programming Python"
brian = User ("brian")
brian. printName()
diana = Programmer ("Diana")
diana. printName()
diana. doPython() |
The output:
Name = brian Name = Diana Programming Python |
Brian is an instance of User and can only access the method printName. Diana is an instance of Programmer, a class with inheritance from User, and can access both the methods in Programmer and User.
An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided.
We create a class (Human) with one inner class (Head).
An instance is created that calls a method in the inner class:
#!/usr/bin/env python
class Human:
def __init__(self):
self. name = 'Guido'
self. head = self. Head()
class Head:
def talk (self):
return 'talking...'
if __name__ == '__main__':
guido = Human ()
print guido. name
print guido. head. talk() |
Output:
Guido talking... |
In the program above we have the inner class Head() which has its own method. An inner class can have both methods and variables. In this example the constructor of the class Human (__init__) creates a new head object.
>You are by no means limited to the number of inner classes, for example this code will work too:
#!/usr/bin/env python
class Human:
def __init__(self):
self. name = 'Guido'
self. head = self. Head()
self. brain = self. Brain()
class Head:
def talk (self):
return 'talking...'
class Brain:
def think (self):
return 'thinking...'
if __name__ == '__main__':
guido = Human ()
print guido. name
print guido. head. talk()
print guido. brain. think() |
By using inner classes you can make your code even more object orientated. A single object can hold several sub objects. We can use them to add more structure to our programs.
We may not always know what kind of objects we want to create in advance.
Some objects can be created only at execution time after a user requests so.
Examples when you may use a factory method:
To deal with this we can use the factory method pattern.
The idea is to have one function, the factory, that takes an input string and outputs an object.
obj = Car. factory("Racecar") obj. drive() |
Key fact: a factory method returns (new) objects.
The type of object depends on the type of input string you specify. This technique could make your program more easily extensible also. A new programmer could easily add functionality by adding a new string and class,
without having to read all of the source code.The example below demonstrates a factory method. The factory method (named factory) returns a new object of either type depending on the input.
class Car (object): def factory (type): if type == "Racecar": return Racecar () if type == "Van": return Van () assert 0, "Bad car creation: " + type factory = staticmethod(factory ) class Racecar (Car ): def drive (self): print("Racecar driving.") class Van (Car ): def drive (self): print("Van driving.") # Create object using factory. obj = Car. factory("Racecar") obj. drive() |
Output:
Racecar driving. |
In English there are many examples of recursion:
You might wonder, what does this have to do with programming?
You may want to split a complex problem into several smaller ones. You are already familiar with loops or iterations. In some situations recursion may be a better solution.
In Python, a function is recursive if it calls itself and has a termination condition. Why a termination condition? To stop the function from calling itself ad infinity.
Recursion in with a list
Let’s start with a very basic example: adding all numbers in a list. Without recursion, this could be:
#!/usr/bin/env python def sum(list): sum = 0 # Add every number in the list. for i in range(0, len(list)): sum = sum + list[i ] # Return the sum. return sum print(sum([5,7,3,8,10])) |
Where we simply call the sum function, the function adds every element to the variable sum and returns. To do this recursively:
#!/usr/bin/env python def sum(list): if len(list) == 1: return list[0] else: return list[0] + sum(list[1: ]) print(sum([5,7,3,8,10])) |
If the length of the list is one it returns the list (the termination condition). Else, it returns the element and a call to the function sum() minus one element of the list. If all calls are executed, it returns reaches the termination condition and returns the answer.
Factorial with recursion
The mathematical definition of factorial is: n! = n * (n-1)!, if n > 1 and f(1) = 1. Example: 3! = 3 x 2 x 1 = 6. We can implement this in Python using a recursive function:
#!/usr/bin/env python def factorial (n ): if n == 1: return 1 else: return n * factorial (n-1) print factorial (3) |
When calling the factorial function n = 3. Thus it returns n * factorial(n-1). This process will continue until n =
1. If n==1 is reached, it will return the result.Everytime a function calls itself and stores some memory. Thus, a recursive function could hold much more memory than a traditional function. Python stops the function calls after a depth of 1000 calls. If you run this example:
#!/usr/bin/env python def factorial (n ): if n == 1: return 1 else: return n * factorial (n-1) print factorial (3000) |
You will get the error:
RuntimeError: maximum recursion depth exceeded |
In other programming languages, your program could simply crash. You can resolve this by modifying the number of recursion calls such as:
#!/usr/bin/env python import sys sys. setrecursionlimit(5000) def factorial (n ): if n == 1: return 1 else: return n * factorial (n-1) print factorial (3000) |
but keep in mind there is still a limit to the input for the factorial function. For this reason, you should use recursion wisely. As you learned now for the factorial problem, a recursive function is not the best solution. For other problems such as traversing a directory, recursion may be a good solution.
We can track events in a software application, this is known as logging. Let’s start with a simple example, we will log a warning message.
As opposed to just printing the errors, logging can be configured to disable output or save to a file. This is a big advantage to simple printing the errors.
import logging # print a log message to the console. logging. warning('This is a warning!') |
This will output:
WARNING:root:This is a warning! |
We can easily output to a file:
import logging logging. basicConfig(filename='program.log',level=logging. DEBUG) logging. warning('An example message.') logging. warning('Another message') |
The importance of a log message depends on the severity.
The logger module has several levels of severity. We set the level of severity using this line of code:
logging. basicConfig(level=logging. DEBUG) |
These are the levels of severity:
Type | Description |
---|---|
DEBUG | Information only for problem diagnostics |
INFO | The program is running as expected |
WARNING | Indicate something went wrong |
ERROR | The software will no longer be able to function |
CRITICAL | Very serious error |
The default logging level is warning, which implies that other messages are ignored. If you want to print debug or info log messages you have to change the logging level like so:
import logging logging. basicConfig(level=logging. DEBUG) logging. debug('Debug message') |
You can enable time for logging using this line of code:
logging. basicConfig(format='%(asctime)s %(message)s') |
An example below:
import logging logging. basicConfig(format='%(asctime)s %(message)s', level=logging. DEBUG) logging. info('Logging app started') logging. warning('An example logging message.') logging. warning('Another log message') |
Output:
2015-06-25 23:24:01,153 Logging app started 2015-06-25 23:24:01,153 An example message. 2015-06-25 23:24:01,153 Another message |
The subprocess module enables you to start new applications from your Python program. How cool is that?
You can start a process in Python using the Popen function call. The program below starts the unix program ‘cat’ and the second parameter is the argument. This is equivalent to ‘cat test.py’. You can start any program with any parameter.
#!/usr/bin/env python from subprocess import Popen, PIPE process = Popen (['cat', 'test.py'], stdout=PIPE, stderr=PIPE ) stdout, stderr = process. communicate() print stdout |
The process.communicate() call reads input and output from the process. stdout is the process output. stderr
will be written only if an error occurs. If you want to wait for the program to finish you can call Popen.wait().Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is:
subprocess. call(args, *, stdin=None, stdout=None, stderr=None, shell=False) # Run the command described by args. # Wait for command to complete, then return the returncode attribute. |
In the example below the full command would be “ls -l”
#!/usr/bin/env python import subprocess subprocess. call(["ls", "-l"])td> |
We can get the output of a program and store it in a string directly using check_output. The method is defined as:
subprocess. check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False) # Run command with arguments and return its output as a byte string. |
Example usage:
#!/usr/bin/env python import subprocess s = subprocess. check_output(["echo", "Hello World!"]) print("s = " + s ) |
In Python you can create threads using the thread module in Python 2.x or _thread module in Python 3. We will use the threading module to interact with it.
A thread is an operating system process with different features than a normal process:
When would you use threading? Usually when you want a function to occur at the same time as your program. If you create server software, you want the server not only listens to one connection but to many connections. In short, threads enable programs to execute multiple tasks at once.
Let’s create a thread program. In this program we will start 10 threads which will each output their id.
import threading
# Our thread class
class MyThread (threading. Thread):
def __init__(self,x ):
self.__x = x
threading. Thread.__init__(self)
def run (self):
print str(self.__x )
# Start 10 threads.
for x
in xrange(10):
MyThread (x ). start() |
Output:
0 1 ... 9 |
Threads do not have to stop if run once. Threads could be timed, where a threads functionality is repeated ever
y x seconds.In Python, the Timer class is a subclass of the Thread class. This means it behaves similar. We can use the timer class to create timed threads. Timers are started with the .start() method call, just like regular threads. The program below creates a thread that starts after 5 seconds.
#!/usr/bin/env python from threading import * def hello (): print "hello, world" # create thread t = Timer (10.0, hello ) # start thread after 10 seconds t. start() |
Repeating functionality using threads
We can execute threads endlessly like this:
#!/usr/bin/env python from threading import * import time def handleClient1 (): while(True): print "Waiting for client 1..." time. sleep(5) # wait 5 seconds def handleClient2 (): while(True): print "Waiting for client 2..." time. sleep(5) # wait 5 seconds # create threads t = Timer (5.0, handleClient1 ) t2 = Timer (3.0, handleClient2 ) # start threads t. start() t2. start() |
We can create anonymous functions, known as lambda functions. Lambda functions are different from normal Python functions, they origin from Lambda Calculus. It allows you to write very short functions.
Lambda function example
This code shows the use of a lambda function:
#!/usr/bin/env python f = lambda x : 2 * x print f (3) |
A return statements is never used in a lambda function, it always returns
something. A lambda functions may contain if statements:
#!/usr/bin/env python f = lambda x: x > 10 print(f (2)) print(f (12)) |
The definition of map is map(function,iterable). It applies a function to every item in the iteratable. We can use map() to on a lambda function with a list:
#!/usr/bin/env python list = [1,2,3,4,5] squaredList = map( lambda x: x*x, list) print(squaredList ) |
Anywhere you use lambda functions, you could use normal functions instead. A lambda function is not a
statement, it is an expression. Lambda functions do not support a block of statements.filter(function,iterable) creates a new list from the elmeents for which the function returns True. Example:
#!/usr/bin/env python list = [1,2,3,4,5,6,7,8,9,10] newList = filter( lambda x: x % 2 == 0, list) print(newList ) |
The returning list returns contains only the elements for which the lambda expression “lamba x: x % 2
== 0” is true.The reduce function, reduce(function, iterable) applies two arguments cumulatively to the items of iterable, from left to right. Example:
#!/usr/bin/env python list = [1,2,3,4,5] s = reduce( lambda x,y: x+y, list) print() |
In this case the expression is always true, thus it simply sums up the elements of the list. Another example:
#!/usr/bin/env python list = [10,6,7,5,2,1,8,5] s = reduce( lambda x,y: x if (x > y ) else y, list) print() |
A set in Python is a collection of objects. Sets are available in Python 2.4 and newer versions. They are different from lists or tuples in that they are modeled after sets in mathematics.
Set example
To create a set, we use the set() function.
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) print(x ) |
If we add the same item element multiple times, they are removed. A set may not contain the same element multiple times.
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram", "Postcard"]) print(x ) |
If you use Python version 2.6 or a later version, you can use a simplified notation:
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) print(x ) y = {"Postcard","Radio","Telegram"} print(y ) |
Clear elements from set
To remove all elements from sets:
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) x. clear() print(x ) |
Add elements to a set
To add elements to a set:
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) x. add("Telephone") print(x ) |
Remove elements to a set
To remove elements to a set:
!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) x. remove("Radio") print(x ) |
Difference between two sets
To find the difference between two sets use:
#!/usr/bin/env python x = set(["Postcard", "Radio", "Telegram"]) y = set(["Radio","Television"]) print( x. difference(y ) ) print( y. difference(x ) ) |
Be aware that x.difference(y) is different from y.difference(x).
Subset
To test if a set is a subset use:
#!/usr/bin/env python x = set(["a","b","c","d"]) y = set(["c","d"]) print( x. issubset(y ) )<b> </b> |
Super-set
To test if a set is a super-set:
#!/usr/bin/env python x = set(["a","b","c","d"]) y = set(["c","d"]) print( x. issuperset(y ) ) |
Intersection
To test for intersection, use:
#!/usr/bin/env python x = set(["a","b","c","d"]) y = set(["c","d"]) print( x. intersection(y ) ) |
As you are programming, the software can quickly scale into a large code base. To manage complexity we can use classes, functions and modules.
To show the accessible functions (and included variables) in a module, you can use this code:
#!/usr/bin/env python import sys print(dir(sys)) |
Result:
['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', '_multiarch', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style' , 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags' , 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions'] |
You can create your own module in these steps:
Create a file called test.py (your module)
#!/usr/bin/env python def add (a,b ): return a+b |
Then create a file called app.py:
from test import * print('hello') print(add (5,2)) |
A graph in mathematics and computer science consists of “nodes” which may or may not be connected with one another. Connections between nodes are called edges. A graph can be directed (arrows) or undirected. The edges could represent distance or weight.
Python does not have a graph data type. To use graphs we can either use a module or implement it ourselves:
A directed graph can be defined as:
#!/usr/bin/env python graph = {'A': ['B', 'C'], 'B': ['C', 'A'], 'C': ['D'], 'D': ['A']} print(graph ) |
The networkx software module has support for creating, manipulating graphs.
#!/usr/bin/env python import networkx as nx G=nx. Graph() G. add_node("A") G. add_node("B") G. add_node("C") G. add_edge("A","B") G. add_edge("B","C") G. add_edge("C","A") print("Nodes: " + str(G. nodes())) print("Edges: " + str(G. edges())) |
Result:
Nodes: ['A', 'C', 'B'] Edges: [('A', 'C'), ('A', 'B'), ('C', 'B')]
A finite state machine (FSM) is a mathematical model of computation with states, transitions, inputs and outputs. This machine is always in a one state at the time and can move to other states using transitions. A transition changes the state of the machine to another state.
A large number of problems can be modeled using finite state machines. Simple examples of state machines used in modern life are vending machines, elevators and traffic lights. Advanced usage are artificial intelligence, language parsing and communication protocol design.
First install the Fysom module:
sudo pip install fysom
We can define a Finite State Machine (FSM) with two states: sleeping and awake. To move between the states we will define the transitions wakeup() and sleep().
Example:
from fysom import * fsm = Fysom ({'initial': 'awake', 'final': 'red', 'events': [ {'name': 'wakeup', 'src': 'sleeping', 'dst': 'awake'}, {'name': 'sleep', 'src': 'awake', 'dst': 'sleeping'}]}) print(fsm. current) # awake fsm. sleep() print(fsm. current) # sleeping fsm. wakeup() print(fsm. current) # awake |
Result:
awake sleeping awake |
There are several implementations of Finite State Machines in Python:
In computer science, a tree is a data structure that is modeled after nature. Unlike trees in nature, the tree data structure is upside down: the root of the tree is on top. A tree consists of nodes and its connections are called edges. The bottom nodes are also named leaf nodes. A tree may not have a cycle.
Python does not have built-in support for trees.
A binary tree is a data structure where every node has at most two children (left and right child). The root of a tree is on top. Every node below has a node above known as the parent node.We define a class thee which has a left and right attribute. From this binary tree we define the root (top of the three) and a left and right node.
#!/usr/bin/env python
class Tree (object):
def __init__(self):
self. left = None
self. right = None
self. data = None
root = Tree ()
root. data = "root"
root. left = Tree ()
root. left. data = "left"
root. right = Tree ()
root. right. data = "right"
print(root. left. data) |
You could then further create the tree like this:
#!/usr/bin/env python
class Tree (object):
def __init__(self):
self. left = None
self. right = None
self. data = None
root = Tree ()
root. data = "root"
root. left = Tree ()
root. left. data = "left"
root. right = Tree ()
root. right. data = "right"
root. left. left = Tree ()
root. left. left. data = "left 2"
root. left. right = Tree ()
root. left. right. data = "left-right" |
More detailed: While this is not directly useful in web applications or most desktop applications, it is very useful to know.
In this article you will learn how to use binary numbers in Python, how to convert them to decimals and how to do bitwise operations on them.
At the lowest level, the computer has no notion whatsoever of numbers except ‘there is a signal’ or ‘these is not a signal’. You can think of this as a light switch: Either the switch is on or it is off.
This tiny amount of information, the smallest amount of information that you can store in a computer, is known as a bit. We represent a bit as either low (0) or high (1).
To represent higher numbers than 1, the idea was born to use a sequence of bits. A sequence of eight bits could store much larger numbers, this is called a byte. A sequence consisting of ones and zeroes is known as binary. Our traditional counting system with ten digits is known as decimal.
Lets see that in practice:
# Prints out a few binary numbers. print int('00', 2) print int('01', 2) print int('10', 2) print int('11', 2) |
The second parameter 2, tells Python we have a number based on 2 elements (1 and 0). To convert a byte (8 bits) to decimal, simple write a combination of eight bits in the first parameter.
# Prints out a few binary numbers. print int('00000010', 2) # outputs 2 print int('00000011', 2) # outputs 3 print int('00010001', 2) # outputs 17 print int('11111111', 2) # outputs 255 |
How does the computer do this? Every digit (from right to left) is multiplied by the power of two.
The number ‘00010001‘ is (1 x 2^0) + (0 x 2^1) + (0 x 2^2) + (0 x 2^3) + (1 x 2^4) + (0 x 2^5) + (0 x 2^6) + (0 x 2^7) = 16 + 1 = 17. Remember, read from right to left.
The number ‘00110010’ would be (0 x 2^0) + (1 x 2^1) + (0 x 2^2) + (0 x 2^3) + (1 x 2^4) + (1 x 2^5) + (0 x 2^6) + (0 x 2^7) = 32+16+2 = 50.
Try the sequence ‘00101010’ yourself to see if you understand and verify with a Python
program.Binary Left Shift and Binary Right Shift
Multiplication by a factor two and division by a factor of two is very easy in binary. We simply shift the bits left or right. We shift left below:
Bit 4 | Bit 3 | Bit 2 | Bit 1 |
---|---|---|---|
0 | 1 | 0 | 1 |
1 | 0 | 1 | 0 |
Before shifting (0,1,0,1) we have the number 5 . After shifting (1,0,1,0) we have the number 10. In python you can use the bitwise left operator (<<) to shift left and the bitwise right operator (>>) to shift right.
inputA = int('0101',2) print "Before shifting " + str(inputA ) + " " + bin (inputA ) print "After shifting in binary: " + bin (inputA << 1) print "After shifting in decimal: " + str(inputA << 1) |
Output:
Before shifting 5 0b101 After shifting in binary: 0b1010 After shifting in decimal: 10 |
Given two inputs, the computer can do several logic operations with those bits. Let’s take the AND operator. If input A and input B are positive, the output will be positive. We will demonstrate the AND operator graphically, the two left ones are input A and input B, the right circle is the output:
In code this is as simple as using the & symbol, which represents the Logical AND operator.
# This code will execute a bitwise logical AND. Both inputA and inputB are bits. inputA = 1 inputB = 1 print inputA & inputB # Bitwise AND |
By changing the inputs you will have the same results as the image above. We can do the AND operator on a sequence:
inputA = int('00100011',2) # define binary sequence inputA inputB = int('00101101',2) # define binary sequence inputB print bin (inputA & inputB ) # logical AND on inputA and inputB and output in binary |
Output:
0b100001 # equals 00100001 |
This makes sense because if you do the operation by hand:
00100011 00101101 -------- Logical bitwise AND 00100001 |
Now that you have learned the AND operator, let’s have a look at the OR operator. Given two inputs, the output will be zero only if A and B are both zero.
To execute it, we use the | operator. A sequence of bits can simply be executed like this:
inputA = int('00100011',2) # define binary number inputB = int('00101101',2) # define binary number print bin (inputA ) # prints inputA in binary print bin (inputB ) # prints inputB in binary print bin (inputA | inputB ) # Execute bitwise logical OR and print result in binary |
Output:
0b100011 0b101101 0b101111 |
This is an interesting operator: The Exclusive OR or shortly XOR.
To execute it, we use the ^ operator. A sequence of bits can simply be executed like this:
inputA = int('00100011',2) # define binary number inputB = int('00101101',2) # define binary number print bin (inputA ) # prints inputA in binary print bin (inputB ) # prints inputB in binary print bin (inputA ^ inputB ) # Execute bitwise logical OR and print result in binary |
Output:
0b100011 0b101101 0b1110 |
We can use debugging tools to minimize and find bugs. In this article you will learn the best
Python debugging tricks.A graphical interface is shown using the PuDB terminal.
Installation
to install with Python 3:
sudo pip3 install pudb |
for Python 2.x use:
sudo pip install pudb |
Debugging
Start debugging with:
$ pudb3 program. py |
(or sudo if you don’t have the right permissions)
You can take step by step through the program. Use the n key to take a step through the program. The current variable contents are shown on the right top.
You can set breakpoints using the b key. To continue execution until the next breakpoints, press the c key.
The module pdb supports setting breakpoints. A breakpoint is an intentional pause of the program. where you can get more information about the programs state.
To set a breakpoint, insert the line
pdb. set_trace() |
Example
A practical example:
import pdb x = 3 y = 4 pdb. set_trace() total = x + y pdb. set_trace() |
We have inserted a few breakpoints in this program. The program will pause at each breakpoint (pdb.set_trace()). To view a variables contents simply type the variable name:
$ python3 program. py (Pdb ) x 3 (Pdb ) y 4 (Pdb ) total *** NameError: name 'total' is not defined (Pdb ) |
Press c or continue to go on with the programs execution until the next breakpoint
(Pdb ) c --Return-- > program. py(7)<module>()->None -> total = x + y (Pdb ) total 7 |
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.
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.
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 🙂
In this tutorial you will learn how to use the SQLite database management system with Python. You will learn how to use SQLite, SQL queries, RDBMS and more of this cool stuff!
Data is retrieved from a database system using the SQL language.
Python has bindings for many database systems including MySQL, Postregsql, Oracle, Microsoft SQL Server and Maria DB.
One of these database management systems (DBMS) is called SQLite. SQLite was created in the year 2000 and is one of the many management systems in the database zoo.
SQL is a special-purpose programming language designed for managing data held in a databases. The language has been around since 1986 and is worth learning. The is an old funny video about SQL
It is a self-contained, serverless, zero-configuration, transactional SQL database engine. The SQLite project is
sponsored by Bloomberg and Mozilla.Use this command to install SQLite:
$ sudo apt-get install sqlite |
Verify if it is correctly installed. Copy this program and save it as test1.py
#!/usr/bin/python # -*- coding: utf-8 -*- import sqlite3 as lite import sys con = None try: con = lite. connect('test.db') cur = con. cursor() cur. execute('SELECT SQLITE_VERSION()') data = cur. fetchone() print "SQLite version: %s" % data except lite. Error, e: print "Error %s:" % e. args[0] sys. exit(1) finally: if con: con. close() |
Execute with:
$ python test1. py |
It should output:
SQLite version: 3.8.2 |
What did the script above do?
The script connected to a new database called test.db with this line:
con = lite. connect('test.db') |
It then queries the database management system with the command
SELECT SQLITE_VERSION () |
which in turn returned its version number. That line is known as an SQL query.
The script below will store data into a new database called user.db
#!/usr/bin/python # -*- coding: utf-8 -*- import sqlite3 as lite import sys con = lite. connect('user.db') with con: cur = con. cursor() cur. execute("CREATE TABLE Users(Id INT, Name TEXT)") cur. execute("INSERT INTO Users VALUES(1,'Michelle')") cur. execute("INSERT INTO Users VALUES(2,'Sonya')") cur. execute("INSERT INTO Users VALUES(3,'Greg')") |
SQLite is a database management system that uses tables. These tables can have relations with other tables: it’s called relational database management system or RDBMS. The table defines the structure of the data and can hold the data. A database can hold many different tables. The table gets created using the command:
cur. execute("CREATE TABLE Users(Id INT, Name TEXT)") |
We add records into the table with these commands:
cur. execute("INSERT INTO Users VALUES(2,'Sonya')") cur. execute("INSERT INTO Users VALUES(3,'Greg')") |
The first value is the ID. The second value is the name. Once we run the script the data gets inserted into the database table Users:
We can explore the database using two methods: the command line and a graphical interface.
From console: To explore using the command line type these commands:
sqlite3 user. db . tables SELECT * FROM Users; |
This will output the data in the table Users.
sqlite> SELECT * FROM Users; 1|Michelle 2|Sonya 3|Greg |
From GUI: If you want to use a GUI instead, there is a lot of choice. Personally I picked sqllite-man but there are many others. We install using:
sudo apt-get install sqliteman |
We start the application sqliteman. A gui pops up.
Press File > Open > user.db. It appears like not much has changed, do not worry, this is just the user interface. On the left is a small tree view, press Tables > users. The full table including all records will be showing now.
This GUI can be used to modify the records (data) in the table and to add new tables.
SQL has many commands to interact with the database. You can try the commands below from the command line or from the GUI:
sqlite3 user. db SELECT * FROM Users; SELECT count (* ) FROM Users; SELECT name FROM Users; SELECT * FROM Users WHERE id = 2; DELETE FROM Users WHERE id = 6; |
We can use those queries in a Python program:
#!/usr/bin/python # -*- coding: utf-8 -*- import sqlite3 as lite import sys con = lite. connect('user.db') with con: cur = con. cursor() cur. execute("SELECT * FROM Users") rows = cur. fetchall() for row in rows: print row |
This will output all data in the Users table from the database:
$ python get. py (1, u'Michelle') (2, u'Sonya') (3, u'Greg') |
Creating a user information database
We can structure our data across multiple tables. This keeps our data structured, fast and organized. If we would have a single table to store everything, we would quickly have a big chaotic mess. What we will do is create multiple tables and use them in a combination. We create two tables:
Users:
Jobs:
To create these tables, you can do that by hand in the GUI or use the script below:
# -*- coding: utf-8 -*- import sqlite3 as lite import sys con = lite. connect('system.db') with con: cur = con. cursor() cur. execute("CREATE TABLE Users(Id INT, Name TEXT)") cur. execute("INSERT INTO Users VALUES(1,'Michelle')") cur. execute("INSERT INTO Users VALUES(2,'Howard')") cur. execute("INSERT INTO Users VALUES(3,'Greg')") cur. execute("CREATE TABLE Jobs(Id INT, Uid INT, Profession TEXT)") cur. execute("INSERT INTO Jobs VALUES(1,1,'Scientist')") cur. execute("INSERT INTO Jobs VALUES(2,2,'Marketeer')") cur. execute("INSERT INTO Jobs VALUES(3,3,'Developer')") |
The jobs table has an extra parameter, Uid. We use that to connect the two tables in an SQL query:
SELECT users. name, jobs. profession FROM jobs INNER JOIN users ON users. ID = jobs. uid |
You can incorporate that SQL query in a Python script:
#!/usr/bin/python # -*- coding: utf-8 -*- import sqlite3 as lite import sys con = lite. connect('system.db') with con: cur = con. cursor() cur. execute("SELECT users.name, jobs.profession FROM jobs INNER JOIN users ON users.ID = jobs.uid") rows = cur. fetchall() for row in rows: print row |
It should output:
$ python get2. py (u'Michelle', u'Scientist') (u'Howard', u'Marketeer') (u'Greg', u'Developer') |
You may like: Databases and data analysis
In this tutorial you will learn how to use a widely used database management system called MySQL in Python. You do not need any previous knowledge of MySQL to use this tutorial, but there is a lot more to MySQL than covered in this short introductory tutorial.
MySQL tutorial
Data is stored in a collection of tables with each table consisting of a set of rows and columns. This is similar to how data is stored in SQLite. To interact with the data stored in tables we use a special-purpose programming language called SQL.
Step 1: Install MySQL
First you must install a MySQL driver, use the specific installation method below.
On Windows:
Install MySQLdb using the installer.
On Linux:
Install MySQLdb using:
sudo apt-get install python-mysqldb yum install mysql-python |
depending on your version.
On Mac:
Follow the installation instructions from stackoverflow
MySQL server has to be running before going to the next step.
Step 2: Setup the database
Make sure you have database access, from the command line type:
mysql -u USERNAME -p |
MySQL will then ask your password. Type these commands:
mysql> CREATE DATABASE pythonspot; mysql> USE pythonspot; |
We go on the create the table:
CREATE TABLE IF NOT EXISTS examples ( id int(11) NOT NULL AUTO_INCREMENT, description varchar (45), PRIMARY KEY (id) ); |
Then we can insert data into the table (these are SQL queries):
INSERT INTO examples (description ) VALUES ("Hello World"); INSERT INTO examples (description ) VALUES ("MySQL Example"); INSERT INTO examples (description ) VALUES ("Flask Example"); |
You can now grab all records from the table using a SQL query:
mysql> SELECT * FROM examples; +----+---------------+ | id | description | +----+---------------+ | 1 | Hello World | | 2 | MySQL Example | | 3 | Flask Example | +----+---------------+ 3 rows in set (0.01 sec ) |
Step 3: Getting the data from Python
You can access the database directly from Python using the MySQLdb module.
#!/usr/bin/python import MySQLdb db = MySQLdb. connect(host="localhost", # your host user="root", # username passwd="root", # password db="pythonspot") # name of the database # Create a Cursor object to execute queries. cur = db. cursor() # Select data from table using SQL query. cur. execute("SELECT * FROM examples") # print the first and second columns for row in cur. fetchall() : print row [0], " ", row [1] |
Output:
1 Hello World 2 MySQL Example 3 Flask Example |
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:
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:
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:
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) |
Web apps are often created using a framework. Frameworks make it easier to develop web apps that are scalable, reliable and maintainable. It avoids recreating the same code over and over again.
Common features are:
A framework may offer some or all of these features.
For example, the Flask web application framework does not have database support and you would need a
separate module to use a database. The Django web application framework supports databases by default.As you are doing web development, you want to avoid spending time on programming things that have already been solved. On the other hand, if you are an experienced web developer a web framework may not offer
everything you need.Django and Flask are the most popular web frameworks. However, you may want to evaluate the frameworks. An overview:
The most popular python web application framework is Django, followed by Flask.
Django is the most used Python web framework. It takes care of many things so you can focus on the web app development. Sites built withDjango have dealt with high traffic spikes such as 50 thousands hits per second.
Database access is achieved through an Object-relational mapper: You define your data models in Python and Django deals with the actual database management systems (SQL). However, if you need to you can write your own SQL Queries with Django. URL routing is supported by Django. It encourages beautiful URL design such as ending without .php or .asp.
Features:
If you want to know more about Django, read here.
Flask is a Python micro framework which is modular by design. The framework is intended to build web apps. Flask does not have a specific database system or ORM system. If you want to use a database, you’ll have to use extensions. Flask is often combined with SQLAlchemy for database use.
Flask is very easy to get running, a minimal app would be:
from flask import Flask app = Flask (__name__ ) @app. route('/') def hello_world (): return 'Hello World!' if __name__ == '__main__': app. run() |
The framework supports URL routing, template (using Jinja2), session management and has some out of the box security.
Features:
If you want to know more about Flask, read here.
To run your app on the web, you will need hosting. Unless you want to do hosting yourself, you need a party to host.
Hosting servers:
In this tutorial you’ll learn how to build a web app with Python.
We’ll use a micro-framework called Flask. It has a small core but is extensible with many plugins such as SQLAlchemy, Babel, CouchDB, MongoDB etc.
Some Flask example apps are:
Why Flask?
Install Flask using the command below:
pip install Flask |
Create a file called hello.py
from flask import Flask app = Flask (__name__ ) @app. route("/") def hello (): return "Hello World!" if __name__ == "__main__": app. run() |
Finally run the web app using this command:
$ python hello. py * Running on http://localhost:5000/ |
Open http://localhost:5000/ in your webbrowser, and “Hello World!” should appear.
Jinja2 is a template engine for Python. You can use it when rendering data to web pages. For every link you visit, you want to show the data with the formatting. By using a template engine we can seperate display logic (html, css) from the actual Python code. Let’s start with an example
Getting started with Jinja2
Create the directories:
And create the file user.html in /app/templates:
<title>{% block title % }{% endblock % }</title> <ul> {% for user in users % } <li>{{ user }}</a></li> {% endfor % } </ul> |
Then create the code app.py in /app/app.py:
from flask import Flask, flash, redirect, render_template, request from random import randint app = Flask (__name__ ) @app. route("/") def index (): return "Flask App!" @app. route("/user/") def hello (): users = [ "Frank", "Steve", "Alice", "Bruce" ] return render_template ( 'user.html', **locals()) if __name__ == "__main__": app. run(host='0.0.0.0', port=8080) |
Finally execute with:
python app. py * Running on http://0.0.0.0:8080/ |
You can then open http://127.0.0.1:8080/user/ in your browser. This will output the data formatted according to html:
About Jinja
A Jinja2 template is simply a text file that does not need to have a specific extension such as .html, .xml.
A template may contain tags and special delimiters:
Delimiters | Usage |
---|---|
{% … %} | Statements |
{{ … }} | Expressions to print to the template output |
{# … #} | Comments not included in the template output |
# … ## | Line Statements |
In the example above we have two statements and one expression. We have not included any comments.
Base template and child templates
A Jinja2 template can extend a base template. On a webpage with many sites you may want these pages look similar. In /templates/ create a file called base.html with this code:
<!DOCTYPE html> <html lang="en"> <head> {% block head % } <link rel="stylesheet" href="style.css" /> <title>{% block title % }{% endblock % } - My Webpage</title> {% endblock % } </head> <body> <div id="content">{% block content % }{% endblock % }</div> <div id="footer"> {% block footer % } Copyright 2015 by <a href="https://pythonspot.com/">pythonspot</a>. {% endblock % } </div> </body> </html> |
We did not set a style.css, but you could set one. Change /templates/user.html to:
{% extends "base.html" % } {% block title % }Index {% endblock % } {% block head % } {{ super() }} <style type="text/css"> . important { color: #336699; } </style> {% endblock % } {% block content % } <h1>Users</h1> <p class="important"> <ul> {% for user in users % } <li>{{ user }}</a></li> {% endfor % } </ul> </p> {% endblock % } |
Restart the app with:
python app. py * Running on http://0.0.0.0:8080/ |
Output:
Install Flask using the command below:
$ pip install Flask |
Create a file called hello.py
from flask import Flask app = Flask (__name__ ) @app. route("/") def index (): return "Hello World!" if __name__ == "__main__": app. run(host='0.0.0.0', port=4000) |
Finally run the web app using this command:
$ python hello. py |
Open http://localhost:4000/ in your webbrowser, and “Hello World!” should appear.
Create this Python file and save it as app.py:
from flask import Flask from flask import Flask, flash, redirect, render_template, request, session, abort import os app = Flask (__name__ ) @app. route('/') def home (): if not session. get('logged_in'): return render_template ('login.html') else: return "Hello Boss!" @app. route('/login', methods=['POST']) def do_admin_login (): if request. form['password'] == 'password' and request. form['username'] == 'admin': session ['logged_in'] = True else: flash ('wrong password!') return home () if __name__ == "__main__": app. secret_key = os. urandom(12) app. run(debug=True,host='0.0.0.0', port=4000) |
There are two routes (paths you can see in your browser URL bar) created here:
@app. route('/') @app. route('/login', methods=['POST']) |
The first one displays the login screen or the home screen, based on the condition if you are logged in. The second route validates the login variables on login.
We create the directory /templates/. Create the file /templates/login.html with this code:
{% block body % } {% if session ['logged_in'] % } <p>You're logged in already!</p> {% else %} <form action="/login" method="POST"> <input type="username" name="username" placeholder="Username"> <input type="password" name="password" placeholder="Password"> <input type="submit" value="Log in"> </form> {% endif %} {% endblock %} |
Run the web app using this command:
$ python hello. py |
Open http://localhost:4000/ in your webbrowser, and the login screen should appear. The login credentials are shown in the do_admin_login() function.
While functional, the login screen looks like an early 90s User Interface (UI). We picked a random login template from codepen.io. We create the directory /static/ with the file style.css.
* { box-sizing: border-box; } *:focus { outline: none; } body { font-family: Arial; background-color: #3498DB; padding: 50px; } . login { margin: 20px auto; width: 300px; } . login-screen { background-color: #FFF; padding: 20px; border-radius: 5px } . app-title { text-align: center; color: #777; } . login-form { text-align: center; } . control-group { margin-bottom: 10px; } input { text-align: center; background-color: #ECF0F1; border: 2px solid transparent; border-radius: 3px; font-size: 16px; font-weight: 200; padding: 10px 0; width: 250px; transition: border .5s; } input:focus { border: 2px solid #3498DB; box-shadow: none; } . btn { border: 2px solid transparent; background: #3498DB; color: #ffffff; font-size: 16px; line-height: 25px; padding: 10px 0; text-decoration: none; text-shadow: none; border-radius: 3px; box-shadow: none; transition: 0.25s; display: block; width: 250px; margin: 0 auto; } . btn:hover { background-color: #2980B9; } . login-link { font-size: 12px; color: #444; display: block; margin-top: 12px; } |
Modify the login.html template as:
<link rel="stylesheet" href="/static/style.css" type="text/css"> {% block body % } {% if session ['logged_in'] % } <p>You're logged in already!</p> {% else %} <form action="/login" method="POST"> <div class="login"> <div class="login-screen"> <div class="app-title"> <h1>Login</h1> </div> <div class="login-form"> <div class="control-group"> <input type="text" class="login-field" value="" placeholder="username" name="username"> <label class="login-field-icon fui-user" for="login-name"></label> </div> <div class="control-group"> <input type="password" class="login-field" value="" placeholder="password" name="password"> <label class="login-field-icon fui-lock" for="login-pass"></label> </div> <input type="submit" value="Log in" class="btn btn-primary btn-large btn-block" > <br> </div> </div> </div> </form> {% endif %} {% endblock %} |
Once you restart the application this screen should appear:
Awesome, ain’t it? 🙂
As you may have seen, there is no logout button or functionality. Creating that is very easy. The solution proposed below is only one of the many solutions. We create a new route /logout which directs to the function logout(). This function clears the session variable and returns to the login screen.
@app. route("/logout") def logout (): session ['logged_in'] = False return home () |
The full code:
from flask import Flask from flask import Flask, flash, redirect, render_template, request, session, abort import os app = Flask (__name__ ) @app. route('/') def home (): if not session. get('logged_in'): return render_template ('login.html') else: return "Hello Boss! <a href='/logout'>Logout</a>" @app. route('/login', methods=['POST']) def do_admin_login (): if request. form['password'] == 'password' and request. form['username'] == 'admin': session ['logged_in'] = True else: flash ('wrong password!') return home () @app. route("/logout") def logout (): session ['logged_in'] = False return home () if __name__ == "__main__": app. secret_key = os. urandom(12) app. run(debug=True,host='0.0.0.0', port=4000) |
If you want a multi-user login system, you should add a database layer to the application. Flask does not have out of the box database support. You have to use a third party library if you want database support. In this tutorial we will use SqlAlchemy. If you do not have that installed type:
$ sudo pip install Flask-SqlAlchemy |
SQLAlchemy is an SQL toolkit and object-relational mapper (ORM) for the Python programming language. It has support for MySQL, Microsoft SQL Server and many more relational database management systems. If all of these terms are unfamiliar to you, just keep reading.
Create the file tabledef.py:
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:///tutorial.db', echo=True)
Base = declarative_base ()
########################################################################
class User (Base ):
""""""
__tablename__ = "users"
id = Column (Integer, primary_key=True)
username = Column (String )
password = Column (String )
#----------------------------------------------------------------------
def __init__(self, username, password ):
""""""
self. username = username
self. password = password
# create tables
Base. metadata. create_all(engine ) |
Execute it with:
python tabledef. py |
This file will create the database structure. Inside the directory you will find a file called tutorial.db. Create a file called dummy.py which will contain this code:
import datetime from sqlalchemy import create_engine from sqlalchemy. orm import sessionmaker from tabledef import * engine = create_engine ('sqlite:///tutorial.db', echo=True) # create a Session Session = sessionmaker (bind=engine ) session = Session () user = User ("admin","password") session. add(user) user = User ("python","python") session. add(user) user = User ("jumpiness","python") session. add(user) # commit the record the database session. commit() session. commit() |
Execute:
$ python dummy. py |
This will put dummy data into your database. Finally we update our app.py
Validating the login credentials with SqlAlchemy
The next step is to write the functionality that validates the user and password exist in the database. Using SqlAlchemy we can do this (dummy/pseudo code):
@app. route('/test') def test(): POST_USERNAME = "python" POST_PASSWORD = "python" Session = sessionmaker (bind=engine ) s = Session () query = s. query(User ).filter(User. username. in_([POST_USERNAME ]), User. password. in_([POST_PASSWORD ]) ) result = query. first() if result: return "Object found" else: return "Object not found " + POST_USERNAME + " " + POST_PASSWORD |
We use SqlAlchemys Oject Relational Mapping (ORM). We map the objects to relational database tables and vice versa. The definition (User) is given in tabledef.py. The s.query function is where the query is build. We have two conditions: the username and password must match. The query.first() returns true if the object exists, false if it does not. This gives us this total code:
from flask import Flask from flask import Flask, flash, redirect, render_template, request, session, abort import os from sqlalchemy. orm import sessionmaker from tabledef import * engine = create_engine ('sqlite:///tutorial.db', echo=True) app = Flask (__name__ ) @app. route('/') def home (): if not session. get('logged_in'): return render_template ('login.html') else: return "Hello Boss! <a href='/logout'>Logout</a>" @app. route('/login', methods=['POST']) def do_admin_login (): POST_USERNAME = str(request. form['username']) POST_PASSWORD = str(request. form['password']) Session = sessionmaker (bind=engine ) s = Session () query = s. query(User ).filter(User. username. in_([POST_USERNAME ]), User. password. in_([POST_PASSWORD ]) ) result = query. first() if result: session ['logged_in'] = True else: flash ('wrong password!') return home () @app. route("/logout") def logout (): session ['logged_in'] = False return home () if __name__ == "__main__": app. secret_key = os. urandom(12) app. run(debug=True,host='0.0.0.0', port=4000) |
You can now login with any user defined in the database table.
We have demonstrated a simple login app above. However, it is your job to properly secure it. There are many guys out there that are going to try to break into your app.
Best practices:
In this article you will learn how to use Google for login authentication in a Flask app. Instead of using a custom user login system, you could use Google to authenticate with your website. Your visitors may already have an account on Google, so why not use it to login?
To do so, we use a protocol called OAuth. From wikipedia:
OAuth is a protocol that utilizes tokens in order to access resources on behalf of a resource owner. Think giving a user the ability to give out a valet key to certain portions of your site. Many sites, such as Google, Facebook, and Twitter use OAuth for authenticating third party clients in order to access certain user resources.
Don’t worry if that sounds vague, we’ll take you through the steps.
Getting started
Visit https://code.google.com/apis/console/ and press on credentials. Click “Create new Client ID”.
You will see this screen popup:
Type your information and press “Create Client ID”. You then have a Client ID and Client secret which you will need.
Login authentication with Google
We use a module called flask_oauth to authenticate with Google. It is maintained by Armin Ronacher, the creator of Flask, so you can be sure the module does not die. The module uses OAuth, a protocol that gives tokens in order to access resources. Other modules may not have as good support.
Copy the code below and set the client id and client secret that you got from Google above. Just replace the lines:
GOOGLE_CLIENT_ID = 'PUT CLIENT ID' GOOGLE_CLIENT_SECRET = 'PUT CLIENT SECRET' |
Save the program as app.py
from flask import Flask, redirect, url_for, session from flask_oauth import OAuth # You must configure these 3 values from Google APIs console # https://code.google.com/apis/console GOOGLE_CLIENT_ID = 'PUT CLIENT ID' GOOGLE_CLIENT_SECRET = 'PUT CLIENT SECRET' REDIRECT_URI = '/oauth2callback' # one of the Redirect URIs from Google APIs console SECRET_KEY = 'development key' DEBUG = True app = Flask (__name__ ) app. debug = DEBUG app. secret_key = SECRET_KEY oauth = OAuth () google = oauth. remote_app('google', base_url='https://www.google.com/accounts/', authorize_url='https://accounts.google.com/o/oauth2/auth', request_token_url=None, request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email', 'response_type': 'code'}, access_token_url='https://accounts.google.com/o/oauth2/token', access_token_method='POST', access_token_params={'grant_type': 'authorization_code'}, consumer_key=GOOGLE_CLIENT_ID, consumer_secret=GOOGLE_CLIENT_SECRET ) @app. route('/') def index (): access_token = session. get('access_token') if access_token is None: return redirect (url_for ('login')) access_token = access_token [0] from urllib2 import Request, urlopen, URLError headers = {'Authorization': 'OAuth '+access_token } req = Request ('https://www.googleapis.com/oauth2/v1/userinfo', None, headers ) try: res = urlopen (req ) except URLError, e: if e.code == 401: # Unauthorized - bad token session. pop('access_token', None) return redirect (url_for ('login')) return res. read() return res. read() @app. route('/login') def login (): callback=url_for ('authorized', _external=True) return google. authorize(callback=callback ) @app. route(REDIRECT_URI ) @google. authorized_handler def authorized (resp ): access_token = resp ['access_token'] session ['access_token'] = access_token, '' return redirect (url_for ('index')) @google. tokengetter def get_access_token (): return session. get('access_token') def main (): app. run() if __name__ == '__main__': main () |
Execute using:
python app. py * Running on http://127.0.0.1:5000/ * Restarting with reloader |
You can then open the link to see the login screen. The app will simply return your account information encoded in JSON format once you accept.
Finally, you can validate if the access token is set on new routes.
Download Flask Examples
In this tutorial you will learn how to use Twitter for login authentication in a Flask app. Instead of using a custom user login system, you could useTwitter to authenticate with your website. Your visitors may already have an account on Twitter, so why not use it to login?
To do so, we use a protocol called OAuth. From wikipedia:
OAuth is a protocol that utilizes tokens in order to access resources on behalf of a resource owner. Think giving a user the ability to give out a valet key to certain portions of your site. Many sites, such as Google, Facebook, and Twitter use OAuth for authenticating third party clients in order to access certain user resources.
Don’t worry if that sounds vague, we’ll take you through the steps.
Get the twitter consumer_key and consumer_secret
Go to https://apps.twitter.com/ and create a new app.
Specify your app details:
You will then see this screen:
Press Keys and Access Tokens. Finally you will see your consumer_key and consumer_secret which you will need for your app.
The code
Create the file app.py and set your consumer_key and consumer_secret that you got from twitter.
from flask import Flask, request, redirect, url_for, session, g, flash, \ render_template from flask_oauth import OAuth from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy. orm import scoped_session, sessionmaker from sqlalchemy. ext. declarative import declarative_base # configuration SECRET_KEY = 'development key' DEBUG = True # setup flask app = Flask (__name__ ) app. debug = DEBUG app. secret_key = SECRET_KEY oauth = OAuth () # Use Twitter as example remote application twitter = oauth. remote_app('twitter', # unless absolute urls are used to make requests, this will be added # before all URLs. This is also true for request_token_url and others. base_url='https://api.twitter.com/1/', # where flask should look for new request tokens request_token_url='https://api.twitter.com/oauth/request_token', # where flask should exchange the token with the remote application access_token_url='https://api.twitter.com/oauth/access_token', # twitter knows two authorizatiom URLs. /authorize and /authenticate. # they mostly work the same, but for sign on /authenticate is # expected because this will give the user a slightly different # user interface on the twitter side. authorize_url='https://api.twitter.com/oauth/authenticate', # the consumer keys from the twitter application registry. consumer_key='ADD TWITTER CONSUMER KEY', consumer_secret='ADD TWITTER CONSUMER SECRET' ) @twitter. tokengetter def get_twitter_token (token=None): return session. get('twitter_token') @app. route('/') def index (): access_token = session. get('access_token') if access_token is None: return redirect (url_for ('login')) access_token = access_token [0] return render_template ('index.html') @app. route('/login') def login (): return twitter. authorize(callback=url_for ('oauth_authorized', next=request. args. get('next') or request. referrer or None)) @app. route('/logout') def logout (): session. pop('screen_name', None) flash ('You were signed out') return redirect (request. referrer or url_for ('index')) @app. route('/oauth-authorized') @twitter. authorized_handler def oauth_authorized (resp ): next_url = request. args. get('next') or url_for ('index') if resp is None: flash (u'You denied the request to sign in.') return redirect (next_url ) access_token = resp ['oauth_token'] session ['access_token'] = access_token session ['screen_name'] = resp ['screen_name'] session ['twitter_token'] = ( resp ['oauth_token'], resp ['oauth_token_secret'] ) return redirect (url_for ('index')) if __name__ == '__main__': app. run() |
Create the directory /templates/ with the file index.html
{% block body % } <h2>Flask Login App</h2> {% if session ['screen_name'] % } Hello {{ session ['screen_name'] }}! {% else % } <p> Sign in with twitter. <p> <a href="{{ url_for('login') }}"><img src="{{ url_for('static', filename='sign-in.png') }}" alt="sign in"></a> {% endif % } {% endblock % } |
Finally create the directory /static/ with the image sign-in.png:
Start your app with:
python app. py |
Open the app in your web-browser. Then your users can simply follow the steps to login:
This tutorial will teach you how to build a Flask app that combined with JSON and the Google Charts API. If you have no experience with Flask before I recommend reading my previous tutorials, they are great fun!
Install Flask using the command below:
$ pip install Flask |
Create a file called hello.py
from flask import Flask app = Flask (__name__ ) @app. route("/") def hello (): return "Hello World!" if __name__ == "__main__": app. run() |
Finally run the web app using this command:
$ python hello. py * Running on http://localhost:5000/ |
Open http://localhost:5000/ in your webbrowser, and “Hello World!” should appear.
To display awesome charts we first need some data. There are two common ways to get data in web apps: data from servers using an API (usually JSON) and data from databases. I use the Fixer.io JSON API to get some financial data, but any JSON API should do. If you are unfamiliar with JSON, see this article.
We wrote this script to get the exchange rates:
import json import urllib2 def getExchangeRates (): rates = [] response = urllib2. urlopen('http://api.fixer.io/latest') data = response. read() rdata = json. loads(data, parse_float=float) rates. append( rdata ['rates']['USD'] ) rates. append( rdata ['rates']['GBP'] ) rates. append( rdata ['rates']['JPY'] ) rates. append( rdata ['rates']['AUD'] ) return rates rates = getExchangeRates () print rates |
With the Google Charts API you can display live data on your site. There are a lot of great charts there that are easy to add to your Flask app. We simply give the data that we got from the server through JSON and parsed, to the Google Charts API.
Create a flask app with the directory /templates/ for your templates. This is the main Flask code:
from flask import Flask, flash, redirect, render_template, request, session, abort import os import json import urllib2 tmpl_dir = os. path. join(os. path. dirname(os. path. abspath(__file__ )), 'templates') app = Flask (__name__, template_folder=tmpl_dir ) def getExchangeRates (): rates = [] response = urllib2. urlopen('http://api.fixer.io/latest') data = response. read() rdata = json. loads(data, parse_float=float) rates. append( rdata ['rates']['USD'] ) rates. append( rdata ['rates']['GBP'] ) rates. append( rdata ['rates']['HKD'] ) rates. append( rdata ['rates']['AUD'] ) return rates @app. route("/") def index (): rates = getExchangeRates () return render_template ('test.html',**locals()) @app. route("/hello") def hello (): return "Hello World!" if __name__ == "__main__": app. run() |
And we have this template:
{% block body % } <script type="text/javascript" src="https://www.google.com/jsapi"></script> <div id="chart_div" style="width: 900px; height: 300px;"><div> <script type='text/javascript'>//<![CDATA [ google. load('visualization', '1', {packages: ['corechart', 'bar']}); google. setOnLoadCallback(drawBasic ); function drawBasic () { var data = google. visualization. arrayToDataTable([ ['Currency', 'Rate', { role: 'style' }], ['USD', {{rates [0]}}, 'gold'], ['GBP', {{rates [1]}}, 'silver'], ['HKD', {{rates [2]}}, 'brown'], ['AUD', {{rates [3]}}, 'blue'] ]); var options = { title: 'Exchange rate overview', chartArea: {width: '50%'}, hAxis: { title: '', minValue: 0 }, vAxis: { title: '' } }; var chart = new google. visualization. BarChart(document. getElementById('chart_div')); chart. draw(data, options ); } // ]]> </script> {% endblock % } |
Result:
We use WTForms, a module for validation of forms. We will start with a simple form containing one field asking for a name.
We create this in code:
from flask import Flask, render_template, flash, request from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField # App config. DEBUG = True app = Flask (__name__ ) app. config. from_object(__name__ ) app. config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a' class ReusableForm (Form ): name = TextField ('Name:', validators=[validators. required()]) @app. route("/", methods=['GET', 'POST']) def hello (): form = ReusableForm (request. form) print form. errors if request. method == 'POST': name=request. form['name'] print name if form. validate(): # Save the comment here. flash ('Hello ' + name ) else: flash ('All the form fields are required. ') return render_template ('hello.html', form=form ) if __name__ == "__main__": app. run() |
We then create the template hello.html in the /templates/ directory:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Reusable Form Demo</title> </head> <body> {% with messages = get_flashed_messages (with_categories=true ) % } {% if messages % } <ul> {% for message in messages % } <li>{{ message [1] }}</li> {% endfor % } </ul> {% endif % } {% endwith % } <form action="" method="post"> {{ form. csrf }} <div class="input text"> {{ form. name. label }} {{ form. name }} </div> <div class="input submit"> <input type="submit" value="Submit" /> </div> </form> </body> </html> |
Start the application and open it in your webbrowser at http://127.0.0.1:5000.
python miniapp. py * Running on http://127.0.0.1:5000/ * Restarting with reloader |
If you will submit an empty form, you will get an error. But if you enter your name, it will greet you. Form validation is handled by the wtforms module, but because we gave the argument
name = TextField ('Name:', validators=[validators. required()]) |
the field cannot be empty. Other parameters can be given here.
We use bootstrap to style the form.Bootstrap is a popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web. It makes front-end web development faster and easier. The output will be:
You can get the bootstrap files from http://getbootstrap.com/getting-started/#download and extract them in a new directory /static/. The code remains almost the same, but the template is changed. Code:
from flask import Flask, render_template, flash, request from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField # App config. DEBUG = True app = Flask (__name__ ) app. config. from_object(__name__ ) app. config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a' class ReusableForm (Form ): name = TextField ('Name:', validators=[validators. required()]) @app. route("/", methods=['GET', 'POST']) def hello (): form = ReusableForm (request. form) print form. errors if request. method == 'POST': name=request. form['name'] print name if form. validate(): # Save the comment here. flash ('Hello ' + name ) else: flash ('Error: All the form fields are required. ') return render_template ('hello.html', form=form ) if __name__ == "__main__": app. run() |
We added bootstrap to the template hello.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Reusable Form Demo</title> <link rel="stylesheet" media="screen" href ="static/bootstrap.min.css"> <link rel="stylesheet" href="static/bootstrap-theme.min.css"> <meta name="viewport" content = "width=device-width, initial-scale=1.0"> </head> <body> <div class="container"> <h2>Flask Web Form</h2> <form action="" method="post" role="form"> {{ form. csrf }} <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" name="name" placeholder="What's your name?"> </div> <button type="submit" class="btn btn-success">Submit</button> </form> <br> {% with messages = get_flashed_messages (with_categories=true ) % } {% if messages % } {% for message in messages % } {% if "Error" not in message [1]: % } <div class="alert alert-info"> <strong>Success! </strong> {{ message [1] }} </div> {% endif % } {% if "Error" in message [1]: % } <div class="alert alert-warning"> {{ message [1] }} </div> {% endif % } {% endfor % } {% endif % } {% endwith % } </div> <br> </div> </div> </body> </html> |
We use the same principle to create a registration form asking for name, email and password. We update the Form class:
class ReusableForm (Form ): name = TextField ('Name:', validators=[validators. required()]) email = TextField ('Email:', validators=[validators. required(), validators. Length(min=6, max=35)]) password = TextField ('Password:', validators=[validators. required(), validators. Length(min=3, max=35)]) def reset (self): blankData = MultiDict ([ ('csrf', self. reset_csrf() ) ]) self. process(blankData ) |
And we can get the variables passed using:
name=request. form['name'] password=request. form['password'] email=request. form['email'] |
Full code:
from flask import Flask, render_template, flash, request from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField # App config. DEBUG = True app = Flask (__name__ ) app. config. from_object(__name__ ) app. config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a' class ReusableForm (Form ): name = TextField ('Name:', validators=[validators. required()]) email = TextField ('Email:', validators=[validators. required(), validators. Length(min=6, max=35)]) password = TextField ('Password:', validators=[validators. required(), validators. Length(min=3, max=35)]) @app. route("/", methods=['GET', 'POST']) def hello (): form = ReusableForm (request. form) print form. errors if request. method == 'POST': name=request. form['name'] password=request. form['password'] email=request. form['email'] print name, " ", email, " ", password if form. validate(): # Save the comment here. flash ('Thanks for registration ' + name ) else: flash ('Error: All the form fields are required. ') return render_template ('hello.html', form=form ) if __name__ == "__main__": app. run() |
Update the template hello.html with this code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Reusable Form Demo</title> <link rel="stylesheet" media="screen" href ="static/bootstrap.min.css"> <link rel="stylesheet" href="static/bootstrap-theme.min.css"> <meta name="viewport" content = "width=device-width, initial-scale=1.0"> </head> <body> <div class="container"> <h2>Flask Web Form</h2> <form action="" method="post" role="form"> {{ form. csrf }} <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" name="name" placeholder="What's your name?"> <br> <label for="email">Email:</label> <input type="text" class="form-control" id="email" name="email" placeholder="Your email will be used to contact you."> <br> <label for="password">Password:</label> <input type="password" class="form-control" id="password" name="password" placeholder="Enter a password."> </div> <button type="submit" class="btn btn-success">Sign Up</button> </form> <br> {% with messages = get_flashed_messages (with_categories=true ) % } {% if messages % } {% for message in messages % } {% if "Error" not in message [1]: % } <div class="alert alert-info"> <strong>Success! </strong> {{ message [1] }} </div> {% endif % } {% if "Error" in message [1]: % } <div class="alert alert-warning"> {{ message [1] }} </div> {% endif % } {% endfor % } {% endif % } {% endwith % } </div> <br> </div> </div> </body> </html> |
Output:
WTForms can validate email, password, numbers and many more. For a list of validators see: http://wtforms.readthedocs.org/en/latest/validators.html
You can use the Flask framework and use static files together.
Flask will give you URL routing, many features and all the Python benefits.
You may want an application that is partly dynamic and partly static. Or you may simply want to browse with URL routing. In this article we will teach you how to load static HTML files with Flask.
Create a file called app.py with this contents:
from flask import Flask, render_template app = Flask (__name__ ) @app. route('/<string:page_name>/') def render_static (page_name ): return render_template ('%s.html' % page_name ) if __name__ == '__main__': app. run() |
This application initializes a Flask app with the method:
app. run() |
The app creates an URL route for any possible page and links that to static html files with:
@app. route('/<string:page_name>/') def render_static (page_name ): return render_template ('%s.html' % page_name ) |
Create a directory /templates/ and add the file hello.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World Template</title> </head> <body> Hello World </body> </html> |
Start the server with:
$ python app. py * Running on http://127.0.0.1:5000/ |
Then any .html file is accesible using URL routing.
For example the static file hello.html can be accessed using http://127.0.0.1:5000/hello. You can store any css file in the /static/ directory.
In this article you will learn about cookiecutter, a command-line utility that creates projects from project templates. With cookiecutter you can create a new python Flask project. This is similar to a standard Flask project except that using this method you start with several complete templates and a list of features.
Flask boilerplate features
Features
manage.py
script.Install the Flask dependencies:
sudo pip install flask_script sudo pip install flask_migrate sudo pip install flask_assets sudo pip install flask_bcrypt sudo pip install flask_cache sudo pip install flask_debugtoolbar |
Install and clone cookieclutter
sudo pip install cookiecutter sudo pip install mock==1.0.1 cookiecutter https://github. com/sloria/cookiecutter-flask. git |
You will be asked some information about your project, enter as you wish:
remote: Counting objects: 1545, done. remote: Total 1545 (delta 0), reused 0 (delta 0), pack-reused 1545 Receiving objects: 100% (1545/1545), 1.57 MiB | 720.00 KiB/s, done. Resolving deltas: 100% (857/857), done. Checking connectivity... done. full_name (default is "Steven Loria")? Steven Loria email (default is "[email protected]")? slorial@gmail. com github_username (default is "sloria")? sloria project_name (default is "My Flask App")? example app_name (default is "myflaskapp")? example project_short_description (default is "A flasky app.")? a flasky app |
Then enter your project directory (example) and start the server
cd example python manage. py runserver |
You can then open http://127.0.0.1:5000
We will start by creating a web application that displays a bar chart from a Python array. Create a directory /templates/ and add the file chart.html with this content:
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" /> <title>Chart. js </title> <!-- import plugin script --> <script src='static/Chart.min.js'></script> </head><body> <h1>Flask Chart. js</h1><!-- bar chart canvas element --><canvas id="chart" width="600" height="400"></canvas> <script> // bar chart data var barData = { labels : [{% for item in labels % } "{{item}}", {% endfor % }], datasets : [ { fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(151,187,205,1)", pointColor: "rgba(151,187,205,1)", data : [{% for item in values % } {{item }}, {% endfor % }] } ] } // get bar chart canvas var mychart = document. getElementById("chart"). getContext("2d"); steps = 10 max = 10 // draw bar chart new Chart (mychart ). Bar(barData, { scaleOverride: true, scaleSteps: steps, scaleStepWidth: Math. ceil(max / steps ), scaleStartValue: 0, scaleShowVerticalLines: true, scaleShowGridLines : true, barShowStroke : true, scaleShowLabels: true }); </script> </body></html> |
Create the directory /static/ and add the file Chart.min.js to it. You can get it either from the Chart.js website or use the link. Finally go into the home directory and create app.py with this contents:
from flask import Flask from flask import Markup from flask import Flask from flask import render_template app = Flask (__name__ ) @app. route("/") def chart (): labels = ["January","February","March","April","May","June","July","August"] values = [10,9,8,7,6,4,7,8] return render_template ('chart.html', values=values, labels=labels ) if __name__ == "__main__": app. run(host='0.0.0.0', port=5001) |
Finally run:
python app. py |
To create a line chart, we can simply modify the chart.html template. Change it to:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Chart. js </title> <!-- import plugin script --> <script src='static/Chart.min.js'></script> </head> <body> <h1>Flask Chart. js</h1> <!-- bar chart canvas element --> <canvas id="chart" width="600" height="400"></canvas> <script> // bar chart data var barData = { labels : [{% for item in labels % } "{{item}}", {% endfor % }], datasets : [ { fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(151,187,205,1)", pointColor: "rgba(151,187,205,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(151,187,205,1)", bezierCurve : false, data : [{% for item in values % } {{item }}, {% endfor % }] }] } Chart. defaults. global. animationSteps = 50; Chart. defaults. global. tooltipYPadding = 16; Chart. defaults. global. tooltipCornerRadius = 0; Chart. defaults. global. tooltipTitleFontStyle = "normal"; Chart. defaults. global. tooltipFillColor = "rgba(0,0,0,0.8)"; Chart. defaults. global. animationEasing = "easeOutBounce"; Chart. defaults. global. responsive = false; Chart. defaults. global. scaleLineColor = "black"; Chart. defaults. global. scaleFontSize = 16; // get bar chart canvas var mychart = document. getElementById("chart"). getContext("2d"); steps = 10 max = 10 // draw bar chart var LineChartDemo = new Chart (mychart ). Line(barData, { scaleOverride: true, scaleSteps: steps, scaleStepWidth: Math. ceil(max / steps ), scaleStartValue: 0, scaleShowVerticalLines: true, scaleShowGridLines : true, barShowStroke : true, scaleShowLabels: true, bezierCurve: false, }); </script> </body> </html> |
Output:
To create a pie chart, we must modify the application code slightly. We need 3 arrays: values, labels and colors. Colors are defined in hexadecimal, as usual in HTML. To iterate them in one loop, we zip them.
from flask import Flask from flask import Markup from flask import Flask from flask import render_template app = Flask (__name__ ) @app. route("/") def chart (): labels = ["January","February","March","April","May","June","July","August"] values = [10,9,8,7,6,4,7,8] colors = [ "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA","#ABCDEF", "#DDDDDD", "#ABCABC" ] return render_template ('chart.html', set=zip(values, labels, colors )) if __name__ == "__main__": app. run(host='0.0.0.0', port=5001) |
Secondly we modify the template to:
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" /> <title>Chart. js </title> <!-- import plugin script --> <script src='static/Chart.min.js'></script> </head><body> <h1>Flask Chart. js</h1><!-- bar chart canvas element --><canvas id="chart" width="600" height="400"></canvas> <script> var pieData = [ {% for item,label,colors in set % } { value: {{item }}, label: "{{label}}", color : "{{colors}}" }, {% endfor % } ]; // get bar chart canvas var mychart = document. getElementById("chart"). getContext("2d"); steps = 10 max = 10 // draw pie chart new Chart (document. getElementById("chart"). getContext("2d")). Pie(pieData ); </script> </body></html> |
Result:
Introduction
JSON (JavaScript Object Notation) is frequently used between a server and a web application. An example of JSON data:
{ "persons": [ { "city": "Seattle", "name": "Brian" }, { "city": "Amsterdam", "name": "David" } ] } |
The json module enables you to convert between JSON and Python Objects.
Convert JSON to Python Object (Dict)
To convert JSON to a Python dict use this:
import json json_data = '{"name": "Brian", "city": "Seattle"}' python_obj = json. loads(json_data ) print python_obj ["name"] print python_obj ["city"] |
Convert JSON to Python Object (List)
JSON data can be directly mapped to a Python list.
import json array = '{"drinks": ["coffee", "tea", "water"]}' data = json. loads(array) for element in data ['drinks']: print element |
Convert JSON to Python Object (float)
Floating points can be mapped using the decimal library.
import json from decimal import Decimal jsondata = '{"number": 1.573937639}' x = json. loads(jsondata, parse_float=Decimal ) print x ['number'] |
Convert JSON to Python Object (Example)
JSON data often holds multiple objects, an example of how to use that below:
import json json_input = '{"persons": [{"name": "Brian", "city": "Seattle"}, {"name": "David", "city": "Amsterdam"} ] }' try: decoded = json. loads(json_input ) # Access data for x in decoded ['persons']: print x ['name'] except (ValueError, KeyError, TypeError): print "JSON format error" |
Convert Python Object (Dict) to JSON
If you want to convert a Python Object to JSON use the json.dumps() method.
import json from decimal import Decimal d = {} d ["Name"] = "Luke" d ["Country"] = "Canada" print json. dumps(d, ensure_ascii=False) # result {"Country": "Canada", "Name": "Luke"} |
Converting JSON data to Python objects
JSON data can be converted (deserialized) to Pyhon objects using the json.loads() function. A table of the mapping:
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
If you want to display JSON data you can use the json.dumps() function.
import json json_data = '{"name": "Brian", "city": "Seattle"}' python_obj = json. loads(json_data ) print json. dumps(python_obj, sort_keys=True, indent=4) |
More
Google Charts with JSON data
In this tutorial we will build a webbrowser with Python. We will use the PyQT library which has a web component. In this tutorial you will learn how to link all the components together. We will use the default rendering engine and not roll one in this tutorial.
If you have not done our pyqt4 beginner tutorial, you could try it. If python-kde4 cannot be found update your repository to find it. The Ubuntu or Debian install guide .
Install the required packages:
sudo pip install python-qt4 sudo apt-get install qt4-designer sudo apt-get install pyqt4-dev-tools sudo apt-get install python-kde4 |
Start qt4-designer from your applications menu. The QT Designer application will appear:
Select Main Window and press Create. We now have our designer window open. Drag a KWebView component on the window. If you have a QtWebView in the component list. use that instead. We also add an Line Edit on top. Press File > Save As > browser.ui. Run the command:
pyuic4 browser. ui > browser. py |
This will generate a Python file. Remove the line “from kwebview import KWebView” from the bottom of the browser.py file. Change KWebView to QtWebView. We want to use QtWebView instead. If you are
lazy to change that, take the browser.py file from below.Create a file called run.py with this contents:
import sys
from browser
import BrowserDialog
from PyQt4
import QtCore, QtGui
from PyQt4. QtCore
import QUrl
from PyQt4. QtWebKit
import QWebView
class MyBrowser (QtGui. QDialog):
def __init__(self, parent=None):
QtGui. QWidget.__init__(self, parent )
QWebView.__init__(self)
self. ui = BrowserDialog ()
self. ui. setupUi(self)
self. ui. lineEdit. returnPressed. connect(self. loadURL)
def loadURL (self):
url = self. ui. lineEdit. text()
self. ui. qwebview. load(QUrl (url ))
self. show()
#self.ui.lineEdit.setText("")
if __name__ == "__main__":
app = QtGui. QApplication(sys. argv)
myapp = MyBrowser ()
myapp. ui. qwebview. load(QUrl ('http://www.pythonspot.com'))
myapp. show()
sys. exit(app. exec_()) |
This code will use the UI as defined in browser.py and add logic to it. The lines
self. ui. lineEdit. returnPressed. connect(self. loadURL) def loadURL (self): url = self. ui. lineEdit. text() self. ui. qwebview. load(QUrl (url )) self. show() #self.ui.lineEdit.setText("") |
The first line defines the callback or event. If a person presses enter (returnPressed), it will call the function loadURL. It makes sure that once you press enter, the page is loaded with that function. If you did everything correctly, you should be able to run the browser with the command:
python run. py |
Please make sure you type the full url, e.g. : http://pythonspot.com including the http:// part. Your browser should now start:
If your code does not run, please use the codes below (or look at the differences and change whats wrong):
browser.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'browser.ui' # # Created: Fri Jan 30 20:49:32 2015 # by: PyQt4 UI code generator 4.10.4 # # WARNING! All changes made in this file will be lost! import sys from PyQt4 import QtCore, QtGui from PyQt4. QtGui import QApplication from PyQt4. QtCore import QUrl from PyQt4. QtWebKit import QWebView try: _fromUtf8 = QtCore. QString. fromUtf8 except AttributeError: def _fromUtf8 (): return s try: _encoding = QtGui. QApplication. UnicodeUTF8 def _translate (context, text, disambig ): return QtGui. QApplication. translate(context, text, disambig, _encoding ) except AttributeError: def _translate (context, text, disambig ): return QtGui. QApplication. translate(context, text, disambig ) class BrowserDialog (object): def setupUi (self, Dialog ): Dialog. setObjectName(_fromUtf8 ("Dialog")) Dialog. resize(1024, 768) self. qwebview = QWebView (Dialog ) self. qwebview. setGeometry(QtCore. QRect(0, 50, 1020, 711)) self. qwebview. setObjectName(_fromUtf8 ("kwebview")) self. lineEdit = QtGui. QLineEdit(Dialog ) self. lineEdit. setGeometry(QtCore. QRect(10, 20, 1000, 25)) self. lineEdit. setObjectName(_fromUtf8 ("lineEdit")) self. retranslateUi(Dialog ) QtCore. QMetaObject. connectSlotsByName(Dialog ) def retranslateUi (self, Dialog ): Dialog. setWindowTitle(_translate ("Browser", "Browser", None)) |
run.py
import sys
from browser
import BrowserDialog
from PyQt4
import QtCore, QtGui
from PyQt4. QtCore
import QUrl
from PyQt4. QtWebKit
import QWebView
class MyBrowser (QtGui. QDialog):
def __init__(self, parent=None):
QtGui. QWidget.__init__(self, parent )
QWebView.__init__(self)
self. ui = BrowserDialog ()
self. ui. setupUi(self)
self. ui. lineEdit. returnPressed. connect(self. loadURL)
def loadURL (self):
url = self. ui. lineEdit. text()
self. ui. qwebview. load(QUrl (url ))
self. show()
#self.ui.lineEdit.setText("")
if __name__ == "__main__":
app = QtGui. QApplication(sys. argv)
myapp = MyBrowser ()
myapp. ui. qwebview. load(QUrl ('http://www.pythonspot.com'))
myapp. show()
sys. exit(app. exec_()) |
Download PyQT Code (Bulk Collection)
We have created a python program that generates a wordcloud based on your gmail account. The output may look something like this depending on the contents of your emails.
First you will need a small script that interacts with the gmail service. We have created a small script that interact with gmail. It relies on gmaillib installed and you will need to set: allow “less-secure” applications to access gmail server: https://www.google.com/settings/security/lesssecureapps
Gmail example:
#!/usr/bin/env python import gmaillib from collections import Counter def getMails (cnt,account, start,amount ): emails = account. inbox(start, amount ) for email in emails: cnt [email. sender_addr] += 1 amountOfMails = 100 cnt = Counter () username = raw_input("Gmail account: ") password = raw_input("Password: ") account = gmaillib. account(username, password ) getMails (cnt,account,0,amountOfMails ) print cnt |
If this script runs successfully you have almost all requirements installed. You will also need the library called wordcloud. We rebuild the system such that we get one long string containing the message bodies, which we feed as input to the wordcloud instance. The variable amount contains the number of mails to fetch. We have set it to 100 but you could set it to all messages using get_inbox_count() or you could simply fetch all emails of the last week.
Final program:
#!/usr/bin/env python import gmaillib from collections import Counter from wordcloud import WordCloud import matplotlib. pyplot as plt amount = 100 cnt = Counter () username = raw_input("Gmail account: ") password = raw_input("Password: ") account = gmaillib. account(username, password ) emails = account. inbox(0, amount ) data = "" for email in emails: data = data + str(email. body) wordcloud = WordCloud (). generate(data ) plt. imshow(wordcloud ) plt. axis("off") plt. show() |
Django is a high-level Python Web framework that takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.
In this tutorial you will learn how to setup a basic Django web app.
Install Django using:
pip install Django==1.7.1 |
Once installed, create the directory /django-hello/ for your app. In this directory create the file hello.py with this contents:
#!/usr/bin/env python import sys from django. conf import settings from django. conf. urls import patterns from django. http import HttpResponse from django. core. management import execute_from_command_line settings. configure( DEBUG=True, SECRET_KEY='asecretkey', ROOT_URLCONF=sys. modules[__name__ ], ) def index (request ): return HttpResponse ('Hello, World') urlpatterns = patterns ('', (r'^hello/$', index ), ) if __name__ == "__main__": execute_from_command_line (sys. argv) |
Execute the script using:
python hello. py runserver |
A HTTP Django server will start and if you open http://127.0.0.1:8000/hello/
Django code explanation:
The top lines import the Django library:
from django. conf import settings from django. conf. urls import patterns from django. http import HttpResponse from django. core. management import execute_from_command_line |
If you open the link /hello/, the webserver will call the index() function. We map the url to the function using:
urlpatterns = patterns ('', (r'^hello/$', index ), ) |
In Django we have url friendly urls. This means you do not have a url that ends in /id=1359835, but instead we have the directory as name. Finally, we set some default settings using settings.configure.
If you want to start with python web development, you could use a web framework named Django. It is designed to be fast, secure and scalable. It comes with an object-relational mapper (ORM), which means that objects in Python are mapped to objects in a database.
Applications created with Django are separated in three separate layers: model (database), view (appearance) and controller (logic), or shortly themodel-view-controller (MVC) architecture.
Start with:
django-admin startproject mysite
This will create the directory mysite. Open mysite/mysite/settings.py. You can configure your desired database here:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. path. join(BASE_DIR, 'db.sqlite3'), } } |
Pick from sqlite3, postgresql_psycopg2, mysql or oracle. Name is the name of your database. If you use SQLite the database will be created automatically. For MySQL and Postgresql you need to create the database yourself. Go up one directory to /mysite/ and run:
python manage. py runserver |
The terminal should say:
Performing system checks... System check identified no issues (0 silenced ). You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. August 16, 2015 - 14:45:29 Django version 1.7.1, using settings 'myapp.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [16/Aug/2015 14:45:35] "GET / HTTP/1.1" 200 1759 |
Open http://127.0.0.1:8000 in your web browser and you should see:
Now we created a project, we create an app. A project can have many apps.
python manage.py startapp notes
This creates the files:
notes/ __init__.py admin.py migrations/ __init__.py models.py tests.py views.py
Change /mysite/notes/models.py to:
from django. db import models class Note (models. Model): text = models. CharField(max_length=120) created = models. DateTimeField(auto_now_add=True) |
Open /mysite/mysite/settings.py, add the web app:
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'notes' ) |
Run
python manage.py syncdb
which will update the database. We then update /mysite/mysite/admin.py to:
from django. contrib import admin # Register your models here. from . models import Note class NoteAdmin (admin. ModelAdmin): class Meta: model = Note admin.site. register(Note,NoteAdmin ) |
Run:
python manage. py makemigrations notes python manage. py migrate |
Start your server using:
manage.py runserver
Once you open up the admin panel http://127.0.0.1:8000/admin, Notes will appear on the page:
If you click on notes you can view all notes and add/delete them:
We have all our data in the database, now we want to create our app. Open /mysite/settings.py and add:
#print "base dir path", BASE_DIR #print os.path.join(os.path.dirname(BASE_DIR), "mysite", "static", "templates") TEMPLATE_DIRS = ( os. path. join(os. path. dirname(BASE_DIR ), "mysite", "static", "templates"), #'/home/frankbox/python/djangoapp/mysite/static/templates', ) |
to the bottom of the file. This defines the directory of our templates (html).
Change /mysite/mysite/urls.py to:
from django. conf. urls import patterns, include, url from django. contrib import admin urlpatterns = patterns ('', # Examples: # url(r'^$', 'mysite.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url (r'^$', 'notes.views.home', name='home'), url (r'^admin/', include (admin.site. urls)), ) |
Finally create /mysite/static/templates/ and add note.html, which is a simple static html file.
<!DOCTYPE html> <html> <head> </head> <body> <b>Hello</b> </body> </html> |
Open http://127.0.0.1:8000/ to test if works. Change note.html to:
<!DOCTYPE html> <html> <head> </head> <body> <h2>Notes</h2> <ul> {% for note in notes.all % } <li>{{ note. text }}</li> {% endfor % } </ul> </body> </html> |
Then open /mysite/notes/views.py and change to:
from django. shortcuts import render, render_to_response, RequestContext from django. template import RequestContext, loader from django. http import HttpResponse from . models import Note # Create your views here. def home (request ): notes = Note. objects template = loader. get_template('note.html') context = {'notes': notes } return render (request, 'note.html', context ) #return render_to_response("note.html", notes) |
Once you fire up your browser you will see the list of notes:
While it’s nice to have a list, we want to add some notes to it.
Create the file /mysite/notes/forms.py
from django import forms from . models import Note class NoteForm (forms. ModelForm): class Meta: model = Note |
Change view.py to:
from django. shortcuts import render, render_to_response, RequestContext from django. template import RequestContext, loader from django. http import HttpResponse from . models import Note from . forms import NoteForm # Create your views here. def home (request ): notes = Note. objects template = loader. get_template('note.html') form = NoteForm (request. POST or None) if form. is_valid(): save_it = form. save(commit=False) save_it. save() context = {'notes': notes, 'form': form } return render (request, 'note.html', context ) #return render_to_response("note.html", notes) |
Finally we update note.html to:
<!DOCTYPE html> <html> <head> </head> <body> <h2>Notes</h2> <ul> {% for note in notes.all % } <li>{{ note. text }}</li> {% endfor % } </ul> <form method='POST' action=''>{% csrf_token % } {{ form. as_p }} <input type='submit'> </form> </body> </html> |
Run it and we have our note taking app 🙂
By modifying the note.html we can style it like any other html/css website. If you change note.html to:
<!DOCTYPE html> <html> <head> <link href="http://codepen.io/edbond88/pen/CcgvA.css" media="screen" rel="stylesheet" type="text/css" /> <style> body { background: rgba (222,222,222,1); margin: 20px; } </style> </head> <body> <h1>Django Note Taking App</h1> {% for note in notes.all % } <aside class="note-wrap note-white"> <li>{{ note. text }}</li> </aside> {% endfor % } <form method='POST' action=''>{% csrf_token % } {{ form. as_p }} <input type='submit' value='Add note'> </form> </body> </html> |
You will get:
To create a graphical interface (GUI) in Python you need to make use of a library or module. There are at least three widely used modules for creating GUIs with Python:
While you can create a graphical user interface (GUI) with any of these modules, they do not have the same features.
Tk provides basic widgets such as a button, menu, text and label. It is very limited compared to QT4 and WxPython but it is also the oldest module. It runs on most versions of Mac OS, Linux and Windows.
QT4 and QT5 are developed by the Qt company. Graphical applications made with QT4 or QT5 run on Windows, Linux/X11 and Mac OS X. It has tons of widgets including tabs, buttons, item views, table views, progressbars, input fields, calendar views and many more. Compared to Tk it has a lot more widgets available.
WxPython is a module that creates a native GUI look regardless of the operating system used. On Windows it will look as windows application while on Mac OS it will look as a Mac application. This can be a clear advantage compared to QT4 or Tk, depending on your purpose. WxWidgets has many widgets available such as buttons, menu, text but also more advanced widgets as a htmlview or tree control.
In this tutorial you will learn about in network programming. You will learn about the client-server model that is in use for the World Wide Web, E-mail and many other applications.
This code will start a simple web server using sockets. It waits for a connection and if a connection is received it will output the bytes received.
#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 62 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket. AF_INET, socket. SOCK_STREAM) s. bind((TCP_IP, TCP_PORT )) s. listen(1) conn, addr = s. accept() print 'Connection address:', addr while 1: data = conn. recv(BUFFER_SIZE ) if not data: break print "received data:", data conn. send(data ) # echo conn. close() |
Execute with:
$ python server. py |
This opens the web server at port 62. In a second screen, open a client with Telnet. If you use the same machine for the client and server use:
$ telnet 127.0.0.1 62. |
If you use another machine as client, type the according IP address of that machine. You can find it with ifconfig.
Everything you write from the client will arrive at the server. The server sends the received messages back. An example output below (Click to enlarge):
The client script below sends a message to the server. The server must be running!
#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 1024 MESSAGE = "Hello, World!" s = socket.socket(socket. AF_INET, socket. SOCK_STREAM) s. connect((TCP_IP, TCP_PORT )) s. send(MESSAGE ) data = s. recv(BUFFER_SIZE ) s. close() print "received data:", data |
This client simply mimics the behavior we did in Telnet.
Limitations of the server code
The server code above can only interact with one client. If you try to connect with a second terminal it simply won’t reply to the new client. To let the server interact with multiple clients you need to use multi-threading. We rebuild the server script to accept multiple client connections:
#!/usr/bin/env python
import socket
from threading
import Thread
from SocketServer
import ThreadingMixIn
class ClientThread (Thread ):
def __init__(self,ip,port ):
Thread.__init__(self)
self. ip = ip
self. port = port
print "[+] New thread started for "+ip+":"+str(port )
def run (self):
while True:
data = conn. recv(2048)
if
not data:
break
print "received data:", data
conn. send(data ) # echo
TCP_IP = '0.0.0.0'
TCP_PORT = 62
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
tcpsock = socket.socket(socket. AF_INET, socket. SOCK_STREAM)
tcpsock. setsockopt(socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
tcpsock. bind((TCP_IP, TCP_PORT ))
threads = []
while True:
tcpsock. listen(4)
print "Waiting for incoming connections..."
(conn, (ip,port )) = tcpsock. accept()
newthread = ClientThread (ip,port )
newthread. start()
threads. append(newthread )
for t
in threads:
t. join() |
So far we have simply sent messages back and forth. Every message can have a specific meaning in an application. This is known as the protocol. The meaning of these messages must be the same on both the sender and receiver side. The Transport Layer below makes sure that messages are received (TCP). The Internet Layer is the IPv4 protocol. All we have to define is the Application Layer.
Below we modified the server to accept simple commands (We use the non-threading server for simplicity). We changed the port to 64. Server code with a protocol:
#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 64 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket. AF_INET, socket. SOCK_STREAM) s. bind((TCP_IP, TCP_PORT )) s. listen(1) conn, addr = s. accept() print 'Connection address:', addr while 1: data = conn. recv(BUFFER_SIZE ) if not data: break print "received data:", data #conn.send(data) # echo if "/version" in data: conn. send("Demo versionn") if "/echo" in data: data = data. replace("/echo","") conn. send(data + "n") conn. close() |
Run the server with:
sudo python server. py |
A client can then connect with telnet (make sure you pick the right IP):
$ telnet 127.0.0.1 64 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. message /version Demo version /echo Repeat this Repeat this |
If you want to request data from webservers, the traditional way to do that in Python is using the urllib library. While this library is effective, you could easily create more complexity than needed when building something. Is there another way?
Requests is an Apache2 Licensed HTTP library, written in Python. It’s powered by httplib and urllib3, but it does all the hard work for you.
To install type:
git clone https://github. com/kennethreitz/requests. git cd requests sudo python setup. py install |
The Requests library is now installed. We will list some examples below:
Grabbing raw html using HTTP/HTTPS requests
We can now query a website as :
import requests r = requests. get('http://pythonspot.com/') print r. content |
Save it and run with:
python website.py
It will output the raw HTML code.
Download binary image using Python
from PIL import Image from StringIO import StringIO import requests r = requests. get('http://1.bp.blogspot.com/_r-MQun1PKUg/SlnHnaLcw6I/AAAAAAAAA_U$ i = Image.open(StringIO(r.content)) i.show() |
An image retrieved using python
Website status code (is the website online?)
import requests r = requests. get('http://pythonspot.com/') print r. status_code |
This returns 200 (OK). A list of status codes can be found here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Retrieve JSON from a webserver
You can easily grab a JSON object from a webserver.
import requests import requests r = requests. get('https://api.github.com/events') print r. json() |
HTTP Post requests using Python
from StringIO import StringIO import requests payload = {'key1': 'value1', 'key2': 'value2'} r = requests. post("http://httpbin.org/post", data=payload ) print(r. text) |
SSL verification, verify certificates using Python
from StringIO import StringIO import requests print requests. get('https://github.com', verify=True) |
Extract data from the HTTP response header
With every request you send to a HTTP server, the server will send you some additional data. You can get extract data from an HTTP response using:
#!/usr/bin/env python import requests r = requests. get('http://pythonspot.com/') print r. headers |
This will return the data in JSON format. We can parse the data encoded in JSON format to a Python dict.
#!/usr/bin/env python import requests import json r = requests. get('http://pythonspot.com/') jsondata = str(r. headers). replace('\'','"') headerObj = json. loads(jsondata ) print headerObj ['server'] print headerObj ['content-length'] print headerObj ['content-encoding'] print headerObj ['content-type'] print headerObj ['date'] print headerObj ['x-powered-by'] |
Extract data from HTML response
Once you get the data from a server, you can parse it using python string functions or use a library. BeautifulSoup is often used. An example code that gets the page title and links:
from bs4 import BeautifulSoup import requests # get html data r = requests. get('http://stackoverflow.com/') html_doc = r. content # create a beautifulsoup object soup = BeautifulSoup (html_doc ) # get title print soup. title # print all links for link in soup. find_all('a'): print(link. get('href')) |
The urllib2 module can be used to download data from the web (network resource access). This data can be a file, a website or whatever you want Python to download. The module supports HTTP, HTTPS, FTP and several other protocols.
In this article you will learn how to download data from the web using Python.
To download a plain text file use this code:
import urllib2 response = urllib2. urlopen('https://wordpress.org/plugins/about/readme.txt') data = response. read() print(data ) |
We get a response object using the urllib2.urlopen() method, where the parameter is the link. All of the file contents is received using the response.read() method call. After calling this, we have the file data in a Python
variable of type string.This will request the html code from a website. It will output everything to the screen.
import urllib2 response = urllib2. urlopen('http://en.wikipedia.org/') html = response. read() print html |
You can save the data to disk very easily after downloading the file:
import urllib2 response = urllib2. urlopen('https://wordpress.org/plugins/about/readme.txt') data = response. read() # Write data to file filename = "test.txt" file_ = open(filename, 'w') file_. write(data ) file_. close() |
The first part of the code downloads the file contents into the variable data:
import urllib2 response = urllib2. urlopen('https://wordpress.org/plugins/about/readme.txt') data = response. read() |
The second part stores it into a file (this file does not need to have the same filename)
# Write data to file filename = "test.txt" file_ = open(filename, 'w') file_. write(data ) file_. close() |
The ‘w’ parameter creates the file (or overwrites if it exists). You can read more about writing files here.
In this article you will learn how to parse the HTML (HyperText Mark-up Language) of a website. There are several Python libraries to achieve that. We will give a demonstration of a few popular ones.
Beautiful Soup – a python package for parsing HTML and XML
This library is very popular and can even work with malformed markup. To get the contents of a single div, you can use the code below:
from BeautifulSoup import BeautifulSoup import urllib2 # get the contents response = urllib2. urlopen('http://en.wikipedia.org/wiki/Python_(programming_language)') html = response. read() parsed_html = BeautifulSoup (html ) print parsed_html. body. find('div', attrs={'class':'toc'}) |
This will output the HTML code of within the div called ‘toc’ (table of contents) of the wikipedia article. If you want only the raw text use:
print parsed_html. body. find('div', attrs={'class':'toc'}). text |
If you want to get the page title, you need to get it from the head section:
print parsed_html. head. find('title'). text |
To grab all images URLs from a website, you can use this code:
from BeautifulSoup import BeautifulSoup import urllib2 url = 'http://www.arstechnica.com/' data = urllib2. urlopen(url ). read() soup = BeautifulSoup (data ) links = soup. findAll('img', src=True) for link in links: print(link ["src"]) |
To grab all URLs from the webpage, use this:
from BeautifulSoup import BeautifulSoup import urllib2 url = 'http://www.arstechnica.com/' data = urllib2. urlopen(url ). read() soup = BeautifulSoup (data ) links = soup. findAll('a') for link in links: print(link ["href"]) |
PyQuery – a jquery like library for Python
To extract data from the tags we can use PyQuery. It can grab the actual text contents and the html contents, depending on what you need. To grab a tag you use the call pq(‘tag’).
from pyquery import PyQuery import urllib2 response = urllib2. urlopen('http://en.wikipedia.org/wiki/Python_(programming_language)') html = response. read() pq = PyQuery (html ) tag = pq ('div#toc') # print the text of the div print tag. text() # print the html of the div print tag. html() |
To get the title simply use:
tag = pq ('title') |
HTMLParser – Simple HTML and XHTML parser
The usage of this library is very different. With this library you have to put all your logic in the WebParser class. A basic example of usage below:
from HTMLParser import HTMLParser import urllib2 # create parse class WebParser (HTMLParser): def handle_starttag (self, tag, attrs ): print "Tag: " + tag # get the contents response = urllib2. urlopen('http://en.wikipedia.org/wiki/Python_(programming_language)') html = response. read() # instantiate the parser and fed it some HTML parser = WebParser () parser. feed(html ) |
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.
Directory listing
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") |
Other functions
For other functions please refer to the official library documentation.
There are tons of (ro)bots out there for IRC (Internet Relay Chat). So how do you start and build one in Python, just for fun?
You will need a program that connects with an IRC server and acts like a traditional IRC client. IRC servers never ask for any type of complicated human verification such as solving captchas, which is why we can simply connect with a script. The script itself will use network sockets, a library that is often used to provide network interactions in many programming languages including Python and C/C++.
IRC and Python
To communicate with an IRC server, you need to use the IRC protocol. The IRC protocol has distinct messages such as PRIVMSG, USER, NICK and JOIN. If you are curious, you could read the entire protocol. But following this tutorial may be a lot simpler 😉 Authentication is achieved using only a few steps:
The IRC protocol is a layer on top of the IP protocol. To create a socket we use the command:
irc = socket.socket(socket. AF_INET, socket. SOCK_STREAM) |
socket.AF_INET tells the library to use the network protocol IPv4. The second argument tells the library to use stream sockets, which are traditionally implemented on the TCP protocol. (IRC works over TCP/IP). We then must use the commands to authenticate with the server:
USER botname botname botname: phrase NICK botname JOIN #channel |
Sometimes the IDENT command is neccesary too. Summing up, we get this class (save it as irc.py):
import socket
import sys
class IRC:
irc = socket.socket()
def __init__(self):
self. irc = socket.socket(socket. AF_INET, socket. SOCK_STREAM)
def send (self, chan, msg ):
self. irc. send("PRIVMSG " + chan + " " + msg + "n")
def connect (self, server, channel, botnick ):
#defines the socket
print "connecting to:"+server
self. irc. connect((server, 6667)) #connects to the server
self. irc. send("USER " + botnick + " " + botnick +" " + botnick + " :This is a fun bot!n") #user authentication
self. irc. send("NICK " + botnick + "n")
self. irc. send("JOIN " + channel + "n") #join the chan
def get_text (self):
text=self. irc. recv(2040) #receive the text
if text. find('PING') != -1:
self. irc. send('PONG ' + text. split() [1] + 'rn')
return text |
Now that we have the network connectivity class, we can use it as an instance. We will keep our (ro)bot simple for explanatory purposes. The bot will reply “Hello!” if it gets the message “hello” in the channel it resides.
from irc import * import os import random channel = "#testit" server = "irc.freenode.net" nickname = "reddity" irc = IRC () irc. connect(server, channel, nickname ) while 1: text = irc. get_text() print text if "PRIVMSG" in text and channel in text and "hello" in text: irc. send(channel, "Hello!") |
Save it as bot.py and run with python bot.py. Connect with a traditional irc client (mirc,hexchat,irsii) to the the channel and observe the experiment has worked! You can now extend it with any cool features you can imagine.
[toc]In this tutorial you will learn how to receive email using the poplib module. The mail server needs to support pop3, but most mail servers do this. The Post Office Protocol (POP3) is for receiving mail only, for sending you will need the SMTP protocol.
Every email will contain many variables, but these are the most important ones:
Feature | Description |
---|---|
message-id | unique identifier |
from | where did the email come from? |
to | where was the email sent to? |
date | date |
subject | Email subject. |
You can request messages directly from a mail server using the Post Office Protocol (protocol). You do not have to worry about the internal protocol because you can use the poplib module.
Connect and authenticate with the server using:
# connect to server server = poplib. POP3(SERVER ) # login server.user(USER ) server. pass_(PASSWORD ) |
The program below gets 10 emails from the server including mail header
import poplib import string, random import StringIO, rfc822 def readMail (): SERVER = "YOUR MAIL SERVER" USER = "YOUR USERNAME [email protected]" PASSWORD = "YOUR PASSWORD" # connect to server server = poplib. POP3(SERVER ) # login server.user(USER ) server. pass_(PASSWORD ) # list items on server resp, items, octets = server.list() for i in range(0,10): id, size = string. split(items [i ]) resp, text, octets = server. retr(id) text = string. join(text, "\n") file = StringIO.StringIO(text ) message = rfc822. Message(file) for k, v in message. items(): print k, "=", v readMail () |
Web scraping is the technique to extract data from a website.
The module BeautifulSoup is designed for web scraping. The BeautifulSoup module can handle HTML and XML. It provides simple method for searching, navigating and modifying the parse tree.
The example below prints all links on a webpage:
from BeautifulSoup import BeautifulSoup import urllib2 import re html_page = urllib2. urlopen("http://arstechnica.com") soup = BeautifulSoup (html_page ) for link in soup. findAll('a', attrs={'href': re.compile("^http://")}): print link. get('href') |
It downloads the raw html code with the line:
html_page = urllib2. urlopen("http://arstechnica.com") |
A BeautifulSoup object is created and we use this object to find all links:
soup = BeautifulSoup (html_page ) for link in soup. findAll('a', attrs={'href': re.compile("^http://")}): print link. get('href') |
To store the links in an array you can use:
from BeautifulSoup import BeautifulSoup import urllib2 import re html_page = urllib2. urlopen("http://arstechnica.com") soup = BeautifulSoup (html_page ) links = [] for link in soup. findAll('a', attrs={'href': re.compile("^http://")}): links. append(link. get('href')) print(links ) |
If you repeatingly extract links you can use the function below:
from BeautifulSoup import BeautifulSoup import urllib2 import re def getLinks (url ): html_page = urllib2. urlopen(url ) soup = BeautifulSoup (html_page ) links = [] for link in soup. findAll('a', attrs={'href': re.compile("^http://")}): links. append(link. get('href')) return links print( getLinks ("http://arstechnica.com") ) |
A line chart can be created using the Matplotlib plot() function. While we can just plot a line, we are not limited to that. We can explicitly define the grid, the x and y axis scale and labels, title and display options.
Line chart example
The example below will create a line chart.
from pylab import * t = arange (0.0, 2.0, 0.01) s = sin (2.5*pi*t ) plot (t, s ) xlabel ('time (s)') ylabel ('voltage (mV)') title ('Sine Wave') grid (True) show () |
Output:
The lines:
from pylab import * t = arange (0.0, 2.0, 0.01) s = sin (2.5*pi*t ) |
simply define the data to be plotted.
from pylab import * t = arange (0.0, 2.0, 0.01) s = sin (2.5*pi*t ) plot (t, s ) show () |
plots the chart. The other statements are very straightforward: statements xlabel() sets the x-axis text, ylabel() sets the y-axis text, title() sets the chart title and grid(True) simply turns on the grid.
If you want to save the plot to the disk, call the statement:
savefig ("line_chart.png") |
Plot a custom Line Chart
If you want to plot using an array (list), you can execute this script:
from pylab import * t = arange (0.0, 20.0, 1) s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] plot (t, s ) xlabel ('Item (s)') ylabel ('Value') title ('Python Line Chart: Plotting numbers') grid (True) show () |
The statement:
t = arange (0.0, 20.0, 1) |
defines start from 0, plot 20 items (length of our array) with steps of 1.
Output:
Multiple plots
If you want to plot multiple lines in one chart, simply call the plot() function multiple times. An example:
from pylab import * t = arange (0.0, 20.0, 1) s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] s2 = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] plot (t, s ) plot (t, s2 ) xlabel ('Item (s)') ylabel ('Value') title ('Python Line Chart: Plotting numbers') grid (True) show () |
Output:
In case you want to plot them in different views in the same window you can use this:
import matplotlib. pyplot as plt from pylab import * t = arange (0.0, 20.0, 1) s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] s2 = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] plt. subplot(2, 1, 1) plt. plot(t, s ) plt. ylabel('Value') plt. title('First chart') plt. grid(True) plt. subplot(2, 1, 2) plt. plot(t, s2 ) plt. xlabel('Item (s)') plt. ylabel('Value') plt. title('Second chart') plt. grid(True) plt. show() |
Output:
The plt.subplot() statement is key here. The subplot() command specifies numrows, numcols and fignum.
Styling the plot
If you want thick lines or set the color, use:
plot (t, s, color="red", linewidth=2.5, linestyle="-") |
Matplotlib can be used to create histograms. A histogram shows the frequency on the vertical axis and the horizontal axis is another dimension. Usually it has bins, where every bin has a minimum and maximum value. Each bin also has a frequency between x and infinite.
Matplotlib histogram example
Below we show the most minimal Matplotlib histogram:
import numpy as np import matplotlib. mlab as mlab import matplotlib. pyplot as plt x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100] num_bins = 5 n, bins, patches = plt. hist(x, num_bins, facecolor='blue', alpha=0.5) plt. show() |
Output:
A complete matplotlib python histogram
Many things can be added to a histogram such as a fit line, labels and so on. The code below creates a more advanced histogram.
#!/usr/bin/env python import numpy as np import matplotlib. mlab as mlab import matplotlib. pyplot as plt # example data mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x = mu + sigma * np.random. randn(10000) num_bins = 20 # the histogram of the data n, bins, patches = plt. hist(x, num_bins, normed=1, facecolor='blue', alpha=0.5) # add a 'best fit' line y = mlab. normpdf(bins, mu, sigma ) plt. plot(bins, y, 'r--') plt. xlabel('Smarts') plt. ylabel('Probability') plt. title(r'Histogram of IQ: $\mu=100$, $\sigma=15$') # Tweak spacing to prevent clipping of ylabel plt. subplots_adjust(left=0.15) plt. show() |
Output:
Matplotlib may be used to create bar charts. You might like the Matplotlib gallery.
Bar chart code
The code below creates a bar chart:
import matplotlib. pyplot as plt; plt. rcdefaults() import numpy as np import matplotlib. pyplot as plt objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp') y_pos = np. arange(len(objects )) performance = [10,8,6,4,2,1] plt. bar(y_pos, performance, align='center', alpha=0.5) plt. xticks(y_pos, objects ) plt. ylabel('Usage') plt. title('Programming language usage') plt. show() |
Output:
Matplotlib charts can be horizontal, to create a horizontal bar chart:
import matplotlib. pyplot as plt; plt. rcdefaults() import numpy as np import matplotlib. pyplot as plt objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp') y_pos = np. arange(len(objects )) performance = [10,8,6,4,2,1] plt. barh(y_pos, performance, align='center', alpha=0.5) plt. yticks(y_pos, objects ) plt. xlabel('Usage') plt. title('Programming language usage') plt. show() |
Output:
More on bar charts
You can compare two data series using this Matplotlib code:
import numpy as np import matplotlib. pyplot as plt # data to plot n_groups = 4 means_frank = (90, 55, 40, 65) means_guido = (85, 62, 54, 20) # create plot fig, ax = plt. subplots() index = np. arange(n_groups ) bar_width = 0.35 opacity = 0.8 rects1 = plt. bar(index, means_frank, bar_width, alpha=opacity, color='b', label='Frank') rects2 = plt. bar(index + bar_width, means_guido, bar_width, alpha=opacity, color='g', label='Guido') plt. xlabel('Person') plt. ylabel('Scores') plt. title('Scores by person') plt. xticks(index + bar_width, ('A', 'B', 'C', 'D')) plt. legend() plt. tight_layout() plt. show() |
Output:
Download All Matplotlib Examples
Matplotlib supports pie charts using the pie() function. You might like the Matplotlib gallery.
Matplotlib pie chart
The code below creates a pie chart:
import matplotlib. pyplot as plt # Data to plot labels = 'Python', 'C++', 'Ruby', 'Java' sizes = [215, 130, 245, 210] colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] explode = (0.1, 0, 0, 0) # explode 1st slice # Plot plt. pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) plt. axis('equal') plt. show() |
Output:
To add a legend use the plt.legend() function:
import matplotlib. pyplot as plt labels = ['Cookies', 'Jellybean', 'Milkshake', 'Cheesecake'] sizes = [38.4, 40.6, 20.7, 10.3] colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] patches, texts = plt. pie(sizes, colors=colors, shadow=True, startangle=90) plt. legend(patches, labels, loc="best") plt. axis('equal') plt. tight_layout() plt. show() |
Output:
Download All Matplotlib Examples
Save figure
Matplotlib can save plots directly to a file using savefig().
The method can be used like this:
fig. savefig('plot.png') |
Complete example:
import matplotlib import matplotlib. pyplot as plt import numpy as np y = [2,4,6,8,10,12,14,16,18,20] x = np. arange(10) fig = plt. figure() ax = plt. subplot(111) ax. plot(x, y, label='$y = numbers') plt. title('Legend inside') ax. legend() #plt.show() fig. savefig('plot.png') |
To change the format, simply change the extension like so:
fig. savefig('plot.pdf') |
You can open your file using
display plot. png |
Updating a matplotlib plot is straightforward. Create the data, the plot and update in a loop.
Setting interactive mode on is essential: plt.ion(). This controls if the figure is redrawn every draw() command. If it is False (the default), then the figure does not update itself.
Update plot example
Copy the code below to test an interactive plot.
import matplotlib. pyplot as plt import numpy as np x = np. linspace(0, 10*np. pi, 100) y = np. sin(x ) plt. ion() fig = plt. figure() ax = fig. add_subplot(111) line1, = ax. plot(x, y, 'b-') for phase in np. linspace(0, 10*np. pi, 100): line1. set_ydata(np. sin(0.5 * x + phase )) fig. canvas. draw() |
Explanation
We create the data to plot using:
x = np. linspace(0, 10*np. pi, 100) y = np. sin(x ) |
Turn on interacive mode using:
plt. ion() |
Configure the plot (the ‘b-‘ indicates a blue line):
fig = plt. figure() ax = fig. add_subplot(111) line1, = ax. plot(x, y, 'b-') |
And finally update in a loop:
for phase in np. linspace(0, 10*np. pi, 100): line1. set_ydata(np. sin(0.5 * x + phase )) fig. canvas. draw() |
Matplotlib supports plots with time on the horizontal (x) axis. The data values will be put on the vertical (y) axis. In this article we’ll demonstrate that using a few examples.
It is required to use the Python datetime module, a standard module.
Plot time
You can plot time using a timestamp:
import matplotlib import matplotlib. pyplot as plt import numpy as np import datetime # create data y = [ 2,4,6,8,10,12,14,16,18,20 ] x = [datetime.datetime. now() + datetime. timedelta(hours=i ) for i in range(len(y ))] # plot plt. plot(x,y ) plt. gcf(). autofmt_xdate() plt. show() |
If you want to change the interval use one of the lines below:
# minutes x = [datetime.datetime. now() + datetime. timedelta(minutes=i ) for i in range(len(y ))] |
Time plot from specific hour/minute
To start from a specific date, create a new timestamp using datetime.datetime(year, month, day, hour, minute).
Full example:
import matplotlib import matplotlib. pyplot as plt import numpy as np import datetime # create data customdate = datetime.datetime(2016, 1, 1, 13, 30) y = [ 2,4,6,8,10,12,14,16,18,20 ] x = [customdate + datetime. timedelta(hours=i ) for i in range(len(y ))] # plot plt. plot(x,y ) plt. gcf(). autofmt_xdate() plt. show() |
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.
The match function is defined as:
re. match(pattern, string) |
The parameters are:
Parameters | Description |
---|---|
pattern | a regular expression |
string | the input string |
If you want to match a string to a numberic sequence of exactly five, you can use this code:
#!/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:
String | Match |
---|---|
12345 | True |
12358 | True |
55555 | True |
123 | False |
123K5 | False |
5555555 | False |
We can use the same function to validate email address. The grammar rules are seen in re.compile and in the grammar table.
#!/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 is defined as:
re. search(pattern, string) |
The parameters are:
Parameter | Description |
---|---|
pattern | a regular expression, defines the string to be searched |
string | the search space |
To search if an e-mail address is in a string:
#!/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.") |
A few examples of regular expressions:
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 protected][^@]+\.[^@]+ | |
Date MM/DD/YY | (\d+/\d+/\d+) |
Integer (positive) | (? |
Integer | [+-]?(? |
Float | (?<=>)\d+.\d+|\d+ |
Hexadecimal | \s–([0-9a-fA-F]+)(?:–)?\s |
Overview of the regex grammar:
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 |
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.
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. >>> from qutip import * >>> about () QuTiP: Quantum Toolbox in Python Copyright (c ) 2011 and later. Paul D. Nation & 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/2.2.0/index.html
You can use Python instead of JavaScript and in this tutorial we will show you how to do that.
In a hurry? Download the code from this site:
> Download Extension Code
(and scroll down to Method B)
To start, we will have to create a manifest file: manifest.json.
{ "manifest_version": 2, "name": "Python Chrome Plugin", "description": "This extension runs Python code.", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "activeTab", "https://ajax.googleapis.com/" ] } |
Create a file called popup.html
<!doctype html> <!-- This page is shown when the extension button is clicked, because the "browser_action" field in manifest. json contains the "default_popup" key with value "popup.html". --> <html> <head> <title>Getting Started Extension's Popup</title> <style> body { font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif; font-size: 100%; } #status { /* avoid an excessively wide status text */ white-space: pre; text-overflow: ellipsis; overflow: hidden; max-width: 400px; } </style> <!-- - JavaScript and HTML must be in separate files: see our Content Security - Policy documentation[1] for details and explanation. - - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy --> <script src="popup.js"></script> </head> <body> <div id="status"></div> <img id="image-result" hidden> </body> </html> |
Finally get an icon and save it as icon.png. Open chrome://extensions and press developer mode. Press “load unpacked extension”, select your directory
and press ok.We have two options to add Python into a chrome extension:
Now that you have the basics right we can add Python to the code. To run Python in the browser you have several options including Brython and emcascripten. We decided to give Brython a try. We will run the Brython script from a server. Change popup.html to:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="iso-8859-1"> <style> body { margin: 0 !important; padding: 0 !important; width: 800; } #frame { overflow: hidden; width:790; height:324; } </style> </head> <body onLoad=""> <iframe src=http://brython. info/console. html id="frame" seamless="seamless" scrolling="no"></iframe> </body> </html> |
Once you restart your plugin you will have a Python (Brython) interpreter inside your Google Chrome.
Running your own scripts
To run your own script simply change the url inside the popup.html frame:
<iframe src="BRYTHON SCRIPT URL" id="frame" seamless="seamless" scrolling="no"></iframe> |
The script should run on your own server. You can run any Brython script from the web. Using Brython you can simply type Python code inside the script tags. Have a look at this Brython examples or simply browse the gallery.
There are several tools to compile Python to Javascript. Rapydscript works fine, Pyjs does not work well with chrome (requires special parameter on start).
Install Rapydscript with:
sudo apt-get install npm sudo ln -s /usr/bin/nodejs /usr/bin/node sudo npm install rapydscript |
Download the code from this site:
Download Extension Code
Change the file /src/hello.py to you needs:
# Example Python script # (for rapydscript, a python to javascript compiler) #def doHelloMessage(): # alert('hello') #doHelloMessage() # modify html page document. getElementById("result"). innerHTML = 'Compiled Python script in Chrome' # write into log console. log('hello from python') |
Run:
./make. sh |
You can find your extension in /compiledpythonextension/. Load it in chrome as unpackaged extension and
see it working 🙂Chrome plugins are created using HTML, JavaScript and CSS. We can use Python to create normal Chrome extensions using a Python to Javascript compiler (Rapydscript).
Leave a comment 🙂
You may like:
Teach Your Kids to Code: Learn Python Programming at Any Age
Starting a simple HTTP web server
A simple HTTP Server can be started in seconds.
python -m SimpleHTTPServer |
For Python3:
python -m http. server |
Once started you can open http://127.0.0.1:8000/. The browser will show you the contents of the directory.
A funny text
Try the statement below to display a poem by Tim Peters.
import this |
XKCD web comic
You can open a comic on XKCD using:
import antigravity |
Using Zip to combine arrays
You can zip the two arrays with:
b = [[1, 2, 3, 4], [6, 5, 4, 3]] zip(*b ) [(1, 6), (2, 5), (3, 4), (4, 3)] |
Reverse a list
To reverse a list, you could implement a function. But why go through the trouble if it’s already implemented?
b = [1,2,3,4,5] b. reverse() print b [5, 4, 3, 2, 1] |
Reverse a string
Instead of creating a method to reverse a string you can use:
s = "Hello world" s = s [::-1] print s dlrow olleH |
Swapping variables
You do not need to define a temporary variable to swap to variables in Python:
a = 1 b = 3 b,a = a,b print a print b 1 |
If you know of any tips or tricks, leave a comment. 🙂
A text-to-speech (TTS) system converts normal language text into speech. How can we use speech synthesis in Python?
Pyttsx is a cross-platform speech (Mac OSX, Windows, and Linux) library. You can set voice metadata such as age, gender, id, language and name. Thee speech engine comes with a large amount of voices.
Text to speech sample:
Install with:
sudo pip install pyttsx |
Create the code speech1.py
import pyttsx engine = pyttsx. init() engine. say('The quick brown fox jumped over the lazy dog.') engine. runAndWait() |
And execute it with python.
eSpeak is a compact open source software speech synthesizer for English and other languages, for Linux and Windows.
Text to speech sample:
We can install using:
sudo apt-get install espeak |
Create the code speech2.py:
import os os. system("espeak 'The quick brown fox'") |
It is very easy to use, but like pyttsx it sounds very robotic.
The gtts module no longer works.
I found a script on Github that uses the Google speech engine. The script comes with many options and does not speak, instead it saves to an mp3. We added a command to play the mp3 automatically:
os. system("mpg321 out.mp3 -quiet") |
Run with:
python gtts. py -s 'Python programming example' |
The voice is extremely natural. The only disadvantage is that you need to be connected with the Internet when
running this script.You might like:
Python scripts can be run on Android using the Scripting Layer For Android (SL4A) in combination with a Python interpreter for Android.
SL4A
The SL4A project makes scripting on Android possible, it supports many programming languages including Python, Perl, Lua, BeanShell, JavaScript, JRuby and shell. The SL4A project has a lot of contributors from Google but it is not an official Google project.
Scripts can access Android specific features such as calling, text message (SMS), take picture, text to speech, bluetooth and many more.
In this article you will learn how to run Python on Android devices using SL4A.
Keep in mind that SL4A is designed for developers and in alpha quality software.
First enable installation of programs from unknown sources. By default Android devices can only install apps from the Google Play Store.
You have to enable the permission ‘Install from Unknown Sources’, by going to Settings -> Security -> Unknown Sources and tap the box.
After you have have updated these settings donwload the SL4A APK. Visit https://github.com/kuri65536/sl4a on your Android device and download the SL4A APK (or use the QR code on the right).
Once downloaded an installation menu will popup, requesting all permissions on your Android device.
Install the Py4A app. The Python for Android app is built to run solely on
Android devices. You should use this app together with SL4A.
You can pick any version of Py4A, but bare in mind the supported version on Android:
The git repository is: https://github.com/kuri65536/python-for-android/releases
You could also use the QR code on the right using a QR scanner on your Android device.
Once Py4A is installed, start the app and press install. This will install the Python interpreter.
Open SL4A again. Many scripts will appear (in a list). You can now run Python scripts on your Android Device!
Press on a program such as speak.py A little popup will be shown. Pressing on the terminal icon will start the Python script.
The third button (the pencil) will open an editor. This is not a full blown IDE but a simple editor.
You may prefer your favorite Python editor whatever it may be (vim/emacs fans here? PyCharm? Atom?)
All scripts are stored in /sl4a/scripts/
If you installed the Python 3 interpreter, the programs will show with a .py3 extension instead of a .py extension.
A simple program (Spaceship Launch):
"""TTS Rocket Launch.""" __author__ = 'Frank <[email protected]>' import android droid = android. Android() message = "Python on Android" droid. ttsSpeak(message ) for i in range(10,0,-1): droid. ttsSpeak(str(i )) droid. ttsSpeak("We have lift off!") droid. ttsSpeak("rrrrrr") |
More examples:
http://www.mattcutts.com/blog/android-barcode-scanner/
https://github.com/damonkohler/sl4a/blob/wiki/Tutorials.md
QPython is a script engine that lets you run Python scripts on Android. If you want to make Android apps, use Kivy instead.
QPython
The QPython app contains:
Please do note QPython is a scripting engine, similar to SL4A, and not an app development platform.
On the Google Playstore you can find both QPython (Python 2.7.2) and QPython3 (Python 3.2.2). We recommend going with Python 3.2.2 but you may want to use Python 2.7.2 for legacy reasons.
Once installed you will find yourself in an iconic menu. The first icon will open the Python interpreter.
The second icon (Editor) will start the Python IDE. It has syntax highlighting and some other features. While its useful to have an IDE on the Android device, typing on a mobile or tablet may not be your preference. All scripts are stored in /storage/emulated/0/com.hipipal.qpyplus/scripts. You can upload directly to this directory from your favorite editor on the desktop. (The run button is at the bottom.)
You can use the Python interpreter directly on your Android device. This is similar to starting the Python interpreter on Windows or Linux. While useful at times, you probably prefer using the IDE.
OpenCV (cv2) can be used to extract data from images and do operations on them. We demonstrate some examples of that below:
Image properties
We can extract the width, height and color depth using the code below:
import cv2 import numpy as np # read image into matrix. m = cv2. imread("python.png") # get image properties. h,w,bpp = np. shape(m ) # print image properties. print "width: " + str(w ) print "height: " + str(h ) print "bpp: " + str(bpp ) |
Access pixel data
We can access the pixel data of an image directly using the matrix, example:
import cv2 import numpy as np # read image into matrix. m = cv2. imread("python.png") # get image properties. h,w,bpp = np. shape(m ) # print pixel value y = 1 x = 1 print m [y ][x ] |
To iterate over all pixels in the image you can use:
import cv2 import numpy as np # read image into matrix. m = cv2. imread("python.png") # get image properties. h,w,bpp = np. shape(m ) # iterate over the entire image. for py in range(0,h ): for px in range(0,w ): print m [py ][px ] |
Image manipulation
You can modify the pixels and pixel channels (r,g,b) directly. In the example below we remove one color channel:
import cv2 import numpy as np # read image into matrix. m = cv2. imread("python.png") # get image properties. h,w,bpp = np. shape(m ) # iterate over the entire image. for py in range(0,h ): for px in range(0,w ): m [py ][px ][0] = 0 # display image cv2. imshow('matrix', m ) cv2. waitKey(0) |
To change the entire image, you’ll have to change all channels: m[py][px][0], m[py][px][1], m[py][px][2].
Save image
You can save a modified image to the disk using:
cv2. imwrite('filename.png',m ) |
A histogram is collected counts of data organized into a set of bins. Every bin shows the frequency. OpenCV can generate histograms for both color and gray scale images. You may want to use histograms for computer vision tasks.
Histogram example
Given an image we can generate a histogram for the blue, green and red values.
We use the function cv.CalcHist(image, channel, mask, histSize, range)
Parameters:
Histogram for a color image:
# draw histogram in python. import cv2 import numpy as np img = cv2. imread('image.jpg') h = np. zeros((300,256,3)) bins = np. arange(256). reshape(256,1) color = [ (255,0,0),(0,255,0),(0,0,255) ] for ch, col in enumerate(color ): hist_item = cv2. calcHist([img ],[ch ],None,[256],[0,255]) cv2. normalize(hist_item,hist_item,0,255,cv2. NORM_MINMAX) hist=np. int32(np. around(hist_item )) pts = np. column_stack((bins,hist )) cv2. polylines(h,[pts ],False,col ) h=np. flipud(h ) cv2. imshow('colorhist',h ) cv2. waitKey(0) |
The program will detect regions of interest, classify them as cars and show rectangles around them.
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.For every frame:
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 Code + Video + Cascade file
Template matching is a technique for finding areas of an image that are similar to a patch (template).
Its application may be robotics or manufacturing.
Introduction
A patch is a small image with certain features. The goal of template matching is to find the patch/template in an image.
To find them we need both:
The template image T is slided over the source image S (moved over the source image), and the program tries
to find matches using statistics.Lets have a look at the code:
import numpy as np import cv2 image = cv2. imread('photo.jpg') template = cv2. imread('template.jpg') # resize images image = cv2. resize(image, (0,0), fx=0.5, fy=0.5) template = cv2. resize(template, (0,0), fx=0.5, fy=0.5) # Convert to grayscale imageGray = cv2. cvtColor(image, cv2. COLOR_BGR2GRAY) templateGray = cv2. cvtColor(template, cv2. COLOR_BGR2GRAY) # Find template result = cv2. matchTemplate(imageGray,templateGray, cv2. TM_CCOEFF) min_val, max_val, min_loc, max_loc = cv2. minMaxLoc(result ) top_left = max_loc h,w = templateGray. shape bottom_right = (top_left [0] + w, top_left [1] + h ) cv2. rectangle(image,top_left, bottom_right,(0,0,255),4) # Show result cv2. imshow("Template", template ) cv2. imshow("Result", image ) cv2. moveWindow("Template", 10, 50); cv2. moveWindow("Result", 150, 50); cv2. waitKey(0) |
First we load both the source image and template image with imread(). We resize themand convert them to grayscale for faster detection:
image = cv2. imread('photo.jpg') template = cv2. imread('template.jpg') image = cv2. resize(image, (0,0), fx=0.5, fy=0.5) template = cv2. resize(template, (0,0), fx=0.5, fy=0.5) imageGray = cv2. cvtColor(image, cv2. COLOR_BGR2GRAY) templateGray = cv2. cvtColor(template, cv2. COLOR_BGR2GRAY) |
We use the cv2.matchTemplate(image,template,method) method to find the most similar area in the image. The third argument is the statistical method.
This method has six matching methods: CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED, CV_TM_CCOEFF and CV_TM_CCOEFF_NORMED.
which are simply different statistical comparison methods
Finally, we get the rectangle variables and display the image.
Template matching is not scale invariant nor is it rotation invariant. It is a very basic and straightforward method where we find the most correlating area. Thus, this method of object detection depends on the kind of application you want to build. For non scale and rotation changing input, this method works great.
You may like: Robotics or Car tracking with cascades.
Inspired by Netflix, we decided to implement a focal point algorithm. If you use the generated thumbnails on mobile websites, it may increase your click-through-rate (CTR) for YouTube videos.
Eiterway, it’s a fun experiment.
Focal Point
All images have a region of interest, usually a person or face.
This algorithm that finds the region of interest is called a focal point algorithm. Given an input image, a new image (thumbnail) will be created based on the region of interest.
Start with an snapshot image that you want to use as a thumbnail. We use Haar features to find the most interesting region in an image. The haar cascade files can be found here:
Download these files in a /data/ directory.
#! /usr/bin/python import cv2 bodyCascade = cv2. CascadeClassifier('data/haarcascade_mcs_upperbody.xml') frame = cv2. imread('snapshot.png') frameHeight, frameWidth, frameChannels = frame. shape regions = bodyCascade. detectMultiScale(frame, 1.8, 2) x,y,w,h = regions [0] cv2. imwrite('thumbnail.png', frame [0:frameHeight,x:x+w ]) cv2. rectangle(frame,(x,0),(x+w,frameHeight ),(0,255,255),6) cv2. imshow("Result",frame ) cv2. waitKey(0); |
We load the haar cascade file using cv2.CascadeClassifier() and we load the image using cv2.imread()
Then bodyCascade.detectMultiScale() detects regions of interest using the loaded haar features.
The image is saved as thumbnail using cv2.imwrite() and finally we show the image and highlight the region of interest with a rectangle. After running you will have a nice thumbnail for mobile webpages or apps.
If you also want to detect both body and face you can use:
#! /usr/bin/python import cv2 bodyCascade = cv2. CascadeClassifier('data/haarcascade_mcs_upperbody.xml') faceCascade = cv2. CascadeClassifier('data/lbpcascade_frontalface.xml') frame = cv2. imread('snapshot2.png') frameHeight, frameWidth, frameChannels = frame. shape regions = bodyCascade. detectMultiScale(frame, 1.5, 2) x,y,w,h = regions [0] cv2. imwrite('thumbnail.png', frame [0:frameHeight,x:x+w ]) cv2. rectangle(frame,(x,0),(x+w,frameHeight ),(0,255,255),6) faceregions = faceCascade. detectMultiScale(frame, 1.5, 2) x,y,w,h = faceregions [0] cv2. rectangle(frame,(x,y ),(x+w,y+h ),(0,255,0),6) cv2. imshow("Result",frame ) cv2. waitKey(0); cv2. imwrite('out.png', frame ) |
In this article we will demonstrate how to create a simple guessing game.
The goal of the game is to guess the right number.
Example
An example run below:
You may like
The user will be asked to guess the random number. We first pick the random number:
from random import randint x = randint (1,9) |
The randint() function will pick a pseudo random number between 1 and 10. Then we have to continue until the correct number is found:
guess = -1 print("Guess the number below 10:") while guess != x: guess = int(raw_input("Guess: ")) if guess != x: print("Wrong guess") else: print("Guessed correctly") |
The code below starts the game:
from random import randint x = randint (1,9) guess = -1 print "Guess the number below 10:" while guess != x: guess = int(raw_input("Guess: ")) if guess != x: print("Wrong guess") else: print("Guessed correctly") |
An example run:
Guess the number below 10: Guess: 3 Wrong guess Guess: 6 Wrong guess .. |
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
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.
Continue the next tutorial and learn how to add game logic and build games 🙂
In this tutorial you will learn how to build the game snake. The game is an arcade game and it has very simple logic, which is why it is an ideal example to demonstrate how to build games with Pygame.
The player is represented as snake, which grows if it eats an apple. The goal of the game is to eat as many apples as possible without colliding into yourself. This is very easy in the early phase of the game but is increasingly more difficult as the length of the snake grows.
Getting started: Basic structure and event handling.
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):
class Player: x = 10 y = 10 speed = 1 def moveRight (self): self. x = self. x + self. speed def moveLeft (self): self. x = self. x - self. speed def moveUp (self): self. y = self. y - self. speed def moveDown (self): self. y = self. y + self. speed |
A player object can be created and variables can be modified using the movement methods.
We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:
pygame. event. pump() keys = pygame. key. get_pressed() if (keys [K_RIGHT ]): print "Right arrow pressed." |
The complete code gives us the ability to move the player across the screen:
from pygame.locals
import *
import pygame
class Player:
x = 10
y = 10
speed = 1
def moveRight (self):
self. x = self. x + self. speed
def moveLeft (self):
self. x = self. x - self. speed
def moveUp (self):
self. y = self. y - self. speed
def moveDown (self):
self. y = self. y + self. speed
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self. player = Player ()
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()
def on_event (self, event ):
if event.type == QUIT:
self._running = False
def on_loop (self):
pass
def on_render (self):
self._display_surf. fill((0,0,0))
self._display_surf. blit(self._image_surf,(self. player. x,self. player. 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 ):
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()
self. on_cleanup()
if __name__ == "__main__" :
theApp = App ()
theApp. on_execute() |
You can now move the block around the screen with the arrow keys.
Building the player (snake)
The player controls a snake which has an initial length. This snake is always moving and changes the direction it moves when pressing an arrow key. To do so, update the player class:
class Player: x = 0 y = 0 speed = 32 direction = 0 def update (self): if self. direction == 0: self. x = self. x + self. speed if self. direction == 1: self. x = self. x - self. speed if self. direction == 2: self. y = self. y - self. speed if self. direction == 3: self. y = self. y + self. speed def moveRight (self): self. direction = 0 def moveLeft (self): self. direction = 1 def moveUp (self): self. direction = 2 def moveDown (self): self. direction = 3 |
and don’t forget to add a delay to the game loop.
import time ... time. sleep (100.0 / 1000.0); |
This starts to act more as a snake, but it does not have a base length yet. We keep track of the snakes old positions and move the head of the snake. We also moved the drawing method into the snake. Copy the code and you will have a moving snake:
from pygame.locals
import *
import pygame
import time
class Player:
x = []
y = []
step = 44
direction = 0
length = 3
updateCountMax = 2
updateCount = 0
def __init__(self, length ):
self. length = length
for i
in range(0,length ):
self. x. append(0)
self. y. append(0)
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):
print "self.x[" + str(i ) + "] = self.x[" + str(i-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 App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self. player = Player (10)
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()
def on_event (self, event ):
if event.type == QUIT:
self._running = False
def on_loop (self):
self. player. update()
pass
def on_render (self):
self._display_surf. fill((0,0,0))
self. player. 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() |
Result:
Game logic
The snake game has some rules:
We start by creating a new class that enables us to create apples:
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)) |
For simplicity sake, we display the apples as green cubes. We have this basic code, but it does not do a lot more than moving the snake and showing the apple:
from pygame.locals
import *
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 = []
y = []
step = 44
direction = 0
length = 3
updateCountMax = 2
updateCount = 0
def __init__(self, length ):
self. length = length
for i
in range(0,length ):
self. x. append(0)
self. y. append(0)
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):
print "self.x[" + str(i ) + "] = self.x[" + str(i-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 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. player = Player (10)
self. apple = Apple (5,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. player. update()
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 )
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() |
We have thus to add the game logic. To know if the snakes position matches the apples position, we have to do collision detection. This simply means veryfing that the cordinate of the snake are intersecting with the coordinates of the apple. We create a new method to do that:
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 |
It will return True if the coordinates (x1,y1) are intersection with (x2,y2) given its block size bsize. We call this method to determine if the snake collides with the apple. We need to check this for the entire snake and not only for the head, because we do not want the new position of the apple to be somewhere on the snake. We use the same isCollision method to determine if the snake collides with itself (= lose game).
Full source:
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[1] = 1*44
self. x[2] = 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 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 (3)
self. apple = Apple (5,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("block.jpg"). convert()
self._apple_surf = pygame. image. load("block.jpg"). convert()
def on_event (self, event ):
if event.type == QUIT:
self._running = False
def on_loop (self):
self. player. 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 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 )
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() |
Conclusion:
You learned how to create the game snake in Python along with concepts such as collision detection, image loading and event handling. Many things could be added to this little toy game but this serves as a very simple example. 🙂
In this tutorial you will learn how to build a maze game. The idea is simply to move around the maze with the arrow keys.
Getting started: Basic structure and event handling.
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):
class Player: x = 10 y = 10 speed = 1 def moveRight (self): self. x = self. x + self. speed def moveLeft (self): self. x = self. x - self. speed def moveUp (self): self. y = self. y - self. speed def moveDown (self): self. y = self. y + self. speed |
A player object can be created and variables can be modified using the movement methods. We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:
pygame. event. pump() keys = pygame. key. get_pressed() if (keys [K_RIGHT ]): print "Right arrow pressed." |
The complete code gives us the ability to move the player across the screen:
from pygame.locals
import *
import pygame
class Player:
x = 10
y = 10
speed = 1
def moveRight (self):
self. x = self. x + self. speed
def moveLeft (self):
self. x = self. x - self. speed
def moveUp (self):
self. y = self. y - self. speed
def moveDown (self):
self. y = self. y + self. speed
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self. player = Player ()
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()
def on_event (self, event ):
if event.type == QUIT:
self._running = False
def on_loop (self):
pass
def on_render (self):
self._display_surf. fill((0,0,0))
self._display_surf. blit(self._image_surf,(self. player. x,self. player. 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 ):
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()
self. on_cleanup()
if __name__ == "__main__" :
theApp = App ()
theApp. on_execute() |
You can now move the block around the screen with the arrow keys.
Creating the maze
We define a matrix of NxM to represent the positions of the maze blocks. In this matrix the element 1 represents the presence of a block and element 0 represents the absence.
class Maze:
def __init__(self):
self. M = 10
self. N = 8
self. maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,] |
We have this complete code to draw the maze:
from pygame.locals
import *
import pygame
class Player:
x = 44
y = 44
speed = 1
def moveRight (self):
self. x = self. x + self. speed
def moveLeft (self):
self. x = self. x - self. speed
def moveUp (self):
self. y = self. y - self. speed
def moveDown (self):
self. y = self. y + self. speed
class Maze:
def __init__(self):
self. M = 10
self. N = 8
self. maze = [ 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,1,1,1,1,1,0,1,
1,0,1,0,0,0,0,0,0,1,
1,0,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,]
def draw (self,display_surf,image_surf ):
bx = 0
by = 0
for i
in range(0,self. M*self. N):
if self. maze[ bx + (by*self. M) ] == 1:
display_surf. blit(image_surf,( bx * 44 , by * 44))
bx = bx + 1
if bx > self. M-1:
bx = 0
by = by + 1
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self._block_surf = None
self. player = Player ()
self. maze = Maze ()
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("player.png"). convert()
self._block_surf = pygame. image. load("block.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. fill((0,0,0))
self._display_surf. blit(self._image_surf,(self. player. x,self. player. y))
self. maze. draw(self._display_surf, self._block_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()
self. on_cleanup()
if __name__ == "__main__" :
theApp = App ()
theApp. on_execute() |
Concluding
You learned how to create a 2d maze in Python. Now you may want to add collision detection which we showed in our previous tutorial. Because we already explained that concept we will not go over it again 🙂
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.
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() |
Conclusion
You learned how to create a basic computer player using an very simple AI algorithm.
Next: Learn basic sidescroller logic
In this article you will learn how to implement jump and run logic in Pygame. To do so, we will implement the player logic.
You may like
Teach Your Kids to Code: Learn Python Programming at Any Age
Movement
Moving left and right is similar to the previous tutorial and simply mean changing the (x,y) position of the player. For jumping, we use a formula from classical mechanics:
F = 1/2 * m * v^2 |
Where F is the force up/down, m is the mass of your object and v is the velocity. The velocity goes down over time because when the player jumps the velocity will not increase more in this simulation. If the player reaches the ground, the jump ends. In Python, we set a variable isjump to indicate if the player is jumping or not. If the player is, its position will be updated according to the above formula.
Full Code:
from pygame.locals
import *
import pygame
import math
from time
import sleep
class Player:
x = 10
y = 500
speed = 10
# Stores if player is jumping or not.
isjump = 0
# Force (v) up and mass m.
v = 8
m = 2
def moveRight (self):
self. x = self. x + self. speed
def moveLeft (self):
self. x = self. x - self. speed
def jump (self):
self. isjump = 1
def update (self):
if self. isjump:
# Calculate force (F). F = 0.5 * mass * velocity^2.
if self. v > 0:
F = ( 0.5 * self. m * (self. v*self. v) )
else:
F = - ( 0.5 * self. m * (self. v*self. v) )
# Change position
self. y = self. y - F
# Change velocity
self. v = self. v - 1
# If ground is reached, reset variables.
if self. y >= 500:
self. y = 500
self. isjump = 0
self. v = 8
class App:
windowWidth = 800
windowHeight = 600
player = 0
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None
self. player = Player ()
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()
def on_event (self, event ):
if event.type == QUIT:
self._running = False
def on_loop (self):
pass
def on_render (self):
self._display_surf. fill((0,0,0))
self._display_surf. blit(self._image_surf,(self. player. x,self. player. y))
self. player. update()
pygame. display. flip()
sleep (0.03)
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. jump()
if (keys [K_ESCAPE ]):
self._running = False
self. on_loop()
self. on_render()
self. on_cleanup()
if __name__ == "__main__" :
theApp = App ()
theApp. on_execute() |
If you want to jump on objects, simply add them to the screen, do collision detection and reset the jump variables if collision is true.
Google has a great Speech Recognition API. This API converts spoken text (microphone) into written text (Python strings), briefly Speech to Text. You can simply speak in a microphone and Google API will translate this into written text. The API has excellent results for English language.
Google has also created the JavaScript Web Speech API, so you can recognize speech also in JavaScript if you want, here’s the link: https://www.google.com/intl/en/chrome/demos/speech.html. To use it on the web you will need Google Chrome version 25 or later.
Google Speech API v2 is limited to 50 queries per day. Make sure you have a good microphone.
Are you are looking for text to speech instead?
This is the installation guide for Ubuntu Linux. But this will probably work on other platforms is well. You will need to install a few packages: PyAudio, PortAudio and SpeechRecognition. PyAudio 0.2.9 is required and you may need to compile that manually.
git clone http://people. csail. mit. edu/hubert/git/pyaudio. git cd pyaudio sudo python setup. py install sudo apt-get installl libportaudio-dev sudo apt-get install python-dev sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev sudo pip3 install SpeechRecognition |
This program will record audio from your microphone, send it to the speech API and return a Python string.
The audio is recorded using the speech recognition module, the module will include on top of the program. Secondly we send the record speech to the Google speech recognition API which will then return the output.
r.recognize_google(audio) returns a string.
#!/usr/bin/env python3 # Requires PyAudio and PySpeech. import speech_recognition as sr # Record Audio r = sr. Recognizer() with sr. Microphone() as source: print("Say something!") audio = r. listen(source ) # Speech recognition using Google Speech Recognition try: # for testing purposes, we're just using the default API key # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` # instead of `r.recognize_google(audio)` print("You said: " + r. recognize_google(audio )) except sr. UnknownValueError: print("Google Speech Recognition could not understand audio") except sr. RequestError as e: print("Could not request results from Google Speech Recognition service; {0}". format(e )) |
You may like: Personal Assistant Jarvis (Speech Recognition and Text to Speech) or Speech Engines
I thought it would be cool to create a personal assistant in Python. If you are into movies you may have heard of Jarvis, an A.I. based character in the Iron Man films. In this tutorial we will create a robot.
The features I want to have are:
For this tutorial you will need (Ubuntu) Linux, Python and a working microphone.
This is what you’ll create:
Speech recognition can by done using the Python SpeechRecognition module. We make use of the Google Speech API because of it’s great quality.
Various APIs and programs are available for text to speech applications. Espeak and pyttsx work out of the box but sound very robotic. We decided to go with the Google Text To Speech API, gTTS.
sudo pip install gTTS |
Using it is as simple as:
from gtts import gTTS import os tts = gTTS (text='Hello World', lang='en') tts. save("hello.mp3") os. system("mpg321 hello.mp3") |
The program below will answer spoken questions.
#!/usr/bin/env python3 # Requires PyAudio and PySpeech. import speech_recognition as sr from time import ctime import time import os from gtts import gTTS def speak (audioString ): print(audioString ) tts = gTTS (text=audioString, lang='en') tts. save("audio.mp3") os. system("mpg321 audio.mp3") def recordAudio (): # Record Audio r = sr. Recognizer() with sr. Microphone() as source: print("Say something!") audio = r. listen(source ) # Speech recognition using Google Speech Recognition data = "" try: # Uses the default API key # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` data = r. recognize_google(audio ) print("You said: " + data ) except sr. UnknownValueError: print("Google Speech Recognition could not understand audio") except sr. RequestError as e: print("Could not request results from Google Speech Recognition service; {0}". format(e )) return data def jarvis (data ): if "how are you" in data: speak ("I am fine") if "what time is it" in data: speak (ctime ()) if "where is" in data: data = data. split(" ") location = data [2] speak ("Hold on Frank, I will show you where " + location + " is.") os. system("chromium-browser https://www.google.nl/maps/place/" + location + "/&") # initialization time. sleep(2) speak ("Hi Frank, what can I do for you?") while 1: data = recordAudio () jarvis (data ) |