set, dict and other structures

2005-01-31 Thread bearophileHUGS
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less memory, not storing values? Maybe this requires too much code rewriting). I presume such sets are like

Re: set, dict and other structures

2005-01-31 Thread bearophileHUGS
You are really gentle Raymond Hettinger, but surely I'm not asking you/anyone to write some code just for me; I don't have real applications, I'm just learning/playing. Your words are quite good answers to most of my questions. The only left small topic is about the group/set-like

Re: set, dict and other structures

2005-02-02 Thread bearophileHUGS
Leif K-Brooks: They look exactly the same speed-wise to me: There's a little overhead, you can see it with a bigger test: .from time import clock .n = 1*10**6 . .t1 = clock() .d = set() .for i in xrange(n): . d.add(i) .t2 = clock() .for i in xrange(n): . d.remove(i) .t3 = clock() .d =

Re: set, dict and other structures

2005-02-02 Thread bearophileHUGS
r = {} for x in a: if x not in b: r[x] = r[a] for x in b: if x not in a: r[x] = r[b] I know, this is wrong :-] This looks better: r = {} for x in a: if x not in b: r[x] = a[x] for x in b: if x not in a: r[x] = b[x] Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: graph visualisation

2005-02-08 Thread bearophileHUGS
This isn't a visualisation routine, but it contains some of them, and with few mod you can adapt them for your needs: http://gato.sourceforge.net/ Gato - the Graph Animation Toolbox - is a software which visualizes algorithms on graphs. Bearophile --

Re: lambda and for that matter goto not forgetting sugar

2005-02-11 Thread bearophileHUGS
Nick Coghlan wrote: Anyway, check out AlternateLambdaSyntax on the python.org Wiki It's an interesting page: http://www.python.org/moin/AlternateLambdaSyntax Some days ago I suggested something different: maybe def can become a function, then it can work as lambda (and still as the old def) too.

Re: builtin functions for and and or?

2005-02-13 Thread bearophileHUGS
In Python there are so many ways to do things... This looks like another one, I haven't tested it: not False in imap(pred, iterable) As usual tests are required to measure the faster one. I agree with Roose, there are are some primitive operations (like this, and flatten, partition, mass removal

Syntax across languages

2005-10-23 Thread bearophileHUGS
This post comes from a boring morning, if you are busy ignore this. This post is only for relaxed people. I've found this page, Syntax Across Languages, it contains many errors and omissions, but it's interesting. http://merd.sourceforge.net/pixel/language-study/syntax-across-languages.html

Re: Syntax across languages

2005-10-23 Thread bearophileHUGS
Thank you for the comments, Fredrik Lundh. (that's (mostly) CPython-dependent, and should be avoided) Then a non CPython-dependent way of doing it can be even more useful. sure looks like four possible outcomes. Right (but to me four explicit answers seem better than three answers and an

Re: Syntax across languages

2005-10-23 Thread bearophileHUGS
Thank you Fredrik Lundh for showing everybody that indeed lot of people feel the need of such function in Python too. to create a generic version, you have to decide which sequences to treat like sequences In my version I give the function some parameter(s) to define what I want to flatten.

Re: System tray Icon

2005-10-23 Thread bearophileHUGS
Questo non e' sufficiente, ma puo' essere un punto di partenza: http://www.pycode.com/modules/?id=2tab=download Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Syntax across languages

2005-10-23 Thread bearophileHUGS
Thank you for all the answers, some people have already answered for me about most details I don't agree :-) Mike MeyerRexx has a global control that lets you set the number of digits to be considered significant in doing an FP equality test. Mathematica too, I think. Tom AndersonThere are all

MSH (shell)

2005-10-27 Thread bearophileHUGS
This can be of little interest of some people here, but I think it can be interesting enough to justify a post. An article on the Microsoft Command Shell: http://arstechnica.com/guides/other/msh.ars (Its syntax is rather Python-like, but Py syntax seems better to me, even taking into account that

Re: how to discard a line if it's not a number?

2005-10-29 Thread bearophileHUGS
This is a possible solution, using exceptions: fileName = data out = file(fileName + _filt.txt, w) for line in file(fileName + .txt): try: nline = float(line) except ValueError: pass else: out.write(str(nline) + \n) out.close() If the file is small enough this

Re: How do I sort these?

2005-10-30 Thread bearophileHUGS
# This can be a solution: from operator import itemgetter seq1a = ([2] * 4) + ([1] * 4) seq2a = [complex(3-el,7-el) for el in range(1, 9)] assert len(seq1a) == len(seq2a) print seq1a, seq2a, \n mix = zip(seq1a, seq2a) # mix.sort() # Not possible mix.sort(key=itemgetter(0)) # If you need tuples

Re: Python's website does a great disservice to the language

2005-11-01 Thread bearophileHUGS
Grant Edwards wrote: May God save us from professional looking web sites. I like the Python web site. It's simple, easy to read, and easy to use. I strongly agree with you, the web is full of web sites that are nice looking but have microscopic fixed fonts (against the very spirit of Html),

Re: Python's website does a great disservice to the language

2005-11-01 Thread bearophileHUGS
CppNewBand maybe a readable default font is in order. That font looks fine to me, maybe there's a problem in the default font of your browser, you can probably fix your problem... Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Burrows-Wheeler (BWT) Algorithm in Python

2005-11-03 Thread bearophileHUGS
Michael J. Fromberger: I can send you a Python implementation I wrote, if you like; but if you're interested in better understanding how the transform works, I would recommend you try writing your own implementation. I'd like to see it, if you want you can put it somewhere or send it

Re: help make it faster please

2005-11-10 Thread bearophileHUGS
This can be faster, it avoids doing the same things more times: from string import maketrans, ascii_lowercase, ascii_uppercase def create_words(afile): stripper = '[,;{}_?!():[]\.=+-*\t\n\r^%0123456789/ mapper = maketrans(stripper + ascii_uppercase, *len(stripper)

Re: help make it faster please

2005-11-12 Thread bearophileHUGS
Thank you Bengt Richter and Sybren Stuvel for your comments, my little procedure can be improved a bit in many ways, it was just a first quickly written version (but it can be enough for a basic usage). Bengt Richter: good way to prepare for split Maybe there is a better way, that is putting in

Re: Iterator addition

2005-11-12 Thread bearophileHUGS
Tom Anderson: And we're halfway to looking like perl already! Perhaps a more pythonic thing would be to define a then operator: all_lines = file1 then file2 then file3 Or a chain one: all_lines = file1 chain file2 chain file3 Bearophile --

D foreach

2005-11-13 Thread bearophileHUGS
The Digital Mars D compiler is a kind of improved c++, it contains a foreach statement: http://www.digitalmars.com/d/statement.html#foreach Usage example: foreach(int i, inout int p; v1) p = i; Is equal to Python: for i in xrange(len(v)): v[i] = i That is: v1 = range(len(v1)) (Some people use

Re: Underscores in Python numbers

2005-11-19 Thread bearophileHUGS
Steven D'Aprano: Perhaps Python should concatenate numeric literals at compile time: 123 456 is the same as 123456. I think using the underscore it is more explicit: n = 123_456 Alternatively the underscore syntax may be used to separate the number from its base: 22875 == 22875_10 == 595b_16

Re: Underscores in Python numbers

2005-11-19 Thread bearophileHUGS
Roy SmithWe already have a perfectly good syntax for entering octal and hex integers, There is this syntax: 1536 == int(600, 16) that accepts strings only, up to a base of 36. There are the hex() and oct() functions. There is the %x and %o sintax, that isn't easy to remember. There are the 0x600

Re: Underscores in Python numbers

2005-11-20 Thread bearophileHUGS
Peter HansenOr maybe one should instead interpret this as numeric literals need more bells and whistles, and I don't care which of these two we add, but we have to do *something*!. :-) The purpose of my words was: when you think about adding a new syntax/functionality to a language, you have to

Mixed types and variants

2005-11-23 Thread bearophileHUGS
Notes: - This email is about Mark Dufour's Shed Skin (SS) (http://shed-skin.blogspot.com), but the errors/ingenuousness it contains are mine. My experience with C++ is limited still. - The following code comes from a discussion with Mark. One of the purposes of SS is to produce fast-running

Re: Computer Language Shootout

2005-11-29 Thread bearophileHUGS
This is a direct translation of the D code, maybe it's not the faster Python implementation, and surely it's not the shorter one. But Psyco makes it much faster (Psyco likes low level style code). ShedSkink is (almost) able to compile it too, producing a really fast executable (with some smart

Re: Computer Language Shootout

2005-11-30 Thread bearophileHUGS
malv: Hi bearophileH, bearophile is enough :-) Could you post some more information about ShedSkink? ShedSkin (SS) is a Python - C++ compiler (or translator) written in Python, created by Mark Dufour. Its development was initially financed by the summer of code by Google. It contains some

Recursive structures

2004-12-20 Thread bearophileHUGS
(This is a repost from another python newsgroup). While using some nested data structures, I've seen that I'd like to have a function that tells me if a given data structure contains one or more cyclic references (a way to recognise a cycle in a graph is to do a depth-first search, marking

Re: Recursive structures

2004-12-20 Thread bearophileHUGS
Leif K-Brooks: http://python.org/doc/current/lib/module-pprint.html#l2h-749 Thank you very much, I see that the function is already there, even with the same name :-) I've seen that it doesn't work for all my tests, like this one with n = 3000: from pprint import isrecursive from time import

Optional Static Typing

2004-12-23 Thread bearophileHUGS
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo (http://boo.codehaus.org/) is a different language, but I like its as instead of : and -, to have: def min(a as

Re: Optional Static Typing

2004-12-23 Thread bearophileHUGS
Doug Holton: And there are some disadvantages to doing it this way. It means Python is more flexible to use than Boo, I've just suggested the *syntax* that I like more. Bye, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Other notes

2005-01-05 Thread bearophileHUGS
Thank you to all the gentle people that has given me some comments, and sorry for bothering you... Doug Holton: This will also likely never appear in Python. I know, that's why I've defined it wild. Also, you might request the NetLogo and StarLogo developers to support Jython (in addition to

Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-01 Thread bearophileHUGS
From An Introduction to Dao: So I realized the goodness of scripting languages, and spent about two weeks to write some Perl scripts to retrieve informa- tion from GO and construct the DAG. But that experience with Perl was not very nice, because of its complicated syntax. Then I started to

Re: for x in list that maximizes f(x)] --newbie help

2005-12-01 Thread bearophileHUGS
If the elements of mylist can be compared (es. not complex values), then this can be a solution for you: from math import sin as f mylist = [float(2*i)/10 for i in xrange(10)] pairs = [(f(x), x) for x in mylist] ymax = max(pairs)[1] print pairs, \n print ymax You can also try this, for Py2.4:

str attributes

2005-12-06 Thread bearophileHUGS
I don't know if this was already discussed. I think that maybe Python 2.5 can add some attributes to the str object: str.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' str.hexdigits '0123456789abcdefABCDEF' etc. And maybe this too: str.maketrans(from, to) (I think

Syntax and speed

2005-12-12 Thread bearophileHUGS
ShedSkin (http://shed-skin.blogspot.com) has taught me something: simple syntax and high speed can often go together, in a computer language. This means two things: 1) A fast language can have a simple (python-like) syntax. For example a language fast as C++ can allow: d = {hello:1} as a syntax

Online Ruby

2005-12-14 Thread bearophileHUGS
Maybe a page like this for Python exists already, or it can be useful to create it: http://tryruby.hobix.com/ Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: special operator =+

2005-12-15 Thread bearophileHUGS
kenny NguyenDoes anyone know the operator =+? It isn't an operator, it's equivalent to = (assignment) only. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Guido at Google

2005-12-22 Thread bearophileHUGS
This is interesting. With more Python time in Guido's hands maybe Py 3.0 is a bit closer... :-) I don't know if this is a silly idea: A small part of the wealth of a modern state is probably determined by the software it uses/produces, and a small part of this software is open source or free.

Re: Guido at Google

2005-12-23 Thread bearophileHUGS
This topic is discussed on Slashdot too: http://slashdot.org/articles/05/12/22/1832226.shtml?tid=217 There are some interesting comments, for example from curious Java or Perl programmers, etc. Some of them can probably appreciate this: http://cheeseshop.python.org/pypi/typecheck Among the noise

Re: GUI and graph

2005-12-23 Thread bearophileHUGS
Maybe this graph library can be useful to you: http://sourceforge.net/projects/pynetwork/ Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Distributions, RE-verb and the like

2005-12-29 Thread bearophileHUGS
Psyco is finished now, and it works on the x86, for Win, the new macs, many linux boxes, etc, and it's quite useful, so maybe it can be added to the standard Python distribution. PyChecker (and the other similar ones that work differently) is very useful too, and it's pure Python, so maybe it too

Re: Distributions, RE-verb and the like

2006-01-03 Thread bearophileHUGS
Paul McGuire wrote: I don't find 'Interval' to be very easy on the eyes. In this case, I stole^H^H^H^H^H borrowed the re form of [A-Za-z0-9], providing a method named srange (s is for string) such that srange(a-fA-F) would return the string abcdefABCDEF. Thank you for your answers Paul.

Re: inline function call

2006-01-04 Thread bearophileHUGS
Peter Hansenbut I'd be happy to see a real-world case where Psyco gave a much bigger boost.) Psyco can be very fast, but: - the program has to be the right one; - you have to use low level programming, programming more like in C, avoiding most of the nice things Python has, like list generators,

Re: inline function call

2006-01-04 Thread bearophileHUGS
I haven't examined the code very well, but generally I don't suggest to use exceptions inside tight loops that that have to go fast. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Processing and adding numbers from lines.

2006-01-05 Thread bearophileHUGS
With your input this returns: [200, 20, 0, 13050] so it's not what you want, but maybe it can be a starting point for you: from re import findall txt = \ test test test description (100-10-0, 6700 test) test test test description (100-10-0, 6350 test) lines = txt.split(\n) res =

Re: python speed

2006-01-06 Thread bearophileHUGS
It seems that Java JDK 1.4 (-server) HotSpot compiler sometimes (in this test, on this computer, etc.) can produce programs faster than C and Fortran ones in n-body simulations and similar stuff: http://shootout.alioth.debian.org/gp4/benchmark.php?test=nbodylang=all

Re: Multiway Branching

2006-01-09 Thread bearophileHUGS
A dict can be useful: byte1, byte2 = 32, 1 conv1 = {(32, 32):0, (36,32):natural, (32,1):5, (66,32):0.167} print conv1[byte1, byte2] If you use Psyco maybe something like this can be faster: conv2 = dict((k1*256+k2,v) for (k1,k2),v in conv1.items()) print conv2[(byte18) + byte2] conv1/conv2

Pliant language

2006-01-09 Thread bearophileHUGS
This post is about a programming language that I've never used, called Pliant. Sometimes knowing something about other languages can be useful for our language, so I think this is not a fully off topic post. Time ago I have found Python (that now I am using a lot) because I like to explore less

Re: Maximum List size (item number) limit?

2006-01-11 Thread bearophileHUGS
Juho Schultz NIR_mean_l only from lines 1, 4, 7, ... R_mean_l only from lines 2, 5, 8, ... G_mean_l only from lines 3, 6, 9, ... This can be the problem, but it can be right too. The following code is shorter and I hope cleaner, with it maybe Kriston-Vizi Janos can fix his problem. class

Re: flatten a level one list

2006-01-12 Thread bearophileHUGS
Well, maybe it's time to add a n-levels flatten() function to the language (or to add it to itertools). Python is open source, but I am not able to modify its C sources yet... Maybe Raymond Hettinger can find some time to do it for Py 2.5. Bye, bearophile --

Freezing

2006-01-12 Thread bearophileHUGS
Most of my ideas seem usless or stupid, but I think expressing them here doesn't harm much. This is an idea for Py 3.0, because it's not backward compatible. Dicts and sets require immutable keys, like tuples or frozensets, but to me they look like a duplication. So the idea is to remove tuples

Re: Freezing

2006-01-12 Thread bearophileHUGS
The first line of that example has to be: s = |set([1, 3, 5])| But I don't know/remember why set() can't accept many values like max/min: max([1,2,5]) max((1,2,5)) max(1,2,3) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Freezing

2006-01-13 Thread bearophileHUGS
Raymond Hettinger: I'm curious whether you've had an actual use for dictionaries as keys. I've never had this need (probably because it's an unsupported thing to do too). Likewise, how about frozensets? Have you had occasion to use them as keys? They were created to support sets of sets,

Re: Limiting the size of List and making it Homogeneous

2006-01-13 Thread bearophileHUGS
The array module allows you to specify a single type of elements. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Command config, quitting, binary, Timer

2005-09-06 Thread bearophileHUGS
Dennis Lee Bieber: Yes, but only when ref-counts go to 0... it may be that this tight loop never allowed stuff to go to 0 ref-counts. It definitely never returned control, so besides eating memory that way, any events for the GUI framework were also not being handled and had to be queued.

Re: Command config, quitting, binary, Timer

2005-09-06 Thread bearophileHUGS
BearophileThis can be fixed with a different dictionary that doesn't contain the leading 0s, No other dict is necessary: ! _nibbles = {0:, 1:0001, 2:0010, 3:0011, ! 4:0100, 5:0101, 6:0110, 7:0111, ! 8:1000, 9:1001, A:1010, B:1011, ! C:1100, D:1101, E:1110,

Re: sorting tuples...

2005-09-17 Thread bearophileHUGS
Uhm, if the file is clean you can use something like this: data = \ 200501221530 John *** long string here *** 200504151625 Clyde *** clyde's long string here *** 200503130935 Jeremy *** jeremy string here records = [rec.split(\n) for rec in data.split(\n\n)] records.sort() print records

C#3.0 and lambdas

2005-09-18 Thread bearophileHUGS
On Slashdot there is a discussion about the future C#3.0: http://developers.slashdot.org/developers/05/09/18/0545217.shtml?tid=109tid=8 http://msdn.microsoft.com/vcsharp/future/ There are many differences, but it looks a bit more like Python:

Re: Nufox : Xul + Python

2005-10-01 Thread bearophileHUGS
Nufox seems a really interesting thing (probably it can even be used to design GUIs for local desktop apps), but the site seems down at the moment. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Nufox : Xul + Python

2005-10-02 Thread bearophileHUGS
It doesn't work yet, to me... bearophile -- http://mail.python.org/mailman/listinfo/python-list

Multiple assignments simplification

2005-10-12 Thread bearophileHUGS
The current version of ShedSkin (http://shedskin.sourceforge.net/ experimental Python to C++ compiler) is rather alpha, and the development work is focused on debugging and implementing some more basic Python functionality. But hopefully in future versions more work will be spent to make the

Re: Multiple assignments simplification

2005-10-13 Thread bearophileHUGS
Thank you George Sakkis for your fast and accurate answer. In my life I am encountering lot of graph-based solutions to my problems. I'll try to implement your solution as soon as possible. Fredrik Lundhworking on a Python to C/C++ translator without knowing what kind of optimizations a C/C++

Re: Adding methods to an object

2005-10-13 Thread bearophileHUGS
This isn't code of mine, it's probably from the cookbook, maybe with little changes: | def addMethod(object, method, name=None): | if name is None: name = method.func_name | class newclass(object.__class__): | pass | setattr(newclass, name, method) | object.__class__ =

Some set operators

2005-10-15 Thread bearophileHUGS
Sometimes I suggest to add things to the language (like adding some set methods to dicts), but I've seen that I tend to forget the meaning of six set/frozenset operators: s t s = t s | t s |= t s ^ t s ^= t My suggestion is to remove them, and keep them only as explicit non-operator versions

Re: List of strings to list of floats ?

2005-10-17 Thread bearophileHUGS
Erik Max Francis: result = [sqrt(x**2 + y**2) for x, y in zip(xs, ys)] Another possibility: from math import hypot result = [hypot(*xy) for xy in zip(xs, ys)] Or better: from math import hypot result = map(hypot, xs, ys) bearophile -- http://mail.python.org/mailman/listinfo/python-list

Spaces and tabs again

2005-08-13 Thread bearophileHUGS
Hello, I know this topic was discussed a *lot* in the past, sorry if it bores you... From the Daily Python-URL I've seen this interesting Floating Point Benchmark: http://www.fourmilab.ch/fourmilog/archives/2005-08/000567.html This is the source pack: http://www.fourmilab.ch/fbench/fbench.zip

Re: Please Criticize My Code

2005-08-20 Thread bearophileHUGS
Two versions of mine, one of the fastest (not using Psyco) and one of the shortest: . from itertools import groupby . . def audioactiveFast(n): . strl = {(1,1,1): 31, (1,1): 21, (1,): 11, . (2,2,2): 32, (2,2): 22, (2,): 12, . (3,3,3): 33, (3,3): 23, (3,): 13 } .

Re: Bug in string.find

2005-08-28 Thread bearophileHUGS
I agree with Bryan Olson. I think it's a kind of bug, and it has to be fixed, like few other things. But I understand that this change can give little problems to the already written code... Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Command config, quitting, binary, Timer

2005-09-03 Thread bearophileHUGS
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. --- I am still ignorant about Tkinter. This little program, after pressing the Go eats more and more RAM, is it normal? Can it be avoided? (In normal programs this is isn't a real

Re: Command config, quitting, binary, Timer

2005-09-04 Thread bearophileHUGS
Witn your suggestions and with some tests and work I've solved most of the problems, thank you all for the comments. Peter Hansen: What did you expect to happen with the infinite loop inside dogo()? I expected that the same memory used by the b.config(command=...) can be used by the successive

Re: Recursive tree list from dictionary

2006-01-14 Thread bearophileHUGS
This isn't much tested, so don't trust it much, and I hope it's not overkill. You can find Graph here: http://sourceforge.net/projects/pynetwork/ With this you can plot the tree, if you want: g.springCoords(); g.plot2d() Bear hugs, bearophile def scan(g, parent): subs = [scan(g, sub) for

Re: Arithmetic sequences in Python

2006-01-16 Thread bearophileHUGS
Ranges of letters are quite useful, they are used a lot in Delphi/Ada languages: a, b, c, d, e... I like the syntax [1..n], it looks natural enough to me, but I think the Ruby syntax with ... isn't much natural. To avoid bugs the following two lines must have the same meaning: [1..n-1] [1..(n-1)]

weakref and memoizing

2006-01-19 Thread bearophileHUGS
This is yet another memoize decorator, it's meant to be resilient (and fast enough): http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466320 Like most memoize decorators it stores the pairs of data-result in cache dictionary, but Garrett Rooney says: it could be better if it used the new

isplit

2006-01-26 Thread bearophileHUGS
I have a file of lines that contains some extraneous chars, this the basic version of code to process it: IDtable = .join(map(chr, xrange(256))) text = file(..., rb).read().translate(IDtable, toRemove) for raw_line in file(file_name): line = raw_line.translate(IDtable, toRemove) ... A

Re: Efficient Find and Replace

2006-01-27 Thread bearophileHUGS
Because L.index() and L[x:x] both take O(N) time in the worst case. Why do you think L[x:x] can be O(N)? This looks O-linear enough to me: from random import choice L = [choice(ab) for i in xrange(10)] L ['b', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'a'] for x in xrange(len(L)): ... if

Re: Efficient Find and Replace

2006-01-27 Thread bearophileHUGS
But how is L[x] = Y an O(1) operation. Given x finding L[x] would require to traverse x nodes in the list. Python list is a deceptive name, because they are 1D arrays of pointers. Maybe they are called lists because array(...) is shorter than list(...). Bye, bearophile --

Re: Question about idioms for clearing a list

2006-01-31 Thread bearophileHUGS
Diez B. Roggisch: The reason is that l = [] just rebinds a new object (a list, but it could be anything) to a name, while l[:] = [] will alter the object _referred_ to by l. That is a HUGE difference! In my programs I have seen that there is another practical difference between version 1 and

Re: A class with built-in doctest feature ?

2006-02-02 Thread bearophileHUGS
The idea looks interesting, but you can also design a couple of functions that scan the docstrings of a given class and its methods to produce what you need: doctestAll(C) toHtml(C) This is probably simpler and gives similar results. Bye, bearophile --

Re: python's library support

2006-02-04 Thread bearophileHUGS
Robert KernAnd several others if you google a bit. Yes: http://sourceforge.net/projects/pynetwork/ Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

To paletted image

2006-02-10 Thread bearophileHUGS
Hello, this time I have a question about PIL usage, maybe if Lundh has some time he can answer me. I am experimenting different color quantization algorithms, so having computed the palette with a clustering function, I use the code below to quantize the original image to produce an image without

Re: Create dict from two lists

2006-02-10 Thread bearophileHUGS
py: Thanks, itertools.izip and just zip work great. However, I should have mentioned this, is that I need to keep the new dictionary sorted. A Python dictionary is an unsorted data structure, so you cannot have it sorted as you please. (I have seen that lot of people ask for a sorted

Re: Shortest prime number program

2006-02-11 Thread bearophileHUGS
This is a little shorter :-) [2]+[x for x in range(2,99)if 2**x%x==2] Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Is python very slow compared to C

2006-02-11 Thread bearophileHUGS
Yes this language is very slow, but frequently this isn't a problem, because you are using fast functions/libraries written in C, or you are doing I/O bound operations. And Psyco, numeric/numarray, ShedSkin can help in some other cases (Pyrex and other solutions allow you to mix in lower level

Re: Is python very slow compared to C

2006-02-12 Thread bearophileHUGS
Steven D'ApranoVery slow to do what, compared to what? The decay time of the tau meson? Probably every answer I can give you is wrong for you, so answering is almost useless... In this thread we have already given the most pertinent answers to the original question from Diffuse. I can show you

Re: processing limitation in Python

2006-02-14 Thread bearophileHUGS
Using CPython or GMPY with a smarter algorithm in acceptable time you can find that: 12345678987654 == 2 * 3 * 2057613164609 It's a very big number to factorize with that naive algorithm, so the program hangs... (I have used an online factoring service). Bye, bearophile --

Re: how to write a C-style for loop?

2006-02-15 Thread bearophileHUGS
Steven D'ApranoThat's a completely different question, so of course it has a completely different answer. Here is one way: Other versions without the creation of a list: for i in (2**n for n in xrange(6)): do_something(i) for i in (1n for n in xrange(6)): do_something(i) for i in

Re: define loop statement?

2006-02-17 Thread bearophileHUGS
David Isaac: I would like to be able to define a loop statement (nevermind why) so that I can write something like loop 10: do_something instead of for i in range(10): do_something Possible? If so, how? It seems that you are looking for macros; maybe Logix language

Re: Python vs. Lisp -- please explain

2006-02-19 Thread bearophileHUGS
The question about the speed is interesting. Generally the more dynamic and flexible your language is, the slower are the programs it produces. Lisp is almost unique in such matters (and in this regard it's maybe better than CPython) because it allows the programmer to mix and blend different

Re: Python vs. Lisp -- please explain

2006-02-19 Thread bearophileHUGS
Luis M. González[Shed-Skin] ... but so far it looks great (only one developer though..). Two developers, I am there too :-) I think people aren't much interested so far because there aren't good ways to link/join/use SSPython compied code from CPython code. A good solution is probably to: -

Re: deriving from float or int

2006-02-21 Thread bearophileHUGS
Probably this is interesting for you: http://home.tiscali.be/be052320/Unum.html I think its API can be improved, but it can be used. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: how to break a for loop?

2006-02-21 Thread bearophileHUGS
This time it seems that using itertools gives slower results, this is the test code: import itertools as it from operator import __not__ def trim_leading_zeros(seq): if not seq: return seq for pos, el in enumerate(seq): if el != 0: break if seq[pos] == 0:

With pyMinGW

2006-02-21 Thread bearophileHUGS
To use Pyrex, SWIG and the like on a Win2K I have followed this way: http://jove.prohosting.com/iwave/ipython/pyMinGW.html I already had MinGW 3.4.2, so I have decompressed the Python 2.4.2 sources, I have merged in the pyMinGW patch, and I have run the global compilation with: make -f

Re: With pyMinGW

2006-02-22 Thread bearophileHUGS
Thank you for your answers, Khalid. It seems you may be using an old version of pyMinGW, I have downloaded it yesterday from your site. the relevant part dealing with include directories in zlib.mak should read now as follows: I have checked, and that relevant part is the same in my

Re: Python vs. Lisp -- please explain

2006-02-22 Thread bearophileHUGS
Carl Friedrich Bolz: Indeed, there are similarities to pyrex. Of course in pyrex you have to give the types yourself, but since the type inference engine of PyPy can sometimes be hard to understand this is maybe not the worst trade-off. A nice advantage of the PyPy approach would be that you

Re: What's up with this code?

2006-02-22 Thread bearophileHUGS
Gregory Petrosyan: coefs.extend(it.chain(rcoefs1, rcoefs2)) #? -- here is magic Can't you just do a couple of extend? Something like: coefs.extend(rcoefs1) coefs.extend(rcoefs2) This looks simpler and probably faster too. Bye, bearophile --

Re: Pythonic gui format?

2006-02-22 Thread bearophileHUGS
Sorry for the late reply, I have some ideas about a possible GUI toolkit design (that works with one already done like GTK), I'll probably show them here. In the meantime I can show this one: http://thinlet.sourceforge.net/home.html I like it because the way GUIs are defined is quite short. bye,

Re: make a class instance from a string ?

2006-02-23 Thread bearophileHUGS
Bo Yang: to get an instance of the class 'classname' from a string , in python , how do I do that ? This is a possibile way: class C: pass c = locals()[C]() print c Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Pure python implementation of string-like class

2006-02-24 Thread bearophileHUGS
Maybe you can create your class using an array of 'L' with the array standard module. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

  1   2   3   4   5   6   7   8   9   10   >