Re: Newbie edit/compile/run cycle question

2007-12-09 Thread Arnaud Delobelle
rly. In the shell, type 'help(reload)'. This might help. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Are Python deques linked lists?

2007-12-10 Thread Arnaud Delobelle
time insertion anywhere in the list. > > It's on the shelf between the jar of phlogiston and the perpetual > motion machine. I recommend a quick glance at any article on linked list. Here's one: http://en.wikipedia.org/wiki/Linked_list -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: python vs perl performance test

2007-12-13 Thread Arnaud Delobelle
, 54, 54, 54, 54, 54] Instead you want imap(randrange, repeat(n, p)): >>> map(randrange, repeat(100, 10)) [69, 68, 81, 26, 60, 76, 40, 55, 76, 75] I don't understand why you make the Python version so complicated. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: container.___le___ can use only <=?

2007-12-15 Thread Arnaud Delobelle
f __gt__(self, other): return self.compare(other, operator.gt) # etc... This way the LarchTree object doesn't interpret the comparison operators, just passes them on to its elements. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert a sequence of bits to a bit-string

2007-12-15 Thread Arnaud Delobelle
e whole weekend :) > Thanks, > Thomas HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: About Rational Number (PEP 239/PEP 240)

2007-12-15 Thread Arnaud Delobelle
of integers, they need to be kept in normalised form. * Nobody uses big fractions apart from mathematicians (and maybe bookmakers?). * Having yet another numerical type would make it difficult what type to expect from a calculation. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: python docs in contrib?

2007-12-16 Thread Arnaud Delobelle
d wondered why? > > > What contrib? > > contrib section of the archive, as opposed to main Are you refering to debian packages for python? In this case you may be better off posting on one of the debian mailing lists. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: build a syntax tree

2007-12-18 Thread Arnaud Delobelle
> If I've to build a tree, please give me resources and/or examples of > what physically is a tree and how to build on python. > > Thanks :) You can use a parser generator to build the syntax tree that you want. See http://wiki.python.org/moin/LanguageParsing for a list. Perhaps

Re: fiber(cooperative multi-threading)

2007-12-22 Thread Arnaud Delobelle
nd(*val) if val else it.next() co, val = n[0], n[1:] it = fibers.get(co) if it is None: fibers[co] = it = co() except StopIteration: return val[0] if val else None # Now the two 'fibers' def f1(): print "f1 start"

Re: fiber(cooperative multi-threading)

2007-12-22 Thread Arnaud Delobelle
On Dec 22, 2:37 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > I am not really familiar with ruby but these fibers seem to be some > > sort of coroutines.  Since python 2.5, generators can be sent values, > > this

Re: Modify arguments between __new__ and __init__

2007-12-23 Thread Arnaud Delobelle
gt; >>> s = Spam('spam and eggs', 'tomato', 'beans are off') > > __new__ ('spam and eggs', 'tomato', 'beans are off') > __init__ ('spam and eggs', 'tomato', 'beans are off') > > Is there any way to do this, or am I all outta luck? > > -- > Steven The ususal way is to override the __call__ method of the metaclass. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: list in a tuple

2007-12-24 Thread Arnaud Delobelle
ble Traceback (most recent call last): File "", line 1, in File "tuple.py", line 4, in __setitem__ raise TypeError("'mytuple' object is immutable") TypeError: 'mytuple' object is immutable >>> a ([1, 3], 2) It wouldn't slow anything down I suppose, just make some currently failing code succeed. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: list in a tuple

2007-12-24 Thread Arnaud Delobelle
On Dec 24, 4:08 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] > class mytuple(tuple): >     "It's ok to do t[i] = obj as long as t[i] was already obj" >     def __setitem__(self, i, val): >         if self[i] is not val: >             raise TypeError(&qu

Re: list in a tuple

2007-12-24 Thread Arnaud Delobelle
this thread. * It gave a motivation for the 'mytuple' class. I'm not sure what I think about it yet :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: list in a tuple

2007-12-26 Thread Arnaud Delobelle
is mutable a += c # equivalent to a.__iadd__(c) b is a # -> True OTOH augmented assignent are a slight optimisation: a[i] += 1 will look for the value of a and i only once and duplicate them on the stack, whereas a[i] = a[i] + 1 will need to resolve a and i twice (which can be costly if a and i are globals) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Bizarre behavior with mutable default arguments

2007-12-31 Thread Arnaud Delobelle
bout the current behaviour is that it is easy to reason with (once you know what happens), even though you almost have to get bitten once. But using this to have static variable is extremely ugly IMHO. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Unexpected __metaclass__ method behavior

2007-12-31 Thread Arnaud Delobelle
: def foo(self): return 'instance foo' @classmethod def classfoo(cls): return 'class foo' def do(x): if isinstance(x, type): return x.classfoo() else: return x.foo() Then: >>> bar = Bar() >>> do(bar) 'instance foo' >>> do(Bar) 'class foo' HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Bizarre behavior with mutable default arguments

2008-01-01 Thread Arnaud Delobelle
On Jan 1, 5:26 am, NickC <[EMAIL PROTECTED]> wrote: > On Jan 1, 3:22 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > > On Dec 31, 10:58 am, Odalrick <[EMAIL PROTECTED]> wrote: > > > > I'm surprised noone has said anything about the why of default

Re: Bizarre behavior with mutable default arguments

2008-01-01 Thread Arnaud Delobelle
kwargs.setdefault(name, val) return f(*args, **kwargs) return decorated return decorator Here is your example: >>> z=1 >>> @default(z=z) ... def foo(a, z): ... print a + z ... >>> z=None >>> foo(3) 4 Another example, using mutables: >>> @default(history=[]) ... def bar(x, history): ... history.append(x) ... return list(history) ... >>> map(bar, 'spam') [['s'], ['s', 'p'], ['s', 'p', 'a'], ['s', 'p', 'a', 'm']] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Fate of itertools.dropwhile() and itertools.takewhile()

2008-01-03 Thread Arnaud Delobelle
now.year) >     if os.path.exists(filename): >         candidates = ("%s.%d" % (filename, x) for x in count(1)) >         filename = dropwhile(os.path.exists, candidates).next() > > Much clearer than the alternatives I think, please keep dropwhile and > takewhile in ite

Re: New-style objects are not instances, apparently

2008-01-03 Thread Arnaud Delobelle
was the case with instances of old-style classes. So in your case isinstance(MyClass(), Exception) will return True. > Is there one, or is > the distinction between traditional classes and built-in types only > going to get more and more hazy? I'm not sure what you mean here. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: adding class functionality, nested scoping

2008-01-04 Thread Arnaud Delobelle
setattr(cls, methodname, types.MethodType(_f, None, cls)) Of course this looks more like your first version, which trims down to the following and is probably a better option: def enhance_all_methods(cls, replacement): for methodname in cls.__dict__: if not methodname.s

Re: Details about pythons set implementation

2008-01-04 Thread Arnaud Delobelle
is actually std::set< T, std::less > So make your own less_complex() function (for example, use lexicographic order), std::set should give you a set of complex numbers (sorry if some syntax is incorrect I haven't done any C++ for a while :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Details about pythons set implementation

2008-01-04 Thread Arnaud Delobelle
f complex numbers. Any total order will do, the easiest to pick being lexicographical as already pointed out, i.e. a+bi < c + di iff a < c or a == c and b < d > Which is bigger, 4+2j or 2+4j? 4+2j, lexicographically! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Questions about subclassing an int

2008-01-04 Thread Arnaud Delobelle
ent call last): >    File "", line 1, in ? > TypeError: an integer is required > > Now it's no longer a syntax error but I don't see why it's different? Same as above, though I don't understand why you get a SyntaxError for T and a TypeError for R. AFAICT both shoult give a TypeError. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: fastest method to choose a random element

2008-01-05 Thread Arnaud Delobelle
6, 15: 101, 17: 109, 19: 97} >>> p = picker(range(20), lambda x: False, 10) >>> p.next() Traceback (most recent call last): File "", line 1, in StopIteration I don't know if there is a good idea in there, I'll let you be the judge :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: fastest method to choose a random element

2008-01-05 Thread Arnaud Delobelle
On Jan 5, 8:50 pm, Paul Hankin <[EMAIL PROTECTED]> wrote: > On Jan 5, 5:12 pm, Paul Hankin <[EMAIL PROTECTED]> wrote: > > > > > On Jan 5, 4:14 pm, [EMAIL PROTECTED] wrote: > > > > On Jan 5, 5:07 pm, [EMAIL PROTECTED] wrote: > > > > > Hello,

Re: mod-python on Mac OSX 10.5

2008-01-08 Thread Arnaud Delobelle
x86_64" $(SRCS) $(LDFLAGS) $ (LIBS) Now that I look at this, I don' know if both are necessary... But it worked for me. There was a discussion to the mod_python mailing list in october 2007: http://www.modpython.org/pipermail/mod_python/2007-October/ -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Another dumb scope question for a closure.

2008-01-09 Thread Arnaud Delobelle
on't work here). Note that the 'nonlocal' keyword solves this problem in py3k: Python 3.0a1+ (py3k:59330, Dec 4 2007, 18:44:39) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def inc(j): ... def f(): ... nonlocal j ... j += 1 ... return j ... return f ... >>> i = inc(3) >>> i() 4 >>> i() 5 >>> -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop in a loop?

2008-01-17 Thread Arnaud Delobelle
gen = repeat(default, len(iterables) - 1) return izip(*[chain(it, repeatnext(defaultgen)) for it in iterables]) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Is this a bug, or is it me?

2008-01-19 Thread Arnaud Delobelle
he staticmethod decorator's role is to 'flag up' the method as static for further processing when type(C).__new__ is called. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Is this a bug, or is it me?

2008-01-19 Thread Arnaud Delobelle
ould compile to: class A: a = 42 def _genexpr(_a): for _ in "a": yield _a list(_genexpr(a)) Which would behave as the OP expected (and I think as it is reasonable to expect if one doesn't know about how genexprs are implemented). Other reasons why I think this is desirable are exposed (maybe not very convincingly) in this post to python-ideas. http://mail.python.org/pipermail/python-ideas/2007-December/001260.html -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Default attribute values pattern

2008-01-19 Thread Arnaud Delobelle
>     del(argdict[key]) >   else: >     retval = default >   return retval > Dictionaries already have a method for this. It's called pop. It's a good idea to have a look at methods of builtin types before reimplementing the wheel! Grab(argdict, key, default) is argdict.pop(key, default) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Bug in __init__?

2008-01-20 Thread Arnaud Delobelle
re seems to be about one message a week about this. Indeed I reckon the greatest benefit of early binding of default function arguments is that it attracts lots of new people to comp.lang.python. > It's quiet elegant way of creating cache. IMHO, calling it 'elegant' is pushin

Re: Just for fun: Countdown numbers game solver

2008-01-20 Thread Arnaud Delobelle
untown(numbers, target) -> None -- print all countdown solutions""" for dummy in calc(numbers, print_filter(target)): pass def best_countdown(numbers, target): """best_countdown(numbers, target) -> str -- return shortest solution"""

Re: Just for fun: Countdown numbers game solver

2008-01-20 Thread Arnaud Delobelle
3))*(2+1)) 283 = (3+(((5*4)*2)*(6+1))) 284 : can't be done For 2, 3, 5, 8, 9, 10, the smallest unreachable target is 796 (and there are five others: 829, 956, 961, 964, 991). For 2, 4, 5, 8, 9, 10, the smallest is 807 (but there are 23 others) Time to go to bed :) -- Arnaud PS: apolo

Re: Just for fun: Countdown numbers game solver

2008-01-20 Thread Arnaud Delobelle
On Jan 21, 1:07 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > On Jan 20, 5:41 pm, [EMAIL PROTECTED] wrote: > > > Ever since I learnt to program I've always loved writing solvers for > > the Countdown numbers game problem in different languages > > Ok so h

Re: Just for fun: Countdown numbers game solver

2008-01-20 Thread Arnaud Delobelle
(100, 7, 'sub', 3, 'div', 1, 'sub', 9, 'add', 6, 'mul') >         (7, 3, 'mul', 6, 'sub', 9, 'mul', 1, 'sub', 100, 'add') >         (100, 7, 'mul', 6, 'sub', 1, 'sub', 9, 'add', 3, 'div') >         (100, 9, 'add', 7, 'add', 1, 'add', 3, 'div', 6, 'mul') >         (100, 9, 'sub', 7, 'div', 6, 'mul', 3, 'mul', 1, 'mul') In countdown you are not required to use all numbers to reach the target. This means you are missing solutions, e.g. (1, 3, 6, 'mul', 'add', 7 , 'add', 9, 'mul') After a quick glance at your code it seems to me that you can only have solutions of the type: (num, num, op, num, op, num, op, num, op, num, op) this omits many possible solutions (see the one above). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-21 Thread Arnaud Delobelle
ant > version (on my webpage linked to in the original post) takes about 15 > seconds. It seems to me like surely this problem can be more > efficiently solved than that? I haven't had the time to look at your solution yet. I wanted to have a go without being influenced. > Arnaud:

Re: Just for fun: Countdown numbers game solver

2008-01-21 Thread Arnaud Delobelle
On Jan 21, 9:12 am, Terry Jones <[EMAIL PROTECTED]> wrote: [...] > Hi Arnaud. Hi Terry [...] > WRT to the missing solution, note that my code only allowed multiplication > by 1 if it was the last thing done. That was because you can multiply by 1 > at any time, and I didn&#

Re: Default attribute values pattern

2008-01-21 Thread Arnaud Delobelle
hes > the KeyError. If you really really want to write a grab function (IMHO pop is good enough): def grab(kw, key, default=None): return kw.pop(key, default) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-21 Thread Arnaud Delobelle
On Jan 21, 9:01 am, [EMAIL PROTECTED] wrote: > Arnaud: I haven't had time to play with your solution yet - how quick > does it run? Ok I've done some quick timings, it's faster than I remembered it: numbers = [2, 4, 5, 8, 25] target = 758 Average time to find (1) best

Re: Prioritization function needed (recursive help!)

2008-01-21 Thread Arnaud Delobelle
't know if > that's the best approach.  See my other post on this. > > Thanks There's a very recent thread on this subject (topological sort) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-21 Thread Arnaud Delobelle
'//', '-+', '--'] def pretty(h): "Print a readable form of a number's history" if isinstance(h, int): return str(h) else: x, op, y = h x, y, xop, yop = pretty(x), pretty(y), getop(x), getop(y) if xop + op in lbracket: x = "(%s)" % x if op + yop in rbracket: y = "(%s)" % y return ''.join((x, op, y)) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: pairs from a list

2008-01-21 Thread Arnaud Delobelle
; list(pairs(xrange(10))) [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] >>> list(pairs('hello')) [('h', 'e'), ('l', 'l')] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: pairs from a list

2008-01-22 Thread Arnaud Delobelle
rom left to right, so there is no risk that e.g. pairs4([1, 2, 3, 4]) returns [(2, 1), (4, 3)]. Is there anything else that I am overlooking? [1] http://docs.python.org/lib/itertools-functions.html -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: pairs from a list

2008-01-22 Thread Arnaud Delobelle
'import pairs; l=range(1000)') print 'pairs%s: %s' % (i, t.timeit(1)) if __name__ == '__main__': compare() = marigold:python arno$ python pairs.py pairs1: 0.789824962616 pairs2: 4.08462786674 pairs3: 2.90438890457 pair

Re: pairs from a list

2008-01-22 Thread Arnaud Delobelle
On Jan 22, 4:10 pm, Alan Isaac <[EMAIL PROTECTED]> wrote: > http://bugs.python.org/issue1121416> > > fwiw, > Alan Isaac Thanks. So I guess I shouldn't take the code snippet I quoted as a specification of izip but rather as an illustration. -- Arnaud -- http://mail.p

Re: pairs from a list

2008-01-22 Thread Arnaud Delobelle
ought might justify leaving the routine alone, or > optimising for readability instead. But it's fun! Some-of-us-can't-help-it'ly yours -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: question

2008-01-22 Thread Arnaud Delobelle
time I'd love to hear your thoughts. > > Thanks. > > Jay What you want is a dictionary: albumInfo = { 'Rush': 'Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres'], 'Enchant': ['A Blueprint of the World', 'Wounded', 'Time Lost'], ... } then to find the info just do: >>> albumInfo['Enchant'] ['A Blueprint of the World', 'Wounded', 'Time Lost'] It also makes it easy to add a new album on the fly: >>> albumInfo["Lark's tongue in Aspic"] = [ ... ] Hope that helps. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-22 Thread Arnaud Delobelle
On Jan 22, 9:05 am, Terry Jones <[EMAIL PROTECTED]> wrote: > Hi Arnaud > > > I've tried a completely different approach, that I imagine as 'folding'.  I > > thought it would improve performance over my previous effort but extremely > > lim

Re: Just for fun: Countdown numbers game solver

2008-01-22 Thread Arnaud Delobelle
On Jan 22, 10:56 pm, [EMAIL PROTECTED] wrote: > Arnaud and Terry, > > Great solutions both of you! Much nicer than mine. I particularly like > Arnaud's latest one based on folding because it's so neat and > conceptually simple. For me, it's the closest so far to

Re: Just for fun: Countdown numbers game solver

2008-01-23 Thread Arnaud Delobelle
On Jan 23, 3:45 pm, Terry Jones <[EMAIL PROTECTED]> wrote: > Hi Arnaud and Dan Hi Terry > >>>>> "Arnaud" == Arnaud Delobelle <[EMAIL PROTECTED]> writes: > >> What was wrong with the very fast(?) code you sent earlier? > > Arnaud>

Re: pairs from a list

2008-01-23 Thread Arnaud Delobelle
On Jan 23, 7:06 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > The OP wanted an answer to a simple question, not a lecture on good > software engineering principles. I wholeheartedly agree. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-23 Thread Arnaud Delobelle
On Jan 23, 8:40 pm, Terry Jones <[EMAIL PROTECTED]> wrote: > >>>>> "Arnaud" == Arnaud Delobelle <[EMAIL PROTECTED]> writes: > > Arnaud> FWIW, I have a clear idea of what the space of solutions is, and > Arnaud> which solutions I consider to be

Re: Creating new types and invoking super

2008-01-23 Thread Arnaud Delobelle
ss A(object): >     @init >     def __init__(self): pass This kind of approach can't work, you need to call super(A, obj). super(type(obj), obj) is pointless, it defeats the whole purpose of the mro! The only way I can think of would be to create a metaclass, but I don't think it

Re: Creating new types and invoking super

2008-01-23 Thread Arnaud Delobelle
On Jan 23, 10:18 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote: > 2008/1/23, Arnaud Delobelle <[EMAIL PROTECTED]>: > > > The only way I can think of would be to create a metaclass, but I > > don't think it's worth it.  super(A, obj).__init__(

Re: Creating new types and invoking super

2008-01-23 Thread Arnaud Delobelle
On Jan 23, 11:51 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: Oops: > > class callsuper(object): >     def __init__(self, f): >         self.f = f >     def __get__(self, obj, cls=None): >         def newfunc(*args, **kwargs): >             super(self.cls, obj).__in

Re: Smart factory class

2008-01-23 Thread Arnaud Delobelle
()): return ns[classname]() ... >>> class A: pass ... >>> mkobj('A') <__main__.A instance at 0x6bd28> >>> But why do you want to do this? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating new types and invoking super

2008-01-23 Thread Arnaud Delobelle
On Jan 24, 7:19 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: Oops again > > Change this line to: >               getattr(super.cls, obj), self.f.__name__)() I mean getattr(super(self.cls, obj), self.f.__name__)() -- Arnaud -- http://mail.python.org/mailman/

Re: Just for fun: Countdown numbers game solver

2008-01-26 Thread Arnaud Delobelle
I can interface it with my little testing suite. Thanks for posting it! It was a lot of fun thinking about this, although I am sure that there is a lot of room for improvement. In particular, there must be a simple way to avoid the amount of list tinkering in fold(). Any feedback greatly appr

Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
hmark(posmax): from time import clock alist = [3]*1000 + [5] + [3]*1000 t = clock() for _ in xrange(6000): r = posmax(alist) print posmax.__name__, round(clock() - t, 2), r if __name__ == "__main__": posmax_benchmark(clever_posmax) posmax_benchmark(simple_posmax) = marigold:python arno$ python posmax.py clever_posmax 3.25 1000 simple_posmax 0.95 1000 simple_posmax is more than 3x faster on my machine. It's not surprising as even though the list is walked twice, it is all done in C and no new objects have to be created. Then only non-C bit is when the result of max(l) is fed to l.index(). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:42 am, Paul Rubin <http://[EMAIL PROTECTED]> wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> writes: > > def simple_posmax(l, key=None): > >     if key: > >         return l.index(max(l, key=key)) > >     else: > >         return

Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:32 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] > > simple_posmax is more than 3x faster on my machine.  It's not > surprising as even though the list is walked twice, it is all done in > C and no new objects have to be created. Then only non-C bit

Python self-evaluating strings

2008-01-27 Thread Arnaud Delobelle
gates well. Both u and v are one- liners. I'm hoping for no funky line wrapping here. Note: I'm not quoting the string as it makes no difference since they evaluate to themselves:) I'd like to know if anyone on the list has indulged in this time- bending/mind-wasting activity before. If so, it would be nice to create a list of such expressions. Quining's-better-than-ironing'ly yours -- Arnaud [1] http://www.nyx.net/~gthompso/self_pyth.txt -- http://mail.python.org/mailman/listinfo/python-list

Re: explicit protocols and duck typing

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 7:10 pm, [EMAIL PROTECTED] wrote: > Hi all, > > As I understand it, the idea behind duck typing is that you just take > an object and if it has the methods you want to use you use it > assuming it to be the right type of object. I'm interested in > extending this idea a bit, but I feel t

Re: optional static typing for Python

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:00 pm, "Russ P." <[EMAIL PROTECTED]> wrote: > On Jan 27, 2:49 pm, "André" <[EMAIL PROTECTED]> wrote: > > Perhaps this:http://www.python.org/dev/peps/pep-3107/mightbe > > relevant? > > André > > Thanks. If I read this correctly, this PEP is on track for Python 3.0. > Wonderful! Note t

Re: Python self-evaluating strings

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 4:14 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] > 1. Starting from the classic idea (lambda x:x%x)('lambda x:x%x') I got   > the following > v=(lambda x:x%('"''""'+x+'"''""'))

Re: Python self-evaluating strings

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:58 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Arnaud Delobelle" <[EMAIL PROTECTED]> wrote in message > Some years ago there was a substantial thread on this, (Shortest > Self-Reproducing Programs, or some such) including a fairly long

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-27 Thread Arnaud Delobelle
ted return decorator class Test(object): @autoassign('foo', 'bar') def __init__(self, baz): print 'baz =', baz t = Test(foo=1, bar=2, baz=6) # or Test(6, foo=1, bar=2) print t.foo print t.bar print t.baz -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-27 Thread Arnaud Delobelle
got default values, type annotations > (in Python3), *args and **kwargs, _ private names, and now we want to add > auto-assignment. Don't forget keyword only arguments! I find signatures too complicated already, please let's not clutter them even more. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-28 Thread Arnaud Delobelle
On Jan 28, 4:47 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Sun, 27 Jan 2008 23:51:28 -0200, Arnaud Delobelle > <[EMAIL PROTECTED]> escribió: > > > Nice! I've got a slight variation without magic argument names: > > > class Test(objec

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-28 Thread Arnaud Delobelle
s why inspect may be preferable -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-28 Thread Arnaud Delobelle
t', 'baz'): try: getattr(globals()[obj], attr) except AttributeError: print '%s.%s raises AttributeError' % (obj, attr) output baz = 5 baz = 6 1 2 8 3 10 11 5 102 w.foo raises AttributeError w.bar raises AttributeError t.baz raises AttributeError -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-28 Thread Arnaud Delobelle
57] (list returned contains time taken to reach solution. By comparison your code takes 0.264s on my machine. Mine is faster on this example, but one example is not representative!) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: optional static typing for Python

2008-01-28 Thread Arnaud Delobelle
On Jan 28, 4:31 pm, John Nagle <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: [...] > > Note that annotations do not provide explicit typing, AFAIK: > > > def f(x:int) -> int: return x*2 > > > is stricly equivalent to > > > def f(x): return x*2 &

Re: py3k feature proposal: field auto-assignment in constructors

2008-01-28 Thread Arnaud Delobelle
other area that was mentioned obliquely: preservation of > docstrings (and other function attributes) I think @wraps(...) does this (definitely copies __doc__). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python self-evaluating strings

2008-01-28 Thread Arnaud Delobelle
On Jan 29, 3:48 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Arnaud Delobelle" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > | I found > this:http://groups.google.com/group/comp.lang.python/browse_thread/thread/... > >

Re: extending Python - passing nested lists

2008-01-29 Thread Arnaud Delobelle
bject *item) Append the object item at the end of list list. Return 0 if successful; return -1 and set an exception if unsuccessful. Analogous to list.append(item). > > } > > Can somebody give me a hint here, please? Passing simple arguments to and > fro (e. g. single integer values) is no problem, but lists of unknown size? HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-29 Thread Arnaud Delobelle
de... My code is quite naive and I suspect that combining your caching and the tree pruning discussed in this thread would yield faster results. Not sure if I'll have time to try this though... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: extending Python - passing nested lists

2008-01-29 Thread Arnaud Delobelle
g array.array? Unless I am mistaken, these are just a thin wrapper around normal C arrays. Anyway you could always convert your list into a c array, do lots and lots of fast calculations, then convert it back again to a list. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Just for fun: Countdown numbers game solver

2008-01-29 Thread Arnaud Delobelle
On Jan 29, 9:02 am, [EMAIL PROTECTED] wrote: > If you've found an efficient way to walk through the possible > solutions only once As discussed earlier in this thread, the definition of 'only once' is not as clear cut as one would first think (see Terry's thoughts on t

Re: Executing other python code

2008-01-29 Thread Arnaud Delobelle
ript(scriptname): script = __import__(scriptname) for obj in scriptobjs: setattr(script, obj.__name__, obj) return script def playscript(script): script.play() === Something like this. Obviously this is over simplified! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: extending Python - passing nested lists

2008-01-29 Thread Arnaud Delobelle
*_foo(PyObject *self, PyObject *args) { >   double *v; >   if (!PyArg_Parse(args, "(d)", &v)) >     return NULL; > to get a list as an array of doubles into 'v' (and back to Python)? You can always iterate over the elements of the list an fill your c array with the results. Look at the array_fromlist() function in arraymodule.c, this function does exactly this. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: breaking out of outer loops

2008-01-29 Thread Arnaud Delobelle
  for arg in args: >         ans = [x+[y] for x in ans for y in arg] >     return ans     While we're at it, a generator version: def iproduct(head=None, *tail): if head is None: return ((),) else: return ((x,)+y for x in head for y in iproduct(*tail)) for a,

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Arnaud Delobelle
dex] Or one could use the trick of counting from the right (untested): n = len(a) for i, x in enumerate(a): if x == 99: del a[i-n] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread Arnaud Delobelle
On Jan 30, 11:57 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > n = len(a) > for i, x in enumerate(a): >     if x == 99: del a[i-n] Oops. That can't work. Don't know what I was thinking here. I probably did had one mental refactoring too many... -- Arnaud -- http:

Re: list traversal and remove

2008-01-31 Thread Arnaud Delobelle
ps.google.com/group/comp.lang.python/browse_thread/thread/290623baaa46e72a/ -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread Arnaud Delobelle
inside = {} for x, i in sorted(bounds): if inside.pop(i, None) is None: for j, y in inside.iteritems(): if y != x: yield i, j inside[i] = x == >>> list(overlaps(lst)) [(1, 2), (3, 0)] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: helper function in a class' namespace

2008-01-31 Thread Arnaud Delobelle
t; 'H> %s' % M Traceback (most recent call last): File "", line 1, in TypeError: not all arguments converted during string formatting Pedantically yours, -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: helper function in a class' namespace

2008-01-31 Thread Arnaud Delobelle
class A(object): ... def _helper(x): return '<<%s>>' % x ... def __init__(self, msg, _helper=_helper): ... print _helper(msg) ... del _helper ... >>> A('spam') <> <__main__.A object at 0x70170> Confusingly yours, -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: dict comprehension

2008-01-31 Thread Arnaud Delobelle
for more information. >>> {x*x for x in range(10)} {0, 1, 4, 81, 64, 9, 16, 49, 25, 36} >>> {x:x*x for x in range(10)} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81} >>> That's nice. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: PLEASE ACCEPT MY SINCERE APOLOGIES

2008-01-31 Thread Arnaud Delobelle
t; > prohibited. Please reply to the message immediately by informing the sender > > that the message was misdirected. After replying, please erase it from your > > computer system. Your assistance in correcting this error is appreciated. > > Could you now specifically apologize for all those question > marks!!! > > :-) > > Peace, Paddy. And for top-posting :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How to identify which numbers in a list are within each others' range

2008-02-01 Thread Arnaud Delobelle
On Feb 1, 11:23 am, Thomas Pani <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > > This is definitely the best way: > > > from itertools import chain > > > def overlaps(lst): > >     bounds = chain(*(((x[1],i), (x[2], i)) for i,x in enumerate(lst)

Re: How to identify which numbers in a list are within each others' range

2008-02-01 Thread Arnaud Delobelle
On Feb 1, 1:28 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > Yours  is O(n^2) and \Omega(n^2).  I think mine is O(max(c, nlogn)) > (assuming constant time access for dictionaries, but 'inside' could be > replaced with a list) where c is the number of overlaps.  I

Re: How to identify which numbers in a list are within each others' range

2008-02-01 Thread Arnaud Delobelle
On Feb 1, 2:44 pm, "Neil Cerutti" <[EMAIL PROTECTED]> wrote: > On Feb 1, 2008 8:28 AM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > > Yours  is O(n^2) and \Omega(n^2).  I think mine is O(max(c, nlogn)) > > (assuming constant time access for dictionaries,

Re: How to identify which numbers in a list are within each others' range

2008-02-01 Thread Arnaud Delobelle
strips.sort(cmp=strip_sort) >     for s in strips: >         node = s[2] >         if s[1] == 'L': >             clique.add(node) >         if s[1] == 'R': >             new_edges = [(node, i) for i in clique if i != node] >             edges += new_edges >    

Re: How to identify which numbers in a list are within each others' range

2008-02-01 Thread Arnaud Delobelle
On Feb 1, 8:34 pm, "Neil Cerutti" <[EMAIL PROTECTED]> wrote: > On Feb 1, 2008 3:16 PM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > The total number of iterations is 1+2+...+n = n(n+1)/2 (when n is the > > number of intervals) so this has quadratic behaviou

Re: bags? 2.5.x?

2008-02-02 Thread Arnaud Delobelle
sets, then - multiset(A).union(multiset(B)) == multiset(A.union(B)) - multiset(A).intersection(multiset(B)) == multiset(A.intersection(B)) (same for difference, symmetric_difference) I think this is how I would like my multisets :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: bags? 2.5.x?

2008-02-03 Thread Arnaud Delobelle
On Feb 2, 5:28 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Sat, 2008-02-02 at 01:17 -0800, Paul Rubin wrote: > > Arnaud Delobelle <[EMAIL PROTECTED]> writes: > > > * For sets {x, y} union {y, z} = {x, y, z}.  The natural way of > > >   extending this to

<    1   2   3   4   5   6   7   8   9   10   >