Skip to main content

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)"

(and is a Python keyword, so it -- and every parameter before it -- is exposed as positional-only, hence the /. You can never pass and=... by keyword from Python anyway.)

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

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.19.tar.gz (51.1 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.19-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jy-0.1.19.tar.gz
  • Upload date:
  • Size: 51.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jy-0.1.19.tar.gz
Algorithm Hash digest
SHA256 148ed7989951af5d06e9e60a42ebc20d0ad064590ab0e0f49b0abda76573c34e
MD5 74641d00a9a6650536aca9356e03f81f
BLAKE2b-256 9c0b97196592c4a772e76f35899f345b2a02cfc1a59864a29f5a3f69e249fbcc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jy-0.1.19-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jy-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 fd94da45cc5fc65a925b6cc12411c8d3ccca993ba823d733972b4e795db2e1b5
MD5 2815fbd681b7d2536192f92e2a16691b
BLAKE2b-256 f824f729b1916174d45bdf2a73e22f98c7f1741538c2c9ce0eb7ec0ef9775a1c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.19 This release

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

1 file

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page