Re: efficient running median

2009-10-13 Thread Raymond Hettinger
On Oct 13, 11:57 am, sturlamolden wrote: > On 13 Okt, 18:33, Paul Rubin wrote: > > > The obvious way to compute a running median involves a tree structure > > so you can quickly insert and delete elements, and find the median. > > That would be asymtotically O(n log

Re: The rap against "while True:" loops

2009-10-14 Thread Raymond Hettinger
> And I know somebody, in other languages, thinks > it's a Best Practice to avoid using exceptions for flow control. Ah, now we have two code prions in just one thread. I hope no newbie or supervisor reads this thread and latches on to those two counter-productive ideas. ISTM, both ideas are dang

Re: efficient running median

2009-10-15 Thread Raymond Hettinger
[Janto Dreijer] > I found a PDF by Soumya D. Mohanty entitled "Efficient Algorithm for > computing a Running Median" (2003) by Googling. It has code snippets > at the end, but it's not going to be a simple cut-and-paste job. It > will take some work figuring out the missing parts. See http://code.

Re: slicing return iter?

2009-10-17 Thread Raymond Hettinger
[StarWing] > > > sometimes I want to iterate a part of a sequence. but don't want to > > > copy it. i.e. . . . > I had checked it for serval times. maybe it's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index -- that's supporte

Re: slicing return iter?

2009-10-18 Thread Raymond Hettinger
[Raymond] > > If it really is a sequence (with len and getitem), you can write your > > own indexing iterator: > > >   def myslice(seq, start, stop, step): > >        'Allow forward or backwards iteration over a subslice' > >        for i in range(start, stop, step): > >            yield seq[i] [S

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Raymond Hettinger
> > On Nov 7, 2009, at 9:44 AM, Ray Holt wrote: > > >       I am taking the MIT online course Introduction to Computer Science and > >       Programming. I have a assignment to write a program to compute and > > print > >       the 1000th. prime number. Can someone give me some leads on the > > c

Re: My own accounting python euler problem

2009-11-11 Thread Raymond Hettinger
[vsoler] > In the accounting department I am working for we are from time to time > confronted to the following problem: > > A customer sends us a check for a given amount, but without specifying > what invoices it cancels. It is up to us to find out which ones the > payment corresponds to. > > For

Re: Does turtle graphics have the wrong associations?

2009-11-12 Thread Raymond Hettinger
On Nov 11, 10:21 pm, "Alf P. Steinbach" wrote: > One reaction to http://preview.tinyurl.com/ProgrammingBookP3> has > been that turtle graphics may be off-putting to some readers because it is > associated with children's learning. > > What do you think? How about calling it Raptor Graphics that w

Re: faster than list.extend()

2009-11-16 Thread Raymond Hettinger
On Nov 16, 2:41 pm, Chris Rebert wrote: > On Mon, Nov 16, 2009 at 2:30 PM, Hyunchul Kim > > wrote: > > Hi, all. > > > I want to improve speed of following simple function. > > Any suggestion? > > > ** > > def triple(inputlist): > >   results = [] > >   for x in inputlist: > >     results.

Re: python bijection

2009-11-21 Thread Raymond Hettinger
On Nov 19, 3:24 pm, Joshua Bronson wrote: > I couldn't find a library providing a bijective map data structure > (allowing for constant-time lookups by value) in the few minutes I > looked, so I took a few more minutes to code one > up:http://bitbucket.org/jab/toys/src/tip/bijection.py > > Is thi

Re: Your impression of for-novice writings on assertions

2010-02-02 Thread Raymond Hettinger
On Feb 2, 12:54 pm, "Alf P. Steinbach" wrote: > The first topic is about assertions and exceptions. I wonder whether this text > is easy or difficult to understand for a beginner. Or any improvements that > could be made. To my eyes it reads nicely. You may want to try it out on a real beginner

Selenium/SauceLabs OpenSpace at Pycon

2010-02-03 Thread Raymond Hettinger
For those who are interested, the Sauce Labs team, http://saucelabs.com/about/team, is hosting two free tutorial open space sessions at Pycon in Atlanta. In the short session, people bringing their laptops should be able to record a web session in their browser, convert the recorded activity to a

Re: Selenium/SauceLabs OpenSpace at Pycon

2010-02-04 Thread Raymond Hettinger
> >For those who are interested, the Sauce Labs team, > >http://saucelabs.com/about/team, is hosting two free tutorial open > >space sessions at Pycon in Atlanta. [Aahz] > Congrats on the new job! Thanks. I'm really enjoying working with Jim Baker and Frank Wierzbicki. Raymond -- http://mail

Re: method to intercept string formatting % operations

2010-02-05 Thread Raymond Hettinger
On Feb 5, 6:57 am, bradallen wrote: > Hello, > > For container class derived from namedtuple, but which also behaves > like a dictionary by implementing __getitem__ for non-integer index > values, is there a special reserved method which allows intercepting % > string formatting operations? I woul

Re: Python 3: Plist as OrderedDict

2010-02-08 Thread Raymond Hettinger
On Feb 8, 8:02 pm, Gnarlodious wrote: > I am trying to read a *.plist into Python 3's OrderedDict but can't > figure it out. Saying something like this: ... > I "upgraded" to Py3 to have OrderedDict, so please don't say it is > impossible... You may be able to monkey patch an OrderedDict into the

Re: To (monkey)patch or not to (monkey)patch, that is the question

2010-02-09 Thread Raymond Hettinger
On Feb 9, 12:54 am, George Sakkis wrote: > So I'm wondering if there is a consensus on when it's better to (hard) > patch, monkey patch or just try to work around a third party package > that doesn't do exactly what one would like. Does it have mainly to do > with the reason for the patch (e.g. fi

Re: collections use __next__() in python 2.6?

2010-02-26 Thread Raymond Hettinger
On Feb 26, 10:08 am, Gary Robinson wrote: > The Python 2.6.4 docs for collections > athttp://docs.python.org/library/collections.htmlsay that __next__() is an > abstract method for the Iterable ABC. But my understanding is that __next__() > isn't supposed to be used until Python 3. Also, I'm us

Re: random.gauss: range

2010-02-26 Thread Raymond Hettinger
On Feb 26, 1:26 pm, pistacchio wrote: > hi, > i'm trying the random.gauss function. can anyone explain how to get a > number between a given range? like, from 0 to 20 with an average of > 10? and how to determine the "steep" of the curve? i've never studied > it, so mu and sigma don't really tell

Re: Queue peek?

2010-03-02 Thread Raymond Hettinger
On Mar 2, 8:29 am, Veloz wrote: > Hi all > I'm looking for a queue that I can use with multiprocessing, which has > a peek method. > > I've seen some discussion about queue.peek but don't see anything in > the docs about it. > > Does python have a queue class with peek semantics? Am curious about

Re: negative "counts" in collections.Counter?

2010-03-07 Thread Raymond Hettinger
MyCounter(Counter): def __sub__(self, other): result = self.copy() for elem, cnt in other.items(): result[elem] -= cnt Hopes this gives you some insight into the design choices. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Steven D'Aprano] > Thanks for the explanation Raymond. A few comments follow: You're welcome :-) > Would you consider a feature enhancement adding an additional method, > analogous to update(), to perform subtractions? I recognise that it's > easy to subclass and do it yourself, but there does

Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Vlastimil Brom] > Thank you very much for the exhaustive explanation Raymond! You're welcome. > I am by far not able to follow all of the mathematical background, but > even for zero-truncating multiset, I would expect the truncation on > input rather than on output of some operations. I debat

Re: negative "counts" in collections.Counter?

2010-03-08 Thread Raymond Hettinger
[Gregory Ewing] > I think you should be providing two types: one is a > multiset, which disallows negative counts altogether; > the other behaves like a sparse vector with appropriate > arithmetic operations. That is pretty close to what I said above: """ In this case, we have an indication that

Re: short-circuiting any/all ?

2010-03-22 Thread Raymond Hettinger
On Mar 22, 7:45 am, kj wrote: > I have a list of items L, and a test function is_invalid that checks > the validity of each item.  To check that there are no invalid > items in L, I could check the value of any(map(is_invalid, L)). > But this approach is suboptimal in the sense that, no matter wha

Re: Classes as namespaces?

2010-03-27 Thread Raymond Hettinger
On Mar 26, 7:49 am, kj wrote: > What's the word on using "classes as namespaces"?  E.g. > > class _cfg(object): >     spam = 1 >     jambon = 3 >     huevos = 2 > > breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos) Works for me. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Binary Decimals in Python

2010-03-30 Thread Raymond Hettinger
On Mar 30, 8:13 am, aditya wrote: > To get the decimal representation of a binary number, I can just do > this: > > int('11',2) # returns 3 > > But decimal binary numbers throw a ValueError: > > int('1.1',2) # should return 1.5, throws error instead. > > Is this by design? It seems to me that this

Re: sort array, apply rearrangement to second

2010-03-31 Thread Raymond Hettinger
On Mar 30, 4:25 pm, s...@sig.for.address (Victor Eijkhout) wrote: > I have two arrays, made with numpy. The first one has values that I want > to use as sorting keys; the second one needs to be sorted by those keys. > Obviously I could turn them into a dictionary  of pairs and sort by the > first m

Re: sorting ascending/descending with operator.attrgetter

2010-03-31 Thread Raymond Hettinger
On Mar 31, 10:08 am, Chris Curvey wrote: > I must be having a brain cramp.  Given a list of objects, how can I > sort the list on one attribute in descending order, then sort within > each group in ascending order on another attribute. > > For example: > > class Foo: >     def __init__(self, a, b,

Re: [RELEASED] Python 3.2 alpha 3

2010-11-16 Thread Raymond Hettinger
On Nov 16, 9:23 am, Mark Summerfield wrote: > I think it might be worth mentioning in What's New: FWIW, I'll be updating the What's New document for the Beta. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-11-24 Thread Raymond Hettinger
I'm writing-up more guidance on how to use super() and would like to point at some real-world Python examples of cooperative multiple inheritance. Google searches take me to old papers for C++ and Eiffel, but that don't seem to be relevant to most Python programmers (i.e. a WalkingMenu example whe

Re: Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-11-25 Thread Raymond Hettinger
On Nov 24, 9:16 pm, Alice Bevan–McGregor wrote: > On 2010-11-24 12:08:04 -0800, Raymond Hettinger said: > > > I'm writing-up more guidance on how to use super() and would like to > > point at some real-world Python examples of cooperative multiple > > inheritance

Re: Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-11-25 Thread Raymond Hettinger
On Nov 25, 3:38 pm, John Nagle wrote: >     Multiple inheritance in Python is basically what fell out of > CPython's internals, not a design.   Sorry to disagree. That is historically inaccurate. Guido designed super() on purpose. He took his cues from "Putting Metaclasses to Work" by Ira Forma

Re: Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-11-25 Thread Raymond Hettinger
[Paul Rubin] > I'd mention the SocketServer library, except I'm not sure what you > mean by "cooperative", so I don't know if that counts. Cooperative multiple inheritance is a specific problem when there is a diamond diagram with the same method name needing to be called on multiple paths and eac

Re: Needed: Real-world examples for Python's Cooperative Multiple Inheritance

2010-11-28 Thread Raymond Hettinger
On Nov 28, 4:36 am, coldpizza wrote: > Did you try google code search? It is *not* the same as google code > hosting. > The site ishttp://www.google.com/codesearchand you can select Python > in the 'language' dropdown. Yes, I use Google's code search frequently and did try it for super(). However

Re: move to end, in Python 3.2 Really?

2011-01-17 Thread Raymond Hettinger
On Jan 17, 6:51 pm, nn wrote: > ...But the api on this baffles me a bit: > > >>> d = OrderedDict.fromkeys('abcde') > >>> d.move_to_end('b', last=False) > >>> ''.join(d.keys) > > 'bacde' > > I understand that "end" could potentially mean either end, but would > "move_to_end" and "move_to_beginning"

Re: UTF-8 question from Dive into Python 3

2011-01-18 Thread Raymond Hettinger
On Jan 17, 2:19 pm, carlo wrote: > Hi, > recently I had to study *seriously* Unicode and encodings for one > project in Python but I left with a couple of doubts arised after > reading the unicode chapter of Dive into Python 3 book by Mark > Pilgrim. > > 1- Mark says: > "Also (and you’ll have to t

Re: Wrappers in python

2011-01-27 Thread Raymond Hettinger
On Jan 27, 4:10 am, sl33k_ wrote: > What are wrappers? > >  What entities do they wrap around? > > Struggling to understand the concept. http://www.castle-cadenza.demon.co.uk/wrapper.htm Raymond -- http://mail.python.org/mailman/listinfo/python-list

Use the Source Luke

2011-01-28 Thread Raymond Hettinger
I hoping a new trend will start with dev's putting direct source code links in their documentation: http://rhettinger.wordpress.com/2011/01/28/open-your-source-more/ I'm looking for more examples of projects that routinely link their docs back into relavant sections of code. Have any of you all

Re: Use the Source Luke

2011-01-28 Thread Raymond Hettinger
[Jack Diedrich] > I think you overestimate how common it used to be to carry around the > sourcecode for the software you use compared to now;  In the past it > wasn't even always possible - if the Sun cc compiler core dumps you > have no recourse to code. You're right of course. For the Python w

Re: Use the Source Luke

2011-01-29 Thread Raymond Hettinger
On Jan 29, 3:22 am, TP wrote: > On Fri, Jan 28, 2011 at 10:32 AM, Raymond Hettinger wrote: > > I hoping a new trend will start with dev's putting direct > > source code links in their documentation: > > >  http://rhettinger.wordpress.com/2011/01/28/open-your-sourc

Re: Use the Source Luke

2011-01-29 Thread Raymond Hettinger
On Jan 28, 3:10 pm, Ben Finney wrote: > Raymond Hettinger writes: > > The rest of the blame lies with installers. They all treat > > human-readable scripts like they were binaries and tuck the code away > > in a dark corner. > > That’s hardly a “blame” of installers

Re: Use the Source Luke

2011-01-31 Thread Raymond Hettinger
On Jan 30, 6:47 am, Tim Wintle wrote: > +1 - I think the source links are very useful (and thanks for pushing > them). Happy to do it. > However I think the biggest changes that have probably happened with > python itself are: > >  (1) More users for whom this is their first language. >  (2) CS

Re: IDLE: A cornicopia of mediocrity and obfuscation.

2011-02-01 Thread Raymond Hettinger
On Jan 31, 9:39 am, rantingrick wrote: > IDLE: cornucopia ... > These are just the top of the list. The peak of a huge iceberg that > threatens to sink the community in the arms of chaos never to return. That being said, I've taught a lot of people Python using IDLE. It's a surprisingly producti

An amazing one-minute bit of fun at the interactive prompt

2011-02-20 Thread Raymond Hettinger
>>> e = 10.0 ** -7; n = 0; z = c = complex(-0.75, e) >>> while abs(z) < 2.0: n += 1 z = z * z + c >>> n * e 3.1415926 Compute π ± e by counting Mandlebrot set iterations :-) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: An amazing one-minute bit of fun at the interactive prompt

2011-02-20 Thread Raymond Hettinger
> Compute ð ± e by counting Mandlebrot set iterations :-) That should be: pi plus-or-minus e Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: An amazing one-minute bit of fun at the interactive prompt

2011-02-21 Thread Raymond Hettinger
On Feb 21, 12:08 am, Mark Dickinson wrote: > On Feb 20, 8:08 am, Raymond Hettinger wrote: > > > [...] > > >>> n * e > > > 3.1415926 > Very neat!  Is it supposed to be obvious why this gives an > approximation to pi?  If so, I'll think about it a

Re: Performance of list vs. set equality operations

2010-04-07 Thread Raymond Hettinger
[Gustavo Nare] > In other words: The more different elements two collections have, the > faster it is to compare them as sets. And as a consequence, the more > equivalent elements two collections have, the faster it is to compare > them as lists. > > Is this correct? If two collections are equal,

Re: Performance of list vs. set equality operations

2010-04-07 Thread Raymond Hettinger
[Raymond Hettinger] > > If the two collections have unequal sizes, then both ways immediately > > return unequal. [Steven D'Aprano] > Perhaps I'm misinterpreting what you are saying, but I can't confirm that > behaviour, at least not for subclasses of list: F

Re: Performance of list vs. set equality operations

2010-04-08 Thread Raymond Hettinger
[Steven D'Aprano] > So what happens in my example with a subclass that (falsely) reports a > different length even when the lists are the same? > > I can guess that perhaps Py_SIZE does not call the subclass __len__ > method, and therefore is not fooled by it lying. Is that the case? Yes. Py_SIZE

Re: Performance of list vs. set equality operations

2010-04-09 Thread Raymond Hettinger
> > I don't know if it's a good "fix" anyway.  If you subclass an internal > > type, you can certainly supply your own rich comparison methods, which > > would (IMO) put the CPU computation burden where it belongs if you > > decide to do something goofy like subclass a list and then override > > __

Re: Tough sorting problem: or, I'm confusing myself

2010-04-11 Thread Raymond Hettinger
On Apr 9, 8:03 am, david jensen wrote: > Hi all, > > I'm trying to find a good way of doing the following: > > Each n-tuple in combinations( range( 2 ** m ), n ) has a corresponding > value n-tuple (call them "scores" for clarity later). I'm currently > storing them in a dictionary, by doing: > >

Re: Tough sorting problem: or, I'm confusing myself

2010-04-14 Thread Raymond Hettinger
> I'm not sure a heap will help much, and at least to me, > doesn't improve readability. nlargest() should save quite a few comparisons and run much faster than sorted(). Not sure what the readability issue is. The phrase "nlargest(2, iterable)" does exactly what it says, finds the 2 largest ele

Re: Tough sorting problem: or, I'm confusing myself

2010-04-14 Thread Raymond Hettinger
> > Not sure what the readability issue is.  The phrase "nlargest(2, > > iterable)" does exactly what it says, finds the 2 largest elements > > from an iterable.  That makes the programmer's intent more clear than > > the slower, but semanticly equivalent form:  sorted(iterable)[:2]. > > I think yo

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Raymond Hettinger
On Apr 3, 3:30 am, Alain Ketterlin wrote: > Hi all, > > I've just spent a few hours debugging code similar to this: > > d = dict() > for r in [1,2,3]: >     d[r] = [r for r in [4,5,6]] > print d > > THe problem is that the "r" in d[r] somehow captures the value of the > "r" in the list comprehensi

Re: a.extend(b) better than a+=b ?

2010-04-22 Thread Raymond Hettinger
On Apr 22, 12:10 am, candide wrote: > Suppose a and b are lists. > > What is more efficient in order to extend the list a by appending all > the items in the list b ? > > I imagine a.extend(b)to be more efficient for only appendinding the > items from b while a+=b creates a copy of a before append

Re: Hi, friends. I wanna ask if there is a function which is able to take a list as argument and then return its top-k maximums?

2010-04-23 Thread Raymond Hettinger
On Apr 22, 10:49 am, John Nagle wrote: > Chris Rebert wrote: > > 2010/4/22 Jo Chan : > >> Hi,friends. > >>  I wanna ask if there is a function which is able to take a list as > >> argument > >> and then return its top-k maximums? > >> I only know about max which is poorly a top-1 maximum function

Re: Hi, friends. I wanna ask if there is a function which is able to take a list as argument and then return its top-k maximums?

2010-04-23 Thread Raymond Hettinger
On Apr 23, 1:24 am, Bryan wrote: > That is interesting. The above algorithm for nlargest is better, but > to use it for nsmallest requires a largest-on-top heap, which the > module does not bother to implement. FWIW, the pure python versions differ because they are both implemented in terms of th

Re: Deleting more than one element from a list

2010-04-25 Thread Raymond Hettinger
On Apr 21, 12:56 pm, candide wrote: > Is the del instruction able to remove _at the same_ time more than one > element from a list ? > > For instance, this seems to be correct : > >  >>> z=[45,12,96,33,66,'c',20,99] >  >>> del z[2], z[6],z[0] >  >>> z > [12, 33, 66, 'c', 20] >  >>> > > How

Re: str.count algorithm

2010-05-04 Thread Raymond Hettinger
On May 4, 12:12 pm, Hellnar wrote: > Hello, > I am trying to find what algorithm Python uses for the built-in > str.count function, if it has a name. Roughly the same as: sum(1 for c in s if c == tgt) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: List comprehension + lambdas - strange behaviour

2010-05-06 Thread Raymond Hettinger
On May 6, 9:34 pm, Artur Siekielski wrote: > Hello. > I found this strange behaviour of lambdas, closures and list > comprehensions: > > >>> funs = [lambda: x for x in range(5)] > >>> [f() for f in funs] > > [4, 4, 4, 4, 4] > > Of course I was expecting the list [0, 1, 2, 3, 4] as the result. The

Re: an element from a set

2010-05-17 Thread Raymond Hettinger
On May 14, 3:24 pm, gerardob wrote: > Hello, let S be a python set which is not empty > (http://docs.python.org/library/sets.html) > > i would like to obtain one element (anyone, it doesn't matter which one) and > assign it to a variable. > > How can i do this? x = next(iter(s)) or you can suppl

Re: function that counts...

2010-05-24 Thread Raymond Hettinger
On May 19, 12:58 pm, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: > > In [197]: def prttn(m, n): >      tot = 0 >      for i in range(n): >          s = str(i) >          sum = 0 >          for j in range(len(s)): >              sum += int(s[j]) >  

Re: why any( ) instead of firsttrue( ) ?

2010-06-08 Thread Raymond Hettinger
On Jun 8, 2:16 pm, danieldelay wrote: >    def firsttrue(iterable): >      for element in iterable: >          if element: >              return element >      return None > > This function "firsttrue( )" could probably be used anywhere "any( )" is > used, but with the ability to retrieve the firs

Re: Why defaultdict?

2010-07-01 Thread Raymond Hettinger
On Jul 1, 9:11 pm, Steven D'Aprano wrote: > I would like to better understand some of the design choices made in > collections.defaultdict. . . . > If callable is None, defaultdicts are > *exactly* equivalent to built-in dicts, so I wonder why the API wasn't > added on to dict rather than a separ

Re: Python -- floating point arithmetic

2010-07-07 Thread Raymond Hettinger
On Jul 7, 5:55 am, Mark Dickinson wrote: > On Jul 7, 1:05 pm, david mainzer wrote: > > > > > Dear Python-User, > > > today i create some slides about floating point arithmetic. I used an > > example from > > >http://docs.python.org/tutorial/floatingpoint.html > > > so i start the python shell on

Re: Why doesn't python's list append() method return the list itself?

2010-07-11 Thread Raymond Hettinger
On Jul 11, 8:59 am, dhruvbird wrote: > Why doesn't python's list append() method return the list itself? For > that matter, even the reverse() and sort() methods? Because Guido thinks that having those methods return None is the best way to communicate that the underlying object has been mutated

Re: Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

2010-07-24 Thread Raymond Hettinger
On Jul 24, 12:47 am, Lacrima wrote: > Hi! > > I have two super classes: > > class SuperClass1(object): >     def __init__(self, word): >         print word > > class SuperClass2(object): >     def __init__(self, word, word2): >         print word, word2 > > Also I have subclass of these classes: >

Re: Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

2010-07-25 Thread Raymond Hettinger
On Jul 24, 3:56 am, Lacrima wrote: > Thank you for your answer. You're welcome. > Some things are still not clear. Your example works great. But if I > remove "super(SuperClass1, self).__init__(**kwds)" from SuperClass1's > __init__, the example stops working. That is when I instantiate > SubCla

Re: Compare two nested dictionaries

2010-07-25 Thread Raymond Hettinger
[targetsmart] > > I am trying to compare two nested dictionaries, I want to know what is > > the exact difference between them. I tried this solution [Steven D'Aprano] > If you want to know the difference between two dictionaries, you have to > consider: > > (1) Keys that are in the first dict, b

Re: Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

2010-07-26 Thread Raymond Hettinger
[Ethan Furman] > Speaking of new-style classes only, don't they all end in object?  And > if the MRO is only known at run-time, how is one to know at code-time > whether your (new-style) class is at the end of the line? That is a bit of a PITA. One way of handling it is to design your diamond so

Re: Performance ordered dictionary vs normal dictionary

2010-07-29 Thread Raymond Hettinger
On Jul 28, 6:47 pm, Navkirat Singh wrote: > I was wondering what would be better to do some medium > to heavy book keeping in memory - Ordered Dictionary > or a plain simple Dictionary object?? The current implementation of OrderedDict is based on regular dictionaries, so it performance cannot be

Re: Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

2010-07-30 Thread Raymond Hettinger
On Jul 25, 5:30 pm, Gregory Ewing wrote: > Raymond Hettinger wrote: > > Every class > > in the MRO implementing the target method *must* call super() to give > > the next class in the MRO a chance to run. > > EXCEPT for the last one, which must NOT call super! > &

Re: shelf-like list?

2010-08-13 Thread Raymond Hettinger
On Aug 12, 1:37 pm, Thomas Jollans wrote: > On Tuesday 10 August 2010, it occurred to kj to exclaim: > > > I'm looking for a module that implements "persistent lists": objects > > that behave like lists except that all their elements are stored > > on disk.  IOW, the equivalent of "shelves", but f

Re: EXOR or symmetric difference for the Counter class

2010-08-14 Thread Raymond Hettinger
On Aug 12, 1:20 pm, Paddy wrote: > I find myself needing to calculate the difference between two Counters > or multisets or bags. > > I want those items that are unique to each bag. Tell us about your use cases. I'm curious how a program would ascribe semantic meaning to the result. The phrase

Re: random number generation

2010-08-16 Thread Raymond Hettinger
On Aug 16, 5:37 pm, Jah_Alarm wrote: > hi, > > I need to generate a binary array with a specified average proportion > of 1s (e.g. [1 0 0 0 > > 0 1 0 0] has this proportion = 25%). In Matlab I run something like > random(m,n) > between 0 and 1. I'm trying to use random.randint(0,2,size=[m,n]), but

Re: EXOR or symmetric difference for the Counter class

2010-08-16 Thread Raymond Hettinger
[Paddy] > Lets say you have two *sets* of integers representing two near-copies > of some system, then a measure of their difference could be calculated > as: > > len(X.symmetric_difference(Y)) / (len(X) + len(Y)) * 100 % > > If the two collections of integers are allowed duplicates then you > need

q.join() is probably the wrong method for you

2010-08-18 Thread Raymond Hettinger
The join() method is all about waiting for all the tasks to be done. If you don't care whether the tasks have actually finished, you can periodically poll the unfinished task count: stop = time() + timeout while q.unfinished_tasks and time() < stop: sleep(1) This loop will exist either

Re: Python Developer - HFT Trading firm - Chicago, IL

2010-08-21 Thread Raymond Hettinger
On Aug 21, 2:30 am, Lawrence D'Oliveiro wrote: > Wasn’t HFT an exacerbating factor in just about every major stockmarket > downturn since, oh, 1987? IMO, it was a mitigating factor. HFT firms provide liquidity and help price discovery. Investor sentiment is what drives rallys and crashes. Raymo

Re: [Python-Dev] Released: Python 2.6.6

2010-08-24 Thread Raymond Hettinger
On Aug 24, 2010, at 12:31 PM, Barry Warsaw wrote: > Hello fellow Pythoneers and Pythonistas, > > I'm very happy to announce the release of Python 2.6.6. Thanks Barry :-) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Making the case for repeat

2009-06-07 Thread Raymond Hettinger
[pataphor] > So here is my proposed suggestion for a once and for all reconciliation > of various functions in itertools that can not stand on their own and > keep a straight face. Interesting phraseology ;-) Enticing and yet fallacious in its presumption of known and accepted usability problems.

Re: itertools.intersect?

2009-06-15 Thread Raymond Hettinger
[David Wilson] > The problem is simple: given one or more ordered sequences, return > only the objects that appear in each sequence, without reading the > whole set into memory. This is basically an SQL many-many join. FWIW, this is equivalent to the Welfare Crook problem in David Gries book, The

Re: alternative to JoinableQueue's please

2009-06-26 Thread Raymond Hettinger
[Filipe Fernandes] > The reasons for using JoinableQueue I think are obvious.  I want to > block the main processing using queue.join() until the tasks that have > been placed on the queue have been finished by the worker processes. > > I can't be the only one experiencing this (besides Brian)... a

Re: No trees in the stdlib?

2009-06-26 Thread Raymond Hettinger
[Tom Reed] > Why no trees in the standard library, if not as a built in? The sqlite3 module is built on a binary-tree structure. It can be used with persistent data or kept in-memory. The gdbm module has similar flexibility (see the F mode). FWIW, there are some ASPN implementing various types of

Re: No trees in the stdlib?

2009-06-26 Thread Raymond Hettinger
[João Valverde] > What's lacking is an associative array that preserves ordering, doesn't > require a hash function and has fast insertions and deletions in > O(log(n)). FWIW, Py3.1 has an OrderedDict() that preserves insertion order. It has O(1) lookup, deletion, insertion, and popping; and O(n)

Re: finding most common elements between thousands of multiple arrays.

2009-07-08 Thread Raymond Hettinger
[Scott David Daniels] > def most_frequent(arr, N): >      '''Return the top N (freq, val) elements in arr''' >      counted = frequency(arr) # get an iterator for freq-val pairs >      heap = [] >      # First, just fill up the array with the first N distinct >      for i in range(N): >          tr

Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Raymond Hettinger
On Jul 22, 4:55 am, Duncan Booth wrote: > Steven D'Aprano wrote: > > But that's the wrong solution to the problem. The OP wants the largest > > (or smallest) item, which he expects to get by sorting, then grabbing > > the first element: > > > sorted(alist)[0] > > > That requires sorting the entir

Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Raymond Hettinger
On Jul 20, 9:27 am, Phillip B Oldham wrote: > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from

Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Raymond Hettinger
> > While nothing in the list/tuple code requires you to make that > > distinction, > > it is important because that philosophy pervades the language.  If you > > follow Guido's direction, you'll find that the various parts of the > > language fit together better.  Do otherwise and you'll be going

Re: Help understanding the decisions *behind* python? - immutable objects

2009-07-31 Thread Raymond Hettinger
On Jul 26, 11:24 am, John Nagle wrote: > A tuple is really a frozen list.  Arguably, frozen objects > should have been a general concept.  Conceptually, they're > simple - once "__init__" has run, there can be no more changes > to fields of the object. I would argue that freezing and thawing (mak

Re: heapq "key" arguments

2009-08-03 Thread Raymond Hettinger
[Duncan Booth] > The documentation doesn't say anything directly about stability, but the > implementation is actually stable. You can probably assume it must be at > least for nlargest and nsmallest otherwise the stated equivalence wouldn't > hold: > > e.g. nsmallest documentation says: > >      

Re: heapq "key" arguments

2009-08-03 Thread Raymond Hettinger
[Joshua Bronson]: > According tohttp://docs.python.org/library/heapq.html, Python 2.5 > added an optional "key" argument to heapq.nsmallest and > heapq.nlargest. I could never understand why they didn't also add a > "key" argument to the other relevant functions (heapify, heappush, > etc). The pro

Re: no-clobber dicts?

2009-08-04 Thread Raymond Hettinger
[kj] > The implication here is that .update() does not in turn use > .__setitem__(), which I find a bit surprising. It's never wise to make assumptions about this sort of thing. Every method in a class or type is allowed to directly access or modify its private, internal data. The implementation

Re: Subclassing Python's dict

2009-08-06 Thread Raymond Hettinger
> Are you referring to Python 3.0?  Python 2.6 does not have > collections.UserDict In Python2.6, it is in its own module. >>> from UserDict import UserDict Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Subclassing Python's dict

2009-08-06 Thread Raymond Hettinger
> Xavier Ho wrote: > > You should subclass collections.UserDict, and not the default dict class. > > Refer to the collections module. > > Xavier, why do you think that is the correct approach? The docs say > "The need for this class has been largely supplanted by the ability to > subclass directly

Re: Python docs disappointing - group effort to hire writers?

2009-08-11 Thread Raymond Hettinger
[Paul Rubin] > I think the Python tutorial is aimed at users who are newbies to > Python but not newbies to programming.  Writing a tutorial for total > newbies is a completely different problem, that would result in a much > different document that's less useful to the existing tutorial's > intend

Re: Social problems of Python doc [was Re: Python docs disappointing]

2009-08-12 Thread Raymond Hettinger
On Aug 12, 3:32 am, Paul Boddie wrote: > Maybe the problem is that although everyone welcomes contributions and > changes (or says that they do), the mechanisms remain largely beyond > criticism. FWIW, I support the idea the regular docs incorporating links to freely editable wiki pages. That wi

Re: Social problems of Python doc [was Re: Python docs disappointing]

2009-08-12 Thread Raymond Hettinger
[Xah Lee] > i've wrote several articles about this issue, total time spend on this > is probably more than 2 months full-time work. See: > > • Python Documentation Problems >  http://xahlee.org/perl-python/python_doc_index.html I just read you post. You did devote a substantial amount of time

Re: Social problems of Python doc [was Re: Python docs disappointing]

2009-08-12 Thread Raymond Hettinger
[Raymond Hettinger] > Here are a few thoughts on list.sort() for those who are interested: After one more reading of Xah Lee's posts on the documentation for sort, here are couple more thoughts: * The reason that list.sort() allows None for the cmp parameter is not so that you c

Re: implementing descriptors

2009-08-13 Thread Raymond Hettinger
[David] > I am new to Python and I have a question about descriptors.  If I have > a class as written below, is there a way to use descriptors to be > certain that the datetime in start is always before the one in end? > > class foo(object): >    def __init__(self,a = None,b = None) >       self.st

Re: implementing descriptors

2009-08-15 Thread Raymond Hettinger
> Raymond, >    This functionality is exactly what I was looking for. Thanks!  I'll > be using this to solve my problem. > >    Now that I'm on the right track, I'm still a bit confused about how > __get__ and __set__ are useful.  Admittedly, I don't need to > understand them to solve this problem,

<    2   3   4   5   6   7   8   >