Magic attributes

As well as magic methods, many Python objects have magic attributes. These are attributes whose names start and end with double underscores and that have a special meaning to the Python interpreter. Table B.12 lists the most common of these attributes along with their description. Most Python objects have a doc attribute that holds the docstring for the object. If there is no docstring, this attribute will be None. Docstrings are displayed by the help function and used by automated...

Chiron and the dynamic experience

Chiron packages IronPython Silverlight applications as the browser requests them. This means that you can have a truly dynamic experience developing Silverlight applications with IronPython. Simply edit the Python file with a text editor or IDE and refresh the browser, and you immediately see the changes in front of you. The Silverlight Dynamic Languages SDK is comprised of the DLR runtime and Iron-Python, IronRuby, and Managed JScript assemblies compiled for Silverlight, along with the...

Dynamic languages on NET and the DLR

For a while, the CLR had the reputation of being a bad platform for dynamic languages. As Jim Hugunin proved with IronPython, this isn't true. One of the reasons that Microsoft took on the IronPython project was to push the development of the CLR to make it an even better platform for hosting multiple languages, particularly dynamic languages. The DLR, which makes several dynamic languages available for .NET, is the concrete result of this work. So, why all the fuss about dynamic languages...

Iron Python for NET programmers

IronPython is a completely new language available to .NET programmers. It opens up new styles of programming and brings the power and flexibility of dynamic programming languages to the .NET platform. Programming techniques such as functional programming and creating classes and functions at runtime are possible with traditional .NET languages like VB.NET and 17 Several of these are provided by FePy, a community distribution of IronPython. See http fepy.sourceforge.net C , but they're a lot...

Modules packages and importing

The last thing you want when programming is to have all your code contained in a single monolithic file. This makes it almost impossible to find anything. Ideally, you want to break your program down into small files containing only closely related classes or functionality. In Python, these are called modules. NOTE A module is a Python source file (a text file) whose name ends with .py. Objects (names) defined in a module can be imported and used elsewhere. They're very different from .NET...

Silverlight a new CLR

At Mix 2007, a conference for web designers and developers, Microsoft surprised just about everyone by announcing both the DLR and Silverlight. Silverlight is something of a breakthrough for Microsoft. Their usual pattern is to release an early version of a project with an exciting-sounding name, and then the final version with an anonymous and boring name.24 This time around they've broken the mold Silverlight was originally codenamed Windows Presentation Foundation Everywhere, or WPF E. WPF...

The Python standard library

Python comes with batteries included. This statement refers to the standard library that accompanies CPython. Many of the modules it provides are pure-Python modules, but some of them are extension modules written in C. 25 Like Epydoc http epydoc.sourceforge.net. NOTE An open source project called Ironclad,26 created by Resolver Systems, reimplements the Python C-API in C with the goal of allowing you to import and use Python C extensions from IronPython. Unfortunately, extension modules...

This chapter covers

Exception handling, list comprehensions, and closures Python project structure modules and packages The Python standard library Programming is a craft. Knowing the rules of a programming language is the science behind the art of creating well-structured and elegant programs. Your programs may be small and functional, or they may be vast cathedrals adorned with the intricate masonry of recursive algorithms and data structures. The joy of programming is found in the process of creation as much...

Translating MSDN documentation into Iron Python

We've done enough talking about using .NET classes it's time to put what you've learned into action. Let's start by creating a simple Windows Forms application, using classes from the System.Windows.Forms assembly. Windows Forms is a rich and mature GUI framework. It contains an extraordinary number of different components for building applications, including sophisticated data-bound controls.4 You can see a small selection of the components available in figure 3.2. Windows Forms controls...

What is the relational model

It's a formal framework for reasoning about databases, devised by Edgar Codd in 1969. It defines databases in terms of relations, which are sets of things that Codd called tuples, although from a Python perspective they're more like dictionaries keyed by strings. All of the tuples in a relation have the same attributes (which are the keys of the dictionaries), and the values of the attributes have to be atomic they can't be lists or tuples themselves. In relational database management systems,...

Silverlight controls

