Hi, using web2py, frequently I write functions that will do stuff with
specific database records.

When writing functions that will deal with specific database records,
I want to write them to be flexible on the way I may reference the
record I want. I mean, I may pass different kinds of arguments to the
function and, as long as this argument is sufficient to identify a
record, the function should figure it out how to retrieve the record.

For example, if I call myfunc("some string"), the function will query
db for a record with title==arg, if I have an object that has enough
information for the function to build a query, I want to be able to
call it myfunc(myobj). If I have an ID returned from a database
insertion, I want to be able to pass it as myfunc(id), if I have the
record itself, I want to be able to pass it also, like
myfunc(rows.first()), and so on..

My doubt is how to reliably identify if the argument is from web2py. I
thought in something like:


def somefunc(arg1)
    if isinstance(arg1, string):
        myrecord = db().select(db.mytable.myfield == arg1)
    if isinstance(arg1, myclass):
        myrecord = db.mytable(myclass.record_id)
    elif IS_WEB2PY_ID(arg1):
        myrecord = db.mytable(arg1)
    elif IS_WEB2PY_RECORD(arg1):
        myrecord = arg1
    # do whatever with myrecord


So, how could be the test for IS_WEB2PY_ID() ? I took a look at web2py
source and saw that insert() may return many things, it may be an
integer id, a dict, a tuple... I also got a bit confused when tables
uses custom primarykeys.

And about IS_WEB2PY_RECORD() ? Test if argument is an instance of Row
class is enough?

Regards,
Fabiano

Reply via email to