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

__getitem__, __setitem__, __len__ missing #402

Closed
brupelo opened this issue Mar 16, 2019 · 8 comments
Closed

__getitem__, __setitem__, __len__ missing #402

brupelo opened this issue Mar 16, 2019 · 8 comments

Comments

@brupelo
Copy link

brupelo commented Mar 16, 2019

From the docs I've seen right now there are these methods implemented:

fn __bool__(&self) -> PyResult<bool>
fn __bytes__(&self) -> PyResult<PyBytes>
fn __delattr__(&mut self, name: FromPyObject) -> PyResult<()>
fn __format__(&self, format_spec: &str) -> PyResult<impl ToPyObject<ObjectType=PyString>>
fn __getattr__(&self, name: FromPyObject) -> PyResult<impl IntoPyObject>
fn __hash__(&self) -> PyResult<impl PrimInt>
fn __repr__(&self) -> PyResult<impl ToPyObject<ObjectType=PyString>>
fn __richcmp__(&self, other: impl FromPyObject, op: CompareOp) -> PyResult<impl ToPyObject>
fn __setattr__(&mut self, name: FromPyObject, value: FromPyObject) -> PyResult<()>
fn __str__(&self) -> PyResult<impl ToPyObject<ObjectType=PyString>>
fn __unicode__(&self) -> PyResult<PyUnicode>

Is that all? Asking cos it'd be really convenient to have other ones such as __getitem__, __setitem__, __len__, right now instead __getitem__ I'm using methods called get or length, which are far away from being pythonic, ie:

#[pyclass]
struct ScopeSelectors {
    wrap: _ScopeSelectors,
}

#[pyproto]
impl PyObjectProtocol for ScopeSelectors {
    fn __repr__(&self) -> PyResult<String> {
        Ok(format!("{:?}", self.wrap))
    }
}

#[pymethods]
impl ScopeSelectors {
    fn length(&self) -> usize {
        return self.wrap.selectors.len();
    }

    fn get(&self, index: usize) -> ScopeSelector {
        return ScopeSelector {
            wrap: self.wrap.selectors[index].clone(),
        };
    }
}

:(

Ps. Using version = "0.6.0-alpha.4"

@brupelo
Copy link
Author

brupelo commented Mar 16, 2019

Of course, not just __getitem__, __setitem__, __len__, other methods such as:

object.__abs__(self)
object.__add__(self, other)
object.__aenter__(self)
object.__aexit__(self, exc_type, exc_value, traceback)
object.__aiter__(self)
object.__and__(self, other)
object.__anext__(self)
object.__await__(self)
object.__bool__(self)
object.__bytes__(self)
object.__call__(self[, args...])
object.__ceil__(self)
object.__complex__(self)
object.__contains__(self, item)
object.__del__(self)
object.__delattr__(self, name)
object.__delete__(self, instance)
object.__delitem__(self, key)
object.__dir__(self)
object.__divmod__(self, other)
object.__enter__(self)
object.__eq__(self, other)
object.__exit__(self, exc_type, exc_value, traceback)
object.__float__(self)
object.__floor__(self)
object.__floordiv__(self, other)
object.__format__(self, format_spec)
object.__ge__(self, other)
object.__get__(self, instance, owner)
object.__getattr__(self, name)
object.__getattribute__(self, name)
object.__getitem__(self, key)
object.__gt__(self, other)
object.__hash__(self)
object.__iadd__(self, other)
object.__iand__(self, other)
object.__ifloordiv__(self, other)
object.__ilshift__(self, other)
object.__imatmul__(self, other)
object.__imod__(self, other)
object.__imul__(self, other)
object.__index__(self)
object.__init__(self[, ...])
object.__int__(self)
object.__invert__(self)
object.__ior__(self, other)
object.__ipow__(self, other[, modulo])
object.__irshift__(self, other)
object.__isub__(self, other)
object.__iter__(self)
object.__itruediv__(self, other)
object.__ixor__(self, other)
object.__le__(self, other)
object.__len__(self)
object.__length_hint__(self)
object.__lshift__(self, other)
object.__lt__(self, other)
object.__matmul__(self, other)
object.__missing__(self, key)
object.__mod__(self, other)
object.__mul__(self, other)
object.__ne__(self, other)
object.__neg__(self)
object.__new__(cls[, ...])
object.__or__(self, other)
object.__pos__(self)
object.__pow__(self, other[, modulo])
object.__radd__(self, other)
object.__rand__(self, other)
object.__rdivmod__(self, other)
object.__repr__(self)
object.__reversed__(self)
object.__rfloordiv__(self, other)
object.__rlshift__(self, other)
object.__rmatmul__(self, other)
object.__rmod__(self, other)
object.__rmul__(self, other)
object.__ror__(self, other)
object.__round__(self[, ndigits])
object.__rpow__(self, other)
object.__rrshift__(self, other)
object.__rshift__(self, other)
object.__rsub__(self, other)
object.__rtruediv__(self, other)
object.__rxor__(self, other)
object.__set__(self, instance, value)
object.__set_name__(self, owner, name)
object.__setattr__(self, name, value)
object.__setitem__(self, key, value)
object.__slots__
object.__str__(self)
object.__sub__(self, other)
object.__truediv__(self, other)
object.__trunc__(self)
object.__xor__(self, other)

Could also be truly interesting to have :)

