Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PyScript list comprehensions don't work for Float64Array #5

Open
almarklein opened this issue Mar 21, 2018 · 0 comments
Open

PyScript list comprehensions don't work for Float64Array #5

almarklein opened this issue Mar 21, 2018 · 0 comments

Comments

@almarklein
Copy link
Member

@nathanielatom commented on Sat Aug 26 2017

Firstly, I'd like to say thank you because PyScript is a wonderful package and has helped boost my productivity quite often.

Observed behaviour:

import flexx.pyscript as ps
code = '''
def foo():
    """
    return new Float64Array([3.14, 0, -1, 0.57])
    """
def bar(arr):
    return [elem for elem in arr]
bar([3.14, 0, -1, 0.57]), bar(foo()), foo()
'''
ps.evalpy(code)
# "[ [ 3.14, 0, -1, 0.57 ],\n  [ '0', '1', '2', '3' ],\n  Float64Array [ 3.14, 0, -1, 0.57 ] ]"

Expected behaviour would be for the second call to match the third.

Coming from a python background, I wasn't aware of a distinction between Array and Float64Array, so this caught me off guard. For a workaround, I casted the Float64Array to an Array using [].slice.call(float_arr) before the list comprehension.

Python 3.6
Flexx 0.4.1


@almarklein commented on Tue Sep 05 2017

Thanks for reporting this. It is known behavior, at least by me :) but I can see how it can be unexpected. This is part of #332, I think.


@jburgy commented on Wed Feb 28 2018

FWIW, here's a slightly different broke-around:

from flexx.pyscript import evaljs, py2js
from re import sub

pycode = '''
def foo():
    """
    return new Float64Array([3.14, 0, -1, 0.57])
    """
def bar(arr):
    return [elem for elem in arr]
bar([3.14, 0, -1, 0.57]), bar(foo()), foo()
'''
jscode = sub(r'Array.isArray\(([^)]+)\)', r'(Array.isArray(\1) || ArrayBuffer.isView(\1))', py2js(pycode))
evaljs(jscode)
# "[ [ 3.14, 0, -1, 0.57 ],\n. [ 3.14, 0, -1, 0.57 ],\n  Float64Array [ 3.14, 0, -1, 0.57 ] ]"
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant