*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit cd67d49c authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Implement context manager for temporarily monkey-patching methods

parent 08234824
No related branches found
No related tags found
1 merge request!17Update commands for updating draft files & add basic vocabulary management commands
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
"""Utility functions for Invenio-Utilities-TUW.""" """Utility functions for Invenio-Utilities-TUW."""
from contextlib import contextmanager
from difflib import SequenceMatcher from difflib import SequenceMatcher
from invenio_access.permissions import any_user, system_identity from invenio_access.permissions import any_user, system_identity
...@@ -54,3 +55,12 @@ def get_identity_for_user(user): ...@@ -54,3 +55,12 @@ def get_identity_for_user(user):
def similarity(a: str, b: str) -> float: def similarity(a: str, b: str) -> float:
"""Calculate the similarity between two strings.""" """Calculate the similarity between two strings."""
return SequenceMatcher(None, a, b).ratio() return SequenceMatcher(None, a, b).ratio()
@contextmanager
def monkey_patch_temp(object_, method_name, new_method):
"""Temporarily monkey patch an object's method."""
x = getattr(object_, method_name)
setattr(object_, method_name, new_method.__get__(object_, type(object_)))
yield object_
setattr(object_, method_name, x)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment