#35 Add grep string to cmd.get_names


I think a simple and useful extension to cmd.get_names() would be a pattern or grep string to filter the names. We could then make a call like,

cmd.get_names("all", grep="ligand")

to only get the objects names "ligand" back.


  • Jason Vertrees

    Jason Vertrees - 2009-02-23

    nb. This would also be useful in a for-loop situation,

    for ligOnly in cmd.get_names("objects", grep="lig"):
    # doStuff...

  • Thomas Holder

    Thomas Holder - 2011-06-30

    this already works like this:


  • Thomas Holder

    Thomas Holder - 2013-08-01
    • status: open --> closed
    • Group: -->
  • Thomas Holder

    Thomas Holder - 2013-08-01

    This is already available with the selection argument, but special attention is required for object vs. atom selections. This will not properly evaluate object patterns, only atom selections. This can be achieved by parenthesis or with the "model" operator.

    This will fail (only returns first matching name):

    cmd.get_names('all', selection='lig*')

    This works:

    cmd.get_names('all', selection='model lig*')
    cmd.get_names('all', selection='(lig*)')

    The latter might include object names which do not match "lig*" themselves, but have atoms in a named selection which matches "lig*".

    For a strict name "grepping", I suggest a pythonic solution:

    [name for name in cmd.get_names('all') if 'lig' in name]