Rafael Cardoso Dias Costa | 1 Jul 2010 14:05
Picon

Re: Cython error :: UnicodeDecodeError

Stefan,

This is the response:

$ sudo patch Scanning.py filename-decoding.patch
patching file Scanning.py
Hunk #1 FAILED at 127.
1 out of 1 hunk FAILED -- saving rejects to file Scanning.py.rej
patching file Scanning.py
Hunk #1 FAILED at 479.
1 out of 1 hunk FAILED -- saving rejects to file Scanning.py.rej
patching file Scanning.py
Hunk #1 succeeded at 159 with fuzz 2 (offset -7 lines).
patching file Scanning.py
Hunk #1 FAILED at 66.
Hunk #2 FAILED at 80.
2 out of 2 hunks FAILED -- saving rejects to file Scanning.py.rej



"Scanning.py.rej" is attached in this message.

Thank you!







On Wed, Jun 30, 2010 at 5:07 PM, Stefan Behnel <stefan_ml <at> behnel.de> wrote:
Rafael Cardoso Dias Costa, 30.06.2010 21:49:

On Wed, Jun 30, 2010 at 4:21 PM, Stefan Behnel wrote:

Rafael Cardoso Dias Costa, 30.06.2010 20:46:

        print repr(self.get_description())
        if self._escaped_description is None:
            self._escaped_description = \
                self.get_description().encode('ASCII',
'replace').decode("ASCII")
        return self._escaped_description


'/home/rfcard/\xc3\x81rea de Trabalho/emso-python/Cython Test/hello.pyx'


Ok, this is an encoded byte string for the absolute file path. This is a
problem with the initial file path of the main source file in Python 2. All
related file paths, such as those from imported or included files, are
handled as unicode strings, thus the encoding code above.

I'll see if I can fix it.

Here's a patch - could you try it on your side?

Stefan



--
Rafael Cardoso Dias Costa
Engenheiro de Controle e Automação - UFMG
Mestrando em Engenharia Química - Controle de Processos - UFRGS

Attachment (Scanning.py.rej): application/octet-stream, 1601 bytes
Rafael Cardoso Dias Costa | 1 Jul 2010 14:09
Picon

Re: Cython error :: UnicodeDecodeError

Stefan,

"Scanning.py" is 0.12.1 file version no modified






On Thu, Jul 1, 2010 at 9:05 AM, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
Stefan,

This is the response:

$ sudo patch Scanning.py filename-decoding.patch
patching file Scanning.py
Hunk #1 FAILED at 127.
1 out of 1 hunk FAILED -- saving rejects to file Scanning.py.rej
patching file Scanning.py
Hunk #1 FAILED at 479.
1 out of 1 hunk FAILED -- saving rejects to file Scanning.py.rej
patching file Scanning.py
Hunk #1 succeeded at 159 with fuzz 2 (offset -7 lines).
patching file Scanning.py
Hunk #1 FAILED at 66.
Hunk #2 FAILED at 80.
2 out of 2 hunks FAILED -- saving rejects to file Scanning.py.rej



"Scanning.py.rej" is attached in this message.

Thank you!








On Wed, Jun 30, 2010 at 5:07 PM, Stefan Behnel <stefan_ml <at> behnel.de> wrote:
Rafael Cardoso Dias Costa, 30.06.2010 21:49:

On Wed, Jun 30, 2010 at 4:21 PM, Stefan Behnel wrote:

Rafael Cardoso Dias Costa, 30.06.2010 20:46:

        print repr(self.get_description())
        if self._escaped_description is None:
            self._escaped_description = \
                self.get_description().encode('ASCII',
'replace').decode("ASCII")
        return self._escaped_description


'/home/rfcard/\xc3\x81rea de Trabalho/emso-python/Cython Test/hello.pyx'


Ok, this is an encoded byte string for the absolute file path. This is a
problem with the initial file path of the main source file in Python 2. All
related file paths, such as those from imported or included files, are
handled as unicode strings, thus the encoding code above.

I'll see if I can fix it.

Here's a patch - could you try it on your side?

Stefan



--
Rafael Cardoso Dias Costa
Engenheiro de Controle e Automação - UFMG
Mestrando em Engenharia Química - Controle de Processos - UFRGS




--
Rafael Cardoso Dias Costa
Engenheiro de Controle e Automação - UFMG
Mestrando em Engenharia Química - Controle de Processos - UFRGS

Rafael Cardoso Dias Costa | 1 Jul 2010 19:01
Picon

typedef problem

Hi, guys!

How I translate in Cython the follow header statements?

/**
 * The EMSO reporting call back function prototype.
 * This function receives a string <at> a str and its length <at> a len.
 * The expected behavior is to print the given string somewhere and
 * return the number of characters actually printed.
 *
 * A very simple implementation which prints into the stdout could be:
 * <at> code
 * int_t simple_report(const char *str, int_t len){
 *     char buff[256];
 *     int_t ret = snprintf(buff, len, str);
 *     printf(buff);
 *     return ret;
 * }
 * <at> endcode
 */
typedef int_t(EMSO_REPORT)(const char *str, int_t len);


Thank you!

--
Rafael Cardoso Dias Costa
Control and Automation Engineer - UFMG
Chemical Engineering Master Student - Process Control - UFRGS

Lisandro Dalcin | 1 Jul 2010 19:28
Picon
Gravatar

Re: typedef problem

On 1 July 2010 14:01, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Hi, guys!
>
> How I translate in Cython the follow header statements?
>
> /**
>  * The EMSO reporting call back function prototype.
>  * This function receives a string  <at> a str and its length  <at> a len.
>  * The expected behavior is to print the given string somewhere and
>  * return the number of characters actually printed.
>  *
>  * A very simple implementation which prints into the stdout could be:
>  *  <at> code
>  * int_t simple_report(const char *str, int_t len){
>  *     char buff[256];
>  *     int_t ret = snprintf(buff, len, str);
>  *     printf(buff);
>  *     return ret;
>  * }
>  *  <at> endcode
>  */
> typedef int_t(EMSO_REPORT)(const char *str, int_t len);
>
>

cdef extern from *: #
    ctypedef int int_t
    ctypedef char const_char "const_char"
    ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
    EMSO_REPORT simple_report

--

-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Rafael Cardoso Dias Costa | 1 Jul 2010 20:06
Picon

Re: typedef problem

Works fine.

Thank you!

On Thu, Jul 1, 2010 at 2:28 PM, Lisandro Dalcin <dalcinl <at> gmail.com> wrote:
On 1 July 2010 14:01, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Hi, guys!
>
> How I translate in Cython the follow header statements?
>
> /**
>  * The EMSO reporting call back function prototype.
>  * This function receives a string <at> a str and its length <at> a len.
>  * The expected behavior is to print the given string somewhere and
>  * return the number of characters actually printed.
>  *
>  * A very simple implementation which prints into the stdout could be:
>  * <at> code
>  * int_t simple_report(const char *str, int_t len){
>  *     char buff[256];
>  *     int_t ret = snprintf(buff, len, str);
>  *     printf(buff);
>  *     return ret;
>  * }
>  * <at> endcode
>  */
> typedef int_t(EMSO_REPORT)(const char *str, int_t len);
>
>

cdef extern from *: #
   ctypedef int int_t
   ctypedef char const_char "const_char"
   ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
   EMSO_REPORT simple_report

--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169



--
Rafael Cardoso Dias Costa
Engenheiro de Controle e Automação - UFMG
Mestrando em Engenharia Química - Controle de Processos - UFRGS

Rafael Cardoso Dias Costa | 1 Jul 2010 21:23
Picon

Expose the c function to the Python environment

Guys,

I wish to expose the c function to the Python environment.
How do I?
I'm trying as follow, but is giving error.


cdef extern from "emso_dll.h":
    ctypedef long int_t "int_t"
    ctypedef char const_char "const_char"
    ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
    void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
        const_char *path_dll, int_t *ret_val)

cpdef py_emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
        const_char *path_dll, int_t *ret_val):
    emso_init(out, err, path, path_dll, ret_val)


Thank you!


--
Rafael Cardoso Dias Costa
Control and Automation Engineer - UFMG
Chemical Engineering Master Student - Process Control - UFRGS

Lisandro Dalcin | 1 Jul 2010 21:36
Picon
Gravatar

Re: Expose the c function to the Python environment

On 1 July 2010 16:23, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Guys,
>
> I wish to expose the c function to the Python environment. How do I?
> I'm trying as follow, but is giving error.
>
> cdef extern from "emso_dll.h":
>     ctypedef long int_t "int_t"
>     ctypedef char const_char "const_char"
>     ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
>     void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
>         const_char *path_dll, int_t *ret_val)
>
> cpdef py_emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
>         const_char *path_dll, int_t *ret_val):
>     emso_init(out, err, path, path_dll, ret_val)
>

This is a pretty standard C-wrapping task :-)

You cannot pass pointers from Python code, then you have to accept
Python callables, use a couple of global vars, and a wrapper C
function: more or less like below.

def object out_callable = None
def object err_callable = None

cdef int_t out_callback(const_char *s, int_t n):
    if out_callable is None: return 0
    ret = out_callable(s)
    if ret is None: return 0
    return ret

cdef int_t err_callback(const_char *s, int_t n):
    if err_callable is None: return 0
    ret = err_callable(s)
    if ret is None: return 0
    return ret

def init(path, path_dll, out=None, err=None):
    global out_callable, err_callable
    out_callable = out
    err_callable = err
    cdef int_t ret_val = 0
    emso_init(out_callback, err_callback, path, path_dll, &ret_val)
    return ret_val

Disclaimer: This was written in the browser, so is untested, I leave
the details to you.

--

-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Rafael Cardoso Dias Costa | 2 Jul 2010 15:14
Picon

Re: Expose the c function to the Python environment

Thank you, Lisandro!

I'm testing the code that you wrote.
I do not understand why Cython not doing the automatic conversion of types.
He gives the following error message:



Error converting Pyrex file to C:
------------------------------------------------------------
...
cdef object out_callable = None
cdef object err_callable = None

cdef int_t out_callback(const_char *s, int_t n):
   if out_callable is None: return 0
   ret = out_callable(s)
                      ^
------------------------------------------------------------

/home/rfcard/emso-python/CythonEMSO/emso.pyx:14:23: Cannot convert 'emso.const_char *' to Python object




I changed "def" with "cdef" in indicated lines.


cdef extern from "emso_dll.h":
    ctypedef long int_t "int_t"
    ctypedef char const_char "const_char"
    ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
    void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
        const_char *path_dll, int_t *ret_val)


cdef object out_callable = None  <==========================
cdef object err_callable = None   <==========================

cdef int_t out_callback(const_char *s, int_t n):
   if out_callable is None: return 0
   ret = out_callable(s)
   if ret is None: return 0
   return ret

cdef int_t err_callback(const_char *s, int_t n):
   if err_callable is None: return 0
   ret = err_callable(s)
   if ret is None: return 0
   return ret

def init(path, path_dll, out=None, err=None):
   global out_callable, err_callable
   out_callable = out
   err_callable = err
   cdef int_t ret_val = 0
   emso_init(out_callback, err_callback, path, path_dll, &ret_val)
   return ret_val



I await answers.
Thank you!


--
Rafael Cardoso Dias Costa
Control and Automation Engineer - UFMG
Chemical Engineering Master Student - Process Control - UFRGS







On Thu, Jul 1, 2010 at 4:36 PM, Lisandro Dalcin <dalcinl <at> gmail.com> wrote:
On 1 July 2010 16:23, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Guys,
>
> I wish to expose the c function to the Python environment. How do I?
> I'm trying as follow, but is giving error.
>
> cdef extern from "emso_dll.h":
>     ctypedef long int_t "int_t"
>     ctypedef char const_char "const_char"
>     ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
>     void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
>         const_char *path_dll, int_t *ret_val)
>
> cpdef py_emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
>         const_char *path_dll, int_t *ret_val):
>     emso_init(out, err, path, path_dll, ret_val)
>

This is a pretty standard C-wrapping task :-)

You cannot pass pointers from Python code, then you have to accept
Python callables, use a couple of global vars, and a wrapper C
function: more or less like below.

def object out_callable = None
def object err_callable = None

cdef int_t out_callback(const_char *s, int_t n):
   if out_callable is None: return 0
   ret = out_callable(s)
   if ret is None: return 0
   return ret

cdef int_t err_callback(const_char *s, int_t n):
   if err_callable is None: return 0
   ret = err_callable(s)
   if ret is None: return 0
   return ret

def init(path, path_dll, out=None, err=None):
   global out_callable, err_callable
   out_callable = out
   err_callable = err
   cdef int_t ret_val = 0
   emso_init(out_callback, err_callback, path, path_dll, &ret_val)
   return ret_val

Disclaimer: This was written in the browser, so is untested, I leave
the details to you.

--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169


Lisandro Dalcin | 2 Jul 2010 15:57
Picon
Gravatar

Re: Expose the c function to the Python environment

On 2 July 2010 10:14, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Thank you, Lisandro!
>
> I'm testing the code that you wrote.
> I do not understand why Cython not doing the automatic conversion of types.
> He gives the following error message:
>

Try to add explicit casts to <char*>, like this out_callable(<char*>s)

>
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
> cdef object out_callable = None
> cdef object err_callable = None
>
> cdef int_t out_callback(const_char *s, int_t n):
>    if out_callable is None: return 0
>    ret = out_callable(s)
>                       ^
> ------------------------------------------------------------
>
> /home/rfcard/emso-python/CythonEMSO/emso.pyx:14:23: Cannot convert
> 'emso.const_char *' to Python object
>
>
>
>
> I changed "def" with "cdef" in indicated lines.
>
>
> cdef extern from "emso_dll.h":
>     ctypedef long int_t "int_t"
>     ctypedef char const_char "const_char"
>     ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
>     void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
>         const_char *path_dll, int_t *ret_val)
>
>
> cdef object out_callable = None  <==========================
> cdef object err_callable = None   <==========================
>
> cdef int_t out_callback(const_char *s, int_t n):
>    if out_callable is None: return 0
>    ret = out_callable(s)
>    if ret is None: return 0
>    return ret
>
> cdef int_t err_callback(const_char *s, int_t n):
>    if err_callable is None: return 0
>    ret = err_callable(s)
>    if ret is None: return 0
>    return ret
>
> def init(path, path_dll, out=None, err=None):
>    global out_callable, err_callable
>    out_callable = out
>    err_callable = err
>    cdef int_t ret_val = 0
>    emso_init(out_callback, err_callback, path, path_dll, &ret_val)
>    return ret_val
>
>
>
> I await answers.
> Thank you!
>
>
> --
> Rafael Cardoso Dias Costa
> Control and Automation Engineer - UFMG
> Chemical Engineering Master Student - Process Control - UFRGS
>
>
>
>
>
>
>
> On Thu, Jul 1, 2010 at 4:36 PM, Lisandro Dalcin <dalcinl <at> gmail.com> wrote:
>>
>> On 1 July 2010 16:23, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
>> > Guys,
>> >
>> > I wish to expose the c function to the Python environment. How do I?
>> > I'm trying as follow, but is giving error.
>> >
>> > cdef extern from "emso_dll.h":
>> >     ctypedef long int_t "int_t"
>> >     ctypedef char const_char "const_char"
>> >     ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
>> >     void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path,
>> > \
>> >         const_char *path_dll, int_t *ret_val)
>> >
>> > cpdef py_emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path,
>> > \
>> >         const_char *path_dll, int_t *ret_val):
>> >     emso_init(out, err, path, path_dll, ret_val)
>> >
>>
>> This is a pretty standard C-wrapping task :-)
>>
>> You cannot pass pointers from Python code, then you have to accept
>> Python callables, use a couple of global vars, and a wrapper C
>> function: more or less like below.
>>
>> def object out_callable = None
>> def object err_callable = None
>>
>> cdef int_t out_callback(const_char *s, int_t n):
>>    if out_callable is None: return 0
>>    ret = out_callable(s)
>>    if ret is None: return 0
>>    return ret
>>
>> cdef int_t err_callback(const_char *s, int_t n):
>>    if err_callable is None: return 0
>>    ret = err_callable(s)
>>    if ret is None: return 0
>>    return ret
>>
>> def init(path, path_dll, out=None, err=None):
>>    global out_callable, err_callable
>>    out_callable = out
>>    err_callable = err
>>    cdef int_t ret_val = 0
>>    emso_init(out_callback, err_callback, path, path_dll, &ret_val)
>>    return ret_val
>>
>> Disclaimer: This was written in the browser, so is untested, I leave
>> the details to you.
>>
>> --
>> Lisandro Dalcin
>> ---------------
>> CIMEC (INTEC/CONICET-UNL)
>> Predio CONICET-Santa Fe
>> Colectora RN 168 Km 472, Paraje El Pozo
>> Tel: +54-342-4511594 (ext 1011)
>> Tel/Fax: +54-342-4511169
>
>
>

--

-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Felix Schlesinger | 2 Jul 2010 21:07
Favicon

Fast parsing

Hi,

I have a bunch of programs for which the slowest part is reading text-
based data into an internal datastructure. A simple situation might
look something like this:

for l in input:
    line = l.rstrip().split()
    foo = int(line[1]
    bar = float(line[2])
    data[foo] = bar

Where data might be a dict, a numpy array or even a pytables object
(hdf5). The processing can be more complex, but the files are almost
always in simple 'table' like formats.
Even very simple examples are often CPU and not disk-limited in
standard python. Does anyone here have experience with speeding such
loops up? Just dropping the whole thing into cython does not seem to
do much (probably because of all the python calls and conversions).
Does using C string routines help? Are there already fast parsers for
this kind of simple situation around? Just looking for some experience
from others before reinventing the wheel.

Thanks
  Felix


Gmane