简体   繁体   中英

How can I increase code readability in python for this?

I am running my script in Flask and to catch and print the errors on the server I have made functions that either return None if succeeded else return an error message .

The problem is I have many functions which runs one after another and uses global variable from earlier function, this makes the code unorganized. What can I do?

App.py

from flask import Flask
from main import *


app = Flask(__name__)
@app.route('/')
def main():
    input = request.args.get('input')
    first_response = function1(input)

    if first_response is None:
        second_response = function2() # no input from hereon

        if second_response is None:
            third_response = function3() # functions are imported from main.py

            if third_response is None:
            ...
               if ...

               else ...
            else: 
                return third_response
        else:
             return second_response
    else:
        return first_response

main.py

def function1(input):
    global new_variable1
    if input is valid:
        new_variable1 = round(input,2)
    else:
        return "the value is not integer"

def function2():
    global new_variable2
    if new_variable1 > 8:
         new_variable2 = new_variable1 / 8
    else: 
       return "the division is not working, value is 0"

def function3():
...

This is just a demo of what's going on. The last function will return a value either side. So if everything goes right I would be able to see the right output as well as I will see error on any given function.

The code works fine, but I need better alternative to do this.

Thanks!

Ah...you have (correctly) determined that you have two things to do:

  1. Process your data, and
  2. Deal with errors.

So let's process the data replacing global with parameteters (and come back to the error handling in a bit). You want to do something like this.

main.py

    def function1(some_number):
        if some_number is valid:
            return round(some_number, 2)

    def function2(a_rounded_number):
        if a_rounded_number > 8:
            return a_rounded_number / 8

So each function should return the results of its work. Then the calling routine can just send the results of each function to the next function, like this:

app.py


   # [code snipped]

   result1 = function1(the_input_value)
   result2 = function2(result1)
   result3 = function3(result2)

But...how do we deal with unexpected or error conditions? We use exceptions, like this:

main.py

    def function1(some_number):
        if some_number is valid:
            return round(some_number, 2)
        else:
            raise ValueError("some_number was not valid")
     

and then in the calling routine

app.py


   try:
       result1 = function1(some_input_value)
   except (ValueError as some_exception):
       return str(some_exception)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM