Skip to content

Instantly share code, notes, and snippets.

@lost-theory
Created October 22, 2012 05:10
Show Gist options
  • Save lost-theory/3929765 to your computer and use it in GitHub Desktop.
Save lost-theory/3929765 to your computer and use it in GitHub Desktop.
composition vs. inheritance
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')
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')
== 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