Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 10th, 2006, 3:13 AM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
There's also Beautiful Soup, which is supposed to be quite highly regarded.
Arevos is offline   Reply With Quote
Old Mar 10th, 2006, 6:27 AM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Yeah, I heard about Beautiful Soup, but not htmlib. I really don't want to import an entire parsing library when all I need to do is parse one kind of tag with one function.
Sane is offline   Reply With Quote
Old Mar 10th, 2006, 4:21 PM   #13
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
I'm sure there is an easier way (there always seems to be one in Python :p), but I made a little reverser function.

It will take either a string, tuple, or list and return the same data-type, reversed.

def reverse(ls):
	"""Reverses and return a string, list, or tuple."""
	res = [ls[len(ls) - x] for x in range(1, len(ls) + 1)]
	
	if types.StringType == type(ls):
		res = "".join(res)
	
	elif types.TupleType == type(ls):
		res = tuple(res)

	return res
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Mar 10th, 2006 at 4:51 PM.
Jessehk is offline   Reply With Quote
Old Mar 10th, 2006, 5:15 PM   #14
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Python 2.4 has the "reversed" function, which iterates over a sequence backward. So you could do this:
def reverse(s):
    r = reversed(s)
    if type(s) == list:  return list(r)
    if type(s) == tuple: return tuple(r)
    if type(s) == str:   return "".join(r)
Or even this:
def reverse(s):
    return { str : "".join, list : list, tuple : tuple }[type(s)](reversed(s))
Arevos is offline   Reply With Quote
Old Mar 10th, 2006, 5:19 PM   #15
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
I didn't know about the reversed() function.

*And I think that is the most clever use of dictionaries I have ever seen. :eek

Thanks Arevos
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC