Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f51e830

Browse files
committed
Random, Datetime, Threading
1 parent d874635 commit f51e830

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,9 @@ from statistics import mean, median, variance, stdev, pvariance, pstdev
513513
```python
514514
from random import random, randint, choice, shuffle, gauss, seed
515515

516-
<float> = random()
517-
<int> = randint(from_inclusive, to_inclusive)
518-
<el> = choice(<list>)
516+
<float> = random() # A float inside [0, 1).
517+
<int> = randint(from_inc, to_inc) # An int inside [from_inc, to_inc].
518+
<el> = choice(<list>) # Keeps the list intact.
519519
```
520520

521521
### Bin, Hex
@@ -614,15 +614,15 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
614614
<tzinfo> = UTC # UTC timezone. London without DST.
615615
<tzinfo> = tzlocal() # Local timezone. Also gettz().
616616
<tzinfo> = gettz('<Continent>/<City>') # 'Continent/City_Name' timezone or None.
617-
<DTa> = <DT>.astimezone(<tzinfo>) # Datetime, converted to passed timezone.
618-
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Unconverted object with new timezone.
617+
<DTa> = <DT>.astimezone(<tzinfo>) # Datetime, converted to the passed timezone.
618+
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Unconverted object with a new timezone.
619619
```
620620

621621
### Encode
622622
```python
623623
<D/T/DT> = D/T/DT.fromisoformat('<iso>') # Object from ISO string. Raises ValueError.
624624
<DT> = DT.strptime(<str>, '<format>') # Datetime from str, according to format.
625-
<D/DTn> = D/DT.fromordinal(<int>) # D/DTn from days since Christ, at midnight.
625+
<D/DTn> = D/DT.fromordinal(<int>) # D/DTn from days since the Gregorian NYE 1.
626626
<DTn> = DT.fromtimestamp(<real>) # Local time DTn from seconds since the Epoch.
627627
<DTa> = DT.fromtimestamp(<real>, <tz.>) # Aware datetime from seconds since the Epoch.
628628
```
@@ -633,7 +633,7 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
633633
```python
634634
<str> = <D/T/DT>.isoformat(sep='T') # Also timespec='auto/hours/minutes/seconds'.
635635
<str> = <D/T/DT>.strftime('<format>') # Custom string representation.
636-
<int> = <D/DT>.toordinal() # Days since Christ, ignoring time and tz.
636+
<int> = <D/DT>.toordinal() # Days since Gregorian NYE 1, ignoring time and tz.
637637
<float> = <DTn>.timestamp() # Seconds since the Epoch, from DTn in local tz.
638638
<float> = <DTa>.timestamp() # Seconds since the Epoch, from DTa.
639639
```
@@ -2050,19 +2050,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier
20502050

20512051
### Thread
20522052
```python
2053-
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2054-
<Thread>.start() # Starts the thread.
2055-
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2056-
<Thread>.join() # Waits for thread to finish.
2053+
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2054+
<Thread>.start() # Starts the thread.
2055+
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2056+
<Thread>.join() # Waits for thread to finish.
20572057
```
20582058
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
20592059
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
20602060

20612061
### Lock
20622062
```python
2063-
<lock> = RLock() # Lock that can only be released by the owner.
2064-
<lock>.acquire() # Waits for lock to be available.
2065-
<lock>.release() # Makes lock available again.
2063+
<lock> = RLock() # Lock that can only be released by the owner.
2064+
<lock>.acquire() # Waits for lock to be available.
2065+
<lock>.release() # Makes lock available again.
20662066
```
20672067

20682068
#### Or:
@@ -2074,9 +2074,9 @@ with lock:
20742074

20752075
### Semaphore, Event, Barrier
20762076
```python
2077-
<Semaphore> = Semaphore(value=1) # Lock that can be acquired by 'value' threads.
2078-
<Event> = Event() # Method wait() blocks until set() is called.
2079-
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called n_times.
2077+
<Semaphore> = Semaphore(value=1) # Lock that can be acquired by 'value' threads.
2078+
<Event> = Event() # Method wait() blocks until set() is called.
2079+
<Barrier> = Barrier(n_times) # Wait() blocks until it's called n_times.
20802080
```
20812081

20822082
### Thread Pool Executor
@@ -2086,15 +2086,15 @@ from concurrent.futures import ThreadPoolExecutor
20862086
```
20872087

20882088
```python
2089-
<Exec> = ThreadPoolExecutor([max_workers]) # Use max_workers to limit the number of threads.
2090-
<Exec>.shutdown(wait=True) # Or: `with ThreadPoolExecutor() as executor: …`
2089+
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: …`
2090+
<Exec>.shutdown(wait=True) # Cleans-up the resources associated with Exec.
20912091
```
20922092

20932093
```python
2094-
<iter> = <Exec>.map(<func>, <args_1>, ...) # A multithreaded and non-lazy map().
2095-
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object.
2096-
<bool> = <Futr>.done() # Checks if thread has finished executing.
2097-
<obj> = <Futr>.result() # Waits for thread to finish and returns result.
2094+
<iter> = <Exec>.map(<func>, <args_1>, ...) # A multithreaded and non-lazy map().
2095+
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object.
2096+
<bool> = <Futr>.done() # Checks if the thread has finished executing.
2097+
<obj> = <Futr>.result() # Waits for thread to finish and returns result.
20982098
```
20992099

21002100
### Queue
@@ -2105,10 +2105,10 @@ from queue import Queue
21052105
```
21062106

21072107
```python
2108-
<Queue>.put(<el>) # Blocks until queue stops being full.
2109-
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2110-
<el> = <Queue>.get() # Blocks until queue stops being empty.
2111-
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
2108+
<Queue>.put(<el>) # Blocks until queue stops being full.
2109+
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2110+
<el> = <Queue>.get() # Blocks until queue stops being empty.
2111+
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
21122112
```
21132113

21142114

@@ -2158,7 +2158,7 @@ delattr(<object>, '<attr_name>') # Equivalent to `del <object>.<attr_n
21582158
### Parameters
21592159
```python
21602160
from inspect import signature
2161-
<Sig> = signature(<function>) # Signature object of the function.
2161+
<Sig> = signature(<function>) # Function's Signature object.
21622162
<dict> = <Sig>.parameters # Dict of function's Parameter objects.
21632163
<str> = <Param>.name # Parameter's name.
21642164
<memb> = <Param>.kind # Member of ParameterKind enum.

index.html

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@
615615

616616
<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
617617

618-
&lt;float&gt; = random()
619-
&lt;int&gt; = randint(from_inclusive, to_inclusive)
620-
&lt;el&gt; = choice(&lt;list&gt;)
618+
&lt;float&gt; = random() <span class="hljs-comment"># A float inside [0, 1).</span>
619+
&lt;int&gt; = randint(from_inc, to_inc) <span class="hljs-comment"># An int inside [from_inc, to_inc].</span>
620+
&lt;el&gt; = choice(&lt;list&gt;) <span class="hljs-comment"># Keeps the list intact.</span>
621621
</code></pre></div>
622622

623623
<div><h3 id="binhex">Bin, Hex</h3><pre><code class="python language-python hljs">&lt;int&gt; = ±<span class="hljs-number">0</span>b&lt;bin&gt; <span class="hljs-comment"># Or: ±0x&lt;hex&gt;</span>
@@ -694,13 +694,13 @@
694694
<div><h3 id="timezone">Timezone</h3><pre><code class="python language-python apache hljs">&lt;tzinfo&gt; = UTC <span class="hljs-comment"># UTC timezone. London without DST.</span>
695695
&lt;tzinfo&gt; = tzlocal() <span class="hljs-comment"># Local timezone. Also gettz().</span>
696696
&lt;tzinfo&gt; = gettz(<span class="hljs-string">'&lt;Continent&gt;/&lt;City&gt;'</span>) <span class="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
697-
&lt;DTa&gt; = &lt;DT&gt;.astimezone(&lt;tzinfo&gt;) <span class="hljs-comment"># Datetime, converted to passed timezone.</span>
698-
&lt;Ta/DTa&gt; = &lt;T/DT&gt;.replace(tzinfo=&lt;tzinfo&gt;) <span class="hljs-comment"># Unconverted object with new timezone.</span>
697+
&lt;DTa&gt; = &lt;DT&gt;.astimezone(&lt;tzinfo&gt;) <span class="hljs-comment"># Datetime, converted to the passed timezone.</span>
698+
&lt;Ta/DTa&gt; = &lt;T/DT&gt;.replace(tzinfo=&lt;tzinfo&gt;) <span class="hljs-comment"># Unconverted object with a new timezone.</span>
699699
</code></pre></div>
700700

701701
<div><h3 id="encode">Encode</h3><pre><code class="python language-python apache hljs">&lt;D/T/DT&gt; = D/T/DT.fromisoformat(<span class="hljs-string">'&lt;iso&gt;'</span>) <span class="hljs-comment"># Object from ISO string. Raises ValueError.</span>
702702
&lt;DT&gt; = DT.strptime(&lt;str&gt;, <span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Datetime from str, according to format.</span>
703-
&lt;D/DTn&gt; = D/DT.fromordinal(&lt;int&gt;) <span class="hljs-comment"># D/DTn from days since Christ, at midnight.</span>
703+
&lt;D/DTn&gt; = D/DT.fromordinal(&lt;int&gt;) <span class="hljs-comment"># D/DTn from days since the Gregorian NYE 1.</span>
704704
&lt;DTn&gt; = DT.fromtimestamp(&lt;real&gt;) <span class="hljs-comment"># Local time DTn from seconds since the Epoch.</span>
705705
&lt;DTa&gt; = DT.fromtimestamp(&lt;real&gt;, &lt;tz.&gt;) <span class="hljs-comment"># Aware datetime from seconds since the Epoch.</span>
706706
</code></pre></div>
@@ -711,7 +711,7 @@
711711
</ul>
712712
<div><h3 id="decode">Decode</h3><pre><code class="python language-python hljs">&lt;str&gt; = &lt;D/T/DT&gt;.isoformat(sep=<span class="hljs-string">'T'</span>) <span class="hljs-comment"># Also timespec='auto/hours/minutes/seconds'.</span>
713713
&lt;str&gt; = &lt;D/T/DT&gt;.strftime(<span class="hljs-string">'&lt;format&gt;'</span>) <span class="hljs-comment"># Custom string representation.</span>
714-
&lt;int&gt; = &lt;D/DT&gt;.toordinal() <span class="hljs-comment"># Days since Christ, ignoring time and tz.</span>
714+
&lt;int&gt; = &lt;D/DT&gt;.toordinal() <span class="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span>
715715
&lt;float&gt; = &lt;DTn&gt;.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTn in local tz.</span>
716716
&lt;float&gt; = &lt;DTa&gt;.timestamp() <span class="hljs-comment"># Seconds since the Epoch, from DTa.</span>
717717
</code></pre></div>
@@ -1835,52 +1835,52 @@
18351835
</code></pre></div>
18361836

18371837

1838-
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">&lt;Thread&gt; = Thread(target=&lt;function&gt;) <span class="hljs-comment"># Use `args=&lt;collection&gt;` to set arguments.</span>
1839-
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1840-
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1841-
&lt;Thread&gt;.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">&lt;Thread&gt; = Thread(target=&lt;function&gt;) <span class="hljs-comment"># Use `args=&lt;collection&gt;` to set arguments.</span>
1839+
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1840+
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1841+
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
18421842
</code></pre></div>
18431843

18441844
<ul>
18451845
<li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=&lt;dict&gt;'</span></code> to pass keyword arguments to the function.</strong></li>
18461846
<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>
18471847
</ul>
1848-
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span>
1849-
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1850-
&lt;lock&gt;.release() <span class="hljs-comment"># Makes lock available again.</span>
1848+
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span>
1849+
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1850+
&lt;lock&gt;.release() <span class="hljs-comment"># Makes lock available again.</span>
18511851
</code></pre></div>
18521852

18531853
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
18541854
<span class="hljs-keyword">with</span> lock:
18551855
...
18561856
</code></pre></div>
18571857

1858-
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-python hljs">&lt;Semaphore&gt; = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span>
1859-
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1860-
&lt;Barrier&gt; = 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">&lt;Semaphore&gt; = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span>
1859+
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1860+
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Wait() blocks until it's called n_times.</span>
18611861
</code></pre></div>
18621862

18631863
<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
18641864
</code></pre></div>
18651865

18661866

1867-
<pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor([max_workers]) <span class="hljs-comment"># Use max_workers to limit the number of threads.</span>
1868-
&lt;Exec&gt;.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">&lt;Exec&gt; = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as &lt;name&gt;: …`</span>
1868+
&lt;Exec&gt;.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Cleans-up the resources associated with Exec.</span>
18691869
</code></pre>
1870-
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span>
1871-
&lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
1872-
&lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1873-
&lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1870+
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span>
1871+
&lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
1872+
&lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
1873+
&lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
18741874
</code></pre>
18751875
<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
18761876
&lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>)
18771877
</code></pre></div>
18781878

18791879

1880-
<pre><code class="python language-python hljs">&lt;Queue&gt;.put(&lt;el&gt;) <span class="hljs-comment"># Blocks until queue stops being full.</span>
1881-
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1882-
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1883-
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
1880+
<pre><code class="python language-python hljs">&lt;Queue&gt;.put(&lt;el&gt;) <span class="hljs-comment"># Blocks until queue stops being full.</span>
1881+
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1882+
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1883+
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
18841884
</code></pre>
18851885
<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
18861886
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
@@ -1914,7 +1914,7 @@
19141914
</code></pre></div>
19151915

19161916
<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-
&lt;Sig&gt; = signature(&lt;function&gt;) <span class="hljs-comment"># Signature object of the function.</span>
1917+
&lt;Sig&gt; = signature(&lt;function&gt;) <span class="hljs-comment"># Function's Signature object.</span>
19181918
&lt;dict&gt; = &lt;Sig&gt;.parameters <span class="hljs-comment"># Dict of function's Parameter objects.</span>
19191919
&lt;str&gt; = &lt;Param&gt;.name <span class="hljs-comment"># Parameter's name.</span>
19201920
&lt;memb&gt; = &lt;Param&gt;.kind <span class="hljs-comment"># Member of ParameterKind enum.</span>

0 commit comments

Comments
 (0)