Skip to content

Instantly share code, notes, and snippets.

@damontallen
Last active December 29, 2020 02:39
Show Gist options
  • Save damontallen/5675210 to your computer and use it in GitHub Desktop.
Save damontallen/5675210 to your computer and use it in GitHub Desktop.
Less brittle HTML table converter of the quickref text.
<table border="1" cellpadding="3" cellspacing="0" style="border:1px solid black;border-collapse:collapse;">
<tr><td style="background-color:lightBlue;" colspan="2"; ><b>IPython -- An enhanced Interactive Python - Quick Reference Card</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="border-bottom: 1px solid transparent;"> obj?, obj?? </td><td style="border-bottom: 1px solid transparent;"> Get help, or more help for object (also works as</tr>
<tr><td style="border-top: 1px solid transparent;"> </td><td style="border-top: 1px solid transparent;"> ?obj, ??obj).</tr>
<tr><td>?foo.*abc*</td><td> List names in 'foo' containing 'abc' in them.</td></tr>
<tr><td>%magic</td><td> Information about IPython's 'magic' % functions.</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td colspan="2"; style="border-bottom: 1px solid transparent;">Magic functions are prefixed by % or %%, and typically take their arguments</td></tr>
<tr><td colspan="2"; style="border-top: 1px solid transparent; border-bottom: 1px solid transparent;">without parentheses, quotes or even commas for convenience. Line magics take a</td></tr>
<tr><td colspan="2"; style="border-top: 1px solid transparent;">single % and cell magics are prefixed with two %%.</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="background-color:#D5E0C5;" colspan="2"; ><b>Example magic function calls:</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td>%alias d ls -F</td><td> 'd' is now an alias for 'ls -F'</td></tr>
<tr><td>alias d ls -F</td><td> Works if 'alias' not a python name</td></tr>
<tr><td>alist = %alias</td><td> Get list of aliases to 'alist'</td></tr>
<tr><td>cd /usr/share</td><td> Obvious. cd -&#60tab&#62 to choose from visited dirs.</td></tr>
<tr><td>%cd??</td><td> See help AND source for magic %cd</td></tr>
<tr><td>%timeit x=10</td><td> time the 'x=10' statement with high precision.</td></tr>
<tr><td style="border-bottom: 1px solid transparent;"> %%timeit x=2**100 </td><td style="border-bottom: 1px solid transparent;"> time 'x*100' with a setup of 'x=2**100'; setup code is not</tr>
<tr><td style="border-top: 1px solid transparent;"> x**100 </td><td style="border-top: 1px solid transparent;"> counted. This is an example of a cell magic.</tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="background-color:#D5E0C5;" colspan="2"; ><b>System commands:</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td>!cp a.txt b/</td><td> System command escape, calls os.system()</td></tr>
<tr><td>cp a.txt b/</td><td> after %rehashx, most system commands work without !</td></tr>
<tr><td>cp ${f}.txt $bar</td><td> Variable expansion in magics and system commands</td></tr>
<tr><td>files = !ls /usr</td><td> Capture sytem command output</td></tr>
<tr><td>files.s, files.l, files.n</td><td> "a b c", ['a','b','c'], 'a\nb\nc'</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="background-color:#D5E0C5;" colspan="2"; ><b>History:</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td>_i, _ii, _iii</td><td> Previous, next previous, next next previous input</td></tr>
<tr><td>_i4, _ih[2:5]</td><td> Input history line 4, lines 2-4</td></tr>
<tr><td>exec _i81</td><td> Execute input history line #81 again</td></tr>
<tr><td>%rep 81</td><td> Edit input history line #81</td></tr>
<tr><td>_, __, ___</td><td> previous, next previous, next next previous output</td></tr>
<tr><td>_dh</td><td> Directory history</td></tr>
<tr><td>_oh</td><td> Output history</td></tr>
<tr><td>%hist</td><td> Command history. '%hist -g foo' search history for 'foo'</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="background-color:#D5E0C5;" colspan="2"; ><b>Autocall:</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td>f 1,2</td><td> f(1,2) # Off by default, enable with %autocall magic.</td></tr>
<tr><td>/f 1,2</td><td> f(1,2) (forced autoparen)</td></tr>
<tr><td>,f 1 2</td><td> f("1","2")</td></tr>
<tr><td>;f 1 2</td><td> f("1 2")</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td colspan="2"; style="border-bottom: 1px solid transparent;">Remember: TAB completion works in many contexts, not just file names</td></tr>
<tr><td colspan="2"; style="border-top: 1px solid transparent;">or python names.</td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td style="background-color:#D5E0C5;" colspan="2"; ><b>The following magic functions are currently available:</b></td></tr>
<tr><td colspan="2"; > </td></tr>
<tr><td>%alias</td><td> Define an alias for a system command.</td></tr>
<tr><td>%alias_magic</td><td> %alias_magic [-l] [-c] name target</td></tr>
<tr><td>%autocall</td><td> Make functions callable without having to type parentheses.</td></tr>
<tr><td>%automagic</td><td> Make magic functions callable without having to type the initial %.</td></tr>
<tr><td>%bookmark</td><td> Manage IPython's bookmark system.</td></tr>
<tr><td>%cd</td><td> Change the current working directory.</td></tr>
<tr><td>%clear</td><td> Clear the terminal.</td></tr>
<tr><td>%colors</td><td> Switch color scheme for prompts, info system and exception handlers.</td></tr>
<tr><td>%config</td><td> configure IPython</td></tr>
<tr><td>%connect_info</td><td> Print information for connecting other clients to this kernel</td></tr>
<tr><td>%debug</td><td> Activate the interactive debugger in post-mortem mode.</td></tr>
<tr><td>%dhist</td><td> Print your history of visited directories.</td></tr>
<tr><td>%dirs</td><td> Return the current directory stack.</td></tr>
<tr><td>%doctest_mode</td><td> Toggle doctest mode on and off.</td></tr>
<tr><td>%ed</td><td> Alias for `%edit`.</td></tr>
<tr><td>%edit</td><td> Bring up an editor and execute the resulting code.</td></tr>
<tr><td>%env</td><td> List environment variables.</td></tr>
<tr><td>%gui</td><td> Enable or disable IPython GUI event loop integration.</td></tr>
<tr><td>%hist</td><td> Print input history (_i&#60n&#62 variables), with most recent last.</td></tr>
<tr><td>%history</td><td> Print input history (_i&#60n&#62 variables), with most recent last.</td></tr>
<tr><td>%install_default_config</td><td> %install_default_config has been deprecated.</td></tr>
<tr><td>%install_ext</td><td> Download and install an extension from a URL, e.g.::</td></tr>
<tr><td>%install_profiles</td><td> %install_profiles has been deprecated.</td></tr>
<tr><td>%killbgscripts</td><td> Kill all BG processes started by %%script and its family.</td></tr>
<tr><td>%less</td><td> Show a file through the pager.</td></tr>
<tr><td>%load</td><td> Load code into the current frontend.</td></tr>
<tr><td>%load_ext</td><td> Load an IPython extension by its module name.</td></tr>
<tr><td>%loadpy</td><td> Alias of `%load`</td></tr>
<tr><td>%logoff</td><td> Temporarily stop logging.</td></tr>
<tr><td>%logon</td><td> Restart logging.</td></tr>
<tr><td>%logstart</td><td> Start logging anywhere in a session.</td></tr>
<tr><td>%logstate</td><td> Print the status of the logging system.</td></tr>
<tr><td>%logstop</td><td> Fully stop logging and close log file.</td></tr>
<tr><td>%lsmagic</td><td> List currently available magic functions.</td></tr>
<tr><td>%macro</td><td> Define a macro for future re-execution. It accepts ranges of history,</td></tr>
<tr><td>%magic</td><td> Print information about the magic function system.</td></tr>
<tr><td>%man</td><td> Find the man page for the given command and display in pager.</td></tr>
<tr><td>%more</td><td> Show a file through the pager.</td></tr>
<tr><td>%notebook</td><td> %notebook [-e] [-f FORMAT] filename</td></tr>
<tr><td>%page</td><td> Pretty print the object and display it through a pager.</td></tr>
<tr><td>%pastebin</td><td> Upload code to Github's Gist paste bin, returning the URL.</td></tr>
<tr><td>%pdb</td><td> Control the automatic calling of the pdb interactive debugger.</td></tr>
<tr><td>%pdef</td><td> Print the definition header for any callable object.</td></tr>
<tr><td>%pdoc</td><td> Print the docstring for an object.</td></tr>
<tr><td>%pfile</td><td> Print (or run through pager) the file where an object is defined.</td></tr>
<tr><td>%pinfo</td><td> Provide detailed information about an object.</td></tr>
<tr><td>%pinfo2</td><td> Provide extra detailed information about an object.</td></tr>
<tr><td>%popd</td><td> Change to directory popped off the top of the stack.</td></tr>
<tr><td>%pprint</td><td> Toggle pretty printing on/off.</td></tr>
<tr><td>%precision</td><td> Set floating point precision for pretty printing.</td></tr>
<tr><td>%profile</td><td> Print your currently active IPython profile.</td></tr>
<tr><td>%prun</td><td> Run a statement through the python code profiler.</td></tr>
<tr><td>%psearch</td><td> Search for object in namespaces by wildcard.</td></tr>
<tr><td>%psource</td><td> Print (or run through pager) the source code for an object.</td></tr>
<tr><td>%pushd</td><td> Place the current dir on stack and change directory.</td></tr>
<tr><td>%pwd</td><td> Return the current working directory path.</td></tr>
<tr><td>%pycat</td><td> Show a syntax-highlighted file through a pager.</td></tr>
<tr><td>%pylab</td><td> Load numpy and matplotlib to work interactively.</td></tr>
<tr><td>%qtconsole</td><td> Open a qtconsole connected to this kernel.</td></tr>
<tr><td>%quickref</td><td> Show a quick reference sheet</td></tr>
<tr><td>%quickref_file</td><td> Save a quick reference sheet on disk</td></tr>
<tr><td>%recall</td><td> Repeat a command, or get command to input line for editing.</td></tr>
<tr><td>%rehashx</td><td> Update the alias table with all executable files in $PATH.</td></tr>
<tr><td>%reload_ext</td><td> Reload an IPython extension by its module name.</td></tr>
<tr><td>%rep</td><td> Repeat a command, or get command to input line for editing.</td></tr>
<tr><td>%rerun</td><td> Re-run previous input</td></tr>
<tr><td>%reset</td><td> Resets the namespace by removing all names defined by the user, if</td></tr>
<tr><td>%reset_selective</td><td> Resets the namespace by removing names defined by the user.</td></tr>
<tr><td>%run</td><td> Run the named file inside IPython as a program.</td></tr>
<tr><td>%save</td><td> Save a set of lines or a macro to a given filename.</td></tr>
<tr><td>%sc</td><td> Shell capture - run shell command and capture output (DEPRECATED use !).</td></tr>
<tr><td>%store</td><td> Lightweight persistence for python variables.</td></tr>
<tr><td>%sx</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>
<tr><td>%system</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>
<tr><td>%tb</td><td> Print the last traceback with the currently active exception mode.</td></tr>
<tr><td>%time</td><td> Time execution of a Python statement or expression.</td></tr>
<tr><td>%timeit</td><td> Time execution of a Python statement or expression</td></tr>
<tr><td>%unalias</td><td> Remove an alias</td></tr>
<tr><td>%unload_ext</td><td> Unload an IPython extension by its module name.</td></tr>
<tr><td>%who</td><td> Print all interactive variables, with some minimal formatting.</td></tr>
<tr><td>%who_ls</td><td> Return a sorted list of all interactive variables.</td></tr>
<tr><td>%whos</td><td> Like %who, but gives some extra information about each variable.</td></tr>
<tr><td>%xdel</td><td> Delete a variable, trying to clear it from anywhere that</td></tr>
<tr><td>%xmode</td><td> Switch modes for the exception handlers.</td></tr>
<tr><td>%%!</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>
<tr><td>%%bash</td><td> %%bash script magic</td></tr>
<tr><td>%%capture</td><td> %capture [--no-stderr] [--no-stdout] [output]</td></tr>
<tr><td>%%file</td><td> %file [-a] filename</td></tr>
<tr><td>%%perl</td><td> %%perl script magic</td></tr>
<tr><td>%%prun</td><td> Run a statement through the python code profiler.</td></tr>
<tr><td>%%python3</td><td> %%python3 script magic</td></tr>
<tr><td>%%ruby</td><td> %%ruby script magic</td></tr>
<tr><td>%%script</td><td> %shebang [--proc PROC] [--bg] [--err ERR] [--out OUT]</td></tr>
<tr><td>%%sh</td><td> %%sh script magic</td></tr>
<tr><td>%%sx</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>
<tr><td>%%system</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>
<tr><td>%%timeit</td><td> Time execution of a Python statement or expression</td></tr>
</table>
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Keyed_quick_ref_new"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Making an quickref HTML table"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Developed by Damon Allen\n",
" * Inspired by this [PDF](http://dl.dropboxusercontent.com/u/54552252/ipython-quickref.pdf)\n",
"* Last Modified: May 29, 2013, 9:10 pm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows the conversion of:\n",
"\n",
"* quickref's text to a dictionary \n",
"* a quickref ditionary to an HTML table"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pprint import pprint as pr\n",
"from collections import OrderedDict as Or_dict"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Parsing flags"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def heading_check(lines, index):\n",
" \"\"\"This function determines if the line at the provided is a heading.\"\"\"\n",
" check = (not(lines[index-1]) and not(lines[index+1]))\n",
" return check\n",
"\n",
"def title_check(lines, index):\n",
" \"\"\"This functions checks to see if the index is pointing at the title line.\"\"\"\n",
" check = (not(lines[index-1]) and (\"=======\" in lines[index+1]))\n",
" return check\n",
"\n",
"def right_column_check(lines, index):\n",
" \"\"\"This function determines if the line is part of an explanation, \n",
" e.i. it belongs in the right-hand column\"\"\"\n",
" line = lines[index]\n",
" return (('\\t' in line) or (' ' == line[0:7]))\n",
"\n",
"def comment_check(lines, index):\n",
" \"\"\"This fuinction determines if the line is a comment rather than an example or a subject header\"\"\"\n",
" colon_not_in_line = \":\" not in lines[index]\n",
" colon_in_line_before = \":\" in lines[index-1]\n",
" exists = lines[index] !=''\n",
" blank_line_before = not(lines[index-1])\n",
" \n",
" parts = lines[index].split(':')\n",
" more_than_one_part = len(parts)>1\n",
" first_part_without_extra_spaces = parts[0].strip()==parts[0]\n",
" not_right_hand_column = not(right_column_check(lines, index))\n",
" next_not_right_hand_column = not(right_column_check(lines, index+1))\n",
" \n",
" check1 = (colon_in_line_before and colon_not_in_line)\n",
" check2 = (exists and not(colon_in_line_before) and colon_not_in_line)\n",
" check3 = blank_line_before and more_than_one_part and first_part_without_extra_spaces\n",
" check4 = not_right_hand_column \n",
" check5 = next_not_right_hand_column\n",
" check6 = \"=====\" not in lines[index]\n",
" line = lines[index]\n",
" return ((check1 or check2 or check3)and check4 and check5 and check6 and exists)\n",
"\n",
"def multiline_start_check(lines, index, not_in_comments = True):#multiline_check\n",
" \"\"\"This function determines if the current line is actually the start of a multiple line example\"\"\"\n",
" not_empty = lines[index] != ''\n",
" not_markdown = '=====' not in lines[index]\n",
" parts = lines[index].split(':')\n",
" if len(parts)>1:\n",
" not_just_cmd = parts[1]!='' \n",
" else:\n",
" not_just_cmd = True\n",
" colon_not_in_line = \":\" not in lines[index]\n",
" not_right_hand_column = not(right_column_check(lines, index))\n",
" next_right_hand = right_column_check(lines, index+1)\n",
" check1 = colon_not_in_line and not_just_cmd and not_right_hand_column\n",
" check2 = not_empty and not_markdown and not_in_comments\n",
" check3 = next_right_hand and not_just_cmd\n",
" return (check1 and check2) or check3\n",
"\n",
"def check_for_range(lines, index):\n",
" \"\"\"Check for range text, i.e. [2:5], in the line \"\"\"\n",
" parts = lines[index].split(':')\n",
" check = False\n",
" for part in parts:\n",
" check = check or ('[' in part and ']' not in part)\n",
" return check"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Parsing flag testing"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext ipython_nose"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def test_multispace_Rcolumn():\n",
" \"\"\"Test that lines with multiple spaces in front are flagged to be in the right-hand column\"\"\"\n",
" Lines = [\":\",' ?obj, ??obj).']\n",
" assert right_column_check(Lines,1) == True\n",
" \n",
"def test_tab_escape_Rcolumn():\n",
" \"\"\"Test that lines with tab escapes infront are flagged to be in the right-hand column\"\"\"\n",
" Lines = [\":\",'\\tRight-hand column material']\n",
" assert right_column_check(Lines,1) == True\n",
" \n",
"def test_heading_check():\n",
" \"\"\"Test that lines that have blank lines before and after are flagged as headings\"\"\"\n",
" Lines = ['','Heading','']\n",
" assert heading_check(Lines,1) == True\n",
"\n",
"def test_title_check():\n",
" \"\"\"Test that lines that have blank lines before and a row of ==== after are flagged as titles\"\"\"\n",
" Lines = ['','Title Here','===========']\n",
" assert title_check(Lines,1) == True\n",
"\n",
"def test_comment_check_Type_1():\n",
" \"\"\"Test that lines without : and have blank lines before are flagged as comments\"\"\"\n",
" Lines = ['','comment here','']\n",
" assert comment_check(Lines,1) == True\n",
"\n",
"def test_comment_check_Type_2():\n",
" \"\"\"Test that lines like \"rember: comment\" with blank lines before are flagged as comments\"\"\"\n",
" Lines = ['','Rember: comment','' ]\n",
" assert comment_check(Lines,1) == True\n",
"\n",
"def test_comment_check_Type_3():\n",
" \"\"\"Test that lines without : and have a : in the line before but are NOT right hand columns are flagged as comments\"\"\"\n",
" Lines = [':','comment here','']\n",
" assert comment_check(Lines,1) == True\n",
" \n",
"def test_comment_check_Type_4():\n",
" \"\"\"0 - Test that lines that are part of multi-line comments are flagged as comments\"\"\"\n",
" Lines = ['comment here','comment here','' ]\n",
" assert comment_check(Lines,1) == True\n",
" \n",
"def test_comment_check_Type_5():\n",
" \"\"\"Test that lines with \"=========\" in them are not flagged as comments\"\"\"\n",
" Lines = ['Title','=========','']\n",
" assert comment_check(Lines,1) == False\n",
" \n",
"def test_comment_check_Type_6():\n",
" \"\"\"Test that lines that are right hand columns are not flagged as comments\"\"\"\n",
" Lines = [':','\\tnot comment', '']\n",
" assert comment_check(Lines,1) == False\n",
"\n",
"def test_multiline_start_check():\n",
" \"\"\"Test that lines are part of commands that span multiple lines are flaged as such\"\"\"\n",
" Lines = ['cmd: info','cmd part 1','cmd part 2', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == True\n",
"\n",
"def test_check_for_range():\n",
" \"\"\"Test that lines that contain ranges like [2:5] are flagged correctly\"\"\"\n",
" Lines = [\"\", \"[2:5]:range\"]\n",
" assert check_for_range(Lines, 1) == True\n",
"\n",
"def test_check_for_range_multiline():\n",
" \"\"\"2 - Test that multi-lines that contain ranges like [2:5] are flagged correctly\"\"\"\n",
" Lines = [\"\", \"[2:5]range\"]\n",
" assert check_for_range(Lines, 1) == True\n",
" \n",
"def test_multiline_start_check2():\n",
" \"\"\"3 - Test enrties that are just example folowed by the info on the next line are not flaged as multiline\"\"\"\n",
" Lines = ['cmd: info','cmd part 1:',' cmd part 2', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == False\n",
"\n",
"def test_multiline_start_check3():\n",
" \"\"\"4 - Test enrties that are have multiple lines of explanation are flaged as multiline\"\"\"\n",
" Lines = ['cmd: info','obj?, obj?? : Get help, or more help for object (also works as'\n",
" ,' ?obj, ??obj).', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == True\n",
" \n",
"def test_multiline_start_check4():\n",
" \"\"\"5 - Test enrties that are not multiple lines are not flaged as such\"\"\"\n",
" Lines = ['cmd: info','Test cmd: exlpained','next cmd: exlpained', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == False\n",
" \n",
"def test_multiline_start_check5():\n",
" \"\"\"6 - Test enrties that are have multiple lines of explanation are just after an empty line flaged as multiline\"\"\"\n",
" Lines = ['','obj?, obj?? : Get help, or more help for object (also works as'\n",
" ,' ?obj, ??obj).', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == True\n",
"\n",
"def test_multiline_start_check6():\n",
" \"\"\"7 - Test enrties that right hand entryies are not flagged as a multiline start\"\"\"\n",
" Lines = ['','obj?? : Get help, or more help for object (also works as'\n",
" ,' ?obj, ??obj).', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 2, True) == False\n",
"\n",
"def test_empty_multiline():\n",
" \"\"\"Test enrties are empty are not flagged as a multiline start\"\"\"\n",
" Lines = ['','','Title', 'cmd part 3', 'info']\n",
" assert multiline_start_check(Lines, 1, True) == False\n",
" \n",
"def test_check_for_range_empty():\n",
" \"\"\"Test enrties are empty are not flagged as a range\"\"\"\n",
" Lines = [\"\", \"\",\"[2:5]range\"]\n",
" assert check_for_range(Lines, 1) == False\n",
" \n",
"def test_empty_comment_check():\n",
" \"\"\"Test enrties are empty are not flagged as a comment\"\"\"\n",
" Lines = [':','', 'Heading']\n",
" assert comment_check(Lines,1) == False\n",
" \n",
"def test_empty_Rcolumn():\n",
" \"\"\"Test enrties are empty are not flagged as a right-hand column\"\"\"\n",
" Lines = [\":\",'','\\tRight-hand column material']\n",
" assert right_column_check(Lines,1) == False\n",
" \n",
"def test_empty_heading_check():\n",
" \"\"\"Test enrties are empty are not flagged as a heading\"\"\"\n",
" Lines = ['','','Heading','']\n",
" assert heading_check(Lines,1) == False\n",
"\n",
"def test_title_check():\n",
" \"\"\"Test enrties are empty are not flagged as a heading title\"\"\"\n",
" Lines = ['','','Title Here','===========']\n",
" assert title_check(Lines,1) == False\n",
" \n",
"def test_markdown_not_multiline():\n",
" \"\"\"Test enrties are empty are not flagged as a multiline start\"\"\"\n",
" Lines = ['','Title', '===============', '']\n",
" assert multiline_start_check(Lines, 2, True) == False\n",
"\n",
"def test_multiline_example_and_explain():\n",
" \"\"\"Test enrties that have multiple lines of example and explanation are flagged as a multiline start\"\"\"\n",
" Lines = [\"%timeit x=10 : time the 'x=10' statement with high precision.\",\n",
" '%%timeit x=2**100',\n",
" \"x**100 : time 'x*100' with a setup of 'x=2**100'; setup code is not\",\n",
" ' counted. This is an example of a cell magic.']\n",
" assert multiline_start_check(Lines, 1, True) == True"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# This is IPython magic..\n",
"%nose"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div id=\"ipython_nose_eb43ed4613e64c4583a695b8f11360d7\"></div>"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7 = $(\"#ipython_nose_eb43ed4613e64c4583a695b8f11360d7\");"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7.append($(\"<span>.</span>\"));"
],
"output_type": "display_data"
},
{
"javascript": [
"delete document.ipython_nose_eb43ed4613e64c4583a695b8f11360d7;"
],
"output_type": "display_data"
},
{
"html": [
" <style type=\"text/css\">\n",
" span.nosefailedfunc {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" }\n",
" div.noseresults {\n",
" width: 100%;\n",
" }\n",
" div.nosebar {\n",
" float: left;\n",
" padding: 1ex 0px 1ex 0px;\n",
" }\n",
" div.nosebar.fail {\n",
" background: #ff3019; /* Old browsers */\n",
" /* FF3.6+ */\n",
" background: -moz-linear-gradient(top, #ff3019 0%, #cf0404 100%);\n",
" /* Chrome,Safari4+ */\n",
" background: -webkit-gradient(linear, left top, left bottom,\n",
" color-stop(0%,#ff3019),\n",
" color-stop(100%,#cf0404));\n",
" /* Chrome10+,Safari5.1+ */\n",
" background: -webkit-linear-gradient(top, #ff3019 0%,#cf0404 100%);\n",
" /* Opera 11.10+ */\n",
" background: -o-linear-gradient(top, #ff3019 0%,#cf0404 100%);\n",
" /* IE10+ */\n",
" background: -ms-linear-gradient(top, #ff3019 0%,#cf0404 100%);\n",
" /* W3C */\n",
" background: linear-gradient(to bottom, #ff3019 0%,#cf0404 100%);\n",
" }\n",
" div.nosebar.pass {\n",
" background: #52b152;\n",
" background: -moz-linear-gradient(top, #52b152 1%, #008a00 100%);\n",
" background: -webkit-gradient(linear, left top, left bottom,\n",
" color-stop(1%,#52b152),\n",
" color-stop(100%,#008a00));\n",
" background: -webkit-linear-gradient(top, #52b152 1%,#008a00 100%);\n",
" background: -o-linear-gradient(top, #52b152 1%,#008a00 100%);\n",
" background: -ms-linear-gradient(top, #52b152 1%,#008a00 100%);\n",
" background: linear-gradient(to bottom, #52b152 1%,#008a00 100%);\n",
" }\n",
" div.nosebar.skip {\n",
" background: #f1e767;\n",
" background: -moz-linear-gradient(top, #f1e767 0%, #feb645 100%);\n",
" background: -webkit-gradient(linear, left top, left bottom,\n",
" color-stop(0%,#f1e767),\n",
" color-stop(100%,#feb645));\n",
" background: -webkit-linear-gradient(top, #f1e767 0%,#feb645 100%);\n",
" background: -o-linear-gradient(top, #f1e767 0%,#feb645 100%);\n",
" background: -ms-linear-gradient(top, #f1e767 0%,#feb645 100%);\n",
" background: linear-gradient(to bottom, #f1e767 0%,#feb645 100%);\n",
" }\n",
" div.nosebar.leftmost {\n",
" border-radius: 4px 0 0 4px;\n",
" }\n",
" div.nosebar.rightmost {\n",
" border-radius: 0 4px 4px 0;\n",
" }\n",
" div.nosefailbanner {\n",
" border-radius: 4px 0 0 4px;\n",
" border-left: 10px solid #cf0404;\n",
" padding: 0.5ex 0em 0.5ex 1em;\n",
" margin-top: 1ex;\n",
" margin-bottom: 0px;\n",
" }\n",
" div.nosefailbanner.expanded {\n",
" border-radius: 4px 4px 0 0;\n",
" border-top: 10px solid #cf0404;\n",
" }\n",
" pre.nosetraceback {\n",
" border-radius: 0 4px 4px 4px;\n",
" border-left: 10px solid #cf0404;\n",
" padding: 1em;\n",
" margin-left: 0px;\n",
" margin-top: 0px;\n",
" display: none;\n",
" }\n",
" </style>\n",
" \n",
" <script>\n",
" setTimeout(function () {\n",
" $('.nosefailtoggle').bind(\n",
" 'click',\n",
" function () {\n",
" $(\n",
" $(this)\n",
" .parent().toggleClass('expanded')\n",
" .parent()\n",
" .children()\n",
" .filter('.nosetraceback')\n",
" ).toggle();\n",
" }\n",
" );},\n",
" 0);\n",
" </script>\n",
" \n",
" <div class=\"noseresults\">\n",
" <div class=\"nosebar fail leftmost\" style=\"width: 0%\">\n",
" &nbsp;\n",
" </div>\n",
" <div class=\"nosebar skip\" style=\"width: 0%\">\n",
" &nbsp;\n",
" </div>\n",
" <div class=\"nosebar pass rightmost\" style=\"width: 100%\">\n",
" &nbsp;\n",
" </div>\n",
" 25/25 tests passed\n",
" </div>\n",
" "
],
"output_type": "pyout",
"prompt_number": 16,
"text": [
"25/25 tests passed\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Build quickref dictionary from text file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with open(\"quick_ref.txt\",'r') as f:\n",
" quickref_text = f.read()\n",
"lines = quickref_text.split('\\n')\n",
"print(len(lines))\n",
"#pr(lines)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"251\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def build_dict(lines):\n",
" \"\"\"This function takes an array of strings generated by quickref and turns it into an ordered dict\"\"\"\n",
" Quick_ref_dic = Or_dict()\n",
" index = 0\n",
" last_index = index\n",
" key_part = ''\n",
" top_style = ' style=\"border-bottom: 1px solid transparent;\">'\n",
" mid_style = ' style=\"border-top: 1px solid transparent; border-bottom: 1px solid transparent;\">'\n",
" bot_style = ' style=\"border-top: 1px solid transparent;\">'\n",
" break_section = True\n",
" multiline = {'In multiline':False,'column':'Left'}\n",
" lines.append(None)\n",
" while index<len(lines) and lines[index+1]!=None:\n",
" if not(lines[index]) or '=======' in lines[index]: #skip empty lines and markdown underline\n",
" if break_section:\n",
" break_section = False\n",
" in_comment = False\n",
" if index==0:\n",
" index +=1\n",
" continue\n",
" if title_check(lines, index): #or heading_check(lines, index):# or comment_check(lines, index):\n",
" \"\"\"Check to see if the current line is the title\"\"\"\n",
" subject = lines[index]\n",
" current_dic = Or_dict()\n",
" current_dic['_type_']='Title'\n",
" Quick_ref_dic[subject]=current_dic\n",
" elif heading_check(lines, index):# or comment_check(lines, index):\n",
" \"\"\"Check to see if the current line is a new heading\"\"\"\n",
" subject = lines[index]\n",
" current_dic = Or_dict()\n",
" current_dic['_type_']='Heading'\n",
" Quick_ref_dic[subject]=current_dic\n",
" elif comment_check(lines, index) and (not(break_section) or in_comment):\n",
" \"\"\"Check to see if the current line is a comment instead of an example\"\"\"\n",
" if not in_comment:\n",
" in_comment = True\n",
" current_dic = Or_dict()\n",
" current_dic['_type_']='Comment'\n",
" break_section = True\n",
" current_comment = '_comment_at_{}_'.format(str(index))\n",
" Quick_ref_dic[current_comment] = current_dic\n",
" comment = lines[index]\n",
" comment_key = '_Comment_line_at_{}_'.format(str(index))\n",
" if not(lines[index-1]):\n",
" style = top_style\n",
" elif not(lines[index+1]):\n",
" style = bot_style\n",
" else:\n",
" style = mid_style\n",
" current_dic[comment_key] = [style, comment]\n",
" elif multiline_start_check(lines, index) and not multiline['In multiline']:\n",
" \"\"\"Check to see is the current line is the start of a multilined example \"\"\"\n",
" current_dic['_Multiline_Flag_{}_'.format(str(index))] = []\n",
" multiline = {'In multiline':True,'column':'Left'}#'In multiline''In multiline'\n",
" parts = lines[index].split(':')\n",
" if len(parts)==1:\n",
" parts.append(' ')\n",
" key_part = [parts[0].strip()]\n",
" value_part = [parts[1].strip()]\n",
" #print(parts)\n",
" index +=1\n",
" continue\n",
" elif check_for_range(lines, index):\n",
" \"\"\"Make sure ranges like [2:5] aren't inturped as example:explanation \"\"\"\n",
" pieces = lines[index].split(':')\n",
" parts = []\n",
" tmp = ''\n",
" for part in pieces:\n",
" left = part.count('[')\n",
" right = part.count(']')\n",
" if left>right and not(tmp):\n",
" tmp = part\n",
" continue\n",
" if tmp:\n",
" parts.append(tmp+':'+part)\n",
" tmp=''\n",
" else:\n",
" parts.append(part)\n",
" if not(multiline['In multiline']):\n",
" part_key = parts[0]\n",
" part_key = part_key.strip()\n",
" Quick_ref_dic[subject][part_key]=parts[1]\n",
" else:\n",
" \n",
" if multiline['column'] == 'Left' and len(parts)==1:\n",
" parts.append(' ')\n",
" elif multiline['column'] == 'Right' and len(parts)==1:\n",
" parts.insert(0,' ')\n",
" key_part.append(parts[0])\n",
" value_part.append(parts[1])\n",
" index+=1\n",
" continue\n",
" elif right_column_check(lines, index):\n",
" \"\"\"Determine if the current line only belongs in the explanation column\"\"\"\n",
" if not(multiline['In multiline']):\n",
" key = list(Quick_ref_dic[subject].keys())[-1]\n",
" if Quick_ref_dic[subject][key] == None: #the previous line had no explanation\n",
" Quick_ref_dic[subject][key] = lines[index].strip('\\t')\n",
" else:\n",
" Quick_ref_dic[subject][key] = Quick_ref_dic[subject][key]+\" \"+lines[index].strip()\n",
" else:\n",
" if multiline['column'] == 'Left':\n",
" multiline['column'] = 'Right'\n",
" value_part.append(lines[index].strip())\n",
" elif \":\" in lines[index] or not(lines[index]):\n",
" \"\"\"This line contains atleast part of an example and maybe even part of an explanation \"\"\"\n",
" #print(lines[index])\n",
" parts = lines[index].split(\":\")\n",
" if len(parts)==1:\n",
" parts.append(None)\n",
" if multiline['In multiline']:\n",
" if right_column_check(lines, index+1):\n",
" key_part.append(parts[0])\n",
" value_part.append(parts[1])\n",
" index+=1\n",
" continue\n",
" if value_part[0]==' ' or value_part[0]=='':#shift the explanations to the top of the example\n",
" blank =[]\n",
" for index_b, b in enumerate(value_part):#remove the starting blank entries from the elplainations\n",
" #print(b)\n",
" if b==' ' or b=='':\n",
" blank.append(index_b)\n",
" else:\n",
" break\n",
" blank.reverse()\n",
" for index_b in blank:\n",
" value_part.pop(index_b)\n",
" len_check = len(key_part)-len(value_part)\n",
" if len_check>0:\n",
" value_part += [' ']*len_check\n",
" elif len_check<0:\n",
" key_part += [' ']*(-len_check)\n",
" Last_key = list(Quick_ref_dic[subject].keys())[-1]\n",
" entry = list(zip(key_part, value_part))\n",
" #print(entry)\n",
" Quick_ref_dic[subject][Last_key] = entry\n",
" multiline={'In multiline':False,'column':'Left'}\n",
" if lines[index]:\n",
" part_key = parts[0]\n",
" part_key = part_key.strip()\n",
" Quick_ref_dic[subject][part_key]=parts[1]\n",
" if lines[index]:\n",
" break_section = True\n",
" if multiline['In multiline'] and multiline['column']=='Left':\n",
" key_part.append(lines[index])\n",
" value_part.append(' ')\n",
" print('here')\n",
" if last_index+1!=index:\n",
" skipped = \"pre-processes {} lines\".format(index-last_index-1)\n",
" else:\n",
" skipped = \"\"\n",
" #print('loop {} {}'.format(index, skipped)) \n",
" last_index = index\n",
" index+=1\n",
" return Quick_ref_dic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Quick_ref_dic = build_dict(lines)\n",
"pr(Quick_ref_dic)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'IPython -- An enhanced Interactive Python - Quick Reference Card': {'_type_': 'Title',\n",
" '_Multiline_Flag_4_': [('obj?, obj??',\n",
" 'Get help, or more help for object (also works as'),\n",
" (' ',\n",
" '?obj, ??obj).')],\n",
" '?foo.*abc*': \" List names in 'foo' containing 'abc' in them.\",\n",
" '%magic': \" Information about IPython's 'magic' % functions.\"},\n",
" '_comment_at_9_': {'_type_': 'Comment',\n",
" '_Comment_line_at_9_': [' style=\"border-bottom: 1px solid transparent;\">',\n",
" 'Magic functions are prefixed by % or %%, and typically take their arguments'],\n",
" '_Comment_line_at_10_': [' style=\"border-top: 1px solid transparent; border-bottom: 1px solid transparent;\">',\n",
" 'without parentheses, quotes or even commas for convenience. Line magics take a'],\n",
" '_Comment_line_at_11_': [' style=\"border-top: 1px solid transparent;\">',\n",
" 'single % and cell magics are prefixed with two %%.']},\n",
" 'Example magic function calls:': {'_type_': 'Heading',\n",
" '%alias d ls -F': \" 'd' is now an alias for 'ls -F'\",\n",
" 'alias d ls -F': \" Works if 'alias' not a python name\",\n",
" 'alist = %alias': \" Get list of aliases to 'alist'\",\n",
" 'cd /usr/share': ' Obvious. cd -<tab> to choose from visited dirs.',\n",
" '%cd??': ' See help AND source for magic %cd',\n",
" '%timeit x=10': \" time the 'x=10' statement with high precision.\",\n",
" '_Multiline_Flag_21_': [('%%timeit x=2**100',\n",
" \" time 'x*100' with a setup of 'x=2**100'; setup code is not\"),\n",
" ('x**100 ',\n",
" 'counted. This is an example of a cell magic.')]},\n",
" 'System commands:': {'_type_': 'Heading',\n",
" '!cp a.txt b/': ' System command escape, calls os.system()',\n",
" 'cp a.txt b/': ' after %rehashx, most system commands work without !',\n",
" 'cp ${f}.txt $bar': ' Variable expansion in magics and system commands',\n",
" 'files = !ls /usr': ' Capture sytem command output',\n",
" 'files.s, files.l, files.n': ' \"a b c\", [\\'a\\',\\'b\\',\\'c\\'], \\'a\\\\nb\\\\nc\\''},\n",
" 'History:': {'_type_': 'Heading',\n",
" '_i, _ii, _iii': ' Previous, next previous, next next previous input',\n",
" '_i4, _ih[2:5]': ' Input history line 4, lines 2-4',\n",
" 'exec _i81': ' Execute input history line #81 again',\n",
" '%rep 81': ' Edit input history line #81',\n",
" '_, __, ___': ' previous, next previous, next next previous output',\n",
" '_dh': ' Directory history',\n",
" '_oh': ' Output history',\n",
" '%hist': \" Command history. '%hist -g foo' search history for 'foo'\"},\n",
" 'Autocall:': {'_type_': 'Heading',\n",
" 'f 1,2': ' f(1,2) # Off by default, enable with %autocall magic.',\n",
" '/f 1,2': ' f(1,2) (forced autoparen)',\n",
" ',f 1 2': ' f(\"1\",\"2\")',\n",
" ';f 1 2': ' f(\"1 2\")'},\n",
" '_comment_at_51_': {'_type_': 'Comment',\n",
" '_Comment_line_at_51_': [' style=\"border-bottom: 1px solid transparent;\">',\n",
" 'Remember: TAB completion works in many contexts, not just file names'],\n",
" '_Comment_line_at_52_': [' style=\"border-top: 1px solid transparent;\">',\n",
" 'or python names.']},\n",
" 'The following magic functions are currently available:': {'_type_': 'Heading',\n",
" '%alias': ' Define an alias for a system command.',\n",
" '%alias_magic': ' %alias_magic [-l] [-c] name target',\n",
" '%autocall': ' Make functions callable without having to type parentheses.',\n",
" '%automagic': ' Make magic functions callable without having to type the initial %.',\n",
" '%bookmark': \" Manage IPython's bookmark system.\",\n",
" '%cd': ' Change the current working directory.',\n",
" '%clear': ' Clear the terminal.',\n",
" '%colors': ' Switch color scheme for prompts, info system and exception handlers.',\n",
" '%config': ' configure IPython',\n",
" '%connect_info': ' Print information for connecting other clients to this kernel',\n",
" '%debug': ' Activate the interactive debugger in post-mortem mode.',\n",
" '%dhist': ' Print your history of visited directories.',\n",
" '%dirs': ' Return the current directory stack.',\n",
" '%doctest_mode': ' Toggle doctest mode on and off.',\n",
" '%ed': ' Alias for `%edit`.',\n",
" '%edit': ' Bring up an editor and execute the resulting code.',\n",
" '%env': ' List environment variables.',\n",
" '%gui': ' Enable or disable IPython GUI event loop integration.',\n",
" '%hist': ' Print input history (_i<n> variables), with most recent last.',\n",
" '%history': ' Print input history (_i<n> variables), with most recent last.',\n",
" '%install_default_config': ' %install_default_config has been deprecated.',\n",
" '%install_ext': ' Download and install an extension from a URL, e.g.::',\n",
" '%install_profiles': ' %install_profiles has been deprecated.',\n",
" '%killbgscripts': ' Kill all BG processes started by %%script and its family.',\n",
" '%less': ' Show a file through the pager.',\n",
" '%load': ' Load code into the current frontend.',\n",
" '%load_ext': ' Load an IPython extension by its module name.',\n",
" '%loadpy': ' Alias of `%load`',\n",
" '%logoff': ' Temporarily stop logging.',\n",
" '%logon': ' Restart logging.',\n",
" '%logstart': ' Start logging anywhere in a session.',\n",
" '%logstate': ' Print the status of the logging system.',\n",
" '%logstop': ' Fully stop logging and close log file.',\n",
" '%lsmagic': ' List currently available magic functions.',\n",
" '%macro': ' Define a macro for future re-execution. It accepts ranges of history,',\n",
" '%magic': ' Print information about the magic function system.',\n",
" '%man': ' Find the man page for the given command and display in pager.',\n",
" '%more': ' Show a file through the pager.',\n",
" '%notebook': ' %notebook [-e] [-f FORMAT] filename',\n",
" '%page': ' Pretty print the object and display it through a pager.',\n",
" '%pastebin': \" Upload code to Github's Gist paste bin, returning the URL.\",\n",
" '%pdb': ' Control the automatic calling of the pdb interactive debugger.',\n",
" '%pdef': ' Print the definition header for any callable object.',\n",
" '%pdoc': ' Print the docstring for an object.',\n",
" '%pfile': ' Print (or run through pager) the file where an object is defined.',\n",
" '%pinfo': ' Provide detailed information about an object.',\n",
" '%pinfo2': ' Provide extra detailed information about an object.',\n",
" '%popd': ' Change to directory popped off the top of the stack.',\n",
" '%pprint': ' Toggle pretty printing on/off.',\n",
" '%precision': ' Set floating point precision for pretty printing.',\n",
" '%profile': ' Print your currently active IPython profile.',\n",
" '%prun': ' Run a statement through the python code profiler.',\n",
" '%psearch': ' Search for object in namespaces by wildcard.',\n",
" '%psource': ' Print (or run through pager) the source code for an object.',\n",
" '%pushd': ' Place the current dir on stack and change directory.',\n",
" '%pwd': ' Return the current working directory path.',\n",
" '%pycat': ' Show a syntax-highlighted file through a pager.',\n",
" '%pylab': ' Load numpy and matplotlib to work interactively.',\n",
" '%qtconsole': ' Open a qtconsole connected to this kernel.',\n",
" '%quickref': ' Show a quick reference sheet',\n",
" '%quickref_file': ' Save a quick reference sheet on disk',\n",
" '%recall': ' Repeat a command, or get command to input line for editing.',\n",
" '%rehashx': ' Update the alias table with all executable files in $PATH.',\n",
" '%reload_ext': ' Reload an IPython extension by its module name.',\n",
" '%rep': ' Repeat a command, or get command to input line for editing.',\n",
" '%rerun': ' Re-run previous input',\n",
" '%reset': ' Resets the namespace by removing all names defined by the user, if',\n",
" '%reset_selective': ' Resets the namespace by removing names defined by the user.',\n",
" '%run': ' Run the named file inside IPython as a program.',\n",
" '%save': ' Save a set of lines or a macro to a given filename.',\n",
" '%sc': ' Shell capture - run shell command and capture output (DEPRECATED use !).',\n",
" '%store': ' Lightweight persistence for python variables.',\n",
" '%sx': ' Shell execute - run shell command and capture output (!! is short-hand).',\n",
" '%system': ' Shell execute - run shell command and capture output (!! is short-hand).',\n",
" '%tb': ' Print the last traceback with the currently active exception mode.',\n",
" '%time': ' Time execution of a Python statement or expression.',\n",
" '%timeit': ' Time execution of a Python statement or expression',\n",
" '%unalias': ' Remove an alias',\n",
" '%unload_ext': ' Unload an IPython extension by its module name.',\n",
" '%who': ' Print all interactive variables, with some minimal formatting.',\n",
" '%who_ls': ' Return a sorted list of all interactive variables.',\n",
" '%whos': ' Like %who, but gives some extra information about each variable.',\n",
" '%xdel': ' Delete a variable, trying to clear it from anywhere that',\n",
" '%xmode': ' Switch modes for the exception handlers.',\n",
" '%%!': ' Shell execute - run shell command and capture output (!! is short-hand).',\n",
" '%%bash': ' %%bash script magic',\n",
" '%%capture': ' %capture [--no-stderr] [--no-stdout] [output]',\n",
" '%%file': ' %file [-a] filename',\n",
" '%%perl': ' %%perl script magic',\n",
" '%%prun': ' Run a statement through the python code profiler.',\n",
" '%%python3': ' %%python3 script magic',\n",
" '%%ruby': ' %%ruby script magic',\n",
" '%%script': ' %shebang [--proc PROC] [--bg] [--err ERR] [--out OUT]',\n",
" '%%sh': ' %%sh script magic',\n",
" '%%sx': ' Shell execute - run shell command and capture output (!! is short-hand).',\n",
" '%%system': ' Shell execute - run shell command and capture output (!! is short-hand).',\n",
" '%%timeit': ' Time execution of a Python statement or expression'}}\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Build HTML from dictionary"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def clean_text(text):\n",
" \"\"\"This function replaces problem characters with HTML friendly characters \"\"\"\n",
" if '<' in text or '>' in text: #allow for the display of <n> and <tab>\n",
" text = text.replace('<','&#60')\n",
" text = text.replace('>','&#62')\n",
" if '$' in text: #ensure that $ renders as a $\n",
" #$ will render correctly when viewed in a browser but not\n",
" #using IPython.display.HTML\n",
" pass\n",
" text= text.strip('\\b')\n",
" return text\n",
"\n",
"def multiline_html(lines):\n",
" \"\"\"This function handels multi line examples (should be merged with comments)\"\"\"\n",
" table = ''\n",
" top_style = ' style=\"border-bottom: 1px solid transparent;\">'\n",
" mid_style = ' style=\"border-top: 1px solid transparent; border-bottom: 1px solid transparent;\">'\n",
" bot_style = ' style=\"border-top: 1px solid transparent;\">'\n",
" for line in lines:\n",
" if line==lines[0]:\n",
" style = top_style\n",
" elif line==lines[-1]:\n",
" style = bot_style\n",
" else:\n",
" style = mid_style\n",
" table += '<tr><td {0} {1} </td><td {0} {2}</tr>\\n'.format(style, line[0], line[1])\n",
" return table\n",
"\n",
"def Comment_lines(comment_dict):\n",
" \"\"\"This function handels comments in the quickref dictionary should be merged with multilines \"\"\"\n",
" table = ''\n",
" for key, value in comment_dict.items():\n",
" if '_Comment_line_' in key:\n",
" style = value[0]\n",
" comment = clean_text(value[1])\n",
" table += '<tr><td colspan=\"2\"; ' + style + comment + '</td></tr>\\n'\n",
" return table\n",
"\n",
"def build_HTML_table(QuickRef_Dict):\n",
" \"\"\"This function builds a HTML table of the quickref text like the one seen at \n",
" http://dl.dropboxusercontent.com/u/54552252/ipython-quickref.pdf\"\"\"\n",
" Table = '<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\">\\n'\n",
" comment_start = True\n",
" for key, value in QuickRef_Dict.items():\n",
" label = clean_text(key)\n",
" if value['_type_'] == 'Title' or value['_type_'] == 'Heading':\n",
" if value['_type_'] == 'Title':\n",
" color = 'lightBlue'\n",
" else:\n",
" color = '#D5E0C5'\n",
" Table+='<tr><td colspan=\"2\"; > </td></tr>\\n'\n",
" background = ' style=\"background-color:'+color+';\" '\n",
" Table+=\"<tr><td\"+background+' colspan=\"2\"; ><b>'+label+\"</b></td></tr>\\n\"\n",
" Table+='<tr><td colspan=\"2\"; > </td></tr>\\n'\n",
" for example, explanation in value.items():\n",
" if ('_Multiline_Flag_' not in example) and ('_type_' not in example):\n",
" text_left = clean_text(example)\n",
" text_right = clean_text(explanation)\n",
" Table+=\"<tr><td>\"+text_left+\"</td><td>\"+text_right+\"</td></tr>\\n\"\n",
" elif '_Multiline_Flag_' in example:\n",
" Table += multiline_html(explanation)\n",
" if value['_type_'] == 'Comment':\n",
" Table+='<tr><td colspan=\"2\"; > </td></tr>\\n'\n",
" Table+=Comment_lines(value)\n",
" \n",
" Table +=\"</table>\"\n",
" return Table"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import HTML\n",
"Table = build_HTML_table(Quick_ref_dic)\n",
"with open('HTML_Table_alt.html','w') as f:\n",
" f.write(Table)\n",
"HTML(Table)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid black;border-collapse:collapse;\">\n",
"<tr><td style=\"background-color:lightBlue;\" colspan=\"2\"; ><b>IPython -- An enhanced Interactive Python - Quick Reference Card</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"border-bottom: 1px solid transparent;\"> obj?, obj?? </td><td style=\"border-bottom: 1px solid transparent;\"> Get help, or more help for object (also works as</tr>\n",
"<tr><td style=\"border-top: 1px solid transparent;\"> </td><td style=\"border-top: 1px solid transparent;\"> ?obj, ??obj).</tr>\n",
"<tr><td>?foo.*abc*</td><td> List names in 'foo' containing 'abc' in them.</td></tr>\n",
"<tr><td>%magic</td><td> Information about IPython's 'magic' % functions.</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td colspan=\"2\"; style=\"border-bottom: 1px solid transparent;\">Magic functions are prefixed by % or %%, and typically take their arguments</td></tr>\n",
"<tr><td colspan=\"2\"; style=\"border-top: 1px solid transparent; border-bottom: 1px solid transparent;\">without parentheses, quotes or even commas for convenience. Line magics take a</td></tr>\n",
"<tr><td colspan=\"2\"; style=\"border-top: 1px solid transparent;\">single % and cell magics are prefixed with two %%.</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"background-color:#D5E0C5;\" colspan=\"2\"; ><b>Example magic function calls:</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td>%alias d ls -F</td><td> 'd' is now an alias for 'ls -F'</td></tr>\n",
"<tr><td>alias d ls -F</td><td> Works if 'alias' not a python name</td></tr>\n",
"<tr><td>alist = %alias</td><td> Get list of aliases to 'alist'</td></tr>\n",
"<tr><td>cd /usr/share</td><td> Obvious. cd -&#60tab&#62 to choose from visited dirs.</td></tr>\n",
"<tr><td>%cd??</td><td> See help AND source for magic %cd</td></tr>\n",
"<tr><td>%timeit x=10</td><td> time the 'x=10' statement with high precision.</td></tr>\n",
"<tr><td style=\"border-bottom: 1px solid transparent;\"> %%timeit x=2**100 </td><td style=\"border-bottom: 1px solid transparent;\"> time 'x*100' with a setup of 'x=2**100'; setup code is not</tr>\n",
"<tr><td style=\"border-top: 1px solid transparent;\"> x**100 </td><td style=\"border-top: 1px solid transparent;\"> counted. This is an example of a cell magic.</tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"background-color:#D5E0C5;\" colspan=\"2\"; ><b>System commands:</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td>!cp a.txt b/</td><td> System command escape, calls os.system()</td></tr>\n",
"<tr><td>cp a.txt b/</td><td> after %rehashx, most system commands work without !</td></tr>\n",
"<tr><td>cp ${f}.txt $bar</td><td> Variable expansion in magics and system commands</td></tr>\n",
"<tr><td>files = !ls /usr</td><td> Capture sytem command output</td></tr>\n",
"<tr><td>files.s, files.l, files.n</td><td> \"a b c\", ['a','b','c'], 'a\\nb\\nc'</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"background-color:#D5E0C5;\" colspan=\"2\"; ><b>History:</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td>_i, _ii, _iii</td><td> Previous, next previous, next next previous input</td></tr>\n",
"<tr><td>_i4, _ih[2:5]</td><td> Input history line 4, lines 2-4</td></tr>\n",
"<tr><td>exec _i81</td><td> Execute input history line #81 again</td></tr>\n",
"<tr><td>%rep 81</td><td> Edit input history line #81</td></tr>\n",
"<tr><td>_, __, ___</td><td> previous, next previous, next next previous output</td></tr>\n",
"<tr><td>_dh</td><td> Directory history</td></tr>\n",
"<tr><td>_oh</td><td> Output history</td></tr>\n",
"<tr><td>%hist</td><td> Command history. '%hist -g foo' search history for 'foo'</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"background-color:#D5E0C5;\" colspan=\"2\"; ><b>Autocall:</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td>f 1,2</td><td> f(1,2) # Off by default, enable with %autocall magic.</td></tr>\n",
"<tr><td>/f 1,2</td><td> f(1,2) (forced autoparen)</td></tr>\n",
"<tr><td>,f 1 2</td><td> f(\"1\",\"2\")</td></tr>\n",
"<tr><td>;f 1 2</td><td> f(\"1 2\")</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td colspan=\"2\"; style=\"border-bottom: 1px solid transparent;\">Remember: TAB completion works in many contexts, not just file names</td></tr>\n",
"<tr><td colspan=\"2\"; style=\"border-top: 1px solid transparent;\">or python names.</td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td style=\"background-color:#D5E0C5;\" colspan=\"2\"; ><b>The following magic functions are currently available:</b></td></tr>\n",
"<tr><td colspan=\"2\"; > </td></tr>\n",
"<tr><td>%alias</td><td> Define an alias for a system command.</td></tr>\n",
"<tr><td>%alias_magic</td><td> %alias_magic [-l] [-c] name target</td></tr>\n",
"<tr><td>%autocall</td><td> Make functions callable without having to type parentheses.</td></tr>\n",
"<tr><td>%automagic</td><td> Make magic functions callable without having to type the initial %.</td></tr>\n",
"<tr><td>%bookmark</td><td> Manage IPython's bookmark system.</td></tr>\n",
"<tr><td>%cd</td><td> Change the current working directory.</td></tr>\n",
"<tr><td>%clear</td><td> Clear the terminal.</td></tr>\n",
"<tr><td>%colors</td><td> Switch color scheme for prompts, info system and exception handlers.</td></tr>\n",
"<tr><td>%config</td><td> configure IPython</td></tr>\n",
"<tr><td>%connect_info</td><td> Print information for connecting other clients to this kernel</td></tr>\n",
"<tr><td>%debug</td><td> Activate the interactive debugger in post-mortem mode.</td></tr>\n",
"<tr><td>%dhist</td><td> Print your history of visited directories.</td></tr>\n",
"<tr><td>%dirs</td><td> Return the current directory stack.</td></tr>\n",
"<tr><td>%doctest_mode</td><td> Toggle doctest mode on and off.</td></tr>\n",
"<tr><td>%ed</td><td> Alias for `%edit`.</td></tr>\n",
"<tr><td>%edit</td><td> Bring up an editor and execute the resulting code.</td></tr>\n",
"<tr><td>%env</td><td> List environment variables.</td></tr>\n",
"<tr><td>%gui</td><td> Enable or disable IPython GUI event loop integration.</td></tr>\n",
"<tr><td>%hist</td><td> Print input history (_i&#60n&#62 variables), with most recent last.</td></tr>\n",
"<tr><td>%history</td><td> Print input history (_i&#60n&#62 variables), with most recent last.</td></tr>\n",
"<tr><td>%install_default_config</td><td> %install_default_config has been deprecated.</td></tr>\n",
"<tr><td>%install_ext</td><td> Download and install an extension from a URL, e.g.::</td></tr>\n",
"<tr><td>%install_profiles</td><td> %install_profiles has been deprecated.</td></tr>\n",
"<tr><td>%killbgscripts</td><td> Kill all BG processes started by %%script and its family.</td></tr>\n",
"<tr><td>%less</td><td> Show a file through the pager.</td></tr>\n",
"<tr><td>%load</td><td> Load code into the current frontend.</td></tr>\n",
"<tr><td>%load_ext</td><td> Load an IPython extension by its module name.</td></tr>\n",
"<tr><td>%loadpy</td><td> Alias of `%load`</td></tr>\n",
"<tr><td>%logoff</td><td> Temporarily stop logging.</td></tr>\n",
"<tr><td>%logon</td><td> Restart logging.</td></tr>\n",
"<tr><td>%logstart</td><td> Start logging anywhere in a session.</td></tr>\n",
"<tr><td>%logstate</td><td> Print the status of the logging system.</td></tr>\n",
"<tr><td>%logstop</td><td> Fully stop logging and close log file.</td></tr>\n",
"<tr><td>%lsmagic</td><td> List currently available magic functions.</td></tr>\n",
"<tr><td>%macro</td><td> Define a macro for future re-execution. It accepts ranges of history,</td></tr>\n",
"<tr><td>%magic</td><td> Print information about the magic function system.</td></tr>\n",
"<tr><td>%man</td><td> Find the man page for the given command and display in pager.</td></tr>\n",
"<tr><td>%more</td><td> Show a file through the pager.</td></tr>\n",
"<tr><td>%notebook</td><td> %notebook [-e] [-f FORMAT] filename</td></tr>\n",
"<tr><td>%page</td><td> Pretty print the object and display it through a pager.</td></tr>\n",
"<tr><td>%pastebin</td><td> Upload code to Github's Gist paste bin, returning the URL.</td></tr>\n",
"<tr><td>%pdb</td><td> Control the automatic calling of the pdb interactive debugger.</td></tr>\n",
"<tr><td>%pdef</td><td> Print the definition header for any callable object.</td></tr>\n",
"<tr><td>%pdoc</td><td> Print the docstring for an object.</td></tr>\n",
"<tr><td>%pfile</td><td> Print (or run through pager) the file where an object is defined.</td></tr>\n",
"<tr><td>%pinfo</td><td> Provide detailed information about an object.</td></tr>\n",
"<tr><td>%pinfo2</td><td> Provide extra detailed information about an object.</td></tr>\n",
"<tr><td>%popd</td><td> Change to directory popped off the top of the stack.</td></tr>\n",
"<tr><td>%pprint</td><td> Toggle pretty printing on/off.</td></tr>\n",
"<tr><td>%precision</td><td> Set floating point precision for pretty printing.</td></tr>\n",
"<tr><td>%profile</td><td> Print your currently active IPython profile.</td></tr>\n",
"<tr><td>%prun</td><td> Run a statement through the python code profiler.</td></tr>\n",
"<tr><td>%psearch</td><td> Search for object in namespaces by wildcard.</td></tr>\n",
"<tr><td>%psource</td><td> Print (or run through pager) the source code for an object.</td></tr>\n",
"<tr><td>%pushd</td><td> Place the current dir on stack and change directory.</td></tr>\n",
"<tr><td>%pwd</td><td> Return the current working directory path.</td></tr>\n",
"<tr><td>%pycat</td><td> Show a syntax-highlighted file through a pager.</td></tr>\n",
"<tr><td>%pylab</td><td> Load numpy and matplotlib to work interactively.</td></tr>\n",
"<tr><td>%qtconsole</td><td> Open a qtconsole connected to this kernel.</td></tr>\n",
"<tr><td>%quickref</td><td> Show a quick reference sheet</td></tr>\n",
"<tr><td>%quickref_file</td><td> Save a quick reference sheet on disk</td></tr>\n",
"<tr><td>%recall</td><td> Repeat a command, or get command to input line for editing.</td></tr>\n",
"<tr><td>%rehashx</td><td> Update the alias table with all executable files in $PATH.</td></tr>\n",
"<tr><td>%reload_ext</td><td> Reload an IPython extension by its module name.</td></tr>\n",
"<tr><td>%rep</td><td> Repeat a command, or get command to input line for editing.</td></tr>\n",
"<tr><td>%rerun</td><td> Re-run previous input</td></tr>\n",
"<tr><td>%reset</td><td> Resets the namespace by removing all names defined by the user, if</td></tr>\n",
"<tr><td>%reset_selective</td><td> Resets the namespace by removing names defined by the user.</td></tr>\n",
"<tr><td>%run</td><td> Run the named file inside IPython as a program.</td></tr>\n",
"<tr><td>%save</td><td> Save a set of lines or a macro to a given filename.</td></tr>\n",
"<tr><td>%sc</td><td> Shell capture - run shell command and capture output (DEPRECATED use !).</td></tr>\n",
"<tr><td>%store</td><td> Lightweight persistence for python variables.</td></tr>\n",
"<tr><td>%sx</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>\n",
"<tr><td>%system</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>\n",
"<tr><td>%tb</td><td> Print the last traceback with the currently active exception mode.</td></tr>\n",
"<tr><td>%time</td><td> Time execution of a Python statement or expression.</td></tr>\n",
"<tr><td>%timeit</td><td> Time execution of a Python statement or expression</td></tr>\n",
"<tr><td>%unalias</td><td> Remove an alias</td></tr>\n",
"<tr><td>%unload_ext</td><td> Unload an IPython extension by its module name.</td></tr>\n",
"<tr><td>%who</td><td> Print all interactive variables, with some minimal formatting.</td></tr>\n",
"<tr><td>%who_ls</td><td> Return a sorted list of all interactive variables.</td></tr>\n",
"<tr><td>%whos</td><td> Like %who, but gives some extra information about each variable.</td></tr>\n",
"<tr><td>%xdel</td><td> Delete a variable, trying to clear it from anywhere that</td></tr>\n",
"<tr><td>%xmode</td><td> Switch modes for the exception handlers.</td></tr>\n",
"<tr><td>%%!</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>\n",
"<tr><td>%%bash</td><td> %%bash script magic</td></tr>\n",
"<tr><td>%%capture</td><td> %capture [--no-stderr] [--no-stdout] [output]</td></tr>\n",
"<tr><td>%%file</td><td> %file [-a] filename</td></tr>\n",
"<tr><td>%%perl</td><td> %%perl script magic</td></tr>\n",
"<tr><td>%%prun</td><td> Run a statement through the python code profiler.</td></tr>\n",
"<tr><td>%%python3</td><td> %%python3 script magic</td></tr>\n",
"<tr><td>%%ruby</td><td> %%ruby script magic</td></tr>\n",
"<tr><td>%%script</td><td> %shebang [--proc PROC] [--bg] [--err ERR] [--out OUT]</td></tr>\n",
"<tr><td>%%sh</td><td> %%sh script magic</td></tr>\n",
"<tr><td>%%sx</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>\n",
"<tr><td>%%system</td><td> Shell execute - run shell command and capture output (!! is short-hand).</td></tr>\n",
"<tr><td>%%timeit</td><td> Time execution of a Python statement or expression</td></tr>\n",
"</table>"
],
"output_type": "pyout",
"prompt_number": 18,
"text": [
"<IPython.core.display.HTML at 0x489ed50>"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment