Skip to content

Instantly share code, notes, and snippets.

View dmahugh's full-sized avatar

Doug Mahugh dmahugh

View GitHub Profile
@dmahugh
dmahugh / wordle_guess.py
Created June 22, 2022 13:01
Wordle pattern generator
ef wordle_guess(guess, answer):
"""Given a Wordle guess and the current answer, return the Wordle pattern
for that guess. The pattern is a 5-character string
containing G, Y, or W for Green, Yellow, or White."""
assert len(guess) == 5
assert len(answer) == 5
pattern = "WWWWW"
matched = "" # characters in the guess that are correct-position matches
@dmahugh
dmahugh / get_wordle_words.py
Created June 20, 2022 22:15
Download the current list of Wordle words
"""Get list of Wordle words.
"""
from datetime import datetime
from bs4 import BeautifulSoup
import requests
WORDLE_URL = "https://www.nytimes.com/games/wordle/"
FILENAME = f"wordle-words-{datetime.today().strftime('%Y-%m-%d')}.txt"
@dmahugh
dmahugh / index.html
Created February 9, 2020 03:44
HTML template for GAE deployment example
<html>
<head>
<title>Sample app</title>
</head>
<body>
<h1>{{ title }}</h1>
<p><a href="/docs">Docs</a></p>
<p><a href="/about">About</a></p>
<p><a href="/">Home</a></p>
</body>
@dmahugh
dmahugh / main.py
Created February 9, 2020 03:43
simple Flask app for GAE deployment example
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def homepage():
return render_template("page.html", title="HOME PAGE")
@app.route("/docs")
def docs():
@dmahugh
dmahugh / utils.py
Created January 17, 2020 01:21
utility function to print a sqlite table to the console
import shutil
def print_table(*, cursor=None, table=None, title=None, rows=10):
"""Print contents of a table or cursor.
This is for quick printing of small data sets for diagnostic purposes, may
not work well with large numbers of columns. Takes a sqlite3 cursor and
table name as input. Output format and column widths is adjusted to fit
the current console line length as needed.
"""
if not cursor or not table:
@dmahugh
dmahugh / create_sql_server_user.py
Created December 19, 2019 17:39
example of how to create a new user in Cloud SQL for SQL Server
"""Simple test of connecting to a Cloud SQL for SQL Server instance
and creating a new admin user account.
This script assumes that you've created a Cloud SQL for SQL Server
instance and have the Cloud SQL Proxy running locally and listening
on 127.0.01. For instructions on those setup steps, see
https://github.com/dmahugh/cloud-sql-pyodbc
Note two places below you need to set YOUR-* to your chosen passwords.
"""
@dmahugh
dmahugh / excel_to_json.py
Last active July 11, 2023 13:00
convert XLSX file to JSON
"""Example of how to convert an xlsx file to JSON.
Requirements:
- Python 3.7 or higher
- openpyxl (pip install openpyxl)
Assumptions:
- the active worksheet contains a rectangular array of data,
with column names in the first row
- the data fits in memory (makes the code below a bit simpler)
@dmahugh
dmahugh / keyvault_get_secret.py
Created February 20, 2019 22:34
Minimal Python code to read secrets from Azure Key Vaults
# dependencies: azure-common, azure-keyvault
from azure.common.credentials import ServicePrincipalCredentials
from azure.keyvault import KeyVaultAuthentication, KeyVaultClient
def get_secret(secret_name, key_vault_uri, client_id, secret, tenant):
"""Get a secret from Key Vault.
"""
credentials = ServicePrincipalCredentials(
client_id=client_id, secret=secret, tenant=tenant)
@dmahugh
dmahugh / azure_devops_list_projects.py
Created February 8, 2019 06:37
Azure Devops REST API example: list projects in an instance
"""Azure Devops REST API example: list the projects in an instance
To run this example, create an ..\_private folder with an azuredevops.json file
in it that contains a PAT:
{
"bugs-read-only": "YOUR-PERSONAL-ACCESS-TOKEN-HERE"
}
"""
import json
@dmahugh
dmahugh / Import JSON data.EXCEL.yaml
Created February 6, 2019 20:41
Imports JSON data into a table.
name: Import JSON data
description: Imports JSON data into a table.
host: EXCEL
api_set: {}
script:
content: |
$("#import-json-data").click(() => tryCatch(importJsonData));
async function importJsonData() {
await Excel.run(async (context) => {