News for hackers. Stuff that matters.
Re:Brython – A Python 3 implementation for client-side web p... by linkdd on Monday July 06, 2020 @08:34AM (#23747190)
What is this monstrosity in the tutorial ?

>>> document <= 'Hello!' # write in the DOM

Since when the __le__ method has any side effects?

So to sum it up:

- it runs inside the Javascript VM

- you change the syntax by breaking the semantics

- the import mechanisms is completely different

Is it still Python then ?

NB: Still, it's a fun project and hope you had fun making it.

Re:Brython – A Python 3 implementation for client-side web p... by wenc on Monday July 06, 2020 @10:54AM (#23748556)
Looks like the FAQ covers this question:

https://brython.info/static_doc/en/faq.html

Quote:

Q: why use the operator <= to build the tree of DOM elements? This is not pythonic!

A: Python has no built-in structure to manipulate trees, ie to add "child" or "sibling" nodes to a tree node. For these operations, functions can be used; the syntax proposed by Brython is to use operators: this is easier to type (no parenthesis) and more readable

To add a sibling node, the operator + is used.

To add a child, the operator <= was chosen for these reasons:

* it has the shape of a left arrow; note that Python function annotations use a new operator -> that was chosen for its arrow shape

* it looks like an augmented assignment because of the equal sign

* it can't be confused with "lesser or equal" because a line with document <= elt would be a no-op if it was "lesser or equal", which is always used in a condition or as the return value of a function

* we are so used to interpret the 2 signs < and = as "lesser or equal" that we forget that they are a convention for programming languages, to replace the real sign ≤

* in Python, <= is used as an operator for sets with a different meaning than "lesser or equal"

* the sign < is often used in computer science to mean something else than "lesser than": in Python and many other languages, << means left shift; in HTML tags are enclosed with < and >

* Python uses the same operator % for very different operations: modulo and string formatting

Re:Brython – A Python 3 implementation for client-side web p... by linkdd on Monday July 06, 2020 @01:04PM (#23749904)
I'll try to explain why I strongly disagree with the FAQ:

> this is easier to type (no parenthesis) and more readable

When 99% of the open-source codebase uses "<=" as lesser-than-equal, no it is not more readable to change that.

> It has the shape of a left arrow

Then, why not `child >> tree` ? or `tree << child`

> it looks like an augmented assignment because of the equal sign

No, it looks like a comparison. This is totally subjective.

> it can't be confused with "lesser or equal" because [...] would be a no-op

Wrong, the value of _ variable contains the result of the last expression. What value can i expect here?

> we are so used to interpret the 2 signs < and = as "lesser or equal" that we forget that they are a convention for programming languages

I agree, and trying to be subversive won't change that convention.

> in Python, <= is used as an operator for sets with a different meaning

The implementation is different, but the math are the same. The "<", "<=", "==", "!=", ">=", ">" operators are comparison operators.

"setA < setB" has the same meaning as "numberA < numberB", aka: a comparison between two object in an ordered space.

> the sign < is often used in computer science to mean something else than "lesser than", [...], << means left shift; in HTML tags are enclosed with < and >

You are talking about a character not an operator.

> Python uses the same operator % for very different operations: modulo and string formatting

This PEP ( https://www.python.org/dev/peps/pep-0292/ ) tries to replace % as an operator, because it should not be used for string formatting.

Re:Brython – A Python 3 implementation for client-side web p... by corytheboyd on Monday July 06, 2020 @12:01PM (#23749261)
Not at all a Python user, but I don’t get how this is a valid argument at all:

> Python has no built-in structure to manipulate trees

What’s wrong with an API like appendChild etc? I know you’re just quoting the FAQ but if anyone has an answer I am legitimately curious. I have worked with Ruby for many years and loath cute DSLs but I always thought it was just the Ruby community that went overboard on that shit haha

Re:Brython – A Python 3 implementation for client-side web p... by dragonwriter on Monday July 06, 2020 @12:47PM (#23749732)
> What’s wrong with an API like appendChild etc?

Verbosity vs. fluency, given the application domain, I expect is the concern. I'd rather have fluent DSL separate from the basic language runtime, though, so that adopting it is a separate decision from using the runtime.

Re:Brython – A Python 3 implementation for client-side web p... by bruxis on Monday July 06, 2020 @03:16PM (#23751632)
+1, would have much rather seen a function-based interface for DOM manipulation than this monstrosity.
Re:Brython – A Python 3 implementation for client-side web p... by divbzero on Monday July 06, 2020 @06:55PM (#23753712)
I agree with GP on this.

Alternatives to <= that could be more Pythonic:

  document.append(node)

  node >> document
which parallel

  # list append
  list().append(item)

  # print to stream in Python 2
  print "example" >> sys.stderr
Re:Brython – A Python 3 implementation for client-side web p... by red_hare on Monday July 06, 2020 @09:48AM (#23747801)
I will never understand python API developers obsession with overloading the operator magic methods.

Apache Beam is one of the worst offenders to me. The recommended method is to connect your functional transforms when building your pipeline as if it were a series Bash commands piped together (overloading bitwise OR). Who thought “oh, you know what language and API is synonymous with ‘write a large complicated amount of business logic in me’, Bash.”

Re:Brython – A Python 3 implementation for client-side web p... by pyuser583 on Monday July 06, 2020 @10:08AM (#23747987)
Your comment made me smile because Apache Beam is one of my favorite tools, but it seems nobody outside Spotify has had heard of it.

The bitwise operation dunder methods have no definition outside of integers. So they’re basically freebies to do what you want with.

Honestly I think the Apache Beam API makes a lot of sense.

Re:Brython – A Python 3 implementation for client-side web p... by red_hare on Monday July 06, 2020 @04:21PM (#23752294)
To be clear, I love Beam. It runs all of my startups stream and batch processing.

I just think the Java API's version...

    pipeline = pipeline
      .apply("FirstTransform, MyFirstParDo())
      .apply("SecondTransform, MySecondParDo())
is much clearer than the Python version...

    pipeline = (
        pipeline
        | "FirstTransform" >> MyFirstParDo()
        | "SecondTransform" >> MySecondParDo()
    )
Re:Brython – A Python 3 implementation for client-side web p... by pyuser583 on Monday July 06, 2020 @11:20PM (#23755371)
Agreed. I prefer the Java version, for a number of reasons. Scio is even better. But the bitwise operations make sense python wise.
Re:Brython – A Python 3 implementation for client-side web p... by pushingice on Monday July 06, 2020 @10:20AM (#23748147)
This is because it started out as a Google project. There is an internal time-series framework there called Monarch that does the same thing for composing operations.
Re:Brython – A Python 3 implementation for client-side web p... by heavyset_go on Monday July 06, 2020 @11:37AM (#23749021)
I have to agree with this sentiment.
Re:Brython – A Python 3 implementation for client-side web p... by patrickserrano on Monday July 06, 2020 @10:33AM (#23748292)
Oh man, I ran into that when I made my first contribution to our internal Airflow project and was just like wtf is happening.
Re:Brython – A Python 3 implementation for client-side web p... by recov on Monday July 06, 2020 @11:28AM (#23748928)
To be fair it's pretty nice when you get used to it as I doubt you're going to be using bit shifts much.
Re:Brython – A Python 3 implementation for client-side web p... by red_hare on Monday July 06, 2020 @04:24PM (#23752329)
It's fine. But there are some unintuitive edge cases.

Like, for example, if you have tasks A, B, C, and D you can do...

    A >> (B, C) >> D
but not...

    (B, C) >> A >> D
you have to write...

    D << A << (B, C)
Re:Brython – A Python 3 implementation for client-side web p... by mjburgess on Monday July 06, 2020 @08:56AM (#23747369)
It's still python; you can overload operators and give them side-effects in any conforming implementation.

It may not be "pythonic", but there's an established tradition of operator methods and there's no requirement that python code conform to any particular tradition.

Re:Brython – A Python 3 implementation for client-side web p... by stoksc on Monday July 06, 2020 @09:18AM (#23747552)
Yeah, overloading to build a little dsl over some objects sort of is something I’ve seen a lot. See airflow:

https://stackoverflow.com/questions/52389105/how-operator-de...

When it’s used that way, you’ve lost that the operation is semantically analogous to __le__. I think the assumption it has no side effects goes, too.

Edit: not saying I like it, though.

Re:Brython – A Python 3 implementation for client-side web p... by duckerude on Monday July 06, 2020 @09:48AM (#23747800)
Overloading comparison operators like this is a particularly bad idea in Python, because of the "smart" behavior they have baked in. Overloading >> is comparatively safe.

Unless you implement __ge__, `'Hello!' >= document` will also call __le__, which may or may not be what you want. And `'Hello!' >= document <= 10` is equivalent to `'Hello!' >= document and document <= 10`, which can lead to confusing surprises.

(Brython does implement all of those pitfalls faithfully)

Re:Brython – A Python 3 implementation for client-side web p... by linkdd on Monday July 06, 2020 @09:03AM (#23747423)
Sorry, what I meant was: Is it the Python you want in your browser?

So, this is just another implementation with its tradeoffs, just like when using Jython to have Python (without its ecosystem) and the JVM. Here you have Python with another semantic to learn.

Re:Brython – A Python 3 implementation for client-side web p... by VectorLock on Monday July 06, 2020 @09:20AM (#23747567)
I see this happening in a lot of Python libraries. It's still Python - or *ython maybe we can call it. The idiom of cutesy operator overloaded spread in from other languages; I'm personally not a fan.
Re:Brython – A Python 3 implementation for client-side web p... by LockAndLol on Monday July 06, 2020 @12:39PM (#23749636)
Oof... yeah, that's terrible and not intuitive at all. appendChild or innerHTML = ... should be enough. That override is pretty unnecessary.
Re:Brython – A Python 3 implementation for client-side web p... by nickthemagicman on Monday July 06, 2020 @12:40PM (#23749659)
I think it looks great!