<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def match_identities(db, uuid, matcher):
 """Search for similar unique identities. The function will search in the registry for similar identities to 'uuid'. The result will be a list matches containing unique identities objects. This list will not(!) include the given unique identity. The criteria used to check when an identity matches with another one is defined by 'matcher' parameter. This parameter is an instance of 'IdentityMatcher' class. :param db: database manager :param uuid: identifier of the identity to match :param matcher: criteria used to match identities :returns: list of matched unique identities :raises NotFoundError: raised when 'uuid' does not exist in the registry """

    uidentities = []

    with db.connect() as session:
        uidentity = find_unique_identity(session, uuid)

        if not uidentity:
            raise NotFoundError(entity=uuid)

        # Get all identities expect of the one requested one query above (uid)
        candidates = session.query(UniqueIdentity).\
            filter(UniqueIdentity.uuid != uuid).\
            order_by(UniqueIdentity.uuid)

        for candidate in candidates:
            if not matcher.match(uidentity, candidate):
                continue
            uidentities.append(candidate)

        # Detach objects from the session
        session.expunge_all()

    return uidentities