From 13d97443bafc06e386b2adac587b0f4dfaf89859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 1 Mar 2022 08:50:59 +0100 Subject: [PATCH] Coroutines, Pandas --- README.md | 26 +++++++++++++------------- index.html | 36 ++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 60d415fda..55f09046d 100644 --- a/README.md +++ b/README.md @@ -1122,7 +1122,7 @@ class MyHashable: ``` ### Sortable -* **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.** +* **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.** @@ -1525,11 +1525,11 @@ arguments = sys.argv[1:] ```python from argparse import ArgumentParser, FileType p = ArgumentParser(description=) -p.add_argument('-', '--', action='store_true') # Flag -p.add_argument('-', '--', type=) # Option -p.add_argument('', type=, nargs=1) # First argument -p.add_argument('', type=, nargs='+') # Remaining arguments -p.add_argument('', type=, nargs='*') # Optional arguments +p.add_argument('-', '--', action='store_true') # Flag. +p.add_argument('-', '--', type=) # Option. +p.add_argument('', type=, nargs=1) # First argument. +p.add_argument('', type=, nargs='+') # Remaining arguments. +p.add_argument('', type=, nargs='*') # Optional arguments. args = p.parse_args() # Exits on error. value = args. ``` @@ -2334,7 +2334,7 @@ async def human_controller(screen, moves): await asyncio.sleep(0.005) async def model(moves, state): - while state['*'] not in {p for id_, p in state.items() if id_ != '*'}: + while state['*'] not in (state[id_] for id_ in range(10)): id_, d = await moves.get() x, y = state[id_] deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)} @@ -3292,7 +3292,12 @@ b 3 4 ``` * **Use `'[col_key_1, col_key_2][row_key]'` to get the fifth result's values.** -#### DataFrame — Encode, Decode, Plot: +#### DataFrame — Plot, Encode, Decode: +```python +import matplotlib.pyplot as plt +.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show() +``` + ```python = pd.read_json/html('') = pd.read_csv/pickle/excel('') @@ -3307,11 +3312,6 @@ b 3 4 .to_sql('', ) ``` -```python -import matplotlib.pyplot as plt -.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show() -``` - ### GroupBy **Object that groups together rows of a dataframe based on the value of the passed column.** diff --git a/index.html b/index.html index 589747ba5..7edc05340 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -950,7 +950,7 @@

Sortable

    -
  • 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.
  • +
  • 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.
from functools import total_ordering
@@ -1057,7 +1057,7 @@
 

Collection

  • Only required methods are iter() and len().
  • This cheatsheet actually means '<iterable>' when it uses '<collection>'.
  • -
  • I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only iterable objects that are not collections.
  • +
  • I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are not collections while being iterable.
class MyCollection:
     def __init__(self, a):
         self.a = a
@@ -1298,11 +1298,11 @@
 
 

Argument Parser

from argparse import ArgumentParser, FileType
 p = ArgumentParser(description=<str>)
-p.add_argument('-<short_name>', '--<name>', action='store_true')  # Flag
-p.add_argument('-<short_name>', '--<name>', type=<type>)          # Option
-p.add_argument('<name>', type=<type>, nargs=1)                    # First argument
-p.add_argument('<name>', type=<type>, nargs='+')                  # Remaining arguments
-p.add_argument('<name>', type=<type>, nargs='*')                  # Optional arguments
+p.add_argument('-<short_name>', '--<name>', action='store_true')  # Flag.
+p.add_argument('-<short_name>', '--<name>', type=<type>)          # Option.
+p.add_argument('<name>', type=<type>, nargs=1)                    # First argument.
+p.add_argument('<name>', type=<type>, nargs='+')                  # Remaining arguments.
+p.add_argument('<name>', type=<type>, nargs='*')                  # Optional arguments.
 args  = p.parse_args()                                            # Exits on error.
 value = args.<name>
 
@@ -1319,7 +1319,7 @@
  • 'encoding=None' means that the default encoding is used, which is platform dependent. Best practice is to use 'encoding="utf-8"' whenever possible.
  • 'newline=None' means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.
  • -
  • 'newline=""' means no conversions take place, but input is still broken into chunks by readline() and readlines() on either '\n', '\r' or '\r\n'.
  • +
  • 'newline=""' means no conversions take place, but input is still broken into chunks by readline() and readlines() on '\n', '\r' and '\r\n'.

Modes

  • 'r' - Read (default).
  • @@ -1330,7 +1330,7 @@
  • 'r+' - Read and write from the start.
  • 'a+' - Read and write from the end.
  • 't' - Text mode (default).
  • -
  • 'b' - Binary mode.
  • +
  • 'b' - Binary mode (must be paired with one of above).

Exceptions

  • 'FileNotFoundError' can be raised when reading with 'r' or 'r+'.
  • 'FileExistsError' can be raised when writing with 'x'.
  • @@ -1910,7 +1910,7 @@ await asyncio.sleep(0.005) async def model(moves, state): - while state['*'] not in {p for id_, p in state.items() if id_ != '*'}: + while state['*'] not in (state[id_] for id_ in range(10)): id_, d = await moves.get() x, y = state[id_] deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)} @@ -2680,20 +2680,20 @@
    • Use '<DF>[col_key_1, col_key_2][row_key]' to get the fifth result's values.
    -

    DataFrame — Encode, Decode, Plot:

    <DF> = pd.read_json/html('<str/path/url>')
    +

    DataFrame — Plot, Encode, Decode:

    import matplotlib.pyplot as plt
    +<DF>.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
    +
    + +
    <DF> = pd.read_json/html('<str/path/url>')
     <DF> = pd.read_csv/pickle/excel('<path/url>')
     <DF> = pd.read_sql('<table_name/query>', <connection>)
     <DF> = pd.read_clipboard()
    -
    - +
<dict> = <DF>.to_dict(['d/l/s/sp/r/i'])
 <str>  = <DF>.to_json/html/csv/markdown/latex([<path>])
 <DF>.to_pickle/excel(<path>)
 <DF>.to_sql('<table_name>', <connection>)
 
-
import matplotlib.pyplot as plt
-<DF>.plot.line/bar/hist/scatter([x=column_key, y=column_key/s]); plt.show()
-

GroupBy

Object that groups together rows of a dataframe based on the value of the passed column.

>>> df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], index=list('abc'), columns=list('xyz'))
 >>> df.groupby('z').get_group(6)
    x  y
@@ -2884,7 +2884,7 @@