Skip to content

Instantly share code, notes, and snippets.

@1328
Created May 16, 2014 21:18
Show Gist options
  • Select an option

  • Save 1328/3737b9eae4efec96a0dd to your computer and use it in GitHub Desktop.

Select an option

Save 1328/3737b9eae4efec96a0dd to your computer and use it in GitHub Desktop.
validation
from pprint import pprint
def check_int(x):
'''probably want to work out how to deal with floats as well'''
'''but this is just proof of concept'''
try:
int(x)
return True, x
except ValueError:
return False, '=int("{}")'.format(x)
def check_lessthan(x,y):
if x<y:
return True, x
return False, '="Error: {} should be less than {}"'.format(x,y)
print(check_int(5))
print(check_int('word'))
'''let's make a simple 3x3 table'''
table=[[0 for a in range(3)] for b in range(3)]
table[0][1] = 4
table[1][1] = 6
table[2][1] = 'word'
'''now lets create an list of checks'''
'''check[x] will be the validation function(s) for column x'''
checks = [[] for _ in range(3)]
'''ok, validation for second column, confirm its an int'''
checks[1].append(check_int)
''' and less than 5'''
checks[1].append(lambda x:check_lessthan(x,5))
for check in checks[1]:
for row in table:
ok, row[1]=check(row[1])
if not ok:
break # Note it checks int before it does value comparison!
pprint(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment