text_prompt
stringlengths
157
13.1k
code_prompt
stringlengths
7
19.8k
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def i2c_master_read(self, addr, length, flags=I2C_NO_FLAGS): """Make an I2C read access. The given I2C device is addressed and clock cycles for `length` bytes ar...
data = array.array('B', (0,) * length) status, rx_len = api.py_aa_i2c_read_ext(self.handle, addr, flags, length, data) _raise_i2c_status_code_error_if_failure(status) del data[rx_len:] return bytes(data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def poll(self, timeout=None): """Wait for an event to occur. If `timeout` is given, if specifies the length of time in milliseconds which the function will wait ...
if timeout is None: timeout = -1 ret = api.py_aa_async_poll(self.handle, timeout) _raise_error_if_negative(ret) events = list() for event in (POLL_I2C_READ, POLL_I2C_WRITE, POLL_SPI, POLL_I2C_MONITOR): if ret & event: eve...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def enable_i2c_slave(self, slave_address): """Enable I2C slave mode. The device will respond to the specified slave_address if it is addressed. You can wait for ...
ret = api.py_aa_i2c_slave_enable(self.handle, slave_address, self.BUFFER_SIZE, self.BUFFER_SIZE) _raise_error_if_negative(ret)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def i2c_slave_read(self): """Read the bytes from an I2C slave reception. The bytes are returned as a string object. """
data = array.array('B', (0,) * self.BUFFER_SIZE) status, addr, rx_len = api.py_aa_i2c_slave_read_ext(self.handle, self.BUFFER_SIZE, data) _raise_i2c_status_code_error_if_failure(status) # In case of general call, actually return the general call address if addr ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def i2c_slave_last_transmit_size(self): """Returns the number of bytes transmitted by the slave."""
ret = api.py_aa_i2c_slave_write_stats(self.handle) _raise_error_if_negative(ret) return ret
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def i2c_monitor_read(self): """Retrieved any data fetched by the monitor. This function has an integrated timeout mechanism. You should use :func:`poll` to deter...
data = array.array('H', (0,) * self.BUFFER_SIZE) ret = api.py_aa_i2c_monitor_read(self.handle, self.BUFFER_SIZE, data) _raise_error_if_negative(ret) del data[ret:] return data.tolist()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def spi_bitrate(self): """SPI bitrate in kHz. Not every bitrate is supported by the host adapter. Therefore, the actual bitrate may be less than the value which ...
ret = api.py_aa_spi_bitrate(self.handle, 0) _raise_error_if_negative(ret) return ret
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def spi_configure(self, polarity, phase, bitorder): """Configure the SPI interface."""
ret = api.py_aa_spi_configure(self.handle, polarity, phase, bitorder) _raise_error_if_negative(ret)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def spi_configure_mode(self, spi_mode): """Configure the SPI interface by the well known SPI modes."""
if spi_mode == SPI_MODE_0: self.spi_configure(SPI_POL_RISING_FALLING, SPI_PHASE_SAMPLE_SETUP, SPI_BITORDER_MSB) elif spi_mode == SPI_MODE_3: self.spi_configure(SPI_POL_FALLING_RISING, SPI_PHASE_SETUP_SAMPLE, SPI_BITORDER_MSB) else:...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def spi_write(self, data): """Write a stream of bytes to a SPI device."""
data_out = array.array('B', data) data_in = array.array('B', (0,) * len(data_out)) ret = api.py_aa_spi_write(self.handle, len(data_out), data_out, len(data_in), data_in) _raise_error_if_negative(ret) return bytes(data_in)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def spi_ss_polarity(self, polarity): """Change the ouput polarity on the SS line. Please note, that this only affects the master functions. """
ret = api.py_aa_spi_master_ss_polarity(self.handle, polarity) _raise_error_if_negative(ret)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def edit_form(self, obj): """Customize edit form."""
form = super(OAISetModelView, self).edit_form(obj) del form.spec return form
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _schema_from_verb(verb, partial=False): """Return an instance of schema for given verb."""
from .verbs import Verbs return getattr(Verbs, verb)(partial=partial)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def serialize(pagination, **kwargs): """Return resumption token serializer."""
if not pagination.has_next: return token_builder = URLSafeTimedSerializer( current_app.config['SECRET_KEY'], salt=kwargs['verb'], ) schema = _schema_from_verb(kwargs['verb'], partial=False) data = dict(seed=random.random(), page=pagination.next_num, kwargs=s...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _deserialize(self, value, attr, data): """Serialize resumption token."""
token_builder = URLSafeTimedSerializer( current_app.config['SECRET_KEY'], salt=data['verb'], ) result = token_builder.loads(value, max_age=current_app.config[ 'OAISERVER_RESUMPTION_TOKEN_EXPIRE_TIME']) result['token'] = value result['kwargs'] ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def load(self, data, many=None, partial=None): """Deserialize a data structure to an object."""
result = super(ResumptionTokenSchema, self).load( data, many=many, partial=partial ) result.data.update( result.data.get('resumptionToken', {}).get('kwargs', {}) ) return result
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def make_request_validator(request): """Validate arguments in incomming request."""
verb = request.values.get('verb', '', type=str) resumption_token = request.values.get('resumptionToken', None) schema = Verbs if resumption_token is None else ResumptionVerbs return getattr(schema, verb, OAISchema)(partial=False)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def from_iso_permissive(datestring, use_dateutil=True): """Parse an ISO8601-formatted datetime and return a datetime object. Inspired by the marshmallow.utils.fr...
dateutil_available = False try: from dateutil import parser dateutil_available = True except ImportError: dateutil_available = False import datetime # Use dateutil's parser if possible if dateutil_available and use_dateutil: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def validate(self, data): """Check range between dates under keys ``from_`` and ``until``."""
if 'verb' in data and data['verb'] != self.__class__.__name__: raise ValidationError( # FIXME encode data 'This is not a valid OAI-PMH verb:{0}'.format(data['verb']), field_names=['verb'], ) if 'from_' in data and 'until' in data ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def sets(self): """Get list of sets."""
if self.cache: return self.cache.get( self.app.config['OAISERVER_CACHE_KEY'])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def sets(self, values): """Set list of sets."""
# if cache server is configured, save sets list if self.cache: self.cache.set(self.app.config['OAISERVER_CACHE_KEY'], values)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def register_signals_oaiset(self): """Register OAISet signals to update records."""
from .models import OAISet from .receivers import after_insert_oai_set, \ after_update_oai_set, after_delete_oai_set listen(OAISet, 'after_insert', after_insert_oai_set) listen(OAISet, 'after_update', after_update_oai_set) listen(OAISet, 'after_delete', after_delete_...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def unregister_signals_oaiset(self): """Unregister signals oaiset."""
from .models import OAISet from .receivers import after_insert_oai_set, \ after_update_oai_set, after_delete_oai_set if contains(OAISet, 'after_insert', after_insert_oai_set): remove(OAISet, 'after_insert', after_insert_oai_set) remove(OAISet, 'after_update',...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def extract_params(params): """ Extracts the values of a set of parameters, recursing into nested dictionaries. """
values = [] if isinstance(params, dict): for key, value in params.items(): values.extend(extract_params(value)) elif isinstance(params, list): for value in params: values.extend(extract_params(value)) else: values.append(params) return values
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_list(self, list_name, options=None): """ Get detailed metadata information about a list. """
options = options or {} data = {'list': list_name} data.update(options) return self.api_get('list', data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def import_contacts(self, email, password, include_name=False): """ Fetch email contacts from a user's address book on one of the major email websites. Currently...
data = {'email': email, 'password': password} if include_name: data['names'] = 1 return self.api_post('contacts', data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def push_content(self, title, url, images=None, date=None, expire_date=None, description=None, location=None, price=None, tags=None, author=None, site_name=None, ...
vars = vars or {} data = {'title': title, 'url': url} if images is not None: data['images'] = images if date is not None: data['date'] = date if expire_date is not None: data['expire_date'] = date if location is not Non...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def delete_alert(self, email, alert_id): """ delete user alert """
data = {'email': email, 'alert_id': alert_id} return self.api_delete('alert', data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_purchase(self, purchase_id, purchase_key='sid'): """ Retrieve information about a purchase using the system's unique ID or a client's ID @param id_: a st...
data = {'purchase_id': purchase_id, 'purchase_key': purchase_key} return self.api_get('purchase', data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def receive_verify_post(self, post_params): """ Returns true if the incoming request is an authenticated verify post. """
if isinstance(post_params, dict): required_params = ['action', 'email', 'send_id', 'sig'] if not self.check_for_valid_postback_actions(required_params, post_params): return False else: return False if post_params['action'] != 'verify': ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def receive_hardbounce_post(self, post_params): """ Hard bounce postbacks """
if isinstance(post_params, dict): required_params = ['action', 'email', 'sig'] if not self.check_for_valid_postback_actions(required_params, post_params): return False else: return False if post_params['action'] != 'hardbounce': r...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def check_for_valid_postback_actions(self, required_keys, post_params): """ checks if post_params contain required keys """
for key in required_keys: if key not in post_params: return False return True
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def api_get(self, action, data, headers=None): """ Perform an HTTP GET request, using the shared-secret auth hash. @param action: API action call @param data: di...
return self._api_request(action, data, 'GET', headers)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def api_post(self, action, data, binary_data_param=None): """ Perform an HTTP POST request, using the shared-secret auth hash. @param action: API action call @pa...
binary_data_param = binary_data_param or [] if binary_data_param: return self.api_post_multipart(action, data, binary_data_param) else: return self._api_request(action, data, 'POST')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def api_post_multipart(self, action, data, binary_data_param): """ Perform an HTTP Multipart POST request, using the shared-secret auth hash. @param action: API ...
binary_data = {} data = data.copy() try: file_handles = [] for param in binary_data_param: if param in data: binary_data[param] = file_handle = open(data[param], 'r') file_handles.append(file_handle) ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _api_request(self, action, data, request_type, headers=None): """ Make Request to Sailthru API with given data and api key, format and signature hash """
if 'file' in data: file_data = {'file': open(data['file'], 'rb')} else: file_data = None return self._http_request(action, self._prepare_json_payload(data), request_type, file_data, headers)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def validation_error(exception): """Return formatter validation error."""
messages = getattr(exception, 'messages', None) if messages is None: messages = getattr(exception, 'data', {'messages': None})['messages'] def extract_errors(): """Extract errors from exception.""" if isinstance(messages, dict): for field, message in messages.items(): ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def response(args): """Response endpoint."""
e_tree = getattr(xml, args['verb'].lower())(**args) response = make_response(etree.tostring( e_tree, pretty_print=True, xml_declaration=True, encoding='UTF-8', )) response.headers['Content-Type'] = 'text/xml' return response
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _create_percolator_mapping(index, doc_type): """Update mappings with the percolator field. .. note:: This is only needed from ElasticSearch v5 onwards, becau...
if ES_VERSION[0] >= 5: current_search_client.indices.put_mapping( index=index, doc_type=doc_type, body=PERCOLATOR_MAPPING, ignore=[400, 404])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _percolate_query(index, doc_type, percolator_doc_type, document): """Get results for a percolate query."""
if ES_VERSION[0] in (2, 5): results = current_search_client.percolate( index=index, doc_type=doc_type, allow_no_indices=True, ignore_unavailable=True, body={'doc': document} ) return results['matches'] elif ES_VERSION[0] == 6: results = current_search_cli...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _new_percolator(spec, search_pattern): """Create new percolator associated with the new set."""
if spec and search_pattern: query = query_string_parser(search_pattern=search_pattern).to_dict() for index in current_search.mappings.keys(): # Create the percolator doc_type in the existing index for >= ES5 # TODO: Consider doing this only once in app initialization ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _delete_percolator(spec, search_pattern): """Delete percolator associated with the new oaiset."""
if spec: for index in current_search.mappings.keys(): # Create the percolator doc_type in the existing index for >= ES5 percolator_doc_type = _get_percolator_doc_type(index) _create_percolator_mapping(index, percolator_doc_type) current_search_client.delete( ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _build_cache(): """Build sets cache."""
sets = current_oaiserver.sets if sets is None: # build sets cache sets = current_oaiserver.sets = [ oaiset.spec for oaiset in OAISet.query.filter( OAISet.search_pattern.is_(None)).all()] return sets
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_record_sets(record): """Find matching sets."""
# get lists of sets with search_pattern equals to None but already in the # set list inside the record record_sets = set(record.get('_oai', {}).get('sets', [])) for spec in _build_cache(): if spec in record_sets: yield spec # get list of sets that match using percolator ind...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _records_commit(record_ids): """Commit all records."""
for record_id in record_ids: record = Record.get_record(record_id) record.commit()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def update_affected_records(spec=None, search_pattern=None): """Update all affected records by OAISet change. :param spec: The record spec. :param search_pattern...
chunk_size = current_app.config['OAISERVER_CELERY_TASK_CHUNK_SIZE'] record_ids = get_affected_records(spec=spec, search_pattern=search_pattern) group( update_records_sets.s(list(filter(None, chunk))) for chunk in zip_longest(*[iter(record_ids)] * chunk_size) )()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def envelope(**kwargs): """Create OAI-PMH envelope for response."""
e_oaipmh = Element(etree.QName(NS_OAIPMH, 'OAI-PMH'), nsmap=NSMAP) e_oaipmh.set(etree.QName(NS_XSI, 'schemaLocation'), '{0} {1}'.format(NS_OAIPMH, NS_OAIPMH_XSD)) e_tree = ElementTree(element=e_oaipmh) if current_app.config['OAISERVER_XSL_URL']: e_oaipmh.addprevious(etree.Proc...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def error(errors): """Create error element."""
e_tree, e_oaipmh = envelope() for code, message in errors: e_error = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, 'error')) e_error.set('code', code) e_error.text = message return e_tree
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def verb(**kwargs): """Create OAI-PMH envelope for response with verb."""
e_tree, e_oaipmh = envelope(**kwargs) e_element = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, kwargs['verb'])) return e_tree, e_element
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def resumption_token(parent, pagination, **kwargs): """Attach resumption token element to a parent."""
# Do not add resumptionToken if all results fit to the first page. if pagination.page == 1 and not pagination.has_next: return token = serialize(pagination, **kwargs) e_resumptionToken = SubElement(parent, etree.QName(NS_OAIPMH, 'resumptio...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def listsets(**kwargs): """Create OAI-PMH response for ListSets verb."""
e_tree, e_listsets = verb(**kwargs) page = kwargs.get('resumptionToken', {}).get('page', 1) size = current_app.config['OAISERVER_PAGE_SIZE'] oai_sets = OAISet.query.paginate(page=page, per_page=size, error_out=False) for oai_set in oai_sets.items: e_set = SubElement(e_listsets, etree.QNam...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def listmetadataformats(**kwargs): """Create OAI-PMH response for ListMetadataFormats verb."""
cfg = current_app.config e_tree, e_listmetadataformats = verb(**kwargs) if 'identifier' in kwargs: # test if record exists OAIIDProvider.get(pid_value=kwargs['identifier']) for prefix, metadata in cfg.get('OAISERVER_METADATA_FORMATS', {}).items(): e_metadataformat = SubElement...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def listidentifiers(**kwargs): """Create OAI-PMH response for verb ListIdentifiers."""
e_tree, e_listidentifiers = verb(**kwargs) result = get_records(**kwargs) for record in result.items: pid = oaiid_fetcher(record['id'], record['json']['_source']) header( e_listidentifiers, identifier=pid.pid_value, datestamp=record['updated'], ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def listrecords(**kwargs): """Create OAI-PMH response for verb ListRecords."""
record_dumper = serializer(kwargs['metadataPrefix']) e_tree, e_listrecords = verb(**kwargs) result = get_records(**kwargs) for record in result.items: pid = oaiid_fetcher(record['id'], record['json']['_source']) e_record = SubElement(e_listrecords, etree....
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def oaiid_fetcher(record_uuid, data): """Fetch a record's identifier. :param record_uuid: The record UUID. :param data: The record data. :returns: A :class:`inve...
pid_value = data.get('_oai', {}).get('id') if pid_value is None: raise PersistentIdentifierError() return FetchedPID( provider=OAIIDProvider, pid_type=OAIIDProvider.pid_type, pid_value=str(pid_value), )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def validate_spec(self, key, value): """Forbit updates of set identifier."""
if self.spec and self.spec != value: raise OAISetSpecUpdateError("Updating spec is not allowed.") return value
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def add_record(self, record): """Add a record to the OAISet. :param record: Record to be added. :type record: `invenio_records.api.Record` or derivative. """
record.setdefault('_oai', {}).setdefault('sets', []) assert not self.has_record(record) record['_oai']['sets'].append(self.spec)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def remove_record(self, record): """Remove a record from the OAISet. :param record: Record to be removed. :type record: `invenio_records.api.Record` or derivativ...
assert self.has_record(record) record['_oai']['sets'] = [ s for s in record['_oai']['sets'] if s != self.spec]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def oaiserver(sets, records): """Initialize OAI-PMH server."""
from invenio_db import db from invenio_oaiserver.models import OAISet from invenio_records.api import Record # create a OAI Set with db.session.begin_nested(): for i in range(sets): db.session.add(OAISet( spec='test{0}'.format(i), name='Test{0}'....
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def serializer(metadata_prefix): """Return etree_dumper instances. :param metadata_prefix: One of the metadata identifiers configured in ``OAISERVER_METADATA_FOR...
metadataFormats = current_app.config['OAISERVER_METADATA_FORMATS'] serializer_ = metadataFormats[metadata_prefix]['serializer'] if isinstance(serializer_, tuple): return partial(import_string(serializer_[0]), **serializer_[1]) return import_string(serializer_)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def dumps_etree(pid, record, **kwargs): """Dump MARC21 compatible record. :param pid: The :class:`invenio_pidstore.models.PersistentIdentifier` instance. :param ...
from dojson.contrib.to_marc21 import to_marc21 from dojson.contrib.to_marc21.utils import dumps_etree return dumps_etree(to_marc21.do(record['_source']), **kwargs)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def eprints_description(metadataPolicy, dataPolicy, submissionPolicy=None, content=None): """Generate the eprints element for the identify response. The eprints ...
eprints = Element(etree.QName(NS_EPRINTS[None], 'eprints'), nsmap=NS_EPRINTS) eprints.set(etree.QName(ns['xsi'], 'schemaLocation'), '{0} {1}'.format(EPRINTS_SCHEMA_LOCATION, EPRINTS_SCHEMA_LOCATION_XSD)) if content: contentEleme...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def oai_identifier_description(scheme, repositoryIdentifier, delimiter, sampleIdentifier): """Generate the oai-identifier element for the identify response. The ...
oai_identifier = Element(etree.QName(NS_OAI_IDENTIFIER[None], 'oai_identifier'), nsmap=NS_OAI_IDENTIFIER) oai_identifier.set(etree.QName(ns['xsi'], 'schemaLocation'), '{0} {1}'.format(OAI_IDENTIFIER_SCHEMA_LOCATION, ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def friends_description(baseURLs): """Generate the friends element for the identify response. The friends container is recommended for use by repositories to lis...
friends = Element(etree.QName(NS_FRIENDS[None], 'friends'), nsmap=NS_FRIENDS) friends.set(etree.QName(ns['xsi'], 'schemaLocation'), '{0} {1}'.format(FRIENDS_SCHEMA_LOCATION, FRIENDS_SCHEMA_LOCATION_XSD)) for baseURL in baseURLs: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def after_insert_oai_set(mapper, connection, target): """Update records on OAISet insertion."""
_new_percolator(spec=target.spec, search_pattern=target.search_pattern) sleep(2) update_affected_records.delay( search_pattern=target.search_pattern )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def after_update_oai_set(mapper, connection, target): """Update records on OAISet update."""
_delete_percolator(spec=target.spec, search_pattern=target.search_pattern) _new_percolator(spec=target.spec, search_pattern=target.search_pattern) sleep(2) update_affected_records.delay( spec=target.spec, search_pattern=target.search_pattern )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def after_delete_oai_set(mapper, connection, target): """Update records on OAISet deletion."""
_delete_percolator(spec=target.spec, search_pattern=target.search_pattern) sleep(2) update_affected_records.delay( spec=target.spec )
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def query_string_parser(search_pattern): """Elasticsearch query string parser."""
if not hasattr(current_oaiserver, 'query_parser'): query_parser = current_app.config['OAISERVER_QUERY_PARSER'] if isinstance(query_parser, six.string_types): query_parser = import_string(query_parser) current_oaiserver.query_parser = query_parser return current_oaiserver.que...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_affected_records(spec=None, search_pattern=None): """Get list of affected records. :param spec: The record spec. :param search_pattern: The search patter...
# spec pattern query # ---------- ---------- ------- # None None None # None Y Y # X None X # X '' X # X Y X OR Y if spec is None and search_pattern is None: raise StopIteration queries =...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_records(**kwargs): """Get records paginated."""
page_ = kwargs.get('resumptionToken', {}).get('page', 1) size_ = current_app.config['OAISERVER_PAGE_SIZE'] scroll = current_app.config['OAISERVER_RESUMPTION_TOKEN_EXPIRE_TIME'] scroll_id = kwargs.get('resumptionToken', {}).get('scroll_id') if scroll_id is None: search = OAIServerSearch( ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_file_path(filename, local=True, relative_to_module=None, my_dir=my_dir): """ Look for an existing path matching filename. Try to resolve relative to the ...
# override my_dir if module is provided if relative_to_module is not None: my_dir = os.path.dirname(relative_to_module.__file__) user_path = result = filename if local: user_path = os.path.expanduser(filename) result = os.path.abspath(user_path) if os.path.exists(result)...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def load_if_not_loaded(widget, filenames, verbose=False, delay=0.1, force=False, local=True, evaluator=None): """ Load a javascript file to the Jupyter notebook ...
if evaluator is None: evaluator = EVALUATOR # default if not specified. for filename in filenames: loaded = False if force or not filename in LOADED_JAVASCRIPT: js_text = get_text_from_file_name(filename, local) if verbose: print("loading javascr...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _set(self, name, value): "Proxy to set a property of the widget element." return self.widget(self.widget_element._set(name, value))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def base_url(root): """Determine the base url for a root element."""
for attr, value in root.attrib.iteritems(): if attr.endswith('base') and 'http' in value: return value return None
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def clean_ns(tag): """Return a tag and its namespace separately."""
if '}' in tag: split = tag.split('}') return split[0].strip('{'), split[-1] return '', tag
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def xpath(node, query, namespaces={}): """A safe xpath that only uses namespaces if available."""
if namespaces and 'None' not in namespaces: return node.xpath(query, namespaces=namespaces) return node.xpath(query)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def innertext(node): """Return the inner text of a node. If a node has no sub elements, this is just node.text. Otherwise, it's node.text + sub-element-text + no...
if not len(node): return node.text return (node.text or '') + ''.join([etree.tostring(c) for c in node]) + (node.tail or '')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def parse(document, clean_html=True, unix_timestamp=False, encoding=None): """Parse a document and return a feedparser dictionary with attr key access. If clean_...
if isinstance(clean_html, bool): cleaner = default_cleaner if clean_html else fake_cleaner else: cleaner = clean_html result = feedparser.FeedParserDict() result['feed'] = feedparser.FeedParserDict() result['entries'] = [] result['bozo'] = 0 try: parser = SpeedParser...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def changed_path(self): "Find any changed path and update all changed modification times." result = None # default for path in self.paths_to_modification_times: lastmod = self.paths_to_modification_times[path] mod = os.path.getmtime(path) if mod > lastmod: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _parse_date_iso8601(dateString): '''Parse a variety of ISO-8601-compatible formats like 20040105''' m = None for _iso8601_match in _iso8601_matches: m = _iso8601_match(dateString) if m: break if not m: return if m.span() == (0, 0): return params = ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _parse_date_onblog(dateString): '''Parse a string according to the OnBlog 8-bit date format''' m = _korean_onblog_date_re.match(dateString) if not m: return w3dtfdate = '%(year)s-%(month)s-%(day)sT%(hour)s:%(minute)s:%(second)s%(zonediff)s' % \ {'year': m.group(1), 'month': m...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _parse_date_nate(dateString): '''Parse a string according to the Nate 8-bit date format''' m = _korean_nate_date_re.match(dateString) if not m: return hour = int(m.group(5)) ampm = m.group(4) if (ampm == _korean_pm): hour += 12 hour = str(hour) if len(hour) == 1: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _parse_date_greek(dateString): '''Parse a string according to a Greek 8-bit date format.''' m = _greek_date_format_re.match(dateString) if not m: return wday = _greek_wdays[m.group(1)] month = _greek_months[m.group(3)] rfc822date = '%(wday)s, %(day)s %(month)s %(year)s %(hour)s:%(min...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def _parse_date_hungarian(dateString): '''Parse a string according to a Hungarian 8-bit date format.''' m = _hungarian_date_format_re.match(dateString) if not m or m.group(2) not in _hungarian_months: return None month = _hungarian_months[m.group(2)] day = m.group(3) if len(day) == 1: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def parse_date(dateString): '''Parses a variety of date formats into a 9-tuple in GMT''' if not dateString: return None for handler in _date_handlers: try: date9tuple = handler(dateString) except (KeyError, OverflowError, ValueError): continue if not d...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def handle_chunk_wrapper(self, status, name, content, file_info): """wrapper to allow output redirects for handle_chunk."""
out = self.output if out is not None: with out: print("handling chunk " + repr(type(content))) self.handle_chunk(status, name, content, file_info) else: self.handle_chunk(status, name, content, file_info)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def handle_chunk(self, status, name, content, file_info): "Handle one chunk of the file. Override this method for peicewise delivery or error handling." if status == "error": msg = repr(file_info.get("message")) exc = JavaScriptError(msg) exc.file_info = file_info ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def search(self, query, method="lucene", start=None, rows=None, access_token=None): """Search the ORCID database. Parameters :param query: string Query in line w...
if access_token is None: access_token = self. \ get_search_token_from_orcid() headers = {'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % access_token} return self._search(query, method, start, rows, headers, ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def search_generator(self, query, method="lucene", pagination=10, access_token=None): """Search the ORCID database with a generator. The generator will yield eve...
if access_token is None: access_token = self. \ get_search_token_from_orcid() headers = {'Accept': 'application/orcid+json', 'Authorization': 'Bearer %s' % access_token} index = 0 while True: paginated_result = self._search(q...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_search_token_from_orcid(self, scope='/read-public'): """Get a token for searching ORCID records. Parameters :param scope: string /read-public or /read-me...
payload = {'client_id': self._key, 'client_secret': self._secret, 'scope': scope, 'grant_type': 'client_credentials' } url = "%s/oauth/token" % self._endpoint headers = {'Accept': 'application/json'} response ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_token_from_authorization_code(self, authorization_code, redirect_uri): """Like `get_token`, but using an OAuth 2 authorization code. Use this method if y...
token_dict = { "client_id": self._key, "client_secret": self._secret, "grant_type": "authorization_code", "code": authorization_code, "redirect_uri": redirect_uri, } response = requests.post(self._token_url, data=token_dict, ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def read_record_public(self, orcid_id, request_type, token, put_code=None, accept_type='application/orcid+json'): """Get the public info about the researcher. Pa...
return self._get_info(orcid_id, self._get_public_info, request_type, token, put_code, accept_type)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_user_orcid(self, user_id, password, redirect_uri): """Get the user orcid from authentication process. Parameters :param user_id: string The id of the use...
response = self._authenticate(user_id, password, redirect_uri, '/authenticate') return response['orcid']
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def read_record_member(self, orcid_id, request_type, token, put_code=None, accept_type='application/orcid+json'): """Get the member info about the researcher. Pa...
return self._get_info(orcid_id, self._get_member_info, request_type, token, put_code, accept_type)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_access_tokens(self, authorization_code): """From the authorization code, get the "access token" and the "refresh token" from Box. Args: authorization_cod...
response = self.box_request.get_access_token(authorization_code) try: att = response.json() except Exception, ex: raise BoxHttpResponseError(ex) if response.status_code >= 400: raise BoxError(response.status_code, att) return att['access_tok...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def unpack_frame(message): """Called to unpack a STOMP message into a dictionary. returned = { # STOMP Command: # Headers e.g. 'headers' : { 'destination' : 'xyz...
body = [] returned = dict(cmd='', headers={}, body='') breakdown = message.split('\n') # Get the message command: returned['cmd'] = breakdown[0] breakdown = breakdown[1:] def headD(field): # find the first ':' everything to the left of this is a # header, everything to th...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ack(messageid, transactionid=None): """STOMP acknowledge command. Acknowledge receipt of a specific message from the server. messageid: This is the id of the...
header = 'message-id: %s' % messageid if transactionid: header = 'message-id: %s\ntransaction: %s' % (messageid, transactionid) return "ACK\n%s\n\n\x00\n" % header
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def send(dest, msg, transactionid=None): """STOMP send command. dest: This is the channel we wish to subscribe to msg: This is the message body to be sent. trans...
transheader = '' if transactionid: transheader = 'transaction: %s\n' % transactionid return "SEND\ndestination: %s\n%s\n%s\x00\n" % (dest, transheader, msg)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def setCmd(self, cmd): """Check the cmd is valid, FrameError will be raised if its not."""
cmd = cmd.upper() if cmd not in VALID_COMMANDS: raise FrameError("The cmd '%s' is not valid! It must be one of '%s' (STOMP v%s)." % ( cmd, VALID_COMMANDS, STOMP_VERSION) ) else: self._cmd = cmd
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def pack(self): """Called to create a STOMP message from the internal values. """
headers = ''.join( ['%s:%s\n' % (f, v) for f, v in sorted(self.headers.items())] ) stomp_message = "%s\n%s\n%s%s\n" % (self._cmd, headers, self.body, NULL) # import pprint # print "stomp_message: ", pprint.pprint(stomp_message) return stomp_message