Re: Best strategy for finding a pattern in a sequence of integers
On Fri, 21 Nov 2008 18:10:02 +0100 Gerard flanagan [EMAIL PROTECTED] wrote: data = ''' 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1''' data = [int(x) for x in data.split()] from itertools import groupby But groupby needs sorted data? Suppose the rules do not conflict or overlap and between them divide all the values, then maybe this would work: class StateMachine: def __init__(self,*rules): self.rules = rules self.state = len(rules) #deliberately unreachable self.first = True def change(self,x): #check and/or change state for i,rule in enumerate(self.rules): if rule(x): if i == self.state: #no state change return False else: #maybe state change self.state = i if self.first: #set initial state, no change self.first = False return False else: return True #state is changed raise ValueError def test(): data = ''' 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1''' data = map(int, data.split()) def rule1(x): return x in set((0, 3, 9)) def rule2(x): return x in set((6, 1, 10)) state = StateMachine(rule1,rule2) L = [] res = [] for x in data: if state.change(x): res.append(list(L)) L =[] L.append(x) res.append(list(L)) print res if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Possible bug in Tkinter for Python 2.6
On Wed, 19 Nov 2008 10:57:53 +0100 Eric Brunel [EMAIL PROTECTED] wrote: I'm trying out Python 2.6 and I found what might be a bug in the Tkinter module. How can I report it? maybe here: http://bugs.python.org/issue3774 The possible bug is a traceback when trying to delete a menu item in a menu where no items have associated commands. [...] It just seems the _tclCommands attribute on menus is initialized to None and never changed if none of the items actually have a command I ran into the same issue after I downloaded PysolFC-1.1 . I actually uploaded a patch for PysolFC for it for Python26 on Ubuntu and forgot all about it, thinking it could be some API change. Thanks for nailing down the problem a bit more and asking the questions here. It helped me to discover the bug reporting facility. According to the link above this bug is already reported and fixed, but it's nice to know anyway. Now for some really strange bug that still breaks PysolFC-1.1 on windows for me with Python26 ... (Python25 not affected): File c:\python26\lib\lib-tk\Tkinter.py, line 1202, in configure return self._configure('configure', cnf, kw) File c:\python26\lib\lib-tk\Tkinter.py, line 1193, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: image pyimage237 doesn't exist I suspect there has been some upgrade or new compilation of tcl that causes this behavior. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is psyco available for python 2.6?
On Thu, 30 Oct 2008 17:45:40 +0100 Gerhard Häring [EMAIL PROTECTED] wrote: psyco seems to just work on Linux with Python 2.6. So it is probably only a matter of compiling it on Windows for Python 2.6. Yes. I compiled it using wp setup.py build --compiler=mingw32 with cygwin, where wp was an alias for my windows xp python executable. For the OP and other people interested in windows binaries: I am in no way connected to or involved in the psyco development process -- except that I downloaded and compiled it -- but I have put a zip file on line for people who have a lot of trust in me and little patience for waiting for the official distribution. Just unpack it and put it in your site-packages directory. http://members.home.nl/anton.vredegoor/psyco/ A. -- http://mail.python.org/mailman/listinfo/python-list
Re: need help with converting c function to python function
In article [EMAIL PROTECTED], [EMAIL PROTECTED] says... i have a c function from some modbus documentation that i need to translate into python. it looks like this: unsigned short CRC16(puchMsg, usDataLen) unsigned char *puchMsg ; unsigned short usDataLen ; { unsigned char uchCRCHi = 0xFF ; unsigned char uchCRCLo = 0xFF ; unsigned uIndex ; while (usDataLen--) { uIndex = uchCRCHi ^ *puchMsgg++ ; uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ; uchCRCLo = auchCRCLo[uIndex] ; } return (uchCRCHi 8 | uchCRCLo) ; } some of it i can make out, but i can't seem to figgure out this part ' auchCRCHi[uIndex}; it looks like a typo, because there is a closing } that does not match the opening [. here is what i have so far, but is not giving me the right values def crc16(data): crc_hi = 0xff crc_lo = 0xff for x in data: crc_index = crc_hi ^ x crc_hi = crc_lo ^ (crc_hi | crc_index) crc_lo = crc_lo | crc_index return (crc_hi 8 | crc_lo) whaddya think? Use lateral thinking. CRC usually means some standard data checking algorithm. If you google for crc16 you will find various python implementations. Off hand I would first try this one because he seems to have been thinking about it: http://mail.python.org/pipermail/python-list/2005-September/342097.html But I don't really know what it is you are looking for, cyclic redundancy check? http://en.wikipedia.org/wiki/Cyclic_redundancy_check A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Permutation over a list with selected elements
[EMAIL PROTECTED] wrote: Given a list of elements that are either a character or a character follows by a number, e.g. ['a', 'b', 'c1', 'd', 'e1', 'f', 'c2', 'x', 'e2'] find all the permutations that are given by switching the positions of the elements that: (1) begins with the same letter, and (2) follows by a number. With the above list, some possible permutations are: ['a', 'b', 'c2', 'd', 'e1', 'f', 'c1', 'x', 'e2'] ['a', 'b', 'c1', 'd', 'e2', 'f', 'c2', 'x', 'e1'] ['a', 'b', 'c2', 'd', 'e2', 'f', 'c1', 'x', 'e1'] Another idea, untested. Also I am not sure whether sequences types are supposed to be returning functions ... A. from operator import mul from collections import defaultdict class Swapper: Given a set of indices this class returns functions which will swap elements in a list *in place*. Each function corresponds to a permutation of the set of indices. def __init__(self,L): self.L = L self.n = reduce(mul,range(2,len(L)+1),1) #faculty def __getitem__(self,i): L = self.L if not -1iself.n: raise IndexError def func(R): Q = perm([R[j] for j in L],i) for j,x in zip(L,Q): R[j] = x return func def perm(L,m): #permutation m of list L res = [] T = L[::-1] for i in range(len(L),0,-1): res.append(T.pop(m%i)) m /= i return res[::-1] def cross(args): #Raymond Hettinger's cross product function from ASPN ans = [[]] for arg in args: ans = [x+[y] for x in ans for y in arg] return ans def find_sets_of_indices_to_permute(L): set_by_letter = defaultdict(list) for i, elem in enumerate(L): if len(elem)1: set_by_letter[elem[0]].append(i) return set_by_letter.values() def test(): L = ['a', 'b', 'c1', 'd', 'e1', 'f', 'c2', 'x', 'e2'] I = find_sets_of_indices_to_permute(L) #Alex Martelli's function M = map(Swapper,I) for F in cross(M): # conserve the original list because #the functions modify a list in place R = list(L) # apply each permutation function one by one, # each is acting on a different set of indices for fn in F: fn(R) print R if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new class on the fly
Alex Martelli wrote: You can find a few examples of me demonstrating the subject of your interest by searching for my name e.g. on video.google.com; searching for my name on Amazon will show some books using similar techniques, and searching for my name on groups.google.com will find about 50,000 posts many of which exhibit essentially the same approach. Unfortunately, I can't currently offer such courses commercially while staying employed as Uber Tech Lead for Google, Inc, but if the monies on offer make it worth my while for me to drop million bucks worth of stock options, plus a vigorish for my other comp package _and_ the incredible amount of happiness I get every day from my job (where I get to interact with truly brlliant people, who, if and when they leave an erroneous snippet in, are GRATEFUL to me for pointing it out, rather than RESENTFUL and DEFENSIVE), I'll surely consider that most seriously (as long as the monies in question are in escrow for my personal reassurance). And still you are not bored with yourself? What a waste. Until such conditions should obtain, I'll just have to keep freely helping the people who are WORTH helping, and poking sarcastic funs at those who prove themselves ot be a waste of oxygen instead. So you have found out about the trick of never being wrong, and what's worse you now have a large group of followers continually reinforcing you and thus keeping you stuck in the psychic plane. May you have the life you deserve, It seems you already have it. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
Duncan Booth wrote: Recently there has been quite a bit of publicity about the One Laptop Per Child project. The XO laptop is just beginning rollout to children and provides two main programming environments: Squeak and Python. It is an exciting thought that that soon there will be millions of children in countries such as Nigeria, Brazil, Uruguay or Nepal[*] who have the potential to learn to program, but tragic if the Python community is too arrogant to consider it acceptable to use anything but English and ASCII. Please don't be too quick with assuming arrogance. I have studied social psychology for eleven years and my thesis was just about such a subject. I even held a degree in social psychology for some time before my government in their infinite wisdom decided to 'upgrade' the system so that only people holding *working* positions at a university would be able to convert their degrees to the new system. I suspect discerning people can still sense a twinge of disagreement with that in my professional attitude. However I still think the results of my research were valid. The idea was to try and measure whether it would be better for foreign students visiting the Netherlands to be kept in their own separate groups being able to speak their native language and to hold on to their own culture versus directly integrating them with the main culture by mixing them up with Dutch student groups (in this case the main culture was Dutch). I think I my research data supported the idea that it is best even for the foreigners themselves to adapt as quickly as possible to the main culture and start to interact with it by socializing with 'main culture' persons. My research at that time didn't fit in at all with the political climate of the time and subsequently it was impossible for me to find a job. That didn't mean that I forgot about it. I think a lot of the same ideas would help the OLPC project so that they will not make the same mistake of creating separate student populations. I believe -but that is a personal belief which I haven't been able to prove yet by doing research- that those people currently holding positions of power in the main culture actively *prevent* new groups to integrate because it would threaten their positions of power. So instead of having a favorable view of teachers who are 'adapting' to their students culture I have in fact quite the opposite view: Those teachers are actually harming the future prospects of their students. I'm not sure either whether they do it because they're trying to protect their own positions or are merely complicit to higher up political forces. Whatever you make of my position I would appreciate if you'd not directly conclude that I'm just being arrogant or haven't thought about the matter if I am of a different opinion than you. Yes, any sensible widespread project is going to mandate a particular language for variable names and comments, but I see no reason at all why they all have to use English. Well I clearly do see a reason why it would be in their very best interest to immediately start to use English and to interact with the main Python community. [*] BTW, I see OLPC Nepal is looking for volunteer Python programmers this Summer: if anyone fancies spending 6+ weeks in Nepal this Summer for no pay, see http://www.mail-archive.com/[EMAIL PROTECTED]/msg04109.html Thanks. I'll think about it. The main problem I see for my participation is that I have absolutely *no* personal funds to contribute to this project, not even to pay for my trip to that country or to pay my rent while I'm abroad. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
HYRY wrote: - should non-ASCII identifiers be supported? why? Yes. I want this for years. I am Chinese, and teaching some 12 years old children learning programming. The biggest problem is we cannot use Chinese words for the identifiers. As the program source becomes longer, they always lost their thought about the program logic. That is probably because they are just entering the developmental phase of being able to use formal operational reasoning. I can understand that they are looking for something to put the blame on but it is an error to give in to the idea that it is hard for 12 year olds to learn a foreign language. You realize that children learn new languages a lot faster than adults? English keywords and libraries is not the problem, because we only use about 30 - 50 of these words for teaching programming idea. They can remember these words in one week. But for the name of variable or function, it is difficult to remember all the English word. For example, when we are doing numbers, maybe these words: [odd, even, prime, minus ...], when we are programming for drawing: [line, circle, pixel, ...], when it's for GUI: [ button, event, menu...]. There are so many words that they cannot just remeber and use these words to explain there idea. Again, it's probably not the language but the formal logic they have problems with. Please do *not* conclude that some child is not very good at math or logic or programming when they are slow at first. It doesn't tell one very much how good they might become later on and some premature idea the teacher might have formed about a pupil in that phase can even be harmful for their later perspectives. Eventlly, when these children go to high school and study enough English, I think they can use English words for programming. But as the beginning step, it is difficult to learn both programming and English. The older they get the harder it is for them to learn language. By withholding them English language experience at an early age you are doing them a disservice because later on they will have more trouble. The other thing is trying to teach them formal operational logic when they are not yet ready for it. In that case it would be better to wait until they are ready, but unfortunately there are large variations in the age at which children become ready. Please do not confuse the two very different matters of language acquisition and formal operational logic. Language is learned at an early age while formal logic starts at about the age of eleven (but with very large variation among children). So, I made a little program, just replace all the Chinese words in the program to some sequency identifiers such as [a1, a2, a3, ...], So we can do programming in Chinese, and Python can still run it. Why not use IronPython? But anyway you are actually making things worse by *not* teaching them the language now that they will need later on and by *teaching* them formal operational logic at an age when they just get disappointed and frustrated by not yet being able to understand it. Better go easy on them and teach them lots of English computing terms and only introduce logic when they show they are ready. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
In article [EMAIL PROTECTED], [EMAIL PROTECTED] says... Martin v. Löwis: This PEP suggests to support non-ASCII letters (such as accented characters, Cyrillic, Greek, Kanji, etc.) in Python identifiers. I support this to ease integration with other languages and platforms that allow non-ASCII letters to be used in identifiers. Python has a strong heritage as a glue language and this has been enabled by adapting to the features of various environments rather than trying to assert a Pythonic view of how things should work. Neil Ouch! Now I seem to be disagreeing with the one who writes my editor. What will become of me now? A. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
Neil Hodgson wrote: Anton Vredegoor: Ouch! Now I seem to be disagreeing with the one who writes my editor. What will become of me now? It should be OK. I try to keep my anger under control and not cut off the pixel supply at the first stirrings of dissent. Thanks! I guess I won't have to make the obligatory Sovjet Russia joke now :-) It may be an idea to provide some more help for multilingual text such as allowing ranges of characters to be represented as hex escapes or character names automatically. Then someone who only normally uses ASCII can more easily audit patches that could contain non-ASCII characters. Now that I read about IronPython already supporting some larger character set I feel like I'm somewhat caught in a side effect of an embrace and extend scheme. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting troubles
[EMAIL PROTECTED] wrote: I see. I figured that list comprehensions made another list(duh), but I thought I could relink the object(List) to the new list and keep it once the function ended. Is it possible to pass a reference(to an object.. Like 'List', basically) to a function and change the reference to point to something created inside a function? Or all data unreturned from a function is lost and no references kept?(The second, I'd guess, since it's local temporary scope, but you never know, maybe Python can :) ) Maybe this (untested): def qsort(seq): if seq: pivot = seq.pop() Q = L,R = [],[] for x in seq: Q[x=pivot].append(x) qsort(R) seq[:] = L+[pivot]+R A. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
Martin v. Löwis wrote: In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel, changé, ошибка, or 売り場 (hoping that the latter one means counter). I am against this PEP for the following reasons: It will split up the Python user community into different language or interest groups without having any benefit as to making the language more expressive in an algorithmic way. Some time ago there was a discussion about introducing macros into the language. Among the reasons why macros were excluded was precisely because anyone could start writing their own kind of dialect of Python code, resulting in less people being able to read what other programmers wrote. And that last thing: 'Being able to easily read what other people wrote' (sometimes that 'other people' is yourself half a year later, but that isn't relevant in this specific case) is one of the main virtues in the Python programming community. Correct me if I'm wrong please. At that time I was considering to give up some user conformity because the very powerful syntax extensions would make Python rival Lisp. It's worth sacrificing something if one gets some other thing in return. However since then we have gained metaclasses, iterators and generators and even a C-like 'if' construct. Personally I'd also like to have a 'repeat-until'. These things are enough to keep us busy for a long time and in some respects this new syntax is even more powerful/dangerous than macros. But most importantly these extra burdens on the ease with which one is to read code are offset by gaining more expressiveness in the *coding* of scripts. While I have little doubt that in the end some stubborn mathematician or Frenchman will succeed in writing a preprocessor that would enable him to indoctrinate his students into his specific version of reality, I see little reason to actively endorse such foolishness. The last argument I'd like to make is about the very possibly reality that in a few years the Internet will be dominated by the Chinese language instead of by the English language. As a Dutchman I have no special interest in English being the language of the Internet but -given the status quo- I can see the advantages of everyone speaking the *same* language. If it be Chinese, Chinese I will start to learn, however inept I might be at it at first. That doesn't mean however that one should actively open up to a kind of contest as to which language will become the main language! On the contrary one should hold out as long as possible to the united group one has instead of dispersing into all kinds of experimental directions. Do we harm the Chinese in this way one might ask by making it harder for them to gain access to the net? Do we harm ourselves by not opening up in time to the new status quo? Yes, in a way these are valid points, but one should not forget that more advanced countries also have a responsibility to lead the way by providing an example, one should not think too lightly about that. Anyway, I feel that it will not be possible to hold off these developments in the long run, but great beneficial effects can still be attained by keeping the language as simple and expressive as possible and to adjust to new realities as soon as one of them becomes undeniably apparent (which is something entirely different than enthusiastically inviting them in and let them fight it out against each other in your own house) all the time taking responsibility to lead the way as long as one has any consensus left. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: OT somewhat: Do you telecommute? What do you wish the boss understood about it?
estherschindler wrote: * If you telecommute, full- or part-time, what *one* thing do you wish the CIO or IT Management would understand that they don't currently get? I'm not currently telecommuting but last year I had a telecommuting job for half a year. What I would want to say to all future employers considering to hire telecommuting personnel is : Don't let the telecommuter pay the extra costs that are caused by telecommuting. And I don't mean only the financial aspects, I also mean the immaterial costs. For example if one is working at home it can be hard for an employer to check whether the work is still progressing and if there are no immediate results there can be some suspicion that the employee is just sitting at home watching television or is out shopping, because hey, there's no way to check that anyway and people tend to become lazy if no one is watching them? So the employer can become tempted to find ways to check upon his employee by other means. Why not let him write a daily report of his activities even if you never read it? Why not ask for an open MSN chat window at all times so that one can check how fast the employee is responding? Is he at his desk *right now*? These are all things that are not usually asked of the people sitting in the main office and create an extra burden for the employee. In fact the employee gets burdened with the costs of the employers insecurities. If one doesn't trust the employee then don't hire him or don't let him telecommute in the first place! Then there are other aspects. For example sometimes I had to use an expensive mobile Internet connection when I was on the road or when the Internet connection at home didn't work. It was some account that lets one use some amount of megabytes for free but after that was used up there were high costs for each further megabyte. It was certainly the wrong kind of subscription but sometimes it's hard to determine whether one buys an expensive flat rate subscription with the risk of all this money never being used because one is using the home Internet connection all the time. On the other hand things can really get expensive if one has the cheap fixed megabytes type of account and the home Internet connection fails for an extended period or if one has to be on location often. So sometimes the wrong business decision was made. But if someone at the workplace has a HD crash or some other costly error happens this is normally not something the employee has to pay for. If one is informed about the costs and one doesn't read the emails but just says fix that server malfunction *now*, don't mind the connection costs one should not be scolding the employee for the large bills that appear one month later. Then there are things like travel costs and hotel costs, say we want the employee to be present at the office for a few days each month, the employee can pay for it in advance and the employer will reimburse him later on. Normally employees get a fixed paycheck each month and there are few extra costs involved and things can get arranged quickly. However the extra costs for the telecommuter are highly variable and so there can be a situation where one has payed in advance out of ones own pocket and one has to ask more than once to receive the money back. If one has to ask too often this can be highly demoralizing, because this is time and money spent on the company without earning anything. The employer maybe starts to think: Hey this guy is living in an expensive hotel and eating in restaurants while other people go there to have a vacation, so why should I have to pay for that? Well for the employee it's a completely different story, hotel rooms aren't fun if one arrives late at night and leaves early in the morning and cities remain tantalizing mysteries if one never has the time to do some sightseeing. There is also the idea that working at home is some luxurious privilege that the employee should be thankful for. I can tell you that even the nicest home can become a prison if one has to be there all the time. In fact any escape can be a relief so one is thankful to spend some time in a hotel room ... But that doesn't mean it's vacation! No way. It's just that other people get out of their homes normally at the beginning of the day, a telecommuter *has* to go out for a walk or go bicycling for half an hour or so during lunch break just to keep fit. A normal employee can integrate that into his routine of coming to work and having lunch at a nearby restaurant. So all in all my conclusion is, if one wants the employee to be happy and loyal, don't destroy his good intentions by letting him pay for all kinds of luxuries that he didn't ask for and that aren't even much fun anyway. Even though such things might seem the most desirable working environments for those having to work in a crowded office where they have to go to each day, sitting alone at
Re: Would You Write Python Articles or Screencasts for Money?
Antoon Pardon wrote: That's a good point, and also a valid reason for restricting the voting community to PSF members. Thanks, Alex. So in order to avoid a suspicion of a conflict of interest you want to turn the whole thing into private property of the PSF? That is the most ridiculous suggestion I have ever I kind of understand why they would want to do this. If you have no limitations on who may vote, such a contest can easily turn into a contest of who can mobilize the biggest clan of supporters. Sure, any democratic process can be derailed by a coordinated effort of people with a different mentality. To prevent such things by killing the democratic process oneself right at the beginning of a project is a peculiar way of avoiding this risk. Now maybe there are better ways to avoid this kind of unwanted effect but I wouldn't call Steve's suggestion ridiculous. It's about as ridiculous as proving that a stiff parrot is dead by grabbing it by the legs and repeatedly hitting it's head on the counter. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Would You Write Python Articles or Screencasts for Money?
Anton Vredegoor wrote: It's about as ridiculous as proving that a stiff parrot is dead by grabbing it by the legs and repeatedly hitting it's head on the counter. Or to write it's where its is more appropriate. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Would You Write Python Articles or Screencasts for Money?
Antoon Pardon wrote: On 2007-04-25, Anton Vredegoor [EMAIL PROTECTED] wrote: Antoon Pardon wrote: That's a good point, and also a valid reason for restricting the voting community to PSF members. Thanks, Alex. So in order to avoid a suspicion of a conflict of interest you want to turn the whole thing into private property of the PSF? That is the most ridiculous suggestion I have ever I kind of understand why they would want to do this. If you have no limitations on who may vote, such a contest can easily turn into a contest of who can mobilize the biggest clan of supporters. Sure, any democratic process can be derailed by a coordinated effort of people with a different mentality. To prevent such things by killing the democratic process oneself right at the beginning of a project is a peculiar way of avoiding this risk. As far as I understood the idea was to reward excellence. The process to achieve this can be democratic, but in that case it is just a means to an end. The democratic process was not an end itself. Yes, but this sub thread was about avoiding a suspicion of a conflict of interests. If this suspicion is to be avoided by just openly promoting the interests of the members of the PSF that is one hell of a way of solving the problem. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Would You Write Python Articles or Screencasts for Money?
Steve Holden wrote: I'm sorry, but while the PSF is a democratically-run organization its franchise doesn't extend beyond the membership. I didn't realize this was about an PSF internal affair. Of course a group of people can decide on its internal matters without asking anyone else, as long as its actions do not damage the larger community around it. I'm sorry to have interfered with your private discussions, they took place on an international newsgroup aimed at the global python community at large, you might consider taking it elsewhere, but since there is no moderation, go and spam this group with your posts as much as you like. You might as well suggest that America isn't being democratic because it doesn't allow the French to vote for its president. Neither France nor America strike me as being very democratic at the moment, because large parts of their communities have no way of voting on a person or party that represents their specific interests. It's the same way in the Netherlands where I live by he way, so no criticism is intended by me, in any way. I was picturing this thread more in terms like the international court which at least constitutionally *tries* to be democratic in a global community alike manner, albeit unfortunately also failing miserably. It seems current western democracies are preferable over the total chaos that we see in Iraq because its internal legal structure was removed without providing a functional alternative. History shows -to me- that even an evil dictator is better than that. That doesn't mean people shouldn't try to aim higher. Higher than accepting even a benevolent dictator. So in my case the question becomes if I would be willing to promote the interests of some group that doesn't represent me, for a chance that I may comply with their objectives, which I cannot influence. I'd say: No I don't want to do that, although I also wouldn't go out of my way in order to *not* comply. Thanks for clearing it all up. Have a nice life. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Would You Write Python Articles or Screencasts for Money?
Steve Holden wrote: When cash is involved, it's important to avoid even the slightest hint of a suggestion of a suspicion of a conflict of interest; that, I guess, is why firms that run contests with cash prizes always declare employees and their families not eligible, and why I think the PSF should do likewise in this case. That's a good point, and also a valid reason for restricting the voting community to PSF members. Thanks, Alex. So in order to avoid a suspicion of a conflict of interest you want to turn the whole thing into private property of the PSF? That is the most ridiculous suggestion I have ever -- http://mail.python.org/mailman/listinfo/python-list
Re: TK-grid problem, please help
Ray wrote: hi, I have a question about how to use .grid_forget (in python/TK) I need to work on grid repeatly. everytime when a button is pressed, the rows of grid is different. such like, first time, it generate 10 rows of data. 2nd time, it maybe only 5 rows. so I need a way to RESET the grid data every time. how can I do it? by grid_forger()?, then would anyone can help on how to use grid_forget() the sample code as following: I'm not sure if it solves your problem but this modification of your code at least *looks* like it works better. The entries are completely destroyed so that the next time you call the function they can be recreated. The trick I am using is to use a list in the arguments of the function but that is a bit of a hack, the list 'remembers' its state from the last time the function was called, I think one should use classes for bookkeeping such things instead. from Tkinter import * def mygrid(text,M = []): how to use grid_forget() to clean the grid??### while M: x = M.pop() x.destroy() rows = [] count=int(text) for i in range(count): cols = [] for j in range(4): e = Entry(frame3, relief=RIDGE) M.append(e) e.grid(row=i, column=j, sticky=NSEW) e.insert(END, '%d.%d' % (i, j)) cols.append(e) rows.append(cols) root=Tk() frame1=Frame(root, width=150, height=100) frame1.pack() text=Entry(frame1) text.pack(side=LEFT) button=Button(frame1, text='generate grid', command=(lambda: mygrid(text.get( button.pack() frame2=Frame(root, width=150, height=100) frame2.pack() button2=Button(frame2, text='exit', command=root.quit) button2.pack() frame3=Frame(root, width=150, height=300) frame3.pack() root.mainloop() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
[EMAIL PROTECTED] wrote: This one gets the order wrong. With def test(): L = 1, 2, 3, 'a', 4, 'a', 5, 'a', 6, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() print it1.next() print it2.next() print it1.next() print it2.next() 5 will appear before 4. Thanks. This one will give 4 first and it uses a normal iterator. from collections import deque def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: if pred(x) == p: Q[p].append(x) while Q[p]: yield Q[p].popleft() else: Q[~p].append(x) for x in gen(p): yield x while Q[p]: yield Q[p].popleft() return gen(1),gen(0) def test(): #L = 1, 2, 3, 'a', 'a' #L = 'a', 1, 2, 'a' #L = 1, 'a', 3, 'a', 4, 5, 6, 'a' L = 1, 2, 3, 'a', 4, 'a', 5, 'a', 6, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() print it1.next() print it2.next() print it1.next() print it2.next() print it2.next() print it2.next() if __name__=='__main__': test() Because the function can be stopped and resumed anytime, it is possible that at the new point of execution the queue has changed. For example if in these lines from the code above: if pred(x) == p: Q[p].append(x) while Q[p]: yield Q[p].popleft() I would make the change: if pred(x) == p: while Q[p]: yield Q[p].popleft() yield x Then the output will be out of order ... I wonder if this function is OK now. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: from collections import deque def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: if pred(x) == p: Q[p].append(x) while Q[p]: yield Q[p].popleft() else: Q[~p].append(x) for x in gen(p): yield x while Q[p]: yield Q[p].popleft() return gen(1),gen(0) Do we even need the line for x in gen(p): yield x ??!! It seems to work the same without it. from collections import deque def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: if pred(x) == p: Q[p].append(x) while Q[p]: yield Q[p].popleft() else: Q[~p].append(x) while Q[p]: yield Q[p].popleft() return gen(1),gen(0) What's up here? Was it a fata morgana? Am I overlooking something? A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: What's up here? Was it a fata morgana? Am I overlooking something? Even more crazy version: def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: Q[pred(x) == p].append(x) while Q[p]: yield Q[p].popleft() while Q[p]: yield Q[p].popleft() return gen(1),gen(0) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: Q[pred(x) == p].append(x) while Q[p]: yield Q[p].popleft() while Q[p]: yield Q[p].popleft() return gen(1),gen(0) This should be: def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): for x in it: Q[pred(x)].append(x) while Q[p]: yield Q[p].popleft() while Q[p]: yield Q[p].popleft() return gen(1),gen(0) But I'm still not sure if this is the desired function. Is it normal for people to start replying to their own messages when they're studying coroutines? A -- http://mail.python.org/mailman/listinfo/python-list
Re: Expanding tkinter widgets to fill the window
KDawg44 wrote: I am writing a GUI front end in Python using Tkinter. I have developed the GUI in a grid and specified the size of the window. The widgets are centered into the middle of the window. I would like them to fill the window. I tried using the sticky=E+W+N+S option on the widgets themselves and the window itself. How can I get this? If at all possible post a short, self-contained, correct, example demonstrating your question. http://homepage1.nifty.com/algafield/sscce.html A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: [EMAIL PROTECTED] wrote: Try it with def test(): L = 'a', 1, 2, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() The last print statement raises StopIteration... We, however, expected each iterator to contain two elements (one yielding 'a' then 'a', and the other yielding 1 then 2). Ouch! I never understood much about generators anyway. How about this one? from collections import deque class sentinel(object): pass class myiter(object): def __init__(self,seq): self.seq = seq self.index = -1 def __iter__(self): return self def next(self): self.index +=1 if self.index len(self.seq): return self.seq[self.index] else: return sentinel def mygen(seq): for x in seq: if x is sentinel: #way past bedtime raise StopIteration yield x def xsplitter(seq, pred): Q = deque(),deque() it = myiter(seq) def gen(p): for x in it: while Q[p]: yield Q[p].popleft() if pred(x) == p: yield x else: Q[~p].append(x) for x in gen(p): yield x return map(mygen,[gen(1),gen(0)]) def test(): L = 'a', 1, 2, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: Anton Vredegoor wrote: [EMAIL PROTECTED] wrote: Try it with def test(): L = 'a', 1, 2, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() The last print statement raises StopIteration... We, however, expected each iterator to contain two elements (one yielding 'a' then 'a', and the other yielding 1 then 2). Ouch! I never understood much about generators anyway. How about this one? No that can result in an infinite loop after yet another print it1.next() This one however ... from collections import deque class sentinel(object): pass class myiter(object): def __init__(self,seq): self.seq = seq self.index = -1 def __iter__(self): return self def next(self): self.index +=1 if self.index len(self.seq): return self.seq[self.index] else: return sentinel def xsplitter(seq, pred): Q = deque(),deque() it = myiter(seq) def gen(p): for x in it: while Q[p]: yield Q[p].popleft() if x is sentinel: break if pred(x) == p: yield x else: Q[~p].append(x) for x in gen(p): yield x return gen(1),gen(0) def test(): L = 'a', 1, 2, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
[EMAIL PROTECTED] wrote: Um, no. That one stops prematurely if your input sequence is: L = 1, 2, 3, 'a', 'a' Ah, thanks! You get points for persistence, however. :) Maybe this one is better? from collections import deque from itertools import chain, repeat def xsplitter(seq, pred): Q = deque(),deque() sentinel = object() it = chain(seq,repeat(sentinel)) def gen(p): for x in it: if pred(x) != p and x is not sentinel: Q[~p].append(x) for x in gen(p): yield x else: while Q[p]: yield Q[p].popleft() if pred(x) == p: yield x else: break return gen(1),gen(0) def test(): L = 1, 2, 3, 'a', 'a' #L = 'a', 1, 2, 'a' #L = 1, 'a', 3, 'a', 4, 5, 6, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
Anton Vredegoor wrote: Maybe this one is better? No, this one keeps generating output. But this one stops at least: from collections import deque from itertools import chain, repeat def xsplitter(seq, pred): Q = deque(),deque() sentinel = object() it = chain(seq,repeat(sentinel)) def gen(p): for x in it: if x is sentinel: while Q[p]: yield Q[p].popleft() break elif pred(x) == p: while Q[p]: yield Q[p].popleft() yield x else: Q[~p].append(x) for x in gen(p): yield x return gen(1),gen(0) def test(): L = 1, 2, 3, 'a', 'a' #L = 'a', 1, 2, 'a' #L = 1, 'a', 3, 'a', 4, 5, 6, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() if __name__=='__main__': test() Are there any other cases this doesn't cover? A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
[EMAIL PROTECTED] wrote: I have modified, simplified and (hopefully) improved Steven's code like this (but it may be a bit slower, because the class It is inside the function?): Here is a yet more simple version, I wonder if it still does the same thing, whatever it is you are looking for :-) def xsplitter(iseq, pred): class It(object): def __init__(self, fun, parity): self.divert = fun self.parity = parity self.queue = collections.deque() def append(self, item): self.queue.append(item) def __iter__(self): while self.queue: yield self.queue.popleft() for item in iseq: if pred(item) == self.parity: yield item else: self.divert(item) for x in self: yield x it1 = It(lambda y: it2.append(y), True) it2 = It(lambda y: it1.append(y), False) return it1, it2 A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
[EMAIL PROTECTED] wrote: If you don't wish to use objects, you can replace them with a closure: import collections def xsplitter(iseq, pred): queue = [ collections.deque(), collections.deque() ] def it(parity): while True: if queue[parity]: yield queue[parity].popleft() else: try: el = iseq.next() if pred(el) == parity: yield el else: queue[not parity].append(el) except StopIteration: raise its = [ it(False), it(True) ] return its[True], its[False] idata = iter([1, 'a', 3, 'a', 4, 5, 6, 'a']) it1, it2 = xsplitter(idata, lambda x: x == 'a') from itertools import izip for el1, el2 in izip(it1, it2): print el1, el2 Oh, I and do like your rewrite; it's much less repetitive and cleaner than my original version. But still, the 'while True:' loop and the 'try-except' clause and the explicit StopIteration are not necessary ... from collections import deque def xsplitter(seq, pred): Q = deque(),deque() it = iter(seq) def gen(p): while Q[p]: yield Q[p].popleft() for x in it: if pred(x) == p: yield x else: Q[~p].append(x) for x in gen(p): yield x return gen(1),gen(0) def test(): L = 1, 'a', 3, 'a', 4, 5, 6, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiremberco
[EMAIL PROTECTED] wrote: Try it with def test(): L = 'a', 1, 2, 'a' it1, it2 = xsplitter(L, lambda x: x == 'a') print it1.next() print it2.next() print it1.next() print it2.next() The last print statement raises StopIteration... We, however, expected each iterator to contain two elements (one yielding 'a' then 'a', and the other yielding 1 then 2). Ouch! I never understood much about generators anyway. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: combination function in python
[EMAIL PROTECTED] wrote: Isn't that what docstrings are for? Can't you leave the function name noverk() and add something to the effect of this function calculates combinations? Then it would show up in searches, wouldn't it? Yes, a doc string would help finding it in searches, however since this thread now contains a collection of function names it will suffice. There is also the other consideration of being able to easily read code that others write. Functions that try to do the same thing having the same name would help. If we had some module in the standard distro that would contain comb and fac and maybe a few other functions people could start using the same name to point to the same thing etc. def noverk(n,k): ? ? ?return reduce(lambda a,b: a*(n-b)/(b+1),xrange(k),1) This is a rather concise function which has the added advantage that it returns 0 when kn. import gmpy print gmpy.comb(4,8) ## 0 There is more to it than that. If I see the way the function computes the values it gives me hints about how I could write a function that lists all combinations. For example the fact that one can divide the product 'on the fly' -I mean without computing the totals above and below the division bar- tells me something about how to tackle the problem of generating the corresponding combinatorial structure. I *love* combinatorial structures. Little functions like this have more than once helped me to understand them better. Math is not so much my way of describing things, I like executable pseudo code better. Sometimes I had to read math formulas and have become somewhat used to that notation -it has its uses too- but if things are written down in code I can rely on the *computer* to execute the code instead of my brain which unfortunately is not as reliable. These advantages are lost when one just imports some optimized code library. Perhaps you should use the name comb_works_just_like_the_gmpy_version_only_slower() Or maybe 'comb_in_executable_pseudocode', but maybe some other implementations posted in this thread would be better suited for that. Anyway, I have the impression you just don't get the point of people posting various functions so that one can determine which algorithms work best or so that one can include each others ideas and create better functions that combine the best elements of all posted code. Cutting and pasting Python functions works a lot better in Usenet than importing C libraries. If no one posts code -even if it's not completely working code- how are we to learn from each other? The fact that someone else somewhere already saw the light and wrote the perfect function in optimized C or Fortran code should not stop us from 'reinventing the wheel' -as you call it- because one can get more proficient in wheel making in the process and every now and then it even leads to better wheels. Don't even get me started about this archaic scheme of writing code only for 'work' instead of for gaining insight that you seem to promote. But when there *is* no documentation, that becomes a problem, doesn't it? This thread is also documentation and I hope it will encourage people to post more code and share thoughts. Unless you don't know how to write the functions you need in which case you're better off relying on external modules. Have you ever wondered why Python even *has* a standard library? Why doesn't everyone just write the functionality they need? The main virtue of Pythons standard library should be that it shows one how to do things and that it introduces a common naming scheme. That was what got me interested in python in the first place: Now I got to see how things work! No more dark secrets and proprietary code! Sometimes it's better to sacrifice clarity for speed and write optimized lower level code but that doesn't mean there aren't large trade offs involved. In the future if computers will become faster it would be wise to drop the optimized code libraries again and reintroduce the Python versions of the same because by then the Python versions would be fast enough and it would also result in a smaller code base (but a more functional and flexible and readable one). What are you doing on this planet anyway if it's not trying to understand things? Since I'm also interested in the functions themselves -in this case- I'd rather have a few lines of code in my source than importing an optimized code library. Well, some people prefer optimized libraries because they have real work to do, not just acedemic excercizes. Real work is just an excuse for having a larger slice of the cake than other people. Other people can be just as or even more essential in the whole process of developing code than those who get paid. A reason against using third party libraries in general is not wanting to include too many external dependencies that would force the user to download all kinds of things from
Re: proposed PEP: iterator splicing
Paul Rubin wrote: def some_gen(): ... yield *some_other_gen() comes to mind. Less clutter, and avoids yet another temp variable polluting the namespace. Thoughts? Well, not directly related to your question, but maybe these are some ideas that would help determine what we think generators are and what we would like them to become. I'm currently also fascinated by the new generator possibilities, for example sending back a value to the generator by making yield return a value. What I would like to use it for is when I have a very long generator and I need just a slice of the values. That would mean running through a loop, discarding all the values until the generator is in the desired state and only then start doing something with the output. Instead I would like to directly set or 'wind' -like a file- a generator into some specific state. That would mean having some convention for generators signaling their length (like xrange): it = xrange(100) len(it) 100 Note: xrange didn't create a list, but it has a length! Also we would need some convention for a generator to signal that it can jump to a certain state without computing all previous values. That means the computations are independent and could for example be distributed across different processors or threads. it = range(100) it[50:] [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] But currently this doesn't work for xrange: it = xrange(100) it[50:] Traceback (most recent call last): File stdin, line 1, in ? TypeError: sequence index must be integer Even though xrange *could* know somehow what its slice would look like. Another problem I have is with the itertools module: itertools.islice(g(),1) Traceback (most recent call last): File stdin, line 1, in ? ValueError: Stop argument must be a non-negative integer or None. I want islice and related functions to use long integers for indexing. But of course this only makes sense when there already are generator slices possible, else there would be no practical way to reach such big numbers by silently looping through the parts of the sequence until one reaches the point one is interested in. I also have thought about the thing you are proposing, there is itertools.chain of course but that only works when one can call it from 'outside' the generator. Suppose one wants to generate all unique permutations of something. One idea would be to sort the sequence and then start generating successors until one reaches the point the sequence is completely reversed. But what if one wants to start with the actual state the sequence is in? One could generate successors until one reaches the 'end' and then continue by generating successors from the 'beginning' until one reaches the original state. Note that by changing the cmp function this generator could also iterate in reverse from any point. There only would need to be a way to change the cmp function of a running generator instance. from operator import ge,le from itertools import chain def mutate(R,i,j): a,b,c,d,e = R[:i],R[i:i+1],R[i+1:j],R[j:j+1],R[j+1:] return a+d+(c+b+e)[::-1] def _pgen(L, cmpf = ge): R = L[:] yield R n = len(R) if n = 2: while True: i,j = n-2,n-1 while cmpf(R[i],R[i+1]): i -= 1 if i == -1: return while cmpf(R[i],R[j]): j -= 1 R = mutate(R,i,j) yield R def perm(L): F = _pgen(L) B = _pgen(L,le) B.next() return chain(F,B) def test(): P = '12124' g = perm(P) for i,x in enumerate(g): print '%4i) %s' %(i, x) if __name__ == '__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: proposed PEP: iterator splicing
Kay Schluehr wrote: Maybe you should start by developing a design pattern first and publish it in the Cookbook. I have the fuzzy impression that the idea you are after, requires more powerfull control structures such as delimited continuations that are beyond ths scope of Pythons simple coroutines. I was reacting to a request for thoughts. I firmly believe that there should be a relatively unrestricted brainstorming phase before one tries to implement things. I have often noticed a lot of counterproductive bickering about use cases and implementations on the developers list, but luckily not so much here. There is something like premature optimizations with respect to creative processes too! A. -- http://mail.python.org/mailman/listinfo/python-list
Re: combination function in python
Jussi Piitulainen wrote: There's probably even a really clever way to avoid that final division, but I suspect that would cost more in time and memory than it would save. We're getting closer and closer to something I already posted a few times here. This implementation was unfortunate because I consistently used an uncommon name for it so people couldn't easily find it (mea culpa), and maybe also because it uses the despised reduce builtin. def noverk(n,k): return reduce(lambda a,b: a*(n-b)/(b+1),xrange(k),1) BTW I hereby give up the strange name for this and request a better name that everybody will use so there will be no confusion anymore. Maybe comb(n,k) ? Here's one non-clever one for integers n, k that uses n^k / k^k (falling powers) with the smaller of k and n - k as lower index: def choose(n, k): if 0 = k = n: ntok = 1 ktok = 1 for t in xrange(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 Ha, my function uses smaller subproducts :-) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: combination function in python
[EMAIL PROTECTED] wrote: We're getting closer and closer to something I already posted a few times here. This implementation was unfortunate because I consistently used an uncommon name for it so people couldn't easily find it But then, who's looking for it? The OP was trying to find it in the docs, assuming it was some kind of builtin function. I can see the logic of that and I can also see the logic of including some other smallish functions like for example fac. Failing that, the next recourse would be the Internet, or a Usenet search but that would imply well named Usenet posts and function names. (mea culpa), and maybe also because it uses the despised reduce builtin. def noverk(n,k): � � �return reduce(lambda a,b: a*(n-b)/(b+1),xrange(k),1) This is a rather concise function which has the added advantage that it returns 0 when kn. BTW I hereby give up the strange name for this and request a better name that everybody will use so there will be no confusion anymore. Maybe comb(n,k) ? No, that name's already used by gmpy. And a single function doesn't replace all of gmpy's other functionality, many of which are needed in the same applications where comb() is used, so there's really no need for your function. Actually, by proposing comb I am *honoring* gmpy and I'm also complying with it. In Python we use namespaces to differentiate between such things. You might want to read the documentation about it some time. Your time is better spent applying the tools provided by gmpy rather than trying to re-invent the wheel. Please let me be the judge of how to spend my time. In this case it seems rather unjustified to introduce dependencies on external modules when only a few functions are needed. Since I'm also interested in the functions themselves -in this case- I'd rather have a few lines of code in my source than importing an optimized code library. There *are* situations where such things are completely justified, but I don't think this is one of them. You could take it up with the gmpy author and induce him to get gmpy included in the standard distro if you are so inclined. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Emergence of Grok
Paul McGuire wrote: I just stumbled upon a great-looking project, to make Zope3 more approachable to mere mortals such as myself. Echoing the ROR mantra of convention over configuration, the Grok project (http:// grok.zope.org/) aims to stand on the shoulders of Zope3, while providing the ease of development of ROR (for instance, no more ZCML). Philipp von Weitershausen has a set of slides at http://philikon.de/files/grok-bbq-sprint.pdf, and the Grok project page links to a number of tutorials. Am I the last person to hear about this? No, not anymore since I learned about it later than you :-) However I can assure you this won't work. I'll tell you why. I'm a reasonably experienced python programmer and eager and interested to explore new -to me- paradigms, especially when there seems to be a possibility for making some money. I was lucky to find some programming job where I could learn zope and plone and make some money at the same time. I noticed that somewhere between pure python and zope there is a division where one cannot cross except by *paying* someone some money in order to learn how things work. When I told my employer at the time about this -he was totally *not* a programmer- the response was like, hey, but his other guy learned about it in one month etc, etc, or he just pointed at some plone programming colleagues who 'kicked ass'. Upon inspection of the 'ass kicking' colleagues' code I noticed that they had only a very rudimentary knowledge of python, or even didn't know much about programming altogether. There is no way around paying money because however 'intuitive' the code seems to be to the developers it is just not possible to read their sourcecode and understand what they are doing. This is a result of rapidly changing conventions that make it impossible to reproduce or adapt working code to one's specific likings. So the code examples are just one offs, working only for this specific case. Any adaptation will break the code, forcing one to contact -and pay- the developers, who are probably already working on some more advanced version. As result we see the emergence of 'more easy' solutions -like plone- which try to hide the complexities of zope for the end user but end up becoming even harder to 'grok' because the zope developers are racing ahead and don't bother to explain things unless they get paid. And why shouldn't they because they have to make a living too, don't they? Well I've got to make a living too and being forced to continually follow them around unable to pay for education and as a consequence not able to reproduce their code is just not financially feasible. Sorry, but this stuff is only 'grokkable' for the early adopters, however good ones' general python skills are. The problem gets even worse because by now -in order to make any money programming zope at all- one has to learn the zillion different ways _plone_ makes things 'easier' (it's not like there is a clean slate when one starts to work at a zope/plone programming site). The plone community is generally despised by the zope community because they are even more programming for money and not for universal enlightenment. I even suspect some plone programmers of hiding essential information from colleagues because that make *them* the experts, it's either that or maybe they just don't know themselves why things work the way they do and can't answer questions. OK, lets give them the benefit of doubt. While such deviousness generally cannot be assumed for the zope developers - try and ask questions on zope versus plone forums and notice the difference in attitude to persons asking for information- there is still a large difference in the way the zope community and the general python community 'think' about documentation. One can get close to understanding but in order to make it work there is no way around paying someone. I just know because I am good with python, I can read code and I know when things are not explained adequately. Essentially all this stuff is a pyramid scheme, only profitable for the well connected and/or the early adopters. That doesn't mean that I haven't met a lot of enthusiastic and helpful zope developers and programmers, it's just that they don't understand or don't want to understand that money can be a show stopper for people wanting to learn about zope. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple integer comparison problem
Bart Willems wrote: I have a feeling that there's a Python-solution that is shorter yet better readable, I just can't figure it out yet... Shorter (and faster for big lists): Yes. More readable: I don't know, I guess that depends on ones familiarity with the procedure. import bisect def grader(score): c = bisect.bisect([60,70,80,90],score) return 'FDCBA'[c] A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hpw make lists that are easy to sort.
Paul Rubin wrote: Oh, I see what you mean. I don't see an obvious faster way to do it and I don't have the feeling that one necessarily exists. As someone mentioned, you could do an n-way merge, which at least avoids using quadratic memory. Here's a version using Frederik Lundh's trick of representing a lazy list as its head plus the generator for the tail: That's a beautiful trick! I was just exploring some idea about traversing the matrix starting from the upper left and ending at the lower right by forming some kind of wave like front line. It's currently very slow but I hope it can be polished a bit. Also I was trying to figure out if it could have any advantage over the straight row by row merge, but now that these lazy rows have appeared the field has changed a lot :-) def typewriter(L,R): Z = [0] * len(R) M = [(L[0]+R[0],0)] while M: val,k = min(M) yield val Z[k] += 1 M = [] for k,x in enumerate(Z): if x len(R): M.append((L[k]+R[x],k)) if not x: break A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hpw make lists that are easy to sort.
Terry Reedy wrote: If I understand correctly, you want to multiiply each of m numbers by each of n numbers, giving m*n products. That is O(m*n) work. Inserting (and extracting) each of these is a constant size m priority cue takes, I believe, O(log(m)) work, for a total of m*n*log(m). That is faster than O(m*n*log(m*n)) for sorting m*n random numbers. According to this page: http://maven.smith.edu/~orourke/TOPP/P41.html You are very close. The only thing is whether the logarithmic factor can be removed. But there's more: /quote If the input consists of n integers between - M and M, an algorithm of Seidel based on fast Fourier transforms runs in O(n + M log M) time [Eri99a]. The $ \Omega$(n2) lower bounds require exponentially large integers. quote So maybe there is something at least for this specific case. I hope I'm not irritating someone by posting my thought processes here, since posting things sometimes seems to be the only way to find the links. I wonder if it's the selective attention that makes them turn up after posting or whether your talk about big O's has put me on the right track. Thanks anyway. The problem is still open in general, but some hacks are possible, as Paul Rubin said. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hpw make lists that are easy to sort.
Terry Reedy wrote: If I understand correctly, you want to multiiply each of m numbers by each of n numbers, giving m*n products. That is O(m*n) work. Inserting (and extracting) each of these is a constant size m priority cue takes, I believe, O(log(m)) work, for a total of m*n*log(m). That is faster than O(m*n*log(m*n)) for sorting m*n random numbers. Probably, I'm not very good with big O computations. Please forget my earlier post and please also ignore the unfortunate subject line. I want the cartesian product of the lists but I want the sums of the items. Suppose the function to combine the items is def f(i,j): return i+j And the list we want to sort would be made like this: LR = [f(i,j) for i in L for j in R] That would be like in my original post. However if the function would have been: def g(i,j): return n*i+j The resulting cartesian product of the list would be sorted a lot quicker, especially if the two lists -L and R- we start with are sorted. (n is the length of both lists here) So if changing the way the items are combined influences sorting time, is there also a way to optimize the order of generating the items for later sorting. I mean optimized for a specific combining function, in this case function f. I don't know how you would sort by hashing. Me too. I also learned that hashing is O(1) for non-mathematicians. Probably I'm suffering from a mild outbreak of spring. I'm currently trying to stop myself from starting to smoke again or writing critical posts about PyPy, if it explains anything. A. -- http://mail.python.org/mailman/listinfo/python-list
Hpw make lists that are easy to sort.
Python's sorting algorithm takes advantage of preexisting order in a sequence: #sort_test.py import random import time def test(): n = 1000 k = 2**28 L = random.sample(xrange(-k,k),n) R = random.sample(xrange(-k,k),n) t = time.time() LR = [(i+j) for i in L for j in R] print time.time()-t LR.sort() print time.time()-t print t = time.time() #L.sort() R.sort() presorted_LR = [(i+j) for i in L for j in R] print time.time()-t presorted_LR.sort() print time.time()-t if __name__=='__main__': test() On this -very slow- computer this prints: d:\python25\pythonw -u sort_test.py 1.1014305 8.9603815 1.1014305 5.4900954 Exit code: 0 Presorting the second sequence gains us more than three seconds. I wonder if there is a way to generate the combined items in such a way that sorting them is even faster? Is there some other sorting algorithm that can specifically take advantage of this way -or another way- of generating this list? The final sequence is len(L)*len(R) long but it is produced from only len(L)+len(R) different items, is it possible to exploit this fact? I'd also be interested in a more general solution that would work for summing the items of more than two lists in this way. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hpw make lists that are easy to sort.
Paul Rubin wrote: Well there are various hacks one can think of, but is there an actual application you have in mind? Suppose both input lists are sorted. Then the product list is still not sorted but it's also not completely unsorted. How can I sort the product? I want to know if it is necessary to compute the complete product list first in order to sort it. Is it possible to generate the items in sorted order using only a small stack? Also, I have a sumfour script that is slow because of sorting. It would become competitive to the hashing solution if the sorting would be about ten times faster. If the items could be generated directly in order the script would also have only a very small memory footprint. If you really want the sum of several probability distriutions (in this case it's the sum of several copies of the uniform distribution), it's the convolution of the distributions being summed. You can do that with the fast fourier transform much more efficiently than grinding out that cartesian product. But I don't know if that's anything like what you're trying to do. I want the product, but sorted in less time. If Fourier transforms can help, I want them :-) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hpw make lists that are easy to sort.
Terry Reedy wrote: One could generate the items in order in less space by doing, for instance, an m-way merge, in which only the lowest member of each of the m sublists is present at any one time. But I don't know if this (which is O(m*n*log(m))) would be any faster (in some Python implementation) for any particular values of m and m. If hashing is O(n+m), it would mean that it would be faster. I'm not sure if I can agree with your analysis. All information to generate the product is already inside the two lists we begin with. Doesn't that make the product less complex than a random n*m matrix? Or is that what you are saying with O(m*n*log(m)) ? A. -- http://mail.python.org/mailman/listinfo/python-list
Re: A better webpage filter
John J. Lee wrote: http://webcleaner.sourceforge.net/ Thanks, I will look into it sometime. Essentially my problem has been solved by switching to opera, but old habits die hard and I find myself using Mozilla and my little script more often than would be logical. Maybe the idea of having a *Python* script open at all times to which all content goes through is just too tempting. I mean if there's some possible irritation on a site theoretically I could just write a specific function to get rid of it. This mental setting works as a placebo on my web browsing experience so that the actual problems don't always even need to be solved ... I hope I'm not losing all traditional programmers here in this approach :-) Not actually tried it myself, though did browse some of the code once or twice -- does some clever stuff. Lots of other Python-implemented HTTP proxies, some of which are relevant (though AFAIK all less sophisticated than webcleaner), are listed on Alan Kennedy's nice page here: http://xhaus.com/alan/python/proxies.html A surprising amount of diversity there. At least now I know what general category seems to be nearest to my solution so thanks again for that. However my solution is not really doing anything like the programs on this page (although it is related to removing ads), instead it tries to modulate a copy of the page after it's been saved on disk. This removes all kinds of links and enables one to definitely and finally reshape the form the page will take. As such it is more concerned with the metaphysical image the page makes on the users brain and less with the actual content or the security aspects. One thing I noticed though on that (nice!) Alan Kennedy page is that there was a script that was so small that it didn't even have a homepage but instead it just relied on a google groups post! I guess you can see that I liked that one :-) My filter is even smaller. I've tried to make it smaller still by removing the batch file and using webbrowser.open(some cStringIO object) but that didn't work on windows. regards, A. -- http://mail.python.org/mailman/listinfo/python-list
Re: A better webpage filter
Gabriel Genellina wrote: If you don't mind using JavaScript instead of Python, UserJS is for you: http://www.opera.com/support/tutorials/userjs/ My script loads a saved copy of a page and uses it to open an extra tab with a filtered view. It also works when javascript is disabled. A. -- http://mail.python.org/mailman/listinfo/python-list
A better webpage filter
Since a few days I've been experimenting with a construct that enables me to send the sourcecode of the web page I'm reading through a Python script and then into a new tab in Mozilla. The new tab is automatically opened so the process feels very natural, although there's a lot of reading, filtering and writing behind the scene. I want to do three things with this post: A) Explain the process so that people can try it for themselves and say Hey stupid, I've been doing the same thing with greasemonkey for ages, or maybe You're great, this is easy to see, since the crux of the biscuit is the apostrophe. Both kind of comments are very welcome. B) Explain why I want such a thing. C) If this approach is still valid after all the before, ask help for writing a better Python htmlfilter.py So here we go: A) Explain the process We need : - mozilla firefox http://en-us.www.mozilla.com/en-US/ - add-on viewsourcewith https://addons.mozilla.org/firefox/394/ - batch file (on windows): (htmfilter.bat) d:\python25\python.exe D:\Python25\Scripts\htmlfilter.py %1 out.html start out.html - a python script: #htmfilter.py import sys def htmlfilter(fname, skip = []): f = file(fname) data = f.read() L = [] for i,x in enumerate(data): if x == '': j = i elif x =='': L.append((j,i)) R = list(data) for i,j in reversed(L): s = data[i:j+1] for x in skip: if x in s: R[i:j+1] = ' ' break return ''.join(R) def test(): if len(sys.argv) == 2: skip = ['div','table'] fname = sys.argv[1].strip() print htmlfilter(fname,skip) if __name__=='__main__': test() Now install the htmlfilter.py file in your Python scripts dir and adapt the batchfile to point to it. To use the viewsourcewith add-on to open the batchfile: Go to some webpage, left click and view the source with the batchfile. B) Explain why I want such a thing. OK maybe this should have been the thing to start with, but hey it's such an interesting technique it's almost a waste no to give it a chance before my idea is dissed :-) Most web pages I visit lately are taking so much room for ads (even with adblocker installed) that the mere 20 columns of text that are available for reading are slowing me down unacceptably. I have tried clicking 'print this' or 'printer friendly' or using 'no style' from the mozilla menu and switching back again for other pages but it was tedious to say the least. Every webpage has different conventions. In the end I just started editing web pages' source code by hand, cutting out the beef and saving it as a html file with only text, no scripts or formatting. But that was also not very satisfying because raw web pages are *big*. Then I found out I often could just replace all 'table' or 'div' elements with a space and the page -although not very html compliant any more- still loads and often the text looks a lot better. This worked for at least 50 percent of the pages and restored my autonomy and independence in reading web pages! (Which I do a lot by the way, maybe for most people the problem is not very irritating, because they don't read as much? Tell me that too, I want to know :-) C) Ask help writing a better Python htmlfilter.py Please. You see the code for yourself, this must be done better :-) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: A better webpage filter
Gabriel Genellina wrote: I use the Opera browser: http://www.opera.com Among other things (like having tabs for ages!): - enable/disable tables and divs (like you do) - enable/disable images with a keystroke, or only show cached images. - enable/disable CSS - banner supressing (aggressive) - enable/disable scripting - fit to page width (for those annoying sites that insist on using a fixed width of about 400 pixels, less than 1/3 of my actual screen size) - apply your custom CSS or javascript on any page - edit the page source and *refresh* the original page to reflect your changes All of this makes a very smooth web navigation - specially on a slow computer or slow connection. Thanks! I forgot about that one. It does what I want natively so I will go that route for now. Still I think there must be some use for my method of filtering. It's just too good to not have some use :-) Maybe in the future -when web pages will add new advertisement tactics faster than web browser builders can change their toolbox or instruct their users. After all, I was editing the filter script on one screen and another screen was using the new filter as soon as I had saved it. Maybe someday someone will write a GUI where one can click some radio buttons that would define what goes through and what not. Possibly such a filter could be collectively maintained on a live webpage with an update frequency of a few seconds or something. Just to make sure we're prepared for the worst :-) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: [JOB] Sr. Python Developer, Northern VA
Steven D. Arnold wrote: Neosynapse is seeking a senior software developer located in or Subtract ten points from your credibility for writing senior here. willing to relocate to the Northern VA area to join a project building one of the largest grid computing data platforms in the world. Skill and experience required for this engagement include: Another 10 points minus for claiming to be the largest (biggest, industry leader etc.). Are you looking for a developer or is this just an ad for your company? * at least 7 years experience in professional software development; Minus 20 points for using the term professional. Open source is cool. * at least 3 years of Python software development experience; OK. You can ask for that. As long as you're not blind when someone shows you some code. I mean some people can't see code until it's connected to some 'job'. * proven experience with design, implementation, debugging and documentation of large projects; Hmm. Why, may I ask? Can you even code? Who are you anyway? * past experience with the development of networking client and server software; You really seem to know what you want, eh? * experience using SQL databases in the development of software applications; Experience, experience, experience. Why don't you just re-hire the one you just fired because he wouldn't say yes to your absurd demands? I mean after he made a honest guesstimate of the time frame necessary for the job and you pinned him down to the minute? * excellent written and oral communication skills, including the ability to clearly explain design choices and discuss alternatives with colleagues and managers. Yeah. The following skills are desirable but not mandatory: * experience using SAN file systems; * experience with general web software development; * experience writing applications that leverage Oracle databases; * experience writing and integrating C modules with Python; * experience with Ruby or Ruby on Rails. * endure stupid boss messing with the creative process. You must be able to function independently as you will work from home a significant amount of time on this project. Limited travel to PA, NJ, and CO will be required. The initial phase of this project will last 6 months, with follow-on work planned for approximately 3 years. Says you. It could be anything from 3 months to ten years, depending on how often you change your mind. If interested and qualified for this position, please submit your detailed resume, contact and rate requirements to [EMAIL PROTECTED] with the subject line PY-5. Principals only. Go away with that resume Why do you think you have a right to my personal info when you're not giving any? By the way, I'm in Europe, there's more than just US you know. A. /rant -- http://mail.python.org/mailman/listinfo/python-list
Re: [JOB] Sr. Python Developer, Northern VA
Steve Holden wrote: /rant Feel better now? Yes! But *now* I'm afraid it will have negative consequences for my future employability. However if it will lead to adjusting the kind of submissions at http://www.python.org/community/jobs/ it was probably worth it. A. 'thanks for asking' -- http://mail.python.org/mailman/listinfo/python-list
Re: [JOB] Sr. Python Developer, Northern VA
[EMAIL PROTECTED] wrote: I think the steady increase in the number of active listings over the past couple years bodes well for the job prospects of Python programmers as a whole. There are currently 99 job postings on the job board dating back to mid-December. A year ago there were about 60, a year before that, about 40. Agreed. There's now probably even room on the job market for those who don't belong to the select elite of early adopters. We might even root out the professionalist attitude and address the problem of tying people to their resumes. A number of those companies seem to be fairly enlightened about the use of open source software (search for open). You can't expect the world to completely change overnight. If I never speak up there's little chance my ideas will get noticed. But there's probably some better format possible for my message. Most companies probably still funnel external job postings through their HR departments. As a result, a certain amount of boilerplate text and corporate puffery is bound to turn up in many postings. Be thankful they at least found the job board or comp.lang.python and aren't just trying to recruit through traditional channels. Actually, I like people posting their jobs on an unmoderated forum. How else would it be possible to try and get applicants and employers to compare their ideas? I mean, not everyone is able to visit expensive high-profile hiring fests like pycon. You'd probably never see job postings for the Space Telescope Science Institute or The World Wide Workshop for Children's Media Technology and Learning if they only appeared in the Washington Post or New York Times. Sure. But my whole problem is that while I'd be immensely useful in a space telescope science institute or a genome database research institute or in an artists educational institute or in a psychological statistics institute or in a computer science or mathematics institute etc. there is no way people can see that, because they're thinking in resumes, job experience and formal education instead of in just asking themselves what needs to be done and can he do it. You know, like duck typing :-) From what I get around the 'net I gather the problem is a bit less pronounced in the US though. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: [JOB] Sr. Python Developer, Northern VA
Michael Bentley wrote: Perhaps it is different where you live, but here you can put on your resume relevant things that aren't paying jobs. Otherwise nobody would ever get their first job, right? Sure you can. But around here if one has been unemployed for a while it's nearly impossible to get hired ever again. It doesn't matter how many years programming Python one puts on the resume. Unpaid activities are just not *visible*. So HRM people keep asking questions like 'But what have you been doing?' In the end I just started my own company, and while I'm not always employed as a freelancer I can at least now say I'm running my own business. The basic problem however is that it's just not anyones business whether one has been walking through the country making pictures or doing some desk job. All that should matter is can he do the job and is he motivated. Asking a person who he *is* (resume) is not Pythonic! Especially if one never reads the output. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: [JOB] Sr. Python Developer, Northern VA
John J. Lee wrote: You may not realise it if you haven't been applying for work since you did that, but I'm sure you've done a lot for your employability (I hate that word, it implies that it's a one-sided business, clearly false) by working as a freelancer. Since I'm freelancing my leverage has been a lot better and it has resulted in a few short term regular job contracts. Unfortunately there's also the new problem that when people learn that they can also employ me as a freelancer they might prefer that, since it gives them less risk, but then they don't want to pay me a freelance tariff :-( Your original rant seemed rather one-sided, though. Yes, I tried to let it slip that it was a rant. At least it has got some discussion going. The basic problem however is that it's just not anyones business whether one has been walking through the country making pictures or doing some desk job. All that should matter is can he do the job and is he motivated. Asking a person who he *is* (resume) is not Pythonic! I sympathise but conventional wisdom (which surely has a lot of truth in it) is that employers are not faced with the problem of minimising false negatives (failing to hire when they should have hired). They are faced with the problem of minimising false positives (hiring when they should not have hired). That's a gross simplification of course, but I'm sure you appreciate the point -- if you're hiring employees, being fairly risk-averse is probably quite rational. So when we really mean business, you're back to static typing? I've often thought that randomness is a good thing here though, globally -- a world of perfectly rational selfish employers would fail to employ legions of highly productive people. Luckily incompetence in hiring steps in often enough to save the day, in this best of all possible worlds 0.75 wink For me it's easy: I just can't stand handing over my resume anymore (that's because of severe psychological damage caused by the Dutch social security system), so whatever happens, I'm forced to find rational employers. And with rational I mean a dynamically typing employer! I'm the kind of employee object that when it gets typechecked it raises a ResumeError. (no hiring incompetence at the employer I'm just about to move to, it goes without saying :-) Well, on my next leisurely stroll along the river side, I will consciously enjoy the freedom of currently having no job instead of the usual I'm lucky I have no kids to worry about, just to humor you! A. -- http://mail.python.org/mailman/listinfo/python-list
Re: To count number of quadruplets with sum = 0
n00m wrote: 62.5030784639 Maybe this one could save a few seconds, it works best when there are multiple occurrences of the same value. A. from time import time def freq(L): D = {} for x in L: D[x] = D.get(x,0)+1 return D def test(): t = time() f = file('m4000.txt') f.readline() L = [] for line in f: L.append(map(int,line.split())) q,w,e,r = map(freq,zip(*L)) sch,h = 0,{} for xk,xv in q.iteritems(): for yk,yv in w.iteritems(): if h.has_key(xk+yk): h[xk+yk] += xv*yv else: h[xk+yk] = xv*yv for xk,xv in e.iteritems(): for yk,yv in r.iteritems(): if h.has_key(-(xk+yk)): sch += h[-(xk+yk)]*xv*yv print sch print time()-t if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Raymond Hettinger wrote: Since people are posting their solutions now (originally only hints were provided for the homework problem), here's mine: Homework problem? Do you have some information from the OP that I can't find in this thread? Anyway, I consider the 'homework' idea and the associated self concept that turns people into -for example- backstabbing computer scientists who never release their code as the root of all evil in the educational system. Why should I uphold a moral code that I don't agree with? To me people posting homework problems are like refugees which I will help if I can. def genpool(n, m): if n == 1: yield [m] else: for i in xrange(1, m): for rest in genpool(n-1, m-i): yield rest + [i] import random print random.choice(list(genpool(n=4, m=20))) OK back to the *computer* code. Great function! And it's ideally suited for memoization too. Too bad this memoizor doesn't accept keyword arguments, but for the rest it works fine and greatly speeds it up. import random def memoize(fn): cache = {} def proxy(*args): try: return cache[args] except KeyError: return cache.setdefault(args, fn(*args)) return proxy @memoize def genpool(n, m): if n == 1: yield [m] else: for i in xrange(1, m): for rest in genpool(n-1, m-i): yield rest + [i] def test(): print random.choice(list(genpool(5,50))) if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Paul Rubin wrote: def genpool(n, m): if n == 1: yield [m] else: for i in xrange(1, m): for rest in genpool(n-1, m-i): yield rest + [i] import random print random.choice(list(genpool(n=4, m=20))) This generates a lot of the partitions more than once, with possibly unequal probability. Well, I just noticed that with my memoization function it produces too little :-( But I hope you notice that this function doesn't create only partitions but all possible outcomes? A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Anton Vredegoor wrote: def memoize(fn): cache = {} def proxy(*args): try: return cache[args] except KeyError: return cache.setdefault(args, fn(*args)) return proxy Sorry this doesn't work in this case. This works: def memoize(fn): cache = {} def proxy(*args): try: return cache[args] except KeyError: return cache.setdefault(args, list(fn(*args))) return proxy But then we lose all speed advantages from memoizing so it's no good either. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Dick Moores wrote: If the added constraint is instead that the probability of generating a given list of length N be the same as that of generating any other list of length N, then I believe my function does the job. Of course, [1,46,1,1,1] and [1,1,46,1,1], as Python lists, are distinct. I ran this test for M == 8 and N == 4: Yes, I believe your function is OK. But the 'fencepost' method posted earlier in this thread also does this and it's faster AND it's only two line of code ... A = [] B = [] for x in range(10): lst = sumRndInt(8,4) if lst not in A: A.append(lst) B.append(1) else: i = A.index(lst) B[i] += 1 A.sort() Doesn't sorting break the correspondence between A and B? Also, a more pythonic way to count would be to convert the lst into a tuple and then do something with a dictionary. Dictionaries have faster lookup. For example: T = tuple(lst) D[T] = D.get(T,0) + 1 in the loop in order to count the occurrences. I'm totally fascinated with this stuff myself so it's a bit hard to guess whether someone still is interested, but anyway, here are some further explorations involving partitions. The code prints the number of distinct permutations for each partition. from operator import mul def part(m,n,r): L = [0]*n while m: x = pn(m-1,n-1) if r x: L[n-1] += 1 m -= 1 n -= 1 else: for i in range(n): L[i] += 1 r -= x m -= n return L def memoize(fn): cache = {} def proxy(*args): try: return cache[args] except KeyError: return cache.setdefault(args, fn(*args)) return proxy @memoize def pn(m,n): if mn or n==0: return 0 if m==n or n==1: return 1 return pn(m-1,n-1)+pn(m-n,n) @memoize def fac(n): if n == 1: return 1 return n*fac(n-1) @memoize def ncomb(n,k): return reduce(lambda a,b: a*(n-b)/(b+1),range(k),1) @memoize def _np(T): if T: up = fac(sum(T)) down = reduce(mul,map(fac,T)) return up/down else: return 1 def nperm(L): f = dict((x,L.count(x)) for x in set(L)) return _np(tuple(f.values())) def test(): m = 50 n = 5 np=pn(m,n) total = 0 for r in xrange(np): x = part(m,n,r) np = nperm(x) total += np print np,x assert total == ncomb(m-1,n-1) if __name__=='__main__': test() I posted some easy way of generating all outcomes by index -using a combinations function- before in this thread. Using just a single randint call one could output a random list of numbers adding up to something. But I think it would be possible to do it with distinct permutations of these partitions too. I think I did that in old thread. The only problem is that in order to look up which distinct permutation of which partition is indexed there should be a way to compute a table containing info about how many permutations a certain partition contributes to the total. But the code would then also need a distinct permutation by index function (I've got one but it's not exactly beautiful) while the code is becoming very complex even now without the extra lookup table, compared to the simple combinations code. And compared to the two line fencepost method it's just crazy :-) But I have a hunch there must be a way to generate such a table without actually computing any partitions at all and then one could do some kind of 3d bisect to look things up. Maybe in a few years, or sooner if someone posts an idea that simplifies things. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Dick Moores wrote: Paul Rubin's fencepost method is about 14 times faster than mine for the same M == 8 and N == 4! :( Actually they looked a bit similar after I had mucked a bit with them :-) But indeed it's slow. Sorry, I don't understand this. Could you spell it out for me by rewriting my above test to use it? Thanks! OK here you go. I'm copying a modification from something that I used to check something else, I hope you recognize the code after my refactoring. from itertools import islice import random def sumRndInt(m,n): while 1: L = [random.randint(1,m-n+1) for i in xrange(n)] if sum(L) == m: yield tuple(L) def fencepost(m,n): while 1: t = sorted(random.sample(xrange(1,m), n-1)) yield tuple((j-i) for i,j in zip([0]+t, t+[m])) def freq(g,k): D = {} for x in islice(g,k): D[x] = D.get(x,0)+1 return D def test(): m = 7 n = 4 k = 2 R = freq(sumRndInt(m,n),k) F = freq(fencepost(m,n),k) assert sorted(R) == sorted(F) for x in sorted(R): print x,R[x],F[x] if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Raymond Hettinger wrote: To make the solutions equi-probable, a simple approach is to recursively enumerate all possibilities and then choose one of them with random.choice(). Maybe it is possible to generate the possibilities by an indexing function and then use randint to pick one of them. I suppose this is like the bricks and bins problem this thread was about: http://groups.google.nl/group/comp.lang.python/browse_thread/thread/4782b54fa39b3bad Except that the bins now have at least 1 brick in them (if we have positive numbers). I posted a rather simplistic solution (but working I think) after Steven Taschuk made some insightful remarks. I believe it is possible to generate the list of numbers directly instead of permuting a list of '0' and '1' characters and then finding the positions of the '1' elements. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Terry Reedy wrote: Partitioning positive count m into n positive counts that sum to m is a standard combinatorial problem at least 300 years old. The number of such partitions, P(m,n) has no known exact formula but can be computed inductively rather easily. The partitions for m and n can be ranked in lexicographic order from 0 to P(m,n)-1. Given a rank r in that range, one can calculate the particular partition that has that rank. So a equi-probable random count in the range can be turned into a equi-probable random partition. Yes that was one of my first ideas too. But later on Steven pointed out that one can view the problem like this: 0001100010100 That would be [3,4,3,1,2] where the '1' elements are like dividing shutters that partition the row of '0'. This means that the problem is reduced to permutations (albeit unique permutations) which are a lot simpler to compute than partitions. Ok I'll admit that I succeeded in translating my 'Goldberg' solution to this case, I can't expect anyone to dust off and read 4 year old threads anyway :-) (by the way I'm still convinced that this code can be simplified a lot) def starters(L): n,np,R = len(L),1,range(len(L)) bf = [L[:i].count(L[i]) for i in R] for i in R: np = np*(n-i)/(bf[i]+1) return [(i,np*L[i:].count(L[i])/n) for i in R if not bf[i]] def perm(index,L): remain,n = index,len(L) res,T = L[:],L[:] for i in range(n): for j,k in starters(T): if remain-k 0: res[i] = T.pop(j) break remain -= k return res def nperm(L): return reduce(lambda a,b:a+b,[k for j,k in starters(L)]) def bb(i,bricks,bins): L = [1] * (bins-1) + [0] * (bins-1) R = [1] for x in perm(i,L): if x: R.append(1) else: R[-1]+=1 return R def test(): bricks,bins = 7, 4 L = [1] * (bins-1) + [0] * (bins-1) for i in range(nperm(L)): print bb(i,bricks,bins) if __name__=='__main__': test() This topic is section 3.1 in Combinatorial Algorithms: Generation, Enumeration, and Search by Kreher and Stinson. The authors reference several other books as their sources. Great book! I plan to someday rewrite many of their pseudocode algorithms in Python. That would be my dream job. If only I understood more of them. But I'm slowly making progress in other areas so that one day I will maybe reread the book and also understand the second half. A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Anton Vredegoor wrote: L = [1] * (bins-1) + [0] * (bins-1) replace these lines in the code by: L = [1] * (bins-1) + [0] * (bricks-bins) A. -- http://mail.python.org/mailman/listinfo/python-list
Re: number generator
Terry Reedy wrote: Anton Vredegoor [EMAIL PROTECTED] wrote in message | Yes that was one of my first ideas too. But later on Steven pointed out | that one can view the problem like this: | | 0001100010100 | | That would be [3,4,3,1,2] | | where the '1' elements are like dividing shutters that partition the row | of '0'. This means that the problem is reduced to permutations (albeit If any of the 1s appear at the ends or together, then you would have 0s in the partition, which is not allowed, as I understood the spec. Yes, I was writing about the bricks and bins problem from 4 years ago which is very similar. | unique permutations) which are a lot simpler to compute than partitions. I think the simplicity is actually about the same. Probably yes. It's like the difference between Pascal's triangle and the partition numbers' triangle. Anyway, Paul Rubin's idea in this same thread stimulated me to simplify my code a lot. It's rather late here so I hope I haven't slipped up again. def comb(i,n,k): for j in range(k,0,-1): while noverk(n,j) i : n -= 1 i -= noverk(n,j) yield n def noverk(n,k): return reduce(lambda a,b: a*(n-b)/(b+1),range(k),1) def bb(i,bricks,bins): L = [j+1 for j in comb(i,bricks,bins-1)] return [(i-j) for i,j in zip([bricks]+L,L+[0])] def test(): bricks, bins = 6,4 n = noverk(bricks-1,bins-1) for i in range(n): print bb(i,bricks,bins) if __name__=='__main__': test() A. -- http://mail.python.org/mailman/listinfo/python-list
Re: permutations - fast with low memory consumption?
Gerard Flanagan wrote: No claims with respect to speed, but the kslice function here: http://gflanagan.net/site/python/utils/sequtils/ will give the 'k-subsets' which then need to be permuted - alternatively Google. Maybe the function below could then do these permutations. Anton. def _sorted(seq): Return a sorted copy of seq, preserving the type. res = seq[0:0] decorated = ((x,i) for i,x in enumerate(seq)) for x,i in sorted(decorated): res += seq[i:i+1] return res def _swap_and_reverse_tail(R,i,j): Swap R[i] and R[j], reverse R[i+1:]. Returns a copy, preserving the type. a,b,c,d,e = R[:i],R[i:i+1],R[i+1:j],R[j:j+1],R[j+1:] return a+d+(c+b+e)[::-1] def permutations(seq): Generate sorted permutations of any sequence that can be indexed and sliced, preserving the type. e.g. seq can be a string, list, tuple or array. n = len(seq) if n == 1: yield seq[:] elif n = 2: R = _sorted(seq) while True: yield R i,j = n-2,n-1 while R[i] = R[i+1] : i -= 1 if i == -1: return while R[i] = R[j]: j -= 1 R = _swap_and_reverse_tail(R,i,j) def test(): seq = 'gkDr217sKGMNLPsrtqeiczxyq' P = permutations(seq) for i,x in enumerate(P): print '%s' %(x) if i == 10: break if __name__ == '__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: proof of concept python and tkinter gnugo interface
grindel wrote: Anton Vredegoor wrote: [...] Here's the proof of concept, just copy it to some dir and run the Python script: http://home.hccnet.nl/a.vredegoor/gnugo/ It needs Python 2.5 which you can get at: http://www.python.org/ If you talking about a simple gui for gnu go it's been done to death. see any misc. pandanet client and numerous other softwares such as durish or go knot. If your client is going to do something uniquely different from programs like this then you should focus in this aspect of the program and develop other features later. It's also important that it be able to read and write sgf files It's uniquely different from numerous other softwares in that it is completely open source and runs from a standard Python installation (but of course one has to get a Gnugo executable from somewhere). I also implemented reading and writing SGF files and browsing through the move history now. Total command set at the moment: -click on a coordinate: generate a move there and go out of replay mode -space bar: computer makes a move and goes out of replay mode -up key: save an SGF file (only the moves) -down key: read an SGF file -left key: go into replay mode and undo a move -right key: show next move, if at end goes out of replay mode Remember we're still only talking about a proof of concept script! I just want to attract people, and put this script into an SVN somewhere. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I access a main frunction from an import module?
Jim wrote: I have created an import module. And would like to access a function from the main script, e.g., file abc.py: ### def a(): m() return None file main.py: # from abc import * def m(): print 'something' return None a() import sys def a(): sys.modules['__main__'].m() return None Anton 'now why would anyone want to do *that* ?' -- http://mail.python.org/mailman/listinfo/python-list
proof of concept python and tkinter gnugo interface
For the last few days I've been doodling with a script that provides a graphical interface to gnugo by using its GTP protocol. At the moment the script is *very* basic, in fact the only thing it does is to allow one to click on a coordinate and place a move there OR press the space bar in order to let gnugo generate a move. However, I feel that this idea has some potential, it could be made to undo or redo moves or load sgf-games. But most importantly: It could load the list of move suggestions from gnugo, do some computations itself in *Python* on that list and then generate a move. So I thought that it would be best to present this script while it is still small and modifications can be done easily. In the end there's possibly a lot of potential and I think I'm going to need some help from people that are enthousiastic about Python or gnugo. What I want to accomplish with this post is to get to know whether it would be a good idea to make it a sourceforge project (it would be even better if some more experienced sourceforger would do it for me :-) or whether I should just go on doodling on this script by myself, gradually adapting it to fit my personal interests and come back to you in a few years with a more complete script. Is there any need for such a beast for *more* people than just me to work on? Here's the proof of concept, just copy it to some dir and run the Python script: http://home.hccnet.nl/a.vredegoor/gnugo/ It needs Python 2.5 which you can get at: http://www.python.org/ Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: code is data
Paul Boddie wrote: Anton Vredegoor wrote: Yes, but also what some other posters mentioned, making Pythons internal parsing tree available to other programs (and to Python itself) by using a widely used standard like XML as its datatype. http://pysch.sourceforge.net/ast.html Very interesting, it led me to some sxml describing pages and it also tricked me into reading some Python documentation that I had always considered to be hiding some arcane deep Python magic. I guess now that Python is officially entering tree territory (as opposed to providing third party functionality) it seems unavoidable that Python's officially endorsed tree datatype will also be used for some of its internal structures, thereby making it more accessible to programmers like me and to outside programmers. I was going to write a long reply to one of your previous messages, but the above link references a project which may intersect with some of your expectations. Meanwhile, it should be noted that the availability Somehow I get the impression of getting away with my posts luckily, while you now are duping other interested readers into not reading your innermost feelings about this subject. Let's get it in the open, don't spare me :-) of Python AST processing tools is not a recent thing: the compiler module has been around for a long time, and it is possible to modify the AST and to generate bytecode from it; my own experiments have centred on producing other representations from the AST, and other more successful projects (eg. ShedSkin) produce other languages (eg. C++) from the AST. Well maybe this trick of vehemently denying the existence of something on Usenet worked again, by materializing the thing as a reaction. However, I knew of the existence of such languages but I am mostly interested in standardized code interchange, like for example with JSONP which fetches some external javascriptcode from another server using JSON and places the translated javascript into a webpage at the request of the clients browser or so it seems. Maybe a Python webserver could also emit pieces of javascript code by getting them from a *Python* code library after translating Python code on the fly? That would open up the web to Python programmers without browsers needing to understand Python. Like Jython, but now as separately distributed functions from different servers. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: How to generate all permutations of a string?
Girish Sahani wrote: I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496724 anton -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] code is data
Bruno Desthuilliers wrote: You mean like 'converting' javascript to python or python to ruby (or converting any home-grown DSL to Python, etc) ? Yes, but also what some other posters mentioned, making Pythons internal parsing tree available to other programs (and to Python itself) by using a widely used standard like XML as its datatype. Then there are some people who keep insisting they don't understand what I'm talking about until I simplify things enough to get them on-board, count me in then :( Sorry about that. but then simply dismiss my ideas with 'you can already do that easily with this standard python construct'. This strategy was also eloquently refuted by some other poster, so I don't need to repeat it :-) I've gotten a lot of things to think about, so thanks all for your thoughts, but since this is getting way above my head I'll just wimp out and leave the rest of the thread to the experts! No way you will escape from your responsabilities so easily !-) Ok, count me back in then too :-) Of course I will be available for further discussion. If more than ten people demand a PEP and no better champion is available (very unlikely) I'll even write a proposal. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] code is data
Diez B. Roggisch wrote: ... The whole point of a code transformation mechanism like the one Anton is talking about is to be dynamic. Else one just needs a preprocessor... No, it is not the whole point. The point is The idea is that we now have a fast parser (ElementTree) with a reasonable 'API' and a data type (XML or JSON) that can be used as an intermediate form to store parsing trees. Especially statically typed little languages seem to be very swallow-able. Maybe I will be able to reimplement GFABasic (my first love computer language, although not my first relationship) someday, just for fun. No on-the-fly code generation here. He essentially wants lisp-style-macros with better parsing. Still a programming language. Not a data-monger. The 'problem' is that a lot of incredibly smart people are reading and replying here who are seeing a lot more into my post than I was prepared for :-) Anyway, the last few weeks I have been busy transforming MsWord documents into XML using Open Office, and next parsing this XML and transforming it into a special subset of HTML using ElementTree's XMLWriter class. Then the output of the XMLWriter was put into a Zope/Plone page but I added special markup for footnotes, making them plone objects that could be separately edited, and I added image tags for images that were retrieved from a separate server using an XSLT script. To accomplish that a special zope parser was written to recognize my nonstandard footnote and image tags, and to create the necessary objects, and to insert them into the page. After that I came across some turbogears code (which is stacking code at different levels like it were those things you put under your beer glass) and still later I saw some JSON equivalents of XML. JSON looks a lot like Python dicts which makes it seem likely that javascript will be able to interface with Python more efficiently. Remember that ElementTree comes from the same place that brought us PIL which is a package that can transform images into different types. So if we can transform documents, images and XML, why not sourcecode? Especially if it's not a conversion into a 'lossy' file format, (I consider dynamically typed code versus statically typed code the analog thing to JPEG versus bitmaps) it would be easy to convert all datatypes into the datatypes of another language, thereby making it possible to exchange code between languages. Algorithms just being things that convert sets of data-objects into other sets of data-objects. Now if one would equate standardized code exchange between languages and within a language with macros then I guess there is nothing left for me to do but wait till a certain google bot comes knocking at my ip-address port 80 and transfers me to the google equivalent of Guantanamo. But the whole point of distinguishing macros from official language structures *is* standardization, as some other clever poster already pointed out, so it would be extremely unfair to equate trans-language standardized code exchange with the guerrilla type macro activities that are plaguing the Lisp community. Then there are some people who keep insisting they don't understand what I'm talking about until I simplify things enough to get them on-board, but then simply dismiss my ideas with 'you can already do that easily with this standard python construct'. This strategy was also eloquently refuted by some other poster, so I don't need to repeat it :-) I've gotten a lot of things to think about, so thanks all for your thoughts, but since this is getting way above my head I'll just wimp out and leave the rest of the thread to the experts! Regards, Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] code is data
bruno at modulix wrote: I still don't get the point. Well, I've got to be careful here, lest I'd be associated with the terr.., eh, the childp..., eh the macro-enablers. The idea is to have a way to transform a Python (.py) module into XML and then do source code manipulations in XML-space using ElementTree. But rest assured, there is no such module, nor will we ever need it for anything. Anton use cases are for the faint-hearted -- http://mail.python.org/mailman/listinfo/python-list
[OT] code is data
With the inclusion of ElementTree (an XML-parser) in Python25 and recent developments concerning JSON (a very Pythonesque but somewhat limited XML notation scheme, let's call it statically typed XML) Python seems to have reached a stage where it now seems to be possible to completely swallow lesser languages code, modify it, and spit out new source code targeting the original language the code was written in, or even make a translation to other languages. The idea is that we now have a fast parser (ElementTree) with a reasonable 'API' and a data type (XML or JSON) that can be used as an intermediate form to store parsing trees. Especially statically typed little languages seem to be very swallow-able. Maybe I will be able to reimplement GFABasic (my first love computer language, although not my first relationship) someday, just for fun. Then there are things like cTypes (calling functions from native DLL's) and PyPy (implementing Python in Python). All this taken together, to me it starts looking like we're now entering a territory that traditionally was exclusively in the Lisp domain. Yes, Python had eval and exec for a long time already, and metatypes and generators are having some strange unexplored possibilities too, but the day will come soon (and at last when PyPy is reaching execution speeds close to cPython) where Python will be able to swallow smaller languages, and finally it will be able to swallow its own tail, like Lisp but then more powerful (because of the widely used standard data types and the code exchange between languages that that makes possible). Your thoughts please. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: An oddity in list comparison and element assignment
Alex Martelli wrote: [EMAIL PROTECTED] wrote: Can somebody please shut down this bot? I think it's running out of Much as you might love for somebody to shut me down, that (unfortunately, no doubt, from your viewpoint) is quite unlikely to happen. Although making predictions is always difficult, especially about the future, the most likely course of events is that I shall continue for a while to survive, probably in tolerable health. You've got that completely wrong. I was not trying to kill you but I was trying to revive you. A process that is not evolving is dead. Stopping it frees up valuable resources that enable it to become alive again. Anton 'being alive is being mutable' -- http://mail.python.org/mailman/listinfo/python-list
Re: An oddity in list comparison and element assignment
Alex Martelli wrote: [snip] Can somebody please shut down this bot? I think it's running out of control. It seems to be unable to understand that don't be evil might be good when you're small (at least it's not very bad) but that it becomes distinctly evil when you're big. What is good when you're big? I really don't know and I think there's even not many other people who do. But simply forbidding things that are not precisely definable -the way mathematicians have been doing before physicists shook them out of it- seems to do more harm than good. In my opinion it looks like there is a path from rigid rule adherence to slowly accepting more doubt and inconsistencies -because we're all adults here- and this has something to do with letting go of things like childish adherence to static typing and confusion between equality and identity. Let me qualify that last paragraph before anyone concludes I have become disfunctional too and will lead everyone to their destruction. There seem to always be certain unclear parts in a programming language and people are constantly trying out new structures in order to map some new territory. I remember sets, generators and metaclasses. Only after people noticing problems (don't modify the thing you're iterating over) ways are found to solve them (you can if you put everything back just at the right moment) and finally these ways are condensed into officially endorsed coding practices. Now we're struggling with immutability and sequences. They're not a problem if you know what you're doing, but what exactly is it that those who know what they're doing do? It indicates that maybe it's the birth of a new language construct. But why should it stop there? I expect a certain openness and willingness to discuss controversial matters from a community even if it were only to educate newcomers. But it might be the case that such willingness to accept doubt, without it turning into actively seeking it -that seems to be foolish, but who am I to judge even that- is what makes it possible to develop higher language and cognitive structures. Anton 'even if it means turning into lisp before moving on' -- http://mail.python.org/mailman/listinfo/python-list
Re: integer to binary...
[EMAIL PROTECTED] wrote: does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... def bits(i,n): return tuple((0,1)[ij 1] for j in xrange(n-1,-1,-1)) bits(7,4) (0, 1, 1, 1) Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: A critic of Guido's blog on Python's lambda
[EMAIL PROTECTED] wrote: When you consider that there was just a big flamewar on comp.lang.lisp about the lack of standard mechanisms for both threading and sockets in Common Lisp (with the lispers arguing that it wasn't needed) I find it curious that someone can say Common Lisp scales well. In comp.lang.python there are often discussions about which is the best web framework or what is the best gui. There seems to be some common meme in these kinds of discussions and the lambda controversy. I'm even ready to expand the concept even more and include documentation problems and polymorphic typing. So what is the big advantage of using parens then that is making people give up documenting their code by naming functions? (See, I'm getting into the right kind of lingo for discussing these kind of questions) Well, there seems to be some advantage to conceptually decoupling a function from what it is doing *now* (which can be easily named) and what it is doing in some other situation. Naming things is only a ballast and makes the mental model not fit the brain (introducing pythonic terminology here for the lispers). This is a lot like polymorphic functions. For example an adding function sometimes adds integers and sometimes floats or complex variables and it can be defined just once without specifying which type of parameters it is going to get. I assume this to be a piece of cake for most lispers and pythoneers, but possibly this could still confuse some static typers. An anonymous function is like a polymorphic function in that it is possible to make the mental model about it polymorphic, instead of just its parameters. This enables the lispers to just take what it does and paste it where that needs to be done (inventing crypto speak here). This is a very effective way of handling operations and it would surprise me if not 99 percent of the Python coders do things mentally this way too and only add names and documentation at the last possible moment (mental compile time documentation procedure). So here we're integrating mental models concerning polymorphism into the way we talk and think about code, and naming things explicitly always seems to be a burden. But now we let the other side of our brain speak for a moment, it was always the side that translated everything we wanted to say to each other here into mental Unicode so that we can hear what the others are saying (further diving into the linguistic pit I am digging here). Yes, communication is what suffers from *not* naming things, and right after it documentation and standardization. How else are we going to communicate our findings verbally to the non coders and the trans coders? Also naming functions and variables can help us create appropriate mental models that 'fix' certain things in place and keep them in the same state, because now they are 'documented'. This promotes people being able to work together and also it enables measuring progress, very important aspects for old world companies who won't understand the way things are evolving (even if they seem to have roaring success at the moment). Not to say that I invented something new, it was always a theme, but now it's a meme,(he, he), the conflict between the scripture and the mysticism. It's such a pity that everyone understands some way or another that mysticism is the way things work but that none wants to acknowledge it. What am I doing here coding Python one might ask, well, the knowledge has to be transfered to my brain first *somehow*, and until someone finds a better way to do that or until there is so much procedural information in my head that I can start autocoding (oh no) that seems to be the better option. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Martin v. Löwis wrote: Well, if the document is UTF-8, you should decode it as UTF-8, of course. Thanks. This and: http://en.wikipedia.org/wiki/UTF-8 solved my problem with understanding the encoding. Anton proof that I understand it now (please anyone, prove me wrong if you can): from zipfile import ZipFile, ZIP_DEFLATED def by80(seq): it = iter(seq) while it: yield ''.join(it.next() for i in range(80)) def utfCheck(infn): zin = ZipFile(infn, 'r', ZIP_DEFLATED) data = zin.read('content.xml').decode('utf-8') for line in by80(data): print line.encode('1252') def test(): infn = xxx.sxw utfCheck(infn) if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Serge Orlov wrote: I extracted content.xml from a test file and the header is: ?xml version=1.0 encoding=UTF-8? So any xml library should handle it just fine, without you trying to guess the encoding. Yes my header also says UTF-8. However some kind person send me an e-mail stating that since I am getting \x94 and such output when using repr (even if str is giving correct output) there could be some problem with the XML-file not being completely UTF-8. Or is there some other reason I'm getting these \x94 codes? Or maybe this is just as it should be and there's no problem at all? Again? Anton 'octopussies respond only off-list' -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Richard Brodie wrote: Anton Vredegoor [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Yes my header also says UTF-8. However some kind person send me an e-mail stating that since I am getting \x94 and such output when using repr (even if str is giving correct output) there could be some problem with the XML-file not being completely UTF-8. Or is there some other reason I'm getting these \x94 codes? Well that rather depends on what you are doing. If you take utf-8, decode it to Unicode, then re-encode it as cp1252 you'll possibly get \x94. OTOH, if you see '\x94' in a Unicode string, something is wrong somewhere. Well, I mailed the content.xml to someone as a text attachment and it was damaged at the other end, whereas sending it as a file resulted in flawless transfer. So I guess there is something not quite UTF-8 in it. However Firefox has no problem opening it either here or at the other persons computer (the undamaged file of course). By the way, I also sent an MSwWord document (not as text) that I edited using OO back to the same person who is using MsWord and he is at the moment still recovering from an MSWord crash. Could it have something to do with the OO document being half as big as the MsWord Doc :-) Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Serge Orlov wrote: Anton Vredegoor wrote: In fact there are a lot of printable things that haven't got a text attribute, for example some items with tag ()s. In my sample file I see text:s text:c=2/, is that you're talking about? Since my file is small I can say for sure this tag represents two space characters. Or for example in firefox: text:s/ in Amsterdam text:s/ So, probably yes. If it doesn't have a text attribrute if you iterate over it using OOopy for example: o = OOoPy (infile = fname) c = o.read ('content.xml') for x in c.getiterator(): if x.text: Then we know for sure you have recreated my other problem. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Anton Vredegoor wrote: So, probably yes. If it doesn't have a text attribrute if you iterate over it using OOopy for example: Sorry about that, I meant if the text attribute is None, but there *is* some text. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Martin v. Löwis wrote: So if that is the case: What is the problem then? If you interpret the document as cp1252, and it contains \x93 and \x94, what is it that you don't like about that? In yet other words: what actions are you performing, what are the results you expect to get, and what are the results that you actually get? Well, where do these cp1252 codes come from? The xml-file claims it's utf-8. I just tried out some random decodings and cp1252 seemed to work. I don't like to have to guess this way. I think John wouldn't even allow it :-) Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
John Machin wrote: Firstly, this should be 'content.xml', not 'contents.xml'. Right, the code doesn't do *anything* :-( Thanks for pointing that out. At least it doesn't do much harm either :-| Secondly, as pointed out by Sergei, the data is encoded by OOo as UTF-8 e.g. what is '\x94' in cp1252 is \u201d which is '\xe2\x80\x9d' in UTF-8. The kill_gremlins function is intended to fix Unicode strings that have been obtained by decoding 8-bit strings using 'latin1' instead of 'cp1252'. When you pump '\xe2\x80\x9c' through the kill_gremlins function, it changes the \x80 to a Euro symbol, and leaves the other two alone. Because the \x9d is not defined in cp1252, it then causes your code to die in a hole when you attempt to encode it as cp1252: UnicodeEncodeError: 'charmap' codec can't encode character u'\x9d' in position 1761: character maps to undefined Yeah, converting to cp1252 was all that was necessary, like Sergei wrote. I don't see how this code repairs anything (quite the contrary!), unless there's some side effect of just read/writestr. Enlightenment, please. You're quite right. I'm extremely embarrassed now. What's left for me is just to explain how it got this bad. First I noticed that by extracting from content.xml using OOopy's getiterator function, some \x94 codes were left inside the document. But that was an *artifact*, because if one prints something using s.__repr__() as is used for example when printing a list of strings (duh) the output is not the same as when one prints with 'print s'. I guess what is called then is str(s). Ok, now we have that out of the way, I hope. So I immediately posted a message about conversion errors, assuming something in the open office xml file was not quite 1252. In fact it wasn't, it was UTF-8 like Sergei wrote, but it was easy to convert it to cp1252, no problem. Then I also noticed that not all xml-tags were printed if I just iterated the xml-tree and filtered out only those elements with a text attribute, like 'if x.text: print x' In fact there are a lot of printable things that haven't got a text attribute, for example some items with tag ()s. When F pointed me to gremlins there was on this page the following text: quote Some applications add CP1252 (Windows, Western Europe) characters to documents marked up as ISO 8859-1 (Latin 1) or other encodings. These characters are not valid ISO-8859-1 characters, and may cause all sorts of problems in processing and display applications. /quote I concluded that these \x94 codes (which I didn't know about them being a figment of my representation yet) were responsible for my iterator skipping over some text elements, but in fact the iterator skipped them because they had no text attribute even though they were somehow containing text. Now add my natural tendency to see that what I think is the case rather than neutrally observing the world as it is into the mix and of course I saw the \x94 disappear (but that was because I now was printing them straight and not indirectly as elements of a list) and also I thought that now the xml-parsing 'errors' had disappeared but that was just because I saw some text element appear that I thought I hadn't seen before (but in fact it was there all the time). One man's enlightenment sometimes is another's embarrassment, or so it seems. Thanks to you all clearing up my perceptions, and sorry about all the confusion I created. What I want to know next is how to access and print the elements that contain text but have no text attribute, that is, if it's not to taxing on my badly damaged ego. Anton -- http://mail.python.org/mailman/listinfo/python-list
not quite 1252
I'm trying to import text from an open office document (save as .sxw and read the data from content.xml inside the sxw-archive using elementtree and such tools). The encoding that gives me the least problems seems to be cp1252, however it's not completely perfect because there are still characters in it like \93 or \94. Has anyone handled this before? I'd rather not reinvent the wheel and start translating strings 'by hand'. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Fredrik Lundh wrote: Anton Vredegoor wrote: I'm trying to import text from an open office document (save as .sxw and read the data from content.xml inside the sxw-archive using elementtree and such tools). The encoding that gives me the least problems seems to be cp1252, however it's not completely perfect because there are still characters in it like \93 or \94. Has anyone handled this before? this might help: http://effbot.org/zone/unicode-gremlins.htm Thanks a lot! The code below not only made the strange chars go away, but it also fixed the xml-parsing errors ... Maybe it's useful to someone else too, use at own risk though. Anton from gremlins import kill_gremlins from zipfile import ZipFile, ZIP_DEFLATED def repair(infn,outfn): zin = ZipFile(infn, 'r', ZIP_DEFLATED) zout = ZipFile(outfn, 'w', ZIP_DEFLATED) for x in zin.namelist(): data = zin.read(x) if x == 'contents.xml': zout.writestr(x,kill_gremlins(data).encode('cp1252')) else: zout.writestr(x,data) zout.close() def test(): infn = .sxw outfn = 'dg.sxw' repair(infn,outfn) if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: not quite 1252
Martin v. Löwis wrote: Not sure I understand the question. If you process data in cp1252, then \x94 and \x94 are legal characters, and the Python codec should support them just fine. Tell that to the guys from open-office. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for resources for making the jump from Java to Python easier and more productive
ToddLMorgan wrote: I'm just starting out with python, after having a long history with Java. I was wondering if there were any resources or tips from anyone out there in Python-land that can help me make the transition as successfully as possible? Perhaps you've made the transition yourself or just have experience with folks who have made the transition. Some time ago I had to learn a bit of Java in order to be able to write some signed jython browser applets that I could use while working at a highly restricted public library computer. So I guess I was coming from the opposite direction :-) Anyway I wondered why (and how!) very small jython scripts could replace large blocks of java code. Maybe looking at it from a jython perspective will be educational for you. I think it would be a lot of fun realizing how much java code can actually be 'automatically' generated or filled in by jython traversing its class system. Also I'm hoping it would produce an opportunity for an advanced java coder to write some cool signed java applet in jython that would have the look and feel of idle.py for python but that would run from a webbrowser. I know it can be done because I did write some experimental but still already very functional things like a jython console webbrowser applet, a websucker.py running from within a browser, and I hacked some other jython editor-console (eclipse is good for such things) to add some functionality and do my own signing and library selection. Don't ask for my code yet though, it's nowhere near presentable. Anyway, since then I found a job that gives me access to less locked down computers which is also fun. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: two generators working in tandem
Michael Spencer wrote: This returns an iterator that 'nests' an arbitrary number of sequences (odometer-style). def nest(*sequences): def _nest(outer, inner): for outer_item in outer: if not isinstance(outer_item, tuple): outer_item = (outer_item,) for inner_item in inner: yield outer_item + (inner_item,) return reduce(_nest, sequences) Nice! Here's a nonrecursive version. It creates a list of iterators that are repeating their values just enough times to sychronize the nesting. I wonder if 'ncycle' would be a useful generalization for itertools' 'cycle' function. Anton def ncycle(seq,n): while True: for x in seq: for dummy in xrange(n): yield x def cross(*args): p = 1 R = [] for arg in args[::-1]: L = list(arg) R.append(ncycle(L,p)) p *= len(L) R.reverse() for dummy in xrange(p): yield [x.next() for x in R] def test(): s1='a1','a2','a3','a4' s2='b1','b2' s3='c1','c2','c3' for x in cross(s1,s2,s3): print x if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast generation of permutations
Paul Rubin wrote: Cool, I'd still like to know why (13**5)-13 = C(52,5) other than by just doing the arithmetic and comparing the results. Maybe your tkinter script can show that. That seems to be very hard :-) Unless I'm missing something. Anton def noverk(n,k): return reduce(lambda a,b: a*(n-b)/(b+1),range(k),1) print noverk(52,5) print 13**5-13 #prints: 2598960 371280 -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast generation of permutations
Anton Vredegoor wrote: Paul Rubin wrote: Cool, I'd still like to know why (13**5)-13 = C(52,5) other than by just doing the arithmetic and comparing the results. Maybe your tkinter script can show that. That seems to be very hard :-) Unless I'm missing something. Like a factor seven, you mentioned that a few posts back. Sorry about that. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: Intro to Pyparsing Article at ONLamp
Paul McGuire wrote: There are two types of parsers: design-driven and data-driven. With design-driven parsing, you start with a BNF that defines your language or data format, and then construct the corresponding grammar parser. As the design evolves and expands (new features, keywords, additional options), the parser has to be adjusted to keep up. With data-driven parsing, you are starting with data to be parsed, and you have to discern the patterns that structure this data. Data-driven parsing usually shows this exact phenomenon that you describe, that new structures that were not seen or recognized before arrive in new data files, and the parser breaks. There are a number of steps you can take to make your parser less fragile in the face of uncertain data inputs: - using results names to access parsed tokens, instead of relying on simple position within an array of tokens - anticipating features that are not shown in the input data, but that are known to be supported (for example, the grammar expressions returned by pyparsing's makeHTMLTags method support arbitrary HTML attributes - this creates a more robust parser than simply coding a parser or regexp to match 'A HREF=' + quotedString) - accepting case-insensitive inputs - accepting whitespace between adjacent tokens, but not requiring it - pyparsing already does this for you I'd like to add another parser type, lets call this a natural language parser type. Here we have to quickly adapt to human typing errors or problems with the tranmission channel. I think videotext pages offer both kinds of challenges, so could provide good training material. Of course in such circumstances it seems to be hardly possible for a computer alone to produce correct parsing. Sometimes I even have to start up a chess program to inspect a game after parsing it into a pgn file and correct unlikely or impossible move sequences. So since we're now into human assisted parsing anyway, the most gain would be made in further inproving the user interface? For example, I had this experience when parsing chess games from videotext pages I grab from my videotext enabled TV capture card. Maybe once or twice in a year there's a chess page with games on videotext, but videotext chess display format always changes slightly in the meantime so I have to adapt my script. For such things I've switched back to 'hand' coding because it seems to be more flexible. Do these chess games display in PGN format (for instance, 15. Bg5 Rf8 16. a3 Bd5 17. Re1+ Nde5)? The examples directory that comes with pyparsing includes a PGN parser (submitted by Alberto Santini). Ah, now I remember, I think this was what got me started on pyparsing some time ago. The dutch videotext pages are online too (and there's a game today): http://teletekst.nos.nl/tekst/683-01.html But as I said there can be transmission errors and human errors. And the dutch notation is used, for example a L is a B, a P is a K, D is Q, T is R. I'd be interested in a parser that could make inferences about chess games and use it to correct these pages! What I would like to see, in order to improve on this situation is a graphical (tkinter) editor-highlighter in which it would be possible to select blocks of text from an (example) page and 'name' this block of text and select a grammar which it complies with, in order to assign a role to it later. That would be the perfect companion to pyparsing. At the moment I don't even know if such a thing would be feasible... There are some commercial parser generator products that work exactly this way, so I'm sure it's feasible. Yes, this would be a huge enabler for creating grammars. And pave the way for a natural language parser. Maybe there's even some (sketchy) path now to link computer languages and natural languages. In my mind Python has always been closer to human languages than other programming languages. From what I learned about it, language recognition is the easy part, language production is what is hard. But even the easy part has a long way to go, and since we're also using a *visual* interface for something that in the end originates from sound sequences (even what I type here is essentially a representation of a verbal report) we have ultimately a difficult switch back to auditory parsing ahead of us. But in the meantime the tools produced (even if only for text parsing) are already useful and entertaining. Keep up the good work. Anton. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast generation of permutations
Paul Rubin wrote: def deals(): for i in xrange(13**5): cards = [(i//p) % 13 for p in (1, 13, 169, 2197, 28561)] yield cards This gives hands like [0,0,0,0,1] and [0,0,0,1,0] which are permutations of one another. Below is a piece of code that avoids this. Here's how to interprete its output. Suppose one gets a hand like [0,1,2,3,4]. This means that it would be possible to create 1024 (4**5) poker hands from this, by coloring the cards. Another hand, for example [0,0,1,2,3], would allow only 384 colorings, because the two zeros would contribute choose 2 out of 4 (colors), so 6 colorings. The other numbers remain available for 4 colorings, which gives 6*4**3 (==384) colorings for this hand. Similar things happen for other partionings of the hands into numbers. In fact I am now investigating a description based on integer partitions of the number 5. I am not sure whether I want to partition based on colors or on numbers, that seems to arbitrary. This is very fascinating. Maybe someday I'll make a tkinter script with a visual tree structure allowing all kinds of numbers of cards, and arbitrary numbers of variables to partition by. This would then give result sets like those strange quantum particles, such as quarks. Have fun, Anton def hands(L = [0]): if len(L) == 6: if L[1] != L[-1]: #no five of a kind yield L[1:] else: for i in range(L[-1],13): for H in hands(L+[i]): yield H def pprint(i,hand): print '%5i: ' %i, for x in hand: print '%2i ' % x, print def test(): H = hands() total = 0 for i,x in enumerate(H): pprint(i,x) if __name__=='__main__': test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Intro to Pyparsing Article at ONLamp
Paul McGuire wrote: I just published my first article on ONLamp, a beginner's walkthrough for pyparsing. Please check it out at http://www.onlamp.com/pub/a/python/2006/01/26/pyparsing.html, and be sure to post any questions or comments. I like your article and pyparsing. But since you ask for comments I'll give some. For unchanging datafile formats pyparsing seems to be OK. But for highly volatile data like videotext pages or maybe some html tables one often has the experience of failure after investing some time in writing a grammar because the dataformats seem to change between the times one uses the script. For example, I had this experience when parsing chess games from videotext pages I grab from my videotext enabled TV capture card. Maybe once or twice in a year there's a chess page with games on videotext, but videotext chess display format always changes slightly in the meantime so I have to adapt my script. For such things I've switched back to 'hand' coding because it seems to be more flexible. (Or use a live internet connection to view the game instead of parsing videotext, but that's a lot less fun, and I don't have internet in some places.) What I would like to see, in order to improve on this situation is a graphical (tkinter) editor-highlighter in which it would be possible to select blocks of text from an (example) page and 'name' this block of text and select a grammar which it complies with, in order to assign a role to it later. That would be the perfect companion to pyparsing. At the moment I don't even know if such a thing would be feasible, or how hard it would be to make it, but I remember having seen data analyzing tools based on fixed column width data files, which is of course in a whole other league of difficulty of programming, but at least it gives some encouragement to the idea that it would be possible. Thank you for your ONLamp article and for making pyparsing available. I had some fun experimenting with it and it gave me some insights in parsing grammars. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: excellent book on information theory
Paul Rubin wrote: signal processing, for example. Perhaps it could be improved by being more explicit about what the reader needs to know, and giving references to other books where the prerequisites can be found. There are lots of good explanations, graphs, diagrams and such things in the margins (I'm a few pages further in the book now) but the main course seems to be mathematical formulas. The author should reverse the roles these presentations play, move the archaic math jargon to the margin, or better to a separate latech document, suitable for those unwilling to join the rest of humanity. A separate Python library would be handy too, and if not in the main text it could still be useful for those who lack training in obscure scientific dialects and want to understand things without any agreed upon beforehand gibberish that is mainly meant to exclude those not in the guild. I also don't think presenting the math in Python would make things any easier conceptually. The math in Sussman and Wisdom's Structure and Interpretation of Classical Mechanics is all presented in Scheme, but it's still the same math that's normally presented as equations, and you have to think just as hard to understand it. The problem for me is that I recognize many of the used concepts, but they seem to be deliberately put in cryptic greek letters and undecipherable gibberish. It would not be necessary to present the math in Python, any reasonably consistent kind of pseudocode (but not Scheme or math notation) would made things a lot more clear to me. Something on a related subject with a presentation I like a bit better (but it has its problems too, while your book has more of these nice explanations and stuff, although in the margin): http://www.math.mtu.edu/~kreher/cages.html The authors of this book also seems to think we cannot do without obscure math notation, something which I disagree with very much, but at least they provide some pseudo code and some computer code, unfortunately in C but still better than nothing. The text of the book is not downloadable, but the algorithms source codes are. All of the books writers seem to have not caught up with the idea of hyperlinks and continue to dwell in neolithical paper dreams :-) If they only woke up and let someone like me write some Visual Python code to illustrate the algorithms or even let me just write Python implementations of the algorithms to accompany the books, I'd probably have work for years to come. Math is a beautiful subject, and is not at all secret or inaccessible. Try to broaden your horizons a bit ;-). I hope you're not trying to outexpertize me. You seem to be thinking that you know more about math than me, probably because you have a formal education in the subject? If so, you're proving my point, and thank you very much. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: excellent book on information theory
Paul Rubin wrote: The first few pages are a review of probability theory but I think they assume you've seen it before. The book's subject matter is more mathematical by nature than what most programmers deal with from day to day, and as such, the book is not for everyone. And so the cycle repeats itself. We teach our students the world is all about money, and sure enough, the world is all about money. If we would continue to keep the interesting things away from most of the people, by hiding it behind mathematical jargon we end up believing that functional programming is connected to having a math degree and more such self serving and self fullfilling prophecies. An excellent book would break with this jargon advertising salesmanship. Anton but I'll give it one more try -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: excellent book on information theory
Terry Hancock wrote: On 19 Jan 2006 13:57:06 +0100 Anton Vredegoor [EMAIL PROTECTED] wrote: Some time ago I tried to 'sell' Python to a mathematician. The crucial point was that it was not (in standard Python) possible to have a matrix A and a matrix B and then do for example: A = A * B and have a matrix multiplication performed. Um, why not? I'm trying to think what would be the stumbling block. I don't use matrix multiplies much, but I have implemented 3D vector math so that * is the dot product and % is the cross product, which is pretty trivial to do. Of course ! And to think that I even have used this trick a few times, for example to implement set operations using long integers. I feel ashamed. In my defense I can only explain what happened. Four years ago (when I knew a lot less of Python) I tried to use Numeric to do A*B for matrices, but that resulted in something else than expected. So I redefined the star operator by subclassing a *numeric python* object but then it didn't work (the subclassing IIRC). Then it turned out there was a Matrix module for Numeric that did exacly what was asked, but by that time I was trying to understand Numeric by reading the docs and 'selling Python' at the same time, which didn't work too well ... The main reason for that was that it was necessary to convince someone not having any Python knowledge to install Python *and* some module that I didn't know about and then that module needed *another* install which I didn't know about, and the docs for Numeric were separate from Python. Just too much at once. I believe if I just had implemented matrix multiplication myself at the time in plain Python I wouldn't have overcomplicated the matter in such a way that I couldn't convince anyone else anymore :-) So I got lost in Numerics complexities and that made me forget the basic option. By now I have used Numeric enough to make it likely that I could explain its use to someone. But even when I cured myself of this deficiency, the memory of failure stayed in my head. Witness a classic freudian fixation phenomenon in a Python learning curve :-) In order to prevent such mental damage for future Python programmers, I propose to add a simple matrix multiplication module to the standard distribution. The only obstacle I've run into is that you can't (easily) define *new* operators and precedence levels. There *is* a trick for doing this that was posted on the list some time back, which involved overloading an operator to apply an operator: It would've allowed you to do something like this: a |dot| b a |cross| b or perhaps a dot b a cross b I don't remember where this is posted. The trick was in overloading the , , or | to interact specially with operator objects. That's very nice. Thanks to you for mentioning this and to Jorge, who provided the link to activestate for this recipe in another message. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
ago wrote: [Something I mostly agree with] According to Anton the number of possible solutions can be reduced using 1) number swapping, 2) mirroring, 3) blocks/rows/columns swapping. All those operations create equivalent matrices. For a 9X9 grid, this should give a reduction factor = (9!)*(48)*(6^12) minus the number of duplicated combinations given by the methods above. I am not sure how to calculate the number of duplicated combinations and therefore do not know if the result is good enough. As mentioned, I doubt that it is a viable approach, but I find it an intriguing approach nevertheless. We could start hunting down net sites giving sudoku problems and claim they are trying to sell us the same problem twice :-) Or produce counterfeit problems ourselves and get rich quick. But I wonder about that 6^12 term. Within each 3-row block there are 6 permutations. There are 3 3-row blocks and 3 3-column blocks. Then between blocks (swapping complete 3-row blocks) permutations also give a factor 6. So in my count (but I suck at math) this term schould be: 6**8 (also switching to Python exponentiation notation) Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: excellent book on information theory
Paul Rubin wrote: For an absolutely amazing translation feat, try Michael Kandel's Polish-to-English translation of Stanislaw Lem's The Cyberiad. Returning to the original book, why did they write a lot of it (at least the first few pages until I gave up, after having trouble understanding formulas about concepts I have no such trouble with when framed in less jargonized from) in unintelligible mathemathical notation when there's Python? I prefer a nice Python function over some strange latech symbols. If not Python there's always pseudo code or good old natural language. Don't tell me those math formulas are what it 'really' is, or even that it's more precise that way. The old trick of 'but there are some things that cannot be expressed in any other way than by using formulas' doesn't get one many optimization points in my world. Anton -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: excellent book on information theory
Juho Schultz wrote: Last month I spent about an hour trying to explain why a*2.5e-8 = x raises a SyntaxError and why it should be written x = a*2.5e-8 The guy who wrote the 1st line has MSc in Physics from Cambridge (UK). In mathematics, there is no difference between the two lines. Some time ago I tried to 'sell' Python to a mathematician. The crucial point was that it was not (in standard Python) possible to have a matrix A and a matrix B and then do for example: A = A * B and have a matrix multiplication performed. Since the whole conversation started because there was a need to use this notation for a standard mathematics course this didn't result in adopting Python for it. Meanwhile there has been some progress in Python use there, and of course there are specialized Python packages that enable this kind of notation, but it remains true that there *is* an abyss between computer science and mathematics. Mathematics should change ;-) But that doesn't mean that I wouldn't like standard Python to have A*B for matrices. The problem is that so called 'conventional' mathematical notations leave many options for interpretation, depending on the context and on mutual understanding between mathematicians, excluding non-mathematicians very effectively. A (Python) interpreter has no such problems and will allow precise inspection of what is meant by a piece of code. It has additional advantages in that it can function as a kind of mathematical spellchecker for people like me who often miscode things. Some mathematicians I know can write formulas page after page, while I, if I were to write (or read) a page of formulas there would be at least one mistake throwing me of course for the rest of the document, so that I would need to go back again and again. Does that make me a bad mathematician or does it signify that mathematical notation should change? For me the answer is clear, but that could be because I can't read the stuff without the documentation, and the documentation (mathematics) is considered to be known to everyone (with a math education of course) but I doubt if that is really the case and, even if it were the case it doesn't imply that being explicit (in giving the procedures in computer and human readable form at the same time, for example in Python) wouldn't be even better. Anton -- http://mail.python.org/mailman/listinfo/python-list