Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Third party not pure-python

  • editdistance is a Levenshtein distance module written in C++ and Cython
  • See
import json
import sys

sys.path.insert(0, 'pypi')
import editdistance

def lambda_handler(event, context):
    if 'queryStringParameters' not in event:
        return {
            'statusCode': 500,
            'headers': { 'Content-Type': 'application/json' },
            'body': json.dumps({ 'error': 'Missing queryStringParameters' })
        }
        
    
    if event['queryStringParameters'] == None or 'a' not in event['queryStringParameters'] or 'b' not in event['queryStringParameters']:
        return {
            'statusCode': 400,
            'headers': { 'Content-Type': 'application/json' },
            'body': json.dumps({ 'error': 'Missing field' })
        }

    distance = editdistance.eval(event['queryStringParameters']['a'], event['queryStringParameters']['b'])

    return {
        'statusCode': 200,
        'headers': { 'Content-Type': 'application/json' },
        'body': json.dumps({
            'a' : event['queryStringParameters']['a'],
            'b' : event['queryStringParameters']['b'],
            'editdistance': distance,
        })
    }

if __name__ == '__main__':
    print(lambda_handler({
       'queryStringParameters': {
          'a': 'xyz',
          'b': 'xrp',
       }
    }, {}))

{% embed include file="src/examples/app_editdistance/requirements.txt)

  • Needs a linux box either locally or on Amazon AWS.