Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get random() or recentchanges() to work #187

Open
nnt0 opened this issue Mar 17, 2018 · 3 comments
Open

Can't get random() or recentchanges() to work #187

nnt0 opened this issue Mar 17, 2018 · 3 comments

Comments

@nnt0
Copy link

nnt0 commented Mar 17, 2018

Hi,

im trying to use random() and recentchanges() but everytime i print the output of one of them i get this:

>>random = site.random(namespace=0, limit=10)
>>random
<List object 'random' for <Site object 'test.wikipedia.org/w/'>>

and the same for recentchanges()

>>> recent = site.recentchanges()
>>> recent
<List object 'recentchanges' for <Site object 'test.wikipedia.org/w/'>>

I can't figure out what i'm doing wrong. Im using Python 3.
Thanks.

@danmichaelo
Copy link
Member

Hi,

Thesere are iterators, so you must pull items from them. A quick way to convert an iterator to a list is to use list():

random = site.random(namespace=0, limit=10)
list(random)

or you can use

next(random)

to just get the first item.

In general, you will want to process an iterator using a for loop or similar:

for page in site.random(namespace=0, limit=10):
# do something with page

@nnt0
Copy link
Author

nnt0 commented Mar 20, 2018

Thank you for your answer. I tried it and it worked but i get way more than my limit.

Using

for page in site.random(namespace=0, limit=10):
print(page)

gives me way to many sites.

@danmichaelo
Copy link
Member

Ouch, didn't know that. I'm not the original author of this library, I just maintain it. Guess the limit is just the number of items it retrieves in each batch then. That should at least be documented.

In that case you can either do something like this to get the first 10 elements:

pages = site.random(namespace=0)
pages = [next(pages) for x in range(10)]

or use itertools:

import itertools
pages = site.random(namespace=0)
pages = itertools.islice(pages, 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants