diff --git a/README.md b/README.md index 869fd1c..bbdb6a0 100644 --- a/README.md +++ b/README.md @@ -1906,7 +1906,7 @@ with : # Exits the block with commit() 1 >>> conn.execute('SELECT * FROM person').fetchall() [(1, 'Jean-Luc', 187)] -``` +``` ### MySQL **Has a very similar interface, with differences listed below.** @@ -2401,14 +2401,14 @@ Curses ------ #### Runs a basic file explorer in the terminal: ```python -from curses import wrapper, ascii, A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER +from curses import wrapper, ascii, A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER from os import listdir, path, chdir def main(screen): ch, first, selected, paths = 0, 0, 0, listdir() while ch != ascii.ESC: height, _ = screen.getmaxyx() - screen.clear() + screen.erase() for y, filename in enumerate(paths[first : first+height]): screen.addstr(y, 0, filename, A_REVERSE * (selected == first + y)) ch = screen.getch() @@ -2872,7 +2872,7 @@ def write_to_wav_file(filename, float_samples, nchannels=1, sampwidth=2, framera a_float = max(-1, min(1 - 2e-16, a_float)) a_float += sampwidth == 1 a_float *= pow(2, sampwidth * 8 - 1) - return int(a_float).to_bytes(sampwidth, 'little', signed=sampwidth!=1) + return int(a_float).to_bytes(sampwidth, 'little', signed=sampwidth!=1) with wave.open(filename, 'wb') as file: file.setnchannels(nchannels) file.setsampwidth(sampwidth) @@ -2949,8 +2949,8 @@ screen = pg.display.set_mode((500, 500)) rect = pg.Rect(240, 240, 20, 20) while all(event.type != pg.QUIT for event in pg.event.get()): deltas = {pg.K_UP: (0, -1), pg.K_RIGHT: (1, 0), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0)} - for key_code, is_pressed in enumerate(pg.key.get_pressed()): - rect = rect.move(deltas[key_code]) if key_code in deltas and is_pressed else rect + for ch, is_pressed in enumerate(pg.key.get_pressed()): + rect = rect.move(deltas[ch]) if ch in deltas and is_pressed else rect screen.fill((0, 0, 0)) pg.draw.rect(screen, (255, 255, 255), rect) pg.display.flip() @@ -3046,7 +3046,7 @@ def run(screen, images, mario, tiles): clock = pg.time.Clock() while all(event.type != pg.QUIT for event in pg.event.get()): keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w} - pressed = {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on} + pressed = {keys.get(ch) for ch, is_prsd in enumerate(pg.key.get_pressed()) if is_prsd} update_speed(mario, tiles, pressed) update_position(mario, tiles) draw(screen, images, mario, tiles, pressed) @@ -3216,9 +3216,9 @@ b 3 4 #### Merge, Join, Concat: ```python >>> l = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) - x y -a 1 2 -b 3 4 + x y +a 1 2 +b 3 4 >>> r = DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z']) y z b 4 5 @@ -3263,7 +3263,7 @@ c 6 7 = .rank/diff/cumsum/ffill/interpl() # Or: .apply/agg/transform() = .fillna() # Or: .applymap() ``` -* **All operations operate on columns by default. Use `'axis=1'` parameter to process the rows instead.** +* **All operations operate on columns by default. Use `'axis=1'` parameter to process the rows instead.** ```python >>> df = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) @@ -3331,7 +3331,7 @@ c 7 8 #### Aggregate, Transform, Map: ```python = .sum/max/mean/idxmax/all() # Or: .apply/agg() - = .rank/diff/cumsum/ffill() # Or: .aggregate() + = .rank/diff/cumsum/ffill() # Or: .aggregate() = .fillna() # Or: .transform() ``` @@ -3386,7 +3386,7 @@ from plotly.express import line
```python -covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', +covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', usecols=['iso_code', 'date', 'total_deaths', 'population']) continents = pd.read_csv('https://gist.githubusercontent.com/stevewithington/20a69c0b6d2ff' '846ea5d35e5fc47f26c/raw/country-and-continent-codes-list-csv.csv', diff --git a/index.html b/index.html index 7beb5b0..12607bd 100644 --- a/index.html +++ b/index.html @@ -1960,14 +1960,14 @@ print(table) -

#Curses

Runs a basic file explorer in the terminal:

from curses import wrapper, ascii, A_REVERSE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER
+

#Curses

Runs a basic file explorer in the terminal:

from curses import wrapper, ascii, A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
 from os import listdir, path, chdir
 
 def main(screen):
     ch, first, selected, paths = 0, 0, 0, listdir()
     while ch != ascii.ESC:
         height, _ = screen.getmaxyx()
-        screen.clear()
+        screen.erase()
         for y, filename in enumerate(paths[first : first+height]):
             screen.addstr(y, 0, filename, A_REVERSE * (selected == first + y))
         ch = screen.getch()
@@ -2034,7 +2034,7 @@ WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(prog
     version    = table.find('th', text='Stable release').next_sibling.strings.__next__()
     logo_url   = table.find('img')['src']
     logo       = requests.get(f'https:{logo_url}').content
-    with open('test.png', 'wb') as file:
+    with open(os.path.basename(logo_url), 'wb') as file:
         file.write(logo)
     print(python_url, version)
 except requests.exceptions.ConnectionError:
@@ -2101,8 +2101,8 @@ duration = perf_counter() - start_time
 

Profiling by Line

# $ pip3 install line_profiler memory_profiler
 @profile
 def main():
-    a = [*range(10000)]
-    b = {*range(10000)}
+    l = list(range(10000))
+    s = set(range(10000))
 main()
 
@@ -2111,16 +2111,16 @@ Line # Hits Time Per Hit % Time Line Contents ======================================================= 1 @profile 2 def main(): - 3 1 955.0 955.0 43.7 a = [*range(10000)] - 4 1 1231.0 1231.0 56.3 b = {*range(10000)} + 3 1 955.0 955.0 43.7 l = list(range(10000)) + 4 1 1231.0 1231.0 56.3 s = set(range(10000))
$ python3 -m memory_profiler test.py
 Line #         Mem usage      Increment   Line Contents
 =======================================================
      1        37.668 MiB     37.668 MiB   @profile
      2                                    def main():
-     3        38.012 MiB      0.344 MiB       a = [*range(10000)]
-     4        38.477 MiB      0.465 MiB       b = {*range(10000)}
+     3        38.012 MiB      0.344 MiB       l = list(range(10000))
+     4        38.477 MiB      0.465 MiB       s = set(range(10000))
 

Call Graph

Generates a PNG image of the call graph with highlighted bottlenecks:

# $ pip3 install pycallgraph2
 from pycallgraph2 import output, PyCallGraph
@@ -2274,7 +2274,7 @@ frames = []
     y = sum(range(velocity))
     frame = Image.new('L', (WIDTH, WIDTH))
     draw  = ImageDraw.Draw(frame)
-    draw.ellipse((WIDTH/2-R, y, WIDTH/2+R, y+R*2), fill='white')
+    draw.ellipse(((WIDTH / 2) - R, y, (WIDTH / 2) + R, y + (R * 2)), fill='white')
     frames.append(frame)
 frames += reversed(frames[1:-1])
 imageio.mimsave('test.gif', frames, duration=0.03)
@@ -2332,7 +2332,7 @@ nframes      = <Wave_read>.getnframes()         -1, min(1 - 2e-16, a_float))
         a_float += sampwidth == 1
         a_float *= pow(2, sampwidth * 8 - 1)
-        return int(a_float).to_bytes(sampwidth, 'little', signed=sampwidth!=1) 
+        return int(a_float).to_bytes(sampwidth, 'little', signed=sampwidth!=1)
     with wave.open(filename, 'wb') as file:
         file.setnchannels(nchannels)
         file.setsampwidth(sampwidth)
@@ -2392,8 +2392,8 @@ screen = pg.display.set_mode((500, 240, 240, 20, 20)
 while all(event.type != pg.QUIT for event in pg.event.get()):
     deltas = {pg.K_UP: (0, -1), pg.K_RIGHT: (1, 0), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0)}
-    for key_code, is_pressed in enumerate(pg.key.get_pressed()):
-        rect = rect.move(deltas[key_code]) if key_code in deltas and is_pressed else rect
+    for ch, is_pressed in enumerate(pg.key.get_pressed()):
+        rect = rect.move(deltas[ch]) if ch in deltas and is_pressed else rect
     screen.fill((0, 0, 0))
     pg.draw.rect(screen, (255, 255, 255), rect)
     pg.display.flip()
@@ -2472,7 +2472,7 @@ SIZE, MAX_SPEED = 50, P(while all(event.type != pg.QUIT for event in pg.event.get()):
         keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w}
-        pressed = {keys.get(i) for i, on in enumerate(pg.key.get_pressed()) if on}
+        pressed = {keys.get(ch) for ch, is_prsd in enumerate(pg.key.get_pressed()) if is_prsd}
         update_speed(mario, tiles, pressed)
         update_position(mario, tiles)
         draw(screen, images, mario, tiles, pressed)
@@ -2606,9 +2606,9 @@ b  3  4
 <DF>    = <DF>.sort_values(column_key/s)      # Sorts rows by the passed column/s.
 

Merge, Join, Concat:

>>> l = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
-   x  y 
-a  1  2 
-b  3  4 
+   x  y
+a  1  2
+b  3  4
 >>> r = DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z'])
    y  z
 b  4  5
@@ -2651,7 +2651,7 @@ c  6  7
 
    -
  • All operations operate on columns by default. Use 'axis=1' parameter to process the rows instead.
  • +
  • All operations operate on columns by default. Use 'axis=1' parameter to process the rows instead.
>>> df = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
    x  y
@@ -2704,7 +2704,7 @@ c  7  8
 <DF> = <GB>.get_group(group_key/s)            # Selects a group by value of grouping column.
 

Aggregate, Transform, Map:

<DF> = <GB>.sum/max/mean/idxmax/all()         # Or: <GB>.apply/agg(<agg_func>)
-<DF> = <GB>.rank/diff/cumsum/ffill()          # Or: <GB>.aggregate(<trans_func>)  
+<DF> = <GB>.rank/diff/cumsum/ffill()          # Or: <GB>.aggregate(<trans_func>)
 <DF> = <GB>.fillna(<el>)                      # Or: <GB>.transform(<map_func>)
 
@@ -2742,7 +2742,7 @@ c 7 8 <Figure>.write_html/json/image('<path>') # Also: <Figure>.show()
-

Covid deaths by continent:

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv', 
+

Covid deaths by continent:

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
                     usecols=['iso_code', 'date', 'total_deaths', 'population'])
 continents = pd.read_csv('https://gist.githubusercontent.com/stevewithington/20a69c0b6d2ff'
                          '846ea5d35e5fc47f26c/raw/country-and-continent-codes-list-csv.csv',