Skip to content

Instantly share code, notes, and snippets.

View kgriffs's full-sized avatar

Kurt Griffiths kgriffs

View GitHub Profile
@kgriffs
kgriffs / trim_mp3s.sh
Last active March 19, 2024 17:28
Bash script to trip mp3s to one hour in length and convert to AAC
#!/bin/bash
# Set the directory where your MP3 files are located
input_dir="."
# Set the desired output bitrate (in bits per second)
output_bitrate="192k" # Adjust this value as needed
# Loop through each MP3 file in the directory
for file in "$input_dir"/*.mp3; do
@kgriffs
kgriffs / example.py
Last active February 13, 2024 19:22
Python TZ Aware datetime and DST Conversion Example
import datetime
import pytz
tz_ny = pytz.timezone('America/New_York')
dt_aware_dst_active = tz_ny.localize(datetime.datetime(2023, 5, 1, 12, 0))
dt_aware_dst_inactive = tz_ny.localize(datetime.datetime(2023, 12, 1, 12, 0))
print("Original DST Active Offset (America/New_York):", dt_aware_dst_active.dst())
print("Original DST Active (America/New_York):", dt_aware_dst_active)
@kgriffs
kgriffs / example.yml
Created January 16, 2024 17:04
Github Action (GHA) caching a downloaded file example
- name: Cache downloaded file
id: cache-some-file
uses: actions/cache@v3
with:
path: some-file
key: ${{ runner.os }}-downloaded-file-${{ hashFiles('some-file') }}
- name: Fetch smctl MSI
if: steps.cache-some-file.outputs.cache-hit != 'true'
shell: pwsh
run: |
@kgriffs
kgriffs / datadog_fetch_logs.py
Last active November 27, 2023 17:26
DataDog Download Logs Example
import requests
import time
class DatadogLogIterator:
_MAX_LIMIT = 1_000
_FETCH_LIMIT = _MAX_LIMIT
def __init__(self, query, start, end, api_key, app_key):
self._cursor = None
@kgriffs
kgriffs / async-does-not-mix.py
Last active November 18, 2022 21:22
Example demonstrating how a synchronous control flow never yields to tasks on the event loop, starving them.
import asyncio
import httpx
import time
async def do_work_async():
http = httpx.AsyncClient()
while True:
resp = await http.get('https://httpbin.org/status/202')
@kgriffs
kgriffs / example_asyncio_signal_shutdown.py
Last active November 2, 2022 01:52
Example showing how to trap a signal with a Python asyncio coroutine function and exit cleanly without logging an error.
import asyncio
import functools
import signal
import sys
import time
async def worker(shutdown_event):
try:
while True:
@kgriffs
kgriffs / aiobotocore-config-example.py
Last active November 7, 2022 16:39
aiobotocore config example (vs. boto3) for setting connect_timeout and usign the standard retries mode
import asyncio
import aiobotocore.session
import aiobotocore.config
async def example():
# Equivalent to boto3.client('s3', config=botocore.client.Config(**kwargs))
# See also for available options:
# * https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
@kgriffs
kgriffs / boto3async.py
Last active October 4, 2023 14:47
asyncio boto3 wrapper to run boto3 client methods in a thread pool executor as an alternative to aiobotocore
# =============================================================================
# Copyright 2022 by Rafid Al-Humaimidi. All rights reserved.
# Licensed via Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
#
# Forked from: https://github.com/rafidka/boto3async
# =============================================================================
"""Adds simple async wrappers around boto3 client methods.
This module adds async methods to the stock boto3 clients. The async versions of
@kgriffs
kgriffs / example.sh
Last active August 24, 2022 03:18
Start redis server without disk persistence; memory only for testing (avoids having to clean up a dump.rdb file)
redis-server --save "" --appendonly no
@kgriffs
kgriffs / listening.sh
Created July 29, 2022 22:01
macOS port listening bash
# https://stackoverflow.com/a/30029855/21784
listening() {
if [ $# -eq 0 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P
elif [ $# -eq 1 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color $1
else
echo "Usage: listening [pattern]"
fi
}