• Stars
    star
    136
  • Rank 266,199 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 14 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

Dependency-based Python Module Reloader

Python Module Reloader

Build Status PyPI Version

This library implements a dependency-based module reloader for Python. Unlike the builtin reload() function, this reloader will reload the requested module and all other modules that are dependent on that module.

A detailed discussion of the reloader's implementation is available here:

http://www.indelible.org/ink/python-reloading/

Usage

The reloader works by tracking dependencies between imported modules. It must first be enabled in order to track those dependencies. The reloader has no dependency information for modules that were imported before it was enabled or after it is disabled, so you'll probably want to enable the reloader early in your application's startup process.

import reloader
reloader.enable()

# Import additional modules
import module1
import module2

To manually reload an imported module, pass it to the reloader's reload() method:

import example
reloader.reload(example)

Note that you must pass the module object itself and not a string containing the module's name. If you only have the module's name, you can fetch the module object from the global sys.modules dictionary:

reloader.reload(sys.modules['example'])

You can also query a module's dependencies for informational or debugging purposes:

reloader.get_dependencies(example)

You can disable the reloader's dependency tracking at any time:

reloader.disable()

Blacklisting Modules

There may be times when you don't want a module and its dependency hierarchy to be reloaded. The module might rarely change and be expensive to import. To support these cases, you can explicitly "blacklist" modules from the reloading process using the blacklist argument to enable().

reloader.enable(blacklist=['os', 'ConfigParser'])

The blacklist can be any iterable listing the fully-qualified names of modules that should be ignored. Note that blacklisted modules will still appear in the dependency graph for completeness; they will just not be reloaded.

An Interactive Example

This example demonstrates how easily the reloader can be used from the interactive Python interpreter. Imagine you have the module example.py open in a text editor, and it contains the following:

print "I am example.py"

Our interactive session starts like this:

>>> import reloader
>>> reloader.enable()
>>> import example
I am example.py

Now modify example.py in your text editor. You can then reload the example in your interactive session:

>>> reloader.reload(example)
I am the modified example.py

This is a simplistic example that doesn't fully demonstrate the power of the reloader's dependency-based module tracking, but it hopefully illustrates the basic usage and utility of the system.

The __reload__() Callback

If a module has a __reload__() function, it will be called with a copy of the original module's dictionary after it has been reloaded. This provides a convenient mechanism for preserving state between reloads.

Consider a module named counts that contains the following code:

COUNTER = 0

The module's COUNTER variable will be reset to 0 when the module is reloaded:

>>> import counts
>>> counts.COUNTER += 1
>>> counts.COUNTER
1
>>> reloader.reload(counts)
>>> counts.COUNTER += 1
1

We can preserve the value of COUNTER across reloads by adding a __reload__() function to the counts module:

def __reload__(state):
    global COUNTER
    COUNTER = state['COUNTER']

Now when we reload counts:

>>> import counts
>>> counts.COUNTER += 1
>>> counts.COUNTER
1
>>> reloader.reload(counts)
>>> counts.COUNTER += 1
>>> counts.COUNTER
2

More Repositories

1

chrome-utm-stripper

Browser extension that strips Google Analytics (UTM) parameters, and various other click tracking tokens, from URL query strings
JavaScript
702
star
2

vim-graphql

A Vim plugin that provides GraphQL file detection, syntax highlighting, and indentation.
Vim Script
482
star
3

stale

Stale identifies (and optionally deletes) stale Delicious and Pinboard links.
Python
149
star
4

php-python

Embedded Python Extension for PHP
C
68
star
5

gmail-fixed-font

Use your browser's monospace font for message body text in Gmail
JavaScript
60
star
6

AFHTTPClientLogger

Configurable HTTP request logger for AFNetworking
Objective-C
41
star
7

apprankings

App Store Rankings Scraper
Python
40
star
8

flake8-assertive

Flake8 unittest assert method checker
Python
31
star
9

vim-phabricator

Vim plugin for Phabricator and Arcanist
Vim Script
18
star
10

vesta

Vestaboard client library for Python
Python
17
star
11

PropertyKit

PropertyKit provides tools for working with Objective-C Declared Properties
Objective-C
12
star
12

dotfiles

My public dotfiles
Vim Script
6
star
13

flask-facebook

Facebook Support for Flask
Python
4
star
14

haitwu-appengine

Haitwu - Find Haikus in Tweets
Python
4
star
15

indelible-ink

Indelible Ink
Python
3
star
16

flake8-author

Flake8 extension that checks Python modules for __author__ attributes
Python
3
star
17

pwdcomposer

Password Composer for iOS
3
star
18

tgprot

The Telegard Protocol Pack
3
star
19

django-ink

Ink is a blogging application for Django.
Python
3
star
20

dammit-winamp

DAMMIT WinAMP plugin that Soco and I wrote at Computer Science House in the fall of 1998
C++
2
star
21

MIDI

MIDI Input Library for Windows
C++
2
star
22

gh-cat

Concatenate and print the contents of files from a remote GitHub repository
Shell
1
star
23

pygame-starfield

PyGame Starfield Example
Python
1
star
24

brightkite-python

Python library for the Brightkite API
Python
1
star
25

jparise.github.com

Indelible.org
HTML
1
star
26

yuri

Yuri is a URI manipulation library for Python
Python
1
star
27

marconi

Marconi is a network media server.
Python
1
star
28

indelible-django

Indelible.org Django Site
Python
1
star
29

evite-gcal

A Greasemonkey script that replaces the Outlook Calendar link in Evite invitations with a Google Calendar link.
JavaScript
1
star
30

caching-dns

Caching DNS Server in Visual Basic
Visual Basic
1
star
31

rugen.org

Rugen.org
1
star