Created
October 22, 2012 05:10
-
-
Save lost-theory/3929765 to your computer and use it in GitHub Desktop.
composition vs. inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListView(object): | |
def get_items(self): | |
return [] | |
class CategoryView(ListView): | |
def get_items(self): | |
return [ | |
{'id': 1, 'title': 'First post', 'body': 'Hello world.', 'tags': ['red', 'blue'], 'category': 'something'}, | |
{'id': 2, 'title': 'Second post', 'body': 'Jello world.', 'tags': ['blue', 'green'], 'category': 'cool'}, | |
{'id': 3, 'title': 'Third post', 'body': 'Yellow world.', 'tags': ['yellow'], 'category': 'cool'}, | |
] | |
class Modifier(object): | |
def __init__(self, listview): | |
self.listview = listview | |
class TagModifier(Modifier): | |
def tagged(self, tagname): | |
return [p for p in self.listview.get_items() if tagname in p['tags']] | |
class SearchModifier(Modifier): | |
def search(self, query): | |
return [p for p in self.listview.get_items() if query in str(p.values()).lower()] | |
class CustomCategoryView(CategoryView): | |
def __init__(self): | |
super(CustomCategoryView, self).__init__() | |
self.tagmod = TagModifier(self) | |
self.searchmod = SearchModifier(self) | |
def tagged(self, tagname): | |
return self.tagmod.tagged(tagname) | |
def search(self, query): | |
return self.searchmod.search(query) | |
if __name__ == "__main__": | |
ccv = CustomCategoryView() | |
print "\n== get_items ==" | |
print ccv.get_items() | |
print "\n== tagged('blue') ==" | |
print ccv.tagged('blue') | |
print "\n== search('jello') ==" | |
print ccv.search('jello') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListView(object): | |
def get_items(self): | |
return [] | |
class CategoryView(ListView): | |
def get_items(self): | |
return [ | |
{'id': 1, 'title': 'First post', 'body': 'Hello world.', 'tags': ['red', 'blue'], 'category': 'something'}, | |
{'id': 2, 'title': 'Second post', 'body': 'Jello world.', 'tags': ['blue', 'green'], 'category': 'cool'}, | |
{'id': 3, 'title': 'Third post', 'body': 'Yellow world.', 'tags': ['yellow'], 'category': 'cool'}, | |
] | |
class TagModifier(ListView): | |
def tagged(self, tagname): | |
return [p for p in super(TagModifier, self).get_items() if tagname in p['tags']] | |
class SearchModifier(ListView): | |
def search(self, query): | |
return [p for p in super(TagModifier, self).get_items() if query in str(p.values()).lower()] | |
class CustomCategoryView(TagModifier, SearchModifier, CategoryView): | |
pass | |
if __name__ == "__main__": | |
ccv = CustomCategoryView() | |
print "\n== get_items ==" | |
print ccv.get_items() | |
print "\n== tagged('blue') ==" | |
print ccv.tagged('blue') | |
print "\n== search('jello') ==" | |
print ccv.search('jello') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
== get_items == | |
[{'body': 'Hello world.', 'category': 'something', 'tags': ['red', 'blue'], 'id': 1, 'title': 'First post'}, {'body': 'Jello world.', 'category': 'cool', 'tags': ['blue', 'green'], 'id': 2, 'title': 'Second post'}, {'body': 'Yellow world.', 'category': 'cool', 'tags': ['yellow'], 'id': 3, 'title': 'Third post'}] | |
== tagged('blue') == | |
[{'body': 'Hello world.', 'category': 'something', 'tags': ['red', 'blue'], 'id': 1, 'title': 'First post'}, {'body': 'Jello world.', 'category': 'cool', 'tags': ['blue', 'green'], 'id': 2, 'title': 'Second post'}] | |
== search('jello') == | |
[{'body': 'Jello world.', 'category': 'cool', 'tags': ['blue', 'green'], 'id': 2, 'title': 'Second post'}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment