# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/06_utils.misc.ipynb.

# %% auto 0
__all__ = ['nict', 'GenericEncoder', 'safe_int']

# %% ../../nbs/06_utils.misc.ipynb 1
import pprint

# %% ../../nbs/06_utils.misc.ipynb 2
class nict(dict):
    def __init__(self, d=dict(), /, **kwargs):
        if d is None: d = {}
        else:
            assert isinstance(d, dict), f'expected dict, got {type(d)}'
            d = d.copy()

        d.update(kwargs)
        for k,v  in d.items():
            if isinstance(v, dict):
                d[k] = nict(**v)
            elif isinstance(v, list):
                d[k] = [nict(**item) if isinstance(item, dict) else item for item in v]
            elif isinstance(v, tuple):
                d[k] = tuple(nict(**item) if isinstance(item, dict) else item for item in v)
            else:
                d[k] = v

        super().__init__(**d)

    def __getattr__(self, key):
        return self[key] if key in self else super().__getattribute__(key)

    def __setattr__(self, key, value):
        self[key] = value

    def __delattr__(self, key):
        del self[key]

    def __dir__(self):
        return list(self.keys()) + dir(super())

    def _to_dict(self):
        return {k: v._to_dict() if isinstance(v, nict) else v for k, v in self.items()}

# %% ../../nbs/06_utils.misc.ipynb 13
import json
from datetime import datetime

# %% ../../nbs/06_utils.misc.ipynb 14
class GenericEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        try:
            return obj.__dict__
        except AttributeError:
            try:
                return str(obj)
            except:
                return f"<{type(obj).__name__}>"

# %% ../../nbs/06_utils.misc.ipynb 15
def safe_int(s, default=0):
    try:
        return int(s)
    except ValueError:
        return default
