Skip to content

@lost-theory /composition.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.