本文整理汇总了Python中jinja2.Template.generate方法的典型用法代码示例。如果您正苦于以下问题:Python Template.generate方法的具体用法?Python Template.generate怎么用?Python Template.generate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.Template
的用法示例。
在下文中一共展示了Template.generate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_async_iteration_in_templates
# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import generate [as 别名]
def test_async_iteration_in_templates():
t = Template('{% for x in rng %}{{ x }}{% endfor %}',
enable_async=True)
async def async_iterator():
for item in [1, 2, 3]:
yield item
rv = list(t.generate(rng=async_iterator()))
assert rv == ['1', '2', '3']
示例2: test_async_iteration_in_templates_extended
# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import generate [as 别名]
def test_async_iteration_in_templates_extended():
t = Template('{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}',
enable_async=True)
async def async_iterator():
for item in [1, 2, 3]:
yield item
rv = list(t.generate(rng=async_iterator()))
assert rv == ['0/1', '1/2', '2/3']
示例3: test_async_generate
# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import generate [as 别名]
def test_async_generate():
t = Template('{% for x in [1, 2, 3] %}{{ x }}{% endfor %}',
enable_async=True)
rv = list(t.generate())
assert rv == ['1', '2', '3']
示例4: Controller
# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import generate [as 别名]
class Controller(object):
"""
The abstract base class for the controller of storage. This class does not actually impliment any
functionality (other than the constructor) but rather sets the method contracts for the methods supported by all
controllers.
:param template: The template string or Template instance for the jinja2 template used to create the \
contents of the tiny
:type template: str or jinja2.Template
:param str prefix_separator: The seperator between the prefix and tiny_text
:param int initial_tiny_length: The length to try for autogenerating tiny text
:param int max_retries: The max number if attempts to generate a unique tiny
:param bool overwrite: Overwrite (if tiny text is provided)
:ivar jinja2.Template template: The template used to generate the HTML of the tinyurl
:ivar str prefix_separator: The separator betweek the prefix and tiny_text for any URL created by :func:`create_url`
:ivar int initial_tiny_length: The initial length to attempt the auto_generated tiny_text, increased ny one for \
each collision after the second.
:ivar int max_retries: The maximum number of collisions that will be tolerated before raising an exception
:ivar bool overwrite: Whether a duplicat (provided) tiny_text will overwrite \
(otherwise raises :class:`TinyURLExistsException`)
"""
__metaclass__ = ABCMeta
def __init__(self, template, prefix_separator=".", tiny_length=4, max_retries=5, overwrite=False, analytics=None):
self.prefix_separator = prefix_separator
self.initial_tiny_length = tiny_length
self.max_retries = max_retries
self.overwrite = overwrite
self._template = None
self.template = template
self.analytics = analytics
@property
def template(self):
"""
The jinja2 template used to create the meta redirect
:setter: Takes either a string (which is a valid Template) or jinja2.Template object
:getter: Returns the jinja2.Template object
:type: jinja2.Template
"""
return self._template
@template.setter
def template(self, template):
"""
Set the template
:param template: The template the controller will use to generate the tinyURL
:type template: str, jinja2.Template
:raises; AttributeError
"""
if isinstance(template, Template):
self._template = template
else:
self.template = Template(template)
@abstractmethod
def put(self, url):
"""
Put the tiny url to storage.
If the tiny_text is provided in the url, it will be used (and overwritten is specified)
Otherwise, it will attempt to generate non-conflicting tiny text up to max_retries times.
Often, with large number of tiny_urls we see failure counts increase, in which case the initial_tiny_length
should be increased to increase the namespace
:param url: the stiny.utl.StaticURL to be generated
:raises: TooManyNameCollisions - if we're unable to autogenerate a tiny_text name
:raises: TinyURLExistsException - if the provided tiny exists and we're not overwriting
"""
pass
@abstractmethod
def get(self, tiny_uri):
"""
Get the tiny url from storage.
If the tiny_text is provided in the url, it will be used (and overwritten is specified)
Otherwise, it will attempt to generate non-conflicting tiny text up to max_retries times.
Often, with large number of tiny_urls we see failure counts increase, in which case the initial_tiny_length
should be increased to increase the namespace
:param tiny_uri: the name of the tiny url
:raises: TinyURLDoesNotExistsException - if the provided tiny exists and we're not overwriting
"""
pass
@abstractmethod
def delete(self, url):
"""
Delete the tiny url from storage.
:param url: the stiny.utl.StaticURL to be deleted (tiny_text must be provided) or str of tiny_text
:raises: TinyURLDoesNotExistsException - if the provided tiny does not exist
"""
pass
@abstractmethod
#.........这里部分代码省略.........
示例5: Template
# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import generate [as 别名]
from jinja2 import Template
template_str = 'first={{first}}\n' \
'last={{last}}'
template = Template(template_str)
stream = template.stream({'first': 'John', 'last': 'Doe'})
count = 0
while True:
try:
count += 1
resolved = stream.next()
print count, resolved
except StopIteration:
break
generator = template.generate({'first': 'John', 'last': 'Doe'})
count = 0
for s in generator:
count += 1
print count, s