def _get_all_imports():
"""
Return list of all the imports
This prevents sub-modules (geoms, stats, utils, ...)
from being imported into the user namespace by the
following import statement
from plotnine import *
This is because `from Module import Something`
leads to `Module` itself coming into the namespace!!
"""
import types
lst = [name for name, obj in globals().items()
if not (name.startswith('_') or
name == 'absolute_import' or
isinstance(obj, types.ModuleType))]
return lst
python类absolute_import()的实例源码
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)
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)
def _collect_import(self, name, fromlist, level):
if sys.version_info[0] == 2:
if name == '__future__' and 'absolute_import' in (fromlist or ()):
self._level = 0
have_star = False
if fromlist is not None:
fromlist = set(fromlist)
if '*' in fromlist:
fromlist.remove('*')
have_star = True
# Collect this import to belong to this module
self._module._imported_modules.append(
(have_star,
(name, self._module, fromlist, level),
{'attr': DependencyInfo(
conditional=self.in_if,
tryexcept=self.in_tryexcept,
function=self.in_def,
fromlist=False)}))
def test_smarkets_style_from_import_capitals_are_not_lowered():
input = '''from __future__ import absolute_import, division, print_function
from imps.strings import AAAA
from imps.strings import get_doc_string, strip_to_module_name, strip_to_module_name_from_import
from imps.strings import ZZZZ
'''
# Possible alternative:
# output = '''from __future__ import absolute_import, division, print_function
#
# from imps.strings import (
# AAAA,
# get_doc_string,
# strip_to_module_name,
# strip_to_module_name_from_import
# ZZZZ,
# )
# '''
assert Sorter('s', max_line_length=110).sort(input) == input
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)
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)
def _collect_import(self, name, fromlist, level):
if sys.version_info[0] == 2:
if name == '__future__' and 'absolute_import' in (fromlist or ()):
self._level = 0
have_star = False
if fromlist is not None:
fromlist = set(fromlist)
if '*' in fromlist:
fromlist.remove('*')
have_star = True
# Record this import as originating from this module for subsequent
# handling by the _process_imports() method.
self._module._deferred_imports.append(
(have_star,
(name, self._module, fromlist, level),
{'edge_attr': DependencyInfo(
conditional=self.in_if,
tryexcept=self.in_tryexcept,
function=self.in_def,
fromlist=False)}))
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)
def _init():
ENV = globals(); del ENV['_init']
from ._talib import _init; _init(ENV)
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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:hakkuframework
作者: 4shadoww
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
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 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 _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)
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)
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 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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:packaging
作者: blockstack
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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 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_smarkets_style():
input = '''from __future__ import absolute_import, division, print_function
import ast
import configparser
import os
import StringIO
import sys
from functools import *
from os import path
import flake8
import pytest
from flake8.defaults import NOQA_INLINE_REGEXP, STATISTIC_NAMES
from flake8.exceptions import *
from pytest import *
from pytest import capture
from pytest import compat, config
from common.interfaces import Config
from common.rest.decorators import jsonify
from han.db import Database
from winners.server.db_access import (
acknowledge_winner_exposure_for_market,
get_acknowledged_winner_exposures_for_market,
)
from . import A
from . import B
from .A import A
from .B import B
from .. import A
from .. import B
from ..A import A
from ..B import B
'''
assert Sorter('s', 80, ['common', 'winners', 'han']).sort(input) == input
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 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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:islam-buddy
作者: hamir
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
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 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)
fix_add_all__future__imports.py 文件源码
项目:FightstickDisplay
作者: calexil
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
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 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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:FightstickDisplay
作者: calexil
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
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 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 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 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