|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>June 9, 2023</aside> |
| 57 | + <aside>June 30, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
1609 | 1609 | <span class="hljs-keyword">with</span> <conn>.begin(): ... <span class="hljs-comment"># Exits the block with commit or rollback.</span>
|
1610 | 1610 | </code></pre></div>
|
1611 | 1611 |
|
1612 |
| -<pre><code class="text language-text">┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
1613 |
| -┃ Dialect │ pip3 install │ import │ Dependencies ┃ |
1614 |
| -┠────────────┼──────────────┼───────────┼──────────────────────────────────┨ |
1615 |
| -┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃ |
1616 |
| -┃ postgresql │ psycopg2 │ psycopg2 │ www.pypi.org/project/psycopg2 ┃ |
1617 |
| -┃ mssql │ pyodbc │ pyodbc │ www.pypi.org/project/pyodbc ┃ |
1618 |
| -┃ oracle │ oracledb │ oracledb │ www.pypi.org/project/oracledb ┃ |
1619 |
| -┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 1612 | +<pre><code class="text language-text">┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 1613 | +┃ Dialect │ pip3 install │ import │ Dependencies ┃ |
| 1614 | +┠────────────┼──────────────┼──────────┼──────────────────────────────────┨ |
| 1615 | +┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃ |
| 1616 | +┃ postgresql │ psycopg2 │ psycopg2 │ www.pypi.org/project/psycopg2 ┃ |
| 1617 | +┃ mssql │ pyodbc │ pyodbc │ www.pypi.org/project/pyodbc ┃ |
| 1618 | +┃ oracle │ oracledb │ oracledb │ www.pypi.org/project/oracledb ┃ |
| 1619 | +┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
1620 | 1620 | </code></pre>
|
1621 | 1621 | <div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>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>
|
1622 | 1622 | <int> = <bytes>[<index>] <span class="hljs-comment"># Returns an int in range from 0 to 255.</span>
|
@@ -2124,44 +2124,43 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2124 | 2124 | </code></pre></div>
|
2125 | 2125 |
|
2126 | 2126 | <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
|
2127 |
| -<span class="hljs-meta">>>> </span>timeit(<span class="hljs-string">"''.join(str(i) for i in range(100))"</span>, |
2128 |
| -<span class="hljs-meta">... </span> number=<span class="hljs-number">10000</span>, globals=globals(), setup=<span class="hljs-string">'pass'</span>) |
2129 |
| -<span class="hljs-number">0.34986</span> |
2130 |
| -</code></pre></div> |
2131 |
| - |
2132 |
| -<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install line_profiler memory_profiler</span> |
2133 |
| -<span class="hljs-meta">@profile</span> |
2134 |
| -<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span> |
2135 |
| - a = [*range(<span class="hljs-number">10000</span>)] |
2136 |
| - b = {*range(<span class="hljs-number">10000</span>)} |
2137 |
| -main() |
2138 |
| -</code></pre></div> |
2139 |
| - |
2140 |
| -<pre><code class="text language-text">$ kernprof -lv test.py |
| 2127 | +<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>) |
| 2128 | +<span class="hljs-number">0.19373</span> |
| 2129 | +</code></pre></div> |
| 2130 | + |
| 2131 | +<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="text language-text">$ pip3 install line_profiler |
| 2132 | +$ echo " |
| 2133 | +@profile |
| 2134 | +def main(): |
| 2135 | + a = list(range(10000)) |
| 2136 | + b = set(range(10000)) |
| 2137 | +main()" > test.py |
| 2138 | +$ kernprof -lv test.py |
2141 | 2139 | Line # Hits Time Per Hit % Time Line Contents
|
2142 | 2140 | =======================================================
|
2143 | 2141 | 1 @profile
|
2144 | 2142 | 2 def main():
|
2145 |
| - 3 1 955.0 955.0 43.7 a = [*range(10000)] |
2146 |
| - 4 1 1231.0 1231.0 56.3 b = {*range(10000)} |
2147 |
| - |
2148 |
| -$ python3 -m memory_profiler test.py |
2149 |
| -Line # Mem usage Increment Line Contents |
2150 |
| -======================================================= |
2151 |
| - 1 37.668 MiB 37.668 MiB @profile |
2152 |
| - 2 def main(): |
2153 |
| - 3 38.012 MiB 0.344 MiB a = [*range(10000)] |
2154 |
| - 4 38.477 MiB 0.465 MiB b = {*range(10000)} |
2155 |
| -</code></pre> |
2156 |
| -<div><h3 id="callgraph">Call Graph</h3><div><h4 id="generatesapngimageofthecallgraphwithhighlightedbottlenecks">Generates a PNG image of the call graph with highlighted bottlenecks:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pycallgraph2; apt/brew install graphviz</span> |
2157 |
| -<span class="hljs-keyword">import</span> pycallgraph2 <span class="hljs-keyword">as</span> cg, datetime |
| 2143 | + 3 1 219.0 219.0 31.1 a = list(range(10000)) |
| 2144 | + 4 1 487.0 487.0 68.9 b = set(range(10000)) |
| 2145 | +</code></pre></div> |
2158 | 2146 |
|
2159 |
| -filename = <span class="hljs-string">f'profile-<span class="hljs-subst">{datetime.datetime.now():%Y%m%d_%H%M%S}</span>.png'</span> |
2160 |
| -drawer = cg.output.GraphvizOutput(output_file=filename) |
2161 |
| -<span class="hljs-keyword">with</span> cg.PyCallGraph(drawer): |
2162 |
| - <code_to_be_profiled> |
2163 |
| -</code></pre></div></div> |
| 2147 | +<div><h3 id="callandflamegraphs">Call and Flame Graphs</h3><pre><code class="bash language-bash hljs">$ pip3 install gprof2dot snakeviz |
| 2148 | +$ apt/brew install graphviz |
| 2149 | +$ python3 -m cProfile -o test.prof test.py |
| 2150 | +$ gprof2dot -f pstats test.prof | dot -Tpng -o test.png; xdg-open/open test.png |
| 2151 | +$ snakeviz test.prof |
| 2152 | +</code></pre></div> |
2164 | 2153 |
|
| 2154 | +<div><h3 id="samplingandmemoryprofilers">Sampling and Memory Profilers</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━┓ |
| 2155 | +┃ pip3 install │ How to run │ Target │ Type │ Live ┃ |
| 2156 | +┠──────────────┼───────────────────────────────┼────────────┼──────────┼──────┨ |
| 2157 | +┃ py-spy │ py-spy top -- python3 test.py │ CPU │ Sampling │ Yes ┃ |
| 2158 | +┃ pyinstrument │ pyinstrument test.py │ CPU │ Sampling │ No ┃ |
| 2159 | +┃ scalene │ scalene test.py │ CPU+Memory │ Sampling │ No ┃ |
| 2160 | +┃ memray │ memray run --live test.py │ Memory │ Tracing │ Yes ┃ |
| 2161 | +┃ filprofiler │ fil-profile run test.py │ Memory │ Tracing │ No ┃ |
| 2162 | +┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━┛ |
| 2163 | +</code></pre></div> |
2165 | 2164 |
|
2166 | 2165 | <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>
|
2167 | 2166 | <span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
|
@@ -2934,7 +2933,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2934 | 2933 |
|
2935 | 2934 |
|
2936 | 2935 | <footer>
|
2937 |
| - <aside>June 9, 2023</aside> |
| 2936 | + <aside>June 30, 2023</aside> |
2938 | 2937 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2939 | 2938 | </footer>
|
2940 | 2939 |
|
|
0 commit comments