Re: dict comprehension

2008-02-03 Thread Arnaud Delobelle
ifficult thing to do? Yes, due to the HDNP (Highly Dynamic Nature of Python). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: bags? 2.5.x?

2008-02-03 Thread Arnaud Delobelle
ch contains either of the bags). I agree, and this is what I proposed in my post (except that I spelt A| B as A.union(B)) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python feature request : operator for function composition

2008-02-03 Thread Arnaud Delobelle
gt; Alternatives > - > One could use other unused operators like F << G or F * G to write > terse function composition expressions. I' m not sure which one is > best and would use the proposed & if no one presents arguments against > it. What about other callable objects? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Does anyone else use this little idiom?

2008-02-03 Thread Arnaud Delobelle
action in repeat(f, n): action() I don't know how 'Pythonic' this would be... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python feature request : operator for function composition

2008-02-03 Thread Arnaud Delobelle
On Feb 3, 9:43 am, Kay Schluehr <[EMAIL PROTECTED]> wrote: > On 3 Feb., 10:13, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > > > > On Feb 3, 5:09 am, Kay Schluehr <[EMAIL PROTECTED]> wrote: > > > > As you know, there is no operator for functio

Re: Is it explicitly specified?

2008-02-03 Thread Arnaud Delobelle
to create an object which is only ever used as the default arguement: nokey = object() def func(key=nokey): if key is nokey: # key not given else: # key given -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Is it explicitly specified?

2008-02-03 Thread Arnaud Delobelle
eople prefer the > pseudo-default or sentinal value approach given by Arnaud.  The only change > I would make from his post is to add at least an underscore to the sentinal > name to indicate that it is private to the module and not for use of > importers (and excluded from 'import

Re: type, object hierarchy?

2008-02-03 Thread Arnaud Delobelle
bclass(Dog, object)  #True > print issubclass(type, object)  #True > print issubclass(Dog, type)   #False Are you suggesting a new axiom for propositional logic: ((P => Q) ^ (R => Q)) => (P => R) ? I.e. in fruit logic: every orange is a fruit and every apple is a

Re: Python feature request : operator for function composition

2008-02-04 Thread Arnaud Delobelle
double(4) > > 64 > > Probably not the best implementation, but you get the idea. This is nice. * I wouldn't choose '&' as the composing operator as when I read 'double & square' I think 'take an x, double it & square it' which is the wrong interpretation (perhaps << instead?). * I would call the decorator 'composable'. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python-URL! - weekly Python news and links (Feb 4)

2008-02-04 Thread Arnaud Delobelle
onal problems that everyone goes through. > You'll learn a lot of useful things in the process." - Peter Hansen Maybe that should go in the FAQ ;-) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: IronPython vs CPython: faster in 1.6 times?

2008-02-05 Thread Arnaud Delobelle
time:%5.2f s' % ( time.time() - start) Could it be because .NET doesn't have arbitrary length integer types and your little benchmark will create lots of integers > 2**32 ? What is the result if you replace foo(a) with def foo(a): return sqrt(a) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Why does list have no 'get' method?

2008-02-07 Thread Arnaud Delobelle
bearophile Personally, between * foo if foo else bar * foo or bar I prefer the second. Maybe it could be spelt * foo else bar ? Moreover between the following two: * foores = foo() foobar = foores if foores else bar * foobar = foo() or bar I also prefer the second. Maybe it could

Re: Distinguishing between functions and methods in a decorator.

2008-02-07 Thread Arnaud Delobelle
in the latter case, > the first parameter will be the implicit 'self', which I would like to > ignore. However, when the wrapping occurs, the method still looks like > a function. > > Berteun I think you're better off having a 'wrap' decorator for functions and a 'wrapmethod' decorator for methods. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Code block function syntax, anonymous functions decorator

2008-02-07 Thread Arnaud Delobelle
x27;s called '@apply' (I don't know what your @call returns): @apply def the_answer(x=6): return x*(x+1) print the_answer :-) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Why not a Python compiler?

2008-02-07 Thread Arnaud Delobelle
> the compiler could do little else except translate it to something > like: > > (python:add a b) [snip more interesting considerations about compiling python] Please get back on topic. This discussion is about parsecs and wookies now. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Critique of first python code

2008-02-08 Thread Arnaud Delobelle
of non iterables in L.''' >    if L == []: >       return count >    n = [] >    for e in L: >       if not hasattr(e,'__iter__'): >          print e, >          count += 1 >       else: >          n[:] += e I would write n.extend(e) here (or n += e) >    print '\n' >    return traverse(n,count) > > L = grow([],10) > C = traverse(L) > ## > > -- > Zack -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: are elements of a list in sequence in list b

2008-02-08 Thread Arnaud Delobelle
) except IndexError: return True issubseq('m&e', 'spam&eggs) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: keyword 'in' not returning a bool?

2008-02-08 Thread Arnaud Delobelle
Typical usage is: if 3 <= x < 9: # meaning (3 <= x) and (x < 9) # do something... So 't' in sample == True means the same as ('t' in sample) and (sample == True) Which will never be true as sample is a dictionary. To solve this, use brackets: >>> ('t' in sample) == True True HTH -- 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-08 Thread Arnaud Delobelle
On Feb 8, 1:48 pm, Boris Borcic <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > (...) > > > > > from itertools import chain > > > def overlaps(lst): > >     bounds = chain(*(((x[1],i), (x[2], i)) for i,x in enumerate(lst))) > &

Re: keyword 'in' not returning a bool?

2008-02-08 Thread Arnaud Delobelle
True > > It's operator precedence. 'in' has lower precedence than '=='. Therefore >     't' in sample == True > evaluates as >     't' in (sample == True) > > The real question is why does >     't' in (sample == True) > cause an error:   >     TypeError: argument of type 'bool' is not iterable > while >     't' in sample == True > does not? That should have told you precedence is not the reason! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: multi-Singleton-like using __new__

2008-02-08 Thread Arnaud Delobelle
the metaclass. By default is calls cls.__new__ then cls.__init__, e.g.: class RDFObject(object): # This metaclass prevents __init__ from being automatically called # after __new__ class __metaclass__(type): def __call__(cls, *args, **kwargs): return cls.__new__(cls, *args, **kwargs) # ... HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: multi-Singleton-like using __new__

2008-02-09 Thread Arnaud Delobelle
obj = cls._cache[uri] = object.__new__(cls) obj.__init__(uri, *args, **kwargs) return obj It doesn't look up the cache sho much and I think reads just as well. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Edit Python code programmatically

2008-02-09 Thread Arnaud Delobelle
? > > inspect is a module, inspect is the name. It is not a module for > editing Python code per se, but it will help with the other part. I don't think the OP wants to edit python code *objects*, rather he wants to edit python *source* code programmatically. Inspect is not the tool for this. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

An idea for fast function composition

2008-02-16 Thread Arnaud Delobelle
y lookups (LOAD_GLOBALs) in f2. A C version of 'compose' could easily be written that doesn't require the use of a python lambda-function (as created by 'getcomposer'). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: problem with mod_python

2008-02-16 Thread Arnaud Delobelle
bove problem are welcome too. > thanks! Have you tried the mod_python mailing list? (see http://www.modpython.org/) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
eritems())) else: return (pfx, d), test = {"mays" : {"eggs" : "spam"}, "jam" : {"soda" : {"love" : "dump"}}, "lamba" : 23 } >>> print dict(flattendict(test)) {'lamba': 23, 'mays/eggs': 'spam', 'jam/soda/love': 'dump'} You an even have other separators ;) >>> print dict(flattendict(test, sep='.')) {'lamba': 23, 'jam.soda.love': 'dump', 'mays.eggs': 'spam'} -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: class static variables and __dict__

2008-02-17 Thread Arnaud Delobelle
ss__.__dict__ >     tmpD.update(d) >     d = tmpD >     supers = list(obj.__class__.__bases__) >     for c in supers: >        tmpD = c.__dict__ >        tmpD.update(d) >        d = tmpD >        supers.extend(c.__bases__) >     return d > > -- > Zack Child class attributes override base class ones, so your function will get the wrong dict I think in cases like: class A(object): x = 1 class B(A): x = 2 Why not do something like this: def fulldict(obj): d = {} for t in reversed(type(obj).__mro__): d.update(t.__dict__) d.update(obj.__dict__) return d -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
On Feb 17, 12:18 pm, Terry Jones <[EMAIL PROTECTED]> wrote: > Hi Arnaud & Benjamin > > Here's a version that's a bit more general. It handles keys whose values > are empty dicts (assigning None to the value in the result), and also dict > keys that are not stri

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
On Feb 17, 4:03 pm, Boris Borcic <[EMAIL PROTECTED]> wrote: > George Sakkis wrote: > > On Feb 17, 7:51 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > >> BTW, I keep using the idiom itertools.chain(*iterable).  I guess that > >> during functio

Re: Seemingly odd 'is' comparison.

2008-02-18 Thread Arnaud Delobelle
e (LOAD_CONST 1 / LOAD_CONST 1 / COMPARE_OP 8). Whereas when "3.0*1.0 is 3.0" is evaluated, *two* different float objects are put on the stack and compared (LOAD_CONST 3 / LOAD_CONST 1 / COMPARE_OP 8). Therefore the result is False. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: flattening a dict

2008-02-18 Thread Arnaud Delobelle
from people who obfuscate multi- > expression functions by writing them as a generator expression. I'm sorry if my Python is difficult to understand. That's because I've got a bit of a Lisp... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Seemingly odd 'is' comparison.

2008-02-19 Thread Arnaud Delobelle
On Feb 19, 1:47 pm, Boris Borcic <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > On Feb 13, 10:19 pm, Tobiah <[EMAIL PROTECTED]> wrote: > >>>>> print float(3.0) is float(3.0) > >> True > >>>>> print float(3.0 * 1.0) is

Re: Threads vs. continuations

2008-02-19 Thread Arnaud Delobelle
On Feb 19, 8:26 pm, [EMAIL PROTECTED] wrote: [...] > The only thing preventing Python from being > that language is the difficulty of integrating a macro system, n'est- > ce pas? Well there's logix (http://www.livelogix.net/logix/) -- Arnaud -- http://mail.python.org/mail

Re: Return value of an assignment statement?

2008-02-23 Thread Arnaud Delobelle
(match, actions=actions, default_action=None): for string, action in actions: if match(string): return action return default_action find_action(re.compile('some pattern').match)() -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Article of interest: Python pros/cons for the enterprise

2008-02-24 Thread Arnaud Delobelle
t; (python 3.0) >> def counter(acc=0, step=1): ... def tick(): ... nonlocal acc ... acc += step ... return acc ... return tick ... >>> c = counter() >>> c() 1 >>> c() 2 >>> -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: PyEuler

2008-02-25 Thread Arnaud Delobelle
d a ;-) Ahem. I admit that somehow, I am proud of this. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python code for 2.5 and 2.4?

2008-02-25 Thread Arnaud Delobelle
2.5' not in version: >    # use custom all() script > > Or simply: try: all except NameError: def all(iterable): for x in iterable: if not x: return False return True -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: PyEuler

2008-02-25 Thread Arnaud Delobelle
ind it a bit more readable. In this case why not: def xfib(a=1, b=1): while True: yield a a += b yield b b += a -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: PyEuler

2008-02-25 Thread Arnaud Delobelle
On Feb 25, 10:52 pm, [EMAIL PROTECTED] wrote: > Arnaud Delobelle: > > > In this case why not: > > > def xfib(a=1, b=1): > >     while True: > >         yield a > >         a += b > >         yield b > >         b += a > > That's

Re: Python code for 2.5 and 2.4?

2008-02-25 Thread Arnaud Delobelle
On Feb 25, 11:17 pm, Paul Rubin <http://[EMAIL PROTECTED]> wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> writes: > >     def all(iterable): > >         for x in iterable: > >             if not x: return False > >         return True > > from itertools

Re: How about adding rational fraction to Python?

2008-02-26 Thread Arnaud Delobelle
given the digital nature of modern computers), and integers are the more natural numerical objects to associate with discrete things. As with the soccer example given by D'Arcy J.M. Cain, many objects can't be chopped in halves or quarters (especially in CS) therefore float division is no meaningless to them. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How about adding rational fraction to Python?

2008-02-26 Thread Arnaud Delobelle
et 0.20001 of a cake :) Or perhaps it would be better to have 2**n guests... Or maybe one should think of the cake as 1.0 cake, as experience shows that cakes can be cut... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Making string-formatting smarter by handling generators?

2008-02-27 Thread Arnaud Delobelle
f > parameters such as > >    "%s=%s&%s=%s" % map(urllib.quote, params) > > Any suggestions?  (even if it's just "get over your hangup with > wrapping the results in list()/tuple()" :) get over your hangup with wrapping the results in tuple()! (not list() though, as explained above). Or you could always define def tmap(*args): return tuple(map(*args)) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python's BNF

2008-02-27 Thread Arnaud Delobelle
On Feb 27, 5:23 pm, [EMAIL PROTECTED] wrote: > I spent too long Googling for Python's BNF. Eventually found it at > Python.org, but only by accident. http://www.google.com/search?q=python+grammar -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Making string-formatting smarter by handling generators?

2008-02-27 Thread Arnaud Delobelle
urlib.quote exactly once altogether. Whereas [urllib.quote(x) for x in params] looks for urlib.quote once for every element in params. (Of course in the example given params only has 4 elements so it doesn't matter). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Place n indistinguishable items into k distinguishable boxes

2008-02-28 Thread Arnaud Delobelle
t;"" seq = [n]*k + [0] while True: yield tuple(seq[i] - seq[i+1] for i in xrange(k)) i = seq.index(0) - 1 if i >= 1: seq[i:k] = [seq[i] - 1] * (k - i) else: return -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Place n indistinguishable items into k distinguishable boxes

2008-02-28 Thread Arnaud Delobelle
On Feb 28, 4:44 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > ... here is another attempt on the same principle: > > --- > def boxings(n, k): >     """boxings(n, k) -> iterator > >     Generate all ways to place n indistiguish

Re: Odd handling of type.__init__ bases

2008-02-28 Thread Arnaud Delobelle
def __new__(cls, name, bases, dict): bases += (abase,) return type.__new__(cls, name, bases, dict) HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How about adding rational fraction to Python?

2008-02-28 Thread Arnaud Delobelle
very sound decision. What screws me is that I'm going to have to type p//q in the future. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Nested module import clutters package namespace?

2008-02-28 Thread Arnaud Delobelle
'    y: Here is y.' > > = > > Looks like a bug to me - or perhaps someone who actually understands how   > import works could explain it? Say: pack/ __init__.py x.py importing x in any manner will automatically load pack and put x in pack's namespace. Try: from pack import x as foo sys.modules['pack'].x This explain the behaviour observed by the OP. AFAIK it is not a bug. The tutoria hints at this without going into too much detail. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How about adding rational fraction to Python?

2008-02-29 Thread Arnaud Delobelle
On Feb 29, 10:10 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Arnaud Delobelle" <[EMAIL PROTECTED]> wrote in message > > | What screws me is that I'm going to have to type p//q in the future. > > When I compare that pain to the gain of

Re: How about adding rational fraction to Python?

2008-03-01 Thread Arnaud Delobelle
On Mar 1, 5:49 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Arnaud Delobelle" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > | Perhaps it'll be like when I quit smoking six years ago.  I didn't > | enjoy it although I

Re: First post from a Python newbiw

2008-03-03 Thread Arnaud Delobelle
a[1] > > Apart from doing something like > a=[0,0,0] > b=[0,0,0] > c=[0,0,0] > d=[a,b,c] > > is there a better way of creating d?? It's a FAQ: http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Exception or not

2008-03-03 Thread Arnaud Delobelle
rn: [...] You could have a look at how django [http://www.djangoproject.com] and how they handle forms: http://www.djangoproject.com/documentation/newforms/ In fact their newforms module should work fine as a standalone. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How about adding rational fraction to Python?

2008-03-03 Thread Arnaud Delobelle
On Mar 3, 4:39 pm, Paul Rubin <http://[EMAIL PROTECTED]> wrote: [...] > You are right, C is even worse than I remembered. It's good enough to be the language used for the reference implementation of python :-) [...] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: any chance regular expressions are cached?

2008-03-10 Thread Arnaud Delobelle
ld' >>> spaces = " " >>> import re >>> r = re.compile('\\n') >>> from timeit import Timer >>> Timer("r.sub('\\n'+spaces, s)", "from __main__ import r,spaces,s").timeit() 1.7726190090179443 >>> Timer("s.replace('\\n', '\\n'+spaces)", "from __main__ import s, >>> spaces").timeit() 0.76739501953125 >>> Timer("re.sub('\\n', '\\n'+spaces, s)", "from __main__ import re, s, >>> spaces").timeit() 4.3669700622558594 >>> Regexps are still more than twice slower. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: List as FIFO in for loop

2008-03-10 Thread Arnaud Delobelle
7;: l += list(' EGGS') ... if c == 'S': l += list(' NI') ... >>> ''.join(l) 'SPAM NI EGGS NI' >>> Of course in practice this is no different from doing 'for c in l:...' but it is safe against implementation changes. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: TextWrapper keepking line breaks?

2008-03-10 Thread Arnaud Delobelle
ere seems no way to do this, > but before writing my own solution, I want to know whether > the solution already exists or not. > > Thanks. Don't know but you could write: >>> import textwrap >>> def wraplines(text): ... return '\n'.join(textwra

Re: List Combinations

2008-03-12 Thread Arnaud Delobelle
nor a hack, I haven't tried to measure it against other approaches (obviously it wouldn't beat the eval(...) hack). I haven't optimised it for readability (by others) either :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: List mutation method gotcha - How well known?

2008-03-13 Thread Arnaud Delobelle
9 introduces .add(), .discard(), .toggle() for MutableSets which all mutate self and return a non None object. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python regex

2008-03-13 Thread Arnaud Delobelle
order... negate > > -- Andrew There would be many ways to do this. One: >>> import re >>> r = re.compile(r'/\*(.*?)\*/') >>> tst = '.a { color: 0xAACC66; /* Fav color */ }' >>> m = r.search(tst) >>> m.group(1) ' Fav color ' >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python regex

2008-03-13 Thread Arnaud Delobelle
Hi\nthere!' Hi there! >>> print r'Hi\nthere!' Hi\nthere! >>> If you haven't done so already, I suggest reading the tutorial. Here is a link to the relevant section on strings: http://docs.python.org/tut/node5.html#SECTION00512 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: getattr(foo, 'foobar') not the same as foo.foobar?

2008-03-13 Thread Arnaud Delobelle
s created, the first one is already dead. So The second one takes its place in memory, hence their ids are equal 3. In (B) the first foo.bar is kept alive for comparing with the second, hence they have a different id. 4. Both points above follow from the fact that foo.bar is really a function ca

Re: getattr(foo, 'foobar') not the same as foo.foobar?

2008-03-13 Thread Arnaud Delobelle
On Mar 13, 11:29 pm, Dave Kuhlman <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > > 4.  Both points above follow from the fact that foo.bar is really a > > function call that returns a (potentially) new object: in fact what > > really happens is something

Re: How do I iterate over items in a dict grouped by N number of elements?

2008-03-14 Thread Arnaud Delobelle
Note, the left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using "izip(*[iter(s)]*n)". For data that doesn't fit n-length groups exactly, the last tuple can be pre-padded with fill values using "izip(*[chain(s, [None]*(n-1))]*n)". -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Custom Exception Value?

2008-03-14 Thread Arnaud Delobelle
by zero',), >  ) > > Wy does the output of ZeroDivisionError appear in sys.exc_info() but > for test it is just Test() (should be Test('mess') right??) Three ways to do it right: * Override __repr__() rather than __str__ or forget about __repr__() and instead: * c

Re: Is there Python equivalent to Perl BEGIN{} block?

2008-03-14 Thread Arnaud Delobelle
> "check_env()" function before it reaches the "def" statement. > > I need the check to be done before the subprocess import, because our > customers use different Python versions, some of them do not have > subprocess module. So i want to exit if i see that Python version > being used doesn't have that module. > > The solution to that problem with what you suggested could be wrapping > the subprocess import with function, am i correct? why not: try: from subprocess import * except ImportError: sys.exit('No subprocess module :(') # Rest of module -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Joseph Weizenbaum

2008-03-15 Thread Arnaud Delobelle
On Mar 15, 5:47 am, Tim Roberts <[EMAIL PROTECTED]> wrote: > Jeff Schwab <[EMAIL PROTECTED]> wrote: > >Roel Schroeven wrote: > >> [EMAIL PROTECTED] schreef: > >>> On Mar 14, 1:47 pm, "Reedick, Andrew" <[EMAIL PROTECTED]> wrote: > > Subject: RIP: Joseph Weizenbaum > > Creator of Eliza: > >>>

Re: Getting started with OS X Leopard

2008-03-15 Thread Arnaud Delobelle
om MacPorts. That's not quite what I wanted, > because they only have a version for Python 2.4. *Sigh*. MacPorts seems > to be getting new ports all the time. The problem is, there also seems > to be an aweful lot of ports gathering bitrot. Is there a particular reason you want python from MacPorts? OSX Leopard comes with python 2.5, that's what I use on my mac. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Advantage of the array module over lists?

2008-03-16 Thread Arnaud Delobelle
('test(list, n)', 'from __main__ import test, n').timeit(1) 2.4988760948181152 >>> from array import array >>> arr = lambda n: array('i', n) >>> timeit.Timer('test(arr, n)', 'from __main__ import test, arr, n').timeit(1) 5.7419960498809814 >>> -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Interesting math problem

2008-03-17 Thread Arnaud Delobelle
urn [(i+1)*dist/parts - i*dist/parts for i in xrange(parts)] >>> slope(130, 50) [2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3] >>> len(slope(130, 50)) 50 >>> sum(slope(130, 50)) 130 >>> Smugly yours -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: keyboard "interrupt"

2008-03-18 Thread Arnaud Delobelle
nput in the buffer. Make sense? Totally easy? Let me > know... If you are on a UNIX platform, you can use the curses module. http://docs.python.org/lib/module-curses.html There is a link there to a tutorial, but it seems to be broken. A quick search finds it there: http://www.amk.ca/python/howto/

Re: ftp recursively

2008-03-18 Thread Arnaud Delobelle
name = line.rpartition(' ')[-1] # assumes no whitespace in filenames if line[0] == 'd': #filename is a directory print 'directory', name else: print 'file', name #This will print all files in cwd prefixed by 'directory'

Re: finding items that occur more than once in a list

2008-03-18 Thread Arnaud Delobelle
!= j == k: yield k i, j = j, k >>> list(duplicates([1, 2, 3, 2, 2, 4, 1])) [1, 2] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: finding items that occur more than once in a list

2008-03-18 Thread Arnaud Delobelle
t; was just, in effect, implicit linked listing - ie it still needs a > linear search to handle collisions, it just avoids the need for > explicitly stored link fields. Perhaps I'm mistaken. I don't think you are mistaken, but if I'm wrong I'd be grateful for a link to

Re: finding items that occur more than once in a list

2008-03-20 Thread Arnaud Delobelle
On Mar 19, 3:08 am, Bryan Olson <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > Ninereeds wrote: > >> Hrvoje Niksic wrote: > >>> This doesn't apply to Python, which implements dict storage as an > >>> open-addressed table and automat

Re: How can I make a function equal to 0?

2008-03-21 Thread Arnaud Delobelle
gt;>> @WhyOhWhy ... def f(x): print "Why oh why,", x ... >>> f("do this?") Why oh why, do this? >>> f == 0 True >>> -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: finding items that occur more than once in a list

2008-03-22 Thread Arnaud Delobelle
h table behaves just like a linked list. Lastly, if one deals with a totally ordered set of object but they are not hashable (there isn't a good hash function), then Ninereeds' idea of sorting first is still useful. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: function-class frustration

2008-03-22 Thread Arnaud Delobelle
or queue of recent results, > you could implement it yourself easily. As you guessed, __ and ___ have the expected meaning in IPython. moreover the nth output in the session is stored in _n. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: List question

2008-03-22 Thread Arnaud Delobelle
 Also, > the docs don't mention it.http://docs.python.org/ref/keywords.html That's because you have to do: from bearophile import musings HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Do any of you recommend Python as a first programming language?

2008-03-22 Thread Arnaud Delobelle
d principles. I'm not from the US and I'm not sure what 9th/12th grade are, but if you want to use programming to explore maths, er I mean math, have a look at the sage project: http://www.sagemath.org/ -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Do any of you recommend Python as a first programming language?

2008-03-22 Thread Arnaud Delobelle
I felt like a man who's had a black and white TV set all his life and watches colour TV for the first time. What if computers had been designed as 'lambda-calculus machines' from the start rather than Turing machines? Anyway, here the conclusion that I draw: learn lambda-calculus

Re: Pyparsing help

2008-03-23 Thread Arnaud Delobelle
x27;: 0, 'color': 'cyan', 'pattern': 'blank', 'layerNumber': 0, 'minSpacing': 0, 'blink': 0, 'minWidth': 0, 'visible': 1, 'pitch': 0, 'selectable': 1, 'lineStyle': 'solid'} >>> p['Layer']['METAL2']['maskName'] 'metal2' >>> p['Technology']['gridResolution'] 5 >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Do any of you recommend Python as a first programming language?

2008-03-23 Thread Arnaud Delobelle
programmers who have all agreed to use the same language. Anyway, I have browsed the book and agree with Paul Rubin that there doesn't seem to be any unusual notation in it, although there are a numbers of topics that I am not really familiar with. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Arnaud Delobelle
constant time but I haven't checked the code. > Same for bool(dict) (which is what you get when you run "if dict: ..."). It has to be constant time as it is a lower bound for inserts (which average to constant time). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Why I hate lambdas (Re: Do any of you recommend Python as afirst programming language?)

2008-03-24 Thread Arnaud Delobelle
bute? Surely the name of the function should be that of the function > object, not of one of the shared parts? >>> foo1.__name__ 'foo' >>> foo1.func_code.co_name 'foo' As seen above, func.__name__ and func.func_code.co_name are the same thing (until tampered with). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Why I hate lambdas (Re: Do any of you recommend Python as afirst programming language?)

2008-03-24 Thread Arnaud Delobelle
sense for code objects to have a name (taken from the def statement) than for function objects, as there is exactly one code object for every def statement. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortcutting the function call stack

2008-03-24 Thread Arnaud Delobelle
acked @hijack def foo(x): x = x + 1 hijacker(x) return x * 2 def hijacker(x): if x == 21: print "[Oh no, it's going to be 42!]" raise HijackReturn(41) # --- marigold:junk arno$ python -i hijack.py >>> foo(10) 22 >>> foo(20) [Oh no, it's going to be 42!] 41 >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: how to dynamically create class methods ?

2008-03-25 Thread Arnaud Delobelle
>>> check_hypo = get_spellchecker('hypopothamus') >>> check_hypo('Hypopothamus') True >>> check_hypo('Big scary mammal') False (Warning: this is all untested). HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: how to dynamically create class methods ?

2008-03-26 Thread Arnaud Delobelle
help. > > class MyClass(object): >     pass > > records = ["spam", "ham"] > for record in records: >     # define a new function >     def f(n): >         return (record + " ")*n >     # create a new instance >     instance = MyClass() >     # and dynamically add a method to it >     setattr(instance, 'execute', f) >     instance.execute(5) Amusingly naive'ly yours -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: how to dynamically create class methods ?

2008-03-26 Thread Arnaud Delobelle
7;ham ham ham ham ham ' 'ham ham ham ham ham ' Because the name 'record' in f is bound to 'ham' after the loop. To fix this, you can for example change def f(n): ... to def f(n, record=record): ... This way, 'record' is local to f and won't change at the next iteration of the loop. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Strange loop behavior

2008-03-26 Thread Arnaud Delobelle
;> print "''" '' Why do you wrap f.read(...) in the repr() function? If you remove the two repr() I suspect your code will work. OTOH do you know that the read() method of file objects does what you want? You could simply write: file_str = f.read() Or are there some other factors that prevent you from doing this? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Strange loop behavior

2008-03-26 Thread Arnaud Delobelle
ying to read a binary file and > put it's contents in an xml message to send via the network, so that I > can re-create it on the other side. I do need to keep the xml aspect > though. Is there a better way of doing this? > > Thanks, > Gabriel Have a look at the uu and base64 modules: http://docs.python.org/lib/module-uu.html http://docs.python.org/lib/module-base64.html -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: how to dynamically instantiate an object inheriting from several classes?

2008-11-22 Thread Arnaud Delobelle
case you change your mind). In the meantime why not adopt the terminology commonly used on this list and by Python users at large? Nobody else would talk about 'a function that takes a reference to a class'. It's just plain confusing. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-23 Thread Arnaud Delobelle
Aaron Brady <[EMAIL PROTECTED]> writes: > Truth and clarity are not tired of this thread. This is such a marvellously economic way of putting it, it's poetic! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python 3 __cmp__ semantic change?

2008-11-23 Thread Arnaud Delobelle
Arnaud Delobelle <[EMAIL PROTECTED]> writes: >> (BTW, I think your earlier decorator had a bug, in that it failed to >> define __ne__ but then called "self != other".) > > That would be true for Python 2.x but I'm explicitly writing code for > Python 3 h

Re: Python 3 __cmp__ semantic change?

2008-11-23 Thread Arnaud Delobelle
Steven D'Aprano <[EMAIL PROTECTED]> writes: > On Sat, 22 Nov 2008 09:10:04 +, Arnaud Delobelle wrote: > >> That's not surprising. You're measuring the wrong things. If you read >> what I wrote, you'll see that I'm talking about Fraction.__g

Re: Python 3 __cmp__ semantic change?

2008-11-23 Thread Arnaud Delobelle
George Sakkis <[EMAIL PROTECTED]> writes: > On Nov 23, 6:14 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > >> So how did I get it into my head that defining __eq__ would create the >> correct behaviour for __ne__ automatically?  And more puzzlingly, how >&

Re: how to detect if a dictionary has been modified ?

2008-11-23 Thread Arnaud Delobelle
sting technique. I must point out, though, that it doesn't > indicate if a dict has been changed, only that potentially changing > operations have been performed. So it really depends on what Stef > originally meant by changed, and perhaps what is meant by == :) > > x = {'

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Arnaud Delobelle
re a way to use a dictionary this way, or am I stuck with > copy/pasting blocks of "if m:"? But there is no reason why you should use a dictionary; just use a list of key-value pairs: patterns = [ ("pattern1", re.compile(">.+?.+?>(.+?)"), ("pattern2", re.compile("something else"), ] for name, pattern in patterns: ... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

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