{"id":17166607,"url":"https://github.com/miso-belica/py.js","last_synced_at":"2026-01-27T16:06:09.854Z","repository":{"id":42485557,"uuid":"300556746","full_name":"miso-belica/py.js","owner":"miso-belica","description":"Python expressions parser and evaluator written in Javascript (https://github.com/odoo/odoo/tree/14.0/addons/web/static/lib/py.js)","archived":false,"fork":false,"pushed_at":"2025-11-22T07:34:28.000Z","size":627,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-22T09:14:07.736Z","etag":null,"topics":["eval","evaluator","javascript","parser","python","tokenizer"],"latest_commit_sha":null,"homepage":"https://openerp-web-v7.readthedocs.io/en/stable/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miso-belica.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-10-02T08:53:56.000Z","updated_at":"2025-11-22T07:34:31.000Z","dependencies_parsed_at":"2024-06-24T10:12:37.153Z","dependency_job_id":null,"html_url":"https://github.com/miso-belica/py.js","commit_stats":{"total_commits":68,"total_committers":24,"mean_commits":"2.8333333333333335","dds":0.7058823529411764,"last_synced_commit":"7f45562a085b8178336ba63c6613439090487d27"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/miso-belica/py.js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-belica%2Fpy.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-belica%2Fpy.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-belica%2Fpy.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-belica%2Fpy.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miso-belica","download_url":"https://codeload.github.com/miso-belica/py.js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-belica%2Fpy.js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28816514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T12:25:15.069Z","status":"ssl_error","status_checked_at":"2026-01-27T12:25:05.297Z","response_time":168,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eval","evaluator","javascript","parser","python","tokenizer"],"created_at":"2024-10-14T23:06:08.168Z","updated_at":"2026-01-27T16:06:09.837Z","avatar_url":"https://github.com/miso-belica.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"What\n====\n\n``py.js`` is a parser and evaluator of Python expressions, written in\npure javascript.\n\n``py.js`` is not intended to implement a full Python interpreter\n(although it could be used for such an effort later on), its\nspecification document is the `Python 2.7 Expressions spec\n\u003chttps://docs.python.org/2.7/reference/expressions.html\u003e`_\n(along with the lexical analysis part).\n\nSyntax\n------\n\n* Lambdas and ternaries should be parsed but are not implemented (in\n  the evaluator)\n* Only floats are implemented, ``int`` literals are parsed as floats.\n* Octal and hexadecimal literals are not implemented\n* Srings are backed by JavaScript strings and probably behave like\n  ``unicode`` more than like ``str``\n* Slices don't work\n\nBuiltins\n--------\n\n``py.js`` currently implements the following builtins:\n\n``type``\n    Restricted to creating new types, can't be used to get an object's\n    type (yet)\n\n``None``\n\n``True``\n\n``False``\n\n``NotImplemented``\n    Returned from rich comparison methods when the comparison is not\n    implemented for this combination of operands. In ``py.js``, this\n    is also the default implementation for all rich comparison methods.\n\n``issubclass``\n\n``object``\n\n``bool``\n    Does not inherit from ``int``, since ``int`` is not currently\n    implemented.\n\n``float``\n\n``str``\n\n``tuple``\n    Constructor/coercer is not implemented, only handles literals\n\n``list``\n    Same as tuple (``list`` is currently an alias for ``tuple``)\n\n``dict``\n    Implements trivial getting, setting and len, nothing beyond that.\n\nNote that most methods are probably missing from all of these.\n\nData model protocols\n--------------------\n\n``py.js`` currently implements the following protocols (or\nsub-protocols) of the `Python 2.7 data model\n\u003chttps://docs.python.org/2.7/reference/expressions.html\u003e`_:\n\nRich comparisons\n    Pretty much complete (including operator fallbacks), although the\n    behavior is currently undefined if an operation does not return\n    either a ``py.bool`` or ``NotImplemented``.\n\n    ``__hash__`` is supported (and used), but it should return **a\n    javascript string**. ``py.js``'s dict build on javascript objects,\n    reimplementing numeral hashing is worthless complexity at this\n    point.\n\nBoolean conversion\n    Implementing ``__nonzero__`` should work.\n\nCustomizing attribute access\n    Protocols for getting and setting attributes (including new-style\n    extension) fully implemented but for ``__delattr__`` (since\n    ``del`` is a statement)\n\nDescriptor protocol\n    As with attributes, ``__delete__`` is not implemented.\n\nCallable objects\n    Work, although the handling of arguments isn't exactly nailed\n    down. For now, callables get two (javascript) arguments ``args``\n    and ``kwargs``, holding (respectively) positional and keyword\n    arguments.\n\n    Conflicts are *not* handled at this point.\n\nCollections Abstract Base Classes\n    Container is the only implemented ABC protocol (ABCs themselves\n    are not currently implemented) (well technically Callable and\n    Hashable are kind-of implemented as well)\n\nNumeric type emulation\n    Operators are implemented (but not tested), ``abs``, ``divmod``\n    and ``pow`` builtins are not implemented yet. Neither are ``oct``\n    and ``hex`` but I'm not sure we care (I'm not sure we care about\n    ``pow`` or even ``divmod`` either, for that matter)\n\nUtilities\n---------\n\n``py.js`` also provides (and exposes) a few utilities for \"userland\"\nimplementation:\n\n``def``\n    Wraps a native javascript function into a ``py.js`` function, so\n    that it can be called from native expressions.\n\n    Does not ensure the return types are type-compatible with\n    ``py.js`` types.\n\n    When accessing instance methods, ``py.js`` automatically wraps\n    these in a variant of ``py.PY_def``, to behave as Python's (bound)\n    methods.\n\nWhy\n===\n\nOriginally, to learn about Pratt parsers (which are very, very good at\nparsing expressions with lots of infix or mixfix symbols). The\nevaluator part came because \"why not\" and because I work on a product\nwith the \"feature\" of transmitting Python expressions (over the wire)\nwhich the client is supposed to evaluate.\n\nHow\n===\n\nAt this point, only three steps exist in ``py.js``: tokenizing,\nparsing and evaluation. It is possible that a compilation step be\nadded later (for performance reasons).\n\nTo evaluate a Python expression, the caller merely needs to call\n`py.eval`_. `py.eval`_ takes a mandatory Python\nexpression to evaluate (as a string) and an optional context, for the\nsubstitution of the free variables in the expression::\n\n    \u003e py.eval(\"type in ('a', 'b', 'c') and foo\", {type: 'c', foo: true});\n    true\n\nThis is great for one-shot evaluation of expressions. If the\nexpression will need to be repeatedly evaluated with the same\nparameters, the various parsing and evaluation steps can be performed\nseparately: `py.eval`_ is really a shortcut for sequentially calling\n`py.tokenize`_, `py.parse`_ and `py.evaluate`_.\n\nAPI\n===\n\n.. _py.eval:\n\n``py.eval(expr[, context])``\n    \"Do everything\" function, to use for one-shot evaluation of a\n    Python expression: it will internally handle the tokenizing,\n    parsing and actual evaluation of the Python expression without\n    having to perform these separately.\n\n    ``expr``\n        Python expression to evaluate\n    ``context``\n        context dictionary holding the substitutions for the free\n        variables in the expression\n\n.. _py.tokenize:\n\n``py.tokenize(expr)``\n    ``expr``\n        Python expression to tokenize\n\n.. _py.parse:\n\n``py.parse(tokens)``\n    Parses a token stream and returns an abstract syntax tree of the\n    expression (if the token stream represents a valid Python\n    expression).\n\n    A parse tree is stateless and can be memoized and used multiple\n    times in separate evaluations.\n\n    ``tokens``\n         stream of tokens returned by `py.tokenize`_\n\n.. _py.evaluate:\n\n``py.evaluate(ast[, context])``\n    ``ast``\n        The output of `py.parse`_\n    ``context``\n        The evaluation context for the Python expression.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-belica%2Fpy.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiso-belica%2Fpy.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-belica%2Fpy.js/lists"}