Skip to content

Instantly share code, notes, and snippets.

View alecbw's full-sized avatar

Alec Barrett-Wilsdon alecbw

View GitHub Profile
@alecbw
alecbw / cron_display.sh
Created September 2, 2022 09:56
Display a message on Mac every 30 minutes
crontab -e
# Then, in vim editor, add
*/30 * * * * osascript -e 'display notification "🚰 drink water 🚰"'
# Then, save and exit
:wq
# To confirm, run
crontab -l
@alecbw
alecbw / add_utms_to_links_in_text.py
Last active August 31, 2022 13:48
Can find and replace all urls with a given domain in a block of text or stringified HTML
@alecbw
alecbw / runFunctionOnceDefined.js
Created October 1, 2021 22:07
Waits ("sleeps") until a Function is defined in the local environment
// This solves a problem where a function in one source.js file wants to call a function in a separate source-2.js file that has yet to be defined
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function runFunctionOnceDefined(functionName) {
while(typeof window[functionName] !== "function") {
await sleep(50); // check every 0.05s
}
import requests
import os
def get_stripe_checkout_session(checkout_session_id):
api_url = "https://api.stripe.com/v1/checkout/sessions/"
api_url += checkout_session_id
api_url += "?expand[]=line_items"
api_url += "&expand[]=customer"
api_url += "&expand[]=total_details.breakdown.discounts.discount"
@alecbw
alecbw / Generic_CloudFormation_IAM_For_External_Vendor_Access.yml
Last active June 8, 2021 03:07
YAML for CloudFormation that creates an IAM User, an IAM Policy with permissions on S3, Glue, Athena to use Athena and DynamoDB, and attaches the Policy to the User. It will also produce an Access Key and Access Secret.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Creates an IAM User with attached Role for VENDOR Access'
Resources:
User:
Type: AWS::IAM::User
Properties:
UserName: 'VENDOR-access-iam-user'
Tags:
- Key: "deployment"
Value: "CloudFormation"
@alecbw
alecbw / Log_Container_Shutdown.py
Created May 26, 2021 15:32
Logs SIGKILL value such that an Error Handling platform like Sentry can capture it
import signal
def shutdown(signum, frame):
logging.error(f'Caught SIGTERM {signum}, shutting down')
exit(0)
if __name__ == "__main__":
# Register handler
signal.signal(signal.SIGTERM, shutdown)
@alecbw
alecbw / generate_s3_presigned_url.py
Created May 21, 2021 19:09
Wrapper to generate a pre-signed URL for temporary access (up to 7 days) to a file in an otherwise inaccessible S3 bucket. Optionally supports S3 Transfer Acceleration
# DO NOTE: this will generate a url even if the file_name is not actually in the bucket
def generate_s3_presigned_url(bucket_name, file_name, **kwargs):
client = boto3.client(
's3',
config=Config(
signature_version='s3v4',
s3 = {'use_accelerate_endpoint': kwargs.get("accelerate_endpoint", False)}
)
)
@alecbw
alecbw / detect_ip_address_type.py
Created May 19, 2021 17:43
Parsing functions to detect if a string is an IP Address, and if so, which type.
from string import hexdigits
import logging
# ex: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
def is_ipv6(potential_ip_str):
pieces = potential_ip_str.split(':')
if len(pieces) != 8:
return is_ip = False
else:
for i in range(len(pieces)):
@alecbw
alecbw / pretty_print_json.sh
Created May 16, 2021 01:56
A quick oneliner you can add to your .bash_profile to pretty print the contents of your clipboard.
# Takes the contents of the clipboard, replaces apostrophes (') with quotation marks (") and replaces titlecased bools (True/False) with JSON acceptable lowercase, then pretty prints
function pjson() {
pbpaste | sed "s/'/\"/g" | sed "s/True/true/g" | sed "s/False/false/g" | json_pp
}
@alecbw
alecbw / zip_two_lists_of_dictionaries.py
Created April 21, 2021 01:48
Combine two lists of dictionaries into one list of dictionaries, where a primary_key is used to join the dictionaries between them
from collections import defaultdict
"""
Zip is at the dict level - if only some of the dicts in a lod have a key,
only resultant dicts with one of their primary_keys will have that given k:v pair
When both lods have a given (non-primary) key, the lod_2 value is prioritized.
Originally from https://stackoverflow.com/questions/5501810/join-two-lists-of-dictionaries-on-a-single-key
"""
def zip_lods(lod_1, lod_2, primary_key, **kwargs):