Pulse
pulse.js

pulse.js

Python bindings for JavaScript builtins in transpiled code

pulse.js

The pulse.js module provides typed Python wrappers for JavaScript objects, functions, and built-ins. Use these when writing @ps.javascript decorated functions that get transpiled to JavaScript.

Why pulse.js?

When you write a @ps.javascript function, your Python code gets transpiled to JavaScript. But Python and JavaScript have different built-ins: Python has dict and set, JavaScript has Map and Set. The pulse.js module bridges this gap by providing Python classes that transpile to their JavaScript equivalents.

from pulse.js import Set, Array, Math, console

@ps.javascript
def example():
    items = Set([1, 2, 3])          # -> new Set([1, 2, 3])
    arr = Array.from_(items)        # -> Array.from(items)
    rounded = Math.floor(3.7)       # -> Math.floor(3.7)
    console.log(rounded)            # -> console.log(rounded)

Module organization

The module provides three categories of exports:

Classes (constructors + static methods)

Use new when called, provide static methods:

  • Array - Indexed collection
  • Date - Date and time
  • Error - Error types
  • Map - Key-value collection
  • Number - Number utilities
  • Object - Object utilities
  • Promise - Async operations
  • RegExp - Regular expressions
  • Set - Unique value collection
  • String - String utilities
  • WeakMap - Weak key-value collection
  • WeakSet - Weak value collection

Namespaces (static functions only)

Access methods via the namespace:

React bindings

Hooks and components for React:

Special functions

  • throw(error) - Throw an error (statement, not expression)
  • obj(key=value) - Create plain JS object literal
  • undefined - JavaScript undefined value

Import patterns

Import classes and namespaces from pulse.js:

from pulse.js import Set, Map, Array, Math, console, window

Or import namespace modules for aliased access:

import pulse.js.math as Math
import pulse.js.json as JSON

Math.floor(3.7)        # -> Math.floor(3.7)
JSON.stringify(data)   # -> JSON.stringify(data)

Import React hooks from pulse.js.react:

from pulse.js.react import useState, useEffect, useRef

See also

On this page