<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def reference_to_path(reference):
 """Convert a reference to a Python object to a string path."""

    # Try to pop the options off whatever they passed in.
    if isinstance(reference, basestring):
        # This is an object path name in str form.
        import re
        if not re.match(r'^[^\d\W]([a-zA-Z._]|((?<!\.)\d))+$', reference):
            raise errors.BadObjectPathError(
                'Invalid reference path, must meet Python\'s identifier '
                'requirements, passed value was "%s".', reference)
        return reference

    if callable(reference):
        # This is a function or a class.
        # Try to figure out the path to the reference.
        parts = [reference.__module__]
        if hasattr(reference, 'im_class'):
            parts.append(reference.im_class.__name__)

        if hasattr(reference, 'func_name'):
            parts.append(reference.func_name)
        elif reference.__module__ == '__builtin__':
            return reference.__name__
        elif hasattr(reference, '__name__'):
            # Probably a class
            parts.append(reference.__name__)
        else:
            raise errors.BadObjectPathError("Invalid object type.")

        return '.'.join(parts)

        raise errors.BadObjectPathError("Unable to determine path to callable.")

    elif hasattr(reference, '__package__'):
        # This is probably a module.
        return reference.__name__

    raise errors.BadObjectPathError("Must provide a reference path or reference.")