Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't iterate over a list class in Python
    primarykey
    data
    text
    <p>I'm trying to write a simple GUI front end for Plurk using pyplurk.</p> <p>I have successfully got it to create the API connection, log in, and retrieve and display a list of friends. Now I'm trying to retrieve and display a list of Plurks. </p> <p>pyplurk provides a GetNewPlurks function as follows:</p> <pre><code> def GetNewPlurks(self, since): '''Get new plurks since the specified time. Args: since: [datetime.datetime] the timestamp criterion. Returns: A PlurkPostList object or None. ''' offset = jsonizer.conv_datetime(since) status_code, result = self._CallAPI('/Polling/getPlurks', offset=offset) return None if status_code != 200 else \ PlurkPostList(result['plurks'], result['plurk_users'].values()) </code></pre> <p>As you can see this returns a PlurkPostList, which in turn is defined as follows:</p> <pre><code>class PlurkPostList: '''A list of plurks and the set of users that posted them.''' def __init__(self, plurk_json_list, user_json_list=[]): self._plurks = [PlurkPost(p) for p in plurk_json_list] self._users = [PlurkUser(u) for u in user_json_list] def __iter__(self): return self._plurks def GetUsers(self): return self._users def __eq__(self, other): if other.__class__ != PlurkPostList: return False if self._plurks != other._plurks: return False if self._users != other._users: return False return True </code></pre> <p>Now I expected to be able to do something like this:</p> <pre><code>api = plurk_api_urllib2.PlurkAPI(open('api.key').read().strip(), debug_level=1) plurkproxy = PlurkProxy(api, json.loads) user = plurkproxy.Login('my_user', 'my_pass') ps = plurkproxy.GetNewPlurks(datetime.datetime(2009, 12, 12, 0, 0, 0)) print ps for p in ps: print str(p) </code></pre> <p>When I run this, what I actually get is:</p> <pre><code>&lt;plurk.PlurkPostList instance at 0x01E8D738&gt; </code></pre> <p>from the "print ps", then:</p> <pre><code> for p in ps: TypeError: __iter__ returned non-iterator of type 'list' </code></pre> <p>I don't understand - surely a list <em>is</em> iterable? Where am I going wrong - how do I access the Plurks in the PlurkPostList?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload