public
Last active

composition vs. inheritance

  • Download Gist
composition.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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')
inheritance.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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')
output
1 2 3 4 5 6 7 8 9
 
== 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'}]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.