Silverlight ships with a set of standard controls based on WPF and contained in the System.Windows.Controls namespace. Figure 13.3 shows examples of the standard controls. Figure 13.3 Some of the standard Silverlight controls Figure 13.3 Some of the standard Silverlight controls As you might expect from their WPF inheritance, the Silverlight controls are attractive and easy to use. Figure 13.4 shows a TextBox with a Button and a TextBlock. Using and configuring llie controls from code is...

Wiring the commands to the view

You've done this before, and these commands are just as easy to wire up as the previous ones. The nice bonus is that adding these commands to the user interface will make MultiDoc actually usable. To spice up this section, we add a couple of new features an application icon and a new document command. The first is purely cosmetic, but no Windows Forms application would be complete without an application icon to replace the default one When a form is displayed, an icon is displayed in the...

System administration with Python

Every computer user does some system administration, even if it's only maintaining a stable and working system. System administration encompasses everything from keeping a computer operating to maintaining large networks with many computers and servers. Although these are radically different situations, they share some needs and techniques in common. We start our look at administration with an example of simple scripting. For simple tasks, one of Python's great advantages is that it doesn't...

Using the interactive interpreter

When you download IronPython,33 you have two choices. You can download and install the msi installer IronPython 2 only , which includes the Python 2.5 standard library. Alternately, you can download and unpack the binary distribution that comes as a zip file. Whichever route you take, you'll have two executables, ipy.exe and ipyw.exe, which are the equivalents of the Python executables python.exe and pythonw.exe. Both are used to launch Python scripts ipy.exe launches them with a console...

Python datatypes

Python boasts a rich set of built-in datatypes that you can use without importing any modules. Most of these, with the exception of the set that we introduce shortly, have syntax for including them within your code. To understand the examples in this book or to write Python yourself, you're going to need to recognize them. Table 2.1 contains a guide to all the basic datatypes and their syntaxes. Table 2.1 The built-in datatypes and their syntaxes Table 2.1 The built-in datatypes and their...

A new language for NET

An introduction to IronPython Python and dynamic languages on .NET The IronPython interactive interpreter Live object introspection with help and dir The .NET framework was launched in 2000 and has since become a popular platform for object-oriented programming. Its heart and soul is the Common Language Runtime CLR , which is a powerful system including a just-in-time compiler, built-in memory management, and security features. Fortunately, you can write .NET programs that take advantage of...

Dynamic attribute access

Python attribute access uses straightforward syntax, shared with other imperative languages like Java and C . Through properties, you can control what happens when individual attributes are fetched or set but, with Python, you can provide access to arbitrary attributes through the_getattr_method. 8 This page in the Python documentation lists among other things the protocol methods for rich comparison 9 See the following article on rich comparison in CPython and IronPython, which also shows a...

Functional testing

Functional tests, or acceptance tests, are high-level tests of an application from the outside. As much as possible, they should interact with the application in the same way the user does. Where unit tests test the components of your application, functional tests test the interaction of those components they'll often pick up on bugs or problems that unit tests miss. Functional tests can be more than just useful tests, though. In the Extreme Programming XP tradition you know a methodology has...

XPS documents and flow content

Ironpython Wpf

XML Paper Specification XPS is a combination of a document markup language, which is a subset of XAML, and code support for viewing and printing documents. Although you won't read this in the Microsoft documentation, many see XPS as Microsoft's answer to Adobe's Portable Document Format PDF for creating print-ready documents. Fortunately, that debate is irrelevant to us because the classes that provide XPS support are a fantastic way of incorporating documents within WPF applications. One use...

WPF in action

Wxpython Combobox Grid

Although WPF doesn't have as many controls as Windows Forms, it includes standard controls such as check boxes, drop-down lists, and radio buttons. It also includes a range of new controls, both for advanced layout and entirely new user interface components. WPF also covers a wide range of areas beyond traditional user interfaces, including document support and 3D drawing. Even though it doesn't have all the controls that Windows Forms does, it still does an awful lot. It's extremely useful for...

Protocols instead of interfaces

Interfaces are used in C to specify behavior of objects. For example, if a class implements the IDisposable interface, then you can provide a Dispose method to release resources used by your objects. .NET has a whole host of interfaces, and you can create new ones. If a class implements an interface, it provides a static target for the compiler to call whenever an operation provided by that interface is used in your code. NOTE C does have one example of duck typing enumeration with the...

Iron Python and the CLR

So far, we've focused on navigating the .NET framework and the Python language, emphasizing the skills you need to make effective use of IronPython. But not all .NET concepts map easily to Python concepts certain corners of .NET can't be brought directly into Python. This section looks at some of these dusty corners and how they work in IronPython. Most of the things we explore are simple but, even if they're easy, you need to know the trick in order to use them. The first line of IronPython...

Testing with mocks

The point of unit testing is to test as many aspects of your code as possible in isolation from the other parts it's easier to tightly specify the behavior of your code and makes the tests run faster. In practice, life is never quite so neat. Your objects need to interact with other objects, and you need to test that interaction. Several different approaches can minimize the number of live objects your tests need to use the most useful of these approaches is creating mock objects. Working with...

The NET Xml Writer

The .NET XML support is enshrined in the System.Xml assembly. The System.Xml namespace has classes for reading and writing XML. There are further namespaces for working with XML schema, XSLT transformations, and XPath queries. Table 5.2 shows the major XML namespaces in .NET 2.0. Provides support for standards-based XML support. Includes the XmlReader and XmlWriter classes. Support for XML Schemas in the form of schema definition language XSD schemas. Classes to serialize objects in XML form....

Working with Visual Studio or Mono Develop

Net Opensource

Visual Studio Express is free to download and use, but you have to choose which language you'll be developing with. There are separate versions for C , C , VB.NET, and web development, but you can have several different versions of Visual Studio Express installed on the same machine. If you have Visual Studio Professional, then you don't have to make a choice, as it supports all of the standard .NET languages. Figure 14.1 shows a new class library being created with the VB.NET version of Visual...

Listing A Test Case class for Value which tests the specification

See def testConstructorShouldStoreValue self value Value 6 value attribute not set correctly add returned the wrong answer Wrong answer for isEven with an even number Wrong answer for isEven with an odd number The test methods all create an instance of Test methods called our Value class and then test its state or the result of calling methods. The calls to the self. assertEquals assert methods do the actual testing, and j value.value, 6, they all follow a pattern...

Visual Studio Express and ironPython

Microsoft Visual Express Interface

For this example, we use the free version of Visual Studio, Visual Studio Express. Visual Studio Express doesn't have IronPython integration, so we have to find another way to work with it to create our dialog. Depending on which version you download,9 it can generate Visual Basic, C , C , or Visual J . Because they all compile to .NET assemblies and we won't be directly writing code in this example, it doesn't really matter which one you choose. Having generated code in another language can be...

Trying it out using PostgreSQL

To begin exploring the Data Provider layer, we need a database to work with and a corresponding data provider to connect to it from IronPython. In these examples we're going to use PostgreSQL, a high-quality open source DBMS. You can download the Figure 12.2 The structure of a DataSet Figure 12.2 The structure of a DataSet database engine from www.postgresql.org, and the ADO.NET data provider for PostgreSQL is Npgsql, which is available at npgsql.org. You can administer Postgres databases...

WMI and the System Management assembly

Powershell Ntlogevent

One of the primary Windows interfaces for system management is Management Instrumentation, known affectionately by the acronym WMI. WMI is a management infrastructure, through which system components provide information about their state and notification of events. You can use WMI to change configuration, interrogate the local system or remote computers, and respond to events. Practical uses for WMI include tasks like inventorying all installed software, uninstalling programs, creating...

Using a simple web service

When an article is published on a weblog, the blogging system will update its Atom or RSS feed so that news readers can alert users to the new article. We can download the feed using the WebClient class, which provides a high-level interface for talking to websites. gt gt gt from System.Net import WebClient gt gt gt url gt gt gt content WebClient .DownloadString url Now content is a string containing XML in the Atom format. An Atom document looks something like what you see in listing 12.14.