{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we import our own module called `myprofile` (we have it in the same directory as this notebook)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import myprofile" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a docstring at the top -- the comments there are what appear when we ask for help" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on module myprofile:\n", "\n", "NAME\n", " myprofile\n", "\n", "DESCRIPTION\n", " A very simple profiling class. Define some timers and methods\n", " to start and stop them. Nesting of timers is tracked so we can\n", " pretty print the profiling information.\n", " \n", " # define a timer object, labeled 'my timer'\n", " a = timer('my timer')\n", " \n", " This will add 'my timer' to the list of keys in the 'my timer'\n", " dictionary. Subsequent calls to the timer class constructor\n", " will have no effect.\n", " \n", " # start timing the 'my timer' block of code\n", " a.begin()\n", " \n", " ... do stuff here ...\n", " \n", " # end the timing of the 'my timer' block of code\n", " a.end()\n", " \n", " for best results, the block of code timed should be large\n", " enough to offset the overhead of the timer class method\n", " calls.\n", " \n", " Multiple timers can be instanciated and nested. The stackCount\n", " global parameter keeps count of the level of nesting, and the\n", " timerNesting data structure stores the nesting level for each\n", " defined timer.\n", " \n", " timeReport() is called at the end to print out a summary of the\n", " timing.\n", " \n", " At present, no enforcement is done to ensure proper nesting.\n", "\n", "CLASSES\n", " builtins.object\n", " Timer\n", " \n", " class Timer(builtins.object)\n", " | Timer(name)\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, name)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | begin(self)\n", " | \n", " | end(self)\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n", "FUNCTIONS\n", " time_report()\n", "\n", "DATA\n", " print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...\n", " stack_count = 0\n", " timer_nesting = {}\n", " timer_order = []\n", " timers = {}\n", "\n", "FILE\n", " /Users/marivi/Documents/GitHub/python-class/lectures/01-python/myprofile.py\n", "\n", "\n" ] } ], "source": [ "help(myprofile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module simply provides a way to time routines (python and ipython have built-in methods for this too)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "main loop: 0.000827789306640625\n", "332833500.0\n" ] } ], "source": [ "t = myprofile.Timer(\"main loop\")\n", "t.begin()\n", "\n", "sum = 0.0\n", "for n in range(1000):\n", " sum += n**2\n", "\n", "\n", "t.end()\n", "myprofile.time_report()\n", "\n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the file `myprofile.py`, you will see a block of code under\n", "\n", "```\n", "if __name__ == \"__main__\":\n", "```\n", "\n", "That code is executed if the file is run directly, either from the commandline as:\n", "\n", "`python myprofile.py`\n", "\n", "for through the `%run` magic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%run myprofile" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }