def mk_time_info_extractor(spec):
    """
    Returns a function that will extract information from timestamps in a dict format.
    The specification should be a list of timetuple attributes
    (see https://docs.python.org/2/library/time.html#time.struct_time) to extract,
    or a {k: v, ...} dict where v are the timetuple attributes, and k what you want to call them in the output.

    Example:
        fun = mk_time_info_extractor({'day_of_week': 'tm_wday', 'hour_of_day': 'tm_hour'})
        # assuming you defined some timestamp called t...
        print t
        print fun(t)
    2015-06-02 20:46:16.629000
    {'day_of_week': 1, 'hour_of_day': 20}
    """
    if not isinstance(spec, dict):  # if spec is not a dict, make it so
        spec = {x: x for x in spec}

    def extractor(timestamp):
        time_struct = timestamp.timetuple()
        return {k: time_struct.__getattribute__(v) for k, v in spec.items()}

    return extractor