jy


Namejy JSON
Version 0.1.13 PyPI version JSON
download
home_pagehttps://github.com/thorwhalen/jy
SummaryTools to control JS from Python (jy for Js pY or Js python proxY)
upload_time2024-11-12 12:53:59
maintainerNone
docs_urlNone
authorThor Whalen
requires_pythonNone
licenseapache-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/cf/1c/7789284d30d03af71daae3dd2b774b1e7586898ffcc163544bfd9303eae6/jy-0.1.13.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.13",
    "project_urls": {
        "Homepage": "https://github.com/thorwhalen/jy"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fa1ba136f77e69a92c386127bf1ab0547eb82d829108e0c3a466c4d01a55623",
                "md5": "00e8e56e6b1e80523254362aacbd69bc",
                "sha256": "09565dbd6a52197d9f734108a0b37845f2b4cb612c2902d4c53970df15317837"
            },
            "downloads": -1,
            "filename": "jy-0.1.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "00e8e56e6b1e80523254362aacbd69bc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 19637,
            "upload_time": "2024-11-12T12:53:58",
            "upload_time_iso_8601": "2024-11-12T12:53:58.290310Z",
            "url": "https://files.pythonhosted.org/packages/4f/a1/ba136f77e69a92c386127bf1ab0547eb82d829108e0c3a466c4d01a55623/jy-0.1.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf1c7789284d30d03af71daae3dd2b774b1e7586898ffcc163544bfd9303eae6",
                "md5": "cc555430ecf53f40e8b32ef9d3e5f41f",
                "sha256": "e2df7186d56e26f735a5b6c561f6cd1405b6a3dd43bbef31226580123e59277c"
            },
            "downloads": -1,
            "filename": "jy-0.1.13.tar.gz",
            "has_sig": false,
            "md5_digest": "cc555430ecf53f40e8b32ef9d3e5f41f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 17676,
            "upload_time": "2024-11-12T12:53:59",
            "upload_time_iso_8601": "2024-11-12T12:53:59.671618Z",
            "url": "https://files.pythonhosted.org/packages/cf/1c/7789284d30d03af71daae3dd2b774b1e7586898ffcc163544bfd9303eae6/jy-0.1.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-12 12:53:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "thorwhalen",
    "github_project": "jy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jy"
}
        
Elapsed time: 0.50600s