Re: Python Windows Extensions for Mac

2011-08-20 Thread Günther Dietrich
In article ,
 Chris Rebert  wrote:

>On Fri, Aug 19, 2011 at 1:02 PM, johnny.venter  wrote:
>>
>> Hello, I am looking for the Python Windows Extensions to see if they can be 
>> installed on a Mac.THanks.
>
>Your request is nonsensical.

That's not completely true.


>pywin32 wraps the Windows API libraries.
>Mac OS X is not Windows; it does not implement the Windows API. Thus,
>there is nothing for pywin32 to wrap on a Mac.

Again: Not completely true.
One could run python under wine and get -- at least a part of -- the 
Windows API.


>Square peg, round hole.

wine might be the tool that makes the square peg round.



Best regards,

Günther
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stop quoting spam [was Re: Hot Girls ...]

2011-08-20 Thread Alec Taylor
On Sat, Aug 20, 2011 at 9:24 AM, Albert W. Hopkins
 wrote:
>
>
> On Friday, August 19 at 17:12 (-0400), Matty Sarro said:
>
>>
>> If you're that offended then spend the cycles fixing the damn list so
>> it
>> stops having so much spam. You realize spam comes in almost
>> constantly,
>> right? Enough that multiple tines over the past weeks there have been
>> no
>> less than 3 threads about it.
>
> For me, the original post ended in my spam box, which means my filter is
> doing it's job, but when you re-post it, my filter did not regard it as
> spam.  I actually wish it had.  Therefore you are an enabler.
>
>
>> If php, red hat, and perl can manage it for their lists, why not
>> python? Is
>> that a statement about python programmers?
>>
>
> The python list is (also) a Usenet newsgroup.  Usenet is distributed and
> therefore there is no central place to filter spam (each usenet host
> would have to have its own filter and what one considers spam another
> might consider ham)... anyway, that's neither here nor there.  Having my
> own filter usually works.
>
> I'm not here to dis you, just to try to help you understand the how/why
> regarding the re-post and why your attitude about it might give the
> impression of apathy toward your peer community.
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I found said joke rather funny :P
-- 
http://mail.python.org/mailman/listinfo/python-list


Compare tuples of different lenght

2011-08-20 Thread Jurgens de Bruin
Hi,

I have a list of tuples:

[(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]

I would like to compare all the tuples to each other and if one
element if found two tuples the smallest tuples is removed from the
list.

example if tuple 1 and tuple 3 are compare it should find that a
single element in each are the same and tuple 1 should be removed
resulting in

[(12,13),(2,3,4),(8,),(5,6),(7,8,9),]

the same for tuple 4 and 6 resulting in

[(12,13),(2,3,4),(5,6),(7,8,9),]

is this possible as I am having no success.

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


Re: Help on PyQt4 QProcess

2011-08-20 Thread Phil Thompson
On Fri, 19 Aug 2011 14:32:12 -0700 (PDT), Edgar Fuentes
 wrote:
> On Aug 19, 4:21 pm, Carl Banks  wrote:
>> On Friday, August 19, 2011 12:55:40 PM UTC-7, Edgar Fuentes wrote:
>> > On Aug 19, 1:56 pm, Phil Thompson
>> >  wrote:
>> > > On Fri, 19 Aug 2011 10:15:20 -0700 (PDT), Edgar Fuentes
>> > >  wrote:
>> > > > Dear friends,
>>
>> > > > I need execute an external program from a gui using PyQt4, to
avoid
>> > > > that hang the main thread, i must connect the signal
>> > > > "finished(int)"
>> > > > of a QProcess to work properly.
>>
>> > > > for example, why this program don't work?
>>
>> > > >    from PyQt4.QtCore import QProcess
>> > > >    pro = QProcess() # create QProcess object
>> > > >    pro.connect(pro, SIGNAL('started()'), lambda
>> > > > x="started":print(x))        # connect
>> > > >    pro.connect(pro, SIGNAL("finished(int)"), lambda
>> > > > x="finished":print(x))
>> > > >    pro.start('python',['hello.py'])        # star hello.py
program
>> > > > (contain print("hello world!"))
>> > > >    timeout = -1
>> > > >    pro.waitForFinished(timeout)
>> > > >    print(pro.readAllStandardOutput().data())
>>
>> > > > output:
>>
>> > > >    started
>> > > >    0
>> > > >    b'hello world!\n'
>>
>> > > > see that not emit the signal finished(int)
>>
>> > > Yes it is, and your lambda slot is printing "0" which is the return
>> > > code
>> > > of the process.
>>
>> > > Phil
>>
>> > Ok, but the output should be:
>>
>> >     started
>> >     b'hello world!\n'
>> >     finished
>>
>> > no?.
>>
>> > thanks Phil
>>
>> Two issues.  First of all, your slot for the finished function does not
>> have the correct prototype, and it's accidentally not throwing an
>> exception because of your unnecessary use of default arguments.
 Anyway,
>> to fix that, try this:
>>
>> pro.connect(pro, SIGNAL("finished(int)"), lambda v,
>> x="finished":print(x))
>>
>> Notice that it adds an argument to the lambda (v) that accepts the int
>> argument of the signal.  If you don't have that argument there, the int
>> argument goes into x, which is why Python prints 0 instead of
"finished".
>>
>> Second, processess run asynchrously, and because of line-buffering, IO
>> can output asynchronously, and so there's no guarantee what order
output
>> occurs.  You might try calling the python subprocess with the '-u'
switch
>> to force unbuffered IO, which might be enough to force synchronous
output
>> (depending on how signal/slot and subprocess semantics are
implemented).
>>
>> Carl Banks
> 
> Thanks Carl, your intervention was very helpful for me, this solve my
> semantic error. I need to study more about signal/slots and process.

In which case you should look at the modern, Pythonic connection syntax
rather than the old one...

pro.started.connect(lambda: print("started"))
pro.finished.connect(lambda: print("finished"))

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


Re: Compare tuples of different lenght

2011-08-20 Thread Chris Rebert
On Sat, Aug 20, 2011 at 1:25 AM, Jurgens de Bruin  wrote:
> Hi,
>
> I have a list of tuples:
>
> [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
>
> I would like to compare all the tuples to each other and if one
> element if found two tuples the smallest tuples is removed from the
> list.

So, would [(5,6), (6,7,8)] become [(6,7,8)] ?

If no, then I believe you're trying to solve the set covering problem:
http://en.wikipedia.org/wiki/Set_cover_problem

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compare tuples of different lenght

2011-08-20 Thread Jurgens de Bruin
On Aug 20, 10:45 am, Chris Rebert  wrote:
> On Sat, Aug 20, 2011 at 1:25 AM, Jurgens de Bruin  wrote:
>
> > Hi,
>
> > I have a list of tuples:
>
> > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
>
> > I would like to compare all the tuples to each other and if one
> > element if found two tuples the smallest tuples is removed from the
> > list.
>
> So, would [(5,6), (6,7,8)] become [(6,7,8)] ?
>
> If no, then I believe you're trying to solve the set covering 
> problem:http://en.wikipedia.org/wiki/Set_cover_problem
>
> Cheers,
> Chris
> --http://rebertia.com

[(5,6), (6,7,8)] would become [(6,7,8)].

Thanks for the response

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


Re: Compare tuples of different lenght

2011-08-20 Thread Jurgens de Bruin
On Aug 20, 10:45 am, Chris Rebert  wrote:
> On Sat, Aug 20, 2011 at 1:25 AM, Jurgens de Bruin  wrote:
>
> > Hi,
>
> > I have a list of tuples:
>
> > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
>
> > I would like to compare all the tuples to each other and if one
> > element if found two tuples the smallest tuples is removed from the
> > list.
>
> So, would [(5,6), (6,7,8)] become [(6,7,8)] ?
>
> If no, then I believe you're trying to solve the set covering 
> problem:http://en.wikipedia.org/wiki/Set_cover_problem
>
> Cheers,
> Chris
> --http://rebertia.com

[(5,6), (6,7,8)] will indeed become [(6,7,8)]

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


Python import search path

2011-08-20 Thread Kevin Zhang
Hi,

This is the directory tree.
project
└── sme
├── src
│   ├── a.pth
│   ├── sss.py
└── test
└── ttt.py

I need to import sss.py in ttt.py.

Found a few solution in python docs.
A. sys.path.append
B. add ***.pth file
C. edit .../site-packages/site.py

I found out that a.pth only works when in directory like
/usr/lib/pythonX.Y/site-packages
instead of in current directory.

I think A is not so pretty, and I don't have root  privilege to use B and C.
So any both more elegant and practical solutions?
Thanks.

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


Re: Stop quoting spam [was Re: Hot Girls ...]

2011-08-20 Thread D'Arcy J.M. Cain
On Fri, 19 Aug 2011 17:10:49 -0400
Rodrick Brown  wrote:
> It's not the end of the world calm down I thought it was quite funny for a 
> friday joke! 

The first message might have been funny (if you are twelve) but the
rest were annoying and insulting.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


hello python

2011-08-20 Thread wukexin
from china 


--
designed by wk...@qq.com-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compare tuples of different lenght

2011-08-20 Thread Steven D'Aprano
Jurgens de Bruin wrote:

> Hi,
> 
> I have a list of tuples:
> 
> [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> I would like to compare all the tuples to each other and if one
> element if found two tuples the smallest tuples is removed from the
> list.

It's not clear what you mean by "smallest" tuple. Is (8,) smaller than
(7,8,9)?

I'm going to guess you care only about the length of the tuple, and not the
items themselves.

Let's start with a couple of helper functions.

def compare(t1, t2):
'Return -1 if t1 is "smaller" than t2, 0 if equal, and +1 if "bigger".'
if len(t1) < len(t2): return -1
elif len(t1) > len(t2): return 1
else: return 0

def match_any_item(t1, t2):
try:
s1 = set(t1)
s2 = set(t2)
return bool(s1 & s2)
except TypeError:
# Can't convert to sets because at least one item is mutable.
# Let's do this the slow(?) way.
matched = [x for x in t1 if x in t2]
return bool(matched)



list_of_tuples = [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
flags = [True]*len(list_of_tuples)
for i,t1 in enumerate(list_of_tuples):
for j in range(i+1, len(list_of_tuples)):
t2 = list_of_tuples[j]
if match_any_item(t1, t2):
n = compare(t1, t2)
if n == -1:
# Flag t1 to be removed.
flags[i] = False
elif n == 1:
# Flag t2 to be removed.
flags[j] = False

saved_tuples = []
for t,flag in zip(list_of_tuples, flags):
if flag: saved_tuples.append(t)



This gives:

>>> saved_tuples
[(12, 13), (2, 3, 4), (5, 6), (7, 8, 9)]


which matches what you wanted:

> [(12,13),(2,3,4),(5,6),(7,8,9),]




-- 
Steven

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


Re: Compare tuples of different lenght

2011-08-20 Thread Jurgens de Bruin
On Aug 20, 12:17 pm, Steven D'Aprano  wrote:
> Jurgens de Bruin wrote:
> > Hi,
>
> > I have a list of tuples:
>
> > [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
>
> > I would like to compare all the tuples to each other and if one
> > element if found two tuples the smallest tuples is removed from the
> > list.
>
> It's not clear what you mean by "smallest" tuple. Is (8,) smaller than
> (7,8,9)?
>
> I'm going to guess you care only about the length of the tuple, and not the
> items themselves.
>
> Let's start with a couple of helper functions.
>
> def compare(t1, t2):
>     'Return -1 if t1 is "smaller" than t2, 0 if equal, and +1 if "bigger".'
>     if len(t1) < len(t2): return -1
>     elif len(t1) > len(t2): return 1
>     else: return 0
>
> def match_any_item(t1, t2):
>     try:
>         s1 = set(t1)
>         s2 = set(t2)
>         return bool(s1 & s2)
>     except TypeError:
>         # Can't convert to sets because at least one item is mutable.
>         # Let's do this the slow(?) way.
>         matched = [x for x in t1 if x in t2]
>         return bool(matched)
>
> list_of_tuples = [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> flags = [True]*len(list_of_tuples)
> for i,t1 in enumerate(list_of_tuples):
>     for j in range(i+1, len(list_of_tuples)):
>         t2 = list_of_tuples[j]
>         if match_any_item(t1, t2):
>             n = compare(t1, t2)
>             if n == -1:
>                 # Flag t1 to be removed.
>                 flags[i] = False
>             elif n == 1:
>                 # Flag t2 to be removed.
>                 flags[j] = False
>
> saved_tuples = []
> for t,flag in zip(list_of_tuples, flags):
>     if flag: saved_tuples.append(t)
>
> This gives:
>
> >>> saved_tuples
>
> [(12, 13), (2, 3, 4), (5, 6), (7, 8, 9)]
>
> which matches what you wanted:
>
> > [(12,13),(2,3,4),(5,6),(7,8,9),]
>
> --
> Steven

Thanks Steven. This works great!!!

Appreciated very much!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compare tuples of different lenght

2011-08-20 Thread Peter Otten
Jurgens de Bruin wrote:

> Hi,
> 
> I have a list of tuples:
> 
> [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> I would like to compare all the tuples to each other and if one
> element if found two tuples the smallest tuples is removed from the
> list.
> 
> example if tuple 1 and tuple 3 are compare it should find that a
> single element in each are the same and tuple 1 should be removed
> resulting in
> 
> [(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> the same for tuple 4 and 6 resulting in
> 
> [(12,13),(2,3,4),(5,6),(7,8,9),]
> 
> is this possible as I am having no success.
> 
> Thanks

from collections import Counter, defaultdict
from itertools import chain

def process_counter(sample):
c = Counter()
d = defaultdict(list)
for items in sample:
c.update(items)
d[len(items)].append(items)

result = []
for cluster in sorted(d.values(), key=len):
c.subtract(chain.from_iterable(cluster))
for items in cluster:
if not any(c[item] for item in items):
result.append(items)

result.sort(key=sample.index)
return result

if __name__ == "__main__":
for process in [process_counter]:
print process.__name__

sample = [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
wanted = [(12,13),(2,3,4),(5,6),(7,8,9),]
assert process(sample) == wanted


sample = [(5,6), (6,7,8)]
wanted = [(6,7,8)]
got = process(sample)
assert got == wanted

sample = wanted = [(5, 6), (6, 7)]
assert process(sample) == wanted

sample = [(1,), (1, 2), (2, 3, 4)]
wanted = [(2, 3, 4)]
assert process(sample) == wanted
 

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


Re: Hot Girls are Looking for Sex

2011-08-20 Thread hackingKK
Well, they might be indented in the right places but i don't know if 
loops, conditions, functions, if they all happen or not.

:)
Happy hacking.
Krishnakant,

On 20/08/11 01:47, Matty Sarro wrote:

That's great - but do they program in python?

On Fri, Aug 19, 2011 at 2:38 PM, Sajjad Ahmad  wrote:

See All details on

http://hotelandtourism9.blogspot.com/2011/08/indian-hotels-wall-st-effect.html

.

See All details on

http://hotelandtourism9.blogspot.com/2011/08/indian-hotels-wall-st-effect.html

.

See All details on

http://hotelandtourism9.blogspot.com/2011/08/indian-hotels-wall-st-effect.html

.

See All details on

http://hotelandtourism9.blogspot.com/2011/08/indian-hotels-wall-st-effect.html

.

See All details on

http://hotelandtourism9.blogspot.com/2011/08/indian-hotels-wall-st-effect.html

.

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



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


Re: Stop quoting spam [was Re: Hot Girls ...]

2011-08-20 Thread David Robinow
On Sat, Aug 20, 2011 at 4:16 AM, Alec Taylor  wrote:
>> ...
> I found said joke rather funny :P
 Perhaps, as a retired amateur comedian, my standards are too high,
but I don't think adding a smilie to a stupid post suddenly turns it
into a joke. Nevertheless, the quality of the attempt is not really
the issue here. The would-be humorist did not need to quote the spam.
Please, don't do it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python import search path

2011-08-20 Thread Chris Angelico
On Sat, Aug 20, 2011 at 10:52 AM, Kevin Zhang  wrote:
> Found a few solution in python docs.
> A. sys.path.append

> I think A is not so pretty, and I don't have root  privilege to use B and C.
> So any both more elegant and practical solutions?

If, as I understand from your directory tree, ttt.py is a tester for
sss.py, then I'd say this is the right option. It's only going to be
used in the special environment of testing, so it's okay to have a
single line of code up the top that makes it convenient.

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


Re: Stop quoting spam [was Re: Hot Girls ...]

2011-08-20 Thread D'Arcy J.M. Cain
On Sat, 20 Aug 2011 08:17:32 -0400
David Robinow  wrote:
> > I found said joke rather funny :P
>  Perhaps, as a retired amateur comedian, my standards are too high,

How does one retire from amateur status?  Do you suddenly start
charging for telling jokes?  :-)

> but I don't think adding a smilie to a stupid post suddenly turns it
> into a joke. Nevertheless, the quality of the attempt is not really
> the issue here. The would-be humorist did not need to quote the spam.

Well, exactly.  I don't think that anyone made any comment about the
quality of the joke when talking about the first posting.  The only
thing that people said was that he shouldn't have repeated the spam.
Everyone, including the original poster, who defended the post did so
on the grounds that it was funny.  That's certainly debatable but no
one was telling him not to post until he gets funnier.

http://en.wikipedia.org/wiki/Straw_man

Whether the post was funny or not is a judgment call.  No one is saying
not to post unfunny jokes because no one is the arbiter of what's
funny.  If you want to argue with the complainers, argue with their
actual complaint.  Tell us why it is OK to repeat spam with all the
spammy URLS intact

By the way, my joke above is hilarious.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Really, stop repeating spam! (Was: Hot Girls...)

2011-08-20 Thread D'Arcy J.M. Cain
On Sat, 20 Aug 2011 15:16:08 +0530
hackingKK  wrote:
> Well, they might be indented in the right places but i don't know if 
> loops, conditions, functions, if they all happen or not.
> :)

[Entire spam deleted AGAIN]

Good grief!  Haven't you seen all the followups to that posting you
replied to?  Are you two actually in cahoots with the spammer?

Wait, I get it.  The spammer, Matty and you are all on gmail.  You are
all the same person, aren't you?

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Really, stop repeating spam! (Was: Hot Girls...)

2011-08-20 Thread Chris Angelico
On Sat, Aug 20, 2011 at 3:57 PM, D'Arcy J.M. Cain  wrote:
> Wait, I get it.  The spammer, Matty and you are all on gmail.  You are
> all the same person, aren't you?
>

Gmail is all one person now? That would explain why I keep seeing
things I agree with. I had no idea there were so many of me around!

Wait, does that make me Agent Smith?

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


Re: Compare tuples of different lenght

2011-08-20 Thread John O'Hagan
On Sat, 20 Aug 2011 01:25:18 -0700 (PDT)
Jurgens de Bruin  wrote:

> Hi,
> 
> I have a list of tuples:
> 
> [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> I would like to compare all the tuples to each other and if one
> element if found two tuples the smallest tuples is removed from the
> list.
[...]

This should work:

def long_match(tuples):
sorted_tuples = sorted(tuples, key=len)
for n, t in enumerate(sorted_tuples):
for s in sorted_tuples[n + 1:]:
if len(s) > len(t) and any(i in s for i in t):
tuples.remove(t)
break
return tuples 
 

Regards,

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


Re: testing if a list contains a sublist

2011-08-20 Thread Simon Forman
On Mon, Aug 15, 2011 at 4:26 PM, Johannes  wrote:
> hi list,
> what is the best way to check if a given list (lets call it l1) is
> totally contained in a second list (l2)?
>
> for example:
> l1 = [1,2], l2 = [1,2,3,4,5] -> l1 is contained in l2
> l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2
> l1 = [1,2,3], l2 = [1,3,5,7] -> l1 is not contained in l2
>
> my problem is the second example, which makes it impossible to work with
> sets insteads of lists. But something like set.issubset for lists would
> be nice.
>
> greatz Johannes
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Probably not the most efficient way, but I wanted to mention it:


from difflib import SequenceMatcher


def list_in(a, b):
'''Is a completely contained in b?'''
matcher = SequenceMatcher(a=a, b=b)
m = matcher.find_longest_match(0, len(a), 0, len(b))
return m.size == len(a)


Cheers,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Windows Extensions for Mac

2011-08-20 Thread Kevin Walzer

On 8/19/11 4:02 PM, johnny.venter wrote:


Hello, I am looking for the Python Windows Extensions to see if they can be 
installed on a Mac.THanks.



You can certainly try to install them via easy_install, I supposed, but 
it's doubtful they would do anything, as the Mac does not support win32 
API calls any more than Windows supports Cocoa/Objective-C calls.


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: List spam

2011-08-20 Thread George
I find python group is filled with spam mails, is there any way to filter
these mails before sending it to the group.
I can't see this situation with similar user group, such as the jsr.

George.


On 20/08/2011 07:07, "Ben Finney"  wrote:

> Javier  writes:

> You will lose a lot of people
> asking/answering interesting stuff, and
> maybe eventually the list will
> die.

I don't think it would die, but the chances are greater that it
> would
become insular and further disconnected from the Python community,
> and
hence far less useful.

> Me (like many people with little free time)
> seldom post in
> blogs/forums/mailing lists where I need to register.

+1

--
> 
 \  ³Ignorance more frequently begets confidence than does |
  `\
> knowledge.² ‹Charles Darwin, _The Descent of Man_, 1871 |
_o__)
> |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list



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


extended slicing and negative stop value problem

2011-08-20 Thread Max Moroz
Would it be a good idea to change Python definition so that a[10, -1, -1]
referred to the elements starting with position 10, going down to the
beginning?

This would require disabling the "negative stop value means counting from
the end of the array" magic whenever the step value is negative.

The reason for this idea is that many people (me including) try to use
extended slices with negative step values, only to realize that they are
messed up. For example, if your stop value is reduced in a loop from a
positive number to -1, the behavior breaks whenever it hits -1.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extended slicing and negative stop value problem

2011-08-20 Thread Chris Angelico
On Sat, Aug 20, 2011 at 7:20 PM, Max Moroz  wrote:
> Would it be a good idea to change Python definition so that a[10, -1, -1]
> referred to the elements starting with position 10, going down to the
> beginning?

Well, first off I think it's a dangerous idea to change semantics of
something like that. I can see your use case, but I think that what
you want is covered by simply omitting the stop marker:

>>> a="qwertyuiopasdfghjklzxcvbnm"
>>> a[10:1:-1]
'apoiuytre'
>>> a[10:0:-1]
'apoiuytrew'
>>> a[10::-1]
'apoiuytrewq'

If you're using a variable for the stop value, you just need to set it
to an explicit None if it would fall negative:

>>> a[10:None:-1]
'apoiuytrewq'

Hope that helps!

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


Re: try... except with unknown error types

2011-08-20 Thread Paul Rubin
Steven D'Aprano  writes:
>> You can catch all exceptions by catching the base class Exception:
>
> Except that is nearly always poor advice, because it catches too much: it
> hides bugs in code, as well as things which should be caught.
> You should always catch the absolute minimum you need to catch.

But there's no way to know what that minimum is.  Python libraries throw
all sorts of exceptions that their documentation doesn't mention.
Java's checked exceptions are obnoxious but they do have their
attractions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extended slicing and negative stop value problem

2011-08-20 Thread Max
On Aug 20, 11:29 am, Chris Angelico  wrote:
> If you're using a variable for the stop value, you just need to set it
> to an explicit None if it would fall negative:
>
> >>> a[10:None:-1]
>

That doesn't work if it's set in a loop or if it's calculated as a
formula. For example, this very simple code doesn't work because of
the "-1 problem".

# find the longest substring that reads the same left to right and
right to left
for substr_length in range(len(input),0,-1):
for starting_pos in range(len(input)-substr_length+1):
ending_pos = starting_pos + substr_length - 1
if input[starting_pos:ending_pos+1] == input[ending_pos :
starting_pos-1 : -1]:
   print(input[starting_pos:ending_pos+1])
   exit(0)

Of course you can rewrite it, but it becomes quite ugly.

(Not to mention, people who learn the language would not always know
this, and will end up with a bug.)

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


Re: extended slicing and negative stop value problem

2011-08-20 Thread Chris Angelico
On Sat, Aug 20, 2011 at 7:52 PM, Max  wrote:
> That doesn't work if it's set in a loop or if it's calculated as a
> formula. For example, this very simple code doesn't work because of
> the "-1 problem".
>

Right, which is what I meant by setting it to an explicit None:

if input[starting_pos:ending_pos+1] == input[ending_pos :
starting_pos-1 if starting_pos >= 0 else None : -1]:

You're right that it starts to get ugly, though.

Of course, there are other ways to find the longest palindromic
substring in a string:

# I wouldn't bother counting a one-character "palindrome"
for substr_length in range(len(input),1,-1):
   for starting_pos in range(len(input)-substr_length+1):
   ending_pos = starting_pos + substr_length - 1
   testme = input[starting_pos:ending_pos+1]
   if testme == testme[::-1]:
  print(testme)
  exit(0)

That is, snip out the string and then reverse that snipped piece,
rather than reverse-slicing from the original. This doesn't solve the
issue of slicing backwards with variable halts, though.

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


Re: try... except with unknown error types

2011-08-20 Thread Steven D'Aprano
Paul Rubin wrote:

> Steven D'Aprano  writes:
>>> You can catch all exceptions by catching the base class Exception:
>>
>> Except that is nearly always poor advice, because it catches too much: it
>> hides bugs in code, as well as things which should be caught.
>> You should always catch the absolute minimum you need to catch.
> 
> But there's no way to know what that minimum is.  Python libraries throw
> all sorts of exceptions that their documentation doesn't mention.

Yes, you're absolutely correct. But it's also irrelevant. Most of those
exceptions should not be caught, even if you know what they are, because
they represent either bugs that should be fixed, or bad data which should
raise an exception. A bare except, or except Exception, is hardly ever the
right approach.

As for exceptions which should be caught, they should be dealt with on a
case-by-case basis. There's no need to identify all those obscure
exception-raising cases ahead of time. After all, unless you're writing
software for a nuclear reactor, or an aeroplane's autopilot, chances are
that *bugs don't really matter*. That is to say, if you release software
with a hidden bug, the consequences generally aren't very important.
(Depends on the nature of the software, and the bug, of course. Sometimes
bugs are important. How's your test suite?) At some point, you will get a
bug report, and then you will fix the bug. The fix may involve catching an
extra exception, or avoiding generating the exception in the first place.

Trying to predict ahead of time every possible exception that could be
raised, and deal with them correctly (as opposed to just sweeping them
under the carpet), is not only impossible but also usually unnecessary.

It took me a long time to realise that the world won't end if I write a
piece of software with a bug. Now I realise that software is never
finished, there's always going to be a next version, so trying to make it
perfect is a fool's errand. It's very liberating :)


> Java's checked exceptions are obnoxious but they do have their
> attractions.

No doubt about it, the concept is attractive, but a few Java heavyweights
now consider checked exceptions to be a mistake.

http://www.mindview.net/Etc/Discussions/CheckedExceptions
http://radio-weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html

More here:
http://www.ibm.com/developerworks/java/library/j-jtp05254/index.html



-- 
Steven

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


Re: try... except with unknown error types

2011-08-20 Thread John Nagle

On 8/19/2011 1:24 PM, John Gordon wrote:

In<4e4ec405$0$29994$c3e8da3$54964...@news.astraweb.com>  Steven 
D'Aprano  writes:


You can catch all exceptions by catching the base class Exception:



Except that is nearly always poor advice, because it catches too much: it
hides bugs in code, as well as things which should be caught.



You should always catch the absolute minimum you need to catch.


   Right.  When in doubt, catch EnvironmentError.  That means something
external to the program, at the OS or network level, has a problem.
"Exception" covers errors which are program bugs, like references to
undefined class members.

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


Re: extended slicing and negative stop value problem

2011-08-20 Thread Steven D'Aprano
Pardon me for breaking threading, but I don't have Max's original post.

On Sat, Aug 20, 2011 at 7:20 PM, Max Moroz  wrote:

> Would it be a good idea to change Python definition so that a[10, -1, -1]

I presume you mean slice notation a[10:-1:-1].


> referred to the elements starting with position 10, going down to the
> beginning?

No, almost certainly not. Such a change would break backwards compatibility,
and so would only be allowed under very unusual circumstances: the current
behaviour would have to be a major problem, or the new behaviour a huge
benefit, or both, to make up for:

(1) the extra work needed to change the behaviour (probably involving
a "from __future__ import ..." feature for the first version or two); 

(2) breaking people's existing code; and

(3) forcing people to learn the new behaviour and unlearn the old.

Even if the old behaviour is "wrong", the work needed to fix it may be more
than the benefit. If this was going to be "fixed", the time was probably
about three years ago, when Python3 was just starting. Now such a change
will probably need to wait for the hypothetical Python 4000.

 
> This would require disabling the "negative stop value means counting from
> the end of the array" magic whenever the step value is negative.

Which will hurt people who expect the current behaviour:

>>> a[8:-8:-1]
[8, 7, 6, 5, 4, 3]


> The reason for this idea is that many people (me including) try to use
> extended slices with negative step values, only to realize that they are
> messed up. For example, if your stop value is reduced in a loop from a
> positive number to -1, the behavior breaks whenever it hits -1.

Yes, negative step values are unintuitive, especially if the step is not -1.
The solution is, "Don't do that then!". 

The usual advice is to do your slicing twice, reversing it the second time:


a[0:11][::-1]
# Instead of a[10:-1:-1], which looks like it should work, but doesn't.

(or use the reversed() built-in instead of the second slice), or to write a
helper function to adjust the indexes and get whatever behaviour you like.
Hint:

>>> a[10:-11:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



-- 
Steven

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


Re: Python Windows Extensions for Mac

2011-08-20 Thread Johnny Venter
Thank you all for the replies. I would like to query various Windows' objects 
and resources from Mac and/or Linux such as Active Directory users, network 
shares, group members, etc... What module or methods can I use with python to 
accomplish this? 

I found dcerpc might be the way to go. 

On Aug 20, 2011, at 1:39 PM, Kevin Walzer  wrote:

> On 8/19/11 4:02 PM, johnny.venter wrote:
>> 
>> Hello, I am looking for the Python Windows Extensions to see if they can be 
>> installed on a Mac.THanks.
>> 
> 
> You can certainly try to install them via easy_install, I supposed, but it's 
> doubtful they would do anything, as the Mac does not support win32 API calls 
> any more than Windows supports Cocoa/Objective-C calls.
> 
> -- 
> Kevin Walzer
> Code by Kevin
> http://www.codebykevin.com
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Python Windows Extensions for Mac

2011-08-20 Thread Chris Angelico
On Sun, Aug 21, 2011 at 12:51 AM, Johnny Venter  wrote:
> Thank you all for the replies. I would like to query various Windows' objects 
> and resources from Mac and/or Linux such as Active Directory users, network 
> shares, group members, etc... What module or methods can I use with python to 
> accomplish this?
>

The concept doesn't have meaning on a non-Windows computer, so I am
going to take the liberty of assuming that you really want to query
them from a different computer - some kind of network query. If that's
not the case, can you clarify exactly what your setup is?

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


Re: Replacement for the shelve module?

2011-08-20 Thread Gregory Ewing

Robert Kern wrote:


That's just incorrect. You shouldn't use (binary) floats for many
*accounting* purposes, but for many financial/econometric analyses, floats
are de rigeur and work much better than decimals


There's a certain accounting package I work with that *does*
use floats -- binary ones -- for accounting purposes, and
somehow manages to get away with it. Not something I would
recommend trying at home, though.

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


Re: Replacement for the shelve module?

2011-08-20 Thread Chris Angelico
On Sun, Aug 21, 2011 at 1:37 AM, Gregory Ewing
 wrote:
> There's a certain accounting package I work with that *does*
> use floats -- binary ones -- for accounting purposes, and
> somehow manages to get away with it. Not something I would
> recommend trying at home, though.
>

Probably quite a few, actually. It's not a very visible problem so
long as you always have plenty of "spare precision", and you round
everything off to two decimals (or however many for your currency).
Eventually you'll start seeing weird results that are a cent off, but
you won't notice them often. And hey. You store $1.23 as 1.23, and it
just works! It must be the right thing to do!

Me, I store dollars-and-cents currency in cents. Always. But that's
because I never need fractional cents. I'm not sure what the best way
to handle fractional cents is, but I'm fairly confident that this
isn't it:

http://thedailywtf.com/Articles/Price-in-Nonsense.aspx

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


Re: Python Windows Extensions for Mac

2011-08-20 Thread Johnny Venter
Yes, I want to make my queries from a remote non-Windows computer. Here is the 
scenario:

>From my mac, I want to use python to access and read objects from a remote  
>Windows computer joined to a Windows 2003 functional level domain. Given this, 
>what is the best way to accomplish this?


On Aug 20, 2011, at 7:57 PM, Chris Angelico  wrote:

> On Sun, Aug 21, 2011 at 12:51 AM, Johnny Venter  
> wrote:
>> Thank you all for the replies. I would like to query various Windows' 
>> objects and resources from Mac and/or Linux such as Active Directory users, 
>> network shares, group members, etc... What module or methods can I use with 
>> python to accomplish this?
>> 
> 
> The concept doesn't have meaning on a non-Windows computer, so I am
> going to take the liberty of assuming that you really want to query
> them from a different computer - some kind of network query. If that's
> not the case, can you clarify exactly what your setup is?
> 
> Chris Angelico
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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