Float to String "%.7e" - diff between Python-2.6 and Python-2.7

2012-10-30 Thread andrew . mackeith
When formatting a float using the exponential format, the rounding is different 
in Python-2.6 and Python-2.7. See example below.
Is this intentional?

Is there any way of forcing the Python-2.6 behavior (for compatibility reasons 
when testing)?

>c:\python26\python
r:\asiData\abaObjects\lib>c:\python26\python
Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 
>>> [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+02,2.096732160+02]
>>> for a in x:
... print ' %.9e%.7e'%(a,a)
...
 2.096732130e+022.0967321e+02
 2.096732140e+022.0967321e+02
 2.096732150e+022.0967322e+02 <<<<<<<<
 2.096732151e+022.0967322e+02
 4.096732160e+004.0967322e+00
>>> for a in x:
... print '%.9e   %.7e'%(-a,-a)
...
-2.096732130e+02   -2.0967321e+02
-2.096732140e+02   -2.0967321e+02
-2.096732150e+02   -2.0967322e+02 <<<<<<<<
-2.096732151e+02   -2.0967322e+02
-4.096732160e+00   -4.0967322e+00
>>> ^Z

>c:\python27\python
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 
>>> [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+02,2.096732160+02]
>>> for a in x:
... print ' %.9e%.7e'%(a,a)
...
 2.096732130e+022.0967321e+02
 2.096732140e+022.0967321e+02
 2.096732150e+022.0967321e+02 <<<<<<<<
 2.096732151e+022.0967322e+02
 4.096732160e+004.0967322e+00
>>> for a in x:
... print '%.9e   %.7e'%(-a,-a)
...
-2.096732130e+02   -2.0967321e+02
-2.096732140e+02   -2.0967321e+02
-2.096732150e+02   -2.0967321e+02 <<<<<<<<
-2.096732151e+02   -2.0967322e+02
-4.096732160e+00   -4.0967322e+00
>>> ^Z

>
Andrew MacKeith
-- 
http://mail.python.org/mailman/listinfo/python-list


PyBool_FromLong

2005-09-02 Thread Andrew MacKeith
In the C API Docs, the signature of PyBool from long seems to be incorrect.

int PyBool_FromLong(long v)
 Returns Py_True or Py_False depending on the truth value of v. New in 
version 2.3.

The description would suggest:

PyObject* PyBool_FromLong(long v)

-- 
http://mail.python.org/mailman/listinfo/python-list


PyXML and Python-2.6

2009-04-07 Thread Andrew MacKeith
The Python.org "SIG for XML Processing in Python" page indicates that 
"The SIG, through the mailing list and the PyXML project hosted on 
SourceForge...".


The PyXML project on SourceForge " is no longer maintained. ", so 
perhaps the SIG page could be updated.


Is there a guide to porting projects that depend on PyXML to Python-2.6?

Andrew MacKeith
--
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: PyGUI 2.0.1

2009-04-13 Thread Andrew MacKeith


Greg Ewing wrote:

PyGUI 2.0.1 is available:

  http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/

Fixes some problems in setup.py affecting installation
on Linux and Windows.


What is PyGUI?
--

PyGUI is a cross-platform GUI toolkit designed to be lightweight
and have a highly Pythonic API.



I installed PyGUI-2.0.1 on Windows (32 bit, non Administrator access).
I got the following error in 2.0.1 when attempting the BlobEdit example:

D:\users\omh\runabq\pyGUI>c:\python26\python
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> from GUI import Application,  ScrollableView, Document, Window, \
...   FileType, Cursor, rgb
  File "", line 1, in 
  File "c:\python26\Lib\site-packages\GUI\__init__.py", line 78, in __getattr__
traceback.print_stack()
Failed to import 'Application' from Applications
Traceback (most recent call last):
  File "c:\python26\Lib\site-packages\GUI\__init__.py", line 69, in __getattr__
module = __import__(modname, self.__dict__, locals(), [name])
  File "c:\python26\Lib\site-packages\GUI\Win32\Applications.py", line 8, in 

import Components, Windows
  File "c:\python26\Lib\site-packages\GUI\Win32\Components.py", line 12, in 

from Win32 import win_none
  File "c:\python26\lib\site-packages\GUI\Win32\__init__.py", line 2, in 

raise ImportError("This should not be imported.")
ImportError: This should not be imported.

I presume that one should be able to import GUI.Win32 if I need to import 
win_none from it.

If I comment out line 2, I get the following error:
...
  File "c:\python26\Lib\site-packages\GUI\Win32\Components.py", line 12, in 

from Win32 import win_none
ImportError: cannot import name win_none

The directory structure that was installed is as follows:
c:\python26\Lib\site-packages\GUI
c:\python26\Lib\site-packages\GUI\Generic
c:\python26\Lib\site-packages\GUI\Resources
c:\python26\Lib\site-packages\GUI\Resources\cursors
c:\python26\Lib\site-packages\GUI\Win32


Andrew MacKeith
--
http://mail.python.org/mailman/listinfo/python-list


explicit call to __init__(self) in subclass needed?

2009-09-17 Thread Andrew MacKeith

I create a class like this in Python-2.6

>>> class Y(str):
...   def __init__(self, s):
...  pass
...
>>> y = Y('giraffe')
>>> y
'giraffe'
>>>

How does the base class (str) get initialized with the value passed to 
Y.__init__() ?

Is this behavior specific to the str type, or do base classes not need to be 
explicitly initialized?

Andrew
--
http://mail.python.org/mailman/listinfo/python-list


Re: explicit call to __init__(self) in subclass needed?

2009-09-18 Thread Andrew MacKeith



Bruno Desthuilliers wrote:

Andrew MacKeith a écrit :

I create a class like this in Python-2.6

 >>> class Y(str):
...   def __init__(self, s):
...  pass
...
 >>> y = Y('giraffe')
 >>> y
'giraffe'
 >>>

How does the base class (str) get initialized with the value passed to 
Y.__init__() ?


It happens in the __new__ method (which is the proper "constructor")

class Y(str):
def __new__(cls, value, foo="foo"):
instance = str.__new__(cls, value)
instance.foo = foo
return instance

def __repr__(self):
return "<%s(%s, %s)>" % (type(self).__name__, self, self.foo)



Is this behavior specific to the str type,  or do base classes not need
to be explicitly initialized?


When you override a method in a derived class, it's your responsability 
to call on the parent(s) class(es) implementation. __init__ is not an 
exception to that rule. The point is that since __init__ works by 
mutating the newly created instance, it makes no sense to have a 
distinct __init__ for immutable types - which have their "value" set 
once for all at creation time.


Thanks for the explanation, Bruno.

I have been successfully using a class derived from str, and in that
class I add a lot of extra data to the derived class instance in the
__init__ method of the derived class, so it is possible to mutate the
derived class __dict__, even if the base class data remains immutable.


See example below.

The __init__ must have the same arguments as the base class.

>>> class Y(str):
...   def __init__(self):
...  self.color = 'blue'
...
>>> y = Y('Parrot')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __init__() takes exactly 1 argument (2 given)
>>>
>>> class Y(str):
...   def __init__(self, text):
...  self.color = 'blue'
...
>>> y = Y('Parrot')
>>> y
'Parrot'
>>> y.color
'blue'
>>> y[:3]
'Par'
>>> y.size = 'small'
>>>
>>> y.__class__

>>>

But you can get bitten if you do something that returns a new object

>>> y += 'Tail'
>>> y
'ParrotTail'
>>> y.color
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'str' object has no attribute 'color'
>>>
>>> y.__class__

>>>

Andrew
--
http://mail.python.org/mailman/listinfo/python-list