MCPcopy
hub / github.com/hyperopt/hyperopt / as_apply

Function as_apply

hyperopt/pyll/base.py:196–219  ·  view source on GitHub ↗

Smart way of turning object into an Apply

(obj)

Source from the content-addressed store, hash-verified

194
195
196def 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
222class Apply:

Callers 15

test_lnormFunction · 0.90
test_repeatableFunction · 0.90
test_sampleFunction · 0.90
test_as_apply_literalFunction · 0.90

Calls 3

ApplyClass · 0.85
LiteralClass · 0.85
itemsMethod · 0.80

Tested by 15

test_lnormFunction · 0.72
test_repeatableFunction · 0.72
test_sampleFunction · 0.72
test_as_apply_literalFunction · 0.72