[Tutor] compare two souce files
Hello Tutor I would like to compare two souce code files but ignoring doc strings, comments and space (and perharps in future statement by statement comparision) e.g class Foo def foo(): # prints my name return 'my name' class Boo def boo(): print 'my name' Want to check if Foo and Boo are the same. At functional level one can use boo.func_code.co_code == foo.func_code.co_code What is the class level equivlant of this? cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare two souce files
Jojo Mwebaze wrote: Hello Tutor I would like to compare two souce code files but ignoring doc strings, comments and space (and perharps in future statement by statement comparision) e.g class Foo def foo(): # prints my name return 'my name' class Boo def boo(): print 'my name' Want to check if Foo and Boo are the same. At functional level one can use boo.func_code.co_code == foo.func_code.co_code What is the class level equivlant of this? Foo.foo.func_code.co_code == Boo.boo.func_code.co_code To check if the classes are equivalent, something like this will get you started: if dir(Foo) == dir(Boo): for attr in dir(Foo): if getattr(Foo, attr) != getattr(Boo, attr): print "Unequal" break -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python: 27 times faster than bash
I rewrote a bash script that gathers data from a bunch of text files. The script uses grep, sed, and awk to parse the data and takes around 5.5 seconds to run. After rewriting the script in python, it now takes a little over 0.2 seconds to run. Thanks to those of you who helped me with some questions on proper syntax. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Need Help!] def main(): pass if __name__ == '__main__': main() postPos=words[3] f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") lines = f.readlines() for line in lines: words = line.split(",") print (words[3],postPos) close = f When I run the above script, the field postPos doesn't match the input! postPost should be the same as words[3] can't guess why? any help? *** Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32. *** >>> 1 3 2 3 3 3 >>> Terry Green ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
This code shouldn't even actually execute, you should get an indexerror exception. Close your IDE and try it again. Should make the issue more clear. - Sent from a mobile device with a bad e-mail client. - On Oct 27, 2010, at 1:04 PM, "Terry Green" wrote: > Need Help!] > > > > def main(): > > pass > > > > if __name__ == '__main__': > > main() > > postPos=words[3] > > f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") > > lines = f.readlines() > > for line in lines: > > words = line.split(",") > > print (words[3],postPos) > > close = f > > > > When I run the above script, the field postPos doesn't match the input! > > postPost should be the same as words[3] > > can't guess why? > > any help? > > > > *** Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit > (Intel)] on win32. *** > > >>> > > 1 3 > > 2 3 > > 3 3 > > >>> > > > > Terry Green > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Terry Green wrote: def main(): pass Useless function, does nothing. Why is it here? if __name__ == '__main__': main() Also does nothing useful. Why is it here? postPos=words[3] This like will fail with NameError, because words is not defined anywhere. This is not the code you are running. We can't tell what is wrong with the code you run when you show us something completely different. Please send us the *actual* code you run, not something you've re-typed from memory, or copied and pasted in bits and pieces from different places. By the way, why are you calling a *word* "postPos"? Pos normally is an abbreviation for *position*, which will be a number. To say: postPos = words[3] makes postPos a word, not a position. So the name is misleading. You should change it. f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") lines = f.readlines() for line in lines: words = line.split(",") print (words[3],postPos) close = f I'm pretty sure that's not what you want. This line "close = f" will define a new variable called "close", which is equal to the open file object "f". What you want is to call the close method of the file object: f.close() When I run the above script, the field postPos doesn't match the input! postPost should be the same as words[3] You only set postPos once, outside the loop, and then never update it inside the loop. Follow the execution chain by hand: lines = ["a,b,c,d,e", "f,g,h,i,j,k"] Somehow postPos gets set to something before the loop starts. I don't know how, because you haven't shown us that part of the code. But let's say postPos is set to "z", just as an example. So you enter the for-loop: line gets set to "a,b,c,d,e" words get sets to ["a", "b", "c", "d", "e"] print words[3], postPos => prints "d", "z" line gets set to "f,g,h,i,j,k" words get sets to ["f", "g", "h", "i", "j", "k"] print words[3], postPos => prints "i", "z" -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Thu, Oct 28, 2010 at 8:11 PM, Steven D'Aprano wrote: > >> postPos=words[3] > > This like will fail with NameError, because words is not defined anywhere. > > This is not the code you are running. We can't tell what is wrong with the > code you run when you show us something completely different. > > Please send us the *actual* code you run, not something you've re-typed from > memory, or copied and pasted in bits and pieces from different places. > Actually Steven, if you are using IDLE (as a lot of new programmers are), the code by default does NOT execute in a subprocess but instead in the same interpreter IDLE is running. Therefore it can actually have variables stick around from previous executions. So if he didn't have that line in his code the first time he executed, then words would be defined. Then the second time perhaps he added that line, and in that case it will actually succeed because an old value of 'words' is floating around. It also causes issues because if you import a module from the interpreter and then use it in your code, it will work only during that instance, but if you try to run the code standalone it will fail when you try to use the module. I know that sounds weird and that's why IDLE should be configured to run as a subprocess. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Luke Paireepinart wrote: On Thu, Oct 28, 2010 at 8:11 PM, Steven D'Aprano wrote: postPos=words[3] This like will fail with NameError, because words is not defined anywhere. This is not the code you are running. We can't tell what is wrong with the code you run when you show us something completely different. Please send us the *actual* code you run, not something you've re-typed from memory, or copied and pasted in bits and pieces from different places. Actually Steven, if you are using IDLE (as a lot of new programmers are), the code by default does NOT execute in a subprocess but instead in the same interpreter IDLE is running. Therefore it can actually have variables stick around from previous executions. And? That's no different from running code in the default Python interactive interpreter. If you have run code that has an effect on the results you are getting, and don't show that code, then the code you have run is not the code you have shown. Just like I said :) > So if he didn't have that line in his code the first time he executed, then words would be defined. Then the second time perhaps he added that line, and in that case it will actually succeed because an old value of 'words' is floating around. Exactly. The code the Original Poster *actually* executed included a line that defined words. In this case, any extra code is probably innocuous, but next time, who knows? I then went on to identify the likely cause of the problem -- the OP fails to redefine postPos each time through the loop. But can I be sure? No, because I don't know what else the OP did. I can't think of anything realistic that might have an effect, but perhaps that's my failure of imagination. When you have a problem with code, it is *vital* that you know what code is actually being executed. There are very few programming tasks harder than trying to debug code that doesn't actually contain any bugs, or contains bugs different from the ones you are seeing, because the code you are actually executing is something different from what you think you are executing. You know the joke about the drunk who lost his keys in the park, and then spends the next few hours searching under the light pole in the street because the light is better there? That's what it can be like. It also causes issues because if you import a module from the interpreter and then use it in your code, it will work only during that instance, but if you try to run the code standalone it will fail when you try to use the module. I know that sounds weird and that's why IDLE should be configured to run as a subprocess. Regardless of whether you are using IDLE, some other IDE, or just the Python interpreter, you should never post code until you are sure that it is the actual code that is being executed. If that means quitting IDLE and starting it again in a fresh session, then do so. For anything more than six or eight lines of code, I never trust I understand it until I've seen it run in a fresh environment. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to install BeautifulSoup?
64-bit Vista Python 2.6.4 I just downloaded BeautifulSoup-3.0.8.1, which is a folder containing setup.py. Does anyone know how to run setup,py? I've already tried C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1>setup.py File "C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1\setup.py", line 22 print "Unit tests have failed!" ^ and C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1>python setup.py File "setup.py", line 22 print "Unit tests have failed!" ^ Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to install BeautifulSoup?
On Thu, Oct 28, 2010 at 23:18, Abhijeet Rastogi wrote: > > In command prompt, do something like > > $python setup.py install Yes, that did it. Thanks! Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor