I am working on a class that requires multiple rules to validate against e.g. if certain pattern appears more than, equal, less than to a certain number. I have the output of this regular expression I am validating in a list and checking the length of the list. Now how do I call one of these methods (__gt__
, __eq__
etc) dynamically?
My approach:
func = getattr(len(re.findall('\d+', 'password1'), method_name) #method_name
#can be any one of the values __eq__, __gt__).
if func(desired_length):
print ":validated"
else:
raise Exception("Not sufficient complexity")
For example, for method_name='__eq__'
and desired_length=1
, the above will result True
. For method_name='__gt__'
and desired_length=1
, the above will result False
(if a number appears in this string more than once).
But I realize int
objects don't really implement these methods. Is there any way I can achieve this?
Aucun commentaire:
Enregistrer un commentaire