<commit_before>
__all__ = ['patch_object', 'with_patched_object', 'PatchHandler'] # patched_context, see below

from fudge.util import wraps

def patch_object(obj, attr_name, patched_value):
    """A utility to patch an object and return :class:`fudge.PatchHandler` for later restoration.
    
    You can use more convenient wrappers :func:`with_patched_object` and :func:`patched_context`
    """
    if isinstance(obj, (str, unicode)):
        obj_path = obj
        obj = __import__(obj_path)
        for part in obj_path.split('.')[1:]:
            obj = getattr(obj, part)

    handle = PatchHandler(obj, attr_name)
    handle.patch(patched_value)
    return handle

try:
    from contextlib import contextmanager
except ImportError:
    pass
else:
    # in 2.5+
    @contextmanager
    def patched_context(obj, attr_name, patched_value):
        """A context manager to execute :func:`fudge.patch_object` from the with statement
        
        Example::
            
            >>> class Session:
            ...     state = 'clean'
            ... <commit_msg>minor docstring rewording<commit_after>
__all__ = ['patch_object', 'with_patched_object', 'PatchHandler'] # patched_context, see below

from fudge.util import wraps

def patch_object(obj, attr_name, patched_value):
    """A utility to patch an object and return :class:`fudge.PatchHandler` for later restoration.
    
    You can use more convenient wrappers :func:`with_patched_object` and :func:`patched_context`
    """
    if isinstance(obj, (str, unicode)):
        obj_path = obj
        obj = __import__(obj_path)
        for part in obj_path.split('.')[1:]:
            obj = getattr(obj, part)

    handle = PatchHandler(obj, attr_name)
    handle.patch(patched_value)
    return handle

try:
    from contextlib import contextmanager
except ImportError:
    pass
else:
    # in 2.5+
    @contextmanager
    def patched_context(obj, attr_name, patched_value):
        """A context manager to execute :func:`fudge.patch_object` in a with statement
        
        Example::
            
            >>> class Session:
            ...     state = 'clean'
            ... 
{'fraction_non_alphanumeric': 0.1013953488372093, 'fraction_numerical': 0.0027906976744186047, 'mean_word_length': 2.740869565217391, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2527452', 'n_tokens_mistral': 638, 'n_tokens_neox': 626, 'n_words': 180}