<commit_before>#!/usr/bin/env python
# coding=utf-8
from __future__ import division, print_function, unicode_literals

import time
from datetime import timedelta

import wrapt
from sacred.config.custom_containers import FallbackDict
from sacred.config.signature import Signature
from sacred.randomness import create_rnd, get_seed

__sacred__ = True


def create_captured_function(function, prefix=None):
    sig = Signature(function)
    function.signature = sig
    function.uses_randomness = ("_seed" in sig.arguments or
                                "_rnd" in sig.arguments)
    function.logger = None
    function.config = {}
    function.rnd = None
    function.run = None
    function.prefix = prefix
    return captured_function(function)


@wrapt.decorator
def captured_function(wrapped, instance, args, kwargs):
    options = FallbackDict(
        wrapped.config,
        _config=wrapped.config,
        _log=wrapped.logger,
        _run=wrapped.run
    )
    if wrapped.uses_randomness:  # only generate _seed and _rnd if needed
        options['_seed'] = get_seed(wrapped.rnd)
        options['_rnd'] = create_rnd(options['_seed'])

    bound = (instance is not None)
    args, kwargs = wrapped.signature.construct_arguments(args, kwargs, options,
                                                         bound)
    wrapped.logger.debug("Started")
    start_time = time.time()
    # =================== run actual function =================================
    result = wrapped(*args, **kwargs)
    # =========================================================================
    stop_time = time.time()
    elapsed_time = timedelta(seconds=round(stop_time - start_time))
    wrapped.logger.debug("Finished after %s.", elapsed_time)

    return result
<commit_msg>Allow captured functions to be called normal code<commit_after>#!/usr/bin/env python
# coding=utf-8
from __future__ import division, print_function, unicode_literals

import time
from datetime import timedelta

import wrapt
from sacred.config.custom_containers import FallbackDict
from sacred.config.signature import Signature
from sacred.randomness import create_rnd, get_seed

__sacred__ = True


def create_captured_function(function, prefix=None):
    sig = Signature(function)
    function.signature = sig
    function.uses_randomness = ("_seed" in sig.arguments or
                                "_rnd" in sig.arguments)
    function.logger = None
    function.config = {}
    function.rnd = None
    function.run = None
    function.prefix = prefix
    return captured_function(function)


@wrapt.decorator
def captured_function(wrapped, instance, args, kwargs):
    options = FallbackDict(
        wrapped.config,
        _config=wrapped.config,
        _log=wrapped.logger,
        _run=wrapped.run
    )
    if wrapped.uses_randomness:  # only generate _seed and _rnd if needed
        options['_seed'] = get_seed(wrapped.rnd)
        options['_rnd'] = create_rnd(options['_seed'])

    bound = (instance is not None)
    args, kwargs = wrapped.signature.construct_arguments(args, kwargs, options,
                                                         bound)
    if wrapped.logger is not None:
        wrapped.logger.debug("Started")
        start_time = time.time()
    # =================== run actual function =================================
    result = wrapped(*args, **kwargs)
    # =========================================================================
    if wrapped.logger is not None:
        stop_time = time.time()
        elapsed_time = timedelta(seconds=round(stop_time - start_time))
        wrapped.logger.debug("Finished after %s.", elapsed_time)

    return result

{'fraction_non_alphanumeric': 0.14953779227841218, 'fraction_numerical': 0.000543773790103317, 'mean_word_length': 3.180681818181818, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5197237', 'n_tokens_mistral': 1006, 'n_tokens_neox': 979, 'n_words': 245}