<commit_before>""" Handle most tasks related to DynamoDB interaction """
import time
import sys

from boto import dynamodb2
from boto.dynamodb2.table import Table
from boto.exception import DynamoDBResponseError, JSONResponseError

from dynamic_dynamodb.log_handler import LOGGER as logger
from dynamic_dynamodb.config_handler import CONFIGURATION as configuration


def __get_connection_dynamodb(retries=3):
    """ Ensure connection to DynamoDB

    :type retries: int
    :param retries: Number of times to retry to connect to DynamoDB
    """
    connected = False
    while not connected:
        logger.debug('Connecting to DynamoDB in {0}'.format(
            configuration['global']['region']))
        try:
            if (configuration['global']['aws_access_key_id'] and
                    configuration['global']['aws_secret_access_key']):
                connection = dynamodb2.connect_to_region(
                    configuration['global']['region'],
                    aws_access_key_id=
                    configuration['global']['aws_access_key_id'],
                    aws_secret_access_key=
                    configuration['global']['aws_secret_access_key'])
            else:
                connection = dynamodb2.connect_to_region(
                    configuration['global']['region'])
            connected = True
            logger.debug('Connected to DynamoDB in {0}'.format(
                configuration['global']['region']))

        except Exception as err:
            logger.error('Failed to connect to DynamoDB: {0}'.format(err))
            if retries == 0:
                logger.error(
                    'Please report an issue at: '
                    'https://github.com/sebdah/dynamic-dynamodb/issues')
                raise
            else:
                logger.error('Retrying in 5 seconds')
                retries -= 1
                time.sleep(5)

    return connection


def describe_table(table_name):
    """ Return table details

    :type table_name: str
    :param table_name: Name of the DynamoDB table
    :returns: dict
    """
    return DYNAMODB_CONNECTION.describe_table(table_name)


def get_table(table_name):
    """ Return the DynamoDB table

    :type table_name: str
    :param table_name: Name of the DynamoDB table
    :returns: boto.dynamodb.table.Table
    """
    try:
        table = Table(table_name, connection=DYNAMODB_CONNECTION)
    except DynamoDBResponseError as error:
        dynamodb_error = error.body['__type'].rsplit('#', 1)[1]<commit_msg>Now catching if the DynamoDB connection is None<commit_after>""" Handle most tasks related to DynamoDB interaction """
import time
import sys

from boto import dynamodb2
from boto.dynamodb2.table import Table
from boto.exception import DynamoDBResponseError, JSONResponseError

from dynamic_dynamodb.log_handler import LOGGER as logger
from dynamic_dynamodb.config_handler import CONFIGURATION as configuration


def __get_connection_dynamodb(retries=3):
    """ Ensure connection to DynamoDB

    :type retries: int
    :param retries: Number of times to retry to connect to DynamoDB
    """
    connected = False
    while not connected:
        logger.debug('Connecting to DynamoDB in {0}'.format(
            configuration['global']['region']))

        if (configuration['global']['aws_access_key_id'] and
                configuration['global']['aws_secret_access_key']):
            connection = dynamodb2.connect_to_region(
                configuration['global']['region'],
                aws_access_key_id=
                configuration['global']['aws_access_key_id'],
                aws_secret_access_key=
                configuration['global']['aws_secret_access_key'])
        else:
            connection = dynamodb2.connect_to_region(
                configuration['global']['region'])

        if connection:
            connected = True
            logger.debug('Connected to DynamoDB in {0}'.format(
                configuration['global']['region']))

        logger.error(
            'Failed to connect to DynamoDB in region {0}'.format(
                configuration['global']['region']))

        if retries == 0:
            logger.error(
                'Please report an issue at: '
                'https://github.com/sebdah/dynamic-dynamodb/issues')
            raise
        else:
            logger.error('Retrying in 5 seconds')
            retries -= 1
            time.sleep(5)

    return connection


def describe_table(table_name):
    """ Return table details

    :type table_name: str
    :param table_name: Name of the DynamoDB table
    :returns: dict
    """
    return DYNAMODB_CONNECTION.describe_table(table_name)


def get_table(table_name):
    """ Return the DynamoDB table

    :type table_name: str
    :param table_name: Name of the DynamoDB table
    :returns: boto.dynamodb.table.Table
    """
    try:
        table = Table(table_name, connection=DYNAMODB_CONNECTION)
    except DynamoDBResponseError as error:
        dynamodb_error = error.body['__type'].rsplit('#', 1)[1]
{'fraction_non_alphanumeric': 0.09322536045822635, 'fraction_numerical': 0.005530317993284614, 'mean_word_length': 2.482806052269601, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4506501', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1328, 'n_words': 349}