[Pyrex] Access to nested structures and arrays
Robert Bradshaw
robertwb at math.washington.edu
Fri Mar 16 20:19:29 UTC 2007
On Mar 16, 2007, at 10:29 AM, Pierre GM wrote:
> [...]
> First question:
> When writing extension types, am I limited to use only Python
> objects as
> arguments of __init__ ?
Yes, as __init__ cannot be a cdef function (otherwise it wouldn't be
callable via python).
> Using properties was the quickest way I found to access the
> elements of the
> underlying C substructure, but that looks a bit bloated: I'm
> putting layers
> over layers.
>
> Second question:
> isn't there a better way than that ?
The c-level substructure cannot be directly accessed from python, as
python code deals only with python objects. If, however, you have
another extension class that knows that object x is of type l_model,
it can call, e.g., x.npar directly.
> Finally, using properties is fine up to a point. Some elements of the
> substructures are pointers to C arrays (for example, parametric in
> c_model).
> I need to read and write to these arrays. Reading is fine, but I
> have a
> problem with writing: the __set__ method I use lets me write the
> whole array
> at once, but I'd like to modify some individual elements.
> For example:
>>>> X = l_model.parametric_flags
> X is a ndarray, everything's fine
>>>> l_model.parametric_flags = X[::-1]
> I'm setting the whole array at once, so that's fine as well. But now,
>>>> l_model.parametric_flags[0] = True
> will NOT update the first element of the underlying C substructure,
> c_model.parametric, as I'd like it to.
>
> Third question:
> how can I modify the underlying C array from Python ?
You can't. You have to write a python function that when called
modifies the array.
> I hope it doesn't sound too confusing. I'm still very new to C and
> Pyrex, I'm
> sure you'll be lenient.
>
> Thanks a lot in advance for any idea/comments.
Perhaps if you think of things in terms of providing a python
interface to manipulating the underlying c structure, rather than
trying to expose the c structure itself, things would become easier
to do.
More information about the Pyrex
mailing list