<commit_before># To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

__author__ = "<NAME> <<EMAIL>>"
__date__ = "$Jun 4, 2014 11:10:55 AM$"

import copy

import h5py

import HDF5_serializers

# Need in order to have logging information no matter what.
import debugging_tools


# Get the logger
logger = debugging_tools.logging.getLogger(__name__)


class EmptyArrayRecorder(object):
    @debugging_tools.log_call(logger)
    def __init__(self):
        pass

    @debugging_tools.log_call(logger)
    def __nonzero__(self):
        return(False)

    # For forward compatibility with Python 3
    __bool__ = __nonzero__

    @debugging_tools.log_call(logger)
    def get(self, key, default=None):
        return(default)

    @debugging_tools.log_call(logger)
    def __contains__(self, key):
        return(False)

    @debugging_tools.log_call(logger)
    def __getitem__(self, key):
        raise(KeyError("unable to open object (Symbol table: Can't open object " + repr(key) + ")"))

    @debugging_tools.log_call(logger)
    def __call__(self, key, value):
        # Exception will be thrown if value is empty or if key already exists (as intended).
        if value.size:
            pass
        else:
            raise Exception("The array provided for output by the name: \"" + key + "\" is empty.")


class HDF5ArrayRecorder(object):
    @debugging_tools.log_call(logger)
    def __init__(self, hdf5_handle, overwrite_dataset = False):
        self.hdf5_handle = hdf5_handle
        self.overwrite_dataset = overwrite_dataset

    @debugging_tools.log_call(logger)
    def __nonzero__(self):
        return(True)

    # For forward compatibility with Python 3
    __bool__ = __nonzero__

    @debugging_tools.log_call(logger)
    def get(self, key, default=None):
        value = default

        try:
            value = self.__getitem__(key)
        except KeyError:
            pass

        return(value)

    @debugging_tools.log_call(logger)
    def __contains__(self, key):
        return(key in self.hdf5_handle)

    @debugging_tools.log_call(logger)
    def __getitem__(self, key):
        try:
            return(HDF5_serializers.read_numpy_structured_array_from_HDF5(self.hdf5_handle, key))
        except:
            raise(KeyError("unable to open object (Symbol table: Can't open object " + repr(key) + " in " + repr(self.hdf5_handle) + ")"))

    @debugging_tools.log_call(logger)
    def __call__(self, key, value):
        # Attempt to create a dataset in self.hdf5_handle named key with value and do not overwrite.
        # Exception will be thrown if value is empty or if key already exists (as intended).
        if value.size:
            HDF5_serializers.write_numpy_structured_array_to_HDF5(self.hdf5_handle,
                                                                  key,
                                                                  value,
                                                                  overwrite = self.overwrite_dataset)
            self.hdf5_handle.file.flush()
        else:
            raise Exception("The array provided for output by the name: \"" + key + "\" is empty.")


@debugging_tools.log_call(logger)
def generate_HDF5_array_recorder(hdf5_handle, group_name = "", enable = True, overwrite_group = False, allow_overwrite_dataset = False):
    """
        Generates a function used for writing arrays (structured or otherwise)
        to a group in an HDF5 file.
        
        Args:
            hdf5_handle:        The HDF5 file group to place the debug contents into.

            group_name:         The name of the group within hdf5_handle to save the contents to.<commit_msg>HDF5_recorder: Changed generic Exception to ValueError in __call__ methods of the classes EmptyArrayRecorder and HDF5ArrayRecorder.<commit_after># To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

__author__ = "<NAME> <<EMAIL>>"
__date__ = "$Jun 4, 2014 11:10:55 AM$"

import copy

import h5py

import HDF5_serializers

# Need in order to have logging information no matter what.
import debugging_tools


# Get the logger
logger = debugging_tools.logging.getLogger(__name__)


class EmptyArrayRecorder(object):
    @debugging_tools.log_call(logger)
    def __init__(self):
        pass

    @debugging_tools.log_call(logger)
    def __nonzero__(self):
        return(False)

    # For forward compatibility with Python 3
    __bool__ = __nonzero__

    @debugging_tools.log_call(logger)
    def get(self, key, default=None):
        return(default)

    @debugging_tools.log_call(logger)
    def __contains__(self, key):
        return(False)

    @debugging_tools.log_call(logger)
    def __getitem__(self, key):
        raise(KeyError("unable to open object (Symbol table: Can't open object " + repr(key) + ")"))

    @debugging_tools.log_call(logger)
    def __call__(self, key, value):
        # Exception will be thrown if value is empty or if key already exists (as intended).
        if value.size:
            pass
        else:
            raise ValueError("The array provided for output by the name: \"" + key + "\" is empty.")


class HDF5ArrayRecorder(object):
    @debugging_tools.log_call(logger)
    def __init__(self, hdf5_handle, overwrite_dataset = False):
        self.hdf5_handle = hdf5_handle
        self.overwrite_dataset = overwrite_dataset

    @debugging_tools.log_call(logger)
    def __nonzero__(self):
        return(True)

    # For forward compatibility with Python 3
    __bool__ = __nonzero__

    @debugging_tools.log_call(logger)
    def get(self, key, default=None):
        value = default

        try:
            value = self.__getitem__(key)
        except KeyError:
            pass

        return(value)

    @debugging_tools.log_call(logger)
    def __contains__(self, key):
        return(key in self.hdf5_handle)

    @debugging_tools.log_call(logger)
    def __getitem__(self, key):
        try:
            return(HDF5_serializers.read_numpy_structured_array_from_HDF5(self.hdf5_handle, key))
        except:
            raise(KeyError("unable to open object (Symbol table: Can't open object " + repr(key) + " in " + repr(self.hdf5_handle) + ")"))

    @debugging_tools.log_call(logger)
    def __call__(self, key, value):
        # Attempt to create a dataset in self.hdf5_handle named key with value and do not overwrite.
        # Exception will be thrown if value is empty or if key already exists (as intended).
        if value.size:
            HDF5_serializers.write_numpy_structured_array_to_HDF5(self.hdf5_handle,
                                                                  key,
                                                                  value,
                                                                  overwrite = self.overwrite_dataset)
            self.hdf5_handle.file.flush()
        else:
            raise ValueError("The array provided for output by the name: \"" + key + "\" is empty.")


@debugging_tools.log_call(logger)
def generate_HDF5_array_recorder(hdf5_handle, group_name = "", enable = True, overwrite_group = False, allow_overwrite_dataset = False):
    """
        Generates a function used for writing arrays (structured or otherwise)
        to a group in an HDF5 file.
        
        Args:
            hdf5_handle:        The HDF5 file group to place the debug contents into.

            group_name:         The name of the group within hdf5_handle to save the contents to.
{'fraction_non_alphanumeric': 0.07988550611501431, 'fraction_numerical': 0.00936768149882904, 'mean_word_length': 2.4657348963029757, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5289352', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2159, 'n_words': 635}