Skip to main content

Tools to control JS from Python (jy for Js pY or Js python proxY)

Project description

jy

Tools to control JS from Python.

(jy stands for "Js pY" or "Js python proxY".)

To install: pip install jy

Documentation

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

// "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 + "__"
    }
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jy-0.1.17.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jy-0.1.17-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file jy-0.1.17.tar.gz.

File metadata

  • Download URL: jy-0.1.17.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.15

File hashes

Hashes for jy-0.1.17.tar.gz
Algorithm Hash digest
SHA256 a7439c6e0dea9ec13e27897c1ddb4333af91299972819e4bfd2fe76ffc7449a0
MD5 aba0ec3cda3ff8cb9f1a94bf12f5cb6a
BLAKE2b-256 0b86c197cfd684e4086ebdc144667d099da0bfa08ab9a79cd09b554e78c35ac7

See more details on using hashes here.

File details

Details for the file jy-0.1.17-py3-none-any.whl.

File metadata

  • Download URL: jy-0.1.17-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.15

File hashes

Hashes for jy-0.1.17-py3-none-any.whl
Algorithm Hash digest
SHA256 815afc309097c764e8ba64eef7a36dcfb0391612039321f39bdbb9f72edf839d
MD5 4ce526839590717274ea080a3dc876a3
BLAKE2b-256 defaacc595180b4bdc8cbbb31d2e95ebde78b890fa44a233a6f34e9877c19aa3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page