Skip to content

Instantly share code, notes, and snippets.

### Keybase proof
I hereby claim:
* I am jacobpledger on github.
* I am jacobpledger (https://keybase.io/jacobpledger) on keybase.
* I have a public key ASBiGUU0AiWBbCUDo5f1vQ6HIJXzanwL9ECfTKCp_Ctw7Qo
To claim this, I am signing this object:
@jacobpledger
jacobpledger / bfs.py
Created October 28, 2020 19:31
Breadth-first search in Python.
from typing import Any
def bfs(haystack: dict, needle: Any) -> Any:
"""
Breadth-first search in a dict for the specified key, for which the
value is returned.
:param haystack: The dictionary we want to search.
:param needle: the key, whose value we are searching for
:return: the value at the given key or None if not found.
@jacobpledger
jacobpledger / dfs.py
Last active October 28, 2020 19:31
Depth-first search in Python.
from typing import Any
def dfs(haystack: dict, needle: Any) -> Any:
"""
Depth-first search in a dict for the specified key, for which the
value is returned.
:param haystack: The dictionary we want to search.
:param needle: the key, whose value we are searching for
:return: the value at the given key or None if not found.
@jacobpledger
jacobpledger / setup-headless-pi.py
Created August 8, 2020 03:07
Run this after flashing the OS image to a Raspberry Pi SD card to have it connect to your WiFi automatically, allowing headless configuration.
#!/usr/bin/env python3
import argparse
import os
import platform
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
@jacobpledger
jacobpledger / UserBasedExceptionMiddleware
Created October 26, 2017 21:08
Django Middleware to Allow Debug for Superusers
# copy this into your project and add the class to
# your MIDDLEWARE_CLASSES in your settings
from django.views.debug import technical_500_response
import sys
class UserBasedExceptionMiddleware(object):
def process_exception(self, request, exception):
if request.user.is_superuser:
return technical_500_response(request, *sys.exc_info())
@jacobpledger
jacobpledger / find_dead_code.py
Created March 8, 2017 14:31
Django Dead Code Finder
# Copied from https://djangosnippets.org/snippets/10588/
import fnmatch
import pyclbr
import os
import re
import logging
from django.conf import settings
from django.core.management.base import NoArgsCommand
# https://djangosnippets.org/snippets/10591/
from django.core.cache import cache
from django.utils.functional import cached_property
from django.core.paginator import Paginator, Page, PageNotAnInteger
class CachedPaginator(Paginator):
"""A paginator that caches the results on a page by page basis."""
def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True, cache_key=None, cache_timeout=300):
#!/bin/bash
log=$HOME/Documents/log.txt
lm=$(date -r $log +%d)
now=$(date +%d)
if [ $now -gt $lm ]; then
echo '' >> $log
echo $(date -I) $(date +%A) >> $log
echo '' >> $log
@jacobpledger
jacobpledger / validators.py
Last active October 3, 2017 19:38 — forked from jrosebr1/validators.py
Validator for files, checking the size, extension and mimetype for Django.
import mimetypes
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
class FileValidator(object):
"""