Skip to content

Instantly share code, notes, and snippets.

View evan-burke's full-sized avatar

Evan Burke evan-burke

  • Oakland, CA
View GitHub Profile
# simple password generator
# https://docs.python.org/3/library/secrets.html#recipes-and-best-practices
import argparse
import secrets
import string
# todo: add args for specialchars etc
# specify min numbers of capital letters, digits, etc.
@evan-burke
evan-burke / smtp.py
Last active February 4, 2022 01:14
python smtp quick reference
# work in progress. I'm not 100% sure on distinction between legacy compat32 api and new API yet, so grain of salt and all.
import email
# __init__ is empty in email.mime so you need to import specific files/modules.
import email.mime.multipart
@evan-burke
evan-burke / seq_idx.py
Last active June 15, 2020 20:43
Detect repeated values in a series, and assigning an index to each sequence
import pandas as pd
# Use case: triggering an alert only if, say, monitoring is outside of a desired value for 4 hours in a row
def detect_sequential_failures(series, how_many):
# Takes as input a pd.Series of True or False values.
# Calculate like, e.g.,: df['my_condition_evaluation'] = df['testcol'] < threshold)
# then: detect_sequential_failures('my_condition_evaluation', 3)
#
# Returns a series with None for False items or True items in a sequence < how_many in a row,
@evan-burke
evan-burke / configparser
Last active August 12, 2020 17:06
python configfile parser example
# because I have to look up the syntax every time:
import configparser
CONFIG_FILENAME = "/path/to/config.file"
# or
# CONFIG_FILENAME = "../relative/path/to/config.file"
# for current dir, just: "config.file"
@evan-burke
evan-burke / jupyter_asyncio_example.py
Last active June 9, 2021 00:42
jupter & asyncio issues
# Jupyter runs its own event loop, so trying to work with asyncio causes problems in Jupyter.
# Errors are most commonly:
# RuntimeError: This event loop is already running
## NOTE: as of IPython 7.0, released in late 2018, this may not be needed, per:
# https://blog.jupyter.org/ipython-7-0-async-repl-a35ce050f7f7
# However, await(x) can be used anywhere in Jupyter, but will error on a standalone script.
# Use asyncio.run(x) or something for .py scripts as described in https://docs.python.org/3/library/asyncio-task.html
# Solution:
@evan-burke
evan-burke / docker_wsl_mounts.md
Last active February 1, 2020 01:23
docker WSL volume mounts

Docker under WSL has some issues with volume mounts. They will tend to fail silently (as far as I can tell).

Steps to follow to get these working:

  1. Change default mount point for the Win host FS under WSL. Edit /etc/wsl.conf and add or change the following. You may need to fully reboot Windows for this to take effect.
[automount]
@evan-burke
evan-burke / sadb.py
Last active September 5, 2020 23:39
simple sqlalchemy wrapper
import configparser
import sqlalchemy as sa
__sadb_version__ = "0.0.2"
# 2020-09-03:
# Jeremy Howard's 'fastsql' is quite a bit better designed than this, but it's also higher-level - uses more ORM stuff
# https://github.com/fastai/fastsql
# honestly this could probably be a full module...
@evan-burke
evan-burke / fabric_ssh.py
Created January 9, 2020 23:29
ssh with Fabric using an SSH key
# Fabric can be used to run commands on a remote system over SSH.
# Sadly, its docuentation is a bit short for connecting using a private key file.
# Other options exist too, like this one using an SSH config file - https://gist.github.com/aubricus/5157931
import fabric
# Openssh formatted private key:
keyfile = "/path/to/your/privkey"
host = "fqdn"
@evan-burke
evan-burke / schema.py
Last active July 5, 2022 16:08
Fastest way to generate a SQL schema from raw data and/or insert data to the table
# Use Pandas & SQLAlchemy.
# https://stackoverflow.com/questions/23103962/how-to-write-dataframe-to-postgres-table
# Note this will create a new table; see the 'faster option' at the above link for a method using 'copy' to an existing table.
# However, 'copy' cannot do an upsert; that requires inserting to a temp table, then upserting form temp table to destination table.
# This will lack PKs and FKs and indexes, of course, so if used naively you may see data duplication.
# Also the df.to_sql command can do an append (but not upsert), using the if_exists param:
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
@evan-burke
evan-burke / dnspython.py
Last active November 29, 2021 13:00
quick dnspython usage reference
# https://github.com/rthalley/dnspython
# pip install dnspython
# NOTE reverse DNS lookups on Docker seem to be failing with Docker versions between ~18.6 and ~19.03.8, as of 2020-05-13
# This generates errors like:
# `The DNS response does not contain an answer to the question: 9.8.7.6.in-addr.arpa. IN PTR`
# Workaround: start containers with external DNS where possible using the --dns flag in 'docker run'
import dns.resolver
import sys