Silly idea to make Python popular
I have an idea that can kill the most frequent complain about python.
BPython.
BPython is a simple wrapper around pyhton which processes a .bpy file, produces a .py file, then compiles it into a .pyc.
What does it do? It uses braces to control flow.
Since braces are actual python syntax, you will have to use #{ and #}
As an added bonus, if you are careful, a bpython program will also be a valid python program.
Of course it has issues (like module loading) but those can be worked around.
The implementation should not be more than 30 lines of python. Or bpython ;-)
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.
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 ;-)
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. ;)
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?
I put comments in it because x={} is valid python syntax :-)