pyvideo / richard

video indexing site
Other
216 stars 55 forks source link

use of smart_int #289

Closed nadrane closed 9 years ago

nadrane commented 9 years ago

I noticed that the standard type conversion function int is often replaced by smart_int in this codebase. Here is what the code looks like:

def smart_int(value, default=None):
    try:
        return int(value)
    except (TypeError, ValueError):
        pass
    return default

In the 5 times this function is used, not once is the default parameter used. In essence, this function is used to ignore type conversion errors and allow to program to continue with bad data. Granted, the code always checks to see if the return value is None after this function is called, but we're doing ourselves a disservice by not throwing the exception. Exceptions contain valuable information about what went wrong, and I'd think we should log this information rather than intentionally throwing the data away.

Any thoughts?

willkg commented 9 years ago

This is explicitly used for pulling GET/POST data and in all the cases it's used if the data isn't a valid int, then we treat it as if we got no data at all. We can't control user-provided data.

The default part is there because it's nice to have a default. I don't recall if it's used or not, but it's a nice to have thing at some point if one needs it.

Hope that helps!