Skip to content

Instantly share code, notes, and snippets.

View djm's full-sized avatar

Darian Moody djm

View GitHub Profile
@djm
djm / dl.py
Created July 19, 2014 13:03
Download all from imgur embed URL
import os
import sys
from os.path import basename, splitext
from urlparse import urlparse
import requests
from bs4 import BeautifulSoup
def extract_full_size_url_from_tag(image_tag):
@djm
djm / Dockerfile
Created October 20, 2014 21:03
Simple docker/fig setup for Django
FROM python:2.7
# Force stdin, stdout and stderr to be totally unbuffered.
ENV PYTHONUNBUFFERED 1
# For Pillow to have freetype support.
RUN ln -s /usr/include/freetype2 /usr/include/freetype
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
@djm
djm / closures_ftw.py
Created February 26, 2015 02:57
Prefer closures over classes keeping state
# Bad
class Join(object):
"""
>>> join_by_commas = Join(',')
>>> join_by_commas(['o', 'm', 'g'])
'o,m,g'
"""
def __init__(self, separator=u' '):
self.separator = separator
@djm
djm / cancel_jobs.py
Last active August 31, 2015 21:02
Delete/cancel all in progress and pending jobs in an Elastic Transcoder region
import time
from boto import elastictranscoder
REQUEST_FREQ = 0.3 # Every x seconds
REGION = 'eu-west-1'
TO_CANCEL = [
'Progressing',
'Submitted'
]
@djm
djm / cron
Last active December 17, 2015 09:58
Cron one liners for virtualenv based django management commands
# I mean this:
* */12 * * * /path/to/virtualenv/bin/python /path/to/django/project/manage.py cleanup
# over this:
* */12 * * * source /path/to/virtualenv/bin/activate; /path/to/django/project/manage.py cleanup
@djm
djm / retrieve_dict.py
Last active December 30, 2015 00:19
Retrieve a list of a Scrapy project's spiders via Python
# Works on Scrapy v0.20
from scrapy.crawler import Crawler
from scrapy.utils.project import get_project_settings
def get_spiders():
""" Retrieves a dict of the spiders classes available to the current
scrapy keyed by their string-name.
"""
settings = get_project_settings()
@djm
djm / human_readable.py
Last active January 3, 2016 10:59
Convert Programming Cased strings to a Human Readable one
import re
def camelcase_to_human_readable(camel_cased_string):
""" Converts a string from CamelCase to Human readable.
e.g SuperBadRobot -> Super Bad Robot
"""
return ' '.join(re.findall("([A-Z][^A-Z]*)", camel_cased_string)).strip()

Keybase proof

I hereby claim:

  • I am djm on github.
  • I am djm (https://keybase.io/djm) on keybase.
  • I have a public key whose fingerprint is DBB9 0727 AD94 8582 ECED 66EB 7EA3 B5B0 D8AD 79BB

To claim this, I am signing this object:

@djm
djm / pre-commit.py
Created September 21, 2016 13:53 — forked from Geekfish/pre-commit.py
Pre-commit hook with flake8 and auto-named migrations
#!/usr/bin/env python
import sys, subprocess, collections
from flake8.hooks import git_hook, get_git_param
# `get_git_param` will retrieve configuration from your local git config and
# then fall back to using the environment variables that the hook has always
# supported.
# For example, to set the complexity, you'll need to do:
# git config flake8.complexity 10
COMPLEXITY = get_git_param('FLAKE8_COMPLEXITY', 10)
@djm
djm / bookmarklets.js
Created September 26, 2016 13:22 — forked from Geekfish/bookmarklets.js
Trello Card bookmarklets
// Get Card's last URL segment (eg 4468-fix-the-login-page):
javascript:(function(s){try{s=document.selection.createRange().text}catch(_){s=document.getSelection()}prompt('', window.location.href.split("/").pop().split("#")[0])})()
// Get Card's full hash (eg 5731b92ef660293533517de9) :
javascript:(function(s){try{s=document.selection.createRange().text}catch(_){s=document.getSelection()}$('.js-more-menu').click(); prompt('', $('.js-export-json').attr('href').split("/")[2]); $('.js-more-menu').click();})()
// Get Card's unique id (board-independent, eg rKBmQeOk) :
javascript:(function(s){try{s=document.selection.createRange().text}catch(_){s=document.getSelection()}$('.js-more-menu').click(); prompt('', $('.js-short-url').attr('value').split("/").slice(-1)[0]); $('.js-more-menu').click();})()