Last active
January 31, 2019 21:30
-
-
Save jdbrice/4765a6f63cc0b14965a450a10b0af4fa to your computer and use it in GitHub Desktop.
Makes numpy arange ranges from the shell, even chain together with commas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy | |
import sys | |
if len(sys.argv) < 4: | |
print( "Usage: arange start stop step [, ndecimals]" ) | |
exit() | |
if not sys.stdin.isatty(): | |
for line in sys.stdin: | |
sys.stdout.write(line.strip()) | |
start = float(sys.argv[1]) | |
stop = float(sys.argv[2]) | |
step = float(sys.argv[3]) | |
ndigits = 2; | |
if len( sys.argv ) > 4: | |
ndigits = sys.argv[4] | |
vals = "" | |
fmt = "%0." + str(ndigits) + "f" | |
for v in numpy.arange( start, stop, step ): | |
vals = vals + fmt % v + ", " | |
if not sys.stdin.isatty() : | |
print(", " + vals[:-2]) | |
else : | |
print(vals[:-2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Usage Example: | |
$> arange 0 3 0.1 1 | arange 3 4 0.1 1 | |
0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment