[Tutor] eval func with floating...

2011-08-23 Thread simulacrx

hi;
---
check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")

while True:
a=raw_input("type your query : \n")
c=set(a).intersection(set(check))

if c:
print *eval("float(%s)"%a)*
else:
print "error! please use -defined operators-!"
-

this script works.. but if i type

15/8

it returns 1.. no floatin'.. i must fix this? can anyone help me?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eval func with floating...

2011-08-23 Thread Troels Emtekær Linnet
Thats because you do integer division.

Do either:

float(15)/8
15/float(8)
15.0/8
15/8.0

Just be sure, that either 15 or 8 is a float, and not both an integer.

Best
Troels


2011/8/16 simulacrx 

>  hi;
> ---
> check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")
>
> while True:
> a=raw_input("type your query : \n")
> c=set(a).intersection(set(check))
>
> if c:
> print *eval("float(%s)"%a)*
> else:
> print "error! please use -defined operators-!"
> -
>
> this script works.. but if i type
>
> 15/8
>
> it returns 1.. no floatin'.. i must fix this? can anyone help me?
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eval func with floating...

2011-08-23 Thread Peter Otten
simulacrx wrote:

> check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")

You have to quote the digits: 1 is an integer while "1" is a string of 
length one: 

>>> "1" == 1
False

Also, you forgot the "0". 
Note that there's no need to use a tuple as set() will happily accept a 
string:

check = set("0123456789/*-+,()")
 
> while True:
>  a=raw_input("type your query : \n")
>  c=set(a).intersection(set(check))
> 
>  if c:
>  print *eval("float(%s)"%a)*
>  else:
>  print "error! please use -defined operators-!"
> -
> 
> this script works.. but if i type
> 
> 15/8
> 
> it returns 1.. no floatin'.. i must fix this? can anyone help me?


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


Re: [Tutor] eval func with floating...

2011-08-23 Thread Joel Goldstick
On Tue, Aug 23, 2011 at 6:52 AM, Peter Otten <__pete...@web.de> wrote:

> simulacrx wrote:
>
> > check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")
>
> You have to quote the digits: 1 is an integer while "1" is a string of
> length one:
>
> >>> "1" == 1
> False
>
> Also, you forgot the "0".
> Note that there's no need to use a tuple as set() will happily accept a
> string:
>
> check = set("0123456789/*-+,()")
>
> > while True:
> >  a=raw_input("type your query : \n")
> >  c=set(a).intersection(set(check))
> >
> >  if c:
> >  print *eval("float(%s)"%a)*
> >  else:
> >  print "error! please use -defined operators-!"
> > -
> >
> > this script works.. but if i type
> >
> > 15/8
> >
> > it returns 1.. no floatin'.. i must fix this? can anyone help me?
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

I'm not sure if the performance difference would matter, but if you make
check a set above your loop there is no need to use the set(check) in the
loop when check will do

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


Re: [Tutor] eval func with floating...

2011-08-23 Thread Chanakya Mitra
c=set(a).intersection(set(check))

 

shouldn’t this be:

 

c=set(a).issubset(set(check))

 

?

 

set(a).intersection(set(check)) will be true as long as only one element 
appears in both. ie. 15/a8 kill the process and spit out an error instead of 
asking "error! please use -defined operators-!"

 


Sent: 23 August 2011 12:53
To: tutor@python.org
Subject: Re: [Tutor] eval func with floating...

 

 

On Tue, Aug 23, 2011 at 6:52 AM, Peter Otten <__pete...@web.de> wrote:

simulacrx wrote:

> check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")

You have to quote the digits: 1 is an integer while "1" is a string of
length one:

>>> "1" == 1
False

Also, you forgot the "0".
Note that there's no need to use a tuple as set() will happily accept a
string:

check = set("0123456789/*-+,()")


> while True:
>  a=raw_input("type your query : \n")
>  c=set(a).intersection(set(check))
>
>  if c:
>  print *eval("float(%s)"%a)*
>  else:
>  print "error! please use -defined operators-!"
> -
>
> this script works.. but if i type
>
> 15/8
>
> it returns 1.. no floatin'.. i must fix this? can anyone help me?



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


I'm not sure if the performance difference would matter, but if you make check 
a set above your loop there is no need to use the set(check) in the loop when 
check will do

-- 
Joel Goldstick

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


Re: [Tutor] eval func with floating...

2011-08-23 Thread Peter Otten
Chanakya Mitra wrote:

[simulacrx]
> c=set(a).intersection(set(check))

[Chanakya Mitra]
> shouldn’t this be:
> c=set(a).issubset(set(check))
> ?

> set(a).intersection(set(check)) will be true as long as only one element
> appears in both. ie. 15/a8 kill the process and spit out an error instead
> of asking "error! please use -defined operators-!"

You are right. 

The original poster may still run into errors because input strings that 
aren't a valid Python expression are possible even with the restricted 
character set.

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


[Tutor] Zip - password protect

2011-08-23 Thread James Reynolds
Does anyone know if a way to password protect a zip file? I have no issues
creating the zip file, but I would like to password protect as well. I'm
aware of a commercial solution, but I would like something open source.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zip - password protect

2011-08-23 Thread Emile van Sebille

On 8/23/2011 10:19 AM James Reynolds said...

Does anyone know if a way to password protect a zip file? I have no
issues creating the zip file, but I would like to password protect as
well. I'm aware of a commercial solution, but I would like something
open source.




The standard zipfile library includes a setpassword option.

help(zipfile.PyZipFile.setpassword)

Help on method setpassword in module zipfile:

setpassword(self, pwd) unbound zipfile.PyZipFile method
Set default password for encrypted files.


Emile




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


Re: [Tutor] Zip - password protect

2011-08-23 Thread James Reynolds
I tried that already.

that's only to set the default password to read the zip, not to set a
pasword (wording is misleading I think)

On Tue, Aug 23, 2011 at 1:42 PM, Emile van Sebille  wrote:

> On 8/23/2011 10:19 AM James Reynolds said...
>
>  Does anyone know if a way to password protect a zip file? I have no
>> issues creating the zip file, but I would like to password protect as
>> well. I'm aware of a commercial solution, but I would like something
>> open source.
>>
>>
>
> The standard zipfile library includes a setpassword option.
>
> help(zipfile.PyZipFile.**setpassword)
>
> Help on method setpassword in module zipfile:
>
> setpassword(self, pwd) unbound zipfile.PyZipFile method
>Set default password for encrypted files.
>
>
> Emile
>
>
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zip - password protect

2011-08-23 Thread Emile van Sebille

On 8/23/2011 11:23 AM James Reynolds said...

I tried that already.

that's only to set the default password to read the zip, not to set a
pasword (wording is misleading I think)



Did you see 
http://stackoverflow.com/questions/2195747/python-code-to-create-a-password-encrypted-zip-file 
?  Check the answer that references the external open-source 7-Zip utility.


Emile

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


Re: [Tutor] Zip - password protect

2011-08-23 Thread Alan Gauld

On 23/08/11 19:23, James Reynolds wrote:

I tried that already.

that's only to set the default password to read the zip, not to set a
pasword (wording is misleading I think)



I'm confused.

Which password are you thinking of?

Is it at the OS level to prevent access to the file
 - in which case the fact that its a zipfile is immaterial.
Or is it the zipfile's own password? In which case what
is different from the "default password to read the file"?


What exactly are you trying to achieve?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] issues with urllib and loading a webpage.

2011-08-23 Thread Robert Sjoblom
So, an issue regarding urllib (python 3) came up earlier. I solved it
by using httplib2 instead, but I'm rather curious as to why urllib
wouldn't work.

Here's the code I'm working with:
from http.client import HTTPConnection
HTTPConnection.debuglevel = 1
from urllib.request import urlopen

url = 
"http://www.boursorama.com/includes/cours/last_transactions.phtml?symbole=1xEURUS";
response = urlopen(url)
print(response.headers.as_string())
print(type(response))

Output is:

