Skip to content

Instantly share code, notes, and snippets.

@studiotomi
studiotomi / async_downloader.py
Last active August 28, 2019 15:00
Asyncrounous Anything in python 3
import threading
from queue import Queue
class AsyncThread(threading.Thread):
"""
Based off of ssynchronous downloading script pull from stackoverflow
http://stackoverflow.com/questions/18883964/asynchronous-file-downloads-in-python
"""
def __init__(self, queue, func):
@studiotomi
studiotomi / gist:d25e7dbe42a6019fdc08
Created June 19, 2015 15:36
Work on Chunks of an array
def chunks(l, n):
""" Yield successive n-sized chunks from l.
http://stackoverflow.com/a/312464/1572077
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
@studiotomi
studiotomi / gist:18cf6c91e80da680d5a3
Created July 14, 2015 22:33
Find what base class an attributes is coming from in Python
def find_attribute_class(model, attribute):
"""
If the attribute is found, it returns an array of places the attribute is defined.
The array is ordered by priority, the first element being the one that is called.
If not found, returns an empty array.
"""
return [m for m in model.mro() if attribute in m.__dict__]
@studiotomi
studiotomi / kubctl-cheat-sheet.sh
Created June 24, 2016 19:30
Kubernetes cheat sheat
# To determine which node a container is on:
kubectl get pods -o wide
# To view current resource usuage and limits of a cluser
kubectl describe nodes <cluster name>
# To view what is happening on a container
kubectl attach <container-name>
# To start an interactive shell on a container
@studiotomi
studiotomi / kccontexts.sh
Last active August 3, 2021 18:11
Set kubernets current-context from one of the contexts in the config file
kccontexts() {
# Works with kubectl v1.4.0 and the config file generated by gcloud containers clusters get-credentials
# If you pass in an arg, then we will filter the contexts for that string
# The following block creates a list of the contexts and filters if an argument
# is passed in.
if [[ $1 ]]
then
options=(`kubectl config get-contexts -o name | sort | grep $1`)
else
@studiotomi
studiotomi / ps-snapshot.sh
Created February 7, 2017 16:33
Back up postgres db to google cloud storage
#!/bin/bash
# Requirements:
# - gcloud/gsutil is installed on the box
# - gcloud is logged in as a user with write access to Google Cloud Storage
# - The file has execution rights so that it can be run in cron
# - The Google Cloud Storage bucket already exits
# Exit on any error
set -e
@studiotomi
studiotomi / containerCommand.sh
Created June 12, 2019 14:59
Given a string, find id of matching docker container, and return id and name or execute arbitrary command.
#!/bin/bash
function containerCommand() {
# Given a string, search for the matching running docker containers.
# If more than one argument is passed, attempt to run remaning arguments
# on that docker container.
container=`docker ps --format '{{.ID}} {{.Names}}'1`;
shift;
if [[ ! -z "$@" ]]
then
find the difference between the arc lengths of the inner and outer radius you want and divide by the kerf width, that'll give you the number of cuts, then space them evenly
@studiotomi
studiotomi / truncate.sql
Created August 19, 2019 17:22
truncate all tables in a postgres schema
DO $func$
BEGIN
EXECUTE (
SELECT
'TRUNCATE TABLE ' || string_agg(oid::regclass::text, ', ') || ' CASCADE'
FROM
pg_class
WHERE
relkind = 'r'
AND relnamespace = 'schema_name'::regnamespace);
@studiotomi
studiotomi / python_profile.sh
Created October 7, 2019 14:23
profile a python process
python3 -m cProfile -s cumulative script.py > profile.txt