Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

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

FaxWeb is done

FaxWe­b, a web fron­tend for mget­ty+send­fax is fin­ished. It work­s. It's prob­a­bly close to bugfree ;-)

The miss­ing piece is a nicer reim­ple­men­ta­tion of re­spond (and this one will be cross-­plat­for­m, too) us­ing PyQt, which is 50% done.

I am on­ly miss­ing how to im­ple­ment por­ta­ble systray icon­s. On Mac they make no sense, on Lin­ux I have it work­ing, on Win­dows I have no idea.

Here's the sim­ple in­ter­face for faxwe­b:

http://static.flickr.com/54/139230422_f453d38430_t.jpg http://static.flickr.com/50/139230433_0d87a2f0ae_t.jpg http://static.flickr.com/47/139230434_f91f8d0b01_t.jpg

It even has a lit­tle AJAXy "the page does­n't reload" niceties cour­tesy of MochiK­it!

Al­so from MochiK­it, the nicer, round­ed look&feel. Com­pare to this old­er, ugli­er one:

http://static.flickr.com/48/137206420_07ca331974_t.jpg

I know the new one is not good, ei­ther, but I have de­cid­ed that since I can't aim for awe­some, I should aim for ad­e­quate, and set­tle for bor­ing and harm­less.

Of course, if any CSS/X­HTML gu­ru vol­un­teers for a makeover, I'd be very hap­py, since I use the same CSS ev­ery­where (even on parts of this blog ;-).

All in al­l, a plea­sure to write this thing, thanks to Cher­ryPy!

BPython Lives!!!

In Jan­uary, I sug­gest­ed it would be triv­ial to write a pre­proces­sor that would ac­cept a ver­sion of python which de­lim­it­ed blocks with braces in­stead of in­den­ta­tion.

Ok, al­most, I sug­gest­ed #{ and #} as de­lim­iter­s.

Well, I had a few min­utes free to­day, and here it is, a func­tion­al BPython->Python com­pil­er, so to speak.

Here's the kind of im­put it takes (no­tice how it's not in­dent­ed at al­l:

def factorial(n):
#{
if n==1:
#{
return 1
#}
else:
#{
return n*factorial(n-1)
#}
#}

for x in range(1,10):
#{
print x,"!=",factorial(x)
#}

And it pro­duces this (I am not hap­py with the in­dent­ing of the com­ments, but who cares):

def factorial(n):
  #{
  if n==1:
    #{
    return 1
  #}
  else:
    #{
    return n*factorial(n-1)
  #}
#}

for x in range(1,10):
  #{
  print x,"!=",factorial(x)
#}

As you can see, this is both a le­gal Python and a le­gal BPython pro­gram ;-)

It has some prob­lem­s, like not work­ing when you use a line like this:

#{ x=1

But hey, it is python with braces.

Here's the code. I pre­dict­ed 30 lines. It's 34. And it's 99% a ripoff of Ka Ping Yee's re­gur­gi­tate.py which you can find all around the we­b.

#!/usr/bin/env python
import tokenize, sys
program = []
lastrow, lastcol = 1, 0
lastline = ''
indlevel=0
def rebuild(type, token, (startrow, startcol), (endrow, endcol), line):
    global lastrow, lastcol, lastline, program,indlevel
    if type==52 and token.startswith ("#{"):
            type=5
            indlevel+=1
    if type==52 and token.startswith ("#}"):
            type=6
            indlevel-=1
    line="  "*indlevel+line.lstrip()
    startcol+=indlevel*2
    endcol+=indlevel*2
    # Deal with the bits between tokens.
    if lastrow == startrow == endrow:            # ordinary token
        program.append(line[lastcol:startcol])
    elif lastrow != startrow:                    # backslash continuation
        program.append(lastline[lastcol:] + line[:startcol])
    elif startrow != endrow:                     # multi-line string
        program.append(lastline[lastcol:startcol])
    # Append the token itself.
    program.append(token)
    # Save some information for the next time around.
    if token and token[-1] == '\n':
        lastrow, lastcol = endrow+1, 0           # start on next line
    else:
        lastrow, lastcol = endrow, endcol        # remember last position
    lastline = line                              # remember last line
tokenize.tokenize(sys.stdin.readline, rebuild)
for piece in program: sys.stdout.write(piece)

So, all of you who dis­like python be­cause of the lack of braces and the sig­nif­i­cant whites­pace:

BPython has no sig­nif­i­cant whites­pace, and braces are manda­to­ry.

En­joy cod­ing!

Silly idea to make Python popular

I have an idea that can kill the most fre­quent com­plain about python.

BPython.

BPython is a sim­ple wrap­per around py­h­ton which pro­cess­es a .bpy file, pro­duces a .py file, then com­piles it in­to a .py­c.

What does it do? It us­es braces to con­trol flow.

Since braces are ac­tu­al python syn­tax, you will have to use #{ and #}

As an added bonus, if you are care­ful, a bpython pro­gram will al­so be a valid python pro­gram.

Of course it has is­sues (like mod­ule load­ing) but those can be worked around.

The im­ple­men­ta­tion should not be more than 30 lines of python. Or bpython ;-)

My first time

I just found here the an­nounce­ment of the first free soft­ware I pub­lished (at least, that I re­cal­l), from may 13, 1996. So, It's go­ing to be 10 years in 5 month­s!

Killer quote:

Requires:
Python 1.3 (Maybe 1.2 would work too)
XForms 0.80 (NOT 0.75)

Ok, here is how you write the shortest one

About http://www.py­con­test.net... here's how it's done.

Sad­ly, it's pret­ty much im­pos­si­ble to put the code on the web be­cause it has some re­al­ly non-ascii stuff in it ;-)

Here's the key tricks (thanks to Re­mi and the oth­er­s):

One of the key tricks is writ­ing a short ver­sion of a very long oc­tal num­ber (or three­).

I tried us­ing base36 and in­t('­gob­ledy­gook',36).

You could use 0x­hex­a­code­here.

But here's the most space-­ef­fi­cient one: use base 256!

For each byte you wan­t, cre­ate a char us­ing chr(byte). Then paste them all to­geth­er. Then put it in your code as a string. If you are lucky you will have no quotes or new­lines in it, and the python in­ter­preter will eat it.

You can lat­er get the byte num­ber x us­ing or­d('h!als­d­f'[x]), and each byte by some di­vid­ing and mod­u­lo op­er­a­tion.

An­oth­er im­por­tant piece is en­cod­ing 7 3-char­ac­ter strings as com­pact­ly as pos­si­ble, and split­ting them by off­sets (which are en­cod­ed in the pre­vio­su­ly men­tioned big num­ber in base 8 ;-)

One ex­tra squeeze is find­ing the short­est way to con­cate­nate strings.

It's ''.join(), but if you are us­ing it twice (or more), you save space by do­ing j=''.join and us­ing j lat­er.

Last one: In­stead of defin­ing a func­tion, do a lamb­da:

def sev­en_seg(x): re­turn .....

sev­en_seg=lamb­da x:

6 chars less!

And here is the short­est code I got so far (121 char­ac­ter­s, re­quires python 2.4. On 2.2, it has to be 124).

In the con­test, some guys are at 120. I don't know how. I can't even guess how ;-)

Up­date: it turns out I did know how to do it check it out I think it's the first pub­lic 120 :-)

BTW: you can't write that us­ing kwrite. Vim work­s, though.


Contents © 2000-2023 Roberto Alsina