add __contains__ to collections

Bug #313233 reported by Markus Korn
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
launchpadlib
Triaged
Low
Unassigned

Bug Description

In same cases I need to know if a resource-object is in a collection, it would be nice if launchpadlib could implement a __contains__ for collections. I think it would make sense to base this check on the self_link attribute of the entries.

As an example this should return True instead of False:

In [12]: thekorn = launchpad.people["thekorn"]

In [13]: bughelper_dev = launchpad.people["bughelper-dev"]

In [14]: thekorn in bughelper_dev.members
Out[14]: False

Markus

Revision history for this message
Markus Korn (thekorn) wrote :

Or, fixing it by implementing a solution for bug 264098 would be even better

Changed in launchpadlib:
importance: Undecided → High
status: New → Triaged
importance: High → Medium
Revision history for this message
Leonard Richardson (leonardr) wrote :

Wouldn't this require iterating over the (possibly huge) collection? I don't think we want to encourage that any more than we already do.

Revision history for this message
Markus Korn (thekorn) wrote :

If you would like to block iterating over possibly huge collections you should think about overwriting __contains__ and raise an Exception instead, because python automatically falls back to something like the following if __contains__ is absent and __iter__ is implemented for a class:

for z in x:
  if y==x: return True
return False

which is proportional to len(x).
(This also shows that this bug would be fixed if the bug mentioned in my last comment is fixed)

Revision history for this message
Markus Korn (thekorn) wrote :

I just had an completely different idea: why not generating the url of an collections item and send an request to this url? If this raises a HTTPError with a 404 status return False otherwise return True.

Revision history for this message
Francis J. Lacoste (flacoste) wrote :

It really depends on the underlying collection.

There is not always a 'url af a collection item'. For example, messages have their URL as part of their containing collection (/ubuntu/+bug/1/+message/1) but bug don't necessarily.

So testing if a bug is in the bugs collection (obvious that it is) is easier than checking if the bug is in the colletion of bugs affecting firefox (really need to check if there is a bugtask on firefox for that bug) or is in my subscribed bug colletion (other kind of check).

So I'm not entirely sure it's practical to implement. (Btw, for the example you showed, i think there is an inTeam() method you can call to check that information).

Revision history for this message
Markus Korn (thekorn) wrote :

hmm, an inTeam() method would be good, but there is not such method available (yet)

In [5]: "inTeam" in dir(me)
Out[5]: False

In [6]: dir(me)

Changed in launchpadlib:
importance: Medium → Undecided
importance: Undecided → Low
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.