[Tutor] import and unittest
Hi, the documentation of pyunit say that it is good to place the unittest in a seperate module. I wondered if it was possible to do something like this: src/ -a_module/ -sub_module/ test/ -a_module/ -sub_module/ So the test are in a complete different directory than the code and the test dir is a complete copy of the src dir but with unittest instead of code. I have something like this but I don't know how to organize the imports in the tests and I don't know if this is a good idea. What do you think? Thanks -- Thomas Coopman [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change the font size of the lines and rectangle on aTkinter Canvas
I want to change the coordinates because I was under the wrong impression; when I gave some thought then only I realised that the COORDINATES need to be changed in order to increase the dimensions of the GRAPH and in turn the entire frame, canvas, and the toplevel window that is holding the frame and the canvas.. Is there a method that redraws the toplevel windows that is holding the widgets If yes, then can you suggest me how to do it.. Thanks in anticipation. Regards, Asrarahmed Kadri On 1/16/07, Alan Gauld [EMAIL PROTECTED] wrote: Asrarahmed Kadri [EMAIL PROTECTED] wrote I want to provide with a functionality of changing the font size, in this case the width of the lines. Its not really the font size. fonts only apply to text, its only the line width you want to change. However todo that I think the easiest way is just to elete the existing line (and maybe the whole graph) and redraw with the new parameters. Provideed the data has been precalculated then this should be a rapid operation. Is it possible to do it dynamically; I mean the graph is already drawn, now with a Menu, suppose I want to provide the user with options of 50%, 100% and 200% font size. You will need to store the line thicknesses in variables so that the drawing routine can be parameter driven. Then simply delete the existing graph and redraw it with the new thickness - remember you may need to rescale your axis to cater for thicker/thinner lines. If you are using a filled Rectangle rather than a real line (which is what I would actually expect to see in a bar chart) then obviously the line thickness simply involves setting the difference in the y coordinates appropriately. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change the font size of the lines and rectangle on aTkinter Canvas
On Tue, 16 Jan 2007 00:33:20 - Alan Gauld [EMAIL PROTECTED] wrote: Asrarahmed Kadri [EMAIL PROTECTED] wrote I want to provide with a functionality of changing the font size, in this case the width of the lines. Its not really the font size. fonts only apply to text, its only the line width you want to change. However todo that I think the easiest way is just to elete the existing line (and maybe the whole graph) and redraw with the new parameters. Provideed the data has been precalculated then this should be a rapid operation. I am not sure if the OP meant to increase the graph elements according to the font size, so the graph does not look tiny next to a huge font. Anyway, there is no need to delete and recreate the items, just calculate the new coordinates and pass them to Canvas.coords() Is it possible to do it dynamically; I mean the graph is already drawn, now with a Menu, suppose I want to provide the user with options of 50%, 100% and 200% font size. You will need to store the line thicknesses in variables so that the drawing routine can be parameter driven. Then simply delete the existing graph and redraw it with the new thickness - remember you may need to rescale your axis to cater for thicker/thinner lines. This is probably not necessary either, you can query the size of canvas elements dynamically, use Canvas.bbox() to query width and height of rectangles or Canvas.itemcget(item, 'width') and again Canvas.bbox() for line elements. If you are using a filled Rectangle rather than a real line (which is what I would actually expect to see in a bar chart) then obviously the line thickness simply involves setting the difference in the y coordinates appropriately. If you only want to be able to resize the graph elements into half and double size it is probably easiest to use the Canvas.scale() method for this, like from Tkinter import * c=Canvas() c.pack(fill='both', expand=1) r = c.create_rectangle(20, 20, 70, 30, fill='red') c.scale(r, 20, 20, 2, 2) c.scale(r, 20, 20, 0.5, 0.5) However, text, image and window items cannot be scaled. If you want to scale text items, it might be the best to pre-define e.g. three pixel-sized fonts, like fonts = {'small':'helvetica -10', 'normal':'helvetica -12', 'big':'helvetica -14'} and then provide a callback that will both change the font size for the text elements and scale the graph elements (and the x- and y-axis). I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python query
Dear All, My name is Alexander Kapshuk. I'm interested in learning to program in Python. I have very little programming experience. I've learnt some basics of programming with Logo. I've downloaded Python 2.5 for Windows XP Professional. I would appreciate any advice on what to do next. What books/tutorials to use? I understand that learning a programming language involves practice. So, I suppose, I could use a book/tutorial with loads of practical code samples to try out. Any other practical advice you may deem feasible would be welcome. Thanking you in advance. Alexander Kapshuk. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import and unittest
Thomas Coopman [EMAIL PROTECTED] wrote . I wondered if it was possible to do something like this: src/ -a_module/ -sub_module/ test/ -a_module/ -sub_module/ I don;t see any reason why not although its slightly more work. Personally I tend to keep the tests with the code, but thats mainly because tools such as editors tend to remember the last folder opened and its a pain navigating between the two folders. The other system I have used(in C++ not Python) is to have a test folder inside each src folder like: src/ mod1/ f1.py test/ testf1.py mod2/ f2.py f3.py test/ testf1.py testf2.py etc. This minimises navigation and keeps the tests separate. Its also relatively easy to filter out the tests when it comes time to package upp the code for distribution (assuming you want to lose them!) I have something like this but I don't know how to organize the imports in the tests and I don't know if this is a good idea. What do you think? I think in Python you should create a package structure for your code so that import can find the modules more easily. But I've never tried this in Python, my Python projects are rarely big enough to warrant it. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import and unittest
On Tue, 16 Jan 2007 10:06:37 - Alan Gauld [EMAIL PROTECTED] wrote: Thomas Coopman [EMAIL PROTECTED] wrote . I wondered if it was possible to do something like this: src/ -a_module/ -sub_module/ test/ -a_module/ -sub_module/ I don;t see any reason why not although its slightly more work. Personally I tend to keep the tests with the code, but thats mainly because tools such as editors tend to remember the last folder opened and its a pain navigating between the two folders. The other system I have used(in C++ not Python) is to have a test folder inside each src folder like: src/ mod1/ f1.py test/ testf1.py mod2/ f2.py f3.py test/ testf1.py testf2.py etc. This minimises navigation and keeps the tests separate. Its also relatively easy to filter out the tests when it comes time to package upp the code for distribution (assuming you want to lose them!) I think I will use something like this. I have something like this but I don't know how to organize the imports in the tests and I don't know if this is a good idea. What do you think? I think in Python you should create a package structure for your code so that import can find the modules more easily. But I've never tried this in Python, my Python projects are rarely big enough to warrant it. Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] it is about definitions
is there a difference between: class foo: and class foo(): or between these function definitions: def bar(): and def bar: ? thanks in advance E. - Станете част от Европейското On-line семейство! Регистрирайте .EU домейн на изключителна цена от 15,90 лв.! www.SuperHosting.bg - Динамични Хостинг Решения ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Clustering?
Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) Thanks in advance for your help, Carlos ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] it is about definitions
* [EMAIL PROTECTED] [EMAIL PROTECTED] [070116 15:19]: is there a difference between: class foo: and class foo(): This is a SyntaxError. or between these function definitions: def bar(): and def bar: This too. Andreas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python query
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alexander Kapshuk Sent: Tuesday, January 16, 2007 2:50 AM To: tutor@python.org Subject: Re: [Tutor] python query Dear All, My name is Alexander Kapshuk. I'm interested in learning to program in Python. I have very little programming experience. I've learnt some basics of programming with Logo. I've downloaded Python 2.5 for Windows XP Professional. I would appreciate any advice on what to do next. What books/tutorials to use? I understand that learning a programming language involves practice. So, I suppose, I could use a book/tutorial with loads of practical code samples to try out. Any other practical advice you may deem feasible would be welcome. Thanking you in advance. Alexander Kapshuk. Here's some good books to read. http://www.python.org/infogami-faq/tutor/tutor-what-are-some-good-books- on-python/ Feel free to post questions to this list too. Mike - NOTICE: This e-mail transmission and any documents or files attached to it contain information for the sole use of the above-identified individual or entity. Its contents may be privileged, confidential, and exempt from disclosure under the law. Any dissemination, distribution, or copying of this communication is strictly prohibited. Please notify the sender immediately if you are not the intended recipient. FGNS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change the font size of the lines and rectangle on aTkinter Canvas
--- Asrarahmed Kadri [EMAIL PROTECTED] wrote: Is there a method that redraws the toplevel windows that is holding the widgets Yes, you can issue an update() command to any widget. Usually you don't need to as Tkinter figures out when things have changed and updates itself, but occasionally you may need to fo it manually. Alan G. ___ The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider. http://uk.docs.yahoo.com/nowyoucan.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python query
Alexander Kapshuk wrote: Dear All, My name is Alexander Kapshuk. I’m interested in learning to program in Python. I have very little programming experience. I’ve learnt some basics of programming with Logo. I’ve downloaded Python 2.5 for Windows XP Professional. I would appreciate any advice on what to do next. What books/tutorials to use? There are quite a few beginners tutorials available online, there is a list here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import and unittest
Thomas Coopman wrote: Hi, the documentation of pyunit say that it is good to place the unittest in a seperate module. I wondered if it was possible to do something like this: src/ -a_module/ -sub_module/ test/ -a_module/ -sub_module/ So the test are in a complete different directory than the code and the test dir is a complete copy of the src dir but with unittest instead of code. I have something like this but I don't know how to organize the imports in the tests and I don't know if this is a good idea. What do you think? I have tried this but I think it is more trouble than it is worth having an extra set of directories to navigate. I put the tests in the same directory as the module under test. The tests for module.py are in moduletest.py. By putting 'test' at the end, the tests are listed next to the module under test in directory listings, which I like. I use ant (a Java tool) for most of my packaging and it is easy to filter files by name so I don't need an extra directory for that. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
2007/1/16, Carlos [EMAIL PROTECTED]: Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) First, this feels like a list of tuples rather than a list of lists; however, tuples and lists don't differ that much in their behaviour, so there's nothing really lost. And yes, it is possible. An inline if would be the way I would resolve that: def withinrange(list,xmin,xmax,ymin,ymax): # Get the elements of list for which the first part of the pair is between xmin and xmax # (inclusive) and the second between ymin and ymax. return [c for c in list if xmin = c[0] = xmax and ymin = c[1] = ymax] The longer but clearer method of doing the same would be: def withinrange(list,xmin,xmax,ymin,ymax): templist = [] for c in list: if xmin = c[0] = xmax and ymin = c[1] = ymax: templist.append(c) return templist -- Andre Engels, [EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import and unittest
I wondered if it was possible to do something like this: src/ -a_module/ -sub_module/ test/ -a_module/ -sub_module/ Why not really keep the test code with the main code? # module code here # if __name__ == '__main__': import unittest class TestModuleCode(unittest.TestCase): test harness for Module code def setUp(self): boiler plate for multiple tests pass def testSomething ensure something happens as expected pass unittest.main() I even have some files where I test for command line parameters and if so I process those. If not, I run the unittests. Cheers Duncan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] it is about definitions
On Tue, 16 Jan 2007, Andreas Kostyrka wrote: * [EMAIL PROTECTED] [EMAIL PROTECTED] [070116 15:19]: is there a difference between: class foo: and class foo(): This is a SyntaxError. Hi Andreas, We should ask Emilia why she's asking this question. This particular problem sounds way too close to a homework question for my comfort. A more cautious approach might be better. We can point out: http://www.python.org/doc/tut/node11.html#SECTION001131 http://www.python.org/doc/tut/node11.html#SECTION001150 If Emilia is a CS student, then pointing out the grammar at: http://www.python.org/doc/ref/class.html#tok-inheritance would probably be helpful too. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] it is about definitions
Andreas Kostyrka wrote: * [EMAIL PROTECTED] [EMAIL PROTECTED] [070116 15:19]: is there a difference between: class foo: and class foo(): This is a SyntaxError. In Python 2.5 it is allowed and creates an old-style class, the same as if the parentheses are omitted. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Array indexing
Hello, I'm using Active Python v2.4.3.11 on a Windows XP machine. Probably more relevant is that I'm just learning Python, as in I've been writing Python for less than 24 hours. While trying to implement a PE parser, I ran into the following problem: #** START CODE*** data = file.read(128); directoryTable = struct.unpack('', data); i=0; print Export table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); print Import table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); #** END CODE*** This code throws a syntax error at the first i+=1 on line 4. Why is this the case? It seems like it would be very useful to be able to increment an index after referencing into an array. Is my approach busted? Is there a better way to reference elements? The fix I'm currently using is to write the index I want: (directoryTable[0], directoryTable[1]) I was hoping someone on this list could point me in the right direction. Thanks in advance! Cheers, Joe ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Array indexing
While trying to implement a PE parser, I ran into the following problem: #** START CODE*** data = file.read(128); directoryTable = struct.unpack('', data); i=0; print Export table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); print Import table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); #** END CODE*** This code throws a syntax error at the first i+=1 on line 4. Hi Joe, Yes. Python's assignments aren't expressions --- in Python, assignments are meant to visually stand out. Unfortunately, this means you can't put the assignment within the array indexing expression. There are a few workarounds. One is to treat the directoryTable as a stream of values that we can iterate across. For example: ## values = (3, 1, 4, 1, 5) i = iter(values) i tupleiterator object at 0x6dd50 ## 'i' here is an iterator that we can repeatedly use to get sequential elements: # i.next() 3 i.next() 1 i.next() 4 # In some sense, this should allow you to do what you had in your original code, since i.next() will both give you the value and, internally, move the iterator forward. See: http://www.python.org/doc/tut/node11.html#SECTION001190 for a quick-and-dirty introduction to iterators. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change the font size of the lines and rectangle on aTkinter Canvas
On Tue, 16 Jan 2007 09:56:27 + (GMT) ALAN GAULD [EMAIL PROTECTED] wrote: --- Asrarahmed Kadri [EMAIL PROTECTED] wrote: Is there a method that redraws the toplevel windows that is holding the widgets Yes, you can issue an update() command to any widget. Usually you don't need to as Tkinter figures out when things have changed and updates itself, but occasionally you may need to fo it manually. Or, if you mean to change the toplevel's size, use Toplevel.configure(width=..., height=...) Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Array indexing
On Tue, Jan 16, 2007 at 11:28:49AM -0500, Joe Abbey wrote: Hello, I'm using Active Python v2.4.3.11 on a Windows XP machine. Probably more relevant is that I'm just learning Python, as in I've been writing Python for less than 24 hours. While trying to implement a PE parser, I ran into the following problem: #** START CODE*** data = file.read(128); directoryTable = struct.unpack('', data); i=0; print Export table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); print Import table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); #** END CODE*** This code throws a syntax error at the first i+=1 on line 4. Why is this the case? In Python, i += 1 is a statement. You have used in as an expression. In Python, an expression returns a value; a statement does not. It seems like it would be very useful to be able to increment an index after referencing into an array. What you are asking for is viewed by some as useful. But, I think it is too confusing. Should the variable be incremented before or after it is used to index into the array? C/C++ gives you a choice: you can use either i++ or ++i, which makes code harder to read, I think. And, what about: x = y[i+=1] + z[i] Has the second use of i been incremented or not. Is my approach busted? Is there a better way to reference elements? Instead of: x = directoryTable[i] + directoryTable[i+=1]); use something like: x = directoryTable[i] + directoryTable[i+1] And, by the way, you do not need all those semicolons at the end of each line. In Python, the semicolon is a statement separator, not a statement terminator. It is more Pythonic to use a semicolon between statements only when there are more than one statement on a line. And writing more than one statement on a line is usually discouraged anyway. The fix I'm currently using is to write the index I want: (directoryTable[0], directoryTable[1]) Or, if you need an index variable: directoryTable[i], directoryTable[i+1]) Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
Hallo , Andre thanks a lot for your help, seems to me like my script can work with your function. I found this searching the internet: cluster 1.1.1b2 python-cluster is a simple package that allows to create several groups (clusters) of objects from a list from cluster import * data = [12,34,23,32,46,96,13] cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) cl.getlevel(10) # get clusters of items closer than 10 [96, 46, [12, 13, 23, 34, 32]] cl.getlevel(5) # get clusters of items closer than 5 [96, 46, [12, 13], 23, [34, 32]] I would like to give it a try because I have the impression that it can be helpful too. My problem now is the lambda function, I was wondering if someone could be so kind as to give me an example that could work in my list with nested tuples. Thanks again! Carlos Andre Engels wrote: 2007/1/16, Carlos [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]: Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) First, this feels like a list of tuples rather than a list of lists; however, tuples and lists don't differ that much in their behaviour, so there's nothing really lost. And yes, it is possible. An inline if would be the way I would resolve that: def withinrange(list,xmin,xmax,ymin,ymax): # Get the elements of list for which the first part of the pair is between xmin and xmax # (inclusive) and the second between ymin and ymax. return [c for c in list if xmin = c[0] = xmax and ymin = c[1] = ymax] The longer but clearer method of doing the same would be: def withinrange(list,xmin,xmax,ymin,ymax): templist = [] for c in list: if xmin = c[0] = xmax and ymin = c[1] = ymax: templist.append(c) return templist -- Andre Engels, [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.432 / Virus Database: 268.16.12/630 - Release Date: 1/15/2007 8:28 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Array indexing
On 1/16/07, Dave Kuhlman [EMAIL PROTECTED] wrote: On Tue, Jan 16, 2007 at 11:28:49AM -0500, Joe Abbey wrote: Hello, I'm using Active Python v2.4.3.11 on a Windows XP machine. Probably more relevant is that I'm just learning Python, as in I've been writing Python for less than 24 hours. While trying to implement a PE parser, I ran into the following problem: #** START CODE*** data = file.read(128); directoryTable = struct.unpack('', data); i=0; print Export table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); print Import table 0x%08X + 0x%08x % (directoryTable[i+=1], directoryTable[i+=1]); #** END CODE*** This code throws a syntax error at the first i+=1 on line 4. Why is this the case? In Python, i += 1 is a statement. You have used in as an expression. In Python, an expression returns a value; a statement does not. It seems like it would be very useful to be able to increment an index after referencing into an array. What you are asking for is viewed by some as useful. But, I think it is too confusing. Should the variable be incremented before or after it is used to index into the array? C/C++ gives you a choice: you can use either i++ or ++i, which makes code harder to read, I think. And, what about: x = y[i+=1] + z[i] Has the second use of i been incremented or not. Is my approach busted? Is there a better way to reference elements? Instead of: x = directoryTable[i] + directoryTable[i+=1]); use something like: x = directoryTable[i] + directoryTable[i+1] And, by the way, you do not need all those semicolons at the end of each line. In Python, the semicolon is a statement separator, not a statement terminator. It is more Pythonic to use a semicolon between statements only when there are more than one statement on a line. And writing more than one statement on a line is usually discouraged anyway. The fix I'm currently using is to write the index I want: (directoryTable[0], directoryTable[1]) Or, if you need an index variable: directoryTable[i], directoryTable[i+1]) Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thank you Danny for the iterator tutorial. I'll check the link. Thanks Dave for the language lesson. As you could tell from my code snippet, C\C++ is what I have programmed the most in. For this case I believe the iterator example would be most preferred. #** START CODE*** dTable = iterator(directoryTable) print Export table 0x%08X + 0x%08x % (dTable.next(), dTable.next ()) #** END CODE*** But for now the explicit indexing will work fine :) Thanks! Joe ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about object oriented programming and inheritance using datetime module
On Mon, 15 Jan 2007, Kent Johnson wrote: [EMAIL PROTECTED] wrote: My class inherits from the date class, but I have to type 'from datetime import date' before I can initialize the class definition. Is there some way to avoid this ? No, and really there is no reason to want to avoid this. You have to import any external module that you want to use directly. Imports are very common in Python code and there is no reason not to use them. I had a similar issue when I started Python, so I think I know what tpc may be after. My thought was that I did not want to do the import if the class was not actually going to be used. I was really thinking about it the wrong way, though. Really, I would not want to do the import unless the class was going to be *defined* for use. The right approach here is to put the class into a module, and the import statement into the new module as well. Then, a program that needs to use the class imports the module; and that module, only if it is imported, imports the classes on which it depends. In this case, tpc's class might be defined in a MyDateStuff.py module, which contains: import datetime class age_calculator(datetime.date): etc. Then, when he imports MyDateStuff, it imports datetime and defines age_calculator. I second Kent's concerns over this, though. It sounds like age_calculator is really a method that uses datetime.date; not a subclass of datetime.date. The question you should ask is: will an age_calculator object actually also be a date object? If so, subclassing makes sense. Otherwise, think of using a method. Put another way, you should think of a statement like class age_calculator(datetime.date): as meaning define a new class named age_calculator; an age_calculator object is a type of date object. An example is if you wanted to create a birthdate class, which was just like a regular date, but also included the birthstone that corresponded to the date. We could create a birthdate module that included a Birthdate class: ### import datetime class Birthdate(datetime.date): def __init__(self, year, month, day): stones = [Garnet, Amethyst, Aquamarine, Diamond, Emerald, Perl, Ruby, Python, Sapphire, Opal, Topaz, Turquoise] self.birthstone = stones[month-1] ### We could create a Birthdate object like this: import birthdate z = birthdate.Birthdate(1971, 7, 12) Note, it has the birthstone for July: z.birthstone 'Ruby' It also has inherited the other attributes and methods of the standard datetime.date class on which it was based: z.isoformat() '1971-07-12' Because a birthdate is after all, just a particular kind of date; and the Birthdate class is just a particular kind of date class. But in your case, with a name like age_calculator, it doesn't sound like an age_calculator is a kind of date. It sounds like a thing that calculates ages. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to convert a long decimal into a string?
Here's a function I wrote some time ago, and just discovered that in one important category of cases, long numbers with a decimal point, it doesn't do what I intended. = def numberRounding(n, significantDigits=4): Rounds a number (float or integer, negative or positive) to any number of significant digits. If an integer, there is no limitation on it's size. import decimal def d(x): return decimal.Decimal(str(x)) decimal.getcontext().prec = significantDigits return d(n)/1 == Now, print numberRounding(232.3452345230987987098709879087098709870987098745234, 30) prints 232.345234523 whereas if the first argument is enclosed in quotes, it does what I indended. Thus: print numberRounding('232.3452345230987987098709879087098709870987098745234', 30) prints 232.345234523098798709870987909 . So my question is, how can I revise numberRounding() so that it is not necessary to employ the quotes. Or alternatively, is there a way to non-manually put quotes around an argument that is a long decimal? If have no idea at all about the second. As for the first, I believe I could devise an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). But I have the feeling that Python must have a way already built in. Does it? Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about object oriented programming and inheritance using datetime module
Terry Carroll wrote: An example is if you wanted to create a birthdate class, which was just like a regular date, but also included the birthstone that corresponded to the date. We could create a birthdate module that included a Birthdate class: ### import datetime class Birthdate(datetime.date): def __init__(self, year, month, day): stones = [Garnet, Amethyst, Aquamarine, Diamond, Emerald, Perl, Ruby, Python, Sapphire, Opal, Topaz, Turquoise] self.birthstone = stones[month-1] I think you are missing the line datetime.date.__init__(self, year, month, day) somewhere in here. I am very surprised that this works, my understanding was that datetime.date was immutable and required overriding __new__() rather than __init__(). But it does work... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
Dick Moores wrote: Here's a function I wrote some time ago, and just discovered that in one important category of cases, long numbers with a decimal point, it doesn't do what I intended. = def numberRounding(n, significantDigits=4): Rounds a number (float or integer, negative or positive) to any number of significant digits. If an integer, there is no limitation on it's size. import decimal def d(x): return decimal.Decimal(str(x)) decimal.getcontext().prec = significantDigits return d(n)/1 == Now, print numberRounding(232.3452345230987987098709879087098709870987098745234, 30) prints 232.345234523 The problem is that 232.3452345230987987098709879087098709870987098745234 is a float which cannot represent this number exactly. Just typing it at the interpreter prompt shows the problem: 232.3452345230987987098709879087098709870987098745234 232.34523452309881 str(_) '232.345234523' So the precision you want is lost immediately when the constant is created. whereas if the first argument is enclosed in quotes, it does what I indended. Thus: print numberRounding('232.3452345230987987098709879087098709870987098745234', 30) prints 232.345234523098798709870987909 . So my question is, how can I revise numberRounding() so that it is not necessary to employ the quotes. You can't. A float simply can't represent the number you want and the function has no way to access the textual representation of the number. Or alternatively, is there a way to non-manually put quotes around an argument that is a long decimal? No Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
At 12:16 PM 1/16/2007, Kent Johnson wrote: Dick Moores wrote: Here's a function I wrote some time ago, and just discovered that in one important category of cases, long numbers with a decimal point, it doesn't do what I intended. = def numberRounding(n, significantDigits=4): Rounds a number (float or integer, negative or positive) to any number of significant digits. If an integer, there is no limitation on it's size. import decimal def d(x): return decimal.Decimal(str(x)) decimal.getcontext().prec = significantDigits return d(n)/1 == Now, print numberRounding(232.3452345230987987098709879087098709870987098745234, 30) prints 232.345234523 The problem is that 232.3452345230987987098709879087098709870987098745234 is a float which cannot represent this number exactly. Just typing it at the interpreter prompt shows the problem: 232.3452345230987987098709879087098709870987098745234 232.34523452309881 str(_) '232.345234523' So the precision you want is lost immediately when the constant is created. whereas if the first argument is enclosed in quotes, it does what I indended. Thus: print numberRounding('232.3452345230987987098709879087098709870987098745234', 30) prints 232.345234523098798709870987909 . So my question is, how can I revise numberRounding() so that it is not necessary to employ the quotes. You can't. A float simply can't represent the number you want and the function has no way to access the textual representation of the number. Or alternatively, is there a way to non-manually put quotes around an argument that is a long decimal? No Thanks, Kent. So I go with working up an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). Dick Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
On Tue, 16 Jan 2007, Dick Moores wrote: Here's a function I wrote some time ago, and just discovered that in one important category of cases, long numbers with a decimal point, it doesn't do what I intended. = def numberRounding(n, significantDigits=4): Rounds a number (float or integer, negative or positive) to any number of significant digits. If an integer, there is no limitation on it's size. import decimal def d(x): return decimal.Decimal(str(x)) decimal.getcontext().prec = significantDigits return d(n)/1 == Now, print numberRounding(232.3452345230987987098709879087098709870987098745234, 30) prints 232.345234523 whereas if the first argument is enclosed in quotes, it does what I indended. Thus: print numberRounding('232.3452345230987987098709879087098709870987098745234', 30) prints 232.345234523098798709870987909 . So my question is, how can I revise numberRounding() so that it is not necessary to employ the quotes. I'm guessing that you can't. When you do: numberRounding(232.3452345230987987098709879087098709870987098745234,30) you are calling numberRounding with a first argument of a floating point, whose value is set from 232.3452345230987987098709879087098709870987098745234, but is going to be cut down immediately to the amount of precision that you have on your machine. For me, that's 232.34523452309881, well short of 30. So it's the same as calling: numberRounding(232.34523452309881,30) By the time your function gets control, you're already down to the less-precise value. That's probably why a direct decimal conversion from float is not allowed, to avoid the illusion of more precision than there really is. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
Dick Moores wrote: Thanks, Kent. So I go with working up an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). I really don't know what you mean by this. You are lost as soon as you write 232.3452345230987987098709879087098709870987098745234 without quotes, there is no float literal with that value. You could pass the integer 2323452345230987987098709879087098709870987098745234 as the argument if you always have the implied decimal point in the same place. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
On Tue, 2007-01-16 at 12:28 -0800, Dick Moores wrote: So I go with working up an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). This seems like a lot of effort for not much reward. Where is n coming from? If you already have something that holds the required level of precision, why does it need to be transformed? Could it already be a string? If n is entered through raw_input, then you received it as a string. I don't want to push you in the wrong direction, but it seems like there must be a better way. -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
At 12:47 PM 1/16/2007, you wrote: On Tue, 2007-01-16 at 12:28 -0800, Dick Moores wrote: So I go with working up an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). This seems like a lot of effort for not much reward. Where is n coming from? If you already have something that holds the required level of precision, why does it need to be transformed? Could it already be a string? If n is entered through raw_input, then you received it as a string. I don't want to push you in the wrong direction, but it seems like there must be a better way. LLoyd, Yes, that's right, isn't it. And functions I would use that feed into numberRounding() would be things such as my decPow(), which returns a string: (All corrective comments on it are welcome!) == def decPow(n, power, precision=40): Raise any number n (as a string) to any integral power, to any degree of precision. import decimal def d(x): return decimal.Decimal(str(x)) if power == 0: return 1 elif power 0: decimal.getcontext().prec = precision product = d(n) for k in range(1,power): product = product * d(n) return product elif power 0: decimal.getcontext().prec = precision quotient = 1/d(n) for k in range(1, -(power)): quotient = quotient / d(n) return quotient Still, I'd like to see if can write that algorithm. ;) Thanks very much, Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Subprocess popen problem depending on how I start program
Hi, I'm writing a program that downloads audio streams from the net, to do this I use mplayer and tcpdump, which I kick off as detached processes (if thats the correct terminology), I do it this way because i'm using pygtk and I dont want the program to wait for the processes to finish otherwise the app would appear to freeze. I have written 2 modules, one contains a class and methods that do all the work, the other is the gui module, I've got it working fairly well at least when I start the program from within a bash shell (Im using Ubuntu Linux BTW) The modules both have a line '#!/usr/bin/env python' at the top and are set as executable too so I can run them from a bash shell. Now I have one eye on distributing the modules in the future I'm thinking how people will start the app, and I discover that if I use the 'run application' program (alt F2 in most linux's I think) to call the module it starts ok and works normally until I attempt to use the part that starts the subprocess in the background, it just does not work and the gui continues to respond as if nothing ever happened. When I check the processes running I can see that it has not started a sub shell to run the command. also tried other things like creating an application launcher, this will work if I tick 'run in terminal' but not otherwise. I've also started learning about distutils and installed the modules on my system using it, then created a python script (made it executable) and tried using this to start the program, same problem. I'm scratching around trying to find an answer but I'm at a loss now so would sure appreciate some help, thanks. Wayne.. ps. sorry if it's wordy, I want to give enough info. if you need anymore info just say. ___ New Yahoo! Mail is the ultimate force in competitive emailing. Find out more at the Yahoo! Mail Championships. Plus: play games and win prizes. http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dealing with 406 HTTP response
Dear pythoners, has anybody dealt with a 406 reponse through a urllib.FancyURLopener class ? I have a code that downloads images from websites and soemsites respond with an 406 which I don't know how to handle thanks! Tsila ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
Carlos [EMAIL PROTECTED] wrote from cluster import * data = [12,34,23,32,46,96,13] cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) cl.getlevel(10) # get clusters of items closer than 10 [96, 46, [12, 13, 23, 34, 32]] I would like to give it a try because I have the impression that it can be helpful too. My problem now is the lambda function, I was wondering if someone could be so kind as to give me an example that could work in my list with nested tuples. lambda is just a shorthand way of writing a simple function. You don't need to use lambda. The above line could have been done thisaway: def f(x,y): return abs(x-y) cl = HierarchicalClustering(data, f) So provided you can write a function to do what you want you don't need the lambda if it confuses you. In a general sense: def f(p): return expression is the same as f = lambda p: expression So anywhere that a function name is nmeeded you can put a lambda. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with 406 HTTP response
On Tue, 16 Jan 2007, Tsila Hassine wrote: has anybody dealt with a 406 reponse through a urllib.FancyURLopener class ? I have a code that downloads images from websites and soemsites respond with an 406 which I don't know how to handle Hi Tsila, Have you looked at the description of a 406 error? http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html What's going on is that FancyURLOpener doesn't specify the types it can accept, so by default, the types of things are quite limited. You'll want to look into the addheader() method of your FancyURLOpener to reassure the web server that you will accept anything from them, by adding a permissive Accept: header. For example: opener.addheader('Accept', '*/*') should add a 'Accept:' header that eats everything. But for some reason, it appears that this method isn't documented in the Standard Library! http://www.python.org/doc/lib/urlopener-objs.html does anyone know what's going on there? Documentation on addheader can be found here: http://pydoc.org/2.4.1/urllib.html#FancyURLopener-addheader but the method should really be included in the API docs. I'll send a ping to the Documentation folks and see if this will be amended. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to convert a long decimal into a string?
At 12:45 PM 1/16/2007, Kent Johnson wrote: Dick Moores wrote: Thanks, Kent. So I go with working up an algorithm for first converting n to an int (for example, multiplying the above n by 1000), converting to a string, putting the decimal point back in between indices 2 and 3, then using that string as n (thereby avoiding the use of quotes around n as the first argument). I really don't know what you mean by this. You are lost as soon as you write 232.3452345230987987098709879087098709870987098745234 without quotes, there is no float literal with that value. You could pass the integer 2323452345230987987098709879087098709870987098745234 as the argument if you always have the implied decimal point in the same place. You're right, of course. As usual. Just one of those You can't get there from here things. But I don't really need to, as Lloyd pointed out. Thanks, Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with 406 HTTP response (fwd)
-- Forwarded message -- Date: Wed, 17 Jan 2007 02:08:48 +0100 From: Tsila Hassine [EMAIL PROTECTED] To: Danny Yoo [EMAIL PROTECTED] Subject: Re: [Tutor] dealing with 406 HTTP response Danny - thanks! it works though I am not sure I fully understand what i did :-) I am iverriding teh opener class with my fancy one, then creatign an opener instance of it, and then i am specifying the addheader method you mentioned. is that the right way to do it (well - it works) - and what did i just do in terms of class overriding ? thanks a lot for your help! Tsila On 1/17/07, Danny Yoo [EMAIL PROTECTED] wrote: On Tue, 16 Jan 2007, Tsila Hassine wrote: has anybody dealt with a 406 reponse through a urllib.FancyURLopener class ? I have a code that downloads images from websites and soemsites respond with an 406 which I don't know how to handle Hi Tsila, Have you looked at the description of a 406 error? http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html What's going on is that FancyURLOpener doesn't specify the types it can accept, so by default, the types of things are quite limited. You'll want to look into the addheader() method of your FancyURLOpener to reassure the web server that you will accept anything from them, by adding a permissive Accept: header. For example: opener.addheader('Accept', '*/*') should add a 'Accept:' header that eats everything. But for some reason, it appears that this method isn't documented in the Standard Library! http://www.python.org/doc/lib/urlopener-objs.html does anyone know what's going on there? Documentation on addheader can be found here: http://pydoc.org/2.4.1/urllib.html#FancyURLopener-addheader but the method should really be included in the API docs. I'll send a ping to the Documentation folks and see if this will be amended. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] mutability, __new__
[hijacking the question about object oriented programming and inheritance using datetime module thread here] On Tue, 16 Jan 2007, Kent Johnson wrote: I think you are missing the line datetime.date.__init__(self, year, month, day) somewhere in here. Yeah; sloppy of me. I am very surprised that this works, my understanding was that datetime.date was immutable and required overriding __new__() rather than __init__(). But it does work... Yeah, come to think of it, that is interesting. A quick test shows that datetime.date is apparently immutable, because it can be used as a dictionary key. But if it's immutable, then __init__ shouldn't work, and, as you say, you gotta go with __new__. I have to admit, I don't completely comprehend the use of __new__, but the discussion at http://www.python.org/download/releases/2.2.3/descrintro/ explained quite a lot. How can you find if a particular object is immutable or not? Or, relatedly, if you wanted to define an object that was immutable, how would you do it? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difference between 'yield' and 'print'
Is there any difference between yield and print in python script?i have written a script based on fibonacci series where in i used yield and print in two different scripts: the script is given below: def fib(n): a,b = 0,1 while a=n: print a a,b = b,a+b for x in fib(4): print x. When i executed this script i am getting this error: Typeerror:nonetype is not iterable But the same script if 'print' is replaced with 'yield' inside while loop it is executing properly without any type errors could any one reply why print cant be used instead of yield and why error is generated? -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor