Next / Previous / Contents / Shipman's homepage

11.4. engComma(): Invert an English name

Converts “Great Auk” to “Auk, Great”, for example.

abbr.py
# - - -   e n g C o m m a

def engComma ( eng ):
    '''Reverse a name as "GENERIC-PART[, SPECIFIC-PART]"

      [ eng is a string ->
          if eng contains any spaces ->
            return  (longest suffix of eng not containing a space) +
            ", " + (eng up to last space)
          else -> return eng ]
    '''
    #-- 1 --
    # [ if eng contains any spaces ->
    #     spacePos  :=  position of the last space
    #   else ->
    #     return eng ]
    spacePos = eng.rfind ( ' ' )
    if spacePos < 0:
        return eng

    #-- 2 --
    return "%s, %s" % (eng[spacePos+1:], eng[:spacePos])