All Projects → asottile → aspy.refactor_imports

asottile / aspy.refactor_imports

Licence: MIT license
Utilities for refactoring imports in python-like syntax.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to aspy.refactor imports

smuggler
🚣 Smuggle all imports
Stars: ✭ 72 (+414.29%)
Mutual labels:  refactoring, imports
lint-deps
Lint for unused or missing dependencies in your node.js projects. Customize with plugins or configuration.
Stars: ✭ 48 (+242.86%)
Mutual labels:  imports
clang-tool
Simple and powerful standalone project for clang-based tools using libtooling (e.g. refactoring, auto-completion, etc.)
Stars: ✭ 35 (+150%)
Mutual labels:  refactoring
refactoring-for-testability-cpp
Hard-to-test patterns in C++ and how to refactor them
Stars: ✭ 40 (+185.71%)
Mutual labels:  refactoring
refactor-css
Helps you identify reoccurring CSS class name combinations in your markup
Stars: ✭ 28 (+100%)
Mutual labels:  refactoring
twist
📡 Static and serverless canonical imports for your Go packages
Stars: ✭ 23 (+64.29%)
Mutual labels:  imports
refactoring-golf
A Refactoring Golf exercise
Stars: ✭ 40 (+185.71%)
Mutual labels:  refactoring
pypackage
Cookiecutter python package using Poetry, mypy, black, isort, autoflake, pytest, mkdocs, and GitHub Actions
Stars: ✭ 12 (-14.29%)
Mutual labels:  isort
liquigraph
Migrations for Neo4j
Stars: ✭ 122 (+771.43%)
Mutual labels:  refactoring
php-finder refactoring-kata
🐘🔍Incomprehensible Finder Refactoring Kata port for PHP
Stars: ✭ 22 (+57.14%)
Mutual labels:  refactoring
kite
small + super-fast HTML templating
Stars: ✭ 18 (+28.57%)
Mutual labels:  imports
comby-rust
Rust refactoring templates for comby, the structural find-and-replace tool.
Stars: ✭ 23 (+64.29%)
Mutual labels:  refactoring
Refactoring-Chapter-1
Worked example from Fowler's Chapter 1
Stars: ✭ 34 (+142.86%)
Mutual labels:  refactoring
seed-isort-config
Statically populate the `known_third_party` `isort` setting.
Stars: ✭ 67 (+378.57%)
Mutual labels:  isort
gopatch
Refactoring and code transformation tool for Go.
Stars: ✭ 466 (+3228.57%)
Mutual labels:  refactoring
refactoring-koans-js
Refactoring Koans to help you learn to refactor code smells in javascript
Stars: ✭ 15 (+7.14%)
Mutual labels:  refactoring
DDDToolbox
A set of Roslyn refactorings supporting DDD design
Stars: ✭ 31 (+121.43%)
Mutual labels:  refactoring
tzientist
Scientist-like library for Node.js in TypeScript
Stars: ✭ 37 (+164.29%)
Mutual labels:  refactoring
arcanist-linters
A collection of custom Arcanist linters
Stars: ✭ 64 (+357.14%)
Mutual labels:  isort
atom-python-isort
Atom.io plugin to sort Python imports
Stars: ✭ 19 (+35.71%)
Mutual labels:  isort

Build Status Azure DevOps coverage pre-commit.ci status

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].