本文整理汇总了Python中rx.Observable.from_方法的典型用法代码示例。如果您正苦于以下问题:Python Observable.from_方法的具体用法?Python Observable.from_怎么用?Python Observable.from_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.Observable
的用法示例。
在下文中一共展示了Observable.from_方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def main():
Observable.from_(sources) \
.merge_all() \
.subscribe(lambda x: print(x))
示例2: run
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def run(self):
self.socket.listen(5)
def clients_iter():
try:
while True:
yield self.socket.accept()
except:
pass
def send_start(c):
return c[0].send(b'Asterisk Call Manager/6.6.6\r\n\r\n')
Observable.from_(clients_iter()) \
.subscribe(send_start)
示例3: __init__
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def __init__(self, socket_client, topic='gdax'):
self.ws = socket_client
self.delay = 1
self.stream = KafkaStream.consumer(topic='gdax', offset='end')
self.source = Observable.from_(self.stream)
super(AvroListener, self).__init__()
示例4: __sense_environment
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def __sense_environment(self):
Observable.from_(
self.sensors). subscribe(
lambda sensor: self.working_memory.append(
Fact(
sensor=sensor.name,
data=sensor.exec(),
binding=sensor.binding)))
Observable.from_(
self.working_memory). subscribe(
lambda fact: setattr(
self.world_state,
fact.binding,
fact.data.response))
示例5: firstnames_from_db
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def firstnames_from_db(file_name):
file = open(file_name)
# collect and push stored people firstnames
return Observable.from_(file) \
.flat_map(lambda content: content.split(', ')) \
.filter(lambda name: name!='') \
.map(lambda name: name.split()[0]) \
.group_by(lambda firstname: firstname) \
.flat_map(lambda grp: grp.count().map(lambda ct: (grp.key, ct)))
开发者ID:PacktPublishing,项目名称:Mastering-Python-Design-Patterns-Second-Edition,代码行数:12,代码来源:rx_peoplelist_1.py
示例6: frequent_firstnames_from_db
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def frequent_firstnames_from_db(file_name):
file = open(file_name)
# collect and push only the frequent firstnames
return Observable.from_(file) \
.flat_map(lambda content: content.split(', ')) \
.filter(lambda name: name!='') \
.map(lambda name: name.split()[0]) \
.group_by(lambda firstname: firstname) \
.flat_map(lambda grp: grp.count().map(lambda ct: (grp.key, ct))) \
.filter(lambda name_and_ct: name_and_ct[1] > 3)
开发者ID:PacktPublishing,项目名称:Mastering-Python-Design-Patterns-Second-Edition,代码行数:13,代码来源:rx_peoplelist_3.py
示例7: test_accepts_multiple_subscription_fields_defined_in_schema
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def test_accepts_multiple_subscription_fields_defined_in_schema():
# type: () -> None
SubscriptionTypeMultiple = GraphQLObjectType(
name="Subscription",
fields=OrderedDict(
[
("importantEmail", GraphQLField(EmailEventType)),
("nonImportantEmail", GraphQLField(EmailEventType)),
]
),
)
test_schema = GraphQLSchema(query=QueryType, subscription=SubscriptionTypeMultiple)
stream = Subject()
send_important_email, subscription = create_subscription(stream, test_schema)
email = Email(
from_="yuzhi@graphql.org",
subject="Alright",
message="Tests are good",
unread=True,
)
inbox = []
stream.subscribe(inbox.append)
send_important_email(email)
assert len(inbox) == 1
assert inbox[0][0] == email
示例8: test_accepts_type_definition_with_sync_subscribe_function
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def test_accepts_type_definition_with_sync_subscribe_function():
# type: () -> None
SubscriptionType = GraphQLObjectType(
name="Subscription",
fields=OrderedDict(
[
(
"importantEmail",
GraphQLField(
EmailEventType, resolver=lambda *_: Observable.from_([None])
),
)
]
),
)
test_schema = GraphQLSchema(query=QueryType, subscription=SubscriptionType)
stream = Subject()
send_important_email, subscription = create_subscription(stream, test_schema)
email = Email(
from_="yuzhi@graphql.org",
subject="Alright",
message="Tests are good",
unread=True,
)
inbox = []
subscription.subscribe(inbox.append)
send_important_email(email)
assert len(inbox) == 1
assert inbox[0].data == {"importantEmail": None}
示例9: test_produces_a_payload_for_multiple_subscribe_in_same_subscription
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def test_produces_a_payload_for_multiple_subscribe_in_same_subscription():
# type: () -> None
stream = Subject()
send_important_email, subscription1 = create_subscription(stream)
subscription2 = create_subscription(stream)[1]
payload1 = []
payload2 = []
subscription1.subscribe(payload1.append)
subscription2.subscribe(payload2.append)
email = Email(
from_="yuzhi@graphql.org",
subject="Alright",
message="Tests are good",
unread=True,
)
send_important_email(email)
expected_payload = {
"importantEmail": {
"email": {"from": "yuzhi@graphql.org", "subject": "Alright"},
"inbox": {"unread": 1, "total": 2},
}
}
assert payload1[0].data == expected_payload
assert payload2[0].data == expected_payload
# Subscription Publish Phase
示例10: test_uses_the_subscription_schema_for_subscriptions
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def test_uses_the_subscription_schema_for_subscriptions():
# type: () -> None
from rx import Observable
doc = "query Q { a } subscription S { a }"
class Data(object):
a = "b"
c = "d"
ast = parse(doc)
Q = GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)})
S = GraphQLObjectType(
"S",
{
"a": GraphQLField(
GraphQLString, resolver=lambda root, info: Observable.from_(["b"])
)
},
)
result = execute(
GraphQLSchema(Q, subscription=S),
ast,
Data(),
operation_name="S",
allow_subscriptions=True,
)
assert isinstance(result, Observable)
l = []
result.subscribe(l.append)
result = l[0]
assert not result.errors
assert result.data == {"a": "b"}
示例11: create_subscription
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def create_subscription(
stream, # type: Subject
schema=email_schema, # type: GraphQLSchema
ast=None, # type: Optional[Any]
vars=None, # type: Optional[Any]
):
# type: (...) -> Tuple[Callable, Union[ExecutionResult, Observable]]
class Root(object):
class inbox(object):
emails = [
Email(
from_="joe@graphql.org",
subject="Hello",
message="Hello World",
unread=False,
)
]
@staticmethod
def importantEmail():
return stream
def send_important_email(new_email):
# type: (Email) -> None
Root.inbox.emails.append(new_email)
stream.on_next((new_email, Root.inbox))
# stream.on_completed()
default_ast = parse(
"""
subscription {
importantEmail {
email {
from
subject
}
inbox {
unread
total
}
}
}
"""
)
return (
send_important_email,
graphql(schema, ast or default_ast, Root, None, vars, allow_subscriptions=True),
)
示例12: test_produces_a_payload_per_subscription_event
# 需要导入模块: from rx import Observable [as 别名]
# 或者: from rx.Observable import from_ [as 别名]
def test_produces_a_payload_per_subscription_event():
# type: () -> None
stream = Subject()
send_important_email, subscription = create_subscription(stream)
payload = []
subscription.subscribe(payload.append)
send_important_email(
Email(
from_="yuzhi@graphql.org",
subject="Alright",
message="Tests are good",
unread=True,
)
)
expected_payload = {
"importantEmail": {
"email": {"from": "yuzhi@graphql.org", "subject": "Alright"},
"inbox": {"unread": 1, "total": 2},
}
}
assert len(payload) == 1
assert payload[0].data == expected_payload
send_important_email(
Email(
from_="hyo@graphql.org",
subject="Tools",
message="I <3 making things",
unread=True,
)
)
expected_payload = {
"importantEmail": {
"email": {"from": "hyo@graphql.org", "subject": "Tools"},
"inbox": {"unread": 2, "total": 3},
}
}
assert len(payload) == 2
assert payload[-1].data == expected_payload
# The client decides to disconnect
stream.on_completed()
send_important_email(
Email(
from_="adam@graphql.org",
subject="Important",
message="Read me please",
unread=True,
)
)
assert len(payload) == 2