<commit_before>import logging
import json
import tempfile
import shutil
from contextlib import contextmanager

from archivekit import Resource
from loadkit.util import json_default, json_hook

log = logging.getLogger(__name__)


class Table(Resource):
    """ The table holds a temporary, cleaned representation of the
    package resource (as a newline-separated set of JSON
    documents). """

    GROUP = 'tables'

    @contextmanager
    def store(self):
        """ Create a context manager to store records in the cleaned
        table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:

            def write(o):
                line = json.dumps(o, default=json_default)
                return output.write(line + '\n')

            yield write

            output.seek(0)
            log.info("Uploading generated table (%r)...", self._obj)
            self.save_file(output.name, destructive=True)
        finally:
            try:
                output.close()
            except:
                pass

    def records(self):
        """ Get each record that has been stored in the table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:
            log.info("Loading table from (%r)...", self._obj)
            shutil.copyfileobj(self.fh(), output)
            output.seek(0)

            for line in output.file:
                yield json.loads(line, object_hook=json_hook)

        finally:
            try:
                output.close()
            except:
                pass

    def __repr__(self):
        return '<Table(%r)>' % self.name
<commit_msg>nicer logging<commit_after>import logging
import json
import tempfile
import shutil
from contextlib import contextmanager

from archivekit import Resource
from loadkit.util import json_default, json_hook

log = logging.getLogger(__name__)


class Table(Resource):
    """ The table holds a temporary, cleaned representation of the
    package resource (as a newline-separated set of JSON
    documents). """

    GROUP = 'tables'

    @contextmanager
    def store(self):
        """ Create a context manager to store records in the cleaned
        table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:

            def write(o):
                line = json.dumps(o, default=json_default)
                return output.write(line + '\n')

            yield write

            output.seek(0)
            log.info("Uploading generated table (%s)...", self._obj)
            self.save_file(output.name, destructive=True)
        finally:
            try:
                output.close()
            except:
                pass

    def records(self):
        """ Get each record that has been stored in the table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:
            log.info("Loading table from (%s)...", self._obj)
            shutil.copyfileobj(self.fh(), output)
            output.seek(0)

            for line in output.file:
                yield json.loads(line, object_hook=json_hook)

        finally:
            try:
                output.close()
            except:
                pass

    def __repr__(self):
        return '<Table(%r)>' % self.name

{'fraction_non_alphanumeric': 0.09620098039215687, 'fraction_numerical': 0.0012254901960784314, 'mean_word_length': 1.8842756183745584, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1741861', 'n_tokens_mistral': 881, 'n_tokens_neox': 838, 'n_words': 251}