Python __future__.print_function() Examples

The following are 30 code examples of __future__.print_function(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module __future__ , or try the search function .
Example #1
Source File: basic_graph_ops.py    From coremltools with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def fill_outputs(gd):
    """
    Fills the output lists of of a graph of ParsedNode

    Takes a graph in "dict{str, ParsedNode}" form, and returns a new graph.
    """
    # fill outputs
    for k, v in gd.items():
        for i in v.inputs:
            gd[i].outputs.append(v.name)
        for i in v.control_inputs:
            gd[i].control_outputs.append(v.name)
    get_tuple_ops = ["Split", "SplitV", "LSTMBlock"]
    for k, v in gd.items():
        if v.op in get_tuple_ops:
            outputs = [[out, int(gd[out].attr["index"])] for out in v.outputs]
            outputs.sort(key=lambda x: x[1])
            gd[k].outputs = [out for [out, _] in outputs]

    return gd 
Example #2
Source File: translate_to_legacy.py    From pscript with BSD 2-Clause "Simplified" License 7 votes vote down vote up
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 
Example #3
Source File: test_tutorials.py    From sqlalchemy with MIT License 7 votes vote down vote up
def _run_doctest_for_content(self, name, content):
        optionflags = (
            doctest.ELLIPSIS
            | doctest.NORMALIZE_WHITESPACE
            | doctest.IGNORE_EXCEPTION_DETAIL
            | _get_allow_unicode_flag()
        )
        runner = doctest.DocTestRunner(
            verbose=None,
            optionflags=optionflags,
            checker=_get_unicode_checker(),
        )
        globs = {"print_function": print_function}
        parser = doctest.DocTestParser()
        test = parser.get_doctest(content, globs, name, name, 0)
        runner.run(test)
        runner.summarize()
        assert not runner.failures 
Example #4
Source File: const_elimination.py    From coremltools with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def const_elimination(prog):
    """
    prog: Program

    # Replace non-const ops that have const Var
    # outputs replaced with const op. Example:
    #
    # Given:
    #   %2, %3 = non_const_op(...)  # %2 is const, %3 isn't const
    #   %4 = other_op(%2, %3)
    #
    # Result:
    #   _, %3 = non_const_op(...)  # _ is the ignored output
    #   %2_const = const(mode=m)  # %2_const name is for illustration only
    #   %4 = other_op(%2_const, %3)
    #
    # where m is 'file_value' / 'immediate_value' depending on heuristics
    # in get_const_mode.
    """
    for f_name, f in prog.functions.items():
        const_elimination_block(f) 
Example #5
Source File: auth.py    From colabtools with Apache License 2.0 7 votes vote down vote up
def _check_adc():
  """Return whether the application default credential exists and is valid."""
  # google-auth wants to warn the user if no project is set, which makes sense
  # for cloud-only users, but not in our case. We temporarily chnage the logging
  # level here to silence.
  logger = _logging.getLogger()
  log_level = logger.level
  logger.setLevel(_logging.ERROR)
  try:
    creds, _ = _google_auth.default()
  except _google_auth.exceptions.DefaultCredentialsError:
    return False
  finally:
    logger.setLevel(log_level)
  transport = _auth_requests.Request()
  try:
    creds.refresh(transport)
  except Exception as e:  # pylint:disable=broad-except
    _logging.info('Failure refreshing credentials: %s', e)
  return creds.valid 
Example #6
Source File: parser.py    From pythonparser with MIT License 6 votes vote down vote up
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 
Example #7
Source File: test_parser.py    From pythonparser with MIT License 6 votes vote down vote up
def test_future_print(self):
        self.assertParsesSuite(
            [{"ty": "ImportFrom",
              "names": [{"ty": "alias", "name": "print_function", "asname": None}],
              "module": "__future__", "level": 0},
             {"ty": "Expr", "value":
                {"ty": "Call", "func": {"ty": "Name", "id": "print", "ctx": None},
                 "starargs": None, "kwargs": None, "args": [self.ast_x], "keywords": []}}],
            "from __future__ import print_function·print(x)") 
Example #8
Source File: fix_print_with_import.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 
Example #9
Source File: base.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #10
Source File: base.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
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) 
Example #11
Source File: fix_add__future__imports_except_unicode_literals.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def transform(self, node, results):
        # Reverse order:
        future_import(u"print_function", node)
        future_import(u"division", node)
        future_import(u"absolute_import", node) 
Example #12
Source File: fix_print_with_import.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 
Example #13
Source File: base.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #14
Source File: doctest_nose_plugin.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def _patchTestCase(self, case):
        if case:
            case._dt_test.globs['print_function'] = print_function
            case._dt_checker = _checker
        return case 
Example #15
Source File: base.py    From verge3d-blender-addon with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #16
Source File: fix_add_all__future__imports.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #17
Source File: fix_add_all__future__imports.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
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) 
Example #18
Source File: fix_print_with_import.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
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 
Example #19
Source File: fix_add__future__imports_except_unicode_literals.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def transform(self, node, results):
        # Reverse order:
        future_import(u"print_function", node)
        future_import(u"division", node)
        future_import(u"absolute_import", node) 
Example #20
Source File: fix_add_all__future__imports.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
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) 
Example #21
Source File: base.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
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) 
Example #22
Source File: fix_print_with_import.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
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 
Example #23
Source File: fix_add__future__imports_except_unicode_literals.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
def transform(self, node, results):
        # Reverse order:
        future_import(u"print_function", node)
        future_import(u"division", node)
        future_import(u"absolute_import", node) 
Example #24
Source File: vfp2py_convert_visitor.py    From vfp2py with MIT License 6 votes vote down vote up
def visitPrg(self, ctx):
        if ctx.classDef():
            self.class_list = [self.visit(classDef.classDefStart())[0] for classDef in ctx.classDef()]
        if ctx.funcDef():
            self.function_list = [self.visit(funcdef.funcDefStart())[0] for funcdef in ctx.funcDef()]

        self.imports = ['from __future__ import division, print_function']
        self.imports.append('from vfp2py import vfpfunc')
        self.imports.append('from vfp2py.vfpfunc import DB, Array, C, F, M, S')
        self.imports.append('from vfp2py.vfpfunc import parameters, lparameters, vfpclass')
        defs = []

        for i, child in enumerate(ctx.children):
            if isinstance(child, ctx.parser.FuncDefContext):
                funcname, decorator, funcbody = self.visit(child)
                if i == 0 and funcname == '_program_main':
                    funcname = CodeStr('MAIN')
                defs += [
                    add_args_to_code('@{}', (decorator,)),
                    add_args_to_code('def {}():', (funcname,)),
                    funcbody
                ]
                if child.lineComment():
                    defs += sum((self.visit(comment) for comment in child.lineComment()), [])
            elif not isinstance(child, antlr4.tree.Tree.TerminalNodeImpl):
                defs += self.visit(child)

        imports = isort.SortImports(file_contents='\n'.join(set(self.imports)), line_length=100000).output.splitlines()
        return  [CodeStr(imp) for imp in imports] + defs 
Example #25
Source File: check_futures_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def check_file(path, old_division):
  futures = set()
  count = 0
  for line in open(path, encoding='utf-8') if six.PY3 else open(path):
    count += 1
    m = FUTURES_PATTERN.match(line)
    if m:
      futures.add(m.group(1))
    else:
      m = FUTURES_PATTERN_2.match(line)
      if m:
        for entry in m.groups():
          futures.add(entry)
  if not count:
    return  # Skip empty files
  if old_division:
    # This file checks correct behavior without importing division
    # from __future__, so make sure it's doing that.
    expected = set(['absolute_import', 'print_function'])
    if futures != expected:
      raise AssertionError(('Incorrect futures for old_division file:\n'
                            '  expected = %s\n  got = %s') %
                           (' '.join(expected), ' '.join(futures)))
  else:
    missing = REQUIRED_FUTURES - futures
    if missing:
      raise AssertionError('Missing futures: %s' % ' '.join(missing)) 
Example #26
Source File: naming_utils.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def escape_name(name):
    ret = "".join([i if i in _varname_charset else "_" for i in name])
    if ret.endswith("_"):
        return ret
    else:
        return ret + "_" 
Example #27
Source File: naming_utils.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def escape_fn_name(name):
    ret = "".join([i if i in _varname_charset else "_" for i in name])
    ret = escape_name(name)
    if ret.startswith("f_"):
        return ret
    else:
        return "f_" + ret 
Example #28
Source File: _transformers.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_unique_edge_name(self, graph, name):  # type: (Graph, Text) -> Text
        self.num_added += 1
        return graph.get_unique_edge_name(name + "_" + str(self.num_added)) 
Example #29
Source File: _transformers.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_unique_edge_name(self, graph, name):  # type: (Graph, Text) -> Text
        self.num_added += 1
        return graph.get_unique_edge_name(name + "_" + str(self.num_added)) 
Example #30
Source File: _operators.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _add_transpose_before_after(
    layer_func,  # function for layer conversion
    input_names,  # List[str]
    output_names,  # List[str]
    transpose_dims,  # List[int]
    **kwargs
):  # type: ignore

    for i, input_ in enumerate(input_names):
        kwargs["builder"].add_permute(
            name=kwargs["node"].name + "_input_transpose" + str(i),
            dim=transpose_dims,
            input_name=input_,
            output_name=kwargs["node"].name + "_" + input_ + "_transpose",
        )

    new_input_names = [
        kwargs["node"].name + "_" + input_ + "_transpose" for input_ in input_names
    ]
    new_output_names = [output_ + "_transpose" for output_ in output_names]
    layer_func(new_input_names, new_output_names, **kwargs)

    for i, output_ in enumerate(output_names):
        kwargs["builder"].add_permute(
            name=kwargs["node"].name + "_output_transpose" + str(i),
            dim=transpose_dims,
            input_name=output_ + "_transpose",
            output_name=output_,
        )