Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 89a1275a authored by hhakim's avatar hhakim
Browse files

Override default __repr__() in Faust class and use it to refactor display().

__repr__() uses the new to_string() function from Faust::TransformHelper.
parent bac3976e
Branches
No related tags found
No related merge requests found
...@@ -47,6 +47,7 @@ namespace Faust { ...@@ -47,6 +47,7 @@ namespace Faust {
faust_unsigned_int get_total_nnz() const; faust_unsigned_int get_total_nnz() const;
faust_unsigned_int size() const; faust_unsigned_int size() const;
void display() const; void display() const;
string to_string() const;
MatDense<FPP,Cpu> get_fact(faust_unsigned_int id) const; MatDense<FPP,Cpu> get_fact(faust_unsigned_int id) const;
MatDense<FPP,Cpu> get_product() const; MatDense<FPP,Cpu> get_product() const;
void save_mat_file(const char* filename) const; void save_mat_file(const char* filename) const;
......
...@@ -109,6 +109,12 @@ namespace Faust { ...@@ -109,6 +109,12 @@ namespace Faust {
this->transform->Display(); this->transform->Display();
} }
template<typename FPP>
string TransformHelper<FPP,Cpu>::to_string() const
{
return this->transform->to_string();
}
template<typename FPP> template<typename FPP>
MatDense<FPP,Cpu> TransformHelper<FPP,Cpu>::get_fact(faust_unsigned_int id) const MatDense<FPP,Cpu> TransformHelper<FPP,Cpu>::get_fact(faust_unsigned_int id) const
{ {
......
...@@ -266,6 +266,35 @@ class Faust: ...@@ -266,6 +266,35 @@ class Faust:
F_ctrans = Faust(core_obj=F.m_faust.getH()) F_ctrans = Faust(core_obj=F.m_faust.getH())
return F_ctrans return F_ctrans
def __repr__(F):
"""
Returns a str object representing the Faust object.
This method overloads a Python function.
NOTE: Ideally this function is intented to return a valid Python
expression but here this is not the case. Only information is
displayed.
Args:
F: the Faust object.
Examples:
>>> from FaustPy import Faust, RandFaustType
>>> F = Faust.randFaust(RandFaustType.MIXTE, RandFaustType.REAL,
>>> 1, 2, 50, 100, .5)
>>> F.__repr__()
>>> # the same function is called when typing F in a terminal:
>>> F
<b/>See also Faust.nnz_sum, Faust.rcg, Faust.shape, Faust.get_factor,
<b/>Faust.get_nb_factors, Faust.display
"""
#_str = super(object, F).__repr__()
_str = str(F.m_faust.to_string())
return _str
def display(F): def display(F):
""" """
Displays information about F. Displays information about F.
...@@ -283,13 +312,16 @@ class Faust: ...@@ -283,13 +312,16 @@ class Faust:
>>> from FaustPy import Faust, RandFaustType >>> from FaustPy import Faust, RandFaustType
>>> F = Faust.randFaust(RandFaustType.MIXTE, RandFaustType.REAL, >>> F = Faust.randFaust(RandFaustType.MIXTE, RandFaustType.REAL,
>>> 1, 2, 50, 100, .5) >>> 1, 2, 50, 100, .5)
>>> F.display() >>> F.print()
>>> # equivalent to:
>>> F
<b/>See also Faust.nnz_sum, Faust.rcg, Faust.shape, Faust.get_factor, <b/>See also Faust.nnz_sum, Faust.rcg, Faust.shape, Faust.get_factor,
Faust.get_nb_factors <b/>Faust.get_nb_factors, Faust.__repr__
""" """
F.m_faust.display() print(F.__repr__())
#F.m_faust.display()
def __mul__(F, A): def __mul__(F, A):
""" """
......
...@@ -58,6 +58,7 @@ class FaustCoreCpp ...@@ -58,6 +58,7 @@ class FaustCoreCpp
//FaustCoreCpp(): transform(){} //FaustCoreCpp(): transform(){}
void Display() const { transform.display();} void Display() const { transform.display();}
const char* to_string() const;
void push_back(FPP* valueMat,unsigned int nbrow,unsigned int nbcol); void push_back(FPP* valueMat,unsigned int nbrow,unsigned int nbcol);
void push_back(FPP* data, int* row_ptr, int* id_col, int nnz, int nrows, int ncols); void push_back(FPP* data, int* row_ptr, int* id_col, int nnz, int nrows, int ncols);
unsigned int getNbRow() const; unsigned int getNbRow() const;
......
...@@ -149,6 +149,12 @@ double FaustCoreCpp<FPP>::get_nb_factors() const ...@@ -149,6 +149,12 @@ double FaustCoreCpp<FPP>::get_nb_factors() const
return nb_fact; return nb_fact;
} }
template<typename FPP>
const char* FaustCoreCpp<FPP>::to_string() const
{
return this->transform.to_string().c_str();
}
template<typename FPP> template<typename FPP>
unsigned int FaustCoreCpp<FPP>::get_fact_nb_rows(unsigned int& i) const unsigned int FaustCoreCpp<FPP>::get_fact_nb_rows(unsigned int& i) const
{ {
......
...@@ -43,6 +43,7 @@ cdef extern from "FaustCoreCpp.h" : ...@@ -43,6 +43,7 @@ cdef extern from "FaustCoreCpp.h" :
cdef cppclass FaustCoreCpp[FPP] : cdef cppclass FaustCoreCpp[FPP] :
FaustCoreCpp() FaustCoreCpp()
void Display() const void Display() const
const char* to_string() const
void push_back(FPP* valueMat,unsigned int nbrow,unsigned int nbcol) void push_back(FPP* valueMat,unsigned int nbrow,unsigned int nbcol)
void push_back(FPP* data, int* row_ptr, int* id_col, int nnz, int nrows, void push_back(FPP* data, int* row_ptr, int* id_col, int nnz, int nrows,
int ncols) int ncols)
......
...@@ -45,7 +45,7 @@ cimport numpy as np ...@@ -45,7 +45,7 @@ cimport numpy as np
import copy import copy
from libc.stdlib cimport malloc, free; from libc.stdlib cimport malloc, free;
from libc.string cimport memcpy; from libc.string cimport memcpy, strlen;
from libcpp cimport bool from libcpp cimport bool
from libcpp cimport complex from libcpp cimport complex
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
...@@ -270,6 +270,16 @@ cdef class FaustCore: ...@@ -270,6 +270,16 @@ cdef class FaustCore:
else: else:
self.core_faust_cplx.Display() self.core_faust_cplx.Display()
def to_string(self):
cdef const char* c_str
if(self._isReal):
c_str = self.core_faust_dbl.to_string()
else:
c_str = self.core_faust_cplx.to_string()
cdef length = strlen(c_str)
py_str = c_str[:length].decode('UTF-8')
return py_str
def nnz(self): def nnz(self):
cdef unsigned long long nnz = 0 cdef unsigned long long nnz = 0
if(self._isReal): if(self._isReal):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment