From d91d9d6e04e4f24bd2d604fa410f267636c309ca Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 3 Dec 2020 11:38:29 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20get=20method=20to=20Scrolls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/autodoc/index.rst | 2 +- royalnet/lazy/lazy.py | 1 - royalnet/scrolls/__init__.py | 9 +++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/source/autodoc/index.rst b/docs/source/autodoc/index.rst index da4b4caa..ff89b29d 100644 --- a/docs/source/autodoc/index.rst +++ b/docs/source/autodoc/index.rst @@ -9,7 +9,7 @@ It may be incomplete or outdated, as it is automatically updated. :maxdepth: 3 alchemist - baron campaigns + lazy scrolls typing diff --git a/royalnet/lazy/lazy.py b/royalnet/lazy/lazy.py index f1473c02..533c961b 100644 --- a/royalnet/lazy/lazy.py +++ b/royalnet/lazy/lazy.py @@ -1,6 +1,5 @@ from __future__ import annotations from royalnet.typing import * -import functools Result = TypeVar("Result") diff --git a/royalnet/scrolls/__init__.py b/royalnet/scrolls/__init__.py index 01491fc4..948f91ac 100644 --- a/royalnet/scrolls/__init__.py +++ b/royalnet/scrolls/__init__.py @@ -13,6 +13,9 @@ import json from .exc import * +T = TypeVar("T") + + class Scroll: """ A configuration handler that allows getting values from both the environment variables and a config file. @@ -127,6 +130,12 @@ class Scroll: except NotFoundError: return self._get_from_config(item) + def get(self, item: str, default: T) -> Union[JSONScalar, T]: + try: + return self[item] + except NotFoundError: + return default + Scroll.loaders = { ".json": Scroll.from_json,