1

I have a function in python that does error checking on a string. If the function finds an error it will return a tuple with an error code and an identifier for that error. Later in the program I have the function that handles the error. It accepts the tuple into two variables.

errCode, i = check_string(s,pathType)

check_string is the function that produces the tuple. What I want it to does is return just zero if there is no error. When a zero is returned the above code seems to fail because there is nothing for the variable i.

Is there an easy way to get this to work or do I just need to have the program return a tuple of 0 and 0 if no error is found?

Levon
  • 138,105
  • 33
  • 200
  • 191
Reimus Klinsman
  • 898
  • 2
  • 12
  • 23

4 Answers4

3

I would use the exception system for this.

class StringError(Exception):
    NO_E = 0
    HAS_Z = 1

def string_checker(string):
    if 'e' not in string:
        raise StringError('e not found in string', StringError.NO_E)
    if 'z' in string:
        raise StringError('z not allowed in string', StringError.HAS_Z)
    return string.upper()

s = 'testing'
try:
    ret = string_checker(s)
    print 'String was okay:', ret
except StringError as e:
    print 'String not okay with an error code of', e.args[1]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 1
    Maybe I'm not getting the problem, but all this to simply check if the tuple contains one or two values? *Not* meant as a criticism, and clearly using the exception mechanism has its place (given EAFP), but seems more complex than necessary. – Levon Jul 29 '12 at 10:52
  • 1
    @Levon Returning error values is rather C-ish and not very Pythonic. Maybe I've read too much in-between the lines, but given the OP's first paragraph, my first thought was "use Exceptions" - in this case, `string_checker` returns a valid string (without using tuples and codes), otherwise raises, which can be handled where ever required... Just seems a "cleaner" design to me. – Jon Clements Jul 29 '12 at 10:58
  • 1
    @Levon You're welcome. It's also easy to "forget" to check error codes leading to something breaking later, or even worse, being a silent bug that does "who knows what" over time... – Jon Clements Jul 29 '12 at 11:07
  • Yes, that's a very valid point, and a definite advantage of exceptions, they won't let you forget them for sure :) and require to be handled. I guess it's a matter of when and where to use them, but I understand your reasons for this solution much better now. – Levon Jul 29 '12 at 11:11
  • Great. This is pretty much what I need. – Reimus Klinsman Jul 30 '12 at 00:32
1

Why not simply check the length of the tuple before trying to retrieve the values?

err_tup = check_String(s, pathType)
if len(err_tup) == 2:
    errCode, i = err_tup
else: # assuming it can only be 1 otherwise
    errCode = err_tup

This would work without making any changes to the rest of your code that generates the tuple, and is semantically clear.

Based on "Simple is better than complex."

Levon
  • 138,105
  • 33
  • 200
  • 191
1

You could use None as the value for error

def check_string(s,pathType):
    # No error
    return (None, i)
0

If you are going to return zero:

foo = func()
if not foo:
    print 'No errors'
else:
    # foo is tuple
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284