[Cython] Utility Codes and templates

Stefan Behnel stefan_ml at behnel.de
Fri Jul 22 18:04:44 CEST 2011


Dag Sverre Seljebotn, 22.07.2011 17:49:
> On 07/22/2011 05:46 PM, Dag Sverre Seljebotn wrote:
>> On 07/22/2011 03:07 PM, Stefan Behnel wrote:
>>> Stefan Behnel, 22.07.2011 15:04:
>>>> Dag Sverre Seljebotn, 22.07.2011 13:54:
>>>>> On 07/22/2011 01:10 PM, Stefan Behnel wrote:
>>>>>> I'm fine with using a template engine for the more involved cases
>>>>>> (which
>>>>>> are rare enough). However, I'd prefer not adding a new dependency, but
>>>>>> just shipping a tiny single-module engine with Cython, e.g.
>>>>>> Templite or
>>>>>> pyratemp (just found them, never used them).
>>>>>
>>>>> BTW, I don't think anybody is suggesting having an *external*
>>>>> dependency
>>>>> that users would need to go fetch themselves....that would be insane.
>>>>
>>>> Sure. I also don't mind having a small engine in a package, but just
>>>> putting a single file in the source tree (and overwriting it with a new
>>>> version at need) would be a perfectly small and nonintrusive addition.
>>>> And
>>>> given that there really are engines of that size (one file, <500 lines),
>>>> it's not so unrealistic either.
>>>>
>>>> That being said, if you can recommend Tempita (which IMHO qualifies as
>>>> "small enough"), I won't object to it.
>>>
>>> ... although, isn't it unfortunate that it uses "{{...}}" for template
>>> code? How well readable is that when used in C code?
>>
>> At least in emacs, pretty much any template syntax will mess up things
>> pretty badly, especially automatic indentation and so on.
>>
>> What I do is using mmm-mode, so that everything within {{ and }} is
>> highlighted with python-mode, and the rest with c-mode. I'll share my
>> configuration later.
>>
>> I'm merely stating why this has never been an issue for me, I'm NOT
>> suggesting everyone should use emacs (or that plain text editing isn't
>> convenient); you do have a point.
>>
>> Keep in mind that a syntax must work well with utility code written in
>> Cython as well.
>
> OTOH one *might* want to have
>
> code.put_tempita(..) # or similar
>
> in Python code, and in that case plain text readability is very important.
>
> Of course, hacking Tempita to make it %[ ]% instead is a five minute job.
> But I'm happy with any other engine that somebody have actually tried out
> extensively.

Tempita has a syntax for comments - what about using the regular C and 
Cython comments as template syntax delimiters? That way, you'd write

    /*# some comment */

to get an actual comment in C code, and

    /* for x in y */

for template code. For Cython code, we could use "##", or maybe "#<" and 
">#". That should work pretty well with editors.

Stefan


More information about the cython-devel mailing list