Skip to content
Snippets Groups Projects
Commit 14ce4fd1 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added extract_optional

parent 988a08ab
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
* added optional argument to `File.get_value` * added optional argument to `File.get_value`
* added `file_contents` schema * added `file_contents` schema
* added nested configuration sources * added nested configuration sources
* added `extract_optional`
...@@ -10,3 +10,7 @@ Satella provides a set of routines to use with optionals: ...@@ -10,3 +10,7 @@ Satella provides a set of routines to use with optionals:
.. autoclass:: satella.coding.optionals.Optional .. autoclass:: satella.coding.optionals.Optional
:members: :members:
To extract the value from an :class:`~satella.coding.optionals.Optional` you may use:
.. autofunction:: satella.coding.optionals.extract_optional
__version__ = '2.16.2a3' __version__ = '2.16.2a4'
...@@ -3,7 +3,7 @@ import typing as tp ...@@ -3,7 +3,7 @@ import typing as tp
from satella.coding.structures import Proxy from satella.coding.structures import Proxy
from satella.coding.typing import V from satella.coding.typing import V
__all__ = ['call_if_nnone', 'iterate_if_nnone', 'Optional'] __all__ = ['call_if_nnone', 'iterate_if_nnone', 'Optional', 'extract_optional']
def iterate_if_nnone(iterable: tp.Optional[tp.Iterable]) -> tp.Iterable: def iterate_if_nnone(iterable: tp.Optional[tp.Iterable]) -> tp.Iterable:
...@@ -116,3 +116,17 @@ class Optional(Proxy): ...@@ -116,3 +116,17 @@ class Optional(Proxy):
EMPTY_OPTIONAL = Optional(None) EMPTY_OPTIONAL = Optional(None)
def extract_optional(v):
"""
If v is an optional, extract the value that it wraps.
If it is not, return v
:param v: value to extract the value from
:return: resulting value
"""
if isinstance(v, Optional):
return getattr(v, '_Proxy__obj')
else:
return v
import unittest import unittest
from satella.coding.optionals import call_if_nnone, iterate_if_nnone, Optional from satella.coding.optionals import call_if_nnone, iterate_if_nnone, Optional, extract_optional
class TestOptionals(unittest.TestCase): class TestOptionals(unittest.TestCase):
def test_extract_optional(self):
b = Optional(None)
self.assertIs(extract_optional(b.test.value), None)
def test_optional(self): def test_optional(self):
b = Optional(None) b = Optional(None)
self.assertFalse(b.consumer.cancel().result()) self.assertFalse(b.consumer.cancel().result())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment