Source

wheezy.template / demos / bigtable / bigtable.py

Full commit
Andriy Kornatsky… 2909527 















Andriy Kornatsky… 2cba00c 

Andriy Kornatsky… 2909527 






Andriy Kornatsky… 2cba00c 

Andriy Kornatsky… 2909527 













Andriy Kornatsky… 2cba00c 


Andriy Kornatsky… 2909527 














Andriy Kornatsky… 778ef0d 
Andriy Kornatsky… 2909527 


Andriy Kornatsky… 2cba00c 

Andriy Kornatsky… 2909527 













Andriy Kornatsky… 2cba00c 


Andriy Kornatsky… 2909527 




























































Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 2909527 





















Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 2909527 
Andriy Kornatsky… 99fe64f 
Andriy Kornatsky… 2909527 



















Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 2909527 










Andriy Kornatsky… b32aafe 


Andriy Kornatsky… d53a103 
Andriy Kornatsky… b32aafe 






Andriy Kornatsky… d53a103 
Andriy Kornatsky… 2909527 










Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 2909527 



Andriy Kornatsky… 3f5851e 






























Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 3f5851e 





Andriy Kornatsky… 9ab21cf 




















Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… 9ab21cf 


Andriy Kornatsky… fd7f733 















Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… fd7f733 


Andriy Kornatsky… 59c3172 
Andriy Kornatsky… 9ab21cf 
Andriy Kornatsky… 59c3172 




















Andriy Kornatsky… e48f13e 
Andriy Kornatsky… 59c3172 



Andriy Kornatsky… e48f13e 
Andriy Kornatsky… edcde98 
Andriy Kornatsky… 2909527 
Andriy Kornatsky… edcde98 
Andriy Kornatsky… 2909527 

Andriy Kornatsky… 2cba00c 
Andriy Kornatsky… edcde98 
Andriy Kornatsky… 2909527 

Andriy Kornatsky… 99fe64f 
Andriy Kornatsky… 2909527 


Andriy Kornatsky… edcde98 






Andriy Kornatsky… 2909527 
Andriy Kornatsky… edcde98 
Andriy Kornatsky… 2909527 



"""
"""

import sys

try:
    from wheezy.html.utils import escape_html as escape
except ImportError:
    import cgi
    escape = cgi.escape

PY3 = sys.version_info[0] >= 3
s = PY3 and str or unicode

ctx = {
    'table': [dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10)
              for x in range(1000)]
}


# region: python list append

if PY3:
    def test_list_append():
        b = []
        w = b.append
        table = ctx['table']
        w('<table>\n')
        for row in table:
            w('<tr>\n')
            for key, value in row.items():
                w('<td>')
                w(escape(key))
                w('</td><td>')
                w(str(value))
                w('</td>\n')
            w('</tr>\n')
        w('</table>')
        return ''.join(b)
else:
    def test_list_append():  # noqa
        b = []
        w = b.append
        table = ctx['table']
        w(u'<table>\n')
        for row in table:
            w(u'<tr>\n')
            for key, value in row.items():
                w(u'<td>')
                w(escape(key))
                w(u'</td><td>')
                w(unicode(value))
                w(u'</td>\n')
            w(u'</tr>\n')
        w(u'</table>')
        return ''.join(b)


# region: python list extend

if PY3:
    def test_list_extend():
        b = []
        e = b.extend
        table = ctx['table']
        e(('<table>\n',))
        for row in table:
            e(('<tr>\n',))
            for key, value in row.items():
                e(('<td>',
                   escape(key),
                   '</td><td>',
                   str(value),
                   '</td>\n'))
            e(('</tr>\n',))
        e(('</table>',))
        return ''.join(b)
else:
    def test_list_extend():  # noqa
        b = []
        e = b.extend
        table = ctx['table']
        e((u'<table>\n',))
        for row in table:
            e((u'<tr>\n',))
            for key, value in row.items():
                e((u'<td>',
                   escape(key),
                   u'</td><td>',
                   unicode(value),
                   u'</td>\n'))
            e((u'</tr>\n',))
        e((u'</table>',))
        return ''.join(b)


# region: wheezy.template

try:
    from wheezy.template.engine import Engine
    from wheezy.template.loader import DictLoader
    from wheezy.template.ext.core import CoreExtension
except ImportError:
    test_wheezy_template = None
else:
    engine = Engine(loader=DictLoader({'x': s("""\
@require(table)
<table>
    @for row in table:
    <tr>
        @for key, value in row.items():
        <td>@key!h</td><td>@value!s</td>
        @end
    </tr>
    @end
</table>
""")}), extensions=[CoreExtension()])
    engine.global_vars.update({'h': escape})
    wheezy_template = engine.get_template('x')

    def test_wheezy_template():
        return wheezy_template.render(ctx)


# region: Jinja2

try:
    from jinja2 import Environment
except ImportError:
    test_jinja2 = None
else:
    jinja2_template = Environment().from_string(s("""\
<table>
    {% for row in table: %}
    <tr>
        {% for key, value in row.items(): %}
        <td>{{ key | e }}</td><td>{{ value }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
"""))

    def test_jinja2():
        return jinja2_template.render(ctx)


# region: tornado

try:
    from tornado.template import Template
except ImportError:
    test_tornado = None
else:
    tornado_template = Template(s("""\
<table>
    {% for row in table %}
    <tr>
        {% for key, value in row.items() %}
        <td>{{ key }}</td><td>{{ value }}</td>
        {% end %}
    </tr>
    {% end %}
</table>
"""))

    def test_tornado():
        return tornado_template.generate(**ctx).decode('utf8')


# region: mako

try:
    from mako.template import Template
except ImportError:
    test_mako = None
else:
    mako_template = Template(s("""\
<table>
    % for row in table:
    <tr>
        % for key, value in row.items():
        <td>${ key | h }</td><td>${ value }</td>
        % endfor
    </tr>
    % endfor
</table>
"""))

    def test_mako():
        return mako_template.render(**ctx)


# region: tenjin

try:
    import tenjin
except ImportError:
    test_tenjin = None
else:
    try:
        import webext
        helpers = {
            'to_str': webext.to_str,
            'escape': webext.escape_html
        }
    except ImportError:
        helpers = {
            'to_str': tenjin.helpers.to_str,
            'escape': tenjin.helpers.escape
        }
    tenjin_template = tenjin.Template(encoding='utf8')
    tenjin_template.convert(s("""\
<table>
    <?py for row in table: ?>
    <tr>
        <?py for key, value in row.items(): ?>
        <td>${ key }</td><td>#{ value }</td>
        <?py #end ?>
    </tr>
    <?py #end ?>
</table>
"""))

    def test_tenjin():
        return tenjin_template.render(ctx, helpers)


# region: web2py

try:
    import cStringIO
    from gluon.html import xmlescape
    from gluon.template import get_parsed
except ImportError:
    test_web2py = None
else:
    # see gluon.globals.Response
    class DummyResponse(object):
        def __init__(self):
            self.body = cStringIO.StringIO()

        def write(self, data, escape=True):
            if not escape:
                self.body.write(str(data))
            else:
                self.body.write(xmlescape(data))

    web2py_template = compile(get_parsed(s("""\
<table>
    {{ for row in table: }}
    <tr>
        {{ for key, value in row.items(): }}
        <td>{{ =key }}</td><td>{{ =value }}</td>
        {{ pass }}
    </tr>
    {{ pass }}
</table>
""")), '', 'exec')

    def test_web2py():
        response = DummyResponse()
        exec(web2py_template, {}, dict(response=response, **ctx))
        return response.body.getvalue().decode('utf8')


# region: django

try:
    from django.conf import settings
    settings.configure()
    from django.template import Template
    from django.template import Context
except ImportError:
    test_django = None
else:
    django_template = Template(s("""\
<table>
    {% for row in table %}
    <tr>
        {% for key, value in row.items %}
        <td>{{ key }}</td><td>{{ value }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
"""))

    def test_django():
        return django_template.render(Context(ctx))

# region: chameleon

try:
    from chameleon.zpt.template import PageTemplate
except ImportError:
    test_chameleon = None
else:
    chameleon_template = PageTemplate(s("""\
<table>
    <tr tal:repeat="row table">
        <i tal:omit-tag="" tal:repeat="key row">
        <td>${key}</td><td>${row[key]}</td>
        </i>
    </tr>
</table>
"""))

    def test_chameleon():
        return chameleon_template.render(**ctx)

#region: cheetah

try:
    from Cheetah.Template import Template
except ImportError:
    test_cheetah = None
else:
    cheetah_ctx = {}
    cheetah_template = Template(s("""\
#import cgi
<table>
    #for $row in $table
    <tr>
        #for $key, $value in $row.items
        <td>$cgi.escape($key)</td><td>$value</td>
        #end for
    </tr>
    #end for
</table>
"""), searchList=[cheetah_ctx])

    def test_cheetah():
        cheetah_ctx.update(ctx)
        output = cheetah_template.respond()
        cheetah_ctx.clear()
        return output


def run(number=100):
    import profile
    from timeit import Timer
    from pstats import Stats
    names = globals().keys()
    names = sorted([(name, globals()[name])
                    for name in names if name.startswith('test_')])
    print("                    msec    rps  tcalls  funcs")
    for name, test in names:
        if test:
            assert isinstance(test(), s)
            t = Timer(setup='from __main__ import %s as t' % name,
                      stmt='t()')
            t = t.timeit(number=number)
            st = Stats(profile.Profile().runctx(
                'test()', globals(), locals()))
            print('%-17s %6.2f %6.2f %7d %6d' % (name[5:],
                                                 1000 * t / number,
                                                 number / t,
                                                 st.total_calls,
                                                 len(st.stats)))
        else:
            print('%-25s not installed' % name[5:])


if __name__ == '__main__':
    run()