associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Seu texto de busca "linkto:"Cnpj"" não retornou nenhum resultado. Por favor mude os termos do texto, para mais informações veja HelpOnSearching.
(!) Considere fazer uma busca completa do texto com seus próprios termos.

Excluir mensagem

Cnpj

Receita: Classe Cnpj

Classe para lidar mais facilmente com Cnpjs.

Código

   1 class Cnpj:
   2     def __init__( self ):
   3         """
   4         Class to interact with Cnpj brazilian numbers
   5         """
   6         pass
   7         
   8     def validate( self, cnpj ):
   9         """ 
  10         Method to validate brazilian cnpjs
  11         Tests:
  12         
  13         >>> print Cnpj().validate('61882613000194')
  14         True
  15         >>> print Cnpj().validate('61882613000195')
  16         False
  17         >>> print Cnpj().validate('53.612.734/0001-98')
  18         True
  19         >>> print Cnpj().validate('69.435.154/0001-02')
  20         True
  21         >>> print Cnpj().validate('69.435.154/0001-01')
  22         False
  23         """
  24         # defining some variables
  25         lista_validacao_um = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4 , 3, 2]
  26         lista_validacao_dois = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
  27         
  28         # cleaning the cnpj
  29         cnpj = cnpj.replace( "-", "" )
  30         cnpj = cnpj.replace( ".", "" )
  31         cnpj = cnpj.replace( "/", "" )
  32     
  33         # finding out the digits
  34         verificadores = cnpj[-2:]
  35         
  36         # verifying the lenght of the cnpj
  37         if len( cnpj ) != 14:
  38             return False
  39         
  40         # calculating the first digit
  41         soma = 0
  42         id = 0
  43         for numero in cnpj:
  44             
  45             # to do not raise indexerrors
  46             try:
  47                 lista_validacao_um[id]
  48             except:
  49                 break
  50                 
  51             soma += int( numero ) * int( lista_validacao_um[id] )
  52             id += 1
  53         
  54         soma = soma % 11
  55         if soma < 2:
  56             digito_um = 0
  57         else:
  58             digito_um = 11 - soma
  59         
  60         digito_um = str( digito_um ) # converting to string, for later comparison
  61         
  62         # calculating the second digit
  63         # suming the two lists
  64         soma = 0
  65         id = 0
  66         
  67         # suming the two lists
  68         for numero in cnpj:
  69             
  70             # to do not raise indexerrors
  71             try:
  72                 lista_validacao_dois[id]
  73             except:
  74                 break
  75             
  76             soma += int( numero ) * int( lista_validacao_dois[id] )
  77             id += 1
  78         
  79         # defining the digit
  80         soma = soma % 11
  81         if soma < 2:
  82             digito_dois = 0
  83         else:
  84             digito_dois = 11 - soma
  85         
  86         digito_dois = str( digito_dois )
  87     
  88         # returnig
  89         return bool( verificadores == digito_um + digito_dois )
  90 
  91     def format( self, cnpj ):
  92         """
  93         Method to format cnpj numbers.
  94         Tests:
  95         
  96         >>> print Cnpj().format('53612734000198')
  97         53.612.734/0001-98
  98         """
  99         return "%s.%s.%s/%s-%s" % ( cnpj[0:2], cnpj[2:5], cnpj[5:8], cnpj[8:12], cnpj[12:14] )
 100 
 101 # tests
 102 #import doctest; doctest.testmod()

Exemplo de uso

Dentro do próprio código, nas docstrings.

Volta para CookBook.


Murtog