pigfoot’s weblog

We should forget about small efficiencies — Donald Knuth

Archive for the ‘Python’ Category

Python 2.5 released

Python

Python 2.5 final released!, from digg.

This release (mailing list announcement) includes:

  • PEP 308: conditional expressions
  • PEP 328: absolute and relative imports
  • PEP 341: unified try/except/finally
  • PEP 342: new generator features
  • PEP 343: the ‘with’ statement
  • new packages: ctypes, ElementTree, hashlib, sqlite, wsgiref

For more details, you can refer to Python 2.5 what’s new.

Popularity: 27% [?]

CLR Inside Out: IronPython

CLR Inside Out: IronPython, from MSDN Magazine

IronPython is the code name for an implementation of the Python programming language written by the CLR team at Microsoft. IronPython runs on the Microsoft .NET Framework and supports an interactive console with fully dynamic compilation. It is well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining full compatibility with the Python language.

This column will give a brief overview of Python and what sets dynamic languages apart from other languages. I will discuss iterative development, describe how IronPython integrates with .NET while staying true to Python syntax, and show the advantages of using IronPython to utilize .NET.

Besides, Here are some sample codes on CodePlex:

Popularity: 18% [?]

  • 0 Comments
  • Filed under: Develop, Python
  • Effective Python Programming

    Python

    Effective Python Programming, from OSCON 2005.

    Python’s growing popularity has brought many new developers to the language, often coming from other languages that have very different idioms and restrictions. This can lead to less-than-optimal coding styles.

    This tutorial focuses on writing Python in a “Pythonic” way, working through a series of examples.

    This is a concentrated exercise in helping people write code in a way that makes the best use of language features. It is of course possible to write bad Python–just as it’s possible to write bad code in any language. However, it is suprisingly easy to write good Python and hopefully this tutorial will help you bootstrap yourself into a better Python programmer.

    This tutorial is aimed at people who already know Python, but don’t consider themselves experts. (Python experts will probably know most of the things discussed in this tutorial.)

    You can download the presentation file (PDF) (159 slides) here.

    Popularity: 16% [?]

  • 0 Comments
  • Filed under: Develop, Python
  • Untwisting Python Network Programming, from O’ReillyNet.

    This article introduces basic client-side networking using both core Python modules and the Twisted framework. For its example, I will show how to send, receive, and delete emails, and conduct Telnet sessions.

    I have written two functionally equivalent examples, one using the core modules (mail-core.py) and another using Twisted (mail-twisted.py), with both start, stop, and interact with a server to process emails.

    Here is outline:

    core Python modules

    • Sending Mails with smtplib
    • Retrieving Emails with poplib
    • Conducting Telnet with telnetlib

    Twisted Way

    • Sending Mails the Twisted Way
    • Retrieving Mails with Twisted
    • Doing Telnet with Twisted

    When to Be Twisted?

    The two functionally equivalent programs, one using Python core modules and the other using the Twisted framework, significantly differ from each other in terms of programming style and the amount of code. Then when should you use either of the two options?

    For basic programs such as the command-line client of this example, the Python core networking modules are more desirable due to the simplicity and performance advantages. However, most real-world networking programs are very complex, and Twisted’s asynchronous programming model is more effective.

    For example, BitTorrent, the popular peer-to-peer file sharing client that performs massive parallel downloading of data chunks from different sources, uses Twisted. Twisted also works well in programs with graphical user interface (GUI), because its asynchronous nature fits more seamlessly with the event-driven programming models of modern GUI frameworks. In fact, Twisted has integration with popular GUI frameworks including PyGTK, Qt, Tkinter, WxPython, and Win32.

    The other area where Twisted shines is in server programming. A typical network server uses multithreading so that it can handle multiple clients concurrently. The asynchronous mechanism of Twisted alleviates the creation and handling of threads by server programs. In addition, Twisted provides several protocols on which to build new networking services, enabling rapid development of complex servers. One such project is Quotient, which adopts Twisted to build a multiprotocol messaging server that supports a variety of protocols and services including SMTP, POP3, IMAP, webmail, and SIP.

    Popularity: 16% [?]

  • 0 Comments
  • Filed under: Develop, Python
  • Although mordem md5sum(1) of coreutils in Linux can acknowledge the md5sum of FreeBSD format, I still want to convert FreeBSD format to Linux one via python. Here is original file:

    md5sum generated by md5(1) of FreeBSD

    MD5 (Thunderbird-1.5.0.4.exe) = 3b862cd35571ecec3de5e34112bf3f15
    MD5 (Thunderbird-1.5.0.5.exe) = 3b93bd5529e2a81054514f57dc4b3916

    And I want to convert to:

    md5sum generated by md5sum(1) of Linux

    3b862cd35571ecec3de5e34112bf3f15 Thunderbird-1.5.0.4.exe
    3b93bd5529e2a81054514f57dc4b3916 Thunderbird-1.5.0.5.exe

    The sample code is quite simple:

    # Open file named md5sum.txt
    hFile = open ("md5sum.txt", "rU")

    # Read whole content into a List file handle
    aListLines = [szLine for szLine in hFile]

    # Loading completed, then close file handle
    hFile.close()

    # Reset output list
    aListOutputLines = []

    # Process each line one by one
    for szLine in aListLines:

    ....# split 'szFile = szHash' by three charactes of ' = '
    ....szFile, szHash = szLine.split(' = ')

    ....# Strip what we don't need and add two characters of space
    ....aListOutputLines.append(szHash.rstrip('\n') \
    ........+ ' ' * 2 \
    ........+ szFile.lstrip('MD5 (').rstrip(')'))

    # Now aListOutputLines is final result, hence we open file to write next
    hFile = open("md5sum.txt.new", "w")

    # Process each line one by one
    for szLine in aListOutputLines:
    ....hFile.write("%s\n" % szLine)

    # Closing a handle is a good habit, it's unnecessary though
    hFile.close()

    It’s all ;-)

    By the way, the meaning of open (”md5sum.txt”, “rU”) is called open file with Universal Newline.

    Popularity: 16% [?]

  • 2 Comments
  • Filed under: Develop, Python