Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
1da328ad
Commit
1da328ad
authored
Jun 20, 2018
by
scoder
Committed by
GitHub
Jun 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2366 from gabrieldemarmiesse/test_buffer
Added tests to "Implementing the buffer protocol" part 1
parents
b24cbfba
a9488226
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
16 deletions
+18
-16
docs/examples/userguide/buffer/matrix.pyx
docs/examples/userguide/buffer/matrix.pyx
+16
-0
docs/src/userguide/buffer.rst
docs/src/userguide/buffer.rst
+2
-16
No files found.
docs/examples/userguide/buffer/matrix.pyx
0 → 100644
View file @
1da328ad
# distutils: language = c++
# matrix.pyx
from
libcpp.vector
cimport
vector
cdef
class
Matrix
:
cdef
unsigned
ncols
cdef
vector
[
float
]
v
def
__cinit__
(
self
,
unsigned
ncols
):
self
.
ncols
=
ncols
def
add_row
(
self
):
"""Adds a row, initially zero-filled."""
self
.
v
.
resize
(
self
.
v
.
size
()
+
self
.
ncols
)
docs/src/userguide/buffer.rst
View file @
1da328ad
...
...
@@ -16,21 +16,7 @@ The following Cython/C++ code implements a matrix of floats,
where the number of columns is fixed at construction time
but rows can be added dynamically.
::
# matrix.pyx
from libcpp.vector cimport vector
cdef class Matrix:
cdef unsigned ncols
cdef vector[float] v
def __cinit__(self, unsigned ncols):
self.ncols = ncols
def add_row(self):
"""Adds a row, initially zero-filled."""
self.v.extend(self.ncols)
.. literalinclude:: ../../examples/userguide/buffer/matrix.pyx
There are no methods to do anything productive with the matrices' contents.
We could implement custom ``__getitem__``, ``__setitem__``, etc. for this,
...
...
@@ -57,7 +43,7 @@ which Cython handles specially.
def add_row(self):
"""Adds a row, initially zero-filled."""
self.v.
extend(
self.ncols)
self.v.
resize(self.v.size() +
self.ncols)
def __getbuffer__(self, Py_buffer *buffer, int flags):
cdef Py_ssize_t itemsize = sizeof(self.v[0])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment