Text
stringlengths
1
9.41k
If you work in such a domain, you can still use Python—simply split off the parts of the application that require optimal speed into compiled extensions, and link those into your system for use in Python scripts. We won’t talk about extensions much in this text, but this is really just an instance of the Python-as-con...
A prime example of this dual language strategy is the NumPy numeric programming extension for Python; by combining compiled and optimized numeric extension libraries with the Python language, NumPy turns Python into a numeric programming tool that is efficient and easy to use. You may never need to code such extensions...
This estimate is based on various statistics, like download rates and developer surveys. Because Python is open source, a more exact count is difficult— there are no license registrations to tally.
Moreover, Python is automatically included **|** ----- with Linux distributions, Macintosh computers, and some products and hardware, further clouding the user-base picture. In general, though, Python enjoys a large user base and a very active developer community.
Because Python has been around for some 19 years and has been widely used, it is also very stable and robust.
Besides being employed by individual users, Python is also being applied in real revenue-generating products by real companies.
For instance: - Google makes extensive use of Python in its web search systems, and employs Python’s creator. - The YouTube video sharing service is largely written in Python. - The popular BitTorrent peer-to-peer file sharing system is a Python program. - Google’s popular App Engine web development framework u...
Probably the only common thread amongst the companies using Python today is that Python is used all over the map, in terms of application domains.
Its general-purpose nature makes it applicable to almost all fields, not just one.
In fact, it’s safe to say that virtually every substantial organization writing software is using Python, whether for short-term tactical tasks, such as testing and administration, or for longterm strategic product development.
Python has proven to work well in both modes. **|** ----- For more details on companies using Python today, see Python’s website at http://www _.python.org._ ###### What Can I Do with Python? In addition to being a well-designed programming language, Python is useful for accomplishing real-world tasks—the sorts o...
In fact, as a general-purpose language, Python’s roles are virtually unlimited: you can use it for everything from website development and gaming to robotics and spacecraft control. However, the most common Python roles currently seem to fall into a few broad categories.
The next few sections describe some of Python’s most common applications today, as well as tools used in each domain.
We won’t be able to explore the tools mentioned here in any depth—if you are interested in any of these topics, see the Python website or other resources for more details. ###### Systems Programming Python’s built-in interfaces to operating-system services make it ideal for writing portable, maintainable system-admin...
Python programs can search files and directory trees, launch other programs, do_ parallel processing with processes and threads, and so on. Python’s standard library comes with POSIX bindings and support for all the usual OS tools: environment variables, files, sockets, pipes, processes, multiple threads, regular expr...
In addition, the bulk of Python’s system interfaces are designed to be portable; for example, a script that copies directory trees typically runs unchanged on all major Python platforms.
The Stackless _Python system, used by EVE Online, also offers advanced solutions to multiprocessing_ requirements. ###### GUIs Python’s simplicity and rapid turnaround also make it a good match for graphical user interface programming.
Python comes with a standard object-oriented interface to the Tk GUI API called tkinter (Tkinter in 2.6) that allows Python programs to implement portable GUIs with a native look and feel.
Python/tkinter GUIs run unchanged on Microsoft Windows, X Windows (on Unix and Linux), and the Mac OS (both Classic and OS X).
A free extension package, _PMW, adds advanced widgets to the tkinter_ toolkit.
In addition, the wxPython GUI API, based on a C++ library, offers an alternative toolkit for constructing portable GUIs in Python. **|** ----- Higher-level toolkits such as PythonCard and Dabo are built on top of base APIs such as wxPython and tkinter.
With the proper library, you can also use GUI support in other toolkits in Python, such as _Qt with PyQt,_ _GTK with PyGTK,_ _MFC with_ PyWin32, .NET with IronPython, and Swing with Jython (the Java version of Python, described in Chapter 2) or JPype.
For applications that run in web browsers or have simple interface requirements, both Jython and Python web frameworks and serverside CGI scripts, described in the next section, provide additional user interface options. ###### Internet Scripting Python comes with standard Internet modules that allow Python programs ...
Scripts can communicate over sockets; extract form information sent to server-side CGI scripts; transfer files by FTP; parse, generate, and analyze XML files; send, receive, compose, and parse email; fetch web pages by URLs; parse the HTML and XML of fetched web pages; communicate over XML-RPC, SOAP, and Telnet; and mo...
Python’s libraries make these tasks remarkably simple. In addition, a large collection of third-party tools are available on the Web for doing Internet programming in Python.
For instance, the HTMLGen system generates HTML files from Python class-based descriptions, the mod_python package runs Python efficiently within the Apache web server and supports server-side templating with its Python Server Pages, and the Jython system provides for seamless Python/Java integration and supports codin...
Many of these include features such as object-relational mappers, a Model/View/Controller architecture, server-side scripting and templating, and AJAX support, to provide complete and enterprise-level web development solutions. ###### Component Integration We discussed the component integration role earlier when desc...
Python’s ability to be extended by and embedded in C and C++ systems makes it useful as a flexible glue language for scripting the behavior of other systems and components.
For instance, integrating a C library into Python enables Python to test and launch the library’s components, and embedding Python in a product enables onsite customizations to be coded without having to recompile the entire product (or ship its source code at all). **|** ----- Tools such as the _SWIG and_ _SIP cod...
Larger frameworks, such as Python’s COM support on Windows, the Jython Java-based implementation, the IronPython .NET-based implementation, and various CORBA toolkits for Python, provide alternative ways to script components.
On Windows, for example, Python scripts can use frameworks to script Word and Excel. ###### Database Programming For traditional database demands, there are Python interfaces to all commonly used relational database systems—Sybase, Oracle, Informix, ODBC, MySQL, PostgreSQL, SQLite, and more.
The Python world has also defined a portable database API for accessing SQL database systems from Python scripts, which looks the same on a variety of underlying database systems.
For instance, because the vendor interfaces implement the portable API, a script written to work with the free MySQL system will work largely unchanged on other systems (such as Oracle); all you have to do is replace the underlying vendor interface. Python’s standard pickle module provides a simple object persistence ...
Because of this, it’s possible to prototype systems in Python initially, and then move selected components to a compiled language such as C or C++ for delivery.
Unlike some prototyping tools, Python doesn’t require a complete rewrite once the prototype has solidified.
Parts of the system that don’t require the efficiency of a language such as C++ can remain coded in Python for ease of maintenance and use. ###### Numeric and Scientific Programming The NumPy numeric programming extension for Python mentioned earlier includes such advanced tools as an array object, interfaces to stan...
By integrating Python with numeric routines coded in a compiled language for speed, NumPy turns Python into a sophisticated yet easy-to-use numeric programming tool that can often replace existing code written in traditional compiled languages such as FORTRAN or C++.
Additional numeric tools for Python support **|** ----- animation, 3D visualization, parallel processing, and so on.
The popular SciPy and _ScientificPython extensions, for example, provide additional libraries of scientific pro-_ gramming tools and use NumPy code. ###### Gaming, Images, Serial Ports, XML, Robots, and More Python is commonly applied in more domains than can be mentioned here.
For example, you can do: - Game programming and multimedia in Python with the pygame system - Serial port communication on Windows, Linux, and more with the _PySerial_ extension - Image processing with PIL, PyOpenGL, Blender, Maya, and others - Robot control programming with the PyRo toolkit - XML parsing wit...
You’ll find support for many such fields at the PyPI websites, and via web searches (search Google or http://www.python _.org for links)._ Many of these specific domains are largely just instances of Python’s component integration role in action again.
Adding it as a frontend to libraries of components written in a compiled language such as C makes Python useful for scripting in a wide variety of domains.
As a general-purpose language that supports integration, Python is widely applicable. ###### How Is Python Supported? As a popular open source system, Python enjoys a large and active development community that responds to issues and develops enhancements with a speed that many commercial software developers would fi...
Changes follow a formal PEP (Python Enhancement Proposal) protocol and must be accompanied by extensions to Python’s extensive regression testing system.
In fact, modifying Python today is roughly as involved as changing commercial software—a far cry from Python’s early days, when an email to its creator would suffice, but a good thing given its current large user base. **|** ----- The PSF (Python Software Foundation), a formal nonprofit group, organizes conferences...
Numerous Python conferences are held around the world; O’Reilly’s OSCON and the PSF’s PyCon are the largest.
The former of these addresses multiple open source projects, and the latter is a Python-only event that has experienced strong growth in recent years.
Attendance at PyCon 2008 nearly doubled from the prior year, growing from 586 attendees in 2007 to over 1,000 in 2008.
This was on the heels of a 40% attendance increase in 2007, from 410 in 2006. PyCon 2009 had 943 attendees, a slight decrease from 2008, but a still very strong showing during a global recession. ###### What Are Python’s Technical Strengths? Naturally, this is a developer’s question.
If you don’t already have a programming background, the language in the next few sections may be a bit baffling—don’t worry, we’ll explore all of these terms in more detail as we proceed through this book.
For developers, though, here is a quick introduction to some of Python’s top technical features. ###### It’s Object-Oriented Python is an object-oriented language, from the ground up.
Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance; yet, in the context of Python’s simple syntax and typing, OOP is remarkably easy to apply.
In fact, if you don’t understand these terms, you’ll find they are much easier to learn with Python than with just about any other OOP language available. Besides serving as a powerful code structuring and reuse device, Python’s OOP nature makes it ideal as a scripting tool for object-oriented systems languages such a...
For example, with the appropriate glue code, Python programs can subclass (specialize) classes implemented in C++, Java, and C#. Of equal significance, OOP is an option in Python; you can go far without having to become an object guru all at once.
Much like C++, Python supports both procedural and object-oriented programming modes. Its object-oriented tools can be applied if and when constraints allow.
This is especially useful in tactical development modes, which preclude design phases. ###### It’s Free Python is completely free to use and distribute.
As with other open source software, such as Tcl, Perl, Linux, and Apache, you can fetch the entire Python system’s source code for free on the Internet.
There are no restrictions on copying it, embedding it in your systems, or shipping it with your products.
In fact, you can even sell Python’s source code, if you are so inclined. **|** ----- But don’t get the wrong idea: “free” doesn’t mean “unsupported.” On the contrary, the Python online community responds to user queries with a speed that most commercial software help desks would do well to try to emulate.
Moreover, because Python comes with complete source code, it empowers developers, leading to the creation of a large team of implementation experts.
Although studying or changing a programming language’s implementation isn’t everyone’s idea of fun, it’s comforting to know that you can do so if you need to.
You’re not dependent on the whims of a commercial vendor; the ultimate documentation source is at your disposal. As mentioned earlier, Python development is performed by a community that largely coordinates its efforts over the Internet.
It consists of Python’s creator—Guido van _Rossum, the officially anointed Benevolent Dictator for Life (BDFL) of Python—plus a_ supporting cast of thousands.
Language changes must follow a formal enhancement procedure and be scrutinized by both other developers and the BDFL.
Happily, this tends to make Python more conservative with changes than some other languages. ###### It’s Portable The standard implementation of Python is written in portable ANSI C, and it compiles and runs on virtually every major platform currently in use.
For example, Python programs run today on everything from PDAs to supercomputers.
As a partial list, Python is available on: - Linux and Unix systems - Microsoft Windows and DOS (all modern flavors) - Mac OS (both OS X and Classic) - BeOS, OS/2, VMS, and QNX - Real-time systems such as VxWorks - Cray supercomputers and IBM mainframes - PDAs running Palm OS, PocketPC, and Linux - Cell...
Further, Python programs are automatically compiled to portable byte code, which runs the same on any platform with a compatible version of Python installed (more on this in the next chapter). **|** ----- What that means is that Python programs using the core language and standard libraries run the same on Linux, W...
As mentioned earlier, Python also includes an interface to the Tk GUI toolkit called tkinter (Tkinter in 2.6), which allows Python programs to implement full-featured graphical user interfaces that run on all major GUI platforms without program changes. ###### It’s Powerful From a features perspective, Python is some...
Its toolset places it between traditional scripting languages (such as Tcl, Scheme, and Perl) and systems development languages (such as C, C++, and Java).
Python provides all the simplicity and ease of use of a scripting language, along with more advanced software-engineering tools typically found in compiled languages.
Unlike some scripting languages, this combination makes Python useful for large-scale development projects.
As a preview, here are some of the main things you’ll find in Python’s toolbox: _Dynamic typing_ Python keeps track of the kinds of objects your program uses when it runs; it doesn’t require complicated type and size declarations in your code.
In fact, as you’ll see in Chapter 6, there is no such thing as a type or variable declaration anywhere in Python.
Because Python code does not constrain data types, it is also usually automatically applicable to a whole range of objects. _Automatic memory management_ Python automatically allocates objects and reclaims (“garbage collects”) them when they are no longer used, and most can grow and shrink on demand.
As you’ll learn, Python keeps track of low-level memory details so you don’t have to. _Programming-in-the-large support_ For building larger systems, Python includes tools such as modules, classes, and exceptions.
These tools allow you to organize systems into components, use OOP to reuse and customize code, and handle events and errors gracefully. _Built-in object types_ Python provides commonly used data structures such as lists, dictionaries, and strings as intrinsic parts of the language; as you’ll see, they’re both flexible...
For instance, built-in objects can grow and shrink on demand, can be arbitrarily nested to represent complex information, and more. _Built-in tools_ To process all those object types, Python comes with powerful and standard operations, including concatenation (joining collections), slicing (extracting sections), sortin...
Once you learn the language itself, Python’s library tools are where much of the application-level action occurs. _Third-party utilities_ Because Python is open source, developers are encouraged to contribute precoded tools that support tasks beyond those supported by its built-ins; on the Web, you’ll find free support...
For example, Python’s C API lets C programs call and be called by Python programs flexibly.
That means you can add functionality to the Python system as needed, and use Python programs within other environments or systems. Mixing Python with libraries coded in languages such as C or C++, for instance, makes it an easy-to-use frontend language and customization tool.
As mentioned earlier, this also makes Python good at rapid prototyping; systems may be implemented in Python first, to leverage its speed of development, and later moved to C for delivery, one piece at a time, according to performance demands. ###### It’s Easy to Use To run a Python program, you simply type it and ru...
There are no intermediate compile and link steps, like there are for languages such as C or C++.
Python executes programs immediately, which makes for an interactive programming experience and rapid turnaround after program changes—in many cases, you can witness the effect of a program change as fast as you can type it. Of course, development cycle turnaround is only one aspect of Python’s ease of use.
It also provides a deliberately simple syntax and powerful built-in tools.
In fact, some have gone so far as to call Python “executable pseudocode.” Because it eliminates much of the complexity in other tools, Python programs are simpler, smaller, and more flexible than equivalent programs in languages like C, C++, and Java. **|** ----- ###### It’s Easy to Learn This brings us to a key p...
In fact, you can expect to be coding significant Python programs in a matter of days (or perhaps in just hours, if you’re already an experienced programmer).
That’s good news for professional developers seeking to learn the language to use on the job, as well as for end users of systems that expose a Python layer for customization or control. Today, many systems rely on the fact that end users can quickly learn enough Python to tailor their Python customizations’ code onsi...
Although Python does have advanced programming tools, its core language will still seem simple to beginners and gurus alike. ###### It’s Named After Monty Python OK, this isn’t quite a technical strength, but it does seem to be a surprisingly well-kept secret that I wish to expose up front.
Despite all the reptile icons in the Python world, the truth is that Python creator Guido van Rossum named it after the BBC comedy series _Monty Python’s Flying Circus.
He is a big fan of Monty Python, as are many_ software developers (indeed, there seems to almost be a symmetry between the two fields). This legacy inevitably adds a humorous quality to Python code examples.
For instance, the traditional “foo” and “bar” for generic variable names become “spam” and “eggs” in the Python world.
The occasional “Brian,” “ni,” and “shrubbery” likewise owe their appearances to this namesake.
It even impacts the Python community at large: talks at Python conferences are regularly billed as “The Spanish Inquisition.” All of this is, of course, very funny if you are familiar with the show, but less so otherwise.
You don’t need to be familiar with the series to make sense of examples that borrow references to Monty Python (including many you will see in this book), but at least you now know their root. ###### How Does Python Stack Up to Language X? Finally, to place it in the context of what you may already know, people somet...
We talked about performance earlier, so here we’ll focus on functionality.
While other languages are also useful tools to know and use, many people find that Python: **|** ----- - Is more powerful than Tcl.
Python’s support for “programming in the large” makes it applicable to the development of larger systems. - Has a cleaner syntax and simpler design than Perl, which makes it more readable and maintainable and helps reduce program bugs. - Is simpler and easier to use than Java.
Python is a scripting language, but Java inherits much of the complexity and syntax of systems languages such as C++. - Is simpler and easier to use than C++, but it doesn’t often compete with C++; as a scripting language, Python typically serves different roles. - Is both more powerful and more cross-platform than...
Its open source nature also means it is not controlled by a single company. - Is more readable and general-purpose than PHP.