From 68c58db5aadc89fe16ea8966e46cab3643af5a55 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Tue, 26 Mar 2019 21:28:26 +0100 Subject: [PATCH] rebased to current master --- README.md | 20 +++++++-------- web/script.js | 70 +++++++++++++++++++++++++++++++-------------------- web/style.css | 47 ++++++++++++++++++---------------- 3 files changed, 79 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index cf534ab..6d91881 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Comprehensive Python Cheatsheet Contents -------- -**   ** **1. Collections:** ** ** **[`List`](#list)**__,__ **[`Dict`](#dictionary)**__,__ **[`Set`](#set)**__,__ **[`Range`](#range)**__,__ **[`Enumerate`](#enumerate)**__,__ **[`Namedtuple`](#named-tuple)**__,__ **[`Iterator`](#iterator)**__,__ **[`Generator`](#generator)**__.__ -**   ** **2. Types:** **          ** **[`Type`](#type)**__,__ **[`String`](#string)**__,__ **[`Regex`](#regex)**__,__ **[`Format`](#format)**__,__ **[`Numbers`](#numbers)**__,__ **[`Combinatorics`](#combinatorics)**__,__ **[`Datetime`](#datetime)**__.__ -**   ** **3. Syntax:** **         ** **[`Arguments`](#arguments)**__,__ **[`Splat`](#splat-operator)**__,__ **[`Inline`](#inline)**__,__ **[`Closure`](#closure)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Enum`](#enum)**__,__ **[`Exceptions`](#exceptions)**__.__ -**   ** **4. System:** **        ** **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#path)**__,__ **[`Command_Execution`](#command-execution)**__.__ -**   ** **5. Data:** **             ** **[`CSV`](#csv)**__,__ **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__ -**   ** **6. Advanced:** **   ** **[`Threading`](#threading)**__,__ **[`Introspection`](#introspection)**__,__ **[`Metaprograming`](#metaprograming)**__,__ **[`Operator`](#operator)**__,__ **[`Eval`](#eval)**__,__ **[`Coroutine`](#coroutine)**__.__ -**   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profile)**__,__ +**   ** **1. Collections:** ** ** **[`List`](#list)**__,__ **[`Dict`](#dictionary)**__,__ **[`Set`](#set)**__,__ **[`Range`](#range)**__,__ **[`Enumerate`](#enumerate)**__,__ **[`Namedtuple`](#named-tuple)**__,__ **[`Iterator`](#iterator)**__,__ **[`Generator`](#generator)**__.__ +**   ** **2. Types:** **          ** **[`Type`](#type)**__,__ **[`String`](#string)**__,__ **[`Regex`](#regex)**__,__ **[`Format`](#format)**__,__ **[`Numbers`](#numbers)**__,__ **[`Combinatorics`](#combinatorics)**__,__ **[`Datetime`](#datetime)**__.__ +**   ** **3. Syntax:** **         ** **[`Arguments`](#arguments)**__,__ **[`Splat`](#splat-operator)**__,__ **[`Inline`](#inline)**__,__ **[`Closure`](#closure)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Enum`](#enum)**__,__ **[`Exceptions`](#exceptions)**__.__ +**   ** **4. System:** **        ** **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#path)**__,__ **[`Command_Execution`](#command-execution)**__.__ +**   ** **5. Data:** **             ** **[`CSV`](#csv)**__,__ **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__ +**   ** **6. Advanced:** **   ** **[`Threading`](#threading)**__,__ **[`Introspection`](#introspection)**__,__ **[`Metaprograming`](#metaprograming)**__,__ **[`Operator`](#operator)**__,__ **[`Eval`](#eval)**__,__ **[`Coroutine`](#coroutine)**__.__ +**   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profile)**__,__ **                                 ** **[`NumPy`](#numpy)**__,__ **[`Image`](#image)**__,__ **[`Audio`](#audio)**__.__ @@ -467,7 +467,7 @@ from dateutil.tz import UTC, tzlocal, gettz = date(year, month, day) = time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, fold=0)
= datetime(year, month, day, hour=0, minute=0, second=0, ...) - = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, + = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) ``` * **Use `'.weekday()'` to get the day of the week (Mon == 0).** @@ -1834,7 +1834,7 @@ duration = pc() - start_time ### Timing a Snippet ```python >>> from timeit import timeit ->>> timeit('"-".join(str(a) for a in range(100))', +>>> timeit('"-".join(str(a) for a in range(100))', ... number=10000, globals=globals(), setup='pass') 0.34986 ``` @@ -2082,7 +2082,7 @@ Basic Script Template #!/usr/bin/env python3 # # Usage: .py -# +# from collections import namedtuple from enum import Enum diff --git a/web/script.js b/web/script.js index 813a01f..5f0a203 100644 --- a/web/script.js +++ b/web/script.js @@ -3,25 +3,42 @@ $(document).ready(function() { }); function parseMd() { - var GITHUB = 'https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md'; + var GITHUB = + "https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md"; jQuery.get(GITHUB, function(text) { + console.log(text); + text = removeMdToc(text); + console.log(text); var converter = new showdown.Converter(); html = converter.makeHtml(text); - aDiv = $('#main_container'); + aDiv = $("#main_container"); nodes = $.parseHTML(html); aDiv.after(nodes); removeOrigToc(); insertLinks(); - d3.selectAll("code").each(function() { hljs.highlightBlock(this); }); + addImgParentClass(); + d3.selectAll("code").each(function() { + hljs.highlightBlock(this); + }); addToc(); }); } -function removeOrigToc() { - headerContents = $('#contents') - contentsList = headerContents.next() - headerContents.remove() - contentsList.remove() +function removeMdToc(text) { + var out = []; + lines = text.match(/[^\r\n]+/g); + insideContents = false; + for (line of lines) { + if (line.trim() === "Contents") { + insideContents = true; + } else if (line.trim() === "Main") { + insideContents = false; + } + if (!insideContents) { + out.push(line); + } + } + return out.join("\n"); } function insertLinks() { @@ -32,29 +49,28 @@ function insertLinks() { }); } function addImgParentClass() { - $("img").each(function() { - $(this) - .parent() - .addClass("img_parent"); - }); + let monty_img = $("img").first(); + monty_img.parent().addClass("img_parent"); + monty_img.before("
"); } function addToc() { - headerMain = $('#main'); + headerMain = $("#main"); nodes = $.parseHTML(TOC); headerMain.before(nodes); } -var TOC = '
' + -'

Contents#

\n' + -'
ToC = {\n' +
-'    \'1. Collections\': [List, Dict, Set, Range, Enumerate, Namedtuple, Iterator, Generator],\n' +
-'    \'2. Types\':       [Type, String, Regex, Format, Numbers, Combinatorics, Datetimeᴺᴱᵂ],\n' +
-'    \'3. Syntax\':      [Arguments, Splat, Inline, Closure, Decorator, Class, Enum, Exceptions],\n' +
-'    \'4. System\':      [Print, Input, Command_Line_Arguments, Open, Pathᴺᴱᵂ, Command_Execution],\n' +
-'    \'5. Data\':        [CSV, JSON, Pickle, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' +
-'    \'6. Advanced\':    [Threading, Introspection, Metaprograming, Operator, Eval, Coroutine],\n' +
-'    \'7. Libraries\':   [Progress_Bar, Plot, Table, Curses, Loggingᴺᴱᵂ, Scraping, Web, Profile,\n' +
-'                       NumPy, Image, Audio]\n' +
-'}\n' +
-'
\n'; +var TOC = + "
" + + '

Contents#

\n' + + '
ToC = {\n' +
+  '    \'1. Collections\': [List, Dict, Set, Range, Enumerate, Namedtuple, Iterator, Generator],\n' +
+  '    \'2. Types\':       [Type, String, Regex, Format, Numbers, Combinatorics, Datetimeᴺᴱᵂ],\n' +
+  '    \'3. Syntax\':      [Arguments, Splat, Inline, Closure, Decorator, Class, Enum, Exceptions],\n' +
+  '    \'4. System\':      [Print, Input, Command_Line_Arguments, Open, Pathᴺᴱᵂ, Command_Execution],\n' +
+  '    \'5. Data\':        [CSV, JSON, Pickle, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' +
+  '    \'6. Advanced\':    [Threading, Introspection, Metaprograming, Operator, Eval, Coroutine],\n' +
+  '    \'7. Libraries\':   [Progress_Bar, Plot, Table, Curses, Loggingᴺᴱᵂ, Scraping, Web, Profile,\n' +
+  '                       NumPy, Image, Audio]\n' +
+  "}\n" +
+  "
\n"; diff --git a/web/style.css b/web/style.css index c05e2b7..6ddac8c 100644 --- a/web/style.css +++ b/web/style.css @@ -1,9 +1,7 @@ - /* Copyright 2013 Michael Bostock. All rights reserved. Do not copy. */ @import url(https://fonts.googleapis.com/css?family=PT+Serif|PT+Serif:b|PT+Serif:i|PT+Sans|PT+Sans:b); - .ocks-org body { background: #fcfcfa; color: #333; @@ -11,6 +9,7 @@ margin: 1em auto 4em auto; position: relative; width: 960px; + padding: 1rem; } .ocks-org header, @@ -39,21 +38,22 @@ h1 { font-size: 64px; font-weight: 300; letter-spacing: -2px; - margin: .3em 0 .1em 0; + margin: 0.3em 0 0.1em 0; } h2 { margin-top: 2em; } -h1, h2 { +h1, +h2 { text-rendering: optimizeLegibility; } h2 a[name], h2 a[id] { color: #ccc; - padding: 0 .3em; + padding-right: 0.3em; } header, @@ -64,12 +64,12 @@ footer { .ocks-org header aside, .ocks-org footer aside { float: left; - margin-right: .5em; + margin-right: 0.5em; } .ocks-org header aside:after, .ocks-org footer aside:after { - padding-left: .5em; + padding-left: 0.5em; content: "/"; } @@ -89,7 +89,8 @@ h1 ~ aside { margin-bottom: 2em; } -body > p, li > p { +body > p, +li > p { line-height: 1.5em; } @@ -123,7 +124,9 @@ a:not(:hover) { text-decoration: none; } -pre, code, textarea { +pre, +code, +textarea { font-family: "Menlo", monospace; } @@ -187,13 +190,15 @@ svg { font: 10px sans-serif; } -.axis path, .axis line { +.axis path, +.axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } -sup, sub { +sup, +sub { line-height: 0; } @@ -214,23 +219,23 @@ blockquote q:before, blockquote q:after { content: ""; } -h3,h4,p, ul{ +h3, +h4, +p, +ul { padding-left: 2rem; } -.img_parent{ +.img_parent { padding: 0; } -@media only screen -and (max-device-width : 1024px){ - .ocks-org body { - margin: 1rem; - } - - h1{ +@media only screen and (max-device-width: 1024px) { + h1 { font-size: 3rem; } - .img_parent, h1, img{ + .img_parent, + h1, + img { max-width: calc(100vw - 2em); min-width: calc(100vw - 2em); }