Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module provides argument manipulation functions like pop_arg. |
| 5 | """ |
| 6 | |
| 7 | import gen_print as gp |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 8 | import collections |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 9 | |
| 10 | |
Michael Walsh | eefa8d9 | 2019-11-20 13:42:38 -0600 | [diff] [blame] | 11 | def pop_arg(pop_arg_default=None, *args, **kwargs): |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 12 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 13 | Pop a named argument from the args/kwargs and return a tuple consisting of the argument value, the |
| 14 | modified args and the modified kwargs. |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 15 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 16 | The name of the argument is determined automatically by this function by examining the source code which |
| 17 | calls it (see examples below). If no suitable argument can be found, the default value passed to this |
| 18 | function will be returned as the argument value. This function is useful for wrapper functions that wish |
| 19 | to process arguments in some way before calling subordinate function. |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 20 | |
| 21 | Examples: |
| 22 | |
| 23 | Given this code: |
| 24 | |
| 25 | def func1(*args, **kwargs): |
| 26 | |
| 27 | last_name, args, kwargs = pop_arg('Doe', *args, **kwargs) |
| 28 | some_function(last_name.capitalize(), *args, **kwargs) |
| 29 | |
| 30 | Consider this call to func1: |
| 31 | |
| 32 | func1('Johnson', ssn='111-11-1111') |
| 33 | |
| 34 | The pop_arg in func1 would return the following: |
| 35 | |
| 36 | 'Johnson', [], {'ssn': "111-11-1111"} |
| 37 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 38 | Notice that the 'args' value returned is an empty list. Since last_name was assumed to be the first |
| 39 | positional argument, it was popped from args. |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 40 | |
| 41 | Now consider this call to func1: |
| 42 | |
| 43 | func1(last_name='Johnson', ssn='111-11-1111') |
| 44 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 45 | The pop_arg in func1 would return the same last_name value as in the previous example. The only |
| 46 | difference being that the last_name value was popped from kwargs rather than from args. |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 47 | |
| 48 | Description of argument(s): |
Michael Walsh | eefa8d9 | 2019-11-20 13:42:38 -0600 | [diff] [blame] | 49 | pop_arg_default The value to return if the named argument is not present in args/kwargs. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 50 | args The positional arguments passed to the calling function. |
| 51 | kwargs The keyword arguments passed to the calling function. |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 52 | """ |
| 53 | |
| 54 | # Retrieve the argument name by examining the source code. |
| 55 | arg_name = gp.get_arg_name(None, arg_num=-3, stack_frame_ix=2) |
| 56 | if arg_name in kwargs: |
| 57 | arg_value = kwargs.pop(arg_name) |
| 58 | else: |
| 59 | # Convert args from a tuple to a list. |
| 60 | args = list(args) |
| 61 | if args: |
| 62 | arg_value = args.pop(0) |
| 63 | else: |
Michael Walsh | eefa8d9 | 2019-11-20 13:42:38 -0600 | [diff] [blame] | 64 | arg_value = pop_arg_default |
Michael Walsh | 0ff2eed | 2019-03-12 16:21:47 -0500 | [diff] [blame] | 65 | |
| 66 | return arg_value, args, kwargs |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def source_to_object(value): |
| 70 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 71 | Evaluate string value as python source code and return the resulting object. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 72 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 73 | If value is NOT a string or can not be interpreted as a python source object definition, simply return |
| 74 | value. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 75 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 76 | The idea is to convert python object definition source code (e.g. for lists, dictionaries, tuples, etc.) |
| 77 | into an object. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 78 | |
| 79 | Example: |
| 80 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 81 | Note that this first example is a special case in that it is a short-cut for specifying a |
| 82 | collections.OrderedDict. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 83 | |
| 84 | result = source_to_object("[('one', 1), ('two', 2), ('three', 3)]") |
| 85 | |
| 86 | The result is a collections.OrderedDict object: |
| 87 | |
| 88 | result: |
| 89 | [one]: 1 |
| 90 | [two]: 2 |
| 91 | [three]: 3 |
| 92 | |
| 93 | This is a short-cut for the long form shown here: |
| 94 | |
| 95 | result = source_to_object("collections.OrderedDict([ |
| 96 | ('one', 1), |
| 97 | ('two', 2), |
| 98 | ('three', 3)])") |
| 99 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 100 | Also note that support for this special-case short-cut precludes the possibility of interpreting such a |
| 101 | string as a list of tuples. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 102 | |
| 103 | Example: |
| 104 | |
| 105 | In this example, the result will be a list: |
| 106 | |
| 107 | result = source_to_object("[1, 2, 3]") |
| 108 | |
| 109 | result: |
| 110 | result[0]: 1 |
| 111 | result[1]: 2 |
| 112 | result[2]: 3 |
| 113 | |
| 114 | Example: |
| 115 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 116 | In this example, the value passed to this function is not a string, so it is simply returned. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 117 | |
| 118 | result = source_to_object(1) |
| 119 | |
| 120 | More examples: |
| 121 | result = source_to_object("dict(one=1, two=2, three=3)") |
| 122 | result = source_to_object("{'one':1, 'two':2, 'three':3}") |
| 123 | result = source_to_object(True) |
| 124 | etc. |
| 125 | |
| 126 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 127 | value If value is a string, it will be evaluated as a python statement. If the |
| 128 | statement is valid, the resulting object will be returned. In all other |
| 129 | cases, the value will simply be returned. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 130 | """ |
| 131 | |
| 132 | if type(value) not in gp.get_string_types(): |
| 133 | return value |
| 134 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 135 | # Strip white space prior to attempting to interpret the string as python code. |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 136 | value = value.strip() |
| 137 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 138 | # Try special case of collections.OrderedDict which accepts a list of tuple pairs. |
Michael Walsh | 3af6087 | 2019-08-01 11:13:18 -0500 | [diff] [blame] | 139 | if value.startswith("[("): |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 140 | try: |
| 141 | return eval("collections.OrderedDict(" + value + ")") |
| 142 | except (TypeError, NameError, ValueError): |
| 143 | pass |
| 144 | |
| 145 | try: |
| 146 | return eval(value) |
| 147 | except (NameError, SyntaxError): |
| 148 | pass |
| 149 | |
| 150 | return value |
| 151 | |
| 152 | |
| 153 | def args_to_objects(args): |
| 154 | r""" |
| 155 | Run source_to_object() on each element in args and return the result. |
| 156 | |
| 157 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 158 | args A type of dictionary, list, set, tuple or simple object whose elements |
| 159 | are to be converted via a call to source_to_object(). |
Michael Walsh | c28deec | 2019-05-17 15:35:51 -0500 | [diff] [blame] | 160 | """ |
| 161 | |
| 162 | type_of_dict = gp.is_dict(args) |
| 163 | if type_of_dict: |
| 164 | if type_of_dict == gp.dict_type(): |
| 165 | return {k: source_to_object(v) for (k, v) in args.items()} |
| 166 | elif type_of_dict == gp.ordered_dict_type(): |
| 167 | return collections.OrderedDict((k, v) for (k, v) in args.items()) |
| 168 | elif type_of_dict == gp.dot_dict_type(): |
| 169 | return DotDict((k, v) for (k, v) in args.items()) |
| 170 | elif type_of_dict == gp.normalized_dict_type(): |
| 171 | return NormalizedDict((k, v) for (k, v) in args.items()) |
| 172 | # Assume args is list, tuple or set. |
| 173 | if type(args) in (list, set): |
| 174 | return [source_to_object(arg) for arg in args] |
| 175 | elif type(args) is tuple: |
| 176 | return tuple([source_to_object(arg) for arg in args]) |
| 177 | |
| 178 | return source_to_object(args) |