from contextlib import contextmanager
@contextmanager
def tag(name):
print "<%s>" % name,
yield
print "</%s>" % name,
with tag("h1"):
print "moo",
print
with tag("div"):
print "foo",
<h1>moo</h1>
<div>foo</div>
# -*-coding:utf-8 -*-
class SimpleText(object):
def __init__(self, text):
self.text = text
def content(self):
return self.text
def __str__(self):
return self.content()
class TagDecorator(SimpleText):
def __init__(self, text, tag):
super(TagDecorator, self).__init__(text)
self.tag = tag
def content(self):
return '<{0}>{1}</{0}>'.format(self.tag, super(TagDecorator, self).content())
a = SimpleText('moo')
print('SimpleText: %s' % a)
b = TagDecorator('moo', 'h1')
print('TagDecorator (h1): %s' % b)