<commit_before>import collections
import hashlib
import logging
import time

from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS
from django.utils import six

from cacheback.utils import enqueue_task, get_cache, get_job_class


logger = logging.getLogger('cacheback')

MEMCACHE_MAX_EXPIRATION = 2592000


# Container for call args (which makes things simpler to pass around)
Call = collections.namedtuple("Call", ['args', 'kwargs'])


def to_bytestring(value):
    """
    Encode a string as a UTF8 bytestring.  This function could be passed a
    bytestring or unicode string so must distinguish between the two.
    """
    if isinstance(value, six.text_type):
        return value.encode('utf8')
    if isinstance(value, six.binary_type):
        return value
    if six.PY2:
        return str(value)
    return bytes(str(value), 'utf8')


class Job(object):
    """
    A cached read job.

    This is the core class for the package which is intended to be subclassed
    to allow the caching behaviour to be customised.
    """
    # All items are stored in memcache as a tuple (expiry, data).  We don't use
    # the TTL functionality within memcache but implement on own.  If the
    # expiry value is None, this indicates that there is already a job created
    # for refreshing this item.

    #: Default cache lifetime is 10 minutes.  After this time, the result will
    #: be considered stale and requests will trigger a job to refresh it.
    lifetime = 600

    #: Timeout period during which no new tasks will be created for a<commit_msg>Refs #57 - added handling of Django Model instances out of the box<commit_after>import collections
import hashlib
import logging
import time

from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS
from django.db.models import Model as DjangoModel
from django.utils import six

from cacheback.utils import enqueue_task, get_cache, get_job_class


logger = logging.getLogger('cacheback')

MEMCACHE_MAX_EXPIRATION = 2592000


# Container for call args (which makes things simpler to pass around)
Call = collections.namedtuple("Call", ['args', 'kwargs'])


def to_bytestring(value):
    """
    Encode an object as a UTF8 bytestring.  This function could be passed a
    bytestring, unicode string or object so must distinguish between them.

    :param value: object we want to transform into a bytestring
    :returns: a bytestring
    """
    if isinstance(value, DjangoModel):
        return ('%s:%s' % (value.__class__, hash(value))).encode('utf-8')
    if isinstance(value, six.text_type):
        return value.encode('utf8')
    if isinstance(value, six.binary_type):
        return value
    if six.PY2:
        return str(value)
    return bytes(str(value), 'utf8')


class Job(object):
    """
    A cached read job.

    This is the core class for the package which is intended to be subclassed
    to allow the caching behaviour to be customised.
    """
    # All items are stored in memcache as a tuple (expiry, data).  We don't use
    # the TTL functionality within memcache but implement on own.  If the
    # expiry value is None, this indicates that there is already a job created
    # for refreshing this item.

    #: Default cache lifetime is 10 minutes.  After this time, the result will
    #: be considered stale and requests will trigger a job to refresh it.
    lifetime = 600

    #: Timeout period during which no new tasks will be created for a
{'fraction_non_alphanumeric': 0.06917336394948335, 'fraction_numerical': 0.010045924225028703, 'mean_word_length': 3.473684210526316, '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': '7103325', 'n_tokens_mistral': 1002, 'n_tokens_neox': 933, 'n_words': 426}