Ticket #6 (new task)

Opened 2 years ago

define more custom HocXX_Types, add a general function to convert Object* to proper HocXX_Type

Reported by: emuller Assigned to: emuller
Priority: major Milestone:
Component: component1 Version:
Keywords: Cc:

Description

OK, so I added a nrnpy_HocVectorType, which is still just a PyHocObject?, but has an additional member "toarray"... you can guess what it does ;-) (see: changeset:32)

I think we should add more of such types: HocSectionType?, HocArrayType? etc ... one for each sub type ... and customize each one for a great OOP experience.

The getattro has to be modified to create and return objects of the proper type given their hoc type. This isn't hard.

What doesn't work yet, I guess it has to be implemented somewhere, is that functions that return Vectors still return HocObjects?, rather than HocVectors?.

Examples:

Python 2.4.4 (#1, May 11 2007, 14:58:41)
[GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import neuron as nrn
NEURON -- VERSION 6.0.pygetsetcall.20 (30) 2007-05-22 (30M) by John W. Moore, Michael Hines, and Ted Carnevale
Duke and Yale University -- Copyright 1984-2007
>>>nrn.h('objref x')
1
>>>nrn.h('x = new Vector(11)')
1
>>>v = nrn.h.x
>>>type(v)
<type 'hoc.HocVector'>
>>>v.toarray()
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

** note this works now: **

>>>dir(v)
['__call__', '__class__', '__delattr__', '__delitem__', '__doc__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', 'toarray']

**but**

>>>v1 = nrn.h.newvec(5)
>>>type(v1)
<type 'hoc.HocObject'>
>>>dir(v1)
['__call__', '__class__', '__delattr__', '__delitem__', '__doc__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__']
>>>v1.toarray()
Traceback (most recent call last):   File "<stdin>", line 1, in ?
AttributeError: 'hoc.HocObject' object has no attribute 'toarray'

So if you guys like the approach, I'll dig around to find out where this has to be fixed ... probably we should define a function which takes a Object* and returns the proper python object type... and use that where appropriate.

also, I think we should think about conforming to standard pythonic C-API naming conventions ...

something like

PyHocObject_Type
PyHocVector_Type
PyHocArray_Type
PyHocSection_Type

cheers,

Eilif