<commit_before>import tree
import re


class Q(object):
    def __init__(self, query_stmt, *args, **kwargs):
        self.translated_stmt = self._format(query_stmt, *args, **kwargs) 

    def clause(self):
        return self.translated_stmt

    def _format(self, query_stmt, *args, **kwargs):
        matches = re.findall(r':[_a-zA-Z\d]+', query_stmt)
        if kwargs:
            if not all([(':%s' % k) in matches for k in kwargs]):
                raise ValueError
            query_stmt = self._format_with_kwargs(query_stmt, matches, **kwargs)
        elif args:
            if len(args) != len(matches):
                raise ValueError
            query_stmt = self._format_with_args(query_stmt, matches, *args)
        return query_stmt

    def _value_by_type(self, val):
        if type(val) in (basestring, str, unicode):
            val = '\'%s\'' % val
        elif type(val) in (int, long):
            val = '%d' % val
        elif type(val) in (float):
            val = '%f' % val
        else:
            raise ValueError
        return val

    def _format_with_args(self, query_stmt, matches, *args):
        for i in range(len(matches)):
            query_stmt = query_stmt.replace(matches[i], self._value_by_type(args[i])) 
        return query_stmt<commit_msg>handle None as null in query args<commit_after>import tree
import re


class Q(object):
    def __init__(self, query_stmt, *args, **kwargs):
        self.translated_stmt = self._format(query_stmt, *args, **kwargs) 

    def clause(self):
        return self.translated_stmt

    def _format(self, query_stmt, *args, **kwargs):
        matches = re.findall(r':[_a-zA-Z\d]+', query_stmt)
        if kwargs:
            if not all([(':%s' % k) in matches for k in kwargs]):
                raise ValueError
            query_stmt = self._format_with_kwargs(query_stmt, matches, **kwargs)
        elif args:
            if len(args) != len(matches):
                raise ValueError
            query_stmt = self._format_with_args(query_stmt, matches, *args)
        return query_stmt

    def _value_by_type(self, val):
        if val is None:
            val = 'null'
        elif type(val) in (basestring, str, unicode):
            val = '\'%s\'' % val
        elif type(val) in (int, long):
            val = '%d' % val
        elif type(val) in (float):
            val = '%f' % val
        else:
            raise ValueError
        return val

    def _format_with_args(self, query_stmt, matches, *args):
        for i in range(len(matches)):
            query_stmt = query_stmt.replace(matches[i], self._value_by_type(args[i])) 
        return query_stmt
{'fraction_non_alphanumeric': 0.11786928598413299, 'fraction_numerical': 0.0, 'mean_word_length': 2.1674641148325358, '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': '723273', 'n_tokens_mistral': 815, 'n_tokens_neox': 796, 'n_words': 219}