def get_classes(module, superclass=None):
    """
    Return a list of new-style classes defined in *module*, excluding
    _private and __magic__ names, and optionally filtering only those
    inheriting from *superclass*. Note that both arguments are actual
    modules, not names.

    This method only returns classes that were defined in *module*.
    Those imported from elsewhere are ignored.
    """

    objects = [
        getattr(module, name)
        for name in dir(module)
        if not name.startswith("_")]

    # filter out everything that isn't a new-style
    # class, or wasn't defined in *module* (ie, it
    # is imported from somewhere else)
    classes = [
        obj for obj in objects
        if isinstance(obj, type) and (obj.__module__ == module.__name__)
    ]

    # if a superclass was given, filter the classes
    # again to remove those that aren't its subclass
    if superclass is not None:
        classes = [
            cls for cls in classes
            if issubclass(cls, superclass)]

    return classes