import random


def get_framesets(cls, maximum=10, pattern=None):
    """ Gather FrameSet objects from either Frames or Bars.
        If `pattern` is set to a compiled regex pattern,
        return all FrameSets matching the pattern.
        Otherwise, return up to `maximum` random FrameSets.
    """
    frametypes = set()
    framenames = cls.names()
    if pattern is None:
        while len(frametypes) < maximum:
            frametypes.add(cls.get_by_name(random.choice(framenames)))
    else:
        frametypes.update(
            cls.get_by_name(s)
            for s in framenames
            if pattern.search(s) is not None
        )
    return frametypes