My cream cheese is made with renewable energy
And you can see how at http://creamcheese.com
And you can see how at http://creamcheese.com
I thought I had a memory leak in a program I was writing for my wife today and found this awesome answer on StackOverflow. I turned it into a decorator to I could detect memory leaks more easily in the future. The code for the decorator is below.
def report_memory_leaked(f): """ Counts objects allocated in memory before and after the decorated function and reports the results. If you decorate a function like @report_memory_leaked def myfunc(): pass # do stuff It will report objects by type that were leaked or created. """ from collections import defaultdict from gc import get_objects import logging def obj_counting_wrapper(*args, **kwargs): before=defaultdict(int) for i in get_objects():before[type(i)]+=1 retval = f(*args, **kwargs) after=defaultdict(int) for i in get_objects():after[type(i)]+=1 leaked_or_returned = [(k,after[k]-before[k]) for k in
after if after[k]-before[k]]
if leaked_or_returned: logging.debug('Leaked or returned:\n%r' % leaked_or_returned) return retval return obj_counting_wrapperI’m trying out Postmarkapp in order to improve my email deliverability for a business I own. The API is dead simple and it only took about an hour to get integrated into my existing code. Below is a simple Python class that is a subclass of GAE’s EmailMessage class and allows you to send mail via Postmark instead of GAE’s built-in mail service.
Obviously, you need your own API key from Postmark. But, using this class you could easily just make a few changes to your existing appengine code and call, e.g. msg.pmsend() instead of msg.send() assuming you’re already using the EmailMessage class to build your outgoing emails. Oh…there’s no error handling yet obviously. Caveat emptor.
from google.appengine.api import mail from google.appengine.api import urlfetch try: import json except ImportError: try: import simplejson as json except ImportError: from django.utils import simplejson as json class PMEmailMessage(mail.EmailMessage): """ PMEmailMessage subclass that enables you to call the pmsend() method in order to send via Postmark instead of using GAE's built-in mail service. """ PM_API_KEY = 'PUTYOURKEYHERE' PM_URL = 'http://api.postmarkapp.com/email' @classmethod def get_pm_headers(cls, test=False): """docstring for get_pm_headers""" d = { 'Accept' : 'application/json', "Content-Type" : "application/json", } if test: k = 'POSTMARK_API_TEST' else: k = cls.PM_API_KEY d['X-Postmark-Server-Token'] = k return d @property def postmark_dict(self): """docstring for postmark_dict""" param_mapping = { 'Headers' : 'headers', 'ReplyTo' : 'reply_to', 'Tag' : 'tag', 'To' : 'to', 'From' : 'sender', 'Cc' : 'cc', 'Bcc' : 'bcc', 'Subject' : 'subject', 'HtmlBody' : 'html', 'TextBody' : 'body', } d = {} for k,v in param_mapping.iteritems(): val = getattr(self, v, None) if val: d[k] = val return d @property def postmark_json(self): """docstring for postmark_json""" return json.dumps(self.postmark_dict) def pmsend(self): """docstring for pmsend""" response = urlfetch.fetch(self.PM_URL, method=urlfetch.POST,
payload=self.postmark_json, headers=self.get_pm_headers())
return response