Software: Apache. PHP/5.3.29 uname -a: Linux tardis23.nocplanet.net 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024
Safe-mode: OFF (not secure) /opt/alt/python37/share/doc/alt-python37-mako/doc/ drwxr-xr-x | |
| Viewing file: Select action/file-type: Mako 1.1.0 Documentation
Search:
Release: 1.1.0
Mako 1.1.0 Documentation
»
The Unicode Chapter
The Unicode ChapterThe Unicode Chapter¶The Python language supports two ways of representing what we
know as “strings”, i.e. series of characters. In Python 2, the
two types are Contrast the “byte-string” type with the “unicode/string” type.
Objects of this latter type are created whenever you say something like
When Python 2 attempts to treat a byte-string as a string, which
means it’s attempting to compare/parse its characters, to coerce
it into another encoding, or to decode it to a unicode object,
it has to guess what the encoding is. In this case, it will
pretty much always guess the encoding as There is one operation that Python can do with a non-ASCII
byte-string, and it’s a great source of confusion: it can dump the
byte-string straight out to a stream or a file, with nary a care
what the encoding is. To Python, this is pretty much like
dumping any other kind of binary data (like an image) to a
stream somewhere. In Python 2, it is common to see programs that
embed all kinds of international characters and encodings into
plain byte-strings (i.e. using The “pass through encoded data” scheme is what template
languages like Cheetah and earlier versions of Myghty do by
default. Mako as of version 0.2 also supports this mode of
operation when using Python 2, using the In normal Mako operation, all parsed template constructs and
output streams are handled internally as Python Specifying the Encoding of a Template File¶This is the most basic encoding-related setting, and it is
equivalent to Python’s “magic encoding comment”, as described in
pep-0263. Any
template that contains non-ASCII characters requires that this
comment be present so that Mako can decode to unicode (and also
make usage of Python’s AST parsing services). Mako’s lexer will
use this encoding in order to convert the template source into a
## -*- coding: utf-8 -*-
Alors vous imaginez ma surprise, au lever du jour, quand
une drôle de petite voix m’a réveillé. Elle disait:
« S’il vous plaît… dessine-moi un mouton! »For the picky, the regular expression used is derived from that of the above mentioned pep: #.*coding[:=]\s*([-\w.]+).*\nThe lexer will convert to unicode in all cases, so that if any
characters exist in the template that are outside of the
specified encoding (or the default of As an alternative, the template encoding can be specified
programmatically to either t = TemplateLookup(directories=['./'], input_encoding='utf-8')The above will assume all located templates specify Handling Expressions¶The next area that encoding comes into play is in expression constructs. By default, Mako’s treatment of an expression like this: ${"hello world"}looks something like this: context.write(unicode("hello world"))In Python 3, it’s just: context.write(str("hello world"))That is, the output of all expressions is run through the
``unicode`` built-in. This is the default setting, and can be
modified to expect various encodings. The ${"voix m’a réveillé."} ## error in Python 2!You must instead say this: ${u"voix m’a réveillé."} ## OK !Similarly, if you are reading data from a file that is streaming bytes, or returning data from some object that is returning a Python byte-string containing a non-ASCII encoding, you have to explicitly decode to unicode first, such as: ${call_my_object().decode('utf-8')}Note that filehandles acquired by If you want a certain encoding applied to all expressions,
override the t = Template(templatetext, default_filters=['decode.utf8'])Note that the built-in The Defining Output Encoding¶Now that we have a template which produces a pure unicode output stream, all the hard work is done. We can take the output and do anything with it. As stated in the “Usage” chapter, both from mako.template import Template
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'], output_encoding='utf-8', encoding_errors='replace')
mytemplate = mylookup.get_template("foo.txt")
print(mytemplate.render())
print(mytemplate.render_unicode())The above method disgards the output encoding keyword argument; you can encode yourself by saying: print(mytemplate.render_unicode().encode('utf-8', 'replace'))Buffer Selection¶Mako does play some games with the style of buffering used internally, to maximize performance. Since the buffer is by far the most heavily used object in a render operation, it’s important! When calling Saying to Heck with It: Disabling the Usage of Unicode Entirely¶Some segments of Mako’s userbase choose to make no usage of
Unicode whatsoever, and instead would prefer the “pass through”
approach; all string expressions in their templates return
encoded byte-strings, and they would like these strings to pass
right through. The only advantage to this approach is that
templates need not use # -*- coding:utf-8 -*-
from mako.template import Template
t = Template("drôle de petite voix m’a réveillé.", disable_unicode=True, input_encoding='utf-8')
print(t.code)The The generated module source code will contain elements like these: # -*- coding:utf-8 -*-
# ...more generated code ...
def render_body(context,**pageargs):
context.caller_stack.push_frame()
try:
__M_locals = dict(pageargs=pageargs)
# SOURCE LINE 1
context.write('dr\xc3\xb4le de petite voix m\xe2\x80\x99a r\xc3\xa9veill\xc3\xa9.')
return ''
finally:
context.caller_stack.pop_frame()Where above that the string literal used within When The Changed in version 0.3.4: In prior versions, it used Rules for using
|
:: Command execute :: | |
--[ c99shell v.2.1 [PHP 7 Update] [1.12.2019] maintained by KaizenLouie and updated by cermmik | C99Shell Github (MySQL update) | Generation time: 0.0136 ]-- |