コード例 #1
0
def predict(sentences,
            word_index,
            index_chunk,
            model: Model,
            model_config: BiLSTMCRFModelConfigure,
            parallel=False) -> Observable:
    x = sentence_to_vec(sentences, word_index, model_config)

    start = time.clock()
    preds = model.predict(x, batch_size=1024)
    print("Predict cost time {} s".format(time.clock() - start))
    tags_encode = np.argmax(preds, axis=2)
    tags_decode = Observable.of(*tags_encode)

    if parallel:
        return Observable.zip(Observable.of(*sentences), tags_decode, lambda s, i: (s, i)) \
            .flat_map(lambda v: Observable.just(v)
                      .subscribe_on(pool_scheduler)
                      .map(lambda v: (v[0], v[1][-len(v[0]):]))
                      .map(lambda v: (v[0], list(map(lambda i: index_chunk[i], v[1]))))
                      .map(lambda v: cut_sentence_str(*v)))
    else:
        return Observable.zip(Observable.of(*sentences), tags_decode, lambda s, i: (s, i)) \
            .map(lambda v: (v[0], v[1][-len(v[0]):])) \
            .map(lambda v: (v[0], list(map(lambda i: index_chunk[i], v[1])))) \
            .map(lambda v: cut_sentence_str(*v))
コード例 #2
0
def test_sliding_window():
    from collections import deque
    window_size = 3

    def adder(agg, val):
        agg.append(val)
        return agg

    Observable.of(1, 2, 3, 4, 5, 6, 7, ) \
        .scan(adder, seed=deque(maxlen=window_size)) \
        .filter(lambda x: len(x) == window_size) \
        .map(tuple) \
        .subscribe(print)
コード例 #3
0
    def on_start(self, connection_context, op_id, params):
        try:
            execution_result = self.execute(connection_context.request_context,
                                            params)
            if not isinstance(execution_result, Observable):
                # pylint cannot find of method
                observable = Observable.of(execution_result, GQL_COMPLETE)  # pylint: disable=E1101
                # Register the operation using None even though we are not implementing async
                # iterators. This is useful for bookkeeping purpose, allowing us to ignore generated
                # events for closed operations and avoid sending an unnecessary web socket messages.
                # Requires that `unsubscribe` is overridden to handle the None case.
                connection_context.register_operation(op_id, None)
            else:
                observable = execution_result
                connection_context.register_operation(op_id, observable)

            def on_complete(conn_context):
                # unsubscribe from the completed operation
                self.on_stop(conn_context, op_id)

            observable.subscribe(
                SubscriptionObserver(
                    connection_context,
                    op_id,
                    self.send_execution_result,
                    self.send_error,
                    on_complete,
                ))

        # appropriate to catch all errors here
        except Exception as e:  # pylint: disable=W0703
            self.send_error(connection_context, op_id, str(e))
コード例 #4
0
   def filter(self, event, size, msgs=2):
      # min size of the event data
      source = Observable.of(event)

      source\
         .map(lambda s: len(s))\
         .filter(lambda i : i>=size)\
         .subscribe(lambda value : print(value)) # an observer is setup here with subscribe function

      # map associet une fonction/transforme fait une action
      # filter garde ce qu'on veut
      # subscribe c'est l'output en fonction d'un type d'events next, error, terminé

      # emits integer each 1000 ms
      # Observables can be created for button events, requests, timers, and even live Twitter feeds.
      Observable.interval(1000)\
         .map(lambda i : "{0} Mississipi".format(i))\
         .subscribe(lambda s : print(s))

      input("press any key to quit\n")

      from random import randint

      ''' use the rx events for the connection with the agent bidder class ?? '''
      three_emissions = Observable.range(1,msgs) # msg are separately sent to different subscribers
      three_random_ints = three_emissions.map(lambda i: randint(1,1000))
      three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i)))
      three_random_ints.subscribe(lambda i: print("subscriber 2 received".format(i)))

      # send the same message to different subscribers
      three_random_ints = three_emissions.mpa(lambda i: randint(1,1000)).publish()
      three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i)))
      three_random_ints.subscribe(lambda i: print("subscriber 1 received".format(i)))
      three_random_ints.connect()

      # .auto_connect(2) hold off until the number of subscriber reach 2
      # then  emissions is firing at this moment

      # Observable.zip(letters, intervals, lambda s, i: (s, i)) \ .subscribe(lambda t: print(t))


      '''
      save event SQL alchemy DB
      from sqlalchemy import create_engine, text
      from rx import Observable

      engine = create_engine('sqlite:///rexon_metals.db')
      conn = engine.connect()

      def customer_for_id(customer_id):
          stmt = text("SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = :id")
          return Observable.from_(conn.execute(stmt, id=customer_id))

      # Query customers with IDs 1, 3, and 5
      Observable.of(1, 3, 5) \
          .flat_map(lambda id: customer_for_id(id)) \
          .subscribe(lambda r: print(r))
      '''
      return
コード例 #5
0
    def _build_local_discover_observable(self, thing_filter):
        """Builds an Observable to discover Things using the local method."""

        found_tds = [
            ThingDescription.from_thing(exposed_thing.thing).to_str()
            for exposed_thing in self._servient.exposed_things
            if self._is_fragment_match(exposed_thing, thing_filter)
        ]

        # noinspection PyUnresolvedReferences
        return Observable.of(*found_tds)
コード例 #6
0
    def on_start(self, connection_context, op_id, params):
        try:
            execution_result = self.execute(
                # Even though this object is referred to as the "request_context", it is
                # actually a IWorkspaceProcessContext. This is a naming restriction from the underlying
                # GeventSubscriptionServer. Here, we create a new request context for every
                # incoming GraphQL request
                connection_context.request_context.create_request_context(),
                params,
            )
            if not isinstance(execution_result, Observable):
                # pylint cannot find of method
                observable = Observable.of(execution_result, GQL_COMPLETE)  # pylint: disable=E1101
                # Register the operation using None even though we are not implementing async
                # iterators. This is useful for bookkeeping purpose, allowing us to ignore generated
                # events for closed operations and avoid sending an unnecessary web socket messages.
                # Requires that `unsubscribe` is overridden to handle the None case.
                connection_context.register_operation(op_id, None)
            else:
                observable = execution_result

                # This registered operation will get overwritten below, but is necessary to handle
                # the messages that get emitted as soon as the subscription is made (e.g. handling
                # subscription events that are already queued up)
                connection_context.register_operation(op_id, observable)

            def on_complete(conn_context):
                # unsubscribe from the completed operation
                self.on_stop(conn_context, op_id)

            disposable = observable.subscribe(
                SubscriptionObserver(
                    connection_context,
                    op_id,
                    self.send_execution_result,
                    self.send_error,
                    on_complete,
                ))

            # replace the registered operation with the subscribed observer, to ensure that it gets
            # cleaned up upon connection close
            connection_context.register_operation(op_id, disposable)

        # appropriate to catch all errors here
        except Exception as e:  # pylint: disable=W0703
            self.send_error(connection_context, op_id, str(e))
コード例 #7
0
 def on_start(self, connection_context, op_id, params):
     try:
         execution_result = self.execute(connection_context.request_context,
                                         params)
         if not isinstance(execution_result, Observable):
             # pylint cannot find of method
             observable = Observable.of(execution_result, GQL_COMPLETE)  # pylint: disable=E1101
         else:
             observable = execution_result
         observable.subscribe(
             SubscriptionObserver(
                 connection_context,
                 op_id,
                 self.send_execution_result,
                 self.send_error,
                 self.on_close,
             ))
     # appropriate to catch all errors here
     except Exception as e:  # pylint: disable=W0703
         self.send_error(connection_context, op_id, str(e))
コード例 #8
0
    def test_of_empty(self):
        results = []

        Observable.of().subscribe_(results.append)

        assert (len(results) == 0)
コード例 #9
0
from sqlalchemy import create_engine, text
from rx import Observable

engine = create_engine('sqlite:///rexon_metals.db')
conn = engine.connect()


