【发布时间】:2021-12-30 13:20:08
【问题描述】:
所以我编写了一个名为“anonimize”的函数,它使用正则表达式获取一个字符串并返回该字符串,其中一些细节被涂黑。 该函数本身可以正常工作,但我试图将它变成一个装饰器,结果却出现了很多次崩溃。 我正在学习装饰器,并且真的很想了解我做错了什么。 在这个不成功的版本中,我得到了“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.")
【问题讨论】:
-
我更正了缩进
标签: python class crash decorator python-decorators