|
| 1 | +from pytm import Dataflow as DF |
| 2 | +from pytm import Element |
| 3 | + |
| 4 | + |
| 5 | +def req_reply(src: Element, dest: Element, req_name: str, reply_name=None) -> (DF, DF): |
| 6 | + ''' |
| 7 | + This function creates two datflows where one dataflow is a request |
| 8 | + and the second dataflow is the corresponding reply to the newly created request. |
| 9 | +
|
| 10 | + Args: |
| 11 | + req_name: name of the request dataflow |
| 12 | + reply_name: name of the reply datadlow |
| 13 | + if not set the name will be "Reply to <name>" |
| 14 | +
|
| 15 | + Usage: |
| 16 | + query_titles, reply_titles = req_reply(api, database, 'Query book titles') |
| 17 | +
|
| 18 | + view_authors, reply_authors = req_reply(api, database, |
| 19 | + req_name='Query authors view', |
| 20 | + reply_name='Authors, with top titles') |
| 21 | +
|
| 22 | + Returns: |
| 23 | + a tuple of two dataflows, where the first is the request and the second is the reply. |
| 24 | +
|
| 25 | + ''' |
| 26 | + if not reply_name: |
| 27 | + reply_name = f'Reply to {req_name}' |
| 28 | + req = DF(src, dest, req_name) |
| 29 | + reply = DF(dest, src, name=reply_name) |
| 30 | + reply.responseTo = req |
| 31 | + return req, reply |
| 32 | + |
| 33 | + |
| 34 | +def reply(req: DF, **kwargs) -> DF: |
| 35 | + ''' |
| 36 | + This function takes a dataflow as an argument and returns a new dataflow, which is a response to the given dataflow. |
| 37 | +
|
| 38 | + Args: |
| 39 | + req: a dataflow for which a reply should be generated |
| 40 | + kwargs: key word arguments for the newly created reply |
| 41 | + Usage: |
| 42 | + client_query = Dataflow(client, api, "Get authors page") |
| 43 | + api_query = Dataflow(api, database, 'Get authors') |
| 44 | + api_reply = reply(api_query) |
| 45 | + client_reply = reply(client_query) |
| 46 | + Returns: |
| 47 | + a Dataflow which is a reply to the given datadlow req |
| 48 | + ''' |
| 49 | + if 'name' not in kwargs: |
| 50 | + name = f'Reply to {req.name}' |
| 51 | + else: |
| 52 | + name = kwargs['name'] |
| 53 | + del kwargs['name'] |
| 54 | + reply = DF(req.sink, req.source, name, **kwargs) |
| 55 | + reply.responseTo = req |
| 56 | + return req, reply |
0 commit comments