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 / 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 / thousandsSeparater.js
Created June 29, 2015 13:08
Javasript thousands seperator
function thousdansSeparater(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
@johnpaulhayes
johnpaulhayes / reverse_mailmerge.asp
Created May 8, 2015 11:39
Macro for splitting a mail-merge document into single files.
Sub Splitter()
Dim Mask As String
Dim Letters As Long
Dim Counter As Long
Dim DocName As String
Dim oDoc As Document
Dim oNewDoc As Document
Set oDoc = ActiveDocument
oDoc.Save
@johnpaulhayes
johnpaulhayes / sum_value_in_dict.py
Last active August 29, 2015 14:19
Sum a value field in a dictionary
dicts_with_values = [{"value": 30}, {"value": 20}, {"value": 1}]
""" Horrible! """
result = reduce(lambda x, y: x + y, [i["value"] for i in dicts_with_values])
""" Nice, Pythonic """
result = sum([i["value"] for i in dicts_with_values])
@johnpaulhayes
johnpaulhayes / ExchangeRates.py
Created February 19, 2015 17:57
Retrieve exchange rates from Central Bank of Ireland and load them into a dictionary
# -*- coding: utf-8 -*-
import urllib
import xlrd
class ExchangeRates():
"""
Class to fetch exchange rate information from the Central Bank of Ireland
Example:
@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 / upload_file_to_s3.py
Created June 25, 2014 14:52
Upload a file to S3 bucket
"""
Requirements:
AWS Account
Valid Access key and secret
A valid bucket setup
"""
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import sys
@johnpaulhayes
johnpaulhayes / ubuntu_update_heartbleed.txt
Created April 10, 2014 09:18
Update Ubuntu OpenSSL in response to Heartbleed bug.
sudo apt-get update
sudo apt-get install -y libssl1.0.0 openssl
# Confirm Build Date is at least Aril 7th 2014
openssl version -a
# Restart all services listed by this command:
sudo lsof -n | grep ssl | grep DEL