[Tutor] running a .py file from the comand line

2018-04-02 Thread Rex Florian via Tutor
Hello,

I am running Python 3.6 in a Window 7 environment.  I have a python script that 
I am trying to run from the command line.

The script is from a Learning to Program file demonstrating event driven 
programming.  I have copied it to a file named Ascii Keys.py into my user 
directory c:\Users\Rex

I try to execute the file by typing python Ascii Keys.py at the command line 
and receive the following message:

python:  can't open file 'Ascii':  [errno2] no such file or directory

I expected the python .py file to open and run.

I check with the path command and receive among other paths these to paths to 
Python.

c:\Users\Rex/AppData\Local\Programs\Python\Python36-32\Scripts

c:\Users\Rex/AppData\Local\Programs\Python\Python36-32

I also load python from c:\Users\Rex and try running Ascii Keys from >> and get 
the following error:

File "", line 1
   Ascii Keys

SyntaxError:  invalid syntax

Why doesn't python open my file?

Also, in windows explorer, I tried to find the above paths and could not see 
AppData under c:\Users\Rex

What am I missing here?

import msvcrt
import sys

# First the event handlers
def doKeyEvent(key):
if key == '\x00' or key == '\xe0':
   key = msvcrt.getch()
print ( ord(key), ' ', end='')
sys.stdout.flush() # make sure it appears on screen

def doQuit(key):
print() # force a new line
raise SystemExit

# first clear some screen space
lines = 25 
for n in range(lines): print()

# Now the main event-loop
while True:
ky = msvcrt.getch()
if len(str(ky)) != 0:
# we have a real event
if " " in str(ky):
doQuit(ky)
else: 
doKeyEvent(ky)

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


Re: [Tutor] running a .py file from the comand line

2018-04-02 Thread Alan Gauld via Tutor
On 02/04/18 01:46, Rex Florian via Tutor wrote:
> Hello,
> 
> I am running Python 3.6 in a Window 7 environment. > I have a python script 
> that I am trying to run from the command line.

> I have copied it to a file named Ascii Keys.py into my user directory 
> c:\Users\Rex
> 
> I try to execute the file by typing python Ascii Keys.py 

Try

python c:\Users\Rex\"ascii keys.py"

Note the quotes to cater for the space.

> python:  can't open file 'Ascii':  [errno2] no such file or directory

The space confuses windows CMD, so it thinks you have
two files called 'Ascii' and 'keys.py'

By quoting the name it should see it as a single file name.

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

2018-04-02 Thread Alan Gauld via Tutor
On 02/04/18 04:19, Steven D'Aprano wrote:
> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote:
>> On01/04/18 20:20, Albert-Jan Roskam wrote:
>>> fmt="%Y-%m-%d %H:%M\n"
>>> f.write(now.strftime(fmt))
>>> Lately I've been using format(), which uses __format__, because I find it 
>>> slightly more readable:
>>> format(datetime.now(), "%Y-%m-%d %H:%M")
>> Interesting,
>> I didn't know that format() recognised the datetime format codes.
> It doesn't. It is the datetime object that recognises them. format() 
> merely passes the format string to the datetime.__format__ method, which 
> is what recognises the codes. It doesn't care what it is.
Aha! That makes sense. I've never really used format() so have never
bothered to find out how it works. To the point that until this thread I
hadn't realized we even had a __format__() operator.

As I said, I need to do some reading. Obviously a gap in my python
education.

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

2018-04-02 Thread Mats Wichmann
On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote:
> On 02/04/18 04:19, Steven D'Aprano wrote:
>> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote:
>>> On01/04/18 20:20, Albert-Jan Roskam wrote:
 fmt="%Y-%m-%d %H:%M\n"
 f.write(now.strftime(fmt))
 Lately I've been using format(), which uses __format__, because I find it 
 slightly more readable:
 format(datetime.now(), "%Y-%m-%d %H:%M")
>>> Interesting,
>>> I didn't know that format() recognised the datetime format codes.
>> It doesn't. It is the datetime object that recognises them. format() 
>> merely passes the format string to the datetime.__format__ method, which 
>> is what recognises the codes. It doesn't care what it is.
> Aha! That makes sense. I've never really used format() so have never
> bothered to find out how it works. To the point that until this thread I
> hadn't realized we even had a __format__() operator.
> 
> As I said, I need to do some reading. Obviously a gap in my python
> education.
> 

so since we're all learning things here, how would this play out with
the new f-strings?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pythonic

2018-04-02 Thread David Rock

> On Mar 30, 2018, at 04:15, George Fischhof  wrote:
> 
> 2.)
> argparse
> 
> it is good, but you can write more Pythonic code using click
> https://pypi.python.org/pypi/click/
> it is also Pythonic to use / know the Python ecosystem (the packages)

It’s just as (if not more) pythonic to use the standard libraries. It’s very 
common in a professional environment to not have access to outside (i.e., 
internet) resources.  I wouldn’t venture into Pypi unless there’s something you 
can’t do well with what’s already provided by default.


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] pythonic

2018-04-02 Thread leam hall
On Mon, Apr 2, 2018 at 9:01 AM, David Rock  wrote:

> It’s just as (if not more) pythonic to use the standard libraries. It’s very 
> common in a professional environment to not have access to outside (i.e., 
> internet) resources.  I wouldn’t venture into Pypi unless there’s something 
> you can’t do well with what’s already provided by default.

+1.

Use standard libraries; most places I've worked didn't allow a lot of
non-standard library code at all. If something is in the standard
library you have some level of assurance that the maintenance is
moderated and monitored.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pythonic

2018-04-02 Thread Steven D'Aprano
On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote:
> On 04/02/2018 02:56 AM, Alan Gauld via Tutor wrote:
> > On 02/04/18 04:19, Steven D'Aprano wrote:
> >> On Sun, Apr 01, 2018 at 10:58:51PM +0100, Alan Gauld via Tutor wrote:
> >>> On01/04/18 20:20, Albert-Jan Roskam wrote:
>  fmt="%Y-%m-%d %H:%M\n"
>  f.write(now.strftime(fmt))
>  Lately I've been using format(), which uses __format__, because I find 
>  it slightly more readable:
>  format(datetime.now(), "%Y-%m-%d %H:%M")
> >>> Interesting,
> >>> I didn't know that format() recognised the datetime format codes.
> >> It doesn't. It is the datetime object that recognises them. format() 
> >> merely passes the format string to the datetime.__format__ method, which 
> >> is what recognises the codes. It doesn't care what it is.
> > Aha! That makes sense. I've never really used format() so have never
> > bothered to find out how it works. To the point that until this thread I
> > hadn't realized we even had a __format__() operator.
> > 
> > As I said, I need to do some reading. Obviously a gap in my python
> > education.
> > 
> 
> so since we're all learning things here, how would this play out with
> the new f-strings?

I don't think f-strings are even a bit Pythonic.

They look like string constants, but they're actually a hidden call to 
eval().

py> x = 2
py> f'hello {3*x}'
'hello 6'

So they can execute arbitrary code that has arbitrary side-effects:

py> L = []
py> f'hello {L.append(1) or 99}'
'hello 99'
py> L
[1]


By my count, they violate at least three of the Zen of Python:

Explicit is better than implicit.
Simple is better than complex.
Special cases aren't special enough to break the rules.

They can only be used once, and are not re-usable (unlike proper 
templates):


py> x = 2
py> template = f'x = {x}'  # gets the value of x now
py> print(template)
x = 2
py> x = 99
py> print(template)  # still has the old value of x
x = 2


However, they do use the same mechanism as format():

py> from datetime import datetime
py> t = datetime.now()
py> format(t, '%H:%M')
'23:22'
py> f'{t:%H:%M}'
'23:22'



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


Re: [Tutor] pythonic

2018-04-02 Thread Peter Otten
Steven D'Aprano wrote:

> On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote:

>> so since we're all learning things here, how would this play out with
>> the new f-strings?
> 
> I don't think f-strings are even a bit Pythonic.
> 
> They look like string constants, but they're actually a hidden call to
> eval().

But because you cannot f-ify a string variable (without an additional eval() 
call) you aren't tempted to feed them user-provided data.

> They can only be used once, and are not re-usable (unlike proper
> templates):

You can't eat your cake an have it. As "proper templates" they would indeed 
be as dangerous as eval().

> By my count, they violate at least three of the Zen of Python:
> 
> Explicit is better than implicit.
> Simple is better than complex.
> Special cases aren't special enough to break the rules.

As I'm getting tired of writing

"...{foo}...{bar}...".format(foo=foo, bar=bar, ...)

lately I'd say they win big in the "practicality beats you-name-it" area.


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


Re: [Tutor] pythonic

2018-04-02 Thread Mats Wichmann
On 04/02/2018 08:28 AM, Peter Otten wrote:
> Steven D'Aprano wrote:
> 
>> On Mon, Apr 02, 2018 at 06:49:52AM -0600, Mats Wichmann wrote:
> 
>>> so since we're all learning things here, how would this play out with
>>> the new f-strings?
>>
>> I don't think f-strings are even a bit Pythonic.
>>
>> They look like string constants, but they're actually a hidden call to
>> eval().
> 
> But because you cannot f-ify a string variable (without an additional eval() 
> call) you aren't tempted to feed them user-provided data.
> 
>> They can only be used once, and are not re-usable (unlike proper
>> templates):
> 
> You can't eat your cake an have it. As "proper templates" they would indeed 
> be as dangerous as eval().
> 
>> By my count, they violate at least three of the Zen of Python:
>>
>> Explicit is better than implicit.
>> Simple is better than complex.
>> Special cases aren't special enough to break the rules.
> 
> As I'm getting tired of writing
> 
> "...{foo}...{bar}...".format(foo=foo, bar=bar, ...)
> 
> lately I'd say they win big in the "practicality beats you-name-it" area.

so swooping back to the origial code, it seems there are several options
for expressing the output. Picking one of the lines (and replacing the
writes to a file with prints for simplicity):

print("Date: {}-{}-{} {}:{}\n".format(now.year,
now.month,
now.day,
now.hour,
now.minute))
print("Date:", format(now, "%Y-%m-%d %H:%M"))
print(f"Date: {now:%Y-%m-%d %H:%M}")

those aren't exactly identical, since the using %m-%d form causes
zero-padding:

Date: 2018-4-2 10:23

Date: 2018-04-02 10:23
Date: 2018-04-02 10:23


For me, I don't know if the f-string version is the most Pythonic, but
it has an appealing conciseness in this particular context :)



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


Re: [Tutor] running a .py file from the comand line

2018-04-02 Thread eryk sun
On Mon, Apr 2, 2018 at 8:53 AM, Alan Gauld via Tutor  wrote:
>
> Try
>
> python c:\Users\Rex\"ascii keys.py"
>
> Note the quotes to cater for the space.
>
>> python:  can't open file 'Ascii':  [errno2] no such file or directory
>
> The space confuses windows CMD, so it thinks you have
> two files called 'Ascii' and 'keys.py'

Unlike Unix, this is not due to the shell in Windows. A process is
started with a raw command line string. For a C/C++ application, the
default process entry point is provided by the C runtime and does the
setup work to call the application entry point (e.g. [w]main). This
includes parsing the command line into an argv array according to
documented rules [1]. An application can also call GetCommandLineW [2]
and CommandLineToArgvW [3].

[1]: https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments
[2]: https://msdn.microsoft.com/en-us/library/ms683156
[3]: https://msdn.microsoft.com/en-us/library/bb776391

CPython is written in C and uses the standard Windows C/C++ wmain and
wWinMain application entry points. If you run "python
C:\Users\Rex\Ascii Keys.py", the C runtime parses this into an argv
array with 3 items: "python", "C:\Users\Rex\Ascii", and "Keys.py".
Thus Python tries to open a script named "C:\Users\Rex\Ascii".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running a .py file from the comand line

2018-04-02 Thread Mats Wichmann
On 04/02/2018 11:41 AM, eryk sun wrote:
> On Mon, Apr 2, 2018 at 8:53 AM, Alan Gauld via Tutor  wrote:
>>
>> Try
>>
>> python c:\Users\Rex\"ascii keys.py"
>>
>> Note the quotes to cater for the space.
>>
>>> python:  can't open file 'Ascii':  [errno2] no such file or directory
>>
>> The space confuses windows CMD, so it thinks you have
>> two files called 'Ascii' and 'keys.py'
> 
> Unlike Unix, this is not due to the shell in Windows. A process is
> started with a raw command line string. For a C/C++ application, the
> default process entry point is provided by the C runtime and does the
> setup work to call the application entry point (e.g. [w]main). This
> includes parsing the command line into an argv array according to
> documented rules [1]. An application can also call GetCommandLineW [2]
> and CommandLineToArgvW [3].
> 
> [1]: 
> https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments
> [2]: https://msdn.microsoft.com/en-us/library/ms683156
> [3]: https://msdn.microsoft.com/en-us/library/bb776391
> 
> CPython is written in C and uses the standard Windows C/C++ wmain and
> wWinMain application entry points. If you run "python
> C:\Users\Rex\Ascii Keys.py", the C runtime parses this into an argv
> array with 3 items: "python", "C:\Users\Rex\Ascii", and "Keys.py".
> Thus Python tries to open a script named "C:\Users\Rex\Ascii".


so in summary... if you have things set up so you can click-to-launch,
filenames with spaces in them will work, but you'll be better off
avoiding them, esp. if you intend to launch from a command line.


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


[Tutor] Python help

2018-04-02 Thread Shannon Evans via Tutor
Hi, I am trying to write a code with if statements but the code keeps just
repeating and not carrying on.
I am trying to get user to input a grade, either A, B, C, D, E or F and
trying to have an error message if anything but these values are inputted.
This is what i've wrote so far:

while True:
try:
Grade = int(raw_input("Please enter your Grade: "))
except ValueError:
print("Error, Please enter A, B, C, D, E or F")
continue

if Grade <> 'A','B','C','D','E','F':
print ("Error, Please enter A, B, C, D, E or F")
continue
else:#Grade succesfully completed, and we're happy with its value.
#ready to exit the loop.
break

When trying to run it just writes the error message for any letter you
input.
Any hep would be appreciated thank you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pythonic

2018-04-02 Thread Steven D'Aprano
On Mon, Apr 02, 2018 at 04:28:10PM +0200, Peter Otten wrote:

> > They look like string constants, but they're actually a hidden call to
> > eval().
> 
> But because you cannot f-ify a string variable (without an additional eval() 
> call) you aren't tempted to feed them user-provided data.

If only that were the case...

https://mail.python.org/pipermail/python-list/2018-March/731967.html

He reads f-strings from user-supplied data, then evals them.

But its okay, he's only doing it within his own organisation, and we all 
know that "insiders" are always 100% trusted. "Insider attack" is just a 
pair of words. Right?


> As I'm getting tired of writing
> 
> "...{foo}...{bar}...".format(foo=foo, bar=bar, ...)

You can write:

template.format(**(locals()))

or possibly nicer still:

template.format_map(locals())


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


Re: [Tutor] Python help

2018-04-02 Thread Steven D'Aprano
On Mon, Apr 02, 2018 at 11:44:39PM +0100, Shannon Evans via Tutor wrote:
> Hi, I am trying to write a code with if statements but the code keeps just
> repeating and not carrying on.
> I am trying to get user to input a grade, either A, B, C, D, E or F and
> trying to have an error message if anything but these values are inputted.
> This is what i've wrote so far:
> 
> while True:
> try:
> Grade = int(raw_input("Please enter your Grade: "))

Here you convert the user's input into an integer, a number such as 1, 
2, 57, 92746 etc. If that *fails*, you run this block:

> except ValueError:
> print("Error, Please enter A, B, C, D, E or F")
> continue

and start again.

If it succeeds, because the user entered (let's say) 99, then you run 
another test:

> if Grade <> 'A','B','C','D','E','F':
> print ("Error, Please enter A, B, C, D, E or F")
> continue

That will ALWAYS fail, because you are trying to compare the integer 99 
(for example) with the tuple of six characters:

99 <> ('A', 'B','C','D','E','F')

which is always false. So that block will ALWAYS fail, and you always go 
back to the start.




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


Re: [Tutor] Python help

2018-04-02 Thread Alan Gauld via Tutor
On 02/04/18 23:44, Shannon Evans via Tutor wrote:
> Hi, I am trying to write a code with if statements but the code keeps just
> repeating and not carrying on.

There are quite a few problems here, see comments below.

> while True:
> try:
> Grade = int(raw_input("Please enter your Grade: "))

You are trying to convert the input to an integer.
But A-F will not convert so thats the first problem
right there.

> except ValueError:
> print("Error, Please enter A, B, C, D, E or F")
> continue

And here you prompt for an A-F input not an integer.


> if Grade <> 'A','B','C','D','E','F':
This doesn't do what you think. It tests to see if Grade
is not equal to a tuple of values ('A','B','C','D','E','F')
You need to use 'in' instead. That will check whether
or not Grade is *one* of the values in the tuple.

if Grade not in 'A','B','C','D','E','F':

> print ("Error, Please enter A, B, C, D, E or F")
> continue
> else:#Grade succesfully completed, and we're happy with its value.
> #ready to exit the loop.
> break

Taking out the conversion to int() would be a good start.
Then change the if test to use 'in'.

-- 
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 help

2018-04-02 Thread Steven D'Aprano
On Tue, Apr 03, 2018 at 01:00:59AM +0100, Alan Gauld via Tutor wrote:

> You need to use 'in' instead. That will check whether
> or not Grade is *one* of the values in the tuple.
> 
> if Grade not in 'A','B','C','D','E','F':

Actually, that returns a tuple consisting of a flag plus five more 
strings:

py> 'A' in 'A','B','C', 'D', 'E', 'F'
(True, 'B','C', 'D', 'E', 'F')

We need to put round brackets (parentheses) around the scores:

if Grade not in ('A','B','C','D','E','F'):


Don't be tempted to take a short-cut:

# WRONG!
if Grade not in 'ABCDEF':

as that would accepted grades like "CDEF" and "BC".



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


Re: [Tutor] Python help

2018-04-02 Thread Alan Gauld via Tutor
On 03/04/18 01:19, Steven D'Aprano wrote:
> On Tue, Apr 03, 2018 at 01:00:59AM +0100, Alan Gauld via Tutor wrote:
>
>> You need to use 'in' instead. That will check whether
>> or not Grade is *one* of the values in the tuple.
>>
>> if Grade not in 'A','B','C','D','E','F':
> Actually, that returns a tuple consisting of a flag plus five more 
> strings:
>
> py> 'A' in 'A','B','C', 'D', 'E', 'F'
> (True, 'B','C', 'D', 'E', 'F')
>
> We need to put round brackets (parentheses) around the scores:
>
> if Grade not in ('A','B','C','D','E','F'):

Oops, yes, a cut n' paste error, I forgot to add the parens.
mea culpa!

-- 

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 help

2018-04-02 Thread Alan Gauld via Tutor
On 03/04/18 01:19, Steven D'Aprano wrote:
>
>> if Grade not in 'A','B','C','D','E','F':
> Actually, that returns a tuple consisting of a flag plus five more 
> strings:
>
> py> 'A' in 'A','B','C', 'D', 'E', 'F'
> (True, 'B','C', 'D', 'E', 'F')

Although in the context of the program the colon at the end ensures
we get a syntax error so the mistake is found pretty quickly...

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