Re: [Tutor] Python Script Testing

2018-03-15 Thread Alan Gauld via Tutor
Please always use Reply All or Reply List when responding to list emails.

Farwarding to list...

On 15/03/18 17:47, Gaurav Parashar wrote:
> Opening the file inside the script seems the only possible solution.
> As far as I remember about the coursera course, the gave the input
> file, and we need to upload the output file, running the script on our
> local system. So, the above method should work.
>
> On 15 Mar 2018 11:13 pm, "Alan Gauld via Tutor"  > wrote:
>
> On 15/03/18 16:56, Preeti Saxena wrote:
> > Hi,
> >    I am new to python. I am trying to test my program, like a
> coursera
> > submission, which takes run time arguments using "raw_input()".
> Is there a
> > way I can write all the input data into a file and pass it from
> command
> > line to the script?
>
> In normal Python yes, you can simply pass the file name as an
> argument to the program or you can use input redirection to
> read the data from the file as if it were input by a user.
>
> Unfortunately neither of these options is likely to work in
> a web environment - one of the disadvantages of using such
> a tool. The issue is not a Python one but one with your
> tutorial. I suggest you contact the authors directly to
> ask if they can suggest a solution (or incorporate
> such a feature).
>
> > 1. I have to use "raw_input".
> > 2. I do not wish to open a file inside the script as that will
> beat the
> > purpose.
>
> In real-world python you could just use input redirection:
>
> ## data.txt ##
> Alan
> ##
>
> ## myscript.py ###
> name = raw_input("What's your name? ")
> print "Hello ", name
> ##
>
> Executed with:
>
> $ python myscript.py < data.txt
>
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> 
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
> 
>
>
> ___
> Tutor maillist  -  Tutor@python.org 
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 
>

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Script Testing

2018-03-15 Thread Alex Kleider

On 2018-03-15 09:56, Preeti Saxena wrote:

Hi,
   I am new to python. I am trying to test my program, like a coursera
submission, which takes run time arguments using "raw_input()". Is 
there a

way I can write all the input data into a file and pass it from command
line to the script?
1. I have to use "raw_input".
2. I do not wish to open a file inside the script as that will beat the
purpose.


If you are using Linux or other UNIX like system (such as a Mac) the 
'HERE Document' feature [1] of the OS might prove useful:


# File: dummy.py
inputs = {}
inputs["first"] = raw_input("")
inputs["second"] = raw_input("")
for key in inputs.keys():
print inputs[key]

Then from the command line:
alex@x301n4:~$ python dummy.py << HERE
first input
second input
HERE

On my system[2] the result is:
second input
first input

[1] http://tldp.org/LDP/abs/html/here-docs.html
[2] Ubuntu 16.4LTS
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Script Testing

2018-03-15 Thread Alan Gauld via Tutor
On 15/03/18 16:56, Preeti Saxena wrote:
> Hi,
>I am new to python. I am trying to test my program, like a coursera
> submission, which takes run time arguments using "raw_input()". Is there a
> way I can write all the input data into a file and pass it from command
> line to the script?

In normal Python yes, you can simply pass the file name as an
argument to the program or you can use input redirection to
read the data from the file as if it were input by a user.

Unfortunately neither of these options is likely to work in
a web environment - one of the disadvantages of using such
a tool. The issue is not a Python one but one with your
tutorial. I suggest you contact the authors directly to
ask if they can suggest a solution (or incorporate
such a feature).

> 1. I have to use "raw_input".
> 2. I do not wish to open a file inside the script as that will beat the
> purpose.

In real-world python you could just use input redirection:

## data.txt ##
Alan
##

## myscript.py ###
name = raw_input("What's your name? ")
print "Hello ", name
##

Executed with:

$ python myscript.py < data.txt


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Script Testing

2018-03-15 Thread Preeti Saxena
Hi,
   I am new to python. I am trying to test my program, like a coursera
submission, which takes run time arguments using "raw_input()". Is there a
way I can write all the input data into a file and pass it from command
line to the script?
1. I have to use "raw_input".
2. I do not wish to open a file inside the script as that will beat the
purpose.

Thanks for any and all the help.

Thanks,
Preeti
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor