philipjkim
philipjkim

Reputation: 4139

Type validation for cx_Oracle.OracleCursor

This could be very basic question. I'd like to validate a variable is type of cx_Oracle.OracleCursor, so I tried a couple of trials but all failed:

import cx_Oracle

def execute_sql(cursor):
    """Execute a SQL using cursor(cx_Oracle.OracleCursor)."""
    if not cursor:
        logging.error('cursor is null.')
        return None  # TODO: raise some exception.
    # elif isinstance(cursor, OracleCursor): # failed
    # elif isinstance(cursor, cx_Oracle.OracleCursor): # failed
        logging.error('cursor is not OracleCursor.')
        return None  # TODO: raise some exception.
# ...

How can I validate the type of cursor?

And the additional question is what exception/error is suitable for invalid type error?

Thanks for any help!

Upvotes: 1

Views: 575

Answers (2)

JoshL
JoshL

Reputation: 10988

This works for me:

if isinstance(cursor, cx_Oracle.Cursor):
    ...

Upvotes: 1

Michał Niklas
Michał Niklas

Reputation: 54302

You can check type of variable using type(variable):

if str(type(cursor)) == "<type 'OracleCursor'>":
       ...

PS I know that isinstance() is recommended.

Upvotes: 1

Related Questions