Re: bool behavior in Python 3000?

2007-07-12 Thread Nis Jørgensen
Alan Isaac skrev:
> Since it is seemingly ignored in most of the comments
> on this thread, I just want to remind that PEP 285
> http://www.python.org/dev/peps/pep-0285/
> says this:
> 
>   In an ideal world, bool might be better implemented as a
>   separate integer type that knows how to perform mixed-mode
>   arithmetic.
> 
> I mentioned Python 3000 since that is an opportunity for an ideal world.

You forgot to quote this bit:

4) Should we strive to eliminate non-Boolean operations on bools
   in the future, through suitable warnings, so that for example
   True+1 would eventually (in Python 3000) be illegal?

=> No.

   There's a small but vocal minority that would prefer to see
   "textbook" bools that don't support arithmetic operations at
   all, but most reviewers agree with me that bools should always
   allow arithmetic operations.

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


Re: Where is the syntax for the dict() constructor ?!

2007-07-05 Thread Nis Jørgensen
Wildemar Wildenburger skrev:
> Nis Jørgensen wrote:
>> Neil Cerutti skrev:
>>
>>  
>>> Mostly you can use the default 'excel' dialect and be quite
>>> happy, since Excel is the main reason anybody still cares about
>>> this unecessarily hard to parse (it requires more than one
>>> character of lookahead for no reason except bad design) data
>>> format.
>>> 
>>
>> I knew there had to be a reason why everyone is using xml these days ...
>>
>> Nis
>>   
> Are you serious? 

No.

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


Re: Where is the syntax for the dict() constructor ?!

2007-07-05 Thread Nis Jørgensen
Neil Cerutti skrev:

> Mostly you can use the default 'excel' dialect and be quite
> happy, since Excel is the main reason anybody still cares about
> this unecessarily hard to parse (it requires more than one
> character of lookahead for no reason except bad design) data
> format.

I knew there had to be a reason why everyone is using xml these days ...

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


Re: Proposal: s1.intersects(s2)

2007-07-05 Thread Nis Jørgensen
Steven D'Aprano skrev:
> On Wed, 04 Jul 2007 23:53:15 -0400, David Abrahams wrote:
> 
>> on Wed Jul 04 2007, "Steven D'Aprano" 
>>  wrote:
>>
>>> On Wed, 04 Jul 2007 14:37:34 +, Marc 'BlackJack' Rintsch wrote:
>>>
 On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:

> Here's an implementation of the functionality I propose, as a
> free-standing function:
>
> def intersects(s1,s2):
> if len(s1) < len(s2):
> for x in s1:
> if x in s2: return True
> else:
> for x in s2:
> if x in s1 return True
> return False
 In Python 2.5 this can be written a bit more concise:

 def intersects(set_a, set_b):
 if len(set_a) < len(set_b):
 set_a, set_b = set_b, set_a
 return any(item in set_a for item in set_b)
>>>
>>> I'd rather see sets gain an method "isintersect()" 
>> And why is that a good name?  It's not even grammatical.
> 
> Neither is "It's not even grammatical", but all but purists use it
> regardless.
> 
> A common Python convention is to have test functions named something
> like "isfoo", e.g. str.isdigit(), isspace(), islower() etc. They're not
> exactly grammatical either, e.g. isdigit() should actually be "contains
> one or more digits, and nothing but digits". (Presumably the pedantically
> correct name was rejected as being too long.) I was just following that
> convention.

The problem is, these functions can be read as "X is [consisting only
of] digit[s]", "X is lower [case]" etc, where the bits in brackets have
been removed for brewity. In the case of "s1 is intersect s2" there is
no way I can see of adding words to get a correct sentence. The
"obvious" naming is "s1.intersects(s2)" which reads as "s1 intersects
s2", a perfectly cromulent sentence.

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


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-07-05 Thread Nis Jørgensen
Bruno Desthuilliers skrev:
> Paul Rubin a écrit :
>> Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
>>
>>> Haskell - as other languages using type-inference like OCaml - are in
>>> a different category. Yes, I know, don't say it, they are statically
>>> typed - but it's mostly structural typing, not declarative
>>> typing. Which makes them much more usable IMHO. 
>>
>>
>> Some users in fact recommend writing an explicit type signature for
>> every Haskell function, which functions sort of like a unit test.
> 
> Stop here. explicit type signature == declarative static typing != unit
> test.

Well, it quacks like a duck ...

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


Re: Generator for k-permutations without repetition

2007-07-04 Thread Nis Jørgensen
bullockbefriending bard skrev:
> On Jul 4, 7:09 pm, Nis Jørgensen <[EMAIL PROTECTED]> wrote:
>> bullockbefriending bard skrev:
>> A quick solution, not extensively tested
>>
>> # base needs to be an of the python builtin  set
>>
>> def k_perm(base,k):
>> for e in base:  
>> if k == 1:
>> yield [e]  
>> else:
>> for perm in k_perm(base-set([e]),k-1):
>> yield [e] + perm
> 
> thank you!. i'll give this a try. seems to do exactly what i need.
> sorry about the ambiguity in my example. i chose 3 from 3 merely to
> keep the size of the example case small, without thinking enough about
> how it might cause confusion.

I managed to simplify it a little:

def k_perm(base,k):
if k == 0:
yield []
else:
for e in base:  
for perm in k_perm(base-set([e]),k-1):
yield [e] + perm



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


Re: Generator for k-permutations without repetition

2007-07-04 Thread Nis Jørgensen
bullockbefriending bard skrev:
> I was able to google a recipe for a k_permutations generator,  such
> that i can write:
> 
> x = range(1, 4)  # (say)
> [combi for combi in k_permutations(x, 3)] =>
> 
> [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1,
> 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2,
> 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1],
> [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3,
> 3, 2], [3, 3, 3]]
> 
> but what i really want is the above without repetition, i.e.:
> 
> [combi for combi in k_permutations_without_repetitions(x, 3)] =>
> 
> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
> 
> For smallish lists, it's no problem to generate the list as follows:
> 
> no_reps_combis = [combi for combi in k_permutations(x, 3) if \
>  
> len(set(combi)) == 3]
> 
> but i'm sure there must be a way to do this as a pure generator, just
> that i wouldn't have a clue how to go about it.
> 
> Help please, Python Gods! :)

I was initially confused by your example, since the size of the
underlying set is the same as of the individual permutations. In this
case, you are just asking for all permutations of the set.

As far as I can tell from a quick googling, the relevant definition of
k-permutation is "an ordered list of k elements from the set". Thus your
first example does not really generate k_permutations.

A quick solution, not extensively tested

# base needs to be an of the python builtin  set

def k_perm(base,k): 
for e in base:  
if k == 1:
yield [e]   
else:
for perm in k_perm(base-set([e]),k-1):
yield [e] + perm

>>> list(k_perm(set([1,2,3,4]),3))
[[1, 2, 3], [1, 2, 4], [1, 3, 2], [1, 3, 4], [1, 4, 2], [1, 4, 3], [2,
1, 3], [2, 1, 4], [2, 3, 1], [2, 3, 4], [2, 4, 1], [2, 4, 3], [3, 1, 2],
[3, 1, 4], [3, 2, 1], [3, 2, 4], [3, 4, 1], [3, 4, 2], [4, 1, 2], [4, 1,
3], [4, 2, 1], [4, 2, 3], [4, 3, 1], [4, 3, 2]]


Much to my initial surprise, it "works" for k<1 as well:

>>> list(k_perm(set([1,2,3,4]),-1))
[]

Hope this helps

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


Re: howto resend args and kwargs to other func?

2007-07-02 Thread Nis Jørgensen
Bruno Desthuilliers skrev:

>> Why do people do this without posting what the actual solution is
> 
> Probably because those people think usenet is a free help desk ?

Usenet definitely isn't a help desk. You often get useful answers from
usenet, from people who are not reading from a script.

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


Re: howto resend args and kwargs to other func?

2007-07-02 Thread Nis Jørgensen
Bruno Desthuilliers skrev:

>> Why do people do this without posting what the actual solution is
> 
> Probably because those people think usenet is a free help desk ?

Usenet definitely isn't a help desk. You often get useful answers from
usenet, from people who are not reading from a script.

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


Re: howto resend args and kwargs to other func?

2007-07-02 Thread Nis Jørgensen
Bruno Desthuilliers skrev:

>> Why do people do this without posting what the actual solution is
> 
> Probably because those people think usenet is a free help desk ?

Usenet definitely isn't a help desk. You often get useful answers from
usenet, from people who are not reading from a script.

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


Re: howto resend args and kwargs to other func?

2007-07-02 Thread Nis Jørgensen
Bruno Desthuilliers skrev:

>> Why do people do this without posting what the actual solution is
> 
> Probably because those people think usenet is a free help desk ?

Usenet definitely isn't a help desk. You often get useful answers from
usenet, from people who are not reading from a script.

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


Re: mapping subintervals

2007-06-13 Thread Nis Jørgensen
Matteo skrev:

> OK - I'm going to assume your intervals are inclusive (i.e. 34-51
> contains both 34 and 51).
> 
> If your intervals are all really all non-overlapping, one thing you
> can try is to put all the endpoints in a single list, and sort it.
> Then, you can use the bisect module to search for intervals, which
> will give you a logarithmic time algorithm.
> 
> Here, I'm going to assume you just need the index of the containing
> interval. If you really need a name (i.e. 'a1' or 'a2'), you can use a
> list of names, and index into that.
> 
> I hope those assumptions are valid! if so, the following should work:

I have taken the liberty of simplifying your code, using the fact that
tuples are sorted lexicographically. Note that this requires all
intervals to be tuples and not lists (since list(a) < tuple(b) is always
True).

from bisect import bisect

def test_interval(ivl,intervals):

  # Find where ivl would lie in the list
  # i.e. the index of the first interval sorting as larger than ivl
  idx=bisect(intervals,ivl)
  # Left endpoints equal is a special case - a matching interval will be
  # to the right of the insertion point
  if idx < len(intervals) and intervals[idx][0] == ivl[0]:
if intervals[idx][1] >= ivl[1]:
return idx
else:
return None
  # Otherwise, we need to check to the left of the insertion point
  if idx > 0 and intervals[idx-1][1] >= ivl[1]:
return idx-1
  else:
return None

>>> intervals =[(10, 21), (34, 51), (77, 101)]
>>> print test_interval((34,35),intervals)
1
>>> print test_interval((34,53),intervals)
None
>>> print test_interval((77,53),intervals)
2
>>> print test_interval((77,83),intervals)
2
>>> print test_interval((77,102),intervals)
None
>>> print test_interval((77,101),intervals)
2

u"Nis J\xf8rgensen"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Thunderbird access to this newsgroup

2007-06-12 Thread Nis Jørgensen
Rostfrei skrev:
> Hello!
> 
> I'm writing this message over Google web access. I'm trying to access
> to the comp.lang.python newsgroup trough the Thunderbird, but I just
> can't configure it properly. What is the news server for this
> newsgroup. If I ping comp.lang.python it is not resolved. For instance
> I had no problem configuring Eclipse news server. I just entered
> news.eclipse.org as a news server address and after it I can browse
> available groups.

Newsgroups are not specific to one server, they can be "carried" by any
number of servers. Most ISP's make a news server available to their
customers which carries the "standard" newsgroup hierarchy - which
comp.lang.python is part of. Try using news. as the server
name, or look at their help pages for the correct server name/setup.
There used to be quite a few free and open servers available, but I
don't know the situation today.

Many organizations keep their own newsgroup servers, which carry only
their own groups (and possibly a few relevant ones from the standard
hierarchy) - news.eclipse.org is probably such a server.

Hope this helps.

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


Re: for ... else ?

2007-06-12 Thread Nis Jørgensen
exhuma.twn skrev:

>> for number in range(10,100):
>>  for divisor in range(2,number):
>>  if number % divisor == 0:
>>  break
>>  else:
>>  print number,
>>
> 
> Oh my. Would it not be an idea to rename this "else" into a "finally"?
> As Gabriel points out, the else-block gets executed after the for loop
> exits *normally*. In that case, is the "else" not semantically
> misleading? I would surely misunderstand it if I saw it the first time.

"finally" would be at least equally confusing IMO, indicating that the
code is always called (although this would of course make it a
ridiculous construct).

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


Re: Newbie question - better way to do this?

2007-05-28 Thread Nis Jørgensen
Steve Howell skrev:
> --- Nis Jørgensen <[EMAIL PROTECTED]> wrote:
> 
>> Steve Howell skrev:
>>
>>> def firstIsCapitalized(word):
>>> return 'A' <= word[0] <= 'Z'
>> For someone who is worried about the impact of
>> non-ascii identifiers,
>> you are making surprising assumptions about the
>> contents of data.
>>
> 
> The function there, which I don't even remotely
> defend, had nothing to with the main point of the
> thread.  Others pointed out that correct idiom here is
> word.istitle(), but the main point of the thread was
> how to prevent the newbie from essentially reinventing
> itertools.groupby.

The subject line says "Newbie question - better way to do this". I was
hinting at a better  way to do what you did, which was supposedly a
better way of doing what the newbie wanted.

I disagree that word.istitle is the correct idiom - from the naming of
the function in the original example, I would guess "word[0].isupper"
would do the trick.

> If you want to point out holes in my logic about the
> impact of non-ascii identifiers (the above code,
> though bad, does not suggest a hole in my logic; it
> perhaps even adds to my case), can you kindly do it in
> a thread where that's the main point of the
> discussion?

Will do.

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


Re: unit testing

2007-05-28 Thread Nis Jørgensen
Steve Howell skrev:

> And, really, if
> you're not doing automated tests on your application
> now, you don't know what you're missing.

Quote of the day, IMO.

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


Re: Newbie question - better way to do this?

2007-05-28 Thread Nis Jørgensen
Steve Howell skrev:

> def firstIsCapitalized(word):
> return 'A' <= word[0] <= 'Z'

For someone who is worried about the impact of non-ascii identifiers,
you are making surprising assumptions about the contents of data.

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


Re: Create an XML document

2007-05-22 Thread Nis Jørgensen
[EMAIL PROTECTED] skrev:
> Hi all,
> 
> I am attempting to create an XML document dynamically with Python. It
> needs the following format:
> 
> 
>   
>   1179775800
>   1800
> 
> 
> 
> I tried using minidom with the following code:
> 
> 
> 
> from xml.dom.minidom import Document
> 
> doc = Document()
> 
> zappt = doc.createElement('zAppointments')
> zappt.setAttribute('reminder', '15')
> doc.appendChild(zappt)
> 
> appt = doc.createElement('appointment')
> zappt.appendChild(appt)
> 
> begin = doc.createElement('begin')
> appt.appendChild(begin)
> f = file(r'path\to\file.xml', 'w')
> f.write(doc.toprettyxml(indent=''))
> f.close()
> 
> 

> How do I get Python to put values into the elements by tag name? I can
> parse my documents just fine, but now I need to edit them and write
> them out to a file for an application I am working on. I am sure I am
> missing something obvious.

>From http://wiki.python.org/moin/MiniDom

Add an Element with Text Inside

Create & add an XML element to an XML document, the element has text inside.

ex: hello, world!


from xml.dom.minidom import parse

dom = parse("bar.xml")
x = dom.createElement("foo")  # creates 
txt = dom.createTextNode("hello, world!")  # creates "hello, world!"
x.appendChild(txt)  # results in hello, world!
dom.childNodes[1].appendChild(x)  # appends at end of 1st child's \ children
print dom.toxml()
-- 
http://mail.python.org/mailman/listinfo/python-list