def uri_leaf(uri):
    """
    Get the "leaf" - fragment id or last segment - of a URI. Useful e.g. for
    getting a term from a "namespace like" URI. Examples:

        >>> uri_leaf('http://example.org/ns/things#item')
        'item'
        >>> uri_leaf('http://example.org/ns/stuff/item')
        'item'
        >>> uri_leaf('http://example.org/ns/stuff/')
        >>>
        >>> uri_leaf('urn:example.org:stuff')
        'stuff'
        >>> uri_leaf('example.org')
        >>>
    """
    for char in ('#', '/', ':'):
        if uri.endswith(char):
            break
        # base, sep, leaf = uri.rpartition(char)
        if char in uri:
            sep = char
            leaf = uri.rsplit(char)[-1]
        else:
            sep = ''
            leaf = uri
        if sep and leaf:
            return leaf