Home ›
Debugging › Below is an instruction that describes a task. Write a respo...
Below is an instruction that describes a task. Write a respo...
Debugging · General · coding
72Quality
91%Useful
87Reliability
The prompt
Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: This code provides a Python API for KB SRU. To use it, you can create an instance of the "sru" class and call its "search" method, passing in a query string and optionally a collection name, start record number, maximum number of records to return, and record schema. The method will return a "response" object containing the results of the search. You can then use the various properties of the "response" object to extract specific information from the search results, such as titles, creators, and subjects. ### Input: ### Output: #! /usr/bin/env python """ Python API for KB SRU """ import sys import urllib import requests from lxml import etree SRU_BASEURL = ' SRU_BASEURL += '?version=1.2&maximumRecords=%i' SRU_BASEURL += '&operation=searchRetrieve' SRU_BASEURL += '&startRecord=%i' SRU_BASEURL += '&recordSchema=%s' SRU_BASEURL += '&x-collection=%s&query=%s' SETS = {'ANP': {'collection': 'ANP', 'description_en': 'Radio Bulletins ANP Press Agency', 'description_nl': 'ANP Radiobulletins Digitaal', 'metadataPrefix': 'didl', 'recordschema': 'dcx', 'setname': 'anp', 'time_period': [1937, 1989]}, 'DPO': {'collection': 'DPO_boekdeel', 'description_en': 'Early Dutch Books Online', 'description_nl': 'Early Dutch Books Online', 'metadataPrefix': 'didl', 'recordschema': 'ddd', 'setname': 'DPO', 'time_period': [1781, 1800]}, 'BYVANCK': {'description_en': 'Medieval Illuminated Manuscripts', 'description_nl': 'Middeleeuwse Verluchte Handschriften', 'metadataPrefix': 'dcx', 'setname': 'BYVANCK', 'time_period': [500, 1500]}, 'SGD': {'description_en': 'States General Digital', 'description_nl': 'Staten-Generaal Digitaal', 'metadataPrefix': 'dcx', 'setname': 'sgd:register', 'time_period': [1962, 1994]}, 'GGC': {'collection': 'GGC', 'description_en': 'General Catalogue KB', 'description_nl': 'Algemene Catalogus KB', 'metadataPrefix': 'dcx', 'recordschema': 'dcx', 'setname': 'ggc', 'time_period': [1937, 2021]}} # No idea what to use here? # Name spaces in GGC records srw_ns = ' tel_ns = ' xsi_ns = ' dc_ns = ' dcterms_ns = ' dcx_ns = ' NSMAPGGC = {"srw": srw_ns, "tel": tel_ns, "xsi": xsi_ns, "dc": dc_ns, "dcterms": dcterms_ns, "dcx": dcx_ns} class response(): def __init__(self, record_data, sru): self.record_data = record_data self.sru = sru def getElementText(self, tagName, attributeName, attributeValue): # Returns text content of all elements for which tag matches tagName, # and attribute value equals attributeValue. Set attributeName to empty # string to get all tagName matches. textFields = [] for r in self.record_data.iter(): if r.tag == tagName: if attributeName != '': try: if r.attrib[attributeName] == attributeValue: textFields.append(r.text) except KeyError: pass else: textFields.append(r.text) return textFields @property def records(self): if self.sru.nr_of_records == 0: record_data = " " else: ns = {'zs': ' record_data = self.record_data.xpath("zs:records/zs:record", namespaces=ns)[0] return record(record_data, self.sru) # Below property functions all return a list with all instances that satisfy # criteria @property def typesDutch(self): return(self.getElementText('{ '{ 'nl')) @property def typesDCMI(self): return(self.getElementText('{ '{ 'DCMIType')) @property def identifiersISBN(self): return(self.getElementText('{ '{ 'dcterms:ISBN')) @property def identifiersBrinkman(self): return(self.getElementText('{ '{ 'dcx:Brinkman')) @property def identifiersURI(self): return(self.getElementText('{ '{ 'dcterms:URI')) @property def identifiersOCLC(self): return(self.getElementText('{ '{ 'OCLC')) @property def languagesDutch(self): return(self.getElementText('{ '{ 'nl')) @property def languagesEnglish(self): return(self.getElementText('{ '{ 'en')) @property def languagesFrench(self): return(self.getElementText('{ '{ 'fr')) @property def languagesISO639(self): return(self.getElementText('{ '{ 'dcterms:ISO639-2')) @property def dates(self): return(self.getElementText('{ '', '')) @property def extents(self): return(self.getElementText('{ '', '')) @property def creators(self): return(self.getElementText('{ '', '')) @property def contributors(self): return(self.getElementText('{ '', '')) @property def titles(self): return(self.getElementText('{ '', '')) @property def titlesMain(self): return(self.getElementText('{ '{ 'dcx:maintitle')) @property def titlesIntermediate(self): return(self.getElementText('{ '{ 'dcx:intermediatetitle')) @property def publishers(self): return(self.getElementText('{ '', '')) @property def countries(self): return(self.getElementText('{ '', '')) @property def subjectsBrinkman(self): return(self.getElementText('{ '{ 'dcx:Brinkman')) @property def subjectsISO9707(self): return(self.getElementText('{ '{ 'ISO_9707_[Brinkman]')) @property def subjectsUNESCO(self): return(self.getElementText('{ '{ 'UNESCO')) @property def collectionIdentifiers(self): return(self.getElementText('{ '{ 'dcx:collectionIdentifier')) @property def recordIdentifiersURI(self): return(self.getElementText('{ '{ 'dcterms:URI')) @property def annotations(self): # Note that annotations sometimes contain language or itenID attibutes; # ignored for now (collect everything). return(self.getElementText('{ '', '')) class record(): def __init__(self, record_data, sru): self.record_data = record_data self.sru = sru def __iter__(self): return self # This works under Python 2.7 def next(self): if self.sru.nr_of_records == 0: raise StopIteration if self.sru.startrecord 0: return response(record_data, self) return False def run_query(self): url = SRU_BASEURL % (self.maximumrecords, self.startrecord, self.recordschema, self.collection, self.query) if self.DEBUG: sys.stdout.write(url) r = requests.get(url) if not r.status_code == 200: raise Exception('Error while getting data from %s' % url) record_data = etree.fromstring(r.content) return record_data
Find similar in the app →
Why this prompt
- 91% found it useful across General
Source
Hugging Face
Related Debugging prompts
Bug Reproduction Detective
Quality 93 · Claude CodeSecurity-Focused Code Review
Quality 93 · Claude CodePerformance Bottleneck Optimizer
Quality 93 · Claude CodeProduction Incident Root Cause
Quality 93 · Claude CodePython Bug Triage Assistant
Quality 92 · Gemini, GPT-5.6, ClaudeCode Review Assistant
Quality 90 · GPT-5.6