My Python scripts boilerplate

this is the boilerplate I use as a snippet in VS Code when I create a new Python script

21 Jul 2022 starting note.

When I create a new Python file, I just type my shortcut bp (=boilerplate) and select my snippet bp_python to insert all the below at once - massive time saver.

In VS Code: Code > Preferences > Configure User Snippets > python.json

29 Sep 2022 updated.

    "Nic: Python boilerplate": {
        "prefix": "bp_python",
        "body": [
            "from datetime import datetime",
            "import os",
            "print(\"----------\")",
            "ts_file = f\"{datetime.now().strftime('%y%m%d-%H%M')}\"",
            "ts_db = f\"{datetime.now().strftime('%Y-%m-%d %H:%M')}\"",
            "ts_time = f\"{datetime.now().strftime('%H:%M:%S')}\"",
            "print(f\"{ts_time} starting {os.path.basename(__file__)}\")",
            "import time",
            "start_time = time.time()",
            "",
            "from dotenv import load_dotenv",
            "load_dotenv()",
            "USER = os.getenv(\"USER\")",
            "",
            "import sys",
            "sys.path.append(f\"/Users/{USER}/Python/indeXee\")",
            "",
            "# import my_utils",
            "# import grist_BB",
            "# import grist_PE",
            "# import dbee",
            "",
            "from inspect import currentframe",
            "def get_linenumber():",
            "    \"\"\"",
            "    print line numbers with f\"{get_linenumber()}\"",
            "    \"\"\"",
            "    cf = currentframe()",
            "    return cf.f_back.f_lineno",
            "",
            "import pprint",
            "pp = pprint.PrettyPrinter(indent=4)",
            "",
            "count = 0",
            "count_row = 0",
            "",
            "test = True",
            "v = True # verbose mode",
            "",
            "print(f\"{os.path.basename(__file__)} boilerplate loaded -----------\\n\")",
            "####################",
            "# $0SCRIPT_TITLE",
            "",
            "### Script-specific imports",
            "",
            "",
            "",
            "### Global Variables",
            "",
            "",
            "",
            "### Functions",
            "",
            "",
            "",
            "### Main",
            "",
            "",
            "",
            "",
            "########################################################################################################",
            "",
            "if __name__ == '__main__':",
            "    print()",
            "    print()",
            "    print('-------------------------------')",
            "    print(f\"{os.path.basename(__file__)}\")",
            "    print()",
            "    print(f\"{count=}\")",
            "    print()",
            "    print('-------------------------------')",
            "    run_time = round((time.time() - start_time), 1)",
            "    if run_time > 60:",
            "        print(f'{os.path.basename(__file__)} finished in {run_time/60} minutes.')",
            "    else:",
            "        print(f'{os.path.basename(__file__)} finished in {run_time}s.')",
            "    print()",
        ],
        "description": "Nic: Python boilerplate"
    },

outputs:

from datetime import datetime
import os
print("----------")
ts_file = f"{datetime.now().strftime('%y%m%d-%H%M')}"
ts_db = f"{datetime.now().strftime('%Y-%m-%d %H:%M')}"
ts_time = f"{datetime.now().strftime('%H:%M:%S')}"
print(f"{ts_time} starting {os.path.basename(__file__)}")
import time
start_time = time.time()

from dotenv import load_dotenv
load_dotenv()
USER = os.getenv("USER")

import sys
sys.path.append(f"/Users/{USER}/Python/indeXee")

# import my_utils
# import grist_BB
# import grist_PE
# import dbee

from inspect import currentframe
def get_linenumber():
    """
    print line numbers with f"{get_linenumber()}"
    """
    cf = currentframe()
    return cf.f_back.f_lineno

import pprint
pp = pprint.PrettyPrinter(indent=4)

count = 0
count_row = 0

test = True
v = True # verbose mode

print(f"{os.path.basename(__file__)} boilerplate loaded -----------\n")
####################
# SCRIPT_TITLE

### Script-specific imports



### Global Variables



### Functions



### Main




########################################################################################################

if __name__ == '__main__':
    print()
    print()
    print('-------------------------------')
    print(f"{os.path.basename(__file__)}")
    print()
    print(f"{count=}")
    print()
    print('-------------------------------')
    run_time = round((time.time() - start_time), 1)
    if run_time > 60:
        print(f'{os.path.basename(__file__)} finished in {run_time/60} minutes.')
    else:
        print(f'{os.path.basename(__file__)} finished in {run_time}s.')
    print()

links

social