python-isc-dhcp-leases icon indicating copy to clipboard operation
python-isc-dhcp-leases copied to clipboard

Move string parsing to separate function

Open dholl opened this issue 3 years ago • 1 comments

This pull request addresses issue #24 by moving the string parsing code into a separate function. It preserves existing API usage, but adds a new publicly-exposed function.

dholl avatar Feb 27 '21 19:02 dholl

Comments / changes are welcome! I'm indifferent to exactly how #24 gets implemented, so in this first draft, I attempted the minimum possible to isolate the parsing logic. But maybe folks want this as a static function instead?

Personally, instead of returning a list of leases, I'd rather tweak this function into returning an iterable to permit parsing very large lease files and filtering for active leases on-the-fly. If folks are okay with this idea, I'm happy to tweak this new function and update the pull request. It would change the "simple case" calling from

leases = isc_leases_from_string(the_string)

to

leases = list(isc_leases_from_string(the_string))

It would also allow for things like:

active_leases = list(filter(lambda lease: lease.valid and lease.active, isc_leases_from_string(the_string)))

or fancier:

for active_lease in filter(lambda lease: lease.valid and lease.active, isc_leases_from_string(the_string)):
    # do something like launch coroutine to investigate this lease...

but the basic idea is that for very large lease files, we never have to have an entire list of Lease objects in memory at once if we don't need it, especially since the contents of the lease file is still loaded as a string.

dholl avatar Feb 27 '21 19:02 dholl