当前位置: 首页 > 知识库问答 >
问题:

Python中pipe和>>是什么?[副本]

易扬
2023-03-14
lines = p | 'read' >> ReadFromText(known_args.input)

  # Count the occurrences of each word.
  def count_ones(word_ones):
    (word, ones) = word_ones
    return (word, sum(ones))

  counts = (lines
            | 'split' >> (beam.ParDo(WordExtractingDoFn())
                          .with_output_types(unicode))
            | 'pair_with_one' >> beam.Map(lambda x: (x, 1))
            | 'group' >> beam.GroupByKey()
            | 'count' >> beam.Map(count_ones))

来自:https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/wordcount.py#l92

Python中>的语法和用法是什么?

共有1个答案

谯志诚
2023-03-14

默认情况下,代表逻辑或位或运算符,>代表右移,但幸运的是,您可以在Python中重载运算符。因此,为了对>>进行自定义只需在类__or____rshift__中重载以下两个dunder(magic)方法:

class A():
    def __or__(self):
        pass
    def __rshift__(self):
        pass

我建议您阅读更多关于Python数据模型的内容。

现在看一下Beam Python SDK,__or__PTransform类中重载:

  def __or__(self, right):
    """Used to compose PTransforms, e.g., ptransform1 | ptransform2."""
    if isinstance(right, PTransform):
      return _ChainedPTransform(self, right)
    return NotImplemented
 类似资料: