Skip to content

Instantly share code, notes, and snippets.

View danwald's full-sized avatar
💻
coding

Danny Crasto danwald

💻
coding
View GitHub Profile
import threading
import time
from concurrent.futures import ProcessPoolExecutor
lock = threading.Lock()
def process_items(name):
lock_id = id(lock)
print(f"{name}: acquiring lock:{lock_id}")
with lock:
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "requests",
# "beautifulsoup4",
# "selenium",
# "ipdb",
# ]
# ///
@danwald
danwald / uv_cowsay.py
Created May 20, 2025 16:24
uv cowsay
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = ["cowsay"]
# ///
import cowsay
cowsay.cow('Hello World')
@danwald
danwald / attrib.py
Last active May 11, 2023 16:37
attribute lookup
import dataclasses
import logging
logging.basicConfig(level='INFO')
logger = logging.getLogger(__name__)
@dataclasses.dataclass
class DictAttrib:
# class attribute
'''
encodes a pair of +ive ints to an int and complementary method
src: http://mathforum.org/library/drmath/view/56036.html
'''
def encode_pair(a: int, b: int) -> int:
'''
implements
[(a + b)^2 + 3a + b]/2
@danwald
danwald / flac2mp3.sh
Created July 27, 2019 10:07
flac to mp3
#!/bin/bash
for a in *.flac; do
ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done
@danwald
danwald / redis_keys_scan_age
Last active August 27, 2018 07:57
scans redis to get key counts
import redis
import sys
ip = '172.35.5.201'
port = 6379
db = 1
sixty_days_back_sec = 5184000
thirty_days_back_sec = sixty_days_back_sec / 2
ten_days_back_sec = thirty_days_back_sec / 3
total=ten_db=thirty_db=sixty_db=deleted=0
@danwald
danwald / fc.sh
Last active February 12, 2017 19:48
Flock commands so scripts/cronjobs don't overrun. Send email on failure.
#!/bin/bash
export from_name=''
lockfile=$1
flock=`which flock`
#uses mandrill-sendmail via @blackdotsh's https://raw.githubusercontent.com/blackdotsh/mandrillapp-cli/master/sendmail.sh
sendmail=$HOME/msendmail.sh
dt=`date`
$flock -xn "/tmp/$lockfile" echo "${@:2}"
@danwald
danwald / dbash.sh
Created July 11, 2015 10:29
open a bash shell in a docker container or latest one
function dbash(){
if test -z "$1"
then
container=`docker ps -lq`
echo "Using last active container $container"
else
container=$1
fi
docker exec -it $container bash
}
@danwald
danwald / a.rb
Created May 3, 2015 00:36
Extract the last timestamp from a logfile and check if it was more than a minute ago
LOG_TS=$(tail -n 1000 __STR__ | grep INFO | tail -n 1 | grep -Po "\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}") && [[ $((`date +%s`-`date +%s -d"$LOG_TS"`)) -lt 60 ]] || echo "More than a minute"