<commit_before>from __future__ import print_function
import boto3
import json
import uuid
from decimal import *

# Monkey patch Decimal's default Context to allow
# inexact and rounded representation of floats
from boto3.dynamodb.types import DYNAMODB_CONTEXT
# Inhibit Inexact Exceptions
DYNAMODB_CONTEXT.traps[Inexact] = 0

# Inhibit Rounded Exceptions
DYNAMODB_CONTEXT.traps[Rounded] = 0


def lambda_handler(event, context):
    dynamo = boto3.resource('dynamodb').Table('senseware')

    # When testing lambda the packet is the request body is the event
    # but when testing through the Api Gateway it is wrapped in an additional
    # request dictionary
    try:
        packet = json.loads(event['body'])
    except KeyError:
        packet = event

    for record in packet['data']:
        # DynamoDb wants decimals, not floats
        record['value'] = Decimal(record['value'])
        record['raw'] = Decimal(record['raw'])

        # Add the streams meta data to every record (optional)
        record['sn'] = packet['sn']
        record['site'] = packet['site']
        record['location'] = packet['location']
        record['mod'] = packet['mod']
        record['sid'] = packet['sid']
        record['type'] = packet['type']
        record['cid'] = packet['cid']
        record['name'] = packet['name']
        record['unit'] = packet['unit']

        # Give this record an ID
        record['partition'] = str(uuid.uuid4())

        # Put it into the database
        dynamo.put_item(
            TableName='senseware',
            Item=record
        )

    # Return a Success Message
    return {
      "statusCode": 200,
      "body": ""
    }<commit_msg>Instead of inserting empty string remove the key from the record for missing values<commit_after>from __future__ import print_function
import boto3
import json
import uuid
from decimal import *

# Monkey patch Decimal's default Context to allow
# inexact and rounded representation of floats
from boto3.dynamodb.types import DYNAMODB_CONTEXT
# Inhibit Inexact Exceptions
DYNAMODB_CONTEXT.traps[Inexact] = 0

# Inhibit Rounded Exceptions
DYNAMODB_CONTEXT.traps[Rounded] = 0


def lambda_handler(event, context):
    dynamo = boto3.resource('dynamodb').Table('senseware')

    # When testing lambda the packet is the request body is the event
    # but when testing through the Api Gateway it is wrapped in an additional
    # request dictionary
    try:
        packet = json.loads(event['body'])
    except KeyError:
        packet = event

    for record in packet['data']:
        for key in ['value', 'raw']:
            if record[key] != "" and record[key] != None:
                # DynamoDb wants decimals, not floats
                record[key] = Decimal(record[key])
            else:
                del record[key]

        for key in ['pkt', 'ts']:
            if record[key] == "" or record[key] == None:
                del record[key]

        # Add the streams meta data to every record (optional)
        for key in ['sn', 'site', 'location', 'mod', 'sid', 'type', 'cid', 'name', 'unit']:
            if packet[key] != "" and packet[key] != None:
                # DynamoDb wants decimals, not floats
                record[key] = packet[key]

        # Give this record an ID
        record['partition'] = str(uuid.uuid4())

        # Put it into the database
        dynamo.put_item(
            TableName='senseware',
            Item=record
        )

    # Return a Success Message
    return {
      "statusCode": 200,
      "body": ""
    }
{'fraction_non_alphanumeric': 0.10740531373657433, 'fraction_numerical': 0.005087620124364048, 'mean_word_length': 2.622313203684749, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6842957', 'n_tokens_mistral': 1044, 'n_tokens_neox': 994, 'n_words': 326}