Giter Club home page Giter Club logo

importmagic's Introduction

Import Magic image

The goal of this package is to be able to automatically manage imports in Python. To that end it can:

  • Build an index of all known symbols in all packages.
  • Find unresolved references in source, and resolve them against the index, effectively automating imports.
  • Automatically arrange imports according to PEP8.

It was originally written for the Sublime Text 2 Python Import Magic plugin.

Example of use in Sublime Text 2 plugin

Example of Import Magic at work

Using the library

Build an index:

index = importmagic.SymbolIndex()
index.build_index(sys.path)
with open('index.json') as fd:
    index.serialize(fd)

Load an existing index:

with open('index.json') as fd:
    index = SymbolIndex.deserialize(fd)

Find unresolved and unreferenced symbols:

scope = importmagic.Scope.from_source(python_source)
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()

Print new import block:

start_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)

Update source code with new import blocks:

python_source = importmagic.update_imports(python_source, index, unresolved, unreferenced)

For more fine-grained control over what symbols are imported, the index can be queried directly:

imports = importmagic.Imports(index, python_source)
imports.remove(unreferenced)

for symbol in unresolved:
    for score, module, variable in index.symbol_scores(symbol):
        if variable is None:
            imports.add_import(module)
        else:
            imports.add_import_from(module, variable)
        break

python_source = imports.update_source()

importmagic's People

Contributors

alecthomas avatar birkenfeld avatar cpitclaudel avatar damnwidget avatar jorgenschaefer avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.