<commit_before>#!/usr/bin/env python
import json
from peyotl.nexson_validation.helper import SeverityCodes, _NEXEL, errorReturn
from peyotl.nexson_validation.schema import add_schema_attributes
from peyotl.nexson_validation.err_generator import factory2code, \
                                                   gen_MissingExpectedListWarning, \
                                                   gen_MissingMandatoryKeyWarning, \
                                                   gen_MissingOptionalKeyWarning, \
                                                   gen_MultipleTipsToSameOttIdWarning, \
                                                   gen_RepeatedIDWarning, \
                                                   gen_UnparseableMetaWarning, \
                                                   gen_UnrecognizedKeyWarning, \
                                                   gen_WrongValueTypeWarning
from peyotl.nexson_syntax.helper import add_literal_meta, \
                                        get_nexml_el, \
                                        find_val_literal_meta_first, \
                                        find_nested_meta_first, \
                                        extract_meta, \
                                        _add_value_to_dict_bf
from peyotl.nexson_syntax import detect_nexson_version
from peyotl.utility import get_logger
_LOG = get_logger(__name__)

_EMPTY_TUPLE = tuple()
_USING_IDREF_ONLY_PATHS = False

def delete_same_agent_annotation(obj, annotation):
    agent_id = annotation.get('@wasAssociatedWithAgentId')
    delete_annotation(obj, agent_id=agent_id)

def delete_annotation(obj,
                      agent_id=None,
                      annot_id=None,
                      nexson_version=None):
    if nexson_version is None:
        nexson_version = detect_nexson_version(obj)
    nex_el = get_nexml_el(obj)
    annotation_list = get_annotation_list(nex_el, nexson_version)
    delete_annotation_from_annot_list(annotation_list, agent_id=agent_id, annot_id=annot_id)

def delete_annotation_from_annot_list(annotation_list, annotation, agent_id=None, annot_id=None):
    _LOG.debug('delete_annotation_from_annot_list with agent_id = ' + str(agent_id))
    to_remove_inds = []
    # TODO should check preserve field...
    if agent_id is not None:
        for n, annot in enumerate(annotation_list):
            if annot.get('@wasAssociatedWithAgentId') == agent_id:
                to_remove_inds.append(n)
    else:
        if annot_id is None:
            raise ValueError('Either agent_id or annot_id must have a value')
        for n, annot in enumerate(annotation_list):
            if annot.get('@id') == annot_id:
                to_remove_inds.append(n)
    while len(to_remove_inds) > 0:
        n = to_remove_inds.pop(-1)
        del annotation_list[n]


def replace_same_agent_annotation(obj, annotation):
    agent_id = annotation.get('@wasAssociatedWithAgentId')
    replace_annotation(obj, annotation, agent_id=agent_id)

def replace_annotation(obj,<commit_msg>bug fix delete_annotation_from_annot_list<commit_after>#!/usr/bin/env python
import json
from peyotl.nexson_validation.helper import SeverityCodes, _NEXEL, errorReturn
from peyotl.nexson_validation.schema import add_schema_attributes
from peyotl.nexson_validation.err_generator import factory2code, \
                                                   gen_MissingExpectedListWarning, \
                                                   gen_MissingMandatoryKeyWarning, \
                                                   gen_MissingOptionalKeyWarning, \
                                                   gen_MultipleTipsToSameOttIdWarning, \
                                                   gen_RepeatedIDWarning, \
                                                   gen_UnparseableMetaWarning, \
                                                   gen_UnrecognizedKeyWarning, \
                                                   gen_WrongValueTypeWarning
from peyotl.nexson_syntax.helper import add_literal_meta, \
                                        get_nexml_el, \
                                        find_val_literal_meta_first, \
                                        find_nested_meta_first, \
                                        extract_meta, \
                                        _add_value_to_dict_bf
from peyotl.nexson_syntax import detect_nexson_version
from peyotl.utility import get_logger
_LOG = get_logger(__name__)

_EMPTY_TUPLE = tuple()
_USING_IDREF_ONLY_PATHS = False

def delete_same_agent_annotation(obj, annotation):
    agent_id = annotation.get('@wasAssociatedWithAgentId')
    delete_annotation(obj, agent_id=agent_id)

def delete_annotation(obj,
                      agent_id=None,
                      annot_id=None,
                      nexson_version=None):
    if nexson_version is None:
        nexson_version = detect_nexson_version(obj)
    nex_el = get_nexml_el(obj)
    annotation_list = get_annotation_list(nex_el, nexson_version)
    delete_annotation_from_annot_list(annotation_list, agent_id=agent_id, annot_id=annot_id)

def delete_annotation_from_annot_list(annotation_list, agent_id=None, annot_id=None):
    _LOG.debug('delete_annotation_from_annot_list with agent_id = ' + str(agent_id))
    to_remove_inds = []
    # TODO should check preserve field...
    if agent_id is not None:
        for n, annot in enumerate(annotation_list):
            if annot.get('@wasAssociatedWithAgentId') == agent_id:
                to_remove_inds.append(n)
    else:
        if annot_id is None:
            raise ValueError('Either agent_id or annot_id must have a value')
        for n, annot in enumerate(annotation_list):
            if annot.get('@id') == annot_id:
                to_remove_inds.append(n)
    while len(to_remove_inds) > 0:
        n = to_remove_inds.pop(-1)
        del annotation_list[n]


def replace_same_agent_annotation(obj, annotation):
    agent_id = annotation.get('@wasAssociatedWithAgentId')
    replace_annotation(obj, annotation, agent_id=agent_id)

def replace_annotation(obj,
{'fraction_non_alphanumeric': 0.06066056245912361, 'fraction_numerical': 0.0009810333551340746, 'mean_word_length': 1.8691369606003752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7477349', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1576, 'n_words': 288}