Re: [Tutor] what is the equivalent function to strtok() in c++

2010-01-11 Thread Stefan Behnel
Alan Gauld, 11.01.2010 10:07: For more sophisticated functionality use the re module or a specialised parser. Just a quick note here, "specialised parser" might sound like something that's hard to write. It's not. There are quite some parser packages available for Python that are easy to use.

Re: [Tutor] samples on sort method of sequence object.

2010-01-13 Thread Stefan Behnel
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'

Re: [Tutor] samples on sort method of sequence object.

2010-01-13 Thread Stefan Behnel
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

Re: [Tutor] samples on sort method of sequence object.

2010-01-14 Thread Stefan Behnel
Lie Ryan, 14.01.2010 01:47: On 01/14/10 06:56, Hugo Arts wrote: On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel 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

Re: [Tutor] Book for Python and XML

2010-01-17 Thread Stefan Behnel
Григор, 15.01.2010 08:28: > Someone to have good E-book for python and XMl The existing books are rather old. You might find these interesting, though: http://effbot.org/zone/element.htm http://codespeak.net/lxml/tutorial.html http://www.nmt.edu/tcc/help/pubs/pylxml Stefan _

Re: [Tutor] exit handler for C extension module

2010-01-17 Thread Stefan Behnel
Shuying Wang, 18.01.2010 03:13: > What would be an atexit equivalent for a C extension module? When my > extension module is unloaded, I would like some clean up functions to > be called from an external c library. I've had a look at the C > extension guide but I can't find anything like that, I'm

Re: [Tutor] exit handler for C extension module

2010-01-18 Thread Stefan Behnel
Shuying Wang, 18.01.2010 08:58: > I found what I was after, right after I posted to this list. It's > Py_AtExit. I'm accessing an old, unsupported database using it's C > API. I didn't write the respective code in Cython, but there's a comment next to it saying /* Don't use Py_AtExit because

Re: [Tutor] Still searching in html files

2010-01-21 Thread Stefan Behnel
Paul Melvin, 21.01.2010 10:03: > I am still looking for information in these files and have a sort of > 'clunky' solution that I would like feedback on please. > > The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like > some advice on is how to get the sorted data and where t

Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Stefan Behnel
vanam, 22.01.2010 07:44: > [Query]: How to write Python string to PYTHON to that file without > intact of the full contents What one would normally do is: read the file (line by line if that's ok in your case), replace the word by the new one, write the new line to a new temporary file. When done,

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Stefan Behnel
Stefan Lesicnik, 22.01.2010 09:11: > I have used the fileinput module for this quiet alot. It has an inplace > option which does what you want and a backup option that keeps a backup. > > http://docs.python.org/library/fileinput.html > http://www.doughellmann.com/PyMOTW/fileinput/index.html Ah, r

Re: [Tutor] parse text file

2010-02-02 Thread Stefan Behnel
Norman Khine, 02.02.2010 10:16: > get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""") > get_title = re.compile(r"""(.*)<\/strong>""") > get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""") > get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 12:26: > Please excuse the obviousness of my question (if it is), but I have > searched the documentation for how to generate a list e.g. [(1,2), > (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point > me in the right direction. You may want to add a lit

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 13:34: > I have solved it myself - must search more before posting! > > If anyone at my kind of level is interested:- > mystring = "[(1,2), (3,4)]" mylist = eval(mystring) mylist > [(1,2), (3,4)] type(mylist) > As others have pointed out, this may or

Re: [Tutor] string to list

2010-02-10 Thread Stefan Behnel
Owain Clarke, 10.02.2010 14:32: >> You may want to add a little bit about your use case. Is that really >> the input you >> have to deal with? Where does it come from? Can you control the >> format? What do >> you want to do with the list you extract from the string? >> >> All of that may have an i

Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Owain Clarke, 10.02.2010 17:57: > data.sort(key=lambda x:x[0]) > data.sort(key=lambda x:x[1]) Two things to note: 1) you can use the operator module, specifically operator.itemgetter 2) given that you have lists as items in the 'data' list, it's enough to call sort() once, as the comparison of l

Re: [Tutor] string to list

2010-02-11 Thread Stefan Behnel
Kent Johnson, 11.02.2010 14:16: > On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote: > >> 2) given that you have lists as items in the 'data' list, it's enough to >> call sort() once, as the comparison of lists is defined as the comparison >> of each item

Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Luke Paireepinart, 14.02.2010 13:12: > Also I think beautifulsoup is part of the Python standardlib now, isn't it? No, it's not. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailma

Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Amit Sethi, 15.02.2010 13:28: > Well ya I was kind of hoping to know about more tools and recommendations on > how to edit broken html. You already found lxml, don't think you can do any better. Note that you shouldn't "edit broken html" but "fix broken HTML and then edit correct HTML". Writing ou

Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Kent Johnson, 15.02.2010 14:05: > On Mon, Feb 15, 2010 at 7:28 AM, Amit Sethi wrote: >> Well ya I was kind of hoping to know about more tools and recommendations on >> how to edit broken html . > > This page lists several alternatives: html5, lxml, elementtree: > http://www.crummy.com/software/Bea

Re: [Tutor] Reading large bz2 Files

2010-02-19 Thread Stefan Behnel
Norman Rieß, 19.02.2010 13:42: > i am trying to read a large bz2 file with this code: > > source_file = bz2.BZ2File(file, "r") > for line in source_file: > print line.strip() > > But after 4311 lines, it stoppes without a errormessage. What does "stops" mean here? Does it crash? Does it exit

Re: [Tutor] Reading large bz2 Files

2010-02-22 Thread Stefan Behnel
Norman Rieß, 19.02.2010 13:42: > i am trying to read a large bz2 file with this code: > > source_file = bz2.BZ2File(file, "r") > for line in source_file: > print line.strip() > > But after 4311 lines, it stoppes without a errormessage. The bz2 file is > much bigger though. Could you send in

Re: [Tutor] raising number to a power

2010-02-25 Thread Stefan Behnel
Monte Milanuk, 25.02.2010 16:47: > Is there a benefit (besides brevity) one way or the other between using: > > import math > ... > math.pow(x,y) # x raised to the power y > > vs. > > x**y > > ? Did you try it? >>> import math >>> print(math.pow(4,4)) 256.0 >>> 4**4 256

Re: [Tutor] raising number to a power

2010-02-25 Thread Stefan Behnel
Monte Milanuk, 25.02.2010 16:47: > Is there a benefit (besides brevity) one way or the other between using: > > import math > ... > math.pow(x,y) # x raised to the power y > > vs. > > x**y > > ? You might also be interested in this: http://docs.python.org/reference/datamodel.html#emulating-n

Re: [Tutor] raising number to a power

2010-02-25 Thread Stefan Behnel
Monte Milanuk, 25.02.2010 18:27: > So... pow(4,4) is equivalent to 4**4, which works on anything - integers, > floats, etc. Correct, e.g. >>> class Test(object): ... def __pow__(self, other, modulo=None): ... print("POW!") ... return 'tutu' ... >>> pow(Test(), 4) POW!

Re: [Tutor] Top posters for 2009

2010-02-26 Thread Stefan Behnel
%) > Luke Paireepinart 32 (2.8%) > Shashwat Anand 30 (2.6%) > Wayne Werner 29 (2.5%) > Steven D'Aprano 28 (2.4%) > Stefan Behnel 24 (2.1%) > Dave Angel 22 (1.9%) > Lie Ryan 19 (1.6%) > Hugo Arts 16 (1.4%) > Benno Lang 14 (1.2%) > David 14 (1.2%) > Giorgio 14 (1.2%) >

Re: [Tutor] Encoding

2010-03-03 Thread Stefan Behnel
Giorgio, 03.03.2010 09:36: i am looking for more informations about encoding in python: i've read that Amazon SimpleDB accepts every string encoded in UTF-8. How can I encode a string? byte_string = unicode_string.encode('utf-8') If you use unicode strings throughout your application, you w

Re: [Tutor] Encoding

2010-03-03 Thread Stefan Behnel
Giorgio, 03.03.2010 14:09: byte_string = unicode_string.encode('utf-8') If you use unicode strings throughout your application, you will be happy with the above. Note that this is an advice, not a condition. Mmm ok. So all strings in the app are unicode by default? Do you know if there is

Re: [Tutor] Encoding

2010-03-03 Thread Stefan Behnel
Giorgio, 03.03.2010 15:50: Depends on your python version. If you use python 2.x, you have to use a u before the string: s = u'Hallo World' Ok. So, let's go back to my first question: s = u'Hallo World' is unicode in python 2.x -> ok Correct. s = 'Hallo World' how is encoded? Depend

Re: [Tutor] Encoding

2010-03-03 Thread Stefan Behnel
Giorgio, 03.03.2010 18:28: string = u"blabla" This is unicode, ok. Unicode UTF-8? No, not UTF-8. Unicode. You may want to read this: http://www.amk.ca/python/howto/unicode Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change su

Re: [Tutor] recursive generator

2010-03-07 Thread Stefan Behnel
Steven D'Aprano, 07.03.2010 14:27: On Sun, 7 Mar 2010 11:58:05 pm spir wrote: def __iter__(self): ''' Iteration on (key,value) pairs. ''' print '*', if self.holdsEntry: yield (self.key,self.value) for child in self.children: prin

Re: [Tutor] Encoding

2010-03-07 Thread Stefan Behnel
Giorgio, 05.03.2010 14:56: What i don't understand is why: s = u"ciao è ciao" is converting a string to unicode, decoding it from the specified encoding but t = "ciao è ciao" t = unicode(t) That should do exactly the same instead of using the specified encoding always assume that if i'm not te

Re: [Tutor] Read XML records one by one

2010-03-08 Thread Stefan Behnel
Hichiro, 08.03.2010 10:48: I'm trying to read one by one record in XML file to find out its tag and attribute for schema matching. But I haven't done yet. So, could you help me?! You were not very specific about your data, neither did you provide enough information about your use case to under

Re: [Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread Stefan Behnel
Emad Nawfal (عمـ نوفل ـاد), 09.03.2010 16:45: What about indexing part1 = word[:4] That's slicing. part2 = word[4] That's indexing. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.

Re: [Tutor] Hi there :.)

2010-03-16 Thread Stefan Behnel
Alan Gauld, 15.03.2010 20:28: wrote (apparently python is slow ?!?). It is all relative. If you want to write fast moving graphics etc then yes, you probably need C++. For anything else you might find Python is fast enough. A good approach tends to be: write it in Python first, benchmark it

Re: [Tutor] parsing a "chunked" text file

2010-03-18 Thread Stefan Behnel
Karim Liateni, 04.03.2010 01:23: Steven D'Aprano wrote: def skip_blanks(lines): """Remove leading and trailing whitespace, ignore blank lines.""" for line in lines: line = line.strip() if line: yield line Is there a big difference to write your first functions as below because I am not familia

Re: [Tutor] Efficiency and speed

2010-03-19 Thread Stefan Behnel
James Reynolds, 19.03.2010 17:41: I've still been working towards learning the language, albeit slowly and I've been working on a project that is somewhat intense on the numerical calculation end of things. Running 10,000 trials takes about 1.5 seconds and running 100,000 trials takes 11 seconds

Re: [Tutor] Efficiency and speed

2010-03-19 Thread Stefan Behnel
James Reynolds, 19.03.2010 21:17: Here's another idea I had. I thought this would be slower than then the previous algorithm because it has another for loop and another while loop. I read that the overhead of such loops is high, so I have been trying to avoid using them where possible. Prematur

Re: [Tutor] Getting traceback info from C-API

2010-04-05 Thread Stefan Behnel
Shu, 06.04.2010 01:06: I have a CAPI extension module that is giving me MemoryError exceptions from once in a while with no other information MemoryError is a bit special in that it usually only occurs when memory allocation fails, in which case raising a new exception object would likely al

Re: [Tutor] create a wrapper for a C program

2010-04-06 Thread Stefan Behnel
anjali nair, 06.04.2010 17:17: I am new to python. My doubt is related to wrapping a C program with a python script.. I am running Netperf for Network Performance Benchmarking. The code is written in C and has an associated Makefile. I thought of writing a Python script which should be able to ru

Re: [Tutor] Sequences of letter

2010-04-12 Thread Stefan Behnel
Juan Jose Del Toro, 12.04.2010 07:12: I wan to write a program that could print out the suquence of letters from "aaa" all the way to "zzz" like this: aaa aab aac ... zzx zzy zzz So far this is what I have: letras = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t

Re: [Tutor] Making pretty web pages from python code examples?

2010-04-13 Thread Stefan Behnel
Modulok, 13.04.2010 16:52: ... generate pretty little web pages ... Note that the 'pretty' bit usually doesn't reside in HTML but in CSS. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.pytho

Re: [Tutor] Problems with creating XML-documents

2010-04-15 Thread Stefan Behnel
Hi, my main advice up-front: if you want good advice, give good details about the problem you face and enough background to let others understand what the real problem is and what your environmental constraints are. Karjer Jdfjdf, 15.04.2010 08:03: I know the theory of XML but have never us

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
Ark, 16.04.2010 08:50: A friend of mine suggested me to do the next experiment in python and Java. It's a simple program to sum all the numbers from 0 to 10. result = i = 0 while i< 10: result += i i += 1 print result I hope you are aware that this is a) a very lous

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
Alan Gauld, 16.04.2010 10:09: Even the built in sum() will be faster than a while loop: result = sum(range(10)) although it still took 10 minutes on my PC. Did you mean to say "minutes" or rather "seconds" here? And did you really mean to use "range" or rather "xrange" (or "range" in

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
Stefan Behnel, 16.04.2010 09:38: A compiler for a statically compiled language can see that the above loop yields a constant result, so it can calculate the result in advance (or at least reduce the loop overhead for the calculation) instead of generating code for the loop as it stands. That

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
Alan Gauld, 16.04.2010 10:29: "Stefan Behnel" wrote import cython @cython.locals(result=cython.longlong, i=cython.longlong) def add(): result = 0 for i in xrange(10): result += i return result print add() This runs in less than half a second on

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
Steven D'Aprano, 16.04.2010 12:00: On Fri, 16 Apr 2010 06:29:40 pm Alan Gauld wrote: "Stefan Behnel" wrote import cython @cython.locals(result=cython.longlong, i=cython.longlong) def add(): result = 0 for i in xrange(10):

Re: [Tutor] Loop comparison

2010-04-17 Thread Stefan Behnel
Dave Angel, 17.04.2010 10:47: Alan Gauld wrote: "Lie Ryan" wrote A friend of mine suggested me to do the next experiment in python and Java. It's a simple program to sum all the numbers from 0 to 10. result = i = 0 while i < 10: result += i i += 1 print result Are you sure

Re: [Tutor] Iterating through a list of strings

2010-05-02 Thread Stefan Behnel
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 proce

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
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): &q

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
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

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
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 ha

Re: [Tutor] Extracting xml text

2010-06-20 Thread Stefan Behnel
T.R. D., 20.06.2010 08:03: I'm trying to parse a list of xml strings and so far it looks like the xml.parsers.expat is the way to go but I'm not quite sure how it works. I'm trying to parse something similar to the following. I'd like to collect all headings and bodies and associate them in a v

Re: [Tutor] Extracting xml text

2010-06-20 Thread Stefan Behnel
Hi, please don't top-post, it makes your replies hard to read in context. Karim, 20.06.2010 10:24: On 06/20/2010 10:14 AM, Stefan Behnel wrote: Use ElementTree's iterparse: from xml.etree.cElementTree import iterparse >> [...] > I know you are promoting Etree and I am

Re: [Tutor] Extracting xml text

2010-06-20 Thread Stefan Behnel
T.R. D., 20.06.2010 16:04: I decided to go with iterparse but trying the simple example in the python interpreter led to an error (see below) and when I tried this with a much larger xml sample, it seemed to print the full elements, not the specific values of the element. For example, given what

Re: [Tutor] retrieve URLs and text from web pages

2010-06-29 Thread Stefan Behnel
Khawla Al-Wehaibi, 29.06.2010 17:12: I decided to go with Regular Expressions to modify the text. In the Python.org it is stated that they provide more options and flexibilty compared to strings and their modules. "their modules" referring to the "string" module and the string methods here, I

Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Stefan Behnel
Richard D. Moores, 05.07.2010 11:37: I keep getting hung up over the meaning of "the return value" of an expression. I am of course familiar with values returned by a function, but don't quite grasp what the return value of, say, the y of "x and y" might mean. Think of a different expression, l

Re: [Tutor] Response to responses about list of lists: a meta exercise in mailinglist recursion

2010-07-13 Thread Stefan Behnel
Siren Saren, 13.07.2010 14:40: I'm not sure if there's a way to submit responses 'live' or whether it's better to respond to subthreads at once or together, so I'll err on the side of discretion and just send one response. It's not generally a problem to send one response like this regarding ma

Re: [Tutor] I don't understand this code

2010-07-14 Thread Stefan Behnel
ZUXOXUS, 13.07.2010 16:43: Hi, I am a true beginner in programming, and im learning with inventwithpython.com. There's something I dont understand, and i would really appreciate any help. In chapter 9, the one about the Hangman game, I don't get the block of code in line 61 59. words = 'ant

Re: [Tutor] Schema change in ElementTree

2010-08-15 Thread Stefan Behnel
Benjamin Serrato, 16.08.2010 05:51: Hi guys, thanks for you help in the past. I had my first occasion to write a useful script recently editing the contents of an xml file. It was pretty simple, but I have two problems which I know have to take care of by opening in Notepad++ and changing manua

Re: [Tutor] Schema change in ElementTree

2010-08-16 Thread Stefan Behnel
justin.mailingli...@gmail.com, 16.08.2010 10:38: # so that ET preserves your namespaces ET._namespace_map["http://www.w3.org/2001/XMLSchema";] = 'xs' # open your file explicitly specifying the correct mode f = open('testing.xml', 'w') xml_tree.write(f, encoding='UTF-8') f.close() Note that it'

Re: [Tutor] Schema change in ElementTree

2010-08-16 Thread Stefan Behnel
> Stefan Behnel wrote: Note that it's best to hit "reply" when responding to other people's postings. That way, mail readers can see the relation between the original posting and the reply and sort the reply into the right thread. Comment to my own posting: as Alan no

Re: [Tutor] Elementtree and pretty printing in Python 2.7

2010-08-22 Thread Stefan Behnel
Jerry Hill, 22.08.2010 16:51: Neither, as far as I know. The XML you get is perfectly valid XML. It's clearly well-formed, but I can't see it being valid without some kind of schema to validate it against. Note that the OP has already written a follow-up but forgot to reply to the origin

Re: [Tutor] OpenMP

2010-10-11 Thread Stefan Behnel
Alan Gauld, 09.10.2010 19:50: Now, the bad news is that so far as I know the python interpreter does not expose its threading model to the OS to even if you use threads the interpreter itself will still be running on a single CPU core. :-( Python's threads are native system threads. They can ru

Re: [Tutor] Moving my C++ code to Python?

2010-10-16 Thread Stefan Behnel
Paul, 15.10.2010 23:29: I have a software written in C/C++ but considering porting most of it to python, as it seems like it's a better choice for decision making portion of the code. Write a wrapper first. I'm also thinking about having a 'matlab' like interface for reading, processing, and

Re: [Tutor] Moving my C++ code to Python?

2010-10-17 Thread Stefan Behnel
Paul, 17.10.2010 23:29: Thank you all for your kind suggestions. It seem that cython and boost.python both look like a good suggestions; SWIG seems like a good tool, but may not meet my requirements. Quick google seems to suggest that boost.python is 7 times slower [1], however. Any thoughts ab

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Alan Gauld, 08.11.2010 17:28: "Steven D'Aprano" wrote def proper_divisors(n): return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0) Why use math.sqrt() instead of just using the ** operator? return sum(x for x in range(1, int(n**0.5)) if n%x == 0) I'd have expected ** to be sign

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Hugo Arts, 08.11.2010 00:53: On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote: def proper_divisors(n): """ Return the sum of the proper divisors of positive integer n """ return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function i

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 06:31: On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano wrote: Richard D. Moores wrote: Coming back to your function: def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ we can write that much

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 12:07: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in about a 2x speedup. See. NO! Use int(n/2)+1 . I'll correct that in

Re: [Tutor] Columnar Transposition Cipher question

2010-11-09 Thread Stefan Behnel
Steven D'Aprano, 09.11.2010 05:01: http://pypi.python.org/pypi/obfuscate Hmm - why is the Windows installer on that page called "linux-i686"? Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.p

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 10.11.2010 08:24: def proper_divisors_sum(n): pd = set((1,)) for x in range(2, int(n**.5)+1): if n % x == 0: pd.update((x, n//x)) return sum(list(pd)) You keep using redundant operations. What "sum(list(s))" does, for s being a set, is:

Re: [Tutor] List comprehension question

2010-11-11 Thread Stefan Behnel
Steven D'Aprano, 12.11.2010 06:07: Richard D. Moores wrote: On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano wrote: P.S. don't take that as a put down -- you should be pleased that your code is around as fast as Tim Peter's code :) Nah. But where is Tim Peter's code? The timeit module was w

Re: [Tutor] Tutor Digest, Vol 81, Issue 35

2010-11-12 Thread Stefan Behnel
Wangolo Joel, 12.11.2010 15:45: I NO LONGER WANT YOU MESSAGES DON'T REPLY ME Nice try. Stefan PS: You can unsubscribe at any time by following the instructions on the mailing list web site. See the footer of any message sent to this list. ___ Tut

Re: [Tutor] Issues Parsing XML

2009-03-12 Thread Stefan Behnel
m...@marcd.org wrote: > I am new to Python and as a first project decided to try to parse an XML > report using Python. I have the following, which works to extract one > element. I am stuck, however, at one element. I want to extract several > differenct elements per line, creating a comma sepa

Re: [Tutor] Parse XML file

2009-03-15 Thread Stefan Behnel
Lukas Agrapidis wrote: > I am trying to parse an XML file using Python and found this resource > http://diveintopython.org/xml_processing/parsing_xml.html You might be interested in ElementTree: http://effbot.org/zone/element.htm It's a lot easier to use than minidom, especially for new users.

Re: [Tutor] Using C in python

2009-03-25 Thread Stefan Behnel
amit sethi wrote: > what are the ways in which i can use C in python programs . Here is a short example that uses Cython to call a couple of C functions in OpenGL and libc ("math.h"). The functions are declared in the "cdef extern" blocks at the top. http://misc.slowchop.com/misc/browser/muckarou

Re: [Tutor] Trouble Parsing XML using lxml

2009-04-06 Thread Stefan Behnel
m...@marcd.org wrote: > I am trying to parse a structure that looks like: > > {urn:FindingImport}TOOL - GD > {urn:FindingImport}TOOL_VERSION - 2.0.8.8 > {urn:FindingImport}AUTHENTICATED_FINDING - TRUE > {urn:FindingImport}GD_VUL_NAME - Rename Built-in Guest Account > {urn:FindingImport}GD_SEVERITY

Re: [Tutor] BeautifulSoup confusion

2009-04-09 Thread Stefan Behnel
Steve Lyskawa wrote: > I am not a programmer by trade but I've been using Python for 10+ years, > usually for text file conversion and protocol analysis. I'm having a > problem with Beautiful Soup. I can get it to scrape off all the href links > on a web page but I am having problems selecting sp

Re: [Tutor] Python and Semantic web

2009-04-18 Thread Stefan Behnel
mbikinyi brat wrote: > > > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Uhm, what about "free and beer"? That woul

Re: [Tutor] Python logging

2009-04-19 Thread Stefan Behnel
Hi, just commenting on this part: Scott SA wrote: > [handler_warn_logfile] > class=handlers.RotatingFileHandler > level=WARNING > formatter=DatetimeLevelMessage > args=('test_warn.log', 'a', 125829120, 5) > filename=test_warn.log > mode=a > > ... which basically says

Re: [Tutor] finding mismatched or unpaired html tags

2009-04-28 Thread Stefan Behnel
A.T.Hofkamp wrote: > Dinesh B Vadhia wrote: >> I'm processing tens of thousands of html files and a few of them >> contain mismatched tags and ElementTree throws the error: >> >> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >> line 124, column 8" >> >> I now want to scan each

Re: [Tutor] Dictionary, integer, compression

2009-04-29 Thread Stefan Behnel
Dinesh B Vadhia wrote: > Say, you have a dictionary of integers, are the integers stored in a > compressed integer format or as integers ie. are integers encoded before > being stored in the dictionary and then decoded when read? Integer objects are not special cased in dictionaries. They are stor

Re: [Tutor] Code Dosent work.

2009-05-03 Thread Stefan Behnel
Jacob Mansfield wrote: > hi everyone, I'm a bit new here but i was wondering if someone could check > some of my code, it's not doing quite what it's meant to. As a general remark: it's a good idea to add a note on what your code is supposed to do, and in what way it behaves unexpectedly, i.e. wha

Re: [Tutor] Parsing Question

2009-05-10 Thread Stefan Behnel
Emile van Sebille wrote: > On 5/9/2009 5:30 PM Alan Gauld said... > >> You should probably be able to do the first for loop as a list >> comprehension, but I can't think of how to get the split() call >> embedded into it right now! >> > > Just for fun -- given datafile contents... > > 1 one 2 t

Re: [Tutor] t = (1, *(2, 3))

2009-05-14 Thread Stefan Behnel
Jabin Jezreel wrote: > I am not allowed to do t = (1, *(2, 3)) > > But I am allowed to do def ts(*t): > ...return t > ... ts(1, *(2, 3)) > (1, 2, 3) > > I realize I can do (1,) + (2,3) > (1, 2, 3) > > What is the rationale behind not having t = (1, *(2, 3)) > have the sam

Re: [Tutor] Spell checking source code?

2009-06-01 Thread Stefan Behnel
Alan Gauld wrote: > > "Allen Fowler" wrote > >> Are there any utilities to help "spell check" source code? >> (Docstrings, etc) > > I used to have an emacvs script that could do it for C/C++ using > the vanilla Unix spell program. Unfiortunately I can't remember its name > but a hunt around the

Re: [Tutor] Fw: unicode, utf-8 problem again

2009-06-04 Thread Stefan Behnel
Dinesh B Vadhia wrote: > Hi! I'm processing a large number of xml files that are all declared as > utf-8 encoded in the header ie. > > > > I'm using elementtree to process the xml files and > don't (usually) have any problems with that. Plus, the workaround that > works is to encode each eleme

Re: [Tutor] XML: changing value of elements

2009-06-10 Thread Stefan Behnel
Hi, it's funny how many times I see Python users go: "I have an XML problem, so I'll use minidom." Because then they have two problems. Johan Geldenhuys wrote: > I have a rather complex XML file and I need to change some values inside > this file. > > So far I have been using minidom, but I can

Re: [Tutor] reading and processing xml files with python

2009-06-21 Thread Stefan Behnel
Hi, python.l...@safe-mail.net wrote: > I am a total python XML noob and wanted some clarification on using python > with reading remote XML data. For XML in general, there's xml.etree.ElementTree in the stdlib. For remote data (and for various other features), you should also try lxml.etree, whi

Re: [Tutor] Urllib, mechanize, beautifulsoup, lxml do not compute (for me)!

2009-07-06 Thread Stefan Behnel
Hi, David Kim wrote: > I have two questions I'm hoping someone will have the patience to > answer as an act of mercy. > > I. How to get past a Terms of Service page? > > I've just started learning python (have never done any programming > prior) and am trying to figure out how to open or downloa

Re: [Tutor] int to bytes and the other way around

2009-07-06 Thread Stefan Behnel
Chris Fuller wrote: > The only things that matter are the arguments and the result. It sounds to > me > like a good case use for SWIG (http:://www.swig.org). You can do really > complicated stuff with swig, and it takes a correspondingly steep learning > curve to achieve, but doing simple stu

Re: [Tutor] browser encoding standards?

2009-07-13 Thread Stefan Behnel
Serdar Tumgoren wrote: > I just ran into a glitch that got me wondering about the proper > encoding to use when outputting data for non-technical folks. > > I took pains to ensure that I converted a number of XML feeds to UTF-8 > prior to storage in a database. And then, when pulling the feed data

Re: [Tutor] XML: changing value of elements

2009-07-16 Thread Stefan Behnel
Hi, a new question usually merits a new thread. Especially after a longer time, replies to older threads tend to remain unread as people simply don't scroll down far enough to notice them. You were lucky. :) Johan Geldenhuys wrote: > I have another question about writing the xml tree to a file.

Re: [Tutor] XML: changing value of elements and writing to a file

2009-07-16 Thread Stefan Behnel
Johan Geldenhuys wrote: > Thanks Stefan (decided to continue with a new thread name), ... which isn't quite enough. As long as you reply to the mail, e-mail/news readers will still sort it into the original thread, so many people will not see it. > I basically wants to create a loop that creates

Re: [Tutor] weather scraping with Beautiful Soup

2009-07-17 Thread Stefan Behnel
Che M wrote: > > > West of Town, Jamestown, Pennsylvania > (PWS) > Updated: pwsid="KPAJAMES1" pwsunit="english" pwsvariable="lu" value="1247814018">3:00 > AM EDT on July 17, 2009 > > > > >

Re: [Tutor] update html pages using python

2009-08-30 Thread Stefan Behnel
Alan Gauld wrote: > "pedro" wrote >> Hi, I was wondering if anyone could point me in the right direction as >> far as the best way to use python to update html. > > There are a number of modules in the standard library that can help but > the best known module for this is BeautifulSoup I would

Re: [Tutor] update html pages using python

2009-08-31 Thread Stefan Behnel
Alan Gauld wrote: > "Stefan Behnel" wrote >>> "pedro" wrote >>>> Hi, I was wondering if anyone could point me in the right direction as >>>> far as the best way to use python to update html. >>> >>> There are a numb

Re: [Tutor] New to python: some advises for image processing tool

2009-10-02 Thread Stefan Behnel
Nicola De Quattro wrote: > So I've to open an image (various formats, first I could need only > .fits), to process the image in order to select the interesting strip > containing the star and the spectrum (first image of the link posted, > but I hope I can select the strip not only in the horizonta

Re: [Tutor] Memory usage

2009-10-07 Thread Stefan Behnel
Lizhi Yang wrote: > Confused. If I create some functions using C++ to load the data into > memory and use python to call those functions, what is the memory > usage then? First (obvious) question: how do you call those functions? Do you use ctypes? Cython? Some other way? Providing a short code s

  1   2   3   >