开发者

How to get cursor from mqlread() when using Freebase API?

I'm using the Python-Freebase module's mqlread(). Using the following query:

query = [{
  "cursor": True,
  "id":     None,
  "type":   "/games/game",
  "mid":    None,
}]

That returns 100 entries, but the result does not include "cursor". If you run th开发者_开发百科e query manually you get something like:

{ "code": "/api/status/ok", "cursor": "eNqFj8FqwzAQRD-mF4sisrtaaaWlhP6H8MHYNTUEW8hpKP36KjgU2kvnMoeZ4THjR923qhKS07wpAvR5VZbYBIL9sE76FDGAs-yCA_FISprL_nWUt5tC383L59ukh9llH_TvpL7Y7rYUdZ4CN9iRITB6vTwnAhriCAwAgXGO8Etkz48dicnvy3ptTGO6OwgsBwIfwJNeHpDWowgh_URO63-M0Z7vM0neGZNL3RrihBKFU_uZS2l3sT-9cpIBaRrnOE8JhaAhQL4B9ZFRmg==", "result": [

{
  "id":   "/en/cities_and_knights_of_catan",
  "mid":  "/m/02hq3",
  "type": "/games/game"
},
....

Which contains the "cursor". But the result from:

freebase.mqlread(query, extended=True)

Will return:

[{u'type': u'/games/game', u'id': u'/en/cities_and_knights_of_catan'}, ...

Which strips out "code" and "cursor". How can I get the "cursor"?


I found that the Freebase-Python module actually includes a method that takes care of cursoring for you. Just call mqlreaditer(query, extended=true), and that will return a Python generator that you can iterate through. Behind the scenes the Freebase module will query and keep track of the cursor, return results on demand until a "false" cursor is reached.

Very simple!


Support for MQL envelope parameters was dropped, presumably accidentally, in the 1.0.8 release of the Freebase Python library.

If not don't need MQL extensions, you can just drop the parameter extended=True parameter and use mqlreaditer(query).

If you need MQL extensions (or other envelope parameters), you'll have to fall back to 1.0.6 until someone fixes the problem and does a new release.

 sudo easy_install freebase==1.0.6
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