示例#1
0
文件: meta.py 项目: vx-qa/pyrchain
def _make_from_pb_fn(fields: List[Field]):
    globals = {'MISSING': MISSING,
               '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY}
    locals = {f'_type_{f.name}': f.type for f in fields}
    globals.update({f'_pb_type_{f.name}': f.default_factory for f in fields if f.default_factory != MISSING})
    body_lines = []
    for field in fields:
        field_type_name = getattr(field.type, "_name", None)
        if field_type_name == "List":
            if field.default_factory != MISSING:
                body_lines.append(
                    "{}=[_pb_type_{}.from_pb(_pb) for _pb in {}.{}],".format(field.name, field.name, _PB_PARAM,
                                                                             field.name))
            else:
                body_lines.append("{}=[i for i in {}.{}],".format(field.name, _PB_PARAM, field.name))
        elif getattr(field.type, _FROM_PB, None):
            if field.default_factory == MISSING:
                raise ValueError("from pb class must provide a default factory")
            else:
                body_lines.append(
                    "{}=_pb_type_{}.from_pb({}.{}),".format(field.name, field.name, _PB_PARAM, field.name))
        else:
            body_lines.append("{}={}.{},".format(field.name, _PB_PARAM, field.name))
    body_lines.insert(0, "return cls(")
    body_lines.append(")")
    return _create_fn("from_pb", ["cls", _PB_PARAM], body_lines, globals=globals,
                      locals=locals)
示例#2
0
    def dataclasses_init_fn_with_kwargs(fields, frozen, has_post_init,
                                        self_name, globals):
        # fields contains both real fields and InitVar pseudo-fields.

        # Make sure we don't have fields without defaults following fields
        # with defaults.  This actually would be caught when exec-ing the
        # function source code, but catching it here gives a better error
        # message, and future-proofs us in case we build up the function
        # using ast.
        seen_default = False
        for f in fields:
            # Only consider fields in the __init__ call.
            if f.init:
                if not (f.default is MISSING and f.default_factory is MISSING):
                    seen_default = True
                elif seen_default:
                    raise TypeError(f'non-default argument {f.name!r} '
                                    'follows default argument')

        locals = {f'_type_{f.name}': f.type for f in fields}
        locals.update({
            'MISSING': MISSING,
            '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY,
        })

        body_lines = []
        for f in fields:
            line = _field_init(f, frozen, locals, self_name)
            # line is None means that this field doesn't require
            # initialization (it's a pseudo-field).  Just skip it.
            if line:
                body_lines.append(line)

        # Does this class have a post-init function?
        if has_post_init:
            params_str = ','.join(f.name for f in fields
                                  if f._field_type is _FIELD_INITVAR)
            body_lines.append(
                f'{self_name}.{_POST_INIT_NAME}({params_str}{", " if params_str else ""} **kwargs)'
            )

        # If no body lines, use 'pass'.
        if not body_lines:
            body_lines = ['pass']

        return _create_fn('__init__', [self_name] +
                          [_init_param(f)
                           for f in fields if f.init] + ["**kwargs"],
                          body_lines,
                          locals=locals,
                          globals=globals,
                          return_type=None)
示例#3
0
def __add_serialize(cls: type, model: bool, enforced_serializable: list,
                    skip_fields: list) -> object:
    """
    Adds __serialize__ method.
    Args:
        cls: class to enchant

    Returns (object): __serialize__ method body

    """
    fields_to_serialize = list(
        filter(lambda el: el.name not in skip_fields, fields(cls)))
    sonosco_self = '__sonosco_self__' if 'self' in fields_to_serialize else 'self'
    serialize_body = __create_serialize_body(fields_to_serialize, model,
                                             enforced_serializable)
    return _create_fn('__serialize__', [sonosco_self],
                      serialize_body,
                      return_type=dict)
示例#4
0
文件: nodes.py 项目: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(
         0,
         dataclasses._create_fn(self.input(0), self.input(1),
                                self.input(2)))