Skip to main content

Ralsina.Me — Roberto Alsina's website

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 ;-)

David Boddie / 2006-04-04 13:49:

Is this a BPython challenge?! ;-)



I imagine that the hard part would be the BPython parser but, once done, maybe you could then write an import hook to make Python access .bpy files transparently.

Roberto Alsina / 2006-04-04 13:50:

Pretty much. Maybe I should offer a reward for the smallest bpython ;-)



Remember python comes with the batteries!



For example, here is the tokenize.tokenize output for this function:



def x(y):

return y



2,0-2,3: NAME 'def'

2,4-2,5: NAME 'x'

2,5-2,6: OP '('

2,6-2,7: NAME 'y'

2,7-2,8: OP ')'

2,8-2,9: OP ':'

2,9-2,10: NEWLINE 'n'

3,0-3,1: INDENT 't'

3,1-3,7: NAME 'return'

3,8-3,9: NAME 'y'

3,9-3,10: NEWLINE 'n'

4,0-4,0: DEDENT ''

4,0-4,0: ENDMARKER ''



And here is for the bpython version:



1,0-1,1: NL 'n'

2,0-2,3: NAME 'def'

2,4-2,5: NAME 'x'

2,5-2,6: OP '('

2,6-2,7: NAME 'y'

2,7-2,8: OP ')'

2,8-2,9: OP ':'

2,9-2,10: NEWLINE 'n'

3,0-3,3: COMMENT '#{n'

4,0-4,6: NAME 'return'

4,7-4,8: NAME 'y'

4,8-4,9: NEWLINE 'n'

5,0-5,3: COMMENT '#}n'

6,0-6,0: ENDMARKER ''



I am pretty sure converting one into the other should be simple ;-)



Look for indents or dedents, remove them.

Look for the comments and replace with indent or dedent as necessary.



Then regurgitate it (google for regurgitate tokenize python ;-)

Ian Monroe / 2006-04-04 13:57:

Is the whole braces vs. spaces thing something that people who use python a lot don't like? I don't really see that, its more just something that gives a bad first impression. Which doesn't seem like a valid complaint really... it'd be similar to not buying a house because it has a dead garden.



I rather like that it forces all python code to have logical indentation.



I do however dislike python syntax. Like I could point out needing to explicitly specify 'self' in class methods, or the messy super calls in PyKDE that I still don't understand, but really its because I'm infatuated with Ruby. ;)

Matt Harrison / 2006-04-04 13:58:

I've had a similar thought, replace any colons in pythons with "{" and put "}" in the correct place. ie



def some_function(foo, bar):

for i in range(10):

print i, foo, bar



would become:



def some_function(foo, bar){

for i in range(10){

preint i, foo, bar

}

}



(looks a lot like js)

Is there a reason why you need to put comments in? I guess the python tokenizer might have issues with mine, but you could easier write a translater that converts them on the fly.



How many people seriously get hung up on the white space issue? The only times it has been an issue for me is:

1. When people mix tabs and spaces

2. Makes using python as a web programming language a bit harder (when you have deeply nested html and need to have consistent python spacing)

Is it that confusing otherwise?

Roberto Alsina / 2006-04-04 13:59:

I put comments in it because x={} is valid python syntax :-)


Contents © 2000-2024 Roberto Alsina