Python equivalent to the "A" or "a" output conversions in C

2012-06-19 Thread Edward C. Jones

Consider the following line in C:
   printf('%a\n', x);
where x is a float or double.  This outputs a hexadecimal representation 
of x.  Can I do this in Python?


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


Re: Python equivalent to the "A" or "a" output conversions in C

2012-06-19 Thread Edward C. Jones

On 06/19/2012 12:41 PM, Hemanth H.M wrote:


>>> float.hex(x)
'0x1.5p+3'

Some days I don't ask the brightest questions.  Suppose x was a numpy 
floating scalar (types numpy.float16, numpy.float32, numpy.float64, or 
numpy.float128).  Is there an easy way to write x in

binary or hex?



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


Using modules from Debian "python3-..." packages with locally compiled Python 3.3

2012-05-08 Thread Edward C. Jones

I use up-to-date Debian testing (wheezy), amd64 architecture.  I downloaded,
compiled and installed Python 3.3.0 alpha 3 (from python.org) using
"altinstall".  Debian wheezy comes with python3.2 (and 2.6 and 2.7).  I
installed the Debian package "python3-bs4" (BeautifulSoup4 for Python3).
Note: Debian uses eggs.

Python3.3a3 cannot find module bs4. Python3.2 can find the module.  Here is
a session with Python 3.3:

> python3.3
Python 3.3.0a3 (default, May  5 2012, 11:30:48)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1012, in _find_and_load
ImportError: No module named 'bs4'
>>>

Here is what I have tried:

1. Put bs4.pth in /usr/local/lib/python3.3/site-packages.  This file should
contain the line:
  /usr/lib/python3/dist-packages
This works.

2. At the start of each Python program using bs4:
 import sys
 sys.path.append('/usr/lib/python3/dist-packages')
 import bs4
This works.

3. In the Python 3.3 source code, do ".configure" then edit Modules/Setup to
contain
SITEPATH=:/usr/lib/python3/dist-packages
Finish compiling and altinstalling python.  "import bs4" works.

The last is the best because I only have to do it once.

Next I tried numpy.  It is installed from Debian package "python3-numpy".
If I compile Python 3.3 _without_ the change in (3) above, I get:

> python3.3
Python 3.3.0a3 (default, May  8 2012, 14:30:18)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1012, in _find_and_load
ImportError: No module named 'numpy'
>>>

which is not a surprise. If I compile and install Python 3.3 _with_ the
change in (3) above, I get:

> python3.3
Python 3.3.0a3 (default, May  8 2012, 14:43:28)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1015, in _find_and_load
  File "", line 634, in load_module
  File "", line 294, in
  module_for_loader_wrapper
  File "", line 522, in _load_module
  File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 137, in

from . import add_newdocs
ImportError: cannot import name add_newdocs
>>>

"add_newdocs.py" is present in the numpy directory.

The "import numpy" works for the Debian python 3.2.

What is the problem?

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


Re: Using modules from Debian "python3-..." packages with locally compiled Python 3.3

2012-05-08 Thread Edward C. Jones

Terry Reedy said:

> Question 1: if you use the .pth method, do you get the same result? 
(I expect you will, but good to

> check.)

Recompiled Pyhton 3.3 without the SITEPATH change.  Same result:

> python3.3
Python 3.3.0a3 (default, May  8 2012, 19:57:45)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1015, in _find_and_load
  File "", line 634, in load_module
  File "", line 294, in 
module_for_loader_wrapper

  File "", line 522, in _load_module
  File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 137, in 


from . import add_newdocs
ImportError: cannot import name add_newdocs
>>>

> Question2: has anyone successfully run numpy with 3.3 on Debian or 
anything else? If no one here

> answers, try Debian or numpy lists.

I don't know.  But Python 3.3.0a3 has been in Debian experimental for 
only a few days.


I have a bunch of code that I want to port to Python 3.  I delayed for 
years because numpy and PIL
had not been ported to Python 3.  Numpy has now been converted and there 
are alternatives to PIL.
Since I want to deal with unicode only once, I would prefer to use 3.3 
but 3.2 would be OK.


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


Hashable object with self references OR how to create a tuple that refers to itself

2012-06-15 Thread Edward C. Jones
I am trying to create a collection of hashable objects, where each 
object contains references to

other objects in the collection.  The references may be circular.

To simplify, one can define
x= list()
x.append(x)
which satisfies x == [x].
Can I create a similar object for tuples which satisfies x == (x,)?

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


Problem with __str__ if baseclass is list

2005-11-12 Thread Edward C. Jones
#! /usr/bin/env python

class A(list):
 def __init__(self, alist, n):
 list.__init__(self, alist)
 self.n = n

 def __str__(self):
 return 'AS(%s, %i)' % (list.__str__(self), self.n)

 def __repr__(self):
 return 'AR(%s, %i)' % (list.__repr__(self), self.n)

a = A(['x', 'y'], 7)

print 1, a
print 2, repr(a)
print 3, list.__str__(a)
print 4, list.__repr__(a)

"""
The output is:

1 AS(AR(['x', 'y'], 7), 7)
2 AR(['x', 'y'], 7)
3 AR(['x', 'y'], 7)
4 ['x', 'y']

Why is list.__str__(a) == "AR(['x', 'y'], 7)"?

Note: The problem goes away if "list.__str__(a)" is replaced with
"list.__repr__(self)".
"""
-- 
http://mail.python.org/mailman/listinfo/python-list


__new__, __init__ and pickle

2005-06-29 Thread Edward C. Jones
Suppose I want to define the class MyClass so I can create an instance by

   MyClass(arg0, arg1, kwarg0=None, kwarg1=0, reuse=None, save=None)

If reuse is not None, it is the name of a pickle file. Unpickle the file 
to get the instance.

If save is not None, it is a file name. Pickle the instance into the 
file as part of creating the instance.

How do I write MyClass? Search Google Groups for the phrase "pickling a 
subclass of tuple" to find a relevant article.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.0

2004-12-14 Thread Edward C. Jones
Chris wrote:
Okay, color me stupid, but what is everyone referencing when they 
mention Python 3.0?  I didn't see any mention of it on the Python site.
http://www.python.org/peps/pep-3000.html
(which happens to be the first hit if you search for "python 3.0" in the
search box on python.org...)

Okay, I feel dumb now. :)
Chris
You are not dumb. Many search phrases are obvious if and only if you 
have already seen them.
--
http://mail.python.org/mailman/listinfo/python-list


Module subprocess: How to "communicate" more than once?

2005-04-01 Thread Edward C. Jones
I have a program named "octave" (a Matlab clone). It runs in a terminal, 
types a prompt and waits for the user to type something. If I try

# Run octave.
oct = subprocess.Popen("octave", stdin=subprocess.PIPE)
# Run an octave called "startup".
oct.communicate("startup")
# Change directory inside octave.
oct.communicate("cd /home/path/to/my/dir")
I get:
Traceback (most recent call last):
  File "./popen.py", line 29, in ?
oct.communicate("cd /home/path/to/my/dir")
  File "/usr/local/lib/python2.4/subprocess.py", line 1044, in communicate
self.stdin.flush()
ValueError: I/O operation on closed file
How do I set up a subprocess so I can send it a command and get the 
answer, then send it another command and get an answer, etc.?
--
http://mail.python.org/mailman/listinfo/python-list


numarray.array can be VERY slow

2005-04-10 Thread Edward C. Jones
#! /usr/bin/env python
"""Should this program take 4.3 seconds to run? I have a Linux PC with
   an AMD Athlon XP 2000+ chip (1.7Gh). I use Python 2.4.1 and numarray
   1.2.3, both compliled from source."""
import time, numarray
from numarray.numerictypes import *
nested = []
for i in range(8):
inner = []
for j in range(8):
inner.append(numarray.ones((256,256), Float64))
nested.append(inner)
t = time.clock()
arr = numarray.array(nested)
print time.clock() - t
--
http://mail.python.org/mailman/listinfo/python-list


Re: numarray.array can be VERY slow

2005-04-10 Thread Edward C. Jones
Steven Bethard wrote:
> As mentioned, this has nothing to do with numarray, and everything to
> do with your inexplicable use of lists.  Why don't you just write this
> as:
>
> arr = numarray.ones((8, 8, 256, 256), Float64)
The code I posted was simplified from a larger program which I have now 
revised. But I still ask: why did it take 4.3 seconds to run?
--
http://mail.python.org/mailman/listinfo/python-list


Does numarray search for blas and lapack during installation?

2005-04-23 Thread Edward C. Jones
I have a PC with Debian sid installed. I install all my Python stuff in 
/usr/local. I just installed numarray 1.3.1. Blaslite and lapacklite 
were compiled. Did the installation process search for blas and lapack?
--
http://mail.python.org/mailman/listinfo/python-list


Lexicographical sort for numarray

2005-04-26 Thread Edward C. Jones
Suppose arr is a two dimensional numarray array. Suppose we do the 
following:

def rowsort(arr):
a = arr.tolist()
a.sort()
return numarray.array(a)
Can this sort be done efficiently in numarray? This function is called 
"rowsort" in MatLab.
--
http://mail.python.org/mailman/listinfo/python-list


Announcement: mrquery finds duplicate images

2005-12-19 Thread Edward C. Jones
I have uploaded "mrquery.05.12.19.tgz"' to my web page
"http://members.tripod.com/~edcjones/pycode.html";.

"mrquery" finds the duplicate images in a collection of
images. It implements a variant of the algorithm in
"Fast Multiresolution Image Querying" by Charles E.
Jacobs, Adam Finkelstein and David H. Salesin. Search
for "mrquery.pdf" at Citeseer or on the Internet. The
mrquery algorithm has been implemented elsewhere.
Especially check out "imgSeek" at
"http://imgseek.python-hosting.com/";.

I order to use this program, you need:
   Familiarity with the standard UNIX commands
   Linux (other UNIX systems may work)
   Python 2.4 or later (2.2 or 2.3 may work)
   Experience programming with Python
   To know how to compile C programs (preferably with gcc)

Warning: this program is new, buggy and command-line
oriented.
-- 
http://mail.python.org/mailman/listinfo/python-list


Widget that displays a directory tree?

2006-01-12 Thread Edward C. Jones
Do any of the Python GUIs have a super-high-level widget that displays a 
directory tree? Most file managers or editors have this type of window.
-- 
http://mail.python.org/mailman/listinfo/python-list


Modifying a variable in a non-global outer scope?

2006-05-19 Thread Edward C. Jones
#! /usr/bin/env python
"""
When I run the following program I get the error message:

UnboundLocalError: local variable 'x' referenced before assignment

Can "inner" change the value of a variable defined in "outer"? Where
is this explained in the docs?
"""
def outer():
 def inner():
 x = x + 1

 x = 3
 inner()
 print x

outer()
-- 
http://mail.python.org/mailman/listinfo/python-list