का उपयोग करके अजगर में जिम्मेदारी पैटर्न की श्रृंखला लागू करना मैं पाइथन में विभिन्न अवधारणाओं की खोज कर रहा हूं और मुझे कोरआउटिन के उदाहरण पर पढ़ना पड़ा जिसका उपयोग जिम्मेदारी डिजाइन पैटर्न की श्रृंखला के लिए किया जा सकता है।कोरआउट
from functools import wraps
def coroutine(function):
@wraps(function)
def wrapper(*args, **kwargs):
generator = function(*args, **kwargs)
next(generator)
return generator
return wrapper
@coroutine
def PlatinumCustomer(successor=None):
cust = (yield)
if cust.custtype == 'platinum':
print "Platinum Customer"
elif successor is not None:
successor.send(cust)
@coroutine
def GoldCustomer(successor=None):
cust = (yield)
if cust.custtype == 'gold':
print "Gold Customer"
elif successor is not None:
successor.send(cust)
@coroutine
def SilverCustomer(successor=None):
cust = (yield)
if cust.custtype == 'silver':
print "Silver Customer"
elif successor is not None:
successor.send(cust)
@coroutine
def DiamondCustomer(successor=None):
cust = (yield)
if cust.custtype == 'diamond':
print "Diamond Customer"
elif successor is not None:
successor.send(cust)
class Customer:
pipeline = PlatinumCustomer(GoldCustomer(SilverCustomer(DiamondCustomer())))
def __init__(self,custtype):
self.custtype = custtype
def HandleCustomer(self):
try:
self.pipeline.send(self)
except StopIteration:
pass
if __name__ == '__main__':
platinum = Customer('platinum')
gold = Customer('gold')
silver = Customer('silver')
diamond = Customer('diamond')
undefined = Customer('undefined')
platinum.HandleCustomer()
gold.HandleCustomer()
undefined.HandleCustomer()
क्या मैं यहाँ करने के लिए कोशिश की है ग्राहकों (प्लेटिनम, सोना, हीरा, रजत) के विभिन्न प्रकार से निपटने के लिए जिम्मेदारी पैटर्न समाधान की एक श्रृंखला बनाने का प्रयास है: मैं निम्नलिखित कोड लिखा था।
उस ग्राहक के पास एक पाइपलाइन है जहां मैंने उस आदेश का उल्लेख किया है जिसमें विभिन्न ग्राहकों को संभाला जाएगा। ग्राहक() हैंडल कस्टमर पाइपलाइन के माध्यम से खुद का एक उदाहरण भेजेगा जो जांच करेगा कि उसके कस्टमाइप से मेल खाता है या फिर उसे इसके उत्तराधिकारी (यदि उपलब्ध हो)
समस्या: समस्या यह है कि मैं उपर्युक्त स्क्रिप्ट चलाता हूं, यह पहले प्लैटिनम ग्राहक को संभालेगा लेकिन सोने या अपरिभाषित नहीं होगा। मुझे लगता है कि ऐसा इसलिए है क्योंकि वह जनरेटर के अंत तक पहुंच गया है। मैं कोड को कैसे संशोधित करूं ताकि प्रत्येक बार जब यह ग्राहक का एक नया उदाहरण हो, तो यह इसकी शुरुआत से पाइपलाइन के माध्यम से जाएगा?
एक आकर्षण की तरह काम करता है। – Rivas
अपवर्तित करने में सक्षम नहीं होने के लिए मैं क्षमा चाहता हूं। मेरे पास पर्याप्त प्रतिष्ठा नहीं है। – Rivas