Ice for Python

Python and Ice have much in common: they both are suited for a wide variety of tasks, excel at application integration, and are easy for developers to use. An Ice language mapping for Python is a recent and welcome addition to the Ice product portfolio, enabling developers to take advantage of the productivity offered by Python's friendly syntax and extensive function library.

Rapid Prototyping

Since Python is an interpreted language, useful work can be performed in just a few brief lines typed at the interpreter's command prompt. For example, we can test if an Ice object is available and obtain its type like this:

>>> proxy = communicator.stringToProxy("hello:tcp -h host.mydomain.com -p 10000")
>>> proxy.ice_ping()
>>> proxy.ice_id()
'::Demo::Hello'

The ability to dynamically invoke operations, examine data structures, diagnose failures, and explore new ideas offers a significant boost to a developer's productivity using Ice for Python.

Scripting

Python's untyped language and comprehensive function library make it ideally suited for quickly writing batch scripts that perform administrative tasks requiring access to Ice resources. Rather than writing utility programs in a compiled language that are invoked by a batch script, Ice for Python integrates access to distributed objects as a natural extension of Python's capabilities.

For example, suppose we need to initiate an archival procedure once each day. We can perform this using Ice for Python by first testing it interactively in the interpreter, and then saving it in a script for automated execution. These steps could be as simple as the following script:

import Ice
Ice.loadSlice("Admin.ice")
comm = Ice.initialize()
admin = Biz.AdminPrx.checkedCast(comm.stringToProxy("Admin"))
admin.initiateArchive()

Although Ice for Python supports static translation of Slice interface definitions into Python code, scripts may also translate the interface definitions dynamically, as shown in the example above. Dynamic translation is advantageous, especially for short or transient scripts, because it accelerates the testing cycle and minimizes external dependencies.

Application Development

Ice for Python is not only appropriate for scripting, but also for large-scale application development. With support for routers, asynchronous invocation, and secure communication via SSL, Ice for Python speeds the construction of sophisticated client applications. Developers interested in writing Ice servers in Python will find a full complement of Ice features, including asynchronous dispatch and servant locators.

Sample Code

To illustrate how Ice is used in various language mappings, consider the following Slice interface definitions:

module Biz {
    struct Item {
        string sku;
        int qty;
    };
    sequence<Item> ItemSeq;
     
    interface Invoice {
        void addItems(ItemSeq items);
        void submit();
    };
    interface InvoiceFactory {
        Invoice* create();
    };
};

Python code that creates an invoice using the factory is shown below:

# Create a proxy for the invoice factory object.
proxy = communicator.stringToProxy("InvoiceFactory:tcp -p 9000")

# Narrow the proxy to the proper type.
factory = Biz.InvoiceFactoryPrx.checkedCast(proxy)

# Use the factory to obtain a proxy for a new invoice object.
invoice = factory.create()

# Add items to the invoice.
item = Biz.Item()
item.sku = "10-6139"
item.qty = 3
items = (item, )
invoice.addItems(items)

# Submit the invoice.
invoice.submit()
Copyright © 2008 ZeroC, Inc.