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 / FetchEmail.py
Created December 18, 2014 22:19
Python Interface for downloading email attachments for unread email.
import email
import imaplib
import os
class PortalEmail():
"""
Interface to downloading attachments from
an email address and save them to a folder.
TODO: Implement the with context protocol
@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 / lambda_zip_handler.py
Created July 10, 2019 10:08
s3_lambda_zip_handler
s3_resource = boto3.resource('s3')
zip_obj = s3_resource.Object(bucket_name="bucket_name_here", key=zip_key)
buffer = BytesIO(zip_obj.get()["Body"].read())
z = zipfile.ZipFile(buffer)
for filename in z.namelist():
file_info = z.getinfo(filename)
s3_resource.meta.client.upload_fileobj(
z.open(filename),
Bucket=bucket,
@johnpaulhayes
johnpaulhayes / new_product.json
Last active January 3, 2018 16:53
Failing json payload
{
"description": "<h2>A CHILDHOOD CLASSIC</h2><p>This is children's bedroom furniture at its very best. The Huckleberry Cabin Bed is wonderfully sturdy and combines storage with a cool place to snuggle down - it's a real childhood classic.</p><img src=\"https://www.gltc.co.uk/pws/client/images/catalogue/products/l2427/original/l2427_1.jpg\" alt=\"\">|<h2>KEY FEATURES</h2><p>Made from a solid beech frame, with MDF panels and a solid pine slatted base; it's painted in Ivory, so it coordinates well with our <a href=\"/category/dept/bedroom-furniture\">Islander Bedroom Furniture</a> range. Features deep drawers and large cupboards that extend the full width of the bed, and a practical ambidextrous ladder/safety rail attachment, to suit your space. Tested to BS: 8509; suitable for 4yrs+. Assembly service available (&pound;100), includes <a href=\"/content/bed-mattress-guarantee\">10 Year GLTC Guarantee</a>.</p><img src=\"https://www.gltc.co.uk/pws/client/images/catalogue/products/l2427/original/l2427_2.jpg\"
@johnpaulhayes
johnpaulhayes / sum_value_in_list_of_dicts.py
Created February 14, 2017 11:32
Sum an items value in a list of dictionaries
from collections import defaultdict
table = [{"name": "a", "value": 10}, {"name": "b", "value": 110}, {"name": "b", "value": 10}]
c = defaultdict(int)
for d in table:
c[d['name']] += d['value']
print c
@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 / 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 = [