Traverse f2py data structure with the following visit function: def visit(item, parents, result, *args, **kwargs): """ parents is a list of key-"f2py data structure" pairs from which items are taken from. result is a f2py data structure that is filled with the
(obj, visit, parents=[], result=None, *args, **kwargs)
| 3523 | |
| 3524 | |
| 3525 | def traverse(obj, visit, parents=[], result=None, *args, **kwargs): |
| 3526 | '''Traverse f2py data structure with the following visit function: |
| 3527 | |
| 3528 | def visit(item, parents, result, *args, **kwargs): |
| 3529 | """ |
| 3530 | |
| 3531 | parents is a list of key-"f2py data structure" pairs from which |
| 3532 | items are taken from. |
| 3533 | |
| 3534 | result is a f2py data structure that is filled with the |
| 3535 | return value of the visit function. |
| 3536 | |
| 3537 | item is 2-tuple (index, value) if parents[-1][1] is a list |
| 3538 | item is 2-tuple (key, value) if parents[-1][1] is a dict |
| 3539 | |
| 3540 | The return value of visit must be None, or of the same kind as |
| 3541 | item, that is, if parents[-1] is a list, the return value must |
| 3542 | be 2-tuple (new_index, new_value), or if parents[-1] is a |
| 3543 | dict, the return value must be 2-tuple (new_key, new_value). |
| 3544 | |
| 3545 | If new_index or new_value is None, the return value of visit |
| 3546 | is ignored, that is, it will not be added to the result. |
| 3547 | |
| 3548 | If the return value is None, the content of obj will be |
| 3549 | traversed, otherwise not. |
| 3550 | """ |
| 3551 | ''' |
| 3552 | |
| 3553 | if _is_visit_pair(obj): |
| 3554 | if obj[0] == 'parent_block': |
| 3555 | # avoid infinite recursion |
| 3556 | return obj |
| 3557 | new_result = visit(obj, parents, result, *args, **kwargs) |
| 3558 | if new_result is not None: |
| 3559 | assert _is_visit_pair(new_result) |
| 3560 | return new_result |
| 3561 | parent = obj |
| 3562 | result_key, obj = obj |
| 3563 | else: |
| 3564 | parent = (None, obj) |
| 3565 | result_key = None |
| 3566 | |
| 3567 | if isinstance(obj, list): |
| 3568 | new_result = [] |
| 3569 | for index, value in enumerate(obj): |
| 3570 | new_index, new_item = traverse((index, value), visit, |
| 3571 | parents + [parent], result, |
| 3572 | *args, **kwargs) |
| 3573 | if new_index is not None: |
| 3574 | new_result.append(new_item) |
| 3575 | elif isinstance(obj, dict): |
| 3576 | new_result = {} |
| 3577 | for key, value in obj.items(): |
| 3578 | new_key, new_value = traverse((key, value), visit, |
| 3579 | parents + [parent], result, |
| 3580 | *args, **kwargs) |
| 3581 | if new_key is not None: |
| 3582 | new_result[new_key] = new_value |
no test coverage detected
searching dependent graphs…