将函数链接为 python 中的 shell 管道命令
在 Unix/linux shell 中我们可以:
seq 0 100 | head -10 | awk 'NF%2==0' | awk 'NF%2==1' | rev
现在我定义了:
seqsrc = list(range(0,100))
def all(src): return src
def head(src, count, offset = 0): return src[:count]
def tail(src, count, offset = 0): return src[-count:]
def odd(src): return [x for x in src if x % 2 != 0]
def even(src): return [x for x in src if x % 2 == 0]
def reverse(src): return src[::1]
...
#def other_sequence_manpulation_method()
这是我的问题:
1. 如何在 python 中获得类似于 shell 管道的语法?
seqdst = all(seqsrc).head(10).odd().even().reverse()
2. 出于某种原因,我想枚举我定义的那些简单函数的所有可能组合,我可以使用 itertools.product() 来生成组合 - 编辑:以及下面的 Seq 类解决方案吗?
possible_head_limit = [10,20,30]
all(seqsrc).head(10) # 10 is one item in possible_head_limit
all(seqsrc).head(10).odd()
all(seqsrc).head(10).odd().even()
all(seqsrc).head(10).odd().even().reverse()
all(seqsrc).head(10).even()
all(seqsrc).head(10).even().odd()
....
all(seqsrc).head(20) # 20 is one item in possible_head_limit
all(seqsrc).head(20).odd()
...
3: 假设 seqsrc = range(0,10)
那么 head(20)
可能会返回与 head(10)
相同的结果,或者有时它毫无意义,
all(seqsrc).head(20).odd().even().reverse()
# = all(seqsrc).head(10).odd().even().reverse()
# = all(seqsrc).head(11).odd().even().reverse()
# ...
我可以吗在方法链中添加控制函数,那么我可以控制返回的意思吗?
ignore_insufficient(True).all(seqsrc).head(20).odd().even().reverse()
ignore_insufficient(False).all(seqsrc).head(20).odd().even().reverse() # it will print some sort of error
# or even I can control each function I defined?
ignore_insufficient(True).all(seqsrc).\
ignore_insufficient(True).head(20).\
ignore_insufficient(False).tail(10)
谢谢!
In Unix/linux shell we can:
seq 0 100 | head -10 | awk 'NF%2==0' | awk 'NF%2==1' | rev
Now I defined:
seqsrc = list(range(0,100))
def all(src): return src
def head(src, count, offset = 0): return src[:count]
def tail(src, count, offset = 0): return src[-count:]
def odd(src): return [x for x in src if x % 2 != 0]
def even(src): return [x for x in src if x % 2 == 0]
def reverse(src): return src[::1]
...
#def other_sequence_manpulation_method()
Here are my questions:
1.
How can I get shell pipe like grammar in python?
seqdst = all(seqsrc).head(10).odd().even().reverse()
2.
For some reason I want to enumerate all possible combinations of those simple functions I defined, can I do it with itertools.product() to generate combinations - EDIT: as well as for the Seq class solution below?
possible_head_limit = [10,20,30]
all(seqsrc).head(10) # 10 is one item in possible_head_limit
all(seqsrc).head(10).odd()
all(seqsrc).head(10).odd().even()
all(seqsrc).head(10).odd().even().reverse()
all(seqsrc).head(10).even()
all(seqsrc).head(10).even().odd()
....
all(seqsrc).head(20) # 20 is one item in possible_head_limit
all(seqsrc).head(20).odd()
...
3:
Suppose seqsrc = range(0,10)
then head(20)
may return same as head(10)
or sometime it is meanless
all(seqsrc).head(20).odd().even().reverse()
# = all(seqsrc).head(10).odd().even().reverse()
# = all(seqsrc).head(11).odd().even().reverse()
# ...
Can I add control function in the method chain, then I can control the mean of the return?
ignore_insufficient(True).all(seqsrc).head(20).odd().even().reverse()
ignore_insufficient(False).all(seqsrc).head(20).odd().even().reverse() # it will print some sort of error
# or even I can control each function I defined?
ignore_insufficient(True).all(seqsrc).\
ignore_insufficient(True).head(20).\
ignore_insufficient(False).tail(10)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的帖子中有很多问题,我不确定是否全部理解。然而,这是一个起点。
可链接方法通常是通过设计具有返回类本身的新实例的方法的类来实现的。这允许从先前方法的返回值调用更多方法。
因此,您可以按如下方式定义
Seq
类:并按如下方式使用它:
请注意,这可以通过多种方式进行改进。例如,在 Javascript 世界中, jQuery 的可链接方法实际上将其结果推送到堆栈中,从而允许 < em>回溯调用历史记录并恢复以前的上下文。有关详细信息,请参阅 end()。
There are many questions in your post and I'm not certain to understand them all. However, here is a starting point.
Chainable methods are usually implemented by designing classes with methods that return new instances of the class itself. This allows to call further methods from the return value of previous methods.
Therefore, you can define a
Seq
class as follows:And use it like:
Note that this can be improved in many ways. For instance, in the Javascript world, jQuery's chainable methods actually push their results into a stack, thus allowing backtracking in the history of calls and restoring a previous context. See end() for details.