Skip to content

Instantly share code, notes, and snippets.

@desh2608
Created May 28, 2018 14:49
Show Gist options
  • Save desh2608/c8b7e3fd3604fc3aea603ddcd4e8cff2 to your computer and use it in GitHub Desktop.
Save desh2608/c8b7e3fd3604fc3aea603ddcd4e8cff2 to your computer and use it in GitHub Desktop.
Example of validating an object in Python
def validate_object(x):
"""This function validates an object x that is supposed to represent an object inside an image, and throws an exception on failure. Specifically it is checking that:
x['polygon'] is a list of >= 3 integer (x,y) pairs representing the corners of the polygon in clockwise or anticlockwise order.
"""
if type(x) != dict:
raise ValueError('dict type input required.')
if 'polygon' not in x:
raise ValueError('polygon object required.')
if not isinstance(x['polygon'], (list,)):
raise ValueError('list type polygon object required.')
points_list = x['polygon']
if len(points_list) < 3:
raise ValueError('More than two points required.')
for x, y in points_list:
if type(x) != int or type(y) != int:
raise ValueError('integer (x,y) pairs required.')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment