Another nntplib Question

  • Thread starter Anthony Papillion
  • Start date
A

Anthony Papillion

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:

resp, groupinfo = server.list()
group = (info[1] for info in groupinfo)
resp, count, first, last, name = server.group(group)

But Python throws a TypeError and tells me I "can't concatenate str
and generator objects'.

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
 
T

Thomas Jollans

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.
 
A

Anthony Papillion

Thomas,

You have been my savior on this journey twice now and I appreciate it.
What you did totally makes sense (and I had forgotten the list was
zero based) and I'm going to try it out right now. Thank you SO much
for your patience. I'm coming from a near pure .NET and PHP background
so I'm still running into little 'gotchas' with Python. On my way
though, I hope. lol

Thanks again!
Anthony
 
M

MRAB

Anthony said:
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].
What's a "list tuple"? It's a list of tuples.

In each of the tuples the name of the group is at index 0.
Now, along those lines, I am wanting to retrieve some information
about each group in the list so I thought I could do this:

resp, groupinfo = server.list()
group = (info[1] for info in groupinfo)

'group' will be a generator, not a list. If you want a list of the names
of the groups, try:

group_names = [info[0] for info in groupinfo]
resp, count, first, last, name = server.group(group)

But Python throws a TypeError and tells me I "can't concatenate str
and generator objects'.

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?
server.group expects the name of a single group, but you're giving it a
generator instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top