-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User input fixes #120
User input fixes #120
Conversation
@@ -22,3 +22,6 @@ clean: | |||
find . -name "*.py[co]" -o -name __pycache__ -exec rm -rf {} + | |||
find . -path "*/*.pyo" -delete | |||
find . -path "*/*.pyc" -delete | |||
|
|||
bci-gui: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition!
bcipy/gui/BCInterface.py
Outdated
@@ -369,7 +418,8 @@ def start_experiment(self) -> None: | |||
Using the inputs gathers, check for validity using the check_input method, then launch the experiment using a | |||
command to bci_main.py and subprocess. | |||
""" | |||
if self.check_input(): | |||
|
|||
if self.check_input() and self.user_id_validator(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should go in the check_input
method, which already extracts the self.user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that make sense! thank you for the feedback!
bcipy/gui/BCInterface.py
Outdated
""" | ||
|
||
# Update based on current inputs | ||
self.user = self.user_input.currentText() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned below this value should already be set when you call this in check_input()
. In the current flow, we would extract user from the input and assign self.user twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleted from function
@@ -327,6 +328,54 @@ def check_input(self) -> bool: | |||
return False | |||
return True | |||
|
|||
def user_id_validator(self) -> bool: | |||
"""Check User ID Requirements. User ID must follow the following Requirements: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great documentation!
bcipy/gui/BCInterface.py
Outdated
|
||
# Check the length of minimum one character up to 25 characters | ||
try: | ||
if len(self.user) == 0 or len(self.user) > 25: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider assigning a length_user
variable since you call it twice, and you could then append it in the message of the alert using f strings,
f'User ID must be of at least one or up to 25. Your current ID is {length_user} characters.'
bcipy/gui/BCInterface.py
Outdated
message_type=AlertMessageType.INFO, | ||
okay_to_exit=True) | ||
return False | ||
# Check for white space |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments are off.. try running make lint
and see if it will catch this formatting stuff for you. If not, you'll need to go in and fix!
bcipy/gui/BCInterface.py
Outdated
def user_id_validator(self) -> bool: | ||
"""Check User ID Requirements. User ID must follow the following Requirements: | ||
|
||
1. Maximum length of 25 letters or numbers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add these as class-level variables at the top. For instance,
self.max_length = 25
in the init
and then use that value instead of the hardcoded 25. Update the documentation to reflect that we set it above. This will make changing it easier in the future. I'd do this for all cases. self.min_length
, self.special_character_regex
message_type=AlertMessageType.CRIT, | ||
okay_to_exit=True) | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of repeated code in this method that follows the same pattern: the user_id is checked against some condition and if it evaluates to True an alert is displayed with an error message specific to that condition. In addition to the increased code, this is also difficult to fully test and will only provide the user with 1 error message at a time. If the user_id has multiple problems this can be a frustrating experience for the end user.
One way to avoid repeating yourself is to abstract out the validations as separate functions (each of which can be independently tested), and store the validation functions along with the error message. For example:
# These functions could be in a separate module
def invalid_length(min=1, max=25):
return lambda string: len(string) < min or len(string) > max
def contains_whitespaces(string):
return re.match(r"^(?=.*[\s])", string)
def has_special_characters(string):
disallowed_chars = re.compile('[^0-9a-zA-Z]+')
return bool(disallowed_chars.search(string))
# This would go in the class definition
user_id_validations = [
(invalid_length(min=self.min_length, max=self.max_length), f'must contain between {self.min_length} and {self.max_length} alphanumeric characters'),
(contains_whitespaces, 'cannot contain white spaces'),
(has_special_characters, 'cannot contain special characters')
]
Then in the user_id_validator
method you can iterate through these validations, check them against the user_id and collect the error messages for any that return True. If any problems are detected they can all be displayed to the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so elegant!
f94b020
to
6ee5fde
Compare
9f072ce
to
21fd9ab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks great~
Overview
User ID verification with the following criteria:
Added User ID Verification for the following
#176210324
https://www.pivotaltracker.com/story/show/175563946
Contributions
Tested as much as I could with combinations oh spaces, special charts, numbers,