Re: Combine two dictionary...

2007-10-01 Thread Raymond Hettinger
On Oct 1, 10:24 am, Abandoned <[EMAIL PROTECTED]> wrote: > Hi.. > dict1={1: 4, 3: 5}... and 2 millions element > dict2={3: 3, 8: 6}... and 3 millions element > > I want to combine dict1 and dict2 and i don't want to use FOR because > i need to performance. The dict.update approach is the fastest

Re: sorteddict PEP proposal [started off as orderedict]

2007-09-26 Thread Raymond Hettinger
[James Stroud ] > Maybe its the PEP killers who act prematurely because I friggin' love > genexps and the little generators they generate. No, it's the PEP author who controls when they ask for pronouncement. The natural instinct is to ask for pronouncement as soon as you have an implementation an

Re: sorteddict PEP proposal [started off as orderedict]

2007-09-26 Thread Raymond Hettinger
[Mark Summerfield] > Below is a PEP proposal for a sorteddict. It arises out of a > discussion on this list that began a few weeks ago with the subject of > "An ordered dictionary for the Python library?" It is worth remembering that the long sought after datatype was a dict that could loop over k

Re: Can a base class know if a method has been overridden?

2007-09-24 Thread Raymond Hettinger
On Sep 24, 8:23 am, Ratko <[EMAIL PROTECTED]> wrote: > Hi all, > > I was wondering if something like this is possible. Can a base class > somehow know if a certain method has been overridden by the subclass? > I appreciate any ideas. > Thanks, > > Ratko It's not hard. Both bound and unbound meth

Re: Sets in Python

2007-09-18 Thread Raymond Hettinger
On Sep 18, 5:39 pm, sapsi <[EMAIL PROTECTED]> wrote: > I recently tried using the set function in Python and was surprised to > find that > > a=[ 1, 2,3, [1,2] ] > > doesn't work with 'set', throwing TyperError (unhashable exception). I > found out that this is because lists can't be hashed. > So,t

Re: Does shuffle() produce uniform result ?

2007-09-04 Thread Raymond Hettinger
for i in range(10): shuffle(sequence) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: is there anybody using __del__ correctly??

2007-08-10 Thread Raymond Hettinger
you search comp.lang.python > for __del__ you will find hundreds of people who were > bitten by __del__, so I usually give advices such as > "you should never __del__ in your code" Good advice. Explicit finalization is almost always preferable. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: zip() function troubles

2007-07-27 Thread Raymond Hettinger
the next method on the input iterators as returns the tuple result). So, if you're seeing behavior changes at 10 millions items, the cause almost certainly lies elsewhere. One possible candidate could be memory consumed by immortal integer objects. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting some element from sets.Set

2007-06-29 Thread Raymond Hettinger
> $ python -m timeit -s "s = set('abcdef')" "x = iter(s).next()" > 100 loops, best of 3: 0.399 usec per loop > > $ python -m timeit -s "s = set('abcdef')" "x = s.pop(); s.add(x)" > 100 loops, best of 3: 0.339 usec per loop > > So it looks like it's more efficient to use

Re: inverting a dictionary of lists

2007-06-14 Thread Raymond Hettinger
On Jun 14, 8:20 pm, [EMAIL PROTECTED] wrote: > I have a very large dictionary of lists: > d = {a:[1,2], b:[2,3], c:[3]} > and i want to reverse the associativity of the integers thusly: > inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]} Try using setdefault: >>> d = {'a':[1,2], 'b':[2,3], 'c':[3]

Re: iterblocks cookbook example

2007-06-02 Thread Raymond Hettinger
On Jun 2, 10:19 am, Steve Howell <[EMAIL PROTECTED]> wrote: > George Sakkis produced the following cookbook recipe, > which addresses a common problem that comes up on this > mailing list: ISTM, this is a common mailing list problem because it is fun to solve, not because people actually need it o

Re: Thread-safety of dict

2007-06-01 Thread Raymond Hettinger
On May 31, 9:12 pm, "Adam Olsen" <[EMAIL PROTECTED]> wrote: > It seems to be a commonly held belief that basic dict operations (get, > set, del) are atomic. They are atomic so long as the key does not have a custom __hash__, __eq__, or __cmp__ method which can trigger arbitrary Python code. With

Re: itertools.groupby

2007-05-29 Thread Raymond Hettinger
On May 28, 8:02 pm, Gordon Airporte <[EMAIL PROTECTED]> wrote: > "Each" seems to imply uniqueness here. Doh! This sort of micro-massaging the docs misses the big picture. If "each" meant unique across the entire input stream, then how the heck could the function work without reading in the entire

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
On May 28, 8:36 pm, "Carsten Haese" <[EMAIL PROTECTED]> wrote: > And while > we're at it, it probably should be keyfunc(value), not key(value). No dice. The itertools.groupby() function is typically used in conjunction with sorted(). It would be a mistake to call it keyfunc in one place and not

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
> > That's not for everyone, so it isn't a loss if > > someone sticks > > with writing plain, clear everyday Python instead of > > an itertool. > > I know most of the module is fairly advanced, and that > average users can mostly avoid it, but this is a very > common-antipattern that groupby() solv

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
On May 28, 8:34 am, 7stud <[EMAIL PROTECTED]> wrote: > >- there are two more examples on the next page. those two > > examples also give sample inputs and outputs. > > I didn't see those. Ah, there's the rub. The two sections of examples and recipes are there for a reason. This isn't a beginne

Re: itertools.groupby

2007-05-27 Thread Raymond Hettinger
On May 27, 8:28 pm, Paul Rubin wrote: > I use the module all the time now and it is great. Thanks for the accolades and the great example. FWIW, I checked in a minor update to the docs: +++ python/trunk/Doc/lib/libitertools.tex Mon May 28 07:23:22 2007 @@ -138,6 +13

Re: itertools.groupby

2007-05-27 Thread Raymond Hettinger
On May 27, 2:59 pm, Steve Howell <[EMAIL PROTECTED]> wrote: > These docs need work. Please do not defend them; > please suggest improvements. FWIW, I wrote those docs. Suggested improvements are welcome; however, I think they already meet a somewhat high standard of quality: - there is an accur

Re: 0 == False but [] != False?

2007-05-23 Thread Raymond Hettinger
> >>> [] == False > False > > Could anybody point out why this is the case? Writing, "if x" is short for writing "if bool(x)". Evaluating bool(x) checks for a x.__nonzero__() and if that method isn't defined, it checks for x.__len__() to see if x is a non-empty container. In your case, writing "i

Re: docs patch: dicts and sets

2007-05-20 Thread Raymond Hettinger
On May 13, 4:52 pm, [EMAIL PROTECTED] wrote: > Dismissing this as not a "real problem" is both wrong > and offensive to people taking the time to actually > propose improvements. I should have elaborated on what I meant by saying that there is not a real problem. Another way to put it is that the

Re: Many-to-many pattern possiable?

2007-05-19 Thread Raymond Hettinger
On May 19, 9:33 am, Jia Lu <[EMAIL PROTECTED]> wrote: > I see dict type can do 1-to-1 pattern, But is there any method to do > 1-to-many, many-to-1 and many-to-many pattern ? >>> mm = {'a': ['A', 'B', 'C'], 'c': ['C', 'D', 'E'], 'b': ['A', 'D']} >>> # Now, invert the relation >>> mmr = {} >>> fo

Re: RFC - n-puzzle.py

2007-05-19 Thread Raymond Hettinger
On May 18, 4:15 pm, Phoe6 <[EMAIL PROTECTED]> wrote: > Hi All, > I would like to request a code and design review of one of my program. > n-puzzle.pyhttp://sarovar.org/snippet/detail.php?type=snippet&id=83 > Its a N-puzzle problem solver ( Wikipedia page > andhttp://norvig.com/ltd/test/n-puzzle.li

Re: Generators in C code

2007-05-14 Thread Raymond Hettinger
like generators in many respects except that you are responsible for tracking state and jumping to an appropriate resume point. Being C, it won't be as convenient as Python generators, but should be able to translate any generator into equivalent C. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: docs patch: dicts and sets

2007-05-11 Thread Raymond Hettinger
On May 11, 5:59 pm, "Alan Isaac" <[EMAIL PROTECTED]> wrote: > This is an attempt to synthesize Bill and Carsten's proposals. > (I'm changing the subject line to better match the topic.) > > http://docs.python.org/lib/typesmapping.html:for footnote (3) > > Keys and values are listed in an ar

Re: change of random state when pyc created??

2007-05-09 Thread Raymond Hettinger
er, you've made non-guaranteed assumptions about the arbitrary ordering of an unordered collection -- a definite no-no). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting some element from sets.Set

2007-05-03 Thread Raymond Hettinger
more approach: x = s.pop() s.add(x) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: fastest way to find the intersection of n lists of sets

2007-04-30 Thread Raymond Hettinger
ad code that built new intermediate sets between step: common = s1 | s2 | s3 | s4 | s5 | s6 | s7 | s8 | s9 Instead, it is faster to build-up a single result set: common = set() for s in s1, s2, s3, s4, s5, s6, s7, s8, s9: common |= s Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: fastest way to find the intersection of n lists of sets

2007-04-30 Thread Raymond Hettinger
the hash values of their elements. Accordingly, binary set operations can run without any calls to element.__hash__(). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: While we're talking about annoyances

2007-04-29 Thread Raymond Hettinger
[Steven D'Aprano] > I recently needed to write a function to generate a rank table from a > list. That is, a list of ranks, where the rank of an item is the position > it would be in if the list were sorted: > > alist = list('defabc') > ranks = [3, 4, 5, 0, 1, 2] . . . > def rank(sequence): > t

Re: Support for new items in set type

2007-04-26 Thread Raymond Hettinger
e set of things you've deleted since them FWIW, I've posted a trial implementation in the Python cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/511496 Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: lambda generator - curious behavior in 2.5

2007-04-21 Thread Raymond Hettinger
y be: if (result 1= NULL && f->f_stacktop == NULL) Please open a bug report on SF and assign to me. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Review/commit patch?

2007-04-05 Thread Raymond Hettinger
> > [Kevin Walzer] > >> How long does it take for a patch at the Python SF tracker to be > >> reviewed and/or committed? I am unfamiliar with how the process works. > > >> (I originally submitted a bug report, then figured out how to patch the > >> item in question, and subsequently submitted a pat

Re: Review/commit patch?

2007-04-05 Thread Raymond Hettinger
[Kevin Walzer] > How long does it take for a patch at the Python SF tracker to be > reviewed and/or committed? I am unfamiliar with how the process works. > > (I originally submitted a bug report, then figured out how to patch the > item in question, and subsequently submitted a patch.) Which bug

Re: challenge ?

2007-03-22 Thread Raymond Hettinger
On Mar 22, 9:41 am, "alain" <[EMAIL PROTECTED]> wrote: > I have a problem I wonder if it has been solved before. > I have a dictionnary and I want the values in the dictionnary to be > annotated with the rank that would be obtained by sorting the values > > def annotate_with_rank(my_dict): >

Re: performance question

2007-03-14 Thread Raymond Hettinger
[Eric Texier] > I need speed here. What will be the fastest method or does it matter? Follow Alex's advice and use the timeit module, but do not generalize from too small examples; otherwise, the relative timings will be thrown-off by issues like the time to lookup "write" and "a" and "str" (all of

Re: number generator

2007-03-14 Thread Raymond Hettinger
[Alex Martelli] > map([random.randrange(5) for i in xrange(45)].count, xrange(5)) > > i.e., this gives 5 integers (each between 0 and 45 included) summing to > 45 -- add 1 to each of them to get the desired result. This is a really nice approach. Besides being fast, it is not too hard to see th

Re: number generator

2007-03-14 Thread Raymond Hettinger
> To make the solutions equi-probable, a simple approach is to > recursively enumerate all possibilities and then choose one of them > with random.choice(). Since people are posting their solutions now (originally only hints were provided for the homework problem), here's mine: def genpool(n, m):

Re: number generator

2007-03-10 Thread Raymond Hettinger
On Mar 9, 7:32 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, cesco wrote: > > Given two positive integers, N and M with N < M, I have to generate N > > positive integers such that sum(N)=M. No more constraints. > > Break it into subproblems. Generate a random nu

Re: Descriptor/Decorator challenge

2007-03-06 Thread Raymond Hettinger
[George Sakkis] > What would the semantics be if m is decorated as local only in A or > only in B ? The goal is to as closely as possible emulate the sematics of under- under name mangling. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: calendar (date) iterator?

2007-03-06 Thread Raymond Hettinger
[Marcus] > I'm looking for useful starting points, suggestions, and sample code, > to implement a calendar iterator. Simply, the iterator is seeded with > an initial calendar date, e.g., "03-12-2006", and then subsequent > calls to next return subsequent dates. The seed could be a standard > cale

Re: list-like behaviour of etree.Element

2007-03-04 Thread Raymond Hettinger
On Mar 4, 12:48 pm, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > The etree.Element (or ElementTree.Element) supports a number of > list-like methods: append, insert, remove. Any special reason why it > doesn't support pop and extend (and maybe count)? Those methods would not be hard to add. Perh

Descriptor/Decorator challenge

2007-03-04 Thread Raymond Hettinger
I had an idea but no time to think it through. Perhaps the under-under name mangling trick can be replaced (in Py3.0) with a suitably designed decorator. Your challenge is to write the decorator. Any trick in the book (metaclasses, descriptors, etc) is fair game. Raymond how we currentl

Re: Python stock market analysis tools?

2007-03-04 Thread Raymond Hettinger
On Mar 4, 7:52 pm, "Mudcat" <[EMAIL PROTECTED]> wrote: > I have done a bit of searching and can't seem to find a stock market > tool written in Python that is active. Anybody know of any? I'm trying > not to re-create the wheel here. What kind of tool do you want? Getting quotes is the easy part:

Re: pop method question

2007-03-03 Thread Raymond Hettinger
nd then in the context of set operations). There was also a notion that threaded programming would benefit by having lookup-then-delete as an atomic transaction. It is unknown to me whether that purported benefit has ever been realized. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Need an identity operator because lambda is too slow

2007-02-17 Thread Raymond Hettinger
[Deron Meranda >] I'm looking for something in > Python which would act like an identity operator, or I-combinator: a > do-nothing function. The best I know of is: (lambda x: x), but it is > not ideal. File a feature request on SourceForge and assign to me. This has come up a couple of times a

Re: builtin set literal

2007-02-15 Thread Raymond Hettinger
>> What about "{,}"? For consistency "(,)" and "[,]" might >> also have to be permissible, and maybe even "{:}" for an >> empty dict. The notion of a set literal was rejected in PEP 218, http://www.python.org/dev/peps/pep-0218/ . One of the reasons for the rejection was that the small benefit of a

Re: Fast constant functions for Py2.5's defaultdict()

2007-02-14 Thread Raymond Hettinger
On Feb 13, 5:09 pm, Giovanni Bajo <[EMAIL PROTECTED]> wrote: > > The itertools.repeat(const).next approach wins on speed and > > flexibility. > > But it's the most unreadable too. Not really. It's unusual but plenty readable (no surprise that repeat(0) repeatedly gives you zero). I think it more

Fast constant functions for Py2.5's defaultdict()

2007-02-13 Thread Raymond Hettinger
FWIW, here are three ways of writing constant functions for collections.defaultdict(): d = defaultdict(int) # slowest way; works only for zero d = defaultdict(lambda: 0) # faster way; works for any constant d = defaultdict(itertools.repeat(0).next)# fastest way; works for any con

Re: Evolving doctests for changing output format

2007-01-10 Thread Raymond Hettinger
[EMAIL PROTECTED] > What I'd like is if I could get doctest to take my tests, and > substitute the obtained output for the provided output. There's currently no support for auto-updating doctests. I think it would make for a good feature request. In the meantime, it may not be difficult to roll

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread Raymond Hettinger
dwelden wrote: > I have successfully used the sort lambda construct described in > http://mail.python.org/pipermail/python-list/2006-April/377443.html. > However, how do I take it one step further such that some values can be > sorted ascending and others descending? Easy enough if the sort values

Re: trees

2006-12-18 Thread Raymond Hettinger
vertigo wrote: > What library/functions/classes could i use to create trees ? Start with random.seed, login as root, use svn to download the trunk and branches, when Spring arrives, the leaves will fill-in ;-) Or just use lists as Fredrik suggested. Or look at an example in the cookbook: http://

Re: How to subclass sets.Set() to change intersection() behavior?

2006-12-12 Thread Raymond Hettinger
[mkppk] > I have kind of strange change I'd like to make to the sets.Set() > intersection() method.. > > Normally, intersection would return items in both s1 and s2 like with > something like this: s1.intersection(s2) . . . > - the lists I am working with are small, like 1-10 items each from set

Re: Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__

2006-12-12 Thread Raymond Hettinger
[Almad] > I discovered this behaviour in dictionary which I find confusing. In > SneakyLang, I've tried to extend dictionary so it visits another class > after something is added: > > class RegisterMap(dict): > def __setitem__(self, k, v): > dict.__setitem__(self, k,v) > self[k]

Re: multi split function taking delimiter list

2006-11-14 Thread Raymond Hettinger
[EMAIL PROTECTED] wrote: > Hi, I'm looking for something like: > > multi_split( 'a:=b+c' , [':=','+'] ) > > returning: > ['a', ':=', 'b', '+', 'c'] > > whats the python way to achieve this, preferably without regexp? I think regexps are likely the right way to do this kind of tokenization. The s

Re: enumerate improvement proposal

2006-10-31 Thread Raymond Hettinger
James Stroud wrote: > I think that it would be handy for enumerate to behave as such: > > def enumerate(itrbl, start=0, step=1): >i = start >for it in itrbl: > yield (i, it) > i += step I proposed something like this long ago and Guido has already rejected it. Part of the reason

Re: "filtered view" upon lists?

2006-09-12 Thread Raymond Hettinger
unction=None): self._actual_list = actual_list self._filter_function = filter_function ### Example calls a = range(10) b = ListView(a, lambda x: x%2==0) print list(b), len(b), b[2], b[:3] a[:] = range(10,20) print list(b), len(b), b[2], b[:3] Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Jumping over in the class hierarchy

2006-08-01 Thread Raymond Hettinger
Pupeno wrote: > I want to jump over a method in the class hierarchy, that is: If I have > class A(object), clas B(A), class C(B) and in C, I want a method to do > exactly what A does but not what B does in its reimplementation, would it > be correct to do: super(A, super(B, self)).method() in C ?

Re: random shuffles

2006-07-21 Thread Raymond Hettinger
Boris Borcic wrote: > does > > x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) > > pick a random shuffle of x with uniform distribution ? > > Intuitively, assuming list.sort() does a minimal number of comparisons to > achieve the sort, I'd say the answer is yes. But I don't feel quite > confo

Re: split a line, respecting double quotes

2006-07-10 Thread Raymond Hettinger
Sion Arrowsmith wrote: > >>> csv.reader(StringIO.StringIO('a b c "d "" e"'), delimiter=' ').next() > ['a', 'b', 'c', 'd " e'] > isn't far off. > > On the other hand, it's kind of a stupid solution. IMO, this solution is on the right track. FWIW, the StringIO wrapper is unnecessary. Any iterable wi

Re: itertools.count() as built-in

2006-05-29 Thread Raymond Hettinger
[EMAIL PROTECTED] wrote: > Is there any chance of itertools.count() ever becoming one of the > built-in functions? That's unlikely. The goal is to have fewer builtins rather than more. Utility and frequency are not the only considerations; otherwise glob.glob, sys.stderr, print.pprint, copy.copy,

Re: iterator? way of generating all possible combinations?

2006-05-26 Thread Raymond Hettinger
[EMAIL PROTECTED] wrote: > Ok, this is really irritating me. I'm sure there are different ways of > doing this - I'm interested in the algo, not the practical solution, > I'm more trying to play with iterators and recursion. I want to create > a program that generates every possible combination

Re: Feature request: sorting a list slice

2006-05-21 Thread Raymond Hettinger
Getting a patch ready for checkin (corrected, documented, reviewed, and tested) is only part of the battle. The real challenge of language design is figuring out whether something should be done. Adding optional parameters to a method makes its invocation slower and makes the API harder to learn

Re: Feature request: sorting a list slice

2006-05-18 Thread Raymond Hettinger
George Sakkis wrote: > It would be useful if list.sort() accepted two more optional > parameters, start and stop, so that you can sort a slice in place. In > other words, > > x = range(100) > x.sort(start=3, stop=-1) > > would be equivalent to > > x[3:-1] = sorted(x[3:-1]) > > but more efficien

Re: list.clear() missing?!?

2006-04-13 Thread Raymond Hettinger
[Dan Christensen] > It's true that this runs at the same speed as the del variants on my > machine. That's not too surprising to me, but I still don't > understand why the del variants are more than 5% faster than the first > version. Understanding it involves looking at implementation specific d

Re: list.clear() missing?!?

2006-04-13 Thread Raymond Hettinger
> > * the request is inane, the underlying problem is trivial, and the > > relevant idiom is fundamental (api expansions should be saved for rich > > new functionality and not become cluttered with infrequently used > > redundant entries) > > Is this sort of editorialising fair, or just a way of no

Re: list.clear() missing?!?

2006-04-12 Thread Raymond Hettinger
[Felipe Almeida Lessa] > > I love benchmarks, so as I was testing the options, I saw something very > > strange: > > > > $ python2.4 -mtimeit 'x = range(10); ' > > 100 loops, best of 3: 6.7 msec per loop > > $ python2.4 -mtimeit 'x = range(10); del x[:]' > > 100 loops, best of 3: 6.35 msec

Re: list.clear() missing?!?

2006-04-12 Thread Raymond Hettinger
[Steven Bethard] > I think these are all good reasons for adding a clear method, but being > that it has been so hotly contended in the past, I don't think it will > get added without a PEP. Anyone out there willing to take out the best > examples from this thread and turn it into a PEP? Somethin

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
[Noah] > I suggested the above because it wasn't obvious to me how one would > pass the arbitrary set of attributes to the lambda expression (and I > envisioned them being specified as strings in this case, since the set > of attributes will be coming from a web form). > > So what about the followi

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
Azolex: > Let's push the diagnosis a bit further : the aversion to the keyword > "lambda" has to do with the fact that it ignores the english word used > by all non-geeks to convey the meaning, eg "given" Right. However, Guido has said that lambda is here to stay, so it's time to get over it. R

Re: Manipulating sets from the 2.4 C API?

2006-04-11 Thread Raymond Hettinger
Dave Opstad wrote: > I just looked through the Python/C API reference for 2.4.3 and didn't > see anything about using sets. I'd been expecting things like PySet_New, > PySet_Check etc. In Py2.4, there was not a custom set C API because the module was still ungoing significant development. For 2.4

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Raymond Hettinger
[George Young] >> For multiple keys the form is quite analogous: >> >> L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) [Noah] > If you are lambda-phobic (as I am) this should also work for an > arbitrary set of attributes: > > attrs = 'attr1 attr2 attr3'.split() > sortlist = [[getat

Re: Good thread pool module

2006-03-22 Thread Raymond Hettinger
David Hirschfield wrote: > There isn't a thread pool module in the standard library, but I'm sure > many have been written by people in the python community. > Anyone have a favorite? Is there one particular implementation that's > recommended? Because of the GIL, thread pools are not as useful in

TaskQueue

2006-03-21 Thread Raymond Hettinger
I would like to get feedback on an idea I had for simplifying the use of queues with daemon consumer threads Sometimes, I launch one or more consumer threads that wait for a task to enter a queue and then work on the task. A recurring problem is that I sometimes need to know if all of the tasks ha

Re: trying to find repeated substrings with regular expression

2006-03-14 Thread Raymond Hettinger
[Robert Dodier] > I'm trying to find substrings that look like 'FOO blah blah blah' > in a string. For example give 'blah FOO blah1a blah1b FOO blah2 > FOO blah3a blah3b blah3b' I want to get three substrings, > 'FOO blah1a blah1b', 'FOO blah2', and 'FOO blah3a blah3b blah3b'. No need for regular

Re: Memory visualization

2006-03-14 Thread Raymond Hettinger
[bearophile] > During the execution of a Icon script there are ways to visualize the > memory: > http://www.cs.arizona.edu/icon/progvis/memmon/memmon.htm . . . > I'd like to know if for Python there is a similar program to > dynamically see the memory in a similar way. > If such tool doesn't exist

Re: "RuntimeError: dictionary changed size during iteration" ; Good atomic copy operations?

2006-03-13 Thread Raymond Hettinger
[robert] > In very rare cases a program crashes (hard to reproduce) : > > * several threads work on an object tree with dict's etc. in it. Items > are added, deleted, iteration over .keys() ... ). The threads are "good" > in such terms, that this core data structure is changed only by atomic > oper

Re: Global Threading Lock 2 - Re: "RuntimeError: dictionary changed size during iteration"..

2006-03-13 Thread Raymond Hettinger
[robert] > That queue/passing-through-only-an-extra-global-var communication is > acceptable for thin thread interaction. > ( hope this extra global var is thread-safe in future Python's :-) ) > > But "real" thread-programming should also be possible in Python - and it > is with the usual disciplin

Re: Q's: pythonD and range(1,12)

2006-03-13 Thread Raymond Hettinger
John Savage wrote: > Could > someone please explain the rationale behind python designers' thinking > in deciding the function "range(1,12)" should return the sequence 1 to > 11 rather than the more intuitively-useful 1 to 12?? There are several ways to do this, closed intervals, half-open interva

Re: "RuntimeError: dictionary changed size during iteration" ; Good atomic copy operations?

2006-03-12 Thread Raymond Hettinger
[robert] > In very rare cases a program crashes (hard to reproduce) : > > * several threads work on an object tree with dict's etc. in it. Items > are added, deleted, iteration over .keys() ... ). The threads are "good" > in such terms, that this core data structure is changed only by atomic > oper

Re: time series calculation in list comprehension?

2006-03-12 Thread Raymond Hettinger
[Peter Otten] > from __future__ import division > > from itertools import islice, tee, izip . . . > def moving_average2(items, n): > first_items, last_items = tee(items) > accu = sum(islice(last_items, n-1)) > for first, last in izip(first_items, last_items): > accu += last >

Re: PEP 354: Enumerations in Python

2006-03-01 Thread Raymond Hettinger
[Ben Finney] > It is possible to simply define a sequence of values of some other > basic type, such as ``int`` or ``str``, to represent discrete > arbitrary values. However, an enumeration ensures that such values > are distinct from any others, and that operations without meaning > ("Wednesday t

Re: Optimize flag question

2006-02-25 Thread Raymond Hettinger
[Olivier Langlois] > > So my question is: what are the 'optimizations' that the Python > > interpreter is doing when you specify the optimize flag and is there > > anything I should be cautious about when using it? Currently, -O provides no optimizations other than eliminating assertions. Raymon

Re: Can optparse do dependencies?

2006-02-25 Thread Raymond Hettinger
[Bob] > I'd like to setup command line switches that are dependent on other > switches, similar to what rpm does listed below. From the grammar below > we see that the "query-options" are dependent on the query switch, > {-q|--query}. Can "optparse" do this or do I have to code my own > "thing"? Th

Re: Psychic bug

2006-02-23 Thread Raymond Hettinger
> a mutate function for a genetic algorithm which is giving me > odd results. I suspect I'm missing somthing really simple, so I'd be > grateful for any suggestions. Basically, when I comment out the line > which is commented out below, it works fine (although of course it > doesn't change the gen

Re: list assignment

2006-02-22 Thread Raymond Hettinger
> [spam, ham] = ['yum', 'YUM'] > > I don't see how this is any different than a tuple unpacking assignment: > > >>> a, b = 1, 2 It's not different. They are ways of writing the same thing. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Multiplication optimization

2006-02-18 Thread Raymond Hettinger
[Paul McGuire] > Does Python's run-time do any optimization of multiplication > operations, like it does for boolean short-cutting? Usually, it is safest (and typically true) to assume that Python performs no optimizations. To go beyond making assumptions, it is easy to run a few timings: >>> fr

Re: getting the line just before or after a pattern searched

2006-02-17 Thread Raymond Hettinger
[EMAIL PROTECTED] wrote: > hi > > i have a file something like this > > abcdefgh > ijklmnopq > 12345678 > rstuvwxyz > . > . > . > 12345678 > . > > whenever i search the file and reach 12345678, how do i get the line > just above and below ( or more than 1 line above/below) the patte

Re: Add a month

2006-02-17 Thread Raymond Hettinger
[EMAIL PROTECTED] > Hi, this is probably a really simple question but... > How do you add a month to a datetime date in python? It would be nice > if you could do something like: > > d = datetime.date(2006,2,17) > dm = datetime.timedelta(months=1) > d_new = d + dm > > but timedelta doesn't have a

Re: Does python have an internal list/dictionary of functions?

2006-02-17 Thread Raymond Hettinger
[Carl J. Van Arsdall] > basically we have: > > >>>def functA(): > ... pass > > >>> functA > > > And what I'd like to do is: > > >>>__internalFuncDict__['functA'] > globals()['functA'] -- http://mail.python.org/mailman/listinfo/python-list

Re: Soduku

2006-02-15 Thread Raymond Hettinger
[Jack Diederich] > Is my math off or does 27ms mean 0.027 seconds? On my laptop (1.3GHz) > an empty python program takes 10ms to run (0.010 secs). I ask out of > vanity, my own solver takes .15 seconds to run (20 seconds for a 16x16 grid). Comparisons for individual puzzles are likely to be meani

Re: Unexpected behaviour of getattr(obj, __dict__)

2006-02-14 Thread Raymond Hettinger
Steven D'Aprano wrote: > I came across this unexpected behaviour of getattr for new style classes. > Example: > > >>> class Parrot(object): > ... thing = [1,2,3] > ... > >>> getattr(Parrot, "thing") is Parrot.thing > True > >>> getattr(Parrot, "__dict__") is Parrot.__dict__ > False > > I would

Re: itertools examples

2006-02-11 Thread Raymond Hettinger
[Felipe Almeida Lessa] > IMHO, on http://www.python.org/doc/current/lib/itertools-example.html , > shouldn't the part > > >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): > ... print map(operator.itemgetter(1), g) > > be > > >>> for k, g in groupby(enumerate(data), lambda (i, x): i-x

Re: ordered sets operations on lists..

2006-02-10 Thread Raymond Hettinger
[Amit Khemka] > > Hello, Is there a *direct* way of doing set operations on lists which > > preserve the order of the input lists ? > > For Ex. l1 = [1, 5, 3, 2, 4, 7] > > l2 = [3, 5, 10] > > > > and (l1 intersect l2) returns [5, 3] (and (l2 intersect l1) [bonono] > what d

Re: Question about idioms for clearing a list

2006-02-10 Thread Raymond Hettinger
> > If you're asking why list's don't have a clear() method, the answer is > > that they already had two ways to do it (slice assignment and slice > > deletion) and Guido must have valued API compactness over collection > > polymorphism. The latter is also evidenced by set.add() vs > > list.append

Re: Question about idioms for clearing a list

2006-02-10 Thread Raymond Hettinger
[Alex Martelli] > I was thinking of something different again, from a use case I did have: > > def buncher(sourceit, sentinel, container, adder, clearer): > for item in sourceit: > if item == sentinel: > yield container > clearer() > else > ad

Re: Question about idioms for clearing a list

2006-02-07 Thread Raymond Hettinger
[Ed Singleton] > Is it obvious to a newbie what the difference between mappings and > "not-mappings", and is it obvious exactly what is and isn't a mapping? FWIW, there is a glossary in the tutorial: http://docs.python.org/tut/node18.html -- http://mail.python.org/mailman/listinfo/python-lis

Re: Question about idioms for clearing a list

2006-02-07 Thread Raymond Hettinger
> I'm a fairly average programmer (better than average compared to my > immediate colleagues). I've read every tutorial I can get my hands > on, but I have no _memory_ of ever coming across the del keyword, let > alone that it is fundamental to Python, My thought is that get/set/del are fundamen

Re: Question about idioms for clearing a list

2006-02-07 Thread Raymond Hettinger
[EMAIL PROTECTED] > In my programs I have seen that there is another practical difference > between version 1 and 3: > (1) mylist[:] = [] > (3) mylist = [] > If you create a big mylist again and again many times, the version 1 > uses the memory more efficiently (probably less work for the garbage >

Re: Question about idioms for clearing a list

2006-02-07 Thread Raymond Hettinger
re was a pithy Tim Peters quotation to the effect that he was unpersuaded by language proposals predicated on some hypothetical average programmer not being smart enough to understand something that the rest of us find to be basic. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: * 'struct-like' list *

2006-02-06 Thread Raymond Hettinger
[Ernesto] > I'm still fairly new to python, so I need some guidance here... > > I have a text file with lots of data. I only need some of the data. I > want to put the useful data into an [array of] struct-like > mechanism(s). The text file looks something like this: > > [BUNCH OF NOT-USEFUL DAT

<    1   2   3   4   5   6   7   8   >