{"id":1588,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u53cd\u5e94\u5f0f\u7f16\u7a0b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1588.html","title":{"rendered":"Python\u53cd\u5e94\u5f0f\u7f16\u7a0b"},"content":{"rendered":"
\n
Python\u53cd\u5e94\u5f0f\u7f16\u7a0b\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n on_next()\u4e8b\u4ef6<\/b>-\u8fd9\u610f\u5473\u7740\u6570\u636e\u6d41\u4e2d\u6709\u4e00\u4e2a\u5143\u7d20\u3002<\/span> get_strings()<\/b>-\u7528\u4e8e\u4ece\u89c2\u5bdf\u8005\u83b7\u53d6\u5b57\u7b26\u4e32\u3002<\/span> <\/body>\u7528\u4e8e\u53cd\u5e94\u5f0f\u7f16\u7a0b\u7684ReactiveX\u6216RX <\/h2>\n
\u53ef\u89c2\u5bdf\u7684\u73ed\u7ea7<\/h3>\n
\u89c2\u5bdf\u8005\u7c7b<\/h3>\n
\n observable <\/b>\u53d1\u51fa\u7684\u6570\u636e\u6d41\u3002\u53ef\u4ee5\u6709\u591a\u4e2a\u53ef\u89c2\u5bdf\u7684\u89c2\u5bdf\u8005\uff0c\u6bcf\u4e2a\u89c2\u5bdf\u8005\u5c06\u63a5\u6536\u53d1\u5c04\u7684\u6bcf\u4e2a\u6570\u636e\u9879\u3002\u89c2\u5bdf\u8005\u53ef\u4ee5\u901a\u8fc7\u8ba2\u9605\u6765\u63a5\u6536\u4e09\u79cd\u7c7b\u578b\u7684\u4e8b\u4ef6\n <\/div>\n
\n on_completed()\u4e8b\u4ef6<\/b>-\u8fd9\u8868\u793a\u6392\u653e\u5df2\u7ed3\u675f\uff0c\u6ca1\u6709\u66f4\u591a\u7684\u7269\u54c1\u8981\u6765\u3002<\/span>
\n on_error()\u4e8b\u4ef6<\/b>-\u8fd9\u4e5f\u610f\u5473\u7740\u53d1\u5c04\u5df2\u7ecf\u7ed3\u675f\uff0c\u4f46\u662f\u5982\u679c observable <\/b>\u629b\u51fa\u4e86\u9519\u8bef\u3002<\/span> <\/p>\n RxPY \u2013\u7528\u4e8e\u53cd\u5e94\u5f0f\u7f16\u7a0b\u7684Python\u6a21\u5757<\/h2>\n
pip install RxPY
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u793a\u4f8b<\/h3>\n
\n RxPY <\/b>\u6a21\u5757\u53ca\u5176\u7c7b
\n Observable <\/b>\u548c
\n Observe\u7528\u4e8e<\/b>\u53cd\u5e94\u5f0f\u7f16\u7a0b\u3002\u57fa\u672c\u4e0a\u6709\u4e24\u4e2a\u7c7b-\n <\/div>\n
\n PrintObserver()<\/b>-\u7528\u4e8e\u4ece\u89c2\u5bdf\u8005\u6253\u5370\u5b57\u7b26\u4e32\u3002\u5b83\u4f7f\u7528\u89c2\u5bdf\u8005\u7c7b\u7684\u6240\u6709\u4e09\u4e2a\u4e8b\u4ef6\u3002\u5b83\u8fd8\u4f7f\u7528\u4e86subscription()\u7c7b\u3002<\/span> <\/p>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>rx import <\/span>Observable, Observer
def <\/span>get_strings(observer):
observer.on_next<\/span>(\"Ram\"<\/span>)
observer.on_next<\/span>(\"Mohan\"<\/span>)
observer.on_next<\/span>(\"Shyam\"<\/span>)
observer.on_completed<\/span>()
class <\/span>PrintObserver(Observer):
def <\/span>on_next(self, value):
print(\"Received {0}\"<\/span>.format<\/span>(value))
def <\/span>on_completed(self):
print(\"Finished\"<\/span>)
def <\/span>on_error(self, error):
print(\"Error: {0}\"<\/span>.format<\/span>(error))
source = Observable.create<\/span>(get_strings)
source.subscribe<\/span>(PrintObserver())
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
Received Ram
Received Mohan
Received Shyam
Finished
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u7528\u4e8e\u53cd\u5e94\u5f0f\u7f16\u7a0b\u7684PyFunctional\u5e93<\/h2>\n
RxPY\u4e0ePyFunctional\u4e4b\u95f4\u7684\u533a\u522b<\/h3>\n
\n RxPY <\/b>\u5904\u7406\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u548c\u4e8b\u4ef6\uff0c\u800c
\n PyFunctional <\/b>\u4e13\u6ce8\u4e8e\u4f7f\u7528\u529f\u80fd\u7f16\u7a0b\u8303\u4f8b\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u3002\n <\/div>\n\u5b89\u88c5PyFunctional\u6a21\u5757<\/h3>\n
pip install pyfunctional
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u793a\u4f8b<\/h3>\n
\n PyFunctional <\/b>\u6a21\u5757\u53ca\u5176
\n seq <\/b>\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u5145\u5f53\u6211\u4eec\u53ef\u4ee5\u8fed\u4ee3\u548c\u64cd\u4f5c\u7684\u6d41\u5bf9\u8c61\u3002\u5728\u8be5\u7a0b\u5e8f\u4e2d\uff0c\u5b83\u4f7f\u7528lamda\u51fd\u6570\u6620\u5c04\u5e8f\u5217\uff0c\u8be5\u51fd\u6570\u5c06\u6bcf\u4e2a\u503c\u52a0\u500d\uff0c\u7136\u540e\u8fc7\u6ee4x\u5927\u4e8e4\u7684\u503c\uff0c\u6700\u540e\u5c06\u5e8f\u5217\u7f29\u51cf\u4e3a\u6240\u6709\u5269\u4f59\u503c\u7684\u603b\u548c\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
from <\/span>functional import <\/span>seq
result = seq(1,2,3).map<\/span>(lambda <\/span>x: x*2).filter<\/span>(lambda <\/span>x: x > 4).reduce<\/span>(lambda <\/span>x, y: x + y)
print <\/span>(\"Result: {}\"<\/span>.format<\/span>(result))
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
Result: 6
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53cd\u5e94\u5f0f\u7f16\u7a0bzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-1588","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1588"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1588"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1588\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}