<commit_before># -*- coding: utf-8 -*-
import hashlib
import json
import tempfile
import zipfile
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.core.servers.basehttp import FileWrapper
from django.http import HttpResponse


def verify(cond):
    if not cond:
        raise PermissionDenied


def sign(text):
    return hashlib.md5(text + settings.SECRET_KEY).hexdigest()


def pack(data):
    text = json.dumps(data)
    return (text, sign(text))


def unpack(text, sig):
    verify(sign(text) == sig)
    return json.loads(text)


def plain_text(name, contents, mimetype='text/plain'):
    """
    Returns a response that downloads a plain text file with the given name and
    contents.
    """
    response = HttpResponse(mimetype='{0}; charset=utf-8'.format(mimetype))
    response['Content-Disposition'] = 'attachment; filename={0}'.format(name)
    response.write(contents)
    return response


def zip_archive(name, files):
    """
    Returns a response that downloads a zip archive with the given name and
    containing the given iterable of files, each represented by a pair, where
    the first component gives the name and the second one gives the contents.
    """
    temp = tempfile.TemporaryFile()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    for n, contents in files:
        archive.writestr(n, contents.encode('utf-8'))
    archive.close()
    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename={0}.zip'.format(name)
    response['Content-Length'] = temp.tell()
    temp.seek(0)
    return response
<commit_msg>Fix utils.plain_text and utils.zip_archive<commit_after># -*- coding: utf-8 -*-
import hashlib
import io
import json
import tempfile
import zipfile
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.core.servers.basehttp import FileWrapper
from django.http import HttpResponse

 
def verify(cond):
    if not cond:
        raise PermissionDenied


def sign(text):
    return hashlib.md5(text + settings.SECRET_KEY).hexdigest()


def pack(data):
    text = json.dumps(data)
    return (text, sign(text))


def unpack(text, sig):
    verify(sign(text) == sig)
    return json.loads(text)


def plain_text(name, contents, content_type='text/plain'):
    """
    Returns a response that downloads a plain text file with the given name and
    contents.
    """
    response = HttpResponse(content_type='{0}; charset=utf-8'.format(content_type))
    response['Content-Disposition'] = 'attachment; filename={0}'.format(name)
    response.write(contents)
    return response


def zip_archive(name, files):
    """
    Returns a response that downloads a zip archive with the given name and
    containing the given iterable of files, each represented by a pair, where
    the first component gives the name and the second one gives the contents.
    """
    temp = io.BytesIO()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    for n, contents in files:
        archive.writestr(n, contents.encode('utf-8'))
    archive.close()
    response = HttpResponse(temp.getvalue(), content_type="application/x-zip-compressed")
    response['Content-Disposition'] = 'attachment; filename={0}.zip'.format(name)
    return response


{'fraction_non_alphanumeric': 0.10802833530106257, 'fraction_numerical': 0.004427390791027155, 'mean_word_length': 3.918722786647315, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4388668', 'n_tokens_mistral': 1000, 'n_tokens_neox': 975, 'n_words': 298}