def customer_for_id(customer_id):
    stmt = text("SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = :id")
    return Observable.from_(conn.execute(stmt, id=customer_id))


# Query customers with IDs 1, 3, and 5
Observable.of(1, 3, 5) \
    .flat_map(lambda id: customer_for_id(id)) \
    .subscribe(lambda r: print(r))
コード例 #10
0

def intense_calculation(value):
    # sleep for a random short duration between 0.5 to 2.0 seconds to simulate a long-running calculation
    time.sleep(random.randint(5, 20) * .1)
    return value


# calculate number of CPU's, then create a ThreadPoolScheduler with that number of threads
optimal_thread_count = multiprocessing.cpu_count()
pool_scheduler = ThreadPoolScheduler(optimal_thread_count)

# Create Process 1
Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
    .map(lambda s: intense_calculation(s)) \
    .subscribe_on(pool_scheduler) \
    .subscribe(on_next=lambda s: print("PROCESS 1: {0} {1}".format(current_thread().name, s)),
               on_error=lambda e: print(e),
               on_completed=lambda: print("PROCESS 1 done!"))

# Create Process 2
Observable.range(1, 10) \
    .map(lambda s: intense_calculation(s)) \
    .subscribe_on(pool_scheduler) \
    .subscribe(on_next=lambda i: print("PROCESS 2: {0} {1}".format(current_thread().name, i)),
               on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 2 done!"))

# Create Process 3, which is infinite
Observable.interval(1000) \
    .map(lambda i: i * 100) \
    .observe_on(pool_scheduler) \
    .map(lambda s: intense_calculation(s)) \
from sqlalchemy import create_engine, text
from rx import Observable

engine = create_engine('sqlite:///rexon_metals.db')
conn = engine.connect()

def customer_for_id(customer_id):
    stmt = text("SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = :id")
    return Observable.from_(conn.execute(stmt, id=customer_id))

# Query customers with IDs 1, 3, and 5
Observable.of(1, 3, 5) \
    .flat_map(lambda id: customer_for_id(id)) \
    .subscribe(lambda r: print(r))
コード例 #12
0
from rx import Observable, Observer

# Using Observable.range()
letters = Observable.range(1, 10)
letters.subscribe(lambda value: print(value))

# Using Observable.just()
greeting = Observable.of("Hello World!")
greeting.subscribe(lambda value: print(value))
コード例 #13
0
from __future__ import print_function
from rx import Observable, Observer

# Using Observable.range()
# create an Observable that emits a particular range of sequential integers
# http://reactivex.io/documentation/operators/range.html
letters = Observable.range(1, 10)
letters.subscribe(lambda value: print(value))

# Using Observable.just()
# create an Observable that emits a particular item
# http://reactivex.io/documentation/operators/just.html

greeting = Observable.just("Hello World!")
greeting.subscribe(lambda value: print(value))


books = Observable.of('ABC', 'EFG', 'HIJ') \
    .subscribe(lambda s: print(s))
コード例 #14
0
def test_split_list():
    # flat_map does not preserve order
    Observable.of(
        [1, 2, 3],
        [4, 5, 6]).flat_map(lambda l: Observable.from_list(l)).subscribe(print)
コード例 #15
0
def say_hello(name, callback):
    callback('hello {}!'.format(name))


# You can use from_callback to produce more observables using a
# function as a factory.
hello = Observable.from_callback(say_hello)
hello('Rudolf').subscribe(print_value)
hello('observable').subscribe(print_value)

# You can turn a list into an observable.
Observable.from_list([1, 2, 3]).subscribe(print_value)

# You can turn a list of arguments into an observable.
Observable.of(1, 2, 3, 'A', 'B', 'C').subscribe(print_value)

# And for testing, you can use "marbles". Marbles are a visual
# representation of when items are emitted through an observable.

# We have to import the marbles module to activate this function and
# we have to import the TestScheduler.
from rx.testing import marbles, TestScheduler

from rx.concurrency import timeout_scheduler, new_thread_scheduler

test_scheduler = TestScheduler()

# When you create an observable, you have the option of passing in a
# scheduler. The scheduler can be the Python Async IO scheduler, the
# scheduler from Tornado or the scheduler from Q.  The scheduler
コード例 #16
0
ファイル: operators1.py プロジェクト: zwvista/SampleMisc
from rx import Observable

source = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

lengths = source.map(lambda s: len(s))

filtered = lengths.filter(lambda i: i >= 5)

filtered.subscribe(lambda value: print("Received {0}".format(value)))

'''
Received 5
Received 5
Received 5
Received 7
'''
コード例 #17
0
from rx import Observable

letters = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

intervals = Observable.interval(1000)

Observable.zip(letters, intervals, lambda s, i: (s, i)) \
    .subscribe(lambda t: print(t))

input("Press any key to quit\n")

#  O/P
#
# ('Alpha', 0)
# ('Beta', 1)
# ('Gamma', 2)
# ('Delta', 3)
# ('Epsilon', 4)
コード例 #18
0
from __future__ import print_function
from rx import Observable

foo = Observable.interval(500).take(5) \
            .zip(Observable.from_('hello'), lambda x, y: y)

bar = Observable.interval(300).take(7) \
    .zip(Observable.of(0, 1, 0, 1, 0, 1, 0), lambda x, y: y)

# -----H-----e-----l-----l-----o
#---0---1---0---1---0---1---0
#------h-----e-----L-----l-----o

# .subscribe(lambda x: print(x), lambda err: print(x), lambda : print("complete"))

foo.with_latest_from(bar, lambda x, y: x.upper() if y == 1 else x.lower()) \
        .subscribe(lambda x: print(x), lambda err: print(err), lambda : print("complete"))

input("Press some keys\n")

#
# Let's try using it in practice here, and we hit that function to combine the values. Let's run this, and we see there a lower case "H," lower case "E-L-L-O," just like we had outlined here. We use withLatestFrom to map an observable, foo, to another observable, but using the latest value from some other observables.
# That is why there is no static version of the withLatestFrom operator. That's because foo here is somehow special. It's different. It's assuming a different responsibility than bar is because essentially it's not like CombineLatest. If we would use CombineLatest, then when one is emitted on bar, it would be combined with latest value from "H" and that would make upper case "H," but that's not what we want.
# We want basically this to be a mapping of that observable, just using some secondary information from other observables. The main observable is foo, that's what we want to map. That's why foo here is special because it's the main observable and we just want to use secondary information from other observables.
コード例 #19
0
def create_numbers_observable(*args):
    return Observable.of(*args)
コード例 #20
0
 def create():
     return Observable.of(1, 2, 3, 4, 5)
コード例 #21
0
 def create():
     return Observable.of(scheduler=scheduler)
コード例 #22
0

class Query(graphene.ObjectType):
    base = graphene.String()


class RandomType(graphene.ObjectType):
    booler = graphene.String()
    seconds = graphene.Int()
    random_int = graphene.Int()


# data = Observable.subscribe(observer=)

flag = True
ret = Observable.of(RandomType(seconds=1, booler=0))

source = Observable


class Subscription(graphene.ObjectType):

    count_seconds = graphene.Int(up_to=graphene.Int())

    random_int = graphene.Field(RandomType)
    print("random_int")
    print(count_seconds.args)

    def resolve_count_seconds(root, info, up_to=5):
        print("callled")
        print(up_to)
コード例 #23
0
from rx import Observable

numbers = Observable.of(1, 2, 3, 4)
numbers.subscribe(on_next=lambda i: print("item: {}".format(i)),
                  on_error=lambda e: print("error: {}".format(e)),
                  on_completed=lambda: print("completed"))
コード例 #24
0
ファイル: test_of.py プロジェクト: AlexMost/RxPY
    def test_of_empty(self):
        results = []

        Observable.of().subscribe(results.append)

        assert(len(results) == 0)
from rx import Observable

source = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

lengths = source.map(lambda s: len(s))

filtered = lengths.filter(lambda i: i >= 5)

filtered.subscribe(lambda value: print("Received {0}".format(value)))



# Chaining

from rx import Observable

Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
    .map(lambda s: len(s)) \
    .filter(lambda i: i >= 5) \
    .subscribe(lambda value: print("Received {0}".format(value)))
コード例 #26
0
ファイル: test_of.py プロジェクト: AlexMost/RxPY
 def create():
     return Observable.of(scheduler=scheduler)
コード例 #27
0
ファイル: test_of.py プロジェクト: AlexMost/RxPY
    def test_of(self):
        results = []

        Observable.of(1,2,3,4,5).subscribe(results.append)

        assert(str([1,2,3,4,5]) == str(results))
コード例 #28
0
ファイル: alignment.py プロジェクト: zwvista/SampleMisc
from rx import Observable, Observer

xs = Observable.of(1,2,3)
ys = Observable.of(4,5,6)
zs = xs + ys  # Concatenate observables
zs.to_list().subscribe(lambda value: print(value))

xs = Observable.of(1,2,3)
ys = xs * 4
ys.to_list().subscribe(lambda value: print(value))

xs = Observable.of(1,2,3)
ys = xs[1:-1]
ys.to_list().subscribe(lambda value: print(value))

xs = Observable.of(1,2,3,4,5,6)
ys = xs.to_blocking()
zs = (x*x for x in ys if x > 3)
for x in zs:
    print(x)

'''
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
[2]
16
25
36
'''
コード例 #29
0
ファイル: test_of.py プロジェクト: AlexMost/RxPY
 def create():
     return Observable.of(1,2,3,4,5, scheduler=scheduler)
コード例 #30
0
from __future__ import print_function
from rx import Observable, Observer


foo = Observable.of('h', 'e', 'l', 'l', 'o') \
        .zip(Observable.interval(600).take(5), lambda x, y: x)

# -----h-----e-----l-----l-----o|
#
# buffer_with_count(2)
#
# ----------[h,e]-------[l,l]--[o]|

# -----h-----e-----l-----l-----o|
# --------x--------x--------x
# buffer_with_time(9000)
#
# --------h--------e--------ll-0|

# -----h-----e-----l-----l-----o| (foo)
# buffer(closing observable)
# --------0--------1--------2|    (bar)
#
# -------h-------e---------ll|

bar = Observable.interval(800).take(3)

# foo.buffer_with_count(2).subscribe(lambda x: print(x))
# foo.buffer_with_time(900).subscribe(lambda x: print(x))
foo.buffer(bar).subscribe(lambda x: print(x), lambda err: print(err),
                          lambda: print("DONE"))
コード例 #31
0
ファイル: combining.py プロジェクト: zwvista/SampleMisc
from rx import Observable

letters = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

intervals = Observable.interval(1000)

Observable.zip(letters, intervals, lambda s, i: (s, i)) \
    .subscribe(lambda t: print(t))

input("Press any key to quit\n")

'''
Press any key to quit
('Alpha', 0)
('Beta', 1)
('Gamma', 2)
('Delta', 3)
('Epsilon', 4)
'''
コード例 #32
0
ファイル: test_of.py プロジェクト: zmyer/RxPY
 def create():
     return Observable.of(1, 2, 3, 4, 5, scheduler=scheduler)
コード例 #33
0
 def resolve_hello(root, info):
     return Observable.of("hello world!")
コード例 #34
0
ファイル: rxPy1.py プロジェクト: mikepy21/OthersPy
from rx import Observable

source = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

lengths = source.map(lambda s: len(s))

filtered = lengths.filter(lambda i: i >= 5)

filtered.subscribe(lambda value: print("Received {0}".format(value)))
コード例 #35
0
from rx import Observable

source = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

lengths = source.map(lambda s: len(s))

filtered = lengths.filter(lambda i: i >= 5)

filtered.subscribe(lambda value: print("Received {0}".format(value)))

# using chaining

Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
    .map(lambda s: len(s)) \
    .filter(lambda i: i >= 5) \
    .subscribe(lambda value: print("Received {0}".format(value)))
コード例 #36
0
    def test_of(self):
        results = []

        Observable.of(1, 2, 3, 4, 5).subscribe_(results.append)

        assert (str([1, 2, 3, 4, 5]) == str(results))