Replies: 1 comment
-
Hi @cclauss, is this something what you are looking for? fromtypingimportList, Tuple, CallablefromcollectionsimportCounterclassPasswordTooShort(Exception): passclassPasswordTooLong(Exception): passclassNoLowercaseCharacter(Exception): passclassNoUppercaseCharacter(Exception): passclassNoSpecialCharacter(Exception): passclassUnacceptableSpecialCharacter(Exception): passclassTooMuchRepetition(Exception): passdef_check_repetition(password: str, max_repetition: int) ->bool: count=Counter(password) returnany(_>max_repetitionfor_incount.values()) defvalidate_password(password: str) ->None: checks: List[Tuple[Callable[[str], bool], Exception]] = [ (lambdapwd: len(pwd) <8, PasswordTooShort("Password is too short.")), (lambdapwd: len(pwd) >20, PasswordTooLong("Password is too long.")), ( lambdapwd: notany(c.islower() forcinpwd), NoLowercaseCharacter("Password has no lowercase characters."), ), ( lambdapwd: notany(c.isupper() forcinpwd), NoUppercaseCharacter("Password has no uppercase characters."), ), ( lambdapwd: notany(cin"1234567890"forcinpwd), NoSpecialCharacter("Password has no acceptable special characters."), ), ( lambdapwd: any(cin",~@"forcinpwd), UnacceptableSpecialCharacter( "Password has unacceptable special characters." ), ), ( lambdapwd: _check_repetition(pwd, 2), TooMuchRepetition("Password has too much repetition."), ), ] errors= [errorforpredicate, errorinchecksifpredicate(password)] iferrors: raiseExceptionGroup("Password Validation Errors", errors) if__name__=="__main__": try: validate_password("passssw~rd1") exceptExceptionGroupase: forexceptionine.exceptions: print(exception) |
BetaWas this translation helpful?Give feedback.
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
-
https://docs.python.org/3/library/exceptions.html#exception-groups (New in Python 3.11)
ExceptionGroups
are useful for validating data and telling the user of multiple failures.For example, if we are examining a string to see if it qualifies as a strong password (#10885) we want to see if it
If each of these raised a separate exception then we could use an
ExceptionGroup
to tell the user all of the problems at once. We could tell them that the proposed password is too short and has no uppercase characters and has a~
which is an unacceptable special character.The contribution should solve a normal algorithm problem but should an
ExceptionGroup
to provide detailed feedback.BetaWas this translation helpful?Give feedback.
All reactions