# AUTOGENERATED! DO NOT EDIT! File to edit: 10_utils.ipynb (unless otherwise specified).

__all__ = ['resolve_path', 'print_dict', 'sort_by_key', 'sec_to_hours', 'to_datetime', 'format_date']

# Cell
import pathlib

# Cell
def resolve_path(path):
    return pathlib.Path(path).resolve()

# Cell
def print_dict(item_dict):
    for k, v in item_dict.items():
        if isinstance(v, dict):
            print(f"{k}:")
            print_dict(v)
        elif isinstance(v, list):
            print(f"{k}:")
            for v1 in v:
                print(f"\t- {v1}")
        else:
            print(f"{k}: {v}")

# Cell
def sort_by_key(items=[], key=None):
    reverse = False
    if key.startswith("-"):
        key = key[1:]
        reverse = True
    if key == None:
        return items
    if len(items) ==0:
        return []
    _item = items[0]
    valid_keys = []

    if isinstance(_item, dict):
        print("This method is for sorting lists of objects and not `dict`.")
        return items
    if isinstance(_item, set):
        print("This method is for sorting lists of objects and not `set`.")
        return items
    if not isinstance(_item, object):
        print("This method is for sorting lists of objects.")
        return items
    try:
        valid_keys += list(set(_item.__dict__.keys()))
    except:
        pass
    if key not in list(valid_keys):
        valid_str = ""
        msg = f"{key} is not a valid sorting option."
        if len(valid_keys) > 0:
            valid_str = ", ".join(valid_keys)
            msg = f"{msg} Choices are: {valid_str}"
        else:
            msg = f"{msg} Are you sure that list of items with the Python class `{str(_item.__class__.__name__)}` can be sorted?"
        raise Exception(msg)
    items = sorted(items, key=lambda x: getattr(x, key), reverse=reverse)
    return items




# Cell

from datetime import datetime, timedelta

def sec_to_hours(seconds):
    a=str(int(seconds//3600))
    b=str(int(seconds%3600)//60)
    c=str(int((seconds%3600)%60))
    d= f"{a} hours {b} mins {c} seconds ago."
    return d


def to_datetime(datetime_str):
    try:
        datetime_str = datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S.%fz")
        valid = True
    except:
        valid = False
    return datetime_str, valid

def format_date(datetime_obj):
    return datetime_obj.strftime('%d %b %Y')