Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Errors while trying to use a complex pure python package #2356

Open
Manusan42 opened this issue Jan 20, 2024 · 3 comments
Open

Errors while trying to use a complex pure python package #2356

Manusan42 opened this issue Jan 20, 2024 · 3 comments

Comments

@Manusan42
Copy link

Hello there,
it's my first contact with Brython and I'm stuck trying to get a certain package running.

Goal

Using the music21-package.
(within a worker and inside a nuxt/vuejs project)

Short backstory

I have successfully integrated the package with polyscript/pyodide, the only problem is, compared to it's CPython execution, the operations are mostly 5 times slower. Most operations (per api call) take already 200+ms with CPython, which results in consistently 1+ seconds for any operation, which is unacceptable. Before I give up and build a server API, I wanted to try a Python to Javascript transpiler and Brython seems the most mature.

What I tried so far and my current problems

I have a devcontainer with python 3.12 and used the brython-cli tool to install a demo app environment.

I use music21 with minimum required packages (without matplotlib or numpy). I checked, and it seems the package itself and all required packages provide pure python wheels, thus should not require c extensions.

As music21 is a large package with many modules, I tried to use the make_package command, which gave an error. Thus I decided to get it first running with just using the standard mechanism.

So I used add_package for music21 and it's required packages and their required packages (I'm not sure if there is a simpler way as to manually trying to check for dependencies of dependencies as add_package seems to be just a copy operation).

The first problem I had here, the package more-itertools could not be added with add_package more-itertools but with add_package more_itertools which might by normal.

Then I added some code using the music21-package to the index.html and set the debug-level to 2.
Then used the dev server with start_server.

Then, I guess, a compiler error happend?

brython.js:15192 Traceback (most recent call last):
  File "http://127.0.0.1:8042/#__main__", line 2, in <module>
    from music21.stream import Score, Part, Measure, Voice
  File "http://127.0.0.1:8042/Lib/site-packages/music21/__init__.py", line 176, in <module>
    from music21 import base  # noqa: E402
  File "http://127.0.0.1:8042/Lib/site-packages/music21/base.py", line 52, in <module>
    from music21 import common
  File "http://127.0.0.1:8042/Lib/site-packages/music21/common/__init__.py", line 44, in <module>
    from music21.common.classTools import *  # including isNum, isListLike
  File "http://127.0.0.1:8042/Lib/site-packages/music21/common/classTools.py", line 46, in <module>
    def isNum(usrData: t.Any) -> t.TypeGuard[t.Union[float, int, Fraction]]:
ImportError: cannot import name 'base' from 'music21' (http://127.0.0.1:8042/Lib/site-packages/music21/__init__.py)

So the parser seems to have a problem with def isNum(usrData: t.Any) -> t.TypeGuard[t.Union[float, int, Fraction]]:?
The relevant context in the mentioned class:

from __future__ import annotations

from collections.abc import Iterable, Collection
import contextlib
import typing as t

if t.TYPE_CHECKING:
    from fractions import Fraction

# ...

def isNum(usrData: t.Any) -> t.TypeGuard[t.Union[float, int, Fraction]]:
    '''...'''
   # ...

Is this already the end of my journey, and the parser is in general not capable to parse this kind of function definition?
Or what might be the next steps to find out what the actual cause is?

Best regards,
Manuel

@PierreQuentel
Copy link
Contributor

Hello,

The commit referenced above fixes a bug related to annotations.

But after fixing it, there is another issue: the package imports module xml, which is not supported by Brython because the CPython module uses a library written in C, and there is no pure-Python replacement.

@PierreQuentel
Copy link
Contributor

I am currently working on a Javascript version of this C library, I will update this issue if I succeed in making it work.

@Manusan42
Copy link
Author

Wow, thank you very much that you looked into it in detail. I really appreciate it.
Indeed I didn't thought about the python standard libs.
And great news that you work on the xml module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants