From ee635c900525ceecc7619a9b47ab042e8fb81a50 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Fri, 16 Sep 2022 15:40:33 +0530 Subject: Add example code to docstring of MonadicDict. * wqflask/utility/monads.py (MonadicDict): Add example code to docstring. --- wqflask/utility/monads.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/wqflask/utility/monads.py b/wqflask/utility/monads.py index d683b0ae..21d04f36 100644 --- a/wqflask/utility/monads.py +++ b/wqflask/utility/monads.py @@ -20,6 +20,50 @@ class MonadicDict(UserDict): Keys in this dictionary can be any python object, but values must be monadic values. + + from pymonad.maybe import Just, Nothing + + Initialize by setting individual keys to monadic values. + >>> d = MonadicDict() + >>> d["foo"] = Just(1) + >>> d["bar"] = Nothing + >>> d + {'foo': 1} + + Initialize by converting a built-in dictionary object. + >>> MonadicDict({"foo": 1}) + {'foo': 1} + >>> MonadicDict({"foo": 1, "bar": None}) + {'foo': 1} + + Initialize from a built-in dictionary object with monadic values. + >>> MonadicDict({"foo": Just(1)}, convert=False) + {'foo': 1} + >>> MonadicDict({"foo": Just(1), "bar": Nothing}, convert=False) + {'foo': 1} + + Get values. For non-existent keys, Nothing is returned. Else, a + Just value is returned. + >>> d["foo"] + Just 1 + >>> d["bar"] + Nothing + + Convert MonadicDict object to a built-in dictionary object. + >>> d.data + {'foo': 1} + >>> type(d) + + >>> type(d.data) + + + Delete keys. Deleting non-existent keys does nothing. + >>> del d["bar"] + >>> d + {'foo': 1} + >>> del d["foo"] + >>> d + {} """ def __init__(self, d={}, convert=True): """ -- cgit v1.2.3