Skip to content

Instantly share code, notes, and snippets.

@justcallmelarry
Last active October 28, 2024 22:25
Show Gist options
  • Save justcallmelarry/8a3c8dbfd95512826a8fa415dc747a92 to your computer and use it in GitHub Desktop.
Save justcallmelarry/8a3c8dbfd95512826a8fa415dc747a92 to your computer and use it in GitHub Desktop.
import json
import os
from typing import Any, Dict, KeysView, cast
class Settings:
def __init__(self, data: Dict) -> None:
self._data = data
def __str__(self) -> str:
return str(self._data)
def __repr__(self) -> str:
return str(self._data)
def __getitem__(self, key: str) -> Any:
value = self._data.get(key, {})
if isinstance(value, dict):
return Settings(value)
return value
def __getattr__(self, key: str) -> Any:
value = self._data.get(key, {})
if isinstance(value, dict):
return Settings(value)
return value
def __bool__(self) -> bool:
return bool(self._data)
def __len__(self) -> int:
return len(self._data)
def __iter__(self) -> Any:
return self._data.__iter__()
def __contains__(self, item: Any) -> Any:
return self._data.__contains__(item)
def __eq__(self, other: Any) -> bool:
if isinstance(other, Settings):
return cast(bool, self._data == other._data)
elif isinstance(other, dict):
return cast(bool, self._data == other)
else:
return False
def keys(self) -> KeysView:
return cast(KeysView, self._data.keys())
def get(self, key: str, default: Any = None) -> Any:
return self._data.get(key, default)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment