Beispiel #1
0
    def test_is_namedtuple(self):
        class AType:
            pass

        a_type = AType
        a_tuple = type((1, 2, 3))

        a_namedtuple = ANamedTuple

        self.assertTrue(is_namedtuple(a_namedtuple))
        self.assertFalse(is_namedtuple(a_type))
        self.assertFalse(is_namedtuple(a_tuple))
Beispiel #2
0
    def beam_sql(self, line: str, cell: str) -> Union[None, PValue]:
        """The beam_sql cell magic that executes a Beam SQL.

    Args:
      line: (optional) the string on the same line after the beam_sql magic.
          Used as the output variable name in the __main__ module.
      cell: everything else in the same notebook cell as a string. Used as a
          Beam SQL query.

    Returns None if running into an error, otherwise a PValue as if a
    SqlTransform is applied.
    """
        if line and not line.strip().isidentifier() or keyword.iskeyword(
                line.strip()):
            on_error(
                'The output_name "%s" is not a valid identifier. Please supply a '
                'valid identifier that is not a Python keyword.', line)
            return
        if not cell or cell.isspace():
            on_error('Please supply the sql to be executed.')
            return
        found = find_pcolls(cell, pcoll_by_name())
        for _, pcoll in found.items():
            if not is_namedtuple(pcoll.element_type):
                on_error(
                    'PCollection %s of type %s is not a NamedTuple. See '
                    'https://beam.apache.org/documentation/programming-guide/#schemas '
                    'for more details.', pcoll, pcoll.element_type)
                return
            register_coder_for_schema(pcoll.element_type)

        output_name, output = apply_sql(cell, line, found)
        cache_output(output_name, output)
        return output
Beispiel #3
0
    def beam_sql(self,
                 line: str,
                 cell: Optional[str] = None) -> Optional[PValue]:
        """The beam_sql line/cell magic that executes a Beam SQL.

    Args:
      line: the string on the same line after the beam_sql magic.
      cell: everything else in the same notebook cell as a string. If None,
        beam_sql is used as line magic. Otherwise, cell magic.

    Returns None if running into an error, otherwise a PValue as if a
    SqlTransform is applied.
    """
        input_str = line
        if cell:
            input_str += ' ' + cell
        parsed = self._parser.parse(input_str.strip().split())
        if not parsed:
            # Failed to parse inputs, let the parser handle the exit.
            return
        output_name = parsed.output_name
        verbose = parsed.verbose
        query = parsed.query

        if output_name and not output_name.isidentifier() or keyword.iskeyword(
                output_name):
            on_error(
                'The output_name "%s" is not a valid identifier. Please supply a '
                'valid identifier that is not a Python keyword.', line)
            return
        if not query:
            on_error('Please supply the SQL query to be executed.')
            return
        query = ' '.join(query)

        found = find_pcolls(query, pcoll_by_name(), verbose=verbose)
        for _, pcoll in found.items():
            if not is_namedtuple(pcoll.element_type):
                on_error(
                    'PCollection %s of type %s is not a NamedTuple. See '
                    'https://beam.apache.org/documentation/programming-guide/#schemas '
                    'for more details.', pcoll, pcoll.element_type)
                return
            register_coder_for_schema(pcoll.element_type, verbose=verbose)

        output_name, output = apply_sql(query, output_name, found)
        cache_output(output_name, output)
        return output