By Paulo Scardine - http://goo.gl/Ke1P0p
No site StackOverflow um usuário de R perguntou como implementar o pipe-operator do pacote dplyr (%>%
), onde x %>% f(y)
é equivalente a f(x, y)
. Adicionalmente, ele gostaria de usar uma sintaxe parecida com o pacote Pipe do cheese shop:
df = df | select('one') | rename(one='new_one')
No pacote Pipe esta sintaxe é chamada de "infix notation", e é equivalente a:
df = rename(select(df, 'one'), one='new_one')
In [3]:
import pandas as pd
df = pd.DataFrame({'one' : [1., 2., 3., 4., 4.],
'two' : [4., 3., 2., 1., 3.]})
def select(df, *args):
return df[list(args)]
def rename(df, **kwargs):
for name, value in kwargs.items():
df = df.rename(columns={'%s' % name: '%s' % value})
return df
In [4]:
df
Out[4]:
In [5]:
select(df, 'one')
Out[5]:
In [6]:
rename(select(df, 'one'), one='other')
Out[6]:
In [7]:
class Idem(object):
def __add__(self, other):
return other * 2
idem = Idem()
In [8]:
idem + 5
Out[8]:
In [9]:
5 + idem
In [24]:
class Idem(object):
def __add__(self, other):
return other * 2
def __radd__(self, other):
return self.__add__(other)
idem = Idem()
In [25]:
5 + idem
Out[25]:
In [11]:
import datetime
data = datetime.date.today()
hora = datetime.time(19)
In [21]:
data
Out[21]:
In [13]:
hora
Out[13]:
In [14]:
datetime.datetime.now()
Out[14]:
In [15]:
data + hora
In [16]:
class SmartDate(datetime.date):
def __add__(self, other):
if isinstance(other, datetime.time):
return datetime.datetime.combine(self, other)
return super(SmartDate, self).__add__(other)
In [17]:
data = SmartDate(*data.timetuple()[:3])
In [18]:
data + hora
Out[18]:
In [19]:
def pipe(original):
class PipeInto(object):
data = {'function': original}
def __init__(self, *args, **kwargs):
self.data['args'] = args
self.data['kwargs'] = kwargs
def __rrshift__(self, other):
return self.data['function'](
other,
*self.data['args'],
**self.data['kwargs']
)
return PipeInto
@pipe
def select(df, *args):
return df[list(cols)]
@pipe
def rename(df, **kwargs):
for name, value in kwargs.items():
df = df.rename(columns={'%s' % name: '%s' % value})
return df
In [28]:
df >> select('two', 'one')
Out[28]:
In [29]:
df
Out[29]:
In [32]:
df >> select('one') >> rename(one='first')
Out[32]:
In [37]:
16 << 1
Out[37]:
In [ ]: