{ "metadata": { "name": "", "signature": "sha256:4134bd19a12d31db2709cb83e2730f975050a0c13e27818ddea25e1c8cab44ac" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#II-[IPython](http://ipython.org/)\n", "\n", "Lecturer:*Jos\u00e9 Pedro Silva*[1](http://www-num.math.uni-wuppertal.de/en/amna/people/jose-pedro-silva.html) - [silva_at_math.uni-wuppertal.de](mailto:silva_at_math.uni-wuppertal.de)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Tab Completion]()\n", "- [Introspection]()\n", "- [We need to go deeper]()\n", "- [%paste]()\n", "- [Exception Handling]()\n", "- [Magic Commands]()\n", "- [Shell]()\n", "- [%pdb]()\n", "- [%time and %timeit]()\n", "- [Profiles]()\n", "- [Miscellaneous]()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Tab Completion" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Objects" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stringA = 'Welcome to Python'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "stringA.capitalize()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "'Welcome to python'" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "dir(stringA)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__getslice__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '_formatter_field_name_split',\n", " '_formatter_parser',\n", " 'capitalize',\n", " 'center',\n", " 'count',\n", " 'decode',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isdigit',\n", " 'islower',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'partition',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Modules" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "math.acos" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "dir(math)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "['__doc__',\n", " '__file__',\n", " '__name__',\n", " '__package__',\n", " 'acos',\n", " 'acosh',\n", " 'asin',\n", " 'asinh',\n", " 'atan',\n", " 'atan2',\n", " 'atanh',\n", " 'ceil',\n", " 'copysign',\n", " 'cos',\n", " 'cosh',\n", " 'degrees',\n", " 'e',\n", " 'erf',\n", " 'erfc',\n", " 'exp',\n", " 'expm1',\n", " 'fabs',\n", " 'factorial',\n", " 'floor',\n", " 'fmod',\n", " 'frexp',\n", " 'fsum',\n", " 'gamma',\n", " 'hypot',\n", " 'isinf',\n", " 'isnan',\n", " 'ldexp',\n", " 'lgamma',\n", " 'log',\n", " 'log10',\n", " 'log1p',\n", " 'modf',\n", " 'pi',\n", " 'pow',\n", " 'radians',\n", " 'sin',\n", " 'sinh',\n", " 'sqrt',\n", " 'tan',\n", " 'tanh',\n", " 'trunc']" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Command line" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!mkdir 'mkdir'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "!rm -r './mkdir/'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "!ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Day1\t\t\t\t\t IV-SciPy.ipynb\ttweet_dumper.py\t\tVII-Tips\r\n", "Day2\t\t\t\t\t IX-Debugging.ipynb\ttweet_dumper.py~\tVII-TipsandTricks.ipynb\r\n", "Docs\t\t\t\t\t pairwise_fort.f\ttweet_dumper.pyc\tVI-Pandas.ipynb\r\n", "Exercises.ipynb\t\t\t\t pairwise_fort.so\tversion_information\tV-Matplotlib.ipynb\r\n", "III-Numpy.ipynb\t\t\t\t README.md\t\tVII-\t\t\tX-Parallelization.ipynb\r\n", "II-IPython.ipynb\t\t\t TOC2.ipynb\t\tVIII-Benchmarks.ipynb\r\n", "I-Introduction to Python Programming.ipynb TOC.ipynb\t\tVIII-Benchmarks.ipynb~\r\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "!cat /proc/cpuinfo" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "processor\t: 0\r\n", "vendor_id\t: GenuineIntel\r\n", "cpu family\t: 6\r\n", "model\t\t: 69\r\n", "model name\t: Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz\r\n", "stepping\t: 1\r\n", "microcode\t: 0x14\r\n", "cpu MHz\t\t: 1900.000\r\n", "cache size\t: 3072 KB\r\n", "physical id\t: 0\r\n", "siblings\t: 4\r\n", "core id\t\t: 0\r\n", "cpu cores\t: 2\r\n", "apicid\t\t: 0\r\n", "initial apicid\t: 0\r\n", "fpu\t\t: yes\r\n", "fpu_exception\t: yes\r\n", "cpuid level\t: 13\r\n", "wp\t\t: yes\r\n", "flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm\r\n", "bogomips\t: 4988.54\r\n", "clflush size\t: 64\r\n", "cache_alignment\t: 64\r\n", "address sizes\t: 39 bits physical, 48 bits virtual\r\n", "power management:\r\n", "\r\n", "processor\t: 1\r\n", "vendor_id\t: GenuineIntel\r\n", "cpu family\t: 6\r\n", "model\t\t: 69\r\n", "model name\t: Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz\r\n", "stepping\t: 1\r\n", "microcode\t: 0x14\r\n", "cpu MHz\t\t: 1900.000\r\n", "cache size\t: 3072 KB\r\n", "physical id\t: 0\r\n", "siblings\t: 4\r\n", "core id\t\t: 1\r\n", "cpu cores\t: 2\r\n", "apicid\t\t: 2\r\n", "initial apicid\t: 2\r\n", "fpu\t\t: yes\r\n", "fpu_exception\t: yes\r\n", "cpuid level\t: 13\r\n", "wp\t\t: yes\r\n", "flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm\r\n", "bogomips\t: 4988.54\r\n", "clflush size\t: 64\r\n", "cache_alignment\t: 64\r\n", "address sizes\t: 39 bits physical, 48 bits virtual\r\n", "power management:\r\n", "\r\n", "processor\t: 2\r\n", "vendor_id\t: GenuineIntel\r\n", "cpu family\t: 6\r\n", "model\t\t: 69\r\n", "model name\t: Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz\r\n", "stepping\t: 1\r\n", "microcode\t: 0x14\r\n", "cpu MHz\t\t: 1500.000\r\n", "cache size\t: 3072 KB\r\n", "physical id\t: 0\r\n", "siblings\t: 4\r\n", "core id\t\t: 0\r\n", "cpu cores\t: 2\r\n", "apicid\t\t: 1\r\n", "initial apicid\t: 1\r\n", "fpu\t\t: yes\r\n", "fpu_exception\t: yes\r\n", "cpuid level\t: 13\r\n", "wp\t\t: yes\r\n", "flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm\r\n", "bogomips\t: 4988.54\r\n", "clflush size\t: 64\r\n", "cache_alignment\t: 64\r\n", "address sizes\t: 39 bits physical, 48 bits virtual\r\n", "power management:\r\n", "\r\n", "processor\t: 3\r\n", "vendor_id\t: GenuineIntel\r\n", "cpu family\t: 6\r\n", "model\t\t: 69\r\n", "model name\t: Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz\r\n", "stepping\t: 1\r\n", "microcode\t: 0x14\r\n", "cpu MHz\t\t: 1900.000\r\n", "cache size\t: 3072 KB\r\n", "physical id\t: 0\r\n", "siblings\t: 4\r\n", "core id\t\t: 1\r\n", "cpu cores\t: 2\r\n", "apicid\t\t: 3\r\n", "initial apicid\t: 3\r\n", "fpu\t\t: yes\r\n", "fpu_exception\t: yes\r\n", "cpuid level\t: 13\r\n", "wp\t\t: yes\r\n", "flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm\r\n", "bogomips\t: 4988.54\r\n", "clflush size\t: 64\r\n", "cache_alignment\t: 64\r\n", "address sizes\t: 39 bits physical, 48 bits virtual\r\n", "power management:\r\n", "\r\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "What is the difference between atan and atan2?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "reload(math)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "?numpy.arctan" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "numpy.arctan(numpy.pi/4) == math.atan(numpy.pi/4)\n", "numpy.sin(numpy.pi/2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "1.0" ] } ], "prompt_number": 57 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Magic" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%quickref" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext version_information\n", "%version_information numpy" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The version_information extension is already loaded. To reload it, use:\n", " %reload_ext version_information\n" ] }, { "html": [ "
SoftwareVersion
Python2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython2.3.1
OSposix [linux2]
numpy1.9.1
Sun Dec 07 14:23:50 2014 CET
" ], "json": [ "{ \"Software versions\" : [{ \"module\" : \"Python\", \"version\" : \"2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\" }, { \"module\" : \"IPython\", \"version\" : \"2.3.1\" }, { \"module\" : \"OS\", \"version\" : \"posix [linux2]\" }, { \"module\" : \"numpy\", \"version\" : \"1.9.1\" } ] }" ], "latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] \\\\ \\hline\n", "IPython & 2.3.1 \\\\ \\hline\n", "OS & posix [linux2] \\\\ \\hline\n", "numpy & 1.9.1 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Sun Dec 07 14:23:50 2014 CET} \\\\ \\hline\n", "\\end{tabular}\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "Software versions\n", "Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\n", "IPython 2.3.1\n", "OS posix [linux2]\n", "numpy 1.9.1\n", "\n", "Sun Dec 07 14:23:50 2014 CET" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"./styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }