Skip to content

Commit 157455e

Browse files
committed
Dataclass
1 parent b147b3b commit 157455e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,12 @@ class <class_name>:
10411041
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
10421042
* **Default_factory can be any [callable](#callable).**
10431043

1044+
#### Inline:
1045+
```python
1046+
from dataclasses import make_dataclass
1047+
<class> = make_dataclass('<class_name>', <list_of_attribute_names>)
1048+
```
1049+
10441050
### Slots
10451051
**Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.**
10461052

@@ -2232,7 +2238,6 @@ def printer():
22322238
reader(adder(printer())) # 100, 101, ..., 109
22332239
```
22342240

2235-
<br>
22362241

22372242
Libraries
22382243
=========
@@ -2272,7 +2277,7 @@ with open('test.csv', encoding='utf-8', newline='') as file:
22722277
rows = csv.reader(file)
22732278
header = [a.title() for a in next(rows)]
22742279
table = tabulate.tabulate(rows, header)
2275-
print(table)
2280+
print(table)
22762281
```
22772282

22782283

@@ -2801,8 +2806,8 @@ Basic Script Template
28012806
from collections import namedtuple
28022807
from dataclasses import make_dataclass
28032808
from enum import Enum
2809+
from sys import argv
28042810
import re
2805-
import sys
28062811

28072812

28082813
def main():

index.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,9 @@
994994
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances.</strong></li>
995995
<li><strong>Default_factory can be any <a href="#callable">callable</a>.</strong></li>
996996
</ul>
997+
<div><h4 id="inline-1">Inline:</h4><pre><code class="python language-python hljs"><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
998+
&lt;class&gt; = make_dataclass<span class="hljs-params">(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;list_of_attribute_names&gt;)</span></code></code></pre></div>
999+
9971000
<div><h3 id="slots">Slots</h3><p><strong>Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClassWithSlots</span>:</span>
9981001
__slots__ = [<span class="hljs-string">'a'</span>]
9991002
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
@@ -1235,7 +1238,7 @@
12351238
member_values = [a.value <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> &lt;enum&gt;]
12361239
random_member = random.choice(list(&lt;enum&gt;))
12371240
</code></pre>
1238-
<div><h3 id="inline-1">Inline</h3><pre><code class="python language-python hljs">Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, [<span class="hljs-string">'fork'</span>, <span class="hljs-string">'knife'</span>, <span class="hljs-string">'spoon'</span>])
1241+
<div><h3 id="inline-2">Inline</h3><pre><code class="python language-python hljs">Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, [<span class="hljs-string">'fork'</span>, <span class="hljs-string">'knife'</span>, <span class="hljs-string">'spoon'</span>])
12391242
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, <span class="hljs-string">'fork knife spoon'</span>)
12401243
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-string">'fork'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'knife'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'spoon'</span>: <span class="hljs-number">3</span>})
12411244
</code></pre></div>
@@ -1940,7 +1943,6 @@
19401943
reader(adder(printer())) <span class="hljs-comment"># 100, 101, ..., 109</span>
19411944
</code></pre></div>
19421945

1943-
<p><br></p>
19441946
<div><h1 id="libraries">Libraries</h1><div><h2 id="progressbar"><a href="#progressbar" name="progressbar">#</a>Progress Bar</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install tqdm</span>
19451947
<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm
19461948
<span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep
@@ -1965,7 +1967,7 @@
19651967
rows = csv.reader(file)
19661968
header = [a.title() <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> next(rows)]
19671969
table = tabulate.tabulate(rows, header)
1968-
print(table)
1970+
print(table)
19691971
</code></pre></div></div>
19701972

19711973

@@ -2382,8 +2384,8 @@
23822384
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
23832385
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
23842386
<span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
2387+
<span class="hljs-keyword">from</span> sys <span class="hljs-keyword">import</span> argv
23852388
<span class="hljs-keyword">import</span> re
2386-
<span class="hljs-keyword">import</span> sys
23872389

23882390

23892391
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>

parse.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ const LRU_CACHE =
221221
const TYPE =
222222
'<code class="python language-python hljs">&lt;class&gt; = type(&lt;class_name&gt;, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)</code>';
223223

224+
const DATACLASS =
225+
'<code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass\n' +
226+
'&lt;class&gt; = make_dataclass<span class="hljs-params">(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;list_of_attribute_names&gt;)</span></code>';
227+
224228

225229
function main() {
226230
const html = getMd();
@@ -324,6 +328,7 @@ function fixHighlights() {
324328
$(`code:contains(ValueError: malformed node)`).html(EVAL);
325329
$(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE);
326330
$(`code:contains(<class_name>, <parents_tuple>, <attributes_dict>)`).html(TYPE);
331+
$(`code:contains(<list_of_attribute_names>)`).html(DATACLASS);
327332
}
328333

329334
function preventPageBreaks() {

0 commit comments

Comments
 (0)