#!/env/bin/bash python from __future__ import print_function import json, re, urllib2 from collections import Counter from sets import Set print('Loading function') def lambda_handler(event, context): # extract phase, also make sure environment is sane requester_email = event['email'] if not requester_email: return 'Error: email field missing' r = urllib2.urlopen(event['URL']) j = None try: j = json.load(r)['data'] except Exception, e: return 'Error: data not found' # transform phase # define schema, this might be swapped for a schema call to SwaggerUI transformedData = { "april_emails": [ "string" ], "your_email": "string", "unique_emails": [ "string" ], "user_domain_counts": [ { "domain": 0 } ] } # define some temporary vars domain_re = re.compile(ur'@([\w]+[\w.]+[\w]+)') domain_count = Counter() unique_emails = Set() april_emails = Set() # only loop once, needs to be o^n complexity for n in j: if not n['email'] : continue # skip if email is missing email = str(n['email']).strip().lower() # normalize the data matchedDomain = re.search(domain_re, email) if matchedDomain: domain_count[matchedDomain.group()[1:]] += 1 if email: unique_emails.add(email) if not n['login_date'] : continue if int(n['login_date'].split('-')[1]) == 4: april_emails.add(email) # load phase # load the temp vars into the schema transformedData['your_email'] = event['email'] transformedData['unique_emails'] = list(unique_emails) transformedData['user_domain_counts'] = [{d:domain_count[d]} for d in domain_count if domain_count[d] > 1] # don't count if it's only 1 transformedData['april_emails'] = list(april_emails) # do some logging print(transformedData) # save/respond with data return transformedData
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question