Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TjsProxy extend collections.abc.Mapping #151

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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