So I wrote a function called 'anonimize' that gets a string and returns that string with some details blacked-out, using regex. The function works fine on its own, but I tried to turn it into a decorator and things went south and got so many crashes so many times. I am learning decorators and would really like to understand what I am doing wrong. In this unsuccessful version I got "TypeError: BankApplication.anonimize() missing 1 required positional argument: 'callable'".
import re
class BankApplication:
def __init__(self, bank_name):
self.name = bank_name
'''
Perform anonimization to the text data:
1. Remove possible account numbers (more than 5 digits in a row)
2. Remove possible email addresses
3. Remove possible First + Last names (two consequtive words
starting from upper case, but not divided by .)
'''
def anonimize(func):
def inner(arg):
ret = func(arg)
print(
re.sub("[A-Z][a-z]+\s[A-Z][a-z]+", "***", re.sub("\S+@\S+\.\S+", "***", re.sub("\d{5,}", "***", arg))))
return ret
return inner
@anonimize
def feedback(self, feedback_text):
print("Called feedback")
@anonimize
def log_info(self, info_to_log):
print("Called feedback")
bank = BankApplication("Bank")
bank.feedback("Name: John Doe\nE-mail: johndoe@fakemail.com\nAccount number: 911911911.")