From 4f55bc6cdbdcc79115a4858a828e8a599093c5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 27 Jul 2023 19:44:26 +0200 Subject: [PATCH] CSV, Threading, Curses --- README.md | 16 ++++++++-------- index.html | 20 ++++++++++---------- parse.js | 8 ++++---- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 08f195c..fe89ad9 100644 --- a/README.md +++ b/README.md @@ -1861,16 +1861,16 @@ import csv ### Read Rows from CSV File ```python -def read_csv_file(filename, dialect='excel'): +def read_csv_file(filename, dialect='excel', **params): with open(filename, encoding='utf-8', newline='') as file: - return list(csv.reader(file, dialect)) + return list(csv.reader(file, dialect, **params)) ``` ### Write Rows to CSV File ```python -def write_to_csv_file(filename, rows, dialect='excel'): +def write_to_csv_file(filename, rows, dialect='excel', **params): with open(filename, 'w', encoding='utf-8', newline='') as file: - writer = csv.writer(file, dialect) + writer = csv.writer(file, dialect, **params) writer.writerows(rows) ``` @@ -2155,7 +2155,7 @@ with : # Enters the block by calling acq ``` * **Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.** * **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.** -* **An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments/results must be [pickable](#pickle).** +* **ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be [pickable](#pickle). Queues must be sent using executor's 'initargs' and 'initializer' parameters.** Operator @@ -2423,17 +2423,17 @@ Curses ------ #### Runs a basic file explorer in the terminal: ```python -import curses, curses.ascii, os +import curses, os from curses import A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER def main(screen): ch, first, selected, paths = 0, 0, 0, os.listdir() - while ch != curses.ascii.ESC: + while ch != ord('q'): height, width = screen.getmaxyx() screen.erase() for y, filename in enumerate(paths[first : first+height]): color = A_REVERSE if filename == paths[selected] else 0 - screen.addstr(y, 0, filename[:width-1], color) + screen.addnstr(y, 0, filename, width-1, color) ch = screen.getch() selected += (ch == KEY_DOWN) - (ch == KEY_UP) selected = max(0, min(len(paths)-1, selected)) diff --git a/index.html b/index.html index 8ccaab3..d1b993d 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -1553,14 +1553,14 @@ CompletedProcess(args=['bc', Read Rows from CSV File
def read_csv_file(filename, dialect='excel'):
+

Read Rows from CSV File

def read_csv_file(filename, dialect='excel', **params):
     with open(filename, encoding='utf-8', newline='') as file:
-        return list(csv.reader(file, dialect))
+        return list(csv.reader(file, dialect, **params))
 
-

Write Rows to CSV File

def write_to_csv_file(filename, rows, dialect='excel'):
+

Write Rows to CSV File

def write_to_csv_file(filename, rows, dialect='excel', **params):
     with open(filename, 'w', encoding='utf-8', newline='') as file:
-        writer = csv.writer(file, dialect)
+        writer = csv.writer(file, dialect, **params)
         writer.writerows(rows)
 
@@ -1783,7 +1783,7 @@ CompletedProcess(args=['bc', pickable. +
  • ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be pickable. Queues must be sent using executor's 'initargs' and 'initializer' parameters.
  • #Operator

    Module of functions that provide the functionality of operators.

    import operator as op
     <obj>     = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>)     # +, -, *, /, //, %
    @@ -1986,17 +1986,17 @@ print(table)
     
    -

    #Curses

    Runs a basic file explorer in the terminal:

    import curses, curses.ascii, os
    +

    #Curses

    Runs a basic file explorer in the terminal:

    import curses, os
     from curses import A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER
     
     def main(screen):
         ch, first, selected, paths = 0, 0, 0, os.listdir()
    -    while ch != curses.ascii.ESC:
    +    while ch != ord('q'):
             height, width = screen.getmaxyx()
             screen.erase()
             for y, filename in enumerate(paths[first : first+height]):
                 color = A_REVERSE if filename == paths[selected] else 0
    -            screen.addstr(y, 0, filename[:width-1], color)
    +            screen.addnstr(y, 0, filename, width-1, color)
             ch = screen.getch()
             selected += (ch == KEY_DOWN) - (ch == KEY_UP)
             selected = max(0, min(len(paths)-1, selected))
    @@ -2933,7 +2933,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
      
     
       
     
    diff --git a/parse.js b/parse.js
    index 8f70b38..974e75f 100755
    --- a/parse.js
    +++ b/parse.js
    @@ -164,17 +164,17 @@ const COROUTINES =
       '    curses.wrapper(main)\n';
     
     const CURSES =
    -  'import curses, curses.ascii, os\n' +
    +  'import curses, os\n' +
       'from curses import A_REVERSE, KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_ENTER\n' +
       '\n' +
       'def main(screen):\n' +
       '    ch, first, selected, paths = 0, 0, 0, os.listdir()\n' +
    -  '    while ch != curses.ascii.ESC:\n' +
    +  '    while ch != ord(\'q\'):\n' +
       '        height, width = screen.getmaxyx()\n' +
       '        screen.erase()\n' +
       '        for y, filename in enumerate(paths[first : first+height]):\n' +
       '            color = A_REVERSE if filename == paths[selected] else 0\n' +
    -  '            screen.addstr(y, 0, filename[:width-1], color)\n' +
    +  '            screen.addnstr(y, 0, filename, width-1, color)\n' +
       '        ch = screen.getch()\n' +
       '        selected += (ch == KEY_DOWN) - (ch == KEY_UP)\n' +
       '        selected = max(0, min(len(paths)-1, selected))\n' +
    @@ -811,7 +811,7 @@ function fixHighlights() {
       $(`code:contains(\'\', , )`).html(TYPE);
       $(`code:contains(ValueError: malformed node)`).html(EVAL);
       $(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES);
    -  $(`code:contains(import curses, curses.ascii, os)`).html(CURSES);
    +  $(`code:contains(import curses, os)`).html(CURSES);
       $(`code:contains(pip3 install tqdm)`).html(PROGRESS_BAR);
       $(`code:contains(>>> logging.basicConfig(level=)`).html(LOGGING_EXAMPLE);
       $(`code:contains(samples_f = (sin(i *)`).html(AUDIO);