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

PyQt progress signals for the Activity Browser #53

Closed aleksandra-kim closed 6 years ago

aleksandra-kim commented 6 years ago

Original report by Adrian Haas (Bitbucket: haasad, GitHub: haasad).


I'm working on a feature for the Activity Browser to import databases (see this pull request). Since importing takes quite a long time, I want to have progress signals.

I currently have a working solution where I subclass the SQLiteBackend and reimplement the private _efficient_write_many method. This is really not ideal and not future-proof. Would you be willing to accept a pull request to let bw2data (and later also bw2io) emit pyqt signals?

I imagine this to look something like this:

try:
    from PyQt5.QtCore import pyqtSignal
except ImportError:
    pyqtSignal = None

add an option bw2data.config.activity_browser=True

if config.activity_browser:
    ab_import_progress_signal.emit(index, total)

where the signals are defined in their own class that are accessible via bw2data.signals

aleksandra-kim commented 6 years ago

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


I would have to think about this a bit more; my first reaction is that you could subclass pretty easily, so would be negative for now:

#!python

class NewBackend(SQLiteBackend):
    def _efficient_write_many(self, *args, **kwargs):
        do_some_signal_stuff()
        super()._efficient_write_many(*args, **kwargs)
        some_more_signal_stuff()

_efficient_write_many isn't really private (i.e. I don't think it would change), just not something that people should use directly.

But! I definitely could be convinced otherwise.

aleksandra-kim commented 6 years ago

Original comment by Adrian Haas (Bitbucket: haasad, GitHub: haasad).


Agreed. This is much more elegant. However i would like to have access to the index variable in the for-loop to update the progressbar.

 for index, (key, ds) in enumerate(data.items()):

Would it be an option to put the inner part of the for loop in its own function? In this case I could subclass it in the way you proposed above.

def _inside(self, index, key, ds):
    for exchange in ds.get('exchanges', []):
    ...
aleksandra-kim commented 6 years ago

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


Split _efficient_write_many_data into two functions, see #53

aleksandra-kim commented 6 years ago

Original comment by Adrian Haas (Bitbucket: haasad, GitHub: haasad).


Thanks!

aleksandra-kim commented 6 years ago

Original comment by Adrian Haas (Bitbucket: haasad, GitHub: haasad).


PR is merged!