Main Content
Good example of recursion in Python - analyse an RSS feed Archive - Originally posted on "The Horse's Mouth" - 2009-11-18 18:19:13 - Graham Ellis
I'm not keen on recursive code - code that calls itself. Very often, such code is elegant in a way, yet so 'clever' that it is hard to follow. There are, however, exceptions where I say "THAT is a good use of recursion" . Once such is in the handling / parsing of RSS (Really Simple Syndication) feeds.
I was asked by one of my delegates about how he might download and analyse an RSS feed in Python ... and after a quick scout around, we downloaded feedparser . The code to grab the feed is silly-trivial:
import feedparser
stuff = feedparser.parse("http://www.wellho.net/horse/index.xml")
stuff , though, is a dictionary that contains other dictionaries, lists, and strings ... and those other dictionaries and strings contain further dictionaries and strings and lists. After all, RSS is an XML feed and the parse method give you back something synonymous with a DOM object.
So I have "pretty print"ed stuff using a function I wrote called display_dict which itself calls display_dict and display_list as appropriate. display_list itself calls display_list and display_dict . You can see the source at [link]
Some other articles
Y151 - Python & XML Searching a Json or XML structure for a specific key / value pair in Python XML handling in Python - a new teaching example using etree XML handling in Python - SAX, DOM and XSLT examples Bookkeeping This article Handling XML in Perl - introduction and early examples Y117 - Already written modules Searching a Json or XML structure for a specific key / value pair in Python Scons - a build system in Python - building hello world Month, Day, Year number to day of week and month names in Python - English and Swedish Programming with random numbers - yet re-using the same values for testing Binary data handling - Python and Perl Reading command line parameters in Python Cacheing class for Python - using a local SQLite database as a key/value store JSON from Python - first principles, easy example Practical Extraction and Reporting - using Python and Extreme Programming How can I do an FTP transfer in Python? A demonstration of how many Python facilities work together Syncronise - software, trains, and buses. Please! Dates and times in Python This article Learning Python - many new example programs Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? Embedding more complex code into a named block Nesting decorators Recursion in Python - the classic example What are callbacks? Why use them? An example in Python What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! This article Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python Conversion of OSI grid references to Eastings and Northings Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python Global - Tcl, PHP, Python Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python Python - function v method Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope