all-tip. I
don't know why yours is different. Also, help(itertools.count) does
the right thing.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
to implement alternate
constructors. For good examples, see dict.fromkeys() or any of the
various datetime constructors.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On May 28, 4:41 pm, blaine <[EMAIL PROTECTED]> wrote:
> 1. Whats a good way to get the keys? I'm using a loop with
> random.choice() at the moment.
Why not keep a separate list of keys and use random.sample()?
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ines(f). Then, append a
"return locals()" to the end of the function and run it through exec.
3. Try hacking a tracing/debugging utility.
4. Run the sourcefile through tokenize, make the appropriate
insertion, and then untokenize.
. . .
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
. The == arises
because nlargest/nsmallest compare decorated tuples. Tuple comparison
always starts with equality tests to find the first unequal element
and then switches back to testing whichever inequality test was
requested.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
good for removing duplicates:
result = [k for k, g in groupby(imerge(*sources))]
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
have a precise
idea of what it means to merge them while preserving order. For
example if the inputs are XYZPDQ and bYlmPz, then what does a merged
sequence look like once the Y and P duplicates are removed? Is it
XZPDQblmz or some intermix of the two like XbZlPmDzQ? If it is the
first
dict subclass and should provide all of those methods.
Also, it is useful in its current form.
> Or would it be better to redefine how defaultdict.fromkeys works, so
> that it first creates the defaultdict, and then goes through the keys?
>
> All comments welcome. If I ge
s run during the lookup phase and creates your
default just-in-time for use:
>>> d = defaultdict(list)
>>> for k in 'zyzygy':
d[k].append(1) # empty list created on lookup if needed
>>> d
defaultdict(, {'y': [1, 1, 1], 'z': [1, 1], 'g': [1]})
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ssive and grows only in 1MB blocks and giving your
observed nasty quadratic behavior.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
7;ve just fixed this for Py2.5.3 and Py2.6. No more quadratic
behavior.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
location? (besides .replace() ;)
> >>> identity = "".join(map(chr, range(256)))
> >>> 'www.example.com'.translate(identity, 'cmowz.')
>
> 'exaple'
And in Py2.6, you'll be able to simplify further:
>>> 'abcde'.translate(None, 'bd')
'ace'
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
as list.sort() except for the step
where the input gets copied. For list inputs, that extra time is
trivial compared to the cost of actually doing the sort. I wouldn't
worry about the negligible performance difference. Use whichever fits
best in your program.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
s, subclassing is the intended way to produce variants of Template
with a different delimiter.
>>> import string
>>> class PercentTemplate(string.Template):
delimiter = "%"
>>> s = PercentTemplate('Move %piece to %position')
>&
n to use iteration instead of recursion
(the latter is *very* expensive in Python).
The fastest way is to write a C function or use Pyrex.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
Non-recursive, 8-bit block table lookup version:
def ham(a, b, ht=[hamdist(a,0) for a in range(256)]):
x = a ^ b
dist = 0
while x:
dist += ht[x & 255]
x >>= 8
return dist
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
lambda
packet: packet[5:1:-1]
Since function calls are expensive in python, you can also gain speed
by parsing multiple fields at a time:
header, timetag, counter = parse(packet)
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[kretik]
>> I've been trying to coax this class to use something other than the
> >> default '$' but it seems setting it to something else has no discernible
> >> effect. Is it necessary to inherit from the class to do this?
[raymond]
> > Yes, subcla
On Jul 3, 9:20 pm, "Dieter Maurer" <[EMAIL PROTECTED]> wrote:
> The apparent reason is that the free variables in
> nested generator definitions are not bound (to a value) at invocation
> time but only at access time.
That's what it is supposed to do. Welcome to
a winning trade-off. So, the optimize() step would need
to be optional, not builtin.
Raymond
FWIW, I mentioned all this at PyConDue but the message must have
gotten lost.
--
http://mail.python.org/mailman/listinfo/python-list
On Jul 12, 10:13 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
> On Jul 11, 3:01 pm, [EMAIL PROTECTED] wrote:
>
> > I have found this perfect hash (minimal too)
> > implementation:http://burtleburtle.net/bob/hash/perfect.html
>
> > I have already translated
] != n - 1:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1]
yield tuple(pool[i] for i in indices)
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
op notch Python programmers
who have programmed happily for years without ever using
__del__. There are ways to get it to work for you, but
it may be the smart thing to pretend that you've never
heard of it.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
mport pi, atan2, radians, degrees, cos, sin
for i in range(360):
t = radians(i)
x = cos(t)
y = sin(t)
a = degrees(atan2(y, x))
print i, t, x, y, a
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On Jul 19, 2:27 am, [EMAIL PROTECTED] wrote:
> Why is Perl so much better than python?
Because dollar signs are a superior form of punctuation.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ended queue
( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259179 )
and then replace the dict with a shelf. Presto, you've got a
persistent deque.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
s)
For linear performance, you can use itertools:
list(itertools.chain(*list_of_lists))
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
put array:
>>> s = [ 108, 58, 68 ]
>>> sorted(range(len(s)), key=s.__getitem__)
[1, 2, 0]
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
7;Duplicate:', record
elif sha1(record).digest() in possible_dups:
seen.add(record)
Raymond
P.S. If the log entries are one liners, maybe it would be better to
use the operating system's sort/uniq filters.
--
http://mail.python.org/mailman/listinfo/python-list
def linear(x):
return 3 * x - 6
lo, hi = 2, 10
r = make_user_distribution(linear, lo, hi)
for i in range(20):
print r()
--
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ort the files:
opt1.py
--
opts = { '-cc': '12',
'-I': r'/my/path/work/'}
opt2.py
--
opts = { '-I': r/my/path/work2/'}
main.py
---
from opts1 import opts as opt1
from opts2 import opts as opt2
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On Apr 8, 8:15 am, "Steven Clark" <[EMAIL PROTECTED]> wrote:
> If I have a list of items of mixed type, can I put something into it
> such that after a list.sort(), is guaranteed to be at the end of the
> list?
Since the other guys gave you the real answer, how about this:
sentinel = object()
myl
it stands.
If you want to tweak it a bit, you can avoid using a flag like
"finished" by using a break-statement.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
print sum(x - mean for x in data)/len(data)
See:
http://svn.python.org/view/sandbox/trunk/statistics/statistics.py?rev=40981&view=markup
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
2x faster for adding numbers only:
Try it with Py2.6 or Py3.0. The sum() function has been optimized and
should be at least 6x faster for summing ints and floats. It should
even beat psyco optimized sums!
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
lasses, not instances.
So the assignment should be: B.testattr = property(...)
Second, the property() call in you example only defines a getter
function (the first argument) and not a setter function (the second
argument) it defaults to a read-only property. That is why
b.testattr='tes
of property() that allows you to re-use the
same getter/setter code for multiple attributes:
http://code.activestate.com/recipes/205126/
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
first
argument is a class, not an instance.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
to know when the producers are
> finished? Yes, there are different approaches like sentinels, but the
> absence of a unified approach built into the library really does seem
> like a deficiency in the library.
See effbot's reply. The task_done and join methods were put there for
peed, then just time both
approaches. However, it's a good run of thumb that builtins are hard
to beat by simulating them in pure python (unless you're using Psyco
as an optimizer).
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
k properly, can anyone offer any advise?
The update() method is not quite right for your purposes.
But a simple generator expression will do the trick:
>>> dict((k, [v, dict1.get(k, 0)]) for k, v in dict2.items())
{456: [3, 3], 234: [5, 5], 123: [4, 3], 157: [2, 0], 567: [2, 0]}
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
propriate implementation of addition.
The time machine strikes again! Try using Py2.6.
The built-in sum() function is much smarter and faster.
It does in fact use C addition.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
it has O(n) memory consumption and O(m) running time. In contrast, the
other variants do more work than necessary by pulling the whole
sequence into memory or by re-summing all n items at every step,
resulting in O(m) memory consumption and O(m*n) running time.
This recipe gets my vote for
to me as odd gambling; retry how often?)
Since the app doesn't seem to care when the dump occurs, it might be
natural to put it in a while-loop that continuously retries until it
succeeds; however, you still run the risk that other threads may never
leave the object alone long enough to dump completely.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ge(n))).
Some of this is a matter of taste and a matter of what you're used to
using, so the answer to what is the "more intuitively-useful" varies
depending on who is answering the question.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
> I have 5 trials max as of now. The error was about once in 3 months in
> my case: that should solve the problem for the rest of the universe ...
> If not, there is another bug going on.
I sure hope your code isn't being used in mission critical apps like
air traffic control :=0
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
on to suspending
other threads, it has the side-effect of suspending control-break
checks. IOW, you won't be able to break out of the dump().
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ng things that could be
done in C by exploiting tp_traverse and by hacking Python's internal
object allocator.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
lowed by the target phrase. The first two are in a group and
the last is not included in the result group. The any-text section is
non-greedy:
>>> import re
>>> re.findall('(FOO.*?)(?=FOO|$)', s)
['FOO blah1a blah1b ', 'FOO blah2 ', 'FOO blah3a blah3b blah3b']
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
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
to the Queue module that makes it a
little bit easier to use a pool of worker threads and then be able to
tell when they are all done with processing:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475160
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[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
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
stay,
so it's time to get over it.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[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
[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
[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
> > * 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
[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
to a variable whose name is in another variable?"
> The answer is almost always "Don't do that, use a
> dictionary.")
The proposed syntax works better with class namespaces
which automatically provide inheritance logic and
method binding. Dictionaries don't provide equivalent
support.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[Joshua Bronson]
> Raymond, do you think there might be any future in including a built-
> in bidict data structure in Python?
I don't think so. There are several forces working against it:
* the recipe is new, so it hasn't had a chance to mature
or to gain a fan club.
oblems with APIs that do
not require the user to specify names for the two directions. It is
too easy to make mistakes that are hard to see. Which is correct,
phonelist['raymond'] or phonelist[raymonds_phonenumber]? There is no
way to tell without looking back at the bijection specificat
(i.e. another
table with foreign-key).
Too bad a DB wasn't used to solve a DB problem.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On Dec 4, 2:03 am, "Alf P. Steinbach" wrote:
> Is this guaranteed to work in Python 3.x?
>
> >>> def foo(): pass
> ...
> >>> foo.blah = 222
> >>> foo.blah
> 222
Yes, function attributes are guaranteed to be writable:
htt
re they become large enough to overpower the
signal in your data (e.g. the Lorentz equation or some other chaotic
sequence).
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On Dec 5, 12:56 pm, Mark Dickinson wrote:
> On Dec 5, 8:25 pm, Raymond Hettinger wrote:
>
> > On Dec 5, 7:37 am, Anton81 wrote:
>
> > > I'd like to do calculations with floats and at some point equality of
> > > two number will be checked.
> > >
e :-)
> [1]:http://gitorious.org/graphine/pages/GraphineForPythonistas
Do you have many users?
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
On Dec 5, 3:22 pm, geremy condra wrote:
> On Sat, Dec 5, 2009 at 4:39 PM, Raymond Hettinger wrote:
> > [geremy condra]
> >> I actually considered using dependencies as an example on the
> >> "graphine for pythonistas"[1] article, but decided to do the maz
On Dec 5, 11:42 pm, Tim Roberts wrote:
> Raymond Hettinger wrote:
>
> > if not round(x - y, 6): ...
>
> That's a dangerous suggestion. It only works if x and y happen to be
> roughly in the range of integers.
Right. Using abs(x-y) < eps is the way to go.
Raymo
und() was along the
lines of performing a quantize or snap-to-grid
operation after each step in the calculation.
That approach parallels the recommendation for how
to use the decimal module for fixed point calculations:
http://docs.python.org/library/decimal.html#decimal-faq
Raymond
--
http://mail
of an IndexableSkiplist is similar to a B+tree but the
implementation in pure python is much simpler.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
plementation
> dependent, of course). If the iterable is a list already, the list
> building is not needed.
Good analysis. That is exactly what is going on under the hood.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
it takes
to iterate the list in the first place:
s = SortedDict(items)
list(s) # O(n log n) to sort and O(n) to iterate
s[newkey] = newval
list(s) # close to O(n)
my two cents,
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
), maxlen=0)
>
> What is the advantage of using a collections.deque against, say, the
> following code?
>
> def consume(iterator, n):
> for _ in islice(iterator, n): pass
The deque version is faster.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
e reasons were part of Guido's rationale.
Tim Peters and I also participated in the conversation and did not
disagree.
So, collections.deque() was born and the problem stopped being an
issue.
Also, Daniel Stuzbach has published a blist implementation that offers
high performance insertions
while ((item = PyIter_Next(it)) != NULL) {
Py_DECREF(item);
}
Py_DECREF(it);
if (PyErr_Occurred())
return NULL;
Py_RETURN_NONE;
}
This code consumes an iterator to exhaustion.
It is short, sweet, and hard to beat
off.
class A:
def __init__(self):
. . .
my_dict = defaultdict(A) # not thread-safe.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
if test1(x,y,z):
return
frobnicate(x,y,z)
for x in X:
f(x)
Or you can write a little state machine with flags and a single loop
but that isn't very readable or satisfying.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
s to become a decent
C++ programmer. Python is distinct because it is not that
hard to learn.
So, if I were hiring, I would focus on general programming skills
and knowledge of the problem domain.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ctly) be recast as key
functions.
All that being said, for many everyday uses, a key function is
simpler to write and faster to run than a cmp function approach.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[Stefan Krah]
> today I have released the following packages for fast arbitrary precision
> decimal arithmetic:
Nice.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
with a cmp function but not a key function?
t = [[9,6],[7,1]],[[2,5],[4,3]] # inputs
t.sort(mycmp) # what would the cmp function be?
print t # what would the output be
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
the nodes within a single
> tree.
Can you give an example of a list of trees and a cmp function
that recursively compares them?
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ssive nodes to be compared.
>From the sound of it, the trees are static during the sort and
would get a nice O(n log n) --> O(n) speed-up if a key function
were allowed to flatten them in a single pass.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
f_lists[1]:
...
>> Are the trees user defined classes?
>
> Not in general. They might be nested tuples, lists, or dictionaries.
> Or they could come from a non-extensible library-defined class, like
> from cElementTree
Are there any of these trees that cannot be transformed
(by a key function) into an equivalent nested list of lists
and values so that the trees can be directly compared to one another
using Python's normal built-in recursive comparison order for lists?
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
be precomputed
in a key function (perhaps involving some complex interaction between
the two compared items like a tree merge or somesuch).
Instead of trying to create a hard comparison from first principles,
there may already be some research on the subject in the SQL world.
It seems to me that SQL
ot;here's how two objects should be
compared").
In contrast, some people who have have had deep experience with
cmp functions may tend to first think of cmp solutions and then
have a harder time seeing solutions with key functions. If you
grew-up on C's qsort() like I did, then a key function may not
be the first thing that pops into your head.
One other interesting data point: Python uses key functions
in min(), max(), heapq.nsmallest(), heapq.nlargest, and
itertools.groupby(). Those functions never supported a
cmp function argument, nor has one ever been requested.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
on (using other techniques) ;-)
Once Kernighan and Ritchie put C's qsort() into the food supply,
we were doomed.
FWIW, I think the standard library's unittest module is also a
code prion -- there's no other explanation for its thriving
despite competition from the vastly superior syntax of py.test
or nose.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ccount:
> print 'grouped acc: ', acc
>
> gives output:
>
> grouped acc: 61
> grouped acc: 64
> grouped acc: 61
>
> am I doing something wrong?
Try another variant of groupby() that doesn't require the data to be
sorted:
http://code.activestate.com/recipes/259173/
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
FWIW, the "additional memory requirements" are typically a set of
pointers to the objects being sorted, so the memory overhead is
typically very small relative to the size of the objects being sorted.
IOW, this isn't much of a consideration in most Python apps.
Raymond
--
http://ma
anks in advance :)
Take a look at http://code.activestate.com/recipes/496908/
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
well.
So, if you hear someone make-up a new "best practice" proscibing
"while True", just smile and continue to write programs as you
have been. You will not be alone. Many excellent programmers
write "while True" whenever it is called for.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
would be asymtotically O(n log n) but messy to implement.
>
> QuickSelect will find the median in O(log n) time.
Right. See http://code.activestate.com/recipes/269554/ for a sample
implementation in Python.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
ng about types and how to compose program components).
"Structured Programming with go to Statements" by Donald Knuth
has an in-depth comparative analysis of many different looping
constructs.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
he missing parts.
See http://code.activestate.com/recipes/576930/ for working Python
code
adapted from that paper.
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
:
'Allow forward or backwards iteration over a subslice'
for i in range(start, stop, step):
yield seq[i]
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
[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(sta
= 10:
primes.extend(values)
print primes[1000-1]
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
-up. Given invoices of
[10, 11, 12, 1000, 13, 14], the 1000 should be easy to match-up
in any grouping of payments. Likewise, when looking for groupings,
start with the most unique values. Given [2, 2, 2, 3, 3, 5, 5, 5,
6.1, 7],
start by trying to match the 6.1 since there is only one occurrence.
t calling it Raptor Graphics that will please everyone ;-)
Raymond
--
http://mail.python.org/mailman/listinfo/python-list
601 - 700 of 875 matches
Mail list logo