<commit_before># -*- coding: utf-8 -*-

#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


from delimiter import exceptions


class UpperBoundProcessor(object):
    """Processes a limit given some upper bound."""

    def create(self, bound):
        return {
            'consumed': 0,
            'bound': bound,
        }

    def decode(self, details):
        return details

    def update(self, details, bound):
        details = details.copy()
        details['bound'] = bound
        return details

    def process(self, details, amount):
        consumed = details['consumed']
        if consumed + amount > details['bound']:
            raise exceptions.OverLimitException
        else:
            details = details.copy()
            details['consumed'] = consumed + amount
            return details<commit_msg>Static methods and a nice view object and better message<commit_after># -*- coding: utf-8 -*-

#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections

from delimiter import exceptions


BoundedResource = collections.namedtuple('BoundedResource',
                                         ['consumed', 'bound'])

class UpperBoundProcessor(object):
    """Processes a limit given some upper bound."""

    @staticmethod
    def create(limit):
        """Given some limit, turn it into a *internal* details dict."""
        return {
            'consumed': 0,
            'bound': limit,
        }

    @staticmethod
    def decode(details):
        """Turn a internal details dict into a user-viewable one."""
        return BoundedResource(details['consumed'], details['bound'])

    @staticmethod
    def update(details, limit):
        """Given internal details dict update it with the given limit."""
        details = details.copy()
        details['bound'] = limit
        return details

    @staticmethod
    def process(details, amount):
        """Given internal details dict process the amount given (or die).

        Updates (and returns) the internal details dict if
        successful (otherwise raises some exception).
        """
        consumed = details['consumed']
        if consumed + amount > details['bound']:
            raise exceptions.OverLimitException(
                "Limit of '%s' can not be passed" % details['bound'])
        else:
            details = details.copy()
            details['consumed'] = consumed + amount
            return details
{'fraction_non_alphanumeric': 0.0904899135446686, 'fraction_numerical': 0.00345821325648415, 'mean_word_length': 2.506060606060606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4191060', 'n_tokens_mistral': 841, 'n_tokens_neox': 827, 'n_words': 349}