Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding=utf8
- class Base(object):
- @classmethod
- def create(cls, data):
- obj = cls(data)
- obj.write()
- return obj
- def write(self):
- raise NotImplementedError()
- class Topic(Base):
- def __init__(self, data):
- self.data = data
- def write(self):
- pass
- class Content(Base):
- def __init__(self, data):
- self.data = data
- def write(self):
- pass
- # Copyright © 2012-13 Qtrac Ltd. All rights reserved.
- # This program or module is free software: you can redistribute it
- # and/or modify it under the terms of the GNU General Public License as
- # published by the Free Software Foundation, either version 3 of the
- # License, or (at your option) any later version. It is provided for
- # educational purposes and is distributed in the hope that it will be
- # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # General Public License for more details.
- import abc
- import sys
- # import bottle
- class AbstractItem(object):
- __metaclass__ = abc.ABCMeta
- @abc.abstractproperty
- def composite(self):
- pass
- def __iter__(self):
- return iter([])
- class SimpleItem(AbstractItem):
- def __init__(self, name, data='__undefined__'):
- self.name = name
- self.data = data
- @property
- def composite(self):
- return False
- def prints(self, indent="", _file=sys.stdout):
- # print("{}${:.2f} {}".format(indent, self.price, self.name), file=_file)
- print "{}${:s} {}".format(indent, self.data, self.name)
- class AbstractCompositeItem(AbstractItem):
- def __init__(self, *items):
- self.children = []
- if items:
- self.add(*items)
- def add(self, first, *items):
- self.children.append(first)
- if items:
- self.children.extend(items)
- def remove(self, item):
- self.children.remove(item)
- def __iter__(self):
- return iter(self.children)
- class CompositeItem(AbstractCompositeItem):
- def __init__(self, name, *items):
- super(CompositeItem, self).__init__(*items)
- self.name = name
- @property
- def composite(self):
- return True
- @property
- def data(self):
- return [item.data for item in self]
- def prints(self, indent="", _file=sys.stdout):
- # print("{}${:.2f} {}".format(indent, self.price, self.name), file=file)
- print "{}${:s} {}".format(indent, self.data, self.name)
- for child in self:
- child.prints(indent + " ")
- if __name__ == '__main__':
- full = SimpleItem('full', 'abc')
- intro = SimpleItem('intro', 'abc')
- preview = SimpleItem('preview', 'abc')
- description = CompositeItem('description', full, intro, preview)
- title = SimpleItem('title', 'abc')
- poll = CompositeItem('poll', title)
- poll.add(SimpleItem('question_1'))
- poll.add(SimpleItem('question_2'))
- poll.add(SimpleItem('question_3'))
- for item in (full, intro, preview, description, title, poll):
- item.prints()
- t = Topic.create({
- 'title': 'abv'
- })
- c = Content.create((full, intro, preview, description, title, poll))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement