Comments (8)
Can you please give an example of code you wrote, what it currently does, and what you would prefer for it to do?
from pyodide.
I have a python DateRange Class that I want to be able to check other dates membership inside the range. So I made the following dunder method in python:
def __contains__(self, date_value: Union[datetime, date]) -> bool:
date_value = self._convert_to_date(date_value)
return self.lower_date <= date_value <= self.upper_date
but when I run the includes method on an instance of the object in Javascript with a JS Date object as the method's argument, I get a KeyError rather than running my python method.
What I was expecting was that it would work like using the in
operator in python and run my __contains__
dunder method. I would expect basically the same with str and the toString method where my str method would run in JS and the same for every other dunder method with an equivalent JS function/method.
from pyodide.
Well toString()
uses __repr__
:
let a = pyodide.runPython(`
class A:
def __repr__(self):
return "a"
A()
`);
a.toString()
I don't recall why we picked __repr__
over __str__
but there isn't an obvious choice.
For __contains__
we seem to map it to has
:
pyodide.runPython(`
class A:
def __contains__(self, other):
return isinstance(other, int) and other > 5
A()
`);
a.has(7); // true
a.has(4); // false
a.has({}); // false
If you register it as a Sequence
then you can use __contains__
via includes
:
let a = pyodide.runPython(`
from collections.abc import Sequence
@Sequence.register
class A:
def __contains__(self, other):
return isinstance(other, int) and other > 5
A()
`);
a.includes(7); // true
This is meant to line up with the way JavaScript objects work: there is Array.prototype.includes
but Map.prototype.has
and Set.prototype.has
, etc. So only Sequence-like objects get an includes
and everything else gets has
instead.
from pyodide.
To the toString method using the __repr__
method, that just seems incorrect since there's a distinction between __repr__
and __str__
that python devs expect and sometimes rely on. Using __repr__
when doing a bare console.log makes since, but not when a dev is trying to string the object.
I don't know how you're running your pyodide code, but I'm running it through TS-Node and adding @Sequence.register
to my code still errors out when using the includes method, as does your example. The has method does work though.
from pyodide.
Oh yes, the @Sequence.register
example is on tip of tree, so it will be supported when we release v0.24.
Looking at the history, it seems that toString
was made to do __repr__
when the project was three months old, which was before things were really discussed at all:
9579aa9
Since then I'm not sure if anyone has discussed whether it should do __repr__
or __str__
. I would be open to changing it if there is a consensus in favor of __str__
.
from pyodide.
Ahh that makes sense for the example. Regardless, it seems that the separation between includes and has in Javascript doesn't apply to python in any sensible way, so I'll leave it at what it is.
I've given my case for __str__
, so I'll leave it at that for the maintainers to decide :)
from pyodide.
I'll open a separate issue and solicit some opinions.
from pyodide.
Okay I'm going to close this issue @Duncanr-glitch but feel free to reopen it if you think it hasn't been resolved, or open a new issue if you have further questions.
from pyodide.
Related Issues (20)
- Debug pyodide release artifacts (and pyodide build debug support)
- "Building and testing Python packages out of tree" commands need some dev tools installed as prerequisites HOT 2
- Add esbuild-py HOT 4
- Add `PyMuPDF` package
- runPython return missing
- cmake -E capabilities is broken HOT 1
- Release 0.26.0 HOT 5
- datasets (huggingface) HOT 6
- Cannot create a WebGL2 context when using SDL HOT 3
- request to add pandasai HOT 1
- request to add llama-index
- request to add langchain HOT 1
- request to add Boto3 HOT 4
- fastobo
- loadPackage() not to access the file system if the cache already exists HOT 2
- cvxpy-base 1.4.3 HOT 2
- html5_canvas_backend no longer works. HOT 1
- RFC Package unvendored stdlibs to sdist / wheel? HOT 3
- `make` in docker container `pyodide/pyodide-env` fails with `could not create work tree dir 'emsdk'` HOT 8
- DOM types in pyodide lead to build issues with Angular and typescript. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyodide.