Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre python (publicaciones antiguas, página 17)

C is not Python

I am port­ing pyspf to C (long sto­ry, and I am stupid for try­ing). But of course, C is not python.

So you don't have anything nearly as nice as re.­com­pile("what­ev­er").s­plit("­somestring").

What is that good for, you may ask? Well, to do things like split­ting email ad­dress­es while val­i­dat­ing them, or in this spe­cif­ic case, to val­i­date SPF mech­a­nisms (n­ev­er­mind what those are).

But hey, you can al­ways do this (ex­cuse me while I weep a lit­tle):

struct bstrList *re_split(const char *string, const char *pattern)
{
    int status;
    regex_t re;
    regmatch_t pmatch[20];

    if (regcomp(&re, pattern, REG_ICASE|REG_EXTENDED) != 0)
    {
        return(0);      /* Report error. */
    }

    bstring tmp=bfromcstr("");
    char *ptr=(char *)string;

    for (;;)
    {
        status = regexec(&re, ptr, (size_t)20, pmatch, 0);
        if (status==REG_NOMATCH)
        {
            break;
        }
        bcatblk (tmp,ptr,pmatch[0].rm_so);
        bconchar (tmp,0);
        bcatblk (tmp,ptr+pmatch[0].rm_so,pmatch[0].rm_eo-pmatch[0].rm_so);
        bconchar (tmp,0);
        ptr=ptr+pmatch[0].rm_eo;

    }
    regfree(&re);
    bcatblk (tmp,ptr,strlen(string)-(ptr-string));
    struct bstrList *l= bsplit(tmp,0);
    return l;
}

And that is prob­a­bly wrong for some cas­es (and it does­n't split the ex­act same way as Python, but that's what unit test­ing is for).

I must be miss­ing some­thing that makes reg­comp & friends nicer to use. Right? Right?

Any regex wizard reading this?

If so, what is the C POSIX regex (y­ou know reg­comp & friend­s) equiv­a­lent of this python reg­u­lar ex­pre­sion:

re.compile(r'^([a-z][a-z0-9_\-\.]*)=', re.IGNORECASE)

Be­cause it sure is­n't this:

regcomp(&re,"^([a-z][a-z0-9_\-\.]*)=",REG_ICASE)

I have been play­ing with it for two hours and am bored :-)

Itching.

Ok, the SPF im­ple­men­ta­tion sit­u­a­tion is kin­da pa­thet­ic.

There seems to be ex­act­ly one main­tained C im­ple­men­ta­tion. And it's win­dows-on­ly.

  • lib­spf's we­b­site seems to have dis­­ap­­peared

  • lib­spf2's not RFC-­­com­­pli­ant (ver­i­­fied for 1.2.5) and their is­­sue re­­port­ing sys­tem bounces.

So, I have tak­en the most com­pli­ant one I found whose code I can ac­tu­al­ly fol­low (that would be the python one) and am reim­ple­ment­ing it in C (us­ing bstr­lib and lib­d­jbdns).

It will prob­a­bly not come to a good end, but hey, it may work ;-)

To the other three guys (or gals)....

... who own a HP Jor­na­da 720 and are us­ing Opie on it and they have the span­ish/lat­in-amer­i­can key­board­... here is your keymap.

I will write some­thing about how to get Lin­ux go­ing right on it soon, but here's the sta­tus re­port, 48 hours in.

This ba­by (un­named yet) has:

  • 32MB of RAM

  • 1 GB of Flash

  • Wifi (802.11b pcm­­ci­a) + IR­­DA + Eth­er­net (pcm­­ci­a) + Any­thing once I find a 16-bit pcm­­ci­a-USB card (any­one has a spare and wants to re­­cy­­cle it? ;-)

  • De­­cent bat­tery life (6 hours use with wifi, 9 with­­out)

  • A key­board

  • A de­­cent screen (640x240)

  • A de­­cent Lin­ux-based GUI (Opie)

  • A some­what er­rat­ic touch­screen

So, what can I do with it:

  • Email

  • Web brows­ing ( With Kon­­queror good­­ness )

  • Pro­­gram­ming (Python, even PyQt2!). They key­board and screen are sur­pris­ing­­ly de­­cen­t.

  • eBook read­­ing. This is the most im­­por­­tant one. In my work, I spend a lot of time wait­­ing. Wait­­ing for the train to ar­rive, for the trip to end, for some­one to come to a meet­ing, for the wait­­er to bring my meal, for stuff to com­pile, for stuff to down­load­­... maybe I wait 3 hours a day. So I read. And this screen (long and some­what thin) is quite spec­­tac­u­lar for read­­ing. Opie-read­­er is pret­­ty good.

  • MP3 and Video play­er (haven't used it yet). I have stream­ing TV at home, cour­tesy of Cher­ryTV (check the links at the left­­). This should work great when Rosario wants to see Mon­te­cristo and I'd rather see Penn & Teller's show.

  • Gen­er­al PIM stuff. Al­though I tend to keep that stuff in my head and my phone.

The bad side:

  • The bizarre screen as­pect ra­­tio con­­fus­es many con­­fig­u­ra­­tion di­alogs.

  • Al­­most no game works un­­less you ro­­tate the screen.

  • The key­board con­­fig­u­ra­­tion took a while, and is not per­­fect yet ( I can't make dead­­_a­­cute work for some rea­­son)

  • The ex­­tra but­­tons don't work (ex­ter­­nal au­­dio recorder, and alarm light-but­­ton)

  • I can't find a way to bind the func­­tion keys to apps in Opie

  • The re­set but­­ton does­n't work (it's now a hang but­­ton)

  • Sus­pend is not re­al­­ly sus­pend on Lin­ux (for un­avoid­able hard­ware rea­­son­s), so it spends bat­tery when sus­pend­ed (may last 12 hours or so, I think).

  • The on­­ly way to re­al­­ly turn it off is to take out the bat­tery (not as bad as it sound­s).

  • If you do that, it takes about one minute to boot.

So, I am us­ing it more as a lap­top (although a re­al­ly, re­al­ly small one, with very, very good bat­tery life :-) than as a PDA.

The small mem­o­ry and CPU means I can't run very de­mand­ing stuff, but I nev­er seem to do that, any­way.

And of course, the re­al­ly bad thing: it's so much fun to hack with, I have trou­ble work­ing!

All in al­l, a great toy, lots of fun, and rather use­ful.

rst2rst works (80% or so)

What is it? A pro­gram that takes a do­cu­tils doc­u­ment tree ( parsed from a RST doc­u­ment or pro­gra­mat­i­cal­ly gen­er­at­ed) then dumps as close as I can guess to rea­son­able RST back.

This lets Re­struc­tured Text be a save­able da­ta for­mat, which is nice.

It's not done as a do­cu­tils writ­er. Sor­ry, I could­n't make that work.

What work­s? Most of it.

What does­n't? A dozen di­rec­tives, cus­tom in­ter­pret­ed text roles, and ta­bles.

Yes, all of those are im­por­tan­t. But the rest seems to work ok!

Look: a 804 line RST doc­u­ment con­tain­ing al­most ev­ery fea­ture of the lan­guage, and the on­ly dif­fer­ence in the gen­er­at­ed HTML out­put be­tween the orig­i­nal and rst2rst's is an in­vis­i­ble dif­fer­ence in con­tin­u­a­tion lines in line block­s.

[ralsina@monty wp]$ python rst2rst.py t1.txt > t2.txt
[ralsina@monty wp]$ /usr/bin/rst2html.py t1.txt t1.html ;  /usr/bin/rst2html.py t2.txt t2.html
[ralsina@monty wp]$ diff t1.html t2.html
468,469c468,469
< <div class="line">But I'm expecting a postal order and I can pay you back
< as soon as it comes.</div>
---
> <div class="line">But I'm expecting a postal order and I can pay you back</div>
> <div class="line">as soon as it comes.</div>
[ralsina@monty wp]$ wc -l t1.txt
804 t1.txt

You can get rst2rst.py and the test­file.

Any­one knows of a re­al do­cu­tils test suite I could bor­row?


Contents © 2000-2020 Roberto Alsina