send: b'GET /includes/cours/last_transactions.phtml?symbole=1xEURUS
HTTP/1.1\r\nAccept-Encoding: identity\r\nHost:
www.boursorama.com\r\nConnection: close\r\nUser-Agent:
Python-urllib/3.2\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server header: Date header: Content-Type header: Connection
header: Cache-Control header: Pragma header: Expires header:
Set-Cookie header: Set-Cookie header: Vary header: Content-Length
header: Content-Language header: X-sid Server: nginx
Date: Tue, 23 Aug 2011 19:08:00 GMT
Content-Type: text/html; charset=ISO-8859-1
Connection: keep-alive
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie: OBJECT_BOURSORAMA=0; expires=Fri, 20-Aug-2021 19:07:59
GMT; path=/; domain=.www.boursorama.com
Set-Cookie: PHPSESSIONID=d6ceed9aab3dba2e61ded126a925a881; path=/;
domain=.www.boursorama.com
Vary: Accept-Encoding,User-Agent
Content-Length: 7787
Content-Language: fr
X-sid: 30,E



Now, if I were to do a data = response.read() and then print(data), I
should get the source code printed (testing this on python.org works,
btw). However, what I do get is:
b''

What gives?

FYI, the httplib2 solution is easy enough:
import httplib2

h = httplib2.Http('.cache') # I prefer to use
folders when working with httplib2
response, content = h.request(url)

Response headers are put in response, the content is accessible as a
byte object in content.
-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zip - password protect

2011-08-23 Thread James Reynolds
On Tue, Aug 23, 2011 at 2:57 PM, Alan Gauld wrote:

> On 23/08/11 19:23, James Reynolds wrote:
>
>> I tried that already.
>>
>> that's only to set the default password to read the zip, not to set a
>> pasword (wording is misleading I think)
>>
>>
> I'm confused.
>
> Which password are you thinking of?
>
> Is it at the OS level to prevent access to the file
>  - in which case the fact that its a zipfile is immaterial.
> Or is it the zipfile's own password? In which case what
> is different from the "default password to read the file"?
>
>
> What exactly are you trying to achieve?
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>

(from earlier)



I need to password protect a zip file I am creating from within python.

I would rather not have a user open the zip and set the password manually.

I'm trying the 7-zip solution, but I can't get past this
error: WindowsError: [Error 2] The system cannot find the file specified

The line it fails at is here: z = subprocess.call(['7z', 'a', '1234', '-y',
name + '.zip'] + self.file_locs)

(from now)

oddly enough, when I open up a python shell, i can run the above just fine
and it creates a .zip file.

But when I run it from eclipse, the exact thing, I get the error.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zip - password protect

2011-08-23 Thread Emile van Sebille

On 8/23/2011 12:31 PM James Reynolds said...

I'm trying the 7-zip solution, but I can't get past this
error: WindowsError: [Error 2] The system cannot find the file specified

The line it fails at is here: z = subprocess.call(['7z', 'a', '1234',
'-y', name + '.zip'] + self.file_locs)

(from now)

oddly enough, when I open up a python shell, i can run the above just
fine and it creates a .zip file.

But when I run it from eclipse, the exact thing, I get the error.


I'd try providing the full path to 7z -- maybe that's why it probably 
can't find the file specified.


Emile


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


Re: [Tutor] issues with urllib and loading a webpage.

2011-08-23 Thread Sander Sweers

On 23/08/11 21:27, Robert Sjoblom wrote:

Here's the code I'm working with:
from http.client import HTTPConnection
HTTPConnection.debuglevel = 1
from urllib.request import urlopen

url = 
"http://www.boursorama.com/includes/cours/last_transactions.phtml?symbole=1xEURUS";
response = urlopen(url)
print(response.headers.as_string())
print(type(response))



Now, if I were to do a data = response.read() and then print(data), I
should get the source code printed (testing this on python.org works,
btw). However, what I do get is:
b''

What gives?


Don't know, works fine for me..

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


Re: [Tutor] Zip - password protect

2011-08-23 Thread James Reynolds
On Tue, Aug 23, 2011 at 3:49 PM, Emile van Sebille  wrote:

> On 8/23/2011 12:31 PM James Reynolds said...
>
>  I'm trying the 7-zip solution, but I can't get past this
>> error: WindowsError: [Error 2] The system cannot find the file specified
>>
>> The line it fails at is here: z = subprocess.call(['7z', 'a', '1234',
>> '-y', name + '.zip'] + self.file_locs)
>>
>> (from now)
>>
>> oddly enough, when I open up a python shell, i can run the above just
>> fine and it creates a .zip file.
>>
>> But when I run it from eclipse, the exact thing, I get the error.
>>
>
> I'd try providing the full path to 7z -- maybe that's why it probably can't
> find the file specified.
>
> Emile
>
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>


It was a silly problem.

I guess Eclipse saves your PATH variables once the program is started,
instead of referencing the os directly. (I just did print "Current path is
%s" % os.path.expandvars("$PATH")) and that told me what was going on.

Anyway, it's saving the files a .7z which isn't doing me much good. i was
under the impression i could save them as .zip, if not I'm just going to
have to password protect manually.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zip - password protect

2011-08-23 Thread James Reynolds
On Tue, Aug 23, 2011 at 4:11 PM, James Reynolds  wrote:

>
>
> On Tue, Aug 23, 2011 at 3:49 PM, Emile van Sebille  wrote:
>
>> On 8/23/2011 12:31 PM James Reynolds said...
>>
>>  I'm trying the 7-zip solution, but I can't get past this
>>> error: WindowsError: [Error 2] The system cannot find the file specified
>>>
>>> The line it fails at is here: z = subprocess.call(['7z', 'a', '1234',
>>> '-y', name + '.zip'] + self.file_locs)
>>>
>>> (from now)
>>>
>>> oddly enough, when I open up a python shell, i can run the above just
>>> fine and it creates a .zip file.
>>>
>>> But when I run it from eclipse, the exact thing, I get the error.
>>>
>>
>> I'd try providing the full path to 7z -- maybe that's why it probably
>> can't find the file specified.
>>
>> Emile
>>
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
>
> It was a silly problem.
>
> I guess Eclipse saves your PATH variables once the program is started,
> instead of referencing the os directly. (I just did print "Current path is
> %s" % os.path.expandvars("$PATH")) and that told me what was going on.
>
> Anyway, it's saving the files a .7z which isn't doing me much good. i was
> under the impression i could save them as .zip, if not I'm just going to
> have to password protect manually.
>
>
got it working, had a flag in the wrong spot. Seems to work better than the
Zip module in some ways.

Thanks for the help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] issues with urllib and loading a webpage.

2011-08-23 Thread Robert Sjoblom
>Don't know, works fine for me..
>
>Greets
>Sander

Are you using Python 3 and urllib, and not using httplib2? Because I
honestly can't get urllib.request.urlopen to work with
http://www.boursorama.com/ -- I only get b'' from there.

best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] issues with urllib and loading a webpage.

2011-08-23 Thread Sander Sweers
On 23 August 2011 22:59, Robert Sjoblom  wrote:
> Are you using Python 3 and urllib, and not using httplib2? Because I
> honestly can't get urllib.request.urlopen to work with
> http://www.boursorama.com/ -- I only get b'' from there.

Yes, Python 3.2 which version one are you using? I tried both with
debug and without.
Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] issues with urllib and loading a webpage

2011-08-23 Thread Robert Sjoblom
>> Are you using Python 3 and urllib, and not using httplib2? Because I
>> honestly can't get urllib.request.urlopen to work with
>> http://www.boursorama.com/ -- I only get b'' from there.
>
> Yes, Python 3.2 which version one are you using? I tried both with
> debug and without.

Python 3.2.1 64 bit on win 32. This is really quite weird. There
should be something there, because the headers alone doesn't account
for the 7kb that's received.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fw: Zip - password protect

2011-08-23 Thread ALAN GAULD
Forwarding to group.

 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/




- Forwarded Message 
From: James Reynolds 
To: Alan Gauld 
Sent: Tuesday, 23 August, 2011 20:11:57
Subject: Re: [Tutor] Zip - password protect




On Tue, Aug 23, 2011 at 2:57 PM, Alan Gauld  wrote:

On 23/08/11 19:23, James Reynolds wrote:
>
>I tried that already.
>>
>>that's only to set the default password to read the zip, not to set a
>>pasword (wording is misleading I think)
>>
>>

I'm confused.
>
>Which password are you thinking of?
>
>Is it at the OS level to prevent access to the file
> - in which case the fact that its a zipfile is immaterial.
>Or is it the zipfile's own password? In which case what
>is different from the "default password to read the file"?
>
>
>What exactly are you trying to achieve?
>
>
>-- 
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>


I need to password protect a zip file I am creating from within python.

I would rather not have a user open the zip and set the password manually.

I'm trying the 7-zip solution, but I can't get past this error: WindowsError: 
[Error 2] The system cannot find the file specified

The line it fails at is here: z = subprocess.call(['7z', 'a', '1234', '-y', 
name 
+ '.zip'] + self.file_locs)___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Speech

2011-08-23 Thread Christopher King
Hello Tutors,
I need help with text to speech and or speech to text. I know of two
packages, but they require win32, which I can't get to work. The Win32
package was filled with pyd's and no py's..Could some one tell me how to get
win32 to work or a package that doesn't use it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Speech

2011-08-23 Thread Steven D'Aprano

Christopher King wrote:

Hello Tutors,
I need help with text to speech and or speech to text. I know of two
packages, but they require win32, which I can't get to work. The Win32
package was filled with pyd's and no py's..Could some one tell me how to get
win32 to work or a package that doesn't use it.


Are you running Windows?

If not, you'll need to use a Windows PC.



--
Steven

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


Re: [Tutor] eval func with floating...

2011-08-23 Thread Christopher King
> if c:
> print *eval("float(%s)"%a)*
> else:
> print "error! please use -defined operators-!"
>
I would use a assert statement for more readability, like so.
*try: assert c*
*except AssertionError: print "error! please use -defined operators-!"*
else: *print *eval("float(%s)"%a)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Speech

2011-08-23 Thread Alex Hall
Depending on what you want, you could try accessible_output. This uses
the first available screen reader to speak, or sapi5 speech if no
screen reader is loaded. I believe the link is:
http://hg.qwitter-client.net/dependencies
HTH.

On 8/23/11, Steven D'Aprano  wrote:
> Christopher King wrote:
>> Hello Tutors,
>> I need help with text to speech and or speech to text. I know of two
>> packages, but they require win32, which I can't get to work. The Win32
>> package was filled with pyd's and no py's..Could some one tell me how to
>> get
>> win32 to work or a package that doesn't use it.
>
> Are you running Windows?
>
> If not, you'll need to use a Windows PC.
>
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eval func with floating...

2011-08-23 Thread Steven D'Aprano

Christopher King wrote:

if c:
print *eval("float(%s)"%a)*
else:
print "error! please use -defined operators-!"


I would use a assert statement for more readability, like so.
*try: assert c*
*except AssertionError: print "error! please use -defined operators-!"*
else: *print *eval("float(%s)"%a)



Only if you want the user to be able to turn off error checking and 
break your program.


"python -O" switches off all assertions.

Do not use asserts except for assertions about internal states of your 
program. Never, ever use them for testing user input, or for reporting 
errors to the user. That is not what they are for. If the user ever sees 
an AssertionError, your code is buggy.




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


Re: [Tutor] Python Speech

2011-08-23 Thread Christopher King
Sorry, forgot to hit reply all.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eval func with floating...

2011-08-23 Thread Christopher King
>
> If the user ever sees an AssertionError, your code is buggy.
>
Well you saw that I caught the AssertionError, so the user
wouldn't technically see it. For the other stuff, I didn't know
the etiquette for assertion
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Speech

2011-08-23 Thread Alex Hall
Sorry, the link is:
http://hg.qwitter-client.net
then select "accessible output".

On 8/23/11, Christopher King  wrote:
> Sorry, forgot to hit reply all.
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eval func with floating...

2011-08-23 Thread Steven D'Aprano

Christopher King wrote:

If the user ever sees an AssertionError, your code is buggy.


Well you saw that I caught the AssertionError, so the user
wouldn't technically see it. For the other stuff, I didn't know
the etiquette for assertion


It's not etiquette, it is the actual way assert works.


If you have code that looks like this:

assert x > 0
print x + 1


and the user runs your code using "python -O", the code is converted to:

print x + 1

and the assert is just *gone*.

It is as simple as that. Any time you use an assert, you should imagine 
that it will just *disappear* some times.


When are assertions useful? If you have ever written code like this:


if x > 0:
print x + 1
else:
# This can never happen!
raise RuntimeError('unexpected internal error')


that's a good case for using an assert. Because you are sure that the 
else can never happen, it won't matter if the assert goes away. But 
because you can't be absolutely 100% sure, it's better to code 
defensively with a check that your program logic is correct.





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