metapensiero / metapensiero.pj

Javascript for refined palates: a Python 3 to ES6 Javascript translator
Other
901 stars 73 forks source link

Update snippets to use enums #79

Open System25 opened 1 year ago

System25 commented 1 year ago

When a class extends enum.Enum you have to define the properties at class level. Also the values need to be an object with "name" and "value" properties.

azazel75 commented 9 months ago

mmm.. Does JS supports Enums? Do you have a link to some docs?

System25 commented 9 months ago

Hi @azazel75, It is not a matter of having enums in Javascript, it is a matter of supporting Python Enums (https://docs.python.org/3/howto/enum.html).

So I have an Enum in Python like:

from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

It has to be transformed into something that allows me to use Color.RED in code. So "RED" has to be defined at class level.

The code is transformed into:

class Color extends Enum {
}
_pj.set_properties(Color, {"RED": 1, "GREEN": 2, "BLUE": 3});

So _pj.set_properties has to be aware that this is an enum so "RED", "GREEN" and "BLUE" are defined at class level.

Thanks