7
Exploring Python’s Frontiers

WHAT YOU WILL LEARN IN THIS CHAPTER:    

  • Specialist application areas using Python
  • Third-party packages for specialist applications
  • How to contribute to Python’s development

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

There are no Wrox code downloads for this chapter, but the various solutions discussed all have packages that are available for download. Many of these are available via the Python Package Index (PyPI) or have binary installers that you can download from the package’s home site. Each section provides information about download locations for the associated packages.

In the earlier chapters of this book, you looked at how Python can be used to interact with your operating system and other programs, how it can manage data using flat files and databases, and how you can build both desktop and web-based applications. You have also seen some of the techniques and tools that can help you build larger scale programs efficiently and reliably.

In this concluding chapter, you see how Python can support you in many wider areas of programming. You consider the various frameworks, packages, libraries, and even distinct Python distributions that have been developed to support specialist areas of interest such as science and language processing. You also see how some niche application types have acquired specialist tools and packages to support their specific needs. Finally, you look at how you can contribute to the Python community itself to help make Python even better.

Drawing Pictures with Python

Many tools are available for drawing and processing graphics in Python. These range from simple drawing libraries like turtle to highly specialized modules and frameworks like matplotlib and Pillow. The following sections describe the capabilities and areas of application of each option.

Using Turtle Graphics

The easiest way to draw pictures from code is probably to use turtle graphics. Turtle graphics was invented as a way of drawing pictures using the programming language Logo. The idea was to issue directional commands to a robotic device—the turtle—with a pen attached that, in turn, produced a drawing. The concept proved popular, and now most languages provide some kind of turtle graphics support. In Python, it comes in the form of the turtle module. By default, the module presents the graphics in a small pop-up window built using Tkinter. You can specify a Tk canvas object (see the next section for more details about canvas objects) when you initialize the turtle system from within an application, or you can use the module at the interactive prompt to experiment with the system. The official documentation gives a comprehensive description of the functions and methods available, and you can get a good feel for what is possible by running the demonstration. Type the following at the OS command prompt:

  python -m turtledemo

This brings up a window with a menu of examples that you can start and stop, and also displays the code of the running example so that you can see how to achieve the same effects in your programs.

Using GUI Canvas Objects

Most GUI frameworks include a canvas object. A canvas is an area on-screen in which you can draw lines and shapes, add images, and even insert text. The Tkinter Canvas object is fairly typical and supports drawing arcs, ovals (including circles), lines, rectangles, polygons, text, images, and even windows (so you can embed a widget inside a canvas). A minimal canvas program showing a red circle looks like this:

  >>> import Tkinter as tk
  >>> top = tk.Tk()
  >>> c = tk.Canvas(top, width=50, height=50)
  >>> c.pack()
  >>> c.create_oval(10,10,40,40,outline='red',fill='red')
  1
  >>> top.mainloop()

The Canvas class contains many, many methods that enable you to build sophisticated graphics programs.

This is all at a very low level of abstraction. For a higher level you can turn to other libraries such as the turtle module discussed earlier, or some more exotic, third-party options such as those discussed next.

Plotting Data

The most popular data-plotting tool for Python is matplotlib, which you can find at http://matplotlib.org/ and downloaded from PyPI or included as part of the scipy package discussed later in this chapter. The website includes links to many examples and tutorials.

matplotlib is closely tied into the other scipy packages and, as such, can be rather intimidating if you only want a simple graph. Several other lightweight packages are available on PyPI that attempt to address this and provide an easier-to-use plotting library, but for serious plotting matplotlib is the best solution.

Using imghdr

If your graphics interests are more focused on images than data, the imghdr module offers some useful help in determining what kind of image file you are dealing with. The module is part of the standard library and is quite simple to use. Rather than relying on the filename extension, it tests the data content of the file to determine the image type.

The module consists of a single function, what(), which takes either a filename or filestream as an argument and returns the image type. The module supports most common file types, but you can extend its range by adding your own custom test functions to handle other image types.

Introducing Pillow

For many years the standard solution for manipulating images in Python was the Python Imaging Library (PIL). PIL has not been ported to Python version 3; instead, a replacement library, called Pillow, has been created that builds on PIL but adds some new features.

Pillow’s homepage is at http://pillow.readthedocs.org. You can install it via PyPI.

Pillow is based on an Image class that can be opened and saved. By specifying the appropriate parameters, converting a JPEG file to a PNG file, for example, can be as simple as this:

  >>> from PIL import Image
  >>> Image.open('foo.jpg').save('foo.png')

You can also use the Image object to retrieve information about the image, such as its size. Many more powerful options are available, too. For example, you can transpose images by flipping them or rotating them, as well as resizing and applying filters. Pillow is like an image editing program that you drive programmatically.

Trying Out ImageMagick

ImageMagick is a similar tool to Pillow, but it’s based on the command-line suite of tools of the same name. The command-line website is at http://www.imagemagick.org/.

The Python package, wand, is on PyPI and uses ctypes to harness much of ImageMagick’s power. You can find the website here: http://docs.wand-py.org. The site has documentation, including a user guide and references.

An image conversion program, similar to the Pillow example in the previous section, looks like this:

>>> from wand.image import Image>>> with Image(filename='foo.jpg') as img:...		img.format = 'png'...		img.save(filename='foo.png')>>>

You can find many other modules and packages for graphics work in Python. The PyPI search tool and web search engines will reveal many examples. The tools that have been highlighted in this chapter should cover most eventualities, but don’t be afraid to try alternatives.

Doing Science with Python

Python has a long tradition of use within the scientific and mathematical communities. As a result, many modules and packages have been created to meet the specialized needs of the communities. Before looking at the third-party options available, you should first consider the built-in support that Python offers.

Python’s native types offer much for scientific computing. In particular, the Python integer type with its effectively unlimited size makes it well suited for working with large volumes and long series calculations. Python’s floating-point type is comparable to that of other languages, but in addition you have the options of using decimal and fraction types that reduce errors due to rounding. Finally, Python is one of the few languages that natively supports the complex, or “imaginary,” number type used so extensively in science and engineering applications.

Of course, having a variety of data types is only half the story; along with the data you need operations to support them. Once again, Python’s built-in operations are supplemented by the standard library with modules like math, cmath, and statistics providing a wide range of options. Modules, such as the collections module, also provide support for more exotic data types like named tuples, ordered dictionaries, and chain maps—used as an efficient way to link multiple mappings.

Although these are all powerful tools, they still do not provide the specific support needed for performing detailed scientific analysis, and this is where the special third-party libraries come into play. Chief among these is the SciPy package, discussed next.

Introducing SciPy

SciPy has a long history, evolving out of several independent development streams. These have gradually come together to form a powerful integrated whole. The SciPy project incorporates six separate bundles that form an integrated “stack” of tools. You can find the SciPy website at http://www.scipy.org/.

The six bundles are:

  • NumPy: One of the oldest mathematical packages for Python and the foundation of many others. NumPy includes a set of types and operations suitable for numerical analysis and simulations. These include an N dimensional array object, linear algebra, Fourier transforms, and various random number generators. In addition, NumPy offers hooks to access the wealth of scientific and mathematic tools written in Fortran and C.
  • SciPy: The package for which the project is named. It includes functions for integration, signal processing, sparse data structures, and numerous special-purpose functions.
  • Matplotlib: This package was discussed under the “Plotting Data” heading earlier in this chapter. It produces high-quality graphs, suitable for publication, offering a great deal of control over layout, labeling, and so on. It aims to compete with commercial packages such as Mathematica and MATLAB in this regard. It also supports several GUI toolkits for building graphics-rich desktop applications.
  • SymPy: This is designed to perform symbolic math. Rather than display numerical results, it uses concepts like sqrt(2) as a symbol within its answers. This would look strange to users without a math background, but to mathematicians it is a standard tool and has the advantage of not being subject to the rounding errors associated with traditional floating-point representations. You can think of it as a tool for doing pure math rather than doing arithmetic. In the former, the result is symbolic; in the latter it is a number. You can use it to solve integral and differential calculus problems, Bessel functions, Eigenvalues, and much more. SymPy includes an interactive shell prompt at which you can enter your expressions, as well as a package you can import into your own applications.
  • Pandas: This is a data analysis toolkit. In Chapter 3 you heard about rpy for interfacing with the R statistics analysis language. Pandas is a pure Python alternative to the R environment. At the time of writing, Pandas is less powerful than R in this regard, but it is a project that is growing with each release. In addition, it integrates with the other SciPy packages better than the rpy solution. If you are purely interested in statistics, stick with rpy, but if you want a more integrated analysis workflow and are using the other SciPy elements, take a close look at Pandas.
  • IPython: This is not specifically aimed at scientific users. It is a powerful replacement for the standard Python interactive interpreter. It replaces the traditional >>> prompt with In[n]:, where n is the number of the command. Output lines are preceded by Out[n]: where n matches the value in the corresponding In[]: prompt. As well as understanding all of the usual Python language, IPython adds several new features. For example, there is a shortcut to the help() function. It also enables you to run OS commands by prefixing them with an exclamation point (!). But IPython is much more than just an interactive prompt. It includes a notebook concept where whole sessions can be stored and retrieved. You can thus work on multiple projects and save the state of each when you are done, then restore that state and continue with all history and so on intact. IPython also works with the other SciPy bundles, including matplotlib and SymPy, and can display graphs or symbolic expressions. You can find examples of what the notebook can do along with full documentation on the IPython website at http://ipython.org/. The combination of matplotlib, SymPy, and IPython offers a powerful alternative to commercial packages such as MATLAB or Mathematica.

Finally, several add-ons for SciPy are included under the SciKit banner. These cover areas such as aeronautical engineering; audio, image, and video processing, environmental science, and others.

Doing Bioscience with Python

One area of science that has come to the fore in recent years is bioscience, and particularly the analysis of DNA. The bioPython package has been developed to meet this need (http://biopython.org/). The package includes support for reading and writing most of the standard files used in bioinformatics as well as a Sequence class for analyzing DNA sequences.

In addition to bioPython, some other modules are available, and you can find them using your preferred search engine. Before trying to reinvent the wheel, it is always wise to check whether somebody else has already done the work for you!

Using GIS

With the explosion of satellite navigation and mobile electronic mapping software, the field of Geographic Information Systems (GIS) has seen an upsurge of interest. ArcGIS (https://www
.arcgis.com) is a standardized set of tools for geo-processing. Python support for ArcGIS comes in the form of ArcPy, a package that has the goal of providing “access to geo-processing tools as well as additional functions, classes, and modules that enable you to create simple or complex workflows quickly and easily.”

There is one big problem. At the time of writing, ArcPy is only available for Python v2.7, not for Python v3. However, for GIS processing ArcPy really is the best option currently available.

Watching Your Language

The study of human language and processing natural languages into data has been an area of study for many years. There have been rapid advances in recent years, and with increased computing power natural language processing is starting to appear in mainstream projects.

Python has the Natural Language ToolKit to support this area, and its homepage is located at http://www.nltk.org/. The toolkit provides access to several specialized tools and enables programmers to parse and tokenize text, analyze its structure, and categorize it. You can find and install NLTK from PyPI.

Getting It All

Although all of the previously discussed packages are powerful tools, getting them installed into a standard Python distribution can be a complex process using the normal installation tools. Fortunately, you have alternatives in the shape of Anaconda and Enthought Canopy. These are distributions of Python packaged up with all of the science tools you are likely to need. In addition to the SciPy and NLTK frameworks already discussed, Anaconda has more than 100 other specialist packages built in. The distribution is made available by Continuum Analytics, which also offers other packages on a commercial basis. Canopy is a very similar concept with a free basic version available as well as enhanced, commercial offerings.

Anaconda can be installed on Windows, Mac OS, or Linux and does not interfere with existing installations of Python. Anaconda supports Python versions 2.6 through 3.4 at the time of writing. The Anaconda homepage is located at https://store.continuum.io/cshop/anaconda/.

Canopy is likewise available on all the major platforms, and its web page is at https://www
.enthought.com/products/canopy/.

Playing Games with Python

You have already seen how Python can be used to build basic games in Chapter 4, “Building Desktop Applications,” where you built several variations on the classic tic-tac-toe game. However, most game players today expect something a tad more exciting and dynamic. Python can support many types of games and has comprehensive support for generating random numbers, an essential part of any game, built right into the random module of the standard library. The module has functions that can simulate dice rolls, pick random choices from a selection, or just generate a random number in a variety of formats.

Enriching the Experience with PyGame

If you want a rich game experience using multimedia, the PyGame third-party package is a good place to start. It provides a set of modules that encapsulate the Simple DirectMedia Layer (SDL) that enables programs to access audio, keyboard, joystick, and graphics hardware. It is also cross-platform, so PyGame works with most popular OSes. It is modular so you only need to use the bits that are useful to you, keeping your code small.

Its website at http://www.pygame.org includes many example programs, as well as several tutorials. PyGame has an active community of users for help and support. Several books on building games using Python and PyGame are also available.

Exploring Other Options

PyGame is not the only gaming-focused library. You can use several other options. One example is Pyganim, which is a sprite animation module built on top of the PyGame infrastructure, but easier to use. Albow is a GUI toolkit specifically targeted at building games with PyGame. Many other packages are written on top of PyGame, bearing witness to its popularity as a foundation games framework.

Of course, you don’t have to use PyGame. Other packages access the low-level hardware and libraries. PyOpenGL, as the name implies, provides access to the OpenGL libraries.

One feature of gaming that is also supported is the back-end physics engines needed to model the real-world behavior of physical objects. Python also has tools to support this in the shape of packages like pymunk for 2-D modeling. Panda3d and the Python Computer Graphics Kit (cgkit) provide support for 3-D.

In addition to graphics, most games also need sound. For that you can use built-in modules in the standard library including aifc, wave, and sunau. The winsound module provides low-level access to Windows sound facilities. On top of these low-level libraries, the gaming community has built several packages to assist in generating suitably exciting sounds to accompany your action.

Many other libraries are available, too. In fact, the number of options available for the games programmer can be bewildering. You can find a useful summary at https://wiki.python.org/moin/PythonGameLibraries.

Going to the Movies

Python has a long history in the movie business, with several computer-generated imagery (CGI) studios adopting Python as a scripting engine. Several well-known movies had Python doing some of the production work behind the scenes. To support this, various packages have been developed and made available to a wider audience. Examples include Nuke, Maya, and Blender. Many of these are built on a foundation provided by the cgkit package mentioned in the “Playing Games with Python” section earlier in this chapter. This means that you have lots of scope for using Python while creating and editing your next video masterpiece.

The Computer Graphics Kit

The cgkit package provides a set of low-level types and operations needed to create 3-D scenes. It also provides a rendering engine, although the results can be displayed in other rendering engines if required. cgkit includes bindings to the Pixar RenderMan API. cgkit also includes the Maya plug-ins that enable Maya (see the next section) to interact with Python and vice versa.

Tutorials and reference documentation are available, although they do expect a basic knowledge of 3-D computer graphics principles such as would be acquired by using 3-D modeling applications.

Version 2 of the cgkit was released in early 2013. cgkit is available for Python versions 2 and 3. The package is stable with very little new development underway.

The homepage for the cgkit is at http://cgkit.sourceforge.net/introduction.html.

Modeling and Animation

Many tools are available for digital compositing of video images. Those discussed here are a representative selection, and all include some degree of Python integration.

The NUKE family of products is aimed squarely at the professional end of the video graphics market. It is a commercial compositing tool that integrates with Python but has a price to match its target audience, although a free trial is available as is a limited-functionality Personal Learning Edition for non-commercial use. You can find the NUKE homepage at http://www.thefoundry
.co.uk/products/nuke-product-family/. NUKE uses Python 2.7.

Maya is another 3-D animation and compositing tool. It, too, is a commercial product, competing with NUKE, and also offers a free trial. It can be scripted using Python, and you can incorporate Maya animations into your Python programs. The Maya/Python integration is part of the cgkit bundle described in the previous section. You can find the Maya homepage at http://www
.autodesk.co.uk/products/autodesk-maya/overview.

Blender is yet another animation and compositing package, but it is open source and therefore free, making it much more accessible to the consumer market. It, too, uses Python for its scripting engine. The homepage is at http://www.blender.org/features/. Blender uses Python 3.

Photo Processing

For processing photographic images, many of the solutions discussed in the “Drawing Pictures with Python” section at the start of the chapter apply. Pillow and ImageMagick are both effective tools for manipulating photographic images, capable of cropping, changing exposure, and so on. In addition, the SciPy bundle of packages includes features for processing images with features such as Gaussian blur.

You can also find many special-purpose modules in PyPI to perform specific tasks such as resizing or cropping images. There is a module, psd-tools, for reading Adobe Photoshop .psd files. For processing the EXIF metadata there is, for example, the pyexif module that uses the exiftool command-line application under the covers.

The final category of photo tools is that of online media managers. Modules enable you to transfer photos to and from various sites such as Picasa, Flickr, Facebook, and Twitter. An example of this category is picasa-downloader. Unfortunately, many of these are still only available for Python v2, and most use the Pillow or ImageMagick tools under the covers anyway.

Working with Audio

You’ve already heard about the built-in modules in the standard library: aifc, wave, sunau, and the winsound. These are just as applicable for non-gaming sound applications.

SciPy and its various packages can also be used to process sound files, especially in conjunction with some of the SciKit add-ons. This is especially useful for analyzing sound content or plotting signal waveforms.

There is a useful Python wiki-page listing many sound- and music-oriented projects at https://wiki.python.org/moin/PythonInMusic.

Integrating with Other Languages

The normal Python distribution that you have been using until now is written and built using the C language and is often referred to as CPython. There are other implementations of the Python language in other languages. These non-C based interpreters facilitate integration of the other language with Python. Two of the best known of these alternate Python versions are Jython, written in Java, and IronPython, an implementation for Microsoft’s .NET environment. A third alternative is Cython, which is not strictly an implementation of Python but a very closely related subset that can be compiled into C to provide very fast performance while still providing the speed of development of a Python-like language. Finally, it is possible to access Tcl/Tk code from within the Tkinter package.

Jython

The Java implementation of Python offers many advantages to Java programmers looking for an interactive environment in which to test their Java classes or to build prototype solutions that can, if necessary, be converted to full Java later.

The distribution includes both an interpreter and a compiler. The interpreter comes with the familiar interactive prompt, as well as the ability to run scripts directly. In addition to importing Python modules (including many of the regular Python standard library modules), Jython can import Java libraries, making the classes available to the Python interpreter as if they were regular Python classes. This makes it possible to exercise and test new Java classes interactively at the Jython prompt. Jython also enables dynamic prototyping of solutions by mixing Java and Python code together. The interpreter can also be used to run script files with all of these same features for bigger projects or prototypes.

The compiler takes Jython code (either pure Python or a mixture of Java classes and Python code) and compiles it into a .java file. This is a powerful tool for prototyping new classes because they can be developed and written in Python, compiled, and included in Java code. Once proven, the Python version can be seamlessly replaced with a pure Java version.

The downside of Jython is that it tends to produce slower code than pure Java and is also more memory hungry. This is largely due to the fact that the compiler effectively embeds a Python interpreter in the output files.

At the time of writing, Jython is at version 2.7, although work is underway to migrate to version 3.

IronPython

IronPython is a version of Python written for the Microsoft .NET framework. .NET is not a single language system; rather, it depends on a common bytecode to which several languages can be compiled. The modules so produced can then be shared between languages. Thus, code written in IronPython can import modules written in C#, C++, Visual Basic, and several other .NET-compatible languages. Similarly, IronPython modules can be imported by any of those other .NET languages. IronPython is an extremely appealing prospect for developers working on the .NET platform.

Better still is the fact that an open source variant of .NET called Mono has been produced that can run under Linux and Mac OS X and many others, including mainframe computers and games consoles. This is achieved while maintaining binary-level compatibility with the Microsoft .NET implementation. (At the other end of the spectrum, a slightly limited version, called Mono Touch, runs on iOS and Android for building smartphone apps.) As .NET becomes the de facto standard for building applications on Microsoft Windows, the availability of Python within that framework is a major boon for Python programmers.

The IronPython implementation supports most of the standard Python library as well as the .NET module system. Modules in .NET are called assemblies, but they are imported into IronPython in exactly the same way that ordinary Python modules are imported. Some issues exist due to the dynamic typing used by Python and the .NET type system, which is more static in nature. However, once understood these can be worked around using some helper features built into IronPython. Full documentation is provided on the IronPython documentation site at http://ironpython.net/documentation/.

At the time of writing, IronPython was compatible with Python version 2.7, and a project was underway to develop a Python 3 version. A set of tools is available to enable IronPython development within the Microsoft VisualStudio IDE, which is the default IDE for .NET development.

Cython

Cython is significantly different from the other language integration options discussed here. It is, in effect, a separate language from Python but is highly compatible with it, describing itself as a super-set of Python. This means Python programmers can easily learn Cython and take advantage of its special features.

So what are these features that would make you want to use Cython? In short, speed. Cython is a compiler that produces C code that, in turn, can be compiled to native machine code and thereby has the potential to run much faster than its Python equivalent. This compiled code can then be imported back into regular Python just like any other module to provide the best of both worlds—easy Python development combined with C-level speed of execution.

The Cython extensions are mainly geared around interacting with native C code, and in this regard are similar to the helper features of the ctypes module discussed in the section “Accessing Native APIs with ctypes and pywin32,” back in Chapter 2.

Cython is not the only Python-to-C translator, but it is probably the easiest to use for the average Python programmer. You can find the Cython website at http://cython.org/.

Tcl/Tk

The tkinter and tix GUI modules are built on top of the Tcl/Tk and Tix toolkits. As such, they have the ability to execute Tcl code from within Python by using a method of the embedded tk object: self.tk.call().

This method is the key to how the tix module is built. If you look at the code in tix.py, you find many method definitions that look like this example from the Notebook class:

  def raised(self):
 	return self.tk.call(self._w, 'raised')

As you can see, the method is simply a wrapper for a call to the underlying Tix widget (self._w). If you are familiar with Tcl/Tk and Tix, you can fairly easily extend the existing Python widgets to utilize some Tcl features that are not otherwise available. An alternative method to call() is eval(), which evaluates an input string as a Tcl expression.

But the integration does not need to stop with GUI widget access. You can pass arbitrary Tcl code to the eval() method and have it executed by the embedded Tcl interpreter. This could include importing Tcl modules that provide features that Python does not. Of course, you need to know Tcl to use this effectively! Here is the basic “Hello World” script that can be run in any command-line console using the standard output stream:

  >>> import tkinter
  >>> tcl = tkinter.Tcl()
  >>> tcl.eval('''
  ... puts "Hello world"
  ... ''')
  Hello world
  ''
  >>>

The section inside triple quotes can be any Tcl script you wish.

Getting Physical

There has been an upsurge of hobbyists interested in programming physical devices in recent years. Low-cost, highly flexible products such as the Arduino microcontroller card and the RaspberryPi single board computer (SBC) have become available at very low cost. Python is well suited to programming such devices, and several libraries exist to assist in the process. Indeed, Python is one of the recommended languages for the RaspberryPi.

It is not necessary to spend money on hardware to connect Python to the outside world. Most computers still have a serial port running the RS232 protocol, and it is possible to connect that to various peripheral devices and access them using Python. Similarly, the MIDI interface can be used to access musical instruments, and even the ubiquitous USB interface can be manipulated with the right modules in place.

As is the case in any kind of integration, you need to understand both ends of the connection. If you are not familiar with the physical devices to which you want to connect, you will struggle. The first step in integrating Python with anything is to first find out how the target device interacts with the world. Only then will the libraries discussed in the following sections become useful to you.

Introducing Serial Options

You can access the RS232 communications port on a computer (or indeed any other serial port, including old-style PS-2 mice or pens) using the serial module made available by the PySerial project, which has its homepage at http://pyserial.sourceforge.net/. Comprehensive documentation and examples are included.

The module provides support for several OSes including Windows and Linux, as well as Jython and IronPython. It can be installed from PyPI, or as a Linux package in most distributions, or, for Windows, as a binary installer. It works with Python versions 2 and 3.

The PyUSB library is available for USB access. It is written in Python using ctypes under the covers to access the low-level code. You can find PyUSB at https://github.com/walac/pyusb. The website is a tad sparse, but it includes a tutorial with several examples. It is assumed that you already understand how USB interfaces function.

PyUSB is available from the GitHub repository, but is easier to install via the PyPI.

Programming the RaspberryPi

The RaspberryPi is a single board computer, about the size of a credit card, and sold at very low cost. It was originally intended to encourage interest in computing technology and programming. It has been hugely successful not only in its original objective, but also as a hobbyist tool. Many owners have capitalized on its small size and built the computer into small physical enclosures for applications such as weather monitoring, security systems, robotics experiments, and vehicle control.

The RaspberryPi runs various distributions of Linux, the default being Raspbian Linux. It includes a full Python distribution, and Python is the recommended language for user programming. Used in its basic configuration, it can be treated like any other computer, and indeed, if mounted in a suitable case with monitor, mouse, and keyboard connected, it functions just like any other Linux-powered PC, albeit with modest computing resources. If used as a control system for a custom project, the programming environment may well involve using some of the peripheral access techniques, especially PyUSB for access to the built-in USB ports.

You can find the RaspberryPi homepage at http://www.raspberrypi.org/.

There is a free community online magazine called “The MagPi,” where enthusiasts can share experiences, tips, and projects. In addition, several books on the subject have been published, as well as regular conferences and local user groups.

Talking to the Arduino

The Arduino products are, in many ways, complementary to the RaspberryPi. Where the RaspberryPi aims to teach about computing and programming, Arduino is more directly aimed at the electronics enthusiast. It enables experimentation with interfaces, both analog and digital. The various microcontroller circuit boards in the Arduino range come in various configurations of inputs and outputs. Typically they include a USB interface, several analog input pins, as well as some digital I/O pins, thus enabling the user to attach various external devices.

Accessories are also available to extend the types of devices that can be connected. Also included is a code library, called Wiring, written in C, that provides access to the various ports. There is an IDE that helps users write their code and provides a single-click mechanism to upload it to the board. The Arduino homepage is at http://www.arduino.cc/.

Although the Arduino processor normally has to have a binary application downloaded, it can also be controlled by connecting the Arduino to a controlling computer, such as a RasberryPi. This enables Python to be used to send instructions to the Arduino via the USB or serial ports. There is a library to facilitate this called pyfirmata, which is available from PyPI. There is a web page that discusses this further, with several examples of what is possible, at http://playground.arduino
.cc/interfacing/python.

Exploring Other Options

The popularity of the RaspberryPi and Arduino projects has spawned several competing products. Many of these are simply low-cost clones of the other projects, especially the Arduino, but others are genuine alternatives with a slightly different set of objectives or presentation of the ideas; for example, creating the smallest board possible. In most cases Python can be used to access the boards over a serial link or even by a network connection using standard Python modules.

Your favorite search engine should find many candidates. You should check the nature of the interfaces provided and how the programming is done. Some Arduino clones require you to program the chip on a genuine Arduino and then transfer it to the clone board for installation in the final device.

Building Python

One area of special interest for Python programmers is Python itself. Python is a very open architecture with many features that enable the programmer to inspect the internal workings of the interpreter, as well as the data structures within the program. Being an open source project means that the development of the language is a community affair and everyone who uses Python can share in its development. If there is a feature of Python that you think is broken or can be improved, there is a process for fixing it. If there is a feature you’d like to add, there is a process for adding it.

If you want to get involved in Python development, whether because you have a personal itch to scratch or just as a way of giving back to the community that gave you Python, you have several ways to get started. You can find a good introduction to all of the options at https://docs
.python.org/devguide/index.html.

Fixing Bugs

Perhaps the most obvious place to start getting involved with Python is in fixing bugs. The very act of reporting bugs is a useful contribution, but supplying fixes is even better. There is an official bug tracker application, and before submitting a bug you should check whether it has already been reported, and what, if anything, is being done to fix it. If it’s a new bug, you can fill in a report (and optionally supply a fix).

Once a bug has been reported, the tracker supports a conversational model whereby users can suggest fixes, comment on patches, and so on. The bug tracker homepage is at https://docs
.python.org/3/bugs.html.

Documenting

In most open source projects, it is easier to get someone to write code than to get someone to write documentation. Python is no exception. Although the official documentation is quite good for an open source project, it still has many areas that are sketchy at best and in some cases completely lacking. (You saw an example of that with the tix module used in Chapter 4. Several tix widgets are available that are not described in the official documentation.)

Volunteering to document some of these darker corners of Python is a worthwhile endeavor, and one that provides a relatively gentle introduction to open source community. Documentation issues are reported on the standard Python bug tracker that you can use to submit bug reports and suggested fixes. If you want to get more fully involved, you can subscribe to the [email protected] mailing list. You can find more specific details on the process on this website: https://docs
.python.org/devguide/docquality.html.

The documentation is actually generated using a purpose-built document processor called Sphinx. The Sphinx content is created in reStructuredText (reST), a lightweight markup system similar to that used on many wiki-pages. (The Python Docutils project supplies the underlying toolset for processing reST files.) The Sphinx website is at http://sphinx-doc.org/index.html.

Testing

Unless you plan on getting involved in core Python development, the best way to contribute to testing is to download and use the early beta releases. You can then report bugs found using the bug tracker as usual.

Adding Features

If your itch is not due to a bug but due to a missing or incomplete feature, either in the Python language itself or in a module, you should consider submitting the idea to the python-ideas mailing list. You can sign up to the list at https://mail.python.org/mailman/listinfo/python-ideas. The list enables your idea to be discussed by the wider community. If yours is considered a good idea, you may be invited to submit a Python Enhancement Proposal, or PEP.

It must be said you have far more chance of getting a module change approved than a core language feature, but language changes do happen, and if it’s a good idea it is worth trying.

Attending Conferences

Another, altogether easier, way of getting involved in the Python community is to attend the various local user groups and conferences held every year. These afford opportunities to learn about Python and its infrastructure, hear about successful projects based on Python, and, of course, to present your own experiences with Python.

Several major international Python conferences are held annually, as well as some smaller events either focused on a local area or a special interest group. Details of these are often announced online, in the various Python mailing lists, and a list is maintained here: https://wiki.python
.org/moin/PythonEvents.

Summary

In this chapter you looked at the wider world of Python. In particular, you considered the specialist areas that are not covered by the standard library but have extensive support from Python in the wider community.

You’ve seen how graphics can be produced and manipulated using core Python modules as well as various third-party libraries, especially Pillow and ImageMagick.

Many third-party libraries are available in the field of science, and the foundation for many of these is the SciPy bundle of packages and tools. Distributions like Anaconda make installation of these packages much easier.

For games programming you discovered that the PyGame package provides low-level graphics and multimedia access. Third-party computational engines also help you to develop realistic game play.

The video processing and movie world is well served with commercial products using Python. Blender provides an open source option for you to create your own video masterpiece.

Python integration with other languages is available in various forms. Jython provides two-way integration with Java, and IronPython integrates with Microsoft’s .NET infrastructure as well as the open source Mono implementation.

You can get Python talking to the physical world via the serial and USB ports either directly or via low-cost microcontroller boards like the Arduino. When combined with small single board computers like the RasberryPi, you can build compact but powerful projects.

Finally, you saw how you can get involved in the Python development activities. Whether it be by fixing documentation or code or simply by getting involved in the discussions on the community mailing lists and forums, you can have a share in improving Python.

EXERCISES

  1. In the section on SciPy you discovered that there were many more areas of science with Python libraries available. Pick some areas of science and see what support you can find in the Python community. (Hint: The Anaconda and Enthought Canopy distributions contain much more than the basic SciPy bundle of packages.)

  2. In the “Going to the Movies” section you saw that commercial (and open source) applications can be scripted using Python as a macro language. This is not the only area where this is possible. Research the use of Python as a macro language and produce a list of some popular applications that can be scripted using Python.

  3. Python is used in many other niche areas. Try to identify an area that you have an interest in and find out what support might be available. (Hint: PyPI has a search facility.)

arrow2  WHAT YOU LEARNED IN THIS CHAPTER

KEY CONCEPT DESCRIPTION
Turtle graphics A graphics technique whereby drawings are produced by moving a virtual turtle around the drawing surface. The turtle has a pen attached that can be raised/lowered, its color changed, and its thickness varied. The turtle can move in a specified direction for a specified distance.
Canvas widget A low-level drawing widget that supports basic operations such as drawing lines, polygons, and circles. It can also handle text and image files.
Pillow An extension of the older Python Imaging Library (PIL). Pillow is used to convert and manipulate image files.
ImageMagick A command-line toolset for manipulating and converting image files. Many libraries and modules encapsulate ImageMagick’s capabilities.
SciPy A set of tools for doing scientific and numerical analysis in Python. There is an eponymous SciPy package that provides general-purpose scientific tools, but the term encompasses a group of other related packages as well.
SciKit A collection of scientific and numerical processing modules that generally relies upon SciPy but is not part of the formal SciPy bundle of packages. SciKit includes packages covering many fields of scientific investigation.
NumPy A package within SciPy that provides advanced numerical processing tools.
SymPy A package within SciPy for doing symbolic math.
Pandas A package within SciPy that provides a statistical data analysis toolset.
IPython Interactive Python. A sophisticated replacement for the standard Python command prompt. It includes a powerful notebook mechanism whereby individual sessions can be saved and restored.
bioPython A suite of tools used for doing bioscience.
ArcGIS A standard set of tools for doing geographical information processing. ArcPy provides a Python wrapper around the ArcGIS toolset.
NLTK A set of tools for analyzing natural language text.
PyGame A toolset for creating games in Python. It includes powerful support for multimedia programming, including graphics and audio as well as interaction with keyboard, mice, joysticks, and other peripherals.
CGI Computer-generated imagery (CGI) has become a widely used tool in the motion picture industry. Python is used in many of the tools used to create and manage CGI images.
CPython The standard implementation of the Python language interpreter, written in C.
Jython An alternative implementation of the Python interpreter written in Java. This enables Jython to import Java classes in addition to Python modules. Jython also features a compiler for turning Python code into Java bytecode that can in turn be imported by Java programs.
Cython A superset of the Python language that can be compiled into C and from there into Python modules with enhanced performance.
IronPython An implementation of Python for Microsoft’s .NET platform. This makes it compatible with any other modules written for .NET (or the open source clone, Mono).
PySerial A project that provides tools whereby a Python programmer can access the serial ports of a computer.
PyUSB A project that provides tools whereby a Python programmer can access the USB devices on a computer.
Sphinx A purpose-built documentation tool used to create the Python documentation. The content format is reStructuredText, which is a kind of markup language supported by the Python Docutils project and toolset.
PEP A Python Enhancement Proposal. This is the formal mechanism for getting changes into a Python release.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset