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

Iterating over projects to get a list of their databases yields peewee error #38

Closed aleksandra-kim closed 8 years ago

aleksandra-kim commented 8 years ago

Original report by Tomas Navarrete Gutierrez (Bitbucket: tomas_navarrete, ).


Hi,

I want to be able to get the names of the databases from each project, and started with:

from brightway2 import *

for p in projects:
projects.current = p.name
print(p.name, databases)

but I get the following error:

  ...:     
default Databases dictionary with 2 object(s):
        biosphere3
        ecoinvent22
---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
<ipython-input-3-9166ed4a7809> in <module>()
----> 1 for p in projects:
      2     projects.current = p.name
      3     print(p.name, databases)
      4 

/home/tomas/virtualenvs/bw2/lib/python3.4/site-packages/bw2data/project.py in __iter__(self)
     63 
     64     def __iter__(self):
---> 65         for project_ds in ProjectDataset.select():
     66             yield project_ds
     67 

/home/tomas/virtualenvs/bw2/lib/python3.4/site-packages/peewee.py in next(self)
   2045             obj = self.qrw._result_cache[self._idx]
   2046         elif not self.qrw._populated:
-> 2047             obj = self.qrw.iterate()
   2048             self.qrw._result_cache.append(obj)
   2049             self.qrw._ct += 1

/home/tomas/virtualenvs/bw2/lib/python3.4/site-packages/peewee.py in iterate(self)
   2096 
   2097     def iterate(self):
-> 2098         row = self.cursor.fetchone()
   2099         if not row:
   2100             self._populated = True

ProgrammingError: Cannot operate on a closed database.

Then of course, if I don't iterate over the projects, I could do:

from brightway2 import *

names = [p.name for p in projects]

for name in names:
    projects.current = name
    print(name, databases)

... and that works fine.

aleksandra-kim commented 8 years ago

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


Fix #38.

Well, this at least fixes the symptoms, but not the real cause. There is no good reason that the projects database should be closed and opened again, but the existing code is convoluted enough (a bit too much magic) to change this easily.

aleksandra-kim commented 8 years ago

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


This has a real solution in commit ab9a55f.