Skip to content

Instantly share code, notes, and snippets.

@bootandy
bootandy / celery_s3.py
Created August 15, 2013 15:20
celery tasks to upload files to S3
settings = {}
@celery.task
def upload_attachment_to_s3(msg_id, file_data):
conn = boto.connect_s3(settings['aws_access_key_id'], settings['aws_secret_access_key'])
#Connect to bucket and create key
bucket_url = settings['env'] + '.attachments'
b = conn.get_bucket(bucket_url)
key_url = str(msg_id) + file_data['filename']
@bootandy
bootandy / reverse.py
Created May 24, 2013 09:57
python coding for interviews question
"""Returns the sentence with the order of the words reversed,
Coding for Interviews contains too many gifs. ->
gifs. many too contains Interviews for Coding
"""
def reverse_space_n(s):
rev = s.split(' ')
print ' '.join(rev[::-1])
Note return type on end of function:
func add(x int, y int) int {
return x + y
}
Can be shortened to:
func add(x, y int) int {
return x + y
}
@bootandy
bootandy / python_logging.yaml
Last active August 26, 2021 07:14
Sample logging config file for python logging module
# use with: logging.config.dictConfig(yaml.load(open('logging.yaml', 'r')))
# Formatters detailed here: http://docs.python.org/2/library/logging.html#logrecord-attributes
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
detail:
format: '%(asctime)s - %(levelname)s - File: %(filename)s - %(funcName)s() - Line: %(lineno)d - %(message)s'
@bootandy
bootandy / python_logging.py
Created May 8, 2013 14:58
sample logging - setting up logging to go to different files
import logging
import logging.handlers
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='myapp.log',
filemode='w')
@bootandy
bootandy / django_views.py
Created April 28, 2013 12:05
django class based views sample code.
# -------- models --------------
class CheckStart(models.Model):
check_start = models.OneToOneField(Checklist, related_name="start")
Registration = models.BooleanField(default=False)
Make_an_offer = models.BooleanField(default=False)
Offer_accepted = models.BooleanField(default=False)
class CheckStartForm(ModelForm):
@bootandy
bootandy / ajax_close_button.js
Created March 12, 2013 15:18
jquery. All elements of class 'closer' now fire off an AJAX request to attr 'ajax_href' and remove themselves from the DOM
$('.closer').click(function() {
var url = $(this).attr('ajax_href');
$.ajax({
type: "POST",
url: url,
});
$(this).parent().fadeOut('fast', function() {
console.log('Im gone);
@bootandy
bootandy / tfl_email_costs.py
Last active December 14, 2015 09:19
python - logs into email and prints your oyster card costs per month by looking for the 'Oyster card topped up' messages in your TFL_MAILBOX email folder
import imaplib
import email
from pprint import pprint
import re
import collections
from email.Iterators import typed_subpart_iterator
""" Assumes you have TFL email you each time your oyster auto top up occurs """
@bootandy
bootandy / test_celery.py
Created February 6, 2013 18:18
python. monkey patch a celery method so we can test it
class TestEmailer(unittest.TestCase):
def setUp(self):
super(TestEmailer, self).setUp()
self.applied_tasks = []
self.original_celery = celery_jobs.post_url.delay
def new_apply_async(*args):
self.applied_tasks.append(args)
@bootandy
bootandy / tornado_linkedin.py
Created January 17, 2013 12:04
LinkedIn login for Tornado.
from tornado import httpclient
from tornado import auth
from tornado import httputil
from tornado import escape
import urllib
#from tornado.httputil import url_concat