Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

Using Decimal library results in compilation errors

Open microwavenby opened this issue 5 years ago • 4 comments

I've a need for accurate decimal values in my code, and thus attempted to utilize Python's Decimal module, but compilation fails when I attempt to run Transcrypt on it.

from decimal import Decimal
class Example:
    def __init__(self, value=Decimal('1.00')):
        self.value=value

My command line param is transcrypt -bn example.py

Dropping use of Decimal from this example compiles to JavaScript successfully.

Python 3.7.6 (default, Dec 30 2019, 19:38:26) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin

MacOS Catalina (10.15.2) Macbook Pro 13-inch 2016

output.txt

microwavenby avatar Feb 04 '20 20:02 microwavenby

I would like to emphasize this request.

I am trying to make a library for validation on both client and server: https://github.com/nandoflorestan/cojanver How great would it be to use the same code for validation on both sides of HTTP?

One of the hurdles is the decimal module. It does not transpile to JS yet because it depends on the locale module which needs itertools.chain().

nandoflorestan avatar Mar 23 '20 12:03 nandoflorestan

Transcrypt doesn't support 3rd party packages and only part of the standard library modules.

Generaly, compatibility goes like this (best to worst):

  • Syntax and built-ins works (there are some edge cases).
  • Specific features, such as kwargs and magic methods, may need pre-compiler switches.
  • Standard library modules support is limited.
  • 3rd party modules are not supported unless they conform with the previous restriction, using 3rd party javascript library is prefered.

To see a list of supported standard library features see section 4 (toward the end) at https://transcrypt.org/docs/html/index.html.

There are different reasons why not all standard library modules are supported, but it is usually because they require Python/CPython specific assets or features that do not exist in a JS engine. You can try adding -xp insert_full_path\python\Lib to force transpilation of required std modules (they are excluded by default), but it will probably fail because of the aforementioned reason. You are better of using a JS library.

AlexECX avatar May 13 '20 14:05 AlexECX

because it depends on the locale module which needs itertools.chain().

An itertools transcrypt module that includes .chain() has been made, so that shouldn't be the issue. What I see is that locale.py has import _bootlocale that imports _locale. I can't find a _locale module anywhere in my python directories, which leads me to believe it is either an external asset or a compiled .dll/.lib.

AlexECX avatar May 13 '20 14:05 AlexECX

Transcrypt will never support all of the standard modules, since many of them are tied to desktop use or at least local use. Still we seek to expand the nr of standard libs supported.

Since there exists a JS module supporting decimals, supporting it's Python equivalent shouldn't be undoable.

https://github.com/MikeMcl/decimal.js/

But I dare not give an estimate about if and when it will be available. We really could use some help on expanding the nr of modules available for Transcrypt.

JdeH avatar Nov 02 '20 09:11 JdeH