Solution Hpd

Sometimes, particularly in Python 2.3, you find yourself using dictionaries as concrete representations of sets. In such cases, you only care about the keys, not the corresponding values, and often you build the dictionaries by calls to dict.fromkeys, such as a dict.fromkeys xrange 1000 b dict.fromkeys xrange 500, 1500 The fastest way to compute the dict that is the set-union is The fastest concise way to compute the dict that is the set-intersection is inter dict.fromkeys x for x in a if x in...

Solution Esb

Perhaps you can do better, if the sequence is big, has been shuffled enough, and comparisons between its items are costly. Sort is very fast, but in the end when applied to a thoroughly shuffled sequence of length n it always takes o n log n time, while there exist algorithms that can be used to get the nth smallest element in time o n . Here is a function with a solid implementation of such an algorithm Find the nth rank ordered element the least value has rank 0 . make a new list, deal with...

Attributesfromarguments

As long as no additional logic is in the body of__init__, the dict returned by calling the built- in function locals contains only the arguments that were passed to__init__ plus those arguments that were not passed but have default values . Function attributesFromDict extracts the object, relying on the convention that the object is always an argument named 'self', and then interprets all other items in the dictionary as names and values of attributes to set. A similar but simpler technique,...

Solution Vpy

You need a factory function that catches the cases in which either the getter or the setter argument is a string, and wraps the appropriate argument into a function, then delegates the rest of the work to Python's built-in property def xproperty fget, fset, fdel None, doc None if isinstance fget, str attr_name fget def fget obj return getattr obj, attr_name elif isinstance fset, str attr_name fset def fset obj, val setattr obj, attr_name, val else raise TypeError, 'either fget or fset must be a...

Python Supertuple

You often want to pass data around by means of tuples, which play the role of C's structs, or that of simple records in other languages. Having to remember which numeric index corresponds to which field, and accessing the fields by indexing, is often bothersome. Some Python Standard Library modules, such as time and os, which in old Python versions used to return tuples, have fixed the problem by returning, instead, instances of tuple-like types that let you access the fields by name, as...

Solution Yvy

The decorate-sort-undecorate DSU idiom is simple and fast def case_insensitive_sort string_list auxiliary_list x.lower , x for x in string_list decorate return x 1 for x in auxiliary_list undecorate In Python 2.4, DSU is natively supported, so assuming the items of string_iist are indeed strings, and not, e.g., Unicode objects , you can use the following even shorter and faster approach def case_insensitive_sort string_list return sorted string_list, key str.lower

Discussion Xtb

If the values in dict d are not unique, then d cannot truly be inverted, meaning that there exists no dict id such that for any valid key k, id d k k. However, the functions shown in this recipe still construct, even in such cases, a pseudo-inverse dict pd such that, for any v that is a value in d, d pd v v. Given the original dict d and the dict x returned by either of the functions shown in this recipe, you can easily check whether x is the true inverse of d or just d's pseudo-inverse x is...

Solution Fcd

Python has a first class unicode type that you can use in place of the plain bytestring str type. It's easy, once you accept the need to explicitly convert between a bytestring and a Unicode string gt gt gt german_ae unicode ' xc3 xa4', 'utf8' Here german_ae is a unicode string representing the German lowercase a with umlaut i.e., diaeresis character ae. It has been constructed from interpreting the bytestring ' xc3 xa4' according to the specified UTF-8 encoding. There are many encodings, but...

Solution Glh

In Python 2.4, solving this problem is the job of function tee in the standard library module you can now iterate on x1 and x2 separately In Python 2.3, you can code tee yourself def yield_with_cache next, cache pop cache.pop for i in itertools.count try cache i next yield cache i it iter iterable return yield_with_cache it.next , yield_with_cache it.next

Discussion Bsw

This recipe implements the same functionality as in the previous Recipe 3.10, but instead of that recipe's simpler roll-our-own approach, this one uses the standard library module sched. sched is a reasonably simple, yet flexible and powerful, module for scheduling tasks that must take place at given times in the future. To use sched, you first instantiate a scheduler object, such as schedule shown in this recipe's Solution , with two arguments. The first argument is the function to call in...

Solution Ldt

Anything that you can do via Interface Builder and .nib files, you can also do directly in your program. Here is a simple demo from math import sin, cos, pi from Foundation import from AppKit import class DemoView NSView n 10 return sin t 1 self.width 0.5 def Y self, t return cos t 1 self.height 0.5 def drawRect_ self, rect self.width self.bounds 1 0 self.height self.bounds 1 1 NSColor.whiteColor .set NSRectFill self.bounds NSColor.blackColor .set step 2 pi self.n loop i step for i in range...

Discussion Wuu

Newcomers to Python particularly ones without experience with binary float calculations in other programming languages are often surprised by the results of seemingly simple calculations. For example gt gt gt f1 .3 assign a float gt gt gt f1 3 try some division gt gt gt f1 3 3 can we get back where we started Binary floating-point arithmetic is the default in Python for very good reasons. You can read all about them in the Python FAQ Frequently Asked Questions document at and even in the...

Solution Vhd

The easiest way to accept an arbitrary Python sequence or any other iterable object in the Python C API is with the PySequence_Fast function. It builds and returns a tuple when needed but returns only its argument with the reference count incremented when the argument is already a list or tuple a preexisting C-level function you want to expose, e.g static double total double data, int len here is how you expose it to Python code static PyObject totalDoubles PyObject self, PyObject args PyObject...

Solution Lpc

Farey fractions, whose crucial properties were studied by Augustin Louis Cauchy, are an excellent way to find rational approximations of floating-point values No error checking on args. lim maximum denominator. Results are numerator, denominator 1, 0 is infinity. z lim - lim Get a 0 of the right type for denominator lower, upper z, z 1 , z 1, z while True mediant lower 0 upper 0 , lower 1 upper 1 if v mediant 1 gt mediant 0 if lim lt mediant 1 return upper lower mediant elif v mediant 1 mediant...

Lexing

Lexing is the process of dividing an input stream into meaningful units, known as tokens, which are then processed. Lexing occurs in tasks such as data processing and in tools for inspecting and modifying text. The regular expression facilities in Python are extensive and highly evolved, so your first consideration for a lexing task is often to determine whether it can be formulated using regular expressions. Also, see the next section about parsers for common languages and how to lex those...

Discussion Oim

The handy little snippet of code in this recipe lets us find a previous weekday and print the properly formatted date, regardless of whether that weekday is in the same month, or even the same year. In this example, we're looking for the last Friday or today, if today is Friday . Friday's integer representation is 4, but to avoid depending on this magical number, we just import the Python Standard Library calendar module and rely instead on its calendar.FRIDAY attribute which, sure enough, is...

Discussion Pyl

In my location as in most others nowadays , time.daylight is always 1 because time.daylight means that this time zone has daylight saving time DST at some time during the year, whether or not DST is in effect today. The very last item in the pseudo-tuple you get by calling time.localtime, on the other hand, is 1 only when DST is currently in effect, otherwise it's 0which, in my experience, is exactly the information one usually needs to check. This recipe wraps this check into a function,...

Solution Pci

Use a typemap for SWIG, written by Mark Hammond, that was posted on comp.lang.python. It maps Win32 API functions that return bool to Python functions that return None and raise exceptions to diagnose errors. The wrapped function must set the standard Windows global LastError if it returns false indicating that it has detected an error . The wrapping function also automatically releases the Python global interpreter lock GIL for the duration of the wrapped function's execution, to allow free...

Solution Vxr

The key idea in this recipe is to have every computer periodically send a heartbeat UDP packet to a computer acting as the server for this heartbeat-monitoring service. The server keeps track of how much time has passed since each computer last sent a heartbeat and reports on computers that have been silent for too long. Here is the client program, HeartbeatClient.py, which must run on every computer we need to monitor Heartbeat client, sends out a UDP packet periodically import socket, time...

Solution Fmr

This task is one that we need to code ourselves, rather than getting an existing package to perform it, if we want complete generality Caller will hand this library a buffer string, and ask us to convert the buffer, or autodetect what codec the buffer probably uses. 'None' stands for a potentially variable byte in the XML spec autodetect_dict bytepattern name, 0x00, 0x00, 0xFE, 0xFF ucs4_be , 0xFF, 0xFE, 0x00, 0x00 ucs4_le , 0xFE, 0xFF, None, None utf_16_be , 0xFF, 0xFE, None, None utf_16_le ,...

Discussion Kzv

When you get a set of rows from a call to any of a cursor's various fetch . . . methods fetchone, fetchmany, fetchall , it is often helpful to be able to access a specific column in a row by field name and not by column number. This recipe shows a function that takes a DB API 2.0 cursor object and returns a dictionary with column numbers keyed by field names. Here's a usage example assuming you put this recipe's code in a module that you call dbutils.py somewhere on your Python sys.path . You...

PLY SPARK and Other Python Parser Generators

PLY and SPARK are two rich, solid, and mature Python-based parser generators. That is, they take as their input some statements that describe the grammar to be parsed and generate the parser for you. To make a useful tool, you must add the semantic actions to be taken when a certain construct in the grammar is recognized. PLY http systems.cs.uchicago.edu ply is a Python implementation of the popular Unix tool yacc. SPARK parses a more general set of grammars than yacc. Both tools use Python...

Discussion Yrl

If you insist, you can also perform this task with a regular expression Having a regular expression to start from may be best if you need to be tolerant of certain specific variations, or to pick up numeric substrings from the middle of larger strings. But for the specific task posed as this recipe's Problem, it's simplest and best to let Python do it

Discussion Jna

This recipe's task occurs fairly often when you're trying to modify the behavior of a standard or third-party Python module, since editing the source of the module itself is undesirable. In particular, this recipe can be handy for debugging, since the example function add_tracing_prints_to_method presented in the Solution lets you see on standard output all details of calls to a method you want to watch, without modifying the library module, and without requiring interactive access to the...

Solution Eav

To chase these problems in an optimal way, you need to alter Python's sources and rebuild Python. Specifically, add the following function in Objects object.c, immediately before the for nr no 0, op refchain._ob_next op amp refchain op op- gt _ob_next, nr op- gt ob_refcnt, no 1 fprintf fp, d refs d , d objs n, nr, _Py_RefTotal, no I place the following macros in my C extensions if defined Py_DEBUG defined DEBUG extern void _Py_CountReferences FILE define CURIOUS x fprintf stderr, __FILE__ d ,...

Solution Gnn

The third-party ctypes extension makes this task pretty easy from ctypes import windll, c_int, c_string, byref load 'Ehllapi.dll' from current dir , and function 'hllapi' from the DLL Ehllap32 windll.ehllapi prepare the arguments with types and initial values h_func c_int 1 h_text c_string 'A' h_len c_int 1 h_ret c_int 999 hllapi byref h_func , h_text, byref h_len , byref h_ret print the resulting values of all arguments after the call print h_func.value, h_text.value, h_len.value, h_ret.value

To perform this task we must examine and rewrite bytecodes in the functions

from opcode import opmap, HAVE_ARGUMENT, EXTENDED_ARG globals .update opmap def _insert_constant value, i, code, constants ''' insert LOAD_CONST for value at code i i 3 . Reuse an existing constant if values coincide, otherwise append new value to the list of constants return index of the value in constants. ''' for pos, v in enumerate constants pos len constants constants.append value code i LOAD_CONST code i 1 pos amp 0xFF code i 2 pos gt gt 8 return pos def _arg_at i, code ''' return...

Discussion Vfn

Relational databases are often perceived as difficult to use. The Python DB API can make them much easier to use, but if your programs work with several different DB engines, it's sometimes tedious to reconcile the implementation differences between the various modules, and, even more, between the engines they connect to. One of the problems of dealing with databases is presenting the result of a query when you may not know much about the data. This recipe uses the cursor's description...

Solution Rlc

Rather than coding your own solution, it's often more clever to reuse a good existing one. For this recipe's task, a good existing solution is packaged in Greg Stein's dtuple module import mx.ODBC.Windows as odbc flist Name, Num, LinkText descr dtuple.TupleDescriptor n for n in flist conn odbc.connect HoldenWebSQL Connect to a database curs conn.cursor Create a cursor sql SELECT s FROM StdPage ORDER BY PageSet, Num , .join flist print sql curs.execute sql rows curs.fetchall for row in rows row...

Discussion Jdf

The simple and standard approach of defining constant initial values of attributes by setting them as class attributes is just fine, as long as we're talking about constants of immutable types, such as numbers or strings. In such cases, it does no harm for all instances of the class to share the same initial-value object for such attributes, and, when you do such operations as self.count 1, you intrinsically rebind the specific, per-instance value of the attribute, without affecting the...

Solution Pby

Jython can do all that Java can, but with Python's elegance and high productivity. For example, here is an ImageJ plug-in that implements a simple image inverter sig public int setup String arg, ij.ImagePlus imp return ij.plugin.filter.PlugInFilter.DOES_8G def run self,ip sig public void run ij.process.ImageProcessor ip pixels ip.getPixels width ip.getWidth r ip.getRoi for x in range r.x, r.x r.width i y width x pixels i 255-pixels i

Installing Pyrex

To use Pyrex, you need to download and install it and you also need to have a C compiler. Pyrex translates your .pyx source into C source and then uses your C compiler to make from that C source a machine-code Python extension module a .pyd file on Windows, a .so file on Linux, a .dyniib file on the Mac, etc. . Installing Pyrex itself is a snap unpack the .tar.gz file, cd with the shell of your choice into the directory thus made, and at the shell prompt type the usual command to install any...

Solution Qxm

To build our tree of objects, we can directly wrap the fast expat parser from xml.parsers import expat class Element object def _ _init_ _ self, name, attributes Record tagname and attributes dictionary self.name name Initialize the element's cdata and children to empty self.cdata '' self.children def addChild self, element self.children.append element def getAttribute self, key return self.attributes.get key def getData self return self.cdata def getElements self, name '' if name return c for...

Solution Kfn

SSH is a secure replacement for the old Telnet protocol. One way to use SSH from a Python program is with the third-party paramiko package auto_ssh.py - remote control via ssh import os, sys, paramiko from getpass import getpass 0 def parse_user user, default_host, default_port ''' given name host port , returns name, host, int port , applying defaults for hose and or port if necessary return user, default_host, default_port user, host user.split ' ', 1 if ' ' in host host, port host.split ' ',...

self

No true difference exists between what I described as the self.something syntax and the theobject.something syntax the former is simply a special case of the latter, when the name of reference theobject happens to be selfl If you're new to OOP in Python, you should try, in an interactive Python environment, the example snippets I have shown so far and those I'm going to show in the rest of this Introduction. One of the best interactive Python environments for such exploration is the GUI shell...

Discussion Cgo

This recipe's program is quite trivial, mostly meant to show how to use a few of the dialogs in the EasyDialogs standard library module for the Mac. You could add quite a few more features, or do a better job of implementing some of those in this recipe, for example, by using getopt from the Python Standard Library to parse the arguments and options, rather than the roll-your-own approach we've taken. Since EasyDialogs is in the Python Standard Library for the Mac, you can count on finding that...

Introduction Xrm

Credit Paul F. Dubois, Ph.D., Program for Climate Model Diagnosis and Intercomparison, Lawrence Livermore National Laboratory This chapter was originally meant to cover mainly topics such as lexing, parsing, and code generationthe classic issues of programs that are about programs. It turns out, however, that Pythonistas did not post many recipes about such tasks, focusing more on highly Python-specific topics such as program introspection, dynamic importing, and generation of functions by...

Discussion Wyd

I needed the code in this recipe specifically to call a C function whose prototype is void FAR PASCAL hllapi int FAR , char FAR , int FAR , int FAR from a DLL named Ehllapi.DLL an implementation of the IBM 3270 HLLAPI for an Italian 3270 terminal emulator, as it happens . Thomas Heller's ctypes extension, found at http sourceforge.net projects ctypes, made the task very easy. In particular, ctypes makes mincemeat of problems related to representing function arguments that must belong to a...