Skip to content

Commit

Permalink
Add method empty() to the API of Population (#268)
Browse files Browse the repository at this point in the history
And make existing `__bool__()` method use it.
  • Loading branch information
akosthekiss authored Jan 26, 2025
1 parent c5c1dc4 commit b30d9f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 13 additions & 4 deletions grammarinator/runtime/population.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024 Renata Hodovan, Akos Kiss.
# Copyright (c) 2023-2025 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand All @@ -14,17 +14,26 @@ class Population:
individuals) and can select trees for mutation or recombination based on some strategy.
"""

def __bool__(self):
def empty(self):
"""
Truth value testing of Populations.
Return whether the population is empty.
Raises :exc:`NotImplementedError` by default.
:return: ``True`` if the population is not empty and ``False`` otherwise.
:return: ``True`` if the population is empty and ``False`` otherwise.
:rtype: bool
"""
raise NotImplementedError()

def __bool__(self):
"""
Truth value testing of Populations.
:return: ``True`` if the population is not empty and ``False`` otherwise.
:rtype: bool
"""
return not self.empty()

def add_individual(self, root, path=None):
"""
Add a tree to the population.
Expand Down
8 changes: 4 additions & 4 deletions grammarinator/tool/default_population.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024 Renata Hodovan, Akos Kiss.
# Copyright (c) 2023-2025 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand Down Expand Up @@ -39,11 +39,11 @@ def __init__(self, directory, extension, codec=None):
os.makedirs(directory, exist_ok=True)
self._files = glob.glob(join(self._directory, f'*.{self._extension}'))

def __bool__(self):
def empty(self):
"""
Check whether there is at least a single individual in the population.
Check whether the population contains no individuals.
"""
return len(self._files) > 0
return len(self._files) == 0

def add_individual(self, root, path=None):
"""
Expand Down

0 comments on commit b30d9f9

Please sign in to comment.