Friday, December 21, 2012

Python List comprehensions now in Brython

Brython improvements

So by now, you've heard of Brython, the implementation of Python 3 (or at least a subset) for your web browser, in Javascript. After all, I've been talking about it for almost a month now (although not in english).

New this week


Pierre has been busy... He implemented the ternary operator, that is, a way to assign one of two values, based on a condition, for example:

x = 7 if a > 2 else 17
List comprehensions are also in! This was a heavily requested feature that was missing from Brython, for example:

y = [ x ** 2 for x in range(10)]
 I did find a bug in some variations, and am looking into the code to fix this.

Another addition this week is that your python code indentation is now relative to the first line of your script, instead of having to start at the first position. That means you can now format your html code and have your Python properly aligned:

<!DOCTYPE html>
<html>
    <head>
        <title>Brython test</title>
        <script src="brython.js"></script>
    </head>
    <body onLoad="brython()">
        <script type="text/python">
        def myfunc():
            return True

        log(myfunc())
        </script>
    </body>
</html>

instead of:
        <script type="text/python">
def myfunc():
    return True

log(myfunc())
        </script>


2 comments:

James said...

Congrats! This is coming along nicely!

Perhaps it's time we get circuits + brython working in a Browser utilizing the full strength of components, loose coupling with a full messages passing system that ties into the DOM events.

Hmmm :)

--JamesMills / prologic

Unknown said...

When will you have an efficient implementation of dicts?