brightway-lca / brightway2-data

Tools for the management of inventory databases and impact assessment methods. Part of the Brightway LCA framework.
https://docs.brightway.dev/
BSD 3-Clause "New" or "Revised" License
8 stars 21 forks source link

Give us a convenience method to get exchanges as a dict for mass updates #57

Open aleksandra-kim opened 6 years ago

aleksandra-kim commented 6 years ago

Original report by Niko Heeren (Bitbucket: nheeren, GitHub: nheeren).


Hey. There is probably a good reason why this does not work as I would expect, but I would like to put it out here nevertheless.

I find it surprising that the following returns None:

activity = db.random()
activity.get('name'), activity.get(u'exchanges')

I find it even more surprising that activity.as_dict()['exchanges'] does not exist.

I am guessing the reason is that you do not want to load the data already at this stage. Also, I gather the intended way to do this is to iterate over activity.exchanges(). However, I sometimes want to manually duplicate & modify datasets from the database and the easiest way to do that is to get the exchanges dictionary and then do my changes on that one. In order to do that, I currently need to load the entire database and then look up the process in question.

It would be nice if there were a convenience method à la activity.exchanges_as_dict() for that. Unfortunately, I do not have the resources to create a PR the moment.

aleksandra-kim commented 6 years ago

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


This is a reasonable request, but a bit harder than it seems. The easiest way to do this would be to adapt some code from wurst, which does the same thing (get an activity with exchanges as dicts), but for an entire database.

In the short term, I am probably going to focus any development time on getting the "next generation" ready. In the long term, I hope we can move away from this conceptual model of an exchange "belonging" to any given activity, and instead see these as separate objects that link two activities in a graph.

aleksandra-kim commented 6 years ago

Original comment by Niko Heeren (Bitbucket: nheeren, GitHub: nheeren).


OK. Wouldn't something like this work?

def return_ex_dicts(self):
    return_me = []
    for exc in self.exchhanges():
        return_me.append(exc.as_dict{})
    return return_me
aleksandra-kim commented 6 years ago

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


Sure, but that only gets you the data loaded - you can't save that directly, there needs to be some massaging. If you look at the wurst code, you can see that this isn't too hard, but also not super simple.

aleksandra-kim commented 6 years ago

Original comment by Niko Heeren (Bitbucket: nheeren, GitHub: nheeren).


alright. so i guess the case is closed then and we consider including it for the next major release.