Skip to content

Commit dd19487

Browse files
Bind Archive.get_random_entry() method
1 parent bcc4d9d commit dd19487

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

libzim/libwrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class Archive : public Wrapper<zim::Archive>
144144
FORWARD(wrapper::Entry, getEntryByPath)
145145
FORWARD(wrapper::Entry, getEntryByTitle)
146146
FORWARD(wrapper::Entry, getMainEntry)
147+
FORWARD(wrapper::Entry, getRandomEntry)
147148
FORWARD(wrapper::Item, getIllustrationItem)
148149
FORWARD(std::set<unsigned int>, getIllustrationSizes)
149150
std::string getUuid() const

libzim/libzim.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,14 @@ cdef class Archive:
11011101
raise KeyError(str(e))
11021102
return Entry.from_entry(move(entry))
11031103
1104+
def get_random_entry(self) -> Entry:
1105+
"""Get a random `Entry`.
1106+
1107+
Returns:
1108+
A random entry.
1109+
"""
1110+
return Entry.from_entry(move(self.c_archive.getRandomEntry()))
1111+
11041112
@property
11051113
def metadata_keys(self) -> List[str]:
11061114
"""List of Metadata keys present in this archive.

libzim/reader.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Archive:
4141
def get_entry_by_path(self, path: str) -> Entry: ...
4242
def has_entry_by_title(self, title: str) -> bool: ...
4343
def get_entry_by_title(self, title: str) -> Entry: ...
44+
def get_random_entry(self) -> Entry: ...
4445
@property
4546
def metadata_keys(self) -> list[str]: ...
4647
def get_metadata_item(self, name: str) -> Item: ...

libzim/zim.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ cdef extern from "libwrapper.h" namespace "wrapper":
153153
vector[string] getMetadataKeys() except +
154154

155155
Entry getMainEntry() except +
156+
Entry getRandomEntry() except +
156157
Item getIllustrationItem() except +
157158
Item getIllustrationItem(int size) except +
158159
size_type getEntryCount() except +

tests/test_libzim_reader.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
import libzim.writer # pyright: ignore [reportMissingModuleSource]
12-
from libzim.reader import Archive # pyright: ignore [reportMissingModuleSource]
12+
from libzim.reader import Archive, Entry # pyright: ignore [reportMissingModuleSource]
1313
from libzim.search import Query, Searcher # pyright: ignore [reportMissingModuleSource]
1414
from libzim.suggestion import ( # pyright: ignore [reportMissingModuleSource]
1515
SuggestionSearcher,
@@ -599,3 +599,11 @@ def filename(self):
599599
assert zim != Different(fpath1)
600600
assert zim == Sub(fpath1)
601601
assert zim != Sub2(fpath1)
602+
603+
604+
@skip_if_offline
605+
@pytest.mark.parametrize(*parametrize_for(["filename"]))
606+
def test_reader_get_random_entry(all_zims, filename):
607+
zim = Archive(all_zims / filename)
608+
609+
assert isinstance(zim.get_random_entry(), Entry)

0 commit comments

Comments
 (0)