From 34bda0ca523b79139e6a56c9b789051c7c059253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 13 Oct 2024 16:44:05 +0200 Subject: [PATCH] Match statement --- README.md | 11 ++++++----- index.html | 17 ++++++++--------- parse.js | 8 +++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 8357f71..868f758 100644 --- a/README.md +++ b/README.md @@ -2171,6 +2171,9 @@ Operator **Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.** ```python import operator as op +``` + +```python = op.not_() # or, and, not (or/and missing) = op.eq/ne/lt/ge/is_/is_not/contains(, ) # ==, !=, <, >=, is, is not, in = op.or_/xor/and_(, ) # |, ^, & @@ -2227,11 +2230,9 @@ match : >>> from pathlib import Path >>> match Path('/home/gto/python-cheatsheet/README.md'): ... case Path( -... parts=['/', 'home', user, *_], -... stem=stem, -... suffix=('.md' | '.txt') as suffix -... ) if stem.lower() == 'readme': -... print(f'{stem}{suffix} is a readme file that belongs to user {user}.') +... parts=['/', 'home', user, *_] +... ) as p if p.stem.lower() == 'readme' and p.suffix.lower() in ['.md', '.txt']: +... print(f'{p.name} is a readme file that belongs to user {user}.') 'README.md is a readme file that belongs to user gto.' ``` diff --git a/index.html b/index.html index 2fe26e5..480394b 100644 --- a/index.html +++ b/index.html @@ -1775,7 +1775,10 @@ CompletedProcess(args=['bc', pickable, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via 'if __name__ == "__main__": ...'.

#Operator

Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.

import operator as op
-<bool> = op.not_(<obj>)                                        # or, and, not (or/and missing)
+
+ + +
<bool> = op.not_(<obj>)                                        # or, and, not (or/and missing)
 <bool> = op.eq/ne/lt/ge/is_/is_not/contains(<obj>, <obj>)      # ==, !=, <, >=, is, is not, in
 <obj>  = op.or_/xor/and_(<int/set>, <int/set>)                 # |, ^, &
 <int>  = op.lshift/rshift(<int>, <int>)                        # <<, >>
@@ -1783,9 +1786,7 @@ CompletedProcess(args=['bc', # -, ~
 <num>  = op.pow(<num>, <num>)                                  # **
 <func> = op.itemgetter/attrgetter/methodcaller(<obj> [, ...])  # [index/key], .name, .name([…])
-
- - +
elementwise_sum  = map(op.add, list_a, list_b)
 sorted_by_second = sorted(<coll.>, key=op.itemgetter(1))
 sorted_by_both   = sorted(<coll.>, key=op.itemgetter(1, 0))
@@ -1825,11 +1826,9 @@ first_element    = op.methodcaller('pop', 

Example

>>> from pathlib import Path
 >>> match Path('/home/gto/python-cheatsheet/README.md'):
 ...     case Path(
-...         parts=['/', 'home', user, *_],
-...         stem=stem,
-...         suffix=('.md' | '.txt') as suffix
-...     ) if stem.lower() == 'readme':
-...         print(f'{stem}{suffix} is a readme file that belongs to user {user}.')
+...         parts=['/', 'home', user, *_]
+...     ) as p if p.stem.lower() == 'readme' and p.suffix.lower() in ['.md', '.txt']:
+...         print(f'{p.name} is a readme file that belongs to user {user}.')
 'README.md is a readme file that belongs to user gto.'
 
diff --git a/parse.js b/parse.js index 62c9514..7dc0e15 100755 --- a/parse.js +++ b/parse.js @@ -112,11 +112,9 @@ const MATCH_EXAMPLE = '>>> from pathlib import Path\n' + '>>> match Path(\'/home/gto/python-cheatsheet/README.md\'):\n' + '... case Path(\n' + - '... parts=[\'/\', \'home\', user, *_],\n' + - '... stem=stem,\n' + - '... suffix=(\'.md\' | \'.txt\') as suffix\n' + - '... ) if stem.lower() == \'readme\':\n' + - '... print(f\'{stem}{suffix} is a readme file that belongs to user {user}.\')\n' + + '... parts=[\'/\', \'home\', user, *_]\n' + + '... ) as p if p.stem.lower() == \'readme\' and p.suffix.lower() in [\'.md\', \'.txt\']:\n' + + '... print(f\'{p.name} is a readme file that belongs to user {user}.\')\n' + '\'README.md is a readme file that belongs to user gto.\'\n'; const COROUTINES =