Advertisement
vasilyev_alexander

Untitled

Aug 29th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf8
  3.  
  4. class Base(object):
  5.     @classmethod
  6.     def create(cls, data):
  7.         obj = cls(data)
  8.         obj.write()
  9.         return obj
  10.  
  11.     def write(self):
  12.         raise NotImplementedError()
  13.  
  14.  
  15. class Topic(Base):
  16.     def __init__(self, data):
  17.         self.data = data
  18.  
  19.     def write(self):
  20.         pass
  21.  
  22.  
  23. class Content(Base):
  24.     def __init__(self, data):
  25.         self.data = data
  26.  
  27.     def write(self):
  28.         pass
  29.  
  30.  
  31. # Copyright © 2012-13 Qtrac Ltd. All rights reserved.
  32. # This program or module is free software: you can redistribute it
  33. # and/or modify it under the terms of the GNU General Public License as
  34. # published by the Free Software Foundation, either version 3 of the
  35. # License, or (at your option) any later version. It is provided for
  36. # educational purposes and is distributed in the hope that it will be
  37. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. # General Public License for more details.
  40.  
  41. import abc
  42. import sys
  43. # import bottle
  44.  
  45.  
  46. class AbstractItem(object):
  47.  
  48.     __metaclass__ = abc.ABCMeta
  49.  
  50.     @abc.abstractproperty
  51.     def composite(self):
  52.         pass
  53.  
  54.     def __iter__(self):
  55.         return iter([])
  56.  
  57.  
  58. class SimpleItem(AbstractItem):
  59.  
  60.     def __init__(self, name, data='__undefined__'):
  61.         self.name = name
  62.         self.data = data
  63.  
  64.  
  65.     @property
  66.     def composite(self):
  67.         return False
  68.  
  69.  
  70.     def prints(self, indent="", _file=sys.stdout):
  71.         # print("{}${:.2f} {}".format(indent, self.price, self.name), file=_file)
  72.         print "{}${:s} {}".format(indent, self.data, self.name)
  73.  
  74. class AbstractCompositeItem(AbstractItem):
  75.  
  76.     def __init__(self, *items):
  77.         self.children = []
  78.         if items:
  79.             self.add(*items)
  80.  
  81.  
  82.     def add(self, first, *items):
  83.         self.children.append(first)
  84.         if items:
  85.             self.children.extend(items)
  86.  
  87.  
  88.     def remove(self, item):
  89.         self.children.remove(item)
  90.  
  91.  
  92.     def __iter__(self):
  93.         return iter(self.children)
  94.  
  95.  
  96. class CompositeItem(AbstractCompositeItem):
  97.  
  98.     def __init__(self, name, *items):
  99.         super(CompositeItem, self).__init__(*items)
  100.         self.name = name
  101.  
  102.  
  103.     @property
  104.     def composite(self):
  105.         return True
  106.  
  107.  
  108.     @property
  109.     def data(self):
  110.         return [item.data for item in self]
  111.  
  112.  
  113.     def prints(self, indent="", _file=sys.stdout):
  114.         # print("{}${:.2f} {}".format(indent, self.price, self.name), file=file)
  115.         print "{}${:s} {}".format(indent, self.data, self.name)
  116.         for child in self:
  117.             child.prints(indent + "      ")
  118.  
  119.  
  120. if __name__ == '__main__':
  121.     full = SimpleItem('full', 'abc')
  122.     intro = SimpleItem('intro', 'abc')
  123.     preview = SimpleItem('preview', 'abc')
  124.     description = CompositeItem('description', full, intro, preview)
  125.  
  126.     title = SimpleItem('title', 'abc')
  127.  
  128.     poll = CompositeItem('poll', title)
  129.     poll.add(SimpleItem('question_1'))
  130.     poll.add(SimpleItem('question_2'))
  131.     poll.add(SimpleItem('question_3'))
  132.  
  133.     for item in (full, intro, preview, description, title, poll):
  134.         item.prints()
  135.  
  136.  
  137.  
  138.     t = Topic.create({
  139.         'title': 'abv'
  140.     })
  141.  
  142.     c = Content.create((full, intro, preview, description, title, poll))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement