Re: [Tutor] Avoiding repetetive pattern match in re module
At 02:41 AM 1/5/2006, Intercodes wrote: Hello everyone, Iam new to this mailing list as well as python(uptime-3 weeks).Today I learnt about RE from http://www.amk.ca/python/howto/regex/.This one was really helpful. I started working out with few examples on my own. The first one was to collect all the HTML tags used in an HTML file. I wrote this code. -- import re file1=open(raw_input(\nEnter The path of the HTML file: ),r) ans= while 1: data=""> if data=""> break ans=ans+data Consider a shorter way to grab the entire file: ans = open(raw_input(\nEnter The path of the HTML file: ),r).read() [snip] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Further help needed!
Notepad opens and prints the text file. Regards, John. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of John Fouhy Sent: 04 January 2006 21:54 To: Tutor Subject: Re: [Tutor] Further help needed! [resending 'cause I forgot to address to tutor..] On 05/01/06, John Corry [EMAIL PROTECTED] wrote: This code works on windows XP + Windows 2000. However it does not work on windows 98SE. I have tried this code on 3 seperate machines with windows 98SE. They all come up with the same error: File C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py, line 310, in RunScript exec codeObject in __main__.__dict__ File C:\test\Script1.py, line 12, in ? 0 error: (31, 'ShellExecute', 'A device attached to the system is not functioning.') Have you verified that the printer on your Win98 machine does work? For example, create a text file on that computer. In Windows Explorer, right-click on the text file and select 'print' from the context menu. What happens? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding repetetive pattern match in re module
Instead of writing a new 'for,if' loop to filter the repetetive tags from the list, is there something that I can add in the re itself to match the pattern only once? Hi Intercodes, As far as I know, no: regular expressions don't have the capacity to remember what tags they've matched in previous matching runs. You may find sets useful in filtering out duplicates: http://www.python.org/doc/lib/module-sets.html For example: ## from sets import Set as set set([3, 1, 4, 1, 5, 9, 2, 6]) Set([1, 2, 3, 4, 5, 6, 9]) ## So you should not need to code much extra logic to do the duplication filtering: sets do this for you already. As an aside, I want to second Alan's recommendation to avoid doing so much string concatenation: it's actually very expensive to do the following: # ans = '' while 1: data=file1.readline() if data==: break ans=ans+data # From a technical standpoint, it has quadratic complexity in terms of what work the computer is doing. It's related to the mathematical idea that 1 + 2 + 3 + 4 + ... + n = n(n+1). http://en.wikipedia.org/wiki/Triangle_number The string concatenation that the code does above is analogous because it rebuilds larger and larger strings in a similar fashion. Alan's approach, to use file1.read(), sucks up the string in one shot, so it's much less computationally expensive. If you do end up having to concatenate a lot of strings together, use the join() method of lists instead. We can talk about this in more detail if you'd like. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding repetetive pattern match in re module
From a technical standpoint, it has quadratic complexity in terms of what work the computer is doing. It's related to the mathematical idea that 1 + 2 + 3 + 4 + ... + n = n(n+1). http://en.wikipedia.org/wiki/Triangle_number Gaa, where did my division sign go? *grin* Sorry, that equation should be: 1 + 2 + ... + n = n(n+1)/2 We can see this: ### import operator def sumUpTo(n): ... return reduce(operator.add, range(n+1)) ... for i in range(10): ... print sumUpTo(i), 'vs', i*(i+1) / 2 ... 0 vs 0 1 vs 1 3 vs 3 6 vs 6 10 vs 10 15 vs 15 21 vs 21 28 vs 28 36 vs 36 45 vs 45 ### If you do end up having to concatenate a lot of strings together, use the join() method of lists instead. I should clarify this. I meant to say we should use the string method of join() on lists. For example, if we didn't have file1.read(), we could still do: ### ans = ''.join(file1.readlines()) ### which takes the lines of the file and joins them altogether at once. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] string to integer
I need program to convert my string in integer. I remember in BASIC I used val(string) command Is there a Python equivalent ? Here is how it should work: val(7) = 7 val(bbab7) = 7 val(aa7aa) = 7 val( 7) = 7 This last is most important, currently I don't know how to convert string7 to integer value 7 in my program (those empty spaces are nasty) btw, I'm working with random numbers, I took 7 just for example :) Also, what are chr() values for enter and (empty) space ? If anybody have a table with chr() values, I'd appreciate if he upload it somewhere. Thanks in advance ! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding repetetive pattern match in re module (fwd)
-- Forwarded message -- Date: Fri, 6 Jan 2006 01:53:41 +0530 From: Intercodes [EMAIL PROTECTED] To: Danny Yoo [EMAIL PROTECTED] Subject: Re: [Tutor] Avoiding repetetive pattern match in re module Hello Danny, Thanks for the response. The read() and join() method worked like charm. I couldn't tune the sets.Set() though. It eliminated duplicates from the list, but I couldn't find a method in sets to print only the set data ( instead I get Set([data1,data2]) when a set object is printed ) Actually,I have to code an compiler for javascript , so Iam working on the basic stuff of parsing,re's and other stuff. I thought re's could do everything ;) --- Intercodes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Disabling a frame in Tkinter
Hi, Is there any way to disable an entire frame (and all its included widgets) in Tkinter. It does not seem to support state=DISABLED. Another Tkinter question: In a listbox, how do I intially set a selected item? What I want to do here is have the 1st item in a listbox selected (ie highlighted) when the appln is run. Cheers Hans ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to integer
On Thu, 5 Jan 2006, Boyan R. wrote: I need program to convert my string in integer. I remember in BASIC I used val(string) command Is there a Python equivalent ? Here is how it should work: val(7) = 7 val(bbab7) = 7 val(aa7aa) = 7 val( 7) = 7 Hi Boyan, Python has a function that's somewhat similar called the int() function. http://www.python.org/doc/lib/built-in-funcs.html#l2h-39 However, it's a bit more strict about its input than what you may be used to: ## int( 5) 5 int(5th) Traceback (most recent call last): File stdin, line 1, in ? ValueError: invalid literal for int(): 5th ## To make something like val, we can try to pull out the first bunch of digits we see, and int()ify that. Here's one example of this approach: ### import re def val(s): Tries to extract the first integer value we can see from string s. result = re.search(r\d+, s) if not result: raise ValueError, (invalid literal for val(): %s % s) return int(result.group(0)) ### Let's see how this works: ## val(5th) 5 val(this is 1derful) 1 val(huh?) Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 6, in val ValueError: invalid literal for val(): huh? ## Also, what are chr() values for enter and (empty) space ? If anybody have a table with chr() values, I'd appreciate if he upload it somewhere. Thanks in advance ! I think you're asking: what's the ordinal values that we input into chr() to get the enter and space characters? If so: do you know about the ord() builtin function? http://www.python.org/doc/lib/built-in-funcs.html#l2h-53 Play around with it, and I think you should be able to get the values you are looking for. If you have more questions, please feel free to ask. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to integer
At 12:40 PM 1/5/2006, Boyan R. wrote: I need program to convert my string in integer. I remember in BASIC I used val(string) command Is there a Python equivalent ? Here is how it should work: val(7) = 7 val(bbab7) = 7 val(aa7aa) = 7 val( 7) = 7 This last is most important, currently I don't know how to convert string7 to integer value 7 in my program (those empty spaces are nasty) btw, I'm working with random numbers, I took 7 just for example :) int(7) - 7 int( 7) - 7 int(aa7aa) -ValueError: invalid literal for int(): aa7aa. You'd need to remove the non-digits using replace or re.sub. what are chr() values for enter and (empty) space ? If anybody have a table with chr() values Are you asking what numeric value passed to chr() gives Enter (etc)? If so consult any ASCII Chart. One is at http://www.lookuptables.com/. Of course Enter in ASCII is CR. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding repetetive pattern match in re module (fwd)
On 05/01/06, Danny Yoo [EMAIL PROTECTED] wrote: -- Forwarded message --Date: Fri, 6 Jan 2006 01:53:41 +0530From: Intercodes [EMAIL PROTECTED]To: Danny Yoo [EMAIL PROTECTED]Subject: Re: [Tutor] Avoiding repetetive pattern match in re moduleHello Danny,Thanks for the response. The read() and join() method worked like charm. Icouldn't tune the sets.Set() though. It eliminated duplicates from the list,but I couldn't find a method in sets to print only the set data ( instead Iget Set([data1,data2]) when a set object is printed ) How about this s = set([1,2,2,3,4,3,3,5]) list(s)[1, 2, 3, 4, 5]Then it just prints out like a list. Also if the objects in the list are strings you can use the .join method to turn it into a string. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to integer
Here is how it should work: val(7) = 7 val(bbab7) = 7 val(aa7aa) = 7 val( 7) = 7 There is no direct equivalent for val() as shpwn above the nearest is int() or float() but that will only work with the first and last examples. (It is a pretty arbitrary functionn to convert aa7aa to 7!) What does val do with a6b5c? You could strip all chars out of the string first: nums = [ch for ch in aString if ch in string.digits] val = int(str(nums)) Which would result in 65 for the example I gave... and that might be what you expect? This last is most important, currently I don't know how to convert string7 to integer value 7 in my program Just use int() Also, what are chr() values for enter and (empty) space ? Why would you need chr if you know its an empty space? Just use ' ' directly. if ch = ' ': 'enter' is more tricky since it depends on your OS and to some extent your terminal type. For example a TekTronics terminal in an xterm window on Linux gives a different value for Enter than the default vt100 terminal... Again, if we knew why you needed to know we might be able to suggest a suitable mechanism otr alternate technique? If anybody have a table with chr() values, I'd appreciate if he upload it somewhere. Just do a google search for ASCII that should find several tables of the value/character combinations. Searching for Unicode will likewise find the Unicode values. chr() is just the string representation of a number. n = ord(c) print chr(ord(c)) should print c since ord() convers the character to a number and chr() converts it back... for n in range(256): print chr(n) will print all of them (although some are unprintable!) HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to integer
Thank you both I think I'll manage to continue with my program :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Excel files to Tab delim files
Dear group, is there any library available that would convert over 2000 .xls files to tab delim text files. I have over 2000 Excel files and I want to convert them to tab delim files, which has become a pain in brain. Thanks Srini __ Yahoo! DSL Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Excel files to Tab delim files
On 06/01/06, Srinivas Iyyer [EMAIL PROTECTED] wrote: Dear group, is there any library available that would convert over 2000 .xls files to tab delim text files. Check out pyExcelerator: http://sourceforge.net/projects/pyexcelerator The documentation is a bit light, but basically, if you do: import pyExcelerator workbook = pyExcelerator.parse_xls('myFile.xls') then workbook will be a data structure looking like [(sheet name, { (row, col): data })] ie: a list of tuples, where the first element is the sheet name (eg, u'Sheet 1', u'Sheet 2') and the second element is a dictionary whose keys are (int, int) pairs representing cells (where A1 is (0, 0)) and whose values are the values of the cells. From that, you should be able to easliy produce a tab delimited file (or anything else you want). The only issue I have had with it is that it reads integers as strings, but I haven't looked at the latest version so this may be fixed, and if you're just writing out to CSV file that won't matter anyway. [be aware, though, that any dates in the spreadsheet will turn into integers] -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Disabling a frame in Tkinter
On 06/01/06, Hans Dushanthakumar [EMAIL PROTECTED] wrote: Hi, Is there any way to disable an entire frame (and all its included widgets) in Tkinter. It does not seem to support state=DISABLED. Not that I'm aware of... You could try maybe something like this (untested): def setState(widget, state=DISABLED): try: widget.config(state=state) except TclError: # not sure what exception will be raised pass for child in widget.children: setState(child, state=state) Another Tkinter question: In a listbox, how do I intially set a selected item? What I want to do here is have the 1st item in a listbox selected (ie highlighted) when the appln is run. Have a look at Fredrik Lundh's Introduction to Tkinter. You can use .selection_clear() to clear the selection and .selection_set(index) to select an item. So, .selection_set(0) will select the first item (just make sure the listbox isn't empty). -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Excel files to Tab delim files
From that, you should be able to easliy produce a tab delimited file (or anything else you want). There's even a 'csv' module in Python's Standard Library to handle some of the awkward cases in generating tab-delimited data: http://www.python.org/doc/lib/module-csv.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Syntax Error
Hi all, I'm working on a little script, but every time that I run it, it comes back as a syntax error around line 24. However, I can't find anything wrong with it. I figure that maybe I just need another set of eyes. Please take a look at this code, and let me know if you can find anything wrong with it that would cause a syntax error? Thanks in advance! Kris Kerwin Script - #!/usr/bin/python import commands import string import os os.chdir('/home/kris/Mail/.inbox.directory') file_string = commands.getoutput('tree -afi') new_file_string = string.replace(file_string, ' ', '_SPACE_') file_list = string.split(new_file_string) # Pop of leading '.' directory. file_list.pop(0) # Pop of directory stats. file_list.pop() index = 1 while index (len(file_list) + 1): file_name = file_list.pop() new_file_name = string.replace(file_name, '_SPACE_', ' ') unix_name = string.replace(new_file_name, ' ', '\ ' file_type = commands.getoutput('file %s' % unix_name) # Line 24 if file_type == '%s: ASCII mail text, with very long lines' % new_file_name: file_contents = commands.getoutput('cat %s' % unix_name) print file_contents index = index + 1 else: index = index + 1 os.chdir('/home/kris/bin/gmail_export/') dircat Description: application/python pgpV7rqku8bBZ.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax Error
On 06/01/06, Kris Kerwin [EMAIL PROTECTED] wrote: unix_name = string.replace(new_file_name, ' ', '\ ' Well, there seems to be a closing parenthesis missing on this line :-) By the way --- starting with python2.2 (?), most of the functions in the string module are deprecated in favour of calling methods on strings themeslves. So, instead of saying: new_file_string = string.replace(file_string, ' ', '_SPACE_') You would say: new_file_string = file_string.replace(' ', '_SPACE_') HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python upgrade from 2.3 to 2.4
Hi, I might be asking this question out of context. however, eversince I started working with Python, the only mailing list that I used ever is this. So this could be also the other reason that I am posting here. I am currently using Python 2.3 for windows XP. I wanted to use PyExcellerator module. However, this module need the Python to be 2.4. So how should I upgrade from 2.3 to 2.4 without loosing the neat libraries and modules that I have been using on 2.3. these python modules and libraries are backbone for my programming skills. so how could I upgrade without loosing theh binaries and modules (eg. BioPython module, EUtils etc. I downloaded the 2.4 MSI and it was installed in Pyhthon2.4 directoryu and when I started Python2.4 windows GUI, it never came up. I do not the reason. Any suggestion and help please. .. thanks Srini __ Yahoo! DSL Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to integer
Alan Gauld said unto the world upon 05/01/06 04:16 PM: Here is how it should work: val(7) = 7 val(bbab7) = 7 val(aa7aa) = 7 val( 7) = 7 There is no direct equivalent for val() as shpwn above the nearest is int() or float() but that will only work with the first and last examples. (It is a pretty arbitrary functionn to convert aa7aa to 7!) What does val do with a6b5c? You could strip all chars out of the string first: nums = [ch for ch in aString if ch in string.digits] val = int(str(nums)) snip I'd spell that nums = [ch for ch in aString if ch.isdigit()] Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor