本文整理汇总了Python中schema.Schema.pop方法的典型用法代码示例。如果您正苦于以下问题:Python Schema.pop方法的具体用法?Python Schema.pop怎么用?Python Schema.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类schema.Schema
的用法示例。
在下文中一共展示了Schema.pop方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_dict
# 需要导入模块: from schema import Schema [as 别名]
# 或者: from schema.Schema import pop [as 别名]
def from_dict(cls, spec):
"""Construct a |Design| instance from a specification based on dictionaries (e.g., parsed from a YAML file).
Parameters
----------
spec : dict
A dictionary containing some of the following keys (all optional):
``'name'``, the name of the level;
``'ivs'``, ``'design_matrix'``, ``'extra_data'``, keyword arguments to the |Design| constructor;
``'order'`` or ``'ordering'``, a string, dictionary, or list determining the ordering method; and
``'n'`` or ``'number'``, the ``number`` argument to the specified ordering.
A dictionary containing any fields not otherwise used
is passed to the |Design| constructor as the ``extra_data`` argument.
See the |description in the docs| for more information.
Returns
-------
name : str
Only returned if `spec` contains a field ``'name'``.
design : |Design|
See Also
--------
experimentator.DesignTree.from_spec
Examples
--------
>>> design_spec = {
...'name': 'block',
...'ivs': {'speed': [1, 2, 3], 'size': [15, 30]},
...'ordering': 'Shuffle',
...'n': 3}
>>> Design.from_dict(design_spec)
Level(name='block', design=Design(ivs=[('speed', [1, 2, 3]), ('size', [15, 30])], design_matrix=None, ordering=Shuffle(number=3, avoid_repeats=False), extra_data={}))
"""
inputs = Schema({
Optional('name'): And(str, len),
Optional('ivs'): And(Use(dict), {Optional(And(str, len)): Iterable}),
Optional('design_matrix'): Use(np.asarray),
Optional(Or('order', 'ordering')): Use(order.OrderSchema.from_any),
Optional(Or('n', 'number')): int,
Optional(
lambda x: x not in {'name', 'ivs', 'design_matrix', 'order', 'ordering', 'n', 'number'}
# Necessary due to https://github.com/keleshev/schema/issues/57
): object,
}).validate(spec)
if 'n' in inputs:
inputs['number'] = inputs.pop('n')
if 'order' in inputs:
inputs['ordering'] = inputs.pop('order')
if 'ordering' not in inputs:
inputs['ordering'] = order.Ordering() if 'design_matrix' in inputs else order.Shuffle()
if 'number' in inputs:
inputs['ordering'].number = inputs.pop('number')
name = inputs.pop('name', None)
extra_keys = set(inputs) - {'ivs', 'design_matrix', 'ordering'}
if extra_keys:
inputs['extra_data'] = {key: inputs.pop(key) for key in extra_keys}
self = cls(**inputs)
return Level(name, self) if name else self