Re: [Tutor] range function and floats?

2011-01-05 Thread Alan Gauld


Joel Knoll mindar...@live.com wrote

I'm trying to write a simple program to give the 
sine of each of a range of numbers, including multiples of pi.  

I keep getting a syntax error, highlighting my use of 2pi 
as an argument in the range, 


range requires integers. You need to scale your floats 
appropriately and convert to integers. Or in your case
use the multipliers of pi as the arguments to range() 
and then multiply by pi when getting the sine.


...this is more about learning how the range function and 
floats work than about writing a super-efficient program.  


Unfortunately they don't work together.

range(0.1,0.5,0.1)  - [0.1,0.2,0.3,0.4]  doesn't work
you need to do:

for n in range(1,5):  use( n/10 )

HTH,


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


Re: [Tutor] range function and floats?

2011-01-05 Thread Steven D'Aprano

Alan Gauld wrote:

...this is more about learning how the range function and floats work 
than about writing a super-efficient program.  


Unfortunately they don't work together.

range(0.1,0.5,0.1)  - [0.1,0.2,0.3,0.4]  doesn't work
you need to do:

for n in range(1,5):  use( n/10 )



There are pitfalls in writing a range() equivalent for floats. Here's 
one way that doesn't work:



def new_range(start, end, step):
# DON'T USE THIS!!!
x = start
while x  end:
yield x
x += step

Here it seems to work:

 list(new_range(5, 10, 0.25))
[5, 5.25, 5.5, 5.75, 6.0, 6.25, 6.5, 6.75, 7.0, 7.25, 7.5,
7.75, 8.0, 8.25, 8.5, 8.75, 9.0, 9.25, 9.5, 9.75]

(Remember that the end argument is excluded!)

But here it fails:


 L = list(new_range(5, 10, 0.1))
 L[-1] == 9.9  # expect the last value to be 9.9
False
 L[-1] == 10.0  # maybe it's 10 then
False

In fact, the last value is a totally unexpected 9.9822. Such 
is the perils of floating point rounding errors.


I've written a recipe for a float range which I hope avoids as many of 
these problems as possible. It isn't possible to avoid *all* rounding 
error when doing floating point calculation, but this should minimize them:


http://code.activestate.com/recipes/577068-floating-point-range/

As a bonus, it allows you to choose whether the start and end points are 
included or excluded.



--
Steven

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


Re: [Tutor] syntax error that i cant spot!

2011-01-05 Thread Steven D'Aprano

Noah Hall wrote:

He has no classes in there. Therefore, there is no place it should be in
this code. Please remember this is Python, and not Java nor anything else.

[...]

It just makes life easier.


Oh the irony... talking about making life easier, who are you talking 
to? What about?


Please quote enough of the previous message to establish context -- when 
you are replying to the message, it is fresh in your mind. When others 
read your reply (possibly days later like I'm doing now), the context is 
anything but clear and your message comes across as merely mysterious 
and obscure.



Thank you.




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


Re: [Tutor] syntax error that i cant spot!

2011-01-05 Thread Steven D'Aprano

Noah Hall wrote:

Please quote enough of the previous message to establish context -- when
you are replying to the message, it is fresh in your mind. When others read
your reply (possibly days later like I'm doing now), the context is anything
but clear and your message comes across  as merely mysterious and obscure.



Certainly. In fact, it was an error in that message - I had deleted
accidentally that which I had quoted. Please be aware, however, rudeness
does nothing for you.



Please and Thank you are rude? Oh my, have you lived a sheltered life :)


--
Steven

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


Re: [Tutor] syntax error that i cant spot!

2011-01-05 Thread Noah Hall

 Please and Thank you are rude? Oh my, have you lived a sheltered life
 :)


Nej, it was your condescension that I found rude. Also, you did it again,
perhaps on purpose though.. ;)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] range function and floats?

2011-01-05 Thread Wayne Werner
On Wed, Jan 5, 2011 at 5:14 AM, Steven D'Aprano st...@pearwood.info wrote:

 Alan Gauld wrote:

  ...this is more about learning how the range function and floats work than
 about writing a super-efficient program.


 Unfortunately they don't work together.

 range(0.1,0.5,0.1)  - [0.1,0.2,0.3,0.4]  doesn't work
 you need to do:

 for n in range(1,5):  use( n/10 )



 There are pitfalls in writing a range() equivalent for floats. Here's one
 way that doesn't work:


 def new_range(start, end, step):
# DON'T USE THIS!!!
x = start
while x  end:
yield x
x += step

 Here it seems to work:

  list(new_range(5, 10, 0.25))
 [5, 5.25, 5.5, 5.75, 6.0, 6.25, 6.5, 6.75, 7.0, 7.25, 7.5,
 7.75, 8.0, 8.25, 8.5, 8.75, 9.0, 9.25, 9.5, 9.75]

 (Remember that the end argument is excluded!)

 But here it fails:


  L = list(new_range(5, 10, 0.1))
  L[-1] == 9.9  # expect the last value to be 9.9
 False
  L[-1] == 10.0  # maybe it's 10 then
 False

 In fact, the last value is a totally unexpected 9.9822. Such is
 the perils of floating point rounding errors.

 I've written a recipe for a float range which I hope avoids as many of
 these problems as possible. It isn't possible to avoid *all* rounding error
 when doing floating point calculation, but this should minimize them:

 http://code.activestate.com/recipes/577068-floating-point-range/

 As a bonus, it allows you to choose whether the start and end points are
 included or excluded.


As an alternative to floating point, you can use the Decimal module:

import decimal

def new_range(start, stop, step):
x = decimal.Decimal(str(start))
step = decimal.Decimal(str(step))
while x  stop:
yield x
x += step

x = list(new_range(5, 10, 0.1))
x[-1] == decimal.Decimal(str(9.9))
#True
float(x[-1]) == 9.9
#True

The decimal module allows you to get rid of those pesky floating point
errors. See http://docs.python.org/library/decimal.html for more info. On a
related note, if you're interested in working with rational numbers (1/23,
3/4, etc.) there is also a fraction module
http://docs.python.org/library/fractions.html.


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


Re: [Tutor] range function and floats?

2011-01-05 Thread Steven D'Aprano

Wayne Werner wrote:


The decimal module allows you to get rid of those pesky floating point
errors. See http://docs.python.org/library/decimal.html for more info. 


That's a myth. Decimal suffers from the same floating point issues as 
binary floats. It's quite easy to demonstrate the same sort of rounding 
errors with Decimal as for float:


 from decimal import Decimal as D
 x = D(1)/D(3)
 3*x == 1
False

Between 1 and 1000 inclusive, there are 354 such numbers:

 nums = []
 for i in range(1, 1001):
... x = D(1)/D(i)
... if x*i != 1: nums.append(i)
...
 len(nums)
354



The problem isn't just division and multiplication, nor does it just 
affect fractional numbers:


 x = D(10)**30
 x + 100 == x
True



Here's another nice little demonstration of the problem:

 def f(a):
... b = 9*a - 9
... c = a - 1/D(9)*b
... return c  # c should always equal 1 exactly
...
 f(D(51.1)) == 1
False




Decimal and float share more things in common than differences. Both are 
floating point numbers. Both have a fixed precision (although you can 
configure what that precision is for Decimal, but not float). Both use a 
finite number of bits, and therefore have a finite resolution. The only 
differences are:


* as mentioned, you can configure Decimal to use more bits and higher 
precision, at the cost of speed and memory;

* floats use base 2 and Decimal uses base 10;
* floats are done in hardware and so are much faster than Decimal;



So it simply isn't correct to suggest that Decimal doesn't suffer from 
rounding error.




--
Steven

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


Re: [Tutor] cx_Oracle help

2011-01-05 Thread F1r3f1y

This guide helped me a lot with cx_Oracle
http://codingtutorials.co.uk/blog/?p=31


Greg Lindstrom-3 wrote:
 
 Hello,
 
 I'm trying to help out a friend and am stumped.  Can you help me out?
 Thanks,
 --greg
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  I will briefly explain the problem I am facing.
  I am using Oracle 9.2, Python 2.5 and I installed cx_Oracle-
 4.3.1-win32-9i-py25 in Python.
 
  From python I tried following :
   import cx_Oracle
   myDsn = cx_Oracle.makedsn('ISCN47',1521,'AUBDBS01')
   CONN = cx_Oracle.connect(myusr, mypwd, myDsn)
 Traceback (most recent call last):
 File pyshell#4, line 1, in module
conn = cx_Oracle.Connection('scott','tiger',myDsn)
RuntimeError: Unable to acquire Oracle environment
 handle
 
  I have set the below environment variables too
   NLS_LANG:  snip.WE8MSWIN1252
   ORACLE_HOME:D:\Tools\oracle\ora92
   ORACLE_SID:   AUBDBS01
   PYTHON_HOME:d:\Utility\Python25
   PYTHONPATH:
 %PYTHON_HOME%\lib;%PYTHON_HOME%\DLLs;%PYTHON_HOME%\Lib\site-packages;%ORACLE_HOME%\bin
   LD_LIBRARY_PATH:   %LD_LIBRARY_PATH%;D:\Tools\oracle\ora92\lib
 
  Not getting any idea where I am wrong?
 
 Regards,
 
 Kishore
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 

-- 
View this message in context: 
http://old.nabble.com/cx_Oracle-help-tp15293236p30596546.html
Sent from the Python - tutor mailing list archive at Nabble.com.

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


Re: [Tutor] range function and floats?

2011-01-05 Thread Wayne Werner
On Wed, Jan 5, 2011 at 10:43 AM, Steven D'Aprano st...@pearwood.infowrote:

 Wayne Werner wrote:

  The decimal module allows you to get rid of those pesky floating point
 errors. See http://docs.python.org/library/decimal.html for more info.


 That's a myth. Decimal suffers from the same floating point issues as
 binary floats. It's quite easy to demonstrate the same sort of rounding
 errors with Decimal as for float:

  from decimal import Decimal as D
  x = D(1)/D(3)
  3*x == 1
 False


I should have clarified - when I say pesky floating point errors I mean
errors in precision that you naturally would not expect.

1/3 == .333 (repeating forever(?)). But on a computer, you're limited to a
specific precision point (sort of), and the moment you truncate
.333(repeating) to *any* finite points of precision you no longer have the
result of the mathematical operation 1/3. Yes, 1/3*3 == 1, but the error in
the Decimal module is *only* in division. It might be useful to define a
repeating flag in the Decimal module for the results of such operations as
1/3, which would get rid of the error in truncation. But this is a
fundamentally different error from the standard floating point errors.

In [106]: 0.9
Out[106]: 0.90002

These are two different numbers, mathematically. If I say x = 0.9, I
naturally assume that 0.9 is the value in x, not 0.90002. Of
course it's all in the implementation of floating points, and the fact that
Python evaluates 0.9 in a floating point context which results in the stored
value, but that ignores the fact that *naturally* one does not expect this.
And anyone who's been through 2nd or 3rd grade or whenever they teach about
equality would expect that this would evaluate to False.

In [112]: 0.90002 == 0.9
Out[112]: True

You don't get such silliness with the Decimal module:

In [125]: D('0.90002') == D('0.9')
Out[125]: False


 Between 1 and 1000 inclusive, there are 354 such numbers:

  nums = []
  for i in range(1, 1001):
 ... x = D(1)/D(i)
 ... if x*i != 1: nums.append(i)
 ...
  len(nums)
 354



 The problem isn't just division and multiplication, nor does it just affect
 fractional numbers:

  x = D(10)**30
  x + 100 == x
 True


Decimal DOES get rid of floating point errors, except in the case of
repeating (or prohibitively large precision values)

In [127]: x = D(10)**30

In [128]: x
Out[128]: Decimal('1.000E+30')

In [129]: x + 100
Out[129]: Decimal('1.000E+30')

If you reset the precision to an incredibly large number:
decimal.getcontext().prec = 1000

In [131]: x = D(10)**30

In [132]: x
Out[132]: Decimal('100')

In [133]: x + 100 == x
Out[133]: False

Voila, the error has vanished!



 So it simply isn't correct to suggest that Decimal doesn't suffer from
 rounding error.


I never said rounding errors - I said pesky floating point errors. When
performing the operation 1/3, I naturally expect that my computer won't hold
each of the 3's after the decimal point, and I don't categorize that as
pesky - that's just good sense if you know a little about computers. I also
expect that .333 * 3 would give me the number .999, and only .999, not
.99911 or some other wonky value. Of course it's interesting to note
that Python handles the precision properly when dealing with strings, but
not with the floating points themselves (at least on this particular trial):

In [141]: .333 * 3
Out[141]: 0.99911

In [142]: str(.333*3)
Out[142]: '0.999'

In [143]: .333 * 3 == .999
Out[143]: False

In [144]: str(.333*3) == str(.999)
Out[144]: True


Decimal and float share more things in common than differences. Both are
 floating point numbers. Both have a fixed precision (although you can
 configure what that precision is for Decimal, but not float). Both use a
 finite number of bits, and therefore have a finite resolution. The only
 differences are:

 * as mentioned, you can configure Decimal to use more bits and higher
 precision, at the cost of speed and memory;
 * floats use base 2 and Decimal uses base 10;
 * floats are done in hardware and so are much faster than Decimal;


Precisely. It's not a magic bullet (1/3 != . mathematically, after
all!), *but* it eliminates the errors that you wouldn't normally expect when
working with standard points of precision, such as the expectation that
0.333 * 3 resulting in .999, not .999011.

Hopefully a little more precise,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] user input help

2011-01-05 Thread Jason Staudenmayer
Hi all, I'm pretty new to programming in general and figured I'd try out 
python. 
I'm working on a small program to add users to a sqlite db. The problem I'm 
having it dealing with the user input, I'd like to be able to repeat the 
function to get the input if the user doesn't accept it.

here's the code I have now:

def promptInput():
 Get employee data from user running this program

lname = raw_input(Please enter employees last name\n)
fname = raw_input(Please enter employees first name\n)
email = raw_input(Please enter employee email address (or press enter to \
leave blank)\n)
result = (lname, fname, email)
return result

def getEmplyInfo():
# get the data from input
result = promptInput()
# print the data so the user can check and verify spelling
print Is the following info correct [y/n]\n%s, %s %s % (result[1], \
result[0], result[2])
check = raw_input()
#see if the user needs to make corrections to the data he entered
if check == y:
print this check is done so we can add user
print %s, %s %s % (result[1], result[0], result[2])
else:
check = 
promptInput()

The if else loop is were I'm loosing it. If I select n it will ask for the 
input 
again but only once. If on the second time around I enter n to re-do it just 
exits.



Thanks

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


Re: [Tutor] user input help

2011-01-05 Thread Alex Hall
On 1/5/11, Jason Staudenmayer jas...@adventureaquarium.com wrote:
 Hi all, I'm pretty new to programming in general and figured I'd try out
 python.
 I'm working on a small program to add users to a sqlite db. The problem I'm
 having it dealing with the user input, I'd like to be able to repeat the
 function to get the input if the user doesn't accept it.

 here's the code I have now:

 def promptInput():
  Get employee data from user running this program

 lname = raw_input(Please enter employees last name\n)
 fname = raw_input(Please enter employees first name\n)
 email = raw_input(Please enter employee email address (or press enter
 to \
 leave blank)\n)
 result = (lname, fname, email)
 return result

 def getEmplyInfo():
 # get the data from input
 result = promptInput()
 # print the data so the user can check and verify spelling
 print Is the following info correct [y/n]\n%s, %s %s % (result[1], \
 result[0], result[2])
 check = raw_input()
 #see if the user needs to make corrections to the data he entered
 if check == y:
 print this check is done so we can add user
 print %s, %s %s % (result[1], result[0], result[2])
 else:
 check = 
 promptInput()

 The if else loop is were I'm loosing it. If I select n it will ask for the
 input
 again but only once. If on the second time around I enter n to re-do it just
 exits.
This is because the function is done once it detects the y or n, so
after you enter the n, one of those if/else statements has fired, and
the function has nothing else to do. You will want a while loop,
something like:

repeat=True
while repeat:
 answer=raw_input(Is the data okay?)
 if answer==y: repeat=False
  else:
   promptInput()
   repeat=True

Anyway, something along those lines. Look in the manual for while
loops. Basically, they are a way to repeat an action until a condition
is met. You will also run across for loops, which are mostly used for
repeating an event a set number of times. You can use them
interchangeably, but they each have situations where one works better
than the other, and you want a while loop here.



 Thanks

 Jason



 ..·º
 ___
 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


[Tutor] wx accelerator table: one keystroke seems skipped

2011-01-05 Thread Alex Hall
Hello all,
First, this is about a wx accelerator table, so if it is too off-topic
for this list, let me know.

I have a table with 23 entries, all of which work. I added another
entry last night, and it does not work. The odd thing, though, is that
I do not get an error of any kind anywhere in the program, the
keystroke simply does not call the function to which it should be
bound. I even changed the function name to something that should have
thrown an exception, but nothing at all happens. The keystroke in
question is ctrl+m and is tied to ctrlM_id and the cancelMove()
function. Below I have pasted my entire setHotkeys() function. Again,
everything works except ctrl+m, but even that keystroke does not cause
problems, it is like it is not even there.

def setHotkeys():
 upArrow_id=wx.NewId()
 downArrow_id=wx.NewId()
 leftArrow_id=wx.NewId()
 rightArrow_id=wx.NewId()
 space_id=wx.NewId()
 a_id=wx.NewId()
 f_id=wx.NewId()
 m_id=wx.NewId()
 r_id=wx.NewId()
 ctrlM_id=wx.NewId()
 ctrlN_id=wx.NewId()
 one_id=wx.NewId()
 two_id=wx.NewId()
 three_id=wx.NewId()
 four_id=wx.NewId()
 five_id=wx.NewId()
 six_id=wx.NewId()
 seven_id=wx.NewId()
 f1_id=wx.NewId()
 f2_id=wx.NewId()
 f3_id=wx.NewId()
 f4_id=wx.NewId()
 hotkeyList=[
  (wx.ACCEL_NORMAL, wx.WXK_UP, upArrow_id), #up one face-up card in
the current stack
  (wx.ACCEL_NORMAL, wx.WXK_DOWN, downArrow_id), #down one face-up card
  (wx.ACCEL_NORMAL, wx.WXK_LEFT, leftArrow_id), #left a stack
  (wx.ACCEL_NORMAL, wx.WXK_RIGHT, rightArrow_id), #right a stack
  (wx.ACCEL_NORMAL, ord('a'), a_id), #try to move last card to Ace stack
  (wx.ACCEL_NORMAL, ord('1'), one_id), #jump to stack 1
  (wx.ACCEL_NORMAL, ord('2'), two_id), #jump to stack 2
  (wx.ACCEL_NORMAL, ord('3'), three_id), #jump to stack 3
  (wx.ACCEL_NORMAL, ord('4'), four_id), #jump to stack 4
  (wx.ACCEL_NORMAL, ord('5'), five_id), #jump to stack 5
  (wx.ACCEL_NORMAL, ord('6'), six_id), #jump to stack 6
  (wx.ACCEL_NORMAL, ord('7'), seven_id), #jump to stack 7
  (wx.ACCEL_NORMAL, ord('f'), f_id), #flip up a new card
  (wx.ACCEL_NORMAL, ord('r'), r_id), #read the current deck card in play
  (wx.ACCEL_CTRL, ord('m'), ctrlM_id), #cancel Move mode
  (wx.ACCEL_CTRL, ord('n'), ctrlN_id), #start a new hand
  (wx.ACCEL_NORMAL, ord('m'), m_id), #mark stack to be moved
  (wx.ACCEL_NORMAL, wx.WXK_SPACE, space_id), #move deck's facing card
to activeStack
  #now f1-f4, for reviewing the four Ace stacks
  (wx.ACCEL_NORMAL, wx.WXK_F1, f1_id),
  (wx.ACCEL_NORMAL, wx.WXK_F2, f2_id),
  (wx.ACCEL_NORMAL, wx.WXK_F3, f3_id),
  (wx.ACCEL_NORMAL, wx.WXK_F4, f4_id)]
 #now bind the keys to their functions, using lambda to pass args
 f.Bind(wx.EVT_MENU, lambda evt, dir=1: exploreStack(evt, dir), id=upArrow_id)
 f.Bind(wx.EVT_MENU, lambda evt, dir=-1: exploreStack(evt, dir),
id=downArrow_id)
 f.Bind(wx.EVT_MENU, lambda evt, dir=-1: move(evt, dir), id=leftArrow_id)
 f.Bind(wx.EVT_MENU, lambda evt, dir=1: move(evt, dir), id=rightArrow_id)
 f.Bind(wx.EVT_MENU, lambda evt: useDeckCard(evt), id=space_id)
 f.Bind(wx.EVT_MENU, lambda evt: toAcePile(evt), id=a_id)
 f.Bind(wx.EVT_MENU, lambda evt: deal(evt), id=f_id)
 f.Bind(wx.EVT_MENU, lambda evt: readDeckCard(evt), id=r_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=1, jump=True: move(evt,
target, jump), id=one_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=2, jump=True: move(evt,
target, jump), id=two_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=3, jump=True: move(evt,
target, jump), id=three_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=4, jump=True: move(evt,
target, jump), id=four_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=5, jump=True: move(evt,
target, jump), id=five_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=6, jump=True: move(evt,
target, jump), id=six_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=7, jump=True: move(evt,
target, jump), id=seven_id)
 f.Bind(wx.EVT_MENU, lambda evt, sel=activeStack: selectStack(evt,
sel), id=m_id)
 f.Bind(wx.EVT_MENU, lambda evt: cancelMove(evt), id=ctrlM_id)
 f.Bind(wx.EVT_MENU, lambda evt: promptForNewGame(evt), id=ctrlN_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=0: reviewAceStack(evt,
target), id=f1_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=1: reviewAceStack(evt,
target), id=f2_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=2: reviewAceStack(evt,
target), id=f3_id)
 f.Bind(wx.EVT_MENU, lambda evt, target=3: reviewAceStack(evt,
target), id=f4_id)
 hotkeys=wx.AcceleratorTable(hotkeyList)
 f.SetAcceleratorTable(hotkeys)

-- 
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] range function and floats?

2011-01-05 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Wayne Werner wrote:

On Wed, Jan 5, 2011 at 10:43 AM, Steven D'Apranost...@pearwood.infowrote:


Wayne Werner wrote:

  The decimal module allows you to get rid of those pesky floating point

errors. See http://docs.python.org/library/decimal.html for more info.



That's a myth. Decimal suffers from the same floating point issues as
binary floats. It's quite easy to demonstrate the same sort of rounding
errors with Decimal as for float:


from decimal import Decimal as D
x = D(1)/D(3)
3*x == 1

False



I should have clarified - when I say pesky floating point errors I mean
errors in precision that you naturally would not expect.


I'd expect them.  Apparently you would not.


1/3 == .333 (repeating forever(?)). But on a computer, you're limited to a
specific precision point (sort of), and the moment you truncate
.333(repeating) to *any* finite points of precision you no longer have the
result of the mathematical operation 1/3. Yes, 1/3*3 == 1, but the error in
the Decimal module is *only* in division. It might be useful to define a
repeating flag in the Decimal module for the results of such operations as
1/3, which would get rid of the error in truncation.


Interesting flag.  So how would it indicate that the decimal version of 
the number 762/477 repeats every 476 digits?


 But this is a

fundamentally different error from the standard floating point errors.

In [106]: 0.9
Out[106]: 0.90002



These errors are in the conversion between string and floating point, 
and back again.  The number 0.9 has a repeating bit pattern in binary 
floating point, so it cannot be represented exactly.  That's not an 
error, it's a direct consequence of the decision to use binary floating 
point.  And that decision was made decades ago, and is independent of 
Python.  When I learned Fortran in the 60's, one of the things the book 
(McCracken) emphasized was not to compare real numbers with equality, 
but to use the form abs(x-y)delta.




These are two different numbers, mathematically. If I say x = 0.9, I
naturally assume that 0.9 is the value in x, not 0.90002. Of
course it's all in the implementation of floating points, and the fact that
Python evaluates 0.9 in a floating point context which results in the stored
value, but that ignores the fact that *naturally* one does not expect this.
And anyone who's been through 2nd or 3rd grade or whenever they teach about
equality would expect that this would evaluate to False.


Lots of stuff they teach in 3rd grade is oversimplified for the real world.


In [112]: 0.90002 == 0.9
Out[112]: True

You don't get such silliness with the Decimal module:

In [125]: D('0.90002') == D('0.9')
Out[125]: False




The conversion between string and float is logically a divide, where the 
denominator is a power of 10.  So decimal floating point doesn't have 
any quantization errors for that conversion.


Even if you use the decimal package, you need to understand these 
things, or you'll make the kind of error made a few days ago, where 
binary floating point values were passed into a decimal constructor. 
That won't fix problems that were already there.  It just may mask 
them for certain cases.



Between 1 and 1000 inclusive, there are 354 such numbers:


nums = []
for i in range(1, 1001):

... x = D(1)/D(i)
... if x*i != 1: nums.append(i)
...

len(nums)

354



The problem isn't just division and multiplication, nor does it just affect
fractional numbers:


x = D(10)**30
x + 100 == x

True



Decimal DOES get rid of floating point errors, except in the case of
repeating (or prohibitively large precision values)

In [127]: x = D(10)**30

In [128]: x
Out[128]: Decimal('1.000E+30')

In [129]: x + 100
Out[129]: Decimal('1.000E+30')

If you reset the precision to an incredibly large number:
decimal.getcontext().prec = 1000

In [131]: x = D(10)**30

In [132]: x
Out[132]: Decimal('100')

In [133]: x + 100 == x
Out[133]: False

Voila, the error has vanished!




So it simply isn't correct to suggest that Decimal doesn't suffer from
rounding error.



I never said rounding errors - I said pesky floating point errors. When
performing the operation 1/3, I naturally expect that my computer won't hold
each of the 3's after the decimal point, and I don't categorize that as
pesky - that's just good sense if you know a little about computers. I also
expect that .333 * 3 would give me the number .999, and only .999, not
.99911 or some other wonky value. Of course it's interesting to note
that Python handles the precision properly when dealing with strings, but
not with the floating points themselves (at least on this particular trial):

In [141]: .333 * 3
Out[141]: 0.99911

In [142]: str(.333*3)
Out[142]: '0.999'

In [143]: .333 * 3 == .999
Out[143]: False

In [144]: str(.333*3) == str(.999)
Out[144]: True


Decimal and 

Re: [Tutor] range function and floats?

2011-01-05 Thread Steven D'Aprano

Wayne Werner wrote:

On Wed, Jan 5, 2011 at 10:43 AM, Steven D'Aprano st...@pearwood.infowrote:


Wayne Werner wrote:

 The decimal module allows you to get rid of those pesky floating point

errors. See http://docs.python.org/library/decimal.html for more info.


That's a myth. Decimal suffers from the same floating point issues as
binary floats. It's quite easy to demonstrate the same sort of rounding
errors with Decimal as for float:


from decimal import Decimal as D
x = D(1)/D(3)
3*x == 1

False



I should have clarified - when I say pesky floating point errors I mean
errors in precision that you naturally would not expect.


Say what?

But that's the whole point -- you *must* expect them, because Decimal 
floating point numbers suffer the *exact* same issues with rounding and 
finite precision as binary floats. It may affect different specific 
numbers, but the issues are exactly the same. Just because Decimal uses 
base 10 doesn't make it immune to the same issues of rounding and 
precision as base 2 floats.




1/3 == .333 (repeating forever(?)). But on a computer, you're limited to a
specific precision point (sort of), and the moment you truncate
.333(repeating) to *any* finite points of precision you no longer have the
result of the mathematical operation 1/3. Yes, 1/3*3 == 1, but the error in
the Decimal module is *only* in division. It might be useful to define a


I'm surprised you can make such an obviously wrong claim -- you even 
discuss errors in addition further down. How can you do that and still 
say that errors only occur with division???


Decimal includes a significant amount of code to detect when the result 
of some operation is inexact, and to signal the user when it occurs. 
This does *not* only happen in division:


 import decimal
 decimal.getcontext().traps[decimal.Inexact] = 1
 a = decimal.Decimal('1e-30')
 b = decimal.Decimal('11.1')
 a+b
Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/local/lib/python3.1/decimal.py, line 1178, in __add__
ans = ans._fix(context)
  File /usr/local/lib/python3.1/decimal.py, line 1653, in _fix
context._raise_error(Inexact)
  File /usr/local/lib/python3.1/decimal.py, line 3812, in _raise_error
raise error(explanation)
decimal.Inexact: None




repeating flag in the Decimal module for the results of such operations as
1/3, which would get rid of the error in truncation. But this is a


You haven't thought that through very well. How do you deal with square 
roots and other fractional powers without truncation errors?


What happens when you operate on two numbers with this repeating flag -- 
is the Decimal module supposed to include a full-blown algebra system to 
generate exact answers to such things as this?


x = 1/Decimal(3)  # 0.333 repeating
y = 1/Decimal(27)  # 0.037037 repeating
x - y/10**9  # 0.3296296296 repeating

How do you deal with numbers like pi or e without truncation?

No, a repeating flag would just add unnecessary complexity for no real 
benefit. If you want to track fractions exactly, use the fractions module.




fundamentally different error from the standard floating point errors.


But they aren't fundamentally different! That's the point! They are the 
same errors. They effect different numbers, because float is base 2 and 
Decimal is base 10, but they have the same fundamental cause.




In [106]: 0.9
Out[106]: 0.90002

These are two different numbers, mathematically. If I say x = 0.9, I
naturally assume that 0.9 is the value in x, not 0.90002. Of


Yes, and the exact same issue can occur in Decimal. It just affects 
different numbers. 0.9 is exact in base 10, but can't be represented 
exactly in base 2. Fortunately, the opposite is not the case: any finite 
number of binary bits is equivalent to a finite number of decimal 
digits, so unless you overflow the number of digits, you won't get an 
equivalent error for binary-decimal conversion. But there are numbers 
which can't be represented exactly in base 10 at any finite precision. 
1/3 is one of them, so is 5/17 and an infinite number of other examples.


You can even find examples of numbers which are exact in base 2 but 
inexact in Decimal at whatever finite precision you choose.


 2**120
1329227995784915872903807060280344576
 int( 1/( 1/Decimal(2**120) ) )
13292279957849158729038070600


Choose more digits for Decimal, and 2**120 can be stored exactly, but 
other, larger, numbers can't.




course it's all in the implementation of floating points, and the fact that
Python evaluates 0.9 in a floating point context which results in the stored
value, but that ignores the fact that *naturally* one does not expect this.
And anyone who's been through 2nd or 3rd grade or whenever they teach about
equality would expect that this would evaluate to False.


Floating point numbers are not the same as the real numbers you learn 
about in school. It doesn't matter whether you use binary 

Re: [Tutor] user input help

2011-01-05 Thread Alan Gauld


Jason Staudenmayer jas...@adventureaquarium.com wrote


I'm working on a small program to add users to a sqlite db.


Try the database topic in my tutorial where I do a very similar 
thing...



code follows -

def promptInput():
   ...
   result = (lname, fname, email)
   return result

def getEmplyInfo():
   # get the data from input
   result = promptInput()
   # print the data so the user can check and verify spelling
   print Is the following info correct [y/n]\n%s, %s %s % 
(result[1], \

   result[0], result[2])
   check = raw_input()
   #see if the user needs to make corrections to the data he entered
   if check == y:
   print this check is done so we can add user
   print %s, %s %s % (result[1], result[0], result[2])
   else:
   check = 
   promptInput()

---



The if else loop is were I'm loosing it.


if else is not a loop it is a branch.
See the What is Profgramming topic in my tutorial for a description
of the different structures. Then see the loops and branching topics
for loop and branch structures. You need a loop. The topics give 
examples.


HTH,


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


Re: [Tutor] wx accelerator table: one keystroke seems skipped

2011-01-05 Thread Octavian Rasnita

From: Alex Hall mehg...@gmail.com

Hello all,
First, this is about a wx accelerator table, so if it is too off-topic
for this list, let me know.

I have a table with 23 entries, all of which work. I added another
entry last night, and it does not work. The odd thing, though, is that
I do not get an error of any kind anywhere in the program, the
keystroke simply does not call the function to which it should be
bound. I even changed the function name to something that should have
thrown an exception, but nothing at all happens. The keystroke in
question is ctrl+m and is tied to ctrlM_id and the cancelMove()


Control+M is equivalent with a enter key so this might be the problem.

Octavian

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


Re: [Tutor] cx_Oracle help

2011-01-05 Thread Christian Witts

On 05/01/2011 16:33, F1r3f1y wrote:

This guide helped me a lot with cx_Oracle
http://codingtutorials.co.uk/blog/?p=31


Greg Lindstrom-3 wrote:
   

Hello,

I'm trying to help out a friend and am stumped.  Can you help me out?
Thanks,
--greg

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  I will briefly explain the problem I am facing.
  I am using Oracle 9.2, Python 2.5 and I installed cx_Oracle-
4.3.1-win32-9i-py25 in Python.

  From python I tried following :
import cx_Oracle
myDsn = cx_Oracle.makedsn('ISCN47',1521,'AUBDBS01')
CONN = cx_Oracle.connect(myusr, mypwd, myDsn)
 Traceback (most recent call last):
 File pyshell#4, line 1, inmodule
conn = cx_Oracle.Connection('scott','tiger',myDsn)
RuntimeError: Unable to acquire Oracle environment
handle

  I have set the below environment variables too
   NLS_LANG:snip.WE8MSWIN1252
   ORACLE_HOME:D:\Tools\oracle\ora92
   ORACLE_SID:   AUBDBS01
   PYTHON_HOME:d:\Utility\Python25
   PYTHONPATH:
%PYTHON_HOME%\lib;%PYTHON_HOME%\DLLs;%PYTHON_HOME%\Lib\site-packages;%ORACLE_HOME%\bin
   LD_LIBRARY_PATH:   %LD_LIBRARY_PATH%;D:\Tools\oracle\ora92\lib

  Not getting any idea where I am wrong?

Regards,

Kishore

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


 
   


You need to install the Runtime, 3rd product option in the Oracle 
Universal Installer.


--
Kind Regards,
Christian Witts


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