Exemplo n.º 1
0
        def create():
            def key_mapper(x):
                if not x:
                    raise Exception(ex)
                else:
                    return math.floor(x / 2.0)

            return xs.pipe(ops.distinct(key_mapper))
Exemplo n.º 2
0
def intensity_steps(steps: List[Step], flushes):
    """

    Given an intensity value, return the list of steps
    which should be played back. The logic indexes higher
    based on the intensity value until it is reset by the final step

    :param steps:
    """

    return pipe(
        ops.flat_map(lambda event: rx.from_iterable(steps).pipe(
            ops.map(lambda step: (step, event)))),
        ops.filter(lambda x: x[0] <= x[1]), ops.map(lambda x: x[0]),
        ops.distinct(flushes=flushes))
Exemplo n.º 3
0
async def main(loop):
    scheduler = AsyncIOScheduler(loop)
    finder = WikipediaFinder(loop)
    stream = Subject()

    def task(term):
        t = loop.create_task(finder.search(term))
        return rx.from_future(t)

    def pretty(result):
        parsed = json.loads(result)
        print(json.dumps(parsed, sort_keys=True, indent=2))

    stream.pipe(
        ops.debounce(0.750),
        ops.distinct(),
        ops.flat_map_latest(task)
    ).subscribe(pretty, scheduler=scheduler)

    def reader():
        line = sys.stdin.readline().strip()
        stream.on_next(line)

    loop.add_reader(sys.stdin.fileno(), reader)
Exemplo n.º 4
0
import rx
from rx import operators as ops

rx.from_(["Alpha", "Beta", "Gamma", "Delta",
          "Epsilon"]).pipe(ops.map(lambda s: len(s)),
                           ops.distinct()).subscribe(lambda i: print(i))
Exemplo n.º 5
0
# Filter source for even numbers
import rx
from rx import operators as op

# Set up a source with a filter
source = rx.from_list([2, 3, 5, 7, 4, 9,
                       8]).pipe(op.filter(lambda value: value % 2 == 0))

# Subscribe a lambda function
source.subscribe(lambda value: print('Lambda Received', value))

# Use distinct to suppress duplicates
source = rx.from_list([2, 3, 5, 2, 4, 3, 2]).pipe(op.distinct())

# Subscribe a lambda function
source.subscribe(lambda value: print('Received', value))
Exemplo n.º 6
0
from rx import from_, operators as ops

from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"
       ]).pipe(ops.distinct(lambda s: len(s))).subscribe(lambda i: print(i))
Exemplo n.º 7
0
    op.average()
).subscribe(reporter)

print('=' * 25)


stocks2 = (('GOOG', 8.95), ('APPL', 7.65), ('APPL', 12.45), ('MSFT', 5.66), ('GOOG', 7.56), ('IBM', 12.76))

source1 = rx.from_list(stocks)
source2 = rx.from_list(stocks2)

# Find the max
rx.merge(source1, source2).pipe(
    op.map(lambda v: v[1]),
    op.max()
).subscribe(reporter)

print('-' * 25)

# Find the min
rx.merge(source1, source2).pipe(
    op.map(lambda v: v[1]),
    op.min()
).subscribe(reporter)

print('-' * 25)

# Only publish unique values
rx.merge(source1, source2).pipe(
    op.distinct()
).subscribe(reporter)
import rx
import rx.operators as ops

numbers = rx.from_([1, 2, 1, 3, 3, 2, 4, 5])
numbers.pipe(ops.distinct()).subscribe(
    on_next=lambda i: print("on_next {}".format(i)),
    on_error=lambda e: print("on_error: {}".format(e)),
    on_completed=lambda: print("on_completed"))
Exemplo n.º 9
0
def create_store(initial_state: Optional[ReduxRootState] = None) -> ReduxRootStore:  # pylint: disable=too-many-locals
    """ Constructs a new store that can handle feature modules.

        Args:
            initial_state: optional initial state of the store, will typically be the empty dict

        Returns:
            An implementation of the store
    """

    # current reducer
    reducer: Reducer = identity_reducer

    def replace_reducer(new_reducer: Reducer) -> None:
        """ Callback that replaces the current reducer

            Args:
                new_reducer: the new reducer

        """
        nonlocal reducer
        reducer = new_reducer

    # subject used to dispatch actions
    actions = Subject()

    # the shared action observable
    actions_ = actions.pipe(op.share())

    _dispatch = actions.on_next

    # our current state
    state = BehaviorSubject(initial_state if initial_state else {})

    # shutdown trigger
    done_ = Subject()

    # The set of known modules, to avoid cycles and duplicate registration
    modules: MutableMapping[str, ReduxFeatureModule] = {}

    # Sequence of added modules
    module_subject = Subject()

    # Subscribe to the resolved modules
    module_ = module_subject.pipe(op.distinct(select_id), op.share())

    # Build the reducers
    reducer_ = module_.pipe(
        op.filter(has_reducer),
        op.scan(reduce_reducers, {}),
        op.map(combine_reducers),
        op.map(replace_reducer),
    )

    # Build the epic
    epic_ = module_.pipe(
        op.map(select_epic),
        op.filter(bool),
        op.map(normalize_epic)
    )

    # Root epic that combines all of the incoming epics
    def root_epic(
        action_: Observable, state_: Observable
    ) -> Observable:
        """ Implementation of the root epic. If listens for new epics
            to come in and automatically subscribes.

            Args:
                action_: the action observable
                state_: the state observable

            Returns
                The observable of resulting actions
        """
        return epic_.pipe(
            op.flat_map(run_epic(action_, state_)),
            op.map(_dispatch)
        )

    # notifications about new feature states
    new_module_ = module_.pipe(
        op.map(select_id),
        op.map(create_action(INIT_ACTION)),
        op.map(_dispatch),
    )

    def _add_feature_module(module: ReduxFeatureModule):
        """ Registers a new feature module

            Args:
                module: the new feature module

        """
        module_id = select_id(module)
        if not module_id in modules:
            modules[module_id] = module
            for dep in select_dependencies(module):
                _add_feature_module(dep)
            module_subject.on_next(module)

    # all state
    internal_ = merge(root_epic(actions_, state), reducer_, new_module_).pipe(
        op.ignore_elements()
    )

    def _as_observable() -> Observable:
        """ Returns the state as an observable

            Returns:
                the observable
        """
        return state

    def _on_completed() -> None:
        """ Triggers the done event """
        done_.on_next(None)

    merge(actions_, internal_).pipe(
        op.map(lambda action: reducer(state.value, action)),
        op.take_until(done_),
    ).subscribe(state, logger.error)

    return ReduxRootStore(
        _as_observable, _dispatch, _add_feature_module, _dispatch, _on_completed
    )
Exemplo n.º 10
0
        def create():
            def key_mapper(x):
                return x / 2

            return xs.pipe(ops.distinct(key_mapper))
Exemplo n.º 11
0
 def create():
     return xs.pipe(ops.distinct())
Exemplo n.º 12
0
        def create():
            def key_mapper(x):
                return math.floor(x / 2.0)

            return xs.pipe(ops.distinct(key_mapper))