Re: Efficient processing of large nuumeric data file

2008-01-18 Thread bearophileHUGS
...and just for fun this D code is about 3.2 times faster than the Psyco version for the same dataset (30% lines with a space): import std.stdio, std.conv, std.string, std.stream; int[int] get_hist(string file_name) { int[int] hist; foreach(string line; new BufferedFile(file_name)) {

Re: pairs from a list

2008-01-22 Thread bearophileHUGS
Alan Isaac>What is the fastest way? (Ignore the import time.)< Maybe someday someone will realize such stuff belongs to the python STD lib... If you need a lazy generator without padding, that splits starting from the start, then this is the faster to me if n is close to 2: def xpartition(seq, n

Re: Duplicating a variable

2008-01-24 Thread bearophileHUGS
Hans: > I have run into a bit of a subtle problem. How do I go about > duplicating a variable (particularly a list of lists) in python. I > was surprised when simple assignment didn't work. Python is quite high-level language, but now and then it too accepts some compromises to increase its spee

Re: find minimum associated values

2008-01-25 Thread bearophileHUGS
I find the first and third solutions simpler to read, and the first solution requires less memory, it probably works quite well with Psyco, and it's easy to translate to other languages (that is important for programs you want to use for a lot of time or in different situations), so I'd use the fir

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
> Henry Baxter wrote: > > def maxi(l): > > m = max(l) > > for i, v in enumerate(l): > > if m == v: > > return i > > What's about l.index(max(l)) ? The version I use: def posmax(seq, key=None): """Return the position of the first maximum item of a sequence. It a

Re: Generational Interfaces

2008-01-26 Thread bearophileHUGS
I think they can be called "soft interfaces" or "heuristic interfaces", it's an example of machine learning. I think it's not easy to apply such idea to a statically typed language, but probably it can be done on Java. I presume in the future GUI will learn more about the usage patterns of their us

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
Paul Rubin: > def posmax(seq, key=lambda x:x): >return max(enumerate(seq), key=lambda k: key(k[1]))[0] Is the Python max able to tell that's the identity function? I don't think so, so your version may be slower and more memory hungry in the common situation where you don't have a key function

LEL

2008-01-26 Thread bearophileHUGS
The LEL mini language (there used for JRuby) seems nice, it may be added to Tkinter: http://ihate.rubyforge.org/profligacy/lel.html http://ihate.rubyforge.org/profligacy/sample.html Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
Steven D'Aprano: > Much to my surprise, the fastest solution I've tried appears to be a pure > Python version not even using max() at all. That version is easy to translate to other languages and you can probably find that Psyco makes it much faster still. Bye, bearophile -- http://mail.python.o

Re: Index of maximum element in list

2008-01-27 Thread bearophileHUGS
bearophile: > That version is easy to translate to other languages and you can > probably find that Psyco makes it much faster still. That operation is quite common, so it deserves a bit more work: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/543271 (I can show you the D/C code if you

Re: A question about osyco

2008-01-28 Thread bearophileHUGS
Marc 'BlackJack' Rintsch: > Try calling the iterative one twice and measure the time of the second > call. IIRC psyco needs at least one call to analyze the function, so the > first call is not speed up. That's how Java HotSpot works, but Psyco is very different from HotSpot, and I think you are

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-30 Thread bearophileHUGS
If you don't want to reinvent the wheel all the time you can use this one: def inplacefilter(pred, alist): """inplacefilter(pred, alist): filters the given list like filter(), but works inplace, minimizing the used memory. It returns None. >>> pr = lambda x: x > 2 >>> l = [] >

Re: comparing two lists, ndiff performance

2008-01-30 Thread bearophileHUGS
Zbigniew Braniecki: > Is there a way to speed it up? Any easier way? Faster method? This problem is a bit messy. Maybe it's better to sidestep the problem, and not use a list, and create an object that wraps the list, so it always keeps an updated record of what changes are done... but you have to

Re: dict comprehension

2008-02-01 Thread bearophileHUGS
Paul McGuire: > Why not just > D = dict(zip(keys,values)) > ?? Because this may require less memory: from itertools import izip D = dict(izip(keys, values)) :-) Bear hugs, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Pyrex bitten by the exception bug

2008-02-01 Thread bearophileHUGS
Paul Sijben: > Is there a fixc for this issue? At the moment I think it's better to drop Pyrex and to use Cython. Maybe you can try that, or ask to their developers. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

LuaJIT improves

2008-02-01 Thread bearophileHUGS
There are news: http://article.gmane.org/gmane.comp.lang.lua.general/44781 I think Psyco may deserve some updates, because it can be useful on Python 3.0 too, on 64bit, and on iterators (with itertools) too. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: dict comprehension

2008-02-02 Thread bearophileHUGS
Steven Bethard: > It also doesn't build the unnecessary intermediate tuples: I see, but can't the interpreter improved to remove similar intermediate tuples anyway? Is this a difficult thing to do? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: psyco question

2008-02-03 Thread bearophileHUGS
miller: > Is there any simple/easy/elegant way to retain a reference to the > *unoptimized* version of f so I can call them both and compare > performance? A simple solution is to defer the optimization. That is test the original code, call Psyco, then test it again: def somefunc(): ... from

Dynamic Lookup in C#

2008-02-04 Thread bearophileHUGS
It seems C# 4.0 may become a bit closer to dynamic languages, especially closer to the Boo language (that is mostly static): http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx As it develops, and its VM gains the method inlining capabilities of HotSpot, it will be faster. Static la

Re: IronPython vs CPython: faster in 1.6 times?

2008-02-05 Thread bearophileHUGS
Mike C. Fletcher: > Not sure if Mono also provides a speedup. There is a set of good benchmarks here, the answer is negative: http://shootout.alioth.debian.org/sandbox/benchmark.php?test=all&lang=iron Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: IronPython vs CPython: faster in 1.6 times?

2008-02-06 Thread bearophileHUGS
Stefan Behnel: > This doesn't look like Mono to me: > IronPython 1.1 (1.1) on .NET 2.0.50727.42 You are right! I think this shows that IronPython isn't faster than CPython at all :-) (And it uses more memory). Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Data Manipulation - Rows to Columns

2008-02-06 Thread bearophileHUGS
This is smells of homework. Here are few alternative solutions of mine that I don't like. I presume a good teacher will refuse them all, because no one of them uses the right tool :-) And every one of them has some small problem (even if they work here). data = """\ TABLE black blue red CHAIR yell

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

2008-02-07 Thread bearophileHUGS
Paul Rubin: > I like the suggestion, except it should be > port = int(sys.argv.get(1, '8000')) > one could imagine your example going wrong in a protocol where 0 is > a valid port number. I think a high-level language like Python must have boolean operators (or and not) that behave in a much

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

2008-02-07 Thread bearophileHUGS
Steven D'Aprano: > With the greatest respect, I think that if you think the second example > "is more clear", you're completely bonkers. *grins* No one is completely normal, I presume :-) I'd like to know what others think about it, about this anti-feature. What I can say is that other computer la

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

2008-02-08 Thread bearophileHUGS
Carl Banks: > I think booleans should be completely disjoint from all other types, > as they are in Java. "and", "or", and "not" should accept only boolean > operands and return only boolean results. I'd like "or" and "and" (and "not") to return booleans only, to avoid bugs and make their meanin

Re: which one is more efficient

2008-02-09 Thread bearophileHUGS
Michael Spencer: > # perhaps faster > $ python -mtimeit -s"t='D'" "if t in 'DE': pass" > 100 loops, best of 3: 0.204 usec per loop That may be faster indeed, but it gives true if t is "DE" too, and that may be bad. The new string search done by Effbot is really good :-) Bye, bearophile -- ht

Re: Pure Python Salsa20 Stream Cipher Implementation

2008-02-09 Thread bearophileHUGS
Gabriel Genellina: > On most platforms -with a notable exception- there is a C compiler > available as a standard tool, which is used to compile Python itself. For that platform the Python site may offer a version of binary Python bundled with the MingGW (and Cython too, maybe), like ShedSkin does

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

2008-02-09 Thread bearophileHUGS
Steven D'Aprano: > What you're trying to do is the equivalent of substring searching. There > are simple algorithms that do this, and there are fast algorithms, but > the simple ones aren't fast and the fast ones aren't simple. > However, for your use case, the simple algorithm is probably fast eno

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

2008-02-09 Thread bearophileHUGS
Steven D'Aprano: > I believe that it is considered an abuse of doctest to write a function > with 28 lines of code and 19 tests I agree with you. Most of my functions/methods have more compact doctests (putting things on many lines has the advantage of letting you locate the failing tests in a sim

Reducing types

2008-02-10 Thread bearophileHUGS
For me Python is useful to write code that gives *correct* results, allowing me to write it in a short & simple way, with quick debugging cycles (and for other purposes, like to write dynamic code, to use it as glue language to use libraries, to create quick GUIs, as scripting for data munging, etc

Re: Turn off ZeroDivisionError?

2008-02-10 Thread bearophileHUGS
Mark Dickinson: > This is probably the only sane way to deal with differences in > platform behaviour when doing float divisions. What Python run on a CPU that doesn't handle the nan correctly? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Reducing types

2008-02-10 Thread bearophileHUGS
Paul McGuire: > Sounds like you've been reading about Cobra... I have heard about that language (for dotnet?) but my idea doesn't come from Cobra... Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: packing greyscale values

2008-02-11 Thread bearophileHUGS
Diez B. Roggisch: > Then build up the mapping. Not elegant, but works. For the OP: note that a list suffices, no dict needed. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: which one is more efficient

2008-02-11 Thread bearophileHUGS
Steven D'Aprano: bearophile: > > The new string search done by Effbot is really good :-) > Care to explain what "new string search" you're referring to? Inquiring > minds want to know. I meant this good work you can find in Python 2.5: http://effbot.org/zone/stringlib.htm Bye, bearophile -- http

Re: Is there an easy way to sort a list by two criteria?

2008-02-11 Thread bearophileHUGS
[repost] Duncan Booth: > >>> from operator import itemgetter > >>> lst = [(1,2,4),(3,2,1),(2,2,2),(2,1,4),(2,4,1)] > >>> lst.sort(key=itemgetter(1)) > >>> lst.sort(key=itemgetter(2)) > >>> lst > [(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)] A little known thing from Python 2.5: >>> fro

Re: ANN: pyparsing 1.4.11 released

2008-02-11 Thread bearophileHUGS
Paul McGuire: > - Added '==' short-cut to see if a given string matches a > pyparsing expression. For instance, you can now write: > > integer = Word(nums) > if "123" == integer: ># do something > > print [ x for x in "123 234 asld".split() if x==integer ] > # prints ['12

Re: Combinatorics

2008-02-12 Thread bearophileHUGS
Michael Robertson: > I'm guessing sage has this, but shouldn't something like this be part of > the standard library (perhaps in C)? My answer is positive. As a reference point you can look at the combinatorics module of Mathematica. Bye, bearophile -- http://mail.python.org/mailman/listinfo/pyt

Re: ways to declare empty set variable

2008-02-12 Thread bearophileHUGS
Paul Rubin: > In 3.0 you may be able to say {,} but there is a contingent that would > just as soon get rid of all that special syntax, so you'd say list() > instead of [], dict() instead of {}, etc. For Python 3.0 I'd like {} for the empty set and {:} for the empty dict, but that idea was refused

Re: ways to declare empty set variable

2008-02-12 Thread bearophileHUGS
Ben Finney: > I often use these myself. They're slightly more explicit, which can > help when I want the reader not to have to think too much, and they're > not particularly verbose because the names are well-chosen and short. I'd like "list" be called "array" ;-) > Note that '()' is syntactical

Re: ways to declare empty set variable

2008-02-12 Thread bearophileHUGS
Ben Finney: > Generator literals do not require the > parens at all. However, the syntax of where the generator literal > *appears* can make it necessary to explicitly group the expression > using parens. Have you taken a look at Boo? In Python this isn't possible: s = char for char in string.digi

Re: IronPython vs CPython: faster in 1.6 times?

2008-02-12 Thread bearophileHUGS
Fuzzyman: > Another interesting little benchmark of CPython and IronPython. Can't > see the code, but it looks like an implementation of the 11 queens > problem and IronPython comes out a clear winner on this one. (Looks > like no benchmark for psyco.) If you want a more reliable set of benchmarks

Re: Combinatorics

2008-02-12 Thread bearophileHUGS
Cameron Laird: > It does occur to me, though, that even more widely applicable > than the combinatorics module of Mathematica (if only because of > its licensing) might be such resources as What I was trying to say is that that Mathematica combinatorics module contains lots and lots and lots of th

Re: regex question

2008-02-13 Thread bearophileHUGS
mathieu, stop writing complex REs like obfuscated toys, use the re.VERBOSE flag and split that RE into several commented and *indented* lines (indented just like Python code), the indentation level has to be used to denote nesting. With that you may be able to solve the problem by yourself. If not,

Re: Big time WTF with generators - bug?

2008-02-13 Thread bearophileHUGS
Paul Rubin: > Even with that though, at least for me, Python starts feeling really > scary when the iterators get this complicated. I start wishing for > a static type system, re-usable iterators, etc. This is an interesting topic. I agree with you, I too was scared in a similar situation. The la

Re: Regular Expression for Prime Numbers (or How I came to fail at them, and love the bomb)

2008-02-14 Thread bearophileHUGS
cokofree: > Sadly that is pretty slow though... It's quadratic, and it's not even short, you can do (quadratic still): print [x for x in range(2, 100) if all(x%i for i in range(2, x))] In D you can write similar code. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Easy PIL question

2008-02-16 Thread bearophileHUGS
Gary Herron: > Try image.getpixel((x,y)) to retrieve the pixel at (x,y). If the OP needs to access many pixels, then he can use the load() method on the image object, and then read/write pixels (tuples of 3 ints) using getitem [] import Image im = Image img = im.load() img[x,y] = ... ... = im

Re: is this data structure build-in or I'll have to write my own class?

2008-02-20 Thread bearophileHUGS
subeen: > I think you should go for 'dictionary' that is a built-in data > structure of Python. The OP asks this too: > maintain the order (for iter and print) So I think an "ordered dict" is fitter, the op can search for an odict implementation, in the cookbook too (I too have written one, but i

PyEuler

2008-02-25 Thread bearophileHUGS
This is the first time I write something that looks like a little rant :-) Surely I'll be wrong in many points, but I presume this isn't going to damage people, so... Here there are some functional Python solutions to some Euler problems: http://pyeuler.wikidot.com/ Some pieces of that code: def

Re: PyEuler

2008-02-25 Thread bearophileHUGS
Paul Rubin: > def ggenfib(): > a,b = 1,2 > while True: > yield a > a,b = b, a=b Your version is the nice basic generator version (the last line contains a +, I presume), but I like this other version too: def xfibonacci(): a = b = 1 yield a yield b

Re: PyEuler

2008-02-25 Thread bearophileHUGS
Arnaud Delobelle: > In this case why not: > > def xfib(a=1, b=1): > while True: > yield a > a += b > yield b > b += a That's a bit less easy to understand than my version, for me. In my version is easy to see the values of the variables. And using a longer funct

Re: PyEuler

2008-02-25 Thread bearophileHUGS
Arnaud Delobelle: > I disagree; the generator function has no right to keep a and b to > itself. a and b cry out to be function parameters. I see. Then that's a generator for a generalized Fibonacci sequence :-) Your version too is useful. Bye, bearophile -- http://mail.python.org/mailman/listi

Indentation and optional delimiters

2008-02-26 Thread bearophileHUGS
This is the best praise of semantic indentation I have read so far, by Chris Okasaki: http://okasaki.blogspot.com/2008/02/in-praise-of-mandatory-indentation-for.html A quotation: >Imagine my surprise when I started teaching this language and found the >students picking it up faster than any langu

dict.get and str.xsplit

2008-02-26 Thread bearophileHUGS
When I use dictionaries I find the dict.get() method very handy, and I'd like to use it often, but I think it's quite slow. This is a small benchmark: from random import randrange from timeit import default_timer as clock def main(N, M): keys = list(set(randrange(2*N) for n in xrange(N)))

Re: dict.get and str.xsplit

2008-02-26 Thread bearophileHUGS
Marc 'BlackJack' Rintsch: > I guess it's the method lookup that's the slow part. Factor it out of the > loop and measure again:: I did know of that optimization, but sometimes I "forget" about it... The new timings: Output timings, best of 3, unloaded CPU: 2.32 s with adict.get 1.56 s with "in"

Re: Indentation and optional delimiters

2008-02-26 Thread bearophileHUGS
[EMAIL PROTECTED]: > Why not b = copyonwrite( a )? > Subclass the interpreter-- make your own session. Your idea may work, but I am talking about a new language (with some small differences, not a revolution). Making such language efficient enough may require to add some complex tricks, copy-on-wr

Re: Web site for comparing languages syntax

2008-02-26 Thread bearophileHUGS
There is this site too, I have written many small programs for it: http://www.codecodex.com/wiki/index.php?title=Main_Page Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.get and str.xsplit

2008-02-26 Thread bearophileHUGS
[EMAIL PROTECTED]: > As for the original prooblem, why not use > defaultdict? I think it's the most idiomatic way to achieve what we > want. And also the fastest one, according to my quick-and-dirty tests: It adds the new keys, I can't accept that: >>> from collections import defaultdict as dd >>

Re: Indentation and optional delimiters

2008-02-26 Thread bearophileHUGS
[EMAIL PROTECTED]: > It's Unpythonic to compile a machine instruction out of a script. But > maybe in the right situations, with the right constraints on a > function, certain chunks could be native, almost like a mini- > compilation. How much machine instruction do you want to support? This lan

Re: Indentation and optional delimiters

2008-02-26 Thread bearophileHUGS
Steven D'Aprano: > Usability for beginners is a good thing, but not at the expense of > teaching them the right way to do things. Insisting on explicit requests > before copying data is a *good* thing. If it's a gotcha for newbies, > that's just a sign that newbies don't know the Right Way from the

Re: Indentation and optional delimiters

2008-02-28 Thread bearophileHUGS
Steven D'Aprano: >the readability of your posts will increase a LOT if you break it up into >paragraphs,< You are right, I'll try to do it (when I go in flux state I write quickly, but I tend to produce long paragraphs). >The thing is, make-another-copy and make-another-reference are semantica

for-else

2008-03-04 Thread bearophileHUGS
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other languages (and you always have to translate long-lived code). - I have used it only once, so far. So so far

Re: for-else

2008-03-04 Thread bearophileHUGS
Raymond HettInger: > FWIW, I'm very happy with for-else. Most of the time, you don't need > it, but when you do, it beats the heck out of doing silly tricks with > flags. I'd like it to be renamed to something more natural :-) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-li

Re: sympy: what's wrong with this picture?

2008-03-04 Thread bearophileHUGS
apatheticagnostic: > I swear, this is one of the most polite-oriented groups I've ever > seen. > Not that that's a bad thing or anything, it's nice to be nice. Yep, and with lot more work it may even become a bit fit for women/ females too. Bye, bearophile -- http://mail.python.org/mailman/listi

Re: for-else

2008-03-05 Thread bearophileHUGS
On Mar 5, 10:44 pm, "Troels Thomsen" wrote: > > The primary use case is searching a container: > > > prep_tasks() > > for item in container: > > if predicate(item): > > found_tasks() > > break > > else: > > not_found_tasks() > > follow_up_tasks > > I've foun

Re: for-else

2008-03-05 Thread bearophileHUGS
Troels Thomsen: > The discussion of words is silly. My surprise about "else following a for > loop what the heck " lasted excactly as long as it takes to read > this sentence. Maybe I don't follow what you are saying, but well chosen words are a very important part of a well designed API.

islice ==> [::]

2008-03-07 Thread bearophileHUGS
I find itertools.islice() useful, so for Python 3.x I may like to see it removed from the itertools module, and the normal slicing syntax [::] extended to work with generators/iterators too. from itertools import islice primes = (x for x in xrange(1,999) if all(x % y for y in xrange(2, x))) print

Re: islice ==> [::]

2008-03-08 Thread bearophileHUGS
George Sakkis: > I had posted some time ago an OO wrapper of itertools; slice notation > for islice was one > motivation:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498272 I think I did see that, but later I forgot of it. It looks nice. Thank you, bearophile -- http://mail.python.org

Re: Advantage of the array module over lists?

2008-03-16 Thread bearophileHUGS
Their efficiency is mostly regarding the space. I think they aren't much speed-efficient because they require many conversions from-to Python types. You can gain speed efficiency too (sometimes a LOT), in some situations, using array with Psyco. Another advantage of arrays (better called "vector"s,

Types, Cython, program readability

2008-03-16 Thread bearophileHUGS
It seems the development of Cython is going very well, quite differently from the dead-looking Pyrex. Hopefully Cython will become more user-friendly too (Pyrex is far from being user-friendly for Windows users, it doesn't even contain a compiler, I think. The ShedSkin Windows installer contains an

Re: Types, Cython, program readability

2008-03-16 Thread bearophileHUGS
Tim Golden: > I'm not entirely sure why you think Pyrex should "contain a compiler". I think lot of Win users (computational biologists?), even people that know how to write good Python code, don't even know how to install a C compiler. >I'm fairly sure it's fine with MingW< (In the past?) I th

Py in Counter Strike

2008-03-17 Thread bearophileHUGS
A PDF file, Embedding_Python_into_Counter-Strike: http://us.pycon.org/common/2008/talkdata/PyCon2008/020/Case_Study_-_Embedding_Python_into_Counter-Strike_Source.pdf It talks about some problems too. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Removal of tkinter from python 3.0? [was: Fate of the repr module in Py3.0]

2008-03-20 Thread bearophileHUGS
Paul Rubin: > Python needs more stuff in its stdlib, not less. > If Tkinter doesn't satisfy, then add Gtk (or whatever) to the standard > distro. If that happens (i.e. some new toolkit is brought in and > declared to be the standard) then it might be ok to drop Tkinter but > it certainly shouldn't

Re: removing all instances of a certain value from a list

2008-03-21 Thread bearophileHUGS
[EMAIL PROTECTED]: > >>> a=filter ( lambda b: b != None, a) With None it's better to use is/not is instead of ==/!= Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: List question

2008-03-21 Thread bearophileHUGS
Ricky Zhou: > Look at the this line: > if 'one' and 'two' in f: Very cute, it's the first time I see a bug like this. I think it's not a common enough pattern to justify a language change, but a bit smarter computer language may be able to do that too ;-) (it's not easy to tell the two meaning

Re: List question

2008-03-22 Thread bearophileHUGS
bearophile: > A more computer-friendly (and Pythonic) syntax may be ('are' is a keyword): Sorry for causing confusion, I was just thinking aloud. English isn't my first language, and sometimes I slip a bit. Replace that with: > A more computer-friendly (and Pythonic) syntax may be ('are' is meant

Re: Duplicating list of lists [newbie]

2008-03-23 Thread bearophileHUGS
Yatsek: > Is there simple way to copy a into b (like a[:]) with all copies of > all objects going as deep as possible? If you only want to copy up to level-2, then you can do something like: cloned = [subl[:] for subl in somelist] Or sometimes safer: cloned = [list(subl) for subl in somelist] If

Re: On copying arrays

2008-03-23 Thread bearophileHUGS
ernesto: best = list(test) may be faster than: best = [x for x in test] Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

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

2008-03-24 Thread bearophileHUGS
Paul Rubin>It's at least pretty good. It's not ideal, but nothing ever is.< I agree. At the moment Python is among the best languages for that, but it's not perfect for that purpose. I think newbies may need a bit more structured language, where you have to put things in a certain order to have a

Re: Filtering a Python list to uniques

2008-03-25 Thread bearophileHUGS
Kelly Greer: > What is the best way to filter a Python list to its unique members? If Python is "batteries included", then an industrial-strength unique() seems one of the most requested 'batteries' that's not included :-) I feel that it's coming in Python 2.6/3.x. In the meantime: http://aspn.act

Re: memory allocation for Python list

2008-03-26 Thread bearophileHUGS
dmitrey: > As I have mentioned, I don't know final length of the list, but > usually I know a good approximation, for example 400. There is no reserve()-like method, but this is a fast enough operation you can do at the beginning: l = [None] * 400 It may speed up your code, but the final resizin

Re: Line segments, overlap, and bits

2008-03-26 Thread bearophileHUGS
Sean Davis>Java has a BitSet class that keeps this kind of thing pretty clean and high-level, but I haven't seen anything like it for python.< If you look around you can usually find Python code able to do most of the things you want, like (you can modify this code to add the boolean operations):

Re: memory allocation for Python list

2008-03-26 Thread bearophileHUGS
dmitrey: > As I have mentioned, I don't know final length of the list, but > usually I know a good approximation, for example 400. You may want to use collections.deque too, it doesn't produce a Python list, but it's quite fast in appending (it's a linked list of small arrays). Bye, bearophile --

Re: Why does python behave so? (removing list items)

2008-03-26 Thread bearophileHUGS
Michał Bentkowski: > Why does python create a reference here, not just copy the variable? I think to increase performance, in memory used and running time (and to have a very uniform way of managing objects). Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Tkinter menus made easy

2008-03-27 Thread bearophileHUGS
On Mar 27, 3:54 pm, [EMAIL PROTECTED] wrote: > Writing Tkinter menu code used to be rather tedious, uninspiring work. > I figured that I could delegate the job to a program: I did develop a proggy that takes the following as input, it's part of my "agui" project, you can use it as an idea to impro

Re: Psyco alternative

2008-03-27 Thread bearophileHUGS
Diez B. Roggisch: > the author says that the approach is flawed, so at *some* > point it will be discontinued. Can't Psyco be improved, so it can compile things like: nums = (i for i in xrange(20) if i % 2) print sum(nums) I think the current Psyco runs slower than Python with generators/ it

Re: Tkinter menus made easy

2008-03-27 Thread bearophileHUGS
[EMAIL PROTECTED]: > Mine does less. But you tell it less to do it. Some of those fields are optional :-) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: problem with sorting

2008-03-28 Thread bearophileHUGS
Duncan Booth: > Both this and raj's suggestion create a single sorted list. Your suggestion > creates two lists: the unsorted one and a separate sorted one. In most > cases the difference is probably insignificant, but if you have a *lot* of > values it might make a difference. The good thing of P

Re: if-else statement

2009-01-10 Thread bearophileHUGS
Scott David Daniels: >       if checking: >           my_var = 'string' >       else: >           my_var = 'other string' > > remember, vertical space only kills trees if printed. I value clarity a lot. But this is more DRY, sometimes it's almost equally clear, and you reduce vertical space, packi

Re: Alphametric fun with Python

2009-01-15 Thread bearophileHUGS
Raymond Hettinger: > Thought you guys might enjoy this: >    http://code.activestate.com/recipes/576615/ Nice and short, but it's also very slow on my PC (Psyco may help). This solves them in moments: http://labix.org/python-constraint Bye, bearophile -- http://mail.python.org/mailman/listinfo/p

Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread bearophileHUGS
mario ruggier, that's a hack that you can forget. Your code can't be read. Don't use list comps for that purpose. Write code that can be read. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Alphametric fun with Python

2009-01-15 Thread bearophileHUGS
Raymond Hettinger: > for simple programs that take minutes to write and get the job done. For fun here's a specific example: from csp import Problem, timing print "SEND+MORE=MONEY problem:" p = Problem("recursivebacktracking") p.addvars("sendmory", range(10)) p.addrule(lambda d,e,y: (d+e)%10 == y

Re: optimizing large dictionaries

2009-01-15 Thread bearophileHUGS
Matimus, your suggestions are all good. Try-except is slower than: if x in adict: ... else: ... A defaultdict is generally faster (there are some conditions when it's not faster, but they aren't much common. I think it's when the ratio of duplicates is really low), creating just a tuple instead of

Re: Alphametric fun with Python

2009-01-16 Thread bearophileHUGS
Terry Reedy: >It illustrates well the point you intended. I don't know what my point was. A suggestion: with that solver, to find solution in a faster way you have to write many equations. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
r: >Of course it would not run in C or Python but the point here is readability.< With languages like Genie you can go close: http://live.gnome.org/Genie Or better Delight (based on the D language), plus some my "Python compatibility" libs (plus Pyd, if you want) you can run that code: http://del

Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
alex23: > Paul, have you looked into Cython at all? I can also suggest ShedSkin and the D language. I have often used D for data munging, producing quick & short programs, when Python isn't fast enough for a certain purpose. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
Michele Simionato: > I don't like that. Scala was designed with the idea of putting > together the two worlds, by I think the result was to get the > complications of both worlds. But some other people may like it, and it's a design experiment worth doing. Every programming paradigm has advantage

Re: Simple image manipulation question

2009-01-19 Thread bearophileHUGS
AlienBaby: > PIL looked promising, but I couldn't see how to put text into the > image once loaded. A 15-second long Google search gives me: http://nedbatchelder.com/blog/200801/truly_transparent_text_with_pil.html Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: list subsetting

2009-01-21 Thread bearophileHUGS
FogleBird: > a.count(3.14) If the values to count are approximated FP values, then you may need something more complex, like: leniter(ifilter(somefunction, a)) Where somefunction uses an approximated comparison, and leniter is just a function that counts the items of a generic iterator. Bye, be

A different kind of interface

2009-01-22 Thread bearophileHUGS
I use the Python shell daily, plus of course normal editors to edit python scripts. They both are very useful for different purposes. But the default interactive shell isn't much handy if you want to modify the past code to run it again, or you want to embed a bit of text in the code, or if you wan

Re: A different kind of interface

2009-01-22 Thread bearophileHUGS
Ben Finney: > Adding an editor to Python solves this problem only for Python. I'm sure that once such "editor" (I use the word editor for lack of a better term) is created, it can also be quickly adapted with other dynamic languages, like Ruby, TCL, Lua, Io, Perl, Awk, ecc. Probably it can't be a

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