# vat.py - functions for handling Cypriot VAT 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

"""Αριθμός Εγγραφής Φ.Π.Α. (Cypriot VAT number).

The Cypriot Αριθμός Εγγραφής Φ.Π.Α. (VAT) number consists of 9 digits
where the last one is a is a letter and functions as a check digit.

>>> compact('CY-10259033P')
'10259033P'
>>> is_valid('CY-10259033P ')
True
>>> is_valid('CY-10259033Z')  # invalid check digit
False
"""

from stdnum.util import clean


def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    number = clean(number, ' -').upper().strip()
    if number.startswith('CY'):
        number = number[2:]
    return number


def calc_check_digit(number):
    """Calculate the check digit. The number passed should not have the
    check digit included."""
    translation = {
        '0': 1, '1': 0, '2': 5, '3': 7, '4': 9,
        '5': 13, '6': 15, '7': 17, '8': 19, '9': 21,
    }
    return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[(
        sum(translation[x] for x in number[::2]) +
        sum(int(x) for x in number[1::2])
    ) % 26]


def is_valid(number):
    """Checks to see if the number provided is a valid VAT number. This
    checks the length, formatting and check digit."""
    try:
        number = compact(number)
    except:
        return False
    return len(number) == 9 and number[:-1].isdigit() and \
           number[0:2] != '12' and \
           number[-1] == calc_check_digit(number[:-1])

@@ -85,16 +85,22 @@
 (C) 2012
+, 2013
  Arthur 
@@ -1067,32 +1067,32 @@
 59033P'%0A%3E%3E%3E 
-is_
 valid
+ate
 ('CY-1025903
@@ -1101,25 +1101,32 @@
  ')%0A
-True
+'10259033P'
 %0A%3E%3E%3E 
-is_
 valid
+ate
 ('CY
@@ -1165,18 +1165,108 @@
 git%0A
-False%0A%22%22%22%0A
+Traceback (most recent call last):%0A    ...%0AInvalidChecksum: ...%0A%22%22%22%0A%0Afrom stdnum.exceptions import *
 %0Afro
@@ -2006,16 +2006,16 @@
 def 
-is_
 valid
+ate
 (num
@@ -2150,21 +2150,8 @@
 %22%22%22%0A
-    try:%0A    
     
@@ -2183,47 +2183,74 @@
     
-except:%0A        return False%0A    return
+if not number%5B:-1%5D.isdigit():%0A        raise InvalidFormat()%0A    if
  len
@@ -2262,55 +2262,50 @@
 er) 
-== 9 and number%5B:-1%5D.isdigit() and %5C%0A          
+!= 9:%0A        raise InvalidLength()%0A    if
  num
@@ -2317,32 +2317,56 @@
 :2%5D 
-!
+=
 = '12'
- and %5C%0A          
+:%0A        raise InvalidComponent()%0A    if
  num
@@ -2373,17 +2373,17 @@
 ber%5B-1%5D 
-=
+!
 = calc_c
@@ -2405,9 +2405,306 @@
 er%5B:-1%5D)
+:%0A        raise InvalidChecksum()%0A    return number%0A%0A%0Adef is_valid(number):%0A    %22%22%22Checks to see if the number provided is a valid VAT number. This%0A    checks the length, formatting and check digit.%22%22%22%0A    try:%0A        return bool(validate(number))%0A    except ValidationError:%0A        return False
 %0A

Implement validate() for Cypriot numbers