In my py-builtins.js file, I have implemented a "js" function, that
takes any Python object and converts it to a javascript object. This
is necessary, so that I can write code like this in Python:
items = [
{ "icon":
'http://www.extjs.com/deploy/dev/examples/menu/list-items.gif', "cls":
'x-btn-icon',
"handler": toolbar_mesh1,
"tooltip": '<b>Draw Mesh I</b><br/>Show an example mesh' },
{ "icon":
'http://www.extjs.com/deploy/dev/examples/menu/list-items.gif', "cls":
'x-btn-icon',
"handler": toolbar_mesh2,
"tooltip": '<b>Draw Mesh II</b><br/>Show an example mesh' },
{ "icon":
'http://www.extjs.com/deploy/dev/examples/menu/list-items.gif', "cls":
'x-btn-icon',
"handler": toolbar_mesh3,
"tooltip": '<b>Draw Mesh III</b><br/>Show an example mesh' },
]
_new(Ext.Toolbar, js({"renderTo": 'mesh-editor', "items": items}))
where items is a tuple() instance, which contains dict() instance as
it's elements. The js() function converts this into a javascript
array, so that ExtJS is happy.
Is this what your js() functions is doing?
The big problem is that javascript {} syntax is not a dictionary. It
only assigns properties to an object, which means that it only accept
strings as keys. However, in Python, I need to use other keys than
just strings.
> Yes. Good point. We want on the JS side to be able to pass ordinaryI agree, that is absolutely necessary.
> JavaScript objects to third party code.
code, after we merge it. I spent last couple days to make more testsWe don't support modules yet, which I would like to just use your
work, so I have implemented str() and except formatting (%), all
things with strings work fine now. I also need to implement class
inheritance and couple other things, but then it will be hopefully
ready for you to seriously look at it and see what's the best way to
merge it.
I really appreciate the feedback and questions that you ask on the
way. I really like the rich py-builtins.js library approach (that we
all need, and thus maintain) and then all of us can maintain the exact
translator, that one needs (e.g. more or less python compatibility, vs
javascript readability).
While reading and gettting familiar with your py-builtins.js, I
noticed that it is a bit more lowlevel than our approach, e.g. no
list(), no str(), no iter() and so on. What is your view on it?
On Mon, May 3, 2010 at 10:33 AM, Peter Rust <pe...@cornerstonenw.com> wrote:
> Thanks for the thinking and the discussion about the differences between JS
> objects and Python objects. I'm thinking we'll want to support 3 basic modes
> in py2js:
>
> (1) Syntax Only. This mode wouldn't wrap [] with list() or {} with dict()
> and would therefore create the cleanest Javascript, but you would be writing
> code against the Javascript built-ins (Array, Object, etc), not the Python
> ones. Basically the only thing it would give you is the clean Python syntax.
> In this mode, I could see it being useful to have a class in Python that has
> simulates the behavior of Javascript objects.
And potentially we can use this mode to write the py-builtins.js
itself. Later on.
> (2) Almost Everything (everything but operator overloading). In this mode,
> you would be writing against the Python built-ins and would depend on
> py-builtins.js to provide those same constructs and functionality in
> Javascript. It would throw an error if it encountered a class that tried to
> overload the operators.
> (3) Everything. This would translate "=", "+", "*", etc into __eq__(),Yes. In fact, I already translate == into __eq__ and "if x:" into "if
> __mul__(), etc and be fairly verbose and not as pretty, but could
> potentially translate any arbitrary Python code into Javascript.
(py.bool(x)) {", it just makes things easier to work with and it
allowed me to fix some problems with strings. As you'll see yourself,
after you implement the str(), you run into all kinds of problems, for
example:
>> (2) Almost Everything (everything but operator
overloading). In this mode,
>> you would be writing against the Python built-ins and would depend on
>> py-builtins.js to provide those same constructs and functionality in
>> Javascript. It would throw an error if it
encountered a class that tried to
>> overload the operators.
>I'm not exactly sure what this means. We certainly
want to have 'builtins' available when we are doing
JavaScript >programming, even if we're 'writing the JavaScript in Python'.
This is my preferred mode – the one I anticipate using most of the time and the one I would like to “market” on the wiki. By “writing against the Python built-ins”, I simply mean that you’d be writing code like “my_list.append(…)” in your python source file as opposed to mode 1, in which you’d write code like “my_array.push(…)” in your python source file.
By not attempting to handle 100% of the issues, we can generate much more readable code. Things start getting bloated and unrecognizable when “if protocol + domain == 'http://www.basecamphq.com'” becomes “if (protocol.__add__(domain).__eq__('http://www.basecamphq.com'))”.
Mode 2 will really focus on generating readable code even when that is at the expense of being able to translate 100% of the python files that exist (it would throw an exception if, for instance, the python source was a class that overloaded an operator).
As Ondrej has pointed out, this gets really tricky when it gets to the falseness of empty strings and using the plus operator for string concatenation. In order the support these, I will need to either (a) decorate each individual string instance with additional methods when it’s passed to str() and return the string itself, rather than a wrapper object or (b) add properties to String.prototype, which I’ve been warned against. At this point, I think I’ll go with (a).
Again, this is just for Mode 2 (Peter’s mode) – I think Ondrej has already started down the path of converting “+” to __add__() and “==” to “__eq__” – this is what Mode 3 would do.
My advice at this point is to focus hard on Mode 2 and Mode 3 – these are the ones we have immediate needs for and can easily figure out. I think it’ll be real hard to have decent visibility on our needs for Mode 1 before we have Modes 2 and 3 working, plus it’ll be distracting. I’d rather not muddy the waters with Mode 1 discussions until we’ve got Modes 2 and 3 nailed down…
-- peter
From: js...@googlegroups.com [mailto:js...@googlegroups.com] On
Behalf Of Jonathan Fine
Sent: Monday, May 03, 2010 2:03 PM
To: js...@googlegroups.com
Subject: Re: A Python implementation of JavaScript objects
On Mon, May 3, 2010 at 9:34 PM, Ondrej Certik <ond...@certik.cz> wrote:
Yes, I agree. I would also concentrate on mode 2 and mode 3 and
especially on keeping the translator as simple as possible, e.g. doing
most of the stuff in the py-builtins.js library (that would support
both mode 2 and mode 3).
Then it will be trivial to have a simple switch in the translator, if
it should generate mode 2 or mode 3 code.