Skip to content

Commit

Permalink
Make TjsProxy extend collections.abc.Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed May 2, 2024
1 parent 62f5eed commit 09bf1da
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions transformers_js_py/proxies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from collections.abc import Mapping
from typing import Any, Awaitable, Union

import js
Expand Down Expand Up @@ -62,7 +63,7 @@ def to_js(obj: Any) -> Any:
)


class TjsProxy:
class TjsProxy(Mapping):
def __init__(self, js_obj: pyodide.ffi.JsProxy):
self._js_obj = js_obj
self._is_class = self._js_obj.typeof == "function" and rx_class_def_code.match(
Expand Down Expand Up @@ -90,8 +91,8 @@ def __getattr__(self, name: str) -> Any:
res = getattr(self._js_obj, name)
return wrap_or_unwrap_proxy_object(res)

def __getitem__(self, key: Any) -> Any:
res = self._js_obj[key]
def __getitem__(self, name: Any) -> Any:
res = getattr(self._js_obj, name)
return wrap_or_unwrap_proxy_object(res)

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

def __repr__(self) -> str:
dictified_self = {k: getattr(self, k) for k in self._js_obj.object_keys()}
return "<TjsProxy({})>".format(repr(dictified_self))
def __iter__(self):
return iter(self._js_obj.object_keys())

def __len__(self):
return len(self._js_obj.object_keys())


class TjsRawImageClassProxy(TjsProxy):
Expand Down

0 comments on commit 09bf1da

Please sign in to comment.