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 list_deployments(self): """List all running deployments. :returns: list of deployments :rtype: list[:class:`marathon.models.deployment.MarathonDeployment`] """
response = self._do_request('GET', '/v2/deployments') return self._parse_response(response, MarathonDeployment, is_list=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 list_queue(self, embed_last_unused_offers=False): """List all the tasks queued up or waiting to be scheduled. :returns: list of queue items :rtype: list[:class:`marathon.models.queue.MarathonQueueItem`] """
if embed_last_unused_offers: params = {'embed': 'lastUnusedOffers'} else: params = {} response = self._do_request('GET', '/v2/queue', params=params) return self._parse_response(response, MarathonQueueItem, is_list=True, resource_name='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 delete_deployment(self, deployment_id, force=False): """Cancel a deployment. :param str deployment_id: deployment id :param bool force: if true, don't create a rollback deployment to restore the previous configuration :returns: a dict containing the deployment id and version (empty dict if force=True) :rtype: dict """
if force: params = {'force': True} self._do_request('DELETE', '/v2/deployments/{deployment}'.format( deployment=deployment_id), params=params) # Successful DELETE with ?force=true returns empty text (and status # code 202). Client code should poll until deployment is removed. return {} else: response = self._do_request( 'DELETE', '/v2/deployments/{deployment}'.format(deployment=deployment_id)) return response.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 assert_valid_path(path): """Checks if a path is a correct format that Marathon expects. Raises ValueError if not valid. :param str path: The app id. :rtype: str """
if path is None: return # As seen in: # https://github.com/mesosphere/marathon/blob/0c11661ca2f259f8a903d114ef79023649a6f04b/src/main/scala/mesosphere/marathon/state/PathId.scala#L71 for id in filter(None, path.strip('/').split('/')): if not ID_PATTERN.match(id): raise ValueError( 'invalid path (allowed: lowercase letters, digits, hyphen, "/", ".", ".."): %r' % path) return path
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def assert_valid_id(id): """Checks if an id is the correct format that Marathon expects. Raises ValueError if not valid. :param str id: App or group id. :rtype: str """
if id is None: return if not ID_PATTERN.match(id.strip('/')): raise ValueError( 'invalid id (allowed: lowercase letters, digits, hyphen, ".", ".."): %r' % id) return id
<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_json(cls, attributes): """Construct an object from a parsed response. :param dict attributes: object attributes from parsed response """
return cls(**{to_snake_case(k): v for k, v in attributes.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 to_json(self, minimal=True): """Encode an object as a JSON string. :param bool minimal: Construct a minimal representation of the object (ignore nulls and empty collections) :rtype: str """
if minimal: return json.dumps(self.json_repr(minimal=True), cls=MarathonMinimalJsonEncoder, sort_keys=True) else: return json.dumps(self.json_repr(), cls=MarathonJsonEncoder, sort_keys=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 from_json(cls, obj): """Construct a MarathonConstraint from a parsed response. :param dict attributes: object attributes from parsed response :rtype: :class:`MarathonConstraint` """
if len(obj) == 2: (field, operator) = obj return cls(field, operator) if len(obj) > 2: (field, operator, value) = obj return cls(field, operator, 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 from_tasks(cls, tasks): """Construct a list of MarathonEndpoints from a list of tasks. :param list[:class:`marathon.models.MarathonTask`] tasks: list of tasks to parse :rtype: list[:class:`MarathonEndpoint`] """
endpoints = [ [ MarathonEndpoint(task.app_id, task.service_ports[ port_index], task.host, task.id, port) for port_index, port in enumerate(task.ports) ] for task in tasks ] # Flatten result return [item for sublist in endpoints for item in sublist]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _format_newlines(prefix, formatted_node, options): """ Convert newlines into U+23EC characters, followed by an actual newline and then a tree prefix so as to position the remaining text under the previous line. """
replacement = u''.join([ options.NEWLINE, u'\n', prefix]) return formatted_node.replace(u'\n', replacement)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: async def get_player(self, tag): """Gets a player. Gets a player with specified tag. If no tag is specified, the request will fail. If the tag is invalid, a brawlstars.InvalidTag will be raised. If the data is missing, a ValueError will be raised. If the connection times out, a brawlstars.Timeout will be raised. If the data was unable to be received, a brawlstars.HTTPError will be raised along with the HTTP status code. On success, will return a Player. """
tag = tag.strip("#") tag = tag.upper() try: async with self.session.get(self._base_url + 'players/' + tag, timeout=self.timeout, headers=self.headers) as resp: if resp.status == 200: data = await resp.json() elif 500 > resp.status > 400: raise HTTPError(resp.status) else: raise Error() except asyncio.TimeoutError: raise Timeout() except ValueError: raise MissingData('data') except Exception: raise InvalidArg('tag') data = Box(data) player = Player(data) return player
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def lazy_module(modname, error_strings=None, lazy_mod_class=LazyModule, level='leaf'): """Function allowing lazy importing of a module into the namespace. A lazy module object is created, registered in `sys.modules`, and returned. This is a hollow module; actual loading, and `ImportErrors` if not found, are delayed until an attempt is made to access attributes of the lazy module. A handy application is to use :func:`lazy_module` early in your own code (say, in `__init__.py`) to register all modulenames you want to be lazy. Because of registration in `sys.modules` later invocations of `import modulename` will also return the lazy object. This means that after initial registration the rest of your code can use regular pyhon import statements and retain the lazyness of the modules. Parameters modname : str The module to import. error_strings : dict, optional A dictionary of strings to use when module-loading fails. Key 'msg' sets the message to use (defaults to :attr:`lazy_import._MSG`). The message is formatted using the remaining dictionary keys. The default message informs the user of which module is missing (key 'module'), what code loaded the module as lazy (key 'caller'), and which package should be installed to solve the dependency (key 'install_name'). None of the keys is mandatory and all are given smart names by default. lazy_mod_class: type, optional Which class to use when instantiating the lazy module, to allow deep customization. The default is :class:`LazyModule` and custom alternatives **must** be a subclass thereof. level : str, optional Which submodule reference to return. Either a reference to the 'leaf' module (the default) or to the 'base' module. This is useful if you'll be using the module functionality in the same place you're calling :func:`lazy_module` from, since then you don't need to run `import` again. Setting *level* does not affect which names/modules get registered in `sys.modules`. For *level* set to 'base' and *modulename* 'aaa.bbb.ccc':: aaa = lazy_import.lazy_module("aaa.bbb.ccc", level='base') # 'aaa' becomes defined in the current namespace, with # (sub)attributes 'aaa.bbb' and 'aaa.bbb.ccc'. # It's the lazy equivalent to: import aaa.bbb.ccc For *level* set to 'leaf':: ccc = lazy_import.lazy_module("aaa.bbb.ccc", level='leaf') # Only 'ccc' becomes set in the current namespace. # Lazy equivalent to: from aaa.bbb import ccc Returns ------- module The module specified by *modname*, or its base, depending on *level*. The module isn't immediately imported. Instead, an instance of *lazy_mod_class* is returned. Upon access to any of its attributes, the module is finally loaded. Examples -------- Lazily-loaded module numpy True 3.141592653589793 <module 'numpy' from '/usr/local/lib/python/site-packages/numpy/__init__.py'> Lazily-loaded module missing_module True ImportError: __main__ attempted to use a functionality that requires module missing_module, but it couldn't be loaded. Please install missing_module and retry. See Also -------- :func:`lazy_callable` :class:`LazyModule` """
if error_strings is None: error_strings = {} _set_default_errornames(modname, error_strings) mod = _lazy_module(modname, error_strings, lazy_mod_class) if level == 'base': return sys.modules[module_basename(modname)] elif level == 'leaf': return mod else: raise ValueError("Parameter 'level' must be one of ('base', 'leaf')")
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def lazy_callable(modname, *names, **kwargs): """Performs lazy importing of one or more callables. :func:`lazy_callable` creates functions that are thin wrappers that pass any and all arguments straight to the target module's callables. These can be functions or classes. The full loading of that module is only actually triggered when the returned lazy function itself is called. This lazy import of the target module uses the same mechanism as :func:`lazy_module`. If, however, the target module has already been fully imported prior to invocation of :func:`lazy_callable`, then the target callables themselves are returned and no lazy imports are made. :func:`lazy_function` and :func:`lazy_function` are aliases of :func:`lazy_callable`. Parameters modname : str The base module from where to import the callable(s) in *names*, or a full 'module_name.callable_name' string. names : str (optional) The callable name(s) to import from the module specified by *modname*. If left empty, *modname* is assumed to also include the callable name to import. error_strings : dict, optional A dictionary of strings to use when reporting loading errors (either a missing module, or a missing callable name in the loaded module). *error_string* follows the same usage as described under :func:`lazy_module`, with the exceptions that 1) a further key, 'msg_callable', can be supplied to be used as the error when a module is successfully loaded but the target callable can't be found therein (defaulting to :attr:`lazy_import._MSG_CALLABLE`); 2) a key 'callable' is always added with the callable name being loaded. lazy_mod_class : type, optional See definition under :func:`lazy_module`. lazy_call_class : type, optional Analogously to *lazy_mod_class*, allows setting a custom class to handle lazy callables, other than the default :class:`LazyCallable`. Returns ------- wrapper function or tuple of wrapper functions If *names* is passed, returns a tuple of wrapper functions, one for each element in *names*. If only *modname* is passed it is assumed to be a full 'module_name.callable_name' string, in which case the wrapper for the imported callable is returned directly, and not in a tuple. Notes ----- Unlike :func:`lazy_module`, which returns a lazy module that eventually mutates into the fully-functional version, :func:`lazy_callable` only returns thin wrappers that never change. This means that the returned wrapper object never truly becomes the one under the module's namespace, even after successful loading of the module in *modname*. This is fine for most practical use cases, but may break code that relies on the usage of the returned objects oter than calling them. One such example is the lazy import of a class: it's fine to use the returned wrapper to instantiate an object, but it can't be used, for instance, to subclass from. Examples -------- Lazily-loaded module numpy array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) <module 'numpy' from '/usr/local/lib/python3.5/site-packages/numpy/__init__.py'> See Also -------- :func:`lazy_module` :class:`LazyCallable` :class:`LazyModule` """
if not names: modname, _, name = modname.rpartition(".") lazy_mod_class = _setdef(kwargs, 'lazy_mod_class', LazyModule) lazy_call_class = _setdef(kwargs, 'lazy_call_class', LazyCallable) error_strings = _setdef(kwargs, 'error_strings', {}) _set_default_errornames(modname, error_strings, call=True) if not names: # We allow passing a single string as 'modname.callable_name', # in which case the wrapper is returned directly and not as a list. return _lazy_callable(modname, name, error_strings.copy(), lazy_mod_class, lazy_call_class) return tuple(_lazy_callable(modname, cname, error_strings.copy(), lazy_mod_class, lazy_call_class) for cname 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 _load_module(module): """Ensures that a module, and its parents, are properly loaded """
modclass = type(module) # We only take care of our own LazyModule instances if not issubclass(modclass, LazyModule): raise TypeError("Passed module is not a LazyModule instance.") with _ImportLockContext(): parent, _, modname = module.__name__.rpartition('.') logger.debug("loading module {}".format(modname)) # We first identify whether this is a loadable LazyModule, then we # strip as much of lazy_import behavior as possible (keeping it cached, # in case loading fails and we need to reset the lazy state). if not hasattr(modclass, '_lazy_import_error_msgs'): # Alreay loaded (no _lazy_import_error_msgs attr). Not reloading. return # First, ensure the parent is loaded (using recursion; *very* unlikely # we'll ever hit a stack limit in this case). modclass._LOADING = True try: if parent: logger.debug("first loading parent module {}".format(parent)) setattr(sys.modules[parent], modname, module) if not hasattr(modclass, '_LOADING'): logger.debug("Module {} already loaded by the parent" .format(modname)) # We've been loaded by the parent. Let's bail. return cached_data = _clean_lazymodule(module) try: # Get Python to do the real import! reload_module(module) except: # Loading failed. We reset our lazy state. logger.debug("Failed to load module {}. Resetting..." .format(modname)) _reset_lazymodule(module, cached_data) raise else: # Successful load logger.debug("Successfully loaded module {}".format(modname)) delattr(modclass, '_LOADING') _reset_lazy_submod_refs(module) except (AttributeError, ImportError) as err: logger.debug("Failed to load {}.\n{}: {}" .format(modname, err.__class__.__name__, err)) logger.lazy_trace() # Under Python 3 reloading our dummy LazyModule instances causes an # AttributeError if the module can't be found. Would be preferrable # if we could always rely on an ImportError. As it is we vet the # AttributeError as thoroughly as possible. if ((six.PY3 and isinstance(err, AttributeError)) and not err.args[0] == "'NoneType' object has no attribute 'name'"): # Not the AttributeError we were looking for. raise msg = modclass._lazy_import_error_msgs['msg'] raise_from(ImportError( msg.format(**modclass._lazy_import_error_strings)), 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 _setdef(argdict, name, defaultvalue): """Like dict.setdefault but sets the default value also if None is present. """
if not name in argdict or argdict[name] is None: argdict[name] = defaultvalue return argdict[name]
<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_lazymodule(module): """Removes all lazy behavior from a module's class, for loading. Also removes all module attributes listed under the module's class deletion dictionaries. Deletion dictionaries are class attributes with names specified in `_DELETION_DICT`. Parameters module: LazyModule Returns ------- dict A dictionary of deleted class attributes, that can be used to reset the lazy state using :func:`_reset_lazymodule`. """
modclass = type(module) _clean_lazy_submod_refs(module) modclass.__getattribute__ = ModuleType.__getattribute__ modclass.__setattr__ = ModuleType.__setattr__ cls_attrs = {} for cls_attr in _CLS_ATTRS: try: cls_attrs[cls_attr] = getattr(modclass, cls_attr) delattr(modclass, cls_attr) except AttributeError: pass return cls_attrs
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _reset_lazymodule(module, cls_attrs): """Resets a module's lazy state from cached data. """
modclass = type(module) del modclass.__getattribute__ del modclass.__setattr__ try: del modclass._LOADING except AttributeError: pass for cls_attr in _CLS_ATTRS: try: setattr(modclass, cls_attr, cls_attrs[cls_attr]) except KeyError: pass _reset_lazy_submod_refs(module)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def timestamp_from_datetime(dt): """ Compute timestamp from a datetime object that could be timezone aware or unaware. """
try: utc_dt = dt.astimezone(pytz.utc) except ValueError: utc_dt = dt.replace(tzinfo=pytz.utc) return timegm(utc_dt.timetuple())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_bytes, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """
if isinstance(s, memoryview): s = bytes(s) if isinstance(s, bytes): if encoding == 'utf-8': return s else: return s.decode('utf-8', errors).encode(encoding, errors) if strings_only and (s is None or isinstance(s, int)): return s if not isinstance(s, six.string_types): try: if six.PY3: return six.text_type(s).encode(encoding) else: return bytes(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return b' '.join([force_bytes(arg, encoding, strings_only, errors) for arg in s]) return six.text_type(s).encode(encoding, errors) else: return s.encode(encoding, errors)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def memoize(func, cache, num_args): """ Wrap a function so that results for any argument tuple are stored in 'cache'. Note that the args to the function must be usable as dictionary keys. Only the first num_args are considered when creating the key. """
@wraps(func) def wrapper(*args): mem_args = args[:num_args] if mem_args in cache: return cache[mem_args] result = func(*args) cache[mem_args] = result return result return wrapper
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def hide_auth(msg): """Remove sensitive information from msg."""
for pattern, repl in RE_HIDE_AUTH: msg = pattern.sub(repl, msg) return 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 init(self): """Prepare the HTTP handler, URL, and HTTP headers for all subsequent requests"""
self.debug('Initializing %r', self) proto = self.server.split('://')[0] if proto == 'https': if hasattr(ssl, 'create_default_context'): context = ssl.create_default_context() if self.ssl_verify: context.check_hostname = True context.verify_mode = ssl.CERT_REQUIRED else: context.check_hostname = False context.verify_mode = ssl.CERT_NONE self._http_handler = urllib2.HTTPSHandler(debuglevel=0, context=context) else: self._http_handler = urllib2.HTTPSHandler(debuglevel=0) elif proto == 'http': self._http_handler = urllib2.HTTPHandler(debuglevel=0) else: raise ValueError('Invalid protocol %s' % proto) self._api_url = self.server + '/api_jsonrpc.php' self._http_headers = { 'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api', } if self.httpuser: self.debug('HTTP authentication enabled') auth = self.httpuser + ':' + self.httppasswd self._http_headers['Authorization'] = 'Basic ' + b64encode(auth.encode('utf-8')).decode('ascii')
<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_age(dt): """Calculate delta between current time and datetime and return a human readable form of the delta object"""
delta = datetime.now() - dt days = delta.days hours, rem = divmod(delta.seconds, 3600) minutes, seconds = divmod(rem, 60) if days: return '%dd %dh %dm' % (days, hours, minutes) else: return '%dh %dm %ds' % (hours, minutes, seconds)
<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_obj(self, method, params=None, auth=True): """Return JSON object expected by the Zabbix API"""
if params is None: params = {} obj = { 'jsonrpc': '2.0', 'method': method, 'params': params, 'auth': self.__auth if auth else None, 'id': self.id, } return json.dumps(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 do_request(self, json_obj): """Perform one HTTP request to Zabbix API"""
self.debug('Request: url="%s" headers=%s', self._api_url, self._http_headers) self.debug('Request: body=%s', json_obj) self.r_query.append(json_obj) request = urllib2.Request(url=self._api_url, data=json_obj.encode('utf-8'), headers=self._http_headers) opener = urllib2.build_opener(self._http_handler) urllib2.install_opener(opener) try: response = opener.open(request, timeout=self.timeout) except Exception as e: raise ZabbixAPIException('HTTP connection problem: %s' % e) self.debug('Response: code=%s', response.code) # NOTE: Getting a 412 response code means the headers are not in the list of allowed headers. if response.code != 200: raise ZabbixAPIException('HTTP error %s: %s' % (response.status, response.reason)) reads = response.read() if len(reads) == 0: raise ZabbixAPIException('Received zero answer') try: jobj = json.loads(reads.decode('utf-8')) except ValueError as e: self.log(ERROR, 'Unable to decode. returned string: %s', reads) raise ZabbixAPIException('Unable to decode response: %s' % e) self.debug('Response: body=%s', jobj) self.id += 1 if 'error' in jobj: # zabbix API error error = jobj['error'] if isinstance(error, dict): raise ZabbixAPIError(**error) try: return jobj['result'] except KeyError: raise ZabbixAPIException('Missing result in API 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 login(self, user=None, password=None, save=True): """Perform a user.login API request"""
if user and password: if save: self.__username = user self.__password = password elif self.__username and self.__password: user = self.__username password = self.__password else: raise ZabbixAPIException('No authentication information available.') self.last_login = time() # Don't print the raw password hashed_pw_string = 'md5(%s)' % md5(password.encode('utf-8')).hexdigest() self.debug('Trying to login with %r:%r', user, hashed_pw_string) obj = self.json_obj('user.login', params={'user': user, 'password': password}, auth=False) self.__auth = self.do_request(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 relogin(self): """Perform a re-login"""
try: self.__auth = None # reset auth before relogin self.login() except ZabbixAPIException as e: self.log(ERROR, 'Zabbix API relogin error (%s)', e) self.__auth = None # logged_in() will always return False raise
<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_auth(self): """Perform a re-login if not signed in or raise an exception"""
if not self.logged_in: if self.relogin_interval and self.last_login and (time() - self.last_login) > self.relogin_interval: self.log(WARNING, 'Zabbix API not logged in. Performing Zabbix API relogin after %d seconds', self.relogin_interval) self.relogin() # Will raise exception in case of login error else: raise ZabbixAPIException('Not logged 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 call(self, method, params=None): """Check authentication and perform actual API request and relogin if needed"""
start_time = time() self.check_auth() self.log(INFO, '[%s-%05d] Calling Zabbix API method "%s"', start_time, self.id, method) self.log(DEBUG, '\twith parameters: %s', params) try: return self.do_request(self.json_obj(method, params=params)) except ZabbixAPIError as ex: if self.relogin_interval and any(i in ex.error['data'] for i in self.LOGIN_ERRORS): self.log(WARNING, 'Zabbix API not logged in (%s). Performing Zabbix API relogin', ex) self.relogin() # Will raise exception in case of login error return self.do_request(self.json_obj(method, params=params)) raise # Re-raise the exception finally: self.log(INFO, '[%s-%05d] Zabbix API method "%s" finished in %g seconds', start_time, self.id, method, (time() - start_time))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def download_parallel(url, directory, idx, min_file_size = 0, max_file_size = -1, no_redirects = False, pos = 0, mode = 's'): """ download function to download parallely """
global main_it global exit_flag global total_chunks global file_name global i_max file_name[idx]= url.split('/')[-1] file_address = directory + '/' + file_name[idx] is_redirects = not no_redirects resp = s.get(url, stream = True, allow_redirects = is_redirects) if not resp.status_code == 200: # ignore this file since server returns invalid response exit_flag += 1 return try: total_size = int(resp.headers['content-length']) except KeyError: total_size = len(resp.content) total_chunks[idx] = total_size / chunk_size if total_chunks[idx] < min_file_size: # ignore this file since file size is lesser than min_file_size exit_flag += 1 return elif max_file_size != -1 and total_chunks[idx] > max_file_size: # ignore this file since file size is greater than max_file_size exit_flag += 1 return file_iterable = resp.iter_content(chunk_size = chunk_size) with open(file_address, 'wb') as f: for sno, data in enumerate(file_iterable): i_max[idx] = sno + 1 f.write(data) exit_flag += 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 download_parallel_gui(root, urls, directory, min_file_size, max_file_size, no_redirects): """ called when paralled downloading is true """
global parallel # create directory to save files if not os.path.exists(directory): os.makedirs(directory) parallel = True app = progress_class(root, urls, directory, min_file_size, max_file_size, no_redirects)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def download_series_gui(frame, urls, directory, min_file_size, max_file_size, no_redirects): """ called when user wants serial downloading """
# create directory to save files if not os.path.exists(directory): os.makedirs(directory) app = progress_class(frame, urls, directory, min_file_size, max_file_size, no_redirects)
<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): """ function called when thread is started """
global parallel if parallel: download_parallel(self.url, self.directory, self.idx, self.min_file_size, self.max_file_size, self.no_redirects) else: download(self.url, self.directory, self.idx, self.min_file_size, self.max_file_size, self.no_redirects)
<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(self): """ function to initialize thread for downloading """
global parallel for self.i in range(0, self.length): if parallel: self.thread.append(myThread(self.url[ self.i ], self.directory, self.i, self.min_file_size, self.max_file_size, self.no_redirects)) else: # if not parallel whole url list is passed self.thread.append(myThread(self.url, self.directory, self.i , self.min_file_size, self.max_file_size, self.no_redirects)) self.progress[self.i]["value"] = 0 self.bytes[self.i] = 0 self.thread[self.i].start() self.read_bytes()
<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_bytes(self): """ reading bytes; update progress bar after 1 ms """
global exit_flag for self.i in range(0, self.length) : self.bytes[self.i] = i_max[self.i] self.maxbytes[self.i] = total_chunks[self.i] self.progress[self.i]["maximum"] = total_chunks[self.i] self.progress[self.i]["value"] = self.bytes[self.i] self.str[self.i].set(file_name[self.i]+ " " + str(self.bytes[self.i]) + "KB / " + str(int(self.maxbytes[self.i] + 1)) + " KB") if exit_flag == self.length: exit_flag = 0 self.frame.destroy() else: self.frame.after(10, self.read_bytes)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def declare_type(self, declared_type): # type: (TypeDef) -> TypeDef """Add this type to our collection, if needed."""
if declared_type not in self.collected_types: self.collected_types[declared_type.name] = declared_type return declared_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 add_namespaces(metadata, namespaces): # type: (Mapping[Text, Any], MutableMapping[Text, Text]) -> None """Collect the provided namespaces, checking for conflicts."""
for key, value in metadata.items(): if key not in namespaces: namespaces[key] = value elif namespaces[key] != value: raise validate.ValidationException( "Namespace prefix '{}' has conflicting definitions '{}'" " and '{}'.".format(key, namespaces[key], 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 collect_namespaces(metadata): # type: (Mapping[Text, Any]) -> Dict[Text, Text] """Walk through the metadata object, collecting namespace declarations."""
namespaces = {} # type: Dict[Text, Text] if "$import_metadata" in metadata: for value in metadata["$import_metadata"].values(): add_namespaces(collect_namespaces(value), namespaces) if "$namespaces" in metadata: add_namespaces(metadata["$namespaces"], namespaces) return namespaces
<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_schema(schema_ref, # type: Union[CommentedMap, CommentedSeq, Text] cache=None # type: Dict ): """ Load a schema that can be used to validate documents using load_and_validate. return: document_loader, avsc_names, schema_metadata, metaschema_loader """
metaschema_names, _metaschema_doc, metaschema_loader = get_metaschema() if cache is not None: metaschema_loader.cache.update(cache) schema_doc, schema_metadata = metaschema_loader.resolve_ref(schema_ref, "") if not isinstance(schema_doc, MutableSequence): raise ValueError("Schema reference must resolve to a list.") validate_doc(metaschema_names, schema_doc, metaschema_loader, True) metactx = schema_metadata.get("@context", {}) metactx.update(collect_namespaces(schema_metadata)) schema_ctx = jsonld_context.salad_to_jsonld_context(schema_doc, metactx)[0] # Create the loader that will be used to load the target document. document_loader = Loader(schema_ctx, cache=cache) # Make the Avro validation that will be used to validate the target # document avsc_names = make_avro_schema(schema_doc, document_loader) return document_loader, avsc_names, schema_metadata, metaschema_loader
<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_and_validate(document_loader, # type: Loader avsc_names, # type: Names document, # type: Union[CommentedMap, Text] strict, # type: bool strict_foreign_properties=False # type: bool ): """Load a document and validate it with the provided schema. return data, metadata """
try: if isinstance(document, CommentedMap): data, metadata = document_loader.resolve_all( document, document["id"], checklinks=True, strict_foreign_properties=strict_foreign_properties) else: data, metadata = document_loader.resolve_ref( document, checklinks=True, strict_foreign_properties=strict_foreign_properties) validate_doc(avsc_names, data, document_loader, strict, strict_foreign_properties=strict_foreign_properties) return data, metadata except validate.ValidationException as exc: raise validate.ValidationException(strip_dup_lineno(str(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 get_anon_name(rec): # type: (MutableMapping[Text, Any]) -> Text """Calculate a reproducible name for anonymous types."""
if "name" in rec: return rec["name"] anon_name = "" if rec['type'] in ('enum', 'https://w3id.org/cwl/salad#enum'): for sym in rec["symbols"]: anon_name += sym return "enum_"+hashlib.sha1(anon_name.encode("UTF-8")).hexdigest() if rec['type'] in ('record', 'https://w3id.org/cwl/salad#record'): for field in rec["fields"]: anon_name += field["name"] return "record_"+hashlib.sha1(anon_name.encode("UTF-8")).hexdigest() if rec['type'] in ('array', 'https://w3id.org/cwl/salad#array'): return "" raise validate.ValidationException("Expected enum or record, was %s" % rec['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 replace_type(items, spec, loader, found, find_embeds=True, deepen=True): # type: (Any, Dict[Text, Any], Loader, Set[Text], bool, bool) -> Any """ Go through and replace types in the 'spec' mapping"""
if isinstance(items, MutableMapping): # recursively check these fields for types to replace if items.get("type") in ("record", "enum") and items.get("name"): if items["name"] in found: return items["name"] found.add(items["name"]) if not deepen: return items items = copy.copy(items) if not items.get("name"): items["name"] = get_anon_name(items) for name in ("type", "items", "fields"): if name in items: items[name] = replace_type( items[name], spec, loader, found, find_embeds=find_embeds, deepen=find_embeds) if isinstance(items[name], MutableSequence): items[name] = flatten(items[name]) return items if isinstance(items, MutableSequence): # recursively transform list return [replace_type(i, spec, loader, found, find_embeds=find_embeds, deepen=deepen) for i in items] if isinstance(items, string_types): # found a string which is a symbol corresponding to a type. replace_with = None if items in loader.vocab: # If it's a vocabulary term, first expand it to its fully qualified # URI items = loader.vocab[items] if items in spec: # Look up in specialization map replace_with = spec[items] if replace_with: return replace_type(replace_with, spec, loader, found, find_embeds=find_embeds) found.add(items) return 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 avro_name(url): # type: (AnyStr) -> AnyStr """ Turn a URL into an Avro-safe name. If the URL has no fragment, return this plain URL. Extract either the last part of the URL fragment past the slash, otherwise the whole fragment. """
frg = urllib.parse.urldefrag(url)[1] if frg != '': if '/' in frg: return frg[frg.rindex('/') + 1:] return frg return url
<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_valid_avro(items, # type: Avro alltypes, # type: Dict[Text, Dict[Text, Any]] found, # type: Set[Text] union=False # type: bool ): """Convert our schema to be more avro like."""
# Possibly could be integrated into our fork of avro/schema.py? if isinstance(items, MutableMapping): items = copy.copy(items) if items.get("name") and items.get("inVocab", True): items["name"] = avro_name(items["name"]) if "type" in items and items["type"] in ( "https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum", "record", "enum"): if (hasattr(items, "get") and items.get("abstract")) or ("abstract" in items): return items if items["name"] in found: return cast(Text, items["name"]) found.add(items["name"]) for field in ("type", "items", "values", "fields"): if field in items: items[field] = make_valid_avro( items[field], alltypes, found, union=True) if "symbols" in items: items["symbols"] = [avro_name(sym) for sym in items["symbols"]] return items if isinstance(items, MutableSequence): ret = [] for i in items: ret.append(make_valid_avro(i, alltypes, found, union=union)) # type: ignore return ret if union and isinstance(items, string_types): if items in alltypes and avro_name(items) not in found: return cast(Dict, make_valid_avro(alltypes[items], alltypes, found, union=union)) items = avro_name(items) return 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 deepcopy_strip(item): # type: (Any) -> Any """ Make a deep copy of list and dict objects. Intentionally do not copy attributes. This is to discard CommentedMap and CommentedSeq metadata which is very expensive with regular copy.deepcopy. """
if isinstance(item, MutableMapping): return {k: deepcopy_strip(v) for k, v in iteritems(item)} if isinstance(item, MutableSequence): return [deepcopy_strip(k) for k in item] return item
<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_avro_schema(i, # type: List[Any] loader # type: Loader """ All in one convenience function. Call make_avro() and make_avro_schema_from_avro() separately if you need the intermediate result for diagnostic output. """
names = Names() avro = make_avro(i, loader) make_avsc_object(convert_to_dict(avro), names) return 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 shortname(inputid): # type: (Text) -> Text """Returns the last segment of the provided fragment or path."""
parsed_id = urllib.parse.urlparse(inputid) if parsed_id.fragment: return parsed_id.fragment.split(u"/")[-1] return parsed_id.path.split(u"/")[-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 print_inheritance(doc, stream): # type: (List[Dict[Text, Any]], IO) -> None """Write a Grapviz inheritance graph for the supplied document."""
stream.write("digraph {\n") for entry in doc: if entry["type"] == "record": label = name = shortname(entry["name"]) fields = entry.get("fields", []) if fields: label += "\\n* %s\\l" % ( "\\l* ".join(shortname(field["name"]) for field in fields)) shape = "ellipse" if entry.get("abstract") else "box" stream.write("\"%s\" [shape=%s label=\"%s\"];\n" % (name, shape, label)) if "extends" in entry: for target in aslist(entry["extends"]): stream.write("\"%s\" -> \"%s\";\n" % (shortname(target), name)) stream.write("}\n")
<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_fieldrefs(doc, loader, stream): # type: (List[Dict[Text, Any]], Loader, IO) -> None """Write a GraphViz graph of the relationships between the fields."""
obj = extend_and_specialize(doc, loader) primitives = set(("http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/2001/XMLSchema#boolean", "http://www.w3.org/2001/XMLSchema#int", "http://www.w3.org/2001/XMLSchema#long", "https://w3id.org/cwl/salad#null", "https://w3id.org/cwl/salad#enum", "https://w3id.org/cwl/salad#array", "https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#Any")) stream.write("digraph {\n") for entry in obj: if entry.get("abstract"): continue if entry["type"] == "record": label = shortname(entry["name"]) for field in entry.get("fields", []): found = set() # type: Set[Text] field_name = shortname(field["name"]) replace_type(field["type"], {}, loader, found, find_embeds=False) for each_type in found: if each_type not in primitives: stream.write( "\"%s\" -> \"%s\" [label=\"%s\"];\n" % (label, shortname(each_type), field_name)) stream.write("}\n")
<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_other_props(all_props, reserved_props): # type: (Dict, Tuple) -> Optional[Dict] """ Retrieve the non-reserved properties from a dictionary of properties @args reserved_props: The set of reserved properties to exclude """
if hasattr(all_props, 'items') and callable(all_props.items): return dict([(k,v) for (k,v) in list(all_props.items()) if k not in reserved_props]) 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 make_avsc_object(json_data, names=None): # type: (Union[Dict[Text, Text], List[Any], Text], Optional[Names]) -> Schema """ Build Avro Schema from data parsed out of JSON string. @arg names: A Name object (tracks seen names and default space) """
if names is None: names = Names() assert isinstance(names, Names) # JSON object (non-union) if hasattr(json_data, 'get') and callable(json_data.get): # type: ignore assert isinstance(json_data, Dict) atype = cast(Text, json_data.get('type')) other_props = get_other_props(json_data, SCHEMA_RESERVED_PROPS) if atype in PRIMITIVE_TYPES: return PrimitiveSchema(atype, other_props) if atype in NAMED_TYPES: name = cast(Text, json_data.get('name')) namespace = cast(Text, json_data.get('namespace', names.default_namespace)) if atype == 'enum': symbols = cast(List[Text], json_data.get('symbols')) doc = json_data.get('doc') return EnumSchema(name, namespace, symbols, names, doc, other_props) if atype in ['record', 'error']: fields = cast(List, json_data.get('fields')) doc = json_data.get('doc') return RecordSchema(name, namespace, fields, names, atype, doc, other_props) raise SchemaParseException('Unknown Named Type: %s' % atype) if atype in VALID_TYPES: if atype == 'array': items = cast(List, json_data.get('items')) return ArraySchema(items, names, other_props) if atype is None: raise SchemaParseException('No "type" property: %s' % json_data) raise SchemaParseException('Undefined type: %s' % atype) # JSON array (union) if isinstance(json_data, list): return UnionSchema(json_data, names) # JSON string (primitive) if json_data in PRIMITIVE_TYPES: return PrimitiveSchema(cast(Text, json_data)) # not for us! fail_msg = "Could not make an Avro Schema object from %s." % json_data raise SchemaParseException(fail_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 get_space(self): # type: () -> Optional[Text] """Back out a namespace from full name."""
if self._full is None: return None if self._full.find('.') > 0: return self._full.rsplit(".", 1)[0] else: return ""
<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_name(self, name_attr, space_attr, new_schema): # type: (Text, Optional[Text], NamedSchema) -> Name """ Add a new schema object to the name set. @arg name_attr: name value read in schema @arg space_attr: namespace value read in schema. @return: the Name that was just added. """
to_add = Name(name_attr, space_attr, self.default_namespace) if to_add.fullname in VALID_TYPES: fail_msg = '%s is a reserved type name.' % to_add.fullname raise SchemaParseException(fail_msg) elif to_add.fullname in self.names: fail_msg = 'The name "%s" is already in use.' % to_add.fullname raise SchemaParseException(fail_msg) self.names[to_add.fullname] = new_schema return to_add
<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_field_objects(field_data, names): # type: (List[Dict[Text, Text]], Names) -> List[Field] """We're going to need to make message parameters too."""
field_objects = [] field_names = [] # type: List[Text] for field in field_data: if hasattr(field, 'get') and callable(field.get): atype = cast(Text, field.get('type')) name = cast(Text, field.get('name')) # null values can have a default value of None has_default = False default = None if 'default' in field: has_default = True default = field.get('default') order = field.get('order') doc = field.get('doc') other_props = get_other_props(field, FIELD_RESERVED_PROPS) new_field = Field(atype, name, has_default, default, order, names, doc, other_props) # make sure field name has not been used yet if new_field.name in field_names: fail_msg = 'Field name %s already in use.' % new_field.name raise SchemaParseException(fail_msg) field_names.append(new_field.name) else: raise SchemaParseException('Not a valid field: %s' % field) field_objects.append(new_field) return field_objects
<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_function(root1, q, s, f, l, o='g'): """ function to get links """
global links links = search(q, o, s, f, l) root1.destroy() root1.quit()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def task(ft): """ to create loading progress bar """
ft.pack(expand = True, fill = BOTH, side = TOP) pb_hD = ttk.Progressbar(ft, orient = 'horizontal', mode = 'indeterminate') pb_hD.pack(expand = True, fill = BOTH, side = TOP) pb_hD.start(50) ft.mainloop()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def download_content_gui(**args): """ function to fetch links and download them """
global row if not args ['directory']: args ['directory'] = args ['query'].replace(' ', '-') root1 = Frame(root) t1 = threading.Thread(target = search_function, args = (root1, args['query'], args['website'], args['file_type'], args['limit'],args['option'])) t1.start() task(root1) t1.join() #new frame for progress bar row = Frame(root) row.pack() if args['parallel']: download_parallel_gui(row, links, args['directory'], args['min_file_size'], args['max_file_size'], args['no_redirects']) else: download_series_gui(row, links, args['directory'], args['min_file_size'], args['max_file_size'], args['no_redirects'])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def click_download(self, event): """ event for download button """
args ['parallel'] = self.p.get() args ['file_type'] = self.optionmenu.get() args ['no_redirects'] = self.t.get() args ['query'] = self.entry_query.get() args ['min_file_size'] = int( self.entry_min.get()) args ['max_file_size'] = int( self.entry_max.get()) args ['limit'] = int( self.entry_limit.get()) args ['website']= self.entry_website.get() args ['option']= self.engine.get() print(args) self.check_threat() download_content_gui( **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 on_entry_click(self, event): """ function that gets called whenever entry is clicked """
if event.widget.config('fg') [4] == 'grey': event.widget.delete(0, "end" ) # delete all the text in the entry event.widget.insert(0, '') #Insert blank for user input event.widget.config(fg = 'black')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def on_focusout(self, event, a): """ function that gets called whenever anywhere except entry is clicked """
if event.widget.get() == '': event.widget.insert(0, default_text[a]) event.widget.config(fg = 'grey')
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ask_dir(self): """ dialogue box for choosing directory """
args ['directory'] = askdirectory(**self.dir_opt) self.dir_text.set(args ['directory'])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def scrape_links(html, engine): """ function to scrape file links from html response """
soup = BeautifulSoup(html, 'lxml') links = [] if engine == 'd': results = soup.findAll('a', {'class': 'result__a'}) for result in results: link = result.get('href')[15:] link = link.replace('/blob/', '/raw/') links.append(link) elif engine == 'g': results = soup.findAll('h3', {'class': 'r'}) for result in results: link = result.a['href'][7:].split('&')[0] link = link.replace('/blob/', '/raw/') links.append(link) return links
<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_url_nofollow(url): """ function to get return code of a url Credits: http://blog.jasonantman.com/2013/06/python-script-to-check-a-list-of-urls-for-return-code-and-final-return-code-if-redirected/ """
try: response = urlopen(url) code = response.getcode() return code except HTTPError as e: return e.code except: return 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 search(query, engine='g', site="", file_type = 'pdf', limit = 10): """ main function to search for links and return valid ones """
if site == "": search_query = "filetype:{0} {1}".format(file_type, query) else: search_query = "site:{0} filetype:{1} {2}".format(site,file_type, query) headers = { 'User Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) \ Gecko/20100101 Firefox/53.0' } if engine == "g": params = { 'q': search_query, 'start': 0, } links = get_google_links(limit, params, headers) elif engine == "d": params = { 'q': search_query, } links = get_duckduckgo_links(limit,params,headers) else: print("Wrong search engine selected!") sys.exit() valid_links = validate_links(links) return valid_links
<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_args(**args): """ function to check if input query is not None and set missing arguments to default value """
if not args['query']: print("\nMissing required query argument.") sys.exit() for key in DEFAULTS: if key not in args: args[key] = DEFAULTS[key] return 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 download_content(**args): """ main function to fetch links and download them """
args = validate_args(**args) if not args['directory']: args['directory'] = args['query'].replace(' ', '-') print("Downloading {0} {1} files on topic {2} from {3} and saving to directory: {4}" .format(args['limit'], args['file_type'], args['query'], args['website'], args['directory'])) links = search(args['query'], args['engine'], args['website'], args['file_type'], args['limit']) if args['parallel']: download_parallel(links, args['directory'], args['min_file_size'], args['max_file_size'], args['no_redirects']) else: download_series(links, args['directory'], args['min_file_size'], args['max_file_size'], args['no_redirects'])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def show_filetypes(extensions): """ function to show valid file extensions """
for item in extensions.items(): val = item[1] if type(item[1]) == list: val = ", ".join(str(x) for x in item[1]) print("{0:4}: {1}".format(val, item[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 find(self, name): """ Return a list of subset of VM that match the pattern name @param name (str): the vm name of the virtual machine @param name (Obj): the vm object that represent the virtual machine (can be Pro or Smart) @return (list): the subset containing the serach result. """
if name.__class__ is 'base.Server.Pro' or name.__class__ is 'base.Server.Smart': # print('DEBUG: matched VM object %s' % name.__class__) pattern = name.vm_name else: # print('DEBUG: matched Str Object %s' % name.__class__) pattern = name # 14/06/2013: since this method is called within a thread and I wont to pass the return objects with queue or # call back, I will allocate a list inside the Interface class object itself, which contain all of the vm found # 02/11/2015: this must be changed ASAP! it's a mess this way... what was I thinking?? self.last_search_result = [vm for vm in self if pattern in vm.vm_name] return self.last_search_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_hypervisors(self): """ Initialize the internal list containing each template available for each hypervisor. :return: [bool] True in case of success, otherwise False """
json_scheme = self.gen_def_json_scheme('GetHypervisors') json_obj = self.call_method_post(method='GetHypervisors', json_scheme=json_scheme) self.json_templates = json_obj d = dict(json_obj) for elem in d['Value']: hv = self.hypervisors[elem['HypervisorType']] for inner_elem in elem['Templates']: o = Template(hv) o.template_id = inner_elem['Id'] o.descr = inner_elem['Description'] o.id_code = inner_elem['IdentificationCode'] o.name = inner_elem['Name'] o.enabled = inner_elem['Enabled'] if hv != 'SMART': for rb in inner_elem['ResourceBounds']: resource_type = rb['ResourceType'] if resource_type == 1: o.resource_bounds.max_cpu = rb['Max'] if resource_type == 2: o.resource_bounds.max_memory = rb['Max'] if resource_type == 3: o.resource_bounds.hdd0 = rb['Max'] if resource_type == 7: o.resource_bounds.hdd1 = rb['Max'] if resource_type == 8: o.resource_bounds.hdd2 = rb['Max'] if resource_type == 9: o.resource_bounds.hdd3 = rb['Max'] self.templates.append(o) return True if json_obj['Success'] is 'True' else 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 purchase_ip(self, debug=False): """ Return an ip object representing a new bought IP @param debug [Boolean] if true, request and response will be printed @return (Ip): Ip object """
json_scheme = self.gen_def_json_scheme('SetPurchaseIpAddress') json_obj = self.call_method_post(method='SetPurchaseIpAddress', json_scheme=json_scheme, debug=debug) try: ip = Ip() ip.ip_addr = json_obj['Value']['Value'] ip.resid = json_obj['Value']['ResourceId'] return ip except: raise Exception('Unknown error retrieving IP.')
<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_ip(self, ip_id): """ Delete an Ip from the boughs ip list @param (str) ip_id: a string representing the resource id of the IP @return: True if json method had success else False """
ip_id = ' "IpAddressResourceId": %s' % ip_id json_scheme = self.gen_def_json_scheme('SetRemoveIpAddress', ip_id) json_obj = self.call_method_post(method='SetRemoveIpAddress', json_scheme=json_scheme) pprint(json_obj) return True if json_obj['Success'] is True else 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 get_package_id(self, name): """ Retrieve the smart package id given is English name @param (str) name: the Aruba Smart package size name, ie: "small", "medium", "large", "extra large". @return: The package id that depends on the Data center and the size choosen. """
json_scheme = self.gen_def_json_scheme('GetPreConfiguredPackages', dict(HypervisorType=4)) json_obj = self.call_method_post(method='GetPreConfiguredPackages ', json_scheme=json_scheme) for package in json_obj['Value']: packageId = package['PackageID'] for description in package['Descriptions']: languageID = description['LanguageID'] packageName = description['Text'] if languageID == 2 and packageName.lower() == name.lower(): return packageId
<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(self, *args, **kwargs): """Wrapper around Requests for GET requests Returns: Response: A Requests Response object """
if 'timeout' not in kwargs: kwargs['timeout'] = self.timeout req = self.session.get(*args, **kwargs) return req
<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_xml(self, *args, **kwargs): """Wrapper around Requests for GET XML requests Returns: Response: A Requests Response object """
req = self.session_xml.get(*args, **kwargs) return req
<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, *args, **kwargs): """Wrapper around Requests for PUT requests Returns: Response: A Requests Response object """
if 'timeout' not in kwargs: kwargs['timeout'] = self.timeout req = self.session.put(*args, **kwargs) return req
<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, *args, **kwargs): """Wrapper around Requests for DELETE requests Returns: Response: A Requests Response object """
if 'timeout' not in kwargs: kwargs['timeout'] = self.timeout req = self.session.delete(*args, **kwargs) return req
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def auth_ping(self): """Test that application can authenticate to Crowd. Attempts to authenticate the application user against the Crowd server. In order for user authentication to work, an application must be able to authenticate. Returns: bool: True if the application authentication succeeded. """
url = self.rest_url + "/non-existent/location" response = self._get(url) if response.status_code == 401: return False elif response.status_code == 404: return True else: # An error encountered - problem with the Crowd server? 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 auth_user(self, username, password): """Authenticate a user account against the Crowd server. Attempts to authenticate the user against the Crowd server. Args: username: The account username. password: The account password. Returns: dict: A dict mapping of user attributes if the application authentication was successful. See the Crowd documentation for the authoritative list of attributes. None: If authentication failed. """
response = self._post(self.rest_url + "/authentication", data=json.dumps({"value": password}), params={"username": username}) # If authentication failed for any reason return None if not response.ok: return None # ...otherwise return a dictionary of user attributes return response.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_session(self, username, password, remote="127.0.0.1", proxy=None): """Create a session for a user. Attempts to create a user session on the Crowd server. Args: username: The account username. password: The account password. remote: The remote address of the user. This can be used to create multiple concurrent sessions for a user. The host you run this program on may need to be configured in Crowd as a trusted proxy for this to work. proxy: Value of X-Forwarded-For server header. Returns: dict: A dict mapping of user attributes if the application authentication was successful. See the Crowd documentation for the authoritative list of attributes. None: If authentication failed. """
params = { "username": username, "password": password, "validation-factors": { "validationFactors": [ {"name": "remote_address", "value": remote, }, ] } } if proxy: params["validation-factors"]["validationFactors"].append({"name": "X-Forwarded-For", "value": proxy, }) response = self._post(self.rest_url + "/session", data=json.dumps(params), params={"expand": "user"}) # If authentication failed for any reason return None if not response.ok: return None # Otherwise return the user object return response.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 validate_session(self, token, remote="127.0.0.1", proxy=None): """Validate a session token. Validate a previously acquired session token against the Crowd server. This may be a token provided by a user from a http cookie or by some other means. Args: token: The session token. remote: The remote address of the user. proxy: Value of X-Forwarded-For server header Returns: dict: A dict mapping of user attributes if the application authentication was successful. See the Crowd documentation for the authoritative list of attributes. None: If authentication failed. """
params = { "validationFactors": [ {"name": "remote_address", "value": remote, }, ] } if proxy: params["validation-factors"]["validationFactors"].append({"name": "X-Forwarded-For", "value": proxy, }) url = self.rest_url + "/session/%s" % token response = self._post(url, data=json.dumps(params), params={"expand": "user"}) # For consistency between methods use None rather than False # If token validation failed for any reason return None if not response.ok: return None # Otherwise return the user object return response.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 terminate_session(self, token): """Terminates the session token, effectively logging out the user from all crowd-enabled services. Args: token: The session token. Returns: True: If session terminated None: If session termination failed """
url = self.rest_url + "/session/%s" % token response = self._delete(url) # For consistency between methods use None rather than False # If token validation failed for any reason return None if not response.ok: return None # Otherwise return True 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 add_user(self, username, raise_on_error=False, **kwargs): """Add a user to the directory Args: username: The account username raise_on_error: optional (default: False) **kwargs: key-value pairs: password: mandatory email: mandatory first_name: optional last_name: optional display_name: optional active: optional (default True) Returns: True: Succeeded False: If unsuccessful """
# Check that mandatory elements have been provided if 'password' not in kwargs: raise ValueError("missing password") if 'email' not in kwargs: raise ValueError("missing email") # Populate data with default and mandatory values. # A KeyError means a mandatory value was not provided, # so raise a ValueError indicating bad args. try: data = { "name": username, "first-name": username, "last-name": username, "display-name": username, "email": kwargs["email"], "password": {"value": kwargs["password"]}, "active": True } except KeyError: return ValueError # Remove special case 'password' del(kwargs["password"]) # Put values from kwargs into data for k, v in kwargs.items(): new_k = k.replace("_", "-") if new_k not in data: raise ValueError("invalid argument %s" % k) data[new_k] = v response = self._post(self.rest_url + "/user", data=json.dumps(data)) if response.status_code == 201: return True if raise_on_error: raise RuntimeError(response.json()['message']) 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 get_user(self, username): """Retrieve information about a user Returns: dict: User information None: If no user or failure occurred """
response = self._get(self.rest_url + "/user", params={"username": username, "expand": "attributes"}) if not response.ok: return None return response.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 set_active(self, username, active_state): """Set the active state of a user Args: username: The account username active_state: True or False Returns: True: If successful None: If no user or failure occurred """
if active_state not in (True, False): raise ValueError("active_state must be True or False") user = self.get_user(username) if user is None: return None if user['active'] is active_state: # Already in desired state return True user['active'] = active_state response = self._put(self.rest_url + "/user", params={"username": username}, data=json.dumps(user)) if response.status_code == 204: return True 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 change_password(self, username, newpassword, raise_on_error=False): """Change new password for a user Args: username: The account username. newpassword: The account new password. raise_on_error: optional (default: False) Returns: True: Succeeded False: If unsuccessful """
response = self._put(self.rest_url + "/user/password", data=json.dumps({"value": newpassword}), params={"username": username}) if response.ok: return True if raise_on_error: raise RuntimeError(response.json()['message']) 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 get_nested_group_users(self, groupname): """Retrieves a list of all users that directly or indirectly belong to the given groupname. Args: groupname: The group name. Returns: list: A list of strings of user names. """
response = self._get(self.rest_url + "/group/user/nested", params={"groupname": groupname, "start-index": 0, "max-results": 99999}) if not response.ok: return None return [u['name'] for u in response.json()['users']]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def user_exists(self, username): """Determines if the user exists. Args: username: The user name. Returns: bool: True if the user exists in the Crowd application. """
response = self._get(self.rest_url + "/user", params={"username": username}) if not response.ok: return None 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 get_memberships(self): """Fetches all group memberships. Returns: dict: key: group name value: (array of users, array of groups) """
response = self._get_xml(self.rest_url + "/group/membership") if not response.ok: return None xmltree = etree.fromstring(response.content) memberships = {} for mg in xmltree.findall('membership'): # coerce values to unicode in a python 2 and 3 compatible way group = u'{}'.format(mg.get('group')) users = [u'{}'.format(u.get('name')) for u in mg.find('users').findall('user')] groups = [u'{}'.format(g.get('name')) for g in mg.find('groups').findall('group')] memberships[group] = {u'users': users, u'groups': groups} return memberships
<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, entity_type, property_name, search_string, start_index=0, max_results=99999): """Performs a user search using the Crowd search API. https://developer.atlassian.com/display/CROWDDEV/Crowd+REST+Resources#CrowdRESTResources-SearchResource Args: entity_type: 'user' or 'group' property_name: eg. 'email', 'name' search_string: the string to search for. start_index: starting index of the results (default: 0) max_results: maximum number of results returned (default: 99999) Returns: json results: Returns search results. """
params = { "entity-type": entity_type, "expand": entity_type, "property-search-restriction": { "property": {"name": property_name, "type": "STRING"}, "match-mode": "CONTAINS", "value": search_string, } } params = { 'entity-type': entity_type, 'expand': entity_type, 'start-index': start_index, 'max-results': max_results } # Construct XML payload of the form: # <property-search-restriction> # <property> # <name>email</name> # <type>STRING</type> # </property> # <match-mode>EXACTLY_MATCHES</match-mode> # <value>[email protected]</value> # </property-search-restriction> root = etree.Element('property-search-restriction') property_ = etree.Element('property') prop_name = etree.Element('name') prop_name.text = property_name property_.append(prop_name) prop_type = etree.Element('type') prop_type.text = 'STRING' property_.append(prop_type) root.append(property_) match_mode = etree.Element('match-mode') match_mode.text = 'CONTAINS' root.append(match_mode) value = etree.Element('value') value.text = search_string root.append(value) # Construct the XML payload expected by search API payload = '<?xml version="1.0" encoding="UTF-8"?>\n' + etree.tostring(root).decode('utf-8') # We're sending XML but would like a JSON response session = self._build_session(content_type='xml') session.headers.update({'Accept': 'application/json'}) response = session.post(self.rest_url + "/search", params=params, data=payload, timeout=self.timeout) if not response.ok: return None return response.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 versions(self) -> List(BlenderVersion): """ The versions associated with Blender """
return [BlenderVersion(tag) for tag in self.git_repo.tags] + [BlenderVersion(BLENDER_VERSION_MASTER)]
<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): """ Copy libraries from the bin directory and place them as appropriate """
self.announce("Moving library files", level=3) # We have already built the libraries in the previous build_ext step self.skip_build = True bin_dir = self.distribution.bin_dir libs = [os.path.join(bin_dir, _lib) for _lib in os.listdir(bin_dir) if os.path.isfile(os.path.join(bin_dir, _lib)) and os.path.splitext(_lib)[1] in [".dll", ".so"] and not (_lib.startswith("python") or _lib.startswith("bpy"))] for lib in libs: shutil.move(lib, os.path.join(self.build_dir, os.path.basename(lib))) # Mark the libs for installation, adding them to # distribution.data_files seems to ensure that setuptools' record # writer appends them to installed-files.txt in the package's egg-info # # Also tried adding the libraries to the distribution.libraries list, # but that never seemed to add them to the installed-files.txt in the # egg-info, and the online recommendation seems to be adding libraries # into eager_resources in the call to setup(), which I think puts them # in data_files anyways. # # What is the best way? self.distribution.data_files = [os.path.join(self.install_dir, os.path.basename(lib)) for lib in libs] # Must be forced to run after adding the libs to data_files self.distribution.run_command("install_data") super().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 run(self): """ Perform build_cmake before doing the 'normal' stuff """
for extension in self.extensions: if extension.name == "bpy": self.build_cmake(extension) super().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 setup_oauth(username, password, basemaps_token_uri, authcfg_id=AUTHCFG_ID, authcfg_name=AUTHCFG_NAME): """Setup oauth configuration to access the BCS API, return authcfg_id on success, None on failure """
cfgjson = { "accessMethod" : 0, "apiKey" : "", "clientId" : "", "clientSecret" : "", "configType" : 1, "grantFlow" : 2, "password" : password, "persistToken" : False, "redirectPort" : '7070', "redirectUrl" : "", "refreshTokenUrl" : "", "requestTimeout" : '30', "requestUrl" : "", "scope" : "", "state" : "", "tokenUrl" : basemaps_token_uri, "username" : username, "version" : 1 } if authcfg_id not in auth_manager().availableAuthMethodConfigs(): authConfig = QgsAuthMethodConfig('OAuth2') authConfig.setId(authcfg_id) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if auth_manager().storeAuthenticationConfig(authConfig): return authcfg_id else: authConfig = QgsAuthMethodConfig() auth_manager().loadAuthenticationConfig(authcfg_id, authConfig, True) authConfig.setName(authcfg_name) authConfig.setConfig('oauth2config', json.dumps(cfgjson)) if auth_manager().updateAuthenticationConfig(authConfig): return authcfg_id 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 in_search_queryset(self, instance_id, index="_all"): """ Return True if an object is part of the search index queryset. Sometimes it's useful to know if an object _should_ be indexed. If an object is saved, how do you know if you should push that change to the search index? The simplest (albeit not most efficient) way is to check if it appears in the underlying search queryset. NB this method doesn't evaluate the entire dataset, it chains an additional queryset filter expression on the end. That's why it's important that the `get_search_queryset` method returns a queryset. Args: instance_id: the id of model object that we are looking for. Kwargs: index: string, the name of the index in which to check. Defaults to '_all'. """
return self.get_search_queryset(index=index).filter(pk=instance_id).exists()
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _raw_sql(self, values): """Prepare SQL statement consisting of a sequence of WHEN .. THEN statements."""
if isinstance(self.model._meta.pk, CharField): when_clauses = " ".join( [self._when("'{}'".format(x), y) for (x, y) in values] ) else: when_clauses = " ".join([self._when(x, y) for (x, y) in values]) table_name = self.model._meta.db_table primary_key = self.model._meta.pk.column return 'SELECT CASE {}."{}" {} ELSE 0 END'.format( table_name, primary_key, when_clauses )
<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_document_cache_key(self): """Key used for storing search docs in local cache."""
return "elasticsearch_django:{}.{}.{}".format( self._meta.app_label, self._meta.model_name, self.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 _is_field_serializable(self, field_name): """Return True if the field can be serialized into a JSON doc."""
return ( self._meta.get_field(field_name).get_internal_type() in self.SIMPLE_UPDATE_FIELD_TYPES )
<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_update_fields(self, index, update_fields): """ Clean the list of update_fields based on the index being updated.\ If any field in the update_fields list is not in the set of properties defined by the index mapping for this model, then we ignore it. If a field _is_ in the mapping, but the underlying model field is a related object, and thereby not directly serializable, then this method will raise a ValueError. """
search_fields = get_model_index_properties(self, index) clean_fields = [f for f in update_fields if f in search_fields] ignore = [f for f in update_fields if f not in search_fields] if ignore: logger.debug( "Ignoring fields from partial update: %s", [f for f in update_fields if f not in search_fields], ) for f in clean_fields: if not self._is_field_serializable(f): raise ValueError( "'%s' cannot be automatically serialized into a search document property. Please override as_search_document_update.", f, ) return clean_fields
<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_search_document_update(self, *, index, update_fields): """ Return a partial update document based on which fields have been updated. If an object is saved with the `update_fields` argument passed through, then it is assumed that this is a 'partial update'. In this scenario we need a {property: value} dictionary containing just the fields we want to update. This method handles two possible update strategies - 'full' or 'partial'. The default 'full' strategy simply returns the value of `as_search_document` - thereby replacing the entire document each time. The 'partial' strategy is more intelligent - it will determine whether the fields passed are in the search document mapping, and return a partial update document that contains only those that are. In addition, if any field that _is_ included cannot be automatically serialized (e.g. a RelatedField object), then this method will raise a ValueError. In this scenario, you should override this method in your subclass. You may also wish to subclass this method to perform field-specific logic - in this example if only the timestamp is being saved, then ignore the update if the timestamp is later than a certain time. """
if UPDATE_STRATEGY == UPDATE_STRATEGY_FULL: return self.as_search_document(index=index) if UPDATE_STRATEGY == UPDATE_STRATEGY_PARTIAL: # in partial mode we update the intersection of update_fields and # properties found in the mapping file. return { k: getattr(self, k) for k in self.clean_update_fields( index=index, update_fields=update_fields ) }
<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_search_action(self, *, index, action): """ Return an object as represented in a bulk api operation. Bulk API operations have a very specific format. This function will call the standard `as_search_document` method on the object and then wrap that up in the correct format for the action specified. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html Args: index: string, the name of the index in which the action is to be taken. Bulk operations are only every carried out on a single index at a time. action: string ['index' | 'update' | 'delete'] - this decides how the final document is formatted. Returns a dictionary. """
if action not in ("index", "update", "delete"): raise ValueError("Action must be 'index', 'update' or 'delete'.") document = { "_index": index, "_type": self.search_doc_type, "_op_type": action, "_id": self.pk, } if action == "index": document["_source"] = self.as_search_document(index=index) elif action == "update": document["doc"] = self.as_search_document(index=index) return document