Skip to content

Commit

Permalink
[IMP] beesdoo_shift_swap: Improve legibility, again
Browse files Browse the repository at this point in the history
The idea here is to slowly empty one set, and fill the other set. This
is fairly intuitive, and uses efficient set operations. It is certainly
more intuitive than emptying the `changes` list every time, and slowly
refilling it over every iteration with unprocessed records.

The only unintuitive (and inefficient) step is the sorting. I would use
an OrderedSet here, but no such class exists in Python.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Oct 31, 2024
1 parent d175911 commit 550e45c
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions beesdoo_shift_swap/models/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ def _prepare_task_day(self):
)

changes = (
list(swaps)
+ list(exchanges)
+ list(solidarity_offers)
+ list(solidarity_requests)
set(swaps)
| set(exchanges)
| set(solidarity_offers)
| set(solidarity_requests)
)

# Sort changes by validation date to evaluate them in the correct order
changes.sort(key=lambda x: x.get_validate_date())
processed = set()

swap_subscription_done = []
for shift in shifts:
to_process = changes
changes = []
# Sort changes by validation date to evaluate them in the correct order
to_process = sorted(changes, key=lambda x: x.get_validate_date())
for rec in to_process:
shift, swap_subscription_done, done = rec.update_shift_data(
shift, swap_subscription_done
)
if not done:
changes.append(rec)
if done:
processed.add(rec)
changes -= processed

return shifts

0 comments on commit 550e45c

Please sign in to comment.