Skip to content

Commit 2c682b4

Browse files
authored
Make TjsProxy extend collections.abc.Mapping (#151)
1 parent f9cafb4 commit 2c682b4

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

transformers_js_py/proxies.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from collections.abc import Mapping
23
from typing import Any, Awaitable, Union
34

45
import js
@@ -62,7 +63,7 @@ def to_js(obj: Any) -> Any:
6263
)
6364

6465

65-
class TjsProxy:
66+
class TjsProxy(Mapping):
6667
def __init__(self, js_obj: pyodide.ffi.JsProxy):
6768
self._js_obj = js_obj
6869
self._is_class = self._js_obj.typeof == "function" and rx_class_def_code.match(
@@ -90,8 +91,8 @@ def __getattr__(self, name: str) -> Any:
9091
res = getattr(self._js_obj, name)
9192
return wrap_or_unwrap_proxy_object(res)
9293

93-
def __getitem__(self, key: Any) -> Any:
94-
res = self._js_obj[key]
94+
def __getitem__(self, name: Any) -> Any:
95+
res = getattr(self._js_obj, name)
9596
return wrap_or_unwrap_proxy_object(res)
9697

9798
def __setitem__(self, key: Any, value: Any) -> None:
@@ -105,9 +106,11 @@ def __setattr__(self, name: str, value: Any) -> None:
105106
value = to_js(value)
106107
setattr(self._js_obj, name, value)
107108

108-
def __repr__(self) -> str:
109-
dictified_self = {k: getattr(self, k) for k in self._js_obj.object_keys()}
110-
return "<TjsProxy({})>".format(repr(dictified_self))
109+
def __iter__(self):
110+
return iter(self._js_obj.object_keys())
111+
112+
def __len__(self):
113+
return len(self._js_obj.object_keys())
111114

112115

113116
class TjsRawImageClassProxy(TjsProxy):

0 commit comments

Comments
 (0)