Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / crange.py
Created February 28, 2010 23:24
def crange(*args)
from __future__ import print_function
def crange(*args):
"""crange([start,] stop[, step]) -> xrange object
Make a closed integer range like xrange but with inclusive stop index,
unless it is not reachable with the given step size.
Examples:
>>> for x in crange(-2,2): print(x, end=' ')
@pyrtsa
pyrtsa / combine.py
Created February 28, 2010 23:27
def combine(function, *arrays)
def combine(function, *arrays):
"""Construct an ndarray through a cartesian product of N sequences.
Example:
>>> combine(sum, [1,3], [.1,.2,.3], [0,10,100])
array([[[ 1.1, 11.1, 101.1],
[ 1.2, 11.2, 101.2],
[ 1.3, 11.3, 101.3]],
<BLANKLINE>
[[ 3.1, 13.1, 103.1],
@pyrtsa
pyrtsa / inspect.py
Created March 2, 2010 14:35
def inspect(*names)
def inspect(*names):
"""Print values of the given expressions. Use at your own risk.
Usage examples:
>>> x,y,something = 1,'y',3.14159
>>> inspect('x')
x = 1
>>> inspect(*'x y something'.split())
x = 1
y = 'y'
@pyrtsa
pyrtsa / vec.py
Created April 5, 2010 13:03
class vec(tuple)
class vec(tuple):
"""
Tuples that behave less like containers, and more like vectors.
>>> 10 * vec(1,2,3) + 1
(11, 21, 31)
All basic operators behave component-wise, and binary operators broadcast
when given a non-iterable operand. Other than that, binary operators require
both operands to be of the same length.
@pyrtsa
pyrtsa / gist:356328
Created April 5, 2010 13:24
def offset(image, d)
def offset(image, d):
"""
Create a subview of image where the indices are offset by (a tuple) d.
Assume you needed to select all the items in a 3-D numpy.ndarray arr
which have neighbors at a distance d = (i, j, k). You can easily view
those items calling:
offset(arr, (-i, -j, -k)).
The neighbors are then, respectively:
offset(arr, d)
@pyrtsa
pyrtsa / dicttuple.py
Created December 6, 2010 16:33
def dicttuple(typename, field_names, **kwargs)
def dicttuple(typename, field_names, **kwargs):
"Create a namedtuple that can be used like a dict in **keyword expansion."
from itertools import count, izip
from collections import namedtuple
t = namedtuple(typename, field_names, **kwargs)
t._index = dict(izip(t._fields, count()))
t.keys = lambda self: self._fields
t.keys.__doc__ = "List of tuple element names."
f = t.__getitem__
t.__getitem__ = lambda sf,i: sf[sf._index[i]] if isstring(i) else f(sf, i)
@pyrtsa
pyrtsa / coerce.hpp
Created May 12, 2011 07:53
Sketching the interface for the Boost.Coerce library
// === The planned interface (work in progress) ================================
namespace boost { namespace coerce {
// Does traits::as<T,S> provide the interface for extending the library?
namespace traits {
template <typename Target, typename Source, typename Enable = void>
struct as;
}
@pyrtsa
pyrtsa / factory.cpp
Created June 28, 2011 16:43
Object factory using Boost.Phoenix, Boost.Function and Boost.SmartPtr libraries
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/variant.hpp>
#include <iostream>
#include <map>
#include <string>
#include <vector>
struct Object1 { std::string hello() const { return "Hi, Object1"; } };
@pyrtsa
pyrtsa / factory.cpp
Created June 29, 2011 19:01
Object factory v2, using Boost.Phoenix, Boost.Function and Boost.SmartPtr libraries
#include <boost/assign/list_of.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/phoenix/bind/bind_function_object.hpp>
#include <boost/phoenix/core/argument.hpp>
#include <boost/phoenix/core/reference.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/object/make_shared.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <iostream>
@pyrtsa
pyrtsa / factory.cpp
Created June 30, 2011 13:25 — forked from benben/factory.cpp
Object factory using Boost.Phoenix, Boost.Function and Boost.SmartPtr libraries
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <map>
#include <string>
#include <vector>
struct Parent { virtual std::string hello() const = 0; };