This code uses a decorator to turn a function into an instance of a special 
Pipe class. The Pipe class uses the Python "magic" method __ror__ to 
overload the bitwise OR operator (i.e, the | operator). The __or__ magic 
method determines the behavior of the | operator when the object with the 
method appears on the left of the | operator, and the __ror__ (i.e., 
"reverse" __or__) magic method determines the behavior when the object with 
the method appears on the right of the | operator. In this case, the 
decorated function is used on the right of the | operator, so the __ror__ 
method is used. For more details, see 
http://minhhh.github.io/posts/a-guide-to-pythons-magic-methods#user-content-numeric-magic-methods.

The Pipe class also includes a __call__ method, so you can call the object 
with some arguments, and it returns a new Pipe object, which passes those 
arguments to the originally decorated function.

For example, the module defines a concat function:

@Pipe 
def concat(iterable, separator=", "): 
    return separator.join(map(str,iterable))

With the above decorator, concat becomes an instance of the Pipe class, 
with self.function set to the function defined above. When you call:

[1, 2, 3] | concat

The __ror__ method of the Pipe object gets called, with the list on the 
left passed as the "other" argument.

When you call:

[1, 2, 3] | concat('#')

First, the __call__ method of the Pipe object gets called with "#" as its 
argument. It then returns a new Pipe object, and the __ror__ method of that 
new Pipe object gets called with the list as the "other" argument.

So, here we've got an example of (a) a decorator implemented as a class 
with an __init__ and __call__ method, (b) a Python magic method, and (c) 
operator overloading (implemented via the magic method).

Anthony

On Thursday, October 5, 2017 at 4:48:43 PM UTC-4, Ramos wrote:
>
> Reading this 
> https://github.com/JulienPalard/Pipe
>
>  i tried this simple code
>
>
> import functools
>
>
> class Pipe:
>
>     def __init__(self, function):
>         self.function = function
>         functools.update_wrapper(self, function)
>
>     def __ror__(self, other):
>         return self.function(other)
>
>     def __call__(self, *args, **kwargs):
>         return Pipe(lambda x: self.function(x, *args, **kwargs))
> @Pipe
> def list(x):
>     return [x]
> @Pipe
> def count(iterable):
>     "Count the size of the given iterable, walking thrue it."
>     count = 0
>     for x in iterable:
>         count += 1
>     return count
>
>
>
> Can someone tell me why/how this code gives me the | available in the 
> console to pipe functions 
>
> >> [2,3,4,2,4] | count |list
> [5]
>
>
>
>
>
> Regards
> António
>
>
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>  Sem 
> vírus. www.avast.com 
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>  
> <#CAEM0BxNhzsCkpy=uUdH7xXr6=g3JxtrGvo5M48XZjq55EK=zrw@mail.gmail.com_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to