• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Python library for finding unresolved symbols in Python code, and the corresponding imports

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

Getting index from cache:

index = importmagic.SymbolIndex()
index.get_or_create_index(name='foo', paths=sys.path)

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()

Configuration

Configuring import styles

  1. Using importmagic.Imports.
imports = importmagic.Imports.set_style(multiline='backslash', max_columns=80, indent_with_tabs=True)

multiline takes backlslash or parentheses.

  1. From setup.cfg

Add configuration to setup.cfg

[importmagic]
multiline = 'parentheses'
max_columns = 120
indent_with_tabs = 1

and pass root directory to importmagic

imports = importmagic.Imports(root_dir='/foo/bar/')

More Repositories

1

chroma

A general purpose syntax highlighter in pure Go
Go
4,360
star
2

gometalinter

DEPRECATED: Use https://github.com/golangci/golangci-lint
Go
3,507
star
3

kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser
Go
3,497
star
4

participle

A parser library for Go
Go
3,479
star
5

entityx

EntityX - A fast, type-safe C++ Entity-Component system
C++
2,170
star
6

kong

Kong is a command-line parser for Go
Go
2,132
star
7

voluptuous

CONTRIBUTIONS ONLY: Voluptuous, despite the name, is a Python data validation library.
Python
1,793
star
8

go_serialization_benchmarks

Benchmarks of Go serialization methods
Go
1,560
star
9

jsonschema

Maintenance has moved to https://github.com/invopop/jsonschema
Go
750
star
10

pawk

PAWK - A Python line processor (like AWK)
Python
516
star
11

gozmq

Go (golang) bindings for the 0mq (zmq, zeromq) C API
Go
468
star
12

log4go

Logging package similar to log4j for the Go programming language
Go
309
star
13

ondir

OnDir is a small program to automate tasks specific to certain directories
C
195
star
14

mph

Minimal Perfect Hashing for Go
Go
173
star
15

repr

Python's repr() for Go
Go
163
star
16

assert

A simple assertion library using Go generics
Go
147
star
17

units

Helpful unit multipliers and functions for Go
Go
123
star
18

gorx

A package and tool providing Reactive eXtensions for Go.
Go
94
star
19

devtodo2

DevTodo the Second
Go
89
star
20

template

Fork of Go's text/template adding newline elision
Go
56
star
21

hcl

Parsing, encoding and decoding of HCL to and from Go types and an AST.
Go
49
star
22

binary

General purpose binary encoder/decoder
Go
48
star
23

SublimeLinter-contrib-gometalinter

SublimeLinter plugin for gometalinter
Python
47
star
24

localcache

Local file-based atomic cache manager
Go
44
star
25

gobundle

DEPRECATED: I recommend https://github.com/GeertJohan/go.rice
Go
39
star
26

geoip

A pure Go interface to the free MaxMind GeoIP database
Go
38
star
27

unsafeslice

Unsafe zero-copy slice casts for Go
Go
37
star
28

SublimePythonImportMagic

This Sublime Text 2 plugin attempts to automatically manage Python imports.
Python
34
star
29

inject

Guice-ish dependency injection for Go.
Go
31
star
30

sequel

Sequel - A Go <-> SQL mapping package
Go
26
star
31

multiplex

This Go package multiplexes streams over a single underlying transport io.ReadWriteCloser.
Go
25
star
32

arena

A very fast arena allocator for Go
Go
22
star
33

tuplespace

A RESTful tuple space server
Go
21
star
34

langx

Language experimentation.
Go
21
star
35

mango-kong

Mango (man page generator) integration for Kong
Go
20
star
36

go-check-sumtype

A simple utility for running exhaustiveness checks on Go "sum types."
Go
20
star
37

atomic

Type-safe atomic values for Go
Go
19
star
38

go-rpcgen

Generates Go RPC server and client boilerplate for interfaces.
Go
17
star
39

SublimeFoldPythonDocstrings

Automatically folds Python docstrings longer than 1 line.
Python
16
star
40

colour

Quake-style colour formatting for Unix terminals
Go
15
star
41

protobuf

A Protobuf IDL parser for Go
Go
15
star
42

oink

Oink is a Python to Javascript translator.
Python
15
star
43

entityx_python

Python bindings for EntityX
C++
14
star
44

kong-yaml

Go
14
star
45

bit

Bit - A simple yet powerful build tool
Go
12
star
46

shreq

This utility verifies all commands used by a shell script against an allow list
Go
11
star
47

types

Useful generic types for Go
Go
11
star
48

app

Modular application framework for Go.
Go
11
star
49

kdl

Go parser for KDL
Go
10
star
50

vheap

Fast, persistent, mmapped, virtual heap.
Go
8
star
51

errors

A simple errors package for Go
Go
8
star
52

kong-hcl

Go
8
star
53

rapid

RESTful API Daemons (and Clients) for Go
Go
7
star
54

genh

genh is an opinionated tool for generating request-handler boilerplate for Go
Go
7
star
55

ReactiveDataStructures

Reactive data structures for Swift based on RxSwift
Swift
7
star
56

lunatic-go

Lunatic bindings for (Tiny)Go
Go
6
star
57

chrysalis

Chrysalis - Source to a 2D Platformer from 1994
C++
6
star
58

dotfiles

My dotfiles.
Vim Script
6
star
59

bootstrap

Go application bootstrapping
Go
6
star
60

devtodo

DevTodo (legacy)
C
6
star
61

waffle

Waffle - A Dependency-Injection-based application framework for Python
Python
5
star
62

porpoise

Porpoise - A Redis-based analytics framework
Python
5
star
63

waitgroup

Like sync.WaitGroup and ergroup.Group had a baby.
Go
5
star
64

esfmt

An opinionated, zero-configuration formatter for ES/TS/ESX/TSX
Go
5
star
65

flam

flam /flæm/ noun, verb, flammed, flam⋅ming. Informal. –noun 1. a deception or trick. 2. a falsehood; lie. –verb (used with object), verb (used without object) 3. to deceive; delude; cheat.
Python
5
star
66

cly

A Python module for adding powerful text-based consoles to your application.
Python
4
star
67

expr

Runtime evaluation of Go-like expressions
Go
4
star
68

simplenotefs

simplenotefs
Python
4
star
69

concurrency

Types and functions for managing concurrency in Go.
Go
4
star
70

replaylog

A type safe implementation of an op replay log
Go
3
star
71

Cache.swift

A flexible RAM and disk-backed cache for Swift
Swift
3
star
72

wit-go

A partial WIT parser and code generator for Go
Go
3
star
73

SublimeLinter-contrib-errcheck

SublimeLinter integration for the Go errcheck utility
Python
3
star
74

SublimeLinter-contrib-golang-cilint

DEPRECATED: Use https://github.com/cixtor/SublimeLinter-golangcilint
Python
2
star
75

aspect

Lightweight Aspect-oriented Module for Python
Python
2
star
76

gptcc

Add Conventional Commits to commit messages using ChatGPT
Shell
2
star
77

WaveGrowl.app

Wave notifications via Growl on Mac
Python
2
star
78

cut

Core Utilities - A set of core utility classes for Python.
Python
2
star
79

kong-toml

Kong configuration loader for TOML
Shell
2
star
80

rest

Go
2
star
81

prototemplate

Process Protocol Buffer definitions with text templates and JavaScript functions
Go
2
star
82

webservice

A webservice dispatcher for Go
Go
1
star
83

pathways

Pathways - An opinionated RESTful web service framework for Go
Go
1
star
84

cktphotography.com

Christine Knight Thomas Photography (website)
JavaScript
1
star
85

psmap

Persistent static maps for Go
Go
1
star