Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5998649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:26:10+00:00 2026-05-23T00:26:10+00:00

In Unix/linux shell we can: seq 0 100 | head -10 | awk ‘NF%2==0’

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:26:11+00:00Added an answer on May 23, 2026 at 12:26 am

    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:

    class Seq(object):
        def __init__(self, seq):
            self.seq = seq
        def __repr__(self):
            return repr(self.seq)
        def __str__(self):
            return str(self.seq)
        def all(self):
            return Seq(self.seq[:])
        def head(self, count):
            return Seq(self.seq[:count])
        def tail(self, count):
            return Seq(self.seq[-count:])
        def odd(self):
            return Seq(self.seq[1::2])
        def even(self):
            return Seq(self.seq[::2])
        def reverse(self):
            return Seq(self.seq[::-1])
    

    And use it like:

    >>> s = Seq(range(0, 100))
    >>> print s.head(10).odd().even().reverse()
    [9, 5, 1]
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to stop losing precious time when dealing with Linux/Unix's shell. If I
On Linux you can convert a date like 2010-10-02 to a unix timestamp in
Linux utilities like sed, awk and other shell scripting features are awesome and life
In Unix/Linux, how do you find out what group a given user is in
How do Window's programmers profile their native C++ code? On Unix/Linux you have gprof
Does Linux/Unix/Posix provide an API to user-space applications to access a monotonically increasing clock,
Python for Unix and Linux System Administration is aimed at sysadmins. Any other favorites
Is there a rough equivalent to the Linux/Unix stdio.h popen() function in the Win32
We are an all Unix shop (Solaris, Linux). This last product cycle I returned
On Unix, I can either use \r (carriage return) or \b (backspace) to overwrite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.