Skip to content

Instantly share code, notes, and snippets.

@nmpowell
nmpowell / matplotlib_colourmap_from_css_gradient.ipynb
Last active April 11, 2023 23:59
Generating a qualitative matplotlib colourmap from a CSS HSL gradient. This is for https://github.com/nmpowell/carbon-intensity-forecast-tracking
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nmpowell
nmpowell / replace_markdown_section.py
Last active April 8, 2023 21:00
Replace a Markdown section using Python
def replace_markdown_section(
filepath: str, section: str, text: str, pad_before="\n\n", pad_after="\n\n"
) -> None:
"""Replace a section in a markdown file with new text.
Args:
filepath (str): Path to markdown file
section (str): Section header to replace
text (str): Text to replace section with
"""
@nmpowell
nmpowell / Please BUILD Hugo
Last active January 16, 2022 22:45
Please BUILD rules for Hugo static site generation. See https://please.build/ for initial setup. Ensure hugo is on your PATH. Then use this file (name it BUILD) in the root of your Hugo project directory to build the site and copy files.
# Zipped
sh_cmd(
name = "build_hugo_zip_and_copy",
# srcs = [":hugo_public_zip"],
# Must escape newly defined env vars within the script to avoid Please expanding them at build time
cmd = [
"DEST=/home/username/hugo_site.com",
"mkdir -p \\\$DEST",
"cp -r $(out_location :hugo_public_zip) \\\$DEST",
],
@nmpowell
nmpowell / python-venv-script.bat
Last active March 28, 2024 01:27
Windows batch script to run a Python program or script within its virtual environment. This can be called from Windows Task Scheduler.
REM Windows batch script to run 1+ Python program/scripts, sequentially, within their virtual environment. This can be called from Windows Task Scheduler.
set original_dir=%CD%
set venv_root_dir="C:\Python-Venvs\env-name"
cd %venv_root_dir%
call %venv_root_dir%\Scripts\activate.bat
python your_python_script.py <arg1> <arg2> <arg3>
@nmpowell
nmpowell / Codes and abbreviations for U.S. states, territories, and other regions.csv
Last active December 21, 2018 11:59
Wikipedia's List of U.S. state abbreviations as of 2018-08-21, cleaned up a little, from https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations#Table. Lines 1-8 are description; line 9 is column headers.
Codes and abbreviations for U.S. states, territories, and other regions
ISO ISO 3166 codes (2-letter, 3-letter, and 3-digit codes from ISO 3166-1; 2+2-letter codes from ISO 3166-2)
ANSI 2-letter and 2-digit codes from the ANSI standard INCITS 38:2009
USPS 2-letter codes used by the United States Postal Service
USCG 2-letter codes used by the United States Coast Guard (red text shows differences between ANSI and USCG)
Abbreviations:
GPO Older variable-length official US Government Printing Office abbreviations
AP Abbreviations from the AP Stylebook (red text shows differences between GPO and AP)
Name status of region ISO ANSI-alphabetic ANSI-numeric USPS USCG GPO AP Other abbreviations
United States of America Federal state US USA 840 US 00 U.S. U.S. U.S.A.
@nmpowell
nmpowell / US Zip Code ZCTA Latitude, Longitude, Population Density.py
Last active July 13, 2023 01:11
USA ZCTA (Zip Code Tabulation Area) Latitude, Longitude, GeoJSON Points, Population, Population Density: 2010 pop figures; 2017 ZCTA codes.
# !/usr/bin/python
"""
US 2017 Zip Code Tabulation Areas (ZCTA), latitude and longitude, and 2010 Census population, from US government data.
The final file includes GeoJSON Points for each ZCTA, and estimated population density per square kilometre.
Python code to create this Gist follows.
Notes:
@nmpowell
nmpowell / python-venv-install-jupyter-qtconsole-notebook.bat
Last active February 22, 2019 17:46
Windows batch script to set up Jupyter's QtConsole and Notebook in a Python3 virtual environment. With shortcuts.
REM Windows batch script to set up Jupyter QtConsole and Notebook in a Python3 virtual environment.
REM Place this script .bat file inside the root venv directory (same level as 'Scripts' and 'Lib' subfolders), and run.
REM Also creates convenience launching scripts.
REM Get current directory name and full path
for %%* in (.) do set VenvDirName=%%~nx*
set VenvDirFullPath=%~dp0
REM Activate the venv
call %VenvDirFullPath%Scripts\activate.bat
@nmpowell
nmpowell / README.md
Last active April 22, 2021 16:16
Windows batch script to install locally-saved Python packages into a virtual environment.

Windows batch script to install locally-saved Python packages into a virtual environment

A hacky script to install Python packages from local wheels / tarballs. pip should do this by retrieving remote files, but if you're behind a firewall, you can use this instead.

  • Find and download packages - ideally platform-specific wheel (.whl) files from: https://pypi.python.org/pypi
  • set the path and name of your desired venv on line 6.
  • package_names_in_order.txt lists files in a specific order, to enable installing requirements in the correct dependency order.
  • /packages/ subdirectory (located relative to the .bat script dir) can contain anything pip accepts: .whl; .tar.gz; ...
  • If you don't require a specific installation order, you can just use (note this is only for .whl files):
@nmpowell
nmpowell / ApplicationOnTime_Mixed_Argument_Demo.bas
Created July 4, 2017 10:32
Excel VBA macro to demonstrate mixed argument types (Integer and String) to Application.OnTime.
' Demonstrating mixed argument types to Application.OnTime (Integer and String).
' See these pages for more:
' - http://www.markrowlinson.co.uk/articles.php?id=10
' - https://www.mrexcel.com/forum/excel-questions/81724-calling-procedure-parameters.html
' - https://stackoverflow.com/questions/31439866/multiple-variable-arguments-to-application-ontime
Sub RunMe()
Dim testName As String
Dim counter As Integer
@nmpowell
nmpowell / CountdownTimer.bas
Created June 28, 2017 07:22
A countdown timer in VBA, for MS Excel. Copy-paste into a module in a .xlsm workbook.
Dim updatingTimeNow As Date
Private Sub Auto_Open()
' Run as the workbook opens
Call InitialiseTimer
End Sub
Private Sub SetUp()
' Run before anything else, independently
Cells(1, 1) = "Countdown timer"