[Tutor] Kill a socket doing a recvfrom
Greetings my masters. How do I kill a socket which is locked in a recvfrom? My effort to kill it failed. The socket is running in its own thread. Any bright ideas? Med venlig hilsen/Kind regards Michael B. Arp Sørensen Programmer / BOFH If you want to enter my network while I'm out, you can find my SSH-key under my mouse mat - Michael Bernhard Arp Sørensen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] help using f2py
Hello! I posted this question on the f2py list but since I haven't got any answers (yet), I thought I'd try my luck here. I'm having a hard time wrapping a fortran subroutine into a python module. The problem seems to be related to having a subroutine argument to the fortran subroutine. A simple version of my problem is the following. Consider a subroutine (sub), which takes as argument a real array (x), the dimension (n) of that array and another subroutine (other_sub) that is to be called by sub : file sub.f95 -- subroutine sub(x,other_sub,n) integer::n real, dimension(n)::x external other_sub call other_sub(x) end subroutine sub -- The gnu fortran compiler compiles this snippet without any warnings or errors. However, when I run it through f2py, all I get is a bunch of errors (see below the error output). I'm not sure if this has any relevance but if I change argument x to a real scalar (instead of an array), then f2py executes fine and the python module is generated. I just don't get it! Can anybody explain me what I am doing wrong, please? Thanks in advance. Jose Amoreira Part of the output of f2py -m sub -c sub.f95: -- creating /tmp/tmpyHp76U/tmp creating /tmp/tmpyHp76U/tmp/tmpyHp76U creating /tmp/tmpyHp76U/tmp/tmpyHp76U/src.linux-x86_64-2.6 compile options: '-I/tmp/tmpyHp76U/src.linux-x86_64-2.6 - I/usr/lib64/python2.6/site-packages/numpy/core/include - I/usr/include/python2.6 -c' gcc: /tmp/tmpyHp76U/src.linux-x86_64-2.6/fortranobject.c gcc: /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c: In function ‘cb_other_sub_in_sub__user__routines’: /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: ‘n’ undeclared (first use in this function) /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: (Each undeclared identifier is reported only once /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: for each function it appears in.) /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c: In function ‘cb_other_sub_in_sub__user__routines’: /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: ‘n’ undeclared (first use in this function) /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: (Each undeclared identifier is reported only once /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c:306: error: for each function it appears in.) error: Command gcc -pthread -fno-strict-aliasing -DNDEBUG -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables - fasynchronous-unwind-tables -g -fwrapv -fPIC -I/tmp/tmpyHp76U/src.linux- x86_64-2.6 -I/usr/lib64/python2.6/site-packages/numpy/core/include - I/usr/include/python2.6 -c /tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.c -o /tmp/tmpyHp76U/tmp/tmpyHp76U/src.linux-x86_64-2.6/submodule.o failed with exit status 1 - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? E.g.: x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] x.sort() x ['var_0', 'var_1', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9'] This clearly does not give the desired result. I once solved this by making a dictionary of which the keys are zfilled versions of the list items (e.g. var_01, etc), then sort the keys, get the dictionary values in the right order, etc. It seemed a cumbersome way. Isn't there an easier way to sort x? Cheers!! Albert-Jan ~~ In the face of ambiguity, refuse the temptation to guess. ~~ --- On Tue, 1/12/10, Kent Johnson ken...@tds.net wrote: From: Kent Johnson ken...@tds.net Subject: Re: [Tutor] samples on sort method of sequence object. To: Make Twilight ph4...@gmail.com Cc: tutor@python.org Date: Tuesday, January 12, 2010, 7:53 PM On Tue, Jan 12, 2010 at 9:39 AM, Make Twilight ph4...@gmail.com wrote: I can understand how to use parameters of cmp and reverse,except the key parameter... Would anyone give me an example of using sort method with key parameter? http://personalpages.tds.net/~kent37/kk/7.html Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
Albert-Jan Roskam, 13.01.2010 13:51: Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? E.g.: x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] x.sort() x ['var_0', 'var_1', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9'] This clearly does not give the desired result. I once solved this by making a dictionary of which the keys are zfilled versions of the list items (e.g. var_01, etc), then sort the keys, get the dictionary values in the right order, etc. It seemed a cumbersome way. Isn't there an easier way to sort x? x.sort(key = lambda name: int(name.split('_')[-1])) Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
2010/1/13 Albert-Jan Roskam fo...@yahoo.com Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] Yes. x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] sorted(x, key=lamda x: int(x.strip('var_'))) SyntaxError: invalid syntax sorted(x, key=lambda x: int(x.strip('var_'))) ['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14'] Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
Very concise == neat solutions. Yummy. ;-) Thanks for your replies! Cheers!! Albert-Jan ~~ In the face of ambiguity, refuse the temptation to guess. ~~ --- On Wed, 1/13/10, Sander Sweers sander.swe...@gmail.com wrote: From: Sander Sweers sander.swe...@gmail.com Subject: Re: [Tutor] samples on sort method of sequence object. To: Albert-Jan Roskam fo...@yahoo.com Cc: *tutor python tutor@python.org Date: Wednesday, January 13, 2010, 2:14 PM 2010/1/13 Albert-Jan Roskam fo...@yahoo.com Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] Yes. x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] sorted(x, key=lamda x: int(x.strip('var_'))) SyntaxError: invalid syntax sorted(x, key=lambda x: int(x.strip('var_'))) ['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14'] Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
I should note that this doesn't solve the general case of mixed numeric/alphabetic sorting: a = ['a_1', 'a_2', 'a_3', 'a_4', 'a_10', 'b_1', 'b_2', 'b_3', 'b_4', 'b_10'] a.sort(key=lambda x: int(x.split('_')[-1])) a ['a_1', 'b_1', 'a_2', 'b_2', 'a_3', 'b_3', 'a_4', 'b_4', 'a_10', 'b_10'] It only works if the alphabetic part of the string is the same everywhere, and it doesn't work with a collection where some numbers appear before, some after, etc. Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t or, in a oneliner: alphanum_key = lambda x: tuple((int(''.join(g)) if k else ''.join(g)) for k, g in groupby(x, str.isdigit)) that's equivalent, but not very readable. It works by grouping sequences of numbers and not numbers together, and treating the number sequences as integers rather than strings. a = ['0a', '1a', '10a', 'a1', 'a2', 'a10', 'a11', 'b2', 'b3', 'b20'] a.sort() a ['0a', '10a', '1a', 'a1', 'a10', 'a11', 'a2', 'b2', 'b20', 'b3'] # regular sort messes up because 'a10' is alphabetically before 'a2'. now with alphanumeric sort: a.sort(key=alphanum_key) a ['0a', '1a', '10a', 'a1', 'a2', 'a10', 'a11', 'b2', 'b3', 'b20'] Hugo On Wed, Jan 13, 2010 at 2:32 PM, Albert-Jan Roskam fo...@yahoo.com wrote: Very concise == neat solutions. Yummy. ;-) Thanks for your replies! Cheers!! Albert-Jan ~~ In the face of ambiguity, refuse the temptation to guess. ~~ --- On *Wed, 1/13/10, Sander Sweers sander.swe...@gmail.com* wrote: From: Sander Sweers sander.swe...@gmail.com Subject: Re: [Tutor] samples on sort method of sequence object. To: Albert-Jan Roskam fo...@yahoo.com Cc: *tutor python tutor@python.org Date: Wednesday, January 13, 2010, 2:14 PM 2010/1/13 Albert-Jan Roskam fo...@yahoo.comhttp://us.mc1107.mail.yahoo.com/mc/compose?to=fo...@yahoo.com Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] Yes. x ['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12'] sorted(x, key=lamda x: int(x.strip('var_'))) SyntaxError: invalid syntax sorted(x, key=lambda x: int(x.strip('var_'))) ['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14'] Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to sort a tab delim file
Hi: I have a tab-delim file: col1 col2 col3 andrew1987 1990 jake 1974 1980 jim 1964 1970 lance1984 1992 how can I sort column 2 and get : jim 1964 1970 jake 1974 1980 lance1984 1992 andrew1987 1990 I know how to sort a items in one list. but I cannot get how this can be done on a file. any suggestions. thanks! Hs ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to sort a tab delim file
On Wed, Jan 13, 2010 at 9:21 AM, Hs Hs ilhs...@yahoo.com wrote: Hi: I have a tab-delim file: col1 col2 col3 andrew1987 1990 jake 1974 1980 jim 1964 1970 lance1984 1992 how can I sort column 2 and get : jim 1964 1970 jake 1974 1980 lance1984 1992 andrew1987 1990 I know how to sort a items in one list. but I cannot get how this can be done on a file. any suggestions. thanks! Hs ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Sounds like a school project to me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to sort a tab delim file
On Wed, Jan 13, 2010 at 9:21 AM, Hs Hs ilhs...@yahoo.com wrote: Hi: I have a tab-delim file: col1 col2 col3 andrew1987 1990 jake 1974 1980 jim 1964 1970 lance1984 1992 how can I sort column 2 and get : jim 1964 1970 jake 1974 1980 lance1984 1992 andrew1987 1990 I know how to sort a items in one list. but I cannot get how this can be done on a file. any suggestions. thanks! Hs ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor http://diveintopython.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Searching in a file
Hi, I have a file generated from a webpage. I want to search that file for a specific keyword, in my case 'NEW'. Once I have found that keyword I want to retrieve information below it, e.g. web link, size of file etc. When I have this information I move off until I find another 'NEW' and the process starts all over. Please can someone give me some pointers on how to do this. I can find the line containing 'NEW' which I get using a simple for loop on every line in the file, but how do I then traverse the next x amount of lines, taking what I want until either the next 'NEW' or eof. e.g. for line in file: if re.findall('NEW', line) # or search list.append(line) # to do something to later I cannot 'get' to the following lines because I would need to get out of the loop. I did use enumerate and was wondering if I could use the line number, or maybe I could use an re iterator. Any advice would be much appreciated. Thanks paul __ Information from ESET Smart Security, version of virus signature database 4767 (20100113) __ The message was checked by ESET Smart Security. http://www.eset.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
Hugo Arts, 13.01.2010 15:25: Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t Note that this won't work in Py3, where integers and strings are not ordered, i.e. not comparable w.r.t the and operators. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel stefan...@behnel.de wrote: Hugo Arts, 13.01.2010 15:25: Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t Note that this won't work in Py3, where integers and strings are not ordered, i.e. not comparable w.r.t the and operators. True. You can accommodate by writing a ComparableInt class extending int, and implement __lt__ and __gt__. It's not exactly succinct, but it works. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to sort a tab delim file
I have a tab-delim file: col1 col2 col3 andrew 1987 1990 jake 1974 1980 jim 1964 1970 lance 1984 1992 how can I sort column 2 and get : jim 1964 1970 jake 1974 1980 lance 1984 1992 andrew 1987 1990 I know how to sort a items in one list. but I cannot get how this can be done on a file. hi, it seems like you almost have all the tools you need to solve this problem! here are your tasks: 1. read the data out of a file and into a list -- experiment with open() and listcomps 2. sort the list -- you already know how to do this 3. create a new file with the sorted data - use open() again, listcomps, str.join(), and file.write() good luck! -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
I have a file generated from a webpage. I want to search that file for a specific keyword, in my case 'NEW'. Once I have found that keyword I want to retrieve information below it, e.g. web link, size of file etc. When I have this information I move off until I find another 'NEW' and the process starts all over. Please can someone give me some pointers on how to do this. I can find the line containing 'NEW' which I get using a simple for loop on every line in the file, but how do I then traverse the next x amount of lines, taking what I want until either the next 'NEW' or eof. : Any advice would be much appreciated. based on your requirements, i think you should abandon the use of 're' and regular expressions in general since you aren't matching a pattern. just use the 'in' operator or any string methods, e.g., {,r}{find,index}(). it sounds like NEW only appears once on any given line, so those should suffice. if not, you'll have to do further checks. given those clues, you should be able to accomplish your task be iterating over each line and processing it as desired. good luck! -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
On 1/13/2010 9:49 AM Paul Melvin said... Hi, I have a file generated from a webpage. I want to search that file for a specific keyword, in my case 'NEW'. Once I have found that keyword I want to retrieve information below it, e.g. web link, size of file etc. When I have this information I move off until I find another 'NEW' and the process starts all over. Please can someone give me some pointers on how to do this. I can find the line containing 'NEW' which I get using a simple for loop on every line in the file, but how do I then traverse the next x amount of lines, taking what I want until either the next 'NEW' or eof. e.g. for line in file: if re.findall('NEW', line) # or search list.append(line) # to do something to later don't use list as i shadows the builtin list I cannot 'get' to the following lines because I would need to get out of the loop. How about (untested): additionalLines = 3 remainingLines = 0 for line in file: if 'NEW' in line or remainingLines: mylist.append(line) if not remainingLines: remainingLines = additionalLines else: remainingLines -= 1 Emile I did use enumerate and was wondering if I could use the line number, or maybe I could use an re iterator. Any advice would be much appreciated. Thanks paul __ Information from ESET Smart Security, version of virus signature database 4767 (20100113) __ The message was checked by ESET Smart Security. http://www.eset.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
On Wed, Jan 13, 2010 at 2:21 PM, Stefan Behnel stefan...@behnel.de wrote: Hugo Arts, 13.01.2010 15:25: Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t Note that this won't work in Py3, where integers and strings are not ordered, i.e. not comparable w.r.t the and operators. It will work fine if all the list items have the same format which I would think is the most common case. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
On Wed, Jan 13, 2010 at 6:49 PM, Paul Melvin p...@assured-networks.co.uk wrote: Hi, I have a file generated from a webpage. I want to search that file for a specific keyword, in my case 'NEW'. Once I have found that keyword I want to retrieve information below it, e.g. web link, size of file etc. When I have this information I move off until I find another 'NEW' and the process starts all over. Please can someone give me some pointers on how to do this. I can find the line containing 'NEW' which I get using a simple for loop on every line in the file, but how do I then traverse the next x amount of lines, taking what I want until either the next 'NEW' or eof. e.g. for line in file: if re.findall('NEW', line) # or search list.append(line) # to do something to later I cannot 'get' to the following lines because I would need to get out of the loop. the most obvious answer would be to take a look at the 'next()' function, that should solve this immediate problem. Another approach is to set a variable, foundnew = True, after you have found the word 'NEW', and continue. Then, in your loop, if foundnew is True, you could process the line for your additional data. Then, after processing, foundnew could be set back to false. Yet another approach is to abandon the for loop entirely, and use a while loop combined with the readline method, which allows you to read lines wherever you like. This is a similar solution to the one using next() above. I would personally prefer solutions one or three. number two seems like it could get complicated when you need to process multiple lines of information after every NEW. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
Hugo Arts hugo.yo...@gmail.com wrote the most obvious answer would be to take a look at the 'next()' function, that should solve this immediate problem. This would be nmy advice too, but you need to get an explicit iterator to use it. it = open(.) for line in it: if 'NEW' in line: ln = it.next() etc... Yet another approach is to abandon the for loop entirely, and use a while loop combined with the readline method, I prefer the next() approach. But a third option is to use a split and apply it to the whole file as a string thereby breaking the file into as many chunks as start with a line containing 'NEW'... data = open().read() for chunk in data.split('NEW'): for line in chunk.split('\n'): # create lines out of the chunks - you may not need this # process lines till done Just a thought. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
On Thu, Jan 14, 2010 at 12:05 AM, Alan Gauld alan.ga...@btinternet.com wrote: I prefer the next() approach. Rightfully so. IMO, The while loop with readline is basically the C version of that, for the poor people who don't have iterators. But a third option is to use a split and apply it to the whole file as a string thereby breaking the file into as many chunks as start with a line containing 'NEW'... data = open().read() for chunk in data.split('NEW'): for line in chunk.split('\n'): # create lines out of the chunks - you may not need this # process lines till done Just a thought. Probably the simplest option by far. Disadvantage is you'll have to load the entire file into memory. Whether that is important depends entirely on the file. But unless the file is extremely large, it shouldn't be a big problem. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
On 01/14/10 06:56, Hugo Arts wrote: On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel stefan...@behnel.de wrote: Hugo Arts, 13.01.2010 15:25: Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t Note that this won't work in Py3, where integers and strings are not ordered, i.e. not comparable w.r.t the and operators. True. You can accommodate by writing a ComparableInt class extending int, and implement __lt__ and __gt__. It's not exactly succinct, but it works. Not necessary, you can just pass the cmp= parameter to sort that checks the chunk's type. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples on sort method of sequence object.
On Thu, Jan 14, 2010 at 1:47 AM, Lie Ryan lie.1...@gmail.com wrote: On 01/14/10 06:56, Hugo Arts wrote: On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel stefan...@behnel.de wrote: Hugo Arts, 13.01.2010 15:25: Here is my solution for the general case: from itertools import groupby def alphanum_key(string): t = [] for isdigit, group in groupby(string, str.isdigit): group = ''.join(group) t.append(int(group) if isdigit else group) return t Note that this won't work in Py3, where integers and strings are not ordered, i.e. not comparable w.r.t the and operators. True. You can accommodate by writing a ComparableInt class extending int, and implement __lt__ and __gt__. It's not exactly succinct, but it works. Not necessary, you can just pass the cmp= parameter to sort that checks the chunk's type. might be interesting to see which performs better. cmp is called more often, but replacing C versions of __lt__ and __gt__ with python versions slows things down also, I would presume. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Keeping a list of attributes of a certain type
Hi everybody, Here is my problem. I have two classes, 'Body' and 'Member', and some attributes of 'Body' can be of type 'Member', but some may not. The precise attributes that 'Body' has depend from instance to instance, and they can be added or deleted. I need any instance of 'Body' to keep an up-to-date list of all its attributes that belong to the class 'Member'. How do I do this? Best, Guilherme -- Guilherme P. de Freitas http://www.gpfreitas.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] confidence interval
Hi tutor, I have this code for generating a confidence interval from an array of values: import numpy as np import scipy as sp def mean_confidence_interval(data, confidence=0.95): a = 1.0*np.array(data) n = len(a) m, se = np.mean(a), sp.stats.stderr(a) h = se * sp.stats.t._ppf((1+confidence)/2., n-1) return m, m-h, m+h This works but I feel there's a better and more succinct way to do this. Does anyone know of an existing python package that can do this for me? Thanks, -Drew ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping a list of attributes of a certain type
Ok, I got something that seems to work for me. Any comments are welcome. class Member(object): def __init__(self): pass class Body(object): def __init__(self): self.members = [] def __setattr__(self, obj, value): if isinstance(value, Member): self.members.append(obj) object.__setattr__(self, obj, value) else: object.__setattr__(self, obj, value) def __delattr__(self, obj): if isinstance(getattr(self, obj), Member): self.members.remove(obj) object.__delattr__(self, obj) else: object.__delattr__(self, obj) john = Body() john.arm = Member() print(john.members) del john.arm print(john.members) On Wed, Jan 13, 2010 at 6:24 PM, Guilherme P. de Freitas guilhe...@gpfreitas.com wrote: Hi everybody, Here is my problem. I have two classes, 'Body' and 'Member', and some attributes of 'Body' can be of type 'Member', but some may not. The precise attributes that 'Body' has depend from instance to instance, and they can be added or deleted. I need any instance of 'Body' to keep an up-to-date list of all its attributes that belong to the class 'Member'. How do I do this? Best, Guilherme -- Guilherme P. de Freitas http://www.gpfreitas.com -- Guilherme P. de Freitas http://www.gpfreitas.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor