From denis.spir at gmail.com Sun May 2 07:49:02 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 2 May 2010 07:49:02 +0200 Subject: [Tutor] python list, right! but concretely? Message-ID: <20100502074902.70843655@o> Hello, Is there anywhere some introduction material to the implementation of python lists (or to fully dynamic and flexible sequences, in general)? More precisely, I'd like to know what kind of base data-structure is used (linked list, dynamic array, something else -- and in case of array, how is resizing computed). Also, how is "generics" (element are of free type, and even heterogeneous) managed? Denis PS: The reason is I'm trying to understand better the underlying layers of magic facilities we use everyday. Starting from about no computer science knowledge. I have a working implementation of linked lists in primitive langguages ;-) (namely C & Pascal), but rather complicated to my taste; and I'm probably overlooking commonly known and used tricks that could make the algorithm simpler or more efficient. ________________________________ vit esse estrany ? spir.wikidot.com From rabidpoobear at gmail.com Sun May 2 08:06:04 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sun, 2 May 2010 01:06:04 -0500 Subject: [Tutor] python list, right! but concretely? In-Reply-To: <20100502074902.70843655@o> References: <20100502074902.70843655@o> Message-ID: 2010/5/2 spir ? : > Hello, > > Is there anywhere some introduction material to the implementation of python lists (or to fully dynamic and flexible sequences, in general)? > More precisely, I'd like to know what kind of base data-structure is used (linked list, dynamic array, something else -- and in case of array, how is resizing computed). Also, how is "generics" (element are of free type, and even heterogeneous) managed? There's no distinction between the types, a list is just a list of Objects, and they can be anything, they can be lists themselves, or a primitive data type like an int (which is still an Object in Python), or whatever. Remember everything in Python inherits from the generic class 'object' or it inherits from a class that inherits from 'object'. I have no idea what the implementation actually is, but I have read about it somewhere so I do know that it's possible to find the implementation details. Good luck, -Luke From alan.gauld at btinternet.com Sun May 2 09:49:30 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 2 May 2010 08:49:30 +0100 Subject: [Tutor] python list, right! but concretely? References: <20100502074902.70843655@o> Message-ID: "spir ?" wrote > Is there anywhere some introduction material to the implementation > of python lists (or to fully dynamic and flexible sequences, in general)? The definitive information is the source code which is freely available. > More precisely, I'd like to know what kind of base data-structure > is used (linked list, dynamic array, something else -- and in case of > array, how is resizing computed). I'd look to the source for that. It may be a combination of traditional techniques. But I've never been curious enough to look :-) > Also, how is "generics" (element are of free type, and even > heterogeneous) managed? Everything is an object in Python. So it is a list of objects. > no computer science knowledge. I have a working implementation > of linked lists in primitive languages ;-) (namely C & Pascal), > but rather complicated to my taste; and I'm probably overlooking > commonly known and used tricks that could make the algorithm > simpler or more efficient. Hmm, Linked lists in C and Pascal are usually pretty trivial. Unless you have done something unusual its hard to think how you could simplify them. You can complexify them for better speed or storage efficiency but the basic linked list in either language is about as simple as dynamic data gets. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sun May 2 10:57:41 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 2 May 2010 18:57:41 +1000 Subject: [Tutor] python list, right! but concretely? In-Reply-To: <20100502074902.70843655@o> References: <20100502074902.70843655@o> Message-ID: <201005021857.42064.steve@pearwood.info> On Sun, 2 May 2010 03:49:02 pm spir ? wrote: > Hello, > > Is there anywhere some introduction material to the implementation of > python lists (or to fully dynamic and flexible sequences, in > general)? More precisely, I'd like to know what kind of base > data-structure is used (linked list, dynamic array, something else -- > and in case of array, how is resizing computed). Also, how is > "generics" (element are of free type, and even heterogeneous) > managed? I'm not sure if this is documented anywhere (other than the source code, of course) but in CPython lists are implemented as arrays of pointers to objects. Because re-sizing arrays is expensive, the array is over-allocated: the array is (very approximately) created twice the size needed, to give room to grow. Then when the array is full, or nearly full, it is doubled in size again. This gives amortised O(1) appends, at the cost of being 50% larger than needed on average. This means that append is nearly always very, very fast, with an occasional, and rare, slow append when the list resizes. Because the list is an array, iteration is very fast and lookups are O(1). However, deleting from the start or middle of the list is O(N), as are insertions. If speed is important, and you need fast insertions and deletions at both the start and end of the list, then you should use collections.deque; however item lookup for deques is O(N). Of course all of this is an implementation detail. Other implementations are free to make other memory/time tradeoffs, although if their lists performed very differently, people would complain. > Denis > > PS: The reason is I'm trying to understand better the underlying > layers of magic facilities we use everyday. Starting from about no > computer science knowledge. I have a working implementation of linked > lists in primitive langguages ;-) (namely C & Pascal), but rather > complicated to my taste; and I'm probably overlooking commonly known > and used tricks that could make the algorithm simpler or more > efficient. The simplest implementation of a linked list in Pascal is something like this: type: ptr = ^node; node = record data: integer; next: ptr end; Here's a procedure to traverse the list, printing each item: {untested} procedure walk(alist: ptr): begin while alist <> nil: begin writeln(alist^.data); alist := alist^.next end; end; Everything else, I leave as an exercise :) -- Steven D'Aprano From lie.1296 at gmail.com Sun May 2 11:44:22 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 02 May 2010 19:44:22 +1000 Subject: [Tutor] python list, right! but concretely? In-Reply-To: <20100502074902.70843655@o> References: <20100502074902.70843655@o> Message-ID: On 05/02/10 15:49, spir ? wrote: > Hello, > > Is there anywhere some introduction material to the implementation of python lists > (or to fully dynamic and flexible sequences, in general)? > More precisely, I'd like to know what kind of base data-structure is used > (linked list, dynamic array, something else -- and in case of array, how is > resizing computed). Also, how is "generics" (element are of free type, and > even heterogeneous) managed? Python's 'list' is an array of pointers to `PyObject` ('object' in Python) and the resizing algorithm keeps the list size such that "allocated / 2 <= actual <= allocated". When list need to resize, it overallocates the list slightly over 1.125 times than the requested size "(newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize". If you're interested in more precise details (since I do omit some, especially those that seems to be micro-optimization-related), you need to read the source at http://code.python.org/hg/trunk/file/e9d930f8b8ff/Objects/listobject.c > Denis > > PS: The reason is I'm trying to understand better the underlying layers of > magic facilities we use everyday. Starting from about no computer science > knowledge. I have a working implementation of linked lists in primitive > langguages ;-) (namely C & Pascal), but rather complicated to my taste; > and I'm probably overlooking commonly known and used tricks that could > make the algorithm simpler or more efficient. Real life implementation is always hairy, especially as the programmer cut corners to drench some speed benefit and include many error checkings. Today is the first time I actually looked at list's implementation, now I know why people hated C, for every line of real code, there's three or four lines of error checking code, e.g. to ensure malloc successfully allocated enough memory or that index access is in range. From denis.spir at gmail.com Sun May 2 14:57:26 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 2 May 2010 14:57:26 +0200 Subject: [Tutor] python list, right! but concretely? In-Reply-To: References: <20100502074902.70843655@o> Message-ID: <20100502145726.5016221d@o> On Sun, 02 May 2010 19:44:22 +1000 Lie Ryan wrote: > On 05/02/10 15:49, spir ? wrote: > > Hello, > > > > Is there anywhere some introduction material to the implementation of python lists > > (or to fully dynamic and flexible sequences, in general)? > > > > More precisely, I'd like to know what kind of base data-structure is used > > (linked list, dynamic array, something else -- and in case of array, > how is > > resizing computed). Also, how is "generics" (element are of free type, and > > even heterogeneous) managed? > > Python's 'list' is an array of pointers to `PyObject` ('object' in > Python) and the resizing algorithm keeps the list size such that > "allocated / 2 <= actual <= allocated". When list need to resize, it > overallocates the list slightly over 1.125 times than the requested size > "(newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize". Thank you very much, that's exactly the kind of information I was looking for. One "mystery" is now solved, if I understand correctly: actually it's not really generics, rather the type is a supertype of all relevant ones. > If you're interested in more precise details (since I do omit some, > especially those that seems to be micro-optimization-related), you need > to read the source at > http://code.python.org/hg/trunk/file/e9d930f8b8ff/Objects/listobject.c Right, I'll have a look myself, now. Thank you for the pointer. > > Denis > > > > PS: The reason is I'm trying to understand better the underlying layers of > > magic facilities we use everyday. Starting from about no computer science > > knowledge. I have a working implementation of linked lists in primitive > > langguages ;-) (namely C & Pascal), but rather complicated to my taste; > > and I'm probably overlooking commonly known and used tricks that could > > make the algorithm simpler or more efficient. > > Real life implementation is always hairy, especially as the programmer > cut corners to drench some speed benefit and include many error > checkings. Today is the first time I actually looked at list's > implementation, now I know why people hated C, for every line of real > code, there's three or four lines of error checking code, e.g. to ensure > malloc successfully allocated enough memory or that index access is in > range. Yes, I know that for having played with C a very long time ago. The reason why I started with Pascal, which makes things slightly simpler (esp. freepascal in my case). But maybe even more verbose: about 450 loc for a link list type (with all required operations, sure). Denis ________________________________ vit esse estrany ? spir.wikidot.com From denis.spir at gmail.com Sun May 2 15:10:11 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 2 May 2010 15:10:11 +0200 Subject: [Tutor] python list, right! but concretely? In-Reply-To: <201005021857.42064.steve@pearwood.info> References: <20100502074902.70843655@o> <201005021857.42064.steve@pearwood.info> Message-ID: <20100502151011.507274e7@o> On Sun, 2 May 2010 18:57:41 +1000 Steven D'Aprano wrote: > On Sun, 2 May 2010 03:49:02 pm spir ? wrote: > > Hello, > > > > Is there anywhere some introduction material to the implementation of > > python lists (or to fully dynamic and flexible sequences, in > > general)? More precisely, I'd like to know what kind of base > > data-structure is used (linked list, dynamic array, something else -- > > and in case of array, how is resizing computed). Also, how is > > "generics" (element are of free type, and even heterogeneous) > > managed? > > I'm not sure if this is documented anywhere (other than the source code, > of course) but in CPython lists are implemented as arrays of pointers > to objects. > > Because re-sizing arrays is expensive, the array is over-allocated: the > array is (very approximately) created twice the size needed, to give > room to grow. Then when the array is full, or nearly full, it is > doubled in size again. This gives amortised O(1) appends, at the cost > of being 50% larger than needed on average. This means that append is > nearly always very, very fast, with an occasional, and rare, slow > append when the list resizes. > > Because the list is an array, iteration is very fast and lookups are > O(1). However, deleting from the start or middle of the list is O(N), > as are insertions. If speed is important, and you need fast insertions > and deletions at both the start and end of the list, then you should > use collections.deque; however item lookup for deques is O(N). > > Of course all of this is an implementation detail. Other implementations > are free to make other memory/time tradeoffs, although if their lists > performed very differently, people would complain. Thank you. The reason why I started with linked lists rather than flexible arrays is the problem of deletion & insertion other than at the end. Is the tail of the list after the given element really shifted left or right, or are tricks used to avoid that? (I mean in a typical implementation). I imagined for instance there could be kinds of null placeholders for deleted items (this indeed makes all the rest more complicated but avoids compressing the list after deletion). > > Denis > > > > PS: The reason is I'm trying to understand better the underlying > > layers of magic facilities we use everyday. Starting from about no > > computer science knowledge. I have a working implementation of linked > > lists in primitive langguages ;-) (namely C & Pascal), but rather > > complicated to my taste; and I'm probably overlooking commonly known > > and used tricks that could make the algorithm simpler or more > > efficient. > > The simplest implementation of a linked list in Pascal is something like > this: > > type: > ptr = ^node; > node = record > data: integer; > next: ptr > end; > > Here's a procedure to traverse the list, printing each item: > > {untested} > procedure walk(alist: ptr): > begin > while alist <> nil: > begin > writeln(alist^.data); > alist := alist^.next > end; > end; > > Everything else, I leave as an exercise :) Yes, you're right. But when I said "linked list", I actually meant a kind of type with all needed "methods" ;-) Denis ________________________________ vit esse estrany ? spir.wikidot.com From steve at pearwood.info Sun May 2 16:50:40 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 3 May 2010 00:50:40 +1000 Subject: [Tutor] python list, right! but concretely? In-Reply-To: References: <20100502074902.70843655@o> Message-ID: <201005030050.41551.steve@pearwood.info> On Sun, 2 May 2010 07:44:22 pm Lie Ryan wrote: > Python's 'list' is an array of pointers to `PyObject` ('object' in > Python) and the resizing algorithm keeps the list size such that > "allocated / 2 <= actual <= allocated". When list need to resize, it > overallocates the list slightly over 1.125 times than the requested > size "(newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize". > > If you're interested in more precise details (since I do omit some, > especially those that seems to be micro-optimization-related), you > need to read the source at > http://code.python.org/hg/trunk/file/e9d930f8b8ff/Objects/listobject.c Thanks for the correction Lie. However, I don't understand what is going on in the code. I'm not a C programmer, so I'm struggling to read it, but as far as I can tell the comment in the code and the code itself are not the same. The code calculates an amount to over-allocate the list: new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6); Some experiments in Python shows that the right hand expression varies like this: >>> for i in range(30): ... print ((i >> 3) + (3 if i < 9 else 6)), ... 3 3 3 3 3 3 3 3 4 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 But the comment in the code says: /* This over-allocates proportional to the list size, making room * for additional growth. The over-allocation is mild, but is * enough to give linear-time amortized behavior over a long * sequence of appends() in the presence of a poorly-performing * system realloc(). * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... */ which doesn't look anything like the numbers above. I don't understand what this growth pattern refers to. What have I misunderstood? -- Steven D'Aprano From denis.spir at gmail.com Sun May 2 19:17:18 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 2 May 2010 19:17:18 +0200 Subject: [Tutor] python list, right! but concretely? In-Reply-To: <201005030050.41551.steve@pearwood.info> References: <20100502074902.70843655@o> <201005030050.41551.steve@pearwood.info> Message-ID: <20100502191718.0fa79d0c@o> On Mon, 3 May 2010 00:50:40 +1000 Steven D'Aprano wrote: > On Sun, 2 May 2010 07:44:22 pm Lie Ryan wrote: > > > Python's 'list' is an array of pointers to `PyObject` ('object' in > > Python) and the resizing algorithm keeps the list size such that > > "allocated / 2 <= actual <= allocated". When list need to resize, it > > overallocates the list slightly over 1.125 times than the requested > > size "(newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize". > > > > If you're interested in more precise details (since I do omit some, > > especially those that seems to be micro-optimization-related), you > > need to read the source at > > http://code.python.org/hg/trunk/file/e9d930f8b8ff/Objects/listobject.c > > Thanks for the correction Lie. However, I don't understand what is going > on in the code. I'm not a C programmer, so I'm struggling to read it, > but as far as I can tell the comment in the code and the code itself > are not the same. > > The code calculates an amount to over-allocate the list: > > new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6); > > Some experiments in Python shows that the right hand expression varies > like this: > > >>> for i in range(30): > ... print ((i >> 3) + (3 if i < 9 else 6)), > ... > 3 3 3 3 3 3 3 3 4 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 Right, I don't understand neither. Since i>>8 <=> i/8, the above code is finally a (fast, because of bit-level op) version of: if size < 8: return 3 else: return size/8 + (3 if size < 9 else 6), > But the comment in the code says: > > /* This over-allocates proportional to the list size, making room > * for additional growth. The over-allocation is mild, but is > * enough to give linear-time amortized behavior over a long > * sequence of appends() in the presence of a poorly-performing > * system realloc(). > * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... > */ > > which doesn't look anything like the numbers above. I don't understand > what this growth pattern refers to. What have I misunderstood? I first thought "growth pattern" is to be interpreted as "how the array size grows when it is progressively fed with new items", like when feeding a list in a loop. But I tried to simulate this, and the size sticks at 3. finally, I think what is called in source "new_allocated" is not the new array memory size, but the _difference_. When writing "size = size + new_allocated" instead of "size = new_allocated", It get a growth pattern of: 0 3 6 9 16 24 33 43 54 66 80 96 114 Which is not exactly what is stated in code, but rather similar... def grow(): size = 0 ; count = 0 for count in range(100): if count > size: size = size + (size >> 3) + (3 if size < 9 else 6) print size, Denis ________________________________ vit esse estrany ? spir.wikidot.com From smokefloat at gmail.com Sun May 2 22:44:42 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 2 May 2010 16:44:42 -0400 Subject: [Tutor] Is there a better way to use scientific notation in an equation? Message-ID: In the following code I'm trying to do basic calculations with coulumb's law #Coulombs Law ''' F = (9*(10**9)) * (Q1*Q2) / (d**2) ''' base = 10 Q1mult = raw_input('First enter multiplier of base 10 charge/coloumb(Q1):') Q1exp = raw_input('Now enter exponent of base 10(Q1):') Q1 = int(Q1mult)*(10**int(Q1exp)) Q2mult = raw_input('First enter multiplier of base 10 charge/coloumb(Q2):') Q2exp = raw_input('Now enter exponent of base 10(Q2):') Q2 = int(Q2mult)*(10**int(Q2exp)) d = raw_input('Enter distance of charges/coulumbs from Q1 to Q2:') a = (9*(10**9))*((int(Q1))*(int(Q2)))/((int(d))**2) print a ********************************************************** Q1 and Q2 are to be entered as base ten scientific notation. When I try to input Q1 as raw input, entering in ((2*(10**7)), I get: ValueError: invalid literal for int() with base 10: '((2)*(10**7))' Which is why I broke it down into it's sub-components(i.e. what to multiply the base 10 by[Q1mult] and what exponent to use with the base 10[Q1exp]). Is there a better way to write this, and what would be the best way to insert the scientific notation if not this way. I know there is probably a module, but this is for my own practice. TIA David From steve at pearwood.info Mon May 3 00:55:26 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 3 May 2010 08:55:26 +1000 Subject: [Tutor] Is there a better way to use scientific notation in an equation? In-Reply-To: References: Message-ID: <201005030855.27147.steve@pearwood.info> On Mon, 3 May 2010 06:44:42 am David Hutto wrote: > In the following code I'm trying to do basic calculations with > coulumb's law > > #Coulombs Law > ''' > F = (9*(10**9)) * (Q1*Q2) / (d**2) > ''' > base = 10 > Q1mult = raw_input('First enter multiplier of base 10 > charge/coloumb(Q1):') Q1exp = raw_input('Now enter exponent of base > 10(Q1):') > Q1 = int(Q1mult)*(10**int(Q1exp)) > Q2mult = raw_input('First enter multiplier of base 10 > charge/coloumb(Q2):') Q2exp = raw_input('Now enter exponent of base > 10(Q2):') > Q2 = int(Q2mult)*(10**int(Q2exp)) > d = raw_input('Enter distance of charges/coulumbs from Q1 to Q2:') > > > > a = (9*(10**9))*((int(Q1))*(int(Q2)))/((int(d))**2) > print a > ********************************************************** > > Q1 and Q2 are to be entered as base ten scientific notation. > When I try to input Q1 as raw input, entering in ((2*(10**7)), I get: > > ValueError: invalid literal for int() with base 10: '((2)*(10**7))' ((2)*(10**7)) is not an integer, it is a mathematical expression. Integers look like: 1 -27 37481000005 and similar. Apart from a leading minus sign to make negative numbers, only digits are allowed. The way to enter numbers in scientific notation is to use floats, in exponential form, just like scientific calculators: 2e7 and to change your program to use float instead of int. Also, you have a lot of unnecessary brackets that don't do anything except make it hard to read. This should do it: a = 9e9*float(Q1)*float(Q2)/float(d)**2 -- Steven D'Aprano From eike.welk at gmx.net Mon May 3 01:17:42 2010 From: eike.welk at gmx.net (Eike Welk) Date: Mon, 3 May 2010 01:17:42 +0200 Subject: [Tutor] Is there a better way to use scientific notation in an equation? In-Reply-To: References: Message-ID: <201005030117.42306.eike.welk@gmx.net> On Sunday May 2 2010 22:44:42 David Hutto wrote: > Q1 and Q2 are to be entered as base ten scientific notation. > When I try to input Q1 as raw input, entering in ((2*(10**7)), I get: > > ValueError: invalid literal for int() with base 10: '((2)*(10**7))' > > Which is why I broke it down into it's sub-components(i.e. what to > multiply the base 10 by[Q1mult] and what exponent to use with the base > 10[Q1exp]). > > Is there a better way to write this, and what would be the best way to > insert the > scientific notation if not this way. Maybe this is what you are looking for: In [1]: 1e9 Out[1]: 1000000000.0 In [2]: float(raw_input()) 1e9 Out[2]: 1000000000.0 Eike. From alan.gauld at btinternet.com Mon May 3 01:58:14 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 3 May 2010 00:58:14 +0100 Subject: [Tutor] python list, right! but concretely? References: <20100502074902.70843655@o> <201005030050.41551.steve@pearwood.info> <20100502191718.0fa79d0c@o> Message-ID: "spir ?" wrote > ...When writing "size = size + new_allocated" instead of > "size = new_allocated", It get a growth pattern of: > 0 3 6 9 16 24 33 43 54 66 80 96 114 > Which is not exactly what is stated in code, but rather similar... You mean like this line in the source?: new_allocated += newsize; Alan G. From paradox at pobox.com Mon May 3 07:16:17 2010 From: paradox at pobox.com (Thomas C. Hicks) Date: Mon, 3 May 2010 13:16:17 +0800 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: Message-ID: <20100503131617.504a1ea4@midgel> I am using Python 2.6.4 in Ubuntu. Since I use Ubuntu (with its every 6 months updates) and want to learn Python I have been working on a post-install script that would get my Ubuntu system up and running with my favorite packages quickly. Basically the script reads a text file, processes the lines in the file and then does an apt-get for each line that is a package name. The text file looks like this: %Comment introducing the next block of packages %Below are the packages for using Chinese on the system %Third line of comment because I am a verbose guy! ibus-pinyin ibus-table-wubi language-pack-zh-hans etc. I read the lines of the file into a list for processing. To strip out the comments lines I am using something like this: for x in lines: if x.startswith('%'): lines.remove(x) This works great for all incidents of comments that are only one line. Sometimes I have blocks of comments that are more than one line and find that the odd numbered lines are stripped from the list but not the even numbered lines (i.e in the above block the line "%Below are the ..." line would not be stripped out of the list). Obviously there is something I don't understand about processing the items in the list and using the string function x.startswith() and the list function list.remove(). Interestingly if I put in "print x" in place of the lines.remove(x) line I get all the comment lines printed. Can anyone point me in the right direction? thomas From stefan_ml at behnel.de Mon May 3 08:49:03 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 03 May 2010 08:49:03 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: <20100503131617.504a1ea4@midgel> References: <20100503131617.504a1ea4@midgel> Message-ID: Thomas C. Hicks, 03.05.2010 07:16: > %Comment introducing the next block of packages > %Below are the packages for using Chinese on the system > %Third line of comment because I am a verbose guy! > ibus-pinyin > ibus-table-wubi > language-pack-zh-hans > > etc. > > I read the lines of the file into a list for processing. To strip > out the comments lines I am using something like this: > > for x in lines: > if x.startswith('%'): > lines.remove(x) > > This works great for all incidents of comments that are only one > line. Sometimes I have blocks of comments that are more than one > line and find that the odd numbered lines are stripped from the list > but not the even numbered lines You are modifying the list during iteration, so the size changes and the iterator gets diverted. Don't remove the line, just skip over it, e.g. def read_package_names(open_text_file): """Read lines, strip any comments and return only the package names found. """ for line in open_text_file: if '%' in line: # take only the part before the '%' line = line.split('%', 1)[0] line = line.strip() if line: yield line with open('packages.txt') as f: for package_name in read_package_names(f): print package_name Stefan From andreengels at gmail.com Mon May 3 09:35:52 2010 From: andreengels at gmail.com (Andre Engels) Date: Mon, 3 May 2010 09:35:52 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: <20100503131617.504a1ea4@midgel> References: <20100503131617.504a1ea4@midgel> Message-ID: On Mon, May 3, 2010 at 7:16 AM, Thomas C. Hicks wrote: > I am using Python 2.6.4 in Ubuntu. ?Since I use Ubuntu (with its every > 6 months updates) and want to learn Python I have been working on a > post-install script that would get my Ubuntu system up and running with > my favorite packages quickly. ?Basically the script reads a text file, > processes the lines in the file and then does an apt-get for each line > that is a package name. ?The text file looks like this: > > %Comment introducing the next block of packages > %Below are the packages for using Chinese on the system > %Third line of comment because I am a verbose guy! > ibus-pinyin > ibus-table-wubi > language-pack-zh-hans > > etc. > > I read the lines of the file into a list for processing. ?To strip > out the comments lines I am using something like this: > > for x in lines: > ? ?if x.startswith('%'): > ? ? ? ?lines.remove(x) > > This works great for all incidents of comments that are only one > line. ?Sometimes I have blocks of comments that are more than one > line and find that the odd numbered lines are stripped from the list > but not the even numbered lines (i.e in the above block the line > "%Below are the ..." line would not be stripped out of the > list). > > Obviously there is something I don't understand about processing > the items in the list and using the string function x.startswith() and > the list function list.remove(). Interestingly if I put in "print x" > in place of the lines.remove(x) line I get all the comment lines > printed. > > Can anyone point me in the right direction? Don't change the list that you are iterating over. As you have found, it leads to (to most) unexpected results. What's going on, is that Python first checks the first line, finds that it needs to be deleted, deletes it, then goes to the second line; however, at that time the original third line has become the second line, so the original second line is not checked. There are several ways to resolve this problem; to me the most obvious are: 1. Get the lines from a copy of the list rather than the list itself: # use lines[:] rather than lines to actually make a copy rather than a new name for the same object linesCopy = lines[:] for x in linesCopy: if x.startswith('%'): lines.remove(x) 2. First get the lines to remove, and remove them afterward: linesToDelete = [] for x in lines: if x.startswith('%'): linesToDelete.append(x) for x in linesToDelete: lines.remove(x) If that looks a bit clumsy, use a generator expression: linesToDelete = [x for x in lines if x.startswith('%')] for x in linesToDelete: lines.remove(x) which idiomatically should probably become: for x in [y for y in lines if y.startswith('%')]: lines.remove(x) -- Andr? Engels, andreengels at gmail.com From rabidpoobear at gmail.com Mon May 3 10:19:22 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 3 May 2010 03:19:22 -0500 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: > If that looks a bit clumsy, use a generator expression: > > linesToDelete = [x for x in lines if x.startswith('%')] > for x in linesToDelete: > ? lines.remove(x) > > which idiomatically should probably become: > > for x in [y for y in lines if y.startswith('%')]: > ? lines.remove(x) > > Hmm, why not lines = [line for line in lines if not line.strip().startswith('%')] note that for a significantly large file this might be sorta slow (since it's an extra iteration over the list), whereas the 'breaking out of the loop on comment lines' approach may be a little faster. it's probably not significant for this application though. Also, yeah, try really hard not to modify lists you're iterating over. In almost every case, it's not actually what you need to do. It's akin to people wanting to create variable names from strings, before they discover that dictionaries are the correct response. Hope that helps, -Luke From rabidpoobear at gmail.com Mon May 3 10:27:08 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 3 May 2010 03:27:08 -0500 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: > > You are modifying the list during iteration, so the size changes and the > iterator gets diverted. Don't remove the line, just skip over it, e.g. > > ? ?def read_package_names(open_text_file): > ? ? ? ?"""Read lines, strip any comments and return only the > ? ? ? ?package names found. > ? ? ? ?""" > ? ? ? ?for line in open_text_file: > ? ? ? ? ? ?if '%' in line: > ? ? ? ? ? ? ? ?# take only the part before the '%' > ? ? ? ? ? ? ? ?line = line.split('%', 1)[0] > ? ? ? ? ? ?line = line.strip() > ? ? ? ? ? ?if line: > ? ? ? ? ? ? ? ?yield line And here if you wanted all the text on the line before the first '%' as a list comprehension it would be something like this: lines = [line[:line.index('%')] for line in lines if not line.strip().startswith('%')] > > ? ?with open('packages.txt') as f: > ? ? ? ?for package_name in read_package_names(f): > ? ? ? ? ? ?print package_name > What's this bizarre syntax? I thought they changed for loop interations so that if you did for line in open('packages.txt'): .... etc... it would automatically close the file handle after the loop terminated. Have I been wrong this whole time? -Luke From andreengels at gmail.com Mon May 3 10:28:02 2010 From: andreengels at gmail.com (Andre Engels) Date: Mon, 3 May 2010 10:28:02 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: On Mon, May 3, 2010 at 10:19 AM, Luke Paireepinart wrote: > Hmm, why not > lines = [line for line in lines if not line.strip().startswith('%')] I knew I missed something.... -- Andr? Engels, andreengels at gmail.com From stefan_ml at behnel.de Mon May 3 10:50:41 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 03 May 2010 10:50:41 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: Luke Paireepinart, 03.05.2010 10:27: > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: >> >> You are modifying the list during iteration, so the size changes and the >> iterator gets diverted. Don't remove the line, just skip over it, e.g. >> >> def read_package_names(open_text_file): >> """Read lines, strip any comments and return only the >> package names found. >> """ >> for line in open_text_file: >> if '%' in line: >> # take only the part before the '%' >> line = line.split('%', 1)[0] >> line = line.strip() >> if line: >> yield line > > And here if you wanted all the text on the line before the first '%' > as a list comprehension it would be something like this: > > lines = [line[:line.index('%')] for line in lines if not > line.strip().startswith('%')] Readability counts, though. And I assumed that the OP wanted the package name, without any potentially surrounding whitespace. >> with open('packages.txt') as f: >> for package_name in read_package_names(f): >> print package_name > > What's this bizarre syntax? Look it up in the docs, it's called "with statement". Its purpose here is to make sure the file is closed after the execution of the statement's body, regardless of any errors that may occur while running the loop. > I thought they changed for loop interations so that if you did > for line in open('packages.txt'): > .... etc... > > it would automatically close the file handle after the loop terminated. > Have I been wrong this whole time? Yes. The fact that the file is automatically closed after the loop is an implementation detail of CPython that does not apply in other Python implementations. Stefan From stefan_ml at behnel.de Mon May 3 10:55:11 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 03 May 2010 10:55:11 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: Luke Paireepinart, 03.05.2010 10:27: > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: >> line = line.split('%', 1)[0] > > lines = [line[:line.index('%')] for line in ... Agreed that line = line[:line.index('%')] is slightly more readable than line = line.split('%', 1)[0] Stefan From denis.spir at gmail.com Mon May 3 12:13:44 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Mon, 3 May 2010 12:13:44 +0200 Subject: [Tutor] python list, right! but concretely? In-Reply-To: References: <20100502074902.70843655@o> <201005030050.41551.steve@pearwood.info> <20100502191718.0fa79d0c@o> Message-ID: <20100503121344.06292e75@o> On Mon, 3 May 2010 00:58:14 +0100 "Alan Gauld" wrote: > > "spir ?" wrote > > > ...When writing "size = size + new_allocated" instead of > > "size = new_allocated", It get a growth pattern of: > > 0 3 6 9 16 24 33 43 54 66 80 96 114 > > Which is not exactly what is stated in code, but rather similar... > > You mean like this line in the source?: > > new_allocated += newsize; > > Alan G. No, sorry, I meant the comment listing the "growth pattern". Denis ________________________________ vit esse estrany ? spir.wikidot.com From rabidpoobear at gmail.com Mon May 3 12:18:41 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 3 May 2010 05:18:41 -0500 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: > Luke Paireepinart, 03.05.2010 10:27: >> What's this bizarre syntax? > > Look it up in the docs, it's called "with statement". Its purpose here is to > make sure the file is closed after the execution of the statement's body, > regardless of any errors that may occur while running the loop. > > >> I thought they changed for loop interations so that if you did >> for line in open('packages.txt'): >> ? ? .... etc... >> >> it would automatically close the file handle after the loop terminated. >> Have I been wrong this whole time? > > Yes. The fact that the file is automatically closed after the loop is an > implementation detail of CPython that does not apply in other Python > implementations. > So why is it an implementation detail? Why is it not universally like that? You never have an explicit reference to the file handle. When it gets garbage-collected after the loop it should get rid of the file handle. I mean, where is the line between 'implementation details' and 'language features'? What reason is there to make lists mutable but strings immutable? Why aren't strings mutable, or lists immutable? It all seems pretty arbitrary to me. Also, the with syntax is kind of ugly. So is the 'for/else' syntax. They are not as intuitively clear as most of the other language constructs to me. -Luke From denis.spir at gmail.com Mon May 3 12:20:35 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Mon, 3 May 2010 12:20:35 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: <20100503122035.3e304a99@o> On Mon, 03 May 2010 10:55:11 +0200 Stefan Behnel wrote: > Luke Paireepinart, 03.05.2010 10:27: > > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: > >> line = line.split('%', 1)[0] > > > > lines = [line[:line.index('%')] for line in ... > > Agreed that > > line = line[:line.index('%')] > > is slightly more readable than > > line = line.split('%', 1)[0] But, to my eyes, endpos = line.index('%') line = line[:endpos] is even more readable ;-) Denis ________________________________ vit esse estrany ? spir.wikidot.com From steve at pearwood.info Mon May 3 12:21:37 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 3 May 2010 20:21:37 +1000 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: <201005032021.37587.steve@pearwood.info> On Mon, 3 May 2010 05:35:52 pm Andre Engels wrote: > Don't change the list that you are iterating over. As you have found, > it leads to (to most) unexpected results. Or if you absolutely have to change the list in place, iterate over it *backwards* (starting at the end, and moving towards the beginning), so that the changes only happen in the part of the list you've already seen. -- Steven D'Aprano From rabidpoobear at gmail.com Mon May 3 12:25:50 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 3 May 2010 05:25:50 -0500 Subject: [Tutor] Iterating through a list of strings In-Reply-To: <20100503122035.3e304a99@o> References: <20100503131617.504a1ea4@midgel> <20100503122035.3e304a99@o> Message-ID: 2010/5/3 spir ? : > On Mon, 03 May 2010 10:55:11 +0200 > Stefan Behnel wrote: > >> Luke Paireepinart, 03.05.2010 10:27: >> > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: >> >> ? ? ? ? ? ? ? ? line = line.split('%', 1)[0] >> > >> > lines = [line[:line.index('%')] for line in ... >> >> Agreed that >> >> ? ? ?line = line[:line.index('%')] >> >> is slightly more readable than >> >> ? ? ?line = line.split('%', 1)[0] > > But, to my eyes, > ? ?endpos = line.index('%') > ? ?line = line[:endpos] > is even more readable ;-) > I prefer this: search_string_percent_sign = '%' end_position_in_line = line.index(search_string_percent_sign) temp = line[:end_position_in_line] line = temp -Luke From stefan_ml at behnel.de Mon May 3 13:08:18 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 03 May 2010 13:08:18 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: Luke Paireepinart, 03.05.2010 12:18: > On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: >> Luke Paireepinart, 03.05.2010 10:27: >>> I thought they changed for loop interations so that if you did >>> for line in open('packages.txt'): >>> .... etc... >>> >>> it would automatically close the file handle after the loop terminated. >>> Have I been wrong this whole time? >> >> Yes. The fact that the file is automatically closed after the loop is an >> implementation detail of CPython that does not apply in other Python >> implementations. > > So why is it an implementation detail? Why is it not universally like that? Because it cannot be done like this without implementation details that are inherent to the CPython interpreter, namely: reference counting. The for-loop cannot know where the object reference came from that it is just trying to iterate over. Only after the for-loop dropped the reference, the reference counting mechanism determines that it was the last reference and cleans up the file. When the file is cleaned up, the code in the file object determines that it wasn't closed yet and closes the file. Neither the for-loop, nor the reference-counting mechanism, nor the garbage collector know that this is something that needs to be done. Only the file object knows that. > You never have an explicit reference to the file handle. When it gets > garbage-collected after the loop it should get rid of the file handle. But you cannot know when that will be. It is not an implementation detail that the file gets closed when the file object is freed. It *is*, however, an implementation detail, when this freeing happens. It can take an arbitrarily long time, so it is usually in your own interest to close the file yourself, if only to get rid of the file handle. Imagine you had opened the file for writing, such as in open("myfile.txt", 'w').write('some text') Here, you have no guarantee that the text has been written to the file at any point in the runtime of your program. Likely not what you want. > I mean, where is the line between 'implementation details' and > 'language features'? See the docs. > What reason is there to make lists mutable but > strings immutable? Performance considerations and use case specific design. > Why aren't strings mutable, or lists immutable? What would be the use case of an immutable list, as opposed to a tuple? How would you use mutable strings in a dictionary? Stefan From steve at pearwood.info Mon May 3 13:15:07 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 3 May 2010 21:15:07 +1000 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: Message-ID: <201005032115.08158.steve@pearwood.info> On Mon, 3 May 2010 08:18:41 pm Luke Paireepinart wrote: > On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: > > Luke Paireepinart, 03.05.2010 10:27: > >> What's this bizarre syntax? > > > > Look it up in the docs, it's called "with statement". Its purpose > > here is to make sure the file is closed after the execution of the > > statement's body, regardless of any errors that may occur while > > running the loop. This holds for file objects, but the with statement is far more powerful and general than that. It's a generalisation of something like: setup() try: block() finally: teardown() (although I admit I haven't really used the with statement enough to be completely sure about what it does). It's not just for files. > >> I thought they changed for loop interations so that if you did > >> for line in open('packages.txt'): > >> ? ? .... etc... > >> > >> it would automatically close the file handle after the loop > >> terminated. Have I been wrong this whole time? > > > > Yes. The fact that the file is automatically closed after the loop > > is an implementation detail of CPython that does not apply in other > > Python implementations. > > So why is it an implementation detail? Why is it not universally > like that? You never have an explicit reference to the file handle. > When it gets garbage-collected after the loop it should get rid of > the file handle. Because not all implementations use reference counting. Jython (Python written in Java) uses the Java garbage collector, and so it doesn't close files until the application shuts down. IronPython uses .Net, and so it does whatever .Net does. Even in CPython, if you have a reference loop, Python can't close the file until the garbage collector runs, and it might not run immediately. It might not run at all, if the object has a __del__ method, or if the caller has turned it off. > I mean, where is the line between 'implementation details' and > 'language features'? What reason is there to make lists mutable but > strings immutable? Why aren't strings mutable, or lists immutable? These are design decisions. The language creator made that decision, and having made that decision, the immutability of strings is a language feature. For instance, some years ago Python's list.sort method was "unstable". Stable sorting is very desirable, because it allows you to do something like this: sort by family name sort by first name sort by address and have intuitively correct results. Unstable sorts don't, subsequent sorts may "mess up" the order from earlier sorts. But because stable sorting is harder to get right without a serious slow-down, Python (the language) didn't *promise* that sorting was stable. Consequently implementations were free to make their own choices. For many years, CPython's sort was stable for small lists but unstable for large lists. Then Tim Peters invented a new sort algorithm which was not only faster than Python's already blindingly fast sort, but was also stable. So for at least one release (2.3, if I remember correctly) CPython's sort was stable but Python the language made no promises that it would remain stable forever. Perhaps it would turn out that Tim Peter's new sort was buggy, and had to be replaced? Or that Tim's testing was flawed, and it was actually slower than the old one? Finally, I think in Python 2.4, it was decided that the new stable sort had proven itself enough that Python (the language) could afford to promise sorting would be stable. Any language wanting to call itself Python would have to provide a stable sort method. Failure to be stable would count as a bug, and not a quality of implementation issue. Python the language promises that files will be automatically closed at some point after you have finished with them. That is a language feature. Exactly when this happens is a quality of implementation issue. The garbage collectors used by Java and .Net prioritise other things over closing files, e.g. performance with multiple CPUs. -- Steven D'Aprano From denis.spir at gmail.com Mon May 3 14:06:08 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Mon, 3 May 2010 14:06:08 +0200 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: <20100503140608.105594c3@o> On Mon, 03 May 2010 13:08:18 +0200 Stefan Behnel wrote: > > Why aren't strings mutable, or lists immutable? > > What would be the use case of an immutable list, as opposed to a tuple? How > would you use mutable strings in a dictionary? [I'm not totally sure of the following, take it with some doubt.] Lua does it in a rather different manner for its tables. Keys are compared by reference, not value. But immutable thingies are guaranteed to be unique. (Meaning eg there are no 2 objects equal to "abc".) This brings the following advantages: * A unique hash func, and fast since the reference (address) is an integer. * Any object can be a key, since the _reference_ is indeed immutable ;-). Surprisingly, this works fine and does the right thing :-) When checking a key: * If it's a value (mutable), unicity ensures that equality of reference also means equality of value. * If it's a "thing" (immutable), like in python comparison in fact compares references. The fact that the actual keys are references allows mutable objects to be used as keys. The price is indeed a lookup at immutable object creation (no idea how this is done, but I've heard it's efficient). Denis ________________________________ vit esse estrany ? spir.wikidot.com From thegreentealeaf at gmail.com Mon May 3 19:10:03 2010 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Mon, 3 May 2010 19:10:03 +0200 Subject: [Tutor] Pattern for using multiple windows in Tkinter Message-ID: I've just started to play with Tkinter and can't really figure out the typical pattern of creating an app with several windows - I tried to find some resources on the web but failed to find how to do this. Here is an attempt to explain using code class MyWindowClass: def __init__(self): self._window = Toplevel() # Is this the correct pattern for creating new windows?? ... first = MyWindowClass() second = MyWindowClass() This works but it also creates the "root" window ... What is the "correct" way of coding an app where I define a class that represents one window and where I want to be able to open multiple windows of that class. I hope you understand my question :) -- The Green Tea Leaf thegreentealeaf at gmail.com thegreentealeaf.blogspot.com From python at bdurham.com Mon May 3 19:19:23 2010 From: python at bdurham.com (python at bdurham.com) Date: Mon, 03 May 2010 13:19:23 -0400 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: <1272907163.17155.1373153603@webmail.messagingengine.com> > Agreed that > > line = line[:line.index('%')] > > is slightly more readable than > > line = line.split('%', 1)[0] How about: line = line.partition('%')[0] partition() works even if '%' isn't present. The index() and split() techniques raise exceptions if '%' isn't present. Malcolm From alan.gauld at btinternet.com Mon May 3 19:28:27 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 3 May 2010 18:28:27 +0100 Subject: [Tutor] Pattern for using multiple windows in Tkinter References: Message-ID: "The Green Tea Leaf" wrote > I've just started to play with Tkinter and can't really figure out the > typical pattern of creating an app with several windows - I tried to > find some resources on the web but failed to find how to do this. Here > is an attempt to explain using code > > class MyWindowClass: > def __init__(self): > self._window = Toplevel() # Is this the correct pattern for Rather than having an instance of TopLevel inside and delegating everything I'd probably go for inheriting from TopLevel, because in this case you really do want your class to be a kind of TopLevel, rather than just having a Toplevel. And then you need to add a parent parameter to init() and when you instantiate it pass the top Tk() as the parent. > first = MyWindowClass() > second = MyWindowClass() > > This works but it also creates the "root" window ... I think you always need a root window, the trick is to hide it - maybe by making it very small and/or unmapping it from the screen. > What is the "correct" way of coding an app where I define a class that > represents one window and where I want to be able to open multiple > windows of that class. A subclass of toplevel should do that but I confess I haven't actually done it. I might have a go later just for fun! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From thegreentealeaf at gmail.com Tue May 4 07:17:25 2010 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 4 May 2010 07:17:25 +0200 Subject: [Tutor] Pattern for using multiple windows in Tkinter In-Reply-To: References: Message-ID: OK, thanks. From dextrous85 at gmail.com Tue May 4 11:47:44 2010 From: dextrous85 at gmail.com (vishwajeet singh) Date: Tue, 4 May 2010 15:17:44 +0530 Subject: [Tutor] datetime.strptime not matching timezone in format Message-ID: Dear All, I am trying to convert date string to datetime using datetime.strptime and when there is timezone part in string conversion is throwing errors. Here is what I am doing string_date = somedate.strftime("%a, %d %b %Y %H:%M:%S %Z") and than later in some other part of code actual_date = datetime.strptime(string_date,"%a, %d %b %Y %H:%M:%S %Z") I am getting following error for this conversion "*time data 'Tue, 04 May 2010 14:59:45 +05:30' does not match format '%a, %d %b %Y %H:%M:%S %Z'*" All formats without timezone works perfectly fine. -- Vishwajeet Singh +91-9657702154 | dextrous85 at gmail.com | http://bootstraptoday.com Twitter: http://twitter.com/vishwajeets | LinkedIn: http://www.linkedin.com/in/singhvishwajeet -------------- next part -------------- An HTML attachment was scrubbed... URL: From ideamonk at gmail.com Tue May 4 12:47:31 2010 From: ideamonk at gmail.com (Abhishek Mishra) Date: Tue, 4 May 2010 16:17:31 +0530 Subject: [Tutor] datetime.strptime not matching timezone in format In-Reply-To: References: Message-ID: %Z stands for time zone name in letters Eg. IST or EDT or GMT, so it would fail to parse +05:30 However I do not have a solution to your problem but a footnote as hint - http://docs.python.org/library/time.html#id1 -- Abhishek On Tue, May 4, 2010 at 3:17 PM, vishwajeet singh wrote: > Dear All, > > I am trying to convert date string to datetime using datetime.strptime and > when there is timezone part in string conversion is throwing errors. > > Here is what I am doing > string_date = somedate.strftime("%a, %d %b %Y %H:%M:%S %Z") > > and than later in some other part of code > actual_date = datetime.strptime(string_date,"%a, %d %b %Y %H:%M:%S %Z") > > I am getting following error for this conversion "*time data 'Tue, 04 May > 2010 14:59:45 +05:30' does not match format '%a, %d %b %Y %H:%M:%S %Z'*" > > All formats without timezone works perfectly fine. > > -- > Vishwajeet Singh > +91-9657702154 | dextrous85 at gmail.com | http://bootstraptoday.com > Twitter: http://twitter.com/vishwajeets | LinkedIn: > http://www.linkedin.com/in/singhvishwajeet > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Tue May 4 12:52:13 2010 From: dextrous85 at gmail.com (vishwajeet singh) Date: Tue, 4 May 2010 16:22:13 +0530 Subject: [Tutor] datetime.strptime not matching timezone in format In-Reply-To: References: Message-ID: On Tue, May 4, 2010 at 4:17 PM, Abhishek Mishra wrote: > %Z stands for time zone name in letters Eg. IST or EDT or GMT, so it would > fail to parse +05:30 > > Ahh...ok thanks for pointing that out but this is kind of stranges as I am using same format to convert than why it's not converting it in IST ? However I do not have a solution to your problem but a footnote as hint - > http://docs.python.org/library/time.html#id1 > > Thanks for the link I am browsing this from morning not much of help there. > -- Abhishek > > On Tue, May 4, 2010 at 3:17 PM, vishwajeet singh wrote: > >> Dear All, >> >> I am trying to convert date string to datetime using datetime.strptime and >> when there is timezone part in string conversion is throwing errors. >> >> Here is what I am doing >> string_date = somedate.strftime("%a, %d %b %Y %H:%M:%S %Z") >> >> and than later in some other part of code >> actual_date = datetime.strptime(string_date,"%a, %d %b %Y %H:%M:%S %Z") >> >> I am getting following error for this conversion "*time data 'Tue, 04 May >> 2010 14:59:45 +05:30' does not match format '%a, %d %b %Y %H:%M:%S %Z'*" >> >> All formats without timezone works perfectly fine. >> >> -- >> Vishwajeet Singh >> +91-9657702154 | dextrous85 at gmail.com | http://bootstraptoday.com >> Twitter: http://twitter.com/vishwajeets | LinkedIn: >> http://www.linkedin.com/in/singhvishwajeet >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -- Vishwajeet Singh +91-9657702154 | dextrous85 at gmail.com | http://bootstraptoday.com Twitter: http://twitter.com/vishwajeets | LinkedIn: http://www.linkedin.com/in/singhvishwajeet -------------- next part -------------- An HTML attachment was scrubbed... URL: From ideamonk at gmail.com Tue May 4 13:07:08 2010 From: ideamonk at gmail.com (Abhishek Mishra) Date: Tue, 4 May 2010 16:37:08 +0530 Subject: [Tutor] datetime.strptime not matching timezone in format In-Reply-To: References: Message-ID: On Tue, May 4, 2010 at 4:22 PM, vishwajeet singh wrote: > > On Tue, May 4, 2010 at 4:17 PM, Abhishek Mishra wrote: > >> %Z stands for time zone name in letters Eg. IST or EDT or GMT, so it would >> fail to parse +05:30 >> >> Ahh...ok thanks for pointing that out but this is kind of stranges as I > am using same format to convert than why it's not converting it in IST ? > > Interestingly, I guess the timezone environment variable is not set on my system . >>> datetime.today() datetime.datetime(2010, 5, 4, 16, 25, 28, 381390) >>> d = datetime.today() >>> string_date = d.strftime("%a, %d %b %Y %H:%M:%S %Z") >>> string_date 'Tue, 04 May 2010 16:25:32 ' >>> ^^ notice absence of time zone An interesting discussion on same issue - http://old.nabble.com/strptime-and-timezones-td18967515.html >>> import time >>> time.tzname ('IST', 'IST') -------------- next part -------------- An HTML attachment was scrubbed... URL: From paradox at pobox.com Tue May 4 14:00:00 2010 From: paradox at pobox.com (Thomas C. Hicks) Date: Tue, 4 May 2010 20:00:00 +0800 Subject: [Tutor] Iterating through a list of strings In-Reply-To: References: <20100503131617.504a1ea4@midgel> Message-ID: <20100504200000.72e7ad6d@midgel> Wow, this is great! I appreciate all the pointers, lots to keep learning here. thomas From eike.welk at gmx.net Wed May 5 16:17:42 2010 From: eike.welk at gmx.net (Eike Welk) Date: Wed, 5 May 2010 16:17:42 +0200 Subject: [Tutor] Write a programming language! (Was: Iterating through a list of strings) In-Reply-To: <20100503131617.504a1ea4@midgel> References: <20100503131617.504a1ea4@midgel> Message-ID: <201005051617.42569.eike.welk@gmx.net> Hello Thomas! On Monday May 3 2010 07:16:17 Thomas C. Hicks wrote: > I am using Python 2.6.4 in Ubuntu. Since I use Ubuntu (with its every > 6 months updates) and want to learn Python I have been working on a > post-install script that would get my Ubuntu system up and running with > my favorite packages quickly. Basically the script reads a text file, > processes the lines in the file and then does an apt-get for each line > that is a package name. The text file looks like this: > > %Comment introducing the next block of packages > %Below are the packages for using Chinese on the system > %Third line of comment because I am a verbose guy! > ibus-pinyin > ibus-table-wubi > language-pack-zh-hans > > etc. Write a programming language! Experienced programmers know, that every file format will eventually evolve into a fully featured programming language. Therefore you should accept the laws of nature, and implement your file format as a programming language from the start. You don't have to start from scratch though. I have written a rough framework for a special purpose language tailored to your needs. The code is here: http://pastebin.com/MVdFW3a9 To run a program, put it into a string, and call the function: execute_program_str( program_text ) This little program shows all of the language's few features: %Call operator (function) apt-get with multiple arguments apt-get ibus-pinyin ibus-table-wubi "any letters can be here" %Operators can be joined into a pipeline: print | nop | cat foo bar baz %There are brackets to group sub-expressions: print first (cat foo bar baz) boo boum You may ask: Is this a joke? Well it kind of is. I wanted to find out how long it would take me to write a very simpleminded, but usable language. It took me 8 hours. Much longer than I had anticipated, even though the concept of the language is severely limited. Extending the language to have (for, while) loops is impossible without completely changing the concept. All computations are done in the parser's parse actions, there is no abstract syntax tree or byte-code. The parse actions are however only called once, when the parser recognizes the associated pattern in the text. Extending the language so that you can define new functions should be possible, but requires some thinking. Pipelines could be converted on the fly to lambdas. Partial function application (functools.partial) could be used to create new, special purpose versions of more general built in functions. Partial function application could also be used to express "if" statements IMHO. Alternatively you could use a shell script to do your additional installation tasks. This is IMHO what the shell is good at. Eike. From wprins at gmail.com Thu May 6 01:06:50 2010 From: wprins at gmail.com (Walter Prins) Date: Thu, 06 May 2010 00:06:50 +0100 Subject: [Tutor] Iterating through a list of strings In-Reply-To: <20100503131617.504a1ea4@midgel> References: <20100503131617.504a1ea4@midgel> Message-ID: <4BE1FA0A.2010800@gmail.com> On 03/05/10 06:16, Thomas C. Hicks wrote: > I am using Python 2.6.4 in Ubuntu. Since I use Ubuntu (with its every > 6 months updates) and want to learn Python I have been working on a > post-install script that would get my Ubuntu system up and running with > my favorite packages quickly. As an aside, you don't particularly need to write a Python script for what you want from scratch, you could look at this script named "dpkg-origins" which is also written in Python and is related to what you want (it helps dump all installed packages on an existing system): http://goonmill.org/static/dpkg-origins It basically allows you to do this...: |dpkg-origins > selections.txt| ... which produces a file listing all the packages installed on a system. Obviously you can edit this file manually if you so choose. Then, you can use the file on a freshly installed machine to reinstall all the packages using this pipeline command (here you don't need Python or a script as such): | cat selections.txt | sudo dpkg --set-selections && sudo apt-get -u dselect-upgrade | Not meaning to spoil your fun, but just thought I'd point all that out... Regards Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From garry.willgoose at newcastle.edu.au Thu May 6 01:12:24 2010 From: garry.willgoose at newcastle.edu.au (Garry Willgoose) Date: Thu, 6 May 2010 09:12:24 +1000 Subject: [Tutor] portability of pickle and shelve across platforms and different python versions? Message-ID: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> I have seen conflicting info on this on the web and in the tutor archive and the docs don't seem to address it explicitly. How portable are files containing pickle'd and shelve'd data? I'm thinking issues like simply O/S portability, through big-end/little-end hardware (for floats/integers which I use a lot), and then for unicode/non-unicode string, 64/32 bit and V2.6/V3.1 implementations of python. Does the version of the encoder in pickle make any difference for this? One post I've seen suggests that as long as the file is opened binary (ie. 'b') all should be well for platform independence. My core question if I give a pickled file to somebody else can i guarantee they can read/load it OK. The other person will be using exactly the same python code to open it as used to create it. ==================================================================== Prof Garry Willgoose, Australian Professorial Fellow in Environmental Engineering, Director, Centre for Climate Impact Management (C2IM), School of Engineering, The University of Newcastle, Callaghan, 2308 Australia. Centre webpage: www.c2im.org.au Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574 (Fri PM-Mon) FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal and Telluric) Env. Engg. Secretary: (International) +61 2 4921 6042 email: garry.willgoose at newcastle.edu.au; g.willgoose at telluricresearch.com email-for-life: garry.willgoose at alum.mit.edu personal webpage: www.telluricresearch.com/garry ==================================================================== "Do not go where the path may lead, go instead where there is no path and leave a trail" Ralph Waldo Emerson ==================================================================== From damontimm at gmail.com Thu May 6 02:37:20 2010 From: damontimm at gmail.com (Damon Timm) Date: Wed, 5 May 2010 20:37:20 -0400 Subject: [Tutor] Newbie & Unittest ... Message-ID: Hi - am trying to write some unit tests for my little python project - I had been hard coding them when necessary here or there but I figured it was time to try and learn how to do it properly. I've read over Python's guide (http://docs.python.org/library/unittest.html) but I am having a hard time understanding how I can apply it *properly* to my first test case ... What I am trying to do is straightforward, I am just not sure how to populate the tests easily. Here is what I want to accomplish: # code import unittest from mlc.filetypes import * # the module I am testing # here are the *correct* key, value pairs I am testing against TAG_VALUES = ( ('title', 'Christmas Waltz'), ('artist', 'Damon Timm'), ('album', 'Homemade'), ) # list of different file types that I want to test my tag grabbing capabilities # the tags inside these files are set to match my TAG_VALUES # I want to make sure my code is extracting them correctly FILES = ( FLACFile('data/lossless/01 - Christmas Waltz.flac'), MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), ) class TestFiles(unittest.TestCase): # this is the basic test def test_values(self): '''see if values from my object match what they should match''' for file in FILES: for k, v in TAG_VALUES: self.assertEqual(self.file.tags[k], v) This test works, however, it only runs as *one* test (which either fails or passes) and I want it to run as 12 different tests (three for each file type) and be able to see which key is failing for which file type. I know I could write them all out individually but that seems unnecessary. I suspect my answer lies in the Suites but I can't wrap my head around it. Thanks! Damon From Art at DrKendall.org Thu May 6 14:06:18 2010 From: Art at DrKendall.org (Art Kendall) Date: Thu, 06 May 2010 08:06:18 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? Message-ID: <4BE2B0BA.6030608@DrKendall.org> I am running Windows 7 64bit Home premium. with quad cpus and 8G memory. I am using Python 2.6.2. I have all the Federalist Papers concatenated into one .txt file. I want to prepare a file with a row for each paper and a column for each term. The cells would contain the count of a term in that paper. In the original application in the 1950's 30 single word terms were used. I can now use NoteTab to get a list of all the 8708 separate words in allWords.txt. I can then use that data in statistical exploration of the set of texts. I have the python program(?) syntax(?) script(?) below that I am using to learn PYTHON. The comments starting with "later" are things I will try to do to make this more useful. I am getting one step at at time to work It works when the number of terms in the term list is small e.g., 10. I get a file with the correct number of rows (87) and count columns (10) in termcounts.txt. The termcounts.txt file is not correct when I have a larger number of terms, e.g., 100. I get a file with only 40 rows and the correct number of columns. With 8700 terms I get only 40 rows I need to be able to have about 8700 terms. (If this were FORTRAN I would say that the subscript indices were getting scrambled.) (As I develop this I would like to be open-ended with the numbers of input papers and open ended with the number of words/terms.) # word counts: Federalist papers import re, textwrap # read the combined file and split into individual papers # later create a new version that deals with all files in a folder rather than having papers concatenated alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) print len(papers) countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") # later create a python program that extracts all words instead of using NoteTab termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") termlist = termfile.readlines() termlist = [item.rstrip("\n") for item in termlist] print len(termlist) # check for SPSS reserved words varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', 'not', 'eq', 'ge', 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v for v in termlist])) syntaxfile.write("data list file= 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") syntaxfile.writelines([v + "\n" for v in varnames]) syntaxfile.write(".\n") # before using the syntax manually replace spaces internal to a string to underscore // replace (ltrtim(rtrim(varname))," ","_") replace any special characters with @ in variable names for p in range(len(papers)): counts = [] for t in termlist: counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], re.IGNORECASE))) if sum(counts) > 0: papernum = re.search("[0-9]+", papers[p]).group(0) countsfile.write(str(papernum) + " " + " ".join([str(s) for s in counts]) + "\n") Art From vincent at vincentdavis.net Thu May 6 15:30:42 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Thu, 6 May 2010 07:30:42 -0600 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: I can't think of a way to do what you ask, without defining a test for each. ButI think what you might actually want is the define the error message to report which one failed. ie, it's one test with a meaningful error message. 'Failed to load' + str(file)+' '+ str(k)+', '+str(v) I am not ecpert on unittests *Vincent Davis 720-301-3003 * vincent at vincentdavis.net my blog | LinkedIn On Wed, May 5, 2010 at 6:37 PM, Damon Timm wrote: > Hi - am trying to write some unit tests for my little python project - > I had been hard coding them when necessary here or there but I figured > it was time to try and learn how to do it properly. > > I've read over Python's guide > (http://docs.python.org/library/unittest.html) but I am having a hard > time understanding how I can apply it *properly* to my first test case > ... > > What I am trying to do is straightforward, I am just not sure how to > populate the tests easily. Here is what I want to accomplish: > > # code > import unittest > from mlc.filetypes import * # the module I am testing > > # here are the *correct* key, value pairs I am testing against > TAG_VALUES = ( > ('title', 'Christmas Waltz'), > ('artist', 'Damon Timm'), > ('album', 'Homemade'), > ) > > # list of different file types that I want to test my tag grabbing > capabilities > # the tags inside these files are set to match my TAG_VALUES > # I want to make sure my code is extracting them correctly > FILES = ( > FLACFile('data/lossless/01 - Christmas Waltz.flac'), > MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), > OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), > MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), > ) > > class TestFiles(unittest.TestCase): > > # this is the basic test > def test_values(self): > '''see if values from my object match what they should match''' > for file in FILES: > for k, v in TAG_VALUES: > self.assertEqual(self.file.tags[k], v) > > This test works, however, it only runs as *one* test (which either > fails or passes) and I want it to run as 12 different tests (three for > each file type) and be able to see which key is failing for which file > type. I know I could write them all out individually but that seems > unnecessary. > > I suspect my answer lies in the Suites but I can't wrap my head around it. > > Thanks! > > Damon > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vincent at vincentdavis.net Thu May 6 15:46:01 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Thu, 6 May 2010 07:46:01 -0600 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: By they way you shouldn't need to use str(file) as I did. Unlessit is not a string already. Bad habit. I am used to numbers vincet On Thursday, May 6, 2010, Vincent Davis wrote: > I can't think of a way to do what you ask, without defining a test for each. ButI think what you might actually want is the define the error message to report which one failed. ie, it's one test with a meaningful error message. > 'Failed to load' + str(file)+' '+ str(k)+', '+str(v)I am not ecpert on unittests > > > > > > Vincent Davis > 720-301-3003 > > vincent at vincentdavis.net > > my blog? | > LinkedIn? > On Wed, May 5, 2010 at 6:37 PM, Damon Timm wrote: > Hi - am trying to write some unit tests for my little python project - > I had been hard coding them when necessary here or there but I figured > it was time to try and learn how to do it properly. > > I've read over Python's guide > (http://docs.python.org/library/unittest.html) but I am having a hard > time understanding how I can apply it *properly* to my first test case > ... > > What I am trying to do is straightforward, I am just not sure how to > populate the tests easily. ?Here is what I want to accomplish: > > # code > import unittest > from mlc.filetypes import * # the module I am testing > > # here are the *correct* key, value pairs I am testing against > TAG_VALUES = ( > ? ?('title', 'Christmas Waltz'), > ? ?('artist', 'Damon Timm'), > ? ?('album', 'Homemade'), > ) > > # list of different file types that I want to test my tag grabbing capabilities > # the tags inside these files are set to match my TAG_VALUES > # I want to make sure my code is extracting them correctly > FILES = ( > ? ?FLACFile('data/lossless/01 - Christmas Waltz.flac'), > ? ?MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), > ? ?OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), > ? ?MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), > ) > > class TestFiles(unittest.TestCase): > > ? ?# this is the basic test > ? ?def test_values(self): > ? ? ? ?'''see if values from my object match what they should match''' > ? ? ? ?for file in FILES: > ? ? ? ? ? ?for k, v in TAG_VALUES: > ? ? ? ? ? ? ? ?self.assertEqual(self.file.tags[k], v) > > This test works, however, it only runs as *one* test (which either > fails or passes) and I want it to run as 12 different tests (three for > each file type) and be able to see which key is failing for which file > type. ?I know I could write them all out individually but that seems > unnecessary. > > I suspect my answer lies in the Suites but I can't wrap my head around it. > > Thanks! > > Damon > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > From bludvigsen at gmail.com Thu May 6 15:54:20 2010 From: bludvigsen at gmail.com (Bjorn Egil Ludvigsen) Date: Thu, 6 May 2010 08:54:20 -0500 Subject: [Tutor] portability of pickle and shelve across platforms and different python versions? In-Reply-To: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> References: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> Message-ID: I do not know all the details, but I think it is NOT portable. The reason I am saying this is from a comment in Mark Summerfields excellent book "Rapid GUI programming with Python and Qt" where he has an example that uses several different load/save methods (chapter 8). He recommends using Qt's QDataStream method to write/read binary fields over pickle/cpickle because the Qt method is fully platform independent and it can handle Qt classes more effortlessly. He writes that files written with QDataStream are platform-independent; the class automatically takes care of endianess and word size. Since he does not say this about pickle I infer from this that files written by pickle does not take care of endianess or word size automatically. Sorry for the guesswork as I might be wrong, but I hope this will guide you in the right direction or that others will come up with more correct answers. Regards, Bjorn On Wed, May 5, 2010 at 6:12 PM, Garry Willgoose < garry.willgoose at newcastle.edu.au> wrote: > I have seen conflicting info on this on the web and in the tutor archive > and the docs don't seem to address it explicitly. How portable are files > containing pickle'd and shelve'd data? I'm thinking issues like simply O/S > portability, through big-end/little-end hardware (for floats/integers which > I use a lot), and then for unicode/non-unicode string, 64/32 bit and > V2.6/V3.1 implementations of python. Does the version of the encoder in > pickle make any difference for this? One post I've seen suggests that as > long as the file is opened binary (ie. 'b') all should be well for platform > independence. > > My core question if I give a pickled file to somebody else can i guarantee > they can read/load it OK. The other person will be using exactly the same > python code to open it as used to create it. > > ==================================================================== > Prof Garry Willgoose, > Australian Professorial Fellow in Environmental Engineering, > Director, Centre for Climate Impact Management (C2IM), > School of Engineering, The University of Newcastle, > Callaghan, 2308 > Australia. > > Centre webpage: www.c2im.org.au > > Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574 (Fri > PM-Mon) > FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal and > Telluric) > Env. Engg. Secretary: (International) +61 2 4921 6042 > > email: garry.willgoose at newcastle.edu.au; g.willgoose at telluricresearch.com > email-for-life: garry.willgoose at alum.mit.edu > personal webpage: www.telluricresearch.com/garry > ==================================================================== > "Do not go where the path may lead, go instead where there is no path and > leave a trail" > Ralph Waldo Emerson > ==================================================================== > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at bdurham.com Thu May 6 16:38:39 2010 From: python at bdurham.com (python at bdurham.com) Date: Thu, 06 May 2010 10:38:39 -0400 Subject: [Tutor] portability of pickle and shelve across platforms and different python versions? In-Reply-To: References: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> Message-ID: <1273156719.22022.1373727145@webmail.messagingengine.com> Garry, I asked a similar question on Stackoverflow.com and got some great responses including at least one from a member of the Python development team. Best way to save complex Python data structures across program sessions (pickle, json, xml, database, other) http://stackoverflow.com/questions/2003693/best-way-to-save-compl ex-python-data-structures-across-program-sessions-pickle To cut-to-the-chase: I believe pickle files are portable across platforms and versions. I do not know how portable shelve files are. Malcolm -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu May 6 17:14:42 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 06 May 2010 11:14:42 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE2B0BA.6030608@DrKendall.org> References: <4BE2B0BA.6030608@DrKendall.org> Message-ID: <4BE2DCE2.4030705@ieee.org> Art Kendall wrote: > I am running Windows 7 64bit Home premium. with quad cpus and 8G > memory. I am using Python 2.6.2. > > I have all the Federalist Papers concatenated into one .txt file. Which is how big? Currently you (unnecessarily) load the entire thing into memory with readlines(). And then you do confusing work to split it apart again, into one list element per paper. And for a while there, you have three copies of the entire text. You're keeping two copies, in the form of alltext and papers. You print out the len(papers). What do you see there? Is it correctly 87 ? If it's not, you have to fix the problem here, before even going on. > I want to prepare a file with a row for each paper and a column for > each term. The cells would contain the count of a term in that paper. > In the original application in the 1950's 30 single word terms were > used. I can now use NoteTab to get a list of all the 8708 separate > words in allWords.txt. I can then use that data in statistical > exploration of the set of texts. > > I have the python program(?) syntax(?) script(?) below that I am using > to learn PYTHON. The comments starting with "later" are things I will > try to do to make this more useful. I am getting one step at at time > to work > > It works when the number of terms in the term list is small e.g., 10. > I get a file with the correct number of rows (87) and count columns > (10) in termcounts.txt. The termcounts.txt file is not correct when I > have a larger number of terms, e.g., 100. I get a file with only 40 > rows and the correct number of columns. With 8700 terms I get only 40 > rows I need to be able to have about 8700 terms. (If this were FORTRAN > I would say that the subscript indices were getting scrambled.) (As I > develop this I would like to be open-ended with the numbers of input > papers and open ended with the number of words/terms.) > > > > # word counts: Federalist papers > > import re, textwrap > # read the combined file and split into individual papers > # later create a new version that deals with all files in a folder > rather than having papers concatenated > alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() > papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) > print len(papers) > > countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") > syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") > # later create a python program that extracts all words instead of > using NoteTab > termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") > termlist = termfile.readlines() > termlist = [item.rstrip("\n") for item in termlist] > print len(termlist) > # check for SPSS reserved words > varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', 'not', > 'eq', 'ge', > 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v > for v in termlist])) > syntaxfile.write("data list file= > 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") > syntaxfile.writelines([v + "\n" for v in varnames]) > syntaxfile.write(".\n") > # before using the syntax manually replace spaces internal to a string > to underscore // replace (ltrtim(rtrim(varname))," ","_") replace > any special characters with @ in variable names > > > for p in range(len(papers)): range(len()) is un-pythonic. Simply do for paper in papers: and of course use paper below instead of papers[p] > counts = [] > for t in termlist: > counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], > re.IGNORECASE))) > if sum(counts) > 0: > papernum = re.search("[0-9]+", papers[p]).group(0) > countsfile.write(str(papernum) + " " + " ".join([str(s) for s in > counts]) + "\n") > > > Art > If you're memory limited, you really should sequence through the files, only loading one at a time, rather than all at once. It's no harder. Use dirlist() to make a list of files, then your loop becomes something like: for infile in filelist: paper = " ".join(open(infile, "r").readlines()) Naturally, to do it right, you should use with... Or at least close each file when done. DaveA From damontimm at gmail.com Thu May 6 17:40:05 2010 From: damontimm at gmail.com (Damon Timm) Date: Thu, 6 May 2010 11:40:05 -0400 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: Hi Vincent - Thanks for your input. Where would I put that string ? In the function's doctsring ? Or just as a print method ? I have been looking online some more and it appears there may be a way to create some sort of generator ... it's still a little confusing to me, though. I was hoping there was an easier way. I can't imagine I am the first person with this task to accomplish ... Thanks, Damon On Thu, May 6, 2010 at 9:46 AM, Vincent Davis wrote: > By they way you shouldn't need to use str(file) as I did. Unlessit is > not a string already. Bad habit. I am used to numbers > vincet > > On Thursday, May 6, 2010, Vincent Davis wrote: >> I can't think of a way to do what you ask, without defining a test for each. ButI think what you might actually want is the define the error message to report which one failed. ie, it's one test with a meaningful error message. >> 'Failed to load' + str(file)+' '+ str(k)+', '+str(v)I am not ecpert on unittests >> >> >> >> >> >> ? Vincent Davis >> ? ? 720-301-3003 >> >> ? ? vincent at vincentdavis.net >> >> ? my blog? | >> ? LinkedIn? >> On Wed, May 5, 2010 at 6:37 PM, Damon Timm wrote: >> Hi - am trying to write some unit tests for my little python project - >> I had been hard coding them when necessary here or there but I figured >> it was time to try and learn how to do it properly. >> >> I've read over Python's guide >> (http://docs.python.org/library/unittest.html) but I am having a hard >> time understanding how I can apply it *properly* to my first test case >> ... >> >> What I am trying to do is straightforward, I am just not sure how to >> populate the tests easily. ?Here is what I want to accomplish: >> >> # code >> import unittest >> from mlc.filetypes import * # the module I am testing >> >> # here are the *correct* key, value pairs I am testing against >> TAG_VALUES = ( >> ?? ?('title', 'Christmas Waltz'), >> ?? ?('artist', 'Damon Timm'), >> ?? ?('album', 'Homemade'), >> ) >> >> # list of different file types that I want to test my tag grabbing capabilities >> # the tags inside these files are set to match my TAG_VALUES >> # I want to make sure my code is extracting them correctly >> FILES = ( >> ?? ?FLACFile('data/lossless/01 - Christmas Waltz.flac'), >> ?? ?MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), >> ?? ?OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), >> ?? ?MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), >> ) >> >> class TestFiles(unittest.TestCase): >> >> ?? ?# this is the basic test >> ?? ?def test_values(self): >> ?? ? ? ?'''see if values from my object match what they should match''' >> ?? ? ? ?for file in FILES: >> ?? ? ? ? ? ?for k, v in TAG_VALUES: >> ?? ? ? ? ? ? ? ?self.assertEqual(self.file.tags[k], v) >> >> This test works, however, it only runs as *one* test (which either >> fails or passes) and I want it to run as 12 different tests (three for >> each file type) and be able to see which key is failing for which file >> type. ?I know I could write them all out individually but that seems >> unnecessary. >> >> I suspect my answer lies in the Suites but I can't wrap my head around it. >> >> Thanks! >> >> Damon >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From lie.1296 at gmail.com Thu May 6 18:26:41 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 07 May 2010 02:26:41 +1000 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: On 05/06/10 10:37, Damon Timm wrote: > Hi - am trying to write some unit tests for my little python project - > I had been hard coding them when necessary here or there but I figured > it was time to try and learn how to do it properly. > > This test works, however, it only runs as *one* test (which either > fails or passes) and I want it to run as 12 different tests (three for > each file type) and be able to see which key is failing for which file > type. I know I could write them all out individually but that seems > unnecessary. One way to do what you wanted is to harness python's dynamicity and generate the methods by their names: class TestFiles(unittest.TestCase): for methname, case in somedict: def test(self): ... __dict__[methname] = test From Art at DrKendall.org Thu May 6 18:34:35 2010 From: Art at DrKendall.org (Art Kendall) Date: Thu, 06 May 2010 12:34:35 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE2DCE2.4030705@ieee.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> Message-ID: <4BE2EF9B.1000801@DrKendall.org> On 5/6/2010 11:14 AM, Dave Angel wrote: > Art Kendall wrote: >> I am running Windows 7 64bit Home premium. with quad cpus and 8G >> memory. I am using Python 2.6.2. >> >> I have all the Federalist Papers concatenated into one .txt file. > Which is how big? Currently you (unnecessarily) load the entire thing > into memory with readlines(). And then you do confusing work to split > it apart again, into one list element per paper. And for a while > there, you have three copies of the entire text. You're keeping two > copies, in the form of alltext and papers. > You print out the len(papers). What do you see there? Is it > correctly 87 ? If it's not, you have to fix the problem here, before > even going on. > >> I want to prepare a file with a row for each paper and a column for >> each term. The cells would contain the count of a term in that >> paper. In the original application in the 1950's 30 single word >> terms were used. I can now use NoteTab to get a list of all the 8708 >> separate words in allWords.txt. I can then use that data in >> statistical exploration of the set of texts. >> >> I have the python program(?) syntax(?) script(?) below that I am >> using to learn PYTHON. The comments starting with "later" are things >> I will try to do to make this more useful. I am getting one step at >> at time to work >> >> It works when the number of terms in the term list is small e.g., >> 10. I get a file with the correct number of rows (87) and count >> columns (10) in termcounts.txt. The termcounts.txt file is not >> correct when I have a larger number of terms, e.g., 100. I get a file >> with only 40 rows and the correct number of columns. With 8700 terms >> I get only 40 rows I need to be able to have about 8700 terms. (If >> this were FORTRAN I would say that the subscript indices were getting >> scrambled.) (As I develop this I would like to be open-ended with >> the numbers of input papers and open ended with the number of >> words/terms.) >> >> >> >> # word counts: Federalist papers >> >> import re, textwrap >> # read the combined file and split into individual papers >> # later create a new version that deals with all files in a folder >> rather than having papers concatenated >> alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() >> papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) >> print len(papers) >> >> countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") >> syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") >> # later create a python program that extracts all words instead of >> using NoteTab >> termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") >> termlist = termfile.readlines() >> termlist = [item.rstrip("\n") for item in termlist] >> print len(termlist) >> # check for SPSS reserved words >> varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', 'not', >> 'eq', 'ge', >> 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v >> for v in termlist])) >> syntaxfile.write("data list file= >> 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") >> syntaxfile.writelines([v + "\n" for v in varnames]) >> syntaxfile.write(".\n") >> # before using the syntax manually replace spaces internal to a >> string to underscore // replace (ltrtim(rtrim(varname))," ","_") >> replace any special characters with @ in variable names >> >> >> for p in range(len(papers)): > range(len()) is un-pythonic. Simply do > for paper in papers: > > and of course use paper below instead of papers[p] >> counts = [] >> for t in termlist: >> counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], >> re.IGNORECASE))) >> if sum(counts) > 0: >> papernum = re.search("[0-9]+", papers[p]).group(0) >> countsfile.write(str(papernum) + " " + " ".join([str(s) for s >> in counts]) + "\n") >> >> >> Art >> > If you're memory limited, you really should sequence through the > files, only loading one at a time, rather than all at once. It's no > harder. Use dirlist() to make a list of files, then your loop becomes > something like: > > for infile in filelist: > paper = " ".join(open(infile, "r").readlines()) > > Naturally, to do it right, you should use with... Or at least > close each file when done. > > DaveA > > Thank you for getting back to me. I am trying to generalize a process that 50 years ago used 30 terms on the whole file and I am using the task of generalizing the process to learn python. In the post I sent there were comments to myself about things that I would want to learn about. One of the first is to learn about processing all files in a folder, so your reply will be very helpful. It seems that dirlist() should allow me to include the filespec in the output file which would be very helpful. to rephrase my questions. Is there a way to tell python to use more RAM? Does python use the same array space over as it counts the occurrences for each input document? Or does it keep every row of the output someplace even after it has written it to the output? If it does keep old arrays, is there a way to "close" the output array in RAM between documents I narrowed down the problem. With 4035 terms it runs OK. With 4040 the end of the output matrix is messed up. I do not think it is a limit of my resources that gets in the way. I have 352G of free hard disk if it goes virtual. I have 8G of RAM. Even if python turns out to be strictly 32Bit I think it would be able to use 3G of RAM. The input file is 1.1M so that should be able to fit in RAM many times. P.S. I hope I remembered correctly that this list put replies at the bottom. Art From davea at ieee.org Thu May 6 19:51:13 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 06 May 2010 13:51:13 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE2EF9B.1000801@DrKendall.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> <4BE2EF9B.1000801@DrKendall.org> Message-ID: <4BE30191.7050304@ieee.org> Art Kendall wrote: > > > On 5/6/2010 11:14 AM, Dave Angel wrote: >> Art Kendall wrote: >>> I am running Windows 7 64bit Home premium. with quad cpus and 8G >>> memory. I am using Python 2.6.2. >>> >>> I have all the Federalist Papers concatenated into one .txt file. >> Which is how big? Currently you (unnecessarily) load the entire >> thing into memory with readlines(). And then you do confusing work >> to split it apart again, into one list element per paper. And for a >> while there, you have three copies of the entire text. You're >> keeping two copies, in the form of alltext and papers. >> You print out the len(papers). What do you see there? Is it >> correctly 87 ? If it's not, you have to fix the problem here, before >> even going on. >> >>> I want to prepare a file with a row for each paper and a column >>> for each term. The cells would contain the count of a term in that >>> paper. In the original application in the 1950's 30 single word >>> terms were used. I can now use NoteTab to get a list of all the 8708 >>> separate words in allWords.txt. I can then use that data in >>> statistical exploration of the set of texts. >>> >>> I have the python program(?) syntax(?) script(?) below that I am >>> using to learn PYTHON. The comments starting with "later" are things >>> I will try to do to make this more useful. I am getting one step at >>> at time to work >>> >>> It works when the number of terms in the term list is small e.g., >>> 10. I get a file with the correct number of rows (87) and count >>> columns (10) in termcounts.txt. The termcounts.txt file is not >>> correct when I have a larger number of terms, e.g., 100. I get a >>> file with only 40 rows and the correct number of columns. With 8700 >>> terms I get only 40 rows I need to be able to have about 8700 terms. >>> (If this were FORTRAN I would say that the subscript indices were >>> getting scrambled.) (As I develop this I would like to be >>> open-ended with the numbers of input papers and open ended with the >>> number of words/terms.) >>> >>> >>> >>> # word counts: Federalist papers >>> >>> import re, textwrap >>> # read the combined file and split into individual papers >>> # later create a new version that deals with all files in a folder >>> rather than having papers concatenated >>> alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() >>> papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) >>> print len(papers) >>> >>> countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") >>> syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") >>> # later create a python program that extracts all words instead of >>> using NoteTab >>> termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") >>> termlist = termfile.readlines() >>> termlist = [item.rstrip("\n") for item in termlist] >>> print len(termlist) >>> # check for SPSS reserved words >>> varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', 'not', >>> 'eq', 'ge', >>> 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v >>> for v in termlist])) >>> syntaxfile.write("data list file= >>> 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") >>> syntaxfile.writelines([v + "\n" for v in varnames]) >>> syntaxfile.write(".\n") >>> # before using the syntax manually replace spaces internal to a >>> string to underscore // replace (ltrtim(rtrim(varname))," ","_") >>> replace any special characters with @ in variable names >>> >>> >>> for p in range(len(papers)): >> range(len()) is un-pythonic. Simply do >> for paper in papers: >> >> and of course use paper below instead of papers[p] >>> counts = [] >>> for t in termlist: >>> counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], >>> re.IGNORECASE))) >>> if sum(counts) > 0: >>> papernum = re.search("[0-9]+", papers[p]).group(0) >>> countsfile.write(str(papernum) + " " + " ".join([str(s) for s >>> in counts]) + "\n") >>> >>> >>> Art >>> >> If you're memory limited, you really should sequence through the >> files, only loading one at a time, rather than all at once. It's no >> harder. Use dirlist() to make a list of files, then your loop >> becomes something like: >> >> for infile in filelist: >> paper = " ".join(open(infile, "r").readlines()) >> >> Naturally, to do it right, you should use with... Or at least >> close each file when done. >> >> DaveA >> >> > > Thank you for getting back to me. I am trying to generalize a process > that 50 years ago used 30 terms on the whole file and I am using the > task of generalizing the process to learn python. In the post I sent > there were comments to myself about things that I would want to learn > about. One of the first is to learn about processing all files in a > folder, so your reply will be very helpful. It seems that dirlist() > should allow me to include the filespec in the output file which would > be very helpful. > > to rephrase my questions. > Is there a way to tell python to use more RAM? > > Does python use the same array space over as it counts the occurrences > for each input document? Or does it keep every row of the output > someplace even after it has written it to the output? If it does keep > old arrays, is there a way to "close" the output array in RAM between > documents > > I narrowed down the problem. With 4035 terms it runs OK. With 4040 > the end of the output matrix is messed up. I do not think it is a > limit of my resources that gets in the way. I have 352G of free hard > disk if it goes virtual. I have 8G of RAM. Even if python turns out > to be strictly 32Bit I think it would be able to use 3G of RAM. The > input file is 1.1M so that should be able to fit in RAM many times. > > P.S. I hope I remembered correctly that this list put replies at the > bottom. > Art > Python comes in 32 and 64 bit versions, so it depends on which you're running. A 32bit executable under Windows is restricted to 2gb, regardless of physical RAM or disk capacity. There is a way to configure that in boot.ini to use 3gb instead, but it doesn't work in all circumstances. Perhaps in 64bit Windows, it lets you use 3gb. I'm not so sure your problem has anything to do with memory, however. If your total input is under 2meg, then it's almost certainly not. But you could get some ideas by examining the len(papers) as I said, and also len(alltext) You ask how to free memory. I'll assume you're using CPython, perhaps version 2.6. If you set a variable to None, it'll free the previous object it pointed at. So when you're done with alltext, you can simply set it to None. Or use the "del" statement, which also frees the name itself. That's already the case with your loop, with the counts variable. Each time through the loop, it gets reassigned to [], freeing the previous counts entirely. If your problem were indeed memory, you could process one file at a time, and cut it by some 80-fold. And if that's not enough, you could process each file one line at a time. You should be able to find your real bug with a judicious use of prints. Are you actually looping through that final for loop 87 times? Or maybe some files don't begin with the word FEDERALIST ? or some files don't have any matches. (To check that, add an else clause for your if sum(). DaveA From damontimm at gmail.com Thu May 6 19:53:08 2010 From: damontimm at gmail.com (Damon Timm) Date: Thu, 6 May 2010 13:53:08 -0400 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: Hi Lie - Thanks for that idea -- I tried it but am getting an error. I read a little about the __dict__ feature but couldn't figure it. I am going to keep searching around for how to dynamically add methods to a class ... here is the error and then the code. Thanks. # ERROR: $ python tests_tagging.py Traceback (most recent call last): File "tests_tagging.py", line 25, in class TestFileTags(unittest.TestCase): File "tests_tagging.py", line 31, in TestFileTags __dict__[test] = new_test NameError: name '__dict__' is not defined # CODE: import unittest from mlc.filetypes import * TAG_VALUES = ( ('title', 'Christmas Waltz'), ('artist', 'Damon Timm'), ('album', 'Homemade'), ) FILES = ( FLACFile('data/lossless/01 - Christmas Waltz.flac'), MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), ) list_of_tests = [] for file in FILES: for k, v in TAG_VALUES: test_name = 'test_' + file.exts[0] + '_' + k list_of_tests.append((test_name, file, k, v)) class TestFileTags(unittest.TestCase): for test in list_of_tests: def new_test(self): self.assertEqual(test[1].tags[test[2]],test[3]) __dict__[test] = new_test if __name__ == '__main__': unittest.main() On Thu, May 6, 2010 at 12:26 PM, Lie Ryan wrote: > On 05/06/10 10:37, Damon Timm wrote: >> Hi - am trying to write some unit tests for my little python project - >> I had been hard coding them when necessary here or there but I figured >> it was time to try and learn how to do it properly. >> >> This test works, however, it only runs as *one* test (which either >> fails or passes) and I want it to run as 12 different tests (three for >> each file type) and be able to see which key is failing for which file >> type. ?I know I could write them all out individually but that seems >> unnecessary. > > One way to do what you wanted is to harness python's dynamicity and > generate the methods by their names: > > class TestFiles(unittest.TestCase): > ? ?for methname, case in somedict: > ? ? ? ?def test(self): > ? ? ? ? ? ? ... > ? ? ? ?__dict__[methname] = test > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From damontimm at gmail.com Thu May 6 20:04:15 2010 From: damontimm at gmail.com (Damon Timm) Date: Thu, 6 May 2010 14:04:15 -0400 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: Ooh! Wait! I found another method that is similar in style and appears to work ... class TestFileTags(unittest.TestCase): pass for test_name, file, key, value in list_of_tests: def test_func(self): self.assertEqual(file.tags[key], value) setattr(TestFileTags, test_name, test_func) I'm not sure if it is the *best* or *right* way to do it, but it does the trick! Damon On Thu, May 6, 2010 at 1:53 PM, Damon Timm wrote: > Hi Lie - > > Thanks for that idea -- I tried it but am getting an error. ?I read a > little about the __dict__ feature but couldn't figure it. ?I am going > to keep searching around for how to dynamically add methods to a class > ... here is the error and then the code. > > Thanks. > > # ERROR: > > $ python tests_tagging.py > Traceback (most recent call last): > ?File "tests_tagging.py", line 25, in > ? ?class TestFileTags(unittest.TestCase): > ?File "tests_tagging.py", line 31, in TestFileTags > ? ?__dict__[test] = new_test > NameError: name '__dict__' is not defined > > # CODE: > > import unittest > from mlc.filetypes import * > > TAG_VALUES = ( > ? ?('title', 'Christmas Waltz'), > ? ?('artist', 'Damon Timm'), > ? ?('album', 'Homemade'), > ) > > FILES = ( > ? ?FLACFile('data/lossless/01 - Christmas Waltz.flac'), > ? ?MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), > ? ?OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), > ? ?MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), > ) > > list_of_tests = [] > for file in FILES: > ? ?for k, v in TAG_VALUES: > ? ? ? ?test_name = 'test_' + file.exts[0] + '_' + k > ? ? ? ?list_of_tests.append((test_name, file, k, v)) > > class TestFileTags(unittest.TestCase): > > ? ?for test in list_of_tests: > ? ? ? ?def new_test(self): > ? ? ? ? ? ?self.assertEqual(test[1].tags[test[2]],test[3]) > > ? ? ? ?__dict__[test] = new_test > > if __name__ == '__main__': > ? ?unittest.main() > > > On Thu, May 6, 2010 at 12:26 PM, Lie Ryan wrote: >> On 05/06/10 10:37, Damon Timm wrote: >>> Hi - am trying to write some unit tests for my little python project - >>> I had been hard coding them when necessary here or there but I figured >>> it was time to try and learn how to do it properly. >>> >>> This test works, however, it only runs as *one* test (which either >>> fails or passes) and I want it to run as 12 different tests (three for >>> each file type) and be able to see which key is failing for which file >>> type. ?I know I could write them all out individually but that seems >>> unnecessary. >> >> One way to do what you wanted is to harness python's dynamicity and >> generate the methods by their names: >> >> class TestFiles(unittest.TestCase): >> ? ?for methname, case in somedict: >> ? ? ? ?def test(self): >> ? ? ? ? ? ? ... >> ? ? ? ?__dict__[methname] = test >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > From damontimm at gmail.com Thu May 6 20:31:00 2010 From: damontimm at gmail.com (Damon Timm) Date: Thu, 6 May 2010 14:31:00 -0400 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: Sorry for the multiple posts ... I'll be quiet for a while until I find a real answer! What I wrote below doesn't actually work -- it appears to work because all the functions have different names but they all reference a single function ... I should have looked more closely at my initial output... I'm going to have to look into why that is. I need a way to make each function unique ... On Thu, May 6, 2010 at 2:04 PM, Damon Timm wrote: > class TestFileTags(unittest.TestCase): > ? ?pass > > for test_name, file, key, value in list_of_tests: > ? ?def test_func(self): > ? ? ? ?self.assertEqual(file.tags[key], value) > > ? ?setattr(TestFileTags, test_name, test_func) From steve at alchemy.com Thu May 6 21:15:49 2010 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 6 May 2010 12:15:49 -0700 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: <20100506191549.GA19672@dragon.alchemy.com> The unit test methods all take message arguments so if you just want to customize the reported error, that's easily done. something like: self.assertEqual(self.file.tags[k], v, "Failure with key "+k) That's easiest. If you really want a separate test for each, you may want to create a factory function which will generate the individual test methods when the testcase object is created. --steve From vincent at vincentdavis.net Thu May 6 21:52:13 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Thu, 6 May 2010 13:52:13 -0600 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: <20100506191549.GA19672@dragon.alchemy.com> References: <20100506191549.GA19672@dragon.alchemy.com> Message-ID: On Thu, May 6, 2010 at 1:15 PM, Steve Willoughby wrote: > The unit test methods all take message arguments so if you just > want to customize the reported error, that's easily done. > > something like: > self.assertEqual(self.file.tags[k], v, "Failure with key "+k) > > That's easiest. If you really want a separate test for each, you > may want to create a factory function which will generate the individual > test methods when the testcase object is created. > > --steve Looks like Steve answered the question you had for me, "self.assertEqual(self.file.tags[k], v, "Failure with key "+k)" I think this is the best(how I would do it) solution, 1 test for files with a meaningful report as to which file is the problem. *Vincent Davis 720-301-3003 * vincent at vincentdavis.net my blog | LinkedIn On Thu, May 6, 2010 at 1:15 PM, Steve Willoughby wrote: > The unit test methods all take message arguments so if you just > want to customize the reported error, that's easily done. > > something like: > self.assertEqual(self.file.tags[k], v, "Failure with key "+k) > > That's easiest. If you really want a separate test for each, you > may want to create a factory function which will generate the individual > test methods when the testcase object is created. > > --steve > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricaraoz at gmail.com Thu May 6 21:53:07 2010 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Thu, 06 May 2010 16:53:07 -0300 Subject: [Tutor] List comprehension + lambdas - strange behaviour In-Reply-To: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> References: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> Message-ID: <4BE31E23.6070501@gmail.com> Artur Siekielski wrote: > Hello. > I found this strange behaviour of lambdas, closures and list > comprehensions: > > >>>> funs = [lambda: x for x in range(5)] >>>> [f() for f in funs] >>>> > [4, 4, 4, 4, 4] > > Of course I was expecting the list [0, 1, 2, 3, 4] as the result. The > 'x' was bound to the final value of 'range(5)' expression for ALL > defined functions. Can you explain this? Is this only counterintuitive > example or an error in CPython? > > > Regards, > Artur > Check this : >>> funs = [(lambda: x) for x in range(5)] >>> funs[0]() 4 >>> x 4 >>> x = 3 >>> funs[0]() 3 >>> del x >>> funs[0]() Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: global name 'x' is not defined So you see, your functions just return the value of x. That's because the lambda have no parameter, so x refers to the global name x. -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Thu May 6 22:27:43 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Thu, 6 May 2010 22:27:43 +0200 Subject: [Tutor] List comprehension + lambdas - strange behaviour In-Reply-To: <4BE31E23.6070501@gmail.com> References: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> <4BE31E23.6070501@gmail.com> Message-ID: <20100506222743.61fa102e@o> On Thu, 06 May 2010 16:53:07 -0300 Ricardo Ar?oz wrote: > So you see, your functions just return the value of x. That's because > the lambda have no parameter, so x refers to the global name x. In other words, the "upvalue" (the variable captured in the closure) is referenced. Meaning if you later change it, the closure sees the change. The same in other dynamic languages. If you want the value to be captured in each func, use a second lambda to pass it: >>> funcs = [(lambda a: (lambda: a))(x) for x in range(5)] >>> [f() for f in funcs] [0, 1, 2, 3, 4] Or even ;-): >>> [(lambda a: (lambda: a))(x)() for x in range(5)] [0, 1, 2, 3, 4] ... but --> KISS principle http://en.wikipedia.org/wiki/Keep_it_simple_stupid Such syntaxes are only good for creating problems, imo. Why not make your life simple? (exception: for exploring the language's guts) Denis ________________________________ vit esse estrany ? spir.wikidot.com From Art at DrKendall.org Thu May 6 21:46:52 2010 From: Art at DrKendall.org (Art Kendall) Date: Thu, 06 May 2010 15:46:52 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE30191.7050304@ieee.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> <4BE2EF9B.1000801@DrKendall.org> <4BE30191.7050304@ieee.org> Message-ID: <4BE31CAC.5020004@DrKendall.org> On 5/6/2010 1:51 PM, Dave Angel wrote: > Art Kendall wrote: >> >> >> On 5/6/2010 11:14 AM, Dave Angel wrote: >>> Art Kendall wrote: >>>> I am running Windows 7 64bit Home premium. with quad cpus and 8G >>>> memory. I am using Python 2.6.2. >>>> >>>> I have all the Federalist Papers concatenated into one .txt file. >>> Which is how big? Currently you (unnecessarily) load the entire >>> thing into memory with readlines(). And then you do confusing work >>> to split it apart again, into one list element per paper. And for >>> a while there, you have three copies of the entire text. You're >>> keeping two copies, in the form of alltext and papers. >>> You print out the len(papers). What do you see there? Is it >>> correctly 87 ? If it's not, you have to fix the problem here, >>> before even going on. >>> >>>> I want to prepare a file with a row for each paper and a column >>>> for each term. The cells would contain the count of a term in that >>>> paper. In the original application in the 1950's 30 single word >>>> terms were used. I can now use NoteTab to get a list of all the >>>> 8708 separate words in allWords.txt. I can then use that data in >>>> statistical exploration of the set of texts. >>>> >>>> I have the python program(?) syntax(?) script(?) below that I am >>>> using to learn PYTHON. The comments starting with "later" are >>>> things I will try to do to make this more useful. I am getting one >>>> step at at time to work >>>> >>>> It works when the number of terms in the term list is small e.g., >>>> 10. I get a file with the correct number of rows (87) and count >>>> columns (10) in termcounts.txt. The termcounts.txt file is not >>>> correct when I have a larger number of terms, e.g., 100. I get a >>>> file with only 40 rows and the correct number of columns. With >>>> 8700 terms I get only 40 rows I need to be able to have about 8700 >>>> terms. (If this were FORTRAN I would say that the subscript indices >>>> were getting scrambled.) (As I develop this I would like to be >>>> open-ended with the numbers of input papers and open ended with the >>>> number of words/terms.) >>>> >>>> >>>> >>>> # word counts: Federalist papers >>>> >>>> import re, textwrap >>>> # read the combined file and split into individual papers >>>> # later create a new version that deals with all files in a folder >>>> rather than having papers concatenated >>>> alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() >>>> papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) >>>> print len(papers) >>>> >>>> countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") >>>> syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") >>>> # later create a python program that extracts all words instead of >>>> using NoteTab >>>> termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") >>>> termlist = termfile.readlines() >>>> termlist = [item.rstrip("\n") for item in termlist] >>>> print len(termlist) >>>> # check for SPSS reserved words >>>> varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', >>>> 'not', 'eq', 'ge', >>>> 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v >>>> for v in termlist])) >>>> syntaxfile.write("data list file= >>>> 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") >>>> syntaxfile.writelines([v + "\n" for v in varnames]) >>>> syntaxfile.write(".\n") >>>> # before using the syntax manually replace spaces internal to a >>>> string to underscore // replace (ltrtim(rtrim(varname))," ","_") >>>> replace any special characters with @ in variable names >>>> >>>> >>>> for p in range(len(papers)): >>> range(len()) is un-pythonic. Simply do >>> for paper in papers: >>> >>> and of course use paper below instead of papers[p] >>>> counts = [] >>>> for t in termlist: >>>> counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], >>>> re.IGNORECASE))) >>>> if sum(counts) > 0: >>>> papernum = re.search("[0-9]+", papers[p]).group(0) >>>> countsfile.write(str(papernum) + " " + " ".join([str(s) for s >>>> in counts]) + "\n") >>>> >>>> >>>> Art >>>> >>> If you're memory limited, you really should sequence through the >>> files, only loading one at a time, rather than all at once. It's no >>> harder. Use dirlist() to make a list of files, then your loop >>> becomes something like: >>> >>> for infile in filelist: >>> paper = " ".join(open(infile, "r").readlines()) >>> >>> Naturally, to do it right, you should use with... Or at least >>> close each file when done. >>> >>> DaveA >>> >>> >> >> Thank you for getting back to me. I am trying to generalize a process >> that 50 years ago used 30 terms on the whole file and I am using the >> task of generalizing the process to learn python. In the post I >> sent there were comments to myself about things that I would want to >> learn about. One of the first is to learn about processing all files >> in a folder, so your reply will be very helpful. It seems that >> dirlist() should allow me to include the filespec in the output file >> which would be very helpful. >> >> to rephrase my questions. >> Is there a way to tell python to use more RAM? >> >> Does python use the same array space over as it counts the >> occurrences for each input document? Or does it keep every row of the >> output someplace even after it has written it to the output? If it >> does keep old arrays, is there a way to "close" the output array in >> RAM between documents >> >> I narrowed down the problem. With 4035 terms it runs OK. With 4040 >> the end of the output matrix is messed up. I do not think it is a >> limit of my resources that gets in the way. I have 352G of free hard >> disk if it goes virtual. I have 8G of RAM. Even if python turns >> out to be strictly 32Bit I think it would be able to use 3G of RAM. >> The input file is 1.1M so that should be able to fit in RAM many times. >> >> P.S. I hope I remembered correctly that this list put replies at the >> bottom. >> Art >> > Python comes in 32 and 64 bit versions, so it depends on which you're > running. A 32bit executable under Windows is restricted to 2gb, > regardless of physical RAM or disk capacity. There is a way to > configure that in boot.ini to use 3gb instead, but it doesn't work in > all circumstances. Perhaps in 64bit Windows, it lets you use 3gb. > > I'm not so sure your problem has anything to do with memory, however. > If your total input is under 2meg, then it's almost certainly not. > But you could get some ideas by examining the len(papers) as I said, > and also len(alltext) > > You ask how to free memory. I'll assume you're using CPython, perhaps > version 2.6. If you set a variable to None, it'll free the previous > object it pointed at. So when you're done with alltext, you can > simply set it to None. Or use the "del" statement, which also frees > the name itself. That's already the case with your loop, with the > counts variable. Each time through the loop, it gets reassigned to > [], freeing the previous counts entirely. > > If your problem were indeed memory, you could process one file at a > time, and cut it by some 80-fold. And if that's not enough, you > could process each file one line at a time. > > You should be able to find your real bug with a judicious use of > prints. Are you actually looping through that final for loop 87 > times? Or maybe some files don't begin with the word FEDERALIST ? or > some files don't have any matches. (To check that, add an else clause > for your if sum(). > > DaveA > > > Dave, Thank you. 87 is what print len(papers) puts on the screen at the beginning of the run. There are 86 papers in the file. I checked and each paper starts with "FEDERALIST No." When I use the 30 original terms, or the 70 used later by others, the output data has the correct document numbers, 1-69, 2 versions of 70, and 71 to 85 in the 86 rows of the output. (which is what I see when I read the text into a word processor). Also the number of output lines that do not start with the correct document number increases as the number of terms increases past 4035. 4045 and 4045 have 84 lines start correctly. 8000 terms has only the first document number read correctly. I make no changes to the python code when I run with a longer list of terms. I make no changes to the original txt file I received. All I change is the number of terms in allWords.txt. All of the longer lists of terms include the terms on the shorter list so counts should not be sparser with a longer list of terms to count. All papers should have some counts. I checked and the python screen says Python 2.6.2 (r262:71605, Apr 14 2009, 22:46:50) [MSC v.1500 64 bit (AMD64)] on win32 so RAM cannot be the problem. I'll cut the big file down into 1 paper per file, put the paper number into the name of the file, and try that. I only need the papers concatenated to get the list of all words that occur in any file. Right now I use NoteTab to cut and paste that list anyways so I don't need to have 1 big file for python. (As I learn python a later task would be to generate that list via python.) BTW is Python some kind of a grandchild to Algol which was around in the early 70's? It seems reminiscent. Art From alan.gauld at btinternet.com Thu May 6 23:15:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 6 May 2010 22:15:34 +0100 Subject: [Tutor] List comprehension + lambdas - strange behaviour References: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> <4BE31E23.6070501@gmail.com> Message-ID: >> I found this strange behaviour of lambdas, closures and list >> comprehensions: >> >> >>>>> funs = [lambda: x for x in range(5)] >>>>> [f() for f in funs] >>>>> >> [4, 4, 4, 4, 4] >> >> Of course I was expecting the list [0, 1, 2, 3, 4] as the result. The >> 'x' was bound to the final value of 'range(5)' expression for ALL >> defined functions. Can you explain this? Is this only counterintuitive >> example or an error in CPython? As others have pointed out you are returning a reference not a value. You can do what you want by defining a lo cal closure using: funs = [lambda y = x: y for x in range(5)] Now you can do for f in funs: print f() and get the answer you expect. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu May 6 23:30:31 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 6 May 2010 22:30:31 +0100 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org><4BE2EF9B.1000801@DrKendall.org> <4BE30191.7050304@ieee.org> <4BE31CAC.5020004@DrKendall.org> Message-ID: "Art Kendall" wrote >>> Is there a way to tell python to use more RAM? Only in an arcane way you should never need. This is not Fortran and Python does all the memory management for you so you don't need to worry 99.9% of the time. > BTW is Python some kind of a grandchild to Algol which was around in the > early 70's? It seems reminiscent. Yes it is a long way down the heirarchy but it is part of the Algol family of imperative languages, being descended via Pascal and B which became C. And they both influenced ABC which became Python... The links are there but not too strong. There are equally strong links to functional languages like Lisp and OOP languages like Simula (which itself has a link to Algol) Pretty much every modern programming language traces its family tree back to either Algol or Lisp or both. Even VB has as much in common with Algol as it does with the original BASIC. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From hbutau at ovi.com Thu May 6 18:51:28 2010 From: hbutau at ovi.com (hbutau at ovi.com) Date: Thu, 6 May 2010 18:51:28 +0200 Subject: [Tutor] Programming microsoft excel Message-ID: <11669.957631888@ovi.com> Hi guys can i use python's win32com module to do the same tasks that i do with visual basic for applications (vba). I mean automating tasks for excel e.t.c and accessing Access databases. If win32com doesnt which module can i use? Thanks in advance -------------------------------------------------------------- Ovi Mail: Simple and user-friendly interface http://mail.ovi.com From davea at ieee.org Fri May 7 02:52:26 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 06 May 2010 20:52:26 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE31CAC.5020004@DrKendall.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> <4BE2EF9B.1000801@DrKendall.org> <4BE30191.7050304@ieee.org> <4BE31CAC.5020004@DrKendall.org> Message-ID: <4BE3644A.9040403@ieee.org> Art Kendall wrote: > > > On 5/6/2010 1:51 PM, Dave Angel wrote: >> Art Kendall wrote: >>> >>> >>> On 5/6/2010 11:14 AM, Dave Angel wrote: >>>> Art Kendall wrote: >>>>> I am running Windows 7 64bit Home premium. with quad cpus and 8G >>>>> memory. I am using Python 2.6.2. >>>>> >>>>> I have all the Federalist Papers concatenated into one .txt file. >>>> Which is how big? Currently you (unnecessarily) load the entire >>>> thing into memory with readlines(). And then you do confusing work >>>> to split it apart again, into one list element per paper. And for >>>> a while there, you have three copies of the entire text. You're >>>> keeping two copies, in the form of alltext and papers. >>>> You print out the len(papers). What do you see there? Is it >>>> correctly 87 ? If it's not, you have to fix the problem here, >>>> before even going on. >>>> >>>>> I want to prepare a file with a row for each paper and a column >>>>> for each term. The cells would contain the count of a term in that >>>>> paper. In the original application in the 1950's 30 single word >>>>> terms were used. I can now use NoteTab to get a list of all the >>>>> 8708 separate words in allWords.txt. I can then use that data in >>>>> statistical exploration of the set of texts. >>>>> >>>>> I have the python program(?) syntax(?) script(?) below that I am >>>>> using to learn PYTHON. The comments starting with "later" are >>>>> things I will try to do to make this more useful. I am getting one >>>>> step at at time to work >>>>> >>>>> It works when the number of terms in the term list is small e.g., >>>>> 10. I get a file with the correct number of rows (87) and count >>>>> columns (10) in termcounts.txt. The termcounts.txt file is not >>>>> correct when I have a larger number of terms, e.g., 100. I get a >>>>> file with only 40 rows and the correct number of columns. With >>>>> 8700 terms I get only 40 rows I need to be able to have about 8700 >>>>> terms. (If this were FORTRAN I would say that the subscript >>>>> indices were getting scrambled.) (As I develop this I would like >>>>> to be open-ended with the numbers of input papers and open ended >>>>> with the number of words/terms.) >>>>> >>>>> >>>>> >>>>> # word counts: Federalist papers >>>>> >>>>> import re, textwrap >>>>> # read the combined file and split into individual papers >>>>> # later create a new version that deals with all files in a folder >>>>> rather than having papers concatenated >>>>> alltext = file("C:/Users/Art/Desktop/fed/feder16v3.txt").readlines() >>>>> papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) >>>>> print len(papers) >>>>> >>>>> countsfile = file("C:/Users/Art/desktop/fed/TermCounts.txt", "w") >>>>> syntaxfile = file("C:/Users/Art/desktop/fed/TermCounts.sps", "w") >>>>> # later create a python program that extracts all words instead of >>>>> using NoteTab >>>>> termfile = open("C:/Users/Art/Desktop/fed/allWords.txt") >>>>> termlist = termfile.readlines() >>>>> termlist = [item.rstrip("\n") for item in termlist] >>>>> print len(termlist) >>>>> # check for SPSS reserved words >>>>> varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', >>>>> 'not', 'eq', 'ge', >>>>> 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or >>>>> v for v in termlist])) >>>>> syntaxfile.write("data list file= >>>>> 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") >>>>> syntaxfile.writelines([v + "\n" for v in varnames]) >>>>> syntaxfile.write(".\n") >>>>> # before using the syntax manually replace spaces internal to a >>>>> string to underscore // replace (ltrtim(rtrim(varname))," ","_") >>>>> replace any special characters with @ in variable names >>>>> >>>>> >>>>> for p in range(len(papers)): >>>> range(len()) is un-pythonic. Simply do >>>> for paper in papers: >>>> >>>> and of course use paper below instead of papers[p] >>>>> counts = [] >>>>> for t in termlist: >>>>> counts.append(len(re.findall(r"\b" + t + r"\b", papers[p], >>>>> re.IGNORECASE))) >>>>> if sum(counts) > 0: >>>>> papernum = re.search("[0-9]+", papers[p]).group(0) >>>>> countsfile.write(str(papernum) + " " + " ".join([str(s) for >>>>> s in counts]) + "\n") >>>>> >>>>> >>>>> Art >>>>> >>>> If you're memory limited, you really should sequence through the >>>> files, only loading one at a time, rather than all at once. It's >>>> no harder. Use dirlist() to make a list of files, then your loop >>>> becomes something like: >>>> >>>> for infile in filelist: >>>> paper = " ".join(open(infile, "r").readlines()) >>>> >>>> Naturally, to do it right, you should use with... Or at least >>>> close each file when done. >>>> >>>> DaveA >>>> >>>> >>> >>> Thank you for getting back to me. I am trying to generalize a >>> process that 50 years ago used 30 terms on the whole file and I am >>> using the task of generalizing the process to learn python. In the >>> post I sent there were comments to myself about things that I would >>> want to learn about. One of the first is to learn about processing >>> all files in a folder, so your reply will be very helpful. It seems >>> that dirlist() should allow me to include the filespec in the output >>> file which would be very helpful. >>> >>> to rephrase my questions. >>> Is there a way to tell python to use more RAM? >>> >>> Does python use the same array space over as it counts the >>> occurrences for each input document? Or does it keep every row of >>> the output someplace even after it has written it to the output? If >>> it does keep old arrays, is there a way to "close" the output array >>> in RAM between documents >>> >>> I narrowed down the problem. With 4035 terms it runs OK. With 4040 >>> the end of the output matrix is messed up. I do not think it is a >>> limit of my resources that gets in the way. I have 352G of free >>> hard disk if it goes virtual. I have 8G of RAM. Even if python >>> turns out to be strictly 32Bit I think it would be able to use 3G of >>> RAM. The input file is 1.1M so that should be able to fit in RAM >>> many times. >>> >>> P.S. I hope I remembered correctly that this list put replies at the >>> bottom. >>> Art >>> >> Python comes in 32 and 64 bit versions, so it depends on which you're >> running. A 32bit executable under Windows is restricted to 2gb, >> regardless of physical RAM or disk capacity. There is a way to >> configure that in boot.ini to use 3gb instead, but it doesn't work in >> all circumstances. Perhaps in 64bit Windows, it lets you use 3gb. >> >> I'm not so sure your problem has anything to do with memory, >> however. If your total input is under 2meg, then it's almost >> certainly not. But you could get some ideas by examining the >> len(papers) as I said, and also len(alltext) >> >> You ask how to free memory. I'll assume you're using CPython, >> perhaps version 2.6. If you set a variable to None, it'll free the >> previous object it pointed at. So when you're done with alltext, you >> can simply set it to None. Or use the "del" statement, which also >> frees the name itself. That's already the case with your loop, with >> the counts variable. Each time through the loop, it gets reassigned >> to [], freeing the previous counts entirely. >> >> If your problem were indeed memory, you could process one file at a >> time, and cut it by some 80-fold. And if that's not enough, you >> could process each file one line at a time. >> >> You should be able to find your real bug with a judicious use of >> prints. Are you actually looping through that final for loop 87 >> times? Or maybe some files don't begin with the word FEDERALIST ? >> or some files don't have any matches. (To check that, add an else >> clause for your if sum(). >> >> DaveA >> >> >> > > Dave, > Thank you. > 87 is what print len(papers) puts on the screen at the beginning of > the run. There are 86 papers in the file. > > I checked and each paper starts with "FEDERALIST No." > > When I use the 30 original terms, or the 70 used later by others, the > output data has the correct document numbers, 1-69, 2 versions of 70, > and 71 to 85 in the 86 rows of the output. (which is what I see when I > read the text into a word processor). Also the number of output lines > that do not start with the correct document number increases as the > number of terms increases past 4035. 4045 and 4045 have 84 lines > start correctly. 8000 terms has only the first document number read > correctly. > > I make no changes to the python code when I run with a longer list of > terms. I make no changes to the original txt file I received. All I > change is the number of terms in allWords.txt. All of the longer lists > of terms include the terms on the shorter list so counts should not be > sparser with a longer list of terms to count. All papers should have > some counts. > > I checked and the python screen says > Python 2.6.2 (r262:71605, Apr 14 2009, 22:46:50) [MSC v.1500 64 bit > (AMD64)] on win32 > so RAM cannot be the problem. > > I'll cut the big file down into 1 paper per file, put the paper number > into the name of the file, and try that. I only need the papers > concatenated to get the list of all words that occur in any file. > Right now I use NoteTab to cut and paste that list anyways so I don't > need to have 1 big file for python. (As I learn python a later task > would be to generate that list via python.) > > BTW is Python some kind of a grandchild to Algol which was around in > the early 70's? It seems reminiscent. > > > Art > I got my own copy of the papers, at http://thomas.loc.gov/home/histdox/fedpaper.txt I copied your code, and added logic to it to initialize termlist from the actual file. And it does complete the output file at 83 lines, approx 17000 columns per line (because most counts are one digit). It takes quite a while, and perhaps you weren't waiting for it to complete. I'd suggest either adding a print to the loop, showing the count, and/or adding a line that prints "done" after the loop terminates normally. I watched memory usage, and as expected, it didn't get very high. There are things you need to redesign, however. One is that all the punctuation and digits and such need to be converted to spaces. DaveA From steve at pearwood.info Fri May 7 03:39:37 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 7 May 2010 11:39:37 +1000 Subject: [Tutor] portability of pickle and shelve across platforms and different python versions? In-Reply-To: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> References: <13128345-B009-423C-B06E-0ADD86569D44@newcastle.edu.au> Message-ID: <201005071139.38787.steve@pearwood.info> On Thu, 6 May 2010 09:12:24 am Garry Willgoose wrote: > How portable are files containing pickle'd and shelve'd data? I'm > thinking issues like simply O/S portability, through > big-end/little-end hardware (for floats/integers which I use a lot), > and then for unicode/non-unicode string, 64/32 bit and V2.6/V3.1 > implementations of python. Does the version of the encoder in pickle > make any difference for this? One post I've seen suggests that as > long as the file is opened binary (ie. 'b') all should be well for > platform independence. Technically, reading the file isn't a matter of pickle, but a matter of the operating system not mangling the contents of the file before it reaches pickle. I would expect that text pickles (protocol 0) would be just fine with opening the file in text mode, but I haven't tried it. Because Python makes no guarantees about floats, but is just a thin wrapper around your platform's C floating point library, you may find occasional differences when pickling floats. E.g. I wouldn't trust pickling NANs and INFs to be platform independent unless the documentation explicitly says so. Unfortunately transferring floats from one platform to another is a hard problem: given a float x on platform A, there's no guarantee that x is even representable on platform B. You can make stronger promises about transferring floats if you know both platforms use IEEE floats, although the C maths libraries differ in their handling of subnormals, overflow, NANs, INFs, and signed zeroes. If these features are important to you, you've probably already discovered that your calculations differ on platforms A and B unless you're using a dedicated numeric library that doesn't rely on the native C maths routines. If this is gobbledygook to you, then don't worry about it, it should Just Work well enough that you won't notice the difference. > My core question if I give a pickled file to somebody else can i > guarantee they can read/load it OK. The other person will be using > exactly the same python code to open it as used to create it. By default, pickling uses protocol 0, which uses the repr() of objects. Nobody can *guarantee* platform independence, because you might feed Python an object like this: class Silly: def __init__(self, arg): self.arg = arg def __repr__(self): if sys.platform == 'posix': # I hate Linux. return "Screw you hippies, I'm going home!" return "Silly(%r)" % self.arg which will break pickling. Similarly if your class has a __setstate__ method which does something stupid. Python is a "consenting adults" language: if you want to shoot yourself in the foot by writing broken code, Python doesn't try to stop you. But for built-ins, with the possible exception of floats depending on the specific platforms in question, you should be safe. -- Steven D'Aprano From steve at pearwood.info Fri May 7 04:05:31 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 7 May 2010 12:05:31 +1000 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: <201005071205.31859.steve@pearwood.info> On Thu, 6 May 2010 10:37:20 am Damon Timm wrote: > class TestFiles(unittest.TestCase): > > # this is the basic test > def test_values(self): > '''see if values from my object match what they should > match''' > for file in FILES: > for k, v in TAG_VALUES: > self.assertEqual(self.file.tags[k], v) > > This test works, however, it only runs as *one* test (which either > fails or passes) That is correct, because you have written it as one test. In unit-testing, a single test can be arbitrarily complex. In your case, you've created a single test which makes 12 different comparisons, and fails if *any* of them fail. Here is an analogy... suppose you are comparing the two strings for equality. Python does this: * If the lengths are different, return False (the test fails); * If the first characters differ, return False; * If the second characters differ, return False; * If the third characters differ, return False; * ... and so on ... * return True (the test passes) The data that you feed are the strings "abcd" and "abce". Is that five tests, with the first four passing and the last failing? Well, yes, but not in any meaningful sense. Even if it is useful to see *where* the strings differ, it would be silly to treat each comparison as a separate test. > and I want it to run as 12 different tests (three > for each file type) and be able to see which key is failing for which > file type. I know I could write them all out individually but that > seems unnecessary. Unfortunately, if you really want them to be twelve individual tests, then you need to write them out individually as twelve separate tests. As for the second question, to see where the failure is, you can pass an extra argument to assertEqual: self.assertEqual(self.file.tags[k], v, "fails for file %s with tag %s and value %s" % (file, k, v)) Alternatively, you can take a separate approach. Since you have four different file types, I would test each type separately, using inheritance to reduce the amount of work needed. # Untested. class TestMP3(unittest.TestCase): filename = 'data/lossy/04 - Christmas Waltz (MP3-79).mp3' filetype = MP3File def __init__(self): self.file = self.filetype(self.filename) def test_title(self): self.assertEquals(self.file.tags['title'], 'Christmas Waltz') def test_artist(self): self.assertEquals(self.file.tags['artist'], 'Damon Timm') def test_album(self): self.assertEquals(self.file.tags['album'], 'Homemade') class TestFLAC(TestMP3): filename = 'data/lossless/01 - Christmas Waltz.flac' filetype = FLACFile class TestOGG(TestMP3): ? ? filetype = OGGFile filename = 'data/lossy/01 - Christmas Waltz (OGG-77).ogg' class TestMP4(TestMP3): ? ? filetype = MP4File filename = 'data/lossy/06 - Christmas Waltz (M4A-64).m4a' And you're done, 12 separate tests with hardly any extra typing. And now you can easily add specific tests, e.g. testing that FLAC actually is lossless: class TestFLAC(TestMP3): filename = 'data/lossless/01 - Christmas Waltz.flac' filetype = FLACFile def test_lossless(self): raw = open('raw sounds.wav', 'r').read() data = self.file.convert_to_wav() self.assertEquals(raw, data) -- Steven D'Aprano From steve at pearwood.info Fri May 7 04:20:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 7 May 2010 12:20:46 +1000 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: References: Message-ID: <201005071220.47246.steve@pearwood.info> On Fri, 7 May 2010 03:53:08 am Damon Timm wrote: > Hi Lie - > > Thanks for that idea -- I tried it but am getting an error. I read a > little about the __dict__ feature but couldn't figure it. I am going > to keep searching around for how to dynamically add methods to a > class ... here is the error and then the code. With respect to Lie, dynamically adding methods is an advanced technique that is overkill for what you seem to be doing, and the code he gave you can't work without major modification. Tests are code too, and the more complicated you make your tests, the less confidence you should have in them. The more tricks you use (dynamic methods, metaclasses, complicated frameworks, etc.) the higher the chances that your test code itself will be buggy, and therefore your pass/fail results are meaningless. For example, some time ago I was testing some code I had written, and was very happy that all my tests were passing. Then I discovered that *dozens* of tests weren't even being called due to a bug in the framework. When I worked around that problem, I discovered that now my tests were failing. Because my framework was complicated, it had a bug in it, which meant my tests were buggy, which meant my code was buggy and I didn't know. The lesson is, keep your test code simple. Don't play tricks or be too clever. Don't trust your test framework, not even well-known ones like Python's own doctest: you should double check your results, e.g. sometimes I will add a test I know will fail, just to make sure that the framework will see it. -- Steven D'Aprano From wescpy at gmail.com Fri May 7 04:48:15 2010 From: wescpy at gmail.com (wesley chun) Date: Thu, 6 May 2010 19:48:15 -0700 Subject: [Tutor] Programming microsoft excel In-Reply-To: <11669.957631888@ovi.com> References: <11669.957631888@ovi.com> Message-ID: > guys can i use python's win32com module to do the same tasks that i do with visual basic for applications (vba). I mean automating tasks for excel e.t.c and accessing Access databases. If win32com doesnt which module can i use? that's definitely the right one, and yes, you can use VB/VBA examples if you port them to Python. i wrote a good-sized section on how to do this in Chapter 23 of my book (see below) with examples for Word, Excel, PowerPoint, and OutLook. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From denis.spir at gmail.com Fri May 7 09:11:38 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Fri, 7 May 2010 09:11:38 +0200 Subject: [Tutor] List comprehension + lambdas - strange behaviour In-Reply-To: References: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> <4BE31E23.6070501@gmail.com> Message-ID: <20100507091138.56ef742f@o> On Thu, 6 May 2010 22:15:34 +0100 "Alan Gauld" wrote: > As others have pointed out you are returning a reference not a value. Yes. (I have said that, too.) But still there is a mystery for me. Better explained byt the following: x = 0 ; print id(x) # an address def f() : print x # 0 x = 1 ; print id(x) # another one f() # 1 This shows, I guess, that the reference of the upvalue x is *not* an address. But the key (maybe the name itself ?) used by python to lookup a symbol's value, in a given scope, at runtime. Indeed f must find its upvalue in the global scope. Note the scope must also be referenced: def f(): # not the global scope x = 0 def g(): print x x = 1 return g # global scope f()() # 1 I guess the above example also shows that upvalues can be "finalised", since here the scope is lost xwhen f runs. Does anyone know if this reasoning is correct, and how this is done? All of this mechanics looks very complicated. I would be happy with setting func attributes like x here as func attributes directly & explicitely: def f(): def g(): print g.x g.x = 1 ... which are still modifiable --explicitely. Denis ________________________________ vit esse estrany ? spir.wikidot.com From alan.gauld at btinternet.com Fri May 7 09:21:21 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 7 May 2010 08:21:21 +0100 Subject: [Tutor] Programming microsoft excel References: <11669.957631888@ovi.com> Message-ID: wrote > guys can i use python's win32com module to do the same tasks that > i do with visual basic for applications (vba). I mean automating tasks > for excel e.t.c and accessing Access databases. Yes. You can use the MFC API or the WSH API from win32com. > If win32com doesnt which module can i use? There are some higher level modules for specific tasks, for example working on the registry and specifically for manuipulating Excel data. So unless you specifically need to control other apps you should consider doing the job directly from Python rather than using Python as a controller, that will usually be a more robust and efficient solution. You can also access the win32 API using ctypes, but I personally find that a bit more complex than using win32com. Finally, if that is your main interest area I strongly recommend Mark Hammond's book "Python Programming on Win32", it's a bit dated now but most of it is still valid. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mail at timgolden.me.uk Fri May 7 10:52:24 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 07 May 2010 09:52:24 +0100 Subject: [Tutor] Programming microsoft excel In-Reply-To: <11669.957631888@ovi.com> References: <11669.957631888@ovi.com> Message-ID: <4BE3D4C8.2020403@timgolden.me.uk> On 06/05/2010 17:51, hbutau at ovi.com wrote: > Hi > guys can i use python's win32com module to do the same tasks > that i do with visual basic for applications (vba). > I mean automating tasks for excel e.t.c and accessing > Access databases. If win32com doesnt which module can i use? You want to look at the http://www.python-excel.org/ page and the associated (and older) http://groups.google.com/group/python-excel Google group TJG From Art at DrKendall.org Fri May 7 13:35:33 2010 From: Art at DrKendall.org (Art Kendall) Date: Fri, 07 May 2010 07:35:33 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE3644A.9040403@ieee.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> <4BE2EF9B.1000801@DrKendall.org> <4BE30191.7050304@ieee.org> <4BE31CAC.5020004@DrKendall.org> <4BE3644A.9040403@ieee.org> Message-ID: <4BE3FB05.6070206@DrKendall.org> On 5/6/2010 8:52 PM, Dave Angel wrote: >> > > I got my own copy of the papers, at > http://thomas.loc.gov/home/histdox/fedpaper.txt > > I copied your code, and added logic to it to initialize termlist from > the actual file. And it does complete the output file at 83 lines, > approx 17000 columns per line (because most counts are one digit). It > takes quite a while, and perhaps you weren't waiting for it to > complete. I'd suggest either adding a print to the loop, showing the > count, and/or adding a line that prints "done" after the loop > terminates normally. > > I watched memory usage, and as expected, it didn't get very high. > There are things you need to redesign, however. One is that all the > punctuation and digits and such need to be converted to spaces. > > > DaveA > > Thank you for going the extra mile. I obtained my copy before I retired in 2001 and there are some differences. In the current copy from the LOC papers 7, 63, and 81 start with "FEDERALIST." (an extra period). That explains why you have 83. There also some comments such as attributed author. After the weekend, I'll do a file compare and see differences in more detail. Please email me your version of the code. I'll try it as is. Then I'll put in a counter, have it print the count and paper number, and a 'done' message. As a check after reading in the counts, I'll include the counts from NoteTab and see if these counts sum to those from NoteTab. I'll use SPSS to create a version of the .txt file with punctuation and numerals changed to spaces and try using that as the corpus. Then I'll try to create a similar file with Python. Art From steve at pearwood.info Fri May 7 13:37:06 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 7 May 2010 21:37:06 +1000 Subject: [Tutor] List comprehension + lambdas - strange behaviour In-Reply-To: <20100507091138.56ef742f@o> References: <23a73fff-3b3c-4c3a-b39c-9fd0df5bd6c3@b7g2000yqk.googlegroups.com> <20100507091138.56ef742f@o> Message-ID: <201005072137.07373.steve@pearwood.info> On Fri, 7 May 2010 05:11:38 pm spir ? wrote: > On Thu, 6 May 2010 22:15:34 +0100 > > "Alan Gauld" wrote: > > As others have pointed out you are returning a reference not a > > value. Others have said that, but it's not true. Python does not have "references". The Python implementation under the hood uses references all the time, but they aren't visible to Python code. In Python you have names, and objects, and nothing else. The classic test of languages with references is, can you write a function that swaps two variables? That is, something like this: >>> x = 1 >>> y = 2 >>> swap(x, y) >>> print x, y 2, 1 But you can't do this in Python. This proves beyond all doubt that Python code does not have references. > Yes. (I have said that, too.) But still there is a mystery for me. > Better explained byt the following: > > x = 0 ; print id(x) # an address No, id(x) is an ID number, not an address. It happens to be that for CPython, the address of the object in the underlying implementation is used as the ID number, but that's an accident of implementation. They could have very easily decided to add 7 to the address, or multiply it by 3. Jython uses a completely different scheme, where ID numbers have nothing to do with addresses. They go 1, 2, 3, 4, ... Python guarantees that no two objects will have the same ID number *at the same time*, but it makes no other promises. CPython, for example, re-uses ID numbers. Jython probably doesn't. > def f() : print x # 0 > x = 1 ; print id(x) # another one > f() # 1 > > This shows, I guess, that the reference of the upvalue x is *not* an > address. But the key (maybe the name itself ?) used by python to > lookup a symbol's value, in a given scope, at runtime. Indeed f must > find its upvalue in the global scope. Yes. The *name* "x" is looked up in the global scope at runtime. The address of the object bound to x is never used by Python, except that CPython uses it as a ID number. > Note the scope must also be referenced: > > def f(): > # not the global scope > x = 0 > def g(): > print x > x = 1 > return g > # global scope > f()() # 1 > > I guess the above example also shows that upvalues can be > "finalised", since here the scope is lost xwhen f runs. > > Does anyone know if this reasoning is correct, and how this is done? Python has the concept of names spaces. Names are strings like "x", "foo", "math" and so forth. When you refer to a name, Python searches each name space in turn: * the local name space of the current function (if any) * the name space of each parent function (if any) in turn * the global name space * the built-in name space and then raises NameError if it doesn't find a match at all. Inside a class, it's a little different, but essentially follows a similar pattern: * the local name space of the method * the global name space * built-ins * raise NameError Notice that the class name space is deliberately left out. When looking up an attribute of an item, it goes something like this: * if the class has a __getattribute__ method, call it * the instance __slots__ (if any) * the instance __dict__ (if it exists) * the class __dict__ * the __dict__ of any superclasses * if the class has a __getattr__ method, call it * raise AttributeError Regardless of how you look up a name or attribute, once Python finds it, it passes the object back to you. Actually, because objects are large complicated objects, the Python implementation actually passes some sort of internal short-cut to the object, for speed. In CPython, that is a pointer. In Jython, it is a reference or safe-pointer. In other Python implementations (IronPython, PyPy, etc.) some other mechanism is used. The mechanism isn't important. From your Python code, you have no way of telling what the mechanism is, all you see is names ("math") and objects (the math module object). In CPython, we can say some other things about the implementation of name spaces. In the global (top level) scope of a module, in the interactive interpreter, and inside a class, the name space is a dictionary with strings as keys. Inside functions, the name space is a bit more complicated: for speed it is turned into a C-level table. (Actually, it's a bit more complicated for classes too, with slots and other optimizations.) This is why Python classes have a __dict__ attribute, and why the dict you get back from globals() is writable, but changing the dict you get back from locals() has no effect inside a function. Objects can have any number of names. If you do this: a = b = c = d = 42 e = c then you have given the object 42 five distinct names. But if you do this: mylist = [] mylist.append(20 + 3) the object 23 is created and stored inside a list, but it has no name. It is an anonymous object, but you can still refer to it: mylist[0] # gives 23 Some operations do a re-binding, that it, they associate a new object with a name: x = "something special" def f(): pass class K: pass create a new binding between an object and the names x, f and K. The command del x will remove the *name* x from the name space. What happens next varies -- if the string used somewhere else, then nothing happens. But if the string is no longer used, then Python will automatically delete the object and reuse its memory. This is why destructors (__del__ methods) don't necessarily run when you expect them to. >>> class Example: ... def __del__(self): ... print "Goodbye!" ... >>> x = Example() >>> mylist = [x] >>> del x >>> mylist[0] = None Goodbye! Hope this helps, -- Steven D'Aprano From davea at ieee.org Fri May 7 14:31:08 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 07 May 2010 08:31:08 -0400 Subject: [Tutor] Is the difference in outputs with different size input lists due to limits on memory with PYTHON? In-Reply-To: <4BE3FB05.6070206@DrKendall.org> References: <4BE2B0BA.6030608@DrKendall.org> <4BE2DCE2.4030705@ieee.org> <4BE2EF9B.1000801@DrKendall.org> <4BE30191.7050304@ieee.org> <4BE31CAC.5020004@DrKendall.org> <4BE3644A.9040403@ieee.org> <4BE3FB05.6070206@DrKendall.org> Message-ID: <4BE4080C.5070903@ieee.org> Art Kendall wrote: > > > On 5/6/2010 8:52 PM, Dave Angel wrote: >>> >> >> I got my own copy of the papers, at >> http://thomas.loc.gov/home/histdox/fedpaper.txt >> >> I copied your code, and added logic to it to initialize termlist from >> the actual file. And it does complete the output file at 83 lines, >> approx 17000 columns per line (because most counts are one digit). >> It takes quite a while, and perhaps you weren't waiting for it to >> complete. I'd suggest either adding a print to the loop, showing the >> count, and/or adding a line that prints "done" after the loop >> terminates normally. >> >> I watched memory usage, and as expected, it didn't get very high. >> There are things you need to redesign, however. One is that all the >> punctuation and digits and such need to be converted to spaces. >> >> >> DaveA >> >> > > Thank you for going the extra mile. > > I obtained my copy before I retired in 2001 and there are some > differences. In the current copy from the LOC papers 7, 63, and 81 > start with "FEDERALIST." (an extra period). That explains why you > have 83. There also some comments such as attributed author. After > the weekend, I'll do a file compare and see differences in more detail. > > Please email me your version of the code. I'll try it as is. Then > I'll put in a counter, have it print the count and paper number, and a > 'done' message. > > As a check after reading in the counts, I'll include the counts from > NoteTab and see if these counts sum to those from NoteTab. > > I'll use SPSS to create a version of the .txt file with punctuation > and numerals changed to spaces and try using that as the corpus. > Then I'll try to create a similar file with Python. > > Art > As long as you realize this is very rough. I just wanted to prove there wasn't anything fundamentally wrong with your approach. But there's still lots to do, especially with regards to cleaning up the text before and between the papers. Anyway, here it is. #!/usr/bin/env python sourcedir = "data/" outputdir = "results/" # word counts: Federalist papers import sys, os import re, textwrap #Create the output directory if it doesn't exist if not os.path.exists(outputdir): os.makedirs(outputdir) # read the combined file and split into individual papers # later create a new version that deals with all files in a folder rather than having papers concatenated alltext = file(sourcedir + "feder16.txt").readlines() filtered = " ".join(alltext).lower() for ch in ('" ' + ". , ' * - ( ) = @ [ ] ; . ` 1 2 3 4 5 6 7 8 9 0 > : / ?").split(): filtered = filtered.replace(ch, " ") #todo: make a better filter, such as keeping only letters, rather than replacing # specific characters words = filtered.split() print "raw word count is", len(words) wordset = set(words) print "wordset reduces it from/to", len(words), len(wordset) #eliminate words shorter than 4 characters words = sorted([word for word in wordset if len(word)>3]) del wordset #free space of wordset print "Eliminating words under 4 characters reduces it to", len(words) #print the first 50 for word in words[:50]: print word print "alltext is size", len(alltext) papers= re.split(r'FEDERALIST No\.'," ".join(alltext)) print "Number of detected papers is ", len(papers) #print first 50 characters of each, so we can see why some of them are missed # by our regex above for index, paper in enumerate(papers): print index, "***", paper[:50] countsfile = file(outputdir + "TermCounts.txt", "w") syntaxfile = file(outputdir + "TermCounts.sps", "w") # later create a python program that extracts all words instead of using NoteTab #termfile = open("allWords.txt") #termlist = termfile.readlines() #termlist = [item.rstrip("\n") for item in termlist] #print "termlist is ", len(termlist) termlist = words # check for SPSS reserved words varnames = textwrap.wrap(" ".join([v.lower() in ['and', 'or', 'not', 'eq', 'ge', 'gt', 'le', 'lt', 'ne', 'all', 'by', 'to','with'] and (v+"_r") or v for v in termlist])) syntaxfile.write("data list file= 'c:/users/Art/desktop/fed/termcounts.txt' free/docnumber\n") syntaxfile.writelines([v + "\n" for v in varnames]) syntaxfile.write(".\n") # before using the syntax manually replace spaces internal to a string to underscore // replace (ltrtim(rtrim(varname))," ","_") replace any special characters with @ in variable names for p, paper in enumerate(papers): counts = [] for t in termlist: counts.append(len(re.findall(r"\b" + t + r"\b", paper, re.IGNORECASE))) print p, counts[:5] if sum(counts) > 0: papernum = re.search("[0-9]+", papers[p]).group(0) countsfile.write(str(papernum) + " " + " ".join([str(s) for s in counts]) + "\n") DaveA From jf_byrnes at comcast.net Fri May 7 17:57:10 2010 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Fri, 07 May 2010 10:57:10 -0500 Subject: [Tutor] PIL problem Message-ID: <4BE43856.4090509@comcast.net> Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - 1.1.6-3ubuntu1 - Python Imaging Library is installed. But trying to import PhotoImage gives these results: >>> from ImageTk import PhotoImage Traceback (most recent call last): File "", line 1, in ImportError: No module named ImageTk What have I gotten wrong? Thanks, Jim From mwalsh at mwalsh.org Fri May 7 18:37:34 2010 From: mwalsh at mwalsh.org (Martin Walsh) Date: Fri, 07 May 2010 11:37:34 -0500 Subject: [Tutor] PIL problem In-Reply-To: <4BE43856.4090509@comcast.net> References: <4BE43856.4090509@comcast.net> Message-ID: <4BE441CE.5090401@mwalsh.org> Jim Byrnes wrote: > Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - > 1.1.6-3ubuntu1 - Python Imaging Library is installed. > > But trying to import PhotoImage gives these results: > > >>>> from ImageTk import PhotoImage > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named ImageTk > > What have I gotten wrong? > Apparently, ImageTk is part of a separate ubuntu package called python-imaging-tk. HTH, Marty From aclark at aclark.net Fri May 7 18:40:58 2010 From: aclark at aclark.net (Alex Clark) Date: Fri, 7 May 2010 16:40:58 +0000 (UTC) Subject: [Tutor] PIL problem References: <4BE43856.4090509@comcast.net> Message-ID: On 2010-05-07, Jim Byrnes wrote: > Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - > 1.1.6-3ubuntu1 - Python Imaging Library is installed. > > But trying to import PhotoImage gives these results: > > > >>> from ImageTk import PhotoImage > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named ImageTk > > What have I gotten wrong? Try import PIL.ImageTk (if you look inside the package, you will notice ImageTk is inside a directory called PIL) > > Thanks, Jim > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Alex Clark ? http://aclark.net Author of Plone 3.3 Site Administration ? http://aclark.net/plone-site-admin From damontimm at gmail.com Fri May 7 20:41:19 2010 From: damontimm at gmail.com (Damon Timm) Date: Fri, 7 May 2010 14:41:19 -0400 Subject: [Tutor] Newbie & Unittest ... In-Reply-To: <201005071220.47246.steve@pearwood.info> References: <201005071220.47246.steve@pearwood.info> Message-ID: Hello again everyone - and thanks for your responses. Adding the unittest method message was something I didn't realize I could do! On Thu, May 6, 2010 at 10:20 PM, Steven D'Aprano wrote: > With respect to Lie, dynamically adding methods is an advanced technique > that is overkill for what you seem to be doing, and the code he gave > you can't work without major modification. I think you make a good argument for simple testing ... and I already fell victim to "It's working great! My tests pass!" when in fact the test wasn't working at all! Here is what I ended up doing, and it (currently) runs 52 tests. I'm not sure if it is worth the trade-off, but I think it saved me some typing (and makes it easy to add another file or tag key/value pair). #!/usr/bin/env python ''' unit tests for tagging.py ''' import unittest from mlc import filetypes TAG_VALUES = ( ('title', 'Christmas Waltz'), ('artist', 'Damon Timm'), ('album', 'Homemade'), ('albumartist', 'Damon Timm'), ('compilation', False ), ('composer', 'Damon Timm'), ('date', '2005'), ('description', 'For more music, visit: damonjustisntfunny.com'), ('discnumber', 1), ('disctotal', 1), ('genre', 'Folk'), ('tracknumber', 1), ('tracktotal', 10), ) FILES = ( filetypes.FLACFile('data/lossless/01 - Christmas Waltz.flac'), filetypes.MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), filetypes.OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), filetypes.MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), ) class TestTagOutput(unittest.TestCase): pass def add_assert_equal(cls, test_name, value1, value2): new_test = lambda self: self.assertEqual(value1, value2) new_test.__doc__ = test_name setattr(cls, test_name, new_test) for file in FILES: for key, value in TAG_VALUES: test_name = 'test_' + file.exts[0] + '_' + key # test_ext_key add_assert_equal(TestFileTags, test_name, file.tags[key], value) if __name__ == '__main__': unittest.main() From jf_byrnes at comcast.net Fri May 7 21:39:02 2010 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Fri, 07 May 2010 14:39:02 -0500 Subject: [Tutor] PIL problem In-Reply-To: <4BE441CE.5090401@mwalsh.org> References: <4BE43856.4090509@comcast.net> <4BE441CE.5090401@mwalsh.org> Message-ID: <4BE46C56.3080109@comcast.net> Martin Walsh wrote: > Jim Byrnes wrote: >> Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - >> 1.1.6-3ubuntu1 - Python Imaging Library is installed. >> >> But trying to import PhotoImage gives these results: >> >> >>>>> from ImageTk import PhotoImage >> Traceback (most recent call last): >> File "", line 1, in >> ImportError: No module named ImageTk >> >> What have I gotten wrong? >> > > Apparently, ImageTk is part of a separate ubuntu package called > python-imaging-tk. > > HTH, > Marty Thanks, once I installed that separate package it worked. Regards, Jim From jf_byrnes at comcast.net Fri May 7 21:40:46 2010 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Fri, 07 May 2010 14:40:46 -0500 Subject: [Tutor] PIL problem In-Reply-To: References: <4BE43856.4090509@comcast.net> Message-ID: <4BE46CBE.1050603@comcast.net> Alex Clark wrote: > On 2010-05-07, Jim Byrnes wrote: >> Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - >> 1.1.6-3ubuntu1 - Python Imaging Library is installed. >> >> But trying to import PhotoImage gives these results: >> >> >>>>> from ImageTk import PhotoImage >> Traceback (most recent call last): >> File "", line 1, in >> ImportError: No module named ImageTk >> >> What have I gotten wrong? > > > Try import PIL.ImageTk > > (if you look inside the package, you will notice ImageTk is inside a directory called PIL) > I didn't have a necessary package installed. Once it is installed import ImageTk works. Regards, Jim From oberoc at gmail.com Sat May 8 18:19:53 2010 From: oberoc at gmail.com (Tino Dai) Date: Sat, 8 May 2010 12:19:53 -0400 Subject: [Tutor] Question about Python being object oriented Message-ID: Hi Everybody, My friend and I were having a disagreement about Python. Has Python always been an OO language or was it at one point a procedural language like C? Thanks! -Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From eike.welk at gmx.net Sat May 8 18:47:51 2010 From: eike.welk at gmx.net (Eike Welk) Date: Sat, 8 May 2010 18:47:51 +0200 Subject: [Tutor] Question about Python being object oriented In-Reply-To: References: Message-ID: <201005081847.51680.eike.welk@gmx.net> On Saturday May 8 2010 18:19:53 Tino Dai wrote: > Hi Everybody, > > My friend and I were having a disagreement about Python. Has Python > always been an OO language or was it at one point a procedural language > like C? Thanks! The Wikipedia article states: Yes, it was always an object oriented language. http://en.wikipedia.org/wiki/History_of_Python However Python has a predecessor, the ABC language, which is not object oriented. http://en.wikipedia.org/wiki/ABC_programming_language Eike. From kbailey at howlermonkey.net Sat May 8 23:31:15 2010 From: kbailey at howlermonkey.net (Kirk Z Bailey) Date: Sat, 08 May 2010 17:31:15 -0400 Subject: [Tutor] An interesting situation befalls me Message-ID: <4BE5D823.8@howlermonkey.net> An instructor of mine is about to teach the FIRST EVER class in Python at Saint Petersburg College; knowing I am a snakecharmer, he asked me for referrals to online resources. Oh my. So I sent back this: " Ah, python., my fav obsession. First, the language website itself: http://www.python.org/ Natch, they offer a tutorial: http://docs.python.org/tutorial/ But this one is better for rank beginniners: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers And there's another one here: http://www.sthurlow.com/python/ And a nice writeup on wikipedia: http://en.wikipedia.org/wiki/Python_%28programming_language%29 You may care to go teleport to planet python: http://planet.python.org/ And you can swim into it at diveintopython: http://diveintopython.org/toc/index.html " Now here is a chance to help influence this getting off on the right foot. I can use reccomendations for texts for use in an introduction to Python class, and I will condense it down and provide them to the good doctor. -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-----+ | BOX | +-----+ think From bgailer at gmail.com Sun May 9 00:38:49 2010 From: bgailer at gmail.com (bob gailer) Date: Sat, 08 May 2010 18:38:49 -0400 Subject: [Tutor] Question about Python being object oriented In-Reply-To: References: Message-ID: <4BE5E7F9.7040602@gmail.com> On 5/8/2010 12:19 PM, Tino Dai wrote: > Hi Everybody, > > My friend and I were having a disagreement about Python. Has > Python always been an OO language or was it at one point a procedural > language like C? Thanks! OO and procedural are not mutually exclusive! From http://en.wikipedia.org/wiki/Imperative_programming: "Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). The terms are often used as synonyms, but the use of procedures has a dramatic effect on how imperative programs appear and how they are constructed. Heavily procedural programming, in which state changes are localized to procedures or restricted to explicit arguments and returns from procedures, is known as structured programming. From the 1960s onwards, structured programming and modular programming in general, have been promoted as techniques to improve the maintainability and overall quality of imperative programs. Object-oriented programming extends this approach." -- Bob Gailer 919-636-4239 Chapel Hill NC From steve at pearwood.info Sun May 9 05:43:50 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 9 May 2010 13:43:50 +1000 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <4BE5D823.8@howlermonkey.net> References: <4BE5D823.8@howlermonkey.net> Message-ID: <201005091343.52007.steve@pearwood.info> On Sun, 9 May 2010 07:31:15 am Kirk Z Bailey wrote: > An instructor of mine is about to teach the FIRST EVER class in > Python at Saint Petersburg College; knowing I am a snakecharmer, he > asked me for referrals to online resources. > > Oh my. > > So I sent back this: [...] > And you can swim into it at diveintopython: > http://diveintopython.org/toc/index.html Here's a counter-opinion: Dive Into Python must die! http://oppugn.us/posts/1272050135.html And a few more suggestions: http://inventwithpython.com Learn Python the Hard Way: http://learnpythonthehardway.com/index -- Steven D'Aprano From lie.1296 at gmail.com Sun May 9 07:11:28 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 09 May 2010 15:11:28 +1000 Subject: [Tutor] Question about Python being object oriented In-Reply-To: References: Message-ID: On 05/09/10 02:19, Tino Dai wrote: > Hi Everybody, > > My friend and I were having a disagreement about Python. Has Python > always been an OO language or was it at one point a procedural language like > C? Thanks! AFAIK Python has always been a mixed paradigm language. You can write fully OO code if you want, as well as procedural-style or imperative-style code (especially handy for quickie-scripts[1]). But as Bob Gailer pointed out, most OOP languages are build on top of procedural base, which is itself is build on top of imperative base. [1] compare to Java, a simple "hello world" must contain a class declaration, then a 'public stupi^B^B^Batic main', etc. Even C must have "public main". From comprookie2000 at gmail.com Sun May 9 00:37:47 2010 From: comprookie2000 at gmail.com (David Abbott) Date: Sat, 8 May 2010 18:37:47 -0400 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <4BE5D823.8@howlermonkey.net> References: <4BE5D823.8@howlermonkey.net> Message-ID: Now here is a chance to help influence this getting off on the right foot. > I can use recomendations for texts for use in an introduction to Python > class, and I will condense it down and provide them to the good doctor. > > -- > end > > Very Truly yours, > - Kirk Bailey, > Largo Florida > > Here are a few; Learning to Program by none other than Alan Gauld http://www.freenetpages.co.uk/hp/alan.gauld/ Essential Python Reading list http://wordaligned.org/articles/essential-python-reading-list Daves Page http://www.rexx.com/~dkuhlman/ -- David Abbott (dabbott) Gentoo http://dev.gentoo.org/~dabbott/ Podcast: http://linuxcrazy.com/ Web: http://nooone.info/ http://dwabbott.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 9 10:27:38 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 9 May 2010 09:27:38 +0100 Subject: [Tutor] An interesting situation befalls me References: <4BE5D823.8@howlermonkey.net> <201005091343.52007.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote >> And you can swim into it at diveintopython: >> http://diveintopython.org/toc/index.html > Here's a counter-opinion: > > Dive Into Python must die! > http://oppugn.us/posts/1272050135.html Hmmm, it sounds a bit like complaints from someone who has never written a book themselves! While some of the complaints are valid others simply reflect the time and place. I believe Mr Pilgrim has taken the conscious decision to have the web site mirror his book, so changing it would leave them out of synch. Similarly, I assume the book was a one-off project and he has no desire to revisit it. (Or maybe his publisher has no desire, it's not all in the author's control!) I took the alternative view that the book was a snapshot of my web site and the web site continues to evolve while the book is frozen in 1999. But both approaches are valid and Pilgrim's book still has much to teach, although it's definitely not for beginners and it's not one of my personal favourites. But if the style fits the reader I certainly wouldn't discourage its use. My own book took just short of 2 years to put together (and it already existed as a web site so I had the basic structure and words in place), updating a book is a significant commitment. Even updating my web site to Python v3 has taken me about 18 months so far and I'm only 70% through it... If you are not a full time author and it's not a primary revenue source it takes more than a change in technology to motivate a rewrite! > And a few more suggestions: > http://inventwithpython.com > > Learn Python the Hard Way: > http://learnpythonthehardway.com/index These were both new ones to me, thanks for sharing! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 9 10:29:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 9 May 2010 09:29:34 +0100 Subject: [Tutor] An interesting situation befalls me References: <4BE5D823.8@howlermonkey.net> Message-ID: "David Abbott" wrote > Learning to Program by none other than Alan Gauld > http://www.freenetpages.co.uk/hp/alan.gauld/ Thanks for the plug but the freenetpages site has been frozen for over 2 years. The current site (including the Python v3 version) is as in my .sig... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 9 14:16:35 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 9 May 2010 13:16:35 +0100 Subject: [Tutor] An interesting situation befalls me References: <4BE5D823.8@howlermonkey.net><201005091343.52007.steve@pearwood.info> Message-ID: "Alan Gauld" wrote >> Learn Python the Hard Way: >> http://learnpythonthehardway.com/index > > These were both new ones to me, thanks for sharing! I've just finished skimming this one and its pretty good IMHO! My only gripe is the final "chapter" - "Advice from an old programmer". It seems Mr Shaw has become somewhat disillusioned with the software engineering profession, but as someone who has beenn programming for at least as long as he has I have to say that I still enjoy it and find it rewarding both personally and financially. So don't let the final page of cynicism put you off a career in software engineering. Otherwise, if you are looking for a newbie tutorial this a is a good one although it has less depth than most other tutors, it's still a good first step. I assume it's still a work in progress too, since it jumps from Exercise (aka chapter) 10 to Excercise 27 in one go! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From joseph.gulizia at gmail.com Sun May 9 15:01:07 2010 From: joseph.gulizia at gmail.com (Joseph Gulizia) Date: Sun, 9 May 2010 08:01:07 -0500 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <4BE5D823.8@howlermonkey.net> References: <4BE5D823.8@howlermonkey.net> Message-ID: As a new learner of programming I'd recommend Head First Programming "A learner's guide to programming, using the Python language" by O'Reilly. It is very basic. Joe On Sat, May 8, 2010 at 4:31 PM, Kirk Z Bailey wrote: > An instructor of mine is about to teach the FIRST EVER class in Python at > Saint Petersburg College; knowing I am a snakecharmer, he asked me for > referrals to online resources. > > Oh my. > > So I sent back this: > " > Ah, python., my fav obsession. First, the language website itself: > http://www.python.org/ > Natch, they offer a tutorial: > http://docs.python.org/tutorial/ > But this one is better for rank beginniners: > http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > And there's another one here: > http://www.sthurlow.com/python/ > And a nice writeup on wikipedia: > http://en.wikipedia.org/wiki/Python_%28programming_language%29 > You may care to go teleport to planet python: > http://planet.python.org/ > And you can swim into it at diveintopython: > http://diveintopython.org/toc/index.html > " > > Now here is a chance to help influence this getting off on the right foot. > I can use reccomendations for texts for use in an introduction to Python > class, and I will condense it down and provide them to the good doctor. > > -- > end > > Very Truly yours, > - Kirk Bailey, > Largo Florida > > kniht +-----+ > | BOX | +-----+ think > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kbailey at howlermonkey.net Sun May 9 15:27:05 2010 From: kbailey at howlermonkey.net (Kirk Z Bailey) Date: Sun, 09 May 2010 09:27:05 -0400 Subject: [Tutor] An interesting situation befalls me revisited Message-ID: <4BE6B829.9040800@howlermonkey.net> Well, this has been a fruitful thread! Now here's the next exciting installment; what's a good TEXTBOOK for a class to use on the subject? -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-----+ | BOX | +-----+ think From denis.spir at gmail.com Sun May 9 19:31:21 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 9 May 2010 19:31:21 +0200 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <201005091343.52007.steve@pearwood.info> References: <4BE5D823.8@howlermonkey.net> <201005091343.52007.steve@pearwood.info> Message-ID: <20100509193121.1db0fef9@o> On Sun, 9 May 2010 13:43:50 +1000 Steven D'Aprano wrote: > http://inventwithpython.com Yep, this one is great! (I gave it a nickname: "play programming!" Is this correct english?) Denis ________________________________ vit esse estrany ? spir.wikidot.com From knacktus at googlemail.com Sun May 9 19:33:51 2010 From: knacktus at googlemail.com (Jan Jansen) Date: Sun, 9 May 2010 19:33:51 +0200 Subject: [Tutor] reading binary file on windows and linux Message-ID: Hello, I've got some trouble reading binary files with struct.unpack on windows. According to the documentation of the binary file's content, at the beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned byte'). Within those bytes there's a sequence to check the file's sanity. The sequence is (in ascii C-Notation): " " "\n" "\r" "\n" " " I've downloaded the file from the same website from two machines. One is a Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble is while on linux everything is fine, on windows the carriage return does not appear when reading the file with struct.unpack. The file sizes on Linux and Windows are exaktly the same, and also my script determines the file sizes correctly on both plattforms (according to the OS). When I open the file on Windows in an editor and display the whitespaces, the linefeed and cariage-return are shown a expected. The code I'm using to check the first 80 bytes of the file is: import struct import sys with open(sys.argv[1]) as source: size = struct.calcsize("80B") raw_data = struct.unpack("80B", source.read(size)) for i, data in enumerate(raw_data): print i, data, chr(data) source.seek(0, 2) print source.tell() Any suggestions are highly appreciated. Cheers, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Sun May 9 19:59:58 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Sun, 9 May 2010 18:59:58 +0100 Subject: [Tutor] reading binary file on windows and linux In-Reply-To: References: Message-ID: On 9 May 2010 18:33, Jan Jansen wrote: > Hello, > > I've got some trouble reading binary files with struct.unpack on windows. > According to the documentation of the binary file's content, at the > beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned > byte'). Within those bytes there's a sequence to check the file's sanity. > The sequence is (in ascii C-Notation): > " " > "\n" > "\r" > "\n" > " " > I've downloaded the file from the same website from two machines. One is a > Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble > is while on linux everything is fine, on windows the carriage return does > not appear when reading the file with struct.unpack. > > The file sizes on Linux and Windows are exaktly the same, and also my > script determines the file sizes correctly on both plattforms (according to > the OS). When I open the file on Windows in an editor and display the > whitespaces, the linefeed and cariage-return are shown a expected. > > The code I'm using to check the first 80 bytes of the file is: > > import struct > import sys > > with open(sys.argv[1]) as source: > size = struct.calcsize("80B") > raw_data = struct.unpack("80B", source.read(size)) > for i, data in enumerate(raw_data): > print i, data, chr(data) > source.seek(0, 2) > print source.tell() > > > Any suggestions are highly appreciated. > > Cheers, > > Jan > I'd guess that it's because newline in windows is /r/n and in linux it's just /n. If you read the file as binary rather than text then it should work the same on both platforms ie use: open(sys.argv[1], "rb") HTH, Adam. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Sun May 9 20:00:06 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 9 May 2010 20:00:06 +0200 Subject: [Tutor] reading binary file on windows and linux In-Reply-To: References: Message-ID: On Sun, May 9, 2010 at 7:33 PM, Jan Jansen wrote: > Hello, > > I've got some trouble reading binary files with struct.unpack on windows. > According to the documentation of the binary file's content, at the > beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned > byte'). Within those bytes there's a sequence to check the file's sanity. > The sequence is (in ascii C-Notation): > " " > "\n" > "\r" > "\n" > " " > I've downloaded the file from the same website from two machines. One is a > Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble > is while on linux everything is fine, on windows the carriage return does > not appear when reading the file with struct.unpack. > > The file sizes on Linux and Windows are exaktly the same, and also my script > determines the file sizes correctly on both plattforms (according to the > OS). When I open the file on Windows in an editor and display the > whitespaces, the linefeed and cariage-return are shown a expected. > > The code I'm using to check the first 80 bytes of the file is: > > import struct > import sys > > with open(sys.argv[1]) as source: > ??? size = struct.calcsize("80B") > ??? raw_data = struct.unpack("80B", source.read(size)) > ??? for i, data in enumerate(raw_data): > ??? ??? print i, data, chr(data) > ??? source.seek(0, 2) > ??? print source.tell() > Since the file is binary, you should use the "b" mode when opening it: with open(sys.argv[1], "rb") as source: otherwise, the file will open in text mode, which converts newline characters to/from a platform specific representation when reading or writing. In windows, that representation is \r\n, meaning that that sequence is converted to just \n when you read from the file. That is why the carriage return disappears. Hugo From steve at pearwood.info Sun May 9 20:06:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 10 May 2010 04:06:46 +1000 Subject: [Tutor] reading binary file on windows and linux In-Reply-To: References: Message-ID: <201005100406.47838.steve@pearwood.info> On Mon, 10 May 2010 03:33:51 am Jan Jansen wrote: > Hello, > > I've got some trouble reading binary files with struct.unpack on > windows. [...] > The code I'm using to check the first 80 bytes of the file is: > > import struct > import sys > > with open(sys.argv[1]) as source: You're opening the file in text mode. On Linux, there's no difference, but on Windows, it will do strange things to the end of lines. You need to open the file in binary mode: open(sys.argv[1], 'rb') -- Steven D'Aprano From denis.spir at gmail.com Sun May 9 20:16:44 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 9 May 2010 20:16:44 +0200 Subject: [Tutor] reading binary file on windows and linux In-Reply-To: References: Message-ID: <20100509201644.7041f05e@o> On Sun, 9 May 2010 19:33:51 +0200 Jan Jansen wrote: > Hello, > > I've got some trouble reading binary files with struct.unpack on windows. > According to the documentation of the binary file's content, at the > beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned > byte'). Within those bytes there's a sequence to check the file's sanity. > The sequence is (in ascii C-Notation): > " " > "\n" > "\r" > "\n" > " " > I've downloaded the file from the same website from two machines. One is a > Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble > is while on linux everything is fine, on windows the carriage return does > not appear when reading the file with struct.unpack. > > The file sizes on Linux and Windows are exaktly the same, and also my script > determines the file sizes correctly on both plattforms (according to the > OS). When I open the file on Windows in an editor and display the > whitespaces, the linefeed and cariage-return are shown a expected. > > The code I'm using to check the first 80 bytes of the file is: > > import struct > import sys > > with open(sys.argv[1]) as source: > size = struct.calcsize("80B") > raw_data = struct.unpack("80B", source.read(size)) > for i, data in enumerate(raw_data): > print i, data, chr(data) > source.seek(0, 2) > print source.tell() I guess (but am not 100% sure because never use 'b'), the issue will be solved using: with open(sys.argv[1], 'rb') as source: The reason is by default files are opened in read 'r' and text mode. In text mode, whatever char seq is used by a given OS with the sense of "line separator" ("\r\n' under win) is silently converted by python to a canonical code made of the single '\n' (char #0xa). So that, in your case, in the header sub-sequence '\r'+'\n' you lose '\r'. In so-called bynary mode 'b' instead, python does not perform this replacement anymore, so that you get the raw byte sequence. Hope I'm right on this and it helps. Denis ________________________________ vit esse estrany ? spir.wikidot.com From denis.spir at gmail.com Sun May 9 21:01:08 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 9 May 2010 21:01:08 +0200 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <201005091343.52007.steve@pearwood.info> References: <4BE5D823.8@howlermonkey.net> <201005091343.52007.steve@pearwood.info> Message-ID: <20100509210108.76e0b84e@o> On Sun, 9 May 2010 13:43:50 +1000 Steven D'Aprano wrote: > Here's a counter-opinion: > > Dive Into Python must die! > http://oppugn.us/posts/1272050135.html I 100% share the article author's opinion. This "book" was surely crafted with the only intention to disgust anybody of programming. A shame (the word is too weak, but english is a foreign language for me so I need to be prudent ;-), have a look at the book yourself and tell me what proper qualifier matches that horror); even more since it uses python and pretends to introduce to this language. I guess the only thing to do is to spread the word "do NOT even have a look at 'dive into python'" to everybody wishing to learn about programming and/or python. Sorry for such bad words, but I think some things cannot be left unsaid. Denis ________________________________ vit esse estrany ? spir.wikidot.com From ilhs_hs at yahoo.com Sun May 9 23:35:15 2010 From: ilhs_hs at yahoo.com (Hs Hs) Date: Sun, 9 May 2010 14:35:15 -0700 (PDT) Subject: [Tutor] An interesting situation befalls me In-Reply-To: <201005091343.52007.steve@pearwood.info> References: <4BE5D823.8@howlermonkey.net> <201005091343.52007.steve@pearwood.info> Message-ID: <502425.11470.qm@web111208.mail.gq1.yahoo.com> It is interesting to note war against 'Dive into Python'. Personally I felt it was good, a quick resource to learn without buying O'reilly books. After I came across Alan Gauld and Mark Lutz's books, I never referred DintoP. I still have the printed version of this book from 2002-3 I guess. After a variety of circus maneuvers, I came to the following conclusion: 1. Go to http://www.freenetpages.co.uk/hp/alan.gauld/ Alan Gauld's book is a wonder. He feeds the basics right into your brain. 2. With Alan's book as basic I bought Mark Lutz's Learning Python. 3. Make use of tutors list. I owe a lot to people here like Alan and Kent Johnson. In no time, you can be a good programmer. this is my personal opinion and nothing to do with Zed's war on DintoP. ________________________________ From: Steven D'Aprano To: tutor at python.org Sent: Sat, May 8, 2010 11:43:50 PM Subject: Re: [Tutor] An interesting situation befalls me On Sun, 9 May 2010 07:31:15 am Kirk Z Bailey wrote: > An instructor of mine is about to teach the FIRST EVER class in > Python at Saint Petersburg College; knowing I am a snakecharmer, he > asked me for referrals to online resources. > > Oh my. > > So I sent back this: [...] > And you can swim into it at diveintopython: > http://diveintopython.org/toc/index.html Here's a counter-opinion: Dive Into Python must die! http://oppugn.us/posts/1272050135.html And a few more suggestions: http://inventwithpython.com Learn Python the Hard Way: http://learnpythonthehardway.com/index -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From pythelico at gmail.com Tue May 11 00:16:31 2010 From: pythelico at gmail.com (Kelly Netterville) Date: Mon, 10 May 2010 18:16:31 -0400 Subject: [Tutor] An interesting situation befalls me In-Reply-To: <4BE5D823.8@howlermonkey.net> References: <4BE5D823.8@howlermonkey.net> Message-ID: On Sat, May 8, 2010 at 5:31 PM, Kirk Z Bailey wrote: > An instructor of mine is about to teach the FIRST EVER class in Python at > Saint Petersburg College; knowing I am a snakecharmer, he asked me for > referrals to online resources. > > Oh my. > > > I haven't seen this one posted in the thread yet: http://www.greenteapress.com/thinkpython/thinkpython.html Think Python: How to think like a Computer Scientist - Download the book for free from the link listed. That's one of the many great things about Python. . . there are a ton of great resources available if you look. Kelly -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Tue May 11 02:44:23 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 10 May 2010 19:44:23 -0500 Subject: [Tutor] An interesting situation befalls me In-Reply-To: References: <4BE5D823.8@howlermonkey.net> Message-ID: On Mon, May 10, 2010 at 5:16 PM, Kelly Netterville wrote: > > > On Sat, May 8, 2010 at 5:31 PM, Kirk Z Bailey wrote: > >> An instructor of mine is about to teach the FIRST EVER class in Python at >> Saint Petersburg College; knowing I am a snakecharmer, he asked me for >> referrals to online resources. >> >> Oh my. >> > http://www.briggs.net.nz/log/writing/snake-wrangling-for-kids/ I don't think anyone has posted that one. It's more directed towards kids, but any reasonable adult can benefit from it, too. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From Art at DrKendall.org Tue May 11 15:54:13 2010 From: Art at DrKendall.org (Art Kendall) Date: Tue, 11 May 2010 09:54:13 -0400 Subject: [Tutor] displaying clock and/or elapsed time Message-ID: <4BE96185.3080409@DrKendall.org> I am learning python and I want to see how long parts of of a process take. Are there system variables that can just be displayed? how do I get variables that contain the wall time (x) and the elapsed time (y)? I would like to put little messages on the screen while I am developing a python script so I can see how long things are taking. Is it something like print ' starting part1' (x y) to put the times on the screen? Is it something like logfile.write( x,y) to put the times in the log file. Art From steve at pearwood.info Tue May 11 16:46:05 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 12 May 2010 00:46:05 +1000 Subject: [Tutor] displaying clock and/or elapsed time In-Reply-To: <4BE96185.3080409@DrKendall.org> References: <4BE96185.3080409@DrKendall.org> Message-ID: <201005120046.06935.steve@pearwood.info> On Tue, 11 May 2010 11:54:13 pm Art Kendall wrote: > I am learning python and I want to see how long parts of of a process > take. Are there system variables that can just be displayed? > how do I get variables that contain the wall time (x) and the elapsed > time (y)? Get the current time in seconds since the start of the universe (10am on January 1st 1970 on Linux systems, it may be slightly different on Windows or Macintosh): >>> import time >>> time.time() 1273588356.8070121 Get the current time in a more human-readable fashion: >>> time.ctime() 'Wed May 12 00:33:22 2010' Get the elapsed time: >>> start = time.time() # save the current time >>> for i in range(1000): # do some work ... pass ... >>> elapsed = time.time() - start >>> print "work took %f seconds" % elapsed work took 0.015870 seconds See the documentation for the time module: http://docs.python.org/library/time.html That's a bit technical and newbie-unfriendly, so you might also like to read this: http://effbot.org/librarybook/time.htm although it's very old and parts of it are obsolete. -- Steven D'Aprano From Sivapathasuntha.Aruliah at amkor.com Tue May 11 10:43:26 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Tue, 11 May 2010 16:43:26 +0800 Subject: [Tutor] (no subject) Message-ID: Hi I am learning Python. When I tried to run any of the program for example csv2html1_ans.py it displays the following message. This error is coming on both Python24 & Python 31. That is whether i give the any one of the following command COMMAND GIVEN 1.C:\python24\python.exe C:\py3eg\quadratic.py 2.C:\python31\python.exe C:\py3eg\quadratic.py A message below appears with the program name. Please advice me how to get over from this issue ERROR MESSAGE command C:\py3eg\csv2html1_ans.py is not a valid Win32 application Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 -------------- next part -------------- An HTML attachment was scrubbed... URL: From vincent at vincentdavis.net Tue May 11 19:24:03 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Tue, 11 May 2010 11:24:03 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: probably need to do something like python C:\py3eg\quadratic.py your cmd prompt/shell should know "python" is a command On Tue, May 11, 2010 at 2:43 AM, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: > > Hi > I am learning Python. When I tried to run any of the program for example > csv2html1_ans.py it displays the following message. This error is coming on > both Python24 & Python 31. That is whether i give the any one of the > following command > > *COMMAND GIVEN* > 1.C:\python24\python.exe C:\py3eg\quadratic.py > 2.C:\python31\python.exe C:\py3eg\quadratic.py > > A message below appears with the program name. Please advice me how to get > over from this issue > *ERROR MESSAGE* > command C:\py3eg\csv2html1_ans.py is not a valid Win32 application > > Regards, > Siva > Test Equipment Engineering > Amkor Technology (S) Pte Ltd > 1 Kaki Bukit View > #03-28 TechView Building > Singapore 415941 > Tel: (65) 6347 1131 > Fax: (65) 6746 4815 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > *Vincent Davis 720-301-3003 * vincent at vincentdavis.net my blog | LinkedIn -------------- next part -------------- An HTML attachment was scrubbed... URL: From nramya82 at gmail.com Tue May 11 20:00:20 2010 From: nramya82 at gmail.com (ramya natarajan) Date: Tue, 11 May 2010 11:00:20 -0700 Subject: [Tutor] Help required to count no of lines that are until 1000 characters Message-ID: Hello, I am very beginner to programming, I got task to Write a loop that reads each line of a file and counts the number of lines that are read until the total length of the lines is 1,000 characters. I have to read lines from files exactly upto 1000 characters. Here is my code: I created file under /tmp/new.txt which has 100 lines and 2700 characters , I wrote code will read exactly 1000 characters and count lines upto those characters.But the problem is its reading entire line and not stopping excatly in 1000 characters. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char < 1000 : for ch in line : char += len(ch) lines += 1 print char , lines 1026 , 38 ---- Its counting entire line instead of character upto 1000 -- can some one point out what mistake am i doing here , where its not stopping at 1000 . I am reading only char by car My new.txt -- cotains content like this is my new number\n Can some one please help. I spent hours and hours to find issue but i am not able to figure out, Any help would be greatly appreciated. Thank you Ramya -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Tue May 11 20:36:10 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Tue, 11 May 2010 13:36:10 -0500 Subject: [Tutor] Help required to count no of lines that are until 1000 characters In-Reply-To: References: Message-ID: On Tue, May 11, 2010 at 1:00 PM, ramya natarajan wrote: > Hello, I have to read lines from > files exactly upto 1000 characters. >But the problem is its reading entire line and not stopping > excatly in 1000 characters. Can some one help what mistake i am doing here?. > > ?? log = open('/tmp/new.txt','r') > ?? lines,char = 0,0 > ?? for line in log.readlines(): > ??????? while char < 1000 : > ??????????????? for ch in line : > ???????????????????? char += len(ch) > ??????????????? lines += 1 > ? print char , lines here's the pseudocode of what you're doing, it might help you understand what the problem is: for every line in the file: if the character count is less than 1000, add the length of the current line. You are missing a condition. Here is another version of your code that has the same problem, see if this helps make it clearer: lines, chars = 0,0 with open('/temp/new.txt') as f: for line in f: if chars > 1000: break chars += len(line) This sounds a lot like a homework problem so I won't give you the answer, but I hope that helps. Also do you realize you are counting newlines as well? You may not want to do this, depending on your intended application. Hope that helps, -Luke From denis.spir at gmail.com Tue May 11 21:02:24 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 11 May 2010 21:02:24 +0200 Subject: [Tutor] Help required to count no of lines that are until 1000 characters In-Reply-To: References: Message-ID: <20100511210224.30cce0b6@o> On Tue, 11 May 2010 11:00:20 -0700 ramya natarajan wrote: > Hello, > > I am very beginner to programming, I got task to Write a loop that reads > each line of a file and counts the number of lines that are read until the > total length of the lines is 1,000 characters. I have to read lines from > files exactly upto 1000 characters. > > Here is my code: > I created file under /tmp/new.txt which has 100 lines and 2700 characters > , I wrote code will read exactly 1000 characters and count lines upto those > characters.But the problem is its reading entire line and not stopping > excatly in 1000 characters. Can some one help what mistake i am doing here?. > > log = open('/tmp/new.txt','r') > lines,char = 0,0 > for line in log.readlines(): > while char < 1000 : > for ch in line : > char += len(ch) > lines += 1 > print char , lines > 1026 , 38 ---- Its counting entire line instead of character upto 1000 > -- can some one point out what mistake am i doing here , where its not > stopping at 1000 . I am reading only char by car > > My new.txt -- cotains content like > this is my new number\n > > Can some one please help. I spent hours and hours to find issue but i am not > able to figure out, Any help would be greatly appreciated. > Thank you > Ramya Either you read line per line, but then you cannot stop exactly at the 1000th character; or you traverse the text char per char, but this is a bit picky. I would read line per line, and when count >= 1000, read chars inside current line to get to the 1000th, if needed. (Your specification does not state this, but your disappointment seems to be about that issue ;-) Denis ________________________________ vit esse estrany ? spir.wikidot.com From shantanoo at gmail.com Tue May 11 21:16:32 2010 From: shantanoo at gmail.com (=?utf-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Wed, 12 May 2010 00:46:32 +0530 Subject: [Tutor] Help required to count no of lines that are until 1000 characters In-Reply-To: <20100511210224.30cce0b6@o> References: <20100511210224.30cce0b6@o> Message-ID: <32D57D72-4CF9-445F-BBE7-FF4BC9C5A018@gmail.com> On 12-May-2010, at 12:32 AM, spir ? wrote: > On Tue, 11 May 2010 11:00:20 -0700 > ramya natarajan wrote: > >> Hello, >> >> I am very beginner to programming, I got task to Write a loop that reads >> each line of a file and counts the number of lines that are read until the >> total length of the lines is 1,000 characters. I have to read lines from >> files exactly upto 1000 characters. >> >> Here is my code: >> I created file under /tmp/new.txt which has 100 lines and 2700 characters >> , I wrote code will read exactly 1000 characters and count lines upto those >> characters.But the problem is its reading entire line and not stopping >> excatly in 1000 characters. Can some one help what mistake i am doing here?. >> >> log = open('/tmp/new.txt','r') >> lines,char = 0,0 >> for line in log.readlines(): >> while char < 1000 : >> for ch in line : >> char += len(ch) >> lines += 1 >> print char , lines >> 1026 , 38 ---- Its counting entire line instead of character upto 1000 >> -- can some one point out what mistake am i doing here , where its not >> stopping at 1000 . I am reading only char by car >> >> My new.txt -- cotains content like >> this is my new number\n >> >> Can some one please help. I spent hours and hours to find issue but i am not >> able to figure out, Any help would be greatly appreciated. >> Thank you >> Ramya > > Either you read line per line, but then you cannot stop exactly at the 1000th character; or you traverse the text char per char, but this is a bit picky. > I would read line per line, and when count >= 1000, read chars inside current line to get to the 1000th, if needed. > (Your specification does not state this, but your disappointment seems to be about that issue ;-) > > Denis You can try read instead of readlines. Something like... print 'Number of lines till 1000th character:', len(open('/tmp/new.txt','r').read(1000).split('\n')) From alan.gauld at btinternet.com Tue May 11 21:18:42 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 11 May 2010 20:18:42 +0100 Subject: [Tutor] Help required to count no of lines that are until 1000characters References: Message-ID: "ramya natarajan" wrote > characters.But the problem is its reading entire line and not stopping > excatly in 1000 characters. Do you really need to stop reading the file at 1000 characters rather than the line containing the 1000th character? That seems a very arbitrary sort of thing to do. > Can some one help what mistake i am doing here?. > > log = open('/tmp/new.txt','r') > lines,char = 0,0 > for line in log.readlines(): > while char < 1000 : > for ch in line : > char += len(ch) > lines += 1 The problem is that the inner for loop will always process every character in the line. You want to stop (or break) from the for loop when char gets to 1000. So you need to insert a test inside the for loop. Or don't use the for loop and use an index to get the characters within your while loop. BTW You don't want to add the length of the characters you just want to add 1... > stopping at 1000 . I am reading only char by car No you are not, you are reading all the chars in every line that you read from the file. In fact even if you fix this loop error you will still be reading the full line from the file. Thats why I asked if you really had to stop reading the file at 1000 chars, because if so this design is fundamentally wrong. But I suspect you only need to stop reading at the line containing the 1000th char... If that is so there is an easier way to do it: # pseudo code chars = 0 for count, line in enumerate(file) if chars + len(line) < 1000 chars += len(line) else: break print "1000 chars read in", count, "lines" HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 11 21:21:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 11 May 2010 20:21:43 +0100 Subject: [Tutor] (no subject) References: Message-ID: "Sivapathasuntha Aruliah" wrote > I am learning Python. When I tried to run any of the program for example > csv2html1_ans.py it displays the following message. This error is coming > on both Python24 & Python 31. That is whether i give the any one of the > following command > > COMMAND GIVEN > 1.C:\python24\python.exe C:\py3eg\quadratic.py > 2.C:\python31\python.exe C:\py3eg\quadratic.py > > A message below appears with the program name. Please advice me how to get > over from this issue > ERROR MESSAGE > command C:\py3eg\csv2html1_ans.py is not a valid Win32 application How are you running the commands? In a DOS box? Which OS version is it? I get a different error message in XP: C:\Documents and Settings\Alan Gauld>ghfghfghfh 'ghfghfghfh' is not recognized as an internal or external command, operable program or batch file. If it is a DOS box then what you are doing should work, but the error suggests it is trying to treat the argument as a program which should not happen! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From dorseye at gmail.com Tue May 11 21:26:56 2010 From: dorseye at gmail.com (Eric Dorsey) Date: Tue, 11 May 2010 13:26:56 -0600 Subject: [Tutor] An interesting situation befalls me In-Reply-To: References: <4BE5D823.8@howlermonkey.net> Message-ID: > On Sat, May 8, 2010 at 5:31 PM, Kirk Z Bailey wrote: >> >>> An instructor of mine is about to teach the FIRST EVER class in Python at >>> Saint Petersburg College; knowing I am a snakecharmer, he asked me for >>> referrals to online resources. >>> >>> Oh my. >>> >> > > Here is another resource, the Byte of Python book: http://www.swaroopch.com/notes/Python_en:Table_of_Contents The online version is free, or you can buy the book. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue May 11 23:45:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 11 May 2010 22:45:34 +0100 Subject: [Tutor] Help required to count no of lines that are until 1000 characters References: <20100511210224.30cce0b6@o> Message-ID: "spir ?" wrote > Either you read line per line, but then you cannot stop exactly at the 1000th > character; > or you traverse the text char per char, but this is a bit picky. Or you could just read 1000 chars from the file then pick out the lines from that. But that requires you to count newlines as characters! :-) HTH, Alan G. From davea at ieee.org Wed May 12 03:50:00 2010 From: davea at ieee.org (Dave Angel) Date: Tue, 11 May 2010 21:50:00 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <4BEA0948.7070004@ieee.org> Sivapathasuntha Aruliah wrote: > Hi > I am learning Python. When I tried to run any of the program for example > csv2html1_ans.py it displays the following message. This error is coming > on both Python24 & Python 31. That is whether i give the any one of the > following command > > COMMAND GIVEN > 1.C:\python24\python.exe C:\py3eg\quadratic.py > 2.C:\python31\python.exe C:\py3eg\quadratic.py > > A message below appears with the program name. Please advice me how to get > over from this issue > ERROR MESSAGE > command C:\py3eg\csv2html1_ans.py is not a valid Win32 application > > Regards, > Siva > Test Equipment Engineering > Amkor Technology (S) Pte Ltd > 1 Kaki Bukit View > #03-28 TechView Building > Singapore 415941 > Tel: (65) 6347 1131 > Fax: (65) 6746 4815 > Please copy and paste the actual contents of your DOS box, rather than paraphrasing. COMMAND hasn't been the normal shell name since Win95 days. You can't use numbers in front of commands in any shell I've used. The error message refers to a different file than anything you specified in your commands. What OS are you using? DaveA From davea at ieee.org Wed May 12 04:23:31 2010 From: davea at ieee.org (Dave Angel) Date: Tue, 11 May 2010 22:23:31 -0400 Subject: [Tutor] Help required to count no of lines that are until 1000 characters In-Reply-To: <4BEA0F42.7080100@dejaviewphoto.com> References: <4BEA0F42.7080100@dejaviewphoto.com> Message-ID: <4BEA1123.6050704@ieee.org> ramya natarajan wrote: > Hello, > > I am very beginner to programming, I got task to Write a loop that > reads > each line of a file and counts the number of lines that are read until > the > total length of the lines is 1,000 characters. I have to read lines from > files exactly upto 1000 characters. > > Here is my code: > I created file under /tmp/new.txt which has 100 lines and 2700 > characters > , I wrote code will read exactly 1000 characters and count lines upto > those > characters.But the problem is its reading entire line and not stopping > excatly in 1000 characters. Can some one help what mistake i am doing > here?. > > log = open('/tmp/new.txt','r') > lines,char = 0,0 > for line in log.readlines(): > while char < 1000 : > for ch in line : > char += len(ch) > lines += 1 > print char , lines > 1026 , 38 ---- Its counting entire line instead of character upto > 1000 > -- can some one point out what mistake am i doing here , where its not > stopping at 1000 . I am reading only char by car > > My new.txt -- cotains content like > this is my new number\n > > Can some one please help. I spent hours and hours to find issue but i > am not > able to figure out, Any help would be greatly appreciated. > Thank you > Ramya > > The problem is ill-specified (contradictory). It'd probably be better to give the exact wording of the assignment. If you read each line of the file, then it would only be a coincidence if you read exactly 1000 characters, as most likely one of those lines will overlap the 1000 byte boundary. But you have a serious bug in your code, that nobody in the first five responses has addressed. That while loop will loop over the first line repeatedly, till it reaches or exceeds 1000, regardless of the length of subsequent lines. So it really just divides 1000 by the length of that first line. Notice that the lines += 1 will execute multiple times for a single iteration of the for loop. Second, once 1000 is reached, the for loop does not quit. So it will read the rest of the file, regardless of how big the file is. It just stops adding to lines or char, since char reached 1000 on the first line. The simplest change to your code which might accomplish what you want is to put the whole thing inside a function, and return from the function when the goal is reached. So instead of a while loop, you need some form of if test. See if you can run with that. Remember that return can return a tuple (pair of numbers). There are plenty of other optimizations and approaches, but you'll learn best by incrementally fixing what you already have. DaveA > From davea at ieee.org Wed May 12 04:28:57 2010 From: davea at ieee.org (Dave Angel) Date: Tue, 11 May 2010 22:28:57 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <4BEA1269.5080409@ieee.org> (1. Please don't top-post. It gets everything out of sequence, and is the wrong convention for this forum 2. Be sure and do a reply-all, so that the message goes to the forum. I'm not here to give private advice. 3. Use your editor's reply-quoting so that we can tell who wrote which parts. Normally, you'll see that as either a leading ">" character or as a "!" character. And one can tell which parts were written by whom by counting the number of those at the beginning of each line) For my real response, see the end of the message, where it belongs. Sivapathasuntha Aruliah wrote: > Dave > Thank you very much for your response. I think I have problem with both > Python23 & Python31. Please help. > > Python23 : The program works but programs written by Mark Summerfield in > his book Programming in Python3 does not work. > Python 31: When I run this program it says in the pop up window " > C:\py3eg\csv2html1_ans.py is not a valid Win32 application" and on the the > dos box it says Access is denied. > Below is the dos box contents > > > C:\>cd python31 > > C:\Python31>python C:\py3eg\quadratic.py > Access is denied. > > C:\Python31>python C:\py3eg\quadratic.py > Access is denied. > > C:\Python31>python C:\py3eg\hello.py > Access is denied. > > C:\Python31>python.exe C:\py3eg\hello.py > Access is denied. > > C:\Python31>cd.. > > C:\>cd python23 > > C:\Python23>python.exe C:\py3eg\hello.py > ('Hello', 'World!') > > C:\Python23>python.exe C:\py3eg\print_unicode.py > Traceback (most recent call last): > File "C:\py3eg\print_unicode.py", line 30, in ? > print_unicode_table(word) > NameError: name 'print_unicode_table' is not defined > > C:\Python23>python.exe C:\py3eg\quadratic.py > File "C:\py3eg\quadratic.py", line 14 > except ValueError as err: > ^ > SyntaxError: invalid syntax > > > > > Regards, > Siva > Test Equipment Engineering > Amkor Technology (S) Pte Ltd > 1 Kaki Bukit View > #03-28 TechView Building > Singapore 415941 > Tel: (65) 6347 1131 > Fax: (65) 6746 4815 > > > > Dave Angel > > > 05/12/2010 09:50 AM > > > To > Sivapathasuntha Aruliah/S1/AAWW at Amkor > cc > tutor at python.org > Subject > Re: [Tutor] (no subject) > > > > > > > > > Sivapathasuntha Aruliah wrote: > >> Hi >> I am learning Python. When I tried to run any of the program for example >> > > >> csv2html1_ans.py it displays the following message. This error is coming >> > > >> on both Python24 & Python 31. That is whether i give the any one of the >> following command >> >> COMMAND GIVEN >> 1.C:\python24\python.exe C:\py3eg\quadratic.py >> 2.C:\python31\python.exe C:\py3eg\quadratic.py >> >> A message below appears with the program name. Please advice me how to >> > get > >> over from this issue >> ERROR MESSAGE >> command C:\py3eg\csv2html1_ans.py is not a valid Win32 application >> >> Regards, >> Siva >> Test Equipment Engineering >> Amkor Technology (S) Pte Ltd >> 1 Kaki Bukit View >> #03-28 TechView Building >> Singapore 415941 >> Tel: (65) 6347 1131 >> Fax: (65) 6746 4815 >> >> > Please copy and paste the actual contents of your DOS box, rather than > paraphrasing. COMMAND hasn't been the normal shell name since Win95 > days. You can't use numbers in front of commands in any shell I've > used. The error message refers to a different file than anything you > specified in your commands. > > What OS are you using? > > DaveA > > > > Again, what OS are you using? I have no idea what the pop up comes from, but I suspect you have some non-trivial code in that python program, perhaps that creates a gui. Is there any tkinter stuff in it? As for "Access is Denied", it usually means you tried to access a non-existent drive, or one which isn't currently mounted. For example, referencing your CD drive with no platter in it. I don't know why "print_unicode_table" is undefined, but apparently you're missing some code. And the except clause changed between 2.x and 3.x, so you need to change the syntax to match the particular interpreter you're using. They're not compatible, although there's a utility to convert from 2.x to 3.x, I don't think there's anything that reverses it. I'd suggest picking one version, and using only books and references that are compatible with it till you're comfortable with the language. DaveA From nramya82 at gmail.com Wed May 12 04:51:46 2010 From: nramya82 at gmail.com (ramya natarajan) Date: Tue, 11 May 2010 19:51:46 -0700 Subject: [Tutor] (no subject) In-Reply-To: <4BEA1269.5080409@ieee.org> References: <4BEA1269.5080409@ieee.org> Message-ID: I really thank you all for the quick response, It was really helpful!! On Tue, May 11, 2010 at 7:28 PM, Dave Angel wrote: > (1. Please don't top-post. It gets everything out of sequence, and is the > wrong convention for this forum > 2. Be sure and do a reply-all, so that the message goes to the forum. I'm > not here to give private advice. > 3. Use your editor's reply-quoting so that we can tell who wrote which > parts. Normally, you'll see that as either a leading ">" character or as a > "!" character. And one can tell which parts were written by whom by > counting the number of those at the beginning of each line) > > For my real response, see the end of the message, where it belongs. > > Sivapathasuntha Aruliah wrote: > >> Dave >> Thank you very much for your response. I think I have problem with both >> Python23 & Python31. Please help. >> >> Python23 : The program works but programs written by Mark Summerfield in >> his book Programming in Python3 does not work. >> Python 31: When I run this program it says in the pop up window " >> C:\py3eg\csv2html1_ans.py is not a valid Win32 application" and on the the >> dos box it says Access is denied. >> Below is the dos box contents >> >> >> C:\>cd python31 >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>python.exe C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>cd.. >> >> C:\>cd python23 >> >> C:\Python23>python.exe C:\py3eg\hello.py >> ('Hello', 'World!') >> >> C:\Python23>python.exe C:\py3eg\print_unicode.py >> Traceback (most recent call last): >> File "C:\py3eg\print_unicode.py", line 30, in ? >> print_unicode_table(word) >> NameError: name 'print_unicode_table' is not defined >> >> C:\Python23>python.exe C:\py3eg\quadratic.py >> File "C:\py3eg\quadratic.py", line 14 >> except ValueError as err: >> ^ >> SyntaxError: invalid syntax >> >> >> >> >> Regards, >> Siva >> Test Equipment Engineering >> Amkor Technology (S) Pte Ltd >> 1 Kaki Bukit View >> #03-28 TechView Building >> Singapore 415941 >> Tel: (65) 6347 1131 >> Fax: (65) 6746 4815 >> >> >> >> Dave Angel >> >> >> 05/12/2010 09:50 AM >> >> >> To >> Sivapathasuntha Aruliah/S1/AAWW at Amkor >> cc >> tutor at python.org >> Subject >> Re: [Tutor] (no subject) >> >> >> >> >> >> >> >> >> Sivapathasuntha Aruliah wrote: >> >> >>> Hi >>> I am learning Python. When I tried to run any of the program for example >>> >>> >> >> >> >>> csv2html1_ans.py it displays the following message. This error is coming >>> >>> >> >> >> >>> on both Python24 & Python 31. That is whether i give the any one of the >>> following command >>> >>> COMMAND GIVEN >>> 1.C:\python24\python.exe C:\py3eg\quadratic.py >>> 2.C:\python31\python.exe C:\py3eg\quadratic.py >>> >>> A message below appears with the program name. Please advice me how to >>> >>> >> get >> >>> over from this issue >>> ERROR MESSAGE >>> command C:\py3eg\csv2html1_ans.py is not a valid Win32 application >>> >>> Regards, >>> Siva >>> Test Equipment Engineering >>> Amkor Technology (S) Pte Ltd >>> 1 Kaki Bukit View >>> #03-28 TechView Building >>> Singapore 415941 >>> Tel: (65) 6347 1131 >>> Fax: (65) 6746 4815 >>> >>> >>> >> Please copy and paste the actual contents of your DOS box, rather than >> paraphrasing. COMMAND hasn't been the normal shell name since Win95 days. >> You can't use numbers in front of commands in any shell I've used. The >> error message refers to a different file than anything you specified in your >> commands. >> >> What OS are you using? >> >> DaveA >> >> >> >> >> > Again, what OS are you using? > > I have no idea what the pop up comes from, but I suspect you have some > non-trivial code in that python program, perhaps that creates a gui. Is > there any tkinter stuff in it? > > As for "Access is Denied", it usually means you tried to access a > non-existent drive, or one which isn't currently mounted. For example, > referencing your CD drive with no platter in it. > > I don't know why "print_unicode_table" is undefined, but apparently you're > missing some code. > > > And the except clause changed between 2.x and 3.x, so you need to change > the syntax to match the particular interpreter you're using. They're not > compatible, although there's a utility to convert from 2.x to 3.x, I don't > think there's anything that reverses it. > > I'd suggest picking one version, and using only books and references that > are compatible with it till you're comfortable with the language. > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Wed May 12 07:35:27 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 00:35:27 -0500 Subject: [Tutor] (no subject) In-Reply-To: <4BEA1269.5080409@ieee.org> References: <4BEA1269.5080409@ieee.org> Message-ID: I'd have rather you top-posted, then I wouldn't have wasted 30 seconds scrolling past a bunch of irrelevant crap that I just gloss over anyway. If I want context I'll read the previous messages in the thread. but that's just MHO. -Luke On Tue, May 11, 2010 at 9:28 PM, Dave Angel wrote: > (1. Please don't top-post. ?It gets everything out of sequence, and is the > wrong convention for this forum > 2. Be sure and do a reply-all, so that the message goes to the forum. ?I'm > not here to give private advice. > 3. Use your editor's reply-quoting so that we can tell who wrote which > parts. ?Normally, you'll see that as either a leading ">" character or as a > "!" character. ?And one can tell which parts were written by whom by > counting the number of those at the beginning of each line) > > For my real response, see the end of the message, where it belongs. > > Sivapathasuntha Aruliah wrote: >> >> Dave >> Thank you very much for your response. I think I have problem with both >> Python23 & Python31. Please help. >> >> Python23 : The program works but programs written by Mark Summerfield in >> his book Programming in Python3 does not work. >> Python 31: When I run this program it says in the pop up window " >> C:\py3eg\csv2html1_ans.py is not a valid Win32 application" and on the the >> dos box it says Access is denied. >> Below is the dos box contents >> >> >> C:\>cd python31 >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>python.exe C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>cd.. >> >> C:\>cd python23 >> >> C:\Python23>python.exe C:\py3eg\hello.py >> ('Hello', 'World!') >> >> C:\Python23>python.exe C:\py3eg\print_unicode.py >> Traceback (most recent call last): >> ?File "C:\py3eg\print_unicode.py", line 30, in ? >> ? ?print_unicode_table(word) >> NameError: name 'print_unicode_table' is not defined >> >> C:\Python23>python.exe C:\py3eg\quadratic.py >> ?File "C:\py3eg\quadratic.py", line 14 >> ? ?except ValueError as err: >> ? ? ? ? ? ? ? ? ? ? ? ^ >> SyntaxError: invalid syntax >> >> >> >> >> Regards, >> Siva >> Test Equipment Engineering >> Amkor Technology (S) Pte Ltd >> 1 Kaki Bukit View >> #03-28 TechView Building >> Singapore 415941 >> Tel: (65) 6347 1131 >> Fax: (65) 6746 4815 >> >> >> >> Dave Angel >> >> >> 05/12/2010 09:50 AM >> >> >> To >> Sivapathasuntha Aruliah/S1/AAWW at Amkor >> cc >> tutor at python.org >> Subject >> Re: [Tutor] (no subject) >> >> >> >> >> >> >> >> >> Sivapathasuntha Aruliah wrote: >> >>> >>> Hi >>> I am learning Python. When I tried to run any of the program for example >>> >> >> >>> >>> csv2html1_ans.py it displays the following message. This error is coming >>> >> >> >>> >>> on both Python24 & Python 31. That is whether i give the any one of the >>> following command >>> >>> COMMAND GIVEN >>> 1.C:\python24\python.exe C:\py3eg\quadratic.py >>> 2.C:\python31\python.exe C:\py3eg\quadratic.py >>> >>> A message below appears with the program name. Please advice me how to >>> >> >> get >>> >>> over from this issue >>> ERROR MESSAGE >>> command ?C:\py3eg\csv2html1_ans.py is not a valid Win32 application >>> >>> Regards, >>> Siva >>> Test Equipment Engineering >>> Amkor Technology (S) Pte Ltd >>> 1 Kaki Bukit View >>> #03-28 TechView Building >>> Singapore 415941 >>> Tel: (65) 6347 1131 >>> Fax: (65) 6746 4815 >>> >>> >> >> Please copy and paste the actual contents of your DOS box, rather than >> paraphrasing. ?COMMAND hasn't been the normal shell name since Win95 days. >> ?You can't use numbers in front of commands in any shell I've used. ?The >> error message refers to a different file than anything you specified in your >> commands. >> >> What OS are you using? >> >> DaveA >> >> >> >> > > Again, what OS are you using? > > I have no idea what the pop up comes from, but I suspect you have some > non-trivial code in that python program, perhaps that creates a gui. ?Is > there any tkinter stuff in it? > > As for "Access is Denied", it usually means you tried to access a > non-existent drive, or one which isn't currently mounted. ?For example, > referencing your CD drive with no platter in it. > > I don't know why "print_unicode_table" is undefined, but apparently you're > missing some code. > > > And the except clause changed between 2.x and 3.x, so you need to change the > syntax to match the particular interpreter you're using. ?They're not > compatible, although there's a utility to convert from 2.x to 3.x, I don't > think there's anything that reverses it. > > I'd suggest picking one version, and using only books and references that > are compatible with it till you're comfortable with the language. > > DaveA > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From rabidpoobear at gmail.com Wed May 12 08:27:16 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 01:27:16 -0500 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: I'm just not going to quote previous threads because with my top-posting and dave's bottom-posting and whatever the heck Siva's posting was... whatever. Read previous e-mails if you need context. Siva is it possible that you accidentally installed the 64-bit version of python 3.1? -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Wed May 12 08:41:22 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 08:41:22 +0200 Subject: [Tutor] (no subject) In-Reply-To: References: <4BEA1269.5080409@ieee.org> Message-ID: <20100512084122.1045d876@o> On Wed, 12 May 2010 00:35:27 -0500 Luke Paireepinart wrote: > I'd have rather you top-posted, then I wouldn't have wasted 30 seconds > scrolling past a bunch of irrelevant crap that I just gloss over > anyway. > If I want context I'll read the previous messages in the thread. > but that's just MHO. > -Luke The problem in this case is not bottom posting (or better: interleaved), rather the "bunch of irrelevant crap" let by the previous poster. Many use an email client feature that quotes all by default, and places the cusor at the end by default as well; which is fine in many cases. But then if they don't bother reviewing the post before sending, & "pruning" irrelevant parts, we get all that stupid stuff (and the new part at the end). If the software would place the cursor on top... MHO, Denis ________________________________ vit esse estrany ? spir.wikidot.com From Sivapathasuntha.Aruliah at amkor.com Wed May 12 08:08:09 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Wed, 12 May 2010 14:08:09 +0800 Subject: [Tutor] (no subject) In-Reply-To: Message-ID: Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 Luke Paireepinart 05/12/2010 01:35 PM To Dave Angel cc Sivapathasuntha Aruliah/S1/AAWW at Amkor, "tutor at python.org" Subject Re: [Tutor] (no subject) I'd have rather you top-posted, then I wouldn't have wasted 30 seconds scrolling past a bunch of irrelevant crap that I just gloss over anyway. If I want context I'll read the previous messages in the thread. but that's just MHO. -Luke On Tue, May 11, 2010 at 9:28 PM, Dave Angel wrote: > (1. Please don't top-post. It gets everything out of sequence, and is the > wrong convention for this forum > 2. Be sure and do a reply-all, so that the message goes to the forum. I'm > not here to give private advice. > 3. Use your editor's reply-quoting so that we can tell who wrote which > parts. Normally, you'll see that as either a leading ">" character or as a > "!" character. And one can tell which parts were written by whom by > counting the number of those at the beginning of each line) > > For my real response, see the end of the message, where it belongs. > > Sivapathasuntha Aruliah wrote: >> >> Dave >> Thank you very much for your response. I think I have problem with both >> Python23 & Python31. Please help. >> >> Python23 : The program works but programs written by Mark Summerfield in >> his book Programming in Python3 does not work. >> Python 31: When I run this program it says in the pop up window " >> C:\py3eg\csv2html1_ans.py is not a valid Win32 application" and on the the >> dos box it says Access is denied. >> Below is the dos box contents >> >> >> C:\>cd python31 >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\quadratic.py >> Access is denied. >> >> C:\Python31>python C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>python.exe C:\py3eg\hello.py >> Access is denied. >> >> C:\Python31>cd.. >> >> C:\>cd python23 >> >> C:\Python23>python.exe C:\py3eg\hello.py >> ('Hello', 'World!') >> >> C:\Python23>python.exe C:\py3eg\print_unicode.py >> Traceback (most recent call last): >> File "C:\py3eg\print_unicode.py", line 30, in ? >> print_unicode_table(word) >> NameError: name 'print_unicode_table' is not defined >> >> C:\Python23>python.exe C:\py3eg\quadratic.py >> File "C:\py3eg\quadratic.py", line 14 >> except ValueError as err: >> ^ >> SyntaxError: invalid syntax >> >> >> >> >> Regards, >> Siva >> Test Equipment Engineering >> Amkor Technology (S) Pte Ltd >> 1 Kaki Bukit View >> #03-28 TechView Building >> Singapore 415941 >> Tel: (65) 6347 1131 >> Fax: (65) 6746 4815 >> >> >> >> Dave Angel >> >> >> 05/12/2010 09:50 AM >> >> >> To >> Sivapathasuntha Aruliah/S1/AAWW at Amkor >> cc >> tutor at python.org >> Subject >> Re: [Tutor] (no subject) >> >> >> >> >> >> >> >> >> Sivapathasuntha Aruliah wrote: >> >>> >>> Hi >>> I am learning Python. When I tried to run any of the program for example >>> >> >> >>> >>> csv2html1_ans.py it displays the following message. This error is coming >>> >> >> >>> >>> on both Python24 & Python 31. That is whether i give the any one of the >>> following command >>> >>> COMMAND GIVEN >>> 1.C:\python24\python.exe C:\py3eg\quadratic.py >>> 2.C:\python31\python.exe C:\py3eg\quadratic.py >>> >>> A message below appears with the program name. Please advice me how to >>> >> >> get >>> >>> over from this issue >>> ERROR MESSAGE >>> command C:\py3eg\csv2html1_ans.py is not a valid Win32 application >>> >>> Regards, >>> Siva >>> Test Equipment Engineering >>> Amkor Technology (S) Pte Ltd >>> 1 Kaki Bukit View >>> #03-28 TechView Building >>> Singapore 415941 >>> Tel: (65) 6347 1131 >>> Fax: (65) 6746 4815 >>> >>> >> >> Please copy and paste the actual contents of your DOS box, rather than >> paraphrasing. COMMAND hasn't been the normal shell name since Win95 days. >> You can't use numbers in front of commands in any shell I've used. The >> error message refers to a different file than anything you specified in your >> commands. >> >> What OS are you using? >> >> DaveA >> >> >> >> > > Again, what OS are you using? > > I have no idea what the pop up comes from, but I suspect you have some > non-trivial code in that python program, perhaps that creates a gui. Is > there any tkinter stuff in it? > > As for "Access is Denied", it usually means you tried to access a > non-existent drive, or one which isn't currently mounted. For example, > referencing your CD drive with no platter in it. > > I don't know why "print_unicode_table" is undefined, but apparently you're > missing some code. > > > And the except clause changed between 2.x and 3.x, so you need to change the > syntax to match the particular interpreter you're using. They're not > compatible, although there's a utility to convert from 2.x to 3.x, I don't > think there's anything that reverses it. > > I'd suggest picking one version, and using only books and references that > are compatible with it till you're comfortable with the language. > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hi I thank you for your prompt response. I am using WINXP. Possibly programs written for Python 3 may not work in Python2 as informed by you due to syntax unmatch. However when I try hello.py on both Python2 and python3, it works on Python2 but does not work in Python3. Here is what I get then followed by when OK prompt is clicked. This means that irrespective program code Python3 does not execute hello.py while python2 executes. This I suspect some setting error? Here is what I got when I run the same program on Python2 Please advice. I give below sourcecode of hello.py #! /usr/bin/env python3 print("Hello", "World!") -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 6999 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 2151 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 2826 bytes Desc: not available URL: From Sivapathasuntha.Aruliah at amkor.com Wed May 12 05:31:19 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Wed, 12 May 2010 11:31:19 +0800 Subject: [Tutor] Unable to run Programs on WINXP using Python3 In-Reply-To: Message-ID: Hi I thank you for your prompt response. I am using WINXP. Possibly programs written for Python 3 may not work in Python2 as informed by you due to syntax unmatch. However when I try programs written for Python3 in Python3 it first comes out with a pop up message "C:\python31\python.exe is not a valid Win32 application" When I click OK prompt on pop up message then on dos box it states "Access is denied." I tried various types such as python, python.exe and without these two also but same pop up message comes out. Please advice. Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 ramya natarajan 05/12/2010 10:51 AM To Dave Angel cc Sivapathasuntha Aruliah/S1/AAWW at Amkor, "tutor at python.org" Subject Re: [Tutor] (no subject) I really thank you all for the quick response, It was really helpful!! On Tue, May 11, 2010 at 7:28 PM, Dave Angel wrote: (1. Please don't top-post. It gets everything out of sequence, and is the wrong convention for this forum 2. Be sure and do a reply-all, so that the message goes to the forum. I'm not here to give private advice. 3. Use your editor's reply-quoting so that we can tell who wrote which parts. Normally, you'll see that as either a leading ">" character or as a "!" character. And one can tell which parts were written by whom by counting the number of those at the beginning of each line) For my real response, see the end of the message, where it belongs. Sivapathasuntha Aruliah wrote: Dave Thank you very much for your response. I think I have problem with both Python23 & Python31. Please help. Python23 : The program works but programs written by Mark Summerfield in his book Programming in Python3 does not work. Python 31: When I run this program it says in the pop up window " C:\py3eg\csv2html1_ans.py is not a valid Win32 application" and on the the dos box it says Access is denied. Below is the dos box contents C:\>cd python31 C:\Python31>python C:\py3eg\quadratic.py Access is denied. C:\Python31>python C:\py3eg\quadratic.py Access is denied. C:\Python31>python C:\py3eg\hello.py Access is denied. C:\Python31>python.exe C:\py3eg\hello.py Access is denied. C:\Python31>cd.. C:\>cd python23 C:\Python23>python.exe C:\py3eg\hello.py ('Hello', 'World!') C:\Python23>python.exe C:\py3eg\print_unicode.py Traceback (most recent call last): File "C:\py3eg\print_unicode.py", line 30, in ? print_unicode_table(word) NameError: name 'print_unicode_table' is not defined C:\Python23>python.exe C:\py3eg\quadratic.py File "C:\py3eg\quadratic.py", line 14 except ValueError as err: ^ SyntaxError: invalid syntax Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 Dave Angel 05/12/2010 09:50 AM To Sivapathasuntha Aruliah/S1/AAWW at Amkor cc tutor at python.org Subject Re: [Tutor] (no subject) Sivapathasuntha Aruliah wrote: Hi I am learning Python. When I tried to run any of the program for example csv2html1_ans.py it displays the following message. This error is coming on both Python24 & Python 31. That is whether i give the any one of the following command COMMAND GIVEN 1.C:\python24\python.exe C:\py3eg\quadratic.py 2.C:\python31\python.exe C:\py3eg\quadratic.py A message below appears with the program name. Please advice me how to get over from this issue ERROR MESSAGE command C:\py3eg\csv2html1_ans.py is not a valid Win32 application Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 Please copy and paste the actual contents of your DOS box, rather than paraphrasing. COMMAND hasn't been the normal shell name since Win95 days. You can't use numbers in front of commands in any shell I've used. The error message refers to a different file than anything you specified in your commands. What OS are you using? DaveA Again, what OS are you using? I have no idea what the pop up comes from, but I suspect you have some non-trivial code in that python program, perhaps that creates a gui. Is there any tkinter stuff in it? As for "Access is Denied", it usually means you tried to access a non-existent drive, or one which isn't currently mounted. For example, referencing your CD drive with no platter in it. I don't know why "print_unicode_table" is undefined, but apparently you're missing some code. And the except clause changed between 2.x and 3.x, so you need to change the syntax to match the particular interpreter you're using. They're not compatible, although there's a utility to convert from 2.x to 3.x, I don't think there's anything that reverses it. I'd suggest picking one version, and using only books and references that are compatible with it till you're comfortable with the language. DaveA _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 12855 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4196 bytes Desc: not available URL: From alan.gauld at btinternet.com Wed May 12 09:33:37 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 08:33:37 +0100 Subject: [Tutor] Unable to run Programs on WINXP using Python3 References: Message-ID: "Sivapathasuntha Aruliah" wrote > syntax unmatch. However when I try programs written for Python3 in Python3 > it first comes out with a pop up message "C:\python31\python.exe is not a > valid Win32 application" I'd concentrate on this first. If XP cant run Python all else is doomed to fail! So I'd uninstall Python 3.1 and reinstall it. Then try again just starting up python3. Only after you have a basic Python3 program running try to get the bigger examples working. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed May 12 09:35:12 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 08:35:12 +0100 Subject: [Tutor] Help required to count no of lines that are until 1000 characters References: <4BEA0F42.7080100@dejaviewphoto.com> <4BEA1123.6050704@ieee.org> Message-ID: "Dave Angel" wrote > But you have a serious bug in your code, that nobody in the first five > responses has addressed. That while loop will loop over the first line > repeatedly, till it reaches or exceeds 1000, regardless of the length of > subsequent lines. Oooh, good catch, I completely missed that one! :-) Alan G From Sivapathasuntha.Aruliah at amkor.com Wed May 12 09:39:47 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Wed, 12 May 2010 15:39:47 +0800 Subject: [Tutor] (no subject) In-Reply-To: Message-ID: Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 Luke Paireepinart 05/12/2010 02:27 PM To Sivapathasuntha Aruliah/S1/AAWW at Amkor cc Dave Angel , "tutor at python.org" Subject Re: [Tutor] (no subject) I'm just not going to quote previous threads because with my top-posting and dave's bottom-posting and whatever the heck Siva's posting was... whatever. Read previous e-mails if you need context. Siva is it possible that you accidentally installed the 64-bit version of python 3.1? Luke I am not sure how to install 32 bit or 64 bit I cannot remember how I installed it. However I just checked and there are two downloads which are isted here. Is it the first one I have to do on the web page http://www.python.org/download/ ? ?Python 3.1.2 Windows x86 MSI Installer (Windows binary -- does not include source) ?Python 3.1.2 Windows X86-64 MSI Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source) -------------- next part -------------- An HTML attachment was scrubbed... URL: From Sivapathasuntha.Aruliah at amkor.com Wed May 12 10:20:21 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Wed, 12 May 2010 16:20:21 +0800 Subject: [Tutor] (no subject) In-Reply-To: Message-ID: Luke Paireepinart 05/12/2010 02:27 PM To Sivapathasuntha Aruliah/S1/AAWW at Amkor cc Dave Angel , "tutor at python.org" Subject Re: [Tutor] (no subject) I'm just not going to quote previous threads because with my top-posting and dave's bottom-posting and whatever the heck Siva's posting was... whatever. Read previous e-mails if you need context. Siva is it possible that you accidentally installed the 64-bit version of python 3.1? Hi Luke I checked again and found that Python Shell has the following which is win32 Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 12 11:13:39 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 10:13:39 +0100 Subject: [Tutor] (no subject) References: Message-ID: "Sivapathasuntha Aruliah" wrote >>Siva is it possible that you accidentally installed the 64-bit version >> of python 3.1? > > Luke > I am not sure how to install 32 bit or 64 bit > I cannot remember how I installed it. However I just checked and there are > two downloads which are isted here. Is it the first one I have to do on > the web page http://www.python.org/download/ ? > > ?Python 3.1.2 Windows x86 MSI Installer (Windows binary -- does not > include source) Yes, the first is the win32 program. Alan G. From rabidpoobear at gmail.com Wed May 12 11:26:10 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 04:26:10 -0500 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: > > Siva is it possible that you accidentally installed the 64-bit version of python 3.1? > Hi Luke > I checked again and found that Python Shell has the following which is win32 > > Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > >>> Can you reply in plain-text next time? ?Your html e-mails are very broken for me and really hard to read. ?Everything is tabbed over randomly and the e-mails don't appear to be in the correct order. How did you start this python console? ?If you can start the console you should be able to run scripts just fine. From lie.1296 at gmail.com Wed May 12 11:49:47 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 12 May 2010 19:49:47 +1000 Subject: [Tutor] Unable to run Programs on WINXP using Python3 In-Reply-To: References: Message-ID: On 05/12/10 13:31, Sivapathasuntha Aruliah wrote: > Hi > I thank you for your prompt response. I am using WINXP. Possibly programs > written for Python 3 may not work in Python2 as informed by you due to > syntax unmatch. Very unlikely. If python is told to execute a faulty script (or even arbitrary, non-python file), python would fail would fail with "SyntaxError" exception. > However when I try programs written for Python3 in Python3 > it first comes out with a pop up message "C:\python31\python.exe is not a > valid Win32 application". Your error message tells that either Python itself has crashed or Windows refuse to execute python.exe since it detected that python.exe is not a program. > When I click OK prompt on pop up message then on > dos box it states "Access is denied." I tried various types such as > python, python.exe and without these two also but same pop up message > comes out. Please advice. How did you install python? Did the installer or the machine crashed half-way through installation? Try reinstalling Python, make sure you download the correct installer. Can you start the interactive interpreter? Try starting just C:\Python26\python.exe (without script argument) From mbnoimi at gmx.com Wed May 12 13:09:05 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Wed, 12 May 2010 13:09:05 +0200 Subject: [Tutor] First steps for C++/Qt developers Message-ID: <4BEA8C51.8050305@gmx.com> Hi All, This is my first post and I want to set a plan for learning python syntax within 2 weeks. I'm C++/Qt desktop developer and I want to learn python for creating rapid web applications so I read some articles about that and I found that django is suitable framework -I guess- but I still need some info to complete my plan then applying it. 1. From where I can start python by examples for C++ developers (I don't want documentation tutorials cuz I need simple tutorials compares between C++ & python syntax just at start point)? 2. What's most IDE like Qt Creator (smart code completing is very important to me)? 3. One of most different -I guess- between PHP & ASP.net is separated html code, does python separates html code or not? 4. Do I need any pre knowledge about CGI principles before stating developing web application with python? 5. Does django provide rapid framework for web applications just like Qt? or you suggest better framework (I don't want to create web applications from scratch)? 6. What's most suitable packaging tool (deploying desktop applications) for Windows OS? 7. Does the period of 2 week enough for learning python syntax & basics of web developing? 8. Does any one tested an experience just like me (learning a new language within short period could be crazy ;-) )? -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed May 12 12:12:29 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 12 May 2010 20:12:29 +1000 Subject: [Tutor] (no subject) In-Reply-To: References: <4BEA1269.5080409@ieee.org> Message-ID: <201005122012.29692.steve@pearwood.info> On Wed, 12 May 2010 03:35:27 pm Luke Paireepinart wrote: > I'd have rather you top-posted, then I wouldn't have wasted 30 > seconds scrolling past a bunch of irrelevant crap that I just gloss > over anyway. > If I want context I'll read the previous messages in the thread. > but that's just MHO. The trick is not to top post, or bottom post, but in-line posting after trimming the quotes to remove extraneous and unnecessary text, while still leaving sufficient text to give context. -- Steven D'Aprano From denis.spir at gmail.com Wed May 12 13:03:17 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 13:03:17 +0200 Subject: [Tutor] (no subject) In-Reply-To: <201005122012.29692.steve@pearwood.info> References: <4BEA1269.5080409@ieee.org> <201005122012.29692.steve@pearwood.info> Message-ID: <20100512130317.055ae1b4@o> On Wed, 12 May 2010 20:12:29 +1000 Steven D'Aprano wrote: > On Wed, 12 May 2010 03:35:27 pm Luke Paireepinart wrote: > > I'd have rather you top-posted, then I wouldn't have wasted 30 > > seconds scrolling past a bunch of irrelevant crap that I just gloss > > over anyway. > > If I want context I'll read the previous messages in the thread. > > but that's just MHO. > > The trick is not to top post, or bottom post, but in-line posting after > trimming the quotes to remove extraneous and unnecessary text, while > still leaving sufficient text to give context. ditto ! Denis ________________________________ vit esse estrany ? spir.wikidot.com From denis.spir at gmail.com Wed May 12 13:25:01 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 13:25:01 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEA8C51.8050305@gmx.com> References: <4BEA8C51.8050305@gmx.com> Message-ID: <20100512132501.67039a97@o> On Wed, 12 May 2010 13:09:05 +0200 "M. Bashir Al-Noimi" wrote: > Hi All, > > This is my first post and I want to set a plan for learning python > syntax within 2 weeks. > > I'm C++/Qt desktop developer and I want to learn python for creating > rapid web applications so I read some articles about that and I found > that django is suitable framework -I guess- but I still need some info > to complete my plan then applying it. Just a few comments: > 1. From where I can start python by examples for C++ developers (I > don't want documentation tutorials cuz I need simple tutorials > compares between C++ & python syntax just at start point)? Depending on what you really mean with these words, it may be a wrong approach, imho. Learning a new language is not just a question of catching syntactic patterns, not even if you properly get associated semantics. It's rather a question of developping a new way of watching topics or problems, and thus modelling & designing differently. Else, what you end up with is "charabia", like if translating form arab to chinese word-for-word ;-) This is especially important when passing from a static to a dynamic language. Dynamicity opens new doors to modelling practice fields. Or rather: when using a static language these doors are closed... > 7. Does the period of 2 week enough for learning python syntax & > basics of web developing? See note above. > 8. Does any one tested an experience just like me (learning a new > language within short period could be crazy ;-) )? Ditto. And yes, when I started with python intensively for a period, I used its OO framework like if it were static! It took me a rather long time to realise I had wrong mental schemes, barriers infact. (Actually, for a while, when watching properly written dynamic code, I even thought it was wrong & ugly & tricky stuff. instead I had a cube of concrete where there should be a brain.) But dynamicity is not the only point. Even rather similar languages like eg Python & Ruby & Lua develop different programming approaches, styles and flavors. denis ________________________________ vit esse estrany ? spir.wikidot.com From mbnoimi at gmx.com Wed May 12 15:01:09 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Wed, 12 May 2010 15:01:09 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <20100512132501.67039a97@o> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> Message-ID: <4BEAA695.7030401@gmx.com> Hi denis, On 12/05/2010 01:25 ?, spir ? wrote: > On Wed, 12 May 2010 13:09:05 +0200 > "M. Bashir Al-Noimi" wrote: > > >> Hi All, >> >> This is my first post and I want to set a plan for learning python >> syntax within 2 weeks. >> >> I'm C++/Qt desktop developer and I want to learn python for creating >> rapid web applications so I read some articles about that and I found >> that django is suitable framework -I guess- but I still need some info >> to complete my plan then applying it. >> > Just a few comments: > humm, you confused me I'm still a newbie and I don't know anything about differences between C++ & python even I couldn't understand you. How C++ is a static language !??!! I use C++ since I was in high school and I still found it most flexible language, and if I found C++ is suitable for rapid web applications (you can read about wt C++ web toolkit) I'll never try to learn a new language but C++ in fact not flexible for web applications, for that I decided to learn python. In addition to your confused comment you didn't answer vital my questions. *PS* Please notice that if I knew the differences between C++ & python or even knew enough info about python be sure that i'll never post any question here cuz there is python-list at python.org mailing list for python questions not for newbie just like me :-[ > > >> 1. From where I can start python by examples for C++ developers (I >> don't want documentation tutorials cuz I need simple tutorials >> compares between C++& python syntax just at start point)? >> > Depending on what you really mean with these words, it may be a wrong approach, imho. Learning a new language is not just a question of catching syntactic patterns, not even if you properly get associated semantics. It's rather a question of developping a new way of watching topics or problems, and thus modelling& designing differently. Else, what you end up with is "charabia", like if translating form arab to chinese word-for-word ;-) > This is especially important when passing from a static to a dynamic language. Dynamicity opens new doors to modelling practice fields. Or rather: when using a static language these doors are closed... > > >> 7. Does the period of 2 week enough for learning python syntax& >> basics of web developing? >> > See note above. > > >> 8. Does any one tested an experience just like me (learning a new >> language within short period could be crazy ;-) )? >> > Ditto. And yes, when I started with python intensively for a period, I used its OO framework like if it were static! It took me a rather long time to realise I had wrong mental schemes, barriers infact. (Actually, for a while, when watching properly written dynamic code, I even thought it was wrong& ugly& tricky stuff. instead I had a cube of concrete where there should be a brain.) > But dynamicity is not the only point. Even rather similar languages like eg Python& Ruby& Lua develop different programming approaches, styles and flavors. > > > denis > ________________________________ > > vit esse estrany ? > > spir.wikidot.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed May 12 14:25:36 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 12 May 2010 08:25:36 -0400 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEAA695.7030401@gmx.com> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> <4BEAA695.7030401@gmx.com> Message-ID: <4BEA9E40.1030306@ieee.org> M. Bashir Al-Noimi wrote: > >> >> > humm, you confused me I'm still a newbie and I don't know anything > about differences between C++ & python even I couldn't understand you. > How C++ is a static language !??!! > In C++, every variable is declared, and the type of that variable is static over its lifetime. The only flexibility there is that a variable may also get a value of some derived type of its declared type. In Python, variables have no fixed type at all, only the objects (that they're bound to) have type. A variable can be an integer one time, a string the next, and an arbitrary object after that. DaveA From denis.spir at gmail.com Wed May 12 14:37:54 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 14:37:54 +0200 Subject: [Tutor] object structure Message-ID: <20100512143754.4638fe85@o> Hello, Python objects are supposed to be mainly a structure composed of a (pointer to a) type and a (pointer to a) value; and to be more or less implemented that way in the C version. When an object is of a standard type like a number, the value field would then point to a C value, or rather in most cases to a custom piece of data built on top of C values. Anyway, this is transparent on the python language side. For an int, for instance, the actual value, in the common sense of the term, is unreachable: the python object has or is equivalent to a value, but does not provide a .value attribute: indeed, it would be a C thingie. In fact, since the object structure's value field points to this value, there is no place for a set of attributes (a dict) like custom objects have: i = 1 ; i.name = "one" # --> AttributeError: 'int' object has no attribute 'name' One can still address predefined fields of standard objects: i = 1 ; print i.__add__ # --> But in fact the method __add__ is found on the class int, not on i; and as the text says it's not even really a method. Instead this works, indeed: class C(object): pass c = C() ; c.name = 'c' Precisely, custom objects have a dict to store arbitrary attributes. But they have no value in the common sense. Or rather their value, in the sense of what is pointed by the value field of the structure representing this object, would precisely be a dict representing a set of attributes. If this holds true, then an object either has a transparent and inaccessible value in the common sense of the term (eg 1), or a dict of attributes (eg {name:'x',count:3}), which are accessible on the python side. Comments, critics, corrections welcome. Now, one can combine both, for instance by subtyping a standard type: class Integer(int): pass i = Integer(1) i.name = "one" print i,i.name # --> "1 one" An instance of Integer has both a value 1 and a dict. Actually, one can do more sophisticated things (below __new__ is needed because of some limitation in float): class Constant(float): def __new__(cls, name, value): obj = float.__new__(cls, value) obj.name = name return obj def __str__(self): return "%s:%s" %(self.name, float.__str__(self)) PI = Constant("pi", 3.14) print PI # --> "pi:3.14" In either case, the actual numerical value is transparently stored and inaccessible: reason why I need to use float.__str__ to print it out. But there also is a dict available to store attributes. So, a question arises: does the structure representing a Constant object like PI have one more field? Namely for type, value *and* dict? Denis ________________________________ vit esse estrany ? spir.wikidot.com From denis.spir at gmail.com Wed May 12 14:52:03 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 14:52:03 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEAA695.7030401@gmx.com> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> <4BEAA695.7030401@gmx.com> Message-ID: <20100512145203.2f44e640@o> On Wed, 12 May 2010 15:01:09 +0200 "M. Bashir Al-Noimi" wrote: > humm, you confused me I'm still a newbie and I don't know anything about > differences between C++ & python even I couldn't understand you. How C++ > is a static language !??!! Have a look at: http://en.wikipedia.org/wiki/Dynamic_language http://en.wikipedia.org/wiki/Static-typing_%28programming_languages%29#Static_typing (and the rest of the article) Denis ________________________________ vit esse estrany ? spir.wikidot.com From mbnoimi at gmx.com Wed May 12 16:12:02 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Wed, 12 May 2010 16:12:02 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEA9E40.1030306@ieee.org> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> <4BEAA695.7030401@gmx.com> <4BEA9E40.1030306@ieee.org> Message-ID: <4BEAB732.7070305@gmx.com> Hi On 12/05/2010 02:25 ?, Dave Angel wrote: > > > M. Bashir Al-Noimi wrote: >> >>> >> humm, you confused me I'm still a newbie and I don't know anything >> about differences between C++ & python even I couldn't understand >> you. How C++ is a static language !??!! >> > In C++, every variable is declared, and the type of that variable is > static over its lifetime. The only flexibility there is that a > variable may also get a value of some derived type of its declared > type. In Python, variables have no fixed type at all, only the > objects (that they're bound to) have type. A variable can be an > integer one time, a string the next, and an arbitrary object after that. nice that's truly easy i think this options are mix of java+VB (gc+no need to decalre the variable) is it? -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbnoimi at gmx.com Wed May 12 16:12:55 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Wed, 12 May 2010 16:12:55 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <20100512145203.2f44e640@o> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> <4BEAA695.7030401@gmx.com> <20100512145203.2f44e640@o> Message-ID: <4BEAB767.5090709@gmx.com> Hi On 12/05/2010 02:52 ?, spir ? wrote: > On Wed, 12 May 2010 15:01:09 +0200 > "M. Bashir Al-Noimi" wrote: > > >> humm, you confused me I'm still a newbie and I don't know anything about >> differences between C++& python even I couldn't understand you. How C++ >> is a static language !??!! >> > Have a look at: > http://en.wikipedia.org/wiki/Dynamic_language > http://en.wikipedia.org/wiki/Static-typing_%28programming_languages%29#Static_typing > (and the rest of the article) > Thanks. What about the other questions, IDE, web framework...etc? -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net From denis.spir at gmail.com Wed May 12 16:00:16 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 16:00:16 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEAB767.5090709@gmx.com> References: <4BEA8C51.8050305@gmx.com> <20100512132501.67039a97@o> <4BEAA695.7030401@gmx.com> <20100512145203.2f44e640@o> <4BEAB767.5090709@gmx.com> Message-ID: <20100512160016.4ce0810f@o> On Wed, 12 May 2010 16:12:55 +0200 "M. Bashir Al-Noimi" wrote: > Hi > > On 12/05/2010 02:52 ?, spir ? wrote: > > On Wed, 12 May 2010 15:01:09 +0200 > > "M. Bashir Al-Noimi" wrote: > > > > > >> humm, you confused me I'm still a newbie and I don't know anything about > >> differences between C++& python even I couldn't understand you. How C++ > >> is a static language !??!! > >> > > Have a look at: > > http://en.wikipedia.org/wiki/Dynamic_language > > http://en.wikipedia.org/wiki/Static-typing_%28programming_languages%29#Static_typing > > (and the rest of the article) > > > Thanks. > > What about the other questions, IDE, web framework...etc? > Personly have no experience at all with http://en.wikipedia.org/wiki/Django_%28web_framework%29. And few in web dev in general. Use no IDE properly speaking (you may discover it's not that advantageous with flexible languages, rather a heavy weight in everyday life ;-) but it's only my opinion). Denis ________________________________ vit esse estrany ? spir.wikidot.com From mbnoimi at gmx.com Wed May 12 17:02:56 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Wed, 12 May 2010 17:02:56 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: References: <4BEA8C51.8050305@gmx.com> Message-ID: <4BEAC320.4060907@gmx.com> Thanks a lot Walter for valuable reply, On 12/05/2010 03:09 ?, Walter Prins wrote: > > On 12 May 2010 12:09, M. Bashir Al-Noimi > wrote: > > 1. From where I can start python by examples for C++ developers > (I don't want documentation tutorials cuz I need simple > tutorials compares between C++ & python syntax just at start > point)? > > Not sure about actual examples, but have a look here: > http://wiki.python.org/moin/LanguageComparisons > > As you say, this is only a starting point however, because Python > really introduces new idioms (natural ways of expression) that does > not exist in other languages. The bigger task is learning proper > Python idioms/ways of expressing your intent naturally in Python, > rather than writing C++ in Python syntax. For me this comparison is enough it's just a magic drug for courage :-P :-P :-P > 1. What's most IDE like Qt Creator (smart code completing is > very important to me)? > 2. One of most different -I guess- between PHP & ASP.net is > separated html code, does python separates html code or not? > > There are multiple ways to do web development in Python, including (as > you put it) PHP style and ASP.Net style. There isn't however really a > RAD/visual environment specifically for *web* development in Python, > however personally I would suggest this is not neccesarily a negative > and may indeed be a positive. > > 1. Do I need any pre knowledge about CGI principles before > stating developing web application with python? > > That depends, but I'd hazard to say understanding web (including CGI) > principles would be neccesary. > > 1. Does django provide rapid framework for web applications > just like Qt? or you suggest better framework (I don't want > to create web applications from scratch)? > > QT is a mostly a UI widget library/framework, DJango is patently not > really a UI widget library, although it takes care of a lot other > details that service in the context of web development. > > At the risk of confusing matters further, you may want to look into > the "Pyjamas" project. > > Pyjamas is essentially a Python to Javascript compiler with a UI > widget abstraction layer and a Web UI layer of its, and that has > bindings to GTK (similar to QT) or its own web-based UI controls. As > a result, applictions targetting/using Pyjamas are/can be written in > pure python, much like any conventional GUI desktop applicatoin would > be, but which can then either run either as a web application or a GUI > application depending on which binding is selected. > > More on the Pyjamas project here: http://pyjs.org/ Nice I read about same thing in Qt labs http://labs.trolltech.com/blogs/2009/09/18/qt-in-the-cloud-with-qwebclient/ but I still have a tiny two questions, pyjamas comes with UI widgets which can bind to web/js or desktop/GTK, Qt or wxWidget ui, *could I use any ui desinger (like Glade) for designing pyjamas ui?* pyjamas from code side like desktop ui where DJango is a full web framework so I'm wondering, *do I need to use one of them or both together (in case if i want to gain the benefits of ajax options)? * > > 1. > > > 2. What's most suitable packaging tool (deploying desktop > applications) for Windows OS? > > There are several, each with pro's and con's. See for example here: > http://pypi.python.org/pypi/bbfreeze/0.95.4 > http://www.py2exe.org/ > > Which one is most suitable will depend on the context you're operating in. > > 1. Does the period of 2 week enough for learning python syntax > & basics of web developing? > > You can probably get going in 2 weeks, but of course you'll not have > mastered anything in that period and you'll be learning well beyond > the initial 2 weeks (as you would be with any new language/platform.) > > Walter > > -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben at richmondtravelsolihull.co.uk Wed May 12 14:33:46 2010 From: ben at richmondtravelsolihull.co.uk (Ben Millane) Date: Wed, 12 May 2010 13:33:46 +0100 Subject: [Tutor] Character creator program Message-ID: <6EDCF82A7DE74520B7233729BE618079@BenPC> Ok first off here is a link to my code http://pastie.org/956909 My problem is, that although I have the program working how I want it to work in general. I still have an issue with the user input section. If my user inputs any of the options from the menu, the program works as it should. But say if the user accidentally pressed enter with no option entered the program ends with an error message, or if they put in the incorrect number or a letter, it returns an error. What I would like to happen is for a "Choice invalid, please choose another option" message to come up if this happens, but I am unsure how to put this in. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 12 18:56:37 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 17:56:37 +0100 Subject: [Tutor] First steps for C++/Qt developers References: <4BEA8C51.8050305@gmx.com> Message-ID: "M. Bashir Al-Noimi" wrote > This is my first post and I want to set a plan for learning python > syntax within 2 weeks. Learning python syntax in 2 weeks is easily achievable if you go through the official tutorial at python.org. It is designed for people coming from another language, especially C++ or Java. If you work through it at a sensible pace - ie slow enough to think about it - you should be done in 2 or 3 days. From there you will know enough to start working through the Django tutorial material and write short simple Python function and programs. > 1. From where I can start python by examples for C++ developers (I > don't want documentation tutorials cuz I need simple tutorials > compares between C++ & python syntax just at start point)? As others have said, trying to convert your knowledge of C++ into Python is likely to result in you writing Python that looks like C++. It is also likely to introduce bad habits based on C++ limitations. You need to learn the Python way of doing things (and its limitations!) > 2. What's most IDE like Qt Creator (smart code completing is very > important to me)? Most Python IDEs do at least basic tooltips and such but if you are familiar with Eclipse then you can install PyDev and get all the usual Eclipse tools, including a good debugger. The one thing that is less useful in Eclipse is its interpreter prompt, but you can just use a tool like IDLE or Pythonwin or IPython in a terminal for that. > 3. One of most different -I guess- between PHP & ASP.net is separated > html code, does python separates html code or not? Python supports all the different styles of web development but Django, like most modern web tools separates code from presentation. > 4. Do I need any pre knowledge about CGI principles before stating > developing web application with python? Since it is all built on CGI it is always a good idea to understand what is really going on. But raw CGI skill is not essential to get started in Django. > 5. Does django provide rapid framework for web applications just like > Qt? or you suggest better framework (I don't want to create web > applications from scratch)? Django is a framework for rapid Web development but it is very different to, for example Glade. It is not an IDE or visual editor. > 6. What's most suitable packaging tool (deploying desktop > applications) for Windows OS? If its a web server based app then installing Python and Django on the server and loading your packages on is a better bet that trying to create an exe file os similar. > 7. Does the period of 2 week enough for learning python syntax & > basics of web developing? Yes, but it will be a steep curve. Ask lots of questions, and read lots of documentation. Ask the questions in the place most likely to answer them correctly! (ie python stuff here, Django stuff on a Django list) > 8. Does any one tested an experience just like me (learning a new > language within short period could be crazy ;-) )? I've been programming for about 30 years and have learned well over 20-30 languages. C++ was the hardest to learn and took about a month to get comfortable, most take a week. Becoming an expert, now that's a different thing altogether - it took me about 4 years to reach that in C++ and about 1 year to lose it again! I'm still learning in Python after 10+ years! (Mainly because I don't use it for industrial stength projects) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Wed May 12 19:01:35 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 19:01:35 +0200 Subject: [Tutor] Character creator program In-Reply-To: <6EDCF82A7DE74520B7233729BE618079@BenPC> References: <6EDCF82A7DE74520B7233729BE618079@BenPC> Message-ID: <20100512190135.5fae7f6e@o> On Wed, 12 May 2010 13:33:46 +0100 "Ben Millane" wrote: > Ok first off here is a link to my code > > http://pastie.org/956909 > > My problem is, that although I have the program working how I want it to work in general. I still have an issue with the user input section. If my user inputs any of the options from the menu, the program works as it should. But say if the user accidentally pressed enter with no option entered the program ends with an error message, or if they put in the incorrect number or a letter, it returns an error. > > What I would like to happen is for a "Choice invalid, please choose another option" message to come up if this happens, but I am unsure how to put this in. > You just need to add an "else" block to your main choice menu, coping with that special case: print a message and "continue", meaning go back to menu. But: I did not read in the detail, still there's something wrong in the overall structure. Namyly with "if total < 30" in the middle of the menu. Probably your app does not work in particuliar cases, and if does anyway, there is something wrong in the structure. Also, you may consider putting all the hardcoded data in statements (esp. character skill points) into proper data structures like lists or better dictionaries in your case... in you don't know yet about custom classes (your app would be pleased with a Character class, indeed). Even if you don't use a class, the logic of your app should not be hand-coded in a main menu, but in dedicated functions like addPoints(skill, count) All together, the aim is to make a clear structure where the *logic* of the app obviously appears. Denis ________________________________ vit esse estrany ? spir.wikidot.com From alan.gauld at btinternet.com Wed May 12 19:12:21 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 18:12:21 +0100 Subject: [Tutor] Character creator program References: <6EDCF82A7DE74520B7233729BE618079@BenPC> Message-ID: "Ben Millane" wrote > My problem is, that although I have the program working how I want it > to work in general. I still have an issue with the user input section. > If my user inputs any of the options from the menu, the program works > as it should. But say if the user accidentally pressed enter with no > option entered the program ends with an error message, > or if they put in the incorrect number or a letter, it returns an error. Thats what you told it to do! Work through the logic of all those if/elifs and see where a blank entry will go. Add the logic to catch it there. It will help if you use functions to reduce the amount of code between the if/elifs. That will make the code much more readable. Another option is then to use a dictionary to call the appropriate function, and if the key is not in the dictionary respond appropriately. Also you xccould move the "make a new selection bit out of the individual sections to the end of the while loop. It saves repetition (The DRY principle - Don't Repeat Yourself) and again keeps the code readable. You will see a short example of what you are doing in my tutorial under "Branching". Look at how I handled the invalid input situation in the "putting it all together" section for some ideas. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From aclark at aclark.net Wed May 12 19:52:23 2010 From: aclark at aclark.net (Alex Clark) Date: Wed, 12 May 2010 17:52:23 +0000 (UTC) Subject: [Tutor] Turbo Gears 2 Training this Saturday in DC Message-ID: Hi Folks, For those of you not on the python-general list, and if live in the Washington, D.C. USA area, we (the Zope/Python Users Group of DC) are having a `Turbo Gears 2` training this weekend: http://www.meetup.com/python-meetup-dc/messages/10123013/ The following day there will be a sprint, which we can cater to beginners if anyone is interested. If so, please contact me: aclark at aclark.net You can register for the class here: http://tg2-class.eventbrite.com/ Hope to see you there! Alex -- Alex Clark ? http://aclark.net Author of Plone 3.3 Site Administration ? http://aclark.net/plone-site-admin From sushikyc at gmail.com Wed May 12 19:58:45 2010 From: sushikyc at gmail.com (Su Chu) Date: Wed, 12 May 2010 13:58:45 -0400 Subject: [Tutor] Find Elements in List That Equal A Specific Value Message-ID: Hi there, I am new to Python. I am attempting to either define a "which" statement or to find a method that already exists to do this sort of operation. My problem is as follows: I have three lists, one with unique values (list 1), one a sequence of values that are not necessarily unique (list2), and a shorter list with the unique values of list 2 (list 3). List 1 and List 2 are of equal lengths. An example: list1 = [ 1, 2, 3, 4, 5, 6 ] list2 = [ 2, 2, 2, 5, 6, 6 ] list3 = [2, 5, 6] What I would like to do is find and sum the elements of list 1 given its corresponding element in list 2 is equal to some element in list 3. For example, the sum of the values in list1 given list2[i]==2 would be 1 + 2 + 3 = 6. the sum of the values in list1 given list2[i]==5 would be 4 the sum of the values in list1 given list2[i]==6 would be 5 + 6 = 11 and so on. Obtaining these values, I'd like to store them in a vector. This seems pretty simple if a 'which' statement exists e.g. (which values in list 1 == list3[k], and looping through k), but I can't find one. To write one seems to require a loop. I'm at a loss, if you could help I'd really appreciate it! -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Wed May 12 19:44:16 2010 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Wed, 12 May 2010 10:44:16 -0700 Subject: [Tutor] creating distribution lists in outlook Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> Pythonistas, I'm trying to find a way to get data into outlook. Specifically to create distribution lists. I need to create a number of distribution lists on a regular basis. Each list is created from one long master list. I will step through the master list 50 lines at a time, save as separate excel files and then read those into outlook. I'd LOVE to be able to automate this process. I've found ways to get contact and lists out of outlook but none on how to get stuff in. Is there a manual to address such python functions? Is it part of win32? Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed May 12 20:22:25 2010 From: bgailer at gmail.com (bob gailer) Date: Wed, 12 May 2010 14:22:25 -0400 Subject: [Tutor] Find Elements in List That Equal A Specific Value In-Reply-To: References: Message-ID: <4BEAF1E1.4000707@gmail.com> On 5/12/2010 1:58 PM, Su Chu wrote: > > I have three lists, one with unique values (list 1), one a sequence of > values that are not necessarily unique (list2), and a shorter list > with the unique values of list 2 (list 3). List 1 and List 2 are of > equal lengths. > > > An example: > list1 = [ 1, 2, 3, 4, 5, 6 ] > list2 = [ 2, 2, 2, 5, 6, 6 ] > list3 = [2, 5, 6] > > What I would like to do is find and sum the elements of list 1 given > its corresponding element in list 2 is equal to some element in list 3. > > For example, > the sum of the values in list1 given list2[i]==2 > would be 1 + 2 + 3 = 6. > the sum of the values in list1 given list2[i]==5 > would be 4 > the sum of the values in list1 given list2[i]==6 > would be 5 + 6 = 11 > > and so on. Obtaining these values, I'd like to store them in a vector. > result = [] for n in list3: result.append(sum(list1[x] for x in range(len(list1)) if list2[x] = n) > This seems pretty simple if a 'which' statement exists e.g. (which > values in list 1 == list3[k], and looping through k), but I can't find > one. To write one seems to require a loop. > -- Bob Gailer 919-636-4239 Chapel Hill NC From davea at ieee.org Wed May 12 20:26:00 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 12 May 2010 14:26:00 -0400 Subject: [Tutor] Find Elements in List That Equal A Specific Value In-Reply-To: References: Message-ID: <4BEAF2B8.2010208@ieee.org> Su Chu wrote: > Hi there, > > I am new to Python. I am attempting to either define a "which" statement or > to find a method that already exists to do this sort of operation. > > My problem is as follows: > I have three lists, one with unique values (list 1), one a sequence of > values that are not necessarily unique (list2), and a shorter list with the > unique values of list 2 (list 3). List 1 and List 2 are of equal lengths. > > > An example: > list1 = [ 1, 2, 3, 4, 5, 6 ] > list2 = [ 2, 2, 2, 5, 6, 6 ] > list3 = [2, 5, 6] > > What I would like to do is find and sum the elements of list 1 given its > corresponding element in list 2 is equal to some element in list 3. > > For example, > the sum of the values in list1 given list2[i]==2 > would be 1 + 2 + 3 = 6. > the sum of the values in list1 given list2[i]==5 > would be 4 > the sum of the values in list1 given list2[i]==6 > would be 5 + 6 = 11 > > and so on. Obtaining these values, I'd like to store them in a vector. > > This seems pretty simple if a 'which' statement exists e.g. (which values in > list 1 == list3[k], and looping through k), but I can't find one. To write > one seems to require a loop. > > What's wrong with a loop? > I'm at a loss, if you could help I'd really appreciate it! > > If this homework has a requirement that says don't use a loop, or don't use Python 3, or don't get the answer from the internet, how about saying so? To see if something is in a list, use: if x in list3 To combine two lists, use zip() To get more help, post some code that you've actually tried, and tell us why you think it's wrong. Then we can help fix it, rather than just solving the homework directly. DaveA From lie.1296 at gmail.com Wed May 12 21:28:12 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 13 May 2010 05:28:12 +1000 Subject: [Tutor] Find Elements in List That Equal A Specific Value In-Reply-To: References: Message-ID: On 05/13/10 03:58, Su Chu wrote: > Hi there, > > I am new to Python. I am attempting to either define a "which" statement or > to find a method that already exists to do this sort of operation. > > My problem is as follows: > I have three lists, one with unique values (list 1), one a sequence of > values that are not necessarily unique (list2), and a shorter list with the > unique values of list 2 (list 3). List 1 and List 2 are of equal lengths. > > > An example: > list1 = [ 1, 2, 3, 4, 5, 6 ] > list2 = [ 2, 2, 2, 5, 6, 6 ] > list3 = [2, 5, 6] > > What I would like to do is find and sum the elements of list 1 given its > corresponding element in list 2 is equal to some element in list 3. > > For example, > the sum of the values in list1 given list2[i]==2 > would be 1 + 2 + 3 = 6. > the sum of the values in list1 given list2[i]==5 > would be 4 > the sum of the values in list1 given list2[i]==6 > would be 5 + 6 = 11 > > and so on. Obtaining these values, I'd like to store them in a vector. > > This seems pretty simple if a 'which' statement exists e.g. (which values in > list 1 == list3[k], and looping through k), but I can't find one. To write > one seems to require a loop. your proposed 'which' statement, had they existed, is a kind of loop, so I'm wondering why you want to avoid loops. >>> [sum(a for a, b in zip(list1, list2) if b == n) for n in list3] [6, 4, 11] From inthefridge at gmail.com Wed May 12 21:44:30 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 12 May 2010 13:44:30 -0600 Subject: [Tutor] raw_input a directory path Message-ID: I have a search and replace script that I am having the user put in the directory path as raw_input. The problem lies when I run the script it doesn't pick that up for some reason. Is there an appropriate way to take a directory path as input? This is on windows... -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Wed May 12 22:10:56 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 15:10:56 -0500 Subject: [Tutor] Find Elements in List That Equal A Specific Value In-Reply-To: <4BEAF1E1.4000707@gmail.com> References: <4BEAF1E1.4000707@gmail.com> Message-ID: On Wed, May 12, 2010 at 1:22 PM, bob gailer wrote: > On 5/12/2010 1:58 PM, Su Chu wrote: >> >> I have three lists, one with unique values (list 1), one a sequence of >> values that are not necessarily unique (list2), and a shorter list with the >> unique values of list 2 (list 3). List 1 and List 2 are of equal lengths. >> >> What I would like to do is find and sum the elements of list 1 given its >> corresponding element in list 2 is equal to some element in list 3. >> > result = [] > for n in list3: > ?result.append(sum(list1[x] for x in range(len(list1)) if list2[x] = n) > bob, your parenthesis are unbalanced and you put an assignment instead of a comparison... I'd probably do this with a dictionary. d = {} for i, val in enumerate(list1): d[list2[i]] = val + d.get(list2[i], 0) # or whatever the default get code is. From denis.spir at gmail.com Wed May 12 22:32:23 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 12 May 2010 22:32:23 +0200 Subject: [Tutor] raw_input a directory path In-Reply-To: References: Message-ID: <20100512223223.6ef0fbf5@o> On Wed, 12 May 2010 13:44:30 -0600 Spencer Parker wrote: > I have a search and replace script that I am having the user put in the > directory path as raw_input. The problem lies when I run the script it > doesn't pick that up for some reason. Is there an appropriate way to take a > directory path as input? This is on windows... You'll need to post the relevant section of code, the result of running it using sample input(s), including full error message; and maybe... what you think about it ;-) Denis ________________________________ vit esse estrany ? spir.wikidot.com From rabidpoobear at gmail.com Wed May 12 22:33:50 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 15:33:50 -0500 Subject: [Tutor] raw_input a directory path In-Reply-To: References: Message-ID: On Wed, May 12, 2010 at 2:44 PM, Spencer Parker wrote: > I have a search and replace script that I am having the user put in the > directory path as raw_input. ?The problem lies when I run the script it > doesn't pick that up for some reason. ?Is there an appropriate way to take a > directory path as input? This is on windows... Well I could speculate about all the different problems this could be caused by, or you could provide more detail. Let's go with that. How do you know the script doesn't input the data correctly? and why do you think it doesn't? Where is the code that performs this? How are you running the script? Directory path should be no different than any other string. I'm guessing it has to do with backslashes but I'll wait until you explicate. -Luke From eike.welk at gmx.net Wed May 12 22:49:50 2010 From: eike.welk at gmx.net (Eike Welk) Date: Wed, 12 May 2010 22:49:50 +0200 Subject: [Tutor] object structure In-Reply-To: <20100512143754.4638fe85@o> References: <20100512143754.4638fe85@o> Message-ID: <201005122249.51056.eike.welk@gmx.net> Hello Denis! On Wednesday May 12 2010 14:37:54 spir ? wrote: > class Integer(int): pass > i = Integer(1) > i.name = "one" > print i,i.name # --> "1 one" > > An instance of Integer has both a value 1 and a dict. Actually, one can do > more sophisticated things (below __new__ is needed because of some > limitation in float): I think it's the other way round: It is quite odd that "Integer" works without "__new__". > class Constant(float): > def __new__(cls, name, value): > obj = float.__new__(cls, value) > obj.name = name > return obj > def __str__(self): > return "%s:%s" %(self.name, float.__str__(self)) > PI = Constant("pi", 3.14) > print PI # --> "pi:3.14" > > In either case, the actual numerical value is transparently stored and > inaccessible: reason why I need to use float.__str__ to print it out. But > there also is a dict available to store attributes. > > So, a question arises: does the structure representing a Constant object > like PI have one more field? Namely for type, value *and* dict? I think you are right. The instances of class "Constant" have a "__dict__" while the "float" instance don't have it. And obviously the "float" instances contain some hidden data to encode the value. However this mechanism (or a very similar one) is available to Python programmers too: classes with a "__slots__" declaration. http://docs.python.org/reference/datamodel.html#slots Instances of these classes have no "__dict__" attribute. The attributes declared with slots are also computed attributes. This means instead of accessing the attribute directly, the Python interpreter calls a function, which returns the attribute. (The function, really the descriptor, is stored in the class.) As you can inherit from classes with a "__slots__" declaration, I think all built in classes could be implemented elegantly with this underlying mechanism: - It is a mechanism for conserving memory (no "__dict__"), important for numbers. - It is also a mechanism for storing hidden data somewhere in the object. The implementation available to the Python programmers is not very exciting, but it could be used for everything. Eike. From inthefridge at gmail.com Wed May 12 23:07:24 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 12 May 2010 15:07:24 -0600 Subject: [Tutor] Fwd: raw_input a directory path In-Reply-To: References: Message-ID: Spreading it to the list... ---------- Forwarded message ---------- From: Spencer Parker Date: Wed, May 12, 2010 at 3:07 PM Subject: Re: [Tutor] raw_input a directory path To: Luke Paireepinart Forget all of what I said...I found the problem...I was encasing the variable with quotes instead of just letting it be a variable. I am dumb... On Wed, May 12, 2010 at 2:33 PM, Luke Paireepinart wrote: > On Wed, May 12, 2010 at 2:44 PM, Spencer Parker > wrote: > > I have a search and replace script that I am having the user put in the > > directory path as raw_input. The problem lies when I run the script it > > doesn't pick that up for some reason. Is there an appropriate way to > take a > > directory path as input? This is on windows... > > Well I could speculate about all the different problems this could be > caused by, > or you could provide more detail. > Let's go with that. > > How do you know the script doesn't input the data correctly? > and why do you think it doesn't? > Where is the code that performs this? > How are you running the script? > > Directory path should be no different than any other string. > I'm guessing it has to do with backslashes but I'll wait until you > explicate. > -Luke > -------------- next part -------------- An HTML attachment was scrubbed... URL: From inthefridge at gmail.com Wed May 12 23:16:47 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 12 May 2010 15:16:47 -0600 Subject: [Tutor] raw_input a directory path In-Reply-To: References: Message-ID: Here is the code: http://dpaste.com/hold/193862/ It still isn't working for me. I don't see it hitting the first for loop or even the second one. It runs without an error at all. I am inputing the directory as: \\Documents\ and\ Settings\\user\\Desktop\\test On Wed, May 12, 2010 at 3:07 PM, Spencer Parker wrote: > Spreading it to the list... > > > ---------- Forwarded message ---------- > From: Spencer Parker > Date: Wed, May 12, 2010 at 3:07 PM > Subject: Re: [Tutor] raw_input a directory path > To: Luke Paireepinart > > > Forget all of what I said...I found the problem...I was encasing the > variable with quotes instead of just letting it be a variable. I am dumb... > > > On Wed, May 12, 2010 at 2:33 PM, Luke Paireepinart > wrote: > >> On Wed, May 12, 2010 at 2:44 PM, Spencer Parker >> wrote: >> > I have a search and replace script that I am having the user put in the >> > directory path as raw_input. The problem lies when I run the script it >> > doesn't pick that up for some reason. Is there an appropriate way to >> take a >> > directory path as input? This is on windows... >> >> Well I could speculate about all the different problems this could be >> caused by, >> or you could provide more detail. >> Let's go with that. >> >> How do you know the script doesn't input the data correctly? >> and why do you think it doesn't? >> Where is the code that performs this? >> How are you running the script? >> >> Directory path should be no different than any other string. >> I'm guessing it has to do with backslashes but I'll wait until you >> explicate. >> -Luke >> > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 13 00:45:45 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 12 May 2010 22:45:45 +0000 (GMT) Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: References: <4BEA8C51.8050305@gmx.com> Message-ID: <258686.81278.qm@web86702.mail.ird.yahoo.com> Forwarded to the list. Please use Reply All when posting. From: Walter Prins To: Alan Gauld Sent: Wednesday, 12 May, 2010 18:36:31 Subject: Re: [Tutor] First steps for C++/Qt developers Regarding IDE's: I find Eclipse with PyDev pretty good, and it recently gained support for Django specifically, so it's a good fit from that point of view for you. This of course does not include a form designer, you can choose whatever tool you like there. As an aside, there's also Delphi RAD'esque tool called "Boa constructor" which aims to be a RAD IDE with forms designer for Python. Not sure how active the project is etc, but maybe worth a look. As for Pyjamas, I don't know enough about it to answer if and how it may be used together with Django, although I can't see any fundamental reasons why that should not be possible. But, perhaps it's an idea for you to just develop some feel for Python itself first, writing some things from scratch, and once you've got a bit of a Python foundation you can have a look at Pyjamas etc. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjpark at rondaandspencer.info Wed May 12 21:17:11 2010 From: sjpark at rondaandspencer.info (Spencer Parker) Date: Wed, 12 May 2010 13:17:11 -0600 Subject: [Tutor] raw_input a directory path Message-ID: I have a search and replace script that I am having the user put in the directory path as raw_input. The problem lies when I run the script it doesn't pick that up for some reason. Is there an appropriate way to take a directory path as input? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 13 00:52:29 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 May 2010 23:52:29 +0100 Subject: [Tutor] creating distribution lists in outlook References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> Message-ID: "Pirritano, Matthew" wrote > I've found ways to get contact and lists out of outlook but none on how > to get stuff in. Is there a manual to address such python functions? Is > it part of win32? You can use COM to communicate with Outlook via win32com. However the COM interface to Outlook was fairly obscure as I recall so it will likely take a fair bit of experimenting - the interactive prompt may be your friend here! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu May 13 01:08:20 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 13 May 2010 00:08:20 +0100 Subject: [Tutor] raw_input a directory path References: Message-ID: "Spencer Parker" wrote > Here is the code: > http://dpaste.com/hold/193862/ First comment: why are you using print followed by raw_input? You could put the prompt string inside raw_input - if you need a new line just put a \n in the prompt string. It will shorten and clarify the code. > It still isn't working for me. I don't see > it hitting the first for loop or even the second one. It runs without an > error at all. What if you put a print to echo the directory value? > I am inputing the directory as: \\Documents\ and\ > Settings\\user\\Desktop\\test I don't think you should need the double \\, Python will do that for you if its reading from raw_input - the print should confirm that. os.walk has a return value of a tuple with 3 values not 2. I'd expect a ValueError there... unless the walk returns nothing which would be true if the directory was empty - or did not exist? Again use the print to check the directory is valid. Try using listdir() on it to see if it can read it. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rabidpoobear at gmail.com Thu May 13 01:15:58 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 18:15:58 -0500 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> Message-ID: On Wed, May 12, 2010 at 12:44 PM, Pirritano, Matthew wrote: > Pythonistas, > > I?m trying to find a way to get data into outlook. Specifically to create > distribution lists. Have you considered looking into Outlook's Import feature for address books? It may be possible to import data from csv's already, and you may be able to structure these into groups in a clever way and avoid almost all of the work of interfacing Outlook to your program. Depending on the scale / frequency of this project it might not be feasible, but it's something to consider. -Luke From MPirritano at ochca.com Thu May 13 01:20:51 2010 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Wed, 12 May 2010 16:20:51 -0700 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> That's the way we've been doing it. The issue, and inspiration for pythonification is that the list has 1000+ emails. Outlook only allows about 50 per list, which leads to the need to create 20+ separate lists, which takes a considerable amount of time. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: Luke Paireepinart [mailto:rabidpoobear at gmail.com] Sent: Wednesday, May 12, 2010 4:16 PM To: Pirritano, Matthew Cc: tutor at python.org Subject: Re: [Tutor] creating distribution lists in outlook On Wed, May 12, 2010 at 12:44 PM, Pirritano, Matthew wrote: > Pythonistas, > > I'm trying to find a way to get data into outlook. Specifically to create > distribution lists. Have you considered looking into Outlook's Import feature for address books? It may be possible to import data from csv's already, and you may be able to structure these into groups in a clever way and avoid almost all of the work of interfacing Outlook to your program. Depending on the scale / frequency of this project it might not be feasible, but it's something to consider. -Luke From rabidpoobear at gmail.com Thu May 13 01:25:47 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 18:25:47 -0500 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> Message-ID: On Wed, May 12, 2010 at 6:20 PM, Pirritano, Matthew wrote: > That's the way we've been doing it. The issue, and inspiration for > pythonification is that the list has 1000+ emails. Outlook only allows > about 50 per list, which leads to the need to create 20+ separate lists, > which takes a considerable amount of time. > Can't you import them all at once though? from a single file? rather than 20 separate ones? (i.e. they would still be 20 lists but you'd only have to go through the import process once.) Are you trying to automate Outlook to send the e-mails, or just to load in the list? From MPirritano at ochca.com Thu May 13 01:32:42 2010 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Wed, 12 May 2010 16:32:42 -0700 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> Here's the thing. You can import them all at once. But then you have 1000+ contacts in one contacts folder. When you create the distribution lists a number get cut off if you try to put too many into the list. But there is no indication of how many went into the list. And there are not row numbers or anything like it in the contacts folders. So you'd have to keep in mind how far you got and then choose the next set, and it would be way to labor intensive if you were to count to 50 and then do that again. It's easier to create 20 separate files and make corresponding lists. But still takes a while. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: Luke Paireepinart [mailto:rabidpoobear at gmail.com] Sent: Wednesday, May 12, 2010 4:26 PM To: Pirritano, Matthew Cc: tutor at python.org Subject: Re: [Tutor] creating distribution lists in outlook On Wed, May 12, 2010 at 6:20 PM, Pirritano, Matthew wrote: > That's the way we've been doing it. The issue, and inspiration for > pythonification is that the list has 1000+ emails. Outlook only allows > about 50 per list, which leads to the need to create 20+ separate lists, > which takes a considerable amount of time. > Can't you import them all at once though? from a single file? rather than 20 separate ones? (i.e. they would still be 20 lists but you'd only have to go through the import process once.) Are you trying to automate Outlook to send the e-mails, or just to load in the list? From rabidpoobear at gmail.com Thu May 13 02:00:11 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 12 May 2010 19:00:11 -0500 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> Message-ID: On Wed, May 12, 2010 at 6:32 PM, Pirritano, Matthew wrote: > Here's the thing. You can import them all at once. But then you have > 1000+ contacts in one contacts folder. When you create the distribution > lists a number get cut off if you try to put too many into the list. But > there is no indication of how many went into the list. And there are not > row numbers or anything like it in the contacts folders. So you'd have > to keep in mind how far you got and then choose the next set, and it > would be way to labor intensive if you were to count to 50 and then do > that again. It's easier to create 20 separate files and make > corresponding lists. ?But still takes a while. Pardon me for continuing not to address your specific question, but this seems like something that businesses would commonly run into and I'd be very surprised if there's not a way around this already. Is there really no way to have a group of more than 50? Have you tried creating distribution lists based on contact groups? If this is possible you could automatically add all contacts to one specific group. I really think there's a simple solution if you look into it, but I don't have outlook, I use Thunderbird. Are you able to use a plugin to perform the work or is that against your company's policies? I just don't see automating with COM being the cleanest / most robust approach, but if you really want to try it hopefully we can help if you run into any python-specific issues! -Luke From steve at pearwood.info Thu May 13 02:20:01 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 13 May 2010 10:20:01 +1000 Subject: [Tutor] Find Elements in List That Equal A Specific Value In-Reply-To: References: Message-ID: <201005131020.02431.steve@pearwood.info> On Thu, 13 May 2010 03:58:45 am Su Chu wrote: > My problem is as follows: > I have three lists, one with unique values (list 1), one a sequence > of values that are not necessarily unique (list2), and a shorter list > with the unique values of list 2 (list 3). List 1 and List 2 are of > equal lengths. > > > An example: > list1 = [ 1, 2, 3, 4, 5, 6 ] > list2 = [ 2, 2, 2, 5, 6, 6 ] > list3 = [2, 5, 6] > > What I would like to do is find and sum the elements of list 1 given > its corresponding element in list 2 is equal to some element in list > 3. Rather than trying to find a single magic command that happens to do exactly what you want, let's break that up into individual steps and walk through it by hand. Start at the end -- you want to match elements from list2 which equals a particular value. Where that value comes from doesn't matter. results = [] for el in list2: if el = value: results.append(el) Unfortunately, doing this loses the information we really need: the *position* of the element. We don't actually care about the element itself. How can we get the position? That's what the enumerate() function is for, it takes a list and lazily returns pairs of (position, element). So let's adapt the for-loop to do the job: positions = [] for (pos, el) in enumerate(list2): if el == value: positions.append(pos) This can be written as a "list comprehension", which is syntactic sugar for a loop: positions = [pos for (pos, el) in enumerate(list2) if el == value] Now we have a list of positions. We need to extract the elements in list1 at those positions. Here's a for-loop to do it: elements = [] for pos in positions: # positions defined above elements.append(list1[pos]) And as list comps: positions = [pos for (pos, el) in enumerate(list2) if el == value] elements = [list1[pos] for pos in positions] But we don't need two separate loops, we can combine them into one loop: elements = [] for (pos, el) in enumerate(list2): if el == value: elements.append(list1[pos]) And as a single list comp: elements = [list1[pos] for (pos,el) in enumerate(list2) if el in list3] And now sum them: sum(elements) Now let's put this into a function, so you can call it with different values: def extract_and_sum(list1, list2, value): elements = [list1[i] for (i,x) in enumerate(list2) if x in list3] return sum(elements) And now call it in a loop: sums = [] for value in list3: sums.append(extract_and_sum(list1, list2, value)) And you are done. -- Steven D'Aprano From davea at ieee.org Thu May 13 04:00:49 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 12 May 2010 22:00:49 -0400 Subject: [Tutor] raw_input a directory path In-Reply-To: References: Message-ID: <4BEB5D51.8000301@ieee.org> Spencer Parker wrote: > Here is the code: > http://dpaste.com/hold/193862/ > > It still isn't working for me. I don't see > it hitting the first for loop or even the second one. It runs without an > error at all. > > I am inputing the directory as: \\Documents\ and\ > Settings\\user\\Desktop\\test > > When using raw_input(), no characters are substituted and none need escaping. It's not a literal to need double-backslashing, and it's not a Unix shell, to need escaping of the space character. What you type is what you get, other than things like backspace and enter. Prove it to yourself with print, and then type it straight. You might also add an extra (untested) : if not (os.path.exists(directory) and os.path.isdir(directory)): print "Not a valid directory" DaveA From mbnoimi at gmx.com Thu May 13 05:18:16 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Thu, 13 May 2010 05:18:16 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: References: <4BEA8C51.8050305@gmx.com> Message-ID: <4BEB6F78.8090407@gmx.com> Thanks Alan, On 12/05/2010 06:56 ?, Alan Gauld wrote: > > "M. Bashir Al-Noimi" wrote >> This is my first post and I want to set a plan for learning python >> syntax within 2 weeks. > > Learning python syntax in 2 weeks is easily achievable if you go > through the official tutorial at python.org. It is designed for people > coming from another language, especially C++ or Java. If you work > through it at a sensible pace - ie slow enough to think about it - you > should be done > in 2 or 3 days. Oh my god, I finished the basics of python within one day (continues 10 hours)!!! What's amazing language :-* > From there you will know enough to start working through the Django > tutorial material and write short simple Python function and programs. > >> 1. From where I can start python by examples for C++ developers (I >> don't want documentation tutorials cuz I need simple tutorials >> compares between C++ & python syntax just at start point)? > > As others have said, trying to convert your knowledge of C++ into > Python is likely to result in you writing Python that looks like C++. > It is also likely to introduce bad habits based on C++ limitations. > You need to learn the Python way of doing things (and its limitations!) Actually I found some principles like extended C++ libraries, for example I'm using Qt classes for creating objects just like dictionary in python, where python provides them by default wow =-O . > >> 2. What's most IDE like Qt Creator (smart code completing is very >> important to me)? > > Most Python IDEs do at least basic tooltips and such but if you are > familiar with Eclipse then you can install PyDev and get all the usual > Eclipse tools, including a good debugger. The one thing that is less > useful in Eclipse is its interpreter prompt, but you can just use a > tool like IDLE or Pythonwin or IPython in a terminal for that. PyDev is amazing specially I'm familiar with eclipse (I was using it before releasing Qt Creator) but as I can see it's suitable for web applications (specially with Aptana) more than desktop application because web application don't need visual editor but in case if I want to develope dektop applications (of course in the future) what's most suitable IDE for that? Until now I tested the following: 1. PyDev (95/100): amazing and gave a good impression for first look but it's not supporting visual editor for desktop applications. 2. eric (80/100): the most active IDE for desktop applications in python but it has many negatives like autocompleting is so stupid managing the projects syncing changes between UI files and the code bad interface. 3. boa-constructor (70/100): generally it has only two negatives it uses wxPython and it's not active since 2007 although I prefer it more than eric but I really hate wx 4. SPE (50/100): most likely to eric by it has arranged interface, uses wxPython and it's not active since 2006 (dead project) I found many IDEs for desktop application but most of them very simple or/and dead (PythonCard... etc) Do u've more details? > >> 3. One of most different -I guess- between PHP & ASP.net is separated >> html code, does python separates html code or not? > > Python supports all the different styles of web development but > Django, like most modern web tools separates code from presentation. > >> 4. Do I need any pre knowledge about CGI principles before stating >> developing web application with python? > > Since it is all built on CGI it is always a good idea to understand > what is really going on. But raw CGI skill is not essential to get > started in Django. Could you guide me to quick guide for learning CGI? > >> 5. Does django provide rapid framework for web applications just like >> Qt? or you suggest better framework (I don't want to create web >> applications from scratch)? > > Django is a framework for rapid Web development but it is very > different to, for example Glade. It is not an IDE or visual editor. > >> 6. What's most suitable packaging tool (deploying desktop >> applications) for Windows OS? > > If its a web server based app then installing Python and Django on the > server and loading your packages on is a better bet that trying to > create an exe file os similar. > >> 7. Does the period of 2 week enough for learning python syntax & >> basics of web developing? > > Yes, but it will be a steep curve. > Ask lots of questions, and read lots of documentation. > Ask the questions in the place most likely to answer them correctly! > (ie python stuff here, Django stuff on a Django list) > >> 8. Does any one tested an experience just like me (learning a new >> language within short period could be crazy ;-) )? > > I've been programming for about 30 years and have learned well over > 20-30 languages. C++ was the hardest to learn and took about a month > to get comfortable, most take a week. > Becoming an expert, now that's a different thing altogether - it took > me about 4 years to reach that in C++ and about 1 year to lose it > again! I'm still learning in Python after 10+ years! (Mainly because I > don't use it for industrial stength projects) > > -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbnoimi at gmx.com Thu May 13 05:21:17 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Thu, 13 May 2010 05:21:17 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <258686.81278.qm@web86702.mail.ird.yahoo.com> References: <4BEA8C51.8050305@gmx.com> <258686.81278.qm@web86702.mail.ird.yahoo.com> Message-ID: <4BEB702D.3000408@gmx.com> Hi Walter, On 13/05/2010 12:45 ?, ALAN GAULD wrote: > Forwarded to the list. > Please use Reply All when posting. > *From:* Walter Prins > *To:* Alan Gauld > *Sent:* Wednesday, 12 May, 2010 18:36:31 > *Subject:* Re: [Tutor] First steps for C++/Qt developers > > Regarding IDE's: I find Eclipse with PyDev pretty good, and it > recently gained support for Django specifically, so it's a good fit > from that point of view for you. This of course does not include a > form designer, you can choose whatever tool you like there. As an > aside, there's also Delphi RAD'esque tool called "Boa constructor" > which aims to be a RAD IDE with forms designer for Python. Not sure > how active the project is etc, but maybe worth a look. As I said above Boa constructor has two negatives it uses wxPython and it's not active since 2007 > > As for Pyjamas, I don't know enough about it to answer if and how it > may be used together with Django, although I can't see any fundamental > reasons why that should not be possible. But, perhaps it's an idea > for you to just develop some feel for Python itself first, writing > some things from scratch, and once you've got a bit of a Python > foundation you can have a look at Pyjamas etc. > > Walter -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 13 09:18:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 13 May 2010 08:18:40 +0100 Subject: [Tutor] creating distribution lists in outlook References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> Message-ID: "Luke Paireepinart" > 1000+ contacts in one contacts folder. When you create the distribution > lists a number get cut off if you try to put too many into the list. But > there is no indication of how many went into the list. I think Luke made a good point. This is usually done by businesses at the Exchange server as a central list. Do you use Exchange? Outlook is intended as an individual mail client so doesn't expect any one person to be putting togetrher really big lists. (A stupid assumption because I've been hit with this limit too.) If you are using Exchange you should look at the admin interface of that and see how to create big lists - I've never done it personally but I know that's how I need to get a big list created - via the Exchange admin team. Just a thought. Alan G. From benmillane at googlemail.com Thu May 13 11:05:31 2010 From: benmillane at googlemail.com (Ben Millane) Date: Thu, 13 May 2010 10:05:31 +0100 Subject: [Tutor] Character creator program In-Reply-To: References: <6EDCF82A7DE74520B7233729BE618079@BenPC> Message-ID: > > Ok, thank you everyone for the helpful replies. I am currently very new to > both Python and programming (started looking into the whole business about 2 > weeks ago) and I know how awful my coding skills are at the moment. But, > thanks to lists like these, I hope I can improve my skills. I will look at > the branching tutorial for sure and add my else statement in too. Thanks > again. -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Thu May 13 16:34:44 2010 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Thu, 13 May 2010 07:34:44 -0700 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90D667B98@HCAMAIL03.ochca.com> I appreciate the effort to help! I work for the county. If we were a business, or maybe just in better economic times maybe we could afford a program that does email distribution lists. I believe that is the way the companies probably deal with this. Here's what Microsoft says about the size of outlook distribution lists: http://support.microsoft.com/kb/238569 Between 50 & 70 emails per list. That's just a limitation of outlook. At least outlook 2003. On the other hand I have heard that Outlook 2007 might have dealt with this limitation. I will probably check that out. It does seem like the python solution is going to be too labor intensive. As with many such issues I thought it would be a fun challenge to see if I could get it to work. : ) Thanks for the help. matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: Luke Paireepinart [mailto:rabidpoobear at gmail.com] Sent: Wednesday, May 12, 2010 5:00 PM To: Pirritano, Matthew Cc: tutor at python.org Subject: Re: [Tutor] creating distribution lists in outlook On Wed, May 12, 2010 at 6:32 PM, Pirritano, Matthew wrote: > Here's the thing. You can import them all at once. But then you have > 1000+ contacts in one contacts folder. When you create the distribution > lists a number get cut off if you try to put too many into the list. But > there is no indication of how many went into the list. And there are not > row numbers or anything like it in the contacts folders. So you'd have > to keep in mind how far you got and then choose the next set, and it > would be way to labor intensive if you were to count to 50 and then do > that again. It's easier to create 20 separate files and make > corresponding lists. ?But still takes a while. Pardon me for continuing not to address your specific question, but this seems like something that businesses would commonly run into and I'd be very surprised if there's not a way around this already. Is there really no way to have a group of more than 50? Have you tried creating distribution lists based on contact groups? If this is possible you could automatically add all contacts to one specific group. I really think there's a simple solution if you look into it, but I don't have outlook, I use Thunderbird. Are you able to use a plugin to perform the work or is that against your company's policies? I just don't see automating with COM being the cleanest / most robust approach, but if you really want to try it hopefully we can help if you run into any python-specific issues! -Luke From MPirritano at ochca.com Thu May 13 16:44:04 2010 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Thu, 13 May 2010 07:44:04 -0700 Subject: [Tutor] creating distribution lists in outlook In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com><97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com><97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90D667BA4@HCAMAIL03.ochca.com> Thanks for all of the feedback. The work continues. Unfortunately I think the admin interface is only for internal lists, not emails outside the server. Thanks matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Alan Gauld Sent: Thursday, May 13, 2010 12:19 AM To: tutor at python.org Subject: Re: [Tutor] creating distribution lists in outlook "Luke Paireepinart" > 1000+ contacts in one contacts folder. When you create the distribution > lists a number get cut off if you try to put too many into the list. But > there is no indication of how many went into the list. I think Luke made a good point. This is usually done by businesses at the Exchange server as a central list. Do you use Exchange? Outlook is intended as an individual mail client so doesn't expect any one person to be putting togetrher really big lists. (A stupid assumption because I've been hit with this limit too.) If you are using Exchange you should look at the admin interface of that and see how to create big lists - I've never done it personally but I know that's how I need to get a big list created - via the Exchange admin team. Just a thought. Alan G. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From inthefridge at gmail.com Thu May 13 16:48:02 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Thu, 13 May 2010 08:48:02 -0600 Subject: [Tutor] raw_input a directory path In-Reply-To: <4BEB5D51.8000301@ieee.org> References: <4BEB5D51.8000301@ieee.org> Message-ID: I figured out the issue...for some reason it works with the trailing slash, but not without it. Once I do that...everything is all good... On Wed, May 12, 2010 at 8:00 PM, Dave Angel wrote: > > > Spencer Parker wrote: > >> Here is the code: >> http://dpaste.com/hold/193862/ >> >> It still isn't working for me. I don't >> see >> it hitting the first for loop or even the second one. It runs without an >> error at all. >> >> I am inputing the directory as: \\Documents\ and\ >> Settings\\user\\Desktop\\test >> >> > When using raw_input(), no characters are substituted and none need > escaping. It's not a literal to need double-backslashing, and it's not a > Unix shell, to need escaping of the space character. What you type is what > you get, other than things like backspace and enter. > > Prove it to yourself with print, and then type it straight. You might also > add an extra (untested) : > > if not (os.path.exists(directory) and os.path.isdir(directory)): > print "Not a valid directory" > > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From inthefridge at gmail.com Fri May 14 00:17:32 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Thu, 13 May 2010 16:17:32 -0600 Subject: [Tutor] raw_input a directory path In-Reply-To: References: <4BEB5D51.8000301@ieee.org> Message-ID: In these files I also have some special characters I need to remove, but I cannot figure out how to specify this. I need to remove a vertical tab and a formfeed character. How would I specify to remove these characters? I have tried \v and \f On Thu, May 13, 2010 at 8:48 AM, Spencer Parker wrote: > I figured out the issue...for some reason it works with the trailing slash, > but not without it. Once I do that...everything is all good... > > > On Wed, May 12, 2010 at 8:00 PM, Dave Angel wrote: > >> >> >> Spencer Parker wrote: >> >>> Here is the code: >>> http://dpaste.com/hold/193862/ >>> >>> It still isn't working for me. I don't >>> see >>> it hitting the first for loop or even the second one. It runs without an >>> error at all. >>> >>> I am inputing the directory as: \\Documents\ and\ >>> Settings\\user\\Desktop\\test >>> >>> >> When using raw_input(), no characters are substituted and none need >> escaping. It's not a literal to need double-backslashing, and it's not a >> Unix shell, to need escaping of the space character. What you type is what >> you get, other than things like backspace and enter. >> >> Prove it to yourself with print, and then type it straight. You might >> also add an extra (untested) : >> >> if not (os.path.exists(directory) and os.path.isdir(directory)): >> print "Not a valid directory" >> >> >> DaveA >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Fri May 14 01:06:49 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 14 May 2010 01:06:49 +0200 Subject: [Tutor] raw_input a directory path In-Reply-To: References: <4BEB5D51.8000301@ieee.org> Message-ID: On Fri, May 14, 2010 at 12:17 AM, Spencer Parker wrote: > In these files I also have some special characters I need to remove, but I > cannot figure out how to specify this. > I need to remove a vertical tab and a formfeed character. ?How would I > specify to remove these characters? ?I have tried \v and \f > As was noted, raw_input doesn't want escaping done on its input, it just takes it as is. Escaping is something done only for representation purposes. If you want your script to replace a vertical tab, you'll need to enter a vertical tab character into your console. To be fair, it's not immediately obvious how to do that. I must confess I have no idea how you would do that on windows[1] Alternatively, you could change your script so that you can, for example, enter the corresponding ASCII code as input (the chr function can convert ascii codes to their corresponding character). Hugo [1] had you been using linux I would have suggested: $ echo -e '\v' | xclip -i -selection c Then just paste as input. It's not exactly convenient, but does solve the problem. From hugo.yoshi at gmail.com Fri May 14 02:52:50 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 14 May 2010 02:52:50 +0200 Subject: [Tutor] raw_input a directory path In-Reply-To: <4BEC9B5E.2080906@dejaviewphoto.com> References: <4BEB5D51.8000301@ieee.org> <4BEC9B5E.2080906@dejaviewphoto.com> Message-ID: On Fri, May 14, 2010 at 2:37 AM, Dave Angel wrote: > The other approach is to have the user type in the text according to some > escaping language (like the one used for literals), and explicitly decode > that after the raw_input(). ?I thought there was a way in the stdlib, but I > can't seem to find it right now. > the str.decode method has a 'string_escape' encoding: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print raw_input('prompt: ').decode('string_escape') prompt: hello\nhello hello hello >>> Hugo From knacktus at googlemail.com Fri May 14 06:49:59 2010 From: knacktus at googlemail.com (Jan Jansen) Date: Fri, 14 May 2010 06:49:59 +0200 Subject: [Tutor] Design Question: File Object used everywhere Message-ID: <4BECD677.6070000@googlemail.com> Hi there, I'm working on a code to read and write large amounts of binary data according to a given specification. In the specification there are a lot of "segments" defined. The segments in turn have defintions of datatypes and what they represent, how many of some of the data values are present in the file and sometimes the offset from the beginning of the file. Now I wonder, what would be a good way to model the code. Currently I have one class, that is the "FileReader". This class holds the file object, information about the endianess and also a method to read data (using the struct module). Then, I have more classes representing the segements. In those classes I define data-formats, call the read-method of the FileReader object and hold the data. Currently I'm passing the FileReader object as arguement. Here some examples, first the "FileReader" class: class JTFile(): def __init__(self, file_obj): self.file_stream = file_obj self.version_string = "" self.endian_format_prefix = "" def read_data(self, fmt, pos = None): format_size = struct.calcsize(fmt) if pos is not None: self.file_stream.seek(pos) return struct.unpack_from(self.endian_format_prefix + fmt, self.file_stream.read(format_size)) and here an example for a segment class that uses a FileReader instance (file_stream): class LSGSegement(): def __init__(self, file_stream): self.file_stream = file_stream self.lsg_root_element = None self._read_lsg_root() def _read_lsg_root(self): fmt = "80Bi" raw_data = self.file_stream.read_data(fmt) self.lsg_root_element = LSGRootElement(raw_data[:79], raw_data[79]) So, now I wonder, what would be a good pythonic way to model the FileReader class. Maybe use a global functions to avoid passing the FileReader object around? Or something like "Singleton" I've heard about but never used it? Or keept it like that? Cheers, Jan From rabidpoobear at gmail.com Fri May 14 07:35:41 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 14 May 2010 00:35:41 -0500 Subject: [Tutor] Design Question: File Object used everywhere In-Reply-To: <4BECD677.6070000@googlemail.com> References: <4BECD677.6070000@googlemail.com> Message-ID: On Thu, May 13, 2010 at 11:49 PM, Jan Jansen wrote: > Hi there, > > I'm working on a code to read and write large amounts of binary data > according to a given specification. In the specification there are a lot of > "segments" defined. The segments in turn have defintions of datatypes and > what they represent, how many of some of the data values are present in the > file and sometimes the offset from the beginning of the file. > > Now I wonder, what would be a good way to model the code. Personally I would just create a class that inherits from object (btw I don't think you should have empty parenthesis after your classname, I believe you should explicitly say object so it's clear what you're inheriting from) and just implement the generic file object methods (in this case read() and close() is probably all you'd need.) that way you can pass it to other classes if you need to. Basically the pythonic way is to make it as generic as possible and to exploit duck typing so that other classes don't need to know how you're actually implementing your reading/writing behind the scenes. I'm not sure if that's acceptable (or even desirable) in this particular situation though. From davea at ieee.org Fri May 14 12:44:27 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 14 May 2010 06:44:27 -0400 Subject: [Tutor] Design Question: File Object used everywhere In-Reply-To: <4BECD677.6070000@googlemail.com> References: <4BECD677.6070000@googlemail.com> Message-ID: <4BED298B.80006@ieee.org> Jan Jansen wrote: > Hi there, > > I'm working on a code to read and write large amounts of binary data > according to a given specification. In the specification there are a > lot of "segments" defined. The segments in turn have defintions of > datatypes and what they represent, how many of some of the data values > are present in the file and sometimes the offset from the beginning of > the file. > > Now I wonder, what would be a good way to model the code. > > Currently I have one class, that is the "FileReader". This class holds > the file object, information about the endianess and also a method to > read data (using the struct module). Then, I have more classes > representing the segements. In those classes I define data-formats, > call the read-method of the FileReader object and hold the data. > Currently I'm passing the FileReader object as arguement. > > Here some examples, first the "FileReader" class: > > class JTFile(): > > def __init__(self, file_obj): > self.file_stream = file_obj > self.version_string = "" > self.endian_format_prefix = "" > > def read_data(self, fmt, pos = None): > format_size = struct.calcsize(fmt) > if pos is not None: > self.file_stream.seek(pos) > return struct.unpack_from(self.endian_format_prefix + fmt, > self.file_stream.read(format_size)) > > and here an example for a segment class that uses a FileReader > instance (file_stream): > > class LSGSegement(): > > def __init__(self, file_stream): > self.file_stream = file_stream > self.lsg_root_element = None > self._read_lsg_root() > > def _read_lsg_root(self): > fmt = "80Bi" > raw_data = self.file_stream.read_data(fmt) > self.lsg_root_element = LSGRootElement(raw_data[:79], > raw_data[79]) > > So, now I wonder, what would be a good pythonic way to model the > FileReader class. Maybe use a global functions to avoid passing the > FileReader object around? Or something like "Singleton" I've heard > about but never used it? Or keept it like that? > > Cheers, > > Jan > > I agree with Luke's advice, but would add some comments. As soon as you have a global (or a singleton) representing a file, you're making the explicit assumption that you'll never have two such files open. So what happens if you need to merge two such files? Start over? You need to continue to pass something representing the file (JTFile object) into each constructor. The real question is one of state, which isn't clear from your example. The file_stream attribute of an object of class JTFile has a file position, which you are implitly using. But you said some segments are at fixed positions in the file, and presumably some are serially related to other segments. Or perhaps some segments are really a section of the file containing smaller segments of different type(s). Similarly, each object, after being created, probably has relationship to other objects. Without knowing that, you can't design those object classes. Finally, you need to decide early on what to do about data validation. If the file happens to be busted, how are you going to notify the user. If you read it in an ad-hoc, random order, you'll have a very hard time informing the user anything useful about what's wrong with it, never mind recovering from it. It's really a problem in serialization, where you read a file by deserializing. Consider whether the file is going to be always small enough to support simply interpreting the entire stream into a tree of objects, and then dealing with them. Conceivably you can do that lazily, only deserializing objects as they are referenced. But the possibility of doing that depends highly on whether there is what amounts to a "directory" in the file, or whether each object's position is determined by the length of the previous one. In addition to deserializing in one pass, or lazily deserializing, consider deserializing with callbacks. In this approach you do not necessarily keep the intermediate objects, you just call a specified user routine, who should keep the objects if she cares about them, or process them or ignore them as needed. I've had to choose each of these approaches for different projects, and the choice depended in large part on the definition of the data file, and whether it could be randomly accessed. DaveA From denis.spir at gmail.com Fri May 14 13:25:25 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Fri, 14 May 2010 13:25:25 +0200 Subject: [Tutor] Design Question: File Object used everywhere In-Reply-To: <4BECD677.6070000@googlemail.com> References: <4BECD677.6070000@googlemail.com> Message-ID: <20100514132525.1d5df44d@o> On Fri, 14 May 2010 06:49:59 +0200 Jan Jansen wrote: > Hi there, > > I'm working on a code to read and write large amounts of binary data > according to a given specification. In the specification there are a lot > of "segments" defined. The segments in turn have defintions of datatypes > and what they represent, how many of some of the data values are present > in the file and sometimes the offset from the beginning of the file. > > Now I wonder, what would be a good way to model the code. > > Currently I have one class, that is the "FileReader". This class holds > the file object, information about the endianess and also a method to > read data (using the struct module). Then, I have more classes > representing the segements. In those classes I define data-formats, call > the read-method of the FileReader object and hold the data. Currently > I'm passing the FileReader object as arguement. > > Here some examples, first the "FileReader" class: > > class JTFile(): > > def __init__(self, file_obj): > self.file_stream = file_obj > self.version_string = "" > self.endian_format_prefix = "" > > def read_data(self, fmt, pos = None): > format_size = struct.calcsize(fmt) > if pos is not None: > self.file_stream.seek(pos) > return struct.unpack_from(self.endian_format_prefix + fmt, > self.file_stream.read(format_size)) Since JTFile (as name says) is mainly a file, you could subtype it from file, thus avoiding its file_stream attribute and replacing self.file_stream.read/seek with direct self.read/seek. Also, this better mirrors the model, I guess. > and here an example for a segment class that uses a FileReader instance > (file_stream): > > class LSGSegement(): > > def __init__(self, file_stream): > self.file_stream = file_stream > self.lsg_root_element = None > self._read_lsg_root() > > def _read_lsg_root(self): > fmt = "80Bi" > raw_data = self.file_stream.read_data(fmt) > self.lsg_root_element = LSGRootElement(raw_data[:79], raw_data[79]) > > So, now I wonder, what would be a good pythonic way to model the > FileReader class. Maybe use a global functions to avoid passing the > FileReader object around? Or something like "Singleton" I've heard about > but never used it? Or keept it like that? A singleton object is just a unique instance of a type. The singleton pattern simply ensures this unicity by refusing to create more instances. The python way, I guess, is rather a gentleman agreement, which in this case means creating a single instance and no more, since you are the only user of your file-reading service. If this were to be distributed as module, then document this "uncity" point, or implement the singleton pattern. (But I don't understand why there should be only one file-reader. Rather, there should be only one per (disk) file ;-) If you make the type a subtype of file, then this is certainly automatically ensured since no 2 python file objects can point to the same disk file -- I guess -- to be checked.) > Cheers, > > Jan Denis ________________________________ vit esse estrany ? spir.wikidot.com From steve at lonetwin.net Fri May 14 11:22:39 2010 From: steve at lonetwin.net (steve) Date: Fri, 14 May 2010 14:52:39 +0530 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BEB6F78.8090407@gmx.com> References: <4BEA8C51.8050305@gmx.com> <4BEB6F78.8090407@gmx.com> Message-ID: <4BED165F.6080004@lonetwin.net> Hello Bashir, On 05/13/2010 08:48 AM, M. Bashir Al-Noimi wrote: > Thanks Alan, > [...snip...] > Oh my god, I finished the basics of python within one day (continues 10 > hours)!!! > What's amazing language :-* > Yes, everyone here feels the same way :). Anyways, now that you have a hang of python and you already know Qt, I would recommend playing around with PyQT just to get a feel of the language using API that you already know. http://www.riverbankcomputing.co.uk/ http://www.commandprompt.com/community/pyqt/ If you want to directly head on to learning Web Programming though, I suggest you start with: http://www.djangobook.com/en/2.0/ Although, I personally am a bit biased towards: http://www.cherrypy.org/ ...which is a lean, no-frills web application framework. Like other have mentioned, you don't need to know CGI programming to write web applications these days, but knowing how CGI works helps immensely. For a high level overview of web programming using python, see: http://docs.python.org/howto/webservers.html hth, cheers, - steve -- random spiel: http://lonetwin.net/ what i'm stumbling into: http://lonetwin.stumbleupon.com/ From mbnoimi at gmx.com Fri May 14 16:11:25 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Fri, 14 May 2010 16:11:25 +0200 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: <4BED165F.6080004@lonetwin.net> References: <4BEA8C51.8050305@gmx.com> <4BEB6F78.8090407@gmx.com> <4BED165F.6080004@lonetwin.net> Message-ID: <4BED5A0D.9030805@gmx.com> Thanks Steve, On 14/05/2010 11:22 ?, steve wrote: > Hello Bashir, > > On 05/13/2010 08:48 AM, M. Bashir Al-Noimi wrote: >> Thanks Alan, >> [...snip...] >> Oh my god, I finished the basics of python within one day (continues 10 >> hours)!!! >> What's amazing language :-* >> > > Yes, everyone here feels the same way :). Anyways, now that you have a > hang of python and you already know Qt, I would recommend playing > around with PyQT just to get a feel of the language using API that you > already know. > > http://www.riverbankcomputing.co.uk/ > http://www.commandprompt.com/community/pyqt/ Actually I wrote first PyQt application yesterday by using eric IDE, although it has stupid autocompleting and bad GUI but I could create Hello World app within 5 minutes, that's it. My adventure in python going smoothly and community of python has truly open minds. > > If you want to directly head on to learning Web Programming though, I > suggest you start with: > http://www.djangobook.com/en/2.0/ > > Although, I personally am a bit biased towards: > http://www.cherrypy.org/ > > ...which is a lean, no-frills web application framework. In simple words could you give me what's distinguished differences between cherrypy and django (I didn't stat with django cuz I'm still python principles)? > > Like other have mentioned, you don't need to know CGI programming to > write web applications these days, but knowing how CGI works helps > immensely. For a high level overview of web programming using python, > see: > > http://docs.python.org/howto/webservers.html > > hth, > cheers, > - steve > -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From garywk at cableone.net Fri May 14 19:55:10 2010 From: garywk at cableone.net (Gary Koskenmaki) Date: Fri, 14 May 2010 10:55:10 -0700 Subject: [Tutor] application with tabs Message-ID: <1273859710.4367.44.camel@lappy.gawako.local> Hi, I'm new to any kind of application development, but have done some python scripting. What I'm doing is creating an application for a non-profit social services organization that will allow them to track the services they give their clients. My plan so far was to use tabs for the different functions of the application such as entering data into the database, viewing data, and running reports. I'm running into problems combining tabs with multiple data fields and buttons. I can create multiple fields(text boxes) with wxFrame and wxPanel, but when using wxNotebook to create tabs only the last text box in each tab shows up. Say I have 3 text boxes, only the third one shows up. If I comment it out the second one shows up. I have a very similar problem with buttons. I have read and experimented with the examples on wxpywiki with no joy. Using the following example from wxpywiki the text boxes created in the TabPanel class are shown on every tab. When moving the text box creation to the NotebookDemo class, so that I can have different types/numbers of text boxes on each tab, I run into the problem of having more than one text field displayed in each tab. The first code example is the one I'm using. http://wiki.wxpython.org/Notebooks Am I going about creating this application interface in the most difficult way? Is there a better/easier way to create multiple application views in the same window? Can anyone recommend good books, tutorials, etc...? I'm doing this on an i386 install of Debian and have python-wxgtk2.8 installed as well as libwxgtk2.8. The version of python itself is 2.5.5. All related packages are Debian packages. From einstein_87 at hotmail.com Fri May 14 10:08:30 2010 From: einstein_87 at hotmail.com (she haohao) Date: Fri, 14 May 2010 16:08:30 +0800 Subject: [Tutor] Help Message-ID: Hi, I am a beginner in python and I have a problem here and I hope someone can help me. Your help is greatly appreciated. Question. Say I have a .fa file and I want to print a subsequence from the file without the \n how can i do it. Example: Inside the test.fa file I have >chromosome 1 ACTGTGTTC ACGTCGACC AVGTTTTTT ACGTTaGTC so if I say i wan the subsequence starting from the 7th character to the 11th(i.e when i let p=(7+11)/2=9 and v=2) character.(excluding the first line), mean I want TTCAC. So how should I write the code so that \n does not appear if i want p=10 and v=3. Thanks for the help. Have a wonderful day ahead! Angeline _________________________________________________________________ Hotmail: Powerful Free email with security by Microsoft. https://signup.live.com/signup.aspx?id=60969 -------------- next part -------------- An HTML attachment was scrubbed... URL: From ideamonk at gmail.com Fri May 14 21:25:51 2010 From: ideamonk at gmail.com (Abhishek Mishra) Date: Sat, 15 May 2010 00:55:51 +0530 Subject: [Tutor] Help In-Reply-To: References: Message-ID: Not sure if I understood the problem exactly, but you could linearize the text by using something like this -- >>> foo = '''ACTGTGTTC ... ACGTCGACC ... AVGTTTTTT ... ACGTTaGTC''' >>> foo 'ACTGTGTTC\nACGTCGACC\nAVGTTTTTT\nACGTTaGTC' >>> linear1 = ''.join(foo.split('\n')) >>> linear1 'ACTGTGTTCACGTCGACCAVGTTTTTTACGTTaGTC' >>> >>> linear2 = foo.replace('\n','') >>> linear2 'ACTGTGTTCACGTCGACCAVGTTTTTTACGTTaGTC' >>> >>> linear1 = ''.join(foo.split('\n')) >>> linear2 = foo.replace('\n','') ^^ these are the two ways in which you can linearize the input text by removing all '\n' after that you can grab 7th to 11th character like this - >>> linear2[6:11] 'TTCAC' ^^ notice that the index is zero based, so 7th character actually 6 as its index. 2010/5/14 she haohao > > > Hi, > > > I am a beginner in python and I have a problem here and I hope someone can > help me. > > Your help is greatly appreciated. > > Question. > > Say I have a .fa file and I want to print a subsequence from the file > without the \n how can i do it. > > Example: Inside the test.fa file I have > > >chromosome 1 > ACTGTGTTC > ACGTCGACC > AVGTTTTTT > ACGTTaGTC > > so if I say i wan the subsequence starting from the 7th character to the > 11th(i.e when i let p=(7+11)/2=9 and v=2) character.(excluding the first > line), mean I want TTCAC. > > So how should I write the code so that \n does not appear if i want p=10 > and v=3. > > Thanks for the help. > > Have a wonderful day ahead! > Angeline > > > ------------------------------ > Hotmail: Powerful Free email with security by Microsoft. Get it now. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Fri May 14 22:00:40 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 14 May 2010 22:00:40 +0200 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On Fri, May 14, 2010 at 9:25 PM, Abhishek Mishra wrote: > >>>> linear1 = ''.join(foo.split('\n')) >>>> linear2 = foo.replace('\n','') > ^^ these are the two ways in which you can linearize the input text by > removing all ?'\n' +1 for the replace. More obvious, cleaner, more efficient. Hugo From rabidpoobear at gmail.com Fri May 14 22:18:13 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 14 May 2010 15:18:13 -0500 Subject: [Tutor] application with tabs In-Reply-To: <1273859710.4367.44.camel@lappy.gawako.local> References: <1273859710.4367.44.camel@lappy.gawako.local> Message-ID: On Fri, May 14, 2010 at 12:55 PM, Gary Koskenmaki wrote: > Hi, > > I'm new to any kind of application development, but have done some > python scripting. > > What I'm doing is creating an application for a non-profit social > services organization that will allow them to track the services they > give their clients. > > My plan so far was to use tabs for the different functions of the > application such as entering data into the database, viewing data, and > running reports. ?I'm running into problems combining tabs with multiple > data fields and buttons. ?I can create multiple fields(text boxes) with > wxFrame and wxPanel, but when using wxNotebook to create tabs only the > last text box in each tab shows up. ?Say I have 3 text boxes, only the > third one shows up. ?If I comment it out the second one shows up. ?I > have a very similar problem with buttons. > I have never used this tabbing before, but I'm guessing that you have to catch the tab switching event and manually set the active panel that the tab widget is displaying. Something like (pseudocode) panel1 = MyPanel() panel2 = MyPanel2() tabber = WxNotebook() def myfunc(event): if event.name == 'tab1': tabber.set_active(panel1) else: tabber.set_active(panel2) tabber.register_callback('tabswitch', myfunc) That's purely a guess though. > > Am I going about creating this application interface in the most > difficult way? ?Is there a better/easier way to create multiple > application views in the same window? ?Can anyone recommend good books, > tutorials, etc...? > Personally I like Qt more than WxWindows. In fact I like TK more as well, but tkinter looks kinda crappy and non-native on almost every platform. There are lots of resources for Qt, and for pyQt specifically. This is probably not the right place to ask this question though, we're more for general Python questions. May be better to ask on a wxpython list. HTH, -Luke From hugo.yoshi at gmail.com Fri May 14 23:51:25 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 14 May 2010 23:51:25 +0200 Subject: [Tutor] application with tabs In-Reply-To: <1273859710.4367.44.camel@lappy.gawako.local> References: <1273859710.4367.44.camel@lappy.gawako.local> Message-ID: On Fri, May 14, 2010 at 7:55 PM, Gary Koskenmaki wrote: > Hi, > > > My plan so far was to use tabs for the different functions of the > application such as entering data into the database, viewing data, and > running reports. ?I'm running into problems combining tabs with multiple > data fields and buttons. ?I can create multiple fields(text boxes) with > wxFrame and wxPanel, but when using wxNotebook to create tabs only the > last text box in each tab shows up. ?Say I have 3 text boxes, only the > third one shows up. ?If I comment it out the second one shows up. ?I > have a very similar problem with buttons. > I don't know how exactly you're doing it, but in general you'd create a wxPanel for each tab, place all your controls on it, and then use the notebook.addPage call to add the panel. > I have read and experimented with the examples on wxpywiki with no joy. > Using the following example from wxpywiki the text boxes created in the > TabPanel class are shown on every tab. ?When moving the text box > creation to the NotebookDemo class, so that I can have different > types/numbers of text boxes on each tab, I run into the problem of > having more than one text field displayed in each tab. > Right, that example pretty uses the approach I describe. The example does have three different tabs, they just all look the same because they are created from the same class. Please show us what exactly the code you're using that leads to the problem you describe. I suspect this is where things go wrong. Hugo From alan.gauld at btinternet.com Sat May 15 01:18:36 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 May 2010 00:18:36 +0100 Subject: [Tutor] creating distribution lists in outlook References: <97D6F0A82A6E894DAF44B9F575305CC90D66780E@HCAMAIL03.ochca.com><97D6F0A82A6E894DAF44B9F575305CC90D667AEB@HCAMAIL03.ochca.com><97D6F0A82A6E894DAF44B9F575305CC90D667AFE@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90D667B98@HCAMAIL03.ochca.com> Message-ID: "Pirritano, Matthew" wrote > It does seem like the python solution is going to be too labor intensive. > As with many such issues I thought it would be a fun challenge to see > if I could get it to work. : ) You probably could, but there is another way. Python does not have Outlook's limitations so, if you only need the distro lists for announcements - or even as a mail relay - you could use Python's native email libraries. Build a simple config/admin UI of your own and then use Python to send out messages - which could be text files. Or use Python (or indeed mailman or any other email list handler) as a relay so you send a mail to the relay and have it send the mail out to as many people as you want. That way you avoid Outlook's limits and all you need is an SMTP relay host. (You could even install sendmail or such to do that if you wanted) And all for free...apart from the cost of administering it of course... Alan G. From alan.gauld at btinternet.com Sat May 15 01:41:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 May 2010 00:41:43 +0100 Subject: [Tutor] application with tabs References: <1273859710.4367.44.camel@lappy.gawako.local> Message-ID: "Luke Paireepinart" wrote > well, but tkinter looks kinda crappy and non-native on almost every > platform. Check out the new themed widgets in Tkinter for Python 2.7 and 3.1 They use native platform widgets so don't just look like the native interface they are the native interface... Hello world example here: http://www.testingreflections.com/node/view/8382 Module doc here http://docs.python.org/dev/library/ttk.html And the Tk tutorial with screenshots here: http://www.tkdocs.com/tutorial/onepage.html And a new TKGui builder that includes support for Python - I haven't tried this yet... http://puretkgui.sourceforge.net/ I'm hoping to add a page to my tutorial on the use of both Tix and ttk in the new Python v3 version, if I ever get time to finish it! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat May 15 01:51:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 May 2010 00:51:44 +0100 Subject: [Tutor] First steps for C++/Qt developers References: <4BEA8C51.8050305@gmx.com> <4BEB6F78.8090407@gmx.com> <4BED165F.6080004@lonetwin.net> <4BED5A0D.9030805@gmx.com> Message-ID: "M. Bashir Al-Noimi" wrote > > Although, I personally am a bit biased towards: > > http://www.cherrypy.org/ > In simple words could you give me what's distinguished differences > between cherrypy and django (I didn't stat with django cuz I'm still > python principles)? They work a little differently and Django gives you lots of extra features that CherryPy doesn't - you need extra libraries to get the exta features. (Things like a templating engine and onject persistent database access. I'm also not sure how much of an admin GUI CherryPy delivers out of the box) In fact you can use TurboGears which is a direct competitor to Django and uses CherryPy as part of its framework. (Or it did - I know the latest version of TG has changed a lot!) One of the good and bad things about Python is that it supports many, many, different web tookits from the simplest CGI through to Zope and Plone which are enterprise class web frameworks(albeit with very different emphases). For most folks the middle ground includes things like Pylons, CherryPy and TG and Django. You can do most of what most people need with these and they are simpler in practice than either raw CGI or the heavyweight tools. So pick one and stick to it. Like languages or GUI toolkits, once you learn one moving to another is relatively painfree. Provided it does what you need and has a good support network don't stress over it! HTH, Alan G. From rabidpoobear at gmail.com Sat May 15 04:08:26 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 14 May 2010 21:08:26 -0500 Subject: [Tutor] application with tabs In-Reply-To: References: <1273859710.4367.44.camel@lappy.gawako.local> Message-ID: Thanks for that info, Alan! It's pretty awesome to have support for a gui that looks native and is also included with Python by default. I'll check it out soon. On 5/14/10, Alan Gauld wrote: > > "Luke Paireepinart" wrote > >> well, but tkinter looks kinda crappy and non-native on almost every >> platform. > > Check out the new themed widgets in Tkinter for Python 2.7 and 3.1 > They use native platform widgets so don't just look like the native > interface they are the native interface... > > Hello world example here: > > http://www.testingreflections.com/node/view/8382 > > Module doc here > > http://docs.python.org/dev/library/ttk.html > > And the Tk tutorial with screenshots here: > > http://www.tkdocs.com/tutorial/onepage.html > > And a new TKGui builder that includes support for Python - I haven't tried > this > yet... > > http://puretkgui.sourceforge.net/ > > I'm hoping to add a page to my tutorial on the use of both Tix and ttk in > the new Python v3 version, if I ever get time to finish it! > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Sent from my mobile device From mbnoimi at gmx.com Sat May 15 05:37:04 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Sat, 15 May 2010 05:37:04 +0200 Subject: [Tutor] [SOLVED] First steps for C++/Qt developers In-Reply-To: References: <4BEA8C51.8050305@gmx.com> <4BEB6F78.8090407@gmx.com> <4BED165F.6080004@lonetwin.net> <4BED5A0D.9030805@gmx.com> Message-ID: <4BEE16E0.8050501@gmx.com> Thanks Alan, On 15/05/2010 01:51 ?, Alan Gauld wrote: > "M. Bashir Al-Noimi" wrote > >> > Although, I personally am a bit biased towards: >> > http://www.cherrypy.org/ >> In simple words could you give me what's distinguished differences >> between cherrypy and django (I didn't stat with django cuz I'm still >> python principles)? > > They work a little differently and Django gives you lots of extra > features that CherryPy doesn't - you need extra libraries to get the > exta features. (Things like a templating engine and onject persistent > database access. I'm also not sure how much of an admin GUI CherryPy > delivers out of the box) In fact you can use TurboGears which is a > direct competitor to Django and uses CherryPy as part of its > framework. (Or it did - I know the latest version of TG has changed a > lot!) > > One of the good and bad things about Python is that it supports many, > many, different web tookits from the simplest CGI through to Zope and > Plone which are enterprise class web frameworks(albeit with very > different emphases). For most folks the middle ground includes things > like Pylons, CherryPy and TG and Django. You can do most of what most > people need with these and they are simpler in practice than either > raw CGI or the heavyweight tools. So pick one and stick to it. Like > languages or GUI toolkits, once you learn one moving to another is > relatively painfree. Provided it does what you need and has a good > support network don't stress over it! After reading many posts and articles I picked up Django because it fits my needs till now. Thanks once again for all. -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat May 15 06:13:58 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 15 May 2010 14:13:58 +1000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: <201005151413.59136.steve@pearwood.info> On Fri, 14 May 2010 06:08:30 pm she haohao wrote: > Say I have a .fa file and I want to print a subsequence from the file > without the \n how can i do it. > > Example: Inside the test.fa file I have > > >chromosome 1 > > ACTGTGTTC > ACGTCGACC > AVGTTTTTT > ACGTTaGTC > > so if I say i wan the subsequence starting from the 7th character to > the 11th(i.e when i let p=(7+11)/2=9 and v=2) character.(excluding > the first line), mean I want TTCAC. > > So how should I write the code so that \n does not appear if i want > p=10 and v=3. text = open('test.fa', 'r').read() # includes newlines text = text.replace('\n', '') # remove newlines To get the characters 7 to 11 inclusive "TTCAC", you have to remember that Python starts counting at 0, not 1, and the second position is excluded. So you have to write: text[6:11] to get TTCAC. -- Steven D'Aprano From steve at lonetwin.net Sat May 15 09:03:24 2010 From: steve at lonetwin.net (steve) Date: Sat, 15 May 2010 12:33:24 +0530 Subject: [Tutor] First steps for C++/Qt developers In-Reply-To: References: <4BEA8C51.8050305@gmx.com> <4BEB6F78.8090407@gmx.com> <4BED165F.6080004@lonetwin.net> <4BED5A0D.9030805@gmx.com> Message-ID: <4BEE473C.9070301@lonetwin.net> Hi, Sorry, I couldn't reply any earlier. Anyways, Alan explained it quite well. I just wanted to explain why I prefer cherrypy ... On 05/15/2010 05:21 AM, Alan Gauld wrote: > "M. Bashir Al-Noimi" wrote > >> > Although, I personally am a bit biased towards: >> > http://www.cherrypy.org/ >> In simple words could you give me what's distinguished differences >> between cherrypy and django (I didn't stat with django cuz I'm still >> python principles)? > > They work a little differently and Django gives you lots of extra features > that CherryPy doesn't - you need extra libraries to get the exta features. > (Things like a templating engine and onject persistent database access. > I'm also not sure how much of an admin GUI CherryPy delivers out of > the box). That's right, CherryPy is just the web application server, whereas Django is the entire framework. Cherrypy's lack of a builtin templating system, database connector or admin interface is a design decision. This is so that you can plug in whatever components you prefer. For example, you may use cherrypy+SQLAlchemy+Kid or cherrypy+SQLObject+Genshi ...etc. > [...snip...] > One of the good and bad things about Python is that it supports > many, many, different web tookits from the simplest CGI through to Zope > and Plone which are enterprise class web frameworks(albeit with very > different emphases). For most folks the middle ground includes things > like Pylons, CherryPy and TG and Django. That's quite right. > You can do most of what > most people need with these and they are simpler in practice than either > raw CGI or the heavyweight tools. So pick one and stick to it. I disagree here. I personally recommend using something 'lean' like cherrypy when building a web interface /around/ your application (for instance, a control panel for a server application, or a XML-RPC+HTTP API for your existing application) where as using a full blown framework like Django if your web-application itself is your main application (like a (web2.0) web site). that said ... > Like languages > or GUI toolkits, once you learn one moving to another is relatively painfree. > Provided it does what you need and has a good support network don't > stress over it! > +1 hth, cheers, - steve -- random spiel: http://lonetwin.net/ what i'm stumbling into: http://lonetwin.stumbleupon.com/ From alan.gauld at btinternet.com Sat May 15 17:28:51 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 May 2010 16:28:51 +0100 Subject: [Tutor] application with tabs References: <1273859710.4367.44.camel@lappy.gawako.local> Message-ID: "Luke Paireepinart" wrote in message >> And a new TKGui builder that includes support for Python - I haven't tried >> this >> yet... >> >> http://puretkgui.sourceforge.net/ >> I've now had a play and it looks promising but does not currently support Tkinter - option is greyed out. It is still at version 0.10 so a lot of work to be really useful but it definitely shows promise. It supports ttk and Tix as well as vanilla Tk. One to watch but maybe not quite ready for prime time just yet. Alan G. From steve at alchemy.com Sat May 15 20:24:29 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sat, 15 May 2010 11:24:29 -0700 Subject: [Tutor] ttk printing (was Re: application with tabs) In-Reply-To: References: Message-ID: <20100515182429.GA28712@dragon.alchemy.com> I've been seeing Alan and others advocate the new themed widgets for Tkinter and it looks pretty cool, but in what I've seen in an admittedly cursory glance I don't see that there's yet much in the way of printing support for apps (other than getting raw PostScript for what's on a canvas) which is one of the things still pushing me toward wxPython instead. I'd love to make use of all the time I invested over the years learning Tcl/Tk and Tkinter, but this doesn't look like there's a good solution (such as support for writing to the Windows GDI on Windows, although the PostScript output is likely adequate on Unix and Macs). Is there something people are using in that space for Tk? -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From alan.gauld at btinternet.com Sun May 16 00:13:39 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 15 May 2010 22:13:39 +0000 (GMT) Subject: [Tutor] ttk printing (was Re: application with tabs) In-Reply-To: <20100515182429.GA28712@dragon.alchemy.com> References: <20100515182429.GA28712@dragon.alchemy.com> Message-ID: <722910.90896.qm@web86701.mail.ird.yahoo.com> Printing is always a pain from GUIs and wxWidgets support for it is a striong argument in its favour. For most of my GUI needs printing is rarely an issue and if I do need to print I gmerate an HTML document and print that via the browser. But thats not a great solution where precision is needed I agree. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ________________________________ From: Steve Willoughby To: Alan Gauld Cc: tutor at python.org Sent: Saturday, 15 May, 2010 19:24:29 Subject: ttk printing (was Re: [Tutor] application with tabs) I've been seeing Alan and others advocate the new themed widgets for Tkinter and it looks pretty cool, but in what I've seen in an admittedly cursory glance I don't see that there's yet much in the way of printing support for apps (other than getting raw PostScript for what's on a canvas) which is one of the things still pushing me toward wxPython instead. I'd love to make use of all the time I invested over the years learning Tcl/Tk and Tkinter, but this doesn't look like there's a good solution (such as support for writing to the Windows GDI on Windows, although the PostScript output is likely adequate on Unix and Macs). Is there something people are using in that space for Tk? -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. -------------- next part -------------- An HTML attachment was scrubbed... URL: From einstein_87 at hotmail.com Sat May 15 19:41:32 2010 From: einstein_87 at hotmail.com (she haohao) Date: Sun, 16 May 2010 01:41:32 +0800 Subject: [Tutor] Help Message-ID: Hi, I have some questions that I am unable to figure out. Let say I have a file name peaks.txt. Chr1 7 9 4.5 5.5 chr10 6 9 3.5 4.5 chr1 10 6 2.5 4.4 Question is how can i sort the file so that it looks like this: Chr1 7 9 4.5 5.5 chr1 10 6 2.5 4.4 chr10 6 9 3.5 4.5 Next is how do I extract out the p-values(those highlighted in red) After I extracted out all the p-values. for example all the p-values from chr1 is 6,7,9,10 and for chr10 are 6 and 9. So for example if the p-value is 7 from chr1, i would open out a file called chr1.fa which look like this: >chr1 ATTGTACT ATTTGTAT ATTCGTCA and I will extract out the subsequence TACTA. Basically p-value(in this case its 7) position counting from second line of the chr1.fa file and print out the subsequence from starting from position 7-d and 7+d, where d=2. Thus if the p-values is taken from chr10 then we read from the a file with file name chr10.fa which can look like like: chr10 TTAGTACT GTACTAGT ACGTATTT So the question is how do I do this for all the p-values.(i.e all the p-values from chr1 and all the p-values from chr10) if let say we dont know peaks.txt files have how many lines. And how do i output it to a file such that it will have the following format: Chr1 peak value 6: TTGTA peak value 7: TACTA etc etc for all the p-values of chr1 chr10 peak value 7: TTACT etc etc etc... thanks for the help, Angeline _________________________________________________________________ Hotmail: Trusted email with Microsoft?s powerful SPAM protection. https://signup.live.com/signup.aspx?id=60969 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 16 00:22:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 May 2010 23:22:00 +0100 Subject: [Tutor] ttk printing (was Re: application with tabs) References: <20100515182429.GA28712@dragon.alchemy.com> <722910.90896.qm@web86701.mail.ird.yahoo.com> Message-ID: "ALAN GAULD" wrote > For most of my GUI needs printing is rarely an > issue and if I do need to print I generate an HTML document I meant to add that this is similar to the traditional way of priniting in Unix which was to generate a [gnt]roff document and then send it to print via lpr. HTML is the modern equivalent to troff... Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ________________________________ From: Steve Willoughby To: Alan Gauld Cc: tutor at python.org Sent: Saturday, 15 May, 2010 19:24:29 Subject: ttk printing (was Re: [Tutor] application with tabs) I've been seeing Alan and others advocate the new themed widgets for Tkinter and it looks pretty cool, but in what I've seen in an admittedly cursory glance I don't see that there's yet much in the way of printing support for apps (other than getting raw PostScript for what's on a canvas) which is one of the things still pushing me toward wxPython instead. I'd love to make use of all the time I invested over the years learning Tcl/Tk and Tkinter, but this doesn't look like there's a good solution (such as support for writing to the Windows GDI on Windows, although the PostScript output is likely adequate on Unix and Macs). Is there something people are using in that space for Tk? -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. -------------------------------------------------------------------------------- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at alchemy.com Sun May 16 00:47:19 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sat, 15 May 2010 15:47:19 -0700 Subject: [Tutor] ttk printing (was Re: application with tabs) In-Reply-To: References: <722910.90896.qm@web86701.mail.ird.yahoo.com> Message-ID: <20100515224719.GA31862@dragon.alchemy.com> On Sat, May 15, 2010 at 11:22:00PM +0100, Alan Gauld wrote: > > "ALAN GAULD" wrote > > >For most of my GUI needs printing is rarely an > >issue and if I do need to print I generate an HTML document > > I meant to add that this is similar to the traditional way of priniting > in Unix which was to generate a [gnt]roff document and then send it > to print via lpr. > > HTML is the modern equivalent to troff... Yeah, but like troff, it's wholly inadequate for a lot of applications, unfortunately. I'd personally just write a PostScript preamble to do what I need, generate PostScript directly, and spool it up to lpr or whatever and get away (usually) with assuming that people are using PS printers or have their lpr/cups/whatever system configured to deal with it. Windows, of course, gets in the way of that strategy painfully. (And, yeah, I've been known to output groff and TeX from apps too :) -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From alan.gauld at btinternet.com Sun May 16 00:56:02 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 15 May 2010 22:56:02 +0000 (GMT) Subject: [Tutor] ttk printing (was Re: application with tabs) In-Reply-To: <20100515224719.GA31862@dragon.alchemy.com> References: <722910.90896.qm@web86701.mail.ird.yahoo.com> <20100515224719.GA31862@dragon.alchemy.com> Message-ID: <137454.22634.qm@web86708.mail.ird.yahoo.com> > > HTML is the modern equivalent to troff... > > Yeah, but like troff, it's wholly inadequate for a lot of applications, Well at least with troff you can do arbitrarily complex graphics etc (albeit with great difficulty using pic!) but in HTML you are much more limited and often have to resort to generating bitmaps and including them as images in an html document. Being able to print a GDI mirror is much better but sadly many (most?) GUI tookits don't offer that in a cross platform kind of way - wxWidgets is one of the few that seems to work fairly painlessly. And if you need good graphical printing wxWidgets is probably the way to go. But if you only need a GUI to control a program's operations (as I do) then Tkinter is fine. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Sun May 16 01:38:56 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sat, 15 May 2010 16:38:56 -0700 Subject: [Tutor] ttk printing (was Re: application with tabs) In-Reply-To: <137454.22634.qm@web86708.mail.ird.yahoo.com> References: <722910.90896.qm@web86701.mail.ird.yahoo.com> <20100515224719.GA31862@dragon.alchemy.com> <137454.22634.qm@web86708.mail.ird.yahoo.com> Message-ID: <20100515233856.GB31862@dragon.alchemy.com> On Sat, May 15, 2010 at 10:56:02PM +0000, ALAN GAULD wrote: > > > Well at least with troff you can do arbitrarily complex graphics etc > (albeit with great difficulty using pic!) but in HTML you are much Like many things, the learning curve does pay off if one's willing to take it. > Being able to print a GDI mirror is much better but sadly many (most?) > GUI tookits don't offer that in a cross platform kind of way - wxWidgets > is one of the few that seems to work fairly painlessly. And if you need > good graphical printing wxWidgets is probably the way to go. > > But if you only need a GUI to control a program's operations (as I do) > then Tkinter is fine. I'd agree with all of the above. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From davea at ieee.org Sun May 16 01:58:33 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 15 May 2010 19:58:33 -0400 Subject: [Tutor] Help In-Reply-To: References: Message-ID: <4BEF3529.5000802@ieee.org> she haohao wrote: > Hi, > > I have some questions that I am unable to figure out. > > Let say I have a file name peaks.txt. > > Chr1 7 9 4.5 5.5 > chr10 6 9 3.5 4.5 > chr1 10 6 2.5 4.4 > > Question is how can i sort the file so that it looks like this: > > > > Chr1 7 9 4.5 5.5 > chr1 10 6 2.5 4.4 > chr10 6 9 3.5 4.5 > > Next is how do I extract out the p-values(those highlighted in red) > > After I extracted out all the p-values. for example all the p-values from chr1 is 6,7,9,10 and for chr10 are 6 and 9. > > So for example if the p-value is 7 from chr1, i would open out a file called chr1.fa which look like this: > > >> chr1 >> > ATTGTACT > ATTTGTAT > ATTCGTCA > > and I will extract out the subsequence TACTA. Basically p-value(in this case its 7) position counting from second line of the chr1.fa file and print out the subsequence from starting from position 7-d and 7+d, where d=2. Thus if the p-values is taken from chr10 then we read from the a file with file name chr10.fa which can look like like: > > chr10 > TTAGTACT > GTACTAGT > ACGTATTT > > So the question is how do I do this for all the p-values.(i.e all the p-values from chr1 and all the p-values from chr10) if let say we dont know peaks.txt files have how many lines. > > And how do i output it to a file such that it will have the following format: > > Chr1 > > peak value 6: TTGTA > > peak value 7: TACTA > > etc etc for all the p-values of chr1 > > chr10 > > peak value 7: TTACT > > etc etc etc... > > > thanks for the help, > Angeline > > > Red has no meaning in a text message, which is what this list is comprised of. What does your code look like now? Where are you stuck? str.split() can be used to divide a line up by whitespace into "words". So if you split a line (string), you get a list. You can use use [] to extract specific items from that list. The first item in that list is your key, so you can then put it into a dictionary. Don't forget that a dictionary doesn't allow dups, so when you see the dictionary already has a match, append to it, rather than replacing it. DaveA From alan.gauld at btinternet.com Sun May 16 02:04:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 16 May 2010 01:04:11 +0100 Subject: [Tutor] Help References: Message-ID: "she haohao" wrote > Question is how can i sort the file so that it looks like this: > > Chr1 7 9 4.5 5.5 > chr1 10 6 2.5 4.4 > chr10 6 9 3.5 4.5 I have no idea! How would you do it manually? In what way is this considered sorted? What is the sorting criteria? It is not obvious to me! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at ieee.org Sun May 16 03:48:21 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 15 May 2010 21:48:21 -0400 Subject: [Tutor] Help In-Reply-To: References: , <4BEF3529.5000802@ieee.org> Message-ID: <4BEF4EE5.6080102@ieee.org> (You forgot to post to the list. Normally, you can just do a reply-all to get both the list, and the person who last responded. You also top-posted, rather than putting your new message at the end. I'll now continue at the end.) she haohao wrote: > I am stuck because i dont know how do i extract all the p values and how do i sort the file and how i open the respective file. Thanks for helping > > >> Date: Sat, 15 May 2010 19:58:33 -0400 >> From: davea at ieee.org >> To: einstein_87 at hotmail.com >> CC: tutor at python.org >> Subject: Re: [Tutor] Help >> >> she haohao wrote: >> >>> Hi, >>> >>> I have some questions that I am unable to figure out. >>> >>> Let say I have a file name peaks.txt. >>> >>> Chr1 7 9 4.5 5.5 >>> chr10 6 9 3.5 4.5 >>> chr1 10 6 2.5 4.4 >>> >>> Question is how can i sort the file so that it looks like this: >>> >>> >>> >>> Chr1 7 9 4.5 5.5 >>> chr1 10 6 2.5 4.4 >>> chr10 6 9 3.5 4.5 >>> >>> Next is how do I extract out the p-values(those highlighted in red) >>> >>> After I extracted out all the p-values. for example all the p-values from chr1 is 6,7,9,10 and for chr10 are 6 and 9. >>> >>> So for example if the p-value is 7 from chr1, i would open out a file called chr1.fa which look like this: >>> >>> >>> >>>> chr1 >>>> >>>> >>> ATTGTACT >>> ATTTGTAT >>> ATTCGTCA >>> >>> and I will extract out the subsequence TACTA. Basically p-value(in this case its 7) position counting from second line of the chr1.fa file and print out the subsequence from starting from position 7-d and 7+d, where d=2. Thus if the p-values is taken from chr10 then we read from the a file with file name chr10.fa which can look like like: >>> >>> chr10 >>> TTAGTACT >>> GTACTAGT >>> ACGTATTT >>> >>> So the question is how do I do this for all the p-values.(i.e all the p-values from chr1 and all the p-values from chr10) if let say we dont know peaks.txt files have how many lines. >>> >>> And how do i output it to a file such that it will have the following format: >>> >>> Chr1 >>> >>> peak value 6: TTGTA >>> >>> peak value 7: TACTA >>> >>> etc etc for all the p-values of chr1 >>> >>> chr10 >>> >>> peak value 7: TTACT >>> >>> etc etc etc... >>> >>> >>> thanks for the help, >>> Angeline >>> >>> >>> >>> >> Red has no meaning in a text message, which is what this list is >> comprised of. >> >> What does your code look like now? Where are you stuck? >> >> str.split() can be used to divide a line up by whitespace into "words". >> So if you split a line (string), you get a list. You can use use [] to >> extract specific items from that list. >> >> The first item in that list is your key, so you can then put it into a >> dictionary. Don't forget that a dictionary doesn't allow dups, so when >> you see the dictionary already has a match, append to it, rather than >> replacing it. >> >> DaveA >> I didn't offer to write it for you, but to try to help you fix what you've written. When you have written something that sort-of works, please post it, along with a specific question about what's failing. sort() will sort data, not files. If you read in the original data with readlines(), and sort() that list, it'll be sorted by the first few characters of each line. Note that may not be what you mean by sorted, since Chr10 will come before Chr2. Still it'll put lines of identical keys together. After you sort the lines, you can create another file with open(..."w") and use writelines(). Don't forget to close() it. But you probably don't want it sorted, you want a dictionary. Of course, if it's an assignment, then it depends on the wording of the assignment. open() and read() will read data from a file, any file. DaveA From edquiver at gmail.com Sun May 16 09:10:23 2010 From: edquiver at gmail.com (Yutao Deng) Date: Sun, 16 May 2010 15:10:23 +0800 Subject: [Tutor] pickle.load() all dict Message-ID: Hi all: I'm trying to learn to use Python wrote a applet to record every day doing. and i use the pickle pickle.dump something to file no problem i think. but pickle.load whith a problem. can not load all dict do my way that what i pickle.dump(). My code: #### import cPickle as pickle pickle_file = open("data2","rb") i = pickle.load(pickle_file) print i i = pickle.load(pickle_file) print i i = pickle.load(pickle_file) print i i = pickle.load(pickle_file) print i i = pickle.load(pickle_file) print i #### console show : {'2010-5-23': ['1242', 'first']} {'2010-5-24': ['1232', 'third']} {'2010-5-25': ['211', 'second']} {'2010-3-22': ['3211', 'fourrrr']} {'2050-3-2': ['3990', '322']} This is i want but that's silly. if the dict too much, then i have not ideas. the other way from http://mail.python.org/pipermail/tutor/2005-July/039859.html #### import cPickle as pickle pickle_file = open("data2","rb") number_of_pickles = pickle.load(pickle_file) for n in range(number_of_pickles): p = pickle.load(pickle_file) print p #### this way didnt work for me. console show: Traceback (most recent call last): number_of_pickles = pickle.load(pickle_file) cPickle.UnpicklingError: invalid load key, ' '. how do i define a range for pickle.load a file . i mean that how can i know how many dict in the data2. -------------- next part -------------- An HTML attachment was scrubbed... URL: From knacktus at googlemail.com Sun May 16 11:12:06 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 16 May 2010 11:12:06 +0200 Subject: [Tutor] pickle.load() all dict In-Reply-To: References: Message-ID: <4BEFB6E6.30004@googlemail.com> Am 16.05.2010 09:10, schrieb Yutao Deng: > Hi all: > I'm trying to learn to use Python wrote a applet to record every day > doing. > and i use the pickle > pickle.dump something to file no problem i think. > but pickle.load whith a problem. can not load all dict do my way that > what i pickle.dump(). > > My code: > #### > import cPickle as pickle > pickle_file = open("data2","rb") > i = pickle.load(pickle_file) > print i > i = pickle.load(pickle_file) > print i > i = pickle.load(pickle_file) > print i > i = pickle.load(pickle_file) > print i > i = pickle.load(pickle_file) > print i > #### > console show : > {'2010-5-23': ['1242', 'first']} > {'2010-5-24': ['1232', 'third']} > {'2010-5-25': ['211', 'second']} > {'2010-3-22': ['3211', 'fourrrr']} > {'2050-3-2': ['3990', '322']} > > This is i want but that's silly. if the dict too much, then i have not > ideas. > > the other way from > http://mail.python.org/pipermail/tutor/2005-July/039859.html > > #### > import cPickle as pickle > pickle_file = open("data2","rb") > > number_of_pickles = pickle.load(pickle_file) > for n in range(number_of_pickles): > p = pickle.load(pickle_file) > print p > #### > this way didnt work for me. > > console show: > Traceback (most recent call last): > number_of_pickles = pickle.load(pickle_file) > cPickle.UnpicklingError: invalid load key, ' > '. > how do i define a range for pickle.load a file . i mean that how can i > know how many dict in the data2. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > If you want to unpickle a file which contains an unknown number of pickled objects, this should work. You'll get a list containing all the objects. ############################################################# import cPickle import pprint test_data_1 = {"2010-12-13": ["1324", "first"]} test_data_2 = {"2010-12-14": ["234", "first_and_a_half"]} test_data_3 = {"2010-12-15": ["132224", "second"]} for data in (test_data_1, test_data_2, test_data_3): with open("data2.pickle", "ab") as file_stream: cPickle.dump(data, file_stream, 2) with open("data2.pickle", "rb") as file_stream: pickled_objects = [] while True: try: pickled_objects.append(cPickle.load(file_stream)) except EOFError: break print "Number of pickled objects is %s." % len(pickled_objects) pprint.pprint(pickled_objects) ############################################################Cheers, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: From edquiver at gmail.com Sun May 16 14:45:33 2010 From: edquiver at gmail.com (Yutao Deng) Date: Sun, 16 May 2010 20:45:33 +0800 Subject: [Tutor] pickle.load() all dict Message-ID: LOL, try...except is a good idea. i fix it lick this: ############ with open("data2.pickle","rb") as file_stream: c = 0 while True: try: i = cPickle.load(file_stream) print i c += 1 except: print "Numer of pickled objects is %s." %c break ############# -------------- next part -------------- An HTML attachment was scrubbed... URL: From knacktus at googlemail.com Sun May 16 15:05:18 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 16 May 2010 15:05:18 +0200 Subject: [Tutor] pickle.load() all dict In-Reply-To: References: Message-ID: <4BEFED8E.40400@googlemail.com> Am 16.05.2010 14:45, schrieb Yutao Deng: > LOL, try...except is a good idea. > i fix it lick this: > > ############ > with open("data2.pickle","rb") as file_stream: > c = 0 > while True: > try: > i = cPickle.load(file_stream) > print i > c += 1 > except: > print "Numer of pickled objects is %s." %c > break > ############# > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Just a remark on your except statement: You should not use "except:" without specifying a concrete exception class, because all kind of exceptions are then handled by the except block (and therefore hidden). If for example an IOError occurs while reading the file (for what ever reason...), it would be handled by your except block and you would never know that there was a problem reading the file. If you use "except EOFError:" the exception block is evaluated only when the EOFError occurs. All other exceptions are raised as usual. Best regards, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: From janssonks at gmail.com Sun May 16 23:22:13 2010 From: janssonks at gmail.com (Karl Jansson) Date: Sun, 16 May 2010 16:22:13 -0500 Subject: [Tutor] updating python on a mac Message-ID: <199FC0C1-CD15-4FB2-A8C1-551BA725BDA6@gmail.com> I downloaded python 3.1.2 for the mac, and I don't know how to make it so that when I type "python" in the terminal that I get python 3.1.2 instead of 2.5.1, which came with my mac. In the instructions, it says to "update shell profile" but when I do that I get the following message, and nothing gets updated: /Applications/Python\ 3.1/Update\ Shell\ Profile.command ; exit; This script will update your shell profile when the 'bin' directory of python is not early enough of the PATH of your shell. These changes will be effective only in shell windows that you open after running this script. All right, you're a python lover already logout From alan.gauld at btinternet.com Sun May 16 23:31:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 16 May 2010 22:31:44 +0100 Subject: [Tutor] updating python on a mac References: <199FC0C1-CD15-4FB2-A8C1-551BA725BDA6@gmail.com> Message-ID: "Karl Jansson" wrote >I downloaded python 3.1.2 for the mac, and I don't know how to make it > so that when I type "python" in the terminal that I get python 3.1.2 > instead of 2.5.1, which came with my mac. I haven't done this personally so I can only guess but... > /Applications/Python\ 3.1/Update\ Shell\ Profile.command ; exit; > This script will update your shell profile when the 'bin' directory > of python is not early enough of the PATH of your shell. > These changes will be effective only in shell windows that you open > after running this script. > All right, you're a python lover already > logout Did you follow these instructions? ie did you logout and then restart the shell window - ie Terminal? If so and it didn't work try running the env command Look for the PATH entry and see whether Python 2.5 comes before Python 3.1. If so you probably need to edit your ~/.profile or ~/.bashrc or ~/.cshrc depending which shell you use. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From edquiver at gmail.com Mon May 17 04:04:06 2010 From: edquiver at gmail.com (Yutao) Date: Mon, 17 May 2010 10:04:06 +0800 Subject: [Tutor] pickle.load() all dict In-Reply-To: References: Message-ID: Thank you Jan. you are right . an exception whitout a reason not conducive to pragramming. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Sivapathasuntha.Aruliah at amkor.com Mon May 17 10:05:56 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Mon, 17 May 2010 16:05:56 +0800 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: Message-ID: Hi If possible please run the following two lines after saving it as a py file on WINXP and check whether it runs smooythly. When I run I get error. I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. However when I give the following command on Python shell print("Hello", "World!") it neately prints Hello World! #! /usr/bin/env python3 print("Hello", "World!") Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From cyclespoke at windstream.net Mon May 17 15:08:38 2010 From: cyclespoke at windstream.net (Peter) Date: Mon, 17 May 2010 09:08:38 -0400 Subject: [Tutor] Learning python using Michael Dawson's book Message-ID: <4BF13FD6.5080000@windstream.net> Hello, I am at the very beginning of learning Python. If anyone is familiar with Michael Dawson's book: "Python Programming for the Absolute Beginner" The following script (according to the book) should create "block lettering" created by dashes and vertical lines. If I could show a picture of it I would. I do not get the same result as the book. Thank you for any input. Peter The script goes like this: --------------------------------------------------------------- #Game Over- Version 2 #Demonstrates the use of quotes in strings print("Program 'Game Over' 2.0") print("Same", "message", "as before") print("Just", "a bit", "bigger") print("Here", end=" ") print("it is...") print( """ """ ) From rabidpoobear at gmail.com Mon May 17 15:54:15 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 17 May 2010 08:54:15 -0500 Subject: [Tutor] Learning python using Michael Dawson's book In-Reply-To: <4BF13FD6.5080000@windstream.net> References: <4BF13FD6.5080000@windstream.net> Message-ID: I don't see any printing of dashes whatsoever. can you explain in more detail what output you're getting, how it's different from what you expected, and why you think that happened? On 5/17/10, Peter wrote: > Hello, > I am at the very beginning of learning Python. If anyone is familiar > with Michael Dawson's book: "Python Programming for the Absolute Beginner" > The following script (according to the book) should create "block > lettering" created by dashes and vertical lines. If I could show a > picture of it I would. I do not get the same result as the book. > Thank you for any input. > Peter > > > The script goes like this: > > --------------------------------------------------------------- > > #Game Over- Version 2 > #Demonstrates the use of quotes in strings > > print("Program 'Game Over' 2.0") > > print("Same", "message", "as before") > > print("Just", > "a bit", > "bigger") > > print("Here", end=" ") > print("it is...") > > print( > """ > """ > ) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Sent from my mobile device From steve at pearwood.info Mon May 17 16:02:26 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 18 May 2010 00:02:26 +1000 Subject: [Tutor] Learning python using Michael Dawson's book In-Reply-To: <4BF13FD6.5080000@windstream.net> References: <4BF13FD6.5080000@windstream.net> Message-ID: <201005180002.28080.steve@pearwood.info> On Mon, 17 May 2010 11:08:38 pm Peter wrote: > Hello, > I am at the very beginning of learning Python. If anyone is familiar > with Michael Dawson's book: "Python Programming for the Absolute > Beginner" The following script (according to the book) should create > "block lettering" created by dashes and vertical lines. If I could > show a picture of it I would. Why don't you copy and paste it? > I do not get the same result as the book. What result do you get? What result do you expect? -- Steven D'Aprano From cyclespoke at windstream.net Mon May 17 16:29:58 2010 From: cyclespoke at windstream.net (Peter) Date: Mon, 17 May 2010 10:29:58 -0400 Subject: [Tutor] Trying to get this to work - attached is the source code Message-ID: <4BF152E6.3000603@windstream.net> Attached is the source code which accompanies the book and lessons. When I type it in I do not get the same result. I do not get the block lettering, or am I supposed to? Peter ------------------------------------------------------------------------------------------- Hello, > I am at the very beginning of learning Python. If anyone is familiar > with Michael Dawson's book: "Python Programming for the Absolute > Beginner" The following script (according to the book) should create > "block lettering" created by dashes and vertical lines. If I could > show a picture of it I would. ---------------------------------------------------------------------------------------------- Below I have copied and pasted what appears after running the accompanied source code. When I type it manually, I cant get it to work. # Game Over - Version 2 # Demonstrates the use of quotes in strings print("Program 'Peter Stein' 2.0") print("Same", "message", "as before") print("Just", "a bit", "smaller") print("Here", end=" ") print("it is...") print( """ _____ ___ ___ ___ _____ / ___| / | / |/ | | ___| | | / /| | / /| /| | | |__ | | _ / ___ | / / |__/ | | | __| | |_| | / / | | / / | | | |___ \_____/ /_/ |_| /_/ |_| |_____| _____ _ _ _____ _____ / _ \ | | / / | ___| | _ \ | | | | | | / / | |__ | |_| | | | | | | | / / | __| | _ / | |_| | | |/ / | |___ | | \ \ \_____/ |___/ |_____| |_| \_\ """ ) input("\n\nPress the enter key to exit.") -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: game_over2.py URL: From bgailer at gmail.com Mon May 17 18:14:07 2010 From: bgailer at gmail.com (bob gailer) Date: Mon, 17 May 2010 12:14:07 -0400 Subject: [Tutor] Trying to get this to work - attached is the source code In-Reply-To: <4BF152E6.3000603@windstream.net> References: <4BF152E6.3000603@windstream.net> Message-ID: <4BF16B4F.6060703@gmail.com> On 5/17/2010 10:29 AM, Peter wrote: > > Attached is the source code which accompanies the book and lessons. > When I type it in I do not get the same result. I do not get the block > lettering, or am I supposed to? > Peter > ------------------------------------------------------------------------------------------- > > > Hello, >> I am at the very beginning of learning Python. If anyone is familiar >> with Michael Dawson's book: "Python Programming for the Absolute >> Beginner" The following script (according to the book) should create >> "block lettering" created by dashes and vertical lines. If I could >> show a picture of it I would. > > > ---------------------------------------------------------------------------------------------- > > > Below I have copied and pasted what appears after running the > accompanied source code. When I type it manually, I cant get it to work. What does "type it manually" mean? What does can't get it to work" mean? > > # Game Over - Version 2 > # Demonstrates the use of quotes in strings > > print("Program 'Peter Stein' 2.0") > > print("Same", "message", "as before") > > print("Just", > "a bit", > "smaller") > > print("Here", end=" ") > print("it is...") > > print( > """ > _____ ___ ___ ___ _____ > / ___| / | / |/ | | ___| > | | / /| | / /| /| | | |__ > | | _ / ___ | / / |__/ | | | __| > | |_| | / / | | / / | | | |___ > \_____/ /_/ |_| /_/ |_| |_____| > > _____ _ _ _____ _____ > / _ \ | | / / | ___| | _ \ > | | | | | | / / | |__ | |_| | > | | | | | | / / | __| | _ / > | |_| | | |/ / | |___ | | \ \ > \_____/ |___/ |_____| |_| \_\ > > """ > ) > > input("\n\nPress the enter key to exit.") > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Mon May 17 18:29:43 2010 From: wprins at gmail.com (Walter Prins) Date: Mon, 17 May 2010 17:29:43 +0100 Subject: [Tutor] Trying to get this to work - attached is the source code In-Reply-To: <4BF152E6.3000603@windstream.net> References: <4BF152E6.3000603@windstream.net> Message-ID: Hi Peter, We're not familiar with the book, so you'll have to tell us exactly what you're doing, what you're seeing, and what you're expecting ot see instead. Suffice it to say, the script seems fine. When you run it (from an operating system command prompt) it will print, in the command window, the output in block letters etc. You don't really (I suppose) want to be entering those statements directly into the interpreter (which is what I'm guessing "typing it manually" might mean), although they should also work. Obviously the exact output will be different because if you enter the lines in the program manually into the interpreter each line will be executed straight after entry. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Mon May 17 19:21:00 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Mon, 17 May 2010 18:21:00 +0100 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: References: Message-ID: On 17 May 2010 09:05, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: > > Hi > If possible please run the following two lines after *saving it as a py > file on WINXP* and check whether it runs smooythly. *When I run I get > error. *I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 > 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. However > when I give the following command on Python shell print("Hello", "World!") > it neately prints *Hello World!* > > #! /usr/bin/env python3 > > print("Hello", "World!") > > > > Regards, > Siva > Hi, are you sure you got your python path correct? Are you running 64bit windows by any chance? -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From alan at baselinedata.co.uk Mon May 17 13:06:26 2010 From: alan at baselinedata.co.uk (Alan Harris-Reid) Date: Mon, 17 May 2010 12:06:26 +0100 Subject: [Tutor] Unable to run a simple Hello.py in WinXP Message-ID: <4BF12332.9060608@baselinedata.co.uk> Hi Siva, I can run the .py file from the DOS command line without any problem under WinXP. Can you give some more information regarding your error message? Regards, Alan Harris-Reid From jf_byrnes at comcast.net Mon May 17 21:04:43 2010 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Mon, 17 May 2010 14:04:43 -0500 Subject: [Tutor] Trying to get this to work - attached is the source code In-Reply-To: References: <4BF152E6.3000603@windstream.net> Message-ID: <4BF1934B.9030109@comcast.net> Walter Prins wrote: > Hi Peter, > > We're not familiar with the book, so you'll have to tell us exactly what > you're doing, what you're seeing, and what you're expecting ot see instead. > > Suffice it to say, the script seems fine. When you run it (from an > operating system command prompt) it will print, in the command window, the > output in block letters etc. Could be this is the problem. If it is not started at the command prompt you won't see anything. Tripped me up when I first started. > You don't really (I suppose) want to be entering those statements directly > into the interpreter (which is what I'm guessing "typing it manually" might > mean), although they should also work. Obviously the exact output will be > different because if you enter the lines in the program manually into the > interpreter each line will be executed straight after entry. I would guess that this means he types the program from the book, saves it and then runs it. I often do this also, I find that it slows me down and I tend to pay more attention to what I am reading. Regards, Jim > Walter > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From yashwinkanchan at gmail.com Mon May 17 21:28:13 2010 From: yashwinkanchan at gmail.com (Yashwin Kanchan) Date: Mon, 17 May 2010 20:28:13 +0100 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: References: Message-ID: Hi Siva This looks more of issue with the python installation on your PC. Could be a bad installation, in which case I would suggest re-installation of python. Also it could be something to do with permission on your PC. Also you say that you were successful in running the script on python shell. Could you please tell us do you mean IDLE or the python command prompt?In either case please tell us how do you to initiate the shell on your machine. Regards Yashwin Kanchan On 17 May 2010 09:05, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: > > Hi > If possible please run the following two lines after *saving it as a py > file on WINXP* and check whether it runs smooythly. *When I run I get > error. *I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 > 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. However > when I give the following command on Python shell print("Hello", "World!") > it neately prints *Hello World!* > > #! /usr/bin/env python3 > > print("Hello", "World!") > > > > Regards, > Siva > Test Equipment Engineering > Amkor Technology (S) Pte Ltd > 1 Kaki Bukit View > #03-28 TechView Building > Singapore 415941 > Tel: (65) 6347 1131 > Fax: (65) 6746 4815 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From alan.gauld at btinternet.com Mon May 17 23:11:10 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 17 May 2010 22:11:10 +0100 Subject: [Tutor] Trying to get this to work - attached is the source code References: <4BF152E6.3000603@windstream.net> Message-ID: "Peter" wrote > Attached is the source code which accompanies the book and lessons. When > I type it in I do not get the same result. I do not get the block > lettering, or am I supposed to? You need to use a monospaced font(like Courier). If you use something like Arial or Times the spacing will be all messed up and it won't look right. Could that be the problem? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ > Peter > ------------------------------------------------------------------------------------------- > > Hello, >> I am at the very beginning of learning Python. If anyone is familiar >> with Michael Dawson's book: "Python Programming for the Absolute >> Beginner" The following script (according to the book) should create >> "block lettering" created by dashes and vertical lines. If I could >> show a picture of it I would. > > > ---------------------------------------------------------------------------------------------- > > Below I have copied and pasted what appears after running the > accompanied source code. When I type it manually, I cant get it to work. > > # Game Over - Version 2 > # Demonstrates the use of quotes in strings > > print("Program 'Peter Stein' 2.0") > > print("Same", "message", "as before") > > print("Just", > "a bit", > "smaller") > > print("Here", end=" ") > print("it is...") > > print( > """ > _____ ___ ___ ___ _____ > / ___| / | / |/ | | ___| > | | / /| | / /| /| | | |__ > | | _ / ___ | / / |__/ | | | __| > | |_| | / / | | / / | | | |___ > \_____/ /_/ |_| /_/ |_| |_____| > > _____ _ _ _____ _____ > / _ \ | | / / | ___| | _ \ > | | | | | | / / | |__ | |_| | > | | | | | | / / | __| | _ / > | |_| | | |/ / | |___ | | \ \ > \_____/ |___/ |_____| |_| \_\ > > """ > ) > > input("\n\nPress the enter key to exit.") > -------------------------------------------------------------------------------- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at lonetwin.net Tue May 18 00:27:09 2010 From: steve at lonetwin.net (steve) Date: Tue, 18 May 2010 03:57:09 +0530 Subject: [Tutor] Question about packaging with distutils (specifying metadata) Message-ID: <4BF1C2BD.4050502@lonetwin.net> Hello, I am preparing a package for distribution using distutils and when writing the setup.py, i want to specific meta-data like version, author, maintainer ...etc. My question is, assuming that I already have all of that information in the package's __init__.py as ____ variables(*), is it considered good 'style' to import the module one is packaging, in the setup.py to access these variables ? cheers, - steve (*) can't find a reference for doing this in the docs, but I think doing this is a pretty strong convention -- I've seen in most (all?) standard modules as well as third party modules do this. -- random spiel: http://lonetwin.net/ what i'm stumbling into: http://lonetwin.stumbleupon.com/ From steve at pearwood.info Tue May 18 01:53:18 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 18 May 2010 09:53:18 +1000 Subject: [Tutor] Question about packaging with distutils (specifying metadata) In-Reply-To: <4BF1C2BD.4050502@lonetwin.net> References: <4BF1C2BD.4050502@lonetwin.net> Message-ID: <201005180953.18839.steve@pearwood.info> On Tue, 18 May 2010 08:27:09 am steve wrote: > Hello, > > I am preparing a package for distribution using distutils and when > writing the setup.py, i want to specific meta-data like version, > author, maintainer ...etc. > > My question is, assuming that I already have all of that information > in the package's __init__.py as ____ variables(*), is it > considered good 'style' to import the module one is packaging, in the > setup.py to access these variables ? It's considered good style to keep the setup.py script as simple as possible. But with that proviso, there's nothing wrong with importing your own modulo to access the metadata. -- Steven D'Aprano From adam.jtm30 at gmail.com Tue May 18 04:52:51 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 18 May 2010 03:52:51 +0100 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: References: Message-ID: On 18 May 2010 01:30, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: > > > > > > *Adam Bark * > > > *05/18/2010 01:21 AM* > To > Sivapathasuntha Aruliah/S1/AAWW at Amkor > cc > tutor at python.org > Subject > Re: [Tutor] Unable to run a simple Hello.py in WinXP > > > > > > > > > On 17 May 2010 09:05, Sivapathasuntha Aruliah <* > Sivapathasuntha.Aruliah at amkor.com* > > wrote: > > Hi > If possible please run the following two lines after *saving it as a py > file on WINXP* and check whether it runs smooythly. *When I run I get > error. *I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 > 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. However > when I give the following command on Python shell print("Hello", "World!") > it neately prints *Hello World!* > > #! /usr/bin/env python3 > > print("Hello", "World!") > > > > Regards, > Siva > > Hi, are you sure you got your python path correct? Are you running 64bit > windows by any chance? > Hi Adam > I also suspected it could be 64 bit but from IDLE gui which I used it shows > the followings which confirms 32 bit? So please if you have WINXP bit32 > please try to run and show me what command including paths you used to get a > successful run. > > "Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit > (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > >>> " > Sorry I haven't got winXP. The 32 bit on the command line just tells you what python was built for. You need to check your system (in Control Panel) to find out what build of windows you are running. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From rabidpoobear at gmail.com Tue May 18 05:14:24 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 17 May 2010 22:14:24 -0500 Subject: [Tutor] Fwd: Learning python using Michael Dawson's book In-Reply-To: <4BF14DEF.8080003@windstream.net> References: <4BF13FD6.5080000@windstream.net> <4BF14DEF.8080003@windstream.net> Message-ID: Forwarding. Peter use reply-all don't reply offlist please. ---------- Forwarded message ---------- From: Peter Date: Mon, 17 May 2010 10:08:47 -0400 Subject: Re: [Tutor] Learning python using Michael Dawson's book To: Luke Paireepinart Hi, The result in the book has lettering like the following: ___ __ __ | | | | | | | |__| | | | | __ | | | | | | | | | |__| |__| |__| On 5/17/2010 9:54 AM, Luke Paireepinart wrote: > I don't see any printing of dashes whatsoever. > can you explain in more detail what output you're getting, how it's > different from what you expected, and why you think that happened? > > On 5/17/10, Peter wrote: > >> Hello, >> I am at the very beginning of learning Python. If anyone is familiar >> with Michael Dawson's book: "Python Programming for the Absolute Beginner" >> The following script (according to the book) should create "block >> lettering" created by dashes and vertical lines. If I could show a >> picture of it I would. I do not get the same result as the book. >> Thank you for any input. >> Peter >> >> >> The script goes like this: >> >> --------------------------------------------------------------- >> >> #Game Over- Version 2 >> #Demonstrates the use of quotes in strings >> >> print("Program 'Game Over' 2.0") >> >> print("Same", "message", "as before") >> >> print("Just", >> "a bit", >> "bigger") >> >> print("Here", end=" ") >> print("it is...") >> >> print( >> """ >> """ >> ) >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -- Sent from my mobile device From Sivapathasuntha.Aruliah at amkor.com Tue May 18 02:30:27 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Tue, 18 May 2010 08:30:27 +0800 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: Message-ID: Adam Bark 05/18/2010 01:21 AM To Sivapathasuntha Aruliah/S1/AAWW at Amkor cc tutor at python.org Subject Re: [Tutor] Unable to run a simple Hello.py in WinXP On 17 May 2010 09:05, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: Hi If possible please run the following two lines after saving it as a py file on WINXP and check whether it runs smooythly. When I run I get error. I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. However when I give the following command on Python shell print("Hello", "World!") it neately prints Hello World! #! /usr/bin/env python3 print("Hello", "World!") Regards, Siva Hi, are you sure you got your python path correct? Are you running 64bit windows by any chance? Hi Adam I also suspected it could be 64 bit but from IDLE gui which I used it shows the followings which confirms 32 bit? So please if you have WINXP bit32 please try to run and show me what command including paths you used to get a successful run. "Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> " -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From Sivapathasuntha.Aruliah at amkor.com Tue May 18 02:40:48 2010 From: Sivapathasuntha.Aruliah at amkor.com (Sivapathasuntha Aruliah) Date: Tue, 18 May 2010 08:40:48 +0800 Subject: [Tutor] Unable to run a simple Hello.py in WinXP In-Reply-To: Message-ID: Hi Yashwin When I run the hello.py from it's location by double clicking it shows an error with the pop up window C:\Python31\python.exe is not a valid win32 application. When I give the command from IDLE then it says SyntaxError: invalid syntax Which can be seen from Python Shell. Please analyse. Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 Yashwin Kanchan 05/18/2010 03:28 AM To Sivapathasuntha Aruliah/S1/AAWW at Amkor cc tutor at python.org Subject Re: [Tutor] Unable to run a simple Hello.py in WinXP Hi Siva This looks more of issue with the python installation on your PC. Could be a bad installation, in which case I would suggest re-installation of python. Also it could be something to do with permission on your PC. Also you say that you were successful in running the script on python shell. Could you please tell us do you mean IDLE or the python command prompt?In either case please tell us how do you to initiate the shell on your machine. Regards Yashwin Kanchan On 17 May 2010 09:05, Sivapathasuntha Aruliah < Sivapathasuntha.Aruliah at amkor.com> wrote: Hi If possible please run the following two lines after saving it as a py file on WINXP and check whether it runs smooythly. When I run I get error. I use Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. However when I give the following command on Python shell print("Hello", "World!") it neately prints Hello World! #! /usr/bin/env python3 print("Hello", "World!") Regards, Siva Test Equipment Engineering Amkor Technology (S) Pte Ltd 1 Kaki Bukit View #03-28 TechView Building Singapore 415941 Tel: (65) 6347 1131 Fax: (65) 6746 4815 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 8549 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 4880 bytes Desc: not available URL: From mbnoimi at gmx.com Tue May 18 14:34:16 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Tue, 18 May 2010 14:34:16 +0200 Subject: [Tutor] Different between pass & continue Message-ID: <4BF28948.7070307@gmx.com> Hi All, I couldn't understand the difference between pass and continue keywords, could you explain to me? -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net From cwitts at compuscan.co.za Tue May 18 14:17:28 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 18 May 2010 14:17:28 +0200 Subject: [Tutor] Different between pass & continue In-Reply-To: <4BF28948.7070307@gmx.com> References: <4BF28948.7070307@gmx.com> Message-ID: <4BF28558.2080601@compuscan.co.za> M. Bashir Al-Noimi wrote: > Hi All, > > > I couldn't understand the difference between pass and continue > keywords, could you explain to me? > > Taken from the docs at http://docs.python.org/tutorial/controlflow.html The continue statement continues the next iteration of a loop for eg. for line in file: if not line.strip(): # If the line is empty then continue The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: while True: pass # Busy-wait for keyboard interrupt (Ctrl+C) -- Kind Regards, Christian Witts From steve at pearwood.info Tue May 18 14:20:26 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 18 May 2010 22:20:26 +1000 Subject: [Tutor] Different between pass & continue In-Reply-To: <4BF28948.7070307@gmx.com> References: <4BF28948.7070307@gmx.com> Message-ID: <201005182220.27184.steve@pearwood.info> On Tue, 18 May 2010 10:34:16 pm M. Bashir Al-Noimi wrote: > Hi All, > > > I couldn't understand the difference between pass and continue > keywords, could you explain to me? "pass" is a do-nothing statement. It literally does nothing. "continue" is only allowed inside a for-loop or while-loop, and means "jump to the start of the loop". It is related to "break". Consider the difference between these three loops: >>> for x in (1,2,3): ... print(x) ... pass ... print(x, "again") ... 1 1 again 2 2 again 3 3 again >>> >>> >>> for x in (1,2,3): ... print(x) ... continue ... print(x, "again") ... 1 2 3 >>> >>> >>> for x in (1,2,3): ... print(x) ... break ... print(x, "again") ... 1 >>> -- Steven D'Aprano From delegbede at dudupay.com Tue May 18 14:40:31 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 13:40:31 +0100 Subject: [Tutor] PYTHON 3.1 Message-ID: Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> print 'hello' SyntaxError: invalid syntax (, line 1) >>> print ('hello') hello >>> the above print is what i came across having installed python 3.0 and trying to run the print command. with previous versions, a print command takes the form print 'parameter' and the output is parameter but with this new version it seems you need to put in brackets like: print ('hello') to get an output like: hello please confirm this is a new syntax for print. thank you. i will put up morte concerns as they arrive. thanks. -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From eire1130 at gmail.com Tue May 18 15:02:46 2010 From: eire1130 at gmail.com (James Reynolds) Date: Tue, 18 May 2010 09:02:46 -0400 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: On Tue, May 18, 2010 at 8:40 AM, Dipo Elegbede wrote: > Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit > (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > >>> print 'hello' > SyntaxError: invalid syntax (, line 1) > >>> print ('hello') > hello > >>> > > the above print is what i came across having installed python 3.0 and > trying to run the print command. > with previous versions, a print command takes the form > print 'parameter' > and the output is > parameter > > but with this new version it seems you need to put in brackets like: > print ('hello') > to get an output like: > hello > > please confirm this is a new syntax for print. > thank you. > > i will put up morte concerns as they arrive. > > thanks. > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise Application > Development > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > In python 3K print is a function. So, print('hello, world') is the correct syntax. You may find this article helpful: http://docs.python.org/py3k/whatsnew/3.0.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Tue May 18 15:08:10 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 18 May 2010 15:08:10 +0200 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: <4BF2913A.2090101@compuscan.co.za> Dipo Elegbede wrote: > Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit > (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > >>> print 'hello' > SyntaxError: invalid syntax (, line 1) > >>> print ('hello') > hello > >>> > > the above print is what i came across having installed python 3.0 and > trying to run the print command. > with previous versions, a print command takes the form > print 'parameter' > and the output is > parameter > > but with this new version it seems you need to put in brackets like: > print ('hello') > to get an output like: > hello > > please confirm this is a new syntax for print. > thank you. > > i will put up morte concerns as they arrive. > > thanks. > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The Python 3.x series changed the print statement to a print function. -- Kind Regards, Christian Witts From delegbede at dudupay.com Tue May 18 15:30:42 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 14:30:42 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: thanks a lot. i was almost going to abandon this python again out of frustration. i have done it before but with you guys around, it would never happen again. i have a pdf version of python programming for absolute beginners, could anyone please help me with its accompaning CD content? thanks as i anticipate responses. regards. On Tue, May 18, 2010 at 2:02 PM, James Reynolds wrote: > > > On Tue, May 18, 2010 at 8:40 AM, Dipo Elegbede wrote: > >> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "copyright", "credits" or "license()" for more information. >> >>> print 'hello' >> SyntaxError: invalid syntax (, line 1) >> >>> print ('hello') >> hello >> >>> >> >> the above print is what i came across having installed python 3.0 and >> trying to run the print command. >> with previous versions, a print command takes the form >> print 'parameter' >> and the output is >> parameter >> >> but with this new version it seems you need to put in brackets like: >> print ('hello') >> to get an output like: >> hello >> >> please confirm this is a new syntax for print. >> thank you. >> >> i will put up morte concerns as they arrive. >> >> thanks. >> -- >> Elegbede Muhammed Oladipupo >> OCA >> +2348077682428 >> +2347042171716 >> www.dudupay.com >> Mobile Banking Solutions | Transaction Processing | Enterprise Application >> Development >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > In python 3K print is a function. > > So, print('hello, world') is the correct syntax. > > You may find this article helpful: > http://docs.python.org/py3k/whatsnew/3.0.html > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Tue May 18 15:36:02 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 14:36:02 +0100 Subject: [Tutor] what is wrong with this syntax? Message-ID: ples help me figure out what is wrong with this syntax? print('Here are the numbers from 0 to 9') for i in the range(10): print(i) thank you. i am currently reading a byte of a python. thanks. -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From rizaldi.m at gmail.com Tue May 18 15:45:05 2010 From: rizaldi.m at gmail.com (zaldi) Date: Tue, 18 May 2010 20:45:05 +0700 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: Message-ID: in the header of for loop, you don't need to use "the" -> for i in range(10) On 5/18/10, Dipo Elegbede wrote: > ples help me figure out what is wrong with this syntax? > > > print('Here are the numbers from 0 to 9') > for i in the range(10): > print(i) > > thank you. > > i am currently reading a byte of a python. > > thanks. > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise Application > Development > From alex.gunn at smallshinyant.com Tue May 18 15:47:08 2010 From: alex.gunn at smallshinyant.com (alex gunn) Date: Tue, 18 May 2010 14:47:08 +0100 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: Message-ID: its the "the" part print('Here are the numbers from 0 to 9') # for i in the range(10): #your version for i in range(10): #try this print(i) im still learning myself, so be gentle if im wrong but it worked for me. Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Tue May 18 15:49:31 2010 From: davea at ieee.org (Dave Angel) Date: Tue, 18 May 2010 09:49:31 -0400 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: <4BF29AEB.8090402@ieee.org> (Please don't top-post. Add your comments to the end of the portion you're quoting.) Dipo Elegbede wrote: > thanks a lot. > > i was almost going to abandon this python again out of frustration. i have > done it before but with you guys around, it would never happen again. > > i have a pdf version of python programming for absolute beginners, could > anyone please help me with its accompaning CD content? > > thanks as i anticipate responses. > > regards. > > I don't know the version that your CD was written for. If you're going to use a tutorial, it's smart to get a matching version of Python. So if your tutorial is for 2.x, you should get Python 2.6 (or soon, 2.7). Otherwise, you'll be frequently frustrated by the differences. They're not that bad, once you know the language. But while you're learning, try to match your learning materials with your version. DaveA From vceder at canterburyschool.org Tue May 18 15:43:32 2010 From: vceder at canterburyschool.org (Vern Ceder) Date: Tue, 18 May 2010 09:43:32 -0400 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: Message-ID: <4BF29984.1050202@canterburyschool.org> Dipo Elegbede wrote: > ples help me figure out what is wrong with this syntax? > > > print('Here are the numbers from 0 to 9') > for i in the range(10): > print(i) Remove the word "the" print('Here are the numbers from 0 to 9') for i in range(10): print(i) Cheers, Vern > > thank you. > > i am currently reading a byte of a python. > > thanks. > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW From delegbede at dudupay.com Tue May 18 15:53:45 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 14:53:45 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: <4BF29AEB.8090402@ieee.org> References: <4BF29AEB.8090402@ieee.org> Message-ID: I AM CURRENTLY LEARNING WITH PYTHON 3.0 just about now, you are all blowing my minds. this is great. On Tue, May 18, 2010 at 2:49 PM, Dave Angel wrote: > (Please don't top-post. Add your comments to the end of the portion you're > quoting.) > > > Dipo Elegbede wrote: > >> thanks a lot. >> >> i was almost going to abandon this python again out of frustration. i have >> done it before but with you guys around, it would never happen again. >> >> i have a pdf version of python programming for absolute beginners, could >> anyone please help me with its accompaning CD content? >> >> thanks as i anticipate responses. >> >> regards. >> >> > I don't know the version that your CD was written for. > > If you're going to use a tutorial, it's smart to get a matching version of > Python. So if your tutorial is for 2.x, you should get Python 2.6 (or soon, > 2.7). Otherwise, you'll be frequently frustrated by the differences. > > They're not that bad, once you know the language. But while you're > learning, try to match your learning materials with your version. > > DaveA > > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue May 18 15:56:30 2010 From: wprins at gmail.com (Walter Prins) Date: Tue, 18 May 2010 14:56:30 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: IMHO: If you're new to Python and just trying to learn the language, I'd suggest sticking to Python 2.x for now, as the vast majority of Python material out there still use and refer to Python 2.x syntax. IMHO it'll be a lot easier learning and coping with what's changed in Python 3 only once you are already comfortable with Python 2.x syntax, rather than trying to use materials and books referencing 2.x on 3.x and then consequently running into unexpected issues as above, and never being sure whether issues you run into is due to some mistake on your part or a difference between 2.x and 3.x. -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Tue May 18 16:06:52 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 15:06:52 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: That's a good one Sir, i started out with 2.x series but left it for a while. Coming back now, i'm getting on well just for this few changes but I think with a forum like this, I'd fare well in this pythonic journey. Thanks. On Tue, May 18, 2010 at 2:56 PM, Walter Prins wrote: > IMHO: If you're new to Python and just trying to learn the language, I'd > suggest sticking to Python 2.x for now, as the vast majority of Python > material out there still use and refer to Python 2.x syntax. IMHO it'll be > a lot easier learning and coping with what's changed in Python 3 only once > you are already comfortable with Python 2.x syntax, rather than trying to > use materials and books referencing 2.x on 3.x and then consequently running > into unexpected issues as above, and never being sure whether issues you run > into is due to some mistake on your part or a difference between 2.x and > 3.x. > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Tue May 18 16:36:24 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 15:36:24 +0100 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: Message-ID: all worked well. thanks all. On Tue, May 18, 2010 at 2:47 PM, alex gunn wrote: > its the "the" part > > print('Here are the numbers from 0 to 9') > # for i in the range(10): #your version > for i in range(10): #try this > print(i) > > im still learning myself, so be gentle if im wrong but it worked for me. > > Alex > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue May 18 17:23:55 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 19 May 2010 01:23:55 +1000 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: Message-ID: <201005190123.56159.steve@pearwood.info> On Tue, 18 May 2010 11:36:02 pm Dipo Elegbede wrote: > ples help me figure out what is wrong with this syntax? > > > print('Here are the numbers from 0 to 9') > for i in the range(10): > print(i) > > thank you. Others have already given you the answer, but more important is for you to learn *how* to get the answer. Look at the error message Python prints: >>> for i in the range(10): File "", line 1 for i in the range(10): ^ SyntaxError: invalid syntax You get a SyntaxError, which tells you that what you've written makes no sense to the Python compiler. It also tells you that the error has nothing to do with either of the print lines. Unfortunately Python isn't smart enough to recognise that the problem is with "the" rather than "range(10)", but it points you to the correct line. -- Steven D'Aprano From delegbede at dudupay.com Tue May 18 17:55:33 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 16:55:33 +0100 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: <201005190123.56159.steve@pearwood.info> References: <201005190123.56159.steve@pearwood.info> Message-ID: thanks Steven. I'll always be mindful of that. By the way, I need someone to briefly explain to me how the while loop works. a little on break will also do. thanks. On 5/18/10, Steven D'Aprano wrote: > On Tue, 18 May 2010 11:36:02 pm Dipo Elegbede wrote: >> ples help me figure out what is wrong with this syntax? >> >> >> print('Here are the numbers from 0 to 9') >> for i in the range(10): >> print(i) >> >> thank you. > > Others have already given you the answer, but more important is for you > to learn *how* to get the answer. > > Look at the error message Python prints: > >>>> for i in the range(10): > File "", line 1 > for i in the range(10): > ^ > SyntaxError: invalid syntax > > > You get a SyntaxError, which tells you that what you've written makes no > sense to the Python compiler. It also tells you that the error has > nothing to do with either of the print lines. > > Unfortunately Python isn't smart enough to recognise that the problem is > with "the" rather than "range(10)", but it points you to the correct > line. > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From delegbede at dudupay.com Tue May 18 18:39:50 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 17:39:50 +0100 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: References: <201005190123.56159.steve@pearwood.info> Message-ID: A LITTLE EXPLANATIONS ON CONTINUE WOULD BE APPRECIATED TOO. in a recap, i would appreciate any brief explanation on 1. break 2. continue 3. while loop how they work and application in writing codes. thank you all. On 5/18/10, Dipo Elegbede wrote: > thanks Steven. I'll always be mindful of that. > By the way, I need someone to briefly explain to me how the while loop > works. > a little on break will also do. > thanks. > > On 5/18/10, Steven D'Aprano wrote: >> On Tue, 18 May 2010 11:36:02 pm Dipo Elegbede wrote: >>> ples help me figure out what is wrong with this syntax? >>> >>> >>> print('Here are the numbers from 0 to 9') >>> for i in the range(10): >>> print(i) >>> >>> thank you. >> >> Others have already given you the answer, but more important is for you >> to learn *how* to get the answer. >> >> Look at the error message Python prints: >> >>>>> for i in the range(10): >> File "", line 1 >> for i in the range(10): >> ^ >> SyntaxError: invalid syntax >> >> >> You get a SyntaxError, which tells you that what you've written makes no >> sense to the Python compiler. It also tells you that the error has >> nothing to do with either of the print lines. >> >> Unfortunately Python isn't smart enough to recognise that the problem is >> with "the" rather than "range(10)", but it points you to the correct >> line. >> >> >> >> -- >> Steven D'Aprano >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From steve at alchemy.com Tue May 18 19:02:45 2010 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 18 May 2010 10:02:45 -0700 Subject: [Tutor] Loop basics (was Re: what is wrong with this syntax?) In-Reply-To: References: <201005190123.56159.steve@pearwood.info> Message-ID: <20100518170245.GA36925@dragon.alchemy.com> I'm changing the subject line because this is going into a different topic. On Tue, May 18, 2010 at 05:39:50PM +0100, Dipo Elegbede wrote: > A LITTLE EXPLANATIONS ON CONTINUE WOULD BE APPRECIATED TOO. > in a recap, i would appreciate any brief explanation on > 1. break > 2. continue > 3. while loop These are the basic constructs in many languages for repeating a set of tasks over and over, as long as some condition remains true. Say you had a function which asks the user a yes or no question and returns True if they said 'yes' or False if they said 'no'. You want to play a game as long as they keep saying they're willing to play, so assuming a function play_game() which does the actual playing, making Python keep doing this repeatedly would look like this: while ask_yes_or_no('Do you want to play a game?'): play_game() If you get into the loop and decide you want to bail out early rather than waiting for the condition to become False on its own, you can just put a break statement inside the loop. As soon as Python encounters that break, it will stop the loop. while ask_yes_or_no('Do you want to play a game?'): print 'Okay, that will be fun.' if not ask_yes_or_no('Are you sure, though?'): break play_game() continue is like break in that it upsets the normal flow of the loop body, but whereas break stops the loop completely, continue abandons only THIS run through the loop, jumps immediately back to the top, and continues from there, testing the condition to see if another trip through the loop is allowed at this point. For example, you might write the ask_yes_or_no function like this: def ask_yes_or_no(prompt): while True: answer = raw_input(prompt) if answer == 'both': print 'Now that's just silly, try again.' continue if answer == 'yes': return True if answer == 'no': return False print 'Please answer "yes" or "no".' From denis.spir at gmail.com Tue May 18 19:09:40 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 18 May 2010 19:09:40 +0200 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <4BF29AEB.8090402@ieee.org> Message-ID: <20100518190940.5bd20e1e@o> On Tue, 18 May 2010 14:53:45 +0100 Dipo Elegbede wrote: > I AM CURRENTLY LEARNING WITH PYTHON 3.0 > just about now, you are all blowing my minds. > this is great. Please don't write your replies on top. Write them instead just after the part(s) of the message you're replying to; and delete the rest. By doing so, you help keeping the flow of the discussion; else, everything gets messed up after 2-3 replies. Denis ________________________________ vit esse estrany ? spir.wikidot.com From alan.gauld at btinternet.com Tue May 18 19:11:03 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 May 2010 18:11:03 +0100 Subject: [Tutor] PYTHON 3.1 References: Message-ID: "Dipo Elegbede" wrote > please confirm this is a new syntax for print. > thank you. > > i will put up morte concerns as they arrive. Please read the Whats New in Python v3 documents first. Version 3 of Python is a major change in the language with many big changes. Do not just try stuff and send it here every time something breaks. Read the documents first so you know what to expect. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk From delegbede at dudupay.com Tue May 18 19:15:37 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 18:15:37 +0100 Subject: [Tutor] Loop basics (was Re: what is wrong with this syntax?) In-Reply-To: <20100518170245.GA36925@dragon.alchemy.com> References: <201005190123.56159.steve@pearwood.info> <20100518170245.GA36925@dragon.alchemy.com> Message-ID: thanks Steve, this response came handy. I would have to take this home and read. if i encounter difficulties, I'd get back to the house. I'm grateful. If I get more explanations though, it would be great. Regards, On 5/18/10, Steve Willoughby wrote: > I'm changing the subject line because this is going into a different topic. > > On Tue, May 18, 2010 at 05:39:50PM +0100, Dipo Elegbede wrote: >> A LITTLE EXPLANATIONS ON CONTINUE WOULD BE APPRECIATED TOO. >> in a recap, i would appreciate any brief explanation on >> 1. break >> 2. continue >> 3. while loop > > These are the basic constructs in many languages for repeating a set of > tasks over and over, as long as some condition remains true. Say you had > a function which asks the user a yes or no question and returns True if > they said 'yes' or False if they said 'no'. > > You want to play a game as long as they keep saying they're willing to > play, so assuming a function play_game() which does the actual playing, > making Python keep doing this repeatedly would look like this: > > while ask_yes_or_no('Do you want to play a game?'): > play_game() > > If you get into the loop and decide you want to bail out early rather > than waiting for the condition to become False on its own, you can > just put a break statement inside the loop. As soon as Python encounters > that break, it will stop the loop. > > while ask_yes_or_no('Do you want to play a game?'): > print 'Okay, that will be fun.' > if not ask_yes_or_no('Are you sure, though?'): > break > play_game() > > > continue is like break in that it upsets the normal flow of the loop > body, but whereas break stops the loop completely, continue abandons > only THIS run through the loop, jumps immediately back to the top, > and continues from there, testing the condition to see if another trip > through the loop is allowed at this point. > > For example, you might write the ask_yes_or_no function like this: > > def ask_yes_or_no(prompt): > while True: > answer = raw_input(prompt) > if answer == 'both': > print 'Now that's just silly, try again.' > continue > if answer == 'yes': > return True > if answer == 'no': > return False > print 'Please answer "yes" or "no".' > > > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From bgailer at gmail.com Tue May 18 19:18:13 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 18 May 2010 13:18:13 -0400 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: <201005190123.56159.steve@pearwood.info> References: <201005190123.56159.steve@pearwood.info> Message-ID: <4BF2CBD5.8070801@gmail.com> On 5/18/2010 11:23 AM, Steven D'Aprano wrote: > Others have already given you the answer, but more important is for you > to learn *how* to get the answer. > > Look at the error message Python prints: > > >>>> for i in the range(10): >>>> > File "", line 1 > for i in the range(10): > ^ > SyntaxError: invalid syntax > > > You get a SyntaxError, which tells you that what you've written makes no > sense to the Python compiler. It also tells you that the error has > nothing to do with either of the print lines. > > Unfortunately Python isn't smart enough to recognise that the problem is with "the" rather than "range(10)" > To be more specific - Python is "happy" with "for i in the ". It is "expecting"either : or some operator. "range" is neither - so that is where the error pointer is. Example: the = [1,2,3] for i in the: print(i) for i in the + [4]: print(i) -- Bob Gailer 919-636-4239 Chapel Hill NC From delegbede at dudupay.com Tue May 18 19:17:01 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Tue, 18 May 2010 18:17:01 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: <20100518190940.5bd20e1e@o> References: <4BF29AEB.8090402@ieee.org> <20100518190940.5bd20e1e@o> Message-ID: ok On 5/18/10, spir ? wrote: > On Tue, 18 May 2010 14:53:45 +0100 > Dipo Elegbede wrote: > >> I AM CURRENTLY LEARNING WITH PYTHON 3.0 >> just about now, you are all blowing my minds. >> this is great. > > Please don't write your replies on top. Write them instead just after the > part(s) of the message you're replying to; and delete the rest. By doing so, > you help keeping the flow of the discussion; else, everything gets messed up > after 2-3 replies. > > Denis > ________________________________ > > vit esse estrany ? > > spir.wikidot.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From alan.gauld at btinternet.com Tue May 18 19:17:07 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 May 2010 18:17:07 +0100 Subject: [Tutor] what is wrong with this syntax? References: <201005190123.56159.steve@pearwood.info> Message-ID: "Dipo Elegbede" wrote > By the way, I need someone to briefly explain to me how the while > loop works. > a little on break will also do. Your tutorial should do that but you can also look at the Loops section of my tutorial - use the V3 version - for a discussion of while loops. It does not cover break/continue because they are not really necessary to write programs, simply convenience features so I don't cover them till much later. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Tue May 18 19:29:46 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 18 May 2010 19:29:46 +0200 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: <201005190123.56159.steve@pearwood.info> References: <201005190123.56159.steve@pearwood.info> Message-ID: <20100518192946.28c2391d@o> On Wed, 19 May 2010 01:23:55 +1000 Steven D'Aprano wrote: > On Tue, 18 May 2010 11:36:02 pm Dipo Elegbede wrote: > > ples help me figure out what is wrong with this syntax? > > > > > > print('Here are the numbers from 0 to 9') > > for i in the range(10): > > print(i) > > > > thank you. > > Others have already given you the answer, but more important is for you > to learn *how* to get the answer. > > Look at the error message Python prints: > > >>> for i in the range(10): > File "", line 1 > for i in the range(10): > ^ > SyntaxError: invalid syntax > > > You get a SyntaxError, which tells you that what you've written makes no > sense to the Python compiler. It also tells you that the error has > nothing to do with either of the print lines. > > Unfortunately Python isn't smart enough to recognise that the problem is > with "the" rather than "range(10)", but it points you to the correct > line. And logically, if the error is not at the pointed word/line, it will be just before. This means that when analysing your code, python passed on the real error cause, because of a possible ambiguity; but then the rest makes no sense, so it stops and points where it got blocked, immediately after the error. In the code above, "the" could be a name you gave to a list, for instance; since an expression starting like "for i in listName" is correct, Python cannot stop on "the"... but then the rest of the line makes no more sense for it. (Fortunately, python 3.2, planned for April 1, 2011, will be informed that "the" is an english article. This is possible since there is no ambiguity with "th?" (fr), thank to Python's clever diacritic-awareness. Only remains then the problematic case of "a".) Denis ________________________________ vit esse estrany ? spir.wikidot.com From mbnoimi at gmx.com Tue May 18 22:00:34 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Tue, 18 May 2010 22:00:34 +0200 Subject: [Tutor] [SOLVED] Different between pass & continue In-Reply-To: <201005182220.27184.steve@pearwood.info> References: <4BF28948.7070307@gmx.com> <201005182220.27184.steve@pearwood.info> Message-ID: <4BF2F1E2.9040505@gmx.com> Thanks On 18/05/2010 02:20 ?, Steven D'Aprano wrote: > On Tue, 18 May 2010 10:34:16 pm M. Bashir Al-Noimi wrote: > >> Hi All, >> >> >> I couldn't understand the difference between pass and continue >> keywords, could you explain to me? >> > > "pass" is a do-nothing statement. It literally does nothing. > > "continue" is only allowed inside a for-loop or while-loop, and > means "jump to the start of the loop". It is related to "break". > > Consider the difference between these three loops: > > > >>>> for x in (1,2,3): >>>> > ... print(x) > ... pass > ... print(x, "again") > ... > 1 > 1 again > 2 > 2 again > 3 > 3 again > >>>> >>>> for x in (1,2,3): >>>> > ... print(x) > ... continue > ... print(x, "again") > ... > 1 > 2 > 3 > >>>> >>>> for x in (1,2,3): >>>> > ... print(x) > ... break > ... print(x, "again") > ... > 1 > >>>> > > > -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Tue May 18 22:49:15 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 18 May 2010 16:49:15 -0400 Subject: [Tutor] Unit testing command-line options from argparse or optparse Message-ID: Hello all, Does anyone have advice for writing unit tests against variables set by command-line options? I have a program I'd like to run in either "debug" or "live" mode, with various settings associated with each. Below is some pseudo-code that shows what I'd like to do: <> mode = p.parse_args() #always set to either --debug or --live if mode.live: recipients = ['jsmith at email.com', 'janedoe at email.com'] # set logging to a file elif mode.debug: recipients = ['admin at admin.com'] # log to stdout The "live" and "debug" attributes are set by command-line flags passed to the argparse module. What I'd like to do is write tests that check whether various settings (recipients, logging, etc.) are configured properly based on the command-line options. But if "mode" is not set until runtime, I clearly can't import it into my suite of unit tests, right? Is there some standard testing approach to this problem (perhaps mocking?) that you all can recommend? I'd greatly appreciate it. Serdar -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed May 19 01:45:03 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 19 May 2010 09:45:03 +1000 Subject: [Tutor] what is wrong with this syntax? In-Reply-To: <20100518192946.28c2391d@o> References: <201005190123.56159.steve@pearwood.info> <20100518192946.28c2391d@o> Message-ID: <201005190945.04125.steve@pearwood.info> On Wed, 19 May 2010 03:29:46 am spir ? wrote: > (Fortunately, python 3.2, planned for April 1, 2011, will be informed > that "the" is an english article. This is possible since there is no > ambiguity with "th?" (fr), thank to Python's clever > diacritic-awareness. Only remains then the problematic case of "a".) I know you are joking, but that's exactly what Apple's Hypertalk programming language did. In Hypertalk, you could write: get the second field put it before the third field go to the last card of the next background or: get second field put it before third field go to last card of next background -- Steven D'Aprano From knacktus at googlemail.com Wed May 19 05:15:44 2010 From: knacktus at googlemail.com (Knacktus) Date: Wed, 19 May 2010 05:15:44 +0200 Subject: [Tutor] Unit testing command-line options from argparse or optparse In-Reply-To: References: Message-ID: <4BF357E0.3070002@googlemail.com> Am 18.05.2010 22:49, schrieb Serdar Tumgoren: > Hello all, > > Does anyone have advice for writing unit tests against variables set > by command-line options? > > I have a program I'd like to run in either "debug" or "live" mode, > with various settings associated with each. Below is some pseudo-code > that shows what I'd like to do: > > <> > mode = p.parse_args() #always set to either --debug or --live > > if mode.live: > recipients = ['jsmith at email.com ', > 'janedoe at email.com '] > # set logging to a file > elif mode.debug: > recipients = ['admin at admin.com '] > # log to stdout > > The "live" and "debug" attributes are set by command-line flags passed > to the argparse module. What I'd like to do is write tests that check > whether various settings (recipients, logging, etc.) are configured > properly based on the command-line options. > > But if "mode" is not set until runtime, I clearly can't import it into > my suite of unit tests, right? Is there some standard testing approach > to this problem (perhaps mocking?) that you all can recommend? > > I'd greatly appreciate it. > Serdar > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I just had a look at the optparse documentation ... huuu, quite heavier than I have expected.... But to your question: You could reorganise your main module. Put all your code which is on module level into a function called "main" with mode as argurment and add the neat "if __name__ == "__main__" condition at the end of your module to parse the command line options and call your main function. When you import your module to your test, you have to call the "main" function "manually" and can pass a mock for the mode as required. Let's say your main module is called "serdars_main_module" serdars_main_module.py: -------------------------- ######################################################## def main(mode): # all the program logic if mode.live: recipients = ['jsmith at email.com ', 'janedoe at email.com '] # set logging to a file elif mode.debug: recipients = ['admin at admin.com '] # log to stdout # ... if __name__ == "__main__": mode = p.parse_args() #always set to either --debug or --liv main(mode) ######################################################### Then your test module could look like: serdars_test_module.py: ------------------------- ######################################################### import serdars_main_module import unittest class ArgParseMock(object): def __init__(self, live, debug): self.live = live self.debug = debug class TestDebugMode(unittest.TestCase): def test_live_mode(self): mode = ArgParseMock(True, False) # create the mock for the arguments serdars_main_module.main(mode) # call the main logic with the mock # .... def test_debug_mode(self): mode = ArgParseMock(False, True) # create the mock for the arguments serdars_main_module.main(mode) # call the main logic with the mock # .... ########################################################## Hope that helps, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreengels at gmail.com Wed May 19 09:38:34 2010 From: andreengels at gmail.com (Andre Engels) Date: Wed, 19 May 2010 09:38:34 +0200 Subject: [Tutor] Fwd: Learning python using Michael Dawson's book In-Reply-To: References: <4BF13FD6.5080000@windstream.net> <4BF14DEF.8080003@windstream.net> Message-ID: On Tue, May 18, 2010 at 5:14 AM, Luke Paireepinart wrote: > Forwarding. Peter use reply-all don't reply offlist please. > > ---------- Forwarded message ---------- > From: Peter > Date: Mon, 17 May 2010 10:08:47 -0400 > Subject: Re: [Tutor] Learning python using Michael Dawson's book > To: Luke Paireepinart > > Hi, > The result in the book has lettering like the following: > ___ ? ?__ ? ? ? __ > | ? ?| ? ?| ? ?| ? ? | ? ?| > | ? ?|__| ? ?| ? ? | ? ?| > | ? ?__ ? ? | ? ? ?| ? ?| > | ? ?| ? ?| ? ?| ? ? | ? ?| > |__| ? ?|__| ? ? |__| Well, that's definitely not what the code would be producing, nor does it seem to be related to what the example claims to be doing... Perhaps instead of print( """ """ ) which is a very strange idiom (though valid), you should write: print( """ ___ __ __ | | | | | | | |__| | | | | __ | | | | | | | | | |__| |__| |__| """ ) or perhaps you are looking at one example and the explanation of another? -- Andr? Engels, andreengels at gmail.com From delegbede at dudupay.com Wed May 19 09:48:45 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Wed, 19 May 2010 08:48:45 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: Thanks Alan, I'm on it. Regards. On 5/18/10, Alan Gauld wrote: > > "Dipo Elegbede" wrote > >> please confirm this is a new syntax for print. >> thank you. >> >> i will put up morte concerns as they arrive. > > Please read the Whats New in Python v3 documents first. > Version 3 of Python is a major change in the language with > many big changes. Do not just try stuff and send it here > every time something breaks. > > Read the documents first so you know what to expect. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From delegbede at dudupay.com Wed May 19 10:51:14 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Wed, 19 May 2010 09:51:14 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: Message-ID: Hi Alan. I was looking through your page "http://www.alan-g.me.uk/l2p/index.htm" Covering the topic: Looping - Or the art of repeating oneself! Under for loop, Note 3: .......*You can prove that by typing print( list( range(1,13) )*...... The print statement above seem to be incomplete, I think a single parenthesis is missing. it should be six in all. Please confirm and effect the correction on the web page. Thanks. On 5/19/10, Dipo Elegbede wrote: > Thanks Alan, I'm on it. > Regards. > > On 5/18/10, Alan Gauld wrote: >> >> "Dipo Elegbede" wrote >> >>> please confirm this is a new syntax for print. >>> thank you. >>> >>> i will put up morte concerns as they arrive. >> >> Please read the Whats New in Python v3 documents first. >> Version 3 of Python is a major change in the language with >> many big changes. Do not just try stuff and send it here >> every time something breaks. >> >> Read the documents first so you know what to expect. >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From davea at ieee.org Wed May 19 11:46:31 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 19 May 2010 05:46:31 -0400 Subject: [Tutor] Unit testing command-line options from argparse or optparse In-Reply-To: References: Message-ID: <4BF3B377.8090802@ieee.org> Serdar Tumgoren wrote: > Hello all, > > Does anyone have advice for writing unit tests against variables set by > command-line options? > > I have a program I'd like to run in either "debug" or "live" mode, with > various settings associated with each. Below is some pseudo-code that shows > what I'd like to do: > > <> > mode = p.parse_args() #always set to either --debug or --live > > if mode.live: > recipients = ['jsmith at email.com', 'janedoe at email.com'] > # set logging to a file > elif mode.debug: > recipients = ['admin at admin.com'] > # log to stdout > > The "live" and "debug" attributes are set by command-line flags passed to > the argparse module. What I'd like to do is write tests that check whether > various settings (recipients, logging, etc.) are configured properly based > on the command-line options. > > But if "mode" is not set until runtime, I clearly can't import it into my > suite of unit tests, right? Is there some standard testing approach to this > problem (perhaps mocking?) that you all can recommend? > > I'd greatly appreciate it. > Serdar > > I don't see the problem. If 'mode' is a global in module doit.py, then just use import doit and later, if doit.mode.debug The only tricky thing is to make sure that the initialization code has been run before you actually use the latter code. And, presuming mode is an object of a special class made for the purpose, you could go a bit further. You could create the object (with default attributes) in the top-level code of doit.py. That way it already exists when the import is finished. And you could then use from doit import mode Now, change the semantics of parse_args() so that it takes the mode object as a parameter, and modifies it according to the arguments. As long as you don't reassign mode itself, the test code could continue to use its own "variable". But once again, don't use the mode.debug until the initialization has been done. DaveA From joseph.gulizia at gmail.com Wed May 19 13:11:04 2010 From: joseph.gulizia at gmail.com (Joseph Gulizia) Date: Wed, 19 May 2010 06:11:04 -0500 Subject: [Tutor] Fwd: Learning python using Michael Dawson's book In-Reply-To: References: <4BF13FD6.5080000@windstream.net> <4BF14DEF.8080003@windstream.net> Message-ID: I posted this two days ago (I thought to the list...but apparently not). I'm hopeful it helps in some way. I have the same book. Using a text editor and the underscore and dash keys and a few others...the program example is creating an ASCII art version on the words "Game Over". Type it manually probably means to type it out...NOT use copy and paste. Can't get it to work probably means....it doesn't display properly (as the book shows). What needs to be done is he needs to play with the spacing of the characters and different keys to get the "letters" drawn to display closer to the book's example. """ _______ |__ ___| _____ _____ | | / __ \ / ___ \ _ | | | | | | | |___| | | |_| | | |__| | | ____/_ \____/ \______/ \______| """ On Wed, May 19, 2010 at 2:38 AM, Andre Engels wrote: > On Tue, May 18, 2010 at 5:14 AM, Luke Paireepinart > wrote: > > Forwarding. Peter use reply-all don't reply offlist please. > > > > ---------- Forwarded message ---------- > > From: Peter > > Date: Mon, 17 May 2010 10:08:47 -0400 > > Subject: Re: [Tutor] Learning python using Michael Dawson's book > > To: Luke Paireepinart > > > > Hi, > > The result in the book has lettering like the following: > > ___ __ __ > > | | | | | | > > | |__| | | | > > | __ | | | > > | | | | | | > > |__| |__| |__| > > Well, that's definitely not what the code would be producing, nor does > it seem to be related to what the example claims to be doing... > Perhaps instead of > > print( > """ > """ > ) > > which is a very strange idiom (though valid), you should write: > > print( > """ > ___ __ __ > | | | | | | > | |__| | | | > | __ | | | > | | | | | | > |__| |__| |__| > """ > ) > > or perhaps you are looking at one example and the explanation of another? > > > > -- > Andr? Engels, andreengels at gmail.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cyclespoke at windstream.net Wed May 19 15:16:50 2010 From: cyclespoke at windstream.net (Peter) Date: Wed, 19 May 2010 09:16:50 -0400 Subject: [Tutor] Fwd: Learning python using Michael Dawson's book In-Reply-To: References: <4BF13FD6.5080000@windstream.net> <4BF14DEF.8080003@windstream.net> Message-ID: <4BF3E4C2.5060209@windstream.net> Thank you for the responses to my question. This was my first posting and future questions will be made "clearer" to the group. Joseph, I have a feeling you are correct about the spacing. I will try that. Its easy to just load the code file and see the result, but I will learn more if I type out what I see in the book then see a result. Thank much, Peter On 5/19/2010 7:11 AM, Joseph Gulizia wrote: > I posted this two days ago (I thought to the list...but apparently > not). I'm hopeful it helps in some way. > > > > I have the same book. > > Using a text editor and the underscore and dash keys and a few > others...the program example is creating an ASCII art version on the > words "Game Over". > > Type it manually probably means to type it out...NOT use copy and paste. > > Can't get it to work probably means....it doesn't display properly (as > the book shows). > > What needs to be done is he needs to play with the spacing of the > characters and different keys to get the "letters" drawn to display > closer to the book's example. > > > """ > _______ > |__ ___| _____ _____ > | | / __ \ / ___ \ > _ | | | | | | | |___| | > | |_| | | |__| | | ____/_ > \____/ \______/ \______| > > """ > > On Wed, May 19, 2010 at 2:38 AM, Andre Engels > wrote: > > On Tue, May 18, 2010 at 5:14 AM, Luke Paireepinart > > wrote: > > Forwarding. Peter use reply-all don't reply offlist please. > > > > ---------- Forwarded message ---------- > > From: Peter > > > Date: Mon, 17 May 2010 10:08:47 -0400 > > Subject: Re: [Tutor] Learning python using Michael Dawson's book > > To: Luke Paireepinart > > > > > Hi, > > The result in the book has lettering like the following: > > ___ __ __ > > | | | | | | > > | |__| | | | > > | __ | | | > > | | | | | | > > |__| |__| |__| > > Well, that's definitely not what the code would be producing, nor does > it seem to be related to what the example claims to be doing... > Perhaps instead of > > print( > """ > """ > ) > > which is a very strange idiom (though valid), you should write: > > print( > """ > ___ __ __ > | | | | | | > | |__| | | | > | __ | | | > | | | | | | > |__| |__| |__| > """ > ) > > or perhaps you are looking at one example and the explanation of > another? > > > > -- > Andr? Engels, andreengels at gmail.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Wed May 19 19:07:20 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Wed, 19 May 2010 13:07:20 -0400 Subject: [Tutor] Unit testing command-line options from argparse or optparse In-Reply-To: <4BF3B377.8090802@ieee.org> References: <4BF3B377.8090802@ieee.org> Message-ID: Ah, okay -- both of those options make sense. I'll try my hand at Jan's first and will report back if I have any problems. Many thanks as always! -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 19 19:27:03 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 19 May 2010 18:27:03 +0100 Subject: [Tutor] Fwd: Learning python using Michael Dawson's book References: <4BF13FD6.5080000@windstream.net> <4BF14DEF.8080003@windstream.net> <4BF3E4C2.5060209@windstream.net> Message-ID: I thought I had posted a reply on this but I don't see it in the archive... You should make sure your font is set to a mono-spaced variety for this kind of thing otherwise it gets very difficult to align everything... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From Sterba_Robert at emc.com Wed May 19 20:19:58 2010 From: Sterba_Robert at emc.com (Sterba_Robert at emc.com) Date: Wed, 19 May 2010 14:19:58 -0400 Subject: [Tutor] How to convert short name to fqdn Message-ID: <70B5A210D071A849B81D212D5EEDEA9708C4B282@CORPUSMX60C.corp.emc.com> I have a likely simple question, that for some reason is not working as expected. I need to take a servers shortname and convert it to fully qualified. These servers reside in various subdomains, so simply appending a suffix will not work. my hope was that socket.getfqdn('servername') would do this, however this only appears to work if the connection specific domain suffix is the same as the server I'm looking up. If it's different, it just returns the client name. example. my workstation is on subdomaina.domain.com client is on subdomainb.domain.com any short name I put in on subdmainA comes back correctly, but if i put in a short name that resolves to any other subdomain, it just returns the shortname. This is the intended behaviour if socket.getfqdn() cann't resolve the name, however I don't see why it can't resolve the name. from nslookup, I can resolve the client to fqdn just fine there is only one entry for it, so it is not a resolution issue, but appears to be specific to the way the socket.getfqdn() works. Is there something I'm missing, or is there an easy way to do this? Thanks, Rob From garywk at cableone.net Wed May 19 22:47:45 2010 From: garywk at cableone.net (Gary Koskenmaki) Date: Wed, 19 May 2010 13:47:45 -0700 Subject: [Tutor] SetTopWindow Message-ID: <1274302065.2470.40.camel@lappy.gawako.local> I have what is a noob's confusion about this that will lead to bigger problems for me later on if I don't clear it up. In wxPython what a user normally refers to as a "window" is known as a frame. And what is known in wxPython as a window is an object such as a TextCtrl object. At least that is what I take from "wxPython in Action". Now, wxApp has a method called SetTopWindow and you normally pass your wxframe object to it so that it knows what is to be top object. class MyApp(wx.App): def OnInit(self): frame = MyFrame(blah, blah, blah, blah) frame.Show() frame.SetTopWindow(frame) return True class MyFrame(wx.Frame): def __init__(self, blah, blah, blah): wx.Frame.__init__(self, blah,blah,blah) My question, and it's probably a stupid question, is why is SetTopWindow, in how it is named, referring to a window object rather than a frame object? Isn't SetTopWindow saying which frame should be the top frame object? The naming of it seems really ambiguous to me. Why isn't it named something like SetTopFrame? What am I missing? From alan.gauld at btinternet.com Thu May 20 01:19:52 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 20 May 2010 00:19:52 +0100 Subject: [Tutor] SetTopWindow References: <1274302065.2470.40.camel@lappy.gawako.local> Message-ID: "Gary Koskenmaki" wrote >I have what is a noob's confusion about this that will lead to bigger > problems for me later on if I don't clear it up. Don't worry, this kind of confusion abounds in GUI toolkits of all kinds not just wxPython. > In wxPython what a user normally refers to as a "window" is known as > a > frame. And what is known in wxPython as a window is an object such > as a > TextCtrl object. At least that is what I take from "wxPython in > Action". Correct, and the latter usage is common parlance in most GUI frameworks. A window is any visible area of the screen, including widgets. The name for the top level object varies from toolkit to toolkit. > Now, wxApp has a method called SetTopWindow and you normally pass > your > wxframe object to it so that it knows what is to be top object. I think this is historic since many early toolkits had some kind of setTopWindow method (Microsoft and Borland, and at least one X windows toolkit all did). The name in wxPython is simply a reflection of the underlying C++ wxWidgets which, I think, simply reflects the historical legacy. > Why isn't it named something like SetTopFrame? > What am I missing? Nothing, it's just one of the many accidents of programming legacy. Like why does Lisp use "car" to get the first item off a list? Because that's the machine instruction that did it on the computer they built the first Lisp on... There are a lot of these historical accidents that creep into programming languages. Learn to love them! :-) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From norman at khine.net Fri May 21 11:35:06 2010 From: norman at khine.net (Norman Khine) Date: Fri, 21 May 2010 11:35:06 +0200 Subject: [Tutor] loop raw input Message-ID: hello, i have this script, but i am stuck at the loop, if the user types an incorrect database id: http://paste.lisp.org/+25D3 how would i write the exception correctly? norman -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From neven.gorsic at gmail.com Fri May 21 12:17:59 2010 From: neven.gorsic at gmail.com (=?UTF-8?B?TmV2ZW4gR29yxaFpxIc=?=) Date: Fri, 21 May 2010 12:17:59 +0200 Subject: [Tutor] Python 2.5.4 - error in rounding Message-ID: Hi! I run into Python error in rounding and not know how to predict when it will occur in order to prevent wrong result. What can I do to assure accurate result? >>> "%.2f" % 0.445 '0.45' correct >>> "%.2f" % 0.455 '0.46' correct >>> "%.2f" % 0.465 '0.47' correct >>> "%.2f" % 0.475 '0.47' not correct >>> "%.2f" % 0.485 '0.48' not correct >>> "%.2f" % 0.495 '0.50' correct Neven -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri May 21 14:14:51 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 21 May 2010 22:14:51 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: On 05/21/10 20:17, Neven Gor?i? wrote: > Hi! > > I run into Python error in rounding and not know how to predict when it will > occur in order to prevent wrong result. That's because it's floating point number. > What can I do to assure accurate result? Use decimal module to do precise control over your rounding. From neven.gorsic at gmail.com Fri May 21 17:30:48 2010 From: neven.gorsic at gmail.com (=?UTF-8?B?TmV2ZW4gR29yxaFpxIc=?=) Date: Fri, 21 May 2010 17:30:48 +0200 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: Thanks! It's pity that Python has such unreliable functions so you never know in advanced when you will hit new one ... --------------------------- On Fri, May 21, 2010 at 2:14 PM, Lie Ryan wrote: > On 05/21/10 20:17, Neven Gor?i? wrote: > > Hi! > > > > I run into Python error in rounding and not know how to predict when it > will > > occur in order to prevent wrong result. > > That's because it's floating point number. > > > What can I do to assure accurate result? > > Use decimal module to do precise control over your rounding. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri May 21 17:35:02 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 22 May 2010 01:35:02 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding Message-ID: <4BF6A826.90504@gmail.com> On 05/22/10 01:30, Neven Gor?i? wrote: > Thanks! > It's pity that Python has such unreliable functions so you never know in > advanced when you will hit new one ... Well, it's not Python but the machine. Floating point number is not Real numbers; there are certain limitations imposed by physical limitations. You can read: What Every Computer Scientist Should Know About Floating-Point Arithmetic: http://docs.sun.com/source/806-3568/ncg_goldberg.html From neven.gorsic at gmail.com Fri May 21 17:48:27 2010 From: neven.gorsic at gmail.com (=?UTF-8?B?TmV2ZW4gR29yxaFpxIc=?=) Date: Fri, 21 May 2010 17:48:27 +0200 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: <4BF6A826.90504@gmail.com> References: <4BF6A826.90504@gmail.com> Message-ID: Thanks for the assistance and the article. Neven ---------------------- On Fri, May 21, 2010 at 5:35 PM, Lie Ryan wrote: > On 05/22/10 01:30, Neven Gor?i? wrote: > > Thanks! > > It's pity that Python has such unreliable functions so you never know in > > advanced when you will hit new one ... > > Well, it's not Python but the machine. Floating point number is not Real > numbers; there are certain limitations imposed by physical limitations. > > You can read: What Every Computer Scientist Should Know About > Floating-Point Arithmetic: > http://docs.sun.com/source/806-3568/ncg_goldberg.html > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri May 21 19:48:39 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 21 May 2010 13:48:39 -0400 Subject: [Tutor] loop raw input In-Reply-To: References: Message-ID: <4BF6C777.1070300@gmail.com> On 5/21/2010 5:35 AM, Norman Khine wrote: > hello, > i have this script, but i am stuck at the loop, if the user types an > incorrect database id: > > http://paste.lisp.org/+25D3 > > how would i write the exception correctly? > Which line of code could raise the ValueError exception? That line should be in the try block. I also suggest unique error messages. If the user does not enter a number tell him so. Also you need a way to break out of the loop once user enters a valid key. Consider using the isdigit function to test the input instead of an exception. -- Bob Gailer 919-636-4239 Chapel Hill NC From bgailer at gmail.com Fri May 21 19:54:29 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 21 May 2010 13:54:29 -0400 Subject: [Tutor] loop raw input In-Reply-To: <4BF6C777.1070300@gmail.com> References: <4BF6C777.1070300@gmail.com> Message-ID: <4BF6C8D5.4070300@gmail.com> Also note that the database function creates a local variable "database" but does nothing with it. When the function terminates that variable is lost. Did you intend to return it? Also it is not good practice to use the same name for a function and a variable. -- Bob Gailer 919-636-4239 Chapel Hill NC From emile at fenx.com Fri May 21 21:42:12 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 21 May 2010 12:42:12 -0700 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: On 5/21/2010 8:30 AM Neven Gor?i? said... > Thanks! > It's pity that Python has such unreliable functions so you never know in > advanced when you will hit new one ... The problem is with floats, and all languages suffer similarly. Most of us develop various techniques for avoiding these types of issues after being bitten. Emile From norman at khine.net Fri May 21 23:13:01 2010 From: norman at khine.net (Norman Khine) Date: Fri, 21 May 2010 23:13:01 +0200 Subject: [Tutor] loop raw input In-Reply-To: <4BF6C8D5.4070300@gmail.com> References: <4BF6C777.1070300@gmail.com> <4BF6C8D5.4070300@gmail.com> Message-ID: hi, thanks for the feedback, it is much appreciated. On Fri, May 21, 2010 at 7:54 PM, bob gailer wrote: > Also note that the database function creates a local variable "database" but > does nothing with it. > When the function terminates that variable is lost. > Did you intend to return it? yes, i was going to use it further in the script, i will change the script so that it stores this. i have not reached that stage yet in the script where i require this. > Also it is not good practice to use the same name for a function and a > variable. here was a version i did before i read your reply, http://paste.lisp.org/+25DN is there anything you think i could do to make it more efficient? thanks norman > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From wescpy at gmail.com Fri May 21 23:16:20 2010 From: wescpy at gmail.com (wesley chun) Date: Fri, 21 May 2010 14:16:20 -0700 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: correct, it is a floating point issue regardless of language.. it's not just Python. you cannot accurately represent repeating fractions with binary digits (bits). more info specific to Python here: http://docs.python.org/tutorial/floatingpoint.html also, keep in mind that '%f' is not a rounding operation... it just converts floats to strings. if you want to round, you need to use both string formatting as well as the round() built-in function. finally, +1 on using decimal.Decimal as necessary comfortwise. cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.gauld at btinternet.com Fri May 21 23:51:31 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 21 May 2010 22:51:31 +0100 Subject: [Tutor] Python 2.5.4 - error in rounding References: Message-ID: "Neven Gorsic" wrote > I run into Python error in rounding and not know how to predict when > it will > occur in order to prevent wrong result. It depends how you define wrong. When I was at scvhool the rules f or rounding decimals said that if it ended in 5 you rounded to the nearest even digit. So 0.45 -> 0.4 and 0.55 ->0.6 But you seem to assume a 5 always rounds up... > What can I do to assure accurate result? Just to be picky, Python is being accurate, but it is accurately reflecting the binary value... But I think you knew that! :-) > >>>> "%.2f" % 0.465 > '0.47' correct >>>> "%.2f" % 0.475 > '0.47' not correct >>>> "%.2f" % 0.485 > '0.48' not correct The good news is that python's round() function seems to have had the same teacher as you :-) So >>>> "%.2f" % round(0.475,2) > '0.48' >>>> "%.2f" % round(0.485, 2) > '0.49' HTH, Alan G. From alan.gauld at btinternet.com Fri May 21 23:56:12 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 21 May 2010 22:56:12 +0100 Subject: [Tutor] Python 2.5.4 - error in rounding References: Message-ID: "Neven Gor?i?" wrote > It's pity that Python has such unreliable functions so > you never know in advanced when you will hit new one ... The functions are not unreliable. That would imply they give out unpredictable answers. In fact the answers are completely predictable and consistent and correct. They are just working with binary floating point not decimal real numbers. As humans educated to use real numbers we have to get used to that. Alan G. From metolone+gmane at gmail.com Sat May 22 04:16:24 2010 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 21 May 2010 19:16:24 -0700 Subject: [Tutor] Python 2.5.4 - error in rounding References: Message-ID: "Alan Gauld" wrote in message news:ht6v97$632$1 at dough.gmane.org... > > "Neven Gorsic" wrote > >> I run into Python error in rounding and not know how to predict when it >> will >> occur in order to prevent wrong result. > > It depends how you define wrong. When I was at scvhool the rules f > or rounding decimals said that if it ended in 5 you rounded to the > nearest even digit. > So 0.45 -> 0.4 and 0.55 ->0.6 > > But you seem to assume a 5 always rounds up... > >> What can I do to assure accurate result? > > Just to be picky, Python is being accurate, but it is > accurately reflecting the binary value... But I think > you knew that! :-) > >> >>>>> "%.2f" % 0.465 >> '0.47' correct >>>>> "%.2f" % 0.475 >> '0.47' not correct >>>>> "%.2f" % 0.485 >> '0.48' not correct > > The good news is that python's round() function seems > to have had the same teacher as you :-) > So > >>>>> "%.2f" % round(0.475,2) >> '0.48' >>>>> "%.2f" % round(0.485, 2) >> '0.49' Only coincidentally! >>> .475 0.47499999999999998 # %.2f will round down to 0.47 >>> .485 0.48499999999999999 # %.2f will round down to 0.48 >>> round(.475,2) 0.47999999999999998 # %.2f will round up to 0.48 >>> round(.485,2) 0.48999999999999999 # %.2f will round up to 0.49 -Mark From prasadaraon50 at gmail.com Sat May 22 09:46:03 2010 From: prasadaraon50 at gmail.com (prasad rao) Date: Sat, 22 May 2010 13:16:03 +0530 Subject: [Tutor] writing csv files Message-ID: hello! I got a problem writing csv file. 1) csvw=csv.writer(open('/home/prasad/kkm','w'), dialect='excel',fieldnames=names) Traceback (most recent call last): File "", line 1, in TypeError: 'fieldnames' is an invalid keyword argument for this function 2) for x in csvr: ... y=lambda x: ''.join([x.split()[3],x.split()[-3],x.split()[-6]]) ... csvw.write(y) ... Traceback (most recent call last): File "", line 3, in AttributeError: '_csv.writer' object has no attribute 'write' At http://www.python.org/dev/peps/pep-0305/ they are using the function write and the argument fieldnames. Where is the problem?Please someone show me a way to do it. Prasad From alan.gauld at btinternet.com Sat May 22 10:27:47 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 22 May 2010 09:27:47 +0100 Subject: [Tutor] writing csv files References: Message-ID: "prasad rao" wrote > I got a problem writing csv file. I can't help with that specifically but... > for x in csvr: > ... y=lambda x: > ''.join([x.split()[3],x.split()[-3],x.split()[-6]]) > ... csvw.write(y) lambdas are intended for anonymous functions so if you are not going to pass the lambda expression directly but assign it to a name, then it would be better to take the definition outside the loop and only do it once. def y(x): return ''.join([x.split()[3],x.split()[-3],x.split()[-6]]) for x in csvr: csvw.write(y) > Traceback (most recent call last): > File "", line 3, in > AttributeError: '_csv.writer' object has no attribute 'write' But I can't answer this bit, sorry. Alan G From sander.sweers at gmail.com Sat May 22 10:49:25 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 22 May 2010 10:49:25 +0200 Subject: [Tutor] writing csv files In-Reply-To: References: Message-ID: On 22 May 2010 09:46, prasad rao wrote: > ?csvw=csv.writer(open('/home/prasad/kkm','w'), > dialect='excel',fieldnames=names) > Traceback (most recent call last): > ?File "", line 1, in > TypeError: 'fieldnames' is an invalid keyword argument for this function fieldnames is part of the dictreader and dictwriter objects and you use the writer object. See the online docs [1] for more info. Greets Sander [1] http://docs.python.org/library/csv.html#csv.DictWriter From __peter__ at web.de Sat May 22 10:48:25 2010 From: __peter__ at web.de (Peter Otten) Date: Sat, 22 May 2010 10:48:25 +0200 Subject: [Tutor] writing csv files References: Message-ID: prasad rao wrote: > hello! > > I got a problem writing csv file. > > 1) > csvw=csv.writer(open('/home/prasad/kkm','w'), > dialect='excel',fieldnames=names) > Traceback (most recent call last): > File "", line 1, in > TypeError: 'fieldnames' is an invalid keyword argument for this function > > 2) > for x in csvr: > ... y=lambda x: ''.join([x.split()[3],x.split()[-3],x.split()[-6]]) > ... csvw.write(y) > ... > Traceback (most recent call last): > File "", line 3, in > AttributeError: '_csv.writer' object has no attribute 'write' > > At http://www.python.org/dev/peps/pep-0305/ they are using the function > write > and the argument fieldnames. > > Where is the problem?Please someone show me a way to do it. The PEP is not uptodate. If you are using Python 2.6 have a look at http://docs.python.org/library/csv.html You can find the documentation for other versions of Python via http://docs.python.org/index.html If you are using 2.6 the following might work: # prepare rows rows = (row.split() for row in csvr) rows = ((row[3], row[-3], row[-6]) for row in rows) with open(filename, "wb") as out: w = csv.writer(out, dialect="excel") w.writerow(names) # write header w.writerows(rows) # write data Peter From prasadaraon50 at gmail.com Sat May 22 13:16:39 2010 From: prasadaraon50 at gmail.com (prasad rao) Date: Sat, 22 May 2010 16:46:39 +0530 Subject: [Tutor] writing csv files Message-ID: Thanks I got it. I reached the old PEP document by searching the keyword 'excel' Thanks or the help From steve at pearwood.info Sat May 22 14:30:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 May 2010 22:30:57 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: <201005222230.57534.steve@pearwood.info> On Sat, 22 May 2010 07:51:31 am Alan Gauld wrote: > "Neven Gorsic" wrote > > > I run into Python error in rounding and not know how to predict > > when it will > > occur in order to prevent wrong result. > > It depends how you define wrong. When I was at scvhool the rules f > or rounding decimals said that if it ended in 5 you rounded to the > nearest even digit. > So 0.45 -> 0.4 and 0.55 ->0.6 That's called banker's rounding, and if you perform many calculations, on average it leads to the smallest rounding error. > But you seem to assume a 5 always rounds up... That's how I was taught to do rounding in school too. -- Steven D'Aprano From steve at pearwood.info Sat May 22 14:32:10 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 May 2010 22:32:10 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: Message-ID: <201005222232.11199.steve@pearwood.info> On Sat, 22 May 2010 07:16:20 am wesley chun wrote: > correct, it is a floating point issue regardless of language.. it's > not just Python. you cannot accurately represent repeating fractions > with binary digits (bits). more info specific to Python here: > http://docs.python.org/tutorial/floatingpoint.html > > also, keep in mind that '%f' is not a rounding operation... it just > converts floats to strings. if you want to round, you need to use > both string formatting as well as the round() built-in function. > > finally, +1 on using decimal.Decimal as necessary comfortwise. Why do people keep recommending Decimal? Decimals suffer from the exact same issues as floats, plus they are slower. You can't represent all fractions as Decimals either: >>> from decimal import Decimal >>> d = Decimal(1)/Decimal(3) >>> d * 3 == 1 False If you care about representing fractions exactly, use the fraction module, not decimal. >>> from fractions import Fraction >>> f = Fraction(1, 3) >>> f * 3 == 1 True -- Steven D'Aprano From waynejwerner at gmail.com Sat May 22 16:19:07 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Sat, 22 May 2010 09:19:07 -0500 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: <201005222232.11199.steve@pearwood.info> References: <201005222232.11199.steve@pearwood.info> Message-ID: On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano wrote: > Why do people keep recommending Decimal? Decimals suffer from the exact > same issues as floats, > This is exactly incorrect! The Decimal operator offers /exact/ decimal point operations. They implement non-hardware operations to preserve exactness. For more information on exactly what and how the decimal module does what it does, see the following: http://docs.python.org/library/decimal.html http://speleotrove.com/decimal/ http://754r.ucbtest.org/standards/854.pdf plus they are slower. > Because if memory serves correctly the Python implementation uses serial arithmetic, rather than the hardware implementation of floating point calculations. Please stop propagating myths about the Decimal module. For an example about the exactness of Decimal v Float: >>> d = Decimal(1)/Decimal(3) >>> d Decimal('0.3333333333333333333333333333') >>> d*Decimal(3) Decimal('0.9999999999999999999999999999') >>> d = 1/3.0 >>> d*3 1.0 3*.3333 != 1 Floating point rounds that, while Decimal does not. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat May 22 18:58:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 23 May 2010 02:58:57 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: <201005222232.11199.steve@pearwood.info> Message-ID: <201005230258.58723.steve@pearwood.info> On Sun, 23 May 2010 12:19:07 am Wayne Werner wrote: > On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano wrote: > > Why do people keep recommending Decimal? Decimals suffer from the > > exact same issues as floats, > > This is exactly incorrect! The Decimal operator offers /exact/ > decimal point operations. Decimal is only exact for fractions which can be represented by a finite sum of powers-of-ten, like 0.1, just like floats can only represent fractions exactly if they can be represented by a finite sum of powers-of-two, like 0.5. Not only did I demonstrate an example of rounding error using Decimal in my post, but you then repeated that rounding error and then had the audacity to claim that it was "exact": > For an example about the exactness of Decimal v Float: > >>> d = Decimal(1)/Decimal(3) > >>> d > > Decimal('0.3333333333333333333333333333') > > >>> d*Decimal(3) > > Decimal('0.9999999999999999999999999999') Does that look like exactly one to you? I don't know what they taught you, but when I was in school, I learned that one was exactly 1, not 0.9 or 0.999 or even 0.9999999999999999999999999999. But don't believe five hundred years of mathematics, do the test yourself: >>> d*Decimal(3) == 1 False We can calculate the exact error: >>> d = Decimal(1)/Decimal(3) >>> 1 - d*Decimal(3) == 0 False >>> 1 - d*Decimal(3) Decimal('1E-28') Small, but not zero. And adding more precision isn't the answer: it will make the error smaller, true enough, but not zero: >>> decimal.getcontext().prec = 100 >>> d = Decimal(1)/Decimal(3) >>> d*Decimal(3) == 1 False >>> 1 - d*Decimal(3) Decimal('1E-100') To get that error to zero exactly, you need an infinite precision. > They implement non-hardware operations to > preserve exactness. For more information on exactly what and how the > decimal module does what it does, see the following: > > http://docs.python.org/library/decimal.html I know what the decimal module does. It is capable of representing *decimal* numbers exactly, but not all fractions are exact decimal numbers, just as not all fractions are exact binary numbers. For exact fractions, you need the fractions module. > plus they are slower. > > > Because if memory serves correctly the Python implementation uses > serial arithmetic, rather than the hardware implementation of > floating point calculations. I don't understand what you mean by serial arithmetic, but the reason the decimal module is slow is that it is currently written in pure Python. A C version would be faster (but still slower than the hardware implementation of float calculations). > Please stop propagating myths about the Decimal module. I'm not. You are misrepresenting Decimal as a panacea for all rounding issues, which it is not. > >>> d = 1/3.0 > >>> d*3 > 1.0 Curiously, floats perform that specific calculation better than Decimal. That shows that sometimes you can have *too much* precision for a calculation. float rounds off the answer after just 17 decimal places (by memory), giving exactly 1, while Decimal (by default) rounds to 28 places, leading to an error. Of course, there are other calculations where Decimal is more accurate: >>> sum(0.1 for i in range(10)) == 1 False >>> sum(Decimal('0.1') for i in range(10)) == 1 True This is only to be expected, because 0.1 is an exact power of ten, but not an exact power of two. -- Steven D'Aprano From lie.1296 at gmail.com Sun May 23 09:59:03 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 23 May 2010 17:59:03 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: <201005222232.11199.steve@pearwood.info> References: <201005222232.11199.steve@pearwood.info> Message-ID: On 05/22/10 22:32, Steven D'Aprano wrote: > On Sat, 22 May 2010 07:16:20 am wesley chun wrote: >> correct, it is a floating point issue regardless of language.. it's >> not just Python. you cannot accurately represent repeating fractions >> with binary digits (bits). more info specific to Python here: >> http://docs.python.org/tutorial/floatingpoint.html >> >> also, keep in mind that '%f' is not a rounding operation... it just >> converts floats to strings. if you want to round, you need to use >> both string formatting as well as the round() built-in function. >> >> finally, +1 on using decimal.Decimal as necessary comfortwise. > > Why do people keep recommending Decimal? Decimals suffer from the exact > same issues as floats, plus they are slower. I was recommending Decimal because, to me, the OP seems to want to control how the rounding done, instead of actually wanting precision. Decimal is perfectly fine solution if you only want to control the rounding; using fraction to control rounding is possible, but can be a little awkward. Of course, Decimal only gives "predictable rounding", it doesn't really solve infinite .999.. problems. From waynejwerner at gmail.com Sun May 23 19:06:28 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Sun, 23 May 2010 10:06:28 -0700 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: <201005230258.58723.steve@pearwood.info> References: <201005222232.11199.steve@pearwood.info> <201005230258.58723.steve@pearwood.info> Message-ID: On Sat, May 22, 2010 at 9:58 AM, Steven D'Aprano wrote: > On Sun, 23 May 2010 12:19:07 am Wayne Werner wrote: > > On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano > wrote: > > > Why do people keep recommending Decimal? Decimals suffer from the > > > exact same issues as floats, > > > > This is exactly incorrect! The Decimal operator offers /exact/ > > decimal point operations. > > Decimal is only exact for fractions which can be represented by a finite > sum of powers-of-ten, like 0.1, just like floats can only represent > fractions exactly if they can be represented by a finite sum of > powers-of-two, like 0.5. > > Not only did I demonstrate an example of rounding error using Decimal in > my post, but you then repeated that rounding error and then had the > audacity to claim that it was "exact": > Decimal doesn't round - exact precision, not exact accuracy. Floating point has neither reliable precision or accuracy, at least to certain extents. Decimal, OTOH will perform exactly the same under the exact same circumstances every time. No matter how many points of precision you go out to, .3333 * 3 can -never- be equal to 1 (except for very large values of 3). 1/3 is a different number than .33333 repeating. It's close, getting closer the further out you go, and once it reaches infinity then sure, it's equivalent. But unfortunately computers are finite state machines and therefore are not capable of expressing the rational number 1/3 in its decimal equivalent. This has nothing to do with the Decimal module which will always perform reliably - you can count on Decimals to behave, precisely, but floats not so much > > I'm not. You are misrepresenting Decimal as a panacea for all rounding > issues, which it is not. > I never said anything about rounding, I only said it performed Decimal calculations exactly which it does. > > >>> d = 1/3.0 > > >>> d*3 > > 1.0 > > Curiously, floats perform that specific calculation better than Decimal. > That shows that sometimes you can have *too much* precision for > calculation. float rounds off the answer after just 17 decimal places > (by memory), giving exactly 1, while Decimal (by default) rounds to 28 > places, leading to an error. > > Of course, there are other calculations where Decimal is more accurate: > > >>> sum(0.1 for i in range(10)) == 1 > False > >>> sum(Decimal('0.1') for i in range(10)) == 1 > True > > This is only to be expected, because 0.1 is an exact power of ten, but > not an exact power of two. > > Which is exactly what I stated - that Decimals perform exact decimal operations, and are thus more reliable than floating point calculations. Converting fractions to decimals is a separate issue entirely, but one that Decimals will at least behave reliably on, which is the definition of exact that I was using. If you want accurate representation of rational numbers, then of course like you suggested the Fraction module is available. -Wayne > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Sun May 23 21:40:13 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sun, 23 May 2010 15:40:13 -0400 Subject: [Tutor] class methods: using class vars as args? Message-ID: Hello all, I know Python reasonably well, but I still run into basic questions which those over on the other python list request I post here instead. I figure this would be one of them: Why would this not work: class c(object): def __init__(self, arg1, arg2): self.arg1=arg1 self.arg2=arg2 def doSomething(self, arg3=self.arg1): ... The above results in an error that "name 'self' is not defined". Why can I not set the default values of a method's arguments to class vars like that? Thanks! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From alan.gauld at btinternet.com Sun May 23 21:54:04 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 23 May 2010 20:54:04 +0100 Subject: [Tutor] class methods: using class vars as args? References: Message-ID: "Alex Hall" wrote > class c(object): > def __init__(self, arg1, arg2): > self.arg1=arg1 > self.arg2=arg2 > > def doSomething(self, arg3=self.arg1): > ... > > The above results in an error that "name 'self' is not defined". Why > can I not set the default values of a method's arguments to class > vars > like that? Thanks! Because they are not class vars they are instance vars. self.arg1 does not exist until after an instance has been created and they have been set by __init__. At the time the class is defined self.arg1 does not exist. You could do class c(object): defArg = 42 def __init__(self, arg1, arg2): self.arg1=arg1 def doSomething(self, arg3=defArg): ... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk From woodm1979 at gmail.com Sun May 23 21:57:01 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Sun, 23 May 2010 13:57:01 -0600 Subject: [Tutor] class methods: using class vars as args? In-Reply-To: References: Message-ID: Hey Alex, What's happening is that you're still in "defining functions" mode on the line def doSomething(self, arg3=self.arg1): self, which is really nothing more than a parameter being passed in (special parameter, but a parameter none the less) hasn't been assigned a value yet. Imagine this function definition: def do_something(a, b, c=a+b): The parameter instances haven't been instantiated yet. Another way to look at it: You haven't finished defining the class yet, so you can't access data specific to an instance. Not the most technical description, but it's certainly how I look at it. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Sun, May 23, 2010 at 1:40 PM, Alex Hall wrote: > Hello all, > I know Python reasonably well, but I still run into basic questions > which those over on the other python list request I post here instead. > I figure this would be one of them: > Why would this not work: > > class c(object): > def __init__(self, arg1, arg2): > self.arg1=arg1 > self.arg2=arg2 > > def doSomething(self, arg3=self.arg1): > ... > > The above results in an error that "name 'self' is not defined". Why > can I not set the default values of a method's arguments to class vars > like that? Thanks! > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon May 24 02:37:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 24 May 2010 10:37:57 +1000 Subject: [Tutor] Python 2.5.4 - error in rounding In-Reply-To: References: <201005230258.58723.steve@pearwood.info> Message-ID: <201005241037.58025.steve@pearwood.info> On Mon, 24 May 2010 03:06:28 am Wayne Werner wrote: > On Sat, May 22, 2010 at 9:58 AM, Steven D'Aprano wrote: > > On Sun, 23 May 2010 12:19:07 am Wayne Werner wrote: > > > On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano > > > > wrote: > > > > Why do people keep recommending Decimal? Decimals suffer from > > > > the exact same issues as floats, > > > > > > This is exactly incorrect! The Decimal operator offers /exact/ > > > decimal point operations. > > > > Decimal is only exact for fractions which can be represented by a > > finite sum of powers-of-ten, like 0.1, just like floats can only > > represent fractions exactly if they can be represented by a finite > > sum of powers-of-two, like 0.5. > > > > Not only did I demonstrate an example of rounding error using > > Decimal in my post, but you then repeated that rounding error and > > then had the audacity to claim that it was "exact": > > Decimal doesn't round - exact precision, not exact accuracy. Of course decimal rounds! Did you even bother to read the page on Decimal that you told me to read? It has a section called: "Mitigating round-off error with increased precision" http://docs.python.org/library/decimal.html#mitigating-round-off-error-with-increased-precision Why would it have round-off error if it doesn't round? Not only do Decimal calculations round, but it gives the user a choice of rounding modes (e.g. round down, round up, banker's rounding), and whether to trap rounding and treat it as an error. I demonstrated an example of this rounding, a calculation of Decimal(1)/Decimal(3) which did NOT produce the correct result exactly, but ROUNDED the result to 28 decimal places. Still don't believe me? Then explain this: >>> x = Decimal('0.9999999999999999999999999999') >>> x + Decimal(7)/Decimal(10**29) Decimal('1.000000000000000000000000000') The answer without rounding is: 0.99999999999999999999999999997 not one. And then there is this: >>> decimal.getcontext().rounding = decimal.ROUND_DOWN >>> x + Decimal(7)/Decimal(10**29) == x True IEEE-compliant floats also have a choice of rounding modes, but few high-level programming languages expose that functionality. > Floating > point has neither reliable precision or accuracy, at least to certain > extents. On IEEE-compliant systems (which include nearly any computer you're likely to work on) floating point has reliable precision. C singles are reliably 32 bits with 24 binary digits of precision C doubles (which Python uses) are reliably 64 bits with 53 binary digits of precision. As for accuracy, any lack of accuracy (correctness) is a bug in the implementation, not a fundamental flaw in float. E.g. the infamous Pentium FDIV bug. > Decimal, OTOH will perform exactly the same under the exact > same circumstances every time. And so will floats. Decimals and floats are both constructed exactly the same way: number = significant digits * base ** exponent The only difference is that Decimal uses digits 0...9 for the digits and ten for the base, while floats use 0,1 for the digits and two for the base. This makes Decimal very useful because we humans like to work with base-ten numbers like 0.1 and get upset that they can't be expressed exactly in base-two. But Decimal is subject to the exact same issues as float, because at a fundamental level they are constructed the same way with the same limitations. The difference in base merely affects *which* numbers can't be expressed exactly without rounding, not the existence of such numbers. Because we tend to *think* in base 10, we naturally get annoyed that while binary floats can express 0.099999999999999992 exactly, and 0.10000000000000001 exactly, they miss out on 0.1 (as well as an infinite number of other rationals). But decimal floats suffer from the same issue. Between Decimal('0.0999...9') and Decimal('0.1') there are an infinite number of rationals that can't be expressed exactly, and if a calculation *should* produce one of those rationals, it will be rounded to an appropriate > No matter how many points of precision > you go out to, .3333 * 3 can -never- be equal to 1 (except for very > large values of 3). Really? Would you care to put money on that? >>> decimal.getcontext().prec = 3 >>> Decimal('0.3333') Decimal('0.3333') >>> Decimal('0.3333') * 3 Decimal('1.00') > 1/3 is a different number than .33333 repeating. Nonsense. 0.333 repeating is *exactly* one third. Ask a mathematician. Here is a short proof: x = 0.33333... # goes on forever 10x = 3.33333... # still goes on forever subtract the first from the second: 9x = 3.00000... = 3 exactly so x = 3/9 = 1/3 exactly. > It's close, getting closer the further out you go, and once it > reaches infinity then sure, it's equivalent. You can't "reach" infinity. That's why it is *repeating* -- it never stops. This demonstrates that you can't write 1/3 in decimal exactly, you *must* round the number to a finite number of places. You can't write 1/3 exactly in binary either, but you can in base-three: "0.1". > But unfortunately > computers are finite state machines and therefore are not capable of > expressing the rational number 1/3 in its decimal equivalent. Right. Which means any calculation that *should* produce 1/3, like Decimal(1)/Decimal(3), *must* be rounded. > This has nothing to do with the Decimal module which will always > perform reliably - you can count on Decimals to behave, precisely, > but floats not so much You are confused. float(1)/float(3) will always produce the same result, rounded the same way, just as Decimal(1)/Decimal(3) will. [...] > If you want accurate representation of rational numbers, then of > course like you suggested the Fraction module is available. Which is what I said, and you told me to stop spreading myths about Decimal. -- Steven D'Aprano From ahmedn82 at hotmail.com Mon May 24 06:34:53 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Mon, 24 May 2010 12:34:53 +0800 Subject: [Tutor] requesting a help Message-ID: I am facing the same problem that little complicated. I have this kind of data in a file and actually it's coming from another class and it's in formex:0 00 11 01 1and another data which it's in form :0110so now what I need to put it in form data= [[[0,0],[0]], [[0,1],[1]], [[1,0],[1]], [[1,1],[0]] ] that is the form for a class that I can use the [0,1] is the inputs and inputs[0] is = 0 and inputs[1] is = to 1 . the same thing to the output[0] is = 0 and so onok, now this is the problem I have successes to to that reading from file in form0 00 11 01 1the output0110and I write it in a file called it data in the new form which is exactly what I want in form [[[0,0],[0]], [[0,1],[1]], [[1,0],[1]], [[1,1],[0]],]but I got a problem. I cannot use this as a data when I read it from the data file cuz it's string and I tried to use int but couldn`t solve it yet.wish you can help me to find the solution in this problemether I can read the data from the original file and deal with it separately or write it to file and read it again which I am trying to do f=open('data.txt')t=open('target.txt')n=file('newdata.txt','w') def arange (inputs, targets, outputfile): casesNo = len (inputs.readline()) for s in range (casesNo): for line in inputs: data=line.rsplit() i=','.join(data) break for line1 in targets: data1=line1.rsplit() #print data1[0] u=','.join(data1) z= str(i) w= str(u) outputfile.write('[[%s],' % (z)+ '[%s]], \n' %(w)) break outputfile.close() arange(f,t,n) # f : input data, t: target data, n: outputfilelooking to hearing from you as soon as,once again thanks for your help and cooperation, Regards, Hotmail: Trusted email with Microsoft?s powerful SPAM protection. Sign up now. _________________________________________________________________ Hotmail: Powerful Free email with security by Microsoft. https://signup.live.com/signup.aspx?id=60969 -------------- next part -------------- An HTML attachment was scrubbed... URL: From woodm1979 at gmail.com Mon May 24 07:10:05 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Sun, 23 May 2010 23:10:05 -0600 Subject: [Tutor] requesting a help In-Reply-To: References: Message-ID: I'd start with something like this: final_result = [] for data, target in zip(f, t): a, b = [elem.strip() for elem in line.split()] c = target.strip() final_result.append([[a, b], [c]]) Though I'm not sure why you have the "result" data in single element lists. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Sun, May 23, 2010 at 10:34 PM, Ahmed AL-Masri wrote: > > I am facing the same problem that little complicated. > I have this kind of data in a file and actually it's coming from another > class and it's in form > ex: > 0 0 > 0 1 > 1 0 > 1 1 > and another data which it's in form : > 0 > 1 > 1 > 0 > so now what I need to put it in form > data= [[[0,0],[0]], > [[0,1],[1]], > [[1,0],[1]], > [[1,1],[0]] > ] > > that is the form for a class that I can use the [0,1] is the inputs and > inputs[0] is = 0 and inputs[1] is = to 1 . the same thing to the output[0] > is = 0 and so on > ok, now this is the problem > I have successes to to that reading from file in form > 0 0 > 0 1 > 1 0 > 1 1 > the output > 0 > 1 > 1 > 0 > and I write it in a file called it data in the new form which is exactly > what I want in form > [[[0,0],[0]], > [[0,1],[1]], > [[1,0],[1]], > [[1,1],[0]],] > but I got a problem. I cannot use this as a data when I read it from the > data file cuz it's string and I tried to use int but couldn`t solve it yet. > wish you can help me to find the solution in this problem > ether I can read the data from the original file and deal with > it separately or write it to file and read it again which I am trying to do > > f=open('data.txt') > t=open('target.txt') > n=file('newdata.txt','w') > > def arange (inputs, targets, outputfile): > casesNo = len (inputs.readline()) > > for s in range (casesNo): > for line in inputs: > data=line.rsplit() > i=','.join(data) > break > for line1 in targets: > data1=line1.rsplit() > #print data1[0] > u=','.join(data1) > z= str(i) > w= str(u) > outputfile.write('[[%s],' % (z)+ '[%s]], \n' %(w)) > break > outputfile.close() > > arange(f,t,n) # f : input data, t: target data, n: outputfile > looking to hearing from you as soon as, > once again thanks for your help and cooperation, > > Regards, > > > > ------------------------------ > Hotmail: Trusted email with Microsoft?s powerful SPAM protection. Sign up > now. > > ------------------------------ > Hotmail: Powerful Free email with security by Microsoft. Get it now. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From patriciaandmikewelch at msn.com Mon May 24 02:28:44 2010 From: patriciaandmikewelch at msn.com (patricia welch) Date: Sun, 23 May 2010 18:28:44 -0600 Subject: [Tutor] i need help with a code Message-ID: here is what I need and I am completely stumped on what to do. I ususally do not have a problem doing this. but this one is just killing me. Programming a Personal Budget Program Write a program for a personal budget that satisfies these conditions: . The program should have the following command menu selections: Add New Expense, Remove Expense, Add New Income, Remove Income, Exit e The Add New Expense command will prompt the user for the amount of the expense and the frequency of the expense per month. The total monthly expense of an item is its expense multiplied by its monthly frequency. The total initia! budget for monthly expenses is $4,000. If the new expense exceeds what is left for monthly expenses, the program should display a message that the expense was rejected because the budget was exceeded. If the expense can be covered by the monthly budget, the program should display a message that the expense was accepted and state the budget left after the expense. . The Remove Expense should prompt the user for the expense amount and its monthly frequency. tf the expense amount to be removed exceeds whatever has been used of the current budget, then a message should be displayed to recheck the expense amounts. Otherwise the remaining budget will be increased by the amount of the expense reduction and a message displayed stating the amount of the currently available budget funds. ~ . The Add New Income option will prompt the user for the amount of the increase in monthly income and increase the monthly budget by that amount. A message will be displayed indicating the amount of the new available funds. . The Remove Income option will prompt the user for the amount of reduction in monthly income. If the reduction exceeds the available funds, then print a message indicating the amount owing. Otherwise, set the budget to the difference and print the amount of funds available. " c) Programming a Personal Budget Program addExpense( ) removeExpense( ) addRevenue( ) removeRevenue( ) 3. All projects should implement a While loop in their code to display the command selection menu. The While loop should use== a variable* called choice to capture the user's menu selection. The While loop should test to see what value the variable choice has to determine which menu Programming a Personal Budget Progra'm / /variable declarations: Declare Integer choice = 0 Declare Real total Budget = 4000 / /main selection menu While choice != 5 / /display menu Display "Menu Selections:" Display "1_ Add an Expense" Display "2 - Remove an Expense" Display "3 - Add Revenue" Display "4 - Remove Revenue" Display "5 - Exit" Display "Enter your selection:" Input choice !/check menu selection If choice == 1 Then "---./ addExpense( ) Else If choice == 2 Then removeExpense( ) Else If choice == 3 Then addRevenue( ) Else If choice == 4 Then removeRevenue( ) Else If choice == 5 Then Display "Goodbye!" Else Display "Invalid input - please try again." End If End While -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Mon May 24 09:55:29 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Mon, 24 May 2010 08:55:29 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: <436826.15982.qm@web86704.mail.ird.yahoo.com> References: <275057.19841.qm@web86701.mail.ird.yahoo.com> <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> Message-ID: Hello Sir, I'm trying to write a program that checks for the square root of any number input by the User. It's a fairly simple coding though atleast for an ambitious beginner. I want the code to first, calculate the square root and the tell the user whether or not. It should end up telling the User that this is a perfect square. Below is the coding: print('Check for a perfect square') number = int(input('Which number are you checking for? ')) square = (number)**(1/2.0) print('Square Root is = %.2f'%(square)) I am trying to put in something like: if square (is perfect square): print ('This is a perfect square') Pls Help. Thanks. On 5/21/10, ALAN GAULD wrote: > I suspect the likely cause is that you need to change directory to the > directory(folder) where your code lives. Lets say you keep your code > in C:\Projects\Python > > start the DOS box > Type (the bits in bold): > > C\:..\> cd C:\Projects\Python > > Now type > > C:\Projects\Python>python myprogram.py > > using whatever your python file is called... > > Now, provided everything is set up properly it should run. > In fact you might even get away wioth jusdt typing: > > C:\Projects\Python\> myprogram.py > > Because in theory Windows should know to run python > for a .py file.... But it often "forgets" :-( > > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > > > > > > ________________________________ > From: Dipo Elegbede > To: ALAN GAULD > Sent: Friday, 21 May, 2010 19:35:57 > Subject: Re: PYTHON 3.1 > > I still can not successfully run Python from the windows command > prompt after doing all you've directed in the tutorials. (Pls note, > I'm not ruling out the probability that I didn't get the instructions > right. As a matter of fact, this is possibly why I still have the > problem.) > Sir, I'm hoping you could take me through that again. > Like when I type > C:document and setting>python xx.py > It tells me the file does not exit. > It atimes starts python when I type python at the prompt to give: > C:....Python> > When i type the name of the file at the python prompt, I still get an > error.e.g > Python>read.py > It comes with error. > It's challenging because some examples in your tutorial require I run > from the prompt and so I'm getting stucked midway. > Please Help. > Regards, > > On 5/21/10, Dipo Elegbede wrote: >> Hi Alan Sir, >> >> I have been reading through your site and the tutorials, it's just >> something else, I started feeling like a real programmer when i worked >> through the easygui thingy, it was really a mind-blower. >> I hope you'll pardon my pace of learning. I still didn't get some >> aspect under 'Conversing with the user' >> i really read through and I am hoping to reread but I would like you >> shed more light on the stdin and stdout areas. >> They are really confusing. >> Please help. >> Regards, >> >> On 5/20/10, Dipo Elegbede wrote: >>> Ok, Master. I should would have a lot to learn from you. >>> >>> I hope you'd oblige me that rare priviledge. >>> >>> Regards, Master! >>> >>> On 5/20/10, ALAN GAULD wrote: >>>> >>>> >>>>> I may consider pascal after excelling in Python. >>>> >>>> I wouldn't bother, the only place it is used nowadays is in the >>>> Borland Delphi programming tool for Windows(*). Delphi is very >>>> good if you already know Pascal but otherwise is just another >>>> language to learn! :-) >>>> >>>> (*)Although there is a freeware version of Pascal - fpc - that is >>>> compatible with Delphi if you really want to try it out. But >>>> definitely wait till after Python. (Actually Python is a good >>>> intro to Delphi, they have many features in common) >>>> >>>> Alan G. >>> >>> >>> -- >>> Elegbede Muhammed Oladipupo >>> OCA >>> +2348077682428 >>> +2347042171716 >>> www.dudupay.com >>> Mobile Banking Solutions | Transaction Processing | Enterprise >>> Application Development >>> >> >> >> -- >> Elegbede Muhammed Oladipupo >> OCA >> +2348077682428 >> +2347042171716 >> www.dudupay.com >> Mobile Banking Solutions | Transaction Processing | Enterprise >> Application Development >> > > -- > Sent from my mobile device > > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From alan.gauld at btinternet.com Mon May 24 10:01:32 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 24 May 2010 09:01:32 +0100 Subject: [Tutor] i need help with a code References: Message-ID: "patricia welch" wrote > here is what I need and I am completely stumped on what to do. I'm not quite sure what you are asking us here? This appears to be a homework excercise? If so the rules of the list mean we won't give you an solution but we will try to point you towards the answer. However in this case you appear to have been given the outline answer is some kind of weird VB like pseudo code. So what is your question? Are you trying to convert the "VB" code into Python? If so make a stab at it and show us what you've done. / /variable declarations: Declare Integer choice = 0 Declare Real total Budget = 4000 / /main selection menu While choice != 5 / /display menu Display "Menu Selections:" Display "1_ Add an Expense" Display "2 - Remove an Expense" Display "3 - Add Revenue" Display "4 - Remove Revenue" Display "5 - Exit" Display "Enter your selection:" Input choice !/check menu selection If choice == 1 Then "---./ addExpense( ) Else If choice == 2 Then removeExpense( ) Else If choice == 3 Then addRevenue( ) Else If choice == 4 Then removeRevenue( ) Else If choice == 5 Then Display "Goodbye!" Else Display "Invalid input - please try again." End If End While -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From woodm1979 at gmail.com Mon May 24 10:20:19 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Mon, 24 May 2010 02:20:19 -0600 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <275057.19841.qm@web86701.mail.ird.yahoo.com> <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> Message-ID: Well, I'd use the raw_input function instead of the input function. and I'd check out the math.floor function as well. :-) Lemme know if you have any other questions. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Mon, May 24, 2010 at 1:55 AM, Dipo Elegbede wrote: > Hello Sir, > > I'm trying to write a program that checks for the square root of any > number input by the User. > It's a fairly simple coding though atleast for an ambitious beginner. > > I want the code to first, calculate the square root and the tell the > user whether or not. > > It should end up telling the User that this is a perfect square. > > Below is the coding: > > print('Check for a perfect square') > number = int(input('Which number are you checking for? ')) > square = (number)**(1/2.0) > print('Square Root is = %.2f'%(square)) > > I am trying to put in something like: > > if square (is perfect square): > print ('This is a perfect square') > > Pls Help. > > Thanks. > > > On 5/21/10, ALAN GAULD wrote: > > I suspect the likely cause is that you need to change directory to the > > directory(folder) where your code lives. Lets say you keep your code > > in C:\Projects\Python > > > > start the DOS box > > Type (the bits in bold): > > > > C\:..\> cd C:\Projects\Python > > > > Now type > > > > C:\Projects\Python>python myprogram.py > > > > using whatever your python file is called... > > > > Now, provided everything is set up properly it should run. > > In fact you might even get away wioth jusdt typing: > > > > C:\Projects\Python\> myprogram.py > > > > Because in theory Windows should know to run python > > for a .py file.... But it often "forgets" :-( > > > > > > Alan Gauld > > Author of the Learn To Program website > > http://www.alan-g.me.uk/ > > > > > > > > > > > > ________________________________ > > From: Dipo Elegbede > > To: ALAN GAULD > > Sent: Friday, 21 May, 2010 19:35:57 > > Subject: Re: PYTHON 3.1 > > > > I still can not successfully run Python from the windows command > > prompt after doing all you've directed in the tutorials. (Pls note, > > I'm not ruling out the probability that I didn't get the instructions > > right. As a matter of fact, this is possibly why I still have the > > problem.) > > Sir, I'm hoping you could take me through that again. > > Like when I type > > C:document and setting>python xx.py > > It tells me the file does not exit. > > It atimes starts python when I type python at the prompt to give: > > C:....Python> > > When i type the name of the file at the python prompt, I still get an > > error.e.g > > Python>read.py > > It comes with error. > > It's challenging because some examples in your tutorial require I run > > from the prompt and so I'm getting stucked midway. > > Please Help. > > Regards, > > > > On 5/21/10, Dipo Elegbede wrote: > >> Hi Alan Sir, > >> > >> I have been reading through your site and the tutorials, it's just > >> something else, I started feeling like a real programmer when i worked > >> through the easygui thingy, it was really a mind-blower. > >> I hope you'll pardon my pace of learning. I still didn't get some > >> aspect under 'Conversing with the user' > >> i really read through and I am hoping to reread but I would like you > >> shed more light on the stdin and stdout areas. > >> They are really confusing. > >> Please help. > >> Regards, > >> > >> On 5/20/10, Dipo Elegbede wrote: > >>> Ok, Master. I should would have a lot to learn from you. > >>> > >>> I hope you'd oblige me that rare priviledge. > >>> > >>> Regards, Master! > >>> > >>> On 5/20/10, ALAN GAULD wrote: > >>>> > >>>> > >>>>> I may consider pascal after excelling in Python. > >>>> > >>>> I wouldn't bother, the only place it is used nowadays is in the > >>>> Borland Delphi programming tool for Windows(*). Delphi is very > >>>> good if you already know Pascal but otherwise is just another > >>>> language to learn! :-) > >>>> > >>>> (*)Although there is a freeware version of Pascal - fpc - that is > >>>> compatible with Delphi if you really want to try it out. But > >>>> definitely wait till after Python. (Actually Python is a good > >>>> intro to Delphi, they have many features in common) > >>>> > >>>> Alan G. > >>> > >>> > >>> -- > >>> Elegbede Muhammed Oladipupo > >>> OCA > >>> +2348077682428 > >>> +2347042171716 > >>> www.dudupay.com > >>> Mobile Banking Solutions | Transaction Processing | Enterprise > >>> Application Development > >>> > >> > >> > >> -- > >> Elegbede Muhammed Oladipupo > >> OCA > >> +2348077682428 > >> +2347042171716 > >> www.dudupay.com > >> Mobile Banking Solutions | Transaction Processing | Enterprise > >> Application Development > >> > > > > -- > > Sent from my mobile device > > > > Elegbede Muhammed Oladipupo > > OCA > > +2348077682428 > > +2347042171716 > > www.dudupay.com > > Mobile Banking Solutions | Transaction Processing | Enterprise > > Application Development > > > > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 24 11:34:11 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Mon, 24 May 2010 09:34:11 +0000 (GMT) Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <275057.19841.qm@web86701.mail.ird.yahoo.com> <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> Message-ID: <582007.7747.qm@web86705.mail.ird.yahoo.com> > Well, I'd use the raw_input function instead of the input function. input is raw_input in Python 3.1. raw_input doesn't exist. Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmcaine at googlemail.com Mon May 24 11:36:59 2010 From: cmcaine at googlemail.com (C M Caine) Date: Mon, 24 May 2010 10:36:59 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <275057.19841.qm@web86701.mail.ird.yahoo.com> <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> Message-ID: On 24 May 2010 09:20, Matthew Wood wrote: > Well, I'd use the raw_input function instead of the input function. > > and I'd check out the math.floor function as well.? :-) > > Lemme know if you have any other questions. > > -- > > I enjoy haiku > but sometimes they don't make sense; > refrigerator? raw_input has been renamed to input in python 3.0. To get the old input behaviour you have to use eval. Great sig, btw. From woodm1979 at gmail.com Mon May 24 11:51:18 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Mon, 24 May 2010 03:51:18 -0600 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> Message-ID: Well, what do you know! Turns out, I never use that function anyway, so it won't change a damn thing for me. But thanks for the heads up! And thanks for the nod to the haiku. So many people don't see it. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Mon, May 24, 2010 at 3:36 AM, C M Caine wrote: > On 24 May 2010 09:20, Matthew Wood wrote: > > Well, I'd use the raw_input function instead of the input function. > > > > and I'd check out the math.floor function as well. :-) > > > > Lemme know if you have any other questions. > > > > -- > > > > I enjoy haiku > > but sometimes they don't make sense; > > refrigerator? > > raw_input has been renamed to input in python 3.0. To get the old > input behaviour you have to use eval. > > Great sig, btw. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bala.biophysics at gmail.com Mon May 24 18:35:21 2010 From: bala.biophysics at gmail.com (Bala subramanian) Date: Mon, 24 May 2010 18:35:21 +0200 Subject: [Tutor] circular looping Message-ID: Friends, I have a list. I have to do some comparison of each item in the list with each other items in the list. from numpy import zeros a=zeros((5,5)) myres=['R', 'N', 'L', 'C', 'M'] DON=['R','N','L'] ; ALL=['R','L','M','S'] for x in myres: for y in myres: if x in DON: if y in ALL: a[res.index(x),res.index(y)] = 1 else: continue But here the value of y changes sequentially. I want y to cycle through the list something like the following. Is there any function to do such circular iteration. cycle 1 y's value should be 'N', 'L', 'C', 'M, 'R' index 1,2,3,4,0 of myres cycle 2 y's value should be 'L', 'C', 'M, 'R', 'N' index 2,3,4,0,1 ,, cycle 3 y's value should be 'C', 'M', 'R', 'N','L' index 3,4,0,1,2 ,, Thank you, Bala -------------- next part -------------- An HTML attachment was scrubbed... URL: From evosweet at hotmail.com Mon May 24 19:28:30 2010 From: evosweet at hotmail.com (Rayon) Date: Mon, 24 May 2010 13:28:30 -0400 Subject: [Tutor] problem with cherrypy Message-ID: I am trying to get and set some session variables in ram with cherrypy Here is my code: import cherrypy #cherry.session.get('user') #cherry.session.get('password') def security(f): cherry.session.get('user') cherry.session.get('password') error message: AttributeError: 'module' object has no attribute 'session' Could some one plz help me -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 24 19:40:59 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 24 May 2010 18:40:59 +0100 Subject: [Tutor] problem with cherrypy References: Message-ID: "Rayon" wrote >I am trying to get and set some session variables in ram with >cherrypy > > Here is my code: > > import cherrypy > #cherry.session.get('user') > #cherry.session.get('password') You import cherrypy but you use cherry? Should it maybe be: cherrypy.session? or maybe even cherrypy.cherry.session? > AttributeError: 'module' object has no attribute 'session' OTOH the error message seems to suggest it recognises cherry as a module. Which doesn't seem consistent with your code? Puzzled and curious, Alan G. From alan.gauld at btinternet.com Mon May 24 19:57:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 24 May 2010 18:57:13 +0100 Subject: [Tutor] circular looping References: Message-ID: "Bala subramanian" wrote > But here the value of y changes sequentially. I want y to cycle > through the > list something like the following. Is there any function to do such > circular > iteration. > cycle 1 y's value should be 'N', 'L', 'C', 'M, 'R' index 1,2,3,4,0 > of myres > cycle 2 y's value should be 'L', 'C', 'M, 'R', 'N' index 2,3,4,0,1 > ,, > cycle 3 y's value should be 'C', 'M', 'R', 'N','L' index 3,4,0,1,2 > ,, The usual trick to get cyclic indices is to use the mod operator with the length of the list (and maybe adding a constant) So L = "RNLCM" y = 0 -> (y+1) % len(L) -> 1 y = 1 -> 2 ... y = 4 -> 0 y = 5 -> 1 etc.... Does that help? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From evosweet at hotmail.com Mon May 24 20:24:18 2010 From: evosweet at hotmail.com (Rayon) Date: Mon, 24 May 2010 14:24:18 -0400 Subject: [Tutor] problems with cherrypy sessions Message-ID: Here is the code I am not sure why this is not working I have sessions turned on. import cherrypy @cherrypy.expose def security(f): cherrypy.session.get('id') return f -------------- next part -------------- An HTML attachment was scrubbed... URL: From woodm1979 at gmail.com Mon May 24 20:27:43 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Mon, 24 May 2010 12:27:43 -0600 Subject: [Tutor] circular looping In-Reply-To: References: Message-ID: This is a GREAT application for generators! def cycle(seq, index): l = len(seq) for i in range(len(seq)): cursor = (i + index) % l yield seq[cursor] Then you just pass it the starting index you want, and all is well in the world. :-) Also, gratuitous use of the enumerate function in for-loops is grand: for x_index, x in enumerate(myres): if not x in DON: continue for y_index, y in enumerate(cycle(myres)): print x, y if y in ALL: a[x_index][y_index] = 1 print '-' -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Mon, May 24, 2010 at 10:35 AM, Bala subramanian < bala.biophysics at gmail.com> wrote: > Friends, > I have a list. I have to do some comparison of each item in the list with > each other items in the list. > > from numpy import zeros > a=zeros((5,5)) > > myres=['R', 'N', 'L', 'C', 'M'] > DON=['R','N','L'] ; ALL=['R','L','M','S'] > > for x in myres: > for y in myres: > if x in DON: > if y in ALL: a[res.index(x),res.index(y)] = 1 > else: continue > > But here the value of y changes sequentially. I want y to cycle through the > list something like the following. Is there any function to do such circular > iteration. > cycle 1 y's value should be 'N', 'L', 'C', 'M, 'R' index 1,2,3,4,0 of > myres > cycle 2 y's value should be 'L', 'C', 'M, 'R', 'N' index 2,3,4,0,1 > ,, > cycle 3 y's value should be 'C', 'M', 'R', 'N','L' index 3,4,0,1,2 > ,, > > Thank you, > Bala > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbnoimi at gmx.com Tue May 25 20:28:54 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Tue, 25 May 2010 20:28:54 +0200 Subject: [Tutor] Append sub-key to dictionary Message-ID: <4BFC16E6.6020703@gmx.com> Hi All, I'm trying to append a new sub-key to specific dictionary but it replacing the content of parent key instead of add a sub-key! How I can fix this issue? --snippet-- addressbook = { 'work': { 'Andre': { 'phone': '22761654', 'e-mail': '5456646', 'address': 'Syria, Aleppo', 'website': 'www.sdff.com' } }, 'Friends': { 'Ahmad': { 'phone': '34646464', 'picture': '/home/fiends/wael.png', 'posts': { 'blog': 'http://www.dsfd.com/', 'newspaper': 'http://news.com/' } }, 'Issa': { 'e-mail': 'asd at dsfdsc.com' } } } addressbook['Friends'] = { 'kassem': { 'address':'Aleppo Street', 'Country':'Palestine', 'articles':{ 'blog':'http://blogger.com', 'news':'http://news.com/' } } } -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From vinces1979 at gmail.com Tue May 25 19:38:54 2010 From: vinces1979 at gmail.com (vince spicer) Date: Tue, 25 May 2010 11:38:54 -0600 Subject: [Tutor] Append sub-key to dictionary In-Reply-To: <4BFC16E6.6020703@gmx.com> References: <4BFC16E6.6020703@gmx.com> Message-ID: On Tue, May 25, 2010 at 12:28 PM, M. Bashir Al-Noimi wrote: > Hi All, > > I'm trying to append a new sub-key to specific dictionary but it replacing > the content of parent key instead of add a sub-key! > > > How I can fix this issue? > > --snippet-- > > addressbook = { > 'work': { > 'Andre': { > 'phone': '22761654', > 'e-mail': '5456646', > 'address': 'Syria, Aleppo', > 'website': 'www.sdff.com' > } > }, > 'Friends': { > 'Ahmad': { > 'phone': '34646464', > 'picture': '/home/fiends/wael.png', > 'posts': { > 'blog': 'http://www.dsfd.com/', > 'newspaper': 'http://news.com/' > } > }, > 'Issa': { > 'e-mail': 'asd at dsfdsc.com' > } > } > } > > addressbook['Friends'] = { > 'kassem': { > 'address':'Aleppo Street', > 'Country':'Palestine', > 'articles':{ > 'blog':'http://blogger.com', > 'news':'http://news.com/' > } > } > } > > > -- > Best Regards > Muhammad Bashir Al-Noimi > My Blog: http://mbnoimi.net > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You may have better results with: addressbook['Friends']['kassem"] = { 'address':'Aleppo Street', 'Country':'Palestine', 'articles':{ 'blog':' http://blogger.com', 'news':'http://news.com/' } } } this will create a new key in the Friends dict Vince -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbnoimi at gmx.com Tue May 25 20:56:58 2010 From: mbnoimi at gmx.com (M. Bashir Al-Noimi) Date: Tue, 25 May 2010 20:56:58 +0200 Subject: [Tutor] [SOLVED] Append sub-key to dictionary In-Reply-To: References: <4BFC16E6.6020703@gmx.com> Message-ID: <4BFC1D7A.2090706@gmx.com> Thanks Vince ;-) On 25/05/2010 07:38 ?, vince spicer wrote: > On Tue, May 25, 2010 at 12:28 PM, M. Bashir Al-Noimi > wrote: > > Hi All, > > I'm trying to append a new sub-key to specific dictionary but it > replacing the content of parent key instead of add a sub-key! > > > How I can fix this issue? > > --snippet-- > > addressbook = { > 'work': { > 'Andre': { > 'phone': '22761654', > 'e-mail': '5456646', > 'address': 'Syria, Aleppo', > 'website': 'www.sdff.com ' > } > }, > 'Friends': { > 'Ahmad': { > 'phone': '34646464', > 'picture': '/home/fiends/wael.png', > 'posts': { > 'blog': 'http://www.dsfd.com/ ', > 'newspaper': 'http://news.com/ ' > } > }, > 'Issa': { > 'e-mail': 'asd at dsfdsc.com ' > } > } > } > > addressbook['Friends'] = { > 'kassem': { > 'address':'Aleppo Street', > 'Country':'Palestine', > 'articles':{ > 'blog':'http://blogger.com ', > 'news':'http://news.com/ ' > } > } > } > > > -- > Best Regards > Muhammad Bashir Al-Noimi > My Blog:http://mbnoimi.net > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > You may have better results with: > > addressbook['Friends']['kassem"] = { > 'address':'Aleppo Street', > 'Country':'Palestine', > 'articles':{ > > 'blog':'http://blogger.com', > > 'news':'http://news.com/' > } > } > } > > > this will create a new key in the Friends dict > > Vince -- Best Regards Muhammad Bashir Al-Noimi My Blog: http://mbnoimi.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Wed May 26 03:13:57 2010 From: mehgcap at gmail.com (Alex Hall) Date: Tue, 25 May 2010 21:13:57 -0400 Subject: [Tutor] 2d list index inverting? Message-ID: Hello all, I have a 2d list being used for a battleship game. I have structured the program so that it uses a grid class, which implements this array along with a bunch of other methods and vars. For example, to get at the top left square, you would say: Grid.getSquareAt(0,0) and inside getSquareAt is simply: def getSquareAt(self, x, y): return self.b[x][y] #b is the internal 2d list for the class However, I am getting very confused with indexing. I keep getting errors about list index out of range and I am not sure why. I have a feeling that using 2d lists is supposed to go like a matrix (row,column) and not like a coordinate plane (column, row). Any suggestions? If you want the full source, just let me know and I will update the zip file on my server and send the link. TIA! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Wed May 26 03:38:58 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 26 May 2010 03:38:58 +0200 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: On Wed, May 26, 2010 at 3:13 AM, Alex Hall wrote: > Hello all, > I have a 2d list being used for a battleship game. I have structured > the program so that it uses a grid class, which implements this array > along with a bunch of other methods and vars. For example, to get at > the top left square, you would say: > Grid.getSquareAt(0,0) > and inside getSquareAt is simply: > ?def getSquareAt(self, x, y): > ?return self.b[x][y] #b is the internal 2d list for the class > > However, I am getting very confused with indexing. I keep getting > errors about list index out of range and I am not sure why. I have a > feeling that using 2d lists is supposed to go like a matrix > (row,column) and not like a coordinate plane (column, row). A 2D list doesn't really exist. What you're using is just a list whose elements are also lists. A nested data structure. And whether those sub-lists should be the rows or the columns? It doesn't matter. A list is just a list. Sequential data elements. It doesn't care whether it represents a row or a column. What are 'row' and 'column' anyway? just words designating some arbitrary notion. Conventions. You can swap one for the other, and the data remains accessible. As long as you're consistent, there's no problem. The real problem is something else entirely. Somewhere in your code, you are using an index that is greater than the size of the list. Perhaps you're not consistent, somewhere. Mixing up your row/column order. Perhaps something else is amiss. No way to tell from the snippet. Hugo From mehgcap at gmail.com Wed May 26 03:47:19 2010 From: mehgcap at gmail.com (Alex Hall) Date: Tue, 25 May 2010 21:47:19 -0400 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: On 5/25/10, Hugo Arts wrote: > On Wed, May 26, 2010 at 3:13 AM, Alex Hall wrote: >> Hello all, >> I have a 2d list being used for a battleship game. I have structured >> the program so that it uses a grid class, which implements this array >> along with a bunch of other methods and vars. For example, to get at >> the top left square, you would say: >> Grid.getSquareAt(0,0) >> and inside getSquareAt is simply: >> def getSquareAt(self, x, y): >> return self.b[x][y] #b is the internal 2d list for the class >> >> However, I am getting very confused with indexing. I keep getting >> errors about list index out of range and I am not sure why. I have a >> feeling that using 2d lists is supposed to go like a matrix >> (row,column) and not like a coordinate plane (column, row). > > A 2D list doesn't really exist. What you're using is just a list whose > elements are also lists. A nested data structure. And whether those > sub-lists should be the rows or the columns? It doesn't matter. A list > is just a list. Sequential data elements. It doesn't care whether it > represents a row or a column. What are 'row' and 'column' anyway? just > words designating some arbitrary notion. Conventions. You can swap one > for the other, and the data remains accessible. As long as you're > consistent, there's no problem. I thought so, but I was hoping you would not say that as this means a logic bug deep in my code, and those are the hardest to track down... > > The real problem is something else entirely. Somewhere in your code, > you are using an index that is greater than the size of the list. Yes, and it looks like my coordinates are bing reversed somewhere, but I cannot find anywhere where that is happening in the code. > Perhaps you're not consistent, somewhere. Mixing up your row/column > order. Perhaps something else is amiss. No way to tell from the > snippet. So, a lot of print() statements then... > > Hugo > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Wed May 26 04:14:58 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 26 May 2010 04:14:58 +0200 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: On Wed, May 26, 2010 at 3:47 AM, Alex Hall wrote: > > I thought so, but I was hoping you would not say that as this means a > logic bug deep in my code, and those are the hardest to track down... Unfortunately, yes. Bug hunting is part art, part science. >> The real problem is something else entirely. Somewhere in your code, >> you are using an index that is greater than the size of the list. > > Yes, and it looks like my coordinates are bing reversed somewhere, but > I cannot find anywhere where that is happening in the code. This is a good thing. You already have some idea of what is happening. Confirm your suspicions, then work from there. >> Perhaps you're not consistent, somewhere. Mixing up your row/column >> order. Perhaps something else is amiss. No way to tell from the >> snippet. > So, a lot of print() statements then... It's a good start. I suggest you begin at the place where the error occurs, then work your way back slowly. verify at each point that your data is mixed up, until you find what introduced the mixup. There are other some other useful tools. The python debugger might be of some help. http://docs.python.org/library/pdb.html It's especially useful to gain more insight in what precisely a piece of code is doing. There is a learning curve to it, but once you know how to use it it will pay back many times over. I think you'll have this bug resolved soon enough by yourself, but should you get the urge to bang your head against a wall repeatedly, come back here and we'll have a closer look at it. Good luck and happy hunting, Hugo From mehgcap at gmail.com Wed May 26 04:59:46 2010 From: mehgcap at gmail.com (Alex Hall) Date: Tue, 25 May 2010 22:59:46 -0400 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: On 5/25/10, Hugo Arts wrote: > On Wed, May 26, 2010 at 3:47 AM, Alex Hall wrote: >> >> I thought so, but I was hoping you would not say that as this means a >> logic bug deep in my code, and those are the hardest to track down... > > Unfortunately, yes. Bug hunting is part art, part science. You can say that again. My major (just one year left!) is computer science, focusing in programming (though Python is something I am pursuing on my own), so I have done a lot of finding seemingly impossible bugs. It is never fun, though, until that wonderful point when you squash the offending insect! > >>> The real problem is something else entirely. Somewhere in your code, >>> you are using an index that is greater than the size of the list. >> >> Yes, and it looks like my coordinates are bing reversed somewhere, but >> I cannot find anywhere where that is happening in the code. > > This is a good thing. You already have some idea of what is happening. > Confirm your suspicions, then work from there. > >>> Perhaps you're not consistent, somewhere. Mixing up your row/column >>> order. Perhaps something else is amiss. No way to tell from the >>> snippet. >> So, a lot of print() statements then... > > It's a good start. I suggest you begin at the place where the error > occurs, then work your way back slowly. verify at each point that your > data is mixed up, until you find what introduced the mixup. Yes. I have found that, somewhere, my rows are being swapped for columns, and vice versa. My grid on the screen is 10 columns of 14 rows, but the computer sees 14 columns of 10 rows. However, I am currently at a loss as to just where this switch is taking place. Still, it looks like finding one line will fix it, not every reference to coordinates. > > There are other some other useful tools. The python debugger might be > of some help. http://docs.python.org/library/pdb.html > It's especially useful to gain more insight in what precisely a piece > of code is doing. There is a learning curve to it, but once you know > how to use it it will pay back many times over. I am blind, using Jaws for Windows to access my computer (http://www.freedomscientific.com). Is this debugger a cmd line tool, or is it / is it part of an IDE? Basically I am hoping it is not its own interface, as these are often not too compatible with screen readers. I will definitely read through the page, though, and that should help to answer the above question. I just figured I would ask in case you had any knowledge of the debugger's accessibility. > > I think you'll have this bug resolved soon enough by yourself, but > should you get the urge to bang your head against a wall repeatedly, > come back here and we'll have a closer look at it. Good luck and happy > hunting, > > Hugo Thanks! Hopefully I will not need to have anyone on here go through the code, but sometimes the best thing is a fresh pair of eyes. The problem is that none of my friends know a thing about Python, so I guess that is where this list comes in, should it prove necessary. Thanks for the offer! > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Wed May 26 07:27:58 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 26 May 2010 07:27:58 +0200 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: On Wed, May 26, 2010 at 4:59 AM, Alex Hall wrote: > > I am blind, using Jaws for Windows to access my computer > (http://www.freedomscientific.com). Is this debugger a cmd line tool, > or is it / is it part of an IDE? Basically I am hoping it is not its > own interface, as these are often not too compatible with screen > readers. I will definitely read through the page, though, and that > should help to answer the above question. I just figured I would ask > in case you had any knowledge of the debugger's accessibility. It's a command-line tool, similar to the gnu debugger (if you're familiar with that). Just run "python -m pdb your_script.py" and you'll drop into the debugger's prompt. if your python interpreter works, this'll work just fine. Hugo From davea at ieee.org Wed May 26 10:46:18 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 26 May 2010 04:46:18 -0400 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: <4BFCDFDA.4040200@ieee.org> Hugo Arts wrote: > On Wed, May 26, 2010 at 3:13 AM, Alex Hall wrote: > >> Hello all, >> I have a 2d list being used for a battleship game. I have structured >> the program so that it uses a grid class, which implements this array >> along with a bunch of other methods and vars. For example, to get at >> the top left square, you would say: >> Grid.getSquareAt(0,0) >> and inside getSquareAt is simply: >> def getSquareAt(self, x, y): >> return self.b[x][y] #b is the internal 2d list for the class >> >> However, I am getting very confused with indexing. I keep getting >> errors about list index out of range and I am not sure why. I have a >> feeling that using 2d lists is supposed to go like a matrix >> (row,column) and not like a coordinate plane (column, row). >> > > A 2D list doesn't really exist. What you're using is just a list whose > elements are also lists. A nested data structure. And whether those > sub-lists should be the rows or the columns? It doesn't matter. A list > is just a list. Sequential data elements. It doesn't care whether it > represents a row or a column. What are 'row' and 'column' anyway? just > words designating some arbitrary notion. Conventions. You can swap one > for the other, and the data remains accessible. As long as you're > consistent, there's no problem. > > The real problem is something else entirely. Somewhere in your code, > you are using an index that is greater than the size of the list. > Perhaps you're not consistent, somewhere. Mixing up your row/column > order. Perhaps something else is amiss. No way to tell from the > snippet. > > Hugo > > My question would be how are you creating these lists, and how you're updating them. If you're doing a 20x20 board, are you actually creating 20 lists, each of size 20, in instance attribute b ? Do you do that in the __init__() constructor? Or are you doing some form of "sparse array" where you only initialize the items that have ships in them? As for updating, are you always doing the update by assigning to self.b[row][col] ? You could always add a try/catch to the spot that crashes, and in the catch clause, temporarily print out the subscripts you're actually seeing. As Hugo says, you could simply have values out of range. DaveA From denis.spir at gmail.com Wed May 26 11:26:15 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Wed, 26 May 2010 11:26:15 +0200 Subject: [Tutor] 2d list index inverting? In-Reply-To: References: Message-ID: <20100526112615.44dbfd3d@o> On Tue, 25 May 2010 21:47:19 -0400 Alex Hall wrote: > I thought so, but I was hoping you would not say that as this means a > logic bug deep in my code, and those are the hardest to track down... No, they're not. On the contrary. Logical bugs are easily diagnosed by printing out relevant values at the right place in your code. Just do it and in 5 mn your code works as expected. Denis ________________________________ vit esse estrany ? spir.wikidot.com From hugo.yoshi at gmail.com Wed May 26 22:20:56 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 26 May 2010 22:20:56 +0200 Subject: [Tutor] 2d list index inverting? In-Reply-To: <20100526112615.44dbfd3d@o> References: <20100526112615.44dbfd3d@o> Message-ID: 2010/5/26 spir ? : > > No, they're not. On the contrary. Logical bugs are easily diagnosed by printing out relevant values at the right place in your code. Just do it and in 5 mn your code works as expected. > And wormholes are easily stabilized by lining their interior with exotic matter ;-) Seriously though, deciding what the relevant values and the right places are is not always as trivial as that sentence implies. Out of the three types of errors generally recognized (compiler errors, run-time errors, logic errors), the logic error is the hardest to debug, precisely because it is difficult to locate. From tim at johnsons-web.com Thu May 27 00:32:22 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Wed, 26 May 2010 14:32:22 -0800 Subject: [Tutor] Successfully trapping the [505 file not found] error Message-ID: <20100526223222.GA4020@johnsons-web.com> Using Python 2.6.2 on Slackware 13.0 32-bit. I'm using Python as a command-line application to do an FTP download which processes a file of jpg filenames, downloading each. Not all of the file names can be found. I'm having a problem trapping *all* of the file not found errors. As an example my application (which is heavily logged) will successfully handle the 'not found' exeception multiples of time, continuing with the download process, and then inexplicably (to me) fails. What follows is the exception handling snippet: ## ------------------------------------------------------------------ except ftplib.error_perm, e: msg = "Couldn't get %s ---- %s" % (fname,str(sys.exc_info()[1])) log.add(msg) more-code-follows ## ------------------------------------------------------------------ When the code above is successful, the application simply logs the exception and continues. When the code above fails the traceback is as follows: ## ------------------------------------------------------------------ Traceback (most recent call last): <... traceback stack of program locations removed ...> error_perm: 550 file not found. ERROR MESSAGE: 550 file not found. ERROR TYPE: ## ------------------------------------------------------------------ Is there something that can improve my error handling process here so that my application safely and consistantly continues downloading so that the entire download process is completed? -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From rabidpoobear at gmail.com Thu May 27 01:16:36 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 26 May 2010 18:16:36 -0500 Subject: [Tutor] Successfully trapping the [505 file not found] error In-Reply-To: <20100526223222.GA4020@johnsons-web.com> References: <20100526223222.GA4020@johnsons-web.com> Message-ID: > > What follows is the exception handling snippet: > ## ------------------------------------------------------------------ > except ftplib.error_perm, e: > ? ?msg = "Couldn't get %s ---- %s" % (fname,str(sys.exc_info()[1])) > ? ? ? ?log.add(msg) > ? ? ? ?more-code-follows > ## ------------------------------------------------------------------ > When the code above is successful, the application simply > logs the exception and continues. > When the code above fails the traceback is as follows: > Are you sure you aren't doing anything with the ftp object in the "more code follows"? You are probably creating another error of the same type while processing your exception, so the second one doesn't get caught. like this (except with ftp obviously): try: f = open('in.txt') except IOError: y = open('log.txt', 'w') y.write('failed!') y.close() From tim at johnsons-web.com Thu May 27 01:44:31 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Wed, 26 May 2010 15:44:31 -0800 Subject: [Tutor] Successfully trapping the [505 file not found] error In-Reply-To: References: <20100526223222.GA4020@johnsons-web.com> Message-ID: <20100526234431.GB4020@johnsons-web.com> * Luke Paireepinart [100526 15:37]: > > Are you sure you aren't doing anything with the ftp object in the > "more code follows"? > You are probably creating another error of the same type while > processing your exception, so the second one doesn't get caught. :) Ain't it nice to have a second set of eyes! You are correct. I'm attempting to reconnect without the exception handling. Thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From nikunjbadjatya at gmail.com Thu May 27 09:05:51 2010 From: nikunjbadjatya at gmail.com (nikunj badjatya) Date: Thu, 27 May 2010 12:35:51 +0530 Subject: [Tutor] Program_to_catch_changes_in_webpage Message-ID: Hi All, I am actually using a web interface for outlook mails. Pointing the link to my browser opens a page which shows me all my mails. I had to refresh it to check for a new mail. Many a times I was very late to send a reply because of it. So a thought to make a script which will automatically refresh that web page, and tells to user for any new mails by popping a dialog or making a sound. * It should periodically check for any changes in that webpage . * If their is any, then notification should be sent to user. ( some dialog pop up, or some sound. ) Any idea as to how to proceed on this. ? OS - Linux ( CentOS 5.4) Browser - Firefox, Opera Thanks, Nikunj Badjatya Bangalore, India -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 27 09:50:51 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 27 May 2010 08:50:51 +0100 Subject: [Tutor] Program_to_catch_changes_in_webpage References: Message-ID: "nikunj badjatya" > I am actually using a web interface for outlook mails. > * It should periodically check for any changes in that webpage . > * If their is any, then notification should be sent to user. ( some > dialog > pop up, or some sound. ) Is it the standard Outlook Web Access(OWA) tool that comes with MS Exchange? If so you can adjust your settings to have both an audible and visual notification of new mail. > OS - Linux ( CentOS 5.4) > Browser - Firefox, Opera Although that might only work on IE under Windows... I've never tried on Linux although the basic GUI works OK there. > So a thought to make a script which will automatically refresh that > web > page, and tells to user for any new mails by popping a dialog or > making a > sound. Are you trying to write a script that talks to the web browser to refresh the page? Or are you looking to monitor the server? I suspect the second option is easier, but could make you unpopular with the admin team! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From nikunjbadjatya at gmail.com Thu May 27 10:19:28 2010 From: nikunjbadjatya at gmail.com (nikunj badjatya) Date: Thu, 27 May 2010 13:49:28 +0530 Subject: [Tutor] Program_to_catch_changes_in_webpage In-Reply-To: References: Message-ID: Hi, >Is it the standard Outlook Web Access(OWA) tool that >comes with MS Exchange? Yes. >Are you trying to write a script that talks to the web >browser to refresh the page? Or are you looking to monitor >the server? Yes, Talking to the web browser seems to be easiest way without notifying admin. Nikunj Badjatya BTech On Thu, May 27, 2010 at 1:20 PM, Alan Gauld wrote: > "nikunj badjatya" > > > I am actually using a web interface for outlook mails. >> > > * It should periodically check for any changes in that webpage . >> >> * If their is any, then notification should be sent to user. ( some >> dialog >> pop up, or some sound. ) >> > > Is it the standard Outlook Web Access(OWA) tool that > comes with MS Exchange? If so you can adjust your > settings to have both an audible and visual notification > of new mail. > > > OS - Linux ( CentOS 5.4) >> Browser - Firefox, Opera >> > > Although that might only work on IE under Windows... > I've never tried on Linux although the basic GUI works OK there. > > > So a thought to make a script which will automatically refresh that web >> page, and tells to user for any new mails by popping a dialog or making a >> sound. >> > > Are you trying to write a script that talks to the web > browser to refresh the page? Or are you looking to monitor > the server? I suspect the second option is easier, but could > make you unpopular with the admin team! > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Thu May 27 12:37:48 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Thu, 27 May 2010 11:37:48 +0100 Subject: [Tutor] PYTHON 3.1 In-Reply-To: <971282.32516.qm@web86708.mail.ird.yahoo.com> References: <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> <255402.45907.qm@web86702.mail.ird.yahoo.com> <971282.32516.qm@web86708.mail.ird.yahoo.com> Message-ID: Sir, Under the handling files topic, trying to compile the addressbook example you gave, am I to put them all in one file and save as address book? or each is a different module is saved in different files? I have attached for your correction what I did, please let me know if it is wrong and then point out my mistakes. I'll have a look at that topic again and make sure I fix whatever correction you point out for me. Thank You. On 5/25/10, ALAN GAULD wrote: > OK, That means Python has not set up your PATH environment variable. > This seems to be a common problem in recent releases, I think it is a > bug but the Pyton developers disagree! :-) > > You need to follow the instructions in the box in the Getting Started topic. > > The test of whether it works is to go to the folder with your scripts in > and just type python at the prompt. The usual >>> prompt should appear. > > As to your attachment here are some explanations: > >> C:\Documents and Settings\Administrator>cd\C:\Python31\Project >> The filename, directory name, or volume label syntax is incorrect. > > You put the \ in front of the drive again. > Also please type a space between the cd and the path, it makes > it much easier to read! > ie: > cd C:\Python31\Project > >> C:\Documents and Settings\Administrator>cd\Python31\Project > > This worked because the folder is on the same drive as the prompt, ie C. > >> C:\Python31\Project>var.py >> Access is denied. > > This is odd. Can you try two things: > > 1) type dir var.* (dir for *dir*ectory listing) > 2) assuming var.py is listed type attrib var.py > > Let me know what it says. > >> C:\Python31\Project>python var.py >> 'python' is not recognized as an internal or external command, >> operable program or batch file. > > This should have worked if the PATH environment was set up properly... > >> C:\Python31\Project>testingformat >> Access is denied. > > You need to specify the .py because Windows uses that to know > what program to use to execute the file - if its been set up properly! > >> C:\Python31\Project>python testingformat >> 'python' is not recognized as an internal or external command, >> operable program or batch file. > > Again, even if PATH is fixed you would still neeed the .py extension > >> C:\Python31\Project>cd\python31 > >> C:\Python31>python var.py >> this is a string.this string continues here. > > This worked so var.py is in your python31 folder.... Is there also > a copy in the Projects folder? > > You should move your Projects folder somewhere else though. > Because if you uninstall python 3.1 - for example when > version 3.2 comes out - the uninstaller will delete python31 > and everything under it - including your code! > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > > > > > > ________________________________ > From: Dipo Elegbede > To: ALAN GAULD > Sent: Tuesday, 25 May, 2010 9:06:39 > Subject: Re: PYTHON 3.1 > > got back to what you adviced Sir. > I did the cd to the file where the codes were saved and typed > but it replied python wasnot recognised. > I have attached the screen of the command prompt for you to see what i mean. > Thanks for your time. > > On 5/24/10, Dipo Elegbede wrote: >> I'll see to that again and do exactly as you have directed. >> Thanks and best regards, >> >> >> On 5/24/10, ALAN GAULD wrote: >>> OK, See my other email. >>> You should keep your code files in a completely separate >>> folder from Python so that if you ever upgrade Python >>> you don't lose all your code! >>> >>> The trick is to CD into the folder you store your code >>> not the folder where Python lives. >>> >>> Alan Gauld >>> Author of the Learn To Program website >>> http://www.alan-g.me.uk/ >>> >>> >>> >>> >>> >>> ________________________________ >>> From: Dipo Elegbede >>> To: ALAN GAULD >>> Sent: Monday, 24 May, 2010 14:48:32 >>> Subject: Re: PYTHON 3.1 >>> >>> Sir, I got it now. >>> I moved all my codes to the python directory. I initially had another >>> folder in the Python31 folder but now moved all to the Python31 folder >>> and it's running well on the command prompt. >>> Thanks. >>> I'll go ahead to read stdin and stdout again and see what I can make of >>> it. >>> Regards, >>> >>> On 5/24/10, Dipo Elegbede wrote: >>>> i have tried all that you advised, i still didn't run from the command >>>> prompt. >>>> >>>> Pls find attached the screen shot for various attempts I made. >>>> >>>> thanks. >>>> >>>> On 5/21/10, ALAN GAULD wrote: >>>>> I suspect the likely cause is that you need to change directory to the >>>>> directory(folder) where your code lives. Lets say you keep your code >>>>> in C:\Projects\Python >>>>> >>>>> start the DOS box >>>>> Type (the bits in bold): >>>>> >>>>> C\:..\> cd C:\Projects\Python >>>>> >>>>> Now type >>>>> >>>>> C:\Projects\Python>python myprogram.py >>>>> >>>>> using whatever your python file is called... >>>>> >>>>> Now, provided everything is set up properly it should run. >>>>> In fact you might even get away wioth jusdt typing: >>>>> >>>>> C:\Projects\Python\> myprogram.py >>>>> >>>>> Because in theory Windows should know to run python >>>>> for a .py file.... But it often "forgets" :-( >>>>> >>>>> >>>>> Alan Gauld >>>>> Author of the Learn To Program website >>>>> http://www.alan-g.me.uk/ >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> ________________________________ >>>>> From: Dipo Elegbede >>>>> To: ALAN GAULD >>>>> Sent: Friday, 21 May, 2010 19:35:57 >>>>> Subject: Re: PYTHON 3.1 >>>>> >>>>> I still can not successfully run Python from the windows command >>>>> prompt after doing all you've directed in the tutorials. (Pls note, >>>>> I'm not ruling out the probability that I didn't get the instructions >>>>> right. As a matter of fact, this is possibly why I still have the >>>>> problem.) >>>>> Sir, I'm hoping you could take me through that again. >>>>> Like when I type >>>>> C:document and setting>python xx.py >>>>> It tells me the file does not exit. >>>>> It atimes starts python when I type python at the prompt to give: >>>>> C:....Python> >>>>> When i type the name of the file at the python prompt, I still get an >>>>> error.e.g >>>>> Python>read.py >>>>> It comes with error. >>>>> It's challenging because some examples in your tutorial require I run >>>>> from the prompt and so I'm getting stucked midway. >>>>> Please Help. >>>>> Regards, >>>>> >>>>> On 5/21/10, Dipo Elegbede wrote: >>>>>> Hi Alan Sir, >>>>>> >>>>>> I have been reading through your site and the tutorials, it's just >>>>>> something else, I started feeling like a real programmer when i worked >>>>>> through the easygui thingy, it was really a mind-blower. >>>>>> I hope you'll pardon my pace of learning. I still didn't get some >>>>>> aspect under 'Conversing with the user' >>>>>> i really read through and I am hoping to reread but I would like you >>>>>> shed more light on the stdin and stdout areas. >>>>>> They are really confusing. >>>>>> Please help. >>>>>> Regards, >>>>>> >>>>>> On 5/20/10, Dipo Elegbede wrote: >>>>>>> Ok, Master. I should would have a lot to learn from you. >>>>>>> >>>>>>> I hope you'd oblige me that rare priviledge. >>>>>>> >>>>>>> Regards, Master! >>>>>>> >>>>>>> On 5/20/10, ALAN GAULD wrote: >>>>>>>> >>>>>>>> >>>>>>>>> I may consider pascal after excelling in Python. >>>>>>>> >>>>>>>> I wouldn't bother, the only place it is used nowadays is in the >>>>>>>> Borland Delphi programming tool for Windows(*). Delphi is very >>>>>>>> good if you already know Pascal but otherwise is just another >>>>>>>> language to learn! :-) >>>>>>>> >>>>>>>> (*)Although there is a freeware version of Pascal - fpc - that is >>>>>>>> compatible with Delphi if you really want to try it out. But >>>>>>>> definitely wait till after Python. (Actually Python is a good >>>>>>>> intro to Delphi, they have many features in common) >>>>>>>> >>>>>>>> Alan G. >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Elegbede Muhammed Oladipupo >>>>>>> OCA >>>>>>> +2348077682428 >>>>>>> +2347042171716 >>>>>>> www.dudupay.com >>>>>>> Mobile Banking Solutions | Transaction Processing | Enterprise >>>>>>> Application Development >>>>>>> >>>>>> >>>>>> >>>>>> -- >>>>>> Elegbede Muhammed Oladipupo >>>>>> OCA >>>>>> +2348077682428 >>>>>> +2347042171716 >>>>>> www.dudupay.com >>>>>> Mobile Banking Solutions | Transaction Processing | Enterprise >>>>>> Application Development >>>>>> >>>>> >>>>> -- >>>>> Sent from my mobile device >>>>> >>>>> Elegbede Muhammed Oladipupo >>>>> OCA >>>>> +2348077682428 >>>>> +2347042171716 >>>>> www.dudupay.com >>>>> Mobile Banking Solutions | Transaction Processing | Enterprise >>>>> Application Development >>>>> >>>> >>>> >>>> -- >>>> Elegbede Muhammed Oladipupo >>>> OCA >>>> +2348077682428 >>>> +2347042171716 >>>> www.dudupay.com >>>> Mobile Banking Solutions | Transaction Processing | Enterprise >>>> Application Development >>>> >>> >>> >>> -- >>> Elegbede Muhammed Oladipupo >>> OCA >>> +2348077682428 >>> +2347042171716 >>> www.dudupay.com >>> Mobile Banking Solutions | Transaction Processing | Enterprise >>> Application Development >>> >> >> -- >> Sent from my mobile device >> >> Elegbede Muhammed Oladipupo >> OCA >> +2348077682428 >> +2347042171716 >> www.dudupay.com >> Mobile Banking Solutions | Transaction Processing | Enterprise >> Application Development >> > > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- A non-text attachment was scrubbed... Name: addbk.py Type: application/octet-stream Size: 1587 bytes Desc: not available URL: From delegbede at dudupay.com Thu May 27 13:00:06 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Thu, 27 May 2010 12:00:06 +0100 Subject: [Tutor] Program_to_catch_changes_in_webpage In-Reply-To: References: Message-ID: i hope i'll one day get to this level in my programming quest. welldone all. On 5/27/10, nikunj badjatya wrote: > Hi, > >>Is it the standard Outlook Web Access(OWA) tool that >>comes with MS Exchange? > > Yes. > >>Are you trying to write a script that talks to the web >>browser to refresh the page? Or are you looking to monitor >>the server? > > Yes, Talking to the web browser seems to be easiest way without notifying > admin. > > > > Nikunj Badjatya > BTech > > > > > On Thu, May 27, 2010 at 1:20 PM, Alan Gauld > wrote: > >> "nikunj badjatya" >> >> >> I am actually using a web interface for outlook mails. >>> >> >> * It should periodically check for any changes in that webpage . >>> >>> * If their is any, then notification should be sent to user. ( some >>> dialog >>> pop up, or some sound. ) >>> >> >> Is it the standard Outlook Web Access(OWA) tool that >> comes with MS Exchange? If so you can adjust your >> settings to have both an audible and visual notification >> of new mail. >> >> >> OS - Linux ( CentOS 5.4) >>> Browser - Firefox, Opera >>> >> >> Although that might only work on IE under Windows... >> I've never tried on Linux although the basic GUI works OK there. >> >> >> So a thought to make a script which will automatically refresh that web >>> page, and tells to user for any new mails by popping a dialog or making a >>> sound. >>> >> >> Are you trying to write a script that talks to the web >> browser to refresh the page? Or are you looking to monitor >> the server? I suspect the second option is easier, but could >> make you unpopular with the admin team! >> >> HTH, >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From norman at khine.net Thu May 27 14:12:58 2010 From: norman at khine.net (Norman Khine) Date: Thu, 27 May 2010 14:12:58 +0200 Subject: [Tutor] copy directory from one part to another and preserve permissions Message-ID: hello, i have this code: if i want to copy the 'files' directory contents and preserve permissions is this the right way to do this? import shutil path_to_old_files_dir = '/var/www/dev.site.com/htdocs/files' path_to_new_files_dir = '/var/www/site.com/htdocs/sites/default/files' shutil.rmtree(path_to_new_files_dir) shutil.copytree(path_to_old_files_dir, path_to_new_files_dir) thanks norman -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From wescpy at gmail.com Thu May 27 19:47:49 2010 From: wescpy at gmail.com (wesley chun) Date: Thu, 27 May 2010 10:47:49 -0700 Subject: [Tutor] PYTHON 3.1 In-Reply-To: References: <706308.15726.qm@web86704.mail.ird.yahoo.com> <436826.15982.qm@web86704.mail.ird.yahoo.com> <255402.45907.qm@web86702.mail.ird.yahoo.com> <971282.32516.qm@web86708.mail.ird.yahoo.com> Message-ID: greetings and welcome (back) to Python! i have a few comments for you: 1. The syntax for Python 3.x has changed from 2.x, so please be aware of the differences as you are learning. Most books and code out there is still 2.x. 3.x is being adopted but because of the differences, it is slower than most version upgrades. also, 2.x is still going to be current for awhile... it has not been obsoleted yet. 2. Are you in Nigeria? if so, you may wish to attend the Nigerian stop of the Python Africa Tour: http://www.coactivate.org/projects/python-african-tour/blog/2010/04/26/preparing-our-3rd-stop-nigeria/ 3. Absolute Beginner CD: you asked for it last year -- http://www.nairaland.com/nigeria/topic-176782.64.html#msg3450555 -- but i guess no one has sent one to you yet? anyway, that edition is only for Python 2. there is now a 3rd edition that is in Python 3. you should be able to order it from your local technical bookstore, or you can buy it from Amazon... they ship to Africa. here is the link to the new edition: http://amazon.com/dp/1435455002 4. Put your reply to my email on the bottom and not on the top. I know it's not easy because you are on a mobile, but it makes things easier to read. Yes, it is the opposite way of doing it from regular business email. best regards, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From js at storta.net Thu May 27 20:45:25 2010 From: js at storta.net (John Storta Jr.) Date: Thu, 27 May 2010 14:45:25 -0400 Subject: [Tutor] Recent Documents Message-ID: <1274985925.11193.9.camel@aragorn> I am working on an app that runs in gnome and uses gtk. I am extremely new to Python. (been working with it for all of 2 days). I am wanting to add a 'Recent Documents' menu option to the app. Gnome tracks your recent documents -- apparently in a file called '.recently-used.xbel' in your home directory. So as to avoid reinventing the wheel, does anyone know of any examples of accessing the Gnome recently used file list from within Python? Or is there some samples of how to implement something independently? It seems like the type of thing that would be fairly common as part of integrating a Python app with Gnome. I cannot image every app maintains their own list of recently used docs when it is already stored within the desktop. If anyone has any ideas, please let me know. Thanks, John S. From joel.goldstick at gmail.com Thu May 27 21:22:02 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 27 May 2010 15:22:02 -0400 Subject: [Tutor] Recent Documents In-Reply-To: <1274985925.11193.9.camel@aragorn> References: <1274985925.11193.9.camel@aragorn> Message-ID: On Thu, May 27, 2010 at 2:45 PM, John Storta Jr. wrote: > I am working on an app that runs in gnome and uses gtk. I am extremely > new to Python. (been working with it for all of 2 days). > > I am wanting to add a 'Recent Documents' menu option to the app. Gnome > tracks your recent documents -- apparently in a file called > '.recently-used.xbel' in your home directory. > > So as to avoid reinventing the wheel, does anyone know of any examples > of accessing the Gnome recently used file list from within Python? Or > is there some samples of how to implement something independently? > > It seems like the type of thing that would be fairly common as part of > integrating a Python app with Gnome. I cannot image every app maintains > their own list of recently used docs when it is already stored within > the desktop. > > If anyone has any ideas, please let me know. > > > Thanks, > John S. > > I looked at the file. It is an xml file. There are several xml modules included with python that will help you to read this file. Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at pythontoo.com Thu May 27 21:34:13 2010 From: david at pythontoo.com (David Abbott) Date: Thu, 27 May 2010 15:34:13 -0400 Subject: [Tutor] Recent Documents In-Reply-To: <1274985925.11193.9.camel@aragorn> References: <1274985925.11193.9.camel@aragorn> Message-ID: On Thu, May 27, 2010 at 2:45 PM, John Storta Jr. wrote: > I am working on an app that runs in gnome and uses gtk. ?I am extremely > new to Python. ?(been working with it for all of 2 days). > > I am wanting to add a 'Recent Documents' menu option to the app. ?Gnome > tracks your recent documents -- apparently in a file called > '.recently-used.xbel' in your home directory. > > So as to avoid reinventing the wheel, does anyone know of any examples > of accessing the Gnome recently used file list from within Python? ?Or > is there some samples of how to implement something independently? If you go here [1] and download Deskbar-Applet 2.30.0, once extracted go to deskbar handlers and look at recent.py it may give you some ideas. class RecentHandler(deskbar.interfaces.Module): INFOS = {'icon': deskbar.core.Utils.load_icon('document-open-recent'), "name": _("Recent Documents"), "description": _("Retrieve your recently accessed files and locations"), "version": VERSION} [1] http://download.gnome.org/sources/deskbar-applet/2.30/deskbar-applet-2.30.1.tar.g -- David Abbott (dabbott) Gentoo http://dev.gentoo.org/~dabbott/ From denis.spir at gmail.com Thu May 27 21:49:42 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Thu, 27 May 2010 21:49:42 +0200 Subject: [Tutor] class methods: using class vars as args? In-Reply-To: References: Message-ID: <20100527214942.3533972e@o> On Sun, 23 May 2010 15:40:13 -0400 Alex Hall wrote: > Hello all, > I know Python reasonably well, but I still run into basic questions > which those over on the other python list request I post here instead. > I figure this would be one of them: > Why would this not work: > > class c(object): > def __init__(self, arg1, arg2): > self.arg1=arg1 > self.arg2=arg2 > > def doSomething(self, arg3=self.arg1): > ... > > The above results in an error that "name 'self' is not defined". Why > can I not set the default values of a method's arguments to class vars > like that? Thanks! Python gives you the answer (I'm not kidding): "name 'self' is not defined". "self" is not a magic name in python, unlike in some other languages, automagically receiving a value. It is just a name you can use for anything, like "self=1". A *convention* tells that this name is used for the *target* of a message. For instance: l = [3,1,2] l.sort() calls sort on the target l, which becomes the object on which sort applies. Right? Then, python magically (this is the only magic) adds l as first argument to the method sort of the list class, at call time. If sort were defined in python, its header could look like: def sort(target_list, compare_function=None): The convention requires "target_list" to be rather called "self". In your code, nowhere is a variable called "self" defined, before you try to use it. Since this happens in a class def, it should be first defined outside (probably in the global scope). If you write "self=1" somewhere before the class def, python will stop complaining about "self"... but it will complain about "arg1"! A solution for what I guess you're trying to do is: def doSomething(self, arg3=None): if arg3 is None then arg3=self.arg1 The reason is that your default value, not only is not a constant, but is a variable depending on the actual instance, at call time. The only handle available to address an instance is precisely as target of a method call. So, you can only define something (here a default value) that depends on the instance from *inside* a method body, where the handle exists (magically brought by python). Hope it's clear. Denis ________________________________ vit esse estrany ? spir.wikidot.com From breamoreboy at yahoo.co.uk Thu May 27 22:21:09 2010 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 27 May 2010 21:21:09 +0100 Subject: [Tutor] class methods: using class vars as args? In-Reply-To: References: Message-ID: On 23/05/2010 20:40, Alex Hall wrote: > Hello all, > I know Python reasonably well, but I still run into basic questions > which those over on the other python list request I post here instead. > I figure this would be one of them: > Why would this not work: > > class c(object): > def __init__(self, arg1, arg2): > self.arg1=arg1 > self.arg2=arg2 > > def doSomething(self, arg3=self.arg1): > ... > > The above results in an error that "name 'self' is not defined". Why > can I not set the default values of a method's arguments to class vars > like that? Thanks! > > You've already had some explanations as to what happens, but what are you trying to achieve? Why don't you forget about arg3 because it is arg1, which must exist by creating an instance of class c, or you wouldn't be able to call doSomething in the first place? HTH. Mark Lawrence From mehgcap at gmail.com Thu May 27 23:35:56 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 27 May 2010 17:35:56 -0400 Subject: [Tutor] Battleship grid not working how I expect Message-ID: Hi all, I very much hoped not to have to do this, but I have been staring at my code for two days and I just cannot see what is going on. http://www.gateway2somewhere.com/bs.zip has my code in it. You need wxPython to run it. speech.py has some pywin stuff, but that is only to interface with screen readers and is not important to most, probably all, of you. In board.py, you will want to find the speak method in the grid class and comment out the line "speech.speak(txt)", as well as commenting out "import speech" at the top of the file. The problem: I have a battleship grid, where each square is a wx.Button and the row letters and column numbers are wx.StaticTexts. Internally, I refer to coordinates as (x,y), so which column you are in followed by which row. Battleship uses (row,col) for coordinates, which is why anything to do with printing or reading coordinates may seem backwards. I want a grid with 10 rows and 14 columns or, in Battleship terms, columns 1-14 and rows a-j. Somewhere, or maybe several somewheres, something is getting switched around and I end up with columns 1-10 and rows a-n, but my movement function still allows me to move in a 10cx14r grid, not the other way around. I have added a lot of comments, but if something does not make sense, please ask me to clarify. Also, I only indent one space, and I use #closeing tags. This is because I use a screen reader, and it makes life a great deal easier (especially in Python) to do it this way. Thanks in advance for any light you can shed on this very frustrating problem! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From mehgcap at gmail.com Thu May 27 23:42:30 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 27 May 2010 17:42:30 -0400 Subject: [Tutor] class methods: using class vars as args? In-Reply-To: References: Message-ID: Thanks for all the explanations, everyone. This does make sense, and I am now using the if(arg==None): arg=self.arg idea. It only adds a couple lines, and is, if anything, more explicit than what I was doing before. On 5/27/10, Mark Lawrence wrote: > On 23/05/2010 20:40, Alex Hall wrote: >> Hello all, >> I know Python reasonably well, but I still run into basic questions >> which those over on the other python list request I post here instead. >> I figure this would be one of them: >> Why would this not work: >> >> class c(object): >> def __init__(self, arg1, arg2): >> self.arg1=arg1 >> self.arg2=arg2 >> >> def doSomething(self, arg3=self.arg1): >> ... >> >> The above results in an error that "name 'self' is not defined". Why >> can I not set the default values of a method's arguments to class vars >> like that? Thanks! >> >> > You've already had some explanations as to what happens, but what are > you trying to achieve? Why don't you forget about arg3 because it is > arg1, which must exist by creating an instance of class c, or you > wouldn't be able to call doSomething in the first place? > > HTH. > > Mark Lawrence > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From dperlman at wisc.edu Fri May 28 00:15:59 2010 From: dperlman at wisc.edu (David Perlman) Date: Thu, 27 May 2010 17:15:59 -0500 Subject: [Tutor] list of dicts <-> dict of lists? Message-ID: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Using the csv.DictReader and csv.DictWriter lets you read and write lists of dictionaries from files containing tabular data. I have a system that naturally generates tabular data in the form of a dictionary of lists: the dictionary key is the name of the column, and then the value is a list which contains the data for that column. Sort of like a spreadsheet. I would like to use csv.DictWriter to write out this data but that requires a list of dicts. I can convert the dict of lists to a list of dicts, and thus make it compatible with csv.DictWriter, with the following ugly comprehensions: >>> y {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} >>> [dict([(i,y[i][j]) for i in y.keys()]) for j in range(len(y[y.keys()[0]]))] [{'a': 1, 'c': 7, 'b': 4}, {'a': 2, 'c': 8, 'b': 5}, {'a': 3, 'c': 9, 'b': 6}] ...but I'm wondering if anyone knows of a more elegant way, perhaps something built-in suited for this purpose... I am aware that any solution will only work if the lists in the dict are all the same length. :) Thanks! -- -dave---------------------------------------------------------------- "Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth." -Dr. Steven Hyman, Harvard From woodm1979 at gmail.com Fri May 28 01:19:20 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Thu, 27 May 2010 17:19:20 -0600 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: #!/usr/bin/env python Here's my best attempt.? I'm not sure if it's "simpler" than yours, but for me it seems a bit cleaner.? Then again, I LOVE the zip operator, and the '*' operator too.? :-)? Whenever I see a "transpose this" type problem, I think zip. y = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} print y old = [dict([(i, y[i][j]) for i in y.keys()]) ????????????????????????? for j in range(len(y[y.keys()[0]]))] print old keys, values = zip(* y.items()) new = [dict([(i, j) for i, j in zip(keys, column)]) ??????????????????? for column in zip(* values)] print new print new == old I BELIEVE there's some new cool features in 2.6 or maybe 3.0 where non-simultaneous access to my_dict.keys() and my_dict.values() will keep them "paired up", but I don't know the details. If you skipped the upper line (setting keys and values) you'd be accessing y.keys() 3 times (in this example). I'm not sure if you're guaranteed to have the order remain the same for all three accesses. If that's the case, I'd change the code to be: # This line isn't necessary #keys, values = zip(* y.items()) new = [dict([(i, j) for i, j in zip(y.keys(), column)]) for column in zip(* y.values())] or even # This line isn't necessary #keys, values = zip(* y.items()) new = [dict([(i, j) for i, j in zip(y, column)]) for column in zip(* y.values())] But since I'm a coward, and I'd like my code to run on older versions of python too, I'd leave the initial step. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Thu, May 27, 2010 at 4:15 PM, David Perlman wrote: > > Using the csv.DictReader and csv.DictWriter lets you read and write lists of dictionaries from files containing tabular data. ?I have a system that naturally generates tabular data in the form of a dictionary of lists: the dictionary key is the name of the column, and then the value is a list which contains the data for that column. ?Sort of like a spreadsheet. ?I would like to use csv.DictWriter to write out this data but that requires a list of dicts. > > I can convert the dict of lists to a list of dicts, and thus make it compatible with csv.DictWriter, with the following ugly comprehensions: > > >>> y > {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} > >>> [dict([(i,y[i][j]) for i in y.keys()]) for j in range(len(y[y.keys()[0]]))] > [{'a': 1, 'c': 7, 'b': 4}, {'a': 2, 'c': 8, 'b': 5}, {'a': 3, 'c': 9, 'b': 6}] > > ...but I'm wondering if anyone knows of a more elegant way, perhaps something built-in suited for this purpose... > > I am aware that any solution will only work if the lists in the dict are all the same length. ?:) > > Thanks! > > > -- > -dave---------------------------------------------------------------- > "Pseudo-colored pictures of a person's brain lighting up are > undoubtedly more persuasive than a pattern of squiggles produced by a > polygraph. ?That could be a big problem if the goal is to get to the > truth." ?-Dr. Steven Hyman, Harvard > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From breamoreboy at yahoo.co.uk Fri May 28 01:29:04 2010 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 28 May 2010 00:29:04 +0100 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: I confess that I don't like top posting :) Please see below. On 28/05/2010 00:19, Matthew Wood wrote: > #!/usr/bin/env python > > > Here's my best attempt. I'm not sure if it's "simpler" than yours, > but for me it seems a bit cleaner. Then again, I LOVE the zip > operator, and the '*' operator too. :-) Whenever I see a "transpose > this" type problem, I think zip. > > > y = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} > > print y > > old = [dict([(i, y[i][j]) for i in y.keys()]) > for j in range(len(y[y.keys()[0]]))] > print old > > > keys, values = zip(* y.items()) > new = [dict([(i, j) for i, j in zip(keys, column)]) > for column in zip(* values)] > > print new > > print new == old > > I BELIEVE there's some new cool features in 2.6 or maybe 3.0 where > non-simultaneous access to my_dict.keys() and my_dict.values() will > keep them "paired up", but I don't know the details. If you skipped > the upper line (setting keys and values) you'd be accessing y.keys() 3 > times (in this example). I'm not sure if you're guaranteed to have > the order remain the same for all three accesses. If that's the case, > I'd change the code to be: > > # This line isn't necessary #keys, values = zip(* y.items()) > new = [dict([(i, j) for i, j in zip(y.keys(), column)]) > for column in zip(* y.values())] > > or even > > # This line isn't necessary #keys, values = zip(* y.items()) > new = [dict([(i, j) for i, j in zip(y, column)]) > for column in zip(* y.values())] > > RTFM? :) From the Python docs for 2.6.5 "items() Return a copy of the dictionary?s list of (key, value) pairs. CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary?s history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()). The same relationship holds for the iterkeys() and itervalues() methods: pairs = zip(d.itervalues(), d.iterkeys()) provides the same value for pairs. Another way to create the same list is pairs = [(v, k) for (k, v) in d.iteritems()]." > But since I'm a coward, and I'd like my code to run on older versions > of python too, I'd leave the initial step. > -- > > I enjoy haiku > but sometimes they don't make sense; > refrigerator? > > > On Thu, May 27, 2010 at 4:15 PM, David Perlman wrote: >> >> Using the csv.DictReader and csv.DictWriter lets you read and write lists of dictionaries from files containing tabular data. I have a system that naturally generates tabular data in the form of a dictionary of lists: the dictionary key is the name of the column, and then the value is a list which contains the data for that column. Sort of like a spreadsheet. I would like to use csv.DictWriter to write out this data but that requires a list of dicts. >> >> I can convert the dict of lists to a list of dicts, and thus make it compatible with csv.DictWriter, with the following ugly comprehensions: >> >>>>> y >> {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} >>>>> [dict([(i,y[i][j]) for i in y.keys()]) for j in range(len(y[y.keys()[0]]))] >> [{'a': 1, 'c': 7, 'b': 4}, {'a': 2, 'c': 8, 'b': 5}, {'a': 3, 'c': 9, 'b': 6}] >> >> ...but I'm wondering if anyone knows of a more elegant way, perhaps something built-in suited for this purpose... >> >> I am aware that any solution will only work if the lists in the dict are all the same length. :) >> >> Thanks! >> >> >> -- >> -dave---------------------------------------------------------------- >> "Pseudo-colored pictures of a person's brain lighting up are >> undoubtedly more persuasive than a pattern of squiggles produced by a >> polygraph. That could be a big problem if the goal is to get to the >> truth." -Dr. Steven Hyman, Harvard >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From woodm1979 at gmail.com Fri May 28 01:44:36 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Thu, 27 May 2010 17:44:36 -0600 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: Well, that makes a lot of sense. I probably should have looked it up. :-) That said, the version with an extra line will work on python < 2.6, so I'd probably just leave it that way. But thanks the docs pointer. Always useful. That said, if I KNEW that my software was only to be implemented in 3.0 or greater, I'd go with the new, cool, slick, shows-I-read-the-docs way! From kagebatsu at cox.net Thu May 27 06:25:45 2010 From: kagebatsu at cox.net (kagebatsu) Date: Wed, 26 May 2010 21:25:45 -0700 (PDT) Subject: [Tutor] NLTK needs YAML? In-Reply-To: References: Message-ID: <28689370.post@talk.nabble.com> Michael Scharf-6 wrote: > > $ sudo python setup.py install > > Traceback (most recent call last): > File "setup.py", line 13, in > import nltk > File "/private/tmp/nltk-installer/nltk/__init__.py", line 92, in > > from yamltags import * > File "/private/tmp/nltk-installer/nltk/yamltags.py", line 1, in > import yaml > ImportError: No module named yaml > I'm having the *exact* same issue. Has anyone helped you solve this? I'm emailing a professor at my school about this ...maybe he can help if neither of us has figured it out? also, when i do: sudo port install py26-nltk i get: Unable to execute port: can't read "build.cmd": Failed to locate 'make' in path: '/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin' or at its MacPorts configuration time location, did you move it? It doesn't seem like you're having that issue, though. I am =/ Brandon ps. sorry this reply wasn't an answer. -- View this message in context: http://old.nabble.com/-Tutor--NLTK-needs-YAML--tp28348409p28689370.html Sent from the Python - tutor mailing list archive at Nabble.com. From t.gkikopoulos at dundee.ac.uk Thu May 27 11:37:20 2010 From: t.gkikopoulos at dundee.ac.uk (trias) Date: Thu, 27 May 2010 02:37:20 -0700 (PDT) Subject: [Tutor] Speed it up... Message-ID: <28691677.post@talk.nabble.com> Hi, I have wrote the following lines that work fine, but are quite slow, are there any obvious things I can consider to speed things up? Thanks import MySQLdb import scipy import csv dbtest=MySQLdb.connect(host="***",user="***",passwd="***") cursor=dbtest.cursor() cursor.execute("""SELECT tfs_name FROM tfs_sites GROUP by tfs_name""") result=cursor.fetchall() dbtest.close() TFname=[] for row in result: TFname.append(row[0]) del result T={} i=0 for TF in TFname: while i<1: dbtest=MySQLdb.connect(host="***",user="***",passwd="***",db="***") cursor=dbtest.cursor() cursor.execute("""SELECT tfs_chr,tfs_pos,tfs_val FROM tfs_sites WHERE tfs_name='%s'"""%(TF)) result=cursor.fetchall() TFchr=[] TFpos=[] TFval=[] i+=1 for row in result: TFchr.append(row[0]) TFpos.append(row[1]) TFval.append(row[2]) TFc=[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] counter=0 for TFsite in TFchr: TFc[(int(TFsite)-1)].append((int(TFpos[counter]),int(TFval[counter]))) T[TF]=TFc counter+=1 -- View this message in context: http://old.nabble.com/Speed-it-up...-tp28691677p28691677.html Sent from the Python - tutor mailing list archive at Nabble.com. From woodm1979 at gmail.com Fri May 28 02:18:20 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Thu, 27 May 2010 18:18:20 -0600 Subject: [Tutor] Speed it up... In-Reply-To: <28691677.post@talk.nabble.com> References: <28691677.post@talk.nabble.com> Message-ID: On Thu, May 27, 2010 at 3:37 AM, trias wrote: > > Hi, > > ?I have wrote the following lines that work fine, but are quite slow, are > there any obvious things I can consider to speed things up? > > ?Thanks > > import MySQLdb > > import scipy > > import csv > > dbtest=MySQLdb.connect(host="***",user="***",passwd="***") > > cursor=dbtest.cursor() > > cursor.execute("""SELECT tfs_name FROM tfs_sites GROUP by tfs_name""") > > result=cursor.fetchall() > > dbtest.close() > > TFname=[] > > for row in result: > > ? ?TFname.append(row[0]) > > del result > > T={} > > i=0 > > for TF in TFname: > > ? ?while i<1: > > ? ? ? ?dbtest=MySQLdb.connect(host="***",user="***",passwd="***",db="***") > > ? ? ? ?cursor=dbtest.cursor() > > ? ? ? ?cursor.execute("""SELECT tfs_chr,tfs_pos,tfs_val FROM tfs_sites > WHERE tfs_name='%s'"""%(TF)) > > ? ? ? ?result=cursor.fetchall() > > ? ? ? ?TFchr=[] > > ? ? ? ?TFpos=[] > > ? ? ? ?TFval=[] > > ? ? ? ?i+=1 > > ? ? ? ?for row in result: > > ? ? ? ? ? ?TFchr.append(row[0]) > > ? ? ? ? ? ?TFpos.append(row[1]) > > ? ? ? ? ? ?TFval.append(row[2]) > > ? ? ? ? ? ?TFc=[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]] > > ? ? ? ? ? ?counter=0 > > ? ? ? ? ? ?for TFsite in TFchr: > > > TFc[(int(TFsite)-1)].append((int(TFpos[counter]),int(TFval[counter]))) > > ? ? ? ? ? ? ? ?T[TF]=TFc > > ? ? ? ? ? ? ? ?counter+=1 > -- > View this message in context: http://old.nabble.com/Speed-it-up...-tp28691677p28691677.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Heh, I guess I'll try the "replay after message" plan everyone seems to like here. You don't need to make a new connection or cursor every time you run a query. Just use the cursor you created once. Secondly, if you can figure out a way to combine all your queries into one larger query, your speed will increase. In this case, you're querying the same table multiple times. It'd be easier on your database if you just queried it once. SELECT tfs_name, tfs_chr, tfs_pos, tfs_val FROM tfs_sites You'll still get all the information you'd get the other way, and you only have to hit the DB once. (This somewhat invalidates the first point I made, but ...) If you can't reduce it to that single query (for whatever reason), since your first query is only grabbing a single column of data, you should use "distinct" instead of "group by". SELECT DISTINCT tfs_name FROM tfs_sites Also, and this will be a FAR SMALLER speedup: you have several instances where you do this: big_container = [] for row in some_query_result: big_container.append(row[0]) it's faster, and I think clearer/cleaner to do: big_container = [row[0] for row in some_query_result] or, if you want to make sure they're all ints: big_container = [int(row[0]) for row in some_query_result] TFname, TFchr, TFpos, TFval can all be constructed this way. There's a few other things you could do that would speed up things, but the biggest would be to reduce the connection time to your DB, and reduce the number of queries. -- I enjoy haiku but sometimes they don't make sense; refrigerator? From steve at pearwood.info Fri May 28 02:51:01 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 28 May 2010 10:51:01 +1000 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: <201005281051.01942.steve@pearwood.info> On Fri, 28 May 2010 09:19:20 am Matthew Wood wrote: > I BELIEVE there's some new cool features in 2.6 or maybe 3.0 where > non-simultaneous access to my_dict.keys() and my_dict.values() will > keep them "paired up", but I don't know the details. This is not a new feature, but a very old feature. It became a guaranteed promise of the language in Python 2.0: http://docs.python.org/release/2.0/lib/typesmapping.html and worked at least back to Python 1.5: [steve at sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} >>> # mess with the dict a little bit ... d['a'] = -1 >>> d['f'] = -1 >>> del d['a'] >>> d['f'] = 6 >>> d['a'] = 1 >>> print d {'f': 6, 'd': 4, 'e': 5, 'b': 2, 'c': 3, 'a': 1} >>> d.keys() ['f', 'd', 'e', 'b', 'c', 'a'] >>> d.values() [6, 4, 5, 2, 3, 1] Since this is guaranteed, your comment: > # This line isn't necessary #keys, values = zip(* y.items()) ... > But since I'm a coward, and I'd like my code to run on older versions > of python too, I'd leave the initial step. is unfortunately cargo-cult programming (programming without understanding what you are doing). This sounds bad, and it is, but we've all been there and done that, so don't take offence. Firstly, it's not necessary, and secondly, even if it was necessary, it won't work in older versions: >>> keys, items = zip(*d.items()) File "", line 1 keys, items = zip(*d.items()) ^ SyntaxError: invalid syntax So drop it. You either trust Python, or you don't, and if you don't, you can't trust it to do *anything*. (If d.keys() and d.values() are buggy, how do you know that zip() or d.items() aren't buggy too?) The exception to this is if you are handling non-dict mappings that don't make promises about keeping d.keys() and d.values() aligned in order. And that requires a comment to show why you're doing what otherwise would seen to be unnecessary work: # Support badly-behaved mappings like mymodule.stupid_dict. keys, items = zip(*d.items()) ... rather than: # Appease the Python gods so they give us cargo and not bugs. keys, values = zip(d.items()) ... See the difference between cargo cult programming and defensive programming? -- Steven D'Aprano From steve at pearwood.info Fri May 28 02:56:09 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 28 May 2010 10:56:09 +1000 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: <201005281056.09886.steve@pearwood.info> On Fri, 28 May 2010 09:44:36 am Matthew Wood wrote: > That said, the version with an extra line will work on python < 2.6, > so I'd probably just leave it that way. Why? That's like saying: "I could write y = x+2 in Python, but y = 1+x+1 will work too, so I'll write that instead, just in case." Yes, I suppose that there are buggy Python implementations where x+2 doesn't work correctly but 1+x+1 does, and there might be stupid data types that are the same, but do you really need to support such badly-behaved objects? -- Steven D'Aprano From steve at pearwood.info Fri May 28 03:10:41 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 28 May 2010 11:10:41 +1000 Subject: [Tutor] copy directory from one part to another and preserve permissions In-Reply-To: References: Message-ID: <201005281110.41416.steve@pearwood.info> On Thu, 27 May 2010 10:12:58 pm Norman Khine wrote: > hello, > i have this code: > > if i want to copy the 'files' directory contents and preserve > permissions is this the right way to do this? > > import shutil > path_to_old_files_dir = '/var/www/dev.site.com/htdocs/files' > path_to_new_files_dir = > '/var/www/site.com/htdocs/sites/default/files' > > shutil.rmtree(path_to_new_files_dir) > shutil.copytree(path_to_old_files_dir, path_to_new_files_dir) Seems right to me, although I must admit I haven't tested it thoroughly. You might want to wrap the call to rmtree in a try...except block in case path_to_new_files doesn't exist. -- Steven D'Aprano From woodm1979 at gmail.com Fri May 28 04:00:46 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Thu, 27 May 2010 20:00:46 -0600 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <201005281056.09886.steve@pearwood.info> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> Message-ID: Wow. Something horrible happened here. http://xkcd.com/386/ I THOUGHT the guaranteed same-ordering of dict.keys and dict.values started in python 2.6. That was a simple mistake. It turns out, that's not the case. But in general, access to dicts and sets is unordered, so you can't/don't/shouldn't count on ordering. The solution to take keys and values from dict.items() DOES guarantee their ordering, even if dict.keys and dict.values aren't. That's all I was trying to say. It's unfortunate that I had the python version the guaranteed same-ordering wrong. I'm certainly not trying to say that you shouldn't trust language features. That's not it at all. But imagine if that guaranteed behavior started in 2.5 for example. Then, if you want your code to work on 2.3, you'd definitely want to pull them out of the dict via dict.items(). I think your response was quite rude. I mean really, cargo cult programming? May John Frum forgive your unnecessary aggression. I just tried to suggest a solution and I think it's crappy that you accused me of "programming without understanding what you are doing". I recognize that you told me not to take offense; but, no offense, nobody cared when Carrie Prejean (Miss California) said that either. So, I do apologize for the mistake I made, and hopefully we (both you AND I) can nip this mailing-list flame-war in the bud. Anyway, I hope David (the original questioner) got a little help, or at least another token for confirmation that any list comprehension solution will be semi-ugly/semi-complex. -- I enjoy haiku but sometimes they don't make sense; refrigerator? -------------- next part -------------- An HTML attachment was scrubbed... URL: From timomlists at gmail.com Fri May 28 08:58:53 2010 From: timomlists at gmail.com (Timo) Date: Fri, 28 May 2010 08:58:53 +0200 Subject: [Tutor] Recent Documents In-Reply-To: <1274985925.11193.9.camel@aragorn> References: <1274985925.11193.9.camel@aragorn> Message-ID: <4BFF69AD.8040605@gmail.com> On 27-05-10 20:45, John Storta Jr. wrote: > I am working on an app that runs in gnome and uses gtk. I am extremely > new to Python. (been working with it for all of 2 days). > > I am wanting to add a 'Recent Documents' menu option to the app. Gnome > tracks your recent documents -- apparently in a file called > '.recently-used.xbel' in your home directory. > > So as to avoid reinventing the wheel, does anyone know of any examples > of accessing the Gnome recently used file list from within Python? Or > is there some samples of how to implement something independently? > > It seems like the type of thing that would be fairly common as part of > integrating a Python app with Gnome. I cannot image every app maintains > their own list of recently used docs when it is already stored within > the desktop. > > If anyone has any ideas, please let me know. > PyGTK has some widgets that deal with that. See the PyGTK reference manual and see the widgets that start with "Recent*". Cheers, Timo > > Thanks, > John S. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Fri May 28 09:33:24 2010 From: __peter__ at web.de (Peter Otten) Date: Fri, 28 May 2010 09:33:24 +0200 Subject: [Tutor] list of dicts <-> dict of lists? References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: David Perlman wrote: > Using the csv.DictReader and csv.DictWriter lets you read and write > lists of dictionaries from files containing tabular data. I have a > system that naturally generates tabular data in the form of a > dictionary of lists: the dictionary key is the name of the column, and > then the value is a list which contains the data for that column. > Sort of like a spreadsheet. I would like to use csv.DictWriter to > write out this data but that requires a list of dicts. > > I can convert the dict of lists to a list of dicts, and thus make it > compatible with csv.DictWriter, with the following ugly comprehensions: > > >>> y > {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} > >>> [dict([(i,y[i][j]) for i in y.keys()]) for j in > range(len(y[y.keys()[0]]))] > [{'a': 1, 'c': 7, 'b': 4}, {'a': 2, 'c': 8, 'b': 5}, {'a': 3, 'c': 9, > 'b': 6}] > > ...but I'm wondering if anyone knows of a more elegant way, perhaps > something built-in suited for this purpose... > > I am aware that any solution will only work if the lists in the dict > are all the same length. :) I think it's simpler and therefore more appropriate to use a normal csv.writer here: >>> import csv >>> import sys >>> data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} >>> writer = csv.writer(sys.stdout) >>> writer.writerow(data.keys()) a,c,b >>> writer.writerows(zip(*data.values())) 1,7,4 2,8,5 3,9,6 Peter From denis.spir at gmail.com Fri May 28 11:09:02 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Fri, 28 May 2010 11:09:02 +0200 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> Message-ID: <20100528110902.3401e2a7@o> On Thu, 27 May 2010 20:00:46 -0600 Matthew Wood wrote: > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values started > in python 2.6. That was a simple mistake. > > It turns out, that's not the case. But in general, access to dicts and sets > is unordered, so you can't/don't/shouldn't count on ordering. The solution > to take keys and values from dict.items() DOES guarantee their ordering, > even if dict.keys and dict.values aren't. The word "order" is a bit over-used :-) Python's guarantee is that the *output* orders of keys() & value() match each other. Say, they're consistent // sequences. This is a different feature from preserving *input* order of of keys, or of key:value pairs. (Which is not true is Python or Lua, for instance, but in recent Ruby versions, yes: http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/.) Denis ________________________________ vit esse estrany ? spir.wikidot.com From sander.sweers at gmail.com Fri May 28 11:32:59 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 28 May 2010 11:32:59 +0200 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <20100528110902.3401e2a7@o> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> <20100528110902.3401e2a7@o> Message-ID: 2010/5/28 spir ? : > his is a different feature from preserving *input* order of of keys, or of key:value pairs. In Python 2.7 and 3.1 [1] we now have the OrderedDict which does preserve input order. Greets Sander [1] http://www.python.org/dev/peps/pep-0372/ From dperlman at wisc.edu Fri May 28 21:13:48 2010 From: dperlman at wisc.edu (David Perlman) Date: Fri, 28 May 2010 14:13:48 -0500 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: <18A77726-B6CD-4057-9974-3C5B0BD3D183@wisc.edu> Aha, now this is the clever solution that I didn't find "outside the box". :) On May 28, 2010, at 2:33 AM, Peter Otten wrote: > I think it's simpler and therefore more appropriate to use a normal > csv.writer here: > >>>> import csv >>>> import sys >>>> data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} >>>> writer = csv.writer(sys.stdout) >>>> writer.writerow(data.keys()) > a,c,b >>>> writer.writerows(zip(*data.values())) > 1,7,4 > 2,8,5 > 3,9,6 > > Peter -- -dave---------------------------------------------------------------- "Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth." -Dr. Steven Hyman, Harvard From dperlman at wisc.edu Fri May 28 21:27:06 2010 From: dperlman at wisc.edu (David Perlman) Date: Fri, 28 May 2010 14:27:06 -0500 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> Message-ID: <40F06AF4-2DE8-462C-ABE9-2CAF9006454E@wisc.edu> Oh, except one problem: the csv.DictWriter lets you tell it what order you want the columns output in. With your version, they just show up in whatever order Python wants them. On May 28, 2010, at 2:33 AM, Peter Otten wrote: > I think it's simpler and therefore more appropriate to use a normal > csv.writer here: > >>>> import csv >>>> import sys >>>> data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} >>>> writer = csv.writer(sys.stdout) >>>> writer.writerow(data.keys()) > a,c,b >>>> writer.writerows(zip(*data.values())) > 1,7,4 > 2,8,5 > 3,9,6 -- -dave---------------------------------------------------------------- "Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth." -Dr. Steven Hyman, Harvard From mehgcap at gmail.com Fri May 28 23:06:30 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 28 May 2010 17:06:30 -0400 Subject: [Tutor] disregard Battleship post (for now) Message-ID: Hi all, A couple days ago I posted a request for help with a strange problem with my Battleship game. I finally double-checked the constructor for a wx.GridSizer and, to my surprise, found that I had reversed the column/row args in my call to said constructor. I will have to confirm with a sighted person, but I believe that switching those two args and updating the loop that populates the sizer has since fixed the problem. If not I will be writing back, but for now assume I am all set. Thanks anyway, though. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Fri May 28 23:11:09 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 28 May 2010 23:11:09 +0200 Subject: [Tutor] disregard Battleship post (for now) In-Reply-To: References: Message-ID: On Fri, May 28, 2010 at 11:06 PM, Alex Hall wrote: > Hi all, > A couple days ago I posted a request for help with a strange problem > with my Battleship game. I finally double-checked the constructor for > a wx.GridSizer and, to my surprise, found that I had reversed the > column/row args in my call to said constructor. I will have to confirm > with a sighted person, but I believe that switching those two args and > updating the loop that populates the sizer has since fixed the > problem. If not I will be writing back, but for now assume I am all > set. Thanks anyway, though. > This mail came around right about 10 seconds after I finally got around to downloading that zip file ;) Ah well, glad you solved it. It was bound to be something like this. Hugo From mehgcap at gmail.com Fri May 28 23:13:50 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 28 May 2010 17:13:50 -0400 Subject: [Tutor] disregard Battleship post (for now) In-Reply-To: References: Message-ID: On 5/28/10, Hugo Arts wrote: > On Fri, May 28, 2010 at 11:06 PM, Alex Hall wrote: >> Hi all, >> A couple days ago I posted a request for help with a strange problem >> with my Battleship game. I finally double-checked the constructor for >> a wx.GridSizer and, to my surprise, found that I had reversed the >> column/row args in my call to said constructor. I will have to confirm >> with a sighted person, but I believe that switching those two args and >> updating the loop that populates the sizer has since fixed the >> problem. If not I will be writing back, but for now assume I am all >> set. Thanks anyway, though. >> > > This mail came around right about 10 seconds after I finally got > around to downloading that zip file ;) Good timing, then; at least you did not spend time unnecessarily searching for a bug I had found. Thanks anyway, though! > Ah well, glad you solved it. It was bound to be something like this. Mee, too. It seems that the most frustrating bugs are invariably the smallest mistakes. > > Hugo > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From __peter__ at web.de Fri May 28 23:33:54 2010 From: __peter__ at web.de (Peter Otten) Date: Fri, 28 May 2010 23:33:54 +0200 Subject: [Tutor] list of dicts <-> dict of lists? References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <40F06AF4-2DE8-462C-ABE9-2CAF9006454E@wisc.edu> Message-ID: David Perlman wrote: > Oh, except one problem: the csv.DictWriter lets you tell it what order > you want the columns output in. With your version, they just show up > in whatever order Python wants them. That's not hard to fix: >>> fieldnames = "abca" >>> cols = [data[fn] for fn in fieldnames] >>> writer.writerows(zip(*cols)) 1,4,7,1 2,5,8,2 3,6,9,3 Peter From shawnblazer94 at gmail.com Sat May 29 00:09:14 2010 From: shawnblazer94 at gmail.com (Shawn Blazer) Date: Fri, 28 May 2010 18:09:14 -0400 Subject: [Tutor] Recursion - Beginner Message-ID: Hello! I'm a high school student, and I'm having some trouble learning recursion in my class... For example: Trace the sequence of recursive calls that glee(2,1) spawns: def glee ( idol , scrub ) : if idol == 0 : return scrub elif idol < 0 : return scrub + glee ( idol + 10 , idol % 3 ) else : return scrub + glee ( idol - scrub , idol % 3 ) Also, I'm not really sure what a question like this is asking... >>> getLeaves ( jenny ) [5,3,0,9] >>> >>> getLeaves ( joshua ) [15,17,19,11,13] I know its really simple, but I'm really new to this and I'd really appreciate some help. Thanks! -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ From emile at fenx.com Sat May 29 00:23:53 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 28 May 2010 15:23:53 -0700 Subject: [Tutor] Recursion - Beginner In-Reply-To: References: Message-ID: On 5/28/2010 3:09 PM Shawn Blazer said... > Hello! I'm a high school student, and I'm having some trouble learning > recursion in my class... > For example: > > Trace the sequence of recursive calls that glee(2,1) spawns: I imagine what you'd need to do is manually follow the code for glee step-by-step when the values (2,1) are passed into it, so I'd write something like: -pass---idol----scrub--1stReturn--2ndReturn--3rdReturn 1 2 1 n/a n/a pass2 2 1 2 n/a n/a pass3 3 -1 1 and continue until the the recursion completes. > > def glee ( idol , scrub ) : > if idol == 0 : > return scrub > elif idol < 0 : > return scrub + glee ( idol + 10 , idol % 3 ) > else : > return scrub + glee ( idol - scrub , idol % 3 ) > > Also, I'm not really sure what a question like this is asking... > To answer that you'd need the code for getLeaves, which isn't here. It must have been provided to you somewhere... Emile >>>> getLeaves ( jenny ) > [5,3,0,9] >>>> >>>> getLeaves ( joshua ) > [15,17,19,11,13] > > I know its really simple, but I'm really new to this and I'd really > appreciate some help. > Thanks! > From shawnblazer94 at gmail.com Sat May 29 01:11:13 2010 From: shawnblazer94 at gmail.com (Shawn Blazer) Date: Fri, 28 May 2010 19:11:13 -0400 Subject: [Tutor] Homework Problem Message-ID: This problem told me to use map and filter, so how would I use that to solve it? >>> remove ( 5 , 6 ) 6 >>> remove ( 5 , 5 ) >>> remove ( 1 , [1 , [1 , [2 , 13]] , 1 , [2] , 5] ) [[[2 , 13]] , [2] , 5] >>> >>> remove ( 2 , [1 , [1 , [2 , 13]] , 1 , [2] , 5] ) [1 , [1 , [13]] , 1 , [] , 5] Thanks! -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ From alan.gauld at btinternet.com Sat May 29 01:23:15 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 29 May 2010 00:23:15 +0100 Subject: [Tutor] Recursion - Beginner References: Message-ID: "Shawn Blazer" wrote > Hello! I'm a high school student, and I'm having some trouble > learning recursion in my class... > For example: > > Trace the sequence of recursive calls that glee(2,1) spawns: > > def glee ( idol , scrub ) : > if idol == 0 : > return scrub > elif idol < 0 : > return scrub + glee ( idol + 10 , idol % 3 ) > else : > return scrub + glee ( idol - scrub , idol % 3 ) I'd start with a print statement right at the top of glee. Something like print "glee( idol=", idol,", scrub=", scrub, ")" That will show you what haoppens with each call of glee. >From there its a short step to tracing the return values etc. > Also, I'm not really sure what a question like this is asking... > >>>> getLeaves ( jenny ) > [5,3,0,9] >>>> >>>> getLeaves ( joshua ) > [15,17,19,11,13] Me neither - I don't even see a question?! BTW My tutorial has a topic on recursion, that might help... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sat May 29 02:55:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 29 May 2010 10:55:47 +1000 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> Message-ID: <201005291055.47958.steve@pearwood.info> On Fri, 28 May 2010 12:00:46 pm Matthew Wood wrote: > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values > started in python 2.6. That was a simple mistake. > > It turns out, that's not the case. But in general, access to dicts > and sets is unordered, so you can't/don't/shouldn't count on > ordering. You can't count on getting a *specific* order, but you can count on getting a *consistent* order. (So long as you don't modify the dictionary between calls, of course.) > The solution to take keys and values from dict.items() > DOES guarantee their ordering, even if dict.keys and dict.values > aren't. And so does zip(d.keys(), d.values()). They both guarantee the same consistent ordering. The fact is, yes, your solution does work, but your rationale for preferring it is irrational. That's not meant to be insulting, we all have preferences based on irrational little quirks, we wouldn't be human otherwise. When there are two equally good solutions, you have to pick one or the other for essentially an irrational reason -- if there was a rational reason to prefer one over the other, they wouldn't be equally good. If you choose to continue using the dict.items() solution, by all means do so because you like it, or because it gives you a warm fuzzy feeling, or because it's easier for you to remember. Or because you've profiled it and it is 3% faster or uses 1% less memory (I made those numbers up, by the way). These are all good reasons for choosing a solution over another solution. But stop trying to justify it on the basis of it being safer and more backwards compatible, because that's simply not correct. That's what pushes your solution out of personal preference to cargo-cult programming: following the form without understanding the semantics. The semantics of dict.keys() and values() guarantee the same order. [...] > But imagine if that guaranteed behavior started in 2.5 for example. > Then, if you want your code to work on 2.3, you'd definitely want to > pull them out of the dict via dict.items(). But it didn't start in 2.5. It has been part of Python essentially forever. If I argued, "Imagine that dictionaries only gained an items() method in 2.5, and you wanted it to work in 2.3, you'd need to avoid dict.items()", what would you say? > I think your response was quite rude. If you can't take constructive criticism without getting offended and crying "oh how rude!", there's a serious problem. > I mean really, cargo cult programming? > I just tried to suggest a solution and I think it's crappy that you > accused me of "programming without understanding what you are doing". I think it is quite clear that in *this* specific case you don't understand what you are doing, because you are recommending a solution that you labelled "This line isn't necessary". If it's not necessary, why include it? We all code badly at times. I'm sure if you were to go through my code line by line, you'd find some real clangers caused by me failing to fully understand what I was doing too. Patches and bug reports are welcome :) If it makes you feel any better, I was once told by Alex Martelli (one of the Python demi-gods) that if he were marking my code for an assignment he would fail me over what I believed was a trivial stylistic difference of opinion. I was declaring globals even if I didn't assign to them, e.g.: def func(x): global y return x + y It took me a long time, perhaps a few years, but I've come around to Martelli's position on globals and no longer declare them unless I assign to them. I still think a fail over such a small issue is awfully harsh, but perhaps he was having a bad day. I've come to understand the semantics of the global statement better, and can see that unnecessary global declarations goes against the purpose and meaning of the statement. I was, in short, cargo-cult programming, using global without understanding it. As harsh as Martelli's response was, I believe I'm a better coder today because of it than if he had just patted me on the head and said "that's okay, you write anything you like". -- Steven D'Aprano From steve at pearwood.info Sat May 29 03:01:10 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 29 May 2010 11:01:10 +1000 Subject: [Tutor] class methods: using class vars as args? In-Reply-To: References: Message-ID: <201005291101.11168.steve@pearwood.info> On Fri, 28 May 2010 07:42:30 am Alex Hall wrote: > Thanks for all the explanations, everyone. This does make sense, and > I am now using the > if(arg==None): arg=self.arg > idea. It only adds a couple lines, and is, if anything, more explicit > than what I was doing before. You should use "if arg is None" rather than an equality test. In this case, you are using None as a sentinel value. That is, you want your test to pass only if you actually receive None as an argument, not merely something that is equal to None. Using "arg is None" as the test clearly indicates your intention: The value None, and no other value, is the sentinel triggering special behaviour while the equality test is potentially subject to false positives, e.g. if somebody calls your code but passes it something like this: class EqualsEverything: def __eq__(self, other): return True instead of None. -- Steven D'Aprano From janssonks at gmail.com Sat May 29 03:17:37 2010 From: janssonks at gmail.com (Karl Jansson) Date: Fri, 28 May 2010 20:17:37 -0500 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <201005291055.47958.steve@pearwood.info> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> <201005291055.47958.steve@pearwood.info> Message-ID: <222D7869-3735-492F-916C-9D8C31CAC7B1@gmail.com> I was trying to build python, and this printed to the terminal: Python build finished, but the necessary bits to build these modules were not found: _gdbm ossaudiodev readline spwd To find the necessary bits, look in setup.py in detect_modules() for the module's name. I'm new to python, so i don't know if this is important, or what it means at all. I looked in setup.py, and it didn't tell me anything. What does it mean by "the necessary bits" were not found? From rabidpoobear at gmail.com Sat May 29 03:33:07 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 28 May 2010 20:33:07 -0500 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <222D7869-3735-492F-916C-9D8C31CAC7B1@gmail.com> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> <201005291055.47958.steve@pearwood.info> <222D7869-3735-492F-916C-9D8C31CAC7B1@gmail.com> Message-ID: > I'm new to python, so i don't know if this is important, or what it means at all. ?I looked in setup.py, and it didn't tell me anything. ?What does it mean by "the necessary bits" were not found? Not really sure, but in the future please create a new e-mail to tutor at python.org rather than hijacking a thread. And don't just hit "reply" and delete the contents of a different e-mail, you have to create an entirely new message. Otherwise your message will get threaded with that other message's thread in certain e-mail clients. From hugo.yoshi at gmail.com Sat May 29 03:43:06 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 29 May 2010 03:43:06 +0200 Subject: [Tutor] python build issues (missing modules) Message-ID: First of all, don't reply to an e-mail unrelated to your own problem, create a new thread. The subject line is very confusing, and it also screws up thread-based mail readers like gmail. On Sat, May 29, 2010 at 3:17 AM, Karl Jansson wrote: > I was trying to build python, and this printed to the terminal: > > Python build finished, but the necessary bits to build these modules were not found: > _gdbm ? ? ? ? ? ? ?ossaudiodev ? ? ? ?readline > spwd These are a few python modules part of the standard library. gdbm are python bindings to the GNU dbm library (a simple database), ossaudiodev provides low-level access to oss audio devices, readline provides advanced line-editing capabilities, and spwd gives access to the UNIX shadow password file. > To find the necessary bits, look in setup.py in detect_modules() for the module's name. > > I'm new to python, so i don't know if this is important, or what it means at all. ?I looked in setup.py, and it didn't tell me anything. ?What does it mean by "the necessary bits" were not found? The libraries are written in C, and depend on external libraries. The build system could not find all of the required files to build them. That doesn't mean the build failed, you'll have a working python, but you'll be unable to use these modules in your python code. Most of these libraries are not terribly important (YMMV), but not having the readline module might also affect behaviour of the interactive interpreter (not sure on this one). To fix it, you'll need to get the required files. Most linux distros will have some kind of separate development package that has the files. On ubuntu, for example, you might try something akin to this: $ sudo apt-get install build-essential libgdm-dev libreadline5-dev That should get you the files needed to compile at least the readline and _gdbm files. as for hunting down the others, checking the function named in the error message might provide a clue as to what's needed. Hugo From denis.spir at gmail.com Sat May 29 09:03:36 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sat, 29 May 2010 09:03:36 +0200 Subject: [Tutor] Homework Problem In-Reply-To: References: Message-ID: <20100529090336.53f32c47@o> On Fri, 28 May 2010 19:11:13 -0400 "Shawn Blazer" wrote: > > This problem told me to use map and filter, so how would I use that to > solve it? [some piece of interactive session] > Thanks! So, where's the problem? Denis ________________________________ vit esse estrany ? spir.wikidot.com From woodm1979 at gmail.com Sat May 29 09:24:12 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Sat, 29 May 2010 01:24:12 -0600 Subject: [Tutor] list of dicts <-> dict of lists? In-Reply-To: <201005291055.47958.steve@pearwood.info> References: <10C89DA0-FD91-4A5D-B3D4-11F4373DEA51@wisc.edu> <201005281056.09886.steve@pearwood.info> <201005291055.47958.steve@pearwood.info> Message-ID: On Fri, May 28, 2010 at 6:55 PM, Steven D'Aprano wrote: > On Fri, 28 May 2010 12:00:46 pm Matthew Wood wrote: > > > I THOUGHT the guaranteed same-ordering of dict.keys and dict.values > > started in python 2.6. That was a simple mistake. > > > > It turns out, that's not the case. But in general, access to dicts > > and sets is unordered, so you can't/don't/shouldn't count on > > ordering. > > You can't count on getting a *specific* order, but you can count on > getting a *consistent* order. (So long as you don't modify the > dictionary between calls, of course.) > > > > The solution to take keys and values from dict.items() > > DOES guarantee their ordering, even if dict.keys and dict.values > > aren't. > > And so does zip(d.keys(), d.values()). They both guarantee the same > consistent ordering. > > The fact is, yes, your solution does work, but your rationale for > preferring it is irrational. That's not meant to be insulting, we all > have preferences based on irrational little quirks, we wouldn't be > human otherwise. When there are two equally good solutions, you have to > pick one or the other for essentially an irrational reason -- if there > was a rational reason to prefer one over the other, they wouldn't be > equally good. > > If you choose to continue using the dict.items() solution, by all means > do so because you like it, or because it gives you a warm fuzzy > feeling, or because it's easier for you to remember. Or because you've > profiled it and it is 3% faster or uses 1% less memory (I made those > numbers up, by the way). These are all good reasons for choosing a > solution over another solution. > > But stop trying to justify it on the basis of it being safer and more > backwards compatible, because that's simply not correct. That's what > pushes your solution out of personal preference to cargo-cult > programming: following the form without understanding the semantics. > The semantics of dict.keys() and values() guarantee the same order. > > > [...] > > But imagine if that guaranteed behavior started in 2.5 for example. > > Then, if you want your code to work on 2.3, you'd definitely want to > > pull them out of the dict via dict.items(). > > But it didn't start in 2.5. It has been part of Python essentially > forever. If I argued, "Imagine that dictionaries only gained an items() > method in 2.5, and you wanted it to work in 2.3, you'd need to avoid > dict.items()", what would you say? > > > > I think your response was quite rude. > > If you can't take constructive criticism without getting offended and > crying "oh how rude!", there's a serious problem. > > > > I mean really, cargo cult programming? > > I just tried to suggest a solution and I think it's crappy that you > > accused me of "programming without understanding what you are doing". > > I think it is quite clear that in *this* specific case you don't > understand what you are doing, because you are recommending a solution > that you labelled "This line isn't necessary". If it's not necessary, > why include it? > > We all code badly at times. I'm sure if you were to go through my code > line by line, you'd find some real clangers caused by me failing to > fully understand what I was doing too. Patches and bug reports are > welcome :) > > If it makes you feel any better, I was once told by Alex Martelli (one > of the Python demi-gods) that if he were marking my code for an > assignment he would fail me over what I believed was a trivial > stylistic difference of opinion. I was declaring globals even if I > didn't assign to them, e.g.: > > def func(x): > global y > return x + y > > It took me a long time, perhaps a few years, but I've come around to > Martelli's position on globals and no longer declare them unless I > assign to them. I still think a fail over such a small issue is awfully > harsh, but perhaps he was having a bad day. I've come to understand the > semantics of the global statement better, and can see that unnecessary > global declarations goes against the purpose and meaning of the > statement. I was, in short, cargo-cult programming, using global > without understanding it. As harsh as Martelli's response was, I > believe I'm a better coder today because of it than if he had just > patted me on the head and said "that's okay, you write anything you > like". > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Well, since it appears that my offer to nip the email flame-war in the bud was declined, I'll reluctantly respond. And as name dropping Alex Martelli seems the order of the day, I'll go ahead and share my experience with him as well: He decided to put my code into the python cook book (for those that demand citations: Python Cookbook, 1st edition, recipe 2.7, page 49). He did so while making suggestions on how my code could be improved in many ways, including efficiency and correctness. He made those suggestions in a very professional, and classy manner, as can be seen in the text of the book. That's all I'm really asking for here. I'm 100% sure I made a version-reference mistake. I'm also 100% sure you recognized that I made a version-reference mistake. Neither of those facts are in dispute here. In fact, it's very important, in the context of a tutoring medium like this email list, that someone point out the mistakes made by others. In other words, constructive criticism is extremely important and very appropriate here, and indeed most every situation. I both give and receive constructive criticism on a daily if not hourly basis. But your response was neither professional, nor classy. At the very least, it was over the top. Nowhere in Alex's response to me did he insult me, accuse me of "cargo-cult programming" or "programming without understanding what I'm doing", nor use any other insulting/diminishing phrase. He simply mentioned the ways my code could/should be improved, and showed a better example. In conclusion, I've remembered the "dress, sort, undress" idiom for sorting data by attribute ever since. So yes, even when someone is classy, professional, and dare I say nice, messages are be learned. I hope you have a great weekend. -- Matthew Wood I enjoy haiku but sometimes they don't make sense; refrigerator? -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Sat May 29 10:29:43 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sat, 29 May 2010 10:29:43 +0200 Subject: [Tutor] SENTINEL, & more Message-ID: <20100529102943.05ff6a18@o> Hello, from the thread: "class methods: using class vars as args?" On Sat, 29 May 2010 11:01:10 +1000 Steven D'Aprano wrote: > On Fri, 28 May 2010 07:42:30 am Alex Hall wrote: > > Thanks for all the explanations, everyone. This does make sense, and > > I am now using the > > if(arg==None): arg=self.arg > > idea. It only adds a couple lines, and is, if anything, more explicit > > than what I was doing before. > > You should use "if arg is None" rather than an equality test. > > In this case, you are using None as a sentinel value. That is, you want > your test to pass only if you actually receive None as an argument, not > merely something that is equal to None. > > Using "arg is None" as the test clearly indicates your intention: > > The value None, and no other value, is the sentinel triggering special > behaviour > > while the equality test is potentially subject to false positives, e.g. > if somebody calls your code but passes it something like this: > > class EqualsEverything: > def __eq__(self, other): > return True > > instead of None. I'll try to clarify the purpose and use of sentinels with an example. Please, advanced programmers correct me. A point is that, in languages like python, sentinels are under-used, because everybody tends to une None instead, or as all-purpose sentinel. Imagine you're designing a kind of database of books; with a user interface to enter new data. What happens when an author is unknown? A proper way, I guess, to cope with this case, is to define a sentinel object, eg: UNKNOWN_AUTHOR = Object() There are many ways to define a sentinel; one could have defined "=0" or "=False" or whatever. But this choice is simple, clear, and secure because a custom object in python will only compare equal to itself -- by default. Sentinels are commonly written upercase because they are constant, predefined, elements. Say, when users have to deal with an unknown author, they press a special button or enter a special valuen, eg '*', the software then silently converts to UNKNOWN_AUTHOR. Now, cases of unknown authors all are *marked* with the same mark UNKNOWN_AUTHOR; this mark only happens in this very case, thus only means this. In other words, this is a clear & safe *semantic* mark. Later, when the application operates on data, it can compare the value stored in the "author" field, to catch the special mark case UNKNOWN_AUTHOR. Eg class Book(Object): ... AUTHOR_DEFAULT_TEXT = "" def write(self): ... if self.author is UNKNOWN_AUTHOR: author_text = Book.AUTHOR_DEFAULT_TEXT ... Hope I'm clear. In the very case of UNKNOWN_AUTHOR, it would hardly have any consequence to use "==", instead of "is", as relational operator for comparison. Because, as said above, by default, custom objects only compare equal to themselves in python. But * This default behaviour can be overriden, as shown by Steven above. * Using "is" clarifies your intent to the reader, including yourself. * Not all languages make a difference between "==" and "is". (Actually, very few do it.) Good habits... === additional stuff -- more personal reflexion -- critics welcome === Sentinels belong to a wider category of programming elements, or objects, I call "marks". (Conventional term for this notion welcome.) Marks are elements that play a role in a programmer's model, but have no value. What is the value of NOVICE_MODE for a game? of the SPADE card suit? of the character '?'? These are notions, meaning semantic values, that must exist in an application but have no "natural" value -- since they are not values semantically, unlike a position or a color. In C, on could use a preprocessor flag for this: #define NOVICE_MODE ... #ifdef NOVICE_MODE ... #endif NOVICE_MODE is here like a value-less symbol in the program: precisely what we mean. But not all languages have such features. (Indeed, there is a value behind the scene, but it is not accessible to the programmer; so, the semantics is correct.) Thus, we need to _arbitrarily_ assign marks values. Commonly, natural numbers are used for that: they are called "nominals" (--> http://en.wikipedia.org/wiki/Nominal_number) precisely because they act like symbol names for things that have no value. The case of characters is typical: that '?' is represented by 248 is just arbitrary; we just need something, and software can only deal with values; so, we need a value. the only subset of a character set that is not arbitrarily ordered is precisely the suite of digits: because they are used to compose ordinals, which themselves form the archetype of every order. In the case of card suits, I could define an independant mark for each suit. But the 4 of them also build a whole, namely the set of card suits. For such a notion, some languages introduce a builtin feature; for instance Pascal has "enumerations" for this (http://en.wikipedia.org/wiki/Enumeration_%28programming%29): var suit : (clubs, diamonds, hearts, spades); A side-advantage of a nominal enumeration is that, each mark silently mapping to an ordinal number, marks happen to be ordered. Then, it's possible to compare them for inequality like in the game of bridge: clubs Message-ID: "Shawn Blazer" wrote > This problem told me to use map and filter, so how would I use that > to solve it? Because its homework we won't solve it for you, we will only answer questions or suggest approaches. >From your earlier post it looks like you have all the tools: recursion, map and filter. Now what do you not understand? What have you tried? FWIW My tutorial covers map and filter in the "Functional Programming" topic. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From lie.1296 at gmail.com Sat May 29 20:24:22 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 30 May 2010 04:24:22 +1000 Subject: [Tutor] SENTINEL, & more In-Reply-To: <20100529102943.05ff6a18@o> References: <20100529102943.05ff6a18@o> Message-ID: On 05/29/10 18:29, spir ? wrote: > Hello, > > > from the thread: "class methods: using class vars as args?" > > On Sat, 29 May 2010 11:01:10 +1000 Steven D'Aprano > wrote: > >> On Fri, 28 May 2010 07:42:30 am Alex Hall wrote: >>> Thanks for all the explanations, everyone. This does make sense, >>> and I am now using the if(arg==None): arg=self.arg idea. It only >>> adds a couple lines, and is, if anything, more explicit than what >>> I was doing before. >> >> You should use "if arg is None" rather than an equality test. >> >> In this case, you are using None as a sentinel value. That is, you >> want your test to pass only if you actually receive None as an >> argument, not merely something that is equal to None. >> >> Using "arg is None" as the test clearly indicates your intention: >> >> The value None, and no other value, is the sentinel triggering >> special behaviour >> >> while the equality test is potentially subject to false positives, >> e.g. if somebody calls your code but passes it something like >> this: >> >> class EqualsEverything: def __eq__(self, other): return True >> >> instead of None. > > I'll try to clarify the purpose and use of sentinels with an example. > Please, advanced programmers correct me. A point is that, in > languages like python, sentinels are under-used, because everybody > tends to une None instead, or as all-purpose sentinel. Sentinels are underused not because everyone uses None, but because in many cases sentinels can be dangerous if not explicitly checked. In many cases, python prefers Exceptions (e.g. for-loop iteration) to sentinels. > Imagine you're designing a kind of database of books; with a user > interface to enter new data. What happens when an author is unknown? > A proper way, I guess, to cope with this case, is to define a > sentinel object, eg: UNKNOWN_AUTHOR = Object() There are many ways to > define a sentinel; one could have defined "=0" or "=False" or > whatever. But this choice is simple, clear, and secure because a > custom object in python will only compare equal to itself -- by > default. Sentinels are commonly written upercase because they are > constant, predefined, elements. In this case, I would prefer an unknown author to be an empty string (i.e. "") because using object() does not persist between serialization to the database (not to mention having to special-case it everywhere, with empty string, you only need to special case whenever you need to). > Hope I'm clear. In the very case of UNKNOWN_AUTHOR, it would hardly > have any consequence to use "==", instead of "is", as relational > operator for comparison. Because, as said above, by default, custom > objects only compare equal to themselves in python. But * This > default behaviour can be overriden, as shown by Steven above. * Using > "is" clarifies your intent to the reader, including yourself. * Not > all languages make a difference between "==" and "is". (Actually, > very few do it.) Good habits... > > > > === additional stuff -- more personal reflexion -- critics welcome > === > > Sentinels belong to a wider category of programming elements, or > objects, I call "marks". (Conventional term for this notion welcome.) > Marks are elements that play a role in a programmer's model, but have > no value. What is the value of NOVICE_MODE for a game? of the SPADE > card suit? of the character '?'? These are notions, meaning semantic > values, that must exist in an application but have no "natural" value > -- since they are not values semantically, unlike a position or a > color. What *is* "value"? Is there any difference between "semantic value" and "natural value"? IMHO, there is no difference, "numerical value" is only a subset of all "value". > In C, on could use a preprocessor flag for this: #define > NOVICE_MODE ... #ifdef NOVICE_MODE ... #endif NOVICE_MODE is here > like a value-less symbol in the program: precisely what we mean. But > not all languages have such features. (Indeed, there is a value > behind the scene, but it is not accessible to the programmer; so, the > semantics is correct.) > > Thus, we need to _arbitrarily_ assign marks values. Commonly, natural > numbers are used for that: they are called "nominals" (--> > http://en.wikipedia.org/wiki/Nominal_number) precisely because they > act like symbol names for things that have no value. The case of > characters is typical: that '?' is represented by 248 is just > arbitrary; we just need something, and software can only deal with > values; Digital computers can only deal with "natural numbers" (i.e. {0, 1, 2, 3, ...}), that's why we need to encode all values as natural numbers. integers maps nicely to natural number (0:0, 1:1, -1:2, 2:3, -2:4, 3:5, -3:6, 4:7, -4:8, ...). Everything has a value, but the question of whether such value is representable in a computer is equivalent to asking whether the value is representable as integers, or in other words, whether the "cardinality" of the set of all such possible values is less than or equal to the "cardinality" of the set of all integers. In cases where the value is not representable in integers (such as the case of real numbers), then in many practical situation, we make do with a subset of the possible values and approximate the rest (e.g. 'float' type is an encoding of subset of real numbers into integers, 'string' is an encoding of text/stream into a list of integers). The set of problems that is solvable by a digital computer is dependant on whether the problem can be encoded into a Countable Set http://en.wikipedia.org/wiki/Countable_set > An interesting exercise is to define, in and for python, practicle > types for isolated marks (sentinels), mark sequences (enumerations), > and mark sets. See: http://code.activestate.com/recipes/413486/ From mehgcap at gmail.com Sat May 29 21:49:45 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sat, 29 May 2010 15:49:45 -0400 Subject: [Tutor] class methods as static methods? Message-ID: Hi all, In Battleship, I have a weapons.py file, currently with just one missile type (a Harpoon anti-ship missile). This Harpoon class defines a getImpactCoords method, which returns all coordinates on the map that it will hit. I would like to not instantiate a Harpoon object, just call the Harpoon's getImpactCoords method and pass it the required arguments. Is this possible? Thanks. Sorry if I got the terms backwards in the subject; I can never remember which is static and which is non-static. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From lie.1296 at gmail.com Sun May 30 00:32:13 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 30 May 2010 08:32:13 +1000 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: On 05/30/10 05:49, Alex Hall wrote: > Hi all, > In Battleship, I have a weapons.py file, currently with just one > missile type (a Harpoon anti-ship missile). This Harpoon class defines > a getImpactCoords method, which returns all coordinates on the map > that it will hit. I would like to not instantiate a Harpoon object, > just call the Harpoon's getImpactCoords method and pass it the > required arguments. Is this possible? Thanks. Sorry if I got the terms > backwards in the subject; I can never remember which is static and > which is non-static. > Yes you can make it a static method or class method: class Harpoon(object): @staticmethod def inst(a, b, c): print a, b, c @classmethod def cmeth(cls, a, b, c): print cls print a, b, c Harpoon.inst(1, 2, 3) Harpoon.cmeth(1, 2, 3) the question is, why would you want to? getImpactCoords() doesn't seem to be a function that makes sense without a missile instance (I may be mistaken). From eike.welk at gmx.net Sun May 30 00:36:24 2010 From: eike.welk at gmx.net (Eike Welk) Date: Sun, 30 May 2010 00:36:24 +0200 Subject: [Tutor] SENTINEL, & more Message-ID: <201005300036.24493.eike.welk@gmx.net> Hey Denis! On Saturday May 29 2010 10:29:43 spir ? wrote: > I'll try to clarify the purpose and use of sentinels with an example. > Please, advanced programmers correct me. A point is that, in languages > like python, sentinels are under-used, because everybody tends to une None > instead, or as all-purpose sentinel. > > Imagine you're designing a kind of database of books; with a user interface > to enter new data. What happens when an author is unknown? A proper way, I > guess, to cope with this case, is to define a sentinel object, eg: > UNKNOWN_AUTHOR = Object() > There are many ways to define a sentinel; one could have defined "=0" or > "=False" or whatever. But this choice is simple, clear, and secure because > a custom object in python will only compare equal to itself -- by default. > Sentinels are commonly written upercase because they are constant, > predefined, elements. I waited for a thread like this to appear, because I have a quirky, but IMHO elegant, solution for those kinds of variables: class EnumMeta(type): def __repr__(self): return self.__name__ class Enum(object): __metaclass__ = EnumMeta Objects are created by inheriting from the Enum class. (Not by instantiating it.) >>> class EAST(Enum): pass >>> class WEST(Enum): pass >>> class NORTH(Enum): pass >>> class SOUTH(Enum): pass The objects know their name, and when printed their name is printed. In this respect they behave similarly to None. >>> print NORTH, SOUTH, EAST, WEST NORTH SOUTH EAST WEST I call the class Enum, but this is certainly the wrong term since the values are not enumerated. But is Sentinel the right term for something like this? I thought a sentinel is a soldier who guards something. Hello English native speakers! What is a good name? Eike. P.S. By the way Denis, an earlier thread from you on the subject got me thinking about it. From breamoreboy at yahoo.co.uk Sun May 30 02:04:20 2010 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 30 May 2010 01:04:20 +0100 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: On 29/05/2010 20:49, Alex Hall wrote: > Hi all, > In Battleship, I have a weapons.py file, currently with just one > missile type (a Harpoon anti-ship missile). This Harpoon class defines > a getImpactCoords method, which returns all coordinates on the map > that it will hit. I would like to not instantiate a Harpoon object, > just call the Harpoon's getImpactCoords method and pass it the > required arguments. Is this possible? Thanks. Sorry if I got the terms > backwards in the subject; I can never remember which is static and > which is non-static. > Hi Alex, See you're still going for it :) I think that you're trying to build a Yamoto/Musashi before you've built a raft from oil drums or whatever :) If I'm wrong, I'll apologise here and now. For a really great introduction to Python, I suggest diveintopython, it's what got me going eight years ago. Kindest regards. Mark Lawrence. From mehgcap at gmail.com Sun May 30 03:50:22 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sat, 29 May 2010 21:50:22 -0400 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: On 5/29/10, Mark Lawrence wrote: > On 29/05/2010 20:49, Alex Hall wrote: >> Hi all, >> In Battleship, I have a weapons.py file, currently with just one >> missile type (a Harpoon anti-ship missile). This Harpoon class defines >> a getImpactCoords method, which returns all coordinates on the map >> that it will hit. I would like to not instantiate a Harpoon object, >> just call the Harpoon's getImpactCoords method and pass it the >> required arguments. Is this possible? Thanks. Sorry if I got the terms >> backwards in the subject; I can never remember which is static and >> which is non-static. >> > > Hi Alex, > > See you're still going for it :) > > I think that you're trying to build a Yamoto/Musashi before you've built > a raft from oil drums or whatever :) If I'm wrong, I'll apologise here > and now. I have built one app in Python and have experience in Java and Javascript, as well as some in PHP; in fact, I am going into my fourth year of college for a computer science degree in September. While they have not done as much programming as I would like, I have had enough that I can find the commonalities between languages and generally know what I am looking for (make this public, turn that into a class instead of an independent collection of vars...) That said, I have no professional experience programming and do only assigned problems and hobby-level programming. My Screenless Widgets app is nearing beta testing and works to my satisfaction, but I am sure there is much I could do to improve it. Still, everyone has to start somewhere... I say all this not to express any offense at your message - believe me, none taken - but rather to tell everyone just where I am coming from. > > For a really great introduction to Python, I suggest diveintopython, > it's what got me going eight years ago. I feel that I understand the basics; what I am running into are things that crop up and I learn them as needed; if I learn a concept but then never use it, I will forget it, or mix it up with a similar comcept in another language, so I generally attack things by reading intro tutorials, modifying them, and then continuing from there until I feel that I can start my own project from scratch and figure out the pieces as I go along. > > Kindest regards. > > Mark Lawrence. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From steve at pearwood.info Sun May 30 04:04:05 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 30 May 2010 12:04:05 +1000 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: <201005301204.07183.steve@pearwood.info> On Sun, 30 May 2010 05:49:45 am Alex Hall wrote: > Hi all, > In Battleship, I have a weapons.py file, currently with just one > missile type (a Harpoon anti-ship missile). This Harpoon class > defines a getImpactCoords method, which returns all coordinates on > the map that it will hit. I would like to not instantiate a Harpoon > object, just call the Harpoon's getImpactCoords method and pass it > the required arguments. I don't understand the logic here. Surely the impact coordinates depends on the individual missile (an instance)? > Is this possible? Thanks. Sorry if I got the > terms backwards in the subject; I can never remember which is static > and which is non-static. In Python terminology, a static method is an ordinary function that is called as a method from either a class or a class instance: class C(object): @staticmethod def method(x): return "Called with argument %s", % x Note that the method does not receive an automatic instance argument (usually called self) when you call it. A class method is like an ordinary method, except instead of receiving the instance (self) as the first argument, it receives the class (usually called cls): class C(object): @classmethod def method(cls, x): return "Called from class %s with argument %s", % (cls, x) The method always receives the class, regardless of whether you call it from the class using C.method(x) or from an instance C().method(x). You might also be interested in what I call "dualmethod", which passes the class as first argument if you call it from the class, and the instance if you call it from the instance: http://code.activestate.com/recipes/577030-dualmethod-descriptor/ -- Steven D'Aprano From steve at pearwood.info Sun May 30 04:04:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 30 May 2010 12:04:56 +1000 Subject: [Tutor] SENTINEL, & more In-Reply-To: <20100529102943.05ff6a18@o> References: <20100529102943.05ff6a18@o> Message-ID: <201005301204.56401.steve@pearwood.info> On Sat, 29 May 2010 06:29:43 pm spir ? wrote: > I'll try to clarify the purpose and use of sentinels with an example. > Please, advanced programmers correct me. A point is that, in > languages like python, sentinels are under-used, because everybody > tends to une None instead, or as all-purpose sentinel. But None is a perfectly good sentinel, and it is very common! So how can you say that sentinels are underused? Strictly speaking, a sentinel is a guard value in an array, string, list or other sequence. See for example: http://en.wikipedia.org/wiki/Sentinel_value In this sense, sentinels are rare in Python because sequences usually know their own length and so you don't need a special sentinel to mark the end of the sequence. But I extend the term sentinel to mean any special value passed as an argument as a signal or mark. In this sense, sentinels are very common, and the most common sentinel is None. Other useful sentinels are the empty string, 0 and -1, and of course you can create your own unique sentinels using object(). > Imagine you're designing a kind of database of books; with a user > interface to enter new data. What happens when an author is unknown? The obvious way is to use the empty string for unknown or no author. This avoids needing any special checks, since the empty string is a perfectly good string, and if you need special processing, you can do so very simply: def print_book(record): if record.title: print "Title: %s" % record.title if record.author: print "Author: %s" % record.author If you want to distinguish between unknown and no author, the obvious solution is to pass "?" as unknown and "" for no author. -- Steven D'Aprano From breamoreboy at yahoo.co.uk Sun May 30 05:08:29 2010 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 30 May 2010 04:08:29 +0100 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: Hi Alex, thanks for the response, please see below. On 30/05/2010 02:50, Alex Hall wrote: > On 5/29/10, Mark Lawrence wrote: >> On 29/05/2010 20:49, Alex Hall wrote: >>> Hi all, >>> In Battleship, I have a weapons.py file, currently with just one >>> missile type (a Harpoon anti-ship missile). This Harpoon class defines >>> a getImpactCoords method, which returns all coordinates on the map >>> that it will hit. I would like to not instantiate a Harpoon object, >>> just call the Harpoon's getImpactCoords method and pass it the >>> required arguments. Is this possible? Thanks. Sorry if I got the terms >>> backwards in the subject; I can never remember which is static and >>> which is non-static. >>> >> >> Hi Alex, >> >> See you're still going for it :) >> >> I think that you're trying to build a Yamoto/Musashi before you've built >> a raft from oil drums or whatever :) If I'm wrong, I'll apologise here >> and now. > I have built one app in Python and have experience in Java and > Javascript, as well as some in PHP; in fact, I am going into my fourth > year of college for a computer science degree in September. While they > have not done as much programming as I would like, I have had enough > that I can find the commonalities between languages and generally know > what I am looking for (make this public, turn that into a class > instead of an independent collection of vars...) > That said, I have no professional experience programming and do only > assigned problems and hobby-level programming. My Screenless Widgets > app is nearing beta testing and works to my satisfaction, but I am > sure there is much I could do to improve it. Still, everyone has to > start somewhere... > I say all this not to express any offense at your message - believe > me, none taken - but rather to tell everyone just where I am coming > from. I should hope not, I used to be big-headed, but now I'm perfect :) >> >> For a really great introduction to Python, I suggest diveintopython, >> it's what got me going eight years ago. > I feel that I understand the basics; what I am running into are things > that crop up and I learn them as needed; if I learn a concept but then > never use it, I will forget it, or mix it up with a similar comcept in > another language, so I generally attack things by reading intro > tutorials, modifying them, and then continuing from there until I feel > that I can start my own project from scratch and figure out the pieces > as I go along. I suggest that you do *NOT* understand the basics, at least wrt Python, otherwise you would not have placed your original queries on c.l.py, before being asked to move to this ng/ml. Regardless of that, you're on *THE* finest group of mls/ngs going for getting very sound advice from some of the most highly respected guys/gals going. And if you want some kind of feeling of industry experiences, subscribe to the Python bugs/development/ideas ngs/mls (if you haven't already done so) and you'll very rapidly get an idea of just how difficult software development can get. As an example, look for the thread on comp.lang.python within the last 24 hours from myself subject "xrange issue 7721". A relatively simple thing you'd have thought, but read the background on the Python bug tracker, and you'll see it ain't quite that easy. But then, what do I know, I've only 34 years industry experience, albeit slightly tempered over the last nine years by physical and mental ill health. >> >> Kindest regards. >> >> Mark Lawrence. >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > I'm now going to bed, as it's 04:08 BST and I'm absolutely shattered. Kindest regards. Mark Lawrence. From alan.gauld at btinternet.com Sun May 30 09:26:22 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 30 May 2010 08:26:22 +0100 Subject: [Tutor] class methods as static methods? References: Message-ID: "Alex Hall" wrote > that it will hit. I would like to not instantiate a Harpoon object, > just call the Harpoon's getImpactCoords method and pass it the > required arguments. Is this possible? Others have pointed out that a) This is possible using staticmetjhod or classmetjod decorators and b) it seems a strange choice since you would expect the ability to use more than one harpoon and hence be better with an instance... I will add that if you really want a class method then maybe you can do without the class completely and just use a function? The usual reason for class methods is to operate on the class as a whole - ie all instances - it is not to provide functionality without any instances (except in languages without functions, like Java, which really use static methods as a kluge to cover their limitations!) And functioons are much easiert to write and manage than instanceless classes! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Sun May 30 12:10:53 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 30 May 2010 12:10:53 +0200 Subject: [Tutor] SENTINEL, & more In-Reply-To: <201005300036.24493.eike.welk@gmx.net> References: <201005300036.24493.eike.welk@gmx.net> Message-ID: <20100530121053.36d9eb9e@o> On Sun, 30 May 2010 00:36:24 +0200 Eike Welk wrote: > Hey Denis! > > I waited for a thread like this to appear, because I have a quirky, but IMHO > elegant, solution for those kinds of variables: > > class EnumMeta(type): > def __repr__(self): > return self.__name__ > > class Enum(object): > __metaclass__ = EnumMeta > > > Objects are created by inheriting from the Enum class. (Not by instantiating > it.) > > >>> class EAST(Enum): pass > >>> class WEST(Enum): pass > >>> class NORTH(Enum): pass > >>> class SOUTH(Enum): pass > > > The objects know their name, and when printed their name is printed. In this > respect they behave similarly to None. > > >>> print NORTH, SOUTH, EAST, WEST > NORTH SOUTH EAST WEST Great from the pov of practicality, but severely distorts the language's semantics, no? *The* issue is custom objects don't know their names. I have always wanted named objects to systematically know their own name. In python, classes, funcs, methods (possible a few other more) know it. >>> def f():pass ... >>> f.__name__ 'f' But sure, the double trick of using classes instead of instances, plus the metaclass to define repr as __name__ works great :-) > I call the class Enum, but this is certainly the wrong term since the values > are not enumerated. But is Sentinel the right term for something like this? I > thought a sentinel is a soldier who guards something. Hello English native > speakers! What is a good name? Enum is wrong, indeed. I call that "mark" (as you know). Perfect for me; but do not know if it fits sell in english. The meaning is about the same as the one of "token", but "token" is loaded in the field of programming. "Code" would also be great, but its sense is rather too open. I have a kind of simulation for pascal enums. See code below (at the time, I used the term "code".) * The user must explicitely specifiy code name. * Automatically gives a code an ordinal value if none defined. * The user can define a code/mark suite in one go: passing a name list if implicitely valued, a dict if explicitely valued. Individuals become attributes of the class. This allows clear naming such as "direction.NORTH". > Eike. > > P.S. By the way Denis, an earlier thread from you on the subject got me > thinking about it. ;-) Denis ================================ #!/usr/bin/env python # coding: utf-8 from sys import exit as exit from copy import copy as copy class Code(object): value = 1 def __init__(self, name=None, value=None): self.name = '_' if name is None else name self.value = Code.value if value is None else value Code.value = self.value + 1 def __eq__(self, other): if not isinstance(other,Code): raise TypeError("Can compare Code object only with other Code.") return self.value == other.value def __lt__(self, other): if not isinstance(other,Code): raise TypeError("Can compare Code object only with other Code.") return self.value < other.value def __repr__(self): return "Code(%s,%s)" %(self.name,self.value) def __str__(self): return "%s:%s" %(self.name,self.value) class CodeSuite(object): def __init__(self, code_defs): if isinstance(code_defs, list): for code_def in code_defs: code = Code(code_def) setattr(self, code_def, code) elif isinstance(code_defs, dict): for code_def in code_defs.items(): (name,value) = code_def code = Code(name,value) setattr(self, name, code) else: raise TypeError("A code suite definition must be a list or dict") def __getattr__(self, name): raise NameError("CodeSuite does not hold code '%s'." %name) def __iter__(self): codes = self.__dict__.values() codes.sort(key = lambda c:c.value) return iter(codes) def __repr__(self): cs = ",".join("'%s':%s" %(c.name,c.value) for c in self) return "CodeSuite({%s})" %cs def __str__(self): codes = " ".join(str(code) for code in self) return "CodeSuite:(%s)" %codes ### fast test ### def test(): print "=== code suite without values ===" suits = CodeSuite(['diamond','club','heart','spade']) print suits print repr(suits) print suits.heart print suits.heart>suits.club, suits.heart>suits.spade print print "=== code suite with values ===" cards = CodeSuite({'_7_':0,'_8_':0,'_9_':0, 'V':2,'D':3,'R':4, '_10_':10,'_1_':11}) print cards print repr(cards) print cards.D print cards.D>cards.R, cards.D>cards._9_ print cards.x # ==> error test() ================================ ________________________________ vit esse estrany ? spir.wikidot.com From timomlists at gmail.com Sun May 30 13:42:02 2010 From: timomlists at gmail.com (Timo) Date: Sun, 30 May 2010 13:42:02 +0200 Subject: [Tutor] Uploading a file (non-form) Message-ID: Hello, I am searching and searching and searching and ... Still haven't found the solution. Basicly, I want to upload a file to my http server. The user shouldn't have to do anything for this, so a userform on my webpage is a no-go. For just text, I managed to do it with a form, but fill it in automaticly through the code. Like: Clientside: form = urllib.urlencode([("text", "Some example text")]) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(None)) opener.open(urllib2.Request(url, None, USER_AGENT), form) Serverside: form = cgi.FieldStorage() text = form['text'].value Now I can use 'text' on the server, for mailing etc. But I really can't manage to do this with a file. Anyone can give me a helping hand here? Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Sun May 30 15:56:28 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sun, 30 May 2010 09:56:28 -0400 Subject: [Tutor] class methods as static methods? In-Reply-To: References: Message-ID: On 5/30/10, Alan Gauld wrote: > "Alex Hall" wrote > >> that it will hit. I would like to not instantiate a Harpoon object, >> just call the Harpoon's getImpactCoords method and pass it the >> required arguments. Is this possible? > > Others have pointed out that > a) This is possible using staticmetjhod or classmetjod decorators and > b) it seems a strange choice since you would expect the ability to > use more than one harpoon and hence be better with an instance... > > I will add that if you really want a class method then maybe > you can do without the class completely and just use a function? Yes, that is what I ended up doing for testing, but it works well and I do not think I will be making missile classes, just put each missile type in its own file in a weapons subfolder, then import "from weapons import *" to import all weapon files. > > The usual reason for class methods is to operate on the > class as a whole - ie all instances - it is not to provide > functionality > without any instances (except in languages without functions, > like Java, which really use static methods as a kluge to cover > their limitations!) > > And functioons are much easiert to write and manage than > instanceless classes! > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From alan.gauld at btinternet.com Sun May 30 17:38:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 30 May 2010 16:38:06 +0100 Subject: [Tutor] SENTINEL, & more References: <201005300036.24493.eike.welk@gmx.net> <20100530121053.36d9eb9e@o> Message-ID: "spir ?" wrote in > *The* issue is custom objects don't know their names. The objects don't have names. They are referenced by names. But the name has nothing to do with the object it references. If multiple names reference the same object which name is the object supposed to associate itself with? The concept doesn't make any sense! It makes some sense in a static language like C++ (well, sometimes) because an variable is a block of memory and the thing in that block can be associated with the block's name (but that doesn't work for pointers for the same reason as Python names don't work.) But even with static vars you can have multiple references to the static object so it breaks down again... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From robert.johansson at math.umu.se Sun May 30 17:39:45 2010 From: robert.johansson at math.umu.se (Robert Johansson) Date: Sun, 30 May 2010 17:39:45 +0200 Subject: [Tutor] namespaces Message-ID: <000001cb000e$54889350$fd99b9f0$@johansson@math.umu.se> Hi, This code generates the message "UnboundLocalError: local variable 'doubles' referenced before assignment" (line: if d[0] == d[1] and doubles == 2:) http://pastebin.com/mYBaCfj1 I think I have a fair picture of what it means but I would be very happy if someone could explain the difference between the two variables h and doubles in the code. Why is one accessible from the function but not the other? I looked into rules for namespaces but I'm still confused. Below is another sample of the code Cheers, Robert from random import * h = 6 doubles = 0 # current number of consecutive doubles def roll(): d = [randint(1, h), randint(1, h)] if d[0] == d[1] and doubles == 2: doubles = 0 return 0 elif d[0] == d[1] and doubles < 2: doubles += 1 return sum(d) else: return sum(d) for n in range(10): d = roll() print d -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Sun May 30 18:33:54 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 30 May 2010 18:33:54 +0200 Subject: [Tutor] namespaces In-Reply-To: <000001cb000e$54889350$fd99b9f0$@johansson@math.umu.se> References: <000001cb000e$54889350$fd99b9f0$@johansson@math.umu.se> Message-ID: Hi Robert > This code generates the message ?UnboundLocalError: local variable 'doubles' referenced before assignment? (line: if d[0] == d[1] and doubles == 2:) > > http://pastebin.com/mYBaCfj1 > > I think I have a fair picture of what it means but I would be very happy if someone could explain the difference between the two variables h and doubles in the code. Why is one accessible from the function but not the other? I looked into rules for namespaces but I?m still confused. Below is another sample of the code You assign a value to doubles in the roll() function, making Python think doubles is a local variable (which hasn't been assigned anything when you first use it, throwing the exception). If you assign some value to h after the first line in roll() (eg, h = 6), you'd get the same exception, but then for h. So, if you assign a value to a variable inside a function() and you want that variable to be the global one (instead of the implicitly assumed local one), you'll have to explicitly tell Python that: "global doubles" (probably on the first line in the function). Since you hadn't assigned any value to h inside roll(), only used it, Python assumes it's the global one. See also the second answer to this question: http://stackoverflow.com/questions/423379/global-variables-in-python Hope that helps, Evert > > Cheers, Robert > > from random import * > > h = 6 > doubles = 0 # current number of consecutive doubles > > def roll(): > d = [randint(1, h), randint(1, h)] > if d[0] == d[1] and doubles == 2: > doubles = 0 > return 0 > elif d[0] == d[1] and doubles < 2: > doubles += 1 > return sum(d) > else: > return sum(d) > > for n in range(10): > d = roll() > print d > From robert.johansson at math.umu.se Sun May 30 18:47:50 2010 From: robert.johansson at math.umu.se (Robert Johansson) Date: Sun, 30 May 2010 18:47:50 +0200 Subject: [Tutor] namespaces In-Reply-To: References: <000001cb000e$54889350$fd99b9f0$@johansson@math.umu.se> Message-ID: Thanks Evert for pointing out the difference and the discussion on global variables, it helped. /Robert -----Ursprungligt meddelande----- Fr?n: Evert Rol [mailto:evert.rol at gmail.com] Skickat: den 30 maj 2010 18:34 Till: Robert Johansson Kopia: tutor at python.org ?mne: Re: [Tutor] namespaces Hi Robert > This code generates the message "UnboundLocalError: local variable 'doubles' referenced before assignment" (line: if d[0] == d[1] and doubles == 2:) > > http://pastebin.com/mYBaCfj1 > > I think I have a fair picture of what it means but I would be very happy if someone could explain the difference between the two variables h and doubles in the code. Why is one accessible from the function but not the other? I looked into rules for namespaces but I'm still confused. Below is another sample of the code You assign a value to doubles in the roll() function, making Python think doubles is a local variable (which hasn't been assigned anything when you first use it, throwing the exception). If you assign some value to h after the first line in roll() (eg, h = 6), you'd get the same exception, but then for h. So, if you assign a value to a variable inside a function() and you want that variable to be the global one (instead of the implicitly assumed local one), you'll have to explicitly tell Python that: "global doubles" (probably on the first line in the function). Since you hadn't assigned any value to h inside roll(), only used it, Python assumes it's the global one. See also the second answer to this question: http://stackoverflow.com/questions/423379/global-variables-in-python Hope that helps, Evert > > Cheers, Robert > > from random import * > > h = 6 > doubles = 0 # current number of consecutive doubles > > def roll(): > d = [randint(1, h), randint(1, h)] > if d[0] == d[1] and doubles == 2: > doubles = 0 > return 0 > elif d[0] == d[1] and doubles < 2: > doubles += 1 > return sum(d) > else: > return sum(d) > > for n in range(10): > d = roll() > print d > From woodm1979 at gmail.com Sun May 30 18:59:30 2010 From: woodm1979 at gmail.com (Matthew Wood) Date: Sun, 30 May 2010 10:59:30 -0600 Subject: [Tutor] namespaces In-Reply-To: References: Message-ID: That's probably my least favorite error message in python. I wish that somehow it would have the line number of the first assignment statement instead of the first read statement. I know why it's not that way, but I just wish it weren't. -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Sun, May 30, 2010 at 10:47 AM, Robert Johansson < robert.johansson at math.umu.se> wrote: > Thanks Evert for pointing out the difference and the discussion on global > variables, it helped. > > /Robert > > -----Ursprungligt meddelande----- > Fr?n: Evert Rol [mailto:evert.rol at gmail.com] > Skickat: den 30 maj 2010 18:34 > Till: Robert Johansson > Kopia: tutor at python.org > ?mne: Re: [Tutor] namespaces > > Hi Robert > > > This code generates the message "UnboundLocalError: local variable > 'doubles' referenced before assignment" (line: if d[0] == d[1] and doubles > == 2:) > > > > http://pastebin.com/mYBaCfj1 > > > > I think I have a fair picture of what it means but I would be very happy > if someone could explain the difference between the two variables h and > doubles in the code. Why is one accessible from the function but not the > other? I looked into rules for namespaces but I'm still confused. Below is > another sample of the code > > You assign a value to doubles in the roll() function, making Python think > doubles is a local variable (which hasn't been assigned anything when you > first use it, throwing the exception). > If you assign some value to h after the first line in roll() (eg, h = 6), > you'd get the same exception, but then for h. > So, if you assign a value to a variable inside a function() and you want > that variable to be the global one (instead of the implicitly assumed local > one), you'll have to explicitly tell Python that: "global doubles" (probably > on the first line in the function). > Since you hadn't assigned any value to h inside roll(), only used it, > Python assumes it's the global one. > > See also the second answer to this question: > http://stackoverflow.com/questions/423379/global-variables-in-python > > Hope that helps, > > Evert > > > > > > Cheers, Robert > > > > from random import * > > > > h = 6 > > doubles = 0 # current number of consecutive doubles > > > > def roll(): > > d = [randint(1, h), randint(1, h)] > > if d[0] == d[1] and doubles == 2: > > doubles = 0 > > return 0 > > elif d[0] == d[1] and doubles < 2: > > doubles += 1 > > return sum(d) > > else: > > return sum(d) > > > > for n in range(10): > > d = roll() > > print d > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Sun May 30 20:17:43 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Sun, 30 May 2010 19:17:43 +0100 Subject: [Tutor] PYTHON ON NOKIA E71 Message-ID: Hi all, Pls does anyone know whether I can install python and code on my Nokia E71? I have full access to computer at my office but not at home. This is creating a serious break in my flow of study. With python on my phone,I can learn and code on the fly. Already,I read Alan Gauld's note from my phone browser but I need to start doing stuffs with my phone. If you got an idea or the link, pls bring it on. Thanks and Best regards, -- Sent from my mobile device Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From rabidpoobear at gmail.com Sun May 30 20:21:59 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sun, 30 May 2010 13:21:59 -0500 Subject: [Tutor] PYTHON ON NOKIA E71 In-Reply-To: References: Message-ID: On Sun, May 30, 2010 at 1:17 PM, Dipo Elegbede wrote: > Hi all, > Pls does anyone know whether I can install python and code on my Nokia E71? > I have full access to computer at my office but not at home. This is > creating a serious break in my flow of study. > With python on my phone,I can learn and code on the fly. > Already,I read Alan Gauld's note from my phone browser but I need to > start doing stuffs with my phone. > If you got an idea or the link, pls bring it on. > Thanks and Best regards, Yep I've got Python and Pygame on my E71 so it's definitely possible. You could've probably found the reference on Google faster than posting here and waiting for a reply. Good luck, it's a lot of fun having Python on your phone! -Luke From zebra05 at gmail.com Mon May 31 01:21:27 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 31 May 2010 01:21:27 +0200 Subject: [Tutor] Using Beautiful Soup to find an HTML element by its class attribute Message-ID: Hi everyone, I am using urllib to scrape an HTML page, and creating an instance of BeautifulSoup as follows: * *from BeautifulSoup import BeautifulSoup import re, urllib doc = urllib.urlopen(" http://weather.za.msn.com/local.aspx?wealocations=wc:SFXX0010&q=Cape+Town%2c+Western+Cape ").read() soup = BeautifulSoup(''.join(doc)) #print soup.prettify() weather_row = soup.findAll('tr', class="rs1") print weather_row The call to soup.findAll is failing because "class" is a reserved keyword in Python. Using BeautifulSoup, how can I find a specific table row whose only identifying attribute is its CSS class? Thanks. -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Mon May 31 01:32:59 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 31 May 2010 01:32:59 +0200 Subject: [Tutor] Using Beautiful Soup to find an HTML element by its class attribute Message-ID: Hi everyone, I am using urllib to scrape an HTML page, and creating an instance of BeautifulSoup as follows: * *from BeautifulSoup import BeautifulSoup import re, urllib doc = urllib.urlopen(my_url).read() soup = BeautifulSoup(''.join(doc)) #print soup.prettify() weather_row = soup.findAll('tr', class="rs1") print weather_row The call to soup.findAll is failing because "class" is a reserved keyword in Python. Using BeautifulSoup, how can I find a specific table row whose only identifying attribute is its CSS class? Thanks. -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Mon May 31 02:01:48 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 31 May 2010 02:01:48 +0200 Subject: [Tutor] Using Beautiful Soup to find an HTML element by its class attribute In-Reply-To: References: Message-ID: Thanks all, I found the solution: using the attributes dictionary as follows: weather_row = soup.findAll('tr', { "class" : "rs1" }) Many thanks. On Mon, May 31, 2010 at 1:32 AM, Sithembewena Lloyd Dube wrote: > Hi everyone, > > I am using urllib to scrape an HTML page, and creating an instance of > BeautifulSoup as follows: > * > *from BeautifulSoup import BeautifulSoup > import re, urllib > > doc = urllib.urlopen(my_url).read() > soup = BeautifulSoup(''.join(doc)) > > #print soup.prettify() > weather_row = soup.findAll('tr', class="rs1") > print weather_row > > The call to soup.findAll is failing because "class" is a reserved keyword > in Python. Using BeautifulSoup, how can I find a specific table row whose > only identifying attribute is its CSS class? > > Thanks. > > > -- > Regards, > Sithembewena Lloyd Dube > http://www.lloyddube.com > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Mon May 31 04:08:35 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sun, 30 May 2010 22:08:35 -0400 Subject: [Tutor] playing game across internet: suggestions for design? Message-ID: Hi all, While Battleship is not quite where I want it in terms of weapons, and while I await a response on another list to improve that, I figured I would at least start inquiries on the internet front. My plan is to let myself and a friend play each other at the game over the internet. I realize that one of us will have to play server and the other client, but the roles nake no difference. When a session starts, I will create my playing board and place my ships, and my friend will do the same. Then, I get his board and he gets mine. In this way I can fire at a board holding his ships, and he can fire at a board holding my ships. Each turn, I imagine some information moving from one computer to the other, such as the functions he called and their arguments. I can then parse this information and output information ("Your Battleship was sunk"). Basically, how might I go about setting up a connection between the two computers? Not at the socket level, but how do I tell his game that mine is ready to start? How do I decide who is server and who is client? Thanks! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From alan.gauld at btinternet.com Mon May 31 09:58:27 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 31 May 2010 08:58:27 +0100 Subject: [Tutor] playing game across internet: suggestions for design? References: Message-ID: "Alex Hall" wrote > I realize that one of us will have to play server and the other > client, but the roles nake no difference. Actually since there are only two players in Battleships you could dispense with a server and do a peer to peer game. Or you could create a single server and both be clients. There are several ways to architect this. You could even make it a web app with cookies to record which player is which. > Basically, how might I go about setting up a connection between the > two computers? Not at the socket level, but how do I tell his game > that mine is ready to start? How do I decide who is server and who > is > client? Thanks! If you go for a socket level interaction then you need to define your own protocol, or message set. There is a basic example of that in my tutorial in the Network Programming topic under the AddressBook example. If the protocol is significant in size - lots of commands - its best to define the message strings as constants in a shared module that both client and server can read. Python's string formatting characters make a useful templating language. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/tutor/ From mehgcap at gmail.com Mon May 31 14:38:23 2010 From: mehgcap at gmail.com (Alex Hall) Date: Mon, 31 May 2010 08:38:23 -0400 Subject: [Tutor] playing game across internet: suggestions for design? In-Reply-To: References: Message-ID: On 5/31/10, Alan Gauld wrote: > > "Alex Hall" wrote > >> I realize that one of us will have to play server and the other >> client, but the roles nake no difference. > > Actually since there are only two players in Battleships you > could dispense with a server and do a peer to peer game. > > Or you could create a single server and both be clients. When you say 'peer to peer', is this still with Python sockets? It sounds like what I am looking for! Or, creating a server and both of us being clients: I have a server (not my own machine, but I rent space on an iPowerWeb.com server) so I could do this, but I would have no idea where to start. Sounds like the p2p connection is the best one. > There are several ways to architect this. > You could even make it a web app with cookies to > record which player is which. I need a lot of keyboard interaction and popup dialogs with lists and input controls, and my server does not have Python on it. Good thought, though; maybe I could strip this one down and port to js... > >> Basically, how might I go about setting up a connection between the >> two computers? Not at the socket level, but how do I tell his game >> that mine is ready to start? How do I decide who is server and who >> is >> client? Thanks! > > If you go for a socket level interaction then you need to > define your own protocol, or message set. There is a basic > example of that in my tutorial in the Network Programming > topic under the AddressBook example. > > If the protocol is significant in size - lots of commands - its > best to define the message strings as constants in a shared > module that both client and server can read. Python's string > formatting characters make a useful templating language. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/tutor/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap