Re: accumulator generators

2008-05-31 Thread Arnaud Delobelle
George Sakkis <[EMAIL PROTECTED]> writes: > On May 31, 4:19 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> Cameron <[EMAIL PROTECTED]> writes: >> > I was reading this http://www.paulgraham.com/icad.html";>Paul >> > Graham article and h

Re: Bring object 'out of' Class?

2008-06-01 Thread Arnaud Delobelle
): '''deals x amount of cards(num) to each hand''' hands = [] for i in range(num_of_hands): newhand = Hand('hand%d' % i) self.deal(newhand, num) hands.append(newhand) print '%s' % (handname.label), '\n', handname, '\n' return Hand Then you can write: >>> hands = deck.deal_cards(4, 5) # On fait une belotte? And I don't see the need of defining 'Hand' inside 'Deck'. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Better performance

2008-06-02 Thread Arnaud Delobelle
And each one has his area of 'work' > > > Thanks for your help ! Python, of course. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: php vs python

2008-06-02 Thread Arnaud Delobelle
To be Pythonic here, "eloquent" code would perhaps often have clear, > clean list comprehensions used when "good" code would use a "for" > loop but still be easy to follow as well and perfectly acceptable in > the vast majority of cases. I find that eloquent Python speakers often tend to write a for loop when mere good ones will try to stick a list comprehension in! Regards -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Rationale for read-only property of co_code

2008-04-03 Thread Arnaud Delobelle
On Apr 3, 11:10 am, João Neves <[EMAIL PROTECTED]> wrote: > On Apr 3, 4:43 am, Scott David Daniels <[EMAIL PROTECTED]> wrote: > > > Nope:  If you change the code in-place, the whole stack's references > > to where they were running would need to get updated to corresponding > > locations in the new

Re: expanding a variable to a dict

2008-04-03 Thread Arnaud Delobelle
ctionary *objects*, not *names*, so write for a in [dictFoo, dictBar, dictFrotz]: ... BTW, hasattr() is not what you want as it check the existence of an attribute, i.e. a.hasattr('x') means that a.x exists; you could write the whole thing as: for a in dictFoo, dictBar, dictFrotz:

Re: generator functions: why won't this work?

2008-04-04 Thread Arnaud Delobelle
thougth enough on it. Looks fine in principle, but   > yield expressions may be a problem. > > -- > Gabriel Genellina Funnily, there's a patch for py3k to make this work as in your idea. It's currently being discussed on python-3000 :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: append to a sublist - please help

2008-04-06 Thread Arnaud Delobelle
RWTH - Aachen University > D 52056 Aachen, Germany This is in the top two FAQ, here is the relevant section: http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: A file iteration question/problem

2008-04-06 Thread Arnaud Delobelle
ition: recfun(lines) lines = iter(open(filename)) recfun(lines) Or if you want the filename to be the argument of you function, wrap it in a non-recursive function: def fun(filename): lines = iter(open(filename)) def recfun(): for line in lines: # Do stuff

Re: A file iteration question/problem

2008-04-07 Thread Arnaud Delobelle
On Apr 7, 2:09 pm, [EMAIL PROTECTED] wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > def recfun(lines): > >     for line in lines: > >         # Do stuff > >         if condition: > >             recfun(lines) > > > lines = iter(open(filena

Re: A file iteration question/problem

2008-04-07 Thread Arnaud Delobelle
      break; > >         # > >         # do various tests > >         # > >         if : > >             recfun(f) > >      Don't do that; Python doesn't have tail recursion and you'll hit the > stack limit. > >                                 John Nagle This function is not tail recursive (the recursive call is in a loop). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Translating keywords

2008-04-07 Thread Arnaud Delobelle
x)) > print(∑(values)) > near = λ a,b,ε=0.01: a-ε ≤ b ≤ a+ε It's all in the eye of the beholder: to me it looks readable, but that's because I've spent 10 years of my life reading and writing stuff like that. Although I would use ∀ and ∃ as aliases for all() and exists() :) -

Re: Translating keywords

2008-04-07 Thread Arnaud Delobelle
On Apr 8, 6:47 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] > Although I would use ∀ and ∃ as aliases for all() > and exists() :) I mean all() and any() of course -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: calling variable function name ?

2008-04-08 Thread Arnaud Delobelle
something entered by user. now i want to call FB. I don't > want to do an if else because if have way too many methods like > this... > > var = "F" + temp > var(param1, param2) > > This does not work ofcourse. Does anyone know how to implement this

Re: CPython VM & byte code resources wanted

2008-04-08 Thread Arnaud Delobelle
aron Bytecodes: http://docs.python.org/lib/bytecodes.html VM: Python/ceval.c HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How to find documentation about methods etc. for iterators

2008-04-09 Thread Arnaud Delobelle
in enumerate(f): # Do stuff How to find out about it? I suppose you need to get familiar with python iterators/iterables and the functions to manipulate them. How to do that depends on your prior experience with such concepts. Unfortunately I am not able to provide you with a good link :( -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Python 3.0 new integer division

2008-04-09 Thread Arnaud Delobelle
fficient extraction > of any one of the 3 digits.  Decimal doesn't use this either. :) > > Mark Naive question: why not just use a long + an exponent? e.g. 132560 -> (13256, 1) 0.534 -> (534, -3) 5.23e10 -> (523, 8) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: @x.setter property implementation

2008-04-10 Thread Arnaud Delobelle
operty def bar(self): return self._bar @bar.setter def setbar(self, x): self._bar = '<%s>' % x # Interactive test - >>> foo = Foo() >>> foo.bar = 3 >>> foo.bar '<3>' >>> foo.bar = 'oeufs' >>> foo.bar '' >>> Having fun'ly yours, -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Obtaining a callable class method object from a specific class

2008-04-10 Thread Arnaud Delobelle
return 3 class Leaf2(Branch): pass - Test - >>> totalscore(Root, 1) 0 >>> totalscore(Branch, 1) 1 >>> totalscore(Leaf, 1) 4 >>> totalscore(Leaf2, 1) 1 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: @x.setter property implementation

2008-04-11 Thread Arnaud Delobelle
27;propset' decorator function. >         self._x = v + 1 > > Now enter the interpreter: > >> import mod > >>> f = mod.Foo() > >>> f.x = 4 > >>> f.x > > 4 > > I don't feel like giving up on this now, so close... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: About __init__ and default arguments

2008-04-11 Thread Arnaud Delobelle
me saves nine. > A BAR in time saves nine. > > I suppose I could set the default to a string literal, test for it and if > true assign the value of self.default to keyword; however, that seems > clunky.  Any ideas how this could be done along the lines of my proposed > but faulty cod

Re: Rounding a number to nearest even

2008-04-11 Thread Arnaud Delobelle
finitions.  (And if you're not using conventional definitions, you > should say so) And I would like to know what unconventional - but mathematically meaningful - definitions lead to lim x != 0 x -> 0 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: class level properties

2008-04-12 Thread Arnaud Delobelle
want to have > to access them as functions rather than variables or properties. Metaclasses, of course! >>> class MetaX(type): ... @property ... def spam(self): return 'eggs' ... >>> class X(object): ... __metaclass__ = MetaX ... >>> X.spam 'eggs' >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: class level properties

2008-04-12 Thread Arnaud Delobelle
On Apr 13, 12:33 am, Charles D Hixson <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > >>>> class MetaX(type): > > > ...     @property > > ...     def spam(self): return 'eggs' > > ... > > >>>> class X(object):

Re: Recommendation for Web Framework

2008-04-13 Thread Arnaud Delobelle
ython > 2.3. Django only requires Python 2.3 IIRC, and I think you can use it with FastCGI instead of mod_python. Doesn't FastCGI only require apache 1.3.x ? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Call a classmethod on a variable class name

2008-04-13 Thread Arnaud Delobelle
object): ... @classmethod ... def bar(cls): print 'Oh my Baz!' ... >>> globals()['Foo'] >>> globals()['Foo'].bar() Oh my Baz! If your class lives in a module, just do getattr(module, classname) instead to get the class object. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Interesting math problem

2008-04-13 Thread Arnaud Delobelle
On Apr 13, 5:35 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On Mar 19, 2:17 pm, "BJörn Lindqvist" <[EMAIL PROTECTED]> wrote: > > > > > On Mon, Mar 17, 2008 at 11:57 PM, Arnaud Delobelle > > > <[EMAIL PROTECTED]> wrote: > > >

Re: eval modifies passed dict

2008-04-14 Thread Arnaud Delobelle
fore expression is parsed. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding a number to nearest even

2008-04-15 Thread Arnaud Delobelle
choose from that two possibilities with equal probability. > So when we round 2.5 we are actually rounding an interval which could > be equally be rounded to 2 or to 3, and the same for 3.5, 4.5 etc. If > the number of such intervals is big, choosing the even number helps to > make as ma

Re: Preferred method for "Assignment by value"

2008-04-15 Thread Arnaud Delobelle
#x27;list' is the type of lists, it is also a 'constructor' for list objects (the same goes for other common buit-in types, such as 'int', 'float', 'str', 'tuple', 'dict'). E.g. >>> foo = [1, 2, 3] >>> bar = list(foo) >>> foo[0] = 4 >>> foo [4, 2, 3] >>> foo = [1, 2, 3] >>> bar = list(foo) >>> bar[0] = 4 >>> bar [4, 2, 3] >>> foo [1, 2, 3] >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Can't do a multiline assignment!

2008-04-17 Thread Arnaud Delobelle
do some group manipulation of fields (e.g. print them all def print_fields(self): for name, value in self._fields.iteritems(): print "%s: %s" % (name, value) ) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Can't do a multiline assignment!

2008-04-17 Thread Arnaud Delobelle
dictionaries (rhm.__dict__ then RequestHeadersManager.__dict__). -- Arnaud -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: manipulating class attributes from a decorator while the class is being defined

2008-04-19 Thread Arnaud Delobelle
e something like this: class Ruleset(list): def add(self, regexp): def decorator(func): self.append((regexp, func)) return func return decorator class Parser(object): rules = Ruleset() @rules.add(r'".*"') def quoted_string(self):

Re: ???Python Memory Management S***s???

2008-04-20 Thread Arnaud Delobelle
 > del instanceList             # You can try: nothing is freed by this > """ > ??? How do you people control python to free the memory in python 2.5 or > python 2.4 ??? > Cheers!!! You mistyped your subject line; it should have read: "Help needed - I don't understand how Python manages memory" -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Alternate indent proposal for python 3000

2008-04-20 Thread Arnaud Delobelle
eme in .py files, so it seems to > me that this doesn't have to be done in the core language. All that's > needed is a variant of 'eval' which expects the alternate scheme, and > that could be prototyped just using text manipulation and the normal > 'eval'. By 'eval', I guess you mean 'exec' :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: What happened with python? messed strings?

2008-04-20 Thread Arnaud Delobelle
On Apr 20, 7:54 pm, <[EMAIL PROTECTED]> wrote: [...] > python should have stayed at version 1.5, every single 'improvement' > has been a mess. But this is the definitive hell. You can still download Python 1.5.2 from python.org: http://www.python.org/download/release

Re: @classmethod question

2008-04-24 Thread Arnaud Delobelle
. clafoutis = Recipe('eggs', 'spam') -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: annoying dictionary problem, non-existing keys

2008-04-24 Thread Arnaud Delobelle
if it > does not exist, it may return only empty, just as in php At the interactive prompt, type 'help(dict)' and read carefully about the different methods provided by dictionary objects (one called 'get' may be of particular interest to you). HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: convert xhtml back to html

2008-04-24 Thread Arnaud Delobelle
ith regexps, but my simpleminded )]+/> doesn't work. I'm not > enough of a regexp pro to figure out that lookahead stuff. Hi, I'm not sure if this is very helpful but the following works on the very simple example below. >>> import re >>> xhtml = 'hell

Re: Parsing text file with #include and #define directives

2008-04-24 Thread Arnaud Delobelle
in expand(recfilename): yield recline elif line.startswith('#define '): _, name, value = line.strip().split(None, 2) defines[name] = value else: yield define_regexp.sub(define_repl, line) It would be easy to modify it to keep track of line numbers and file names. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Class Inheritance - What am I doing wrong?

2008-04-24 Thread Arnaud Delobelle
fallen victim to the Name Mangling Trap [1]. Replace the leading double underscore in the __name attribute with a single one, and Python shall calm down and let your code behave as you expect it to. That is, if you also pass the name parameter to super(A,self).__init__ in B's __init__ method [1] http://docs.python.org/ref/atom-identifiers.html -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Class Inheritance - What am I doing wrong?

2008-04-24 Thread Arnaud Delobelle
Arnaud Delobelle <[EMAIL PROTECTED]> writes: > That is, if you also pass the name parameter to super(A,self).__init__ > in B's __init__ method Oops. should be super(B, self).__init__(name), of course. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Class Inheritance - What am I doing wrong?

2008-04-24 Thread Arnaud Delobelle
By design, python doesn't include mechanisms equivalent to the Java / C++ 'private'. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Little novice program written in Python

2008-04-25 Thread Arnaud Delobelle
def getprimes(N): arr = range(N+1) arr[1] = 0 for s in xrange(2, int(math.sqrt(N))+1): if arr[s]: arr[s*s : N+1 : s] = [0] * (N/s - s + 1) return filter(None, arr) (I think) This all needs to be tested. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: multiple pattern regular expression

2008-04-25 Thread Arnaud Delobelle
gt; > Also should I be using regex at all here ? If every line of the file is of the form name=value, then regexps are indeed not needed. You could do something like that. params = {} for line in file: name, value = line.strip().split('=', 2) params[name] = value (untested

Re: Setting an attribute without calling __setattr__()

2008-04-25 Thread Arnaud Delobelle
Joshua Kugler <[EMAIL PROTECTED]> writes: [...] > self.me = [] > for v in obj: > self.me.append(ObjectProxy(v)) Note that is could be spelt: self.me = map(ObjectProxy, v) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: multiple pattern regular expression

2008-04-25 Thread Arnaud Delobelle
Chris Henry <[EMAIL PROTECTED]> writes: > On Apr 25, 8:37 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> micron_make <[EMAIL PROTECTED]> writes: >> > I am trying to parse a file whose contents are : >> >> > parameter=current >> > ma

Re: Setting an attribute without calling __setattr__()

2008-04-26 Thread Arnaud Delobelle
[EMAIL PROTECTED] (Aahz) writes: > In article <[EMAIL PROTECTED]>, > Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >>Joshua Kugler <[EMAIL PROTECTED]> writes: >>> >>> self.me = [] >>> for v in obj: >>>

Re: removing extension

2008-04-27 Thread Arnaud Delobelle
method of strings: >>> path = r'C:\myimages\imageone.jpg' >>> path.rsplit('.', 1) ['C:\\myimages\\imageone', 'jpg'] >>> path = r"C:\blahblah.blah\images.20.jpg" >>> path.rsplit('.', 1) ['C:\\blahblah.blah\\images.20', 'jpg'] HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Random/anonymous class methods

2008-04-27 Thread Arnaud Delobelle
om.choice(meths) > c.ch() This will work: getattr(c, ch)() Getattr(c, "meth1") is equivalent to c.meth1. Or you could do: meths = [c.meth1, c.meth2] ch = random.choice(meths) ch() -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: removing extension

2008-04-27 Thread Arnaud Delobelle
27;foo.baz/bar'.rsplit('.') ['foo', 'baz/bar'] >>> os.path.splitext('foo.baz/bar') ('foo.baz/bar', '') Conclusion: forget about str.rsplit() (which I suggested), use os.path.splitext() instead :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Arnaud Delobelle
['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ] # these words will be replaced with nothing wordmap = dict((w, []) for w in un_words) # these words will be replaced with the list of their synonyms for wordlist in alterns: for word in wordlist: wordmap[word] = wordlist def compareandremove(sentence): return [x for w in sentence.split() for x in wordmap.get(w, [w])] # Example >>> compareandremove('notepad a pencil with desk') ['book', 'textbook', 'notepad', 'pencil', 'pen', 'pacer', 'desk'] -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Mapping and Filtering Help for Lists

2008-04-27 Thread Arnaud Delobelle
'You! are Free??'.translate(table, '!?') 'you are free' HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Given a string - execute a function by the same name

2008-04-28 Thread Arnaud Delobelle
functions to methods and use getattr( myClass, "method" ) > > 3. Place all my functions in dictionary and lookup the function to be > called 4. Put all my functions in a module and use getattr(module, 'function') -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: How to unget a line when reading from a file/stream iterator/generator?

2008-04-28 Thread Arnaud Delobelle
[EMAIL PROTECTED] writes: > Is there an elegant way to unget a line when reading from a file/stream > iterator/generator? > > By "unget" I mean a way to push back a line into the source stream and > backtrack the iterator/generator one step? > > The only alternative I can see is to put my line rea

Re: list.reverse()

2008-04-28 Thread Arnaud Delobelle
one > > Please explain this behavior. range(5) returns a list from 0 to 4 and > reverse just reverses the items on the list that is returned by > range(5). Why is x None (null)? have you tried typing help(list.reverse) at the interactive prompt? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: python script as executable

2008-04-28 Thread Arnaud Delobelle
throws error: > :No such file or directory > > I am editing file from eclipse for python from windows. and then > uploading on linus machine to run it. > > > any pointers? Have you made your file executable (man chmod)? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Given a string - execute a function by the same name

2008-04-29 Thread Arnaud Delobelle
t; Thanks again to everyone who contributed on this thread. > > Regards, > Malcolm You could avoid #5 from the start using a decorator: functions = {} def register(func): functions[func.__name__] = func return func @register def foo(): print "Foo!" @register

Re: Zope/DTML Infuriating...

2008-04-29 Thread Arnaud Delobelle
e objects (similar to > javascript), that type casting is done through build-in functions > rather than methods, e.g. String.toInt('5') or '5'.toInt() or x = > Integer.fromString('5'). Because Python is not Javascript? In fact some are methods, e.g. str(x) is shorthand for x.__str__(). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: string translate, replace, find and the forward slash

2008-04-29 Thread Arnaud Delobelle
ne here: marigold:junk arno$ python Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> unsafe_chars = "/#()[EMAIL PROTECTED]&*{}

Re: string translate, replace, find and the forward slash

2008-04-29 Thread Arnaud Delobelle
destroy <[EMAIL PROTECTED]> writes: > On Apr 29, 4:50 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >> >> marigold:junk arno$ python >> Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) >> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin >> Type &q

Re: computing with characters

2008-04-30 Thread Arnaud Delobelle
very reasonable place to search; I think chr and ord are > builtin functions (and not str methods) just by an historical > accident. (Or is there any other reason? what's wrong with "a".ord() > or str.from_ordinal(65))? Not a reason, but doesn't ord() word with unicode as well? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: calling variable function name ?

2008-04-30 Thread Arnaud Delobelle
" + param2 > def FA(param1,param2): > print "FC" + param1 + " " + param2 > > temp = sys.argv[1] > > func = globals()["F" + temp] > func("Hello", "World") > > > I ran the script with first parameter as B and i get

Re: is +=1 thread safe

2008-05-02 Thread Arnaud Delobelle
ool effect when you're 12! CLV LOOP: INC $D020 BVC LOOP (from memory, untested!) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Finally had to plonk google gorups.

2008-05-02 Thread Arnaud Delobelle
fusing gmail addresses and Google Groups (groups.google.com), which can be used as a web interface to usenet and mailing lists. Look at the 'Organisation' header of the messages you quote. It seems most of the spam on comp.lang.python is posted from Google Groups, *including* the one with a 'From' header which is not a gmail address. So it would be more efficient to block messages posted from Google Groups than the ones from gmail addresses. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: is +=1 thread safe

2008-05-02 Thread Arnaud Delobelle
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes: > On Fri, 02 May 2008 19:23:54 +0100, Arnaud Delobelle wrote: > >> Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes: >> >>> >>> There are no modern processors with an opc

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Arnaud Delobelle
vát sum() works for any sequence of objects with an __add__ method, not just floats! Your algorithm is specific to floats. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Arnaud Delobelle
Szabolcs Horvát <[EMAIL PROTECTED]> writes: > Arnaud Delobelle wrote: >> >> sum() works for any sequence of objects with an __add__ method, not >> just floats! Your algorithm is specific to floats. > > This occurred to me also, but then I tried > > sum([&#x

Re: dict invert - learning question

2008-05-03 Thread Arnaud Delobelle
over keys and values using the items() or iteritems() method of dictionaries: def invert(d): inv = {} for key, val in d.iteritems(): inv.setdefault(val, []).append(key) return inv -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: word shifts

2008-05-04 Thread Arnaud Delobelle
lines.add(line) for i in xrange(25): line = shift1(line) if line in lines: print line (untested) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: word shifts

2008-05-04 Thread Arnaud Delobelle
ere keys are > "".join(sorted(word)) > Such general strategy to look for a possible invariant key for the > subsets of the required solutions is quite useful. Or even: def get_key(word): initial = ord(word[0]) return tuple((ord(x) - initial) % 26 for x in word[1:]) -- Arn

Re: word shifts

2008-05-05 Thread Arnaud Delobelle
George Sakkis <[EMAIL PROTECTED]> writes: > On May 5, 11:02 pm, dave <[EMAIL PROTECTED]> wrote: >> On 2008-05-04 01:10:40 -0600, Arnaud Delobelle <[EMAIL PROTECTED]> said: >> >> >> >> > dave <[EMAIL PROTECTED]> writes: >> >&g

Re: Am I missing something with Python not having interfaces?

2008-05-06 Thread Arnaud Delobelle
n truth, even without multiple inheritance, Python wouldn't need java-like interfaces. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: STL multimap

2008-05-06 Thread Arnaud Delobelle
ehind the scene?). Else you could use a trampoline, as I've done here: http://www.marooned.org.uk/~arno/python/cogenerator.html (Scroll down to 'Flattening nested cogenerators' and 'recursive cogenerators' - I had fun doing it but it is terribly inefficient). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Am I missing something with Python not having interfaces?

2008-05-06 Thread Arnaud Delobelle
xcellent!) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: python vs. grep

2008-05-06 Thread Arnaud Delobelle
ne in open('bigfile')) sum(1 for line in ifilter(search, open('bigfile'))) ...etc... All this is untested! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: comparing dictionaries

2008-05-07 Thread Arnaud Delobelle
herwise it could be a bit simpler: missing_keys = set(first_dict) for keys, val in second_dict.iteritems(): for key in keys: missing_keys.discard(key) if first_dict.get(key, val) != val: print "wrong value for", key, 'in', keys if missing_keys: print 'some keys are missing:', ',',join(remaining) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Idea for P3K

2008-05-07 Thread Arnaud Delobelle
that x is now bound to 2, but it is only apparent if one closely scrutinises the code. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: letter frequency counter / your thoughts..

2008-05-07 Thread Arnaud Delobelle
onary', but obviously isn'tas it is a list of key,value pairs. * length = len(getvals) Again, we know it's a length, but the length of what? HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Idea for P3K

2008-05-07 Thread Arnaud Delobelle
"Méta-MCI (MVP)" <[EMAIL PROTECTED]> writes: > Hi! > >> I don't often feel like using this word > > Look at languages like OCAML or F # While I like Objective Caml, it is definitively not Python! -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: The del statement

2008-05-08 Thread Arnaud Delobelle
= 4 # Equivalent to x.__setattr__('a', 4) What conclusion should we draw from that? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: The del statement

2008-05-08 Thread Arnaud Delobelle
Duncan Booth wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > > > > > > George Sakkis wrote: > >> One of the few Python constructs that feels less elegant than > >> necessary to me is the del statement. For one thing, it is overloaded >

Re: Newbie to python --- why should i learn !

2008-05-08 Thread Arnaud Delobelle
pistacchio <[EMAIL PROTECTED]> writes: > ocalm forces you to di OOP Ocaml *allows* you to do OOP. It's very much an optional feature of the language, just like for Python. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Surprising difference in behavior between "import blah" and "from blah import thing"

2008-05-08 Thread Arnaud Delobelle
ving to think. > -- Martin Luther King, Jr. > from "Strength to Love," 1963. import system from system import thing is the same as: import system thing = system.thing Do you expect system.thing to be rebound when you rebind thing? HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: anagram finder / dict mapping question

2008-05-08 Thread Arnaud Delobelle
"Kam-Hung Soh" <[EMAIL PROTECTED]> writes: > I avoid creating a temporary variable if it's only used once > immediately, so I would have written: > > newtlist = ''.join(sorted(list(line))) Or ''.join(sorted(line)) as sorted() works wit

Re: Mathematics in Python are not correct

2008-05-08 Thread Arnaud Delobelle
e function from the empty set to the empty set, 0^0 = 1. The arguments for making 0^0 = 0 are weak, it is a bit more convincing to want it to be undefined (as (0,0) is a point of discontinuity of (x,y) -> x^y). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: simple question about Python list

2008-05-09 Thread Arnaud Delobelle
list3 that contains > > > elements from list1 with indexes from list2, i.e. > > > > list3 = [list1[i] for i in list2] > > > > -- > > Paul Hankin This works under python 2.0+, I think. Before that, you could have written: list3 = map(list1.__getitem__, list2) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: anagram finder / dict mapping question

2008-05-09 Thread Arnaud Delobelle
.join(sorted(word.lower())) >> anagrams.setdefault(key, []).append(word) >> return anagrams > > What would be the best method to print the top results, the one's that > had the highest amount of anagrams?? Create a new histogram dict? You can use the max() function to find the biggest list of anagrams: top_results = max(anagrams.itervalues(), key=len) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: anagram finder / dict mapping question

2008-05-10 Thread Arnaud Delobelle
Create a new histogram dict? >>> >>> You can use the max() function to find the biggest list of anagrams: >>> >>> top_results = max(anagrams.itervalues(), key=len) >>> >>> -- >>> Arnaud >> >> That is the biggest list of anagrams, w

Re: File Creation Not Working In A Thread Class?

2008-05-11 Thread Arnaud Delobelle
ead class. > > When I put the same line in a thread class, it no longer works, and I get > an error: > > IOError: [Errno 2] no such file u'fileName' > It's telling you that you haven't got a file called 'fileName'. Posting the code that triggers this

Re: File Creation Not Working In A Thread Class?

2008-05-11 Thread Arnaud Delobelle
bc90021 <[EMAIL PROTECTED]> writes: > On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote: > >> bc90021 <[EMAIL PROTECTED]> writes: >> >>> Hi All, >>> >>> Thanks in advance for any and all help! >>> >>> I have t

Re: File Creation Not Working In A Thread Class?

2008-05-11 Thread Arnaud Delobelle
Arnaud Delobelle <[EMAIL PROTECTED]> writes: > bc90021 <[EMAIL PROTECTED]> writes: > >> On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote: >> >>> bc90021 <[EMAIL PROTECTED]> writes: >>> >>>> Hi All, >>&g

Re: File Creation Not Working In A Thread Class?

2008-05-11 Thread Arnaud Delobelle
file u'tempfileName' This is different from the error message that you posted in your original message. Anyway, what is useful to us is a full traceback, no just an error message. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Arnaud Delobelle
ind its variable to something useful instead of an unused counter. Contrived example: # Print 'hello' 10 times; x is not used for x in xrange(10): print 'hello' # By changing what is iterated over, no unused variable: from itertools import repeat for msg in repeat('hello', 10): print msg -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Please help me finding a way to implement os.path.issubpath(a, b)

2008-09-11 Thread Arnaud Delobelle
r'] >>> 'C:\\'.split('\\') ['C:', ''] So you could write instead something like x1 = path1.rstrip(os.sep).split(os.sep) x2 = path2.rstrip(os.sep).split(os.sep) in your function HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: manipulating files within 'for'

2008-09-12 Thread Arnaud Delobelle
mean? I guess you are on Windows, but how do you run your script? Do you do it from the command line or do you use some IDE? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: setattr in class

2008-09-12 Thread Arnaud Delobelle
attrs = {} for i in '12': moreattrs['title_' + i] = int(i) >>> A.title_1 1 >>> A.title_2 2 >>> -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: n00b question: Better Syntax for Coroutines?

2008-09-13 Thread Arnaud Delobelle
little while ago when thinking about this kind of problem I defined "cogenerators", which roughly are to generators what coroutines are to routines, i.e. they can pass "yielding control" on to another cogenerator [1]. [1] http://www.marooned.org.uk/~arno/python/cogenerator.html -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: recursion gotcha?

2008-09-14 Thread Arnaud Delobelle
ve, which of course has no benefit in Python. In fact it looks like a Scheme implementation of sum translated literally to Python. In Python this algorithm is expressed naturally as: def suma(xs): acc = 0 for x in xs: acc += x return acc -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Function getting a reference to its own module

2008-09-14 Thread Arnaud Delobelle
t; change the reference to itself, but at least both changes are right next > to each other. > > Assume that changing the function name or the module name are both > equally likely/unlikely. > > Which do other people prefer? Which seems "better" to you? Are there any >

<    4   5   6   7   8   9   10   11   12   >