Pipe Character in Python

Pipe character in Python

It is a bitwise OR of integers. For example, if one or both of ax or bx are 1, this evaluates to 1, otherwise to 0. It also works on other integers, for example 15 | 128 = 143, i.e. 00001111 | 10000000 = 10001111 in binary.

What is pipe | and in python?

By default | stands for the logical or bit-wise OR operator, and >> for right shift, but fortunately you can overload operators in Python. So in order to have custom definition for | and >>, you just have to overload the two following dunder(magic) methods in your class __or__ and __rshift__:

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

I recommend you to read more about Python Data Model.

Now Looking on the Beam Python SDK, __or__ is overloaded in the PTransform class:

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

What should I call the symbol “|”?A pipe or bit operator

You have a pipe character (more correctly called the vertical bar character), which when used in an expression is called a binary bitwise operator:

The | operator yields the bitwise (inclusive) OR of its arguments, which must be integers.

The operator is hookable via the __or__ special method, which is what SQLAlchemy uses to build queries, as seen in your example code (in contrast to the boolean or operator, which can't be hooked into due to its short-circuiting behaviour).

why pipe symbol in python adds to integer?

It is not a pipe symbol, it is a bitwise OR.

2 in binary:    10
4 in binary: 100
__________________
with or: 110 (1 or 0: 1, 1 or 0: 1, 0 or 0: 0)

And 110 in binary is 6 decimal.

What does a pipe character in python slice notation do?

testA[0:3|4:6]

evaluates as

testA[0 : (3 | 4) : 6]

Which, in turn, evaluates via bitwise-or to

testA[0 : 7 : 6]

And this corresponds to the range 0 : 7 with a step size of 6. Hence only the first and last index are used.

What does the “|” sign mean in Python?

In Python, the '|' operator is defined by default on integer types and set types.

If the two operands are integers, then it will perform a bitwise or, which is a mathematical operation.

If the two operands are set types, the '|' operator will return the union of two sets.

a = set([1,2,3])
b = set([2,3,4])
c = a|b # = set([1,2,3,4])

Additionally, authors may define operator behavior for custom types, so if something.property is a user-defined object, you should check that class definition for an __or__() method, which will then define the behavior in your code sample.

So, it's impossible to give you a precise answer without knowing the data types for the two operands, but usually it will be a bitwise or.

Functional pipes in python like %% from R's magrittr

One possible way of doing this is by using a module called macropy. Macropy allows you to apply transformations to the code that you have written. Thus a | b can be transformed to b(a). This has a number of advantages and disadvantages.

In comparison to the solution mentioned by Sylvain Leroux, The main advantage is that you do not need to create infix objects for the functions you are interested in using -- just mark the areas of code that you intend to use the transformation. Secondly, since the transformation is applied at compile time, rather than runtime, the transformed code suffers no overhead during runtime -- all the work is done when the byte code is first produced from the source code.

The main disadvantages are that macropy requires a certain way to be activated for it to work (mentioned later). In contrast to a faster runtime, the parsing of the source code is more computationally complex and so the program will take longer to start. Finally, it adds a syntactic style that means programmers who are not familiar with macropy may find your code harder to understand.

Example Code:

run.py

import macropy.activate 
# Activates macropy, modules using macropy cannot be imported before this statement
# in the program.
import target
# import the module using macropy

target.py

from fpipe import macros, fpipe
from macropy.quick_lambda import macros, f
# The `from module import macros, ...` must be used for macropy to know which
# macros it should apply to your code.
# Here two macros have been imported `fpipe`, which does what you want
# and `f` which provides a quicker way to write lambdas.

from math import sqrt

# Using the fpipe macro in a single expression.
# The code between the square braces is interpreted as - str(sqrt(12))
print fpipe[12 | sqrt | str] # prints 3.46410161514

# using a decorator
# All code within the function is examined for `x | y` constructs.
x = 1 # global variable
@fpipe
def sum_range_then_square():
"expected value (1 + 2 + 3)**2 -> 36"
y = 4 # local variable
return range(x, y) | sum | f[_**2]
# `f[_**2]` is macropy syntax for -- `lambda x: x**2`, which would also work here

print sum_range_then_square() # prints 36

# using a with block.
# same as a decorator, but for limited blocks.
with fpipe:
print range(4) | sum # prints 6
print 'a b c' | f[_.split()] # prints ['a', 'b', 'c']

And finally the module that does the hard work. I've called it fpipe for functional pipe as its emulating shell syntax for passing output from one process to another.

fpipe.py

from macropy.core.macros import *
from macropy.core.quotes import macros, q, ast

macros = Macros()

@macros.decorator
@macros.block
@macros.expr
def fpipe(tree, **kw):

@Walker
def pipe_search(tree, stop, **kw):
"""Search code for bitwise or operators and transform `a | b` to `b(a)`."""
if isinstance(tree, BinOp) and isinstance(tree.op, BitOr):
operand = tree.left
function = tree.right
newtree = q[ast[function](ast[operand])]
return newtree

return pipe_search.recurse(tree)

Adding pipe symbol in Python command

One or both of the substituted variables probably ends with a newline. Use .strip() on them to remove it.



Related Topics



Leave a reply



Submit