You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2948 lines
317 KiB
2948 lines
317 KiB
<!DOCTYPE html>
|
|
<html class="ocks-org do-not-copy" lang="en">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
|
|
<title>Comprehensive Python Cheatsheet</title>
|
|
<meta name="description" content="Exhaustive, simple, beautiful and concise. A truly Pythonic cheat sheet about Python programming language.">
|
|
<link rel="icon" href="web/favicon.png">
|
|
|
|
<link rel="stylesheet" href="web/default.min.css">
|
|
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css">
|
|
<link rel="stylesheet" href="web/style.css">
|
|
|
|
<script>
|
|
// Checks if URL ends with "index.html?dark=true"
|
|
if (window.location.search.search(/[?&]theme=dark3/) !== -1) {
|
|
document.write("<link rel=\"stylesheet\" href=\"web/default_dark3.min.css\">");
|
|
document.write("<link rel=\"stylesheet\" href=\"web/style_dark3.css\">");
|
|
}
|
|
else if (window.location.search.search(/[?&]theme=dark2/) !== -1) {
|
|
document.write("<link rel=\"stylesheet\" href=\"web/default_dark2.min.css\">");
|
|
document.write("<link rel=\"stylesheet\" href=\"web/style_dark2.css\">");
|
|
}
|
|
else if (window.location.search.search(/[?&]theme=dark1/) !== -1) {
|
|
document.write("<link rel=\"stylesheet\" href=\"web/default_dark1.min.css\">");
|
|
document.write("<link rel=\"stylesheet\" href=\"web/style_dark1.css\">");
|
|
}
|
|
else if (window.location.search.search(/[?&]theme=dark/) !== -1) {
|
|
document.write("<link rel=\"stylesheet\" href=\"web/default_dark.min.css\">");
|
|
document.write("<link rel=\"stylesheet\" href=\"web/style_dark.css\">");
|
|
}
|
|
</script>
|
|
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:title" content="Comprehensive Python Cheatsheet">
|
|
<meta name="twitter:description" content="Exhaustive, simple, beautiful and concise. A truly Pythonic cheat sheet about Python programming language.">
|
|
<meta name="twitter:image" content="https://gto76.github.io/python-cheatsheet/web/image_social_3.png">
|
|
|
|
<meta property="og:url" content="https://gto76.github.io/python-cheatsheet/">
|
|
<meta property="og:title" content="Comprehensive Python Cheatsheet">
|
|
<meta property="og:description" content="Exhaustive, simple, beautiful and concise. A truly Pythonic cheat sheet about Python programming language.">
|
|
<meta property="og:site_name" content="gto76.github.io">
|
|
<meta property="og:image" content="https://gto76.github.io/python-cheatsheet/web/image_social_3.png">
|
|
|
|
<meta itemprop="url" content="https://gto76.github.io/python-cheatsheet/">
|
|
<meta itemprop="name" content="Comprehensive Python Cheatsheet">
|
|
<meta itemprop="description" content="Exhaustive, simple, beautiful and concise. A truly Pythonic cheat sheet about Python programming language.">
|
|
<meta itemprop="image" content="https://gto76.github.io/python-cheatsheet/web/image_social_3.png">
|
|
|
|
<meta name="google-site-verification" content="w3rvuG0D1kUm_w20qsJecSEZh59Am8jK4eSPVU83e_M">
|
|
<meta name="viewport" id="viewport-meta">
|
|
</head>
|
|
|
|
<body>
|
|
<header>
|
|
<aside>September 8, 2024</aside>
|
|
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
|
</header>
|
|
|
|
<div><h1 id="comprehensivepythoncheatsheet">Comprehensive Python Cheatsheet</h1><p class="banner"><sup><a href="https://raw.githubusercontent.com/gto76/python-cheatsheet/main/README.md">Download text file</a>, <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <a href="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a>, <a href="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a> or <a href="index.html?theme=dark3">Switch to dark theme</a>.
|
|
</sup></p><p class="banner"><img src="web/image_888.jpeg" alt="Monty Python"></p><script>
|
|
// Changes the image and link to theme if URL ends with "index.html?theme=dark".
|
|
if (window.location.search.search(/[?&]theme=dark/) !== -1) {
|
|
|
|
var link_to_theme = document.createElement("a")
|
|
link_to_theme.href = "index.html"
|
|
link_to_theme.text = "Switch to light theme"
|
|
document.getElementsByClassName("banner")[0].firstChild.children[4].replaceWith(link_to_theme)
|
|
|
|
var img_dark = document.createElement("img");
|
|
img_dark.src = "web/image_orig_blue6.png";
|
|
img_dark.alt = "Monthy Python";
|
|
if ((window.location.search.search(/[?&]theme=dark2/) !== -1) ||
|
|
(window.location.search.search(/[?&]theme=dark3/) !== -1)) {
|
|
img_dark.style = "width: 910px;";
|
|
} else {
|
|
img_dark.style = "width: 960px;";
|
|
}
|
|
document.getElementsByClassName("banner")[1].firstChild.replaceWith(img_dark);
|
|
}
|
|
</script><br><div><h2 id="toc"><a href="#toc" name="toc">#</a>Contents</h2><pre><code class="hljs bash" style="line-height: 1.327em;"><strong>ToC</strong> = {
|
|
<strong><span class="hljs-string"><span class="hljs-string">'1. Collections'</span></span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'2. Types'</span></span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regular_Exp</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'3. Syntax'</span></span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#imports">Import</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'4. System'</span></span></strong>: [<a href="#exit">Exit</a>, <a href="#print">Print</a>, <a href="#input">Input</a>, <a href="#commandlinearguments">Command_Line_Arguments</a>, <a href="#open">Open</a>, <a href="#paths">Path</a>, <a href="#oscommands">OS_Commands</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'5. Data'</span></span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'6. Advanced'</span></span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#matchstatement">Match_Stmt</a>, <a href="#logging">Logging</a>, <a href="#introspection">Introspection</a>, <a href="#coroutines">Coroutines</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'7. Libraries'</span></span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#consoleapp">Console_App</a>, <a href="#guiapp">GUI</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>],
|
|
<strong><span class="hljs-string"><span class="hljs-string">'8. Multimedia'</span></span></strong>: [<a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#animation">Animation</a>, <a href="#audio">Audio</a>, <a href="#synthesizer">Synthesizer</a>, <a href="#pygame">Pygame</a>, <a href="#pandas">Pandas</a>, <a href="#plotly">Plotly</a>]
|
|
}
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div><h2 id="main"><a href="#main" name="main">#</a>Main</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-comment"># Skips next line if file was imported.</span>
|
|
main() <span class="hljs-comment"># Runs `def main(): ...` function.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs"><el> = <list>[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
|
|
<list> = <list>[<slice>] <span class="hljs-comment"># Or: <list>[from_inclusive : to_exclusive : ±step]</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><list>.append(<el>) <span class="hljs-comment"># Or: <list> += [<el>]</span>
|
|
<list>.extend(<collection>) <span class="hljs-comment"># Or: <list> += <collection></span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><list>.sort() <span class="hljs-comment"># Sorts in ascending order.</span>
|
|
<list>.reverse() <span class="hljs-comment"># Reverses the list in-place.</span>
|
|
<list> = sorted(<collection>) <span class="hljs-comment"># Returns a new sorted list.</span>
|
|
<iter> = reversed(<list>) <span class="hljs-comment"># Returns reversed iterator.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs">sum_of_elements = sum(<collection>)
|
|
elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)]
|
|
sorted_by_second = sorted(<collection>, key=<span class="hljs-keyword">lambda</span> el: el[<span class="hljs-number">1</span>])
|
|
sorted_by_both = sorted(<collection>, key=<span class="hljs-keyword">lambda</span> el: (el[<span class="hljs-number">1</span>], el[<span class="hljs-number">0</span>]))
|
|
flatter_list = list(itertools.chain.from_iterable(<list>))
|
|
product_of_elems = functools.reduce(<span class="hljs-keyword">lambda</span> out, el: out * el, <collection>)
|
|
list_of_chars = list(<str>)
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>For details about sorted(), min() and max() see <a href="#sortable">sortable</a>.</strong></li>
|
|
<li><strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong></li>
|
|
</ul>
|
|
<pre><code class="python language-python hljs"><int> = len(<list>) <span class="hljs-comment"># Returns number of items. Also works on other collections.</span>
|
|
<int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also `if <el> in <coll>: ...`.</span>
|
|
<int> = <list>.index(<el>) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span>
|
|
<el> = <list>.pop() <span class="hljs-comment"># Removes and returns item from the end or at index if passed.</span>
|
|
<list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
|
|
<list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
|
|
<list>.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
|
|
</code></pre>
|
|
<div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs"><view> = <dict>.keys() <span class="hljs-comment"># Coll. of keys that reflects changes.</span>
|
|
<view> = <dict>.values() <span class="hljs-comment"># Coll. of values that reflects changes.</span>
|
|
<view> = <dict>.items() <span class="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs">value = <dict>.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default if key is missing.</span>
|
|
value = <dict>.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span>
|
|
<dict> = collections.defaultdict(<type>) <span class="hljs-comment"># Returns a dict with default value `<type>()`.</span>
|
|
<dict> = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Returns a dict with default value 1.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><dict> = dict(<collection>) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
|
|
<dict> = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dict from two collections.</span>
|
|
<dict> = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dict from collection of keys.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><dict>.update(<dict>) <span class="hljs-comment"># Adds items. Replaces ones with matching keys.</span>
|
|
value = <dict>.pop(key) <span class="hljs-comment"># Removes item or raises KeyError if missing.</span>
|
|
{k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> <dict>.items() <span class="hljs-keyword">if</span> v == value} <span class="hljs-comment"># Returns set of keys that point to the value.</span>
|
|
{k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> <dict>.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">in</span> keys} <span class="hljs-comment"># Filters the dictionary by keys.</span>
|
|
</code></pre>
|
|
<div><h3 id="counter">Counter</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter
|
|
<span class="hljs-meta">>>> </span>counter = Counter([<span class="hljs-string">'blue'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'red'</span>])
|
|
<span class="hljs-meta">>>> </span>counter[<span class="hljs-string">'yellow'</span>] += <span class="hljs-number">1</span>
|
|
<span class="hljs-meta">>>> </span>print(counter)
|
|
Counter({<span class="hljs-string">'blue'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'red'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'yellow'</span>: <span class="hljs-number">1</span>})
|
|
<span class="hljs-meta">>>> </span>counter.most_common()[<span class="hljs-number">0</span>]
|
|
(<span class="hljs-string">'blue'</span>, <span class="hljs-number">3</span>)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs"><set> = set() <span class="hljs-comment"># `{}` returns a dictionary.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><set>.add(<el>) <span class="hljs-comment"># Or: <set> |= {<el>}</span>
|
|
<set>.update(<collection> [, ...]) <span class="hljs-comment"># Or: <set> |= <set></span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><set> = <set>.union(<coll.>) <span class="hljs-comment"># Or: <set> | <set></span>
|
|
<set> = <set>.intersection(<coll.>) <span class="hljs-comment"># Or: <set> & <set></span>
|
|
<set> = <set>.difference(<coll.>) <span class="hljs-comment"># Or: <set> - <set></span>
|
|
<set> = <set>.symmetric_difference(<coll.>) <span class="hljs-comment"># Or: <set> ^ <set></span>
|
|
<bool> = <set>.issubset(<coll.>) <span class="hljs-comment"># Or: <set> <= <set></span>
|
|
<bool> = <set>.issuperset(<coll.>) <span class="hljs-comment"># Or: <set> >= <set></span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><el> = <set>.pop() <span class="hljs-comment"># Raises KeyError if empty.</span>
|
|
<set>.remove(<el>) <span class="hljs-comment"># Raises KeyError if missing.</span>
|
|
<set>.discard(<el>) <span class="hljs-comment"># Doesn't raise an error.</span>
|
|
</code></pre>
|
|
<div><h3 id="frozenset">Frozen Set</h3><ul>
|
|
<li><strong>Is immutable and hashable.</strong></li>
|
|
<li><strong>That means it can be used as a key in a dictionary or as an element in a set.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><frozenset> = frozenset(<collection>)
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h2 id="tuple"><a href="#tuple" name="tuple">#</a>Tuple</h2><p><strong>Tuple is an immutable and hashable list.</strong></p><pre><code class="python language-python hljs"><tuple> = () <span class="hljs-comment"># Empty tuple.</span>
|
|
<tuple> = (<el>,) <span class="hljs-comment"># Or: <el>,</span>
|
|
<tuple> = (<el_1>, <el_2> [, ...]) <span class="hljs-comment"># Or: <el_1>, <el_2> [, ...]</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="namedtuple">Named Tuple</h3><p><strong>Tuple's subclass with named elements.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
|
|
<span class="hljs-meta">>>> </span>Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>)
|
|
<span class="hljs-meta">>>> </span>p = Point(<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>); p
|
|
Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>)
|
|
<span class="hljs-meta">>>> </span>p[<span class="hljs-number">0</span>]
|
|
<span class="hljs-number">1</span>
|
|
<span class="hljs-meta">>>> </span>p.x
|
|
<span class="hljs-number">1</span>
|
|
<span class="hljs-meta">>>> </span>getattr(p, <span class="hljs-string">'y'</span>)
|
|
<span class="hljs-number">2</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h2 id="range"><a href="#range" name="range">#</a>Range</h2><p><strong>Immutable and hashable sequence of integers.</strong></p><pre><code class="python language-python hljs"><range> = range(stop) <span class="hljs-comment"># range(to_exclusive)</span>
|
|
<range> = range(start, stop) <span class="hljs-comment"># range(from_inclusive, to_exclusive)</span>
|
|
<range> = range(start, stop, ±step) <span class="hljs-comment"># range(from_inclusive, to_exclusive, ±step_size)</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>[i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>)]
|
|
[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
|
|
</code></pre>
|
|
<div><h2 id="enumerate"><a href="#enumerate" name="enumerate">#</a>Enumerate</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">for</span> i, el <span class="hljs-keyword">in</span> enumerate(<coll>, start=<span class="hljs-number">0</span>): <span class="hljs-comment"># Returns next element and its index on each pass.</span>
|
|
...
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="iterator"><a href="#iterator" name="iterator">#</a>Iterator</h2><pre><code class="python language-python hljs"><iter> = iter(<collection>) <span class="hljs-comment"># `iter(<iter>)` returns unmodified iterator.</span>
|
|
<iter> = iter(<function>, to_exclusive) <span class="hljs-comment"># A sequence of return values until 'to_exclusive'.</span>
|
|
<el> = next(<iter> [, default]) <span class="hljs-comment"># Raises StopIteration or returns 'default' on end.</span>
|
|
<list> = list(<iter>) <span class="hljs-comment"># Returns a list of iterator's remaining elements.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="itertools">Itertools</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> itertools <span class="hljs-keyword">as</span> it
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><iter> = it.count(start=<span class="hljs-number">0</span>, step=<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns updated value endlessly. Accepts floats.</span>
|
|
<iter> = it.repeat(<el> [, times]) <span class="hljs-comment"># Returns element endlessly or 'times' times.</span>
|
|
<iter> = it.cycle(<collection>) <span class="hljs-comment"># Repeats the sequence endlessly.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><iter> = it.chain(<coll>, <coll> [, ...]) <span class="hljs-comment"># Empties collections in order (figuratively).</span>
|
|
<iter> = it.chain.from_iterable(<coll>) <span class="hljs-comment"># Empties collections inside a collection in order.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><iter> = it.islice(<coll>, to_exclusive) <span class="hljs-comment"># Only returns first 'to_exclusive' elements.</span>
|
|
<iter> = it.islice(<coll>, from_inc, …) <span class="hljs-comment"># `to_exclusive, +step_size`. Indices can be None.</span>
|
|
</code></pre>
|
|
<div><h2 id="generator"><a href="#generator" name="generator">#</a>Generator</h2><ul>
|
|
<li><strong>Any function that contains a yield statement returns a generator.</strong></li>
|
|
<li><strong>Generators and iterators are interchangeable.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">count</span><span class="hljs-params">(start, step)</span>:</span>
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
|
|
<span class="hljs-keyword">yield</span> start
|
|
start += step
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>counter = count(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
|
|
<span class="hljs-meta">>>> </span>next(counter), next(counter), next(counter)
|
|
(<span class="hljs-number">10</span>, <span class="hljs-number">12</span>, <span class="hljs-number">14</span>)
|
|
</code></pre>
|
|
<div><h2 id="type"><a href="#type" name="type">#</a>Type</h2><ul>
|
|
<li><strong>Everything is an object.</strong></li>
|
|
<li><strong>Every object has a type.</strong></li>
|
|
<li><strong>Type and class are synonymous.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><type> = type(<el>) <span class="hljs-comment"># Or: <el>.__class__</span>
|
|
<bool> = isinstance(<el>, <type>) <span class="hljs-comment"># Or: issubclass(type(<el>), <type>)</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>type(<span class="hljs-string">'a'</span>), <span class="hljs-string">'a'</span>.__class__, str
|
|
(<<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'>)
|
|
</span></code></pre>
|
|
<div><h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have built-in names, so they must be imported:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType, ModuleType
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="abstractbaseclasses">Abstract Base Classes</h3><p><strong>Each abstract base class specifies a set of virtual subclasses. These classes are then recognized by isinstance() and issubclass() as subclasses of the ABC, although they are really not. ABC can also manually decide whether or not a specific class is its virtual subclass, usually based on which methods the class has implemented. For instance, Iterable ABC looks for method iter(), while Collection ABC looks for iter(), contains() and len().</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Iterable, Collection, Sequence
|
|
<span class="hljs-meta">>>> </span>isinstance([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], Iterable)
|
|
<span class="hljs-keyword">True</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓
|
|
┃ │ Iterable │ Collection │ Sequence ┃
|
|
┠──────────────────┼────────────┼────────────┼────────────┨
|
|
┃ list, range, str │ ✓ │ ✓ │ ✓ ┃
|
|
┃ dict, set │ ✓ │ ✓ │ ┃
|
|
┃ iter │ ✓ │ │ ┃
|
|
┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> numbers <span class="hljs-keyword">import</span> Number, Complex, Real, Rational, Integral
|
|
<span class="hljs-meta">>>> </span>isinstance(<span class="hljs-number">123</span>, Number)
|
|
<span class="hljs-keyword">True</span>
|
|
</code></pre>
|
|
<pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓
|
|
┃ │ Number │ Complex │ Real │ Rational │ Integral ┃
|
|
┠────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨
|
|
┃ int │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃
|
|
┃ fractions.Fraction │ ✓ │ ✓ │ ✓ │ ✓ │ ┃
|
|
┃ float │ ✓ │ ✓ │ ✓ │ │ ┃
|
|
┃ complex │ ✓ │ ✓ │ │ │ ┃
|
|
┃ decimal.Decimal │ ✓ │ │ │ │ ┃
|
|
┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛
|
|
</code></pre>
|
|
<div><h2 id="string"><a href="#string" name="string">#</a>String</h2><p><strong>Immutable sequence of characters.</strong></p><pre><code class="python language-python hljs"><str> = <str>.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
|
|
<str> = <str>.strip(<span class="hljs-string">'<chars>'</span>) <span class="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><list> = <str>.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span>
|
|
<list> = <str>.split(sep=<span class="hljs-keyword">None</span>, maxsplit=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Splits on 'sep' str at most 'maxsplit' times.</span>
|
|
<list> = <str>.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n.</span>
|
|
<str> = <str>.join(<coll_of_strings>) <span class="hljs-comment"># Joins elements using string as a separator.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><bool> = <sub_str> <span class="hljs-keyword">in</span> <str> <span class="hljs-comment"># Checks if string contains the substring.</span>
|
|
<bool> = <str>.startswith(<sub_str>) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
|
|
<int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
|
|
<int> = <str>.index(<sub_str>) <span class="hljs-comment"># Same, but raises ValueError if there's no match.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><str> = <str>.lower() <span class="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
|
|
<str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
|
|
<str> = <str>.translate(<table>) <span class="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts int to Unicode character.</span>
|
|
<int> = ord(<str>) <span class="hljs-comment"># Converts Unicode character to int.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings like <code class="python hljs"><span class="hljs-string">'Motörhead'</span></code> before comparing them to other strings, because <code class="python hljs"><span class="hljs-string">'ö'</span></code> can be stored as one or two characters.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'NFC'</span></code> converts such characters to a single character, while <code class="python hljs"><span class="hljs-string">'NFD'</span></code> converts them to two.</strong></li>
|
|
</ul>
|
|
<div><h3 id="propertymethods">Property Methods</h3><pre><code class="python language-python hljs"><bool> = <str>.isdecimal() <span class="hljs-comment"># Checks for [0-9]. Also [०-९] and [٠-٩].</span>
|
|
<bool> = <str>.isdigit() <span class="hljs-comment"># Checks for [²³¹…] and isdecimal().</span>
|
|
<bool> = <str>.isnumeric() <span class="hljs-comment"># Checks for [¼½¾…], [零〇一…] and isdigit().</span>
|
|
<bool> = <str>.isalnum() <span class="hljs-comment"># Checks for [a-zA-Z…] and isnumeric().</span>
|
|
<bool> = <str>.isprintable() <span class="hljs-comment"># Checks for [ !#$%…] and isalnum().</span>
|
|
<bool> = <str>.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c-\x1f\x85\xa0…].</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re
|
|
<str> = re.sub(<span class="hljs-string">r'<regex>'</span>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span>
|
|
<list> = re.findall(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
|
|
<list> = re.split(<span class="hljs-string">r'<regex>'</span>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to keep matches.</span>
|
|
<Match> = re.search(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># First occurrence of the pattern or None.</span>
|
|
<Match> = re.match(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
|
|
<iter> = re.finditer(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Raw string literals do not interpret escape sequences, thus enabling us to use regex-specific escape sequences that cause SyntaxWarning in normal string literals (since 3.12).</strong></li>
|
|
<li><strong>Argument 'new' of re.sub() can be a function that accepts Match object and returns a str.</strong></li>
|
|
<li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
|
|
<li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.MULTILINE'</span></code> makes <code class="python hljs"><span class="hljs-string">'^'</span></code> and <code class="python hljs"><span class="hljs-string">'$'</span></code> match the start/end of each line.</strong></li>
|
|
<li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.DOTALL'</span></code> makes <code class="python hljs"><span class="hljs-string">'.'</span></code> also accept the <code class="python hljs"><span class="hljs-string">'\n'</span></code>.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'re.compile(<regex>)'</span></code> returns a Pattern object with methods sub(), findall(), …</strong></li>
|
|
</ul>
|
|
<div><h3 id="matchobject">Match Object</h3><pre><code class="python language-python hljs"><str> = <Match>.group() <span class="hljs-comment"># Returns the whole match. Also group(0).</span>
|
|
<str> = <Match>.group(<span class="hljs-number">1</span>) <span class="hljs-comment"># Returns part inside the first brackets.</span>
|
|
<tuple> = <Match>.groups() <span class="hljs-comment"># Returns all bracketed parts.</span>
|
|
<int> = <Match>.start() <span class="hljs-comment"># Returns start index of the match.</span>
|
|
<int> = <Match>.end() <span class="hljs-comment"># Returns exclusive end index of the match.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="specialsequences">Special Sequences</h3><pre><code class="python language-python hljs"><span class="hljs-string">'\d'</span> == <span class="hljs-string">'[0-9]'</span> <span class="hljs-comment"># Also [०-९…]. Matches a decimal character.</span>
|
|
<span class="hljs-string">'\w'</span> == <span class="hljs-string">'[a-zA-Z0-9_]'</span> <span class="hljs-comment"># Also [ª²³…]. Matches an alphanumeric or _.</span>
|
|
<span class="hljs-string">'\s'</span> == <span class="hljs-string">'[ \t\n\r\f\v]'</span> <span class="hljs-comment"># Also [\x1c-\x1f…]. Matches a whitespace.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless <code class="python hljs"><span class="hljs-string">'flags=re.ASCII'</span></code> argument is used.</strong></li>
|
|
<li><strong>It restricts special sequence matches to <code class="python hljs"><span class="hljs-string">'[\x00-\x7f]'</span></code> (the first 128 characters) and also prevents <code class="python hljs"><span class="hljs-string">'\s'</span></code> from accepting <code class="python hljs"><span class="hljs-string">'[\x1c-\x1f]'</span></code> (the so-called separator characters).</strong></li>
|
|
<li><strong>Use a capital letter for negation (all non-ASCII characters will be matched when used in combination with ASCII flag).</strong></li>
|
|
</ul>
|
|
<div><h2 id="format"><a href="#format" name="format">#</a>Format</h2><pre><code class="python hljs"><str> = <span class="hljs-string">f'<span class="hljs-subst">{<el_1>}</span>, <span class="hljs-subst">{<el_2>}</span>'</span> <span class="hljs-comment"># Curly brackets can also contain expressions.</span>
|
|
<str> = <span class="hljs-string">'{}, {}'</span>.format(<el_1>, <el_2>) <span class="hljs-comment"># Or: '{0}, {a}'.format(<el_1>, a=<el_2>)</span>
|
|
<str> = <span class="hljs-string">'%s, %s'</span> % (<el_1>, <el_2>) <span class="hljs-comment"># Redundant and inferior C-style formatting.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="example">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>Person = collections.namedtuple(<span class="hljs-string">'Person'</span>, <span class="hljs-string">'name height'</span>)
|
|
<span class="hljs-meta">>>> </span>person = Person(<span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)
|
|
<span class="hljs-meta">>>> </span><span class="hljs-string">f'<span class="hljs-subst">{person.name}</span> is <span class="hljs-subst">{person.height / <span class="hljs-number">100</span>}</span> meters tall.'</span>
|
|
<span class="hljs-string">'Jean-Luc is 1.87 meters tall.'</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="generaloptions">General Options</h3><pre><code class="python language-python hljs">{<el>:<<span class="hljs-number">10</span>} <span class="hljs-comment"># '<el> '</span>
|
|
{<el>:^<span class="hljs-number">10</span>} <span class="hljs-comment"># ' <el> '</span>
|
|
{<el>:><span class="hljs-number">10</span>} <span class="hljs-comment"># ' <el>'</span>
|
|
{<el>:.<<span class="hljs-number">10</span>} <span class="hljs-comment"># '<el>......'</span>
|
|
{<el>:<span class="hljs-number">0</span>} <span class="hljs-comment"># '<el>'</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Objects are rendered using <code class="python hljs"><span class="hljs-string">'format(<el>, <options>)'</span></code>.</strong></li>
|
|
<li><strong>Options can be generated dynamically: <code class="python hljs"><span class="hljs-string">f'<span class="hljs-subst">{<el>:{<str/int>}</span>[…]}'</span></code>.</strong></li>
|
|
<li><strong>Adding <code class="python hljs"><span class="hljs-string">'='</span></code> to the expression prepends it to the output: <code class="python hljs"><span class="hljs-string">f'<span class="hljs-subst">{<span class="hljs-number">1</span>+<span class="hljs-number">1</span>=}</span>'</span></code> returns <code class="python hljs"><span class="hljs-string">'1+1=2'</span></code>.</strong></li>
|
|
<li><strong>Adding <code class="python hljs"><span class="hljs-string">'!r'</span></code> to the expression converts object to string by calling its <a href="#class">repr()</a> method.</strong></li>
|
|
</ul>
|
|
<div><h3 id="strings">Strings</h3><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># 'abcde '</span>
|
|
{<span class="hljs-string">'abcde'</span>:<span class="hljs-number">10.3</span>} <span class="hljs-comment"># 'abc '</span>
|
|
{<span class="hljs-string">'abcde'</span>:<span class="hljs-number">.3</span>} <span class="hljs-comment"># 'abc'</span>
|
|
{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># ' 123456'</span>
|
|
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span>
|
|
{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span>
|
|
{<span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span>
|
|
{<span class="hljs-number">123456</span>:=+<span class="hljs-number">10</span>} <span class="hljs-comment"># '+ 123456'</span>
|
|
{<span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span>
|
|
{<span class="hljs-number">-123456</span>: } <span class="hljs-comment"># '-123456'</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="floats">Floats</h3><pre><code class="python language-python hljs">{<span class="hljs-number">1.23456</span>:<span class="hljs-number">10.3</span>} <span class="hljs-comment"># ' 1.23'</span>
|
|
{<span class="hljs-number">1.23456</span>:<span class="hljs-number">10.3</span>f} <span class="hljs-comment"># ' 1.235'</span>
|
|
{<span class="hljs-number">1.23456</span>:<span class="hljs-number">10.3</span>e} <span class="hljs-comment"># ' 1.235e+00'</span>
|
|
{<span class="hljs-number">1.23456</span>:<span class="hljs-number">10.3</span>%} <span class="hljs-comment"># ' 123.456%'</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="comparisonofpresentationtypes">Comparison of presentation types:</h4><pre><code class="text language-text">┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓
|
|
┃ │ {<float>} │ {<float>:f} │ {<float>:e} │ {<float>:%} ┃
|
|
┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨
|
|
┃ 0.000056789 │ '5.6789e-05' │ '0.000057' │ '5.678900e-05' │ '0.005679%' ┃
|
|
┃ 0.00056789 │ '0.00056789' │ '0.000568' │ '5.678900e-04' │ '0.056789%' ┃
|
|
┃ 0.0056789 │ '0.0056789' │ '0.005679' │ '5.678900e-03' │ '0.567890%' ┃
|
|
┃ 0.056789 │ '0.056789' │ '0.056789' │ '5.678900e-02' │ '5.678900%' ┃
|
|
┃ 0.56789 │ '0.56789' │ '0.567890' │ '5.678900e-01' │ '56.789000%' ┃
|
|
┃ 5.6789 │ '5.6789' │ '5.678900' │ '5.678900e+00' │ '567.890000%' ┃
|
|
┃ 56.789 │ '56.789' │ '56.789000' │ '5.678900e+01' │ '5678.900000%' ┃
|
|
┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛
|
|
|
|
┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓
|
|
┃ │ {<float>:.2} │ {<float>:.2f} │ {<float>:.2e} │ {<float>:.2%} ┃
|
|
┠──────────────┼────────────────┼────────────────┼────────────────┼────────────────┨
|
|
┃ 0.000056789 │ '5.7e-05' │ '0.00' │ '5.68e-05' │ '0.01%' ┃
|
|
┃ 0.00056789 │ '0.00057' │ '0.00' │ '5.68e-04' │ '0.06%' ┃
|
|
┃ 0.0056789 │ '0.0057' │ '0.01' │ '5.68e-03' │ '0.57%' ┃
|
|
┃ 0.056789 │ '0.057' │ '0.06' │ '5.68e-02' │ '5.68%' ┃
|
|
┃ 0.56789 │ '0.57' │ '0.57' │ '5.68e-01' │ '56.79%' ┃
|
|
┃ 5.6789 │ '5.7' │ '5.68' │ '5.68e+00' │ '567.89%' ┃
|
|
┃ 56.789 │ '5.7e+01' │ '56.79' │ '5.68e+01' │ '5678.90%' ┃
|
|
┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┛
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'{<float>:g}'</span></code> is <code class="python hljs"><span class="hljs-string">'{<float>:.6}'</span></code> with stripped zeros, exponent starting at <code class="python hljs"><span class="hljs-string">'1e+06'</span></code>.</strong></li>
|
|
<li><strong>When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes <code class="python hljs"><span class="hljs-string">'{6.5:.0f}'</span></code> a <code class="python hljs"><span class="hljs-string">'6'</span></code> and <code class="python hljs"><span class="hljs-string">'{7.5:.0f}'</span></code> an <code class="python hljs"><span class="hljs-string">'8'</span></code>.</strong></li>
|
|
<li><strong>This rule only effects numbers that can be represented exactly by a float (<code class="python hljs"><span class="hljs-number">.5</span></code>, <code class="python hljs"><span class="hljs-number">.25</span></code>, …).</strong></li>
|
|
</ul>
|
|
<div><h3 id="ints">Ints</h3><pre><code class="python language-python hljs">{<span class="hljs-number">90</span>:c} <span class="hljs-comment"># 'Z'. Unicode character with value 90.</span>
|
|
{<span class="hljs-number">90</span>:b} <span class="hljs-comment"># '1011010'. Number 90 in binary.</span>
|
|
{<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'. Number 90 in uppercase hexadecimal.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs"><int> = int(<float/str/bool>) <span class="hljs-comment"># Or: math.floor(<float>)</span>
|
|
<float> = float(<int/str/bool>) <span class="hljs-comment"># Or: <int/float>e±<int></span>
|
|
<complex> = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: <int/float> ± <int/float>j</span>
|
|
<Fraction> = fractions.Fraction(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>) <span class="hljs-comment"># Or: Fraction(numerator=0, denominator=1)</span>
|
|
<Decimal> = decimal.Decimal(<str/int>) <span class="hljs-comment"># Or: Decimal((sign, digits, exponent))</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'int(<str>)'</span></code> and <code class="python hljs"><span class="hljs-string">'float(<str>)'</span></code> raise ValueError on malformed strings.</strong></li>
|
|
<li><strong>Decimal numbers are stored exactly, unlike most floats where <code class="python hljs"><span class="hljs-string">'1.1 + 2.2 != 3.3'</span></code>.</strong></li>
|
|
<li><strong>Floats can be compared with: <code class="python hljs"><span class="hljs-string">'math.isclose(<float>, <float>)'</span></code>.</strong></li>
|
|
<li><strong>Precision of decimal operations is set with: <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = <int>'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs"><num> = pow(<num>, <num>) <span class="hljs-comment"># Or: <number> ** <number></span>
|
|
<num> = abs(<num>) <span class="hljs-comment"># <float> = abs(<complex>)</span>
|
|
<num> = round(<num> [, ±ndigits]) <span class="hljs-comment"># `round(126, -1) == 130`</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="math">Math</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> e, pi, inf, nan, isinf, isnan <span class="hljs-comment"># `<el> == nan` is always False.</span>
|
|
<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> sin, cos, tan, asin, acos, atan <span class="hljs-comment"># Also: degrees, radians.</span>
|
|
<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> log, log10, log2 <span class="hljs-comment"># Log can accept base as second arg.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="statistics">Statistics</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> statistics <span class="hljs-keyword">import</span> mean, median, variance <span class="hljs-comment"># Also: stdev, quantiles, groupby.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="random">Random</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random, randint, choice <span class="hljs-comment"># Also: shuffle, gauss, triangular, seed.</span>
|
|
<float> = random() <span class="hljs-comment"># A float inside [0, 1).</span>
|
|
<int> = randint(from_inc, to_inc) <span class="hljs-comment"># An int inside [from_inc, to_inc].</span>
|
|
<el> = choice(<sequence>) <span class="hljs-comment"># Keeps the sequence intact.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="binhex">Bin, Hex</h3><pre><code class="python language-python hljs"><int> = ±<span class="hljs-number">0b</span><bin> <span class="hljs-comment"># Or: ±0x<hex></span>
|
|
<int> = int(<span class="hljs-string">'±<bin>'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Or: int('±<hex>', 16)</span>
|
|
<int> = int(<span class="hljs-string">'±0b<bin>'</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Or: int('±0x<hex>', 0)</span>
|
|
<str> = bin(<int>) <span class="hljs-comment"># Returns '[-]0b<bin>'. Also hex().</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="bitwiseoperators">Bitwise Operators</h3><pre><code class="python language-python hljs"><int> = <int> & <int> <span class="hljs-comment"># And (0b1100 & 0b1010 == 0b1000).</span>
|
|
<int> = <int> | <int> <span class="hljs-comment"># Or (0b1100 | 0b1010 == 0b1110).</span>
|
|
<int> = <int> ^ <int> <span class="hljs-comment"># Xor (0b1100 ^ 0b1010 == 0b0110).</span>
|
|
<int> = <int> << n_bits <span class="hljs-comment"># Left shift. Use >> for right.</span>
|
|
<int> = ~<int> <span class="hljs-comment"># Not. Also -<int> - 1.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="combinatorics"><a href="#combinatorics" name="combinatorics">#</a>Combinatorics</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> itertools <span class="hljs-keyword">as</span> it
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>list(it.product([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>], repeat=<span class="hljs-number">3</span>))
|
|
[(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>),
|
|
(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>)]
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>list(it.product(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'abc'</span>)) <span class="hljs-comment"># a b c</span>
|
|
[(<span class="hljs-string">'a'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># a x x x</span>
|
|
(<span class="hljs-string">'b'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'b'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># b x x x</span>
|
|
(<span class="hljs-string">'c'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'c'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'c'</span>, <span class="hljs-string">'c'</span>)] <span class="hljs-comment"># c x x x</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>list(it.combinations(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>)) <span class="hljs-comment"># a b c</span>
|
|
[(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># a . x x</span>
|
|
(<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>)] <span class="hljs-comment"># b . . x</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>list(it.combinations_with_replacement(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>)) <span class="hljs-comment"># a b c</span>
|
|
[(<span class="hljs-string">'a'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># a x x x</span>
|
|
(<span class="hljs-string">'b'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># b . x x</span>
|
|
(<span class="hljs-string">'c'</span>, <span class="hljs-string">'c'</span>)] <span class="hljs-comment"># c . . x</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>list(it.permutations(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>)) <span class="hljs-comment"># a b c</span>
|
|
[(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>), (<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># a . x x</span>
|
|
(<span class="hljs-string">'b'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>), <span class="hljs-comment"># b x . x</span>
|
|
(<span class="hljs-string">'c'</span>, <span class="hljs-string">'a'</span>), (<span class="hljs-string">'c'</span>, <span class="hljs-string">'b'</span>)] <span class="hljs-comment"># c x x .</span>
|
|
</code></pre>
|
|
<div><h2 id="datetime"><a href="#datetime" name="datetime">#</a>Datetime</h2><p><strong>Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install python-dateutil</span>
|
|
<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> date, time, datetime, timedelta, timezone
|
|
<span class="hljs-keyword">from</span> dateutil.tz <span class="hljs-keyword">import</span> tzlocal, gettz
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python apache hljs"><D> = date(year, month, day) <span class="hljs-comment"># Only accepts valid dates from 1 to 9999 AD.</span>
|
|
<T> = time(hour=<span class="hljs-number">0</span>, minute=<span class="hljs-number">0</span>, second=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also: `microsecond=0, tzinfo=None, fold=0`.</span>
|
|
<DT> = datetime(year, month, day, hour=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also: `minute=0, second=0, microsecond=0, …`.</span>
|
|
<TD> = timedelta(weeks=<span class="hljs-number">0</span>, days=<span class="hljs-number">0</span>, hours=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also: `minutes=0, seconds=0, microseconds=0`.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Aware <code class="apache hljs"><span class="hljs-section"><a></span></code> time and datetime objects have defined timezone, while naive <code class="apache hljs"><span class="hljs-section"><n></span></code> don't. If object is naive, it is presumed to be in the system's timezone!</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
|
|
<li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
|
|
</ul>
|
|
<div><h3 id="now">Now</h3><pre><code class="python language-python hljs"><D/DTn> = D/DT.today() <span class="hljs-comment"># Current local date or naive DT. Also DT.now().</span>
|
|
<DTa> = DT.now(<tzinfo>) <span class="hljs-comment"># Aware DT from current time in passed timezone.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>To extract time use <code class="python hljs"><span class="hljs-string">'<DTn>.time()'</span></code>, <code class="python hljs"><span class="hljs-string">'<DTa>.time()'</span></code> or <code class="python hljs"><span class="hljs-string">'<DTa>.timetz()'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs"><tzinfo> = timezone.utc <span class="hljs-comment"># London without daylight saving time (DST).</span>
|
|
<tzinfo> = timezone(<timedelta>) <span class="hljs-comment"># Timezone with fixed offset from UTC.</span>
|
|
<tzinfo> = tzlocal() <span class="hljs-comment"># Local tz with dynamic offset. Also gettz().</span>
|
|
<tzinfo> = gettz(<span class="hljs-string">'<Continent>/<City>'</span>) <span class="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
|
|
<DTa> = <DT>.astimezone([<tzinfo>]) <span class="hljs-comment"># Converts DT to the passed or local fixed zone.</span>
|
|
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <span class="hljs-comment"># Changes object's timezone without conversion.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Timezones returned by tzlocal(), gettz(), and implicit local timezone of naive objects have offsets that vary through time due to DST and historical changes of the zone's base offset.</strong></li>
|
|
<li><strong>Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows. It doesn't return local tz if arg. is omitted.</strong></li>
|
|
</ul>
|
|
<div><h3 id="encode">Encode</h3><pre><code class="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<str>) <span class="hljs-comment"># Object from ISO string. Raises ValueError.</span>
|
|
<DT> = DT.strptime(<str>, <span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Datetime from str, according to format.</span>
|
|
<D/DTn> = D/DT.fromordinal(<int>) <span class="hljs-comment"># D/DT from days since the Gregorian NYE 1.</span>
|
|
<DTn> = DT.fromtimestamp(<float>) <span class="hljs-comment"># Local naive DT from seconds since the Epoch.</span>
|
|
<DTa> = DT.fromtimestamp(<float>, <tz>) <span class="hljs-comment"># Aware datetime from seconds since the Epoch.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>ISO strings come in following forms: <code class="python hljs"><span class="hljs-string">'YYYY-MM-DD'</span></code>, <code class="python hljs"><span class="hljs-string">'HH:MM:SS.mmmuuu[±HH:MM]'</span></code>, or both separated by an arbitrary character. All parts following the hours are optional.</strong></li>
|
|
<li><strong>Python uses the Unix Epoch: <code class="python hljs"><span class="hljs-string">'1970-01-01 00:00 UTC'</span></code>, <code class="python hljs"><span class="hljs-string">'1970-01-01 01:00 CET'</span></code>, …</strong></li>
|
|
</ul>
|
|
<div><h3 id="decode">Decode</h3><pre><code class="python language-python hljs"><str> = <D/T/DT>.isoformat(sep=<span class="hljs-string">'T'</span>) <span class="hljs-comment"># Also `timespec='auto/hours/minutes/seconds/…'`.</span>
|
|
<str> = <D/T/DT>.strftime(<span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Custom string representation of the object.</span>
|
|
<int> = <D/DT>.toordinal() <span class="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span>
|
|
<float> = <DTn>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from local naive DT.</span>
|
|
<float> = <DTa>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from aware datetime.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="format-1">Format</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>dt = datetime.strptime(<span class="hljs-string">'2025-08-14 23:39:00.00 +0200'</span>, <span class="hljs-string">'%Y-%m-%d %H:%M:%S.%f %z'</span>)
|
|
<span class="hljs-meta">>>> </span>dt.strftime(<span class="hljs-string">"%dth of %B '%y (%a), %I:%M %p %Z"</span>)
|
|
<span class="hljs-string">"14th of August '25 (Thu), 11:39 PM UTC+02:00"</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'%z'</span></code> accepts <code class="python hljs"><span class="hljs-string">'±HH[:]MM'</span></code> and returns <code class="python hljs"><span class="hljs-string">'±HHMM'</span></code> or empty string if datetime is naive.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'%Z'</span></code> accepts <code class="python hljs"><span class="hljs-string">'UTC/GMT'</span></code> and local timezone's code and returns timezone's name, <code class="python hljs"><span class="hljs-string">'UTC[±HH:MM]'</span></code> if timezone is nameless, or an empty string if datetime is naive.</strong></li>
|
|
</ul>
|
|
<div><h3 id="arithmetics">Arithmetics</h3><pre><code class="python language-python apache hljs"><bool> = <D/T/DTn> > <D/T/DTn> <span class="hljs-comment"># Ignores time jumps (fold attribute). Also ==.</span>
|
|
<bool> = <DTa> > <DTa> <span class="hljs-comment"># Ignores jumps if they share tz object. Broken ==.</span>
|
|
<TD> = <D/DTn> - <D/DTn> <span class="hljs-comment"># Ignores jumps. Convert to UTC for actual delta.</span>
|
|
<TD> = <DTa> - <DTa> <span class="hljs-comment"># Ignores jumps if they share tzinfo object.</span>
|
|
<D/DT> = <D/DT> ± <TD> <span class="hljs-comment"># Returned datetime can fall into missing hour.</span>
|
|
<TD> = <TD> * <float> <span class="hljs-comment"># Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.</span>
|
|
<float> = <TD> / <TD> <span class="hljs-comment"># How many hours/weeks/years are in TD. Also //.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="arguments"><a href="#arguments" name="arguments">#</a>Arguments</h2><div><h3 id="insidefunctioncall">Inside Function Call</h3><pre><code class="python language-python hljs">func(<positional_args>) <span class="hljs-comment"># func(0, 0)</span>
|
|
func(<keyword_args>) <span class="hljs-comment"># func(x=0, y=0)</span>
|
|
func(<positional_args>, <keyword_args>) <span class="hljs-comment"># func(0, y=0)</span>
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h3 id="insidefunctiondefinition">Inside Function Definition</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(<nondefault_args>)</span>:</span> ... <span class="hljs-comment"># def func(x, y): ...</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(<default_args>)</span>:</span> ... <span class="hljs-comment"># def func(x=0, y=0): ...</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(<nondefault_args>, <default_args>)</span>:</span> ... <span class="hljs-comment"># def func(x, y=0): ...</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Default values are evaluated when function is first encountered in the scope.</strong></li>
|
|
<li><strong>Any mutation of a mutable default value will persist between invocations!</strong></li>
|
|
</ul>
|
|
<div><h2 id="splatoperator"><a href="#splatoperator" name="splatoperator">#</a>Splat Operator</h2><div><h3 id="insidefunctioncall-1">Inside Function Call</h3><p><strong>Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.</strong></p><pre><code class="python language-python hljs">args = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
|
|
kwargs = {<span class="hljs-string">'x'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'y'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'z'</span>: <span class="hljs-number">5</span>}
|
|
func(*args, **kwargs)
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
<div><h4 id="isthesameas">Is the same as:</h4><pre><code class="python language-python hljs">func(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, x=<span class="hljs-number">3</span>, y=<span class="hljs-number">4</span>, z=<span class="hljs-number">5</span>)
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="insidefunctiondefinition-1">Inside Function Definition</h3><p><strong>Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span><span class="hljs-params">(*a)</span>:</span>
|
|
<span class="hljs-keyword">return</span> sum(a)
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>add(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
<span class="hljs-number">6</span>
|
|
</code></pre>
|
|
<div><h4 id="legalargumentcombinations">Legal argument combinations:</h4><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*args)</span>:</span> ... <span class="hljs-comment"># f(1, 2, 3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, *args)</span>:</span> ... <span class="hljs-comment"># f(1, 2, 3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*args, z)</span>:</span> ... <span class="hljs-comment"># f(1, 2, z=3)</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(**kwargs)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, **kwargs)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3)</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*args, **kwargs)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, *args, **kwargs)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*args, y, **kwargs)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3)</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*, x, y, z)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, *, y, z)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, y, *, z)</span>:</span> ... <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)</span>
|
|
</code></pre>
|
|
<div><h3 id="otheruses">Other Uses</h3><pre><code class="python language-python hljs"><list> = [*<coll.> [, ...]] <span class="hljs-comment"># Or: list(<collection>) [+ ...]</span>
|
|
<tuple> = (*<coll.>, [...]) <span class="hljs-comment"># Or: tuple(<collection>) [+ ...]</span>
|
|
<set> = {*<coll.> [, ...]} <span class="hljs-comment"># Or: set(<collection>) [| ...]</span>
|
|
<dict> = {**<dict> [, ...]} <span class="hljs-comment"># Or: <dict> | ... (since 3.9)</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs">head, *body, tail = <coll.> <span class="hljs-comment"># Head or tail can be omitted.</span>
|
|
</code></pre>
|
|
<div><h2 id="inline"><a href="#inline" name="inline">#</a>Inline</h2><div><h3 id="lambda">Lambda</h3><pre><code class="python language-python hljs"><func> = <span class="hljs-keyword">lambda</span>: <return_value> <span class="hljs-comment"># A single statement function.</span>
|
|
<func> = <span class="hljs-keyword">lambda</span> <arg_1>, <arg_2>: <return_value> <span class="hljs-comment"># Also allows default arguments.</span>
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h3 id="comprehensions">Comprehensions</h3><pre><code class="python language-python hljs"><list> = [i+<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)] <span class="hljs-comment"># Or: [1, 2, ..., 10]</span>
|
|
<iter> = (i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) <span class="hljs-keyword">if</span> i > <span class="hljs-number">5</span>) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
|
|
<set> = {i+<span class="hljs-number">5</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {5, 6, ..., 14}</span>
|
|
<dict> = {i: i*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)} <span class="hljs-comment"># Or: {0: 0, 1: 2, ..., 9: 18}</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>[l+r <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-string">'abc'</span> <span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> <span class="hljs-string">'abc'</span>] <span class="hljs-comment"># Inner loop is on the right side.</span>
|
|
[<span class="hljs-string">'aa'</span>, <span class="hljs-string">'ab'</span>, <span class="hljs-string">'ac'</span>, ..., <span class="hljs-string">'cc'</span>]
|
|
</code></pre>
|
|
<div><h3 id="mapfilterreduce">Map, Filter, Reduce</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><iter> = map(<span class="hljs-keyword">lambda</span> x: x + <span class="hljs-number">1</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([1, 2, ..., 10])</span>
|
|
<iter> = filter(<span class="hljs-keyword">lambda</span> x: x > <span class="hljs-number">5</span>, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: iter([6, 7, 8, 9])</span>
|
|
<obj> = reduce(<span class="hljs-keyword">lambda</span> out, x: out + x, range(<span class="hljs-number">10</span>)) <span class="hljs-comment"># Or: 45</span>
|
|
</code></pre>
|
|
<div><h3 id="anyall">Any, All</h3><pre><code class="python language-python hljs"><bool> = any(<collection>) <span class="hljs-comment"># Is `bool(<el>)` True for any el?</span>
|
|
<bool> = all(<collection>) <span class="hljs-comment"># True for all? Also True if empty.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="conditionalexpression">Conditional Expression</h3><pre><code class="python language-python hljs"><obj> = <exp> <span class="hljs-keyword">if</span> <condition> <span class="hljs-keyword">else</span> <exp> <span class="hljs-comment"># Only one expression is evaluated.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>[a <span class="hljs-keyword">if</span> a <span class="hljs-keyword">else</span> <span class="hljs-string">'zero'</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)] <span class="hljs-comment"># `any([0, '', [], None]) == False`</span>
|
|
[<span class="hljs-string">'zero'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
|
|
</code></pre>
|
|
<div><h3 id="namedtupleenumdataclass">Named Tuple, Enum, Dataclass</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
|
|
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Creates a tuple's subclass.</span>
|
|
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Returns its instance.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
|
|
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'N E S W'</span>) <span class="hljs-comment"># Creates an enum.</span>
|
|
direction = Direction.N <span class="hljs-comment"># Returns its member.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
|
|
Player = make_dataclass(<span class="hljs-string">'Player'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) <span class="hljs-comment"># Creates a class.</span>
|
|
player = Player(point, direction) <span class="hljs-comment"># Returns its instance.</span>
|
|
</code></pre>
|
|
<div><h2 id="imports"><a href="#imports" name="imports">#</a>Imports</h2><p><strong>Mechanism that makes code in one file available to another file.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> <module> <span class="hljs-comment"># Imports a built-in or '<module>.py'.</span>
|
|
<span class="hljs-keyword">import</span> <package> <span class="hljs-comment"># Imports a built-in or '<package>/__init__.py'.</span>
|
|
<span class="hljs-keyword">import</span> <package>.<module> <span class="hljs-comment"># Imports a built-in or '<package>/<module>.py'.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Package is a collection of modules, but it can also define its own objects.</strong></li>
|
|
<li><strong>On a filesystem this corresponds to a directory of Python files with an optional init script.</strong></li>
|
|
<li><strong>Running <code class="python hljs"><span class="hljs-string">'import <package>'</span></code> does not automatically provide access to the package's modules unless they are explicitly imported in its init script.</strong></li>
|
|
<li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li>
|
|
<li><strong>For relative imports use <code class="python hljs"><span class="hljs-string">'from .[…][<pkg/module>[.…]] import <obj>'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_multiplier</span><span class="hljs-params">(a)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(b)</span>:</span>
|
|
<span class="hljs-keyword">return</span> a * b
|
|
<span class="hljs-keyword">return</span> out
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>multiply_by_3 = get_multiplier(<span class="hljs-number">3</span>)
|
|
<span class="hljs-meta">>>> </span>multiply_by_3(<span class="hljs-number">10</span>)
|
|
<span class="hljs-number">30</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Any value that is referenced from within multiple nested functions gets shared.</strong></li>
|
|
</ul>
|
|
<div><h3 id="partial">Partial</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
|
|
<function> = partial(<function> [, <arg_1> [, ...]])
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">multiply</span><span class="hljs-params">(a, b)</span>:</span>
|
|
<span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> a * b
|
|
<span class="hljs-meta">>>> </span>multiply_by_3 = partial(multiply, <span class="hljs-number">3</span>)
|
|
<span class="hljs-meta">>>> </span>multiply_by_3(<span class="hljs-number">10</span>)
|
|
<span class="hljs-number">30</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Partial is also useful in cases when function needs to be passed as an argument because it enables us to set its arguments beforehand.</strong></li>
|
|
<li><strong>A few examples being: <code class="python hljs"><span class="hljs-string">'defaultdict(<func>)'</span></code>, <code class="python hljs"><span class="hljs-string">'iter(<func>, to_exc)'</span></code> and dataclass's <code class="python hljs"><span class="hljs-string">'field(default_factory=<func>)'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="nonlocal">Non-Local</h3><p><strong>If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_counter</span><span class="hljs-params">()</span>:</span>
|
|
i = <span class="hljs-number">0</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">()</span>:</span>
|
|
<span class="hljs-keyword">nonlocal</span> i
|
|
i += <span class="hljs-number">1</span>
|
|
<span class="hljs-keyword">return</span> i
|
|
<span class="hljs-keyword">return</span> out
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>counter = get_counter()
|
|
<span class="hljs-meta">>>> </span>counter(), counter(), counter()
|
|
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
</code></pre>
|
|
<div class="pagebreak"></div><div><h2 id="decorator"><a href="#decorator" name="decorator">#</a>Decorator</h2><ul>
|
|
<li><strong>A decorator takes a function, adds some functionality and returns it.</strong></li>
|
|
<li><strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-meta">@decorator_name</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function_that_gets_passed_to_decorator</span><span class="hljs-params">()</span>:</span>
|
|
...
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="debuggerexample">Debugger Example</h3><p><strong>Decorator that prints function's name every time the function is called.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> wraps
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">debug</span><span class="hljs-params">(func)</span>:</span>
|
|
<span class="hljs-meta"> @wraps(func)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(*args, **kwargs)</span>:</span>
|
|
print(func.__name__)
|
|
<span class="hljs-keyword">return</span> func(*args, **kwargs)
|
|
<span class="hljs-keyword">return</span> out
|
|
|
|
<span class="hljs-meta">@debug</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span><span class="hljs-params">(x, y)</span>:</span>
|
|
<span class="hljs-keyword">return</span> x + y
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Wraps is a helper decorator that copies the metadata of the passed function (func) to the function it is wrapping (out).</strong></li>
|
|
<li><strong>Without it, <code class="python hljs"><span class="hljs-string">'add.__name__'</span></code> would return <code class="python hljs"><span class="hljs-string">'out'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="lrucache">LRU Cache</h3><p><strong>Decorator that caches function's return values. All function's arguments must be hashable.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> lru_cache
|
|
|
|
<span class="hljs-meta">@lru_cache(maxsize=None)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(n)</span>:</span>
|
|
<span class="hljs-keyword">return</span> n <span class="hljs-keyword">if</span> n < <span class="hljs-number">2</span> <span class="hljs-keyword">else</span> fib(n-<span class="hljs-number">2</span>) + fib(n-<span class="hljs-number">1</span>)
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Default size of the cache is 128 values. Passing <code class="python hljs"><span class="hljs-string">'maxsize=None'</span></code> makes it unbounded.</strong></li>
|
|
<li><strong>CPython interpreter limits recursion depth to 3000 by default. To increase it use <code class="python hljs"><span class="hljs-string">'sys.setrecursionlimit(<int>)'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="parametrizeddecorator">Parametrized Decorator</h3><p><strong>A decorator that accepts arguments and returns a normal decorator that accepts a function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> wraps
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">debug</span><span class="hljs-params">(print_result=<span class="hljs-keyword">False</span>)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">decorator</span><span class="hljs-params">(func)</span>:</span>
|
|
<span class="hljs-meta"> @wraps(func)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(*args, **kwargs)</span>:</span>
|
|
result = func(*args, **kwargs)
|
|
print(func.__name__, result <span class="hljs-keyword">if</span> print_result <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>)
|
|
<span class="hljs-keyword">return</span> result
|
|
<span class="hljs-keyword">return</span> out
|
|
<span class="hljs-keyword">return</span> decorator
|
|
|
|
<span class="hljs-meta">@debug(print_result=True)</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span><span class="hljs-params">(x, y)</span>:</span>
|
|
<span class="hljs-keyword">return</span> x + y
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Using only <code class="python hljs"><span class="hljs-string">'@debug'</span></code> to decorate the add() function would not work here, because debug would then receive the add() function as a 'print_result' argument. Decorators can however manually check if the argument they received is a function and act accordingly.</strong></li>
|
|
</ul>
|
|
<div><h2 id="class"><a href="#class" name="class">#</a>Class</h2><p><strong>A template for creating user-defined objects.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> str(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
|
|
class_name = self.__class__.__name__
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">f'<span class="hljs-subst">{class_name}</span>(<span class="hljs-subst">{self.a!r}</span>)'</span>
|
|
|
|
<span class="hljs-meta"> @classmethod</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_class_name</span><span class="hljs-params">(cls)</span>:</span>
|
|
<span class="hljs-keyword">return</span> cls.__name__
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Return value of str() should be readable and of repr() unambiguous.</strong></li>
|
|
<li><strong>If only repr() is defined, it will also be used for str().</strong></li>
|
|
<li><strong>Methods decorated with <code class="python hljs"><span class="hljs-string">'@staticmethod'</span></code> do not receive 'self' nor 'cls' as their first arg.</strong></li>
|
|
</ul>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>obj = MyClass(<span class="hljs-number">1</span>)
|
|
<span class="hljs-meta">>>> </span>obj.a, str(obj), repr(obj)
|
|
(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'MyClass(1)'</span>)
|
|
</code></pre>
|
|
<div><h4 id="expressionsthatcallthestrmethod">Expressions that call the str() method:</h4><pre><code class="python language-python hljs">print(<obj>)
|
|
<span class="hljs-string">f'<span class="hljs-subst">{<obj>}</span>'</span>
|
|
logging.warning(<obj>)
|
|
csv.writer(<file>).writerow([<obj>])
|
|
<span class="hljs-keyword">raise</span> Exception(<obj>)
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="expressionsthatcallthereprmethod">Expressions that call the repr() method:</h4><pre><code class="python language-python hljs">print/str/repr([<obj>])
|
|
print/str/repr({<obj>: <obj>})
|
|
<span class="hljs-string">f'<span class="hljs-subst">{<obj>!r}</span>'</span>
|
|
Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span class="hljs-string">'a'</span>]); print/str/repr(Z(<obj>))
|
|
<span class="hljs-meta">>>> </span><obj>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="inheritance">Inheritance</h3><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
|
|
self.name = name
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span><span class="hljs-params">(Person)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, staff_num)</span>:</span>
|
|
super().__init__(name)
|
|
self.staff_num = staff_num
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="multipleinheritance">Multiple inheritance:</h4><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span>:</span> <span class="hljs-keyword">pass</span>
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span>:</span> <span class="hljs-keyword">pass</span>
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">C</span><span class="hljs-params">(A, B)</span>:</span> <span class="hljs-keyword">pass</span>
|
|
</code></pre></div>
|
|
|
|
<p><strong>MRO determines the order in which parent classes are traversed when searching for a method or an attribute:</strong></p>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>C.mro()
|
|
[<<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">C</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">A</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">B</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">object</span>'>]
|
|
</span></code></pre>
|
|
<div><h3 id="typeannotations">Type Annotations</h3><ul>
|
|
<li><strong>They add type hints to variables, arguments and functions (<code class="python hljs"><span class="hljs-string">'def f() -> <type>:'</span></code>).</strong></li>
|
|
<li><strong>Hints are used by type checkers like <a href="https://pypi.org/project/mypy/">mypy</a>, data validation libraries such as <a href="https://pypi.org/project/pydantic/">Pydantic</a> and lately also by <a href="https://pypi.org/project/Cython/">Cython</a> compiler. However, they are not enforced by CPython interpreter.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> abc
|
|
|
|
<name>: <type> [| ...] [= <obj>] <span class="hljs-comment"># `|` since 3.10.</span>
|
|
<name>: list/set/abc.Iterable/abc.Sequence[<type>] [= <obj>] <span class="hljs-comment"># Since 3.9.</span>
|
|
<name>: dict/tuple[<type>, ...] [= <obj>] <span class="hljs-comment"># Since 3.9.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="dataclass">Dataclass</h3><p><strong>Decorator that uses class variables to generate init(), repr() and eq() special methods.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass, field, make_dataclass
|
|
|
|
<span class="hljs-meta">@dataclass(order=False, frozen=False)</span>
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">class_name</span>>:</span>
|
|
<attr_name>: <type>
|
|
<attr_name>: <type> = <default_value>
|
|
<attr_name>: list/dict/set = field(default_factory=list/dict/set)
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong>Objects can be made <a href="#sortable">sortable</a> with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
|
|
<li><strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and 'frozen' must be True.</strong></li>
|
|
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'<attr_name>: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li>
|
|
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
|
|
</ul>
|
|
<pre><code class="python language-python hljs"><class> = make_dataclass(<span class="hljs-string">'<class_name>'</span>, <coll_of_attribute_names>)
|
|
<class> = make_dataclass(<span class="hljs-string">'<class_name>'</span>, <coll_of_tuples>)
|
|
<tuple> = (<span class="hljs-string">'<attr_name>'</span>, <type> [, <default_value>])</code></pre>
|
|
<div><h3 id="property">Property</h3><p><strong>Pythonic way of implementing getters and setters.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
|
|
<span class="hljs-meta"> @property</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">' '</span>.join(self._name)
|
|
|
|
<span class="hljs-meta"> @name.setter</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span><span class="hljs-params">(self, value)</span>:</span>
|
|
self._name = value.split()
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>person = Person()
|
|
<span class="hljs-meta">>>> </span>person.name = <span class="hljs-string">'\t Guido van Rossum \n'</span>
|
|
<span class="hljs-meta">>>> </span>person.name
|
|
<span class="hljs-string">'Guido van Rossum'</span>
|
|
</code></pre>
|
|
<div><h3 id="slots">Slots</h3><p><strong>Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClassWithSlots</span>:</span>
|
|
__slots__ = [<span class="hljs-string">'a'</span>]
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.a = <span class="hljs-number">1</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="copy">Copy</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> copy <span class="hljs-keyword">import</span> copy, deepcopy
|
|
<object> = copy/deepcopy(<object>)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="ducktypes"><a href="#ducktypes" name="ducktypes">#</a>Duck Types</h2><p><strong>A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.</strong></p><div><h3 id="comparable">Comparable</h3><ul>
|
|
<li><strong>If eq() method is not overridden, it returns <code class="python hljs"><span class="hljs-string">'id(self) == id(other)'</span></code>, which is the same as <code class="python hljs"><span class="hljs-string">'self is other'</span></code>.</strong></li>
|
|
<li><strong>That means all objects compare not equal by default.</strong></li>
|
|
<li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.</strong></li>
|
|
<li><strong>Ne() automatically works on any object that has eq() defined.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComparable</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
|
|
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
|
|
<span class="hljs-keyword">return</span> self.a == other.a
|
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
|
|
<div><h3 id="hashable">Hashable</h3><ul>
|
|
<li><strong>Hashable object needs both hash() and eq() methods and its hash value should never change.</strong></li>
|
|
<li><strong>Hashable objects that compare equal must have the same hash value, meaning default hash() that returns <code class="python hljs"><span class="hljs-string">'id(self)'</span></code> will not do.</strong></li>
|
|
<li><strong>That is why Python automatically makes classes unhashable if you only implement eq().</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHashable</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self._a = a
|
|
<span class="hljs-meta"> @property</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> self._a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
|
|
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
|
|
<span class="hljs-keyword">return</span> self.a == other.a
|
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__hash__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> hash(self.a)
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="sortable">Sortable</h3><ul>
|
|
<li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.</strong></li>
|
|
<li><strong>Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.</strong></li>
|
|
<li><strong>When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.</strong></li>
|
|
<li><strong>To sort collection of strings in proper alphabetical order pass <code class="python hljs"><span class="hljs-string">'key=locale.strxfrm'</span></code> to sorted() after running <code class="python hljs"><span class="hljs-string">'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'</span></code>.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering
|
|
|
|
<span class="hljs-meta">@total_ordering</span>
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySortable</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
|
|
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
|
|
<span class="hljs-keyword">return</span> self.a == other.a
|
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__lt__</span><span class="hljs-params">(self, other)</span>:</span>
|
|
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
|
|
<span class="hljs-keyword">return</span> self.a < other.a
|
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="iterator-1">Iterator</h3><ul>
|
|
<li><strong>Any object that has methods next() and iter() is an iterator.</strong></li>
|
|
<li><strong>Next() should return next item or raise StopIteration exception.</strong></li>
|
|
<li><strong>Iter() should return 'self'.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.i = <span class="hljs-number">0</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.i += <span class="hljs-number">1</span>
|
|
<span class="hljs-keyword">return</span> self.i
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> self
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>counter = Counter()
|
|
<span class="hljs-meta">>>> </span>next(counter), next(counter), next(counter)
|
|
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
</code></pre>
|
|
<div><h4 id="pythonhasmanydifferentiteratorobjects">Python has many different iterator objects:</h4><ul>
|
|
<li><strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong></li>
|
|
<li><strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong></li>
|
|
<li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong></li>
|
|
<li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li>
|
|
</ul><div><h3 id="callable">Callable</h3><ul>
|
|
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
|
|
<li><strong>To check if object is callable use <code class="python hljs"><span class="hljs-string">'callable(<obj>)'</span></code>, <code class="python hljs"><span class="hljs-string">'isinstance(<obj>, collections.abc.Callable)'</span></code>, or <code class="python hljs"><span class="hljs-string">'isinstance(<obj>, typing.Callable)'</span></code>.</strong></li>
|
|
<li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'<function>'</span></code> as an argument, it means <code class="python hljs"><span class="hljs-string">'<callable>'</span></code>.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.i = <span class="hljs-number">0</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.i += <span class="hljs-number">1</span>
|
|
<span class="hljs-keyword">return</span> self.i
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>counter = Counter()
|
|
<span class="hljs-meta">>>> </span>counter(), counter(), counter()
|
|
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
</code></pre>
|
|
<div><h3 id="contextmanager">Context Manager</h3><ul>
|
|
<li><strong>With statements only work on objects that have enter() and exit() special methods.</strong></li>
|
|
<li><strong>Enter() should lock the resources and optionally return an object.</strong></li>
|
|
<li><strong>Exit() should release the resources.</strong></li>
|
|
<li><strong>Any exception that happens inside the with block is passed to the exit() method.</strong></li>
|
|
<li><strong>The exit() method can suppress the exception by returning a true value.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyOpen</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, filename)</span>:</span>
|
|
self.filename = filename
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__enter__</span><span class="hljs-params">(self)</span>:</span>
|
|
self.file = open(self.filename)
|
|
<span class="hljs-keyword">return</span> self.file
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span><span class="hljs-params">(self, exc_type, exception, traceback)</span>:</span>
|
|
self.file.close()
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.txt'</span>, <span class="hljs-string">'w'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-meta">... </span> file.write(<span class="hljs-string">'Hello World!'</span>)
|
|
<span class="hljs-meta">>>> </span><span class="hljs-keyword">with</span> MyOpen(<span class="hljs-string">'test.txt'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-meta">... </span> print(file.read())
|
|
Hello World!
|
|
</code></pre>
|
|
<div><h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2><div><h3 id="iterable">Iterable</h3><ul>
|
|
<li><strong>Only required method is iter(). It should return an iterator of object's items.</strong></li>
|
|
<li><strong>Contains() automatically works on any object that has iter() defined.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyIterable</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> iter(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span><span class="hljs-params">(self, el)</span>:</span>
|
|
<span class="hljs-keyword">return</span> el <span class="hljs-keyword">in</span> self.a
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>obj = MyIterable([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
|
|
<span class="hljs-meta">>>> </span>[el <span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> obj]
|
|
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
|
|
<span class="hljs-meta">>>> </span><span class="hljs-number">1</span> <span class="hljs-keyword">in</span> obj
|
|
<span class="hljs-keyword">True</span>
|
|
</code></pre>
|
|
<div><h3 id="collection">Collection</h3><ul>
|
|
<li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li>
|
|
<li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'<iterable>'</span></code> when it uses <code class="python hljs"><span class="hljs-string">'<collection>'</span></code>.</strong></li>
|
|
<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that the reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCollection</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> iter(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span><span class="hljs-params">(self, el)</span>:</span>
|
|
<span class="hljs-keyword">return</span> el <span class="hljs-keyword">in</span> self.a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> len(self.a)
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="sequence">Sequence</h3><ul>
|
|
<li><strong>Only required methods are getitem() and len().</strong></li>
|
|
<li><strong>Getitem() should return an item at the passed index or raise IndexError.</strong></li>
|
|
<li><strong>Iter() and contains() automatically work on any object that has getitem() defined.</strong></li>
|
|
<li><strong>Reversed() automatically works on any object that has getitem() and len() defined.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySequence</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> iter(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span><span class="hljs-params">(self, el)</span>:</span>
|
|
<span class="hljs-keyword">return</span> el <span class="hljs-keyword">in</span> self.a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> len(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getitem__</span><span class="hljs-params">(self, i)</span>:</span>
|
|
<span class="hljs-keyword">return</span> self.a[i]
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__reversed__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> reversed(self.a)
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h4 id="discrepanciesbetweenglossarydefinitionsandabstractbaseclasses">Discrepancies between glossary definitions and abstract base classes:</h4><ul>
|
|
<li><strong>Glossary defines iterable as any object with iter() or getitem() and sequence as any object with getitem() and len(). It does not define collection.</strong></li>
|
|
<li><strong>Passing ABC Iterable to isinstance() or issubclass() checks whether object/class has method iter(), while ABC Collection checks for iter(), contains() and len().</strong></li>
|
|
</ul></div>
|
|
|
|
|
|
|
|
|
|
<div><h3 id="abcsequence">ABC Sequence</h3><ul>
|
|
<li><strong>It's a richer interface than the basic sequence.</strong></li>
|
|
<li><strong>Extending it generates iter(), contains(), reversed(), index() and count().</strong></li>
|
|
<li><strong>Unlike <code class="python hljs"><span class="hljs-string">'abc.Iterable'</span></code> and <code class="python hljs"><span class="hljs-string">'abc.Collection'</span></code>, it is not a duck type. That is why <code class="python hljs"><span class="hljs-string">'issubclass(MySequence, abc.Sequence)'</span></code> would return False even if MySequence had all the methods defined. It however recognizes list, tuple, range, str, bytes, bytearray, array, memoryview and deque, since they are registered as Sequence's virtual subclasses.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> abc
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAbcSequence</span><span class="hljs-params">(abc.Sequence)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
|
self.a = a
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>
|
|
<span class="hljs-keyword">return</span> len(self.a)
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getitem__</span><span class="hljs-params">(self, i)</span>:</span>
|
|
<span class="hljs-keyword">return</span> self.a[i]
|
|
</code></pre></div><div><h4 id="tableofrequiredandautomaticallyavailablespecialmethods">Table of required and automatically available special methods:</h4><pre><code class="text language-text">┏━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓
|
|
┃ │ Iterable │ Collection │ Sequence │ abc.Sequence ┃
|
|
┠────────────┼────────────┼────────────┼────────────┼──────────────┨
|
|
┃ iter() │ ! │ ! │ ✓ │ ✓ ┃
|
|
┃ contains() │ ✓ │ ✓ │ ✓ │ ✓ ┃
|
|
┃ len() │ │ ! │ ! │ ! ┃
|
|
┃ getitem() │ │ │ ! │ ! ┃
|
|
┃ reversed() │ │ │ ✓ │ ✓ ┃
|
|
┃ index() │ │ │ │ ✓ ┃
|
|
┃ count() │ │ │ │ ✓ ┃
|
|
┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Method iter() is required for <code class="python hljs"><span class="hljs-string">'isinstance(<obj>, abc.Iterable)'</span></code> to return True, however any object with getitem() will work with any code expecting an iterable.</strong></li>
|
|
<li><strong>Other extendable ABCs: MutableSequence, Set, MutableSet, Mapping, MutableMapping.</strong></li>
|
|
<li><strong>Names of their required methods are stored in <code class="python hljs"><span class="hljs-string">'<abc>.__abstractmethods__'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2><p><strong>Class of named constants called members.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">enum_name</span>><span class="hljs-params">(Enum)</span>:</span>
|
|
<member_name> = auto() <span class="hljs-comment"># Increment of the last numeric value or 1.</span>
|
|
<member_name> = <value> <span class="hljs-comment"># Values don't have to be hashable.</span>
|
|
<member_name> = <el_1>, <el_2> <span class="hljs-comment"># Values can be collections (this is a tuple).</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Methods receive the member they were called on as the 'self' argument.</strong></li>
|
|
<li><strong>Accessing a member named after a reserved keyword causes SyntaxError.</strong></li>
|
|
</ul>
|
|
<pre><code class="python language-python hljs"><member> = <enum>.<member_name> <span class="hljs-comment"># Returns a member. Raises AttributeError.</span>
|
|
<member> = <enum>[<span class="hljs-string">'<member_name>'</span>] <span class="hljs-comment"># Returns a member. Raises KeyError.</span>
|
|
<member> = <enum>(<value>) <span class="hljs-comment"># Returns a member. Raises ValueError.</span>
|
|
<str> = <member>.name <span class="hljs-comment"># Returns member's name.</span>
|
|
<obj> = <member>.value <span class="hljs-comment"># Returns member's value.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><list> = list(<enum>) <span class="hljs-comment"># Returns enum's members.</span>
|
|
<list> = [a.name <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <enum>] <span class="hljs-comment"># Returns enum's member names.</span>
|
|
<list> = [a.value <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <enum>] <span class="hljs-comment"># Returns enum's member values.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><enum> = type(<member>) <span class="hljs-comment"># Returns member's enum.</span>
|
|
<iter> = itertools.cycle(<enum>) <span class="hljs-comment"># Returns endless iterator of members.</span>
|
|
<member> = random.choice(list(<enum>)) <span class="hljs-comment"># Returns a random member.</span>
|
|
</code></pre>
|
|
<div><h3 id="inline-1">Inline</h3><pre><code class="python language-python hljs">Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, <span class="hljs-string">'FORK KNIFE SPOON'</span>)
|
|
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, [<span class="hljs-string">'FORK'</span>, <span class="hljs-string">'KNIFE'</span>, <span class="hljs-string">'SPOON'</span>])
|
|
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-string">'FORK'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'KNIFE'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'SPOON'</span>: <span class="hljs-number">3</span>})
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="userdefinedfunctionscannotbevaluessotheymustbewrapped">User-defined functions cannot be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
|
|
LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
|
|
<span class="hljs-string">'OR'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)})
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
|
|
<code>
|
|
<span class="hljs-keyword">except</span> <exception>:
|
|
<code>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="complexexample">Complex Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
|
|
<code_1>
|
|
<span class="hljs-keyword">except</span> <exception_a>:
|
|
<code_2_a>
|
|
<span class="hljs-keyword">except</span> <exception_b>:
|
|
<code_2_b>
|
|
<span class="hljs-keyword">else</span>:
|
|
<code_2_c>
|
|
<span class="hljs-keyword">finally</span>:
|
|
<code_3>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'else'</span></code> block will only be executed if <code class="python hljs"><span class="hljs-string">'try'</span></code> block had no exceptions.</strong></li>
|
|
<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed (unless a signal is received).</strong></li>
|
|
<li><strong>All variables that are initialized in executed blocks are also visible in all subsequent blocks, as well as outside the try statement (only function block delimits scope).</strong></li>
|
|
<li><strong>To catch signals use <code class="python hljs"><span class="hljs-string">'signal.signal(signal_number, <func>)'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception>: ...
|
|
<span class="hljs-keyword">except</span> <exception> <span class="hljs-keyword">as</span> <name>: ...
|
|
<span class="hljs-keyword">except</span> (<exception>, [...]): ...
|
|
<span class="hljs-keyword">except</span> (<exception>, [...]) <span class="hljs-keyword">as</span> <name>: ...
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Also catches subclasses of the exception.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'traceback.print_exc()'</span></code> to print the full error message to stderr.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(<name>)'</span></code> to print just the cause of the exception (its arguments).</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'logging.exception(<str>)'</span></code> to log the passed message, followed by the full error message of the caught exception. For details see <a href="#logging">logging</a>.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'sys.exc_info()'</span></code> to get exception type, object, and traceback of caught exception.</strong></li>
|
|
</ul>
|
|
<div><h3 id="raisingexceptions">Raising Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> <exception>
|
|
<span class="hljs-keyword">raise</span> <exception>()
|
|
<span class="hljs-keyword">raise</span> <exception>(<obj> [, ...])
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="reraisingcaughtexception">Re-raising caught exception:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception> [<span class="hljs-keyword">as</span> <name>]:
|
|
...
|
|
<span class="hljs-keyword">raise</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="exceptionobject">Exception Object</h3><pre><code class="python language-python hljs">arguments = <name>.args
|
|
exc_type = <name>.__class__
|
|
filename = <name>.__traceback__.tb_frame.f_code.co_filename
|
|
func_name = <name>.__traceback__.tb_frame.f_code.co_name
|
|
line = linecache.getline(filename, <name>.__traceback__.tb_lineno)
|
|
trace_str = <span class="hljs-string">''</span>.join(traceback.format_tb(<name>.__traceback__))
|
|
error_msg = <span class="hljs-string">''</span>.join(traceback.format_exception(type(<name>), <name>, <name>.__traceback__))
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="builtinexceptions">Built-in Exceptions</h3><pre><code class="text language-text">BaseException
|
|
├── SystemExit <span class="hljs-comment"># Raised by the sys.exit() function.</span>
|
|
├── KeyboardInterrupt <span class="hljs-comment"># Raised when the user hits the interrupt key (ctrl-c).</span>
|
|
└── Exception <span class="hljs-comment"># User-defined exceptions should be derived from this class.</span>
|
|
├── ArithmeticError <span class="hljs-comment"># Base class for arithmetic errors such as ZeroDivisionError.</span>
|
|
├── AssertionError <span class="hljs-comment"># Raised by `assert <exp>` if expression returns false value.</span>
|
|
├── AttributeError <span class="hljs-comment"># Raised when object doesn't have requested attribute/method.</span>
|
|
├── EOFError <span class="hljs-comment"># Raised by input() when it hits an end-of-file condition.</span>
|
|
├── LookupError <span class="hljs-comment"># Base class for errors when a collection can't find an item.</span>
|
|
│ ├── IndexError <span class="hljs-comment"># Raised when a sequence index is out of range.</span>
|
|
│ └── KeyError <span class="hljs-comment"># Raised when a dictionary key or set element is missing.</span>
|
|
├── MemoryError <span class="hljs-comment"># Out of memory. May be too late to start deleting variables.</span>
|
|
├── NameError <span class="hljs-comment"># Raised when nonexistent name (variable/func/class) is used.</span>
|
|
│ └── UnboundLocalError <span class="hljs-comment"># Raised when local name is used before it's being defined.</span>
|
|
├── OSError <span class="hljs-comment"># Errors such as FileExistsError/TimeoutError (see #Open).</span>
|
|
│ └── ConnectionError <span class="hljs-comment"># Errors such as BrokenPipeError/ConnectionAbortedError.</span>
|
|
├── RuntimeError <span class="hljs-comment"># Raised by errors that don't fall into other categories.</span>
|
|
│ ├── NotImplementedEr… <span class="hljs-comment"># Can be raised by abstract methods or by unfinished code.</span>
|
|
│ └── RecursionError <span class="hljs-comment"># Raised when the maximum recursion depth is exceeded.</span>
|
|
├── StopIteration <span class="hljs-comment"># Raised when an empty iterator is passed to next().</span>
|
|
├── TypeError <span class="hljs-comment"># When an argument of the wrong type is passed to function.</span>
|
|
└── ValueError <span class="hljs-comment"># When argument has the right type but inappropriate value.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="collectionsandtheirexceptions">Collections and their exceptions:</h4><pre><code class="text language-text">┏━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓
|
|
┃ │ List │ Set │ Dict ┃
|
|
┠───────────┼────────────┼────────────┼────────────┨
|
|
┃ getitem() │ IndexError │ │ KeyError ┃
|
|
┃ pop() │ IndexError │ KeyError │ KeyError ┃
|
|
┃ remove() │ ValueError │ KeyError │ ┃
|
|
┃ index() │ ValueError │ │ ┃
|
|
┗━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="usefulbuiltinexceptions">Useful built-in exceptions:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> TypeError(<span class="hljs-string">'Argument is of the wrong type!'</span>)
|
|
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">'Argument has the right type but an inappropriate value!'</span>)
|
|
<span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">'I am too lazy to define my own exception!'</span>)
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="userdefinedexceptions">User-defined Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyError</span><span class="hljs-params">(Exception)</span>:</span> <span class="hljs-keyword">pass</span>
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyInputError</span><span class="hljs-params">(MyError)</span>:</span> <span class="hljs-keyword">pass</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="exit"><a href="#exit" name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
|
sys.exit() <span class="hljs-comment"># Exits with exit code 0 (success).</span>
|
|
sys.exit(<int>) <span class="hljs-comment"># Exits with the passed exit code.</span>
|
|
sys.exit(<obj>) <span class="hljs-comment"># Prints to stderr and exits with 1.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h2 id="print"><a href="#print" name="print">#</a>Print</h2><pre><code class="python language-python hljs">print(<el_1>, ..., sep=<span class="hljs-string">' '</span>, end=<span class="hljs-string">'\n'</span>, file=sys.stdout, flush=<span class="hljs-keyword">False</span>)
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'file=sys.stderr'</span></code> for messages about errors.</strong></li>
|
|
<li><strong>Stdout and stderr streams hold output in a buffer until they receive a string containing '\n' or '\r', buffer reaches 4096 characters, <code class="python hljs"><span class="hljs-string">'flush=True'</span></code> is used, or program exits.</strong></li>
|
|
</ul>
|
|
<div><h3 id="prettyprint">Pretty Print</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pprint <span class="hljs-keyword">import</span> pprint
|
|
pprint(<collection>, width=<span class="hljs-number">80</span>, depth=<span class="hljs-keyword">None</span>, compact=<span class="hljs-keyword">False</span>, sort_dicts=<span class="hljs-keyword">True</span>)
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Each item is printed on its own line if collection exceeds 'width' characters.</strong></li>
|
|
<li><strong>Nested collections that are 'depth' levels deep get printed as '…'.</strong></li>
|
|
</ul>
|
|
<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>)
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Reads a line from the user input or pipe if present (trailing newline gets stripped).</strong></li>
|
|
<li><strong>Prompt string is printed to the standard output before input is read.</strong></li>
|
|
<li><strong>Raises EOFError when user hits EOF (ctrl-d/ctrl-z⏎) or input stream gets exhausted.</strong></li>
|
|
</ul>
|
|
<div><h2 id="commandlinearguments"><a href="#commandlinearguments" name="commandlinearguments">#</a>Command Line Arguments</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
|
scripts_path = sys.argv[<span class="hljs-number">0</span>]
|
|
arguments = sys.argv[<span class="hljs-number">1</span>:]
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="argumentparser">Argument Parser</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> argparse <span class="hljs-keyword">import</span> ArgumentParser, FileType
|
|
p = ArgumentParser(description=<str>) <span class="hljs-comment"># Returns a parser.</span>
|
|
p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, action=<span class="hljs-string">'store_true'</span>) <span class="hljs-comment"># Flag (defaults to False).</span>
|
|
p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span class="hljs-string">'--<name>'</span>, type=<type>) <span class="hljs-comment"># Option (defaults to None).</span>
|
|
p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-number">1</span>) <span class="hljs-comment"># Mandatory first argument.</span>
|
|
p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-string">'+'</span>) <span class="hljs-comment"># Mandatory remaining args.</span>
|
|
p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-string">'?/*'</span>) <span class="hljs-comment"># Optional argument/s.</span>
|
|
<args> = p.parse_args() <span class="hljs-comment"># Exits on parsing error.</span>
|
|
<obj> = <args>.<name> <span class="hljs-comment"># Returns `<type>(<arg>)`.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'help=<str>'</span></code> to set argument description that will be displayed in help message.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'default=<obj>'</span></code> to set option's or optional argument's default value.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files. Accepts 'encoding', but 'newline' is None.</strong></li>
|
|
</ul>
|
|
<div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens the file and returns a corresponding file object.</strong></p><pre><code class="python language-python hljs"><file> = open(<path>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>)
|
|
</code></pre></div>
|
|
|
|
|
|
<ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means that the default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on every '\n', '\r' and '\r\n'.</strong></li>
|
|
</ul>
|
|
<div><h3 id="modes">Modes</h3><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read. Used by default.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'w'</span></code> - Write. Deletes existing contents.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'x'</span></code> - Write or fail if the file already exists.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'a'</span></code> - Append. Creates new file if it doesn't exist.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'w+'</span></code> - Read and write. Deletes existing contents.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'r+'</span></code> - Read and write from the start.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'a+'</span></code> - Read and write from the end.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - Binary mode (<code class="python hljs"><span class="hljs-string">'br'</span></code>, <code class="python hljs"><span class="hljs-string">'bw'</span></code>, <code class="python hljs"><span class="hljs-string">'bx'</span></code>, …)</strong></li>
|
|
</ul><div><h3 id="exceptions-1">Exceptions</h3><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'FileNotFoundError'</span></code> can be raised when reading with <code class="python hljs"><span class="hljs-string">'r'</span></code> or <code class="python hljs"><span class="hljs-string">'r+'</span></code>.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'FileExistsError'</span></code> can be raised when writing with <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'IsADirectoryError'</span></code> and <code class="python hljs"><span class="hljs-string">'PermissionError'</span></code> can be raised by any.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'OSError'</span></code> is the parent class of all listed exceptions.</strong></li>
|
|
</ul><div><h3 id="fileobject">File Object</h3><pre><code class="python language-python hljs"><file>.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves to the start of the file.</span>
|
|
<file>.seek(offset) <span class="hljs-comment"># Moves 'offset' chars/bytes from the start.</span>
|
|
<file>.seek(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Moves to the end of the file.</span>
|
|
<bin_file>.seek(±offset, <anchor>) <span class="hljs-comment"># Anchor: 0 start, 1 current position, 2 end.</span>
|
|
</code></pre></div></div></div>
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="python language-python hljs"><str/bytes> = <file>.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars/bytes or until EOF.</span>
|
|
<str/bytes> = <file>.readline() <span class="hljs-comment"># Returns a line or empty string/bytes on EOF.</span>
|
|
<list> = <file>.readlines() <span class="hljs-comment"># Returns a list of remaining lines.</span>
|
|
<str/bytes> = next(<file>) <span class="hljs-comment"># Returns a line using buffer. Do not mix.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><file>.write(<str/bytes>) <span class="hljs-comment"># Writes a string or bytes object.</span>
|
|
<file>.writelines(<collection>) <span class="hljs-comment"># Writes a coll. of strings or bytes objects.</span>
|
|
<file>.flush() <span class="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span>
|
|
<file>.close() <span class="hljs-comment"># Closes the file after flushing write buffer.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Methods do not add or strip trailing newlines, not even writelines().</strong></li>
|
|
</ul>
|
|
<div><h3 id="readtextfromfile">Read Text from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> file.readlines()
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writetexttofile">Write Text to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_file</span><span class="hljs-params">(filename, text)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
|
|
file.write(text)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="paths"><a href="#paths" name="paths">#</a>Paths</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os, glob
|
|
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><str> = os.getcwd() <span class="hljs-comment"># Returns shell's working dir unless changed.</span>
|
|
<str> = os.path.join(<path>, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
|
|
<str> = os.path.realpath(<path>) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><str> = os.path.basename(<path>) <span class="hljs-comment"># Returns final component of the path.</span>
|
|
<str> = os.path.dirname(<path>) <span class="hljs-comment"># Returns path without the final component.</span>
|
|
<tup.> = os.path.splitext(<path>) <span class="hljs-comment"># Splits on last period of the final component.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><list> = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at the path.</span>
|
|
<list> = glob.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><bool> = os.path.exists(<path>) <span class="hljs-comment"># Or: <Path>.exists()</span>
|
|
<bool> = os.path.isfile(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.is_file()</span>
|
|
<bool> = os.path.isdir(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.is_dir()</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><stat> = os.stat(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.stat()</span>
|
|
<num> = <stat>.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
|
|
</code></pre>
|
|
<div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs"><iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span>
|
|
<str> = <DirEntry>.path <span class="hljs-comment"># Returns the whole path as a string.</span>
|
|
<str> = <DirEntry>.name <span class="hljs-comment"># Returns final component as a string.</span>
|
|
<file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns a file object.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="pathobject">Path Object</h3><pre><code class="python language-python hljs"><Path> = Path(<path> [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span>
|
|
<Path> = <path> / <path> [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span>
|
|
<Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute path with resolved symlinks.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span>
|
|
<Path> = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span>
|
|
<Path> = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
|
|
<Path> = Path(__file__).resolve() <span class="hljs-comment"># Returns script's path if cwd wasn't changed.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns Path without the final component.</span>
|
|
<str> = <Path>.name <span class="hljs-comment"># Returns final component as a string.</span>
|
|
<str> = <Path>.stem <span class="hljs-comment"># Returns final component without extension.</span>
|
|
<str> = <Path>.suffix <span class="hljs-comment"># Returns final component's extension.</span>
|
|
<tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span>
|
|
<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns path as a string.</span>
|
|
<file> = open(<Path>) <span class="hljs-comment"># Also <Path>.read/write_text/bytes(<args>).</span>
|
|
</code></pre>
|
|
<div><h2 id="oscommands"><a href="#oscommands" name="oscommands">#</a>OS Commands</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os, shutil, subprocess
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs">os.chdir(<path>) <span class="hljs-comment"># Changes the current working directory.</span>
|
|
os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Permissions are in octal.</span>
|
|
os.makedirs(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also `exist_ok=False`.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file. 'to' can exist or be a dir.</span>
|
|
shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span>
|
|
shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>
|
|
os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file 'to' even on Windows.</span>
|
|
shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into 'to' if it's a dir.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span>
|
|
os.rmdir(<path>) <span class="hljs-comment"># Deletes the empty directory.</span>
|
|
shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the directory.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Paths can be either strings, Paths or DirEntry objects.</strong></li>
|
|
<li><strong>Functions report OS related errors by raising either OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><pipe> = os.popen(<span class="hljs-string">'<command>'</span>) <span class="hljs-comment"># Executes command in sh/cmd. Returns its stdout pipe.</span>
|
|
<str> = <pipe>.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span>
|
|
<int> = <pipe>.close() <span class="hljs-comment"># Closes the pipe. Returns None on success (returncode 0).</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="sends11tothebasiccalculatorandcapturesitsoutput">Sends '1 + 1' to the basic calculator and captures its output:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>subprocess.run(<span class="hljs-string">'bc'</span>, input=<span class="hljs-string">'1 + 1\n'</span>, capture_output=<span class="hljs-keyword">True</span>, text=<span class="hljs-keyword">True</span>)
|
|
CompletedProcess(args=<span class="hljs-string">'bc'</span>, returncode=<span class="hljs-number">0</span>, stdout=<span class="hljs-string">'2\n'</span>, stderr=<span class="hljs-string">''</span>)
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="sendstestintothebasiccalculatorrunninginstandardmodeandsavesitsoutputtotestout">Sends test.in to the basic calculator running in standard mode and saves its output to test.out:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> shlex <span class="hljs-keyword">import</span> split
|
|
<span class="hljs-meta">>>> </span>os.popen(<span class="hljs-string">'echo 1 + 1 > test.in'</span>)
|
|
<span class="hljs-meta">>>> </span>subprocess.run(split(<span class="hljs-string">'bc -s'</span>), stdin=open(<span class="hljs-string">'test.in'</span>), stdout=open(<span class="hljs-string">'test.out'</span>, <span class="hljs-string">'w'</span>))
|
|
CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-string">'-s'</span>], returncode=<span class="hljs-number">0</span>)
|
|
<span class="hljs-meta">>>> </span>open(<span class="hljs-string">'test.out'</span>).read()
|
|
<span class="hljs-string">'2\n'</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="json"><a href="#json" name="json">#</a>JSON</h2><p><strong>Text file format for storing collections of strings and numbers.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> json
|
|
<str> = json.dumps(<object>) <span class="hljs-comment"># Converts object to JSON string.</span>
|
|
<object> = json.loads(<str>) <span class="hljs-comment"># Converts JSON string to object.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="readobjectfromjsonfile">Read Object from JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_json_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> json.load(file)
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writeobjecttojsonfile">Write Object to JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_json_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
|
|
json.dump(an_object, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="pickle"><a href="#pickle" name="pickle">#</a>Pickle</h2><p><strong>Binary file format for storing Python objects.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pickle
|
|
<bytes> = pickle.dumps(<object>) <span class="hljs-comment"># Converts object to bytes object.</span>
|
|
<object> = pickle.loads(<bytes>) <span class="hljs-comment"># Converts bytes object to object.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="readobjectfromfile">Read Object from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_pickle_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> pickle.load(file)
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writeobjecttofile">Write Object to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_pickle_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
|
|
pickle.dump(an_object, file)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="csv"><a href="#csv" name="csv">#</a>CSV</h2><p><strong>Text file format for storing spreadsheets.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> csv
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="read">Read</h3><pre><code class="python language-python hljs"><reader> = csv.reader(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span>
|
|
<list> = next(<reader>) <span class="hljs-comment"># Returns next row as a list of strings.</span>
|
|
<list> = list(<reader>) <span class="hljs-comment"># Returns a list of remaining rows.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or newlines embedded inside quoted fields will not be interpreted correctly!</strong></li>
|
|
<li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong></li>
|
|
<li><strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#dataframeplotencodedecode">Pandas</a> library.</strong></li>
|
|
<li><strong>Reader accepts any iterator of strings, not just files.</strong></li>
|
|
</ul>
|
|
<div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = csv.writer(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span>
|
|
<writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span>
|
|
<writer>.writerows(<coll_of_coll>) <span class="hljs-comment"># Appends multiple rows.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or '\r' will be added in front of every '\n' on platforms that use '\r\n' line endings!</strong></li>
|
|
<li><strong>Open existing file with <code class="python hljs"><span class="hljs-string">'mode="w"'</span></code> to overwrite it or <code class="python hljs"><span class="hljs-string">'mode="a"'</span></code> to append to it.</strong></li>
|
|
</ul>
|
|
<div><h3 id="parameters">Parameters</h3><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values. String or a 'csv.Dialect' object.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'delimiter'</span></code> - A one-character string used to separate fields.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'lineterminator'</span></code> - How writer terminates rows. Reader is hardcoded to '\n', '\r', '\r\n'.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'quotechar'</span></code> - Character for quoting fields that contain special characters.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping quotechars.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields are/get doubled or escaped.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'quoting'</span></code> - 0: As necessary, 1: All, 2: All but numbers which are read as floats, 3: None.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'skipinitialspace'</span></code> - Is space character at the start of the field stripped by the reader.</strong></li>
|
|
</ul><div><h3 id="dialects">Dialects</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓
|
|
┃ │ excel │ excel-tab │ unix ┃
|
|
┠──────────────────┼──────────────┼──────────────┼──────────────┨
|
|
┃ delimiter │ ',' │ '\t' │ ',' ┃
|
|
┃ lineterminator │ '\r\n' │ '\r\n' │ '\n' ┃
|
|
┃ quotechar │ '"' │ '"' │ '"' ┃
|
|
┃ escapechar │ None │ None │ None ┃
|
|
┃ doublequote │ True │ True │ True ┃
|
|
┃ quoting │ 0 │ 0 │ 1 ┃
|
|
┃ skipinitialspace │ False │ False │ False ┃
|
|
┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
<div><h3 id="readrowsfromcsvfile">Read Rows from CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_csv_file</span><span class="hljs-params">(filename, dialect=<span class="hljs-string">'excel'</span>, **params)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> list(csv.reader(file, dialect, **params))
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writerowstocsvfile">Write Rows to CSV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_csv_file</span><span class="hljs-params">(filename, rows, mode=<span class="hljs-string">'w'</span>, dialect=<span class="hljs-string">'excel'</span>, **params)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, mode, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
|
|
writer = csv.writer(file, dialect, **params)
|
|
writer.writerows(rows)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>A server-less database engine that stores each database into a separate file.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3
|
|
<conn> = sqlite3.connect(<path>) <span class="hljs-comment"># Opens existing or new file. Also ':memory:'.</span>
|
|
<conn>.close() <span class="hljs-comment"># Closes connection. Discards uncommitted data.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs"><cursor> = <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
|
|
<tuple> = <cursor>.fetchone() <span class="hljs-comment"># Returns next row. Also next(<cursor>).</span>
|
|
<list> = <cursor>.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(<cursor>).</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
|
|
<conn>.commit() <span class="hljs-comment"># Saves all changes since the last commit.</span>
|
|
<conn>.rollback() <span class="hljs-comment"># Discards all changes since the last commit.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <conn>: <span class="hljs-comment"># Exits the block with commit() or rollback(),</span>
|
|
<conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># depending on whether any exception occurred.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="placeholders">Placeholders</h3><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
|
|
<conn>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span>
|
|
<conn>.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() multiple times.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.</strong></li>
|
|
<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="example-1">Example</h3><p><strong>Values are not actually saved in this example because <code class="python hljs"><span class="hljs-string">'conn.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>conn = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
|
|
<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'CREATE TABLE person (person_id INTEGER PRIMARY KEY, name, height)'</span>)
|
|
<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'INSERT INTO person VALUES (NULL, ?, ?)'</span>, (<span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)).lastrowid
|
|
<span class="hljs-number">1</span>
|
|
<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'SELECT * FROM person'</span>).fetchall()
|
|
[(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="sqlalchemy">SqlAlchemy</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install sqlalchemy</span>
|
|
<span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> create_engine, text
|
|
<engine> = create_engine(<span class="hljs-string">'<url>'</span>) <span class="hljs-comment"># Url: 'dialect://user:password@host/dbname'.</span>
|
|
<conn> = <engine>.connect() <span class="hljs-comment"># Creates a connection. Also <conn>.close().</span>
|
|
<cursor> = <conn>.execute(text(<span class="hljs-string">'<query>'</span>), …) <span class="hljs-comment"># Replaces ':<key>'s with keyword arguments.</span>
|
|
<span class="hljs-keyword">with</span> <conn>.begin(): ... <span class="hljs-comment"># Exits the block with commit or rollback.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="text language-text">┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
|
┃ Dialect │ pip3 install │ import │ Dependencies ┃
|
|
┠────────────┼──────────────┼──────────┼──────────────────────────────────┨
|
|
┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃
|
|
┃ postgresql │ psycopg2 │ psycopg2 │ www.pypi.org/project/psycopg2 ┃
|
|
┃ mssql │ pyodbc │ pyodbc │ www.pypi.org/project/pyodbc ┃
|
|
┃ oracle │ oracledb │ oracledb │ www.pypi.org/project/oracledb ┃
|
|
┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
<div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>A bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.</strong></p><pre><code class="python language-python hljs"><bytes> = <span class="hljs-string">b'<str>'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00-\xff.</span>
|
|
<int> = <bytes>[index] <span class="hljs-comment"># Returns an int in range from 0 to 255.</span>
|
|
<bytes> = <bytes>[<slice>] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
|
|
<bytes> = <bytes>.join(<coll_of_bytes>) <span class="hljs-comment"># Joins elements using bytes as a separator.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs"><bytes> = bytes(<coll_of_ints>) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span>
|
|
<bytes> = bytes(<str>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Encodes the string. Also <str>.encode().</span>
|
|
<bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespaces.</span>
|
|
<bytes> = <int>.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="decode-1">Decode</h3><pre><code class="python language-python hljs"><list> = list(<bytes>) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span>
|
|
<str> = str(<bytes>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Returns a string. Also <bytes>.decode().</span>
|
|
<str> = <bytes>.hex() <span class="hljs-comment"># Returns hex pairs. Accepts `sep=<str>`.</span>
|
|
<int> = int.from_bytes(<bytes>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="readbytesfromfile">Read Bytes from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_bytes</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> file.read()
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writebytestofile">Write Bytes to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_bytes</span><span class="hljs-params">(filename, bytes_obj)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
|
|
file.write(bytes_obj)
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="struct"><a href="#struct" name="struct">#</a>Struct</h2><ul>
|
|
<li><strong>Module that performs conversions between a sequence of numbers and a bytes object.</strong></li>
|
|
<li><strong>System’s type sizes, byte order, and alignment rules are used by default.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> struct <span class="hljs-keyword">import</span> pack, unpack
|
|
|
|
<bytes> = pack(<span class="hljs-string">'<format>'</span>, <el_1> [, ...]) <span class="hljs-comment"># Packs objects according to format string.</span>
|
|
<tuple> = unpack(<span class="hljs-string">'<format>'</span>, <bytes>) <span class="hljs-comment"># Use iter_unpack() to get iterator of tuples.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>pack(<span class="hljs-string">'>hhl'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
<span class="hljs-string">b'\x00\x01\x00\x02\x00\x00\x00\x03'</span>
|
|
<span class="hljs-meta">>>> </span>unpack(<span class="hljs-string">'>hhl'</span>, <span class="hljs-string">b'\x00\x01\x00\x02\x00\x00\x00\x03'</span>)
|
|
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
|
</code></pre>
|
|
<h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignmentpaddingstartformatstringwith">For standard type sizes and manual alignment (padding) start format string with:</h4><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'='</span></code> - System's byte order (usually little-endian).</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'<'</span></code> - Little-endian (i.e. least significant byte first).</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'>'</span></code> - Big-endian (also <code class="python hljs"><span class="hljs-string">'!'</span></code>).</strong></li>
|
|
</ul><div><h4 id="besidesnumberspackandunpackalsosupportbytesobjectsasapartofthesequence">Besides numbers, pack() and unpack() also support bytes objects as a part of the sequence:</h4><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'c'</span></code> - A bytes object with a single element. For pad byte use <code class="python hljs"><span class="hljs-string">'x'</span></code>.</strong></li>
|
|
<li><strong><code class="apache hljs"><span class="hljs-section">'<n>s'</span><span class="hljs-attribute"></span></code> - A bytes object with n elements (not effected by byte order).</strong></li>
|
|
</ul></div></div><div><div><h4 id="integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:</h4><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'b'</span></code> - char (1/1)</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'h'</span></code> - short (2/2)</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'i'</span></code> - int (2/4)</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'l'</span></code> - long (4/4)</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'q'</span></code> - long long (8/8)</strong></li>
|
|
</ul></div><div><h4 id="floatingpointtypesstructalwaysusesstandardsizes">Floating point types (struct always uses standard sizes):</h4><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'f'</span></code> - float (4/4)</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'d'</span></code> - double (8/8)</strong></li>
|
|
</ul></div></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be swapped with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><array> = array(<span class="hljs-string">'<typecode>'</span>, <coll_of_nums>) <span class="hljs-comment"># Array from collection of numbers.</span>
|
|
<array> = array(<span class="hljs-string">'<typecode>'</span>, <bytes>) <span class="hljs-comment"># Array from bytes object.</span>
|
|
<array> = array(<span class="hljs-string">'<typecode>'</span>, <array>) <span class="hljs-comment"># Treats array as a sequence of numbers.</span>
|
|
<array>.fromfile(<file>, n_items) <span class="hljs-comment"># Appends items from the binary file.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><bytes> = bytes(<array>) <span class="hljs-comment"># Returns a copy of array's memory.</span>
|
|
<file>.write(<array>) <span class="hljs-comment"># Writes array's memory to the file.</span>
|
|
</code></pre>
|
|
<div><h2 id="memoryview"><a href="#memoryview" name="memoryview">#</a>Memory View</h2><p><strong>A sequence object that points to the memory of another bytes-like object. Each element can reference a single or multiple consecutive bytes, depending on format. Order and number of elements can be changed with slicing.</strong></p><pre><code class="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <span class="hljs-comment"># Immutable if bytes, else mutable.</span>
|
|
<obj> = <mview>[index] <span class="hljs-comment"># Returns int, float or bytes ('c' format).</span>
|
|
<mview> = <mview>[<slice>] <span class="hljs-comment"># Returns mview with rearranged elements.</span>
|
|
<mview> = <mview>.cast(<span class="hljs-string">'<typecode>'</span>) <span class="hljs-comment"># Only works between B/b/c and other types.</span>
|
|
<mview>.release() <span class="hljs-comment"># Releases memory buffer of the base object.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><bytes> = bytes(<mview>) <span class="hljs-comment"># Returns a new bytes object.</span>
|
|
<bytes> = <bytes>.join(<coll_of_mviews>) <span class="hljs-comment"># Joins mviews using bytes as a separator.</span>
|
|
<array> = array(<span class="hljs-string">'<typecode>'</span>, <mview>) <span class="hljs-comment"># Treats mview as a sequence of numbers.</span>
|
|
<file>.write(<mview>) <span class="hljs-comment"># Writes `bytes(<mview>)` to the file.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><list> = list(<mview>) <span class="hljs-comment"># Returns a list of ints, floats or bytes.</span>
|
|
<str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span>
|
|
<str> = <mview>.hex() <span class="hljs-comment"># Returns hex pairs. Accepts `sep=<str>`.</span>
|
|
</code></pre>
|
|
<div><h2 id="deque"><a href="#deque" name="deque">#</a>Deque</h2><p><strong>A thread-safe list with efficient appends and pops from either side. Pronounced "deck".</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><deque> = deque(<collection>) <span class="hljs-comment"># Use `maxlen=<int>` to set size limit.</span>
|
|
<deque>.appendleft(<el>) <span class="hljs-comment"># Opposite element is dropped if full.</span>
|
|
<deque>.extendleft(<collection>) <span class="hljs-comment"># Passed collection gets reversed.</span>
|
|
<deque>.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Last element becomes first.</span>
|
|
<el> = <deque>.popleft() <span class="hljs-comment"># Raises IndexError if deque is empty.</span>
|
|
</code></pre>
|
|
<div><h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2><p><strong>CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, Lock, RLock, Semaphore, Event, Barrier
|
|
<span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor, as_completed
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set the arguments.</span>
|
|
<Thread>.start() <span class="hljs-comment"># Starts the thread. Also <Thread>.is_alive().</span>
|
|
<Thread>.join() <span class="hljs-comment"># Waits for the thread to finish.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
|
|
</ul>
|
|
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = Lock/RLock() <span class="hljs-comment"># RLock can only be released by acquirer.</span>
|
|
<lock>.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span>
|
|
<lock>.release() <span class="hljs-comment"># Makes the lock available again.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <lock>: <span class="hljs-comment"># Enters the block by calling acquire() and</span>
|
|
... <span class="hljs-comment"># exits it with release(), even on error.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-python hljs"><Semaphore> = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span>
|
|
<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
|
|
<Barrier> = Barrier(n_times) <span class="hljs-comment"># Wait() blocks until it's called n_times.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="queue">Queue</h3><pre><code class="python language-python hljs"><Queue> = queue.Queue(maxsize=<span class="hljs-number">0</span>) <span class="hljs-comment"># A thread-safe first-in-first-out queue.</span>
|
|
<Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span>
|
|
<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
|
|
<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
|
|
<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><pre><code class="python language-python hljs"><Exec> = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as <name>: ...`</span>
|
|
<iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># Multithreaded and non-lazy map(). Keeps order.</span>
|
|
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Creates a thread and returns its Future obj.</span>
|
|
<Exec>.shutdown() <span class="hljs-comment"># Blocks until all threads finish executing.</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
|
|
<obj> = <Future>.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
|
|
<bool> = <Future>.cancel() <span class="hljs-comment"># Cancels or returns False if running/finished.</span>
|
|
<iter> = as_completed(<coll_of_Futures>) <span class="hljs-comment"># `next(<iter>)` returns next completed Future.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if thread finished on time.</strong></li>
|
|
<li><strong>Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.</strong></li>
|
|
<li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <a href="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
|
|
<bool> = op.not_(<obj>) <span class="hljs-comment"># or, and, not (or/and missing)</span>
|
|
<bool> = op.eq/ne/lt/ge/is_/is_not/contains(<obj>, <obj>) <span class="hljs-comment"># ==, !=, <, >=, is, is not, in</span>
|
|
<obj> = op.or_/xor/and_(<int/set>, <int/set>) <span class="hljs-comment"># |, ^, &</span>
|
|
<int> = op.lshift/rshift(<int>, <int>) <span class="hljs-comment"># <<, >></span>
|
|
<obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) <span class="hljs-comment"># +, -, *, /, //, %</span>
|
|
<num> = op.neg/invert(<num>) <span class="hljs-comment"># -, ~</span>
|
|
<num> = op.pow(<num>, <num>) <span class="hljs-comment"># **</span>
|
|
<func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...]) <span class="hljs-comment"># [index/key], .name, .name([…])</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs">elementwise_sum = map(op.add, list_a, list_b)
|
|
sorted_by_second = sorted(<coll.>, key=op.itemgetter(<span class="hljs-number">1</span>))
|
|
sorted_by_both = sorted(<coll.>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
|
|
product_of_elems = functools.reduce(op.mul, <collection>)
|
|
first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span class="hljs-number">0</span>)(<list>)
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Most operators call the object's special method that is named after them (second object is passed as an argument), while logical operators call their own code that relies on bool().</strong></li>
|
|
<li><strong>Comparisons can be chained: <code class="python hljs"><span class="hljs-string">'x < y < z'</span></code> gets converted to <code class="python hljs"><span class="hljs-string">'(x < y) and (y < z)</span></code>'.</strong></li>
|
|
</ul>
|
|
<div><h2 id="matchstatement"><a href="#matchstatement" name="matchstatement">#</a>Match Statement</h2><p><strong>Executes the first block with matching pattern. Added in Python 3.10.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">match</span> <object/expression>:
|
|
<span class="hljs-keyword">case</span> <pattern> [<span class="hljs-keyword">if</span> <condition>]:
|
|
<code>
|
|
...
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="patterns">Patterns</h3><pre><code class="python language-python hljs"><value_pattern> = <span class="hljs-number">1</span>/<span class="hljs-string">'abc'</span>/<span class="hljs-keyword">True</span>/<span class="hljs-keyword">None</span>/math.pi <span class="hljs-comment"># Matches the literal or a dotted name.</span>
|
|
<class_pattern> = <type>() <span class="hljs-comment"># Matches any object of that type.</span>
|
|
<wildcard_patt> = _ <span class="hljs-comment"># Matches any object.</span>
|
|
<capture_patt> = <name> <span class="hljs-comment"># Matches any object and binds it to name.</span>
|
|
<as_pattern> = <pattern> <span class="hljs-keyword">as</span> <name> <span class="hljs-comment"># Binds match to name. Also <type>(<name>).</span>
|
|
<or_pattern> = <pattern> | <pattern> [| ...] <span class="hljs-comment"># Matches any of the patterns.</span>
|
|
<sequence_patt> = [<pattern>, ...] <span class="hljs-comment"># Matches sequence with matching items.</span>
|
|
<mapping_patt> = {<value_pattern>: <patt>, ...} <span class="hljs-comment"># Matches dictionary with matching items.</span>
|
|
<class_pattern> = <type>(<attr_name>=<patt>, ...) <span class="hljs-comment"># Matches object with matching attributes.</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Sequence pattern can also be written as a tuple.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'*<name>'</span></code> and <code class="python hljs"><span class="hljs-string">'**<name>'</span></code> in sequence/mapping patterns to bind remaining items.</strong></li>
|
|
<li><strong>Sequence pattern must match all items, while mapping pattern does not.</strong></li>
|
|
<li><strong>Patterns can be surrounded with brackets to override precedence (<code class="python hljs"><span class="hljs-string">'|'</span></code> > <code class="python hljs"><span class="hljs-string">'as'</span></code> > <code class="python hljs"><span class="hljs-string">','</span></code>).</strong></li>
|
|
<li><strong>Built-in types allow a single positional pattern that is matched against the entire object.</strong></li>
|
|
<li><strong>All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement.</strong></li>
|
|
</ul>
|
|
<div><h3 id="example-2">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
|
|
<span class="hljs-meta">>>> </span><span class="hljs-keyword">match</span> Path(<span class="hljs-string">'/home/gto/python-cheatsheet/README.md'</span>):
|
|
<span class="hljs-meta">... </span> <span class="hljs-keyword">case</span> Path(
|
|
<span class="hljs-meta">... </span> parts=[<span class="hljs-string">'/'</span>, <span class="hljs-string">'home'</span>, user, *_],
|
|
<span class="hljs-meta">... </span> stem=stem,
|
|
<span class="hljs-meta">... </span> suffix=(<span class="hljs-string">'.md'</span> | <span class="hljs-string">'.txt'</span>) <span class="hljs-keyword">as</span> suffix
|
|
<span class="hljs-meta">... </span> ) <span class="hljs-keyword">if</span> stem.lower() == <span class="hljs-string">'readme'</span>:
|
|
<span class="hljs-meta">... </span> print(<span class="hljs-string">f'<span class="hljs-subst">{stem}</span><span class="hljs-subst">{suffix}</span> is a readme file that belongs to user <span class="hljs-subst">{user}</span>.'</span>)
|
|
<span class="hljs-string">'README.md is a readme file that belongs to user gto.'</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="logging"><a href="#logging" name="logging">#</a>Logging</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> logging <span class="hljs-keyword">as</span> log
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs">log.basicConfig(filename=<path>, level=<span class="hljs-string">'DEBUG'</span>) <span class="hljs-comment"># Configures the root logger (see Setup).</span>
|
|
log.debug/info/warning/error/critical(<str>) <span class="hljs-comment"># Logs to the root logger.</span>
|
|
<Logger> = log.getLogger(__name__) <span class="hljs-comment"># Logger named after the module.</span>
|
|
<Logger>.<level>(<str>) <span class="hljs-comment"># Logs to the logger.</span>
|
|
<Logger>.exception(<str>) <span class="hljs-comment"># Error() that appends caught exception.</span>
|
|
</code></pre>
|
|
<div><h3 id="setup">Setup</h3><pre><code class="python language-python hljs">log.basicConfig(
|
|
filename=<span class="hljs-keyword">None</span>, <span class="hljs-comment"># Logs to stderr or appends to file.</span>
|
|
format=<span class="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <span class="hljs-comment"># Add '%(asctime)s' for local datetime.</span>
|
|
level=log.WARNING, <span class="hljs-comment"># Drops messages with lower priority.</span>
|
|
handlers=[log.StreamHandler(sys.stderr)] <span class="hljs-comment"># Uses FileHandler if filename is set.</span>
|
|
)
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><Formatter> = log.Formatter(<span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Creates a Formatter.</span>
|
|
<Handler> = log.FileHandler(<path>, mode=<span class="hljs-string">'a'</span>) <span class="hljs-comment"># Creates a Handler. Also `encoding=None`.</span>
|
|
<Handler>.setFormatter(<Formatter>) <span class="hljs-comment"># Adds Formatter to the Handler.</span>
|
|
<Handler>.setLevel(<int/str>) <span class="hljs-comment"># Processes all messages by default.</span>
|
|
<Logger>.addHandler(<Handler>) <span class="hljs-comment"># Adds Handler to the Logger.</span>
|
|
<Logger>.setLevel(<int/str>) <span class="hljs-comment"># What is sent to its/ancestors' handlers.</span>
|
|
<Logger>.propagate = <bool> <span class="hljs-comment"># Cuts off ancestors' handlers if False.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Parent logger can be specified by naming the child logger <code class="python hljs"><span class="hljs-string">'<parent>.<name>'</span></code>.</strong></li>
|
|
<li><strong>If logger doesn't have a set level it inherits it from the first ancestor that does.</strong></li>
|
|
<li><strong>Formatter also accepts: pathname, filename, funcName, lineno, thread and process.</strong></li>
|
|
<li><strong>RotatingFileHandler creates and deletes files based on 'maxBytes' and 'backupCount' args.</strong></li>
|
|
<li><strong>An object with <code class="python hljs"><span class="hljs-string">'filter(<LogRecord>)'</span></code> method (or the method itself) can be added to loggers and handlers via addFilter(). Message is dropped if filter() returns a false value.</strong></li>
|
|
</ul>
|
|
<div><h4 id="createsaloggerthatwritesallmessagestofileandsendsthemtotherootshandlerthatprintswarningsorhigher">Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>logger = log.getLogger(<span class="hljs-string">'my_module'</span>)
|
|
<span class="hljs-meta">>>> </span>handler = log.FileHandler(<span class="hljs-string">'test.log'</span>, encoding=<span class="hljs-string">'utf-8'</span>)
|
|
<span class="hljs-meta">>>> </span>handler.setFormatter(log.Formatter(<span class="hljs-string">'%(asctime)s %(levelname)s:%(name)s:%(message)s'</span>))
|
|
<span class="hljs-meta">>>> </span>logger.addHandler(handler)
|
|
<span class="hljs-meta">>>> </span>logger.setLevel(<span class="hljs-string">'DEBUG'</span>)
|
|
<span class="hljs-meta">>>> </span>log.basicConfig()
|
|
<span class="hljs-meta">>>> </span>log.root.handlers[<span class="hljs-number">0</span>].setLevel(<span class="hljs-string">'WARNING'</span>)
|
|
<span class="hljs-meta">>>> </span>logger.critical(<span class="hljs-string">'Running out of disk space.'</span>)
|
|
CRITICAL:my_module:Running out of disk space.
|
|
<span class="hljs-meta">>>> </span>print(open(<span class="hljs-string">'test.log'</span>).read())
|
|
2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Names of local vars, functions, classes and modules.</span>
|
|
<dict> = vars() <span class="hljs-comment"># Dict of local vars, functions, etc. Also locals().</span>
|
|
<dict> = globals() <span class="hljs-comment"># Dict of global vars, etc. (including '__builtins__').</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><list> = dir(<obj>) <span class="hljs-comment"># Names of all object's attributes (including methods).</span>
|
|
<dict> = vars(<obj>) <span class="hljs-comment"># Dict of writable attributes. Also <obj>.__dict__.</span>
|
|
<bool> = hasattr(<obj>, <span class="hljs-string">'<attr_name>'</span>) <span class="hljs-comment"># Checks if getattr() raises AttributeError.</span>
|
|
value = getattr(<obj>, <span class="hljs-string">'<attr_name>'</span>) <span class="hljs-comment"># Default value can be passed as the third argument.</span>
|
|
setattr(<obj>, <span class="hljs-string">'<attr_name>'</span>, value) <span class="hljs-comment"># Only works on objects with __dict__ attribute.</span>
|
|
delattr(<obj>, <span class="hljs-string">'<attr_name>'</span>) <span class="hljs-comment"># Same. Also `del <object>.<attr_name>`.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Sig> = inspect.signature(<function>) <span class="hljs-comment"># Returns function's Signature object.</span>
|
|
<dict> = <Sig>.parameters <span class="hljs-comment"># Dict of Parameters. Also <Sig>.return_annotation.</span>
|
|
<memb> = <Param>.kind <span class="hljs-comment"># Member of ParamKind enum (Parameter.KEYWORD_ONLY, …).</span>
|
|
<obj> = <Param>.default <span class="hljs-comment"># Parameter.empty if missing. Also <Param>.annotation.</span>
|
|
</code></pre>
|
|
<div><h2 id="coroutines"><a href="#coroutines" name="coroutines">#</a>Coroutines</h2><ul>
|
|
<li><strong>Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.</strong></li>
|
|
<li><strong>Coroutine definition starts with <code class="python hljs"><span class="hljs-string">'async'</span></code> and its call with <code class="python hljs"><span class="hljs-string">'await'</span></code>.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'asyncio.run(<coroutine>)'</span></code> is the main entry point for asynchronous programs.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> asyncio <span class="hljs-keyword">as</span> aio
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><coro> = <async_function>(<args>) <span class="hljs-comment"># Creates a coroutine by calling async def function.</span>
|
|
<obj> = <span class="hljs-keyword">await</span> <coroutine> <span class="hljs-comment"># Starts the coroutine and returns result.</span>
|
|
<task> = aio.create_task(<coroutine>) <span class="hljs-comment"># Schedules the coroutine for execution.</span>
|
|
<obj> = <span class="hljs-keyword">await</span> <task> <span class="hljs-comment"># Returns result. Also <task>.cancel().</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><coro> = aio.gather(<coro/task>, ...) <span class="hljs-comment"># Schedules coros. Returns list of results on await.</span>
|
|
<coro> = aio.wait(<tasks>, …) <span class="hljs-comment"># `aio.ALL/FIRST_COMPLETED`. Returns (done, pending).</span>
|
|
<iter> = aio.as_completed(<coros/tasks>) <span class="hljs-comment"># Iterator of coros. All return next result on await.</span>
|
|
</code></pre>
|
|
<div><h4 id="runsaterminalgamewhereyoucontrolanasteriskthatmustavoidnumbers">Runs a terminal game where you control an asterisk that must avoid numbers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random
|
|
|
|
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
|
|
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
|
|
W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
|
|
curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>
|
|
screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>
|
|
asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>
|
|
moves = asyncio.Queue()
|
|
state = {<span class="hljs-string">'*'</span>: P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), **{id_: P(W//<span class="hljs-number">2</span>, H//<span class="hljs-number">2</span>) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)}}
|
|
ai = [random_controller(id_, moves) <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)]
|
|
mvc = [human_controller(screen, moves), model(moves, state), view(state, screen)]
|
|
tasks = [asyncio.create_task(coro) <span class="hljs-keyword">for</span> coro <span class="hljs-keyword">in</span> ai + mvc]
|
|
<span class="hljs-keyword">await</span> asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">random_controller</span><span class="hljs-params">(id_, moves)</span>:</span>
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
|
|
d = random.choice(list(D))
|
|
moves.put_nowait((id_, d))
|
|
<span class="hljs-keyword">await</span> asyncio.sleep(random.triangular(<span class="hljs-number">0.01</span>, <span class="hljs-number">0.65</span>))
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">human_controller</span><span class="hljs-params">(screen, moves)</span>:</span>
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
|
|
key_mappings = {<span class="hljs-number">258</span>: D.s, <span class="hljs-number">259</span>: D.n, <span class="hljs-number">260</span>: D.w, <span class="hljs-number">261</span>: D.e}
|
|
<span class="hljs-keyword">if</span> d := key_mappings.get(screen.getch()):
|
|
moves.put_nowait((<span class="hljs-string">'*'</span>, d))
|
|
<span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span><span class="hljs-params">(moves, state)</span>:</span>
|
|
<span class="hljs-keyword">while</span> state[<span class="hljs-string">'*'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (state[id_] <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)):
|
|
id_, d = <span class="hljs-keyword">await</span> moves.get()
|
|
deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
|
|
state[id_] = P((state[id_].x + deltas[d].x) % W, (state[id_].y + deltas[d].y) % H)
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(state, screen)</span>:</span>
|
|
offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
|
|
screen.erase()
|
|
curses.textpad.rectangle(screen, offset.y-<span class="hljs-number">1</span>, offset.x-<span class="hljs-number">1</span>, offset.y+H, offset.x+W)
|
|
<span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():
|
|
screen.addstr(offset.y + (p.y - state[<span class="hljs-string">'*'</span>].y + H//<span class="hljs-number">2</span>) % H,
|
|
offset.x + (p.x - state[<span class="hljs-string">'*'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))
|
|
screen.refresh()
|
|
<span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)
|
|
|
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
|
curses.wrapper(main)
|
|
</code></pre></div>
|
|
|
|
<p><br></p>
|
|
<div><h1 id="libraries">Libraries</h1><div><h2 id="progressbar"><a href="#progressbar" name="progressbar">#</a>Progress Bar</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tqdm</span>
|
|
<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> tqdm, time
|
|
<span class="hljs-meta">>>> </span><span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> tqdm.tqdm([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], desc=<span class="hljs-string">'Processing'</span>):
|
|
<span class="hljs-meta">... </span> time.sleep(<span class="hljs-number">1</span>)
|
|
Processing: 100%|████████████████████| 3/3 [00:03<00:00, 1.00s/it]
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="plot"><a href="#plot" name="plot">#</a>Plot</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install matplotlib</span>
|
|
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
|
|
|
|
plt.plot/bar/scatter(x_data, y_data [, label=<str>]) <span class="hljs-comment"># Or: plt.plot(y_data)</span>
|
|
plt.legend() <span class="hljs-comment"># Adds a legend.</span>
|
|
plt.title/xlabel/ylabel(<str>) <span class="hljs-comment"># Adds a title/label.</span>
|
|
plt.savefig(<path>) <span class="hljs-comment"># Saves the figure.</span>
|
|
plt.show() <span class="hljs-comment"># Displays the figure.</span>
|
|
plt.clf() <span class="hljs-comment"># Clears the figure.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="table"><a href="#table" name="table">#</a>Table</h2><div><h4 id="printsacsvspreadsheettotheconsole">Prints a CSV spreadsheet to the console:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tabulate</span>
|
|
<span class="hljs-keyword">import</span> csv, tabulate
|
|
<span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.csv'</span>, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
|
|
rows = list(csv.reader(file))
|
|
print(tabulate.tabulate(rows, headers=<span class="hljs-string">'firstrow'</span>))
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="consoleapp"><a href="#consoleapp" name="consoleapp">#</a>Console App</h2><div><h4 id="runsabasicfileexplorerintheconsole">Runs a basic file explorer in the console:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install windows-curses</span>
|
|
<span class="hljs-keyword">import</span> curses, os
|
|
<span class="hljs-keyword">from</span> curses <span class="hljs-keyword">import</span> A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>
|
|
ch, first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, os.listdir()
|
|
<span class="hljs-keyword">while</span> ch != ord(<span class="hljs-string">'q'</span>):
|
|
height, width = screen.getmaxyx()
|
|
screen.erase()
|
|
<span class="hljs-keyword">for</span> y, filename <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]):
|
|
color = A_REVERSE <span class="hljs-keyword">if</span> filename == paths[selected] <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
|
|
screen.addnstr(y, <span class="hljs-number">0</span>, filename, width-<span class="hljs-number">1</span>, color)
|
|
ch = screen.getch()
|
|
selected += (ch == KEY_DOWN) - (ch == KEY_UP)
|
|
selected = max(<span class="hljs-number">0</span>, min(len(paths)-<span class="hljs-number">1</span>, selected))
|
|
first += (selected >= first + height) - (selected < first)
|
|
<span class="hljs-keyword">if</span> ch <span class="hljs-keyword">in</span> [KEY_LEFT, KEY_RIGHT, KEY_ENTER, ord(<span class="hljs-string">'\n'</span>), ord(<span class="hljs-string">'\r'</span>)]:
|
|
new_dir = <span class="hljs-string">'..'</span> <span class="hljs-keyword">if</span> ch == KEY_LEFT <span class="hljs-keyword">else</span> paths[selected]
|
|
<span class="hljs-keyword">if</span> os.path.isdir(new_dir):
|
|
os.chdir(new_dir)
|
|
first, selected, paths = <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, os.listdir()
|
|
|
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
|
curses.wrapper(main)
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="guiapp"><a href="#guiapp" name="guiapp">#</a>GUI App</h2><div><h4 id="aweightconverterguiapplication">A weight converter GUI application:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install PySimpleGUI</span>
|
|
<span class="hljs-keyword">import</span> PySimpleGUI <span class="hljs-keyword">as</span> sg
|
|
|
|
text_box = sg.Input(default_text=<span class="hljs-string">'100'</span>, enable_events=<span class="hljs-keyword">True</span>, key=<span class="hljs-string">'-VALUE-'</span>)
|
|
dropdown = sg.InputCombo([<span class="hljs-string">'g'</span>, <span class="hljs-string">'kg'</span>, <span class="hljs-string">'t'</span>], <span class="hljs-string">'kg'</span>, readonly=<span class="hljs-keyword">True</span>, enable_events=<span class="hljs-keyword">True</span>, k=<span class="hljs-string">'-UNIT-'</span>)
|
|
label = sg.Text(<span class="hljs-string">'100 kg is 220.462 lbs.'</span>, key=<span class="hljs-string">'-OUTPUT-'</span>)
|
|
button = sg.Button(<span class="hljs-string">'Close'</span>)
|
|
window = sg.Window(<span class="hljs-string">'Weight Converter'</span>, [[text_box, dropdown], [label], [button]])
|
|
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
|
|
event, values = window.read()
|
|
<span class="hljs-keyword">if</span> event <span class="hljs-keyword">in</span> [sg.WIN_CLOSED, <span class="hljs-string">'Close'</span>]:
|
|
<span class="hljs-keyword">break</span>
|
|
<span class="hljs-keyword">try</span>:
|
|
value = float(values[<span class="hljs-string">'-VALUE-'</span>])
|
|
<span class="hljs-keyword">except</span> ValueError:
|
|
<span class="hljs-keyword">continue</span>
|
|
unit = values[<span class="hljs-string">'-UNIT-'</span>]
|
|
factors = {<span class="hljs-string">'g'</span>: <span class="hljs-number">0.001</span>, <span class="hljs-string">'kg'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'t'</span>: <span class="hljs-number">1000</span>}
|
|
lbs = value * factors[unit] / <span class="hljs-number">0.45359237</span>
|
|
window[<span class="hljs-string">'-OUTPUT-'</span>].update(value=<span class="hljs-string">f'<span class="hljs-subst">{value}</span> <span class="hljs-subst">{unit}</span> is <span class="hljs-subst">{lbs:g}</span> lbs.'</span>)
|
|
window.close()
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="scraping"><a href="#scraping" name="scraping">#</a>Scraping</h2><div><h4 id="scrapespythonsurlandlogofromitswikipediapage">Scrapes Python's URL and logo from its Wikipedia page:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests beautifulsoup4</span>
|
|
<span class="hljs-keyword">import</span> requests, bs4, os
|
|
|
|
response = requests.get(<span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span>)
|
|
document = bs4.BeautifulSoup(response.text, <span class="hljs-string">'html.parser'</span>)
|
|
table = document.find(<span class="hljs-string">'table'</span>, class_=<span class="hljs-string">'infobox vevent'</span>)
|
|
python_url = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Website'</span>).next_sibling.a[<span class="hljs-string">'href'</span>]
|
|
logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>]
|
|
logo = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{logo_url}</span>'</span>).content
|
|
filename = os.path.basename(logo_url)
|
|
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
|
|
file.write(logo)
|
|
print(<span class="hljs-string">f'<span class="hljs-subst">{python_url}</span>, file://<span class="hljs-subst">{os.path.abspath(filename)}</span>'</span>)
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h3 id="selenium">Selenium</h3><p><strong>Library for scraping websites with dynamic content.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install selenium</span>
|
|
<span class="hljs-keyword">from</span> selenium <span class="hljs-keyword">import</span> webdriver
|
|
|
|
<Drv> = webdriver.Chrome/Firefox/Safari/Edge() <span class="hljs-comment"># Opens the browser. Also <Drv>.quit().</span>
|
|
<Drv>.get(<span class="hljs-string">'<url>'</span>) <span class="hljs-comment"># Also <Drv>.implicitly_wait(seconds).</span>
|
|
<El> = <Drv/El>.find_element(<span class="hljs-string">'css selector'</span>, <span class="hljs-string">'<css>'</span>) <span class="hljs-comment"># '<tag>#<id>.<class>[<attr>="<val>"]'.</span>
|
|
<list> = <Drv/El>.find_elements(<span class="hljs-string">'xpath'</span>, <span class="hljs-string">'<xpath>'</span>) <span class="hljs-comment"># '//<tag>[@<attr>="<val>"]'.</span>
|
|
<str> = <El>.get_attribute(<str>) <span class="hljs-comment"># Property if exists. Also <El>.text.</span>
|
|
<El>.click/clear() <span class="hljs-comment"># Also <El>.send_keys(<str>).</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h4 id="xpathalsoavailableinlxmlscrapyandbrowsersconsoleviadxxpath">XPath — also available in lxml, Scrapy, and browser's console via <code class="python hljs"><span class="hljs-string">'$x(<xpath>)'</span></code>:</h4><pre><code class="python language-python hljs"><xpath> = //<element>[/ <span class="hljs-keyword">or</span> // <element>] <span class="hljs-comment"># /<child>, //<descendant>, /../<siblng></span>
|
|
<xpath> = //<element>/following::<element> <span class="hljs-comment"># Next element. Also preceding/parent/…</span>
|
|
<element> = <tag><conditions><index> <span class="hljs-comment"># `<tag> = */a/…`, `<index> = [1/2/…]`.</span>
|
|
<condition> = [<sub_cond> [<span class="hljs-keyword">and</span>/<span class="hljs-keyword">or</span> <sub_cond>]] <span class="hljs-comment"># For negation use `not(<sub_cond>)`.</span>
|
|
<sub_cond> = @<attr>[=<span class="hljs-string">"<val>"</span>] <span class="hljs-comment"># `text()=`, `.=` match (complete) text.</span>
|
|
<sub_cond> = contains(@<attr>, <span class="hljs-string">"<val>"</span>) <span class="hljs-comment"># Is <val> a substring of attr's value?</span>
|
|
<sub_cond> = [//]<element> <span class="hljs-comment"># Has matching child? Descendant if //.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="web"><a href="#web" name="web">#</a>Web</h2><p><strong>Flask is a micro web framework/server. If you just want to open a html file in a web browser use <code class="python hljs"><span class="hljs-string">'webbrowser.open(<path>)'</span></code> instead.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install flask</span>
|
|
<span class="hljs-keyword">import</span> flask
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs">app = flask.Flask(__name__) <span class="hljs-comment"># Returns app object. Put at the top.</span>
|
|
app.run(host=<span class="hljs-keyword">None</span>, port=<span class="hljs-keyword">None</span>, debug=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: $ flask --app FILE run [--ARG[=VAL]]</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Starts the app at <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>. Use <code class="python hljs"><span class="hljs-string">'host="0.0.0.0"'</span></code> to run externally.</strong></li>
|
|
<li><strong>Install a WSGI server like <a href="https://flask.palletsprojects.com/en/latest/deploying/waitress/">Waitress</a> and a HTTP server such as <a href="https://flask.palletsprojects.com/en/latest/deploying/nginx/">Nginx</a> for better security.</strong></li>
|
|
<li><strong>Debug mode restarts the app whenever script changes and displays errors in the browser.</strong></li>
|
|
</ul>
|
|
<div><h3 id="staticrequest">Static Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.route('/img/<path:filename>')</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">return</span> flask.send_from_directory(<span class="hljs-string">'dirname/'</span>, filename)
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="dynamicrequest">Dynamic Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.route('/<sport>')</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_html</span><span class="hljs-params">(sport)</span>:</span>
|
|
<span class="hljs-keyword">return</span> flask.render_template_string(<span class="hljs-string">'<h1>{{title}}</h1>'</span>, title=sport)
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'render_template(filename, <kwargs>)'</span></code> to render file located in templates dir.</strong></li>
|
|
<li><strong>To return an error code use <code class="python hljs"><span class="hljs-string">'abort(<int>)'</span></code> and to redirect use <code class="python hljs"><span class="hljs-string">'redirect(<url>)'</span></code>.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'request.args[<str>]'</span></code> returns parameter from the query string (URL part after '?').</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'session[<str>] = <obj>'</span></code> stores session data. Needs <code class="python hljs"><span class="hljs-string">'app.secret_key = <str>'</span></code>.</strong></li>
|
|
</ul>
|
|
<div><h3 id="restrequest">REST Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.post('/<sport>/odds')</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_json</span><span class="hljs-params">(sport)</span>:</span>
|
|
team = flask.request.form[<span class="hljs-string">'team'</span>]
|
|
<span class="hljs-keyword">return</span> {<span class="hljs-string">'team'</span>: team, <span class="hljs-string">'odds'</span>: [<span class="hljs-number">2.09</span>, <span class="hljs-number">3.74</span>, <span class="hljs-number">3.68</span>]}
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="startstheappinitsownthreadandqueriesitsrestapi">Starts the app in its own thread and queries its REST API:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
|
|
<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> threading, requests
|
|
<span class="hljs-meta">>>> </span>threading.Thread(target=app.run, daemon=<span class="hljs-keyword">True</span>).start()
|
|
<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:5000/football/odds'</span>
|
|
<span class="hljs-meta">>>> </span>request_data = {<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>}
|
|
<span class="hljs-meta">>>> </span>response = requests.post(url, data=request_data)
|
|
<span class="hljs-meta">>>> </span>response.json()
|
|
{<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>, <span class="hljs-string">'odds'</span>: [<span class="hljs-number">2.09</span>, <span class="hljs-number">3.74</span>, <span class="hljs-number">3.68</span>]}
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="profiling"><a href="#profiling" name="profiling">#</a>Profiling</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> perf_counter
|
|
start_time = perf_counter()
|
|
...
|
|
duration_in_seconds = perf_counter() - start_time
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="timingasnippet">Timing a Snippet</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> timeit <span class="hljs-keyword">import</span> timeit
|
|
<span class="hljs-meta">>>> </span>timeit(<span class="hljs-string">'list(range(10000))'</span>, number=<span class="hljs-number">1000</span>, globals=globals(), setup=<span class="hljs-string">'pass'</span>)
|
|
<span class="hljs-number">0.19373</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="text language-text">$ pip3 install line_profiler
|
|
$ echo '@profile
|
|
def main():
|
|
a = list(range(10000))
|
|
b = set(range(10000))
|
|
main()' > test.py
|
|
$ kernprof -lv test.py
|
|
Line # Hits Time Per Hit % Time Line Contents
|
|
==============================================================
|
|
1 @profile
|
|
2 def main():
|
|
3 1 253.4 253.4 32.2 a = list(range(10000))
|
|
4 1 534.1 534.1 67.8 b = set(range(10000))
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="callandflamegraphs">Call and Flame Graphs</h3><pre><code class="bash language-bash hljs">$ apt/brew install graphviz && pip3 install gprof2dot snakeviz <span class="hljs-comment"># Or download installer.</span>
|
|
$ tail --lines=+2 test.py > test.py <span class="hljs-comment"># Removes first line.</span>
|
|
$ python3 -m cProfile -o test.prof test.py <span class="hljs-comment"># Runs built-in profiler.</span>
|
|
$ gprof2dot --format=pstats test.prof | dot -T png -o test.png <span class="hljs-comment"># Generates call graph.</span>
|
|
$ xdg-open/open test.png <span class="hljs-comment"># Displays call graph.</span>
|
|
$ snakeviz test.prof <span class="hljs-comment"># Displays flame graph.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="samplingandmemoryprofilers">Sampling and Memory Profilers</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━┯━━━━━━┓
|
|
┃ pip3 install │ Target │ How to run │ Lines │ Live ┃
|
|
┠──────────────┼────────────┼───────────────────────────────┼───────┼──────┨
|
|
┃ pyinstrument │ CPU │ pyinstrument test.py │ × │ × ┃
|
|
┃ py-spy │ CPU │ py-spy top -- python3 test.py │ × │ ✓ ┃
|
|
┃ scalene │ CPU+Memory │ scalene test.py │ ✓ │ × ┃
|
|
┃ memray │ Memory │ memray run --live test.py │ ✓ │ ✓ ┃
|
|
┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━┷━━━━━━┛
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="numpy"><a href="#numpy" name="numpy">#</a>NumPy</h2><p><strong>Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. An even faster alternative that runs on a GPU is called CuPy.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install numpy</span>
|
|
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><array> = np.array(<list/list_of_lists/…>) <span class="hljs-comment"># Returns a 1d/2d/… NumPy array.</span>
|
|
<array> = np.zeros/ones/empty(<shape>) <span class="hljs-comment"># Also np.full(<shape>, <el>).</span>
|
|
<array> = np.arange(from_inc, to_exc, ±step) <span class="hljs-comment"># Also np.linspace(start, stop, len).</span>
|
|
<array> = np.random.randint(from_inc, to_exc, <shape>) <span class="hljs-comment"># Also np.random.random(<shape>).</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><view> = <array>.reshape(<shape>) <span class="hljs-comment"># Also `<array>.shape = <shape>`.</span>
|
|
<array> = <array>.flatten() <span class="hljs-comment"># Also `<view> = <array>.ravel()`.</span>
|
|
<view> = <array>.transpose() <span class="hljs-comment"># Or: <array>.T</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><array> = np.copy/abs/sqrt/log/int64(<array>) <span class="hljs-comment"># Returns new array of the same shape.</span>
|
|
<array> = <array>.sum/max/mean/argmax/all(axis) <span class="hljs-comment"># Aggregates specified dimension.</span>
|
|
<array> = np.apply_along_axis(<func>, axis, <array>) <span class="hljs-comment"># Func can return a scalar or array.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><array> = np.concatenate(<list_of_arrays>, axis=<span class="hljs-number">0</span>) <span class="hljs-comment"># Links arrays along first axis (rows).</span>
|
|
<array> = np.row_stack/column_stack(<list_of_arrays>) <span class="hljs-comment"># Treats 1d arrays as rows or columns.</span>
|
|
<array> = np.tile/repeat(<array>, <int/list> [, axis]) <span class="hljs-comment"># Tiles array or repeats its elements.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).</strong></li>
|
|
<li><strong>Axis is an index of a dimension. Leftmost dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).</strong></li>
|
|
</ul>
|
|
<div><h3 id="indexing">Indexing</h3><pre><code class="bash hljs"><el> = <2d>[row_index, col_index] <span class="hljs-comment"># Or: <3d>[<int>, <int>, <int>]</span>
|
|
<1d_view> = <2d>[row_index] <span class="hljs-comment"># Or: <3d>[<int>, <int>, <slice>]</span>
|
|
<1d_view> = <2d>[:, col_index] <span class="hljs-comment"># Or: <3d>[<int>, <slice>, <int>]</span>
|
|
<2d_view> = <2d>[from:to_row_i, from:to_col_i] <span class="hljs-comment"># Or: <3d>[<int>, <slice>, <slice>]</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="bash hljs"><1d_array> = <2d>[row_indices, col_indices] <span class="hljs-comment"># Or: <3d>[<int/1d>, <1d>, <1d>]</span>
|
|
<2d_array> = <2d>[row_indices] <span class="hljs-comment"># Or: <3d>[<int/1d>, <1d>, <slice>]</span>
|
|
<2d_array> = <2d>[:, col_indices] <span class="hljs-comment"># Or: <3d>[<int/1d>, <slice>, <1d>]</span>
|
|
<2d_array> = <2d>[np.ix_(row_indices, col_indices)] <span class="hljs-comment"># Or: <3d>[<int/1d/2d>, <2d>, <2d>]</span>
|
|
</code></pre>
|
|
<pre><code class="bash hljs"><2d_bools> = <2d> > <el/1d/2d> <span class="hljs-comment"># 1d object must have size of a row.</span>
|
|
<1/2d_arr> = <2d>[<2d/1d_bools>] <span class="hljs-comment"># 1d_bools must have size of a column.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">':'</span></code> returns a slice of all dimension's indices. Omitted dimensions default to <code class="python hljs"><span class="hljs-string">':'</span></code>.</strong></li>
|
|
<li><strong>Indices should not be tuples because Python converts <code class="python hljs"><span class="hljs-string">'obj[i, j]'</span></code> to <code class="python hljs"><span class="hljs-string">'obj[(i, j)]'</span></code>!</strong></li>
|
|
<li><strong>Indexing with a slice and 1d array works the same as when using two slices (lines 4, 6, 7).</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'ix_([1, 2], [3, 4])'</span></code> returns <code class="python hljs"><span class="hljs-string">'[[1], [2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4]]'</span></code>. Due to broadcasting rules, this is the same as using <code class="python hljs"><span class="hljs-string">'[[1, 1], [2, 2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4], [3, 4]]'</span></code>.</strong></li>
|
|
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
|
|
</ul>
|
|
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>Set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.</strong></p><pre><code class="python language-python hljs">left = [ <span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span> ] <span class="hljs-comment"># Shape: (3,)</span>
|
|
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (1, 3) <- !</span>
|
|
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># Shape: (3, 3) <- !</span>
|
|
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
|
|
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]
|
|
|
|
right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>], <span class="hljs-comment"># Shape: (3, 3) <- !</span>
|
|
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
|
|
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]]
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>])
|
|
[ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]
|
|
<span class="hljs-meta">>>> </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>)
|
|
[[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]
|
|
<span class="hljs-meta">>>> </span>distances = points - wrapped_points
|
|
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
|
|
[<span class="hljs-number">-0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
|
|
[<span class="hljs-number">-0.7</span>, <span class="hljs-number">-0.2</span>, <span class="hljs-number">0.</span> ]]
|
|
<span class="hljs-meta">>>> </span>distances = np.abs(distances)
|
|
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
|
|
[ <span class="hljs-number">0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
|
|
[ <span class="hljs-number">0.7</span>, <span class="hljs-number">0.2</span>, <span class="hljs-number">0.</span> ]]
|
|
<span class="hljs-meta">>>> </span>distances[range(<span class="hljs-number">3</span>), range(<span class="hljs-number">3</span>)] = np.inf
|
|
[[ inf, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
|
|
[ <span class="hljs-number">0.5</span>, inf, <span class="hljs-number">0.2</span>],
|
|
[ <span class="hljs-number">0.7</span>, <span class="hljs-number">0.2</span>, inf]]
|
|
<span class="hljs-meta">>>> </span>distances.argmin(<span class="hljs-number">1</span>)
|
|
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="image"><a href="#image" name="image">#</a>Image</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pillow</span>
|
|
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><Image> = Image.new(<span class="hljs-string">'<mode>'</span>, (width, height)) <span class="hljs-comment"># Creates new image. Also `color=<int/tuple>`.</span>
|
|
<Image> = Image.open(<path>) <span class="hljs-comment"># Identifies format based on file's contents.</span>
|
|
<Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode.</span>
|
|
<Image>.save(<path>) <span class="hljs-comment"># Selects format based on extension (png/jpg…).</span>
|
|
<Image>.show() <span class="hljs-comment"># Opens image in the default preview app.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span>
|
|
<Image>.putpixel((x, y), <int/tuple>) <span class="hljs-comment"># Updates pixel's value.</span>
|
|
<ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a flattened view of pixel values.</span>
|
|
<Image>.putdata(<list/ImagingCore>) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span>
|
|
<Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Draws passed image at the specified location.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Image> = <Image>.filter(<Filter>) <span class="hljs-comment"># `<Filter> = ImageFilter.<name>(<args>)`</span>
|
|
<Image> = <Enhance>.enhance(<float>) <span class="hljs-comment"># `<Enhance> = ImageEnhance.<name>(<Image>)`</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><array> = np.array(<Image>) <span class="hljs-comment"># Creates a 2d/3d NumPy array from the image.</span>
|
|
<Image> = Image.fromarray(np.uint8(<array>)) <span class="hljs-comment"># Use `<array>.clip(0, 255)` to clip values.</span>
|
|
</code></pre>
|
|
<div><h3 id="modes-1">Modes</h3><ul>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - Lightness (i.e. greyscale). Each pixel is an int between 0 and 255.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - Red, green, blue (i.e. true color). Each pixel is a tuple of three ints.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (forth int) means more transparency.</strong></li>
|
|
<li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - Hue, saturation, value color space.</strong></li>
|
|
</ul><div><h3 id="examples">Examples</h3><div><h4 id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><code class="python language-python hljs">WIDTH, HEIGHT = <span class="hljs-number">100</span>, <span class="hljs-number">100</span>
|
|
n_pixels = WIDTH * HEIGHT
|
|
hues = (<span class="hljs-number">255</span> * i/n_pixels <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n_pixels))
|
|
img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT))
|
|
img.putdata([(int(h), <span class="hljs-number">255</span>, <span class="hljs-number">255</span>) <span class="hljs-keyword">for</span> h <span class="hljs-keyword">in</span> hues])
|
|
img.convert(<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
|
</code></pre></div></div></div>
|
|
|
|
|
|
|
|
|
|
<div><h4 id="addsnoisetothepngimageanddisplaysit">Adds noise to the PNG image and displays it:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
|
add_noise = <span class="hljs-keyword">lambda</span> value: max(<span class="hljs-number">0</span>, min(<span class="hljs-number">255</span>, value + randint(<span class="hljs-number">-20</span>, <span class="hljs-number">20</span>)))
|
|
img = Image.open(<span class="hljs-string">'test.png'</span>).convert(<span class="hljs-string">'HSV'</span>)
|
|
img.putdata([(add_noise(h), s, v) <span class="hljs-keyword">for</span> h, s, v <span class="hljs-keyword">in</span> img.getdata()])
|
|
img.show()
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="imagedraw">Image Draw</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> ImageDraw
|
|
<ImageDraw> = ImageDraw.Draw(<Image>) <span class="hljs-comment"># Object for adding 2D graphics to the image.</span>
|
|
<ImageDraw>.point((x, y)) <span class="hljs-comment"># Draws a point. Truncates floats into ints.</span>
|
|
<ImageDraw>.line((x1, y1, x2, y2 [, ...])) <span class="hljs-comment"># To get anti-aliasing use Image's resize().</span>
|
|
<ImageDraw>.arc((x1, y1, x2, y2), deg1, deg2) <span class="hljs-comment"># Always draws in clockwise direction.</span>
|
|
<ImageDraw>.rectangle((x1, y1, x2, y2)) <span class="hljs-comment"># To rotate use Image's rotate() and paste().</span>
|
|
<ImageDraw>.polygon((x1, y1, x2, y2, ...)) <span class="hljs-comment"># Last point gets connected to the first.</span>
|
|
<ImageDraw>.ellipse((x1, y1, x2, y2)) <span class="hljs-comment"># To rotate use Image's rotate() and paste().</span>
|
|
<ImageDraw>.text((x, y), <str>, font=<Font>) <span class="hljs-comment"># `<Font> = ImageFont.truetype(<path>, size)`</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'fill=<color>'</span></code> to set the primary color.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'width=<int>'</span></code> to set the width of lines or contours.</strong></li>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'outline=<color>'</span></code> to set the color of the contours.</strong></li>
|
|
<li><strong>Color can be an int, tuple, <code class="python hljs"><span class="hljs-string">'#rrggbb[aa]'</span></code> string or a color name.</strong></li>
|
|
</ul>
|
|
<div><h2 id="animation"><a href="#animation" name="animation">#</a>Animation</h2><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install imageio</span>
|
|
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
|
|
<span class="hljs-keyword">import</span> imageio
|
|
|
|
WIDTH, HEIGHT, R = <span class="hljs-number">126</span>, <span class="hljs-number">126</span>, <span class="hljs-number">10</span>
|
|
frames = []
|
|
<span class="hljs-keyword">for</span> velocity <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, <span class="hljs-number">16</span>):
|
|
y = sum(range(velocity))
|
|
frame = Image.new(<span class="hljs-string">'L'</span>, (WIDTH, HEIGHT))
|
|
draw = ImageDraw.Draw(frame)
|
|
draw.ellipse((WIDTH/<span class="hljs-number">2</span>-R, y, WIDTH/<span class="hljs-number">2</span>+R, y+R*<span class="hljs-number">2</span>), fill=<span class="hljs-string">'white'</span>)
|
|
frames.append(frame)
|
|
frames += reversed(frames[<span class="hljs-number">1</span>:<span class="hljs-number">-1</span>])
|
|
imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<span class="hljs-number">0.03</span>)
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="audio"><a href="#audio" name="audio">#</a>Audio</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> wave
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><Wave> = wave.open(<span class="hljs-string">'<path>'</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-comment"># Opens the WAV file.</span>
|
|
<int> = <Wave>.getframerate() <span class="hljs-comment"># Returns number of frames per second.</span>
|
|
<int> = <Wave>.getnchannels() <span class="hljs-comment"># Returns number of samples per frame.</span>
|
|
<int> = <Wave>.getsampwidth() <span class="hljs-comment"># Returns number of bytes per sample.</span>
|
|
<tuple> = <Wave>.getparams() <span class="hljs-comment"># Returns namedtuple of all parameters.</span>
|
|
<bytes> = <Wave>.readframes(nframes) <span class="hljs-comment"># Returns next n frames. All if -1.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Wave> = wave.open(<span class="hljs-string">'<path>'</span>, <span class="hljs-string">'wb'</span>) <span class="hljs-comment"># Creates/truncates a file for writing.</span>
|
|
<Wave>.setframerate(<int>) <span class="hljs-comment"># Pass 44100 for CD, 48000 for video.</span>
|
|
<Wave>.setnchannels(<int>) <span class="hljs-comment"># Pass 1 for mono, 2 for stereo.</span>
|
|
<Wave>.setsampwidth(<int>) <span class="hljs-comment"># Pass 2 for CD, 3 for hi-res sound.</span>
|
|
<Wave>.setparams(<tuple>) <span class="hljs-comment"># Sets all parameters.</span>
|
|
<Wave>.writeframes(<bytes>) <span class="hljs-comment"># Appends frames to the file.</span>
|
|
</code></pre>
|
|
<ul>
|
|
<li><strong>Bytes object contains a sequence of frames, each consisting of one or more samples.</strong></li>
|
|
<li><strong>In a stereo signal, the first sample of a frame belongs to the left channel.</strong></li>
|
|
<li><strong>Each sample consists of one or more bytes that, when converted to an integer, indicate the displacement of a speaker membrane at a given moment.</strong></li>
|
|
<li><strong>If sample width is one byte, then the integer should be encoded unsigned.</strong></li>
|
|
<li><strong>For all other sizes, the integer should be encoded signed with little-endian byte order.</strong></li>
|
|
</ul>
|
|
<div><h3 id="samplevalues">Sample Values</h3><pre><code class="python hljs">┏━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━┓
|
|
┃ sampwidth │ min │ zero │ max ┃
|
|
┠───────────┼───────────┼──────┼───────────┨
|
|
┃ <span class="hljs-number">1</span> │ <span class="hljs-number">0</span> │ <span class="hljs-number">128</span> │ <span class="hljs-number">255</span> ┃
|
|
┃ <span class="hljs-number">2</span> │ <span class="hljs-number">-32768</span> │ <span class="hljs-number">0</span> │ <span class="hljs-number">32767</span> ┃
|
|
┃ <span class="hljs-number">3</span> │ <span class="hljs-number">-8388608</span> │ <span class="hljs-number">0</span> │ <span class="hljs-number">8388607</span> ┃
|
|
┗━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━┛
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="readfloatsamplesfromwavfile">Read Float Samples from WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_wav_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_int</span><span class="hljs-params">(bytes_obj)</span>:</span>
|
|
an_int = int.from_bytes(bytes_obj, <span class="hljs-string">'little'</span>, signed=(sampwidth != <span class="hljs-number">1</span>))
|
|
<span class="hljs-keyword">return</span> an_int - <span class="hljs-number">128</span> * (sampwidth == <span class="hljs-number">1</span>)
|
|
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
|
sampwidth = file.getsampwidth()
|
|
frames = file.readframes(<span class="hljs-number">-1</span>)
|
|
bytes_samples = (frames[i : i+sampwidth] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), sampwidth))
|
|
<span class="hljs-keyword">return</span> [get_int(b) / pow(<span class="hljs-number">2</span>, sampwidth * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> b <span class="hljs-keyword">in</span> bytes_samples]
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="writefloatsamplestowavfile">Write Float Samples to WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_wav_file</span><span class="hljs-params">(filename, float_samples, nchannels=<span class="hljs-number">1</span>, sampwidth=<span class="hljs-number">2</span>, framerate=<span class="hljs-number">44100</span>)</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bytes</span><span class="hljs-params">(a_float)</span>:</span>
|
|
a_float = max(<span class="hljs-number">-1</span>, min(<span class="hljs-number">1</span> - <span class="hljs-number">2e-16</span>, a_float))
|
|
a_float += sampwidth == <span class="hljs-number">1</span>
|
|
a_float *= pow(<span class="hljs-number">2</span>, sampwidth * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>)
|
|
<span class="hljs-keyword">return</span> int(a_float).to_bytes(sampwidth, <span class="hljs-string">'little'</span>, signed=(sampwidth != <span class="hljs-number">1</span>))
|
|
<span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
|
|
file.setnchannels(nchannels)
|
|
file.setsampwidth(sampwidth)
|
|
file.setframerate(framerate)
|
|
file.writeframes(<span class="hljs-string">b''</span>.join(get_bytes(f) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> float_samples))
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="examples-1">Examples</h3><div><h4 id="savesa440hzsinewavetoamonowavfile">Saves a 440 Hz sine wave to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> pi, sin
|
|
samples_f = (sin(i * <span class="hljs-number">2</span> * pi * <span class="hljs-number">440</span> / <span class="hljs-number">44100</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">100_000</span>))
|
|
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, samples_f)
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h4 id="addsnoisetothemonowavfile">Adds noise to the mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random
|
|
add_noise = <span class="hljs-keyword">lambda</span> value: value + (random() - <span class="hljs-number">0.5</span>) * <span class="hljs-number">0.03</span>
|
|
samples_f = (add_noise(f) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> read_wav_file(<span class="hljs-string">'test.wav'</span>))
|
|
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, samples_f)
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="playsthewavfile">Plays the WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
|
|
<span class="hljs-keyword">from</span> simpleaudio <span class="hljs-keyword">import</span> play_buffer
|
|
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'test.wav'</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
|
p = file.getparams()
|
|
frames = file.readframes(<span class="hljs-number">-1</span>)
|
|
play_buffer(frames, p.nchannels, p.sampwidth, p.framerate).wait_done()
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="texttospeech">Text to Speech</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pyttsx3</span>
|
|
<span class="hljs-keyword">import</span> pyttsx3
|
|
engine = pyttsx3.init()
|
|
engine.say(<span class="hljs-string">'Sally sells seashells by the seashore.'</span>)
|
|
engine.runAndWait()
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="synthesizer"><a href="#synthesizer" name="synthesizer">#</a>Synthesizer</h2><div><h4 id="playspopcornbygershonkingsley">Plays Popcorn by Gershon Kingsley:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
|
|
<span class="hljs-keyword">import</span> array, itertools <span class="hljs-keyword">as</span> it, math, simpleaudio
|
|
|
|
F = <span class="hljs-number">44100</span>
|
|
P1 = <span class="hljs-string">'71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,,71♩,69♪,,71♩,66♪,,62♩,66♪,,59♩,,,'</span>
|
|
P2 = <span class="hljs-string">'71♩,73♪,,74♩,73♪,,74♪,,71♪,,73♩,71♪,,73♪,,69♪,,71♩,69♪,,71♪,,67♪,,71♩,,,'</span>
|
|
get_pause = <span class="hljs-keyword">lambda</span> seconds: it.repeat(<span class="hljs-number">0</span>, int(seconds * F))
|
|
sin_f = <span class="hljs-keyword">lambda</span> i, hz: math.sin(i * <span class="hljs-number">2</span> * math.pi * hz / F)
|
|
get_wave = <span class="hljs-keyword">lambda</span> hz, seconds: (sin_f(i, hz) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(int(seconds * F)))
|
|
get_hz = <span class="hljs-keyword">lambda</span> note: <span class="hljs-number">8.176</span> * <span class="hljs-number">2</span> ** (int(note[:<span class="hljs-number">2</span>]) / <span class="hljs-number">12</span>)
|
|
get_sec = <span class="hljs-keyword">lambda</span> note: <span class="hljs-number">1</span>/<span class="hljs-number">4</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'♩'</span> <span class="hljs-keyword">in</span> note <span class="hljs-keyword">else</span> <span class="hljs-number">1</span>/<span class="hljs-number">8</span>
|
|
get_samples = <span class="hljs-keyword">lambda</span> note: get_wave(get_hz(note), get_sec(note)) <span class="hljs-keyword">if</span> note <span class="hljs-keyword">else</span> get_pause(<span class="hljs-number">1</span>/<span class="hljs-number">8</span>)
|
|
samples_f = it.chain.from_iterable(get_samples(n) <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> (P1+P2).split(<span class="hljs-string">','</span>))
|
|
samples_i = array.array(<span class="hljs-string">'h'</span>, (int(f * <span class="hljs-number">30000</span>) <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> samples_f))
|
|
simpleaudio.play_buffer(samples_i, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, F).wait_done()
|
|
</code></pre></div></div>
|
|
|
|
|
|
<div><h2 id="pygame"><a href="#pygame" name="pygame">#</a>Pygame</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pygame</span>
|
|
<span class="hljs-keyword">import</span> pygame <span class="hljs-keyword">as</span> pg
|
|
|
|
pg.init()
|
|
screen = pg.display.set_mode((<span class="hljs-number">500</span>, <span class="hljs-number">500</span>))
|
|
rect = pg.Rect(<span class="hljs-number">240</span>, <span class="hljs-number">240</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>)
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT):
|
|
deltas = {pg.K_UP: (<span class="hljs-number">0</span>, <span class="hljs-number">-20</span>), pg.K_RIGHT: (<span class="hljs-number">20</span>, <span class="hljs-number">0</span>), pg.K_DOWN: (<span class="hljs-number">0</span>, <span class="hljs-number">20</span>), pg.K_LEFT: (<span class="hljs-number">-20</span>, <span class="hljs-number">0</span>)}
|
|
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN):
|
|
dx, dy = deltas.get(event.key, (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
|
|
rect = rect.move((dx, dy))
|
|
screen.fill(pg.Color(<span class="hljs-string">'black'</span>))
|
|
pg.draw.rect(screen, pg.Color(<span class="hljs-string">'white'</span>), rect)
|
|
pg.display.flip()
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="rectangle">Rectangle</h3><p><strong>Object for storing rectangular coordinates.</strong></p><pre><code class="python language-python hljs"><Rect> = pg.Rect(x, y, width, height) <span class="hljs-comment"># Floats get truncated into ints.</span>
|
|
<int> = <Rect>.x/y/centerx/centery/… <span class="hljs-comment"># Top, right, bottom, left. Allows assignments.</span>
|
|
<tup.> = <Rect>.topleft/center/… <span class="hljs-comment"># Topright, bottomright, bottomleft. Same.</span>
|
|
<Rect> = <Rect>.move((delta_x, delta_y)) <span class="hljs-comment"># Use move_ip() to move in-place.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><bool> = <Rect>.collidepoint((x, y)) <span class="hljs-comment"># Checks if rectangle contains the point.</span>
|
|
<bool> = <Rect>.colliderect(<Rect>) <span class="hljs-comment"># Checks if the two rectangles overlap.</span>
|
|
<int> = <Rect>.collidelist(<list_of_Rect>) <span class="hljs-comment"># Returns index of first colliding Rect or -1.</span>
|
|
<list> = <Rect>.collidelistall(<list_of_Rect>) <span class="hljs-comment"># Returns indexes of all colliding rectangles.</span>
|
|
</code></pre>
|
|
<div><h3 id="surface">Surface</h3><p><strong>Object for representing images.</strong></p><pre><code class="python language-python hljs"><Surf> = pg.display.set_mode((width, height)) <span class="hljs-comment"># Opens new window and returns its surface.</span>
|
|
<Surf> = pg.Surface((width, height)) <span class="hljs-comment"># New RGB surface. RGBA if `flags=pg.SRCALPHA`.</span>
|
|
<Surf> = pg.image.load(<path/file>) <span class="hljs-comment"># Loads the image. Format depends on source.</span>
|
|
<Surf> = pg.surfarray.make_surface(<np_array>) <span class="hljs-comment"># Also `<np_arr> = surfarray.pixels3d(<Surf>)`.</span>
|
|
<Surf> = <Surf>.subsurface(<Rect>) <span class="hljs-comment"># Creates a new surface from the cutout.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><Surf>.fill(color) <span class="hljs-comment"># Tuple, Color('#rrggbb[aa]') or Color(<name>).</span>
|
|
<Surf>.set_at((x, y), color) <span class="hljs-comment"># Updates pixel. Also <Surf>.get_at((x, y)).</span>
|
|
<Surf>.blit(<Surf>, (x, y)) <span class="hljs-comment"># Draws passed surface at specified location.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.transform <span class="hljs-keyword">import</span> scale, ...
|
|
<Surf> = scale(<Surf>, (width, height)) <span class="hljs-comment"># Returns scaled surface.</span>
|
|
<Surf> = rotate(<Surf>, anticlock_degrees) <span class="hljs-comment"># Returns rotated and scaled surface.</span>
|
|
<Surf> = flip(<Surf>, x_bool, y_bool) <span class="hljs-comment"># Returns flipped surface.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pygame.draw <span class="hljs-keyword">import</span> line, ...
|
|
line(<Surf>, color, (x1, y1), (x2, y2), width) <span class="hljs-comment"># Draws a line to the surface.</span>
|
|
arc(<Surf>, color, <Rect>, from_rad, to_rad) <span class="hljs-comment"># Also ellipse(<Surf>, color, <Rect>, width=0).</span>
|
|
rect(<Surf>, color, <Rect>, width=<span class="hljs-number">0</span>) <span class="hljs-comment"># Also polygon(<Surf>, color, points, width=0).</span>
|
|
</code></pre>
|
|
<div><h3 id="font">Font</h3><pre><code class="python language-python hljs"><Font> = pg.font.Font(<path/file>, size) <span class="hljs-comment"># Loads TTF file. Pass None for default font.</span>
|
|
<Surf> = <Font>.render(text, antialias, color) <span class="hljs-comment"># Background color can be specified at the end.</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="sound">Sound</h3><pre><code class="python language-python hljs"><Sound> = pg.mixer.Sound(<path/file/bytes>) <span class="hljs-comment"># WAV file or bytes/array of signed shorts.</span>
|
|
<Sound>.play/stop() <span class="hljs-comment"># Also set_volume(<float>), fadeout(msec).</span>
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="basicmariobrothersexample">Basic Mario Brothers Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> collections, dataclasses, enum, io, itertools <span class="hljs-keyword">as</span> it, pygame <span class="hljs-keyword">as</span> pg, urllib.request
|
|
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
|
|
|
P = collections.namedtuple(<span class="hljs-string">'P'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Position</span>
|
|
D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Direction</span>
|
|
W, H, MAX_S = <span class="hljs-number">50</span>, <span class="hljs-number">50</span>, P(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>) <span class="hljs-comment"># Width, Height, Max speed</span>
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_screen</span><span class="hljs-params">()</span>:</span>
|
|
pg.init()
|
|
<span class="hljs-keyword">return</span> pg.display.set_mode((W*<span class="hljs-number">16</span>, H*<span class="hljs-number">16</span>))
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_images</span><span class="hljs-params">()</span>:</span>
|
|
url = <span class="hljs-string">'https://gto76.github.io/python-cheatsheet/web/mario_bros.png'</span>
|
|
img = pg.image.load(io.BytesIO(urllib.request.urlopen(url).read()))
|
|
<span class="hljs-keyword">return</span> [img.subsurface(get_rect(x, <span class="hljs-number">0</span>)) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(img.get_width() // <span class="hljs-number">16</span>)]
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_mario</span><span class="hljs-params">()</span>:</span>
|
|
Mario = dataclasses.make_dataclass(<span class="hljs-string">'Mario'</span>, <span class="hljs-string">'rect spd facing_left frame_cycle'</span>.split())
|
|
<span class="hljs-keyword">return</span> Mario(get_rect(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), P(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-keyword">False</span>, it.cycle(range(<span class="hljs-number">3</span>)))
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_tiles</span><span class="hljs-params">()</span>:</span>
|
|
border = [(x, y) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(W) <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> range(H) <span class="hljs-keyword">if</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, W-<span class="hljs-number">1</span>] <span class="hljs-keyword">or</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, H-<span class="hljs-number">1</span>]]
|
|
platforms = [(randint(<span class="hljs-number">1</span>, W-<span class="hljs-number">2</span>), randint(<span class="hljs-number">2</span>, H-<span class="hljs-number">2</span>)) <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(W*H // <span class="hljs-number">10</span>)]
|
|
<span class="hljs-keyword">return</span> [get_rect(x, y) <span class="hljs-keyword">for</span> x, y <span class="hljs-keyword">in</span> border + platforms]
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_rect</span><span class="hljs-params">(x, y)</span>:</span>
|
|
<span class="hljs-keyword">return</span> pg.Rect(x*<span class="hljs-number">16</span>, y*<span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>)
|
|
run(get_screen(), get_images(), get_mario(), get_tiles())
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>
|
|
clock = pg.time.Clock()
|
|
pressed = set()
|
|
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> pg.event.get(pg.QUIT) <span class="hljs-keyword">and</span> clock.tick(<span class="hljs-number">28</span>):
|
|
keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
|
|
pressed |= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYDOWN)}
|
|
pressed -= {keys.get(e.key) <span class="hljs-keyword">for</span> e <span class="hljs-keyword">in</span> pg.event.get(pg.KEYUP)}
|
|
update_speed(mario, tiles, pressed)
|
|
update_position(mario, tiles)
|
|
draw(screen, images, mario, tiles)
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_speed</span><span class="hljs-params">(mario, tiles, pressed)</span>:</span>
|
|
x, y = mario.spd
|
|
x += <span class="hljs-number">2</span> * ((D.e <span class="hljs-keyword">in</span> pressed) - (D.w <span class="hljs-keyword">in</span> pressed))
|
|
x += (x < <span class="hljs-number">0</span>) - (x > <span class="hljs-number">0</span>)
|
|
y += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles) <span class="hljs-keyword">else</span> (D.n <span class="hljs-keyword">in</span> pressed) * <span class="hljs-number">-10</span>
|
|
mario.spd = P(x=max(-MAX_S.x, min(MAX_S.x, x)), y=max(-MAX_S.y, min(MAX_S.y, y)))
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update_position</span><span class="hljs-params">(mario, tiles)</span>:</span>
|
|
x, y = mario.rect.topleft
|
|
n_steps = max(abs(s) <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> mario.spd)
|
|
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(n_steps):
|
|
mario.spd = stop_on_collision(mario.spd, get_boundaries(mario.rect, tiles))
|
|
x, y = x + (mario.spd.x / n_steps), y + (mario.spd.y / n_steps)
|
|
mario.rect.topleft = x, y
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_boundaries</span><span class="hljs-params">(rect, tiles)</span>:</span>
|
|
deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
|
|
<span class="hljs-keyword">return</span> {d <span class="hljs-keyword">for</span> d, delta <span class="hljs-keyword">in</span> deltas.items() <span class="hljs-keyword">if</span> rect.move(delta).collidelist(tiles) != <span class="hljs-number">-1</span>}
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">stop_on_collision</span><span class="hljs-params">(spd, bounds)</span>:</span>
|
|
<span class="hljs-keyword">return</span> P(x=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.w <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x < <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.e <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.x > <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.x,
|
|
y=<span class="hljs-number">0</span> <span class="hljs-keyword">if</span> (D.n <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y < <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span> (D.s <span class="hljs-keyword">in</span> bounds <span class="hljs-keyword">and</span> spd.y > <span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> spd.y)
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">draw</span><span class="hljs-params">(screen, images, mario, tiles)</span>:</span>
|
|
screen.fill((<span class="hljs-number">85</span>, <span class="hljs-number">168</span>, <span class="hljs-number">255</span>))
|
|
mario.facing_left = mario.spd.x < <span class="hljs-number">0</span> <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> mario.facing_left
|
|
is_airborne = D.s <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> get_boundaries(mario.rect, tiles)
|
|
image_index = <span class="hljs-number">4</span> <span class="hljs-keyword">if</span> is_airborne <span class="hljs-keyword">else</span> (next(mario.frame_cycle) <span class="hljs-keyword">if</span> mario.spd.x <span class="hljs-keyword">else</span> <span class="hljs-number">6</span>)
|
|
screen.blit(images[image_index + mario.facing_left * <span class="hljs-number">9</span>], mario.rect)
|
|
<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tiles:
|
|
is_border = t.x <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (W-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>] <span class="hljs-keyword">or</span> t.y <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span>, (H-<span class="hljs-number">1</span>)*<span class="hljs-number">16</span>]
|
|
screen.blit(images[<span class="hljs-number">18</span> <span class="hljs-keyword">if</span> is_border <span class="hljs-keyword">else</span> <span class="hljs-number">19</span>], t)
|
|
pg.display.flip()
|
|
|
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
|
main()
|
|
</code></pre></div>
|
|
|
|
<div><h2 id="pandas"><a href="#pandas" name="pandas">#</a>Pandas</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pandas matplotlib</span>
|
|
<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd, matplotlib.pyplot <span class="hljs-keyword">as</span> plt
|
|
</code></pre></div>
|
|
|
|
<div><h3 id="series">Series</h3><p><strong>Ordered dictionary with a name.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>pd.Series([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], index=[<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>], name=<span class="hljs-string">'a'</span>)
|
|
x <span class="hljs-number">1</span>
|
|
y <span class="hljs-number">2</span>
|
|
Name: a, dtype: int64
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><Sr> = pd.Series(<list>) <span class="hljs-comment"># Assigns RangeIndex starting at 0.</span>
|
|
<Sr> = pd.Series(<dict>) <span class="hljs-comment"># Takes dictionary's keys for index.</span>
|
|
<Sr> = pd.Series(<dict/Series>, index=<list>) <span class="hljs-comment"># Only keeps items with keys specified in index.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><el> = <Sr>.loc[key] <span class="hljs-comment"># Or: <Sr>.iloc[i]</span>
|
|
<Sr> = <Sr>.loc[coll_of_keys] <span class="hljs-comment"># Or: <Sr>.iloc[coll_of_i]</span>
|
|
<Sr> = <Sr>.loc[from_key : to_key_inc] <span class="hljs-comment"># Or: <Sr>.iloc[from_i : to_i_exc]</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><el> = <Sr>[key/i] <span class="hljs-comment"># Or: <Sr>.<key></span>
|
|
<Sr> = <Sr>[coll_of_keys/coll_of_i] <span class="hljs-comment"># Or: <Sr>[key/i : key/i]</span>
|
|
<Sr> = <Sr>[bools] <span class="hljs-comment"># Or: <Sr>.loc/iloc[bools]</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Sr> = <Sr> > <el/Sr> <span class="hljs-comment"># Returns a Series of bools.</span>
|
|
<Sr> = <Sr> + <el/Sr> <span class="hljs-comment"># Items with non-matching keys get value NaN.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Sr> = pd.concat(<coll_of_Sr>) <span class="hljs-comment"># Concats multiple series into one long Series.</span>
|
|
<Sr> = <Sr>.combine_first(<Sr>) <span class="hljs-comment"># Adds items that are not yet present.</span>
|
|
<Sr>.update(<Sr>) <span class="hljs-comment"># Updates items that are already present.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Sr>.plot.line/area/bar/pie/hist() <span class="hljs-comment"># Generates a Matplotlib plot.</span>
|
|
plt.show() <span class="hljs-comment"># Displays the plot. Also plt.savefig(<path>).</span>
|
|
</code></pre>
|
|
<div><h4 id="seriesaggregatetransformmap">Series — Aggregate, Transform, Map:</h4><pre><code class="python language-python hljs"><el> = <Sr>.sum/max/mean/idxmax/all() <span class="hljs-comment"># Or: <Sr>.agg(lambda <Sr>: <el>)</span>
|
|
<Sr> = <Sr>.rank/diff/cumsum/ffill/interpo…() <span class="hljs-comment"># Or: <Sr>.agg/transform(lambda <Sr>: <Sr>)</span>
|
|
<Sr> = <Sr>.fillna(<el>) <span class="hljs-comment"># Or: <Sr>.agg/transform/map(lambda <el>: <el>)</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>sr = pd.Series([<span class="hljs-number">2</span>, <span class="hljs-number">3</span>], index=[<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
|
|
x <span class="hljs-number">2</span>
|
|
y <span class="hljs-number">3</span>
|
|
</code></pre>
|
|
<pre><code class="python hljs">┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'sum'</span> │ [<span class="hljs-string">'sum'</span>] │ {<span class="hljs-string">'s'</span>: <span class="hljs-string">'sum'</span>} ┃
|
|
┠───────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ sr.apply(…) │ <span class="hljs-number">5</span> │ sum <span class="hljs-number">5</span> │ s <span class="hljs-number">5</span> ┃
|
|
┃ sr.agg(…) │ │ │ ┃
|
|
┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
|
|
|
|
┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'rank'</span> │ [<span class="hljs-string">'rank'</span>] │ {<span class="hljs-string">'r'</span>: <span class="hljs-string">'rank'</span>} ┃
|
|
┠───────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ sr.apply(…) │ │ rank │ ┃
|
|
┃ sr.agg(…) │ x <span class="hljs-number">1</span> │ x <span class="hljs-number">1</span> │ r x <span class="hljs-number">1</span> ┃
|
|
┃ │ y <span class="hljs-number">2</span> │ y <span class="hljs-number">2</span> │ y <span class="hljs-number">2</span> ┃
|
|
┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
|
|
<ul>
|
|
<li><strong>Indexing objects can't be tuples because <code class="python hljs"><span class="hljs-string">'obj[x, y]'</span></code> is converted to <code class="python hljs"><span class="hljs-string">'obj[(x, y)]'</span></code>!</strong></li>
|
|
<li><strong>Methods ffill(), interpolate(), fillna() and dropna() accept <code class="python hljs"><span class="hljs-string">'inplace=True'</span></code>.</strong></li>
|
|
<li><strong>Last result has a hierarchical index. Use <code class="python hljs"><span class="hljs-string">'<Sr>[key_1, key_2]'</span></code> to get its values.</strong></li>
|
|
</ul>
|
|
<div><h3 id="dataframe">DataFrame</h3><p><strong>Table with labeled rows and columns.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>pd.DataFrame([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], index=[<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>], columns=[<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
|
|
x y
|
|
a <span class="hljs-number">1</span> <span class="hljs-number">2</span>
|
|
b <span class="hljs-number">3</span> <span class="hljs-number">4</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><DF> = pd.DataFrame(<list_of_rows>) <span class="hljs-comment"># Rows can be either lists, dicts or series.</span>
|
|
<DF> = pd.DataFrame(<dict_of_columns>) <span class="hljs-comment"># Columns can be either lists, dicts or series.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><el> = <DF>.loc[row_key, col_key] <span class="hljs-comment"># Or: <DF>.iloc[row_i, col_i]</span>
|
|
<Sr/DF> = <DF>.loc[row_key/s] <span class="hljs-comment"># Or: <DF>.iloc[row_i/s]</span>
|
|
<Sr/DF> = <DF>.loc[:, col_key/s] <span class="hljs-comment"># Or: <DF>.iloc[:, col_i/s]</span>
|
|
<DF> = <DF>.loc[row_bools, col_bools] <span class="hljs-comment"># Or: <DF>.iloc[row_bools, col_bools]</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><Sr/DF> = <DF>[col_key/s] <span class="hljs-comment"># Or: <DF>.<col_key></span>
|
|
<DF> = <DF>[row_bools] <span class="hljs-comment"># Keeps rows as specified by bools.</span>
|
|
<DF> = <DF>[<DF_of_bools>] <span class="hljs-comment"># Assigns NaN to items that are False in bools.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><DF> = <DF> > <el/Sr/DF> <span class="hljs-comment"># Returns DF of bools. Sr is treated as a row.</span>
|
|
<DF> = <DF> + <el/Sr/DF> <span class="hljs-comment"># Items with non-matching keys get value NaN.</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><DF> = <DF>.set_index(col_key) <span class="hljs-comment"># Replaces row keys with column's values.</span>
|
|
<DF> = <DF>.reset_index(drop=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Drops or moves row keys to column named index.</span>
|
|
<DF> = <DF>.sort_index(ascending=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Sorts rows by row keys. Use `axis=1` for cols.</span>
|
|
<DF> = <DF>.sort_values(col_key/s) <span class="hljs-comment"># Sorts rows by passed column/s. Also `axis=1`.</span>
|
|
</code></pre>
|
|
<div><h4 id="dataframemergejoinconcat">DataFrame — Merge, Join, Concat:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>l = pd.DataFrame([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], index=[<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>], columns=[<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
|
|
x y
|
|
a <span class="hljs-number">1</span> <span class="hljs-number">2</span>
|
|
b <span class="hljs-number">3</span> <span class="hljs-number">4</span>
|
|
<span class="hljs-meta">>>> </span>r = pd.DataFrame([[<span class="hljs-number">4</span>, <span class="hljs-number">5</span>], [<span class="hljs-number">6</span>, <span class="hljs-number">7</span>]], index=[<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>], columns=[<span class="hljs-string">'y'</span>, <span class="hljs-string">'z'</span>])
|
|
y z
|
|
b <span class="hljs-number">4</span> <span class="hljs-number">5</span>
|
|
c <span class="hljs-number">6</span> <span class="hljs-number">7</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python hljs">┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'outer'</span> │ <span class="hljs-string">'inner'</span> │ <span class="hljs-string">'left'</span> │ Description ┃
|
|
┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨
|
|
┃ l.merge(r, on=<span class="hljs-string">'y'</span>, │ x y z │ x y z │ x y z │ Merges on column if <span class="hljs-string">'on'</span> ┃
|
|
┃ how=…) │ <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> . │ <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ <span class="hljs-number">1</span> <span class="hljs-number">2</span> . │ or <span class="hljs-string">'left/right_on'</span> are ┃
|
|
┃ │ <span class="hljs-number">1</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ │ <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ set, else on shared cols.┃
|
|
┃ │ <span class="hljs-number">2</span> . <span class="hljs-number">6</span> <span class="hljs-number">7</span> │ │ │ Uses <span class="hljs-string">'inner'</span> by default. ┃
|
|
┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨
|
|
┃ l.join(r, lsuffix=<span class="hljs-string">'l'</span>, │ x yl yr z │ │ x yl yr z │ Merges on row keys. ┃
|
|
┃ rsuffix=<span class="hljs-string">'r'</span>, │ a <span class="hljs-number">1</span> <span class="hljs-number">2</span> . . │ x yl yr z │ <span class="hljs-number">1</span> <span class="hljs-number">2</span> . . │ Uses <span class="hljs-string">'left'</span> by default. ┃
|
|
┃ how=…) │ b <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ If r is a Series, it is ┃
|
|
┃ │ c . . <span class="hljs-number">6</span> <span class="hljs-number">7</span> │ │ │ treated as a column. ┃
|
|
┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨
|
|
┃ pd.concat([l, r], │ x y z │ y │ │ Adds rows at the bottom. ┃
|
|
┃ axis=<span class="hljs-number">0</span>, │ a <span class="hljs-number">1</span> <span class="hljs-number">2</span> . │ <span class="hljs-number">2</span> │ │ Uses <span class="hljs-string">'outer'</span> by default. ┃
|
|
┃ join=…) │ b <span class="hljs-number">3</span> <span class="hljs-number">4</span> . │ <span class="hljs-number">4</span> │ │ A Series is treated as a ┃
|
|
┃ │ b . <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ <span class="hljs-number">4</span> │ │ column. To add a row use ┃
|
|
┃ │ c . <span class="hljs-number">6</span> <span class="hljs-number">7</span> │ <span class="hljs-number">6</span> │ │ pd.concat([l, DF([sr])]).┃
|
|
┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨
|
|
┃ pd.concat([l, r], │ x y y z │ │ │ Adds columns at the ┃
|
|
┃ axis=<span class="hljs-number">1</span>, │ a <span class="hljs-number">1</span> <span class="hljs-number">2</span> . . │ x y y z │ │ right end. Uses <span class="hljs-string">'outer'</span> ┃
|
|
┃ join=…) │ b <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ │ by default. A Series is ┃
|
|
┃ │ c . . <span class="hljs-number">6</span> <span class="hljs-number">7</span> │ │ │ treated as a column. ┃
|
|
┠────────────────────────┼───────────────┼────────────┼────────────┼──────────────────────────┨
|
|
┃ l.combine_first(r) │ x y z │ │ │ Adds missing rows and ┃
|
|
┃ │ a <span class="hljs-number">1</span> <span class="hljs-number">2</span> . │ │ │ columns. Also updates ┃
|
|
┃ │ b <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> │ │ │ items that contain NaN. ┃
|
|
┃ │ c . <span class="hljs-number">6</span> <span class="hljs-number">7</span> │ │ │ Argument r must be a DF. ┃
|
|
┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
<div><h4 id="dataframeaggregatetransformmap">DataFrame — Aggregate, Transform, Map:</h4><pre><code class="python language-python hljs"><Sr> = <DF>.sum/max/mean/idxmax/all() <span class="hljs-comment"># Or: <DF>.apply/agg(lambda <Sr>: <el>)</span>
|
|
<DF> = <DF>.rank/diff/cumsum/ffill/interpo…() <span class="hljs-comment"># Or: <DF>.apply/agg/transfo…(lambda <Sr>: <Sr>)</span>
|
|
<DF> = <DF>.fillna(<el>) <span class="hljs-comment"># Or: <DF>.applymap(lambda <el>: <el>)</span>
|
|
</code></pre></div>
|
|
|
|
<ul>
|
|
<li><strong>All operations operate on columns by default. Pass <code class="python hljs"><span class="hljs-string">'axis=1'</span></code> to process the rows instead.</strong></li>
|
|
</ul>
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>df = pd.DataFrame([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], index=[<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>], columns=[<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
|
|
x y
|
|
a <span class="hljs-number">1</span> <span class="hljs-number">2</span>
|
|
b <span class="hljs-number">3</span> <span class="hljs-number">4</span>
|
|
</code></pre>
|
|
<pre><code class="python hljs">┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'sum'</span> │ [<span class="hljs-string">'sum'</span>] │ {<span class="hljs-string">'x'</span>: <span class="hljs-string">'sum'</span>} ┃
|
|
┠─────────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ df.apply(…) │ x <span class="hljs-number">4</span> │ x y │ x <span class="hljs-number">4</span> ┃
|
|
┃ df.agg(…) │ y <span class="hljs-number">6</span> │ sum <span class="hljs-number">4</span> <span class="hljs-number">6</span> │ ┃
|
|
┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
|
|
|
|
┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'rank'</span> │ [<span class="hljs-string">'rank'</span>] │ {<span class="hljs-string">'x'</span>: <span class="hljs-string">'rank'</span>} ┃
|
|
┠─────────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ df.apply(…) │ │ x y │ ┃
|
|
┃ df.agg(…) │ x y │ rank rank │ x ┃
|
|
┃ df.transform(…) │ a <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ a <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ a <span class="hljs-number">1</span> ┃
|
|
┃ │ b <span class="hljs-number">2</span> <span class="hljs-number">2</span> │ b <span class="hljs-number">2</span> <span class="hljs-number">2</span> │ b <span class="hljs-number">2</span> ┃
|
|
┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
|
|
<ul>
|
|
<li><strong>Use <code class="python hljs"><span class="hljs-string">'<DF>[col_key_1, col_key_2][row_key]'</span></code> to get the fifth result's values.</strong></li>
|
|
</ul>
|
|
<div><h4 id="dataframeplotencodedecode">DataFrame — Plot, Encode, Decode:</h4><pre><code class="python language-python hljs"><DF>.plot.line/area/bar/scatter(x=col_key, …) <span class="hljs-comment"># `y=col_key/s`. Also hist/box(by=col_key).</span>
|
|
plt.show() <span class="hljs-comment"># Displays the plot. Also plt.savefig(<path>).</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><DF> = pd.read_json/html(<span class="hljs-string">'<str/path/url>'</span>) <span class="hljs-comment"># Run `$ pip3 install beautifulsoup4 lxml`.</span>
|
|
<DF> = pd.read_csv(<span class="hljs-string">'<path/url>'</span>) <span class="hljs-comment"># `header/index_col/dtype/parse_dates/…=<obj>`.</span>
|
|
<DF> = pd.read_pickle/excel(<span class="hljs-string">'<path/url>'</span>) <span class="hljs-comment"># Use `sheet_name=None` to get all Excel sheets.</span>
|
|
<DF> = pd.read_sql(<span class="hljs-string">'<table/query>'</span>, <conn.>) <span class="hljs-comment"># SQLite3/SQLAlchemy connection (see #SQLite).</span>
|
|
</code></pre>
|
|
<pre><code class="python language-python hljs"><dict> = <DF>.to_dict(<span class="hljs-string">'d/l/s/…'</span>) <span class="hljs-comment"># Returns columns as dicts, lists or series.</span>
|
|
<str> = <DF>.to_json/html/csv/latex() <span class="hljs-comment"># Saves output to file if path is passed.</span>
|
|
<DF>.to_pickle/excel(<path>) <span class="hljs-comment"># Run `$ pip3 install "pandas[excel]" odfpy`.</span>
|
|
<DF>.to_sql(<span class="hljs-string">'<table_name>'</span>, <connection>) <span class="hljs-comment"># Also `if_exists='fail/replace/append'`.</span>
|
|
</code></pre>
|
|
<div><h3 id="groupby">GroupBy</h3><p><strong>Object that groups together rows of a dataframe based on the value of the passed column.</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>df = pd.DataFrame([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">6</span>]], list(<span class="hljs-string">'abc'</span>), list(<span class="hljs-string">'xyz'</span>))
|
|
<span class="hljs-meta">>>> </span>df.groupby(<span class="hljs-string">'z'</span>).get_group(<span class="hljs-number">6</span>)
|
|
x y z
|
|
b <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span>
|
|
c <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">6</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><GB> = <DF>.groupby(column_key/s) <span class="hljs-comment"># Splits DF into groups based on passed column.</span>
|
|
<DF> = <GB>.apply(<func>) <span class="hljs-comment"># Maps each group. Func can return DF, Sr or el.</span>
|
|
<GB> = <GB>[column_key] <span class="hljs-comment"># Single column GB. All operations return a Sr.</span>
|
|
<Sr> = <GB>.size() <span class="hljs-comment"># A Sr of group sizes. Same keys as get_group().</span>
|
|
</code></pre>
|
|
<div><h4 id="groupbyaggregatetransformmap">GroupBy — Aggregate, Transform, Map:</h4><pre><code class="python language-python hljs"><DF> = <GB>.sum/max/mean/idxmax/all() <span class="hljs-comment"># Or: <GB>.agg(lambda <Sr>: <el>)</span>
|
|
<DF> = <GB>.rank/diff/cumsum/ffill() <span class="hljs-comment"># Or: <GB>.transform(lambda <Sr>: <Sr>)</span>
|
|
<DF> = <GB>.fillna(<el>) <span class="hljs-comment"># Or: <GB>.transform(lambda <Sr>: <Sr>)</span>
|
|
</code></pre></div>
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>gb = df.groupby(<span class="hljs-string">'z'</span>); gb.apply(print)
|
|
x y z
|
|
a <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span>
|
|
x y z
|
|
b <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span>
|
|
c <span class="hljs-number">7</span> <span class="hljs-number">8</span> <span class="hljs-number">6</span></code></pre>
|
|
<pre><code class="python hljs">┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┓
|
|
┃ │ <span class="hljs-string">'sum'</span> │ <span class="hljs-string">'rank'</span> │ [<span class="hljs-string">'rank'</span>] │ {<span class="hljs-string">'x'</span>: <span class="hljs-string">'rank'</span>} ┃
|
|
┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ gb.agg(…) │ x y │ │ x y │ ┃
|
|
┃ │ z │ x y │ rank rank │ x ┃
|
|
┃ │ <span class="hljs-number">3</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> │ a <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ a <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ a <span class="hljs-number">1</span> ┃
|
|
┃ │ <span class="hljs-number">6</span> <span class="hljs-number">11</span> <span class="hljs-number">13</span> │ b <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ b <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ b <span class="hljs-number">1</span> ┃
|
|
┃ │ │ c <span class="hljs-number">2</span> <span class="hljs-number">2</span> │ c <span class="hljs-number">2</span> <span class="hljs-number">2</span> │ c <span class="hljs-number">2</span> ┃
|
|
┠─────────────────┼─────────────┼─────────────┼─────────────┼───────────────┨
|
|
┃ gb.transform(…) │ x y │ x y │ │ ┃
|
|
┃ │ a <span class="hljs-number">1</span> <span class="hljs-number">2</span> │ a <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ │ ┃
|
|
┃ │ b <span class="hljs-number">11</span> <span class="hljs-number">13</span> │ b <span class="hljs-number">1</span> <span class="hljs-number">1</span> │ │ ┃
|
|
┃ │ c <span class="hljs-number">11</span> <span class="hljs-number">13</span> │ c <span class="hljs-number">2</span> <span class="hljs-number">2</span> │ │ ┃
|
|
┗━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┛
|
|
</code></pre>
|
|
<div><h3 id="rolling">Rolling</h3><p><strong>Object for rolling window calculations.</strong></p><pre><code class="python language-python hljs"><RSr/RDF/RGB> = <Sr/DF/GB>.rolling(win_size) <span class="hljs-comment"># Also: `min_periods=None, center=False`.</span>
|
|
<RSr/RDF/RGB> = <RDF/RGB>[column_key/s] <span class="hljs-comment"># Or: <RDF/RGB>.column_key</span>
|
|
<Sr/DF> = <R>.mean/sum/max() <span class="hljs-comment"># Or: <R>.apply/agg(<agg_func/str>)</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h2 id="plotly"><a href="#plotly" name="plotly">#</a>Plotly</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pandas plotly kaleido</span>
|
|
<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd, plotly.express <span class="hljs-keyword">as</span> ex
|
|
<Figure> = ex.line(<DF>, x=<col_name>, y=<col_name>) <span class="hljs-comment"># Or: ex.line(x=<list>, y=<list>)</span>
|
|
<Figure>.update_layout(margin=dict(t=<span class="hljs-number">0</span>, r=<span class="hljs-number">0</span>, b=<span class="hljs-number">0</span>, l=<span class="hljs-number">0</span>), …) <span class="hljs-comment"># `paper_bgcolor='rgb(0, 0, 0)'`.</span>
|
|
<Figure>.write_html/json/image(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also <Figure>.show().</span>
|
|
</code></pre></div>
|
|
|
|
<div><h4 id="displaysalinechartoftotalcoronavirusdeathspermilliongroupedbycontinent">Displays a line chart of total coronavirus deaths per million grouped by continent:</h4><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:312px; width:914px;"></div><pre><code class="python language-python hljs">covid = pd.read_csv(<span class="hljs-string">'https://raw.githubusercontent.com/owid/covid-19-data/8dde8ca49b'</span>
|
|
<span class="hljs-string">'6e648c17dd420b2726ca0779402651/public/data/owid-covid-data.csv'</span>,
|
|
usecols=[<span class="hljs-string">'iso_code'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'total_deaths'</span>, <span class="hljs-string">'population'</span>])
|
|
continents = pd.read_csv(<span class="hljs-string">'https://gto76.github.io/python-cheatsheet/web/continents.csv'</span>,
|
|
usecols=[<span class="hljs-string">'Three_Letter_Country_Code'</span>, <span class="hljs-string">'Continent_Name'</span>])
|
|
df = pd.merge(covid, continents, left_on=<span class="hljs-string">'iso_code'</span>, right_on=<span class="hljs-string">'Three_Letter_Country_Code'</span>)
|
|
df = df.groupby([<span class="hljs-string">'Continent_Name'</span>, <span class="hljs-string">'date'</span>]).sum().reset_index()
|
|
df[<span class="hljs-string">'Total Deaths per Million'</span>] = df.total_deaths * <span class="hljs-number">1e6</span> / df.population
|
|
df = df[df.date > <span class="hljs-string">'2020-03-14'</span>]
|
|
df = df.rename({<span class="hljs-string">'date'</span>: <span class="hljs-string">'Date'</span>, <span class="hljs-string">'Continent_Name'</span>: <span class="hljs-string">'Continent'</span>}, axis=<span class="hljs-string">'columns'</span>)
|
|
ex.line(df, x=<span class="hljs-string">'Date'</span>, y=<span class="hljs-string">'Total Deaths per Million'</span>, color=<span class="hljs-string">'Continent'</span>).show()
|
|
</code></pre></div>
|
|
|
|
|
|
|
|
<div><h4 id="displaysamultiaxislinechartoftotalcoronaviruscasesandchangesinpricesofbitcoindowjonesandgold">Displays a multi-axis line chart of total coronavirus cases and changes in prices of Bitcoin, Dow Jones and gold:</h4><p></p><div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:287px; width:935px;"></div><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd, plotly.graph_objects <span class="hljs-keyword">as</span> go
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
|
covid, bitcoin, gold, dow = scrape_data()
|
|
display_data(wrangle_data(covid, bitcoin, gold, dow))
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">scrape_data</span><span class="hljs-params">()</span>:</span>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_covid_cases</span><span class="hljs-params">()</span>:</span>
|
|
url = <span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>
|
|
df = pd.read_csv(url, usecols=[<span class="hljs-string">'location'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'total_cases'</span>])
|
|
<span class="hljs-keyword">return</span> df[df.location == <span class="hljs-string">'World'</span>].set_index(<span class="hljs-string">'date'</span>).total_cases
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_ticker</span><span class="hljs-params">(symbol)</span>:</span>
|
|
url = (<span class="hljs-string">f'https://query1.finance.yahoo.com/v7/finance/download/<span class="hljs-subst">{symbol}</span>?'</span>
|
|
<span class="hljs-string">'period1=1579651200&period2=9999999999&interval=1d&events=history'</span>)
|
|
df = pd.read_csv(url, usecols=[<span class="hljs-string">'Date'</span>, <span class="hljs-string">'Close'</span>])
|
|
<span class="hljs-keyword">return</span> df.set_index(<span class="hljs-string">'Date'</span>).Close
|
|
out = get_covid_cases(), get_ticker(<span class="hljs-string">'BTC-USD'</span>), get_ticker(<span class="hljs-string">'GC=F'</span>), get_ticker(<span class="hljs-string">'^DJI'</span>)
|
|
<span class="hljs-keyword">return</span> map(pd.Series.rename, out, [<span class="hljs-string">'Total Cases'</span>, <span class="hljs-string">'Bitcoin'</span>, <span class="hljs-string">'Gold'</span>, <span class="hljs-string">'Dow Jones'</span>])
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrangle_data</span><span class="hljs-params">(covid, bitcoin, gold, dow)</span>:</span>
|
|
df = pd.concat([bitcoin, gold, dow], axis=<span class="hljs-number">1</span>) <span class="hljs-comment"># Creates table by joining columns on dates.</span>
|
|
df = df.sort_index().interpolate() <span class="hljs-comment"># Sorts table by date and interpolates NaN-s.</span>
|
|
df = df.loc[<span class="hljs-string">'2020-02-23'</span>:] <span class="hljs-comment"># Discards rows before '2020-02-23'.</span>
|
|
df = (df / df.iloc[<span class="hljs-number">0</span>]) * <span class="hljs-number">100</span> <span class="hljs-comment"># Calculates percentages relative to day 1.</span>
|
|
df = df.join(covid) <span class="hljs-comment"># Adds column with covid cases.</span>
|
|
<span class="hljs-keyword">return</span> df.sort_values(df.index[<span class="hljs-number">-1</span>], axis=<span class="hljs-number">1</span>) <span class="hljs-comment"># Sorts columns by last day's value.</span>
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display_data</span><span class="hljs-params">(df)</span>:</span>
|
|
figure = go.Figure()
|
|
<span class="hljs-keyword">for</span> col_name <span class="hljs-keyword">in</span> reversed(df.columns):
|
|
yaxis = <span class="hljs-string">'y1'</span> <span class="hljs-keyword">if</span> col_name == <span class="hljs-string">'Total Cases'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'y2'</span>
|
|
trace = go.Scatter(x=df.index, y=df[col_name], name=col_name, yaxis=yaxis)
|
|
figure.add_trace(trace)
|
|
figure.update_layout(
|
|
yaxis1=dict(title=<span class="hljs-string">'Total Cases'</span>, rangemode=<span class="hljs-string">'tozero'</span>),
|
|
yaxis2=dict(title=<span class="hljs-string">'%'</span>, rangemode=<span class="hljs-string">'tozero'</span>, overlaying=<span class="hljs-string">'y'</span>, side=<span class="hljs-string">'right'</span>),
|
|
legend=dict(x=<span class="hljs-number">1.08</span>),
|
|
width=<span class="hljs-number">944</span>,
|
|
height=<span class="hljs-number">423</span>
|
|
)
|
|
figure.show()
|
|
|
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
|
main()
|
|
</code></pre></div>
|
|
|
|
|
|
|
|
<div><h2 id="appendix"><a href="#appendix" name="appendix">#</a>Appendix</h2><div><h3 id="cython">Cython</h3><p><strong>Library that compiles Python code into C.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install cython</span>
|
|
<span class="hljs-keyword">import</span> pyximport; pyximport.install()
|
|
<span class="hljs-keyword">import</span> <cython_script>
|
|
<cython_script>.main()
|
|
</code></pre></div></div>
|
|
|
|
|
|
|
|
<div><h4 id="definitions">Definitions:</h4><ul>
|
|
<li><strong>All <code class="python hljs"><span class="hljs-string">'cdef'</span></code> definitions are optional, but they contribute to the speed-up.</strong></li>
|
|
<li><strong>Script needs to be saved with a <code class="python hljs"><span class="hljs-string">'pyx'</span></code> extension.</strong></li>
|
|
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">cdef</span> <ctype> <var_name> = <obj>
|
|
<span class="hljs-keyword">cdef</span> <ctype>[n_elements] <var_name> = [<el_1>, <el_2>, ...]
|
|
<span class="hljs-keyword">cdef</span> <ctype/void> <func_name>(<ctype> <arg_name>): ...
|
|
</code></pre></div>
|
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">cdef</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">class_name</span>>:</span>
|
|
<span class="hljs-keyword">cdef</span> <span class="hljs-keyword">public</span> <ctype> <attr_name>
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, <ctype> <arg_name>)</span>:</span>
|
|
self.<attr_name> = <arg_name>
|
|
</code></pre>
|
|
<div><h3 id="virtualenvironments">Virtual Environments</h3><p><strong>System for installing libraries directly into project's directory.</strong></p><pre><code class="bash language-bash hljs">$ python3 -m venv NAME <span class="hljs-comment"># Creates virtual environment in current directory.</span>
|
|
$ <span class="hljs-built_in">source</span> NAME/bin/activate <span class="hljs-comment"># Activates env. On Windows run `NAME\Scripts\activate`.</span>
|
|
$ pip3 install LIBRARY <span class="hljs-comment"># Installs the library into active environment.</span>
|
|
$ python3 FILE <span class="hljs-comment"># Runs the script in active environment. Also `./FILE`.</span>
|
|
$ deactivate <span class="hljs-comment"># Deactivates the active virtual environment.</span>
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h3 id="basicscripttemplate">Basic Script Template</h3><p><strong>Run the script with <code class="python hljs"><span class="hljs-string">'$ python3 FILE'</span></code> or <code class="python hljs"><span class="hljs-string">'$ chmod u+x FILE; ./FILE'</span></code>. To automatically start the debugger when uncaught exception occurs run <code class="python hljs"><span class="hljs-string">'$ python3 -m pdb -cc FILE'</span></code>.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment">#!/usr/bin/env python3</span>
|
|
<span class="hljs-comment">#</span>
|
|
<span class="hljs-comment"># Usage: .py</span>
|
|
<span class="hljs-comment">#</span>
|
|
|
|
<span class="hljs-keyword">from</span> sys <span class="hljs-keyword">import</span> argv, exit
|
|
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict, namedtuple
|
|
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
|
|
<span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
|
|
<span class="hljs-keyword">import</span> functools <span class="hljs-keyword">as</span> ft, itertools <span class="hljs-keyword">as</span> it, operator <span class="hljs-keyword">as</span> op, re
|
|
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
|
<span class="hljs-keyword">pass</span>
|
|
|
|
|
|
<span class="hljs-comment">###</span>
|
|
<span class="hljs-comment">## UTIL</span>
|
|
<span class="hljs-comment">#</span>
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_file</span><span class="hljs-params">(filename)</span>:</span>
|
|
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
|
|
<span class="hljs-keyword">return</span> file.readlines()
|
|
|
|
|
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
|
|
main()
|
|
</code></pre></div>
|
|
|
|
|
|
<div><h2 id="index"><a href="#index" name="index">#</a>Index</h2><ul><li><strong>Only available in the <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">PDF</a>.</strong></li>
|
|
<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>
|
|
<li><strong>Searching <code class="python hljs"><span class="hljs-string">'#<title>'</span></code> will limit the search to the titles.</strong></li>
|
|
</ul></div>
|
|
|
|
|
|
<footer>
|
|
<aside>September 8, 2024</aside>
|
|
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
|
</footer>
|
|
|
|
<a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a>
|
|
|
|
<script src="web/jquery-3.4.0.min.js"></script>
|
|
<script src="web/script_2.js"></script>
|
|
<script type="text/javascript" src="https://transactions.sendowl.com/assets/sendowl.js" ></script>
|
|
<script src="web/plotly.min.js"></script>
|
|
<script src="web/covid_deaths.js"></script>
|
|
<script src="web/covid_cases.js"></script>
|
|
</body>
|
|
|
|
</html>
|