Name | jy JSON |
Version |
0.1.17
JSON |
| download |
home_page | https://github.com/thorwhalen/jy |
Summary | Tools to control JS from Python (jy for Js pY or Js python proxY) |
upload_time | 2024-12-03 16:07:44 |
maintainer | None |
docs_url | None |
author | Thor Whalen |
requires_python | None |
license | apache-2.0 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# jy
Tools to control JS from Python.
(``jy`` stands for "Js pY" or "Js python proxY".)
To install: ```pip install jy```
[Documentation](https://i2mint.github.io/jy/)
# Example
Say you have the ``test01.js``
(whose contents are displayed in the next subsection).
from jy import add_js_funcs
js = add_js_funcs("./test01.js")
js has two methods called bar and foo
assert sorted([x for x in dir(js) if not x.startswith('_')]) == [
'add_one', 'bar', 'foo', 'obj', 'prop', 'with_arrow_func', 'with_let'
]
They mirror the signatures of the underlying JS functions
from inspect import signature
assert str(signature(js.foo)) == "(a, b='hello', c=3)"
assert str(signature(js.bar)) == "(green, eggs='food', and=True, ham=4)"
Calling this function returns a string
(the code to call the underlying JS function)
assert js.foo(1, 'hi', 5) == 'foo(1, "hi", 5)'
Notice that you can use positional or keyword arguments
Also, notice that though "prop" is the name of `js`'s attribute,
the function call string does indeed use the original full reference:
``func.assigned.to.nested.prop``
assert js.prop('up') == (
'func.assigned.to.nested.prop("up")'
)
Notice that the python (signature) defaults are applied before translating to JS
assert js.bar(42) == 'bar(42, "food", true, 4)'
alt_js = add_js_funcs(test01_js_code, apply_defaults=False)
You can opt not to do this by specifying `apply_defaults=False`
This will result in only injecting those inputs you specify in the js call string,
which will have the effect of letting JS apply its defaults, what ever they are
alt_js = add_js_funcs(test01_js_code, apply_defaults=False)
assert alt_js.bar(42) == 'bar(42)'
# Appendix
## The ``test01.js`` file's contents
```javascript
// "test01.js" file
// Straight function definition
function foo(a, b="hello", c= 3) {
return a + b.length * c
}
// Straight function definition
function bar(green, eggs = 'food', and= true, ham= 4) {
if (and) return eggs.length * ham
}
// global callable variable
add_one = function (x) {
return x + 1
}
// does the presence of a let break the parser?
let with_let = function (x) {
return x + 2
}
// with arrow func
// (also testing if const breaks the parse)
const with_arrow_func = (y, z= 1) => y * z
// function assigned to a nested property
func.assigned.to.nested.prop = function (x) {
return x + 3
}
// function nested in some other function, assigned to a variable
var obj = (function (exports) {
function bar(name) {
return name + "__"
}
})
```
Raw data
{
"_id": null,
"home_page": "https://github.com/thorwhalen/jy",
"name": "jy",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Thor Whalen",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/0b/86/c197cfd684e4086ebdc144667d099da0bfa08ab9a79cd09b554e78c35ac7/jy-0.1.17.tar.gz",
"platform": "any",
"description": "# jy\n\nTools to control JS from Python.\n\n(``jy`` stands for \"Js pY\" or \"Js python proxY\".)\n\nTo install:\t```pip install jy```\n\n[Documentation](https://i2mint.github.io/jy/)\n\n\n# Example\n\nSay you have the ``test01.js``\n(whose contents are displayed in the next subsection).\n\n from jy import add_js_funcs\n js = add_js_funcs(\"./test01.js\")\n\njs has two methods called bar and foo\n\n assert sorted([x for x in dir(js) if not x.startswith('_')]) == [\n 'add_one', 'bar', 'foo', 'obj', 'prop', 'with_arrow_func', 'with_let'\n ]\n\nThey mirror the signatures of the underlying JS functions\n\n from inspect import signature\n assert str(signature(js.foo)) == \"(a, b='hello', c=3)\"\n assert str(signature(js.bar)) == \"(green, eggs='food', and=True, ham=4)\"\n\nCalling this function returns a string\n(the code to call the underlying JS function)\n\n assert js.foo(1, 'hi', 5) == 'foo(1, \"hi\", 5)'\n\nNotice that you can use positional or keyword arguments\nAlso, notice that though \"prop\" is the name of `js`'s attribute,\nthe function call string does indeed use the original full reference:\n``func.assigned.to.nested.prop``\n\n assert js.prop('up') == (\n 'func.assigned.to.nested.prop(\"up\")'\n )\n\nNotice that the python (signature) defaults are applied before translating to JS\n\n assert js.bar(42) == 'bar(42, \"food\", true, 4)'\n alt_js = add_js_funcs(test01_js_code, apply_defaults=False)\n\nYou can opt not to do this by specifying `apply_defaults=False`\nThis will result in only injecting those inputs you specify in the js call string,\nwhich will have the effect of letting JS apply its defaults, what ever they are\n\n alt_js = add_js_funcs(test01_js_code, apply_defaults=False)\n assert alt_js.bar(42) == 'bar(42)'\n\n\n# Appendix\n\n## The ``test01.js`` file's contents\n\n```javascript\n// \"test01.js\" file\n// Straight function definition\nfunction foo(a, b=\"hello\", c= 3) {\n return a + b.length * c\n}\n\n// Straight function definition\nfunction bar(green, eggs = 'food', and= true, ham= 4) {\n if (and) return eggs.length * ham\n}\n\n// global callable variable\nadd_one = function (x) {\n return x + 1\n}\n\n// does the presence of a let break the parser?\nlet with_let = function (x) {\n return x + 2\n}\n\n// with arrow func\n// (also testing if const breaks the parse)\nconst with_arrow_func = (y, z= 1) => y * z\n\n// function assigned to a nested property\nfunc.assigned.to.nested.prop = function (x) {\n return x + 3\n}\n\n// function nested in some other function, assigned to a variable\nvar obj = (function (exports) {\n function bar(name) {\n return name + \"__\"\n }\n})\n\n```\n",
"bugtrack_url": null,
"license": "apache-2.0",
"summary": "Tools to control JS from Python (jy for Js pY or Js python proxY)",
"version": "0.1.17",
"project_urls": {
"Homepage": "https://github.com/thorwhalen/jy"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "defaacc595180b4bdc8cbbb31d2e95ebde78b890fa44a233a6f34e9877c19aa3",
"md5": "4ce526839590717274ea080a3dc876a3",
"sha256": "815afc309097c764e8ba64eef7a36dcfb0391612039321f39bdbb9f72edf839d"
},
"downloads": -1,
"filename": "jy-0.1.17-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4ce526839590717274ea080a3dc876a3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 20339,
"upload_time": "2024-12-03T16:07:43",
"upload_time_iso_8601": "2024-12-03T16:07:43.025286Z",
"url": "https://files.pythonhosted.org/packages/de/fa/acc595180b4bdc8cbbb31d2e95ebde78b890fa44a233a6f34e9877c19aa3/jy-0.1.17-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0b86c197cfd684e4086ebdc144667d099da0bfa08ab9a79cd09b554e78c35ac7",
"md5": "aba0ec3cda3ff8cb9f1a94bf12f5cb6a",
"sha256": "a7439c6e0dea9ec13e27897c1ddb4333af91299972819e4bfd2fe76ffc7449a0"
},
"downloads": -1,
"filename": "jy-0.1.17.tar.gz",
"has_sig": false,
"md5_digest": "aba0ec3cda3ff8cb9f1a94bf12f5cb6a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18342,
"upload_time": "2024-12-03T16:07:44",
"upload_time_iso_8601": "2024-12-03T16:07:44.648308Z",
"url": "https://files.pythonhosted.org/packages/0b/86/c197cfd684e4086ebdc144667d099da0bfa08ab9a79cd09b554e78c35ac7/jy-0.1.17.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-03 16:07:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thorwhalen",
"github_project": "jy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "jy"
}