<commit_before>#!/usr/bin/env python

from __future__ import absolute_import

import random
from collections import namedtuple

from .util import clean_id, pad_id

"""
Functions for working with Brazilian company identifiers (CNPJ).

"""

CNPJ_FIRST_WEIGHTS = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
CNPJ_SECOND_WEIGHTS = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
CNPJ = namedtuple('CNPJ', ['cnpj', 'firm', 'establishment', 'check', 'valid'])


def validate_cnpj(cnpj, autopad=True):
    """Check whether CNPJ is valid. Optionally pad if too short."""
    cnpj = clean_id(cnpj)
    # all complete CNPJ are 14 digits long
    if len(cnpj) != 14:
        return validate_cnpj(pad_cnpj(cnpj), False) if autopad else False

    # 0 is invalid; smallest valid CNPJ is 191
    if cnpj == '00000000000000':
        return False

    digits = [int(k) for k in cnpj[:13]]  # identifier digits
    # validate the first check digit
    cs = sum(w * k for w, k in zip(CNPJ_FIRST_WEIGHTS, digits[:-1])) % 11
    cs = 0 if cs < 2 else 11 - cs
    if cs != int(cnpj[12]):
        return False  # first check digit is not correct<commit_msg>Ensure CNPJ identifier is exactly 14 digits<commit_after>#!/usr/bin/env python

from __future__ import absolute_import

import random
from collections import namedtuple

from .util import clean_id, pad_id

"""
Functions for working with Brazilian company identifiers (CNPJ).

"""

CNPJ_FIRST_WEIGHTS = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
CNPJ_SECOND_WEIGHTS = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
CNPJ = namedtuple('CNPJ', ['cnpj', 'firm', 'establishment', 'check', 'valid'])


def validate_cnpj(cnpj, autopad=True):
    """Check whether CNPJ is valid. Optionally pad if too short."""
    cnpj = clean_id(cnpj)

    # all complete CNPJ are 14 digits long
    if len(cnpj) < 14:
        if not autopad:
            return False
        cnpj = pad_cnpj(cnpj)

    elif len(cnpj) > 14:
        return False

    # 0 is invalid; smallest valid CNPJ is 191
    if cnpj == '00000000000000':
        return False

    digits = [int(k) for k in cnpj[:13]]  # identifier digits
    # validate the first check digit
    cs = sum(w * k for w, k in zip(CNPJ_FIRST_WEIGHTS, digits[:-1])) % 11
    cs = 0 if cs < 2 else 11 - cs
    if cs != int(cnpj[12]):
        return False  # first check digit is not correct
{'fraction_non_alphanumeric': 0.11972437553832903, 'fraction_numerical': 0.05167958656330749, 'mean_word_length': 3.163082437275986, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5854428', 'n_tokens_mistral': 978, 'n_tokens_neox': 843, 'n_words': 302}