<commit_before>import os
import logging

try:
    from django.core.exceptions import ImproperlyConfigured
except ImportError:
    class ImproperlyConfigured(Exception):
        pass

def envar(key, ktype, default=None, verbose=False):
    """Looks for ENVIRONMENT VARIABLE named `key`;
    returns the value after coercing it to type `ktype`.

    Raises `ImproperlyConfigured` exception if environment
    does not have the variable name defined and `default`
    is set to None.

    If `verbose` is true, values loaded from the environment
    are echoed. This is useful to check which of the defaults
    are being overridden.

    Examples:
        envar('HELLO', str)  # Export HELLO="WORLD"
        envar('COUNT', int)  # Export COUNT=21
        enavr('DEBUG', bool)  # Export DEBUG="True"
    """
    value = os.getenv(key)

    # Hopeless situation
    if value is None and default is None:
        raise ImproperlyConfigured('Cannot load environment variable: %s' % (key))

    # Work with defaults, force through the typecast.
    if value is None:
        return ktype(default)

    # Not using defaults, value is being overridden
    if verbose is True:
        logging.warn("VARIABLE-OVERRIDE:{0}:{1} => {2}".format(key, default, value))

    # Booleans appear as text, convert to their natural form
    if ktype is bool:
        try:
            return {'true': True, 'false': False}[value.lower()]
        except:
            raise ImproperlyConfigured('Boolean variable types must have values: "True" or "False", got "%s"' % value)

    # Let Python work its magic
    return ktype(value)
<commit_msg>Improving reliability, ensure the variable can be evaluated or coerced into the type,  else raise exception.<commit_after>import os
import ast
import logging

try:
    from django.core.exceptions import ImproperlyConfigured
except ImportError:
    class ImproperlyConfigured(Exception):
        pass


def envar(key, ktype, default=None, verbose=False):
    """Looks for ENVIRONMENT VARIABLE named `key`;
    returns the value after coercing it to type `ktype`.

    Raises `ImproperlyConfigured` exception if environment
    does not have the variable name defined and `default`
    is set to None.

    If `verbose` is true, values loaded from the environment
    are echoed. This is useful to check which of the defaults
    are being overridden.

    Examples:
        envar('HELLO', str)  # Export HELLO="WORLD"
        envar('COUNT', int)  # Export COUNT=21
        enavr('DEBUG', bool)  # Export DEBUG="True"
    """
    value = os.getenv(key)
    # Hopeless situation
    if value is None and default is None:
        raise ImproperlyConfigured('Cannot load environment variable: %s' % (key))

    # Work with defaults, force through the typecast.
    if value is None:
        return convert_to_type(ktype, default)

    # Not using defaults, value is being overridden
    if verbose is True:
        logging.warn("VARIABLE-OVERRIDE:{0}:{1} => {2}".format(key, default, value))

    return convert_to_type(ktype, value)


def convert_to_type(ktype, value):
    try:
        evalue = ast.literal_eval(value)
        if ktype == type(evalue):
            return evalue
        else:
            raise ImproperlyConfigured('Value: {0} is not of type {1}'.format(evalue, ktype))
    except (ValueError, SyntaxError):
        try:
            if ktype == bool:
                try:
                    return {'true': True, 'false': False}[value.lower()]
                except:
                    raise ImproperlyConfigured('Boolean variable types must have values: "True" or "False", got "%s"' % value)
            return ktype(value)
        except:
            raise ImproperlyConfigured('Value: {0} is not of type {1}'.format(value, ktype))

{'fraction_non_alphanumeric': 0.09206686123640223, 'fraction_numerical': 0.0037145131334571503, 'mean_word_length': 2.8508682328907047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1300993', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1062, 'n_words': 391}