Mercurial > pylearn
view pylearn/formulas/tags.py @ 1489:35a3a4e2d999
added formulas/nnet.py with inverse_max_pooling(...)
author | Emmanuel Bengio <bengioe@iro.umontreal.ca> |
---|---|
date | Wed, 27 Jul 2011 10:37:41 -0400 |
parents | de153244c8e5 |
children |
line wrap: on
line source
from collections import defaultdict tags_db = defaultdict(set) def tags(*_tags): tags = set() def add_tag(tag): if isinstance(tag, (list, tuple)): map(add_tag, tag) elif isinstance(tag, (str, unicode)): for word in tag.split(" "): tags.add(word) tags.add(tag) else: raise TypeError("Tags should be strings or lists/tuples of strings. Got: %s, of type %s" % (tag, type(tag))) map(add_tag, _tags) tags = tuple(sorted(tags)) def decorator(function): function.tags = tags function.__doc__ += "\n\nTags: %s" % ", ".join(tags) for tag in tags: tags_db[tag].add(function) return function return decorator def search(*tags): return reduce(set.__and__, [tags_db[tag] for tag in tags]) if __name__ == '__main__': common_tags = ['c', 'd'] @tags(common_tags, 'a', 'b', 'long tag') def f(a,b): ''' function f returns a+b ''' return a+b @tags(common_tags, 'x') def g(a,b): ''' function f returns a-b ''' return a-b @tags('c', 'x', 'y', 'z') def h(a,b): ''' function f returns a*b ''' return a*b print f.__doc__ print [x.__name__ for x in search('c', 'd')] print [x.__name__ for x in search('x')]