Skip to content

Commit 6e44ffd

Browse files
committed
Added a decorator that can be used to automatically increase the counter variable, in objects that inherit GeneticOperator.
1 parent 218fc69 commit 6e44ffd

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pygenalgo/operators/genetic_operator.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from numpy.random import default_rng, Generator
33

44
# Public interface.
5-
__all__ = ["GeneticOperator"]
5+
__all__ = ["GeneticOperator", "increase_counter"]
66

77
# Author.
88
__author__ = "Michalis Vrettas, PhD"
@@ -11,6 +11,27 @@
1111
__email__ = "[email protected]"
1212

1313

14+
def increase_counter(method):
15+
"""
16+
Decorator function that is used in the derived
17+
classes main operation to increase the counter
18+
by one.
19+
20+
:param method: that we wrapp its functionality.
21+
22+
:return: the wrapper function.
23+
"""
24+
def wrapper(self, *args, **kwargs):
25+
# Increase the counter.
26+
self.inc_counter()
27+
28+
# Return the output of the wrapped method.
29+
return method(self, *args, **kwargs)
30+
# _end_def_
31+
return wrapper
32+
# _end_def_
33+
34+
1435
class GeneticOperator(object):
1536
"""
1637
Description:

0 commit comments

Comments
 (0)