From 2be9c2df86dfd98a8ece1bfed24a2d255785f3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 7 Oct 2021 10:03:17 +0200 Subject: [PATCH] Format, Class, Enum, Exception --- README.md | 20 ++++++++++---------- index.html | 24 ++++++++++++------------ web/faq.html | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7c213e3..fde4a5d 100644 --- a/README.md +++ b/README.md @@ -425,12 +425,12 @@ Format ### Numbers ```python -{ 123456:10} # ' 123456' -{ 123456:10,} # ' 123,456' -{ 123456:10_} # ' 123_456' -{ 123456:+10} # ' +123456' -{-123456:=10} # '- 123456' -{ 123456: } # ' 123456' +{123456:10} # ' 123456' +{123456:10,} # ' 123,456' +{123456:10_} # ' 123_456' +{123456:+10} # ' +123456' +{123456:=+10} # '+ 123456' +{123456: } # ' 123456' {-123456: } # '-123456' ``` @@ -1031,6 +1031,7 @@ class : : = : list/dict/set = field(default_factory=list/dict/set) ``` +* **For arbitrary type use `'typing.Any'`.** * **Objects can be made sortable with `'order=True'` and immutable with `'frozen=True'`.** * **For object to be hashable, all attributes must be hashable and frozen must be True.** * **Function field() is needed because `': list = []'` would make a list that is shared among all instances.** @@ -1333,9 +1334,9 @@ Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3}) ```python from functools import partial LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r), - 'OR' : partial(lambda l, r: l or r)}) + 'OR': partial(lambda l, r: l or r)}) ``` -* **Another solution in this particular case is to use functions and\_() and or\_() from the module [operator](#operator).** +* **Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.** Exceptions @@ -1362,7 +1363,7 @@ finally: ``` * **Code inside the `'else'` block will only be executed if `'try'` block had no exceptions.** -* **Code inside the `'finally'` block will always be executed.** +* **Code inside the `'finally'` block will always be executed (unless a signal is received).** ### Catching Exceptions ```python @@ -2131,7 +2132,6 @@ sorted_by_second = sorted(, key=op.itemgetter(1)) sorted_by_both = sorted(, key=op.itemgetter(1, 0)) product_of_elems = functools.reduce(op.mul, ) union_of_sets = functools.reduce(op.or_, ) -LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR': op.or_}) last_el = op.methodcaller('pop')() ``` diff --git a/index.html b/index.html index 1d802bb..3326aea 100644 --- a/index.html +++ b/index.html @@ -226,7 +226,7 @@ pre.prettyprint {
- +
@@ -546,12 +546,12 @@ to_exclusive = <range>.stop {'abcde'!r:10} # "'abcde' " -

Numbers

{ 123456:10}                                   # '    123456'
-{ 123456:10,}                                  # '   123,456'
-{ 123456:10_}                                  # '   123_456'
-{ 123456:+10}                                  # '   +123456'
-{-123456:=10}                                  # '-   123456'
-{ 123456: }                                    # ' 123456'
+

Numbers

{123456:10}                                    # '    123456'
+{123456:10,}                                   # '   123,456'
+{123456:10_}                                   # '   123_456'
+{123456:+10}                                   # '   +123456'
+{123456:=+10}                                  # '+   123456'
+{123456: }                                     # ' 123456'
 {-123456: }                                    # '-123456'
 
@@ -1025,6 +1025,7 @@ Z = dataclasses.make_dataclass('Z', [ +
  • For arbitrary type use 'typing.Any'.
  • Objects can be made sortable with 'order=True' and immutable with 'frozen=True'.
  • For object to be hashable, all attributes must be hashable and frozen must be True.
  • Function field() is needed because '<attr_name>: list = []' would make a list that is shared among all instances.
  • @@ -1291,11 +1292,11 @@ Cutlery = Enum('Cutlery', {User-defined functions cannot be values, so they must be wrapped:
    from functools import partial
     LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
    -                           'OR' : partial(lambda l, r: l or r)})
    +                           'OR':  partial(lambda l, r: l or r)})
     
      -
    • Another solution in this particular case is to use functions and_() and or_() from the module operator.
    • +
    • Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.

    #Exceptions

    Basic Example

    try:
         <code>
    @@ -1318,7 +1319,7 @@ LogicOp = Enum('LogicOp', {'else' block will only be executed if 'try' block had no exceptions.
    -
  • Code inside the 'finally' block will always be executed.
  • +
  • Code inside the 'finally' block will always be executed (unless a signal is received).
  • Catching Exceptions

    except <exception>:
     except <exception> as <name>:
    @@ -1903,7 +1904,6 @@ sorted_by_second = sorted(<collection>, key=op.itemgetter(1, 0))
     product_of_elems = functools.reduce(op.mul, <collection>)
     union_of_sets    = functools.reduce(op.or_, <coll_of_sets>)
    -LogicOp          = enum.Enum('LogicOp', {'AND': op.and_, 'OR': op.or_})
     last_el          = op.methodcaller('pop')(<list>)
     

    #Introspection

    Inspecting code at runtime.

    Variables

    <list> = dir()                             # Names of local variables (incl. functions).
    @@ -3007,7 +3007,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
      
     
       
     
    diff --git a/web/faq.html b/web/faq.html
    index 088ed61..1c659c3 100644
    --- a/web/faq.html
    +++ b/web/faq.html
    @@ -1,5 +1,5 @@
     
    Python 2 or Python 3?
    -    Python 3.6 (Except for dataclasses and asyncio that require version 3.7). +    Python 3.7

    What is the best way to use it?