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 set_status(self, status): """ Save the new status and call all defined callbacks """
self.status = status for callback in self._update_status_callbacks: callback(self)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def wait_for_job(self): """ Use a redis blocking list call to wait for a job, and return it. """
blpop_result = self.connection.blpop(self.keys, self.timeout) if blpop_result is None: return None queue_redis_key, job_ident = blpop_result self.set_status('running') return self.get_queue(queue_redis_key), self.get_job(job_ident)
<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_queue(self, queue_redis_key): """ Return a queue based on the key used in redis to store the list """
try: queue_pk = int(queue_redis_key.split(':')[-2]) except: raise DoesNotExist('Unable to get the queue from the key %s' % queue_redis_key) return self.queue_model.get(queue_pk)
<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_keys(self): """ Update the redis keys to listen for new jobs priorities. """
self.keys = self.queue_model.get_waiting_keys(self.queues) if not self.keys: self.log('No queues yet', level='warning') self.last_update_keys = datetime.utcnow()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def run(self): """ The main method of the worker. Will ask redis for list items via blocking calls, get jobs from them, try to execute these jobs, and end when n...
# if status is not None, we already had a run ! if self.status: self.set_status('aborted') raise LimpydJobsException('This worker run is already terminated') self.set_status('starting') self.start_date = datetime.utcnow() if self.max_duration: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def elapsed(self): """ Return a timedelta representation of the time passed sine the worker was running. """
if not self.start_date: return None return (self.end_date or datetime.utcnow()) - self.start_date
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def requeue_delayed_jobs(self): """ Requeue each delayed job that are now ready to be executed """
failures = [] for queue in self.queue_model.get_all_by_priority(self.queues): failures.extend(queue.requeue_delayed_jobs()) self.last_requeue_delayed = datetime.utcnow() for failure in failures: self.log('Unable to requeue %s: %s' % failure)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _main_loop(self): """ Run jobs until must_stop returns True """
fetch_priorities_delay = timedelta(seconds=self.fetch_priorities_delay) fetch_delayed_delay = timedelta(seconds=self.fetch_delayed_delay) while not self.must_stop(): self.set_status('waiting') if self.last_update_keys + fetch_priorities_delay < datetime.utcnow(): ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_error(self, job, queue, exception, trace=None): """ Called when an exception was raised during the execute call for a job. """
to_be_requeued = not job.must_be_cancelled_on_error and self.requeue_times and self.requeue_times >= int(job.tries.hget() or 0) if not to_be_requeued: job.queued.delete() job.hmset(end=str(datetime.utcnow()), status=STATUSES.ERROR) queue.errors.rpush(job.ident) if...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def requeue_job(self, job, queue, priority, delayed_for=None): """ Requeue a job in a queue with the given priority, possibly delayed """
job.requeue(queue_name=queue._cached_name, priority=priority, delayed_for=delayed_for, queue_model=self.queue_model) if hasattr(job, 'on_requeued'): job.on_requeued(queue) self.log(self.job_requeue_message(job, queue))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_error_message(self, job, queue, to_be_requeued, exception, trace=None): """ Return the message to log when a job raised an error """
return '[%s|%s|%s] error: %s [%s]' % (queue._cached_name, job.pk.get(), job._cached_identifier, str(exception), 'requeued' if to_be_requeued else 'NOT requeued')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_requeue_message(self, job, queue): """ Return the message to log when a job is requeued """
priority, delayed_until = job.hmget('priority', 'delayed_until') msg = '[%s|%s|%s] requeued with priority %s' args = [queue._cached_name, job.pk.get(), job._cached_identifier, priority] if delayed_until: msg += ', delayed until %s' args.append(delayed_until) ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_success(self, job, queue, job_result): """ Called just after an execute call was successful. job_result is the value returned by the callback, if any. ""...
job.queued.delete() job.hmset(end=str(datetime.utcnow()), status=STATUSES.SUCCESS) queue.success.rpush(job.ident) self.log(self.job_success_message(job, queue, job_result)) if hasattr(job, 'on_success'): job.on_success(queue, job_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 job_success_message(self, job, queue, job_result): """ Return the message to log when a job is successful """
return '[%s|%s|%s] success, in %s' % (queue._cached_name, job.pk.get(), job._cached_identifier, job.duration)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_started(self, job, queue): """ Called just before the execution of the job """
job.hmset(start=str(datetime.utcnow()), status=STATUSES.RUNNING) job.tries.hincrby(1) self.log(self.job_started_message(job, queue)) if hasattr(job, 'on_started'): job.on_started(queue)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_started_message(self, job, queue): """ Return the message to log just befre the execution of the job """
return '[%s|%s|%s] starting' % (queue._cached_name, job.pk.get(), job._cached_identifier)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_skipped(self, job, queue): """ Called if a job, before trying to run it, has not the "waiting" status, or, after run, if its status was set to "canceled"...
job.queued.delete() self.log(self.job_skipped_message(job, queue), level='warning') if hasattr(job, 'on_skipped'): job.on_skipped(queue)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_skipped_message(self, job, queue): """ Return the message to log when a job was skipped """
return '[%s|%s|%s] job skipped (current status: %s)' % ( queue._cached_name, job.pk.get(), job._cached_identifier, STATUSES.by_value(job._cached_status, 'UNKNOWN'))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_delayed(self, job, queue): """ Called if a job, before trying to run it, has the "delayed" status, or, after run, if its status was set to "delayed" If d...
delayed_until = job.delayed_until.hget() if delayed_until: try: delayed_until = compute_delayed_until(delayed_until=parse(delayed_until)) except (ValueError, TypeError): delayed_until = None if not delayed_until: # by default ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def job_delayed_message(self, job, queue): """ Return the message to log when a job was delayed just before or during its execution """
return '[%s|%s|%s] job delayed until %s' % ( queue._cached_name, job.pk.get(), job._cached_identifier, job.delayed_until.hget())
<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_parser(self): """ Create and return the ``OptionParser`` which will be used to parse the arguments to the worker. """
return OptionParser(prog=self.prog_name, usage=self.usage(), version='%%prog %s' % self.get_version(), option_list=self.option_list)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def manage_options(self): """ Create a parser given the command-line arguments, creates a parser Return True if the programme must exit. """
self.parser = self.create_parser() self.options, self.args = self.parser.parse_args(self.argv) self.do_imports() if self.options.callback and not callable(self.options.callback): self.parser.error('The callback is not callable') self.logger_level = None if...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def do_imports(self): """ Import all importable options """
self.do_import('worker_class', Worker) self.do_import('queue_model', self.options.worker_class.queue_model) self.do_import('error_model', self.options.worker_class.error_model) self.do_import('callback', self.options.worker_class.callback)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def print_options(self): """ Print all options as parsed by the script """
options = [] print("The script is running with the following options:") options.append(("dry_run", self.options.dry_run)) options.append(("worker_config", self.__class__)) database_config = self.database_config or \ self.options.queue_model.database...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def execute(self): """ Main method to call to run the worker """
self.prepare_models() self.prepare_worker() if self.options.print_options: self.print_options() self.run()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def prepare_models(self): """ If a database config ws given as argument, apply it to our models """
if self.database_config: for model in (self.options.queue_model, self.options.error_model): model.database.reset(**self.database_config)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def settable_options(doc, argv, ignore, options_first): """Determine which options we can set, which ones are boolean, and which ones are repeatable. All set ite...
settable, booleans, repeatable, short_map = set(), set(), set(), dict() # Determine which options are settable by docoptcfg and which ones are flags/booleans. options = docopt.parse_defaults(doc) short_map.update((o.short, o.long) for o in options) parsed_argv = docopt.parse_argv(docopt.TokenStrea...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def values_from_env(env_prefix, settable, booleans, repeatable): """Get all values from environment variables. :param str env_prefix: Argument from docoptcfg(). ...
defaults_env = dict() for key in settable: try: defaults_env[key] = get_env(key, env_prefix, key in booleans, key in repeatable) except KeyError: pass return defaults_env
<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_opt(key, config, section, booleans, repeatable): """Get one value from config file. :raise DocoptcfgFileError: If an option is the wrong type. :param str...
# Handle repeatable non-boolean options (e.g. --file=file1.txt --file=file2.txt). if key in repeatable and key not in booleans: return config.get(section, key[2:]).strip('\n').splitlines() # Handle repeatable booleans. if key in repeatable and key in booleans: try: return c...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def values_from_file(docopt_dict, config_option, settable, booleans, repeatable): """Parse config file and read settable values. Can be overridden by both comman...
section = docopt.DocoptExit.usage.split()[1] settable = set(o for o in settable if o != config_option) config = ConfigParser() defaults = dict() # Sanity checks. if config_option not in docopt_dict: raise DocoptcfgError if docopt_dict[config_option] is None or not settable: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def setRepoData(self, searchString, category="", extension="", math=False, game=False, searchFiles=False): """Call this function with all the settings to use f...
self.searchString = searchString self.category = category self.math = math self.game = game self.searchFiles = searchFiles self.extension = extension
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def setOutputObject(self, newOutput=output.CalcpkgOutput(True, True)): """Set an object where all output from calcpkg will be redirected to for this repository...
self.output = newOutput
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def searchIndex(self, printData=True): """Search the index with all the repo's specified parameters"""
backupValue = copy.deepcopy(self.output.printData) self.output.printData = printData self.data = self.index.search(self.searchString, self.category, self.math, self.game, self.searchFiles, self.extension) self.output.printData = backupValue return self.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 countIndex(self): """A wrapper for the count function in calcrepo.index; count using specified parameters"""
self.data = self.index.count(self.searchString, self.category, self.math, self.game, self.searchFiles, self.extension)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def getDownloadUrls(self): """Return a list of the urls to download from"""
data = self.searchIndex(False) fileUrls = [] for datum in data: fileUrl = self.formatDownloadUrl(datum[0]) fileUrls.append(fileUrl) return fileUrls
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def getFileInfos(self): """Return a list of FileInfo objects"""
data = self.searchIndex(False) self.data = data self.printd(" ") fileInfos = [] for datum in data: try: fileInfo = self.getFileInfo(datum[0], datum[1]) fileInfos.append(fileInfo) except NotImplementedError: self.printd("Error: the info command is not supported for " + self.name + ".") 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 downloadFiles(self, prompt=True, extract=False): """Download files from the repository"""
#First, get the download urls data = self.data downloadUrls = self.getDownloadUrls() #Then, confirm the user wants to do this if prompt: confirm = raw_input("Download files [Y/N]? ") if confirm.lower() != 'y': self.printd("Operation aborted by user input") return #Now, if they still 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 downloadFileFromUrl(self, url): """Given a URL, download the specified file"""
fullurl = self.baseUrl + url try: urlobj = urllib2.urlopen(fullurl) contents = urlobj.read() except urllib2.HTTPError, e: self.printd("HTTP error:", e.code, url) return None except urllib2.URLError, e: self.printd("URL error:", e.code, url) return None self.printd("Fetched '%s' (size %d byt...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def openIndex(self, filename, description): """Attempt to delete and recreate an index, returns open file object or None."""
try: os.remove(filename) self.printd(" Deleted old " + description) except: self.printd(" No " + description + " found") # Now, attempt to open a new index try: files = open(filename, 'wt') except: self.printd("Error: Unable to create file " + filename + " in current folder. Quitting.") ...
<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_model(*args): """Load an HTK model from one ore more files. :param args: Filenames of the model (e.g. macros hmmdefs) :return: The model as an OrderedDi...
text = '' for fnm in args: text += open(fnm).read() text += '\n' parser = htk_model_parser.htk_modelParser() model = HtkModelSemantics() return parser.parse(text, rule_name='model', ignorecase=True, semantics=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 save_model(model, filename): """Save the model into a file. :param model: HTK model to be saved :param filename: File where to save the model """
with open(filename, 'w') as f: f.write(serialize_model(model))
<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_model(model): """Serialize the HTK model into a file. :param model: Model to be serialized """
result = '' # First serialize the macros for macro in model['macros']: if macro.get('options', None): result += '~o ' for option in macro['options']['definition']: result += _serialize_option(option) elif macro.get('transition', 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 _load_resources(self): """ Load all the native goldman resources. The route or API endpoint will be automatically determined based on the resource object ins...
for resource in self.RESOURCES: if isinstance(resource, goldman.ModelsResource): route = '/%s' % resource.rtype elif isinstance(resource, goldman.ModelResource): route = '/%s/{rid}' % resource.rtype elif isinstance(resource, goldman.RelatedRe...
<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_serializer(req, exc): # pylint: disable=unused-argument """ Serializer for native falcon HTTPError exceptions. We override the default serializer with...
error = { 'detail': exc.description, 'title': exc.title, 'status': exc.status, } try: error['links'] = {'about': exc.link['href']} except (TypeError, KeyError): error['links'] = {'about': ''} return ( gol...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def find_peakset(dataset, basecolumn=-1, method='', where=None): """ Find peakset from the dataset Parameters dataset : list A list of data basecolumn : int An i...
peakset = [] where_i = None for data in dataset: base = data[basecolumn] base = maidenhair.statistics.average(base) # limit data points if where: adata = [maidenhair.statistics.average(x) for x in data] where_i = np.where(where(adata)) bas...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def normalise_string(string): """ Strips trailing whitespace from string, lowercases it and replaces spaces with underscores """
string = (string.strip()).lower() return re.sub(r'\W+', '_', string)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def json_decode(data): """ Decodes the given JSON as primitives """
if isinstance(data, six.binary_type): data = data.decode('utf-8') return json.loads(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 write(self, *args): '''Write convenience function; writes strings. ''' for s in args: self.out.write(s) event = ''.join(*args)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _save(self, counters): """ Saves the current counters measurements. :param counters: current counters measurements to be saves. """
if self._logger == None: return if len(counters) == 0: return # Sort counters by name counters = sorted(counters, key=LogCounters._get_counter_name) for counter in counters: self._logger.info("counters", self._counter_to_string(counter))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def humanize(text): '''Transform code conventions to human readable strings''' words = [] for part in text.split('_'): for word in RE_CAMEL.findall(part) or [part]: words.append(word.lower()) words[0] = words[0].title() return ' '.join(words)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def is_visit_primitive(obj): '''Returns true if properly visiting the object returns only the object itself.''' from .base import visit if (isinstance(obj, tuple(PRIMITIVE_TYPES)) and not isinstance(obj, STR) and not isinstance(obj, bytes)): return True if (isinstance(obj, CONTAINERS) an...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def clear(self, correlation_id): """ Clears component state. :param correlation_id: (optional) transaction id to trace execution through call chain. """
self._lock.acquire() try: self._cache = {} finally: self._lock.release()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rest_verbs(http_method_names=None): """ Decorator that converts a function-based view into an RestView subclass. Takes a list of allowed methods for the view...
http_method_names = ['GET'] if (http_method_names is None) else http_method_names def decorator(func): WrappedRestView = type( six.PY3 and 'WrappedRestView' or b'WrappedRestView', (RestView,), {'__doc__': func.__doc__} ) # Note, the above allows us...
<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_stations(self): """ Return a list of all stations IDs """
request = requests.get("{}/stations".format( self.base_url)) if request.status_code != 200: return None return request.json()
<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_station_temperature_datetime(self, station_id): """ Return temperature measurement datetime for a given station """
request = requests.get( "{}/station/{}/parameters/temperature/datetime".format( self.base_url, station_id)) if request.status_code != 200: return None return datetime.strptime(request.json(), "%Y-%m-%dT%H:%M:%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 get_station_temperature_value(self, station_id): """ Return temperature value for a given station """
request = requests.get( "{}/station/{}/parameters/temperature/value".format( self.base_url, station_id)) if request.status_code != 200: return None return request.json()
<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_email_status(mx_resolver, recipient_address, sender_address, smtp_timeout=10, helo_hostname=None): """ Checks if an email might be valid by getting the...
domain = recipient_address[recipient_address.find('@') + 1:] if helo_hostname is None: helo_hostname = domain ret = {'status': 101, 'extended_status': None, 'message': "The server is unable to connect."} records = [] try: records = mx_resolver.get_mx_records(helo_hostname) exc...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def start_namespace(self, prefix, uri): """ Declare a namespace prefix Use prefix=None to set the default namespace. """
self._g.startPrefixMapping(prefix, uri) self._ns[prefix] = uri
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def end_namespace(self, prefix): """ Undeclare a namespace prefix. """
del self._ns[prefix] self._g.endPrefixMapping(prefix)
<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_colors(n): """Returns n unique and "evenly" spaced colors for the backgrounds of the projects. Args: n (int): The number of unique colors wanted. Retur...
import matplotlib.pyplot as plt from matplotlib.colors import rgb2hex as r2h from numpy import linspace cols = linspace(0.05, .95, n) cmap = plt.get_cmap('nipy_spectral') return [r2h(cmap(i)) for i in cols]
<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_prefixes(namespaces=None): """Internal function takes a list of prefix, namespace uri tuples and generates a SPARQL PREFIX string. Args: namespaces(lis...
if namespaces is None: namespaces = [ ('bf', str(BIBFRAME)), ('schema', str(SCHEMA_ORG)) ] output = "PREFIX {}: <{}>\n".format( namespaces[0][0], namespaces[0][1]) if len(namespaces) == 1: return output else: for namespace in names...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def copy_graph(subject, existing_graph): """Function takes a subject and an existing graph, returns a new graph with all predicate and objects of the existing gr...
new_graph = rdflib.Graph() for predicate, object_ in existing_graph.predicate_objects(): new_graph.add((subject, predicate, object_)) return new_graph
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def init_app(self, app): """ Initializes a Flask app object for the extension. Args: app(Flask): Flask app """
app.config.setdefault('FEDORA_BASE_URL', 'http://localhost:8080') if hasattr(app, 'teardown_appcontext'): app.teardown_appcontext(self.teardown) else: app.teardown_request(self.teardown)
<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_transaction(self): """Method creates a new transaction resource and sets instance's transaction."""
request = urlllib.request.urlopen( urllib.parse.urljoin(self.base_url, 'fcr:tx')) self.transaction = request.read()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def connect(self, fedora_url, data=None, method='Get'): """Method attempts to connect to REST servers of the Fedora Commons repository using optional data parame...
if data is None: data = {} if not fedora_url.startswith("http"): fedora_url = urllib.parse.urljoin(self.base_url, fedora_url) request = urllib.request.Request(fedora_url, method=method) request.add_header('Accept', 'text/t...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def as_json(self, entity_url, context=None): """Method takes a entity uri and attempts to return the Fedora Object as a JSON-LD. Args: entity_url(str): Fedora C...
try: urllib.request.urlopen(entity_url) except urllib.error.HTTPError: raise ValueError("Cannot open {}".format(entity_url)) entity_graph = self.read(entity_url) entity_json = json.loads( entity_graph.serialize( format='json-ld', ...
<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(self, uri=None, graph=None, data=None): """Method takes an optional URI and graph, first checking if the URL is already present in Fedora, if not, cre...
if uri is not None: existing_entity = self.__dedup__(rdflib.URIRef(uri), graph) if existing_entity is not None: return # Returns nothing else: default_request = urllib.request.Request( "/".join([self.base_url, "rest"]), ...
<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(self, uri): """Method deletes a Fedora Object in the repository Args: uri(str): URI of Fedora Object """
try: self.connect(uri, method='DELETE') return True except urllib.error.HTTPError: return 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 exists(self, uri): """Method returns true is the entity exists in the Repository, false, otherwise Args: uri(str): Entity URI Returns: bool """
##entity_uri = "/".join([self.base_url, entity_id]) try: urllib.request.urlopen(uri) return True except urllib.error.HTTPError: return 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 flush(self): """Method flushes repository, deleting all objects"""
base_graph = rdflib.Graph().parse('{}/rest'.format(self.base_url)) has_child = rdflib.URIRef( 'http://fedora.info/definitions/v4/repository#hasChild') for obj in base_graph.objects(predicate=has_child): self.delete(str(obj))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def insert(self, entity_id, property_uri, value): """Method inserts a new entity's property in Fedora4 Repository Args: entity_id(string): Unique ID of Fedora o...
if not entity_id.startswith("http"): entity_uri = urllib.parse.urljoin(self.base_url, entity_id) else: entity_uri = entity_id if entity_uri.endswith("/"): entity_uri = entity_uri[:-1] if not entity_id.endswith("fcr:metadata"): entity_uri =...
<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(self, uri): """Method takes uri and creates a RDF graph from Fedora Repository Args: uri(str): URI of Fedora URI Returns: rdflib.Graph """
read_response = self.connect(uri) fedora_graph = rdflib.Graph().parse( data=read_response.read(), format='turtle') return fedora_graph
<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_term): """DEPRECIATED Method takes a query term and searches Fedora Repository using SPARQL search endpoint and returns a RDF graph of the...
fedora_search_url = "/".join([self.base_url, 'rest', 'fcr:search']) fedora_search_url = "{}?{}".format( fedora_search_url, urllib.parse.urlencode({"q": query_term})) search_request = urllib.request.Request( fedora_search_url, method='GET') ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _normalize_direction(heading: int) -> int: """ Make sure that 0 < heading < 360 Args: heading: base heading Returns: corrected heading """
while heading > 359: heading = int(heading - 359) while heading < 0: heading = int(heading + 359) return heading
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _gauss(mean: int, sigma: int) -> int: """ Creates a variation from a base value Args: mean: base value sigma: gaussian sigma Returns: random value """
return int(random.gauss(mean, sigma))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _randomize_speed(base_speed: int, sigma: int = None) -> int: """ Creates a variation in wind speed Args: base_speed: base wind speed sigma: sigma value for ga...
if sigma is None: int_sigma = int(base_speed / 4) else: int_sigma = sigma val = MissionWeather._gauss(base_speed, int_sigma) if val < 0: return 0 return min(val, 50)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _randomize_direction(base_heading, sigma) -> int: """ Creates a variation in direction Args: base_heading: base direction sigma: sigma value for gaussian vari...
val = MissionWeather._gauss(base_heading, sigma) val = MissionWeather._normalize_direction(val) return val
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def guess_version_by_running_live_package( pkg_key, default="?" ): # type: (str,str) -> Any """Guess the version of a pkg when pip doesn't provide it. :param str...
try: m = import_module(pkg_key) except ImportError: return default else: return getattr(m, "__version__", default)
<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_api_params(self): """Dictionary with available API parameters :raises ValueError: If value of __class__.params is not dictionary :return: Should return d...
result = self.params if type(result) != dict: raise ValueError( '{}.params should return dictionary'.format( self.__class__.__name__ ) ) 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 get_api_required_params(self): """ List with required params :return: Dictionary with API parameters :raises ValueError: If value of __class__.required_param...
result = self.required_params if type(result) != list: raise ValueError( '{}.required_params should return list'.format( self.__class__.__name__ ) ) 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 _build_params_from_kwargs(self, **kwargs): """Builds parameters from passed arguments Search passed parameters in available methods, prepend specified API ke...
api_methods = self.get_api_params() required_methods = self.get_api_required_params() ret_kwargs = {} for key, val in kwargs.items(): if key not in api_methods: warnings.warn( 'Passed uknown parameter [{}]'.format(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 write(self, label, index): """ Saves a new label, index mapping to the cache. Raises a RuntimeError on a conflict. """
if label in self.cache: if self.cache[label] != index: error_message = 'cache_conflict on label: {} with index: {}\ncache dump: {}'.format(label, index, self.cache) raise RuntimeError(error_message) else: self.cache[label] = index
<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_image(filename, max_size, form_field='image'): """Pack an image from file into multipart-formdata post body"""
try: if os.path.getsize(filename) > (max_size * 1024): raise IdeaScalyError('File is too big, must be less than %skb.' % max_size) except os.error as e: raise IdeaScalyError('Unable to access file: %s' % e.strerror) # build the mulitpart-formdata body ...
<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_configuration(name='Base'): """Create a configuration base class It is built using :class:`ConfigurationMeta`. Subclassing such a base class will regi...
@classmethod def register(cls, element, action, method): if not action in cls.configuration: cls.configuration[action] = {} if method in cls.configuration[action]: raise ValueError('Method %s already defined for action %s' % (method, action)) cls.configuration[ac...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def expose(f=None, base=None, action=None, method=None, kind=BASIC): """Decorator to expose a function .. note:: A module function can be decorated but ``base`` ...
def expose_f(f): f.exposed = True f.exposed_action = action f.exposed_method = method f.exposed_kind = kind return f def register_f(f): f = expose_f(f) base.register(f, action or f.__module__, method or f.__name__) return f if f is not 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 merge_configurations(configurations): """Merge configurations together and raise error if a conflict is detected :param configurations: configurations to mer...
configuration = {} for c in configurations: for k, v in c.iteritems(): if k in configuration: raise ValueError('%s already in a previous base configuration' % k) configuration[k] = v return configuration
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def AsCGI(nsdict={}, typesmodule=None, rpc=False, modules=None): '''Dispatch within a CGI script. ''' if os.environ.get('REQUEST_METHOD') != 'POST': _CGISendFault(Fault(Fault.Client, 'Must use POST')) return ct = os.environ['CONTENT_TYPE'] try: if ct.startswith('multipart/'):...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def AsHandler(request=None, modules=None, **kw): '''Dispatch from within ModPython.''' ps = ParsedSoap(request) kw['request'] = request _Dispatch(ps, modules, _ModPythonSendXML, _ModPythonSendFault, **kw)
<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_xml(self, text, code=200): '''Send some XML. ''' self.send_response(code) if text: self.send_header('Content-type', 'text/xml; charset="%s"' %UNICODE_ENCODING) self.send_header('Content-Length', str(len(text))) self.end_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 do_POST(self): '''The POST command. ''' try: ct = self.headers['content-type'] if ct.startswith('multipart/'): cid = resolvers.MIMEResolver(ct, self.rfile) xml = cid.GetSOAPPart() ps = ParsedSoap(xml, resolver=cid.Resolv...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def punctuate(current_text, new_text, add_punctuation): """ Add punctuation as needed """
if add_punctuation and current_text and not current_text[-1] in string.punctuation: current_text += '. ' spacer = ' ' if not current_text or (not current_text[-1].isspace() and not new_text[0].isspace()) else '' return current_text + spacer + new_text
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ingest(topic, text, **kwargs): """ Ingest the given text for the topic """
if not text: raise ValueError('No text given to ingest for topic: ' + topic) data = {'topic': topic, 'text': text.strip()} data.update(kwargs) db.markovify.insert(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 generate(topic, add_punctuation, character_count=None): """ Generate the text for a given topic """
corpus_cursor = db.markovify.find({'topic': topic}) if(corpus_cursor): corpus = '' for text in corpus_cursor: corpus = punctuate(corpus, text['text'], add_punctuation) text_model = markovify.Text(corpus) sentence = text_model.make_short_sentence(character_count) \ ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def default_setup(): """The default API setup for lxc4u This is the API that you access globally from lxc4u. """
service = LXCService lxc_types = dict(LXC=LXC, LXCWithOverlays=LXCWithOverlays, __default__=UnmanagedLXC) loader = LXCLoader(lxc_types, service) manager = LXCManager(loader, service) return LXCAPI(manager=manager, service=service)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def unordered_pair_eq(pair1, pair2): '''Performs pairwise unordered equality. ``pair1`` == ``pair2`` if and only if ``frozenset(pair1)`` == ``frozenset(pair2)``. ''' (x1, y1), (x2, y2) = pair1, pair2 return (x1 == x2 and y1 == y2) or (x1 == y2 and y1 == x2)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def expand_labels(labels, subtopic=False): '''Expand a set of labels that define a connected component. ``labels`` must define a *positive* connected component: it is all of the edges that make up the *single* connected component in the :class:`LabelStore`. expand will ignore subtopic assignments, and ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def normalize_ident(ident): '''Splits a generic identifier. If ``ident`` is a tuple, then ``(ident[0], ident[1])`` is returned. Otherwise, ``(ident[0], None)`` is returned. ''' if isinstance(ident, tuple) and len(ident) == 2: return ident[0], ident[1] # content_id, subtopic_id 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 idents_from_label(lab, subtopic=False): '''Returns the "ident" of a label. If ``subtopic`` is ``True``, then a pair of pairs is returned, where each pair corresponds to the content id and subtopic id in the given label. Otherwise, a pair of pairs is returned, but the second element of each ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def put(self, *labels): '''Add a new label to the store. :param label: label :type label: :class:`Label` ''' puts = [] for label in labels: k1, k2 = self._keys_from_label(label) v = self._value_from_label(label) puts.append((k1, v)) ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def directly_connected(self, ident): '''Return a generator of labels connected to ``ident``. ``ident`` may be a ``content_id`` or a ``(content_id, subtopic_id)``. If no labels are defined for ``ident``, then the generator will yield no labels. Note that this only retur...