-
Notifications
You must be signed in to change notification settings - Fork 46
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
Feature/base reporter #15
Draft
guimorg
wants to merge
12
commits into
develop
Choose a base branch
from
feature/base-reporter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
244207e
Adding README as long_description
guimorg 8d77047
Merge branch 'develop' of github.com:ahmedkhalf/Circle-Evolution into…
guimorg da36f87
Merge branch 'develop' of github.com:ahmedkhalf/Circle-Evolution into…
guimorg e9b9b09
Adds observable Runner object
guimorg 50045fd
Adds base class for reporters - observers and a LoggerReporter
guimorg f9fd45c
Use of Runner and also notify
guimorg db8b35d
Attaches concrete Reporter
guimorg 68e8594
Fixes stylecode
guimorg a4b4138
Fixes Observer Pattern - Uses Object
guimorg a509ca7
Adds two other methods for reporters to deal with on_start and on_sto…
guimorg 634850a
Few changes for notifications - Adds CSV Reporter - Notify all genera…
guimorg 1c349a9
Only log improvements to the screen
guimorg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""Reporters for capturing events and notifying the user about them. | ||
|
||
You can make your own Reporter class or use the already implemented one's. | ||
Notice the abstract class before implementing your Reporter to see which | ||
functions should be implemented. | ||
""" | ||
from abc import ABC, abstractmethod | ||
|
||
import logging | ||
import logging.config | ||
|
||
|
||
class Reporter(ABC): | ||
"""Base Reporter class. | ||
|
||
The Reporter is responsible for capturing particular events and sending | ||
them for visualization. Please, use this class if you want to implement | ||
your own Reporter. | ||
""" | ||
def __init__(self): | ||
"""Initialization calls setup to configure Reporter""" | ||
self.setup() | ||
|
||
def setup(self): | ||
"""Function for configuring the reporter. | ||
|
||
Some reporters may need configuring some internal parameters or even | ||
creating objects to warmup. This function deals with this | ||
""" | ||
|
||
@abstractmethod | ||
def update(self, report): | ||
"""Receives report from subject. | ||
|
||
This is the main function for reporting events. The Reporter receives a | ||
report and have to deal with what to do with that. For Circle-Evolution | ||
you can expect anything from strings to `numpy.array`. | ||
""" | ||
|
||
|
||
class LoggerReporter(Reporter): | ||
"""Reporter for logging. | ||
|
||
This Reporter is responsible for setting up a Logger object and logging all | ||
events that happened during circle-evolution cycle. Notice that this | ||
reporter only cares about strings and will notify minimal details about | ||
other types. | ||
""" | ||
def setup(self): | ||
"""Sets up Logger""" | ||
config_initial = { | ||
'version': 1, | ||
'disable_existing_loggers': True, | ||
'formatters': { | ||
'simple': { | ||
'format': '%(asctime)s %(name)s %(message)s' | ||
}, | ||
}, | ||
'handlers': { | ||
'console': { | ||
'level': 'INFO', | ||
'class': 'logging.StreamHandler', | ||
'formatter': 'simple' | ||
}, | ||
}, | ||
'loggers': { | ||
'circle-evolution': { | ||
'handlers': ['console'], | ||
'level': 'DEBUG', | ||
} | ||
}, | ||
'root': { | ||
'handlers': ['console'], | ||
'level': 'DEBUG' | ||
} | ||
} | ||
logging.config.dictConfig(config_initial) | ||
self.logger = logging.getLogger(__name__) # Creating new logger | ||
|
||
def update(self, report): | ||
"""Logs events using logger""" | ||
self.logger.debug("Received event...") | ||
if isinstance(report, str): | ||
# Only deals with string messages | ||
self.logger.info(report) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Base Class for Reported Objects. | ||
|
||
If you want to receive reports about any objects in Circle-Evolution you just | ||
need to extend your class with the base Runner. It provides an interface for | ||
attaching reporters and notifying all reporters of a particular event. | ||
""" | ||
|
||
|
||
class Runner: | ||
"""Base Runner class. | ||
|
||
The Runner class is responsible for managing reporters and sending events | ||
to them. If you need to receive updates by a particular reporter you just | ||
need to use this base class. | ||
|
||
Attributes: | ||
_reporters: list of reporters that are going to receive reports. | ||
""" | ||
_reporters = [] | ||
|
||
def attach(self, reporter): | ||
"""Attaches reporter for notifications""" | ||
self._reporters.append(reporter) | ||
|
||
def notify(self, report): | ||
"""Send report to all attached reporters""" | ||
for reporter in self._reporters: | ||
reporter.update(report) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First comment
I don't like the idea of having a single function to handle all commands.
My suggestion is to add many types of functions to deal with different events:
Examples
For example, we can have an onStart method which is called when evolution is about to start.
We can have an onEnd method which is called when evolution ends.
We can have an onStop method, called when the user forcibly closes simulation.
onUpdate called when mutated species replaces the parent species.
onNextGen.... e.t.c
Second comment
I also don't like the idea of having arguments passed as many types as this kind of code is prone to errors. I suggest using a class that holds information on the report.
For example, this class can hold the current generation number, current fitness, and best specie.
Caveats
The problem with my first comment is that it can slow things down especially if using something like onNextGen... So tell me if there is a faster way to do it, or if you want to suggest something else.
The problem with my second comment is that if we pass in the species reference the programmer might modify it and it will modify the actual specie because it is only a reference. But again I believe whoever is programming should be wary, and know about this through the docs.
Conclusion
Please tell me what you think about my two comments. If you have a better idea you may propose it to me.