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

Fix: avoid entries in Grid fields entries being shared between instances #151

Merged
merged 2 commits into from
Apr 22, 2020

Conversation

danielolsen
Copy link
Contributor

Purpose

Fix a bug that was introduced in #121, where entries in the Grid.fields dict are shared between instances. Example:

>>> from powersimdata.input.grid import Grid
>>> western_grid = Grid(['Western'])
Reading bus.csv
Reading plant.csv
Reading gencost.csv
Reading branch.csv
Reading dcline.csv
Reading sub.csv
Reading bus2sub.csv
Reading zone.csv
>>> western_grid.plant.shape
(2527, 35)
>>> eastern_grid = Grid(['Eastern'])
Reading bus.csv
Reading plant.csv
Reading gencost.csv
Reading branch.csv
Reading dcline.csv
Reading sub.csv
Reading bus2sub.csv
Reading zone.csv
>>> western_grid.plant.shape
(10789, 35)
>>> eastern_grid.plant.shape
(10789, 35)
>>> (western_grid.plant == eastern_grid.plant).all().all()
True
>>> western_grid.plant is eastern_grid.plant
True

What is the code doing

In test_grid.py: adding a test that detects this behavior.

In grid.py: moving the definition of the fields and transform dicts into the __init__ so that they are no longer shared.

Time to review

Very quick. The test and fix are both very simple.

@danielolsen danielolsen added the bug Something isn't working label Apr 21, 2020
@danielolsen danielolsen requested review from dmuldrew and rouille April 21, 2020 23:15
@rouille
Copy link
Collaborator

rouille commented Apr 21, 2020

Wouah good catch.

Copy link
Collaborator

@dmuldrew dmuldrew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the issue...these fields were a properties of the class and it popped up when there was more than once instance of the grid. Sorry I've been translating my object design methodology from C# and I missed this Python subtlety...

C# => Python
class: static prop => prop
instance: prop => self.prop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants