Python __future__.absolute_import() Examples

The following are 30 code examples of __future__.absolute_import(). 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: 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 #2
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 #3
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 #4
Source File: __init__.py    From rqalpha-mod-ctp with Apache License 2.0 6 votes vote down vote up
def _init(init=_init, MdSpi=MdApi, TraderSpi=TraderApi):
    import sys
    from types import ModuleType, FunctionType as F

    f = (lambda f:F(f.__code__,env)) if sys.version_info[0] >= 3 else (lambda f:F(f.func_code,env))
    mod = sys.modules[__name__]; Module = type(mod)
    if Module is ModuleType:
        class Module(ModuleType): pass
        mod = Module(__name__); env = mod.__dict__
        env.update((k,v) for k,v in globals().items() if k.startswith('__') and k.endswith('__'))
        MdSpi = dict((k,f(v)) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,f(v)) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))
        sys.modules[__name__] = mod
    else:
        env = mod.__dict__
        for k in ('MdApi','TraderApi','_init'): del env[k]
        MdSpi = dict((k,v) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,v) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))

    f(init)(Module, MdSpi, TraderSpi) 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: python3.py    From python-netsurv with MIT License 6 votes vote down vote up
def visit_importfrom(self, node):
        if node.modname == "__future__":
            for name, _ in node.names:
                if name == "division":
                    self._future_division = True
                elif name == "absolute_import":
                    self._future_absolute_import = True
        else:
            if not self._future_absolute_import:
                if self.linter.is_message_enabled("no-absolute-import"):
                    self.add_message("no-absolute-import", node=node)
                    self._future_absolute_import = True
            if not _is_conditional_import(node) and not node.level:
                self._warn_if_deprecated(node, node.modname, {x[0] for x in node.names})

        if node.names[0][0] == "*":
            if self.linter.is_message_enabled("import-star-module-level"):
                if not isinstance(node.scope(), astroid.Module):
                    self.add_message("import-star-module-level", node=node) 
Example #10
Source File: python3.py    From python-netsurv with MIT License 6 votes vote down vote up
def visit_importfrom(self, node):
        if node.modname == "__future__":
            for name, _ in node.names:
                if name == "division":
                    self._future_division = True
                elif name == "absolute_import":
                    self._future_absolute_import = True
        else:
            if not self._future_absolute_import:
                if self.linter.is_message_enabled("no-absolute-import"):
                    self.add_message("no-absolute-import", node=node)
                    self._future_absolute_import = True
            if not _is_conditional_import(node) and not node.level:
                self._warn_if_deprecated(node, node.modname, {x[0] for x in node.names})

        if node.names[0][0] == "*":
            if self.linter.is_message_enabled("import-star-module-level"):
                if not isinstance(node.scope(), astroid.Module):
                    self.add_message("import-star-module-level", node=node) 
Example #11
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 #12
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 #13
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 #14
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 #15
Source File: __init__.py    From rqalpha-mod-ctp with Apache License 2.0 6 votes vote down vote up
def _init(init=_init, MdSpi=MdApi, TraderSpi=TraderApi):
    import sys
    from types import ModuleType, FunctionType as F

    f = (lambda f:F(f.__code__,env)) if sys.version_info[0] >= 3 else (lambda f:F(f.func_code,env))
    mod = sys.modules[__name__]; Module = type(mod)
    if Module is ModuleType:
        class Module(ModuleType): pass
        mod = Module(__name__); env = mod.__dict__
        env.update((k,v) for k,v in globals().items() if k.startswith('__') and k.endswith('__'))
        MdSpi = dict((k,f(v)) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,f(v)) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))
        sys.modules[__name__] = mod
    else:
        env = mod.__dict__
        for k in ('MdApi','TraderApi','_init'): del env[k]
        MdSpi = dict((k,v) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,v) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))

    f(init)(Module, MdSpi, TraderSpi) 
Example #16
Source File: __init__.py    From rqalpha-mod-ctp with Apache License 2.0 6 votes vote down vote up
def _init(init=_init, MdSpi=MdApi, TraderSpi=TraderApi):
    import sys
    from types import ModuleType, FunctionType as F

    f = (lambda f:F(f.__code__,env)) if sys.version_info[0] >= 3 else (lambda f:F(f.func_code,env))
    mod = sys.modules[__name__]; Module = type(mod)
    if Module is ModuleType:
        class Module(ModuleType): pass
        mod = Module(__name__); env = mod.__dict__
        env.update((k,v) for k,v in globals().items() if k.startswith('__') and k.endswith('__'))
        MdSpi = dict((k,f(v)) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,f(v)) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))
        sys.modules[__name__] = mod
    else:
        env = mod.__dict__
        for k in ('MdApi','TraderApi','_init'): del env[k]
        MdSpi = dict((k,v) for k,v in MdSpi.__dict__.items() if k.startswith('On'))
        TraderSpi = dict((k,v) for k,v in TraderSpi.__dict__.items() if k.startswith('On'))

    f(init)(Module, MdSpi, TraderSpi) 
Example #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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_,
        ) 
Example #26
Source File: _graph.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def from_onnx(node):  # type: (NodeProto) -> Node
        attrs = Attributes.from_onnx(node.attribute)
        name = Text(node.name)
        if len(name) == 0:
            name = "_".join(node.output)
        return Node(name, node.op_type, attrs, list(node.input), list(node.output)) 
Example #27
Source File: _graph.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_unique_edge_name(self, name):  # type: (Text) -> Text
        n_ = name
        i = 0
        while self.has_edge_name(n_):
            n_ = "{}_{}".format(name, i)
            i += 1
        return n_ 
Example #28
Source File: fix_absolute_import.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def transform(self, node, results):
        """
        Copied from FixImport.transform(), but with this line added in
        any modules that had implicit relative imports changed:

            from __future__ import absolute_import"
        """
        if self.skip:
            return
        imp = results['imp']

        if node.type == syms.import_from:
            # Some imps are top-level (eg: 'import ham')
            # some are first level (eg: 'import ham.eggs')
            # some are third level (eg: 'import ham.eggs as spam')
            # Hence, the loop
            while not hasattr(imp, 'value'):
                imp = imp.children[0]
            if self.probably_a_local_import(imp.value):
                imp.value = u"." + imp.value
                imp.changed()
                future_import(u"absolute_import", node)
        else:
            have_local = False
            have_absolute = False
            for mod_name in traverse_imports(imp):
                if self.probably_a_local_import(mod_name):
                    have_local = True
                else:
                    have_absolute = True
            if have_absolute:
                if have_local:
                    # We won't handle both sibling and absolute imports in the
                    # same statement at the moment.
                    self.warning(node, "absolute and local imports together")
                return

            new = FromImport(u".", [imp])
            new.prefix = node.prefix
            future_import(u"absolute_import", node)
            return new 
Example #29
Source File: __init__.py    From rqalpha-mod-ctp with Apache License 2.0 5 votes vote down vote up
def _init(Module, MdSpi, TraderSpi):
    globals()['ApiStruct'] = __import__(__name__+'.ApiStruct', None, None, 'x')

    class LazyProperty(object):
        def __get__(self, obj, cls):
            if obj is None: return self
            value = self.fget()
            name = self.fget.__name__
            setattr(obj, name, value)
            delattr(cls, name)
            return value

    def lazy_property(func):
        self = LazyProperty()
        setattr(Module, func.__name__, self)
        self.fget = func
        return self

    @lazy_property
    def MdApi():
        from ._MdApi import _init, MdApi; _init(ApiStruct)
        return type('MdApi', (MdApi,), MdSpi)

    @lazy_property
    def TraderApi():
        from ._TraderApi import _init, TraderApi; _init(ApiStruct)
        return type('TraderApi', (TraderApi,), TraderSpi) 
Example #30
Source File: python3.py    From linter-pylama with MIT License 5 votes vote down vote up
def visit_importfrom(self, node):
        if node.modname == '__future__':
            for name, _ in node.names:
                if name == 'division':
                    self._future_division = True
                elif name == 'absolute_import':
                    self._future_absolute_import = True
        else:
            if not self._future_absolute_import:
                if self.linter.is_message_enabled('no-absolute-import'):
                    self.add_message('no-absolute-import', node=node)
                    self._future_absolute_import = True
            if not _is_conditional_import(node) and not node.level:
                self._warn_if_deprecated(node, node.modname, {x[0] for x in node.names})

        if node.names[0][0] == '*':
            if self.linter.is_message_enabled('import-star-module-level'):
                if not isinstance(node.scope(), astroid.Module):
                    self.add_message('import-star-module-level', node=node)