MCPcopy
hub / github.com/pyodide/pyodide / as_object_map

Method as_object_map

src/py/_pyodide/_core_docs.py:218–272  ·  view source on GitHub ↗

Returns a new JsProxy that treats the object as a map. The methods :py:func:`~operator.__getitem__`, :py:func:`~operator.__setitem__`, :py:func:`~operator.__contains__`, :py:meth:`~object.__len__`, etc will perform lookups via ``object[key]`` or similar. Not

(self, *, hereditary: bool = False)

Source from the content-addressed store, hash-verified

216 raise NotImplementedError
217
218 def as_object_map(self, *, hereditary: bool = False) -> "JsMutableMap[str, Any]":
219 """Returns a new JsProxy that treats the object as a map.
220
221 The methods :py:func:`~operator.__getitem__`,
222 :py:func:`~operator.__setitem__`, :py:func:`~operator.__contains__`,
223 :py:meth:`~object.__len__`, etc will perform lookups via ``object[key]``
224 or similar.
225
226 Note that ``len(x.as_object_map())`` evaluates in O(n) time (it iterates
227 over the object and counts how many :js:func:`~Reflect.ownKeys` it has).
228 If you need to compute the length in O(1) time, use a real
229 :js:class:`Map` instead.
230
231 .. deprecated:: 0.29.0
232
233 Use :py:meth:`JsProxy.as_py_json` instead.
234
235 Parameters
236 ----------
237 hereditary:
238 If ``True``, any "plain old objects" stored as values in the object
239 will be wrapped in `as_object_map` themselves.
240
241 Examples
242 --------
243
244 >>> from pyodide.code import run_js # doctest: +RUN_IN_PYODIDE
245 >>> o = run_js("({x : {y: 2}})")
246
247 Normally you have to access the properties of ``o`` as attributes:
248
249 >>> o.x.y
250 2
251 >>> o["x"] # is not subscriptable
252 Traceback (most recent call last):
253 TypeError: 'pyodide.ffi.JsProxy' object is not subscriptable
254
255 ``as_object_map`` allows us to access the property with ``getitem``:
256
257 >>> o.as_object_map()["x"].y
258 2
259
260 The inner object is not subscriptable because ``hereditary`` is ``False``:
261
262 >>> o.as_object_map()["x"]["y"]
263 Traceback (most recent call last):
264 TypeError: 'pyodide.ffi.JsProxy' object is not subscriptable
265
266 When ``hereditary`` is ``True``, the inner object is also subscriptable:
267
268 >>> o.as_object_map(hereditary=True)["x"]["y"]
269 2
270
271 """
272 raise NotImplementedError
273
274 def as_py_json(self) -> "JsMutableMap[str, Any] | JsArray[Any]":
275 """Returns a new JsProxy that treats a JavaScript object as Python json.

Callers 4

test_jsproxy_matchFunction · 0.80

Calls

no outgoing calls

Tested by 4

test_jsproxy_matchFunction · 0.64