diff options
author | Arun Isaac | 2022-09-16 15:40:33 +0530 |
---|---|---|
committer | Arun Isaac | 2022-09-16 15:40:33 +0530 |
commit | ee635c900525ceecc7619a9b47ab042e8fb81a50 (patch) | |
tree | 0b6bdb9e254586afa3c7c9990ae67f86d3fed6db | |
parent | 0ac7b18c846fe8df134a2241bb0163e9fd4b7633 (diff) | |
download | genenetwork2-ee635c900525ceecc7619a9b47ab042e8fb81a50.tar.gz |
Add example code to docstring of MonadicDict.
* wqflask/utility/monads.py (MonadicDict): Add example code to
docstring.
-rw-r--r-- | wqflask/utility/monads.py | 44 |
1 files changed, 44 insertions, 0 deletions
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) + <class 'utility.monads.MonadicDict'> + >>> type(d.data) + <class 'dict'> + + Delete keys. Deleting non-existent keys does nothing. + >>> del d["bar"] + >>> d + {'foo': 1} + >>> del d["foo"] + >>> d + {} """ def __init__(self, d={}, convert=True): """ |