Skip to content
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

devs/eventlist: Add repr method to print EventList pretty #2195

Merged
merged 3 commits into from
Aug 5, 2024
Merged

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Aug 5, 2024

Add __repr__ method to print EventList objects in a useful format.

In models, this allows doing things like print(self.simulator.event_list) to validate the events queued in your simulator.

Output of print(self.simulator.event_list):

  • Before: <mesa.experimental.devs.eventlist.EventList object at 0x00000186E42AF830>
  • After: EventList([Event(time=7.046389752627814, priority=5, id=0), Event(time=8.305480965357855, priority=5, id=9), Event(time=10.904561103085891, priority=5, id=6)])

Add __str__ and __repr__ methods to print EventList in a useful format.

In models, this allows doing things like `self.simulator.event_list` to validate the events queued in your simulator.
@EwoutH EwoutH added experimental Release notes label enhancement Release notes label labels Aug 5, 2024
@EwoutH EwoutH requested review from rht and quaquel August 5, 2024 08:14
Copy link

github-actions bot commented Aug 5, 2024

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
Schelling small 🔵 -0.0% [-0.5%, +0.3%] 🔵 +0.9% [+0.7%, +1.1%]
Schelling large 🔵 -0.3% [-1.0%, +0.4%] 🔵 +1.0% [-0.3%, +2.2%]
WolfSheep small 🔵 -1.0% [-2.3%, +0.1%] 🔵 -0.3% [-0.7%, +0.0%]
WolfSheep large 🔵 -0.5% [-1.0%, +0.0%] 🔵 -0.1% [-1.0%, +0.8%]
BoidFlockers small 🔵 +0.2% [-0.6%, +1.0%] 🔵 -0.5% [-1.2%, +0.2%]
BoidFlockers large 🔵 -0.3% [-0.8%, +0.2%] 🔵 -0.2% [-0.6%, +0.1%]

@Corvince
Copy link
Contributor

Corvince commented Aug 5, 2024

Tha ks! This should probably be done for a lot more classes of mesa.

Just a note though: it is more common to just define repr and then if str is not defined, but called, it will automatically use the repr definition

@EwoutH
Copy link
Member Author

EwoutH commented Aug 5, 2024

Use .__repr__() for Programmers vs .__str__() for Users
In general, you should use str when you want to display a string to the user, and repr when you want to represent a string in a way that can be used to recreate the original object, such as in a debugger or when logging the state of an object.

Thanks. I think the current implementation is more __repr__ than __str__, since it's contains all the elements in it in a machine readable way. Will update, and then we can add an (even more) user readable string later.

__str__ will follow from it
@rht rht merged commit fc03f72 into main Aug 5, 2024
9 checks passed
@rht rht deleted the eventlist_str branch August 5, 2024 11:36
@EwoutH EwoutH changed the title devs/eventlist: Add str and repr methods to print EventList devs/eventlist: Add repr method to print EventList pretty Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Release notes label experimental Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants