Project

Profile

Help

Refactor #2169

closed

Suggestion: Start using string format instead of Django templating in generation of repodata

Added by fdobrovo over 7 years ago. Updated about 5 years ago.

Status:
CLOSED - WONTFIX
Priority:
Normal
Assignee:
-
Sprint/Milestone:
-
Start date:
Due date:
% Done:

0%

Estimated time:
Platform Release:
Groomed:
No
Sprint Candidate:
No
Tags:
Pulp 2
Sprint:
Quarter:

Description

I figured out that we could start using plain

"".format()
# or
"" % Dict()

Instead of using Django templateing.
It has a lot simpler escaping (see below) and in my measurements on 12 474 characters long changelog, format is about 22 times faster than Django templates and modulo about double of that.

It would be quite easy transition.

# DAJNGO
t = '<package pkgid="{{ pkgid }}" name="yum" arch="noarch">'
Template(t).render(Context({"pkgid": "some_package_id"}))
# FORMAT
t = '<package pkgid="{ pkgid }" name="yum" arch="noarch">'
t.format(**{"pkgid": "some_package_id"})
# MODULO
t = '<package pkgid="%(pkgid)s" name="yum" arch="noarch">'
t % {"pkgid": "some_package_id"}

Escaping would be also easier:

template = ('<tag>{some {{ var }}</tag><some_tag>text</some_tag>'
            '<tag>some {% tag %} {# comment #} } $(test)s</tag>')

# After Django escaping:
'<tag>{% templatetag openbrace %}some {% templatetag openvariable %}'
' var {% templatetag closevariable %}</tag><some_tag>text</some_tag>'
'<tag>some {% templatetag openblock %} tag '
'{% templatetag closeblock %} {% templatetag opencomment %} comment '
'{% templatetag closecomment %} {% templatetag closebrace %} $(test)s</tag>'

# After Format escaping:
'<tag>{{some {{{{ var }}}}</tag><some_tag>text</some_tag>'
'<tag>some {{% tag %}} {{# comment #}} }} $(test)s</tag>'

# After modulo escaping:
'<tag>{some {{ var }}</tag><some_tag>text</some_tag>'
'<tag>some {% tag %} {# comment #} } $$(test)s</tag>'

FORMAT:
The only escaping needed is doubling all { and }.
This Method was introduced in python 2.6
MODULO:
The only escaping needed is doubling all $.

FORMAT Drawback:
Format does not work well with non-ascii characters. Solution is to use "from future import unicode_literals" or convert all strings it uses to unicode.

MODULO Drawback:
Support only Dict or only positional arguments. Can't be used with both. But this probably does not bothers us?

Also available in: Atom PDF