Posts Tagged python

Passing a dictionary to a dynamicly generated XMLRPC function

Working on some python code for some automation around the house and had the need to be able to pass dictionary object to a function name that was dynamically generated by the input to the script.

Normally this is simple using the xmlrpclib module that you can install for python. All you need to do is pass in the dictionary object into the function and make sure that the function that you write on the XMLRPC server is looking for a dictionary object.

This is how you would pass in a dictionary object to a known XMLRPC function:

s = xmlrpclib.ServerProxy('http://localhost:8000')
dictionary = []
api_call = s.function(dictionary)
return api_call

When you don’t know the function name it is recommended that you use the getattr method as it is quicker, but you cannot pass in a dictionary object into it. So the only solution I found to do this is to use the eval method in the way I did bellow:

s = xmlrpclib.ServerProxy('http://localhost:8000')
dictionary = []
function = 'function name'
api_call = eval('s.%s(dictionary)' % function)
return api_call

I would love to hear any improvements to the way I am doing this if you have any. Please leave a comment.

Tags: ,

Reading RSS with python

I have been working on a way that I can get my podcasts downloaded and sync’d to my mp3player by my home server and it was interesting how simple it is to do with Python.

I have been using feedparser module which is very simple to get Fedora users are able to simply install this via yum the package is

python-feedparser

.

If you are not on fedora check you package manager or down load the module from http://www.feedparser.org

Using feedparser is a simple task. First import the module and load your feed into your script like so:

#! /bin/env python
import feedparser 

# Lets import the feed of computer related posts from puredistortion.com.
feed = feedparser.parse('http://puredistortion.com/category/compstuff/feed/')

From here you are able to pull any data that you need from the feed. Feedparser formats the RSS feed into lists ad dictionaries to make working your way through the information a simple task.

For more information on how to extract the data from the feed once you have imported your RSS feed into your script have a look at http://www.feedparser.org/docs.

Or download a script that was the basis for what I am using to download podcasts to my home server here : get_rss.py (87)

Tags: ,