summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Glover <j@johnglover.net>2012-07-26 18:30:25 +0100
committerJohn Glover <j@johnglover.net>2012-07-26 18:30:25 +0100
commitd78ca5b8052bc7c4be1d18f14b407614f2e0726a (patch)
tree8bb64dc59a06ffaacec09936bb364c056dffb053
parent7b5bb5a4d2d00f6f3b0e921090c18edcdf867d46 (diff)
downloadsimpl-d78ca5b8052bc7c4be1d18f14b407614f2e0726a.tar.gz
simpl-d78ca5b8052bc7c4be1d18f14b407614f2e0726a.tar.bz2
simpl-d78ca5b8052bc7c4be1d18f14b407614f2e0726a.zip
[base] Keep a list of Python Peak objects instead
passing pointers to the C++ classes. This simplifies the Python code slightly, but also makes sure that references to the Python objects are not lost (and the objects then garbage collected too early).
-rw-r--r--simpl/base.pxd3
-rw-r--r--simpl/base.pyx19
2 files changed, 7 insertions, 15 deletions
diff --git a/simpl/base.pxd b/simpl/base.pxd
index 8b825ba..e364e6e 100644
--- a/simpl/base.pxd
+++ b/simpl/base.pxd
@@ -19,6 +19,7 @@ cdef class Frame:
cdef c_Frame* thisptr
cdef int created
cdef set_frame(self, c_Frame* f)
+ cdef list _peaks
cdef extern from "<string>" namespace "std":
@@ -44,9 +45,7 @@ cdef extern from "../src/simpl/base.h" namespace "simpl":
void num_peaks(int new_num_peaks)
int max_peaks()
void max_peaks(int new_max_peaks)
- void add_peak(c_Peak* peak)
c_Peak* peak(int peak_number)
- void peak(int peak_number, c_Peak* peak)
void clear()
# partials
diff --git a/simpl/base.pyx b/simpl/base.pyx
index dda9240..e186fee 100644
--- a/simpl/base.pyx
+++ b/simpl/base.pyx
@@ -36,6 +36,8 @@ cdef class Peak:
cdef class Frame:
def __cinit__(self, size=None, create_new=True, alloc_memory=False):
+ self._peaks = []
+
if create_new:
if size:
self.thisptr = new c_Frame(size, alloc_memory)
@@ -54,21 +56,10 @@ cdef class Frame:
self.thisptr = f
# peaks
- property num_peaks:
- def __get__(self): return self.thisptr.num_peaks()
- def __set__(self, int i): self.thisptr.num_peaks(i)
-
property max_peaks:
def __get__(self): return self.thisptr.max_peaks()
def __set__(self, int i): self.thisptr.max_peaks(i)
- def add_peak(self, Peak p not None):
- self.thisptr.add_peak(p.thisptr)
-
- def add_peaks(self, peaks not None):
- for p in peaks:
- self.add_peak(p)
-
def peak(self, int i):
cdef c_Peak* c_p = self.thisptr.peak(i)
p = Peak(False)
@@ -77,9 +68,11 @@ cdef class Frame:
property peaks:
def __get__(self):
- return [self.peak(i) for i in range(self.thisptr.num_peaks())]
+ if not self._peaks:
+ self._peaks = [self.peak(i) for i in range(self.thisptr.num_peaks())]
+ return self._peaks
def __set__(self, peaks):
- self.add_peaks(peaks)
+ self._peaks = peaks
def clear(self):
self.thisptr.clear()