I mainly program in Java and I found that for data analysis python is more convenient. I am looking for a way to pipe operations in a way that is equivalent to java streams. For example, I would like to do something like (I'm mixing java and python syntax).
(key, value) = Files.lines(Paths.get(path))
.map(line -> new Angle(line))
.filter(angle -> foo(angle))
.map(angle -> (angle, cosine(angle)))
.max(Comparator.comparing(Pair::getValue)
Here I take a list of lines from a file, convert each line into an Angle object, filter the angles by some parameter, then create a list of pairs and finally find the maximal pair. There may be multiple additional operations in addition, but the point is that this is one pipe passing the output of one operation into the next.
I know about python list comprehensions, however they seem to be limited to a single "map" and a single "filter". If I need to pipe several maps using comprehension, the expression soon becomes complicated (I need to put one comprehension inside another comprehension)
Is there a syntax construct in python that allows adding multiple operations in one command?
It is not difficult to achieve it by yourself, for example:
class BasePipe:
def __init__(self, data):
self.data = data
def filter(self, f):
self.data = [d for d in self.data if f(d)]
return self
def map(self, f):
self.data = [*map(f, self.data)]
return self
def __iter__(self):
yield from self.data
def __str__(self):
return str(self.data)
def max(self):
return max(self.data)
def min(self):
return min(self.data)
value = (
BasePipe([1, 2, 3, 4]).
map(lambda x: x * 2).
filter(lambda x: x > 4).
max()
)
And Gives:
8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With