Utilities
Functions associated with v8codec.jstypes
.
jstypes.js_repr_settings
jstypes.js_repr_settings(| None = None,
js_repr: JSRepr *,
bool = False,
force_restore: **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(self,
*,
int = 20,
maxjsobject: int = 20,
maxjsarray: **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
int) jstypes.JSRepr.repr_JSArray(obj: JSArray, level:
repr_JSArrayBuffer
int) jstypes.JSRepr.repr_JSArrayBuffer(obj: JSArrayBuffer, level:
repr_JSError
int) jstypes.JSRepr.repr_JSError(obj: JSError, level:
repr_JSMap
int) jstypes.JSRepr.repr_JSMap(obj: JSMap, level:
repr_JSObject
int) jstypes.JSRepr.repr_JSObject(obj: JSObject, level:
repr_JSSet
int) jstypes.JSRepr.repr_JSSet(obj: JSSet, level:
repr_bytearray
jstypes.JSRepr.repr_bytearray(bytearray,
obj: int = 1,
level: bool | None = None,
truncated: )
repr_bytes
jstypes.JSRepr.repr_bytes(bytes,
obj: int = 1,
level: *,
bool | None = None,
truncated: )
repr_memoryview
memoryview, level: int) jstypes.JSRepr.repr_memoryview(obj:
jstypes.JSReprSettingsNotRestored
jstypes.JSReprSettingsNotRestored()
jstypes.same_value_zero
object) jstypes.same_value_zero(value:
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)
True
Strings 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)
True
jstypes.JSSameValueZero
jstypes.JSSameValueZero
The type of the opaque values returned by same_value_zero
.