Stem is a Python controller library for Tor. With it you can use Tor's control protocol to script against the Tor process, or build things such as Nyx.
Documentation and tutorials available at stem.torproject.org.