# Path: gtimer/private/focus.py
# def create_next_timer(*args, **kwargs):
# def remove_last_timer():
# def create_next_loop(*args, **kwargs):
# def remove_last_loop():
# def refresh_shortcuts():
# def hard_reset():
# def get_current_timer():
# 
# Path: gtimer/private/collapse.py
# def collapse_times():
# def _collapse_subdivision():
# def _copy_timer_stack():
# 
# Path: gtimer/local/times.py
# class Times(object):
#     """ Timing data resides here, including tree structure.
#     (Survives after timing is complete).
#     """
# 
#     def __init__(self,
#                  name=None,
#                  parent=None,
#                  pos_in_parent=None,
#                  save_itrs=True):
#         self.name = None if name is None else str(name)
#         self.parent = parent  # refer to another Times instance.
#         self.pos_in_parent = pos_in_parent  # refers to a stamp name.
#         self.save_itrs = save_itrs
#         self.reset()
# 
#     def __deepcopy__(self, memo):
#         new = type(self)()
#         new.__dict__.update(self.__dict__)
#         new.stamps = copy.deepcopy(self.stamps, memo)
#         new.subdvsn = copy.deepcopy(new.subdvsn, memo)
#         new.par_subdvsn = copy.deepcopy(self.par_subdvsn, memo)
#         # Avoid deepcopy of parent, and update that attribute.
#         for sub_list in itervalues(new.subdvsn):
#             for sub in sub_list:
#                 sub.parent = new
#         for par_dict in itervalues(new.par_subdvsn):
#             for par_list in itervalues(par_dict):
#                 for sub in par_list:
#                     sub.parent = new
#         return new
# 
#     def reset(self):
#         self.stamps = Stamps()
#         self.total = 0.
#         self.stamps_sum = 0.
#         self.self_agg = 0.
#         self.subdvsn = dict()
#         self.par_subdvsn = dict()
#         self.par_in_parent = None
# 
# Path: gtimer/local/merge.py
# def merge_times(rcvr, new):
# def _merge_stamps(rcvr, new):
# def _merge_dict(rcvr, new, attr):
# def _merge_dict_itr(rcvr, new, attr, itr_func):
# def _stamps_as_itr(rcvr, new):
# def _merge_subdivisions(rcvr, new):
# def _merge_par_subdivisions(rcvr, new):


from timeit import default_timer as timer
from gtimer.private import focus as f
from gtimer.private import collapse
from gtimer.local.times import Times
from gtimer.local import merge
    import cPickle as pickle
    import pickle
import copy


def attach_subdivision(times):
    """
    Manual assignment of a (stopped) times object as a subdivision of running
    timer.  Use cases are expected to be very limited (mainly provided as a
    one-Times variant of attach_par_subdivision).

    Notes:
        As with any subdivision, the interval in the receiving timer is assumed to
        totally subsume the time accumulated within the attached object--the total
        in the receiver is not adjusted!

    Args:
        times (Times): Individual Times data object.

    Raises:
        TypeError: If times not a Times data object.
    """
    t = timer()
    if not isinstance(times, Times):
        raise TypeError("Expected Times object for param 'times'.")
    assert times.total > 0., "Attached subdivision has total time 0, appears empty."
    name = times.name
    f.r.self_agg += times.self_agg
    if name not in f.t.subdvsn_awaiting:
        times_copy = copy.deepcopy(times)
        times_copy.parent = f.r
        f.t.subdvsn_awaiting[name] = times_copy
    else:

# Path: gtimer/private/focus.py
# def create_next_timer(*args, **kwargs):
# def remove_last_timer():
# def create_next_loop(*args, **kwargs):
# def remove_last_loop():
# def refresh_shortcuts():
# def hard_reset():
# def get_current_timer():
# 
# Path: gtimer/private/collapse.py
# def collapse_times():
# def _collapse_subdivision():
# def _copy_timer_stack():
# 
# Path: gtimer/local/times.py
# class Times(object):
#     """ Timing data resides here, including tree structure.
#     (Survives after timing is complete).
#     """
# 
#     def __init__(self,
#                  name=None,
#                  parent=None,
#                  pos_in_parent=None,
#                  save_itrs=True):
#         self.name = None if name is None else str(name)
#         self.parent = parent  # refer to another Times instance.
#         self.pos_in_parent = pos_in_parent  # refers to a stamp name.
#         self.save_itrs = save_itrs
#         self.reset()
# 
#     def __deepcopy__(self, memo):
#         new = type(self)()
#         new.__dict__.update(self.__dict__)
#         new.stamps = copy.deepcopy(self.stamps, memo)
#         new.subdvsn = copy.deepcopy(new.subdvsn, memo)
#         new.par_subdvsn = copy.deepcopy(self.par_subdvsn, memo)
#         # Avoid deepcopy of parent, and update that attribute.
#         for sub_list in itervalues(new.subdvsn):
#             for sub in sub_list:
#                 sub.parent = new
#         for par_dict in itervalues(new.par_subdvsn):
#             for par_list in itervalues(par_dict):
#                 for sub in par_list:
#                     sub.parent = new
#         return new
# 
#     def reset(self):
#         self.stamps = Stamps()
#         self.total = 0.
#         self.stamps_sum = 0.
#         self.self_agg = 0.
#         self.subdvsn = dict()
#         self.par_subdvsn = dict()
#         self.par_in_parent = None
# 
# Path: gtimer/local/merge.py
# def merge_times(rcvr, new):
# def _merge_stamps(rcvr, new):
# def _merge_dict(rcvr, new, attr):
# def _merge_dict_itr(rcvr, new, attr, itr_func):
# def _stamps_as_itr(rcvr, new):
# def _merge_subdivisions(rcvr, new):
# def _merge_par_subdivisions(rcvr, new):


# Path: gtimer/public/io.py
from timeit import default_timer as timer
from gtimer.private import focus as f
from gtimer.private import collapse
from gtimer.local.times import Times
from gtimer.local import merge
    import cPickle as pickle
    import pickle
import copy



def attach_subdivision(times):
    """
    Manual assignment of a (stopped) times object as a subdivision of running
    timer.  Use cases are expected to be very limited (mainly provided as a
    one-Times variant of attach_par_subdivision).

    Notes:
        As with any subdivision, the interval in the receiving timer is assumed to
        totally subsume the time accumulated within the attached object--the total
        in the receiver is not adjusted!

    Args:
        times (Times): Individual Times data object.

    Raises:
        TypeError: If times not a Times data object.
    """
    t = timer()
    if not isinstance(times, Times):
        raise TypeError("Expected Times object for param 'times'.")
    assert times.total > 0., "Attached subdivision has total time 0, appears empty."
    name = times.name
    f.r.self_agg += times.self_agg
    if name not in f.t.subdvsn_awaiting:
        times_copy = copy.deepcopy(times)
        times_copy.parent = f.r
        f.t.subdvsn_awaiting[name] = times_copy
    else:

        merge.merge_times(f.t.subdvsn_awaiting[name], times)