Utilities for refactoring imports in python-like syntax.
Project description
aspy.refactor_imports
Utilities for refactoring imports in python-like syntax.
Installation
pip install aspy.refactor_imports
Examples
aspy.refactor_imports.import_obj
Constructing an import object
>>> from aspy.refactor_imports.import_obj import FromImport
>>> from aspy.refactor_imports.import_obj import ImportImport
>>> FromImport.from_str('from foo import bar').to_text()
u'from foo import bar\n'
>>> ImportImport.from_str('import bar as baz').to_text()
u'import bar as baz\n'
Splitting an import object
>>> from aspy.refactor_imports.import_obj import ImportImport
>>> obj = ImportImport.from_str('import foo, bar, baz')
>>> [i.to_text() for i in obj.split_imports()]
[u'import foo\n', u'import bar\n', u'import baz\n']
Sorting import objects
>>> import pprint
>>> from aspy.refactor_imports.import_obj import FromImport
>>> objs = sorted([
FromImport.from_str('from a import foo'),
FromImport.from_str('from a.b import baz'),
FromImport.from_str('from a import bar'),
FromImport.from_str('from a import bar as buz'),
FromImport.from_str('from a import bar as baz'),
])
>>> pprint.pprint([i.to_text() for i in objs])
[u'from a import bar\n',
u'from a import bar as baz\n',
u'from a import bar as buz\n',
u'from a import foo\n',
u'from a.b import baz\n']
# Or to partition into blocks (even with mixed imports)
>>> import buck.pprint as pprint
>>> from aspy.refactor_imports.import_obj import FromImport
>>> from aspy.refactor_imports.import_obj import ImportImport
>>> from aspy.refactor_imports.sort import sort
>>> partitioned = sort(
[
FromImport.from_str('from aspy import refactor_imports'),
ImportImport.from_str('import sys'),
FromImport.from_str('from pyramid.view import view_config'),
ImportImport.from_str('import cached_property'),
],
separate=True,
import_before_from=True,
))
>>> pprint.pprint(partitioned)
(
(ImportImport.from_str(u'import sys\n'),),
(
ImportImport.from_str(u'import cached_property\n'),
FromImport.from_str(u'from pyramid.view import view_config\n'),
),
(FromImport.from_str(u'from aspy import refactor_imports\n'),),
)
aspy.refactor_imports.classify
Classify a module
>>> from aspy.refactor_imports.classify import classify_import
>>> classify_import('__future__')
u'FUTURE'
>>> classify_import('aspy')
u'APPLICATION'
>>> classify_import('pyramid')
u'THIRD_PARTY'
>>> classify_import('os')
u'BUILTIN'
>>> classify_import('os.path')
u'BUILTIN'
Also as convenient constants
## From aspy.refactor_imports.classify
class ImportType(object):
__slots__ = ()
FUTURE = 'FUTURE'
BUILTIN = 'BUILTIN'
THIRD_PARTY = 'THIRD_PARTY'
APPLICATION = 'APPLICATION'
__all__ = [FUTURE, BUILTIN, THIRD_PARTY, APPLICATION]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for aspy.refactor_imports-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88418d9bd1a0d50b9d3836504469dce6f26ee8b2d78bd58b805d73dd8b381d55 |
|
MD5 | 98bea44fb46d7c3bba1ea52784c98307 |
|
BLAKE2b-256 | 21cbc1b7faffb5b802b6408f0957b39401a5bc95c989a0bf7c1965475d9cfb5e |
Close
Hashes for aspy.refactor_imports-1.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d465c18cadad7e5a87810ecf8e516cb6f78e91871f4f55d0f228c6c376bd16a |
|
MD5 | c924d5be894fb42cac0226a9541745da |
|
BLAKE2b-256 | ac2b55535e3df8275bad51b0ca9c06ba68665952541f3e80184b82b8fc55b745 |