Python equivalent of Common Lisp Macros?

Arnaud Delobelle arnodel at googlemail.com
Wed Nov 26 14:44:46 EST 2008


dpapathanasiou <denis.papathanasiou at gmail.com> writes:

> I'm using the feedparser library to extract data from rss feed items.
>
> After I wrote this function, which returns a list of item titles, I
> noticed that most item attributes would be retrieved the same way,
> i.e., the function would look exactly the same, except for the single
> data.append line inside the for loop.
>
> In CL, I could simply write a macro, then replace the data.append line
> depending on which attribute I wanted.
>
> Is there anything similar in Python?
>
> Here's the function:
>
> def item_titles (feed_url):
>     """Return a list of the item titles found in this feed url"""
>     data = []
>     feed = feedparser.parse(feed_url)
>     if feed:
>         if len(feed.version) > 0:
>             for e in feed.entries:
>                 data.append(e.title.encode('utf-8'))
>     return data

Something like this?

def item_attrs (feed_url, attr):
    """Return a list of the item titles found in this feed url"""
    data = []
    feed = feedparser.parse(feed_url)
    if feed:
        if len(feed.version) > 0:
            for e in feed.entries:
                data.append(getattr(e, attr).encode('utf-8'))
    return data

You're not making it clear how the data.append... line changes so it's
hard to know exactly.

-- 
Arnaud



More information about the Python-list mailing list