Recommendations (or best practices) to define functions (or methods)
Hello: Need your help in the "correct" definition of the next function. If necessary, I would like to know about a web site or documentation that tells me about best practices in defining functions, especially for those that consider the error exceptions management. I have the next alternatives but I think there are better: Alternative 1: = def ExecuteSQL(cmdSQL, cursor): try: cursor.execute(cmdSQL) except Exception, e: return e return 1 Seems a good solution but the function is not returning an uniform type, should the code that receives the function result decides the error display according of datatype? If I do this in C# I think I will have problems. Alternative 2: = def ExecuteSQL(cmdSQL, cursor): try: cursor.execute(cmdSQL) except Exception, e: return 0, e # or return (0,e.message) return 1, "ok"# or return (1, "ok") Sounds good, but seems forced. When doing return 1, "ok" I am doing redundancy and I have problems with the type (e is exception type and "ok" is string). Alternative 3: = def ExecuteSQL(cmdSQL, cursor): try: cursor.execute(cmdSQL) except Exception, e: print "ERROR:", e return 0 return 1 It solves the problem of alternative 1 and 2, but the print keyword would have problems if the function is called from an WEB or GUI application and, because there are plans to convert print into a function in py3k. Alternative 4: = def ExecuteSQL(cmdSQL, cursor): try: cursor.execute(cmdSQL) except Exception, e: print >> logerr, e.message return 0 return 1 Seems a good solution but I should always be persuaded to have an open global file (logerr) when invoking this function; otherwise code will generate another error. The function is not totally independent.I think I would be in the same situation when using the "logging" battery. Thanks for your attention. Regards, -- http://mail.python.org/mailman/listinfo/python-list
Re: Recommendations (or best practices) to define functions (or methods)
Martin v. Löwis ha escrito: > vizcayno schrieb: > > Need your help in the "correct" definition of the next function. If > > necessary, I would like to know about a web site or documentation that > > tells me about best practices in defining functions, especially for > > those that consider the error exceptions management. > > I agree with George Sakkis' remarks. The best way to define this function is > > def ExecuteSQL(cmdSQL, cursor): > return cursor.execute(cmdSQL) > > If this raises an exception, it likely means there is something > wrong with the SQL statement. The program should abort, and the > developer should correct it. > > Regards, > Martin Martin: Thanks for your indications. However, what happens when the error is due to data error. Or when the program is reading many files to save data into a database and one or two files have problems with data format. I would like to keep the program running (using exception in a controlled way) for the remaining good files and prepare a log about the failed files. How to keep the same function for a program that runs in batch, or on-line or in the web? Giving the simple example I put, I would like to find more guidelines. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Recommendations (or best practices) to define functions (or methods)
Diez B. Roggisch ha escrito: > vizcayno schrieb: > > Hello: > > Need your help in the "correct" definition of the next function. If > > necessary, I would like to know about a web site or documentation that > > tells me about best practices in defining functions, especially for > > those that consider the error exceptions management. > > I have the next alternatives but I think there are better: > > > > IMHO none of them is good. Python has exceptions. Use them. There is no > need to awkwardly communicate error conditions using return-values. Use > return values to return values. Use exceptions in case of errors. > > Diez Diez, in that case I woul prefer not to use exceptions and wait for Python to abort itself and wait to see the message it issues. -- http://mail.python.org/mailman/listinfo/python-list
Re: Recommendations (or best practices) to define functions (or methods)
Diez B. Roggisch ha escrito: > vizcayno schrieb: > > Hello: > > Need your help in the "correct" definition of the next function. If > > necessary, I would like to know about a web site or documentation that > > tells me about best practices in defining functions, especially for > > those that consider the error exceptions management. > > I have the next alternatives but I think there are better: > > > > IMHO none of them is good. Python has exceptions. Use them. There is no > need to awkwardly communicate error conditions using return-values. Use > return values to return values. Use exceptions in case of errors. > > Diez Diez, in that case I woul prefer not to use exceptions and wait for Python to abort itself and wait to see the message it issues. -- http://mail.python.org/mailman/listinfo/python-list
Re: Recommendations (or best practices) to define functions (or methods)
Thank you for your guidelines and for changing my mind. I am trying to do my best in generating good code and, in that attempt ... I only know that nothing know. Regards. -- http://mail.python.org/mailman/listinfo/python-list
Need guidelines to show results of a process
Hi: I wrote a Python program which, during execution, shows me messages on console indicating at every moment the time and steps being performed so I can have a 'log online' and guess remaining time for termination, I used many 'print' instructions to show those messages, i.e. print "I am in step 4 at "+giveTime() print "I am in step 5 at "+giveTime(), etc. Now I need to execute the same program but from a GUI application. I must show the same messages but on a "text field". As you can guess, it implies the changing of my program or make a copy and replace the print instructions by textField += "I am in step 4 at "+giveTime() then textField += "I am in step 5 at "+giveTime(), etc. I wanted to do the next: if output == "GUI": textField += "I am in step 4 at "+giveTime() force_output() else: print "I am in step 4 at "+giveTime() But it is not smart, elegant, clean ... isn't it? Any ideas please? Regards. -- http://mail.python.org/mailman/listinfo/python-list