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

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: 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 --

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: 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: 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: 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: 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

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

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

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

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: 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__ =

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: Nufox : Xul + Python

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

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

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: 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

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: 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

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: 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

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 } .

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: Merging overlapping spans/ranges

2005-05-10 Thread bearophileHUGS
This is the problem of finding the connected components inside an interval graph. You can implement the algorithms yourself, of you can use my graph data structure here: http://sourceforge.net/projects/pynetwork/ The graph methods: createIntervalgGraph And: connectedComponents can probably solve

Re: Unique Elements in a List

2005-05-09 Thread bearophileHUGS
runes: d = {} for k in data: try: d[k] += 1 except: d[k] = 1 for k,v in d.items(): if v == 1: print k For this problem, if duplicated keys are common enough, this version is probably the faster. With this *different* problem, it seems that sometimes

Re: pyvm -- faster python

2005-05-08 Thread bearophileHUGS
I've seen the benchmarks, they look quite interesting. This project is probably a LOT of work; maybe people can tell us about such efforts *before* doing so much work, so we can discuss it, and avoid wasting time. Maybe you can explain us why it is so fast, and/or maybe you can work with the

Re: ANN: pynetwork 2.25

2005-05-01 Thread bearophileHUGS
Kay SchluehrWhat is wrong with the other librarys that they can't be approved? I presume there are so many kinds of graphs, that one data structure doesn't fit all... On the other hand, I think that trying to design a too much general data structure can have some drawbacks. I've tried a

ANN: pynetwork 2.25

2005-04-30 Thread bearophileHUGS
Pynetwork is a graph library, my first SourceForge project: http://sourceforge.net/projects/pynetwork/ It included tests, some demo, some premilinary docs, and some images. You can see some screenshoots on the SourceForge page for them. I know about 5-6 other graph libraries for Python, one even

Re: large dictionary creation takes a LOT of time.

2005-04-29 Thread bearophileHUGS
Ville Vainio: This is a good place to use 'get' method of dict: frequency[word] = frequency.get(word,0) + 1 I think Kent Johnson is longer, but a bit faster... Bye, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
In my first post you can find two URLs with better flood filling algorithms. You can also modify the easy recursive function, using a list-based stack intead of recursive calls. Bye, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
Then you can probably use something like this: . def boxesArea(m, foreground=1, background=0): . maxr = len(m) . maxc = len(m[0]) . newCol = 2 . oldCol = foreground . for r,row in enumerate(m): . for c,e in enumerate(row): . if e == oldCol: .

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread bearophileHUGS
[EMAIL PROTECTED]: hi Bearphile! That really gives me an idea.Thanks much for that. Yes as you said the algorithm reaches a maximium recursion depth for larger sets i tried. You can improve the little flood filling function, avoiding the bad Python recursivity. Do you see where I am heading

Re: Bounding box on clusters in a 2D list

2005-04-23 Thread bearophileHUGS
I hope this is what you need, sometimes understanding the question is one of the hardest parts :-) If you can use a graph data structure, you can create a graph, and then you can find the lenght of all its connected components (indentations reduced to 2 spaces): . def mat2graph(g, m, good=None,

Re: Smart help again

2005-04-11 Thread bearophileHUGS
You can create your own Exception class, based on this recipe: Thank you for your answer, the recipe you have suggested gives a very big output, but it doesn't contain what I've asked for... :-) I was asking to see that faulty method/function parameters (or the first line of its docstring).

Smart help again

2005-04-10 Thread bearophileHUGS
Hello, here I extend the idea of the smart help I've discussed recently. When I receive an error like: TypeError: fun() takes exactly 2 arguments (1 given) I'd also like to see that method/function parameters (or the first line of its docstring). From a discussion with gentle programmers in

Re: sorting a list and counting interchanges

2005-04-07 Thread bearophileHUGS
Tim's solution is very nice, it comes from basic things often taught in good computer science courses. In Python when you have to do many (-1)**n you can do: (1-2*(n%2)) This seems quite faster. Bearophile -- http://mail.python.org/mailman/listinfo/python-list

On slashdot

2005-04-03 Thread bearophileHUGS
There is a discussion about Python Moving into the Enterprise on Slashdot: http://it.slashdot.org/it/05/04/03/0715209.shtml?tid=156tid=8 Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Suggesting methods with similar names

2005-04-02 Thread bearophileHUGS
Is that last idea so stupid? Still, I'd like to know if you know some little Python search engines for such purpose. Thank you, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Suggesting methods with similar names

2005-04-01 Thread bearophileHUGS
Suggesting method names based on a wrong method name can be useful, but I think the smart help can be improved: it can also be useful to have a suggestion for method names on the basis on a short description (or keywords) about what I want to do to/with the object. Maybe some people here can give

Pseudocode in the wikipedia

2005-04-01 Thread bearophileHUGS
The free wikipedia is adopting a standard pseudocode: http://en.wikipedia.org/wiki/Wikipedia_talk:Wikicode/Specification MShonle says something nice: I support the idea of wikicode. Basically I think we should present code in a Python-like language that doesn't carry so much baggage. For example,

Suggesting methods with similar names

2005-03-30 Thread bearophileHUGS
I have a class Surface with many methods. Working in the interactive window I receive an error like this when I write the wrong method name: table.addGlas() Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'Surface' object has no attribute 'addGlas' Is it possibile

Re: Suggesting methods with similar names

2005-03-30 Thread bearophileHUGS
Raymond HettingerWhen you're done, consider posting the result as an ASPN cookbook recipe. I still cannot write in the cookbook... I think I have problems with the registration. So you can put it there... I've found that difflib is good enough for the string matching. This idea isn't fully mine,

Re: Suggesting methods with similar names

2005-03-30 Thread bearophileHUGS
Diez B. Roggischit will make an object return always _something_ - and thus you don't catch misspellings in non-interactive Uhm, I'm sorry, I don't understand you. If you look at the code I've just posted, you can see that it still raises AttributeError, the difference is just the error

English to a bit of code

2005-03-24 Thread bearophileHUGS
It's still a toy, but it looks interesting. It converts in Python, Lisp and Java, and the shown image looks like Python: http://www.trnmag.com/Stories/2005/032305/Tool_turns_English_to_code_032305.html Google cache for a draft about it:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-22 Thread bearophileHUGS
R.H.: The setdefault() method would continue to exist but would likely not make it into Py3.0. I agee to remove the setdefault. I like the new count method, but I don't like the appendlist method, because I think it's too much specilized. I too use sets a lot; recently I've suggested to add a

Re: list of unique non-subset sets

2005-03-18 Thread bearophileHUGS
Looking at all the hyperedges in the connected component is a big waste... You can look at just the hyperedges that share one or more nodes. (Nodes are the original letters contained in the sets, and they must be hashable). If nodes aren't integers in [0, len(l)) then you can use this simpler

Re: list of unique non-subset sets

2005-03-17 Thread bearophileHUGS
s1 = set(['a','b','c']) s2 = set(['a','c']) s3 = set(['a','d','e','f']) s4 = set(['r','k','l']) s5 = set(['r','k','l']) ls = [s1,s2,s3,s4,s5] result1 = [s1, s3, s5] A result can contain s4 or s5 at random. This problem looks like the one of searching the correct hyperedges for a hypergraph. s1-5

Re: code for Computer Language Shootout

2005-03-16 Thread bearophileHUGS
Michael Spencer's version is nice, this is a bit shortened version. The main() isn't useful for this very short loop, and you can use shorter variable names to make lines shorter (this code isn't much readable, it's just for the Shootout, production quality code has probably to be more readable.

Re: Is there a short-circuiting dictionary get method?

2005-03-09 Thread bearophileHUGS
Maybe this can help: value = d.get('x', lambda: bigscaryfunction()) Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Jigsaw solver

2005-03-01 Thread bearophileHUGS
This can be interesting: http://science.slashdot.org/science/05/03/01/2340238.shtml Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: intersection of 2 list of pairs

2005-02-22 Thread bearophileHUGS
The use of frozenset can okay when sub-sequences are longer, but for this problem it's slow, and anyway there are situations of repeated data like this that have to be considered: frozenset( ('a', 'a') ) == frozenset(['a']) For Py2.4 the faster and better solution seems Peter Otten one. James

Re: exercise: partition a list by equivalence

2005-02-20 Thread bearophileHUGS
John Machin: Perhaps I'm missing a posting of yours -- what are merge2 and merge4? What is this random pairs test? What is xMax (nPairs isn't hard to guess), and how are you generating your input data? merge2 and this random pairs test comes from the post by Brian Beck. merge4 is the first in my

Re: Real-Time Fluid Dynamics for Games...

2005-02-20 Thread bearophileHUGS
Your two email addresses bouce emails back, so I post a shortened version of my comment here. I haven't installed: PyOpenGL-2.0.2.01.py2.4-numpy23 glut-3.7.6 Therefore at the moment I cannot try your interesting code. What's the speed of this Python code on your computer? I'd like to see a

Re: exercise: partition a list by equivalence

2005-02-19 Thread bearophileHUGS
David Eppstein: However it might be easier to treat the input pairs as the edges of a graph and apply a depth-first-search based graph connected components algorithm. || This would be O(n), though. Is the DFS the best one here (instead of BFS)? With the graph implementation that I've just

Re: exercise: partition a list by equivalence

2005-02-19 Thread bearophileHUGS
Bearophile: I presume the complexity is O(n+a); n (the nodes) is proportional to the number of pairs, and a (the arcs) depends on the intricacy of the input pairs. Opps... n (the number of nodes) is the number of different numbers contained in the pairs :-] Bearophile --

Re: exercise: partition a list by equivalence

2005-02-19 Thread bearophileHUGS
John MachinFWIW, here's a brief UAT report: Here is something else for you. Note: for more correct comparisons, for the following tests I've disabled the use of Psyco in Graph (usually enabled, if present). This looks faster than merge2 for this (quite sparse) random pairs test: . import graph #

Graph

2005-02-18 Thread bearophileHUGS
This is the first version of the graph module I was developing... In the meantime I've seen that there are already lots of graph implementations, even a compiled one: ftp://xray.imsb.au.dk/pub/python/packages/Python2.1/RPMS/python-kjbuckets-2.2-7.i686.rpm (This makes my work less important, but it

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

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: 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: 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

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: 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

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

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

<    7   8   9   10   11   12