From cc63249eee8a343424c4ba62cd798cac4ae1b69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 11 Jul 2024 11:37:32 +0200 Subject: [PATCH] String, Duck types, Enum --- README.md | 9 +++++---- index.html | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a6a7957..3421806 100644 --- a/README.md +++ b/README.md @@ -319,7 +319,7 @@ String = in # Checks if string contains the substring. = .startswith() # Pass tuple of strings for multiple options. = .find() # Returns start index of the first match or -1. - = .index() # Same, but raises ValueError if missing. + = .index() # Same, but raises ValueError if there's no match. ``` ```python @@ -1136,7 +1136,7 @@ class MyHashable: * **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.** * **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.** * **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.** -* **For proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.** +* **To sort collection of strings in proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.** ```python from functools import total_ordering @@ -1184,7 +1184,8 @@ class Counter: ### Callable * **All functions and classes have a call() method, hence are callable.** -* **When this cheatsheet uses `''` as an argument, it actually means `''`.** +* **To check if object is callable use `'callable()'`, `'isinstance(, collections.abc.Callable)'`, or `'isinstance(, typing.Callable)'`.** +* **When this cheatsheet uses `''` as an argument, it means `''`.** ```python class Counter: def __init__(self): @@ -1337,7 +1338,7 @@ from enum import Enum, auto class (Enum): = auto() # Increment of the last numeric value or 1. = # Values don't have to be hashable. - = , # Values can be collections (like this tuple). + = , # Values can be collections (this is a tuple). ``` * **Methods receive the member they were called on as the 'self' argument.** * **Accessing a member named after a reserved keyword causes SyntaxError.** diff --git a/index.html b/index.html index 4986253..ed7f7ad 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -305,7 +305,7 @@ Point(x=1, y=2
<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 missing.
+<int>  = <str>.index(<sub_str>)              # Same, but raises ValueError if there's no match.
 
<str>  = <str>.lower()                       # Changes the case. Also upper/capitalize/title().
 <str>  = <str>.replace(old, new [, count])   # Replaces 'old' with 'new' at most 'count' times.
@@ -956,7 +956,7 @@ Z = dataclasses.make_dataclass('Z', [With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.
 
  • Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.
  • When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.
  • -
  • For proper alphabetical order pass 'key=locale.strxfrm' to sorted() after running 'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'.
  • +
  • To sort collection of strings in proper alphabetical order pass 'key=locale.strxfrm' to sorted() after running 'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'.
  • from functools import total_ordering
     
     @total_ordering
    @@ -1000,7 +1000,8 @@ Z = dataclasses.make_dataclass('Z', [File objects returned by the open() function, etc.
     

    Callable

    • All functions and classes have a call() method, hence are callable.
    • -
    • When this cheatsheet uses '<function>' as an argument, it actually means '<callable>'.
    • +
    • To check if object is callable use 'callable(<obj>)', 'isinstance(<obj>, collections.abc.Callable)', or 'isinstance(<obj>, typing.Callable)'.
    • +
    • When this cheatsheet uses '<function>' as an argument, it means '<callable>'.
    class Counter:
         def __init__(self):
             self.i = 0
    @@ -1142,7 +1143,7 @@ Hello World!
     
    class <enum_name>(Enum):
         <member_name> = auto()              # Increment of the last numeric value or 1.
         <member_name> = <value>             # Values don't have to be hashable.
    -    <member_name> = <value>, <value>    # Values can be collections (like this tuple).
    +    <member_name> = <el_1>, <el_2>      # Values can be collections (this is a tuple).
     
    • Methods receive the member they were called on as the 'self' argument.
    • @@ -2931,7 +2932,7 @@ $ deactivate # Deactivates the activ