问题
Suppose I have the following RethinkDB query (as printed in iPython):
In [32]: data_to_archive
Out[32]: <RqlQuery instance: r.db('sensor_db').table('sensor_data').filter(lambda var_2: (r.row['timestamp'] < (r.now() - r.expr(259200.0)))) >
The query is on the database sensor_db
and table sensor_data
, as is clear from the printed output. Is there any way I can retrieve this information as an attribute of the RqlQuery
instance?
(The reason I want to do this is to write succinct code: the query, the database, and the table are currently all passed separated as input arguments to a function, but the latter two are actually contained in the former).
回答1:
Not sure if it exists. Meanwhile, a little ugliness hasn't really hurt anyone except my mom:
In [57]: mom = lambda q, fn: re.search(fn + "\(\'(.*?)\'\)", str(q)).group(1)
In [58]: rql = r.db("foo").table('bar').filter(lambda x: x)
In [59]: mom(rql, 'db')
Out[59]: 'foo'
In [60]: mom(rql, 'table')
Out[60]: 'bar'
来源:https://stackoverflow.com/questions/40025181/how-to-get-the-database-and-table-name-from-a-reql-query