raise permissionerror("permission exists"def permit_user(selfperm_nameusername)'''grant the given permission to the user''tryperm_set self permissions[perm_nameexcept keyerrorraise permissionerror("permission does not exist"elseif username not in self authenticator usersraise invalidusername(usernameperm_set add(usernamethe first method allows us to create new permissionunless it already existsin which case an exception is raised the second allows us to add username to permissionunless either the permission or the username doesn' yet exist we use set instead of list for usernamesso that even if you grant user permission more than oncethe nature of sets means the user is only in the set once we'll discuss sets further in later permissionerror is raised in both methods this new error doesn' require usernameso we'll make it extend exception directlyinstead of our custom authexceptionclass permissionerror(exception)pass finallywe can add method to check whether user has specific permission or not in order for them to be granted accessthey have to be both logged into the authenticator and in the set of people who have been granted access to that privilege if either of these conditions is unsatisfiedan exception is raiseddef check_permission(selfperm_nameusername)if not self authenticator is_logged_in(username)raise notloggedinerror(usernametryperm_set self permissions[perm_nameexcept keyerrorraise permissionerror("permission does not exist"elseif username not in perm_setraise notpermittederror(usernameelsereturn true