Skip to content

Instantly share code, notes, and snippets.

@jnhmcknight
jnhmcknight / download_large_file.py
Created October 25, 2023 16:43 — forked from wasi0013/download_large_file.py
how to download large file using python, requests without exhausting device ram.
import requests
chunk_size = 4096
filename = "logo.png"
document_url = "https://wasi0013.files.wordpress.com/2018/11/my_website_logo_half_circle_green-e1546027650125.png"
with requests.get(document_url, stream=True) as r:
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size):
if chunk:
f.write(chunk)
from enum import Enum, EnumMeta
from flask import Flask, current_app
from werkzeug.routing import BaseConverter, ValidationError
def setup_enum_converter(enum_to_convert):
if not isinstance(enum_to_convert, (Enum, EnumMeta,)):
@jnhmcknight
jnhmcknight / get-out-of-debt.py
Last active October 6, 2022 17:17
Debt clearing month calculator
#!/usr/bin/env python3
"""
This takes a JSON file of your current debts, and does a rollover calculation to see how long it will
take to pay off all of it, based on clearing the smallest debt first and merging that payment into
the next smallest debt's payment, and then repeating that process until all debts have been paid.
This calculator assumes that you are not increasing any of the debts in any way (ie: not still using
your credit card or credit line to pay for any things.)
Save this file locally, and run:
@jnhmcknight
jnhmcknight / s3.py
Last active August 9, 2022 14:21
S3 Bucket Point-In-Time Snapshot Creation
"""
This requires that you have versioning enabled on the S3 bucket. Without that, this script cannot do anything useful.
Install Dependencies: `pip3 install boto3 click dateparser pytz`
Run it: `python3 ./s3.py bucket-snapshot [OPTIONS] BUCKET-NAME UTC-DATE-TIME DESTINATION-FOLDER`
"""
import json
import os
@jnhmcknight
jnhmcknight / repo-sercret.py
Last active May 25, 2022 13:56
Add or update repo secrets based on the contents of one or more .env files
#!/usr/bin/env python3
"""
Add or update repo secrets based on the contents of one or more .env files
To run this, you will need:
- Python3 installed
- have created a personal access token with the relevant privileges within GitHub
- copied this gist into a file named `repo-secret.py`
- and complete the following:
@jnhmcknight
jnhmcknight / postgres_queries_and_commands.sql
Last active November 4, 2022 13:36 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show granted locks
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks AS blocked_locks
JOIN pg_catalog.pg_stat_activity AS blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks AS blocking_locks
#!/usr/bin/env python3
"""Show subnets information.
Given an IP network in CIDR notation and the minimum number of desired subnets,
this script will provide the relevant information about the desired subnets.
Example:
$ ./subnet-divider.py 10.10.0.34/16 4
$ ./subnet-divider.py fd3e:48fe:59b2:43ca::/64 15
@jnhmcknight
jnhmcknight / mandrill-hook-utils.py
Last active April 27, 2020 15:02
Mandrill webhook decorator
"""
Utilities for use with Mandrill Webhooks
requirements:
- six
- email-reply-parser (git+https://github.com/atc0m/email-reply-parser.git#egg=email_reply_parser)
"""
import base64
@jnhmcknight
jnhmcknight / inject_ca.py
Created July 10, 2018 16:01
Inject an arbitrary certificate authority into a certifi bundle
#!/usr/bin/env python
"""
Usage: python ./inject_ca.py <path/to/new/ca.crt> [<path/to/other/new/ca.crt> [...]]
"""
import certifi
import os
import re
import sys
@jnhmcknight
jnhmcknight / colour-inverter.py
Created June 15, 2018 14:09
Invert all colours in a CSS file.
#!/usr/bin/env python
# Concept taken from http://code.activestate.com/recipes/527747-invert-css-hex-colors/
# Modified to take the css filename as an argument and also handle rgb() and rgba() values
#
# To run:
# python ./colour-inverter.py <path/to/file.css>
import os
import re