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


log = logging.getLogger(__name__)


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

    TABLE_RESOURCE = 'table.json'
    
    def __init__(self, package):
        self.package = package
        self.key = package.resource(self.TABLE_RESOURCE)

    @contextmanager
    def store(self):
        """ Create a context manager to store records in the cleaned
        table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:
            yield lambda obj: output.write(json.dumps(obj) + '\n')

            output.seek(0)
            log.info("Uploading generated table to S3 (%r)...", self.key)
            self.key.set_contents_from_file(output)
        finally:
            output.close()

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

            for line in output.file:
                yield json.loads(line)<commit_msg>Rename resource name constant. <commit_after>import json
import logging
import tempfile
from contextlib import contextmanager


log = logging.getLogger(__name__)


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

    RESOURCE = 'table.json'
    
    def __init__(self, package):
        self.package = package
        self.key = package.resource(self.RESOURCE)

    @contextmanager
    def store(self):
        """ Create a context manager to store records in the cleaned
        table. """
        output = tempfile.NamedTemporaryFile(suffix='.json')
        try:
            yield lambda obj: output.write(json.dumps(obj) + '\n')

            output.seek(0)
            log.info("Uploading generated table to S3 (%r)...", self.key)
            self.key.set_contents_from_file(output)
        finally:
            output.close()

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

            for line in output.file:
                yield json.loads(line)
{'fraction_non_alphanumeric': 0.10232383808095952, 'fraction_numerical': 0.0029985007496251873, 'mean_word_length': 2.2950617283950616, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5653300', 'n_tokens_mistral': 737, 'n_tokens_neox': 716, 'n_words': 211}