|
615 | 615 |
|
616 | 616 | <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, shuffle, gauss, seed
|
617 | 617 |
|
618 |
| -<float> = random() |
619 |
| -<int> = randint(from_inclusive, to_inclusive) |
620 |
| -<el> = choice(<list>) |
| 618 | +<float> = random() <span class="hljs-comment"># A float inside [0, 1).</span> |
| 619 | +<int> = randint(from_inc, to_inc) <span class="hljs-comment"># An int inside [from_inc, to_inc].</span> |
| 620 | +<el> = choice(<list>) <span class="hljs-comment"># Keeps the list intact.</span> |
621 | 621 | </code></pre></div>
|
622 | 622 |
|
623 | 623 | <div><h3 id="binhex">Bin, Hex</h3><pre><code class="python language-python hljs"><int> = ±<span class="hljs-number">0</span>b<bin> <span class="hljs-comment"># Or: ±0x<hex></span>
|
|
694 | 694 | <div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs"><tzinfo> = UTC <span class="hljs-comment"># UTC timezone. London without DST.</span>
|
695 | 695 | <tzinfo> = tzlocal() <span class="hljs-comment"># Local timezone. Also gettz().</span>
|
696 | 696 | <tzinfo> = gettz(<span class="hljs-string">'<Continent>/<City>'</span>) <span class="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
|
697 |
| -<DTa> = <DT>.astimezone(<tzinfo>) <span class="hljs-comment"># Datetime, converted to passed timezone.</span> |
698 |
| -<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <span class="hljs-comment"># Unconverted object with new timezone.</span> |
| 697 | +<DTa> = <DT>.astimezone(<tzinfo>) <span class="hljs-comment"># Datetime, converted to the passed timezone.</span> |
| 698 | +<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <span class="hljs-comment"># Unconverted object with a new timezone.</span> |
699 | 699 | </code></pre></div>
|
700 | 700 |
|
701 | 701 | <div><h3 id="encode">Encode</h3><pre><code class="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<span class="hljs-string">'<iso>'</span>) <span class="hljs-comment"># Object from ISO string. Raises ValueError.</span>
|
702 | 702 | <DT> = DT.strptime(<str>, <span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Datetime from str, according to format.</span>
|
703 |
| -<D/DTn> = D/DT.fromordinal(<int>) <span class="hljs-comment"># D/DTn from days since Christ, at midnight.</span> |
| 703 | +<D/DTn> = D/DT.fromordinal(<int>) <span class="hljs-comment"># D/DTn from days since the Gregorian NYE 1.</span> |
704 | 704 | <DTn> = DT.fromtimestamp(<real>) <span class="hljs-comment"># Local time DTn from seconds since the Epoch.</span>
|
705 | 705 | <DTa> = DT.fromtimestamp(<real>, <tz.>) <span class="hljs-comment"># Aware datetime from seconds since the Epoch.</span>
|
706 | 706 | </code></pre></div>
|
|
711 | 711 | </ul>
|
712 | 712 | <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>
|
713 | 713 | <str> = <D/T/DT>.strftime(<span class="hljs-string">'<format>'</span>) <span class="hljs-comment"># Custom string representation.</span>
|
714 |
| -<int> = <D/DT>.toordinal() <span class="hljs-comment"># Days since Christ, ignoring time and tz.</span> |
| 714 | +<int> = <D/DT>.toordinal() <span class="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span> |
715 | 715 | <float> = <DTn>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTn in local tz.</span>
|
716 | 716 | <float> = <DTa>.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTa.</span>
|
717 | 717 | </code></pre></div>
|
|
1835 | 1835 | </code></pre></div>
|
1836 | 1836 |
|
1837 | 1837 |
|
1838 |
| -<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 arguments.</span> |
1839 |
| -<Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
1840 |
| -<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
1841 |
| -<Thread>.join() <span class="hljs-comment"># Waits for thread to finish.</span> |
| 1838 | +<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 arguments.</span> |
| 1839 | +<Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
| 1840 | +<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1841 | +<Thread>.join() <span class="hljs-comment"># Waits for thread to finish.</span> |
1842 | 1842 | </code></pre></div>
|
1843 | 1843 |
|
1844 | 1844 | <ul>
|
1845 | 1845 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
|
1846 | 1846 | <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>
|
1847 | 1847 | </ul>
|
1848 |
| -<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span> |
1849 |
| -<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
1850 |
| -<lock>.release() <span class="hljs-comment"># Makes lock available again.</span> |
| 1848 | +<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span> |
| 1849 | +<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
| 1850 | +<lock>.release() <span class="hljs-comment"># Makes lock available again.</span> |
1851 | 1851 | </code></pre></div>
|
1852 | 1852 |
|
1853 | 1853 | <div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
|
1854 | 1854 | <span class="hljs-keyword">with</span> lock:
|
1855 | 1855 | ...
|
1856 | 1856 | </code></pre></div>
|
1857 | 1857 |
|
1858 |
| -<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> |
1859 |
| -<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
1860 |
| -<Barrier> = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called n_times.</span> |
| 1858 | +<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> |
| 1859 | +<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
| 1860 | +<Barrier> = Barrier(n_times) <span class="hljs-comment"># Wait() blocks until it's called n_times.</span> |
1861 | 1861 | </code></pre></div>
|
1862 | 1862 |
|
1863 | 1863 | <div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><p><strong>Object that manages thread execution.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor
|
1864 | 1864 | </code></pre></div>
|
1865 | 1865 |
|
1866 | 1866 |
|
1867 |
| -<pre><code class="python language-python hljs"><Exec> = ThreadPoolExecutor([max_workers]) <span class="hljs-comment"># Use max_workers to limit the number of threads.</span> |
1868 |
| -<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as executor: …`</span> |
| 1867 | +<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> |
| 1868 | +<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Cleans-up the resources associated with Exec.</span> |
1869 | 1869 | </code></pre>
|
1870 |
| -<pre><code class="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span> |
1871 |
| -<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span> |
1872 |
| -<bool> = <Futr>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
1873 |
| -<obj> = <Futr>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
| 1870 | +<pre><code class="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span> |
| 1871 | +<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span> |
| 1872 | +<bool> = <Futr>.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span> |
| 1873 | +<obj> = <Futr>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
1874 | 1874 | </code></pre>
|
1875 | 1875 | <div><h3 id="queue">Queue</h3><p><strong>A thread-safe FIFO queue. For LIFO queue use LifoQueue.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> queue <span class="hljs-keyword">import</span> Queue
|
1876 | 1876 | <Queue> = Queue(maxsize=<span class="hljs-number">0</span>)
|
1877 | 1877 | </code></pre></div>
|
1878 | 1878 |
|
1879 | 1879 |
|
1880 |
| -<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
1881 |
| -<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
1882 |
| -<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
1883 |
| -<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
| 1880 | +<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
| 1881 | +<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
| 1882 | +<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
| 1883 | +<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
1884 | 1884 | </code></pre>
|
1885 | 1885 | <div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> add, sub, mul, truediv, floordiv, mod, pow, neg, abs
|
1886 | 1886 | <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
|
|
1914 | 1914 | </code></pre></div>
|
1915 | 1915 |
|
1916 | 1916 | <div><h3 id="parameters-1">Parameters</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
|
1917 |
| -<Sig> = signature(<function>) <span class="hljs-comment"># Signature object of the function.</span> |
| 1917 | +<Sig> = signature(<function>) <span class="hljs-comment"># Function's Signature object.</span> |
1918 | 1918 | <dict> = <Sig>.parameters <span class="hljs-comment"># Dict of function's Parameter objects.</span>
|
1919 | 1919 | <str> = <Param>.name <span class="hljs-comment"># Parameter's name.</span>
|
1920 | 1920 | <memb> = <Param>.kind <span class="hljs-comment"># Member of ParameterKind enum.</span>
|
|
0 commit comments