NREL / rex

REsource eXtraction Tool (rex)
https://nrel.github.io/rex
BSD 3-Clause "New" or "Revised" License
20 stars 10 forks source link

Instantiate iterators #182

Closed ppinchuk closed 3 months ago

ppinchuk commented 3 months ago

A lot of the rex Resource classes were trying to be their own iterator instances, which can easily cause infinite loops. For example:

import os
from rex import TESTDATADIR, Resource

fp = os.path.join(TESTDATADIR, 'nsrdb/ri_100_nsrdb_2012.h5')  # or your fav file here - any works
with Resource(fp) as res:
    dataset_permutation = [(a, b) for a in res for b in res]

or for a more verbose option:

with Resource(fp) as res:
    for a in res:
        for b in res:
            print(f"{a}: {b}")

This PR forces the iterable Resource classes to instead create a unique iterator for every __iter__ invocation, thereby removing the infinite loop issue.

bnb32 commented 3 months ago

btw does this close #181? If so you could remove the set / intersect logic from attrs

ppinchuk commented 3 months ago

btw does this close #181? If so you could remove the set / intersect logic from attrs

I don't think so - I didn't actually mess with the dataset name compilation logic