Utilities
Functions associated with v8codec.jstypes.
jstypes.js_repr_settings
jstypes.js_repr_settings(
js_repr: JSRepr | None = None,
*,
force_restore: bool = False,
**kwargs: int | str | None,
)Override the active repr settings for JS types.
This returns a context manager that will restore the previous settings at the end of the context block. The context object is an instance of JSRepr.
Notes
If someone changes the js_repr_settings within your block and your block closes before theirs, your block will emit a JSReprSettingsNotRestored warning and leave the repr settings unchanged. Pass force_restore=True to restore your initial state anyway and not warn.
jstypes.JSRepr
jstypes.JSRepr(maxjsobject: int = 20, maxjsarray: int = 20, **kwargs: Any)Generate repr strings for JS types.
This implements the repr strings used by JSObject, JSArray and others, which can be indented and handle cyclic references by substituting ... after several recursive calls.
Indented reprs are not available before Python 3.12 because JSRepr uses reprlib, which added indented reprs in 3.12.
Attributes
| Name | Description |
|---|---|
| fillvalue | |
| indent | |
| maxjsarray | |
| maxjsobject |
Methods
| Name | Description |
|---|---|
| repr_JSArray | |
| repr_JSArrayBuffer | |
| repr_JSError | |
| repr_JSMap | |
| repr_JSObject | |
| repr_JSSet | |
| repr_bytearray | |
| repr_bytes | |
| repr_memoryview |
repr_JSArray
jstypes.JSRepr.repr_JSArray(obj: JSArray, level: int)repr_JSArrayBuffer
jstypes.JSRepr.repr_JSArrayBuffer(obj: JSArrayBuffer, level: int)repr_JSError
jstypes.JSRepr.repr_JSError(obj: JSError, level: int)repr_JSMap
jstypes.JSRepr.repr_JSMap(obj: JSMap, level: int)repr_JSObject
jstypes.JSRepr.repr_JSObject(obj: JSObject, level: int)repr_JSSet
jstypes.JSRepr.repr_JSSet(obj: JSSet, level: int)repr_bytearray
jstypes.JSRepr.repr_bytearray(
obj: bytearray,
level: int = 1,
truncated: bool | None = None,
)repr_bytes
jstypes.JSRepr.repr_bytes(
obj: bytes,
level: int = 1,
*,
truncated: bool | None = None,
)repr_memoryview
jstypes.JSRepr.repr_memoryview(obj: memoryview, level: int)jstypes.JSReprSettingsNotRestored
jstypes.JSReprSettingsNotRestored()jstypes.same_value_zero
jstypes.same_value_zero(value: object)Get a surrogate value that follows JavaScript same-value-zero equality rules.
Python values can be compared according to same-value-zero by using ==, hash() on the result of calling this function on the values, rather than on the values them directly. Like a key function when sorting.
same_value_zero(x) == same_value_zero(y) is True if x and y are equal under JavaScript’s same-value-zero rule.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| value | object | Any Python object | required |
Returns
| Name | Type | Description |
|---|---|---|
| JSSameValueZero | An opaque value that follows the same-value-zero rules when compared with == or passed to hash(). |
Examples
>>> NaN = float('nan')
>>> NaN == NaN
False
>>> same_value_zero(NaN) == same_value_zero(NaN)
True>>> True == 1
True
>>> same_value_zero(True) == same_value_zero(1)
False>>> l1, l2 = [0], [0]
>>> l1 is l2
False
>>> l1 == l2
True
>>> same_value_zero(l1) == same_value_zero(l2)
False
>>> same_value_zero(l1) == same_value_zero(l1)
TrueStrings and numbers are equal by value.
>>> s1, s2 = str([ord('a')]), str([ord('a')])
>>> s1 is s2
False
>>> s1 == s2
True
>>> same_value_zero(s1) == same_value_zero(s2)
True
>>> same_value_zero(1.0) == same_value_zero(1)
Truejstypes.JSSameValueZero
jstypes.JSSameValueZero
The type of the opaque values returned by same_value_zero.