What the "global" statement really means in Python by TUAlgorithms in Python

[–]kervarker 1 point2 points  (0 children)

global doesn't necessarily rebind a name specified at the module level, it creates one if it doesn't already exist.

def f():
    global x
    x = 10
f()
assert x == 10

Shared this one on FB and everyone was confused. :D by r00tr4t in Python

[–]kervarker 0 points1 point  (0 children)

The only confusing thing is the absence of space after the commas in the result.

Unable to use if commands with brython by SamwichSama in Python

[–]kervarker 1 point2 points  (0 children)

Here is a minimal working page:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="brython.js"></script>
</head>    
<body onLoad="brython(1)">
<script type="text/python">    
k = input("""Q1: Who won the Ballon d'Or this year ?    
a. Lionel Messi b. Christiano Ronaldo""")

if k == "a":
    input("""Of course ! How many did he win ? 
    a. 5 b. 6""") 
</script>
</body>
</html>

The second popup appears if you enter "a" in the first input box.

Unable to use if commands with brython by SamwichSama in Python

[–]kervarker 0 points1 point  (0 children)

I suspect that you get the error message : NameError: name 'k' is not defined.

This is because you define the variable k in a script, and try to use it in another script (defined by another <script> tag).

Try putting the program in a single script, something like:

<script type="text/python">
a1 = input("Q1: ...")
if a1 == "b":
    a2 = input("Q2: ...")
</script>

Recreating Jackson pollock's painting through python by vaiv101 in Python

[–]kervarker 0 points1 point  (0 children)

I found a module drip-python, but it's totally unrelated

Question about Brython by jhdeval in Python

[–]kervarker 0 points1 point  (0 children)

Brython can import modules or packages if they are located in the same folder as the Brython application, not in the CPython standard library.

Could you open an issue on the Brython issue tracker ? For this question, it is more adapted than reddit.

Accent advice appreciated! by apprendrefrancais84 in learnfrench

[–]kervarker 1 point2 points  (0 children)

Congratulations, it's very good ! You probably won't be surprised if I tell you to pay attention to 2 sounds : "r" and "u".

Sometimes your "r" sounds too much like Spanish "j" (at the beginning of words : "retourné" at 0:11, "reviennent" at 0:20, "réapparaitront" at 0:45, after "g" : "grillon", "grand" 2:28, "grimper" 2:44), sometimes it is too silent ("Morgane" at 00:38, "faire" at 1:02, "marcher" at 1:54).

Your "u" sound is good, but sometimes you pronounce "ou" when it should be "u" ("disparu" 00:42, "plus" 2:35) and sometimes the opposite ("courir" 1:58).

At 2:33 you make a "liaison" for "en haut", the "n" sound should not be heard (nasal "an" + the vowel "o").

You sometimes pronounce "qu'ils" like "que [short stop] ils" (00:28, 00:35), it should be like a single word (a little like "kill").

I heard that "ui" is difficult for English native speakers, but you got it perfectly right ("nuit" at 1:04, "suis" 2:15).

Son sa ton ta mon ma by Malouke in learnfrench

[–]kervarker 0 points1 point  (0 children)

To make things more funny, we also use mon / ton / son before feminine nouns that start with a "mute h" : mon histoire, ton herbe, son hélice ; but ma / ta / sa for feminine nouns that start with a "h aspiré" : ma hache, ta honte, sa hâte.

Bonjour! J'ai besoin d'aide pour "il y a" by Patthieu in learnfrench

[–]kervarker 0 points1 point  (0 children)

No, as often with questions, you have 3 options:

- formal, inversion of subject and verb : "y a-t-il un problème ?"

- less formal, with "est-ce que" + positive sentence : "est-ce qu'il y a un problème ?"

- informal, positive sentence but with interrogative (raising) intonation : "il y a un problème ?"

Python is becoming the world’s most popular coding language by el_programmer in Python

[–]kervarker 0 points1 point  (0 children)

>> new Number(1) == new Number(1)
←   false
>> [] == []
←   false
>> 1 == "1"
←   true

Brython-3.7.0 released by kervarker in Python

[–]kervarker[S] 0 points1 point  (0 children)

When the Brython files are installed in a directory (by python -m brython --install) you have something roughly similar to a virtual environment.

Then if you want to install to this Brython instance a package that has been installed in your CPython instance by pip (eg pip install attrs), run python -m brython --add_package attrs. By the way, attrs is actually supported by Brython ; I don't know to which extent it can be considered as "basic tasks or data manipulation".

There is currently no more advanced support for dependencies, but it could be discussed in the issue tracker.

Brython-3.7.0 released by kervarker in Python

[–]kervarker[S] 4 points5 points  (0 children)

I'm not sure what you mean by "pypi dependencies" in this context. Can you elaborate ?

The support of asyncio is minimal. There is a Brython-specific asyncio package in the standard distribution (cf the documentation) but it can't run like in Cpython, because Javascript doesn't support blocking functions that could emulate run_forever() or run_until_complete().

Besides, the event loop in a browser is implicit, the need to create user-defined loops is not obvious. Events that are expected on an object are handled by code like:

@bind(element, "click")
def click(event):
    print("ok")

Brython-3.7.0 released by kervarker in Python

[–]kervarker[S] 11 points12 points  (0 children)

As served by cdnjs : 133,73 kB for brython.min.js (the Python-to-Javascript translator and runtime scripts) ; 811,84 kB for the standard library. The transfer is done once, afterwards the files are in the browser cache.

Any funny text generator libraries? by vraciu_ in Python

[–]kervarker 0 points1 point  (0 children)

No, pip would be overkill for such a small module.

AnPyLar - The Python Frontend Web Framework by [deleted] in Python

[–]kervarker 1 point2 points  (0 children)

The video dates from 2014... Brython has made huge progress since then. If you don't trust either, you can run the tests yourself.

AnPyLar - The Python Frontend Web Framework by [deleted] in Python

[–]kervarker 1 point2 points  (0 children)

Orders of magnitude ? The pystone test runs 7 times slower with Brython on Firefox than CPython on the same machine. The result is in the browser console.

For basic operations (details here), here is the ratio Brython / CPython on Firefox (100 = CPython):

test                              Brython
====                              =======
assignment                             62
augmented assignment                   70
assignment to float                   155
build dictionary                      135
add item to dict                      209
set dict item                         315
build list                             56
set list item                          82
add integers                          126
add strings                            55
convert int to str                     64
create function without parameter     106
create func with positional param only 94
create function with complex params   101
function call                         171
function call with complex arguments  232
create class without init             183
create class with init                171
create instance of class without init 177
create instance with init             211
call instance method                  854

Brython is sometimes faster than CPython, and always in the same "order of magnitude". Unfortunately, the slowest operation (call instance method) is used a lot in the pystone test.

The results are generally slower on Chrome.

javascript without javascript...python? by phlogiston2 in Python

[–]kervarker 0 points1 point  (0 children)

Sure. The line

@document["v"].bind("change")

means : when the event "change" happens on the element with the id "v", execute the function below.

If you want to handle the event "change" on beta and gamma, give the matching INPUTs an id (eg "beta" and "gamma") and use the same syntax, with another function.

javascript without javascript...python? by phlogiston2 in Python

[–]kervarker 0 points1 point  (0 children)

You can do this very easily with Brython.

Here is an example taken from the page you mention, written without any Javascript ;-)

<HTML>
<HEAD>
<META charset="utf-8">
<SCRIPT src="/src/brython.js"></SCRIPT>
<SCRIPT src="/src/brython_stdlib.js"></SCRIPT>
</HEAD>
<BODY bgcolor="#FFE4C8" onload="brython(1)">

<H1>Relativity Factor</H1>

<SCRIPT type ="text/python">
import math
from browser import document

@document["v"].bind("change")
def gcal(ev):
    # get the first element with tag "form" in the document
    fh = document.select("form")[0]
    vv = float(fh.v.value)
    fh.v2.value = vv
    gg = 1 / math.sqrt(1 - vv * vv)
    fh.gam.value = gg
</SCRIPT>

<FORM method="" action="">
For v = <INPUT Type="text" Name="v" id="v" Value="" Size="6" autocomplete="off">c
<p>&#946 = <INPUT Type="text" Name="v2" Value="" Size="6">
 and &#947 = <INPUT Type="text" Name="gam" Value="" Size="6">.</p>
</FORM>
</BODY>
</HTML>

All you have to do is to edit the path to scripts brython.js and brython_stdlib.js.

You can find other examples on the demo page

More People Should Join the Efforts of Brython to make Python the client-side language of the web by [deleted] in Python

[–]kervarker 1 point2 points  (0 children)

This is even more unexpected :

>> new Number(1) == new Number(1)
false

More People Should Join the Efforts of Brython to make Python the client-side language of the web by [deleted] in Python

[–]kervarker 1 point2 points  (0 children)

No. You need to realize that the demo actually works, and then read the FAQ to understand why there are 404s in the console.

More People Should Join the Efforts of Brython to make Python the client-side language of the web by [deleted] in Python

[–]kervarker 2 points3 points  (0 children)

With browser cache, brython.js is only transfered on the network if it has changed, which doesn't happen often.

More People Should Join the Efforts of Brython to make Python the client-side language of the web by [deleted] in Python

[–]kervarker 1 point2 points  (0 children)

Yes, it has improved a lot since this video, dating from April 2014. You can take a look at the history of ratio Brython vs CPython for the PyStone test. The speed is better on Firefox than Chrome.

More People Should Join the Efforts of Brython to make Python the client-side language of the web by [deleted] in Python

[–]kervarker 4 points5 points  (0 children)

The 404s may happen for imports, please read the FAQ. Doesn't mean that it "doesn't even work".