Skip to content

Instantly share code, notes, and snippets.

@gmarkall
gmarkall / simple.py
Created April 25, 2011 10:13
Simple multiple dispatch in Python
class Visitor:
def dispatch(self, tree):
"Dispatcher function, dispatching tree type T to method _T."
meth = getattr(self, "_"+tree.__class__.__name__)
meth(tree)
test edit
@gmarkall
gmarkall / 5mmexample.py
Created April 25, 2011 10:19
Example use of Guido's Five-minute Multimethods
from mm import multimethod
class Scope:
pass
class GlobalScope(Scope):
pass
class Statement:
pass
@gmarkall
gmarkall / dispatchexample.py
Created April 25, 2011 11:47
Example use of the python dispatch package
import dispatch
class Visitor:
@dispatch.on('node')
def visit(node):
print "Base implementation"
@visit.when(Scope)
def visit(node):
@gmarkall
gmarkall / simpleexample.py
Created April 25, 2011 12:12
Example of very simple multiple dispatch in python
class Visitor:
def visit(self, node):
"Dispatcher function, dispatching tree type T to method _T."
meth = getattr(self, "_"+node.__class__.__name__)
meth(node)
def _Scope(self, node):
print "Scope"
@gmarkall
gmarkall / mm.py
Created April 25, 2011 12:15
mm module (by Guido van Rossum)
registry = {}
class MultiMethod(object):
def __init__(self, name):
self._name = name
self._typemap = {}
def __call__(self, *args):
types = tuple(arg.__class__ for arg in args)
function = self._typemap.get(types)
if function is None:
@gmarkall
gmarkall / dispatchexample.py
Created April 25, 2011 17:58
Example use of the python dispatch package
import dispatch
class Visitor:
@dispatch.on('node')
def visit(self, node):
print "Base implementation"
@gmarkall
gmarkall / crashpetscflistget.c
Created October 10, 2012 10:19
Failure to use PetscFListGet for listing available KSP types
#include <petscsys.h>
#include <petscksp.h>
// Struct copied from reg.c so we can access the members of a PetscFList
struct _n_PetscFList {
void (*routine)(void); /* the routine */
char *path; /* path of link library containing routine */
char *name; /* string to identify routine */
char *rname; /* routine name in dynamic library */
PetscFList next; /* next pointer */
@gmarkall
gmarkall / laplace_pyop2.py
Created November 6, 2012 09:51
PyOP2 Laplace demo with a strong boundary condition exemplification
# Set up finite element problem. We do this by defining the finite element
# variational forms using the Unified Form Language (UFL)
E = FiniteElement("Lagrange", "triangle", 1)
v = TestFunction(E)
u = TrialFunction(E)
f = Coefficient(E)
g = Coefficient(E)
int* f()
{
static int a[3] = {1,2,3};
return a;
}
@gmarkall
gmarkall / ibz.f90
Last active December 29, 2015 10:09
module IBZ
implicit none
type Plane
integer :: M(1,1)
end type Plane
contains
integer function t()