[Tutor] Python

2018-09-06 Thread Donna Black via Tutor
How do we save our work on python? IDLE hasn’t installed itself on my computer 

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


Re: [Tutor] Python

2018-09-06 Thread Alan Gauld via Tutor
On 06/09/18 04:14, Donna Black via Tutor wrote:
> How do we save our work on python? IDLE hasn’t installed itself on my 
> computer 

Which OS are you using? That will help us
find/install IDLE.

However, you don't need IDLE to work with Python,
just a text editor.

If I assume you are using Windows then start notepad
and type the 2 lines

print("Hello world")
input("hit return to exit...")

Then save the file as hello.py

You should now be able to run that file by
double clicking it in your file manager.

Alternatively, start a CMD session
(Windows+R type cmd, hit OK) and at the
prompt type

python \the\folder\where\you\saved\the\file\hello.py

If you have problems with any of that let us know.
copy any error messages you get into your
mail message.

-- 
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] Writing for loop output to csv

2018-09-06 Thread Brandon Creech
Hi, I am working to forecast the temperatures for the next 5 days using an
API and a for loop. I would like write the output of this loop to a csv in
this format::

Columns: City, min1, max1, min2, max2,min3,max3,min4,max4,min5,max5
data: Athens,Greece 25.4,26.7etc.
 Nantou,Taiwan 16.18, ..etc

the data prints out on top of each other like this:

Nantou, Taiwan 29.49 20.79
Nantou, Taiwan 30. 49 21.56

Code I have:

from forecastiopy import *
import csv

api_key = 'XXX'

cities = {"Taree, Australia":(-31.8895,152.), "Belmopan,
Belize":(17.251,-88.759), "Nanaimo, Canada":(49.1659,-123.9401),
   "Sherbrooke,Canada":(45.4042,-71.8929),
"Shenyang,China":(41.8057,123.4315),"Crewe,England (UK)":(53.1004,-2.4438),
   "Cairo, Egypt":(30.0444,31.2357),
"Kaiserslautern,Germany":(49.4401,7.7491),
"Athens,Greece":(37.9838,23.7275),
   "Bengaluru,India":(12.9716,77.5946),
"Incheon,Korea":(37.4563,126.7052), "Kathmandu, Nepal":(27.7172,85.3240),

 
"Lahore,Pakistan":(31.5204,74.3587),"Lima,Peru":(-12.0464,-77.0428),"Belgrade,Serbia":(44.7866,20.4489),
   "Nantou,Taiwan":(23.961,120.9719),"Des Moines,United
States":(41.5868,-93.625)}



for city, coords in cities.items():
weather = ForecastIO.ForecastIO( api_key, latitude=coords[0],
longitude=coords[1] )
daily = FIODaily.FIODaily(weather)
for day in range(2,7):
print(str(city) + " " + str(daily.get_day(day)['temperatureMax']) +
" " + str(daily.get_day(day)['temperatureMin']))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] localhosting

2018-09-06 Thread Roger Lea Scherer
Thank you all for your help. I am still chugging away at this problem. I've
switched to Windows PowerShell Version 5.1 since I can select, copy, and
paste more easily in PowerShell. (I can't figure out how to do that in
Anaconda and I get the same errors in PowerShell.)

I removed the "3" in "python3" and it works; along with moving the "www"
folder to where python resides. Thanks for the help.

So it appears the local host is running correctly, but when I run this code:

print("Hello World")

Chrome does not render and I get an error message in PowerShell:
127.0.0.1 - - [06/Sep/2018 11:22:46] "GET /cgi-bin/hello.py HTTP/1.1" 200 -
127.0.0.1 - - [06/Sep/2018 11:22:46] command:
C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
C:\Users\Roger\documents\roger\python\www\cgi-bin\hello.py ""
127.0.0.1 - - [06/Sep/2018 11:22:46] b'  File
"C:\\Users\\Roger\\documents\\roger\\python\\www\\cgi-bin\\hello.py", line
1\r\nprint ""\r\n ^\r\nSyntaxError: Missing
parentheses in call to \'print\'. Did you mean print("")?\r\n'
127.0.0.1 - - [06/Sep/2018 11:22:46] CGI script exit status 0x1

BUT when I run this code:
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)

print(fib(15))

Chrome does not render but I DON'T get an error message, I get this:
127.0.0.1 - - [06/Sep/2018 11:33:30] "GET /cgi-bin/recursive%20fibonacci.py
HTTP/1.1" 200 -
127.0.0.1 - - [06/Sep/2018 11:33:30] command:
C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
"C:\Users\Roger\documents\roger\python\www\cgi-bin\recursive fibonacci.py"
""
127.0.0.1 - - [06/Sep/2018 11:33:30] CGI script exited OK

So I'm confused about 2 things:
1. Why does the one file cause an error in PowerShell (and Anaconda
actually) when it seems to be the same as the other file which appears to
run with no error, but just not render? and,
2. What am I missing: why won't it render since the instructions I have at
the very beginning of this email say, basically, this is all I have to do
to get it to render?

I know this second question is maybe a little bit outside this forum, but I
am struggling to make sense out of what little I know and, truly, this
forum seems the nicest and most helpful I've encountered.

Thank you as always.


On Thu, Aug 30, 2018 at 6:01 AM Mats Wichmann  wrote:

> On 08/30/2018 02:54 AM, Alan Gauld via Tutor wrote:
> > On 30/08/18 00:09, Roger Lea Scherer wrote:
> >> I'm trying to implement a local host. My instructions tell me to type
> the
> >> following command in the command line, make sure I'm in the "www" folder
> >
> > So this is not the folder where python3 is installed. (See below)
> >
> >> python3 -m http.server --cgi 8000
> >
> >> I'm running Anaconda in Windows 10. I get an error: 'python3' is not
> >> recognized as an internal or external command, operable program or batch
> >> file.
> >> But this is the folder that I thought Python 3.6 was in.
> >
> > you said you were in www which is not where Python should
> > be installed.
> >
> > It is likely that Python is not in your system PATH.
> > You need to find out where it is installed and add it.
> > In a standard Python install it would either be in
> > C:\PROGRAM FILES\PYTHON or
> > C:\PYTHON
> >
> > But Anaconda (version please?) could use its own path
> > such as
> >
> > C:\ANACONDA\PYTHON
> >
> >> So how do I correct this? I suspect python3 isn't in this folder. I
> know I
> >> have python3 because I run python3.6 shell practically every day.
> >
> > How do you run it?
> >>From a command line or via a menu/shortcut?
> >
> >> looked in the Program Files folder, came up empty as far as python is
> >> concerned, but I don't know where else to look?
>
> Several things that could help:
>
> you can ask python itself to tell you where it is, since you say python
> works for you.
>
> >>> import sys
> >>> print(sys.executable)
>
> the standard Windows python defaults to a "user install", so it could be
> in a place like
>
> {yourhomdirectory}/AppData/Local/Programs/Python
>
> Anaconda, as Alan says, likely puts it somewhere different.
>
> Python 3 isn't named python3 on Windows unless you take steps to make it
> so, it's just called python.  So modify your instructions accordingly.
>
> Also on Windows, if it was installed, there is a separate thing called
> the Python Launcher, which lets you run the command "py" which typically
> gets put in a place that is always found, and avoids the fiddling with
> getting Python itself into your PATH.  But I'm unsure whether the
> Anaconda install actually installs that. You could try... just type: py
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
Roger Lea Scherer
623.255.7719

  *Strengths:*
   Input, Strategic,
Responsibility,

Learner, Ideation
___
Tutor mai

Re: [Tutor] Writing for loop output to csv

2018-09-06 Thread Alan Gauld via Tutor
On 06/09/18 17:32, Brandon Creech wrote:
> Hi, I am working to forecast the temperatures for the next 5 days using an
> API and a for loop. I would like write the output of this loop to a csv in
> this format::
> 
> Columns: City, min1, max1, min2, max2,min3,max3,min4,max4,min5,max5
> data: Athens,Greece 25.4,26.7etc.
>  Nantou,Taiwan 16.18, ..etc
> 
> the data prints out on top of each other like this:
> 
> Nantou, Taiwan 29.49 20.79
> Nantou, Taiwan 30. 49 21.56

Sorry, can you elaborate I don;t understand what you mean?
The data you show does not seem to be related to the output
you show?

Also having the city be a dual name separated by a comma
within a csv file is likely to create confusion, not least
for yourself. I would suggest splitting it to city and
country. If that doesn't work (for your API perhaps)
then consider changing the separator.  If that doesn't
work for you then tough luck, you'll need to persevere.


> from forecastiopy import *
> import csv

You import csv but don't seem to use it anywhere below?

> 
> api_key = 'XXX'
> 
> cities = {"Taree, Australia":(-31.8895,152.), "Belmopan,
> Belize":(17.251,-88.759), "Nanaimo, Canada":(49.1659,-123.9401),

Again this does not seem to match either the the
data sample shown above or the output.

> for city, coords in cities.items():
> weather = ForecastIO.ForecastIO( api_key, latitude=coords[0],
> longitude=coords[1] )
> daily = FIODaily.FIODaily(weather)
> for day in range(2,7):
> print(str(city) + " " + str(daily.get_day(day)['temperatureMax']) +
> " " + str(daily.get_day(day)['temperatureMin']))

This will produce something like the output you showed
(although I don't think you need all the str() calls).
But it is nothing like the output you said you wanted
in the csv file and you are not using csv anywhere
to generate it.

What exactly are you asking us to help you with?
Everything seems to be doing exactly what you are
asking it to do.

-- 
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] localhosting

2018-09-06 Thread Cameron Simpson
Note: replies inline below, and irrelevant verbiage trimmed. We prefer this 
format on this list: it makes it easy to respond point by point and makes the 
reply read like a conversation. Please consider adopting this style here.


Anyway, to your message:

On 06Sep2018 12:09, Roger Lea Scherer  wrote:

Thank you all for your help. I am still chugging away at this problem. I've
switched to Windows PowerShell Version 5.1 since I can select, copy, and
paste more easily in PowerShell. (I can't figure out how to do that in
Anaconda and I get the same errors in PowerShell.)

I removed the "3" in "python3" and it works; along with moving the "www"
folder to where python resides. Thanks for the help.


Now switch back to python3 and see if it still works, even temporarily. You 
want to isolate what was the fix and what was irrelevant.



So it appears the local host is running correctly, but when I run this code:

print("Hello World")

Chrome does not render and I get an error message in PowerShell:
127.0.0.1 - - [06/Sep/2018 11:22:46] "GET /cgi-bin/hello.py HTTP/1.1" 200 -
127.0.0.1 - - [06/Sep/2018 11:22:46] command:
C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
C:\Users\Roger\documents\roger\python\www\cgi-bin\hello.py ""
127.0.0.1 - - [06/Sep/2018 11:22:46] b'  File
"C:\\Users\\Roger\\documents\\roger\\python\\www\\cgi-bin\\hello.py", line
1\r\nprint ""\r\n ^\r\nSyntaxError: Missing
parentheses in call to \'print\'. Did you mean print("")?\r\n'
127.0.0.1 - - [06/Sep/2018 11:22:46] CGI script exit status 0x1


I'm guessing you "Hello World" print was after the start of the  tag.  
Look at the rrror message you've recited above: it is talking about line 1 of 
your hello.py file, which has this:


 print ""

That is an old-style print _statement_, which will not work in Python 3. So 
your other print isn't the source of this error message: likely it is never 
reached.


Edit your python files to start with this import:

 from __future__ import print_function

That will cause python 2 to expect print to be a function (needing brackets) 
and not a statement, meaning the same syntax will be required for both python 2 
and 3, providing you with consistent behaviour.


Then change the print statement into a print function call:

 print("")


BUT when I run this code:
def fib(n):
   if n < 2:
   return n
   return fib(n-1) + fib(n-2)

print(fib(15))


Note: python 3 print function.


Chrome does not render but I DON'T get an error message, I get this:
127.0.0.1 - - [06/Sep/2018 11:33:30] "GET /cgi-bin/recursive%20fibonacci.py
HTTP/1.1" 200 -
127.0.0.1 - - [06/Sep/2018 11:33:30] command:
C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
"C:\Users\Roger\documents\roger\python\www\cgi-bin\recursive fibonacci.py"
""
127.0.0.1 - - [06/Sep/2018 11:33:30] CGI script exited OK


There should be more than a bare number in the output. CGI scripts require one 
to provide HTTP response headers before the body text: that way they can return 
other stuff than HTML (eg an image or CSV data etc). Try putting these print 
calls at the start of that script:


 print('Content-Type: text/html')
 print()

which mark the rest of the script output as HTML and then provide a blank line 
to indicate the end of the headers. The objective is that your script produces 
this output:


 Content-Type: text/html

 325

That's a made up number, I haven't bothered to compute fib(15), sorry.


So I'm confused about 2 things:
1. Why does the one file cause an error in PowerShell (and Anaconda
actually) when it seems to be the same as the other file which appears to
run with no error, but just not render? and,


Because you're running the file with Python 2 by hand and Python 3 from the web 
server (see the "Python\Python37-32\python.exe" in the log?) Apply the 
suggested __future__ import and you'll get consistent behaviour.



2. What am I missing: why won't it render since the instructions I have at
the very beginning of this email say, basically, this is all I have to do
to get it to render?

I know this second question is maybe a little bit outside this forum, but I
am struggling to make sense out of what little I know and, truly, this
forum seems the nicest and most helpful I've encountered.


The output of a CGI script should be a valid HTTP response: you need some HTTP 
headers describing the output format and _then_ the core programme output. The 
minimal header is a Content-Type: header to denote the program result format.  
See above for how to stick one at the top of any of your CGI scripts.


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


Re: [Tutor] Writing for loop output to csv

2018-09-06 Thread Cameron Simpson

On 06Sep2018 12:32, Brandon Creech  wrote:

Hi, I am working to forecast the temperatures for the next 5 days using an
API and a for loop. I would like write the output of this loop to a csv in
this format::

Columns: City, min1, max1, min2, max2,min3,max3,min4,max4,min5,max5
data: Athens,Greece 25.4,26.7etc.
Nantou,Taiwan 16.18, ..etc

the data prints out on top of each other like this:

Nantou, Taiwan 29.49 20.79
Nantou, Taiwan 30. 49 21.56


Normally there would be more commas in CSV output, eg:

 Nantou, Taiwan,29.49,20.79

and because "Nantou, Taiwan" is a single string, it would typically look like 
this:


 "Nantou, Taiwan",29.49,20.79

to make that clear, otherwise the embedded comma would make it two CSV columns.


Code I have:

from forecastiopy import *
import csv


I see you're importing the csv module, but not using it. [...]

[...snip...]

for city, coords in cities.items():
   weather = ForecastIO.ForecastIO( api_key, latitude=coords[0],
longitude=coords[1] )
   daily = FIODaily.FIODaily(weather)
   for day in range(2,7):
   print(str(city) + " " + str(daily.get_day(day)['temperatureMax']) +
" " + str(daily.get_day(day)['temperatureMin']))


First a small suggestion. Change this:

 print(str(city) + " " + str(daily.get_day(day)['temperatureMax']) + " " + 
str(daily.get_day(day)['temperatureMin']))

like this:

 day_data = daily.get_day(day)
 print(str(city) + " " + str(day_data['temperatureMax']) + " " + 
str(day_data['temperatureMin']))

It fetches the daily data just once and makes your subsequent code easily to 
read and debug.


Second, your actual problem. Your loop is basicly sound, but you're using a 
print call for the output.  The csv module provides a writer object which does 
all the heavy lifting for you: turns strings into quotes strings, puts in 
commas, etc. Try this change:


 csvw = csv.writer(sys.stdout)
 for city, coords in cities.items():
 weather = ForecastIO.ForecastIO( api_key, latitude=coords[0], 
longitude=coords[1] )
 daily = FIODaily.FIODaily(weather)
 for day in range(2,7):
 day_data = daily.get_day(day)
 csvw.writerow([city, day_data['temperatureMax'], 
day_data['temperatureMin']])

You'll also need to "import sys" up the top to use the name "sys.stdout".

See how now you're just passing a list of the values to the csv writer?

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


Re: [Tutor] localhosting

2018-09-06 Thread Alan Gauld via Tutor
On 06/09/18 20:09, Roger Lea Scherer wrote:

> I removed the "3" in "python3" and it works; along with moving the "www"
> folder to where python resides. Thanks for the help.

That may appear to work but I suspect its just hiding the real issue
which is about setting the correct path values.

Did you try using the py launcher that Mats suggested.
I think that is the officially recommended solution
for these issues on Windows...

Anyway...
> So it appears the local host is running correctly, 

I assume you mean some kind of web server running
on the localhost network port? ie 127.0.0.1?

> print("Hello World")
> 
> Chrome does not render and I get an error message in PowerShell:
> 127.0.0.1 - - [06/Sep/2018 11:22:46] "GET /cgi-bin/hello.py HTTP/1.1" 200 -
> 127.0.0.1 - - [06/Sep/2018 11:22:46] command:
> C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
> C:\Users\Roger\documents\roger\python\www\cgi-bin\hello.py ""
> 127.0.0.1 - - [06/Sep/2018 11:22:46] b'  File
> "C:\\Users\\Roger\\documents\\roger\\python\\www\\cgi-bin\\hello.py", line
> 1\r\nprint ""\r\n ^\r\nSyntaxError: Missing
> parentheses in call to \'print\'. Did you mean print("")?\r\n'
> 127.0.0.1 - - [06/Sep/2018 11:22:46] CGI script exit status 0x1

Are you sure that the file hello.py in cgi-bin only
contains the one line? It looks like you may have a
default one with Python 2.7 code in it?



> 1. Why does the one file cause an error in PowerShell (and Anaconda
> actually) when it seems to be the same as the other file which appears to
> run with no error, but just not render? and,

Neither of them output valid html, so I guess that's
why they don't render. The first doesn't appear to be
the hello.py that you think it is - try opening it
in your text editor to check (using the exact path
that the log message uses).

> 2. What am I missing: why won't it render since the instructions I have at
> the very beginning of this email say, basically, this is all I have to do
> to get it to render?

Maybe your instructions are wrong? Maybe they are for Python 2 and you
are running Python 3?

You might find it helpful to read the section on writing web
applications in my tutorial:

http://www.alan-g.me.uk/l2p2/index.htm

Look in the Applications section and read the Web Server
topic for a short example of how to use the basic http.server

> I know this second question is maybe a little bit outside this forum, 

Nope, explanations of why things go wrong are entirely apropos

-- 
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] Writing for loop output to csv

2018-09-06 Thread Peter Otten
Cameron Simpson wrote:

> On 06Sep2018 12:32, Brandon Creech  wrote:
>>Hi, I am working to forecast the temperatures for the next 5 days using an
>>API and a for loop. I would like write the output of this loop to a csv in
>>this format::
>>
>>Columns: City, min1, max1, min2, max2,min3,max3,min4,max4,min5,max5
>>data: Athens,Greece 25.4,26.7etc.
>> Nantou,Taiwan 16.18, ..etc

> print call for the output.  The csv module provides a writer object which
> does all the heavy lifting for you: turns strings into quotes strings,
> puts in commas, etc. Try this change:
> 
>   csvw = csv.writer(sys.stdout)
>   for city, coords in cities.items():
>   weather = ForecastIO.ForecastIO( api_key, latitude=coords[0],
>   longitude=coords[1] ) 
>   daily = FIODaily.FIODaily(weather)
>   for day in range(2,7):
>   day_data = daily.get_day(day)
>   csvw.writerow([city, day_data['temperatureMax'],
>   day_data['temperatureMin']])

If you want all min/max pairs in the same row you can prebuild that row in a 
list. With minimal changes to Cameron's code:

# untested
csvw = csv.writer(sys.stdout)
for city, coords in cities.items():
weather = ForecastIO.ForecastIO(
api_key, latitude=coords[0], longitude=coords[1]
)
daily = FIODaily.FIODaily(weather)
row = [city]
for day in range(2, 7):
day_data = daily.get_day(day)
row.append(day_data['temperatureMin'])
row.append(day_data['temperatureMax'])
csvw.writerow(row)


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