From 157455eb35295c39fdcea94b396ebea0f52293b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 21 Sep 2019 17:38:31 +0200 Subject: [PATCH] Dataclass --- README.md | 11 ++++++++--- index.html | 10 ++++++---- parse.js | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 44585a9..16131c4 100644 --- a/README.md +++ b/README.md @@ -1041,6 +1041,12 @@ class : * **Function field() is needed because `': list = []'` would make a list that is shared among all instances.** * **Default_factory can be any [callable](#callable).** +#### Inline: +```python +from dataclasses import make_dataclass + = make_dataclass('', ) +``` + ### Slots **Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.** @@ -2232,7 +2238,6 @@ def printer(): reader(adder(printer())) # 100, 101, ..., 109 ``` -
Libraries ========= @@ -2272,7 +2277,7 @@ with open('test.csv', encoding='utf-8', newline='') as file: rows = csv.reader(file) header = [a.title() for a in next(rows)] table = tabulate.tabulate(rows, header) -print(table) + print(table) ``` @@ -2801,8 +2806,8 @@ Basic Script Template from collections import namedtuple from dataclasses import make_dataclass from enum import Enum +from sys import argv import re -import sys def main(): diff --git a/index.html b/index.html index 178ab70..90a5ee7 100644 --- a/index.html +++ b/index.html @@ -994,6 +994,9 @@ Z = dataclasses.make_dataclass('Z', [Function field() is needed because '<attr_name>: list = []' would make a list that is shared among all instances.
  • Default_factory can be any callable.
  • +

    Inline:

    from dataclasses import make_dataclass
    +<class> = make_dataclass('<class_name>', <list_of_attribute_names>)
    +

    Slots

    Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.

    class MyClassWithSlots:
         __slots__ = ['a']
         def __init__(self):
    @@ -1235,7 +1238,7 @@ member_names    = [a.name for a for a in <enum>]
     random_member   = random.choice(list(<enum>))
     
    -

    Inline

    Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
    +

    Inline

    Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
     Cutlery = Enum('Cutlery', 'fork knife spoon')
     Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
     
    @@ -1940,7 +1943,6 @@ ValueError: malformed node or string reader(adder(printer())) # 100, 101, ..., 109
    -


    Libraries

    #Progress Bar

    # $ pip3 install tqdm
     from tqdm import tqdm
     from time import sleep
    @@ -1965,7 +1967,7 @@ pyplot.clf()                           # Clears figur
         rows   = csv.reader(file)
         header = [a.title() for a in next(rows)]
         table  = tabulate.tabulate(rows, header)
    -print(table)
    +    print(table)
     
    @@ -2382,8 +2384,8 @@ simpleaudio.play_buffer(frames_b, 1, from collections import namedtuple from dataclasses import make_dataclass from enum import Enum +from sys import argv import re -import sys def main(): diff --git a/parse.js b/parse.js index d58fdda..f7bf668 100755 --- a/parse.js +++ b/parse.js @@ -221,6 +221,10 @@ const LRU_CACHE = const TYPE = '<class> = type(<class_name>, <parents_tuple>, <attributes_dict>)'; +const DATACLASS = + 'from dataclasses import make_dataclass\n' + + '<class> = make_dataclass(\'<class_name>\', <list_of_attribute_names>)'; + function main() { const html = getMd(); @@ -324,6 +328,7 @@ function fixHighlights() { $(`code:contains(ValueError: malformed node)`).html(EVAL); $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE); $(`code:contains(, , )`).html(TYPE); + $(`code:contains()`).html(DATACLASS); } function preventPageBreaks() {