Switch to python3

This commit is contained in:
j 2014-09-30 18:15:32 +02:00
commit 9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions

View file

@ -0,0 +1,21 @@
# __init__.py - collection of United States numbers
# coding: utf-8
#
# Copyright (C) 2012 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
"""Collection of United States numbers."""

View file

@ -0,0 +1,75 @@
# atin.py - functions for handling ATINs
#
# Copyright (C) 2013 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
"""ATIN (U.S. Adoption Taxpayer Identification Number).
An Adoption Taxpayer Identification Number (ATIN) is a temporary
nine-digit number issued by the United States IRS for a child for whom the
adopting parents cannot obtain a Social Security Number.
>>> validate('123-45-6789')
'123456789'
>>> validate('1234-56789') # dash in the wrong place
Traceback (most recent call last):
...
InvalidFormat: ...
>>> format('123456789')
'123-45-6789'
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# regular expression for matching ATINs
_atin_re = re.compile('^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$')
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def validate(number):
"""Checks to see if the number provided is a valid ATIN. This checks
the length and formatting if it is present."""
match = _atin_re.search(clean(number, '').strip())
if not match:
raise InvalidFormat()
# sadly, no more information on ATIN number validation was found
return compact(number)
def is_valid(number):
"""Checks to see if the number provided is a valid ATIN. This checks
the length and formatting if it is present."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
if len(number) == 9:
number = number[:3] + '-' + number[3:5] + '-' + number[5:]
return number

View file

@ -0,0 +1,15 @@
# manually converted from the IRS website
# http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes
01,02,03,04,05,06,11,13,14,16,21,22,23,25,34,51,52,54,55,56,57,58,59,65 campus="Brookhaven"
10,12 campus="Andover"
15,24 campus="Fresno"
20,26,27,45,46 campus="Internet"
30,32,35,36,37,38,61 campus="Cincinnati"
31 campus="Small Business Administration (SBA)"
33,39,41,42,43,46,48,62,63,64,66,68,71,72,73,74,75,76,77,81,82,83,84,85,86,87,88,91,92,93,98,99 campus="Philadelphia"
40,44 campus="Kansas City"
50,53 campus="Austin"
60,67 campus="Atlanta"
80,90 campus="Ogden"
94,95 campus="Memphis"

View file

@ -0,0 +1,92 @@
# ein.py - functions for handling EINs
#
# Copyright (C) 2013 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
"""EIN (U.S. Employer Identification Number).
The Employer Identification Number, also known as Federal Employer
Identification Number (FEIN), is used to identify a business entity in the
United States. It is issued to anyone that has to pay withholding taxes on
employees.
>>> validate('91-1144442')
'911144442'
>>> get_campus('04-2103594') == 'Brookhaven'
True
>>> validate('911-14-4442') # dash in the wrong place
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('07-1144442') # wrong prefix
Traceback (most recent call last):
...
InvalidComponent: ...
>>> format('042103594')
'04-2103594'
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# regular expression for matching EINs
_ein_re = re.compile('^(?P<area>[0-9]{2})-?(?P<group>[0-9]{7})$')
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def get_campus(number):
"""Determine the Campus or other location that issued the EIN."""
from stdnum import numdb
number = compact(number)
results = numdb.get('us/ein').info(number)[0][1]
if not results:
raise InvalidComponent()
return results['campus']
def validate(number):
"""Checks to see if the number provided is a valid EIN. This checks
the length, groups and formatting if it is present."""
match = _ein_re.search(clean(number, '').strip())
if not match:
raise InvalidFormat()
get_campus(number) # raises exception for unknown campus
return compact(number)
def is_valid(number):
"""Checks to see if the number provided is a valid EIN. This checks
the length, groups and formatting if it is present."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
if len(number) == 9:
number = number[:2] + '-' + number[2:]
return number

View file

@ -0,0 +1,96 @@
# itin.py - functions for handling ITINs
#
# Copyright (C) 2013 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
"""ITIN (U.S. Individual Taxpayer Identification Number).
The Individual Taxpayer Identification Number is issued by the United
States IRS to individuals who are required to have a taxpayer
identification number but who are not eligible to obtain a Social Security
Number.
It is a nine-digit number that begins with the number 9 and the
fourth and fifth digit are expected to be in a certain range.
>>> validate('912-90-3456')
'912903456'
>>> validate('9129-03456') # dash in the wrong place
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('123-45-6789') # wrong start digit
Traceback (most recent call last):
...
InvalidComponent: ...
>>> validate('912-93-4567') # wrong group
Traceback (most recent call last):
...
InvalidComponent: ...
>>> compact('1234-56-789')
'123456789'
>>> format('111223333')
'111-22-3333'
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# regular expression for matching ITINs
_itin_re = re.compile('^(?P<area>[0-9]{3})-?(?P<group>[0-9]{2})-?[0-9]{4}$')
# allowed group digits
_allowed_groups = set((str(x) for x in range(70, 100) if x not in (89, 93)))
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def validate(number):
"""Checks to see if the number provided is a valid ITIN. This checks
the length, groups and formatting if it is present."""
match = _itin_re.search(clean(number, '').strip())
if not match:
raise InvalidFormat()
area = match.group('area')
group = match.group('group')
if area[0] != '9' or group not in _allowed_groups:
raise InvalidComponent()
return compact(number)
def is_valid(number):
"""Checks to see if the number provided is a valid ITIN. This checks
the length, groups and formatting if it is present."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
if len(number) == 9:
number = number[:3] + '-' + number[3:5] + '-' + number[5:]
return number

View file

@ -0,0 +1,68 @@
# ptin.py - functions for handling PTINs
#
# Copyright (C) 2013 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
"""PTIN (U.S. Preparer Tax Identification Number).
A Preparer Tax Identification Number (PTIN) is United States
identification number for tax return preparers. It is an eight-digit
number prefixed with a capital P.
>>> validate('P-00634642')
'P00634642'
>>> validate('P01594846')
'P01594846'
>>> validate('00634642') # missing P
Traceback (most recent call last):
...
InvalidFormat: ...
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# regular expression for matching PTINs
_ptin_re = re.compile('^P[0-9]{8}$')
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def validate(number):
"""Checks to see if the number provided is a valid PTIN. This checks
the length, groups and formatting if it is present."""
number = compact(number).upper()
if not _ptin_re.search(number):
raise InvalidFormat()
# sadly, no more information on PTIN number validation was found
return number
def is_valid(number):
"""Checks to see if the number provided is a valid ATIN. This checks
the length, groups and formatting if it is present."""
try:
return bool(validate(number))
except ValidationError:
return False

View file

@ -0,0 +1,97 @@
# ssn.py - functions for handling SSNs
#
# Copyright (C) 2011, 2012, 2013 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
"""SSN (U.S. Social Security Number).
The Social Security Number is used to identify individuals for taxation
purposes.
>>> validate('536-90-4399')
'536904399'
>>> validate('1112-23333') # dash in the wrong place
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('666-00-0000') # invalid area
Traceback (most recent call last):
...
InvalidComponent: ...
>>> validate('078-05-1120') # blacklisted entry
Traceback (most recent call last):
...
InvalidComponent: ...
>>> compact('1234-56-789')
'123456789'
>>> format('111223333')
'111-22-3333'
"""
import re
from stdnum.exceptions import *
from stdnum.util import clean
# regular expression for matching SSN
_ssn_re = re.compile('^(?P<area>[0-9]{3})-?(?P<group>[0-9]{2})-?(?P<serial>[0-9]{4})$')
# blacklist of SSNs
_ssn_blacklist = set(('078-05-1120', '457-55-5462', '219-09-9999'))
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def validate(number):
"""Checks to see if the number provided is a valid SSN. This checks
the length, groups and formatting if it is present."""
match = _ssn_re.search(clean(number, '').strip())
if not match:
raise InvalidFormat()
area = match.group('area')
group = match.group('group')
serial = match.group('serial')
# check for all-0 or some unused areas
# (9xx also won't be issued which includes the advertising range)
if area == '000' or area == '666' or area[0] == '9' or \
group == '00' or serial == '0000':
raise InvalidComponent()
# check blacklists
if format(number) in _ssn_blacklist:
raise InvalidComponent()
return compact(number)
def is_valid(number):
"""Checks to see if the number provided is a valid SSN. This checks
the length, groups and formatting if it is present."""
try:
return bool(validate(number))
except ValidationError:
return False
def format(number):
"""Reformat the passed number to the standard format."""
if len(number) == 9:
number = number[:3] + '-' + number[3:5] + '-' + number[5:]
return number

View file

@ -0,0 +1,97 @@
# tin.py - functions for handling TINs
#
# Copyright (C) 2013 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
"""TIN (U.S. Taxpayer Identification Number).
The Taxpayer Identification Number is used used for tax purposes in the
United States. A TIN may be:
* a Social Security Number (SSN)
* an Individual Taxpayer Identification Number (ITIN)
* an Employer Identification Number (EIN)
* a Preparer Tax Identification Number (PTIN)
* an Adoption Taxpayer Identification Number (ATIN)
>>> compact('123-45-6789')
'123456789'
>>> validate('123-45-6789')
'123456789'
>>> validate('07-3456789')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> guess_type('536-90-4399')
['ssn', 'atin']
>>> guess_type('04-2103594')
['ein']
>>> guess_type('042103594')
['ssn', 'ein', 'atin']
>>> format('042103594')
'042-10-3594'
>>> format('123-456') # invalid numbers are not reformatted
'123-456'
"""
from stdnum.exceptions import *
from stdnum.us import ssn, itin, ein, ptin, atin
from stdnum.util import clean
_tin_modules = (ssn, itin, ein, ptin, atin)
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '-').strip()
def validate(number):
"""Checks to see if the number provided is a valid TIN. This searches
for the proper sub-type and validates using that."""
for mod in _tin_modules:
try:
return mod.validate(number)
except ValidationError:
pass # try next module
# fallback
raise InvalidFormat()
def is_valid(number):
"""Checks to see if the number provided is a valid TIN. This searches
for the proper sub-type and validates using that."""
try:
return bool(validate(number))
except ValidationError:
return False
def guess_type(number):
"""Return a list of possible TIN types for which this number is
valid.."""
return [mod.__name__.rsplit('.', 1)[-1]
for mod in _tin_modules
if mod.is_valid(number)]
def format(number):
"""Reformat the passed number to the standard format."""
for mod in _tin_modules:
if mod.is_valid(number) and hasattr(mod, 'format'):
return mod.format(number)
return number