Skip to content

Instantly share code, notes, and snippets.

View johnpaulhayes's full-sized avatar
💭
Always be coding

John Paul Hayes johnpaulhayes

💭
Always be coding
View GitHub Profile
@johnpaulhayes
johnpaulhayes / wordsinwords.py
Created February 1, 2014 14:46
find a list of words in a word for each entry in a dictionary
word_list = []
with open ('word_list.txt', 'r') as words:
for word in words:
word_list.append(word.strip())
def contains(subword, word):
res = []
for s in subword:
@johnpaulhayes
johnpaulhayes / sets_to_handle_post_data
Created January 29, 2014 12:46
using sets to handle HTTP POST data
required = ['a', 'b', 'c']
data = json.loads(request.data)
missing_fields = set(required) - set(data.keys())
if len(missing_fields) == 0:
return Response(status=200)
else:
error = {"error": "missing required fields", "missing_fields": missing_fields}
return Response(response=dumps(error), status=400)
@johnpaulhayes
johnpaulhayes / celery_tasks.py
Created January 13, 2014 16:22
correct task definitions when using tasks by name.
from celery_app import Celery
celery = Celery("tasks", backend="amqp", broker="amqp://guest@localhost")
@celery.task(name='test_task')
def test_task():
return "boom!"
@johnpaulhayes
johnpaulhayes / vagrant_multi_machine.rb
Created November 27, 2013 13:50
Multi-machine Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
config.vm.define :mongo1 do |mongo1|
mongo1.vm.box = "precise64"
mongo1.vm.network :hostonly, "192.168.33.20"
mongo1.vm.customize ["modifyvm", :id, "--memory", 512]
end
@johnpaulhayes
johnpaulhayes / query_arrays.py
Created October 22, 2013 09:56
Query two arrays for the k-th largest
a = [1, 5, 10, 15]
b = [3, 6, 8, 20]
def query_arrays(a, b, k):
i,j=0,0
merged_array = []
while i <= len(a)-1 and j <= len(b)-1:
if a[i] < b[j]:
merged_array.append(a[i])
i += 1
@johnpaulhayes
johnpaulhayes / reverse.py
Created October 18, 2013 10:34
python implementation to reverse a string
'''
@desc: An algorithm to reverse a list of characters.
@param: A list of characters.
'''
def reverse_algorithm(my_string):
length_of_string, i, reversed_string = len(my_string), 0, []
while i < length_of_string:
reversed_string.append(my_string[length_of_string-1-i])
i += 1
return reversed_string
@johnpaulhayes
johnpaulhayes / gist:6881375
Created October 8, 2013 08:22
pre-commit hook that checks for various python based 'crud' such as pdb, ipdb, print statements, console output. Taken from http://css.dzone.com/articles/why-your-need-git-pre-commit
#!/usr/bin/env python
import os
import re
import subprocess
import sys
modified = re.compile('^(?:M|A)(\s+)(?P<name>.*)')
CHECKS = [
@johnpaulhayes
johnpaulhayes / gist:6580864
Created September 16, 2013 13:44
Switch branches of a number of git repositories in a specific path
from git import Repo
from git import InvalidGitRepositoryError
import sys
import os
import getopt
settings = {
'repos_path': '/path/to/your/git/repos',
'cmd_options': 'b:r:',
'this_file_name': 'the_name_of_this_script.py'
@johnpaulhayes
johnpaulhayes / gist:6422556
Created September 3, 2013 11:17
Perform an action for each directory found in a given location
#! /bin/bash
# Description: For each directory in ~/Development, descend into it, pull the latest from git and come back out of it.
find ~/Development/. -type d -print0 -maxdepth 1 | while IFS= read -r -d '' dir
do
cd $dir
git pull
cd ../
done