Created
October 24, 2019 18:17
-
-
Save dkgndianko/1b770328cb0ead241d17eb7a77fcb7fe to your computer and use it in GitHub Desktop.
This is a method for updating a list of elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def updator(initial_list: Set[T], new_list: Set[T], creator: Callable[T, U], getter: Callable[T, U], deleter: Callable[T]) -> List[U]: | |
result = [] | |
for e in new_list: | |
element = getter(e) | |
if not element: | |
element = creator(e) | |
result.append(element) | |
to_delete = initial_list.difference(new_list) | |
for e in to_delete: | |
deleter(e) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very useful when updating sub-elements.
Sample:
We have a user resource (main element) that have permissions (sub-elements). We can use a similar thing to update user permission.
A function that will update user permission could be defined like this: