Skip to content

Commit 6d014a4

Browse files
author
Irwin Zaid
committed
Removed get_dynamic_array_function by bringing it into cython
1 parent 0640c06 commit 6d014a4

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

dynd/cpp/array.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ..config cimport translate_exception
22
from libc.stdint cimport intptr_t
3+
from libcpp cimport bool
34
from .type cimport type
45

56
cdef extern from 'dynd/array.hpp' namespace 'dynd::nd' nogil:
@@ -16,6 +17,8 @@ cdef extern from 'dynd/array.hpp' namespace 'dynd::nd' nogil:
1617

1718
array view_scalars(type&) except +translate_exception
1819

20+
bool is_null()
21+
1922
array operator<(array &)
2023
array operator<=(array &)
2124
array operator==(array &)

dynd/cpp/func/callable.pxd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
from libcpp cimport bool
2+
3+
from ..array cimport array
14
from ..type cimport type
25

36
cdef extern from 'dynd/func/callable.hpp' namespace 'dynd::nd' nogil:
47
cdef cppclass callable:
58
callable()
69

710
type get_array_type()
11+
12+
bool is_null()
13+
14+
array operator()(...)

dynd/cpp/type.pxd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1+
from libcpp cimport bool
2+
from libcpp.map cimport map
13
from libcpp.string cimport string
24

35
from .types.type_id cimport type_id_t
46

57
from ..config cimport translate_exception
68
from .array cimport array
9+
from .func.callable cimport callable
10+
11+
cdef extern from 'dynd/types/base_type.hpp' namespace 'dynd::ndt' nogil:
12+
cdef cppclass base_type:
13+
void get_dynamic_array_properties(map[string, callable] &)
14+
void get_dynamic_array_functions(map[string, callable] &)
15+
16+
cdef extern from 'dynd/types/builtin_type_properties.hpp' namespace 'dynd' nogil:
17+
void get_builtin_type_dynamic_array_properties(type_id_t, map[string, callable] &)
718

819
cdef extern from 'dynd/type.hpp' namespace 'dynd::ndt' nogil:
920
cdef cppclass type:
1021
type()
1122
type(type_id_t) except +translate_exception
1223
type(string&) except +translate_exception
1324

25+
base_type *get()
26+
1427
size_t get_data_size()
1528
size_t get_default_data_size() except +translate_exception
1629
size_t get_data_alignment()
1730
size_t get_arrmeta_size()
1831
type get_canonical_type()
1932

33+
bool is_builtin()
34+
2035
bint operator==(type&)
2136
bint operator!=(type&)
2237

dynd/nd/array.pyx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GE, Py_GT
44
from libcpp.string cimport string
5+
from libcpp.map cimport map
6+
from cython.operator import dereference
57

68
from ..cpp.array cimport groupby as dynd_groupby
7-
from ..cpp.type cimport type as _type
9+
from ..cpp.func.callable cimport callable as _callable
10+
from ..cpp.type cimport type as _type, get_builtin_type_dynamic_array_properties
811
from ..cpp.types.categorical_type cimport dynd_make_categorical_type
912
from ..cpp.types.datashape_formatter cimport format_datashape as dynd_format_datashape
1013

@@ -72,7 +75,6 @@ cdef extern from 'array_functions.hpp' namespace 'pydynd':
7275
int array_releasebuffer_pep3118(object ndo, Py_buffer *buffer) except -1
7376

7477
cdef extern from 'gfunc_callable_functions.hpp' namespace 'pydynd':
75-
object get_array_dynamic_property(_array&, object) except +translate_exception
7678
void add_array_names_to_dir_dict(_array&, object) except +translate_exception
7779
void set_array_dynamic_property(_array&, object, object) except +translate_exception
7880

@@ -201,7 +203,30 @@ cdef class array(object):
201203
return result.keys()
202204

203205
def __getattr__(self, name):
204-
return get_array_dynamic_property(self.v, name)
206+
if self.v.is_null():
207+
raise AttributeError(name)
208+
209+
cdef map[string, _callable] properties
210+
211+
cdef _type dt = self.v.get_type()
212+
if (not dt.is_builtin()):
213+
dt.get().get_dynamic_array_properties(properties)
214+
else:
215+
get_builtin_type_dynamic_array_properties(dt.get_type_id(), properties);
216+
217+
cdef _callable p = properties[name]
218+
if (not p.is_null()):
219+
return wrap(p(self.v))
220+
221+
cdef map[string, _callable] functions
222+
if (not dt.is_builtin()):
223+
dt.get().get_dynamic_array_functions(functions)
224+
225+
cdef _callable f = functions[name]
226+
if (not f.is_null()):
227+
return wrap(f(self.v))
228+
229+
raise AttributeError(name)
205230

206231
def __setattr__(self, name, value):
207232
set_array_dynamic_property(self.v, name, value)

dynd/src/gfunc_callable_functions.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -126,45 +126,6 @@ void pydynd::add_array_names_to_dir_dict(const dynd::nd::array &n,
126126
}
127127
}
128128

129-
PyObject *pydynd::get_array_dynamic_property(const dynd::nd::array &n,
130-
PyObject *name)
131-
{
132-
if (n.is_null()) {
133-
PyErr_SetObject(PyExc_AttributeError, name);
134-
return NULL;
135-
}
136-
ndt::type dt = n.get_type();
137-
std::map<std::string, nd::callable> properties;
138-
// Search for a property
139-
if (!dt.is_builtin()) {
140-
dt.extended()->get_dynamic_array_properties(properties);
141-
}
142-
else {
143-
get_builtin_type_dynamic_array_properties(dt.get_type_id(), properties);
144-
}
145-
146-
// TODO: We probably want to make some kind of acceleration structure for the
147-
// name lookup
148-
std::string nstr = pystring_as_string(name);
149-
nd::callable p = properties[nstr];
150-
if (!p.is_null()) {
151-
return DyND_PyWrapper_New(p(n));
152-
}
153-
154-
// Search for a function
155-
std::map<std::string, nd::callable> functions;
156-
if (!dt.is_builtin()) {
157-
dt.extended()->get_dynamic_array_functions(functions);
158-
}
159-
nd::callable c = functions[nstr];
160-
if (!c.is_null()) {
161-
return DyND_PyWrapper_New(c(n));
162-
}
163-
164-
PyErr_SetObject(PyExc_AttributeError, name);
165-
return NULL;
166-
}
167-
168129
void pydynd::set_array_dynamic_property(const dynd::nd::array &n,
169130
PyObject *name, PyObject *value)
170131
{

0 commit comments

Comments
 (0)