Smart way of turning object into an Apply
(obj)
| 194 | |
| 195 | |
| 196 | def as_apply(obj): |
| 197 | """Smart way of turning object into an Apply""" |
| 198 | if isinstance(obj, Apply): |
| 199 | rval = obj |
| 200 | elif isinstance(obj, tuple): |
| 201 | rval = Apply("pos_args", [as_apply(a) for a in obj], {}, len(obj)) |
| 202 | elif isinstance(obj, list): |
| 203 | rval = Apply("pos_args", [as_apply(a) for a in obj], {}, None) |
| 204 | elif isinstance(obj, dict): |
| 205 | items = list(obj.items()) |
| 206 | # -- should be fine to allow numbers and simple things |
| 207 | # but think about if it's ok to allow Apply objects |
| 208 | # it messes up sorting at the very least. |
| 209 | items.sort() |
| 210 | if all(isinstance(k, str) for k in obj): |
| 211 | named_args = [(k, as_apply(v)) for (k, v) in items] |
| 212 | rval = Apply("dict", [], named_args, len(named_args)) |
| 213 | else: |
| 214 | new_items = [(k, as_apply(v)) for (k, v) in items] |
| 215 | rval = Apply("dict", [as_apply(new_items)], {}, o_len=None) |
| 216 | else: |
| 217 | rval = Literal(obj) |
| 218 | assert isinstance(rval, Apply) |
| 219 | return rval |
| 220 | |
| 221 | |
| 222 | class Apply: |