xdress/xdress — pull request #127 by scopatz (2013-12-22 05:52:31+00:00)
Title: Better dtype meshing with stlwrap

This is a companion to #126 and should be merged after that PR. This PR adds the ability for dtypes to be constructed from C++ STL containers (namely map and set). Therefore it is now possible to wrap ridiculous types such as `vector>` and `vector>` and any other nestings you can think of. Previously the innermost templated type had to be vectors.

Note that because of the way numpy works, for an array `x` of dtype `d`, `x[0]` returns a scalar of type `d`. `x[0]` is *not* an instance of the associated Python type. This is true even for primitive types, like float. In order to get a Python type, you have to explicitly cast it. For example:

```py
In [2]: x = np.array([42.0], dtype=float)

In [3]: x[0]
Out[3]: 42.0

In [4]: type(x[0])
Out[4]: numpy.float64

In [5]: float(x[0])
Out[5]: 42.0

In [6]: type(float(x[0]))
Out[6]: float
```

This scalar version is a copy of the original data in the ndarray and not a view*. The dtype that is generated does not have the API of the underlying type it is enscapsulating. Thus to get something useful in Python out of this (for the moment), you have to cast to the appropriate dtype. For example

```py
x = np.array([set([16, 42])], dtype=dt.xd_set_int)
s = stlc.SetInt(x[0])
s.add(10)
x[0] = s
```

This casting operation hasn't been extended to cythongen in this PR. There is already some constructor funniness right now and so more work is likely needed there. What is done here, though, will serve as a template for what to do for custom classes.

\* While this doesn't matter much for primitive scalars, it is pretty frustrating for the more complex dtypes we are representing here. Clearly numpy didn't have our use case in mind...