From 4f6d5cf3acc62b666dd847aa2c5b8c3a85fd14aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 15 Apr 2022 11:28:56 +0200 Subject: [PATCH] Format, Numbers, Decorator, Class --- README.md | 10 ++++++---- index.html | 16 ++++++++++------ parse.js | 7 +++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c4c0baf..8a5ce7d 100644 --- a/README.md +++ b/README.md @@ -472,7 +472,7 @@ Format +--------------+----------------+----------------+----------------+----------------+ ``` * **When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes `'{6.5:.0f}'` a `'6'` and `'{7.5:.0f}'` an `'8'`.** -* **This rule only works for numbers that can be represented exactly by a float (`.5`, `.25`, …).** +* **This rule only effects numbers that can be represented exactly by a float (`.5`, `.25`, …).** ### Ints ```python @@ -493,7 +493,7 @@ Numbers = decimal.Decimal() # Or: Decimal((sign, digits, exponent)) ``` * **`'int()'` and `'float()'` raise ValueError on malformed strings.** -* **All decimal numbers are stored exactly, unlike floats where `'1.1 + 2.2 != 3.3'`.** +* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.** * **Precision of decimal operations is set with: `'decimal.getcontext().prec = '`.** ### Basic Functions @@ -921,6 +921,7 @@ from functools import lru_cache def fib(n): return n if n < 2 else fib(n-2) + fib(n-1) ``` +* **Default size of the cache is 128 values. Passing `'maxsize=None'` makes it unbounded.** * **CPython interpreter limits recursion depth to 1000 by default. To increase it use `'sys.setrecursionlimit()'`.** ### Parametrized Decorator @@ -942,6 +943,7 @@ def debug(print_result=False): def add(x, y): return x + y ``` +* **Using only `'@debug'` to decorate the add() function would not work here, because debug would then receive the add() function as a 'print_result' argument. Decorators can however manually check if the argument they received is a function and act accordingly.** Class @@ -974,9 +976,9 @@ raise Exception() #### Repr() use cases: ```python -print([]) +print/str/repr([]) f'{!r}' -Z = dataclasses.make_dataclass('Z', ['a']); print(Z()) +Z = dataclasses.make_dataclass('Z', ['a']); print/str/repr(Z()) >>> ``` diff --git a/index.html b/index.html index cb99022..1e609fc 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -437,7 +437,7 @@ to_exclusive = <range>.stop
  • When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes '{6.5:.0f}' a '6' and '{7.5:.0f}' an '8'.
  • -
  • This rule only works for numbers that can be represented exactly by a float (.5, .25, …).
  • +
  • This rule only effects numbers that can be represented exactly by a float (.5, .25, …).

Ints

{90:c}                                   # 'Z'
 {90:b}                                   # '1011010'
@@ -454,7 +454,7 @@ to_exclusive   = <range>.stop
 
 
  • 'int(<str>)' and 'float(<str>)' raise ValueError on malformed strings.
  • -
  • All decimal numbers are stored exactly, unlike floats where '1.1 + 2.2 != 3.3'.
  • +
  • Decimal numbers are stored exactly, unlike most floats where '1.1 + 2.2 != 3.3'.
  • Precision of decimal operations is set with: 'decimal.getcontext().prec = <int>'.

Basic Functions

<num> = pow(<num>, <num>)                # Or: <num> ** <num>
@@ -784,6 +784,7 @@ creature = Creature(point, direction)
 
 
 
    +
  • Default size of the cache is 128 values. Passing 'maxsize=None' makes it unbounded.
  • CPython interpreter limits recursion depth to 1000 by default. To increase it use 'sys.setrecursionlimit(<depth>)'.

Parametrized Decorator

A decorator that accepts arguments and returns a normal decorator that accepts a function.

from functools import wraps
@@ -804,6 +805,9 @@ creature = Creature(point, direction)
 
+
    +
  • Using only '@debug' to decorate the add() function would not work here, because debug would then receive the add() function as a 'print_result' argument. Decorators can however manually check if the argument they received is a function and act accordingly.
  • +

#Class

class <name>:
     def __init__(self, a):
         self.a = a
@@ -829,9 +833,9 @@ csv.writer(<file>).writerow([<el>])
 raise Exception(<el>)
 
-

Repr() use cases:

print([<el>])
+

Repr() use cases:

print/str/repr([<el>])
 f'{<el>!r}'
-Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
+Z = dataclasses.make_dataclass('Z', ['a']); print/str/repr(Z(<el>))
 >>> <el>
 
@@ -2885,7 +2889,7 @@ $ pyinstaller script.py --add-data '<path>:.' diff --git a/parse.js b/parse.js index 4bab160..8eb91cb 100755 --- a/parse.js +++ b/parse.js @@ -51,6 +51,12 @@ const LRU_CACHE = 'def fib(n):\n' + ' return n if n < 2 else fib(n-2) + fib(n-1)\n'; +const REPR_USE_CASES = + 'print/str/repr([<el>])\n' + + 'f\'{<el>!r}\'\n' + + 'Z = dataclasses.make_dataclass(\'Z\', [\'a\']); print/str/repr(Z(<el>))\n' + + '>>> <el>\n'; + const CONSTRUCTOR_OVERLOADING = 'class <name>:\n' + ' def __init__(self, a=None):\n' + @@ -562,6 +568,7 @@ function fixClasses() { function fixHighlights() { $(`code:contains(@lru_cache(maxsize=None))`).html(LRU_CACHE); $(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING); + $(`code:contains(print/str/repr([]))`).html(REPR_USE_CASES); $(`code:contains(make_dataclass(\'\')`).html(DATACLASS); $(`code:contains(shutil.copy)`).html(SHUTIL_COPY); $(`code:contains(os.rename)`).html(OS_RENAME);