From 4ef9c49bcc4b11faa0292bc527657433d2069872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 15 Apr 2024 02:51:37 +0200 Subject: [PATCH] Imports, Closure, Profiling --- README.md | 23 +++++++++++------------ index.html | 29 +++++++++++++---------------- parse.js | 22 +++++++++++----------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 7ec634c..0e1d033 100644 --- a/README.md +++ b/README.md @@ -824,13 +824,12 @@ import . # Imports a built-in or '/.py'. * **Package is a collection of modules, but it can also define its own objects.** * **On a filesystem this corresponds to a directory of Python files with an optional init script.** * **Running `'import '` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.** +* **Imports are relative to the location of file that was passed to python command, but can be made relative to their own location with `'from .[…][[.…]] import '`.** Closure ------- -**We have/get a closure in Python when:** -* **A nested function references a value of its enclosing function and then** -* **the enclosing function returns the nested function.** +**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.** ```python def get_multiplier(a): @@ -2622,7 +2621,7 @@ Line # Hits Time Per Hit % Time Line Contents ### Call and Flame Graphs ```bash $ apt/brew install graphviz && pip3 install gprof2dot snakeviz # Or download installer. -$ tail --lines=4 test.py > test.py # Removes first line. +$ tail --lines=+2 test.py > test.py # Removes first line. $ python3 -m cProfile -o test.prof test.py # Runs built-in profiler. $ gprof2dot --format=pstats test.prof | dot -T png -o test.png # Generates call graph. $ xdg-open/open test.png # Displays call graph. @@ -2631,14 +2630,14 @@ $ snakeviz test.prof # Displays flame ### Sampling and Memory Profilers ```text -+--------------+----------+------------+-------------------------------+------+ -| pip3 install | Type | Target | How to run | Live | -+--------------+----------+------------+-------------------------------+------+ -| pyinstrument | Sampling | CPU | pyinstrument test.py | No | -| py-spy | Sampling | CPU | py-spy top -- python3 test.py | Yes | -| scalene | Sampling | CPU+Memory | scalene test.py | No | -| memray | Tracing | Memory | memray run --live test.py | Yes | -+--------------+----------+------------+-------------------------------+------+ ++--------------+------------+-------------------------------+-------+------+ +| pip3 install | Target | How to run | Lines | Live | ++--------------+------------+-------------------------------+-------+------+ +| pyinstrument | CPU | pyinstrument test.py | No | No | +| py-spy | CPU | py-spy top -- python3 test.py | No | Yes | +| scalene | CPU+Memory | scalene test.py | Yes | No | +| memray | Memory | memray run --live test.py | Yes | Yes | ++--------------+------------+-------------------------------+-------+------+ ``` diff --git a/index.html b/index.html index d95cfa8..936e033 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -693,18 +693,15 @@ player = Player(point, direction) #
  • Package is a collection of modules, but it can also define its own objects.
  • On a filesystem this corresponds to a directory of Python files with an optional init script.
  • Running 'import <package>' does not automatically provide access to the package's modules unless they are explicitly imported in its init script.
  • +
  • Imports are relative to the location of file that was passed to python command, but can be made relative to their own location with 'from .[…][<file>[.…]] import <obj>'.
  • -

    #Closure

    We have/get a closure in Python when:

      -
    • A nested function references a value of its enclosing function and then
    • -
    • the enclosing function returns the nested function.
    • -
    def get_multiplier(a):
    +

    #Closure

    We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.

    def get_multiplier(a):
         def out(b):
             return a * b
         return out
     
    -
    >>> multiply_by_3 = get_multiplier(3)
     >>> multiply_by_3(10)
     30
    @@ -2158,21 +2155,21 @@ Line #      Hits         Time  Per Hit   % Time  Line Contents
     

    Call and Flame Graphs

    $ apt/brew install graphviz && pip3 install gprof2dot snakeviz  # Or download installer.
    -$ tail --lines=4 test.py > test.py                              # Removes first line.
    +$ tail --lines=+2 test.py > test.py                             # Removes first line.
     $ python3 -m cProfile -o test.prof test.py                      # Runs built-in profiler.
     $ gprof2dot --format=pstats test.prof | dot -T png -o test.png  # Generates call graph.
     $ xdg-open/open test.png                                        # Displays call graph.
     $ snakeviz test.prof                                            # Displays flame graph.
     
    -

    Sampling and Memory Profilers

    ┏━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┓
    -┃ pip3 install │   Type   │   Target   │          How to run           │ Live ┃
    -┠──────────────┼──────────┼────────────┼───────────────────────────────┼──────┨
    -┃ pyinstrument │ Sampling │    CPU     │ pyinstrument test.py          │  ×   ┃
    -┃ py-spy       │ Sampling │    CPU     │ py-spy top -- python3 test.py │  ✓   ┃
    -┃ scalene      │ Sampling │ CPU+Memory │ scalene test.py               │  ×   ┃
    -┃ memray       │ Tracing  │   Memory   │ memray run --live test.py     │  ✓   ┃
    -┗━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┛
    +

    Sampling and Memory Profilers

    ┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━┯━━━━━━┓
    +┃ pip3 install │   Target   │          How to run           │ Lines │ Live ┃
    +┠──────────────┼────────────┼───────────────────────────────┼───────┼──────┨
    +┃ pyinstrument │    CPU     │ pyinstrument test.py          │   ×   │  ×   ┃
    +┃ py-spy       │    CPU     │ py-spy top -- python3 test.py │   ×   │  ✓   ┃
    +┃ scalene      │ CPU+Memory │ scalene test.py               │   ✓   │  ×   ┃
    +┃ memray       │   Memory   │ memray run --live test.py     │   ✓   │  ✓   ┃
    +┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━┷━━━━━━┛
     

    #NumPy

    Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. An even faster alternative that runs on a GPU is called CuPy.

    # $ pip3 install numpy
    @@ -2935,7 +2932,7 @@ $ deactivate                  # Deactivates the activ
      
     
       
     
    diff --git a/parse.js b/parse.js
    index c55ff16..7d2d09e 100755
    --- a/parse.js
    +++ b/parse.js
    @@ -522,19 +522,19 @@ const DIAGRAM_11_B =
       '┗━━━━━━━━━━━━━┷━━━━━━━━━━━━━┛\n';
     
     const DIAGRAM_115_A =
    -  '+--------------+----------+------------+-------------------------------+------+\n' +
    -  '| pip3 install |   Type   |   Target   |          How to run           | Live |\n' +
    -  '+--------------+----------+------------+-------------------------------+------+\n';
    +  '+--------------+------------+-------------------------------+-------+------+\n' +
    +  '| pip3 install |   Target   |          How to run           | Lines | Live |\n' +
    +  '+--------------+------------+-------------------------------+-------+------+\n';
     
     const DIAGRAM_115_B =
    -  '┏━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┓\n' +
    -  '┃ pip3 install │   Type   │   Target   │          How to run           │ Live ┃\n' +
    -  '┠──────────────┼──────────┼────────────┼───────────────────────────────┼──────┨\n' +
    -  '┃ pyinstrument │ Sampling │    CPU     │ pyinstrument test.py          │  ×   ┃\n' +
    -  '┃ py-spy       │ Sampling │    CPU     │ py-spy top -- python3 test.py │  ✓   ┃\n' +
    -  '┃ scalene      │ Sampling │ CPU+Memory │ scalene test.py               │  ×   ┃\n' +
    -  '┃ memray       │ Tracing  │   Memory   │ memray run --live test.py     │  ✓   ┃\n' +
    -  '┗━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┛\n';
    +  '┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━┯━━━━━━┓\n' +
    +  '┃ pip3 install │   Target   │          How to run           │ Lines │ Live ┃\n' +
    +  '┠──────────────┼────────────┼───────────────────────────────┼───────┼──────┨\n' +
    +  '┃ pyinstrument │    CPU     │ pyinstrument test.py          │   ×   │  ×   ┃\n' +
    +  '┃ py-spy       │    CPU     │ py-spy top -- python3 test.py │   ×   │  ✓   ┃\n' +
    +  '┃ scalene      │ CPU+Memory │ scalene test.py               │   ✓   │  ×   ┃\n' +
    +  '┃ memray       │   Memory   │ memray run --live test.py     │   ✓   │  ✓   ┃\n' +
    +  '┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━┷━━━━━━┛\n';
     
     const DIAGRAM_12_A =
       '+-----------+-----------+------+-----------+\n' +