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

from f6a_tw_backend.constants import *
from f6a_tw_backend.django_constants import *

from django.shortcuts import render
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response

from f6a_tw_backend import cfg
from f6a_tw_backend import util


class DefaultView(APIView):
    def get(self, request, format=None):
        return Response({"success": True}, status=status.HTTP_200_OK)

    def post(self, request, format=None):
        data = _get_data(request)

        return Response({"success": True, "data": data}, status=status.HTTP_200_OK)


class QueryView(APIView):
    def get(self, request, format=None):
        data = request.GET.dict()
        the_str = data.get('str', '')
        the_limit = util._int(data.get('limit', 0))

        the_list = _query_data(the_str, the_limit)

        return Response({"success": True, "data": the_list}, status=status.HTTP_200_OK)


class DefaultDetailView(APIView):
    def get(self, request, pk, format=None):
        return Response({"success": True, "pk": pk}, status=status.HTTP_200_OK)

    def post(self, request, pk, format=None):
        data = _get_data(request)

        return Response({"success": True, "pk": pk, "data": data}, status=status.HTTP_200_OK)


class PathView(APIView):
    def get(self, request, path, format=None):
        return Response({"success": True, "path": path}, status=status.HTTP_200_OK)

    def post(self, request, path, format=None):
        data = _get_data(request)

        return Response({"success": True, "path": path, "data": data}, status=status.HTTP_200_OK)


def _query_data(the_str, the_limit):
    if the_str.__class__.__name__ == 'unicode':
        the_str = the_str.encode('utf-8')
    cfg.logger.debug('the_str: (%s, %s), the_limit: (%s, %s)', the_str, the_str.__class__.__name__, the_limit, the_limit.__class__.__name__)

    db_results = []
    query = {} if not the_str else {"$regex": "/" + the_str + "/"}
    for idx in ["name", "en_name", "indication", "permit"]:
        each_db_result_it = util.db_find_it('f6a_tw_backend', {idx: query})
        if the_limit:
            each_db_result_it = each_db_result_it.limit(the_limit)
        db_results += list(each_db_result_it)

    if the_limit:
        db_results = db_results[:the_limit]
<commit_msg>regex<commit_after># -*- coding: utf-8 -*-

from f6a_tw_backend.constants import *
from f6a_tw_backend.django_constants import *

from django.shortcuts import render
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response

from f6a_tw_backend import cfg
from f6a_tw_backend import util


class DefaultView(APIView):
    def get(self, request, format=None):
        return Response({"success": True}, status=status.HTTP_200_OK)

    def post(self, request, format=None):
        data = _get_data(request)

        return Response({"success": True, "data": data}, status=status.HTTP_200_OK)


class QueryView(APIView):
    def get(self, request, format=None):
        data = request.GET.dict()
        the_str = data.get('str', '')
        the_limit = util._int(data.get('limit', 0))

        the_list = _query_data(the_str, the_limit)

        return Response({"success": True, "data": the_list}, status=status.HTTP_200_OK)


class DefaultDetailView(APIView):
    def get(self, request, pk, format=None):
        return Response({"success": True, "pk": pk}, status=status.HTTP_200_OK)

    def post(self, request, pk, format=None):
        data = _get_data(request)

        return Response({"success": True, "pk": pk, "data": data}, status=status.HTTP_200_OK)


class PathView(APIView):
    def get(self, request, path, format=None):
        return Response({"success": True, "path": path}, status=status.HTTP_200_OK)

    def post(self, request, path, format=None):
        data = _get_data(request)

        return Response({"success": True, "path": path, "data": data}, status=status.HTTP_200_OK)


def _query_data(the_str, the_limit):
    if the_str.__class__.__name__ == 'unicode':
        the_str = the_str.encode('utf-8')
    cfg.logger.debug('the_str: (%s, %s), the_limit: (%s, %s)', the_str, the_str.__class__.__name__, the_limit, the_limit.__class__.__name__)

    db_results = []
    query = {} if not the_str else {"$regex": the_str}
    for idx in ["name", "en_name", "indication", "permit"]:
        each_db_result_it = util.db_find_it('f6a_tw_backend', {idx: query})
        if the_limit:
            each_db_result_it = each_db_result_it.limit(the_limit)
        db_results += list(each_db_result_it)

    if the_limit:
        db_results = db_results[:the_limit]

{'fraction_non_alphanumeric': 0.13381478334749364, 'fraction_numerical': 0.01231945624468989, 'mean_word_length': 3.9205851619644725, 'pattern_counts': {'":': 32, '<': 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': '7193108', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1545, 'n_words': 360}