Re: [Tutor] really basic question..

2017-08-05 Thread Alan Gauld via Tutor
On 05/08/17 16:48, bruce wrote:

> redid a search just now. found a bunch of sites that said it's
> doable.. embarrased

Just because its doable doesn't mean you should though...

Bare except clauses can hide a multitude of sins. Unless its
at the top level of your program and you use it to log
any errors that occur its probably a bad idea.

Much better to be as specific as possible and catch only
anticipated errors. Anything else will then cause a crash
and you can investigate the (unanticipated) cause.

Note, you can link multiple error types in a single
except clause

try:...
except Error1:...
except (Error2, Error3, Error4):

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


Re: [Tutor] unorderable types

2017-08-05 Thread Howard Lawrence
# this is a guess number game.
import random

guessesTaken = 0

print('hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', i am thinking of a number between 1 and 20')

while guessesTaken < 6:
print('take a guess.')
guess = input()
guess_value = int(guess)

guessesTaken = guessesTaken + 1

print("type(guess_value)=",type(guess_value))
print("type(number)=",type(number))

if guess_value < number:
print('your guess is too low.')

if guess_value > number:
print('your guess is too high.')

if guess_value == number:
break

if guess_value == number:
   guessesTaken = str(guessesTaken)
   print ('good job, ' + myName + '! you guessed my number in ' +
guessesTaken + ' guesses!')

if guess_value != number:
number = str(number)
print ('nope. the number i was thinking of was ' + number)

# this is a guess number game.
import random

guessesTaken = 0

print('hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', i am thinking of a number between 1 and 20')

while guessesTaken < 6:
print('take a guess.')
guess = input()
guess_value = int(guess)

guessesTaken = guessesTaken + 1

print("type(guess_value)=",type(guess_value))
print("type(number)=",type(number))

if guess_value < number:
print('your guess is too low.')

if guess_value > number:
print('your guess is too high.')

if guess_value == number:
break

if guess_value == number:
   guessesTaken = str(guessesTaken)
   print ('good job, ' + myName + '! you guessed my number in ' +
guessesTaken + ' guesses!')

if guess_value != number:
number = str(number)
print ('nope. the number i was thinking of was ' + number)

# this is a guess number game.
import random

guessesTaken = 0

print('hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', i am thinking of a number between 1 and 20')

while guessesTaken < 6:
print('take a guess.')
guess = input()
guess_value = int(guess)

guessesTaken = guessesTaken + 1

print("type(guess_value)=",type(guess_value))
print("type(number)=",type(number))

if guess_value < number:
print('your guess is too low.')

if guess_value > number:
print('your guess is too high.')

if guess_value == number:
break

if guess_value == number:
   guessesTaken = str(guessesTaken)
   print ('good job, ' + myName + '! you guessed my number in ' +
guessesTaken + ' guesses!')

if guess_value != number:
number = str(number)
print ('nope. the number i was thinking of was ' + number)

# this is a guess number game.
import random

guessesTaken = 0

print('hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', i am thinking of a number between 1 and 20')

while guessesTaken < 6:
print('take a guess.')
guess = input()
guess_value = int(guess)

guessesTaken = guessesTaken + 1

print("type(guess_value)=",type(guess_value))
print("type(number)=",type(number))

if guess_value < number:
print('your guess is too low.')

if guess_value > number:
print('your guess is too high.')

if guess_value == number:
break

if guess_value == number:
   guessesTaken = str(guessesTaken)
   print ('good job, ' + myName + '! you guessed my number in ' +
guessesTaken + ' guesses!')

if guess_value != number:
number = str(number)
print ('nope. the number i was thinking of was ' + number)

# this is a guess number game.
import random

guessesTaken = 0

print('hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', i am thinking of a number between 1 and 20')

while guessesTaken < 6:
print('take a guess.')
guess = input()
guess_value = int(guess)

guessesTaken = guessesTaken + 1

print("type(guess_value)=",type(guess_value))
print("type(number)=",type(number))

if guess_value < number:
print('your guess is too low.')

if guess_value > number:
print('your guess is too high.')

if guess_value == number:
break

if guess_value == number:
   guessesTaken = str(guessesTaken)
   print ('good job, ' + myName + '! you guessed my number in ' +
guessesTaken + ' guesses!')

if guess_value != number:
number = str(number)
print ('nope. the number i was thinking of was ' + number)

=
dont understand the error TypeError unorderable types 'int()' <' str()'
run the code from cmd prompt also error
inserted a print function before the first "if" statement
which return type (guess_value) = 
type(number) = 

Re: [Tutor] unorderable types

2017-08-05 Thread Howard Lawrence
Typing the : print("type (guess_value)=", type (guess_value))
print("type (number)=",type(number)

type (guess_value)= 
type (number)= 

==
the code runs again then prints
type guess_value =< class int>
type number=

=
Now Traceback kicks in
if guess_value < number:
TypeError : unorderable types: int() < str()

Ran the code from Cmd prompt
Got TypeError not supported between
Instance of 'int' and 'str'
=
It's seems up till
If guess_value < number:
 guess_value is a integer
 also number which the computer generated
 =
Still scratching my head-:(
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unorderable types

2017-08-05 Thread Cameron Simpson

On 04Aug2017 22:00, boB Stepp  wrote:

When I attempted to recreate his error message with the original code
snippets he sent, I got something a bit different:


py3: guess = input()
2
py3: guess < number
Traceback (most recent call last):
 File "", line 1, in 
TypeError: '<' not supported between instances of 'str' and 'int'



I don't know about the text of the message, but his original snippets included 
a:


 guess = int(guess)

between those 2 lines.

Anyway, we have his current code, which i can't made produce the error.

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] how to make an lexical scope block?

2017-08-05 Thread Xiaosong Chen
In C, it's a common pattern to use temporary variables in an lexical
scope to prevent the global scope from getting dirty.
For example,

```C
int a[N];
for (int i = 0; i < N; i++) {
  int temp = ...
  a[i] = ... // something got from temp
}
// temp do not exists here
```

But in python, such a pattern seems impossible. An straightforward
translation should be like this:

```python
a = []
for i in range(N):
temp = ...
a.append(...)# something got from temp
# temp DO EXISTS here, will be the value of the last iteration
```

As in the comment, the temporary variable remains existing after the
block. How do you usually deal with this?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to make an lexical scope block?

2017-08-05 Thread Peter Otten
Xiaosong Chen wrote:

> In C, it's a common pattern to use temporary variables in an lexical
> scope to prevent the global scope from getting dirty.
> For example,
> 
> ```C
> int a[N];
> for (int i = 0; i < N; i++) {
>   int temp = ...
>   a[i] = ... // something got from temp
> }
> // temp do not exists here
> ```
> 
> But in python, such a pattern seems impossible. An straightforward
> translation should be like this:
> 
> ```python
> a = []
> for i in range(N):
> temp = ...
> a.append(...)# something got from temp
> # temp DO EXISTS here, will be the value of the last iteration
> ```
> 
> As in the comment, the temporary variable remains existing after the
> block. How do you usually deal with this?

I put the code into a function

def create_a(n):
result = []
for i in range(n):
temp = ...
result.append(...)
return result

a = create_a(N)

At this point you could delete the function

del create_a

but in practice I never do that. 

If you want to go fancy you can rewrite the above as

def replace_with_result(*args, **kw):
def call(f):
return f(*args, **kw)
return call

@replace_with_result(N)
def a(n):
result = []
for i in range(n):
temp = ...
result.append(...)
return result

which will immediately overwrite the function a() with the result of the 
a(N) call -- but I prefer to keep the function around. 

The extra memory is usually negligible, and writing unit tests to detect 
blunders in create_a() is always a good idea, as trivial as it might appear 
on first sight...

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


Re: [Tutor] how to make an lexical scope block?

2017-08-05 Thread Steven D'Aprano
On Sat, Aug 05, 2017 at 03:23:57PM +0800, Xiaosong Chen wrote:
> In C, it's a common pattern to use temporary variables in an lexical
> scope to prevent the global scope from getting dirty.
[...]
> But in python, such a pattern seems impossible. An straightforward
> translation should be like this:
> 
> ```python
> a = []
> for i in range(N):
> temp = ...
> a.append(...)# something got from temp
> # temp DO EXISTS here, will be the value of the last iteration
> ```
> 
> As in the comment, the temporary variable remains existing after the
> block. How do you usually deal with this?


If you use functions (or methods), as you should, then temp will be a 
local variable of the function, and it doesn't matter if it still exists 
after the for loop. It is hidden inside the function scope, and cannot 
affect the global scope.

If you are using global scope, as sometimes is needed, then it depends. 
If this is a script that you run, then it really doesn't matter. A few 
temporary variables more or less doesn't make the script any better or 
worse. Don't worry about it.

If it is a library, where a nice clean global namespace is important, 
you can either refactor the code to make the problem go away, or delete 
the name when you are done:

del temp

deletes the name "temp" from the current namespace.

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


Re: [Tutor] file move with wait period

2017-08-05 Thread banda gunda

Thanks Cameron (I hope I address your first name correctly).
Get Outlook for iOS

From: Cameron Simpson 
Sent: Friday, August 4, 2017 5:18:58 AM
To: banda gunda
Cc: tutor@python.org
Subject: Re: [Tutor] file move with wait period

On 03Aug2017 11:22, banda gunda  wrote:
>Dear tutors,
>
>I am interested to move a set of files, periodically, from source_folder to 
>dest_folder.
>source_folder receives processed files from a ‘decompressing program’.  The 
>files are deposited from the ‘decompressing program’ at periodic random 
>intervals.  Furthermore some of the files dropped in the source_folder are 
>large, of typical size 800MB.  These files take time before it is completely 
>deposited in the source_folder.
>
>I could use shutil.move (source_folder, dest_folder).  But then how?

You'd normally use shutil.move(source_file, dest_file) i.e. move individual
files. Or os.rename, but that requires that the source and dest are the same
filesystem.

>The frequency of moving files from source_folder to dest_folder is flexible.  
>It could be once in 15 minutes (or even lengthier).
>Challenge for me:  How could the code include the wait statement to make sure 
>that the file in the source_folder is completely formed before attempting to 
>move to dest_folder?
>Thanks in advance for your comments/guidance.

That is the tricky thing; this shows up a lot, also with FTP upload locations
and so forth.

The problem, as you state, is knowing when the file is complete. There are 2
basic approaches: do not put a fil into the transfer directory before it is
complete, or to monitor the files for changes. On the premise that the file
will be being actively written until it is complete you could keep a list if
the files in the directory. For each name, record the file's size and
modification time. Wait for that to be unchanged "long enough"; you might then
decide it is ready to move.

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recommended Python Compiler

2017-08-05 Thread George Sconyers via Tutor
Thank you all for the feedback. As most of you pointed out I meant "IDE" 
instead of "compiler."  There are a lot of options for me to check out this 
weekend. Looking forward to cracking a case of Mountain Dew and digging in. 
George


Sent from Yahoo Mail for iPhone


On Tuesday, August 1, 2017, 03:26, Abdur-Rahmaan Janhangeer 
 wrote:

Yes indeed geany is a nice editor. I used it when i was beginning and even
wrote a post about it, Feel free to check it !

https://abdurrahmaanjanhangeer.wordpress.com/2016/11/02/python-getting-rid-of-identation-errors-quickly-checking-for-errors-and-the-use-of-editors/

On Tue, Aug 1, 2017 at 9:43 AM, Asokan Pichai  wrote:

> For a simple beginners editor -- geany is a great choice.
>
> It can be used with minimal or no configuration/set up; and once
> you know your way around  you can tweak it as much as you want.
>
> It is easily installable on Debian/Ubuntu
>
>
> --
> Asokan Pichai
> *---*
> We will find a way. Or, make one. (Hannibal)
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] Unorderable types

2017-08-05 Thread Peter Otten
boB Stepp wrote:

> Did the text of this error message change between Python 3.5 and 3.6?

Yes:

$ python3.5 -c '1 < ""'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: int() < str()

$ python3.6 -c '1 < ""'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '<' not supported between instances of 'int' and 'str'


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


Re: [Tutor] how to make an lexical scope block?

2017-08-05 Thread Alan Gauld via Tutor
On 05/08/17 08:23, Xiaosong Chen wrote:
> In C, it's a common pattern to use temporary variables in an lexical
> scope to prevent the global scope from getting dirty.

This was very common in the early days of C - around 1979-1985 - when
compilers often only considered the first 4 (or 6) characters of a
variable name - even though the name itself could be 16 or 32
characters long.

Thus 'index' and 'indeterminate' and 'indent' were all seen
as the same name. This required careful limiting of the lexical
scope of variables. Nowadays I don't see that as an issue and
most of the C code I work with doesn't limit scope beyond
a function definition. Maybe some old school C programmers
still worry about tight scoping but not the ones I work with!

As for Python it limits names to global (actually module)
and function scope. If you are creating well structured
code based on short clear functions there should not be
much of a problem.

So, to answer the question,

1) we don't tend to need such scoping because the language
permits many names, and it provides module and function scopes.

2) Also we avoid importing with the

from foo import *

style which increases risks of name pollution.

3) We need fewer temporary variables because we can use
tuple unpacking and generator expressions to replace many
scenarios where C would use a temporary variable.

In practice I've never found it to be an issue.

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] really basic question..

2017-08-05 Thread bruce
Hey guys.

A really basic question. I have the following:
  try:
element = WebDriverWait(driver,
100).until(EC.presence_of_element_located((By.ID,
"remarketingStoreId")))
  except TimeoutException:
driver.close()


I was wondering can I do something like the following to handle
"multiple" exceptions? Ie, have an "except" block that catches all
issues other than the specific TimeoutException.

  try:
element = WebDriverWait(driver,
100).until(EC.presence_of_element_located((By.ID,
"remarketingStoreId")))
  except TimeoutException:
driver.close()
  except :
driver.close()


I've looked all over SO, as well as the net in general. I might have
ust missed what I was looking for though.

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


Re: [Tutor] really basic question..

2017-08-05 Thread bruce
Lord...

redid a search just now. found a bunch of sites that said it's
doable.. embarrased

Not sure what I was looking for earlier.. need r u m!



On Sat, Aug 5, 2017 at 11:44 AM, bruce  wrote:
> Hey guys.
>
> A really basic question. I have the following:
>   try:
> element = WebDriverWait(driver,
> 100).until(EC.presence_of_element_located((By.ID,
> "remarketingStoreId")))
>   except TimeoutException:
> driver.close()
>
>
> I was wondering can I do something like the following to handle
> "multiple" exceptions? Ie, have an "except" block that catches all
> issues other than the specific TimeoutException.
>
>   try:
> element = WebDriverWait(driver,
> 100).until(EC.presence_of_element_located((By.ID,
> "remarketingStoreId")))
>   except TimeoutException:
> driver.close()
>   except :
> driver.close()
>
>
> I've looked all over SO, as well as the net in general. I might have
> ust missed what I was looking for though.
>
> Comments??  Thanks much.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor