Skip to content

Instantly share code, notes, and snippets.

View awbacker's full-sized avatar

Andrew Backer awbacker

  • ScantTrust
  • Shanghai, China
View GitHub Profile
@awbacker
awbacker / virtual-env-starter
Created March 16, 2015 08:45
Simple script to load a python virtual env from either ./venv/, a '.venv' file, or a global venv directory
venvnow ()
{
venv_default_root=~/dev/venvs;
cur_dir=`pwd`;
if [ -d venv ]; then
echo " - found local ./venv/, using that";
source ./venv/bin/activate;
return 0;
fi;
if [ -f '.venv' ]; then
@awbacker
awbacker / form_data_flattener.py
Last active August 12, 2021 12:13
Flattens a nested dictionary so that it can be more easily posted as multipart/form-data. Helpful especially with unit tests, requests.post, etc
def flatten_dict_for_formdata(input_dict, array_separator="[{i}]"):
"""
Recursively flattens nested dict()s into a single level suitable for passing to a library
that makes multipart/form-data posts.
- nested dicts, tuples (treated like arrays), lists, list of objects, etc
{
"owner": {
"name": {"first": "andy"},
"favorite_color": ("blue", "green")
},
@awbacker
awbacker / crash-pil.py
Created May 13, 2016 11:03
Crash PIL with single bit TIFF
# -*- coding: utf-8 -*
from __future__ import print_function, absolute_import, unicode_literals
import six
from PIL import Image
from django.core.files.uploadedfile import SimpleUploadedFile
Image.DEBUG = True
with open("/Users/andrew/Desktop/sample.tiff", 'rb') as f:
@awbacker
awbacker / url_ex.py
Last active May 17, 2016 08:46
URL Helper for Django / Django Rest Framework
# -*- coding: utf-8 -*
#
# URL_EX : url helper to make dealing with patterns in urls easier, and prevent common issues
# - calls .as_view() on DRF APIView instances automatically
# - appends a leading ^
# - appends a trailing $ the the call isn't an include()
# - add more shortcuts and patterns as required
#
# Examples:
# urlpatterns = [
@awbacker
awbacker / bash_prompt.sh
Created April 16, 2017 05:33 — forked from insin/bash_prompt.sh
Set color bash prompt according to active virtualenv, git branch and return status of last command.
#!/bin/bash
#
# DESCRIPTION:
#
# Set the bash prompt according to:
# * the active virtualenv
# * the branch/status of the current git repository
# * the return value of the previous command
# * the fact you just came from Windows and are used to having newlines in
# your prompts.
import re
from inspect import isfunction, isclass
from typing import Union, List, Callable
from django.urls import path as _django_path, include, register_converter
from rest_framework.routers import SimpleRouter
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSetMixin
TYP = Union[SimpleRouter, List, Callable, APIView, ViewSetMixin, str]
#!/usr/bin/env python
# -------------------------------------------------------
# Place this somewhere in your path, and make it executable.
# Inside any venv, run `outdated.py path/to/requirements.txt`
# By default it will attempt to read ~/requirements.txt
#
# $> cd project && source .venv/bin/activate
# $> outdated.py requirements/common.txt
# >> Reading requirements (/home/user/project/requirements/common.txt)
@awbacker
awbacker / venvnow.sh
Created March 24, 2023 09:12
Shell function to enable the "logical" virtualenv based on your current directory
function venvnow() {
GREEN="\033[0;32m"
NC='\033[0m' # No Color
venv_default_root=~/dev/venvs
function try_dotvenv {
if [ -d '.venv' ]; then
echo -e " - found ${GREEN}.venv${NC} directory"
if [ -f ".venv/bin/activate" ]; then
source .venv/bin/activate
@awbacker
awbacker / gist:62473baa4e4a662ec3fb8d3198f672e9
Last active August 18, 2023 03:27
Starter script for purging a YT music library
# > python3.11 -m venv .venv
# > source .venv/bin/activate
# > pip install ytmusicapi
# Run the following command to generate the auth token file (oauth.json):
# > ytmusicapi oauth
import logging
import ytmusicapi