Giter Club home page Giter Club logo

import-analyzer's Introduction

Python import analyzer

Analyse internal package imports in a given path with .py files

Terminology

  • Dependents of x = Modules that import in x
  • Dependencies of x = Modules that x imports

Example

Columns

  • 'Dependents' (of module m in row x) = number of modules that import in m
  • 'Dependencies' (of module m in row x) = number of modules that m imports
  • 'Score' = 'Dependents' column minus 'Dependencies' column
  • 'Proportion' = 'Dependents' column / ('Dependents' column + 'Dependencies' column) * 100

What does the numbers mean?

  • A larger score means it is a dependency of other modules more
    • "A dependency of a lot of modules"
  • A smaller score means it depends on other modules more
    • "Depends on a lot of modules"
  • Note that a negative score doesn't mean it's "bad". It is good to split up your code into modules where they logically fit.
  • A high proportion means most connections to other modules are its dependents
  • A low proportion means most connections to other modules are its dependencies

Example output for koneko, using panda's to_markdown() method

Dependents Dependencies Score Proportion
pure.py 10 0 10 100
colors.py 2 0 2 100
__init__.py 14 0 14 100
files.py 5 1 4 83
printer.py 7 2 5 78
utils.py 9 3 6 75
config.py 5 2 3 71
lscat.py 6 4 2 60
api.py 3 3 0 50
cli.py 2 3 -1 40
prompt.py 3 5 -2 38
picker.py 3 5 -2 38
data.py 1 2 -1 33
screens.py 2 4 -2 33
download.py 2 4 -2 33
lscat_prompt.py 1 3 -2 25
assistants.py 2 7 -5 22
main.py 2 7 -5 22
ui.py 2 12 -10 14
lscat_app.py 1 7 -6 12
__main__.py 0 5 -5 0

Why?

Look at this graph generated by pyreverse:

pyreverse

How can we decipher this mess? Well, every box/module has connections to and/or from other modules. Modules with lots of arrows pointing to its box is highly dependent on, while those with lines but no arrows mean it imports in things from smaller modules (functional core) and construct something bigger and complex, probably the entry point to the program (hint hint, the imperative shell).

Installation & usage

  1. git clone https://github.com/twenty5151/import-analyzer
  2. cd import-analyzer
  3. pip install -r requirements.txt
  4. python imports.py -p {PATH_TO_FILES} -n {NAME_OF_PACKAGE}
    • PATH_TO_FILES: the directory where your .py files are
    • NAME_OF_PACKAGE: usually the name of the above directory. Only the internal imports of your modules are analyzed, see from {NAME_OF_PACKAGE} import ...
    • Optional argument:-i {INIT_FILE}
      • INIT_FILE: the name of your init file, where constants are stored. Default is __init__.py

Todo

  • Support from . import ...
  • Recursive discovery of files (currently only works on a flat directory structure)

Details

Roughly equal to the following manual steps:

  1. pyreverse koneko/*.py -o png
  2. Count the type of each arrow in each module box:
    • Dependents = imported by another module (arrow pointing towards box): +1
    • Dependencies = imports another module (arrow pointing away from box): -1
  3. Score = sum
  4. Proportion = dependents / abs(dependencies + dependents)

import-analyzer's People

Contributors

akazukin5151 avatar

Watchers

 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.