/* jquery */ /* jquery accordion style*/ /* jquery init */

15 February 2015

Understanding Web Server Python Scripts

HTML, CSS and JavaScript are known as client-side web languages because they run inside the web browser.

Other languages, for example PHP, run back on the web server. The good news is that another server-side web development language is Python.

In server-side mode the Python script files are copied to the appropriate folder in the web server's directory structure (refer to your web server documentation for this folder's name).

These Python script files are then referenced by calls embedded in the HTML web pages.

One typical use for server scripts is to extract the GET or POST parameter valves from HTTP requests. Another is to validate and process user-entered data on a web form.

HTML Form Example

In Python we can access web form data using the FieldStorage() method, from the cgi module.

Take a look at this HTML snippet from a simple web form:

<form method="POST" action="post.py" value="">
   Search: <input type="text" name="search">
   <input type="submit" name="Submit">
</form>

Inside the web form HTML code we have a text entry field named search and a Submit button.

The form tag itself contains an action parameter value that names a server-side Python script file, here it's post.py. This script is called when the Submit button is clicked.

Let's see what this post.py script might look like:

import cgi

data = cgi.FieldStorage() #initialise
txt = data.getvalue('search') #get search field value

After importing the cgi module we use it to call the FieldStorage() method and assign the returned value to a Python variable called data.

In the next line we use data and the getvalue() method to assign the search field data value to a Python variable called txt.

The txt variable can now be used for whatever purpose you like. For example to initiate a Google search request.

A post from my Learn Python on the Raspberry Pi tutorial.

8 February 2015

Python Multi-core Processor Threads

These days virtually every PC, laptop, tablet and smartphone has a multi-core processor. But can Python make use of this additional processing power?

Well, the good news is that Python's core library has full support for threading. So, each computational activity can be assigned its own dedicated process thread. This is particularly useful in PyGame programs where sprites move around independently.

Unfortunately, the bad news is that these threads cannot be run in parallel. And here's why

It's all down to the Python interpreter which internally uses a Global Interpreter Lock (GIL). The GIL can only execute one Python byte-code instruction at a time - even when running on a modern multi-core processor.

However, all is not lost.

The Parallel Python Project has developed a module which overcomes this GIL limitation. Under the covers the pp module uses a combination of processes and inter-process communications (IPC) to implement parallel computation.

So, pop over to the Parallel Python Project website to see examples of the pp module API in action and read the documentation.

An alternative solution is provided by the PyPar project. Originally hosted at Google project, Pypar is an efficient but easy-to-use module that provides parallel execution through message passing, via the message passing interface standard MPI.

A post from my Learn Python on the Raspberry Pi tutorial.

4 February 2015

More Raspberry Pi Java 8 and BlueJ 3.14

In the latest Oracle Java Magazine there's part 2 of Code Java on the Raspberry Pi by Michael Kolling which describes how to install, run and code with BlueJ version 3.14 on the Raspberry Pi.

And remember, this version of BlueJ includes the Pi4J library for direct access to the Raspberry Pi's hardware.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

1 February 2015

Handling Python Exceptions

Exception handling is a core part of the Python language.

There are a number of reasons why using exceptions is a good idea:

(a) exceptions offer protection against unexpected conditions
(b) exceptions can deliver on-screen messages for warning and error reporting
(c) exceptions are an alternative to the print statement for debugging

Exceptions are especially useful in code associated with error-prone processes, for example data input, file operations and wireless communications.

So, what does a Python exception handler look like?

Here's a simple example:

try:
    x = int(raw_input("Please enter a number: ")
    break
except ValueError:
    print "Not a valid number. Please try again..."

The try part of the exception asks the user for a number. While the exception part tests for non-numeric values and reports problems back to the user. So this is a combination of the '(a)' an '(b)' scenarios above.

Here's a more elaborate example:

try:
    f = open(fname, 'r')
except IOError:
    print 'Can't open file: ', fname
else:
    print fname, ' contains ', len(f.readlines()), ' lines '
    f.close()

This time we only try to do something with the file fname if the open() operation is successful. Otherwise we notify the user of the problem.

Take a look at some non-trivial Python source code to see if you spot the exception handlers and work out why they exist.

A post from my Learn Python on the Raspberry Pi tutorial.