def __init__(self, wd_item_id='', new_item=False, data=None,
                 mediawiki_api_url='https://www.wikidata.org/w/api.php',
                 sparql_endpoint_url='https://query.wikidata.org/sparql',
                 append_value=None, fast_run=False, fast_run_base_filter=None, fast_run_use_refs=False,
                 ref_handler=None, global_ref_mode='KEEP_GOOD', good_refs=None, keep_good_ref_statements=False,
                 search_only=False, item_data=None, user_agent=config['USER_AGENT_DEFAULT'],
                 core_props=None, core_prop_match_thresh=0.66):
        """
        constructor
        :param wd_item_id: Wikidata item id
        :param new_item: This parameter lets the user indicate if a new item should be created
        :type new_item: True or False
        :param data: a dictionary with WD property strings as keys and the data which should be written to
            a WD item as the property values
        :type data: List[WDBaseDataType]
        :param append_value: a list of properties where potential existing values should not be overwritten by the data
            passed in the :parameter data.
        :type append_value: list of property number strings
        :param fast_run: True if this item should be run in fastrun mode, otherwise False. User setting this to True
            should also specify the fast_run_base_filter for these item types
        :type fast_run: bool
        :param fast_run_base_filter: A property value dict determining the Wikidata property and the corresponding value
            which should be used as a filter for this item type. Several filter criteria can be specified. The values
            can be either Wikidata item QIDs, strings or empty strings if the value should be a variable in SPARQL.
            Example: {'P352': '', 'P703': 'Q15978631'} if the basic common type of things this bot runs on is
            human proteins (specified by Uniprot IDs (P352) and 'found in taxon' homo sapiens 'Q15978631').
        :type fast_run_base_filter: dict
        :param fast_run_use_refs: If `True`, fastrun mode will consider references in determining if a statement should
            be updated and written to Wikidata. Otherwise, only the value and qualifiers are used. Default: False
        :type fast_run_use_refs: bool
        :param ref_handler: This parameter defines a function that will manage the reference handling in a custom
            manner. This argument should be a function handle that accepts two arguments, the old/current statement
            (first argument) and new/proposed/to be written statement (second argument), both of type: a subclass of
            WDBaseDataType. The function should return an new item that is the item to be written. The item's values
            properties or qualifiers should not be modified; only references. This function is also used in fastrun mode.
            This will only be used if the ref_mode is set to "CUSTOM".
        :type ref_handler: function
        :param global_ref_mode: sets the reference handling mode for an item. Four modes are possible, 'STRICT_KEEP'
            keeps all references as they are, 'STRICT_KEEP_APPEND' keeps the references as they are and appends
            new ones. 'STRICT_OVERWRITE' overwrites all existing references for given. 'CUSTOM' will use the function
            defined in ref_handler
        :type global_ref_mode: str of value 'STRICT_KEEP', 'STRICT_KEEP_APPEND', 'STRICT_OVERWRITE', 'KEEP_GOOD', 'CUSTOM'
        :param good_refs: This parameter lets the user define blocks of good references. It is a list of dictionaries.
            One block is a dictionary with  Wikidata properties as keys and potential values as the required value for
            a property. There can be arbitrarily many key: value pairs in one reference block.
            Example: [{'P248': 'Q905695', 'P352': None, 'P407': None, 'P1476': None, 'P813': None}]
            This example contains one good reference block, stated in: Uniprot, Uniprot ID, title of Uniprot entry,
            language of work and date when the information has been retrieved. A None type indicates that the value
            varies from reference to reference. In this case, only the value for the Wikidata item for the
            Uniprot database stays stable over all of these references. Key value pairs work here, as Wikidata
            references can hold only one value for one property. The number of good reference blocks is not limited.
            This parameter OVERRIDES any other reference mode set!!
        :type good_refs: list containing dictionaries.
        :param keep_good_ref_statements: Do not delete any statement which has a good reference, either defined in the
            good_refs list or by any other referencing mode.
        :type keep_good_ref_statements: bool
        :param search_only: If this flag is set to True, the data provided will only be used to search for the
            corresponding Wikidata item, but no actual data updates will performed. This is useful, if certain states or
            values on the target item need to be checked before certain data is written to it. In order to write new
            data to the item, the method update() will take data, modify the Wikidata item and a write() call will
            then perform the actual write to Wikidata.
        :type search_only: bool
        :param item_data: A Python JSON object corresponding to the Wikidata item in wd_item_id. This can be used in
            conjunction with wd_item_id in order to provide raw data.
        :param user_agent: The user agent string to use when making http requests
        :type user_agent: str
        :param core_props: Core properties are used to retrieve a Wikidata item based on `data` if a `wd_item_id` is
            not given. This is a set of PIDs to use. If None, all Wikidata properties with a distinct values
            constraint will be used. (see: get_core_props)
        :param core_prop_match_thresh: The proportion of core props that must match during retrieval of an item
            when the wd_item_id is not specified.
        :type core_prop_match_thresh: float
        """
        self.core_prop_match_thresh = core_prop_match_thresh
        self.wd_item_id = wd_item_id
        self.new_item = new_item
        self.mediawiki_api_url = mediawiki_api_url
        self.sparql_endpoint_url = sparql_endpoint_url
        self.data = [] if data is None else data
        self.append_value = [] if append_value is None else append_value
        self.fast_run = fast_run
        self.fast_run_base_filter = fast_run_base_filter
        self.fast_run_use_refs = fast_run_use_refs
        self.ref_handler = ref_handler
        self.global_ref_mode = global_ref_mode
        self.good_refs = good_refs
        self.keep_good_ref_statements = keep_good_ref_statements
        self.search_only = search_only
        self.item_data = item_data
        self.user_agent = user_agent

        self.create_new_item = False
        self.wd_json_representation = {}
        self.statements = []
        self.original_statements = []
        self.entity_metadata = {}
        self.fast_run_container = None
        self.require_write = True
        self.sitelinks = dict()
        self.lastrevid = None  # stores last revisionid after a write occurs

        if self.ref_handler:
            assert callable(self.ref_handler)
        if self.global_ref_mode == "CUSTOM" and self.ref_handler is None:
            raise ValueError("If using a custom ref mode, ref_handler must be set")

        if (core_props is None) and (self.sparql_endpoint_url not in self.DISTINCT_VALUE_PROPS):
            self.get_distinct_value_props(self.sparql_endpoint_url)
        self.core_props = core_props if core_props is not None else self.DISTINCT_VALUE_PROPS[self.sparql_endpoint_url]

        try:
            self.mrh = MappingRelationHelper(self.sparql_endpoint_url)
        except Exception as e:
            # if the "equivalent property" and "mappingRelation" property are not found, we can't know what the
            # QIDs for the mapping relation types are
            self.mrh = None
            warnings.warn("mapping relation types are being ignored")

        if self.fast_run:
            self.init_fastrun()

        if not __debug__:
            if self.require_write and self.fast_run:
                print('fastrun skipped, because no full data match, updating item...')
            elif not self.require_write and self.fast_run:
                print('successful fastrun, no write to Wikidata required')

        if self.wd_item_id != '' and self.create_new_item == True:
            raise IDMissingError('Cannot create a new item, when a wikidata identifier is given')
        elif self.new_item == True and len(self.data) > 0:
            self.create_new_item = True
            self.__construct_claim_json()
        elif self.wd_item_id and self.require_write:
            self.init_data_load()
        elif self.require_write:
            self.init_data_load()
def __init__ ( self , wd_item_id = '' , new_item = False , data = None , mediawiki_api_url = 'https://www.wikidata.org/w/api.php' , sparql_endpoint_url = 'https://query.wikidata.org/sparql' , append_value = None , fast_run = False , fast_run_base_filter = None , fast_run_use_refs = False , ref_handler = None , global_ref_mode = 'KEEP_GOOD' , good_refs = None , keep_good_ref_statements = False , search_only = False , item_data = None , user_agent = config [ 'USER_AGENT_DEFAULT' ] , core_props = None , core_prop_match_thresh = 0.66 ) : self . core_prop_match_thresh = core_prop_match_thresh self . wd_item_id = wd_item_id self . new_item = new_item self . mediawiki_api_url = mediawiki_api_url self . sparql_endpoint_url = sparql_endpoint_url self . data = [ ] if data is None else data self . append_value = [ ] if append_value is None else append_value self . fast_run = fast_run self . fast_run_base_filter = fast_run_base_filter self . fast_run_use_refs = fast_run_use_refs self . ref_handler = ref_handler self . global_ref_mode = global_ref_mode self . good_refs = good_refs self . keep_good_ref_statements = keep_good_ref_statements self . search_only = search_only self . item_data = item_data self . user_agent = user_agent self . create_new_item = False self . wd_json_representation = { } self . statements = [ ] self . original_statements = [ ] self . entity_metadata = { } self . fast_run_container = None self . require_write = True self . sitelinks = dict ( ) self . lastrevid = None if self . ref_handler : assert callable ( self . ref_handler ) if self . global_ref_mode == "CUSTOM" and self . ref_handler is None : raise ValueError ( "If using a custom ref mode, ref_handler must be set" ) if ( core_props is None ) and ( self . sparql_endpoint_url not in self . DISTINCT_VALUE_PROPS ) : self . get_distinct_value_props ( self . sparql_endpoint_url ) self . core_props = core_props if core_props is not None else self . DISTINCT_VALUE_PROPS [ self . sparql_endpoint_url ] try : self . mrh = MappingRelationHelper ( self . sparql_endpoint_url ) except Exception as e : self . mrh = None warnings . warn ( "mapping relation types are being ignored" ) if self . fast_run : self . init_fastrun ( ) if not __debug__ : if self . require_write and self . fast_run : print ( 'fastrun skipped, because no full data match, updating item...' ) elif not self . require_write and self . fast_run : print ( 'successful fastrun, no write to Wikidata required' ) if self . wd_item_id != '' and self . create_new_item == True : raise IDMissingError ( 'Cannot create a new item, when a wikidata identifier is given' ) elif self . new_item == True and len ( self . data ) > 0 : self . create_new_item = True self . __construct_claim_json ( ) elif self . wd_item_id and self . require_write : self . init_data_load ( ) elif self . require_write : self . init_data_load ( )
constructor
        :param wd_item_id: Wikidata item id
        :param new_item: This parameter lets the user indicate if a new item should be created
        :type new_item: True or False
        :param data: a dictionary with WD property strings as keys and the data which should be written to
            a WD item as the property values
        :type data: List[WDBaseDataType]
        :param append_value: a list of properties where potential existing values should not be overwritten by the data
            passed in the :parameter data.
        :type append_value: list of property number strings
        :param fast_run: True if this item should be run in fastrun mode, otherwise False. User setting this to True
            should also specify the fast_run_base_filter for these item types
        :type fast_run: bool
        :param fast_run_base_filter: A property value dict determining the Wikidata property and the corresponding value
            which should be used as a filter for this item type. Several filter criteria can be specified. The values
            can be either Wikidata item QIDs, strings or empty strings if the value should be a variable in SPARQL.
            Example: {'P352': '', 'P703': 'Q15978631'} if the basic common type of things this bot runs on is
            human proteins (specified by Uniprot IDs (P352) and 'found in taxon' homo sapiens 'Q15978631').
        :type fast_run_base_filter: dict
        :param fast_run_use_refs: If `True`, fastrun mode will consider references in determining if a statement should
            be updated and written to Wikidata. Otherwise, only the value and qualifiers are used. Default: False
        :type fast_run_use_refs: bool
        :param ref_handler: This parameter defines a function that will manage the reference handling in a custom
            manner. This argument should be a function handle that accepts two arguments, the old/current statement
            (first argument) and new/proposed/to be written statement (second argument), both of type: a subclass of
            WDBaseDataType. The function should return an new item that is the item to be written. The item's values
            properties or qualifiers should not be modified; only references. This function is also used in fastrun mode.
            This will only be used if the ref_mode is set to "CUSTOM".
        :type ref_handler: function
        :param global_ref_mode: sets the reference handling mode for an item. Four modes are possible, 'STRICT_KEEP'
            keeps all references as they are, 'STRICT_KEEP_APPEND' keeps the references as they are and appends
            new ones. 'STRICT_OVERWRITE' overwrites all existing references for given. 'CUSTOM' will use the function
            defined in ref_handler
        :type global_ref_mode: str of value 'STRICT_KEEP', 'STRICT_KEEP_APPEND', 'STRICT_OVERWRITE', 'KEEP_GOOD', 'CUSTOM'
        :param good_refs: This parameter lets the user define blocks of good references. It is a list of dictionaries.
            One block is a dictionary with  Wikidata properties as keys and potential values as the required value for
            a property. There can be arbitrarily many key: value pairs in one reference block.
            Example: [{'P248': 'Q905695', 'P352': None, 'P407': None, 'P1476': None, 'P813': None}]
            This example contains one good reference block, stated in: Uniprot, Uniprot ID, title of Uniprot entry,
            language of work and date when the information has been retrieved. A None type indicates that the value
            varies from reference to reference. In this case, only the value for the Wikidata item for the
            Uniprot database stays stable over all of these references. Key value pairs work here, as Wikidata
            references can hold only one value for one property. The number of good reference blocks is not limited.
            This parameter OVERRIDES any other reference mode set!!
        :type good_refs: list containing dictionaries.
        :param keep_good_ref_statements: Do not delete any statement which has a good reference, either defined in the
            good_refs list or by any other referencing mode.
        :type keep_good_ref_statements: bool
        :param search_only: If this flag is set to True, the data provided will only be used to search for the
            corresponding Wikidata item, but no actual data updates will performed. This is useful, if certain states or
            values on the target item need to be checked before certain data is written to it. In order to write new
            data to the item, the method update() will take data, modify the Wikidata item and a write() call will
            then perform the actual write to Wikidata.
        :type search_only: bool
        :param item_data: A Python JSON object corresponding to the Wikidata item in wd_item_id. This can be used in
            conjunction with wd_item_id in order to provide raw data.
        :param user_agent: The user agent string to use when making http requests
        :type user_agent: str
        :param core_props: Core properties are used to retrieve a Wikidata item based on `data` if a `wd_item_id` is
            not given. This is a set of PIDs to use. If None, all Wikidata properties with a distinct values
            constraint will be used. (see: get_core_props)
        :param core_prop_match_thresh: The proportion of core props that must match during retrieval of an item
            when the wd_item_id is not specified.
        :type core_prop_match_thresh: float