本文整理汇总了Python中__future__.unicode_literals方法的典型用法代码示例。如果您正苦于以下问题:Python __future__.unicode_literals方法的具体用法?Python __future__.unicode_literals怎么用?Python __future__.unicode_literals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类__future__
的用法示例。
在下文中一共展示了__future__.unicode_literals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_future_unicode_literals
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_future_unicode_literals(self):
self.assertParsesGen(
[{"ty": "ImportFrom",
"names": [{"ty": "alias", "name": "unicode_literals", "asname": None}],
"module": "__future__", "level": 0},
{"ty": "Expr", "value": {"ty": "Str", "s": UnicodeOnly("foo")}}],
"from __future__ import unicode_literals·'foo'",
only_if=lambda ver: ver < (3, 0))
# This test does not validate because unicode_literals import only
# affects subsequent string literals in pythonparser, whereas in ast it
# affects all string literals in the file.
self.assertParsesGen(
[{"ty": "Expr", "value": {"ty": "Str", "s": BytesOnly(b"foo")}},
{"ty": "ImportFrom",
"names": [{"ty": "alias", "name": "unicode_literals", "asname": None}],
"module": "__future__", "level": 0}],
"'foo'·from __future__ import unicode_literals",
only_if=lambda ver: ver == (2, 7), validate_if=lambda: False)
#
# DIAGNOSTICS
#
示例2: setproctitle
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def setproctitle(title):
assert isinstance(title, compat_str)
# ctypes in Jython is not complete
# http://bugs.jython.org/issue2148
if sys.platform.startswith('java'):
return
try:
libc = ctypes.cdll.LoadLibrary('libc.so.6')
except OSError:
return
except TypeError:
# LoadLibrary in Windows Python 2.7.13 only expects
# a bytestring, but since unicode_literals turns
# every string into a unicode string, it fails.
return
title_bytes = title.encode('utf-8')
buf = ctypes.create_string_buffer(len(title_bytes))
buf.value = title_bytes
try:
libc.prctl(15, buf, 0, 0, 0)
except AttributeError:
return # Strange libc, just skip this
示例3: test_treewalker_six_mix
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_treewalker_six_mix():
"""Str/Unicode mix. If str attrs added to tree"""
# On Python 2.x string literals are of type str. Unless, like this
# file, the programmer imports unicode_literals from __future__.
# In that case, string literals become objects of type unicode.
# This test simulates a Py2 user, modifying attributes on a document
# fragment but not using the u'' syntax nor importing unicode_literals
sm_tests = [
('<a href="http://example.com">Example</a>',
[(str('class'), str('test123'))],
'<a>\n class="test123"\n href="http://example.com"\n "Example"'),
('<link href="http://example.com/cow">',
[(str('rel'), str('alternate'))],
'<link>\n href="http://example.com/cow"\n rel="alternate"\n "Example"')
]
for tree in treeTypes.items():
for intext, attrs, expected in sm_tests:
yield runTreewalkerEditTest, intext, expected, attrs, tree
示例4: test_treewalker_six_mix
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_treewalker_six_mix():
"""Str/Unicode mix. If str attrs added to tree"""
# On Python 2.x string literals are of type str. Unless, like this
# file, the programmer imports unicode_literals from __future__.
# In that case, string literals become objects of type unicode.
# This test simulates a Py2 user, modifying attributes on a document
# fragment but not using the u'' syntax nor importing unicode_literals
sm_tests = [
('<a href="http://example.com">Example</a>',
[(str('class'), str('test123'))],
'<a>\n class="test123"\n href="http://example.com"\n "Example"'),
('<link href="http://example.com/cow">',
[(str('rel'), str('alternate'))],
'<link>\n href="http://example.com/cow"\n rel="alternate"\n "Example"')
]
for tree in sorted(treeTypes.items()):
for intext, attrs, expected in sm_tests:
yield runTreewalkerEditTest, intext, expected, attrs, tree
示例5: strip_future_imports
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
示例6: transform
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
示例7: transform
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def transform(self, node, results):
future_import(u"unicode_literals", node)
示例8: read
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def read(self, filenames, encoding=None):
"""Read and parse a filename or a list of filenames.
Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
configuration file locations (e.g. current directory, user's
home directory, systemwide directory), and all existing
configuration files in the list will be read. A single
filename may also be given.
Return list of successfully read files.
"""
if PY2 and isinstance(filenames, bytes):
# we allow for a little unholy magic for Python 2 so that
# people not using unicode_literals can still use the library
# conveniently
warnings.warn(
"You passed a bytestring as `filenames`. This will not work"
" on Python 3. Use `cp.read_file()` or switch to using Unicode"
" strings across the board.",
DeprecationWarning,
stacklevel=2,
)
filenames = [filenames]
elif isinstance(filenames, str):
filenames = [filenames]
read_ok = []
for filename in filenames:
try:
with open(filename, encoding=encoding) as fp:
self._read(fp, filename)
except IOError:
continue
read_ok.append(filename)
return read_ok
示例9: testParseWithNulls
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def testParseWithNulls(self):
# This relies on the from __future__ import unicode_literals, because
# explicitly specifying a unicode literal is a syntax error in Py 3.2
# May want to switch to u'...' if we ever drop Python 3.2 support.
pstring = '\x00\x00August 29, 1924'
self.assertEqual(parse(pstring),
datetime(1924, 8, 29))
示例10: add_flags
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def add_flags(self, flags):
if "print_function" in flags:
self.lexer.print_function = True
if "unicode_literals" in flags:
self.lexer.unicode_literals = True
# Grammar
示例11: _getdoc
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def _getdoc(obj):
u = obj.__doc__
try:
return u'\n'.join(l.strip() for l in u.split(u'\n') if l.strip())
except UnicodeDecodeError:
raise AssertionError(
'{}: Docstring uses unicode but {} misses the line ``from __future__ import unicode_literals``'
.format(obj, type(obj).__module__)
)
示例12: test_Py2_StringIO_module
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_Py2_StringIO_module(self):
"""
This requires that the argument to io.StringIO be made a
unicode string explicitly if we're not using unicode_literals:
Ideally, there would be a fixer for this. For now:
TODO: add the Py3 equivalent for this to the docs. Also add back
a test for the unicode_literals case.
"""
before = """
import cStringIO
import StringIO
s1 = cStringIO.StringIO('my string')
s2 = StringIO.StringIO('my other string')
assert isinstance(s1, cStringIO.InputType)
"""
# There is no io.InputType in Python 3. futurize should change this to
# something like this. But note that the input to io.StringIO
# must be a unicode string on both Py2 and Py3.
after = """
import io
import io
s1 = io.StringIO(u'my string')
s2 = io.StringIO(u'my other string')
assert isinstance(s1, io.StringIO)
"""
self.convert_check(before, after)
示例13: test_type
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_type(self):
"""
The following fails when passed a unicode string on Python
(including when unicode_literals is in effect) and fails when
passed a byte-string on Python 3. So type() always wants a native
string as the first argument.
TODO: maybe provide a replacement that works identically on Py2/3?
"""
mytype = type('blah', (dict,), {"old": 1, "new": 2})
d = mytype()
self.assertTrue(isinstance(d, mytype))
self.assertTrue(isinstance(d, dict))
示例14: test_str_encode_decode_with_py2_str_arg
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def test_str_encode_decode_with_py2_str_arg(self):
# Try passing a standard Py2 string (as if unicode_literals weren't imported)
b = str(TEST_UNICODE_STR).encode(utils.bytes_to_native_str(b'utf-8'))
self.assertTrue(isinstance(b, bytes))
self.assertFalse(isinstance(b, str))
s = b.decode(utils.bytes_to_native_str(b'utf-8'))
self.assertTrue(isinstance(s, str))
self.assertEqual(s, TEST_UNICODE_STR)
示例15: write_static_data
# 需要导入模块: import __future__ [as 别名]
# 或者: from __future__ import unicode_literals [as 别名]
def write_static_data(path):
with open(path, 'w') as f:
f.write("# coding: utf-8")
f.write("from __future__ import unicode_literals\n\n")
printer = pprint.PrettyPrinter(indent=3, stream=f)
print("Writing ships...", end="", flush=True)
f.write("ships = ")
printer.pprint(ship_names())
print("done", flush=True)
print("Writing system names...", end="", flush=True)
f.write("\nsystem_names = ")
printer.pprint(system_names())
print("done", flush=True)
print("Writing constellation names...", end="", flush=True)
f.write("\nconstellation_names = ")
printer.pprint(constellation_names())
print("done", flush=True)
print("Writing region names...", end="", flush=True)
f.write("\nregion_names = ")
printer.pprint(region_names())
print("done", flush=True)
print("Writing relations...", end="", flush=True)
sys2con, con2reg = get_relations()
f.write("\nsystems_to_constellations = ")
printer.pprint(sys2con)
f.write("\nconstellations_to_regions = ")
printer.pprint(con2reg)
print("done", flush=True)