Skip to content

{ Category Archives } Python

Extracting the boundary of a 2D graph

Given a 2D finite element mesh, we wish to determine the boundary nodes and edges using simple graph operations.
Using networkx, the following code builds the graph and then extracts the boundary edges:

import networkx as nx
#Use a MultiGraph to allow parallel edges
G = nx.MultiGraph()
#Each element is a cycle
G.add_edges_from([(1,2),(2,6),(6,5),(5,1)])
G.add_edges_from([(2,3),(3,7),(7,6),(6,2)])
G.add_edges_from([(3,4),(4,8),(8,7),(7,3)])
G.add_edges_from([(5,6),(6,10),(10,9),(9,5)])
G.add_edges_from([(6,7),(7,11),(11,10),(10,6)])
G.add_edges_from([(7,8),(8,12),(12,11),(11,7)])
# Create a boundary graph
G_boundary = nx.Graph()
for v1,v2 [...]

Converting python scripts to windows executables

Converting a non-trivial python script into a windows executable is always a pain but often necessary. This post Using py2exe to freeze your application details a full blown setup.py script that will help. I have modified the setup.py to build an installer that also includes the Microsoft VS C run time dll’s.

Tagged ,

Converting month number to month name

Greg Pinero posted a nice, succinct solution to converting the month number to the month name:
>>>import datetime
>>>named_month = lambda month_num:datetime.date(1900,month_num,1).strftime(‘%B’)
>>> named_month(3)
‘March’

Tagged ,

Pure python special functions

As John Cook has pointed out, you sometimes wish to forego efficiency for portability. To that end, he has compiled a list of special functions and other algorithms with no external dependencies.

A simple script to rename jpg files based on date

As anyone with a digital camera knows, you can generate a lot of files in a very short time from all of your photos. Since almost everyone in my household has a digital camera, that represents a lot of files, haphazardly scattered over many directories.
I’ve looked around quite a bit and found many utilities that [...]

Alphanumeric sorting

Ned Batchelder (the author of the coverage.py module) has a succinct alphanumeric sort:
import re

def tryint(s):
try:
return int(s)
except:
return s

def alphanum_key(s):
“”" Turn a string into a list of string and [...]

Create a Debian package from your Python application

This post describes in enough detail how to package your Python application in a Debian package.

Exceptions

Ned Batchelder has written a concise tutorial on the use of exceptions in Python. A slight improvement would be to illustrate the creation of new exception classes. Will do that here if time allows. Consider the following:
import exceptions
import pprint

class MyException(exceptions.Exception):
def __init__(self, value, **kwargs):
self.value [...]

Combinatorics

Jack Diederich has written a nice C extension for combinatorics. Fast and pythonic. A big problem with many pure python implementations is that they are just too slow for realistic inputs. Consider that the output in this class of problem grows factorially, it’s easy to understand why python will be slow. The C extension [...]

Alphabetic Sort

Short and sweet exposition on alphabetic sort. Using Python 2.4-3 and higher, you can use the sort(key=xxx) syntax. Rather than preserving the stable sort, the key=lambda s: (s.lower(), s) will sort lowercase then uppercase.
>>> items=['greg','Car','car','Goose']
>>> items.sort(key=lambda s: (s.lower(), s))
>>> items
['Car', 'car', 'Goose', 'greg']
>>> items=['greg','Car','car','Goose'][::-1]
>>> items.sort(key=lambda s: (s.lower(), s))
>>> items
['Car', 'car', 'Goose', 'greg']