From 3b033d439cea186c1657181538a372b72039b8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 19 Feb 2024 14:30:50 +0100 Subject: [PATCH] Datetime, Bytes, Struct, Numpy, Audio, Pygame, Pandas --- README.md | 68 +++++++++++++++++++++++++-------------------------- index.html | 72 +++++++++++++++++++++++++++--------------------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index bd21cc9b4..4c571e5ab 100644 --- a/README.md +++ b/README.md @@ -655,9 +655,9 @@ from dateutil.tz import tzlocal, gettz ### Arithmetics ```python = > # Ignores time jumps (fold attribute). Also ==. - = > # Ignores time jumps if they share tzinfo object. + = > # Ignores jumps if they share tz object. Broken ==. = - # Ignores jumps. Convert to UTC for actual delta. - = - # Ignores time jumps if they share tzinfo object. + = - # Ignores jumps if they share tzinfo object. = ± # Returned datetime can fall into missing hour. = * # Also: = abs() and = ±% . = / # How many hours/weeks/years are in TD. Also //. @@ -1951,26 +1951,26 @@ Bytes **Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.** ```python - = b'' # Only accepts ASCII characters and \x00-\xff. - = [] # Returns an int in range from 0 to 255. - = [] # Returns bytes even if it has only one element. - = .join() # Joins elements using bytes as a separator. + = b'' # Only accepts ASCII characters and \x00-\xff. + = [] # Returns an int in range from 0 to 255. + = [] # Returns bytes even if it has only one element. + = .join() # Joins elements using bytes as a separator. ``` ### Encode ```python - = bytes() # Ints must be in range from 0 to 255. - = bytes(, 'utf-8') # Or: .encode('utf-8') - = .to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`. - = bytes.fromhex('') # Hex pairs can be separated by whitespaces. + = bytes() # Ints must be in range from 0 to 255. + = bytes(, 'utf-8') # Or: .encode('utf-8') + = .to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`. + = bytes.fromhex('') # Hex pairs can be separated by whitespaces. ``` ### Decode ```python - = list() # Returns ints in range from 0 to 255. - = str(, 'utf-8') # Or: .decode('utf-8') - = int.from_bytes(, …) # `byteorder='big/little', signed=False`. -'' = .hex() # Returns hex pairs. Accepts `sep=`. + = list() # Returns ints in range from 0 to 255. + = str(, 'utf-8') # Or: .decode('utf-8') + = int.from_bytes(, …) # `byteorder='big/little', signed=False`. +'' = .hex() # Returns hex pairs. Accepts `sep=`. ``` ### Read Bytes from File @@ -2009,12 +2009,12 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03' ### Format #### For standard type sizes and manual alignment (padding) start format string with: * **`'='` - System's byte order (usually little-endian).** -* **`'<'` - Little-endian.** +* **`'<'` - Little-endian (i.e. least significant byte first).** * **`'>'` - Big-endian (also `'!'`).** #### Besides numbers, pack() and unpack() also support bytes objects as part of the sequence: * **`'c'` - A bytes object with a single element. For pad byte use `'x'`.** -* **`'s'` - A bytes object with n elements.** +* **`'s'` - A bytes object with n elements (not effected by byte order).** #### Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets: * **`'b'` - char (1/1)** @@ -2414,7 +2414,7 @@ plt.clf() # Clears the figure. Table ----- -#### Prints a CSV file as an ASCII table: +#### Prints a CSV spreadsheet to the console: ```python # $ pip3 install tabulate import csv, tabulate @@ -2676,7 +2676,7 @@ import numpy as np = np.tile/repeat(, [, axis]) # Tiles array or repeats its elements. ``` * **Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).** -* **Axis is an index of the dimension that gets aggregated. Leftmost dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).** +* **Axis is an index of a dimension. Leftmost dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).** ### Indexing ```perl @@ -2858,21 +2858,21 @@ import wave ``` ```python - = wave.open('', 'rb') # Opens the WAV file. - = .getframerate() # Returns number of frames per second. - = .getnchannels() # Returns number of samples per frame. - = .getsampwidth() # Returns number of bytes per sample. - = .getparams() # Returns collection of listed params. - = .readframes(nframes) # Returns next n frames. All if -1. + = wave.open('', 'rb') # Opens the WAV file. + = .getframerate() # Returns number of frames per second. + = .getnchannels() # Returns number of samples per frame. + = .getsampwidth() # Returns number of bytes per sample. + = .getparams() # Returns namedtuple of all parameters. + = .readframes(nframes) # Returns next n frames. All if -1. ``` ```python - = wave.open('', 'wb') # Opens WAV file for writing. -.setframerate() # Pass 44100 for CD, 48000 for video. -.setnchannels() # Pass 1 for mono, 2 for stereo. -.setsampwidth() # Pass 2 for CD, 3 for hi-res sound. -.setparams() # Sets all parameters. -.writeframes() # Appends frames to the file. + = wave.open('', 'wb') # Creates/truncates a file for writing. +.setframerate() # Pass 44100 for CD, 48000 for video. +.setnchannels() # Pass 1 for mono, 2 for stereo. +.setsampwidth() # Pass 2 for CD, 3 for hi-res sound. +.setparams() # Sets all parameters. +.writeframes() # Appends frames to the file. ``` * **Bytes object contains a sequence of frames, each consisting of one or more samples.** * **In a stereo signal, the first sample of a frame belongs to the left channel.** @@ -3007,7 +3007,7 @@ while not pg.event.get(pg.QUIT): ```python = .collidepoint((x, y)) # Checks if rectangle contains the point. - = .colliderect() # Checks if two rectangles overlap. + = .colliderect() # Checks if the two rectangles overlap. = .collidelist() # Returns index of first colliding Rect or -1. = .collidelistall() # Returns indexes of all colliding rectangles. ``` @@ -3025,7 +3025,7 @@ while not pg.event.get(pg.QUIT): ```python .fill(color) # Tuple, Color('#rrggbb[aa]') or Color(). .set_at((x, y), color) # Updates pixel. Also .get_at((x, y)). -.blit(, (x, y)) # Draws passed surface to the surface. +.blit(, (x, y)) # Draws passed surface at specified location. ``` ```python @@ -3051,7 +3051,7 @@ rect(, color, , width=0) # Also polygon(, color, po ### Sound ```python = pg.mixer.Sound() # WAV file or bytes/array of signed shorts. -.play/stop() # Also .set_volume(). +.play/stop() # Also set_volume(), fadeout(msec). ``` ### Basic Mario Brothers Example @@ -3373,7 +3373,7 @@ c 7 8 6 = .groupby(column_key/s) # Splits DF into groups based on passed column. = .apply() # Maps each group. Func can return DF, Sr or el. = [column_key] # Single column GB. All operations return a Sr. - = .size() # A Sr of group sizes. Keys are group "names". + = .size() # A Sr of group sizes. Same keys as get_group(). ``` #### GroupBy — Aggregate, Transform, Map: diff --git a/index.html b/index.html index 269e48ff2..c0eaea6c1 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -576,9 +576,9 @@
  • '%Z' accepts 'UTC/GMT' and local timezone's code and returns timezone's name, 'UTC[±HH:MM]' if timezone is nameless, or an empty string if datetime is naive.
  • Arithmetics

    <bool>   = <D/T/DTn> > <D/T/DTn>            # Ignores time jumps (fold attribute). Also ==.
    -<bool>   = <DTa>     > <DTa>                # Ignores time jumps if they share tzinfo object.
    +<bool>   = <DTa>     > <DTa>                # Ignores jumps if they share tz object. Broken ==.
     <TD>     = <D/DTn>   - <D/DTn>              # Ignores jumps. Convert to UTC for actual delta.
    -<TD>     = <DTa>     - <DTa>                # Ignores time jumps if they share tzinfo object.
    +<TD>     = <DTa>     - <DTa>                # Ignores jumps if they share tzinfo object.
     <D/DT>   = <D/DT>    ± <TD>                 # Returned datetime can fall into missing hour.
     <TD>     = <TD>      * <float>              # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
     <float>  = <TD>      / <TD>                 # How many hours/weeks/years are in TD. Also //.
    @@ -1614,23 +1614,23 @@
     ┃ oracle     │ oracledb     │ oracledb │ www.pypi.org/project/oracledb    ┃
     ┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
     
    -

    #Bytes

    Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.

    <bytes> = b'<str>'                          # Only accepts ASCII characters and \x00-\xff.
    -<int>   = <bytes>[<index>]                  # Returns an int in range from 0 to 255.
    -<bytes> = <bytes>[<slice>]                  # Returns bytes even if it has only one element.
    -<bytes> = <bytes>.join(<coll_of_bytes>)     # Joins elements using bytes as a separator.
    +

    #Bytes

    Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.

    <bytes> = b'<str>'                       # Only accepts ASCII characters and \x00-\xff.
    +<int>   = <bytes>[<index>]               # Returns an int in range from 0 to 255.
    +<bytes> = <bytes>[<slice>]               # Returns bytes even if it has only one element.
    +<bytes> = <bytes>.join(<coll_of_bytes>)  # Joins elements using bytes as a separator.
     
    -

    Encode

    <bytes> = bytes(<coll_of_ints>)             # Ints must be in range from 0 to 255.
    -<bytes> = bytes(<str>, 'utf-8')             # Or: <str>.encode('utf-8')
    -<bytes> = <int>.to_bytes(n_bytes, …)        # `byteorder='big/little', signed=False`.
    -<bytes> = bytes.fromhex('<hex>')            # Hex pairs can be separated by whitespaces.
    +

    Encode

    <bytes> = bytes(<coll_of_ints>)          # Ints must be in range from 0 to 255.
    +<bytes> = bytes(<str>, 'utf-8')          # Or: <str>.encode('utf-8')
    +<bytes> = <int>.to_bytes(n_bytes, …)     # `byteorder='big/little', signed=False`.
    +<bytes> = bytes.fromhex('<hex>')         # Hex pairs can be separated by whitespaces.
     
    -

    Decode

    <list>  = list(<bytes>)                     # Returns ints in range from 0 to 255.
    -<str>   = str(<bytes>, 'utf-8')             # Or: <bytes>.decode('utf-8')
    -<int>   = int.from_bytes(<bytes>, …)        # `byteorder='big/little', signed=False`.
    -'<hex>' = <bytes>.hex()                     # Returns hex pairs. Accepts `sep=<str>`.
    +

    Decode

    <list>  = list(<bytes>)                  # Returns ints in range from 0 to 255.
    +<str>   = str(<bytes>, 'utf-8')          # Or: <bytes>.decode('utf-8')
    +<int>   = int.from_bytes(<bytes>, …)     # `byteorder='big/little', signed=False`.
    +'<hex>' = <bytes>.hex()                  # Returns hex pairs. Accepts `sep=<str>`.
     

    Read Bytes from File

    def read_bytes(filename):
    @@ -1659,11 +1659,11 @@
     

    Format

    For standard type sizes and manual alignment (padding) start format string with:

    • '=' - System's byte order (usually little-endian).
    • -
    • '<' - Little-endian.
    • +
    • '<' - Little-endian (i.e. least significant byte first).
    • '>' - Big-endian (also '!').

    Besides numbers, pack() and unpack() also support bytes objects as part of the sequence:

    • 'c' - A bytes object with a single element. For pad byte use 'x'.
    • -
    • '<n>s' - A bytes object with n elements.
    • +
    • '<n>s' - A bytes object with n elements (not effected by byte order).

    Integer types. Use a capital letter for unsigned type. Minimum and standard sizes are in brackets:

    • 'b' - char (1/1)
    • 'h' - short (2/2)
    • @@ -1987,7 +1987,7 @@

      Format

      # Clears the figure.

    -

    #Table

    Prints a CSV file as an ASCII table:

    # $ pip3 install tabulate
    +

    #Table

    Prints a CSV spreadsheet to the console:

    # $ pip3 install tabulate
     import csv, tabulate
     with open('test.csv', encoding='utf-8', newline='') as file:
         rows = list(csv.reader(file))
    @@ -2197,7 +2197,7 @@ 

    Format

    Indexing

    <el>       = <2d_array>[row_index, column_index]        # <3d_a>[table_i, row_i, column_i]
     <1d_view>  = <2d_array>[row_index]                      # <3d_a>[table_i, row_i]
    @@ -2341,19 +2341,19 @@ 

    Format

    #Audio

    import wave
     
    -
    <Wave>   = wave.open('<path>', 'rb')   # Opens the WAV file.
    -<int>    = <Wave>.getframerate()       # Returns number of frames per second.
    -<int>    = <Wave>.getnchannels()       # Returns number of samples per frame.
    -<int>    = <Wave>.getsampwidth()       # Returns number of bytes per sample.
    -<params> = <Wave>.getparams()          # Returns collection of listed params.
    -<bytes>  = <Wave>.readframes(nframes)  # Returns next n frames. All if -1.
    +
    <Wave>  = wave.open('<path>', 'rb')   # Opens the WAV file.
    +<int>   = <Wave>.getframerate()       # Returns number of frames per second.
    +<int>   = <Wave>.getnchannels()       # Returns number of samples per frame.
    +<int>   = <Wave>.getsampwidth()       # Returns number of bytes per sample.
    +<tuple> = <Wave>.getparams()          # Returns namedtuple of all parameters.
    +<bytes> = <Wave>.readframes(nframes)  # Returns next n frames. All if -1.
     
    -
    <Wave> = wave.open('<path>', 'wb')     # Opens WAV file for writing.
    -<Wave>.setframerate(<int>)             # Pass 44100 for CD, 48000 for video.
    -<Wave>.setnchannels(<int>)             # Pass 1 for mono, 2 for stereo.
    -<Wave>.setsampwidth(<int>)             # Pass 2 for CD, 3 for hi-res sound.
    -<Wave>.setparams(<params>)             # Sets all parameters.
    -<Wave>.writeframes(<bytes>)            # Appends frames to the file.
    +
    <Wave> = wave.open('<path>', 'wb')    # Creates/truncates a file for writing.
    +<Wave>.setframerate(<int>)            # Pass 44100 for CD, 48000 for video.
    +<Wave>.setnchannels(<int>)            # Pass 1 for mono, 2 for stereo.
    +<Wave>.setsampwidth(<int>)            # Pass 2 for CD, 3 for hi-res sound.
    +<Wave>.setparams(<tuple>)             # Sets all parameters.
    +<Wave>.writeframes(<bytes>)           # Appends frames to the file.
     
    • Bytes object contains a sequence of frames, each consisting of one or more samples.
    • @@ -2464,7 +2464,7 @@

      Format

      <bool> = <Rect>.collidepoint((x, y)) # Checks if rectangle contains the point. -<bool> = <Rect>.colliderect(<Rect>) # Checks if two rectangles overlap. +<bool> = <Rect>.colliderect(<Rect>) # Checks if the two rectangles overlap. <int> = <Rect>.collidelist(<list_of_Rect>) # Returns index of first colliding Rect or -1. <list> = <Rect>.collidelistall(<list_of_Rect>) # Returns indexes of all colliding rectangles.

    @@ -2478,7 +2478,7 @@

    Format

    <Surf>.fill(color) # Tuple, Color('#rrggbb[aa]') or Color(<name>). <Surf>.set_at((x, y), color) # Updates pixel. Also <Surf>.get_at((x, y)). -<Surf>.blit(<Surf>, (x, y)) # Draws passed surface to the surface. +<Surf>.blit(<Surf>, (x, y)) # Draws passed surface at specified location.

    from pygame.transform import scale, ...
     <Surf> = scale(<Surf>, (width, height))         # Returns scaled surface.
    @@ -2495,7 +2495,7 @@ 

    Format

    Sound

    <Sound> = pg.mixer.Sound(<path/file/bytes>)     # WAV file or bytes/array of signed shorts.
    -<Sound>.play/stop()                             # Also <Sound>.set_volume(<float>).
    +<Sound>.play/stop()                             # Also set_volume(<float>), fadeout(msec).
     

    Basic Mario Brothers Example

    import collections, dataclasses, enum, io, itertools as it, pygame as pg, urllib.request
    @@ -2761,7 +2761,7 @@ 

    Format

    <GB> = <DF>.groupby(column_key/s) # Splits DF into groups based on passed column. <DF> = <GB>.apply(<func>) # Maps each group. Func can return DF, Sr or el. <GB> = <GB>[column_key] # Single column GB. All operations return a Sr. -<Sr> = <GB>.size() # A Sr of group sizes. Keys are group "names". +<Sr> = <GB>.size() # A Sr of group sizes. Same keys as get_group().

    GroupBy — Aggregate, Transform, Map:

    <DF> = <GB>.sum/max/mean/idxmax/all()          # Or: <GB>.agg(lambda <Sr>: <el>)
     <DF> = <GB>.rank/diff/cumsum/ffill()           # Or: <GB>.transform(lambda <Sr>: <Sr>)
    @@ -2933,7 +2933,7 @@ 

    Format