Skip to content

Instantly share code, notes, and snippets.

@jaycody
Last active November 11, 2017 06:56
Show Gist options
  • Save jaycody/49b6cdd604f29289522ae458483c69a1 to your computer and use it in GitHub Desktop.
Save jaycody/49b6cdd604f29289522ae458483c69a1 to your computer and use it in GitHub Desktop.
Data validator : checks for positive integer
#! /usr/bin/python -tt
""" Guardian of the function threshold"""
def check_this(x):
"""Returns None if value is anything other than a positive int"""
if not isinstance(x, int):
return None, x
if x < 0:
return None, -1
return 'PASS', 'input: {} is a positive integer'.format(x)
def test_check_this(val_to_check):
"""Compares check_this function's results with expected"""
print '\nChecking input: {} ----->'.format(val_to_check)
results = check_this(val_to_check)
## if PASS
if results[0] is not None:
return results
## if negative
elif results[1] == -1:
return '\tNope! {} is as negative as Julian in Less Than Zero'.format(val_to_check)
## if NOT INT
else:
return '\tNope! Input: {} is of type: {}'.format(val_to_check, type(val_to_check))
if __name__ == '__main__':
## Generator runs each test
T = [10, -10, 'h', [], {}, '', 1.0, None, check_this('16'), '\n']
G = (test_check_this(test_val) for test_val in T)
for testable_item in T:
# run test on that testable_item
print next(G)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment