code-google-com / pyv8

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

Return context.eval() result as Python dict #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
First of all, pyv8 is awesome fast! Thank you for providing this wrapper.

I stumbled upon a problem with the result of the contect.eval() method. It 
returns a JSArray instead 
of a Python dict. Is there a way to convert this JSArray object into a dict? 
Wrapping the result with 
the dict() function ends in a segfault.

I even haven't found a way to list all keys of the JSArray to iterate over this 
object and create a dict 
by myself. I would like to convert the pyv8 result to a json. Maybe there is 
already an easy way to do 
this?

Thanks in advance and keep this great piece of code up.

Marc

Original issue reported on code.google.com by marc.boe...@gmail.com on 5 May 2009 at 7:56

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm not sure what's your real problems, could you give me some example or unit 
test code?

In fact, _PyV8.JSArray support the python Sequence Protocol. It means, you 
could 
use its predefined methods such as __iter__, __len__ or __getitem__ etc. 

>>> list(ctxt.eval("new Array(1, 2, 3)"))
[1, 2, 3]                                
>>>                                      

On the other hand, javascript doesn't have build-in dictionary, even you could 
create object with JSON syntax, such as

>>> r = ctxt.eval("var r = {'a' : 1, 'b' : 2}; r ")
>>> r
<_PyV8.JSObject object at 0x00AE9170>
>>> dir(r)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__float__', '__ge
tattr__', '__getattribute__', '__hash__', '__init__', '__int__', '__js__', '__me
mbers__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__red
uce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'a', 'b']
>>> r.a
1
>>> r.b
2

Original comment by flier...@gmail.com on 5 May 2009 at 4:02

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
hi flier,

thanks for pointing this out. it wasn't clear for that i can use the builtin 
__members__ to get all attributes to 
iterate over.

i've written a small function that converts a cx.eval()-result into a python 
dict. maybe someone needs this 
too.

cheers marc

Original comment by marc.boe...@gmail.com on 5 May 2009 at 11:04

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks, I have merged your contribution in pyv8.py

http://code.google.com/p/pyv8/source/detail?r=123

on the other hand, I add some code to simulate a dict in JSObject class

    def testDict(self):
        import UserDict

        with JSContext() as ctxt:
            obj = ctxt.eval("var r = { 'a' : 1, 'b' : 2 }; r")

            self.assertEqual(1, obj.a)
            self.assertEqual(2, obj.b)

            self.assertEqual({ 'a' : 1, 'b' : 2 }, dict(obj))            

            self.assertEqual({ 'a': 1,
                               'b': [1, 2, 3],
                               'c': { 'str' : 'goofy',
                                      'float' : 1.234,
                                      'obj' : { 'name': 'john doe' }},                                      
                               'd': True,
                               'e': None },
                             convert(ctxt.eval("""var x =
                             { a: 1,
                               b: [1, 2, 3],
                               c: { str: 'goofy',
                                    float: 1.234,
                                    obj: { name: 'john doe' }},
                               d: true,
                               e: null }; x""")))

Original comment by flier...@gmail.com on 6 May 2009 at 3:45