Validator for vertices of a polygon

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












There is an object which has few properties. I wrote a function for test cases which check if the properties are satisfied or not. The function should throw an exception if the property is not satisfied.



But, I think there are better ways to write those test cases. A subset of the property of the object is as follows:




x['polygon'] is a list of >= 3 integer (x,y) pairs representing the
corners of the polygon in clockwise or anticlockwise order.




Current function is as follows:



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


It would be really helpful if someone could please suggest better ways of writing those test cases.







share|improve this question





















  • Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
    – Martin R
    May 9 at 17:56










  • Thanks. I am currently not doing it, but I will write a function for it.
    – user1609160
    May 9 at 18:00










  • @MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
    – 200_success
    May 9 at 19:19










  • @200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
    – Martin R
    May 9 at 19:23
















up vote
4
down vote

favorite












There is an object which has few properties. I wrote a function for test cases which check if the properties are satisfied or not. The function should throw an exception if the property is not satisfied.



But, I think there are better ways to write those test cases. A subset of the property of the object is as follows:




x['polygon'] is a list of >= 3 integer (x,y) pairs representing the
corners of the polygon in clockwise or anticlockwise order.




Current function is as follows:



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


It would be really helpful if someone could please suggest better ways of writing those test cases.







share|improve this question





















  • Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
    – Martin R
    May 9 at 17:56










  • Thanks. I am currently not doing it, but I will write a function for it.
    – user1609160
    May 9 at 18:00










  • @MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
    – 200_success
    May 9 at 19:19










  • @200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
    – Martin R
    May 9 at 19:23












up vote
4
down vote

favorite









up vote
4
down vote

favorite











There is an object which has few properties. I wrote a function for test cases which check if the properties are satisfied or not. The function should throw an exception if the property is not satisfied.



But, I think there are better ways to write those test cases. A subset of the property of the object is as follows:




x['polygon'] is a list of >= 3 integer (x,y) pairs representing the
corners of the polygon in clockwise or anticlockwise order.




Current function is as follows:



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


It would be really helpful if someone could please suggest better ways of writing those test cases.







share|improve this question













There is an object which has few properties. I wrote a function for test cases which check if the properties are satisfied or not. The function should throw an exception if the property is not satisfied.



But, I think there are better ways to write those test cases. A subset of the property of the object is as follows:




x['polygon'] is a list of >= 3 integer (x,y) pairs representing the
corners of the polygon in clockwise or anticlockwise order.




Current function is as follows:



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


It would be really helpful if someone could please suggest better ways of writing those test cases.









share|improve this question












share|improve this question




share|improve this question








edited May 9 at 19:15









200_success

123k14142399




123k14142399









asked May 9 at 17:05









user1609160

233




233











  • Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
    – Martin R
    May 9 at 17:56










  • Thanks. I am currently not doing it, but I will write a function for it.
    – user1609160
    May 9 at 18:00










  • @MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
    – 200_success
    May 9 at 19:19










  • @200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
    – Martin R
    May 9 at 19:23
















  • Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
    – Martin R
    May 9 at 17:56










  • Thanks. I am currently not doing it, but I will write a function for it.
    – user1609160
    May 9 at 18:00










  • @MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
    – 200_success
    May 9 at 19:19










  • @200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
    – Martin R
    May 9 at 19:23















Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
– Martin R
May 9 at 17:56




Where does your code verify that the (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order?
– Martin R
May 9 at 17:56












Thanks. I am currently not doing it, but I will write a function for it.
– user1609160
May 9 at 18:00




Thanks. I am currently not doing it, but I will write a function for it.
– user1609160
May 9 at 18:00












@MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
– 200_success
May 9 at 19:19




@MartinR There's no stated requirement that the polygon be simple or convex. It's not clear that "clockwise" or "anticlockwise" means anything, other than the fact that they are in sequence.
– 200_success
May 9 at 19:19












@200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
– Martin R
May 9 at 19:23




@200_success: If the function is documented to "Specifically check that ... (x,y) pairs represent the corners of the polygon in clockwise or anticlockwise order" then there should be some corresponding code, shouldn't it?
– Martin R
May 9 at 19:23










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










In Python, if you do not specify the return value of a function, then by default it will return None. So no need to hard code return in your case.



You do not need to create the extra variable points_list as you can directly write:



 if len(x['polygon']) < 3:
raise ValueError('More than two points required.')


and:



 for x, y in x['polygon']:
if type(x) != int or type(y) != int:
raise ValueError('integer (x,y) pairs required.')


Of course, choosing a meaningful and significant name for the object x will be better.



When we check if stuff in some_iterable we get True or False. I mean that a ValueError exception is not the most suitable thing that can happen here.



isnstance() returns True or False, and may raise a TypeError exception under certain conditions (check the link), so here also raising a ValueError may not be appropriate.



Because when you raise the exception, you communicate to the user only what he needs to know (through the messages you specified for each case) and because of the last 2 points I mentioned previously, I suggest you to create a custom exception which message can be overridden for each situation:



class InvalidPolygonObject(Exception):
pass


And your function can be written this way:



def validate_object(polygon_object): 
if type(polygon_object) != dict:
raise InvalidPolygonObject('dict type input required.')

if 'polygon' not in polygon_object:
raise InvalidPolygonObject('polygon object required.')

if not isinstance(polygon_object['polygon'], (list,)):
raise InvalidPolygonObject('list type polygon object required.')

if len(polygon_object['polygon']) < 3:
raise InvalidPolygonObject('More than two points required.')

for x, y in polygon_object['polygon']:
if type(x) != int or type(y) != int:
raise InvalidPolygonObject('integer (x,y) pairs required.')





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194029%2fvalidator-for-vertices-of-a-polygon%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    In Python, if you do not specify the return value of a function, then by default it will return None. So no need to hard code return in your case.



    You do not need to create the extra variable points_list as you can directly write:



     if len(x['polygon']) < 3:
    raise ValueError('More than two points required.')


    and:



     for x, y in x['polygon']:
    if type(x) != int or type(y) != int:
    raise ValueError('integer (x,y) pairs required.')


    Of course, choosing a meaningful and significant name for the object x will be better.



    When we check if stuff in some_iterable we get True or False. I mean that a ValueError exception is not the most suitable thing that can happen here.



    isnstance() returns True or False, and may raise a TypeError exception under certain conditions (check the link), so here also raising a ValueError may not be appropriate.



    Because when you raise the exception, you communicate to the user only what he needs to know (through the messages you specified for each case) and because of the last 2 points I mentioned previously, I suggest you to create a custom exception which message can be overridden for each situation:



    class InvalidPolygonObject(Exception):
    pass


    And your function can be written this way:



    def validate_object(polygon_object): 
    if type(polygon_object) != dict:
    raise InvalidPolygonObject('dict type input required.')

    if 'polygon' not in polygon_object:
    raise InvalidPolygonObject('polygon object required.')

    if not isinstance(polygon_object['polygon'], (list,)):
    raise InvalidPolygonObject('list type polygon object required.')

    if len(polygon_object['polygon']) < 3:
    raise InvalidPolygonObject('More than two points required.')

    for x, y in polygon_object['polygon']:
    if type(x) != int or type(y) != int:
    raise InvalidPolygonObject('integer (x,y) pairs required.')





    share|improve this answer

























      up vote
      3
      down vote



      accepted










      In Python, if you do not specify the return value of a function, then by default it will return None. So no need to hard code return in your case.



      You do not need to create the extra variable points_list as you can directly write:



       if len(x['polygon']) < 3:
      raise ValueError('More than two points required.')


      and:



       for x, y in x['polygon']:
      if type(x) != int or type(y) != int:
      raise ValueError('integer (x,y) pairs required.')


      Of course, choosing a meaningful and significant name for the object x will be better.



      When we check if stuff in some_iterable we get True or False. I mean that a ValueError exception is not the most suitable thing that can happen here.



      isnstance() returns True or False, and may raise a TypeError exception under certain conditions (check the link), so here also raising a ValueError may not be appropriate.



      Because when you raise the exception, you communicate to the user only what he needs to know (through the messages you specified for each case) and because of the last 2 points I mentioned previously, I suggest you to create a custom exception which message can be overridden for each situation:



      class InvalidPolygonObject(Exception):
      pass


      And your function can be written this way:



      def validate_object(polygon_object): 
      if type(polygon_object) != dict:
      raise InvalidPolygonObject('dict type input required.')

      if 'polygon' not in polygon_object:
      raise InvalidPolygonObject('polygon object required.')

      if not isinstance(polygon_object['polygon'], (list,)):
      raise InvalidPolygonObject('list type polygon object required.')

      if len(polygon_object['polygon']) < 3:
      raise InvalidPolygonObject('More than two points required.')

      for x, y in polygon_object['polygon']:
      if type(x) != int or type(y) != int:
      raise InvalidPolygonObject('integer (x,y) pairs required.')





      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        In Python, if you do not specify the return value of a function, then by default it will return None. So no need to hard code return in your case.



        You do not need to create the extra variable points_list as you can directly write:



         if len(x['polygon']) < 3:
        raise ValueError('More than two points required.')


        and:



         for x, y in x['polygon']:
        if type(x) != int or type(y) != int:
        raise ValueError('integer (x,y) pairs required.')


        Of course, choosing a meaningful and significant name for the object x will be better.



        When we check if stuff in some_iterable we get True or False. I mean that a ValueError exception is not the most suitable thing that can happen here.



        isnstance() returns True or False, and may raise a TypeError exception under certain conditions (check the link), so here also raising a ValueError may not be appropriate.



        Because when you raise the exception, you communicate to the user only what he needs to know (through the messages you specified for each case) and because of the last 2 points I mentioned previously, I suggest you to create a custom exception which message can be overridden for each situation:



        class InvalidPolygonObject(Exception):
        pass


        And your function can be written this way:



        def validate_object(polygon_object): 
        if type(polygon_object) != dict:
        raise InvalidPolygonObject('dict type input required.')

        if 'polygon' not in polygon_object:
        raise InvalidPolygonObject('polygon object required.')

        if not isinstance(polygon_object['polygon'], (list,)):
        raise InvalidPolygonObject('list type polygon object required.')

        if len(polygon_object['polygon']) < 3:
        raise InvalidPolygonObject('More than two points required.')

        for x, y in polygon_object['polygon']:
        if type(x) != int or type(y) != int:
        raise InvalidPolygonObject('integer (x,y) pairs required.')





        share|improve this answer













        In Python, if you do not specify the return value of a function, then by default it will return None. So no need to hard code return in your case.



        You do not need to create the extra variable points_list as you can directly write:



         if len(x['polygon']) < 3:
        raise ValueError('More than two points required.')


        and:



         for x, y in x['polygon']:
        if type(x) != int or type(y) != int:
        raise ValueError('integer (x,y) pairs required.')


        Of course, choosing a meaningful and significant name for the object x will be better.



        When we check if stuff in some_iterable we get True or False. I mean that a ValueError exception is not the most suitable thing that can happen here.



        isnstance() returns True or False, and may raise a TypeError exception under certain conditions (check the link), so here also raising a ValueError may not be appropriate.



        Because when you raise the exception, you communicate to the user only what he needs to know (through the messages you specified for each case) and because of the last 2 points I mentioned previously, I suggest you to create a custom exception which message can be overridden for each situation:



        class InvalidPolygonObject(Exception):
        pass


        And your function can be written this way:



        def validate_object(polygon_object): 
        if type(polygon_object) != dict:
        raise InvalidPolygonObject('dict type input required.')

        if 'polygon' not in polygon_object:
        raise InvalidPolygonObject('polygon object required.')

        if not isinstance(polygon_object['polygon'], (list,)):
        raise InvalidPolygonObject('list type polygon object required.')

        if len(polygon_object['polygon']) < 3:
        raise InvalidPolygonObject('More than two points required.')

        for x, y in polygon_object['polygon']:
        if type(x) != int or type(y) != int:
        raise InvalidPolygonObject('integer (x,y) pairs required.')






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 9 at 20:55









        Billal BEGUERADJ

        1




        1






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194029%2fvalidator-for-vertices-of-a-polygon%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?