Why the result
Why the third print stement output "'comments': [(1, 2, 3)]", I think it should be []. I am using Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32. # program class PicInfo: def __init__(self, intro="", tags="", comments=[]): self.picintro = intro self.pictags = tags self.comments = comments def addcomment(self, comment): self.comments.insert(0, comment) dbdata = PicInfo() print dbdata.__dict__ dbdata.addcomment((1,2,3)) print dbdata.__dict__ dbdata = PicInfo() print dbdata.__dict__ # output {'pictags': '', 'comments': [], 'picintro': ''} {'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''} {'pictags': '', 'comments': [(1, 2, 3)], 'picintro': ''} <- why comments is not []? -- http://mail.python.org/mailman/listinfo/python-list
make a simple search function for homepage
I want to add some simple search function for my homepage. It need to search through all the html files of my homepage (about 300 pages), and highlight the search words. I made some test with HTMLParser, it works but slow. So, my question is how can I improve its speed? from HTMLParser import HTMLParser class HightLightParser(HTMLParser): def __init__(self, outfile, words): self.outfile = outfile self.words = words self.found = False HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): self.outfile.write( self.get_starttag_text( ) ) def handle_endtag(self, tag): self.outfile.write( "" % tag ) def handle_data(self, data): for word in self.words: data = data.replace(word, "%s" % word) #highlight self.outfile.write(data) class SearchParser(HTMLParser): def __init__(self, words): self.words = words self.found = False HTMLParser.__init__(self) def handle_data(self, data): for word in self.words: if word in data: # search self.found = True words = ["the"] x = SearchParser(words) data = file("input.htm").read() x.feed(data) if x.found: y = HightLightParser(file("output.htm", "w"),words) y.feed(data) -- http://mail.python.org/mailman/listinfo/python-list
How to increase the speed of this program?
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I think the problem is at line #1, #2, #3. import wave import array lfile = wave.open(lfilename) rfile = wave.open(rfilename) ofile = wave.open(ofilename, "w") lformat = lfile.getparams() rformat = rfile.getparams() lframes = lfile.readframes(lformat[3]) rframes = rfile.readframes(rformat[3]) lfile.close() rfile.close() larray = array.array("h", lframes) rarray = array.array("h", rframes) oarray = array.array("h", [0]*(len(larray)+len(rarray))) #1 oarray[0::2] = larray#2 oarray[1::2] = rarray#3 ofile.setnchannels(2) ofile.setsampwidth(2) ofile.setframerate(lformat[2]) ofile.setnframes(len(larray)) ofile.writeframes(oarray.tostring()) ofile.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: How to increase the speed of this program?
I think oarray = array.array("h", [0]*(len(larray)+len(rarray))) #1 oarray[0::2] = larray#2 oarray[1::2] = rarray#3 will be executed at C level, but if I use itertools, the program is executed at Python level. So the itertools version is actually slower than the original program. I tested #1,#2,#3. the speed of #2 and #3 is OK, but #1 is slow. So my question is : are there some methods to create a huge array without an initializer? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to increase the speed of this program?
Peter Otten wrote: > HYRY wrote: > > > I want to join two mono wave file to a stereo wave file by only using > > the default python module. > > Here is my program, but it is much slower than the C version, so how > > can I increase the speed? > > I think the problem is at line #1, #2, #3. > > > oarray = array.array("h", [0]*(len(larray)+len(rarray))) #1 > > ITEMSIZE = 2 > size = ITEMSIZE*(len(larray) + len(rarray)) > oarray = array.array("h") > oarray.fromstring("\0" * size) > > may be a bit faster. > > Peter Thank you very much, that is just what I want. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
> - 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. 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. 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. 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. If non-ASCII identifiers becomes true, I think it will be the best gift for Children who donot know English. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
> That is a good point, but I'd like to ask out of curiosity, at what age > do children generally learn pinyin? (Assuming you speak Mandarin. If > not, replace pinyin with the name of whatever phonetic transliteration > is common in your region.) Granted, pinyin shoehorned into ASCII loses > its tone marks, but the result would still be more mnemonic than an > English word that the student has to learn. > Yes, we use Pinyin, and add a number to deal with tone marks, it is better than English words, but as a Chinese, reading pingyin is much slower than reading HanZi. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
> 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). I think programming language such as Logo, or project such as RUR-PLE are used to teach children programming. So, I think there is no problem to teach a 12 years old child for programming. The real problem is they must remeber many English words before programming logic, and it's not funny at all. > 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. > IronPython is wonderful, I will search for some easy and powerful IDE for it, and switch to IronPython. Learning English is an other subject, my object is to teach them some basic programming logic, not English, not even Python. I don't think English is the necessary condition for formal operational logic. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP 3131: Supporting Non-ASCII Identifiers
> How do you feel about the mix of English keywords and Chinese? > How does the English - like "sentences " look to a Chinese? > > Would you support the extension of this PEP to include Chinese > Keywords? > > Would that be a lesser or greater gift? > Because the students can remember some English words, Mixing characters is not a problem. But it's difficult to express their own thought or logic in English or Pinyin(only mark the pronunciation of the Chinese character). As my experience, I found mixing identifiers of Chinese characters and keywords of English is very easy for reading. Because the large difference between Chinese characters and ASCII characters, I can distinguish my identifiers with keywords and library words quickly. -- http://mail.python.org/mailman/listinfo/python-list
the address of list.append and list.append.__doc__
I have the following questions, I am using Python 2.4.2 >>> a = [1,2,3] >>> id(a.append) 19167152 #1 >>> id(list.append) 11306608 #1 1. the address of a.append and list.append is different, can I get the address of list.append from a.append? >>> id(a.append.__doc__) 19162720 >>> id(a.append.__doc__) 19162336 >>> id(a.append.__doc__) 19162592 >>> id(a.append.__doc__) 19162720 >>> id(list.append.__doc__) 19162336 >>> id(list.append.__doc__) 19162720 >>> id(list.append.__doc__) 19162592 >>> id(list.append.__doc__) 19162336 2. why the address of a.append.__doc__ and list.append.__doc__ change, this means __doc__ is not a normal string, but something return a string. -- http://mail.python.org/mailman/listinfo/python-list
Re: the address of list.append and list.append.__doc__
> No. a.append is a "bound method" - a method that already has an > associated instance, that will be provided as the first argument to the > method call. Bound methods are created "on the fly". > Does this means there is no method to get the original methods from the Bound methods created "on the fly"? I installed python 2.4.4 and tried id(list.append.__doc__) again, here is the result, only id(list.append.__doc__) changes, this is strange, and only happened in the command line. Because I am doing something program that use this id in the command line. Can someone help me try this on your PC, I want to know which is the problem: this version of python, or my PC. Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> id(list.__doc__) 11674984 >>> id(list.__doc__) 11674984 >>> id(list.__doc__) 11674984 >>> id(list.append.__doc__) 11747168 >>> id(list.append.__doc__) 11824352 >>> id(list.append.__doc__) 11747168 >>> id(list.append.__doc__) 11824352 >>> id(list.append.__doc__) 11747168 >>> id(list.append.__doc__) 11824352 >>> id(list.count.__doc__) 11863968 >>> id(list.count.__doc__) 11863968 >>> id(list.count.__doc__) 11863968 >>> id(list.count.__doc__) 11863968 -- http://mail.python.org/mailman/listinfo/python-list
Re: the address of list.append and list.append.__doc__
> "the problem"? > Perhaps if you explain what you really want to do, someone can think the > way to do that, most likely *not* using id() Thanks, now I know I cannot use id() for my problem. Here is my problem: I want to add a docstring translator into the Python interpreter. If the user input: >>> a = [1,2,3] >>> a.append( this translator will show the docstring of append in my native language. Because __doc__ is read only, I added a dict to the interpreter as follows: DOC[list.append.__doc__] = """ translated version of the __doc__ """ When it is the time to show docstring, the program get the translated version from DOC. This works, but I think the key of DOC is too long, so I want to use the id of list.append.__doc__ as the key; or use the id of list.append: DOC[id(list.append.__doc__)] = "..." DOC[id(list.append)] = "..." So, I asked how to get list.append from a.append, and why id(list.append.__doc__) changes. -- http://mail.python.org/mailman/listinfo/python-list
Re: the address of list.append and list.append.__doc__
> There's no such thing as an "original method" - what's stored as an > attribute of the class is a plain function. FWIW, you can get at this > function quite easily - via the im_func attribute of the method. I know about im_func, but I tried the im_func attribute of append and I get error: 'builtin_function_or_method' object has no attribute 'im_func' a = [1,2,3] a.append.im_func # error > Now what I wonder is what you want to do with the internal identifier of > a function or method ? (please not that the use of the memory address as > an id is purely an implementation detail of CPython). I want to use the function( or id of the function) as a key of dict, d = {} d[list.append] = "abc" d[str.find] = "..." and I want a function f, that return list.append when call as f(a.append), so I can get the value in d by d[f(a.append)]. And I also find these is interesting, methods of an unmutable object can be used as key, but methods of a mutable object cannot: a = [1,2,3] d[a.append] = "..." # error: list objects are unhashable b = "123" d[b.find] = "..." # OK -- http://mail.python.org/mailman/listinfo/python-list
why scipy cause my program slow?
Why the exec time of test(readdata()) and test(randomdata()) of following program is different? my test file 150Hz10dB.wav has 2586024 samples, so I set randomdata function to return a list with 2586024 samples. the exec result is: 2586024 10.8603842736 2586024 2.16525233979 test(randomdata()) is 5x faster than test(readdata()) if I remove "from scipy import *" then I get the following result: 2586024 2.21851601473 2586024 2.13885042216 So, what the problem with scipy? Python 2.4.2, scipy ver. 0.5.1 import wave from scipy import * from time import * import random from array import array def readdata(): f = wave.open("150Hz10dB.wav", "rb") t = f.getparams() SampleRate = t[2] data = array("h", f.readframes(t[3])) f.close() left = data[0::2] mean = sum(left)/float(len(left)) left = [abs(x-mean) for x in left] return left def randomdata(): return [random.random()*32768.0 for i in xrange(2586024)] def test(data): print len(data) print type(data) envelop = [] e = 0.0 ga, gr = 0.977579425259, 0.999773268338 ga1, gr1 = 1.0 - ga, 1.0 - gr start = clock() for x in data: if e < x: e *= ga e += ga1*x else: e *= gr e += gr1*x envelop.append(e) print clock() - start return envelop test(readdata()) test(randomdata()) -- http://mail.python.org/mailman/listinfo/python-list
Re: why scipy cause my program slow?
Thanks, by your hint, I change type(data) to type(data[0]), and I get So, calculate with float is about 5x faster numpy.float64. Robert Kern wrote: > HYRY wrote: > > Why the exec time of test(readdata()) and test(randomdata()) of > > following program is different? > > my test file 150Hz10dB.wav has 2586024 samples, so I set randomdata > > function > > to return a list with 2586024 samples. > > the exec result is: > > 2586024 > > > > 10.8603842736 > > 2586024 > > > > 2.16525233979 > > test(randomdata()) is 5x faster than test(readdata()) > > if I remove "from scipy import *" then I get the following result: > > 2586024 > > > > 2.21851601473 > > 2586024 > > > > 2.13885042216 > > > > So, what the problem with scipy? > > You're importing (through scipy) numpy's sum() function. The result type of > that > function is a numpy scalar type. The set of scalar types was introduced for a > number of reasons, mostly having to do with being able to represent the full > range of numerical datatypes that Python does not have builtin types for. > Unfortunately, the code paths that get executed when arithmetic is performed > sith such scalars are still suboptimal; I believe they are still going through > the full ufunc machinery. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: python recursive function
> def bears (n): > if n==42: > return True > if n%5==0: > bears(n-42) > if n%2==0: > bears(n/2) > if n%3==0 or n%4==0: > one = (n%10) > two = ((n%100)/10) > if one!=0 and two!=0: > bears(n-(one*two)) > return False > > If a game hits 42 it should return True, otherwise False. If program > never hits 42 and return True, then it returns False. I figured out > base case, but I still get False when I enter bears(250). Any help > would be very appreciated! try this: def bears (n): if n==42: return True if n%5==0: if bears(n-42): return True if n%2==0: if bears(n/2): return True if n%3==0 or n%4==0: one = (n%10) two = ((n%100)/10) if one!=0 and two!=0: if bears(n-(one*two)): return True return False print bears(42) print bears(250) print bears(50) print bears(84) print bears(41) -- http://mail.python.org/mailman/listinfo/python-list
How to add function return value
I need to write functions that return locals() as follows, def func1(): a = 1 return locals() def func2(): b = 2 return locals() Can I write a decorator that it can automately do this conversion def func1() a = 1 ---> def func1(): a = 1 return locals() -- http://mail.python.org/mailman/listinfo/python-list