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()
'from foo import bar\n'
>>> ImportImport.from_str('import bar as baz').to_text()
'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()]
['import foo\n', 'import bar\n', '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])
['from a import bar\n',
'from a import bar as baz\n',
'from a import bar as buz\n',
'from a import foo\n',
'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('import sys\n'),),
(
ImportImport.from_str('import cached_property\n'),
FromImport.from_str('from pyramid.view import view_config\n'),
),
(FromImport.from_str('from aspy import refactor_imports\n'),),
)
aspy.refactor_imports.classify
Classify a module
>>> from aspy.refactor_imports.classify import classify_import
>>> classify_import('__future__')
'FUTURE'
>>> classify_import('aspy')
'APPLICATION'
>>> classify_import('pyramid')
'THIRD_PARTY'
>>> classify_import('os')
'BUILTIN'
>>> classify_import('os.path')
'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-2.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eec8d1a73bedf64ffb8b589ad919a030c1fb14acf7d1ce0ab192f6eedae895c5 |
|
MD5 | a98fb0d778516de08fccbfe346534413 |
|
BLAKE2b-256 | 346e37cbfba703b06fca29c38079bef76cc01e8496197701fff8f0dded3b5b38 |
Close
Hashes for aspy.refactor_imports-2.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9df76bf19ef81620068b785a386740ab3c8939fcbdcebf20c4a4e0057230d782 |
|
MD5 | f660aabc60a0a75d03179a1698ca2655 |
|
BLAKE2b-256 | 6e35dd64697a0ff36a192c6b333b66bd425488384204576ae846f80093a08a8e |