<commit_before># coding: utf-8

from __future__ import unicode_literals
from hashlib import sha1
from time import time

from django.db import connections
from django.db.models.sql import Query
from django.db.models.sql.where import ExtraWhere, SubqueryConstraint
from django.utils.module_loading import import_string
from django.utils.six import text_type, binary_type

from .settings import cachalot_settings
from .signals import post_invalidation


class UncachableQuery(Exception):
    pass


CACHABLE_PARAM_TYPES = (bool, int, binary_type, text_type, type(None))

def get_query_cache_key(compiler):
    """
    Generates a cache key from a SQLCompiler.

    This cache key is specific to the SQL query and its context
    (which database is used).  The same query in the same context
    (= the same database) must generate the same cache key.

    :arg compiler: A SQLCompiler that will generate the SQL query
    :type compiler: django.db.models.sql.compiler.SQLCompiler
    :return: A cache key
    :rtype: str
    """
    sql, params = compiler.as_sql()
    if any(p.__class__ not in CACHABLE_PARAM_TYPES for p in params):
        raise UncachableQuery
    cache_key = '%s:%s:%s' % (compiler.using, sql, params)
    return sha1(cache_key.encode('utf-8')).hexdigest()


def get_table_cache_key(db_alias, table):
    """
    Generates a cache key from a SQL table.

    :arg db_alias: Alias of the used database<commit_msg>Mark datetime types as valid SQL param types (for psycopg2).<commit_after># coding: utf-8

from __future__ import unicode_literals
import datetime
from hashlib import sha1
from time import time

from django.db import connections
from django.db.models.sql import Query
from django.db.models.sql.where import ExtraWhere, SubqueryConstraint
from django.utils.module_loading import import_string
from django.utils.six import text_type, binary_type

from .settings import cachalot_settings
from .signals import post_invalidation


class UncachableQuery(Exception):
    pass


CACHABLE_PARAM_TYPES = (
    bool, int, binary_type, text_type, type(None),
    datetime.date, datetime.time, datetime.datetime, datetime.timedelta
)

def get_query_cache_key(compiler):
    """
    Generates a cache key from a SQLCompiler.

    This cache key is specific to the SQL query and its context
    (which database is used).  The same query in the same context
    (= the same database) must generate the same cache key.

    :arg compiler: A SQLCompiler that will generate the SQL query
    :type compiler: django.db.models.sql.compiler.SQLCompiler
    :return: A cache key
    :rtype: str
    """
    sql, params = compiler.as_sql()
    if any(p.__class__ not in CACHABLE_PARAM_TYPES for p in params):
        raise UncachableQuery
    cache_key = '%s:%s:%s' % (compiler.using, sql, params)
    return sha1(cache_key.encode('utf-8')).hexdigest()


def get_table_cache_key(db_alias, table):
    """
    Generates a cache key from a SQL table.

    :arg db_alias: Alias of the used database
{'fraction_non_alphanumeric': 0.07566666666666666, 'fraction_numerical': 0.003, 'mean_word_length': 4.010016694490818, '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': '574262', 'n_tokens_mistral': 901, 'n_tokens_neox': 890, 'n_words': 320}