<commit_before># -*- coding: utf-8 -*-
import os
import datetime
from random import random
from hashlib import md5
from contextlib import contextmanager

import mutagen
import dateutil.parser


def randstr(length=None):
    """ Generates a random string of the given length. """

    if length < 1:
        return ''

    digest = md5(str(random())).hexdigest()

    if length:
        return digest[:length]

    return digest


def _import(class_path):
    """ Imports a module or class from a string in dot notation. """

    bits = class_path.split('.')
    mod_name = '.'.join(bits[:-1])
    cls_name = bits[-1]

    mod = __import__(mod_name, None, None, cls_name)

    return getattr(mod, cls_name)


@contextmanager
def ignored(*exceptions):
    """Context manager that ignores all of the specified exceptions. This will
    be in the standard library starting with Python 3.4."""
    try:
        yield
    except exceptions:
        pass


class MetadataManager(object):
    """A format-agnostic metadata wrapper around Mutagen.

    This makes reading/writing audio metadata easy across all possible audio
    formats by using properties for the different keys.
<commit_msg>Add print_traceback option to shiva.utils.ignored<commit_after># -*- coding: utf-8 -*-
import os
import datetime
from random import random
from hashlib import md5
from contextlib import contextmanager
import traceback

import mutagen
import dateutil.parser


def randstr(length=None):
    """ Generates a random string of the given length. """

    if length < 1:
        return ''

    digest = md5(str(random())).hexdigest()

    if length:
        return digest[:length]

    return digest


def _import(class_path):
    """ Imports a module or class from a string in dot notation. """

    bits = class_path.split('.')
    mod_name = '.'.join(bits[:-1])
    cls_name = bits[-1]

    mod = __import__(mod_name, None, None, cls_name)

    return getattr(mod, cls_name)


@contextmanager
def ignored(*exceptions, print_traceback=False):
    """Context manager that ignores all of the specified exceptions. This will
    be in the standard library starting with Python 3.4."""
    try:
        yield
    except exceptions:
        if print_traceback:
            print(traceback.format_exc())


class MetadataManager(object):
    """A format-agnostic metadata wrapper around Mutagen.

    This makes reading/writing audio metadata easy across all possible audio
    formats by using properties for the different keys.

{'fraction_non_alphanumeric': 0.09109069117059529, 'fraction_numerical': 0.006392329204954055, 'mean_word_length': 3.2512733446519526, 'pattern_counts': {'":': 0, '<': 5, '<?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': '295240', 'n_tokens_mistral': 707, 'n_tokens_neox': 682, 'n_words': 261}