Another nntplib Question
Thomas Jollans
thomas at jollans.com
Wed Jun 9 19:36:47 EDT 2010
On 06/10/2010 12:56 AM, Anthony Papillion wrote:
> Hello Everyone,
>
> Thanks to help from this group, my statistical project is going very
> well and I'm learning a LOT about Python as I go. I'm absolutely
> falling in love with this language and I'm now thinking about using it
> for nearly all my projects.
>
> I've run into another snag with nntplib I'm hoping someone can help me
> with. I'm trying to get a list of newsgroups from the server. The
> documentation for the list() method tells me it returns a response
> (string) and a list (tuple).
>
> The list tuple contains the following: groupname, last, first, flag.
> So, thinking I could access the group name in that list like this
> ThisGroup = groupname[1].
>
> Now, along those lines, I am wanting to retrieve some information
> about each group in the list so I thought I could do this:
>
Hi again ^^
Let's have a look at what your code actually does. I'm not going to
check the docs myself just now.
> resp, groupinfo = server.list()
okay, looks fine. so groupinfo would, as per what you said earlier, be
in this format:
[ (groupname1, last1, first1, flag1),
(groupname2, last2, first2, flag2),
... ]
> group = (info[1] for info in groupinfo)
Here, you're using a generator expression to create a generator object
which will yield info[1] if you loop over it. So after this line you
could do this:
for x in group:
print (x)
and it would print every "last" field from above. info[1] is NOT the
first item in info, but the second one, since Python sequences are
zero-indexed. groupname would be info[0]. (it looks as though you might
have forgotten this)
> resp, count, first, last, name = server.group(group)
Here, you're passing the generator object to the server.group method. It
probably wasn't expecting that.
>
> But Python throws a TypeError and tells me I "can't concatenate str
> and generator objects'.
This almost certainly originated in the NNTPlib code. It expected the
object you passed (a generator object) to be a string and did something like
send("BEGINNING OF REQUEST LINE"+thing_I_got_passed)
which, obviously, can't work if you don't pass a string (the group name
is expected, I presume)
So, what you probably want to do would look something like this:
resp, groupinfos = server.list()
for info in groupinfos:
resp, count, first, last, name = server.group(info[0])
# do something with the info here.
>
> What am I doing wrong? I've been banging my head on this for a few
> hours and simply can't get it right. I've even googled for an example
> and couldn't find one that showed how to deal with this.
>
> Can anyone help?
>
> Thanks in advance!
> Anthony
More information about the Python-list
mailing list