Length of a finite generator

前端 未结 3 855
野性不改
野性不改 2021-01-18 03:32

I have these two implementations to compute the length of a finite generator, while keeping the data for further processing:

def count_generator1(generator):         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 04:03

    If you don't need the length of the iterator prior to processing the data, you could use a helper method with a future to add in counting into the processing of your iterator/stream:

    import asyncio
    def ilen(iter):
        """
        Get future with length of iterator
        The future will hold the length once the iteartor is exhausted
        @returns: 
        """
        def ilen_inner(iter, future):
            cnt = 0
            for row in iter:
                cnt += 1
                yield row
            future.set_result(cnt)
        cnt_future = asyncio.Future()
        return ilen_inner(iter, cnt_future), cnt_future
    

    Usage would be:

    data = db_connection.execute(query)
    data, cnt = ilen(data)
    solve_world_hunger(data)
    print(f"Processed {cnt.result()} items")
    

提交回复
热议问题