Skip to content

Commit

Permalink
add head and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatYYX committed Dec 7, 2018
1 parent f6ff635 commit 9a46196
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions rltk/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,39 @@ def generate_dataframe(self, size: int = None, **kwargs):

return pd.DataFrame(table, columns=columns, **kwargs)

def head(self, n: int = 10):
"""
Iterate on first n records.
Args:
n (int, optional): Number of records. Default to 10.
Returns:
iter: Record
"""
for idx, r in enumerate(self.__next__()):
if idx >= n:
break
yield r

def filter(self, condition: Callable, n: int = 10):
"""
Iterate on last n records.
Args:
condition (Callable): `function(r: Record) -> bool`. If the return is True, record will be selected.
n (int, optional): Number of records. Default to 10.
Returns:
iter: Record
"""
counter = 0
for r in self.__next__():
if counter >= n:
break
if condition(r):
yield r

def __iter__(self):
"""
Same as :meth:`__next__`
Expand Down

0 comments on commit 9a46196

Please sign in to comment.