This is a really great point! Not checking to make sure the data is in the right format is dangerous in general and could leave the program (and in turn, your computer) open to cyber vulnerabilities. In real world programming we should always validate our inputs, ESPECIALLY if the data comes from the user (or anywhere we don't directly control).
great tutorial! is there a way to stop reading from stdin if no data is piped to the script? edit: after some google-fu, I was actually able to figure out some code that elegantly handles input both from pipes and normal passed arguments - see below. # Exclude the first argument in passed_arguments, # because it's always just the script's name. passed_arguments = sys.argv[1:] # Checks if anything has been piped in by seeing if stdin is interactive (ie, # expecting input from keyboard). If *not*, process it and add it to the list of # arguments. If it *is*, ignore it. if sys.stdin.isatty() == False: piped_input = sys.stdin.readline().strip().split(" ") arguments = passed_arguments + piped_input else: arguments = passed_arguments this was tested on Linux. it almost certainly also works on Mac OS, and will probably work on Windows too.
Good question! I think that the for loop will not execute if there are no lines available in stdin when you pipe to it. There are also ways to detect and skip if the program is called without piping anything in. Check this answer out for more info on that: stackoverflow.com/questions/3762881/how-do-i-check-if-stdin-has-some-data
Great. A simple overview of data pipeline.
Well explained!
Couldn't that possibly crash with data overload if you use that short cut than you would just have to add a check for that no?
This is a really great point! Not checking to make sure the data is in the right format is dangerous in general and could leave the program (and in turn, your computer) open to cyber vulnerabilities. In real world programming we should always validate our inputs, ESPECIALLY if the data comes from the user (or anywhere we don't directly control).
nice
great tutorial! is there a way to stop reading from stdin if no data is piped to the script?
edit: after some google-fu, I was actually able to figure out some code that elegantly handles input both from pipes and normal passed arguments - see below.
# Exclude the first argument in passed_arguments,
# because it's always just the script's name.
passed_arguments = sys.argv[1:]
# Checks if anything has been piped in by seeing if stdin is interactive (ie,
# expecting input from keyboard). If *not*, process it and add it to the list of
# arguments. If it *is*, ignore it.
if sys.stdin.isatty() == False:
piped_input = sys.stdin.readline().strip().split(" ")
arguments = passed_arguments + piped_input
else:
arguments = passed_arguments
this was tested on Linux. it almost certainly also works on Mac OS, and will probably work on Windows too.
Good question! I think that the for loop will not execute if there are no lines available in stdin when you pipe to it. There are also ways to detect and skip if the program is called without piping anything in. Check this answer out for more info on that: stackoverflow.com/questions/3762881/how-do-i-check-if-stdin-has-some-data
Awesome!! Great solution!