Browse Source

String, Cython

pull/135/merge
Jure Šorn 1 week ago
parent
commit
36f823c8d5
3 changed files with 40 additions and 33 deletions
  1. 27
      README.md
  2. 29
      index.html
  3. 17
      parse.js

27
README.md

@ -327,11 +327,11 @@ String
<bool> = <sub_str> in <str> # Checks if string contains the substring.
<bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
<int> = <str>.find(<sub_str>) # Returns start index of the first match or -1.
<int> = <str>.index(<sub_str>) # Same, but raises ValueError if there's no match.
```
```python
<str> = <str>.lower() # Changes the case. Also upper/capitalize/title().
<str> = <str>.lower() # Lowers the case. Also upper/capitalize/title().
<str> = <str>.casefold() # Same, but converts ẞ/ß to ss, Σ/ς to σ, etc.
<str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
<str> = <str>.translate(<table>) # Use `str.maketrans(<dict>)` to generate table.
```
@ -2788,7 +2788,7 @@ from PIL import Image
### Modes
* **`'L'` - Lightness (greyscale image). Each pixel is an integer between 0 and 255.**
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three integers.**
* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.**
* **`'RGBA'` - RGB with alpha. Low alpha (i.e. fourth int) makes pixel more transparent.**
* **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.**
@ -3410,7 +3410,7 @@ import plotly.express as px, pandas as pd
```python
<Fig> = px.line(<DF> [, y=col_key/s [, x=col_key]]) # Also px.line(y=<list> [, x=<list>]).
<Fig>.update_layout(paper_bgcolor='rgb(0, 0, 0)') # Also `margin=dict(t=0, r=0, b=0, l=0)`.
<Fig>.update_layout(paper_bgcolor='#rrggbb') # Also `margin=dict(t=0, r=0, b=0, l=0)`.
<Fig>.write_html/json/image('<path>') # Use <Fig>.show() to display the plot.
```
@ -3518,21 +3518,24 @@ import <cython_script> # Script must be saved with '.pyx' extens
<cython_script>.main() # Main() isn't automatically executed.
```
#### Definitions:
* **All `'cdef'` definitions are optional, but they contribute to the speed-up.**
* **Also supports C pointers (via `'*'` and `'&'`), structs, unions and enums.**
#### All `'cdef'` definitions are optional, but they contribute to the speed-up:
```python
cdef <ctype/type> <var_name> [= <obj>]
cdef <ctype/type> [*]<var_name> [= <obj>]
cdef <ctype>[n_elements] <var_name> [= <coll_of_nums>]
cdef <ctype/type/void> <func_name>(<ctype/type> <arg_name>): ...
cdef <ctype/type/void> <func_name>(<ctype/type> [*]<arg_name>): ...
```
```python
cdef class <class_name>:
cdef public <ctype/type> <attr_name>
def __init__(self, <ctype/type> <arg_name>):
self.<attr_name> = <arg_name>
cdef public <ctype/type> [*]<attr_name>
def __init__(self, <ctype/type> [*]<arg_name>):
self.<attr_name> = [&]<arg_name>
```
```python
cdef struct <struct_name>:
<ctype> [*]<field_name>
```
### Virtual Environments

29
index.html

@ -56,7 +56,7 @@
<body>
<header>
<aside>May 31, 2025</aside>
<aside>June 2, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -317,9 +317,9 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;sub_str&gt; <span class="hljs-keyword">in</span> &lt;str&gt; <span class="hljs-comment"># Checks if string contains the substring.</span>
&lt;bool&gt; = &lt;str&gt;.startswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same, but raises ValueError if there's no match.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.lower() <span class="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.lower() <span class="hljs-comment"># Lowers the case. Also upper/capitalize/title().</span>
&lt;str&gt; = &lt;str&gt;.casefold() <span class="hljs-comment"># Same, but converts ẞ/ß to ss, Σ/ς to σ, etc.</span>
&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
&lt;str&gt; = &lt;str&gt;.translate(&lt;table&gt;) <span class="hljs-comment"># Use `str.maketrans(&lt;dict&gt;)` to generate table.</span>
</code></pre>
@ -2288,7 +2288,7 @@ right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-numb
<div><h3 id="modes-1">Modes</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an integer between 0 and 255.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three integers.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. fourth int) makes pixel more transparent.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - Hue, saturation, value. Three ints representing color in HSV 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
@ -2789,7 +2789,7 @@ z
</code></pre></div>
<pre><code class="python language-python hljs">&lt;Fig&gt; = px.line(&lt;DF&gt; [, y=col_key/s [, x=col_key]]) <span class="hljs-comment"># Also px.line(y=&lt;list&gt; [, x=&lt;list&gt;]).</span>
&lt;Fig&gt;.update_layout(paper_bgcolor=<span class="hljs-string">'rgb(0, 0, 0)'</span>) <span class="hljs-comment"># Also `margin=dict(t=0, r=0, b=0, l=0)`.</span>
&lt;Fig&gt;.update_layout(paper_bgcolor=<span class="hljs-string">'#rrggbb'</span>) <span class="hljs-comment"># Also `margin=dict(t=0, r=0, b=0, l=0)`.</span>
&lt;Fig&gt;.write_html/json/image(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Use &lt;Fig&gt;.show() to display the plot.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Fig&gt; = px.area/bar/box(&lt;DF&gt;, x=col_key, y=col_keys) <span class="hljs-comment"># Also `color=col_key`. All are optional.</span>
@ -2881,19 +2881,18 @@ px.line(df, x=<span class="hljs-string">'Date'</span>, y=<span class="hljs-strin
<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>Also supports C pointers (via <code class="python hljs"><span class="hljs-string">'*'</span></code> and <code class="python hljs"><span class="hljs-string">'&amp;'</span></code>), structs, unions and enums.</strong></li>
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">cdef</span> &lt;ctype/type&gt; &lt;var_name&gt; [= &lt;obj&gt;]
<div><h4 id="allcdefdefinitionsareoptionalbuttheycontributetothespeedup">All <code class="python hljs"><span class="hljs-string">'cdef'</span></code> definitions are optional, but they contribute to the speed-up:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">cdef</span> &lt;ctype/type&gt; [*]&lt;var_name&gt; [= &lt;obj&gt;]
<span class="hljs-keyword">cdef</span> &lt;ctype&gt;[n_elements] &lt;var_name&gt; [= &lt;coll_of_nums&gt;]
<span class="hljs-keyword">cdef</span> &lt;ctype/type/void&gt; &lt;func_name&gt;(&lt;ctype/type&gt; &lt;arg_name&gt;): ...
<span class="hljs-keyword">cdef</span> &lt;ctype/type/void&gt; &lt;func_name&gt;(&lt;ctype/type&gt; [*]&lt;arg_name&gt;): ...
</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> &lt;<span class="hljs-title">class_name</span>&gt;:</span>
<span class="hljs-keyword">cdef</span> <span class="hljs-keyword">public</span> &lt;ctype/type&gt; &lt;attr_name&gt;
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, &lt;ctype/type&gt; &lt;arg_name&gt;)</span>:</span>
self.&lt;attr_name&gt; = &lt;arg_name&gt;
<span class="hljs-keyword">cdef</span> <span class="hljs-keyword">public</span> &lt;ctype/type&gt; [*]&lt;attr_name&gt;
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, &lt;ctype/type&gt; [*]&lt;arg_name&gt;)</span>:</span>
self.&lt;attr_name&gt; = [&amp;]&lt;arg_name&gt;
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-keyword">cdef</span> <span class="hljs-keyword">struct</span> &lt;<span class="hljs-title">struct_name</span>&gt;:
&lt;ctype&gt; [*]&lt;field_name&gt;
</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="python hljs">$ python3 -m venv NAME <span class="hljs-comment"># Creates virtual environment in current directory.</span>
$ source NAME/bin/activate <span class="hljs-comment"># Activates it. On Windows run `NAME\Scripts\activate`.</span>
@ -2940,7 +2939,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>May 31, 2025</aside>
<aside>June 2, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

17
parse.js

@ -327,15 +327,19 @@ const GROUPBY =
const CYTHON_1 =
'<span class="hljs-keyword">cdef</span> &lt;ctype/type&gt; &lt;var_name&gt; [= &lt;obj&gt;]\n' +
'<span class="hljs-keyword">cdef</span> &lt;ctype/type&gt; [*]&lt;var_name&gt; [= &lt;obj&gt;]\n' +
'<span class="hljs-keyword">cdef</span> &lt;ctype&gt;[n_elements] &lt;var_name&gt; [= &lt;coll_of_nums&gt;]\n' +
'<span class="hljs-keyword">cdef</span> &lt;ctype/type/void&gt; &lt;func_name&gt;(&lt;ctype/type&gt; &lt;arg_name&gt;): ...\n';
'<span class="hljs-keyword">cdef</span> &lt;ctype/type/void&gt; &lt;func_name&gt;(&lt;ctype/type&gt; [*]&lt;arg_name&gt;): ...\n';
const CYTHON_2 =
'<span class="hljs-keyword">cdef</span> <span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">class_name</span>&gt;:</span>\n' +
' <span class="hljs-keyword">cdef</span> <span class="hljs-keyword">public</span> &lt;ctype/type&gt; &lt;attr_name&gt;\n' +
' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, &lt;ctype/type&gt; &lt;arg_name&gt;)</span>:</span>\n' +
' self.&lt;attr_name&gt; = &lt;arg_name&gt;\n';
' <span class="hljs-keyword">cdef</span> <span class="hljs-keyword">public</span> &lt;ctype/type&gt; [*]&lt;attr_name&gt;\n' +
' <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, &lt;ctype/type&gt; [*]&lt;arg_name&gt;)</span>:</span>\n' +
' self.&lt;attr_name&gt; = [&amp;]&lt;arg_name&gt;\n';
const CYTHON_3 =
'<span class="hljs-keyword">cdef</span> <span class="hljs-keyword">struct</span> &lt;<span class="hljs-title">struct_name</span>&gt;:\n' +
' &lt;ctype&gt; [*]&lt;field_name&gt;\n';
const INDEX =
'<li><strong>Ctrl+F / ⌘F is usually sufficient.</strong></li>\n' +
@ -938,8 +942,9 @@ function fixHighlights() {
$(`code:contains(samples_f = (sin(i *)`).html(AUDIO_2);
$(`code:contains(collections, dataclasses, enum, io, itertools)`).html(MARIO);
$(`code:contains(>>> gb = df.groupby)`).html(GROUPBY);
$(`code:contains(cdef <ctype/type> <var_name> [= <obj>])`).html(CYTHON_1);
$(`code:contains(cdef <ctype/type> [*]<var_name> [= <obj>])`).html(CYTHON_1);
$(`code:contains(cdef class <class_name>:)`).html(CYTHON_2);
$(`code:contains(cdef struct <struct_name>:)`).html(CYTHON_3);
$(`ul:contains(Ctrl+F / ⌘F is usually sufficient.)`).html(INDEX);
}

Loading…
Cancel
Save