This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=' ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// === 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; }; |
OlderNewer