Due to a chain (or sequence - pun intended :) of thoughts, I got the idea of writing a simple version of the Unix seq utility (command-line) in Python. (Some Unix versions have a similar command called jot.)
Note: I wrote this program just for fun. As the seq Wikipedia page says, modern versions of bash can do the work of seq. But this program may still be useful on Windows - not sure if the CMD shell has seq-like functionality or not. PowerShell probably has it, is my guess.)
The seq command lets you specify one or two or three numbers as command-line arguments (some of which are optional): the start, stop and step values, and it outputs all numbers in that range and with that step between them (default step is 1). I have not tried to exactly emulate seq, instead I've written my own version. One difference is that mine does not support the step argument (so it can only be 1), at least in this version. That can be added later. Another is that I print the numbers with spaces in between them, not newlines. Another is that I don't support floating-point numbers in this version (again, can be added).
The seq command has more uses than the above description might suggest (in fact, it is mainly used for other things than just printing a sequence of numbers - after all, who would have a need to do that much). Here is one example, on Unix (from the Wikipedia article about seq):
# Remove file1 through file17: for n in `seq 17` do rm file$n doneNote that those are backquotes or grave accents around seq 17 in the above code snippet. It uses sh / bash syntax, so requires one of them, or a compatible shell.
Here is the code for seq1.py:
''' seq1.py Purpose: To act somewhat like the Unix seq command. Author: Vasudev Ram Copyright 2017 Vasudev Ram Web site: https://vasudevram.github.io Blog: https://jugad2.blogspot.com Product store: https://gumroad.com/vasudevram ''' import sys def main(): sa, lsa = sys.argv, len(sys.argv) if lsa < 2: sys.exit(1) try: start = 1 if lsa == 2: end = int(sa) elif lsa == 3: start = int(sa) end = int(sa) else: # lsa > 3 sys.exit(1) except ValueError as ve: sys.exit(1) for num in xrange(start, end + 1): print num, sys.exit(0) if __name__ == '__main__': main()And here are a few runs of seq1.py, and the output of each run, below:
$ py -2 seq1.py $ py -2 seq1.py 1 1 $ py -2 seq1.py 2 1 2 $ py -2 seq1.py 3 1 2 3 $ py -2 seq1.py 1 1 1 $ py -2 seq1.py 1 2 1 2 $ py -2 seq1.py 1 3 1 2 3 $ py -2 seq1.py 4 1 2 3 4 $ py -2 seq1.py 1 4 1 2 3 4 $ py -2 seq1.py 2 2 2 $ py -2 seq1.py 5 3 $ py -2 seq1.py -6 -2 -6 -5 -4 -3 -2 $ py -2 seq1.py -4 -0 -4 -3 -2 -1 0 $ py -2 seq1.py -5 5 -5 -4 -3 -2 -1 0 1 2 3 4 5
There are many other possible uses for seq, if one uses one's imagination, such as rapidly generating various filenames or directory names, with numbers in them (as a prefix, suffix or in the middle), for testing or other purposes, etc.
- Vasudev Ram - Online Python training and consulting Get updates (via Gumroad) on my forthcoming apps and content. Jump to posts: Python * DLang * xtopdf Subscribe to my blog by email My ActiveState Code recipesFollow me on: LinkedIn * Twitter Managed WordPress Hosting by FlyWheel