Пример #1
0
def _spec_to_type(
    key: str, value: Dict[str, Dict], bases: Tuple[Type, ...] = ()) -> Type:
    """Using the type specification, create the custom type objects

    Parameters
    ----------
    key : str
        The key name corresponding to the specification. It is used as a
        template for the custom type name.
    value : Dict
        The dictionary with the type specification.  It looks like:
          {
              "key1": {"type": <type1>, "validator": <validator1>},
              "key2": {"type": <type2>, "validator": <validator2>},
              # ...
          }

    bases : Tuple[Type, ...]
        Base classes

    Returns
    -------
    Type
        Custom type object with validators

    """
    type_k = _type_spec[0]
    dflt_k = _type_spec[6]
    fields = glom(
        # NOTE: original ordering is preserved, apart from moving the data
        # members w/ default arguments later.
        [(k, v) for k, v in value.items() if type_k in v and dflt_k not in v] +
        [(k, v) for k, v in value.items() if type_k in v and dflt_k in v],
        [(
            {
                "k": "0",
                "v": f"1.{type_k}",
                # TODO: non-trivial defaults like mutable types
                "d": Coalesce(f"1.{dflt_k}", default=SKIP),
            },
            T.values(),
            tuple,
        )],
    )  # extract key, value and convert to list of tuples
    ns = dict(
        chain(*glom(
            value.values(),
            [(Coalesce("validator", default_factory=dict), T.items())],
        )))  # chain dict.items() and create namespace
    return make_typedconfig(f"{key}_t", fields, namespace=ns, bases=bases)
Пример #2
0
def _spec_to_type(
    key: str, value: Dict[str, Dict], bases: Tuple[Type, ...] = ()) -> Type:
    """Using the type specification, create the custom type objects

    Parameters
    ----------
    key : str
        The key name corresponding to the specification. It is used as a
        template for the custom type name.
    value : Dict
        The dictionary with the type specification.  It looks like:
          {
              "key1": {"type": <type1>, "validator": <validator1>},
              "key2": {"type": <type2>, "validator": <validator2>},
              # ...
          }

    bases : Tuple[Type]
        Base classes

    Returns
    -------
    Type
        Custom type object with validators

    """
    fields = glom(
        value.items(),
        [(
            {
                "k": "0",
                "v": "1.type",
                # TODO: non-trivial defaults like mutable types
                "d": Coalesce("1.default", default=SKIP),
            },
            T.values(),
            tuple,
        )],
    )  # extract key, value and convert to list of tuples

    ns = dict(
        chain(*glom(
            value.values(),
            [(Coalesce("validator", default_factory=dict), T.items())],
        )))  # chain dict.items() and create namespace

    return make_dataconfig(f"{key}_t", fields, namespace=ns, bases=bases)