Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_opt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

import pytest

from test_streams import Node
Expand Down Expand Up @@ -169,3 +171,11 @@ def test_stream():
def test_dict_map_kwargs():
opt = Opt({"name": "First"})
assert opt.map_kwargs(Node).get().name == "First"


def test_map_enum():
class AnalysisAnalysisState(str, Enum):
NOT_SET = "NOT_SET"
RESOLVED = "RESOLVED"

assert Opt(AnalysisAnalysisState.RESOLVED).map_key("value").get() == "RESOLVED"
14 changes: 10 additions & 4 deletions tinystream.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import functools
import itertools
from typing import Iterable, TypeVar, Callable, List, Dict, Tuple, Iterator, Generic, Type
Expand All @@ -18,22 +19,27 @@


def _key_exists(x: any, key: Key, invert: bool = False):
def _hasattr():
if invert:
return not hasattr(x, key)
else:
return hasattr(x, key)

if isinstance(x, (list, tuple)):
size = len(x)
if invert:
return key >= size
else:
return key < size
elif isinstance(x, enum.Enum):
return _hasattr()
elif isinstance(x, (dict, Iterable)):
if invert:
return key not in x
else:
return key in x
else:
if invert:
return not hasattr(x, key)
else:
return hasattr(x, key)
return _hasattr()


def _get_key_value(x: any, key: Key) -> any:
Expand Down