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

Add the ability to filter which implementors of an Interface will be returned #11

Closed
daltonmatos opened this issue Jun 6, 2012 · 8 comments
Labels

Comments

@daltonmatos
Copy link
Owner

Currently plugnplay always returns all implementors of an Interface when you call MyInterface.implementors(). The idea of this issue is to be able to filter which implementors will be returned by this call.

The first idea would be to pass a callback to the implementors classmethod, something on the same line of Python's built-in filter function.

A simple example:

def callback(obj):
     return len(obj) > 5

MyInterface.implementors(callback)

Assuming that all implementors of MyInterface responds to the __len__ method, only implementors bigger than 5 would be returned by this call.

Another idea: implementors() could return a custom iterable object that has a filer() method. This way you could continue to have this code:

for impl in MyInterface.implementors():
    # code

but at the same time you could do this:

for impl in MyInterface.implementors().filter(callback):
    # code

to iterate on a filtered list of implementors.

@hugosenari
Copy link

Crazy suggestions

Maybe you can use something like java loggers, using canonical name

Something like:

MyInterface.implementors('br.com.something') #all implementators from br.com.something import *
MyInterface.implementors('br.com') #all implementators from br.com import *
MyInterface.implementors('br') #all implementators from br import *

Another cool stuff are more like jquery do where you can pass one string (use canonical name), one function (like you suggest)

Or you can use css filters:

MyInterface.implementors('bla') #get implementations with class name == "bla"
MyInterface.implementors('ble bla') #get implementations with class name == "bla" sub class of ''ble'
MyInterface.implementors('.bla') #get implementations with attr class == "somenthig bla otherthing"
MyInterface.implementors('.ble.bla') #get implementations with attr class == "somenthig bla ble otherthing"
MyInterface.implementors('#someid') #get implementations with id attr = someid
MyInterface.implementors('[bla]') #get implementations with attr bla
MyInterface.implementors('[bla="ble"]') #get implementations with attr bla == "ble"
MyInterface.implementors(callback) #use function 

Using this you have more features then are usually needed :)

You don't need use all css selectors.

@hugosenari
Copy link

Other idea,

Use ref #12 idea for filter

...
implements = [(MyInterface, X)]
..
implements = [(MyInterface, Y)]
...
MyInterface.implementors(X)
#to be more perfect http://guide.couchdb.org/draft/views.html#many

I liked idea for iterator with 'filter' but more if you create one .not function.

@daltonmatos
Copy link
Owner Author

The css selector style is indeed powerful but would increase cinsiderable the internal complexity of plugnplay. I'd rather keep the codebase simple and go with the filter function approach.

@hugosenari
Copy link

I liked callback approach.

But are better if we can pass parameters to callback

def callback(obj, *args, **kw):
     return len(obj) > kw.get('min_len', 5)

def callback_x(obj, x):
     return len(obj) > x

MyInterface.implementors(callback)
MyInterface.implementors(callback, min_len=7)
MyInterface.implementors(callback_x, 8)

@daltonmatos
Copy link
Owner Author

This is indeed interesting!!! Will think about it! Thanks!

daltonmatos added a commit that referenced this issue Jun 16, 2012
The Manager is already capable of receiving a filter callback function
to filter the returned implementors list.

ref #11
@daltonmatos
Copy link
Owner Author

It's done. I will leave this code on this branch for a while and merge it back to develop branch soon.

@hugosenari
Copy link

What you do in bad cases, when filter aren't callable, user pass more params that defined for function ...?

@daltonmatos
Copy link
Owner Author

In this case I let the exception to be raised by the python interpreter. Don' t know if there is anything that plugnplay could do. Raise an exception or let a generic exception be raised by the interpreter seems the same.

And I think that failing silently would be worse, because this would cause too much confusion.

I think passing wrong arguments or a non callable object is a development problem and should be catched at develop time.

Any way, just guessing.

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

No branches or pull requests

2 participants