def __sid_to_username(sid):
        '''
        Provided with a valid Windows Security Identifier (SID) and returns a Username

        Args:
            sid (str): Security Identifier (SID).

        Returns:
            str: Username in the format of username@realm or username@computer.
        '''
        if sid is None or sid == '':
            return ''
        try:
            sid_bin = win32security.GetBinarySid(sid)  # pylint: disable=no-member
        except pywintypes.error as exc:  # pylint: disable=no-member
            raise ValueError(
                    'pkg: Software owned by {0} is not valid: [{1}] {2}'.format(sid, exc.winerror, exc.strerror)
                )
        try:
            name, domain, _account_type = win32security.LookupAccountSid(None, sid_bin)  # pylint: disable=no-member
            user_name = '{0}\\{1}'.format(domain, name)
        except pywintypes.error as exc:  # pylint: disable=no-member
            # if user does not exist...
            # winerror.ERROR_NONE_MAPPED = No mapping between account names and
            # security IDs was carried out.
            if exc.winerror == winerror.ERROR_NONE_MAPPED:  # 1332
                # As the sid is from the registry it should be valid
                # even if it cannot be lookedup, so the sid is returned
                return sid
            else:
                raise ValueError(
                          'Failed looking up sid \'{0}\' username: [{1}] {2}'.format(sid, exc.winerror, exc.strerror)
                        )
        try:
            user_principal = win32security.TranslateName(  # pylint: disable=no-member
                            user_name,
                            win32api.NameSamCompatible,  # pylint: disable=no-member
                            win32api.NameUserPrincipal)  # pylint: disable=no-member
        except pywintypes.error as exc:  # pylint: disable=no-member
            # winerror.ERROR_NO_SUCH_DOMAIN The specified domain either does not exist
            # or could not be contacted, computer may not be part of a domain also
            # winerror.ERROR_INVALID_DOMAINNAME The format of the specified domain name is
            # invalid. e.g. S-1-5-19 which is a local account
            # winerror.ERROR_NONE_MAPPED No mapping between account names and security IDs was done.
            if exc.winerror in (winerror.ERROR_NO_SUCH_DOMAIN,
                                winerror.ERROR_INVALID_DOMAINNAME,
                                winerror.ERROR_NONE_MAPPED):
                return '{0}@{1}'.format(name.lower(), domain.lower())
            else:
                raise
        return user_principal