Re: Refactoring similar subclasses

2010-09-11 Thread Peter Otten
Steven D'Aprano wrote:

> I have some code that currently takes four different classes, A, B, C and
> D, and subclasses each of them in the same way:
> 
> class MyA(A):
> def method(self, x):
> result = super(MyA, self).method(x)
> if result == "spam":
> return "spam spam spam"
> return result
> # many more methods overloaded
> 
> 
> class MyB(B):
> def method(self, x):
> result = super(MyB, self).method(x)
> if result == "spam":
> return "spam spam spam"
> return result
> # many more methods overloaded
> 
> 
> and so on, for MyC and MyD. There's a lot of duplicated code in there.
> What techniques do you suggest for reducing the code duplication? I
> thought about some variation of:
> 
> names = "MyA MyB MyC MyD".split()
> bases = [A, B, C, D]
> d = dict-of-overloaded-methods
> for name, base in zip(names, bases):
> globals()[name] = type(name, [base], d)
> 
> 
> but I'm not sure that this is a good approach, or how to inject the right
> arguments to super in the dict.
> 
> Any suggestions or guidelines?

You could use a mixin:

class Mixin(object):
def method(self, x):
result = super(Mixin, self).method(x)
if result == "spam":
return "spam spam spam"
return result

# ...

for name, base in zip(names, bases):
globals()[name] = type(name, (Mixin, base), {})

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


Re: Hide DOS console for .pyc file

2010-09-11 Thread Lawrence D'Oliveiro
In message 
<[email protected]>, Muddy 
Coder wrote:

> For a quick testing purpose, I deliver .pyc files to my customer. I
> don't want the black DOS console appearing behind my GUI, but I have
> no idea how to do it. Somebody can help? Thanks!

Don’t run it on Windows.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> On Fri, Sep 10, 2010 at 7:19 PM, Lawrence D'Oliveiro
>  wrote:
>
>> In message , Jakson
>> A. Aquino wrote:
>>
>>> On Fri, Sep 10, 2010 at 6:26 AM, Lawrence D'Oliveiro
>>>  wrote:
>>
 In message ,
 Jakson A. Aquino wrote:

> I would like to send code from Vim [1] to R [2] on Microsoft Windows.

 Why such a roundabout way? Why not just run R in a subprocess and feed
 it a script to run?
>>>
>>> Emacs with ESS runs R in a subprocess (at least I think it does). Vim
>>> can't do that.
>>
>> Why not?
> 
> I don't know how to embed R into Vim ...

I wasn’t saying “embed”, I was saying “run R in a subprocess”.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> Your code do send the ^v as expected, but I have problem with the
> selection of the Windows which will receive the ^v. The code above was OK
> in a Windows XP running inside VirtualBox, but when tested in a real
> machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
> into R, but more frequently it is pasted into Vim itself or nowhere.

No big surprise your roundabout GUI-fiddling approach is flaky.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hide DOS console for .pyc file

2010-09-11 Thread Steven D'Aprano
On Fri, 10 Sep 2010 20:28:52 -0700, Muddy Coder wrote:

> Hi Folks,
> 
> For a quick testing purpose, I deliver .pyc files to my customer. I
> don't want the black DOS console appearing behind my GUI, but I have no
> idea how to do it. Somebody can help? Thanks!

Google is your friend.

Googling for "Python dos window" brings up this as the third hit:

http://www.velocityreviews.com/forums/t344026-how-to-run-python-in-
windows-w-o-popping-a-dos-box.html



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


Re: Refactoring similar subclasses

2010-09-11 Thread Steven D'Aprano
On Sat, 11 Sep 2010 08:53:38 +0200, Peter Otten wrote:

> Steven D'Aprano wrote:
> 
>> I have some code that currently takes four different classes, A, B, C
>> and D, and subclasses each of them in the same way:
[...]
>> Any suggestions or guidelines?
> 
> You could use a mixin:

Nice! I'll give it a try. I knew that sooner or later I'd find a reason 
for mixins :)


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


Re: Hide DOS console for .pyc file

2010-09-11 Thread Dave Angel

 On 2:59 PM, Muddy Coder wrote:

Hi Folks,

For a quick testing purpose, I deliver .pyc files to my customer. I
don't want the black DOS console appearing behind my GUI, but I have
no idea how to do it. Somebody can help? Thanks!


Cosmo

In Windows, the executable file statically determines whether a console 
is created.  There are two executables shipped with Python,  python.exe 
and pythonw.exe.  You want to use the latter.


The real question is how to cause Windows to run that rather than 
python.exe.  Answer depends on how your use is launching his program.


If he's starting from a shortcut, change the name of the executable on 
the shortcut.  If he's starting by double-clicking on the script name, 
change the extension of the script from .py to .pyw


You mention that you're shipping only .pyc files.  If that's the case, 
add one more, a .pyw that imports your main script.  Of course, that may 
mean changing that script a little so it works as a module.


DaveA

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


Re: SendKeys and Python 2.7

2010-09-11 Thread Jakson A. Aquino
On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
 wrote:
> In message , Jakson A.
> Aquino wrote:
>
>> Your code do send the ^v as expected, but I have problem with the
>> selection of the Windows which will receive the ^v. The code above was OK
>> in a Windows XP running inside VirtualBox, but when tested in a real
>> machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
>> into R, but more frequently it is pasted into Vim itself or nowhere.
>
> No big surprise your roundabout GUI-fiddling approach is flaky.

Thanks for your comment, but could you please suggest a feasible and
non flaky approach?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hide DOS console for .pyc file

2010-09-11 Thread Peter Otten
Lawrence D'Oliveiro wrote:

> In message
> <[email protected]>, Muddy
> Coder wrote:
> 
>> For a quick testing purpose, I deliver .pyc files to my customer. I
>> don't want the black DOS console appearing behind my GUI, but I have
>> no idea how to do it. Somebody can help? Thanks!
> 
> Don’t run it on Windows.

If you switch the OS for every minor problem you'll run out of operating 
systems pretty soon...

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


Re: SendKeys and Python 2.7

2010-09-11 Thread Jakson A. Aquino
On Fri, Sep 10, 2010 at 10:38 PM, MRAB  wrote:
> I'd add some more small sleeps to give Windows/R time to act, IYSWIM. I
> learned that from experience. :-)

I've tried adding sleeps, small and large, but they didn't make much
of a difference. Anyway, different machines could require different
amounts of sleep and, thus, this workaround may not be generalizable
enough since the plugin would be used by many people on different
systems.

Thanks,

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


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
>  wrote:
>
>> In message , Jakson
>> A. Aquino wrote:
>>
>>> Your code do send the ^v as expected, but I have problem with the
>>> selection of the Windows which will receive the ^v. The code above was
>>> OK in a Windows XP running inside VirtualBox, but when tested in a real
>>> machine, it proved to be highly inconsistent. Sometimes the ^v gets
>>> pasted into R, but more frequently it is pasted into Vim itself or
>>> nowhere.
>>
>> No big surprise your roundabout GUI-fiddling approach is flaky.
> 
> Thanks for your comment, but could you please suggest a feasible and
> non flaky approach?

I already did.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hide DOS console for .pyc file

2010-09-11 Thread Lawrence D'Oliveiro
In message , Peter Otten wrote:

> Lawrence D'Oliveiro wrote:
> 
>> In message
>> <[email protected]>, Muddy
>> Coder wrote:
>> 
>>> For a quick testing purpose, I deliver .pyc files to my customer. I
>>> don't want the black DOS console appearing behind my GUI, but I have
>>> no idea how to do it. Somebody can help? Thanks!
>> 
>> Don’t run it on Windows.
> 
> If you switch the OS for every minor problem you'll run out of operating
> systems pretty soon...

Not if you choose a suitably flexible and portable one to begin with.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 3D cube navigation

2010-09-11 Thread rav
On Sep 10, 9:10 pm, sahilsk  wrote:
> hi, i need to make a 3d cube as a navigation menu.. each face having
> separate  button .. or effect.
> any idea,  how can i make one such 3D figures with functionality of
> mouse events?

One of options is AS3 and Papervision3D - free Flex SDK and many
tutorials are available in the web.
-- 
http://mail.python.org/mailman/listinfo/python-list


Design: Module interface vs. library interface

2010-09-11 Thread Stefan Schwarzer
Hello,

I maintain ftputil [1], an FTP client library.

Users of the library may use it like this:

| import ftputil
|
| with ftputil.FTPHost(server, userid, password) as ftp_host:
| # for example, make a remote file and read its content
| with ftp_host.open("remote_name", "rb") as remote_fobj:
| data = remote_fobj.read()

Even though "ftp files" are defined in module `ftp_file` [2]
via class `FTPFile`, users of the ftputil library should
never use `FTPFile` directly; it's up to `FTPHost.open` to
generate these file objects.

Now, from the "point of view" of the _module_ `ftp_file`,
I'd use

| # ftp_file.py
|
| # used outside this module
| __all__ = ['FTPFile']
|
| class FTPFile(object):
| ...

| # ftputil.py
|
| import ftp_file
|
| class FTPHost(object):
| ...
| def open(name, mode):
| ...
| return ftp_file.FTPFile(...)

On the other hand, from the point of view of the _library_
ftputil, `FTPFile` isn't part of the official API, so I may
as well write

| # ftp_file.py
|
| # be explicit, no user-public interface
| __all__ = []
|
| class FTPFile(object):
| ...

Which approach would you prefer and why? Or some other
approach? Would you use a different approach if the
library-internal class was named `_FTPFile` instead of
`FTPFile`?

[1] http://ftputil.sschwarzer.net
[2] http://ftputil.sschwarzer.net/trac/browser/ftp_file.py

Stefan

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


Static typing, Python, D, DbC

2010-09-11 Thread Bearophile
I write some Python code almost every day, but lately I am using a lot
the D language too. After using D for about three years I now know
some of it, but when I need to write short (< about 1000 lines)
*correct* programs, Python is still the more productive for me.

Static typing, and the usage of transitive const of D are a strange
thing. It seems obvious that they lead to safer code, but on the other
hand experimentally I have seen that my short Python programs are less
buggy than equivalent D ones.

Static typing looks safer, but D offers many low level features, and
generally it contains many more traps or ways to shoot your own foot,
that they more than compensate for the "lack of safety" coming from
Python dynamic typing, even when you don't use those low level
features.

Maybe for large (> about 100_000 lines) programs D may come out to be
less bug-prone than Python (thanks to the better data hiding and
static typing), but I am not sure of this at all...

Static typing also has a significant costs. When you write D2 code
often something doesn't work because of some transitive const or
immutable (or just because of the large number of bugs that need to be
fixed still in the D compiler). So here you pay some cost as debugging
time (or time to avoid those problems). And there is a mental cost
too, because you need to keep part of your attention on those const-
related things instead of your algorithms, etc.



Lately while I program with Python one of the D features that I most
miss is a built-in Design By Contract (see PEP 316), because it avoids
(or helps me to quickly find and fix) many bugs. In my opinion DbC is
also very good used with doctests.

You may implement a poor's man DbC in Python like this:

class Foo:
def _invariant(self):
assert ...
assert ...
return True

def bar(self, ...):
assert self._invariant()
...
res = ...
assert self._invariant()
return res

But this missed several useful features of DbC.


>From the D standard library, I have also appreciated a lazy string
split (something like a str.xplit()). In some situations it reduces
memory waste and increases code performance.



I have installed this, on a Windows Vista OS:
http://www.python.org/ftp/python/2.7/python-2.7.msi

But I have had two problems, the 'random' module was empty, and it
didn't import the new division from the future, so I've had to remove
it and reinstall 2.6.6. Is this just a problem of mine?

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How Python works: What do you know about support for negative indices?

2010-09-11 Thread Aahz
In article <[email protected]>,
Ben Finney   wrote:
>Ben Finney  writes:
>> Raymond Hettinger  writes:
>>>
>>> It doesn't seem to be common knowledge when and how a[x] gets
>>> translated to a[x+len(x)]. So, here's a short info post on how
>>> Python supports negative indices for sequences.
>>
>> Thanks for this. Could you post your messages using a channel that
>> doesn't arbitrarily split your paragraphs into long-short-long-short
>> lines? It makes paragraphs burdensome to read, and I skipped most of
>> the message because of that.
>
>For those who think the problem may be with the recipient's software, I
>see the same annoying line-wrapping problems in the archived message
>http://mail.python.org/pipermail/python-list/2010-September/1255167.html>.

Still looks like *your* problem to me; except for exactly one paragraph,
I don't see comb-style formatting in Lynx at that URL.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

[on old computer technologies and programmers]  "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As."  --Andrew Dalke
-- 
http://mail.python.org/mailman/listinfo/python-list


bug in python documentation?

2010-09-11 Thread Vito 'ZeD' De Tullio
from http://docs.python.org/library/unittest.html

-->8>8>8>8>8>8>8>8>8>8>8>8--

Here is a short script to test three functions from the random module:

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.seq = range(10)

def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))

# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1,2,3))

def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)

def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)

if __name__ == '__main__':
unittest.main()

--8<8<8<8<8<8<8<8<8<8<8<8<--


but test_sample() it's a strange method: what's that "with 
self.assertRaises(ValueErrorr)"?

infact, running the script I have

$ python test_unittest.py
.E.
==
ERROR: test_sample (__main__.TestSequenceFunctions)
--
Traceback (most recent call last):
  File "./test_data_manip.py", line 23, in test_sample
with self.assertRaises(ValueError):
TypeError: failUnlessRaises() takes at least 3 arguments (2 given)

--
Ran 3 tests in 0.001s

FAILED (errors=1)
$


-- 
By ZeD

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


Re: bug in python documentation?

2010-09-11 Thread Benjamin Kaplan
On Sat, Sep 11, 2010 at 11:34 AM, Vito 'ZeD' De Tullio
 wrote:
> from http://docs.python.org/library/unittest.html
>
> -->8>8>8>8>8>8>8>8>8>8>8>8--
>
> Here is a short script to test three functions from the random module:
>
> import random
> import unittest
>
> class TestSequenceFunctions(unittest.TestCase):
>
>    def setUp(self):
>        self.seq = range(10)
>
>    def test_shuffle(self):
>        # make sure the shuffled sequence does not lose any elements
>        random.shuffle(self.seq)
>        self.seq.sort()
>        self.assertEqual(self.seq, range(10))
>
>        # should raise an exception for an immutable sequence
>        self.assertRaises(TypeError, random.shuffle, (1,2,3))
>
>    def test_choice(self):
>        element = random.choice(self.seq)
>        self.assertTrue(element in self.seq)
>
>    def test_sample(self):
>        with self.assertRaises(ValueError):
>            random.sample(self.seq, 20)
>        for element in random.sample(self.seq, 5):
>            self.assertTrue(element in self.seq)
>
> if __name__ == '__main__':
>    unittest.main()
>
> --8<8<8<8<8<8<8<8<8<8<8<8<--
>
>
> but test_sample() it's a strange method: what's that "with
> self.assertRaises(ValueErrorr)"?
>
> infact, running the script I have
>
> $ python test_unittest.py
> .E.
> ==
> ERROR: test_sample (__main__.TestSequenceFunctions)
> --
> Traceback (most recent call last):
>  File "./test_data_manip.py", line 23, in test_sample
>    with self.assertRaises(ValueError):
> TypeError: failUnlessRaises() takes at least 3 arguments (2 given)
>
> --
> Ran 3 tests in 0.001s
>
> FAILED (errors=1)
> $
>
>
> --
> By ZeD


You're looking at the 2.7 documentation. Are you using 2.7?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bug in python documentation?

2010-09-11 Thread Thomas Jollans
On Saturday 11 September 2010, it occurred to Vito 'ZeD' De Tullio to exclaim:
> from http://docs.python.org/library/unittest.html
> 
> $ python test_unittest.py
> .E.
> ==
> ERROR: test_sample (__main__.TestSequenceFunctions)
> --
> Traceback (most recent call last):
>   File "./test_data_manip.py", line 23, in test_sample
> with self.assertRaises(ValueError):
> TypeError: failUnlessRaises() takes at least 3 arguments (2 given)
> 
> --
> Ran 3 tests in 0.001s
> 
> FAILED (errors=1)
> $

Which Python version are you using?
To quote the docs you linked:
http://docs.python.org/library/unittest.html#unittest.TestCase.assertRaises

""" 
Changed in version 2.7: Added the ability to use assertRaises() as a context 
manager.
"""

Are you using Python 2.7 (or 3.x)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bug in python documentation?

2010-09-11 Thread Vito 'ZeD' De Tullio
Benjamin Kaplan wrote:

> You're looking at the 2.7 documentation. Are you using 2.7?

whoops, no: 2.6.5 :\

(but the "new in python X.Y.Z" disclaimer does not apply to the example 
snippets?)

-- 
By ZeD

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


Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-11 Thread Thomas Jollans
On Saturday 11 September 2010, it occurred to Lawrence D'Oliveiro to exclaim:
> In message , MRAB
> 
> wrote:
> > On 08/09/2010 19:07, Georg Brandl wrote:
> >> Thus spake the Lord: Thou shalt indent with four spaces. No more, no
> >> less. Four shall be the number of spaces thou shalt indent, and the
> >> number of thy indenting shall be four. Eight shalt thou not indent,
> >> nor either indent thou two, excepting that thou then proceed to four.
> >> Tabs are right out.
> > 
> > FYI, that should be "thine indenting".
> > 
> > "My/thy" before a consonant, "mine/thine" before a vowel. Compare with
> > "a/an", which we still do.
> 
> The funny thing is, that’s technically “Modern English”...

... and it's regularly used in a number of best-selling books.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static typing, Python, D, DbC

2010-09-11 Thread Lie Ryan
On 09/12/10 00:33, Bearophile wrote:

> 
> 
> Lately while I program with Python one of the D features that I most
> miss is a built-in Design By Contract (see PEP 316), because it avoids
> (or helps me to quickly find and fix) many bugs. In my opinion DbC is
> also very good used with doctests.

> You may implement a poor's man DbC in Python like this:

I would do it like this:

from DbC import DbC
class Foo(__metaclass__=DbC):
def __invariant(self):
... automatically asserted for all methods ...
def __precond(self):
... automatically asserted for all methods ...
def __postcond(self):
... automatically asserted for all methods ...

@precond(attr=value) # asserts self.attr==value
@postcond(func) # a function for more complex assertions
def bar(self, ...):
... clean, uncluttered code ...

and set DbC.uninstall() to uninstall all precond/postcond/invariants at
runtime without any additional overhead. These are all definitely
possible with metaclasses and decorators.

> From the D standard library, I have also appreciated a lazy string
> split (something like a str.xplit()). In some situations it reduces
> memory waste and increases code performance.

Care to open an issue at the tracker? Considering that many Python 3
builtins is now lazy, there might be a chance this is an oversight, or
there might be a reason why string.split is not lazy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: triangle python user's group?

2010-09-11 Thread fields
Tim:
trizpug.org

- fields

On Aug 31, 12:38 pm, "Tim Arnold"  wrote:
> "Albert Hopkins"  wrote in message
>
> news:[email protected]...
>
> > On Mon, 2010-08-30 at 12:38 -0700, Tim Arnold wrote:
> >> Hi,
> >> Is there a python users group in the Research Triangle Park area
> >> (North Carolina, USA)?
>
> > Google "triangle python user's group"
>
> 
> thanks for the pointer
> --Tim

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


Re: Hide DOS console for .pyc file

2010-09-11 Thread Terry Reedy

On 9/11/2010 6:32 AM, Lawrence D'Oliveiro wrote:

In message, Peter Otten wrote:


Lawrence D'Oliveiro wrote:


In message
<[email protected]>, Muddy
Coder wrote:


For a quick testing purpose, I deliver .pyc files to my customer. I
don't want the black DOS console appearing behind my GUI, but I have
no idea how to do it. Somebody can help? Thanks!


Have them run with pythonw.exe instead of python.exe. That is how IDLE 
does it.


--
Terry Jan Reedy

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


fast kdtree tree implementation for python 3?

2010-09-11 Thread _wolf
does anyone have a suggestion for a ready-to-go, fast kdtree
implementation for python 3.1 and up, for nearest-neighbor searches? i
used to use the one from numpy/scipy, but find it a pain to install
for python 3. also, i'm trying to wrap the code from 
http://code.google.com/p/kdtree/
using cython, but i'm still getting errors.

i wish stuff like kdtree, levenshtein edit distance and similar things
were available in the standard library.
-- 
http://mail.python.org/mailman/listinfo/python-list


is there a library/program that converts sqlite database from windows-1252 to utf-8 ?

2010-09-11 Thread Stef Mientki
 thanks,
Stef Mientki
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is there a library/program that converts sqlite database from windows-1252 to utf-8 ?

2010-09-11 Thread Robert Kern
SQLite internally stores its strings as UTF-8 or UTF-16 encoded Unicode. So it's 
not clear what you mean when you say the database is "windows-1252". Can you be 
more specific?


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: fast kdtree tree implementation for python 3?

2010-09-11 Thread Stefan Behnel

_wolf, 11.09.2010 20:15:

does anyone have a suggestion for a ready-to-go, fast kdtree
implementation for python 3.1 and up, for nearest-neighbor searches? i
used to use the one from numpy/scipy, but find it a pain to install
for python 3.


The latest release is supposed to work with Py3.



also, i'm trying to wrap the code from http://code.google.com/p/kdtree/
using cython, but i'm still getting errors.


If you subscribe to the cython-users mailing list, you can ask for help there.



i wish stuff like kdtree, levenshtein edit distance and similar things
were available in the standard library.


Since you're looking for an implementation, I guess you won't be the one 
volunteering to maintain such code in the stdlib, would you?


Stefan

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


Re: fast kdtree tree implementation for python 3?

2010-09-11 Thread Marco Nawijn
On 11 sep, 20:15, _wolf  wrote:
> does anyone have a suggestion for a ready-to-go, fast kdtree
> implementation for python 3.1 and up, for nearest-neighbor searches? i
> used to use the one from numpy/scipy, but find it a pain to install
> for python 3. also, i'm trying to wrap the code 
> fromhttp://code.google.com/p/kdtree/
> using cython, but i'm still getting errors.
>
> i wish stuff like kdtree, levenshtein edit distance and similar things
> were available in the standard library.

Do you know about the kdtree implementation in biopython? I don't know
if it is already available for Python 3, but for me it worked fine in
Python 2.X.

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


SOLVED: Re: Trap Authentication Errors in HTTP Request

2010-09-11 Thread naugiedoggie
On Sep 10, 12:09 pm, naugiedoggie  wrote:
> Hello,
>
> I have a script that authenticates to a web service provider to
> retrieve data.  This script provides an authentication header built in
> a very basic way like this:

The answer is that there is something whacked in the Windoze
implementation for urllib2.

It turns out that the script works fine when run in a linux console.
'401' error is trapped as expected by an exception handler.  In
Winblows, the builtin handler for authentication is supposed to take a
dump after 5 retries, but this seems to not happen.  The retries
continue until a recursion exception is fired.  At this point the
script dumps back to the console.  An exception handler for Exception
will catch this.

Thanks.

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


Bug: urllib2 basic authentication does not trap auth failure in windows?

2010-09-11 Thread naugiedoggie
Hello,

Is this a known issue?

I have a script that authenticates to a web service.  urllib2 process
for authenticating for basic auth is supposed to dump out after 5
retries.

Here's a note from the urllib2.py code:


def http_error_auth_reqed(self, auth_header, host, req, headers):
authreq = headers.get(auth_header, None)
if self.retried > 5:
# Don't fail endlessly - if we failed once, we'll probably
# fail a second time. Hm. Unless the Password Manager is
# prompting for the information. Crap. This isn't great
# but it's better than the current 'repeat until recursion
# depth exceeded' approach 
raise HTTPError(req.get_full_url(), 401, "digest auth
failed",
headers, None)


This note is from the digest handler but the basic handler is exactly
the same in the important respects.

What happens in a windows console is that, in fact, the code dumps
with the message 'maximum recursion depth reached.'

Whereas, in the linux console, the same script exits appropriately
with the trapped 401 error that authentication failed.

Thanks.

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


play sound on Ubuntu 10.4. (pulse audio?)

2010-09-11 Thread News123
Hi,

>From a python script I'd like to play sound on Ubuntu 10.4


I made two attempts:

the first one is drectly writing to '/dev/audio', which works, but only
if no other application plays audio (like for example amarok)

The second attempt was playing audio via ALSA.
The application doesn't fail, but I can hear nothing

Now I don't know how to continue


More details below


1.) write directly to /dev/audio


def Beep_dev_au(frequency, duration, amplitude=10):
sample = 8000
half_period = int(sample/frequency/2)
beep = chr(amplitude)*half_period+chr(0)*half_period
beep *= int(duration*frequency/1000)
audio = file('/dev/audio', 'wb')
audio.write(beep)
audio.close()


the function works well and beeps if no other application is playing audio.

When running it while for example amarok is playing audio, it will fail
with:

IOError: [Errno 16] Device or resource busy: '/dev/audio'



2. Accessing audio via ALSA
=
import alsaaudio
#
def Beep_alsa(frequency, duration=1000, amplitude=1):
sample = 44100
pcm = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK)
pcm.setchannels(1)
pcm.setrate(sample)
pcm.setformat(alsaaudio.PCM_FORMAT_S16_LE)
half_period = int(sample/frequency/2)
beep = (chr(0)+chr(amplitude))*half_period+(chr(0)*2)*half_period
print "L",len(beep)
beep *= int(duration*sample/half_period/2000)

pcm.setperiodsize(160)
for idx in xrange(0,len(beep),320):
frm = beep[idx:idx+320]
if len(frm) == 320:
pcm.write(frm)


this function doesn't raise an exception, but  hear nothing.

I'm a little lost.

As far as I understand Ubuntu uses the pulse audio system by default.
(That's probably why ALSA is not working)



What would be the correct way to play audio on such a system or even better.

What would be the way to detect how to play audio on a linux system
without knowing ufront whether the system uses pulse, ALSA or whatever?


Thanks for your suggestions.


N



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


Re: How to Convert IO Stream to XML Document

2010-09-11 Thread naugiedoggie
On Sep 10, 12:20 pm, jakecjacobson  wrote:
> I am trying to build a Python script that reads a Sitemap file and
> push the URLs to a Google Search Appliance.  I am able to fetch the
> XML document and parse it with regular expressions but I want to move
> to using native XML tools to do this.  The problem I am getting is if
> I use urllib.urlopen(url) I can convert the IO Stream to a XML
> document but if I use urllib2.urlopen and then read the response, I
> get the content but when I use minidom.parse() I get a "IOError:
> [Errno 2] No such file or directory:" error

Hello,

This may not be helpful, but I note that you are doing two different
things with your requests, and judging from the documentation,  the
objects returned by urllib and urllib2 openers do not appear to be the
same.  I don't know why you are calling urllib.urlopen(url) and
urllib2.urlopen(request), but I can tell you that I have used urllib2
opener to retrieve a web services document in XML and then parse it
with minidom.parse().


>
> THIS WORKS but will have issues if the IO Stream is a compressed file
> def GetPageGuts(net, url):
>         pageguts = urllib.urlopen(url)
>         xmldoc = minidom.parse(pageguts)
>         return xmldoc
>
> # THIS DOESN'T WORK, but I don't understand why
> def GetPageGuts(net, url):
>         request=getRequest_obj(net, url)
>         response = urllib2.urlopen(request)
>         response.headers.items()
>         pageguts = response.read()

Did you note the documentation says:

"One caveat: the read() method, if the size argument is omitted or
negative, may not read until the end of the data stream; there is no
good way to determine that the entire stream from a socket has been
read in the general case."

No EOF marker might be the cause of the parsing problem.

Thanks.

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


Re: palindrome iteration

2010-09-11 Thread Aahz
In article ,
Dave Angel   wrote:
>
>def is_palindrom(s):
>s = s.lower()
>return s == s[::-1]

To deal with "real" palindromes such as, "Madam, I'm Adam," you should
probably strip all spaces and punctuation:

# untested
pat = re.compile(r'[a-z]')
def is_palindrome(s):
letters = pat.findall(s.lower())
return letters == reversed(letters)
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

[on old computer technologies and programmers]  "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As."  --Andrew Dalke
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fast kdtree tree implementation for python 3?

2010-09-11 Thread _wolf
> Since you're looking for an implementation, I guess you won't be the one
> volunteering to maintain such code in the stdlib, would you?

this is indeed a problem. i am probably not the right one for this
kind of task.

however, i do sometimes feel like the standard library carries too
much cruft from yesteryear. things like decent image and sound
manipulation, fuzzy string comparison, fast asynchronous HTTP serving
and requesting are definitely things i believe a 2010 programming
language with batteries included should strive to provide.

one avenue to realize this goal could be to prioritize the packages in
pypi. pypi is basically a very good idea and has made things like
finding and installing packages much easier. however, it is also
organized like a dump pile. there are centuries old packages there few
people ever use.

i suggest to add aging (many old packages are good ones, but also
often display a crude form of inner organization; conversely, a
library not updated for a long time is unlikely to be a good answer to
your problem; aging works in both directions), popularity, and
community prioritization (where people vote for essential and relevant
solutions) to the standard library as well as to pypi; in other words,
to unify the two. batteries included is a very good idea, but there
are definitely some old and leaky batteries in there. sadly, since the
standard library modules are always included in each installation,
there are no figures on how much needed they are after all. one would
guess that were such figures available, the aifc library would come
near the end of a ranked listing.

if the community manages, by download figures and voting, to class
packages, a much clearer picture could emerge about the importance of
packages.

one could put python packages into:

* Class A all those packages without which python would not run (such
as sys and site); into

* Class B ('basics'), officially maintained packages; into

* Class C ('community'), packages that are deemed important or
desirable and which are open for community contributions (to make it
likely they get updated soon enough whenever needed); into

* Class D ('debut') all packages submitted to pypi and favorably
tested, reviewed and found relevant by a certain number of people;
into

* Class E ('entry') all packages submitted or found elsewhere on the
web, but not approved by the community; into

* Class F ('failure') all packages that were proposed but never
produced code, and all packages known to be not a good ideas to use
(see discussion going on at http://pypi.python.org/pypi/python-cjson).
Class F can help people to avoid going down the wrong path when
choosing software.

well this goes far beyond the kdtree question. maybe i'll make it a
proposal for a PEP.


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


Re: play sound on Ubuntu 10.4. (pulse audio?)

2010-09-11 Thread Tim Harig
On 2010-09-11, News123  wrote:
> What would be the way to detect how to play audio on a linux system
> without knowing ufront whether the system uses pulse, ALSA or whatever?

I would suggest libao:
http://www.xiph.org/ao/
and it's python bindings:
http://nixbit.com/cat/multimedia/audio/libao/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is there a library/program that converts sqlite database from windows-1252 to utf-8 ?

2010-09-11 Thread Stef Mientki
 On 11-09-2010 21:11, Robert Kern wrote:
> SQLite internally stores its strings as UTF-8 or UTF-16 encoded Unicode. So 
> it's not clear what
> you mean when you say the database is "windows-1252". Can you be more 
> specific?
I doubt that, but I'm not sure ...
For some databases written by other programs and
written with Python, with
cursor = self.conn.cursor ()
self.conn.text_factory = str

Can only be read back with with text_factory = str
then the resulting string columns contains normal strings with windows 1252 
coding, like character 0xC3

Reading these databases with text_factory = unicode,
results in exceptions, when such a string with 0xC3 is encountered.

As I want to switch completely to unicode,
to prevent these kind of problems in the future.

cheers,
Stef

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


Re: How Python works: What do you know about support for negative indices?

2010-09-11 Thread Neil Hodgson
Ben Finney:

> For those who think the problem may be with the recipient's software, I
> see the same annoying line-wrapping problems in the archived message
> http://mail.python.org/pipermail/python-list/2010-September/1255167.html>.

   That looks well-formatted to me and just the same as I see in a news
reader. There appear to be deliberate wraps at sentence end or automatic
wraps to fit <80 columns. Which lines are wrong and why are they wrong?

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


Re: is there a library/program that converts sqlite database from windows-1252 to utf-8 ?

2010-09-11 Thread Robert Kern

On 9/11/10 4:45 PM, Stef Mientki wrote:

  On 11-09-2010 21:11, Robert Kern wrote:

SQLite internally stores its strings as UTF-8 or UTF-16 encoded Unicode. So 
it's not clear what
you mean when you say the database is "windows-1252". Can you be more specific?

I doubt that, but I'm not sure ...


From the documentation, it looks like SQLite does not attempt to validate the 
input as UTF-8 encoded, so it is possible that someone pushed in raw bytes. See 
"Support for UTF-8 and UTF-16" in the following page:


  http://www.sqlite.org/version3.html


For some databases written by other programs and
written with Python, with
 cursor = self.conn.cursor ()
 self.conn.text_factory = str

Can only be read back with with text_factory = str
then the resulting string columns contains normal strings with windows 1252 
coding, like character 0xC3


You can probably use

  self.conn.text_factory = lambda x: x.decode('windows-1252')

to read the data, though I've never tried to use that API myself.

You will need to write a program yourself that opens one connection to your 
existing database for reading and another connection to another database (using 
the defaults) for writing. Then iterate over your tables and copy data from one 
database to the other.


You may also be able to simply dump the database to a text file using "sqlite3 
bad-database.db .dump > bad-sql.sql", read the text file into Python as a 
string, decode it from windows-1252 to unicode and then encode it as utf-8 and 
write it back out. Then use "sqlite3 good-database.db .read good-sql.sql" to 
create the new database. I've never tried such a thing, so it may not work.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: How Python works: What do you know about support for negative indices?

2010-09-11 Thread Cameron Simpson
On 10Sep2010 12:46, Daniel Fetchinson  wrote:
| > Raymond Hettinger  writes:
| >> It doesn't seem to be common knowledge when and how a[x] gets
| >> translated to a[x+len(x)].  So, here's a short info post on how Python
| >> supports negative indices for sequences.
| >
| > Thanks for this. Could you post your messages using a channel that
| > doesn't arbitrarily split your paragraphs into long-short-long-short
| > lines?
| 
| It came across fine for me as well (gmail with basic html interface).
| 
| > It makes paragraphs burdensome to read, and I skipped most of the
| > message because of that.
| 
| You might want to switch to a client where you do not have this problem.

It was fine for me too, using mutt (fixed width text interface, 80
column terminal).
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

A monkey has the right to copy what he sees other monkeys doing.
Shouldn't humans have equivalent rights?
- Alien Being 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fast kdtree tree implementation for python 3?

2010-09-11 Thread _wolf
> Do you know about the kdtree implementation in biopython? I don't know
> if it is already available for Python 3, but for me it worked fine in
> Python 2.X.

i heard they use a brute-force approach and it's slow. that's just
rumors alright. also, judging from the classes list on
http://www.biopython.org/DIST/docs/api/module-tree.html, you will see
you can probably tune in to the latest radio moscow news using it. way
too much for my needs, i just want to find the nearest neighbor on a
2D-plane. but thanks for the suggestion.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static typing, Python, D, DbC

2010-09-11 Thread John Nagle

On 9/11/2010 9:36 AM, Lie Ryan wrote:

On 09/12/10 00:33, Bearophile wrote:




Lately while I program with Python one of the D features that I most
miss is a built-in Design By Contract (see PEP 316), because it avoids
(or helps me to quickly find and fix) many bugs. In my opinion DbC is
also very good used with doctests.



You may implement a poor's man DbC in Python like this:


I would do it like this:


   Design by contract really isn't a good fit to Python.  I've
done proof of correctness work, and there are suitable languages
for it.  It needs a language where global static analysis is
possible, so you can reliably tell what can changes what.

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


Re: audio time-stretching?

2010-09-11 Thread Patrick Charron
On Tue, 07 Sep 2010 15:33:55 +, kj wrote:

> Does anyone know of a Python module for *moderate* "time-stretching"[1]
> an MP3 (or AIFF) file?
> 
> FWIW, the audio I want to time-stretch is human speech.


If you are running your script on Linux you may use gstreamer(pyGST).
With gstreamer you can use filter to modify audio and video file or 
stream. 

P.S. Maybe gstreamer can work on windows to
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The Samurai Principle

2010-09-11 Thread Lawrence D'Oliveiro
In message , Robert 
Kern wrote:

> On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:
>
>> In message, Ian
>> Kelly wrote:
>>
>>> And returning None on failure is dangerous, because if the programmer
>>> does not take care to handle that case, the program may attempt to
>>> regard it as actual data.
>>
>> But None *is* actual data.
> 
> And that is exactly the reason why the Samurai Principle says to not
> return None when the function fails to do what it intended to do.

How can the function “fail” when it returns what it is specified to return?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How Python works: What do you know about support for negative indices?

2010-09-11 Thread Ben Finney
Neil Hodgson  writes:

> There appear to be deliberate wraps at sentence end or automatic wraps
> to fit <80 columns.

The automatic wraps in the code presented in the message are wrong. The
automatic wraps in the bullet point list are, if not wrong, at least
presumably unintended.

I hope that clears up what I meant in this case. The effort devoted to
explaining this issue far outweighs the burden it caused initially. I'll
try to be more explicit when presenting it in future, to forestall this.

-- 
 \ “Of all classes the rich are the most noticed and the least |
  `\  studied.” —John Kenneth Galbraith, _The Age of Uncertainty_, |
_o__) 1977 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fast kdtree tree implementation for python 3?

2010-09-11 Thread David Cournapeau
On Sun, Sep 12, 2010 at 4:26 AM, Stefan Behnel  wrote:
> _wolf, 11.09.2010 20:15:
>>
>> does anyone have a suggestion for a ready-to-go, fast kdtree
>> implementation for python 3.1 and up, for nearest-neighbor searches? i
>> used to use the one from numpy/scipy, but find it a pain to install
>> for python 3.
>
> The latest release is supposed to work with Py3.

The latest (1.5) of numpy does, but there is no support of scipy for
python 3 yet, unless you use unreleased code. I think it is fair to
say it is still rough around the edges,

cheers,

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


Re: The Samurai Principle

2010-09-11 Thread Robert Kern

On 9/11/10 7:08 PM, Lawrence D'Oliveiro wrote:

In message, Robert
Kern wrote:


On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:


In message, Ian
Kelly wrote:


And returning None on failure is dangerous, because if the programmer
does not take care to handle that case, the program may attempt to
regard it as actual data.


But None *is* actual data.


And that is exactly the reason why the Samurai Principle says to not
return None when the function fails to do what it intended to do.


How can the function “fail” when it returns what it is specified to return?


The Samurai Principle is about what specifications and guarantees a programmer 
should make about his functions. It gives advice for programmers on how to 
specify their functions in the face of failure conditions the function itself 
sees. For example, a function that is supposed to fetch a record from a database 
given a key, a failure condition would be that the key does not exist in the 
database. Or the connection times out, or anything else that prevents the 
function from retrieving a correct record for the key. The Principle says that a 
programmer should specify his function such that it raises an exception to 
signify this failure rather than to return None.


Of course, a programmer may make his functions' contracts as loose as he 
pleases, and thus returning None may be "correct" in the sense that it satisfies 
those contracts. The Principle simply advises that making such contracts is not 
a good idea.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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