skip to navigation
skip to content

within 0.2.1

A collection of context managers.

Latest Version: 0.2.2

within is a collection of context managers designed to make everyday tasks simpler and safer to perform.


within is in PyPI, so it can be installed directly using:

$ pip install within



The working_directory context manager allows temporary changes to the current working directory. Within the with statement, the path will be set to the supplied working directory. As soon as the context is left (even in case or errors) the working directory is restored. These context managers can be nested as necessary:

from import working_directory


with working_directory("my/file/path"):
    assert os.getcwd() == "my/file/path"

The temporary_directory context manager allows for the creation of temporary directories that only exist for the lifetime of the context manager. On close (normal, or with an Exception), the directory will be emptied then deleted. Exceptions will not be captured. The string path to the directory is returned on instantiation:

from import temporary_directory


with temporary_directory() as tempdir:
    assert os.path.isdir(tempdir)

    directory = tempdir

assert not os.path.isdir(directory)

For extra style points, why not combine the two context managers:

from import temporary_directory, working_directory


#Python 3 only. If on 2 use contextlib.nested
with temporary_directory() as tempdir, working_directory(tempdir):
    # do stuff


The manage context manager manages database connections, ensuring that connections get closed:

from within.db import manage


with manage(sqlite3.connect(database)) as cursor:
    cursor.execute('SELECT * from table_name;')

Manage also provides an automatic way to commit changes on close:

from within.db import manage


with manage(sqlite3.connect(database), commit_on_close=True) as cursor:
    cursor.execute('INSERT into table_name (column) value ?;', (value,))


Active development occurs on Github. Pull and feature requests are welcome


Testing is done under nose for python 2.7 and 3.4 and done by running the and scripts. Accepted code should have 100% coverage, and, unless absolutely necessary, should pass pep8 standards and score 10.0 on pylint (where possible, even tests should attain this).


within is released under an MIT license.

File Type Py Version Uploaded on Size
within-0.2.1.macosx-10.9-x86_64.exe (md5) MS Windows installer any 2014-06-29 65KB
within-0.2.1.tar.gz (md5) Source 2014-06-29 3KB