Skip to content

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

Merged
rht merged 3 commits into
mainfrom
eventlist_str
Aug 5, 2024
Merged

rht merged 3 commits into
mainfrom
eventlist_str

Conversation

@EwoutH

@EwoutH EwoutH commented Aug 5, 2024 •

Copy link
Copy Markdown
Member

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 quaquel and rht August 5, 2024 08:14
@github-actions

github-actions Bot commented Aug 5, 2024

Copy link
Copy Markdown

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

Corvince commented Aug 5, 2024

Copy link
Copy Markdown
Contributor

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

EwoutH commented Aug 5, 2024

Copy link
Copy Markdown
Member Author

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
@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