Adding a new feature
In this example, we’ll look at an existing codebase and add a new feature. The codebase is like the one we used in the previous example, but it’s a bit more complex. This is because it has a few more functions involving different types of payment methods. Here’s the code:
```python
import re
def validate_card(card):
# Visa cards start with 4 and have 13 or 16 digits.
# MasterCard cards start with 51 through 55 and have 16 digits.
visa_pattern = r'^4[0-9]{12}(?:[0-9]{3})?$'
mastercard_pattern = r'^5[1-5][0-9]{14}$'
if re.match(visa_pattern, card):
return 'Visa'
elif re.match(mastercard_pattern, card):
return 'MasterCard'
else:
return None
def process_payment(card, cart):
card_type = validate_card(card)
if card_type is None:
return 1 # Invalid card
else:
# Process the payment here
# Return 0 if the payment is successful...