gauravssnl / skulpt

Automatically exported from code.google.com/p/skulpt
Other
0 stars 0 forks source link

Python to Javascript type conversion #30

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
It would be nice to have "on the fly" arg conversion for standard python
types when calling pure-js functions.

Code you're trying to run:
myJsFunction('test')
or
myJsFunction({'test1': 'test2', 'test3': 'test4'})

I get python objects in the js instead of js str or objects.

A workaround (ugly) for strings is to append a ".v" to the value, for example :
myJsFunction('test'.v)

For dicts, it's a bit more complicated as we would need to create an empty
object, use the iter$ and use the repr of the keys and the .v of values...

Anyway, a standard python-to-js types conversion would be nice :)

Something like __to_js__ in the types classes ?

Original issue reported on code.google.com by jonathan.schemoul on 8 Sep 2009 at 8:18

GoogleCodeExporter commented 8 years ago
Unfortunately, there's not an obvious mapping from all Python types to a JS 
type. 
That's the reason that there is the different .v types in the first place.

Seems useful for str and list though, I'll try to come up with a useful way to 
do 
something here.

Original comment by sgraham on 9 Sep 2009 at 12:02

GoogleCodeExporter commented 8 years ago
if you are interested here is a working dict to js function (implemented in js):
jsdict = function(dict) {
    var ret = {};
    dict.iter$(function(k, v){
        ret[repr(k).v] = v.v;
    });
    return ret;
}

Original comment by jonathan.schemoul on 9 Sep 2009 at 12:52

GoogleCodeExporter commented 8 years ago
Two other snippets that work around some of this problems for me are:

Object.prototype.__setattr__ = function(attr, value) { this[attr] = value; }
Str$.prototype.toString = function() { return this.v; };

Original comment by cesar.iz...@gmail.com on 15 Mar 2010 at 7:45

GoogleCodeExporter commented 8 years ago
vvbvnvv

Original comment by fortheco...@gmail.com on 1 Nov 2012 at 8:40