Skip to content

Instantly share code, notes, and snippets.

@sgammon
Created November 7, 2011 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgammon/1345982 to your computer and use it in GitHub Desktop.
Save sgammon/1345982 to your computer and use it in GitHub Desktop.
Example map/reduce mapper fn
import os
import logging
from mapreduce import context
from mapreduce import shuffler
from mapreduce import operation
from mapreduce import mapreduce_pipeline
from google.appengine.ext import db
from google.appengine.api import files
from google.appengine.ext import blobstore
def map_invite_to_export(invite):
''' Yield the proper CSV line for an AccountInvite, attached to the corresponding email address. '''
if not hasattr(invite, 'email') or not hasattr(invite, 'token'):
logging.error('Invite at key "'+str(invite.key())+'" is missing a token or email. Cannot dump to CSV. Skipping record.')
else:
logging.info('Examining invite at key '+str(invite.key())+' with target email '+invite.email+'.')
yield ",".join(map(lambda x: '"%s"' % str(x), ## Output values to CSV
[
invite.firstName, ## "John"
invite.lastName, ## "Doe"
",".join(map(lambda x: x.name, [k for k in filter(lambda y: y is not None and True or False, db.get(invite.locations))])), ## "\"John's Bar\",\"Jane's Bar\""
invite.email, ## "john@johnsbar.com"
invite.created,
invite.used,
"".join([ ## "http://<serverhost>/account/invite/claim?token=<token>&source=eminvite"
"http://",
os.environ.get('SERVER_HOST', 'packerseverywhere.appspot.com'),
"/",
"/".join(['account', 'invite', 'claim']),
"?",
"&".join(map(lambda x: "=".join(x), [('t', invite.token), ('source', 'eminvite')]))
])
]
))+"\n"
def export_invite_to_csv(email, csvline):
''' Export mapped invites to a CSV file in the blobstore. '''
logging.info('AccountInvite for '+str(email)+': '+str(csvline))
yield str(csvline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment