def random_or_specified_value(cls, value):
"""
Helper utility for choosing between a user-specified value for a field or a randomly generated value.
:param value: Either a lambda type or a non-lambda type.
:return: The value itself if not a lambda type, otherwise the value of the evaluated lambda (random value)
"""
return value() if isinstance(value, types.LambdaType) else value
python类LambdaType()的实例源码
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd
def _extract_type(typ):
if isinstance(typ, LambdaType):
return typ()
else:
return typ
def __init__(self, schema: LambdaType) -> None:
if callable(schema) and isinstance(schema, LambdaType):
self.schema = schema
else:
raise UndefinedSchema()
def get_argspec(ob):
"""Get a string describing the arguments for the given object."""
argspec = ""
if ob is not None:
if isinstance(ob, type):
fob = _find_constructor(ob)
if fob is None:
fob = lambda: None
elif isinstance(ob, types.MethodType):
fob = ob.__func__
else:
fob = ob
if isinstance(fob, (types.FunctionType, types.LambdaType)):
argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
pat = re.compile('self\,?\s*')
argspec = pat.sub("", argspec)
doc = getattr(ob, "__doc__", "")
if doc:
doc = doc.lstrip()
pos = doc.find("\n")
if pos < 0 or pos > 70:
pos = 70
if argspec:
argspec += "\n"
argspec += doc[:pos]
return argspec
#################################################
#
# Test code
#
def get_config(self):
if isinstance(self.mode, python_types.LambdaType):
mode = func_dump(self.mode)
mode_type = 'lambda'
elif callable(self.mode):
mode = self.mode.__name__
mode_type = 'function'
else:
mode = self.mode
mode_type = 'raw'
if isinstance(self._output_shape, python_types.LambdaType):
output_shape = func_dump(self._output_shape)
output_shape_type = 'lambda'
elif callable(self._output_shape):
output_shape = self._output_shape.__name__
output_shape_type = 'function'
else:
output_shape = self._output_shape
output_shape_type = 'raw'
if isinstance(self._output_mask, python_types.LambdaType):
output_mask = func_dump(self._output_mask)
output_mask_type = 'lambda'
elif callable(self._output_mask):
output_mask = self._output_mask.__name__
output_mask_type = 'function'
else:
output_mask = self._output_mask
output_mask_type = 'raw'
return {'name': self.name,
'mode': mode,
'mode_type': mode_type,
'concat_axis': self.concat_axis,
'dot_axes': self.dot_axes,
'output_shape': output_shape,
'output_shape_type': output_shape_type,
'output_mask': output_mask,
'output_mask_type': output_mask_type,
'arguments': self.arguments}
def __init__(self, lambd):
assert type(lambd) is types.LambdaType
self.lambd = lambd
def getmethparlist(ob):
"Get strings describing the arguments for the given object"
argText1 = argText2 = ""
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
if type(ob)==types.MethodType:
fob = ob.im_func
argOffset = 1
else:
fob = ob
argOffset = 0
# Try and build one for Python defined functions
if type(fob) in [types.FunctionType, types.LambdaType]:
try:
counter = fob.func_code.co_argcount
items2 = list(fob.func_code.co_varnames[argOffset:counter])
realArgs = fob.func_code.co_varnames[argOffset:counter]
defaults = fob.func_defaults or []
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
items1 = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
if fob.func_code.co_flags & 0x4:
items1.append("*"+fob.func_code.co_varnames[counter])
items2.append("*"+fob.func_code.co_varnames[counter])
counter += 1
if fob.func_code.co_flags & 0x8:
items1.append("**"+fob.func_code.co_varnames[counter])
items2.append("**"+fob.func_code.co_varnames[counter])
argText1 = ", ".join(items1)
argText1 = "(%s)" % argText1
argText2 = ", ".join(items2)
argText2 = "(%s)" % argText2
except:
pass
return argText1, argText2
def getmethparlist(ob):
"Get strings describing the arguments for the given object"
argText1 = argText2 = ""
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
if type(ob)==types.MethodType:
fob = ob.im_func
argOffset = 1
else:
fob = ob
argOffset = 0
# Try and build one for Python defined functions
if type(fob) in [types.FunctionType, types.LambdaType]:
try:
counter = fob.func_code.co_argcount
items2 = list(fob.func_code.co_varnames[argOffset:counter])
realArgs = fob.func_code.co_varnames[argOffset:counter]
defaults = fob.func_defaults or []
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
items1 = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
if fob.func_code.co_flags & 0x4:
items1.append("*"+fob.func_code.co_varnames[counter])
items2.append("*"+fob.func_code.co_varnames[counter])
counter += 1
if fob.func_code.co_flags & 0x8:
items1.append("**"+fob.func_code.co_varnames[counter])
items2.append("**"+fob.func_code.co_varnames[counter])
argText1 = ", ".join(items1)
argText1 = "(%s)" % argText1
argText2 = ", ".join(items2)
argText2 = "(%s)" % argText2
except:
pass
return argText1, argText2
data_augmentation.py 文件源码
项目:single_shot_multibox_detector
作者: oarriaga
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd
def get_config(self):
if isinstance(self.function, python_types.LambdaType):
function = func_dump(self.function)
function_type = 'lambda'
else:
function = self.function.__name__
function_type = 'function'
config = {
'function': function,
'function_type': function_type,
'arguments': self.arguments
}
base_config = super(Lambda, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def getSubview(self, request, node, model, viewName):
"""Get a sub-view from me.
@returns: L{widgets.Widget}
"""
view = None
vm = getattr(self, 'wvfactory_' + viewName, None)
if vm is None:
vm = getattr(self, 'factory_' + viewName, None)
if vm is not None:
warnings.warn("factory_ methods are deprecated; please use "
"wvfactory_ instead", DeprecationWarning)
if vm:
if vm.func_code.co_argcount == 3 and not type(vm) == types.LambdaType:
warnings.warn("wvfactory_ methods take (request, node, "
"model) instead of (request, node) now. \n"
"Please instantiate your widgets with a "
"reference to model instead of self.model",
DeprecationWarning)
self.model = model
view = vm(request, node)
self.model = self.mainModel
else:
view = vm(request, node, model)
setupMethod = getattr(self, 'wvupdate_' + viewName, None)
if setupMethod:
if view is None:
view = widgets.Widget(model)
view.setupMethods.append(setupMethod)
return view
def isFunType(obj):
'''??obj????????lambda??
'''
return isinstance(obj,types.MethodType) or isinstance(obj,types.LambdaType)
def _getFuncResult(func,args):
'''?????????????????????????
'''
if not isinstance(func,types.MethodType) and not isinstance(func,types.LambdaType):
raise TypeError("func type %s is not a MethodType or LambdaType" % type(func))
if dict == type(args):
actret = func(**args)
elif tuple == type(args):
actret = func(*args)
else:
actret = func(args)
return actret
def _waitForCompareResult(func,args,compareobj,timeout=10,interval=0.5):
''' ????????
:param actualfunc: ??????????
:param actargs: ???????????
:param compareobj: ??????????????????????
:return comparefunc: True or False
:param timeout: ????
:param interval: ??????
:return type: tuple,(True,try_count,actual,expect)
'''
start = time.time()
waited = 0.0
try_count = 0
while True:
try_count +=1
actret = _getFuncResult(func,args)
if isinstance(compareobj,types.MethodType) or isinstance(compareobj,types.LambdaType):
expret = _getFuncResult(compareobj,actret)
if expret == True:
return True,try_count,actret,expret
else:
expret = compareobj
if actret == expret:
return True,try_count,actret,expret
waited = time.time() - start
if waited < timeout:
time.sleep(min(interval, timeout - waited))
else:
return False,try_count,actret,expret
def __init__(self, lambd):
assert isinstance(lambd, types.LambdaType)
self.lambd = lambd
def __init__(self, lambd):
assert type(lambd) is types.LambdaType
self.lambd = lambd
def get_config(self):
if isinstance(self.mode, python_types.LambdaType):
mode = func_dump(self.mode)
mode_type = 'lambda'
elif callable(self.mode):
mode = self.mode.__name__
mode_type = 'function'
else:
mode = self.mode
mode_type = 'raw'
if isinstance(self._output_shape, python_types.LambdaType):
output_shape = func_dump(self._output_shape)
output_shape_type = 'lambda'
elif callable(self._output_shape):
output_shape = self._output_shape.__name__
output_shape_type = 'function'
else:
output_shape = self._output_shape
output_shape_type = 'raw'
if isinstance(self._output_mask, python_types.LambdaType):
output_mask = func_dump(self._output_mask)
output_mask_type = 'lambda'
elif callable(self._output_mask):
output_mask = self._output_mask.__name__
output_mask_type = 'function'
else:
output_mask = self._output_mask
output_mask_type = 'raw'
return {'name': self.name,
'mode': mode,
'mode_type': mode_type,
'concat_axis': self.concat_axis,
'dot_axes': self.dot_axes,
'output_shape': output_shape,
'output_shape_type': output_shape_type,
'output_mask': output_mask,
'output_mask_type': output_mask_type,
'arguments': self.arguments}