REST API for random number generation using Falcon

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
3
down vote

favorite












I have written REST API for random number generation using Falcon(https://falconframework.org/), just for my learning process.



This is API:



http "localhost:8000/random-number?min=10&max=10"


This is code:



class RandomNumber:
def on_get(self, req, resp):
"""Handles GET requests"""

if 'min' not in req.params:
raise falcon.HTTPMissingParam('min')

if 'max' not in req.params:
raise falcon.HTTPMissingParam('max')

min_n = req.params['min']
max_n = req.params['max']

if min_n.isnumeric() == False:
raise falcon.HTTPInvalidParam('min must be number', min_n)

if max_n.isnumeric() == False:
raise falcon.HTTPInvalidParam('max must be number', max_n)

min_n =int(min_n)
max_n =int(max_n)

if min_n > max_n:
raise falcon.HTTPInvalidParam('min is bigger than max', (min_n, max_n))

number = random.randint(min_n, max_n)
result = 'lowerLimit': min_n, 'higherLimit': max_n, 'number': number
resp.media = result

api = falcon.API()
api.add_route('/random-number', RandomNumber())


Feedback appreciated.







share|improve this question

























    up vote
    3
    down vote

    favorite












    I have written REST API for random number generation using Falcon(https://falconframework.org/), just for my learning process.



    This is API:



    http "localhost:8000/random-number?min=10&max=10"


    This is code:



    class RandomNumber:
    def on_get(self, req, resp):
    """Handles GET requests"""

    if 'min' not in req.params:
    raise falcon.HTTPMissingParam('min')

    if 'max' not in req.params:
    raise falcon.HTTPMissingParam('max')

    min_n = req.params['min']
    max_n = req.params['max']

    if min_n.isnumeric() == False:
    raise falcon.HTTPInvalidParam('min must be number', min_n)

    if max_n.isnumeric() == False:
    raise falcon.HTTPInvalidParam('max must be number', max_n)

    min_n =int(min_n)
    max_n =int(max_n)

    if min_n > max_n:
    raise falcon.HTTPInvalidParam('min is bigger than max', (min_n, max_n))

    number = random.randint(min_n, max_n)
    result = 'lowerLimit': min_n, 'higherLimit': max_n, 'number': number
    resp.media = result

    api = falcon.API()
    api.add_route('/random-number', RandomNumber())


    Feedback appreciated.







    share|improve this question





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I have written REST API for random number generation using Falcon(https://falconframework.org/), just for my learning process.



      This is API:



      http "localhost:8000/random-number?min=10&max=10"


      This is code:



      class RandomNumber:
      def on_get(self, req, resp):
      """Handles GET requests"""

      if 'min' not in req.params:
      raise falcon.HTTPMissingParam('min')

      if 'max' not in req.params:
      raise falcon.HTTPMissingParam('max')

      min_n = req.params['min']
      max_n = req.params['max']

      if min_n.isnumeric() == False:
      raise falcon.HTTPInvalidParam('min must be number', min_n)

      if max_n.isnumeric() == False:
      raise falcon.HTTPInvalidParam('max must be number', max_n)

      min_n =int(min_n)
      max_n =int(max_n)

      if min_n > max_n:
      raise falcon.HTTPInvalidParam('min is bigger than max', (min_n, max_n))

      number = random.randint(min_n, max_n)
      result = 'lowerLimit': min_n, 'higherLimit': max_n, 'number': number
      resp.media = result

      api = falcon.API()
      api.add_route('/random-number', RandomNumber())


      Feedback appreciated.







      share|improve this question











      I have written REST API for random number generation using Falcon(https://falconframework.org/), just for my learning process.



      This is API:



      http "localhost:8000/random-number?min=10&max=10"


      This is code:



      class RandomNumber:
      def on_get(self, req, resp):
      """Handles GET requests"""

      if 'min' not in req.params:
      raise falcon.HTTPMissingParam('min')

      if 'max' not in req.params:
      raise falcon.HTTPMissingParam('max')

      min_n = req.params['min']
      max_n = req.params['max']

      if min_n.isnumeric() == False:
      raise falcon.HTTPInvalidParam('min must be number', min_n)

      if max_n.isnumeric() == False:
      raise falcon.HTTPInvalidParam('max must be number', max_n)

      min_n =int(min_n)
      max_n =int(max_n)

      if min_n > max_n:
      raise falcon.HTTPInvalidParam('min is bigger than max', (min_n, max_n))

      number = random.randint(min_n, max_n)
      result = 'lowerLimit': min_n, 'higherLimit': max_n, 'number': number
      resp.media = result

      api = falcon.API()
      api.add_route('/random-number', RandomNumber())


      Feedback appreciated.









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 2 at 19:08









      WebOrCode

      1184




      1184




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I don't have many great suggestions, but I'll offer a few minor thoughts. If someone else comes along with a better answer, they're free to incorporate these:




          • Spaces after equals signs:



            min_n = int(min_n) # <-- not "=int..."



          • You have some code duplication in parsing an integer HTTP argument. This could be encapsulated like so:



            def numeric_param(req, name):
            try:
            return int(req.params[name])
            except KeyError:
            raise falcon.HTTPMissingParam(name)
            except ValueError:
            raise falcon.HTTPInvalidParam(' must be an integer'.format(name), val)

            class RandomNumber:
            def on_get(self, req, resp):
            """Handles GET requests"""

            min_n = numeric_param(req, 'min')
            max_n = numeric_param(req, 'max')


            Note too that I've taken the slightly more standard (in Python) approach of "try and fail gracefully" rather than explicitly checking if I think I'll be able to succeed; e.g., I just index params and catch a KeyError if it's not there, and I convert the result to an int and catch a ValueError if it's not possible.



          • I've more often seen "upper limit" as opposed to "higher limit".


          Again, these are just minor suggestions, I really don't know how the code could be substantially better to accomplish the same task.






          share|improve this answer




























            up vote
            2
            down vote













            This is a small code, but there are some improvements to make:




            1. useless copy-paste, what if you had 5 conditions?:



              if 'min' not in req.params:
              raise falcon.HTTPMissingParam('min')

              if 'max' not in req.params:
              raise falcon.HTTPMissingParam('max')


            when you can do:



            for c in ['min','max']:
            if c not in req.params:
            raise falcon.HTTPMissingParam(c)



            1. don't test with == False



              if min_n.isnumeric() == False:


            should be



             if not min_n.isnumeric():





            share|improve this answer





















            • I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
              – WebOrCode
              May 3 at 14:07










            • it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
              – Jean-François Fabre
              May 3 at 14:18

















            up vote
            1
            down vote













            Because the answers before me were great,
            I would just like to put a little emphasis on EAFP and duck typing concepts.



            When new (and veteran) developers are exposed to Python, they usually bring the influence they have experienced from other languages.



            And that's why they miss all the power of language,
            And the truth is that it almost always happens when you switch languages.



            As they have already said in the previous answers, Python has a lot of power and convenience to deal with errors in a dynamic way. You do not have to check everything five times before trying it, as they say "If it walks like a duck and then it has to be a duck." (Duck typing concept).
            Any way with this concept the code look more clear and even pretty.



            In your case this is just a small example of the subject of power of language. No matter what language you're programming, always check that you're using the power of language for your own good.



            In python case, try to find the pythonic way to do something, as the Zen of Python says:



            "There should be one-- and preferably only one --obvious way to do it."



            Sharing a link about Duck typing/EAFP:
            https://youtu.be/x3v9zMX1s4s






            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%2f193489%2frest-api-for-random-number-generation-using-falcon%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              I don't have many great suggestions, but I'll offer a few minor thoughts. If someone else comes along with a better answer, they're free to incorporate these:




              • Spaces after equals signs:



                min_n = int(min_n) # <-- not "=int..."



              • You have some code duplication in parsing an integer HTTP argument. This could be encapsulated like so:



                def numeric_param(req, name):
                try:
                return int(req.params[name])
                except KeyError:
                raise falcon.HTTPMissingParam(name)
                except ValueError:
                raise falcon.HTTPInvalidParam(' must be an integer'.format(name), val)

                class RandomNumber:
                def on_get(self, req, resp):
                """Handles GET requests"""

                min_n = numeric_param(req, 'min')
                max_n = numeric_param(req, 'max')


                Note too that I've taken the slightly more standard (in Python) approach of "try and fail gracefully" rather than explicitly checking if I think I'll be able to succeed; e.g., I just index params and catch a KeyError if it's not there, and I convert the result to an int and catch a ValueError if it's not possible.



              • I've more often seen "upper limit" as opposed to "higher limit".


              Again, these are just minor suggestions, I really don't know how the code could be substantially better to accomplish the same task.






              share|improve this answer

























                up vote
                2
                down vote



                accepted










                I don't have many great suggestions, but I'll offer a few minor thoughts. If someone else comes along with a better answer, they're free to incorporate these:




                • Spaces after equals signs:



                  min_n = int(min_n) # <-- not "=int..."



                • You have some code duplication in parsing an integer HTTP argument. This could be encapsulated like so:



                  def numeric_param(req, name):
                  try:
                  return int(req.params[name])
                  except KeyError:
                  raise falcon.HTTPMissingParam(name)
                  except ValueError:
                  raise falcon.HTTPInvalidParam(' must be an integer'.format(name), val)

                  class RandomNumber:
                  def on_get(self, req, resp):
                  """Handles GET requests"""

                  min_n = numeric_param(req, 'min')
                  max_n = numeric_param(req, 'max')


                  Note too that I've taken the slightly more standard (in Python) approach of "try and fail gracefully" rather than explicitly checking if I think I'll be able to succeed; e.g., I just index params and catch a KeyError if it's not there, and I convert the result to an int and catch a ValueError if it's not possible.



                • I've more often seen "upper limit" as opposed to "higher limit".


                Again, these are just minor suggestions, I really don't know how the code could be substantially better to accomplish the same task.






                share|improve this answer























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  I don't have many great suggestions, but I'll offer a few minor thoughts. If someone else comes along with a better answer, they're free to incorporate these:




                  • Spaces after equals signs:



                    min_n = int(min_n) # <-- not "=int..."



                  • You have some code duplication in parsing an integer HTTP argument. This could be encapsulated like so:



                    def numeric_param(req, name):
                    try:
                    return int(req.params[name])
                    except KeyError:
                    raise falcon.HTTPMissingParam(name)
                    except ValueError:
                    raise falcon.HTTPInvalidParam(' must be an integer'.format(name), val)

                    class RandomNumber:
                    def on_get(self, req, resp):
                    """Handles GET requests"""

                    min_n = numeric_param(req, 'min')
                    max_n = numeric_param(req, 'max')


                    Note too that I've taken the slightly more standard (in Python) approach of "try and fail gracefully" rather than explicitly checking if I think I'll be able to succeed; e.g., I just index params and catch a KeyError if it's not there, and I convert the result to an int and catch a ValueError if it's not possible.



                  • I've more often seen "upper limit" as opposed to "higher limit".


                  Again, these are just minor suggestions, I really don't know how the code could be substantially better to accomplish the same task.






                  share|improve this answer













                  I don't have many great suggestions, but I'll offer a few minor thoughts. If someone else comes along with a better answer, they're free to incorporate these:




                  • Spaces after equals signs:



                    min_n = int(min_n) # <-- not "=int..."



                  • You have some code duplication in parsing an integer HTTP argument. This could be encapsulated like so:



                    def numeric_param(req, name):
                    try:
                    return int(req.params[name])
                    except KeyError:
                    raise falcon.HTTPMissingParam(name)
                    except ValueError:
                    raise falcon.HTTPInvalidParam(' must be an integer'.format(name), val)

                    class RandomNumber:
                    def on_get(self, req, resp):
                    """Handles GET requests"""

                    min_n = numeric_param(req, 'min')
                    max_n = numeric_param(req, 'max')


                    Note too that I've taken the slightly more standard (in Python) approach of "try and fail gracefully" rather than explicitly checking if I think I'll be able to succeed; e.g., I just index params and catch a KeyError if it's not there, and I convert the result to an int and catch a ValueError if it's not possible.



                  • I've more often seen "upper limit" as opposed to "higher limit".


                  Again, these are just minor suggestions, I really don't know how the code could be substantially better to accomplish the same task.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 2 at 19:25









                  scnerd

                  6438




                  6438






















                      up vote
                      2
                      down vote













                      This is a small code, but there are some improvements to make:




                      1. useless copy-paste, what if you had 5 conditions?:



                        if 'min' not in req.params:
                        raise falcon.HTTPMissingParam('min')

                        if 'max' not in req.params:
                        raise falcon.HTTPMissingParam('max')


                      when you can do:



                      for c in ['min','max']:
                      if c not in req.params:
                      raise falcon.HTTPMissingParam(c)



                      1. don't test with == False



                        if min_n.isnumeric() == False:


                      should be



                       if not min_n.isnumeric():





                      share|improve this answer





















                      • I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                        – WebOrCode
                        May 3 at 14:07










                      • it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                        – Jean-François Fabre
                        May 3 at 14:18














                      up vote
                      2
                      down vote













                      This is a small code, but there are some improvements to make:




                      1. useless copy-paste, what if you had 5 conditions?:



                        if 'min' not in req.params:
                        raise falcon.HTTPMissingParam('min')

                        if 'max' not in req.params:
                        raise falcon.HTTPMissingParam('max')


                      when you can do:



                      for c in ['min','max']:
                      if c not in req.params:
                      raise falcon.HTTPMissingParam(c)



                      1. don't test with == False



                        if min_n.isnumeric() == False:


                      should be



                       if not min_n.isnumeric():





                      share|improve this answer





















                      • I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                        – WebOrCode
                        May 3 at 14:07










                      • it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                        – Jean-François Fabre
                        May 3 at 14:18












                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      This is a small code, but there are some improvements to make:




                      1. useless copy-paste, what if you had 5 conditions?:



                        if 'min' not in req.params:
                        raise falcon.HTTPMissingParam('min')

                        if 'max' not in req.params:
                        raise falcon.HTTPMissingParam('max')


                      when you can do:



                      for c in ['min','max']:
                      if c not in req.params:
                      raise falcon.HTTPMissingParam(c)



                      1. don't test with == False



                        if min_n.isnumeric() == False:


                      should be



                       if not min_n.isnumeric():





                      share|improve this answer













                      This is a small code, but there are some improvements to make:




                      1. useless copy-paste, what if you had 5 conditions?:



                        if 'min' not in req.params:
                        raise falcon.HTTPMissingParam('min')

                        if 'max' not in req.params:
                        raise falcon.HTTPMissingParam('max')


                      when you can do:



                      for c in ['min','max']:
                      if c not in req.params:
                      raise falcon.HTTPMissingParam(c)



                      1. don't test with == False



                        if min_n.isnumeric() == False:


                      should be



                       if not min_n.isnumeric():






                      share|improve this answer













                      share|improve this answer



                      share|improve this answer











                      answered May 2 at 19:22









                      Jean-François Fabre

                      783210




                      783210











                      • I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                        – WebOrCode
                        May 3 at 14:07










                      • it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                        – Jean-François Fabre
                        May 3 at 14:18
















                      • I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                        – WebOrCode
                        May 3 at 14:07










                      • it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                        – Jean-François Fabre
                        May 3 at 14:18















                      I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                      – WebOrCode
                      May 3 at 14:07




                      I do not know why, but for me, it is much easier to see/understand min_n.isnumeric() == False than not min_n.isnumeric(). That is why I am still using X == False instead of not X.
                      – WebOrCode
                      May 3 at 14:07












                      it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                      – Jean-François Fabre
                      May 3 at 14:18




                      it's because it's bad to do == True. Read stackoverflow.com/a/4050625/6451573
                      – Jean-François Fabre
                      May 3 at 14:18










                      up vote
                      1
                      down vote













                      Because the answers before me were great,
                      I would just like to put a little emphasis on EAFP and duck typing concepts.



                      When new (and veteran) developers are exposed to Python, they usually bring the influence they have experienced from other languages.



                      And that's why they miss all the power of language,
                      And the truth is that it almost always happens when you switch languages.



                      As they have already said in the previous answers, Python has a lot of power and convenience to deal with errors in a dynamic way. You do not have to check everything five times before trying it, as they say "If it walks like a duck and then it has to be a duck." (Duck typing concept).
                      Any way with this concept the code look more clear and even pretty.



                      In your case this is just a small example of the subject of power of language. No matter what language you're programming, always check that you're using the power of language for your own good.



                      In python case, try to find the pythonic way to do something, as the Zen of Python says:



                      "There should be one-- and preferably only one --obvious way to do it."



                      Sharing a link about Duck typing/EAFP:
                      https://youtu.be/x3v9zMX1s4s






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Because the answers before me were great,
                        I would just like to put a little emphasis on EAFP and duck typing concepts.



                        When new (and veteran) developers are exposed to Python, they usually bring the influence they have experienced from other languages.



                        And that's why they miss all the power of language,
                        And the truth is that it almost always happens when you switch languages.



                        As they have already said in the previous answers, Python has a lot of power and convenience to deal with errors in a dynamic way. You do not have to check everything five times before trying it, as they say "If it walks like a duck and then it has to be a duck." (Duck typing concept).
                        Any way with this concept the code look more clear and even pretty.



                        In your case this is just a small example of the subject of power of language. No matter what language you're programming, always check that you're using the power of language for your own good.



                        In python case, try to find the pythonic way to do something, as the Zen of Python says:



                        "There should be one-- and preferably only one --obvious way to do it."



                        Sharing a link about Duck typing/EAFP:
                        https://youtu.be/x3v9zMX1s4s






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Because the answers before me were great,
                          I would just like to put a little emphasis on EAFP and duck typing concepts.



                          When new (and veteran) developers are exposed to Python, they usually bring the influence they have experienced from other languages.



                          And that's why they miss all the power of language,
                          And the truth is that it almost always happens when you switch languages.



                          As they have already said in the previous answers, Python has a lot of power and convenience to deal with errors in a dynamic way. You do not have to check everything five times before trying it, as they say "If it walks like a duck and then it has to be a duck." (Duck typing concept).
                          Any way with this concept the code look more clear and even pretty.



                          In your case this is just a small example of the subject of power of language. No matter what language you're programming, always check that you're using the power of language for your own good.



                          In python case, try to find the pythonic way to do something, as the Zen of Python says:



                          "There should be one-- and preferably only one --obvious way to do it."



                          Sharing a link about Duck typing/EAFP:
                          https://youtu.be/x3v9zMX1s4s






                          share|improve this answer













                          Because the answers before me were great,
                          I would just like to put a little emphasis on EAFP and duck typing concepts.



                          When new (and veteran) developers are exposed to Python, they usually bring the influence they have experienced from other languages.



                          And that's why they miss all the power of language,
                          And the truth is that it almost always happens when you switch languages.



                          As they have already said in the previous answers, Python has a lot of power and convenience to deal with errors in a dynamic way. You do not have to check everything five times before trying it, as they say "If it walks like a duck and then it has to be a duck." (Duck typing concept).
                          Any way with this concept the code look more clear and even pretty.



                          In your case this is just a small example of the subject of power of language. No matter what language you're programming, always check that you're using the power of language for your own good.



                          In python case, try to find the pythonic way to do something, as the Zen of Python says:



                          "There should be one-- and preferably only one --obvious way to do it."



                          Sharing a link about Duck typing/EAFP:
                          https://youtu.be/x3v9zMX1s4s







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered May 3 at 6:17









                          SpazaM

                          112




                          112






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193489%2frest-api-for-random-number-generation-using-falcon%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Python Lists

                              Aion

                              JavaScript Array Iteration Methods