Skip to content

Instantly share code, notes, and snippets.

@rca
rca / ajax_setup.js
Created January 29, 2012 00:27
Setup Django CSRF token in JQuery AJAX requests
/**
* setup JQuery's AJAX methods to setup CSRF token in the request before sending it off.
* http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
*/
function getCookie(name)
{
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
@rca
rca / fsm.py
Last active September 29, 2023 16:41
Python Finite State Machine implementation; logic mostly extracted from https://github.com/kmmbvnr/django-fsm
"""
Finite State Machine
This FSM implementation is extracted from the django-fsm package and licensed
under the same BSD-like license at:
https://github.com/kmmbvnr/django-fsm/blob/master/LICENSE
Basic usage:
@rca
rca / iso2unix.py
Created September 11, 2012 21:08
Convert an ISO 8601 timestamp to unix timestamp
import calendar
from iso8601 import parse_date
def iso2unix(timestamp):
"""
Convert a UTC timestamp formatted in ISO 8601 into a UNIX timestamp
"""
# use iso8601.parse_date to convert the timestamp into a datetime object.
@rca
rca / README.md
Last active February 25, 2022 05:33
An explanation of `git rebase --onto`

git rebase onto fu

Looking at the following git history, there are a few commits that were accidentally made on top of env/dev-auth that should be on a feature branch named experiments/sso-login-app (1. in the command below):

[0][~/Projects/openslate/thing(env/dev-auth:feb0ee9)]
$ git log
commit feb0ee98c8b77e929b9cc23442c5664c9d4986c9 (HEAD -> env/dev-auth)  # this is `0.` in the command below
Author: Roberto Aguilar <roberto@openslate.com>
Date:   Thu Sep 26 00:25:15 2019 -0400
@rca
rca / tasks.py
Created March 27, 2012 08:14
break a big celery job into smaller, batched, chunks
"""
Celery tasks that batch a job with many tasks into smaller work sets.
The problem I'm attempting to solve is one where a job comprised of many
tasks (say 100) will snub out a job comprised of only a few tasks (say 5). It
appears as though by default celery will queue up the second job's 5 tasks
behind the first job's 100 and it will have to wait until the first job's
completion before it even begins.
@rca
rca / openldap_passwd.py
Last active January 21, 2022 14:00
Python hashing and test functions for user passwords stored in OpenLDAP.
#!/usr/bin/env python
"""
http://www.openldap.org/faq/data/cache/347.html
As seen working on Ubuntu 12.04 with OpenLDAP 2.4.28-1.1ubuntu4
Author: Roberto Aguilar <roberto@baremetal.io>
"""
import hashlib
import os
@rca
rca / git_whitespace
Created February 13, 2018 01:31
get rid of whitespace in your uncommitted work
#!/usr/bin/env python2.7
"""
Ensure new lines added to git do not have whitespace
"""
import os
import sys
import time
import traceback
from optparse import OptionParser
@rca
rca / save_requests_session.py
Created September 18, 2013 18:38
Persist a requests session between uses.
#!/usr/bin/env python
import pickle
import requests
URL = 'http://foo.com'
# create a session object
session = requests.Session()
@rca
rca / repath_dylib.sh
Created October 7, 2012 20:37
Repath the references in an OS X .dylib file for packaging within an application.
#!/bin/bash
# Repathing .dylib's references for packaging within an application
# Author: Roberto Aguilar <roberto.c.aguilar@gmail.com>
#
# With help from Nick Jensen's blog at:
# http://goto11.net/how-to-bundle-a-c-library-with-a-cocoa-application/
#
# All references within the dylib except ones to files in /usr/lib are
# converted over to reference the given root.
@rca
rca / gstash.sh
Created September 30, 2012 10:18
Git stash helper functions.
#!/bin/bash
#
# Helper functions around "git stash"
#
# Use this file by sourcing it into your shell environment. For example:
#
# source /path/to/gstash.sh
#
# Once sourced in you can run the commands:
#