generating list of files matching condition

Peter Otten __peter__ at web.de
Fri Nov 25 03:47:50 EST 2016


Seb wrote:

> On Thu, 24 Nov 2016 10:18:21 +0100,
> Peter Otten <__peter__ at web.de> wrote:
> 
>> Is win_files huge? Then it might help to avoid going over the entire
>> list for every interval. Instead you can sort the list and then add to
>> the current list while you are below nextw.
> 
>> My pandas doesn't seem to have Timedelta (probably it's too old), so
>> here's a generic solution using only the stdlib:
> 
> [...]
> 
>> PS: If the files' prefixes differ you cannot sort by name. Instead use
> 
>> ec_files.sort(key=filename_to_time)
> 
>> PPS: There is probably a way to do this by converting the list to a
>> pandas dataframe; it might be worthwhile to ask in a specialised
>> forum.
> 
> Thank you, I learned a few things there (particularly nonlocal, since
> I'm still in Python 2.7)!

Did you get the code to work in Python 2? One way is to tack the nonlocal 
value as an attribute onto the first mutable you can find:

def make_key(delta_t):
    def key(filename):
        if key.upper_bound is None:
            key.upper_bound = filename_to_time(filename) + delta_t
        else:
            t = filename_to_time(filename)
            while t >= key.upper_bound:
                key.upper_bound += delta_t
        return key.upper_bound
    key.upper_bound = None
    return key

And if it worked, did it actually improve performance?




More information about the Python-list mailing list