<s> import re <EOL> from string import digits , split as L <EOL> CARDS = { <EOL> '<STR_LIT>' : re . compile ( r"<STR_LIT>" ) , <EOL> '<STR_LIT>' : re . compile ( r"<STR_LIT>" ) , <EOL> '<STR_LIT>' : re . compile ( r"<STR_LIT>" ) , <EOL> '<STR_LIT>' : re . compile ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : re . compile ( "<STR_LIT>" ) , <EOL> } <EOL> TEST_NUMBERS = L ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def verify_credit_card ( number , allow_test = False ) : <EOL> """<STR_LIT>""" <EOL> return CreditCard ( number ) . verify ( allow_test ) <EOL> class CreditCard ( object ) : <EOL> def __init__ ( self , number ) : <EOL> self . number = number <EOL> def is_number ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . number , basestring ) : <EOL> self . number = "<STR_LIT>" . join ( [ c for c in self . number if c in digits ] ) <EOL> return self . number . isdigit ( ) <EOL> return False <EOL> def is_mod10 ( self ) : <EOL> """<STR_LIT>""" <EOL> double = <NUM_LIT:0> <EOL> total = <NUM_LIT:0> <EOL> for i in range ( len ( self . number ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> for c in str ( ( double + <NUM_LIT:1> ) * int ( self . number [ i ] ) ) : <EOL> total = total + int ( c ) <EOL> double = ( double + <NUM_LIT:1> ) % <NUM_LIT:2> <EOL> return ( total % <NUM_LIT:10> ) == <NUM_LIT:0> <EOL> def is_test ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . number in TEST_NUMBERS <EOL> def get_type ( self ) : <EOL> """<STR_LIT>""" <EOL> for card , pattern in CARDS . iteritems ( ) : <EOL> if pattern . match ( self . number ) : <EOL> return card <EOL> return None <EOL> def verify ( self , allow_test ) : <EOL> """<STR_LIT>""" <EOL> if self . is_number ( ) and ( not self . is_test ( ) or allow_test ) and self . is_mod10 ( ) : <EOL> return self . get_type ( ) <EOL> return None </s>
