Python equivalent of Common Lisp Macros?

J Kenneth King james at agentultra.com
Wed Nov 26 15:00:50 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

No macros -- need the whole "code as data" paradigm which doesn't exist
in python.

The pythonic way to create the sort of abstraction you're looking for
would be to use the *attr functions and either a map of attribute
identities or inspect the attributes from the objects.

Some UNTESTED code...

def extract_entry(e, attrs=['title', 'datetime', 'article']):
    out = []
    for attr in attrs:
        assert hasattr(e, attr)
        out.append(getattr(e, attr))
    return out

Using the inspect module would give an even more generic function, but
with more complex code of course.

Then your code would look more like:

def items(feed_url):
    feed = feedparser.feed(feed_url)
    if feed and len(feed) > 0:
        return [extract_entry(entry) for entry in feed.entries]

Of course, I'm making liberal assumptions about what you're looking for
in your ouput here... but it's just an example, not a full
solution. YMMV. Hope it helps. :)



More information about the Python-list mailing list