text
stringlengths
0
828
4201,"def encode_for_xml(text, wash=False, xml_version='1.0', quote=False):
""""""Encode special characters in a text so that it would be XML-compliant.
:param text: text to encode
:return: an encoded text
""""""
text = text.replace('&', '&')
text = text.replace('<', '&lt;')
if quote:
text = text.replace('""', '&quot;')
if wash:
text = wash_for_xml(text, xml_version=xml_version)
return text"
4202,"def wash_for_xml(text, xml_version='1.0'):
""""""Remove any character which isn't a allowed characters for XML.
The allowed characters depends on the version
of XML.
- XML 1.0:
<http://www.w3.org/TR/REC-xml/#charsets>
- XML 1.1:
<http://www.w3.org/TR/xml11/#charsets>
:param text: input string to wash.
:param xml_version: version of the XML for which we wash the
input. Value for this parameter can be '1.0' or '1.1'
""""""
if xml_version == '1.0':
return RE_ALLOWED_XML_1_0_CHARS.sub(
'', unicode(text, 'utf-8')).encode('utf-8')
else:
return RE_ALLOWED_XML_1_1_CHARS.sub(
'', unicode(text, 'utf-8')).encode('utf-8')"
4203,"def wash_for_utf8(text, correct=True):
""""""Return UTF-8 encoded binary string with incorrect characters washed away.
:param text: input string to wash (can be either a binary or Unicode string)
:param correct: whether to correct bad characters or throw exception
""""""
if isinstance(text, unicode):
return text.encode('utf-8')
errors = ""ignore"" if correct else ""strict""
return text.decode(""utf-8"", errors).encode(""utf-8"", errors)"
4204,"def nice_number(number, thousands_separator=',', max_ndigits_after_dot=None):
""""""Return nicely printed number NUMBER in language LN.
Return nicely printed number NUMBER in language LN using
given THOUSANDS_SEPARATOR character.
If max_ndigits_after_dot is specified and the number is float, the
number is rounded by taking in consideration up to max_ndigits_after_dot
digit after the dot.
This version does not pay attention to locale. See
tmpl_nice_number_via_locale().
""""""
if isinstance(number, float):
if max_ndigits_after_dot is not None:
number = round(number, max_ndigits_after_dot)
int_part, frac_part = str(number).split('.')
return '%s.%s' % (nice_number(int(int_part), thousands_separator),
frac_part)
else:
chars_in = list(str(number))
number = len(chars_in)
chars_out = []
for i in range(0, number):
if i % 3 == 0 and i != 0:
chars_out.append(thousands_separator)
chars_out.append(chars_in[number - i - 1])
chars_out.reverse()
return ''.join(chars_out)"
4205,"def nice_size(size):
""""""Nice size.
:param size: the size.
:type size: int
:return: a nicely printed size.
:rtype: string
""""""
unit = 'B'
if size > 1024:
size /= 1024.0
unit = 'KB'
if size > 1024:
size /= 1024.0
unit = 'MB'
if size > 1024:
size /= 1024.0
unit = 'GB'
return '%s %s' % (nice_number(size, max_ndigits_after_dot=2), unit)"
4206,"def remove_line_breaks(text):
""""""Remove line breaks from input.
Including unicode 'line separator', 'paragraph separator',
and 'next line' characters.
""""""
return unicode(text, 'utf-8').replace('\f', '').replace('\n', '') \
.replace('\r', '').replace(u'\xe2\x80\xa8', '') \