0

I've written what I thought was a simple python script to search through several lines of output and match (i.e. "grep") on a specific string. Listing all queues without the pattern match is simple enough:

from qmf.console import Session
sess = Session()
broker = sess.addBroker("amqp://guest/guest@localhost")
queues = sess.getObjects(_class="queue", _package="org.apache.qpid.broker")
for q in queues:
  print (q)

Running the script produces the following output (truncated):

pmena@myhost=> python ./queue_stuff.py
org.apache.qpid.broker:queue[0-1-1-0-62] 0-1-1-0-3:queue01
org.apache.qpid.broker:queue[0-1-1-0-55] 0-1-1-0-3:queue02
org.apache.qpid.broker:queue[0-1-1-0-63] 0-1-1-0-3:queue03
org.apache.qpid.broker:queue[0-1-1-0-51] 0-1-1-0-3:queue04
.
.
org.apache.qpid.broker:queue[0-1-1-0-51] 0-1-1-0-3:queue99

However when I add an "if" statement to match on a particular string, like so:

from qmf.console import Session
sess = Session()
broker = sess.addBroker("amqp://guest/guest@localhost")
queues = sess.getObjects(_class="queue", _package="org.apache.qpid.broker")
for q in queues:
  if 'queue37' in q:
    print (q)

I get the following error:

pmena@myhost=> python ./queue_stuff.py
Traceback (most recent call last):
  File "./queue_stuff.py", line 6, in <module>
    if 'queue37' in q:
TypeError: argument of type 'Object' is not iterable

I feel like this is a simple python syntax issue, but wasn't able to glean the resolution from other posts.

PAUL MENA
  • 107
  • 1
  • 14
  • 4
    `q` appears to be a class that just has a `__repr__` method to display as a string. Try `if 'queue37' in repr(q):` or `if 'queue37' in str(q):`. The object may have an id value that you can check too, which would likely be faster. – Carcigenicate Apr 21 '20 at 19:26
  • 1
    @Carcigenicate typed faster ... `getObjects` returns *objects*, not strings. When you asked whether a string was in the object, Python decided you were trying to do an element check on something iterable -- hence the error message. If you're confused, print out the type and structure of `q`. Better yet, check the documentation on these objects, so that you can extract the needed attribute to compare against *that*. – Prune Apr 21 '20 at 19:29
  • 1
    Did you try reading the documentation? Specifcially, for the queue object? Also, it would help to explain *specifically what problem you are trying to solve* with this `if` condition. – Karl Knechtel Apr 21 '20 at 19:30

2 Answers2

1

The problem is that the Queue class does not have an iter method like you might suspect. That method needs to be defined for a class for for i in object: to work. This answer goes over a variety of workarounds that people have used, so you can see which one best fits your needs.

LTheriault
  • 1,180
  • 6
  • 15
0

Seems like q is Object instead of a string. You should check

for q in queues:
    print (type(q))

if q is object you cannot use if 'queue37' in q

RahulAN
  • 97
  • 1
  • 1
  • 7
  • It looks like @PAUL MENA's code is intending to loop through an object and not a string. The problem is that the Queue object doesn't have an __iter__ method like people may expect it to. – LTheriault Apr 21 '20 at 19:40
  • Exactly! Q is not a string rather it is some class object. – RahulAN Apr 22 '20 at 05:26