我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用__future__.print_function()。
def fix_future(self, token): """ Fix print_function, absolute_import, with_statement. """ status = getattr(self, '_future_status', 0) if status == 2: return # Done if status == 0 and token.type == 'string': self._future_status = 1 # docstring elif token.type != 'comment': self._future_status = 2 # done i = max(0, token.find_backward('\n')) t = Token(token.total_text, '', i, i) t.fix = '\nfrom __future__ import %s\n' % (', '.join(self.FUTURES)) return t
def transform(self, node, results): # Reverse order: future_import(u"print_function", node) future_import(u"division", node) future_import(u"absolute_import", node)
def transform(self, node, results): # Add the __future__ import first. (Otherwise any shebang or encoding # comment line attached as a prefix to the print statement will be # copied twice and appear twice.) future_import(u'print_function', node) n_stmt = super(FixPrintWithImport, self).transform(node, results) return n_stmt
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)
def test_extract_imports(): result = extract_imports(TEST_SOURCE) assert 'java.lang' not in result assert 'nocomment' not in result assert '_pydev_' not in result assert 'try:\n import os\nexcept:\n pass\n' in result assert 'from sys import path' in result assert 'from os import walk as walk2' in result assert 'ignore' not in result assert 'remove me' not in result assert 'from __future__ import print_function' in result assert 'from os.path import splitext as split' in result assert 'import os.path.splitext as sp' in result
def _patchTestCase(self, case): if case: case._dt_test.globs['print_function'] = print_function case._dt_checker = _checker return case
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)
def add_flags(self, flags): if "print_function" in flags: self.lexer.print_function = True # Grammar
def _check_print_function(self, node): """Verify print_function is imported""" if node.modname == '__future__': for name, _ in node.names: if name == 'print_function': self.seen_print_func = True
def eprint(*args, **kwargs): """ prints to stderr, using the 'from __future__ import print_function' """ print(*args, file=sys.stderr, **kwargs)
def test_exception_path2(self): """Test exception path in exception_validate, expecting windows line endings. """ self.mktmp("from __future__ import print_function\n" "import sys\n" "print('A')\n" "print('B')\n" "print('C', file=sys.stderr)\n" "print('D', file=sys.stderr)\n" ) out = "A\r\nB" tt.ipexec_validate(self.fname, expected_out=out, expected_err="C\r\nD")
def test_all_exports_correspond_to_plugins(self): exports = [name for name in dir(tb.summary) if not name.startswith('_')] futures = frozenset(('absolute_import', 'division', 'print_function')) bad_exports = [ name for name in exports if name not in futures and not any( name == plugin or name.startswith('%s_' % plugin) for plugin in STANDARD_PLUGINS) ] if bad_exports: self.fail( 'The following exports do not correspond to known standard ' 'plugins: %r. Please mark these as private by prepending an ' 'underscore to their names, or, if they correspond to a new ' 'plugin that you are certain should be part of the public API ' 'forever, add that plugin to the STANDARD_PLUGINS set in this ' 'module.' % bad_exports)
def test_importfrom_future(self): binding = FutureImportation('print_function', None, None) assert binding.source_statement == 'from __future__ import print_function' assert str(binding) == '__future__.print_function'
def test_futureImportUsed(self): """__future__ is special, but names are injected in the namespace.""" self.flakes(''' from __future__ import division from __future__ import print_function assert print_function is not division ''')