Fedora Linux Support Community & Resources Center
  #1  
Old 23rd February 2012, 12:49 AM
sbjaved Offline
Registered User
 
Join Date: Oct 2007
Location: Gujranwala, Pakistan
Posts: 46
linuxchrome
[Solved][Q][python] writing functions

I am learning python and need guidance for writing some code. I've written a simple program (with pointers from people) that parses an tv show xml feed and prints their values in plain text after performing some string operations.

Code:
feed = urllib.urlopen(rssPage) #rssPage: address of xml feed
tree = etree.parse(feed)
x = tree.xpath("/rss/channel/item/title/text()") 
x = str(x[0])
for tag in tags: #tags is a list of items like hdtv, xvid, 720p etc
	x = re.sub(r'\b' + tag + r'\b', '', x)
	z = re.sub(r'[^\w\s]', '', x)
y = tree1.xpath("/rss/channel/item/pubDate/text()")
print "%s - %s" %(z.rstrip(), y[0][:16])
The code works fine (prints the name of the show and date). Now since I am parsing more than one feed, I thought the better way was to split the functionality into diff functions: one to get the values and the other to remove the tags. I'm still *very* new to python and came up with the following code.

Code:
def get_value(feed):
	try:
		url = urllib2.urlopen(feed)
		tree = etree.parse(url)
		x = tree.xpath("/rss/channel/item/title/text()") 
		y = tree.xpath("/rss/channel/item/pubDate/text()")
		x = str(x[0])
		y = str(y[0][:16])
		return x
		return y
	except SyntaxError:
		print 'Service Unavailable'
		pass

def del_tag(x):
	tags = ['HDTV', 'LOL', 'VTV', 'x264', 'DIMENSION', 'XviD', '720P', 'IMMERSE', '720p', 'X264']
	for tag in tags:
		x = re.sub(r'\b' + tag + r'\b', '', x)
		y = re.sub(r'[^\w\s]', '', x)
	
def main():
	a = get_value(rssPage) 
	b = del_tag(a)
	print b
			
if __name__ == '__main__':
	main()
My desired working is to supply the xml feed address to the get_value function which returns the title and date as strings assigned to x and y. Then I run the del_tag function on the title string (x) and remove tags and the main() function prints both x and y (Title, Date).

Running this code returns None.

Let the teaching begin

Last edited by sbjaved; 23rd February 2012 at 03:33 AM. Reason: solved
Reply With Quote
  #2  
Old 23rd February 2012, 02:10 AM
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,052
linuxfirefox
Re: [Q][python] writing functions

You assigned "b" from del_tag(x) here:
Quote:
b = del_tag(a)
but your del_tag function doesn't return anything. In that situation, Python will assign "b" with "None". You need to change your del_tag() function to return something


PS. also, this code in your first function makes no sense:
Quote:
return x
return y
You can't have two returns like that, the return x will occur and leave the function and the 'return y' will never occur.
You could have it return a list of the two values:
Quote:
return [x,y]
or something similar. Your del_tag() would then have to be changed to deal with "a" as a list of two members

Last edited by marko; 23rd February 2012 at 03:00 AM.
Reply With Quote
  #3  
Old 23rd February 2012, 02:40 AM
sbjaved Offline
Registered User
 
Join Date: Oct 2007
Location: Gujranwala, Pakistan
Posts: 46
linuxchrome
Re: [Q][python] writing functions

Thank you. Changed print b to return b. There is no output.

If I change it to print a, the title of the show is printed. It seems the get_value() function is working fine but del_tag is not. hmmm.

---------- Post added at 07:40 AM ---------- Previous post was at 07:15 AM ----------

changed code to produce a single return:
Code:
return x + ' ' + y
also added:
Code:
def del_tag(x):
	tags = ['HDTV', 'LOL', 'VTV', 'x264', 'DIMENSION', 'XviD', '720P', 'IMMERSE', '720p', 'X264']
	for tag in tags:
		x = re.sub(r'\b' + tag + r'\b', '', x)
		y = re.sub(r'[^\w\s]', '', x)
                return y
Now print b prints out the title and date of the show...but no tags are being removed.

Last edited by sbjaved; 23rd February 2012 at 03:11 AM.
Reply With Quote
  #4  
Old 23rd February 2012, 03:10 AM
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,052
linuxfirefox
Re: [Q][python] writing functions

that return in del_tag() is in the wrong indentation, it stops the loop from going beyond the first cycle which certainly isn't what you want or you'd not have put in a loop

I'd really suggest you use meaningful variable names and not "a", "b" and "x"
Reply With Quote
  #5  
Old 23rd February 2012, 03:32 AM
sbjaved Offline
Registered User
 
Join Date: Oct 2007
Location: Gujranwala, Pakistan
Posts: 46
linuxchrome
Re: [Q][python] writing functions

YES! Thankyou. Its working
Reply With Quote
Reply

Tags
functions, python, qpython, writing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python importing modules in functions IHeart SymLinks Programming & Packaging 5 4th April 2011 09:24 PM
Error With Python / Python-Twisted-Web HMSS013 Using Fedora 6 11th November 2010 04:07 AM
Python high-precision for trig functions RupertPupkin Programming & Packaging 3 2nd September 2009 05:04 PM
Python, IDLE, Python-tools Gnome. Deonis Using Fedora 2 12th March 2009 03:44 AM
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Using Fedora 0 15th September 2005 05:53 PM


Current GMT-time: 04:26 (Wednesday, 24-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat