Browse Source

Itertools, Numbers, Combinatorics, OS Commands

pull/135/head
Jure Šorn 2 years ago
parent
commit
b393c21c7b
3 changed files with 144 additions and 144 deletions
  1. 138
      README.md
  2. 142
      index.html
  3. 8
      parse.js

138
README.md

@ -200,23 +200,23 @@ Iterator
### Itertools ### Itertools
```python ```python
from itertools import count, repeat, cycle, chain, islice import itertools as it
``` ```
```python ```python
<iter> = count(start=0, step=1) # Returns updated value endlessly. Accepts floats. <iter> = it.count(start=0, step=1) # Returns updated value endlessly. Accepts floats.
<iter> = repeat(<el> [, times]) # Returns element endlessly or 'times' times. <iter> = it.repeat(<el> [, times]) # Returns element endlessly or 'times' times.
<iter> = cycle(<collection>) # Repeats the sequence endlessly. <iter> = it.cycle(<collection>) # Repeats the sequence endlessly.
``` ```
```python ```python
<iter> = chain(<coll_1>, <coll_2> [, ...]) # Empties collections in order (figuratively). <iter> = it.chain(<coll>, <coll> [, ...]) # Empties collections in order (figuratively).
<iter> = chain.from_iterable(<coll>) # Empties collections inside a collection in order. <iter> = it.chain.from_iterable(<coll>) # Empties collections inside a collection in order.
``` ```
```python ```python
<iter> = islice(<coll>, to_exclusive) # Only returns first 'to_exclusive' elements. <iter> = it.islice(<coll>, to_exclusive) # Only returns first 'to_exclusive' elements.
<iter> = islice(<coll>, from_inclusive, …) # `to_exclusive, +step_size`. Indices can be None. <iter> = it.islice(<coll>, from_inc, …) # `to_exclusive, +step_size`. Indices can be None.
``` ```
@ -486,11 +486,11 @@ Format
Numbers Numbers
------- -------
```python ```python
<int> = int(<float/str/bool>) # Or: math.floor(<float>) <int> = int(<float/str/bool>) # Or: math.floor(<float>)
<float> = float(<int/str/bool>) # Or: <real><int> <float> = float(<int/str/bool>) # Or: <real><int>
<complex> = complex(real=0, imag=0) # Or: <real> ± <real>j <complex> = complex(real=0, imag=0) # Or: <real> ± <real>j
<Fraction> = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1) <Fraction> = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1)
<Decimal> = decimal.Decimal(<str/int>) # Or: Decimal((sign, digits, exponent)) <Decimal> = decimal.Decimal(<str/int>) # Or: Decimal((sign, digits, exponent))
``` ```
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.** * **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.** * **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.**
@ -499,47 +499,46 @@ Numbers
### Basic Functions ### Basic Functions
```python ```python
<num> = pow(<num>, <num>) # Or: <num> ** <num> <num> = pow(<num>, <num>) # Or: <num> ** <num>
<num> = abs(<num>) # <float> = abs(<complex>) <num> = abs(<num>) # <float> = abs(<complex>)
<num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130` <num> = round(<num> [, ±ndigits]) # `round(126, -1) == 130`
``` ```
### Math ### Math
```python ```python
from math import e, pi, inf, nan, isinf, isnan from math import e, pi, inf, nan, isinf, isnan # `<el> == nan` is always False.
from math import sin, cos, tan, asin, acos, atan, degrees, radians from math import sin, cos, tan, asin, acos, atan # Also: degrees, radians.
from math import log, log10, log2 from math import log, log10, log2 # Log can accept base as second arg.
``` ```
### Statistics ### Statistics
```python ```python
from statistics import mean, median, variance, stdev, quantiles, groupby from statistics import mean, median, variance # Also: stdev, quantiles, groupby.
``` ```
### Random ### Random
```python ```python
from random import random, randint, choice, shuffle, gauss, seed from random import random, randint, choice # Also shuffle, gauss, triangular, seed.
<float> = random() # A float inside [0, 1).
<float> = random() # A float inside [0, 1). <int> = randint(from_inc, to_inc) # An int inside [from_inc, to_inc].
<int> = randint(from_inc, to_inc) # An int inside [from_inc, to_inc]. <el> = choice(<sequence>) # Keeps the sequence intact.
<el> = choice(<sequence>) # Keeps the sequence intact.
``` ```
### Bin, Hex ### Bin, Hex
```python ```python
<int> = ±0b<bin> # Or: ±0x<hex> <int> = ±0b<bin> # Or: ±0x<hex>
<int> = int('±<bin>', 2) # Or: int('±<hex>', 16) <int> = int('±<bin>', 2) # Or: int('±<hex>', 16)
<int> = int('±0b<bin>', 0) # Or: int('±0x<hex>', 0) <int> = int('±0b<bin>', 0) # Or: int('±0x<hex>', 0)
<str> = bin(<int>) # Returns '[-]0b<bin>'. <str> = bin(<int>) # Returns '[-]0b<bin>'.
``` ```
### Bitwise Operators ### Bitwise Operators
```python ```python
<int> = <int> & <int> # And (0b1100 & 0b1010 == 0b1000). <int> = <int> & <int> # And (0b1100 & 0b1010 == 0b1000).
<int> = <int> | <int> # Or (0b1100 | 0b1010 == 0b1110). <int> = <int> | <int> # Or (0b1100 | 0b1010 == 0b1110).
<int> = <int> ^ <int> # Xor (0b1100 ^ 0b1010 == 0b0110). <int> = <int> ^ <int> # Xor (0b1100 ^ 0b1010 == 0b0110).
<int> = <int> << n_bits # Left shift. Use >> for right. <int> = <int> << n_bits # Left shift. Use >> for right.
<int> = ~<int> # Not. Also -<int> - 1. <int> = ~<int> # Not. Also -<int> - 1.
``` ```
@ -549,39 +548,40 @@ Combinatorics
* **If you want to print the iterator, you need to pass it to the list() function first!** * **If you want to print the iterator, you need to pass it to the list() function first!**
```python ```python
from itertools import product, combinations, combinations_with_replacement, permutations import itertools as it
``` ```
```python ```python
>>> product([0, 1], repeat=3) >>> it.product([0, 1], repeat=3)
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ..., (1, 1, 1)] [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
``` ```
```python ```python
>>> product('abc', 'abc') # a b c >>> it.product('abc', 'abc') # a b c
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x [('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x
('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x
``` ```
```python ```python
>>> combinations('abc', 2) # a b c >>> it.combinations('abc', 2) # a b c
[('a', 'b'), ('a', 'c'), # a . x x [('a', 'b'), ('a', 'c'), # a . x x
('b', 'c')] # b . . x ('b', 'c')] # b . . x
``` ```
```python ```python
>>> combinations_with_replacement('abc', 2) # a b c >>> it.combinations_with_replacement('abc', 2) # a b c
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x [('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
('b', 'b'), ('b', 'c'), # b . x x ('b', 'b'), ('b', 'c'), # b . x x
('c', 'c')] # c . . x ('c', 'c')] # c . . x
``` ```
```python ```python
>>> permutations('abc', 2) # a b c >>> it.permutations('abc', 2) # a b c
[('a', 'b'), ('a', 'c'), # a . x x [('a', 'b'), ('a', 'c'), # a . x x
('b', 'a'), ('b', 'c'), # b x . x ('b', 'a'), ('b', 'c'), # b x . x
('c', 'a'), ('c', 'b')] # c x x . ('c', 'a'), ('c', 'b')] # c x x .
``` ```
@ -1701,34 +1701,34 @@ import os, shutil, subprocess
``` ```
```python ```python
os.chdir(<path>) # Changes the current working directory. os.chdir(<path>) # Changes the current working directory.
os.mkdir(<path>, mode=0o777) # Creates a directory. Mode is in octal. os.mkdir(<path>, mode=0o777) # Creates a directory. Permissions are in octal.
os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`. os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also: `exist_ok=False`.
``` ```
```python ```python
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
shutil.copytree(from, to) # Copies the directory. 'to' must not exist. shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
``` ```
```python ```python
os.rename(from, to) # Renames/moves the file or directory. os.rename(from, to) # Renames/moves the file or directory.
os.replace(from, to) # Same, but overwrites 'to' if it exists. os.replace(from, to) # Same, but overwrites 'to' if it exists.
``` ```
```python ```python
os.remove(<path>) # Deletes the file. os.remove(<path>) # Deletes the file.
os.rmdir(<path>) # Deletes the empty directory. os.rmdir(<path>) # Deletes the empty directory.
shutil.rmtree(<path>) # Deletes the directory. shutil.rmtree(<path>) # Deletes the directory.
``` ```
* **Paths can be either strings, Paths or DirEntry objects.** * **Paths can be either strings, Paths or DirEntry objects.**
* **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).** * **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).**
### Shell Commands ### Shell Commands
```python ```python
<pipe> = os.popen('<command>') # Executes command in sh/cmd and returns its stdout pipe. <pipe> = os.popen('<command>') # Executes command in sh/cmd. Returns its stdout pipe.
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s(). <str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
<int> = <pipe>.close() # Closes the pipe. Returns None on success, int on error. <int> = <pipe>.close() # Closes the pipe. Returns None on success.
``` ```
#### Sends '1 + 1' to the basic calculator and captures its output: #### Sends '1 + 1' to the basic calculator and captures its output:
@ -1754,8 +1754,8 @@ JSON
```python ```python
import json import json
<str> = json.dumps(<object>) # Converts object to JSON string. <str> = json.dumps(<object>) # Converts object to JSON string.
<object> = json.loads(<str>) # Converts JSON string to object. <object> = json.loads(<str>) # Converts JSON string to object.
``` ```
### Read Object from JSON File ### Read Object from JSON File
@ -1779,8 +1779,8 @@ Pickle
```python ```python
import pickle import pickle
<bytes> = pickle.dumps(<object>) # Converts object to bytes object. <bytes> = pickle.dumps(<object>) # Converts object to bytes object.
<object> = pickle.loads(<bytes>) # Converts bytes object to object. <object> = pickle.loads(<bytes>) # Converts bytes object to object.
``` ```
### Read Object from File ### Read Object from File

142
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>July 30, 2022</aside> <aside>September 4, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -220,18 +220,18 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
&lt;list&gt; = list(&lt;iter&gt;) <span class="hljs-comment"># Returns a list of iterator's remaining elements.</span> &lt;list&gt; = list(&lt;iter&gt;) <span class="hljs-comment"># Returns a list of iterator's remaining elements.</span>
</code></pre></div> </code></pre></div>
<div><h3 id="itertools">Itertools</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> count, repeat, cycle, chain, islice <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> </code></pre></div>
<pre><code class="python language-python hljs">&lt;iter&gt; = 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> <pre><code class="python language-python hljs">&lt;iter&gt; = 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>
&lt;iter&gt; = repeat(&lt;el&gt; [, times]) <span class="hljs-comment"># Returns element endlessly or 'times' times.</span> &lt;iter&gt; = it.repeat(&lt;el&gt; [, times]) <span class="hljs-comment"># Returns element endlessly or 'times' times.</span>
&lt;iter&gt; = cycle(&lt;collection&gt;) <span class="hljs-comment"># Repeats the sequence endlessly.</span> &lt;iter&gt; = it.cycle(&lt;collection&gt;) <span class="hljs-comment"># Repeats the sequence endlessly.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;iter&gt; = chain(&lt;coll_1&gt;, &lt;coll_2&gt; [, ...]) <span class="hljs-comment"># Empties collections in order (figuratively).</span> <pre><code class="python language-python hljs">&lt;iter&gt; = it.chain(&lt;coll&gt;, &lt;coll&gt; [, ...]) <span class="hljs-comment"># Empties collections in order (figuratively).</span>
&lt;iter&gt; = chain.from_iterable(&lt;coll&gt;) <span class="hljs-comment"># Empties collections inside a collection in order.</span> &lt;iter&gt; = it.chain.from_iterable(&lt;coll&gt;) <span class="hljs-comment"># Empties collections inside a collection in order.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;iter&gt; = islice(&lt;coll&gt;, to_exclusive) <span class="hljs-comment"># Only returns first 'to_exclusive' elements.</span> <pre><code class="python language-python hljs">&lt;iter&gt; = it.islice(&lt;coll&gt;, to_exclusive) <span class="hljs-comment"># Only returns first 'to_exclusive' elements.</span>
&lt;iter&gt; = islice(&lt;coll&gt;, from_inclusive, …) <span class="hljs-comment"># `to_exclusive, +step_size`. Indices can be None.</span> &lt;iter&gt; = it.islice(&lt;coll&gt;, from_inc, …) <span class="hljs-comment"># `to_exclusive, +step_size`. Indices can be None.</span>
</code></pre> </code></pre>
<div><h2 id="generator"><a href="#generator" name="generator">#</a>Generator</h2><ul> <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>Any function that contains a yield statement returns a generator.</strong></li>
@ -444,11 +444,11 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
{<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'</span> {<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'</span>
</code></pre></div> </code></pre></div>
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span> <div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
&lt;float&gt; = float(&lt;int/str/bool&gt;) <span class="hljs-comment"># Or: &lt;real&gt;&lt;int&gt;</span> &lt;float&gt; = float(&lt;int/str/bool&gt;) <span class="hljs-comment"># Or: &lt;real&gt;&lt;int&gt;</span>
&lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;real&gt; ± &lt;real&gt;j</span> &lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;real&gt; ± &lt;real&gt;j</span>
&lt;Fraction&gt; = 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> &lt;Fraction&gt; = 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>
&lt;Decimal&gt; = decimal.Decimal(&lt;str/int&gt;) <span class="hljs-comment"># Or: Decimal((sign, digits, exponent))</span> &lt;Decimal&gt; = decimal.Decimal(&lt;str/int&gt;) <span class="hljs-comment"># Or: Decimal((sign, digits, exponent))</span>
</code></pre></div> </code></pre></div>
<ul> <ul>
@ -457,67 +457,67 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<li><strong>Floats can be compared with: <code class="python hljs"><span class="hljs-string">'math.isclose(&lt;float&gt;, &lt;float&gt;)'</span></code>.</strong></li> <li><strong>Floats can be compared with: <code class="python hljs"><span class="hljs-string">'math.isclose(&lt;float&gt;, &lt;float&gt;)'</span></code>.</strong></li>
<li><strong>Precision of decimal operations is set with: <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = &lt;int&gt;'</span></code>.</strong></li> <li><strong>Precision of decimal operations is set with: <code class="python hljs"><span class="hljs-string">'decimal.getcontext().prec = &lt;int&gt;'</span></code>.</strong></li>
</ul> </ul>
<div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span> <div><h3 id="basicfunctions">Basic Functions</h3><pre><code class="python language-python hljs">&lt;num&gt; = pow(&lt;num&gt;, &lt;num&gt;) <span class="hljs-comment"># Or: &lt;num&gt; ** &lt;num&gt;</span>
&lt;num&gt; = abs(&lt;num&gt;) <span class="hljs-comment"># &lt;float&gt; = abs(&lt;complex&gt;)</span> &lt;num&gt; = abs(&lt;num&gt;) <span class="hljs-comment"># &lt;float&gt; = abs(&lt;complex&gt;)</span>
&lt;num&gt; = round(&lt;num&gt; [, ±ndigits]) <span class="hljs-comment"># `round(126, -1) == 130`</span> &lt;num&gt; = round(&lt;num&gt; [, ±ndigits]) <span class="hljs-comment"># `round(126, -1) == 130`</span>
</code></pre></div> </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 <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"># `&lt;el&gt; == nan` is always False.</span>
<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> sin, cos, tan, asin, acos, atan, degrees, radians <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-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> </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, stdev, quantiles, groupby <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> </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, shuffle, gauss, seed <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>
&lt;float&gt; = random() <span class="hljs-comment"># A float inside [0, 1).</span>
&lt;float&gt; = random() <span class="hljs-comment"># A float inside [0, 1).</span> &lt;int&gt; = randint(from_inc, to_inc) <span class="hljs-comment"># An int inside [from_inc, to_inc].</span>
&lt;int&gt; = randint(from_inc, to_inc) <span class="hljs-comment"># An int inside [from_inc, to_inc].</span> &lt;el&gt; = choice(&lt;sequence&gt;) <span class="hljs-comment"># Keeps the sequence intact.</span>
&lt;el&gt; = choice(&lt;sequence&gt;) <span class="hljs-comment"># Keeps the sequence intact.</span>
</code></pre></div> </code></pre></div>
<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> <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>
&lt;int&gt; = int(<span class="hljs-string">&lt;bin&gt;'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Or: int('±&lt;hex&gt;', 16)</span> &lt;int&gt; = int(<span class="hljs-string">&lt;bin&gt;'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Or: int('±&lt;hex&gt;', 16)</span>
&lt;int&gt; = int(<span class="hljs-string">'±0b&lt;bin&gt;'</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Or: int('±0x&lt;hex&gt;', 0)</span> &lt;int&gt; = int(<span class="hljs-string">'±0b&lt;bin&gt;'</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Or: int('±0x&lt;hex&gt;', 0)</span>
&lt;str&gt; = bin(&lt;int&gt;) <span class="hljs-comment"># Returns '[-]0b&lt;bin&gt;'.</span> &lt;str&gt; = bin(&lt;int&gt;) <span class="hljs-comment"># Returns '[-]0b&lt;bin&gt;'.</span>
</code></pre></div> </code></pre></div>
<div><h3 id="bitwiseoperators">Bitwise Operators</h3><pre><code class="python language-python hljs">&lt;int&gt; = &lt;int&gt; &amp; &lt;int&gt; <span class="hljs-comment"># And (0b1100 &amp; 0b1010 == 0b1000).</span> <div><h3 id="bitwiseoperators">Bitwise Operators</h3><pre><code class="python language-python hljs">&lt;int&gt; = &lt;int&gt; &amp; &lt;int&gt; <span class="hljs-comment"># And (0b1100 &amp; 0b1010 == 0b1000).</span>
&lt;int&gt; = &lt;int&gt; | &lt;int&gt; <span class="hljs-comment"># Or (0b1100 | 0b1010 == 0b1110).</span> &lt;int&gt; = &lt;int&gt; | &lt;int&gt; <span class="hljs-comment"># Or (0b1100 | 0b1010 == 0b1110).</span>
&lt;int&gt; = &lt;int&gt; ^ &lt;int&gt; <span class="hljs-comment"># Xor (0b1100 ^ 0b1010 == 0b0110).</span> &lt;int&gt; = &lt;int&gt; ^ &lt;int&gt; <span class="hljs-comment"># Xor (0b1100 ^ 0b1010 == 0b0110).</span>
&lt;int&gt; = &lt;int&gt; &lt;&lt; n_bits <span class="hljs-comment"># Left shift. Use &gt;&gt; for right.</span> &lt;int&gt; = &lt;int&gt; &lt;&lt; n_bits <span class="hljs-comment"># Left shift. Use &gt;&gt; for right.</span>
&lt;int&gt; = ~&lt;int&gt; <span class="hljs-comment"># Not. Also -&lt;int&gt; - 1.</span> &lt;int&gt; = ~&lt;int&gt; <span class="hljs-comment"># Not. Also -&lt;int&gt; - 1.</span>
</code></pre></div> </code></pre></div>
<div><h2 id="combinatorics"><a href="#combinatorics" name="combinatorics">#</a>Combinatorics</h2><ul> <div><h2 id="combinatorics"><a href="#combinatorics" name="combinatorics">#</a>Combinatorics</h2><ul>
<li><strong>Every function returns an iterator.</strong></li> <li><strong>Every function returns an iterator.</strong></li>
<li><strong>If you want to print the iterator, you need to pass it to the list() function first!</strong></li> <li><strong>If you want to print the iterator, you need to pass it to the list() function first!</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> product, combinations, combinations_with_replacement, permutations </ul><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> itertools <span class="hljs-keyword">as</span> it
</code></pre></div> </code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>product([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>], repeat=<span class="hljs-number">3</span>) <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>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">1</span>, <span class="hljs-number">1</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> </code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>product(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'abc'</span>) <span class="hljs-comment"># a b c</span> <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>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">'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">'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> (<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> </code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>combinations(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># a b c</span> <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>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">'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> (<span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>)] <span class="hljs-comment"># b . . x</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>combinations_with_replacement(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># a b c</span> <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>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">'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">'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> (<span class="hljs-string">'c'</span>, <span class="hljs-string">'c'</span>)] <span class="hljs-comment"># c . . x</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>permutations(<span class="hljs-string">'abc'</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># a b c</span> <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>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">'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">'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> (<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> </code></pre>
<div><h2 id="datetime"><a href="#datetime" name="datetime">#</a>Datetime</h2><ul> <div><h2 id="datetime"><a href="#datetime" name="datetime">#</a>Datetime</h2><ul>
<li><strong>Module 'datetime' provides 'date' <code class="apache hljs"><span class="hljs-section">&lt;D&gt;</span></code>, 'time' <code class="apache hljs"><span class="hljs-section">&lt;T&gt;</span></code>, 'datetime' <code class="apache hljs"><span class="hljs-section">&lt;DT&gt;</span></code> and 'timedelta' <code class="apache hljs"><span class="hljs-section">&lt;TD&gt;</span></code> classes. All are immutable and hashable.</strong></li> <li><strong>Module 'datetime' provides 'date' <code class="apache hljs"><span class="hljs-section">&lt;D&gt;</span></code>, 'time' <code class="apache hljs"><span class="hljs-section">&lt;T&gt;</span></code>, 'datetime' <code class="apache hljs"><span class="hljs-section">&lt;DT&gt;</span></code> and 'timedelta' <code class="apache hljs"><span class="hljs-section">&lt;TD&gt;</span></code> classes. All are immutable and hashable.</strong></li>
@ -1434,27 +1434,27 @@ value = args.&lt;name&gt;
<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 <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> </code></pre></div>
<pre><code class="python language-python hljs">os.chdir(&lt;path&gt;) <span class="hljs-comment"># Changes the current working directory.</span> <pre><code class="python language-python hljs">os.chdir(&lt;path&gt;) <span class="hljs-comment"># Changes the current working directory.</span>
os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Mode is in octal.</span> os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Permissions are in octal.</span>
os.makedirs(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also: `exist_ok=False`.</span> os.makedirs(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also: `exist_ok=False`.</span>
</code></pre> </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> <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.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span> shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span> <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 'to' if it exists.</span> os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file.</span> <pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file.</span>
os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory.</span> os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory.</span>
shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes the directory.</span> shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes the directory.</span>
</code></pre> </code></pre>
<ul> <ul>
<li><strong>Paths can be either strings, Paths or DirEntry objects.</strong></li> <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> <li><strong>Functions report OS related errors by raising either OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li>
</ul> </ul>
<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;command&gt;'</span>) <span class="hljs-comment"># Executes command in sh/cmd and returns its stdout pipe.</span> <div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;command&gt;'</span>) <span class="hljs-comment"># Executes command in sh/cmd. Returns its stdout pipe.</span>
&lt;str&gt; = &lt;pipe&gt;.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span> &lt;str&gt; = &lt;pipe&gt;.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span>
&lt;int&gt; = &lt;pipe&gt;.close() <span class="hljs-comment"># Closes the pipe. Returns None on success, int on error.</span> &lt;int&gt; = &lt;pipe&gt;.close() <span class="hljs-comment"># Closes the pipe. Returns None on success.</span>
</code></pre></div> </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">&gt;&gt;&gt; </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>) <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">&gt;&gt;&gt; </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>)
@ -1470,8 +1470,8 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</code></pre></div> </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 <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
&lt;str&gt; = json.dumps(&lt;object&gt;) <span class="hljs-comment"># Converts object to JSON string.</span> &lt;str&gt; = json.dumps(&lt;object&gt;) <span class="hljs-comment"># Converts object to JSON string.</span>
&lt;object&gt; = json.loads(&lt;str&gt;) <span class="hljs-comment"># Converts JSON string to object.</span> &lt;object&gt; = json.loads(&lt;str&gt;) <span class="hljs-comment"># Converts JSON string to object.</span>
</code></pre></div> </code></pre></div>
@ -1486,8 +1486,8 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</code></pre></div> </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 <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
&lt;bytes&gt; = pickle.dumps(&lt;object&gt;) <span class="hljs-comment"># Converts object to bytes object.</span> &lt;bytes&gt; = pickle.dumps(&lt;object&gt;) <span class="hljs-comment"># Converts object to bytes object.</span>
&lt;object&gt; = pickle.loads(&lt;bytes&gt;) <span class="hljs-comment"># Converts bytes object to object.</span> &lt;object&gt; = pickle.loads(&lt;bytes&gt;) <span class="hljs-comment"># Converts bytes object to object.</span>
</code></pre></div> </code></pre></div>
@ -2906,7 +2906,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer> <footer>
<aside>July 30, 2022</aside> <aside>September 4, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

8
parse.js

@ -86,12 +86,12 @@ const DATACLASS =
'&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])'; '&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])';
const SHUTIL_COPY = const SHUTIL_COPY =
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' + 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n'; 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
const OS_RENAME = const OS_RENAME =
'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' + 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n'; 'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
const STRUCT_FORMAT = const STRUCT_FORMAT =
'<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>'; '<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>';

|||||||
100:0
Loading…
Cancel
Save