Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre StupidSheet (publicaciones antiguas, página 1)

A *real* programming challenge.

A long time ago, I wrote a piece about how I did­n't like kcal­c. It con­tained a very lame pyqt script show­ing a (IMHO) nicer cal­cu­la­tor. Strange­ly, that lead to two very cool im­ple­men­ta­tions of the con­cep­t!

One of them was writ­ten in Ruby, the oth­er one in C++. I think that has some po­ten­tial.

A few months lat­er, I wrote a spread­sheet based on the same con­cep­t. Al­so based on PyQt.

This Stupid­Sheet has some con­cep­tu­al prob­lem­s. Like, if you want to im­port Ex­cel sheet­s, you would have to re­write ba­sic in python, so it's not a prac­ti­cal pro­gram, but it is a rather nice ex­am­ple show­ing pro­gram­ming us­ing dy­nam­ic lan­guages.

In fac­t, I used it as such last week at Cafe­Con­f.

Now, here's the chal­lenge. If peo­ple that know how to write Ru­by or Ja­va apps us­ing Qt (or KDE, why not) could write a sim­i­lar ap­pli­ca­tion, we all could write a com­par­a­tive guide to Qt/KDE pro­gram­ming on dif­fer­ent lan­guages.

Since we would all be start­ing with a not-­too-­com­plex, but re­al­ly non-triv­ial ex­am­ple, and we would all do the same one, it should be pret­ty un­bi­ased.

In fac­t, if you think this ex­am­ple is bi­ased, please pro­pose an­oth­er one, and do this thing any­way.

You can find Stupid­Sheet here

It has some small bugs (try set­ting B1 to A1+1 with no val­ue in A1 ;-) but they are easy to fix.

We could re­move some fea­tures (like the weird past­ing stuff) to make the ex­am­ple more di­dac­tic.

I hope this gets some an­swers :-)

CafeConf 2005

Nue­va­mente este año voy a es­tar en Cafe­Conf . Es una char­la de 45 min­u­tos so­bre PyQt el 13 de oc­tubre al mediodía.

La idea: La gente se sor­prende cuan­do uno agar­ra KHTM­L, en­gan­cha un par de wid­gets y sale con un nave­g­ador we­b. Se de­be­ri­an sor­pren­der más de que uno puede par­tir de una re­c­eta de 20 líneas en una pági­na web y ter­mi­nar con una planil­la que fun­ciona ;-)

Por lo tan­to, voy a mostrar Stupid­Sheet co­mo un ejem­p­lo de que el de­sar­rol­lo de soft­ware con in­ter­face grá­fi­ca es mas facil de lo que la gente cree.

Co­mo siem­pre, si men­cionás es­ta pági­na, te ganás una cerveza. Máx­i­mo 2 cerveza­s, no muy bue­nas.


I will be at Cafe­Conf again this year. It's a 45-minute thing about PyQt in Oc­to­ber 13th, at noon.

My idea is: Peo­ple are amazed when you hook Khtml to a cou­ple of wid­gets and write a lame web brows­er. They should be more amazed that it is pos­si­ble to start with noth­ing more than a 20-­line recipe from a web­site and end with a func­tion­al spread­sheet ;-)

So, I will be show­ing Stupid­Sheet as an ex­am­ple of how writ­ing GUI soft­ware in Python is sim­pler than peo­ple think.

As usu­al, if you men­tion this page, you get a free beer, max­i­mum 2 beer­s, and not very good beer.

This is why dynamic languages are cool

I wrote a lit­tle spread­sheet thingie a few days ago. [1]

Of course, it's a toy, not the re­al thing at al­l, but it was a nice hack, since it is a re­al, re­cal­cu­lat­ing, ex­ten­si­ble, de­pen­den­cy-check­ing, loop-avoid­ing spread­sheet en­gine in about 50 lines of code.

That's be­cause I was us­ing Python, which is a se­ri­ous­ly cool lan­guage to write that kind of thing in, since all you have to do to eval­u­ate an ex­pres­sion is call eval() on it.

Sure, that's nice, but the re­al core of the spread­sheet en­gine was that you could al­so cre­ate a dic­tio­nary-­like ob­ject that re­cal­cu­lat­ed on-de­mand its con­tents.

That way, when you ask for sheet['a1'], cus­tom code goes to see what a1 has in it (a for­mu­la), cal­cu­lates it if need­ed, and maybe trig­ger a few ex­tra re­cal­cu­la­tions if an­oth­er cell de­pends on a1. [2]

But as any­one who us­es spread­sheets can tell you, weird things ex­ist in ssheet land.

For ex­am­ple, if you copy some­thing, then you paste it, it gets mod­i­fied in the process.

What oth­er app does that???

Here's an ex­am­ple you can check in any spread­sheet:

  • In A1, type "1".

  • In B1, type "A1+1" (should dis­­­play 2)

  • In A2, type 2

  • Copy B1 to B2, and it will dis­­­play 3

Fur­ther, if you look at the for­mu­la in B2, it says A2+1 now.

That's called rel­a­tive cell­names (I think).

In or­der to do that trick, you have to parse the for­mu­la in B1, and then, when you paste it in­to B2, take in­to ac­count the dis­place­ment and mod­i­fy ac­cord­ing­ly. Usu­al­ly, if you want ab­so­lute names, you use $ A1 in­stead, and that would stay un­mod­i­fied.

Now, that throws a nice mon­key­wrench in­to my neat lit­tle spread­sheet [3] be­cause now it sud­den­ly looks not like a spread­sheet at al­l!

So, I start­ed think­ing, how the hell could this be done? The whole ad­van­tage of a python sheet is us­ing eval(), so switch­ing to a pars­er (like if this were a C[++] sheet) would be sil­ly.

I delved in­to the python stan­dard lib. As ev­ery python pro­gram­mer knows, al­most ev­ery­ht­ing is there. If you write python, you read the li­brary ref­er­ence ev­ery day, and mem­o­rize chunks of it, be­cause it's one of the things that make python cool. It's just chock­full of use­ful stuff!

And here I was read­ing about the com­pil­er mod­ule, and the pars­er mod­ule, which can be used to do won­drous stuff with python code. But I could­n't un­der­stand jack­shit about them. I'm a sim­ple coder.

And just as I was go­ing to say, let's write in­stead about the con­nec­tion be­tween free soft­ware and the sex life of frogs [4] I found to­k­enize.

To­k­enize is a mod­ule that pars­es python and turns it in­to to­ken­s. Here's how a+2 looks af­ter you to­k­enize it:

1,0-1,1:        NAME    'a'
1,1-1,2:        OP      '+'
1,2-1,3:        NUMBER  '2'
2,0-2,0:        ENDMARKER       ''

The num­bers on the left side are po­si­tions in the text stream where the to­kens were.

It has just enough in­for­ma­tion that you can to­k­enize a piece of code, and then re­assem­ble it. There's code to do just that, it's called re­gur­gi­tate and it's writ­ten by Ka-Ping Yee.

So, the so­lu­tion is ob­vi­ous. When copy­ing a for­mu­la:

  • To­k­­enize the for­­mu­la to be copied

  • Look for to­kens of type NAME

  • See if it looks like a cel­l­­name, or _cel­l­­name

  • If it's _cel­l­­name, leave as is. That will be our no­­ta­­tion for ab­­so­­lute cells

  • If it's cel­l­­name, dis­­­place it nice­­ly

  • Re­gur­gi­­tate it

Lat­er, when eval­u­at­ing a for­mu­la, if some­one asks for cell _a1 give him cell a1.

And voilà, rel­a­tive cell­s.

This work­s, and it works well (ok, I had to in­tro­duce some ug­ly glob­al­s, I need to learn more stuff), and it is guar­an­teed to to­k­enize in the same way python does it. It's not even re­al­ly slow [5]

I touched a bunch of oth­er things, in­clud­ing sup­port for all the func­tions in python's math mod­ule so you can use them in cell­s. Here's the code to do that:

for name in dir(math):
        if name[0]<>"_":
                self.tools[name]=eval('math.'+name)

Freaky stuff, is­n't it?

What's the main is­sue? Per­for­mance. To put it sim­ply, I se­ri­ous­ly doubt a sheet writ­ten in python can be fast enough for gen­er­al use. But hey, it's ex­ten­si­ble, it's nice, and de­pend­ing on what you are try­ing to do, it may be good enough.

And here's to­day's ver­sion of Stupid­Sheet in­clud­ing rel­a­tive cell­s. Don't wor­ry, it's a small down­load ;-)

Not a calculator

I have been play­ing with this code and it's been lots of fun.

I've hacked it in­to a func­tion­al spread­sheet in (ac­cord­ing to er­ic3) 508 lines of non-­doc code, of which 244 are gen­er­at­ed by pyuic.

Here's my code so far (re­quires PyQt). Give it a look, I think it's kin­da nice.

The on­ly hard part I wrote (at least hard for me) was the cell de­pen­den­cy and re­cal­cu­la­tion sup­port.

There's a test file you can use, too.

It is triv­ial to add func­tions you can use in the cell­s, just lookup python docs for eval() and check en­gine.py.

To use it, unpack it, and from the directory it creates run python ssheet.py

I don't plan to make it a re­al spread­sheet, but it should be fun to hack on :-)

Novell-Ximian-Suse´s future

Lots of peo­ple, when they heard of this merg­er, start­ed think­ing like this:

Ok, so Nov­ell bought Ximi­an. Ximi­an is a GNOME com­pa­ny. They al­so bought Suse. Suse is a dis­tro. Er­go, Suse will be­come a Ximi­an-ori­ent­ed dis­tro.

Well, that makes some sense. But they seem to be for­get­ting of a change that should hap­pen much soon­er, be­cause it:

  1. Is eas­i­er to do

  2. Makes more sense

  3. Costs less mon­ey

What change? Well, the changes that Ximi­an will have to face for now be­ing part of a com­pa­ny that owns a dsitro!

Let´s look at Ximi­an´s prod­uct­s:

  • Xim­i­an con­nec­­tor: I don´t see this chang­ing, ex­­cept for they adding a group­­wise con­nec­­tor.

  • Evo­lu­­tion: I can see Nov­ell push­ing Evo­lu­­tion as a Group­­wise clien­t, for ex­am­­ple. So, this has some legs. I don´t see it be­­com­ing a ma­jor rev­enue source for Nov­el­l, though, so it´s not go­ing to get a large push.

  • Gnu­mer­ic: I see an axe in its fu­­ture. Nov­ell is not in the spread­­sheet busi­­ness. They owned Quat­tro Pro on­ce, did­n´t they?

  • Red Car­pet. I have heard Nov­ell re­al­­ly wan­t­ed this. They have Zen­­Work­s, I sup­­pose some in­­te­­gra­­tion is in or­der, and this will be pro­­duc­­tized. How­ev­er, I don´t think Nov­ell will want to push it as a soft­­ware dis­­tri­bu­­tion mech­a­nism for their com­pe­ti­­tion, at least not while it´s free. If they did, Nov­ell will be giv­ing away what they bought. My guess? It will lose sup­­port for Red Hat, and will be­­come more and more pro­pri­e­tary as time pass­es.

  • Xim­i­an set­up tool­s: I am doubt­ing be­tween the axe and pro­pri­e­tary lock­­­down. Why would Nov­ell want to sup­­port con­­fig­u­ra­­tion tools for their com­pe­ti­­tion? On the oth­­er hand, YaST is bet­ter for their own pro­­duc­t, so why keep this at al­l?

  • Mono: I can see they wan­t­ing this for their own tool de­vel­op­­men­t, and to make a push in­­­to the de­vel­op­­ment tool mar­ket, but Nov­ell has re­al­­ly no foothold there. So, while they will keep itr and push it, it´s not ex­ac­t­­ly a guar­an­­teed suc­cess (then again, noth­ing is).

  • Xim­i­an Gnome: I have no idea.

Al­so, pol­ish­ing the crys­tall ball a lit­tle, if Nov­ell has some cash­flow prob­lems in the next year, they will fo­cus hard­er in what they re­al­ly care about.

My guess is set­up tools will be­come SuSE-on­ly or SuSE-­first, Red Car­pet will be ab­sorbed or re­brand­ed in­to Zen­Works or some­thing, Mono will be mar­ket­ed as .NET plat­for for Lin­ux as soon as they get Forms work­ing, the rest will be "lib­er­at­ed" fe­do­ra-style.

Now, let´s put a re­minder to read this again in 2005 ;-)


Contents © 2000-2024 Roberto Alsina