@kngwyu
Copy link
Member

kngwyu commented Mar 16, 2019

See https://pyo3.rs/master/doc/pyo3/trait.ObjectProtocol.html

@kngwyu kngwyu closed this as completed Mar 16, 2019
@brupelo
Copy link
Author

brupelo commented Mar 16, 2019

Thanks, wasn't aware of that doc and I was using these ones https://github.com/PyO3/pyo3/tree/master/guide/src

@brupelo
Copy link
Author

brupelo commented Mar 16, 2019

If I try:

#[pyproto]
impl PyObjectProtocol for ScopeSelectors {
    fn __repr__(&self) -> PyResult<String> {
        Ok(format!("{:?}", self.wrap))
    }

    fn len(&self) -> PyResult<usize> {
        Ok(self.wrap.selectors.len())
    }
}

it will tell me:

error[E0407]: method `len` is not a member of trait `PyObjectProtocol`
  --> src\selector.rs:26:5
   |
26 | /     fn len(&self) -> PyResult<usize> {
27 | |         Ok(self.wrap.selectors.len())
28 | |     }
   | |_____^ not a member of trait `PyObjectProtocol`

what am I doing wrong there? (sorry for these newbie questions though :()

@kngwyu
Copy link
Member

kngwyu commented Mar 16, 2019

When using with pyproto you should use https://pyo3.rs/master/doc/pyo3/class/sequence/trait.PySequenceProtocol.html
And if you have other questions I recommend to use https://gitter.im/PyO3/Lobby or stackoverflow, instead of github issue

@brupelo
Copy link
Author

brupelo commented Mar 16, 2019

@kngwyu Thanks, that did it. Btw, isn't there any pyo3 irc channel available? Asking cos I don't use gitter at all... ie: freenode/moznet/ofct,.... that'd be quite awesome. Anyway, thanks for this last link

@brupelo
Copy link
Author

brupelo commented Mar 17, 2019

I've got stuck again with this https://github.com/brupelo/mcve/blob/master/src/lib.rs#L48, here's the error I'm getting error.

Also, I've created #pyo3 in both moznet & freenode, it'd be really helpful if you announced those channels on the startup badges in addition to the existing gitter one so guys who like IRC will join, ty!

@brupelo
Copy link
Author

brupelo commented Mar 21, 2019

Never mind about my last unanswered question, at the end I've just ported that rust iterator code to python.

About not commenting on my proposal about pyo3 irc channels... too bad, I assume you're just interested on that fancy gitter thing.

On the other hand, it's quite easy to add freenode badgets on the README.md, look... in my project I've done it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants