lists.arthurdejong.org
RSS feed

python-stdnum commit: r45 - in python-stdnum: stdnum/br tests

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum commit: r45 - in python-stdnum: stdnum/br tests



Author: arthur
Date: Sat Jan 15 21:26:53 2011
New Revision: 45
URL: http://arthurdejong.org/viewvc/python-stdnum?view=rev&revision=45

Log:
add a CPF (Cadastro de Pessoas Físicas) module

Added:
   python-stdnum/stdnum/br/
   python-stdnum/stdnum/br/__init__.py
   python-stdnum/stdnum/br/cpf.py
   python-stdnum/tests/test_br_cpf.doctest

Added: python-stdnum/stdnum/br/__init__.py
==============================================================================

Added: python-stdnum/stdnum/br/cpf.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/br/cpf.py      Sat Jan 15 21:26:53 2011        (r45)
@@ -0,0 +1,65 @@
+# cpf.py - functions for handling CPF numbers
+# coding: utf-8
+#
+# Copyright (C) 2011 Arthur de Jong
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Module for handling CPF (Cadastro de Pessoas Físicas) numbers, the
+Brazillian national identification number.
+
+>>> is_valid('390.533.447-05')
+True
+>>> is_valid('231.002.999-00')
+False
+>>> compact('390.533.447-05')
+'39053344705'
+>>> format('23100299900')
+'231.002.999-00'
+"""
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    number = number.replace(' ','').replace('-','').replace('.','').strip()
+    # pad with leading zeroes
+    return number
+
+def _calc_check_digits(number):
+    """Calculate the check digits for the number."""
+    d1 = sum( (10-i) * int(number[i]) for i in range(9) )
+    d1 = ( 11 - d1 ) % 11 % 10
+    d2 = sum( (11-i) * int(number[i]) for i in range(9) ) + 2 * d1
+    d2 = ( 11 - d2 ) % 11 % 10
+    return '%d%d' % (d1, d2)
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid BSN. This checks
+    the length and whether the check digit is correct."""
+    try:
+        number = compact(number)
+    except:
+        return False
+    return len(number) == 11 and \
+           number.isdigit() and \
+           int(number) > 0 and \
+           _calc_check_digits(number) == number[-2:]
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    return number[:3] + '.' + number[3:6] + '.' + number[6:-2] + '-' + 
number[-2:]

Added: python-stdnum/tests/test_br_cpf.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_br_cpf.doctest     Sat Jan 15 21:26:53 2011        
(r45)
@@ -0,0 +1,42 @@
+test_br_cpf.doctest - more detailed doctests for stdnum.br.cpf module
+
+Copyright (C) 2011 Arthur de Jong
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA
+
+
+This file contains more detailed doctests for the stdnum.nl.cpf module. It
+tries to test more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.br import cpf
+
+
+These are normal variations that should just work.
+
+>>> cpf.is_valid('39053344705')
+True
+>>> cpf.is_valid('23100299900')
+False
+
+These are tests to check what happens when a wrong type is passed.
+
+>>> cpf.is_valid(39053344705) # integer, not string
+False
+>>> cpf.is_valid(None)
+False
+>>> cpf.is_valid('')
+False
--
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits