{"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef drop_columns(\n self, max_na_values: int = None, max_unique_values: int = None\n ):\n \"\"\"\n When max_na_values was informed, remove columns when the proportion of\n total NA values more than max_na_values threshold.\n\n When max_unique_values was informed, remove columns when the proportion\n of the total of unique values is more than the max_unique_values\n threshold, just for columns with type as object or category.\n\n :param max_na_values: proportion threshold of max na values\n :param max_unique_values:\n :return:\n \"\"\"\n step = {}\n\n if max_na_values is not None:\n step = {\n 'data-set': self.iid,\n 'operation': 'drop-na',\n 'expression': '{\"max_na_values\":%s, \"axis\": 1}' % max_na_values\n }\n if max_unique_values is not None:\n step = {\n 'data-set': self.iid,\n 'operation': 'drop-unique',\n 'expression': '{\"max_unique_values\":%s}' % max_unique_values\n }\n self.attr_update(attr='steps', value=[step])", "response": "Drop columns when the proportion of the total NA values is more than max_na_values threshold."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndrop the missing elements from the sequence.", "response": "def dropna(self):\n \"\"\"\n :return:\n\n \"\"\"\n step = {\n 'data-set': self.iid,\n 'operation': 'drop-na',\n 'expression': '{\"axis\": 0}'\n }\n\n self.attr_update(attr='steps', value=[step])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlog a message to the log file", "response": "def log(self, message: str):\n \"\"\"\n @deprecated\n\n :param message:\n :return:\n\n \"\"\"\n dset_log_id = '_%s_log' % self.iid\n\n if dset_log_id not in self.parent.data.keys():\n dset = self.parent.data.create_dataset(\n dset_log_id, shape=(1,),\n dtype=np.dtype([\n ('dt_log', ' pd.DataFrame:\n if compute or self.result is None:\n self.compute()\n return summary(self.result)", "response": "Returns a DataFrame with the summary of the result of the method."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef serve(request, server):\n\n request_ = Request('/'.join(request.postpath),\n request.content.read() if not request.method == 'GET' else '',\n request.args, request.args, {})\n\n response = server.process_request(request_)\n\n request.setHeader('Content-Type', response.content_type)\n request.setResponseCode(get_http_response_code_num(response))\n\n return response.text", "response": "This function is a Twisted Web adapter. It is a pyws request object and the server object is passed to the Twisted web server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_sorted_dependencies(service_model):\n app_models = list(service_model._meta.app_config.get_models())\n dependencies = {model: set() for model in app_models}\n relations = (\n relation\n for model in app_models\n for relation in model._meta.related_objects\n if relation.on_delete in (models.PROTECT, models.CASCADE)\n )\n for rel in relations:\n dependencies[rel.model].add(rel.related_model)\n return stable_topological_sort(app_models, dependencies)", "response": "Returns list of application models in topological order."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the instance fields based on imported from backend data.", "response": "def update_pulled_fields(instance, imported_instance, fields):\n \"\"\"\n Update instance fields based on imported from backend data.\n Save changes to DB only one or more fields were changed.\n \"\"\"\n modified = False\n for field in fields:\n pulled_value = getattr(imported_instance, field)\n current_value = getattr(instance, field)\n if current_value != pulled_value:\n setattr(instance, field, pulled_value)\n logger.info(\"%s's with PK %s %s field updated from value '%s' to value '%s'\",\n instance.__class__.__name__, instance.pk, field, current_value, pulled_value)\n modified = True\n error_message = getattr(imported_instance, 'error_message', '') or getattr(instance, 'error_message', '')\n if error_message and instance.error_message != error_message:\n instance.error_message = imported_instance.error_message\n modified = True\n if modified:\n instance.save()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling a resource not found error.", "response": "def handle_resource_not_found(resource):\n \"\"\"\n Set resource state to ERRED and append/create \"not found\" error message.\n \"\"\"\n resource.set_erred()\n resource.runtime_state = ''\n message = 'Does not exist at backend.'\n if message not in resource.error_message:\n if not resource.error_message:\n resource.error_message = message\n else:\n resource.error_message += ' (%s)' % message\n resource.save()\n logger.warning('%s %s (PK: %s) does not exist at backend.' % (\n resource.__class__.__name__, resource, resource.pk))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef handle_resource_update_success(resource):\n update_fields = []\n if resource.state == resource.States.ERRED:\n resource.recover()\n update_fields.append('state')\n\n if resource.state in (resource.States.UPDATING, resource.States.CREATING):\n resource.set_ok()\n update_fields.append('state')\n\n if resource.error_message:\n resource.error_message = ''\n update_fields.append('error_message')\n\n if update_fields:\n resource.save(update_fields=update_fields)\n logger.warning('%s %s (PK: %s) was successfully updated.' % (\n resource.__class__.__name__, resource, resource.pk))", "response": "Handle the update success of a resource."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmarking the instance as erred and save error message", "response": "def set_instance_erred(self, instance, error_message):\n \"\"\" Mark instance as erred and save error message \"\"\"\n instance.set_erred()\n instance.error_message = error_message\n instance.save(update_fields=['state', 'error_message'])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmap a language to a language code.", "response": "def map_language(language, dash3=True):\n \"\"\" Use ISO 639-3 ?? \"\"\"\n if dash3:\n from iso639 import languages\n else:\n from pycountry import languages\n\n if '_' in language:\n language = language.split('_')[0]\n if len(language) == 2:\n try: return languages.get(alpha2=language.lower())\n except KeyError: pass\n elif len(language) == 3:\n if dash3:\n try: return languages.get(part3=language.lower())\n except KeyError: pass\n try: return languages.get(terminology=language.lower())\n except KeyError: pass\n try: return languages.get(bibliographic=language.lower())\n except KeyError: pass\n else:\n try: return languages.get(name=language.title())\n except KeyError: pass\n if dash3:\n try: return languages.get(inverted=language.title())\n except KeyError: pass\n for l in re.split('[,.;: ]+', language):\n try: return languages.get(name=l.title())\n except KeyError: pass"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_task_signature(cls, instance, serialized_instance, **kwargs):\n cleanup_tasks = [\n ProjectResourceCleanupTask().si(\n core_utils.serialize_class(executor_cls),\n core_utils.serialize_class(model_cls),\n serialized_instance,\n )\n for (model_cls, executor_cls) in cls.executors\n ]\n\n if not cleanup_tasks:\n return core_tasks.EmptyTask()\n\n return chain(cleanup_tasks)", "response": "Returns a task signature for the given instance."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns an OrderedDict ordered by key names from the unsorted_dict", "response": "def sort_dict(unsorted_dict):\n \"\"\"\n Return a OrderedDict ordered by key names from the :unsorted_dict:\n \"\"\"\n sorted_dict = OrderedDict()\n # sort items before inserting them into a dict\n for key, value in sorted(unsorted_dict.items(), key=itemgetter(0)):\n sorted_dict[key] = value\n return sorted_dict"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nformatting time_and_value_list to time segments", "response": "def format_time_and_value_to_segment_list(time_and_value_list, segments_count, start_timestamp,\n end_timestamp, average=False):\n \"\"\"\n Format time_and_value_list to time segments\n\n Parameters\n ^^^^^^^^^^\n time_and_value_list: list of tuples\n Have to be sorted by time\n Example: [(time, value), (time, value) ...]\n segments_count: integer\n How many segments will be in result\n Returns\n ^^^^^^^\n List of dictionaries\n Example:\n [{'from': time1, 'to': time2, 'value': sum_of_values_from_time1_to_time2}, ...]\n \"\"\"\n segment_list = []\n time_step = (end_timestamp - start_timestamp) / segments_count\n for i in range(segments_count):\n segment_start_timestamp = start_timestamp + time_step * i\n segment_end_timestamp = segment_start_timestamp + time_step\n value_list = [\n value for time, value in time_and_value_list\n if time >= segment_start_timestamp and time < segment_end_timestamp]\n segment_value = sum(value_list)\n if average and len(value_list) != 0:\n segment_value /= len(value_list)\n\n segment_list.append({\n 'from': segment_start_timestamp,\n 'to': segment_end_timestamp,\n 'value': segment_value,\n })\n return segment_list"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nserializing Django model instance into a string.", "response": "def serialize_instance(instance):\n \"\"\" Serialize Django model instance \"\"\"\n model_name = force_text(instance._meta)\n return '{}:{}'.format(model_name, instance.pk)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef instance_from_url(url, user=None):\n # XXX: This circular dependency will be removed then filter_queryset_for_user\n # will be moved to model manager method\n from waldur_core.structure.managers import filter_queryset_for_user\n\n url = clear_url(url)\n match = resolve(url)\n model = get_model_from_resolve_match(match)\n queryset = model.objects.all()\n if user is not None:\n queryset = filter_queryset_for_user(model.objects.all(), user)\n return queryset.get(**match.kwargs)", "response": "Restore instance from URL."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncloses this transaction. If this transaction is the base transaction in a begin/commit nesting, the transaction will rollback(). Otherwise, the method returns. This is used to cancel a Transaction without affecting the scope of an enclosing transaction.", "response": "def close(self):\n \"\"\"\n Close this transaction.\n\n If this transaction is the base transaction in a begin/commit\n nesting, the transaction will rollback(). Otherwise, the\n method returns.\n\n This is used to cancel a Transaction without affecting the scope of\n an enclosing transaction.\n \"\"\"\n if not self._connection or not self._parent:\n return\n if not self._parent._is_active:\n # pragma: no cover\n self._connection = None\n # self._parent = None\n return\n if self._parent is self:\n yield from self.rollback()\n else:\n self._is_active = False\n self._connection = None\n self._parent = None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn an app config for the given name.", "response": "def _get_app_config(self, app_name):\n \"\"\"\n Returns an app config for the given name, not by label.\n \"\"\"\n\n matches = [app_config for app_config in apps.get_app_configs()\n if app_config.name == app_name]\n if not matches:\n return\n return matches[0]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_app_version(self, app_config):\n\n base_name = app_config.__module__.split('.')[0]\n module = __import__(base_name)\n return getattr(module, '__version__', 'N/A')", "response": "Get the version of the application."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_quick_access_info(self):\n quick_access_links = []\n\n # add custom links\n quick_access_links.extend(settings.FLUENT_DASHBOARD_QUICK_ACCESS_LINKS)\n\n for model in (structure_models.Project,\n structure_models.Customer,\n core_models.User,\n structure_models.SharedServiceSettings):\n quick_access_links.append(self._get_link_to_model(model))\n\n return quick_access_links", "response": "Returns a list of ListLink items to be added to Quick Access tab."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_erred_shared_settings_module(self):\n result_module = modules.LinkList(title=_('Shared provider settings in erred state'))\n result_module.template = 'admin/dashboard/erred_link_list.html'\n erred_state = structure_models.SharedServiceSettings.States.ERRED\n\n queryset = structure_models.SharedServiceSettings.objects\n settings_in_erred_state = queryset.filter(state=erred_state).count()\n\n if settings_in_erred_state:\n result_module.title = '%s (%s)' % (result_module.title, settings_in_erred_state)\n for service_settings in queryset.filter(state=erred_state).iterator():\n module_child = self._get_link_to_instance(service_settings)\n module_child['error'] = service_settings.error_message\n result_module.children.append(module_child)\n else:\n result_module.pre_content = _('Nothing found.')\n\n return result_module", "response": "Returns a LinkList based module which contains link to shared service setting instances in ERRED state."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of links to resources which are in ERRED state and linked to a shared service settings.", "response": "def _get_erred_resources_module(self):\n \"\"\"\n Returns a list of links to resources which are in ERRED state and linked to a shared service settings.\n \"\"\"\n result_module = modules.LinkList(title=_('Resources in erred state'))\n erred_state = structure_models.NewResource.States.ERRED\n children = []\n\n resource_models = SupportedServices.get_resource_models()\n resources_in_erred_state_overall = 0\n for resource_type, resource_model in resource_models.items():\n queryset = resource_model.objects.filter(service_project_link__service__settings__shared=True)\n erred_amount = queryset.filter(state=erred_state).count()\n if erred_amount:\n resources_in_erred_state_overall = resources_in_erred_state_overall + erred_amount\n link = self._get_erred_resource_link(resource_model, erred_amount, erred_state)\n children.append(link)\n\n if resources_in_erred_state_overall:\n result_module.title = '%s (%s)' % (result_module.title, resources_in_erred_state_overall)\n result_module.children = children\n else:\n result_module.pre_content = _('Nothing found.')\n\n return result_module"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef serve(request, tail, server):\n\n if request.GET:\n body = ''\n else:\n try:\n body = request.body\n except AttributeError:\n body = request.raw_post_data\n\n request = Request(\n tail,\n body,\n parse_qs(request.META['QUERY_STRING']),\n parse_qs(body),\n request.COOKIES,\n )\n\n response = server.process_request(request)\n\n return HttpResponse(\n response.text, content_type=response.content_type,\n status=get_http_response_code_num(response))", "response": "This function serves a Django application."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the index of the given item in the list.", "response": "def index(self, nurest_object):\n \"\"\" Get index of the given item\n Args:\n nurest_object (bambou.NURESTObject): the NURESTObject object to verify\n\n Returns:\n Returns the position of the object.\n\n Raises:\n Raise a ValueError exception if object is not present\n \"\"\"\n for index, obj in enumerate(self):\n if obj.equals(nurest_object):\n return index\n\n raise ValueError(\"%s is not in %s\" % (nurest_object, self))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nregistering the fetcher for a served object.", "response": "def fetcher_with_object(cls, parent_object, relationship=\"child\"):\n \"\"\" Register the fetcher for a served object.\n\n This method will fill the fetcher with `managed_class` instances\n\n Args:\n parent_object: the instance of the parent object to serve\n\n Returns:\n It returns the fetcher instance.\n \"\"\"\n\n fetcher = cls()\n fetcher.parent_object = parent_object\n fetcher.relationship = relationship\n\n rest_name = cls.managed_object_rest_name()\n parent_object.register_fetcher(fetcher, rest_name)\n\n return fetcher"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprepares the headers for the given request based on the filter and order_by parameters.", "response": "def _prepare_headers(self, request, filter=None, order_by=None, group_by=[], page=None, page_size=None):\n \"\"\" Prepare headers for the given request\n\n Args:\n request: the NURESTRequest to send\n filter: string\n order_by: string\n group_by: list of names\n page: int\n page_size: int\n \"\"\"\n\n if filter:\n request.set_header('X-Nuage-Filter', filter)\n\n if order_by:\n request.set_header('X-Nuage-OrderBy', order_by)\n\n if page is not None:\n request.set_header('X-Nuage-Page', str(page))\n\n if page_size:\n request.set_header('X-Nuage-PageSize', str(page_size))\n\n if len(group_by) > 0:\n header = \", \".join(group_by)\n request.set_header('X-Nuage-GroupBy', 'true')\n request.set_header('X-Nuage-Attributes', header)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfetch all managed class objects from the server.", "response": "def fetch(self, filter=None, order_by=None, group_by=[], page=None, page_size=None, query_parameters=None, commit=True, async=False, callback=None):\n \"\"\" Fetch objects according to given filter and page.\n\n Note:\n This method fetches all managed class objects and store them\n in local_name of the served object. which means that the parent\n object will hold them in a list. You can prevent this behavior\n by setting commit to False. In that case, the fetched children\n won't be added in the parent object cache.\n\n Args:\n filter (string): string that represents a predicate filter\n order_by (string): string that represents an order by clause\n group_by (string): list of names for grouping\n page (int): number of the page to load\n page_size (int): number of results per page\n commit (bool): boolean to update current object\n callback (function): Callback that should be called in case of a async request\n\n Returns:\n tuple: Returns a tuple of information (fetcher, served object, fetched objects, connection)\n\n Example:\n >>> entity.children.fetch()\n (, , [, ], )\n \"\"\"\n\n request = NURESTRequest(method=HTTP_METHOD_GET, url=self._prepare_url(), params=query_parameters)\n\n self._prepare_headers(request=request, filter=filter, order_by=order_by, group_by=group_by, page=page, page_size=page_size)\n\n if async:\n return self.parent_object.send_request(request=request, async=async, local_callback=self._did_fetch, remote_callback=callback, user_info={'commit': commit})\n\n connection = self.parent_object.send_request(request=request, user_info={'commit': commit})\n return self._did_fetch(connection=connection)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching objects has been done", "response": "def _did_fetch(self, connection):\n \"\"\" Fetching objects has been done \"\"\"\n\n self.current_connection = connection\n response = connection.response\n should_commit = 'commit' not in connection.user_info or connection.user_info['commit']\n\n if connection.response.status_code >= 400 and BambouConfig._should_raise_bambou_http_error:\n raise BambouHTTPError(connection=connection)\n\n if response.status_code != 200:\n\n if should_commit:\n self.current_total_count = 0\n self.current_page = 0\n self.current_ordered_by = ''\n\n return self._send_content(content=None, connection=connection)\n\n results = response.data\n fetched_objects = list()\n current_ids = list()\n\n if should_commit:\n if 'X-Nuage-Count' in response.headers and response.headers['X-Nuage-Count']:\n self.current_total_count = int(response.headers['X-Nuage-Count'])\n\n if 'X-Nuage-Page' in response.headers and response.headers['X-Nuage-Page']:\n self.current_page = int(response.headers['X-Nuage-Page'])\n\n if 'X-Nuage-OrderBy' in response.headers and response.headers['X-Nuage-OrderBy']:\n self.current_ordered_by = response.headers['X-Nuage-OrderBy']\n\n if results:\n for result in results:\n nurest_object = self.new()\n nurest_object.from_dict(result)\n nurest_object.parent = self.parent_object\n\n fetched_objects.append(nurest_object)\n\n if not should_commit:\n continue\n\n current_ids.append(nurest_object.id)\n\n if nurest_object in self:\n idx = self.index(nurest_object)\n current_object = self[idx]\n current_object.from_dict(nurest_object.to_dict())\n else:\n self.append(nurest_object)\n\n if should_commit:\n for obj in self:\n if obj.id not in current_ids:\n self.remove(obj)\n\n return self._send_content(content=fetched_objects, connection=connection)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get(self, filter=None, order_by=None, group_by=[], page=None, page_size=None, query_parameters=None, commit=True, async=False, callback=None):\n return self.fetch(filter=filter, order_by=order_by, group_by=group_by, page=page, page_size=page_size, query_parameters=query_parameters, commit=commit)[2]", "response": "Fetch object and directly return them\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_first(self, filter=None, order_by=None, group_by=[], query_parameters=None, commit=False, async=False, callback=None):\n objects = self.get(filter=filter, order_by=order_by, group_by=group_by, page=0, page_size=1, query_parameters=query_parameters, commit=commit)\n return objects[0] if len(objects) else None", "response": "Fetch object and directly return the first one\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef count(self, filter=None, order_by=None, group_by=[], page=None, page_size=None, query_parameters=None, async=False, callback=None):\n request = NURESTRequest(method=HTTP_METHOD_HEAD, url=self._prepare_url(), params=query_parameters)\n\n self._prepare_headers(request=request, filter=filter, order_by=order_by, group_by=group_by, page=page, page_size=page_size)\n\n if async:\n return self.parent_object.send_request(request=request, async=async, local_callback=self._did_count, remote_callback=callback)\n\n else:\n connection = self.parent_object.send_request(request=request)\n return self._did_count(connection)", "response": "This method returns the total count of objects that can be fetched according to filter\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_count(self, filter=None, order_by=None, group_by=[], page=None, page_size=None, query_parameters=None):\n return self.count(filter=filter, order_by=order_by, group_by=group_by, page=page, page_size=page_size, query_parameters=query_parameters, async=False)[2]", "response": "Get the total count of objects that can be fetched according to filter\n\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall when count is finished", "response": "def _did_count(self, connection):\n \"\"\" Called when count if finished \"\"\"\n\n self.current_connection = connection\n response = connection.response\n count = 0\n callback = None\n\n if 'X-Nuage-Count' in response.headers:\n count = int(response.headers['X-Nuage-Count'])\n\n if 'remote' in connection.callbacks:\n callback = connection.callbacks['remote']\n\n if connection.async:\n if callback:\n callback(self, self.parent_object, count)\n\n self.current_connection.reset()\n self.current_connection = None\n else:\n\n if connection.response.status_code >= 400 and BambouConfig._should_raise_bambou_http_error:\n raise BambouHTTPError(connection=connection)\n\n return (self, self.parent_object, count)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _send_content(self, content, connection):\n\n if connection:\n\n if connection.async:\n callback = connection.callbacks['remote']\n\n if callback:\n callback(self, self.parent_object, content)\n\n self.current_connection.reset()\n self.current_connection = None\n else:\n return (self, self.parent_object, content)", "response": "Send a content array from the connection"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating the current configuration and the consumption details.", "response": "def update_configuration(self, new_configuration):\n \"\"\" Save how much consumables were used and update current configuration.\n\n Return True if configuration changed.\n \"\"\"\n if new_configuration == self.configuration:\n return False\n now = timezone.now()\n if now.month != self.price_estimate.month:\n raise ConsumptionDetailUpdateError('It is possible to update consumption details only for current month.')\n minutes_from_last_update = self._get_minutes_from_last_update(now)\n for consumable_item, usage in self.configuration.items():\n consumed_after_modification = usage * minutes_from_last_update\n self.consumed_before_update[consumable_item] = (\n self.consumed_before_update.get(consumable_item, 0) + consumed_after_modification)\n self.configuration = new_configuration\n self.last_update_time = now\n self.save()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef consumed_in_month(self):\n month_end = core_utils.month_end(datetime.date(self.price_estimate.year, self.price_estimate.month, 1))\n return self._get_consumed(month_end)", "response": "How many resources were consumed in the month"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the number of consumables that were used by resource until given time.", "response": "def _get_consumed(self, time):\n \"\"\" How many consumables were (or will be) used by resource until given time. \"\"\"\n minutes_from_last_update = self._get_minutes_from_last_update(time)\n if minutes_from_last_update < 0:\n raise ConsumptionDetailCalculateError('Cannot calculate consumption if time < last modification date.')\n _consumed = {}\n for consumable_item in set(list(self.configuration.keys()) + list(self.consumed_before_update.keys())):\n after_update = self.configuration.get(consumable_item, 0) * minutes_from_last_update\n before_update = self.consumed_before_update.get(consumable_item, 0)\n _consumed[consumable_item] = after_update + before_update\n return _consumed"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the number of minutes from last update to given time", "response": "def _get_minutes_from_last_update(self, time):\n \"\"\" How much minutes passed from last update to given time \"\"\"\n time_from_last_update = time - self.last_update_time\n return int(time_from_last_update.total_seconds() / 60)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets list of all price list items that should be used for a given resource.", "response": "def get_for_resource(resource):\n \"\"\" Get list of all price list items that should be used for resource.\n\n If price list item is defined for service - return it, otherwise -\n return default price list item.\n \"\"\"\n resource_content_type = ContentType.objects.get_for_model(resource)\n default_items = set(DefaultPriceListItem.objects.filter(resource_content_type=resource_content_type))\n service = resource.service_project_link.service\n items = set(PriceListItem.objects.filter(\n default_price_list_item__in=default_items, service=service).select_related('default_price_list_item'))\n rewrited_defaults = set([i.default_price_list_item for i in items])\n return items | (default_items - rewrited_defaults)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a list of available extensions for a given class.", "response": "def get_extensions(cls):\n \"\"\" Get a list of available extensions \"\"\"\n assemblies = []\n for waldur_extension in pkg_resources.iter_entry_points('waldur_extensions'):\n extension_module = waldur_extension.load()\n if inspect.isclass(extension_module) and issubclass(extension_module, cls):\n if not extension_module.is_assembly():\n yield extension_module\n else:\n assemblies.append(extension_module)\n for assembly in assemblies:\n yield assembly"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef ellipsis(source, max_length):\n if max_length == 0 or len(source) <= max_length:\n return source\n return source[: max(0, max_length - 3)] + \"...\"", "response": "Truncates a string to be at most max_length long."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrap for str that catches exceptions and returns an ellipsis string.", "response": "def safe_str(source, max_length=0):\n \"\"\"Wrapper for str() that catches exceptions.\"\"\"\n try:\n return ellipsis(str(source), max_length)\n except Exception as e:\n return ellipsis(\"\" % e, max_length)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef safe_repr(source, max_length=0):\n try:\n return ellipsis(repr(source), max_length)\n except Exception as e:\n return ellipsis(\"\" % e, max_length)", "response": "Wrapper for repr that catches exceptions and returns an ellipsis string."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns an object with the key - value pairs in source as attributes.", "response": "def dict_to_object(source):\n \"\"\"Returns an object with the key-value pairs in source as attributes.\"\"\"\n target = inspectable_class.InspectableClass()\n for k, v in source.items():\n setattr(target, k, v)\n return target"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nshallowing copies all public attributes from source_obj to dest_obj.", "response": "def copy_public_attrs(source_obj, dest_obj):\n \"\"\"Shallow copies all public attributes from source_obj to dest_obj.\n\n Overwrites them if they already exist.\n\n \"\"\"\n for name, value in inspect.getmembers(source_obj):\n if not any(name.startswith(x) for x in [\"_\", \"func\", \"im\"]):\n setattr(dest_obj, name, value)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef object_from_string(name):\n if six.PY3:\n if not isinstance(name, str):\n raise TypeError(\"name must be str, not %r\" % type(name))\n else:\n if isinstance(name, unicode):\n name = name.encode(\"ascii\")\n if not isinstance(name, (str, unicode)):\n raise TypeError(\"name must be bytes or unicode, got %r\" % type(name))\n\n pos = name.rfind(\".\")\n if pos < 0:\n raise ValueError(\"Invalid function or class name %s\" % name)\n module_name = name[:pos]\n func_name = name[pos + 1 :]\n try:\n mod = __import__(module_name, fromlist=[func_name], level=0)\n except ImportError:\n # Hail mary. if the from import doesn't work, then just import the top level module\n # and do getattr on it, one level at a time. This will handle cases where imports are\n # done like `from . import submodule as another_name`\n parts = name.split(\".\")\n mod = __import__(parts[0], level=0)\n for i in range(1, len(parts)):\n mod = getattr(mod, parts[i])\n return mod\n\n else:\n return getattr(mod, func_name)", "response": "Creates a Python class or function object from its fully qualified name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef catchable_exceptions(exceptions):\n if isinstance(exceptions, type) and issubclass(exceptions, BaseException):\n return True\n\n if (\n isinstance(exceptions, tuple)\n and exceptions\n and all(issubclass(it, BaseException) for it in exceptions)\n ):\n return True\n\n return False", "response": "Returns True if the given exceptions can be caught in the except clause."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef validate_object_action(self, action_name, obj=None):\n action_method = getattr(self, action_name)\n if not getattr(action_method, 'detail', False) and action_name not in ('update', 'partial_update', 'destroy'):\n # DRF does not add flag 'detail' to update and delete actions, however they execute operation with\n # particular object. We need to enable validation for them too.\n return\n validators = getattr(self, action_name + '_validators', [])\n for validator in validators:\n validator(obj or self.get_object())", "response": "Execute validation for actions that are related to particular object"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef row_dict(self, row):\n d = {}\n for field_name,index in self.field_map.items():\n d[field_name] = self.field_value(row, field_name)\n return d", "response": "returns dictionary version of row using keys from self. field_map"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning detected dialect of filepath and sets self. has_header if not passed in __init__ kwargs Arguments : filepath (str ) - path to target csv file", "response": "def _dialect(self, filepath):\n \"\"\"returns detected dialect of filepath and sets self.has_header\n if not passed in __init__ kwargs\n Arguments:\n filepath (str): filepath of target csv file\n \"\"\"\n with open(filepath, self.read_mode) as csvfile:\n sample = csvfile.read(1024)\n dialect = csv.Sniffer().sniff(sample)\n if self.has_header == None:\n # detect header if header not specified\n self.has_header = csv.Sniffer().has_header(sample)\n csvfile.seek(0)\n return dialect"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_content_type_queryset(models_list):\n content_type_ids = {c.id for c in ContentType.objects.get_for_models(*models_list).values()}\n return ContentType.objects.filter(id__in=content_type_ids)", "response": "Get list of services content types"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef init_registered(self, request):\n created_items = models.DefaultPriceListItem.init_from_registered_resources()\n\n if created_items:\n message = ungettext(\n _('Price item was created: %s.') % created_items[0].name,\n _('Price items were created: %s.') % ', '.join(item.name for item in created_items),\n len(created_items)\n )\n self.message_user(request, message)\n else:\n self.message_user(request, _('Price items for all registered resources have been updated.'))\n\n return redirect(reverse('admin:cost_tracking_defaultpricelistitem_changelist'))", "response": "Create default price list items for each registered resource."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nencrypt the given message", "response": "def encrypt(self, message):\n \"\"\" Encrypt the given message \"\"\"\n\n if not isinstance(message, (bytes, str)):\n raise TypeError\n \n return hashlib.sha1(message.encode('utf-8')).hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef save(self, *args, **kwargs):\n try:\n prev_instance = self.__class__.objects.get(pk=self.pk)\n except self.DoesNotExist:\n prev_instance = None\n\n if prev_instance is None or (not prev_instance.is_active and self.is_active or\n self.schedule != prev_instance.schedule):\n self.update_next_trigger_at()\n\n super(ScheduleMixin, self).save(*args, **kwargs)", "response": "Updates next_trigger_at field of the record if the record is not already active and the record is new."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_version_fields(self):\n options = reversion._get_options(self)\n return options.fields or [f.name for f in self._meta.fields if f not in options.exclude]", "response": "Get the fields that are tracked in object history versions."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _is_version_duplicate(self):\n if self.id is None:\n return False\n try:\n latest_version = Version.objects.get_for_object(self).latest('revision__date_created')\n except Version.DoesNotExist:\n return False\n latest_version_object = latest_version._object_version.object\n fields = self.get_version_fields()\n return all([getattr(self, f) == getattr(latest_version_object, f) for f in fields])", "response": "Check if this object is a duplicate of another object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget all unique instance ancestors", "response": "def get_ancestors(self):\n \"\"\" Get all unique instance ancestors \"\"\"\n ancestors = list(self.get_parents())\n ancestor_unique_attributes = set([(a.__class__, a.id) for a in ancestors])\n ancestors_with_parents = [a for a in ancestors if isinstance(a, DescendantMixin)]\n for ancestor in ancestors_with_parents:\n for parent in ancestor.get_ancestors():\n if (parent.__class__, parent.id) not in ancestor_unique_attributes:\n ancestors.append(parent)\n return ancestors"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking if the connection has succeed.", "response": "def has_succeed(self):\n \"\"\" Check if the connection has succeed\n\n Returns:\n Returns True if connection has succeed.\n False otherwise.\n\n \"\"\"\n status_code = self._response.status_code\n\n if status_code in [HTTP_CODE_ZERO, HTTP_CODE_SUCCESS, HTTP_CODE_CREATED, HTTP_CODE_EMPTY, HTTP_CODE_MULTIPLE_CHOICES]:\n return True\n\n if status_code in [HTTP_CODE_BAD_REQUEST, HTTP_CODE_UNAUTHORIZED, HTTP_CODE_PERMISSION_DENIED, HTTP_CODE_NOT_FOUND, HTTP_CODE_METHOD_NOT_ALLOWED, HTTP_CODE_CONNECTION_TIMEOUT, HTTP_CODE_CONFLICT, HTTP_CODE_PRECONDITION_FAILED, HTTP_CODE_INTERNAL_SERVER_ERROR, HTTP_CODE_SERVICE_UNAVAILABLE]:\n return False\n\n raise Exception('Unknown status code %s.', status_code)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef handle_response_for_connection(self, should_post=False):\n\n status_code = self._response.status_code\n data = self._response.data\n\n # TODO : Get errors in response data after bug fix : http://mvjira.mv.usa.alcatel.com/browse/VSD-2735\n if data and 'errors' in data:\n self._response.errors = data['errors']\n\n if status_code in [HTTP_CODE_SUCCESS, HTTP_CODE_CREATED, HTTP_CODE_EMPTY]:\n return True\n\n if status_code == HTTP_CODE_MULTIPLE_CHOICES:\n return False\n\n if status_code in [HTTP_CODE_PERMISSION_DENIED, HTTP_CODE_UNAUTHORIZED]:\n\n if not should_post:\n return True\n\n return False\n\n if status_code in [HTTP_CODE_CONFLICT, HTTP_CODE_NOT_FOUND, HTTP_CODE_BAD_REQUEST, HTTP_CODE_METHOD_NOT_ALLOWED, HTTP_CODE_PRECONDITION_FAILED, HTTP_CODE_SERVICE_UNAVAILABLE]:\n if not should_post:\n return True\n\n return False\n\n if status_code == HTTP_CODE_INTERNAL_SERVER_ERROR:\n\n return False\n\n if status_code == HTTP_CODE_ZERO:\n bambou_logger.error(\"NURESTConnection: Connection error with code 0. Sending NUNURESTConnectionFailureNotification notification and exiting.\")\n return False\n\n bambou_logger.error(\"NURESTConnection: Report this error, because this should not happen: %s\" % self._response)\n return False", "response": "Check if the response has a success or not and set the response attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncalls when a response is received", "response": "def _did_receive_response(self, response):\n \"\"\" Called when a response is received \"\"\"\n try:\n data = response.json()\n except:\n data = None\n\n self._response = NURESTResponse(status_code=response.status_code, headers=response.headers, data=data, reason=response.reason)\n\n level = logging.WARNING if self._response.status_code >= 300 else logging.DEBUG\n\n bambou_logger.info('< %s %s %s [%s] ' % (self._request.method, self._request.url, self._request.params if self._request.params else \"\", self._response.status_code))\n bambou_logger.log(level, '< headers: %s' % self._response.headers)\n bambou_logger.log(level, '< data:\\n%s' % json.dumps(self._response.data, indent=4))\n\n self._callback(self)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _did_timeout(self):\n\n bambou_logger.debug('Bambou %s on %s has timeout (timeout=%ss)..' % (self._request.method, self._request.url, self.timeout))\n self._has_timeouted = True\n\n if self.async:\n self._callback(self)\n else:\n return self", "response": "Called when a request has timeout."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmakes a synchronous request.", "response": "def _make_request(self, session=None):\n \"\"\" Make a synchronous request \"\"\"\n\n if session is None:\n session = NURESTSession.get_current_session()\n\n self._has_timeouted = False\n\n # Add specific headers\n controller = session.login_controller\n\n enterprise = controller.enterprise\n user_name = controller.user\n api_key = controller.api_key\n certificate = controller.certificate\n\n if self._root_object:\n enterprise = self._root_object.enterprise_name\n user_name = self._root_object.user_name\n api_key = self._root_object.api_key\n\n if self._uses_authentication:\n self._request.set_header('X-Nuage-Organization', enterprise)\n self._request.set_header('Authorization', controller.get_authentication_header(user_name, api_key))\n\n if controller.is_impersonating:\n self._request.set_header('X-Nuage-ProxyUser', controller.impersonation)\n\n headers = self._request.headers\n data = json.dumps(self._request.data)\n\n bambou_logger.info('> %s %s %s' % (self._request.method, self._request.url, self._request.params if self._request.params else \"\"))\n bambou_logger.debug('> headers: %s' % headers)\n bambou_logger.debug('> data:\\n %s' % json.dumps(self._request.data, indent=4))\n\n response = self.__make_request(requests_session=session.requests_session, method=self._request.method, url=self._request.url, params=self._request.params, data=data, headers=headers, certificate=certificate)\n\n retry_request = False\n\n if response.status_code == HTTP_CODE_MULTIPLE_CHOICES:\n self._request.url += '?responseChoice=1'\n bambou_logger.debug('Bambou got [%s] response. Trying to force response choice' % HTTP_CODE_MULTIPLE_CHOICES)\n retry_request = True\n\n elif response.status_code == HTTP_CODE_AUTHENTICATION_EXPIRED and session:\n bambou_logger.debug('Bambou got [%s] response . Trying to reconnect your session that has expired' % HTTP_CODE_AUTHENTICATION_EXPIRED)\n session.reset()\n session.start()\n retry_request = True\n\n if retry_request:\n response = self.__make_request(requests_session=session.requests_session, method=self._request.method, url=self._request.url, params=self._request.params, data=data, headers=headers, certificate=certificate)\n\n return self._did_receive_response(response)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef start(self):\n\n # TODO : Use Timeout here and _ignore_request_idle\n from .nurest_session import NURESTSession\n session = NURESTSession.get_current_session()\n\n if self.async:\n thread = threading.Thread(target=self._make_request, kwargs={'session': session})\n thread.is_daemon = False\n thread.start()\n return self.transaction_id\n\n return self._make_request(session=session)", "response": "Start the HTTP request with a specific method"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nresetting the connection to the original state.", "response": "def reset(self):\n \"\"\" Reset the connection\n\n \"\"\"\n self._request = None\n self._response = None\n self._transaction_id = uuid.uuid4().hex"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a new ConsumptionDetailsQuerySet for the given price estimate.", "response": "def create(self, price_estimate):\n \"\"\" Take configuration from previous month, it it exists.\n Set last_update_time equals to the beginning of the month.\n \"\"\"\n kwargs = {}\n try:\n previous_price_estimate = price_estimate.get_previous()\n except ObjectDoesNotExist:\n pass\n else:\n configuration = previous_price_estimate.consumption_details.configuration\n kwargs['configuration'] = configuration\n month_start = core_utils.month_start(datetime.date(price_estimate.year, price_estimate.month, 1))\n kwargs['last_update_time'] = month_start\n return super(ConsumptionDetailsQuerySet, self).create(price_estimate=price_estimate, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_fields(self):\n fields = super(BaseHookSerializer, self).get_fields()\n fields['event_types'] = serializers.MultipleChoiceField(\n choices=loggers.get_valid_events(), required=False)\n fields['event_groups'] = serializers.MultipleChoiceField(\n choices=loggers.get_event_groups_keys(), required=False)\n return fields", "response": "Returns a dict of all the fields that are required to serialize the log entries."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nusing PriceEstimateSerializer to serialize estimate children", "response": "def build_nested_field(self, field_name, relation_info, nested_depth):\n \"\"\" Use PriceEstimateSerializer to serialize estimate children \"\"\"\n if field_name != 'children':\n return super(PriceEstimateSerializer, self).build_nested_field(field_name, relation_info, nested_depth)\n field_class = self.__class__\n field_kwargs = {'read_only': True, 'many': True, 'context': {'depth': nested_depth - 1}}\n return field_class, field_kwargs"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\npreparing the exception for re - raising with reraise method.", "response": "def prepare_for_reraise(error, exc_info=None):\n \"\"\"Prepares the exception for re-raising with reraise method.\n\n This method attaches type and traceback info to the error object\n so that reraise can properly reraise it using this info.\n\n \"\"\"\n if not hasattr(error, \"_type_\"):\n if exc_info is None:\n exc_info = sys.exc_info()\n error._type_ = exc_info[0]\n error._traceback = exc_info[2]\n return error"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef reraise(error):\n if hasattr(error, \"_type_\"):\n six.reraise(type(error), error, error._traceback)\n raise error", "response": "Re - raises the error that was processed by prepare_for_reraise earlier."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef register(*args, **kwargs):\n if args and callable(args[0]):\n raise ConfigurationError(\n 'You might have tried to decorate a function directly with '\n '@register which is not supported, use @register(...) instead')\n def registrator(origin):\n try:\n server = SERVERS[kwargs.pop('to')]\n except KeyError:\n server = SERVERS.default\n server.add_function(\n NativeFunctionAdapter(origin, *args, **kwargs))\n return origin\n return registrator", "response": "Returns a registrator that is being called with a function as the only\n argument and registers it to the server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef rest_name(cls):\n if cls.__name__ == \"NURESTRootObject\" or cls.__name__ == \"NURESTObject\":\n return \"Not Implemented\"\n\n if cls.__rest_name__ is None:\n raise NotImplementedError('%s has no defined name. Implement rest_name property first.' % cls)\n\n return cls.__rest_name__", "response": "Represents a singular REST name of the class."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrepresent the resource name of the class.", "response": "def resource_name(cls):\n \"\"\" Represents the resource name\n \"\"\"\n if cls.__name__ == \"NURESTRootObject\" or cls.__name__ == \"NURESTObject\":\n return \"Not Implemented\"\n\n if cls.__resource_name__ is None:\n raise NotImplementedError('%s has no defined resource name. Implement resource_name property first.' % cls)\n\n return cls.__resource_name__"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _compute_args(self, data=dict(), **kwargs):\n\n for name, remote_attribute in self._attributes.items():\n default_value = BambouConfig.get_default_attribute_value(self.__class__, name, remote_attribute.attribute_type)\n setattr(self, name, default_value)\n\n if len(data) > 0:\n self.from_dict(data)\n\n for key, value in kwargs.items():\n if hasattr(self, key):\n setattr(self, key, value)", "response": "Compute the arguments for the object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef children_rest_names(self):\n\n names = []\n\n for fetcher in self.fetchers:\n names.append(fetcher.__class__.managed_object_rest_name())\n\n return names", "response": "Gets the list of all possible children ReST names."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_resource_url(self):\n\n name = self.__class__.resource_name\n url = self.__class__.rest_base_url()\n\n if self.id is not None:\n return \"%s/%s/%s\" % (url, name, self.id)\n\n return \"%s/%s\" % (url, name)", "response": "Get resource complete url"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nvalidate the current object attributes and store errors in errors dict.", "response": "def validate(self):\n \"\"\" Validate the current object attributes.\n\n Check all attributes and store errors\n\n Returns:\n Returns True if all attibutes of the object\n respect contraints. Returns False otherwise and\n store error in errors dict.\n\n \"\"\"\n self._attribute_errors = dict() # Reset validation errors\n\n for local_name, attribute in self._attributes.items():\n\n value = getattr(self, local_name, None)\n\n if attribute.is_required and (value is None or value == \"\"):\n self._attribute_errors[local_name] = {'title': 'Invalid input',\n 'description': 'This value is mandatory.',\n 'remote_name': attribute.remote_name}\n continue\n\n if value is None:\n continue # without error\n\n if not self._validate_type(local_name, attribute.remote_name, value, attribute.attribute_type):\n continue\n\n if attribute.min_length is not None and len(value) < attribute.min_length:\n self._attribute_errors[local_name] = {'title': 'Invalid length',\n 'description': 'Attribute %s minimum length should be %s but is %s' % (attribute.remote_name, attribute.min_length, len(value)),\n 'remote_name': attribute.remote_name}\n continue\n\n if attribute.max_length is not None and len(value) > attribute.max_length:\n self._attribute_errors[local_name] = {'title': 'Invalid length',\n 'description': 'Attribute %s maximum length should be %s but is %s' % (attribute.remote_name, attribute.max_length, len(value)),\n 'remote_name': attribute.remote_name}\n continue\n\n if attribute.attribute_type == list:\n valid = True\n for item in value:\n if valid is True:\n valid = self._validate_value(local_name, attribute, item)\n else:\n self._validate_value(local_name, attribute, value)\n\n return self.is_valid()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexposes a local_name as remote_name_nuremote_attribute", "response": "def expose_attribute(self, local_name, attribute_type, remote_name=None, display_name=None, is_required=False, is_readonly=False, max_length=None, min_length=None, is_identifier=False, choices=None, is_unique=False, is_email=False, is_login=False, is_editable=True, is_password=False, can_order=False, can_search=False, subtype=None, min_value=None, max_value=None):\n \"\"\" Expose local_name as remote_name\n\n An exposed attribute `local_name` will be sent within the HTTP request as\n a `remote_name`\n\n \"\"\"\n if remote_name is None:\n remote_name = local_name\n\n if display_name is None:\n display_name = local_name\n\n attribute = NURemoteAttribute(local_name=local_name, remote_name=remote_name, attribute_type=attribute_type)\n attribute.display_name = display_name\n attribute.is_required = is_required\n attribute.is_readonly = is_readonly\n attribute.min_length = min_length\n attribute.max_length = max_length\n attribute.is_editable = is_editable\n attribute.is_identifier = is_identifier\n attribute.choices = choices\n attribute.is_unique = is_unique\n attribute.is_email = is_email\n attribute.is_login = is_login\n attribute.is_password = is_password\n attribute.can_order = can_order\n attribute.can_search = can_search\n attribute.subtype = subtype\n attribute.min_value = min_value\n attribute.max_value = max_value\n\n self._attributes[local_name] = attribute"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck if the current user owns the object", "response": "def is_owned_by_current_user(self):\n \"\"\" Check if the current user owns the object \"\"\"\n\n from bambou.nurest_root_object import NURESTRootObject\n root_object = NURESTRootObject.get_default_root_object()\n return self._owner == root_object.id"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parent_for_matching_rest_name(self, rest_names):\n\n parent = self\n\n while parent:\n if parent.rest_name in rest_names:\n return parent\n\n parent = parent.parent_object\n\n return None", "response": "Return parent that matches a rest name"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef genealogic_types(self):\n\n types = []\n parent = self\n\n while parent:\n types.append(parent.rest_name)\n parent = parent.parent_object\n\n return types", "response": "Get genealogic types Returns a list of all parent types"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all genealogic ids in the current object", "response": "def genealogic_ids(self):\n \"\"\" Get all genealogic ids\n\n Returns:\n A list of all parent ids\n \"\"\"\n\n ids = []\n parent = self\n\n while parent:\n ids.append(parent.id)\n parent = parent.parent_object\n\n return ids"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the creation date with a given format. Default is %b %d %H:%I:%S", "response": "def get_formated_creation_date(self, format='%b %Y %d %H:%I:%S'):\n \"\"\" Return creation date with a given format. Default is '%b %Y %d %H:%I:%S' \"\"\"\n\n if not self._creation_date:\n return None\n\n date = datetime.datetime.utcfromtimestamp(self._creation_date)\n return date.strftime(format)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_child(self, child):\n\n rest_name = child.rest_name\n children = self.fetcher_for_rest_name(rest_name)\n\n if children is None:\n raise InternalConsitencyError('Could not find fetcher with name %s while adding %s in parent %s' % (rest_name, child, self))\n\n if child not in children:\n child.parent_object = self\n children.append(child)", "response": "Add a child to the parent object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove_child(self, child):\n\n rest_name = child.rest_name\n children = self.fetcher_for_rest_name(rest_name)\n\n target_child = None\n\n for local_child in children:\n if local_child.id == child.id:\n target_child = local_child\n break\n\n if target_child:\n target_child.parent_object = None\n children.remove(target_child)", "response": "Removes a child from the list of children."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the child with the given one.", "response": "def update_child(self, child):\n \"\"\" Update child \"\"\"\n\n rest_name = child.rest_name\n children = self.fetcher_for_rest_name(rest_name)\n\n index = None\n\n for local_child in children:\n if local_child.id == child.id:\n index = children.index(local_child)\n break\n\n if index is not None:\n children[index] = child"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef to_dict(self):\n\n dictionary = dict()\n\n for local_name, attribute in self._attributes.items():\n remote_name = attribute.remote_name\n\n if hasattr(self, local_name):\n value = getattr(self, local_name)\n\n # Removed to resolve issue http://mvjira.mv.usa.alcatel.com/browse/VSD-5940 (12/15/2014)\n # if isinstance(value, bool):\n # value = int(value)\n\n if isinstance(value, NURESTObject):\n value = value.to_dict()\n\n if isinstance(value, list) and len(value) > 0 and isinstance(value[0], NURESTObject):\n tmp = list()\n for obj in value:\n tmp.append(obj.to_dict())\n\n value = tmp\n\n dictionary[remote_name] = value\n else:\n pass # pragma: no cover\n\n return dictionary", "response": "Converts the current object into a dictionary using all exposed ReST attributes and their values."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef from_dict(self, dictionary):\n\n for remote_name, remote_value in dictionary.items():\n # Check if a local attribute is exposed with the remote_name\n # if no attribute is exposed, return None\n local_name = next((name for name, attribute in self._attributes.items() if attribute.remote_name == remote_name), None)\n\n if local_name:\n setattr(self, local_name, remote_value)\n else:\n # print('Attribute %s could not be added to object %s' % (remote_name, self))\n pass", "response": "Sets all the exposed ReST attributes from the given dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes object and call given callback in case of call.", "response": "def delete(self, response_choice=1, async=False, callback=None):\n \"\"\" Delete object and call given callback in case of call.\n\n Args:\n response_choice (int): Automatically send a response choice when confirmation is needed\n async (bool): Boolean to make an asynchronous call. Default is False\n callback (function): Callback method that will be triggered in case of asynchronous call\n\n Example:\n >>> entity.delete() # will delete the enterprise from the server\n \"\"\"\n return self._manage_child_object(nurest_object=self, method=HTTP_METHOD_DELETE, async=async, callback=callback, response_choice=response_choice)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nupdate object and call given callback in case of asynchronous call", "response": "def save(self, response_choice=None, async=False, callback=None):\n \"\"\" Update object and call given callback in case of async call\n\n Args:\n async (bool): Boolean to make an asynchronous call. Default is False\n callback (function): Callback method that will be triggered in case of asynchronous call\n\n Example:\n >>> entity.name = \"My Super Object\"\n >>> entity.save() # will save the new name in the server\n \"\"\"\n return self._manage_child_object(nurest_object=self, method=HTTP_METHOD_PUT, async=async, callback=callback, response_choice=response_choice)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends a request to the NUREST server.", "response": "def send_request(self, request, async=False, local_callback=None, remote_callback=None, user_info=None):\n \"\"\" Sends a request, calls the local callback, then the remote callback in case of async call\n\n Args:\n request: The request to send\n local_callback: local method that will be triggered in case of async call\n remote_callback: remote moethd that will be triggered in case of async call\n user_info: contains additionnal information to carry during the request\n\n Returns:\n Returns the object and connection (object, connection)\n \"\"\"\n\n callbacks = dict()\n\n if local_callback:\n callbacks['local'] = local_callback\n\n if remote_callback:\n callbacks['remote'] = remote_callback\n\n connection = NURESTConnection(request=request, async=async, callback=self._did_receive_response, callbacks=callbacks)\n connection.user_info = user_info\n\n return connection.start()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _manage_child_object(self, nurest_object, method=HTTP_METHOD_GET, async=False, callback=None, handler=None, response_choice=None, commit=False):\n url = None\n\n if method == HTTP_METHOD_POST:\n url = self.get_resource_url_for_child_type(nurest_object.__class__)\n else:\n url = self.get_resource_url()\n\n if response_choice is not None:\n url += '?responseChoice=%s' % response_choice\n\n request = NURESTRequest(method=method, url=url, data=nurest_object.to_dict())\n user_info = {'nurest_object': nurest_object, 'commit': commit}\n\n if not handler:\n handler = self._did_perform_standard_operation\n\n if async:\n\n return self.send_request(request=request, async=async, local_callback=handler, remote_callback=callback, user_info=user_info)\n else:\n connection = self.send_request(request=request, user_info=user_info)\n return handler(connection)", "response": "Low level child management. Sends given HTTP method with given nurest_object to given ressource of current object and returns the object and connection."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalling when a response is received from the connection.", "response": "def _did_receive_response(self, connection):\n \"\"\" Receive a response from the connection \"\"\"\n\n if connection.has_timeouted:\n bambou_logger.info(\"NURESTConnection has timeout.\")\n return\n\n has_callbacks = connection.has_callbacks()\n should_post = not has_callbacks\n\n if connection.handle_response_for_connection(should_post=should_post) and has_callbacks:\n callback = connection.callbacks['local']\n callback(connection)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _did_retrieve(self, connection):\n\n response = connection.response\n\n try:\n self.from_dict(response.data[0])\n except:\n pass\n\n return self._did_perform_standard_operation(connection)", "response": "Callback called after fetching the object"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd given nurest_object to the current object and return the object and connection.", "response": "def create_child(self, nurest_object, response_choice=None, async=False, callback=None, commit=True):\n \"\"\" Add given nurest_object to the current object\n\n For example, to add a child into a parent, you can call\n parent.create_child(nurest_object=child)\n\n Args:\n nurest_object (bambou.NURESTObject): the NURESTObject object to add\n response_choice (int): Automatically send a response choice when confirmation is needed\n async (bool): should the request be done asynchronously or not\n callback (function): callback containing the object and the connection\n\n Returns:\n Returns the object and connection (object, connection)\n\n Example:\n >>> entity = NUEntity(name=\"Super Entity\")\n >>> parent_entity.create_child(entity) # the new entity as been created in the parent_entity\n \"\"\"\n\n # if nurest_object.id:\n # raise InternalConsitencyError(\"Cannot create a child that already has an ID: %s.\" % nurest_object)\n\n return self._manage_child_object(nurest_object=nurest_object,\n async=async,\n method=HTTP_METHOD_POST,\n callback=callback,\n handler=self._did_create_child,\n response_choice=response_choice,\n commit=commit)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _did_create_child(self, connection):\n\n response = connection.response\n try:\n connection.user_info['nurest_object'].from_dict(response.data[0])\n except Exception:\n pass\n\n return self._did_perform_standard_operation(connection)", "response": "Callback called after adding a new child nurest_object"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nassigning a list of objects into the current resource.", "response": "def assign(self, objects, nurest_object_type, async=False, callback=None, commit=True):\n \"\"\" Reference a list of objects into the current resource\n\n Args:\n objects (list): list of NURESTObject to link\n nurest_object_type (type): Type of the object to link\n callback (function): Callback method that should be fired at the end\n\n Returns:\n Returns the current object and the connection (object, connection)\n\n Example:\n >>> entity.assign([entity1, entity2, entity3], NUEntity) # entity1, entity2 and entity3 are now part of the entity\n \"\"\"\n\n ids = list()\n\n for nurest_object in objects:\n ids.append(nurest_object.id)\n\n url = self.get_resource_url_for_child_type(nurest_object_type)\n request = NURESTRequest(method=HTTP_METHOD_PUT, url=url, data=ids)\n user_info = {'nurest_objects': objects, 'commit': commit}\n\n if async:\n return self.send_request(request=request,\n async=async,\n local_callback=self._did_perform_standard_operation,\n remote_callback=callback,\n user_info=user_info)\n else:\n connection = self.send_request(request=request,\n user_info=user_info)\n\n return self._did_perform_standard_operation(connection)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncomparing objects REST attributes", "response": "def rest_equals(self, rest_object):\n \"\"\" Compare objects REST attributes\n\n \"\"\"\n if not self.equals(rest_object):\n return False\n\n return self.to_dict() == rest_object.to_dict()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncompares with another object.", "response": "def equals(self, rest_object):\n \"\"\" Compare with another object \"\"\"\n\n if self._is_dirty:\n return False\n\n if rest_object is None:\n return False\n\n if not isinstance(rest_object, NURESTObject):\n raise TypeError('The object is not a NURESTObject %s' % rest_object)\n\n if self.rest_name != rest_object.rest_name:\n return False\n\n if self.id and rest_object.id:\n return self.id == rest_object.id\n\n if self.local_id and rest_object.local_id:\n return self.local_id == rest_object.local_id\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ip_address(request):\n if 'HTTP_X_FORWARDED_FOR' in request.META:\n return request.META['HTTP_X_FORWARDED_FOR'].split(',')[0].strip()\n else:\n return request.META['REMOTE_ADDR']", "response": "Returns the first IP address from the request. META."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_estimates_without_scope_in_month(self, customer):\n estimates = self.get_price_estimates_for_customer(customer)\n if not estimates:\n return []\n\n tables = {model: collections.defaultdict(list)\n for model in self.get_estimated_models()}\n\n dates = set()\n for estimate in estimates:\n date = (estimate.year, estimate.month)\n dates.add(date)\n\n cls = estimate.content_type.model_class()\n for model, table in tables.items():\n if issubclass(cls, model):\n table[date].append(estimate)\n break\n\n invalid_estimates = []\n for date in dates:\n if any(map(lambda table: len(table[date]) == 0, tables.values())):\n for table in tables.values():\n invalid_estimates.extend(table[date])\n print(invalid_estimates)\n return invalid_estimates", "response": "Returns price estimates that are not scopeable in the given customer."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a default value of the attribute_type", "response": "def get_default_value(self):\n \"\"\" Get a default value of the attribute_type \"\"\"\n\n if self.choices:\n return self.choices[0]\n\n value = self.attribute_type()\n\n if self.attribute_type is time:\n value = int(value)\n\n elif self.attribute_type is str:\n value = \"A\"\n\n if self.min_length:\n if self.attribute_type is str:\n value = value.ljust(self.min_length, 'a')\n elif self.attribute_type is int:\n value = self.min_length\n\n elif self.max_length:\n if self.attribute_type is str:\n value = value.ljust(self.max_length, 'a')\n elif self.attribute_type is int:\n value = self.max_length\n\n return value"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the minimum value of the attribute", "response": "def get_min_value(self):\n \"\"\" Get the minimum value \"\"\"\n\n value = self.get_default_value()\n\n if self.attribute_type is str:\n min_value = value[:self.min_length - 1]\n\n elif self.attribute_type is int:\n min_value = self.min_length - 1\n\n else:\n raise TypeError('Attribute %s can not have a minimum value' % self.local_name)\n\n return min_value"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_max_value(self):\n\n value = self.get_default_value()\n\n if self.attribute_type is str:\n max_value = value.ljust(self.max_length + 1, 'a')\n\n elif self.attribute_type is int:\n max_value = self.max_length + 1\n\n else:\n raise TypeError('Attribute %s can not have a maximum value' % self.local_name)\n\n return max_value", "response": "Get the maximum value of the attribute."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a basic root view.", "response": "def get_api_root_view(self, api_urls=None):\n \"\"\"\n Return a basic root view.\n \"\"\"\n api_root_dict = OrderedDict()\n list_name = self.routes[0].name\n for prefix, viewset, basename in self.registry:\n api_root_dict[prefix] = list_name.format(basename=basename)\n\n class APIRootView(views.APIView):\n _ignore_model_permissions = True\n exclude_from_schema = True\n\n def get(self, request, *args, **kwargs):\n # Return a plain {\"name\": \"hyperlink\"} response.\n ret = OrderedDict()\n namespace = request.resolver_match.namespace\n for key, url_name in sorted(api_root_dict.items(), key=itemgetter(0)):\n if namespace:\n url_name = namespace + ':' + url_name\n try:\n ret[key] = reverse(\n url_name,\n args=args,\n kwargs=kwargs,\n request=request,\n format=kwargs.get('format', None)\n )\n except NoReverseMatch:\n # Don't bail out if eg. no list routes exist, only detail routes.\n continue\n\n return Response(ret)\n\n return APIRootView.as_view()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_default_base_name(self, viewset):\n queryset = getattr(viewset, 'queryset', None)\n\n if queryset is not None:\n get_url_name = getattr(queryset.model, 'get_url_name', None)\n if get_url_name is not None:\n return get_url_name()\n\n return super(SortedDefaultRouter, self).get_default_base_name(viewset)", "response": "Attempt to automatically determine base name using get_url_name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef change_customer_nc_users_quota(sender, structure, user, role, signal, **kwargs):\n assert signal in (signals.structure_role_granted, signals.structure_role_revoked), \\\n 'Handler \"change_customer_nc_users_quota\" has to be used only with structure_role signals'\n assert sender in (Customer, Project), \\\n 'Handler \"change_customer_nc_users_quota\" works only with Project and Customer models'\n\n if sender == Customer:\n customer = structure\n elif sender == Project:\n customer = structure.customer\n\n customer_users = customer.get_users()\n customer.set_quota_usage(Customer.Quotas.nc_user_count, customer_users.count())", "response": "Modify nc_user_count quota usage on structure role grant or revoke"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete not shared service settings without services", "response": "def delete_service_settings_on_service_delete(sender, instance, **kwargs):\n \"\"\" Delete not shared service settings without services \"\"\"\n service = instance\n try:\n service_settings = service.settings\n except ServiceSettings.DoesNotExist:\n # If this handler works together with delete_service_settings_on_scope_delete\n # it tries to delete service settings that are already deleted.\n return\n if not service_settings.shared:\n service.settings.delete()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete all service settings that are located in the VM that are located in the given scope.", "response": "def delete_service_settings_on_scope_delete(sender, instance, **kwargs):\n \"\"\" If VM that contains service settings were deleted - all settings\n resources could be safely deleted from NC.\n \"\"\"\n for service_settings in ServiceSettings.objects.filter(scope=instance):\n service_settings.unlink_descendants()\n service_settings.delete()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets the URL of the API endpoint that contains the data.", "response": "def url(self, url):\n \"\"\" Set API URL endpoint\n\n Args:\n url: the url of the API endpoint\n \"\"\"\n if url and url.endswith('/'):\n url = url[:-1]\n\n self._url = url"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the authentication string to place in Authorization Header", "response": "def get_authentication_header(self, user=None, api_key=None, password=None, certificate=None):\n \"\"\" Return authenication string to place in Authorization Header\n\n If API Token is set, it'll be used. Otherwise, the clear\n text password will be sent. Users of NURESTLoginController are responsible to\n clean the password property.\n\n Returns:\n Returns the XREST Authentication string with API Key or user password encoded.\n \"\"\"\n\n if not user:\n user = self.user\n\n if not api_key:\n api_key = self.api_key\n\n if not password:\n password = self.password\n\n if not password:\n password = self.password\n\n if not certificate:\n certificate = self._certificate\n\n if certificate:\n return \"XREST %s\" % urlsafe_b64encode(\"{}:\".format(user).encode('utf-8')).decode('utf-8')\n\n if api_key:\n return \"XREST %s\" % urlsafe_b64encode(\"{}:{}\".format(user, api_key).encode('utf-8')).decode('utf-8')\n\n\n return \"XREST %s\" % urlsafe_b64encode(\"{}:{}\".format(user, password).encode('utf-8')).decode('utf-8')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reset(self):\n\n self._is_impersonating = False\n self._impersonation = None\n\n self.user = None\n self.password = None\n self.api_key = None\n self.enterprise = None\n self.url = None", "response": "Reset the controller to its initial state."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef impersonate(self, user, enterprise):\n\n if not user or not enterprise:\n raise ValueError('You must set a user name and an enterprise name to begin impersonification')\n\n self._is_impersonating = True\n self._impersonation = \"%s@%s\" % (user, enterprise)", "response": "Impersonate a user in an enterprise"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nverifying if the current controller corresponds to the current one.", "response": "def equals(self, controller):\n \"\"\" Verify if the controller corresponds\n to the current one.\n\n \"\"\"\n if controller is None:\n return False\n\n return self.user == controller.user and self.enterprise == controller.enterprise and self.url == controller.url"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a WSGI application that can be used with a single WSGI server.", "response": "def create_application(server, root_url):\n \"\"\"\n WSGI adapter. It creates a simple WSGI application, that can be used with\n any WSGI server. The arguments are:\n\n #. ``server`` is a pyws server object,\n #. ``root_url`` is an URL to which the server will be bound.\n\n An application created by the function transforms WSGI environment into a\n pyws request object. Then it feeds the request to the server, gets the\n response, sets header ``Content-Type`` and returns response text.\n \"\"\"\n\n def serve(environ, start_response):\n\n root = root_url.lstrip('/')\n\n tail, get = (util.request_uri(environ).split('?') + [''])[:2]\n tail = tail[len(util.application_uri(environ)):]\n tail = tail.lstrip('/')\n\n result = []\n\n content_type = 'text/plain'\n status = '200 OK'\n if tail.startswith(root):\n\n tail = tail[len(root):]\n\n get = parse_qs(get)\n method = environ['REQUEST_METHOD']\n\n text, post = '', {}\n if method == 'POST':\n text = environ['wsgi.input'].\\\n read(int(environ.get('CONTENT_LENGTH', 0)))\n post = parse_qs(text)\n\n response = server.process_request(\n Request(tail, text, get, post, {}))\n\n content_type = response.content_type\n status = get_http_response_code(response)\n result.append(response.text)\n\n headers = [('Content-type', content_type)]\n start_response(status, headers)\n\n return result\n\n return serve"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_engine(\n database,\n minsize=1,\n maxsize=10,\n loop=None,\n dialect=_dialect,\n paramstyle=None,\n **kwargs):\n \"\"\"\n A coroutine for Engine creation.\n\n Returns Engine instance with embedded connection pool.\n\n The pool has *minsize* opened connections to sqlite3.\n \"\"\"\n coro = _create_engine(\n database=database,\n minsize=minsize,\n maxsize=maxsize,\n loop=loop,\n dialect=dialect,\n paramstyle=paramstyle,\n **kwargs\n )\n return _EngineContextManager(coro)", "response": "Create an engine and return a context manager."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef release(self, conn):\n if conn.in_transaction:\n raise InvalidRequestError(\n \"Cannot release a connection with \"\n \"not finished transaction\"\n )\n raw = conn.connection\n res = yield from self._pool.release(raw)\n return res", "response": "Revert back a connection to the pool."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn how much consumables are used by resource with current configuration.", "response": "def get_configuration(cls, resource):\n \"\"\" Return how much consumables are used by resource with current configuration.\n\n Output example:\n {\n : ,\n : ,\n ...\n }\n \"\"\"\n strategy = cls._get_strategy(resource.__class__)\n return strategy.get_configuration(resource)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncloses the underlying DBAPI cursor corresponding to the execution.", "response": "def close(self):\n \"\"\"Close this ResultProxy.\n Closes the underlying DBAPI cursor corresponding to the execution.\n Note that any data cached within this ResultProxy is still available.\n For some types of results, this may include buffered rows.\n If this ResultProxy was generated from an implicit execution,\n the underlying Connection will also be closed (returns the\n underlying DBAPI connection to the connection pool.)\n This method is called automatically when:\n * all result rows are exhausted using the fetchXXX() methods.\n * cursor.description is None.\n \"\"\"\n\n if not self._closed:\n self._closed = True\n yield from self._cursor.close()\n # allow consistent errors\n self._cursor = None\n self._weak = None\n else:\n # pragma: no cover\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nobtains the version number", "response": "def get_version():\n \"\"\"Obtain the version number\"\"\"\n import imp\n import os\n mod = imp.load_source(\n 'version', os.path.join('skdata', '__init__.py')\n )\n return mod.__version__"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef accept(self, request, uuid=None):\n invitation = self.get_object()\n\n if invitation.state != models.Invitation.State.PENDING:\n raise ValidationError(_('Only pending invitation can be accepted.'))\n elif invitation.civil_number and invitation.civil_number != request.user.civil_number:\n raise ValidationError(_('User has an invalid civil number.'))\n\n if invitation.project:\n if invitation.project.has_user(request.user):\n raise ValidationError(_('User already has role within this project.'))\n elif invitation.customer.has_user(request.user):\n raise ValidationError(_('User already has role within this customer.'))\n\n if settings.WALDUR_CORE['VALIDATE_INVITATION_EMAIL'] and invitation.email != request.user.email:\n raise ValidationError(_('Invitation and user emails mismatch.'))\n\n replace_email = bool(request.data.get('replace_email'))\n invitation.accept(request.user, replace_email=replace_email)\n return Response({'detail': _('Invitation has been successfully accepted.')},\n status=status.HTTP_200_OK)", "response": "Accept an invitation for current user."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes all resources in the order they belong to.", "response": "def scope_deletion(sender, instance, **kwargs):\n \"\"\" Run different actions on price estimate scope deletion.\n\n If scope is a customer - delete all customer estimates and their children.\n If scope is a deleted resource - redefine consumption details, recalculate\n ancestors estimates and update estimate details.\n If scope is a unlinked resource - delete all resource price estimates and update ancestors.\n In all other cases - update price estimate details.\n \"\"\"\n\n is_resource = isinstance(instance, structure_models.ResourceMixin)\n if is_resource and getattr(instance, 'PERFORM_UNLINK', False):\n _resource_unlink(resource=instance)\n elif is_resource and not getattr(instance, 'PERFORM_UNLINK', False):\n _resource_deletion(resource=instance)\n elif isinstance(instance, structure_models.Customer):\n _customer_deletion(customer=instance)\n else:\n for price_estimate in models.PriceEstimate.objects.filter(scope=instance):\n price_estimate.init_details()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _resource_deletion(resource):\n if resource.__class__ not in CostTrackingRegister.registered_resources:\n return\n new_configuration = {}\n price_estimate = models.PriceEstimate.update_resource_estimate(resource, new_configuration)\n price_estimate.init_details()", "response": "Recalculate consumption details and save resource details"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef resource_update(sender, instance, created=False, **kwargs):\n resource = instance\n try:\n new_configuration = CostTrackingRegister.get_configuration(resource)\n except ResourceNotRegisteredError:\n return\n models.PriceEstimate.update_resource_estimate(\n resource, new_configuration, raise_exception=not _is_in_celery_task())\n # Try to create historical price estimates\n if created:\n _create_historical_estimates(resource, new_configuration)", "response": "Update resource consumption details and price estimate if its configuration has changed."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates resource consumption details and price estimate if its configuration has changed", "response": "def resource_quota_update(sender, instance, **kwargs):\n \"\"\" Update resource consumption details and price estimate if its configuration has changed \"\"\"\n quota = instance\n resource = quota.scope\n try:\n new_configuration = CostTrackingRegister.get_configuration(resource)\n except ResourceNotRegisteredError:\n return\n models.PriceEstimate.update_resource_estimate(\n resource, new_configuration, raise_exception=not _is_in_celery_task())"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate historical consumption details and price estimates for the given resource and configuration.", "response": "def _create_historical_estimates(resource, configuration):\n \"\"\" Create consumption details and price estimates for past months.\n\n Usually we need to update historical values on resource import.\n \"\"\"\n today = timezone.now()\n month_start = core_utils.month_start(today)\n while month_start > resource.created:\n month_start -= relativedelta(months=1)\n models.PriceEstimate.create_historical(resource, configuration, max(month_start, resource.created))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef IPYTHON_MAIN():\n import pkg_resources\n\n runner_frame = inspect.getouterframes(inspect.currentframe())[-2]\n return (\n getattr(runner_frame, \"function\", None)\n == pkg_resources.load_entry_point(\"ipython\", \"console_scripts\", \"ipython\").__name__\n )", "response": "Decide if the Ipython command line is running code."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef register_model(cls, model):\n\n rest_name = model.rest_name\n resource_name = model.resource_name\n\n if rest_name not in cls._model_rest_name_registry:\n cls._model_rest_name_registry[rest_name] = [model]\n cls._model_resource_name_registry[resource_name] = [model]\n\n elif model not in cls._model_rest_name_registry[rest_name]:\n cls._model_rest_name_registry[rest_name].append(model)\n cls._model_resource_name_registry[resource_name].append(model)", "response": "Register a model class according to its remote name\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the first model corresponding to a rest_name", "response": "def get_first_model_with_rest_name(cls, rest_name):\n \"\"\" Get the first model corresponding to a rest_name\n\n Args:\n rest_name: the rest name\n \"\"\"\n\n models = cls.get_models_with_rest_name(rest_name)\n\n if len(models) > 0:\n return models[0]\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_first_model_with_resource_name(cls, resource_name):\n\n models = cls.get_models_with_resource_name(resource_name)\n\n if len(models) > 0:\n return models[0]\n\n return None", "response": "Returns the first model with the given resource name."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run(self):\n while not self._stoped:\n self._tx_event.wait()\n self._tx_event.clear()\n try:\n func = self._tx_queue.get_nowait()\n if isinstance(func, str):\n self._stoped = True\n self._rx_queue.put('closed')\n self.notice()\n break\n except Empty:\n # pragma: no cover\n continue\n try:\n result = func()\n self._rx_queue.put(result)\n except Exception as e:\n self._rx_queue.put(e)\n self.notice()\n else:\n # pragma: no cover\n pass", "response": "run - Runs the thread - specific tasks."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef build_layout(self, dset_id: str):\n all_fields = list(self.get_data(dset_id=dset_id).keys())\n\n try:\n field_reference = self.skd[dset_id].attrs('target')\n except:\n field_reference = all_fields[0]\n\n fields_comparison = [all_fields[1]]\n\n # chart type widget\n self.register_widget(\n chart_type=widgets.RadioButtons(\n options=['individual', 'grouped'],\n value='individual',\n description='Chart Type:'\n )\n )\n\n # bins widget\n self.register_widget(\n bins=IntSlider(\n description='Bins:',\n min=2, max=10, value=2,\n continuous_update=False\n )\n )\n\n # fields comparison widget\n self.register_widget(\n xs=widgets.SelectMultiple(\n description='Xs:',\n options=[f for f in all_fields if not f == field_reference],\n value=fields_comparison\n )\n )\n\n # field reference widget\n self.register_widget(\n y=widgets.Dropdown(\n description='Y:',\n options=all_fields,\n value=field_reference\n )\n )\n # used to internal flow control\n y_changed = [False]\n\n self.register_widget(\n box_filter_panel=widgets.VBox([\n self._('y'), self._('xs'), self._('bins')\n ])\n )\n\n # layout widgets\n self.register_widget(\n table=widgets.HTML(),\n chart=widgets.HTML()\n )\n\n self.register_widget(vbox_chart=widgets.VBox([\n self._('chart_type'), self._('chart')\n ]))\n\n self.register_widget(\n tab=widgets.Tab(\n children=[\n self._('box_filter_panel'),\n self._('table'),\n self._('vbox_chart')\n ]\n )\n )\n\n self.register_widget(dashboard=widgets.HBox([self._('tab')]))\n\n # observe hooks\n def w_y_change(change: dict):\n \"\"\"\n When y field was changed xs field should be updated and data table\n and chart should be displayed/updated.\n\n :param change:\n :return:\n \"\"\"\n # remove reference field from the comparison field list\n _xs = [\n f for f in all_fields\n if not f == change['new']\n ]\n\n y_changed[0] = True # flow control variable\n _xs_value = list(self._('xs').value)\n\n if change['new'] in self._('xs').value:\n _xs_value.pop(_xs_value.index(change['new']))\n if not _xs_value:\n _xs_value = [_xs[0]]\n\n self._('xs').options = _xs\n self._('xs').value = _xs_value\n\n self._display_result(y=change['new'], dset_id=dset_id)\n\n y_changed[0] = False # flow control variable\n\n # widgets registration\n\n # change tab settings\n self._('tab').set_title(0, 'Filter')\n self._('tab').set_title(1, 'Data')\n self._('tab').set_title(2, 'Chart')\n\n # data panel\n self._('table').value = '...'\n\n # chart panel\n self._('chart').value = '...'\n\n # create observe callbacks\n self._('bins').observe(\n lambda change: (\n self._display_result(bins=change['new'], dset_id=dset_id)\n ), 'value'\n )\n self._('y').observe(w_y_change, 'value')\n # execute display result if 'y' was not changing.\n self._('xs').observe(\n lambda change: (\n self._display_result(xs=change['new'], dset_id=dset_id)\n if not y_changed[0] else None\n ), 'value'\n )\n self._('chart_type').observe(\n lambda change: (\n self._display_result(chart_type=change['new'], dset_id=dset_id)\n ), 'value'\n )", "response": "Builds the layout for the given dset_id."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef display(self, dset_id: str):\n # update result\n self.skd[dset_id].compute()\n # build layout\n self.build_layout(dset_id=dset_id)\n # display widgets\n display(self._('dashboard'))\n # display data table and chart\n self._display_result(dset_id=dset_id)", "response": "displays the data table and chart for the given dset_id"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntrying to find the spec and use the underscore starring syntax to identify potential matches.", "response": "def find_spec(self, fullname, target=None):\n \"\"\"Try to finder the spec and if it cannot be found, use the underscore starring syntax\n to identify potential matches.\n \"\"\"\n spec = super().find_spec(fullname, target=target)\n\n if spec is None:\n original = fullname\n\n if \".\" in fullname:\n original, fullname = fullname.rsplit(\".\", 1)\n else:\n original, fullname = \"\", original\n\n if \"_\" in fullname:\n files = fuzzy_file_search(self.path, fullname)\n if files:\n file = Path(sorted(files)[0])\n spec = super().find_spec(\n (original + \".\" + file.stem.split(\".\", 1)[0]).lstrip(\".\"), target=target\n )\n fullname = (original + \".\" + fullname).lstrip(\".\")\n if spec and fullname != spec.name:\n spec = FuzzySpec(\n spec.name,\n spec.loader,\n origin=spec.origin,\n loader_state=spec.loader_state,\n alias=fullname,\n is_package=bool(spec.submodule_search_locations),\n )\n return spec"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_resource_url(self):\n\n name = self.__class__.resource_name\n url = self.__class__.rest_base_url()\n\n return \"%s/%s\" % (url, name)", "response": "Get resource complete url"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsaves the user and perform the callback method", "response": "def save(self, async=False, callback=None, encrypted=True):\n \"\"\" Updates the user and perform the callback method \"\"\"\n\n if self._new_password and encrypted:\n self.password = Sha1.encrypt(self._new_password)\n\n controller = NURESTSession.get_current_session().login_controller\n controller.password = self._new_password\n controller.api_key = None\n\n data = json.dumps(self.to_dict())\n request = NURESTRequest(method=HTTP_METHOD_PUT, url=self.get_resource_url(), data=data)\n\n if async:\n return self.send_request(request=request, async=async, local_callback=self._did_save, remote_callback=callback)\n else:\n connection = self.send_request(request=request)\n return self._did_save(connection)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncall when the save has been successfully executed", "response": "def _did_save(self, connection):\n \"\"\" Launched when save has been successfully executed \"\"\"\n\n self._new_password = None\n\n controller = NURESTSession.get_current_session().login_controller\n controller.password = None\n controller.api_key = self.api_key\n\n if connection.async:\n callback = connection.callbacks['remote']\n\n if connection.user_info:\n callback(connection.user_info, connection)\n else:\n callback(self, connection)\n else:\n return (self, connection)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfetches all information about the current object in the system.", "response": "def fetch(self, async=False, callback=None):\n \"\"\" Fetch all information about the current object\n\n Args:\n async (bool): Boolean to make an asynchronous call. Default is False\n callback (function): Callback method that will be triggered in case of asynchronous call\n\n Returns:\n tuple: (current_fetcher, callee_parent, fetched_bjects, connection)\n\n Example:\n >>> entity = NUEntity(id=\"xxx-xxx-xxx-xxx\")\n >>> entity.fetch() # will get the entity with id \"xxx-xxx-xxx-xxx\"\n >>> print entity.name\n \"My Entity\"\n \"\"\"\n request = NURESTRequest(method=HTTP_METHOD_GET, url=self.get_resource_url())\n\n if async:\n return self.send_request(request=request, async=async, local_callback=self._did_fetch, remote_callback=callback)\n else:\n connection = self.send_request(request=request)\n return self._did_retrieve(connection)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef retired(self):\n\n def gen():\n import csv\n import re\n from datetime import datetime\n from pkg_resources import resource_filename\n\n with open(resource_filename(__package__, 'iso-639-3_Retirements.tab')) as rf:\n rtd = list(csv.reader(rf, delimiter='\\t'))[1:]\n rc = [r[0] for r in rtd]\n for i, _, _, m, s, d in rtd:\n d = datetime.strptime(d, '%Y-%m-%d')\n if not m:\n m = re.findall('\\[([a-z]{3})\\]', s)\n if m:\n m = [m] if isinstance(m, str) else m\n yield i, (d, [self.get(part3=x) for x in m if x not in rc], s)\n else:\n yield i, (d, [], s)\n\n yield 'sh', self.get(part3='hbs') # Add 'sh' as deprecated\n\n return dict(gen())", "response": "Returns a dictionary of code and description for retired languages."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list(self, request, *args, **kwargs):\n return super(CustomerViewSet, self).list(request, *args, **kwargs)", "response": "This method is used to get a list of customers."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef retrieve(self, request, *args, **kwargs):\n return super(CustomerViewSet, self).retrieve(request, *args, **kwargs)", "response": "This method is used to retrieve the related customer record sets."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(self, request, *args, **kwargs):\n return super(CustomerViewSet, self).create(request, *args, **kwargs)", "response": "A custom create method that returns a new customer with the specified attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list(self, request, *args, **kwargs):\n return super(ProjectViewSet, self).list(request, *args, **kwargs)", "response": "Returns a list of projects that are currently connected to the current user."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef retrieve(self, request, *args, **kwargs):\n return super(ProjectViewSet, self).retrieve(request, *args, **kwargs)", "response": "This method is used to retrieve the related resource class information."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create(self, request, *args, **kwargs):\n return super(ProjectViewSet, self).create(request, *args, **kwargs)", "response": "A new project can be created by users with staff privilege or customer owners."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef destroy(self, request, *args, **kwargs):\n return super(ProjectViewSet, self).destroy(request, *args, **kwargs)", "response": "This method is used to delete a project instance."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nchange a user s password", "response": "def password(self, request, uuid=None):\n \"\"\"\n To change a user password, submit a **POST** request to the user's RPC URL, specifying new password\n by staff user or account owner.\n\n Password is expected to be at least 7 symbols long and contain at least one number\n and at least one lower or upper case.\n\n Example of a valid request:\n\n .. code-block:: http\n\n POST /api/users/e0c058d06864441fb4f1c40dee5dd4fd/password/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"password\": \"nQvqHzeP123\",\n }\n \"\"\"\n user = self.get_object()\n\n serializer = serializers.PasswordSerializer(data=request.data)\n serializer.is_valid(raise_exception=True)\n\n new_password = serializer.validated_data['password']\n user.set_password(new_password)\n user.save()\n\n return Response({'detail': _('Password has been successfully updated.')},\n status=status.HTTP_200_OK)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nproject permissions expresses connection of user to a project. User may have either project manager or system administrator permission in the project. Use */api/project-permissions/* endpoint to maintain project permissions. Note that project permissions can be viewed and modified only by customer owners and staff users. To list all visible permissions, run a **GET** query against a list. Response will contain a list of project users and their brief data. To add a new user to the project, **POST** a new relationship to */api/project-permissions/* endpoint specifying project, user and the role of the user ('admin' or 'manager'): .. code-block:: http POST /api/project-permissions/ HTTP/1.1 Accept: application/json Authorization: Token 95a688962bf68678fd4c8cec4d138ddd9493c93b Host: example.com { \"project\": \"http://example.com/api/projects/6c9b01c251c24174a6691a1f894fae31/\", \"role\": \"manager\", \"user\": \"http://example.com/api/users/82cec6c8e0484e0ab1429412fe4194b7/\" }", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n Project permissions expresses connection of user to a project.\n User may have either project manager or system administrator permission in the project.\n Use */api/project-permissions/* endpoint to maintain project permissions.\n\n Note that project permissions can be viewed and modified only by customer owners and staff users.\n\n To list all visible permissions, run a **GET** query against a list.\n Response will contain a list of project users and their brief data.\n\n To add a new user to the project, **POST** a new relationship to */api/project-permissions/* endpoint specifying\n project, user and the role of the user ('admin' or 'manager'):\n\n .. code-block:: http\n\n POST /api/project-permissions/ HTTP/1.1\n Accept: application/json\n Authorization: Token 95a688962bf68678fd4c8cec4d138ddd9493c93b\n Host: example.com\n\n {\n \"project\": \"http://example.com/api/projects/6c9b01c251c24174a6691a1f894fae31/\",\n \"role\": \"manager\",\n \"user\": \"http://example.com/api/users/82cec6c8e0484e0ab1429412fe4194b7/\"\n }\n \"\"\"\n return super(ProjectPermissionViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of SSH keys for the current user.", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n To get a list of SSH keys, run **GET** against */api/keys/* as authenticated user.\n\n A new SSH key can be created by any active users. Example of a valid request:\n\n .. code-block:: http\n\n POST /api/keys/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"name\": \"ssh_public_key1\",\n \"public_key\": \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDURXDP5YhOQUYoDuTxJ84DuzqMJYJqJ8+SZT28\n TtLm5yBDRLKAERqtlbH2gkrQ3US58gd2r8H9jAmQOydfvgwauxuJUE4eDpaMWupqquMYsYLB5f+vVGhdZbbzfc6DTQ2rY\n dknWoMoArlG7MvRMA/xQ0ye1muTv+mYMipnd7Z+WH0uVArYI9QBpqC/gpZRRIouQ4VIQIVWGoT6M4Kat5ZBXEa9yP+9du\n D2C05GX3gumoSAVyAcDHn/xgej9pYRXGha4l+LKkFdGwAoXdV1z79EG1+9ns7wXuqMJFHM2KDpxAizV0GkZcojISvDwuh\n vEAFdOJcqjyyH4FOGYa8usP1 jhon@example.com\",\n }\n \"\"\"\n return super(SshKeyViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of all service settings owned by this user.", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n To get a list of service settings, run **GET** against */api/service-settings/* as an authenticated user.\n Only settings owned by this user or shared settings will be listed.\n\n Supported filters are:\n\n - ?name= - partial matching used for searching\n - ?type= - choices: OpenStack, DigitalOcean, Amazon, JIRA, GitLab, Oracle\n - ?state= - choices: New, Creation Scheduled, Creating, Sync Scheduled, Syncing, In Sync, Erred\n - ?shared= - allows to filter shared service settings\n \"\"\"\n return super(ServiceSettingsViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the service settings.", "response": "def update(self, request, *args, **kwargs):\n \"\"\"\n To update service settings, issue a **PUT** or **PATCH** to */api/service-settings//* as a customer owner.\n You are allowed to change name and credentials only.\n\n Example of a request:\n\n .. code-block:: http\n\n PATCH /api/service-settings/9079705c17d64e6aa0af2e619b0e0702/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"username\": \"admin\",\n \"password\": \"new_secret\"\n }\n \"\"\"\n return super(ServiceSettingsViewSet, self).update(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list(self, request, *args, **kwargs):\n\n return super(ResourceSummaryViewSet, self).list(request, *args, **kwargs)", "response": "Return a list of resources with SLA attached to the resource."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef count(self, request):\n queryset = self.filter_queryset(self.get_queryset())\n return Response({SupportedServices.get_name_for_model(qs.model): qs.count()\n for qs in queryset.querysets})", "response": "Count resources by type. Example output :\n Count resources by type."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfilters services by type", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n Filter services by type\n ^^^^^^^^^^^^^^^^^^^^^^^\n\n It is possible to filter services by their types. Example:\n\n /api/services/?service_type=DigitalOcean&service_type=OpenStack\n \"\"\"\n return super(ServicesViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nallow to execute action only if shared settings are not set.", "response": "def _require_staff_for_shared_settings(request, view, obj=None):\n \"\"\" Allow to execute action only if service settings are not shared or user is staff \"\"\"\n if obj is None:\n return\n\n if obj.settings.shared and not request.user.is_staff:\n raise PermissionDenied(_('Only staff users are allowed to import resources from shared services.'))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlink with Waldur resource.", "response": "def link(self, request, uuid=None):\n \"\"\"\n To get a list of resources available for import, run **GET** against *//link/*\n as an authenticated user.\n Optionally project_uuid parameter can be supplied for services requiring it like OpenStack.\n\n To import (link with Waldur) resource issue **POST** against the same endpoint with resource id.\n\n .. code-block:: http\n\n POST /api/openstack/08039f01c9794efc912f1689f4530cf0/link/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"backend_id\": \"bd5ec24d-9164-440b-a9f2-1b3c807c5df3\",\n \"project\": \"http://example.com/api/projects/e5f973af2eb14d2d8c38d62bcbaccb33/\"\n }\n \"\"\"\n\n service = self.get_object()\n\n if self.request.method == 'GET':\n try:\n backend = self.get_backend(service)\n try:\n resources = backend.get_resources_for_import(**self.get_import_context())\n except ServiceBackendNotImplemented:\n resources = []\n\n page = self.paginate_queryset(resources)\n if page is not None:\n return self.get_paginated_response(page)\n\n return Response(resources)\n except (ServiceBackendError, ValidationError) as e:\n raise APIException(e)\n\n else:\n serializer = self.get_serializer(data=request.data)\n serializer.is_valid(raise_exception=True)\n\n try:\n resource = serializer.save()\n except ServiceBackendError as e:\n raise APIException(e)\n\n resource_imported.send(\n sender=resource.__class__,\n instance=resource,\n )\n\n return Response(serializer.data, status=status.HTTP_200_OK)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unlink(self, request, uuid=None):\n service = self.get_object()\n service.unlink_descendants()\n self.perform_destroy(service)\n\n return Response(status=status.HTTP_204_NO_CONTENT)", "response": "Unlink all related resources service project link and service itself."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list(self, request, *args, **kwargs):\n return super(BaseServiceProjectLinkViewSet, self).list(request, *args, **kwargs)", "response": "This method is used to get a list of all available service - service connections."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching ancestors quotas that have the same name and are registered as aggregator quotas.", "response": "def get_aggregator_quotas(self, quota):\n \"\"\" Fetch ancestors quotas that have the same name and are registered as aggregator quotas. \"\"\"\n ancestors = quota.scope.get_quota_ancestors()\n aggregator_quotas = []\n for ancestor in ancestors:\n for ancestor_quota_field in ancestor.get_quotas_fields(field_class=AggregatorQuotaField):\n if ancestor_quota_field.get_child_quota_name() == quota.name:\n aggregator_quotas.append(ancestor.quotas.get(name=ancestor_quota_field))\n return aggregator_quotas"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd a new event handler.", "response": "def subscribe(self, handler):\n \"\"\"Adds a new event handler.\"\"\"\n assert callable(handler), \"Invalid handler %s\" % handler\n self.handlers.append(handler)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsubscribes to the specified event.", "response": "def on(self, event, handler):\n \"\"\"Attaches the handler to the specified event.\n\n @param event: event to attach the handler to. Any object can be passed\n as event, but string is preferable. If qcore.EnumBase\n instance is passed, its name is used as event key.\n @param handler: event handler.\n @return: self, so calls like this can be chained together.\n\n \"\"\"\n event_hook = self.get_or_create(event)\n event_hook.subscribe(handler)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef off(self, event, handler):\n event_hook = self.get_or_create(event)\n event_hook.unsubscribe(handler)\n return self", "response": "Detaches the handler from the specified event."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef trigger(self, event, *args):\n event_hook = self.get_or_create(event)\n event_hook.trigger(*args)\n return self", "response": "Triggers the specified event by invoking EventHook. trigger under the hood."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef safe_trigger(self, event, *args):\n event_hook = self.get_or_create(event)\n event_hook.safe_trigger(*args)\n return self", "response": "Safely triggers the specified event by invoking EventHook. safe_trigger under the hood."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_or_create(self, event):\n if isinstance(event, EnumBase):\n event = event.short_name\n return self.__dict__.setdefault(event, EventHook())", "response": "Gets or creates a new event hook for the specified event."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_pseudo_abi_for_input(s, timeout=None, proxies=None):\n sighash = Utils.bytes_to_str(s[:4])\n for pseudo_abi in FourByteDirectory.get_pseudo_abi_for_sighash(sighash, timeout=timeout, proxies=proxies):\n types = [ti[\"type\"] for ti in pseudo_abi['inputs']]\n try:\n # test decoding\n _ = decode_abi(types, s[4:])\n yield pseudo_abi\n except eth_abi.exceptions.DecodingError as e:\n continue", "response": "Given a 4bytes. directory return a list of pseudo abi for the input."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npreparing the contract json abi for sighash lookups and fast access", "response": "def _prepare_abi(self, jsonabi):\n \"\"\"\n Prepare the contract json abi for sighash lookups and fast access\n\n :param jsonabi: contracts abi in json format\n :return:\n \"\"\"\n self.signatures = {}\n for element_description in jsonabi:\n abi_e = AbiMethod(element_description)\n if abi_e[\"type\"] == \"constructor\":\n self.signatures[b\"__constructor__\"] = abi_e\n elif abi_e[\"type\"] == \"fallback\":\n abi_e.setdefault(\"inputs\", [])\n self.signatures[b\"__fallback__\"] = abi_e\n elif abi_e[\"type\"] == \"function\":\n # function and signature present\n # todo: we could generate the sighash ourselves? requires keccak256\n if abi_e.get(\"signature\"):\n self.signatures[Utils.str_to_bytes(abi_e[\"signature\"])] = abi_e\n elif abi_e[\"type\"] == \"event\":\n self.signatures[b\"__event__\"] = abi_e\n else:\n raise Exception(\"Invalid abi type: %s - %s - %s\" % (abi_e.get(\"type\"),\n element_description, abi_e))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndescribes the input bytesequence s based on the loaded contract abi definition", "response": "def describe_constructor(self, s):\n \"\"\"\n Describe the input bytesequence (constructor arguments) s based on the loaded contract\n abi definition\n\n :param s: bytes constructor arguments\n :return: AbiMethod instance\n \"\"\"\n method = self.signatures.get(b\"__constructor__\")\n if not method:\n # constructor not available\n m = AbiMethod({\"type\": \"constructor\", \"name\": \"\", \"inputs\": [], \"outputs\": []})\n return m\n\n types_def = method[\"inputs\"]\n types = [t[\"type\"] for t in types_def]\n names = [t[\"name\"] for t in types_def]\n\n if not len(s):\n values = len(types) * [\"\"]\n else:\n values = decode_abi(types, s)\n\n # (type, name, data)\n method.inputs = [{\"type\": t, \"name\": n, \"data\": v} for t, n, v in list(\n zip(types, names, values))]\n return method"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef describe_input(self, s):\n signatures = self.signatures.items()\n\n for sighash, method in signatures:\n if sighash is None or sighash.startswith(b\"__\"):\n continue # skip constructor\n\n if s.startswith(sighash):\n s = s[len(sighash):]\n\n types_def = self.signatures.get(sighash)[\"inputs\"]\n types = [t[\"type\"] for t in types_def]\n names = [t[\"name\"] for t in types_def]\n\n if not len(s):\n values = len(types) * [\"\"]\n else:\n values = decode_abi(types, s)\n\n # (type, name, data)\n method.inputs = [{\"type\": t, \"name\": n, \"data\": v} for t, n, v in list(\n zip(types, names, values))]\n return method\n else:\n method = AbiMethod({\"type\": \"fallback\",\n \"name\": \"__fallback__\",\n \"inputs\": [], \"outputs\": []})\n types_def = self.signatures.get(b\"__fallback__\", {\"inputs\": []})[\"inputs\"]\n types = [t[\"type\"] for t in types_def]\n names = [t[\"name\"] for t in types_def]\n\n values = decode_abi(types, s)\n\n # (type, name, data)\n method.inputs = [{\"type\": t, \"name\": n, \"data\": v} for t, n, v in list(\n zip(types, names, values))]\n return method", "response": "Describe the input bytesequence s based on the loaded contract abi definition\n\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a new AbiMethod object from an input stream containing the input stream s.", "response": "def from_input_lookup(s):\n \"\"\"\n Return a new AbiMethod object from an input stream\n :param s: binary input\n :return: new AbiMethod object matching the provided input stream\n \"\"\"\n for pseudo_abi in FourByteDirectory.get_pseudo_abi_for_input(s):\n method = AbiMethod(pseudo_abi)\n types_def = pseudo_abi[\"inputs\"]\n types = [t[\"type\"] for t in types_def]\n names = [t[\"name\"] for t in types_def]\n\n values = decode_abi(types, s[4:])\n\n # (type, name, data)\n method.inputs = [{\"type\": t, \"name\": n, \"data\": v} for t, n, v in list(\n zip(types, names, values))]\n return method"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nraise an AssertionError if expected is not actual.", "response": "def assert_is(expected, actual, message=None, extra=None):\n \"\"\"Raises an AssertionError if expected is not actual.\"\"\"\n assert expected is actual, _assert_fail_message(\n message, expected, actual, \"is not\", extra\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nraises an AssertionError if expected is actual.", "response": "def assert_is_not(expected, actual, message=None, extra=None):\n \"\"\"Raises an AssertionError if expected is actual.\"\"\"\n assert expected is not actual, _assert_fail_message(\n message, expected, actual, \"is\", extra\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nraise an AssertionError if value is not an instance of types.", "response": "def assert_is_instance(value, types, message=None, extra=None):\n \"\"\"Raises an AssertionError if value is not an instance of type(s).\"\"\"\n assert isinstance(value, types), _assert_fail_message(\n message, value, types, \"is not an instance of\", extra\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef assert_eq(expected, actual, message=None, tolerance=None, extra=None):\n if tolerance is None:\n assert expected == actual, _assert_fail_message(\n message, expected, actual, \"!=\", extra\n )\n else:\n assert isinstance(tolerance, _number_types), (\n \"tolerance parameter to assert_eq must be a number: %r\" % tolerance\n )\n assert isinstance(expected, _number_types) and isinstance(\n actual, _number_types\n ), (\n \"parameters must be numbers when tolerance is specified: %r, %r\"\n % (expected, actual)\n )\n\n diff = abs(expected - actual)\n assert diff <= tolerance, _assert_fail_message(\n message, expected, actual, \"is more than %r away from\" % tolerance, extra\n )", "response": "Raises an AssertionError if expected!= actual."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef assert_dict_eq(expected, actual, number_tolerance=None, dict_path=[]):\n assert_is_instance(expected, dict)\n assert_is_instance(actual, dict)\n\n expected_keys = set(expected.keys())\n actual_keys = set(actual.keys())\n assert expected_keys <= actual_keys, \"Actual dict at %s is missing keys: %r\" % (\n _dict_path_string(dict_path),\n expected_keys - actual_keys,\n )\n assert actual_keys <= expected_keys, \"Actual dict at %s has extra keys: %r\" % (\n _dict_path_string(dict_path),\n actual_keys - expected_keys,\n )\n\n for k in expected_keys:\n key_path = dict_path + [k]\n assert_is_instance(\n actual[k],\n type(expected[k]),\n extra=\"Types don't match for %s\" % _dict_path_string(key_path),\n )\n assert_is_instance(\n expected[k],\n type(actual[k]),\n extra=\"Types don't match for %s\" % _dict_path_string(key_path),\n )\n\n if isinstance(actual[k], dict):\n assert_dict_eq(\n expected[k],\n actual[k],\n number_tolerance=number_tolerance,\n dict_path=key_path,\n )\n elif isinstance(actual[k], _number_types):\n assert_eq(\n expected[k],\n actual[k],\n extra=\"Value doesn't match for %s\" % _dict_path_string(key_path),\n tolerance=number_tolerance,\n )\n else:\n assert_eq(\n expected[k],\n actual[k],\n extra=\"Value doesn't match for %s\" % _dict_path_string(key_path),\n )", "response": "Asserts that two dictionaries are equal producing a custom message if they are not."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef assert_gt(left, right, message=None, extra=None):\n assert left > right, _assert_fail_message(message, left, right, \"<=\", extra)", "response": "Raises an AssertionError if left_hand < = right_hand."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nraises an AssertionError if left_hand < right_hand.", "response": "def assert_ge(left, right, message=None, extra=None):\n \"\"\"Raises an AssertionError if left_hand < right_hand.\"\"\"\n assert left >= right, _assert_fail_message(message, left, right, \"<\", extra)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nraises an AssertionError if left_hand < right_hand.", "response": "def assert_lt(left, right, message=None, extra=None):\n \"\"\"Raises an AssertionError if left_hand >= right_hand.\"\"\"\n assert left < right, _assert_fail_message(message, left, right, \">=\", extra)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nraise an AssertionError if left_hand > right_hand.", "response": "def assert_le(left, right, message=None, extra=None):\n \"\"\"Raises an AssertionError if left_hand > right_hand.\"\"\"\n assert left <= right, _assert_fail_message(message, left, right, \">\", extra)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nraising an AssertionError if obj is not in seq.", "response": "def assert_in(obj, seq, message=None, extra=None):\n \"\"\"Raises an AssertionError if obj is not in seq.\"\"\"\n assert obj in seq, _assert_fail_message(message, obj, seq, \"is not in\", extra)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nraise an AssertionError if obj is in iter.", "response": "def assert_not_in(obj, seq, message=None, extra=None):\n \"\"\"Raises an AssertionError if obj is in iter.\"\"\"\n # for very long strings, provide a truncated error\n if isinstance(seq, six.string_types) and obj in seq and len(seq) > 200:\n index = seq.find(obj)\n start_index = index - 50\n if start_index > 0:\n truncated = \"(truncated) ...\"\n else:\n truncated = \"\"\n start_index = 0\n end_index = index + len(obj) + 50\n truncated += seq[start_index:end_index]\n if end_index < len(seq):\n truncated += \"... (truncated)\"\n assert False, _assert_fail_message(message, obj, truncated, \"is in\", extra)\n assert obj not in seq, _assert_fail_message(message, obj, seq, \"is in\", extra)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nraising an AssertionError if obj is not in seq using assert_eq cmp.", "response": "def assert_in_with_tolerance(obj, seq, tolerance, message=None, extra=None):\n \"\"\"Raises an AssertionError if obj is not in seq using assert_eq cmp.\"\"\"\n for i in seq:\n try:\n assert_eq(obj, i, tolerance=tolerance, message=message, extra=extra)\n return\n except AssertionError:\n pass\n assert False, _assert_fail_message(message, obj, seq, \"is not in\", extra)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef assert_is_substring(substring, subject, message=None, extra=None):\n assert (\n (subject is not None)\n and (substring is not None)\n and (subject.find(substring) != -1)\n ), _assert_fail_message(message, substring, subject, \"is not in\", extra)", "response": "Raises an AssertionError if substring is not a substring of subject."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nraising an AssertionError if substring is a substring of subject.", "response": "def assert_is_not_substring(substring, subject, message=None, extra=None):\n \"\"\"Raises an AssertionError if substring is a substring of subject.\"\"\"\n assert (\n (subject is not None)\n and (substring is not None)\n and (subject.find(substring) == -1)\n ), _assert_fail_message(message, substring, subject, \"is in\", extra)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef assert_unordered_list_eq(expected, actual, message=None):\n missing_in_actual = []\n missing_in_expected = list(actual)\n for x in expected:\n try:\n missing_in_expected.remove(x)\n except ValueError:\n missing_in_actual.append(x)\n\n if missing_in_actual or missing_in_expected:\n if not message:\n message = (\n \"%r not equal to %r; missing items: %r in expected, %r in actual.\"\n % (expected, actual, missing_in_expected, missing_in_actual)\n )\n assert False, message", "response": "Raises an AssertionError if the objects contained\n in expected are not equal to the objects contained\n in actual without regard to their order."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes function only if one of input parameters is not empty", "response": "def _execute_if_not_empty(func):\n \"\"\" Execute function only if one of input parameters is not empty \"\"\"\n def wrapper(*args, **kwargs):\n if any(args[1:]) or any(kwargs.items()):\n return func(*args, **kwargs)\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\npreparing the elasticsearch query for elasticsearch query", "response": "def prepare_search_body(self, should_terms=None, must_terms=None, must_not_terms=None, search_text='', start=None, end=None):\n \"\"\"\n Prepare body for elasticsearch query\n\n Search parameters\n ^^^^^^^^^^^^^^^^^\n These parameters are dictionaries and have format: : [, ...]\n should_terms: it resembles logical OR\n must_terms: it resembles logical AND\n must_not_terms: it resembles logical NOT\n\n search_text : string\n Text for FTS(full text search)\n start, end : datetime\n Filter for event creation time\n \"\"\"\n self.body = self.SearchBody()\n self.body.set_should_terms(should_terms)\n self.body.set_must_terms(must_terms)\n self.body.set_must_not_terms(must_not_terms)\n self.body.set_search_text(search_text)\n self.body.set_timestamp_filter(start, end)\n self.body.prepare()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef execute(cls, instance, async=True, countdown=2, is_heavy_task=False, **kwargs):\n cls.pre_apply(instance, async=async, **kwargs)\n result = cls.apply_signature(instance, async=async, countdown=countdown,\n is_heavy_task=is_heavy_task, **kwargs)\n cls.post_apply(instance, async=async, **kwargs)\n return result", "response": "Execute high level - operation."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef apply_signature(cls, instance, async=True, countdown=None, is_heavy_task=False, **kwargs):\n serialized_instance = utils.serialize_instance(instance)\n\n signature = cls.get_task_signature(instance, serialized_instance, **kwargs)\n link = cls.get_success_signature(instance, serialized_instance, **kwargs)\n link_error = cls.get_failure_signature(instance, serialized_instance, **kwargs)\n\n if async:\n return signature.apply_async(link=link, link_error=link_error, countdown=countdown,\n queue=is_heavy_task and 'heavy' or None)\n else:\n result = signature.apply()\n callback = link if not result.failed() else link_error\n if callback is not None:\n cls._apply_callback(callback, result)\n\n return result.get()", "response": "Serialize input data and apply signature"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a description in format for the given entity.", "response": "def get_entity_description(entity):\n \"\"\"\n Returns description in format:\n * entity human readable name\n * docstring\n \"\"\"\n\n try:\n entity_name = entity.__name__.strip('_')\n except AttributeError:\n # entity is a class instance\n entity_name = entity.__class__.__name__\n\n label = '* %s' % formatting.camelcase_to_spaces(entity_name)\n if entity.__doc__ is not None:\n entity_docstring = formatting.dedent(smart_text(entity.__doc__)).replace('\\n', '\\n\\t')\n return '%s\\n * %s' % (label, entity_docstring)\n\n return label"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_validators_description(view):\n action = getattr(view, 'action', None)\n if action is None:\n return ''\n\n description = ''\n validators = getattr(view, action + '_validators', [])\n for validator in validators:\n validator_description = get_entity_description(validator)\n description += '\\n' + validator_description if description else validator_description\n\n return '### Validators:\\n' + description if description else ''", "response": "Returns a string describing the validators in the order they appear in the view."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_actions_permission_description(view, method):\n action = getattr(view, 'action', None)\n if action is None:\n return ''\n\n if hasattr(view, action + '_permissions'):\n permission_types = (action,)\n elif method in SAFE_METHODS:\n permission_types = ('safe_methods', '%s_extra' % action)\n else:\n permission_types = ('unsafe_methods', '%s_extra' % action)\n\n description = ''\n for permission_type in permission_types:\n action_perms = getattr(view, permission_type + '_permissions', [])\n for permission in action_perms:\n action_perm_description = get_entity_description(permission)\n description += '\\n' + action_perm_description if description else action_perm_description\n\n return description", "response": "Returns the description of the actions permissions."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_permissions_description(view, method):\n if not hasattr(view, 'permission_classes'):\n return ''\n\n description = ''\n for permission_class in view.permission_classes:\n if permission_class == core_permissions.ActionsPermission:\n actions_perm_description = get_actions_permission_description(view, method)\n if actions_perm_description:\n description += '\\n' + actions_perm_description if description else actions_perm_description\n continue\n perm_description = get_entity_description(permission_class)\n description += '\\n' + perm_description if description else perm_description\n\n return '### Permissions:\\n' + description if description else ''", "response": "Returns a string with the description of the permissions."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_validation_description(view, method):\n if method not in ('PUT', 'PATCH', 'POST') or not hasattr(view, 'get_serializer'):\n return ''\n\n serializer = view.get_serializer()\n description = ''\n if hasattr(serializer, 'validate') and serializer.validate.__doc__ is not None:\n description += formatting.dedent(smart_text(serializer.validate.__doc__))\n\n for field in serializer.fields.values():\n if not hasattr(serializer, 'validate_' + field.field_name):\n continue\n\n field_validation = getattr(serializer, 'validate_' + field.field_name)\n\n if field_validation.__doc__ is not None:\n docstring = formatting.dedent(smart_text(field_validation.__doc__)).replace('\\n', '\\n\\t')\n field_description = '* %s\\n * %s' % (field.field_name, docstring)\n description += '\\n' + field_description if description else field_description\n\n return '### Validation:\\n' + description if description else ''", "response": "Returns the validation description in format for a given view and method."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_field_type(field):\n if isinstance(field, core_filters.MappedMultipleChoiceFilter):\n return ' | '.join(['\"%s\"' % f for f in sorted(field.mapped_to_model)])\n if isinstance(field, OrderingFilter) or isinstance(field, ChoiceFilter):\n return ' | '.join(['\"%s\"' % f[0] for f in field.extra['choices']])\n if isinstance(field, ChoiceField):\n return ' | '.join(['\"%s\"' % f for f in sorted(field.choices)])\n if isinstance(field, HyperlinkedRelatedField):\n if field.view_name.endswith('detail'):\n return 'link to %s' % reverse(field.view_name,\n kwargs={'%s' % field.lookup_field: \"'%s'\" % field.lookup_field})\n return reverse(field.view_name)\n if isinstance(field, structure_filters.ServiceTypeFilter):\n return ' | '.join(['\"%s\"' % f for f in SupportedServices.get_filter_mapping().keys()])\n if isinstance(field, ResourceTypeFilter):\n return ' | '.join(['\"%s\"' % f for f in SupportedServices.get_resource_models().keys()])\n if isinstance(field, core_serializers.GenericRelatedField):\n links = []\n for model in field.related_models:\n detail_view_name = core_utils.get_detail_view_name(model)\n for f in field.lookup_fields:\n try:\n link = reverse(detail_view_name, kwargs={'%s' % f: \"'%s'\" % f})\n except NoReverseMatch:\n pass\n else:\n links.append(link)\n break\n path = ', '.join(links)\n if path:\n return 'link to any: %s' % path\n if isinstance(field, core_filters.ContentTypeFilter):\n return \"string in form 'app_label'.'model_name'\"\n if isinstance(field, ModelMultipleChoiceFilter):\n return get_field_type(field.field)\n if isinstance(field, ListSerializer):\n return 'list of [%s]' % get_field_type(field.child)\n if isinstance(field, ManyRelatedField):\n return 'list of [%s]' % get_field_type(field.child_relation)\n if isinstance(field, ModelField):\n return get_field_type(field.model_field)\n\n name = field.__class__.__name__\n for w in ('Filter', 'Field', 'Serializer'):\n name = name.replace(w, '')\n return FIELDS.get(name, name)", "response": "Returns the field type and possible values."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_disabled_action(view):\n if not isinstance(view, core_views.ActionsViewSet):\n return False\n\n action = getattr(view, 'action', None)\n return action in view.disabled_actions if action is not None else False", "response": "Checks whether Link action is disabled."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_allowed_methods(self, callback):\n if hasattr(callback, 'actions'):\n return [method.upper() for method in callback.actions.keys() if method != 'head']\n\n return [\n method for method in\n callback.cls().allowed_methods if method not in ('OPTIONS', 'HEAD')\n ]", "response": "Return a list of the HTTP methods that can be used for this endpoint."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngiving a callback method request return an actual view instance.", "response": "def create_view(self, callback, method, request=None):\n \"\"\"\n Given a callback, return an actual view instance.\n \"\"\"\n view = super(WaldurSchemaGenerator, self).create_view(callback, method, request)\n if is_disabled_action(view):\n view.exclude_from_schema = True\n\n return view"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_description(self, path, method, view):\n description = super(WaldurSchemaGenerator, self).get_description(path, method, view)\n\n permissions_description = get_permissions_description(view, method)\n if permissions_description:\n description += '\\n\\n' + permissions_description if description else permissions_description\n\n if isinstance(view, core_views.ActionsViewSet):\n validators_description = get_validators_description(view)\n if validators_description:\n description += '\\n\\n' + validators_description if description else validators_description\n\n validation_description = get_validation_description(view, method)\n if validation_description:\n description += '\\n\\n' + validation_description if description else validation_description\n\n return description", "response": "Determine a link description."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of coreapi. Field instances corresponding to any applet entry in the request body.", "response": "def get_serializer_fields(self, path, method, view):\n \"\"\"\n Return a list of `coreapi.Field` instances corresponding to any\n request body input, as determined by the serializer class.\n \"\"\"\n if method not in ('PUT', 'PATCH', 'POST'):\n return []\n\n if not hasattr(view, 'get_serializer'):\n return []\n\n serializer = view.get_serializer()\n if not isinstance(serializer, Serializer):\n return []\n\n fields = []\n for field in serializer.fields.values():\n if field.read_only or isinstance(field, HiddenField):\n continue\n\n required = field.required and method != 'PATCH'\n description = force_text(field.help_text) if field.help_text else ''\n field_type = get_field_type(field)\n description += '; ' + field_type if description else field_type\n field = coreapi.Field(\n name=field.field_name,\n location='form',\n required=required,\n description=description,\n schema=schemas.field_to_schema(field),\n )\n fields.append(field)\n\n return fields"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete error message if instance state changed from erred", "response": "def delete_error_message(sender, instance, name, source, target, **kwargs):\n \"\"\" Delete error message if instance state changed from erred \"\"\"\n if source != StateMixin.States.ERRED:\n return\n instance.error_message = ''\n instance.save(update_fields=['error_message'])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfind version string in the file", "response": "def find_version(*file_paths):\n \"\"\"\n read __init__.py\n \"\"\"\n file_path = os.path.join(*file_paths)\n with open(file_path, 'r') as version_file:\n line = version_file.readline()\n while line:\n if line.startswith('__version__'):\n version_match = re.search(\n r\"^__version__ = ['\\\"]([^'\\\"]*)['\\\"]\",\n line,\n re.M\n )\n if version_match:\n return version_match.group(1)\n line = version_file.readline()\n raise RuntimeError('Unable to find version string.')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef trace(enter=False, exit=True):\n\n def decorate(fn):\n @inspection.wraps(fn)\n def new_fn(*args, **kwargs):\n name = fn.__module__ + \".\" + fn.__name__\n if enter:\n print(\n \"%s(args = %s, kwargs = %s) <-\" % (name, repr(args), repr(kwargs))\n )\n try:\n result = fn(*args, **kwargs)\n if exit:\n print(\n \"%s(args = %s, kwargs = %s) -> %s\"\n % (name, repr(args), repr(kwargs), repr(result))\n )\n return result\n except Exception as e:\n if exit:\n print(\n \"%s(args = %s, kwargs = %s) -> thrown %s\"\n % (name, repr(args), repr(kwargs), str(e))\n )\n raise\n\n return new_fn\n\n return decorate", "response": "This decorator prints entry and exit message when the decorated method is called."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _make_value(self, value):\n member = self.__new__(self, value)\n member.__init__(value)\n return member", "response": "Instantiates an arbitrary value."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create(cls, name, members):\n NewEnum = type(name, (cls,), {})\n\n if isinstance(members, dict):\n members = members.items()\n for member in members:\n if isinstance(member, tuple):\n name, value = member\n setattr(NewEnum, name, value)\n elif isinstance(member, EnumBase):\n setattr(NewEnum, member.short_name, member.value)\n else:\n assert False, (\n \"members must be either a dict, \"\n + \"a list of (name, value) tuples, \"\n + \"or a list of EnumBase instances.\"\n )\n\n NewEnum.process()\n\n # needed for pickling to work (hopefully); taken from the namedtuple implementation in the\n # standard library\n try:\n NewEnum.__module__ = sys._getframe(1).f_globals.get(\"__name__\", \"__main__\")\n except (AttributeError, ValueError):\n pass\n\n return NewEnum", "response": "Creates a new enum type based on this one and adds members to the newly created subclass of cls."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses an enum member name or value into an enum member.", "response": "def parse(cls, value, default=_no_default):\n \"\"\"Parses an enum member name or value into an enum member.\n\n Accepts the following types:\n - Members of this enum class. These are returned directly.\n - Integers. If there is an enum member with the integer as a value, that member is returned.\n - Strings. If there is an enum member with the string as its name, that member is returned.\n For integers and strings that don't correspond to an enum member, default is returned; if\n no default is given the function raises KeyError instead.\n\n Examples:\n\n >>> class Color(Enum):\n ... red = 1\n ... blue = 2\n >>> Color.parse(Color.red)\n Color.red\n >>> Color.parse(1)\n Color.red\n >>> Color.parse('blue')\n Color.blue\n\n \"\"\"\n if isinstance(value, cls):\n return value\n elif isinstance(value, six.integer_types) and not isinstance(value, EnumBase):\n e = cls._value_to_member.get(value, _no_default)\n else:\n e = cls._name_to_member.get(value, _no_default)\n if e is _no_default or not e.is_valid():\n if default is _no_default:\n raise _create_invalid_value_error(cls, value)\n return default\n return e"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses a string or integer into a Flags instance.", "response": "def parse(cls, value, default=_no_default):\n \"\"\"Parses a flag integer or string into a Flags instance.\n\n Accepts the following types:\n - Members of this enum class. These are returned directly.\n - Integers. These are converted directly into a Flags instance with the given name.\n - Strings. The function accepts a comma-delimited list of flag names, corresponding to\n members of the enum. These are all ORed together.\n\n Examples:\n\n >>> class Car(Flags):\n ... is_big = 1\n ... has_wheels = 2\n >>> Car.parse(1)\n Car.is_big\n >>> Car.parse(3)\n Car.parse('has_wheels,is_big')\n >>> Car.parse('is_big,has_wheels')\n Car.parse('has_wheels,is_big')\n\n \"\"\"\n if isinstance(value, cls):\n return value\n elif isinstance(value, int):\n e = cls._make_value(value)\n else:\n if not value:\n e = cls._make_value(0)\n else:\n r = 0\n for k in value.split(\",\"):\n v = cls._name_to_member.get(k, _no_default)\n if v is _no_default:\n if default is _no_default:\n raise _create_invalid_value_error(cls, value)\n else:\n return default\n r |= v.value\n e = cls._make_value(r)\n if not e.is_valid():\n if default is _no_default:\n raise _create_invalid_value_error(cls, value)\n return default\n return e"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of all available price list items.", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n To get a list of price list items, run **GET** against */api/price-list-items/* as an authenticated user.\n \"\"\"\n return super(PriceListItemViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create(self, request, *args, **kwargs):\n return super(PriceListItemViewSet, self).create(request, *args, **kwargs)", "response": "Create a new price list item."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update(self, request, *args, **kwargs):\n return super(PriceListItemViewSet, self).update(request, *args, **kwargs)", "response": "Update the item with the given request."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete the current item and all its children.", "response": "def destroy(self, request, *args, **kwargs):\n \"\"\"\n Run **DELETE** request against */api/price-list-items//* to delete price list item.\n Only customer owner and staff can delete price items.\n \"\"\"\n return super(PriceListItemViewSet, self).destroy(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of available price list items for the current user.", "response": "def list(self, request, *args, **kwargs):\n \"\"\"\n To get a list of default price list items, run **GET** against */api/default-price-list-items/*\n as authenticated user.\n\n Price lists can be filtered by:\n - ?key=\n - ?item_type= has to be from list of available item_types\n (available options: 'flavor', 'storage', 'license-os', 'license-application', 'network', 'support')\n - ?resource_type= resource type, for example: 'OpenStack.Instance, 'Oracle.Database')\n \"\"\"\n return super(DefaultPriceListItemViewSet, self).list(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list(self, request, *args, **kwargs):\n return super(MergedPriceListItemViewSet, self).list(request, *args, **kwargs)", "response": "Returns a list of merged price list items."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef Remote(path=None, loader=Notebook, **globals):\n\n class Remote(RemoteMixin, loader):\n ...\n\n return Remote(path=path, **globals)", "response": "A remote notebook finder."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget permission checks that will be executed for current action.", "response": "def get_permission_checks(self, request, view):\n \"\"\"\n Get permission checks that will be executed for current action.\n \"\"\"\n if view.action is None:\n return []\n # if permissions are defined for view directly - use them.\n if hasattr(view, view.action + '_permissions'):\n return getattr(view, view.action + '_permissions')\n # otherwise return view-level permissions + extra view permissions\n extra_permissions = getattr(view, view.action + 'extra_permissions', [])\n if request.method in SAFE_METHODS:\n return getattr(view, 'safe_methods_permissions', []) + extra_permissions\n else:\n return getattr(view, 'unsafe_methods_permissions', []) + extra_permissions"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_function(self, function):\n #noinspection PyTypeChecker\n if not len(self.settings.FUNCTION_MANAGERS):\n raise ConfigurationError(\n 'Where have the default function manager gone?!')\n self.settings.FUNCTION_MANAGERS[0].add_function(function)", "response": "Adds a function to the default fixed function manager."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nformat a raw field as a string.", "response": "def format_raw_field(key):\n \"\"\"\n When ElasticSearch analyzes string, it breaks it into parts.\n In order make query for not-analyzed exact string values, we should use subfield instead.\n\n The index template for Elasticsearch 5.0 has been changed.\n The subfield for string multi-fields has changed from .raw to .keyword\n\n Thus workaround for backward compatibility during migration is required.\n See also: https://github.com/elastic/logstash/blob/v5.4.1/docs/static/breaking-changes.asciidoc\n \"\"\"\n subfield = django_settings.WALDUR_CORE.get('ELASTICSEARCH', {}).get('raw_subfield', 'keyword')\n return '%s.%s' % (camel_case_to_underscore(key), subfield)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a decorator function that applies the decorator_cls that was passed in.", "response": "def decorate(decorator_cls, *args, **kwargs):\n \"\"\"Creates a decorator function that applies the decorator_cls that was passed in.\"\"\"\n global _wrappers\n\n wrapper_cls = _wrappers.get(decorator_cls, None)\n if wrapper_cls is None:\n\n class PythonWrapper(decorator_cls):\n pass\n\n wrapper_cls = PythonWrapper\n wrapper_cls.__name__ = decorator_cls.__name__ + \"PythonWrapper\"\n _wrappers[decorator_cls] = wrapper_cls\n\n def decorator(fn):\n wrapped = wrapper_cls(fn, *args, **kwargs)\n _update_wrapper(wrapped, fn)\n return wrapped\n\n return decorator"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef deprecated(replacement_description):\n\n def decorate(fn_or_class):\n if isinstance(fn_or_class, type):\n pass # Can't change __doc__ of type objects\n else:\n try:\n fn_or_class.__doc__ = \"This API point is obsolete. %s\\n\\n%s\" % (\n replacement_description,\n fn_or_class.__doc__,\n )\n except AttributeError:\n pass # For Cython method descriptors, etc.\n return fn_or_class\n\n return decorate", "response": "A decorator that marks a class as deprecated."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef retry(exception_cls, max_tries=10, sleep=0.05):\n\n assert max_tries > 0\n\n def with_max_retries_call(delegate):\n for i in xrange(0, max_tries):\n try:\n return delegate()\n except exception_cls:\n if i + 1 == max_tries:\n raise\n time.sleep(sleep)\n\n def outer(fn):\n is_generator = inspect.isgeneratorfunction(fn)\n\n @functools.wraps(fn)\n def retry_fun(*args, **kwargs):\n return with_max_retries_call(lambda: fn(*args, **kwargs))\n\n @functools.wraps(fn)\n def retry_generator_fun(*args, **kwargs):\n def get_first_item():\n results = fn(*args, **kwargs)\n for first_result in results:\n return [first_result], results\n return [], results\n\n cache, generator = with_max_retries_call(get_first_item)\n\n for item in cache:\n yield item\n\n for item in generator:\n yield item\n\n if not is_generator:\n # so that qcore.inspection.get_original_fn can retrieve the original function\n retry_fun.fn = fn\n # Necessary for pickling of Cythonized functions to work. Cython's __reduce__\n # method always returns the original name of the function.\n retry_fun.__reduce__ = lambda: fn.__name__\n return retry_fun\n else:\n retry_generator_fun.fn = fn\n retry_generator_fun.__reduce__ = lambda: fn.__name__\n return retry_generator_fun\n\n return outer", "response": "Decorator for retrying a function if it throws an exception."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a context manager into a decorator.", "response": "def decorator_of_context_manager(ctxt):\n \"\"\"Converts a context manager into a decorator.\n\n This decorator will run the decorated function in the context of the\n manager.\n\n :param ctxt: Context to run the function in.\n :return: Wrapper around the original function.\n\n \"\"\"\n\n def decorator_fn(*outer_args, **outer_kwargs):\n def decorator(fn):\n @functools.wraps(fn)\n def wrapper(*args, **kwargs):\n with ctxt(*outer_args, **outer_kwargs):\n return fn(*args, **kwargs)\n\n return wrapper\n\n return decorator\n\n if getattr(ctxt, \"__doc__\", None) is None:\n msg = \"Decorator that runs the inner function in the context of %s\"\n decorator_fn.__doc__ = msg % ctxt\n else:\n decorator_fn.__doc__ = ctxt.__doc__\n return decorator_fn"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nvalidating that the quota change of an object is exceeded.", "response": "def validate_quota_change(self, quota_deltas, raise_exception=False):\n \"\"\"\n Get error messages about object and his ancestor quotas that will be exceeded if quota_delta will be added.\n\n raise_exception - if True QuotaExceededException will be raised if validation fails\n quota_deltas - dictionary of quotas deltas, example:\n {\n 'ram': 1024,\n 'storage': 2048,\n ...\n }\n Example of output:\n ['ram quota limit: 1024, requires: 2048(instance#1)', ...]\n\n \"\"\"\n errors = []\n for name, delta in six.iteritems(quota_deltas):\n quota = self.quotas.get(name=name)\n if quota.is_exceeded(delta):\n errors.append('%s quota limit: %s, requires %s (%s)\\n' % (\n quota.name, quota.limit, quota.usage + delta, quota.scope))\n if not raise_exception:\n return errors\n else:\n if errors:\n raise exceptions.QuotaExceededException(_('One or more quotas were exceeded: %s') % ';'.join(errors))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_sum_of_quotas_as_dict(cls, scopes, quota_names=None, fields=['usage', 'limit']):\n if not scopes:\n return {}\n\n if quota_names is None:\n quota_names = cls.get_quotas_names()\n\n scope_models = set([scope._meta.model for scope in scopes])\n if len(scope_models) > 1:\n raise exceptions.QuotaError(_('All scopes have to be instances of the same model.'))\n\n filter_kwargs = {\n 'content_type': ct_models.ContentType.objects.get_for_model(scopes[0]),\n 'object_id__in': [scope.id for scope in scopes],\n 'name__in': quota_names\n }\n\n result = {}\n if 'usage' in fields:\n items = Quota.objects.filter(**filter_kwargs)\\\n .values('name').annotate(usage=Sum('usage'))\n for item in items:\n result[item['name'] + '_usage'] = item['usage']\n\n if 'limit' in fields:\n unlimited_quotas = Quota.objects.filter(limit=-1, **filter_kwargs)\n unlimited_quotas = list(unlimited_quotas.values_list('name', flat=True))\n for quota_name in unlimited_quotas:\n result[quota_name] = -1\n\n items = Quota.objects\\\n .filter(**filter_kwargs)\\\n .exclude(name__in=unlimited_quotas)\\\n .values('name')\\\n .annotate(limit=Sum('limit'))\n for item in items:\n result[item['name']] = item['limit']\n\n return result", "response": "Returns a dictionary with sum of all scopes quotas."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list(self, request, *args, **kwargs):\n self.queryset = self.filter_queryset(self.get_queryset())\n\n page = self.paginate_queryset(self.queryset)\n if page is not None:\n return self.get_paginated_response(page)\n return response.Response(self.queryset)", "response": "Returns a list of events that are connected to the current user."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a count of events.", "response": "def count(self, request, *args, **kwargs):\n \"\"\"\n To get a count of events - run **GET** against */api/events/count/* as authenticated user.\n Endpoint support same filters as events list.\n\n Response example:\n\n .. code-block:: javascript\n\n {\"count\": 12321}\n \"\"\"\n\n self.queryset = self.filter_queryset(self.get_queryset())\n return response.Response({'count': self.queryset.count()}, status=status.HTTP_200_OK)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncounts the number of events in a specific time range.", "response": "def count_history(self, request, *args, **kwargs):\n \"\"\"\n To get a historical data of events amount - run **GET** against */api/events/count/history/*.\n Endpoint support same filters as events list.\n More about historical data - read at section *Historical data*.\n\n Response example:\n\n .. code-block:: javascript\n\n [\n {\n \"point\": 141111111111,\n \"object\": {\n \"count\": 558\n }\n }\n ]\n \"\"\"\n queryset = self.filter_queryset(self.get_queryset())\n\n mapped = {\n 'start': request.query_params.get('start'),\n 'end': request.query_params.get('end'),\n 'points_count': request.query_params.get('points_count'),\n 'point_list': request.query_params.getlist('point'),\n }\n serializer = core_serializers.HistorySerializer(data={k: v for k, v in mapped.items() if v})\n serializer.is_valid(raise_exception=True)\n\n timestamp_ranges = [{'end': point_date} for point_date in serializer.get_filter_data()]\n aggregated_count = queryset.aggregated_count(timestamp_ranges)\n\n return response.Response(\n [{'point': int(ac['end']), 'object': {'count': ac['count']}} for ac in aggregated_count],\n status=status.HTTP_200_OK)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef scope_types(self, request, *args, **kwargs):\n return response.Response(utils.get_scope_types_mapping().keys())", "response": "Returns a list of scope types acceptable by events filter."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list(self, request, *args, **kwargs):\n return super(AlertViewSet, self).list(request, *args, **kwargs)", "response": "Get a list of all alerts related to a given resource."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create(self, request, *args, **kwargs):\n return super(AlertViewSet, self).create(request, *args, **kwargs)", "response": "Create an alert for the current user."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncloses the current alert.", "response": "def close(self, request, *args, **kwargs):\n \"\"\"\n To close alert - run **POST** against */api/alerts//close/*. No data is required.\n Only users with staff privileges can close alerts.\n \"\"\"\n if not request.user.is_staff:\n raise PermissionDenied()\n alert = self.get_object()\n alert.close()\n\n return response.Response(status=status.HTTP_204_NO_CONTENT)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stats(self, request, *args, **kwargs):\n queryset = self.filter_queryset(self.get_queryset())\n alerts_severities_count = queryset.values('severity').annotate(count=Count('severity'))\n\n severity_names = dict(models.Alert.SeverityChoices.CHOICES)\n # For consistency with all other endpoint we need to return severity names in lower case.\n alerts_severities_count = {\n severity_names[asc['severity']].lower(): asc['count'] for asc in alerts_severities_count}\n for severity_name in severity_names.values():\n if severity_name.lower() not in alerts_severities_count:\n alerts_severities_count[severity_name.lower()] = 0\n\n return response.Response(alerts_severities_count, status=status.HTTP_200_OK)", "response": "This endpoint returns the count of alerts per severities per alert."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a new web hook.", "response": "def create(self, request, *args, **kwargs):\n \"\"\"\n To create new web hook issue **POST** against */api/hooks-web/* as an authenticated user.\n You should specify list of event_types or event_groups.\n\n Example of a request:\n\n .. code-block:: http\n\n POST /api/hooks-web/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"event_types\": [\"resource_start_succeeded\"],\n \"event_groups\": [\"users\"],\n \"destination_url\": \"http://example.com/\"\n }\n\n When hook is activated, **POST** request is issued against destination URL with the following data:\n\n .. code-block:: javascript\n\n {\n \"timestamp\": \"2015-07-14T12:12:56.000000\",\n \"message\": \"Customer ABC LLC has been updated.\",\n \"type\": \"customer_update_succeeded\",\n \"context\": {\n \"user_native_name\": \"Walter Lebrowski\",\n \"customer_contact_details\": \"\",\n \"user_username\": \"Walter\",\n \"user_uuid\": \"1c3323fc4ae44120b57ec40dea1be6e6\",\n \"customer_uuid\": \"4633bbbb0b3a4b91bffc0e18f853de85\",\n \"ip_address\": \"8.8.8.8\",\n \"user_full_name\": \"Walter Lebrowski\",\n \"customer_abbreviation\": \"ABC LLC\",\n \"customer_name\": \"ABC LLC\"\n },\n \"levelname\": \"INFO\"\n }\n\n Note that context depends on event type.\n \"\"\"\n return super(WebHookViewSet, self).create(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a new email hook.", "response": "def create(self, request, *args, **kwargs):\n \"\"\"\n To create new email hook issue **POST** against */api/hooks-email/* as an authenticated user.\n You should specify list of event_types or event_groups.\n\n Example of a request:\n\n .. code-block:: http\n\n POST /api/hooks-email/ HTTP/1.1\n Content-Type: application/json\n Accept: application/json\n Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4\n Host: example.com\n\n {\n \"event_types\": [\"openstack_instance_start_succeeded\"],\n \"event_groups\": [\"users\"],\n \"email\": \"test@example.com\"\n }\n\n You may temporarily disable hook without deleting it by issuing following **PATCH** request against hook URL:\n\n .. code-block:: javascript\n\n {\n \"is_active\": \"false\"\n }\n \"\"\"\n return super(EmailHookViewSet, self).create(request, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(self, request, *args, **kwargs):\n return super(PushHookViewSet, self).create(request, *args, **kwargs)", "response": "Create a new push hook."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nimporting this instrument s settings from the given file. Will throw ImportException if the file does not exist.", "response": "def import_from_file(self, index, filename):\n \"\"\"Import this instrument's settings from the given file. Will\n automatically add the instrument's synth and table to the song's\n synths and tables if needed.\n\n Note that this may invalidate existing instrument accessor objects.\n\n :param index: the index into which to import\n\n :param filename: the file from which to load\n\n :raises ImportException: if importing failed, usually because the song\n doesn't have enough synth or table slots left for the instrument's\n synth or table\n \"\"\"\n\n with open(filename, 'r') as fp:\n self._import_from_struct(index, json.load(fp))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload a Project from a. lsdsng file.", "response": "def load_lsdsng(filename):\n \"\"\"Load a Project from a ``.lsdsng`` file.\n\n :param filename: the name of the file from which to load\n :rtype: :py:class:`pylsdj.Project`\n \"\"\"\n\n # Load preamble data so that we know the name and version of the song\n with open(filename, 'rb') as fp:\n preamble_data = bread.parse(fp, spec.lsdsng_preamble)\n\n with open(filename, 'rb') as fp:\n # Skip the preamble this time around\n fp.seek(int(len(preamble_data) / 8))\n\n # Load compressed data into a block map and use BlockReader to\n # decompress it\n factory = BlockFactory()\n\n while True:\n block_data = bytearray(fp.read(blockutils.BLOCK_SIZE))\n\n if len(block_data) == 0:\n break\n\n block = factory.new_block()\n block.data = block_data\n\n remapped_blocks = filepack.renumber_block_keys(factory.blocks)\n\n reader = BlockReader()\n compressed_data = reader.read(remapped_blocks)\n\n # Now, decompress the raw data and use it and the preamble to construct\n # a Project\n raw_data = filepack.decompress(compressed_data)\n\n name = preamble_data.name\n version = preamble_data.version\n size_blks = int(math.ceil(\n float(len(compressed_data)) / blockutils.BLOCK_SIZE))\n\n return Project(name, version, size_blks, raw_data)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load_srm(filename):\n\n # .srm files are just decompressed projects without headers\n\n # In order to determine the file's size in compressed blocks, we have to\n # compress it first\n with open(filename, 'rb') as fp:\n raw_data = fp.read()\n\n compressed_data = filepack.compress(raw_data)\n\n factory = BlockFactory()\n writer = BlockWriter()\n writer.write(compressed_data, factory)\n\n size_in_blocks = len(factory.blocks)\n\n # We'll give the file a dummy name (\"SRMLOAD\") and version, since we know\n # neither\n name = \"SRMLOAD\"\n version = 0\n\n return Project(name, version, size_in_blocks, raw_data)", "response": "Load a Project from an. srm file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef song(self):\n if self._song is None:\n self._song = Song(self._song_data)\n\n return self._song", "response": "the song associated with the project"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef save(self, filename):\n with open(filename, 'wb') as fp:\n writer = BlockWriter()\n factory = BlockFactory()\n\n preamble_dummy_bytes = bytearray([0] * 9)\n preamble = bread.parse(\n preamble_dummy_bytes, spec.lsdsng_preamble)\n preamble.name = self.name\n preamble.version = self.version\n\n preamble_data = bread.write(preamble)\n raw_data = self.get_raw_data()\n compressed_data = filepack.compress(raw_data)\n\n writer.write(compressed_data, factory)\n\n fp.write(preamble_data)\n\n for key in sorted(factory.blocks.keys()):\n fp.write(bytearray(factory.blocks[key].data))", "response": "Save a project in. lsdsng format to the target file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef save_srm(self, filename):\n with open(filename, 'wb') as fp:\n raw_data = bread.write(self._song_data, spec.song)\n fp.write(raw_data)", "response": "Save a project in. srm format to the target file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef phase_type(self, value):\n '''compresses the waveform horizontally; one of\n ``\"normal\"``, ``\"resync\"``, ``\"resync2\"``'''\n self._params.phase_type = value\n self._overwrite_lock.disable()", "response": "compresses the waveform horizontally ; one of\n normal resync resync2"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef project_list(self):\n return [(i, self.projects[i]) for i in sorted(self.projects.keys())]", "response": "The list of project tuples that the the\n. sav file contains"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsave this object to a file.", "response": "def save(self, filename, callback=_noop_callback):\n \"\"\"Save this file.\n\n :param filename: the file to which to save the .sav file\n :type filename: str\n :param callback: a progress callback function\n :type callback: function\n \"\"\"\n with open(filename, 'wb') as fp:\n self._save(fp, callback)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef split(compressed_data, segment_size, block_factory):\n\n # Split compressed data into blocks\n segments = []\n\n current_segment_start = 0\n index = 0\n data_size = len(compressed_data)\n\n while index < data_size:\n current_byte = compressed_data[index]\n\n if index < data_size - 1:\n next_byte = compressed_data[index + 1]\n else:\n next_byte = None\n\n jump_size = 1\n\n if current_byte == RLE_BYTE:\n assert next_byte is not None, \"Expected a command to follow \" \\\n \"RLE byte\"\n if next_byte == RLE_BYTE:\n jump_size = 2\n else:\n jump_size = 3\n\n elif current_byte == SPECIAL_BYTE:\n assert next_byte is not None, \"Expected a command to follow \" \\\n \"special byte\"\n\n if next_byte == SPECIAL_BYTE:\n jump_size = 2\n elif next_byte == DEFAULT_INSTR_BYTE or \\\n next_byte == DEFAULT_WAVE_BYTE:\n jump_size = 3\n else:\n assert False, \"Encountered unexpected EOF or block \" \\\n \"switch while segmenting\"\n\n # Need two bytes for the jump or EOF\n if index - current_segment_start + jump_size > segment_size - 2:\n segments.append(compressed_data[\n current_segment_start:index])\n\n current_segment_start = index\n else:\n index += jump_size\n\n # Append the last segment, if any\n if current_segment_start != index:\n segments.append(compressed_data[\n current_segment_start:current_segment_start + index])\n\n # Make sure that no data was lost while segmenting\n total_segment_length = sum(map(len, segments))\n assert total_segment_length == len(compressed_data), \"Lost %d bytes of \" \\\n \"data while segmenting\" % (len(compressed_data) - total_segment_length)\n\n block_ids = []\n\n for segment in segments:\n block = block_factory.new_block()\n block_ids.append(block.id)\n\n for (i, segment) in enumerate(segments):\n block = block_factory.blocks[block_ids[i]]\n\n assert len(block.data) == 0, \"Encountered a block with \"\n \"pre-existing data while writing\"\n\n if i == len(segments) - 1:\n # Write EOF to the end of the segment\n add_eof(segment)\n else:\n # Write a pointer to the next segment\n add_block_switch(segment, block_ids[i + 1])\n\n # Pad segment with zeroes until it's large enough\n pad(segment, segment_size)\n\n block.data = segment\n\n return block_ids", "response": "Splits the compressed data into a list of blocks."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrenumber a block map s indices so that tehy match the blocks.", "response": "def renumber_block_keys(blocks):\n \"\"\"Renumber a block map's indices so that tehy match the blocks' block\n switch statements.\n\n :param blocks a block map to renumber\n :rtype: a renumbered copy of the block map\n \"\"\"\n\n # There is an implicit block switch to the 0th block at the start of the\n # file\n byte_switch_keys = [0]\n block_keys = list(blocks.keys())\n\n # Scan the blocks, recording every block switch statement\n for block in list(blocks.values()):\n i = 0\n while i < len(block.data) - 1:\n current_byte = block.data[i]\n next_byte = block.data[i + 1]\n\n if current_byte == RLE_BYTE:\n if next_byte == RLE_BYTE:\n i += 2\n else:\n i += 3\n elif current_byte == SPECIAL_BYTE:\n if next_byte in SPECIAL_DEFAULTS:\n i += 3\n elif next_byte == SPECIAL_BYTE:\n i += 2\n else:\n if next_byte != EOF_BYTE:\n byte_switch_keys.append(next_byte)\n\n break\n\n else:\n i += 1\n\n byte_switch_keys.sort()\n block_keys.sort()\n\n assert len(byte_switch_keys) == len(block_keys), (\n \"Number of blocks that are target of block switches (%d) \"\n % (len(byte_switch_keys)) +\n \"does not equal number of blocks in the song (%d)\"\n % (len(block_keys)) +\n \"; possible corruption\")\n\n if byte_switch_keys == block_keys:\n # No remapping necessary\n return blocks\n\n new_block_map = {}\n\n for block_key, byte_switch_key in zip(\n block_keys, byte_switch_keys):\n\n new_block_map[byte_switch_key] = blocks[block_key]\n\n return new_block_map"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef merge(blocks):\n\n current_block = blocks[sorted(blocks.keys())[0]]\n\n compressed_data = []\n eof = False\n\n while not eof:\n data_size_to_append = None\n next_block = None\n\n i = 0\n while i < len(current_block.data) - 1:\n current_byte = current_block.data[i]\n next_byte = current_block.data[i + 1]\n\n if current_byte == RLE_BYTE:\n if next_byte == RLE_BYTE:\n i += 2\n else:\n i += 3\n elif current_byte == SPECIAL_BYTE:\n if next_byte in SPECIAL_DEFAULTS:\n i += 3\n elif next_byte == SPECIAL_BYTE:\n i += 2\n else:\n data_size_to_append = i\n\n # hit end of file\n if next_byte == EOF_BYTE:\n eof = True\n else:\n next_block = blocks[next_byte]\n\n break\n else:\n i += 1\n\n assert data_size_to_append is not None, \"Ran off the end of a \"\\\n \"block without encountering a block switch or EOF\"\n\n compressed_data.extend(current_block.data[0:data_size_to_append])\n\n if not eof:\n assert next_block is not None, \"Switched blocks, but did \" \\\n \"not provide the next block to switch to\"\n\n current_block = next_block\n\n return compressed_data", "response": "This function takes a list of blocks and returns a list of compressed bytes that can be used to create a new block of compressed data."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding zeroes to a segment until it reaches a certain size.", "response": "def pad(segment, size):\n \"\"\"Add zeroes to a segment until it reaches a certain size.\n\n :param segment: the segment to pad\n :param size: the size to which to pad the segment\n \"\"\"\n for i in range(size - len(segment)):\n segment.append(0)\n\n assert len(segment) == size"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndecompress data that has been compressed by the filepack algorithm.", "response": "def decompress(compressed_data):\n \"\"\"Decompress data that has been compressed by the filepack algorithm.\n\n :param compressed_data: an array of compressed data bytes to decompress\n\n :rtype: an array of decompressed bytes\"\"\"\n raw_data = []\n\n index = 0\n\n while index < len(compressed_data):\n current = compressed_data[index]\n index += 1\n\n if current == RLE_BYTE:\n directive = compressed_data[index]\n index += 1\n\n if directive == RLE_BYTE:\n raw_data.append(RLE_BYTE)\n else:\n count = compressed_data[index]\n index += 1\n\n raw_data.extend([directive] * count)\n elif current == SPECIAL_BYTE:\n directive = compressed_data[index]\n index += 1\n\n if directive == SPECIAL_BYTE:\n raw_data.append(SPECIAL_BYTE)\n elif directive == DEFAULT_WAVE_BYTE:\n count = compressed_data[index]\n index += 1\n\n raw_data.extend(DEFAULT_WAVE * count)\n elif directive == DEFAULT_INSTR_BYTE:\n count = compressed_data[index]\n index += 1\n\n raw_data.extend(DEFAULT_INSTRUMENT_FILEPACK * count)\n elif directive == EOF_BYTE:\n assert False, (\"Unexpected EOF command encountered while \"\n \"decompressing\")\n else:\n assert False, \"Countered unexpected sequence 0x%02x 0x%02x\" % (\n current, directive)\n else:\n raw_data.append(current)\n\n return raw_data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef compress(raw_data):\n raw_data = bytearray(raw_data)\n compressed_data = []\n\n data_size = len(raw_data)\n\n index = 0\n next_bytes = [-1, -1, -1]\n\n def is_default_instrument(index):\n if index + len(DEFAULT_INSTRUMENT_FILEPACK) > len(raw_data):\n return False\n\n instr_bytes = raw_data[index:index + len(DEFAULT_INSTRUMENT_FILEPACK)]\n\n if instr_bytes[0] != 0xa8 or instr_bytes[1] != 0:\n return False\n\n return instr_bytes == DEFAULT_INSTRUMENT_FILEPACK\n\n def is_default_wave(index):\n return (index + len(DEFAULT_WAVE) <= len(raw_data) and\n raw_data[index:index + len(DEFAULT_WAVE)] == DEFAULT_WAVE)\n\n while index < data_size:\n current_byte = raw_data[index]\n\n for i in range(3):\n if index < data_size - (i + 1):\n next_bytes[i] = raw_data[index + (i + 1)]\n else:\n next_bytes[i] = -1\n\n if current_byte == RLE_BYTE:\n compressed_data.append(RLE_BYTE)\n compressed_data.append(RLE_BYTE)\n index += 1\n elif current_byte == SPECIAL_BYTE:\n compressed_data.append(SPECIAL_BYTE)\n compressed_data.append(SPECIAL_BYTE)\n index += 1\n elif is_default_instrument(index):\n counter = 1\n index += len(DEFAULT_INSTRUMENT_FILEPACK)\n\n while (is_default_instrument(index) and\n counter < 0x100):\n counter += 1\n index += len(DEFAULT_INSTRUMENT_FILEPACK)\n\n compressed_data.append(SPECIAL_BYTE)\n compressed_data.append(DEFAULT_INSTR_BYTE)\n compressed_data.append(counter)\n\n elif is_default_wave(index):\n counter = 1\n index += len(DEFAULT_WAVE)\n\n while is_default_wave(index) and counter < 0xff:\n counter += 1\n index += len(DEFAULT_WAVE)\n\n compressed_data.append(SPECIAL_BYTE)\n compressed_data.append(DEFAULT_WAVE_BYTE)\n compressed_data.append(counter)\n\n elif (current_byte == next_bytes[0] and\n next_bytes[0] == next_bytes[1] and\n next_bytes[1] == next_bytes[2]):\n # Do RLE compression\n\n compressed_data.append(RLE_BYTE)\n compressed_data.append(current_byte)\n counter = 0\n\n while (index < data_size and\n raw_data[index] == current_byte and\n counter < 0xff):\n index += 1\n counter += 1\n\n compressed_data.append(counter)\n else:\n compressed_data.append(current_byte)\n index += 1\n\n return compressed_data", "response": "Compress the raw bytes with the filepack algorithm."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a human - readable name without trailing zeroes.", "response": "def name_without_zeroes(name):\n \"\"\"\n Return a human-readable name without LSDJ's trailing zeroes.\n\n :param name: the name from which to strip zeroes\n :rtype: the name, without trailing zeroes\n \"\"\"\n first_zero = name.find(b'\\0')\n\n if first_zero == -1:\n return name\n else:\n return str(name[:first_zero])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef import_lsdinst(self, struct_data):\n self.name = struct_data['name']\n self.automate = struct_data['data']['automate']\n self.pan = struct_data['data']['pan']\n\n if self.table is not None:\n self.table.import_lsdinst(struct_data)", "response": "import from an lsdinst struct"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexporting this instrument s settings to a file.", "response": "def export_to_file(self, filename):\n \"\"\"Export this instrument's settings to a file.\n\n :param filename: the name of the file\n \"\"\"\n instr_json = self.export_struct()\n\n with open(filename, 'w') as fp:\n json.dump(instr_json, fp, indent=2)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite this sample to a WAV file.", "response": "def write_wav(self, filename):\n \"\"\"Write this sample to a WAV file.\n\n :param filename: the file to which to write\n \"\"\"\n\n wave_output = None\n try:\n wave_output = wave.open(filename, 'w')\n\n wave_output.setparams(WAVE_PARAMS)\n\n frames = bytearray([x << 4 for x in self.sample_data])\n\n wave_output.writeframes(frames)\n finally:\n if wave_output is not None:\n wave_output.close()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreading sample data from a WAV file.", "response": "def read_wav(self, filename):\n \"\"\"Read sample data for this sample from a WAV file.\n\n :param filename: the file from which to read\n \"\"\"\n wave_input = None\n\n try:\n wave_input = wave.open(filename, 'r')\n wave_frames = bytearray(\n wave_input.readframes(wave_input.getnframes()))\n\n self.sample_data = [x >> 4 for x in wave_frames]\n\n finally:\n if wave_input is not None:\n wave_input.close()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfinding the local ip address on the given device", "response": "def get_device_address(device):\n \"\"\" find the local ip address on the given device \"\"\"\n if device is None:\n return None\n command = ['ip', 'route', 'list', 'dev', device]\n ip_routes = subprocess.check_output(command).strip()\n for line in ip_routes.split('\\n'):\n seen = ''\n for a in line.split():\n if seen == 'src':\n return a\n seen = a\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_default_net_device():\n with open('/proc/net/route') as fh:\n for line in fh:\n iface, dest, _ = line.split(None, 2)\n if dest == '00000000':\n return iface\n return None", "response": "Find the default network device."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_missing_optional_args_with_value_none(args, optional_args):\n '''\n Adds key-value pairs to the passed dictionary, so that\n afterwards, the dictionary can be used without needing\n to check for KeyErrors.\n\n If the keys passed as a second argument are not present,\n they are added with None as a value.\n\n :args: The dictionary to be completed.\n :optional_args: The keys that need to be added, if\n they are not present.\n :return: The modified dictionary.\n '''\n\n for name in optional_args:\n if not name in args.keys():\n args[name] = None\n return args", "response": "Adds key - value pairs to the passed dictionary so that the keys passed as a second argument are not present."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check_presence_of_mandatory_args(args, mandatory_args):\n '''\n Checks whether all mandatory arguments are passed.\n\n This function aims at methods with many arguments\n which are passed as kwargs so that the order\n in which the are passed does not matter.\n\n :args: The dictionary passed as args.\n :mandatory_args: A list of keys that have to be\n present in the dictionary.\n :raise: :exc:`~ValueError`\n :returns: True, if all mandatory args are passed. If not,\n an exception is raised.\n\n '''\n missing_args = []\n for name in mandatory_args:\n if name not in args.keys():\n missing_args.append(name)\n if len(missing_args) > 0:\n raise ValueError('Missing mandatory arguments: '+', '.join(missing_args))\n else:\n return True", "response": "Checks whether all mandatory arguments are passed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef size(self, store_hashes=True):\n if self.modified:\n self.__cache_content(store_hashes)\n\n return len(self.cached_content)", "response": "Retrieves the size in bytes of this ZIP content."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmonkeys patch the django. db. migrations. writer. MIGRATION_TEMPLATE so that we can run the fixture at fixture_path.", "response": "def monkey_patch_migration_template(self, app, fixture_path):\n \"\"\"\n Monkey patch the django.db.migrations.writer.MIGRATION_TEMPLATE\n\n Monkey patching django.db.migrations.writer.MIGRATION_TEMPLATE means that we \n don't have to do any complex regex or reflection.\n\n It's hacky... but works atm.\n \"\"\"\n self._MIGRATION_TEMPLATE = writer.MIGRATION_TEMPLATE\n module_split = app.module.__name__.split('.')\n\n if len(module_split) == 1:\n module_import = \"import %s\\n\" % module_split[0]\n else:\n module_import = \"from %s import %s\\n\" % (\n '.'.join(module_split[:-1]),\n module_split[-1:][0],\n )\n\n writer.MIGRATION_TEMPLATE = writer.MIGRATION_TEMPLATE\\\n .replace(\n '%(imports)s',\n \"%(imports)s\" + \"\\nfrom django_migration_fixture import fixture\\n%s\" % module_import\n )\\\n .replace(\n '%(operations)s', \n \" migrations.RunPython(**fixture(%s, ['%s'])),\\n\" % (\n app.label,\n os.path.basename(fixture_path)\n ) + \"%(operations)s\\n\" \n )"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns True if a migration exists.", "response": "def migration_exists(self, app, fixture_path):\n \"\"\"\n Return true if it looks like a migration already exists.\n \"\"\"\n base_name = os.path.basename(fixture_path)\n # Loop through all migrations\n for migration_path in glob.glob(os.path.join(app.path, 'migrations', '*.py')):\n if base_name in open(migration_path).read():\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a data migration for app that uses fixture_path.", "response": "def create_migration(self, app, fixture_path):\n \"\"\"\n Create a data migration for app that uses fixture_path.\n \"\"\"\n self.monkey_patch_migration_template(app, fixture_path)\n\n out = StringIO()\n management.call_command('makemigrations', app.label, empty=True, stdout=out)\n\n self.restore_migration_template()\n\n self.stdout.write(out.getvalue())"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef instantiate_for_read_and_search(handle_server_url, reverselookup_username, reverselookup_password, **config):\n '''\n Initialize client with read access and with search function.\n\n :param handle_server_url: The URL of the Handle Server. May be None\n (then, the default 'https://hdl.handle.net' is used).\n :param reverselookup_username: The username to authenticate at the\n reverse lookup servlet.\n :param reverselookup_password: The password to authenticate at the\n reverse lookup servlet.\n :param \\**config: More key-value pairs may be passed that will be passed\n on to the constructor as config. Config options from the\n credentials object are overwritten by this.\n :return: An instance of the client.\n '''\n\n if handle_server_url is None and 'reverselookup_baseuri' not in config.keys():\n raise TypeError('You must specify either \"handle_server_url\" or \"reverselookup_baseuri\".' + \\\n ' Searching not possible without the URL of a search servlet.')\n\n inst = EUDATHandleClient(\n handle_server_url,\n reverselookup_username=reverselookup_username,\n reverselookup_password=reverselookup_password,\n **config\n )\n return inst", "response": "Instantiate an EUDATHandleClient object for reading access and search."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ninitializing client against an HSv8 instance with full read/write access. The method will throw an exception upon bad syntax or non-existing Handle. The existence or validity of the password in the handle is not checked at this moment. :param handle_server_url: The URL of the Handle System server. :param username: This must be a handle value reference in the format \"index:prefix/suffix\". :param password: This is the password stored as secret key in the actual Handle value the username points to. :param \\**config: More key-value pairs may be passed that will be passed on to the constructor as config. :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`: If the username handle is not found. :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError` :return: An instance of the client.", "response": "def instantiate_with_username_and_password(handle_server_url, username, password, **config):\n '''\n Initialize client against an HSv8 instance with full read/write access.\n\n The method will throw an exception upon bad syntax or non-existing\n Handle. The existence or validity of the password in the handle is\n not checked at this moment.\n\n :param handle_server_url: The URL of the Handle System server.\n :param username: This must be a handle value reference in the format\n \"index:prefix/suffix\".\n :param password: This is the password stored as secret key in the\n actual Handle value the username points to.\n :param \\**config: More key-value pairs may be passed that will be passed\n on to the constructor as config.\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`: If the username handle is not found.\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :return: An instance of the client.\n '''\n\n inst = EUDATHandleClient(handle_server_url, username=username, password=password, **config)\n return inst"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef instantiate_with_credentials(credentials, **config):\n '''\n Initialize the client against an HSv8 instance with full read/write\n access.\n\n :param credentials: A credentials object, see separate class\n PIDClientCredentials.\n :param \\**config: More key-value pairs may be passed that will be passed\n on to the constructor as config. Config options from the\n credentials object are overwritten by this.\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`: If the username handle is not found.\n :return: An instance of the client.\n '''\n key_value_pairs = credentials.get_all_args()\n\n if config is not None:\n key_value_pairs.update(**config) # passed config overrides json file\n\n inst = EUDATHandleClient(**key_value_pairs)\n return inst", "response": "Instantiate an EUDATHandleClient object with the specified credentials."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nretrieving a handle record as a complete nested dict.", "response": "def retrieve_handle_record_json(self, handle):\n '''\n Retrieve a handle record from the Handle server as a complete nested\n dict (including index, ttl, timestamp, ...) for later use.\n\n Note: For retrieving a simple dict with only the keys and values,\n please use :meth:`~b2handle.handleclient.EUDATHandleClient.retrieve_handle_record`.\n\n :param handle: The Handle whose record to retrieve.\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :return: The handle record as a nested dict. If the handle does not\n exist, returns None.\n '''\n LOGGER.debug('retrieve_handle_record_json...')\n\n utilhandle.check_handle_syntax(handle)\n response = self.__send_handle_get_request(handle)\n response_content = decoded_response(response)\n \n if hsresponses.handle_not_found(response):\n return None\n elif hsresponses.does_handle_exist(response):\n \n handlerecord_json = json.loads(response_content)\n if not handlerecord_json['handle'] == handle:\n raise GenericHandleError(\n operation='retrieving handle record',\n handle=handle,\n response=response,\n custom_message='The retrieve returned a different handle than was asked for.'\n )\n return handlerecord_json\n elif hsresponses.is_handle_empty(response):\n handlerecord_json = json.loads(response_content)\n return handlerecord_json\n else:\n raise GenericHandleError(\n operation='retrieving',\n handle=handle,\n response=response\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef retrieve_handle_record(self, handle, handlerecord_json=None):\n '''\n Retrieve a handle record from the Handle server as a dict. If there\n is several entries of the same type, only the first one is\n returned. Values of complex types (such as HS_ADMIN) are\n transformed to strings.\n\n :param handle: The handle whose record to retrieve.\n :param handlerecord_json: Optional. If the handlerecord has already\n been retrieved from the server, it can be reused.\n :return: A dict where the keys are keys from the Handle record (except\n for hidden entries) and every value is a string. The result will be\n None if the Handle does not exist.\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n '''\n LOGGER.debug('retrieve_handle_record...')\n\n handlerecord_json = self.__get_handle_record_if_necessary(handle, handlerecord_json)\n if handlerecord_json is None:\n return None # Instead of HandleNotFoundException!\n list_of_entries = handlerecord_json['values']\n\n record_as_dict = {}\n for entry in list_of_entries:\n key = entry['type']\n if not key in record_as_dict.keys():\n record_as_dict[key] = str(entry['data']['value'])\n return record_as_dict", "response": "Retrieve a handle record from the Handle server as a dict."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_value_from_handle(self, handle, key, handlerecord_json=None):\n '''\n Retrieve a single value from a single Handle. If several entries with\n this key exist, the methods returns the first one. If the handle\n does not exist, the method will raise a HandleNotFoundException.\n\n :param handle: The handle to take the value from.\n :param key: The key.\n :return: A string containing the value or None if the Handle record\n does not contain the key.\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n '''\n LOGGER.debug('get_value_from_handle...')\n\n handlerecord_json = self.__get_handle_record_if_necessary(handle, handlerecord_json)\n if handlerecord_json is None:\n raise HandleNotFoundException(handle=handle)\n list_of_entries = handlerecord_json['values']\n\n indices = []\n for i in xrange(len(list_of_entries)):\n if list_of_entries[i]['type'] == key:\n indices.append(i)\n\n if len(indices) == 0:\n return None\n else:\n if len(indices) > 1:\n LOGGER.debug('get_value_from_handle: The handle ' + handle + \\\n ' contains several entries of type \"' + key + \\\n '\". Only the first one is returned.')\n return list_of_entries[indices[0]]['data']['value']", "response": "Retrieves a single value from a single Handle."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef is_10320LOC_empty(self, handle, handlerecord_json=None):\n '''\n Checks if there is a 10320/LOC entry in the handle record.\n *Note:* In the unlikely case that there is a 10320/LOC entry, but it does\n not contain any locations, it is treated as if there was none.\n\n :param handle: The handle.\n :param handlerecord_json: Optional. The content of the response of a\n GET request for the handle as a dict. Avoids another GET request.\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :return: True if the record contains NO 10320/LOC entry; False if it\n does contain one.\n '''\n LOGGER.debug('is_10320LOC_empty...')\n\n handlerecord_json = self.__get_handle_record_if_necessary(handle, handlerecord_json)\n if handlerecord_json is None:\n raise HandleNotFoundException(handle=handle)\n list_of_entries = handlerecord_json['values']\n\n num_entries = 0\n num_URL = 0\n for entry in list_of_entries:\n if entry['type'] == '10320/LOC':\n num_entries += 1\n xmlroot = ET.fromstring(entry['data']['value'])\n list_of_locations = xmlroot.findall('location')\n for item in list_of_locations:\n if item.get('href') is not None:\n num_URL += 1\n if num_entries == 0:\n return True\n else:\n if num_URL == 0:\n return True\n else:\n return False", "response": "Checks if the 10320 / LOC entry in the handle record contains no 10320 / LOC entries."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef generate_and_register_handle(self, prefix, location, checksum=None, additional_URLs=None, **extratypes):\n '''\n Register a new Handle with a unique random name (random UUID).\n\n :param prefix: The prefix of the handle to be registered. The method\n will generate a suffix.\n :param location: The URL of the data entity to be referenced.\n :param checksum: Optional. The checksum string.\n :param extratypes: Optional. Additional key value pairs as dict.\n :param additional_URLs: Optional. A list of URLs (as strings) to be\n added to the handle record as 10320/LOC entry.\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n :return: The new handle name.\n '''\n\n LOGGER.debug('generate_and_register_handle...')\n\n handle = self.generate_PID_name(prefix)\n handle = self.register_handle(\n handle,\n location,\n checksum,\n additional_URLs,\n overwrite=True,\n **extratypes\n )\n return handle", "response": "Generate a unique name and register a new handle with a random UUID."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nmodifying the value of a key - value - pair in a handle record.", "response": "def modify_handle_value(self, handle, ttl=None, add_if_not_exist=True, **kvpairs):\n '''\n Modify entries (key-value-pairs) in a handle record. If the key\n does not exist yet, it is created.\n\n *Note:* We assume that a key exists only once. In case a key exists\n several time, an exception will be raised.\n\n *Note:* To modify 10320/LOC, please use :meth:`~b2handle.handleclient.EUDATHandleClient.add_additional_URL` or\n :meth:`~b2handle.handleclient.EUDATHandleClient.remove_additional_URL`.\n\n :param handle: Handle whose record is to be modified\n :param ttl: Optional. Integer value. If ttl should be set to a\n non-default value.\n :param all other args: The user can specify several key-value-pairs.\n These will be the handle value types and values that will be\n modified. The keys are the names or the handle value types (e.g.\n \"URL\"). The values are the new values to store in \"data\". If the\n key is 'HS_ADMIN', the new value needs to be of the form\n {'handle':'xyz', 'index':xyz}. The permissions will be set to the\n default permissions.\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n '''\n LOGGER.debug('modify_handle_value...')\n\n # Read handle record:\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is None:\n msg = 'Cannot modify unexisting handle'\n raise HandleNotFoundException(handle=handle, msg=msg)\n list_of_entries = handlerecord_json['values']\n\n # HS_ADMIN\n if 'HS_ADMIN' in kvpairs.keys() and not self.__modify_HS_ADMIN:\n msg = 'You may not modify HS_ADMIN'\n raise IllegalOperationException(\n msg=msg,\n operation='modifying HS_ADMIN',\n handle=handle\n )\n\n nothingchanged = True\n new_list_of_entries = []\n list_of_old_and_new_entries = list_of_entries[:]\n keys = kvpairs.keys()\n for key, newval in kvpairs.items():\n # Change existing entry:\n changed = False\n for i in xrange(len(list_of_entries)):\n if list_of_entries[i]['type'] == key:\n if not changed:\n list_of_entries[i]['data'] = newval\n list_of_entries[i].pop('timestamp') # will be ignored anyway\n if key == 'HS_ADMIN':\n newval['permissions'] = self.__HS_ADMIN_permissions\n list_of_entries[i].pop('timestamp') # will be ignored anyway\n list_of_entries[i]['data'] = {\n 'format':'admin',\n 'value':newval\n }\n LOGGER.info('Modified' + \\\n ' \"HS_ADMIN\" of handle ' + handle)\n changed = True\n nothingchanged = False\n new_list_of_entries.append(list_of_entries[i])\n list_of_old_and_new_entries.append(list_of_entries[i])\n else:\n msg = 'There is several entries of type \"' + key + '\".' + \\\n ' This can lead to unexpected behaviour.' + \\\n ' Please clean up before modifying the record.'\n raise BrokenHandleRecordException(handle=handle, msg=msg)\n\n # If the entry doesn't exist yet, add it:\n if not changed:\n if add_if_not_exist:\n LOGGER.debug('modify_handle_value: Adding entry \"' + key + '\"' + \\\n ' to handle ' + handle)\n index = self.__make_another_index(list_of_old_and_new_entries)\n entry_to_add = self.__create_entry(key, newval, index, ttl)\n new_list_of_entries.append(entry_to_add)\n list_of_old_and_new_entries.append(entry_to_add)\n changed = True\n nothingchanged = False\n\n # Add the indices\n indices = []\n for i in xrange(len(new_list_of_entries)):\n indices.append(new_list_of_entries[i]['index'])\n\n # append to the old record:\n if nothingchanged:\n LOGGER.debug('modify_handle_value: There was no entries ' + \\\n str(kvpairs.keys()) + ' to be modified (handle ' + handle + ').' + \\\n ' To add them, set add_if_not_exist = True')\n else:\n op = 'modifying handle values'\n resp, put_payload = self.__send_handle_put_request(\n handle,\n new_list_of_entries,\n indices=indices,\n overwrite=True,\n op=op)\n if hsresponses.handle_success(resp):\n LOGGER.info('Handle modified: ' + handle)\n else:\n msg = 'Values: ' + str(kvpairs)\n raise GenericHandleError(\n operation=op,\n handle=handle,\n response=resp,\n msg=msg,\n payload=put_payload\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_handle_value(self, handle, key):\n '''\n Delete a key-value pair from a handle record. If the key exists more\n than once, all key-value pairs with this key are deleted.\n\n :param handle: Handle from whose record the entry should be deleted.\n :param key: Key to be deleted. Also accepts a list of keys.\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n '''\n LOGGER.debug('delete_handle_value...')\n\n # read handle record:\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is None:\n msg = 'Cannot modify unexisting handle'\n raise HandleNotFoundException(handle=handle, msg=msg)\n list_of_entries = handlerecord_json['values']\n\n\n # find indices to delete:\n keys = None\n indices = []\n if type(key) != type([]):\n keys = [key]\n else:\n keys = key\n keys_done = []\n for key in keys:\n\n # filter HS_ADMIN\n if key == 'HS_ADMIN':\n op = 'deleting \"HS_ADMIN\"'\n raise IllegalOperationException(operation=op, handle=handle)\n\n if key not in keys_done:\n indices_onekey = self.get_handlerecord_indices_for_key(key, list_of_entries)\n indices = indices + indices_onekey\n keys_done.append(key)\n\n # Important: If key not found, do not continue, as deleting without indices would delete the entire handle!!\n if not len(indices) > 0:\n LOGGER.debug('delete_handle_value: No values for key(s) ' + str(keys))\n return None\n else:\n\n # delete and process response:\n op = 'deleting \"' + str(keys) + '\"'\n resp = self.__send_handle_delete_request(handle, indices=indices, op=op)\n if hsresponses.handle_success(resp):\n LOGGER.debug(\"delete_handle_value: Deleted handle values \" + str(keys) + \"of handle \" + handle)\n elif hsresponses.values_not_found(resp):\n pass\n else:\n raise GenericHandleError(\n operation=op,\n handle=handle,\n response=resp\n )", "response": "Delete a key - value pair from a handle record."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndelete the handle and its handle record.", "response": "def delete_handle(self, handle, *other):\n '''Delete the handle and its handle record. If the Handle is not found, an Exception is raised.\n\n :param handle: Handle to be deleted.\n :param other: Deprecated. This only exists to catch wrong method usage\n by users who are used to delete handle VALUES with the method.\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n '''\n\n LOGGER.debug('delete_handle...')\n\n utilhandle.check_handle_syntax(handle)\n\n # Safety check. In old epic client, the method could be used for\n # deleting handle values (not entire handle) by specifying more\n # parameters.\n if len(other) > 0:\n message = 'You specified more than one argument. If you wanted' + \\\n ' to delete just some values from a handle, please use the' + \\\n ' new method \"delete_handle_value()\".'\n raise TypeError(message)\n\n op = 'deleting handle'\n resp = self.__send_handle_delete_request(handle, op=op)\n if hsresponses.handle_success(resp):\n LOGGER.info('Handle ' + handle + ' deleted.')\n elif hsresponses.handle_not_found(resp):\n msg = ('delete_handle: Handle ' + handle + ' did not exist, '\n 'so it could not be deleted.')\n LOGGER.debug(msg)\n raise HandleNotFoundException(msg=msg, handle=handle, response=resp)\n else:\n raise GenericHandleError(op=op, handle=handle, response=resp)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef exchange_additional_URL(self, handle, old, new):\n '''\n Exchange an URL in the 10320/LOC entry against another, keeping the same id\n and other attributes.\n\n :param handle: The handle to modify.\n :param old: The URL to replace.\n :param new: The URL to set as new URL.\n '''\n LOGGER.debug('exchange_additional_URL...')\n\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is None:\n msg = 'Cannot exchange URLs in unexisting handle'\n raise HandleNotFoundException(\n handle=handle,\n msg=msg\n )\n list_of_entries = handlerecord_json['values']\n\n if not self.is_URL_contained_in_10320LOC(handle, old, handlerecord_json):\n LOGGER.debug('exchange_additional_URL: No URLs exchanged, as the url was not in the record.')\n else:\n self.__exchange_URL_in_13020loc(old, new, list_of_entries, handle)\n\n op = 'exchanging URLs'\n resp, put_payload = self.__send_handle_put_request(\n handle,\n list_of_entries,\n overwrite=True,\n op=op\n )\n # TODO FIXME (one day): Implement overwriting by index (less risky)\n if hsresponses.handle_success(resp):\n pass\n else:\n msg = 'Could not exchange URL ' + str(old) + ' against ' + str(new)\n raise GenericHandleError(\n operation=op,\n handle=handle,\n reponse=resp,\n msg=msg,\n payload=put_payload\n )", "response": "Exchange an URL in the 10320 / LOC entry against another."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a URL entry to the handle record s 10320 / LOC entry.", "response": "def add_additional_URL(self, handle, *urls, **attributes):\n '''\n Add a URL entry to the handle record's 10320/LOC entry. If 10320/LOC\n does not exist yet, it is created. If the 10320/LOC entry already\n contains the URL, it is not added a second time.\n\n :param handle: The handle to add the URL to.\n :param urls: The URL(s) to be added. Several URLs may be specified.\n :param attributes: Optional. Additional key-value pairs to set as\n attributes to the elements, e.g. weight, http_role or\n custom attributes. Note: If the URL already exists but the\n attributes are different, they are updated!\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n '''\n LOGGER.debug('add_additional_URL...')\n\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is None:\n msg = 'Cannot add URLS to unexisting handle!'\n raise HandleNotFoundException(handle=handle, msg=msg)\n list_of_entries = handlerecord_json['values']\n\n is_new = False\n for url in urls:\n if not self.is_URL_contained_in_10320LOC(handle, url, handlerecord_json):\n is_new = True\n\n if not is_new:\n LOGGER.debug(\"add_additional_URL: No new URL to be added (so no URL is added at all).\")\n else:\n\n for url in urls:\n self.__add_URL_to_10320LOC(url, list_of_entries, handle)\n\n op = 'adding URLs'\n resp, put_payload = self.__send_handle_put_request(handle, list_of_entries, overwrite=True, op=op)\n # TODO FIXME (one day) Overwrite by index.\n\n if hsresponses.handle_success(resp):\n pass\n else:\n msg = 'Could not add URLs ' + str(urls)\n raise GenericHandleError(\n operation=op,\n handle=handle,\n reponse=resp,\n msg=msg,\n payload=put_payload\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nremove URLs from the 10320LOC entry.", "response": "def remove_additional_URL(self, handle, *urls):\n '''\n Remove a URL from the handle record's 10320/LOC entry.\n\n :param handle: The handle to modify.\n :param urls: The URL(s) to be removed. Several URLs may be specified.\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n '''\n\n LOGGER.debug('remove_additional_URL...')\n\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is None:\n msg = 'Cannot remove URLs from unexisting handle'\n raise HandleNotFoundException(handle=handle, msg=msg)\n list_of_entries = handlerecord_json['values']\n\n for url in urls:\n self.__remove_URL_from_10320LOC(url, list_of_entries, handle)\n\n op = 'removing URLs'\n resp, put_payload = self.__send_handle_put_request(\n handle,\n list_of_entries,\n overwrite=True,\n op=op\n )\n # TODO FIXME (one day): Implement overwriting by index (less risky),\n # once HS have fixed the issue with the indices.\n if hsresponses.handle_success(resp):\n pass\n else:\n op = 'removing \"' + str(urls) + '\"'\n msg = 'Could not remove URLs ' + str(urls)\n raise GenericHandleError(\n operation=op,\n handle=handle,\n reponse=resp,\n msg=msg,\n payload=put_payload\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef register_handle(self, handle, location, checksum=None, additional_URLs=None, overwrite=False, **extratypes):\n '''\n Registers a new Handle with given name. If the handle already exists\n and overwrite is not set to True, the method will throw an\n exception.\n\n :param handle: The full name of the handle to be registered (prefix\n and suffix)\n :param location: The URL of the data entity to be referenced\n :param checksum: Optional. The checksum string.\n :param extratypes: Optional. Additional key value pairs.\n :param additional_URLs: Optional. A list of URLs (as strings) to be\n added to the handle record as 10320/LOC entry.\n :param overwrite: Optional. If set to True, an existing handle record\n will be overwritten. Defaults to False.\n :raises: :exc:`~b2handle.handleexceptions.HandleAlreadyExistsException` Only if overwrite is not set or\n set to False.\n :raises: :exc:`~b2handle.handleexceptions.HandleAuthenticationError`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :return: The handle name.\n '''\n LOGGER.debug('register_handle...')\n\n # If already exists and can't be overwritten:\n if overwrite == False:\n handlerecord_json = self.retrieve_handle_record_json(handle)\n if handlerecord_json is not None:\n msg = 'Could not register handle'\n LOGGER.error(msg + ', as it already exists.')\n raise HandleAlreadyExistsException(handle=handle, msg=msg)\n\n # Create admin entry\n list_of_entries = []\n adminentry = self.__create_admin_entry(\n self.__handleowner,\n self.__HS_ADMIN_permissions,\n self.__make_another_index(list_of_entries, hs_admin=True),\n handle\n )\n list_of_entries.append(adminentry)\n\n # Create other entries\n entry_URL = self.__create_entry(\n 'URL',\n location,\n self.__make_another_index(list_of_entries, url=True)\n )\n list_of_entries.append(entry_URL)\n if checksum is not None:\n entryChecksum = self.__create_entry(\n 'CHECKSUM',\n checksum,\n self.__make_another_index(list_of_entries)\n )\n list_of_entries.append(entryChecksum)\n if extratypes is not None:\n for key, value in extratypes.items():\n entry = self.__create_entry(\n key,\n value,\n self.__make_another_index(list_of_entries)\n )\n list_of_entries.append(entry)\n if additional_URLs is not None and len(additional_URLs) > 0:\n for url in additional_URLs:\n self.__add_URL_to_10320LOC(url, list_of_entries, handle)\n\n # Create record itself and put to server\n op = 'registering handle'\n resp, put_payload = self.__send_handle_put_request(\n handle,\n list_of_entries,\n overwrite=overwrite,\n op=op\n )\n resp_content = decoded_response(resp)\n if hsresponses.was_handle_created(resp) or hsresponses.handle_success(resp):\n LOGGER.info(\"Handle registered: \" + handle)\n return json.loads(resp_content)['handle']\n elif hsresponses.is_temporary_redirect(resp):\n oldurl = resp.url\n newurl = resp.headers['location']\n raise GenericHandleError(\n operation=op,\n handle=handle,\n response=resp,\n payload=put_payload,\n msg='Temporary redirect from ' + oldurl + ' to ' + newurl + '.'\n )\n elif hsresponses.handle_not_found(resp):\n raise GenericHandleError(\n operation=op,\n handle=handle,\n response=resp,\n payload=put_payload,\n msg='Could not create handle. Possibly you used HTTP instead of HTTPS?'\n )\n else:\n raise GenericHandleError(\n operation=op,\n handle=handle,\n reponse=resp,\n payload=put_payload\n )", "response": "Registers a new handle with given name."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsearching for all handles containing the specified key with the specified value.", "response": "def search_handle(self, URL=None, prefix=None, **key_value_pairs):\n '''\n Search for handles containing the specified key with the specified\n value. The search terms are passed on to the reverse lookup servlet\n as-is. The servlet is supposed to be case-insensitive, but if it\n isn't, the wrong case will cause a :exc:`~b2handle.handleexceptions.ReverseLookupException`.\n\n *Note:* If allowed search keys are configured, only these are used. If\n no allowed search keys are specified, all key-value pairs are\n passed on to the reverse lookup servlet, possibly causing a\n :exc:`~b2handle.handleexceptions.ReverseLookupException`.\n\n Example calls:\n\n .. code:: python\n\n list_of_handles = search_handle('http://www.foo.com')\n list_of_handles = search_handle('http://www.foo.com', CHECKSUM=99999)\n list_of_handles = search_handle(URL='http://www.foo.com', CHECKSUM=99999)\n\n\n :param URL: Optional. The URL to search for (reverse lookup). [This is\n NOT the URL of the search servlet!]\n :param prefix: Optional. The Handle prefix to which the search should\n be limited to. If unspecified, the method will search across all\n prefixes present at the server given to the constructor.\n :param key_value_pairs: Optional. Several search fields and values can\n be specified as key-value-pairs,\n e.g. CHECKSUM=123456, URL=www.foo.com\n :raise: :exc:`~b2handle.handleexceptions.ReverseLookupException`: If a search field is specified that\n cannot be used, or if something else goes wrong.\n :return: A list of all Handles (strings) that bear the given key with\n given value of given prefix or server. The list may be empty and\n may also contain more than one element.\n '''\n LOGGER.debug('search_handle...')\n\n list_of_handles = self.__searcher.search_handle(URL=URL, prefix=prefix, **key_value_pairs)\n\n return list_of_handles"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate a unique random name for the notwkalot.", "response": "def generate_PID_name(self, prefix=None):\n '''\n Generate a unique random Handle name (random UUID). The Handle is not\n registered. If a prefix is specified, the PID name has the syntax\n /, otherwise it just returns the generated\n random name (suffix for the Handle).\n\n :param prefix: Optional. The prefix to be used for the Handle name.\n :return: The handle name in the form / or\n .\n '''\n\n LOGGER.debug('generate_PID_name...')\n\n randomuuid = uuid.uuid4()\n if prefix is not None:\n return prefix + '/' + str(randomuuid)\n else:\n return str(randomuuid)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_handlerecord_indices_for_key(self, key, list_of_entries):\n '''\n Finds the Handle entry indices of all entries that have a specific\n type.\n\n *Important:* It finds the Handle System indices! These are not\n the python indices of the list, so they can not be used for\n iteration.\n\n :param key: The key (Handle Record type)\n :param list_of_entries: A list of the existing entries in which to find\n the indices.\n :return: A list of strings, the indices of the entries of type \"key\" in\n the given handle record.\n '''\n\n LOGGER.debug('get_handlerecord_indices_for_key...')\n\n indices = []\n for entry in list_of_entries:\n if entry['type'] == key:\n indices.append(entry['index'])\n return indices", "response": "This function returns the list of strings that are the indices of the entries that have a specific handle record type."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __send_handle_delete_request(self, handle, indices=None, op=None):\n '''\n Send a HTTP DELETE request to the handle server to delete either an\n entire handle or to some specified values from a handle record,\n using the requests module.\n\n :param handle: The handle.\n :param indices: Optional. A list of indices to delete. Defaults to\n None (i.e. the entire handle is deleted.). The list can contain\n integers or strings.\n :return: The server's response.\n '''\n\n resp = self.__handlesystemconnector.send_handle_delete_request(\n handle=handle,\n indices=indices,\n op=op)\n return resp", "response": "Sends a HTTP DELETE request to the handle server."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsends a HTTP PUT request to the handle server.", "response": "def __send_handle_put_request(self, handle, list_of_entries, indices=None, overwrite=False, op=None):\n '''\n Send a HTTP PUT request to the handle server to write either an entire\n handle or to some specified values to an handle record, using the\n requests module.\n\n :param handle: The handle.\n :param list_of_entries: A list of handle record entries to be written,\n in the format [{\"index\":xyz, \"type\":\"xyz\", \"data\":\"xyz\"}] or similar.\n :param indices: Optional. A list of indices to modify. Defaults\n to None (i.e. the entire handle is updated.). The list can\n contain integers or strings.\n :param overwrite: Optional. Whether the handle should be overwritten\n if it exists already.\n :return: The server's response.\n '''\n\n resp, payload = self.__handlesystemconnector.send_handle_put_request(\n handle=handle,\n list_of_entries=list_of_entries,\n indices=indices,\n overwrite=overwrite,\n op=op\n )\n return resp, payload"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsending a HTTP GET request to the handle server to read either an entire handle or to some specified values from a handle record.", "response": "def __send_handle_get_request(self, handle, indices=None):\n '''\n Send a HTTP GET request to the handle server to read either an entire\n handle or to some specified values from a handle record, using the\n requests module.\n\n :param handle: The handle.\n :param indices: Optional. A list of indices to delete. Defaults to\n None (i.e. the entire handle is deleted.). The list can contain\n integers or strings.\n :return: The server's response.\n '''\n\n resp = self.__handlesystemconnector.send_handle_get_request(handle, indices)\n return resp"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the handle record if it is None or the handle record is not the same as the specified handle.", "response": "def __get_handle_record_if_necessary(self, handle, handlerecord_json):\n '''\n Returns the handle record if it is None or if its handle is not the\n same as the specified handle.\n\n '''\n if handlerecord_json is None:\n handlerecord_json = self.retrieve_handle_record_json(handle)\n else:\n if handle != handlerecord_json['handle']:\n handlerecord_json = self.retrieve_handle_record_json(handle)\n return handlerecord_json"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfinding an index not yet used in the handle record and not reserved for any (other) special type. :param: list_of_entries: List of all entries to find which indices are used already. :param url: If True, an index for an URL entry is returned (1, unless it is already in use). :param hs_admin: If True, an index for HS_ADMIN is returned (100 or one of the following). :return: An integer.", "response": "def __make_another_index(self, list_of_entries, url=False, hs_admin=False):\n '''\n Find an index not yet used in the handle record and not reserved for\n any (other) special type.\n\n :param: list_of_entries: List of all entries to find which indices are\n used already.\n :param url: If True, an index for an URL entry is returned (1, unless\n it is already in use).\n :param hs_admin: If True, an index for HS_ADMIN is returned (100 or one\n of the following).\n :return: An integer.\n '''\n\n start = 2\n\n # reserved indices:\n reserved_for_url = set([1])\n reserved_for_admin = set(range(100, 200))\n prohibited_indices = reserved_for_url | reserved_for_admin\n\n if url:\n prohibited_indices = prohibited_indices - reserved_for_url\n start = 1\n elif hs_admin:\n prohibited_indices = prohibited_indices - reserved_for_admin\n start = 100\n\n # existing indices\n existing_indices = set()\n if list_of_entries is not None:\n for entry in list_of_entries:\n existing_indices.add(int(entry['index']))\n\n # find new index:\n all_prohibited_indices = existing_indices | prohibited_indices\n searchmax = max(start, max(all_prohibited_indices)) + 2\n for index in xrange(start, searchmax):\n if index not in all_prohibited_indices:\n return index"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __create_entry(self, entrytype, data, index, ttl=None):\n '''\n Create an entry of any type except HS_ADMIN.\n\n :param entrytype: THe type of entry to create, e.g. 'URL' or\n 'checksum' or ... Note: For entries of type 'HS_ADMIN', please\n use __create_admin_entry(). For type '10320/LOC', please use\n 'add_additional_URL()'\n :param data: The actual value for the entry. Can be a simple string,\n e.g. \"example\", or a dict {\"format\":\"string\", \"value\":\"example\"}.\n :param index: The integer to be used as index.\n :param ttl: Optional. If not set, the library's default is set. If\n there is no default, it is not set by this library, so Handle\n System sets it.\n :return: The entry as a dict.\n '''\n\n if entrytype == 'HS_ADMIN':\n op = 'creating HS_ADMIN entry'\n msg = 'This method can not create HS_ADMIN entries.'\n raise IllegalOperationException(operation=op, msg=msg)\n\n entry = {'index':index, 'type':entrytype, 'data':data}\n\n if ttl is not None:\n entry['ttl'] = ttl\n\n return entry", "response": "Create an entry of any type except HS_ADMIN."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating an admin entry.", "response": "def __create_admin_entry(self, handleowner, permissions, index, handle, ttl=None):\n '''\n Create an entry of type \"HS_ADMIN\".\n\n :param username: The username, i.e. a handle with an index\n (index:prefix/suffix). The value referenced by the index contains\n authentcation information, e.g. a hidden entry containing a key.\n :param permissions: The permissions as a string of zeros and ones,\n e.g. '0111011101011'. If not all twelve bits are set, the remaining\n ones are set to zero.\n :param index: The integer to be used as index of this admin entry (not\n of the username!). Should be 1xx.\n :param ttl: Optional. If not set, the library's default is set. If\n there is no default, it is not set by this library, so Handle\n System sets it.\n :return: The entry as a dict.\n '''\n # If the handle owner is specified, use it. Otherwise, use 200:0.NA/prefix\n # With the prefix taken from the handle that is being created, not from anywhere else.\n if handleowner is None:\n adminindex = '200'\n prefix = handle.split('/')[0]\n adminhandle = '0.NA/' + prefix\n else:\n adminindex, adminhandle = utilhandle.remove_index_from_handle(handleowner)\n\n data = {\n 'value':{\n 'index':adminindex,\n 'handle':adminhandle,\n 'permissions':permissions\n },\n 'format':'admin'\n }\n\n entry = {'index':index, 'type':'HS_ADMIN', 'data':data}\n if ttl is not None:\n entry['ttl'] = ttl\n\n return entry"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __get_python_indices_for_key(self, key, list_of_entries):\n '''\n Finds the indices of all entries that have a specific type. Important:\n This method finds the python indices of the list of entries! These\n are not the Handle System index values!\n\n :param key: The key (Handle Record type)\n :param list_of_entries: A list of the existing entries in which to find\n the indices.\n :return: A list of integers, the indices of the entries of type \"key\"\n in the given list.\n '''\n indices = []\n for i in xrange(len(list_of_entries)):\n if list_of_entries[i]['type'] == key:\n indices.append(i)\n return indices", "response": "This method returns the python indices of all entries that have a specific type."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nexchanging every occurrence of oldurl against newurl in a 10320 / LOC entry.", "response": "def __exchange_URL_in_13020loc(self, oldurl, newurl, list_of_entries, handle):\n '''\n Exchange every occurrence of oldurl against newurl in a 10320/LOC entry.\n This does not change the ids or other xml attributes of the\n element.\n\n :param oldurl: The URL that will be overwritten.\n :param newurl: The URL to write into the entry.\n :param list_of_entries: A list of the existing entries (to find and\n remove the correct one).\n :param handle: Only for the exception message.\n :raise: GenericHandleError: If several 10320/LOC exist (unlikely).\n '''\n\n # Find existing 10320/LOC entries\n python_indices = self.__get_python_indices_for_key(\n '10320/LOC',\n list_of_entries\n )\n\n num_exchanged = 0\n if len(python_indices) > 0:\n\n if len(python_indices) > 1:\n msg = str(len(python_indices)) + ' entries of type \"10320/LOC\".'\n raise BrokenHandleRecordException(handle=handle, msg=msg)\n\n for index in python_indices:\n entry = list_of_entries.pop(index)\n xmlroot = ET.fromstring(entry['data']['value'])\n all_URL_elements = xmlroot.findall('location')\n for element in all_URL_elements:\n if element.get('href') == oldurl:\n LOGGER.debug('__exchange_URL_in_13020loc: Exchanging URL ' + oldurl + ' from 10320/LOC.')\n num_exchanged += 1\n element.set('href', newurl)\n entry['data']['value'] = ET.tostring(xmlroot, encoding=encoding_value)\n list_of_entries.append(entry)\n\n if num_exchanged == 0:\n LOGGER.debug('__exchange_URL_in_13020loc: No URLs exchanged.')\n else:\n message = '__exchange_URL_in_13020loc: The URL \"' + oldurl + '\" was exchanged ' + str(num_exchanged) + \\\n ' times against the new url \"' + newurl + '\" in 10320/LOC.'\n message = message.replace('1 times', 'once')\n LOGGER.debug(message)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __remove_URL_from_10320LOC(self, url, list_of_entries, handle):\n '''\n Remove an URL from the handle record's \"10320/LOC\" entry.\n If it exists several times in the entry, all occurences are removed.\n If the URL is not present, nothing happens.\n If after removing, there is no more URLs in the entry, the entry is\n removed.\n\n :param url: The URL to be removed.\n :param list_of_entries: A list of the existing entries (to find and\n remove the correct one).\n :param handle: Only for the exception message.\n :raise: GenericHandleError: If several 10320/LOC exist (unlikely).\n '''\n\n # Find existing 10320/LOC entries\n python_indices = self.__get_python_indices_for_key(\n '10320/LOC',\n list_of_entries\n )\n\n num_removed = 0\n if len(python_indices) > 0:\n\n if len(python_indices) > 1:\n msg = str(len(python_indices)) + ' entries of type \"10320/LOC\".'\n raise BrokenHandleRecordException(handle=handle, msg=msg)\n\n for index in python_indices:\n entry = list_of_entries.pop(index)\n xmlroot = ET.fromstring(entry['data']['value'])\n all_URL_elements = xmlroot.findall('location')\n for element in all_URL_elements:\n if element.get('href') == url:\n LOGGER.debug('__remove_URL_from_10320LOC: Removing URL ' + url + '.')\n num_removed += 1\n xmlroot.remove(element)\n remaining_URL_elements = xmlroot.findall('location')\n if len(remaining_URL_elements) == 0:\n LOGGER.debug(\"__remove_URL_from_10320LOC: All URLs removed.\")\n # TODO FIXME: If we start adapting the Handle Record by\n # index (instead of overwriting the entire one), be careful\n # to delete the ones that became empty!\n else:\n entry['data']['value'] = ET.tostring(xmlroot, encoding=encoding_value)\n LOGGER.debug('__remove_URL_from_10320LOC: ' + str(len(remaining_URL_elements)) + ' URLs' + \\\n ' left after removal operation.')\n list_of_entries.append(entry)\n if num_removed == 0:\n LOGGER.debug('__remove_URL_from_10320LOC: No URLs removed.')\n else:\n message = '__remove_URL_from_10320LOC: The URL \"' + url + '\" was removed '\\\n + str(num_removed) + ' times.'\n message = message.replace('1 times', 'once')\n LOGGER.debug(message)", "response": "Remove an URL from the handle record s 10320 / LOC entry."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a url to the handle record s 10320 / LOC entry.", "response": "def __add_URL_to_10320LOC(self, url, list_of_entries, handle=None, weight=None, http_role=None, **kvpairs):\n '''\n Add a url to the handle record's \"10320/LOC\" entry.\n If no 10320/LOC entry exists, a new one is created (using the\n default \"chooseby\" attribute, if configured).\n If the URL is already present, it is not added again, but\n the attributes (e.g. weight) are updated/added.\n If the existing 10320/LOC entry is mal-formed, an exception will be\n thrown (xml.etree.ElementTree.ParseError)\n Note: In the unlikely case that several \"10320/LOC\" entries exist,\n an exception is raised.\n\n :param url: The URL to be added.\n :param list_of_entries: A list of the existing entries (to find and\n adapt the correct one).\n :param weight: Optional. The weight to be set (integer between 0 and\n 1). If None, no weight attribute is set. If the value is outside\n the accepted range, it is set to 1.\n :param http_role: Optional. The http_role to be set. This accepts any\n string. Currently, Handle System can process 'conneg'. In future,\n it may be able to process 'no_conneg' and 'browser'.\n :param handle: Optional. Only for the exception message.\n :param all others: Optional. All other key-value pairs will be set to\n the element. Any value is accepted and transformed to string.\n :raise: GenericHandleError: If several 10320/LOC exist (unlikely).\n\n '''\n\n # Find existing 10320/LOC entry or create new\n indices = self.__get_python_indices_for_key('10320/LOC', list_of_entries)\n makenew = False\n entry = None\n if len(indices) == 0:\n index = self.__make_another_index(list_of_entries)\n entry = self.__create_entry('10320/LOC', 'add_later', index)\n makenew = True\n else:\n if len(indices) > 1:\n msg = 'There is ' + str(len(indices)) + ' 10320/LOC entries.'\n raise BrokenHandleRecordException(handle=handle, msg=msg)\n ind = indices[0]\n entry = list_of_entries.pop(ind)\n\n # Get xml data or make new:\n xmlroot = None\n if makenew:\n xmlroot = ET.Element('locations')\n if self.__10320LOC_chooseby is not None:\n xmlroot.set('chooseby', self.__10320LOC_chooseby)\n else:\n try:\n xmlroot = ET.fromstring(entry['data']['value'])\n except TypeError:\n xmlroot = ET.fromstring(entry['data'])\n LOGGER.debug(\"__add_URL_to_10320LOC: xmlroot is (1) \" + ET.tostring(xmlroot, encoding=encoding_value))\n\n # Check if URL already there...\n location_element = None\n existing_location_ids = []\n if not makenew:\n list_of_locations = xmlroot.findall('location')\n for item in list_of_locations:\n try:\n existing_location_ids.append(int(item.get('id')))\n except TypeError:\n pass\n if item.get('href') == url:\n location_element = item\n existing_location_ids.sort()\n # ... if not, add it!\n if location_element is None:\n location_id = 0\n for existing_id in existing_location_ids:\n if location_id == existing_id:\n location_id += 1\n location_element = ET.SubElement(xmlroot, 'location')\n LOGGER.debug(\"__add_URL_to_10320LOC: location_element is (1) \" + ET.tostring(location_element, encoding=encoding_value) + ', now add id ' + str(location_id))\n location_element.set('id', str(location_id))\n LOGGER.debug(\"__add_URL_to_10320LOC: location_element is (2) \" + ET.tostring(location_element, encoding=encoding_value) + ', now add url ' + str(url))\n location_element.set('href', url)\n LOGGER.debug(\"__add_URL_to_10320LOC: location_element is (3) \" + ET.tostring(location_element, encoding=encoding_value))\n self.__set_or_adapt_10320LOC_attributes(location_element, weight, http_role, **kvpairs)\n # FIXME: If we start adapting the Handle Record by index (instead of\n # overwriting the entire one), be careful to add and/or overwrite!\n\n # (Re-)Add entire 10320 to entry, add entry to list of entries:\n LOGGER.debug(\"__add_URL_to_10320LOC: xmlroot is (2) \" + ET.tostring(xmlroot, encoding=encoding_value))\n entry['data'] = ET.tostring(xmlroot, encoding=encoding_value)\n list_of_entries.append(entry)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __set_or_adapt_10320LOC_attributes(self, locelement, weight=None, http_role=None, **kvpairs):\n '''\n Adds or updates attributes of a element. Existing attributes\n are not removed!\n\n :param locelement: A location element as xml snippet\n (xml.etree.ElementTree.Element).\n :param weight: Optional. The weight to be set (integer between 0 and\n 1). If None, no weight attribute is set. If the value is outside\n the accepted range, it is set to 1.\n :param http_role: Optional. The http_role to be set. This accepts any\n string. Currently, Handle System can process 'conneg'. In future,\n it may be able to process 'no_conneg' and 'browser'.\n :param all others: Optional. All other key-value pairs will be set to\n the element. Any value is accepted and transformed to string.\n '''\n\n if weight is not None:\n LOGGER.debug('__set_or_adapt_10320LOC_attributes: weight (' + str(type(weight)) + '): ' + str(weight))\n weight = float(weight)\n if weight < 0 or weight > 1:\n default = 1\n LOGGER.debug('__set_or_adapt_10320LOC_attributes: Invalid weight (' + str(weight) + \\\n '), using default value (' + str(default) + ') instead.')\n weight = default\n weight = str(weight)\n locelement.set('weight', weight)\n\n if http_role is not None:\n locelement.set('http_role', http_role)\n\n for key, value in kvpairs.items():\n locelement.set(key, str(value))", "response": "Set or update the weight and http_role of a location element."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nverify the fist authorization response url code", "response": "def auth_access(self, auth_code):\n \"\"\"\n verify the fist authorization response url code\n\n response data\n \u8fd4\u56de\u503c\u5b57\u6bb5 \u5b57\u6bb5\u7c7b\u578b \u5b57\u6bb5\u8bf4\u660e\n access_token string \u7528\u6237\u6388\u6743\u7684\u552f\u4e00\u7968\u636e\uff0c\u7528\u4e8e\u8c03\u7528\u5fae\u535a\u7684\u5f00\u653e\u63a5\u53e3\uff0c\u540c\u65f6\u4e5f\u662f\u7b2c\u4e09\u65b9\u5e94\u7528\u9a8c\u8bc1\u5fae\u535a\u7528\u6237\u767b\u5f55\u7684\u552f\u4e00\u7968\u636e\uff0c\n \u7b2c\u4e09\u65b9\u5e94\u7528\u5e94\u8be5\u7528\u8be5\u7968\u636e\u548c\u81ea\u5df1\u5e94\u7528\u5185\u7684\u7528\u6237\u5efa\u7acb\u552f\u4e00\u5f71\u5c04\u5173\u7cfb\uff0c\u6765\u8bc6\u522b\u767b\u5f55\u72b6\u6001\uff0c\u4e0d\u80fd\u4f7f\u7528\u672c\u8fd4\u56de\u503c\u91cc\u7684UID\n \u5b57\u6bb5\u6765\u505a\u767b\u5f55\u8bc6\u522b\u3002\n\n expires_in string access_token\u7684\u751f\u547d\u5468\u671f\uff0c\u5355\u4f4d\u662f\u79d2\u6570\u3002\n remind_in string access_token\u7684\u751f\u547d\u5468\u671f\uff08\u8be5\u53c2\u6570\u5373\u5c06\u5e9f\u5f03\uff0c\u5f00\u53d1\u8005\u8bf7\u4f7f\u7528expires_in\uff09\u3002\n uid string \u6388\u6743\u7528\u6237\u7684UID\uff0c\u672c\u5b57\u6bb5\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u5f00\u53d1\u8005\uff0c\u51cf\u5c11\u4e00\u6b21user/show\u63a5\u53e3\u8c03\u7528\u800c\u8fd4\u56de\u7684\uff0c\u7b2c\u4e09\u65b9\u5e94\u7528\u4e0d\u80fd\u7528\u6b64\u5b57\u6bb5\u4f5c\u4e3a\u7528\u6237\n \u767b\u5f55\u72b6\u6001\u7684\u8bc6\u522b\uff0c\u53ea\u6709access_token\u624d\u662f\u7528\u6237\u6388\u6743\u7684\u552f\u4e00\u7968\u636e\u3002\n\n :param auth_code: authorize_url response code\n :return:\n\n normal:\n {\n \"access_token\": \"ACCESS_TOKEN\",\n \"expires_in\": 1234,\n \"remind_in\":\"798114\",\n \"uid\":\"12341234\"\n }\n mobile:\n {\n \"access_token\": \"SlAV32hkKG\",\n \"remind_in\": 3600,\n \"expires_in\": 3600\n \"refresh_token\": \"QXBK19xm62\"\n }\n\n \"\"\"\n data = {\n 'client_id': self.client_id,\n 'client_secret': self.client_secret,\n 'grant_type': 'authorization_code',\n 'code': auth_code,\n 'redirect_uri': self.redirect_url\n }\n return self.request(\"post\", \"access_token\", data=data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend a HTTP GET request to the handle server to read a single entry from a record.", "response": "def send_handle_get_request(self, handle, indices=None):\n '''\n Send a HTTP GET request to the handle server to read either an entire\n handle or to some specified values from a handle record, using the\n requests module.\n\n :param handle: The handle.\n :param indices: Optional. A list of indices to delete. Defaults to\n None (i.e. the entire handle is deleted.). The list can contain\n integers or strings.\n :return: The server's response.\n '''\n\n\n # Assemble required info:\n url = self.make_handle_URL(handle, indices)\n LOGGER.debug('GET Request to '+url)\n head = self.__get_headers('GET')\n veri = self.__HTTPS_verify\n\n # Send the request\n if self.__cert_needed_for_get_request():\n # If this is the first request and the connector uses client cert authentication, we need to send the cert along\n # in the first request that builds the session.\n resp = self.__session.get(url, headers=head, verify=veri, cert=self.__cert_object)\n else:\n # Normal case:\n resp = self.__session.get(url, headers=head, verify=veri)\n \n # Log and return\n self.__log_request_response_to_file(\n logger=REQUESTLOGGER,\n op='GET',\n handle=handle,\n url=url,\n headers=head,\n verify=veri,\n resp=resp\n )\n self.__first_request = False\n return resp"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef send_handle_put_request(self, **args):\n '''\n Send a HTTP PUT request to the handle server to write either an entire\n handle or to some specified values to an handle record, using the\n requests module.\n\n :param handle: The handle.\n :param list_of_entries: A list of handle record entries to be written,\n in the format [{\"index\":xyz, \"type\":\"xyz\", \"data\":\"xyz\"}] or similar.\n :param indices: Optional. A list of indices to delete. Defaults\n to None (i.e. the entire handle is deleted.). The list can\n contain integers or strings.\n :param overwrite: Optional. Whether the handle should be overwritten\n if it exists already.\n :return: The server's response.\n '''\n\n # Check if we have write access at all:\n if not self.__has_write_access:\n raise HandleAuthenticationError(msg=self.__no_auth_message)\n\n # Check args:\n mandatory_args = ['handle', 'list_of_entries']\n optional_args = ['indices', 'op', 'overwrite']\n b2handle.util.add_missing_optional_args_with_value_none(args, optional_args)\n b2handle.util.check_presence_of_mandatory_args(args, mandatory_args)\n handle = args['handle']\n list_of_entries = args['list_of_entries']\n indices = args['indices']\n op = args['op']\n overwrite = args['overwrite'] or False\n\n # Make necessary values:\n url = self.make_handle_URL(handle, indices, overwrite=overwrite)\n LOGGER.debug('PUT Request to '+url)\n payload = json.dumps({'values':list_of_entries})\n LOGGER.debug('PUT Request payload: '+payload)\n head = self.__get_headers('PUT')\n LOGGER.debug('PUT Request headers: '+str(head))\n veri = self.__HTTPS_verify\n\n # Send request to server:\n resp = self.__send_put_request_to_server(url, payload, head, veri, handle)\n if b2handle.hsresponses.is_redirect_from_http_to_https(resp):\n resp = self.__resend_put_request_on_302(payload, head, veri, handle, resp)\n\n # Check response for authentication issues:\n if b2handle.hsresponses.not_authenticated(resp):\n raise HandleAuthenticationError(\n operation=op,\n handle=handle,\n response=resp,\n username=self.__username\n )\n self.__first_request = False\n return resp, payload", "response": "Send a PUT request to the handle server."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsends a HTTP DELETE request to the handle server.", "response": "def send_handle_delete_request(self, **args):\n '''\n Send a HTTP DELETE request to the handle server to delete either an\n entire handle or to some specified values from a handle record,\n using the requests module.\n\n :param handle: The handle.\n :param indices: Optional. A list of indices to delete. Defaults to\n None (i.e. the entire handle is deleted.). The list can contain\n integers or strings.\n :return: The server's response.\n '''\n\n # Check if we have write access at all:\n if not self.__has_write_access:\n raise HandleAuthenticationError(msg=self.__no_auth_message)\n\n # Check args:\n mandatory_args = ['handle']\n optional_args = ['indices', 'op']\n b2handle.util.add_missing_optional_args_with_value_none(args, optional_args)\n b2handle.util.check_presence_of_mandatory_args(args, mandatory_args)\n handle = args['handle']\n indices = args['indices']\n op = args['op']\n\n # Make necessary values:\n url = self.make_handle_URL(handle, indices)\n if indices is not None and len(indices) > 0:\n LOGGER.debug('__send_handle_delete_request: Deleting values '+str(indices)+' from handle '+handle+'.')\n else:\n LOGGER.debug('__send_handle_delete_request: Deleting handle '+handle+'.')\n LOGGER.debug('DELETE Request to '+url)\n head = self.__get_headers('DELETE')\n veri = self.__HTTPS_verify\n\n # Make request:\n resp = None\n if self.__authentication_method == self.__auth_methods['user_pw']:\n resp = self.__session.delete(url, headers=head, verify=veri)\n elif self.__authentication_method == self.__auth_methods['cert']:\n resp = self.__session.delete(url, headers=head, verify=veri, cert=self.__cert_object)\n self.__log_request_response_to_file(\n logger=REQUESTLOGGER,\n op='DELETE',\n handle=handle,\n url=url,\n headers=head,\n verify=veri,\n resp=resp\n )\n\n # Check response for authentication issues:\n if b2handle.hsresponses.not_authenticated(resp):\n raise HandleAuthenticationError(\n operation=op,\n handle=handle,\n response=resp,\n username=self.__username\n )\n self.__first_request = False\n return resp"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef check_if_username_exists(self, username):\n '''\n Check if the username handles exists.\n\n :param username: The username, in the form index:prefix/suffix\n :raises: :exc:`~b2handle.handleexceptions.HandleNotFoundException`\n :raises: :exc:`~b2handle.handleexceptions.GenericHandleError`\n :return: True. If it does not exist, an exception is raised.\n\n *Note:* Only the existence of the handle is verified. The existence or\n validity of the index is not checked, because entries containing\n a key are hidden anyway.\n '''\n LOGGER.debug('check_if_username_exists...')\n\n _, handle = b2handle.utilhandle.remove_index_from_handle(username)\n\n resp = self.send_handle_get_request(handle)\n resp_content = decoded_response(resp)\n if b2handle.hsresponses.does_handle_exist(resp):\n handlerecord_json = json.loads(resp_content)\n if not handlerecord_json['handle'] == handle:\n raise GenericHandleError(\n operation='Checking if username exists',\n handle=handle,\n reponse=resp,\n msg='The check returned a different handle than was asked for.'\n )\n return True\n elif b2handle.hsresponses.handle_not_found(resp):\n msg = 'The username handle does not exist'\n raise HandleNotFoundException(handle=handle, msg=msg, response=resp)\n else:\n op = 'checking if handle exists'\n msg = 'Checking if username exists went wrong'\n raise GenericHandleError(operation=op, handle=handle, response=resp, msg=msg)", "response": "Checks if the username handles exists."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate HTTP headers for different HTTP requests.", "response": "def __get_headers(self, action):\n '''\n Create HTTP headers for different HTTP requests. Content-type and\n Accept are 'application/json', as the library is interacting with\n a REST API.\n\n :param action: Action for which to create the header ('GET', 'PUT',\n 'DELETE', 'SEARCH').\n :return: dict containing key-value pairs, e.g. 'Accept',\n 'Content-Type', etc. (depening on the action).\n '''\n header = {}\n accept = 'application/json'\n content_type = 'application/json'\n\n if action is 'GET':\n header['Accept'] = accept\n\n\n elif action is 'PUT' or action is 'DELETE':\n\n if self.__authentication_method == self.__auth_methods['cert']:\n header['Authorization'] = 'Handle clientCert=\"true\"'\n\n elif self.__authentication_method == self.__auth_methods['user_pw']:\n header['Authorization'] = 'Basic ' + self.__basic_authentication_string\n\n if action is 'PUT':\n header['Content-Type'] = content_type\n\n\n else:\n LOGGER.debug('__getHeader: ACTION is unknown ('+action+')')\n return header"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_handle_URL(self, handle, indices=None, overwrite=None, other_url=None):\n '''\n Create the URL for a HTTP request (URL + query string) to request\n a specific handle from the Handle Server.\n\n :param handle: The handle to access.\n :param indices: Optional. A list of integers or strings. Indices of\n the handle record entries to read or write. Defaults to None.\n :param overwrite: Optional. If set, an overwrite flag will be appended\n to the URL ({?,&}overwrite=true or {?,&}overwrite=false). If not set, no\n flag is set, thus the Handle Server's default behaviour will be\n used. Defaults to None.\n :param other_url: Optional. If a different Handle Server URL than the\n one specified in the constructor should be used. Defaults to None.\n If set, it should be set including the URL extension,\n e.g. '/api/handles/'.\n :return: The complete URL, e.g.\n 'http://some.handle.server/api/handles/prefix/suffix?index=2&index=6&overwrite=false\n '''\n LOGGER.debug('make_handle_URL...')\n separator = '?'\n\n if other_url is not None:\n url = other_url\n else:\n url = self.__handle_server_url.strip('/') +'/'+\\\n self.__REST_API_url_extension.strip('/')\n url = url.strip('/')+'/'+ handle\n\n if indices is None:\n indices = []\n if len(indices) > 0:\n for index in indices:\n url = url+separator+'index='+str(index)\n separator = '&'\n\n if overwrite is not None:\n if overwrite:\n url = url+separator+'overwrite=true'\n else:\n url = url+separator+'overwrite=false'\n\n return url", "response": "This method creates a URL for a specific handle from the Handle Server."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrecord a single hit on a given metric.", "response": "def publish_metric(self, metric_name, metric_value, epoch_seconds=None):\n '''Record a single hit on a given metric.\n\n Args:\n metric_name: The name of the metric to record with Carbon.\n metric_value: The value to record with Carbon.\n epoch_seconds: Optionally specify the time for the metric hit.\n\n Returns:\n None\n\n '''\n if epoch_seconds is None:\n epoch_seconds = self._reactor.seconds()\n self._client_factory.publish_metric(metric_name, metric_value, int(epoch_seconds))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrecord hits to a metric at a specified interval.", "response": "def register_repeating_metric(self, metric_name, frequency, getter):\n '''Record hits to a metric at a specified interval.\n\n Args:\n metric_name: The name of the metric to record with Carbon.\n frequency: The frequency with which to poll the getter and record the value with Carbon.\n getter: A function which takes no arguments and returns the value to record with Carbon.\n\n Returns:\n RepeatingMetricHandle instance. Call .stop() on it to stop recording the metric.\n\n '''\n l = task.LoopingCall(self._publish_repeating_metric, metric_name, getter)\n repeating_metric_handle = RepeatingMetricHandle(l, frequency)\n self._repeating_metric_handles.append(repeating_metric_handle)\n if self.running:\n repeating_metric_handle.start()\n return repeating_metric_handle"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef setup(console=False, port=None, menu=True):\n\n if self._has_been_setup:\n teardown()\n\n register_plugins()\n register_host()\n\n if menu:\n add_to_filemenu()\n self._has_menu = True\n\n self._has_been_setup = True\n print(\"pyblish: Loaded successfully.\")", "response": "Setup integration with Pyblish"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntry showing the most desirable GUI", "response": "def show():\n \"\"\"Try showing the most desirable GUI\n\n This function cycles through the currently registered\n graphical user interfaces, if any, and presents it to\n the user.\n\n \"\"\"\n\n parent = None\n current = QtWidgets.QApplication.activeWindow()\n while current:\n parent = current\n current = parent.parent()\n\n window = (_discover_gui() or _show_no_gui)(parent)\n\n return window"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _nuke_set_zero_margins(widget_object):\n parentApp = QtWidgets.QApplication.allWidgets()\n parentWidgetList = []\n for parent in parentApp:\n for child in parent.children():\n if widget_object.__class__.__name__ == child.__class__.__name__:\n parentWidgetList.append(\n parent.parentWidget())\n parentWidgetList.append(\n parent.parentWidget().parentWidget())\n parentWidgetList.append(\n parent.parentWidget().parentWidget().parentWidget())\n\n for sub in parentWidgetList:\n for tinychild in sub.children():\n try:\n tinychild.setContentsMargins(0, 0, 0, 0)\n except Exception:\n pass", "response": "Remove Nuke margins when docked UI\n .. _More info :"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a new window that is dockable.", "response": "def dock(window):\n \"\"\" Expecting a window to parent into a Nuke panel, that is dockable. \"\"\"\n\n # Deleting existing dock\n # There is a bug where existing docks are kept in-memory when closed via UI\n if self._dock:\n print(\"Deleting existing dock...\")\n parent = self._dock\n dialog = None\n stacked_widget = None\n main_windows = []\n\n # Getting dock parents\n while parent:\n if isinstance(parent, QtWidgets.QDialog):\n dialog = parent\n if isinstance(parent, QtWidgets.QStackedWidget):\n stacked_widget = parent\n if isinstance(parent, QtWidgets.QMainWindow):\n main_windows.append(parent)\n parent = parent.parent()\n\n dialog.deleteLater()\n\n if len(main_windows) > 1:\n # Then it's a floating window\n if stacked_widget.count() == 1:\n # Then it's empty and we can close it,\n # as is native Nuke UI behaviour\n main_windows[0].deleteLater()\n\n # Creating new dock\n pane = nuke.getPaneFor(\"Properties.1\")\n widget_path = \"pyblish_nuke.lib.pyblish_nuke_dockwidget\"\n panel = nukescripts.panels.registerWidgetAsPanel(widget_path,\n window.windowTitle(),\n \"pyblish_nuke.dock\",\n True).addToPane(pane)\n\n panel_widget = panel.customKnob.getObject().widget\n panel_widget.layout().addWidget(window)\n _nuke_set_zero_margins(panel_widget)\n self._dock = panel_widget\n\n return self._dock"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove the index from the handle string.", "response": "def remove_index_from_handle(handle_with_index):\n '''\n Returns index and handle separately, in a tuple.\n\n :handle_with_index: The handle string with an index (e.g.\n 500:prefix/suffix)\n :return: index and handle as a tuple.\n '''\n\n split = handle_with_index.split(':')\n if len(split) == 2:\n split[0] = int(split[0])\n return split\n elif len(split) == 1:\n return (None, handle_with_index)\n elif len(split) > 2:\n raise handleexceptions.HandleSyntaxError(\n msg='Too many colons',\n handle=handle_with_index,\n expected_syntax='index:prefix/suffix')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking the syntax of a handle without an index.", "response": "def check_handle_syntax(string):\n '''\n Checks the syntax of a handle without an index (are prefix\n and suffix there, are there too many slashes?).\n\n :string: The handle without index, as string prefix/suffix.\n :raise: :exc:`~b2handle.handleexceptions.handleexceptions.HandleSyntaxError`\n :return: True. If it's not ok, exceptions are raised.\n\n '''\n\n expected = 'prefix/suffix'\n\n try:\n arr = string.split('/')\n except AttributeError:\n raise handleexceptions.HandleSyntaxError(msg='The provided handle is None', expected_syntax=expected)\n\n if len(arr) < 2:\n msg = 'No slash'\n raise handleexceptions.HandleSyntaxError(msg=msg, handle=string, expected_syntax=expected)\n\n if len(arr[0]) == 0:\n msg = 'Empty prefix'\n raise handleexceptions.HandleSyntaxError(msg=msg, handle=string, expected_syntax=expected)\n\n if len(arr[1]) == 0:\n msg = 'Empty suffix'\n raise handleexceptions.HandleSyntaxError(msg=msg, handle=string, expected_syntax=expected)\n\n if ':' in string:\n check_handle_syntax_with_index(string, base_already_checked=True)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_authentication_string(username, password):\n '''\n Creates an authentication string from the username and password.\n\n :username: Username.\n :password: Password.\n :return: The encoded string.\n '''\n\n username_utf8 = username.encode('utf-8')\n userpw_utf8 = password.encode('utf-8')\n username_perc = quote(username_utf8)\n userpw_perc = quote(userpw_utf8)\n\n authinfostring = username_perc + ':' + userpw_perc\n authinfostring_base64 = base64.b64encode(authinfostring.encode('utf-8')).decode('utf-8')\n return authinfostring_base64", "response": "Creates an authentication string from the username and password."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a string containing all relevant information about a request made to the Handle System, for logging purposes. :handle: The handle that the request is about. :url: The url the request is sent to. :headers: The headers sent along with the request. :verify: Boolean parameter passed to the requests module (https verification). :resp: The request's response. :op: The library operation during which the request was sent. :payload: Optional. The payload sent with the request. :return: A formatted string.", "response": "def make_request_log_message(**args):\n '''\n Creates a string containing all relevant information\n about a request made to the Handle System, for\n logging purposes.\n\n :handle: The handle that the request is about.\n :url: The url the request is sent to.\n :headers: The headers sent along with the request.\n :verify: Boolean parameter passed to the requests\n module (https verification).\n :resp: The request's response.\n :op: The library operation during which the request\n was sent.\n :payload: Optional. The payload sent with the request.\n :return: A formatted string.\n\n '''\n\n mandatory_args = ['op', 'handle', 'url', 'headers', 'verify', 'resp']\n optional_args = ['payload']\n util.check_presence_of_mandatory_args(args, mandatory_args)\n util.add_missing_optional_args_with_value_none(args, optional_args)\n\n space = '\\n '\n message = ''\n message += '\\n'+args['op']+' '+args['handle']\n message += space+'URL: '+args['url']\n message += space+'HEADERS: '+str(args['headers'])\n message += space+'VERIFY: '+str(args['verify'])\n if 'payload' in args.keys():\n message += space+'PAYLOAD:'+space+str(args['payload'])\n message += space+'RESPONSECODE: '+str(args['resp'].status_code)\n message += space+'RESPONSE:'+space+str(args['resp'].content)\n return message"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef request_xml(url, auth=None):\n '''\n Returns an etree.XMLRoot object loaded from the url\n :param str url: URL for the resource to load as an XML\n '''\n try:\n r = requests.get(url, auth=auth, verify=False)\n return r.text.encode('utf-8')\n except BaseException:\n logger.error(\"Skipping %s (error parsing the XML)\" % url)\n return", "response": "Request the XML from the URL and return the result as an etree. XMLRoot object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_catalog_url(self, url):\n '''\n Returns the appropriate catalog URL by replacing html with xml in some\n cases\n :param str url: URL to the catalog\n '''\n u = urlparse.urlsplit(url)\n name, ext = os.path.splitext(u.path)\n if ext == \".html\":\n u = urlparse.urlsplit(url.replace(\".html\", \".xml\"))\n url = u.geturl()\n return url", "response": "Returns the appropriate catalog URL by replacing html with xml in some\n cases"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nyield a URL corresponding to a leaf dataset for each dataset described by the current catalog .", "response": "def _yield_leaves(self, url, tree):\n '''\n Yields a URL corresponding to a leaf dataset for each dataset described by the catalog\n :param str url: URL for the current catalog\n :param lxml.etree.Eleemnt tree: Current XML Tree\n '''\n for leaf in tree.findall('.//{%s}dataset[@urlPath]' % INV_NS):\n # Subset by the skips\n name = leaf.get(\"name\")\n if any([x.match(name) for x in self.skip]):\n logger.info(\"Skipping dataset based on 'skips'. Name: %s\" % name)\n continue\n\n # Subset by before and after\n date_tag = leaf.find('.//{%s}date[@type=\"modified\"]' % INV_NS)\n if date_tag is not None:\n try:\n dt = parse(date_tag.text)\n except ValueError:\n logger.error(\"Skipping dataset.Wrong date string %s \" % date_tag.text)\n continue\n else:\n dt = dt.replace(tzinfo=pytz.utc)\n if self.after and dt < self.after:\n continue\n if self.before and dt > self.before:\n continue\n\n # Subset by the Selects defined\n gid = leaf.get('ID')\n if self.select is not None:\n if gid is not None and any([x.match(gid) for x in self.select]):\n logger.debug(\"Processing %s\" % gid)\n yield \"%s?dataset=%s\" % (url, gid)\n else:\n logger.info(\"Ignoring dataset based on 'selects'. ID: %s\" % gid)\n continue\n else:\n logger.debug(\"Processing %s\" % gid)\n yield \"%s?dataset=%s\" % (url, gid)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncompile the list of catalog reference URLs for the current catalog.", "response": "def _compile_references(self, url, tree):\n '''\n Returns a list of catalog reference URLs for the current catalog\n :param str url: URL for the current catalog\n :param lxml.etree.Eleemnt tree: Current XML Tree\n '''\n references = []\n for ref in tree.findall('.//{%s}catalogRef' % INV_NS):\n # Check skips\n title = ref.get(\"{%s}title\" % XLINK_NS)\n if any([x.match(title) for x in self.skip]):\n logger.info(\"Skipping catalogRef based on 'skips'. Title: %s\" % title)\n continue\n references.append(construct_url(url, ref.get(\"{%s}href\" % XLINK_NS)))\n return references"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _run(self, url, auth):\n '''\n Performs a multiprocess depth-first-search of the catalog references\n and yields a URL for each leaf dataset found\n :param str url: URL for the current catalog\n :param requests.auth.AuthBase auth: requets auth object to use\n '''\n if url in self.visited:\n logger.debug(\"Skipping %s (already crawled)\" % url)\n return\n self.visited.append(url)\n\n logger.info(\"Crawling: %s\" % url)\n url = self._get_catalog_url(url)\n\n # Get an etree object\n xml_content = request_xml(url, auth)\n for ds in self._build_catalog(url, xml_content):\n yield ds", "response": "Performs a multiprocess depth - first search of the catalog references\n and yields a URL for each leaf dataset found\n ."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _build_catalog(self, url, xml_content):\n '''\n Recursive function to perform the DFS and yield the leaf datasets\n :param str url: URL for the current catalog\n :param str xml_content: XML Body returned from HTTP Request\n '''\n try:\n tree = etree.XML(xml_content)\n except BaseException:\n return\n\n # Get a list of URLs\n references = self._compile_references(url, tree)\n # Using multiple processes, make HTTP requests for each child catalog\n jobs = [self.pool.apply_async(request_xml, args=(ref,)) for ref in references]\n responses = [j.get() for j in jobs]\n\n # This is essentially the graph traversal step\n for i, response in enumerate(responses):\n for ds in self._build_catalog(references[i], response):\n yield ds\n\n # Yield the leaves\n for ds in self._yield_leaves(url, tree):\n yield ds", "response": "Recursive function to perform the DFS and yield the leaf datasets"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef find_module(fdr, fqname, path = None):\n '''Find a loader for module or package `fqname`.\n\n This method will be called with the fully qualified name\n of the module. If the finder is installed on `sys.meta_path`,\n it will receive a second argument, which is `None` for\n a top-level module, or `package.__path__` for submodules\n or subpackages [5].\n It should return a loader object if the module was found, or\n `None` if it wasn't. If `find_module()` raises an exception,\n it will be propagated to the caller, aborting the import.\n\n [5] The path argument to `finder.find_module()` is there\n because the `pkg.__path__` variable may be needed\n at this point. It may either come from the actual\n parent module or be supplied by `imp.find_module()`\n or the proposed `imp.get_loader()` function.\n '''\n if fqname in fdr.aliases:\n return Loader(fqname, fdr.aliases[fqname])\n return None", "response": "Find a loader for a module or package."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_module(ldr, fqname):\n '''Load `fqname` from under `ldr.fspath`.\n\n The `fqname` argument is the fully qualified module name,\n eg. \"spam.eggs.ham\". As explained above, when ::\n\n finder.find_module(\"spam.eggs.ham\")\n\n is called, \"spam.eggs\" has already been imported and added\n to `sys.modules`. However, the `find_module()` method isn't\n necessarily always called during an actual import:\n meta tools that analyze import dependencies (such as freeze,\n Installer or py2exe) don't actually load modules, so\n a finder shouldn't depend on the parent package being\n available in `sys.modules`.\n\n The `load_module()` method has a few responsibilities that\n it must fulfill before it runs any code:\n\n * If there is an existing module object named 'fullname' in\n `sys.modules`, the loader must use that existing module.\n (Otherwise, the `reload()` builtin will not work correctly.)\n If a module named 'fullname' does not exist in\n `sys.modules`, the loader must create a new module object\n and add it to `sys.modules`.\n\n Note that the module object must be in `sys.modules`\n before the loader executes the module code. This is\n crucial because the module code may (directly or\n indirectly) import itself; adding it to `sys.modules`\n beforehand prevents unbounded recursion in the worst case\n and multiple loading in the best.\n\n If the load fails, the loader needs to remove any module it\n may have inserted into `sys.modules`. If the module was\n already in `sys.modules` then the loader should leave it\n alone.\n\n * The `__file__` attribute must be set. This must be a string,\n but it may be a dummy value, for example \"\".\n The privilege of not having a `__file__` attribute at all\n is reserved for built-in modules.\n\n * The `__name__` attribute must be set. If one uses\n `imp.new_module()` then the attribute is set automatically.\n\n * If it's a package, the __path__ variable must be set.\n This must be a list, but may be empty if `__path__` has no\n further significance to the importer (more on this later).\n\n * The `__loader__` attribute must be set to the loader object.\n This is mostly for introspection and reloading, but can be\n used for importer-specific extras, for example getting data\n associated with an importer.\n\n The `__package__` attribute [8] must be set.\n\n If the module is a Python module (as opposed to a built-in\n module or a dynamically loaded extension), it should execute\n the module's code in the module's global name space\n (`module.__dict__`).\n\n [8] PEP 366: Main module explicit relative imports\n http://www.python.org/dev/peps/pep-0366/\n '''\n\n scope = ldr.scope.split('.')\n modpath = fqname.split('.')\n\n if scope != modpath[0:len(scope)]:\n raise AssertionError(\n \"%s responsible for %s got request for %s\" % (\n ldr.__class__.__name__,\n ldr.scope,\n fqname,\n )\n )\n\n if fqname in sys.modules:\n mod = sys.modules[fqname]\n else:\n mod = sys.modules.setdefault(fqname, types.ModuleType(fqname))\n\n mod.__loader__ = ldr\n\n fspath = ldr.path_to(fqname)\n\n mod.__file__ = str(fspath)\n\n if fs.is_package(fspath):\n mod.__path__ = [ldr.fspath]\n mod.__package__ = str(fqname)\n else:\n mod.__package__ = str(fqname.rpartition('.')[0])\n\n exec(fs.get_code(fspath), mod.__dict__)\n\n return mod", "response": "Load a module from the given file path."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate an attached thread and fork it.", "response": "def zthread_fork(ctx, func, *args, **kwargs):\n \"\"\"\n Create an attached thread. An attached thread gets a ctx and a PAIR\n pipe back to its parent. It must monitor its pipe, and exit if the\n pipe becomes unreadable. Returns pipe, or NULL if there was an error.\n \"\"\"\n a = ctx.socket(zmq.PAIR)\n a.setsockopt(zmq.LINGER, 0)\n a.setsockopt(zmq.RCVHWM, 100)\n a.setsockopt(zmq.SNDHWM, 100)\n a.setsockopt(zmq.SNDTIMEO, 5000)\n a.setsockopt(zmq.RCVTIMEO, 5000)\n b = ctx.socket(zmq.PAIR)\n b.setsockopt(zmq.LINGER, 0)\n b.setsockopt(zmq.RCVHWM, 100)\n b.setsockopt(zmq.SNDHWM, 100)\n b.setsockopt(zmq.SNDTIMEO, 5000)\n a.setsockopt(zmq.RCVTIMEO, 5000)\n iface = \"inproc://%s\" % binascii.hexlify(os.urandom(8))\n a.bind(iface)\n b.connect(iface)\n\n thread = threading.Thread(target=func, args=((ctx, b) + args), kwargs=kwargs)\n thread.daemon = False\n thread.start()\n\n return a"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nprevent accidental assignment of existing members of the object.", "response": "def _remap(object, name, value, safe=True):\n \"\"\"Prevent accidental assignment of existing members\n\n Arguments:\n object (object): Parent of new attribute\n name (str): Name of new attribute\n value (object): Value of new attribute\n safe (bool): Whether or not to guarantee that\n the new attribute was not overwritten.\n Can be set to False under condition that\n it is superseded by extensive testing.\n\n \"\"\"\n\n if os.getenv(\"QT_TESTING\") is not None and safe:\n # Cannot alter original binding.\n if hasattr(object, name):\n raise AttributeError(\"Cannot override existing name: \"\n \"%s.%s\" % (object.__name__, name))\n\n # Cannot alter classes of functions\n if type(object).__name__ != \"module\":\n raise AttributeError(\"%s != 'module': Cannot alter \"\n \"anything but modules\" % object)\n\n elif hasattr(object, name):\n # Keep track of modifications\n self.__modified__.append(name)\n\n self.__remapped__.append(name)\n\n setattr(object, name, value)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlog the instantiation of an object to the given logger.", "response": "def log_instantiation(LOGGER, classname, args, forbidden, with_date=False):\n '''\n Log the instantiation of an object to the given logger.\n\n :LOGGER: A logger to log to. Please see module \"logging\".\n :classname: The name of the class that is being\n instantiated.\n :args: A dictionary of arguments passed to the instantiation,\n which will be logged on debug level.\n :forbidden: A list of arguments whose values should not be\n logged, e.g. \"password\".\n :with_date: Optional. Boolean. Indicated whether the initiation\n date and time should be logged.\n '''\n\n # Info:\n if with_date:\n LOGGER.info('Instantiating '+classname+' at '+datetime.datetime.now().strftime('%Y-%m-%d_%H:%M'))\n else:\n LOGGER.info('Instantiating '+classname)\n\n # Debug:\n for argname in args:\n if args[argname] is not None:\n if argname in forbidden:\n LOGGER.debug('Param '+argname+'*******')\n else:\n LOGGER.debug('Param '+argname+'='+str(args[argname]))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting dict value if value is bool type False -> false True -> true", "response": "def filter_params(params):\n \"\"\"\n convert dict value if value is bool type,\n False -> \"false\"\n True -> \"true\"\n \"\"\"\n if params is not None:\n new_params = copy.deepcopy(params)\n new_params = dict((k, v) for k, v in new_params.items() if v is not None)\n for key, value in new_params.items():\n if isinstance(value, bool):\n new_params[key] = \"true\" if value else \"false\"\n return new_params"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nhandling response from weibo API.", "response": "def _handler_response(self, response, data=None):\n \"\"\"\n error code response:\n {\n \"request\": \"/statuses/home_timeline.json\",\n \"error_code\": \"20502\",\n \"error\": \"Need you follow uid.\"\n }\n :param response:\n :return:\n \"\"\"\n if response.status_code == 200:\n data = response.json()\n if isinstance(data, dict) and data.get(\"error_code\"):\n\n raise WeiboAPIError(data.get(\"request\"), data.get(\"error_code\"), data.get(\"error\"))\n else:\n return data\n else:\n raise WeiboRequestError(\n \"Weibo API request error: status code: {code} url:{url} ->\"\n \" method:{method}: data={data}\".format(\n code=response.status_code,\n url=response.url,\n method=response.request.method,\n data=data\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrequesting weibo api :param suffix: str, :param params: dict, url query parameters :return:", "response": "def get(self, suffix, params=None):\n \"\"\"\n request weibo api\n :param suffix: str,\n :param params: dict, url query parameters\n :return:\n\n \"\"\"\n\n url = self.base + suffix\n params = filter_params(params)\n\n response = self.session.get(url=url, params=params)\n\n return self._handler_response(response)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef post(self, suffix, params=None, data=None, files=None):\n\n url = self.base + suffix\n params = filter_params(params)\n\n response = self.session.post(url=url, params=params, data=data, files=files)\n\n return self._handler_response(response, data=data)", "response": "POST a new entry in the cache"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef search_handle(self, **args):\n '''\n Search for handles containing the specified key with the specified\n value. The search terms are passed on to the reverse lookup servlet\n as-is. The servlet is supposed to be case-insensitive, but if it\n isn't, the wrong case will cause a :exc:`~b2handle.handleexceptions.ReverseLookupException`.\n\n *Note:* If allowed search keys are configured, only these are used. If\n no allowed search keys are specified, all key-value pairs are\n passed on to the reverse lookup servlet, possibly causing a\n :exc:`~b2handle.handleexceptions.ReverseLookupException`.\n\n Example calls:\n * list_of_handles = search_handle('http://www.foo.com')\n * list_of_handles = search_handle('http://www.foo.com', CHECKSUM=99999)\n * list_of_handles = search_handle(URL='http://www.foo.com', CHECKSUM=99999)\n\n :param URL: Optional. The URL to search for (reverse lookup). [This is\n NOT the URL of the search servlet!]\n :param prefix: Optional. The Handle prefix to which the search should\n be limited to. If unspecified, the method will search across all\n prefixes present at the server given to the constructor.\n :param key_value_pairs: Optional. Several search fields and values can\n be specified as key-value-pairs,\n e.g. CHECKSUM=123456, URL=www.foo.com\n :raise: :exc:`~b2handle.handleexceptions.ReverseLookupException`: If a search field is specified that\n cannot be used, or if something else goes wrong.\n :return: A list of all Handles (strings) that bear the given key with\n given value of given prefix or server. The list may be empty and\n may also contain more than one element.\n '''\n LOGGER.debug('search_handle...')\n if self.__has_search_access:\n return self.__search_handle(**args)\n else:\n LOGGER.error(\n 'Searching not possible. Reason: No access '+\n 'to search system (endpoint: '+\n str(self.__search_url)+').'\n )\n return None", "response": "Search for all handles containing the specified key with the specified value."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_revlookup_query(self, *fulltext_searchterms, **keyvalue_searchterms):\n '''\n Create the part of the solr request that comes after the question mark,\n e.g. ?URL=*dkrz*&CHECKSUM=*abc*. If allowed search keys are\n configured, only these are used. If no'allowed search keys are\n specified, all key-value pairs are passed on to the reverse lookup\n servlet.\n\n :param fulltext_searchterms: Optional. Any term specified will be used\n as search term. Not implemented yet, so will be ignored.\n :param keyvalue_searchterms: Optional. Key-value pairs. Any key-value\n pair will be used to search for the value in the field \"key\".\n Wildcards accepted (refer to the documentation of the reverse\n lookup servlet for syntax.)\n :return: The query string, after the \"?\". If no valid search terms were\n specified, None is returned.\n '''\n LOGGER.debug('create_revlookup_query...')\n\n allowed_search_keys = self.__allowed_search_keys\n only_search_for_allowed_keys = False\n if len(allowed_search_keys) > 0:\n only_search_for_allowed_keys = True\n\n fulltext_searchterms_given = True\n fulltext_searchterms = b2handle.util.remove_value_none_from_list(fulltext_searchterms)\n if len(fulltext_searchterms) == 0:\n fulltext_searchterms_given = False\n \n if fulltext_searchterms_given:\n msg = 'Full-text search is not implemented yet.'+\\\n ' The provided searchterms '+str(fulltext_searchterms)+\\\n ' can not be used.'\n raise ReverseLookupException(msg=msg)\n\n keyvalue_searchterms_given = True\n keyvalue_searchterms = b2handle.util.remove_value_none_from_dict(keyvalue_searchterms)\n if len(keyvalue_searchterms) == 0:\n keyvalue_searchterms_given = False\n\n if not keyvalue_searchterms_given and not fulltext_searchterms_given:\n msg = 'No search terms have been specified. Please specify'+\\\n ' at least one key-value-pair.'\n raise ReverseLookupException(msg=msg)\n\n counter = 0\n query = '?'\n for key, value in keyvalue_searchterms.items():\n\n if only_search_for_allowed_keys and key not in allowed_search_keys:\n msg = 'Cannot search for key \"'+key+'\". Only searches '+\\\n 'for keys '+str(allowed_search_keys)+' are implemented.'\n raise ReverseLookupException(msg=msg)\n else:\n query = query+'&'+key+'='+value\n counter += 1\n\n query = query.replace('?&', '?')\n LOGGER.debug('create_revlookup_query: query: '+query)\n if counter == 0: # unreachable?\n msg = 'No valid search terms have been specified.'\n raise ReverseLookupException(msg=msg)\n return query", "response": "Create the part of the solr request that comes after the question mark."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef load_from_JSON(json_filename):\n '''\n Create a new instance of a PIDClientCredentials with information read\n from a local JSON file.\n\n :param json_filename: The path to the json credentials file. The json\n file should have the following format:\n\n .. code:: json\n\n {\n \"handle_server_url\": \"https://url.to.your.handle.server\",\n \"username\": \"index:prefix/suffix\",\n \"password\": \"ZZZZZZZ\",\n \"prefix\": \"prefix_to_use_for_writing_handles\",\n \"handleowner\": \"username_to_own_handles\"\n }\n\n Any additional key-value-pairs are stored in the instance as\n config.\n :raises: :exc:`~b2handle.handleexceptions.CredentialsFormatError`\n :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`\n :return: An instance.\n '''\n try:\n jsonfilecontent = json.loads(open(json_filename, 'r').read())\n except ValueError as exc:\n raise CredentialsFormatError(msg=\"Invalid JSON syntax: \"+str(exc))\n instance = PIDClientCredentials(credentials_filename=json_filename,**jsonfilecontent)\n return instance", "response": "Load a new instance of a PIDClientCredentials with information read from a local json file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef fixture(app, fixtures, fixtures_dir='fixtures', raise_does_not_exist=False,\n reversible=True, models=[]):\n \"\"\"\n Load fixtures using a data migration.\n\n The migration will by default provide a rollback, deleting items by primary\n key. This is not always what you want ; you may set reversible=False to\n prevent rolling back.\n\n Usage:\n\n import myapp\n import anotherapp\n\n operations = [\n migrations.RunPython(**fixture(myapp, 'eggs.yaml')),\n migrations.RunPython(**fixture(anotherapp, ['sausage.json', 'walks.yaml']))\n migrations.RunPython(**fixture(yap, ['foo.json'], reversible=False))\n ]\n \"\"\"\n fixture_path = os.path.join(app.__path__[0], fixtures_dir)\n if isinstance(fixtures, string_types):\n fixtures = [fixtures]\n\n def get_format(fixture):\n return os.path.splitext(fixture)[1][1:]\n\n def get_objects():\n for fixture in fixtures:\n with open(os.path.join(fixture_path, fixture), 'rb') as f:\n objects = serializers.deserialize(get_format(fixture),\n f,\n ignorenonexistent=True)\n for obj in objects:\n yield obj\n\n def patch_apps(func):\n \"\"\"\n Patch the app registry.\n\n Note that this is necessary so that the Deserializer does not use the\n current version of the model, which may not necessarily be representative\n of the model the fixture was created for.\n \"\"\"\n @wraps(func)\n def inner(apps, schema_editor):\n try:\n # Firstly patch the serializers registry\n original_apps = django.core.serializers.python.apps\n django.core.serializers.python.apps = apps\n return func(apps, schema_editor)\n\n finally:\n # Ensure we always unpatch the serializers registry\n django.core.serializers.python.apps = original_apps\n\n return inner\n\n @patch_apps\n def load_fixture(apps, schema_editor):\n for obj in get_objects():\n obj.save()\n\n @patch_apps\n def unload_fixture(apps, schema_editor):\n for obj in get_objects():\n model = apps.get_model(app.__name__, obj.object.__class__.__name__)\n kwargs = dict()\n if 'id' in obj.object.__dict__:\n kwargs.update(id=obj.object.__dict__.get('id'))\n elif 'slug' in obj.object.__dict__:\n kwargs.update(slug=obj.object.__dict__.get('slug'))\n else:\n kwargs.update(**obj.object.__dict__)\n try:\n model.objects.get(**kwargs).delete()\n except model.DoesNotExist:\n if not raise_does_not_exist:\n raise FixtureObjectDoesNotExist((\"Model %s instance with \"\n \"kwargs %s does not exist.\"\n % (model, kwargs)))\n\n kwargs = dict(code=load_fixture)\n\n if reversible:\n kwargs['reverse_code'] = unload_fixture\n\n return kwargs", "response": "Load a set of fixtures into the app registry."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets all non - zero bits in the set", "response": "def nonzero(self):\n \"\"\"\n Get all non-zero bits\n \"\"\"\n return [i for i in xrange(self.size()) if self.test(i)]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tohexstring(self):\n val = self.tostring()\n st = \"{0:0x}\".format(int(val, 2))\n return st.zfill(len(self.bitmap)*2)", "response": "Returns a hexadecimal string representation of the current bitmap entry."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconstructing BitMap from hex string", "response": "def fromhexstring(cls, hexstring):\n \"\"\"\n Construct BitMap from hex string\n \"\"\"\n bitstring = format(int(hexstring, 16), \"0\" + str(len(hexstring)/4) + \"b\")\n return cls.fromstring(bitstring)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fromstring(cls, bitstring):\n nbits = len(bitstring)\n bm = cls(nbits)\n for i in xrange(nbits):\n if bitstring[-i-1] == '1':\n bm.set(i)\n elif bitstring[-i-1] != '0':\n raise Exception(\"Invalid bit string!\")\n return bm", "response": "Construct a BitMap from a string."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets version information or return default if unable to do so.", "response": "def get_versions():\n \"\"\"Get version information or return default if unable to do so.\"\"\"\n # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have\n # __file__, we can work backwards from there to the root. Some\n # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which\n # case we can only use expanded keywords.\n\n cfg = get_config()\n verbose = cfg.verbose\n\n try:\n return git_versions_from_keywords(get_keywords(), cfg.tag_prefix,\n verbose)\n except NotThisMethod:\n pass\n\n try:\n root = os.path.realpath(__file__)\n # versionfile_source is the relative path from the top of the source\n # tree (where the .git directory might live) to this file. Invert\n # this to find the root from __file__.\n for i in cfg.versionfile_source.split('/'):\n root = os.path.dirname(root)\n except NameError:\n return {\"version\": \"0+unknown\", \"full-revisionid\": None,\n \"dirty\": None,\n \"error\": \"unable to find root of source tree\"}\n\n try:\n pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)\n return render(pieces, cfg.style)\n except NotThisMethod:\n pass\n\n try:\n if cfg.parentdir_prefix:\n return versions_from_parentdir(cfg.parentdir_prefix, root, verbose)\n except NotThisMethod:\n pass\n\n _version_path = os.path.join(os.path.dirname(__file__), '_version')\n if os.path.exists(_version_path):\n with open(_version_path) as f:\n l = f.readline().strip()\n return {\n 'version': l,\n 'error': None,\n 'dirty': None,\n 'full-revisionid': l\n }\n\n return {\"version\": \"0+unknown\", \"full-revisionid\": None,\n \"dirty\": None,\n \"error\": \"unable to compute version\"}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets a value that can be the boolean representation of a string or a boolean itself and returns It as a boolean.", "response": "def get_valid_https_verify(value):\n '''\n Get a value that can be the boolean representation of a string\n or a boolean itself and returns It as a boolean.\n If this is not the case, It returns a string.\n\n :value: The HTTPS_verify input value. A string can be passed as a path\n to a CA_BUNDLE certificate\n :returns: True, False or a string.\n '''\n http_verify_value = value\n bool_values = {'false': False, 'true': True}\n\n if isinstance(value, bool):\n http_verify_value = value\n elif (isinstance(value, str) or isinstance(value, unicode)) and value.lower() in bool_values.keys():\n http_verify_value = bool_values[value.lower()]\n\n return http_verify_value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef setup(self):\n super(CleanCSSFilter, self).setup()\n self.root = current_app.config.get('COLLECT_STATIC_ROOT')", "response": "Initialize filter just before it will be used."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rebase_opt(self):\n if not hasattr(self, '_rebase_opt'):\n # out = b\"MAJOR.MINOR.REVISION\" // b\"3.4.19\" or b\"4.0.0\"\n out, err = Popen(\n ['cleancss', '--version'], stdout=PIPE).communicate()\n ver = int(out[:out.index(b'.')])\n self._rebase_opt = ['--root', self.root] if ver == 3 else []\n return self._rebase_opt", "response": "Determine which option name to use."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrap translation in Angular module.", "response": "def output(self, _in, out, **kwargs):\n \"\"\"Wrap translation in Angular module.\"\"\"\n out.write(\n 'angular.module(\"{0}\", [\"gettext\"]).run('\n '[\"gettextCatalog\", function (gettextCatalog) {{'.format(\n self.catalog_name\n )\n )\n out.write(_in.read())\n out.write('}]);')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef input(self, _in, out, **kwargs):\n language_code = _re_language_code.search(_in.read()).group(\n 'language_code'\n )\n _in.seek(0) # move at the begining after matching the language\n catalog = read_po(_in)\n out.write('gettextCatalog.setStrings(\"{0}\", '.format(language_code))\n out.write(json.dumps({\n key: value.string for key, value in catalog._messages.items()\n if key and value.string\n }))\n out.write(');')", "response": "Process individual translation file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting user and check if password is valid on the database.", "response": "def get_user_and_check_auth(self, username, password):\n \"\"\"Check the combination username/password that is valid on the\n database.\n \"\"\"\n constraint = sql.or_(\n models.USERS.c.name == username,\n models.USERS.c.email == username\n )\n\n user = self.identity_from_db(models.USERS, constraint)\n if user is None:\n raise dci_exc.DCIException('User %s does not exists.' % username,\n status_code=401)\n\n return user, auth.check_passwords_equal(password, user.password)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nquery the Github API to retrieve the needed infos.", "response": "def retrieve_info(self):\n \"\"\"Query the Github API to retrieve the needed infos.\"\"\"\n\n path = urlparse(self.url).path\n path = path.split('/')[1:]\n\n sanity_filter = re.compile('[\\da-z-_]+', re.IGNORECASE)\n self.product = sanity_filter.match(path[0]).group(0)\n self.component = sanity_filter.match(path[1]).group(0)\n self.issue_id = int(path[3])\n\n github_url = '%s/%s/%s/issues/%s' % (_URL_BASE,\n self.product,\n self.component,\n self.issue_id)\n\n result = requests.get(github_url)\n self.status_code = result.status_code\n\n if result.status_code == 200:\n result = result.json()\n self.title = result['title']\n self.reporter = result['user']['login']\n if result['assignee'] is not None:\n self.assignee = result['assignee']['login']\n self.status = result['state']\n self.created_at = result['created_at']\n self.updated_at = result['updated_at']\n self.closed_at = result['closed_at']\n elif result.status_code == 404:\n self.title = 'private issue'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncache the return value in the correct cache directory. Set 'method' to false for static methods.", "response": "def disk_cache(cls, basename, function, *args, method=True, **kwargs):\n \"\"\"\n Cache the return value in the correct cache directory. Set 'method' to\n false for static methods.\n \"\"\"\n @utility.disk_cache(basename, cls.directory(), method=method)\n def wrapper(*args, **kwargs):\n return function(*args, **kwargs)\n\n return wrapper(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef download(cls, url, filename=None):\n return utility.download(url, cls.directory(), filename)", "response": "Download a file into the correct cache directory."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the path that should be used for caching. Different for all subclasses.", "response": "def directory(cls, prefix=None):\n \"\"\"\n Path that should be used for caching. Different for all subclasses.\n \"\"\"\n prefix = prefix or utility.read_config().directory\n name = cls.__name__.lower()\n directory = os.path.expanduser(os.path.join(prefix, name))\n utility.ensure_directory(directory)\n return directory"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the last rconfiguration_id of the last job run by the remoteci.", "response": "def get_last_rconfiguration_id(topic_id, remoteci_id, db_conn=None):\n \"\"\"Get the rconfiguration_id of the last job run by the remoteci.\n\n :param topic_id: the topic\n :param remoteci_id: the remoteci id\n :return: last rconfiguration_id of the remoteci\n \"\"\"\n db_conn = db_conn or flask.g.db_conn\n __TABLE = models.JOBS\n query = sql.select([__TABLE.c.rconfiguration_id]). \\\n order_by(sql.desc(__TABLE.c.created_at)). \\\n where(sql.and_(__TABLE.c.topic_id == topic_id,\n __TABLE.c.remoteci_id == remoteci_id)). \\\n limit(1)\n rconfiguration_id = db_conn.execute(query).fetchone()\n if rconfiguration_id is not None:\n return str(rconfiguration_id[0])\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a remoteci configuration. This will iterate over each available rconfiguration in a round robin manner depending on the last rconfiguration used by the remoteci.", "response": "def get_remoteci_configuration(topic_id, remoteci_id, db_conn=None):\n \"\"\"Get a remoteci configuration. This will iterate over each\n configuration in a round robin manner depending on the last\n rconfiguration used by the remoteci.\"\"\"\n\n db_conn = db_conn or flask.g.db_conn\n last_rconfiguration_id = get_last_rconfiguration_id(\n topic_id, remoteci_id, db_conn=db_conn)\n _RCONFIGURATIONS = models.REMOTECIS_RCONFIGURATIONS\n _J_RCONFIGURATIONS = models.JOIN_REMOTECIS_RCONFIGURATIONS\n query = sql.select([_RCONFIGURATIONS]). \\\n select_from(_J_RCONFIGURATIONS.\n join(_RCONFIGURATIONS)). \\\n where(_J_RCONFIGURATIONS.c.remoteci_id == remoteci_id)\n query = query.where(sql.and_(_RCONFIGURATIONS.c.state != 'archived',\n _RCONFIGURATIONS.c.topic_id == topic_id))\n query = query.order_by(sql.desc(_RCONFIGURATIONS.c.created_at))\n query = query.order_by(sql.asc(_RCONFIGURATIONS.c.name))\n all_rconfigurations = db_conn.execute(query).fetchall()\n\n if len(all_rconfigurations) > 0:\n for i in range(len(all_rconfigurations)):\n if str(all_rconfigurations[i]['id']) == last_rconfiguration_id:\n # if i==0, then indice -1 is the last element\n return all_rconfigurations[i - 1]\n return all_rconfigurations[0]\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef send(self, request: Request) -> None:\n\n handler_factories = self._registry.lookup(request)\n if len(handler_factories) != 1:\n raise ConfigurationException(\"There is no handler registered for this request\")\n handler = handler_factories[0]()\n handler.handle(request)", "response": "Dispatches a request. Expects one and one only target handler."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndispatching a request. Expects zero or more target handlers", "response": "def publish(self, request: Request) -> None:\n \"\"\"\n Dispatches a request. Expects zero or more target handlers\n :param request: The request to dispatch\n :return: None.\n \"\"\"\n handler_factories = self._registry.lookup(request)\n for factory in handler_factories:\n handler = factory()\n handler.handle(request)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndispatch a request over middleware. Returns when the request is sent to the broker.", "response": "def post(self, request: Request) -> None:\n \"\"\"\n Dispatches a request over middleware. Returns when message put onto outgoing channel by producer,\n does not wait for response from a consuming application i.e. is fire-and-forget\n :param request: The request to dispatch\n :return: None\n \"\"\"\n\n if self._producer is None:\n raise ConfigurationException(\"Command Processor requires a BrightsideProducer to post to a Broker\")\n if self._message_mapper_registry is None:\n raise ConfigurationException(\"Command Processor requires a BrightsideMessage Mapper Registry to post to a Broker\")\n\n message_mapper = self._message_mapper_registry.lookup(request)\n message = message_mapper(request)\n self._message_store.add(message)\n self._producer.send(message)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ignore_whitespace_text_nodes(cls, wrapped_node):\n for child in wrapped_node.children:\n if child.is_text and child.value.strip() == '':\n child.delete()\n else:\n cls.ignore_whitespace_text_nodes(child)", "response": "Recursively delete any text nodes containing nothing but whitespace in the given node."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_ns_info_from_node_name(self, name, impl_node):\n if '}' in name:\n ns_uri, name = name.split('}')\n ns_uri = ns_uri[1:]\n prefix = self.get_ns_prefix_for_uri(impl_node, ns_uri)\n elif ':' in name:\n prefix, name = name.split(':')\n ns_uri = self.get_ns_uri_for_prefix(impl_node, prefix)\n if ns_uri is None:\n raise exceptions.UnknownNamespaceException(\n \"Prefix '%s' does not have a defined namespace URI\"\n % prefix)\n else:\n prefix, ns_uri = None, None\n return prefix, name, ns_uri", "response": "Returns a three - element tuple with the prefix local name and namespace URI for the given element or attribute name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef up(self, count=1, to_name=None):\n elem = self._element\n up_count = 0\n while True:\n # Don't go up beyond the document root\n if elem.is_root or elem.parent is None:\n break\n elem = elem.parent\n if to_name is None:\n up_count += 1\n if up_count >= count:\n break\n else:\n if elem.name == to_name:\n break\n return Builder(elem)", "response": "Return a Builder representing an ancestor of the current element."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds a child element to the node with the given name.", "response": "def element(self, *args, **kwargs):\n \"\"\"\n Add a child element to the :class:`xml4h.nodes.Element` node\n represented by this Builder.\n\n :return: a new Builder that represents the child element.\n\n Delegates to :meth:`xml4h.nodes.Element.add_element`.\n \"\"\"\n child_element = self._element.add_element(*args, **kwargs)\n return Builder(child_element)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding one or more attributes to the node represented by this Builder.", "response": "def attributes(self, *args, **kwargs):\n \"\"\"\n Add one or more attributes to the :class:`xml4h.nodes.Element` node\n represented by this Builder.\n\n :return: the current Builder.\n\n Delegates to :meth:`xml4h.nodes.Element.set_attributes`.\n \"\"\"\n self._element.set_attributes(*args, **kwargs)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef processing_instruction(self, target, data):\n self._element.add_instruction(target, data)\n return self", "response": "Adds a processing instruction node to the current node represented by this Builder."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the namespace prefix of the node represented by this Builder.", "response": "def ns_prefix(self, prefix, ns_uri):\n \"\"\"\n Set the namespace prefix of the :class:`xml4h.nodes.Element` node\n represented by this Builder.\n\n :return: the current Builder.\n\n Delegates to :meth:`xml4h.nodes.Element.set_ns_prefix`.\n \"\"\"\n self._element.set_ns_prefix(prefix, ns_uri)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef createCertRequest(pkey, digest=\"sha256\"):\n req = crypto.X509Req()\n\n req.get_subject().C = \"FR\"\n req.get_subject().ST = \"IDF\"\n req.get_subject().L = \"Paris\"\n req.get_subject().O = \"RedHat\" # noqa\n req.get_subject().OU = \"DCI\"\n req.get_subject().CN = \"DCI-remoteCI\"\n\n req.set_pubkey(pkey)\n req.sign(pkey, digest)\n return req", "response": "Create a certificate request."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef verify_existence_and_get(id, table, name=None, get_id=False):\n\n where_clause = table.c.id == id\n if name:\n where_clause = table.c.name == name\n\n if 'state' in table.columns:\n where_clause = sql.and_(table.c.state != 'archived', where_clause)\n\n query = sql.select([table]).where(where_clause)\n result = flask.g.db_conn.execute(query).fetchone()\n\n if result is None:\n raise dci_exc.DCIException('Resource \"%s\" not found.' % id,\n status_code=404)\n if get_id:\n return result.id\n return result", "response": "Verify the existence of a resource in the database and then return it if it exists."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving the list of topics IDs a user has access to.", "response": "def user_topic_ids(user):\n \"\"\"Retrieve the list of topics IDs a user has access to.\"\"\"\n\n if user.is_super_admin() or user.is_read_only_user():\n query = sql.select([models.TOPICS])\n else:\n query = (sql.select([models.JOINS_TOPICS_TEAMS.c.topic_id])\n .select_from(\n models.JOINS_TOPICS_TEAMS.join(\n models.TOPICS, sql.and_(models.JOINS_TOPICS_TEAMS.c.topic_id == models.TOPICS.c.id, # noqa\n models.TOPICS.c.state == 'active')) # noqa\n ).where(\n sql.or_(models.JOINS_TOPICS_TEAMS.c.team_id.in_(user.teams_ids), # noqa\n models.JOINS_TOPICS_TEAMS.c.team_id.in_(user.child_teams_ids)))) # noqa\n\n rows = flask.g.db_conn.execute(query).fetchall()\n return [str(row[0]) for row in rows]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef verify_team_in_topic(user, topic_id):\n if user.is_super_admin() or user.is_read_only_user():\n return\n if str(topic_id) not in user_topic_ids(user):\n raise dci_exc.Unauthorized()", "response": "Verify that the user s team does belong to the given topic."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nformat the level 1 table.", "response": "def _format_level_1(rows, root_table_name):\n \"\"\"\n Transform sqlalchemy source:\n [{'a_id' : 'id1',\n 'a_name' : 'name1,\n 'b_id' : 'id2',\n 'b_name' : 'name2},\n {'a_id' : 'id3',\n 'a_name' : 'name3,\n 'b_id' : 'id4',\n 'b_name' : 'name4}\n ]\n to\n [{'id' : 'id1',\n 'name': 'name2',\n 'b' : {'id': 'id2', 'name': 'name2'},\n {'id' : 'id3',\n 'name': 'name3',\n 'b' : {'id': 'id4', 'name': 'name4'}\n ]\n \"\"\"\n result_rows = []\n for row in rows:\n row = dict(row)\n result_row = {}\n prefixes_to_remove = []\n for field in row:\n if field.startswith('next_topic'):\n prefix = 'next_topic'\n suffix = field[11:]\n if suffix == 'id_1':\n suffix = 'id'\n else:\n prefix, suffix = field.split('_', 1)\n if suffix == 'id' and row[field] is None:\n prefixes_to_remove.append(prefix)\n if prefix not in result_row:\n result_row[prefix] = {suffix: row[field]}\n else:\n result_row[prefix].update({suffix: row[field]})\n # remove field with id == null\n for prefix_to_remove in prefixes_to_remove:\n result_row.pop(prefix_to_remove)\n root_table_fields = result_row.pop(root_table_name)\n result_row.update(root_table_fields)\n result_rows.append(result_row)\n return result_rows"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _format_level_2(rows, list_embeds, embed_many):\n\n def _uniqify_list(list_of_dicts):\n # list() for py34\n result = []\n set_ids = set()\n for v in list_of_dicts:\n if v['id'] in set_ids:\n continue\n set_ids.add(v['id'])\n result.append(v)\n return result\n\n row_ids_to_embed_values = {}\n for row in rows:\n # for each row, associate rows's id -> {all embeds values}\n if row['id'] not in row_ids_to_embed_values:\n row_ids_to_embed_values[row['id']] = {}\n # add embeds values to the current row\n for embd in list_embeds:\n if embd not in row:\n continue\n if embd not in row_ids_to_embed_values[row['id']]:\n # create a list or a dict depending on embed_many\n if embed_many[embd]:\n row_ids_to_embed_values[row['id']][embd] = [row[embd]]\n else:\n row_ids_to_embed_values[row['id']][embd] = row[embd]\n else:\n if embed_many[embd]:\n row_ids_to_embed_values[row['id']][embd].append(row[embd])\n # uniqify each embed list\n for embd in list_embeds:\n if embd in row_ids_to_embed_values[row['id']]:\n embed_values = row_ids_to_embed_values[row['id']][embd]\n if isinstance(embed_values, list):\n row_ids_to_embed_values[row['id']][embd] = _uniqify_list(embed_values) # noqa\n else:\n row_ids_to_embed_values[row['id']][embd] = {}\n if embed_many[embd]:\n row_ids_to_embed_values[row['id']][embd] = []\n\n # last loop over the initial rows in order to keep the ordering\n result = []\n # if row id in seen set then it means the row has been completely processed\n seen = set()\n for row in rows:\n if row['id'] in seen:\n continue\n seen.add(row['id'])\n new_row = {}\n # adds level 1 fields\n for field in row:\n if field not in list_embeds:\n new_row[field] = row[field]\n # adds all level 2 fields\n # list() for py34\n row_ids_to_embed_values_keys = list(row_ids_to_embed_values[new_row['id']].keys()) # noqa\n row_ids_to_embed_values_keys.sort()\n # adds the nested fields if there is somes\n for embd in list_embeds:\n if embd in row_ids_to_embed_values_keys:\n if '.' in embd:\n prefix, suffix = embd.split('.', 1)\n new_row[prefix][suffix] = row_ids_to_embed_values[new_row['id']][embd] # noqa\n else:\n new_row[embd] = row_ids_to_embed_values[new_row['id']][embd] # noqa\n else:\n new_row_embd_value = {}\n if embed_many[embd]:\n new_row_embd_value = []\n if '.' in embd:\n prefix, suffix = embd.split('.', 1)\n new_row[prefix][suffix] = new_row_embd_value\n else:\n new_row[embd] = new_row_embd_value\n # row is complete !\n result.append(new_row)\n return result", "response": "This function is used to format the level 2 of the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef common_values_dict():\n now = datetime.datetime.utcnow().isoformat()\n etag = utils.gen_etag()\n values = {\n 'id': utils.gen_uuid(),\n 'created_at': now,\n 'updated_at': now,\n 'etag': etag\n }\n\n return values", "response": "Build a basic values object used in every create method."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes the query and return the result.", "response": "def execute(self, fetchall=False, fetchone=False, use_labels=True):\n \"\"\"\n :param fetchall: get all rows\n :param fetchone: get only one row\n :param use_labels: prefix row columns names by the table name\n :return:\n \"\"\"\n query = self.get_query(use_labels=use_labels)\n if fetchall:\n return flask.g.db_conn.execute(query).fetchall()\n elif fetchone:\n return flask.g.db_conn.execute(query).fetchone()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns some information about the currently authenticated identity", "response": "def get_identity(identity):\n \"\"\"Returns some information about the currently authenticated identity\"\"\"\n return flask.Response(\n json.dumps(\n {\n 'identity': {\n 'id': identity.id,\n 'etag': identity.etag,\n 'name': identity.name,\n 'fullname': identity.fullname,\n 'email': identity.email,\n 'timezone': identity.timezone,\n 'teams': _encode_dict(identity.teams)\n }\n }\n ), 200,\n headers={'ETag': identity.etag},\n content_type='application/json'\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_issues_by_resource(resource_id, table):\n\n v1_utils.verify_existence_and_get(resource_id, table)\n\n # When retrieving the issues for a job, we actually retrieve\n # the issues attach to the job itself + the issues attached to\n # the components the job has been run with.\n if table.name == 'jobs':\n JJI = models.JOIN_JOBS_ISSUES\n JJC = models.JOIN_JOBS_COMPONENTS\n JCI = models.JOIN_COMPONENTS_ISSUES\n\n # Get all the issues attach to all the components attach to a job\n j1 = sql.join(\n _TABLE,\n sql.join(\n JCI,\n JJC,\n sql.and_(\n JCI.c.component_id == JJC.c.component_id,\n JJC.c.job_id == resource_id,\n ),\n ),\n _TABLE.c.id == JCI.c.issue_id,\n )\n\n query = sql.select([_TABLE]).select_from(j1)\n rows = flask.g.db_conn.execute(query)\n rows = [dict(row) for row in rows]\n\n # Get all the issues attach to a job\n j2 = sql.join(\n _TABLE,\n JJI,\n sql.and_(\n _TABLE.c.id == JJI.c.issue_id,\n JJI.c.job_id == resource_id\n )\n )\n query2 = sql.select([_TABLE]).select_from(j2)\n rows2 = flask.g.db_conn.execute(query2)\n rows += [dict(row) for row in rows2]\n\n # When retrieving the issues for a component, we only retrieve the\n # issues attached to the specified component.\n else:\n JCI = models.JOIN_COMPONENTS_ISSUES\n\n query = (sql.select([_TABLE])\n .select_from(JCI.join(_TABLE))\n .where(JCI.c.component_id == resource_id))\n\n rows = flask.g.db_conn.execute(query)\n rows = [dict(row) for row in rows]\n\n for row in rows:\n if row['tracker'] == 'github':\n l_tracker = github.Github(row['url'])\n elif row['tracker'] == 'bugzilla':\n l_tracker = bugzilla.Bugzilla(row['url'])\n row.update(l_tracker.dump())\n\n return flask.jsonify({'issues': rows,\n '_meta': {'count': len(rows)}})", "response": "Get all the issues attached to a specific job."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unattach_issue(resource_id, issue_id, table):\n\n v1_utils.verify_existence_and_get(issue_id, _TABLE)\n if table.name == 'jobs':\n join_table = models.JOIN_JOBS_ISSUES\n where_clause = sql.and_(join_table.c.job_id == resource_id,\n join_table.c.issue_id == issue_id)\n else:\n join_table = models.JOIN_COMPONENTS_ISSUES\n where_clause = sql.and_(join_table.c.component_id == resource_id,\n join_table.c.issue_id == issue_id)\n\n query = join_table.delete().where(where_clause)\n result = flask.g.db_conn.execute(query)\n\n if not result.rowcount:\n raise dci_exc.DCIConflict('%s_issues' % table.name, issue_id)\n\n return flask.Response(None, 204, content_type='application/json')", "response": "Unattach an issue from a specific job."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nattaching an issue to a specific job.", "response": "def attach_issue(resource_id, table, user_id):\n \"\"\"Attach an issue to a specific job.\"\"\"\n data = schemas.issue.post(flask.request.json)\n issue = _get_or_create_issue(data)\n\n # Second, insert a join record in the JOIN_JOBS_ISSUES or\n # JOIN_COMPONENTS_ISSUES database.\n if table.name == 'jobs':\n join_table = models.JOIN_JOBS_ISSUES\n else:\n join_table = models.JOIN_COMPONENTS_ISSUES\n\n key = '%s_id' % table.name[0:-1]\n query = join_table.insert().values({\n 'user_id': user_id,\n 'issue_id': issue['id'],\n key: resource_id\n })\n\n try:\n flask.g.db_conn.execute(query)\n except sa_exc.IntegrityError:\n raise dci_exc.DCICreationConflict(join_table.name,\n '%s, issue_id' % key)\n\n result = json.dumps({'issue': dict(issue)})\n return flask.Response(result, 201, content_type='application/json')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef collect_staticroot_removal(app, blueprints):\n collect_root = app.extensions['collect'].static_root\n return [bp for bp in blueprints if (\n bp.has_static_folder and bp.static_folder != collect_root)]", "response": "Remove collect s static root folder from list."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_client(access_token):\n url = 'http://keycloak:8080/auth/admin/realms/dci-test/clients'\n r = requests.post(url,\n data=json.dumps(client_data),\n headers=get_auth_headers(access_token))\n if r.status_code in (201, 409):\n print('Keycloak client dci created successfully.')\n else:\n raise Exception(\n 'Error while creating Keycloak client dci:\\nstatus code %s\\n'\n 'error: %s' % (r.status_code, r.content)\n )", "response": "Create the dci client in the master realm."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_user_dci(access_token):\n user_data = {'username': 'dci',\n 'email': 'dci@distributed-ci.io',\n 'enabled': True,\n 'emailVerified': True,\n 'credentials': [{'type': 'password',\n 'value': 'dci'}]}\n r = requests.post('http://keycloak:8080/auth/admin/realms/dci-test/users',\n data=json.dumps(user_data),\n headers=get_auth_headers(access_token))\n if r.status_code in (201, 409):\n print('Keycloak user dci created successfully.')\n else:\n raise Exception('Error while creating user dci:\\nstatus code %s\\n'\n 'error: %s' % (r.status_code, r.content))", "response": "Create a dci user."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninitializes the object with the given parameters.", "response": "def initialize(self, name=None, dbname=None, base=None, generator=None,\n case=None, namespaces=None):\n\n self.name = none_or(name, str)\n \"\"\"\n The name of the site. : str | `None`\n \"\"\"\n\n self.dbname = none_or(dbname, str)\n \"\"\"\n The dbname of the site. : str | `None`\n \"\"\"\n\n self.base = none_or(base, str)\n \"\"\"\n TODO: ??? : str | `None`\n \"\"\"\n\n self.generator = none_or(generator, str)\n \"\"\"\n TODO: ??? : str | `None`\n \"\"\"\n\n self.case = none_or(case, str)\n \"\"\"\n TODO: ??? : str | `None`\n \"\"\"\n\n self.namespaces = none_or(namespaces, list)\n \"\"\"\n A list of :class:`mwtypes.Namespace` | `None`\n \"\"\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _serializeBooleans(params):\n serialized = {}\n for name, value in params.items():\n if value is True:\n value = 'true'\n elif value is False:\n value = 'false'\n serialized[name] = value\n return serialized\n\n for k, v in params.items():\n if isinstance(v, bool):\n params[k] = str(v).lower()", "response": "Convert all booleans to lowercase strings"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwraps function for requests.", "response": "def request(self, method, url, parameters=dict()):\n \"\"\"Requests wrapper function\"\"\"\n\n # The requests library uses urllib, which serializes to \"True\"/\"False\" while Pingdom requires lowercase\n parameters = self._serializeBooleans(parameters)\n\n headers = {'App-Key': self.apikey}\n if self.accountemail:\n headers.update({'Account-Email': self.accountemail})\n\n # Method selection handling\n if method.upper() == 'GET':\n response = requests.get(self.url + url, params=parameters,\n auth=(self.username, self.password),\n headers=headers)\n elif method.upper() == 'POST':\n response = requests.post(self.url + url, data=parameters,\n auth=(self.username, self.password),\n headers=headers)\n elif method.upper() == 'PUT':\n response = requests.put(self.url + url, data=parameters,\n auth=(self.username, self.password),\n headers=headers)\n elif method.upper() == 'DELETE':\n response = requests.delete(self.url + url, params=parameters,\n auth=(self.username, self.password),\n headers=headers)\n else:\n raise Exception(\"Invalid method in pingdom request\")\n\n # Store pingdom api limits\n self.shortlimit = response.headers.get(\n 'Req-Limit-Short',\n self.shortlimit)\n self.longlimit = response.headers.get(\n 'Req-Limit-Long',\n self.longlimit)\n\n # Verify OK response\n if response.status_code != 200:\n sys.stderr.write('ERROR from %s: %d' % (response.url,\n response.status_code))\n sys.stderr.write('Returned data: %s\\n' % response.json())\n response.raise_for_status()\n\n return response"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef actions(self, **parameters):\n\n # Warn user about unhandled parameters\n for key in parameters:\n if key not in ['from', 'to', 'limit', 'offset', 'checkids',\n 'contactids', 'status', 'via']:\n sys.stderr.write('%s not a valid argument for actions()\\n'\n % key)\n\n response = self.request('GET', 'actions', parameters)\n\n return response.json()['actions']", "response": "Returns a list of actions that have been generated for the current date and time."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef getChecks(self, **parameters):\n\n # Warn user about unhandled parameters\n for key in parameters:\n if key not in ['limit', 'offset', 'tags']:\n sys.stderr.write('%s not a valid argument for getChecks()\\n'\n % key)\n\n response = self.request('GET', 'checks', parameters)\n\n return [PingdomCheck(self, x) for x in response.json()['checks']]", "response": "Pulls all checks from the pingdom daemon."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a detailed description of a specified check.", "response": "def getCheck(self, checkid):\n \"\"\"Returns a detailed description of a specified check.\"\"\"\n\n check = PingdomCheck(self, {'id': checkid})\n check.getDetails()\n return check"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of Pingdom probe servers that are in use.", "response": "def probes(self, **kwargs):\n \"\"\"Returns a list of all Pingdom probe servers\n\n Parameters:\n\n * limit -- Limits the number of returned probes to the specified\n quantity\n Type: Integer\n\n * offset -- Offset for listing (requires limit).\n Type: Integer\n Default: 0\n\n * onlyactive -- Return only active probes\n Type: Boolean\n Default: False\n\n * includedeleted -- Include old probes that are no longer in use\n Type: Boolean\n Default: False\n\n Returned structure:\n [\n {\n 'id' : Unique probe id\n 'country' : Country\n 'city' : City\n 'name' : Name\n 'active' : True if probe is active\n 'hostname' : DNS name\n 'ip' : IP address\n 'countryiso': Country ISO code\n },\n ...\n ]\n \"\"\"\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['limit', 'offset', 'onlyactive', 'includedeleted']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of probes()\\n')\n\n return self.request(\"GET\", \"probes\", kwargs).json()['probes']"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef traceroute(self, host, probeid):\n\n response = self.request('GET', 'traceroute', {'host': host,\n 'probeid': probeid})\n return response.json()['traceroute']", "response": "Perform a traceroute to a specified target from a specified Pingdom\n probe."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of all contacts.", "response": "def getContacts(self, **kwargs):\n \"\"\"Returns a list of all contacts.\n\n Optional Parameters:\n\n * limit -- Limits the number of returned contacts to the specified\n quantity.\n Type: Integer\n Default: 100\n\n * offset -- Offset for listing (requires limit.)\n Type: Integer\n Default: 0\n\n Returned structure:\n [\n 'id' : Contact identifier\n 'name' : Contact name\n 'email' : Contact email\n 'cellphone' : Contact telephone\n 'countryiso' : Cellphone country ISO code\n 'defaultsmsprovider' : Default SMS provider\n 'directtwitter' : Send Tweets as direct messages\n 'twitteruser' : Twitter username\n 'paused' : True if contact is pasued\n 'iphonetokens' : iPhone tokens\n 'androidtokens' : android tokens\n ]\n \"\"\"\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['limit', 'offset']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of getContacts()\\n')\n\n return [PingdomContact(self, x) for x in\n self.request(\"GET\", \"notification_contacts\", kwargs).json()['contacts']]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef newContact(self, name, **kwargs):\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['email', 'cellphone', 'countrycode', 'countryiso',\n 'defaultsmsprovider', 'directtwitter',\n 'twitteruser']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of newContact()\\n')\n\n kwargs['name'] = name\n contactinfo = self.request(\"POST\", \"notification_contacts\",\n kwargs).json()['contact']\n\n return PingdomContact(self, contactinfo)", "response": "Create a new contact."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmodify a list of contacts.", "response": "def modifyContacts(self, contactids, paused):\n \"\"\"Modifies a list of contacts.\n\n Provide comma separated list of contact ids and desired paused state\n\n Returns status message\n \"\"\"\n\n response = self.request(\"PUT\", \"notification_contacts\", {'contactids': contactids,\n 'paused': paused})\n return response.json()['message']"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of PingdomEmailReport instances.", "response": "def getEmailReports(self):\n \"\"\"Returns a list of PingdomEmailReport instances.\"\"\"\n\n reports = [PingdomEmailReport(self, x) for x in\n self.request('GET',\n 'reports.email').json()['subscriptions']]\n\n return reports"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef newEmailReport(self, name, **kwargs):\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['checkid', 'frequency', 'contactids',\n 'additionalemails']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of newEmailReport()\\n')\n\n parameters = {'name': name}\n for key, value in kwargs.iteritems():\n parameters[key] = value\n\n return self.request('POST', 'reports.email',\n parameters).json()['message']", "response": "Creates a new email report for the specified checkid frequency contactids and additionalemails."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of PingdomSharedReport instances", "response": "def getSharedReports(self):\n \"\"\"Returns a list of PingdomSharedReport instances\"\"\"\n\n response = self.request('GET',\n 'reports.shared').json()['shared']['banners']\n\n reports = [PingdomSharedReport(self, x) for x in response]\n return reports"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a new shared report.", "response": "def newSharedReport(self, checkid, **kwargs):\n \"\"\"Create a shared report (banner).\n\n Returns status message for operation\n\n Optional parameters:\n\n * auto -- Automatic period (If false, requires: fromyear,\n frommonth, fromday, toyear, tomonth, today)\n Type: Boolean\n\n * type -- Banner type\n Type: String ['uptime', 'response']\n\n * fromyear -- Period start: year\n Type: Integer\n\n * frommonth -- Period start: month\n Type: Integer\n\n * fromday -- Period start: day\n Type: Integer\n\n * toyear -- Period end: year\n Type: Integer\n\n * tomonth -- Period end: month\n Type: Integer\n\n * today -- Period end: day\n Type: Integer\n \"\"\"\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['auto', 'type', 'fromyear', 'frommonth', 'fromday',\n 'toyear', 'tomonth', 'today', 'sharedtype']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of newSharedReport()\\n')\n\n parameters = {'checkid': checkid, 'sharedtype': 'banner'}\n for key, value in kwargs.iteritems():\n parameters[key] = value\n\n return self.request('POST', 'reports.shared',\n parameters).json()['message']"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef encry_decry_cascade(data, masterkey, bool_encry, assoc_data):\n engine1 = botan.cipher(algo=\"Serpent/GCM\", encrypt=bool_encry)\n engine2 = botan.cipher(algo=\"AES-256/GCM\", encrypt=bool_encry)\n engine3 = botan.cipher(algo=\"Twofish/GCM\", encrypt=bool_encry)\n\n hash1 = botan.hash_function(algo=\"SHA-256\")\n hash1.update(masterkey)\n hashed1 = hash1.final()\n\n hash2 = botan.hash_function(algo=\"SHA-256\")\n hash2.update(hashed1)\n hashed2 = hash2.final()\n\n engine1.set_key(key=masterkey)\n engine1.set_assoc_data(assoc_data)\n\n engine2.set_key(key=hashed1)\n engine2.set_assoc_data(assoc_data)\n\n engine3.set_key(key=hashed2)\n engine3.set_assoc_data(assoc_data)\n\n if bool_encry is True:\n nonce1 = generate_nonce_timestamp()\n nonce2 = generate_nonce_timestamp()\n nonce3 = generate_nonce_timestamp()\n\n engine1.start(nonce=nonce1)\n engine2.start(nonce=nonce2)\n engine3.start(nonce=nonce3)\n\n cipher1 = engine1.finish(data)\n cipher2 = engine2.finish(cipher1)\n cipher3 = engine3.finish(cipher2)\n return nonce1 + nonce2 + nonce3 + cipher3\n else:\n nonce1 = data[:__nonce_length__]\n nonce2 = data[__nonce_length__:__nonce_length__ * 2]\n nonce3 = data[__nonce_length__ * 2:__nonce_length__ * 3]\n\n encrypteddata = data[__nonce_length__ * 3:]\n\n engine1.start(nonce=nonce1)\n engine2.start(nonce=nonce2)\n engine3.start(nonce=nonce3)\n\n decrypteddata1 = engine3.finish(encrypteddata)\n if decrypteddata1 == b\"\":\n raise Exception(\"Integrity failure: Invalid passphrase or corrupted data\")\n decrypteddata2 = engine2.finish(decrypteddata1)\n if decrypteddata2 == b\"\":\n raise Exception(\"Integrity failure: Invalid passphrase or corrupted data\")\n decrypteddata3 = engine1.finish(decrypteddata2)\n if decrypteddata3 == b\"\":\n raise Exception(\"Integrity failure: Invalid passphrase or corrupted data\")\n else:\n return decrypteddata3", "response": "This function encryes the data with the master key and decrypts it with the master key."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nregisters the handler for the command or event.", "response": "def register(self, request_class: Request, handler_factory: Callable[[], Handler]) -> None:\n \"\"\"\n Register the handler for the command\n :param request_class: The command or event to dispatch. It must implement getKey()\n :param handler_factory: A factory method to create the handler to dispatch to\n :return:\n \"\"\"\n key = request_class.__name__\n is_command = request_class.is_command()\n is_event = request_class.is_event()\n is_present = key in self._registry\n if is_command and is_present:\n raise ConfigurationException(\"A handler for this request has already been registered\")\n elif is_event and is_present:\n self._registry[key].append(handler_factory)\n elif is_command or is_event:\n self._registry[key] = [handler_factory]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef lookup(self, request: Request) -> List[Callable[[], Handler]]:\n key = request.__class__.__name__\n if key not in self._registry:\n if request.is_command():\n raise ConfigurationException(\"There is no handler registered for this request\")\n elif request.is_event():\n return [] # type: Callable[[] Handler]\n\n return self._registry[key]", "response": "Look up the handler associated with a request - matches the key on the request to a registered handler\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef register(self, request_class: Request, mapper_func: Callable[[Request], BrightsideMessage]) -> None:\n\n key = request_class.__name__\n if key not in self._registry:\n self._registry[key] = mapper_func\n else:\n raise ConfigurationException(\"There is already a message mapper defined for this key; there can be only one\")", "response": "Adds a message mapper function to a factory using the requests key\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlooking up the message mapper function associated with this class.", "response": "def lookup(self, request_class: Request) -> Callable[[Request], BrightsideMessage]:\n \"\"\"\n Looks up the message mapper function associated with this class. Function should take in a Request derived class\n and return a BrightsideMessage derived class, for sending on the wire\n :param request_class:\n :return:\n \"\"\"\n key = request_class.__class__.__name__\n if key not in self._registry:\n raise ConfigurationException(\"There is no message mapper associated with this key; we require a mapper\")\n else:\n return self._registry[key]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef instance_of(*args):\n if len(args) == 2:\n # Standard mode\n value, ref_type = args\n if not isinstance(ref_type, set):\n # ref_type is a single type\n if isinstance(value, ref_type):\n return True\n else:\n raise HasWrongType(wrong_value=value, ref_type=ref_type)\n\n else:\n # ref_type is a set\n match = False\n # test against each of the provided types\n for ref in ref_type:\n if isinstance(value, ref):\n match = True\n break\n if match:\n return True\n else:\n raise HasWrongType(wrong_value=value, ref_type=ref_type,\n help_msg='Value should be an instance of any of {ref_type}')\n\n elif len(args) == 1:\n # Function generator mode\n ref_type = args[0]\n if not isinstance(ref_type, set):\n # ref_type is a single type\n def instance_of_ref(x):\n if isinstance(x, ref_type):\n return True\n else:\n raise HasWrongType(wrong_value=x, ref_type=ref_type)\n else:\n # ref_type is a set\n def instance_of_ref(x):\n match = False\n # test against each of the provided types\n for ref in ref_type:\n if isinstance(x, ref):\n match = True\n break\n if match:\n return True\n else:\n raise HasWrongType(wrong_value=x, ref_type=ref_type,\n help_msg='Value should be an instance of any of {ref_type}')\n\n instance_of_ref.__name__ = 'instance_of_{}'.format(ref_type)\n return instance_of_ref\n else:\n raise TypeError('instance_of expected 2 (normal) or 1 (function generator) arguments, got ' + str(len(args)))", "response": "This function returns a validation function that checks if a single element of the input array is an instance of the specified type."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef subclass_of(*args):\n if len(args) == 2:\n # Standard mode\n typ, ref_type = args\n if not isinstance(ref_type, set):\n # ref_type is a single type\n if issubclass(typ, ref_type):\n return True\n else:\n raise IsWrongType(wrong_value=typ, ref_type=ref_type)\n else:\n # ref_type is a set\n match = False\n # test against each of the provided types\n for ref in ref_type:\n if issubclass(typ, ref):\n match = True\n break\n if match:\n return True\n else:\n raise IsWrongType(wrong_value=typ, ref_type=ref_type,\n help_msg='Value should be a subclass of any of {ref_type}')\n\n elif len(args) == 1:\n # Function generator mode\n ref_type = args[0]\n if not isinstance(ref_type, set):\n def subclass_of_ref(x):\n if issubclass(x, ref_type):\n return True\n else:\n raise IsWrongType(wrong_value=x, ref_type=ref_type)\n else:\n # ref_type is a set\n def subclass_of_ref(x):\n match = False\n # test against each of the provided types\n for ref in ref_type:\n if issubclass(x, ref):\n match = True\n break\n if match:\n return True\n else:\n raise IsWrongType(wrong_value=x, ref_type=ref_type,\n help_msg='Value should be a subclass of any of {ref_type}')\n\n subclass_of_ref.__name__ = 'subclass_of_{}'.format(ref_type)\n return subclass_of_ref\n else:\n raise TypeError('subclass_of expected 2 (normal) or 1 (function generator) arguments, got ' + str(len(args)))", "response": "This function returns a validation function that checks if a class is a subclass of another class."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction decorator for caching pickleable return values on disk. Uses a hash computed from the function arguments for invalidation. If 'method', skip the first argument, usually being self or cls. The cache filepath is 'directory/basename-hash.pickle'.", "response": "def disk_cache(basename, directory, method=False):\n \"\"\"\n Function decorator for caching pickleable return values on disk. Uses a\n hash computed from the function arguments for invalidation. If 'method',\n skip the first argument, usually being self or cls. The cache filepath is\n 'directory/basename-hash.pickle'.\n \"\"\"\n directory = os.path.expanduser(directory)\n ensure_directory(directory)\n\n def wrapper(func):\n @functools.wraps(func)\n def wrapped(*args, **kwargs):\n key = (tuple(args), tuple(kwargs.items()))\n # Don't use self or cls for the invalidation hash.\n if method and key:\n key = key[1:]\n filename = '{}-{}.pickle'.format(basename, hash(key))\n filepath = os.path.join(directory, filename)\n if os.path.isfile(filepath):\n with open(filepath, 'rb') as handle:\n return pickle.load(handle)\n result = func(*args, **kwargs)\n with open(filepath, 'wb') as handle:\n pickle.dump(result, handle)\n return result\n return wrapped\n\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef download(url, directory, filename=None):\n if not filename:\n _, filename = os.path.split(url)\n directory = os.path.expanduser(directory)\n ensure_directory(directory)\n filepath = os.path.join(directory, filename)\n if os.path.isfile(filepath):\n return filepath\n print('Download', filepath)\n with urlopen(url) as response, open(filepath, 'wb') as file_:\n shutil.copyfileobj(response, file_)\n return filepath", "response": "Download a file and return its filename on the local file system."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ensure_directory(directory):\n directory = os.path.expanduser(directory)\n try:\n os.makedirs(directory)\n except OSError as e:\n if e.errno != errno.EEXIST:\n raise e", "response": "Create the directories along the provided directory path that do not exist."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef and_(*validation_func # type: ValidationFuncs\n ):\n # type: (...) -> Callable\n \"\"\"\n An 'and' validator: it returns `True` if all of the provided validators return `True`, or raises a\n `AtLeastOneFailed` failure on the first `False` received or `Exception` caught.\n\n Note that an implicit `and_` is performed if you provide a list of validators to any of the entry points\n (`validate`, `validation`/`validator`, `@validate_arg`, `@validate_out`, `@validate_field` ...)\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :return:\n \"\"\"\n\n validation_func = _process_validation_function_s(list(validation_func), auto_and_wrapper=False)\n\n if len(validation_func) == 1:\n return validation_func[0] # simplification for single validator case: no wrapper\n else:\n def and_v_(x):\n for validator in validation_func:\n try:\n res = validator(x)\n except Exception as e:\n # one validator was unhappy > raise\n raise AtLeastOneFailed(validation_func, x, cause=e)\n if not result_is_success(res):\n # one validator was unhappy > raise\n raise AtLeastOneFailed(validation_func, x)\n\n return True\n\n and_v_.__name__ = 'and({})'.format(get_callable_names(validation_func))\n return and_v_", "response": "A function that returns True if all of the provided validators return True or raises AtLeastOneFailed failure on the first False received or an exception caught."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef not_(validation_func, # type: ValidationFuncs\n catch_all=False # type: bool\n ):\n # type: (...) -> Callable\n \"\"\"\n Generates the inverse of the provided validation functions: when the validator returns `False` or raises a\n `Failure`, this function returns `True`. Otherwise it raises a `DidNotFail` failure.\n\n By default, exceptions of types other than `Failure` are not caught and therefore fail the validation\n (`catch_all=False`). To change this behaviour you can turn the `catch_all` parameter to `True`, in which case all\n exceptions will be caught instead of just `Failure`s.\n\n Note that you may use `not_all()` as a shortcut for\n `not_(and_())`\n\n :param validation_func: the base validation function. A callable, a tuple(callable, help_msg_str),\n a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param catch_all: an optional boolean flag. By default, only Failure are silently caught and turned into\n a 'ok' result. Turning this flag to True will assume that all exceptions should be caught and turned to a\n 'ok' result\n :return:\n \"\"\"\n\n def not_v_(x):\n try:\n res = validation_func(x)\n if not result_is_success(res): # inverse the result\n return True\n\n except Failure:\n return True # caught failure: always return True\n\n except Exception as e:\n if not catch_all:\n raise e\n else:\n return True # caught exception in 'catch_all' mode: return True\n\n # if we're here that's a failure\n raise DidNotFail(wrapped_func=validation_func, wrong_value=x, validation_outcome=res)\n\n not_v_.__name__ = 'not({})'.format(get_callable_name(validation_func))\n return not_v_", "response": "Generates a validation function that does not pass any value."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a function that returns True if at least one of the provided validators returns True.", "response": "def or_(*validation_func # type: ValidationFuncs\n ):\n # type: (...) -> Callable\n \"\"\"\n An 'or' validator: returns `True` if at least one of the provided validators returns `True`. All exceptions will be\n silently caught. In case of failure, a global `AllValidatorsFailed` failure will be raised, together with details\n about all validation results.\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :return:\n \"\"\"\n\n validation_func = _process_validation_function_s(list(validation_func), auto_and_wrapper=False)\n\n if len(validation_func) == 1:\n return validation_func[0] # simplification for single validator case\n else:\n def or_v_(x):\n for validator in validation_func:\n # noinspection PyBroadException\n try:\n res = validator(x)\n if result_is_success(res):\n # we can return : one validator was happy\n return True\n except Exception:\n # catch all silently\n pass\n\n # no validator accepted: gather details and raise\n raise AllValidatorsFailed(validation_func, x)\n\n or_v_.__name__ = 'or({})'.format(get_callable_names(validation_func))\n return or_v_"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef xor_(*validation_func # type: ValidationFuncs\n ):\n # type: (...) -> Callable\n \"\"\"\n A 'xor' validation function: returns `True` if exactly one of the provided validators returns `True`. All exceptions\n will be silently caught. In case of failure, a global `XorTooManySuccess` or `AllValidatorsFailed` will be raised,\n together with details about the various validation results.\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :return:\n \"\"\"\n\n validation_func = _process_validation_function_s(list(validation_func), auto_and_wrapper=False)\n\n if len(validation_func) == 1:\n return validation_func[0] # simplification for single validation function case\n else:\n def xor_v_(x):\n ok_validators = []\n for val_func in validation_func:\n # noinspection PyBroadException\n try:\n res = val_func(x)\n if result_is_success(res):\n ok_validators.append(val_func)\n except Exception:\n pass\n\n # return if were happy or not\n if len(ok_validators) == 1:\n # one unique validation function happy: success\n return True\n\n elif len(ok_validators) > 1:\n # several validation_func happy : fail\n raise XorTooManySuccess(validation_func, x)\n\n else:\n # no validation function happy, fail\n raise AllValidatorsFailed(validation_func, x)\n\n xor_v_.__name__ = 'xor({})'.format(get_callable_names(validation_func))\n return xor_v_", "response": "A function to be applied to a base validation function that returns True if exactly one of the provided validators returns True."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef not_all(*validation_func, # type: ValidationFuncs\n **kwargs\n ):\n # type: (...) -> Callable\n \"\"\"\n An alias for not_(and_(validators)).\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param catch_all: an optional boolean flag. By default, only Failure are silently caught and turned into\n a 'ok' result. Turning this flag to True will assume that all exceptions should be caught and turned to a\n 'ok' result\n :return:\n \"\"\"\n catch_all = pop_kwargs(kwargs, [('catch_all', False)])\n\n # in case this is a list, create a 'and_' around it (otherwise and_ will return the validation function without\n # wrapping it)\n main_validator = and_(*validation_func)\n return not_(main_validator, catch_all=catch_all)", "response": "Returns a new base validation function that does not validate the given base validation function."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef gen_secret(length=64):\n charset = string.ascii_letters + string.digits\n return ''.join(random.SystemRandom().choice(charset)\n for _ in range(length))", "response": "Generates a secret of given length"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef encryptfile(filename, passphrase, algo='srp'):\n try:\n if algo == \"srp\":\n header = b\"Cryptoshop srp \" + b_version\n crypto_algo = \"Serpent/GCM\"\n if algo == \"aes\":\n header = b\"Cryptoshop aes \" + b_version\n crypto_algo = \"AES-256/GCM\"\n if algo == \"twf\":\n header = b\"Cryptoshop twf \" + b_version\n crypto_algo = \"Twofish/GCM\"\n if algo != \"srp\" and algo != \"aes\" and algo != \"twf\":\n return \"No valid algo. Use 'srp' 'aes' or 'twf'\"\n outname = filename + \".cryptoshop\"\n\n internal_key = botan.rng().get(internal_key_length)\n\n # Passphrase derivation...\n salt = botan.rng().get(__salt_size__)\n masterkey = calc_derivation(passphrase=passphrase, salt=salt)\n\n # Encrypt internal key...\n encrypted_key = encry_decry_cascade(data=internal_key, masterkey=masterkey,\n bool_encry=True,\n assoc_data=header)\n with open(filename, 'rb') as filestream:\n file_size = os.stat(filename).st_size\n if file_size == 0:\n raise Exception(\"Error: You can't encrypt empty file.\")\n with open(str(outname), 'wb') as filestreamout:\n filestreamout.write(header)\n filestreamout.write(salt)\n filestreamout.write(encrypted_key)\n\n finished = False\n # the maximum of the progress bar is the total chunk to process. It's files_size // chunk_size\n bar = tqdm(range(file_size // __chunk_size__))\n while not finished:\n chunk = filestream.read(__chunk_size__)\n if len(chunk) == 0 or len(chunk) % __chunk_size__ != 0:\n finished = True\n # An encrypted-chunk output is nonce, gcmtag, and cipher-chunk concatenation.\n encryptedchunk = encry_decry_chunk(chunk=chunk, key=internal_key, bool_encry=True,\n algo=crypto_algo, assoc_data=header)\n filestreamout.write(encryptedchunk)\n bar.update(1)\n\n return \"successfully encrypted\"\n\n except IOError:\n exit(\"Error: file \\\"\" + filename + \"\\\" was not found.\")", "response": "Encrypt a file and write it with. cryptoshop extension."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef decryptfile(filename, passphrase):\n try:\n outname = os.path.splitext(filename)[0].split(\"_\")[-1] # create a string file name without extension.\n with open(filename, 'rb') as filestream:\n file_size = os.stat(filename).st_size\n if file_size == 0:\n raise Exception(\"Error: You can't decrypt empty file.\")\n\n fileheader = filestream.read(header_length)\n\n if fileheader == b\"Cryptoshop srp \" + b_version:\n decrypt_algo = \"Serpent/GCM\"\n if fileheader == b\"Cryptoshop aes \" + b_version:\n decrypt_algo = \"AES-256/GCM\"\n if fileheader == b\"Cryptoshop twf \" + b_version:\n decrypt_algo = \"Twofish/GCM\"\n if fileheader != b\"Cryptoshop srp \" + b_version and fileheader != b\"Cryptoshop aes \" + b_version and fileheader != b\"Cryptoshop twf \" + b_version:\n raise Exception(\"Integrity failure: Bad header\")\n\n salt = filestream.read(__salt_size__)\n encrypted_key = filestream.read(encrypted_key_length)\n\n # Derive the passphrase...\n masterkey = calc_derivation(passphrase=passphrase, salt=salt)\n\n # Decrypt internal key...\n try:\n internal_key = encry_decry_cascade(data=encrypted_key, masterkey=masterkey,\n bool_encry=False, assoc_data=fileheader)\n except Exception as e:\n return e\n\n with open(str(outname), 'wb') as filestreamout:\n files_size = os.stat(filename).st_size\n # the maximum of the progress bar is the total chunk to process. It's files_size // chunk_size\n bar = tqdm(range(files_size // __chunk_size__))\n while True:\n # Don't forget... an encrypted chunk is nonce, gcmtag, and cipher-chunk concatenation.\n encryptedchunk = filestream.read(__nonce_length__ + __gcmtag_length__ + __chunk_size__)\n if len(encryptedchunk) == 0:\n break\n\n # Chunk decryption.\n try:\n original = encry_decry_chunk(chunk=encryptedchunk, key=internal_key, algo=decrypt_algo,\n bool_encry=False, assoc_data=fileheader)\n except Exception as e:\n return e\n else:\n filestreamout.write(original)\n bar.update(1)\n\n return \"successfully decrypted\"\n\n except IOError:\n exit(\"Error: file \\\"\" + filename + \"\\\" was not found.\")", "response": "Decrypt a file and write corresponding decrypted file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsplits a sentence while preserving tags.", "response": "def _tokenize(cls, sentence):\n \"\"\"\n Split a sentence while preserving tags.\n \"\"\"\n while True:\n match = cls._regex_tag.search(sentence)\n if not match:\n yield from cls._split(sentence)\n return\n chunk = sentence[:match.start()]\n yield from cls._split(chunk)\n tag = match.group(0)\n yield tag\n sentence = sentence[(len(chunk) + len(tag)):]"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nimplement a distributed stategy for processing XML files. This function constructs a set of py:mod:`multiprocessing` threads (spread over multiple cores) and uses an internal queue to aggregate outputs. To use this function, implement a `process()` function that takes two arguments -- a :class:`mwxml.Dump` and the path the dump was loaded from. Anything that this function ``yield``s will be `yielded` in turn from the :func:`mwxml.map` function. :Parameters: paths : `iterable` ( `str` | `file` ) a list of paths to dump files to process process : `func` A function that takes a :class:`~mwxml.iteration.dump.Dump` and the path the dump was loaded from and yields threads : int the number of individual processing threads to spool up :Example: >>> import mwxml >>> files = [\"examples/dump.xml\", \"examples/dump2.xml\"] >>> >>> def page_info(dump, path): ... for page in dump: ... yield page.id, page.namespace, page.title ... >>> for id, namespace, title in mwxml.map(page_info, files): ... print(id, namespace, title) ...", "response": "def map(process, paths, threads=None):\n \"\"\"\n Implements a distributed stategy for processing XML files. This\n function constructs a set of py:mod:`multiprocessing` threads (spread over\n multiple cores) and uses an internal queue to aggregate outputs. To use\n this function, implement a `process()` function that takes two arguments\n -- a :class:`mwxml.Dump` and the path the dump was loaded\n from. Anything that this function ``yield``s will be `yielded` in turn\n from the :func:`mwxml.map` function.\n\n :Parameters:\n paths : `iterable` ( `str` | `file` )\n a list of paths to dump files to process\n process : `func`\n A function that takes a :class:`~mwxml.iteration.dump.Dump` and the\n path the dump was loaded from and yields\n threads : int\n the number of individual processing threads to spool up\n\n :Example:\n\n >>> import mwxml\n >>> files = [\"examples/dump.xml\", \"examples/dump2.xml\"]\n >>>\n >>> def page_info(dump, path):\n ... for page in dump:\n ... yield page.id, page.namespace, page.title\n ...\n >>> for id, namespace, title in mwxml.map(page_info, files):\n ... print(id, namespace, title)\n ...\n \"\"\"\n paths = [mwtypes.files.normalize_path(path) for path in paths]\n\n def process_path(path):\n dump = Dump.from_file(mwtypes.files.reader(path))\n yield from process(dump, path)\n\n yield from para.map(process_path, paths, mappers=threads)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dump(self):\n\n return {\n 'title': self.title,\n 'issue_id': self.issue_id,\n 'reporter': self.reporter,\n 'assignee': self.assignee,\n 'status': self.status,\n 'product': self.product,\n 'component': self.component,\n 'created_at': self.created_at,\n 'updated_at': self.updated_at,\n 'closed_at': self.closed_at,\n 'status_code': self.status_code\n }", "response": "Return the object itself."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef apply_on_each_func_args_sig(func,\n cur_args,\n cur_kwargs,\n sig, # type: Signature\n func_to_apply,\n func_to_apply_params_dict):\n \"\"\"\n Applies func_to_apply on each argument of func according to what's received in current call (cur_args, cur_kwargs).\n For each argument of func named 'att' in its signature, the following method is called:\n\n `func_to_apply(cur_att_value, func_to_apply_paramers_dict[att], func, att_name)`\n\n :param func:\n :param cur_args:\n :param cur_kwargs:\n :param sig:\n :param func_to_apply:\n :param func_to_apply_params_dict:\n :return:\n \"\"\"\n\n # match the received arguments with the signature to know who is who\n bound_values = sig.bind(*cur_args, **cur_kwargs)\n\n # add the default values in here to get a full list\n apply_defaults(bound_values)\n\n for att_name, att_value in bound_values.arguments.items():\n if att_name in func_to_apply_params_dict.keys():\n # value = a normal value, or cur_kwargs as a whole\n func_to_apply(att_value, func_to_apply_params_dict[att_name], func, att_name)", "response": "Applies func_to_apply on each argument of func according to what s received in current call."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef is_product_owner(self, team_id):\n\n if self.is_super_admin():\n return True\n team_id = uuid.UUID(str(team_id))\n return team_id in self.child_teams_ids", "response": "Ensure the user is a PRODUCT_OWNER."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntesting if user is in team", "response": "def is_in_team(self, team_id):\n \"\"\"Test if user is in team\"\"\"\n\n if self.is_super_admin():\n return True\n team_id = uuid.UUID(str(team_id))\n return team_id in self.teams or team_id in self.child_teams_ids"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring ther resource has the role REMOTECI.", "response": "def is_remoteci(self, team_id=None):\n \"\"\"Ensure ther resource has the role REMOTECI.\"\"\"\n if team_id is None:\n return self._is_remoteci\n team_id = uuid.UUID(str(team_id))\n if team_id not in self.teams_ids:\n return False\n return self.teams[team_id]['role'] == 'REMOTECI'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_feeder(self, team_id=None):\n if team_id is None:\n return self._is_feeder\n team_id = uuid.UUID(str(team_id))\n if team_id not in self.teams_ids:\n return False\n return self.teams[team_id]['role'] == 'FEEDER'", "response": "Ensure ther resource has the role FEEDER."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the node that contains this node or self if this node is the document.", "response": "def document(self):\n \"\"\"\n :return: the :class:`Document` node that contains this node,\n or ``self`` if this node is the document.\n \"\"\"\n if self.is_document:\n return self\n return self.adapter.wrap_document(self.adapter.impl_document)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef root(self):\n if self.is_root:\n return self\n return self.adapter.wrap_node(\n self.adapter.impl_root_element, self.adapter.impl_document,\n self.adapter)", "response": "Returns the root element of the document that contains this node or self if this node is the root element."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting a list of underlying implementation nodes into a list of NodeList s.", "response": "def _convert_nodelist(self, impl_nodelist):\n \"\"\"\n Convert a list of underlying implementation nodes into a list of\n *xml4h* wrapper nodes.\n \"\"\"\n nodelist = [\n self.adapter.wrap_node(n, self.adapter.impl_document, self.adapter)\n for n in impl_nodelist]\n return NodeList(nodelist)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parent(self):\n parent_impl_node = self.adapter.get_node_parent(self.impl_node)\n return self.adapter.wrap_node(\n parent_impl_node, self.adapter.impl_document, self.adapter)", "response": "get the parent of this node"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef children(self):\n impl_nodelist = self.adapter.get_node_children(self.impl_node)\n return self._convert_nodelist(impl_nodelist)", "response": "Return a list of child nodes of this node."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef child(self, local_name=None, name=None, ns_uri=None, node_type=None,\n filter_fn=None):\n \"\"\"\n :return: the first child node matching the given constraints, or \\\n *None* if there are no matching child nodes.\n\n Delegates to :meth:`NodeList.filter`.\n \"\"\"\n return self.children(name=name, local_name=local_name, ns_uri=ns_uri,\n node_type=node_type, filter_fn=filter_fn, first_only=True)", "response": "Returns the first child node matching the given constraints."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of this node s siblings nodes.", "response": "def siblings(self):\n \"\"\"\n :return: a list of this node's sibling nodes.\n :rtype: NodeList\n \"\"\"\n impl_nodelist = self.adapter.get_node_children(self.parent.impl_node)\n return self._convert_nodelist(\n [n for n in impl_nodelist if n != self.impl_node])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef siblings_before(self):\n impl_nodelist = self.adapter.get_node_children(self.parent.impl_node)\n before_nodelist = []\n for n in impl_nodelist:\n if n == self.impl_node:\n break\n before_nodelist.append(n)\n return self._convert_nodelist(before_nodelist)", "response": "returns a list of the siblings that occur before this node in the DOM."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of the siblings that occur after this node in the DOM.", "response": "def siblings_after(self):\n \"\"\"\n :return: a list of this node's siblings that occur *after* this\n node in the DOM.\n \"\"\"\n impl_nodelist = self.adapter.get_node_children(self.parent.impl_node)\n after_nodelist = []\n is_after_myself = False\n for n in impl_nodelist:\n if is_after_myself:\n after_nodelist.append(n)\n elif n == self.impl_node:\n is_after_myself = True\n return self._convert_nodelist(after_nodelist)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete(self, destroy=True):\n removed_child = self.adapter.remove_node_child(\n self.adapter.get_node_parent(self.impl_node), self.impl_node,\n destroy_node=destroy)\n if removed_child is not None:\n return self.adapter.wrap_node(removed_child, None, self.adapter)\n else:\n return None", "response": "Delete this node from the owning document."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nclones a node from another document to become a child of this node.", "response": "def clone_node(self, node):\n \"\"\"\n Clone a node from another document to become a child of this node, by\n copying the node's data into this document but leaving the node\n untouched in the source document. The node to be cloned can be\n a :class:`Node` based on the same underlying XML library implementation\n and adapter, or a \"raw\" node from that implementation.\n\n :param node: the node in another document to clone.\n :type node: xml4h or implementation node\n \"\"\"\n if isinstance(node, xml4h.nodes.Node):\n child_impl_node = node.impl_node\n else:\n child_impl_node = node # Assume it's a valid impl node\n self.adapter.import_node(self.impl_node, child_impl_node, clone=True)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntransplant a node from another document to become a child of this node.", "response": "def transplant_node(self, node):\n \"\"\"\n Transplant a node from another document to become a child of this node,\n removing it from the source document. The node to be transplanted can\n be a :class:`Node` based on the same underlying XML library\n implementation and adapter, or a \"raw\" node from that implementation.\n\n :param node: the node in another document to transplant.\n :type node: xml4h or implementation node\n \"\"\"\n if isinstance(node, xml4h.nodes.Node):\n child_impl_node = node.impl_node\n original_parent_impl_node = node.parent.impl_node\n else:\n child_impl_node = node # Assume it's a valid impl node\n original_parent_impl_node = self.adapter.get_node_parent(node)\n self.adapter.import_node(self.impl_node, child_impl_node,\n original_parent_impl_node, clone=False)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfind all the children of this node with optional name and namespace URI.", "response": "def find(self, name=None, ns_uri=None, first_only=False):\n \"\"\"\n Find :class:`Element` node descendants of this node, with optional\n constraints to limit the results.\n\n :param name: limit results to elements with this name.\n If *None* or ``'*'`` all element names are matched.\n :type name: string or None\n :param ns_uri: limit results to elements within this namespace URI.\n If *None* all elements are matched, regardless of namespace.\n :type ns_uri: string or None\n :param bool first_only: if *True* only return the first result node\n or *None* if there is no matching node.\n\n :returns: a list of :class:`Element` nodes matching any given\n constraints, or a single node if ``first_only=True``.\n \"\"\"\n if name is None:\n name = '*' # Match all element names\n if ns_uri is None:\n ns_uri = '*' # Match all namespaces\n impl_nodelist = self.adapter.find_node_elements(\n self.impl_node, name=name, ns_uri=ns_uri)\n if first_only:\n if impl_nodelist:\n return self.adapter.wrap_node(\n impl_nodelist[0], self.adapter.impl_document, self.adapter)\n else:\n return None\n return self._convert_nodelist(impl_nodelist)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfinding the first node descendant of this node that matches any optional constraints.", "response": "def find_first(self, name=None, ns_uri=None):\n \"\"\"\n Find the first :class:`Element` node descendant of this node that\n matches any optional constraints, or None if there are no matching\n elements.\n\n Delegates to :meth:`find` with ``first_only=True``.\n \"\"\"\n return self.find(name=name, ns_uri=ns_uri, first_only=True)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_doc(self, name=None, ns_uri=None, first_only=False):\n return self.document.find(name=name, ns_uri=ns_uri,\n first_only=first_only)", "response": "Find the document descendants of this node."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nserializes this node and its descendants to text and write it to a given writer.", "response": "def write(self, writer=None, encoding='utf-8', indent=0, newline='',\n omit_declaration=False, node_depth=0, quote_char='\"'):\n \"\"\"\n Serialize this node and its descendants to text, writing\n the output to a given *writer* or to stdout.\n\n :param writer: an object such as a file or stream to which XML text\n is sent. If *None* text is sent to :attr:`sys.stdout`.\n :type writer: a file, stream, etc or None\n :param string encoding: the character encoding for serialized text.\n :param indent: indentation prefix to apply to descendent nodes for\n pretty-printing. The value can take many forms:\n\n - *int*: the number of spaces to indent. 0 means no indent.\n - *string*: a literal prefix for indented nodes, such as ``\\\\t``.\n - *bool*: no indent if *False*, four spaces indent if *True*.\n - *None*: no indent\n :type indent: string, int, bool, or None\n :param newline: the string value used to separate lines of output.\n The value can take a number of forms:\n\n - *string*: the literal newline value, such as ``\\\\n`` or ``\\\\r``.\n An empty string means no newline.\n - *bool*: no newline if *False*, ``\\\\n`` newline if *True*.\n - *None*: no newline.\n :type newline: string, bool, or None\n :param boolean omit_declaration: if *True* the XML declaration header\n is omitted, otherwise it is included. Note that the declaration is\n only output when serializing an :class:`xml4h.nodes.Document` node.\n :param int node_depth: the indentation level to start at, such as 2 to\n indent output as if the given *node* has two ancestors.\n This parameter will only be useful if you need to output XML text\n fragments that can be assembled into a document. This parameter\n has no effect unless indentation is applied.\n :param string quote_char: the character that delimits quoted content.\n You should never need to mess with this.\n\n Delegates to :func:`xml4h.writer.write_node` applied to this node.\n \"\"\"\n xml4h.write_node(self,\n writer=writer, encoding=encoding, indent=indent,\n newline=newline, omit_declaration=omit_declaration,\n node_depth=node_depth, quote_char=quote_char)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef xml(self, indent=4, **kwargs):\n writer = StringIO()\n self.write(writer, indent=indent, **kwargs)\n return writer.getvalue()", "response": "Returns this node as XML text."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef xpath(self, xpath, **kwargs):\n result = self.adapter.xpath_on_node(self.impl_node, xpath, **kwargs)\n if isinstance(result, (list, tuple)):\n return [self._maybe_wrap_node(r) for r in result]\n else:\n return self._maybe_wrap_node(result)", "response": "Perform an XPath query on the current node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_attributes(self, attr_obj=None, ns_uri=None, **attr_dict):\n self._set_element_attributes(self.impl_node,\n attr_obj=attr_obj, ns_uri=ns_uri, **attr_dict)", "response": "Add or update this element s attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef attributes(self):\n attr_impl_nodes = self.adapter.get_node_attributes(self.impl_node)\n return AttributeDict(attr_impl_nodes, self.impl_node, self.adapter)", "response": "Get or set this element s attributes as name value pairs."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef attribute_nodes(self):\n impl_attr_nodes = self.adapter.get_node_attributes(self.impl_node)\n wrapped_attr_nodes = [\n self.adapter.wrap_node(a, self.adapter.impl_document, self.adapter)\n for a in impl_attr_nodes]\n return sorted(wrapped_attr_nodes, key=lambda x: x.name)", "response": "returns a list of this element s attributes as\n : class : Attribute nodes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef attribute_node(self, name, ns_uri=None):\n attr_impl_node = self.adapter.get_node_attribute_node(\n self.impl_node, name, ns_uri)\n return self.adapter.wrap_node(\n attr_impl_node, self.adapter.impl_document, self.adapter)", "response": "returns the node that matches the given attribute name and ns_uri"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndefines a namespace prefix that will serve as shorthand for the given namespace URI in element names.", "response": "def set_ns_prefix(self, prefix, ns_uri):\n \"\"\"\n Define a namespace prefix that will serve as shorthand for the given\n namespace URI in element names.\n\n :param string prefix: prefix that will serve as an alias for a\n the namespace URI.\n :param string ns_uri: namespace URI that will be denoted by the\n prefix.\n \"\"\"\n self._add_ns_prefix_attr(self.impl_node, prefix, ns_uri)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a new child element to this element with an optional namespace enumeration definition.", "response": "def add_element(self, name, ns_uri=None, attributes=None,\n text=None, before_this_element=False):\n \"\"\"\n Add a new child element to this element, with an optional namespace\n definition. If no namespace is provided the child will be assigned\n to the default namespace.\n\n :param string name: a name for the child node. The name may be used\n to apply a namespace to the child by including:\n\n - a prefix component in the name of the form\n ``ns_prefix:element_name``, where the prefix has already been\n defined for a namespace URI (such as via :meth:`set_ns_prefix`).\n - a literal namespace URI value delimited by curly braces, of\n the form ``{ns_uri}element_name``.\n :param ns_uri: a URI specifying the new element's namespace. If the\n ``name`` parameter specifies a namespace this parameter is ignored.\n :type ns_uri: string or None\n :param attributes: collection of attributes to assign to the new child.\n :type attributes: dict, list, tuple, or None\n :param text: text value to assign to the new child.\n :type text: string or None\n :param bool before_this_element: if *True* the new element is\n added as a sibling preceding this element, instead of as a child.\n In other words, the new element will be a child of this element's\n parent node, and will immediately precent this element in the DOM.\n\n :return: the new child as a an :class:`Element` node.\n \"\"\"\n # Determine local name, namespace and prefix info from tag name\n prefix, local_name, node_ns_uri = \\\n self.adapter.get_ns_info_from_node_name(name, self.impl_node)\n if prefix:\n qname = u'%s:%s' % (prefix, local_name)\n else:\n qname = local_name\n # If no name-derived namespace, apply an alternate namespace\n if node_ns_uri is None:\n if ns_uri is None:\n # Default document namespace\n node_ns_uri = self.adapter.get_ns_uri_for_prefix(\n self.impl_node, None)\n else:\n # keyword-parameter namespace\n node_ns_uri = ns_uri\n # Create element\n child_elem = self.adapter.new_impl_element(\n qname, node_ns_uri, parent=self.impl_node)\n # If element's default namespace was defined by literal uri prefix,\n # create corresponding xmlns attribute for element...\n if not prefix and '}' in name:\n self._set_element_attributes(child_elem,\n {'xmlns': node_ns_uri}, ns_uri=self.XMLNS_URI)\n # ...otherwise define keyword-defined namespace as the default, if any\n elif ns_uri is not None:\n self._set_element_attributes(child_elem,\n {'xmlns': ns_uri}, ns_uri=self.XMLNS_URI)\n # Create subordinate nodes\n if attributes is not None:\n self._set_element_attributes(child_elem, attr_obj=attributes)\n if text is not None:\n self._add_text(child_elem, text)\n # Add new element to its parent before a given node...\n if before_this_element:\n self.adapter.add_node_child(\n self.adapter.get_node_parent(self.impl_node),\n child_elem, before_sibling=self.impl_node)\n # ...or in the default position, appended after existing nodes\n else:\n self.adapter.add_node_child(self.impl_node, child_elem)\n return self.adapter.wrap_node(\n child_elem, self.adapter.impl_document, self.adapter)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_text(self, text):\n if not isinstance(text, basestring):\n text = unicode(text)\n self._add_text(self.impl_node, text)", "response": "Add a text node to this element."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_instruction(self, target, data):\n self._add_instruction(self.impl_node, target, data)", "response": "Add an instruction node to this element."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of name value attribute pairs sorted by attribute name.", "response": "def items(self):\n \"\"\"\n :return: a list of name/value attribute pairs sorted by attribute name.\n \"\"\"\n sorted_keys = sorted(self.keys())\n return [(k, self[k]) for k in sorted_keys]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef namespace_uri(self, name):\n a_node = self.adapter.get_node_attribute_node(self.impl_element, name)\n if a_node is None:\n return None\n return self.adapter.get_node_namespace_uri(a_node)", "response": "returns the namespace URI associated with the named attribute or None if no such attribute exists."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the prefix component of the named attribute", "response": "def prefix(self, name):\n \"\"\"\n :param string name: the name of an attribute to look up.\n\n :return: the prefix component of the named attribute's name,\n or None.\n \"\"\"\n a_node = self.adapter.get_node_attribute_node(self.impl_element, name)\n if a_node is None:\n return None\n return a_node.prefix"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef element(self):\n return self.adapter.wrap_node(\n self.impl_element, self.adapter.impl_document, self.adapter)", "response": "Returns the Element that contains these attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef filter(self, local_name=None, name=None, ns_uri=None, node_type=None,\n filter_fn=None, first_only=False):\n \"\"\"\n Apply filters to the set of nodes in this list.\n\n :param local_name: a local name used to filter the nodes.\n :type local_name: string or None\n :param name: a name used to filter the nodes.\n :type name: string or None\n :param ns_uri: a namespace URI used to filter the nodes.\n If *None* all nodes are returned regardless of namespace.\n :type ns_uri: string or None\n :param node_type: a node type definition used to filter the nodes.\n :type node_type: int node type constant, class, or None\n :param filter_fn: an arbitrary function to filter nodes in this list.\n This function must accept a single :class:`Node` argument and\n return a bool indicating whether to include the node in the\n filtered results.\n\n .. note:: if ``filter_fn`` is provided all other filter arguments\n are ignore.\n :type filter_fn: function or None\n\n :return: the type of the return value depends on the value of the\n ``first_only`` parameter and how many nodes match the filter:\n\n - if ``first_only=False`` return a :class:`NodeList` of filtered\n nodes, which will be empty if there are no matching nodes.\n - if ``first_only=True`` and at least one node matches,\n return the first matching :class:`Node`\n - if ``first_only=True`` and there are no matching nodes,\n return *None*\n \"\"\"\n # Build our own filter function unless a custom function is provided\n if filter_fn is None:\n def filter_fn(n):\n # Test node type first in case other tests require this type\n if node_type is not None:\n # Node type can be specified as an integer constant (e.g.\n # ELEMENT_NODE) or a class.\n if isinstance(node_type, int):\n if not n.is_type(node_type):\n return False\n elif n.__class__ != node_type:\n return False\n if name is not None and n.name != name:\n return False\n if local_name is not None and n.local_name != local_name:\n return False\n if ns_uri is not None and n.ns_uri != ns_uri:\n return False\n return True\n # Filter nodes\n nodelist = filter(filter_fn, self)\n # If requested, return just the first node (or None if no nodes)\n if first_only:\n return nodelist[0] if nodelist else None\n else:\n return NodeList(nodelist)", "response": "This method filters the set of nodes in this list based on the given parameters."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_all_analytics(user, job_id):\n\n args = schemas.args(flask.request.args.to_dict())\n v1_utils.verify_existence_and_get(job_id, models.JOBS)\n\n query = v1_utils.QueryBuilder(_TABLE, args, _A_COLUMNS)\n # If not admin nor rh employee then restrict the view to the team\n if user.is_not_super_admin() and not user.is_read_only_user():\n query.add_extra_condition(_TABLE.c.team_id.in_(user.teams_ids))\n\n query.add_extra_condition(_TABLE.c.job_id == job_id)\n\n nb_rows = query.get_number_of_rows()\n rows = query.execute(fetchall=True)\n rows = v1_utils.format_result(rows, _TABLE.name)\n\n return flask.jsonify({'analytics': rows, '_meta': {'count': nb_rows}})", "response": "Get all analytics of a job."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef write_node(node, writer=None, encoding='utf-8', indent=0, newline='',\n omit_declaration=False, node_depth=0, quote_char='\"'):\n \"\"\"\n Serialize an *xml4h* DOM node and its descendants to text, writing\n the output to a given *writer* or to stdout.\n\n :param node: the DOM node whose content and descendants will\n be serialized.\n :type node: an :class:`xml4h.nodes.Node` or subclass\n :param writer: an object such as a file or stream to which XML text\n is sent. If *None* text is sent to :attr:`sys.stdout`.\n :type writer: a file, stream, etc or None\n :param string encoding: the character encoding for serialized text.\n :param indent: indentation prefix to apply to descendent nodes for\n pretty-printing. The value can take many forms:\n\n - *int*: the number of spaces to indent. 0 means no indent.\n - *string*: a literal prefix for indented nodes, such as ``\\\\t``.\n - *bool*: no indent if *False*, four spaces indent if *True*.\n - *None*: no indent.\n :type indent: string, int, bool, or None\n :param newline: the string value used to separate lines of output.\n The value can take a number of forms:\n\n - *string*: the literal newline value, such as ``\\\\n`` or ``\\\\r``.\n An empty string means no newline.\n - *bool*: no newline if *False*, ``\\\\n`` newline if *True*.\n - *None*: no newline.\n :type newline: string, bool, or None\n :param boolean omit_declaration: if *True* the XML declaration header\n is omitted, otherwise it is included. Note that the declaration is\n only output when serializing an :class:`xml4h.nodes.Document` node.\n :param int node_depth: the indentation level to start at, such as 2 to\n indent output as if the given *node* has two ancestors.\n This parameter will only be useful if you need to output XML text\n fragments that can be assembled into a document. This parameter\n has no effect unless indentation is applied.\n :param string quote_char: the character that delimits quoted content.\n You should never need to mess with this.\n \"\"\"\n def _sanitize_write_value(value):\n \"\"\"Return XML-encoded value.\"\"\"\n if not value:\n return value\n return (value\n .replace(\"&\", \"&\")\n .replace(\"<\", \"<\")\n .replace(\"\\\"\", \""\")\n .replace(\">\", \">\")\n )\n\n def _write_node_impl(node, node_depth):\n \"\"\"\n Internal write implementation that does the real work while keeping\n track of node depth.\n \"\"\"\n # Output document declaration if we're outputting the whole doc\n if node.is_document:\n if not omit_declaration:\n writer.write(\n '%s' % newline)\n for child in node.children:\n _write_node_impl(child,\n node_depth) # node_depth not incremented\n writer.write(newline)\n elif node.is_document_type:\n writer.write(\"\")\n elif node.is_text:\n writer.write(_sanitize_write_value(node.value))\n elif node.is_cdata:\n if ']]>' in node.value:\n raise ValueError(\"']]>' is not allowed in CDATA node value\")\n writer.write(\"\" % node.value)\n #elif node.is_entity_reference: # TODO\n elif node.is_entity:\n writer.write(newline + indent * node_depth)\n writer.write(\"\"\n % (node.name, quote_char, node.value, quote_char))\n elif node.is_processing_instruction:\n writer.write(newline + indent * node_depth)\n writer.write(\"\" % (node.target, node.data))\n elif node.is_comment:\n if '--' in node.value:\n raise ValueError(\"'--' is not allowed in COMMENT node value\")\n writer.write(\"\" % node.value)\n elif node.is_notation:\n writer.write(newline + indent * node_depth)\n writer.write(\"\"\n % (quote_char, node.external_id, quote_char))\n elif node.is_system_identifier:\n writer.write(\" system %s%s%s %s%s%s>\"\n % (quote_char, node.external_id, quote_char,\n quote_char, node.uri, quote_char))\n elif node.is_attribute:\n writer.write(\" %s=%s\" % (node.name, quote_char))\n writer.write(_sanitize_write_value(node.value))\n writer.write(quote_char)\n elif node.is_element:\n # Only need a preceding newline if we're in a sub-element\n if node_depth > 0:\n writer.write(newline)\n writer.write(indent * node_depth)\n writer.write(\"<\" + node.name)\n\n for attr in node.attribute_nodes:\n _write_node_impl(attr, node_depth)\n if node.children:\n found_indented_child = False\n writer.write(\">\")\n for child in node.children:\n _write_node_impl(child, node_depth + 1)\n if not (child.is_text\n or child.is_comment\n or child.is_cdata):\n found_indented_child = True\n if found_indented_child:\n writer.write(newline + indent * node_depth)\n writer.write('' % node.name)\n else:\n writer.write('/>')\n else:\n raise exceptions.Xml4hImplementationBug(\n 'Cannot write node with class: %s' % node.__class__)\n\n # Sanitize whitespace parameters\n if indent is True:\n indent = ' ' * 4\n elif indent is False:\n indent = ''\n elif isinstance(indent, int):\n indent = ' ' * indent\n # If indent but no newline set, always apply a newline (it makes sense)\n if indent and not newline:\n newline = True\n\n if newline is None or newline is False:\n newline = ''\n elif newline is True:\n newline = '\\n'\n\n # We always need a writer, use stdout by default\n if writer is None:\n writer = sys.stdout\n\n # Apply a text encoding if we have one\n if encoding is None:\n writer = writer\n else:\n writer = codecs.getwriter(encoding)(writer)\n\n # Do the business...\n _write_node_impl(node, node_depth)", "response": "Serialize a node to a given writer or to stdout."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nencrypting a chunk of a GCM file with the given key and decrypts it.", "response": "def encry_decry_chunk(chunk, key, algo, bool_encry, assoc_data):\n \"\"\"\n When bool_encry is True, encrypt a chunk of the file with the key and a randomly generated nonce. When it is False,\n the function extract the nonce from the cipherchunk (first 16 bytes), and decrypt the rest of the chunk.\n :param chunk: a chunk in bytes to encrypt or decrypt.\n :param key: a 32 bytes key in bytes.\n :param algo: a string of algorithm. Can be \"srp\" , \"AES\" or \"twf\"\n :param bool_encry: if bool_encry is True, chunk is encrypted. Else, it will be decrypted.\n :param assoc_data: bytes string of additional data for GCM Authentication.\n :return: if bool_encry is True, corresponding nonce + cipherchunk else, a decrypted chunk.\n \"\"\"\n engine = botan.cipher(algo=algo, encrypt=bool_encry)\n engine.set_key(key=key)\n engine.set_assoc_data(assoc_data)\n if bool_encry is True:\n nonce = generate_nonce_timestamp()\n engine.start(nonce=nonce)\n return nonce + engine.finish(chunk)\n else:\n nonce = chunk[:__nonce_length__]\n encryptedchunk = chunk[__nonce_length__:__nonce_length__ + __gcmtag_length__ + __chunk_size__]\n engine.start(nonce=nonce)\n decryptedchunk = engine.finish(encryptedchunk)\n if decryptedchunk == b\"\":\n raise Exception(\"Integrity failure: Invalid passphrase or corrupted data\")\n return decryptedchunk"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nquery Bugzilla API to retrieve the needed infos.", "response": "def retrieve_info(self):\n \"\"\"Query Bugzilla API to retrieve the needed infos.\"\"\"\n\n scheme = urlparse(self.url).scheme\n netloc = urlparse(self.url).netloc\n query = urlparse(self.url).query\n\n if scheme not in ('http', 'https'):\n return\n\n for item in query.split('&'):\n if 'id=' in item:\n ticket_id = item.split('=')[1]\n break\n else:\n return\n\n bugzilla_url = '%s://%s/%s%s' % (scheme, netloc, _URI_BASE, ticket_id)\n\n result = requests.get(bugzilla_url)\n self.status_code = result.status_code\n\n if result.status_code == 200:\n tree = ElementTree.fromstring(result.content)\n\n self.title = tree.findall(\"./bug/short_desc\").pop().text\n self.issue_id = tree.findall(\"./bug/bug_id\").pop().text\n self.reporter = tree.findall(\"./bug/reporter\").pop().text\n self.assignee = tree.findall(\"./bug/assigned_to\").pop().text\n self.status = tree.findall(\"./bug/bug_status\").pop().text\n self.product = tree.findall(\"./bug/product\").pop().text\n self.component = tree.findall(\"./bug/component\").pop().text\n self.created_at = tree.findall(\"./bug/creation_ts\").pop().text\n self.updated_at = tree.findall(\"./bug/delta_ts\").pop().text\n try:\n self.closed_at = (\n tree.findall(\"./bug/cf_last_closed\").pop().text\n )\n except IndexError:\n # cf_last_closed is present only if the issue has been closed\n # if not present it raises an IndexError, meaning the issue\n # isn't closed yet, which is a valid use case.\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef maxlen(max_length,\n strict=False # type: bool\n ):\n \"\"\"\n 'Maximum length' validation_function generator.\n Returns a validation_function to check that len(x) <= max_length (strict=False, default) or len(x) < max_length (strict=True)\n\n :param max_length: maximum length for x\n :param strict: Boolean flag to switch between len(x) <= max_length (strict=False) and len(x) < max_length\n (strict=True)\n :return:\n \"\"\"\n if strict:\n def maxlen_(x):\n if len(x) < max_length:\n return True\n else:\n # raise Failure('maxlen: len(x) < ' + str(max_length) + ' does not hold for x=' + str(x))\n raise TooLong(wrong_value=x, max_length=max_length, strict=True)\n else:\n def maxlen_(x):\n if len(x) <= max_length:\n return True\n else:\n # raise Failure('maxlen: len(x) <= ' + str(max_length) + ' does not hold for x=' + str(x))\n raise TooLong(wrong_value=x, max_length=max_length, strict=False)\n\n maxlen_.__name__ = 'length_{}lesser_than_{}'.format('strictly_' if strict else '', max_length)\n return maxlen_", "response": "Maximum length validation_function generator."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef has_length(ref_length):\n def has_length_(x):\n if len(x) == ref_length:\n return True\n else:\n raise WrongLength(wrong_value=x, ref_length=ref_length)\n\n has_length_.__name__ = 'length_equals_{}'.format(ref_length)\n return has_length_", "response": "length equals validation function generator."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbeing length between validation_function generator.", "response": "def length_between(min_len,\n max_len,\n open_left=False, # type: bool\n open_right=False # type: bool\n ):\n \"\"\"\n 'Is length between' validation_function generator.\n Returns a validation_function to check that `min_len <= len(x) <= max_len (default)`. `open_right` and `open_left`\n flags allow to transform each side into strict mode. For example setting `open_left=True` will enforce\n `min_len < len(x) <= max_len`.\n\n :param min_len: minimum length for x\n :param max_len: maximum length for x\n :param open_left: Boolean flag to turn the left inequality to strict mode\n :param open_right: Boolean flag to turn the right inequality to strict mode\n :return:\n \"\"\"\n if open_left and open_right:\n def length_between_(x):\n if (min_len < len(x)) and (len(x) < max_len):\n return True\n else:\n # raise Failure('length between: {} < len(x) < {} does not hold for x={}'.format(min_len, max_len,\n # x))\n raise LengthNotInRange(wrong_value=x, min_length=min_len, left_strict=True, max_length=max_len,\n right_strict=True)\n elif open_left:\n def length_between_(x):\n if (min_len < len(x)) and (len(x) <= max_len):\n return True\n else:\n # raise Failure('length between: {} < len(x) <= {} does not hold for x={}'.format(min_len, max_len,\n # x))\n raise LengthNotInRange(wrong_value=x, min_length=min_len, left_strict=True, max_length=max_len,\n right_strict=False)\n elif open_right:\n def length_between_(x):\n if (min_len <= len(x)) and (len(x) < max_len):\n return True\n else:\n # raise Failure('length between: {} <= len(x) < {} does not hold for x={}'.format(min_len, max_len,\n # x))\n raise LengthNotInRange(wrong_value=x, min_length=min_len, left_strict=False, max_length=max_len,\n right_strict=True)\n else:\n def length_between_(x):\n if (min_len <= len(x)) and (len(x) <= max_len):\n return True\n else:\n # raise Failure('length between: {} <= len(x) <= {} does not hold for x={}'.format(min_len,\n # max_len, x))\n raise LengthNotInRange(wrong_value=x, min_length=min_len, left_strict=False, max_length=max_len,\n right_strict=False)\n\n length_between_.__name__ = 'length_between_{}_and_{}'.format(min_len, max_len)\n return length_between_"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nvalue in validation_function generator.", "response": "def is_in(allowed_values # type: Set\n ):\n \"\"\"\n 'Values in' validation_function generator.\n Returns a validation_function to check that x is in the provided set of allowed values\n\n :param allowed_values: a set of allowed values\n :return:\n \"\"\"\n def is_in_allowed_values(x):\n if x in allowed_values:\n return True\n else:\n # raise Failure('is_in: x in ' + str(allowed_values) + ' does not hold for x=' + str(x))\n raise NotInAllowedValues(wrong_value=x, allowed_values=allowed_values)\n\n is_in_allowed_values.__name__ = 'is_in_{}'.format(allowed_values)\n return is_in_allowed_values"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_subset(reference_set # type: Set\n ):\n \"\"\"\n 'Is subset' validation_function generator.\n Returns a validation_function to check that x is a subset of reference_set\n\n :param reference_set: the reference set\n :return:\n \"\"\"\n def is_subset_of(x):\n missing = x - reference_set\n if len(missing) == 0:\n return True\n else:\n # raise Failure('is_subset: len(x - reference_set) == 0 does not hold for x=' + str(x)\n # + ' and reference_set=' + str(reference_set) + '. x contains unsupported '\n # 'elements ' + str(missing))\n raise NotSubset(wrong_value=x, reference_set=reference_set, unsupported=missing)\n\n is_subset_of.__name__ = 'is_subset_of_{}'.format(reference_set)\n return is_subset_of", "response": "Is subset validation_function generator."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_superset(reference_set # type: Set\n ):\n \"\"\"\n 'Is superset' validation_function generator.\n Returns a validation_function to check that x is a superset of reference_set\n\n :param reference_set: the reference set\n :return:\n \"\"\"\n def is_superset_of(x):\n missing = reference_set - x\n if len(missing) == 0:\n return True\n else:\n # raise Failure('is_superset: len(reference_set - x) == 0 does not hold for x=' + str(x)\n # + ' and reference_set=' + str(reference_set) + '. x does not contain required '\n # 'elements ' + str(missing))\n raise NotSuperset(wrong_value=x, reference_set=reference_set, missing=missing)\n\n is_superset_of.__name__ = 'is_superset_of_{}'.format(reference_set)\n return is_superset_of", "response": "Is superset validation_function generator."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates a validation_function for collection inputs where each element of the input is validated against the validation_function_func.", "response": "def on_all_(*validation_func):\n \"\"\"\n Generates a validation_function for collection inputs where each element of the input will be validated against the\n validation_functions provided. For convenience, a list of validation_functions can be provided and will be replaced\n with an 'and_'.\n\n Note that if you want to apply DIFFERENT validation_functions for each element in the input, you should rather use\n on_each_.\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :return:\n \"\"\"\n # create the validation functions\n validation_function_func = _process_validation_function_s(list(validation_func))\n\n def on_all_val(x):\n # validate all elements in x in turn\n for idx, x_elt in enumerate(x):\n try:\n res = validation_function_func(x_elt)\n except Exception as e:\n raise InvalidItemInSequence(wrong_value=x_elt, wrapped_func=validation_function_func, validation_outcome=e)\n\n if not result_is_success(res):\n # one element of x was not valid > raise\n # raise Failure('on_all_(' + str(validation_func) + '): failed validation for input '\n # 'element [' + str(idx) + ']: ' + str(x_elt))\n raise InvalidItemInSequence(wrong_value=x_elt, wrapped_func=validation_function_func, validation_outcome=res)\n return True\n\n on_all_val.__name__ = 'apply_<{}>_on_all_elts'.format(get_callable_name(validation_function_func))\n return on_all_val"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef on_each_(*validation_functions_collection):\n # create a tuple of validation functions.\n validation_function_funcs = tuple(_process_validation_function_s(validation_func)\n for validation_func in validation_functions_collection)\n\n # generate a validation function based on the tuple of validation_functions lists\n def on_each_val(x # type: Tuple\n ):\n if len(validation_function_funcs) != len(x):\n raise Failure('on_each_: x does not have the same number of elements than validation_functions_collection.')\n else:\n # apply each validation_function on the input with the same position in the collection\n idx = -1\n for elt, validation_function_func in zip(x, validation_function_funcs):\n idx += 1\n try:\n res = validation_function_func(elt)\n except Exception as e:\n raise InvalidItemInSequence(wrong_value=elt,\n wrapped_func=validation_function_func,\n validation_outcome=e)\n\n if not result_is_success(res):\n # one validation_function was unhappy > raise\n # raise Failure('on_each_(' + str(validation_functions_collection) + '): _validation_function [' + str(idx)\n # + '] (' + str(validation_functions_collection[idx]) + ') failed validation for '\n # 'input ' + str(x[idx]))\n raise InvalidItemInSequence(wrong_value=elt,\n wrapped_func=validation_function_func,\n validation_outcome=res)\n return True\n\n on_each_val.__name__ = 'map_<{}>_on_elts' \\\n ''.format('(' + ', '.join([get_callable_name(f) for f in validation_function_funcs]) + ')')\n return on_each_val", "response": "Generates a validation function for each element of the input."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all the jobs events from a given sequence number.", "response": "def get_jobs_events_from_sequence(user, sequence):\n \"\"\"Get all the jobs events from a given sequence number.\"\"\"\n\n args = schemas.args(flask.request.args.to_dict())\n\n if user.is_not_super_admin():\n raise dci_exc.Unauthorized()\n\n query = sql.select([models.JOBS_EVENTS]). \\\n select_from(models.JOBS_EVENTS.join(models.JOBS,\n models.JOBS.c.id == models.JOBS_EVENTS.c.job_id)). \\\n where(_TABLE.c.id >= sequence)\n sort_list = v1_utils.sort_query(args['sort'], _JOBS_EVENTS_COLUMNS,\n default='created_at')\n query = v1_utils.add_sort_to_query(query, sort_list)\n\n if args.get('limit', None):\n query = query.limit(args.get('limit'))\n if args.get('offset', None):\n query = query.offset(args.get('offset'))\n\n rows = flask.g.db_conn.execute(query).fetchall()\n\n query_nb_rows = sql.select([func.count(models.JOBS_EVENTS.c.id)])\n nb_rows = flask.g.db_conn.execute(query_nb_rows).scalar()\n\n return json.jsonify({'jobs_events': rows, '_meta': {'count': nb_rows}})"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef from_file(cls, f):\n element = ElementIterator.from_file(f)\n assert element.tag == \"mediawiki\"\n return cls.from_element(element)", "response": "Constructs a : class ~mwxml. iteration. dump. Dump from a plain text file pointer."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef from_page_xml(cls, page_xml):\n header = \"\"\"\n \n \n \n \"\"\"\n\n footer = \"\"\n\n return cls.from_file(mwtypes.files.concat(header, page_xml, footer))", "response": "Constructs a ~mwxml. iteration. dump. Dump from a page XML."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef calc_derivation(passphrase, salt):\n argonhash = argon2.low_level.hash_secret_raw((str.encode(passphrase)), salt=salt, hash_len=32,\n time_cost=__argon2_timing_cost__, memory_cost=__argon2_memory_cost__,\n parallelism=__argon2_parallelism__, type=argon2.low_level.Type.I)\n return argonhash", "response": "Calculates a 32 bytes key derivation with Argon2."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the text value of the node s children.", "response": "def get_node_text(self, node):\n \"\"\"\n Return contatenated value of all text node children of this element\n \"\"\"\n text_children = [n.nodeValue for n in self.get_node_children(node)\n if n.nodeType == xml.dom.Node.TEXT_NODE]\n if text_children:\n return u''.join(text_children)\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets text value of node.", "response": "def set_node_text(self, node, text):\n \"\"\"\n Set text value as sole Text child node of element; any existing\n Text nodes are removed\n \"\"\"\n # Remove any existing Text node children\n for child in self.get_node_children(node):\n if child.nodeType == xml.dom.Node.TEXT_NODE:\n self.remove_node_child(node, child, True)\n if text is not None:\n text_node = self.new_impl_text(text)\n self.add_node_child(node, text_node)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating strings from glob strings.", "response": "def _get_contents(self):\n \"\"\"Create strings from glob strings.\"\"\"\n def files():\n for value in super(GlobBundle, self)._get_contents():\n for path in glob.glob(value):\n yield path\n return list(files())"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef is_generic_type(tp):\n if NEW_TYPING:\n return (isinstance(tp, type) and issubclass(tp, Generic) or\n isinstance(tp, _GenericAlias) and\n tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable))\n # SMA: refering to is_callable_type and is_tuple_type to avoid duplicate code\n return isinstance(tp, GenericMeta) and not is_callable_type(tp) and not is_tuple_type(tp)", "response": "Test if the given type is a generic type."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_union_type(tp):\n if NEW_TYPING:\n return (tp is Union or\n isinstance(tp, _GenericAlias) and tp.__origin__ is Union)\n try:\n from typing import _Union\n return type(tp) is _Union\n except ImportError:\n # SMA: support for very old typing module <=3.5.3\n return type(tp) is Union or type(tp) is type(Union)", "response": "Test if the type is a union type."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ntests if the type represents a class variable.", "response": "def is_classvar(tp):\n \"\"\"Test if the type represents a class variable. Examples::\n\n is_classvar(int) == False\n is_classvar(ClassVar) == True\n is_classvar(ClassVar[int]) == True\n is_classvar(ClassVar[List[T]]) == True\n \"\"\"\n if NEW_TYPING:\n return (tp is ClassVar or\n isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar)\n try:\n from typing import _ClassVar\n return type(tp) is _ClassVar\n except:\n # SMA: support for very old typing module <=3.5.3\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse(to_parse, ignore_whitespace_text_nodes=True, adapter=None):\n if adapter is None:\n adapter = best_adapter\n if isinstance(to_parse, basestring) and '<' in to_parse:\n return adapter.parse_string(to_parse, ignore_whitespace_text_nodes)\n else:\n return adapter.parse_file(to_parse, ignore_whitespace_text_nodes)", "response": "Parses an XML document into an XML structure."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a new XML tree tree.", "response": "def build(tagname_or_element, ns_uri=None, adapter=None):\n \"\"\"\n Return a :class:`~xml4h.builder.Builder` that represents an element in\n a new or existing XML DOM and provides \"chainable\" methods focussed\n specifically on adding XML content.\n\n :param tagname_or_element: a string name for the root node of a\n new XML document, or an :class:`~xml4h.nodes.Element` node in an\n existing document.\n :type tagname_or_element: string or :class:`~xml4h.nodes.Element` node\n :param ns_uri: a namespace URI to apply to the new root node. This\n argument has no effect this method is acting on an element.\n :type ns_uri: string or None\n :param adapter: the *xml4h* implementation adapter class used to\n interact with the document DOM nodes.\n If None, :attr:`best_adapter` will be used.\n :type adapter: adapter class or None\n\n :return: a :class:`~xml4h.builder.Builder` instance that represents an\n :class:`~xml4h.nodes.Element` node in an XML DOM.\n \"\"\"\n if adapter is None:\n adapter = best_adapter\n if isinstance(tagname_or_element, basestring):\n doc = adapter.create_document(\n tagname_or_element, ns_uri=ns_uri)\n element = doc.root\n elif isinstance(tagname_or_element, xml4h.nodes.Element):\n element = tagname_or_element\n else:\n raise xml4h.exceptions.IncorrectArgumentTypeException(\n tagname_or_element, [basestring, xml4h.nodes.Element])\n return Builder(element)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a user - friendly description of a NonePolicy taking into account NoneArgPolicy orrow", "response": "def get_none_policy_text(none_policy, # type: int\n verbose=False # type: bool\n ):\n \"\"\"\n Returns a user-friendly description of a NonePolicy taking into account NoneArgPolicy\n\n :param none_policy:\n :param verbose:\n :return:\n \"\"\"\n if none_policy is NonePolicy.SKIP:\n return \"accept None without performing validation\" if verbose else 'SKIP'\n elif none_policy is NonePolicy.FAIL:\n return \"fail on None without performing validation\" if verbose else 'FAIL'\n elif none_policy is NonePolicy.VALIDATE:\n return \"validate None as any other values\" if verbose else 'VALIDATE'\n elif none_policy is NoneArgPolicy.SKIP_IF_NONABLE_ELSE_FAIL:\n return \"accept None without validation if the argument is optional, otherwise fail on None\" if verbose \\\n else 'SKIP_IF_NONABLE_ELSE_FAIL'\n elif none_policy is NoneArgPolicy.SKIP_IF_NONABLE_ELSE_VALIDATE:\n return \"accept None without validation if the argument is optional, otherwise validate None as any other \" \\\n \"values\" if verbose else 'SKIP_IF_NONABLE_ELSE_VALIDATE'\n else:\n raise ValueError('Invalid none_policy ' + str(none_policy))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _add_none_handler(validation_callable, # type: Callable\n none_policy # type: int\n ):\n # type: (...) -> Callable\n \"\"\"\n Adds a wrapper or nothing around the provided validation_callable, depending on the selected policy\n\n :param validation_callable:\n :param none_policy: an int representing the None policy, see NonePolicy\n :return:\n \"\"\"\n if none_policy is NonePolicy.SKIP:\n return _none_accepter(validation_callable) # accept all None values\n\n elif none_policy is NonePolicy.FAIL:\n return _none_rejecter(validation_callable) # reject all None values\n\n elif none_policy is NonePolicy.VALIDATE:\n return validation_callable # do not handle None specifically, do not wrap\n\n else:\n raise ValueError('Invalid none_policy : ' + str(none_policy))", "response": "Adds a wrapper or nothing around the provided validation_callable depending on the selected NonePolicy."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_base_type_dynamically(error_type, additional_type):\n # the new type created dynamically, with the same name\n class new_error_type(with_metaclass(MetaReprForValidator, error_type, additional_type, object)):\n pass\n\n new_error_type.__name__ = error_type.__name__ + '[' + additional_type.__name__ + ']'\n if sys.version_info >= (3, 0):\n new_error_type.__qualname__ = error_type.__qualname__ + '[' + additional_type.__qualname__+ ']'\n new_error_type.__module__ = error_type.__module__\n\n return new_error_type", "response": "Utility method to create a new type dynamically inheriting from both error_type and additional_type."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef assert_valid(name, # type: str\n value, # type: Any\n *validation_func, # type: ValidationFuncs\n **kwargs):\n \"\"\"\n Validates value `value` using validation function(s) `base_validator_s`.\n As opposed to `is_valid`, this function raises a `ValidationError` if validation fails.\n\n It is therefore designed to be used for defensive programming, in an independent statement before the code that you\n intent to protect.\n\n ```python\n assert_valid(x, isfinite):\n ...\n ```\n\n Note: this is a friendly alias for `_validator(base_validator_s)(value)`\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param name: the name of the variable to validate. It will be used in error messages\n :param value: the value to validate\n :param none_policy: describes how None values should be handled. See `NonePolicy` for the various possibilities.\n Default is `NonePolicy.VALIDATE`, meaning that None values will be treated exactly like other values and follow\n the same validation process.\n :param error_type: a subclass of ValidationError to raise in case of validation failure. By default a\n ValidationError will be raised with the provided help_msg\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param kw_context_args: optional keyword arguments providing additional context, that will be provided to the error\n in case of validation failure\n :return: nothing in case of success. In case of failure, raises a if provided, or a ValidationError.\n \"\"\"\n error_type, help_msg, none_policy = pop_kwargs(kwargs, [('error_type', None),\n ('help_msg', None),\n ('none_policy', None)], allow_others=True)\n # the rest of keyword arguments is used as context.\n kw_context_args = kwargs\n\n return Validator(*validation_func, error_type=error_type, help_msg=help_msg,\n none_policy=none_policy).assert_valid(name=name, value=value, **kw_context_args)", "response": "Ensures that a value is valid for a given variable."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nvalidate value using validation function.", "response": "def is_valid(value,\n *validation_func, # type: Union[Callable, List[Callable]]\n **kwargs\n ):\n # type: (...) -> bool\n \"\"\"\n Validates value `value` using validation function(s) `validator_func`.\n As opposed to `assert_valid`, this function returns a boolean indicating if validation was a success or a failure.\n It is therefore designed to be used within if ... else ... statements:\n\n ```python\n if is_valid(x, isfinite):\n ...\n else\n ...\n ```\n\n Note: this is a friendly alias for `return _validator(validator_func, return_bool=True)(value)`\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param value: the value to validate\n :param none_policy: describes how None values should be handled. See `NonePolicy` for the various possibilities.\n Default is `NonePolicy.VALIDATE`, meaning that None values will be treated exactly like other values and follow\n the same validation process. Note that errors raised by NonePolicy.FAIL will be caught and transformed into a\n returned value of False\n :return: True if validation was a success, False otherwise\n \"\"\"\n none_policy = pop_kwargs(kwargs, [('none_policy', None)])\n\n return Validator(*validation_func, none_policy=none_policy).is_valid(value)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates an exception or a validator with a specific name.", "response": "def create_manually(cls,\n validation_function_name, # type: str\n var_name, # type: str\n var_value,\n validation_outcome=None, # type: Any\n help_msg=None, # type: str\n append_details=True, # type: bool\n **kw_context_args):\n \"\"\"\n Creates an instance without using a Validator.\n\n This method is not the primary way that errors are created - they should rather created by the validation entry\n points. However it can be handy in rare edge cases.\n\n :param validation_function_name:\n :param var_name:\n :param var_value:\n :param validation_outcome:\n :param help_msg:\n :param append_details:\n :param kw_context_args:\n :return:\n \"\"\"\n # create a dummy validator\n def val_fun(x):\n pass\n val_fun.__name__ = validation_function_name\n validator = Validator(val_fun, error_type=cls, help_msg=help_msg, **kw_context_args)\n\n # create the exception\n # e = cls(validator, var_value, var_name, validation_outcome=validation_outcome, help_msg=help_msg,\n # append_details=append_details, **kw_context_args)\n e = validator._create_validation_error(var_name, var_value, validation_outcome, error_type=cls,\n help_msg=help_msg, **kw_context_args)\n return e"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_details(self):\n\n # create the exception main message according to the type of result\n if isinstance(self.validation_outcome, Exception):\n\n prefix = 'Validation function [{val}] raised ' if self.display_prefix_for_exc_outcomes else ''\n\n # new: we now remove \"Root validator was [{validator}]\", users can get it through e.validator\n contents = ('Error validating {what}. ' + prefix + '{exception}: {details}')\\\n .format(what=self.get_what_txt(),\n val=self.validator.get_main_function_name(),\n exception=type(self.validation_outcome).__name__,\n details=end_with_dot(str(self.validation_outcome)))\n\n else:\n contents = 'Error validating {what}: validation function [{val}] returned [{result}].' \\\n ''.format(what=self.get_what_txt(), val=self.validator.get_main_function_name(),\n result=self.validation_outcome)\n\n # return 'Wrong value: [{}]'.format(self.var_value)\n return contents", "response": "This function returns the details of the error and the exception main message according to the type of validation outcome."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef assert_valid(self,\n name, # type: str\n value, # type: Any\n error_type=None, # type: Type[ValidationError]\n help_msg=None, # type: str\n **kw_context_args):\n \"\"\"\n Asserts that the provided named value is valid with respect to the inner base validation functions. It returns\n silently in case of success, and raises a `ValidationError` or a subclass in case of failure. This corresponds\n to a 'Defensive programming' (sometimes known as 'Offensive programming') mode.\n\n By default this raises instances of `ValidationError` with a default message, in case of failure. There are\n two ways that you can customize this behaviour:\n\n * if you set `help_msg` in this method or in `Validator` constructor, instances of `ValidationError` created\n will be customized with the provided help message.\n\n * if you set `error_type` in this method or in `Validator` constructor, instances of your custom class will be\n created. Note that you may still provide a `help_msg`.\n\n It is recommended that Users define their own validation error types (case 2 above), so as to provide a unique\n error type for each kind of applicative error. This eases the process of error handling at app-level.\n\n :param name: the name of the variable to validate (for error messages)\n :param value: the value to validate\n :param error_type: a subclass of `ValidationError` to raise in case of validation failure. By default a\n `ValidationError` will be raised with the provided `help_msg`\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param kw_context_args: optional contextual information to store in the exception, and that may be also used\n to format the help message\n :return: nothing in case of success. Otherwise, raises a ValidationError\n \"\"\"\n try:\n # perform validation\n res = self.main_function(value)\n\n except Exception as e:\n # caught any exception: raise ValidationError or subclass with that exception in the details\n # --old bad idea: first wrap into a failure ==> NO !!! I tried and it was making it far too messy/verbose\n\n # note: we do not have to 'raise x from e' of `raise_from`since the ValidationError constructor already\n # sets the __cause__ so we can safely take the same handling than for non-exception failures.\n res = e\n\n # check the result\n if not result_is_success(res):\n raise_(self._create_validation_error(name, value, validation_outcome=res, error_type=error_type,\n help_msg=help_msg, **kw_context_args))", "response": "This method checks that the provided value is valid with respect to the inner base validation functions."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a validation error for the current object.", "response": "def _create_validation_error(self,\n name, # type: str\n value, # type: Any\n validation_outcome=None, # type: Any\n error_type=None, # type: Type[ValidationError]\n help_msg=None, # type: str\n **kw_context_args):\n \"\"\" The function doing the final error raising. \"\"\"\n\n # first merge the info provided in arguments and in self\n error_type = error_type or self.error_type\n help_msg = help_msg or self.help_msg\n ctx = copy(self.kw_context_args)\n ctx.update(kw_context_args)\n\n # allow the class to override the name\n name = self._get_name_for_errors(name)\n\n if issubclass(error_type, TypeError) or issubclass(error_type, ValueError):\n # this is most probably a custom error type, it is already annotated with ValueError and/or TypeError\n # so use it 'as is'\n new_error_type = error_type\n else:\n # Add the appropriate TypeError/ValueError base type dynamically\n additional_type = None\n if isinstance(validation_outcome, Exception):\n if is_error_of_type(validation_outcome, TypeError):\n additional_type = TypeError\n elif is_error_of_type(validation_outcome, ValueError):\n additional_type = ValueError\n if additional_type is None:\n # not much we can do here, let's assume a ValueError, that is more probable\n additional_type = ValueError\n\n new_error_type = add_base_type_dynamically(error_type, additional_type)\n\n # then raise the appropriate ValidationError or subclass\n return new_error_type(validator=self, var_value=value, var_name=name, validation_outcome=validation_outcome,\n help_msg=help_msg, **ctx)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nvalidates the provided value and returns a boolean indicating success or failure.", "response": "def is_valid(self,\n value # type: Any\n ):\n # type: (...) -> bool\n \"\"\"\n Validates the provided value and returns a boolean indicating success or failure. Any Exception happening in\n the validation process will be silently caught.\n\n :param value: the value to validate\n :return: a boolean flag indicating success or failure\n \"\"\"\n # noinspection PyBroadException\n try:\n # perform validation\n res = self.main_function(value)\n\n # return a boolean indicating if success or failure\n return result_is_success(res)\n\n except Exception:\n # caught exception means failure > return False\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndeletes a tag by id.", "response": "def delete_tag_by_id(user, tag_id):\n \"\"\"Delete a tag.\"\"\"\n query = _TABLE.delete().where(_TABLE.c.id == tag_id)\n result = flask.g.db_conn.execute(query)\n\n if not result.rowcount:\n raise dci_exc.DCIConflict('Tag deletion conflict', tag_id)\n\n return flask.Response(None, 204, content_type='application/json')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_app(self, app, entry_point_group='invenio_assets.bundles',\n **kwargs):\n \"\"\"Initialize application object.\n\n :param app: An instance of :class:`~flask.Flask`.\n :param entry_point_group: A name of entry point group used to load\n ``webassets`` bundles.\n\n .. versionchanged:: 1.0.0b2\n The *entrypoint* has been renamed to *entry_point_group*.\n \"\"\"\n self.init_config(app)\n self.env.init_app(app)\n self.collect.init_app(app)\n self.webpack.init_app(app)\n\n if entry_point_group:\n self.load_entrypoint(entry_point_group)\n\n app.extensions['invenio-assets'] = self", "response": "Initialize application object.\n\n :param app: An instance of :class:`~flask.Flask`.\n :param entry_point_group: A name of entry point group used to load\n ``webassets`` bundles.\n\n .. versionchanged:: 1.0.0b2\n The *entrypoint* has been renamed to *entry_point_group*."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef init_config(self, app):\n app.config.setdefault('REQUIREJS_BASEURL', app.static_folder)\n app.config.setdefault('COLLECT_STATIC_ROOT', app.static_folder)\n app.config.setdefault('COLLECT_STORAGE', 'flask_collect.storage.link')\n app.config.setdefault(\n 'COLLECT_FILTER', partial(collect_staticroot_removal, app))\n app.config.setdefault(\n 'WEBPACKEXT_PROJECT', 'invenio_assets.webpack:project')", "response": "Initialize configuration.\n\n :param app: An instance of :class:`~flask.Flask`."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load_entrypoint(self, entry_point_group):\n for ep in pkg_resources.iter_entry_points(entry_point_group):\n self.env.register(ep.name, ep.load())", "response": "Load the entry point."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks that value is an instance of one of the allowed types.", "response": "def assert_instance_of(value,\n allowed_types # type: Union[Type, Tuple[Type]]\n ):\n \"\"\"\n An inlined version of instance_of(var_types)(value) without 'return True': it does not return anything in case of\n success, and raises a HasWrongType exception in case of failure.\n\n Used in validate and validation/validator\n\n :param value: the value to check\n :param allowed_types: the type(s) to enforce. If a tuple of types is provided it is considered alternate types: one\n match is enough to succeed. If None, type will not be enforced\n :return:\n \"\"\"\n if not isinstance(value, allowed_types):\n try:\n # more than 1 ?\n allowed_types[1]\n raise HasWrongType(wrong_value=value, ref_type=allowed_types,\n help_msg='Value should be an instance of any of {ref_type}')\n except IndexError:\n # 1\n allowed_types = allowed_types[0]\n except TypeError:\n # 1\n pass\n raise HasWrongType(wrong_value=value, ref_type=allowed_types)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef assert_subclass_of(typ,\n allowed_types # type: Union[Type, Tuple[Type]]\n ):\n \"\"\"\n An inlined version of subclass_of(var_types)(value) without 'return True': it does not return anything in case of\n success, and raises a IsWrongType exception in case of failure.\n\n Used in validate and validation/validator\n\n :param typ: the type to check\n :param allowed_types: the type(s) to enforce. If a tuple of types is provided it is considered alternate types:\n one match is enough to succeed. If None, type will not be enforced\n :return:\n \"\"\"\n if not issubclass(typ, allowed_types):\n try:\n # more than 1 ?\n allowed_types[1]\n raise IsWrongType(wrong_value=typ, ref_type=allowed_types,\n help_msg='Value should be a subclass of any of {ref_type}')\n except IndexError:\n # 1\n allowed_types = allowed_types[0]\n except TypeError:\n # 1\n pass\n raise IsWrongType(wrong_value=typ, ref_type=allowed_types)", "response": "Checks that the value is a subclass of the type specified in typ."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef validate(name, # type: str\n value, # type: Any\n enforce_not_none=True, # type: bool\n equals=None, # type: Any\n instance_of=None, # type: Union[Type, Tuple[Type]]\n subclass_of=None, # type: Union[Type, Tuple[Type]]\n is_in=None, # type: Container\n subset_of=None, # type: Set\n contains = None, # type: Union[Any, Iterable]\n superset_of=None, # type: Set\n min_value=None, # type: Any\n min_strict=False, # type: bool\n max_value=None, # type: Any\n max_strict=False, # type: bool\n length=None, # type: int\n min_len=None, # type: int\n min_len_strict=False, # type: bool\n max_len=None, # type: int\n max_len_strict=False, # type: bool\n custom=None, # type: Callable[[Any], Any]\n error_type=None, # type: Type[ValidationError]\n help_msg=None, # type: str\n **kw_context_args):\n \"\"\"\n A validation function for quick inline validation of `value`, with minimal capabilities:\n\n * None handling: reject None (enforce_not_none=True, default), or accept None silently (enforce_not_none=False)\n * Type validation: `value` should be an instance of any of `var_types` if provided\n * Value validation:\n * if `allowed_values` is provided, `value` should be in that set\n * if `min_value` (resp. `max_value`) is provided, `value` should be greater than it. Comparison is not strict by\n default and can be set to strict by setting `min_strict`, resp. `max_strict`, to `True`\n * if `min_len` (resp. `max_len`) is provided, `len(value)` should be greater than it. Comparison is not strict by\n default and can be set to strict by setting `min_len_strict`, resp. `max_len_strict`, to `True`\n\n :param name: the applicative name of the checked value, that will be used in error messages\n :param value: the value to check\n :param enforce_not_none: boolean, default True. Whether to enforce that `value` is not None.\n :param equals: an optional value to enforce.\n :param instance_of: optional type(s) to enforce. If a tuple of types is provided it is considered alternate types: one\n match is enough to succeed. If None, type will not be enforced\n :param subclass_of: optional type(s) to enforce. If a tuple of types is provided it is considered alternate types: one\n match is enough to succeed. If None, type will not be enforced\n :param is_in: an optional set of allowed values.\n :param subset_of: an optional superset for the variable\n :param contains: an optional value that the variable should contain (value in variable == True)\n :param superset_of: an optional subset for the variable\n :param min_value: an optional minimum value\n :param min_strict: if True, only values strictly greater than `min_value` will be accepted\n :param max_value: an optional maximum value\n :param max_strict: if True, only values strictly lesser than `max_value` will be accepted\n :param length: an optional strict length\n :param min_len: an optional minimum length\n :param min_len_strict: if True, only values with length strictly greater than `min_len` will be accepted\n :param max_len: an optional maximum length\n :param max_len_strict: if True, only values with length strictly lesser than `max_len` will be accepted\n :param custom: a custom base validation function or list of base validation functions to use. This is the same\n syntax than for valid8 decorators. A callable, a tuple(callable, help_msg_str), a tuple(callable, failure_type),\n or a list of several such elements. Nested lists are supported and indicate an implicit `and_`. Tuples indicate\n an implicit `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be\n used instead of callables, they will be transformed to functions automatically.\n :param error_type: a subclass of `ValidationError` to raise in case of validation failure. By default a\n `ValidationError` will be raised with the provided `help_msg`\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param kw_context_args: optional contextual information to store in the exception, and that may be also used\n to format the help message\n :return: nothing in case of success. Otherwise, raises a ValidationError\n \"\"\"\n\n # backwards compatibility\n instance_of = instance_of or (kw_context_args.pop('allowed_types') if 'allowed_types' in kw_context_args else None)\n is_in = is_in or (kw_context_args.pop('allowed_values') if 'allowed_values' in kw_context_args else None)\n\n try:\n # the following corresponds to an inline version of\n # - _none_rejecter in base.py\n # - gt/lt in comparables.py\n # - is_in/contains/subset_of/superset_of/has_length/minlen/maxlen/is_in in collections.py\n # - instance_of/subclass_of in types.py\n\n # try (https://github.com/orf/inliner) to perform the inlining below automatically without code duplication ?\n # > maybe not because quite dangerous (AST mod) and below we skip the \"return True\" everywhere for performance\n #\n # Another alternative: easy Cython compiling https://github.com/AlanCristhian/statically\n # > but this is not py2 compliant\n\n if value is None:\n # inlined version of _none_rejecter in base.py\n if enforce_not_none:\n raise ValueIsNone(wrong_value=value)\n # raise MissingMandatoryParameterException('Error, ' + name + '\" is mandatory, it should be non-None')\n\n # else do nothing and return\n\n else:\n if equals is not None:\n if value != equals:\n raise NotEqual(wrong_value=value, ref_value=equals)\n\n if instance_of is not None:\n assert_instance_of(value, instance_of)\n\n if subclass_of is not None:\n assert_subclass_of(value, subclass_of)\n\n if is_in is not None:\n # inlined version of is_in(allowed_values=allowed_values)(value) without 'return True'\n if value not in is_in:\n raise NotInAllowedValues(wrong_value=value, allowed_values=is_in)\n\n if contains is not None:\n # inlined version of contains(ref_value=contains)(value) without 'return True'\n if contains not in value:\n raise DoesNotContainValue(wrong_value=value, ref_value=contains)\n\n if subset_of is not None:\n # inlined version of is_subset(reference_set=subset_of)(value)\n missing = value - subset_of\n if len(missing) != 0:\n raise NotSubset(wrong_value=value, reference_set=subset_of, unsupported=missing)\n\n if superset_of is not None:\n # inlined version of is_superset(reference_set=superset_of)(value)\n missing = superset_of - value\n if len(missing) != 0:\n raise NotSuperset(wrong_value=value, reference_set=superset_of, missing=missing)\n\n if min_value is not None:\n # inlined version of gt(min_value=min_value, strict=min_strict)(value) without 'return True'\n if min_strict:\n if not value > min_value:\n raise TooSmall(wrong_value=value, min_value=min_value, strict=True)\n else:\n if not value >= min_value:\n raise TooSmall(wrong_value=value, min_value=min_value, strict=False)\n\n if max_value is not None:\n # inlined version of lt(max_value=max_value, strict=max_strict)(value) without 'return True'\n if max_strict:\n if not value < max_value:\n raise TooBig(wrong_value=value, max_value=max_value, strict=True)\n else:\n if not value <= max_value:\n raise TooBig(wrong_value=value, max_value=max_value, strict=False)\n\n if length is not None:\n # inlined version of has_length() without 'return True'\n if len(value) != length:\n raise WrongLength(wrong_value=value, ref_length=length)\n\n if min_len is not None:\n # inlined version of minlen(min_length=min_len, strict=min_len_strict)(value) without 'return True'\n if min_len_strict:\n if not len(value) > min_len:\n raise TooShort(wrong_value=value, min_length=min_len, strict=True)\n else:\n if not len(value) >= min_len:\n raise TooShort(wrong_value=value, min_length=min_len, strict=False)\n\n if max_len is not None:\n # inlined version of maxlen(max_length=max_len, strict=max_len_strict)(value) without 'return True'\n if max_len_strict:\n if not len(value) < max_len:\n raise TooLong(wrong_value=value, max_length=max_len, strict=True)\n else:\n if not len(value) <= max_len:\n raise TooLong(wrong_value=value, max_length=max_len, strict=False)\n\n except Exception as e:\n err = _QUICK_VALIDATOR._create_validation_error(name, value, validation_outcome=e, error_type=error_type,\n help_msg=help_msg, **kw_context_args)\n raise_(err)\n\n if custom is not None:\n # traditional custom validator\n assert_valid(name, value, custom, error_type=error_type, help_msg=help_msg, **kw_context_args)\n else:\n # basic (and not enough) check to verify that there was no typo leading an argument to be put in kw_context_args\n if error_type is None and help_msg is None and len(kw_context_args) > 0:\n raise ValueError(\"Keyword context arguments have been provided but help_msg and error_type are not: {}\"\n \"\".format(kw_context_args))", "response": "Generic validation function for quick inline validation of a value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef gt(min_value, # type: Any\n strict=False # type: bool\n ):\n \"\"\"\n 'Greater than' validation_function generator.\n Returns a validation_function to check that x >= min_value (strict=False, default) or x > min_value (strict=True)\n\n :param min_value: minimum value for x\n :param strict: Boolean flag to switch between x >= min_value (strict=False) and x > min_value (strict=True)\n :return:\n \"\"\"\n if strict:\n def gt_(x):\n if x > min_value:\n return True\n else:\n # raise Failure('x > ' + str(min_value) + ' does not hold for x=' + str(x))\n # '{val} is not strictly greater than {ref}'\n raise TooSmall(wrong_value=x, min_value=min_value, strict=True)\n else:\n def gt_(x):\n if x >= min_value:\n return True\n else:\n # raise Failure('x >= ' + str(min_value) + ' does not hold for x=' + str(x))\n # '{val} is not greater than {ref}'\n raise TooSmall(wrong_value=x, min_value=min_value, strict=False)\n\n gt_.__name__ = '{}greater_than_{}'.format('strictly_' if strict else '', min_value)\n return gt_", "response": "Returns a validation_function generator. It returns a validation_function to check that x > = min_value"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef lt(max_value, # type: Any\n strict=False # type: bool\n ):\n \"\"\"\n 'Lesser than' validation_function generator.\n Returns a validation_function to check that x <= max_value (strict=False, default) or x < max_value (strict=True)\n\n :param max_value: maximum value for x\n :param strict: Boolean flag to switch between x <= max_value (strict=False) and x < max_value (strict=True)\n :return:\n \"\"\"\n if strict:\n def lt_(x):\n if x < max_value:\n return True\n else:\n # raise Failure('x < ' + str(max_value) + ' does not hold for x=' + str(x))\n # '{val} is not strictly lesser than {ref}'\n raise TooBig(wrong_value=x, max_value=max_value, strict=True)\n else:\n def lt_(x):\n if x <= max_value:\n return True\n else:\n # raise Failure('x <= ' + str(max_value) + ' does not hold for x=' + str(x))\n # '{val} is not lesser than {ref}'\n raise TooBig(wrong_value=x, max_value=max_value, strict=False)\n\n lt_.__name__ = '{}lesser_than_{}'.format('strictly_' if strict else '', max_value)\n return lt_", "response": "Returns a validation_function generator. It checks that x < max_value and returns True if x < max_value"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef between(min_val, # type: Any\n max_val, # type: Any\n open_left=False, # type: bool\n open_right=False # type: bool\n ):\n \"\"\"\n 'Is between' validation_function generator.\n Returns a validation_function to check that min_val <= x <= max_val (default). open_right and open_left flags allow\n to transform each side into strict mode. For example setting open_left=True will enforce min_val < x <= max_val\n\n :param min_val: minimum value for x\n :param max_val: maximum value for x\n :param open_left: Boolean flag to turn the left inequality to strict mode\n :param open_right: Boolean flag to turn the right inequality to strict mode\n :return:\n \"\"\"\n if open_left and open_right:\n def between_(x):\n if (min_val < x) and (x < max_val):\n return True\n else:\n # raise Failure('{} < x < {} does not hold for x={}'.format(min_val, max_val, x))\n raise NotInRange(wrong_value=x, min_value=min_val, left_strict=True,\n max_value=max_val, right_strict=True)\n elif open_left:\n def between_(x):\n if (min_val < x) and (x <= max_val):\n return True\n else:\n # raise Failure('between: {} < x <= {} does not hold for x={}'.format(min_val, max_val, x))\n raise NotInRange(wrong_value=x, min_value=min_val, left_strict=True,\n max_value=max_val, right_strict=False)\n elif open_right:\n def between_(x):\n if (min_val <= x) and (x < max_val):\n return True\n else:\n # raise Failure('between: {} <= x < {} does not hold for x={}'.format(min_val, max_val, x))\n raise NotInRange(wrong_value=x, min_value=min_val, left_strict=False,\n max_value=max_val, right_strict=True)\n else:\n def between_(x):\n if (min_val <= x) and (x <= max_val):\n return True\n else:\n # raise Failure('between: {} <= x <= {} does not hold for x={}'.format(min_val, max_val, x))\n raise NotInRange(wrong_value=x, min_value=min_val, left_strict=False,\n max_value=max_val, right_strict=False)\n\n between_.__name__ = 'between_{}_and_{}'.format(min_val, max_val)\n return between_", "response": "Is between validation_function generator."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _sub_process_main(started_event: Event,\n channel_name: str,\n connection: Connection,\n consumer_configuration: BrightsideConsumerConfiguration,\n consumer_factory: Callable[[Connection, BrightsideConsumerConfiguration, logging.Logger], BrightsideConsumer],\n command_processor_factory: Callable[[str], CommandProcessor],\n mapper_func: Callable[[BrightsideMessage], Request]) -> None:\n \"\"\"\n This is the main method for the sub=process, everything we need to create the message pump and\n channel it needs to be passed in as parameters that can be pickled as when we run they will be serialized\n into this process. The data should be value types, not reference types as we will receive a copy of the original.\n Inter-process communication is signalled by the event - to indicate startup - and the pipeline to facilitate a\n sentinel or stop message\n :param started_event: Used by the sub-process to signal that it is ready\n :param channel_name: The name we want to give the channel to the broker for identification\n :param connection: The 'broker' connection\n :param consumer_configuration: How to configure our consumer of messages from the channel\n :param consumer_factory: Callback to create the consumer. User code as we don't know what consumer library they\n want to use. Arame? Something else?\n :param command_processor_factory: Callback to register subscribers, policies, and task queues then build command\n processor. User code that provides us with their requests and handlers\n :param mapper_func: We need to map between messages on the wire and our handlers\n :return:\n \"\"\"\n\n logger = logging.getLogger(__name__)\n consumer = consumer_factory(connection, consumer_configuration, logger)\n channel = Channel(name=channel_name, consumer=consumer, pipeline=consumer_configuration.pipeline)\n\n # TODO: Fix defaults that need passed in config values\n command_processor = command_processor_factory(channel_name)\n message_pump = MessagePump(command_processor=command_processor, channel=channel, mapper_func=mapper_func,\n timeout=500, unacceptable_message_limit=None, requeue_count=None)\n\n logger.debug(\"Starting the message pump for %s\", channel_name)\n message_pump.run(started_event)", "response": "This is the main method for the sub - process. It is called by the main function of the main function of the sub - process."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nruns migrations in offline mode.", "response": "def run_migrations_offline():\n \"\"\"Run migrations in 'offline' mode.\n\n This configures the context with just a URL\n and not an Engine, though an Engine is acceptable\n here as well. By skipping the Engine creation\n we don't even need a DBAPI to be available.\n\n Calls to context.execute() here emit the given string to the\n script output.\n\n \"\"\"\n app_conf = dci_config.generate_conf()\n url = app_conf['SQLALCHEMY_DATABASE_URI']\n context.configure(\n url=url, target_metadata=target_metadata, literal_binds=True,\n )\n\n with context.begin_transaction():\n context.run_migrations()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef run_migrations_online():\n app_conf = dci_config.generate_conf()\n connectable = dci_config.get_engine(app_conf)\n with connectable.connect() as connection:\n context.configure(\n connection=connection,\n target_metadata=target_metadata,\n )\n\n with context.begin_transaction():\n context.run_migrations()", "response": "Run migrations in online mode."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends a 401 reject response that enables basic auth.", "response": "def reject():\n \"\"\"Sends a 401 reject response that enables basic auth.\"\"\"\n\n auth_message = ('Could not verify your access level for that URL.'\n 'Please login with proper credentials.')\n auth_message = json.dumps({'_status': 'Unauthorized',\n 'message': auth_message})\n\n headers = {'WWW-Authenticate': 'Basic realm=\"Login required\"'}\n return flask.Response(auth_message, 401, headers=headers,\n content_type='application/json')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmodifies a contact. Returns status message Optional Parameters: * name -- Contact name Type: String * email -- Contact email address Type: String * cellphone -- Cellphone number, without the country code part. In some countries you are supposed to exclude leading zeroes. (Requires countrycode and countryiso) Type: String * countrycode -- Cellphone country code (Requires cellphone and countryiso) Type: String * countryiso -- Cellphone country ISO code. For example: US (USA), GB (Britain) or SE (Sweden) (Requires cellphone and countrycode) Type: String * defaultsmsprovider -- Default SMS provider Type: String ['clickatell', 'bulksms', 'esendex', 'cellsynt'] * directtwitter -- Send tweets as direct messages Type: Boolean Default: True * twitteruser -- Twitter user Type: String", "response": "def modify(self, **kwargs):\n \"\"\"Modify a contact.\n\n Returns status message\n\n Optional Parameters:\n\n * name -- Contact name\n Type: String\n\n * email -- Contact email address\n Type: String\n\n * cellphone -- Cellphone number, without the country code part. In\n some countries you are supposed to exclude leading zeroes.\n (Requires countrycode and countryiso)\n Type: String\n\n * countrycode -- Cellphone country code (Requires cellphone and\n countryiso)\n Type: String\n\n * countryiso -- Cellphone country ISO code. For example: US (USA),\n GB (Britain) or SE (Sweden) (Requires cellphone and\n countrycode)\n Type: String\n\n * defaultsmsprovider -- Default SMS provider\n Type: String ['clickatell', 'bulksms', 'esendex',\n 'cellsynt']\n\n * directtwitter -- Send tweets as direct messages\n Type: Boolean\n Default: True\n\n * twitteruser -- Twitter user\n Type: String\n \"\"\"\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['email', 'cellphone', 'countrycode', 'countryiso',\n 'defaultsmsprovider', 'directtwitter',\n 'twitteruser', 'name']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument ' +\n 'of .modify()\\n')\n\n response = self.pingdom.request('PUT', 'notification_contacts/%s' % self.id, kwargs)\n\n return response.json()['message']"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nestablishes a connection to the broker.", "response": "def _establish_connection(self, conn: BrokerConnection) -> None:\n \"\"\"\n We don't use a pool here. We only have one consumer connection per process, so\n we get no value from a pool, and we want to use a heartbeat to keep the consumer\n collection alive, which does not work with a pool\n :return: the connection to the transport\n \"\"\"\n try:\n self._logger.debug(\"Establishing connection.\")\n self._conn = conn.ensure_connection(max_retries=3)\n self._logger.debug('Got connection: %s', conn.as_uri())\n except kombu_exceptions.OperationalError as oe:\n self._logger.error(\"Error connecting to RMQ, could not retry %s\", oe)\n # Try to clean up the mess\n if self._conn is not None:\n self._conn.close()\n else:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run_heartbeat_continuously(self) -> threading.Event:\n\n cancellation_event = threading.Event()\n\n # Effectively a no-op if we are not actually a long-running thread\n if not self._is_long_running_handler:\n return cancellation_event\n\n self._logger.debug(\"Running long running handler on %s\", self._conn)\n\n def _send_heartbeat(cnx: BrokerConnection, period: int, logger: logging.Logger) -> None:\n while not cancellation_event.is_set():\n cnx.heartbeat_check()\n time.sleep(period)\n logger.debug(\"Signalled to exit long-running handler heartbeat\")\n\n\n heartbeat_thread = threading.Thread(target=_send_heartbeat, args=(self._conn, 1, self._logger), daemon=True)\n self._logger.debug(\"Begin heartbeat thread for %s\", self._conn)\n heartbeat_thread.start()\n self._logger.debug(\"Heartbeat running on thread for %s\", self._conn)\n return cancellation_event", "response": "Runs the heartbeat on the broker thread."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _check(user, topic):\n # if export_control then check the team is associated to the product, ie.:\n # - the current user belongs to the product's team\n # OR\n # - the product's team belongs to the user's parents teams\n if topic['export_control']:\n product = v1_utils.verify_existence_and_get(topic['product_id'],\n models.PRODUCTS)\n return (user.is_in_team(product['team_id']) or\n product['team_id'] in user.parent_teams_ids)\n return False", "response": "Checks if the user is in the team of the product."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_stream_or_content_from_request(request):\n\n if request.stream.tell():\n logger.info('Request stream already consumed. '\n 'Storing file content using in-memory data.')\n return request.data\n\n else:\n logger.info('Storing file content using request stream.')\n return request.stream", "response": "Checks if the stream is already consumed and if so returns the content from the request stream. If not returns improper value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning result of performing the given XPath query on the given node. All known namespace prefix-to-URI mappings in the document are automatically included in the XPath invocation. If an empty/default namespace (i.e. None) is defined, this is converted to the prefix name '_' so it can be used despite empty namespace prefixes being unsupported by XPath.", "response": "def xpath_on_node(self, node, xpath, **kwargs):\n \"\"\"\n Return result of performing the given XPath query on the given node.\n\n All known namespace prefix-to-URI mappings in the document are\n automatically included in the XPath invocation.\n\n If an empty/default namespace (i.e. None) is defined, this is\n converted to the prefix name '_' so it can be used despite empty\n namespace prefixes being unsupported by XPath.\n \"\"\"\n if isinstance(node, etree._ElementTree):\n # Document node lxml.etree._ElementTree has no nsmap, lookup root\n root = self.get_impl_root(node)\n namespaces_dict = root.nsmap.copy()\n else:\n namespaces_dict = node.nsmap.copy()\n if 'namespaces' in kwargs:\n namespaces_dict.update(kwargs['namespaces'])\n # Empty namespace prefix is not supported, convert to '_' prefix\n if None in namespaces_dict:\n default_ns_uri = namespaces_dict.pop(None)\n namespaces_dict['_'] = default_ns_uri\n # Include XMLNS namespace if it's not already defined\n if not 'xmlns' in namespaces_dict:\n namespaces_dict['xmlns'] = nodes.Node.XMLNS_URI\n return node.xpath(xpath, namespaces=namespaces_dict)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns True if the given namespace name or value is defined in an ancestor of the given node.", "response": "def _is_ns_in_ancestor(self, node, name, value):\n \"\"\"\n Return True if the given namespace name/value is defined in an\n ancestor of the given node, meaning that the given node need not\n have its own attributes to apply that namespacing.\n \"\"\"\n curr_node = self.get_node_parent(node)\n while curr_node.__class__ == etree._Element:\n if (hasattr(curr_node, 'nsmap')\n and curr_node.nsmap.get(name) == value):\n return True\n for n, v in curr_node.attrib.items():\n if v == value and '{%s}' % nodes.Node.XMLNS_URI in n:\n return True\n curr_node = self.get_node_parent(curr_node)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfinds blog entries with the most comments: qs = generic_annotate(Entry.objects.public(), Comment.objects.public(), Count('comments__id')) for entry in qs: print entry.title, entry.score Find the highest rated foods: generic_annotate(Food, Rating, Avg('ratings__rating'), alias='avg') for food in qs: print food.name, '- average rating:', food.avg .. note:: In both of the above examples it is assumed that a GenericRelation exists on Entry to Comment (named \"comments\") and also on Food to Rating (named \"ratings\"). If a GenericRelation does *not* exist, the query will still return correct results but the code path will be different as it will use the fallback method. .. warning:: If the underlying column type differs between the qs_model's primary key and the generic_qs_model's foreign key column, it will use the fallback method, which can correctly CASTself. :param qs_model: A model or a queryset of objects you want to perform annotation on, e.g. blog entries :param generic_qs_model: A model or queryset containing a GFK, e.g. comments :param aggregator: an aggregation, from django.db.models, e.g. Count('id') or Avg('rating') :param gfk_field: explicitly specify the field w/the gfk :param alias: attribute name to use for annotation", "response": "def generic_annotate(qs_model, generic_qs_model, aggregator, gfk_field=None, alias='score'):\n \"\"\"\n Find blog entries with the most comments:\n \n qs = generic_annotate(Entry.objects.public(), Comment.objects.public(), Count('comments__id'))\n for entry in qs:\n print entry.title, entry.score\n \n Find the highest rated foods:\n \n generic_annotate(Food, Rating, Avg('ratings__rating'), alias='avg')\n for food in qs:\n print food.name, '- average rating:', food.avg\n \n .. note::\n In both of the above examples it is assumed that a GenericRelation exists\n on Entry to Comment (named \"comments\") and also on Food to Rating (named \"ratings\").\n If a GenericRelation does *not* exist, the query will still return correct\n results but the code path will be different as it will use the fallback method.\n \n .. warning::\n If the underlying column type differs between the qs_model's primary\n key and the generic_qs_model's foreign key column, it will use the fallback\n method, which can correctly CASTself.\n \n :param qs_model: A model or a queryset of objects you want to perform\n annotation on, e.g. blog entries\n :param generic_qs_model: A model or queryset containing a GFK, e.g. comments\n :param aggregator: an aggregation, from django.db.models, e.g. Count('id') or Avg('rating')\n :param gfk_field: explicitly specify the field w/the gfk\n :param alias: attribute name to use for annotation\n \"\"\"\n return fallback_generic_annotate(qs_model, generic_qs_model, aggregator, gfk_field, alias)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfind total number of comments on blog entries: generic_aggregate(Entry.objects.public(), Comment.objects.public(), Count('comments__id')) Find the average rating for foods starting with 'a': a_foods = Food.objects.filter(name__startswith='a') generic_aggregate(a_foods, Rating, Avg('ratings__rating')) .. note:: In both of the above examples it is assumed that a GenericRelation exists on Entry to Comment (named \"comments\") and also on Food to Rating (named \"ratings\"). If a GenericRelation does *not* exist, the query will still return correct results but the code path will be different as it will use the fallback method. .. warning:: If the underlying column type differs between the qs_model's primary key and the generic_qs_model's foreign key column, it will use the fallback method, which can correctly CASTself. :param qs_model: A model or a queryset of objects you want to perform annotation on, e.g. blog entries :param generic_qs_model: A model or queryset containing a GFK, e.g. comments :param aggregator: an aggregation, from django.db.models, e.g. Count('id') or Avg('rating') :param gfk_field: explicitly specify the field w/the gfk", "response": "def generic_aggregate(qs_model, generic_qs_model, aggregator, gfk_field=None):\n \"\"\"\n Find total number of comments on blog entries:\n \n generic_aggregate(Entry.objects.public(), Comment.objects.public(), Count('comments__id'))\n \n Find the average rating for foods starting with 'a':\n \n a_foods = Food.objects.filter(name__startswith='a')\n generic_aggregate(a_foods, Rating, Avg('ratings__rating'))\n \n .. note::\n In both of the above examples it is assumed that a GenericRelation exists\n on Entry to Comment (named \"comments\") and also on Food to Rating (named \"ratings\").\n If a GenericRelation does *not* exist, the query will still return correct\n results but the code path will be different as it will use the fallback method.\n \n .. warning::\n If the underlying column type differs between the qs_model's primary\n key and the generic_qs_model's foreign key column, it will use the fallback\n method, which can correctly CASTself.\n\n :param qs_model: A model or a queryset of objects you want to perform\n annotation on, e.g. blog entries\n :param generic_qs_model: A model or queryset containing a GFK, e.g. comments\n :param aggregator: an aggregation, from django.db.models, e.g. Count('id') or Avg('rating')\n :param gfk_field: explicitly specify the field w/the gfk\n \"\"\"\n return fallback_generic_aggregate(qs_model, generic_qs_model, aggregator, gfk_field)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generic_filter(generic_qs_model, filter_qs_model, gfk_field=None):\n generic_qs = normalize_qs_model(generic_qs_model)\n filter_qs = normalize_qs_model(filter_qs_model)\n \n if not gfk_field:\n gfk_field = get_gfk_field(generic_qs.model)\n \n pk_field_type = get_field_type(filter_qs.model._meta.pk)\n gfk_field_type = get_field_type(generic_qs.model._meta.get_field(gfk_field.fk_field))\n if pk_field_type != gfk_field_type:\n return fallback_generic_filter(generic_qs, filter_qs, gfk_field)\n \n return generic_qs.filter(**{\n gfk_field.ct_field: ContentType.objects.get_for_model(filter_qs.model),\n '%s__in' % gfk_field.fk_field: filter_qs.values('pk'),\n })", "response": "Filter the generic queryset to only show ratings made on entries that start with a"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating random etag based on MD5.", "response": "def gen_etag():\n \"\"\"Generate random etag based on MD5.\"\"\"\n\n my_salt = gen_uuid()\n if six.PY2:\n my_salt = my_salt.decode('utf-8')\n elif six.PY3:\n my_salt = my_salt.encode('utf-8')\n md5 = hashlib.md5()\n md5.update(my_salt)\n return md5.hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete(self):\n\n response = self.pingdom.request('DELETE',\n 'reports.shared/%s' % self.id)\n return response.json()['message']", "response": "Delete this email report"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_multiple_of(ref):\n def is_multiple_of_ref(x):\n if x % ref == 0:\n return True\n else:\n raise IsNotMultipleOf(wrong_value=x, ref=ref)\n # raise Failure('x % {ref} == 0 does not hold for x={val}'.format(ref=ref, val=x))\n\n is_multiple_of_ref.__name__ = 'is_multiple_of_{}'.format(ref)\n return is_multiple_of_ref", "response": "Validates that x is a multiple of the reference."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets all components of a topic.", "response": "def get_all_components(user, topic_id):\n \"\"\"Get all components of a topic.\"\"\"\n\n args = schemas.args(flask.request.args.to_dict())\n\n query = v1_utils.QueryBuilder(_TABLE, args, _C_COLUMNS)\n\n query.add_extra_condition(sql.and_(\n _TABLE.c.topic_id == topic_id,\n _TABLE.c.state != 'archived'))\n\n nb_rows = query.get_number_of_rows()\n rows = query.execute(fetchall=True)\n rows = v1_utils.format_result(rows, _TABLE.name, args['embed'],\n _EMBED_MANY)\n\n # Return only the component which have the export_control flag set to true\n #\n if user.is_not_super_admin():\n rows = [row for row in rows if row['export_control']]\n\n return flask.jsonify({'components': rows, '_meta': {'count': nb_rows}})"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the component types of a topic.", "response": "def get_component_types_from_topic(topic_id, db_conn=None):\n \"\"\"Returns the component types of a topic.\"\"\"\n db_conn = db_conn or flask.g.db_conn\n query = sql.select([models.TOPICS]).\\\n where(models.TOPICS.c.id == topic_id)\n topic = db_conn.execute(query).fetchone()\n topic = dict(topic)\n return topic['component_types']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning either the topic component types or the rconfigration s component types.", "response": "def get_component_types(topic_id, remoteci_id, db_conn=None):\n \"\"\"Returns either the topic component types or the rconfigration's\n component types.\"\"\"\n\n db_conn = db_conn or flask.g.db_conn\n rconfiguration = remotecis.get_remoteci_configuration(topic_id,\n remoteci_id,\n db_conn=db_conn)\n\n # if there is no rconfiguration associated to the remoteci or no\n # component types then use the topic's one.\n if (rconfiguration is not None and\n rconfiguration['component_types'] is not None):\n component_types = rconfiguration['component_types']\n else:\n component_types = get_component_types_from_topic(topic_id,\n db_conn=db_conn)\n\n return component_types, rconfiguration"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the last set of components of a given type of a topic.", "response": "def get_last_components_by_type(component_types, topic_id, db_conn=None):\n \"\"\"For each component type of a topic, get the last one.\"\"\"\n db_conn = db_conn or flask.g.db_conn\n schedule_components_ids = []\n for ct in component_types:\n where_clause = sql.and_(models.COMPONENTS.c.type == ct,\n models.COMPONENTS.c.topic_id == topic_id,\n models.COMPONENTS.c.export_control == True,\n models.COMPONENTS.c.state == 'active') # noqa\n query = (sql.select([models.COMPONENTS.c.id])\n .where(where_clause)\n .order_by(sql.desc(models.COMPONENTS.c.created_at)))\n cmpt_id = db_conn.execute(query).fetchone()\n\n if cmpt_id is None:\n msg = 'Component of type \"%s\" not found or not exported.' % ct\n raise dci_exc.DCIException(msg, status_code=412)\n\n cmpt_id = cmpt_id[0]\n if cmpt_id in schedule_components_ids:\n msg = ('Component types %s malformed: type %s duplicated.' %\n (component_types, ct))\n raise dci_exc.DCIException(msg, status_code=412)\n schedule_components_ids.append(cmpt_id)\n return schedule_components_ids"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef verify_and_get_components_ids(topic_id, components_ids, component_types,\n db_conn=None):\n \"\"\"Process some verifications of the provided components ids.\"\"\"\n db_conn = db_conn or flask.g.db_conn\n if len(components_ids) != len(component_types):\n msg = 'The number of component ids does not match the number ' \\\n 'of component types %s' % component_types\n raise dci_exc.DCIException(msg, status_code=412)\n\n # get the components from their ids\n schedule_component_types = set()\n for c_id in components_ids:\n where_clause = sql.and_(models.COMPONENTS.c.id == c_id,\n models.COMPONENTS.c.topic_id == topic_id,\n models.COMPONENTS.c.export_control == True, # noqa\n models.COMPONENTS.c.state == 'active')\n query = (sql.select([models.COMPONENTS])\n .where(where_clause))\n cmpt = db_conn.execute(query).fetchone()\n\n if cmpt is None:\n msg = 'Component id %s not found or not exported' % c_id\n raise dci_exc.DCIException(msg, status_code=412)\n cmpt = dict(cmpt)\n\n if cmpt['type'] in schedule_component_types:\n msg = ('Component types malformed: type %s duplicated.' %\n cmpt['type'])\n raise dci_exc.DCIException(msg, status_code=412)\n schedule_component_types.add(cmpt['type'])\n return components_ids", "response": "Verify that the provided components ids are valid and return the list of components ids that are valid."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef retrieve_tags_from_component(user, c_id):\n JCT = models.JOIN_COMPONENTS_TAGS\n query = (sql.select([models.TAGS])\n .select_from(JCT.join(models.TAGS))\n .where(JCT.c.component_id == c_id))\n rows = flask.g.db_conn.execute(query)\n\n return flask.jsonify({'tags': rows, '_meta': {'count': rows.rowcount}})", "response": "Retrieve all tags attached to a component."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_tag_for_component(user, c_id):\n\n v1_utils.verify_existence_and_get(c_id, _TABLE)\n\n values = {\n 'component_id': c_id\n }\n\n component_tagged = tags.add_tag_to_resource(values,\n models.JOIN_COMPONENTS_TAGS)\n\n return flask.Response(json.dumps(component_tagged), 201,\n content_type='application/json')", "response": "Add a tag on a specific component."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete_tag_for_component(user, c_id, tag_id):\n # Todo : check c_id and tag_id exist in db\n\n query = _TABLE_TAGS.delete().where(_TABLE_TAGS.c.tag_id == tag_id and\n _TABLE_TAGS.c.component_id == c_id)\n\n try:\n flask.g.db_conn.execute(query)\n except sa_exc.IntegrityError:\n raise dci_exc.DCICreationConflict(_TABLE_TAGS.c.tag_id, 'tag_id')\n\n return flask.Response(None, 204, content_type='application/json')", "response": "Delete a tag on a specific component."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef should_be_hidden_as_cause(exc):\n # reduced traceback in case of HasWrongType (instance_of checks)\n from valid8.validation_lib.types import HasWrongType, IsWrongType\n return isinstance(exc, (HasWrongType, IsWrongType))", "response": "Used everywhere to decide if some exception type should be hidden as cause of an error"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_error_of_type(exc, ref_type):\n if isinstance(exc, ref_type):\n return True\n elif hasattr(exc, '__cause__') and exc.__cause__ is not None:\n return is_error_of_type(exc.__cause__, ref_type)", "response": "Helper function to determine if some exception is of some type"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _failure_raiser(validation_callable, # type: Callable\n failure_type=None, # type: Type[WrappingFailure]\n help_msg=None, # type: str\n **kw_context_args):\n # type: (...) -> Callable\n \"\"\"\n Wraps the provided validation function so that in case of failure it raises the given failure_type or a WrappingFailure\n with the given help message.\n\n :param validation_callable:\n :param failure_type: an optional subclass of `WrappingFailure` that should be raised in case of failure, instead of\n `WrappingFailure`.\n :param help_msg: an optional string help message for the raised `WrappingFailure` (if no failure_type is provided)\n :param kw_context_args: optional context arguments for the custom failure message\n :return:\n \"\"\"\n\n # check failure type\n if failure_type is not None and help_msg is not None:\n raise ValueError('Only one of failure_type and help_msg can be set at the same time')\n\n # convert mini-lambdas to functions if needed\n validation_callable = as_function(validation_callable)\n\n # create wrapper\n # option (a) use the `decorate()` helper method to preserve name and signature of the inner object\n # ==> NO, we want to support also non-function callable objects\n\n # option (b) simply create a wrapper manually\n def raiser(x):\n \"\"\" Wraps validation_callable to raise a failure_type_or_help_msg in case of failure \"\"\"\n\n try:\n # perform validation\n res = validation_callable(x)\n\n except Exception as e:\n # no need to raise from e since the __cause__ is already set in the constructor: we can safely commonalize\n res = e\n\n if not result_is_success(res):\n typ = failure_type or WrappingFailure\n exc = typ(wrapped_func=validation_callable, wrong_value=x, validation_outcome=res,\n help_msg=help_msg, **kw_context_args)\n raise exc\n\n # set a name so that the error messages are more user-friendly\n\n # NO, Do not include the callable type or error message in the name since it is only used in error messages where\n # they will appear anyway !\n # ---\n # if help_msg or failure_type:\n # raiser.__name__ = 'failure_raiser({}, {})'.format(get_callable_name(validation_callable),\n # help_msg or failure_type.__name__)\n # else:\n # ---\n # raiser.__name__ = 'failure_raiser({})'.format(get_callable_name(validation_callable))\n raiser.__name__ = get_callable_name(validation_callable)\n # Note: obviously this can hold as long as we do not check the name of this object in any other context than\n # raising errors. If we want to support this, then creating a callable object with everything in the fields will be\n # probably more appropriate so that error messages will be able to display the inner name, while repr() will still\n # say that this is a failure raiser.\n # TODO consider transforming failure_raiser into a class (see comment above)\n\n return raiser", "response": "A function that wraps a validation function so that in case of failure it raises a given failure_type or a WrappingFailure\nWorkItem with the given help message."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a validation callable that accepts None values silently.", "response": "def _none_accepter(validation_callable # type: Callable\n ):\n # type: (...) -> Callable\n \"\"\"\n Wraps the given validation callable to accept None values silently. When a None value is received by the wrapper,\n it is not passed to the validation_callable and instead this function will return True. When any other value is\n received the validation_callable is called as usual.\n\n Note: the created wrapper has the same same than the validation callable for more user-friendly error messages\n\n :param validation_callable:\n :return:\n \"\"\"\n # option (a) use the `decorate()` helper method to preserve name and signature of the inner object\n # ==> NO, we want to support also non-function callable objects\n\n # option (b) simply create a wrapper manually\n def accept_none(x):\n if x is not None:\n # proceed with validation as usual\n return validation_callable(x)\n else:\n # value is None: skip validation\n return True\n\n # set a name so that the error messages are more user-friendly\n accept_none.__name__ = 'skip_on_none({})'.format(get_callable_name(validation_callable))\n\n return accept_none"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a validation callable that rejects None values.", "response": "def _none_rejecter(validation_callable # type: Callable\n ):\n # type: (...) -> Callable\n \"\"\"\n Wraps the given validation callable to reject None values. When a None value is received by the wrapper,\n it is not passed to the validation_callable and instead this function will raise a WrappingFailure. When any other value is\n received the validation_callable is called as usual.\n\n :param validation_callable:\n :return:\n \"\"\"\n # option (a) use the `decorate()` helper method to preserve name and signature of the inner object\n # ==> NO, we want to support also non-function callable objects\n\n # option (b) simply create a wrapper manually\n def reject_none(x):\n if x is not None:\n return validation_callable(x)\n else:\n raise ValueIsNone(wrong_value=x)\n\n # set a name so that the error messages are more user-friendly ==> NO ! here we want to see the checker\n reject_none.__name__ = 'reject_none({})'.format(get_callable_name(validation_callable))\n\n return reject_none"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_details(self):\n strval = str(self.wrong_value)\n if len(strval) > self.__max_str_length_displayed__:\n return '(Actual value is too big to be printed in this message)'\n else:\n return 'Wrong value: [{}]'.format(self.wrong_value)", "response": "Returns the details of the current value of the class attribute."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a string describing the function and outcome", "response": "def get_details(self):\n \"\"\" Overrides the method in Failure so as to add a few details about the wrapped function and outcome \"\"\"\n\n if isinstance(self.validation_outcome, Exception):\n if isinstance(self.validation_outcome, Failure):\n # do not say again what was the value, it is already mentioned inside :)\n end_str = ''\n else:\n end_str = ' for value [{value}]'.format(value=self.wrong_value)\n\n contents = 'Function [{wrapped}] raised [{exception}: {details}]{end}.' \\\n ''.format(wrapped=get_callable_name(self.wrapped_func),\n exception=type(self.validation_outcome).__name__, details=self.validation_outcome,\n end=end_str)\n else:\n contents = 'Function [{wrapped}] returned [{result}] for value [{value}].' \\\n ''.format(wrapped=get_callable_name(self.wrapped_func), result=self.validation_outcome,\n value=self.wrong_value)\n\n return contents"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_context_for_help_msgs(self, context_dict):\n context_dict = copy(context_dict)\n context_dict['wrapped_func'] = get_callable_name(context_dict['wrapped_func'])\n return context_dict", "response": "This method is used to get the context dictionary for the help msgs."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef validate_arg(f,\n arg_name,\n *validation_func, # type: ValidationFuncs\n **kwargs\n ):\n # type: (...) -> Callable\n \"\"\"\n A decorator to apply function input validation for the given argument name, with the provided base validation\n function(s). You may use several such decorators on a given function as long as they are stacked on top of each\n other (no external decorator in the middle)\n\n :param arg_name:\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param error_type: a subclass of ValidationError to raise in case of validation failure. By default a\n ValidationError will be raised with the provided help_msg\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param none_policy: describes how None values should be handled. See `NoneArgPolicy` for the various\n possibilities. Default is `NoneArgPolicy.ACCEPT_IF_OPTIONAl_ELSE_VALIDATE`.\n :param kw_context_args: optional contextual information to store in the exception, and that may be also used\n to format the help message\n :return: a function decorator, able to transform a function into a function that will perform input validation\n before executing the function's code everytime it is executed.\n \"\"\"\n return decorate_with_validation(f, arg_name, *validation_func, **kwargs)", "response": "Decorator to apply function input validation for the given argument name."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_out(*validation_func, # type: ValidationFuncs\n **kwargs):\n # type: (...) -> Callable\n \"\"\"\n A decorator to apply function output validation to this function's output, with the provided base validation\n function(s). You may use several such decorators on a given function as long as they are stacked on top of each\n other (no external decorator in the middle)\n\n :param validation_func: the base validation function or list of base validation functions to use. A callable, a\n tuple(callable, help_msg_str), a tuple(callable, failure_type), or a list of several such elements. Nested lists\n are supported and indicate an implicit `and_` (such as the main list). Tuples indicate an implicit\n `_failure_raiser`. [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead\n of callables, they will be transformed to functions automatically.\n :param none_policy: describes how None values should be handled. See `NoneArgPolicy` for the various\n possibilities. Default is `NoneArgPolicy.ACCEPT_IF_OPTIONAl_ELSE_VALIDATE`.\n :return: a function decorator, able to transform a function into a function that will perform input validation\n before executing the function's code everytime it is executed.\n \"\"\"\n def decorate(f):\n return decorate_with_validation(f, _OUT_KEY, *validation_func, **kwargs)\n return decorate", "response": "Decorator to apply function output validation to this function s output."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef decorate_cls_with_validation(cls,\n field_name, # type: str\n *validation_func, # type: ValidationFuncs\n **kwargs):\n # type: (...) -> Type[Any]\n \"\"\"\n This method is equivalent to decorating a class with the `@validate_field` decorator but can be used a posteriori.\n\n :param cls: the class to decorate\n :param field_name: the name of the argument to validate or _OUT_KEY for output validation\n :param validation_func: the validation function or\n list of validation functions to use. A validation function may be a callable, a tuple(callable, help_msg_str),\n a tuple(callable, failure_type), or a list of several such elements. Nested lists are supported and indicate an\n implicit `and_` (such as the main list). Tuples indicate an implicit `_failure_raiser`.\n [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead of callables, they\n will be transformed to functions automatically.\n :param error_type: a subclass of ValidationError to raise in case of validation failure. By default a\n ValidationError will be raised with the provided help_msg\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param none_policy: describes how None values should be handled. See `NoneArgPolicy` for the various possibilities.\n Default is `NoneArgPolicy.ACCEPT_IF_OPTIONAl_ELSE_REJECT`.\n :param kw_context_args: optional contextual information to store in the exception, and that may be also used\n to format the help message\n :return: the decorated function, that will perform input validation (using `_assert_input_is_valid`) before\n executing the function's code everytime it is executed.\n \"\"\"\n error_type, help_msg, none_policy = pop_kwargs(kwargs, [('error_type', None),\n ('help_msg', None),\n ('none_policy', None)], allow_others=True)\n # the rest of keyword arguments is used as context.\n kw_context_args = kwargs\n\n if not isclass(cls):\n raise TypeError('decorated cls should be a class')\n\n if hasattr(cls, field_name):\n # ** A class field with that name exist. Is it a descriptor ?\n\n var = cls.__dict__[field_name] # note: we cannot use getattr here\n\n if hasattr(var, '__set__') and callable(var.__set__):\n\n if isinstance(var, property):\n # *** OLD WAY which was losing type hints and default values (see var.__set__ signature) ***\n # properties are special beasts: their methods are method-wrappers (CPython) and can not have properties\n # so we have to create a wrapper (sic) before sending it to the main wrapping function\n # def func(inst, value):\n # var.__set__(inst, value)\n\n # *** NEW WAY : more elegant, use directly the setter provided by the user ***\n func = var.fset\n nb_args = 2\n elif ismethod(var.__set__):\n # bound method: normal. Let's access to the underlying function\n func = var.__set__.__func__\n nb_args = 3\n else:\n # strange.. but lets try to continue\n func = var.__set__\n nb_args = 3\n\n # retrieve target function signature, check it and retrieve the 3d param\n # since signature is \"def __set__(self, obj, val)\"\n func_sig = signature(func)\n if len(func_sig.parameters) != nb_args:\n raise ValueError(\"Class field '{}' is a valid class descriptor for class '{}' but it does not implement\"\n \" __set__ with the correct number of parameters, so it is not possible to add \"\n \"validation to it. See https://docs.python.org/3.6/howto/descriptor.html\".\n format(field_name, cls.__name__))\n # extract the correct name\n descriptor_arg_name = list(func_sig.parameters.items())[-1][0]\n\n # do the same than in decorate_with_validation but with a class field validator\n # new_setter = decorate_with_validation(func, descriptor_arg_name, *validation_func, help_msg=help_msg,\n # error_type=error_type, none_policy=none_policy,\n # _clazz_field_name_=field_name, **kw_context_args)\n\n # --create the new validator\n none_policy = none_policy or NoneArgPolicy.SKIP_IF_NONABLE_ELSE_VALIDATE\n new_validator = _create_function_validator(func, func_sig, descriptor_arg_name, *validation_func,\n none_policy=none_policy, error_type=error_type,\n help_msg=help_msg,\n validated_class=cls, validated_class_field_name=field_name,\n **kw_context_args)\n\n # -- create the new setter with validation\n new_setter = decorate_with_validators(func, func_signature=func_sig, **{descriptor_arg_name: new_validator})\n\n # replace the old one\n if isinstance(var, property):\n # properties are special beasts 2\n setattr(cls, field_name, var.setter(new_setter))\n else:\n # do not use type() for python 2 compat\n var.__class__.__set__ = new_setter\n\n elif (hasattr(var, '__get__') and callable(var.__get__)) \\\n or (hasattr(var, '__delete__') and callable(var.__delete__)):\n # this is a descriptor but it does not have any setter method: impossible to validate\n raise ValueError(\"Class field '{}' is a valid class descriptor for class '{}' but it does not implement \"\n \"__set__ so it is not possible to add validation to it. See \"\n \"https://docs.python.org/3.6/howto/descriptor.html\".format(field_name, cls.__name__))\n\n else:\n # this is not a descriptor: unsupported\n raise ValueError(\"Class field '{}.{}' is not a valid class descriptor, see \"\n \"https://docs.python.org/3.6/howto/descriptor.html\".format(cls.__name__, field_name))\n\n else:\n # ** No class field with that name exist\n\n # ? check for attrs ? > no specific need anymore, this is the same than annotating the constructor\n # if hasattr(cls, '__attrs_attrs__'): this was a proof of attrs-defined class\n\n # try to annotate the generated constructor\n try:\n init_func = cls.__init__\n if sys.version_info < (3, 0):\n try:\n # python 2 - we have to access the inner `im_func`\n init_func = cls.__init__.im_func\n except AttributeError:\n pass\n\n cls.__init__ = decorate_with_validation(init_func, field_name, *validation_func, help_msg=help_msg,\n _constructor_of_cls_=cls,\n error_type=error_type, none_policy=none_policy, **kw_context_args)\n\n except InvalidNameError:\n # the field was not found\n\n # TODO should we also check if a __setattr__ is defined ?\n # (for __setattr__ see https://stackoverflow.com/questions/15750522/class-properties-and-setattr/15751159)\n\n # finally raise an error\n raise ValueError(\"@validate_field definition exception: field '{}' can not be found in class '{}', and it \"\n \"is also not an input argument of the __init__ method.\".format(field_name, cls.__name__))\n\n return cls", "response": "This method is used to decorate a class with the validation function. It is equivalent to the function that will perform input validation on the object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef decorate_several_with_validation(func,\n _out_=None, # type: ValidationFuncs\n none_policy=None, # type: int\n **validation_funcs # type: ValidationFuncs\n ):\n # type: (...) -> Callable\n \"\"\"\n This method is equivalent to applying `decorate_with_validation` once for each of the provided arguments of\n the function `func` as well as output `_out_`. validation_funcs keyword arguments are validation functions for each\n arg name.\n\n Note that this method is less flexible than decorate_with_validation since\n * it does not allow to associate a custom error message or error type with each validation.\n * the none_policy is the same for all inputs and outputs\n\n :param func:\n :param _out_:\n :param validation_funcs:\n :param none_policy:\n :return: a function decorated with validation for all of the listed arguments and output if provided.\n \"\"\"\n\n # add validation for output if provided\n if _out_ is not None:\n func = decorate_with_validation(func, _OUT_KEY, _out_, none_policy=none_policy)\n\n # add validation for each of the listed arguments\n for att_name, att_validation_funcs in validation_funcs.items():\n func = decorate_with_validation(func, att_name, att_validation_funcs, none_policy=none_policy)\n\n return func", "response": "Decorate a function with validation for all of the provided arguments and output."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef decorate_with_validation(func,\n arg_name, # type: str\n *validation_func, # type: ValidationFuncs\n **kwargs):\n # type: (...) -> Callable\n \"\"\"\n This method is the inner method used in `@validate_io`, `@validate_arg` and `@validate_out`.\n It can be used if you with to perform decoration manually without a decorator.\n\n :param func:\n :param arg_name: the name of the argument to validate or _OUT_KEY for output validation\n :param validation_func: the validation function or\n list of validation functions to use. A validation function may be a callable, a tuple(callable, help_msg_str),\n a tuple(callable, failure_type), or a list of several such elements. Nested lists are supported and indicate an\n implicit `and_` (such as the main list). Tuples indicate an implicit `_failure_raiser`.\n [mini_lambda](https://smarie.github.io/python-mini-lambda/) expressions can be used instead of callables, they\n will be transformed to functions automatically.\n :param error_type: a subclass of ValidationError to raise in case of validation failure. By default a\n ValidationError will be raised with the provided help_msg\n :param help_msg: an optional help message to be used in the raised error in case of validation failure.\n :param none_policy: describes how None values should be handled. See `NoneArgPolicy` for the various possibilities.\n Default is `NoneArgPolicy.ACCEPT_IF_OPTIONAl_ELSE_REJECT`.\n :param kw_context_args: optional contextual information to store in the exception, and that may be also used\n to format the help message\n :return: the decorated function, that will perform input validation (using `_assert_input_is_valid`) before\n executing the function's code everytime it is executed.\n \"\"\"\n error_type, help_msg, none_policy, _constructor_of_cls_ = pop_kwargs(kwargs, [('error_type', None),\n ('help_msg', None),\n ('none_policy', None),\n ('_constructor_of_cls_', None)],\n allow_others=True)\n # the rest of keyword arguments is used as context.\n kw_context_args = kwargs\n\n none_policy = none_policy or NoneArgPolicy.SKIP_IF_NONABLE_ELSE_VALIDATE\n\n # retrieve target function signature\n func_sig = signature(func)\n\n # create the new validator\n if _constructor_of_cls_ is None:\n # standard method: input validator\n new_validator = _create_function_validator(func, func_sig, arg_name, *validation_func,\n none_policy=none_policy, error_type=error_type,\n help_msg=help_msg, **kw_context_args)\n else:\n # class constructor: field validator\n new_validator = _create_function_validator(func, func_sig, arg_name, *validation_func,\n none_policy=none_policy, error_type=error_type,\n help_msg=help_msg, validated_class=_constructor_of_cls_,\n validated_class_field_name=arg_name,\n **kw_context_args)\n\n # decorate or update decorator with this new validator\n return decorate_with_validators(func, func_signature=func_sig, **{arg_name: new_validator})", "response": "Decorate a function with input validation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the final NonePolicy that is used for validation.", "response": "def _get_final_none_policy_for_validator(is_nonable, # type: bool\n none_policy # type: NoneArgPolicy\n ):\n \"\"\"\n Depending on none_policy and of the fact that the target parameter is nonable or not, returns a corresponding\n NonePolicy\n\n :param is_nonable:\n :param none_policy:\n :return:\n \"\"\"\n if none_policy in {NonePolicy.VALIDATE, NonePolicy.SKIP, NonePolicy.FAIL}:\n none_policy_to_use = none_policy\n\n elif none_policy is NoneArgPolicy.SKIP_IF_NONABLE_ELSE_VALIDATE:\n none_policy_to_use = NonePolicy.SKIP if is_nonable else NonePolicy.VALIDATE\n\n elif none_policy is NoneArgPolicy.SKIP_IF_NONABLE_ELSE_FAIL:\n none_policy_to_use = NonePolicy.SKIP if is_nonable else NonePolicy.FAIL\n\n else:\n raise ValueError('Invalid none policy: ' + str(none_policy))\n return none_policy_to_use"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef decorate_with_validators(func,\n func_signature=None, # type: Signature\n **validators # type: Validator\n ):\n \"\"\"\n Utility method to decorate the provided function with the provided input and output Validator objects. Since this\n method takes Validator objects as argument, it is for advanced users.\n\n :param func: the function to decorate. It might already be decorated, this method will check it and wont create\n another wrapper in this case, simply adding the validators to the existing wrapper\n :param func_signature: the function's signature if it is already known (internal calls), otherwise it will be found\n again by inspection\n :param validators: a dictionary of arg_name (or _out_) => Validator or list of Validator\n :return:\n \"\"\"\n # first turn the dictionary values into lists only\n for arg_name, validator in validators.items():\n if not isinstance(validator, list):\n validators[arg_name] = [validator]\n\n if hasattr(func, '__wrapped__') and hasattr(func.__wrapped__, '__validators__'):\n # ---- This function is already wrapped by our validation wrapper ----\n\n # Update the dictionary of validators with the new validator(s)\n for arg_name, validator in validators.items():\n for v in validator:\n if arg_name in func.__wrapped__.__validators__:\n func.__wrapped__.__validators__[arg_name].append(v)\n else:\n func.__wrapped__.__validators__[arg_name] = [v]\n\n # return the function, no need to wrap it further (it is already wrapped)\n return func\n\n else:\n # ---- This function is not yet wrapped by our validator. ----\n\n # Store the dictionary of validators as an attribute of the function\n if hasattr(func, '__validators__'):\n raise ValueError('Function ' + str(func) + ' already has a defined __validators__ attribute, valid8 '\n 'decorators can not be applied on it')\n else:\n try:\n func.__validators__ = validators\n except AttributeError:\n raise ValueError(\"Error - Could not add validators list to function '%s'\" % func)\n\n # either reuse or recompute function signature\n func_signature = func_signature or signature(func)\n\n # create a wrapper with the same signature\n @wraps(func)\n def validating_wrapper(*args, **kwargs):\n \"\"\" This is the wrapper that will be called everytime the function is called \"\"\"\n\n # (a) Perform input validation by applying `_assert_input_is_valid` on all received arguments\n apply_on_each_func_args_sig(func, args, kwargs, func_signature,\n func_to_apply=_assert_input_is_valid,\n func_to_apply_params_dict=func.__validators__)\n\n # (b) execute the function as usual\n res = func(*args, **kwargs)\n\n # (c) validate output if needed\n if _OUT_KEY in func.__validators__:\n for validator in func.__validators__[_OUT_KEY]:\n validator.assert_valid(res)\n\n return res\n\n return validating_wrapper", "response": "This method is used to decorate a function with input and output Validator objects."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncalls by the `validating_wrapper` in the first step (a) `apply_on_each_func_args` for each function input before executing the function. It simply delegates to the validator. The signature of this function is hardcoded to correspond to `apply_on_each_func_args`'s behaviour and should therefore not be changed. :param input_value: the value to validate :param validator: the Validator object that will be applied on input_value_to_validate :param validated_func: the function for which this validation is performed. This is not used since the Validator knows it already, but we should not change the signature here. :param input_name: the name of the function input that is being validated :return: Nothing", "response": "def _assert_input_is_valid(input_value, # type: Any\n validators, # type: List[InputValidator]\n validated_func, # type: Callable\n input_name # type: str\n ):\n \"\"\"\n Called by the `validating_wrapper` in the first step (a) `apply_on_each_func_args` for each function input before\n executing the function. It simply delegates to the validator. The signature of this function is hardcoded to\n correspond to `apply_on_each_func_args`'s behaviour and should therefore not be changed.\n\n :param input_value: the value to validate\n :param validator: the Validator object that will be applied on input_value_to_validate\n :param validated_func: the function for which this validation is performed. This is not used since the Validator\n knows it already, but we should not change the signature here.\n :param input_name: the name of the function input that is being validated\n :return: Nothing\n \"\"\"\n for validator in validators:\n validator.assert_valid(input_name, input_value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a string that can be used to display the input variable and the function name.", "response": "def get_what_txt(self):\n \"\"\"\n Overrides the base behaviour defined in ValidationError in order to add details about the function.\n :return:\n \"\"\"\n return 'input [{var}] for function [{func}]'.format(var=self.get_variable_str(),\n func=self.validator.get_validated_func_display_name())"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a string that can be used to display the field name for the class.", "response": "def get_what_txt(self):\n \"\"\"\n Overrides the base behaviour defined in ValidationError in order to add details about the class field.\n :return:\n \"\"\"\n return 'field [{field}] for class [{clazz}]'.format(field=self.get_variable_str(),\n clazz=self.validator.get_validated_class_display_name())"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef generate_nonce_timestamp():\n global count\n rng = botan.rng().get(30)\n uuid4 = uuid.uuid4().bytes # 16 byte\n tmpnonce = (bytes(str(count).encode('utf-8'))) + uuid4 + rng\n nonce = tmpnonce[:41] # 41 byte (328 bit)\n count += 1\n return nonce", "response": "Generate unique nonce with counter uuid and rng."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef schedule_jobs(user):\n\n values = schemas.job_schedule.post(flask.request.json)\n\n values.update({\n 'id': utils.gen_uuid(),\n 'created_at': datetime.datetime.utcnow().isoformat(),\n 'updated_at': datetime.datetime.utcnow().isoformat(),\n 'etag': utils.gen_etag(),\n 'status': 'new',\n 'remoteci_id': user.id,\n 'user_agent': flask.request.environ.get('HTTP_USER_AGENT'),\n 'client_version': flask.request.environ.get(\n 'HTTP_CLIENT_VERSION'\n ),\n })\n\n topic_id = values.pop('topic_id')\n topic_id_secondary = values.pop('topic_id_secondary')\n components_ids = values.pop('components_ids')\n\n # check remoteci\n remoteci = v1_utils.verify_existence_and_get(user.id, models.REMOTECIS)\n if remoteci['state'] != 'active':\n message = 'RemoteCI \"%s\" is disabled.' % remoteci['id']\n raise dci_exc.DCIException(message, status_code=412)\n\n # check primary topic\n topic = v1_utils.verify_existence_and_get(topic_id, models.TOPICS)\n if topic['state'] != 'active':\n msg = 'Topic %s:%s not active.' % (topic_id, topic['name'])\n raise dci_exc.DCIException(msg, status_code=412)\n v1_utils.verify_team_in_topic(user, topic_id)\n\n # check secondary topic\n if topic_id_secondary:\n topic_secondary = v1_utils.verify_existence_and_get(\n topic_id_secondary, models.TOPICS)\n if topic_secondary['state'] != 'active':\n msg = 'Topic %s:%s not active.' % (topic_id_secondary,\n topic['name'])\n raise dci_exc.DCIException(msg, status_code=412)\n v1_utils.verify_team_in_topic(user, topic_id_secondary)\n\n dry_run = values.pop('dry_run')\n if dry_run:\n component_types = components.get_component_types_from_topic(topic_id)\n components_ids = components.get_last_components_by_type(\n component_types,\n topic_id\n )\n return flask.Response(\n json.dumps({'components_ids': components_ids, 'job': None}),\n 201,\n content_type='application/json'\n )\n\n remotecis.kill_existing_jobs(remoteci['id'])\n\n values = _build_job(topic_id, remoteci, components_ids, values,\n topic_id_secondary=topic_id_secondary)\n\n return flask.Response(json.dumps({'job': values}), 201,\n headers={'ETag': values['etag']},\n content_type='application/json')", "response": "Dispatch jobs to the server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_new_update_job_from_an_existing_job(user, job_id):\n\n values = {\n 'id': utils.gen_uuid(),\n 'created_at': datetime.datetime.utcnow().isoformat(),\n 'updated_at': datetime.datetime.utcnow().isoformat(),\n 'etag': utils.gen_etag(),\n 'status': 'new'\n }\n\n original_job_id = job_id\n original_job = v1_utils.verify_existence_and_get(original_job_id,\n models.JOBS)\n\n if not user.is_in_team(original_job['team_id']):\n raise dci_exc.Unauthorized()\n\n # get the remoteci\n remoteci_id = str(original_job['remoteci_id'])\n remoteci = v1_utils.verify_existence_and_get(remoteci_id,\n models.REMOTECIS)\n values.update({'remoteci_id': remoteci_id})\n\n # get the associated topic\n topic_id = str(original_job['topic_id'])\n v1_utils.verify_existence_and_get(topic_id, models.TOPICS)\n\n values.update({\n 'user_agent': flask.request.environ.get('HTTP_USER_AGENT'),\n 'client_version': flask.request.environ.get(\n 'HTTP_CLIENT_VERSION'\n ),\n })\n\n values = _build_job(topic_id, remoteci, [], values,\n update_previous_job_id=original_job_id)\n\n return flask.Response(json.dumps({'job': values}), 201,\n headers={'ETag': values['etag']},\n content_type='application/json')", "response": "Create a new job in the same topic as the job_id provided and return the updated job."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new job in the next topic of the topic of the provided job_id.", "response": "def create_new_upgrade_job_from_an_existing_job(user):\n \"\"\"Create a new job in the 'next topic' of the topic of\n the provided job_id.\"\"\"\n values = schemas.job_upgrade.post(flask.request.json)\n\n values.update({\n 'id': utils.gen_uuid(),\n 'created_at': datetime.datetime.utcnow().isoformat(),\n 'updated_at': datetime.datetime.utcnow().isoformat(),\n 'etag': utils.gen_etag(),\n 'status': 'new'\n })\n\n original_job_id = values.pop('job_id')\n original_job = v1_utils.verify_existence_and_get(original_job_id,\n models.JOBS)\n if not user.is_in_team(original_job['team_id']):\n raise dci_exc.Unauthorized()\n\n # get the remoteci\n remoteci_id = str(original_job['remoteci_id'])\n remoteci = v1_utils.verify_existence_and_get(remoteci_id,\n models.REMOTECIS)\n values.update({'remoteci_id': remoteci_id})\n\n # get the associated topic\n topic_id = str(original_job['topic_id'])\n topic = v1_utils.verify_existence_and_get(topic_id, models.TOPICS)\n\n values.update({\n 'user_agent': flask.request.environ.get('HTTP_USER_AGENT'),\n 'client_version': flask.request.environ.get(\n 'HTTP_CLIENT_VERSION'\n ),\n })\n\n next_topic_id = topic['next_topic_id']\n\n if not next_topic_id:\n raise dci_exc.DCIException(\n \"topic %s does not contains a next topic\" % topic_id)\n\n # instantiate a new job in the next_topic_id\n # todo(yassine): make possible the upgrade to choose specific components\n values = _build_job(next_topic_id, remoteci, [], values,\n previous_job_id=original_job_id)\n\n return flask.Response(json.dumps({'job': values}), 201,\n headers={'ETag': values['etag']},\n content_type='application/json')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget all jobs. If topic_id is not None, then return all the jobs with a topic pointed by topic_id.", "response": "def get_all_jobs(user, topic_id=None):\n \"\"\"Get all jobs.\n\n If topic_id is not None, then return all the jobs with a topic\n pointed by topic_id.\n \"\"\"\n # get the diverse parameters\n args = schemas.args(flask.request.args.to_dict())\n\n # build the query thanks to the QueryBuilder class\n query = v1_utils.QueryBuilder(_TABLE, args, _JOBS_COLUMNS)\n\n # add extra conditions for filtering\n\n # # If not admin nor rh employee then restrict the view to the team\n if user.is_not_super_admin() and not user.is_read_only_user():\n query.add_extra_condition(\n sql.or_(\n _TABLE.c.team_id.in_(user.teams_ids),\n _TABLE.c.team_id.in_(user.child_teams_ids)))\n\n # # If topic_id not None, then filter by topic_id\n if topic_id is not None:\n query.add_extra_condition(_TABLE.c.topic_id == topic_id)\n\n # # Get only the non archived jobs\n query.add_extra_condition(_TABLE.c.state != 'archived')\n\n nb_rows = query.get_number_of_rows()\n rows = query.execute(fetchall=True)\n rows = v1_utils.format_result(rows, _TABLE.name, args['embed'],\n _EMBED_MANY)\n\n return flask.jsonify({'jobs': rows, '_meta': {'count': nb_rows}})"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates a job by id", "response": "def update_job_by_id(user, job_id):\n \"\"\"Update a job\n \"\"\"\n # get If-Match header\n if_match_etag = utils.check_and_get_etag(flask.request.headers)\n\n # get the diverse parameters\n values = schemas.job.put(flask.request.json)\n\n job = v1_utils.verify_existence_and_get(job_id, _TABLE)\n job = dict(job)\n\n if not user.is_in_team(job['team_id']):\n raise dci_exc.Unauthorized()\n\n # Update jobstate if needed\n status = values.get('status')\n if status and job.get('status') != status:\n jobstates.insert_jobstate(user, {\n 'status': status,\n 'job_id': job_id\n })\n if status in models.FINAL_STATUSES:\n jobs_events.create_event(job_id, status, job['topic_id'])\n\n where_clause = sql.and_(_TABLE.c.etag == if_match_etag,\n _TABLE.c.id == job_id)\n\n values['etag'] = utils.gen_etag()\n query = _TABLE.update().returning(*_TABLE.columns).\\\n where(where_clause).values(**values)\n\n result = flask.g.db_conn.execute(query)\n if not result.rowcount:\n raise dci_exc.DCIConflict('Job', job_id)\n\n return flask.Response(\n json.dumps({'job': result.fetchone()}), 200,\n headers={'ETag': values['etag']},\n content_type='application/json'\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets all results from a job.", "response": "def get_all_results_from_jobs(user, j_id):\n \"\"\"Get all results from job.\n \"\"\"\n\n job = v1_utils.verify_existence_and_get(j_id, _TABLE)\n\n if not user.is_in_team(job['team_id']) and not user.is_read_only_user():\n raise dci_exc.Unauthorized()\n\n # get testscases from tests_results\n query = sql.select([models.TESTS_RESULTS]). \\\n where(models.TESTS_RESULTS.c.job_id == job['id'])\n all_tests_results = flask.g.db_conn.execute(query).fetchall()\n\n results = []\n for test_result in all_tests_results:\n test_result = dict(test_result)\n results.append({'filename': test_result['name'],\n 'name': test_result['name'],\n 'total': test_result['total'],\n 'failures': test_result['failures'],\n 'errors': test_result['errors'],\n 'skips': test_result['skips'],\n 'time': test_result['time'],\n 'regressions': test_result['regressions'],\n 'successfixes': test_result['successfixes'],\n 'success': test_result['success'],\n 'file_id': test_result['file_id']})\n\n return flask.jsonify({'results': results,\n '_meta': {'count': len(results)}})"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_tags_from_job(user, job_id):\n\n job = v1_utils.verify_existence_and_get(job_id, _TABLE)\n if not user.is_in_team(job['team_id']) and not user.is_read_only_user():\n raise dci_exc.Unauthorized()\n\n JTT = models.JOIN_JOBS_TAGS\n query = (sql.select([models.TAGS])\n .select_from(JTT.join(models.TAGS))\n .where(JTT.c.job_id == job_id))\n rows = flask.g.db_conn.execute(query)\n\n return flask.jsonify({'tags': rows, '_meta': {'count': rows.rowcount}})", "response": "Retrieve all tags attached to a job."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_tag_to_job(user, job_id):\n\n job = v1_utils.verify_existence_and_get(job_id, _TABLE)\n if not user.is_in_team(job['team_id']):\n raise dci_exc.Unauthorized()\n\n values = {\n 'job_id': job_id\n }\n\n job_tagged = tags.add_tag_to_resource(values, models.JOIN_JOBS_TAGS)\n\n return flask.Response(json.dumps(job_tagged), 201,\n content_type='application/json')", "response": "Add a tag to a job."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a tag from a job.", "response": "def delete_tag_from_job(user, job_id, tag_id):\n \"\"\"Delete a tag from a job.\"\"\"\n\n _JJT = models.JOIN_JOBS_TAGS\n job = v1_utils.verify_existence_and_get(job_id, _TABLE)\n if not user.is_in_team(job['team_id']):\n raise dci_exc.Unauthorized()\n v1_utils.verify_existence_and_get(tag_id, models.TAGS)\n\n query = _JJT.delete().where(sql.and_(_JJT.c.tag_id == tag_id,\n _JJT.c.job_id == job_id))\n\n try:\n flask.g.db_conn.execute(query)\n except sa_exc.IntegrityError:\n raise dci_exc.DCICreationConflict('tag', 'tag_id')\n\n return flask.Response(None, 204, content_type='application/json')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _is_node_an_element(self, node):\n # Try the simplest approach first, works for plain old ElementTree\n if isinstance(node, BaseET.Element):\n return True\n # For cElementTree we need to be more cunning (or find a better way)\n if hasattr(node, 'makeelement') and isinstance(node.tag, basestring):\n return True", "response": "Return True if the given node is an Element."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the result of performing the given XPath query on the given node.", "response": "def xpath_on_node(self, node, xpath, **kwargs):\n \"\"\"\n Return result of performing the given XPath query on the given node.\n\n All known namespace prefix-to-URI mappings in the document are\n automatically included in the XPath invocation.\n\n If an empty/default namespace (i.e. None) is defined, this is\n converted to the prefix name '_' so it can be used despite empty\n namespace prefixes being unsupported by XPath.\n \"\"\"\n namespaces_dict = {}\n if 'namespaces' in kwargs:\n namespaces_dict.update(kwargs['namespaces'])\n # Empty namespace prefix is not supported, convert to '_' prefix\n if None in namespaces_dict:\n default_ns_uri = namespaces_dict.pop(None)\n namespaces_dict['_'] = default_ns_uri\n # If no default namespace URI defined, use root's namespace (if any)\n if not '_' in namespaces_dict:\n root = self.get_impl_root(node)\n qname, ns_uri, prefix, local_name = self._unpack_name(\n root.tag, root)\n if ns_uri:\n namespaces_dict['_'] = ns_uri\n # Include XMLNS namespace if it's not already defined\n if not 'xmlns' in namespaces_dict:\n namespaces_dict['xmlns'] = nodes.Node.XMLNS_URI\n return node.findall(xpath, namespaces_dict)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the entries to be purged from the database.", "response": "def get_to_purge_archived_resources(user, table):\n \"\"\"List the entries to be purged from the database. \"\"\"\n\n if user.is_not_super_admin():\n raise dci_exc.Unauthorized()\n\n archived_resources = get_archived_resources(table)\n\n return flask.jsonify({table.name: archived_resources,\n '_meta': {'count': len(archived_resources)}})"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef purge_archived_resources(user, table):\n\n if user.is_not_super_admin():\n raise dci_exc.Unauthorized()\n\n where_clause = sql.and_(\n table.c.state == 'archived'\n )\n query = table.delete().where(where_clause)\n flask.g.db_conn.execute(query)\n\n return flask.Response(None, 204, content_type='application/json')", "response": "Remove the entries to be purged from the database."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef refresh_api_secret(user, resource, table):\n\n resource_name = table.name[0:-1]\n\n where_clause = sql.and_(\n table.c.etag == resource['etag'],\n table.c.id == resource['id'],\n )\n\n values = {\n 'api_secret': signature.gen_secret(),\n 'etag': utils.gen_etag()\n }\n\n query = table.update().where(where_clause).values(**values)\n result = flask.g.db_conn.execute(query)\n\n if not result.rowcount:\n raise dci_exc.DCIConflict(resource_name, resource['id'])\n\n res = flask.jsonify(({'id': resource['id'], 'etag': resource['etag'],\n 'api_secret': values['api_secret']}))\n res.headers.add_header('ETag', values['etag'])\n return res", "response": "Refresh the resource API Secret."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating a NPM package. json file.", "response": "def npm(package_json, output_file, pinned_file):\n \"\"\"Generate a package.json file.\"\"\"\n amd_build_deprecation_warning()\n try:\n version = get_distribution(current_app.name).version\n except DistributionNotFound:\n version = ''\n\n output = {\n 'name': current_app.name,\n 'version': make_semver(version) if version else version,\n 'dependencies': {},\n }\n\n # Load base file\n if package_json:\n output = dict(output, **json.load(package_json))\n\n # Iterate over bundles\n deps = extract_deps(current_app.extensions['invenio-assets'].env,\n click.echo)\n output['dependencies'].update(deps)\n\n # Load pinned dependencies\n if pinned_file:\n output['dependencies'].update(\n json.load(pinned_file).get('dependencies', {}))\n\n # Write to static folder if output file is not specified\n if output_file is None:\n if not os.path.exists(current_app.static_folder):\n os.makedirs(current_app.static_folder)\n output_file = open(\n os.path.join(current_app.static_folder, 'package.json'),\n 'w')\n\n click.echo('Writing {0}'.format(output_file.name))\n json.dump(output, output_file, indent=4)\n output_file.close()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef getAnalyses(self, **kwargs):\n\n # 'from' is a reserved word, use time_from instead\n if kwargs.get('time_from'):\n kwargs['from'] = kwargs.get('time_from')\n del kwargs['time_from']\n if kwargs.get('time_to'):\n kwargs['to'] = kwargs.get('time_to')\n del kwargs['time_to']\n\n # Warn user about unhandled kwargs\n for key in kwargs:\n if key not in ['limit', 'offset', 'from', 'to']:\n sys.stderr.write('%s not a valid argument for analysis()\\n'\n % key)\n\n response = self.pingdom.request('GET', 'analysis/%s' % self.id,\n kwargs)\n\n return [PingdomAnalysis(self, x) for x in response.json()['analysis']]", "response": "Returns a list of the latest root cause analysis results for the specified check."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef getDetails(self):\n\n response = self.pingdom.request('GET', 'checks/%s' % self.id)\n self.__addDetails__(response.json()['check'])\n return response.json()['check']", "response": "Update check details returns dictionary of details"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef modify(self, **kwargs):\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['paused', 'resolution', 'contactids', 'sendtoemail',\n 'sendtosms', 'sendtotwitter', 'sendtoiphone',\n 'sendnotificationwhendown', 'notifyagainevery',\n 'notifywhenbackup', 'created', 'type', 'hostname',\n 'status', 'lasterrortime', 'lasttesttime', 'url',\n 'encryption', 'port', 'auth', 'shouldcontain',\n 'shouldnotcontain', 'postdata', 'additionalurls',\n 'stringtosend', 'stringtoexpect', 'expectedip',\n 'nameserver', 'use_legacy_notifications', 'host',\n 'alert_policy', 'autoresolve', 'probe_filters']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument of' +\n '.modify()\\n')\n\n # If one of the legacy parameters is used, it is required to set the legacy flag.\n # https://github.com/KennethWilke/PingdomLib/issues/12\n if any([k for k in kwargs if k in legacy_notification_parameters]):\n if \"use_legacy_notifications\" in kwargs and kwargs[\"use_legacy_notifications\"] != True:\n raise Exception(\"Cannot set legacy parameter when use_legacy_notifications is not True\")\n kwargs[\"use_legacy_notifications\"] = True\n\n response = self.pingdom.request(\"PUT\", 'checks/%s' % self.id, kwargs)\n\n return response.json()['message']", "response": "Modify settings for a specific check in the specified base class."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef averages(self, **kwargs):\n\n # 'from' is a reserved word, use time_from instead\n if kwargs.get('time_from'):\n kwargs['from'] = kwargs.get('time_from')\n del kwargs['time_from']\n if kwargs.get('time_to'):\n kwargs['to'] = kwargs.get('time_to')\n del kwargs['time_to']\n\n # Warn user about unhandled parameters\n for key in kwargs:\n if key not in ['from', 'to', 'probes', 'includeuptime',\n 'bycountry', 'byprobe']:\n sys.stderr.write(\"'%s'\" % key + ' is not a valid argument of' +\n ' 1:\n log('Warn: {0} version {1} resolved to: {2}'.format(\n repr(package), versions, repr(deps[package])\n ))\n\n return deps"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmaking a semantic version from a Python PEP440 version string.", "response": "def make_semver(version_str):\n \"\"\"Make a semantic version from Python PEP440 version.\n\n Semantic versions does not handle post-releases.\n \"\"\"\n v = parse_version(version_str)\n\n major = v._version.release[0]\n try:\n minor = v._version.release[1]\n except IndexError:\n minor = 0\n try:\n patch = v._version.release[2]\n except IndexError:\n patch = 0\n\n prerelease = []\n if v._version.pre:\n prerelease.append(''.join(str(x) for x in v._version.pre))\n if v._version.dev:\n prerelease.append(''.join(str(x) for x in v._version.dev))\n prerelease = '.'.join(prerelease)\n\n # Create semver\n version = '{0}.{1}.{2}'.format(major, minor, patch)\n\n if prerelease:\n version += '-{0}'.format(prerelease)\n if v.local:\n version += '+{0}'.format(v.local)\n\n return version"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalculating the maximum size for a question pool.", "response": "def get_max_size(pool, num_option, item_length):\n \"\"\"\n Calculate the max number of item that an option can stored in the pool at give time.\n\n This is to limit the pool size to POOL_SIZE\n\n Args:\n option_index (int): the index of the option to calculate the size for\n pool (dict): answer pool\n num_option (int): total number of options available for the question\n item_length (int): the length of the item\n\n Returns:\n int: the max number of items that `option_index` can have\n \"\"\"\n max_items = POOL_SIZE / item_length\n # existing items plus the reserved for min size. If there is an option has 1 item, POOL_OPTION_MIN_SIZE - 1 space\n # is reserved.\n existing = POOL_OPTION_MIN_SIZE * num_option + sum([max(0, len(pool.get(i, {})) - 5) for i in xrange(num_option)])\n return int(max_items - existing)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsubmit an answer to the answer pool", "response": "def offer_answer(pool, answer, rationale, student_id, algo, options):\n \"\"\"\n submit a student answer to the answer pool\n\n The answer maybe selected to stay in the pool depending on the selection algorithm\n\n Args:\n pool (dict): answer pool\n Answer pool format:\n {\n option1_index: {\n 'student_id': { can store algorithm specific info here },\n ...\n }\n option2_index: ...\n }\n answer (int): the option student selected\n rationale (str): the rationale text\n student_id (str): student identifier\n algo (str): the selection algorithm\n options (dict): the options available in the question\n\n Raises:\n UnknownChooseAnswerAlgorithm: when we don't know the algorithm\n \"\"\"\n if algo['name'] == 'simple':\n offer_simple(pool, answer, rationale, student_id, options)\n elif algo['name'] == 'random':\n offer_random(pool, answer, rationale, student_id, options)\n else:\n raise UnknownChooseAnswerAlgorithm()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef offer_simple(pool, answer, rationale, student_id, options):\n existing = pool.setdefault(answer, {})\n if len(existing) >= get_max_size(pool, len(options), POOL_ITEM_LENGTH_SIMPLE):\n student_id_to_remove = random.choice(existing.keys())\n del existing[student_id_to_remove]\n existing[student_id] = {}\n pool[answer] = existing", "response": "This algorithm selects an answer from the pool and adds it to the pool if it reaches the limit."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nvalidate answers based on selection algorithm", "response": "def validate_seeded_answers(answers, options, algo):\n \"\"\"\n Validate answers based on selection algorithm\n\n This is called when instructor setup the tool and providing seeded answers to the question.\n This function is trying to validate if instructor provided enough seeds for a give algorithm.\n e.g. we require 1 seed for each option in simple algorithm and at least 1 seed for random\n algorithm. Because otherwise, the first student won't be able to see the answers on the\n second step where he/she suppose to compare and review other students answers.\n\n Args:\n answers (list): list of dict that contain seeded answers\n options (dict): all options that should exist in the answers\n algo (str): selection algorithm\n\n Returns:\n None if successful, otherwise error message\n \"\"\"\n if algo['name'] == 'simple':\n return validate_seeded_answers_simple(answers, options, algo)\n elif algo['name'] == 'random':\n return validate_seeded_answers_random(answers)\n else:\n raise UnknownChooseAnswerAlgorithm()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_other_answers(pool, seeded_answers, get_student_item_dict, algo, options):\n # \"#\" means the number of responses returned should be the same as the number of options.\n num_responses = len(options) \\\n if 'num_responses' not in algo or algo['num_responses'] == \"#\" \\\n else int(algo['num_responses'])\n\n if algo['name'] == 'simple':\n return get_other_answers_simple(pool, seeded_answers, get_student_item_dict, num_responses)\n elif algo['name'] == 'random':\n return get_other_answers_random(pool, seeded_answers, get_student_item_dict, num_responses)\n else:\n raise UnknownChooseAnswerAlgorithm()", "response": "Select other student s answers from answer pool or seeded answers based on the selection algorithm"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget answers from others with simple algorithm which picks one answer for each option.", "response": "def get_other_answers_simple(pool, seeded_answers, get_student_item_dict, num_responses):\n \"\"\"\n Get answers from others with simple algorithm, which picks one answer for each option.\n\n Args:\n see `get_other_answers`\n num_responses (int): the number of responses to be returned. This value may not be\n respected if there is not enough answers to return\n\n Returns:\n dict: answers based on the selection algorithm\n \"\"\"\n ret = []\n # clean up answers so that all keys are int\n pool = {int(k): v for k, v in pool.items()}\n total_in_pool = len(seeded_answers)\n merged_pool = convert_seeded_answers(seeded_answers)\n student_id = get_student_item_dict()['student_id']\n # merge the dictionaries in the answer dictionary\n for key in pool:\n total_in_pool += len(pool[key])\n # if student_id has value, we assume the student just submitted an answer. So removing it\n # from total number in the pool\n if student_id in pool[key].keys():\n total_in_pool -= 1\n if key in merged_pool:\n merged_pool[key].update(pool[key].items())\n else:\n merged_pool[key] = pool[key]\n\n # remember which option+student_id is selected, so that we don't have duplicates in the result\n selected = []\n\n # loop until we have enough answers to return\n while len(ret) < min(num_responses, total_in_pool):\n for option, students in merged_pool.items():\n student = student_id\n i = 0\n while (student == student_id or i > 100) and (str(option) + student) not in selected:\n # retry until we got a different one or after 100 retries\n # we are suppose to get a different student answer or a seeded one in a few tries\n # as we have at least one seeded answer for each option in the algo. And it is not\n # suppose to overflow i order to break the loop\n student = random.choice(students.keys())\n i += 1\n selected.append(str(option)+student)\n if student.startswith('seeded'):\n # seeded answer, get the rationale from local\n rationale = students[student]\n else:\n student_item = get_student_item_dict(student)\n submission = sas_api.get_answers_for_student(student_item)\n rationale = submission.get_rationale(0)\n ret.append({'option': option, 'rationale': rationale})\n\n # check if we have enough answers\n if len(ret) >= min(num_responses, total_in_pool):\n break\n\n return {\"answers\": ret}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget answers from others with random algorithm which randomly select answer from the pool.", "response": "def get_other_answers_random(pool, seeded_answers, get_student_item_dict, num_responses):\n \"\"\"\n Get answers from others with random algorithm, which randomly select answer from the pool.\n\n Student may get three answers for option 1 or one answer for option 1 and two answers for option 2.\n\n Args:\n see `get_other_answers`\n num_responses (int): the number of responses to be returned. This value may not be\n respected if there is not enough answers to return\n\n Returns:\n dict: answers based on the selection algorithm\n \"\"\"\n ret = []\n # clean up answers so that all keys are int\n pool = {int(k): v for k, v in pool.items()}\n seeded = {'seeded'+str(index): answer for index, answer in enumerate(seeded_answers)}\n merged_pool = seeded.keys()\n\n for key in pool:\n merged_pool += pool[key].keys()\n\n # shuffle\n random.shuffle(merged_pool)\n # get student identifier\n student_id = get_student_item_dict()['student_id']\n\n for student in merged_pool:\n if len(ret) >= num_responses:\n # have enough answers\n break\n elif student == student_id:\n # this is the student's answer so don't return\n continue\n\n if student.startswith('seeded'):\n option = seeded[student]['answer']\n rationale = seeded[student]['rationale']\n else:\n student_item = get_student_item_dict(student)\n submission = sas_api.get_answers_for_student(student_item)\n rationale = submission.get_rationale(0)\n option = submission.get_vote(0)\n ret.append({'option': option, 'rationale': rationale})\n\n return {\"answers\": ret}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef convert_seeded_answers(answers):\n converted = {}\n for index, answer in enumerate(answers):\n converted.setdefault(answer['answer'], {})\n converted[answer['answer']]['seeded' + str(index)] = answer['rationale']\n\n return converted", "response": "Convert seeded answers into the format that can be merged into student answers."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupgrading a batch processor from version 1 to version 2.", "response": "def upgradeProcessor1to2(oldProcessor):\n \"\"\"\n Batch processors stopped polling at version 2, so they no longer needed the\n idleInterval attribute. They also gained a scheduled attribute which\n tracks their interaction with the scheduler. Since they stopped polling,\n we also set them up as a timed event here to make sure that they don't\n silently disappear, never to be seen again: running them with the scheduler\n gives them a chance to figure out what's up and set up whatever other state\n they need to continue to run.\n\n Since this introduces a new dependency of all batch processors on a powerup\n for the IScheduler, install a Scheduler or a SubScheduler if one is not\n already present.\n \"\"\"\n newProcessor = oldProcessor.upgradeVersion(\n oldProcessor.typeName, 1, 2,\n busyInterval=oldProcessor.busyInterval)\n newProcessor.scheduled = extime.Time()\n\n s = newProcessor.store\n sch = iaxiom.IScheduler(s, None)\n if sch is None:\n if s.parent is None:\n # Only site stores have no parents.\n sch = Scheduler(store=s)\n else:\n # Substores get subschedulers.\n sch = SubScheduler(store=s)\n installOn(sch, s)\n\n # And set it up to run.\n sch.schedule(newProcessor, newProcessor.scheduled)\n return newProcessor"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef processor(forType):\n MILLI = 1000\n try:\n processor = _processors[forType]\n except KeyError:\n def __init__(self, *a, **kw):\n item.Item.__init__(self, *a, **kw)\n self.store.powerUp(self, iaxiom.IBatchProcessor)\n\n attrs = {\n '__name__': 'Batch_' + forType.__name__,\n\n '__module__': forType.__module__,\n\n '__init__': __init__,\n\n '__repr__': lambda self: '' % (reflect.qual(self.workUnitType), self.storeID),\n\n 'schemaVersion': 2,\n\n 'workUnitType': forType,\n\n 'scheduled': attributes.timestamp(doc=\"\"\"\n The next time at which this processor is scheduled to run.\n \"\"\", default=None),\n\n # MAGIC NUMBERS AREN'T THEY WONDERFUL?\n 'busyInterval': attributes.integer(doc=\"\", default=MILLI / 10),\n }\n _processors[forType] = processor = item.MetaItem(\n attrs['__name__'],\n (item.Item, _BatchProcessorMixin),\n attrs)\n\n registerUpgrader(\n upgradeProcessor1to2,\n _processors[forType].typeName,\n 1, 2)\n\n return processor", "response": "Create an Axiom Item type suitable for use as a batch processor for the given type."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef storeBatchServiceSpecialCase(st, pups):\n if st.parent is not None:\n try:\n return _SubStoreBatchChannel(st)\n except TypeError:\n return None\n storeService = service.IService(st)\n try:\n return storeService.getServiceNamed(\"Batch Processing Controller\")\n except KeyError:\n return None", "response": "This function returns a simple wrapper that delegates to the site\n store s BatchProcessingControllerService."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmark the unit of work as failed in the database and update the listener so that it is not skipped.", "response": "def mark(self):\n \"\"\"\n Mark the unit of work as failed in the database and update the listener\n so as to skip it next time.\n \"\"\"\n self.reliableListener.lastRun = extime.Time()\n BatchProcessingError(\n store=self.reliableListener.store,\n processor=self.reliableListener.processor,\n listener=self.reliableListener.listener,\n item=self.workUnit,\n error=self.failure.getErrorMessage())"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run(self):\n now = extime.Time()\n if self.step():\n self.scheduled = now + datetime.timedelta(milliseconds=self.busyInterval)\n else:\n self.scheduled = None\n return self.scheduled", "response": "Try to run one unit of work through one listener. If there are more work to run then unschedule this item. If there are no more work then run one unit of work through one listener."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef addReliableListener(self, listener, style=iaxiom.LOCAL):\n existing = self.store.findUnique(_ReliableListener,\n attributes.AND(_ReliableListener.processor == self,\n _ReliableListener.listener == listener),\n default=None)\n if existing is not None:\n return existing\n\n for work in self.store.query(self.workUnitType,\n sort=self.workUnitType.storeID.descending,\n limit=1):\n forwardMark = work.storeID\n backwardMark = work.storeID + 1\n break\n else:\n forwardMark = 0\n backwardMark = 0\n\n if self.scheduled is None:\n self.scheduled = extime.Time()\n iaxiom.IScheduler(self.store).schedule(self, self.scheduled)\n\n return _ReliableListener(store=self.store,\n processor=self,\n listener=listener,\n forwardMark=forwardMark,\n backwardMark=backwardMark,\n style=style)", "response": "Add the given Item to the set which will be notified of Items\n available for processing."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef removeReliableListener(self, listener):\n self.store.query(_ReliableListener,\n attributes.AND(_ReliableListener.processor == self,\n _ReliableListener.listener == listener)).deleteFromStore()\n self.store.query(BatchProcessingError,\n attributes.AND(BatchProcessingError.processor == self,\n BatchProcessingError.listener == listener)).deleteFromStore()", "response": "Removes a previously added listener."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef getReliableListeners(self):\n for rellist in self.store.query(_ReliableListener, _ReliableListener.processor == self):\n yield rellist.listener", "response": "Returns an iterable of the listeners which have been added to\n this batch processor."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getFailedItems(self):\n for failed in self.store.query(BatchProcessingError, BatchProcessingError.processor == self):\n yield (failed.listener, failed.item)", "response": "Returns an iterable of two - tuples of listeners which raised an an\n exception from C { processItem } and the item which was passed as\n ."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef itemAdded(self):\n localCount = self.store.query(\n _ReliableListener,\n attributes.AND(_ReliableListener.processor == self,\n _ReliableListener.style == iaxiom.LOCAL),\n limit=1).count()\n\n remoteCount = self.store.query(\n _ReliableListener,\n attributes.AND(_ReliableListener.processor == self,\n _ReliableListener.style == iaxiom.REMOTE),\n limit=1).count()\n\n if localCount and self.scheduled is None:\n self.scheduled = extime.Time()\n iaxiom.IScheduler(self.store).schedule(self, self.scheduled)\n if remoteCount:\n batchService = iaxiom.IBatchService(self.store, None)\n if batchService is not None:\n batchService.start()", "response": "Called to indicate that a new item of the type monitored by this batch\n processor is being added to the database."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef call(self, itemMethod):\n item = itemMethod.im_self\n method = itemMethod.im_func.func_name\n return self.batchController.getProcess().addCallback(\n CallItemMethod(storepath=item.store.dbdir,\n storeid=item.storeID,\n method=method).do)", "response": "Invoke the given bound item method in the batch process."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrun tasks until stopService is called.", "response": "def processWhileRunning(self):\n \"\"\"\n Run tasks until stopService is called.\n \"\"\"\n work = self.step()\n for result, more in work:\n yield result\n if not self.running:\n break\n if more:\n delay = 0.1\n else:\n delay = 10.0\n yield task.deferLater(reactor, delay, lambda: None)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind every column in every sheet and put it in a new sheet or book.", "response": "def getcols(sheetMatch=None,colMatch=\"Decay\"):\n \"\"\"find every column in every sheet and put it in a new sheet or book.\"\"\"\n book=BOOK()\n if sheetMatch is None:\n matchingSheets=book.sheetNames\n print('all %d sheets selected '%(len(matchingSheets)))\n else:\n matchingSheets=[x for x in book.sheetNames if sheetMatch in x]\n print('%d of %d sheets selected matching \"%s\"'%(len(matchingSheets),len(book.sheetNames),sheetMatch))\n matchingSheetsWithCol=[]\n for sheetName in matchingSheets:\n i = book.sheetNames.index(sheetName) # index of that sheet\n for j,colName in enumerate(book.sheets[i].colDesc):\n if colMatch in colName:\n matchingSheetsWithCol.append((sheetName,j))\n break\n else:\n print(\" no match in [%s]%s\"%(book.bookName,sheetName))\n print(\"%d of %d of those have your column\"%(len(matchingSheetsWithCol),len(matchingSheets)))\n for item in matchingSheetsWithCol:\n print(item,item[0],item[1])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef upgradeStore(self, store):\n self.upgradeEverything(store)\n upgradeExplicitOid(store)\n\n for substore in store.query(SubStore):\n print 'Upgrading: {!r}'.format(substore)\n self.upgradeStore(substore.open())", "response": "Upgrade all the objects in the given store."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef perform(self, store, count):\n self.count = count\n\n try:\n self.upgradeStore(store)\n print 'Upgrade complete'\n except errors.ItemUpgradeError as e:\n print 'Upgrader error:'\n e.originalFailure.printTraceback(file=sys.stdout)\n print self.errorMessageFormat % (\n e.oldType.typeName, e.storeID, e.oldType.schemaVersion,\n e.newType.schemaVersion)", "response": "Perform the upgrades on the given store and count."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef runcode(self, code):\n if not self.locals.get('autocommit', None):\n return self.locals['db'].transact(code.InteractiveConsole.runcode, self, code)\n return code.InteractiveConsole.runcode(self, code)", "response": "Override the local L{InteractiveConsole. runcode to run the code in a specific transaction."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dictionary representing the namespace which should be used to access the user s database.", "response": "def namespace(self):\n \"\"\"\n Return a dictionary representing the namespace which should be\n available to the user.\n \"\"\"\n self._ns = {\n 'db': self.store,\n 'store': store,\n 'autocommit': False,\n }\n return self._ns"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds a new account to the given store.", "response": "def addAccount(self, siteStore, username, domain, password):\n \"\"\"\n Create a new account in the given store.\n\n @param siteStore: A site Store to which login credentials will be\n added.\n @param username: Local part of the username for the credentials to add.\n @param domain: Domain part of the username for the credentials to add.\n @param password: Password for the credentials to add.\n @rtype: L{LoginAccount}\n @return: The added account.\n \"\"\"\n for ls in siteStore.query(userbase.LoginSystem):\n break\n else:\n ls = self.installOn(siteStore)\n try:\n acc = ls.addAccount(username, domain, password)\n except userbase.DuplicateUser:\n raise usage.UsageError(\"An account by that name already exists.\")\n return acc"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new class with some attributes in its schema.", "response": "def itemTypeWithSomeAttributes(attributeTypes):\n \"\"\"\n Create a new L{Item} subclass with L{numAttributes} integers in its\n schema.\n \"\"\"\n class SomeItem(Item):\n typeName = 'someitem_' + str(typeNameCounter())\n for i, attributeType in enumerate(attributeTypes):\n locals()['attr_' + str(i)] = attributeType()\n return SomeItem"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates some items of a particular type in a store.", "response": "def createSomeItems(store, itemType, values, counter):\n \"\"\"\n Create some instances of a particular type in a store.\n \"\"\"\n for i in counter:\n itemType(store=store, **values)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef save(self, commit=True):\n\n # walk through the document fields\n for field_name, field in iter_valid_fields(self._meta):\n setattr(self.instance, field_name, self.cleaned_data.get(field_name))\n\n if commit:\n self.instance.save()\n\n return self.instance", "response": "save the instance or create a new one"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a callable which will invoke the given function in a transaction using the underlying store attribute of the first parameter passed to it.", "response": "def transacted(func):\n \"\"\"\n Return a callable which will invoke C{func} in a transaction using the\n C{store} attribute of the first parameter passed to it. Typically this is\n used to create Item methods which are automatically run in a transaction.\n\n The attributes of the returned callable will resemble those of C{func} as\n closely as L{twisted.python.util.mergeFunctionMetadata} can make them.\n \"\"\"\n def transactionified(item, *a, **kw):\n return item.store.transact(func, item, *a, **kw)\n return mergeFunctionMetadata(func, transactionified)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef dependentItems(store, tableClass, comparisonFactory):\n for cascadingAttr in (_cascadingDeletes.get(tableClass, []) +\n _cascadingDeletes.get(None, [])):\n for cascadedItem in store.query(cascadingAttr.type,\n comparisonFactory(cascadingAttr)):\n yield cascadedItem", "response": "Returns an iterable of items that should be deleted when an item or items\n of a particular item type are deleted."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef allowDeletion(store, tableClass, comparisonFactory):\n for cascadingAttr in (_disallows.get(tableClass, []) +\n _disallows.get(None, [])):\n for cascadedItem in store.query(cascadingAttr.type,\n comparisonFactory(cascadingAttr),\n limit=1):\n return False\n\n return True", "response": "Returns a C { bool } indicating whether deletion of an item or items of a specific item type should be allowed to proceed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef declareLegacyItem(typeName, schemaVersion, attributes, dummyBases=()):\n if (typeName, schemaVersion) in _legacyTypes:\n return _legacyTypes[typeName, schemaVersion]\n if dummyBases:\n realBases = [declareLegacyItem(*A) for A in dummyBases]\n else:\n realBases = (Item,)\n attributes = attributes.copy()\n attributes['__module__'] = 'item_dummy'\n attributes['__legacy__'] = True\n attributes['typeName'] = typeName\n attributes['schemaVersion'] = schemaVersion\n result = type(str('DummyItem<%s,%d>' % (typeName, schemaVersion)),\n realBases,\n attributes)\n assert result is not None, 'wtf, %r' % (type,)\n _legacyTypes[(typeName, schemaVersion)] = result\n return result", "response": "Return a dummy subclass of Item that will have the given attributes and baseItem methods."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nclass decorator for indicating a powerup s powerup interfaces.", "response": "def empowerment(iface, priority=0):\n \"\"\"\n Class decorator for indicating a powerup's powerup interfaces.\n\n The class will also be declared as implementing the interface.\n\n @type iface: L{zope.interface.Interface}\n @param iface: The powerup interface.\n\n @type priority: int\n @param priority: The priority the powerup will be installed at.\n \"\"\"\n def _deco(cls):\n cls.powerupInterfaces = (\n tuple(getattr(cls, 'powerupInterfaces', ())) +\n ((iface, priority),))\n implementer(iface)(cls)\n return cls\n return _deco"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef powerUp(self, powerup, interface=None, priority=0):\n if interface is None:\n for iface, priority in powerup._getPowerupInterfaces():\n self.powerUp(powerup, iface, priority)\n\n elif interface is IPowerupIndirector:\n raise TypeError(\n \"You cannot install a powerup for IPowerupIndirector: \" +\n powerup)\n else:\n forc = self.store.findOrCreate(_PowerupConnector,\n item=self,\n interface=unicode(qual(interface)),\n powerup=powerup)\n forc.priority = priority", "response": "Powerup the specified powerup on the item or store."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef powerDown(self, powerup, interface=None):\n if interface is None:\n for interface, priority in powerup._getPowerupInterfaces():\n self.powerDown(powerup, interface)\n else:\n for cable in self.store.query(_PowerupConnector,\n AND(_PowerupConnector.item == self,\n _PowerupConnector.interface == unicode(qual(interface)),\n _PowerupConnector.powerup == powerup)):\n cable.deleteFromStore()\n return\n raise ValueError(\"Not powered up for %r with %r\" % (interface,\n powerup))", "response": "Power down a powerup."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef powerupsFor(self, interface):\n inMemoryPowerup = self._inMemoryPowerups.get(interface, None)\n if inMemoryPowerup is not None:\n yield inMemoryPowerup\n if self.store is None:\n return\n name = unicode(qual(interface), 'ascii')\n for cable in self.store.query(\n _PowerupConnector,\n AND(_PowerupConnector.interface == name,\n _PowerupConnector.item == self),\n sort=_PowerupConnector.priority.descending):\n pup = cable.powerup\n if pup is None:\n # this powerup was probably deleted during an upgrader.\n cable.deleteFromStore()\n else:\n indirector = IPowerupIndirector(pup, None)\n if indirector is not None:\n yield indirector.indirect(interface)\n else:\n yield pup", "response": "Yields the powerups installed for this item."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns an iterator of the interfaces that are installed on this object and which the given powerup is xid installed on this object.", "response": "def interfacesFor(self, powerup):\n \"\"\"\n Return an iterator of the interfaces for which the given powerup is\n installed on this object.\n\n This is not implemented for in-memory powerups. It will probably fail\n in an unpredictable, implementation-dependent way if used on one.\n \"\"\"\n pc = _PowerupConnector\n for iface in self.store.query(pc,\n AND(pc.item == self,\n pc.powerup == powerup)).getColumn('interface'):\n yield namedAny(iface)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _getPowerupInterfaces(self):\n powerupInterfaces = getattr(self.__class__, \"powerupInterfaces\", ())\n pifs = []\n for x in powerupInterfaces:\n if isinstance(x, type(Interface)):\n #just an interface\n pifs.append((x, 0))\n else:\n #an interface and a priority\n pifs.append(x)\n\n m = getattr(self, \"__getPowerupInterfaces__\", None)\n if m is not None:\n pifs = m(pifs)\n try:\n pifs = [(i, p) for (i, p) in pifs]\n except ValueError:\n raise ValueError(\"return value from %r.__getPowerupInterfaces__\"\n \" not an iterable of 2-tuples\" % (self,))\n return pifs", "response": "Return a list of all powerup interfaces that this object declares that it can be installed on."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbeing this object currently valid as a reference?", "response": "def _currentlyValidAsReferentFor(self, store):\n \"\"\"\n Is this object currently valid as a reference? Objects which will be\n deleted in this transaction, or objects which are not in the same store\n are not valid. See attributes.reference.__get__.\n \"\"\"\n if store is None:\n # If your store is None, you can refer to whoever you want. I'm in\n # a store but it doesn't matter that you're not.\n return True\n if self.store is not store:\n return False\n if self.__deletingObject:\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _schemaPrepareInsert(self, store):\n for name, atr in self.getSchema():\n atr.prepareInsert(self, store)", "response": "Prepare each attribute in my schema for insertion into a given store."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating and return a new instance from a row from the store.", "response": "def existingInStore(cls, store, storeID, attrs):\n \"\"\"Create and return a new instance from a row from the store.\"\"\"\n self = cls.__new__(cls)\n\n self.__justCreated = False\n self.__subinit__(__store=store,\n storeID=storeID,\n __everInserted=True)\n\n schema = self.getSchema()\n assert len(schema) == len(attrs), \"invalid number of attributes\"\n for data, (name, attr) in zip(attrs, schema):\n attr.loaded(self, data)\n self.activate()\n return self"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef getSchema(cls):\n schema = []\n for name, atr in cls.__attributes__:\n atr = atr.__get__(None, cls)\n if isinstance(atr, SQLAttribute):\n schema.append((name, atr))\n cls.getSchema = staticmethod(lambda schema=schema: schema)\n return schema", "response": "getSchema - Returns a list of all persistent class attributes"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef persistentValues(self):\n return dict((k, getattr(self, k)) for (k, attr) in self.getSchema())", "response": "Return a dictionary of all persistent values of the current object."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalls after the database is committed with this object.", "response": "def committed(self):\n \"\"\"\n Called after the database is brought into a consistent state with this\n object.\n \"\"\"\n if self.__deleting:\n self.deleted()\n if not self.__legacy__:\n self.store.objectCache.uncache(self.storeID, self)\n self.__store = None\n self.__justCreated = False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the database to reflect in-memory changes made to this item; for example, to make it show up in store.query() calls where it is now valid, but was not the last time it was persisted to the database. This is called automatically when in 'autocommit mode' (i.e. not in a transaction) and at the end of each transaction for every object that has been changed.", "response": "def checkpoint(self):\n \"\"\"\n Update the database to reflect in-memory changes made to this item; for\n example, to make it show up in store.query() calls where it is now\n valid, but was not the last time it was persisted to the database.\n\n This is called automatically when in 'autocommit mode' (i.e. not in a\n transaction) and at the end of each transaction for every object that\n has been changed.\n \"\"\"\n\n if self.store is None:\n raise NotInStore(\"You can't checkpoint %r: not in a store\" % (self,))\n\n\n if self.__deleting:\n if not self.__everInserted:\n # don't issue duplicate SQL and crap; we were created, then\n # destroyed immediately.\n return\n self.store.executeSQL(self._baseDeleteSQL(self.store), [self.storeID])\n # re-using OIDs plays havoc with the cache, and with other things\n # as well. We need to make sure that we leave a placeholder row at\n # the end of the table.\n if self.__deletingObject:\n # Mark this object as dead.\n self.store.executeSchemaSQL(_schema.CHANGE_TYPE,\n [-1, self.storeID])\n\n # Can't do this any more:\n # self.store.executeSchemaSQL(_schema.DELETE_OBJECT, [self.storeID])\n\n # TODO: need to measure the performance impact of this, then do\n # it to make sure things are in fact deleted:\n # self.store.executeSchemaSQL(_schema.APP_VACUUM)\n\n else:\n assert self.__legacy__\n\n # we're done...\n if self.store.autocommit:\n self.committed()\n return\n\n if self.__everInserted:\n # case 1: we've been inserted before, either previously in this\n # transaction or we were loaded from the db\n if not self.__dirty__:\n # we might have been checkpointed twice within the same\n # transaction; just don't do anything.\n return\n self.store.executeSQL(*self._updateSQL())\n else:\n # case 2: we are in the middle of creating the object, we've never\n # been inserted into the db before\n schemaAttrs = self.getSchema()\n\n insertArgs = [self.storeID]\n for (ignoredName, attrObj) in schemaAttrs:\n attrObjDuplicate, attributeValue = self.__dirty__[attrObj.attrname]\n # assert attrObjDuplicate is attrObj\n insertArgs.append(attributeValue)\n\n # XXX this isn't atomic, gross.\n self.store.executeSQL(self._baseInsertSQL(self.store), insertArgs)\n self.__everInserted = True\n # In case 1, we're dirty but we did an update, synchronizing the\n # database, in case 2, we haven't been created but we issue an insert.\n # In either case, the code in attributes.py sets the attribute *as well\n # as* populating __dirty__, so we clear out dirty and we keep the same\n # value, knowing it's the same as what's in the db.\n self.__dirty__.clear()\n if self.store.autocommit:\n self.committed()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef registerUpgrader(upgrader, typeName, oldVersion, newVersion):\n # assert (typeName, oldVersion, newVersion) not in _upgradeRegistry, \"duplicate upgrader\"\n # ^ this makes the tests blow up so it's just disabled for now; perhaps we\n # should have a specific test mode\n # assert newVersion - oldVersion == 1, \"read the doc string\"\n assert isinstance(typeName, str), \"read the doc string\"\n _upgradeRegistry[typeName, oldVersion] = upgrader", "response": "Register a function which can perform a schema upgrade between two sequence words."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef registerAttributeCopyingUpgrader(itemType, fromVersion, toVersion, postCopy=None):\n def upgrader(old):\n newitem = old.upgradeVersion(itemType.typeName, fromVersion, toVersion,\n **dict((str(name), getattr(old, name))\n for (name, _) in old.getSchema()))\n if postCopy is not None:\n postCopy(newitem)\n return newitem\n registerUpgrader(upgrader, itemType.typeName, fromVersion, toVersion)", "response": "Register an upgrader for itemType from fromVersion to toVersion which will copy all attributes from the legacy item to the new item."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nregisters an upgrader for an item from fromVersion to toVersion which will delete the item from the database.", "response": "def registerDeletionUpgrader(itemType, fromVersion, toVersion):\n \"\"\"\n Register an upgrader for C{itemType}, from C{fromVersion} to C{toVersion},\n which will delete the item from the database.\n\n @param itemType: L{axiom.item.Item} subclass\n @return: None\n \"\"\"\n # XXX This should actually do something more special so that a new table is\n # not created and such.\n def upgrader(old):\n old.deleteFromStore()\n return None\n registerUpgrader(upgrader, itemType.typeName, fromVersion, toVersion)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if the given table has an explicit oid column.", "response": "def _hasExplicitOid(store, table):\n \"\"\"\n Does the given table have an explicit oid column?\n \"\"\"\n return any(info[1] == 'oid' for info\n in store.querySchemaSQL(\n 'PRAGMA *DATABASE*.table_info({})'.format(table)))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupgrading a table to have an explicit oid.", "response": "def _upgradeTableOid(store, table, createTable, postCreate=lambda: None):\n \"\"\"\n Upgrade a table to have an explicit oid.\n\n Must be called in a transaction to avoid corrupting the database.\n \"\"\"\n if _hasExplicitOid(store, table):\n return\n store.executeSchemaSQL(\n 'ALTER TABLE *DATABASE*.{0} RENAME TO {0}_temp'.format(table))\n createTable()\n store.executeSchemaSQL(\n 'INSERT INTO *DATABASE*.{0} '\n 'SELECT oid, * FROM *DATABASE*.{0}_temp'.format(table))\n store.executeSchemaSQL('DROP TABLE *DATABASE*.{0}_temp'.format(table))\n postCreate()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupgrade the system tables to use explicit oid columns.", "response": "def upgradeSystemOid(store):\n \"\"\"\n Upgrade the system tables to use explicit oid columns.\n \"\"\"\n store.transact(\n _upgradeTableOid, store, 'axiom_types',\n lambda: store.executeSchemaSQL(CREATE_TYPES))\n store.transact(\n _upgradeTableOid, store, 'axiom_objects',\n lambda: store.executeSchemaSQL(CREATE_OBJECTS),\n lambda: store.executeSchemaSQL(CREATE_OBJECTS_IDX))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef upgradeExplicitOid(store):\n upgradeSystemOid(store)\n for typename, version in store.querySchemaSQL(LATEST_TYPES):\n cls = _typeNameToMostRecentClass[typename]\n if cls.schemaVersion != version:\n remaining = store.querySQL(\n 'SELECT oid FROM {} LIMIT 1'.format(\n store._tableNameFor(typename, version)))\n if len(remaining) == 0:\n # Nothing to upgrade\n continue\n else:\n raise RuntimeError(\n '{}:{} not fully upgraded to {}'.format(\n typename, version, cls.schemaVersion))\n store.transact(\n _upgradeTableOid,\n store,\n store._tableNameOnlyFor(typename, version),\n lambda: store._justCreateTable(cls),\n lambda: store._createIndexesFor(cls, []))", "response": "Upgrade a store to use explicit oid columns."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking that all of the accumulated Items have a way to get the latest version of the items.", "response": "def checkUpgradePaths(self):\n \"\"\"\n Check that all of the accumulated old Item types have a way to get\n from their current version to the latest version.\n\n @raise axiom.errors.NoUpgradePathAvailable: for any, and all, Items\n that do not have a valid upgrade path\n \"\"\"\n cantUpgradeErrors = []\n\n for oldVersion in self._oldTypesRemaining:\n # We have to be able to get from oldVersion.schemaVersion to\n # the most recent type.\n\n currentType = _typeNameToMostRecentClass.get(\n oldVersion.typeName, None)\n\n if currentType is None:\n # There isn't a current version of this type; it's entirely\n # legacy, will be upgraded by deleting and replacing with\n # something else.\n continue\n\n typeInQuestion = oldVersion.typeName\n upgver = oldVersion.schemaVersion\n\n while upgver < currentType.schemaVersion:\n # Do we have enough of the schema present to upgrade?\n if ((typeInQuestion, upgver)\n not in _upgradeRegistry):\n cantUpgradeErrors.append(\n \"No upgrader present for %s (%s) from %d to %d\" % (\n typeInQuestion, qual(currentType), upgver,\n upgver + 1))\n\n # Is there a type available for each upgrader version?\n if upgver+1 != currentType.schemaVersion:\n if (typeInQuestion, upgver+1) not in _legacyTypes:\n cantUpgradeErrors.append(\n \"Type schema required for upgrade missing:\"\n \" %s version %d\" % (\n typeInQuestion, upgver+1))\n upgver += 1\n\n if cantUpgradeErrors:\n raise NoUpgradePathAvailable('\\n '.join(cantUpgradeErrors))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nqueue a type upgrade for oldtype.", "response": "def queueTypeUpgrade(self, oldtype):\n \"\"\"\n Queue a type upgrade for C{oldtype}.\n \"\"\"\n if oldtype not in self._oldTypesRemaining:\n self._oldTypesRemaining.append(oldtype)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupgrade a legacy item.", "response": "def upgradeItem(self, thisItem):\n \"\"\"\n Upgrade a legacy item.\n\n @raise axiom.errors.UpgraderRecursion: If the given item is already in\n the process of being upgraded.\n \"\"\"\n sid = thisItem.storeID\n if sid in self._currentlyUpgrading:\n raise UpgraderRecursion()\n self._currentlyUpgrading[sid] = thisItem\n try:\n return upgradeAllTheWay(thisItem)\n finally:\n self._currentlyUpgrading.pop(sid)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupgrades the entire store in batches yielding after each batch.", "response": "def upgradeBatch(self, n):\n \"\"\"\n Upgrade the entire store in batches, yielding after each batch.\n\n @param n: Number of upgrades to perform per transaction\n @type n: C{int}\n\n @raise axiom.errors.ItemUpgradeError: if an item upgrade failed\n\n @return: A generator that yields after each batch upgrade. This needs\n to be consumed for upgrading to actually take place.\n \"\"\"\n store = self.store\n\n def _doBatch(itemType):\n upgradedAnything = False\n\n for theItem in store.query(itemType, limit=n):\n upgradedAnything = True\n try:\n self.upgradeItem(theItem)\n except:\n f = Failure()\n raise ItemUpgradeError(\n f, theItem.storeID, itemType,\n _typeNameToMostRecentClass[itemType.typeName])\n\n return upgradedAnything\n\n if self.upgradesPending:\n didAny = False\n\n while self._oldTypesRemaining:\n t0 = self._oldTypesRemaining[0]\n\n upgradedAnything = store.transact(_doBatch, t0)\n if not upgradedAnything:\n self._oldTypesRemaining.pop(0)\n if didAny:\n msg(\"%s finished upgrading %s\" % (store.dbdir.path, qual(t0)))\n continue\n elif not didAny:\n didAny = True\n msg(\"%s beginning upgrade...\" % (store.dbdir.path,))\n\n yield None\n\n if didAny:\n msg(\"%s completely upgraded.\" % (store.dbdir.path,))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef open(self):\n self.vg.open()\n self.__lvh = lvm_lv_from_uuid(self.vg.handle, self.uuid)\n if not bool(self.__lvh):\n raise HandleError(\"Failed to initialize LV Handle.\")", "response": "Opens the lvm vg_t and LV_t and initializes the object."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef name(self):\n self.open()\n name = lvm_lv_get_name(self.__lvh)\n self.close()\n return name", "response": "Returns the logical volume name."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn True if the logical volume is active False otherwise.", "response": "def is_active(self):\n \"\"\"\n Returns True if the logical volume is active, False otherwise.\n \"\"\"\n self.open()\n active = lvm_lv_is_active(self.__lvh)\n self.close()\n return bool(active)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns True if the logical volume is suspended False otherwise.", "response": "def is_suspended(self):\n \"\"\"\n Returns True if the logical volume is suspended, False otherwise.\n \"\"\"\n self.open()\n susp = lvm_lv_is_suspended(self.__lvh)\n self.close()\n return bool(susp)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the logical volume size in the given units.", "response": "def size(self, units=\"MiB\"):\n \"\"\"\n Returns the logical volume size in the given units. Default units are MiB.\n\n *Args:*\n\n * units (str): Unit label ('MiB', 'GiB', etc...). Default is MiB.\n \"\"\"\n self.open()\n size = lvm_lv_get_size(self.__lvh)\n self.close()\n return size_convert(size, units)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef activate(self):\n self.open()\n a = lvm_lv_activate(self.handle)\n self.close()\n if a != 0:\n raise CommitError(\"Failed to activate LV.\")", "response": "Activates the logical volume."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeactivates the logical volume.", "response": "def deactivate(self):\n \"\"\"\n Deactivates the logical volume.\n\n *Raises:*\n\n * HandleError\n \"\"\"\n self.open()\n d = lvm_lv_deactivate(self.handle)\n self.close()\n if d != 0:\n raise CommitError(\"Failed to deactivate LV.\")"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nopen the lvm vg_t and pv_t handle.", "response": "def open(self):\n \"\"\"\n Obtains the lvm, vg_t and pv_t handle. Usually you would never need to use this\n method unless you are doing operations using the ctypes function wrappers in\n conversion.py\n\n *Raises:*\n\n * HandleError\n \"\"\"\n self.vg.open()\n self.__pvh = lvm_pv_from_uuid(self.vg.handle, self.uuid)\n if not bool(self.__pvh):\n raise HandleError(\"Failed to initialize PV Handle.\")"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the physical volume device path.", "response": "def name(self):\n \"\"\"\n Returns the physical volume device path.\n \"\"\"\n self.open()\n name = lvm_pv_get_name(self.handle)\n self.close()\n return name"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef mda_count(self):\n self.open()\n mda = lvm_pv_get_mda_count(self.handle)\n self.close()\n return mda", "response": "Returns the physical volume mda count."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the physical volume size in the given units.", "response": "def size(self, units=\"MiB\"):\n \"\"\"\n Returns the physical volume size in the given units. Default units are MiB.\n\n *Args:*\n\n * units (str): Unit label ('MiB', 'GiB', etc...). Default is MiB.\n \"\"\"\n self.open()\n size = lvm_pv_get_size(self.handle)\n self.close()\n return size_convert(size, units)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dev_size(self, units=\"MiB\"):\n self.open()\n size = lvm_pv_get_dev_size(self.handle)\n self.close()\n return size_convert(size, units)", "response": "Returns the device size in the given units."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef free(self, units=\"MiB\"):\n self.open()\n size = lvm_pv_get_free(self.handle)\n self.close()\n return size_convert(size, units)", "response": "Returns the free size in the given units."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\niterating through the available valid fields.", "response": "def iter_valid_fields(meta):\n \"\"\"walk through the available valid fields..\"\"\"\n\n # fetch field configuration and always add the id_field as exclude\n meta_fields = getattr(meta, 'fields', ())\n meta_exclude = getattr(meta, 'exclude', ())\n meta_exclude += (meta.document._meta.get('id_field'),)\n\n # walk through meta_fields or through the document fields to keep\n # meta_fields order in the form\n if meta_fields:\n for field_name in meta_fields:\n field = meta.document._fields.get(field_name)\n if field:\n yield (field_name, field)\n else:\n for field_name, field in meta.document._fields.iteritems():\n # skip excluded fields\n if field_name not in meta_exclude:\n yield (field_name, field)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves this object from the target.", "response": "def uninstallFrom(self, target):\n \"\"\"\n Remove this object from the target, as well as any dependencies\n that it automatically installed which were not explicitly\n \"pinned\" by calling \"install\", and raising an exception if\n anything still depends on this.\n \"\"\"\n\n #did this class powerup on any interfaces? powerdown if so.\n target.powerDown(self)\n\n\n for dc in self.store.query(_DependencyConnector,\n _DependencyConnector.target==target):\n if dc.installee is self:\n dc.deleteFromStore()\n\n for item in installedUniqueRequirements(self, target):\n uninstallFrom(item, target)\n\n callback = getattr(self, \"uninstalled\", None)\n if callback is not None:\n callback()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the install target if this item is installed on another item. Otherwise returns None.", "response": "def installedOn(self):\n \"\"\"\n If this item is installed on another item, return the install\n target. Otherwise return None.\n \"\"\"\n try:\n return self.store.findUnique(_DependencyConnector,\n _DependencyConnector.installee == self\n ).target\n except ItemNotFound:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef installedDependents(self, target):\n for dc in self.store.query(_DependencyConnector,\n _DependencyConnector.target == target):\n depends = dependentsOf(dc.installee.__class__)\n if self.__class__ in depends:\n yield dc.installee", "response": "Returns an iterable of things installed on the target that require this item."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn an iterable of things installed on the target that this item requires and are not required by any other item.", "response": "def installedUniqueRequirements(self, target):\n \"\"\"\n Return an iterable of things installed on the target that this item\n requires and are not required by anything else.\n \"\"\"\n\n myDepends = dependentsOf(self.__class__)\n #XXX optimize?\n for dc in self.store.query(_DependencyConnector,\n _DependencyConnector.target==target):\n if dc.installee is self:\n #we're checking all the others not ourself\n continue\n depends = dependentsOf(dc.installee.__class__)\n if self.__class__ in depends:\n raise DependencyError(\n \"%r cannot be uninstalled from %r, \"\n \"%r still depends on it\" % (self, target, dc.installee))\n\n for cls in myDepends[:]:\n #If one of my dependencies is required by somebody\n #else, leave it alone\n if cls in depends:\n myDepends.remove(cls)\n\n for dc in self.store.query(_DependencyConnector,\n _DependencyConnector.target==target):\n if (dc.installee.__class__ in myDepends\n and not dc.explicitlyInstalled):\n yield dc.installee"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef installedRequirements(self, target):\n myDepends = dependentsOf(self.__class__)\n for dc in self.store.query(_DependencyConnector,\n _DependencyConnector.target == target):\n if dc.installee.__class__ in myDepends:\n yield dc.installee", "response": "Returns an iterable of things installed on the target that this item requires."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef storeServiceSpecialCase(st, pups):\n if st.parent is not None:\n # If for some bizarre reason we're starting a substore's service, let's\n # just assume that its parent is running its upgraders, rather than\n # risk starting the upgrader run twice. (XXX: it *IS* possible to\n # figure out whether we need to or not, I just doubt this will ever\n # even happen in practice -- fix here if it does)\n return serviceSpecialCase(st, pups)\n if st._axiom_service is not None:\n # not new, don't add twice.\n return st._axiom_service\n\n collection = serviceSpecialCase(st, pups)\n\n st._upgradeService.setServiceParent(collection)\n\n if st.dbdir is not None:\n from axiom import batch\n batcher = batch.BatchProcessingControllerService(st)\n batcher.setServiceParent(collection)\n\n scheduler = iaxiom.IScheduler(st)\n # If it's an old database, we might get a SubScheduler instance. It has no\n # setServiceParent method.\n setServiceParent = getattr(scheduler, 'setServiceParent', None)\n if setServiceParent is not None:\n setServiceParent(collection)\n\n return collection", "response": "Adapt a store to L { IServiceCollection } and return it."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _schedulerServiceSpecialCase(empowered, pups):\n from axiom.scheduler import _SiteScheduler, _UserScheduler\n\n # Give precedence to anything found in the store\n for pup in pups:\n return pup\n # If the empowered is a store, construct a scheduler for it.\n if isinstance(empowered, Store):\n if getattr(empowered, '_schedulerService', None) is None:\n if empowered.parent is None:\n sched = _SiteScheduler(empowered)\n else:\n sched = _UserScheduler(empowered)\n empowered._schedulerService = sched\n return empowered._schedulerService\n return None", "response": "This function creates or returns a previously created L { IScheduler } for the given set of powerups."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nformat a schema mismatch for human consumption.", "response": "def _diffSchema(diskSchema, memorySchema):\n \"\"\"\n Format a schema mismatch for human consumption.\n\n @param diskSchema: The on-disk schema.\n\n @param memorySchema: The in-memory schema.\n\n @rtype: L{bytes}\n @return: A description of the schema differences.\n \"\"\"\n diskSchema = set(diskSchema)\n memorySchema = set(memorySchema)\n diskOnly = diskSchema - memorySchema\n memoryOnly = memorySchema - diskSchema\n diff = []\n if diskOnly:\n diff.append('Only on disk:')\n diff.extend(map(repr, diskOnly))\n if memoryOnly:\n diff.append('Only in memory:')\n diff.extend(map(repr, memoryOnly))\n return '\\n'.join(diff)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nclose this file and commit it to its permanent location.", "response": "def close(self):\n \"\"\"\n Close this file and commit it to its permanent location.\n\n @return: a Deferred which fires when the file has been moved (and\n backed up to tertiary storage, if necessary).\n \"\"\"\n now = time.time()\n try:\n file.close(self)\n _mkdirIfNotExists(self._destpath.dirname())\n self.finalpath = self._destpath\n os.rename(self.name, self.finalpath.path)\n os.utime(self.finalpath.path, (now, now))\n except:\n return defer.fail()\n return defer.succeed(self.finalpath)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _computeFromClause(self, tables):\n tableAliases = []\n self.fromClauseParts = []\n for table in tables:\n # The indirect calls to store.getTableName() will create the tables\n # if needed. (XXX That's bad, actually. They should get created\n # some other way if necessary. -exarkun)\n tableName = table.getTableName(self.store)\n tableAlias = table.getTableAlias(self.store, tuple(tableAliases))\n if tableAlias is None:\n self.fromClauseParts.append(tableName)\n else:\n tableAliases.append(tableAlias)\n self.fromClauseParts.append('%s AS %s' % (tableName,\n tableAlias))\n\n self.sortClauseParts = []\n for attr, direction in self.sort.orderColumns():\n assert direction in ('ASC', 'DESC'), \"%r not in ASC,DESC\" % (direction,)\n if attr.type not in tables:\n raise ValueError(\n \"Ordering references type excluded from comparison\")\n self.sortClauseParts.append(\n '%s %s' % (attr.getColumnName(self.store), direction))", "response": "Generate the FROM and WHERE clauses for the related tables."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _selectStuff(self, verb='SELECT'):\n sqlResults = self._runQuery(verb, self._queryTarget)\n for row in sqlResults:\n yield self._massageData(row)", "response": "Returns a generator which yields the massaged results of this query with the specified SQL verb."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the next entry in the massaged data set.", "response": "def next(self):\n \"\"\"\n This method is deprecated, a holdover from when queries were iterators,\n rather than iterables.\n\n @return: one element of massaged data.\n \"\"\"\n if self._selfiter is None:\n warnings.warn(\n \"Calling 'next' directly on a query is deprecated. \"\n \"Perhaps you want to use iter(query).next(), or something \"\n \"more expressive like store.findFirst or store.findOrCreate?\",\n DeprecationWarning, stacklevel=2)\n self._selfiter = self.__iter__()\n return self._selfiter.next()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef paginate(self, pagesize=20):\n\n sort = self.sort\n oc = list(sort.orderColumns())\n if not oc:\n # You can't have an unsorted pagination.\n sort = self.tableClass.storeID.ascending\n oc = list(sort.orderColumns())\n if len(oc) != 1:\n raise RuntimeError(\"%d-column sorts not supported yet with paginate\" %(len(oc),))\n sortColumn = oc[0][0]\n if oc[0][1] == 'ASC':\n sortOp = operator.gt\n else:\n sortOp = operator.lt\n if _isColumnUnique(sortColumn):\n # This is the easy case. There is never a tie to be broken, so we\n # can just remember our last value and yield from there. Right now\n # this only happens when the column is a storeID, but hopefully in\n # the future we will have more of this.\n tiebreaker = None\n else:\n tiebreaker = self.tableClass.storeID\n\n tied = lambda a, b: (sortColumn.__get__(a) ==\n sortColumn.__get__(b))\n def _AND(a, b):\n if a is None:\n return b\n return attributes.AND(a, b)\n\n results = list(self.store.query(self.tableClass, self.comparison,\n sort=sort, limit=pagesize + 1))\n while results:\n if len(results) == 1:\n # XXX TODO: reject 0 pagesize. If the length of the result set\n # is 1, there's no next result to test for a tie with, so we\n # must be at the end, and we should just yield the result and finish.\n yield results[0]\n return\n for resultidx in range(len(results) - 1):\n # check for a tie.\n result = results[resultidx]\n nextResult = results[resultidx + 1]\n if tied(result, nextResult):\n # Yield any ties first, in the appropriate order.\n lastTieBreaker = tiebreaker.__get__(result)\n # Note that this query is _NOT_ limited: currently large ties\n # will generate arbitrarily large amounts of work.\n trq = self.store.query(\n self.tableClass,\n _AND(self.comparison,\n sortColumn == sortColumn.__get__(result)))\n tiedResults = list(trq)\n tiedResults.sort(key=lambda rslt: (sortColumn.__get__(result),\n tiebreaker.__get__(result)))\n for result in tiedResults:\n yield result\n # re-start the query here ('result' is set to the\n # appropriate value by the inner loop)\n break\n else:\n yield result\n\n lastSortValue = sortColumn.__get__(result) # hooray namespace pollution\n results = list(self.store.query(\n self.tableClass,\n _AND(self.comparison,\n sortOp(sortColumn,\n sortColumn.__get__(result))),\n sort=sort,\n limit=pagesize + 1))", "response": "This method returns an iterable which yields all the results of this query."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a row into an Item instance by loading cached items or creating new ones based on query results.", "response": "def _massageData(self, row):\n \"\"\"\n Convert a row into an Item instance by loading cached items or\n creating new ones based on query results.\n\n @param row: an n-tuple, where n is the number of columns specified by\n my item type.\n\n @return: an instance of the type specified by this query.\n \"\"\"\n result = self.store._loadedItem(self.tableClass, row[0], row[1:])\n assert result.store is not None, \"result %r has funky store\" % (result,)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef getColumn(self, attributeName, raw=False):\n # XXX: 'raw' is undocumented because I think it's completely unused,\n # and it's definitely untested. It should probably be removed when\n # someone has the time. -glyph\n\n # Quotient POP3 server uses it. Not that it shouldn't be removed.\n # ;) -exarkun\n attr = getattr(self.tableClass, attributeName)\n return AttributeQuery(self.store,\n self.tableClass,\n self.comparison,\n self.limit,\n self.offset,\n self.sort,\n attr,\n raw)", "response": "Returns an attribute query for the specified attribute on the Item class that this query was specified by that attribute."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef deleteFromStore(self):\n if (self.limit is None and\n not isinstance(self.sort, attributes.UnspecifiedOrdering)):\n # The ORDER BY is pointless here, and SQLite complains about it.\n return self.cloneQuery(sort=None).deleteFromStore()\n\n #We can do this the fast way or the slow way.\n\n # If there's a 'deleted' callback on the Item type or 'deleteFromStore'\n # is overridden, we have to do it the slow way.\n deletedOverridden = (\n self.tableClass.deleted.im_func is not item.Item.deleted.im_func)\n deleteFromStoreOverridden = (\n self.tableClass.deleteFromStore.im_func is not\n item.Item.deleteFromStore.im_func)\n\n if deletedOverridden or deleteFromStoreOverridden:\n for it in self:\n it.deleteFromStore()\n else:\n\n # Find other item types whose instances need to be deleted\n # when items of the type in this query are deleted, and\n # remove them from the store.\n def itemsToDelete(attr):\n return attr.oneOf(self.getColumn(\"storeID\"))\n\n if not item.allowDeletion(self.store, self.tableClass, itemsToDelete):\n raise errors.DeletionDisallowed(\n 'Cannot delete item; '\n 'has referents with whenDeleted == reference.DISALLOW')\n\n for it in item.dependentItems(self.store,\n self.tableClass, itemsToDelete):\n it.deleteFromStore()\n\n # actually run the DELETE for the items in this query.\n self._runQuery('DELETE', \"\")", "response": "Delete all the Items which are found by this query."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _involvedTables(self):\n # SQL and arguments\n if self.comparison is not None:\n tables = self.comparison.getInvolvedTables()\n self.args = self.comparison.getArgs(self.store)\n else:\n tables = list(self.tableClass)\n self.args = []\n\n for tableClass in self.tableClass:\n if tableClass not in tables:\n raise ValueError(\n \"Comparison omits required reference to result type %s\"\n % tableClass.typeName)\n\n return tables", "response": "Returns a list of tables involved in this query."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _massageData(self, row):\n\n \"\"\"\n Convert a row into a tuple of Item instances, by slicing it\n according to the number of columns for each instance, and then\n proceeding as for ItemQuery._massageData.\n\n @param row: an n-tuple, where n is the total number of columns\n specified by all the item types in this query.\n\n @return: a tuple of instances of the types specified by this query.\n \"\"\"\n offset = 0\n resultBits = []\n\n for i, tableClass in enumerate(self.tableClass):\n numAttrs = self.schemaLengths[i]\n\n result = self.store._loadedItem(self.tableClass[i],\n row[offset],\n row[offset+1:offset+numAttrs])\n assert result.store is not None, \"result %r has funky store\" % (result,)\n resultBits.append(result)\n\n offset += numAttrs\n\n return tuple(resultBits)", "response": "Convert a row into a tuple of Item instances by slicing itby the number of columns for each instance and then slicing itby the number of columns for each instance and then returning a tuple of Item instances."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cloneQuery(self, limit=_noItem, sort=_noItem):\n newq = self.query.cloneQuery(limit=limit, sort=sort)\n return self.__class__(newq)", "response": "Clone the original query which this distinct query wraps and return a new\n wrapper around that clone."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncount the number of distinct results of the wrapped query.", "response": "def count(self):\n \"\"\"\n Count the number of distinct results of the wrapped query.\n\n @return: an L{int} representing the number of distinct results.\n \"\"\"\n if not self.query.store.autocommit:\n self.query.store.checkpoint()\n target = ', '.join([\n tableClass.storeID.getColumnName(self.query.store)\n for tableClass in self.query.tableClass ])\n sql, args = self.query._sqlAndArgs(\n 'SELECT DISTINCT',\n target)\n sql = 'SELECT COUNT(*) FROM (' + sql + ')'\n result = self.query.store.querySQL(sql, args)\n assert len(result) == 1, 'more than one result: %r' % (result,)\n return result[0][0] or 0"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _massageData(self, row):\n if self.raw:\n return row[0]\n return self.attribute.outfilter(row[0], _FakeItemForFilter(self.store))", "response": "Convert a raw database row to the type described by an attribute."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sum(self):\n res = self._runQuery('SELECT', 'SUM(%s)' % (self._queryTarget,)) or [(0,)]\n assert len(res) == 1, \"more than one result: %r\" % (res,)\n dbval = res[0][0] or 0\n return self.attribute.outfilter(dbval, _FakeItemForFilter(self.store))", "response": "Returns the sum of all the values returned by this query."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the average value of the entries in the current column.", "response": "def average(self):\n \"\"\"\n Return the average value (as defined by the AVG implementation in the\n database) of the values specified by this query.\n\n Note: for non-numeric column types the result of this method will be\n nonsensical.\n\n @return: a L{float} representing the 'average' value of this column.\n \"\"\"\n rslt = self._runQuery('SELECT', 'AVG(%s)' % (self._queryTarget,)) or [(0,)]\n assert len(rslt) == 1, 'more than one result: %r' % (rslt,)\n return rslt[0][0]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _attachChild(self, child):\n \"attach a child database, returning an identifier for it\"\n self._childCounter += 1\n databaseName = 'child_db_%d' % (self._childCounter,)\n self._attachedChildren[databaseName] = child\n # ATTACH DATABASE statements can't use bind paramaters, blech.\n self.executeSQL(\"ATTACH DATABASE '%s' AS %s\" % (\n child.dbdir.child('db.sqlite').path,\n databaseName,))\n return databaseName", "response": "attach a child database returning an identifier for it"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalling during the initialization of the base class. This method is called by the base class to check consistency of the database with the items in memory and then checks that all of the items in memory are consistent.", "response": "def _startup(self):\n \"\"\"\n Called during __init__. Check consistency of schema in database with\n classes in memory. Load all Python modules for stored items, and load\n version information for upgrader service to run later.\n \"\"\"\n typesToCheck = []\n\n for oid, module, typename, version in self.querySchemaSQL(_schema.ALL_TYPES):\n if self.debug:\n print\n print 'SCHEMA:', oid, module, typename, version\n if typename not in _typeNameToMostRecentClass:\n try:\n namedAny(module)\n except ValueError as err:\n raise ImportError('cannot find module ' + module, str(err))\n self.typenameAndVersionToID[typename, version] = oid\n\n # Can't call this until typenameAndVersionToID is populated, since this\n # depends on building a reverse map of that.\n persistedSchema = self._loadTypeSchema()\n\n # Now that we have persistedSchema, loop over everything again and\n # prepare old types.\n for (typename, version), typeID in self.typenameAndVersionToID.iteritems():\n cls = _typeNameToMostRecentClass.get(typename)\n\n if cls is not None:\n if version != cls.schemaVersion:\n typesToCheck.append(\n self._prepareOldVersionOf(\n typename, version, persistedSchema))\n else:\n typesToCheck.append(cls)\n\n for cls in typesToCheck:\n self._checkTypeSchemaConsistency(cls, persistedSchema)\n\n # Schema is consistent! Now, if I forgot to create any indexes last\n # time I saw this table, do it now...\n extantIndexes = self._loadExistingIndexes()\n for cls in typesToCheck:\n self._createIndexesFor(cls, extantIndexes)\n\n self._upgradeManager.checkUpgradePaths()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfind an item in the database and create one if not found.", "response": "def findOrCreate(self, userItemClass, __ifnew=None, **attrs):\n \"\"\"\n Usage::\n\n s.findOrCreate(userItemClass [, function] [, x=1, y=2, ...])\n\n Example::\n\n class YourItemType(Item):\n a = integer()\n b = text()\n c = integer()\n\n def f(x):\n print x, \\\"-- it's new!\\\"\n s.findOrCreate(YourItemType, f, a=1, b=u'2')\n\n Search for an item with columns in the database that match the passed\n set of keyword arguments, returning the first match if one is found,\n creating one with the given attributes if not. Takes an optional\n positional argument function to call on the new item if it is new.\n \"\"\"\n andargs = []\n for k, v in attrs.iteritems():\n col = getattr(userItemClass, k)\n andargs.append(col == v)\n\n if len(andargs) == 0:\n cond = []\n elif len(andargs) == 1:\n cond = [andargs[0]]\n else:\n cond = [attributes.AND(*andargs)]\n\n for result in self.query(userItemClass, *cond):\n return result\n newItem = userItemClass(store=self, **attrs)\n if __ifnew is not None:\n __ifnew(newItem)\n return newItem"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nopen a new file somewhere in this Store s file area.", "response": "def newFile(self, *path):\n \"\"\"\n Open a new file somewhere in this Store's file area.\n\n @param path: a sequence of path segments.\n\n @return: an L{AtomicFile}.\n \"\"\"\n assert len(path) > 0, \"newFile requires a nonzero number of segments\"\n if self.dbdir is None:\n if self.filesdir is None:\n raise RuntimeError(\"This in-memory store has no file directory\")\n else:\n tmpbase = self.filesdir\n else:\n tmpbase = self.dbdir\n tmpname = tmpbase.child('temp').child(str(tempCounter.next()) + \".tmp\")\n return AtomicFile(tmpname.path, self.newFilePath(*path))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _loadTypeSchema(self):\n\n # Oops, need an index going the other way. This only happens once per\n # store open, and it's based on data queried from the store, so there\n # doesn't seem to be any broader way to cache and re-use the result.\n # However, if we keyed the resulting dict on the database typeID rather\n # than (typeName, schemaVersion), we wouldn't need the information this\n # dict gives us. That would mean changing the callers of this function\n # to use typeID instead of that tuple, which may be possible. Probably\n # only represents a very tiny possible speedup.\n typeIDToNameAndVersion = {}\n for key, value in self.typenameAndVersionToID.iteritems():\n typeIDToNameAndVersion[value] = key\n\n # Indexing attribute, ordering by it, and getting rid of row_offset\n # from the schema and the sorted() here doesn't seem to be any faster\n # than doing this.\n persistedSchema = sorted(self.querySchemaSQL(\n \"SELECT attribute, type_id, sqltype, indexed, \"\n \"pythontype, docstring FROM *DATABASE*.axiom_attributes \"))\n\n # This is trivially (but measurably!) faster than getattr(attributes,\n # pythontype).\n getAttribute = attributes.__dict__.__getitem__\n\n result = {}\n for (attribute, typeID, sqltype, indexed, pythontype,\n docstring) in persistedSchema:\n key = typeIDToNameAndVersion[typeID]\n if key not in result:\n result[key] = []\n result[key].append((\n attribute, sqltype, indexed,\n getAttribute(pythontype), docstring))\n return result", "response": "Load all of the stored schema information for all types known by this store."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _checkTypeSchemaConsistency(self, actualType, onDiskSchema):\n # make sure that both the runtime and the database both know about this\n # type; if they don't both know, we can't check that their views are\n # consistent\n try:\n inMemorySchema = _inMemorySchemaCache[actualType]\n except KeyError:\n inMemorySchema = _inMemorySchemaCache[actualType] = [\n (storedAttribute.attrname, storedAttribute.sqltype)\n for (name, storedAttribute) in actualType.getSchema()]\n\n key = (actualType.typeName, actualType.schemaVersion)\n persistedSchema = [(storedAttribute[0], storedAttribute[1])\n for storedAttribute in onDiskSchema[key]]\n if inMemorySchema != persistedSchema:\n raise RuntimeError(\n \"Schema mismatch on already-loaded %r <%r> object version %d:\\n%s\" %\n (actualType, actualType.typeName, actualType.schemaVersion,\n _diffSchema(persistedSchema, inMemorySchema)))\n\n if actualType.__legacy__:\n return\n\n if (key[0], key[1] + 1) in onDiskSchema:\n raise RuntimeError(\n \"Memory version of %r is %d; database has newer\" % (\n actualType.typeName, key[1]))", "response": "Called for all known types at database startup. Checks that the schema of the a\n table is consistent with the schema of the onDiskSchema."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _prepareOldVersionOf(self, typename, version, persistedSchema):\n appropriateSchema = persistedSchema[typename, version]\n # create actual attribute objects\n dummyAttributes = {}\n for (attribute, sqlType, indexed, pythontype,\n docstring) in appropriateSchema:\n atr = pythontype(indexed=indexed, doc=docstring)\n dummyAttributes[attribute] = atr\n dummyBases = []\n oldType = declareLegacyItem(\n typename, version, dummyAttributes, dummyBases)\n self._upgradeManager.queueTypeUpgrade(oldType)\n return oldType", "response": "Prepare a dummy item class for the old version of a particular type."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef findUnique(self, tableClass, comparison=None, default=_noItem):\n results = list(self.query(tableClass, comparison, limit=2))\n lr = len(results)\n\n if lr == 0:\n if default is _noItem:\n raise errors.ItemNotFound(comparison)\n else:\n return default\n elif lr == 2:\n raise errors.DuplicateUniqueItem(comparison, results)\n elif lr == 1:\n return results[0]\n else:\n raise AssertionError(\"limit=2 database query returned 3+ results: \",\n comparison, results)", "response": "Find an item in the database which should be unique."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef findFirst(self, tableClass, comparison=None,\n offset=None, sort=None, default=None):\n \"\"\"\n Usage::\n\n s.findFirst(tableClass [, query arguments except 'limit'])\n\n Example::\n\n class YourItemType(Item):\n a = integer()\n b = text()\n c = integer()\n ...\n it = s.findFirst(YourItemType,\n AND(YourItemType.a == 1,\n YourItemType.b == u'2'),\n sort=YourItemType.c.descending)\n\n Search for an item with columns in the database that match the passed\n comparison, offset and sort, returning the first match if one is found,\n or the passed default (None if none is passed) if one is not found.\n \"\"\"\n\n limit = 1\n for item in self.query(tableClass, comparison, limit, offset, sort):\n return item\n return default", "response": "Returns the first item in the database that matches the passed in criteria."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning an iterable of items or items of a certain class.", "response": "def query(self, tableClass, comparison=None,\n limit=None, offset=None, sort=None):\n \"\"\"\n Return a generator of instances of C{tableClass},\n or tuples of instances if C{tableClass} is a\n tuple of classes.\n\n Examples::\n\n fastCars = s.query(Vehicle,\n axiom.attributes.AND(\n Vehicle.wheels == 4,\n Vehicle.maxKPH > 200),\n limit=100,\n sort=Vehicle.maxKPH.descending)\n\n quotesByClient = s.query( (Client, Quote),\n axiom.attributes.AND(\n Client.active == True,\n Quote.client == Client.storeID,\n Quote.created >= someDate),\n limit=10,\n sort=(Client.name.ascending,\n Quote.created.descending))\n\n @param tableClass: a subclass of Item to look for instances of,\n or a tuple of subclasses.\n\n @param comparison: a provider of L{IComparison}, or None, to match\n all items available in the store. If tableClass is a tuple, then\n the comparison must refer to all Item subclasses in that tuple,\n and specify the relationships between them.\n\n @param limit: an int to limit the total length of the results, or None\n for all available results.\n\n @param offset: an int to specify a starting point within the available\n results, or None to start at 0.\n\n @param sort: an L{ISort}, something that comes from an SQLAttribute's\n 'ascending' or 'descending' attribute.\n\n @return: an L{ItemQuery} object, which is an iterable of Items or\n tuples of Items, according to tableClass.\n \"\"\"\n if isinstance(tableClass, tuple):\n queryClass = MultipleItemQuery\n else:\n queryClass = ItemQuery\n\n return queryClass(self, tableClass, comparison, limit, offset, sort)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef batchInsert(self, itemType, itemAttributes, dataRows):\n class FakeItem:\n pass\n _NEEDS_DEFAULT = object() # token for lookup failure\n fakeOSelf = FakeItem()\n fakeOSelf.store = self\n sql = itemType._baseInsertSQL(self)\n indices = {}\n schema = [attr for (name, attr) in itemType.getSchema()]\n for i, attr in enumerate(itemAttributes):\n indices[attr] = i\n for row in dataRows:\n oid = self.store.executeSchemaSQL(\n _schema.CREATE_OBJECT, [self.store.getTypeID(itemType)])\n insertArgs = [oid]\n for attr in schema:\n i = indices.get(attr, _NEEDS_DEFAULT)\n if i is _NEEDS_DEFAULT:\n pyval = attr.default\n else:\n pyval = row[i]\n dbval = attr._convertPyval(fakeOSelf, pyval)\n insertArgs.append(dbval)\n self.executeSQL(sql, insertArgs)", "response": "Create multiple items in the store without loading corresponding Python objects into memory."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd an item to the current transaction s list of changed items.", "response": "def changed(self, item):\n \"\"\"\n An item in this store was changed. Add it to the current transaction's\n list of changed items, if a transaction is currently underway, or raise\n an exception if this L{Store} is currently in a state which does not\n allow changes.\n \"\"\"\n if self._rejectChanges:\n raise errors.ChangeRejected()\n if self.transaction is not None:\n self.transaction.add(item)\n self.touched.add(item)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef transact(self, f, *a, **k):\n if self.transaction is not None:\n return f(*a, **k)\n if self.attachedToParent:\n return self.parent.transact(f, *a, **k)\n try:\n self._begin()\n try:\n result = f(*a, **k)\n self.checkpoint()\n except:\n exc = Failure()\n try:\n self.revert()\n except:\n log.err(exc)\n raise\n raise\n else:\n self._commit()\n return result\n finally:\n self._cleanupTxnState()", "response": "Execute a function in the context of a nested transaction."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the unqualified name of the given attribute of the given table.", "response": "def _indexNameOf(self, tableClass, attrname):\n \"\"\"\n Return the unqualified (ie, no database name) name of the given\n attribute of the given table.\n\n @type tableClass: L{MetaItem}\n @param tableClass: The Python class associated with a table in the\n database.\n\n @param attrname: A sequence of the names of the columns of the\n indicated table which will be included in the named index.\n\n @return: A C{str} giving the name of the index which will index the\n given attributes of the given table.\n \"\"\"\n return \"axiomidx_%s_v%d_%s\" % (tableClass.typeName,\n tableClass.schemaVersion,\n '_'.join(attrname))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nretrieves the fully qualified name of the table holding items of a particular class in this store.", "response": "def getTableName(self, tableClass):\n \"\"\"\n Retrieve the fully qualified name of the table holding items\n of a particular class in this store. If the table does not\n exist in the database, it will be created as a side-effect.\n\n @param tableClass: an Item subclass\n\n @raises axiom.errors.ItemClassesOnly: if an object other than a\n subclass of Item is passed.\n\n @return: a string\n \"\"\"\n if not (isinstance(tableClass, type) and issubclass(tableClass, item.Item)):\n raise errors.ItemClassesOnly(\"Only subclasses of Item have table names.\")\n\n if tableClass not in self.typeToTableNameCache:\n self.typeToTableNameCache[tableClass] = self._tableNameFor(tableClass.typeName, tableClass.schemaVersion)\n # make sure the table exists\n self.getTypeID(tableClass)\n return self.typeToTableNameCache[tableClass]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef getColumnName(self, attribute):\n if attribute not in self.attrToColumnNameCache:\n self.attrToColumnNameCache[attribute] = '.'.join(\n (self.getTableName(attribute.type),\n self.getShortColumnName(attribute)))\n return self.attrToColumnNameCache[attribute]", "response": "Retreive the fully qualified column name for a particular anatomical item in this store."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nretrieves the typeID associated with a particular table in this Store.", "response": "def getTypeID(self, tableClass):\n \"\"\"\n Retrieve the typeID associated with a particular table in the\n in-database schema for this Store. A typeID is an opaque integer\n representing the Item subclass, and the associated table in this\n Store's SQLite database.\n\n @param tableClass: a subclass of Item\n\n @return: an integer\n \"\"\"\n key = (tableClass.typeName,\n tableClass.schemaVersion)\n if key in self.typenameAndVersionToID:\n return self.typenameAndVersionToID[key]\n return self.transact(self._maybeCreateTable, tableClass, key)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _justCreateTable(self, tableClass):\n sqlstr = []\n sqlarg = []\n\n # needs to be calculated including version\n tableName = self._tableNameFor(tableClass.typeName,\n tableClass.schemaVersion)\n\n sqlstr.append(\"CREATE TABLE %s (\" % tableName)\n\n # The column is named \"oid\" instead of \"storeID\" for backwards\n # compatibility with the implicit oid/rowid column in old Stores.\n sqlarg.append(\"oid INTEGER PRIMARY KEY\")\n for nam, atr in tableClass.getSchema():\n sqlarg.append(\"\\n%s %s\" %\n (atr.getShortColumnName(self), atr.sqltype))\n\n sqlstr.append(', '.join(sqlarg))\n sqlstr.append(')')\n self.createSQL(''.join(sqlstr))", "response": "Execute the table creation DDL for an Item subclass."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _maybeCreateTable(self, tableClass, key):\n try:\n self._justCreateTable(tableClass)\n except errors.TableAlreadyExists:\n # Although we don't have a memory of this table from the last time\n # we called \"_startup()\", another process has updated the schema\n # since then.\n self._startup()\n return self.typenameAndVersionToID[key]\n\n\n typeID = self.executeSchemaSQL(_schema.CREATE_TYPE,\n [tableClass.typeName,\n tableClass.__module__,\n tableClass.schemaVersion])\n\n self.typenameAndVersionToID[key] = typeID\n\n if self.tablesCreatedThisTransaction is not None:\n self.tablesCreatedThisTransaction.append(tableClass)\n\n # If the new type is a legacy type (not the current version), we need\n # to queue it for upgrade to ensure that if we are in the middle of an\n # upgrade, legacy items of this version get upgraded.\n cls = _typeNameToMostRecentClass.get(tableClass.typeName)\n if cls is not None and tableClass.schemaVersion != cls.schemaVersion:\n self._upgradeManager.queueTypeUpgrade(tableClass)\n\n # We can pass () for extantIndexes here because since the table didn't\n # exist for tableClass, none of its indexes could have either.\n # Whatever checks _createIndexesFor will make would give the same\n # result against the actual set of existing indexes as they will\n # against ().\n self._createIndexesFor(tableClass, ())\n\n for n, (name, storedAttribute) in enumerate(tableClass.getSchema()):\n self.executeSchemaSQL(\n _schema.ADD_SCHEMA_ATTRIBUTE,\n [typeID, n, storedAttribute.indexed, storedAttribute.sqltype,\n storedAttribute.allowNone, storedAttribute.attrname,\n storedAttribute.doc, storedAttribute.__class__.__name__])\n # XXX probably need something better for pythontype eventually,\n # when we figure out a good way to do user-defined attributes or we\n # start parameterizing references.\n\n return typeID", "response": "Internal method to create a new typeID for an Item subclass."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _createIndexesFor(self, tableClass, extantIndexes):\n try:\n indexes = _requiredTableIndexes[tableClass]\n except KeyError:\n indexes = set()\n for nam, atr in tableClass.getSchema():\n if atr.indexed:\n indexes.add(((atr.getShortColumnName(self),), (atr.attrname,)))\n for compound in atr.compoundIndexes:\n indexes.add((tuple(inatr.getShortColumnName(self) for inatr in compound),\n tuple(inatr.attrname for inatr in compound)))\n _requiredTableIndexes[tableClass] = indexes\n\n # _ZOMFG_ SQL is such a piece of _shit_: you can't fully qualify the\n # table name in CREATE INDEX statements because the _INDEX_ is fully\n # qualified!\n\n indexColumnPrefix = '.'.join(self.getTableName(tableClass).split(\".\")[1:])\n\n for (indexColumns, indexAttrs) in indexes:\n nameOfIndex = self._indexNameOf(tableClass, indexAttrs)\n if nameOfIndex in extantIndexes:\n continue\n csql = 'CREATE INDEX %s.%s ON %s(%s)' % (\n self.databaseName, nameOfIndex, indexColumnPrefix,\n ', '.join(indexColumns))\n self.createSQL(csql)", "response": "Create any indexes which don t exist and are required by the schema\n ."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nretrieving an item by its storeID and return it. If no item is found return the given default. If autoUpgrade is set to False the item s class version is not available.", "response": "def getItemByID(self, storeID, default=_noItem, autoUpgrade=True):\n \"\"\"\n Retrieve an item by its storeID, and return it.\n\n Note: most of the failure modes of this method are catastrophic and\n should not be handled by application code. The only one that\n application programmers should be concerned with is KeyError. They are\n listed for educational purposes.\n\n @param storeID: an L{int} which refers to the store.\n\n @param default: if passed, return this value rather than raising in the\n case where no Item is found.\n\n @raise TypeError: if storeID is not an integer.\n\n @raise UnknownItemType: if the storeID refers to an item row in the\n database, but the corresponding type information is not available to\n Python.\n\n @raise RuntimeError: if the found item's class version is higher than\n the current application is aware of. (In other words, if you have\n upgraded a database to a new schema and then attempt to open it with a\n previous version of the code.)\n\n @raise errors.ItemNotFound: if no item existed with the given storeID.\n\n @return: an Item, or the given default, if it was passed and no row\n corresponding to the given storeID can be located in the database.\n \"\"\"\n\n if not isinstance(storeID, (int, long)):\n raise TypeError(\"storeID *must* be an int or long, not %r\" % (\n type(storeID).__name__,))\n if storeID == STORE_SELF_ID:\n return self\n try:\n return self.objectCache.get(storeID)\n except KeyError:\n pass\n log.msg(interface=iaxiom.IStatEvent, stat_cache_misses=1, key=storeID)\n results = self.querySchemaSQL(_schema.TYPEOF_QUERY, [storeID])\n assert (len(results) in [1, 0]),\\\n \"Database panic: more than one result for TYPEOF!\"\n if results:\n typename, module, version = results[0]\n useMostRecent = False\n moreRecentAvailable = False\n\n # The schema may have changed since the last time I saw the\n # database. Let's look to see if this is suspiciously broken...\n\n if _typeIsTotallyUnknown(typename, version):\n # Another process may have created it - let's re-up the schema\n # and see what we get.\n self._startup()\n\n # OK, all the modules have been loaded now, everything\n # verified.\n if _typeIsTotallyUnknown(typename, version):\n\n # If there is STILL no inkling of it anywhere, we are\n # almost certainly boned. Let's tell the user in a\n # structured way, at least.\n raise errors.UnknownItemType(\n \"cannot load unknown schema/version pair: %r %r - id: %r\" %\n (typename, version, storeID))\n\n if typename in _typeNameToMostRecentClass:\n moreRecentAvailable = True\n mostRecent = _typeNameToMostRecentClass[typename]\n\n if mostRecent.schemaVersion < version:\n raise RuntimeError(\"%s:%d - was found in the database and most recent %s is %d\" %\n (typename, version, typename, mostRecent.schemaVersion))\n if mostRecent.schemaVersion == version:\n useMostRecent = True\n if useMostRecent:\n T = mostRecent\n else:\n T = self.getOldVersionOf(typename, version)\n\n # for the moment we're going to assume no inheritance\n attrs = self.querySQL(T._baseSelectSQL(self), [storeID])\n if len(attrs) == 0:\n if default is _noItem:\n raise errors.ItemNotFound(\n 'No results for known-to-be-good object')\n return default\n elif len(attrs) > 1:\n raise errors.DataIntegrityError(\n 'Too many results for {:d}'.format(storeID))\n attrs = attrs[0]\n x = T.existingInStore(self, storeID, attrs)\n if moreRecentAvailable and (not useMostRecent) and autoUpgrade:\n # upgradeVersion will do caching as necessary, we don't have to\n # cache here. (It must, so that app code can safely call\n # upgradeVersion and get a consistent object out of it.)\n x = self.transact(self._upgradeManager.upgradeItem, x)\n elif not x.__legacy__:\n # We loaded the most recent version of an object\n self.objectCache.cache(storeID, x)\n return x\n if default is _noItem:\n raise errors.ItemNotFound(storeID)\n return default"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef querySQL(self, sql, args=()):\n if self.debug:\n result = timeinto(self.queryTimes, self._queryandfetch, sql, args)\n else:\n result = self._queryandfetch(sql, args)\n return result", "response": "For use with SELECT or SELECT - like PRAGMA statements."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexecute a SQL statement and log the time it took to be created.", "response": "def createSQL(self, sql, args=()):\n \"\"\"\n For use with auto-committing statements such as CREATE TABLE or CREATE\n INDEX.\n \"\"\"\n before = time.time()\n self._execSQL(sql, args)\n after = time.time()\n if after - before > 2.0:\n log.msg('Extremely long CREATE: %s' % (after - before,))\n log.msg(sql)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute a SQL query and returns the ID of the last row.", "response": "def executeSQL(self, sql, args=()):\n \"\"\"\n For use with UPDATE or INSERT statements.\n \"\"\"\n sql = self._execSQL(sql, args)\n result = self.cursor.lastRowID()\n if self.executedThisTransaction is not None:\n self.executedThisTransaction.append((result, sql, args))\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngive a filename to a file containing a __counter__ variable, open it, read the count, add one, rewrite the file. This: __counter__=123 Becomes: __counter__=124", "response": "def updateVersion(fname):\n \"\"\"\n given a filename to a file containing a __counter__ variable,\n open it, read the count, add one, rewrite the file.\n\n This:\n __counter__=123\n Becomes:\n __counter__=124\n\n \"\"\"\n fname=os.path.abspath(fname)\n if not os.path.exists(fname):\n print(\"can not update version! file doesn't exist:\\n\",fname)\n return\n with open(fname) as f:\n raw=f.read().split(\"\\n\")\n for i,line in enumerate(raw):\n if line.startswith(\"__counter__=\"):\n version=int(line.split(\"=\")[1])\n raw[i]=\"__counter__=%d\"%(version+1)\n with open(fname,'w') as f:\n f.write(\"\\n\".join(raw))\n print(\"upgraded version %d -> %d\"%(version,version+1))\n sys.path.insert(0,os.path.dirname(fname))\n import version\n print(\"New version:\",version.__version__)\n with open('version.txt','w') as f:\n f.write(str(version.__version__))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupgrade the given L_SubSchedulerParentHook to version 1 to 2.", "response": "def upgradeParentHook1to2(oldHook):\n \"\"\"\n Add the scheduler attribute to the given L{_SubSchedulerParentHook}.\n \"\"\"\n newHook = oldHook.upgradeVersion(\n oldHook.typeName, 1, 2,\n loginAccount=oldHook.loginAccount,\n scheduledAt=oldHook.scheduledAt,\n scheduler=oldHook.store.findFirst(Scheduler))\n return newHook"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upgradeParentHook3to4(old):\n new = old.upgradeVersion(\n old.typeName, 3, 4, subStore=old.loginAccount)\n uninstallFrom(new, new.store)\n return new", "response": "Upgrade parent hook 3 to 4."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef invokeRunnable(self):\n runnable = self.runnable\n if runnable is None:\n self.deleteFromStore()\n else:\n try:\n self.running = True\n newTime = runnable.run()\n finally:\n self.running = False\n self._rescheduleFromRun(newTime)", "response": "Invoke the runnable and reschedule it if needed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef handleError(self, now, failureObj):\n errorHandler = getattr(self.runnable, 'timedEventErrorHandler', None)\n if errorHandler is not None:\n self._rescheduleFromRun(errorHandler(self, failureObj))\n else:\n self._defaultErrorHandler(now, failureObj)", "response": "Called by the error handler method of the TimedEventThread class to handle the given failure object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef unscheduleFirst(self, runnable):\n for evt in self.store.query(TimedEvent, TimedEvent.runnable == runnable, sort=TimedEvent.time.ascending):\n evt.deleteFromStore()\n break", "response": "Remove from given item from the schedule."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn an iterable of the times at which the given item is scheduled to be run.", "response": "def scheduledTimes(self, runnable):\n \"\"\"\n Return an iterable of the times at which the given item is scheduled to\n run.\n \"\"\"\n events = self.store.query(\n TimedEvent, TimedEvent.runnable == runnable)\n return (event.time for event in events if not event.running)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef startService(self):\n super(_SiteScheduler, self).startService()\n self._transientSchedule(self.now(), self.now())", "response": "Start calling persistent timed events whose time has come."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stopService(self):\n super(_SiteScheduler, self).stopService()\n if self.timer is not None:\n self.timer.cancel()\n self.timer = None", "response": "Stop calling persistent timed events."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _transientSchedule(self, when, now):\n if not self.running:\n return\n if self.timer is not None:\n if self.timer.getTime() < when.asPOSIXTimestamp():\n return\n self.timer.cancel()\n delay = when.asPOSIXTimestamp() - now.asPOSIXTimestamp()\n\n # reactor.callLater allows only positive delay values. The scheduler\n # may want to have scheduled things in the past and that's OK, since we\n # are dealing with Time() instances it's impossible to predict what\n # they are relative to the current time from user code anyway.\n delay = max(_EPSILON, delay)\n self.timer = self.callLater(delay, self.tick)\n self.nextEventAt = when", "response": "Schedule a tick to happen later than C { when }."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nscheduling this service s store to tick at the given time.", "response": "def _transientSchedule(self, when, now):\n \"\"\"\n If this service's store is attached to its parent, ask the parent to\n schedule this substore to tick at the given time.\n\n @param when: The time at which to tick.\n @type when: L{epsilon.extime.Time}\n\n @param now: Present for signature compatibility with\n L{_SiteScheduler._transientSchedule}, but ignored otherwise.\n \"\"\"\n if self.store.parent is not None:\n subStore = self.store.parent.getItemByID(self.store.idInParent)\n hook = self.store.parent.findOrCreate(\n _SubSchedulerParentHook,\n subStore=subStore)\n hook._schedule(when)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving the components in the site store for this SubScheduler.", "response": "def migrateDown(self):\n \"\"\"\n Remove the components in the site store for this SubScheduler.\n \"\"\"\n subStore = self.store.parent.getItemByID(self.store.idInParent)\n ssph = self.store.parent.findUnique(\n _SubSchedulerParentHook,\n _SubSchedulerParentHook.subStore == subStore,\n default=None)\n if ssph is not None:\n te = self.store.parent.findUnique(TimedEvent,\n TimedEvent.runnable == ssph,\n default=None)\n if te is not None:\n te.deleteFromStore()\n ssph.deleteFromStore()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrecreate the hooks in the site store to trigger this SubScheduler.", "response": "def migrateUp(self):\n \"\"\"\n Recreate the hooks in the site store to trigger this SubScheduler.\n \"\"\"\n te = self.store.findFirst(TimedEvent, sort=TimedEvent.time.descending)\n if te is not None:\n self._transientSchedule(te.time, None)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef activate(self):\n # This is unfortunate. Perhaps it is the best thing which works (it is\n # the first I found). -exarkun\n if '_axiom_memory_dummy' in vars(self):\n stacklevel = 7\n else:\n stacklevel = 5\n warnings.warn(\n self.__class__.__name__ + \" is deprecated since Axiom 0.5.32. \"\n \"Just adapt stores to IScheduler.\",\n category=PendingDeprecationWarning,\n stacklevel=stacklevel)", "response": "Activates the current IScheduler."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nschedules this hook at a specific time.", "response": "def _schedule(self, when):\n \"\"\"\n Ensure that this hook is scheduled to run at or before C{when}.\n \"\"\"\n sched = IScheduler(self.store)\n for scheduledAt in sched.scheduledTimes(self):\n if when < scheduledAt:\n sched.reschedule(self, scheduledAt, when)\n break\n else:\n sched.schedule(self, when)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef build_graph(path, term_depth=1000, skim_depth=10,\n d_weights=False, **kwargs):\n\n \"\"\"\n Tokenize a text, index a term matrix, and build out a graph.\n\n Args:\n path (str): The file path.\n term_depth (int): Consider the N most frequent terms.\n skim_depth (int): Connect each word to the N closest siblings.\n d_weights (bool): If true, give \"close\" nodes low weights.\n\n Returns:\n Skimmer: The indexed graph.\n \"\"\"\n\n # Tokenize text.\n click.echo('\\nTokenizing text...')\n t = Text.from_file(path)\n click.echo('Extracted %d tokens' % len(t.tokens))\n\n m = Matrix()\n\n # Index the term matrix.\n click.echo('\\nIndexing terms:')\n m.index(t, t.most_frequent_terms(term_depth), **kwargs)\n\n g = Skimmer()\n\n # Construct the network.\n click.echo('\\nGenerating graph:')\n g.build(t, m, skim_depth, d_weights)\n\n return g", "response": "Builds a graph from a text file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef draw_spring(self, **kwargs):\n\n \"\"\"\n Render a spring layout.\n \"\"\"\n\n nx.draw_spring(\n self.graph,\n with_labels=True,\n font_size=10,\n edge_color='#dddddd',\n node_size=0,\n **kwargs\n )\n\n plt.show()", "response": "Render a spring layout."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build(self, text, matrix, skim_depth=10, d_weights=False):\n\n \"\"\"\n 1. For each term in the passed matrix, score its KDE similarity with\n all other indexed terms.\n\n 2. With the ordered stack of similarities in hand, skim off the top X\n pairs and add them as edges.\n\n Args:\n text (Text): The source text instance.\n matrix (Matrix): An indexed term matrix.\n skim_depth (int): The number of siblings for each term.\n d_weights (bool): If true, give \"close\" words low edge weights.\n \"\"\"\n\n for anchor in bar(matrix.keys):\n\n n1 = text.unstem(anchor)\n\n # Heaviest pair scores:\n pairs = matrix.anchored_pairs(anchor).items()\n for term, weight in list(pairs)[:skim_depth]:\n\n # If edges represent distance, use the complement of the raw\n # score, so that similar words are connected by \"short\" edges.\n if d_weights: weight = 1-weight\n\n n2 = text.unstem(term)\n\n # NetworkX does not handle numpy types when writing graphml,\n # so we cast the weight to a regular float.\n self.graph.add_edge(n1, n2, weight=float(weight))", "response": "Builds the KDE graph from the passed text and matrix."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a configuration dictionary from the configuration source.", "response": "def get_settings(self, section=None, defaults=None):\n \"\"\"\n Gets a named section from the configuration source.\n\n :param section: a :class:`str` representing the section you want to\n retrieve from the configuration source. If ``None`` this will\n fallback to the :attr:`plaster.PlasterURL.fragment`.\n :param defaults: a :class:`dict` that will get passed to\n :class:`configparser.ConfigParser` and will populate the\n ``DEFAULT`` section.\n :return: A :class:`plaster_pastedeploy.ConfigDict` of key/value pairs.\n\n \"\"\"\n # This is a partial reimplementation of\n # ``paste.deploy.loadwsgi.ConfigLoader:get_context`` which supports\n # \"set\" and \"get\" options and filters out any other globals\n section = self._maybe_get_default_name(section)\n if self.filepath is None:\n return {}\n parser = self._get_parser(defaults)\n defaults = parser.defaults()\n\n try:\n raw_items = parser.items(section)\n except NoSectionError:\n return {}\n\n local_conf = OrderedDict()\n get_from_globals = {}\n for option, value in raw_items:\n if option.startswith(\"set \"):\n name = option[4:].strip()\n defaults[name] = value\n elif option.startswith(\"get \"):\n name = option[4:].strip()\n get_from_globals[name] = value\n # insert a value into local_conf to preserve the order\n local_conf[name] = None\n else:\n # annoyingly pastedeploy filters out all defaults unless\n # \"get foo\" is used to pull it in\n if option in defaults:\n continue\n local_conf[option] = value\n for option, global_option in get_from_globals.items():\n local_conf[option] = defaults[global_option]\n return ConfigDict(local_conf, defaults, self)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_wsgi_app(self, name=None, defaults=None):\n name = self._maybe_get_default_name(name)\n defaults = self._get_defaults(defaults)\n return loadapp(\n self.pastedeploy_spec,\n name=name,\n relative_to=self.relative_to,\n global_conf=defaults,\n )", "response": "Reads the configuration source and loads a WSGI application defined by the entry with name name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_wsgi_server(self, name=None, defaults=None):\n name = self._maybe_get_default_name(name)\n defaults = self._get_defaults(defaults)\n return loadserver(\n self.pastedeploy_spec,\n name=name,\n relative_to=self.relative_to,\n global_conf=defaults,\n )", "response": "Reads the configuration source and returns a WSGI server object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_wsgi_filter(self, name=None, defaults=None):\n name = self._maybe_get_default_name(name)\n defaults = self._get_defaults(defaults)\n return loadfilter(\n self.pastedeploy_spec,\n name=name,\n relative_to=self.relative_to,\n global_conf=defaults,\n )", "response": "Reads the configuration soruce and returns a WSGI filter that can be used for the application."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dictionary representing the application settings for a named WSGI application.", "response": "def get_wsgi_app_settings(self, name=None, defaults=None):\n \"\"\"\n Return an :class:`collections.OrderedDict` representing the\n application config for a WSGI application named ``name`` in the\n PasteDeploy config file specified by ``self.uri``.\n\n ``defaults``, if passed, should be a dictionary used as variable\n assignments like ``{'http_port': 8080}``. This is useful if e.g.\n ``%(http_port)s`` is used in the config file.\n\n If the ``name`` is None, this will attempt to parse the name from\n the ``config_uri`` string expecting the format ``inifile#name``.\n If no name is found, the name will default to \"main\".\n\n :param name: The named WSGI app for which to find the settings.\n Defaults to ``None`` which becomes ``main``\n inside :func:`paste.deploy.loadapp`.\n :param defaults: The ``global_conf`` that will be used during settings\n generation.\n :return: A :class:`plaster_pastedeploy.ConfigDict` of key/value pairs.\n\n \"\"\"\n name = self._maybe_get_default_name(name)\n defaults = self._get_defaults(defaults)\n conf = appconfig(\n self.pastedeploy_spec,\n name=name,\n relative_to=self.relative_to,\n global_conf=defaults,\n )\n return ConfigDict(conf.local_conf, conf.global_conf, self)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef setup_logging(self, defaults=None):\n if \"loggers\" in self.get_sections():\n defaults = self._get_defaults(defaults)\n fileConfig(self.uri.path, defaults, disable_existing_loggers=False)\n\n else:\n logging.basicConfig()", "response": "Setup logging via logging. config. fileConfig."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _maybe_get_default_name(self, name):\n if name is None and self.uri.fragment:\n name = self.uri.fragment\n return name", "response": "Checks a name and determines whether to use the default name."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set(self, hue):\n x = hue / 360. * self.winfo_width()\n self.coords('cursor', x, 0, x, self.winfo_height())\n self._variable.set(hue)", "response": "Set cursor position on the color corresponding to the hue value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate an L{IService for the database specified by the given options.", "response": "def makeService(cls, options):\n \"\"\"\n Create an L{IService} for the database specified by the given\n configuration.\n \"\"\"\n from axiom.store import Store\n jm = options['journal-mode']\n if jm is not None:\n jm = jm.decode('ascii')\n store = Store(options['dbdir'], debug=options['debug'], journalMode=jm)\n service = IService(store)\n _CheckSystemVersion(store).setServiceParent(service)\n return service"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the names of all pages in the project.", "response": "def pageNames(matching=False,workbooks=True,graphs=True):\n \"\"\"\n Returns the names of everything (books, notes, graphs, etc.) in the project.\n\n Args:\n matching (str, optional): if given, only return names with this string in it\n workbooks (bool): if True, return workbooks\n graphs (bool): if True, return workbooks\n\n Returns:\n A list of the names of what you requested\n \"\"\"\n # first collect the pages we want\n pages=[]\n if workbooks:\n pages.extend(PyOrigin.WorksheetPages())\n if graphs:\n pages.extend(PyOrigin.GraphPages())\n\n # then turn them into a list of strings\n pages = [x.GetName() for x in pages]\n\n # do our string matching if it's needed\n if matching:\n pages=[x for x in pages if matching in x]\n return pages"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the type of the page with that name.", "response": "def getPageType(name,number=False):\n \"\"\"Returns the type of the page with that name.\n If that name doesn't exist, None is returned.\n\n Args:\n name (str): name of the page to get the folder from\n number (bool): if True, return numbers (i.e., a graph will be 3)\n if False, return words where appropriate (i.e, \"graph\")\n\n Returns:\n string of the type of object the page is\n \"\"\"\n if not name in pageNames():\n return None\n pageType=PyOrigin.Pages(name).GetType()\n if number:\n return str(pageType)\n if pageType==1:\n return \"matrix\"\n if pageType==2:\n return \"book\"\n if pageType==3:\n return \"graph\"\n if pageType==4:\n return \"layout\"\n if pageType==5:\n return \"notes\""} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprinting every page in the project to the console.", "response": "def listEverything(matching=False):\n \"\"\"Prints every page in the project to the console.\n\n Args:\n matching (str, optional): if given, only return names with this string in it\n\n \"\"\"\n pages=pageNames()\n if matching:\n pages=[x for x in pages if matching in x]\n for i,page in enumerate(pages):\n pages[i]=\"%s%s (%s)\"%(pageFolder(page),page,getPageType(page))\n print(\"\\n\".join(sorted(pages)))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sheetNames(book=None):\n if book:\n if not book.lower() in [x.lower() for x in bookNames()]:\n return False\n else:\n book=activeBook()\n if not book:\n return False\n poBook=PyOrigin.WorksheetPages(book)\n if not len(poBook):\n return None\n return [x.GetName() for x in poBook.Layers()]", "response": "Return a list of sheet names of a given book."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getSheet(book=None,sheet=None):\n\n # figure out what book to use\n if book and not book.lower() in [x.lower() for x in bookNames()]:\n print(\"book %s doesn't exist\"%book)\n return\n if book is None:\n book=activeBook().lower()\n if book is None:\n print(\"no book given or selected\")\n return\n\n # figure out what sheet to use\n if sheet and not sheet.lower() in [x.lower() for x in sheetNames(book)]:\n print(\"sheet %s doesn't exist\"%sheet)\n return\n if sheet is None:\n sheet=activeSheet().lower()\n if sheet is None:\n return(\"no sheet given or selected\")\n print\n\n # by now, we know the book/sheet exists and can be found\n for poSheet in PyOrigin.WorksheetPages(book).Layers():\n if poSheet.GetName().lower()==sheet.lower():\n return poSheet\n return False", "response": "returns the pyorigin object for a sheet"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a sheet from a book.", "response": "def sheetDelete(book=None,sheet=None):\n \"\"\"\n Delete a sheet from a book. If either isn't given, use the active one.\n \"\"\"\n if book is None:\n book=activeBook()\n if sheet in sheetNames():\n PyOrigin.WorksheetPages(book).Layers(sheetNames().index(sheet)).Destroy()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete all sheets which contain no data", "response": "def sheetDeleteEmpty(bookName=None):\n \"\"\"Delete all sheets which contain no data\"\"\"\n if bookName is None:\n bookName = activeBook()\n if not bookName.lower() in [x.lower() for x in bookNames()]:\n print(\"can't clean up a book that doesn't exist:\",bookName)\n return\n poBook=PyOrigin.WorksheetPages(bookName)\n namesToKill=[]\n for i,poSheet in enumerate([poSheet for poSheet in poBook.Layers()]):\n poFirstCol=poSheet.Columns(0)\n if poFirstCol.GetLongName()==\"\" and poFirstCol.GetData()==[]:\n namesToKill.append(poSheet.GetName())\n for sheetName in namesToKill:\n print(\"deleting empty sheet\",sheetName)\n sheetDelete(bookName,sheetName)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pickle_load(fname):\n assert type(fname) is str and os.path.exists(fname)\n print(\"loaded\",fname)\n return pickle.load(open(fname,\"rb\"))", "response": "return the contents of a pickle file"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pickle_save(thing,fname=None):\n if fname is None:\n fname=os.path.expanduser(\"~\")+\"/%d.pkl\"%time.time()\n assert type(fname) is str and os.path.isdir(os.path.dirname(fname))\n pickle.dump(thing, open(fname,\"wb\"),pickle.HIGHEST_PROTOCOL)\n print(\"saved\",fname)", "response": "save something to a pickle file"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting a PySheets. Sheets object into a temporary HTML file.", "response": "def sheetToHTML(sheet):\n \"\"\"\n Put 2d numpy data into a temporary HTML file.\n This is a hack, copy/pasted from an earlier version of this software.\n It is very messy, but works great! Good enough for me.\n \"\"\"\n\n assert \"SHEET\" in str(type(sheet))\n #data,names=None,units=None,bookName=None,sheetName=None,xCol=None\n #sheet=OR.SHEET()\n data=sheet.data\n names=sheet.colDesc\n units=sheet.colUnits\n bookName=sheet.bookName\n sheetName=sheet.sheetName\n\n def htmlListToTR(l,trClass=None,tdClass=None,td1Class=None):\n \"\"\"\n turns a list into a something\n call this when generating HTML tables dynamically.\n \"\"\"\n html=\"\"\n for item in l:\n html+=\"%s\"%item\n html+=\"\"\n if trClass:\n html=html.replace(\"\",''%trClass)\n if td1Class:\n html=html.replace(\"\",''%td1Class,1)\n if tdClass:\n html=html.replace(\"\",''%tdClass)\n return html\n\n htmlFname = os.path.expanduser(\"~\")+\"/WKS-%s.%s.html\"%(bookName,sheetName)\n html=\"\"\"\n \n \"\"\"\n html+=\"

FauxRigin

\"\n if bookName or sheetName:\n html+='%s / %s

'%(bookName,sheetName)\n html+=\"\"\n colNames=['']\n for i in range(len(units)):\n shortName=chr(i%26+ord('A'))\n if i>=26:\n shortName=chr(int(i/26-1)+ord('A'))+shortName\n label=\"%s(%s)\"%(shortName,\"X\" if sheet.colTypes[i]==3 else \"Y\")\n colNames.append(label)\n html+=htmlListToTR(colNames,'labelCol','labelCol')\n html+=htmlListToTR(['Long Name']+list(names),'name',td1Class='labelRow')\n html+=htmlListToTR(['Units']+list(units),'units',td1Class='labelRow')\n cutOff=False\n for y in range(len(data)):\n html+=htmlListToTR([y+1]+list(data[y]),trClass='data%d'%(y%2),td1Class='labelRow')\n if y>=200:\n cutOff=True\n break\n html+=\"
\"\n html=html.replace(\">nan<\",\">--<\")\n html=html.replace(\">None<\",\"><\")\n if cutOff:\n html+=\"

... showing only %d of %d rows ...

\"%(y,len(data))\n html+=\"\"\n with open(htmlFname,'w') as f:\n f.write(html)\n import webbrowser\n webbrowser.open(htmlFname)\n return"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dict with the code for each function", "response": "def getCodeBlocks():\n \"\"\"return a dict with the code for each function\"\"\"\n raw=open(\"examples.py\").read()\n d={}\n for block in raw.split(\"if __name__\")[0].split(\"\\ndef \"):\n title=block.split(\"\\n\")[0].split(\"(\")[0]\n if not title.startswith(\"demo_\"):\n continue\n code=[x[4:] for x in block.split(\"\\n\")[1:] if x.startswith(\" \")]\n d[title]=\"\\n\".join(code).strip()\n return d"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dict with the output of each function", "response": "def getOutputBlocks():\n \"\"\"return a dict with the output of each function\"\"\"\n raw=open(\"output.txt\").read()\n d={}\n for block in raw.split(\"\\n####### \")[1:]:\n title=block.split(\"\\n\")[0].split(\"(\")[0]\n block=block.split(\"\\n\",1)[1].strip()\n d[title]=block.split(\"\\nfinished in \")[0]\n return d"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nturns a byte string from the command line into a unicode string.", "response": "def decodeCommandLine(self, cmdline):\n \"\"\"Turn a byte string from the command line into a unicode string.\n \"\"\"\n codec = getattr(sys.stdin, 'encoding', None) or sys.getdefaultencoding()\n return unicode(cmdline, codec)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef tokenize(text):\n\n \"\"\"\n Yield tokens.\n\n Args:\n text (str): The original text.\n\n Yields:\n dict: The next token.\n \"\"\"\n\n stem = PorterStemmer().stem\n tokens = re.finditer('[a-z]+', text.lower())\n\n for offset, match in enumerate(tokens):\n\n # Get the raw token.\n unstemmed = match.group(0)\n\n yield { # Emit the token.\n 'stemmed': stem(unstemmed),\n 'unstemmed': unstemmed,\n 'offset': offset\n }", "response": "Yields the next token in the text."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef sort_dict(d, desc=True):\n\n \"\"\"\n Sort an ordered dictionary by value, descending.\n\n Args:\n d (OrderedDict): An ordered dictionary.\n desc (bool): If true, sort desc.\n\n Returns:\n OrderedDict: The sorted dictionary.\n \"\"\"\n\n sort = sorted(d.items(), key=lambda x: x[1], reverse=desc)\n return OrderedDict(sort)", "response": "Sort an ordered dictionary by value descending."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef window(seq, n=2):\n\n \"\"\"\n Yield a sliding window over an iterable.\n\n Args:\n seq (iter): The sequence.\n n (int): The window width.\n\n Yields:\n tuple: The next window.\n \"\"\"\n\n it = iter(seq)\n result = tuple(islice(it, n))\n\n if len(result) == n:\n yield result\n\n for token in it:\n result = result[1:] + (token,)\n yield result", "response": "Yields a sliding window over an iterable."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ninserting a new user store into the given site store.", "response": "def insertUserStore(siteStore, userStorePath):\n \"\"\"\n Move the SubStore at the indicated location into the given site store's\n directory and then hook it up to the site store's authentication database.\n\n @type siteStore: C{Store}\n @type userStorePath: C{FilePath}\n \"\"\"\n # The following may, but does not need to be in a transaction, because it\n # is merely an attempt to guess a reasonable filesystem name to use for\n # this avatar. The user store being operated on is expected to be used\n # exclusively by this process.\n ls = siteStore.findUnique(LoginSystem)\n unattachedSubStore = Store(userStorePath)\n for lm in unattachedSubStore.query(LoginMethod,\n LoginMethod.account == unattachedSubStore.findUnique(LoginAccount),\n sort=LoginMethod.internal.descending):\n if ls.accountByAddress(lm.localpart, lm.domain) is None:\n localpart, domain = lm.localpart, lm.domain\n break\n else:\n raise AllNamesConflict()\n\n unattachedSubStore.close()\n\n insertLocation = siteStore.newFilePath('account', domain, localpart + '.axiom')\n insertParentLoc = insertLocation.parent()\n if not insertParentLoc.exists():\n insertParentLoc.makedirs()\n if insertLocation.exists():\n raise DatabaseDirectoryConflict()\n userStorePath.moveTo(insertLocation)\n ss = SubStore(store=siteStore, storepath=insertLocation)\n attachedStore = ss.open()\n # migrateUp() manages its own transactions because it interacts with two\n # different stores.\n attachedStore.findUnique(LoginAccount).migrateUp()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef extractUserStore(userAccount, extractionDestination, legacySiteAuthoritative=True):\n if legacySiteAuthoritative:\n # migrateDown() manages its own transactions, since it is copying items\n # between two different stores.\n userAccount.migrateDown()\n av = userAccount.avatars\n av.open().close()\n def _():\n # We're separately deleting several Items from the site store, then\n # we're moving some files. If we cannot move the files, we don't want\n # to delete the items.\n\n # There is one unaccounted failure mode here: if the destination of the\n # move is on a different mount point, the moveTo operation will fall\n # back to a non-atomic copy; if all of the copying succeeds, but then\n # part of the deletion of the source files fails, we will be left\n # without a complete store in this site store's files directory, but\n # the account Items will remain. This will cause odd errors on login\n # and at other unpredictable times. The database is only one file, so\n # we will either remove it all or none of it. Resolving this requires\n # manual intervention currently: delete the substore's database\n # directory and the account items (LoginAccount and LoginMethods)\n # manually.\n\n # However, this failure is extremely unlikely, as it would almost\n # certainly indicate a misconfiguration of the permissions on the site\n # store's files area. As described above, a failure of the call to\n # os.rename(), if the platform's rename is atomic (which it generally\n # is assumed to be) will not move any files and will cause a revert of\n # the transaction which would have deleted the accompanying items.\n\n av.deleteFromStore()\n userAccount.deleteLoginMethods()\n userAccount.deleteFromStore()\n av.storepath.moveTo(extractionDestination)\n userAccount.store.transact(_)", "response": "Move the SubStore for the given user account out of the given site store and place it into the given destination directory."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nretrieves all login methods in the given store optionally constraining them by protocol", "response": "def getLoginMethods(store, protocol=None):\n \"\"\"\n Retrieve L{LoginMethod} items from store C{store}, optionally constraining\n them by protocol\n \"\"\"\n if protocol is not None:\n comp = OR(LoginMethod.protocol == u'*',\n LoginMethod.protocol == protocol)\n else:\n comp = None\n return store.query(LoginMethod, comp)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getAccountNames(store, protocol=None):\n return ((meth.localpart, meth.domain) for meth\n in getLoginMethods(store, protocol))", "response": "Retrieve account name information about the given database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nretrieving a list of all local domain names represented in the given store.", "response": "def getDomainNames(store):\n \"\"\"\n Retrieve a list of all local domain names represented in the given store.\n \"\"\"\n domains = set()\n domains.update(store.query(\n LoginMethod,\n AND(LoginMethod.internal == True,\n LoginMethod.domain != None)).getColumn(\"domain\").distinct())\n return sorted(domains)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef migrateDown(self):\n ss = self.avatars.open()\n def _():\n oldAccounts = ss.query(LoginAccount)\n oldMethods = ss.query(LoginMethod)\n for x in list(oldAccounts) + list(oldMethods):\n x.deleteFromStore()\n self.cloneInto(ss, ss)\n IScheduler(ss).migrateDown()\n ss.transact(_)", "response": "Migrate all the entries from the site store into the site store."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef migrateUp(self):\n siteStore = self.store.parent\n def _():\n # No convenience method for the following because needing to do it is\n # *rare*. It *should* be ugly; 99% of the time if you need to do this\n # you're making a mistake. -glyph\n siteStoreSubRef = siteStore.getItemByID(self.store.idInParent)\n self.cloneInto(siteStore, siteStoreSubRef)\n IScheduler(self.store).migrateUp()\n siteStore.transact(_)", "response": "Copy this LoginAccount and all associated LoginMethods from my store into the immutable object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cloneInto(self, newStore, avatars):\n la = LoginAccount(store=newStore,\n password=self.password,\n avatars=avatars,\n disabled=self.disabled)\n for siteMethod in self.store.query(LoginMethod,\n LoginMethod.account == self):\n LoginMethod(store=newStore,\n localpart=siteMethod.localpart,\n domain=siteMethod.domain,\n internal=siteMethod.internal,\n protocol=siteMethod.protocol,\n verified=siteMethod.verified,\n account=la)\n return la", "response": "Create a copy of this LoginAccount and all associated LoginMethods in a different Store."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef addLoginMethod(self, localpart, domain, protocol=ANY_PROTOCOL, verified=False, internal=False):\n # Out takes you west or something\n if self.store.parent is None:\n # West takes you in\n otherStore = self.avatars.open()\n peer = otherStore.findUnique(LoginAccount)\n else:\n # In takes you east\n otherStore = self.store.parent\n subStoreItem = self.store.parent.getItemByID(self.store.idInParent)\n peer = otherStore.findUnique(LoginAccount,\n LoginAccount.avatars == subStoreItem)\n\n # Up and down take you home\n for store, account in [(otherStore, peer), (self.store, self)]:\n store.findOrCreate(LoginMethod,\n account=account,\n localpart=localpart,\n domain=domain,\n protocol=protocol,\n verified=verified,\n internal=internal)", "response": "Adds a login method to this account."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef replacePassword(self, currentPassword, newPassword):\n if unicode(currentPassword) != self.password:\n return fail(BadCredentials())\n return self.setPassword(newPassword)", "response": "Replace the password of this account with a new one."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef addAccount(self, username, domain, password, avatars=None,\n protocol=u'email', disabled=0, internal=False,\n verified=True):\n \"\"\"\n Create a user account, add it to this LoginBase, and return it.\n\n This method must be called within a transaction in my store.\n\n @param username: the user's name.\n\n @param domain: the domain part of the user's name [XXX TODO: this\n really ought to say something about whether it's a Q2Q domain, a SIP\n domain, an HTTP realm, or an email address domain - right now the\n assumption is generally that it's an email address domain, but not\n always]\n\n @param password: A shared secret.\n\n @param avatars: (Optional). A SubStore which, if passed, will be used\n by cred as the target of all adaptations for this user. By default, I\n will create a SubStore, and plugins can be installed on that substore\n using the powerUp method to provide implementations of cred client\n interfaces.\n\n @raise DuplicateUniqueItem: if the 'avatars' argument already contains\n a LoginAccount.\n\n @return: an instance of a LoginAccount, with all attributes filled out\n as they are passed in, stored in my store.\n \"\"\"\n\n # unicode(None) == u'None', kids.\n if username is not None:\n username = unicode(username)\n if domain is not None:\n domain = unicode(domain)\n if password is not None:\n password = unicode(password)\n\n if self.accountByAddress(username, domain) is not None:\n raise DuplicateUser(username, domain)\n if avatars is None:\n avatars = self.makeAvatars(domain, username)\n\n subStore = avatars.open()\n\n # create this unconditionally; as the docstring says, we must be run\n # within a transaction, so if something goes wrong in the substore\n # transaction this item's creation will be reverted...\n la = LoginAccount(store=self.store,\n password=password,\n avatars=avatars,\n disabled=disabled)\n\n def createSubStoreAccountObjects():\n\n LoginAccount(store=subStore,\n password=password,\n disabled=disabled,\n avatars=subStore)\n\n la.addLoginMethod(localpart=username,\n domain=domain,\n protocol=protocol,\n internal=internal,\n verified=verified)\n\n subStore.transact(createSubStoreAccountObjects)\n return la", "response": "Create a user account and return it."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nidentify an appropriate SQL error object for the given message for the the field.", "response": "def identifySQLError(self, sql, args, e):\n \"\"\"\n Identify an appropriate SQL error object for the given message for the\n supported versions of sqlite.\n\n @return: an SQLError\n \"\"\"\n message = e.args[0]\n if message.startswith(\"table\") and message.endswith(\"already exists\"):\n return errors.TableAlreadyExists(sql, args, e)\n return errors.SQLError(sql, args, e)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef createCacheRemoveCallback(cacheRef, key, finalizer):\n def remove(reference):\n # Weakref callbacks cannot raise exceptions or DOOM ensues\n try:\n finalizer()\n except:\n logErrorNoMatterWhat()\n try:\n cache = cacheRef()\n if cache is not None:\n if key in cache.data:\n if cache.data[key] is reference:\n del cache.data[key]\n except:\n logErrorNoMatterWhat()\n return remove", "response": "Creates a callback that will be called when a cache entry is removed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cache(self, key, value):\n fin = value.__finalizer__()\n try:\n # It's okay if there's already a cache entry for this key as long\n # as the weakref has already been broken. See the comment in\n # get() for an explanation of why this might happen.\n if self.data[key]() is not None:\n raise CacheInconsistency(\n \"Duplicate cache key: %r %r %r\" % (\n key, value, self.data[key]))\n except KeyError:\n pass\n callback = createCacheRemoveCallback(self._ref(self), key, fin)\n self.data[key] = self._ref(value, callback)\n return value", "response": "Add an entry to the cache."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves an entry from the cache.", "response": "def uncache(self, key, value):\n \"\"\"\n Remove a key from the cache.\n\n As a sanity check, if the specified key is present in the cache, it\n must have the given value.\n\n @param key: The key to remove.\n\n @param value: The expected value for the key.\n \"\"\"\n try:\n assert self.get(key) is value\n del self.data[key]\n except KeyError:\n # If the entry has already been removed from the cache, this will\n # result in KeyError which we ignore. If the entry is still in the\n # cache, but the weakref has been broken, this will result in\n # CacheFault (a KeyError subclass) which we also ignore. See the\n # comment in get() for an explanation of why this might happen.\n pass"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting an entry from the cache by key.", "response": "def get(self, key):\n \"\"\"\n Get an entry from the cache by key.\n\n @raise KeyError: if the given key is not present in the cache.\n\n @raise CacheFault: (a L{KeyError} subclass) if the given key is present\n in the cache, but the value it points to is gone.\n \"\"\"\n o = self.data[key]()\n if o is None:\n # On CPython, the weakref callback will always(?) run before any\n # other code has a chance to observe that the weakref is broken;\n # and since the callback removes the item from the dict, this\n # branch of code should never run. However, on PyPy (and possibly\n # other Python implementations), the weakref callback does not run\n # immediately, thus we may be able to observe this intermediate\n # state. Should this occur, we remove the dict item ourselves,\n # and raise CacheFault (which is a KeyError subclass).\n del self.data[key]\n raise CacheFault(\n \"FinalizingCache has %r but its value is no more.\" % (key,))\n log.msg(interface=iaxiom.IStatEvent, stat_cache_hits=1, key=key)\n return o"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_question_xml(root):\n question_dict = dict()\n\n question_prompt_el = root.find('text')\n if question_prompt_el is not None:\n question_dict['text'] = _safe_get_text(question_prompt_el)\n else:\n raise ValidationError(_('Question must have text element.'))\n\n # optional image element\n question_dict.update(parse_image_xml(root))\n\n return question_dict", "response": "Parses the question XML element in the UBCPI XBlock s content XML."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse the options XML element in the UBCPI XBlock s content XML.", "response": "def parse_options_xml(root):\n \"\"\"\n Parse element in the UBCPI XBlock's content XML.\n\n Args:\n root (lxml.etree.Element): The root of the node in the tree.\n\n Returns:\n a list of deserialized representation of options. E.g.\n [{\n 'text': 'Option 1',\n 'image_url': '',\n 'image_position': 'below',\n 'image_show_fields': 0,\n 'image_alt': ''\n },\n {....\n }]\n\n Raises:\n ValidationError: The XML definition is invalid.\n \"\"\"\n options = []\n correct_option = None\n rationale = None\n\n for option_el in root.findall('option'):\n option_dict = dict()\n option_prompt_el = option_el.find('text')\n if option_prompt_el is not None:\n option_dict['text'] = _safe_get_text(option_prompt_el)\n else:\n raise ValidationError(_('Option must have text element.'))\n\n # optional image element\n option_dict.update(parse_image_xml(option_el))\n\n if 'correct' in option_el.attrib and _parse_boolean(option_el.attrib['correct']):\n if correct_option is None:\n correct_option = len(options)\n rationale_el = option_el.find('rationale')\n if rationale_el is not None:\n rationale = {'text': _safe_get_text(rationale_el)}\n else:\n raise ValidationError(_('Missing rationale for correct answer.'))\n else:\n raise ValidationError(_('Only one correct answer can be defined in options.'))\n\n options.append(option_dict)\n\n if correct_option is None or rationale is None:\n raise ValidationError(_('Correct answer and rationale are required and have to be defined in one of the option.'))\n\n return options, correct_option, rationale"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing the UBCPI XBlock s content XML.", "response": "def parse_seeds_xml(root):\n \"\"\"\n Parse element in the UBCPI XBlock's content XML.\n\n Args:\n root (lxml.etree.Element): The root of the node in the tree.\n\n Returns:\n a list of deserialized representation of seeds. E.g.\n [{\n 'answer': 1, # option index starting from one\n 'rationale': 'This is a seeded answer',\n },\n {....\n }]\n\n Raises:\n ValidationError: The XML definition is invalid.\n \"\"\"\n seeds = []\n\n for seed_el in root.findall('seed'):\n seed_dict = dict()\n seed_dict['rationale'] = _safe_get_text(seed_el)\n\n if 'option' in seed_el.attrib:\n seed_dict['answer'] = int(seed_el.attrib['option']) - 1\n else:\n raise ValidationError(_('Seed element must have an option attribute.'))\n\n seeds.append(seed_dict)\n\n return seeds"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the UBCPI XBlock s content from an XML definition.", "response": "def parse_from_xml(root):\n \"\"\"\n Update the UBCPI XBlock's content from an XML definition.\n\n We need to be strict about the XML we accept, to avoid setting\n the XBlock to an invalid state (which will then be persisted).\n\n Args:\n root (lxml.etree.Element): The XML definition of the XBlock's content.\n\n Returns:\n A dictionary of all of the XBlock's content.\n\n Raises:\n UpdateFromXmlError: The XML definition is invalid\n \"\"\"\n\n # Check that the root has the correct tag\n if root.tag != 'ubcpi':\n raise UpdateFromXmlError(_('Every peer instruction tool must contain an \"ubcpi\" element.'))\n\n display_name_el = root.find('display_name')\n if display_name_el is None:\n raise UpdateFromXmlError(_('Every peer instruction tool must contain a \"display_name\" element.'))\n else:\n display_name = _safe_get_text(display_name_el)\n\n rationale_size_min = int(root.attrib['rationale_size_min']) if 'rationale_size_min' in root.attrib else None\n rationale_size_max = int(root.attrib['rationale_size_max']) if 'rationale_size_max' in root.attrib else None\n\n question_el = root.find('question')\n if question_el is None:\n raise UpdateFromXmlError(_('Every peer instruction must tool contain a \"question\" element.'))\n else:\n question = parse_question_xml(question_el)\n\n options_el = root.find('options')\n if options_el is None:\n raise UpdateFromXmlError(_('Every peer instruction must tool contain a \"options\" element.'))\n else:\n options, correct_answer, correct_rationale = parse_options_xml(options_el)\n\n seeds_el = root.find('seeds')\n if seeds_el is None:\n raise UpdateFromXmlError(_('Every peer instruction must tool contain a \"seeds\" element.'))\n else:\n seeds = parse_seeds_xml(seeds_el)\n\n algo = unicode(root.attrib['algorithm']) if 'algorithm' in root.attrib else None\n num_responses = unicode(root.attrib['num_responses']) if 'num_responses' in root.attrib else None\n\n return {\n 'display_name': display_name,\n 'question_text': question,\n 'options': options,\n 'rationale_size': {'min': rationale_size_min, 'max': rationale_size_max},\n 'correct_answer': correct_answer,\n 'correct_rationale': correct_rationale,\n 'seeds': seeds,\n 'algo': {\"name\": algo, 'num_responses': num_responses}\n }"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef serialize_options(options, block):\n for index, option_dict in enumerate(block.options):\n option = etree.SubElement(options, 'option')\n # set correct option and rationale\n if index == block.correct_answer:\n option.set('correct', u'True')\n\n if hasattr(block, 'correct_rationale'):\n rationale = etree.SubElement(option, 'rationale')\n rationale.text = block.correct_rationale['text']\n\n text = etree.SubElement(option, 'text')\n text.text = option_dict.get('text', '')\n\n serialize_image(option_dict, option)", "response": "Serialize the options in peer instruction XBlock to xml\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef serialize_seeds(seeds, block):\n for seed_dict in block.seeds:\n seed = etree.SubElement(seeds, 'seed')\n # options in xml starts with 1\n seed.set('option', unicode(seed_dict.get('answer', 0) + 1))\n seed.text = seed_dict.get('rationale', '')", "response": "Serialize the seeds in peer instruction XBlock to xml\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef serialize_to_xml(root, block):\n root.tag = 'ubcpi'\n\n if block.rationale_size is not None:\n if block.rationale_size.get('min'):\n root.set('rationale_size_min', unicode(block.rationale_size.get('min')))\n if block.rationale_size.get('max'):\n root.set('rationale_size_max', unicode(block.rationale_size['max']))\n\n if block.algo:\n if block.algo.get('name'):\n root.set('algorithm', block.algo.get('name'))\n if block.algo.get('num_responses'):\n root.set('num_responses', unicode(block.algo.get('num_responses')))\n\n display_name = etree.SubElement(root, 'display_name')\n display_name.text = block.display_name\n\n question = etree.SubElement(root, 'question')\n question_text = etree.SubElement(question, 'text')\n question_text.text = block.question_text['text']\n serialize_image(block.question_text, question)\n\n options = etree.SubElement(root, 'options')\n serialize_options(options, block)\n\n seeds = etree.SubElement(root, 'seeds')\n serialize_seeds(seeds, block)", "response": "Serialize the Peer Instruction XBlock s content to XML."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nopen the lvm and vg_t handles.", "response": "def open(self):\n \"\"\"\n Obtains the lvm and vg_t handle. Usually you would never need to use this method\n unless you are doing operations using the ctypes function wrappers in conversion.py\n\n *Raises:*\n\n * HandleError\n \"\"\"\n if not self.handle:\n self.lvm.open()\n self.__vgh = lvm_vg_open(self.lvm.handle, self.name, self.mode)\n if not bool(self.__vgh):\n raise HandleError(\"Failed to initialize VG Handle.\")"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncloses the lvm and vg_t handles.", "response": "def close(self):\n \"\"\"\n Closes the lvm and vg_t handle. Usually you would never need to use this method\n unless you are doing operations using the ctypes function wrappers in conversion.py\n\n *Raises:*\n\n * HandleError\n \"\"\"\n if self.handle:\n cl = lvm_vg_close(self.handle)\n if cl != 0:\n raise HandleError(\"Failed to close VG handle after init check.\")\n self.__vgh = None\n self.lvm.close()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef uuid(self):\n self.open()\n uuid = lvm_vg_get_uuid(self.handle)\n self.close()\n return uuid", "response": "Returns the uuid of the volume group."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the number of volumes in this volume group.", "response": "def extent_count(self):\n \"\"\"\n Returns the volume group extent count.\n \"\"\"\n self.open()\n count = lvm_vg_get_extent_count(self.handle)\n self.close()\n return count"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef free_extent_count(self):\n self.open()\n count = lvm_vg_get_free_extent_count(self.handle)\n self.close()\n return count", "response": "Returns the number of free extent entries in the volume group."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pv_count(self):\n self.open()\n count = lvm_vg_get_pv_count(self.handle)\n self.close()\n return count", "response": "Returns the number of physical volumes in the volume group."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef max_pv_count(self):\n self.open()\n count = lvm_vg_get_max_pv(self.handle)\n self.close()\n return count", "response": "Returns the maximum allowed physical volume count."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef max_lv_count(self):\n self.open()\n count = lvm_vg_get_max_lv(self.handle)\n self.close()\n return count", "response": "Returns the maximum allowed logical volume count."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_clustered(self):\n self.open()\n clust = lvm_vg_is_clustered(self.handle)\n self.close()\n return bool(clust)", "response": "Returns True if the VG is clustered False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef is_exported(self):\n self.open()\n exp = lvm_vg_is_exported(self.handle)\n self.close()\n return bool(exp)", "response": "Returns True if the VG is exported False otherwise."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns True if the VG is partial False otherwise.", "response": "def is_partial(self):\n \"\"\"\n Returns True if the VG is partial, False otherwise.\n \"\"\"\n self.open()\n part = lvm_vg_is_partial(self.handle)\n self.close()\n return bool(part)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sequence(self):\n self.open()\n seq = lvm_vg_get_seqno(self.handle)\n self.close()\n return seq", "response": "Returns the sequence number of the volume group."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef size(self, units=\"MiB\"):\n self.open()\n size = lvm_vg_get_size(self.handle)\n self.close()\n return size_convert(size, units)", "response": "Returns the size of the volume group in the given units."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the free size in the given units.", "response": "def free_size(self, units=\"MiB\"):\n \"\"\"\n Returns the volume group free size in the given units. Default units are MiB.\n\n *Args:*\n\n * units (str): Unit label ('MiB', 'GiB', etc...). Default is MiB.\n \"\"\"\n self.open()\n size = lvm_vg_get_free_size(self.handle)\n self.close()\n return size_convert(size, units)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the extent size in the given units.", "response": "def extent_size(self, units=\"MiB\"):\n \"\"\"\n Returns the volume group extent size in the given units. Default units are MiB.\n\n *Args:*\n\n * units (str): Unit label ('MiB', 'GiB', etc...). Default is MiB.\n \"\"\"\n self.open()\n size = lvm_vg_get_extent_size(self.handle)\n self.close()\n return size_convert(size, units)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_pv(self, device):\n if not os.path.exists(device):\n raise ValueError(\"%s does not exist.\" % device)\n self.open()\n ext = lvm_vg_extend(self.handle, device)\n if ext != 0:\n self.close()\n raise CommitError(\"Failed to extend Volume Group.\")\n self._commit()\n self.close()\n return PhysicalVolume(self, name=device)", "response": "Adds a physical volume to the volume group."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the PhysicalVolume object associated with the given device.", "response": "def get_pv(self, device):\n \"\"\"\n Returns the physical volume associated with the given device::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\", \"w\")\n vg.get_pv(\"/dev/sdb1\")\n\n *Args:*\n\n * device (str): An existing device.\n\n *Raises:*\n\n * ValueError, HandleError\n \"\"\"\n if not os.path.exists(device):\n raise ValueError(\"%s does not exist.\" % device)\n return PhysicalVolume(self, name=device)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remove_pv(self, pv):\n name = pv.name\n self.open()\n rm = lvm_vg_reduce(self.handle, name)\n if rm != 0:\n self.close()\n raise CommitError(\"Failed to remove %s.\" % name)\n self._commit()\n self.close()", "response": "Removes a physical volume from the volume group."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprobes the volume group for physical volumes and returns a list of PhysicalVolume instances.", "response": "def pvscan(self):\n \"\"\"\n Probes the volume group for physical volumes and returns a list of\n PhysicalVolume instances::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\")\n pvs = vg.pvscan()\n\n *Raises:*\n\n * HandleError\n \"\"\"\n self.open()\n pv_list = []\n pv_handles = lvm_vg_list_pvs(self.handle)\n if not bool(pv_handles):\n return pv_list\n pvh = dm_list_first(pv_handles)\n while pvh:\n c = cast(pvh, POINTER(lvm_pv_list))\n pv = PhysicalVolume(self, pvh=c.contents.pv)\n pv_list.append(pv)\n if dm_list_end(pv_handles, pvh):\n # end of linked list\n break\n pvh = dm_list_next(pv_handles, pvh)\n self.close()\n return pv_list"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef lvscan(self):\n self.open()\n lv_list = []\n lv_handles = lvm_vg_list_lvs(self.handle)\n if not bool(lv_handles):\n return lv_list\n lvh = dm_list_first(lv_handles)\n while lvh:\n c = cast(lvh, POINTER(lvm_lv_list))\n lv = LogicalVolume(self, lvh=c.contents.lv)\n lv_list.append(lv)\n if dm_list_end(lv_handles, lvh):\n # end of linked list\n break\n lvh = dm_list_next(lv_handles, lvh)\n self.close()\n return lv_list", "response": "Probes the volume group for logical volumes and returns a list of LogicalVolume instances."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_lv(self, name, length, units):\n if units != \"%\":\n size = size_units[units] * length\n else:\n if not (0 < length <= 100) or type(length) is float:\n raise ValueError(\"Length not supported.\")\n size = (self.size(\"B\") / 100) * length\n self.open()\n lvh = lvm_vg_create_lv_linear(self.handle, name, c_ulonglong(size))\n if not bool(lvh):\n self.close()\n raise CommitError(\"Failed to create LV.\")\n lv = LogicalVolume(self, lvh=lvh)\n self.close()\n return lv", "response": "Creates a new logical volume and returns the LogicalVolume instance associated with the given logical volume."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove a logical volume from the volume group.", "response": "def remove_lv(self, lv):\n \"\"\"\n Removes a logical volume from the volume group::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\", \"w\")\n lv = vg.lvscan()[0]\n vg.remove_lv(lv)\n\n *Args:*\n\n * lv (obj): A LogicalVolume instance.\n\n *Raises:*\n\n * HandleError, CommitError, ValueError\n\n .. note::\n\n The VolumeGroup instance must be in write mode, otherwise CommitError\n is raised.\n \"\"\"\n lv.open()\n rm = lvm_vg_remove_lv(lv.handle)\n lv.close()\n if rm != 0:\n raise CommitError(\"Failed to remove LV.\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving all logical volumes from the volume group.", "response": "def remove_all_lvs(self):\n \"\"\"\n Removes all logical volumes from the volume group.\n\n *Raises:*\n\n * HandleError, CommitError\n \"\"\"\n lvs = self.lvscan()\n for lv in lvs:\n self.remove_lv(lv)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset the volume group extent size in the given units.", "response": "def set_extent_size(self, length, units):\n \"\"\"\n Sets the volume group extent size in the given units::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\", \"w\")\n vg.set_extent_size(2, \"MiB\")\n\n *Args:*\n\n * length (int): The desired length size.\n * units (str): The desired units (\"MiB\", \"GiB\", etc...).\n\n *Raises:*\n\n * HandleError, CommitError, KeyError\n\n .. note::\n\n The VolumeGroup instance must be in write mode, otherwise CommitError\n is raised.\n \"\"\"\n size = length * size_units[units]\n self.open()\n ext = lvm_vg_set_extent_size(self.handle, c_ulong(size))\n self._commit()\n self.close()\n if ext != 0:\n raise CommitError(\"Failed to set extent size.\")"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the value for a pair of terms.", "response": "def set_pair(self, term1, term2, value, **kwargs):\n\n \"\"\"\n Set the value for a pair of terms.\n\n Args:\n term1 (str)\n term2 (str)\n value (mixed)\n \"\"\"\n\n key = self.key(term1, term2)\n self.keys.update([term1, term2])\n self.pairs[key] = value"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_pair(self, term1, term2):\n\n \"\"\"\n Get the value for a pair of terms.\n\n Args:\n term1 (str)\n term2 (str)\n\n Returns:\n The stored value.\n \"\"\"\n\n key = self.key(term1, term2)\n return self.pairs.get(key, None)", "response": "Get the value for a pair of terms."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef index(self, text, terms=None, **kwargs):\n\n \"\"\"\n Index all term pair distances.\n\n Args:\n text (Text): The source text.\n terms (list): Terms to index.\n \"\"\"\n\n self.clear()\n\n # By default, use all terms.\n terms = terms or text.terms.keys()\n\n pairs = combinations(terms, 2)\n count = comb(len(terms), 2)\n\n for t1, t2 in bar(pairs, expected_size=count, every=1000):\n\n # Set the Bray-Curtis distance.\n score = text.score_braycurtis(t1, t2, **kwargs)\n self.set_pair(t1, t2, score)", "response": "Index all term pair distances."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef anchored_pairs(self, anchor):\n\n \"\"\"\n Get distances between an anchor term and all other terms.\n\n Args:\n anchor (str): The anchor term.\n\n Returns:\n OrderedDict: The distances, in descending order.\n \"\"\"\n\n pairs = OrderedDict()\n\n for term in self.keys:\n score = self.get_pair(anchor, term)\n if score: pairs[term] = score\n\n return utils.sort_dict(pairs)", "response": "Returns an OrderedDict of anchored term names to their score."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef from_rgb(r, g=None, b=None):\n c = r if isinstance(r, list) else [r, g, b]\n best = {}\n\n for index, item in enumerate(colors):\n d = __distance(item, c)\n if(not best or d <= best['distance']):\n best = {'distance': d, 'index': index}\n\n if 'index' in best:\n return best['index']\n else:\n return 1", "response": "Return nearest xterm 256 color code from rgb input."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses command line arguments and run utilities.", "response": "def entry():\n \"\"\"Parse command line arguments and run utilities.\"\"\"\n parser = argparse.ArgumentParser()\n parser.add_argument(\n 'action', help='Action to take',\n choices=['from_hex', 'to_rgb', 'to_hex'],\n )\n parser.add_argument(\n 'value', help='Value for the action',\n )\n parsed = parser.parse_args()\n if parsed.action != \"from_hex\":\n try:\n parsed.value = int(parsed.value)\n except ValueError:\n raise argparse.ArgumentError(\n \"Value for this action should be an integer\",\n )\n print(globals()[parsed.action](parsed.value))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef makeSoftwareVersion(store, version, systemVersion):\n return store.findOrCreate(SoftwareVersion,\n systemVersion=systemVersion,\n package=unicode(version.package),\n version=unicode(version.short()),\n major=version.major,\n minor=version.minor,\n micro=version.micro)", "response": "Creates a SoftwareVersion object from store corresponding to the specified version object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef listVersionHistory(store):\n q = store.query(SystemVersion, sort=SystemVersion.creation.descending)\n return [sv.longWindedRepr() for sv in q]", "response": "List the software package version history of store."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if the current version is different from the previously recorded version.", "response": "def checkSystemVersion(s, versions=None):\n \"\"\"\n Check if the current version is different from the previously recorded\n version. If it is, or if there is no previously recorded version,\n create a version matching the current config.\n \"\"\"\n\n if versions is None:\n versions = getSystemVersions()\n\n currentVersionMap = dict([(v.package, v) for v in versions])\n mostRecentSystemVersion = s.findFirst(SystemVersion,\n sort=SystemVersion.creation.descending)\n mostRecentVersionMap = dict([(v.package, v.asVersion()) for v in\n s.query(SoftwareVersion,\n (SoftwareVersion.systemVersion ==\n mostRecentSystemVersion))])\n\n if mostRecentVersionMap != currentVersionMap:\n currentSystemVersion = SystemVersion(store=s, creation=Time())\n for v in currentVersionMap.itervalues():\n makeSoftwareVersion(s, v, currentSystemVersion)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting the version data in this item to a version object.", "response": "def asVersion(self):\n \"\"\"\n Convert the version data in this item to a\n L{twisted.python.versions.Version}.\n \"\"\"\n return versions.Version(self.package, self.major, self.minor, self.micro)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef reset(self):\n self.colNames,self.colDesc,self.colUnits,self.colComments,\\\n self.colTypes,self.colData=[],[],[],[],[],[]", "response": "clears all columns in the current object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef colAdd(self,name=\"\",desc=\"\",unit=\"\",comment=\"\",coltype=0,data=[],pos=None):\n if pos is None:\n pos=len(self.colNames)\n self.colNames.insert(pos,name)\n self.colDesc.insert(pos,desc)\n self.colUnits.insert(pos,unit)\n self.colComments.insert(pos,comment)\n self.colTypes.insert(pos,coltype)\n self.colData.insert(pos,data)\n return", "response": "Add a new column to the log."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a column at a single index. Negative numbers count from the end.", "response": "def colDelete(self,colI=-1):\n \"\"\"delete a column at a single index. Negative numbers count from the end.\"\"\"\n# print(\"DELETING COLUMN: [%d] %s\"%(colI,self.colDesc[colI]))\n self.colNames.pop(colI)\n self.colDesc.pop(colI)\n self.colUnits.pop(colI)\n self.colComments.pop(colI)\n self.colTypes.pop(colI)\n self.colData.pop(colI)\n return"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef onex(self):\n xCols=[i for i in range(self.nCols) if self.colTypes[i]==3]\n if len(xCols)>1:\n for colI in xCols[1:][::-1]:\n self.colDelete(colI)", "response": "delete all X columns except the first one."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef alignXY(self):\n\n # figure out what data we have and will align to\n xVals=[]\n xCols=[x for x in range(self.nCols) if self.colTypes[x]==3]\n yCols=[x for x in range(self.nCols) if self.colTypes[x]==0]\n xCols,yCols=np.array(xCols),np.array(yCols)\n for xCol in xCols:\n xVals.extend(self.colData[xCol])\n #xVals=list(np.round(set(xVals),5))\n xVals=list(sorted(list(set(xVals))))\n\n # prepare our new aligned dataset\n newData=np.empty(len(xVals)*self.nCols)\n newData[:]=np.nan\n newData=newData.reshape(len(xVals),self.nCols)\n oldData=np.round(self.data,5)\n\n # do the alignment\n for xCol in xCols:\n columnsToShift=[xCol]\n for col in range(xCol+1,self.nCols):\n if self.colTypes[col]==0:\n columnsToShift.append(col)\n else:\n break\n # determine how to move each row\n for row in range(len(oldData)):\n oldXvalue=oldData[row,xCol]\n if oldXvalue in xVals:\n newRow=xVals.index(oldXvalue)\n newData[newRow,columnsToShift]=oldData[row,columnsToShift]\n\n # commit changes\n newData[:,0]=xVals\n self.data=newData\n self.onex()", "response": "aligns XY pairs or XYYY etc."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef wiggle(self,noiseLevel=.1):\n noise=(np.random.rand(*self.data.shape))-.5\n self.data=self.data+noise*noiseLevel", "response": "Slightly changes value of every cell in the worksheet. Used for testing."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\npull data into this OR. SHEET from a real book or a real sheet", "response": "def pull(self,bookName=None,sheetName=None):\n \"\"\"pull data into this OR.SHEET from a real book/sheet in Origin\"\"\"\n\n # tons of validation\n if bookName is None and self.bookName: bookName=self.bookName\n if sheetName is None and self.sheetName: sheetName=self.sheetName\n if bookName is None: bookName=OR.activeBook()\n if bookName and sheetName is None: sheetName=OR.activeSheet()\n if not bookName or not sheetName:\n print(\"can't figure out where to pull from! [%s]%s\"%(bookName,sheetName))\n return\n\n # finally doing the thing\n poSheet=OR.getSheet(bookName,sheetName)\n self.bookName=bookName\n self.sheetName=sheetName\n self.desc=poSheet.GetLongName()\n self.colNames=[poCol.GetName() for poCol in poSheet.Columns()]\n self.colDesc=[poCol.GetLongName() for poCol in poSheet.Columns()]\n self.colUnits=[poCol.GetUnits() for poCol in poSheet.Columns()]\n self.colComments=[poCol.GetComments() for poCol in poSheet.Columns()]\n self.colTypes=[poCol.GetType() for poCol in poSheet.Columns()]\n self.colData=[poCol.GetData() for poCol in poSheet.Columns()]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef push(self,bookName=None,sheetName=None,overwrite=False):\n # tons of validation\n if bookName: self.bookName=bookName\n if sheetName: self.sheetName=sheetName\n if not self.sheetName in OR.sheetNames(bookName):\n print(\"can't find [%s]%s!\"%(bookName,sheetName))\n return\n\n # clear out out sheet by deleting EVERY column\n poSheet=OR.getSheet(bookName,sheetName) # CPyWorksheetPageI\n if not poSheet:\n print(\"WARNING: didn't get posheet\",poSheet,bookName,sheetName)\n for poCol in [x for x in poSheet if x.IsValid()]:\n poCol.Destroy()\n\n # create columns and assign properties to each\n for i in range(len(self.colNames)):\n poSheet.InsertCol(i,self.colNames[i])\n poSheet.Columns(i).SetName(self.colNames[i])\n poSheet.Columns(i).SetLongName(self.colDesc[i])\n poSheet.Columns(i).SetUnits(self.colUnits[i])\n poSheet.Columns(i).SetComments(self.colComments[i])\n poSheet.Columns(i).SetType(self.colTypes[i])\n poSheet.Columns(i).SetData(self.colData[i])", "response": "pull this OR. SHEET into a real book or sheet"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef nRows(self):\n if self.nCols: return max([len(x) for x in self.colData])\n else: return 0", "response": "returns maximum number of rows based on the longest colData"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning all of colData as a 2D numpy array.", "response": "def data(self):\n \"\"\"return all of colData as a 2D numpy array.\"\"\"\n data=np.empty((self.nRows,self.nCols),dtype=np.float)\n data[:]=np.nan # make everything nan by default\n for colNum,colData in enumerate(self.colData):\n validIs=np.where([np.isreal(v) for v in colData])[0]\n validData=np.ones(len(colData))*np.nan\n validData[validIs]=np.array(colData)[validIs]\n data[:len(colData),colNum]=validData # only fill cells that have data\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef data(self,data):\n assert type(data) is np.ndarray\n assert data.shape[1] == self.nCols\n for i in range(self.nCols):\n self.colData[i]=data[:,i].tolist()", "response": "Given a 2D numpy array fill colData with it."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nchange style on focus out events.", "response": "def focusout(self, event):\n \"\"\"Change style on focus out events.\"\"\"\n bc = self.style.lookup(\"TEntry\", \"bordercolor\", (\"!focus\",))\n dc = self.style.lookup(\"TEntry\", \"darkcolor\", (\"!focus\",))\n lc = self.style.lookup(\"TEntry\", \"lightcolor\", (\"!focus\",))\n self.style.configure(\"%s.spinbox.TFrame\" % self.frame, bordercolor=bc,\n darkcolor=dc, lightcolor=lc)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nchange style on focus in events.", "response": "def focusin(self, event):\n \"\"\"Change style on focus in events.\"\"\"\n self.old_value = self.get()\n bc = self.style.lookup(\"TEntry\", \"bordercolor\", (\"focus\",))\n dc = self.style.lookup(\"TEntry\", \"darkcolor\", (\"focus\",))\n lc = self.style.lookup(\"TEntry\", \"lightcolor\", (\"focus\",))\n self.style.configure(\"%s.spinbox.TFrame\" % self.frame, bordercolor=bc,\n darkcolor=dc, lightcolor=lc)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef open(self):\n if not self.handle:\n try:\n path = self.system_dir\n except AttributeError:\n path = ''\n self.__handle = lvm_init(path)\n if not bool(self.__handle):\n raise HandleError(\"Failed to initialize LVM handle.\")", "response": "Opens the lvm handle."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef close(self):\n if self.handle:\n q = lvm_quit(self.handle)\n if q != 0:\n raise HandleError(\"Failed to close LVM handle.\")\n self.__handle = None", "response": "Closes the lvm handle."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning an instance of a specific existing volume group.", "response": "def get_vg(self, name, mode=\"r\"):\n \"\"\"\n Returns an instance of VolumeGroup. The name parameter should be an existing\n volume group. By default, all volume groups are open in \"read\" mode::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\")\n\n To open a volume group with write permissions set the mode parameter to \"w\"::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\", \"w\")\n\n *Args:*\n\n * name (str): An existing volume group name.\n * mode (str): \"r\" or \"w\" for read/write respectively. Default is \"r\".\n\n *Raises:*\n\n * HandleError\n \"\"\"\n vg = VolumeGroup(self, name=name, mode=mode)\n return vg"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_vg(self, name, devices):\n self.open()\n vgh = lvm_vg_create(self.handle, name)\n if not bool(vgh):\n self.close()\n raise HandleError(\"Failed to create VG.\")\n for device in devices:\n if not os.path.exists(device):\n self._destroy_vg(vgh)\n raise ValueError(\"%s does not exist.\" % device)\n ext = lvm_vg_extend(vgh, device)\n if ext != 0:\n self._destroy_vg(vgh)\n raise CommitError(\"Failed to extend Volume Group.\")\n try:\n self._commit_vg(vgh)\n except CommitError:\n self._destroy_vg(vgh)\n raise CommitError(\"Failed to add %s to VolumeGroup.\" % device)\n self._close_vg(vgh)\n vg = VolumeGroup(self, name)\n return vg", "response": "Create a new volume group with the given name and added physycal\n devices."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves a volume group from the disk.", "response": "def remove_vg(self, vg):\n \"\"\"\n Removes a volume group::\n\n from lvm2py import *\n\n lvm = LVM()\n vg = lvm.get_vg(\"myvg\", \"w\")\n lvm.remove_vg(vg)\n\n *Args:*\n\n * vg (obj): A VolumeGroup instance.\n\n *Raises:*\n\n * HandleError, CommitError\n\n .. note::\n\n The VolumeGroup instance must be in write mode, otherwise CommitError\n is raised.\n \"\"\"\n vg.open()\n rm = lvm_vg_remove(vg.handle)\n if rm != 0:\n vg.close()\n raise CommitError(\"Failed to remove VG.\")\n com = lvm_vg_write(vg.handle)\n if com != 0:\n vg.close()\n raise CommitError(\"Failed to commit changes to disk.\")\n vg.close()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of VolumeGroup objects for all the volumes in the system.", "response": "def vgscan(self):\n \"\"\"\n Probes the system for volume groups and returns a list of VolumeGroup\n instances::\n\n from lvm2py import *\n\n lvm = LVM()\n vgs = lvm.vgscan()\n\n *Raises:*\n\n * HandleError\n \"\"\"\n vg_list = []\n self.open()\n names = lvm_list_vg_names(self.handle)\n if not bool(names):\n return vg_list\n vgnames = []\n vg = dm_list_first(names)\n while vg:\n c = cast(vg, POINTER(lvm_str_list))\n vgnames.append(c.contents.str)\n if dm_list_end(names, vg):\n # end of linked list\n break\n vg = dm_list_next(names, vg)\n self.close()\n for name in vgnames:\n vginst = self.get_vg(name)\n vg_list.append(vginst)\n return vg_list"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a text from a file.", "response": "def from_file(cls, path):\n\n \"\"\"\n Create a text from a file.\n\n Args:\n path (str): The file path.\n \"\"\"\n\n with open(path, 'r', errors='replace') as f:\n return cls(f.read())"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef load_stopwords(self, path):\n\n \"\"\"\n Load a set of stopwords.\n\n Args:\n path (str): The stopwords file path.\n \"\"\"\n\n if path:\n with open(path) as f:\n self.stopwords = set(f.read().splitlines())\n\n else:\n self.stopwords = set(\n pkgutil\n .get_data('textplot', 'data/stopwords.txt')\n .decode('utf8')\n .splitlines()\n )", "response": "Loads a set of stopwords from a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning an OrderedDict of term counts.", "response": "def term_counts(self):\n\n \"\"\"\n Returns:\n OrderedDict: An ordered dictionary of term counts.\n \"\"\"\n\n counts = OrderedDict()\n for term in self.terms:\n counts[term] = len(self.terms[term])\n\n return utils.sort_dict(counts)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a dictionary that maps occurrence counts to the terms that have that many times in the text.", "response": "def term_count_buckets(self):\n\n \"\"\"\n Returns:\n dict: A dictionary that maps occurrence counts to the terms that\n appear that many times in the text.\n \"\"\"\n\n buckets = {}\n for term, count in self.term_counts().items():\n if count in buckets: buckets[count].append(term)\n else: buckets[count] = [term]\n\n return buckets"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the X most frequent terms in the text and then probe down to get .", "response": "def most_frequent_terms(self, depth):\n\n \"\"\"\n Get the X most frequent terms in the text, and then probe down to get\n any other terms that have the same count as the last term.\n\n Args:\n depth (int): The number of terms.\n\n Returns:\n set: The set of frequent terms.\n \"\"\"\n\n counts = self.term_counts()\n\n # Get the top X terms and the instance count of the last word.\n top_terms = set(list(counts.keys())[:depth])\n end_count = list(counts.values())[:depth][-1]\n\n # Merge in all other words with that appear that number of times, so\n # that we don't truncate the last bucket - eg, half of the words that\n # appear 5 times, but not the other half.\n\n bucket = self.term_count_buckets()[end_count]\n return top_terms.union(set(bucket))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the most common unstemmed variant of a term.", "response": "def unstem(self, term):\n\n \"\"\"\n Given a stemmed term, get the most common unstemmed variant.\n\n Args:\n term (str): A stemmed term.\n\n Returns:\n str: The unstemmed token.\n \"\"\"\n\n originals = []\n for i in self.terms[term]:\n originals.append(self.tokens[i]['unstemmed'])\n\n mode = Counter(originals).most_common(1)\n return mode[0][0]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nestimating the kernel density of the instances of term in the text.", "response": "def kde(self, term, bandwidth=2000, samples=1000, kernel='gaussian'):\n\n \"\"\"\n Estimate the kernel density of the instances of term in the text.\n\n Args:\n term (str): A stemmed term.\n bandwidth (int): The kernel bandwidth.\n samples (int): The number of evenly-spaced sample points.\n kernel (str): The kernel function.\n\n Returns:\n np.array: The density estimate.\n \"\"\"\n\n # Get the offsets of the term instances.\n terms = np.array(self.terms[term])[:, np.newaxis]\n\n # Fit the density estimator on the terms.\n kde = KernelDensity(kernel=kernel, bandwidth=bandwidth).fit(terms)\n\n # Score an evely-spaced array of samples.\n x_axis = np.linspace(0, len(self.tokens), samples)[:, np.newaxis]\n scores = kde.score_samples(x_axis)\n\n # Scale the scores to integrate to 1.\n return np.exp(scores) * (len(self.tokens) / samples)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompute the geometric area of the overlap between the kernel density of two terms.", "response": "def score_intersect(self, term1, term2, **kwargs):\n\n \"\"\"\n Compute the geometric area of the overlap between the kernel density\n estimates of two terms.\n\n Args:\n term1 (str)\n term2 (str)\n\n Returns: float\n \"\"\"\n\n t1_kde = self.kde(term1, **kwargs)\n t2_kde = self.kde(term2, **kwargs)\n\n # Integrate the overlap.\n overlap = np.minimum(t1_kde, t2_kde)\n return np.trapz(overlap)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef score_cosine(self, term1, term2, **kwargs):\n\n \"\"\"\n Compute a weighting score based on the cosine distance between the\n kernel density estimates of two terms.\n\n Args:\n term1 (str)\n term2 (str)\n\n Returns: float\n \"\"\"\n\n t1_kde = self.kde(term1, **kwargs)\n t2_kde = self.kde(term2, **kwargs)\n\n return 1-distance.cosine(t1_kde, t2_kde)", "response": "Compute a weighting score based on the cosine distance between the kernel density estimates of two terms."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef score_braycurtis(self, term1, term2, **kwargs):\n\n \"\"\"\n Compute a weighting score based on the \"City Block\" distance between\n the kernel density estimates of two terms.\n\n Args:\n term1 (str)\n term2 (str)\n\n Returns: float\n \"\"\"\n\n t1_kde = self.kde(term1, **kwargs)\n t2_kde = self.kde(term2, **kwargs)\n\n return 1-distance.braycurtis(t1_kde, t2_kde)", "response": "Compute a weighting score based on the kernel density estimates of two terms."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef plot_term_kdes(self, words, **kwargs):\n\n \"\"\"\n Plot kernel density estimates for multiple words.\n\n Args:\n words (list): A list of unstemmed terms.\n \"\"\"\n\n stem = PorterStemmer().stem\n\n for word in words:\n kde = self.kde(stem(word), **kwargs)\n plt.plot(kde)\n\n plt.show()", "response": "Plot kernel density estimates for multiple words."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntrying to generate a new formfield from the given field - classname and returns the corresponding object.", "response": "def generate(self, field_name, field):\n \"\"\"Tries to lookup a matching formfield generator (lowercase\n field-classname) and raises a NotImplementedError of no generator\n can be found.\n \"\"\"\n\n if hasattr(self, 'generate_%s' % field.__class__.__name__.lower()):\n generator = getattr(\n self,\n 'generate_%s' % field.__class__.__name__.lower())\n return generator(\n field_name,\n field,\n (field.verbose_name or field_name).capitalize())\n else:\n raise NotImplementedError('%s is not supported by MongoForm' % \\\n field.__class__.__name__)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _fixIndex(self, index, truncate=False):\n assert not isinstance(index, slice), 'slices are not supported (yet)'\n if index < 0:\n index += self.length\n if index < 0:\n if not truncate:\n raise IndexError('stored List index out of range')\n else:\n index = 0\n return index", "response": "Fixes the index of the internal list."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef overlapping(startAttribute, # X\n endAttribute, # Y\n startValue, # A\n endValue, # B\n ):\n \"\"\"\n Return an L{axiom.iaxiom.IComparison} (an object that can be passed as the\n 'comparison' argument to Store.query/.sum/.count) which will constrain a\n query against 2 attributes for ranges which overlap with the given\n arguments.\n\n For a database with Items of class O which represent values in this\n configuration::\n\n X Y\n (a) (b)\n |-------------------|\n (c) (d)\n |--------| (e) (f)\n |--------|\n\n (g) (h)\n |---| (i) (j)\n |------|\n\n (k) (l)\n |-------------------------------------|\n\n (a) (l)\n |-----------------------------|\n (c) (b)\n |------------------------|\n\n (c) (a)\n |----|\n (b) (l)\n |---------|\n\n The query::\n\n myStore.query(\n O,\n findOverlapping(O.X, O.Y,\n a, b))\n\n Will return a generator of Items of class O which represent segments a-b,\n c-d, e-f, k-l, a-l, c-b, c-a and b-l, but NOT segments g-h or i-j.\n\n (NOTE: If you want to pass attributes of different classes for\n startAttribute and endAttribute, read the implementation of this method to\n discover the additional join clauses required. This may be eliminated some\n day so for now, consider this method undefined over multiple classes.)\n\n In the database where this query is run, for an item N, all values of\n N.startAttribute must be less than N.endAttribute.\n\n startValue must be less than endValue.\n \"\"\"\n assert startValue <= endValue\n\n return OR(\n AND(startAttribute >= startValue,\n startAttribute <= endValue),\n AND(endAttribute >= startValue,\n endAttribute <= endValue),\n AND(startAttribute <= startValue,\n endAttribute >= endValue)\n )", "response": "Returns an iterator of items that overlap with the given start and end attributes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompare two in - database tuples. Useful when sorting by a compound keysequence.", "response": "def _tupleCompare(tuple1, ineq, tuple2,\n eq=lambda a,b: (a==b),\n ander=AND,\n orer=OR):\n \"\"\"\n Compare two 'in-database tuples'. Useful when sorting by a compound key\n and slicing into the middle of that query.\n \"\"\"\n\n orholder = []\n for limit in range(len(tuple1)):\n eqconstraint = [\n eq(elem1, elem2) for elem1, elem2 in zip(tuple1, tuple2)[:limit]]\n ineqconstraint = ineq(tuple1[limit], tuple2[limit])\n orholder.append(ander(*(eqconstraint + [ineqconstraint])))\n return orer(*orholder)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef truncate_rationale(rationale, max_length=MAX_RATIONALE_SIZE_IN_EVENT):\n if isinstance(rationale, basestring) and max_length is not None and len(rationale) > max_length:\n return rationale[0:max_length], True\n else:\n return rationale, False", "response": "Truncates the rationale for analytics event emission if necessary"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_options(options):\n errors = []\n\n if int(options['rationale_size']['min']) < 1:\n errors.append(_('Minimum Characters'))\n if int(options['rationale_size']['max']) < 0 or int(options['rationale_size']['max']) > MAX_RATIONALE_SIZE:\n errors.append(_('Maximum Characters'))\n if not any(error in [_('Minimum Characters'), _('Maximum Characters')] for error in errors) \\\n and int(options['rationale_size']['max']) <= int(options['rationale_size']['min']):\n errors += [_('Minimum Characters'), _('Maximum Characters')]\n try:\n if options['algo']['num_responses'] != '#' and int(options['algo']['num_responses']) < 0:\n errors.append(_('Number of Responses'))\n except ValueError:\n errors.append(_('Not an Integer'))\n\n if not errors:\n return None\n else:\n return {'options_error': _('Invalid Option(s): ') + ', '.join(errors)}", "response": "Validate the options that course author set up and return errors in a dict if there is any error"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a student item_dict from our surrounding context.", "response": "def get_student_item_dict(self, anonymous_user_id=None):\n \"\"\"Create a student_item_dict from our surrounding context.\n\n See also: submissions.api for details.\n\n Args:\n anonymous_user_id(str): A unique anonymous_user_id for (user, course) pair.\n Returns:\n (dict): The student item associated with this XBlock instance. This\n includes the student id, item id, and course id.\n \"\"\"\n\n item_id = self._serialize_opaque_key(self.scope_ids.usage_id)\n\n # This is not the real way course_ids should work, but this is a\n # temporary expediency for LMS integration\n if hasattr(self, \"xmodule_runtime\"):\n course_id = self.get_course_id() # pylint:disable=E1101\n\n if anonymous_user_id:\n student_id = anonymous_user_id\n else:\n student_id = self.xmodule_runtime.anonymous_student_id # pylint:disable=E1101\n else:\n course_id = \"edX/Enchantment_101/April_1\"\n if self.scope_ids.user_id is None:\n student_id = ''\n else:\n student_id = unicode(self.scope_ids.user_id)\n\n student_item_dict = dict(\n student_id=student_id,\n item_id=item_id,\n course_id=course_id,\n item_type='ubcpi'\n )\n return student_item_dict"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nretrieves the answers for a student and question.", "response": "def get_answers_for_student(student_item):\n \"\"\"\n Retrieve answers from backend for a student and question\n\n Args:\n student_item (dict): The location of the problem this submission is\n associated with, as defined by a course, student, and item.\n\n Returns:\n Answers: answers for the student\n \"\"\"\n submissions = sub_api.get_submissions(student_item)\n if not submissions:\n return Answers()\n\n latest_submission = submissions[0]\n latest_answer_item = latest_submission.get('answer', {})\n return Answers(latest_answer_item.get(ANSWER_LIST_KEY, []))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_answer_for_student(student_item, vote, rationale):\n answers = get_answers_for_student(student_item)\n answers.add_answer(vote, rationale)\n\n sub_api.create_submission(student_item, {\n ANSWER_LIST_KEY: answers.get_answers_as_list()\n })", "response": "Adds an answer for a student to the backend\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _safe_get(self, revision, key):\n if self.has_revision(revision):\n return self.raw_answers[revision].get(key)\n else:\n return None", "response": "Get an answer data by revision"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_answer(self, vote, rationale):\n self.raw_answers.append({\n VOTE_KEY: vote,\n RATIONALE_KEY: rationale,\n })", "response": "Add an answer to the list of answers for the current node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef exceptionToString(e,silent=False):\n exc_type, exc_obj, exc_tb = sys.exc_info()\n s=(\"\\n\"+\"=\"*50+\"\\n\")\n s+=\"EXCEPTION THROWN UNEXPECTEDLY\\n\"\n s+=\" FILE: %s\\n\"%os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]\n s+=\" LINE: %s\\n\"%exc_tb.tb_lineno\n s+=\" TYPE: %s\\n\"%exc_type\n s+='-'*50+'\\n'\n s+=traceback.format_exc()\n s=s.strip()+'\\n'+\"=\"*50+\"\\n\"\n if not silent:\n print(s)\n return s", "response": "when you except Exception as e give me the e and I ll give you a string."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef createNew(cls, store, pathSegments):\n if isinstance(pathSegments, basestring):\n raise ValueError(\n 'Received %r instead of a sequence' % (pathSegments,))\n if store.dbdir is None:\n self = cls(store=store, storepath=None)\n else:\n storepath = store.newDirectory(*pathSegments)\n self = cls(store=store, storepath=storepath)\n self.open()\n self.close()\n return self", "response": "Create a new SubStore allocating a new file space for it."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef createStore(self, debug, journalMode=None):\n if self.storepath is None:\n self.store._memorySubstores.append(self) # don't fall out of cache\n if self.store.filesdir is None:\n filesdir = None\n else:\n filesdir = (self.store.filesdir.child(\"_substore_files\")\n .child(str(self.storeID))\n .path)\n return Store(parent=self.store,\n filesdir=filesdir,\n idInParent=self.storeID,\n debug=debug,\n journalMode=journalMode)\n else:\n return Store(self.storepath.path,\n parent=self.store,\n idInParent=self.storeID,\n debug=debug,\n journalMode=journalMode)", "response": "Create the actual Store this Substore represents."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupgrading the catalog 1 to 2.", "response": "def upgradeCatalog1to2(oldCatalog):\n \"\"\"\n Create _TagName instances which version 2 of Catalog automatically creates\n for use in determining the tagNames result, but which version 1 of Catalog\n did not create.\n \"\"\"\n newCatalog = oldCatalog.upgradeVersion('tag_catalog', 1, 2,\n tagCount=oldCatalog.tagCount)\n tags = newCatalog.store.query(Tag, Tag.catalog == newCatalog)\n tagNames = tags.getColumn(\"name\").distinct()\n for t in tagNames:\n _TagName(store=newCatalog.store, catalog=newCatalog, name=t)\n return newCatalog"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning an iterator of unicode strings - the unique tag names which have been applied objects in this catalog.", "response": "def tagNames(self):\n \"\"\"\n Return an iterator of unicode strings - the unique tag names which have\n been applied objects in this catalog.\n \"\"\"\n return self.store.query(_TagName, _TagName.catalog == self).getColumn(\"name\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning an iterator of unicode strings - the tag names which apply to the given object.", "response": "def tagsOf(self, obj):\n \"\"\"\n Return an iterator of unicode strings - the tag names which apply to\n the given object.\n \"\"\"\n return self.store.query(\n Tag,\n AND(Tag.catalog == self,\n Tag.object == obj)).getColumn(\"name\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef loaded(self, oself, dbval):\n setattr(oself, self.dbunderlying, dbval)\n delattr(oself, self.underlying)", "response": "This method is invoked when the item is loaded from the database and the item s database value is retrieved."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _convertPyval(self, oself, pyval):\n # convert to dbval later, I guess?\n if pyval is None and not self.allowNone:\n raise TypeError(\"attribute [%s.%s = %s()] must not be None\" % (\n self.classname, self.attrname, self.__class__.__name__))\n\n return self.infilter(pyval, oself, oself.store)", "response": "Convert a Python value to a value suitable for inserting into the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _queryContainer(self, store):\n if self._subselectSQL is None:\n sql, args = self.container._sqlAndArgs('SELECT',\n self.container._queryTarget)\n self._subselectSQL, self._subselectArgs = sql, args\n return self._subselectSQL", "response": "Generate and cache the subselect SQL and its arguments."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a sequence of the items in the container.", "response": "def _sequenceContainer(self, store):\n \"\"\"\n Smash whatever we got into a list and save the result in case we are\n executed multiple times. This keeps us from tripping up over\n generators and the like.\n \"\"\"\n if self._sequence is None:\n self._sequence = list(self.container)\n self._clause = ', '.join(['?'] * len(self._sequence))\n return self._clause"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the list of arguments to filter the data using the attribute type being tested for containment and hand back the resulting list.", "response": "def _sequenceArgs(self, store):\n \"\"\"\n Filter each element of the data using the attribute type being\n tested for containment and hand back the resulting list.\n \"\"\"\n self._sequenceContainer(store) # Force _sequence to be valid\n return [self.attribute.infilter(pyval, None, store) for pyval in self._sequence]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef prepareInsert(self, oself, store):\n if self.relative:\n fspath = self.__get__(oself)\n oself.__dirty__[self.attrname] = self, self.infilter(fspath, oself, store)", "response": "Prepare for insertion into the database by making the dbunderlying\n attribute of the item a relative pathname with respect to the store\n attribute."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef full_restore(self, using=None):\n using = using or router.db_for_write(self.__class__, instance=self)\n restore_counter = Counter()\n if self.deleted:\n time = self.deleted_at\n else:\n return restore_counter\n self.collector = models.deletion.Collector(using=using)\n self.collector.collect([self])\n \n\n for model, instances in self.collector.data.items():\n instances_to_delete = sorted(instances, key=attrgetter(\"pk\"))\n\n self.sort()\n\n for qs in self.collector.fast_deletes:\n # TODO make sure the queryset delete has been made a soft delete\n for qs_instance in qs:\n restore_counter.update([qs_instance._meta.model_name])\n qs_instance.restore(time=time)\n\n for model, instances in self.collector.data.items():\n for instance in instances:\n restore_counter.update([instance._meta.model_name])\n instance.restore(time=time)\n\n return sum(restore_counter.values()), dict(restore_counter)\n\n \"\"\"\n Restores itself, as well as objects that might have been deleted along with it if cascade is the deletion strategy\n \"\"\"\n self.collector = models.deletion.Collector(using=using) \n self.collector.collect([self], keep_parents=keep_parents)", "response": "Restores all objects in this object and all of its children."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconnecting to AWS EC2", "response": "def connect_to_ec2(region='us-east-1', access_key=None, secret_key=None):\n \"\"\" Connect to AWS ec2\n\n :type region: str\n :param region: AWS region to connect to\n :type access_key: str\n :param access_key: AWS access key id\n :type secret_key: str\n :param secret_key: AWS secret access key\n :returns: boto.ec2.connection.EC2Connection -- EC2 connection\n \"\"\"\n\n if access_key:\n # Connect using supplied credentials\n logger.info('Connecting to AWS EC2 in {}'.format(region))\n connection = ec2.connect_to_region(\n region,\n aws_access_key_id=access_key,\n aws_secret_access_key=secret_key)\n else:\n # Fetch instance metadata\n metadata = get_instance_metadata(timeout=1, num_retries=1)\n if metadata:\n try:\n region = metadata['placement']['availability-zone'][:-1]\n except KeyError:\n pass\n\n # Connect using env vars or boto credentials\n logger.info('Connecting to AWS EC2 in {}'.format(region))\n connection = ec2.connect_to_region(region)\n\n if not connection:\n logger.error('An error occurred when connecting to EC2')\n sys.exit(1)\n\n return connection"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse(self, output):\n output = self._get_lines_with_stems(output)\n words = self._make_unique(output)\n return self._parse_for_simple_stems(words)", "response": "Parse a given text into a list of simple stems."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef validlocations(configuration=None):\n # type: () -> List[Dict]\n \"\"\"\n Read valid locations from HDX\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n List[Dict]: A list of valid locations\n \"\"\"\n if Locations._validlocations is None:\n if configuration is None:\n configuration = Configuration.read()\n Locations._validlocations = configuration.call_remoteckan('group_list', {'all_fields': True})\n return Locations._validlocations", "response": "Reads valid locations from HDX and returns a list of dictionaries."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_location_from_HDX_code(code, locations=None, configuration=None):\n # type: (str, Optional[List[Dict]], Optional[Configuration]) -> Optional[str]\n \"\"\"Get location from HDX location code\n\n Args:\n code (str): code for which to get location name\n locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[str]: location name\n \"\"\"\n if locations is None:\n locations = Locations.validlocations(configuration)\n for locdict in locations:\n if code.upper() == locdict['name'].upper():\n return locdict['title']\n return None", "response": "Get location name from HDX location code"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_HDX_code_from_location(location, locations=None, configuration=None):\n # type: (str, Optional[List[Dict]], Optional[Configuration]) -> Optional[str]\n \"\"\"Get HDX code for location\n\n Args:\n location (str): Location for which to get HDX code\n locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[str]: HDX code or None\n \"\"\"\n if locations is None:\n locations = Locations.validlocations(configuration)\n locationupper = location.upper()\n for locdict in locations:\n locationcode = locdict['name'].upper()\n if locationupper == locationcode:\n return locationcode\n\n for locdict in locations:\n if locationupper == locdict['title'].upper():\n return locdict['name'].upper()\n return None", "response": "Get HDX code for a location."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn HDX code for location and whether it matches the given locations.", "response": "def get_HDX_code_from_location_partial(location, locations=None, configuration=None):\n # type: (str, Optional[List[Dict]], Optional[Configuration]) -> Tuple[Optional[str], bool]\n \"\"\"Get HDX code for location\n\n Args:\n location (str): Location for which to get HDX code\n locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Tuple[Optional[str], bool]: HDX code and if the match is exact or (None, False) for no match\n \"\"\"\n hdx_code = Locations.get_HDX_code_from_location(location, locations, configuration)\n\n if hdx_code is not None:\n return hdx_code, True\n\n if locations is None:\n locations = Locations.validlocations(configuration)\n locationupper = location.upper()\n for locdict in locations:\n locationname = locdict['title'].upper()\n if locationupper in locationname or locationname in locationupper:\n return locdict['name'].upper(), False\n\n return None, False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef Main():\n argument_parser = argparse.ArgumentParser(\n description='Validates dtFabric format definitions.')\n\n argument_parser.add_argument(\n 'source', nargs='?', action='store', metavar='PATH', default=None,\n help=(\n 'path of the file or directory containing the dtFabric format '\n 'definitions.'))\n\n options = argument_parser.parse_args()\n\n if not options.source:\n print('Source value is missing.')\n print('')\n argument_parser.print_help()\n print('')\n return False\n\n if not os.path.exists(options.source):\n print('No such file: {0:s}'.format(options.source))\n print('')\n return False\n\n logging.basicConfig(\n level=logging.INFO, format='[%(levelname)s] %(message)s')\n\n\n source_is_directory = os.path.isdir(options.source)\n\n validator = DefinitionsValidator()\n\n if source_is_directory:\n source_description = os.path.join(options.source, '*.yaml')\n else:\n source_description = options.source\n\n print('Validating dtFabric definitions in: {0:s}'.format(source_description))\n if source_is_directory:\n result = validator.CheckDirectory(options.source)\n else:\n result = validator.CheckFile(options.source)\n\n if not result:\n print('FAILURE')\n else:\n print('SUCCESS')\n\n return result", "response": "The main function of the main program."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef CheckDirectory(self, path, extension='yaml'):\n result = True\n\n if extension:\n glob_spec = os.path.join(path, '*.{0:s}'.format(extension))\n else:\n glob_spec = os.path.join(path, '*')\n\n for definition_file in sorted(glob.glob(glob_spec)):\n if not self.CheckFile(definition_file):\n result = False\n\n return result", "response": "Checks that the directory contains valid definition files."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef CheckFile(self, path):\n print('Checking: {0:s}'.format(path))\n\n definitions_registry = registry.DataTypeDefinitionsRegistry()\n definitions_reader = reader.YAMLDataTypeDefinitionsFileReader()\n result = False\n\n try:\n definitions_reader.ReadFile(definitions_registry, path)\n result = True\n\n except KeyError as exception:\n logging.warning((\n 'Unable to register data type definition in file: {0:s} with '\n 'error: {1:s}').format(path, exception))\n\n except errors.FormatError as exception:\n logging.warning(\n 'Unable to validate file: {0:s} with error: {1:s}'.format(\n path, exception))\n\n return result", "response": "Checks the file for valid data type definitions."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef styleattribute(self, element):\n css_text = element.get('style')\n if css_text:\n return cssutils.css.CSSStyleDeclaration(cssText=css_text)\n else:\n return None", "response": "returns css. CSSStyleDeclaration of inline styles for html"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread the configuration file and return it as a dict", "response": "def get_configuration(filename):\n \"\"\" Read configuration file\n\n :type filename: str\n :param filename: Path to the configuration file\n \"\"\"\n logger.debug('Reading configuration from {}'.format(filename))\n conf = SafeConfigParser()\n conf.read(filename)\n\n if not conf:\n logger.error('Configuration file {} not found'.format(filename))\n sys.exit(1)\n\n if not conf.has_section('general'):\n logger.error('Missing [general] section in the configuration file')\n sys.exit(1)\n\n try:\n config = {\n 'access-key-id': conf.get('general', 'access-key-id'),\n 'secret-access-key': conf.get('general', 'secret-access-key'),\n 'region': conf.get('general', 'region'),\n }\n except NoOptionError as err:\n logger.error('Error in config file: {}'.format(err))\n sys.exit(1)\n\n return config"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef inline_css(html_message, encoding='unicode'):\n\n document = etree.HTML(html_message)\n converter = Conversion()\n converter.perform(document, html_message, '', encoding=encoding)\n return converter.convertedHTML", "response": "Inlines all CSS in an HTML string into the applicable elements."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if the byte stream is large enough for the data type.", "response": "def _CheckByteStreamSize(self, byte_stream, byte_offset, data_type_size):\n \"\"\"Checks if the byte stream is large enough for the data type.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (int): offset into the byte stream where to start.\n data_type_size (int): data type size.\n\n Raises:\n ByteStreamTooSmallError: if the byte stream is too small.\n MappingError: if the size of the byte stream cannot be determined.\n \"\"\"\n try:\n byte_stream_size = len(byte_stream)\n\n except Exception as exception:\n raise errors.MappingError(exception)\n\n if byte_stream_size - byte_offset < data_type_size:\n raise errors.ByteStreamTooSmallError(\n 'Byte stream too small requested: {0:d} available: {1:d}'.format(\n data_type_size, byte_stream_size))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nretrieves the byte stream operation.", "response": "def _GetByteStreamOperation(self):\n \"\"\"Retrieves the byte stream operation.\n\n Returns:\n ByteStreamOperation: byte stream operation or None if unable to determine.\n \"\"\"\n byte_order_string = self.GetStructByteOrderString()\n format_string = self.GetStructFormatString() # pylint: disable=assignment-from-none\n if not format_string:\n return None\n\n format_string = ''.join([byte_order_string, format_string])\n return byte_operations.StructOperation(format_string)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef GetStructByteOrderString(self):\n if not self._data_type_definition:\n return None\n\n return self._BYTE_ORDER_STRINGS.get(\n self._data_type_definition.byte_order, None)", "response": "Retrieves the Python struct format string."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfolds the data type into a byte stream.", "response": "def FoldByteStream(self, mapped_value, **unused_kwargs):\n \"\"\"Folds the data type into a byte stream.\n\n Args:\n mapped_value (object): mapped value.\n\n Returns:\n bytes: byte stream.\n\n Raises:\n FoldingError: if the data type definition cannot be folded into\n the byte stream.\n \"\"\"\n try:\n value = self.FoldValue(mapped_value)\n return self._operation.WriteTo(tuple([value]))\n\n except Exception as exception:\n error_string = (\n 'Unable to write: {0:s} to byte stream with error: {1!s}').format(\n self._data_type_definition.name, exception)\n raise errors.FoldingError(error_string)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef MapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n object: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n data_type_size = self._data_type_definition.GetByteSize()\n self._CheckByteStreamSize(byte_stream, byte_offset, data_type_size)\n\n try:\n struct_tuple = self._operation.ReadFrom(byte_stream[byte_offset:])\n mapped_value = self.MapValue(*struct_tuple)\n\n except Exception as exception:\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: {2!s}').format(\n self._data_type_definition.name, byte_offset, exception)\n raise errors.MappingError(error_string)\n\n if context:\n context.byte_size = data_type_size\n\n return mapped_value", "response": "Maps the data type on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef FoldValue(self, value):\n if value is False and self._data_type_definition.false_value is not None:\n return self._data_type_definition.false_value\n\n if value is True and self._data_type_definition.true_value is not None:\n return self._data_type_definition.true_value\n\n raise ValueError('No matching True and False values')", "response": "Folds the data type into a value."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef GetStructFormatString(self):\n if self._data_type_definition.format == definitions.FORMAT_UNSIGNED:\n return self._FORMAT_STRINGS_UNSIGNED.get(\n self._data_type_definition.size, None)\n\n return self._FORMAT_STRINGS_SIGNED.get(\n self._data_type_definition.size, None)", "response": "Retrieves the format string as used by Python struct."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef FoldByteStream(self, mapped_value, **unused_kwargs):\n value = None\n\n try:\n if self._byte_order == definitions.BYTE_ORDER_BIG_ENDIAN:\n value = mapped_value.bytes\n elif self._byte_order == definitions.BYTE_ORDER_LITTLE_ENDIAN:\n value = mapped_value.bytes_le\n\n except Exception as exception:\n error_string = (\n 'Unable to write: {0:s} to byte stream with error: {1!s}').format(\n self._data_type_definition.name, exception)\n raise errors.FoldingError(error_string)\n\n return value", "response": "Folds the data type into a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef MapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n uuid.UUID: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n data_type_size = self._data_type_definition.GetByteSize()\n self._CheckByteStreamSize(byte_stream, byte_offset, data_type_size)\n\n try:\n if self._byte_order == definitions.BYTE_ORDER_BIG_ENDIAN:\n mapped_value = uuid.UUID(\n bytes=byte_stream[byte_offset:byte_offset + 16])\n elif self._byte_order == definitions.BYTE_ORDER_LITTLE_ENDIAN:\n mapped_value = uuid.UUID(\n bytes_le=byte_stream[byte_offset:byte_offset + 16])\n\n except Exception as exception:\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: {2!s}').format(\n self._data_type_definition.name, byte_offset, exception)\n raise errors.MappingError(error_string)\n\n if context:\n context.byte_size = data_type_size\n\n return mapped_value", "response": "Maps the data type on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _CalculateElementsDataSize(self, context):\n elements_data_size = None\n\n if self._HasElementsDataSize():\n elements_data_size = self._EvaluateElementsDataSize(context)\n\n elif self._HasNumberOfElements():\n element_byte_size = self._element_data_type_definition.GetByteSize()\n if element_byte_size is not None:\n number_of_elements = self._EvaluateNumberOfElements(context)\n elements_data_size = number_of_elements * element_byte_size\n\n return elements_data_size", "response": "Calculates the elements data size for the elements of the specified data type map context."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nevaluate the elements data size of the elements element type map.", "response": "def _EvaluateElementsDataSize(self, context):\n \"\"\"Evaluates elements data size.\n\n Args:\n context (DataTypeMapContext): data type map context.\n\n Returns:\n int: elements data size.\n\n Raises:\n MappingError: if the elements data size cannot be determined.\n \"\"\"\n elements_data_size = None\n if self._data_type_definition.elements_data_size:\n elements_data_size = self._data_type_definition.elements_data_size\n\n elif self._data_type_definition.elements_data_size_expression:\n expression = self._data_type_definition.elements_data_size_expression\n namespace = {}\n if context and context.values:\n namespace.update(context.values)\n # Make sure __builtins__ contains an empty dictionary.\n namespace['__builtins__'] = {}\n\n try:\n elements_data_size = eval(expression, namespace) # pylint: disable=eval-used\n except Exception as exception:\n raise errors.MappingError(\n 'Unable to determine elements data size with error: {0!s}'.format(\n exception))\n\n if elements_data_size is None or elements_data_size < 0:\n raise errors.MappingError(\n 'Invalid elements data size: {0!s}'.format(elements_data_size))\n\n return elements_data_size"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _EvaluateNumberOfElements(self, context):\n number_of_elements = None\n if self._data_type_definition.number_of_elements:\n number_of_elements = self._data_type_definition.number_of_elements\n\n elif self._data_type_definition.number_of_elements_expression:\n expression = self._data_type_definition.number_of_elements_expression\n namespace = {}\n if context and context.values:\n namespace.update(context.values)\n # Make sure __builtins__ contains an empty dictionary.\n namespace['__builtins__'] = {}\n\n try:\n number_of_elements = eval(expression, namespace) # pylint: disable=eval-used\n except Exception as exception:\n raise errors.MappingError(\n 'Unable to determine number of elements with error: {0!s}'.format(\n exception))\n\n if number_of_elements is None or number_of_elements < 0:\n raise errors.MappingError(\n 'Invalid number of elements: {0!s}'.format(number_of_elements))\n\n return number_of_elements", "response": "Evaluates the number of elements in the data type map."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving the element data type definition.", "response": "def _GetElementDataTypeDefinition(self, data_type_definition):\n \"\"\"Retrieves the element data type definition.\n\n Args:\n data_type_definition (DataTypeDefinition): data type definition.\n\n Returns:\n DataTypeDefinition: element data type definition.\n\n Raises:\n FormatError: if the element data type cannot be determined from the data\n type definition.\n \"\"\"\n if not data_type_definition:\n raise errors.FormatError('Missing data type definition')\n\n element_data_type_definition = getattr(\n data_type_definition, 'element_data_type_definition', None)\n if not element_data_type_definition:\n raise errors.FormatError(\n 'Invalid data type definition missing element')\n\n return element_data_type_definition"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef GetSizeHint(self, context=None, **unused_kwargs):\n context_state = getattr(context, 'state', {})\n\n elements_data_size = self.GetByteSize()\n if elements_data_size:\n return elements_data_size\n\n try:\n elements_data_size = self._CalculateElementsDataSize(context)\n except errors.MappingError:\n pass\n\n if elements_data_size is None and self._HasElementsTerminator():\n size_hints = context_state.get('size_hints', {})\n size_hint = size_hints.get(self._data_type_definition.name, None)\n\n elements_data_size = 0\n\n if size_hint:\n elements_data_size = size_hint.byte_size\n\n if not size_hint or not size_hint.is_complete:\n elements_data_size += self._element_data_type_definition.GetByteSize()\n\n return elements_data_size", "response": "Retrieves a hint about the size of the elements."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmap a composite data type on a byte stream.", "response": "def _CompositeMapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps a sequence of composite data types on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n tuple[object, ...]: mapped values.\n\n Raises:\n ByteStreamTooSmallError: if the byte stream is too small.\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n elements_data_size = None\n elements_terminator = None\n number_of_elements = None\n\n if self._HasElementsDataSize():\n elements_data_size = self._EvaluateElementsDataSize(context)\n\n element_byte_size = self._element_data_type_definition.GetByteSize()\n if element_byte_size is not None:\n number_of_elements, _ = divmod(elements_data_size, element_byte_size)\n else:\n elements_terminator = (\n self._element_data_type_definition.elements_terminator)\n\n elif self._HasElementsTerminator():\n elements_terminator = self._data_type_definition.elements_terminator\n\n elif self._HasNumberOfElements():\n number_of_elements = self._EvaluateNumberOfElements(context)\n\n if elements_terminator is None and number_of_elements is None:\n raise errors.MappingError(\n 'Unable to determine element terminator or number of elements')\n\n context_state = getattr(context, 'state', {})\n\n elements_data_offset = context_state.get('elements_data_offset', 0)\n element_index = context_state.get('element_index', 0)\n element_value = None\n mapped_values = context_state.get('mapped_values', [])\n size_hints = context_state.get('size_hints', {})\n subcontext = context_state.get('context', None)\n\n if not subcontext:\n subcontext = DataTypeMapContext()\n\n try:\n while byte_stream[byte_offset:]:\n if (number_of_elements is not None and\n element_index == number_of_elements):\n break\n\n if (elements_data_size is not None and\n elements_data_offset >= elements_data_size):\n break\n\n element_value = self._element_data_type_map.MapByteStream(\n byte_stream, byte_offset=byte_offset, context=subcontext)\n\n byte_offset += subcontext.byte_size\n elements_data_offset += subcontext.byte_size\n element_index += 1\n mapped_values.append(element_value)\n\n if (elements_terminator is not None and\n element_value == elements_terminator):\n break\n\n except errors.ByteStreamTooSmallError as exception:\n context_state['context'] = subcontext\n context_state['elements_data_offset'] = elements_data_offset\n context_state['element_index'] = element_index\n context_state['mapped_values'] = mapped_values\n raise errors.ByteStreamTooSmallError(exception)\n\n except Exception as exception:\n raise errors.MappingError(exception)\n\n if number_of_elements is not None and element_index != number_of_elements:\n context_state['context'] = subcontext\n context_state['elements_data_offset'] = elements_data_offset\n context_state['element_index'] = element_index\n context_state['mapped_values'] = mapped_values\n\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: missing element: {2:d}').format(\n self._data_type_definition.name, byte_offset, element_index - 1)\n raise errors.ByteStreamTooSmallError(error_string)\n\n if (elements_terminator is not None and\n element_value != elements_terminator and (\n elements_data_size is None or\n elements_data_offset < elements_data_size)):\n byte_stream_size = len(byte_stream)\n\n size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(\n byte_stream_size - byte_offset)\n\n context_state['context'] = subcontext\n context_state['elements_data_offset'] = elements_data_offset\n context_state['element_index'] = element_index\n context_state['mapped_values'] = mapped_values\n context_state['size_hints'] = size_hints\n\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: unable to find elements terminator').format(\n self._data_type_definition.name, byte_offset)\n raise errors.ByteStreamTooSmallError(error_string)\n\n if context:\n context.byte_size = elements_data_offset\n context.state = {}\n\n return tuple(mapped_values)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _LinearFoldByteStream(self, mapped_value, **unused_kwargs):\n try:\n return self._operation.WriteTo(mapped_value)\n\n except Exception as exception:\n error_string = (\n 'Unable to write: {0:s} to byte stream with error: {1!s}').format(\n self._data_type_definition.name, exception)\n raise errors.FoldingError(error_string)", "response": "Folds the data type into a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _LinearMapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps a data type sequence on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n tuple[object, ...]: mapped values.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n elements_data_size = self._data_type_definition.GetByteSize()\n self._CheckByteStreamSize(byte_stream, byte_offset, elements_data_size)\n\n try:\n struct_tuple = self._operation.ReadFrom(byte_stream[byte_offset:])\n mapped_values = map(self._element_data_type_map.MapValue, struct_tuple)\n\n except Exception as exception:\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: {2!s}').format(\n self._data_type_definition.name, byte_offset, exception)\n raise errors.MappingError(error_string)\n\n if context:\n context.byte_size = elements_data_size\n\n return tuple(mapped_values)", "response": "Maps a data type sequence on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nretrieve the Python struct format string.", "response": "def GetStructFormatString(self):\n \"\"\"Retrieves the Python struct format string.\n\n Returns:\n str: format string as used by Python struct or None if format string\n cannot be determined.\n \"\"\"\n if not self._element_data_type_map:\n return None\n\n number_of_elements = None\n if self._data_type_definition.elements_data_size:\n element_byte_size = self._element_data_type_definition.GetByteSize()\n if element_byte_size is None:\n return None\n\n number_of_elements, _ = divmod(\n self._data_type_definition.elements_data_size, element_byte_size)\n\n elif self._data_type_definition.number_of_elements:\n number_of_elements = self._data_type_definition.number_of_elements\n\n format_string = self._element_data_type_map.GetStructFormatString()\n if not number_of_elements or not format_string:\n return None\n\n return '{0:d}{1:s}'.format(number_of_elements, format_string)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef FoldByteStream(self, mapped_value, context=None, **unused_kwargs):\n elements_data_size = self._CalculateElementsDataSize(context)\n if elements_data_size is not None:\n if elements_data_size != len(mapped_value):\n raise errors.FoldingError(\n 'Mismatch between elements data size and mapped value size')\n\n elif not self._HasElementsTerminator():\n raise errors.FoldingError('Unable to determine elements data size')\n\n else:\n elements_terminator = self._data_type_definition.elements_terminator\n elements_terminator_size = len(elements_terminator)\n if mapped_value[-elements_terminator_size:] != elements_terminator:\n mapped_value = b''.join([mapped_value, elements_terminator])\n\n return mapped_value", "response": "Folds the data type into a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef MapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n tuple[object, ...]: mapped values.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n context_state = getattr(context, 'state', {})\n\n size_hints = context_state.get('size_hints', {})\n\n elements_data_size = self._CalculateElementsDataSize(context)\n if elements_data_size is not None:\n self._CheckByteStreamSize(byte_stream, byte_offset, elements_data_size)\n\n elif not self._HasElementsTerminator():\n raise errors.MappingError(\n 'Unable to determine elements data size and missing elements '\n 'terminator')\n\n else:\n byte_stream_size = len(byte_stream)\n\n element_byte_size = self._element_data_type_definition.GetByteSize()\n elements_data_offset = byte_offset\n next_elements_data_offset = elements_data_offset + element_byte_size\n\n elements_terminator = self._data_type_definition.elements_terminator\n element_value = byte_stream[\n elements_data_offset:next_elements_data_offset]\n\n while byte_stream[elements_data_offset:]:\n elements_data_offset = next_elements_data_offset\n if element_value == elements_terminator:\n elements_data_size = elements_data_offset - byte_offset\n break\n\n next_elements_data_offset += element_byte_size\n element_value = byte_stream[\n elements_data_offset:next_elements_data_offset]\n\n if element_value != elements_terminator:\n size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(\n byte_stream_size - byte_offset)\n\n context_state['size_hints'] = size_hints\n\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: unable to find elements terminator').format(\n self._data_type_definition.name, byte_offset)\n raise errors.ByteStreamTooSmallError(error_string)\n\n if context:\n context.byte_size = elements_data_size\n\n size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(\n elements_data_size, is_complete=True)\n\n context_state['size_hints'] = size_hints\n\n return byte_stream[byte_offset:byte_offset + elements_data_size]", "response": "Maps the data type on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmaps the data type on a byte stream.", "response": "def MapByteStream(self, byte_stream, byte_offset=0, **unused_kwargs):\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n\n Returns:\n object: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n return byte_stream[byte_offset:byte_offset + self.byte_size]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfolding the data type into a byte stream.", "response": "def FoldByteStream(self, mapped_value, **kwargs):\n \"\"\"Folds the data type into a byte stream.\n\n Args:\n mapped_value (object): mapped value.\n\n Returns:\n bytes: byte stream.\n\n Raises:\n FoldingError: if the data type definition cannot be folded into\n the byte stream.\n \"\"\"\n try:\n byte_stream = mapped_value.encode(self._data_type_definition.encoding)\n\n except Exception as exception:\n error_string = (\n 'Unable to write: {0:s} to byte stream with error: {1!s}').format(\n self._data_type_definition.name, exception)\n raise errors.MappingError(error_string)\n\n return super(StringMap, self).FoldByteStream(byte_stream, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmaps the data type on a byte stream.", "response": "def MapByteStream(self, byte_stream, byte_offset=0, **kwargs):\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n\n Returns:\n str: mapped values.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n byte_stream = super(StringMap, self).MapByteStream(\n byte_stream, byte_offset=byte_offset, **kwargs)\n\n if self._HasElementsTerminator():\n # Remove the elements terminator and any trailing data from\n # the byte stream.\n elements_terminator = self._data_type_definition.elements_terminator\n elements_terminator_size = len(elements_terminator)\n\n byte_offset = 0\n byte_stream_size = len(byte_stream)\n\n while byte_offset < byte_stream_size:\n end_offset = byte_offset + elements_terminator_size\n if byte_stream[byte_offset:end_offset] == elements_terminator:\n break\n\n byte_offset += elements_terminator_size\n\n byte_stream = byte_stream[:byte_offset]\n\n try:\n return byte_stream.decode(self._data_type_definition.encoding)\n\n except Exception as exception:\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: {2!s}').format(\n self._data_type_definition.name, byte_offset, exception)\n raise errors.MappingError(error_string)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if a data type definition needs a composite map.", "response": "def _CheckCompositeMap(self, data_type_definition):\n \"\"\"Determines if the data type definition needs a composite map.\n\n Args:\n data_type_definition (DataTypeDefinition): structure data type definition.\n\n Returns:\n bool: True if a composite map is needed, False otherwise.\n\n Raises:\n FormatError: if a composite map is needed cannot be determined from the\n data type definition.\n \"\"\"\n if not data_type_definition:\n raise errors.FormatError('Missing data type definition')\n\n members = getattr(data_type_definition, 'members', None)\n if not members:\n raise errors.FormatError('Invalid data type definition missing members')\n\n is_composite_map = False\n last_member_byte_order = data_type_definition.byte_order\n\n for member_definition in members:\n if member_definition.IsComposite():\n is_composite_map = True\n break\n\n # TODO: check for padding type\n # TODO: determine if padding type can be defined as linear\n if (last_member_byte_order != definitions.BYTE_ORDER_NATIVE and\n member_definition.byte_order != definitions.BYTE_ORDER_NATIVE and\n last_member_byte_order != member_definition.byte_order):\n is_composite_map = True\n break\n\n last_member_byte_order = member_definition.byte_order\n\n return is_composite_map"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _CompositeFoldByteStream(\n self, mapped_value, context=None, **unused_kwargs):\n \"\"\"Folds the data type into a byte stream.\n\n Args:\n mapped_value (object): mapped value.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n bytes: byte stream.\n\n Raises:\n FoldingError: if the data type definition cannot be folded into\n the byte stream.\n \"\"\"\n context_state = getattr(context, 'state', {})\n\n attribute_index = context_state.get('attribute_index', 0)\n subcontext = context_state.get('context', None)\n\n if not subcontext:\n subcontext = DataTypeMapContext(values={\n type(mapped_value).__name__: mapped_value})\n\n data_attributes = []\n\n for attribute_index in range(attribute_index, self._number_of_attributes):\n attribute_name = self._attribute_names[attribute_index]\n data_type_map = self._data_type_maps[attribute_index]\n\n member_value = getattr(mapped_value, attribute_name, None)\n if data_type_map is None or member_value is None:\n continue\n\n member_data = data_type_map.FoldByteStream(\n member_value, context=subcontext)\n if member_data is None:\n return None\n\n data_attributes.append(member_data)\n\n if context:\n context.state = {}\n\n return b''.join(data_attributes)", "response": "Folds the data type into a byte stream."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _CompositeMapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps a sequence of composite data types on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n object: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n context_state = getattr(context, 'state', {})\n\n attribute_index = context_state.get('attribute_index', 0)\n mapped_values = context_state.get('mapped_values', None)\n subcontext = context_state.get('context', None)\n\n if not mapped_values:\n mapped_values = self._structure_values_class()\n if not subcontext:\n subcontext = DataTypeMapContext(values={\n type(mapped_values).__name__: mapped_values})\n\n members_data_size = 0\n\n for attribute_index in range(attribute_index, self._number_of_attributes):\n attribute_name = self._attribute_names[attribute_index]\n data_type_map = self._data_type_maps[attribute_index]\n member_definition = self._data_type_definition.members[attribute_index]\n\n condition = getattr(member_definition, 'condition', None)\n if condition:\n namespace = dict(subcontext.values)\n # Make sure __builtins__ contains an empty dictionary.\n namespace['__builtins__'] = {}\n\n try:\n condition_result = eval(condition, namespace) # pylint: disable=eval-used\n except Exception as exception:\n raise errors.MappingError(\n 'Unable to evaluate condition with error: {0!s}'.format(\n exception))\n\n if not isinstance(condition_result, bool):\n raise errors.MappingError(\n 'Condition does not result in a boolean value')\n\n if not condition_result:\n continue\n\n if isinstance(member_definition, data_types.PaddingDefinition):\n _, byte_size = divmod(\n members_data_size, member_definition.alignment_size)\n if byte_size > 0:\n byte_size = member_definition.alignment_size - byte_size\n\n data_type_map.byte_size = byte_size\n\n try:\n value = data_type_map.MapByteStream(\n byte_stream, byte_offset=byte_offset, context=subcontext)\n setattr(mapped_values, attribute_name, value)\n\n except errors.ByteStreamTooSmallError as exception:\n context_state['attribute_index'] = attribute_index\n context_state['context'] = subcontext\n context_state['mapped_values'] = mapped_values\n raise errors.ByteStreamTooSmallError(exception)\n\n except Exception as exception:\n raise errors.MappingError(exception)\n\n supported_values = getattr(member_definition, 'values', None)\n if supported_values and value not in supported_values:\n raise errors.MappingError(\n 'Value: {0!s} not in supported values: {1:s}'.format(\n value, ', '.join([\n '{0!s}'.format(value) for value in supported_values])))\n\n byte_offset += subcontext.byte_size\n members_data_size += subcontext.byte_size\n\n if attribute_index != (self._number_of_attributes - 1):\n context_state['attribute_index'] = attribute_index\n context_state['context'] = subcontext\n context_state['mapped_values'] = mapped_values\n\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: missing attribute: {2:d}').format(\n self._data_type_definition.name, byte_offset, attribute_index)\n raise errors.ByteStreamTooSmallError(error_string)\n\n if context:\n context.byte_size = members_data_size\n context.state = {}\n\n return mapped_values", "response": "Maps a composite data type on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndetermine the attribute names of the members of the data type definition.", "response": "def _GetAttributeNames(self, data_type_definition):\n \"\"\"Determines the attribute (or field) names of the members.\n\n Args:\n data_type_definition (DataTypeDefinition): data type definition.\n\n Returns:\n list[str]: attribute names.\n\n Raises:\n FormatError: if the attribute names cannot be determined from the data\n type definition.\n \"\"\"\n if not data_type_definition:\n raise errors.FormatError('Missing data type definition')\n\n attribute_names = []\n for member_definition in data_type_definition.members:\n attribute_names.append(member_definition.name)\n\n return attribute_names"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _GetMemberDataTypeMaps(self, data_type_definition, data_type_map_cache):\n if not data_type_definition:\n raise errors.FormatError('Missing data type definition')\n\n members = getattr(data_type_definition, 'members', None)\n if not members:\n raise errors.FormatError('Invalid data type definition missing members')\n\n data_type_maps = []\n\n members_data_size = 0\n for member_definition in members:\n if isinstance(member_definition, data_types.MemberDataTypeDefinition):\n member_definition = member_definition.member_data_type_definition\n\n if (data_type_definition.byte_order != definitions.BYTE_ORDER_NATIVE and\n member_definition.byte_order == definitions.BYTE_ORDER_NATIVE):\n # Make a copy of the data type definition where byte-order can be\n # safely changed.\n member_definition = copy.copy(member_definition)\n member_definition.name = '_{0:s}_{1:s}'.format(\n data_type_definition.name, member_definition.name)\n member_definition.byte_order = data_type_definition.byte_order\n\n if member_definition.name not in data_type_map_cache:\n data_type_map = DataTypeMapFactory.CreateDataTypeMapByType(\n member_definition)\n data_type_map_cache[member_definition.name] = data_type_map\n\n data_type_map = data_type_map_cache[member_definition.name]\n if members_data_size is not None:\n if not isinstance(member_definition, data_types.PaddingDefinition):\n byte_size = member_definition.GetByteSize()\n else:\n _, byte_size = divmod(\n members_data_size, member_definition.alignment_size)\n if byte_size > 0:\n byte_size = member_definition.alignment_size - byte_size\n\n data_type_map.byte_size = byte_size\n\n if byte_size is None:\n members_data_size = None\n else:\n members_data_size += byte_size\n\n data_type_maps.append(data_type_map)\n\n return data_type_maps", "response": "Retrieves the member data type maps."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfold the data type into a byte stream.", "response": "def _LinearFoldByteStream(self, mapped_value, **unused_kwargs):\n \"\"\"Folds the data type into a byte stream.\n\n Args:\n mapped_value (object): mapped value.\n\n Returns:\n bytes: byte stream.\n\n Raises:\n FoldingError: if the data type definition cannot be folded into\n the byte stream.\n \"\"\"\n try:\n attribute_values = [\n getattr(mapped_value, attribute_name, None)\n for attribute_name in self._attribute_names]\n attribute_values = [\n value for value in attribute_values if value is not None]\n return self._operation.WriteTo(tuple(attribute_values))\n\n except Exception as exception:\n error_string = (\n 'Unable to write: {0:s} to byte stream with error: {1!s}').format(\n self._data_type_definition.name, exception)\n raise errors.FoldingError(error_string)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _LinearMapByteStream(\n self, byte_stream, byte_offset=0, context=None, **unused_kwargs):\n \"\"\"Maps a data type sequence on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n byte_offset (Optional[int]): offset into the byte stream where to start.\n context (Optional[DataTypeMapContext]): data type map context.\n\n Returns:\n object: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n members_data_size = self._data_type_definition.GetByteSize()\n self._CheckByteStreamSize(byte_stream, byte_offset, members_data_size)\n\n try:\n struct_tuple = self._operation.ReadFrom(byte_stream[byte_offset:])\n struct_values = []\n for attribute_index, value in enumerate(struct_tuple):\n data_type_map = self._data_type_maps[attribute_index]\n member_definition = self._data_type_definition.members[attribute_index]\n\n value = data_type_map.MapValue(value)\n\n supported_values = getattr(member_definition, 'values', None)\n if supported_values and value not in supported_values:\n raise errors.MappingError(\n 'Value: {0!s} not in supported values: {1:s}'.format(\n value, ', '.join([\n '{0!s}'.format(value) for value in supported_values])))\n\n struct_values.append(value)\n\n mapped_value = self._structure_values_class(*struct_values)\n\n except Exception as exception:\n error_string = (\n 'Unable to read: {0:s} from byte stream at offset: {1:d} '\n 'with error: {2!s}').format(\n self._data_type_definition.name, byte_offset, exception)\n raise errors.MappingError(error_string)\n\n if context:\n context.byte_size = members_data_size\n\n return mapped_value", "response": "Maps a data type sequence on a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nretrieves a hint about the size of the data type.", "response": "def GetSizeHint(self, context=None, **unused_kwargs):\n \"\"\"Retrieves a hint about the size.\n\n Args:\n context (Optional[DataTypeMapContext]): data type map context, used to\n determine the size hint.\n\n Returns:\n int: hint of the number of bytes needed from the byte stream or None.\n \"\"\"\n context_state = getattr(context, 'state', {})\n\n subcontext = context_state.get('context', None)\n if not subcontext:\n mapped_values = context_state.get('mapped_values', None)\n subcontext = DataTypeMapContext(values={\n type(mapped_values).__name__: mapped_values})\n\n size_hint = 0\n for data_type_map in self._data_type_maps:\n data_type_size = data_type_map.GetSizeHint(context=subcontext)\n if data_type_size is None:\n break\n\n size_hint += data_type_size\n\n return size_hint"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nretrieve the Python struct format string.", "response": "def GetStructFormatString(self):\n \"\"\"Retrieves the Python struct format string.\n\n Returns:\n str: format string as used by Python struct or None if format string\n cannot be determined.\n \"\"\"\n if self._format_string is None and self._data_type_maps:\n format_strings = []\n for member_data_type_map in self._data_type_maps:\n if member_data_type_map is None:\n return None\n\n member_format_string = member_data_type_map.GetStructFormatString()\n if member_format_string is None:\n return None\n\n format_strings.append(member_format_string)\n\n self._format_string = ''.join(format_strings)\n\n return self._format_string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfold the data type into a byte stream.", "response": "def FoldByteStream(self, mapped_value, **unused_kwargs): # pylint: disable=redundant-returns-doc\n \"\"\"Folds the data type into a byte stream.\n\n Args:\n mapped_value (object): mapped value.\n\n Returns:\n bytes: byte stream.\n\n Raises:\n FoldingError: if the data type definition cannot be folded into\n the byte stream.\n \"\"\"\n raise errors.FoldingError(\n 'Unable to fold {0:s} data type into byte stream'.format(\n self._data_type_definition.TYPE_INDICATOR))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmaps the data type on a byte stream.", "response": "def MapByteStream(self, byte_stream, **unused_kwargs): # pylint: disable=redundant-returns-doc\n \"\"\"Maps the data type on a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n\n Returns:\n object: mapped value.\n\n Raises:\n MappingError: if the data type definition cannot be mapped on\n the byte stream.\n \"\"\"\n raise errors.MappingError(\n 'Unable to map {0:s} data type to byte stream'.format(\n self._data_type_definition.TYPE_INDICATOR))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef GetName(self, number):\n value = self._data_type_definition.values_per_number.get(number, None)\n if not value:\n return None\n\n return value.name", "response": "Retrieves the name of an enumeration value by number."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef CreateDataTypeMap(self, definition_name):\n data_type_definition = self._definitions_registry.GetDefinitionByName(\n definition_name)\n if not data_type_definition:\n return None\n\n return DataTypeMapFactory.CreateDataTypeMapByType(data_type_definition)", "response": "Creates a specific data type map by name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef CreateDataTypeMapByType(cls, data_type_definition):\n data_type_map_class = cls._MAP_PER_DEFINITION.get(\n data_type_definition.TYPE_INDICATOR, None)\n if not data_type_map_class:\n return None\n\n return data_type_map_class(data_type_definition)", "response": "Creates a specific data type map by type indicator."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nretrieving the byte size of the data type definition.", "response": "def GetByteSize(self):\n \"\"\"Retrieves the byte size of the data type definition.\n\n Returns:\n int: data type size in bytes or None if size cannot be determined.\n \"\"\"\n if self.size == definitions.SIZE_NATIVE or self.units != 'bytes':\n return None\n\n return self.size"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nretrieving the byte size of the data type definition.", "response": "def GetByteSize(self):\n \"\"\"Retrieves the byte size of the data type definition.\n\n Returns:\n int: data type size in bytes or None if size cannot be determined.\n \"\"\"\n if not self.element_data_type_definition:\n return None\n\n if self.elements_data_size:\n return self.elements_data_size\n\n if not self.number_of_elements:\n return None\n\n element_byte_size = self.element_data_type_definition.GetByteSize()\n if not element_byte_size:\n return None\n\n return element_byte_size * self.number_of_elements"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding a member definition to the internal list of members.", "response": "def AddMemberDefinition(self, member_definition):\n \"\"\"Adds a member definition.\n\n Args:\n member_definition (DataTypeDefinition): member data type definition.\n \"\"\"\n self._byte_size = None\n self.members.append(member_definition)\n\n if self.sections:\n section_definition = self.sections[-1]\n section_definition.members.append(member_definition)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndetermines if the data type is composite.", "response": "def IsComposite(self):\n \"\"\"Determines if the data type is composite.\n\n A composite data type consists of other data types.\n\n Returns:\n bool: True if the data type is composite, False otherwise.\n \"\"\"\n return bool(self.condition) or (\n self.member_data_type_definition and\n self.member_data_type_definition.IsComposite())"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef GetByteSize(self):\n if self._byte_size is None and self.members:\n self._byte_size = 0\n for member_definition in self.members:\n if isinstance(member_definition, PaddingDefinition):\n _, byte_size = divmod(\n self._byte_size, member_definition.alignment_size)\n if byte_size > 0:\n byte_size = member_definition.alignment_size - byte_size\n\n else:\n byte_size = member_definition.GetByteSize()\n if byte_size is None:\n self._byte_size = None\n break\n\n self._byte_size += byte_size\n\n return self._byte_size", "response": "Retrieves the byte size of the data type definition."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef GetByteSize(self):\n if self._byte_size is None and self.members:\n self._byte_size = 0\n for member_definition in self.members:\n byte_size = member_definition.GetByteSize()\n if byte_size is None:\n self._byte_size = None\n break\n\n self._byte_size = max(self._byte_size, byte_size)\n\n return self._byte_size", "response": "Retrieves the byte size of the data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds an enumeration value.", "response": "def AddValue(self, name, number, aliases=None, description=None):\n \"\"\"Adds an enumeration value.\n\n Args:\n name (str): name.\n number (int): number.\n aliases (Optional[list[str]]): aliases.\n description (Optional[str]): description.\n\n Raises:\n KeyError: if the enumeration value already exists.\n \"\"\"\n if name in self.values_per_name:\n raise KeyError('Value with name: {0:s} already exists.'.format(name))\n\n if number in self.values_per_number:\n raise KeyError('Value with number: {0!s} already exists.'.format(number))\n\n for alias in aliases or []:\n if alias in self.values_per_alias:\n raise KeyError('Value with alias: {0:s} already exists.'.format(alias))\n\n enumeration_value = EnumerationValue(\n name, number, aliases=aliases, description=description)\n\n self.values.append(enumeration_value)\n self.values_per_name[name] = enumeration_value\n self.values_per_number[number] = enumeration_value\n\n for alias in aliases or []:\n self.values_per_alias[alias] = enumeration_value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a member definition to the internal list of members.", "response": "def AddMemberDefinition(self, member_definition):\n \"\"\"Adds a member definition.\n\n Args:\n member_definition (DataTypeDefinition): member data type definition.\n \"\"\"\n self.members.append(member_definition)\n member_definition.family_definition = self"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parse_series(s):\n if isinstance(s, Element):\n s = s._format_element()\n if not s or s == '*':\n # Happens when there's nothing, which the CSS parser thinks of as *\n return (0, 0)\n if isinstance(s, int):\n # Happens when you just get a number\n return (0, s)\n if s == 'odd':\n return (2, 1)\n elif s == 'even':\n return (2, 0)\n elif s == 'n':\n return (1, 0)\n if 'n' not in s:\n # Just a b\n return (0, int(s))\n a, b = s.split('n', 1)\n if not a:\n a = 1\n elif a == '-' or a == '+':\n a = int(a+'1')\n else:\n a = int(a)\n if not b:\n b = 0\n elif b == '-' or b == '+':\n b = int(b+'1')\n else:\n b = int(b)\n return (a, b)", "response": "Parses a series of numbers into a tuple of two sets of numbers."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_star_prefix(self):\n if self.path:\n self.path += '*/'\n else:\n self.path = '*/'\n self.star_prefix = True", "response": "Adds a /* prefix if there is no prefix."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _ReadBooleanDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a boolean data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n BooleanDataTypeDefinition: boolean data type definition.\n \"\"\"\n return self._ReadFixedSizeDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.BooleanDefinition, definition_name,\n self._SUPPORTED_ATTRIBUTES_BOOLEAN, is_member=is_member,\n supported_size_values=(1, 2, 4))", "response": "Reads a boolean data type definition."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread a character data type definition.", "response": "def _ReadCharacterDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a character data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n CharacterDataTypeDefinition: character data type definition.\n \"\"\"\n return self._ReadFixedSizeDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.CharacterDefinition, definition_name,\n self._SUPPORTED_ATTRIBUTES_FIXED_SIZE_DATA_TYPE,\n is_member=is_member, supported_size_values=(1, 2, 4))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _ReadConstantDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a constant data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n ConstantDataTypeDefinition: constant data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n error_message = 'data type not supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n value = definition_values.get('value', None)\n if value is None:\n error_message = 'missing value'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object = self._ReadSemanticDataTypeDefinition(\n definitions_registry, definition_values, data_types.ConstantDefinition,\n definition_name, self._SUPPORTED_DEFINITION_VALUES_CONSTANT)\n definition_object.value = value\n\n return definition_object", "response": "Reads a constant data type definition."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ReadDataTypeDefinition(\n self, definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values):\n \"\"\"Reads a data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_definition_values (set[str]): names of the supported definition\n values.\n\n Returns:\n DataTypeDefinition: data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n aliases = definition_values.get('aliases', None)\n description = definition_values.get('description', None)\n urls = definition_values.get('urls', None)\n\n unsupported_definition_values = set(definition_values.keys()).difference(\n supported_definition_values)\n if unsupported_definition_values:\n error_message = 'unsupported definition values: {0:s}'.format(\n ', '.join(unsupported_definition_values))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n return data_type_definition_class(\n definition_name, aliases=aliases, description=description, urls=urls)", "response": "Reads a data type definition from the data type definitions registry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread a data type definition with members.", "response": "def _ReadDataTypeDefinitionWithMembers(\n self, definitions_registry, definition_values,\n data_type_definition_class, definition_name, supports_conditions=False):\n \"\"\"Reads a data type definition with members.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supports_conditions (Optional[bool]): True if conditions are supported\n by the data type definition.\n\n Returns:\n StringDefinition: string data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n members = definition_values.get('members', None)\n if not members:\n error_message = 'missing members'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_STORAGE_DATA_TYPE_WITH_MEMBERS)\n definition_object = self._ReadDataTypeDefinition(\n definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values)\n\n attributes = definition_values.get('attributes', None)\n if attributes:\n unsupported_attributes = set(attributes.keys()).difference(\n self._SUPPORTED_ATTRIBUTES_STORAGE_DATA_TYPE)\n if unsupported_attributes:\n error_message = 'unsupported attributes: {0:s}'.format(\n ', '.join(unsupported_attributes))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n byte_order = attributes.get('byte_order', definitions.BYTE_ORDER_NATIVE)\n if byte_order not in definitions.BYTE_ORDERS:\n error_message = 'unsupported byte-order attribute: {0!s}'.format(\n byte_order)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.byte_order = byte_order\n\n for member in members:\n section = member.get('section', None)\n if section:\n member_section_definition = data_types.MemberSectionDefinition(section)\n definition_object.AddSectionDefinition(member_section_definition)\n else:\n member_data_type_definition = self._ReadMemberDataTypeDefinitionMember(\n definitions_registry, member, definition_object.name,\n supports_conditions=supports_conditions)\n definition_object.AddMemberDefinition(member_data_type_definition)\n\n return definition_object"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _ReadEnumerationDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads an enumeration data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n EnumerationDataTypeDefinition: enumeration data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n error_message = 'data type not supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n values = definition_values.get('values')\n if not values:\n error_message = 'missing values'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object = self._ReadSemanticDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.EnumerationDefinition, definition_name,\n self._SUPPORTED_DEFINITION_VALUES_ENUMERATION)\n\n last_name = None\n for enumeration_value in values:\n aliases = enumeration_value.get('aliases', None)\n description = enumeration_value.get('description', None)\n name = enumeration_value.get('name', None)\n number = enumeration_value.get('number', None)\n\n if not name or number is None:\n if last_name:\n error_location = 'after: {0:s}'.format(last_name)\n else:\n error_location = 'at start'\n\n error_message = '{0:s} missing name or number'.format(error_location)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n else:\n try:\n definition_object.AddValue(\n name, number, aliases=aliases, description=description)\n except KeyError as exception:\n error_message = '{0!s}'.format(exception)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n last_name = name\n\n return definition_object", "response": "Reads an enumeration data type definition."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading an element sequence data type definition.", "response": "def _ReadElementSequenceDataTypeDefinition(\n self, definitions_registry, definition_values,\n data_type_definition_class, definition_name, supported_definition_values):\n \"\"\"Reads an element sequence data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_definition_values (set[str]): names of the supported definition\n values.\n\n Returns:\n SequenceDefinition: sequence data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n unsupported_definition_values = set(definition_values.keys()).difference(\n supported_definition_values)\n if unsupported_definition_values:\n error_message = 'unsupported definition values: {0:s}'.format(\n ', '.join(unsupported_definition_values))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n element_data_type = definition_values.get('element_data_type', None)\n if not element_data_type:\n error_message = 'missing element data type'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n elements_data_size = definition_values.get('elements_data_size', None)\n elements_terminator = definition_values.get('elements_terminator', None)\n number_of_elements = definition_values.get('number_of_elements', None)\n\n size_values = (elements_data_size, elements_terminator, number_of_elements)\n size_values = [value for value in size_values if value is not None]\n\n if not size_values:\n error_message = (\n 'missing element data size, elements terminator and number of '\n 'elements')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if len(size_values) > 1:\n error_message = (\n 'element data size, elements terminator and number of elements '\n 'not allowed to be set at the same time')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n element_data_type_definition = definitions_registry.GetDefinitionByName(\n element_data_type)\n if not element_data_type_definition:\n error_message = 'undefined element data type: {0:s}.'.format(\n element_data_type)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n element_byte_size = element_data_type_definition.GetByteSize()\n element_type_indicator = element_data_type_definition.TYPE_INDICATOR\n if not element_byte_size and element_type_indicator != (\n definitions.TYPE_INDICATOR_STRING):\n error_message = (\n 'unsupported variable size element data type: {0:s}'.format(\n element_data_type))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n aliases = definition_values.get('aliases', None)\n description = definition_values.get('description', None)\n urls = definition_values.get('urls', None)\n\n definition_object = data_type_definition_class(\n definition_name, element_data_type_definition, aliases=aliases,\n data_type=element_data_type, description=description, urls=urls)\n\n if elements_data_size is not None:\n try:\n definition_object.elements_data_size = int(elements_data_size)\n except ValueError:\n definition_object.elements_data_size_expression = elements_data_size\n\n elif elements_terminator is not None:\n if isinstance(elements_terminator, py2to3.UNICODE_TYPE):\n elements_terminator = elements_terminator.encode('ascii')\n\n definition_object.elements_terminator = elements_terminator\n\n elif number_of_elements is not None:\n try:\n definition_object.number_of_elements = int(number_of_elements)\n except ValueError:\n definition_object.number_of_elements_expression = number_of_elements\n\n return definition_object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ReadFixedSizeDataTypeDefinition(\n self, definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_attributes,\n default_size=definitions.SIZE_NATIVE, default_units='bytes',\n is_member=False, supported_size_values=None):\n \"\"\"Reads a fixed-size data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_attributes (set[str]): names of the supported attributes.\n default_size (Optional[int]): default size.\n default_units (Optional[str]): default units.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n supported_size_values (Optional[tuple[int]]): supported size values,\n or None if not set.\n\n Returns:\n FixedSizeDataTypeDefinition: fixed-size data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n definition_object = self._ReadStorageDataTypeDefinition(\n definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_attributes, is_member=is_member)\n\n attributes = definition_values.get('attributes', None)\n if attributes:\n size = attributes.get('size', default_size)\n if size != definitions.SIZE_NATIVE:\n try:\n int(size)\n except ValueError:\n error_message = 'unuspported size attribute: {0!s}'.format(size)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if supported_size_values and size not in supported_size_values:\n error_message = 'unuspported size value: {0!s}'.format(size)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.size = size\n definition_object.units = attributes.get('units', default_units)\n\n return definition_object", "response": "Reads a fixed - size data type definition."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread a floating - point data type definition.", "response": "def _ReadFloatingPointDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a floating-point data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n FloatingPointDefinition: floating-point data type definition.\n \"\"\"\n return self._ReadFixedSizeDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.FloatingPointDefinition, definition_name,\n self._SUPPORTED_ATTRIBUTES_FIXED_SIZE_DATA_TYPE,\n is_member=is_member, supported_size_values=(4, 8))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _ReadFormatDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a format data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n FormatDefinition: format definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n error_message = 'data type not supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object = self._ReadLayoutDataTypeDefinition(\n definitions_registry, definition_values, data_types.FormatDefinition,\n definition_name, self._SUPPORTED_DEFINITION_VALUES_FORMAT)\n\n # TODO: disabled for now\n # layout = definition_values.get('layout', None)\n # if layout is None:\n # error_message = 'missing layout'\n # raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.metadata = definition_values.get('metadata', {})\n\n attributes = definition_values.get('attributes', None)\n if attributes:\n unsupported_attributes = set(attributes.keys()).difference(\n self._SUPPORTED_ATTRIBUTES_FORMAT)\n if unsupported_attributes:\n error_message = 'unsupported attributes: {0:s}'.format(\n ', '.join(unsupported_attributes))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n byte_order = attributes.get('byte_order', definitions.BYTE_ORDER_NATIVE)\n if byte_order not in definitions.BYTE_ORDERS:\n error_message = 'unsupported byte-order attribute: {0!s}'.format(\n byte_order)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.byte_order = byte_order\n\n return definition_object", "response": "Reads a format data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _ReadIntegerDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads an integer data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n IntegerDataTypeDefinition: integer data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n definition_object = self._ReadFixedSizeDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.IntegerDefinition, definition_name,\n self._SUPPORTED_ATTRIBUTES_INTEGER, is_member=is_member,\n supported_size_values=(1, 2, 4, 8))\n\n attributes = definition_values.get('attributes', None)\n if attributes:\n format_attribute = attributes.get('format', definitions.FORMAT_SIGNED)\n if format_attribute not in self._INTEGER_FORMAT_ATTRIBUTES:\n error_message = 'unsupported format attribute: {0!s}'.format(\n format_attribute)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.format = format_attribute\n\n return definition_object", "response": "Reads an integer data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _ReadLayoutDataTypeDefinition(\n self, definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values):\n \"\"\"Reads a layout data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_definition_values (set[str]): names of the supported definition\n values.\n\n Returns:\n LayoutDataTypeDefinition: layout data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n return self._ReadDataTypeDefinition(\n definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values)", "response": "Reads a layout data type definition."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _ReadMemberDataTypeDefinitionMember(\n self, definitions_registry, definition_values, definition_name,\n supports_conditions=False):\n \"\"\"Reads a member data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n supports_conditions (Optional[bool]): True if conditions are supported\n by the data type definition.\n\n Returns:\n DataTypeDefinition: structure member data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if not definition_values:\n error_message = 'invalid structure member missing definition values'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n name = definition_values.get('name', None)\n type_indicator = definition_values.get('type', None)\n\n if not name and type_indicator != definitions.TYPE_INDICATOR_UNION:\n error_message = 'invalid structure member missing name'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n # TODO: detect duplicate names.\n\n data_type = definition_values.get('data_type', None)\n\n type_values = (data_type, type_indicator)\n type_values = [value for value in type_values if value is not None]\n\n if not type_values:\n error_message = (\n 'invalid structure member: {0:s} both data type and type are '\n 'missing').format(name or '')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if len(type_values) > 1:\n error_message = (\n 'invalid structure member: {0:s} data type and type not allowed to '\n 'be set at the same time').format(name or '')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n condition = definition_values.get('condition', None)\n if not supports_conditions and condition:\n error_message = (\n 'invalid structure member: {0:s} unsupported condition').format(\n name or '')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n value = definition_values.get('value', None)\n values = definition_values.get('values', None)\n\n if None not in (value, values):\n error_message = (\n 'invalid structure member: {0:s} value and values not allowed to '\n 'be set at the same time').format(name or '')\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if value:\n values = [value]\n\n supported_values = None\n if values:\n supported_values = []\n for value in values:\n if isinstance(value, py2to3.UNICODE_TYPE):\n value = value.encode('ascii')\n\n supported_values.append(value)\n\n if type_indicator is not None:\n data_type_callback = self._DATA_TYPE_CALLBACKS.get(type_indicator, None)\n if data_type_callback:\n data_type_callback = getattr(self, data_type_callback, None)\n if not data_type_callback:\n error_message = 'unuspported data type definition: {0:s}.'.format(\n type_indicator)\n raise errors.DefinitionReaderError(name, error_message)\n\n try:\n data_type_definition = data_type_callback(\n definitions_registry, definition_values, name, is_member=True)\n except errors.DefinitionReaderError as exception:\n error_message = 'in: {0:s} {1:s}'.format(\n exception.name or '', exception.message)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if condition or supported_values:\n definition_object = data_types.MemberDataTypeDefinition(\n name, data_type_definition, condition=condition,\n values=supported_values)\n else:\n definition_object = data_type_definition\n\n elif data_type is not None:\n data_type_definition = definitions_registry.GetDefinitionByName(\n data_type)\n if not data_type_definition:\n error_message = (\n 'invalid structure member: {0:s} undefined data type: '\n '{1:s}').format(name or '', data_type)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n unsupported_definition_values = set(definition_values.keys()).difference(\n self._SUPPORTED_DEFINITION_VALUES_MEMBER_DATA_TYPE)\n if unsupported_definition_values:\n error_message = 'unsupported definition values: {0:s}'.format(\n ', '.join(unsupported_definition_values))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n aliases = definition_values.get('aliases', None)\n description = definition_values.get('description', None)\n\n definition_object = data_types.MemberDataTypeDefinition(\n name, data_type_definition, aliases=aliases, condition=condition,\n data_type=data_type, description=description, values=supported_values)\n\n return definition_object", "response": "Reads a member data type definition member."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreads a padding data type definition.", "response": "def _ReadPaddingDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a padding data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n PaddingtDefinition: padding definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if not is_member:\n error_message = 'data type only supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object = self._ReadDataTypeDefinition(\n definitions_registry, definition_values, data_types.PaddingDefinition,\n definition_name, self._SUPPORTED_DEFINITION_VALUES_PADDING)\n\n alignment_size = definition_values.get('alignment_size', None)\n if not alignment_size:\n error_message = 'missing alignment_size'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n try:\n int(alignment_size)\n except ValueError:\n error_message = 'unuspported alignment size attribute: {0!s}'.format(\n alignment_size)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if alignment_size not in (2, 4, 8, 16):\n error_message = 'unuspported alignment size value: {0!s}'.format(\n alignment_size)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.alignment_size = alignment_size\n\n return definition_object"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread a semantic data type definition.", "response": "def _ReadSemanticDataTypeDefinition(\n self, definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values):\n \"\"\"Reads a semantic data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_definition_values (set[str]): names of the supported definition\n values.\n\n Returns:\n SemanticDataTypeDefinition: semantic data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n return self._ReadDataTypeDefinition(\n definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ReadSequenceDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a sequence data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n SequenceDefinition: sequence data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_ELEMENTS_MEMBER_DATA_TYPE)\n else:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_ELEMENTS_DATA_TYPE)\n\n return self._ReadElementSequenceDataTypeDefinition(\n definitions_registry, definition_values, data_types.SequenceDefinition,\n definition_name, supported_definition_values)", "response": "Reads a sequence data type definition."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _ReadStorageDataTypeDefinition(\n self, definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_attributes, is_member=False):\n \"\"\"Reads a storage data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n data_type_definition_class (str): data type definition class.\n definition_name (str): name of the definition.\n supported_attributes (set[str]): names of the supported attributes.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n StorageDataTypeDefinition: storage data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_MEMBER_DATA_TYPE)\n else:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_STORAGE_DATA_TYPE)\n\n definition_object = self._ReadDataTypeDefinition(\n definitions_registry, definition_values, data_type_definition_class,\n definition_name, supported_definition_values)\n\n attributes = definition_values.get('attributes', None)\n if attributes:\n unsupported_attributes = set(attributes.keys()).difference(\n supported_attributes)\n if unsupported_attributes:\n error_message = 'unsupported attributes: {0:s}'.format(\n ', '.join(unsupported_attributes))\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n byte_order = attributes.get('byte_order', definitions.BYTE_ORDER_NATIVE)\n if byte_order not in definitions.BYTE_ORDERS:\n error_message = 'unsupported byte-order attribute: {0!s}'.format(\n byte_order)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.byte_order = byte_order\n\n return definition_object", "response": "Reads a storage data type definition."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _ReadStreamDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a stream data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n StreamDefinition: stream data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_ELEMENTS_MEMBER_DATA_TYPE)\n else:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_ELEMENTS_DATA_TYPE)\n\n return self._ReadElementSequenceDataTypeDefinition(\n definitions_registry, definition_values, data_types.StreamDefinition,\n definition_name, supported_definition_values)", "response": "Reads a data type definition."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ReadStringDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a string data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n StringDefinition: string data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n supported_definition_values = (\n self._SUPPORTED_DEFINITION_VALUES_STRING_MEMBER)\n else:\n supported_definition_values = self._SUPPORTED_DEFINITION_VALUES_STRING\n\n definition_object = self._ReadElementSequenceDataTypeDefinition(\n definitions_registry, definition_values, data_types.StringDefinition,\n definition_name, supported_definition_values)\n\n encoding = definition_values.get('encoding', None)\n if not encoding:\n error_message = 'missing encoding'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.encoding = encoding\n\n return definition_object", "response": "Reads a string data type definition."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread a structure data type definition.", "response": "def _ReadStructureDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a structure data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n StructureDefinition: structure data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n error_message = 'data type not supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n return self._ReadDataTypeDefinitionWithMembers(\n definitions_registry, definition_values, data_types.StructureDefinition,\n definition_name, supports_conditions=True)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ReadStructureFamilyDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads a structure family data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n StructureDefinition: structure data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if is_member:\n error_message = 'data type not supported as member'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object = self._ReadLayoutDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.StructureFamilyDefinition, definition_name,\n self._SUPPORTED_DEFINITION_VALUES_STRUCTURE_FAMILY)\n\n runtime = definition_values.get('runtime', None)\n if not runtime:\n error_message = 'missing runtime'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n runtime_data_type_definition = definitions_registry.GetDefinitionByName(\n runtime)\n if not runtime_data_type_definition:\n error_message = 'undefined runtime: {0:s}.'.format(runtime)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if runtime_data_type_definition.family_definition:\n error_message = 'runtime: {0:s} already part of a family.'.format(runtime)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.AddRuntimeDefinition(runtime_data_type_definition)\n\n members = definition_values.get('members', None)\n if not members:\n error_message = 'missing members'\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n for member in members:\n member_data_type_definition = definitions_registry.GetDefinitionByName(\n member)\n if not member_data_type_definition:\n error_message = 'undefined member: {0:s}.'.format(member)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n if member_data_type_definition.family_definition:\n error_message = 'member: {0:s} already part of a family.'.format(member)\n raise errors.DefinitionReaderError(definition_name, error_message)\n\n definition_object.AddMemberDefinition(member_data_type_definition)\n\n return definition_object", "response": "Reads a structure family data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _ReadUnionDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads an union data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n UnionDefinition: union data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n return self._ReadDataTypeDefinitionWithMembers(\n definitions_registry, definition_values, data_types.UnionDefinition,\n definition_name, supports_conditions=False)", "response": "Reads a union data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreads an UUID data type definition.", "response": "def _ReadUUIDDataTypeDefinition(\n self, definitions_registry, definition_values, definition_name,\n is_member=False):\n \"\"\"Reads an UUID data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n definition_name (str): name of the definition.\n is_member (Optional[bool]): True if the data type definition is a member\n data type definition.\n\n Returns:\n UUIDDataTypeDefinition: UUID data type definition.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n return self._ReadFixedSizeDataTypeDefinition(\n definitions_registry, definition_values,\n data_types.UUIDDefinition, definition_name,\n self._SUPPORTED_ATTRIBUTES_FIXED_SIZE_DATA_TYPE, default_size=16,\n is_member=is_member, supported_size_values=(16, ))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading a data type definition from the given definitions registry and returns it.", "response": "def _ReadDefinition(self, definitions_registry, definition_values):\n \"\"\"Reads a data type definition.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n definition_values (dict[str, object]): definition values.\n\n Returns:\n DataTypeDefinition: data type definition or None.\n\n Raises:\n DefinitionReaderError: if the definitions values are missing or if\n the format is incorrect.\n \"\"\"\n if not definition_values:\n error_message = 'missing definition values'\n raise errors.DefinitionReaderError(None, error_message)\n\n name = definition_values.get('name', None)\n if not name:\n error_message = 'missing name'\n raise errors.DefinitionReaderError(None, error_message)\n\n type_indicator = definition_values.get('type', None)\n if not type_indicator:\n error_message = 'invalid definition missing type'\n raise errors.DefinitionReaderError(name, error_message)\n\n data_type_callback = self._DATA_TYPE_CALLBACKS.get(type_indicator, None)\n if data_type_callback:\n data_type_callback = getattr(self, data_type_callback, None)\n if not data_type_callback:\n error_message = 'unuspported data type definition: {0:s}.'.format(\n type_indicator)\n raise errors.DefinitionReaderError(name, error_message)\n\n return data_type_callback(definitions_registry, definition_values, name)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ReadFile(self, definitions_registry, path):\n with open(path, 'r') as file_object:\n self.ReadFileObject(definitions_registry, file_object)", "response": "Reads data type definitions from a file into the registry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nretrieve a format error location.", "response": "def _GetFormatErrorLocation(\n self, yaml_definition, last_definition_object):\n \"\"\"Retrieves a format error location.\n\n Args:\n yaml_definition (dict[str, object]): current YAML definition.\n last_definition_object (DataTypeDefinition): previous data type\n definition.\n\n Returns:\n str: format error location.\n \"\"\"\n name = yaml_definition.get('name', None)\n if name:\n error_location = 'in: {0:s}'.format(name or '')\n elif last_definition_object:\n error_location = 'after: {0:s}'.format(last_definition_object.name)\n else:\n error_location = 'at start'\n\n return error_location"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading data type definitions from a file - like object into the data type definitions registry.", "response": "def ReadFileObject(self, definitions_registry, file_object):\n \"\"\"Reads data type definitions from a file-like object into the registry.\n\n Args:\n definitions_registry (DataTypeDefinitionsRegistry): data type definitions\n registry.\n file_object (file): file-like object to read from.\n\n Raises:\n FormatError: if the definitions values are missing or if the format is\n incorrect.\n \"\"\"\n last_definition_object = None\n error_location = None\n error_message = None\n\n try:\n yaml_generator = yaml.safe_load_all(file_object)\n\n for yaml_definition in yaml_generator:\n definition_object = self._ReadDefinition(\n definitions_registry, yaml_definition)\n if not definition_object:\n error_location = self._GetFormatErrorLocation(\n yaml_definition, last_definition_object)\n error_message = '{0:s} Missing definition object.'.format(\n error_location)\n raise errors.FormatError(error_message)\n\n definitions_registry.RegisterDefinition(definition_object)\n last_definition_object = definition_object\n\n except errors.DefinitionReaderError as exception:\n error_message = 'in: {0:s} {1:s}'.format(\n exception.name or '', exception.message)\n raise errors.FormatError(error_message)\n\n except (yaml.reader.ReaderError, yaml.scanner.ScannerError) as exception:\n error_location = self._GetFormatErrorLocation({}, last_definition_object)\n error_message = '{0:s} {1!s}'.format(error_location, exception)\n raise errors.FormatError(error_message)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfinds files with an optional extension in a given path.", "response": "def find_files(path, filter=\"*.md\"):\n \"\"\" Finds files with an (optional) given extension in a given path. \"\"\"\n if os.path.isfile(path):\n return [path]\n\n if os.path.isdir(path):\n matches = []\n for root, dirnames, filenames in os.walk(path):\n for filename in fnmatch.filter(filenames, filter):\n matches.append(os.path.join(root, filename))\n return matches"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreads the organization given by identifier from HDX and returns the Organization object if successful.", "response": "def read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['Organization']\n \"\"\"Reads the organization given by identifier from HDX and returns Organization object\n\n Args:\n identifier (str): Identifier of organization\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[Organization]: Organization object if successful read, None if not\n \"\"\"\n\n organization = Organization(configuration=configuration)\n result = organization._load_from_hdx('organization', identifier)\n if result:\n return organization\n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_users(self, capacity=None):\n # type: (Optional[str]) -> List[User]\n \"\"\"Returns the organization's users.\n\n Args:\n capacity (Optional[str]): Filter by capacity eg. member, admin. Defaults to None.\n Returns:\n List[User]: Organization's users.\n \"\"\"\n users = list()\n usersdicts = self.data.get('users')\n if usersdicts is not None:\n for userdata in usersdicts:\n if capacity is not None and userdata['capacity'] != capacity:\n continue\n id = userdata.get('id')\n if id is None:\n id = userdata['name']\n user = hdx.data.user.User.read_from_hdx(id, configuration=self.configuration)\n user['capacity'] = userdata['capacity']\n users.append(user)\n return users", "response": "Returns the organization s users."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds or update a user in organization with new metadata.", "response": "def add_update_user(self, user, capacity=None):\n # type: (Union[hdx.data.user.User,Dict,str],Optional[str]) -> None\n \"\"\"Add new or update existing user in organization with new metadata. Capacity eg. member, admin\n must be supplied either within the User object or dictionary or using the capacity argument (which takes\n precedence).\n\n Args:\n user (Union[User,Dict,str]): Either a user id or user metadata either from a User object or a dictionary\n capacity (Optional[str]): Capacity of user eg. member, admin. Defaults to None.\n\n Returns:\n None\n\n \"\"\"\n if isinstance(user, str):\n user = hdx.data.user.User.read_from_hdx(user, configuration=self.configuration)\n elif isinstance(user, dict):\n user = hdx.data.user.User(user, configuration=self.configuration)\n if isinstance(user, hdx.data.user.User):\n users = self.data.get('users')\n if users is None:\n users = list()\n self.data['users'] = users\n if capacity is not None:\n user['capacity'] = capacity\n self._addupdate_hdxobject(users, 'name', user)\n return\n raise HDXError('Type %s cannot be added as a user!' % type(user).__name__)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_update_users(self, users, capacity=None):\n # type: (List[Union[hdx.data.user.User,Dict,str]],Optional[str]) -> None\n \"\"\"Add new or update existing users in organization with new metadata. Capacity eg. member, admin\n must be supplied either within the User object or dictionary or using the capacity argument (which takes\n precedence).\n\n Args:\n users (List[Union[User,Dict,str]]): A list of either user ids or users metadata from User objects or dictionaries\n capacity (Optional[str]): Capacity of users eg. member, admin. Defaults to None.\n\n Returns:\n None\n \"\"\"\n if not isinstance(users, list):\n raise HDXError('Users should be a list!')\n for user in users:\n self.add_update_user(user, capacity)", "response": "Adds or updates existing users in organization with new metadata."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_datasets(self, query='*:*', **kwargs):\n # type: (str, Any) -> List[hdx.data.dataset.Dataset]\n \"\"\"Get list of datasets in organization\n\n Args:\n query (str): Restrict datasets returned to this query (in Solr format). Defaults to '*:*'.\n **kwargs: See below\n sort (string): Sorting of the search results. Defaults to 'relevance asc, metadata_modified desc'.\n rows (int): Number of matching rows to return. Defaults to all datasets (sys.maxsize).\n start (int): Offset in the complete result for where the set of returned datasets should begin\n facet (string): Whether to enable faceted results. Default to True.\n facet.mincount (int): Minimum counts for facet fields should be included in the results\n facet.limit (int): Maximum number of values the facet fields return (- = unlimited). Defaults to 50.\n facet.field (List[str]): Fields to facet upon. Default is empty.\n use_default_schema (bool): Use default package schema instead of custom schema. Defaults to False.\n\n Returns:\n List[Dataset]: List of datasets in organization\n \"\"\"\n return hdx.data.dataset.Dataset.search_in_hdx(query=query,\n configuration=self.configuration,\n fq='organization:%s' % self.data['name'], **kwargs)", "response": "Get list of datasets in organization"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_organization_names(configuration=None, **kwargs):\n # type: (Optional[Configuration], Any) -> List[str]\n \"\"\"Get all organization names in HDX\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n **kwargs: See below\n sort (str): Sort the search results according to field name and sort-order. Allowed fields are \u2018name\u2019, \u2018package_count\u2019 and \u2018title\u2019. Defaults to 'name asc'.\n organizations (List[str]): List of names of the groups to return.\n all_fields (bool): Return group dictionaries instead of just names. Only core fields are returned - get some more using the include_* options. Defaults to False.\n include_extras (bool): If all_fields, include the group extra fields. Defaults to False.\n include_tags (bool): If all_fields, include the group tags. Defaults to False.\n include_groups: If all_fields, include the groups the groups are in. Defaults to False.\n\n Returns:\n List[str]: List of all organization names in HDX\n \"\"\"\n organization = Organization(configuration=configuration)\n organization['id'] = 'all organizations' # only for error message if produced\n return organization._write_to_hdx('list', kwargs, 'id')", "response": "Get all organization names in HDX"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmakes a read call to HDX passing in given parameter.", "response": "def _read_from_hdx(self, object_type, value, fieldname='id',\n action=None, **kwargs):\n # type: (str, str, str, Optional[str], Any) -> Tuple[bool, Union[Dict, str]]\n \"\"\"Makes a read call to HDX passing in given parameter.\n\n Args:\n object_type (str): Description of HDX object type (for messages)\n value (str): Value of HDX field\n fieldname (str): HDX field name. Defaults to id.\n action (Optional[str]): Replacement CKAN action url to use. Defaults to None.\n **kwargs: Other fields to pass to CKAN.\n\n Returns:\n Tuple[bool, Union[Dict, str]]: (True/False, HDX object metadata/Error)\n \"\"\"\n if not fieldname:\n raise HDXError('Empty %s field name!' % object_type)\n if action is None:\n action = self.actions()['show']\n data = {fieldname: value}\n data.update(kwargs)\n try:\n result = self.configuration.call_remoteckan(action, data)\n return True, result\n except NotFound:\n return False, '%s=%s: not found!' % (fieldname, value)\n except Exception as e:\n raisefrom(HDXError, 'Failed when trying to read: %s=%s! (POST)' % (fieldname, value), e)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if HDX object exists and if so load it.", "response": "def _check_load_existing_object(self, object_type, id_field_name, operation='update'):\n # type: (str, str, str) -> None\n \"\"\"Check metadata exists and contains HDX object identifier, and if so load HDX object\n\n Args:\n object_type (str): Description of HDX object type (for messages)\n id_field_name (str): Name of field containing HDX object identifier\n operation (str): Operation to report if error. Defaults to update.\n\n Returns:\n None\n \"\"\"\n self._check_existing_object(object_type, id_field_name)\n if not self._load_from_hdx(object_type, self.data[id_field_name]):\n raise HDXError('No existing %s to %s!' % (object_type, operation))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate or updates an HDX object in HDX and return HDX object metadata dict Args: action (str): Action to perform eg. 'create', 'update' data (Dict): Data to write to HDX id_field_name (str): Name of field containing HDX object identifier or None file_to_upload (Optional[str]): File to upload to HDX Returns: Dict: HDX object metadata", "response": "def _write_to_hdx(self, action, data, id_field_name, file_to_upload=None):\n # type: (str, Dict, str, Optional[str]) -> Dict\n \"\"\"Creates or updates an HDX object in HDX and return HDX object metadata dict\n\n Args:\n action (str): Action to perform eg. 'create', 'update'\n data (Dict): Data to write to HDX\n id_field_name (str): Name of field containing HDX object identifier or None\n file_to_upload (Optional[str]): File to upload to HDX\n\n Returns:\n Dict: HDX object metadata\n \"\"\"\n file = None\n try:\n if file_to_upload:\n file = open(file_to_upload, 'rb')\n files = [('upload', file)]\n else:\n files = None\n return self.configuration.call_remoteckan(self.actions()[action], data, files=files)\n except Exception as e:\n raisefrom(HDXError, 'Failed when trying to %s %s! (POST)' % (action, data[id_field_name]), e)\n finally:\n if file_to_upload and file:\n file.close()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsaving or updates an HDX object in HDX and returns None", "response": "def _save_to_hdx(self, action, id_field_name, file_to_upload=None):\n # type: (str, str, Optional[str]) -> None\n \"\"\"Creates or updates an HDX object in HDX, saving current data and replacing with returned HDX object data\n from HDX\n\n Args:\n action (str): Action to perform: 'create' or 'update'\n id_field_name (str): Name of field containing HDX object identifier\n file_to_upload (Optional[str]): File to upload to HDX\n\n Returns:\n None\n \"\"\"\n result = self._write_to_hdx(action, self.data, id_field_name, file_to_upload)\n self.old_data = self.data\n self.data = result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _delete_from_hdx(self, object_type, id_field_name):\n # type: (str, str) -> None\n \"\"\"Helper method to deletes a resource from HDX\n\n Args:\n object_type (str): Description of HDX object type (for messages)\n id_field_name (str): Name of field containing HDX object identifier\n\n Returns:\n None\n \"\"\"\n if id_field_name not in self.data:\n raise HDXError('No %s field (mandatory) in %s!' % (id_field_name, object_type))\n self._save_to_hdx('delete', id_field_name)", "response": "Internal method to delete a resource from HDX"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _remove_hdxobject(self, objlist, obj, matchon='id', delete=False):\n # type: (List[Union[HDXObjectUpperBound,Dict]], Union[HDXObjectUpperBound,Dict,str], str, bool) -> bool\n \"\"\"Remove an HDX object from a list within the parent HDX object\n\n Args:\n objlist (List[Union[T <= HDXObject,Dict]]): list of HDX objects\n obj (Union[T <= HDXObject,Dict,str]): Either an id or hdx object metadata either from an HDX object or a dictionary\n matchon (str): Field to match on. Defaults to id.\n delete (bool): Whether to delete HDX object. Defaults to False.\n\n Returns:\n bool: True if object removed, False if not\n \"\"\"\n if objlist is None:\n return False\n if isinstance(obj, six.string_types):\n obj_id = obj\n elif isinstance(obj, dict) or isinstance(obj, HDXObject):\n obj_id = obj.get(matchon)\n else:\n raise HDXError('Type of object not a string, dict or T<=HDXObject')\n if not obj_id:\n return False\n for i, objdata in enumerate(objlist):\n objid = objdata.get(matchon)\n if objid and objid == obj_id:\n if delete:\n objlist[i].delete_from_hdx()\n del objlist[i]\n return True\n return False", "response": "Remove an HDX object from a list within the parent HDX object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _convert_hdxobjects(self, hdxobjects):\n # type: (List[HDXObjectUpperBound]) -> List[HDXObjectUpperBound]\n \"\"\"Helper function to convert supplied list of HDX objects to a list of dict\n\n Args:\n hdxobjects (List[T <= HDXObject]): List of HDX objects to convert\n\n Returns:\n List[Dict]: List of HDX objects converted to simple dictionaries\n \"\"\"\n newhdxobjects = list()\n for hdxobject in hdxobjects:\n newhdxobjects.append(hdxobject.data)\n return newhdxobjects", "response": "Helper function to convert a list of HDX objects to a list of dict containing HDX objects"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _copy_hdxobjects(self, hdxobjects, hdxobjectclass, attribute_to_copy=None):\n # type: (List[HDXObjectUpperBound], type, Optional[str]) -> List[HDXObjectUpperBound]\n \"\"\"Helper function to make a deep copy of a supplied list of HDX objects\n\n Args:\n hdxobjects (List[T <= HDXObject]): list of HDX objects to copy\n hdxobjectclass (type): Type of the HDX Objects to be copied\n attribute_to_copy (Optional[str]): An attribute to copy over from the HDX object. Defaults to None.\n\n Returns:\n List[T <= HDXObject]: Deep copy of list of HDX objects\n \"\"\"\n newhdxobjects = list()\n for hdxobject in hdxobjects:\n newhdxobjectdata = copy.deepcopy(hdxobject.data)\n newhdxobject = hdxobjectclass(newhdxobjectdata, configuration=self.configuration)\n if attribute_to_copy:\n value = getattr(hdxobject, attribute_to_copy)\n setattr(newhdxobject, attribute_to_copy, value)\n newhdxobjects.append(newhdxobject)\n return newhdxobjects", "response": "Helper function to make a deep copy of a list of HDX objects"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _separate_hdxobjects(self, hdxobjects, hdxobjects_name, id_field, hdxobjectclass):\n # type: (List[HDXObjectUpperBound], str, str, type) -> None\n \"\"\"Helper function to take a list of HDX objects contained in the internal dictionary and add them to a\n supplied list of HDX objects or update existing metadata if any objects already exist in the list. The list in\n the internal dictionary is then deleted.\n\n Args:\n hdxobjects (List[T <= HDXObject]): list of HDX objects to which to add new objects or update existing ones\n hdxobjects_name (str): Name of key in internal dictionary from which to obtain list of HDX objects\n id_field (str): Field on which to match to determine if object already exists in list\n hdxobjectclass (type): Type of the HDX Object to be added/updated\n\n Returns:\n None\n \"\"\"\n new_hdxobjects = self.data.get(hdxobjects_name, list())\n \"\"\":type : List[HDXObjectUpperBound]\"\"\"\n if new_hdxobjects:\n hdxobject_names = set()\n for hdxobject in hdxobjects:\n hdxobject_name = hdxobject[id_field]\n hdxobject_names.add(hdxobject_name)\n for new_hdxobject in new_hdxobjects:\n if hdxobject_name == new_hdxobject[id_field]:\n merge_two_dictionaries(hdxobject, new_hdxobject)\n break\n for new_hdxobject in new_hdxobjects:\n if not new_hdxobject[id_field] in hdxobject_names:\n hdxobjects.append(hdxobjectclass(new_hdxobject, configuration=self.configuration))\n del self.data[hdxobjects_name]", "response": "This function is used to separate a list of HDX objects from the internal dictionary and add them to the internal dictionary if any objects already exist in the internal dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_tags(self):\n # type: () -> List[str]\n \"\"\"Return the dataset's list of tags\n\n Returns:\n List[str]: list of tags or [] if there are none\n \"\"\"\n tags = self.data.get('tags', None)\n if not tags:\n return list()\n return [x['name'] for x in tags]", "response": "Return the dataset s list of tags"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _add_tag(self, tag):\n # type: (str) -> bool\n \"\"\"Add a tag\n\n Args:\n tag (str): Tag to add\n\n Returns:\n bool: True if tag added or False if tag already present\n \"\"\"\n tags = self.data.get('tags', None)\n if tags:\n if tag in [x['name'] for x in tags]:\n return False\n else:\n tags = list()\n tags.append({'name': tag})\n self.data['tags'] = tags\n return True", "response": "Add a tag to the internal data structure."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _add_tags(self, tags):\n # type: (List[str]) -> bool\n \"\"\"Add a list of tag\n\n Args:\n tags (List[str]): list of tags to add\n\n Returns:\n bool: True if all tags added or False if any already present.\n \"\"\"\n alltagsadded = True\n for tag in tags:\n if not self._add_tag(tag):\n alltagsadded = False\n return alltagsadded", "response": "Add a list of tags to the internal cache."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_stringlist_from_commastring(self, field):\n # type: (str) -> List[str]\n \"\"\"Return list of strings from comma separated list\n\n Args:\n field (str): Field containing comma separated list\n\n Returns:\n List[str]: List of strings\n \"\"\"\n strings = self.data.get(field)\n if strings:\n return strings.split(',')\n else:\n return list()", "response": "Returns list of strings from comma separated list\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a string to a comma separated list of strings", "response": "def _add_string_to_commastring(self, field, string):\n # type: (str, str) -> bool\n \"\"\"Add a string to a comma separated list of strings\n\n Args:\n field (str): Field containing comma separated list\n string (str): String to add\n\n Returns:\n bool: True if string added or False if string already present\n \"\"\"\n if string in self._get_stringlist_from_commastring(field):\n return False\n strings = '%s,%s' % (self.data.get(field, ''), string)\n if strings[0] == ',':\n strings = strings[1:]\n self.data[field] = strings\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a list of strings to a comma separated list of strings", "response": "def _add_strings_to_commastring(self, field, strings):\n # type: (str, List[str]) -> bool\n \"\"\"Add a list of strings to a comma separated list of strings\n\n Args:\n field (str): Field containing comma separated list\n strings (List[str]): list of strings to add\n\n Returns:\n bool: True if all strings added or False if any already present.\n \"\"\"\n allstringsadded = True\n for string in strings:\n if not self._add_string_to_commastring(field, string):\n allstringsadded = False\n return allstringsadded"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving a string from a comma separated list of strings", "response": "def _remove_string_from_commastring(self, field, string):\n # type: (str, str) -> bool\n \"\"\"Remove a string from a comma separated list of strings\n\n Args:\n field (str): Field containing comma separated list\n string (str): String to remove\n\n Returns:\n bool: True if string removed or False if not\n \"\"\"\n commastring = self.data.get(field, '')\n if string in commastring:\n self.data[field] = commastring.replace(string, '')\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread the resource given by identifier from HDX and returns Resource object if successful.", "response": "def read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['Resource']\n \"\"\"Reads the resource given by identifier from HDX and returns Resource object\n\n Args:\n identifier (str): Identifier of resource\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[Resource]: Resource object if successful read, None if not\n \"\"\"\n\n if is_valid_uuid(identifier) is False:\n raise HDXError('%s is not a valid resource id!' % identifier)\n resource = Resource(configuration=configuration)\n result = resource._load_from_hdx('resource', identifier)\n if result:\n return resource\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_file_to_upload(self, file_to_upload):\n # type: (str) -> None\n \"\"\"Delete any existing url and set the file uploaded to the local path provided\n\n Args:\n file_to_upload (str): Local path to file to upload\n\n Returns:\n None\n \"\"\"\n if 'url' in self.data:\n del self.data['url']\n self.file_to_upload = file_to_upload", "response": "Delete any existing url and set the file uploaded to the local path provided\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef check_url_filetoupload(self):\n # type: () -> None\n \"\"\"Check if url or file to upload provided for resource and add resource_type and url_type if not supplied\n\n Returns:\n None\n \"\"\"\n if self.file_to_upload is None:\n if 'url' in self.data:\n if 'resource_type' not in self.data:\n self.data['resource_type'] = 'api'\n if 'url_type' not in self.data:\n self.data['url_type'] = 'api'\n else:\n raise HDXError('Either a url or a file to upload must be supplied!')\n else:\n if 'url' in self.data:\n if self.data['url'] != hdx.data.dataset.Dataset.temporary_url:\n raise HDXError('Either a url or a file to upload must be supplied not both!')\n if 'resource_type' not in self.data:\n self.data['resource_type'] = 'file.upload'\n if 'url_type' not in self.data:\n self.data['url_type'] = 'upload'\n if 'tracking_summary' in self.data:\n del self.data['tracking_summary']", "response": "Check if url or file to upload is supplied for resource and add resource_type and url_type"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nupdates the resource in HDX if it exists in HDX and if so update it.", "response": "def update_in_hdx(self, **kwargs):\n # type: (Any) -> None\n \"\"\"Check if resource exists in HDX and if so, update it\n\n Args:\n **kwargs: See below\n operation (string): Operation to perform eg. patch. Defaults to update.\n\n Returns:\n None\n \"\"\"\n self._check_load_existing_object('resource', 'id')\n if self.file_to_upload and 'url' in self.data:\n del self.data['url']\n self._merge_hdx_update('resource', 'id', self.file_to_upload, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_in_hdx(self):\n # type: () -> None\n \"\"\"Check if resource exists in HDX and if so, update it, otherwise create it\n\n Returns:\n None\n \"\"\"\n self.check_required_fields()\n id = self.data.get('id')\n if id and self._load_from_hdx('resource', id):\n logger.warning('%s exists. Updating %s' % ('resource', id))\n if self.file_to_upload and 'url' in self.data:\n del self.data['url']\n self._merge_hdx_update('resource', 'id', self.file_to_upload)\n else:\n self._save_to_hdx('create', 'name', self.file_to_upload)", "response": "Check if resource exists in HDX and if so update it otherwise create it."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_dataset(self):\n # type: () -> hdx.data.dataset.Dataset\n \"\"\"Return dataset containing this resource\n\n Returns:\n hdx.data.dataset.Dataset: Dataset containing this resource\n \"\"\"\n package_id = self.data.get('package_id')\n if package_id is None:\n raise HDXError('Resource has no package id!')\n return hdx.data.dataset.Dataset.read_from_hdx(package_id)", "response": "Returns the dataset containing this resource."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef search_in_hdx(query, configuration=None, **kwargs):\n # type: (str, Optional[Configuration], Any) -> List['Resource']\n \"\"\"Searches for resources in HDX. NOTE: Does not search dataset metadata!\n\n Args:\n query (str): Query\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n **kwargs: See below\n order_by (str): A field on the Resource model that orders the results\n offset (int): Apply an offset to the query\n limit (int): Apply a limit to the query\n Returns:\n List[Resource]: List of resources resulting from query\n \"\"\"\n\n resources = []\n resource = Resource(configuration=configuration)\n success, result = resource._read_from_hdx('resource', query, 'query', Resource.actions()['search'])\n if result:\n count = result.get('count', None)\n if count:\n for resourcedict in result['results']:\n resource = Resource(resourcedict, configuration=configuration)\n resources.append(resource)\n else:\n logger.debug(result)\n return resources", "response": "Searches for resources in HDX."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndownload the resource store to provided folder or temporary folder if no folder supplied", "response": "def download(self, folder=None):\n # type: (Optional[str]) -> Tuple[str, str]\n \"\"\"Download resource store to provided folder or temporary folder if no folder supplied\n\n Args:\n folder (Optional[str]): Folder to download resource to. Defaults to None.\n\n Returns:\n Tuple[str, str]: (URL downloaded, Path to downloaded file)\n\n \"\"\"\n # Download the resource\n url = self.data.get('url', None)\n if not url:\n raise HDXError('No URL to download!')\n logger.debug('Downloading %s' % url)\n filename = self.data['name']\n format = '.%s' % self.data['format']\n if format not in filename:\n filename = '%s%s' % (filename, format)\n with Download(full_agent=self.configuration.get_user_agent()) as downloader:\n path = downloader.download_file(url, folder, filename)\n return url, path"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget list of resources that have a datastore returning their ids.", "response": "def get_all_resource_ids_in_datastore(configuration=None):\n # type: (Optional[Configuration]) -> List[str]\n \"\"\"Get list of resources that have a datastore returning their ids.\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n List[str]: List of resource ids that are in the datastore\n \"\"\"\n resource = Resource(configuration=configuration)\n success, result = resource._read_from_hdx('datastore', '_table_metadata', 'resource_id',\n Resource.actions()['datastore_search'], limit=10000)\n resource_ids = list()\n if not success:\n logger.debug(result)\n else:\n for record in result['records']:\n resource_ids.append(record['name'])\n return resource_ids"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if the resource has a datastore.", "response": "def has_datastore(self):\n # type: () -> bool\n \"\"\"Check if the resource has a datastore.\n\n Returns:\n bool: Whether the resource has a datastore or not\n \"\"\"\n success, result = self._read_from_hdx('datastore', self.data['id'], 'resource_id',\n self.actions()['datastore_search'])\n if not success:\n logger.debug(result)\n else:\n if result:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_datastore(self):\n # type: () -> None\n \"\"\"Delete a resource from the HDX datastore\n\n Returns:\n None\n \"\"\"\n success, result = self._read_from_hdx('datastore', self.data['id'], 'resource_id',\n self.actions()['datastore_delete'],\n force=True)\n if not success:\n logger.debug(result)", "response": "Delete a resource from the HDX datastore"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_datastore(self, schema=None, primary_key=None,\n delete_first=0, path=None):\n # type: (Optional[List[Dict]], Optional[str], int, Optional[str]) -> None\n \"\"\"For tabular data, create a resource in the HDX datastore which enables data preview in HDX. If no schema is provided\n all fields are assumed to be text. If path is not supplied, the file is first downloaded from HDX.\n\n Args:\n schema (List[Dict]): List of fields and types of form {'id': 'FIELD', 'type': 'TYPE'}. Defaults to None.\n primary_key (Optional[str]): Primary key of schema. Defaults to None.\n delete_first (int): Delete datastore before creation. 0 = No, 1 = Yes, 2 = If no primary key. Defaults to 0.\n path (Optional[str]): Local path to file that was uploaded. Defaults to None.\n\n Returns:\n None\n \"\"\"\n if delete_first == 0:\n pass\n elif delete_first == 1:\n self.delete_datastore()\n elif delete_first == 2:\n if primary_key is None:\n self.delete_datastore()\n else:\n raise HDXError('delete_first must be 0, 1 or 2! (0 = No, 1 = Yes, 2 = Delete if no primary key)')\n if path is None:\n # Download the resource\n url, path = self.download()\n delete_after_download = True\n else:\n url = path\n delete_after_download = False\n\n def convert_to_text(extended_rows):\n for number, headers, row in extended_rows:\n for i, val in enumerate(row):\n row[i] = str(val)\n yield (number, headers, row)\n\n with Download(full_agent=self.configuration.get_user_agent()) as downloader:\n try:\n stream = downloader.get_tabular_stream(path, headers=1, post_parse=[convert_to_text],\n bytes_sample_size=1000000)\n nonefieldname = False\n if schema is None:\n schema = list()\n for fieldname in stream.headers:\n if fieldname is not None:\n schema.append({'id': fieldname, 'type': 'text'})\n else:\n nonefieldname = True\n data = {'resource_id': self.data['id'], 'force': True, 'fields': schema, 'primary_key': primary_key}\n self._write_to_hdx('datastore_create', data, 'resource_id')\n if primary_key is None:\n method = 'insert'\n else:\n method = 'upsert'\n logger.debug('Uploading data from %s to datastore' % url)\n offset = 0\n chunksize = 100\n rowset = stream.read(keyed=True, limit=chunksize)\n while len(rowset) != 0:\n if nonefieldname:\n for row in rowset:\n del row[None]\n data = {'resource_id': self.data['id'], 'force': True, 'method': method, 'records': rowset}\n self._write_to_hdx('datastore_upsert', data, 'resource_id')\n rowset = stream.read(keyed=True, limit=chunksize)\n logger.debug('Uploading: %s' % offset)\n offset += chunksize\n except Exception as e:\n raisefrom(HDXError, 'Upload to datastore of %s failed!' % url, e)\n finally:\n if delete_after_download:\n remove(path)", "response": "Create a resource in the HDX datastore which enables data preview in HDX."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_datastore_from_dict_schema(self, data, delete_first=0, path=None):\n # type: (dict, int, Optional[str]) -> None\n \"\"\"For tabular data, create a resource in the HDX datastore which enables data preview in HDX from a dictionary\n containing a list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'} and optionally a primary key.\n If path is not supplied, the file is first downloaded from HDX.\n\n Args:\n data (dict): Dictionary containing list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'}\n delete_first (int): Delete datastore before creation. 0 = No, 1 = Yes, 2 = If no primary key. Defaults to 0.\n path (Optional[str]): Local path to file that was uploaded. Defaults to None.\n\n Returns:\n None\n \"\"\"\n schema = data['schema']\n primary_key = data.get('primary_key')\n self.create_datastore(schema, primary_key, delete_first, path=path)", "response": "Create a resource in the HDX datastore from a dictionary containing a list of fields and types of form TYPE and optionally a primary key."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a resource in the HDX datastore from a YAML file containing a list of fields and types of form TYPE and optionally a primary key.", "response": "def create_datastore_from_yaml_schema(self, yaml_path, delete_first=0,\n path=None):\n # type: (str, Optional[int], Optional[str]) -> None\n \"\"\"For tabular data, create a resource in the HDX datastore which enables data preview in HDX from a YAML file\n containing a list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'} and optionally a primary key.\n If path is not supplied, the file is first downloaded from HDX.\n\n Args:\n yaml_path (str): Path to YAML file containing list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'}\n delete_first (int): Delete datastore before creation. 0 = No, 1 = Yes, 2 = If no primary key. Defaults to 0.\n path (Optional[str]): Local path to file that was uploaded. Defaults to None.\n\n Returns:\n None\n \"\"\"\n data = load_yaml(yaml_path)\n self.create_datastore_from_dict_schema(data, delete_first, path=path)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_datastore_from_json_schema(self, json_path, delete_first=0, path=None):\n # type: (str, int, Optional[str]) -> None\n \"\"\"For tabular data, create a resource in the HDX datastore which enables data preview in HDX from a JSON file\n containing a list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'} and optionally a primary key.\n If path is not supplied, the file is first downloaded from HDX.\n\n Args:\n json_path (str): Path to JSON file containing list of fields and types of form {'id': 'FIELD', 'type': 'TYPE'}\n delete_first (int): Delete datastore before creation. 0 = No, 1 = Yes, 2 = If no primary key. Defaults to 0.\n path (Optional[str]): Local path to file that was uploaded. Defaults to None.\n\n Returns:\n None\n \"\"\"\n data = load_json(json_path)\n self.create_datastore_from_dict_schema(data, delete_first, path=path)", "response": "For tabular data create a resource in the HDX datastore which enables data preview in HDX from a JSON file containing a list of fields and types of form TYPE and optionally a primary key."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_datastore_for_topline(self, delete_first=0, path=None):\n # type: (int, Optional[str]) -> None\n \"\"\"For tabular data, create a resource in the HDX datastore which enables data preview in HDX using the built in\n YAML definition for a topline. If path is not supplied, the file is first downloaded from HDX.\n\n Args:\n delete_first (int): Delete datastore before creation. 0 = No, 1 = Yes, 2 = If no primary key. Defaults to 0.\n path (Optional[str]): Local path to file that was uploaded. Defaults to None.\n\n Returns:\n None\n \"\"\"\n data = load_yaml(script_dir_plus_file(join('..', 'hdx_datasource_topline.yml'), Resource))\n self.create_datastore_from_dict_schema(data, delete_first, path=path)", "response": "Create a resource in the HDX datastore which enables data preview in HDX using the built in\n YAML definition for a topline."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_resource_view(self, resource_view):\n # type: (Union[ResourceView,Dict]) -> ResourceView\n \"\"\"Get resource view id\n\n Args:\n resource_view (Union[ResourceView,Dict]): ResourceView metadata from a ResourceView object or dictionary\n\n Returns:\n ResourceView: ResourceView object\n \"\"\"\n if isinstance(resource_view, dict):\n resource_view = ResourceView(resource_view, configuration=self.configuration)\n if isinstance(resource_view, ResourceView):\n return resource_view\n raise HDXError('Type %s is not a valid resource view!' % type(resource_view).__name__)", "response": "Get resource view id from a resource view object or dictionary"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_update_resource_views(self, resource_views):\n # type: (List[Union[ResourceView,Dict]]) -> None\n \"\"\"Add new or update existing resource views in resource with new metadata.\n\n Args:\n resource_views (List[Union[ResourceView,Dict]]): A list of resource views metadata from ResourceView objects or dictionaries\n\n Returns:\n None\n \"\"\"\n if not isinstance(resource_views, list):\n raise HDXError('ResourceViews should be a list!')\n for resource_view in resource_views:\n self.add_update_resource_view(resource_view)", "response": "Add new or update existing resource views in resource with new metadata."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreordering resource views in resource.", "response": "def reorder_resource_views(self, resource_views):\n # type: (List[Union[ResourceView,Dict,str]]) -> None\n \"\"\"Order resource views in resource.\n\n Args:\n resource_views (List[Union[ResourceView,Dict,str]]): A list of either resource view ids or resource views metadata from ResourceView objects or dictionaries\n\n Returns:\n None\n \"\"\"\n if not isinstance(resource_views, list):\n raise HDXError('ResourceViews should be a list!')\n ids = list()\n for resource_view in resource_views:\n if isinstance(resource_view, str):\n resource_view_id = resource_view\n else:\n resource_view_id = resource_view['id']\n if is_valid_uuid(resource_view_id) is False:\n raise HDXError('%s is not a valid resource view id!' % resource_view)\n ids.append(resource_view_id)\n _, result = self._read_from_hdx('resource view', self.data['id'], 'id',\n ResourceView.actions()['reorder'], order=ids)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete a resource view from the resource and HDX", "response": "def delete_resource_view(self, resource_view):\n # type: (Union[ResourceView,Dict,str]) -> None\n \"\"\"Delete a resource view from the resource and HDX\n\n Args:\n resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary\n\n Returns:\n None\n \"\"\"\n if isinstance(resource_view, str):\n if is_valid_uuid(resource_view) is False:\n raise HDXError('%s is not a valid resource view id!' % resource_view)\n resource_view = ResourceView({'id': resource_view}, configuration=self.configuration)\n else:\n resource_view = self._get_resource_view(resource_view)\n if 'id' not in resource_view:\n found = False\n title = resource_view.get('title')\n for rv in self.get_resource_views():\n if resource_view['title'] == rv['title']:\n resource_view = rv\n found = True\n break\n if not found:\n raise HDXError('No resource views have title %s in this resource!' % title)\n resource_view.delete_from_hdx()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse_for_simple_stems(output, skip_empty=False, skip_same_stems=True):\n lines_with_stems = _get_lines_with_stems(output)\n stems = list()\n\n last_word = None\n for line in lines_with_stems:\n word, stem, _ = line.split(\"\\t\")\n stem = stem if stem != '-' else None\n\n if skip_empty and (stem is None):\n continue\n\n if last_word != word:\n stems.append((word, []))\n\n ## append new stem only if not on list already\n stem = None if skip_same_stems and stem in stems[-1][1] else stem\n if stem is not None:\n stems[-1][1].append(stem)\n\n last_word = word\n\n return stems", "response": "Parses the output stem lines to produce a list with possible stems for each word in the output."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _CreateClassTemplate(cls, data_type_definition):\n type_name = data_type_definition.name\n\n type_description = data_type_definition.description or type_name\n while type_description.endswith('.'):\n type_description = type_description[:-1]\n\n class_attributes_description = []\n init_arguments = []\n instance_attributes = []\n\n for member_definition in data_type_definition.members:\n attribute_name = member_definition.name\n\n description = member_definition.description or attribute_name\n while description.endswith('.'):\n description = description[:-1]\n\n member_data_type = getattr(member_definition, 'member_data_type', '')\n if isinstance(member_definition, data_types.MemberDataTypeDefinition):\n member_definition = member_definition.member_data_type_definition\n\n member_type_indicator = member_definition.TYPE_INDICATOR\n if member_type_indicator == definitions.TYPE_INDICATOR_SEQUENCE:\n element_type_indicator = member_definition.element_data_type\n member_type_indicator = 'tuple[{0:s}]'.format(element_type_indicator)\n else:\n member_type_indicator = cls._PYTHON_NATIVE_TYPES.get(\n member_type_indicator, member_data_type)\n\n argument = '{0:s}=None'.format(attribute_name)\n\n definition = ' self.{0:s} = {0:s}'.format(attribute_name)\n\n description = ' {0:s} ({1:s}): {2:s}.'.format(\n attribute_name, member_type_indicator, description)\n\n class_attributes_description.append(description)\n init_arguments.append(argument)\n instance_attributes.append(definition)\n\n class_attributes_description = '\\n'.join(\n sorted(class_attributes_description))\n init_arguments = ', '.join(init_arguments)\n instance_attributes = '\\n'.join(sorted(instance_attributes))\n\n template_values = {\n 'class_attributes_description': class_attributes_description,\n 'init_arguments': init_arguments,\n 'instance_attributes': instance_attributes,\n 'type_description': type_description,\n 'type_name': type_name}\n\n return cls._CLASS_TEMPLATE.format(**template_values)", "response": "Creates the class template."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _IsIdentifier(cls, string):\n return (\n string and not string[0].isdigit() and\n all(character.isalnum() or character == '_' for character in string))", "response": "Checks if a string contains an identifier."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nvalidates the data type definition.", "response": "def _ValidateDataTypeDefinition(cls, data_type_definition):\n \"\"\"Validates the data type definition.\n\n Args:\n data_type_definition (DataTypeDefinition): data type definition.\n\n Raises:\n ValueError: if the data type definition is not considered valid.\n \"\"\"\n if not cls._IsIdentifier(data_type_definition.name):\n raise ValueError(\n 'Data type definition name: {0!s} not a valid identifier'.format(\n data_type_definition.name))\n\n if keyword.iskeyword(data_type_definition.name):\n raise ValueError(\n 'Data type definition name: {0!s} matches keyword'.format(\n data_type_definition.name))\n\n members = getattr(data_type_definition, 'members', None)\n if not members:\n raise ValueError(\n 'Data type definition name: {0!s} missing members'.format(\n data_type_definition.name))\n\n defined_attribute_names = set()\n\n for member_definition in members:\n attribute_name = member_definition.name\n\n if not cls._IsIdentifier(attribute_name):\n raise ValueError('Attribute name: {0!s} not a valid identifier'.format(\n attribute_name))\n\n if attribute_name.startswith('_'):\n raise ValueError('Attribute name: {0!s} starts with underscore'.format(\n attribute_name))\n\n if keyword.iskeyword(attribute_name):\n raise ValueError('Attribute name: {0!s} matches keyword'.format(\n attribute_name))\n\n if attribute_name in defined_attribute_names:\n raise ValueError('Attribute name: {0!s} already defined'.format(\n attribute_name))\n\n defined_attribute_names.add(attribute_name)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef CreateClass(cls, data_type_definition):\n cls._ValidateDataTypeDefinition(data_type_definition)\n\n class_definition = cls._CreateClassTemplate(data_type_definition)\n\n namespace = {\n '__builtins__' : {\n 'object': builtins.object,\n 'super': builtins.super},\n '__name__': '{0:s}'.format(data_type_definition.name)}\n\n if sys.version_info[0] >= 3:\n # pylint: disable=no-member\n namespace['__builtins__']['__build_class__'] = builtins.__build_class__\n\n exec(class_definition, namespace) # pylint: disable=exec-used\n\n return namespace[data_type_definition.name]", "response": "Creates a new structure values class."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef GetDefinitionByName(self, name):\n lookup_name = name.lower()\n if lookup_name not in self._definitions:\n lookup_name = self._aliases.get(name, None)\n\n return self._definitions.get(lookup_name, None)", "response": "Retrieves a specific data type definition by name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef RegisterDefinition(self, data_type_definition):\n name_lower = data_type_definition.name.lower()\n if name_lower in self._definitions:\n raise KeyError('Definition already set for name: {0:s}.'.format(\n data_type_definition.name))\n\n if data_type_definition.name in self._aliases:\n raise KeyError('Alias already set for name: {0:s}.'.format(\n data_type_definition.name))\n\n for alias in data_type_definition.aliases:\n if alias in self._aliases:\n raise KeyError('Alias already set for name: {0:s}.'.format(alias))\n\n self._definitions[name_lower] = data_type_definition\n\n for alias in data_type_definition.aliases:\n self._aliases[alias] = name_lower\n\n if data_type_definition.TYPE_INDICATOR == definitions.TYPE_INDICATOR_FORMAT:\n self._format_definitions.append(name_lower)", "response": "Registers a data type definition."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef apply_on_csv_string(rules_str, func):\n splitted = rules_str.split(\",\")\n for str in splitted:\n func(str.strip())", "response": "Splits a given string by comma trims whitespace on the resulting strings and applies a given function to each item."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the HTML for the Favicon - URL for the Model.", "response": "def placeFavicon(context):\n \"\"\"\n Gets Favicon-URL for the Model.\n\n Template Syntax:\n\n {% placeFavicon %}\n\n \"\"\"\n fav = Favicon.objects.filter(isFavicon=True).first()\n if not fav:\n return mark_safe('')\n html = ''\n for rel in config:\n for size in sorted(config[rel], reverse=True):\n n = fav.get_favicon(size=size, rel=rel)\n html += '' % (\n n.rel, n.size, n.size, n.faviconImage.url)\n\n default_fav = fav.get_favicon(size=32, rel='shortcut icon')\n html += '' % (\n default_fav.rel, default_fav.size, default_fav.size, default_fav.faviconImage.url)\n\n return mark_safe(html)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets default theme name based in config file.", "response": "def set_default_theme(theme):\n \"\"\"\n Set default theme name based in config file.\n \"\"\"\n pref_init() # make sure config files exist\n parser = cp.ConfigParser()\n parser.read(PREFS_FILE)\n # Do we need to create a section?\n if not parser.has_section(\"theme\"):\n parser.add_section(\"theme\")\n parser.set(\"theme\", \"default\", theme)\n # best way to make sure no file truncation?\n with open(\"%s.2\" % PREFS_FILE, \"w\") as fp:\n parser.write(fp)\n copy(\"%s.2\" % PREFS_FILE, PREFS_FILE)\n unlink(\"%s.2\" % PREFS_FILE,)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget theme name based on manual input prefs file or default to plain.", "response": "def pick_theme(manual):\n \"\"\"\n Return theme name based on manual input, prefs file, or default to \"plain\".\n \"\"\"\n if manual:\n return manual\n pref_init()\n parser = cp.ConfigParser()\n parser.read(PREFS_FILE)\n try:\n theme = parser.get(\"theme\", \"default\")\n except (cp.NoSectionError, cp.NoOptionError):\n theme = \"plain\"\n return theme"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates ~. cdk dir if it doesn t exist. Copy the default prefs file if it doesn t exist.", "response": "def pref_init():\n \"\"\"Can be called without penalty. Create ~/.cdk dir if it doesn't\n exist. Copy the default pref file if it doesn't exist.\"\"\"\n\n # make sure we have a ~/.cdk dir\n if not isdir(PREFS_DIR):\n mkdir(PREFS_DIR)\n # make sure we have a default prefs file\n if not isfile(PREFS_FILE):\n copy(join(LOCATION, \"custom\", \"prefs\"), PREFS_DIR)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall a theme file to the themes directory.", "response": "def install_theme(path_to_theme):\n \"\"\"\n Pass a path to a theme file which will be extracted to the themes directory.\n \"\"\"\n pref_init()\n # cp the file\n filename = basename(path_to_theme)\n dest = join(THEMES_DIR, filename)\n copy(path_to_theme, dest)\n # unzip\n zf = zipfile.ZipFile(dest)\n # should make sure zipfile contains only themename folder which doesn't conflict\n # with existing themename. Or some kind of sanity check\n zf.extractall(THEMES_DIR) # plus this is a potential security flaw pre 2.7.4\n # remove the copied zipfile\n unlink(dest)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef main():\n # Try parsing command line args and flags with docopt\n args = docopt(__doc__, version=\"cdk\")\n # Am I going to need validation? No Schema for the moment...\n if args['FILE']:\n out = output_file(args['FILE'])\n # Great! Run asciidoc with appropriate flags\n theme = pick_theme(args['--theme'])\n if theme not in listdir(THEMES_DIR):\n exit('Selected theme \"%s\" not found. Check ~/.cdk/prefs' % theme)\n cmd = create_command(theme, args['--bare'], args['--toc'], args['--notransition'],\n args['--logo'])\n run_command(cmd, args)\n if args['--toc']:\n add_css(out, '.deck-container .deck-toc li a span{color: #888;display:inline;}')\n if args['--custom-css']:\n add_css_file(out, args['--custom-css'])\n if args['--open']:\n webbrowser.open(\"file://\" + abspath(out))\n\n # other commands\n elif args['--generate']:\n if isfile(args['--generate']):\n exit(\"%s already exists!\" % args['--generate'])\n with open(args['--generate'], \"w\") as fp:\n sample = join(LOCATION, \"custom\", \"sample.asc\")\n fp.write(open(sample).read())\n print(\"Created sample slide deck in %s...\" % args['--generate'])\n exit()\n\n elif args['--install-theme']:\n path = args['--install-theme']\n if not isfile(path):\n exit(\"Theme file not found.\")\n if not path.endswith(\".zip\"):\n exit(\"Theme installation currently only supports theme install from \"\n \".zip files.\")\n install_theme(path)\n elif args['--default-theme']:\n set_default_theme(args['--default-theme'])", "response": "Entry point for asciidocapi\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmoves contents of resources key in internal dictionary into self. resources", "response": "def separate_resources(self):\n # type: () -> None\n \"\"\"Move contents of resources key in internal dictionary into self.resources\n\n Returns:\n None\n \"\"\"\n self._separate_hdxobjects(self.resources, 'resources', 'name', hdx.data.resource.Resource)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a resource object from a resource object or a dictionary.", "response": "def _get_resource_from_obj(self, resource):\n # type: (Union[hdx.data.resource.Resource,Dict,str]) -> hdx.data.resource.Resource\n \"\"\"Add new or update existing resource in dataset with new metadata\n\n Args:\n resource (Union[hdx.data.resource.Resource,Dict,str]): Either resource id or resource metadata from a Resource object or a dictionary\n\n Returns:\n hdx.data.resource.Resource: Resource object\n \"\"\"\n if isinstance(resource, str):\n if is_valid_uuid(resource) is False:\n raise HDXError('%s is not a valid resource id!' % resource)\n resource = hdx.data.resource.Resource.read_from_hdx(resource, configuration=self.configuration)\n elif isinstance(resource, dict):\n resource = hdx.data.resource.Resource(resource, configuration=self.configuration)\n if not isinstance(resource, hdx.data.resource.Resource):\n raise HDXError('Type %s cannot be added as a resource!' % type(resource).__name__)\n return resource"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_update_resource(self, resource, ignore_datasetid=False):\n # type: (Union[hdx.data.resource.Resource,Dict,str], bool) -> None\n \"\"\"Add new or update existing resource in dataset with new metadata\n\n Args:\n resource (Union[hdx.data.resource.Resource,Dict,str]): Either resource id or resource metadata from a Resource object or a dictionary\n ignore_datasetid (bool): Whether to ignore dataset id in the resource\n\n Returns:\n None\n \"\"\"\n resource = self._get_resource_from_obj(resource)\n if 'package_id' in resource:\n if not ignore_datasetid:\n raise HDXError('Resource %s being added already has a dataset id!' % (resource['name']))\n resource.check_url_filetoupload()\n resource_updated = self._addupdate_hdxobject(self.resources, 'name', resource)\n if resource.get_file_to_upload():\n resource_updated.set_file_to_upload(resource.get_file_to_upload())", "response": "Add or update a resource in dataset with new metadata"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds new or update existing resources with new metadata to the dataset", "response": "def add_update_resources(self, resources, ignore_datasetid=False):\n # type: (List[Union[hdx.data.resource.Resource,Dict,str]], bool) -> None\n \"\"\"Add new or update existing resources with new metadata to the dataset\n\n Args:\n resources (List[Union[hdx.data.resource.Resource,Dict,str]]): A list of either resource ids or resources metadata from either Resource objects or dictionaries\n ignore_datasetid (bool): Whether to ignore dataset id in the resource. Defaults to False.\n\n Returns:\n None\n \"\"\"\n if not isinstance(resources, list):\n raise HDXError('Resources should be a list!')\n for resource in resources:\n self.add_update_resource(resource, ignore_datasetid)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a resource from the dataset and also from HDX by default", "response": "def delete_resource(self, resource, delete=True):\n # type: (Union[hdx.data.resource.Resource,Dict,str], bool) -> bool\n \"\"\"Delete a resource from the dataset and also from HDX by default\n\n Args:\n resource (Union[hdx.data.resource.Resource,Dict,str]): Either resource id or resource metadata from a Resource object or a dictionary\n delete (bool): Whetehr to delete the resource from HDX (not just the dataset). Defaults to True.\n\n Returns:\n bool: True if resource removed or False if not\n \"\"\"\n if isinstance(resource, str):\n if is_valid_uuid(resource) is False:\n raise HDXError('%s is not a valid resource id!' % resource)\n return self._remove_hdxobject(self.resources, resource, delete=delete)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef reorder_resources(self, resource_ids, hxl_update=True):\n # type: (List[str], bool) -> None\n \"\"\"Reorder resources in dataset according to provided list.\n If only some resource ids are supplied then these are\n assumed to be first and the other resources will stay in\n their original order.\n\n Args:\n resource_ids (List[str]): List of resource ids\n hxl_update (bool): Whether to call package_hxl_update. Defaults to True.\n\n Returns:\n None\n \"\"\"\n dataset_id = self.data.get('id')\n if not dataset_id:\n raise HDXError('Dataset has no id! It must be read, created or updated first.')\n data = {'id': dataset_id,\n 'order': resource_ids}\n self._write_to_hdx('reorder', data, 'package_id')\n if hxl_update:\n self.hxl_update()", "response": "Reorder resources in the dataset according to provided list."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef update_from_yaml(self, path=join('config', 'hdx_dataset_static.yml')):\n # type: (str) -> None\n \"\"\"Update dataset metadata with static metadata from YAML file\n\n Args:\n path (str): Path to YAML dataset metadata. Defaults to config/hdx_dataset_static.yml.\n\n Returns:\n None\n \"\"\"\n super(Dataset, self).update_from_yaml(path)\n self.separate_resources()", "response": "Update dataset metadata with static metadata from YAML file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating dataset metadata with static metadata from JSON file at path.", "response": "def update_from_json(self, path=join('config', 'hdx_dataset_static.json')):\n # type: (str) -> None\n \"\"\"Update dataset metadata with static metadata from JSON file\n\n Args:\n path (str): Path to JSON dataset metadata. Defaults to config/hdx_dataset_static.json.\n\n Returns:\n None\n \"\"\"\n super(Dataset, self).update_from_json(path)\n self.separate_resources()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['Dataset']\n \"\"\"Reads the dataset given by identifier from HDX and returns Dataset object\n\n Args:\n identifier (str): Identifier of dataset\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[Dataset]: Dataset object if successful read, None if not\n \"\"\"\n\n dataset = Dataset(configuration=configuration)\n result = dataset._dataset_load_from_hdx(identifier)\n if result:\n return dataset\n return None", "response": "Reads the dataset given by identifier from HDX and returns Dataset object if successful."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _dataset_create_resources(self):\n # type: () -> None\n \"\"\"Creates resource objects in dataset\n \"\"\"\n\n if 'resources' in self.data:\n self.old_data['resources'] = self._copy_hdxobjects(self.resources, hdx.data.resource.Resource, 'file_to_upload')\n self.init_resources()\n self.separate_resources()", "response": "Creates resource objects in dataset\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload the dataset given by either id or name from HDX and creates the resources if needed.", "response": "def _dataset_load_from_hdx(self, id_or_name):\n # type: (str) -> bool\n \"\"\"Loads the dataset given by either id or name from HDX\n\n Args:\n id_or_name (str): Either id or name of dataset\n\n Returns:\n bool: True if loaded, False if not\n \"\"\"\n\n if not self._load_from_hdx('dataset', id_or_name):\n return False\n self._dataset_create_resources()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck that metadata for dataset and resources is complete.", "response": "def check_required_fields(self, ignore_fields=list(), allow_no_resources=False):\n # type: (List[str], bool) -> None\n \"\"\"Check that metadata for dataset and its resources is complete. The parameter ignore_fields\n should be set if required to any fields that should be ignored for the particular operation.\n\n Args:\n ignore_fields (List[str]): Fields to ignore. Default is [].\n allow_no_resources (bool): Whether to allow no resources. Defaults to False.\n\n Returns:\n None\n \"\"\"\n if self.is_requestable():\n self._check_required_fields('dataset-requestable', ignore_fields)\n else:\n self._check_required_fields('dataset', ignore_fields)\n if len(self.resources) == 0 and not allow_no_resources:\n raise HDXError('There are no resources! Please add at least one resource!')\n for resource in self.resources:\n ignore_fields = ['package_id']\n resource.check_required_fields(ignore_fields=ignore_fields)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _dataset_merge_filestore_resource(self, resource, updated_resource, filestore_resources, ignore_fields):\n # type: (hdx.data.Resource, hdx.data.Resource, List[hdx.data.Resource], List[str]) -> None\n \"\"\"Helper method to merge updated resource from dataset into HDX resource read from HDX including filestore.\n\n Args:\n resource (hdx.data.Resource): Resource read from HDX\n updated_resource (hdx.data.Resource): Updated resource from dataset\n filestore_resources (List[hdx.data.Resource]): List of resources that use filestore (to be appended to)\n ignore_fields (List[str]): List of fields to ignore when checking resource\n\n Returns:\n None\n \"\"\"\n if updated_resource.get_file_to_upload():\n resource.set_file_to_upload(updated_resource.get_file_to_upload())\n filestore_resources.append(resource)\n merge_two_dictionaries(resource, updated_resource)\n resource.check_required_fields(ignore_fields=ignore_fields)\n if resource.get_file_to_upload():\n resource['url'] = Dataset.temporary_url", "response": "Helper method to merge updated resource into resource from dataset into filestore."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _dataset_merge_filestore_newresource(self, new_resource, ignore_fields, filestore_resources):\n # type: (hdx.data.Resource, List[str], List[hdx.data.Resource]) -> None\n \"\"\"Helper method to add new resource from dataset including filestore.\n\n Args:\n new_resource (hdx.data.Resource): New resource from dataset\n ignore_fields (List[str]): List of fields to ignore when checking resource\n filestore_resources (List[hdx.data.Resource]): List of resources that use filestore (to be appended to)\n\n Returns:\n None\n \"\"\"\n new_resource.check_required_fields(ignore_fields=ignore_fields)\n self.resources.append(new_resource)\n if new_resource.get_file_to_upload():\n filestore_resources.append(new_resource)\n new_resource['url'] = Dataset.temporary_url", "response": "Helper method to add new resource from dataset including filestore."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _add_filestore_resources(self, filestore_resources, create_default_views, hxl_update):\n # type: (List[hdx.data.Resource], bool, bool) -> None\n \"\"\"Helper method to create files in filestore by updating resources.\n\n Args:\n filestore_resources (List[hdx.data.Resource]): List of resources that use filestore (to be appended to)\n create_default_views (bool): Whether to call package_create_default_resource_views.\n hxl_update (bool): Whether to call package_hxl_update.\n\n Returns:\n None\n \"\"\"\n for resource in filestore_resources:\n for created_resource in self.data['resources']:\n if resource['name'] == created_resource['name']:\n merge_two_dictionaries(resource.data, created_resource)\n del resource['url']\n resource.update_in_hdx()\n merge_two_dictionaries(created_resource, resource.data)\n break\n self.init_resources()\n self.separate_resources()\n if create_default_views:\n self.create_default_views()\n if hxl_update:\n self.hxl_update()", "response": "Private method to add filestore resources."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_in_hdx(self, update_resources=True, update_resources_by_name=True,\n remove_additional_resources=False, create_default_views=True, hxl_update=True):\n # type: (bool, bool, bool, bool, bool) -> None\n \"\"\"Check if dataset exists in HDX and if so, update it\n\n Args:\n update_resources (bool): Whether to update resources. Defaults to True.\n update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.\n remove_additional_resources (bool): Remove additional resources found in dataset. Defaults to False.\n create_default_views (bool): Whether to call package_create_default_resource_views. Defaults to True.\n hxl_update (bool): Whether to call package_hxl_update. Defaults to True.\n\n Returns:\n None\n \"\"\"\n loaded = False\n if 'id' in self.data:\n self._check_existing_object('dataset', 'id')\n if self._dataset_load_from_hdx(self.data['id']):\n loaded = True\n else:\n logger.warning('Failed to load dataset with id %s' % self.data['id'])\n if not loaded:\n self._check_existing_object('dataset', 'name')\n if not self._dataset_load_from_hdx(self.data['name']):\n raise HDXError('No existing dataset to update!')\n self._dataset_merge_hdx_update(update_resources=update_resources,\n update_resources_by_name=update_resources_by_name,\n remove_additional_resources=remove_additional_resources,\n create_default_views=create_default_views,\n hxl_update=hxl_update)", "response": "Update the resource in HDX if it exists in HDX and if so create the resource if needed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_in_hdx(self, allow_no_resources=False, update_resources=True, update_resources_by_name=True,\n remove_additional_resources=False, create_default_views=True, hxl_update=True):\n # type: (bool, bool, bool, bool, bool, bool) -> None\n \"\"\"Check if dataset exists in HDX and if so, update it, otherwise create it\n\n Args:\n allow_no_resources (bool): Whether to allow no resources. Defaults to False.\n update_resources (bool): Whether to update resources (if updating). Defaults to True.\n update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.\n remove_additional_resources (bool): Remove additional resources found in dataset (if updating). Defaults to False.\n create_default_views (bool): Whether to call package_create_default_resource_views (if updating). Defaults to True.\n hxl_update (bool): Whether to call package_hxl_update. Defaults to True.\n\n Returns:\n None\n \"\"\"\n self.check_required_fields(allow_no_resources=allow_no_resources)\n loadedid = None\n if 'id' in self.data:\n if self._dataset_load_from_hdx(self.data['id']):\n loadedid = self.data['id']\n else:\n logger.warning('Failed to load dataset with id %s' % self.data['id'])\n if not loadedid:\n if self._dataset_load_from_hdx(self.data['name']):\n loadedid = self.data['name']\n if loadedid:\n logger.warning('Dataset exists. Updating %s' % loadedid)\n self._dataset_merge_hdx_update(update_resources=update_resources,\n update_resources_by_name=update_resources_by_name,\n remove_additional_resources=remove_additional_resources,\n create_default_views=create_default_views,\n hxl_update=hxl_update)\n return\n\n filestore_resources = list()\n if self.resources:\n ignore_fields = ['package_id']\n for resource in self.resources:\n resource.check_required_fields(ignore_fields=ignore_fields)\n if resource.get_file_to_upload():\n filestore_resources.append(resource)\n resource['url'] = Dataset.temporary_url\n self.data['resources'] = self._convert_hdxobjects(self.resources)\n self._save_to_hdx('create', 'name')\n self._add_filestore_resources(filestore_resources, False, hxl_update)", "response": "Checks if dataset exists in HDX and if so updates it otherwise creates it."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsearches for datasets in HDX and returns them as a list of Dataset objects.", "response": "def search_in_hdx(cls, query='*:*', configuration=None, page_size=1000, **kwargs):\n # type: (Optional[str], Optional[Configuration], int, Any) -> List['Dataset']\n \"\"\"Searches for datasets in HDX\n\n Args:\n query (Optional[str]): Query (in Solr format). Defaults to '*:*'.\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n page_size (int): Size of page to return. Defaults to 1000.\n **kwargs: See below\n fq (string): Any filter queries to apply\n sort (string): Sorting of the search results. Defaults to 'relevance asc, metadata_modified desc'.\n rows (int): Number of matching rows to return. Defaults to all datasets (sys.maxsize).\n start (int): Offset in the complete result for where the set of returned datasets should begin\n facet (string): Whether to enable faceted results. Default to True.\n facet.mincount (int): Minimum counts for facet fields should be included in the results\n facet.limit (int): Maximum number of values the facet fields return (- = unlimited). Defaults to 50.\n facet.field (List[str]): Fields to facet upon. Default is empty.\n use_default_schema (bool): Use default package schema instead of custom schema. Defaults to False.\n\n Returns:\n List[Dataset]: list of datasets resulting from query\n \"\"\"\n\n dataset = Dataset(configuration=configuration)\n total_rows = kwargs.get('rows', cls.max_int)\n start = kwargs.get('start', 0)\n all_datasets = None\n attempts = 0\n while attempts < cls.max_attempts and all_datasets is None: # if the count values vary for multiple calls, then must redo query\n all_datasets = list()\n counts = set()\n for page in range(total_rows // page_size + 1):\n pagetimespagesize = page * page_size\n kwargs['start'] = start + pagetimespagesize\n rows_left = total_rows - pagetimespagesize\n rows = min(rows_left, page_size)\n kwargs['rows'] = rows\n _, result = dataset._read_from_hdx('dataset', query, 'q', Dataset.actions()['search'], **kwargs)\n datasets = list()\n if result:\n count = result.get('count', None)\n if count:\n counts.add(count)\n no_results = len(result['results'])\n for datasetdict in result['results']:\n dataset = Dataset(configuration=configuration)\n dataset.old_data = dict()\n dataset.data = datasetdict\n dataset._dataset_create_resources()\n datasets.append(dataset)\n all_datasets += datasets\n if no_results < rows:\n break\n else:\n break\n else:\n logger.debug(result)\n if all_datasets and len(counts) != 1: # Make sure counts are all same for multiple calls to HDX\n all_datasets = None\n attempts += 1\n else:\n ids = [dataset['id'] for dataset in all_datasets] # check for duplicates (shouldn't happen)\n if len(ids) != len(set(ids)):\n all_datasets = None\n attempts += 1\n if attempts == cls.max_attempts and all_datasets is None:\n raise HDXError('Maximum attempts reached for searching for datasets!')\n return all_datasets"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_all_dataset_names(configuration=None, **kwargs):\n # type: (Optional[Configuration], Any) -> List[str]\n \"\"\"Get all dataset names in HDX\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n **kwargs: See below\n limit (int): Number of rows to return. Defaults to all dataset names.\n offset (int): Offset in the complete result for where the set of returned dataset names should begin\n\n Returns:\n List[str]: list of all dataset names in HDX\n \"\"\"\n dataset = Dataset(configuration=configuration)\n dataset['id'] = 'all dataset names' # only for error message if produced\n return dataset._write_to_hdx('list', kwargs, 'id')", "response": "Get all dataset names in HDX"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_datasets(cls, configuration=None, page_size=1000, check_duplicates=True, **kwargs):\n # type: (Optional[Configuration], int, bool, Any) -> List['Dataset']\n \"\"\"Get all datasets in HDX\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n page_size (int): Size of page to return. Defaults to 1000.\n check_duplicates (bool): Whether to check for duplicate datasets. Defaults to True.\n **kwargs: See below\n limit (int): Number of rows to return. Defaults to all datasets (sys.maxsize)\n offset (int): Offset in the complete result for where the set of returned datasets should begin\n\n Returns:\n List[Dataset]: list of all datasets in HDX\n \"\"\"\n\n dataset = Dataset(configuration=configuration)\n dataset['id'] = 'all datasets' # only for error message if produced\n total_rows = kwargs.get('limit', cls.max_int)\n start = kwargs.get('offset', 0)\n all_datasets = None\n attempts = 0\n while attempts < cls.max_attempts and all_datasets is None: # if the dataset names vary for multiple calls, then must redo query\n all_datasets = list()\n for page in range(total_rows // page_size + 1):\n pagetimespagesize = page * page_size\n kwargs['offset'] = start + pagetimespagesize\n rows_left = total_rows - pagetimespagesize\n rows = min(rows_left, page_size)\n kwargs['limit'] = rows\n result = dataset._write_to_hdx('all', kwargs, 'id')\n datasets = list()\n if isinstance(result, list):\n no_results = len(result)\n if no_results == 0 and page == 0:\n all_datasets = None\n break\n for datasetdict in result:\n dataset = Dataset(configuration=configuration)\n dataset.old_data = dict()\n dataset.data = datasetdict\n dataset._dataset_create_resources()\n datasets.append(dataset)\n all_datasets += datasets\n if no_results < rows:\n break\n else:\n logger.debug(result)\n if all_datasets is None:\n attempts += 1\n elif check_duplicates:\n names_list = [dataset['name'] for dataset in all_datasets]\n names = set(names_list)\n if len(names_list) != len(names): # check for duplicates (shouldn't happen)\n all_datasets = None\n attempts += 1\n # This check is no longer valid because of showcases being returned by package_list!\n # elif total_rows == max_int:\n # all_names = set(Dataset.get_all_dataset_names()) # check dataset names match package_list\n # if names != all_names:\n # all_datasets = None\n # attempts += 1\n if attempts == cls.max_attempts and all_datasets is None:\n raise HDXError('Maximum attempts reached for getting all datasets!')\n return all_datasets", "response": "Get all datasets in HDX."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets all resources from a list of datasets", "response": "def get_all_resources(datasets):\n # type: (List['Dataset']) -> List[hdx.data.resource.Resource]\n \"\"\"Get all resources from a list of datasets (such as returned by search)\n\n Args:\n datasets (List[Dataset]): list of datasets\n\n Returns:\n List[hdx.data.resource.Resource]: list of resources within those datasets\n \"\"\"\n resources = []\n for dataset in datasets:\n for resource in dataset.get_resources():\n resources.append(resource)\n return resources"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets dataset date as datetime. datetime object. For range returns start date.", "response": "def get_dataset_date_as_datetime(self):\n # type: () -> Optional[datetime]\n \"\"\"Get dataset date as datetime.datetime object. For range returns start date.\n\n Returns:\n Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set\n \"\"\"\n dataset_date = self.data.get('dataset_date', None)\n if dataset_date:\n if '-' in dataset_date:\n dataset_date = dataset_date.split('-')[0]\n return datetime.strptime(dataset_date, '%m/%d/%Y')\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting dataset end date as datetime. datetime object.", "response": "def get_dataset_end_date_as_datetime(self):\n # type: () -> Optional[datetime]\n \"\"\"Get dataset end date as datetime.datetime object.\n\n Returns:\n Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set\n \"\"\"\n dataset_date = self.data.get('dataset_date', None)\n if dataset_date:\n if '-' in dataset_date:\n dataset_date = dataset_date.split('-')[1]\n return datetime.strptime(dataset_date, '%m/%d/%Y')\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_formatted_date(dataset_date, date_format=None):\n # type: (Optional[datetime], Optional[str]) -> Optional[str]\n \"\"\"Get supplied dataset date as string in specified format. \n If no format is supplied, an ISO 8601 string is returned.\n\n Args:\n dataset_date (Optional[datetime.datetime]): dataset date in datetime.datetime format \n date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.\n\n Returns:\n Optional[str]: Dataset date string or None if no date is set\n \"\"\"\n if dataset_date:\n if date_format:\n return dataset_date.strftime(date_format)\n else:\n return dataset_date.date().isoformat()\n else:\n return None", "response": "Returns the supplied dataset date as string in specified format."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_dataset_date(self, date_format=None):\n # type: (Optional[str]) -> Optional[str]\n \"\"\"Get dataset date as string in specified format. For range returns start date.\n If no format is supplied, an ISO 8601 string is returned.\n\n Args:\n date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.\n\n Returns:\n Optional[str]: Dataset date string or None if no date is set\n \"\"\"\n dataset_date = self.get_dataset_date_as_datetime()\n return self._get_formatted_date(dataset_date, date_format)", "response": "Returns the dataset date as string in specified format. For range returns start date."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_dataset_end_date(self, date_format=None):\n # type: (Optional[str]) -> Optional[str]\n \"\"\"Get dataset date as string in specified format. For range returns start date.\n If no format is supplied, an ISO 8601 string is returned.\n\n Args:\n date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.\n\n Returns:\n Optional[str]: Dataset date string or None if no date is set\n \"\"\"\n dataset_date = self.get_dataset_end_date_as_datetime()\n return self._get_formatted_date(dataset_date, date_format)", "response": "Returns the dataset end date as string in specified format. For range returns start date."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_dataset_date_from_datetime(self, dataset_date, dataset_end_date=None):\n # type: (datetime, Optional[datetime]) -> None\n \"\"\"Set dataset date from datetime.datetime object\n\n Args:\n dataset_date (datetime.datetime): Dataset date\n dataset_end_date (Optional[datetime.datetime]): Dataset end date\n\n Returns:\n None\n \"\"\"\n start_date = dataset_date.strftime('%m/%d/%Y')\n if dataset_end_date is None:\n self.data['dataset_date'] = start_date\n else:\n end_date = dataset_end_date.strftime('%m/%d/%Y')\n self.data['dataset_date'] = '%s-%s' % (start_date, end_date)", "response": "Set dataset date from datetime. datetime object"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_date(dataset_date, date_format):\n # type: (str, Optional[str]) -> datetime\n \"\"\"Parse dataset date from string using specified format. If no format is supplied, the function will guess.\n For unambiguous formats, this should be fine.\n\n Args:\n dataset_date (str): Dataset date string\n date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.\n\n Returns:\n datetime.datetime\n \"\"\"\n if date_format is None:\n try:\n return parser.parse(dataset_date)\n except (ValueError, OverflowError) as e:\n raisefrom(HDXError, 'Invalid dataset date!', e)\n else:\n try:\n return datetime.strptime(dataset_date, date_format)\n except ValueError as e:\n raisefrom(HDXError, 'Invalid dataset date!', e)", "response": "Parse dataset date from string using specified format."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nset the dataset date from string using specified format.", "response": "def set_dataset_date(self, dataset_date, dataset_end_date=None, date_format=None):\n # type: (str, Optional[str], Optional[str]) -> None\n \"\"\"Set dataset date from string using specified format. If no format is supplied, the function will guess.\n For unambiguous formats, this should be fine.\n\n Args:\n dataset_date (str): Dataset date string\n dataset_end_date (Optional[str]): Dataset end date string\n date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.\n\n Returns:\n None\n \"\"\"\n parsed_date = self._parse_date(dataset_date, date_format)\n if dataset_end_date is None:\n self.set_dataset_date_from_datetime(parsed_date)\n else:\n parsed_end_date = self._parse_date(dataset_end_date, date_format)\n self.set_dataset_date_from_datetime(parsed_date, parsed_end_date)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_dataset_year_range(self, dataset_year, dataset_end_year=None):\n # type: (Union[str, int], Optional[Union[str, int]]) -> None\n \"\"\"Set dataset date as a range from year or start and end year.\n\n Args:\n dataset_year (Union[str, int]): Dataset year given as string or int\n dataset_end_year (Optional[Union[str, int]]): Dataset end year given as string or int\n\n Returns:\n None\n \"\"\"\n if isinstance(dataset_year, int):\n dataset_date = '01/01/%d' % dataset_year\n elif isinstance(dataset_year, str):\n dataset_date = '01/01/%s' % dataset_year\n else:\n raise hdx.data.hdxobject.HDXError('dataset_year has type %s which is not supported!' % type(dataset_year).__name__)\n if dataset_end_year is None:\n dataset_end_year = dataset_year\n if isinstance(dataset_end_year, int):\n dataset_end_date = '31/12/%d' % dataset_end_year\n elif isinstance(dataset_end_year, str):\n dataset_end_date = '31/12/%s' % dataset_end_year\n else:\n raise hdx.data.hdxobject.HDXError('dataset_end_year has type %s which is not supported!' % type(dataset_end_year).__name__)\n self.set_dataset_date(dataset_date, dataset_end_date)", "response": "Set the dataset date as a range from year or start and end year."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the expected update frequency in textual rather than numeric form.", "response": "def get_expected_update_frequency(self):\n # type: () -> Optional[str]\n \"\"\"Get expected update frequency (in textual rather than numeric form)\n\n Returns:\n Optional[str]: Update frequency in textual form or None if the update frequency doesn't exist or is blank.\n \"\"\"\n days = self.data.get('data_update_frequency', None)\n if days:\n return Dataset.transform_update_frequency(days)\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_expected_update_frequency(self, update_frequency):\n # type: (str) -> None\n \"\"\"Set expected update frequency\n\n Args:\n update_frequency (str): Update frequency\n\n Returns:\n None\n \"\"\"\n try:\n int(update_frequency)\n except ValueError:\n update_frequency = Dataset.transform_update_frequency(update_frequency)\n if not update_frequency:\n raise HDXError('Invalid update frequency supplied!')\n self.data['data_update_frequency'] = update_frequency", "response": "Set expected update frequency for the entry."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove_tag(self, tag):\n # type: (str) -> bool\n \"\"\"Remove a tag\n\n Args:\n tag (str): Tag to remove\n\n Returns:\n bool: True if tag removed or False if not\n \"\"\"\n return self._remove_hdxobject(self.data.get('tags'), tag, matchon='name')", "response": "Removes a tag from the taglist"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_location(self, locations=None):\n # type: (Optional[List[str]]) -> List[str]\n \"\"\"Return the dataset's location\n\n Args:\n locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.\n\n Returns:\n List[str]: list of locations or [] if there are none\n \"\"\"\n countries = self.data.get('groups', None)\n if not countries:\n return list()\n return [Locations.get_location_from_HDX_code(x['name'], locations=locations,\n configuration=self.configuration) for x in countries]", "response": "Returns the dataset s location from HDX."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_country_location(self, country, exact=True, locations=None, use_live=True):\n # type: (str, bool, Optional[List[str]], bool) -> bool\n \"\"\"Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,\n converted to an iso 3 code. If the country is already added, it is ignored.\n\n Args:\n country (str): Country to add\n exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.\n locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.\n use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.\n\n Returns:\n bool: True if country added or False if country already present\n \"\"\"\n iso3, match = Country.get_iso3_country_code_fuzzy(country, use_live=use_live)\n if iso3 is None:\n raise HDXError('Country: %s - cannot find iso3 code!' % country)\n return self.add_other_location(iso3, exact=exact,\n alterror='Country: %s with iso3: %s could not be found in HDX list!' %\n (country, iso3),\n locations=locations)", "response": "Add a country location to the HDX list."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds a list of countries to the HDX database.", "response": "def add_country_locations(self, countries, locations=None, use_live=True):\n # type: (List[str], Optional[List[str]], bool) -> bool\n \"\"\"Add a list of countries. If iso 3 codes are not provided, values are parsed and where they are valid country\n names, converted to iso 3 codes. If any country is already added, it is ignored.\n\n Args:\n countries (List[str]): list of countries to add\n locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.\n use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.\n\n Returns:\n bool: True if all countries added or False if any already present.\n \"\"\"\n allcountriesadded = True\n for country in countries:\n if not self.add_country_location(country, locations=locations, use_live=use_live):\n allcountriesadded = False\n return allcountriesadded"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd all countries in a region.", "response": "def add_region_location(self, region, locations=None, use_live=True):\n # type: (str, Optional[List[str]], bool) -> bool\n \"\"\"Add all countries in a region. If a 3 digit UNStats M49 region code is not provided, value is parsed as a\n region name. If any country is already added, it is ignored.\n\n Args:\n region (str): M49 region, intermediate region or subregion to add\n locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.\n use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.\n\n Returns:\n bool: True if all countries in region added or False if any already present.\n \"\"\"\n return self.add_country_locations(Country.get_countries_in_region(region, exception=HDXError,\n use_live=use_live), locations=locations)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a location which is not a country or region. Value is parsed and compared to existing locations in HDX.", "response": "def add_other_location(self, location, exact=True, alterror=None, locations=None):\n # type: (str, bool, Optional[str], Optional[List[str]]) -> bool\n \"\"\"Add a location which is not a country or region. Value is parsed and compared to existing locations in\n HDX. If the location is already added, it is ignored.\n\n Args:\n location (str): Location to add\n exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.\n alterror (Optional[str]): Alternative error message to builtin if location not found. Defaults to None.\n locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.\n\n Returns:\n bool: True if location added or False if location already present\n \"\"\"\n hdx_code, match = Locations.get_HDX_code_from_location_partial(location, locations=locations,\n configuration=self.configuration)\n if hdx_code is None or (exact is True and match is False):\n if alterror is None:\n raise HDXError('Location: %s - cannot find in HDX!' % location)\n else:\n raise HDXError(alterror)\n groups = self.data.get('groups', None)\n hdx_code = hdx_code.lower()\n if groups:\n if hdx_code in [x['name'] for x in groups]:\n return False\n else:\n groups = list()\n groups.append({'name': hdx_code})\n self.data['groups'] = groups\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef remove_location(self, location):\n # type: (str) -> bool\n \"\"\"Remove a location. If the location is already added, it is ignored.\n\n Args:\n location (str): Location to remove\n\n Returns:\n bool: True if location removed or False if not\n \"\"\"\n res = self._remove_hdxobject(self.data.get('groups'), location, matchon='name')\n if not res:\n res = self._remove_hdxobject(self.data.get('groups'), location.upper(), matchon='name')\n if not res:\n res = self._remove_hdxobject(self.data.get('groups'), location.lower(), matchon='name')\n return res", "response": "Removes a location from the resource."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the dataset s maintainer.", "response": "def get_maintainer(self):\n # type: () -> hdx.data.user.User\n \"\"\"Get the dataset's maintainer.\n\n Returns:\n User: Dataset's maintainer\n \"\"\"\n return hdx.data.user.User.read_from_hdx(self.data['maintainer'], configuration=self.configuration)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the dataset s maintainer.", "response": "def set_maintainer(self, maintainer):\n # type: (Union[hdx.data.user.User,Dict,str]) -> None\n \"\"\"Set the dataset's maintainer.\n\n Args:\n maintainer (Union[User,Dict,str]): Either a user id or User metadata from a User object or dictionary.\n Returns:\n None\n \"\"\"\n if isinstance(maintainer, hdx.data.user.User) or isinstance(maintainer, dict):\n if 'id' not in maintainer:\n maintainer = hdx.data.user.User.read_from_hdx(maintainer['name'], configuration=self.configuration)\n maintainer = maintainer['id']\n elif not isinstance(maintainer, str):\n raise HDXError('Type %s cannot be added as a maintainer!' % type(maintainer).__name__)\n if is_valid_uuid(maintainer) is False:\n raise HDXError('%s is not a valid user id for a maintainer!' % maintainer)\n self.data['maintainer'] = maintainer"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_organization(self):\n # type: () -> hdx.data.organization.Organization\n \"\"\"Get the dataset's organization.\n\n Returns:\n Organization: Dataset's organization\n \"\"\"\n return hdx.data.organization.Organization.read_from_hdx(self.data['owner_org'], configuration=self.configuration)", "response": "Get the dataset s organization."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_organization(self, organization):\n # type: (Union[hdx.data.organization.Organization,Dict,str]) -> None\n \"\"\"Set the dataset's organization.\n\n Args:\n organization (Union[Organization,Dict,str]): Either an Organization id or Organization metadata from an Organization object or dictionary.\n Returns:\n None\n \"\"\"\n if isinstance(organization, hdx.data.organization.Organization) or isinstance(organization, dict):\n if 'id' not in organization:\n organization = hdx.data.organization.Organization.read_from_hdx(organization['name'], configuration=self.configuration)\n organization = organization['id']\n elif not isinstance(organization, str):\n raise HDXError('Type %s cannot be added as a organization!' % type(organization).__name__)\n if is_valid_uuid(organization) is False and organization != 'hdx':\n raise HDXError('%s is not a valid organization id!' % organization)\n self.data['owner_org'] = organization", "response": "Set the dataset s organization."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget any showcases the dataset is in", "response": "def get_showcases(self):\n # type: () -> List[hdx.data.showcase.Showcase]\n \"\"\"Get any showcases the dataset is in\n\n Returns:\n List[Showcase]: list of showcases\n \"\"\"\n assoc_result, showcases_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='package_id',\n action=hdx.data.showcase.Showcase.actions()['list_showcases'])\n showcases = list()\n if assoc_result:\n for showcase_dict in showcases_dicts:\n showcase = hdx.data.showcase.Showcase(showcase_dict, configuration=self.configuration)\n showcases.append(showcase)\n return showcases"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_dataset_showcase_dict(self, showcase):\n # type: (Union[hdx.data.showcase.Showcase, Dict,str]) -> Dict\n \"\"\"Get dataset showcase dict\n\n Args:\n showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary\n\n Returns:\n dict: dataset showcase dict\n \"\"\"\n if isinstance(showcase, hdx.data.showcase.Showcase) or isinstance(showcase, dict):\n if 'id' not in showcase:\n showcase = hdx.data.showcase.Showcase.read_from_hdx(showcase['name'])\n showcase = showcase['id']\n elif not isinstance(showcase, str):\n raise HDXError('Type %s cannot be added as a showcase!' % type(showcase).__name__)\n if is_valid_uuid(showcase) is False:\n raise HDXError('%s is not a valid showcase id!' % showcase)\n return {'package_id': self.data['id'], 'showcase_id': showcase}", "response": "Get dataset showcase dict from a showcase object or a dictionary"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_showcase(self, showcase, showcases_to_check=None):\n # type: (Union[hdx.data.showcase.Showcase,Dict,str], List[hdx.data.showcase.Showcase]) -> bool\n \"\"\"Add dataset to showcase\n\n Args:\n showcase (Union[Showcase,Dict,str]): Either a showcase id or showcase metadata from a Showcase object or dictionary\n showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.\n\n Returns:\n bool: True if the showcase was added, False if already present\n \"\"\"\n dataset_showcase = self._get_dataset_showcase_dict(showcase)\n if showcases_to_check is None:\n showcases_to_check = self.get_showcases()\n for showcase in showcases_to_check:\n if dataset_showcase['showcase_id'] == showcase['id']:\n return False\n showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)\n showcase._write_to_hdx('associate', dataset_showcase, 'package_id')\n return True", "response": "Adds a dataset to a showcase."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_showcases(self, showcases, showcases_to_check=None):\n # type: (List[Union[hdx.data.showcase.Showcase,Dict,str]], List[hdx.data.showcase.Showcase]) -> bool\n \"\"\"Add dataset to multiple showcases\n\n Args:\n showcases (List[Union[Showcase,Dict,str]]): A list of either showcase ids or showcase metadata from Showcase objects or dictionaries\n showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.\n\n Returns:\n bool: True if all showcases added or False if any already present\n \"\"\"\n if showcases_to_check is None:\n showcases_to_check = self.get_showcases()\n allshowcasesadded = True\n for showcase in showcases:\n if not self.add_showcase(showcase, showcases_to_check=showcases_to_check):\n allshowcasesadded = False\n return allshowcasesadded", "response": "Add dataset to multiple showcases"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove_showcase(self, showcase):\n # type: (Union[hdx.data.showcase.Showcase,Dict,str]) -> None\n \"\"\"Remove dataset from showcase\n\n Args:\n showcase (Union[Showcase,Dict,str]): Either a showcase id string or showcase metadata from a Showcase object or dictionary\n\n Returns:\n None\n \"\"\"\n dataset_showcase = self._get_dataset_showcase_dict(showcase)\n showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)\n showcase._write_to_hdx('disassociate', dataset_showcase, 'package_id')", "response": "Removes dataset from showcase"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the dataset to be of type requestable or notater.", "response": "def set_requestable(self, requestable=True):\n # type: (bool) -> None\n \"\"\"Set the dataset to be of type requestable or not\n\n Args:\n requestable (bool): Set whether dataset is requestable. Defaults to True.\n\n Returns:\n None\n \"\"\"\n self.data['is_requestdata_type'] = requestable\n if requestable:\n self.data['private'] = False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_filetypes(self):\n # type: () -> List[str]\n \"\"\"Return list of filetypes in your data\n\n Returns:\n List[str]: List of filetypes\n \"\"\"\n if not self.is_requestable():\n return [resource.get_file_type() for resource in self.get_resources()]\n return self._get_stringlist_from_commastring('file_types')", "response": "Return list of filetypes in your data\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncleans dataset tags according to tags cleanup spreadsheet and return if any changes occurred", "response": "def clean_dataset_tags(self):\n # type: () -> Tuple[bool, bool]\n \"\"\"Clean dataset tags according to tags cleanup spreadsheet and return if any changes occurred\n\n Returns:\n Tuple[bool, bool]: Returns (True if tags changed or False if not, True if error or False if not)\n \"\"\"\n tags_dict, wildcard_tags = Tags.tagscleanupdicts()\n\n def delete_tag(tag):\n logger.info('%s - Deleting tag %s!' % (self.data['name'], tag))\n return self.remove_tag(tag), False\n\n def update_tag(tag, final_tags, wording, remove_existing=True):\n text = '%s - %s: %s -> ' % (self.data['name'], wording, tag)\n if not final_tags:\n logger.error('%snothing!' % text)\n return False, True\n tags_lower_five = final_tags[:5].lower()\n if tags_lower_five == 'merge' or tags_lower_five == 'split' or (\n ';' not in final_tags and len(final_tags) > 50):\n logger.error('%s%s - Invalid final tag!' % (text, final_tags))\n return False, True\n if remove_existing:\n self.remove_tag(tag)\n tags = ', '.join(self.get_tags())\n if self.add_tags(final_tags.split(';')):\n logger.info('%s%s! Dataset tags: %s' % (text, final_tags, tags))\n else:\n logger.warning(\n '%s%s - At least one of the tags already exists! Dataset tags: %s' % (text, final_tags, tags))\n return True, False\n\n def do_action(tag, tags_dict_key):\n whattodo = tags_dict[tags_dict_key]\n action = whattodo[u'action']\n final_tags = whattodo[u'final tags (semicolon separated)']\n if action == u'Delete':\n changed, error = delete_tag(tag)\n elif action == u'Merge':\n changed, error = update_tag(tag, final_tags, 'Merging')\n elif action == u'Fix spelling':\n changed, error = update_tag(tag, final_tags, 'Fixing spelling')\n elif action == u'Non English':\n changed, error = update_tag(tag, final_tags, 'Anglicising', remove_existing=False)\n else:\n changed = False\n error = False\n return changed, error\n\n def process_tag(tag):\n changed = False\n error = False\n if tag in tags_dict.keys():\n changed, error = do_action(tag, tag)\n else:\n for wildcard_tag in wildcard_tags:\n if fnmatch.fnmatch(tag, wildcard_tag):\n changed, error = do_action(tag, wildcard_tag)\n break\n return changed, error\n\n anychange = False\n anyerror = False\n for tag in self.get_tags():\n changed, error = process_tag(tag)\n if changed:\n anychange = True\n if error:\n anyerror = True\n\n return anychange, anyerror"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset the resource that will be used for displaying QuickCharts in dataset preview", "response": "def set_quickchart_resource(self, resource):\n # type: (Union[hdx.data.resource.Resource,Dict,str,int]) -> bool\n \"\"\"Set the resource that will be used for displaying QuickCharts in dataset preview\n\n Args:\n resource (Union[hdx.data.resource.Resource,Dict,str,int]): Either resource id or name, resource metadata from a Resource object or a dictionary or position\n\n Returns:\n bool: Returns True if resource for QuickCharts in dataset preview set or False if not\n \"\"\"\n if isinstance(resource, int) and not isinstance(resource, bool):\n resource = self.get_resources()[resource]\n if isinstance(resource, hdx.data.resource.Resource) or isinstance(resource, dict):\n res = resource.get('id')\n if res is None:\n resource = resource['name']\n else:\n resource = res\n elif not isinstance(resource, str):\n raise hdx.data.hdxobject.HDXError('Resource id cannot be found in type %s!' % type(resource).__name__)\n if is_valid_uuid(resource) is True:\n search = 'id'\n else:\n search = 'name'\n changed = False\n for dataset_resource in self.resources:\n if dataset_resource[search] == resource:\n dataset_resource.enable_dataset_preview()\n self.preview_resource()\n changed = True\n else:\n dataset_resource.disable_dataset_preview()\n return changed"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating default resource views for all resources in dataset", "response": "def create_default_views(self, create_datastore_views=False):\n # type: (bool) -> None\n \"\"\"Create default resource views for all resources in dataset\n\n Args:\n create_datastore_views (bool): Whether to try to create resource views that point to the datastore\n\n Returns:\n None\n \"\"\"\n package = deepcopy(self.data)\n if self.resources:\n package['resources'] = self._convert_hdxobjects(self.resources)\n\n data = {'package': package, 'create_datastore_views': create_datastore_views}\n self._write_to_hdx('create_default_views', data, 'package')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_credentials(self):\n # type: () -> Optional[Tuple[str, str]]\n \"\"\"\n Return HDX site username and password\n\n Returns:\n Optional[Tuple[str, str]]: HDX site username and password or None\n\n \"\"\"\n site = self.data[self.hdx_site]\n username = site.get('username')\n if username:\n return b64decode(username).decode('utf-8'), b64decode(site['password']).decode('utf-8')\n else:\n return None", "response": "Returns username and password of the HDX site if username and password are not provided otherwise None."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalls the remote CKAN method in the CKAN instance.", "response": "def call_remoteckan(self, *args, **kwargs):\n # type: (Any, Any) -> Dict\n \"\"\"\n Calls the remote CKAN\n\n Args:\n *args: Arguments to pass to remote CKAN call_action method\n **kwargs: Keyword arguments to pass to remote CKAN call_action method\n\n Returns:\n Dict: The response from the remote CKAN call_action method\n\n \"\"\"\n requests_kwargs = kwargs.get('requests_kwargs', dict())\n credentials = self._get_credentials()\n if credentials:\n requests_kwargs['auth'] = credentials\n kwargs['requests_kwargs'] = requests_kwargs\n apikey = kwargs.get('apikey', self.get_api_key())\n kwargs['apikey'] = apikey\n return self.remoteckan().call_action(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _environment_variables(**kwargs):\n # type: (Any) -> Any\n \"\"\"\n Overwrite keyword arguments with environment variables\n\n Args:\n **kwargs: See below\n hdx_url (str): HDX url to use. Overrides hdx_site.\n hdx_site (str): HDX site to use eg. prod, test. Defaults to test.\n hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.\n\n Returns:\n kwargs: Changed keyword arguments\n\n \"\"\"\n\n hdx_key = os.getenv('HDX_KEY')\n if hdx_key is not None:\n kwargs['hdx_key'] = hdx_key\n hdx_url = os.getenv('HDX_URL')\n if hdx_url is not None:\n kwargs['hdx_url'] = hdx_url\n else:\n hdx_site = os.getenv('HDX_SITE')\n if hdx_site is not None:\n kwargs['hdx_site'] = hdx_site\n return kwargs", "response": "Returns a dictionary of keyword arguments with environment variables set in the object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_remoteckan(cls, site_url, user_agent=None, user_agent_config_yaml=None, user_agent_lookup=None,\n session=None, **kwargs):\n # type: (str, Optional[str], Optional[str], Optional[str], requests.Session, Any) -> ckanapi.RemoteCKAN\n \"\"\"\n Create remote CKAN instance from configuration\n\n Args:\n site_url (str): Site url.\n user_agent (Optional[str]): User agent string. HDXPythonLibrary/X.X.X- is prefixed.\n user_agent_config_yaml (Optional[str]): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.\n user_agent_lookup (Optional[str]): Lookup key for YAML. Ignored if user_agent supplied.\n session (requests.Session): requests Session object to use. Defaults to calling hdx.utilities.session.get_session()\n\n Returns:\n ckanapi.RemoteCKAN: Remote CKAN instance\n\n \"\"\"\n if not session:\n session = get_session(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,\n method_whitelist=frozenset(['HEAD', 'TRACE', 'GET', 'POST', 'PUT',\n 'OPTIONS', 'DELETE']), **kwargs)\n ua = session.headers['User-Agent']\n else:\n ua = kwargs.get('full_agent')\n if not ua:\n ua = UserAgent.get(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,\n **kwargs)\n return ckanapi.RemoteCKAN(site_url, user_agent=ua, session=session)", "response": "Create CKAN instance from configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets up remote CKAN from provided CKAN or by creating from configuration Args: remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration. Returns: None", "response": "def setup_remoteckan(self, remoteckan=None, **kwargs):\n # type: (Optional[ckanapi.RemoteCKAN], Any) -> None\n \"\"\"\n Set up remote CKAN from provided CKAN or by creating from configuration\n\n Args:\n remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.\n\n Returns:\n None\n\n \"\"\"\n if remoteckan is None:\n self._remoteckan = self.create_remoteckan(self.get_hdx_site_url(), full_agent=self.get_user_agent(),\n **kwargs)\n else:\n self._remoteckan = remoteckan"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets up the HDX configuration object.", "response": "def setup(cls, configuration=None, **kwargs):\n # type: (Optional['Configuration'], Any) -> None\n \"\"\"\n Set up the HDX configuration\n\n Args:\n configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.\n **kwargs: See below\n user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.\n user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.\n user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.\n hdx_url (str): HDX url to use. Overrides hdx_site.\n hdx_site (str): HDX site to use eg. prod, test.\n hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.\n hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.\n hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR\n hdx_config_json (str): Path to JSON HDX configuration OR\n hdx_config_yaml (str): Path to YAML HDX configuration\n project_config_dict (dict): Project configuration dictionary OR\n project_config_json (str): Path to JSON Project configuration OR\n project_config_yaml (str): Path to YAML Project configuration\n hdx_base_config_dict (dict): HDX base configuration dictionary OR\n hdx_base_config_json (str): Path to JSON HDX base configuration OR\n hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.\n\n Returns:\n None\n\n \"\"\"\n if configuration is None:\n cls._configuration = Configuration(**kwargs)\n else:\n cls._configuration = configuration"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating HDX configuration object.", "response": "def _create(cls, configuration=None, remoteckan=None, **kwargs):\n # type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str\n \"\"\"\n Create HDX configuration\n\n Args:\n configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.\n remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.\n **kwargs: See below\n user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.\n user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.\n user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.\n hdx_url (str): HDX url to use. Overrides hdx_site.\n hdx_site (str): HDX site to use eg. prod, test.\n hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.\n hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.\n hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR\n hdx_config_json (str): Path to JSON HDX configuration OR\n hdx_config_yaml (str): Path to YAML HDX configuration\n project_config_dict (dict): Project configuration dictionary OR\n project_config_json (str): Path to JSON Project configuration OR\n project_config_yaml (str): Path to YAML Project configuration\n hdx_base_config_dict (dict): HDX base configuration dictionary OR\n hdx_base_config_json (str): Path to JSON HDX base configuration OR\n hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.\n\n Returns:\n str: HDX site url\n\n \"\"\"\n kwargs = cls._environment_variables(**kwargs)\n cls.setup(configuration, **kwargs)\n cls._configuration.setup_remoteckan(remoteckan, **kwargs)\n return cls._configuration.get_hdx_site_url()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create(cls, configuration=None, remoteckan=None, **kwargs):\n # type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str\n \"\"\"\n Create HDX configuration. Can only be called once (will raise an error if called more than once).\n\n Args:\n configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.\n remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.\n **kwargs: See below\n user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.\n user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.\n user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.\n hdx_url (str): HDX url to use. Overrides hdx_site.\n hdx_site (str): HDX site to use eg. prod, test.\n hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.\n hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.\n hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR\n hdx_config_json (str): Path to JSON HDX configuration OR\n hdx_config_yaml (str): Path to YAML HDX configuration\n project_config_dict (dict): Project configuration dictionary OR\n project_config_json (str): Path to JSON Project configuration OR\n project_config_yaml (str): Path to YAML Project configuration\n hdx_base_config_dict (dict): HDX base configuration dictionary OR\n hdx_base_config_json (str): Path to JSON HDX base configuration OR\n hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.\n\n Returns:\n str: HDX site url\n\n \"\"\"\n if cls._configuration is not None:\n raise ConfigurationError('Configuration already created!')\n return cls._create(configuration=configuration, remoteckan=remoteckan, **kwargs)", "response": "Create HDX configuration object."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert a dictionary into a string with assignments", "response": "def kwargs_to_variable_assignment(kwargs: dict, value_representation=repr,\n assignment_operator: str = ' = ',\n statement_separator: str = '\\n',\n statement_per_line: bool = False) -> str:\n \"\"\"\n Convert a dictionary into a string with assignments\n\n Each assignment is constructed based on:\n key assignment_operator value_representation(value) statement_separator,\n where key and value are the key and value of the dictionary.\n Moreover one can seprate the assignment statements by new lines.\n\n Parameters\n ----------\n kwargs : dict\n\n assignment_operator: str, optional:\n Assignment operator (\" = \" in python)\n value_representation: str, optinal\n How to represent the value in the assignments (repr function in python)\n statement_separator : str, optional:\n Statement separator (new line in python)\n statement_per_line: bool, optional\n Insert each statement on a different line\n\n Returns\n -------\n str\n All the assignemnts.\n\n >>> kwargs_to_variable_assignment({'a': 2, 'b': \"abc\"})\n \"a = 2\\\\nb = 'abc'\\\\n\"\n >>> kwargs_to_variable_assignment({'a':2 ,'b': \"abc\"}, statement_per_line=True)\n \"a = 2\\\\n\\\\nb = 'abc'\\\\n\"\n >>> kwargs_to_variable_assignment({'a': 2})\n 'a = 2\\\\n'\n >>> kwargs_to_variable_assignment({'a': 2}, statement_per_line=True)\n 'a = 2\\\\n'\n \"\"\"\n code = []\n join_str = '\\n' if statement_per_line else ''\n for key, value in kwargs.items():\n code.append(key + assignment_operator +\n value_representation(value)+statement_separator)\n return join_str.join(code)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decode_json(json_input: Union[str, None] = None):\n if json_input is None:\n return {}\n else:\n if isinstance(json_input, str) is False:\n raise TypeError()\n elif json_input[-5:] == \".json\":\n with open(json_input) as f:\n decoded_json = json.load(f)\n else:\n decoded_json = json.loads(json_input)\n return decoded_json", "response": "Simple wrapper of json. load and json. loads."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if an object is json serialisable.", "response": "def is_jsonable(obj) -> bool:\n \"\"\"\n Check if an object is jsonable.\n\n An object is jsonable if it is json serialisable and by loading its json representation the same object is recovered.\n Parameters\n ----------\n obj : \n Python object\n\n Returns\n -------\n bool\n\n >>> is_jsonable([1,2,3])\n True\n >>> is_jsonable((1,2,3))\n False\n >>> is_jsonable({'a':True,'b':1,'c':None})\n True\n \"\"\"\n try:\n return obj==json.loads(json.dumps(obj))\n except TypeError:\n return False\n except:\n raise"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if an expresion can be literal_eval.", "response": "def is_literal_eval(node_or_string) -> tuple:\n \"\"\"\n Check if an expresion can be literal_eval.\n\n ----------\n node_or_string : \n Input\n\n Returns\n -------\n tuple\n (bool,python object)\n\n \n If it can be literal_eval the python object is returned. Otherwise None it is returned.\n \n >>> is_literal_eval('[1,2,3]')\n (True, [1, 2, 3])\n >>> is_literal_eval('a')\n (False, None)\n \"\"\"\n try:\n obj=ast.literal_eval(node_or_string)\n return (True, obj)\n except:\n return (False, None)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the duplicates in a list.", "response": "def find_duplicates(l: list) -> set:\n \"\"\"\n Return the duplicates in a list.\n\n The function relies on\n https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .\n Parameters\n ----------\n l : list\n Name\n\n Returns\n -------\n set\n Duplicated values\n\n >>> find_duplicates([1,2,3])\n set()\n >>> find_duplicates([1,2,1])\n {1}\n \"\"\"\n return set([x for x in l if l.count(x) > 1])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sort_dict(d: dict, by: str = 'key',\n allow_duplicates: bool = True) -> collections.OrderedDict:\n \"\"\"\n Sort a dictionary by key or value.\n\n The function relies on\n https://docs.python.org/3/library/collections.html#collections.OrderedDict .\n The dulicated are determined based on\n https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .\n Parameters\n ----------\n d : dict\n Input dictionary\n by : ['key','value'], optional\n By what to sort the input dictionary\n allow_duplicates : bool, optional\n Flag to indicate if the duplicates are allowed.\n Returns\n -------\n collections.OrderedDict\n Sorted dictionary.\n\n >>> sort_dict({2: 3, 1: 2, 3: 1})\n OrderedDict([(1, 2), (2, 3), (3, 1)])\n >>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')\n OrderedDict([(3, 1), (1, 2), (2, 3)])\n >>> sort_dict({'2': 3, '1': 2}, by='value')\n OrderedDict([('1', 2), ('2', 3)])\n >>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)\n Traceback (most recent call last):\n ...\n ValueError: There are duplicates in the values: {1}\n >>> sort_dict({1:1,2:3},by=True)\n Traceback (most recent call last):\n ...\n ValueError: by can be 'key' or 'value'.\n \"\"\"\n if by == 'key':\n i = 0\n elif by == 'value':\n values = list(d.values())\n if len(values) != len(set(values)) and not allow_duplicates:\n duplicates = find_duplicates(values)\n raise ValueError(\"There are duplicates in the values: {}\".format(duplicates))\n i = 1\n else:\n raise ValueError(\"by can be 'key' or 'value'.\")\n\n return collections.OrderedDict(sorted(d.items(), key=lambda t: t[i]))", "response": "Sort a dictionary by key or value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngrouping a dictionary by values.", "response": "def group_dict_by_value(d: dict) -> dict:\n \"\"\"\n Group a dictionary by values.\n\n\n Parameters\n ----------\n d : dict\n Input dictionary\n\n Returns\n -------\n dict\n Output dictionary. The keys are the values of the initial dictionary\n and the values ae given by a list of keys corresponding to the value.\n\n >>> group_dict_by_value({2: 3, 1: 2, 3: 1})\n {3: [2], 2: [1], 1: [3]}\n >>> group_dict_by_value({2: 3, 1: 2, 3: 1, 10:1, 12: 3})\n {3: [2, 12], 2: [1], 1: [3, 10]}\n \"\"\"\n d_out = {}\n for k, v in d.items():\n if v in d_out:\n d_out[v].append(k)\n else:\n d_out[v] = [k]\n return d_out"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef variable_status(code: str,\n exclude_variable: Union[set, None] = None,\n jsonable_parameter: bool = True) -> tuple:\n \"\"\"\n Find the possible parameters and \"global\" variables from a python code.\n\n This is achieved by parsing the abstract syntax tree.\n\n Parameters\n ----------\n code : str\n Input code as string.\n exclude_variable : set, None, optional\n Variable to exclude.\n jsonable_parameter: bool, True, optional\n Consider only jsonable parameter\n\n Returns\n -------\n tuple\n (a set of possible parameter, a set of parameter to exclude, a dictionary of possible parameter )\n\n A variable is a possible parameter if 1) it is not in the input exclude_variable,\n 2) the code contains only assignments, and 3) it is used only to bound objects.\n\n The set of parameter to exclude is the union of the input exclude_variable and all names that looks like a global variable.\n The dictionary of possible parameter {parameter name, parameter value} is available only if jsonable_parameter is True. \n\n\n\n\n >>> variable_status(\"a=3\")\n ({'a'}, {'a'}, {'a': 3})\n >>> variable_status(\"a=3\",jsonable_parameter=False)\n ({'a'}, {'a'}, {})\n >>> variable_status(\"a += 1\")\n (set(), {'a'}, {})\n >>> variable_status(\"def f(x,y=3):\\\\n\\\\t pass\")\n (set(), {'f'}, {})\n >>> variable_status(\"class C(A):\\\\n\\\\t pass\")\n (set(), {'C'}, {})\n >>> variable_status(\"import f\")\n (set(), {'f'}, {})\n >>> variable_status(\"import f as g\")\n (set(), {'g'}, {})\n >>> variable_status(\"from X import f\")\n (set(), {'f'}, {})\n >>> variable_status(\"from X import f as g\")\n (set(), {'g'}, {})\n \"\"\"\n if exclude_variable is None:\n exclude_variable = set()\n else:\n exclude_variable = copy.deepcopy(exclude_variable)\n root = ast.parse(code)\n store_variable_name = set()\n assign_only = True\n dict_parameter={}\n\n for node in ast.iter_child_nodes(root):\n if isinstance(node, ast.Assign):\n for assign_node in ast.walk(node):\n if isinstance(assign_node, ast.Name):\n\n if isinstance(assign_node.ctx, ast.Store):\n if jsonable_parameter is False:\n store_variable_name |= {assign_node.id}\n else:\n exclude_variable |= {assign_node.id}\n\n _is_literal_eval,_value=is_literal_eval(node.value)\n\n if jsonable_parameter is True:\n for assign_node in ast.iter_child_nodes(node):\n if isinstance(assign_node, ast.Tuple):\n i=0\n for assign_tuple_node in ast.iter_child_nodes(assign_node):\n if isinstance(assign_tuple_node, ast.Name):\n\n if isinstance(_value,(collections.Iterable)) and is_jsonable(_value[i]) and _is_literal_eval:\n dict_parameter[assign_tuple_node.id]=_value[i] \n store_variable_name |= {assign_tuple_node.id} \n else:\n exclude_variable |= {assign_tuple_node.id}\n i += 1\n else:\n if isinstance(assign_node, ast.Name):\n if is_jsonable(_value) and _is_literal_eval:\n dict_parameter[assign_node.id]=_value\n store_variable_name |= {assign_node.id} \n else:\n exclude_variable |= {assign_node.id}\n\n elif isinstance(node, ast.AugAssign):\n for assign_node in ast.walk(node):\n if isinstance(assign_node, ast.Name):\n exclude_variable |= {assign_node.id}\n # class and function\n elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):\n assign_only = False\n exclude_variable |= {node.name}\n # import\n elif isinstance(node, ast.Import):\n assign_only = False\n for node1 in ast.iter_child_nodes(node):\n if node1.asname is not None:\n exclude_variable |= {node1.asname}\n else:\n exclude_variable |= {node1.name}\n # import from\n elif isinstance(node, ast.ImportFrom):\n assign_only = False\n for node1 in ast.iter_child_nodes(node):\n if node1.asname is not None:\n exclude_variable |= {node1.asname}\n else:\n exclude_variable |= {node1.name}\n else:\n assign_only = False\n if assign_only is True:\n possible_parameter = store_variable_name-exclude_variable\n if jsonable_parameter is True:\n dict_parameter = {k:dict_parameter[k] for k in possible_parameter}\n return (possible_parameter, store_variable_name | exclude_variable, dict_parameter)\n return set(), store_variable_name | exclude_variable, {}", "response": "Return the list of possible parameters and global variables from a python code."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nincrements the name where the incremental part is given by parameters.", "response": "def increment_name(name: str, start_marker: str = \" (\",\n end_marker: str = \")\") -> str:\n \"\"\"\n Increment the name where the incremental part is given by parameters.\n\n Parameters\n ----------\n name : str, nbformat.notebooknode.NotebookNode\n Name\n start_marker : str\n The marker used before the incremental\n end_marker : str\n The marker after the incrementa\n\n Returns\n -------\n str\n Incremented name.\n\n >>> increment_name('abc')\n 'abc (1)'\n >>> increment_name('abc(1)')\n 'abc(1) (1)'\n >>> increment_name('abc (123)')\n 'abc (124)'\n >>> increment_name('abc-1',start_marker='-',end_marker='')\n 'abc-2'\n >>> increment_name('abc[2]',start_marker='[',end_marker=']')\n 'abc[3]'\n >>> increment_name('abc1',start_marker='',end_marker='')\n Traceback (most recent call last):\n ...\n ValueError: start_marker can not be the empty string.\n \"\"\"\n if start_marker == '':\n raise ValueError(\"start_marker can not be the empty string.\")\n a = name\n start = len(a)-a[::-1].find(start_marker[::-1])\n\n if (a[len(a)-len(end_marker):len(a)] == end_marker\n and start < (len(a)-len(end_marker))\n and a[start-len(start_marker):start] == start_marker\n and a[start:len(a)-len(end_marker)].isdigit()):\n\n old_int = int(a[start:len(a)-len(end_marker)])\n new_int = old_int+1\n new_name = a[:start]+str(new_int)+end_marker\n else:\n new_name = a+start_marker+'1'+end_marker\n return new_name"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['ResourceView']\n \"\"\"Reads the resource view given by identifier from HDX and returns ResourceView object\n\n Args:\n identifier (str): Identifier of resource view\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[ResourceView]: ResourceView object if successful read, None if not\n \"\"\"\n\n resourceview = ResourceView(configuration=configuration)\n result = resourceview._load_from_hdx('resource view', identifier)\n if result:\n return resourceview\n return None", "response": "Reads the resource view given by identifier from HDX and returns ResourceView object if successful."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_for_resource(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> List['ResourceView']\n \"\"\"Read all resource views for a resource given by identifier from HDX and returns list of ResourceView objects\n\n Args:\n identifier (str): Identifier of resource\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n List[ResourceView]: List of ResourceView objects\n \"\"\"\n\n resourceview = ResourceView(configuration=configuration)\n success, result = resourceview._read_from_hdx('resource view', identifier, 'id', ResourceView.actions()['list'])\n resourceviews = list()\n if success:\n for resourceviewdict in result:\n resourceview = ResourceView(resourceviewdict, configuration=configuration)\n resourceviews.append(resourceview)\n return resourceviews", "response": "Read all resource views for a resource given by identifier from HDX and return list of ResourceView objects."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if resource view exists in HDX and if so update resource view", "response": "def _update_resource_view(self, log=False):\n # type: () -> bool\n \"\"\"Check if resource view exists in HDX and if so, update resource view\n\n Returns:\n bool: True if updated and False if not\n \"\"\"\n update = False\n if 'id' in self.data and self._load_from_hdx('resource view', self.data['id']):\n update = True\n else:\n if 'resource_id' in self.data:\n resource_views = self.get_all_for_resource(self.data['resource_id'])\n for resource_view in resource_views:\n if self.data['title'] == resource_view['title']:\n self.old_data = self.data\n self.data = resource_view.data\n update = True\n break\n if update:\n if log:\n logger.warning('resource view exists. Updating %s' % self.data['id'])\n self._merge_hdx_update('resource view', 'id')\n return update"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_in_hdx(self):\n # type: () -> None\n \"\"\"Check if resource view exists in HDX and if so, update it, otherwise create resource view\n\n Returns:\n None\n \"\"\"\n self.check_required_fields()\n if not self._update_resource_view(log=True):\n self._save_to_hdx('create', 'title')", "response": "Check if resource view exists in HDX and if so create it otherwise create resource view\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncopies all fields except id resource_id and package_id from another resource view.", "response": "def copy(self, resource_view):\n # type: (Union[ResourceView,Dict,str]) -> None\n \"\"\"Copies all fields except id, resource_id and package_id from another resource view.\n\n Args:\n resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary\n\n Returns:\n None\n \"\"\"\n if isinstance(resource_view, str):\n if is_valid_uuid(resource_view) is False:\n raise HDXError('%s is not a valid resource view id!' % resource_view)\n resource_view = ResourceView.read_from_hdx(resource_view)\n if not isinstance(resource_view, dict) and not isinstance(resource_view, ResourceView):\n raise HDXError('%s is not a valid resource view!' % resource_view)\n for key in resource_view:\n if key not in ('id', 'resource_id', 'package_id'):\n self.data[key] = resource_view[key]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef tagscleanupdicts(configuration=None, url=None, keycolumn=5, failchained=True):\n # type: (Optional[Configuration], Optional[str], int, bool) -> Tuple[Dict,List]\n \"\"\"\n Get tags cleanup dictionaries\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n url (Optional[str]): Url of tags cleanup spreadsheet. Defaults to None (internal configuration parameter).\n keycolumn (int): Column number of tag column in spreadsheet. Defaults to 5.\n failchained (bool): Fail if chained rules found. Defaults to True.\n\n Returns:\n Tuple[Dict,List]: Returns (Tags dictionary, Wildcard tags list)\n \"\"\"\n if not Tags._tags_dict:\n if configuration is None:\n configuration = Configuration.read()\n with Download(full_agent=configuration.get_user_agent()) as downloader:\n if url is None:\n url = configuration['tags_cleanup_url']\n Tags._tags_dict = downloader.download_tabular_rows_as_dicts(url, keycolumn=keycolumn)\n keys = Tags._tags_dict.keys()\n chainerror = False\n for i, tag in enumerate(keys):\n whattodo = Tags._tags_dict[tag]\n action = whattodo[u'action']\n final_tags = whattodo[u'final tags (semicolon separated)']\n for final_tag in final_tags.split(';'):\n if final_tag in keys:\n index = list(keys).index(final_tag)\n if index != i:\n whattodo2 = Tags._tags_dict[final_tag]\n action2 = whattodo2[u'action']\n if action2 != 'OK' and action2 != 'Other':\n final_tags2 = whattodo2[u'final tags (semicolon separated)']\n if final_tag not in final_tags2.split(';'):\n chainerror = True\n if failchained:\n logger.error('Chained rules: %s (%s -> %s) | %s (%s -> %s)' %\n (action, tag, final_tags, action2, final_tag, final_tags2))\n\n if failchained and chainerror:\n raise ChainRuleError('Chained rules for tags detected!')\n Tags._wildcard_tags = list()\n for tag in Tags._tags_dict:\n if '*' in tag:\n Tags._wildcard_tags.append(tag)\n return Tags._tags_dict, Tags._wildcard_tags", "response": "Get tags cleanup dictionaries."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['User']\n \"\"\"Reads the user given by identifier from HDX and returns User object\n\n Args:\n identifier (str): Identifier of user\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[User]: User object if successful read, None if not\n \"\"\"\n\n user = User(configuration=configuration)\n result = user._load_from_hdx('user', identifier)\n if result:\n return user\n return None", "response": "Reads the user given by identifier from HDX and returns User object if successful."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_in_hdx(self):\n # type: () -> None\n \"\"\"Check if user exists in HDX and if so, update user\n\n Returns:\n None\n \"\"\"\n capacity = self.data.get('capacity')\n if capacity is not None:\n del self.data['capacity'] # remove capacity (which comes from users from Organization)\n self._update_in_hdx('user', 'id')\n if capacity is not None:\n self.data['capacity'] = capacity", "response": "Check if user exists in HDX and if so update user\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if user exists in HDX and if so create it otherwise create user", "response": "def create_in_hdx(self):\n # type: () -> None\n \"\"\"Check if user exists in HDX and if so, update it, otherwise create user\n\n Returns:\n None\n \"\"\"\n capacity = self.data.get('capacity')\n if capacity is not None:\n del self.data['capacity']\n self._create_in_hdx('user', 'id', 'name')\n if capacity is not None:\n self.data['capacity'] = capacity"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef email(self, subject, text_body, html_body=None, sender=None, **kwargs):\n # type: (str, str, Optional[str], Optional[str], Any) -> None\n \"\"\"Emails a user.\n\n Args:\n subject (str): Email subject\n text_body (str): Plain text email body\n html_body (str): HTML email body\n sender (Optional[str]): Email sender. Defaults to SMTP username.\n **kwargs: See below\n mail_options (List): Mail options (see smtplib documentation)\n rcpt_options (List): Recipient options (see smtplib documentation)\n\n Returns:\n None\n \"\"\"\n self.configuration.emailer().send([self.data['email']], subject, text_body, html_body=html_body, sender=sender,\n **kwargs)", "response": "Emails a user.\n\n Args:\n subject (str): Email subject\n text_body (str): Plain text email body\n html_body (str): HTML email body\n sender (Optional[str]): Email sender. Defaults to SMTP username.\n **kwargs: See below\n mail_options (List): Mail options (see smtplib documentation)\n rcpt_options (List): Recipient options (see smtplib documentation)\n\n Returns:\n None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all users in HDX", "response": "def get_all_users(configuration=None, **kwargs):\n # type: (Optional[Configuration], Any) -> List['User']\n \"\"\"Get all users in HDX\n\n Args:\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n **kwargs: See below\n q (str): Restrict to names containing a string. Defaults to all users.\n order_by (str): Field by which to sort - any user field or edits (number_of_edits). Defaults to 'name'.\n\n Returns:\n List[User]: List of all users in HDX\n \"\"\"\n user = User(configuration=configuration)\n user['id'] = 'all users' # only for error message if produced\n result = user._write_to_hdx('list', kwargs, 'id')\n users = list()\n if result:\n for userdict in result:\n user = User(userdict, configuration=configuration)\n users.append(user)\n else:\n logger.debug(result)\n return users"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef email_users(users, subject, text_body, html_body=None, sender=None, configuration=None, **kwargs):\n # type: (List['User'], str, str, Optional[str], Optional[str], Optional[Configuration], Any) -> None\n \"\"\"Email a list of users\n\n Args:\n users (List[User]): List of users\n subject (str): Email subject\n text_body (str): Plain text email body\n html_body (str): HTML email body\n sender (Optional[str]): Email sender. Defaults to SMTP username.\n configuration (Optional[Configuration]): HDX configuration. Defaults to configuration of first user in list.\n **kwargs: See below\n mail_options (List): Mail options (see smtplib documentation)\n rcpt_options (List): Recipient options (see smtplib documentation)\n\n Returns:\n None\n \"\"\"\n if not users:\n raise ValueError('No users supplied')\n recipients = list()\n for user in users:\n recipients.append(user.data['email'])\n if configuration is None:\n configuration = users[0].configuration\n configuration.emailer().send(recipients, subject, text_body, html_body=html_body, sender=sender, **kwargs)", "response": "Email a list of users to the HDX server."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_organizations(self, permission='read'):\n # type: (str) -> List['Organization']\n \"\"\"Get organizations in HDX that this user is a member of.\n\n Args:\n permission (str): Permission to check for. Defaults to 'read'.\n\n Returns:\n List[Organization]: List of organizations in HDX that this user is a member of\n \"\"\"\n success, result = self._read_from_hdx('user', self.data['name'], 'id', self.actions()['listorgs'],\n permission=permission)\n organizations = list()\n if success:\n for organizationdict in result:\n organization = hdx.data.organization.Organization.read_from_hdx(organizationdict['id'])\n organizations.append(organization)\n return organizations", "response": "Get organizations in HDX that this user is a member of."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntrying loading the config from the given path. If no path is specified the default config is returned.", "response": "def get_lint_config(config_path=None):\n \"\"\" Tries loading the config from the given path. If no path is specified, the default config path\n is tried, and if that is not specified, we the default config is returned. \"\"\"\n # config path specified\n if config_path:\n config = LintConfig.load_from_file(config_path)\n click.echo(\"Using config from {0}\".format(config_path))\n # default config path\n elif os.path.exists(DEFAULT_CONFIG_FILE):\n config = LintConfig.load_from_file(DEFAULT_CONFIG_FILE)\n click.echo(\"Using config from {0}\".format(DEFAULT_CONFIG_FILE))\n # no config file\n else:\n config = LintConfig()\n\n return config"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef main():\n # Read configuration from the config file if present, else fall back to\n # command line options\n if args.config:\n config = config_file_parser.get_configuration(args.config)\n access_key_id = config['access-key-id']\n secret_access_key = config['secret-access-key']\n region = config['region']\n else:\n access_key_id = args.access_key_id\n secret_access_key = args.secret_access_key\n region = args.region\n\n if args.daemon:\n pid_file = '/tmp/automatic-ebs-snapshots.pid'\n daemon = AutoEBSDaemon(pid_file)\n\n if args.daemon == 'start':\n daemon.start()\n\n elif args.daemon == 'stop':\n daemon.stop()\n sys.exit(0)\n\n elif args.daemon == 'restart':\n daemon.restart()\n\n elif args.daemon in ['foreground', 'fg']:\n daemon.run()\n\n else:\n print 'Valid options for --daemon are start, stop and restart'\n sys.exit(1)\n\n # Connect to AWS\n connection = connection_manager.connect_to_ec2(\n region, access_key_id, secret_access_key)\n\n if args.watch:\n volume_manager.watch(\n connection,\n args.watch,\n args.interval,\n args.retention)\n\n if args.unwatch:\n volume_manager.unwatch(connection, args.unwatch)\n\n if args.watch_file:\n volume_manager.watch_from_file(connection, args.watch_file)\n\n if args.unwatch_file:\n volume_manager.unwatch_from_file(connection, args.unwatch_file)\n\n if args.snapshots:\n volume_manager.list_snapshots(connection, args.snapshots)\n\n if args.list:\n volume_manager.list(connection)\n\n if args.run:\n snapshot_manager.run(connection)", "response": "Main function for the main function of the main function."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run(self, check_interval=300):\n while True:\n # Read configuration from the config file if present, else fall\n # back to command line options\n if args.config:\n config = config_file_parser.get_configuration(args.config)\n access_key_id = config['access-key-id']\n secret_access_key = config['secret-access-key']\n region = config['region']\n else:\n access_key_id = args.access_key_id\n secret_access_key = args.secret_access_key\n region = args.region\n\n # Connect to AWS\n connection = connection_manager.connect_to_ec2(\n region, access_key_id, secret_access_key)\n\n snapshot_manager.run(connection)\n\n logger.info('Sleeping {} seconds until next check'.format(\n check_interval))\n time.sleep(check_interval)", "response": "Run the daemon daemon"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\niterates over the lines in a given markdown string and applies all the enabled line rules to each line.", "response": "def _apply_line_rules(self, markdown_string):\n \"\"\" Iterates over the lines in a given markdown string and applies all the enabled line rules to each line \"\"\"\n all_violations = []\n lines = markdown_string.split(\"\\n\")\n line_rules = self.line_rules\n line_nr = 1\n ignoring = False\n for line in lines:\n if ignoring:\n if line.strip() == '':\n ignoring = False\n else:\n if line.strip() == '':\n ignoring = True\n continue\n\n for rule in line_rules:\n violation = rule.validate(line)\n if violation:\n violation.line_nr = line_nr\n all_violations.append(violation)\n line_nr += 1\n return all_violations"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlint a list of files.", "response": "def lint_files(self, files):\n \"\"\" Lints a list of files.\n :param files: list of files to lint\n :return: a list of violations found in the files\n \"\"\"\n all_violations = []\n for filename in files:\n with open(filename, 'r') as f:\n content = f.read()\n violations = self.lint(content)\n all_violations.extend(violations)\n for e in violations:\n print(\"{0}:{1}: {2} {3}\".format(filename, e.line_nr, e.rule_id, e.message))\n return len(all_violations)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading the values from a byte stream.", "response": "def ReadFrom(self, byte_stream):\n \"\"\"Read values from a byte stream.\n\n Args:\n byte_stream (bytes): byte stream.\n\n Returns:\n tuple[object, ...]: values copies from the byte stream.\n\n Raises:\n IOError: if byte stream cannot be read.\n OSError: if byte stream cannot be read.\n \"\"\"\n try:\n return self._struct.unpack_from(byte_stream)\n except (TypeError, struct.error) as exception:\n raise IOError('Unable to read byte stream with error: {0!s}'.format(\n exception))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef WriteTo(self, values):\n try:\n return self._struct.pack(*values)\n except (TypeError, struct.error) as exception:\n raise IOError('Unable to write stream with error: {0!s}'.format(\n exception))", "response": "Writes values to a byte stream."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure that we have snapshots for a given volume.", "response": "def run(connection):\n \"\"\" Ensure that we have snapshots for a given volume\n\n :type connection: boto.ec2.connection.EC2Connection\n :param connection: EC2 connection object\n :returns: None\n \"\"\"\n volumes = volume_manager.get_watched_volumes(connection)\n\n for volume in volumes:\n _ensure_snapshot(connection, volume)\n _remove_old_snapshots(connection, volume)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a new snapshot for the given volume", "response": "def _create_snapshot(volume):\n \"\"\" Create a new snapshot\n\n :type volume: boto.ec2.volume.Volume\n :param volume: Volume to snapshot\n :returns: boto.ec2.snapshot.Snapshot -- The new snapshot\n \"\"\"\n logger.info('Creating new snapshot for {}'.format(volume.id))\n snapshot = volume.create_snapshot(\n description=\"Automatic snapshot by Automated EBS Snapshots\")\n logger.info('Created snapshot {} for volume {}'.format(\n snapshot.id, volume.id))\n\n return snapshot"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _ensure_snapshot(connection, volume):\n if 'AutomatedEBSSnapshots' not in volume.tags:\n logger.warning(\n 'Missing tag AutomatedEBSSnapshots for volume {}'.format(\n volume.id))\n return\n\n interval = volume.tags['AutomatedEBSSnapshots']\n if volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:\n logger.warning(\n '\"{}\" is not a valid snapshotting interval for volume {}'.format(\n interval, volume.id))\n return\n\n snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})\n\n # Create a snapshot if we don't have any\n if not snapshots:\n _create_snapshot(volume)\n return\n\n min_delta = 3600*24*365*10 # 10 years :)\n for snapshot in snapshots:\n timestamp = datetime.datetime.strptime(\n snapshot.start_time,\n '%Y-%m-%dT%H:%M:%S.000Z')\n delta_seconds = int(\n (datetime.datetime.utcnow() - timestamp).total_seconds())\n\n if delta_seconds < min_delta:\n min_delta = delta_seconds\n\n logger.info('The newest snapshot for {} is {} seconds old'.format(\n volume.id, min_delta))\n if interval == 'hourly' and min_delta > 3600:\n _create_snapshot(volume)\n elif interval == 'daily' and min_delta > 3600*24:\n _create_snapshot(volume)\n elif interval == 'weekly' and min_delta > 3600*24*7:\n _create_snapshot(volume)\n elif interval == 'monthly' and min_delta > 3600*24*30:\n _create_snapshot(volume)\n elif interval == 'yearly' and min_delta > 3600*24*365:\n _create_snapshot(volume)\n else:\n logger.info('No need for a new snapshot of {}'.format(volume.id))", "response": "Ensures that a given volume has an appropriate snapshot."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving old snapshots from the list of old snapshots.", "response": "def _remove_old_snapshots(connection, volume):\n \"\"\" Remove old snapshots\n\n :type connection: boto.ec2.connection.EC2Connection\n :param connection: EC2 connection object\n :type volume: boto.ec2.volume.Volume\n :param volume: Volume to check\n :returns: None\n \"\"\"\n if 'AutomatedEBSSnapshotsRetention' not in volume.tags:\n logger.warning(\n 'Missing tag AutomatedEBSSnapshotsRetention for volume {}'.format(\n volume.id))\n return\n retention = int(volume.tags['AutomatedEBSSnapshotsRetention'])\n\n snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})\n\n # Sort the list based on the start time\n snapshots.sort(key=lambda x: x.start_time)\n\n # Remove snapshots we want to keep\n snapshots = snapshots[:-int(retention)]\n\n if not snapshots:\n logger.info('No old snapshots to remove')\n return\n\n for snapshot in snapshots:\n logger.info('Deleting snapshot {}'.format(snapshot.id))\n try:\n snapshot.delete()\n except EC2ResponseError as error:\n logger.warning('Could not remove snapshot: {}'.format(\n error.message))\n\n logger.info('Done deleting snapshots')"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist watched EBS volumes and return a new object.", "response": "def list(connection):\n \"\"\" List watched EBS volumes\n\n :type connection: boto.ec2.connection.EC2Connection\n :param connection: EC2 connection object\n :returns: None\n \"\"\"\n volumes = get_watched_volumes(connection)\n\n if not volumes:\n logger.info('No watched volumes found')\n return\n\n logger.info(\n '+-----------------------'\n '+----------------------'\n '+--------------'\n '+------------+')\n logger.info(\n '| {volume:<21} '\n '| {volume_name:<20.20} '\n '| {interval:<12} '\n '| {retention:<10} |'.format(\n volume='Volume ID',\n volume_name='Volume name',\n interval='Interval',\n retention='Retention'))\n logger.info(\n '+-----------------------'\n '+----------------------'\n '+--------------'\n '+------------+')\n\n for volume in volumes:\n if 'AutomatedEBSSnapshots' not in volume.tags:\n interval = 'Interval tag not found'\n elif volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:\n interval = 'Invalid interval'\n else:\n interval = volume.tags['AutomatedEBSSnapshots']\n\n if 'AutomatedEBSSnapshotsRetention' not in volume.tags:\n retention = 0\n else:\n retention = volume.tags['AutomatedEBSSnapshotsRetention']\n\n # Get the volume name\n try:\n volume_name = volume.tags['Name']\n except KeyError:\n volume_name = ''\n\n logger.info(\n '|\u00a0{volume_id:<14} '\n '| {volume_name:<20.20} '\n '| {interval:<12} '\n '| {retention:<10} |'.format(\n volume_id=volume.id,\n volume_name=volume_name,\n interval=interval,\n retention=retention))\n\n logger.info(\n '+-----------------------'\n '+----------------------'\n '+--------------'\n '+------------+')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef unwatch(connection, volume_id):\n try:\n volume = connection.get_all_volumes(volume_ids=[volume_id])[0]\n volume.remove_tag('AutomatedEBSSnapshots')\n except EC2ResponseError:\n pass\n\n logger.info('Removed {} from the watchlist'.format(volume_id))\n\n return True", "response": "Remove watching of a volume"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef watch(connection, volume_id, interval='daily', retention=0):\n try:\n volume = connection.get_all_volumes(volume_ids=[volume_id])[0]\n except EC2ResponseError:\n logger.warning('Volume {} not found'.format(volume_id))\n return False\n\n if interval not in VALID_INTERVALS:\n logger.warning(\n '{} is not a valid interval. Valid intervals are {}'.format(\n interval, ', '.join(VALID_INTERVALS)))\n\n # Remove the tag first\n volume.remove_tag('AutomatedEBSSnapshots')\n\n # Re-add the tag\n volume.add_tag('AutomatedEBSSnapshots', value=interval)\n\n # Remove the tag first\n volume.remove_tag('AutomatedEBSSnapshotsRetention')\n\n # Re-add the tag\n volume.add_tag('AutomatedEBSSnapshotsRetention', value=int(retention))\n\n logger.info('Updated the rotation interval to {} for {}'.format(\n interval, volume_id))\n\n return True", "response": "Start watching a new volume and add it to the watchlist"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_volume_id(connection, volume):\n # Regular expression to check whether input is a volume id\n volume_id_pattern = re.compile('vol-\\w{8}')\n\n if volume_id_pattern.match(volume):\n # input is volume id\n try:\n # Check whether it exists\n connection.get_all_volumes(volume_ids=[volume])\n volume_id = volume\n except EC2ResponseError:\n logger.warning('Volume {} not found'.format(volume))\n return None\n else:\n # input is volume name\n name_filter = {'tag-key': 'Name', 'tag-value': volume}\n volumes = connection.get_all_volumes(filters=name_filter)\n if not volumes:\n logger.warning('Volume {} not found'.format(volume))\n return None\n if len(volumes) > 1:\n logger.warning('Volume {} not unique'.format(volume))\n volume_id = volumes[0].id\n\n return volume_id", "response": "Get Volume ID from the given volume. Input can be volume id\n or volume Name\n . Returns None if the given volume does not exist."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef watch_from_file(connection, file_name):\n with open(file_name, 'r') as filehandle:\n for line in filehandle.xreadlines():\n volume, interval, retention = line.rstrip().split(',')\n watch(\n connection,\n get_volume_id(connection, volume),\n interval, retention)", "response": "Start watching a new block device from a file."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstarts watching a new volume :type connection: boto.ec2.connection.EC2Connection :param connection: EC2 connection object :type file_name: str :param file_name: path to config file :returns: None", "response": "def unwatch_from_file(connection, file_name):\n \"\"\" Start watching a new volume\n\n :type connection: boto.ec2.connection.EC2Connection\n :param connection: EC2 connection object\n :type file_name: str\n :param file_name: path to config file\n :returns: None\n \"\"\"\n with open(file_name, 'r') as filehandle:\n for line in filehandle.xreadlines():\n volume, interval, retention = line.rstrip().split(',')\n unwatch(connection, get_volume_id(connection, volume))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists all snapshots for the specified volume", "response": "def list_snapshots(connection, volume):\n \"\"\" List all snapshots for the volume\n\n :type connection: boto.ec2.connection.EC2Connection\n :param connection: EC2 connection object\n :type volume: str\n :param volume: Volume ID or Volume Name\n :returns: None\n \"\"\"\n\n logger.info(\n '+----------------'\n '+----------------------'\n '+---------------------------+')\n logger.info(\n '| {snapshot:<14} '\n '| {snapshot_name:<20.20} '\n '| {created:<25} |'.format(\n snapshot='Snapshot ID',\n snapshot_name='Snapshot name',\n created='Created'))\n logger.info(\n '+----------------'\n '+----------------------'\n '+---------------------------+')\n\n vid = get_volume_id(connection, volume)\n if vid:\n vol = connection.get_all_volumes(volume_ids=[vid])[0]\n for snap in vol.snapshots():\n logger.info(\n '| {snapshot:<14} '\n '| {snapshot_name:<20.20} '\n '| {created:<25} |'.format(\n snapshot=snap.id,\n snapshot_name=snap.tags.get('Name', ''),\n created=snap.start_time))\n\n logger.info(\n '+----------------'\n '+----------------------'\n '+---------------------------+')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting stems for the words using a given parser", "response": "def stem(self, words, parser, **kwargs):\n \"\"\"\n Get stems for the words using a given parser\n\n Example:\n from .parsing import ListParser\n\n parser = ListParser()\n stemmer = Morfologik()\n stemmer.stem(['ja ta\u0144cz\u0119 a ona \u015bpi], parser)\n\n [\n ('ja': ['ja']),\n ('ta\u0144cz\u0119': ['ta\u0144czy\u0107']),\n ('a': ['a']),\n ('ona': ['on']),\n ('\u015bpi': ['spa\u0107'])\n ]\n \"\"\"\n output = self._run_morfologik(words)\n return parser.parse(output, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrunning morfologik java jar and returns the result as unicode.", "response": "def _run_morfologik(self, words):\n \"\"\"\n Runs morfologik java jar and assumes that input and output is\n UTF-8 encoded.\n \"\"\"\n p = subprocess.Popen(\n ['java', '-jar', self.jar_path, 'plstem',\n '-ie', 'UTF-8',\n '-oe', 'UTF-8'],\n bufsize=-1,\n stdin=subprocess.PIPE,\n stdout=subprocess.PIPE,\n stderr=subprocess.STDOUT)\n out, _ = p.communicate(input=bytes(\"\\n\".join(words), \"utf-8\"))\n return decode(out, 'utf-8')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_from_hdx(identifier, configuration=None):\n # type: (str, Optional[Configuration]) -> Optional['Showcase']\n \"\"\"Reads the showcase given by identifier from HDX and returns Showcase object\n\n Args:\n identifier (str): Identifier of showcase\n configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.\n\n Returns:\n Optional[Showcase]: Showcase object if successful read, None if not\n \"\"\"\n\n showcase = Showcase(configuration=configuration)\n result = showcase._load_from_hdx('showcase', identifier)\n if result:\n return showcase\n return None", "response": "Reads the showcase given by identifier from HDX and returns the object if successful."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_datasets(self):\n # type: () -> List[hdx.data.dataset.Dataset]\n \"\"\"Get any datasets in the showcase\n\n Returns:\n List[Dataset]: List of datasets\n \"\"\"\n assoc_result, datasets_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='showcase_id',\n action=self.actions()['list_datasets'])\n datasets = list()\n if assoc_result:\n for dataset_dict in datasets_dicts:\n dataset = hdx.data.dataset.Dataset(dataset_dict, configuration=self.configuration)\n datasets.append(dataset)\n return datasets", "response": "Get any datasets in the showcase\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting showcase dataset dict from a showcase object or dict", "response": "def _get_showcase_dataset_dict(self, dataset):\n # type: (Union[hdx.data.dataset.Dataset,Dict,str]) -> Dict\n \"\"\"Get showcase dataset dict\n\n Args:\n showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary\n\n Returns:\n Dict: showcase dataset dict\n \"\"\"\n if isinstance(dataset, hdx.data.dataset.Dataset) or isinstance(dataset, dict):\n if 'id' not in dataset:\n dataset = hdx.data.dataset.Dataset.read_from_hdx(dataset['name'])\n dataset = dataset['id']\n elif not isinstance(dataset, str):\n raise hdx.data.hdxobject.HDXError('Type %s cannot be added as a dataset!' % type(dataset).__name__)\n if is_valid_uuid(dataset) is False:\n raise hdx.data.hdxobject.HDXError('%s is not a valid dataset id!' % dataset)\n return {'showcase_id': self.data['id'], 'package_id': dataset}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_dataset(self, dataset, datasets_to_check=None):\n # type: (Union[hdx.data.dataset.Dataset,Dict,str], List[hdx.data.dataset.Dataset]) -> bool\n \"\"\"Add a dataset\n\n Args:\n dataset (Union[Dataset,Dict,str]): Either a dataset id or dataset metadata either from a Dataset object or a dictionary\n datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.\n\n Returns:\n bool: True if the dataset was added, False if already present\n \"\"\"\n showcase_dataset = self._get_showcase_dataset_dict(dataset)\n if datasets_to_check is None:\n datasets_to_check = self.get_datasets()\n for dataset in datasets_to_check:\n if showcase_dataset['package_id'] == dataset['id']:\n return False\n self._write_to_hdx('associate', showcase_dataset, 'package_id')\n return True", "response": "Adds a dataset to the showcase."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd multiple datasets to the showcase.", "response": "def add_datasets(self, datasets, datasets_to_check=None):\n # type: (List[Union[hdx.data.dataset.Dataset,Dict,str]], List[hdx.data.dataset.Dataset]) -> bool\n \"\"\"Add multiple datasets\n\n Args:\n datasets (List[Union[Dataset,Dict,str]]): A list of either dataset ids or dataset metadata from Dataset objects or dictionaries\n datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.\n\n Returns:\n bool: True if all datasets added or False if any already present\n \"\"\"\n if datasets_to_check is None:\n datasets_to_check = self.get_datasets()\n alldatasetsadded = True\n for dataset in datasets:\n if not self.add_dataset(dataset, datasets_to_check=datasets_to_check):\n alldatasetsadded = False\n return alldatasetsadded"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef possible_parameter(nb, jsonable_parameter=True, end_cell_index=None):\n jh = _JupyterNotebookHelper(nb, jsonable_parameter, end_cell_index)\n\n if jsonable_parameter is True:\n PossibleParameter=collections.namedtuple('PossibleParameter',['name','value','cell_index'])\n else:\n PossibleParameter=collections.namedtuple('PossibleParameter',['name', 'cell_index'])\n\n res=[]\n for name, cell_index in jh.param_cell_index.items():\n if jsonable_parameter is True:\n res.append(PossibleParameter(name=name,value=jh.param_value[name],cell_index=cell_index))\n else:\n res.append(PossibleParameter(name=name,cell_index=cell_index))\n\n return sorted(res, key = lambda x: (x.name))", "response": "Find the possible parameters from a jupyter notebook."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns an input jupyter notebook file and optionally (python3 only) parametrise it. One can pass arguments as keyword arguments or in a json format (file or string). For safety reasons, in order to avoid any code injection, only json serialisable keywords arguments are available. The keyword arguments are firstly encoded in json format using the standard json encoder. The json content is decoded into python objects using the standard json decoder and it is mapped to a variable assignment by unpacking it. The assignments are appended at the end of the cell where they are initially defined. Parameters ---------- input_path : str Path of the input jupyter notebook. output_path : str, optional Path of the output jupyter notebook. One can use the input_path location as relative path by starting with \"///\" . * can be used once in the beggining or end (excluding the \".ipynb\" extension) as a wildcard of the input_path filename. \"///_run_jnb/*-output\" is the default value and states that the output is in the run_jnb folder with respect to the input_path directory and \"-output\" is appended to the input name. execution_path : str, optional The path of the folder where to execute the notebook. r'///input' or r'///output' can be used to denote the input / output folder. return_mode : ['parametrised_only','except',True, False], optional Flag to write the generated notebook to the output_path: \"parametrised_only\" writes the generated notebook without executing it, \"except\" writes in case of an exception, True writes always, False writes never. overwrite : bool, optional Flag to overwrite or not the output_path. If the parameter is False the used output_path will be incremented until a valid one is found. timeout : int, optional ExecutePreprocessor.timeout kernel_name : str, optional ExecutePreprocessor.kernel_name ep_kwargs : dict, optional Other kwargs accepted by nbconvert.preprocessors.ExecutePreprocessor jsonable_parameter: bool, optional Parametrise only jsonable parameters end_cell_index : int, optional End cell index used to slice the notebook in finding the possible parameters. arg : str Path of a json file (it should end in \".json\") or json formatted string used to parametrise the jupyter notebook. It should containt json objects. It is decoded into python objects following https://docs.python.org/3.6/library/json.html#json-to-py-table . kwargs: json serialsable keyword arguments used to parametrise the jupyter notebook. Returns ------- collections.namedtuple The fields are ('output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback'). If the generated file is written the output path is returned otherwise None. If an error is catched the details are return otherwise None.", "response": "def run_jnb(input_path, output_path=r\"///_run_jnb/*-output\",\n execution_path=r'///input',\n return_mode='except',\n overwrite=False,\n timeout=ExecutePreprocessor.timeout.default_value,\n kernel_name=ExecutePreprocessor.kernel_name.default_value,\n ep_kwargs=None, jsonable_parameter=True, end_cell_index=None, arg=None, **kwargs):\n \"\"\"\n Run an input jupyter notebook file and optionally (python3 only)\n parametrise it.\n\n One can pass arguments as keyword arguments or in a json format (file or string).\n For safety reasons, in order to avoid any code injection,\n only json serialisable keywords arguments are available. The keyword\n arguments are firstly encoded in json format using the standard json encoder.\n The json content is decoded into python objects using the standard json decoder\n and it is mapped to a variable assignment by unpacking it.\n The assignments are appended at the end of the cell where they are initially defined.\n\n\n Parameters\n ----------\n input_path : str\n Path of the input jupyter notebook.\n output_path : str, optional\n Path of the output jupyter notebook.\n One can use the input_path location as relative path by starting with \"///\" .\n * can be used once in the beggining or end (excluding the \".ipynb\" extension) as a wildcard of the input_path filename.\n \"///_run_jnb/*-output\" is the default value and states that the output\n is in the run_jnb folder with respect to the input_path directory and \"-output\" is appended to the input name.\n execution_path : str, optional\n The path of the folder where to execute the notebook.\n r'///input' or r'///output' can be used to denote the input / output folder.\n\n return_mode : ['parametrised_only','except',True, False], optional\n Flag to write the generated notebook to the output_path: \"parametrised_only\" writes the generated notebook without executing it, \"except\" writes in case of an exception, True writes always, False writes never.\n overwrite : bool, optional\n Flag to overwrite or not the output_path. If the parameter is False\n the used output_path will be incremented until a valid one is found.\n timeout : int, optional\n ExecutePreprocessor.timeout\n kernel_name : str, optional\n ExecutePreprocessor.kernel_name\n ep_kwargs : dict, optional\n Other kwargs accepted by nbconvert.preprocessors.ExecutePreprocessor\n jsonable_parameter: bool, optional\n Parametrise only jsonable parameters\n end_cell_index : int, optional\n End cell index used to slice the notebook in finding the possible parameters.\n arg : str\n Path of a json file (it should end in \".json\") or json formatted string used to parametrise the jupyter notebook.\n It should containt json objects. It is decoded into python objects following https://docs.python.org/3.6/library/json.html#json-to-py-table .\n kwargs:\n json serialsable keyword arguments used to parametrise the jupyter notebook.\n\n Returns\n -------\n collections.namedtuple\n The fields are ('output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback').\n If the generated file is written the output path is returned otherwise None.\n If an error is catched the details are return otherwise None.\n \"\"\"\n\n if os.path.splitext(input_path)[1] != '.ipynb':\n raise ValueError(\"The extension of input_path = '{}' is not '.ipynb'\".format(input_path))\n if os.path.basename(input_path) == '*':\n raise ValueError(\"The filename ={} can not start with *\".format(input_path))\n\n\n input_path_dir, input_path_base = os.path.split(input_path)\n if output_path.startswith(r'///'):\n input_rel = True\n output_path = output_path[3:]\n else:\n input_rel = False\n output_path_dir, output_path_base = os.path.split(output_path)\n\n if input_rel:\n output_path_dir = os.path.join(input_path_dir, output_path_dir)\n output_path_dir = os.path.normpath(output_path_dir)\n\n if os.path.exists(output_path_dir) is False:\n os.makedirs(output_path_dir)\n\n if output_path_base.endswith('.ipynb'):\n pass\n elif output_path_base.startswith(\"*\"):\n output_path_base = input_path_base[:-6]+output_path_base[1:]+'.ipynb'\n elif output_path_base.endswith(\"*\"):\n output_path_base = output_path_base[:-1]+input_path_base[:-6]+'.ipynb'\n else:\n raise ValueError(\"Invalid output_path\")\n\n output_path = os.path.abspath(os.path.join(output_path_dir,\n output_path_base))\n\n if output_path is not None and os.path.splitext(output_path)[1] != '.ipynb':\n raise ValueError(\"The extension of output_path = '{}' is not '.ipynb'\".format(output_path))\n\n if execution_path.startswith(r'///input'):\n execution_path = os.path.join(input_path_dir, execution_path[8:])\n elif execution_path.startswith(r'///output'):\n execution_path = os.path.join(input_path_dir, execution_path[8:])\n execution_path = os.path.normpath(execution_path)\n\n if os.path.exists(execution_path) is False:\n os.makedirs(execution_path)\n\n if ep_kwargs is None:\n ep_kwargs = {}\n\n if return_mode not in ['parametrised_only', 'except', True, False]:\n raise TypeError(\"return mode is not valid!\")\n\n kwarg_to_json = json.dumps(kwargs)\n kwarg_as_kwarg = decode_json(kwarg_to_json)\n arg_as_kwarg = decode_json(arg)\n\n multiple_kwarg = set(arg_as_kwarg.keys()) & set(kwarg_as_kwarg.keys())\n if multiple_kwarg != set():\n raise ValueError('Multiple values for keyword argument {}'.format(multiple_kwarg))\n jupyter_kwargs = {**arg_as_kwarg, **kwarg_as_kwarg}\n nb = _read_nb(input_path)\n\n # clean notebook\n for i, cell in enumerate(nb['cells']):\n if cell['cell_type'] == 'code':\n nb['cells'][i]['outputs'] = []\n nb['cells'][i]['execution_count'] = None\n\n if jupyter_kwargs != {}:\n params_of_interest = {}\n jnh = _JupyterNotebookHelper(nb, jsonable_parameter ,end_cell_index)\n for el in jupyter_kwargs.keys():\n if el not in jnh.param_cell_index.keys():\n raise ValueError(repr(el)+' is not a possible parameter {}.'.format(list(jnh.param_cell_index.keys())))\n else:\n params_of_interest[el] = jnh.param_cell_index[el]\n params_of_interest = sort_dict(params_of_interest, by='value')\n cell_index_param = group_dict_by_value(params_of_interest)\n for key, value in cell_index_param.items():\n cell_param = {k: jupyter_kwargs[k] for k in value}\n cell_code = kwargs_to_variable_assignment(cell_param)\n marked_code = _mark_auto_generated_code(cell_code)\n nb['cells'][key]['source'] += marked_code\n\n if return_mode != 'parametrised_only':\n ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel_name,\n **ep_kwargs)\n\n catch_except = False\n\n error = (None, None, None, None)\n try:\n if return_mode != 'parametrised_only':\n ep.preprocess(nb, {'metadata': {'path': execution_path}})\n except CellExecutionError:\n catch_except = True\n\n for cell in nb['cells'][::-1]:\n if cell['cell_type'] == 'code' and cell.get('outputs') != []:\n for output in cell['outputs']:\n if output.get('output_type') == 'error':\n error = (cell['execution_count'],\n output.get('ename'), output.get('evalue'),\n output.get('traceback'))\n break\n if error[0] is not None:\n break\n else:\n raise ValueError('Cell expected to have an error.')\n except:\n raise\n\n if return_mode == 'except':\n if catch_except is True:\n nb_return = True\n else:\n nb_return = None\n elif return_mode is True or return_mode == 'parametrised_only':\n nb_return = True\n elif return_mode is False:\n nb_return = None\n\n if nb_return is not None:\n if overwrite is False:\n while os.path.exists(output_path):\n dirname, basename = os.path.split(output_path)\n root, ext = os.path.splitext(basename)\n new_root = increment_name(root)\n output_path = os.path.join(dirname, new_root+ext)\n nb_return = output_path # update the output_path\n _write_nb(nb, output_path)\n Output=collections.namedtuple('Output',['output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback'])\n res = Output(output_nb_path=nb_return,error_prompt_number=error[0],\n error_type=error[1],error_value=error[2],error_traceback=error[3])\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef join(self, joiner, formatter=lambda s, t: t.format(s),\n template=\"{}\"):\n \"\"\"Join values and convert to string\n\n Example:\n\n >>> from ww import l\n >>> lst = l('012')\n >>> lst.join(',')\n u'0,1,2'\n >>> lst.join(',', template=\"{}#\")\n u'0#,1#,2#'\n >>> string = lst.join(',',\\\n formatter = lambda x, y: str(int(x) ** 2))\n >>> string\n u'0,1,4'\n \"\"\"\n\n return ww.s(joiner).join(self, formatter, template)", "response": "Join values and convert to string"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef append(self, *values):\n\n for value in values:\n list.append(self, value)\n return self", "response": "Append values at the end of the list."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef extend(self, *iterables):\n\n for value in iterables:\n list.extend(self, value)\n return self", "response": "Add all values of all iterables at the end of the list"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nnormalizing the value for writing into a cell.", "response": "def normalize_cell_value(value):\n \"\"\"Process value for writing into a cell.\n\n Args:\n value: any type of variable\n\n Returns:\n json serialized value if value is list or dict, else value\n \"\"\"\n if isinstance(value, dict) or isinstance(value, list):\n return json.dumps(value)\n return value"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_addresses_from_input_file(input_file_name):\n mode = 'r'\n if sys.version_info[0] < 3:\n mode = 'rb'\n with io.open(input_file_name, mode) as input_file:\n reader = csv.reader(input_file, delimiter=',', quotechar='\"')\n\n addresses = list(map(tuple, reader))\n\n if len(addresses) == 0:\n raise Exception('No addresses found in input file')\n\n header_columns = list(column.lower() for column in addresses.pop(0))\n\n try:\n address_index = header_columns.index('address')\n zipcode_index = header_columns.index('zipcode')\n except ValueError:\n raise Exception(\"\"\"The first row of the input CSV must be a header that contains \\\na column labeled 'address' and a column labeled 'zipcode'.\"\"\")\n\n return list((row[address_index], row[zipcode_index]) for row in addresses)", "response": "Read addresses from input file into list of tuples. This only supports address and zipcode headers."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_identifiers_from_input_file(input_file_name):\n valid_identifiers = ['address', 'zipcode', 'unit', 'city', 'state', 'slug', 'block_id', 'msa',\n 'num_bins', 'property_type', 'client_value', 'client_value_sqft', 'meta']\n mode = 'r'\n if sys.version_info[0] < 3:\n mode = 'rb'\n with io.open(input_file_name, mode) as input_file:\n result = [{identifier: val for identifier, val in list(row.items())\n if identifier in valid_identifiers}\n for row in csv.DictReader(input_file, skipinitialspace=True)]\n return result", "response": "Read identifiers from input file into list of dicts with the header row values\nATTRIBS as keys and the rest of the rows as values."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_real_time_locate(ipAddress, auth, url):\n real_time_locate_url = \"/imcrs/res/access/realtimeLocate?type=2&value=\" + str(ipAddress) + \"&total=false\"\n f_url = url + real_time_locate_url\n r = requests.get(f_url, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n response = json.loads(r.text)\n if 'realtimeLocation' in response:\n real_time_locate = json.loads(r.text)['realtimeLocation']\n if type(real_time_locate) is dict:\n real_time_locate = [real_time_locate]\n return real_time_locate\n else:\n return json.loads(r.text)['realtimeLocation']\n else:\n return json.loads(r.text)\n\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_real_time_locate: An Error has occured\"", "response": "This function returns the real time locate of the specified host."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfunction takes devId of specific device and issues a RESTFUL call to get the IP MAC ARP list of the target device.", "response": "def get_ip_mac_arp_list(devId, auth,url):\n \"\"\"\n function takes devid of specific device and issues a RESTFUL call to get the IP/MAC/ARP list from the target device.\n\n :param devId: int or str value of the target device.\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries containing the IP/MAC/ARP list of the target device.\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> ip_mac_list = get_ip_mac_arp_list('10', auth.creds, auth.url)\n\n >>> assert type(ip_mac_list) is list\n\n >>> assert 'deviceId' in ip_mac_list[0]\n\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n ip_mac_arp_list_url = \"/imcrs/res/access/ipMacArp/\" + str(devId)\n f_url = url + ip_mac_arp_list_url\n r = requests.get(f_url, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n macarplist = (json.loads(r.text))\n if len(macarplist) > 1:\n return macarplist['ipMacArp']\n else:\n return ['this function is unsupported']\n\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_ip_mac_arp_list: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_ip_scope_detail(scopeId, auth, url ):\n get_ip_scope_url = \"/imcrs/res/access/assignedIpScope/\"+str(scopeId)\n\n f_url = url + get_ip_scope_url\n r = requests.get(f_url, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n ipscopelist = (json.loads(r.text))\n return ipscopelist\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_ip_scope: An Error has occured\"", "response": "function takes input of scopeId and auth object and url and returns the list of IP address scopes currently configured on the HPE IMC server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning to delete an entire IP segment from the IMC IP Address management under terminal access", "response": "def delete_ip_scope(network_address, auth, url):\n '''Function to delete an entire IP segment from the IMC IP Address management under terminal access\n :param network_address\n :param auth\n :param url\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> new_scope = add_ip_scope('10.50.0.1', '10.50.0.254', 'cyoung', 'test group', auth.creds, auth.url)\n\n >>> delete_scope = delete_ip_scope('10.50.0.0/24', auth.creds, auth.url)\n\n\n '''\n scope_id = get_scope_id(network_address, auth,url)\n delete_ip_address_url = '''/imcrs/res/access/assignedIpScope/'''+str(scope_id)\n f_url = url + delete_ip_address_url\n r = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n return r\n if r.status_code == 204:\n #print(\"IP Segment Successfully Deleted\")\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" delete_ip_scope: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfunctions to add new host IP address allocation to existing scope ID", "response": "def add_scope_ip(ipaddress, name, description, scopeid, auth, url):\n \"\"\"\n Function to add new host IP address allocation to existing scope ID\n\n :param ipaddress:\n\n :param name: name of the owner of this host\n\n :param description: Description of the host\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return:\n\n :rtype:\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> add_scope_ip('10.50.0.5', 'cyoung', 'New Test Host','175', auth.creds, auth.url)\n\n \"\"\"\n new_ip = { \"ip\": ipaddress,\n \"name\": name,\n \"description\": description}\n add_scope_ip_url = '/imcrs/res/access/assignedIpScope/ip?ipScopeId='+str(scopeid)\n f_url = url + add_scope_ip_url\n payload = json.dumps(new_ip)\n r = requests.post(f_url, auth=auth, headers=HEADERS,\n data=payload) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n #print(\"IP Scope Successfully Created\")\n return r.status_code\n elif r.status_code == 409:\n #print(\"IP Scope Already Exists\")\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" add_ip_scope: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove_scope_ip(hostid, auth, url):\n add_scope_ip_url = '/imcrs/res/access/assignedIpScope/ip/'+str(hostid)\n f_url = url + add_scope_ip_url\n\n r = requests.delete(f_url, auth=auth, headers=HEADERS,\n )\n try:\n if r.status_code == 204:\n #print(\"Host Successfully Deleted\")\n return r.status_code\n elif r.status_code == 409:\n #print(\"IP Scope Already Exists\")\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" add_ip_scope: An Error has occured\"", "response": "Function to add remove IP address allocation to the base url of the host"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_ip_scope_hosts( scopeId, auth, url):\n get_ip_scope_url = \"/imcrs/res/access/assignedIpScope/ip?size=10000&ipScopeId=\"+str(scopeId)\n f_url = url + get_ip_scope_url\n r = requests.get(f_url, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n ipscopelist = (json.loads(r.text))\n if ipscopelist == {}:\n return ipscopelist\n else: ipscopelist = ipscopelist['assignedIpInfo']\n if type(ipscopelist) is dict:\n ipscope = []\n ipscope.append(ipscopelist)\n return ipscope\n return ipscopelist\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_ip_scope: An Error has occured\"", "response": "Function returns list of allocated IP address for the specified scope"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_host_to_segment(ipaddress, name, description, network_address, auth, url):\n ''' Function to abstract existing add_scope_ip_function. Allows for use of network address rather than forcing human\n to learn the scope_id\n :param ipaddress:\n\n :param name: name of the owner of this host\n\n :param description: Description of the host\n\n :param: network_address: network address of the target scope in format x.x.x.x/yy where x.x.x.x representents the\n network address and yy represents the length of the subnet mask. Example: 10.50.0.0 255.255.255.0 would be written\n as 10.50.0.0/24\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return:\n\n :rtype:\n\n\n\n '''\n scope_id = get_scope_id(network_address, auth, url)\n add_scope_ip(ipaddress, name, description, scope_id, auth,url)", "response": "Function to abstract existing add_scope_ip_function. Allows for use of network address rather than forcing human\n to learn the scope_id\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_host_from_segment(ipaddress, networkaddress, auth, url):\n '''Function to abstract\n\n '''\n host_id = get_host_id(ipaddress, networkaddress, auth, url)\n remove_scope_ip(host_id, auth.creds, auth.url)", "response": "Function to abstract\n remove host from segment"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef generate_signature(method, version, endpoint,\n date, rel_url, content_type, content,\n access_key, secret_key, hash_type):\n '''\n Generates the API request signature from the given parameters.\n '''\n hash_type = hash_type\n hostname = endpoint._val.netloc # FIXME: migrate to public API\n if version >= 'v4.20181215':\n content = b''\n else:\n if content_type.startswith('multipart/'):\n content = b''\n body_hash = hashlib.new(hash_type, content).hexdigest()\n\n sign_str = '{}\\n{}\\n{}\\nhost:{}\\ncontent-type:{}\\nx-backendai-version:{}\\n{}'.format( # noqa\n method.upper(),\n rel_url,\n date.isoformat(),\n hostname,\n content_type.lower(),\n version,\n body_hash\n )\n sign_bytes = sign_str.encode()\n\n sign_key = hmac.new(secret_key.encode(),\n date.strftime('%Y%m%d').encode(), hash_type).digest()\n sign_key = hmac.new(sign_key, hostname.encode(), hash_type).digest()\n\n signature = hmac.new(sign_key, sign_bytes, hash_type).hexdigest()\n headers = {\n 'Authorization': 'BackendAI signMethod=HMAC-{}, credential={}:{}'.format(\n hash_type.upper(),\n access_key,\n signature\n ),\n }\n return headers, signature", "response": "Generates the API request signature from the given parameters."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfetch the list of agents with the given status with limit and offset for pagination.", "response": "async def list_with_limit(cls,\n limit,\n offset,\n status: str = 'ALIVE',\n fields: Iterable[str] = None) -> Sequence[dict]:\n '''\n Fetches the list of agents with the given status with limit and offset for\n pagination.\n\n :param limit: number of agents to get\n :param offset: offset index of agents to get\n :param status: An upper-cased string constant representing agent\n status (one of ``'ALIVE'``, ``'TERMINATED'``, ``'LOST'``,\n etc.)\n :param fields: Additional per-agent query fields to fetch.\n '''\n if fields is None:\n fields = (\n 'id',\n 'addr',\n 'status',\n 'first_contact',\n 'mem_slots',\n 'cpu_slots',\n 'gpu_slots',\n )\n q = 'query($limit: Int!, $offset: Int!, $status: String) {' \\\n ' agent_list(limit: $limit, offset: $offset, status: $status) {' \\\n ' items { $fields }' \\\n ' total_count' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n variables = {\n 'limit': limit,\n 'offset': offset,\n 'status': status,\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['agent_list']"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_content(self, value: RequestContent, *,\n content_type: str = None):\n '''\n Sets the content of the request.\n '''\n assert self._attached_files is None, \\\n 'cannot set content because you already attached files.'\n guessed_content_type = 'application/octet-stream'\n if value is None:\n guessed_content_type = 'text/plain'\n self._content = b''\n elif isinstance(value, str):\n guessed_content_type = 'text/plain'\n self._content = value.encode('utf-8')\n else:\n guessed_content_type = 'application/octet-stream'\n self._content = value\n self.content_type = (content_type if content_type is not None\n else guessed_content_type)", "response": "Sets the content of the request."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nattach a list of files represented as AttachedFile.", "response": "def attach_files(self, files: Sequence[AttachedFile]):\n '''\n Attach a list of files represented as AttachedFile.\n '''\n assert not self._content, 'content must be empty to attach files.'\n self.content_type = 'multipart/form-data'\n self._attached_files = files"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncalculate the signature of the request and adds it to the headers.", "response": "def _sign(self, rel_url, access_key=None, secret_key=None, hash_type=None):\n '''\n Calculates the signature of the given request and adds the\n Authorization HTTP header.\n It should be called at the very end of request preparation and before\n sending the request to the server.\n '''\n if access_key is None:\n access_key = self.config.access_key\n if secret_key is None:\n secret_key = self.config.secret_key\n if hash_type is None:\n hash_type = self.config.hash_type\n hdrs, _ = generate_signature(\n self.method, self.config.version, self.config.endpoint,\n self.date, str(rel_url), self.content_type, self._content,\n access_key, secret_key, hash_type)\n self.headers.update(hdrs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsend a request to the server and reads the response.", "response": "def fetch(self, **kwargs) -> 'FetchContextManager':\n '''\n Sends the request to the server and reads the response.\n\n You may use this method either with plain synchronous Session or\n AsyncSession. Both the followings patterns are valid:\n\n .. code-block:: python3\n\n from ai.backend.client.request import Request\n from ai.backend.client.session import Session\n\n with Session() as sess:\n rqst = Request(sess, 'GET', ...)\n with rqst.fetch() as resp:\n print(resp.text())\n\n .. code-block:: python3\n\n from ai.backend.client.request import Request\n from ai.backend.client.session import AsyncSession\n\n async with AsyncSession() as sess:\n rqst = Request(sess, 'GET', ...)\n async with rqst.fetch() as resp:\n print(await resp.text())\n '''\n assert self.method in self._allowed_methods, \\\n 'Disallowed HTTP method: {}'.format(self.method)\n self.date = datetime.now(tzutc())\n self.headers['Date'] = self.date.isoformat()\n if self.content_type is not None:\n self.headers['Content-Type'] = self.content_type\n full_url = self._build_url()\n self._sign(full_url.relative())\n rqst_ctx = self.session.aiohttp_session.request(\n self.method,\n str(full_url),\n data=self._pack_content(),\n timeout=_default_request_timeout,\n headers=self.headers)\n return FetchContextManager(self.session, rqst_ctx, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef connect_websocket(self, **kwargs) -> 'WebSocketContextManager':\n '''\n Creates a WebSocket connection.\n\n .. warning::\n\n This method only works with\n :class:`~ai.backend.client.session.AsyncSession`.\n '''\n assert isinstance(self.session, AsyncSession), \\\n 'Cannot use websockets with sessions in the synchronous mode'\n assert self.method == 'GET', 'Invalid websocket method'\n self.date = datetime.now(tzutc())\n self.headers['Date'] = self.date.isoformat()\n # websocket is always a \"binary\" stream.\n self.content_type = 'application/octet-stream'\n full_url = self._build_url()\n self._sign(full_url.relative())\n ws_ctx = self.session.aiohttp_session.ws_connect(\n str(full_url),\n autoping=True, heartbeat=30.0,\n headers=self.headers)\n return WebSocketContextManager(self.session, ws_ctx, **kwargs)", "response": "Creates a WebSocket connection."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_perf_task(task, auth, url):\n add_perf_task_url = \"/imcrs/perf/task\"\n f_url = url + add_perf_task_url\n payload = json.dumps(task)\n response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' add_perf_task: An Error has occured'", "response": "function adds a performance task to the device"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfunction takes a str object containing the name of an existing performance task and issues a RESTFUL call to the IMC REST service to get the performance task information.", "response": "def get_perf_task(task_name, auth, url):\n \"\"\"\n function takes the a str object containing the name of an existing performance tasks and\n issues a RESTFUL call to the IMC REST service. It will return a list\n\n :param task_name: str containing the name of the performance task\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: 204\n\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.perf import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> selected_task = get_perf_task('Cisco_Temperature', auth.creds, auth.url)\n\n >>> assert type(selected_task) is dict\n\n >>> assert 'taskName' in selected_task\n \"\"\"\n get_perf_task_url = \"/imcrs/perf/task?name=\" + task_name + \"&orderBy=taskId&desc=false\"\n f_url = url + get_perf_task_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n perf_task_info = (json.loads(response.text))\n if 'task' in perf_task_info:\n perf_task_info = (json.loads(response.text))['task']\n else:\n perf_task_info = \"Task Doesn't Exist\"\n return perf_task_info\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_perf_task: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfunctions to delete a Perf Task from the IMC RS.", "response": "def delete_perf_task(task_name, auth, url):\n \"\"\"\n Function takes a str of the target task_name to be deleted and retrieves task_id using\n the get_perf_task function. Once the task_id has been successfully retrieved it is\n populated into the task_id variable and an DELETE call is made against the HPE IMC REST\n interface to delete the target task.\n :param task_name: str of task name\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: int of 204 if successful, str of \"Perf Task doesn't exist\" i\n\n :rtype: int\n\n \"\"\"\n task_id = get_perf_task(task_name, auth, url)\n if isinstance(task_id, str):\n print(\"Perf task doesn't exist\")\n return 403\n task_id = task_id['taskId']\n get_perf_task_url = \"/imcrs/perf/task/delete/\" + str(task_id)\n f_url = url + get_perf_task_url\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n print(\"Perf Task successfully delete\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' delete_perf_task: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nprocesses a response from the API.", "response": "def process_json_response(self, response):\n \"\"\"For a json response, check if there was any error and throw exception.\n Otherwise, create a housecanary.response.Response.\"\"\"\n\n response_json = response.json()\n\n # handle errors\n code_key = \"code\"\n if code_key in response_json and response_json[code_key] != constants.HTTP_CODE_OK:\n code = response_json[code_key]\n\n message = response_json\n if \"message\" in response_json:\n message = response_json[\"message\"]\n elif \"code_description\" in response_json:\n message = response_json[\"code_description\"]\n\n if code == constants.HTTP_FORBIDDEN:\n raise housecanary.exceptions.UnauthorizedException(code, message)\n if code == constants.HTTP_TOO_MANY_REQUESTS:\n raise housecanary.exceptions.RateLimitException(code, message, response)\n else:\n raise housecanary.exceptions.RequestException(code, message)\n\n request_url = response.request.url\n\n endpoint_name = self._parse_endpoint_name_from_url(request_url)\n\n return Response.create(endpoint_name, response_json, response)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start_watcher_thread(self):\n # Create watcher thread\n watcher_thread = threading.Thread(target=self.run_watcher)\n\n # If the reload mode is `spawn_wait`\n if self._reload_mode == self.RELOAD_MODE_V_SPAWN_WAIT:\n # Use non-daemon thread\n daemon = False\n\n # If the reload mode is not `spawn_wait`\n else:\n # Use daemon thread\n daemon = True\n\n # Set whether the thread is daemon\n watcher_thread.setDaemon(daemon)\n\n # Start watcher thread\n watcher_thread.start()\n\n # Return watcher thread\n return watcher_thread", "response": "Start the watcher thread."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _find_watch_paths(self):\n # Add directory paths in `sys.path` to watch paths\n watch_path_s = set(os.path.abspath(x) for x in sys.path)\n\n # For each extra path\n for extra_path in self._extra_paths or ():\n # Get the extra path's directory path\n extra_dir_path = os.path.dirname(os.path.abspath(extra_path))\n\n # Add to watch paths\n watch_path_s.add(extra_dir_path)\n\n # For each module in `sys.modules`\n for module in list(sys.modules.values()):\n # Get module file path\n module_path = getattr(module, '__file__', None)\n\n # If have module file path\n if module_path is not None:\n # Get module directory path\n module_dir_path = os.path.dirname(os.path.abspath(module_path))\n\n # Add to watch paths\n watch_path_s.add(module_dir_path)\n\n # Find short paths of these watch paths.\n # E.g. if both `/home` and `/home/aoik` exist, only keep `/home`.\n watch_path_s = self._find_short_paths(watch_path_s)\n\n # Return the watch paths\n return watch_path_s", "response": "Find paths to watch."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _find_short_paths(self, paths):\n # Split each path to parts.\n # E.g. '/home/aoik' to ['', 'home', 'aoik']\n path_parts_s = [path.split(os.path.sep) for path in paths]\n\n # Root node\n root_node = {}\n\n # Sort these path parts by length, with the longest being the first.\n #\n # Longer paths appear first so that their extra parts are discarded\n # when a shorter path is found at 5TQ8L.\n #\n # Then for each path's parts.\n for parts in sorted(path_parts_s, key=len, reverse=True):\n # Start from the root node\n node = root_node\n\n # For each part of the path\n for part in parts:\n # Create node of the path\n node = node.setdefault(part, {})\n\n # 5TQ8L\n # Clear the last path part's node's child nodes.\n #\n # This aims to keep only the shortest path that needs be watched.\n #\n node.clear()\n\n # Short paths\n short_path_s = set()\n\n # Collect leaf paths\n self._collect_leaf_paths(\n node=root_node,\n path_parts=(),\n leaf_paths=short_path_s,\n )\n\n # Return short paths\n return short_path_s", "response": "Find short paths of given paths."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncollects paths of leaf nodes. :param node: Starting node. Type is dict. Key is child node's path part. Value is child node. :param path_parts: The starting node's path parts. Type is tuple. :param leaf_paths: Leaf path list. :return: None.", "response": "def _collect_leaf_paths(self, node, path_parts, leaf_paths):\n \"\"\"\n Collect paths of leaf nodes.\n\n :param node:\n Starting node. Type is dict.\n\n Key is child node's path part. Value is child node.\n\n :param path_parts:\n The starting node's path parts. Type is tuple.\n\n :param leaf_paths:\n Leaf path list.\n\n :return:\n None.\n \"\"\"\n # If the node is leaf node\n if not node:\n # Get node path\n node_path = '/'.join(path_parts)\n\n # Add to list\n leaf_paths.add(node_path)\n\n # If the node is not leaf node\n else:\n # For each child node\n for child_path_part, child_node in node.items():\n # Get the child node's path parts\n child_path_part_s = path_parts + (child_path_part,)\n\n # Visit the child node\n self._collect_leaf_paths(\n node=child_node,\n path_parts=child_path_part_s,\n leaf_paths=leaf_paths,\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dispatch(self, event):\n # Get file path\n file_path = event.src_path\n\n # If the file path is in extra paths\n if file_path in self._extra_paths:\n # Call `reload`\n self.reload()\n\n # If the file path ends with `.pyc` or `.pyo`\n if file_path.endswith(('.pyc', '.pyo')):\n # Get `.py` file path\n file_path = file_path[:-1]\n\n # If the file path ends with `.py`\n if file_path.endswith('.py'):\n # Get the file's directory path\n file_dir = os.path.dirname(file_path)\n\n # If the file's directory path starts with any of the watch paths\n if file_dir.startswith(tuple(self._watch_paths)):\n # Call `reload`\n self.reload()", "response": "Dispatches the event object."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef reload(self):\n # Get reload mode\n reload_mode = self._reload_mode\n\n # If reload mode is `exec`\n if self._reload_mode == self.RELOAD_MODE_V_EXEC:\n # Call `reload_using_exec`\n self.reload_using_exec()\n\n # If reload mode is `spawn_exit`\n elif self._reload_mode == self.RELOAD_MODE_V_SPAWN_EXIT:\n # Call `reload_using_spawn_exit`\n self.reload_using_spawn_exit()\n\n # If reload mode is `spawn_wait`\n elif self._reload_mode == self.RELOAD_MODE_V_SPAWN_WAIT:\n # Call `reload_using_spawn_wait`\n self.reload_using_spawn_wait()\n\n # If reload mode is none of above\n else:\n # Get error message\n error_msg = 'Invalid reload mode: {}.'.format(repr(reload_mode))\n\n # Raise error\n raise ValueError(error_msg)", "response": "Reload the program.\n\n :return:\n None."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreloading the program process using the executable.", "response": "def reload_using_exec(self):\n \"\"\"\n Reload the program process.\n\n :return:\n None.\n \"\"\"\n # Create command parts\n cmd_parts = [sys.executable] + sys.argv\n\n # Get env dict copy\n env_copy = os.environ.copy()\n\n # Reload the program process\n os.execvpe(\n # Program file path\n sys.executable,\n # Command parts\n cmd_parts,\n # Env dict\n env_copy,\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nspawns a subprocess and exit the current process.", "response": "def reload_using_spawn_exit(self):\n \"\"\"\n Spawn a subprocess and exit the current process.\n\n :return:\n None.\n \"\"\"\n # Create command parts\n cmd_parts = [sys.executable] + sys.argv\n\n # Get env dict copy\n env_copy = os.environ.copy()\n\n # Spawn subprocess\n subprocess.Popen(cmd_parts, env=env_copy, close_fds=True)\n\n # If need force exit\n if self._force_exit:\n # Force exit\n os._exit(0) # pylint: disable=protected-access\n\n # If not need force exit\n else:\n # Send interrupt to main thread\n interrupt_main()\n\n # Set the flag\n self._watcher_to_stop = True\n\n # Exit the watcher thread\n sys.exit(0)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nspawns a subprocess and wait until it finishes.", "response": "def reload_using_spawn_wait(self):\n \"\"\"\n Spawn a subprocess and wait until it finishes.\n\n :return:\n None.\n \"\"\"\n # Create command parts\n cmd_parts = [sys.executable] + sys.argv\n\n # Get env dict copy\n env_copy = os.environ.copy()\n\n # Send interrupt to main thread\n interrupt_main()\n\n # Spawn subprocess and wait until it finishes\n subprocess.call(cmd_parts, env=env_copy, close_fds=True)\n\n # Exit the watcher thread\n sys.exit(0)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef agent(agent_id):\n '''\n Show the information about the given agent.\n '''\n fields = [\n ('ID', 'id'),\n ('Status', 'status'),\n ('Region', 'region'),\n ('First Contact', 'first_contact'),\n ('CPU Usage (%)', 'cpu_cur_pct'),\n ('Used Memory (MiB)', 'mem_cur_bytes'),\n ('Total slots', 'available_slots'),\n ('Occupied slots', 'occupied_slots'),\n ]\n if is_legacy_server():\n del fields[9]\n del fields[6]\n q = 'query($agent_id:String!) {' \\\n ' agent(agent_id:$agent_id) { $fields }' \\\n '}'\n q = q.replace('$fields', ' '.join(item[1] for item in fields))\n v = {'agent_id': agent_id}\n with Session() as session:\n try:\n resp = session.Admin.query(q, v)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n info = resp['agent']\n rows = []\n for name, key in fields:\n if key == 'mem_cur_bytes' and info[key] is not None:\n info[key] = round(info[key] / 2 ** 20, 1)\n if key in info:\n rows.append((name, info[key]))\n print(tabulate(rows, headers=('Field', 'Value')))", "response": "Show the information about the given agent."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef agents(status, all):\n '''\n List and manage agents.\n (admin privilege required)\n '''\n fields = [\n ('ID', 'id'),\n ('Status', 'status'),\n ('Region', 'region'),\n ('First Contact', 'first_contact'),\n ('CPU Usage (%)', 'cpu_cur_pct'),\n ('Used Memory (MiB)', 'mem_cur_bytes'),\n ('Total slots', 'available_slots'),\n ('Occupied slots', 'occupied_slots'),\n ]\n if is_legacy_server():\n del fields[9]\n del fields[6]\n\n def execute_paginated_query(limit, offset):\n try:\n resp_agents = session.Agent.list_with_limit(\n limit, offset, status, fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n return resp_agents\n\n def round_mem(results):\n for item in results:\n if 'mem_cur_bytes' in item and item['mem_cur_bytes'] is not None:\n item['mem_cur_bytes'] = round(item['mem_cur_bytes'] / 2 ** 20, 1)\n return results\n\n def _generate_paginated_results(interval):\n offset = 0\n is_first = True\n total_count = -1\n while True:\n limit = (interval if is_first else\n min(interval, total_count - offset))\n try:\n result = execute_paginated_query(limit, offset)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n offset += interval\n total_count = result['total_count']\n items = result['items']\n items = round_mem(items)\n table = tabulate((item.values() for item in items),\n headers=(item[0] for item in fields))\n if is_first:\n is_first = False\n else:\n table_rows = table.split('\\n')\n table = '\\n'.join(table_rows[2:])\n yield table + '\\n'\n\n if not offset < total_count:\n break\n\n with Session() as session:\n paginating_interval = 10\n if all:\n click.echo_via_pager(_generate_paginated_results(paginating_interval))\n else:\n result = execute_paginated_query(paginating_interval, offset=0)\n total_count = result['total_count']\n if total_count == 0:\n print('There are no matching agents.')\n return\n items = result['items']\n items = round_mem(items)\n fields = [field for field in fields if field[1] in items[0]]\n print(tabulate((item.values() for item in items),\n headers=(item[0] for item in fields)))\n if total_count > paginating_interval:\n print(\"More agents can be displayed by using --all option.\")", "response": "Return a list of agents with the given status and all if all is True."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshow details about a keypair resource policy.", "response": "def resource_policy(name):\n \"\"\"\n Show details about a keypair resource policy. When `name` option is omitted, the\n resource policy for the current access_key will be returned.\n \"\"\"\n fields = [\n ('Name', 'name'),\n ('Created At', 'created_at'),\n ('Default for Unspecified', 'default_for_unspecified'),\n ('Total Resource Slot', 'total_resource_slots'),\n ('Max Concurrent Sessions', 'max_concurrent_sessions'),\n ('Max Containers per Session', 'max_containers_per_session'),\n ('Max vFolder Count', 'max_vfolder_count'),\n ('Max vFolder Size', 'max_vfolder_size'),\n ('Idle Timeeout', 'idle_timeout'),\n ('Allowed vFolder Hosts', 'allowed_vfolder_hosts'),\n ]\n with Session() as session:\n try:\n rp = session.ResourcePolicy(session.config.access_key)\n info = rp.info(name, fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n rows = []\n if info is None:\n print('No such resource policy.')\n sys.exit(1)\n for name, key in fields:\n rows.append((name, info[key]))\n print(tabulate(rows, headers=('Field', 'Value')))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef resource_policies(ctx):\n '''\n List and manage resource policies.\n (admin privilege required)\n '''\n if ctx.invoked_subcommand is not None:\n return\n fields = [\n ('Name', 'name'),\n ('Created At', 'created_at'),\n ('Default for Unspecified', 'default_for_unspecified'),\n ('Total Resource Slot', 'total_resource_slots'),\n ('Max Concurrent Sessions', 'max_concurrent_sessions'),\n ('Max Containers per Session', 'max_containers_per_session'),\n ('Max vFolder Count', 'max_vfolder_count'),\n ('Max vFolder Size', 'max_vfolder_size'),\n ('Idle Timeeout', 'idle_timeout'),\n ('Allowed vFolder Hosts', 'allowed_vfolder_hosts'),\n ]\n with Session() as session:\n try:\n items = session.ResourcePolicy.list(fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if len(items) == 0:\n print('There are no keypair resource policies.')\n return\n print(tabulate((item.values() for item in items),\n headers=(item[0] for item in fields)))", "response": "List and manage resource policies."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd a new keypair resource policy.", "response": "def add(name, default_for_unspecified, total_resource_slots, max_concurrent_sessions,\n max_containers_per_session, max_vfolder_count, max_vfolder_size,\n idle_timeout, allowed_vfolder_hosts):\n '''\n Add a new keypair resource policy.\n\n NAME: NAME of a new keypair resource policy.\n '''\n with Session() as session:\n try:\n data = session.ResourcePolicy.create(\n name,\n default_for_unspecified=default_for_unspecified,\n total_resource_slots=total_resource_slots,\n max_concurrent_sessions=max_concurrent_sessions,\n max_containers_per_session=max_containers_per_session,\n max_vfolder_count=max_vfolder_count,\n max_vfolder_size=max_vfolder_size,\n idle_timeout=idle_timeout,\n allowed_vfolder_hosts=allowed_vfolder_hosts,\n )\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if not data['ok']:\n print_fail('KeyPair Resource Policy creation has failed: {0}'\n .format(data['msg']))\n sys.exit(1)\n item = data['resource_policy']\n print('Keypair resource policy ' + item['name'] + ' is created.')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a keypair resource policy.", "response": "def delete(name):\n \"\"\"\n Delete a keypair resource policy.\n\n NAME: NAME of a keypair resource policy to delete.\n \"\"\"\n with Session() as session:\n if input('Are you sure? (y/n): ').lower().strip()[:1] != 'y':\n print('Canceled.')\n sys.exit(1)\n try:\n data = session.ResourcePolicy.delete(name)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if not data['ok']:\n print_fail('KeyPair Resource Policy deletion has failed: {0}'\n .format(data['msg']))\n sys.exit(1)\n print('Resource policy ' + name + ' is deleted.')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrun a local proxy to a service provided by Backend.AI compute sessions. The type of proxy depends on the app definition: plain TCP or HTTP. \\b SESSID: The compute session ID. APP: The name of service provided by the given session.", "response": "def app(session_id, app, bind, port):\n \"\"\"\n Run a local proxy to a service provided by Backend.AI compute sessions.\n\n The type of proxy depends on the app definition: plain TCP or HTTP.\n\n \\b\n SESSID: The compute session ID.\n APP: The name of service provided by the given session.\n \"\"\"\n api_session = None\n runner = None\n\n async def app_setup():\n nonlocal api_session, runner\n loop = current_loop()\n api_session = AsyncSession()\n # TODO: generalize protocol using service ports metadata\n protocol = 'http'\n runner = ProxyRunner(api_session, session_id, app,\n protocol, bind, port,\n loop=loop)\n await runner.ready()\n print_info(\n \"A local proxy to the application \\\"{0}\\\" \".format(app) +\n \"provided by the session \\\"{0}\\\" \".format(session_id) +\n \"is available at: {0}://{1}:{2}\"\n .format(protocol, bind, port)\n )\n\n async def app_shutdown():\n nonlocal api_session, runner\n print_info(\"Shutting down....\")\n await runner.close()\n await api_session.close()\n print_info(\"The local proxy to \\\"{}\\\" has terminated.\"\n .format(app))\n\n asyncio_run_forever(app_setup(), app_shutdown(),\n stop_signals={signal.SIGINT, signal.SIGTERM})"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nretrieve a configuration value from the environment variables.", "response": "def get_env(key: str,\n default: Any = None,\n clean: Callable[[str], Any] = lambda v: v):\n '''\n Retrieves a configuration value from the environment variables.\n The given *key* is uppercased and prefixed by ``\"BACKEND_\"`` and then\n ``\"SORNA_\"`` if the former does not exist.\n\n :param key: The key name.\n :param default: The default value returned when there is no corresponding\n environment variable.\n :param clean: A single-argument function that is applied to the result of lookup\n (in both successes and the default value for failures).\n The default is returning the value as-is.\n\n :returns: The value processed by the *clean* function.\n '''\n key = key.upper()\n v = os.environ.get('BACKEND_' + key)\n if v is None:\n v = os.environ.get('SORNA_' + key)\n if v is None:\n if default is None:\n raise KeyError(key)\n v = default\n return clean(v)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nshowing the output logs of a running container.", "response": "def logs(sess_id_or_alias):\n '''\n Shows the output logs of a running container.\n\n \\b\n SESSID: Session ID or its alias given when creating the session.\n '''\n with Session() as session:\n try:\n print_wait('Retrieving container logs...')\n kernel = session.Kernel(sess_id_or_alias)\n result = kernel.get_logs().get('result')\n logs = result.get('logs') if 'logs' in result else ''\n print(logs)\n print_done('End of logs.')\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrap the key from the function input.", "response": "def _wrap_key(function, args, kws):\n\t'''\n\tget the key from the function input.\n\t'''\n\treturn hashlib.md5(pickle.dumps((_from_file(function) + function.__name__, args, kws))).hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(key, adapter = MemoryAdapter):\n\t'''\n\tget the cache value\n\t'''\n\ttry:\n\t\treturn pickle.loads(adapter().get(key))\n\texcept CacheExpiredException:\n\t\treturn None", "response": "get the cache value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set(key, value, timeout = -1, adapter = MemoryAdapter):\n\t'''\n\tset cache by code, must set timeout length\n\t'''\n\tif adapter(timeout = timeout).set(key, pickle.dumps(value)):\n\t\treturn value\n\telse:\n\t\treturn None", "response": "set a value in the cache by code"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wrapcache(timeout = -1, adapter = MemoryAdapter):\n\t'''\n\tthe Decorator to cache Function.\n\t'''\n\tdef _wrapcache(function):\n\t\t@wraps(function)\n\t\tdef __wrapcache(*args, **kws):\n\t\t\thash_key = _wrap_key(function, args, kws)\n\t\t\ttry:\n\t\t\t\tadapter_instance = adapter()\n\t\t\t\treturn pickle.loads(adapter_instance.get(hash_key))\n\t\t\texcept CacheExpiredException:\n\t\t\t\t#timeout\n\t\t\t\tvalue = function(*args, **kws)\n\t\t\t\tset(hash_key, value, timeout, adapter)\n\t\t\t\treturn value\n\t\treturn __wrapcache\n\treturn _wrapcache", "response": "Decorator to cache a function."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate an Excel file containing data returned by the Analytics API and saves it to a file.", "response": "def export_analytics_data_to_excel(data, output_file_name, result_info_key, identifier_keys):\n \"\"\"Creates an Excel file containing data returned by the Analytics API\n\n Args:\n data: Analytics API data as a list of dicts\n output_file_name: File name for output Excel file (use .xlsx extension).\n\n \"\"\"\n workbook = create_excel_workbook(data, result_info_key, identifier_keys)\n workbook.save(output_file_name)\n print('Saved Excel file to {}'.format(output_file_name))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef export_analytics_data_to_csv(data, output_folder, result_info_key, identifier_keys):\n workbook = create_excel_workbook(data, result_info_key, identifier_keys)\n\n suffix = '.csv'\n\n if not os.path.exists(output_folder):\n os.makedirs(output_folder)\n\n for worksheet in workbook.worksheets:\n file_name = utilities.convert_title_to_snake_case(worksheet.title)\n\n file_path = os.path.join(output_folder, file_name + suffix)\n\n mode = 'w'\n if sys.version_info[0] < 3:\n mode = 'wb'\n with io.open(file_path, mode) as output_file:\n csv_writer = csv.writer(output_file)\n for row in worksheet.rows:\n csv_writer.writerow([cell.value for cell in row])\n\n print('Saved CSV files to {}'.format(output_folder))", "response": "Creates a CSV file containing data returned by the Analytics API and saves it into the specified output_folder."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate an Excel file made up of combining the Value Report or Rental Report Excel output for the provided addresses. Args: addresses: A list of (address, zipcode) tuples output_file_name: A file name for the Excel output endpoint: One of 'value_report' or 'rental_report' report_type: One of 'full' or 'summary' retry: optional boolean to retry if rate limit is reached api_key: optional API Key api_secret: optional API Secret files_path: Path to save individual files. If None, don't save files", "response": "def concat_excel_reports(addresses, output_file_name, endpoint, report_type,\n retry, api_key, api_secret, files_path):\n \"\"\"Creates an Excel file made up of combining the Value Report or Rental Report Excel\n output for the provided addresses.\n\n Args:\n addresses: A list of (address, zipcode) tuples\n output_file_name: A file name for the Excel output\n endpoint: One of 'value_report' or 'rental_report'\n report_type: One of 'full' or 'summary'\n retry: optional boolean to retry if rate limit is reached\n api_key: optional API Key\n api_secret: optional API Secret\n files_path: Path to save individual files. If None, don't save files\n \"\"\"\n # create the master workbook to output\n master_workbook = openpyxl.Workbook()\n\n if api_key is not None and api_secret is not None:\n client = ApiClient(api_key, api_secret)\n else:\n client = ApiClient()\n\n errors = []\n\n # for each address, call the API and load the xlsx content in a workbook.\n for index, addr in enumerate(addresses):\n print('Processing {}'.format(addr[0]))\n result = _get_excel_report(\n client, endpoint, addr[0], addr[1], report_type, retry)\n\n if not result['success']:\n print('Error retrieving report for {}'.format(addr[0]))\n print(result['content'])\n errors.append({'address': addr[0], 'message': result['content']})\n continue\n\n orig_wb = openpyxl.load_workbook(filename=io.BytesIO(result['content']))\n\n _save_individual_file(orig_wb, files_path, addr[0])\n\n # for each worksheet for this address\n for sheet_name in orig_wb.get_sheet_names():\n # if worksheet doesn't exist in master workbook, create it\n if sheet_name in master_workbook.get_sheet_names():\n master_ws = master_workbook.get_sheet_by_name(sheet_name)\n else:\n master_ws = master_workbook.create_sheet(sheet_name)\n\n # get all the rows in the address worksheet\n orig_rows = orig_wb.get_sheet_by_name(sheet_name).rows\n\n if sheet_name == 'Summary' or sheet_name == 'Chart Data':\n _process_non_standard_sheet(master_ws, orig_rows, addr, index)\n continue\n\n _process_standard_sheet(master_ws, orig_rows, addr, index)\n\n # remove the first sheet which will be empty\n master_workbook.remove(master_workbook.worksheets[0])\n\n # if any errors occurred, write them to an \"Errors\" worksheet\n if len(errors) > 0:\n errors_sheet = master_workbook.create_sheet('Errors')\n for error_idx, error in enumerate(errors):\n errors_sheet.cell(row=error_idx+1, column=1, value=error['address'])\n errors_sheet.cell(row=error_idx+1, column=2, value=error['message'])\n\n # save the master workbook to output_file_name\n adjust_column_width_workbook(master_workbook)\n output_file_path = os.path.join(files_path, output_file_name)\n master_workbook.save(output_file_path)\n print('Saved output to {}'.format(output_file_path))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_excel_workbook(data, result_info_key, identifier_keys):\n workbook = analytics_data_excel.get_excel_workbook(data, result_info_key, identifier_keys)\n adjust_column_width_workbook(workbook)\n return workbook", "response": "Creates the Excel Workbook"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadjusts column width in the specified worksheet.", "response": "def adjust_column_width(worksheet):\n \"\"\"Adjust column width in worksheet.\n\n Args:\n worksheet: worksheet to be adjusted\n \"\"\"\n dims = {}\n padding = 1\n for row in worksheet.rows:\n for cell in row:\n if not cell.value:\n continue\n dims[cell.column] = max(\n dims.get(cell.column, 0),\n len(str(cell.value))\n )\n for col, value in list(dims.items()):\n worksheet.column_dimensions[col].width = value + padding"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_ap_info(ipaddress, auth, url):\n get_ap_info_url = \"/imcrs/wlan/apInfo/queryApBasicInfoByCondition?ipAddress=\" + str(ipaddress)\n f_url = url + get_ap_info_url\n payload = None\n r = requests.get(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n # print(r.status_code)\n try:\n if r.status_code == 200:\n if len(r.text) > 0:\n return json.loads(r.text)['apBasicInfo']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_ap_info_all: An Error has occured\"", "response": "This function returns the details of the target access point at time of query."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of compute sessions.", "response": "def sessions(status, access_key, id_only, all):\n '''\n List and manage compute sessions.\n '''\n fields = [\n ('Session ID', 'sess_id'),\n ]\n with Session() as session:\n if is_admin(session):\n fields.append(('Owner', 'access_key'))\n if not id_only:\n fields.extend([\n ('Image', 'image'),\n ('Tag', 'tag'),\n ('Created At', 'created_at',),\n ('Terminated At', 'terminated_at'),\n ('Status', 'status'),\n ('Occupied Resource', 'occupied_slots'),\n ('Used Memory (MiB)', 'mem_cur_bytes'),\n ('Max Used Memory (MiB)', 'mem_max_bytes'),\n ('CPU Using (%)', 'cpu_using'),\n ])\n if is_legacy_server():\n del fields[2]\n\n def execute_paginated_query(limit, offset):\n q = '''\n query($limit:Int!, $offset:Int!, $ak:String, $status:String) {\n compute_session_list(\n limit:$limit, offset:$offset, access_key:$ak, status:$status) {\n items { $fields }\n total_count\n }\n }'''\n q = textwrap.dedent(q).strip()\n q = q.replace('$fields', ' '.join(item[1] for item in fields))\n v = {\n 'limit': limit,\n 'offset': offset,\n 'status': status if status != 'ALL' else None,\n 'ak': access_key,\n }\n try:\n resp = session.Admin.query(q, v)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n return resp['compute_session_list']\n\n def round_mem(items):\n for item in items:\n if 'mem_cur_bytes' in item:\n item['mem_cur_bytes'] = round(item['mem_cur_bytes'] / 2 ** 20, 1)\n if 'mem_max_bytes' in item:\n item['mem_max_bytes'] = round(item['mem_max_bytes'] / 2 ** 20, 1)\n return items\n\n def _generate_paginated_results(interval):\n offset = 0\n is_first = True\n total_count = -1\n while True:\n limit = (interval if is_first else\n min(interval, total_count - offset))\n try:\n result = execute_paginated_query(limit, offset)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n offset += interval\n total_count = result['total_count']\n items = result['items']\n items = round_mem(items)\n\n if id_only:\n yield '\\n'.join([item['sess_id'] for item in items]) + '\\n'\n else:\n table = tabulate([item.values() for item in items],\n headers=(item[0] for item in fields))\n if not is_first:\n table_rows = table.split('\\n')\n table = '\\n'.join(table_rows[2:])\n yield table + '\\n'\n\n if is_first:\n is_first = False\n if not offset < total_count:\n break\n\n with Session() as session:\n paginating_interval = 10\n if all:\n click.echo_via_pager(_generate_paginated_results(paginating_interval))\n else:\n result = execute_paginated_query(paginating_interval, offset=0)\n total_count = result['total_count']\n if total_count == 0:\n print('There are no compute sessions currently {0}.'\n .format(status.lower()))\n return\n items = result['items']\n items = round_mem(items)\n if id_only:\n for item in items:\n print(item['sess_id'])\n else:\n print(tabulate([item.values() for item in items],\n headers=(item[0] for item in fields)))\n if total_count > paginating_interval:\n print(\"More sessions can be displayed by using --all option.\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nshow detailed information for a running compute session.", "response": "def session(sess_id_or_alias):\n '''\n Show detailed information for a running compute session.\n\n SESSID: Session id or its alias.\n '''\n fields = [\n ('Session ID', 'sess_id'),\n ('Role', 'role'),\n ('Image', 'image'),\n ('Tag', 'tag'),\n ('Created At', 'created_at'),\n ('Terminated At', 'terminated_at'),\n ('Agent', 'agent'),\n ('Status', 'status'),\n ('Status Info', 'status_info'),\n ('Occupied Resources', 'occupied_slots'),\n ('CPU Used (ms)', 'cpu_used'),\n ('Used Memory (MiB)', 'mem_cur_bytes'),\n ('Max Used Memory (MiB)', 'mem_max_bytes'),\n ('Number of Queries', 'num_queries'),\n ('Network RX Bytes', 'net_rx_bytes'),\n ('Network TX Bytes', 'net_tx_bytes'),\n ('IO Read Bytes', 'io_read_bytes'),\n ('IO Write Bytes', 'io_write_bytes'),\n ('IO Max Scratch Size', 'io_max_scratch_size'),\n ('IO Current Scratch Size', 'io_cur_scratch_size'),\n ('CPU Using (%)', 'cpu_using'),\n ]\n if is_legacy_server():\n del fields[3]\n q = 'query($sess_id:String) {' \\\n ' compute_session(sess_id:$sess_id) { $fields }' \\\n '}'\n q = q.replace('$fields', ' '.join(item[1] for item in fields))\n v = {'sess_id': sess_id_or_alias}\n with Session() as session:\n try:\n resp = session.Admin.query(q, v)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if resp['compute_session']['sess_id'] is None:\n print('There is no such running compute session.')\n return\n print('Session detail:\\n---------------')\n for i, value in enumerate(resp['compute_session'].values()):\n if fields[i][1] in ['mem_cur_bytes', 'mem_max_bytes']:\n value = round(value / 2 ** 20, 1)\n print(fields[i][0] + ': ' + str(value))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a new instance of the correct type of Response based on the data.", "response": "def create(cls, endpoint_name, json_body, original_response):\n \"\"\"Factory for creating the correct type of Response based on the data.\n Args:\n endpoint_name (str) - The endpoint of the request, such as \"property/value\"\n json_body - The response body in json format.\n original_response (response object) - server response returned from an http request.\n \"\"\"\n\n if endpoint_name == \"property/value_report\":\n return ValueReportResponse(endpoint_name, json_body, original_response)\n\n if endpoint_name == \"property/rental_report\":\n return RentalReportResponse(endpoint_name, json_body, original_response)\n\n prefix = endpoint_name.split(\"/\")[0]\n\n if prefix == \"block\":\n return BlockResponse(endpoint_name, json_body, original_response)\n\n if prefix == \"zip\":\n return ZipCodeResponse(endpoint_name, json_body, original_response)\n\n if prefix == \"msa\":\n return MsaResponse(endpoint_name, json_body, original_response)\n\n return PropertyResponse(endpoint_name, json_body, original_response)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a list of business error messages strings for each of the requested objects that had a business error. Returns an empty list if there was no error.", "response": "def get_object_errors(self):\n \"\"\"Gets a list of business error message strings\n for each of the requested objects that had a business error.\n If there was no error, returns an empty list\n\n Returns:\n List of strings\n \"\"\"\n if self._object_errors is None:\n self._object_errors = [{str(o): o.get_errors()}\n for o in self.objects()\n if o.has_error()]\n\n return self._object_errors"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning true if any requested object had a business logic error code.", "response": "def has_object_error(self):\n \"\"\"Returns true if any requested object had a business logic error,\n otherwise returns false\n\n Returns:\n boolean\n \"\"\"\n if self._has_object_error is None:\n # scan the objects for any business error codes\n self._has_object_error = next(\n (True for o in self.objects()\n if o.has_error()),\n False)\n return self._has_object_error"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of rate limit details.", "response": "def rate_limits(self):\n \"\"\"Returns a list of rate limit details.\"\"\"\n if not self._rate_limits:\n self._rate_limits = utilities.get_rate_limits(self.response)\n\n return self._rate_limits"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef check_imc_creds(auth, url):\n test_url = '/imcrs'\n f_url = url + test_url\n try:\n response = requests.get(f_url, auth=auth, headers=HEADERS, verify=False)\n return bool(response.status_code == 200)\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" test_imc_creds: An Error has occured\"", "response": "Function takes input of auth class object auth object and URL and returns a BOOL of TRUE\n if the authentication was successful."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef print_to_file(object_name):\n with open('pyoutput.txt', 'w') as filehandler:\n output = None\n if isinstance(object_name, list):\n output = json.dumps(object, indent=4)\n if isinstance(object_name, dict):\n output = json.dumps(object, indent=4)\n if isinstance(object_name, str):\n output = object_name\n filehandler.write(output)", "response": "Function prints out to file handler and saves to current working directory as pyoutput. txt\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_auth(self):\n url = self.h_url + self.server + \":\" + self.port\n auth = requests.auth.HTTPDigestAuth(self.username, self.password)\n f_url = url + \"/imcrs\"\n try:\n response = requests.get(f_url, auth=auth, headers=HEADERS, verify=False)\n if response.status_code != 200: # checks for valid IMC credentials\n print(\"Error:\\n\" + \"Error: \\n You're credentials are invalid. Please try again\\n\\n\")\n set_imc_creds()\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + 'The IMC server address is invalid. Please try again'", "response": "This method requests an authentication object from the HPE IMC NMS and returns an HTTPDigest Auth Object containing the ID of the object."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef print_title(title, is_end=False):\n # If is end title\n if is_end:\n # Use `=`\n sep = '====='\n\n # If is not end title\n else:\n # Use `-`\n sep = '-----'\n\n # If is not end title\n if not is_end:\n # Print an empty line for visual comfort\n print_text()\n\n # Print the title, e.g. `----- {title} -----`\n print_text('# {sep} {title} {sep}'.format(title=title, sep=sep))", "response": "Print a title in a sequence of words."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_full_python_version():\n # Get version part, e.g. `3.5.1.final.0`\n version_part = '.'.join(str(x) for x in sys.version_info)\n\n # Get integer width, e.g. 32 or 64\n int_width = struct.calcsize('P') * 8\n\n # Get integer width part, e.g. `64bit` or `32bit`\n int_width_part = str(int_width) + 'bit'\n\n # Return full Python version\n return version_part + '.' + int_width_part", "response": "Get full Python version."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting given virtual environment s python program path.", "response": "def get_python_path(venv_path):\n \"\"\"\n Get given virtual environment's `python` program path.\n\n :param venv_path: Virtual environment directory path.\n\n :return: `python` program path.\n \"\"\"\n # Get `bin` directory path\n bin_path = get_bin_path(venv_path)\n\n # Get `python` program path\n program_path = os.path.join(bin_path, 'python')\n\n # If the platform is Windows\n if sys.platform.startswith('win'):\n # Add `.exe` suffix to the `python` program path\n program_path = program_path + '.exe'\n\n # Return the `python` program path\n return program_path"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_options(ctx):\n # Add option\n ctx.add_option(\n '--always',\n action='store_true',\n default=False,\n dest='always',\n help='whether always run tasks.',\n )\n\n # Add option\n ctx.add_option(\n '--check-import',\n action='store_true',\n default=False,\n dest='check_import',\n help='whether import module for dirty checking.',\n )\n\n # Add option\n ctx.add_option(\n '--venv',\n dest='venv',\n help=(\n 'virtual environment directory relative path relative to top'\n ' directory.'\n ),\n )\n\n # Add option\n ctx.add_option(\n '--venv-add-version',\n default='1',\n dest='venv_add_version',\n # Convert to int so that the value can be used as boolean\n type=int,\n metavar='0|1',\n help=(\n 'whether add full Python version to virtual environment directory'\n ' name. E.g. `.py3.5.1.final.0.64bit`. Default is add.'\n ),\n )\n\n # Add option\n ctx.add_option(\n '--req',\n default=None,\n dest='req_path',\n help='requirements file relative path relative to top directory.',\n )", "response": "Add command line options."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds given path to PYTHONPATH environment variable.", "response": "def add_pythonpath(path):\n \"\"\"\n Prepend given path to environment variable PYTHONPATH.\n\n :param path: Path to add to PYTHONPATH.\n\n :return: New PYTHONPATH value.\n \"\"\"\n # Get PYTHONPATH value. Default is empty string.\n pythonpath = os.environ.setdefault('PYTHONPATH', '')\n\n # If given path is not in PYTHONPATH\n if path not in pythonpath.split(os.pathsep):\n # Prepend given path to PYTHONPATH\n pythonpath = os.environ['PYTHONPATH'] = \\\n (path + os.pathsep + pythonpath) if pythonpath else path\n\n # Return new PYTHONPATH value\n return pythonpath"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mark_path(path):\n # If given path is not string,\n # or given path is absolute path.\n if not isinstance(path, str) or os.path.isabs(path):\n # Get error message\n msg = 'Error (2D9ZA): Given path is not relative path: {0}.'.format(\n path\n )\n\n # Raise error\n raise ValueError(msg)\n\n # If given path is string,\n # and given path is not absolute path.\n\n # Wrap given path\n return _ItemWrapper(type='path', item=path)", "response": "Wrap given path as relative path relative to top directory."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrap given item as input or output target that should be added to task.", "response": "def _mark_target(type, item):\n \"\"\"\n Wrap given item as input or output target that should be added to task.\n\n Wrapper object will be handled specially in \\\n :paramref:`create_cmd_task.parts`.\n\n :param type: Target type.\n\n Allowed values:\n - 'input'\n - 'output'\n\n :param item: Item to mark as input or output target.\n\n Allowed values:\n - Relative path relative to top directory.\n - Node object.\n - List of these.\n\n :return: Wrapper object.\n \"\"\"\n # If given type is not valid\n if type not in ('input', 'output'):\n # Get error message\n msg = 'Error (7D74X): Type is not valid: {0}'.format(type)\n\n # Raise error\n raise ValueError(msg)\n\n # If given type is valid.\n\n # Store given item\n orig_item = item\n\n # If given path is list\n if isinstance(item, list):\n # Use it as items list\n item_s = item\n\n # If given path is not list\n else:\n # Create items list containing given path\n item_s = [item]\n\n # For the items list's each item\n for item in item_s:\n # If the item is string,\n # and the item is absolute path.\n if isinstance(item, str) and os.path.isabs(item):\n # Get error message\n msg = (\n 'Error (5VWOZ): Given path is not relative path: {0}.'\n ).format(item)\n\n # Raise error\n raise ValueError(msg)\n\n # Wrap given item\n return _ItemWrapper(type=type, item=orig_item)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_node(ctx, path):\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # Get top directory's relative path relative to `wscript` directory\n top_dir_relpath = os.path.relpath(\n # Top directory's absolute path\n ctx.top_dir,\n # `wscript` directory's absolute path\n ctx.run_dir,\n )\n\n # Convert given relative path to be relative to `wscript` directory\n node_path = os.path.join(top_dir_relpath, path)\n\n # Create node using the relative path relative to `wscript` directory\n node = ctx.path.make_node(node_path)\n\n # Return the created node\n return node", "response": "Create a node for given relative path relative to the top directory."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _normalize_items(\n ctx,\n items,\n str_to_node=False,\n node_to_str=False,\n allow_task=False,\n):\n \"\"\"\n Normalize given items.\n\n Do several things:\n - Ignore None.\n - Flatten list.\n - Unwrap wrapped item in `_ItemWrapper`.\n\n :param ctx: BuildContext object.\n\n :param items: Items list to normalize.\n\n :param str_to_node: Convert string to node.\n\n :param node_to_str: Convert node to absolute path.\n\n :param allow_task: Whether allow task item.\n\n :return: Normalized tuples list.\n\n Tuple format is:\n ::\n\n (\n normalized_item, # Normalized item.\n wrapper_type, # Original `_ItemWrapper` type.\n )\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # Normalized tuples list\n norm_tuple_s = []\n\n # If given items list is empty\n if not items:\n # Return empty list\n return norm_tuple_s\n\n # If given items list is not empty.\n\n # For given items list's each item\n for item in items:\n # If the item is item wrapper\n if isinstance(item, _ItemWrapper):\n # Get wrapper type\n wrapper_type = item.type()\n\n # Get real item\n item = item.item()\n\n # If the item is not item wrapper\n else:\n # Set wrapper type be None\n wrapper_type = None\n\n # Use the item as real item\n item = item\n\n # If the real item is list\n if isinstance(item, list):\n # Use the real item as real items list\n real_item_s = item\n\n # If the real item is not list\n else:\n # Create real items list containing the real item\n real_item_s = [item]\n\n # For each real item\n for real_item in real_item_s:\n # If the real item is None\n if real_item is None:\n # Ignore None\n continue\n\n # If the real item is not None.\n\n # If the real item is string\n elif isinstance(real_item, str):\n # If need convert string to node\n if (wrapper_type is not None) or str_to_node:\n # If the path string is absolute path\n if os.path.isabs(real_item):\n # Get error message\n msg = (\n 'Error (7MWU9): Given path is not relative path:'\n ' {0}.'\n ).format(real_item)\n\n # Raise error\n raise ValueError(msg)\n\n # If the path string is not absolute path.\n\n # Create node as normalized item\n norm_item = create_node(ctx, real_item)\n\n # If need convert node to absolute path\n if node_to_str:\n # Convert the node to absolute path\n norm_item = norm_item.abspath()\n\n # If not need convert string to node\n else:\n # Use the string as normalized item\n norm_item = real_item\n\n # Create normalized tuple\n norm_tuple = (norm_item, wrapper_type)\n\n # If the real item is not string.\n\n # If the real item is node\n elif isinstance(real_item, Node):\n # If need convert node to absolute path\n if node_to_str:\n # Convert the node to absolute path\n real_item = real_item.abspath()\n\n # Create normalized tuple\n norm_tuple = (real_item, wrapper_type)\n\n # If the real item is not node.\n\n # If the real item is task\n elif isinstance(real_item, Task):\n # If allow task item\n if allow_task:\n # Create normalized tuple\n norm_tuple = (real_item, wrapper_type)\n\n # If not allow task item\n else:\n # Get error message\n msg = 'Error (6PVMG): Item type is not valid: {0}.'.format(\n real_item\n )\n\n # Raise error\n raise ValueError(msg)\n\n # If the real item is not task.\n\n # If the real item is not None, string, node, or task\n else:\n # Get error message\n msg = 'Error (63KUG): Item type is not valid: {0}.'.format(\n real_item\n )\n\n # Raise error\n raise ValueError(msg)\n\n # Add the normalized tuple to list\n norm_tuple_s.append(norm_tuple)\n\n # Return the normalized tuples list\n return norm_tuple_s", "response": "Normalizes given items list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the touch file at given path.", "response": "def update_touch_file(\n ctx,\n path,\n check_import=False,\n check_import_module=None,\n check_import_python=None,\n always=False,\n):\n \"\"\"\n Update touch file at given path.\n\n Do two things:\n - Create touch file if it not exists.\n - Update touch file if import checking fails.\n\n The returned touch file node is used as task's output target for dirty\n checking. Task will run if the touch file changes.\n\n :param ctx: BuildContext instance.\n\n :param path: Touch file relative path relative to top directory.\n\n :param check_import: Whether import module for dirty checking.\n\n :param check_import_module: Module name to import for dirty checking.\n\n :param check_import_python: Python program to use for dirty checking.\n\n :param always: Whether always run.\n\n :return: A two-item tuple.\n\n Tuple format is:\n ::\n\n (\n touch_file_node, # Touch file node.\n task_needs_run, # Whether task needs run.\n )\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # Print title\n print_title('Update touch file: {}'.format(path))\n\n # Create touch node\n touch_node = create_node(ctx, path)\n\n # Whether task needs run\n need_run = False\n\n # If the touch file not exists,\n # or `always` flag is on.\n if not touch_node.exists() or always:\n # Set `need_run` flag on\n need_run = True\n\n # If the touch file exists,\n # and `always` flag is off.\n else:\n # If need import module for dirty checking,\n # and module name to import is given.\n if check_import and check_import_module:\n # Get import statement.\n # Notice `from` import ensures the imported module is not imported\n # as `__main__` module. And `__name__` exists in any module.\n import_stmt = 'from {} import __name__'.format(check_import_module)\n\n # Print info\n print_text('Check import: {}'.format(import_stmt))\n\n # If Python program to check import is not given\n if check_import_python is None:\n # Get error message\n msg = (\n 'Error (3BKFW): Python program to check import is not'\n ' given.'\n )\n\n # Raise error\n raise ValueError(msg)\n\n # If Python program to check import is given.\n\n # Normalize given Python program path\n check_import_python, _ = _normalize_items(\n ctx=ctx,\n items=[check_import_python],\n # Convert node to absolute path\n node_to_str=True,\n )[0]\n\n # If the Python program path is not string\n if not isinstance(check_import_python, str):\n # Get error message\n msg = (\n 'Error (39FQE): Given Python program to check import is'\n ' not string or node: {0}.'\n ).format(check_import_python)\n\n # Raise error\n raise ValueError(msg)\n\n # If the Python program path is string.\n\n # If the Python program path is not absolute path\n if not os.path.isabs(check_import_python):\n # Convert the Python program path to absolute path\n check_import_python = \\\n create_node(ctx, check_import_python).abspath()\n\n # The Python program path is absolute path now.\n\n # Get command parts\n cmd_part_s = [\n # Python program absolute path\n check_import_python,\n\n # Run code\n '-c',\n\n # Code to run\n import_stmt\n ]\n\n # Print the command in multi-line format\n print_text(_format_multi_line_command(cmd_part_s))\n\n #\n try:\n # Run the command\n subprocess.check_output(cmd_part_s)\n\n # If not have error,\n # it means the module can be imported.\n # Set `need_run` flag off.\n need_run = False\n\n # If have error,\n # it means the module can not be imported.\n #\n # Notice the program may not exist. So catch general exception.\n except Exception: # pylint: disable=W0703\n # Set `need_run` flag on\n need_run = True\n\n # If task needs run\n if need_run:\n # If the touch file's parent directory not exists\n if not touch_node.parent.exists():\n # Create the touch file's parent directory\n touch_node.parent.mkdir()\n\n # Write current time to the touch file to force content change.\n # This will fail dirty-checking and cause task to run.\n touch_node.write('{0}\\n'.format(datetime.utcnow()))\n\n # Print info\n print_text('Updated.')\n\n # If task not needs run\n else:\n # Print info\n print_text('Skipped.')\n\n # Print end title\n print_title('Update touch file: {}'.format(path), is_end=True)\n\n # Return a two-item tuple\n return touch_node, need_run"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef chain_tasks(tasks):\n # If given tasks list is not empty\n if tasks:\n # Previous task\n previous_task = None\n\n # For given tasks list's each task\n for task in tasks:\n # If the task is not None.\n # Task can be None to allow code like ``task if _PY2 else None``.\n if task is not None:\n # If previous task is not None\n if previous_task is not None:\n # Set the task to run after the previous task\n task.set_run_after(previous_task)\n\n # Set the task as previous task for the next task\n previous_task = task\n\n # Return given tasks list.\n return tasks", "response": "Chain given tasks. Set each task to run after its previous task."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_cmd_task(\n ctx,\n parts,\n inputs=None,\n outputs=None,\n env=None,\n cwd=None,\n task_name=None,\n cache_key=None,\n always=False,\n add_to_group=True,\n):\n \"\"\"\n Create task that runs given command.\n\n :param ctx: BuildContext object.\n\n :param parts: Command parts list.\n\n Each part can be:\n - **None**: Will be ignored.\n - **String**: Will be used as-is.\n - **Node object**: Will be converted to absolute path.\n - **List of these**: Will be flattened.\n - **Item wrapper of these**: Will be unwrapped. Item marked as\n input or output target will be added to created task.\n\n :param inputs: Input items list to add to created task.\n\n Each item can be:\n - **None**: Will be ignored.\n - **Path**: Relative path relative to top directory. Will be\n converted to node.\n - **Node object**: Will be used as-is.\n - **Task object**: Will set created task to run after the task.\n - **List of these**: Will be flattened.\n - **Item wrapper of these**: Will be unwrapped.\n\n :param outputs: Outputs items list to add to created task.\n\n Each item can be:\n - **None**: Will be ignored.\n - **Path**: Relative path relative to top directory. Will be\n converted to node.\n - **Node object**: Will be used as-is.\n - **Task object**: Will set the task to run after created task.\n - **List of these**: Will be flattened.\n - **Item wrapper of these**: Will be unwrapped.\n\n :param env: Environment dict.\n\n Default is use given context object's environment dict.\n\n :param cwd: Working directory in which to run given command.\n\n Default is top directory.\n\n :param task_name: Task name for display purpose.\n\n Default is use the caller function's name.\n\n :param cache_key: Task cache key.\n\n :param always: Whether mark created task as always run.\n\n :param add_to_group: Whether add created task to given context.\n\n :return: Created task.\n\n :notes:\n It is recommended to refer to files inside project using relative\n paths. This is even required if the files are used as task's input or\n output targets.\n\n It is not ideal to make relative paths relative to current working\n directory, or relative to `wscript` file, because neither of the two\n choices guarantee a fixed start directory.\n\n It is not ideal to let `Waf` resolve relative paths neither, because\n the start directory used for resolving relative paths varies with\n different types of command context (i.e. OptionsContext,\n ConfigurationContext, BuildContext, and Context).\n\n It is best to make relative paths relative to project's top directory.\n To do so, first use `Waf`'s pre-defined path variable `top` to locate\n project's top directory (Notice path variable `top`'s value is relative\n to `wscript` file.). Then `aoikwafutil`'s functions like\n `create_cmd_task` will use path variable `top`'s value to resolve other\n relative paths.\n\n `create_cmd_task` handles relative paths using rules below:\n\n - Relative paths marked by `mark_path`, `mark_input`, or\n `mark_output` are relative to top directory.\n\n - Relative paths passed to argument `parts` are used as-is. They\n are relative to the working directory in which to run the\n command. However, they can be marked to be relative to top\n directory instead.\n\n - Relative paths passed to argument `inputs` or `outputs` are\n relative to top directory. They need not be marked.\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # If task name is not given\n if not task_name:\n # Get the caller function's name\n task_name = inspect.stack()[1][3]\n\n # Print title\n print_title('Create task: {}'.format(task_name))\n\n # If cache key is given\n if cache_key:\n # Find cached task\n cached_task = _TASKS_CACHE.get(cache_key, None)\n\n # If have found cached task\n if cached_task is not None:\n # Print info\n print_text('Use cached task `{}`.'.format(cache_key))\n\n # Print end title\n print_title('Create task: {}'.format(task_name), is_end=True)\n\n # Return the cached task\n return cached_task\n\n # If have not found cached task.\n # Continue to create task.\n\n # If cache key is not given.\n # Continue to create task.\n\n # Normalized command parts list\n norm_part_s = []\n\n # Input nodes list\n input_node_s = []\n\n # Output nodes list\n output_node_s = []\n\n # Get the first command part that is program path\n first_part = parts[0]\n\n # If the first command part is list.\n # Notice the first command part can be list if the value is from `ctx.env`,\n # e.g `ctx.env.PYTHON`.\n if isinstance(first_part, list):\n # Use the first item in the list as the first command part\n parts[0] = first_part[0]\n\n # For given command parts list's each command part\n for part, wrapper_type in _normalize_items(ctx=ctx, items=parts):\n # If the command part is string\n if isinstance(part, str):\n # Add the string to the normalized list\n norm_part_s.append(part)\n\n # If the command part is node\n elif isinstance(part, Node):\n # Add the node's absolute path to the normalized list\n norm_part_s.append(part.abspath())\n\n # If the wrapper type is `input`\n if wrapper_type == 'input':\n # Add the node to the input nodes list\n input_node_s.append(part)\n\n # If the wrapper type is `output`\n elif wrapper_type == 'output':\n # Add the node to the output nodes list\n output_node_s.append(part)\n\n # If the wrapper type is not `input` or `output`\n else:\n # Do nothing\n pass\n\n # If the command part is not string or node\n else:\n # Get error message\n msg = 'Error (2W9YD): Command part is not valid: {0}.'.format(part)\n\n # Raise error\n raise ValueError(msg)\n\n # If environment dict is not given\n if env is None:\n # Use given context object's environment dict\n env = ctx.env\n\n # If working directory path is not given\n if cwd is None:\n # Set working directory path be top directory absolute path\n cwd = ctx.top_dir\n\n # If the working directory path is not absolute path.\n if not os.path.isabs(cwd):\n # Convert the working directory path to absolute path\n cwd = create_node(ctx, cwd).abspath()\n\n # Create task that runs command\n task = CmdTask(\n # Context object\n ctx=ctx,\n\n # Command parts list\n parts=norm_part_s,\n\n # Environment dict\n env=env,\n\n # Working directory\n cwd=cwd,\n\n # Task name\n task_name=task_name,\n )\n\n # For each input or output item.\n # Notice the code structure for handling input and output items are the\n # same so use the same code with different variables to avoid duplicate\n # code.\n for wrapper_type, item_s, node_s, nodes_set_func in [\n ('input', inputs, input_node_s, task.set_inputs),\n ('output', outputs, output_node_s, task.set_outputs),\n ]:\n # For each normalized item\n for item, _ in _normalize_items(\n # Context\n ctx=ctx,\n\n # Items to normalize\n items=item_s,\n\n # Convert string to node\n str_to_node=True,\n\n # Allow task item\n allow_task=True,\n ):\n # If the item is node\n if isinstance(item, Node):\n # Add the node to the nodes list\n node_s.append(item)\n\n # If the item is task\n elif isinstance(item, Task):\n # If the wrapper type is `input`\n if wrapper_type == 'input':\n # Set the created task to run after the task\n task.set_run_after(item)\n\n # If the wrapper type is `output`\n elif wrapper_type == 'output':\n # Set the task to run after the created task\n item.set_run_after(task)\n\n # If the wrapper type is not `input` or `output`\n else:\n # Get error message\n msg = (\n 'Error (3ZLGJ): Wrapper type is not valid: {0}.'\n ).format(wrapper_type)\n\n # Raise error\n raise ValueError(msg)\n\n # If the item is not node or task\n else:\n # Get error message\n msg = 'Error (5H4GC): Item type is not valid: {0}.'.format(\n item\n )\n\n # Raise error\n raise ValueError(msg)\n\n # Add these nodes to the created task as input or output targets\n nodes_set_func(node_s)\n\n # If need mark the created task as always run\n if always:\n # Mark the created task as always run\n task.always_run = True # pylint: disable=W0201\n\n # If need add the created task to given context\n if add_to_group:\n # Add the created task to given context\n ctx.add_to_group(task)\n\n # If cache key is given\n if cache_key:\n # Add the created task to the cache\n _TASKS_CACHE[cache_key] = task\n\n # Print end title\n print_title('Create task: {}'.format(task_name), is_end=True)\n\n # Return the task\n return task", "response": "Create a task that runs given command."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprint given context s info.", "response": "def print_ctx(ctx):\n \"\"\"\n Print given context's info.\n\n :param ctx: Context object.\n\n :return: None.\n \"\"\"\n # Print title\n print_title('ctx attributes')\n\n # Print context object's attributes\n print_text(dir(ctx))\n\n # Print end title\n print_title('ctx attributes', is_end=True)\n\n # Print title\n print_title('ctx.options')\n\n # Print context options dict\n print_text(pformat(vars(ctx.options), indent=4, width=1))\n\n # Print end title\n print_title('ctx.options', is_end=True)\n\n # If the context object has `env` attribute.\n # Notice plain context object not has `env` attribute.\n if hasattr(ctx, 'env'):\n # Print title\n print_title('ctx.env')\n\n # Print context environment variables dict\n print_text(pformat(dict(ctx.env), indent=4, width=1))\n\n # Print end title\n print_title('ctx.env', is_end=True)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate task that sets up virtualenv.", "response": "def virtualenv_setup(\n ctx,\n python,\n inputs=None,\n outputs=None,\n touch=None,\n check_import=False,\n pip_setup_file=None,\n pip_setup_touch=None,\n cache_key=None,\n always=False,\n):\n \"\"\"\n Create task that sets up `virtualenv` package.\n\n :param ctx: BuildContext object.\n\n :param python: Python program path.\n\n :param inputs: Input items list to add to created task.\n\n See :paramref:`create_cmd_task.inputs` for allowed item types.\n\n :param outputs: Output items list to add to created task.\n\n See :paramref:`create_cmd_task.outputs` for allowed item types.\n\n :param touch: Touch file path for dirty checking.\n\n :param check_import: Whether import module for dirty checking.\n\n :param pip_setup_file: `get-pip.py` file path for `pip_setup` task.\n\n :param pip_setup_touch: Touch file path for `pip_setup` task.\n\n :param cache_key: Task cache key.\n\n :param always: Whether always run.\n\n :return: Created task.\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # If `get-pip.py` file path is not given\n if pip_setup_file is None:\n # Not create task that sets up `pip`\n pip_setup_task = None\n\n # If `get-pip.py` file path is given\n else:\n # Create task that sets up `pip`\n pip_setup_task = pip_setup(\n # Context\n ctx=ctx,\n\n # Python program path\n python=python,\n\n # `get-pip.py` file path\n setup_file=pip_setup_file,\n\n # Touch file path\n touch=pip_setup_touch,\n\n # Whether import module for dirty checking\n check_import=check_import,\n\n # Whether always run\n always=always,\n )\n\n # If touch file path is not given\n if touch is None:\n # Not update touch file\n touch_node = None\n else:\n # Update touch file\n touch_node, always = update_touch_file(\n # Context\n ctx=ctx,\n\n # Touch file path\n path=touch,\n\n # Whether import module for dirty checking\n check_import=check_import,\n\n # Module name to import for dirty checking\n check_import_module='virtualenv',\n\n # Python program path for dirty checking\n check_import_python=python,\n\n # Whether always run\n always=always,\n )\n\n # Create task that sets up `virtualenv` package\n task = create_cmd_task(\n # Context\n ctx=ctx,\n\n # Command parts\n parts=[\n # Python program path\n python,\n\n # Run module\n '-m',\n\n # Module name\n 'pip',\n\n # Install package\n 'install',\n\n # Package name\n 'virtualenv',\n ],\n\n # Input items list\n inputs=[\n # Run after the task that sets up `pip`\n pip_setup_task,\n\n # Given input items list\n inputs,\n ],\n\n # Output items list\n outputs=[\n # Use the touch node as output target for dirty checking\n touch_node,\n\n # Given output items list\n outputs,\n ],\n\n # Whether always run\n always=always,\n\n # Task cache key\n cache_key=cache_key or (python, 'virtualenv'),\n )\n\n # Return the created task\n return task"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a virtual environment.", "response": "def create_venv(\n ctx,\n python,\n venv_path,\n inputs=None,\n outputs=None,\n pip_setup_file=None,\n pip_setup_touch=None,\n virtualenv_setup_touch=None,\n task_name=None,\n cache_key=None,\n always=False,\n):\n \"\"\"\n Create task that sets up virtual environment.\n\n :param ctx: BuildContext object.\n\n :param python: Python program path.\n\n :param venv_path: Virtual environment directory relative path relative to\n top directory.\n\n :param inputs: Input items list to add to created task.\n\n See :paramref:`create_cmd_task.inputs` for allowed item types.\n\n :param outputs: Output items list to add to created task.\n\n See :paramref:`create_cmd_task.outputs` for allowed item types.\n\n :param pip_setup_file: `get-pip.py` file path for `pip_setup` task.\n\n :param pip_setup_touch: Touch file path for `pip_setup` task.\n\n :param virtualenv_setup_touch: Touch file path for `virtualenv_setup` task.\n\n :param task_name: Task name for display purpose.\n\n :param cache_key: Task cache key.\n\n :param always: Whether always run.\n\n :return: Created task.\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # Create task that sets up `virtualenv` package\n virtualenv_setup_task = virtualenv_setup(\n # Context\n ctx=ctx,\n\n # Python program path\n python=python,\n\n # Touch file path\n touch=virtualenv_setup_touch,\n\n # `get-pip.py` file path for `pip_setup` task.\n pip_setup_file=pip_setup_file,\n\n # Touch file path for `pip_setup` task.\n pip_setup_touch=pip_setup_touch,\n )\n\n # Get virtual environment directory path node\n venv_path_node, _ = _normalize_items(\n ctx=ctx,\n items=[venv_path],\n # Convert path string to node\n str_to_node=True\n )[0]\n\n # Create task that sets up virtual environment.\n task = create_cmd_task(\n # Context\n ctx=ctx,\n\n # Command parts\n parts=[\n # Python program path\n python,\n\n # Run module\n '-m',\n\n # Module name\n 'virtualenv',\n\n # Virtual environment directory absolute path\n venv_path_node.abspath(),\n ],\n\n # Input items list\n inputs=[\n # Run after the task that sets up `virtualenv` package\n virtualenv_setup_task,\n\n # Given input items list\n inputs,\n ],\n\n # Output items list\n outputs=[\n # Add the virtual environment's `python` program path as output\n # target for dirty checking\n get_python_path(venv_path),\n\n # Add the virtual environment's `pip` program path as output target\n # for dirty checking\n get_pip_path(venv_path),\n\n # Given output items list\n outputs,\n ],\n\n # Whether always run\n always=always,\n\n # Task name\n task_name=task_name,\n\n # Task cache key\n cache_key=cache_key or (python, venv_path),\n )\n\n # Return the created task\n return task"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a pip - ins - req task.", "response": "def pip_ins_req(\n ctx,\n python,\n req_path,\n venv_path=None,\n inputs=None,\n outputs=None,\n touch=None,\n check_import=False,\n check_import_module=None,\n pip_setup_file=None,\n pip_setup_touch=None,\n virtualenv_setup_touch=None,\n always=False,\n):\n \"\"\"\n Create task that uses given virtual environment's `pip` to sets up \\\n packages listed in given requirements file.\n\n :param ctx: BuildContext object.\n\n :param python: Python program path used to set up `pip` and `virtualenv`.\n\n :param req_path: Requirements file relative path relative to top directory.\n\n :param venv_path: Virtual environment directory relative path relative to\n top directory.\n\n If given, will create the virtual environment and set up packages\n listed in given requirements file in the virtual environment.\n\n If not given, will set up packages listed in given requirements file in\n given Python program's environment.\n\n :param inputs: Input items list to add to created task.\n\n See :paramref:`create_cmd_task.inputs` for allowed item types.\n\n :param outputs: Output items list to add to created task.\n\n See :paramref:`create_cmd_task.outputs` for allowed item types.\n\n :param touch: Touch file path for dirty checking.\n\n :param check_import: Whether import module for dirty checking.\n\n :param check_import_module: Module name to import for dirty checking.\n\n :param pip_setup_file: `get-pip.py` file path for `pip_setup` task.\n\n :param pip_setup_touch: Touch file path for `pip_setup` task.\n\n :param virtualenv_setup_touch: Touch file path for `virtualenv_setup` task.\n\n :param always: Whether always run.\n\n :return: Created task.\n \"\"\"\n # Ensure given context object is BuildContext object\n _ensure_build_context(ctx)\n\n # If virtual environment directory path is not given\n if venv_path is None:\n # Use given Python program path\n venv_python = python\n\n # If virtual environment directory path is given\n else:\n # Get Python program path in the virtual environment\n venv_python = get_python_path(venv_path)\n\n # Mark the path as input target\n venv_python = mark_input(venv_python)\n\n # If virtual environment directory path is not given,\n # it means not create virtual environment.\n if venv_path is None:\n # Create task that sets up `pip`\n pip_setup_task = pip_setup(\n # Context\n ctx=ctx,\n\n # Python program path\n python=python,\n\n # `get-pip.py` file path\n setup_file=pip_setup_file,\n\n # Touch file path\n touch=pip_setup_touch,\n\n # Whether import module for dirty checking\n always=always,\n )\n\n # Not create virtual environment\n venv_task = None\n\n # If virtual environment directory path is given\n else:\n # Not create task that sets up `pip` here because `create_venv`\n # function below will do\n pip_setup_task = None\n\n # Create task that sets up virtual environment\n venv_task = create_venv(\n # Context\n ctx=ctx,\n\n # Python program path\n python=python,\n\n # Virtual environment directory path\n venv_path=venv_path,\n\n # Output items list\n outputs=[\n # Add the virtual environment's `python` program path as output\n # target for dirty checking\n get_python_path(venv_path),\n\n # Add the virtual environment's `pip` program path as output\n # target for dirty checking\n get_pip_path(venv_path),\n ],\n\n # Whether always run\n always=always,\n\n # Task name\n task_name='Create venv `{}`'.format(venv_path),\n\n # `get-pip.py` file path for `pip_setup` task\n pip_setup_file=pip_setup_file,\n\n # Touch file path for `pip_setup` task\n pip_setup_touch=pip_setup_touch,\n\n # Touch file path for `virtualenv_setup` task\n virtualenv_setup_touch=virtualenv_setup_touch,\n )\n\n # If touch file path is not given\n if not touch:\n # Not update touch file\n touch_node = None\n\n # If touch file path is given\n else:\n # Update touch file\n touch_node, always = update_touch_file(\n # Context\n ctx=ctx,\n\n # Touch file path\n path=touch,\n\n # Whether import module for dirty checking\n check_import=check_import,\n\n # Module name to import for dirty checking\n check_import_module=check_import_module,\n\n # Python program path for dirty checking\n check_import_python=venv_python,\n\n # Whether always run\n always=always,\n )\n\n # Create task that sets up packages\n task = create_cmd_task(\n # Context\n ctx=ctx,\n\n # Command parts\n parts=[\n # Python program path\n venv_python,\n\n # Run module\n '-m',\n\n # Module name\n 'pip',\n\n # Install package\n 'install',\n\n # Read package names from requirements file\n '-r',\n\n # Requirements file path. Mark as input target.\n mark_input(req_path),\n ],\n\n # Input items list\n inputs=inputs,\n\n # Output items list\n outputs=[\n # Use the touch node as output target for dirty checking\n touch_node,\n\n # Given output items list\n outputs,\n ],\n\n # Whether always run\n always=always,\n )\n\n # Chain these tasks to run one after another\n chain_tasks([\n pip_setup_task,\n venv_task,\n task,\n ])\n\n # Return the created task\n return task"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef git_clean(ctx):\n # Get command parts\n cmd_part_s = [\n # Program path\n 'git',\n\n # Clean untracked files\n 'clean',\n\n # Remove all untracked files\n '-x',\n\n # Remove untracked directories too\n '-d',\n\n # Force to remove\n '-f',\n\n # Give two `-f` flags to remove sub-repositories too\n '-f',\n ]\n\n # Print title\n print_title('git_clean')\n\n # Print the command in multi-line format\n print_text(_format_multi_line_command(cmd_part_s))\n\n # Create subprocess to run the command in top directory\n proc = subprocess.Popen(cmd_part_s, cwd=ctx.top_dir)\n\n # Wait the subprocess to finish\n proc.wait()\n\n # Print end title\n print_title('git_clean', is_end=True)", "response": "Delete all untracked files and directories in the current git repository."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run(self):\n # Print task title\n print_title(self._task_name_title)\n\n # Get context object\n ctx = self._ctx\n\n # Get command parts\n cmd_part_s = self._parts\n\n # Print title\n print_title('Find program')\n\n # Get program path from command parts\n program_path = cmd_part_s[0]\n\n # If the program path ends with one of the file extensions below\n if program_path.endswith(('.exe', '.com', '.bat', '.cmd')):\n # Remove the file extension.\n # This is because `Waf`'s `find_binary` function adds each of the\n # file extensions when finding the program.\n program_path = program_path[:-4]\n\n # Print info\n print_text('Find program: {0}'.format(program_path))\n\n #\n try:\n # Find program\n found_program_path_s = ctx.find_program(program_path, quiet=True)\n\n # If program paths are not found\n except ConfigurationError:\n # Get error message\n msg = 'Error (2D7VS): Program is not found: {0}'.format(\n program_path\n )\n\n # Raise error\n raise ValueError(msg)\n\n # If program paths are found.\n\n # If program paths are found.\n # Use the first program path found.\n # If program paths are not found. (Should not happen.)\n # Use given program path.\n found_program_path = found_program_path_s[0] \\\n if found_program_path_s else program_path\n\n # Use the program path found as the first command part\n cmd_part_s[0] = found_program_path\n\n # Print info\n print_text('Use program: {0}'.format(found_program_path))\n\n # Print end title\n print_title('Find program', is_end=True)\n\n # Print title\n print_title('PATH')\n\n # Print environment variable PATH's value, one part per line\n print_text('\\n'.join(os.environ.get('PATH', '').split(os.pathsep)))\n\n # Print end title\n print_title('PATH', is_end=True)\n\n # Print title\n print_title('PYTHONPATH')\n\n # Print environment variable PYTHONPATH's value, one part per line\n print_text(\n '\\n'.join(os.environ.get('PYTHONPATH', '').split(os.pathsep))\n )\n\n # Print end title\n print_title('PYTHONPATH', is_end=True)\n\n # Print title\n print_title('DIR')\n\n # Print working directory\n print_text(self._cwd)\n\n # Print end title\n print_title('DIR', is_end=True)\n\n # Print title\n print_title('CMD')\n\n # Print the command in multi-line format\n print_text(_format_multi_line_command(cmd_part_s))\n\n # Print end title\n print_title('CMD', is_end=True)\n\n # Print title\n print_title('RUN')\n\n # Run the command in the working directory\n exit_code = self.exec_command(cmd_part_s, cwd=self._cwd)\n\n # Print the command's exit code\n print_text('Exit code: {0}'.format(exit_code))\n\n # Print end title\n print_title('RUN', is_end=True)\n\n # Print task end title\n print_title(self._task_name_title, True)\n\n # Return the exit code\n return exit_code", "response": "Run command.\n\n :return: Command exit code."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ac_info_all(auth, url):\n get_ac_info_all_url = \"/imcrs/wlan/acInfo/queryAcBasicInfo\"\n f_url = url + get_ac_info_all_url\n payload = None\n r = requests.get(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n # print(r.status_code)\n try:\n if r.status_code == 200:\n if len(r.text) > 0:\n return json.loads(r.text)['acBasicInfo']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_ac_info_all: An Error has occured\"", "response": "This function returns a list of dictionaries where each element of the list represents a single wireless controller which has been discovered in the HPE IMC WSM module."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef addvlan(self, vlanid, vlan_name):\n create_dev_vlan( vlanid, vlan_name, self.auth, self.url, devid = self.devid)", "response": "This function creates a new VLAN on the device."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfunctioning operates on the IMCDev object. Takes input of vlanid (1-4094), auth and url to execute the delete_dev_vlans method on the IMCDev object. Device must be supported in the HPE IMC Platform VLAN Manager module. :param vlanid: str of VLANId ( valid 1-4094 ) :return:", "response": "def delvlan(self, vlanid):\n \"\"\"\n Function operates on the IMCDev object. Takes input of vlanid (1-4094),\n auth and url to execute the delete_dev_vlans method on the IMCDev object. Device must be\n supported in the HPE IMC Platform VLAN Manager module.\n :param vlanid: str of VLANId ( valid 1-4094 )\n :return:\n \"\"\"\n delete_dev_vlans( vlanid, self.auth, self.url, devid = self.devid)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfunction operates on the IMCDev object and updates the ipmacarp attribute :return:", "response": "def getipmacarp(self):\n \"\"\"\n Function operates on the IMCDev object and updates the ipmacarp attribute\n :return:\n \"\"\"\n self.ipmacarp = get_ip_mac_arp_list(self.auth, self.url, devid = self.devid)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning operates on the IMCInterface object and configures the interface into an administratively down state and refreshes contents of self.adminstatus :return:", "response": "def down(self):\n \"\"\"\n Function operates on the IMCInterface object and configures the interface into an\n administratively down state and refreshes contents of self.adminstatus\n :return:\n \"\"\"\n set_interface_down(self.ifIndex, self.auth, self.url, devip=self.ip)\n self.adminstatus = get_interface_details(self.ifIndex, self.auth, self.url, devip=self.ip)[\n 'adminStatusDesc']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction operates on the IMCInterface object and configures the interface into an an administratively up state and refreshes contents of self. adminstatus", "response": "def up(self):\n \"\"\"\n Function operates on the IMCInterface object and configures the interface into an\n administratively up state and refreshes contents of self.adminstatus\n :return:\n \"\"\"\n set_interface_up(self.ifIndex, self.auth, self.url, devip=self.ip)\n self.adminstatus = get_interface_details(self.ifIndex, self.auth, self.url, devip=self.ip)[\n 'adminStatusDesc']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nobjecting method takes in input of hostipaddress, name and description and adds them to the parent ip scope. :param hostipaddress: str of ipv4 address of the target host ip record :param name: str of the name of the owner of the target host ip record :param description: str of a description of the target host ip record :return:", "response": "def allocate_ip(self, hostipaddress, name, description):\n \"\"\"\n Object method takes in input of hostipaddress, name and description and adds them to the\n parent ip scope.\n :param hostipaddress: str of ipv4 address of the target host ip record\n :param name: str of the name of the owner of the target host ip record\n :param description: str of a description of the target host ip record\n :return:\n \"\"\"\n add_scope_ip(hostipaddress, name, description, self.auth, self.url, scopeid=self.id)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nobjecting method takes in input of hostip address,removes them from the parent ip scope. :param hostid: str of the hostid of the target host ip record :return:", "response": "def deallocate_ip(self, hostipaddress):\n \"\"\"\n Object method takes in input of hostip address,removes them from the parent ip scope.\n :param hostid: str of the hostid of the target host ip record\n\n :return:\n \"\"\"\n delete_host_from_segment(hostipaddress, self.netaddr, self.auth, self.url)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes no input or returns list of dicts where each dictionary represents one or more dictionaries where each dictionary represents one or more telnet template", "response": "def get_telnet_template(auth, url, template_name=None):\n \"\"\"\n Takes no input, or template_name as input to issue RESTUL call to HP IMC\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param template_name: str value of template name\n\n :return list object containing one or more dictionaries where each dictionary represents one\n telnet template\n\n :rtype list\n\n \"\"\"\n f_url = url + \"/imcrs/plat/res/telnet?start=0&size=10000&desc=false&total=false\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n telnet_templates = (json.loads(response.text))\n template = None\n if type(telnet_templates['telnetParamTemplate']) is dict:\n my_templates = [telnet_templates['telnetParamTemplate']]\n telnet_templates['telnetParamTemplate'] = my_templates\n if template_name is None:\n return telnet_templates['telnetParamTemplate']\n elif template_name is not None:\n for telnet_template in telnet_templates['telnetParamTemplate']:\n\n if telnet_template['name'] == template_name:\n template = [telnet_template]\n print (type(template))\n if template == None:\n return 404\n else:\n return template\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_telnet_templates: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef modify_telnet_template(auth, url, telnet_template, template_name= None, template_id = None):\n if template_name is None:\n template_name = telnet_template['name']\n if template_id is None:\n telnet_templates = get_telnet_template(auth, url)\n template_id = None\n for template in telnet_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/telnet/\"+str(template_id)+\"/update\"\n response = requests.put(f_url, data = json.dumps(telnet_template), auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" modify_telnet_template: An Error has occured\"", "response": "Function takes input of a dictionary containing the required key - value pair for the modification of a specific telnet template."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ntakes template_name as input to issue RESTUL call to HP IMC which will delete the specific telnet template from the IMC system :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param template_name: str value of template name :param template_id: str value template template_id value :return: int HTTP response code :rtype int", "response": "def delete_telnet_template(auth, url, template_name= None, template_id= None):\n \"\"\"\n Takes template_name as input to issue RESTUL call to HP IMC which will delete the specific\n telnet template from the IMC system\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param template_name: str value of template name\n\n :param template_id: str value template template_id value\n\n :return: int HTTP response code\n\n :rtype int\n \"\"\"\n try:\n if template_id is None:\n telnet_templates = get_telnet_template(auth, url)\n if template_name is None:\n template_name = telnet_template['name']\n template_id = None\n for template in telnet_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/telnet/%s/delete\" % template_id\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_telnet_template: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_ssh_template(auth, url, ssh_template ):\n f_url = url + \"/imcrs/plat/res/ssh/add\"\n response = requests.post(f_url, data = json.dumps(ssh_template), auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" create_ssh_template: An Error has occured\"", "response": "Function creates an SSH template"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntake no input, or template_name as input to issue RESTUL call to HP IMC :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param template_name: str value of template name :return list object containing one or more dictionaries where each dictionary represents one ssh template :rtype list", "response": "def get_ssh_template(auth, url, template_name=None):\n \"\"\"\n Takes no input, or template_name as input to issue RESTUL call to HP IMC\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param template_name: str value of template name\n\n :return list object containing one or more dictionaries where each dictionary represents one\n ssh template\n\n :rtype list\n\n \"\"\"\n f_url = url + \"/imcrs/plat/res/ssh?start=0&size=10000&desc=false&total=false\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n ssh_templates = (json.loads(response.text))\n template = None\n if type(ssh_templates['sshParamTemplate']) is dict:\n my_templates = [ssh_templates['sshParamTemplate']]\n ssh_templates['sshParamTemplate'] = my_templates\n if template_name is None:\n return ssh_templates['sshParamTemplate']\n elif template_name is not None:\n for ssh_template in ssh_templates['sshParamTemplate']:\n if ssh_template['name'] == template_name:\n template = [ssh_template]\n print (type(template))\n if template == None:\n return 404\n else:\n return template\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_ssh_templates: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions takes input of a dictionary containing the required key - value pair for the modification of an internal SSH template.", "response": "def modify_ssh_template(auth, url, ssh_template, template_name= None, template_id = None):\n \"\"\"\n Function takes input of a dictionry containing the required key/value pair for the modification\n of a ssh template.\n\n :param auth:\n :param url:\n :param ssh_template: Human readable label which is the name of the specific ssh template\n :param template_id Internal IMC number which designates the specific ssh template\n :return: int value of HTTP response code 201 for proper creation or 404 for failed creation\n :rtype int\n\n Sample of proper KV pairs. Please see documentation for valid values for different fields.\n\n ssh_template = {\n \"type\": \"0\",\n \"name\": \"ssh_admin_template\",\n \"authType\": \"3\",\n \"authTypeStr\": \"Password + Super Password\",\n \"userName\": \"newadmin\",\n \"password\": \"password\",\n \"superPassword\": \"password\",\n \"port\": \"22\",\n \"timeout\": \"10\",\n \"retries\": \"3\",\n \"keyFileName\": \"\",\n \"keyPhrase\": \"\"\n }\n \"\"\"\n if template_name is None:\n template_name = ssh_template['name']\n if template_id is None:\n ssh_templates = get_ssh_template(auth, url)\n template_id = None\n for template in ssh_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/ssh/\"+str(template_id)+\"/update\"\n response = requests.put(f_url, data = json.dumps(ssh_template), auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" modify_ssh_template: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_ssh_template(auth, url, template_name= None, template_id= None):\n try:\n if template_id is None:\n ssh_templates = get_ssh_template(auth, url)\n if template_name is None:\n template_name = ssh_template['name']\n template_id = None\n for template in ssh_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/ssh/%s/delete\" % template_id\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_ssh_template: An Error has occured\"", "response": "This function will issue RESTUL call to HP IMC which will delete the specific\n ssh template from the system."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntaking no input, or template_name as input to issue RESTUL call to HP IMC :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param template_name: str value of template name :return list object containing one or more dictionaries where each dictionary represents one snmp template :rtype list", "response": "def get_snmp_templates(auth, url, template_name=None):\n \"\"\"\n Takes no input, or template_name as input to issue RESTUL call to HP IMC\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param template_name: str value of template name\n\n :return list object containing one or more dictionaries where each dictionary represents one\n snmp template\n\n :rtype list\n\n \"\"\"\n f_url = url + \"/imcrs/plat/res/snmp?start=0&size=10000&desc=false&total=false\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n snmp_templates = (json.loads(response.text))\n template = None\n if type(snmp_templates['snmpParamTemplate']) is dict:\n my_templates = [snmp_templates['snmpParamTemplate']]\n snmp_templates['snmpParamTemplate'] = my_templates\n if template_name is None:\n return snmp_templates['snmpParamTemplate']\n elif template_name is not None:\n for snmp_template in snmp_templates['snmpParamTemplate']:\n if snmp_template['name'] == template_name:\n template = [snmp_template]\n if template == None:\n return 404\n else:\n return template\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_snmp_templates: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef modify_snmp_template(auth, url, snmp_template, template_name= None, template_id = None):\n if template_name is None:\n template_name = snmp_template['name']\n if template_id is None:\n snmp_templates = get_snmp_templates(auth, url)\n template_id = None\n for template in snmp_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/snmp/\"+str(template_id)+\"/update\"\n response = requests.put(f_url, data = json.dumps(snmp_template), auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" modify_snmp_template: An Error has occured\"", "response": "Function to modify an existing SNMP template."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_snmp_template(auth, url, template_name= None, template_id= None):\n try:\n if template_id is None:\n snmp_templates = get_snmp_templates(auth, url)\n if template_name is None:\n template_name = snmp_template['name']\n template_id = None\n for template in snmp_templates:\n if template['name'] == template_name:\n template_id = template['id']\n f_url = url + \"/imcrs/plat/res/snmp/%s/delete\" % template_id\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_snmp_template: An Error has occured\"", "response": "This function will issue RESTUL call to HP IMC which will delete the specific\n from the IMC system."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntaking in ipaddress as input to fetch device assett details from HP IMC RESTFUL API", "response": "def get_dev_asset_details(ipaddress, auth, url):\n \"\"\"Takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API\n\n :param ipaddress: IP address of the device you wish to gather the asset details\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: object of type list containing the device asset details, with each asset contained\n in a dictionary\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.netassets import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> single_asset = get_dev_asset_details('10.101.0.1', auth.creds, auth.url)\n\n >>> assert type(single_asset) is list\n\n >>> assert 'name' in single_asset[0]\n\n \"\"\"\n ipaddress = get_dev_details(ipaddress, auth, url)\n if isinstance(ipaddress, dict):\n ipaddress = ipaddress['ip']\n else:\n print(\"Asset Doesn't Exist\")\n return 403\n f_url = url + \"/imcrs/netasset/asset?assetDevice.ip=\" + str(ipaddress)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_asset_info = (json.loads(response.text))\n if len(dev_asset_info) > 0:\n dev_asset_info = dev_asset_info['netAsset']\n if isinstance(dev_asset_info, dict):\n dev_asset_info = [dev_asset_info]\n if isinstance(dev_asset_info, list):\n dev_asset_info[:] = [dev for dev in dev_asset_info if dev.get('deviceIp') ==\n ipaddress]\n return dev_asset_info\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_dev_asset_details: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ntakes no input to fetch device assett details from HP IMC RESTFUL API", "response": "def get_dev_asset_details_all(auth, url):\n \"\"\"Takes no input to fetch device assett details from HP IMC RESTFUL API\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionatires containing the device asset details\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.netassets import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> all_assets = get_dev_asset_details_all( auth.creds, auth.url)\n\n >>> assert type(all_assets) is list\n\n >>> assert 'asset' in all_assets[0]\n\n \"\"\"\n f_url = url + \"/imcrs/netasset/asset?start=0&size=15000\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_asset_info = (json.loads(response.text))['netAsset']\n return dev_asset_info\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_dev_asset_details: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nruns a non - encrypted non - authorized API proxy server.", "response": "def proxy(ctx, bind, port):\n \"\"\"\n Run a non-encrypted non-authorized API proxy server.\n Use this only for development and testing!\n \"\"\"\n app = web.Application()\n app.on_startup.append(startup_proxy)\n app.on_cleanup.append(cleanup_proxy)\n\n app.router.add_route(\"GET\", r'/stream/{path:.*$}', websocket_handler)\n app.router.add_route(\"GET\", r'/wsproxy/{path:.*$}', websocket_handler)\n app.router.add_route('*', r'/{path:.*$}', web_handler)\n if getattr(ctx.args, 'testing', False):\n return app\n web.run_app(app, host=bind, port=port)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntakes string input of IP address to issue RESTUL call to HP IMC\\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param network_address: str IPv4 Network Address :param category: str or int corresponding to device category (0=router, 1=switches, see API docs for other examples) :return: dictionary of device details :rtype: dict >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.device import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> dev_list = get_all_devs( auth.creds, auth.url, network_address= '10.11.') >>> assert type(dev_list) is list >>> assert 'sysName' in dev_list[0]", "response": "def get_all_devs(auth, url, network_address=None, category=None, label=None):\n \"\"\"Takes string input of IP address to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n :param network_address: str IPv4 Network Address\n :param category: str or int corresponding to device category (0=router, 1=switches, see API docs for other examples)\n :return: dictionary of device details\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> dev_list = get_all_devs( auth.creds, auth.url, network_address= '10.11.')\n\n >>> assert type(dev_list) is list\n\n >>> assert 'sysName' in dev_list[0]\n\n \"\"\"\n base_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false\"\n end_url = \"&start=0&size=1000&orderBy=id&desc=false&total=false\"\n if network_address:\n network_address = \"&ip=\" + str(network_address)\n else:\n network_address = ''\n if label:\n label = \"&label=\" + str(label)\n else:\n label = ''\n if category:\n category = \"&category\" + category\n else:\n category = ''\n f_url = url + base_url + str(network_address) + str(label) + str(category) + end_url\n print(f_url)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_details = (json.loads(response.text))\n if len(dev_details) == 0:\n print(\"Device not found\")\n return \"Device not found\"\n elif type(dev_details['device']) is dict:\n return [dev_details['device']]\n else:\n return dev_details['device']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_dev_details(ip_address, auth, url):\n get_dev_details_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false&ip=\" + \\\n str(ip_address) + \"&start=0&size=1000&orderBy=id&desc=false&total=false\"\n f_url = url + get_dev_details_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_details = (json.loads(response.text))\n if len(dev_details) == 0:\n print(\"Device not found\")\n return \"Device not found\"\n elif isinstance(dev_details['device'], list):\n for i in dev_details['device']:\n if i['ip'] == ip_address:\n dev_details = i\n return dev_details\n elif isinstance(dev_details['device'], dict):\n return dev_details['device']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_dev_details: An Error has occured\"", "response": "This function issues RESTUL call to HP IMC API to get details of a device."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_dev_interface(auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_dev_interface_url = \"/imcrs/plat/res/device/\" + str(devid) + \\\n \"/interface?start=0&size=1000&desc=false&total=false\"\n f_url = url + get_dev_interface_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n int_list = json.loads(response.text)\n if 'interface' in int_list:\n return int_list['interface']\n else:\n return []\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_dev_interface: An Error has occured\"", "response": "Function returns list of device objects which contains a dictionary per interface"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_dev_mac_learn(auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n f_url = url + '/imcrs/res/access/ipMacLearn/' + str(devid)\n try:\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n if response.status_code == 200:\n if len(json.loads(response.text)) < 1:\n mac_learn_query = []\n return mac_learn_query\n else:\n mac_learn_query = (json.loads(response.text))['ipMacLearnResult']\n if isinstance(mac_learn_query, dict):\n mac_learn_query = [mac_learn_query]\n return mac_learn_query\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_dev_mac_learn: An Error has occured\"", "response": "function takes devid of specific device and issues a RESTFUL call to gather the current IP - MAC learning entries on the target device."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning runs the command list on the target device and returns the output of the commands.", "response": "def run_dev_cmd(cmd_list, auth, url, devid=None, devip=None):\n \"\"\"\n Function takes devid of target device and a sequential list of strings which define the\n specific commands to be run on the target device and returns a str object containing the\n output of the commands.\n\n :param devid: int devid of the target device\n\n :param cmd_list: list of strings\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devip: str of ipv4 address of the target device\n\n :return: str containing the response of the commands\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> cmd_list = ['display version']\n\n >>> cmd_output = run_dev_cmd( cmd_list, auth.creds, auth.url, devid ='10')\n\n >>> cmd_output = run_dev_cmd( cmd_list, auth.creds, auth.url, devip='10.101.0.221')\n\n >>> assert type(cmd_output) is dict\n\n >>> assert 'cmdlist' in cmd_output\n\n >>> assert 'success' in cmd_output\n\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n run_dev_cmd_url = '/imcrs/icc/confFile/executeCmd'\n f_url = url + run_dev_cmd_url\n cmd_list = _make_cmd_list(cmd_list)\n payload = '''{ \"deviceId\" : \"''' + str(devid) + '''\",\n \"cmdlist\" : { \"cmd\" :\n [''' + cmd_list + ''']\n }\n }'''\n try:\n response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n if response.status_code == 200:\n if len(response.text) < 1:\n return ''\n else:\n return json.loads(response.text)\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" run_dev_cmd: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_interface_details(auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_all_interface_details_url = \"/imcrs/plat/res/device/\" + str(\n devid) + \"/interface/?start=0&size=1000&desc=false&total=false\"\n f_url = url + get_all_interface_details_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_details = (json.loads(response.text))\n return dev_details['interface']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_all_interface_details: An Error has occured\"", "response": "function to get all interfaces on a specific device"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction takest devid and ifindex of specific device and interface and issues a RESTFUL call to \" shut\" the specified interface on the target device. :param devid: int or str value of the target device :param devip: ipv4 address of the target devices :param ifindex: int or str value of the target interface :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: HTTP status code 204 with no values. :rtype:int >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.device import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221') >>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devid = '10') 204 >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221') >>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devip = '10.101.0.221') 204 >>> assert type(int_down_response) is int >>> assert int_down_response is 204 >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221')", "response": "def set_interface_down(ifindex, auth, url, devid=None, devip=None):\n \"\"\"\n function takest devid and ifindex of specific device and interface and issues a RESTFUL call\n to \" shut\" the specified interface on the target device.\n :param devid: int or str value of the target device\n\n :param devip: ipv4 address of the target devices\n\n :param ifindex: int or str value of the target interface\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: HTTP status code 204 with no values.\n\n :rtype:int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221')\n\n >>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devid = '10')\n 204\n\n >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221')\n\n >>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devip = '10.101.0.221')\n 204\n\n >>> assert type(int_down_response) is int\n\n >>> assert int_down_response is 204\n\n >>> int_up_response = set_inteface_up('9', auth.creds, auth.url, devip = '10.101.0.221')\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n set_int_down_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \\\n \"/down\"\n f_url = url + set_int_down_url\n try:\n response = requests.put(f_url, auth=auth, headers=HEADERS)\n print(response.status_code)\n if response.status_code == 204:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" set_inteface_down: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_inteface_up(ifindex, auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n set_int_up_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \"/up\"\n f_url = url + set_int_up_url\n try:\n response = requests.put(f_url, auth=auth, headers=HEADERS)\n if response.status_code == 204:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" set_inteface_up: An Error has occured\"", "response": "This function sets the interface up on the specified device and returns a RESTFUL call to undo shut the specified interface on the target device."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _make_cmd_list(cmd_list):\n cmd = ''\n for i in cmd_list:\n cmd = cmd + '\"' + i + '\",'\n cmd = cmd[:-1]\n return cmd", "response": "Helper function to easily create a proper json formated string from a list of strings"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a new keypair with the given options.", "response": "async def create(cls, user_id: Union[int, str],\n is_active: bool = True,\n is_admin: bool = False,\n resource_policy: str = None,\n rate_limit: int = None,\n fields: Iterable[str] = None) -> dict:\n '''\n Creates a new keypair with the given options.\n You need an admin privilege for this operation.\n '''\n if fields is None:\n fields = ('access_key', 'secret_key')\n uid_type = 'Int!' if isinstance(user_id, int) else 'String!'\n q = 'mutation($user_id: {0}, $input: KeyPairInput!) {{'.format(uid_type) + \\\n ' create_keypair(user_id: $user_id, props: $input) {' \\\n ' ok msg keypair { $fields }' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n variables = {\n 'user_id': user_id,\n 'input': {\n 'is_active': is_active,\n 'is_admin': is_admin,\n 'resource_policy': resource_policy,\n 'rate_limit': rate_limit,\n },\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['create_keypair']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating the current keypair with the given options.", "response": "async def update(cls, access_key: str,\n is_active: bool = None,\n is_admin: bool = None,\n resource_policy: str = None,\n rate_limit: int = None) -> dict:\n \"\"\"\n Creates a new keypair with the given options.\n You need an admin privilege for this operation.\n \"\"\"\n q = 'mutation($access_key: String!, $input: ModifyKeyPairInput!) {' + \\\n ' modify_keypair(access_key: $access_key, props: $input) {' \\\n ' ok msg' \\\n ' }' \\\n '}'\n variables = {\n 'access_key': access_key,\n 'input': {\n 'is_active': is_active,\n 'is_admin': is_admin,\n 'resource_policy': resource_policy,\n 'rate_limit': rate_limit,\n },\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['modify_keypair']"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def delete(cls, access_key: str):\n q = 'mutation($access_key: String!) {' \\\n ' delete_keypair(access_key: $access_key) {' \\\n ' ok msg' \\\n ' }' \\\n '}'\n variables = {\n 'access_key': access_key,\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['delete_keypair']", "response": "Deletes an existing keypair with given ACCESSKEY."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def list(cls, user_id: Union[int, str] = None,\n is_active: bool = None,\n fields: Iterable[str] = None) -> Sequence[dict]:\n '''\n Lists the keypairs.\n You need an admin privilege for this operation.\n '''\n if fields is None:\n fields = (\n 'access_key', 'secret_key',\n 'is_active', 'is_admin',\n )\n if user_id is None:\n q = 'query($is_active: Boolean) {' \\\n ' keypairs(is_active: $is_active) {' \\\n ' $fields' \\\n ' }' \\\n '}'\n else:\n uid_type = 'Int!' if isinstance(user_id, int) else 'String!'\n q = 'query($user_id: {0}, $is_active: Boolean) {{'.format(uid_type) + \\\n ' keypairs(user_id: $user_id, is_active: $is_active) {' \\\n ' $fields' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n variables = {\n 'is_active': is_active,\n }\n if user_id is not None:\n variables['user_id'] = user_id\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['keypairs']", "response": "Lists the keypairs.\n You need an admin privilege for this operation."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def info(self, fields: Iterable[str] = None) -> dict:\n '''\n Returns the keypair's information such as resource limits.\n\n :param fields: Additional per-agent query fields to fetch.\n\n .. versionadded:: 18.12\n '''\n if fields is None:\n fields = (\n 'access_key', 'secret_key',\n 'is_active', 'is_admin',\n )\n q = 'query {' \\\n ' keypair {' \\\n ' $fields' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n rqst = Request(self.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['keypair']", "response": "Returns the keypair s information such as resource limits and resource limits."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def activate(cls, access_key: str) -> dict:\n '''\n Activates this keypair.\n You need an admin privilege for this operation.\n '''\n q = 'mutation($access_key: String!, $input: ModifyKeyPairInput!) {' + \\\n ' modify_keypair(access_key: $access_key, props: $input) {' \\\n ' ok msg' \\\n ' }' \\\n '}'\n variables = {\n 'access_key': access_key,\n 'input': {\n 'is_active': True,\n 'is_admin': None,\n 'resource_policy': None,\n 'rate_limit': None,\n },\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['modify_keypair']", "response": "Activates this keypair.\n You need an admin privilege for this operation."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeactivates this keypair. Deactivated keypairs cannot make any API requests unless activated again by an administrator. You need an admin privilege for this operation.", "response": "async def deactivate(cls, access_key: str) -> dict:\n '''\n Deactivates this keypair.\n Deactivated keypairs cannot make any API requests\n unless activated again by an administrator.\n You need an admin privilege for this operation.\n '''\n q = 'mutation($access_key: String!, $input: ModifyKeyPairInput!) {' + \\\n ' modify_keypair(access_key: $access_key, props: $input) {' \\\n ' ok msg' \\\n ' }' \\\n '}'\n variables = {\n 'access_key': access_key,\n 'input': {\n 'is_active': False,\n 'is_admin': None,\n 'resource_policy': None,\n 'rate_limit': None,\n },\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['modify_keypair']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction adds a new Ip Scope to the HPE IMC base access object.", "response": "def add_ip_scope(auth, url,startIp, endIp, name, description):\n \"\"\"\n Function takes input of four strings Start Ip, endIp, name, and description to add new Ip Scope to terminal access\n in the HPE IMC base platform\n :param startIp: str Start of IP address scope ex. '10.101.0.1'\n :param endIp: str End of IP address scope ex. '10.101.0.254'\n :param name: str Name of the owner of this IP scope ex. 'admin'\n :param description: str description of the Ip scope\n :return:\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n\n add_ip_scope_url = \"/imcrs/res/access/assignedIpScope\"\n f_url = url + add_ip_scope_url\n payload = ('''{ \"startIp\": \"%s\", \"endIp\": \"%s\",\"name\": \"%s\",\"description\": \"%s\" }'''\n %(str(startIp), str(endIp), str(name), str(description)))\n r = requests.post(f_url, auth=auth, headers=HEADERS, data=payload) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n print(\"IP Scope Successfully Created\")\n return r.status_code\n elif r.status_code == 409:\n print (\"IP Scope Already Exists\")\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" add_ip_scope: An Error has occured\"\n\n\n #Add host to IP scope\n #http://10.101.0.203:8080/imcrs/res/access/assignedIpScope/ip?ipScopeId=1\n '''{\n \"ip\": \"10.101.0.1\",\n \"name\": \"Cisco2811.lab.local\",\n \"description\": \"Cisco 2811\",\n \"parentId\": \"1\"\n }'''"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def check_presets(cls):\n '''\n Lists all resource presets in the current scaling group with additiona\n information.\n '''\n rqst = Request(cls.session, 'POST', '/resource/check-presets')\n async with rqst.fetch() as resp:\n return await resp.json()", "response": "Lists all resource presets in the current scaling group with additiona\n information."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_perf_task(task, auth, url):\n add_perf_task_url = \"/imcrs/perf/task\"\n f_url = url + add_perf_task_url\n payload = json.dumps(task)\n # creates the URL using the payload variable as the contents\n r = requests.post(f_url, data = payload, auth=auth, headers=headers)\n try:\n return r.status_code\n\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_alarms: An Error has occured'", "response": "function takes a python dict containing all required fields for a performance task and issues a RESTFUL call to create the performance task."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfunctions takes a str object containing the name of an existing performance task and issues a RESTFUL call to the IMC REST service and returns a list of the performance task s attributes.", "response": "def get_perf_task(task_name, auth, url):\n \"\"\"\n function takes the a str object containing the name of an existing performance tasks and issues a RESTFUL call\n to the IMC REST service. It will return a list\n\n :param task_name: str containing the name of the performance task\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: 204\n\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.perf import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> selected_task = get_perf_task('Cisco_Temperature', auth.creds, auth.url)\n\n >>> assert type(selected_task) is dict\n\n >>> assert 'taskName' in selected_task\n \"\"\"\n get_perf_task_url = \"/imcrs/perf/task?name=\"+task_name+\"&orderBy=taskId&desc=false\"\n f_url = url + get_perf_task_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n try:\n if r.status_code == 200:\n perf_task_info = (json.loads(r.text))['task']\n return perf_task_info\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_alarms: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_vm_host_info(hostip, auth, url):\n hostId = get_dev_details(hostip, auth, url)['id']\n get_vm_host_info_url = \"/imcrs/vrm/host?hostId=\" + str(hostId)\n f_url = url + get_vm_host_info_url\n payload = None\n r = requests.get(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n # print(r.status_code)\n try:\n if r.status_code == 200:\n if len(r.text) > 0:\n return json.loads(r.text)\n elif r.status_code == 204:\n print(\"Device is not a supported Hypervisor\")\n return \"Device is not a supported Hypervisor\"\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_vm_host_info: An Error has occured\"", "response": "function to get information for the target VM"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_ac_info_all(auth, url):\n f_url = url + \"/imcrs/wlan/acInfo/queryAcBasicInfo\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n if len(response.text) > 0:\n acs = json.loads(response.text)['acBasicInfo']\n if type(acs) is dict:\n acs = [acs]\n return acs\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_ac_info_all: An Error has occured\"", "response": "function takes no input to RESTFUL call to HP IMC API to get list of ACs that have been discovered in the HPE IMC WSM module"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_imc_creds(h_url=None, imc_server=None, imc_port=None, imc_user=None,imc_pw=None):\n global auth, url\n if h_url is None:\n imc_protocol = input(\n \"What protocol would you like to use to connect to the IMC server: \\n Press 1 for HTTP: \\n Press 2 for HTTPS:\")\n if imc_protocol == \"1\":\n h_url = 'http://'\n else:\n h_url = 'https://'\n imc_server = input(\"What is the ip address of the IMC server?\")\n imc_port = input(\"What is the port number of the IMC server?\")\n imc_user = input(\"What is the username of the IMC eAPI user?\")\n imc_pw = input('''What is the password of the IMC eAPI user?''')\n url = h_url + imc_server + \":\" + imc_port\n auth = requests.auth.HTTPDigestAuth(imc_user, imc_pw)\n test_url = '/imcrs'\n f_url = url + test_url\n try:\n r = requests.get(f_url, auth=auth, headers=headers, verify=False)\n print (r.status_code)\n return auth\n # checks for reqeusts exceptions\n except requests.exceptions.RequestException as e:\n print(\"Error:\\n\" + str(e))\n print(\"\\n\\nThe IMC server address is invalid. Please try again\\n\\n\")\n set_imc_creds()\n if r.status_code != 200: # checks for valid IMC credentials\n print(\"Error: \\n You're credentials are invalid. Please try again\\n\\n\")\n set_imc_creds()\n else:\n print(\"You've successfully access the IMC eAPI\")", "response": "This function prompts user for IMC server information and credentuials and stores the values in url and auth global variables"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction prints out the object to file handler and saves it to current working directory as pyoutput. txt", "response": "def print_to_file(object):\n \"\"\"\n Function takes in object of type str, list, or dict and prints out to current working directory as pyoutput.txt\n :param: Object: object of type str, list, or dict\n :return: No return. Just prints out to file handler and save to current working directory as pyoutput.txt\n \"\"\"\n with open ('pyoutput.txt', 'w') as fh:\n x = None\n if type(object) is list:\n x = json.dumps(object, indent = 4)\n if type(object) is dict:\n x = json.dumps(object, indent = 4)\n if type (object) is str:\n x = object\n fh.write(x)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_auth(self):\n url = self.h_url + self.server + \":\" + self.port\n auth = requests.auth.HTTPDigestAuth(self.username,self.password)\n auth_url = \"/imcrs\"\n f_url = url + auth_url\n try:\n r = requests.get(f_url, auth=auth, headers=headers, verify=False)\n return r.status_code\n # checks for reqeusts exceptions\n except requests.exceptions.RequestException as e:\n return (\"Error:\\n\" + str(e) + '\\n\\nThe IMC server address is invalid. Please try again')\n set_imc_creds()\n if r.status_code != 200: # checks for valid IMC credentials\n return (\"Error:\\n\" + str(e) +\"Error: \\n You're credentials are invalid. Please try again\\n\\n\")\n set_imc_creds()", "response": "This method requests an authentication object from the HPE IMC NMS and returns an HTTPDigest Auth Object containing the current state of the object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_version(root):\n version_json = os.path.join(root, 'version.json')\n if os.path.exists(version_json):\n with open(version_json, 'r') as version_json_file:\n return json.load(version_json_file)\n return None", "response": "Load and return the contents of version. json."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall this instance s request_client s post method with the specified component endpoint_name and identifier_input.", "response": "def fetch(self, endpoint_name, identifier_input, query_params=None):\n \"\"\"Calls this instance's request_client's post method with the\n specified component endpoint\n\n Args:\n - endpoint_name (str) - The endpoint to call like \"property/value\".\n - identifier_input - One or more identifiers to request data for. An identifier can\n be in one of these forms:\n\n - A list of property identifier dicts:\n - A property identifier dict can contain the following keys:\n (address, zipcode, unit, city, state, slug, meta).\n One of 'address' or 'slug' is required.\n\n Ex: [{\"address\": \"82 County Line Rd\",\n \"zipcode\": \"72173\",\n \"meta\": \"some ID\"}]\n\n A slug is a URL-safe string that identifies a property.\n These are obtained from HouseCanary.\n\n Ex: [{\"slug\": \"123-Example-St-San-Francisco-CA-94105\"}]\n\n - A list of dicts representing a block:\n - A block identifier dict can contain the following keys:\n (block_id, num_bins, property_type, meta).\n 'block_id' is required.\n\n Ex: [{\"block_id\": \"060750615003005\", \"meta\": \"some ID\"}]\n\n - A list of dicts representing a zipcode:\n\n Ex: [{\"zipcode\": \"90274\", \"meta\": \"some ID\"}]\n\n - A list of dicts representing an MSA:\n\n Ex: [{\"msa\": \"41860\", \"meta\": \"some ID\"}]\n\n The \"meta\" field is always optional.\n\n Returns:\n A Response object, or the output of a custom OutputGenerator\n if one was specified in the constructor.\n \"\"\"\n\n endpoint_url = constants.URL_PREFIX + \"/\" + self._version + \"/\" + endpoint_name\n\n if query_params is None:\n query_params = {}\n\n if len(identifier_input) == 1:\n # If only one identifier specified, use a GET request\n query_params.update(identifier_input[0])\n return self._request_client.get(endpoint_url, query_params)\n\n # when more than one address, use a POST request\n return self._request_client.post(endpoint_url, identifier_input, query_params)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalls this instance s request_client s get method with the the specified component endpoint", "response": "def fetch_synchronous(self, endpoint_name, query_params=None):\n \"\"\"Calls this instance's request_client's get method with the\n specified component endpoint\"\"\"\n\n endpoint_url = constants.URL_PREFIX + \"/\" + self._version + \"/\" + endpoint_name\n\n if query_params is None:\n query_params = {}\n\n return self._request_client.get(endpoint_url, query_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_identifier_input(self, identifier_data):\n\n identifier_input = []\n\n if isinstance(identifier_data, list) and len(identifier_data) > 0:\n # if list, convert each item in the list to json\n for address in identifier_data:\n identifier_input.append(self._convert_to_identifier_json(address))\n else:\n identifier_input.append(self._convert_to_identifier_json(identifier_data))\n\n return identifier_input", "response": "Convert the various formats of input identifier_data into\n the proper json format expected by the ApiClient fetch method."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef fetch_identifier_component(self, endpoint_name, identifier_data, query_params=None):\n\n if query_params is None:\n query_params = {}\n\n identifier_input = self.get_identifier_input(identifier_data)\n\n return self._api_client.fetch(endpoint_name, identifier_input, query_params)", "response": "Fetch the identifier component of the given endpoint"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts input address data into json format", "response": "def _convert_to_identifier_json(self, address_data):\n \"\"\"Convert input address data into json format\"\"\"\n\n if isinstance(address_data, str):\n # allow just passing a slug string.\n return {\"slug\": address_data}\n\n if isinstance(address_data, tuple) and len(address_data) > 0:\n address_json = {\"address\": address_data[0]}\n if len(address_data) > 1:\n address_json[\"zipcode\"] = address_data[1]\n if len(address_data) > 2:\n address_json[\"meta\"] = address_data[2]\n return address_json\n\n if isinstance(address_data, dict):\n allowed_keys = [\"address\", \"zipcode\", \"unit\", \"city\", \"state\", \"slug\", \"meta\",\n \"client_value\", \"client_value_sqft\"]\n\n # ensure the dict does not contain any unallowed keys\n for key in address_data:\n if key not in allowed_keys:\n msg = \"Key in address input not allowed: \" + key\n raise housecanary.exceptions.InvalidInputException(msg)\n\n # ensure it contains an \"address\" key\n if \"address\" in address_data or \"slug\" in address_data:\n return address_data\n\n # if we made it here, the input was not valid.\n msg = (\"Input is invalid. Must be a list of (address, zipcode) tuples, or a dict or list\"\n \" of dicts with each item containing at least an 'address' or 'slug' key.\")\n raise housecanary.exceptions.InvalidInputException((msg))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef value_report(self, address, zipcode, report_type=\"full\", format_type=\"json\"):\n query_params = {\n \"report_type\": report_type,\n \"format\": format_type,\n \"address\": address,\n \"zipcode\": zipcode\n }\n\n return self._api_client.fetch_synchronous(\"property/value_report\", query_params)", "response": "Call the value_report component on the value store."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall the rental_report component on the specified address and zipcode.", "response": "def rental_report(self, address, zipcode, format_type=\"json\"):\n \"\"\"Call the rental_report component\n\n Rental Report only supports a single address.\n\n Args:\n - address\n - zipcode\n\n Kwargs:\n - format_type - \"json\", \"xlsx\" or \"all\". Default is \"json\".\n \"\"\"\n\n # only json is supported by rental report.\n query_params = {\n \"format\": format_type,\n \"address\": address,\n \"zipcode\": zipcode\n }\n\n return self._api_client.fetch_synchronous(\"property/rental_report\", query_params)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall the zip component_mget endpoint.", "response": "def component_mget(self, zip_data, components):\n \"\"\"Call the zip component_mget endpoint\n\n Args:\n - zip_data - As described in the class docstring.\n - components - A list of strings for each component to include in the request.\n Example: [\"zip/details\", \"zip/volatility\"]\n \"\"\"\n if not isinstance(components, list):\n print(\"Components param must be a list\")\n return\n\n query_params = {\"components\": \",\".join(components)}\n\n return self.fetch_identifier_component(\n \"zip/component_mget\", zip_data, query_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef version(request):\n version_json = import_string(version_callback)(settings.BASE_DIR)\n if version_json is None:\n return HttpResponseNotFound('version.json not found')\n else:\n return JsonResponse(version_json)", "response": "Returns the contents of version. json or a 404."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrunning all the Django checks and returns a JsonResponse with either a status code of 200 or 500 depending on the results of the checks.", "response": "def heartbeat(request):\n \"\"\"\n Runs all the Django checks and returns a JsonResponse with either\n a status code of 200 or 500 depending on the results of the checks.\n\n Any check that returns a warning or worse (error, critical) will\n return a 500 response.\n \"\"\"\n all_checks = checks.registry.registry.get_checks(\n include_deployment_checks=not settings.DEBUG,\n )\n\n details = {}\n statuses = {}\n level = 0\n\n for check in all_checks:\n detail = heartbeat_check_detail(check)\n statuses[check.__name__] = detail['status']\n level = max(level, detail['level'])\n if detail['level'] > 0:\n details[check.__name__] = detail\n\n if level < checks.messages.WARNING:\n status_code = 200\n heartbeat_passed.send(sender=heartbeat, level=level)\n else:\n status_code = 500\n heartbeat_failed.send(sender=heartbeat, level=level)\n\n payload = {\n 'status': level_to_text(level),\n 'checks': statuses,\n 'details': details,\n }\n return JsonResponse(payload, status=status_code)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a list of ComponentResult objects from the json_data.", "response": "def _create_component_results(json_data, result_key):\n \"\"\" Returns a list of ComponentResult from the json_data\"\"\"\n component_results = []\n for key, value in list(json_data.items()):\n if key not in [result_key, \"meta\"]:\n component_result = ComponentResult(\n key,\n value[\"result\"],\n value[\"api_code\"],\n value[\"api_code_description\"]\n )\n\n component_results.append(component_result)\n\n return component_results"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn whether there was a business logic error when fetching data for any components for this property.", "response": "def has_error(self):\n \"\"\"Returns whether there was a business logic error when fetching data\n for any components for this property.\n\n Returns:\n boolean\n \"\"\"\n return next(\n (True for cr in self.component_results\n if cr.has_error()),\n False\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the error messages for this property.", "response": "def get_errors(self):\n \"\"\"If there were any business errors fetching data for this property,\n returns the error messages.\n\n Returns:\n string - the error message, or None if there was no error.\n\n \"\"\"\n return [{cr.component_name: cr.get_error()}\n for cr in self.component_results if cr.has_error()]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_from_json(cls, json_data):\n prop = Property()\n address_info = json_data[\"address_info\"]\n prop.address = address_info[\"address\"]\n prop.block_id = address_info[\"block_id\"]\n prop.zipcode = address_info[\"zipcode\"]\n prop.zipcode_plus4 = address_info[\"zipcode_plus4\"]\n prop.address_full = address_info[\"address_full\"]\n prop.city = address_info[\"city\"]\n prop.county_fips = address_info[\"county_fips\"]\n prop.geo_precision = address_info[\"geo_precision\"]\n prop.lat = address_info[\"lat\"]\n prop.lng = address_info[\"lng\"]\n prop.slug = address_info[\"slug\"]\n prop.state = address_info[\"state\"]\n prop.unit = address_info[\"unit\"]\n\n prop.meta = None\n if \"meta\" in json_data:\n prop.meta = json_data[\"meta\"]\n\n prop.component_results = _create_component_results(json_data, \"address_info\")\n\n return prop", "response": "Deserialize property json data into a Property object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef starts_when(iterable, condition):\n # type: (Iterable, Union[Callable, Any]) -> Iterable\n \"\"\"Start yielding items when a condition arise.\n\n Args:\n iterable: the iterable to filter.\n condition: if the callable returns True once, start yielding\n items. If it's not a callable, it will be converted\n to one as `lambda condition: condition == item`.\n\n Example:\n\n >>> list(starts_when(range(10), lambda x: x > 5))\n [6, 7, 8, 9]\n >>> list(starts_when(range(10), 7))\n [7, 8, 9]\n \"\"\"\n if not callable(condition):\n cond_value = condition\n\n def condition(x):\n return x == cond_value\n return itertools.dropwhile(lambda x: not condition(x), iterable)", "response": "Start yielding items when a condition arise."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstopping yielding items when a condition arise.", "response": "def stops_when(iterable, condition):\n # type: (Iterable, Union[Callable, Any]) -> Iterable\n \"\"\"Stop yielding items when a condition arise.\n\n Args:\n iterable: the iterable to filter.\n condition: if the callable returns True once, stop yielding\n items. If it's not a callable, it will be converted\n to one as `lambda condition: condition == item`.\n\n Example:\n\n >>> list(stops_when(range(10), lambda x: x > 5))\n [0, 1, 2, 3, 4, 5]\n >>> list(stops_when(range(10), 7))\n [0, 1, 2, 3, 4, 5, 6]\n \"\"\"\n if not callable(condition):\n cond_value = condition\n\n def condition(x):\n return x == cond_value\n return itertools.takewhile(lambda x: not condition(x), iterable)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef skip_duplicates(iterable, key=None, fingerprints=()):\n # type: (Iterable, Callable, Any) -> Iterable\n \"\"\"\n Returns a generator that will yield all objects from iterable, skipping\n duplicates.\n\n Duplicates are identified using the `key` function to calculate a\n unique fingerprint. This does not use natural equality, but the\n result use a set() to remove duplicates, so defining __eq__\n on your objects would have no effect.\n\n By default the fingerprint is the object itself,\n which ensure the functions works as-is with an iterable of primitives\n such as int, str or tuple.\n\n :Example:\n\n >>> list(skip_duplicates([1, 2, 3, 4, 4, 2, 1, 3 , 4]))\n [1, 2, 3, 4]\n\n The return value of `key` MUST be hashable, which means for\n non hashable objects such as dict, set or list, you need to specify\n a a function that returns a hashable fingerprint.\n\n :Example:\n\n >>> list(skip_duplicates(([], [], (), [1, 2], (1, 2)),\n ... lambda x: tuple(x)))\n [[], [1, 2]]\n >>> list(skip_duplicates(([], [], (), [1, 2], (1, 2)),\n ... lambda x: (type(x), tuple(x))))\n [[], (), [1, 2], (1, 2)]\n\n For more complex types, such as custom classes, the default behavior\n is to remove nothing. You MUST provide a `key` function is you wish\n to filter those.\n\n :Example:\n\n >>> class Test(object):\n ... def __init__(self, foo='bar'):\n ... self.foo = foo\n ... def __repr__(self):\n ... return \"Test('%s')\" % self.foo\n ...\n >>> list(skip_duplicates([Test(), Test(), Test('other')]))\n [Test('bar'), Test('bar'), Test('other')]\n >>> list(skip_duplicates([Test(), Test(), Test('other')],\\\n lambda x: x.foo))\n [Test('bar'), Test('other')]\n\n \"\"\"\n\n fingerprints = fingerprints or set()\n fingerprint = None # needed on type errors unrelated to hashing\n\n try:\n # duplicate some code to gain perf in the most common case\n if key is None:\n for x in iterable:\n if x not in fingerprints:\n yield x\n fingerprints.add(x)\n else:\n for x in iterable:\n fingerprint = key(x)\n if fingerprint not in fingerprints:\n yield x\n fingerprints.add(fingerprint)\n except TypeError:\n try:\n hash(fingerprint)\n except TypeError:\n raise TypeError(\n \"The 'key' function returned a non hashable object of type \"\n \"'%s' when receiving '%s'. Make sure this function always \"\n \"returns a hashable object. Hint: immutable primitives like\"\n \"int, str or tuple, are hashable while dict, set and list are \"\n \"not.\" % (type(fingerprint), x))\n else:\n raise", "response": "Returns a generator that yields all objects from iterable skipping duplicate entries."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nyield items from an iterable in chunks.", "response": "def chunks(iterable, chunksize, cast=tuple):\n # type: (Iterable, int, Callable) -> Iterable\n \"\"\"\n Yields items from an iterator in iterable chunks.\n \"\"\"\n it = iter(iterable)\n while True:\n yield cast(itertools.chain([next(it)],\n itertools.islice(it, chunksize - 1)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef window(iterable, size=2, cast=tuple):\n # type: (Iterable, int, Callable) -> Iterable\n \"\"\"\n Yields iterms by bunch of a given size, but rolling only one item\n in and out at a time when iterating.\n\n >>> list(window([1, 2, 3]))\n [(1, 2), (2, 3)]\n\n By default, this will cast the window to a tuple before yielding it;\n however, any function that will accept an iterable as its argument\n is a valid target.\n\n If you pass None as a cast value, the deque will be returned as-is,\n which is more performant. However, since only one deque is used\n for the entire iteration, you'll get the same reference everytime,\n only the deque will contains different items. The result might not\n be what you want :\n\n >>> list(window([1, 2, 3], cast=None))\n [deque([2, 3], maxlen=2), deque([2, 3], maxlen=2)]\n\n \"\"\"\n iterable = iter(iterable)\n d = deque(itertools.islice(iterable, size), size)\n if cast:\n yield cast(d)\n for x in iterable:\n d.append(x)\n yield cast(d)\n else:\n yield d\n for x in iterable:\n d.append(x)\n yield d", "response": "Yields the items in the given iterable in a given size but rolling only one item\n in and out at a time when iterating."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef at_index(iterable, index):\n # type: (Iterable[T], int) -> T\n \"\"\"\" Return the item at the index of this iterable or raises IndexError.\n\n WARNING: this will consume generators.\n\n Negative indices are allowed but be aware they will cause n items to\n be held in memory, where n = abs(index)\n \"\"\"\n try:\n if index < 0:\n return deque(iterable, maxlen=abs(index)).popleft()\n\n return next(itertools.islice(iterable, index, index + 1))\n except (StopIteration, IndexError) as e:\n raise_from(IndexError('Index \"%d\" out of range' % index), e)", "response": "Return the item at the index of this iterable."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef first_true(iterable, func):\n # type: (Iterable[T], Callable) -> T\n \"\"\"\" Return the first item of the iterable for which func(item) == True.\n\n Or raises IndexError.\n\n WARNING: this will consume generators.\n \"\"\"\n try:\n return next((x for x in iterable if func(x)))\n except StopIteration as e:\n # TODO: Find a better error message\n raise_from(IndexError('No match for %s' % func), e)", "response": "Return the first item of the iterable for which func returns True."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef iterslice(iterable, start=0, stop=None, step=1):\n # type: (Iterable[T], int, int, int) -> Iterable[T]\n \"\"\" Like itertools.islice, but accept int and callables.\n\n If `start` is a callable, start the slice after the first time\n start(item) == True.\n\n If `stop` is a callable, stop the slice after the first time\n stop(item) == True.\n \"\"\"\n\n if step < 0:\n raise ValueError(\"The step can not be negative: '%s' given\" % step)\n\n if not isinstance(start, int):\n\n # [Callable:Callable]\n if not isinstance(stop, int) and stop:\n return stops_when(starts_when(iterable, start), stop)\n\n # [Callable:int]\n return starts_when(itertools.islice(iterable, None, stop, step), start)\n\n # [int:Callable]\n if not isinstance(stop, int) and stop:\n return stops_when(itertools.islice(iterable, start, None, step), stop)\n\n # [int:int]\n return itertools.islice(iterable, start, stop, step)", "response": "Like itertools. islice but accept int and callables."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef firsts(iterable, items=1, default=None):\n # type: (Iterable[T], int, T) -> Iterable[T]\n \"\"\" Lazily return the first x items from this iterable or default. \"\"\"\n\n try:\n items = int(items)\n except (ValueError, TypeError):\n raise ValueError(\"items should be usable as an int but is currently \"\n \"'{}' of type '{}'\".format(items, type(items)))\n\n # TODO: replace this so that it returns lasts()\n if items < 0:\n raise ValueError(ww.f(\"items is {items} but should \"\n \"be greater than 0. If you wish to get the last \"\n \"items, use the lasts() function.\"))\n\n i = 0\n for i, item in zip(range(items), iterable):\n yield item\n\n for x in range(items - (i + 1)):\n yield default", "response": "Lazily return the first x items from this iterable or default."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef lasts(iterable, items=1, default=None):\n # type: (Iterable[T], int, T) -> Iterable[T]\n \"\"\" Lazily return the last x items from this iterable or default. \"\"\"\n\n last_items = deque(iterable, maxlen=items)\n\n for _ in range(items - len(last_items)):\n yield default\n\n for y in last_items:\n yield y", "response": "Lazily return the last x items from this iterable or default."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndetermine execution mode. Legacy mode : <= v4. 20171215", "response": "def is_legacy_server():\n '''Determine execution mode.\n\n Legacy mode: <= v4.20181215\n '''\n with Session() as session:\n ret = session.Kernel.hello()\n bai_version = ret['version']\n legacy = True if bai_version <= 'v4.20181215' else False\n return legacy"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef close(self):\n '''\n Terminates the session. It schedules the ``close()`` coroutine\n of the underlying aiohttp session and then enqueues a sentinel\n object to indicate termination. Then it waits until the worker\n thread to self-terminate by joining.\n '''\n if self._closed:\n return\n self._closed = True\n self._worker_thread.work_queue.put(self.aiohttp_session.close())\n self._worker_thread.work_queue.put(self.worker_thread.sentinel)\n self._worker_thread.join()", "response": "Closes the session and waits until the worker thread is self - terminated."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef asyncio_run_forever(setup_coro, shutdown_coro, *,\n stop_signals={signal.SIGINT}, debug=False):\n '''\n A proposed-but-not-implemented asyncio.run_forever() API based on\n @vxgmichel's idea.\n See discussions on https://github.com/python/asyncio/pull/465\n '''\n async def wait_for_stop():\n loop = current_loop()\n future = loop.create_future()\n for stop_sig in stop_signals:\n loop.add_signal_handler(stop_sig, future.set_result, stop_sig)\n try:\n recv_sig = await future\n finally:\n loop.remove_signal_handler(recv_sig)\n\n loop = asyncio.new_event_loop()\n try:\n asyncio.set_event_loop(loop)\n loop.set_debug(debug)\n loop.run_until_complete(setup_coro)\n loop.run_until_complete(wait_for_stop())\n finally:\n try:\n loop.run_until_complete(shutdown_coro)\n _cancel_all_tasks(loop)\n if hasattr(loop, 'shutdown_asyncgens'): # Python 3.6+\n loop.run_until_complete(loop.shutdown_asyncgens())\n finally:\n asyncio.set_event_loop(None)\n loop.close()", "response": "A proposed - but - not - implemented asyncio. run_forever API based on asyncio. run_forever."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef clear(self):\n\t\t'''\n\t\tclaar all the cache, and release memory\n\t\t'''\n\t\tfor node in self.dli():\n\t\t\tnode.empty = True\n\t\t\tnode.key = None\n\t\t\tnode.value = None\n\t\t\n\t\tself.head = _dlnode()\n\t\tself.head.next = self.head\n\t\tself.head.prev = self.head\n\t\tself.listSize = 1\n\t\t\n\t\tself.table.clear()\n\t\t\n\t\t# status var\n\t\tself.hit_cnt = 0 \n\t\tself.miss_cnt = 0\n\t\tself.remove_cnt = 0", "response": "clear all the cache and release memory"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete the item with the given key. If the key is not present in the dictionary default is returned.", "response": "def pop(self, key, default = None):\n\t\t\"\"\"Delete the item\"\"\"\n\t\tnode = self.get(key, None)\n\t\t\n\t\tif node == None:\n\t\t\tvalue = default\n\t\telse:\n\t\t\tvalue = node\n\t\ttry:\n\t\t\tdel self[key]\n\t\texcept:\n\t\t\treturn value\n\t\treturn value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef main():\n try:\n # Get the `src` directory's absolute path\n src_path = os.path.dirname(\n # `aoiklivereload` directory's absolute path\n os.path.dirname(\n # `demo` directory's absolute path\n os.path.dirname(\n # This file's absolute path\n os.path.abspath(__file__)\n )\n )\n )\n\n # If the `src` directory path is not in `sys.path`\n if src_path not in sys.path:\n # Add to `sys.path`.\n #\n # This aims to save user setting PYTHONPATH when running this demo.\n #\n sys.path.append(src_path)\n\n # Import reloader class\n from aoiklivereload import LiveReloader\n\n # Create reloader\n reloader = LiveReloader()\n\n # Start watcher thread\n reloader.start_watcher_thread()\n\n # Server host\n server_host = '0.0.0.0'\n\n # Server port\n server_port = 8000\n\n # Get message\n msg = '# ----- Run server -----\\nHost: {}\\nPort: {}'.format(\n server_host, server_port\n )\n\n # Print message\n print(msg)\n\n # Create request handler\n @bottle.get('/')\n def hello_handler(): # pylint: disable=unused-variable\n \"\"\"\n Request handler.\n\n :return:\n Response body.\n \"\"\"\n # Return response body\n return 'hello'\n\n # Run server\n bottle.run(host=server_host, port=server_port)\n\n # If have `KeyboardInterrupt`\n except KeyboardInterrupt:\n # Not treat as error\n pass", "response": "Main function.\n\n :return:\n None."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets or create a compute session.", "response": "async def get_or_create(cls, lang: str, *,\n client_token: str = None,\n mounts: Iterable[str] = None,\n envs: Mapping[str, str] = None,\n resources: Mapping[str, int] = None,\n cluster_size: int = 1,\n tag: str = None,\n owner_access_key: str = None) -> 'Kernel':\n '''\n Get-or-creates a compute session.\n If *client_token* is ``None``, it creates a new compute session as long as\n the server has enough resources and your API key has remaining quota.\n If *client_token* is a valid string and there is an existing compute session\n with the same token and the same *lang*, then it returns the :class:`Kernel`\n instance representing the existing session.\n\n :param lang: The image name and tag for the compute session.\n Example: ``python:3.6-ubuntu``.\n Check out the full list of available images in your server using (TODO:\n new API).\n :param client_token: A client-side identifier to seamlessly reuse the compute\n session already created.\n :param mounts: The list of vfolder names that belongs to the currrent API\n access key.\n :param envs: The environment variables which always bypasses the jail policy.\n :param resources: The resource specification. (TODO: details)\n :param cluster_size: The number of containers in this compute session.\n Must be at least 1.\n :param tag: An optional string to annotate extra information.\n :param owner: An optional access key that owns the created session. (Only\n available to administrators)\n\n :returns: The :class:`Kernel` instance.\n '''\n if client_token:\n assert 4 <= len(client_token) <= 64, \\\n 'Client session token should be 4 to 64 characters long.'\n else:\n client_token = uuid.uuid4().hex\n if mounts is None:\n mounts = []\n if resources is None:\n resources = {}\n mounts.extend(cls.session.config.vfolder_mounts)\n rqst = Request(cls.session, 'POST', '/kernel/create')\n rqst.set_json({\n 'lang': lang,\n 'tag': tag,\n 'clientSessionToken': client_token,\n 'config': {\n 'mounts': mounts,\n 'environ': envs,\n 'clusterSize': cluster_size,\n 'resources': resources,\n },\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n o = cls(data['kernelId'], owner_access_key) # type: ignore\n o.created = data.get('created', True) # True is for legacy\n o.service_ports = data.get('servicePorts', [])\n return o"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def destroy(self):\n '''\n Destroys the compute session.\n Since the server literally kills the container(s), all ongoing executions are\n forcibly interrupted.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'DELETE', '/kernel/{}'.format(self.kernel_id),\n params=params)\n async with rqst.fetch() as resp:\n if resp.status == 200:\n return await resp.json()", "response": "Destroys the compute session."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def restart(self):\n '''\n Restarts the compute session.\n The server force-destroys the current running container(s), but keeps their\n temporary scratch directories intact.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'PATCH', '/kernel/{}'.format(self.kernel_id),\n params=params)\n async with rqst.fetch():\n pass", "response": "Restarts the compute session."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the auto - completion candidates from the given code string.", "response": "async def complete(self, code: str, opts: dict = None) -> Iterable[str]:\n '''\n Gets the auto-completion candidates from the given code string,\n as if a user has pressed the tab key just after the code in\n IDEs.\n\n Depending on the language of the compute session, this feature\n may not be supported. Unsupported sessions returns an empty list.\n\n :param code: An (incomplete) code text.\n :param opts: Additional information about the current cursor position,\n such as row, col, line and the remainder text.\n\n :returns: An ordered list of strings.\n '''\n opts = {} if opts is None else opts\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'POST', '/kernel/{}/complete'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'code': code,\n 'options': {\n 'row': int(opts.get('row', 0)),\n 'col': int(opts.get('col', 0)),\n 'line': opts.get('line', ''),\n 'post': opts.get('post', ''),\n },\n })\n async with rqst.fetch() as resp:\n return await resp.json()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nretrieve a brief information about the compute session.", "response": "async def get_info(self):\n '''\n Retrieves a brief information about the compute session.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'GET', '/kernel/{}'.format(self.kernel_id),\n params=params)\n async with rqst.fetch() as resp:\n return await resp.json()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def execute(self, run_id: str = None,\n code: str = None,\n mode: str = 'query',\n opts: dict = None):\n '''\n Executes a code snippet directly in the compute session or sends a set of\n build/clean/execute commands to the compute session.\n\n For more details about using this API, please refer :doc:`the official API\n documentation `.\n\n :param run_id: A unique identifier for a particular run loop. In the\n first call, it may be ``None`` so that the server auto-assigns one.\n Subsequent calls must use the returned ``runId`` value to request\n continuation or to send user inputs.\n :param code: A code snippet as string. In the continuation requests, it\n must be an empty string. When sending user inputs, this is where the\n user input string is stored.\n :param mode: A constant string which is one of ``\"query\"``, ``\"batch\"``,\n ``\"continue\"``, and ``\"user-input\"``.\n :param opts: A dict for specifying additional options. Mainly used in the\n batch mode to specify build/clean/execution commands.\n See :ref:`the API object reference `\n for details.\n\n :returns: :ref:`An execution result object `\n '''\n opts = opts if opts is not None else {}\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n if mode in {'query', 'continue', 'input'}:\n assert code is not None, \\\n 'The code argument must be a valid string even when empty.'\n rqst = Request(self.session,\n 'POST', '/kernel/{}'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'mode': mode,\n 'code': code,\n 'runId': run_id,\n })\n elif mode == 'batch':\n rqst = Request(self.session,\n 'POST', '/kernel/{}'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'mode': mode,\n 'code': code,\n 'runId': run_id,\n 'options': {\n 'clean': opts.get('clean', None),\n 'build': opts.get('build', None),\n 'buildLog': bool(opts.get('buildLog', False)),\n 'exec': opts.get('exec', None),\n },\n })\n elif mode == 'complete':\n rqst = Request(self.session,\n 'POST', '/kernel/{}/complete'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'code': code,\n 'options': {\n 'row': int(opts.get('row', 0)),\n 'col': int(opts.get('col', 0)),\n 'line': opts.get('line', ''),\n 'post': opts.get('post', ''),\n },\n })\n else:\n raise BackendClientError('Invalid execution mode: {0}'.format(mode))\n async with rqst.fetch() as resp:\n return (await resp.json())['result']", "response": "Executes a code snippet directly in the compute session or sends a set of build - clean - execute commands to the compute session."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def upload(self, files: Sequence[Union[str, Path]],\n basedir: Union[str, Path] = None,\n show_progress: bool = False):\n '''\n Uploads the given list of files to the compute session.\n You may refer them in the batch-mode execution or from the code\n executed in the server afterwards.\n\n :param files: The list of file paths in the client-side.\n If the paths include directories, the location of them in the compute\n session is calculated from the relative path to *basedir* and all\n intermediate parent directories are automatically created if not exists.\n\n For example, if a file path is ``/home/user/test/data.txt`` (or\n ``test/data.txt``) where *basedir* is ``/home/user`` (or the current\n working directory is ``/home/user``), the uploaded file is located at\n ``/home/work/test/data.txt`` in the compute session container.\n :param basedir: The directory prefix where the files reside.\n The default value is the current working directory.\n :param show_progress: Displays a progress bar during uploads.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n base_path = (Path.cwd() if basedir is None\n else Path(basedir).resolve())\n files = [Path(file).resolve() for file in files]\n total_size = 0\n for file_path in files:\n total_size += file_path.stat().st_size\n tqdm_obj = tqdm(desc='Uploading files',\n unit='bytes', unit_scale=True,\n total=total_size,\n disable=not show_progress)\n with tqdm_obj:\n attachments = []\n for file_path in files:\n try:\n attachments.append(AttachedFile(\n str(file_path.relative_to(base_path)),\n ProgressReportingReader(str(file_path),\n tqdm_instance=tqdm_obj),\n 'application/octet-stream',\n ))\n except ValueError:\n msg = 'File \"{0}\" is outside of the base directory \"{1}\".' \\\n .format(file_path, base_path)\n raise ValueError(msg) from None\n\n rqst = Request(self.session,\n 'POST', '/kernel/{}/upload'.format(self.kernel_id),\n params=params)\n rqst.attach_files(attachments)\n async with rqst.fetch() as resp:\n return resp", "response": "Uploads the given list of files to the compute session container."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def download(self, files: Sequence[Union[str, Path]],\n dest: Union[str, Path] = '.',\n show_progress: bool = False):\n '''\n Downloads the given list of files from the compute session.\n\n :param files: The list of file paths in the compute session.\n If they are relative paths, the path is calculated from\n ``/home/work`` in the compute session container.\n :param dest: The destination directory in the client-side.\n :param show_progress: Displays a progress bar during downloads.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'GET', '/kernel/{}/download'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'files': [*map(str, files)],\n })\n async with rqst.fetch() as resp:\n chunk_size = 1 * 1024\n file_names = None\n tqdm_obj = tqdm(desc='Downloading files',\n unit='bytes', unit_scale=True,\n total=resp.content.total_bytes,\n disable=not show_progress)\n with tqdm_obj as pbar:\n fp = None\n while True:\n chunk = await resp.aread(chunk_size)\n if not chunk:\n break\n pbar.update(len(chunk))\n # TODO: more elegant parsing of multipart response?\n for part in chunk.split(b'\\r\\n'):\n if part.startswith(b'--'):\n if fp:\n fp.close()\n with tarfile.open(fp.name) as tarf:\n tarf.extractall(path=dest)\n file_names = tarf.getnames()\n os.unlink(fp.name)\n fp = tempfile.NamedTemporaryFile(suffix='.tar',\n delete=False)\n elif part.startswith(b'Content-') or part == b'':\n continue\n else:\n fp.write(part)\n if fp:\n fp.close()\n os.unlink(fp.name)\n result = {'file_names': file_names}\n return result", "response": "Downloads the given list of files from the compute session container."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def list_files(self, path: Union[str, Path] = '.'):\n '''\n Gets the list of files in the given path inside the compute session\n container.\n\n :param path: The directory path in the compute session.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n rqst = Request(self.session,\n 'GET', '/kernel/{}/files'.format(self.kernel_id),\n params=params)\n rqst.set_json({\n 'path': path,\n })\n async with rqst.fetch() as resp:\n return await resp.json()", "response": "Gets the list of files in the given path inside the compute session\n container."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nopen a pseudo - terminal of the kernel.", "response": "def stream_pty(self) -> 'StreamPty':\n '''\n Opens a pseudo-terminal of the kernel (if supported) streamed via\n websockets.\n\n :returns: a :class:`StreamPty` object.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n request = Request(self.session,\n 'GET', '/stream/kernel/{}/pty'.format(self.kernel_id),\n params=params)\n return request.connect_websocket(response_cls=StreamPty)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nexecuting a code snippet in the streaming mode.", "response": "def stream_execute(self, code: str = '', *,\n mode: str = 'query',\n opts: dict = None) -> WebSocketResponse:\n '''\n Executes a code snippet in the streaming mode.\n Since the returned websocket represents a run loop, there is no need to\n specify *run_id* explicitly.\n '''\n params = {}\n if self.owner_access_key:\n params['owner_access_key'] = self.owner_access_key\n opts = {} if opts is None else opts\n if mode == 'query':\n opts = {}\n elif mode == 'batch':\n opts = {\n 'clean': opts.get('clean', None),\n 'build': opts.get('build', None),\n 'buildLog': bool(opts.get('buildLog', False)),\n 'exec': opts.get('exec', None),\n }\n else:\n msg = 'Invalid stream-execution mode: {0}'.format(mode)\n raise BackendClientError(msg)\n request = Request(self.session,\n 'GET', '/stream/kernel/{}/execute'.format(self.kernel_id),\n params=params)\n\n async def send_code(ws):\n await ws.send_json({\n 'code': code,\n 'mode': mode,\n 'options': opts,\n })\n\n return request.connect_websocket(on_enter=send_code)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _connect(self, mqttc, userdata, flags, rc):\n if rc == 0:\n self.connected = True\n results = []\n LOGGER.info(\"MQTT Connected with result code \" + str(rc) + \" (Success)\")\n # result, mid = self._mqttc.subscribe(self.topicInput)\n results.append((self.topicInput, tuple(self._mqttc.subscribe(self.topicInput))))\n results.append((self.topicPolyglotConnection, tuple(self._mqttc.subscribe(self.topicPolyglotConnection))))\n for (topic, (result, mid)) in results:\n if result == 0:\n LOGGER.info(\"MQTT Subscribing to topic: \" + topic + \" - \" + \" MID: \" + str(mid) + \" Result: \" + str(result))\n else:\n LOGGER.info(\"MQTT Subscription to \" + topic + \" failed. This is unusual. MID: \" + str(mid) + \" Result: \" + str(result))\n # If subscription fails, try to reconnect.\n self._mqttc.reconnect()\n self._mqttc.publish(self.topicSelfConnection, json.dumps(\n {\n 'connected': True,\n 'node': self.profileNum\n }), retain=True)\n LOGGER.info('Sent Connected message to Polyglot')\n else:\n LOGGER.error(\"MQTT Failed to connect. Result code: \" + str(rc))", "response": "Callback for when the client receives a CONNACK response from the server."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _message(self, mqttc, userdata, msg):\n try:\n inputCmds = ['query', 'command', 'result', 'status', 'shortPoll', 'longPoll', 'delete']\n parsed_msg = json.loads(msg.payload.decode('utf-8'))\n if 'node' in parsed_msg:\n if parsed_msg['node'] != 'polyglot':\n return\n del parsed_msg['node']\n for key in parsed_msg:\n # LOGGER.debug('MQTT Received Message: {}: {}'.format(msg.topic, parsed_msg))\n if key == 'config':\n self.inConfig(parsed_msg[key])\n elif key == 'connected':\n self.polyglotConnected = parsed_msg[key]\n elif key == 'stop':\n LOGGER.debug('Received stop from Polyglot... Shutting Down.')\n self.stop()\n elif key in inputCmds:\n self.input(parsed_msg)\n else:\n LOGGER.error('Invalid command received in message from Polyglot: {}'.format(key))\n\n except (ValueError) as err:\n LOGGER.error('MQTT Received Payload Error: {}'.format(err), exc_info=True)", "response": "This callback is called when a PUBLISH message is received from the server."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _disconnect(self, mqttc, userdata, rc):\n self.connected = False\n if rc != 0:\n LOGGER.info(\"MQTT Unexpected disconnection. Trying reconnect.\")\n try:\n self._mqttc.reconnect()\n except Exception as ex:\n template = \"An exception of type {0} occured. Arguments:\\n{1!r}\"\n message = template.format(type(ex).__name__, ex.args)\n LOGGER.error(\"MQTT Connection error: \" + message)\n else:\n LOGGER.info(\"MQTT Graceful disconnection.\")", "response": "Callback for when a DISCONNECT occurs."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _startMqtt(self):\n LOGGER.info('Connecting to MQTT... {}:{}'.format(self._server, self._port))\n try:\n # self._mqttc.connect_async(str(self._server), int(self._port), 10)\n self._mqttc.connect_async('{}'.format(self._server), int(self._port), 10)\n self._mqttc.loop_forever()\n except Exception as ex:\n template = \"An exception of type {0} occurred. Arguments:\\n{1!r}\"\n message = template.format(type(ex).__name__, ex.args)\n LOGGER.error(\"MQTT Connection error: {}\".format(message), exc_info=True)", "response": "Starts the MQTT client thread."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef stop(self):\n # self.loop.call_soon_threadsafe(self.loop.stop)\n # self.loop.stop()\n # self._longPoll.cancel()\n # self._shortPoll.cancel()\n if self.connected:\n LOGGER.info('Disconnecting from MQTT... {}:{}'.format(self._server, self._port))\n self._mqttc.publish(self.topicSelfConnection, json.dumps({'node': self.profileNum, 'connected': False}), retain=True)\n self._mqttc.loop_stop()\n self._mqttc.disconnect()\n try:\n for watcher in self.__stopObservers:\n watcher()\n except KeyError as e:\n LOGGER.exception('KeyError in gotConfig: {}'.format(e), exc_info=True)", "response": "Stop the client and disconnect the broker if clean shutdown."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsends a message to Polyglot.", "response": "def send(self, message):\n \"\"\"\n Formatted Message to send to Polyglot. Connection messages are sent automatically from this module\n so this method is used to send commands to/from Polyglot and formats it for consumption\n \"\"\"\n if not isinstance(message, dict) and self.connected:\n warnings.warn('payload not a dictionary')\n return False\n try:\n message['node'] = self.profileNum\n self._mqttc.publish(self.topicInput, json.dumps(message), retain=False)\n except TypeError as err:\n LOGGER.error('MQTT Send Error: {}'.format(err), exc_info=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef addNode(self, node):\n LOGGER.info('Adding node {}({})'.format(node.name, node.address))\n message = {\n 'addnode': {\n 'nodes': [{\n 'address': node.address,\n 'name': node.name,\n 'node_def_id': node.id,\n 'primary': node.primary,\n 'drivers': node.drivers,\n 'hint': node.hint\n }]\n }\n }\n self.send(message)", "response": "Adds a node to the NodeServer"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend custom dictionary to Polyglot to save and retrieve on startup.", "response": "def saveCustomData(self, data):\n \"\"\"\n Send custom dictionary to Polyglot to save and be retrieved on startup.\n\n :param data: Dictionary of key value pairs to store in Polyglot database.\n \"\"\"\n LOGGER.info('Sending customData to Polyglot.')\n message = { 'customdata': data }\n self.send(message)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsend custom dictionary to Polyglot to save and retrieve on startup.", "response": "def saveCustomParams(self, data):\n \"\"\"\n Send custom dictionary to Polyglot to save and be retrieved on startup.\n\n :param data: Dictionary of key value pairs to store in Polyglot database.\n \"\"\"\n LOGGER.info('Sending customParams to Polyglot.')\n message = { 'customparams': data }\n self.send(message)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding custom notice to Polyglot", "response": "def addNotice(self, data):\n \"\"\"\n Add custom notice to front-end for this NodeServers\n\n :param data: String of characters to add as a notification in the front-end.\n \"\"\"\n LOGGER.info('Sending addnotice to Polyglot: {}'.format(data))\n message = { 'addnotice': data }\n self.send(message)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef removeNotice(self, data):\n LOGGER.info('Sending removenotice to Polyglot for index {}'.format(data))\n message = { 'removenotice': data }\n self.send(message)", "response": "Add custom notice to front - end for this node"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delNode(self, address):\n LOGGER.info('Removing node {}'.format(address))\n message = {\n 'removenode': {\n 'address': address\n }\n }\n self.send(message)", "response": "Delete a node from the NodeServer"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getNode(self, address):\n try:\n for node in self.config['nodes']:\n if node['address'] == address:\n return node\n return False\n except KeyError:\n LOGGER.error('Usually means we have not received the config yet.', exc_info=True)\n return False", "response": "Get Node by Address of existing nodes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef inConfig(self, config):\n self.config = config\n self.isyVersion = config['isyVersion']\n try:\n for watcher in self.__configObservers:\n watcher(config)\n\n self.send_custom_config_docs()\n\n except KeyError as e:\n LOGGER.error('KeyError in gotConfig: {}'.format(e), exc_info=True)", "response": "Called by Polyglot when a new config is received from Polyglot."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef save_typed_params(self, data):\n LOGGER.info('Sending typed parameters to Polyglot.')\n if type(data) is not list:\n data = [ data ]\n message = { 'typedparams': data }\n self.send(message)", "response": "Send typed parameters to Polyglot"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delNode(self, address):\n if address in self.nodes:\n del self.nodes[address]\n self.poly.delNode(address)", "response": "Delete a node from the node list."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nqueries the database for the specified key - value dictionary.", "response": "async def query(cls, query: str,\n variables: Optional[Mapping[str, Any]] = None,\n ) -> Any:\n '''\n Sends the GraphQL query and returns the response.\n\n :param query: The GraphQL query string.\n :param variables: An optional key-value dictionary\n to fill the interpolated template variables\n in the query.\n\n :returns: The object parsed from the response JSON string.\n '''\n gql_query = {\n 'query': query,\n 'variables': variables if variables else {},\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json(gql_query)\n async with rqst.fetch() as resp:\n return await resp.json()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning human readable string from number of seconds", "response": "def get_readable_time_string(seconds):\n \"\"\"Returns human readable string from number of seconds\"\"\"\n seconds = int(seconds)\n minutes = seconds // 60\n seconds = seconds % 60\n hours = minutes // 60\n minutes = minutes % 60\n days = hours // 24\n hours = hours % 24\n\n result = \"\"\n if days > 0:\n result += \"%d %s \" % (days, \"Day\" if (days == 1) else \"Days\")\n if hours > 0:\n result += \"%d %s \" % (hours, \"Hour\" if (hours == 1) else \"Hours\")\n if minutes > 0:\n result += \"%d %s \" % (minutes, \"Minute\" if (minutes == 1) else \"Minutes\")\n if seconds > 0:\n result += \"%d %s \" % (seconds, \"Second\" if (seconds == 1) else \"Seconds\")\n\n return result.strip()"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of rate limit information from a given response s headers.", "response": "def get_rate_limits(response):\n \"\"\"Returns a list of rate limit information from a given response's headers.\"\"\"\n periods = response.headers['X-RateLimit-Period']\n if not periods:\n return []\n\n rate_limits = []\n\n periods = periods.split(',')\n limits = response.headers['X-RateLimit-Limit'].split(',')\n remaining = response.headers['X-RateLimit-Remaining'].split(',')\n reset = response.headers['X-RateLimit-Reset'].split(',')\n\n for idx, period in enumerate(periods):\n rate_limit = {}\n limit_period = get_readable_time_string(period)\n rate_limit[\"period\"] = limit_period\n rate_limit[\"period_seconds\"] = period\n rate_limit[\"request_limit\"] = limits[idx]\n rate_limit[\"requests_remaining\"] = remaining[idx]\n\n reset_datetime = get_datetime_from_timestamp(reset[idx])\n rate_limit[\"reset\"] = reset_datetime\n\n right_now = datetime.now()\n if (reset_datetime is not None) and (right_now < reset_datetime):\n # add 1 second because of rounding\n seconds_remaining = (reset_datetime - right_now).seconds + 1\n else:\n seconds_remaining = 0\n\n rate_limit[\"reset_in_seconds\"] = seconds_remaining\n\n rate_limit[\"time_to_reset\"] = get_readable_time_string(seconds_remaining)\n rate_limits.append(rate_limit)\n\n return rate_limits"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef isubset(self, *keys):\n # type: (*Hashable) -> ww.g\n \"\"\"Return key, self[key] as generator for key in keys.\n\n Raise KeyError if a key does not exist\n\n Args:\n keys: Iterable containing keys\n\n Example:\n\n >>> from ww import d\n >>> list(d({1: 1, 2: 2, 3: 3}).isubset(1, 3))\n [(1, 1), (3, 3)]\n \"\"\"\n return ww.g((key, self[key]) for key in keys)", "response": "Return key as generator for key in keys Raise KeyError if a key does not exist\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef swap(self):\n # type: () -> DictWrapper\n \"\"\"Swap key and value\n\n /!\\ Be carreful, if there are duplicate values, only one will\n survive /!\\\n\n Example:\n\n >>> from ww import d\n >>> d({1: 2, 2: 2, 3: 3}).swap()\n {2: 2, 3: 3}\n \"\"\"\n return self.__class__((v, k) for k, v in self.items())", "response": "Swap key and value of a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fromkeys(cls, iterable, value=None):\n # TODO : type: (Iterable, Union[Any, Callable]) -> DictWrapper\n # https://github.com/python/mypy/issues/2254\n \"\"\"Create a new d from\n\n Args:\n iterable: Iterable containing keys\n value: value to associate with each key.\n If callable, will be value[key]\n\n Returns: new DictWrapper\n\n Example:\n\n >>> from ww import d\n >>> sorted(d.fromkeys('123', value=4).items())\n [('1', 4), ('2', 4), ('3', 4)]\n >>> sorted(d.fromkeys(range(3), value=lambda e:e**2).items())\n [(0, 0), (1, 1), (2, 4)]\n \"\"\"\n if not callable(value):\n return cls(dict.fromkeys(iterable, value))\n\n return cls((key, value(key)) for key in iterable)", "response": "Create a new DictWrapper from a list of keys and a value."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef main():\n try:\n # Get the `src` directory's absolute path\n src_path = os.path.dirname(\n # `aoiklivereload` directory's absolute path\n os.path.dirname(\n # `demo` directory's absolute path\n os.path.dirname(\n # This file's absolute path\n os.path.abspath(__file__)\n )\n )\n )\n\n # If the `src` directory path is not in `sys.path`\n if src_path not in sys.path:\n # Add to `sys.path`.\n #\n # This aims to save user setting PYTHONPATH when running this demo.\n #\n sys.path.append(src_path)\n\n # Import reloader class\n from aoiklivereload import LiveReloader\n\n # Create reloader\n reloader = LiveReloader()\n\n # Start watcher thread\n reloader.start_watcher_thread()\n\n # Server host\n server_host = '0.0.0.0'\n\n # Server port\n server_port = 8000\n\n # Get message\n msg = '# ----- Run server -----\\nHost: {}\\nPort: {}'.format(\n server_host, server_port\n )\n\n # Print message\n print(msg)\n\n # Create Flask app\n flask_app = flask.Flask(__name__)\n\n # Create request handler\n @flask_app.route('/')\n def hello_handler(): # pylint: disable=unused-variable\n \"\"\"\n Request handler.\n\n :return:\n Response body.\n \"\"\"\n # Return response body\n return 'hello'\n\n # Run server\n flask_app.run(\n host=server_host,\n port=server_port,\n # Disable Flask's reloader\n debug=False,\n )\n\n # If have `KeyboardInterrupt`\n except KeyboardInterrupt:\n # Not treat as error\n pass", "response": "Main function.\n\n :return:\n None."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngenerating an Excel workbook object given api_data", "response": "def get_excel_workbook(api_data, result_info_key, identifier_keys):\n \"\"\"Generates an Excel workbook object given api_data returned by the Analytics API\n\n Args:\n api_data: Analytics API data as a list of dicts (one per identifier)\n result_info_key: the key in api_data dicts that contains the data results\n identifier_keys: the list of keys used as requested identifiers\n (address, zipcode, block_id, etc)\n\n Returns:\n raw excel file data\n \"\"\"\n\n cleaned_data = []\n\n for item_data in api_data:\n result_info = item_data.pop(result_info_key, {})\n\n cleaned_item_data = {}\n\n if 'meta' in item_data:\n meta = item_data.pop('meta')\n cleaned_item_data['meta'] = meta\n\n for key in item_data:\n cleaned_item_data[key] = item_data[key]['result']\n\n cleaned_item_data[result_info_key] = result_info\n\n cleaned_data.append(cleaned_item_data)\n\n data_list = copy.deepcopy(cleaned_data)\n\n workbook = openpyxl.Workbook()\n\n write_worksheets(workbook, data_list, result_info_key, identifier_keys)\n\n return workbook"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwrite rest of the worksheets into the current workbook.", "response": "def write_worksheets(workbook, data_list, result_info_key, identifier_keys):\n \"\"\"Writes rest of the worksheets to workbook.\n\n Args:\n workbook: workbook to write into\n data_list: Analytics API data as a list of dicts\n result_info_key: the key in api_data dicts that contains the data results\n identifier_keys: the list of keys used as requested identifiers\n (address, zipcode, block_id, etc)\n \"\"\"\n\n # we can use the first item to figure out the worksheet keys\n worksheet_keys = get_worksheet_keys(data_list[0], result_info_key)\n\n for key in worksheet_keys:\n\n title = key.split('/')[1]\n\n title = utilities.convert_snake_to_title_case(title)\n\n title = KEY_TO_WORKSHEET_MAP.get(title, title)\n\n if key == 'property/nod':\n # the property/nod endpoint needs to be split into two worksheets\n create_property_nod_worksheets(workbook, data_list, result_info_key, identifier_keys)\n else:\n # all other endpoints are written to a single worksheet\n\n # Maximum 31 characters allowed in sheet title\n worksheet = workbook.create_sheet(title=title[:31])\n\n processed_data = process_data(key, data_list, result_info_key, identifier_keys)\n\n write_data(worksheet, processed_data)\n\n # remove the first, unused empty sheet\n workbook.remove_sheet(workbook.active)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_property_nod_worksheets(workbook, data_list, result_info_key, identifier_keys):\n nod_details_list = []\n nod_default_history_list = []\n\n for prop_data in data_list:\n nod_data = prop_data['property/nod']\n\n if nod_data is None:\n nod_data = {}\n\n default_history_data = nod_data.pop('default_history', [])\n\n _set_identifier_fields(nod_data, prop_data, result_info_key, identifier_keys)\n\n nod_details_list.append(nod_data)\n\n for item in default_history_data:\n _set_identifier_fields(item, prop_data, result_info_key, identifier_keys)\n nod_default_history_list.append(item)\n\n worksheet = workbook.create_sheet(title='NOD Details')\n write_data(worksheet, nod_details_list)\n\n worksheet = workbook.create_sheet(title='NOD Default History')\n write_data(worksheet, nod_default_history_list)", "response": "Creates two worksheets out of the property and nod data."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting sorted keys from the dict ignoring result_info_key and meta keys", "response": "def get_worksheet_keys(data_dict, result_info_key):\n \"\"\"Gets sorted keys from the dict, ignoring result_info_key and 'meta' key\n Args:\n data_dict: dict to pull keys from\n\n Returns:\n list of keys in the dict other than the result_info_key\n \"\"\"\n keys = set(data_dict.keys())\n keys.remove(result_info_key)\n if 'meta' in keys:\n keys.remove('meta')\n return sorted(keys)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all possible keys from a list of dicts sorting by leading_columns first", "response": "def get_keys(data_list, leading_columns=LEADING_COLUMNS):\n \"\"\"Gets all possible keys from a list of dicts, sorting by leading_columns first\n\n Args:\n data_list: list of dicts to pull keys from\n leading_columns: list of keys to put first in the result\n\n Returns:\n list of keys to be included as columns in excel worksheet\n \"\"\"\n all_keys = set().union(*(list(d.keys()) for d in data_list))\n\n leading_keys = []\n\n for key in leading_columns:\n if key not in all_keys:\n continue\n leading_keys.append(key)\n all_keys.remove(key)\n\n return leading_keys + sorted(all_keys)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a key in api_data dicts pulls the data for that endpoint out of the data_list for each address and processes the data into a more specific format and returns the data.", "response": "def process_data(key, data_list, result_info_key, identifier_keys):\n \"\"\" Given a key as the endpoint name, pulls the data for that endpoint out\n of the data_list for each address, processes the data into a more\n excel-friendly format and returns that data.\n\n Args:\n key: the endpoint name of the data to process\n data_list: the main data list to take the data from\n result_info_key: the key in api_data dicts that contains the data results\n identifier_keys: the list of keys used as requested identifiers\n (address, zipcode, block_id, etc)\n\n Returns:\n A list of dicts (rows) to be written to a worksheet\n \"\"\"\n master_data = []\n\n for item_data in data_list:\n data = item_data[key]\n\n if data is None:\n current_item_data = {}\n else:\n if key == 'property/value':\n current_item_data = data['value']\n\n elif key == 'property/details':\n top_level_keys = ['property', 'assessment']\n current_item_data = flatten_top_level_keys(data, top_level_keys)\n\n elif key == 'property/school':\n current_item_data = data['school']\n\n school_list = []\n for school_type_key in current_item_data:\n schools = current_item_data[school_type_key]\n for school in schools:\n school['school_type'] = school_type_key\n school['school_address'] = school['address']\n school['school_zipcode'] = school['zipcode']\n school_list.append(school)\n\n current_item_data = school_list\n\n elif key == 'property/value_forecast':\n current_item_data = {}\n for month_key in data:\n current_item_data[month_key] = data[month_key]['value']\n\n elif key in ['property/value_within_block', 'property/rental_value_within_block']:\n current_item_data = flatten_top_level_keys(data, [\n 'housecanary_value_percentile_range',\n 'housecanary_value_sqft_percentile_range',\n 'client_value_percentile_range',\n 'client_value_sqft_percentile_range'\n ])\n\n elif key in ['property/zip_details', 'zip/details']:\n top_level_keys = ['multi_family', 'single_family']\n current_item_data = flatten_top_level_keys(data, top_level_keys)\n\n else:\n current_item_data = data\n\n if isinstance(current_item_data, dict):\n _set_identifier_fields(current_item_data, item_data, result_info_key, identifier_keys)\n\n master_data.append(current_item_data)\n else:\n # it's a list\n for item in current_item_data:\n _set_identifier_fields(item, item_data, result_info_key, identifier_keys)\n\n master_data.extend(current_item_data)\n\n return master_data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef check_database_connected(app_configs, **kwargs):\n errors = []\n\n try:\n connection.ensure_connection()\n except OperationalError as e:\n msg = 'Could not connect to database: {!s}'.format(e)\n errors.append(checks.Error(msg,\n id=health.ERROR_CANNOT_CONNECT_DATABASE))\n except ImproperlyConfigured as e:\n msg = 'Datbase misconfigured: \"{!s}\"'.format(e)\n errors.append(checks.Error(msg,\n id=health.ERROR_MISCONFIGURED_DATABASE))\n else:\n if not connection.is_usable():\n errors.append(checks.Error('Database connection is not usable',\n id=health.ERROR_UNUSABLE_DATABASE))\n\n return errors", "response": "A Django check to see if connecting to the configured default\n database backend succeeds."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef check_migrations_applied(app_configs, **kwargs):\n from django.db.migrations.loader import MigrationLoader\n errors = []\n\n # Load migrations from disk/DB\n try:\n loader = MigrationLoader(connection, ignore_no_migrations=True)\n except (ImproperlyConfigured, ProgrammingError, OperationalError):\n msg = \"Can't connect to database to check migrations\"\n return [checks.Info(msg, id=health.INFO_CANT_CHECK_MIGRATIONS)]\n\n if app_configs:\n app_labels = [app.label for app in app_configs]\n else:\n app_labels = loader.migrated_apps\n\n for node, migration in loader.graph.nodes.items():\n if migration.app_label not in app_labels:\n continue\n if node not in loader.applied_migrations:\n msg = 'Unapplied migration {}'.format(migration)\n # NB: This *must* be a Warning, not an Error, because Errors\n # prevent migrations from being run.\n errors.append(checks.Warning(msg,\n id=health.WARNING_UNAPPLIED_MIGRATION))\n\n return errors", "response": "A Django check to see if all migrations have been applied correctly."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks Redis connection and see if Redis ping is successful.", "response": "def check_redis_connected(app_configs, **kwargs):\n \"\"\"\n A Django check to connect to the default redis connection\n using ``django_redis.get_redis_connection`` and see if Redis\n responds to a ``PING`` command.\n \"\"\"\n import redis\n from django_redis import get_redis_connection\n errors = []\n\n try:\n connection = get_redis_connection('default')\n except redis.ConnectionError as e:\n msg = 'Could not connect to redis: {!s}'.format(e)\n errors.append(checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_REDIS))\n except NotImplementedError as e:\n msg = 'Redis client not available: {!s}'.format(e)\n errors.append(checks.Error(msg, id=health.ERROR_MISSING_REDIS_CLIENT))\n except ImproperlyConfigured as e:\n msg = 'Redis misconfigured: \"{!s}\"'.format(e)\n errors.append(checks.Error(msg, id=health.ERROR_MISCONFIGURED_REDIS))\n else:\n result = connection.ping()\n if not result:\n msg = 'Redis ping failed'\n errors.append(checks.Error(msg, id=health.ERROR_REDIS_PING_FAILED))\n return errors"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfunctions takes in no param as input to fetch Real - Time Alarms from HPE IMC RESTFUL API", "response": "def get_realtime_alarm(username, auth, url):\n \"\"\"Takes in no param as input to fetch RealTime Alarms from HP IMC RESTFUL API\n\n :param username OpeatorName, String type. Required. Default Value \"admin\". Checks the operator\n has the privileges to view the Real-Time Alarms.\n\n :param devId: int or str value of the target device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return:list of dictionaries where each element of the list represents a single alarm as pulled from the the current\n list of realtime alarms in the HPE IMC Platform\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.alarms import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> real_time_alarm = get_realtime_alarm('admin', auth.creds, auth.url)\n\n >>> type(real_time_alarm)\n \n >>> assert 'faultDesc' in real_time_alarm[0]\n\n \"\"\"\n get_realtime_alarm_url = \"/imcrs/fault/faultRealTime?operatorName=\" + username\n f_url = url + get_realtime_alarm_url\n r = requests.get(f_url, auth=auth, headers=headers)\n try:\n realtime_alarm_list = (json.loads(r.text))\n return realtime_alarm_list['faultRealTime']['faultRealTimeList']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_realtime_alarm: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of dicts where each element of the list represents a single alarm.", "response": "def get_alarms(username, auth, url):\n \"\"\"Takes in no param as input to fetch RealTime Alarms from HP IMC RESTFUL API\n\n :param username OpeatorName, String type. Required. Default Value \"admin\". Checks the operator\n has the privileges to view the Real-Time Alarms.\n\n :param devId: int or str value of the target device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return:list of dictionaries where each element of the list represents a single alarm as pulled from the the current\n list of browse alarms in the HPE IMC Platform\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.alarms import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> all_alarms = get_alarms('admin', auth.creds, auth.url)\n\n >>> type(all_alarms)\n \n >>> assert 'ackStatus' in all_alarms[0]\n\n\n [{'OID': '1.3.6.1.4.1.11.2.14.11.15.2.75.5.3.1.6.7',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '1001',\n 'alarmCategoryDesc': 'Wireless Device Alarm',\n 'alarmDesc': 'The device(MAC Address: F4 B7 E2 95 A0 CD) is detected to be flooding the network. Attack Frame Type: Probe Request.',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187616',\n 'alarmLevel': '3',\n 'alarmLevelDesc': 'Minor',\n 'deviceId': '31',\n 'deviceIp': '10.10.10.5',\n 'deviceName': 'HP830_WSC',\n 'faultTime': '1469471298',\n 'faultTimeDesc': '2016-07-25 14:28:18',\n 'holdInfo': '',\n 'id': '187616',\n 'originalType': '1',\n 'originalTypeDesc': 'Trap',\n 'paras': '*Attack MAC= F4 B7 E2 95 A0 CD;Attack Frame Type=Probe Request',\n 'parentId': '0',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '15',\n 'alarmCategoryDesc': 'Other Alarms from NMS',\n 'alarmDesc': 'iMC alarm system has received 100 alarms about the event (hh3cPeriodicalTrap) of device ADP_Initial_Config(10.20.10.10) from 2016-07-25 12:56:01 to 2016-07-25 14:36:01.',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187625',\n 'alarmLevel': '4',\n 'alarmLevelDesc': 'Warning',\n 'deviceId': '0',\n 'deviceIp': '127.0.0.1',\n 'deviceName': 'NMS',\n 'faultTime': '1469471761',\n 'faultTimeDesc': '2016-07-25 14:36:01',\n 'id': '187625',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': 'Start Time=2016-07-25 12:56:01;Stop Time=2016-07-25 14:36:01;Times=100;*Repeat Event Name=hh3cPeriodicalTrap;*Device IP=10.20.10.10;Device Name=ADP_Initial_Config',\n 'parentId': '173953',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '15',\n 'alarmCategoryDesc': 'Other Alarms from NMS',\n 'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient device interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:07:06 to 2016-07-25 14:40:21.',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187626',\n 'alarmLevel': '4',\n 'alarmLevelDesc': 'Warning',\n 'deviceId': '0',\n 'deviceIp': '127.0.0.1',\n 'deviceName': 'NMS',\n 'faultTime': '1469472021',\n 'faultTimeDesc': '2016-07-25 14:40:21',\n 'id': '187626',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': 'Start Time=2016-07-25 14:07:06;Stop Time=2016-07-25 14:40:21;Times=100;*Repeat Event Name=Some ambient device interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',\n 'parentId': '173953',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.3.2.6.11',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '9',\n 'alarmCategoryDesc': 'Traffic Analysis and Audit Alarm',\n 'alarmDesc': 'The in direction of interface GigabitEthernet1/0/23 on device 192.168.1.221 at 2016-07-25 14:32:00 is greater than the baseline(2.87 Mbps).',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187631',\n 'alarmLevel': '2',\n 'alarmLevelDesc': 'Major',\n 'deviceId': '67',\n 'deviceIp': '10.101.0.203',\n 'deviceName': 'HPEIMC72WIN2K12R2',\n 'faultTime': '1469472140',\n 'faultTimeDesc': '2016-07-25 14:42:20',\n 'holdInfo': '',\n 'id': '187631',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': '*UNBA Server Name=NMS;*UNBA Server IP=127.0.0.1;*Alarm Object Name=The in direction of interface GigabitEthernet1/0/23 on device 192.168.1.221;Alarm Occur Time=2016-07-25 14:32:00;Current Flow Speed=48.38 Mbps;Alarm Severity=2;Threshold Speed=2.87 Mbps;Device IP=192.168.1.221;Interface Index=23;Interface Name=GigabitEthernet1/0/23;Flow Direction=1',\n 'parentId': '0',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.3.2.6.11',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '9',\n 'alarmCategoryDesc': 'Traffic Analysis and Audit Alarm',\n 'alarmDesc': 'The out direction of interface GigabitEthernet1/0/2 on device 192.168.1.221 at 2016-07-25 14:32:00 is greater than the baseline(1.78 Mbps).',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187632',\n 'alarmLevel': '2',\n 'alarmLevelDesc': 'Major',\n 'deviceId': '67',\n 'deviceIp': '10.101.0.203',\n 'deviceName': 'HPEIMC72WIN2K12R2',\n 'faultTime': '1469472140',\n 'faultTimeDesc': '2016-07-25 14:42:20',\n 'holdInfo': '',\n 'id': '187632',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': '*UNBA Server Name=NMS;*UNBA Server IP=127.0.0.1;*Alarm Object Name=The out direction of interface GigabitEthernet1/0/2 on device 192.168.1.221;Alarm Occur Time=2016-07-25 14:32:00;Current Flow Speed=48.79 Mbps;Alarm Severity=2;Threshold Speed=1.78 Mbps;Device IP=192.168.1.221;Interface Index=2;Interface Name=GigabitEthernet1/0/2;Flow Direction=0',\n 'parentId': '0',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '15',\n 'alarmCategoryDesc': 'Other Alarms from NMS',\n 'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient AP interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:14:00 to 2016-07-25 14:47:55.',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187637',\n 'alarmLevel': '4',\n 'alarmLevelDesc': 'Warning',\n 'deviceId': '0',\n 'deviceIp': '127.0.0.1',\n 'deviceName': 'NMS',\n 'faultTime': '1469472475',\n 'faultTimeDesc': '2016-07-25 14:47:55',\n 'id': '187637',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': 'Start Time=2016-07-25 14:14:00;Stop Time=2016-07-25 14:47:55;Times=100;*Repeat Event Name=Some ambient AP interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',\n 'parentId': '173953',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'},\n {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',\n 'ackStatus': '0',\n 'ackStatusDesc': 'Unacknowledged',\n 'ackTime': '0',\n 'ackTimeDesc': '',\n 'ackUserName': '',\n 'alarmCategory': '15',\n 'alarmCategoryDesc': 'Other Alarms from NMS',\n 'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient device interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:40:21 to 2016-07-25 15:15:50.',\n 'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187654',\n 'alarmLevel': '4',\n 'alarmLevelDesc': 'Warning',\n 'deviceId': '0',\n 'deviceIp': '127.0.0.1',\n 'deviceName': 'NMS',\n 'faultTime': '1469474150',\n 'faultTimeDesc': '2016-07-25 15:15:50',\n 'id': '187654',\n 'originalType': '3',\n 'originalTypeDesc': 'iMC',\n 'paras': 'Start Time=2016-07-25 14:40:21;Stop Time=2016-07-25 15:15:50;Times=100;*Repeat Event Name=Some ambient device interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',\n 'parentId': '173953',\n 'recStatus': '0',\n 'recStatusDesc': 'Unrecovered',\n 'recTime': '0',\n 'recTimeDesc': '',\n 'recUserName': '',\n 'remark': '',\n 'somState': '0'}]\n \"\"\"\n get_alarms_url = \"/imcrs/fault/alarm?operatorName=\" + username + \"&recStatus=0&ackStatus=0&timeRange=0&size=50&desc=true\"\n f_url = url + get_alarms_url\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n try:\n if r.status_code == 200:\n alarm_list = (json.loads(r.text))\n return alarm_list['alarm']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_alarms: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_dev_alarms(auth, url, devid=None, devip=None):\n # checks to see if the imc credentials are already available\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n f_url = url + \"/imcrs/fault/alarm?operatorName=admin&deviceId=\" + \\\n str(devid) + \"&desc=false\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_alarm = (json.loads(response.text))\n if 'alarm' in dev_alarm:\n return dev_alarm['alarm']\n else:\n return \"Device has no alarms\"\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_dev_alarms: An Error has occured'", "response": "function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for that device."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_realtime_alarm(username, auth, url):\n f_url = url + \"/imcrs/fault/faultRealTime?operatorName=\" + username\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n realtime_alarm_list = (json.loads(response.text))\n return realtime_alarm_list['faultRealTime']['faultRealTimeList']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_realtime_alarm: An Error has occured'", "response": "Function takes in no param as input to fetch Real - Time Alarms from HP IMC RESTFUL API"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_alarms(username, auth, url):\n f_url = url + \"/imcrs/fault/alarm?operatorName=\" + username + \\\n \"&recStatus=0&ackStatus=0&timeRange=0&size=50&desc=true\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n alarm_list = (json.loads(response.text))\n return alarm_list['alarm']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_alarms: An Error has occured'", "response": "Function takes in no param as input to fetch Real - Time Alarms from HP IMC RESTFUL API"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning to take str input of alarm_id issues a REST call to the IMC REST interface and returns the details of a specific alarm", "response": "def get_alarm_details(alarm_id, auth, url):\n \"\"\"\n function to take str input of alarm_id, issues a REST call to the IMC REST interface and\n returns a dictionary object which contains the details of a specific alarm.\n :param alarm_id: str number which represents the internal ID of a specific alarm\n :param auth:\n :param url:\n :return:\n \"\"\"\n f_url = url + \"/imcrs/fault/alarm/\" + str(alarm_id)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n alarm_details = json.loads(response.text)\n return alarm_details\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_alarm_details: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfunction sends to REST API to acknowledge the specified alarm in the IMC alarm database.", "response": "def acknowledge_alarm(alarm_id, auth, url):\n \"\"\"\n Function tasks input of str of alarm ID and sends to REST API. Function will acknowledge\n designated alarm in the IMC alarm database.\n :param alarm_id: str of alarm ID\n param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n :return: integer HTTP response code\n\n :rtype int\n \"\"\"\n f_url = url + \"/imcrs/fault/alarm/acknowledge/\"+str(alarm_id)\n response = requests.put(f_url, auth=auth, headers=HEADERS)\n try:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_alarms: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_operator(operator, auth, url,headers=HEADERS):\n create_operator_url = '/imcrs/plat/operator'\n f_url = url + create_operator_url\n payload = json.dumps(operator, indent=4)\n # creates the URL using the payload variable as the contents\n r = requests.post(f_url, data=payload, auth=auth, headers=headers)\n try:\n if r.status_code == 409:\n #print(\"Operator Already Exists\")\n return r.status_code\n elif r.status_code == 201:\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' create_operator: An Error has occured'", "response": "Function creates a new operator in HPE IMC Restful API"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists virtual folders that belongs to the current user.", "response": "def list():\n '''List virtual folders that belongs to the current user.'''\n fields = [\n ('Name', 'name'),\n ('ID', 'id'),\n ('Owner', 'is_owner'),\n ('Permission', 'permission'),\n ]\n with Session() as session:\n try:\n resp = session.VFolder.list()\n if not resp:\n print('There is no virtual folders created yet.')\n return\n rows = (tuple(vf[key] for _, key in fields) for vf in resp)\n hdrs = (display_name for display_name, _ in fields)\n print(tabulate(rows, hdrs))\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_hosts():\n '''List the hosts of virtual folders that is accessible to the current user.'''\n with Session() as session:\n try:\n resp = session.VFolder.list_hosts()\n print(f\"Default vfolder host: {resp['default']}\")\n print(f\"Usable hosts: {', '.join(resp['allowed'])}\")\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "List the hosts of virtual folders that is accessible to the current user."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create(name, host):\n '''Create a new virtual folder.\n\n \\b\n NAME: Name of a virtual folder.\n HOST: Name of a virtual folder host in which the virtual folder will be created.\n '''\n with Session() as session:\n try:\n result = session.VFolder.create(name, host)\n print('Virtual folder \"{0}\" is created.'.format(result['name']))\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "Create a new virtual folder."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes the given virtual folder. This operation is irreversible!", "response": "def delete(name):\n '''Delete the given virtual folder. This operation is irreversible!\n\n NAME: Name of a virtual folder.\n '''\n with Session() as session:\n try:\n session.VFolder(name).delete()\n print_done('Deleted.')\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrenames the given virtual folder.", "response": "def rename(old_name, new_name):\n '''Rename the given virtual folder. This operation is irreversible!\n You cannot change the vfolders that are shared by other users,\n and the new name must be unique among all your accessible vfolders\n including the shared ones.\n\n OLD_NAME: The current name of a virtual folder.\n NEW_NAME: The new name of a virtual folder.\n '''\n with Session() as session:\n try:\n session.VFolder(old_name).rename(new_name)\n print_done('Renamed.')\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef info(name):\n '''Show the information of the given virtual folder.\n\n NAME: Name of a virtual folder.\n '''\n with Session() as session:\n try:\n result = session.VFolder(name).info()\n print('Virtual folder \"{0}\" (ID: {1})'\n .format(result['name'], result['id']))\n print('- Owner:', result['is_owner'])\n print('- Permission:', result['permission'])\n print('- Number of files: {0}'.format(result['numFiles']))\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "Show the information of the given virtual folder."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef download(name, filenames):\n '''\n Download a file from the virtual folder to the current working directory.\n The files with the same names will be overwirtten.\n\n \\b\n NAME: Name of a virtual folder.\n FILENAMES: Paths of the files to be uploaded.\n '''\n with Session() as session:\n try:\n session.VFolder(name).download(filenames, show_progress=True)\n print_done('Done.')\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "Downloads a file from the virtual folder to the current working directory."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates an empty directory in the virtual folder.", "response": "def mkdir(name, path):\n '''Create an empty directory in the virtual folder.\n\n \\b\n NAME: Name of a virtual folder.\n PATH: The name or path of directory. Parent directories are created automatically\n if they do not exist.\n '''\n with Session() as session:\n try:\n session.VFolder(name).mkdir(path)\n print_done('Done.')\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndeletes files in a virtual folder.", "response": "def rm(name, filenames, recursive):\n '''\n Delete files in a virtual folder.\n If one of the given paths is a directory and the recursive option is enabled,\n all its content and the directory itself are recursively deleted.\n\n This operation is irreversible!\n\n \\b\n NAME: Name of a virtual folder.\n FILENAMES: Paths of the files to delete.\n '''\n with Session() as session:\n try:\n if input(\"> Are you sure? (y/n): \").lower().strip()[:1] == 'y':\n session.VFolder(name).delete_files(\n filenames,\n recursive=recursive)\n print_done('Done.')\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist files in a virtual folder.", "response": "def ls(name, path):\n \"\"\"\n List files in a path of a virtual folder.\n\n \\b\n NAME: Name of a virtual folder.\n PATH: Path inside vfolder.\n \"\"\"\n with Session() as session:\n try:\n print_wait('Retrieving list of files in \"{}\"...'.format(path))\n result = session.VFolder(name).list_files(path)\n if 'error_msg' in result and result['error_msg']:\n print_fail(result['error_msg'])\n return\n files = json.loads(result['files'])\n table = []\n headers = ['file name', 'size', 'modified', 'mode']\n for file in files:\n mdt = datetime.fromtimestamp(file['mtime'])\n mtime = mdt.strftime('%b %d %Y %H:%M:%S')\n row = [file['filename'], file['size'], mtime, file['mode']]\n table.append(row)\n print_done('Retrived.')\n print(tabulate(table, headers=headers))\n except Exception as e:\n print_error(e)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef invite(name, emails, perm):\n with Session() as session:\n try:\n assert perm in ['rw', 'ro'], \\\n 'Invalid permission: {}'.format(perm)\n result = session.VFolder(name).invite(perm, emails)\n invited_ids = result.get('invited_ids', [])\n if len(invited_ids) > 0:\n print('Invitation sent to:')\n for invitee in invited_ids:\n print('\\t- ' + invitee)\n else:\n print('No users found. Invitation was not sent.')\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "Invite other users to access the virtual folder."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists and manage received invitations.", "response": "def invitations():\n \"\"\"List and manage received invitations.\n \"\"\"\n with Session() as session:\n try:\n result = session.VFolder.invitations()\n invitations = result.get('invitations', [])\n if len(invitations) < 1:\n print('No invitations.')\n return\n print('List of invitations (inviter, vfolder id, permission):')\n for cnt, inv in enumerate(invitations):\n if inv['perm'] == 'rw':\n perm = 'read-write'\n elif inv['perm'] == 'ro':\n perm = 'read-only'\n else:\n perm = inv['perm']\n print('[{}] {}, {}, {}'.format(cnt + 1, inv['inviter'],\n inv['vfolder_id'], perm))\n\n selection = input('Choose invitation number to manage: ')\n if selection.isdigit():\n selection = int(selection) - 1\n else:\n return\n if 0 <= selection < len(invitations):\n while True:\n action = input('Choose action. (a)ccept, (r)eject, (c)ancel: ')\n if action.lower() == 'a':\n # TODO: Let user can select access_key among many.\n # Currently, the config objects holds only one key.\n config = get_config()\n result = session.VFolder.accept_invitation(\n invitations[selection]['id'], config.access_key)\n print(result['msg'])\n break\n elif action.lower() == 'r':\n result = session.VFolder.delete_invitation(\n invitations[selection]['id'])\n print(result['msg'])\n break\n elif action.lower() == 'c':\n break\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a given check callback with the provided object to the list of checks. Useful for built - ins advanced custom checks.", "response": "def init_check(self, check, obj):\n \"\"\"\n Adds a given check callback with the provided object to the list\n of checks. Useful for built-ins but also advanced custom checks.\n \"\"\"\n self.logger.info('Adding extension check %s' % check.__name__)\n check = functools.wraps(check)(functools.partial(check, obj))\n self.check(func=check)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninitializing the extension with the given app.", "response": "def init_app(self, app):\n \"\"\"\n Initializes the extension with the given app, registers the\n built-in views with an own blueprint and hooks up our signal\n callbacks.\n \"\"\"\n # If no version path was provided in the init of the Dockerflow\n # class we'll use the parent directory of the app root path.\n if self.version_path is None:\n self.version_path = os.path.dirname(app.root_path)\n\n for view in (\n ('/__version__', 'version', self._version_view),\n ('/__heartbeat__', 'heartbeat', self._heartbeat_view),\n ('/__lbheartbeat__', 'lbheartbeat', self._lbheartbeat_view),\n ):\n self._blueprint.add_url_rule(*view)\n self._blueprint.before_app_request(self._before_request)\n self._blueprint.after_app_request(self._after_request)\n self._blueprint.app_errorhandler(HeartbeatFailure)(self._heartbeat_exception_handler)\n app.register_blueprint(self._blueprint)\n got_request_exception.connect(self._got_request_exception, sender=app)\n\n if not hasattr(app, 'extensions'): # pragma: nocover\n app.extensions = {}\n app.extensions['dockerflow'] = self"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _after_request(self, response):\n if not getattr(g, '_has_exception', False):\n extra = self.summary_extra()\n self.summary_logger.info('', extra=extra)\n return response", "response": "Called by the HTTP server when the request has finished."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall when a request exception occurs.", "response": "def _got_request_exception(self, sender, exception, **extra):\n \"\"\"\n The signal handler for the got_request_exception signal.\n \"\"\"\n extra = self.summary_extra()\n extra['errno'] = 500\n self.summary_logger.error(str(exception), extra=extra)\n g._has_exception = True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef user_id(self):\n # This needs flask-login to be installed\n if not has_flask_login:\n return\n\n # and the actual login manager installed\n if not hasattr(current_app, 'login_manager'):\n return\n\n # fail if no current_user was attached to the request context\n try:\n is_authenticated = current_user.is_authenticated\n except AttributeError:\n return\n\n # because is_authenticated could be a callable, call it\n if callable(is_authenticated):\n is_authenticated = is_authenticated()\n\n # and fail if the user isn't authenticated\n if not is_authenticated:\n return\n\n # finally return the user id\n return current_user.get_id()", "response": "Return the ID of the current user"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef summary_extra(self):\n out = {\n 'errno': 0,\n 'agent': request.headers.get('User-Agent', ''),\n 'lang': request.headers.get('Accept-Language', ''),\n 'method': request.method,\n 'path': request.path,\n }\n\n # set the uid value to the current user ID\n user_id = self.user_id()\n if user_id is None:\n user_id = ''\n out['uid'] = user_id\n\n # the rid value to the current request ID\n request_id = g.get('_request_id', None)\n if request_id is not None:\n out['rid'] = request_id\n\n # and the t value to the time it took to render\n start_timestamp = g.get('_start_timestamp', None)\n if start_timestamp is not None:\n # Duration of request, in milliseconds.\n out['t'] = int(1000 * (time.time() - start_timestamp))\n\n return out", "response": "Build the extra data for the summary logger."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nview that returns the contents of version. json or a 404.", "response": "def _version_view(self):\n \"\"\"\n View that returns the contents of version.json or a 404.\n \"\"\"\n version_json = self._version_callback(self.version_path)\n if version_json is None:\n return 'version.json not found', 404\n else:\n return jsonify(version_json)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _heartbeat_view(self):\n details = {}\n statuses = {}\n level = 0\n\n for name, check in self.checks.items():\n detail = self._heartbeat_check_detail(check)\n statuses[name] = detail['status']\n level = max(level, detail['level'])\n if detail['level'] > 0:\n details[name] = detail\n\n payload = {\n 'status': checks.level_to_text(level),\n 'checks': statuses,\n 'details': details,\n }\n\n def render(status_code):\n return make_response(jsonify(payload), status_code)\n\n if level < checks.WARNING:\n status_code = 200\n heartbeat_passed.send(self, level=level)\n return render(status_code)\n else:\n status_code = 500\n heartbeat_failed.send(self, level=level)\n raise HeartbeatFailure(response=render(status_code))", "response": "Runs all the registered checks and returns a JSON response with either a status code of 200 or 500 depending on the results of the checks."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef drange(start: Decimal, stop: Decimal, num: int):\n '''\n A simplified version of numpy.linspace with default options\n '''\n delta = stop - start\n step = delta / (num - 1)\n yield from (start + step * Decimal(tick) for tick in range(0, num))", "response": "A simplified version of numpy. linspace with default options\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef range_expr(arg):\n '''\n Accepts a range expression which generates a range of values for a variable.\n\n Linear space range: \"linspace:1,2,10\" (start, stop, num) as in numpy.linspace\n Pythonic range: \"range:1,10,2\" (start, stop[, step]) as in Python's range\n Case range: \"case:a,b,c\" (comma-separated strings)\n '''\n key, value = arg.split('=', maxsplit=1)\n assert _rx_range_key.match(key), 'The key must be a valid slug string.'\n try:\n if value.startswith('case:'):\n return key, value[5:].split(',')\n elif value.startswith('linspace:'):\n start, stop, num = value[9:].split(',')\n return key, tuple(drange(Decimal(start), Decimal(stop), int(num)))\n elif value.startswith('range:'):\n range_args = map(int, value[6:].split(','))\n return key, tuple(range(*range_args))\n else:\n raise ArgumentTypeError('Unrecognized range expression type')\n except ValueError as e:\n raise ArgumentTypeError(str(e))", "response": "Generates a range expression which generates a range of values for a variable."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def exec_loop(stdout, stderr, kernel, mode, code, *, opts=None,\n vprint_done=print_done, is_multi=False):\n '''\n Fully streamed asynchronous version of the execute loop.\n '''\n async with kernel.stream_execute(code, mode=mode, opts=opts) as stream:\n async for result in stream:\n if result.type == aiohttp.WSMsgType.TEXT:\n result = json.loads(result.data)\n else:\n # future extension\n continue\n for rec in result.get('console', []):\n if rec[0] == 'stdout':\n print(rec[1], end='', file=stdout)\n elif rec[0] == 'stderr':\n print(rec[1], end='', file=stderr)\n else:\n print('----- output record (type: {0}) -----'.format(rec[0]),\n file=stdout)\n print(rec[1], file=stdout)\n print('----- end of record -----', file=stdout)\n stdout.flush()\n files = result.get('files', [])\n if files:\n print('--- generated files ---', file=stdout)\n for item in files:\n print('{0}: {1}'.format(item['name'], item['url']), file=stdout)\n print('--- end of generated files ---', file=stdout)\n if result['status'] == 'clean-finished':\n exitCode = result.get('exitCode')\n msg = 'Clean finished. (exit code = {0})'.format(exitCode)\n if is_multi:\n print(msg, file=stderr)\n vprint_done(msg)\n elif result['status'] == 'build-finished':\n exitCode = result.get('exitCode')\n msg = 'Build finished. (exit code = {0})'.format(exitCode)\n if is_multi:\n print(msg, file=stderr)\n vprint_done(msg)\n elif result['status'] == 'finished':\n exitCode = result.get('exitCode')\n msg = 'Execution finished. (exit code = {0})'.format(exitCode)\n if is_multi:\n print(msg, file=stderr)\n vprint_done(msg)\n break\n elif result['status'] == 'waiting-input':\n if result['options'].get('is_password', False):\n code = getpass.getpass()\n else:\n code = input()\n await stream.send_str(code)\n elif result['status'] == 'continued':\n pass", "response": "Execute the code in a single asynchronous thread."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exec_loop_sync(stdout, stderr, kernel, mode, code, *, opts=None,\n vprint_done=print_done):\n '''\n Old synchronous polling version of the execute loop.\n '''\n opts = opts if opts else {}\n run_id = None # use server-assigned run ID\n while True:\n result = kernel.execute(run_id, code, mode=mode, opts=opts)\n run_id = result['runId']\n opts.clear() # used only once\n for rec in result['console']:\n if rec[0] == 'stdout':\n print(rec[1], end='', file=stdout)\n elif rec[0] == 'stderr':\n print(rec[1], end='', file=stderr)\n else:\n print('----- output record (type: {0}) -----'.format(rec[0]),\n file=stdout)\n print(rec[1], file=stdout)\n print('----- end of record -----', file=stdout)\n stdout.flush()\n files = result.get('files', [])\n if files:\n print('--- generated files ---', file=stdout)\n for item in files:\n print('{0}: {1}'.format(item['name'], item['url']), file=stdout)\n print('--- end of generated files ---', file=stdout)\n if result['status'] == 'clean-finished':\n exitCode = result.get('exitCode')\n vprint_done('Clean finished. (exit code = {0}'.format(exitCode),\n file=stdout)\n mode = 'continue'\n code = ''\n elif result['status'] == 'build-finished':\n exitCode = result.get('exitCode')\n vprint_done('Build finished. (exit code = {0})'.format(exitCode),\n file=stdout)\n mode = 'continue'\n code = ''\n elif result['status'] == 'finished':\n exitCode = result.get('exitCode')\n vprint_done('Execution finished. (exit code = {0})'.format(exitCode),\n file=stdout)\n break\n elif result['status'] == 'waiting-input':\n mode = 'input'\n if result['options'].get('is_password', False):\n code = getpass.getpass()\n else:\n code = input()\n elif result['status'] == 'continued':\n mode = 'continue'\n code = ''", "response": "Execute a single kernel loop."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run(lang, files, session_id, cluster_size, code, clean, build, exec, terminal,\n basedir, rm, env, env_range, build_range, exec_range, max_parallel, mount,\n stats, tag, resources, quiet):\n '''\n Run the given code snippet or files in a session.\n Depending on the session ID you give (default is random),\n it may reuse an existing session or create a new one.\n\n \\b\n LANG: The name (and version/platform tags appended after a colon) of session\n runtime or programming language.')\n FILES: The code file(s). Can be added multiple times.\n '''\n if quiet:\n vprint_info = vprint_wait = vprint_done = _noop\n else:\n vprint_info = print_info\n vprint_wait = print_wait\n vprint_done = print_done\n if files and code:\n print('You can run only either source files or command-line '\n 'code snippet.', file=sys.stderr)\n sys.exit(1)\n if not files and not code:\n print('You should provide the command-line code snippet using '\n '\"-c\" option if run without files.', file=sys.stderr)\n sys.exit(1)\n\n envs = _prepare_env_arg(env)\n resources = _prepare_resource_arg(resources)\n mount = _prepare_mount_arg(mount)\n\n if not (1 <= cluster_size < 4):\n print('Invalid cluster size.', file=sys.stderr)\n sys.exit(1)\n\n if env_range is None: env_range = [] # noqa\n if build_range is None: build_range = [] # noqa\n if exec_range is None: exec_range = [] # noqa\n\n env_ranges = {v: r for v, r in env_range}\n build_ranges = {v: r for v, r in build_range}\n exec_ranges = {v: r for v, r in exec_range}\n\n env_var_maps = [dict(zip(env_ranges.keys(), values))\n for values in itertools.product(*env_ranges.values())]\n build_var_maps = [dict(zip(build_ranges.keys(), values))\n for values in itertools.product(*build_ranges.values())]\n exec_var_maps = [dict(zip(exec_ranges.keys(), values))\n for values in itertools.product(*exec_ranges.values())]\n case_set = collections.OrderedDict()\n vmaps_product = itertools.product(env_var_maps, build_var_maps, exec_var_maps)\n build_template = string.Template(build)\n exec_template = string.Template(exec)\n env_templates = {k: string.Template(v) for k, v in envs.items()}\n for env_vmap, build_vmap, exec_vmap in vmaps_product:\n interpolated_envs = tuple((k, vt.substitute(env_vmap))\n for k, vt in env_templates.items())\n if build:\n interpolated_build = build_template.substitute(build_vmap)\n else:\n interpolated_build = '*'\n if exec:\n interpolated_exec = exec_template.substitute(exec_vmap)\n else:\n interpolated_exec = '*'\n case_set[(interpolated_envs, interpolated_build, interpolated_exec)] = 1\n\n is_multi = (len(case_set) > 1)\n if is_multi:\n if max_parallel <= 0:\n print('The number maximum parallel sessions must be '\n 'a positive integer.', file=sys.stderr)\n sys.exit(1)\n if terminal:\n print('You cannot run multiple cases with terminal.', file=sys.stderr)\n sys.exit(1)\n if not quiet:\n vprint_info('Running multiple sessions for the following combinations:')\n for case in case_set.keys():\n pretty_env = ' '.join('{}={}'.format(item[0], item[1])\n for item in case[0])\n print('env = {!r}, build = {!r}, exec = {!r}'\n .format(pretty_env, case[1], case[2]))\n\n def _run_legacy(session, idx, session_id, envs,\n clean_cmd, build_cmd, exec_cmd):\n try:\n kernel = session.Kernel.get_or_create(\n lang,\n client_token=session_id,\n cluster_size=cluster_size,\n mounts=mount,\n envs=envs,\n resources=resources,\n tag=tag)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if kernel.created:\n vprint_done('[{0}] Session {0} is ready.'.format(idx, kernel.kernel_id))\n else:\n vprint_done('[{0}] Reusing session {0}...'.format(idx, kernel.kernel_id))\n if kernel.service_ports:\n print_info('This session provides the following app services: '\n ', '.join(sport['name'] for sport in kernel.service_ports))\n\n try:\n if files:\n vprint_wait('[{0}] Uploading source files...'.format(idx))\n ret = kernel.upload(files, basedir=basedir,\n show_progress=True)\n if ret.status // 100 != 2:\n print_fail('[{0}] Uploading source files failed!'.format(idx))\n print('{0}: {1}\\n{2}'.format(\n ret.status, ret.reason, ret.text()))\n return\n vprint_done('[{0}] Uploading done.'.format(idx))\n opts = {\n 'clean': clean_cmd,\n 'build': build_cmd,\n 'exec': exec_cmd,\n }\n if not terminal:\n exec_loop_sync(sys.stdout, sys.stderr, kernel, 'batch', '',\n opts=opts,\n vprint_done=vprint_done)\n if terminal:\n raise NotImplementedError('Terminal access is not supported in '\n 'the legacy synchronous mode.')\n if code:\n exec_loop_sync(sys.stdout, sys.stderr, kernel, 'query', code,\n vprint_done=vprint_done)\n vprint_done('[{0}] Execution finished.'.format(idx))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n finally:\n if rm:\n vprint_wait('[{0}] Cleaning up the session...'.format(idx))\n ret = kernel.destroy()\n vprint_done('[{0}] Cleaned up the session.'.format(idx))\n if stats:\n _stats = ret.get('stats', None) if ret else None\n if _stats:\n _stats.pop('precpu_used', None)\n _stats.pop('precpu_system_used', None)\n _stats.pop('cpu_system_used', None)\n print('[{0}] Statistics:\\n{1}'\n .format(idx, _format_stats(_stats)))\n else:\n print('[{0}] Statistics is not available.'.format(idx))\n\n async def _run(session, idx, session_id, envs,\n clean_cmd, build_cmd, exec_cmd,\n is_multi=False):\n try:\n kernel = await session.Kernel.get_or_create(\n lang,\n client_token=session_id,\n cluster_size=cluster_size,\n mounts=mount,\n envs=envs,\n resources=resources,\n tag=tag)\n except BackendError as e:\n print_fail('[{0}] {1}'.format(idx, e))\n return\n if kernel.created:\n vprint_done('[{0}] Session {1} is ready.'.format(idx, kernel.kernel_id))\n else:\n vprint_done('[{0}] Reusing session {1}...'.format(idx, kernel.kernel_id))\n\n if not is_multi:\n stdout = sys.stdout\n stderr = sys.stderr\n else:\n log_dir = Path.home() / '.cache' / 'backend.ai' / 'client-logs'\n log_dir.mkdir(parents=True, exist_ok=True)\n stdout = open(log_dir / '{0}.stdout.log'.format(session_id),\n 'w', encoding='utf-8')\n stderr = open(log_dir / '{0}.stderr.log'.format(session_id),\n 'w', encoding='utf-8')\n\n try:\n def indexed_vprint_done(msg):\n vprint_done('[{0}] '.format(idx) + msg)\n if files:\n if not is_multi:\n vprint_wait('[{0}] Uploading source files...'.format(idx))\n ret = await kernel.upload(files, basedir=basedir,\n show_progress=not is_multi)\n if ret.status // 100 != 2:\n print_fail('[{0}] Uploading source files failed!'.format(idx))\n print('{0}: {1}\\n{2}'.format(\n ret.status, ret.reason, ret.text()), file=stderr)\n raise RuntimeError('Uploading source files has failed!')\n if not is_multi:\n vprint_done('[{0}] Uploading done.'.format(idx))\n opts = {\n 'clean': clean_cmd,\n 'build': build_cmd,\n 'exec': exec_cmd,\n }\n if not terminal:\n await exec_loop(stdout, stderr, kernel, 'batch', '',\n opts=opts,\n vprint_done=indexed_vprint_done,\n is_multi=is_multi)\n if terminal:\n await exec_terminal(kernel)\n return\n if code:\n await exec_loop(stdout, stderr, kernel, 'query', code,\n vprint_done=indexed_vprint_done,\n is_multi=is_multi)\n except BackendError as e:\n print_fail('[{0}] {1}'.format(idx, e))\n raise RuntimeError(e)\n except Exception as e:\n print_fail('[{0}] Execution failed!'.format(idx))\n traceback.print_exc()\n raise RuntimeError(e)\n finally:\n try:\n if rm:\n if not is_multi:\n vprint_wait('[{0}] Cleaning up the session...'.format(idx))\n ret = await kernel.destroy()\n vprint_done('[{0}] Cleaned up the session.'.format(idx))\n if stats:\n _stats = ret.get('stats', None) if ret else None\n if _stats:\n _stats.pop('precpu_used', None)\n _stats.pop('precpu_system_used', None)\n _stats.pop('cpu_system_used', None)\n stats_str = _format_stats(_stats)\n print(format_info('[{0}] Statistics:'.format(idx)) +\n '\\n{0}'.format(stats_str))\n if is_multi:\n print('Statistics:\\n{0}'.format(stats_str),\n file=stderr)\n else:\n print_warn('[{0}] Statistics: unavailable.'.format(idx))\n if is_multi:\n print('Statistics: unavailable.', file=stderr)\n finally:\n if is_multi:\n stdout.close()\n stderr.close()\n\n def _run_cases_legacy():\n if session_id is None:\n session_id_prefix = token_hex(5)\n else:\n session_id_prefix = session_id\n vprint_info('Session token prefix: {0}'.format(session_id_prefix))\n vprint_info('In the legacy mode, all cases will run serially!')\n with Session() as session:\n for idx, case in enumerate(case_set.keys()):\n if is_multi:\n _session_id = '{0}-{1}'.format(session_id_prefix, idx)\n else:\n _session_id = session_id_prefix\n envs = dict(case[0])\n clean_cmd = clean if clean else '*'\n build_cmd = case[1]\n exec_cmd = case[2]\n _run_legacy(session, idx, _session_id, envs,\n clean_cmd, build_cmd, exec_cmd)\n\n async def _run_cases():\n loop = current_loop()\n if session_id is None:\n session_id_prefix = token_hex(5)\n else:\n session_id_prefix = session_id\n vprint_info('Session token prefix: {0}'.format(session_id_prefix))\n if is_multi:\n print_info('Check out the stdout/stderr logs stored in '\n '~/.cache/backend.ai/client-logs directory.')\n async with AsyncSession() as session:\n tasks = []\n # TODO: limit max-parallelism using aiojobs\n for idx, case in enumerate(case_set.keys()):\n if is_multi:\n _session_id = '{0}-{1}'.format(session_id_prefix, idx)\n else:\n _session_id = session_id_prefix\n envs = dict(case[0])\n clean_cmd = clean if clean else '*'\n build_cmd = case[1]\n exec_cmd = case[2]\n t = loop.create_task(\n _run(session, idx, _session_id, envs,\n clean_cmd, build_cmd, exec_cmd,\n is_multi=is_multi))\n tasks.append(t)\n results = await asyncio.gather(*tasks, return_exceptions=True)\n if any(map(lambda r: isinstance(r, Exception), results)):\n if is_multi:\n print_fail('There were failed cases!')\n sys.exit(1)\n\n if is_legacy_server():\n _run_cases_legacy()\n else:\n loop = asyncio.get_event_loop()\n try:\n loop.run_until_complete(_run_cases())\n finally:\n loop.close()", "response": "Runs the given code snippet or files in a session."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstart a single compute session.", "response": "def start(lang, session_id, owner, env, mount, tag, resources, cluster_size):\n '''\n Prepare and start a single compute session without executing codes.\n You may use the created session to execute codes using the \"run\" command\n or connect to an application service provided by the session using the \"app\"\n command.\n\n\n \\b\n LANG: The name (and version/platform tags appended after a colon) of session\n runtime or programming language.\n '''\n if session_id is None:\n session_id = token_hex(5)\n else:\n session_id = session_id\n\n ######\n envs = _prepare_env_arg(env)\n resources = _prepare_resource_arg(resources)\n mount = _prepare_mount_arg(mount)\n with Session() as session:\n try:\n kernel = session.Kernel.get_or_create(\n lang,\n client_token=session_id,\n cluster_size=cluster_size,\n mounts=mount,\n envs=envs,\n resources=resources,\n owner_access_key=owner,\n tag=tag)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n else:\n if kernel.created:\n print_info('Session ID {0} is created and ready.'\n .format(session_id))\n else:\n print_info('Session ID {0} is already running and ready.'\n .format(session_id))\n if kernel.service_ports:\n print_info('This session provides the following app services: ' +\n ', '.join(sport['name']\n for sport in kernel.service_ports))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef terminate(sess_id_or_alias, owner, stats):\n '''\n Terminate the given session.\n\n SESSID: session ID or its alias given when creating the session.\n '''\n print_wait('Terminating the session(s)...')\n with Session() as session:\n has_failure = False\n for sess in sess_id_or_alias:\n try:\n kernel = session.Kernel(sess, owner)\n ret = kernel.destroy()\n except BackendAPIError as e:\n print_error(e)\n if e.status == 404:\n print_info(\n 'If you are an admin, use \"-o\" / \"--owner\" option '\n 'to terminate other user\\'s session.')\n has_failure = True\n except Exception as e:\n print_error(e)\n has_failure = True\n if has_failure:\n sys.exit(1)\n else:\n print_done('Done.')\n if stats:\n stats = ret.get('stats', None) if ret else None\n if stats:\n print(_format_stats(stats))\n else:\n print('Statistics is not available.')", "response": "Terminate the given session."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_custom_view_details(name, auth, url):\n view_id = get_custom_views(auth, url, name=name)[0]['symbolId']\n get_custom_view_details_url = '/imcrs/plat/res/view/custom/' + str(view_id)\n f_url = url + get_custom_view_details_url\n r = requests.get(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 200:\n current_devices = (json.loads(r.text))\n if 'device' in current_devices:\n return current_devices['device']\n else:\n return []\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_custom_views: An Error has occured'", "response": "function takes name auth object and url and returns a list of dictionaries containing the details of the custom views"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfunctioning to add a list of devices to a specific custom views from HPE IMC", "response": "def add_devs_custom_views(custom_view_name, dev_list, auth, url):\n \"\"\"\n function takes a list of devIDs from devices discovered in the HPE IMC platform and and issues a RESTFUL call to\n add the list of devices to a specific custom views from HPE IMC.\n\n :param dev_list: list containing the devID of all devices to be contained in this custom view.\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: str of creation results ( \"view \" + name + \"created successfully\"\n\n :rtype: str\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.groups import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n \"\"\"\n view_id = get_custom_views(auth, url, name=custom_view_name)[0]['symbolId']\n add_devs_custom_views_url = '/imcrs/plat/res/view/custom/'+str(view_id)\n payload = '''{\"device\" : '''+ json.dumps(dev_list) + '''}'''\n f_url = url + add_devs_custom_views_url\n r = requests.put(f_url, data = payload, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 204:\n print ('View ' + custom_view_name +' : Devices Successfully Added')\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_custom_views: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes input of auth, url, and name and issues a RESTFUL call to delete a specific of custom views from HPE IMC. :param name: string containg the name of the desired custom view :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: str of creation results ( \"view \" + name + \"created successfully\" :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.groups import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> delete_custom_view(auth.creds, auth.url, name = \"L1 View\") 'View L1 View deleted successfully' >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View') >>> assert view_1 == None >>> delete_custom_view(auth.creds, auth.url, name = \"L2 View\") 'View L2 View deleted successfully' >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View') >>> assert view_2 == None", "response": "def delete_custom_view(auth, url, name):\n \"\"\"\n function takes input of auth, url, and name and issues a RESTFUL call to delete a specific of custom views from HPE\n IMC.\n :param name: string containg the name of the desired custom view\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: str of creation results ( \"view \" + name + \"created successfully\"\n\n :rtype: str\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.groups import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_custom_view(auth.creds, auth.url, name = \"L1 View\")\n 'View L1 View deleted successfully'\n\n >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View')\n\n >>> assert view_1 == None\n\n >>> delete_custom_view(auth.creds, auth.url, name = \"L2 View\")\n 'View L2 View deleted successfully'\n\n >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View')\n\n >>> assert view_2 == None\n\n \"\"\"\n view_id = get_custom_views(auth, url,name )[0]['symbolId']\n delete_custom_view_url = '/imcrs/plat/res/view/custom/'+str(view_id)\n f_url = url + delete_custom_view_url\n r = requests.delete(f_url, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 204:\n return 'View ' + name +' deleted successfully'\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' delete_custom_view: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nliking unicode. split but accept several separators and regexes.", "response": "def multisplit(string, # type: unicode\n *separators, # type: unicode\n **kwargs # type: Union[unicode, C[..., I[unicode]]]\n ): # type: (...) -> I\n \"\"\" Like unicode.split, but accept several separators and regexes\n\n Args:\n string: the string to split.\n separators: strings you can split on. Each string can be a\n regex.\n maxsplit: max number of time you wish to split. default is 0,\n which means no limit.\n flags: flags you wish to pass if you use regexes. You should\n pass them as a string containing a combination of:\n\n - 'm' for re.MULTILINE\n - 'x' for re.VERBOSE\n - 'v' for re.VERBOSE\n - 's' for re.DOTALL\n - '.' for re.DOTALL\n - 'd' for re.DEBUG\n - 'i' for re.IGNORECASE\n - 'u' for re.UNICODE\n - 'l' for re.LOCALE\n cast: what to cast the result to\n\n Returns:\n An iterable of substrings.\n\n Raises:\n ValueError: if you pass a flag without separators.\n TypeError: if you pass something else than unicode strings.\n\n Example:\n\n >>> for word in multisplit(u'fat black cat, big'): print(word)\n fat\n black\n cat,\n big\n >>> string = u'a,b;c/d=a,b;c/d'\n >>> chunks = multisplit(string, u',', u';', u'[/=]', maxsplit=4)\n >>> for chunk in chunks: print(chunk)\n a\n b\n c\n d\n a,b;c/d\n\n \"\"\"\n\n cast = kwargs.pop('cast', list)\n flags = parse_re_flags(kwargs.get('flags', 0))\n # 0 means \"no limit\" for re.split\n maxsplit = require_positive_number(kwargs.get('maxsplit', 0),\n 'maxsplit')\n\n # no separator means we use the default unicode.split behavior\n if not separators:\n if flags:\n raise ValueError(ww.s >> \"\"\"\n You can't pass flags without passing\n a separator. Flags only have sense if\n you split using a regex.\n \"\"\")\n\n maxsplit = maxsplit or -1 # -1 means \"no limit\" for unicode.split\n return unicode.split(string, None, maxsplit)\n\n # Check that all separators are strings\n for i, sep in enumerate(separators):\n if not isinstance(sep, unicode):\n raise TypeError(ww.s >> \"\"\"\n '{!r}', the separator at index '{}', is of type '{}'.\n multisplit() only accepts unicode strings.\n \"\"\".format(sep, i, type(sep)))\n\n # TODO: split let many empty strings in the result. Fix it.\n\n seps = list(separators) # cast to list so we can slice it\n\n # simple code for when you need to split the whole string\n if maxsplit == 0:\n return cast(_split(string, seps, flags))\n\n # slow implementation with checks for recursive maxsplit\n return cast(_split_with_max(string, seps, maxsplit, flags))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef multireplace(string, # type: unicode\n patterns, # type: str_or_str_iterable\n substitutions, # type: str_istr_icallable\n maxreplace=0, # type: int\n flags=0 # type: unicode\n ): # type: (...) -> bool\n \"\"\" Like unicode.replace() but accept several substitutions and regexes\n\n Args:\n string: the string to split on.\n patterns: a string, or an iterable of strings to be replaced.\n substitutions: a string or an iterable of string to use as a\n replacement. You can pass either one string, or\n an iterable containing the same number of\n sustitutions that you passed as patterns. You can\n also pass a callable instead of a string. It\n should expact a match object as a parameter.\n maxreplace: the max number of replacement to make. 0 is no limit,\n which is the default.\n flags: flags you wish to pass if you use regexes. You should\n pass them as a string containing a combination of:\n\n - 'm' for re.MULTILINE\n - 'x' for re.VERBOSE\n - 'v' for re.VERBOSE\n - 's' for re.DOTALL\n - '.' for re.DOTALL\n - 'd' for re.DEBUG\n - 'i' for re.IGNORECASE\n - 'u' for re.UNICODE\n - 'l' for re.LOCALE\n\n Returns:\n The string with replaced bits.\n\n Raises:\n ValueError: if you pass the wrong number of substitution.\n\n Example:\n\n >>> print(multireplace(u'a,b;c/d', (u',', u';', u'/'), u','))\n a,b,c,d\n >>> print(multireplace(u'a1b33c-d', u'\\d+', u','))\n a,b,c-d\n >>> print(multireplace(u'a-1,b-3,3c-d', u',|-', u'', maxreplace=3))\n a1b3,3c-d\n >>> def upper(match):\n ... return match.group().upper()\n ...\n >>> print(multireplace(u'a-1,b-3,3c-d', u'[ab]', upper))\n A-1,B-3,3c-d\n \"\"\"\n\n # we can pass either a string or an iterable of strings\n patterns = ensure_tuple(patterns)\n substitutions = ensure_tuple(substitutions)\n\n # you can either have:\n # - many patterns, one substitution\n # - many patterns, exactly as many substitutions\n # anything else is an error\n num_of_subs = len(substitutions)\n num_of_patterns = len(patterns)\n\n if num_of_subs == 1 and num_of_patterns > 0:\n substitutions *= num_of_patterns\n elif len(patterns) != num_of_subs:\n raise ValueError(\"You must have exactly one substitution \"\n \"for each pattern or only one substitution\")\n\n flags = parse_re_flags(flags)\n\n # no limit for replacing, use a simple code\n if not maxreplace:\n for pattern, sub in zip(patterns, substitutions):\n string, count = re.subn(pattern, sub, string, flags=flags)\n return string\n\n # ensure we respect the max number of replace accross substitutions\n for pattern, sub in zip(patterns, substitutions):\n string, count = re.subn(pattern, sub, string,\n count=maxreplace, flags=flags)\n maxreplace -= count\n if maxreplace == 0:\n break\n\n return string", "response": "Like unicode. replace but accept several substitutions and regexes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef status():\n '''Show the manager's current status.'''\n with Session() as session:\n resp = session.Manager.status()\n print(tabulate([('Status', 'Active Sessions'),\n (resp['status'], resp['active_sessions'])],\n headers='firstrow'))", "response": "Show the manager s current status."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_dev_vlans(auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_dev_vlans_url = \"/imcrs/vlan?devId=\" + str(devid) + \"&start=0&size=5000&total=false\"\n f_url = url + get_dev_vlans_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_vlans = (json.loads(response.text))\n return dev_vlans['vlan']\n elif response.status_code == 409:\n return {'vlan': 'no vlans'}\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_dev_vlans: An Error has occured'", "response": "Function takes input of devId to issue RESTUL call to HP IMC API to get list of vlans on the target device"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning to RESTFULL call to HP IMC API to get list of trunk interfaces", "response": "def get_trunk_interfaces(auth, url, devid=None, devip=None):\n \"\"\"Function takes devId as input to RESTFULL call to HP IMC platform\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: list of dictionaries where each element of the list represents an interface which\n has been configured as a\n VLAN trunk port\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> trunk_interfaces = get_trunk_interfaces('10', auth.creds, auth.url)\n\n >>> assert type(trunk_interfaces) is list\n\n >>> assert len(trunk_interfaces[0]) == 3\n\n >>> assert 'allowedVlans' in trunk_interfaces[0]\n\n >>> assert 'ifIndex' in trunk_interfaces[0]\n\n >>> assert 'pvid' in trunk_interfaces[0]\n\n >>> get_trunk_interfaces('350', auth.creds, auth.url)\n ['No trunk inteface']\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_trunk_interfaces_url = \"/imcrs/vlan/trunk?devId=\" + str(devid) + \\\n \"&start=1&size=5000&total=false\"\n f_url = url + get_trunk_interfaces_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_trunk_interfaces = (json.loads(response.text))\n if len(dev_trunk_interfaces) == 2:\n if isinstance(dev_trunk_interfaces['trunkIf'], list):\n return dev_trunk_interfaces['trunkIf']\n elif isinstance(dev_trunk_interfaces['trunkIf'], dict):\n return [dev_trunk_interfaces['trunkIf']]\n else:\n dev_trunk_interfaces['trunkIf'] = [\"No trunk inteface\"]\n return dev_trunk_interfaces['trunkIf']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_trunk_interfaces: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning returns list of dicts where each element of the list represents an interface which has been configured as a VLAN access port", "response": "def get_device_access_interfaces(auth, url, devid=None, devip=None):\n \"\"\"\n Function takes devid pr devip as input to RESTFUL call to HP IMC platform\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: list of dictionaries where each element of the list represents an interface which\n has been configured as a\n VLAN access port\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> access_interfaces = get_device_access_interfaces('10', auth.creds, auth.url)\n\n >>> assert type(access_interfaces) is list\n\n >>> assert (len(access_interfaces[0])) is 2\n\n >>> assert 'ifIndex' in access_interfaces[0]\n\n >>> assert 'pvid' in access_interfaces[0]\n\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_access_interface_vlan_url = \"/imcrs/vlan/access?devId=\" + str(devid) + \\\n \"&start=1&size=500&total=false\"\n f_url = url + get_access_interface_vlan_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_access_interfaces = (json.loads(response.text))\n if type(dev_access_interfaces['accessIf']) is dict:\n return [dev_access_interfaces['accessIf']]\n if len(dev_access_interfaces) == 2:\n return dev_access_interfaces['accessIf']\n else:\n dev_access_interfaces['accessIf'] = [\"No access inteface\"]\n return dev_access_interfaces['accessIf']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_device_access_interfaces: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction returns list of hybrid interfaces for a device", "response": "def get_device_hybrid_interfaces(auth, url, devid=None, devip=None):\n \"\"\"\n Function takes devId as input to RESTFUL call to HP IMC platform\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: list of dictionaries where each element of the list represents an interface which\n has been configured as a\n VLAN access port\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> hybrid_interfaces = get_device_hybrid_interfaces('10', auth.creds, auth.url)\n\n >>> assert type(access_interfaces) is list\n\n >>> assert (len(access_interfaces[0])) is 2\n\n >>> assert 'ifIndex' in access_interfaces[0]\n\n >>> assert 'pvid' in access_interfaces[0]\n\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n get_hybrid_interface_vlan_url = \"/imcrs/vlan/hybrid?devId=\" + str(devid) + \\\n \"&start=1&size=500&total=false\"\n f_url = url + get_hybrid_interface_vlan_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n dev_hybrid_interfaces = (json.loads(response.text))\n if len(dev_hybrid_interfaces) == 2:\n dev_hybrid = dev_hybrid_interfaces['hybridIf']\n if isinstance(dev_hybrid, dict):\n dev_hybrid = [dev_hybrid]\n return dev_hybrid\n else:\n dev_hybrid_interfaces['hybridIf'] = [\"No hybrid inteface\"]\n return dev_hybrid_interfaces['hybridIf']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_device_hybrid_interfaces: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_hybrid_interface(ifindex, pvid, taggedvlans, untaggedvlans, auth, url, devip=None,\n devid=None):\n \"\"\"\n Function takes ifindex, pvid, tagged vlans untagged vlans as input values to add a hybrid\n port to a HPE Comware based switch. These functions only apply to HPE Comware based devices.\n :param ifindex: str ifIndex value of target interface\n :param pvid: str 802.1q value (1-4094) of target VLAN\n :param taggedvlans: str 802.1q value, seperated by commas, of target tagged VLANs\n :param untaggedvlans: str 802.1q value, seperated by commas, of target untagged VLANs\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n :param devid: str requires devid of the target device\n :param devip: str of ipv4 address of the target device\n :return int of http response code\n :rtype int\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n add_hybrid_interface_url = \"/imcrs/vlan/hybrid?devId=\" + str(devid) + \\\n \"&start=1&size=500&total=false\"\n f_url = url + add_hybrid_interface_url\n payload = '''{\"ifIndex\": \"''' + ifindex + '''\",\n \"pvid\": \"''' + pvid + '''\",\n \"taggedVlans\": \"''' + taggedvlans + '''\",\n \"untagVlanFlag\": \"true\",\n \"untaggedVlans\": \"''' + untaggedvlans + '''\"\n }'''\n response = requests.post(f_url, auth=auth, data=payload, headers=HEADERS)\n try:\n if response.status_code == 201:\n return 201\n if response.status_code == 409:\n return 409\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_device_hybrid_interfaces: An Error has occured\"", "response": "Function adds a hybrid interface to a HPE Comware based switch."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction deletes a hybrid interface from the specified device.", "response": "def delete_hybrid_interface(ifindex, auth, url, devip=None, devid=None):\n \"\"\"\n Function takes devip ( ipv4 address ), ifIndex and pvid (vlanid) of specific device and\n 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device.\n\n :param ifindex: str value of ifIndex for a specific interface on the device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: int of 204 if successful or 409 if not succesful\n\n :rtype: int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_hybrid_interface('9', auth.creds, auth.url, devip='10.101.0.221')\n 409\n\n >>> add_hybrid = add_hybrid_interface('9', '1', '10', '1', auth.creds, auth.url,\n devip='10.101.0.221')\n\n >>> delete_hybrid = delete_hybrid_interface('9', auth.creds, auth.url, devip='10.101.0.221')\n\n >>> assert type(delete_hybrid) is int\n\n >>> assert delete_hybrid == 204\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n f_url = url + \"/imcrs/vlan/hybrid?devId=\" + devid + \"&ifIndex=\" + ifindex\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n return 204\n if response.status_code == 409:\n return 409\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_device_hybrid_interfaces: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfunctioning sets the access interface for the specified VLAN on the specified device.", "response": "def set_access_interface_pvid(ifindex, pvid, auth, url, devip=None, devid=None):\n \"\"\"\n Function takes devip ( ipv4 address ), ifIndex and pvid (vlanid) of specific device and\n 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device.\n\n :param ifindex: str value of ifIndex for a specific interface on the device\n\n :param pvid: str value of dot1q VLAN desired to apply to the device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: int of 204 if successful or 409 if not succesful\n\n :rtype: int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url,\n devip='10.101.0.221')\n\n >>> set_access_int_vlan = set_access_interface_pvid('9', '10', auth.creds, auth.url,\n devip='10.101.0.221')\n\n >>> assert type(set_access_int_vlan) is int\n\n >>> assert set_access_int_vlan == 204\n\n >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url,\n devip='10.101.0.221')\n\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n set_access_interface_pvid_url = \"/imcrs/vlan/access?devId=\" + devid + \"&destVlanId=\" + pvid \\\n + \"&ifIndex=\" + str(ifindex)\n f_url = url + set_access_interface_pvid_url\n response = requests.put(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n return 204\n if response.status_code == 409:\n return 409\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" set_access_interface_pvid: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes devid and vlanid vlan_name of specific device and 802.1q VLAN tag and issues a RESTFUL call to add the specified VLAN from the target device. VLAN Name MUST be valid on target device. :param vlanid:int or str value of target 802.1q VLAN :param vlan_name: str value of the target 802.1q VLAN name. MUST be valid name on target device. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: str HTTP Response code. Should be 201 if successfully created :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url)", "response": "def create_dev_vlan(vlanid, vlan_name, auth, url, devid=None, devip=None):\n \"\"\"\n function takes devid and vlanid vlan_name of specific device and 802.1q VLAN tag\n and issues a RESTFUL call to add the specified VLAN from the target device. VLAN Name\n MUST be valid on target device.\n\n :param vlanid:int or str value of target 802.1q VLAN\n\n :param vlan_name: str value of the target 802.1q VLAN name. MUST be valid name on target device.\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param devid: str requires devid of the target device\n\n :param devip: str of ipv4 address of the target device\n\n :return: str HTTP Response code. Should be 201 if successfully created\n\n :rtype: str\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vlanm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url)\n\n\n \"\"\"\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n create_dev_vlan_url = \"/imcrs/vlan?devId=\" + str(devid)\n f_url = url + create_dev_vlan_url\n payload = '''{\"vlanId\":\"%s\", \"vlanName\":\"%s\"}''' % (str(vlanid), vlan_name)\n response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 201:\n print('Vlan Created')\n return 201\n elif response.status_code == 409:\n print('''Unable to create VLAN.\\nVLAN Already Exists\\nDevice does not support VLAN\n function''')\n return 409\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" create_dev_vlan: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_dev_vlans(vlanid, auth, url, devid=None, devip=None):\n if devip is not None:\n devid = get_dev_details(devip, auth, url)['id']\n remove_dev_vlan_url = \"/imcrs/vlan/delvlan?devId=\" + str(devid) + \"&vlanId=\" + str(vlanid)\n f_url = url + remove_dev_vlan_url\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n print('Vlan deleted')\n return response.status_code\n elif response.status_code == 409:\n print('Unable to delete VLAN.\\nVLAN does not Exist\\nDevice does not support VLAN '\n 'function')\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_dev_vlans: An Error has occured\"", "response": "This function deletes a specific VLAN from the specified device and 802. 1q VLAN tag."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nuse the HouseCanary API Python Client to access the API", "response": "def _get_results_from_api(identifiers, endpoints, api_key, api_secret):\n \"\"\"Use the HouseCanary API Python Client to access the API\"\"\"\n\n if api_key is not None and api_secret is not None:\n client = housecanary.ApiClient(api_key, api_secret)\n else:\n client = housecanary.ApiClient()\n\n wrapper = getattr(client, endpoints[0].split('/')[0])\n\n if len(endpoints) > 1:\n # use component_mget to request multiple endpoints in one call\n return wrapper.component_mget(identifiers, endpoints)\n else:\n return wrapper.fetch_identifier_component(endpoints[0], identifiers)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef images():\n '''\n Show the list of registered images in this cluster.\n '''\n fields = [\n ('Name', 'name'),\n ('Registry', 'registry'),\n ('Tag', 'tag'),\n ('Digest', 'digest'),\n ('Size', 'size_bytes'),\n ('Aliases', 'aliases'),\n ]\n with Session() as session:\n try:\n items = session.Image.list(fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if len(items) == 0:\n print('There are no registered images.')\n return\n print(tabulate((item.values() for item in items),\n headers=(item[0] for item in fields),\n floatfmt=',.0f'))", "response": "Show the list of registered images in this cluster."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the kernel image metadata from all configured docker registries.", "response": "def rescan_images(registry):\n '''Update the kernel image metadata from all configured docker registries.'''\n with Session() as session:\n try:\n result = session.Image.rescanImages(registry)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if result['ok']:\n print(\"kernel image metadata updated\")\n else:\n print(\"rescanning failed: {0}\".format(result['msg']))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds an image alias.", "response": "def alias_image(alias, target):\n '''Add an image alias.'''\n with Session() as session:\n try:\n result = session.Image.aliasImage(alias, target)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if result['ok']:\n print(\"alias {0} created for target {1}\".format(alias, target))\n else:\n print(result['msg'])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove an image alias.", "response": "def dealias_image(alias):\n '''Remove an image alias.'''\n with Session() as session:\n try:\n result = session.Image.dealiasImage(alias)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if result['ok']:\n print(\"alias {0} removed.\".format(alias))\n else:\n print(result['msg'])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfunctions takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API", "response": "def get_dev_asset_details(ipaddress):\n \"\"\"Takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API\n :param ipaddress: IP address of the device you wish to gather the asset details\n :return: object of type list containing the device asset details\n \"\"\"\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_asset_url = \"/imcrs/netasset/asset?assetDevice.ip=\" + str(ipaddress)\n f_url = url + get_dev_asset_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n dev_asset_info = (json.loads(r.text))\n if len(dev_asset_info) > 0:\n dev_asset_info = dev_asset_info['netAsset']\n if type(dev_asset_info) == dict:\n dev_asset_info = [dev_asset_info]\n if type(dev_asset_info) == list:\n dev_asset_info[:] = [dev for dev in dev_asset_info if dev.get('deviceIp') == ipaddress]\n return dev_asset_info\n else:\n print(\"get_dev_asset_details: An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of dictionaries containing the serial numbers of the asset list.", "response": "def get_serial_numbers(assetList):\n \"\"\"\n Helper function: Uses return of get_dev_asset_details function to evaluate to evaluate for multipe serial objects.\n :param assetList: output of get_dev_asset_details function\n :return: the serial_list object of list type which contains one or more dictionaries of the asset details\n \"\"\"\n serial_list = []\n if type(assetList) == list:\n for i in assetList:\n if len(i['serialNum']) > 0:\n serial_list.append(i)\n return serial_list"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_trunk_interfaces(devId):\n\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_trunk_interfaces_url = \"/imcrs/vlan/trunk?devId=\" + str(devId) + \"&start=1&size=5000&total=false\"\n f_url = url + get_trunk_interfaces_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n dev_trunk_interfaces = (json.loads(r.text))\n if len(dev_trunk_interfaces) == 2:\n return dev_trunk_interfaces['trunkIf']\n else:\n dev_trunk_interfaces['trunkIf'] = [\"No trunk inteface\"]\n return dev_trunk_interfaces['trunkIf']", "response": "Function takes devId as input to HP IMC platform\n returns list of dictionaries containing of trunk interfaces configured as an 802. 1q trunk\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_device_access_interfaces(devId):\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_access_interface_vlan_url = \"/imcrs/vlan/access?devId=\" + str(devId) + \"&start=1&size=500&total=false\"\n f_url = url + get_access_interface_vlan_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n dev_access_interfaces = (json.loads(r.text))\n if len(dev_access_interfaces) == 2:\n return dev_access_interfaces['accessIf']\n else:\n dev_access_interfaces['accessIf'] = [\"No access inteface\"]\n return dev_access_interfaces['accessIf']\n else:\n print(\"get_device_access_interfaces: An Error has occured\")", "response": "Function takes devId as input to RESTFUL call to HP IMC platform\n function returns list of dictionaries containing interfaces configured as access ports\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_dev_details(ip_address):\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_details_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false&ip=\" + \\\n str(ip_address) + \"&start=0&size=1000&orderBy=id&desc=false&total=false\"\n f_url = url + get_dev_details_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n dev_details = (json.loads(r.text))\n if len(dev_details) == 0:\n print(\"Device not found\")\n return \"Device not found\"\n elif type(dev_details['device']) == list:\n for i in dev_details['device']:\n if i['ip'] == ip_address:\n dev_details = i\n return dev_details\n elif type(dev_details['device']) == dict:\n return dev_details['device']\n else:\n print(\"dev_details: An Error has occured\")", "response": "This function returns a dictionary of device details for a given IP address."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_dev_vlans(devId):\n\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_vlans_url = \"/imcrs/vlan?devId=\" + str(devId) + \"&start=0&size=5000&total=false\"\n f_url = url + get_dev_vlans_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n dev_details = (json.loads(r.text))['vlan']\n return dev_details\n elif r.status_code == 409:\n return [{'vlan': 'None'}]\n else:\n print(\"get_dev_vlans: An Error has occured\")", "response": "Function takes input of devId to issue RESTUL call to HP IMC\n API call to HP IMC\n API"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning takes devid as input to RESTFUL call to HP IMC platform function returns a list object which contains a dictionary per interface", "response": "def get_dev_interface(devid):\n \"\"\"\n Function takes devid as input to RESTFUL call to HP IMC platform\n :param devid: requires devid as the only input\n :return: list object which contains a dictionary per interface\n \"\"\"\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_interface_url = \"/imcrs/plat/res/device/\" + str(devid) + \\\n \"/interface?start=0&size=1000&desc=false&total=false\"\n f_url = url + get_dev_interface_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n int_list = (json.loads(r.text))['interface']\n return int_list\n else:\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_dev_run_config(devId):\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_run_url = \"/imcrs/icc/deviceCfg/\" + str(devId) + \"/currentRun\"\n f_url = url + get_dev_run_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # print (r.status_code)\n if r.status_code == 200:\n run_conf = (json.loads(r.text))['content']\n type(run_conf)\n if run_conf is None:\n return \"This features is no supported on this device\"\n else:\n return run_conf\n else:\n return \"This features is not supported on this device\"", "response": "This function returns the most current running config file for a specific device."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfunctioning takes the devId of a specific device and issues a RESTFUL call to get the most current startup config file as known by the HP IMC Base Platform ICC module for the target device. :param devId: int or str value of the target device :return: str which contains the entire content of the target device startup configuration. If the device is not currently supported in the HP IMC Base Platform ICC module, this call returns a string of \"This feature is not supported on this device\"", "response": "def get_dev_start_config(devId):\n \"\"\"\n function takes the devId of a specific device and issues a RESTFUL call to get the most current startup config\n file as known by the HP IMC Base Platform ICC module for the target device.\n :param devId: int or str value of the target device\n :return: str which contains the entire content of the target device startup configuration. If the device is not\n currently supported in the HP IMC Base Platform ICC module, this call returns a string of \"This feature is not\n supported on this device\"\n \"\"\"\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_run_url = \"/imcrs/icc/deviceCfg/\" + str(devId) + \"/currentStart\"\n f_url = url + get_dev_run_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n if r.status_code == 200:\n start_conf = (json.loads(r.text))['content']\n return start_conf\n else:\n # print (r.status_code)\n return \"This feature is not supported on this device\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_dev_alarms(devId):\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_dev_alarm_url = \"/imcrs/fault/alarm?operatorName=admin&deviceId=\" + \\\n str(devId) + \"&desc=false\"\n f_url = url + get_dev_alarm_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n if r.status_code == 200:\n dev_alarm = (json.loads(r.text))\n if 'alarm' in dev_alarm:\n return dev_alarm['alarm']\n else:\n return \"Device has no alarms\"", "response": "function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for that device."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfunction takes the ipAddress of a specific host and issues a RESTFUL call to get the device and interface that the target host is currently connected to.", "response": "def get_real_time_locate(ipAddress):\n \"\"\"\n function takes the ipAddress of a specific host and issues a RESTFUL call to get the device and interface that the\n target host is currently connected to.\n :param ipAddress: str value valid IPv4 IP address\n :return: dictionary containing hostIp, devId, deviceIP, ifDesc, ifIndex\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n real_time_locate_url = \"/imcrs/res/access/realtimeLocate?type=2&value=\" + str(ipAddress) + \"&total=false\"\n f_url = url + real_time_locate_url\n r = requests.get(f_url, auth=auth, headers=headers) # creates the URL using the payload variable as the contents\n if r.status_code == 200:\n return json.loads(r.text)['realtimeLocation']\n\n else:\n print(r.status_code)\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes devId of specific device and issues a RESTFUL call to get the IP MAC ARP list from the target device.", "response": "def get_ip_mac_arp_list(devId):\n \"\"\"\n function takes devid of specific device and issues a RESTFUL call to get the IP/MAC/ARP list from the target device.\n :param devId: int or str value of the target device.\n :return: list of dictionaries containing the IP/MAC/ARP list of the target device.\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n ip_mac_arp_list_url = \"/imcrs/res/access/ipMacArp/\" + str(devId)\n f_url = url + ip_mac_arp_list_url\n r = requests.get(f_url, auth=auth, headers=headers) # creates the URL using the payload variable as the contents\n if r.status_code == 200:\n macarplist = (json.loads(r.text))\n if len(macarplist) > 1:\n return macarplist['ipMacArp']\n else:\n return ['this function is unsupported']\n\n else:\n print(r.status_code)\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC. Optioanl Name input will return only the specified view.", "response": "def get_custom_views(name=None):\n \"\"\"\n function takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC. Optioanl Name input\n will return only the specified view.\n :param name: string containg the name of the desired custom view\n :return: list of dictionaries containing attributes of the custom views.\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n if name is None:\n get_custom_views_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=false'\n elif name is not None:\n get_custom_views_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&name='+ name + '&desc=false&total=false'\n f_url = url + get_custom_views_url\n r = requests.get(f_url, auth=auth, headers=headers) # creates the URL using the payload variable as the contents\n if r.status_code == 200:\n customviewlist = (json.loads(r.text))['customView']\n if type(customviewlist) is dict:\n customviewlist = [customviewlist]\n return customviewlist\n else:\n return customviewlist\n else:\n print(r.status_code)\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning creates a list of custom views from HPE IMC. Optioanl Name input is a list of custom views. It returns only the specified view.", "response": "def create_custom_views(name=None, upperview=None):\n \"\"\"\n function takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC. Optioanl Name input\n will return only the specified view.\n :param name: string containg the name of the desired custom view\n :return: list of dictionaries containing attributes of the custom views.\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n create_custom_views_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=falsee'\n f_url = url + create_custom_views_url\n if upperview is None:\n payload = '''{ \"name\": \"''' + name + '''\",\n \"upLevelSymbolId\" : \"\"}'''\n else:\n parentviewid = get_custom_views(upperview)[0]['symbolId']\n payload = '''{\n \"name\": \"'''+name+ '''\"upperview\" : \"'''+str(parentviewid)+'''\"}'''\n print (payload)\n r = requests.post(f_url, data = payload, auth=auth, headers=headers) # creates the URL using the payload variable as the contents\n if r.status_code == 201:\n return 'View ' + name +' created successfully'\n else:\n print(r.status_code)\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_dev_vlan(devid, vlanid, vlan_name):\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n create_dev_vlan_url = \"/imcrs/vlan?devId=\" + str(devid)\n f_url = url + create_dev_vlan_url\n payload = '''{ \"vlanId\": \"''' + str(vlanid) + '''\", \"vlanName\" : \"''' + str(vlan_name) + '''\"}'''\n r = requests.post(f_url, data=payload, auth=auth,\n headers=headers) # creates the URL using the payload variable as the contents\n print (r.status_code)\n if r.status_code == 201:\n print ('Vlan Created')\n return r.status_code\n elif r.status_code == 409:\n return '''Unable to create VLAN.\\nVLAN Already Exists\\nDevice does not support VLAN function'''\n else:\n print(\"An Error has occured\")", "response": "function creates a VLAN from a specific device and 802. 1q VLAN tag"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_dev_vlans(devid, vlanid):\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n remove_dev_vlan_url = \"/imcrs/vlan/delvlan?devId=\" + str(devid) + \"&vlanId=\" + str(vlanid)\n f_url = url + remove_dev_vlan_url\n payload = None\n r = requests.delete(f_url, auth=auth,\n headers=headers) # creates the URL using the payload variable as the contents\n print (r.status_code)\n if r.status_code == 204:\n print ('Vlan deleted')\n return r.status_code\n elif r.status_code == 409:\n print ('Unable to delete VLAN.\\nVLAN does not Exist\\nDevice does not support VLAN function')\n return r.status_code\n else:\n print(\"An Error has occured\")", "response": "function deletes the specified VLAN from the target device and 802. 1q VLAN tag."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction takest devid and ifindex of specific device and interface and issues a RESTFUL call to shut the specifie d interface on the target device.", "response": "def set_inteface_down(devid, ifindex):\n \"\"\"\n function takest devid and ifindex of specific device and interface and issues a RESTFUL call to \" shut\" the specifie\n d interface on the target device.\n :param devid: int or str value of the target device\n :param ifindex: int or str value of the target interface\n :return: HTTP status code 204 with no values.\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n set_int_down_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \"/down\"\n f_url = url + set_int_down_url\n payload = None\n r = requests.put(f_url, auth=auth,\n headers=headers) # creates the URL using the payload variable as the contents\n print(r.status_code)\n if r.status_code == 204:\n return r.status_code\n else:\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction takest devid and ifindex of specific device and interface and issues a RESTFUL call to undo shut the spec ified interface on the target device.", "response": "def set_inteface_up(devid, ifindex):\n \"\"\"\n function takest devid and ifindex of specific device and interface and issues a RESTFUL call to \"undo shut\" the spec\n ified interface on the target device.\n :param devid: int or str value of the target device\n :param ifindex: int or str value of the target interface\n :return: HTTP status code 204 with no values.\n \"\"\"\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n set_int_up_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \"/up\"\n f_url = url + set_int_up_url\n payload = None\n r = requests.put(f_url, auth=auth,\n headers=headers) # creates the URL using the payload variable as the contents\n print(r.status_code)\n if r.status_code == 204:\n return r.status_code\n else:\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction takes hostId as input to RESTFUL call to HP IMC function returns the VM Host information for the target hypervisor", "response": "def get_vm_host_info(hostId):\n \"\"\"\n function takes hostId as input to RESTFUL call to HP IMC\n :param hostId: int or string of HostId of Hypervisor host\n :return:list of dictionatires contraining the VM Host information for the target hypervisor\n \"\"\"\n global r\n if auth is None or url is None: # checks to see if the imc credentials are already available\n set_imc_creds()\n get_vm_host_info_url = \"/imcrs/vrm/host?hostId=\" + str(hostId)\n f_url = url + get_vm_host_info_url\n payload = None\n r = requests.get(f_url, auth=auth,\n headers=headers) # creates the URL using the payload variable as the contents\n # print(r.status_code)\n if r.status_code == 200:\n if len(r.text) > 0:\n return json.loads(r.text)\n elif r.status_code == 204:\n print(\"Device is not a supported Hypervisor\")\n return \"Device is not a supported Hypervisor\"\n else:\n print(\"An Error has occured\")"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_trap_definitions():\n # checks to see if the imc credentials are already available\n if auth is None or url is None:\n set_imc_creds()\n global r\n get_trap_def_url = \"/imcrs/fault/trapDefine/sync/query?enterpriseId=1.3.6.1.4.1.11&size=10000\"\n f_url = url + get_trap_def_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n # r.status_code\n if r.status_code == 200:\n trap_def_list = (json.loads(r.text))\n return trap_def_list['trapDefine']\n else:\n print(\"get_dev_asset_details: An Error has occured\")", "response": "function takes in no param as input to fetch SNMP TRAP definitions from HP IMC RESTFUL API\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rate_limits(self):\n if not self._rate_limits:\n self._rate_limits = utilities.get_rate_limits(self._response)\n return self._rate_limits", "response": "Returns list of rate limit information from the response"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_custom_views(auth, url, name=None):\n get_custom_view_url = None\n if name is None:\n get_custom_view_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false' \\\n '&total=false'\n elif name is not None:\n get_custom_view_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&name=' + \\\n name + '&desc=false&total=false'\n f_url = url + get_custom_view_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n custom_view_list = (json.loads(response.text))\n if 'customView' in custom_view_list:\n custom_view_list = custom_view_list['customView']\n if isinstance(custom_view_list, dict):\n custom_view_list = [custom_view_list]\n return custom_view_list\n else:\n return custom_view_list\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_custom_views: An Error has occured'", "response": "function returns a list of dictionaries containing attributes of the custom views of the HPE\n IMC."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_custom_view_details(name, auth, url):\n view_id = get_custom_views(auth, url, name=name)\n if view_id is None:\n return view_id\n view_id = get_custom_views(auth, url, name=name)[0]['symbolId']\n get_custom_view_details_url = '/imcrs/plat/res/view/custom/' + str(view_id)\n f_url = url + get_custom_view_details_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n current_devices = (json.loads(response.text))\n if 'device' in current_devices:\n if isinstance(current_devices['device'], dict):\n return [current_devices['device']]\n else:\n return current_devices['device']\n else:\n return []\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_custom_views: An Error has occured'", "response": "function returns a list of dictionaries of custom views from an HPE\n IMC"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC. Optional Name input will return only the specified view. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param name: string containg the name of the desired custom view :param upperview: str contraining the name of the desired parent custom view :return: str of creation results ( \"view \" + name + \"created successfully\" :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.groups import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") #Create L1 custom view >>> create_custom_views(auth.creds, auth.url, name='L1 View') 'View L1 View created successfully' >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View') >>> assert type(view_1) is list >>> assert view_1[0]['name'] == 'L1 View' #Create Nested custome view >>> create_custom_views(auth.creds, auth.url, name='L2 View', upperview='L1 View') 'View L2 View created successfully' >>> view_2 = get_custom_views( auth.creds, auth.url, name = 'L2 View') >>> assert type(view_2) is list >>> assert view_2[0]['name'] == 'L2 View'", "response": "def create_custom_views(auth, url, name=None, upperview=None):\n \"\"\"\n function takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC.\n Optional Name input will return only the specified view.\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param name: string containg the name of the desired custom view\n\n :param upperview: str contraining the name of the desired parent custom view\n\n :return: str of creation results ( \"view \" + name + \"created successfully\"\n\n :rtype: str\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.groups import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n #Create L1 custom view\n >>> create_custom_views(auth.creds, auth.url, name='L1 View')\n 'View L1 View created successfully'\n\n >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View')\n\n >>> assert type(view_1) is list\n\n >>> assert view_1[0]['name'] == 'L1 View'\n\n #Create Nested custome view\n >>> create_custom_views(auth.creds, auth.url, name='L2 View', upperview='L1 View')\n 'View L2 View created successfully'\n\n >>> view_2 = get_custom_views( auth.creds, auth.url, name = 'L2 View')\n\n >>> assert type(view_2) is list\n\n >>> assert view_2[0]['name'] == 'L2 View'\n\n \"\"\"\n create_custom_views_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false' \\\n '&total=false'\n f_url = url + create_custom_views_url\n if upperview is None:\n payload = '''{ \"name\": \"''' + name + '''\",\n \"upLevelSymbolId\" : \"\"}'''\n else:\n parentviewid = get_custom_views(auth, url, upperview)[0]['symbolId']\n payload = '''{ \"name\": \"''' + name + '''\",\n \"upLevelSymbolId\" : \"''' + str(parentviewid) + '''\"}'''\n response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 201:\n print('View ' + name + ' created successfully')\n return response.status_code\n elif response.status_code == 409:\n print(\"View \" + name + \" already exists\")\n return response.status_code\n else:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_custom_views: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_devs_custom_views(custom_view_name, dev_list, auth, url):\n view_id = get_custom_views(auth, url, name=custom_view_name)\n if view_id is None:\n print(\"View \" + custom_view_name + \" doesn't exist\")\n return view_id\n view_id = get_custom_views(auth, url, name=custom_view_name)[0]['symbolId']\n add_devs_custom_views_url = '/imcrs/plat/res/view/custom/' + str(view_id)\n device_list = []\n for dev in dev_list:\n new_dev = {\"id\": dev}\n device_list.append(new_dev)\n payload = '''{\"device\" : ''' + json.dumps(device_list) + '''}'''\n print(payload)\n f_url = url + add_devs_custom_views_url\n response = requests.put(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n print('View ' + custom_view_name + ' : Devices Successfully Added')\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' get_custom_views: An Error has occured'", "response": "This function creates a specific custom view from a list of devices discovered in the HPE IMC platform and issues a RESTFUL call to add the list of devices to the specific custom views."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning takes input of auth, url, and name and issues a RESTFUL call to delete a specific of custom views from HPE IMC. :param name: string containg the name of the desired custom view :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: str of creation results ( \"view \" + name + \"created successfully\" :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.groups import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> delete_custom_view(auth.creds, auth.url, name = \"L1 View\") 'View L1 View deleted successfully' >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View') >>> assert view_1 is None >>> delete_custom_view(auth.creds, auth.url, name = \"L2 View\") 'View L2 View deleted successfully' >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View') >>> assert view_2 is None", "response": "def delete_custom_view(auth, url, name):\n \"\"\"\n function takes input of auth, url, and name and issues a RESTFUL call to delete a specific\n of custom views from HPE\n IMC.\n :param name: string containg the name of the desired custom view\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: str of creation results ( \"view \" + name + \"created successfully\"\n\n :rtype: str\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.groups import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_custom_view(auth.creds, auth.url, name = \"L1 View\")\n 'View L1 View deleted successfully'\n\n >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View')\n\n >>> assert view_1 is None\n\n >>> delete_custom_view(auth.creds, auth.url, name = \"L2 View\")\n 'View L2 View deleted successfully'\n\n >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View')\n\n >>> assert view_2 is None\n\n \"\"\"\n view_id = get_custom_views(auth, url, name)\n if view_id is None:\n print(\"View \" + name + \" doesn't exists\")\n return view_id\n view_id = get_custom_views(auth, url, name)[0]['symbolId']\n delete_custom_view_url = '/imcrs/plat/res/view/custom/' + str(view_id)\n f_url = url + delete_custom_view_url\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n print('View ' + name + ' deleted successfully')\n return response.status_code\n else:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' delete_custom_view: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the token for make request.", "response": "def _get_token(self):\r\n \"\"\"\r\n Get token for make request. The The data obtained herein are used \r\n in the variable header.\r\n\r\n Returns:\r\n To perform the request, receive in return a dictionary\r\n with several keys. With this method only return the token\r\n as it will use it for subsequent requests, such as a \r\n sentence translate. Returns one string type.\r\n \"\"\"\r\n informations = self._set_format_oauth()\r\n oauth_url = \"https://datamarket.accesscontrol.windows.net/v2/OAuth2-13\"\r\n token = requests.post(oauth_url, informations).json()\r\n return token[\"access_token\"]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _set_format_oauth(self):\r\n format_oauth = urllib.parse.urlencode({\r\n 'client_id': self._client_id,\r\n 'client_secret': self._client_secret,\r\n 'scope': self._url_request,\r\n 'grant_type': self._grant_type\r\n }).encode(\"utf-8\")\r\n return format_oauth", "response": "Returns a dict that can be used to make the request for OAuth."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _make_request(self, params, translation_url, headers):\r\n resp = requests.get(translation_url, params=params, headers=headers)\r\n resp.encoding = \"UTF-8-sig\"\r\n result = resp.json()\r\n return result", "response": "Make a request to the specified URL and return the response."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dictionary of language information on the given text.", "response": "def detect_language(self, text):\r\n \"\"\"\r\n Params:\r\n ::text = Text for identify language.\r\n \r\n Returns:\r\n Returns language present on text.\r\n \"\"\"\r\n infos_translate = TextDetectLanguageModel(text).to_dict()\r\n mode_translate = TranslatorMode.Detect.value\r\n return self._get_content(infos_translate, mode_translate)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a list of languages present on array of texts.", "response": "def detect_languages(self, texts):\r\n \"\"\"\r\n Params:\r\n ::texts = Array of texts for detect languages\r\n\r\n Returns:\r\n Returns language present on array of text.\r\n \"\"\"\r\n text_list = TextUtils.format_list_to_send(texts)\r\n infos_translate = TextDetectLanguageModel(text_list).to_dict()\r\n texts_for_detect = TextUtils.change_key(infos_translate, \"text\",\r\n \"texts\", infos_translate[\"text\"])\r\n mode_translate = TranslatorMode.DetectArray.value\r\n return self._get_content(texts_for_detect, mode_translate)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntranslating a text to a language.", "response": "def translate(self, text, to_lang, from_lang=None, \r\n content_type=\"text/plain\", category=None):\r\n \"\"\"\r\n This method takes as a parameter the desired text to be translated\r\n and the language to which should be translated. To find the code \r\n for each language just go to the library home page.\r\n The parameter ::from_lang:: is optional because the api microsoft \r\n recognizes the language used in a sentence automatically.\r\n The parameter ::content_type:: defaults to \"text/plain\". In fact\r\n it can be of two types: the very \"text/plain\" or \"text/html\".\r\n By default the parameter ::category:: is defined as \"general\", \r\n we do not touch it.\r\n \"\"\"\r\n infos_translate = TextModel(text, to_lang,\r\n from_lang, content_type, category).to_dict()\r\n mode_translate = TranslatorMode.Translate.value\r\n return self._get_content(infos_translate, mode_translate)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions to get information for the target VM", "response": "def get_vm_host_info(hostip, auth, url):\n \"\"\"\n function takes hostId as input to RESTFUL call to HP IMC\n\n :param hostip: int or string of hostip of Hypervisor host\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: Dictionary contraining the information for the target VM host\n\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.vrm import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> host_info = get_vm_host_info('10.101.0.6', auth.creds, auth.url)\n\n >>> assert type(host_info) is dict\n\n >>> assert len(host_info) == 10\n\n >>> assert 'cpuFeg' in host_info\n\n >>> assert 'cpuNum' in host_info\n\n >>> assert 'devId' in host_info\n\n >>> assert 'devIp' in host_info\n\n >>> assert 'diskSize' in host_info\n\n >>> assert 'memory' in host_info\n\n >>> assert 'parentDevId' in host_info\n\n >>> assert 'porductFlag' in host_info\n\n >>> assert 'serverName' in host_info\n\n >>> assert 'vendor' in host_info\n\n \"\"\"\n hostid = get_dev_details(hostip, auth, url)['id']\n f_url = url + \"/imcrs/vrm/host?hostId=\" + str(hostid)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n if len(response.text) > 0:\n return json.loads(response.text)\n elif response.status_code == 204:\n print(\"Device is not a supported Hypervisor\")\n return \"Device is not a supported Hypervisor\"\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_vm_host_info: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfunctions takes no inputs and returns a list of dictionaries of all of the operators currently configured on the HPE IMC system", "response": "def get_plat_operator(auth, url,headers=HEADERS):\n '''\n Funtion takes no inputs and returns a list of dictionaties of all of the operators currently configured on the HPE\n IMC system\n :return: list of dictionaries\n '''\n get_operator_url = '/imcrs/plat/operator?start=0&size=1000&orderBy=id&desc=false&total=false'\n f_url = url + get_operator_url\n try:\n r = requests.get(f_url, auth=auth, headers=headers)\n plat_oper_list = json.loads(r.text)\n return plat_oper_list['operator']\n except requests.exceptions.RequestException as e:\n print (\"Error:\\n\" + str(e) + ' get_plat_operator: An Error has occured')\n return \"Error:\\n\" + str(e) + ' get_plat_operator: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions to set the password of an existing operator account", "response": "def delete_plat_operator(operator,auth, url, headers=HEADERS):\n \"\"\"\n Function to set the password of an existing operator\n :param operator: str Name of the operator account\n :param password: str New password\n :param url: str url of IMC server, see requests library docs for more info\n :param auth: str see requests library docs for more info\n :param headers: json formated string. default values set in module\n :return:\n \"\"\"\n #oper_id = None\n plat_oper_list = get_plat_operator(auth, url)\n for i in plat_oper_list:\n if operator == i['name']:\n oper_id = i['id']\n if oper_id == None:\n return(\"\\n User does not exist\")\n delete_plat_operator_url = \"/imcrs/plat/operator/\"\n f_url = url + delete_plat_operator_url + str(oper_id)\n r = requests.delete(f_url, auth=auth, headers=headers)\n try:\n if r.status_code == 204:\n print(\"\\n Operator: \" + operator +\n \" was successfully deleted\")\n return r.status_code\n except requests.exceptions.RequestException as e:\n print (\"Error:\\n\" + str(e) + ' delete_plat_operator: An Error has occured')\n return \"Error:\\n\" + str(e) + ' delete_plat_operator: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the index of the value in the list", "response": "def index(self, value):\n \"\"\"\n Args:\n value: index\n\n Returns: index of the values\n\n Raises:\n ValueError: value is not in list\n \"\"\"\n\n for i, x in enumerate(self):\n if x == value:\n return i\n\n raise ValueError(\"{} is not in list\".format(value))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting a dictionary update sequence element into a dictionary update sequence element that stems from self.", "response": "def to_d(self):\n \"\"\"\n Args: self\n\n Returns: a d that stems from self\n\n Raises:\n ValueError: dictionary update sequence element #index has length\n len(tuple); 2 is required\n\n TypeError: cannot convert dictionary update sequence element\n #index to a sequence\n \"\"\"\n\n try:\n return ww.d(self)\n except (TypeError, ValueError):\n\n for i, element in enumerate(self):\n try:\n iter(element)\n\n # TODO: find out why we can't cover this branch. The code\n # is tested but don't appear in coverage\n except TypeError: # pragma: no cover\n # TODO: use raise_from ?\n raise ValueError((\"'{}' (position {}) is not iterable. You\"\n \" can only create a dictionary from a \"\n \"elements that are iterables, such as \"\n \"tuples, lists, etc.\")\n .format(element, i))\n\n try:\n size = len(element)\n except TypeError: # ignore generators, it's already consummed\n pass\n else:\n raise ValueError((\"'{}' (position {}) contains {} \"\n \"elements. You can only create a \"\n \"dictionary from iterables containing \"\n \"2 elements.\").format(element, i, size))\n\n raise"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfunctions takes no input and returns a list of dictionaries containing the configuration templates in the root folder of the ICC configuration template library.", "response": "def get_cfg_template(auth, url, folder = None):\n '''\n Function takes no input and returns a list of dictionaries containing the configuration templates in the root folder\n of the icc configuration template library.\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: List of Dictionaries containing folders and configuration files in the ICC library.\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.icc import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> config_templates = get_cfg_template(auth.creds, auth.url)\n\n >>> assert type(config_templates) is list\n\n >>> assert 'confFileName' in config_templates[0]\n\n\n >>> config_templates_folder = get_cfg_template(auth.creds, auth.url, folder='ADP_Configs')\n\n >>> assert type(config_templates_folder) is list\n\n >>> assert 'confFileName' in config_templates_folder[0]\n\n >>> config_template_no_folder = get_cfg_template(auth.creds, auth.url, folder='Doesnt_Exist')\n\n >>> assert config_template_no_folder == None\n '''\n if folder == None:\n get_cfg_template_url = \"/imcrs/icc/confFile/list\"\n else:\n folder_id = get_folder_id(folder, auth, url)\n get_cfg_template_url = \"/imcrs/icc/confFile/list/\"+str(folder_id)\n f_url = url + get_cfg_template_url\n r = requests.get(f_url,auth=auth, headers=HEADERS)\n #print (r.status_code)\n try:\n if r.status_code == 200:\n cfg_template_list = (json.loads(r.text))\n return cfg_template_list['confFile']\n\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_cfg_template: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfunctions to create a configuration segment in the IMC RS.", "response": "def create_cfg_segment(filename, filecontent, description, auth, url):\n '''\n Takes a str into var filecontent which represents the entire content of a configuration segment, or partial\n configuration file. Takes a str into var description which represents the description of the configuration segment\n :param filename: str containing the name of the configuration segment.\n\n :param filecontent: str containing the entire contents of the configuration segment\n\n :param description: str contrianing the description of the configuration segment\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: If successful, Boolena of type True\n\n :rtype: Boolean\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.icc import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> filecontent = (\"\"\"sample file content\"\"\")\n >>> create_new_file = create_cfg_segment('CW7SNMP.cfg', filecontent, 'My New Template', auth.creds, auth.url)\n\n >>> template_id = get_template_id('CW7SNMP.cfg', auth.creds, auth.url)\n\n >>> assert type(template_id) is str\n\n >>>\n '''\n payload = {\"confFileName\": filename,\n \"confFileType\": \"2\",\n \"cfgFileParent\": \"-1\",\n \"confFileDesc\": description,\n \"content\": filecontent}\n create_cfg_segment_url = \"/imcrs/icc/confFile\"\n f_url = url + create_cfg_segment_url\n # creates the URL using the payload variable as the contents\n r = requests.post(f_url,data= (json.dumps(payload)), auth=auth, headers=HEADERS)\n try:\n if r.status_code == 201:\n return True\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" create_cfg_segment: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_template_id(template_name, auth, url):\n object_list = get_cfg_template(auth=auth, url=url)\n for object in object_list:\n if object['confFileName'] == template_name:\n return object['confFileId']\n return \"template not found\"", "response": "Helper function takes str input of folder name and returns str numerical id of the folder."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nuses the get_template_id() funct to gather the template_id to craft a url which is sent to the IMC server using a Delete Method :param template_name: str containing the entire contents of the configuration segment :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: If successful, Boolean of type True :rtype: Boolean >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.icc import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> delete_cfg_template('CW7SNMP.cfg', auth.creds, auth.url) True >>> get_template_id('CW7SNMP.cfg', auth.creds, auth.url) 'template not found'", "response": "def delete_cfg_template(template_name, auth, url):\n '''Uses the get_template_id() funct to gather the template_id to craft a url which is sent to the IMC server using\n a Delete Method\n :param template_name: str containing the entire contents of the configuration segment\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: If successful, Boolean of type True\n\n :rtype: Boolean\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.icc import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_cfg_template('CW7SNMP.cfg', auth.creds, auth.url)\n True\n\n >>> get_template_id('CW7SNMP.cfg', auth.creds, auth.url)\n 'template not found'\n\n '''\n file_id = get_template_id(template_name, auth, url)\n delete_cfg_template_url = \"/imcrs/icc/confFile/\"+str(file_id)\n f_url = url + delete_cfg_template_url\n # creates the URL using the payload variable as the contents\n r = requests.delete(f_url, auth=auth, headers=HEADERS)\n #print (r.status_code)\n try:\n if r.status_code == 204:\n return True\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" delete_cfg_template: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_folder_id(folder_name, auth, url):\n object_list = get_cfg_template(auth=auth, url=url)\n for object in object_list:\n if object['confFileName'] == folder_name:\n return object['confFileId']\n return \"Folder not found\"", "response": "This function returns str numerical id of the folder."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwraps the given text with bold enable or disable ANSI sequences.", "response": "def bold(text: str) -> str:\n '''\n Wraps the given text with bold enable/disable ANSI sequences.\n '''\n return (style(text, bold=True, reset=False) +\n style('', bold=False, reset=False))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def status(cls):\n '''\n Returns the current status of the configured API server.\n '''\n rqst = Request(cls.session, 'GET', '/manager/status')\n rqst.set_json({\n 'status': 'running',\n })\n async with rqst.fetch() as resp:\n return await resp.json()", "response": "Returns the current status of the configured API server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def freeze(cls, force_kill: bool = False):\n '''\n Freezes the configured API server.\n Any API clients will no longer be able to create new compute sessions nor\n create and modify vfolders/keypairs/etc.\n This is used to enter the maintenance mode of the server for unobtrusive\n manager and/or agent upgrades.\n\n :param force_kill: If set ``True``, immediately shuts down all running\n compute sessions forcibly. If not set, clients who have running compute\n session are still able to interact with them though they cannot create\n new compute sessions.\n '''\n rqst = Request(cls.session, 'PUT', '/manager/status')\n rqst.set_json({\n 'status': 'frozen',\n 'force_kill': force_kill,\n })\n async with rqst.fetch() as resp:\n assert resp.status == 204", "response": "Freezes the configured compute keypair store."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_dev_alarms(devId, auth=auth.creds, url=auth.url):\n # checks to see if the imc credentials are already available\n get_dev_alarm_url = \"/imcrs/fault/alarm?operatorName=admin&deviceId=\" + \\\n str(devId) + \"&desc=false\"\n f_url = url + get_dev_alarm_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=headers)\n try:\n if r.status_code == 200:\n dev_alarm = (json.loads(r.text))\n if 'alarm' in dev_alarm:\n return dev_alarm['alarm']\n else:\n return \"Device has no alarms\"\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_alarms: An Error has occured'", "response": "function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for that device."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upload(sess_id_or_alias, files):\n if len(files) < 1:\n return\n with Session() as session:\n try:\n print_wait('Uploading files...')\n kernel = session.Kernel(sess_id_or_alias)\n kernel.upload(files, show_progress=True)\n print_done('Uploaded.')\n except Exception as e:\n print_error(e)\n sys.exit(1)", "response": "Upload files to user s home folder."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndownload files from a running container.", "response": "def download(sess_id_or_alias, files, dest):\n \"\"\"\n Download files from a running container.\n\n \\b\n SESSID: Session ID or its alias given when creating the session.\n FILES: Paths inside container.\n \"\"\"\n if len(files) < 1:\n return\n with Session() as session:\n try:\n print_wait('Downloading file(s) from {}...'\n .format(sess_id_or_alias))\n kernel = session.Kernel(sess_id_or_alias)\n kernel.download(files, dest, show_progress=True)\n print_done('Downloaded to {}.'.format(dest.resolve()))\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists files in a running container.", "response": "def ls(sess_id_or_alias, path):\n \"\"\"\n List files in a path of a running container.\n\n \\b\n SESSID: Session ID or its alias given when creating the session.\n PATH: Path inside container.\n \"\"\"\n with Session() as session:\n try:\n print_wait('Retrieving list of files in \"{}\"...'.format(path))\n kernel = session.Kernel(sess_id_or_alias)\n result = kernel.list_files(path)\n\n if 'errors' in result and result['errors']:\n print_fail(result['errors'])\n sys.exit(1)\n\n files = json.loads(result['files'])\n table = []\n headers = ['file name', 'size', 'modified', 'mode']\n for file in files:\n mdt = datetime.fromtimestamp(file['mtime'])\n mtime = mdt.strftime('%b %d %Y %H:%M:%S')\n row = [file['filename'], file['size'], mtime, file['mode']]\n table.append(row)\n print_done('Retrived.')\n print('Path in container:', result['abspath'], end='')\n print(tabulate(table, headers=headers))\n except Exception as e:\n print_error(e)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef main():\n try:\n # Get the `src` directory's absolute path\n src_path = os.path.dirname(\n # `aoiklivereload` directory's absolute path\n os.path.dirname(\n # `demo` directory's absolute path\n os.path.dirname(\n # This file's absolute path\n os.path.abspath(__file__)\n )\n )\n )\n\n # If the `src` directory path is not in `sys.path`\n if src_path not in sys.path:\n # Add to `sys.path`.\n #\n # This aims to save user setting PYTHONPATH when running this demo.\n #\n sys.path.append(src_path)\n\n # Import reloader class\n from aoiklivereload import LiveReloader\n\n # Create reloader\n reloader = LiveReloader()\n\n # Start watcher thread\n reloader.start_watcher_thread()\n\n # Server host\n server_host = '0.0.0.0'\n\n # Server port\n server_port = 8000\n\n # Get message\n msg = '# ----- Run server -----\\nHost: {}\\nPort: {}'.format(\n server_host, server_port\n )\n\n # Print message\n print(msg)\n\n # Create Sanic app\n sanic_app = Sanic()\n\n # Create request handler\n @sanic_app.route('/')\n async def hello_handler(request): # pylint: disable=unused-variable\n \"\"\"\n Request handler.\n\n :return:\n Response body.\n \"\"\"\n # Return response body\n return text('hello')\n\n # Run server.\n #\n # Notice `KeyboardInterrupt` will be caught inside `sanic_app.run`.\n #\n sanic_app.run(\n host=server_host,\n port=server_port,\n )\n\n # If have `KeyboardInterrupt`\n except KeyboardInterrupt:\n # Not treat as error\n pass", "response": "Main function.\n\n :return:\n None."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_cfg_template(auth, url, folder=None):\n if folder is None:\n get_cfg_template_url = \"/imcrs/icc/confFile/list\"\n else:\n folder_id = get_folder_id(folder, auth, url)\n get_cfg_template_url = \"/imcrs/icc/confFile/list/\" + str(folder_id)\n f_url = url + get_cfg_template_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n cfg_template_list = (json.loads(response.text))['confFile']\n if isinstance(cfg_template_list, list):\n return cfg_template_list\n elif isinstance(cfg_template_list, dict):\n return [cfg_template_list]\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_cfg_template: An Error has occured\"", "response": "Function takes no input and returns a list of dictionaries containing the configuration files in the ICC configuration template library."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_cfg_segment(filename, filecontent, description, auth, url):\n payload = {\"confFileName\": filename,\n \"confFileType\": \"2\",\n \"cfgFileParent\": \"-1\",\n \"confFileDesc\": description,\n \"content\": filecontent}\n f_url = url + \"/imcrs/icc/confFile\"\n response = requests.post(f_url, data=(json.dumps(payload)), auth=auth, headers=HEADERS)\n try:\n if response.status_code == 201:\n print(\"Template successfully created\")\n return response.status_code\n elif response.status_code is not 201:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" create_cfg_segment: An Error has occured\"", "response": "Function to create a new configuration segment."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_folder_id(folder_name, auth, url):\n object_list = get_cfg_template(auth=auth, url=url)\n for template in object_list:\n if template['confFileName'] == folder_name:\n return int(template['confFileId'])\n return \"Folder not found\"", "response": "This function returns the folder id of the folder."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nuse the get_template_id() funct to gather the template_id to craft a url which is sent to the IMC server using a Delete Method :param template_name: str containing the entire contents of the configuration segment :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: If successful, return int of status.code 204. :rtype: Int >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.icc import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> delete_cfg_template('CW7SNMP.cfg', auth.creds, auth.url)", "response": "def delete_cfg_template(template_name, auth, url):\n \"\"\"Uses the get_template_id() funct to gather the template_id\n to craft a url which is sent to the IMC server using a Delete Method\n :param template_name: str containing the entire contents of the configuration segment\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: If successful, return int of status.code 204.\n\n :rtype: Int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.icc import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_cfg_template('CW7SNMP.cfg', auth.creds, auth.url)\n\n\n \"\"\"\n file_id = get_template_id(template_name, auth, url)\n f_url = url + \"/imcrs/icc/confFile/\" + str(file_id)\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n print(\"Template successfully Deleted\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_cfg_template: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_template_details(template_name, auth, url):\n file_id = get_template_id(template_name, auth, url)\n if isinstance(file_id, str):\n return file_id\n f_url = url + \"/imcrs/icc/confFile/\" + str(file_id)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n template_details = json.loads(response.text)\n return template_details\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_template_contents: An Error has occured\"", "response": "Uses the get_template_id method to gather the template_id to craft a\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning takes input of devId to issue RESTUL call to HP IMC to get the list of vlans on the devices.", "response": "def get_dev_vlans(devid, auth, url):\n \"\"\"Function takes input of devID to issue RESTUL call to HP IMC\n :param devid: requires devId as the only input parameter\n :return: dictionary of existing vlans on the devices. Device must be supported in HP IMC platform VLAN manager module\n \"\"\"\n\n # checks to see if the imc credentials are already available\n get_dev_vlans_url = \"/imcrs/vlan?devId=\" + str(devid) + \"&start=0&size=5000&total=false\"\n f_url = url + get_dev_vlans_url\n\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_vlans = (json.loads(r.text))\n return dev_vlans['vlan']\n elif r.status_code == 409:\n return {'vlan': 'no vlans'}\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_vlans: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_trunk_interfaces(devId, auth, url):\n\n # checks to see if the imc credentials are already available\n get_trunk_interfaces_url = \"/imcrs/vlan/trunk?devId=\" + str(devId) + \"&start=1&size=5000&total=false\"\n f_url = url + get_trunk_interfaces_url\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_trunk_interfaces = (json.loads(r.text))\n if len(dev_trunk_interfaces) == 2:\n return dev_trunk_interfaces['trunkIf']\n else:\n dev_trunk_interfaces['trunkIf'] = [\"No trunk inteface\"]\n return dev_trunk_interfaces['trunkIf']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_trunk_interfaces: An Error has occured'", "response": "Function takes devId as input to RESTFULL call to HP IMC platform\n and returns a list of dictionaries containing of trunk interfaces configured as an 802. 1q trunk ifeface"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes devId as input to RESTFUL call to HP IMC platform and returns the list of access interfaces configured as access ports", "response": "def get_device_access_interfaces(devId, auth, url):\n \"\"\"Function takes devId as input to RESTFUL call to HP IMC platform\n :param devId: requires deviceID as the only input parameter\n :return: list of dictionaries containing interfaces configured as access ports\n \"\"\"\n # checks to see if the imc credentials are already available\n get_access_interface_vlan_url = \"/imcrs/vlan/access?devId=\" + str(devId) + \"&start=1&size=500&total=false\"\n f_url = url + get_access_interface_vlan_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_access_interfaces = (json.loads(r.text))\n if len(dev_access_interfaces) == 2:\n return dev_access_interfaces['accessIf']\n else:\n dev_access_interfaces['accessIf'] = [\"No access inteface\"]\n return dev_access_interfaces['accessIf']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_device_access_interfaces: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfunction creates a VLAN from a specific device and 802. 1q VLAN tag", "response": "def create_dev_vlan(devid, vlanid, vlan_name, auth, url):\n \"\"\"\n function takes devid and vlanid vlan_name of specific device and 802.1q VLAN tag and issues a RESTFUL call to add the\n specified VLAN from the target device. VLAN Name MUST be valid on target device.\n :param devid: int or str value of the target device\n :param vlanid:int or str value of target 802.1q VLAN\n :param vlan_name: str value of the target 802.1q VLAN name. MUST be valid name on target device.\n :return:HTTP Status code of 201 with no values.\n \"\"\"\n create_dev_vlan_url = \"/imcrs/vlan?devId=\" + str(devid)\n f_url = url + create_dev_vlan_url\n payload = '''{ \"vlanId\": \"''' + str(vlanid) + '''\", \"vlanName\" : \"''' + str(vlan_name) + '''\"}'''\n r = requests.post(f_url, data=payload, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n return r\n try:\n\n if r.status_code == 201:\n print ('Vlan Created')\n return r.status_code\n elif r.status_code == 409:\n return '''Unable to create VLAN.\\nVLAN Already Exists\\nDevice does not support VLAN function'''\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" create_dev_vlan: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_dev_vlans(devid, vlanid, auth, url):\n remove_dev_vlan_url = \"/imcrs/vlan/delvlan?devId=\" + str(devid) + \"&vlanId=\" + str(vlanid)\n f_url = url + remove_dev_vlan_url\n payload = None\n r = requests.delete(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 204:\n print ('Vlan deleted')\n return r.status_code\n elif r.status_code == 409:\n print ('Unable to delete VLAN.\\nVLAN does not Exist\\nDevice does not support VLAN function')\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" delete_dev_vlans: An Error has occured\"", "response": "function deletes the specified VLAN from the target device and 802. 1q VLAN tag."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_operator(operator, auth, url):\n f_url = url + '/imcrs/plat/operator'\n payload = json.dumps(operator, indent=4)\n response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 409:\n return response.status_code\n elif response.status_code == 201:\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' create_operator: An Error has occured'", "response": "Function creates a new operator in the HPE IMC Restful API"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning to set the password of an existing operator", "response": "def set_operator_password(operator, password, auth, url):\n \"\"\"\n Function to set the password of an existing operator\n\n :param operator: str Name of the operator account\n\n :param password: str New password\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: int of 204 if successfull,\n\n :rtype: int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.operator import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> operator = { \"fullName\" : \"test administrator\", \"sessionTimeout\" : \"30\",\n \"password\" : \"password\",\"operatorGroupId\" : \"1\",\n \"name\" : \"testadmin\",\"desc\" : \"test admin account\",\n \"defaultAcl\" : \"\",\"authType\" : \"0\"}\n\n >>> new_operator = create_operator(operator, auth.creds, auth.url)\n\n >>> set_new_password = set_operator_password('testadmin', 'newpassword', auth.creds, auth.url)\n\n >>> assert type(set_new_password) is int\n\n >>> assert set_new_password == 204\n\n \"\"\"\n if operator is None:\n operator = input(\n '''\\n What is the username you wish to change the password?''')\n oper_id = ''\n authtype = None\n plat_oper_list = get_plat_operator(auth, url)\n for i in plat_oper_list:\n if i['name'] == operator:\n oper_id = i['id']\n authtype = i['authType']\n if oper_id == '':\n return \"User does not exist\"\n change_pw_url = \"/imcrs/plat/operator/\"\n f_url = url + change_pw_url + oper_id\n if password is None:\n password = input(\n '''\\n ============ Please input the operators new password:\\n ============ ''')\n payload = json.dumps({'password': password, 'authType': authtype})\n response = requests.put(f_url, data=payload, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n # print(\"Operator:\" + operator +\n # \" password was successfully changed\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' set_operator_password: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_plat_operator(auth, url):\n f_url = url + '/imcrs/plat/operator?start=0&size=1000&orderBy=id&desc=false&total=false'\n try:\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n plat_oper_list = json.loads(response.text)['operator']\n if isinstance(plat_oper_list, dict):\n oper_list = [plat_oper_list]\n return oper_list\n return plat_oper_list\n except requests.exceptions.RequestException as error:\n print(\"Error:\\n\" + str(error) + ' get_plat_operator: An Error has occured')\n return \"Error:\\n\" + str(error) + ' get_plat_operator: An Error has occured'", "response": "Function takes no inputs and returns a list of dictionaries where each element represents one operator currently holding the HPE IMC system and each element represents one of the operators currently holding the HPE IMC system."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning to set the password of an existing operator account", "response": "def delete_plat_operator(operator, auth, url):\n \"\"\"\n Function to set the password of an existing operator\n :param operator: str Name of the operator account\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: int of 204 if successfull\n\n :rtype: int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.operator import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> success_delete_operator = delete_plat_operator('testadmin', auth.creds, auth.url)\n\n >>> assert type(success_delete_operator) is int\n\n >>> assert success_delete_operator == 204\n\n >>> fail_delete_operator = delete_plat_operator('testadmin', auth.creds, auth.url)\n\n >>> assert type(fail_delete_operator) is int\n\n >>> assert fail_delete_operator == 409\n\n \"\"\"\n oper_id = None\n plat_oper_list = get_plat_operator(auth, url)\n for i in plat_oper_list:\n if operator == i['name']:\n oper_id = i['id']\n else:\n oper_id = None\n if oper_id is None:\n # print (\"User does not exist\")\n return 409\n f_url = url + \"/imcrs/plat/operator/\" + str(oper_id)\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n # print(\"Operator: \" + operator +\n # \" was successfully deleted\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + ' delete_plat_operator: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef execute_request(self, url, http_method, query_params, post_data):\n\n response = requests.request(http_method, url, params=query_params,\n auth=self._auth, json=post_data,\n headers={'User-Agent': USER_AGENT})\n\n if isinstance(self._output_generator, str) and self._output_generator.lower() == \"json\":\n # shortcut for just getting json back\n return response.json()\n elif self._output_generator is not None:\n return self._output_generator.process_response(response)\n else:\n return response", "response": "Makes a request to the specified url endpoint with the specified http method params and post data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmaking a POST request to the specified url endpoint.", "response": "def post(self, url, post_data, query_params=None):\n \"\"\"Makes a POST request to the specified url endpoint.\n\n Args:\n url (string): The url to the API without query params.\n Example: \"https://api.housecanary.com/v2/property/value\"\n post_data: Json post data to send in the body of the request.\n query_params (dict): Optional. Dictionary of query params to add to the request.\n\n Returns:\n The result of calling this instance's OutputGenerator process_response method\n on the requests.Response object.\n If no OutputGenerator is specified for this instance, returns the requests.Response.\n \"\"\"\n if query_params is None:\n query_params = {}\n\n return self.execute_request(url, \"POST\", query_params, post_data)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning which takes in a CSV file and creates a list of custom voices for each item in the CSV file.", "response": "def import_custom_views(filename):\n \"\"\"\n Function which takes in a csv files as input to the create_custom_views function from the pyhpeimc python module\n available at https://github.com/HPNetworking/HP-Intelligent-Management-Center\n :param filename: user-defined filename which contains two column named \"name\" and \"upperview\" as input into the\n create_custom_views function from the pyhpeimc module.\n :return: returns output of the create_custom_vies function (str) for each item in the CSV file.\n \"\"\"\n with open(filename) as csvfile:\n # decodes file as csv as a python dictionary\n reader = csv.DictReader(csvfile)\n for view in reader:\n # loads each row of the CSV as a JSON string\n name = view['name']\n upperview = view['upperview']\n if len(upperview) is 0:\n upperview = None\n create_custom_views(name,upperview,auth=auth.creds, url=auth.url)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction takes the ipAddress of a specific host and issues a RESTFUL call to get the device and interface that the target host is currently connected to. Note: Although intended to return a single location, Multiple locations may be returned for a single host due to a partially discovered network or misconfigured environment. :param host_ipaddress: str value valid IPv4 IP address :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents the location of the target host :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.termaccess import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> found_device = get_real_time_locate('10.101.0.51', auth.creds, auth.url) >>> assert type(found_device) is list >>> assert 'deviceId' in found_device[0] >>> assert 'deviceId' in found_device[0] >>> assert 'deviceId' in found_device[0] >>> assert 'deviceId' in found_device[0] >>> no_device = get_real_time_locate('192.168.254.254', auth.creds, auth.url) >>> assert type(no_device) is dict >>> assert len(no_device) == 0", "response": "def get_real_time_locate(host_ipaddress, auth, url):\n \"\"\"\n function takes the ipAddress of a specific host and issues a RESTFUL call to get the device and\n interface that the target host is currently connected to. Note: Although intended to return a\n single location, Multiple locations may be returned for a single host due to a partially\n discovered network or misconfigured environment.\n\n :param host_ipaddress: str value valid IPv4 IP address\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries where each element of the list represents the location of the\n target host\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> found_device = get_real_time_locate('10.101.0.51', auth.creds, auth.url)\n\n >>> assert type(found_device) is list\n\n >>> assert 'deviceId' in found_device[0]\n\n >>> assert 'deviceId' in found_device[0]\n\n >>> assert 'deviceId' in found_device[0]\n\n >>> assert 'deviceId' in found_device[0]\n\n >>> no_device = get_real_time_locate('192.168.254.254', auth.creds, auth.url)\n\n >>> assert type(no_device) is dict\n\n >>> assert len(no_device) == 0\n\n \"\"\"\n f_url = url + \"/imcrs/res/access/realtimeLocate?type=2&value=\" + str(host_ipaddress) + \\\n \"&total=false\"\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n response = json.loads(response.text)\n if 'realtimeLocation' in response:\n real_time_locate = response['realtimeLocation']\n if isinstance(real_time_locate, dict):\n real_time_locate = [real_time_locate]\n return real_time_locate\n else:\n return json.loads(response)['realtimeLocation']\n else:\n print(\"Host not found\")\n return 403\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_real_time_locate: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ip_mac_arp_list(auth, url, devid=None, devip=None):\n if devip is not None:\n dev_details = get_dev_details(devip, auth, url)\n if isinstance(dev_details, str):\n print(\"Device not found\")\n return 403\n else:\n devid = get_dev_details(devip, auth, url)['id']\n f_url = url + \"/imcrs/res/access/ipMacArp/\" + str(devid)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n ipmacarplist = (json.loads(response.text))\n if 'ipMacArp' in ipmacarplist:\n return ipmacarplist['ipMacArp']\n else:\n return ['this function is unsupported']\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_ip_mac_arp_list: An Error has occured\"", "response": "function takes devid of specific device and issues a RESTFUL call to get the IP MAC ARP list of the target device."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ip_scope(auth, url, scopeid=None, ):\n if scopeid is None:\n get_ip_scope_url = \"/imcrs/res/access/assignedIpScope\"\n else:\n get_ip_scope_url = \"/imcrs/res/access/assignedIpScope/ip?ipScopeId=\" + str(scopeid)\n\n f_url = url + get_ip_scope_url\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n ipscopelist = (json.loads(response.text))['assignedIpScope']\n if isinstance(ipscopelist, list):\n return ipscopelist\n elif isinstance(ipscopelist, dict):\n return [ipscopelist]\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_ip_scope: An Error has occured\"", "response": "function returns all IP address scopes currently configured on the HPE\n IMC server"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfunctions to add new Ip Scope to terminal access in HPE IMC base platform", "response": "def add_ip_scope(name, description, auth, url, startip=None, endip=None, network_address=None):\n \"\"\"\n Function takes input of four strings Start Ip, endIp, name, and description to add new Ip Scope\n to terminal access in the HPE IMC base platform\n\n :param name: str Name of the owner of this IP scope ex. 'admin'\n\n :param description: str description of the Ip scope\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param startip: str Start of IP address scope ex. '10.101.0.1'\n\n :param endip: str End of IP address scope ex. '10.101.0.254'\n\n :param network_address: ipv4 network address + subnet bits of target scope\n\n :return: 200 if successfull\n\n :rtype:\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> delete_ip_scope('10.50.0.0/24', auth.creds, auth.url)\n \n\n >>> new_scope = add_ip_scope('10.50.0.1', '10.50.0.254', 'cyoung', 'test group', auth.creds, auth.url)\n\n >>> assert type(new_scope) is int\n\n >>> assert new_scope == 200\n\n >>> existing_scope = add_ip_scope('10.50.0.1', '10.50.0.254', 'cyoung', 'test group', auth.creds, auth.url)\n\n >>> assert type(existing_scope) is int\n\n >>> assert existing_scope == 409\n\n\n \"\"\"\n if network_address is not None:\n nw_address = ipaddress.IPv4Network(network_address)\n startip = nw_address[1]\n endip = nw_address[-2]\n f_url = url + \"/imcrs/res/access/assignedIpScope\"\n payload = ('''{ \"startIp\": \"%s\", \"endIp\": \"%s\",\"name\": \"%s\",\"description\": \"%s\" }'''\n % (str(startip), str(endip), str(name), str(description)))\n response = requests.post(f_url, auth=auth, headers=HEADERS, data=payload)\n try:\n if response.status_code == 200:\n # print(\"IP Scope Successfully Created\")\n return response.status_code\n elif response.status_code == 409:\n # print (\"IP Scope Already Exists\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" add_ip_scope: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_ip_scope(network_address, auth, url):\n scope_id = get_scope_id(network_address, auth, url)\n if scope_id == \"Scope Doesn't Exist\":\n return scope_id\n f_url = url + '''/imcrs/res/access/assignedIpScope/''' + str(scope_id)\n response = requests.delete(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 204:\n # print(\"IP Segment Successfully Deleted\")\n return 204\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" delete_ip_scope: An Error has occured\"", "response": "Function to delete an entire IP segment from the IMC IP Address management under terminal access"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_scope_ip(hostipaddress, name, description, auth, url, scopeid=None, network_address=None):\n if network_address is not None:\n scopeid = get_scope_id(network_address, auth, url)\n if scopeid == \"Scope Doesn't Exist\":\n return scopeid\n new_ip = {\"ip\": hostipaddress,\n \"name\": name,\n \"description\": description}\n f_url = url + '/imcrs/res/access/assignedIpScope/ip?ipScopeId=' + str(scopeid)\n payload = json.dumps(new_ip)\n response = requests.post(f_url, auth=auth, headers=HEADERS, data=payload)\n try:\n if response.status_code == 200:\n # print(\"IP Host Successfully Created\")\n return response.status_code\n elif response.status_code == 409:\n # print(\"IP Host Already Exists\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" add_ip_scope: An Error has occured\"", "response": "Function to add new host IP address allocation to existing scope ID"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef remove_scope_ip(hostid, auth, url):\n f_url = url + '/imcrs/res/access/assignedIpScope/ip/' + str(hostid)\n response = requests.delete(f_url, auth=auth, headers=HEADERS, )\n try:\n if response.status_code == 204:\n # print(\"Host Successfully Deleted\")\n return response.status_code\n elif response.status_code == 409:\n # print(\"IP Scope Already Exists\")\n return response.status_code\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" add_ip_scope: An Error has occured\"", "response": "Function to add remove IP address allocation to the base url of the IMC RS interface"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction returns list of allocated IP address for the specified scope ID and network address", "response": "def get_ip_scope_hosts(auth, url, scopeid=None, network_address=None):\n \"\"\"\n Function requires input of scope ID and returns list of allocated IP address for the\n specified scope\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param scopeid: Integer of the desired scope id\n\n :param network_address: ipv4 network address + subnet bits of target scope\n\n :return: list of dictionary objects where each element of the list represents a single host\n assigned to the IP scope\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.termaccess import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> scope_id = get_scope_id('10.50.0.0/24', auth.creds, auth.url)\n\n >>> ip_scope_hosts = get_ip_scope_hosts(scope_id, auth.creds, auth.url)\n\n >>> assert type(ip_scope_hosts) is list\n\n >>> assert 'name' in ip_scope_hosts[0]\n\n >>> assert 'description' in ip_scope_hosts[0]\n\n >>> assert 'ip' in ip_scope_hosts[0]\n\n >>> assert 'id' in ip_scope_hosts[0]\n\n \"\"\"\n if network_address is not None:\n scopeid = get_scope_id(network_address, auth, url)\n if scopeid == \"Scope Doesn't Exist\":\n return scopeid\n f_url = url + \"/imcrs/res/access/assignedIpScope/ip?size=10000&ipScopeId=\" + str(scopeid)\n response = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if response.status_code == 200:\n ipscopelist = (json.loads(response.text))\n if ipscopelist == {}:\n return [ipscopelist]\n else:\n ipscopelist = ipscopelist['assignedIpInfo']\n if isinstance(ipscopelist, dict):\n ipscope = [ipscopelist]\n return ipscope\n return ipscopelist\n except requests.exceptions.RequestException as error:\n return \"Error:\\n\" + str(error) + \" get_ip_scope: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef allow_attribute_comments(chain, node):\n\n # TODO: find the relevant citation for why this is the correct way to comment attributes\n if isinstance(node.previous_sibling(), astroid.Assign) and \\\n isinstance(node.parent, (astroid.Class, astroid.Module)) and \\\n isinstance(node.value, astroid.Const) and \\\n isinstance(node.value.value, BASESTRING):\n return\n\n chain()", "response": "Augment the chain to allow comments on class attributes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef download(self, url, path, name_audio):\n if path is not None:\n with open(str(path+name_audio), 'wb') as handle:\n response = requests.get(url, stream = True)\n if not response.ok:\n raise Exception(\"Error in audio download.\")\n for block in response.iter_content(1024):\n if not block:\n break\n handle.write(block)", "response": "Download the audio file from the url and save it to the path."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_dev_asset_details(ipaddress, auth, url):\n get_dev_asset_url = \"/imcrs/netasset/asset?assetDevice.ip=\" + str(ipaddress)\n f_url = url + get_dev_asset_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_asset_info = (json.loads(r.text))\n if len(dev_asset_info) > 0:\n dev_asset_info = dev_asset_info['netAsset']\n if type(dev_asset_info) == dict:\n dev_asset_info = [dev_asset_info]\n if type(dev_asset_info) == list:\n dev_asset_info[:] = [dev for dev in dev_asset_info if dev.get('deviceIp') == ipaddress]\n return dev_asset_info\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_asset_details: An Error has occured'", "response": "Takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction takes no input to fetch device assett details from HP IMC RESTFUL API and returns a list of device asset details", "response": "def get_dev_asset_details_all(auth, url):\n \"\"\"Takes no input to fetch device assett details from HP IMC RESTFUL API\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionatires containing the device asset details\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.netassets import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> all_assets = get_dev_asset_details_all( auth.creds, auth.url)\n\n >>> assert type(all_assets) is list\n\n >>> assert 'asset' in all_assets[0]\n\n \"\"\"\n get_dev_asset_details_all_url = \"/imcrs/netasset/asset?start=0&size=15000\"\n f_url = url + get_dev_asset_details_all_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_asset_info = (json.loads(r.text))['netAsset']\n return dev_asset_info\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_dev_asset_details: An Error has occured'"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef check_database_connected(db):\n from sqlalchemy.exc import DBAPIError, SQLAlchemyError\n\n errors = []\n try:\n with db.engine.connect() as connection:\n connection.execute('SELECT 1;')\n except DBAPIError as e:\n msg = 'DB-API error: {!s}'.format(e)\n errors.append(Error(msg, id=health.ERROR_DB_API_EXCEPTION))\n except SQLAlchemyError as e:\n msg = 'Database misconfigured: \"{!s}\"'.format(e)\n errors.append(Error(msg, id=health.ERROR_SQLALCHEMY_EXCEPTION))\n return errors", "response": "Check if the connection to the configured default database backend succeeds."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef check_migrations_applied(migrate):\n errors = []\n\n from alembic.migration import MigrationContext\n from alembic.script import ScriptDirectory\n from sqlalchemy.exc import DBAPIError, SQLAlchemyError\n\n # pass in Migrate.directory here explicitly to be compatible with\n # older versions of Flask-Migrate that required the directory to be passed\n config = migrate.get_config(directory=migrate.directory)\n script = ScriptDirectory.from_config(config)\n\n try:\n with migrate.db.engine.connect() as connection:\n context = MigrationContext.configure(connection)\n db_heads = set(context.get_current_heads())\n script_heads = set(script.get_heads())\n except (DBAPIError, SQLAlchemyError) as e:\n msg = \"Can't connect to database to check migrations: {!s}\".format(e)\n return [Info(msg, id=health.INFO_CANT_CHECK_MIGRATIONS)]\n\n if db_heads != script_heads:\n msg = \"Unapplied migrations found: {}\".format(', '.join(script_heads))\n errors.append(Warning(msg, id=health.WARNING_UNAPPLIED_MIGRATION))\n return errors", "response": "Check if all migrations have been applied correctly."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef check_redis_connected(client):\n import redis\n errors = []\n\n try:\n result = client.ping()\n except redis.ConnectionError as e:\n msg = 'Could not connect to redis: {!s}'.format(e)\n errors.append(Error(msg, id=health.ERROR_CANNOT_CONNECT_REDIS))\n except redis.RedisError as e:\n errors.append(Error('Redis error: \"{!s}\"'.format(e),\n id=health.ERROR_REDIS_EXCEPTION))\n else:\n if not result:\n errors.append(Error('Redis ping failed',\n id=health.ERROR_REDIS_PING_FAILED))\n return errors", "response": "Check Redis connection and see if it responds to the PING command."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_custom_views(auth, url,name=None,headers=HEADERS):\n if name is None:\n get_custom_view_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=false'\n elif name is not None:\n get_custom_view_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&name='+name+'&desc=false&total=false'\n f_url = url + get_custom_view_url\n r = requests.get(f_url, auth=auth, headers=headers)\n try:\n if r.status_code == 200:\n custom_view_list = (json.loads(r.text))[\"customView\"]\n if type(custom_view_list) == dict:\n custom_view_list = [custom_view_list]\n return custom_view_list\n else:\n return custom_view_list\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_custom_views: An Error has occured'", "response": "function takes input and returns a list of dictionaries of custom views from an HPE IMC."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_custom_views(auth, url,name=None, upperview=None):\n create_custom_views_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=false'\n f_url = url + create_custom_views_url\n if upperview is None:\n payload = '''{ \"name\": \"''' + name + '''\",\n \"upLevelSymbolId\" : \"\"}'''\n print (payload)\n else:\n parentviewid = get_custom_views(auth, url, upperview)[0]['symbolId']\n payload = '''{ \"name\": \"'''+name+ '''\",\n \"upLevelSymbolId\" : \"'''+str(parentviewid)+'''\"}'''\n print (payload)\n r = requests.post(f_url, data = payload, auth=auth, headers=HEADERS) # creates the URL using the payload variable as the contents\n try:\n if r.status_code == 201:\n return 'View ' + name +' created successfully'\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + ' get_custom_views: An Error has occured'", "response": "function takes no input and issues a RESTFUL call to get a list of custom views from HPE IMC. Optioanl Name input is only return the specified view."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfunctioning takes the devId of a specific device and issues a RESTFUL call to get the most current running config for that device.", "response": "def get_dev_run_config(devid, auth, url):\n \"\"\"\n function takes the devId of a specific device and issues a RESTFUL call to get the most current running config\n file as known by the HP IMC Base Platform ICC module for the target device.\n :param devid: int or str value of the target device\n :return: str which contains the entire content of the target device running configuration. If the device is not\n currently supported in the HP IMC Base Platform ICC module, this call returns a string of \"This feature is not\n supported on this device\"\n \"\"\"\n # checks to see if the imc credentials are already available\n get_dev_run_url = \"/imcrs/icc/deviceCfg/\" + str(devid) + \"/currentRun\"\n f_url = url + get_dev_run_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # print (r.status_code)\n try:\n if r.status_code == 200:\n try:\n run_conf = (json.loads(r.text))['content']\n return run_conf\n except:\n return \"This features is no supported on this device\"\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_run_config: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_dev_start_config(devId, auth, url):\n # checks to see if the imc credentials are already available\n get_dev_run_url = \"/imcrs/icc/deviceCfg/\" + str(devId) + \"/currentStart\"\n f_url = url + get_dev_run_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n try:\n if r.status_code == 200:\n try:\n start_conf = (json.loads(r.text))['content']\n return start_conf\n except:\n return \"Start Conf not supported on this device\"\n\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_run_config: An Error has occured\"", "response": "function takes the devId of a specific device and issues a RESTFUL call to get the most current startup configuration for the device."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfunctions takest devid and ifindex of specific device and interface and issues a RESTFUL call to undo shut the spec ified interface on the target device.", "response": "def set_inteface_up(devid, ifindex, auth, url):\n \"\"\"\n function takest devid and ifindex of specific device and interface and issues a RESTFUL call to \"undo shut\" the spec\n ified interface on the target device.\n :param devid: int or str value of the target device\n :param ifindex: int or str value of the target interface\n :return: HTTP status code 204 with no values.\n \"\"\"\n set_int_up_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \"/up\"\n f_url = url + set_int_up_url\n try:\n r = requests.put(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n if r.status_code == 204:\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" set_inteface_up: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef vfolders(access_key):\n '''\n List and manage virtual folders.\n '''\n fields = [\n ('Name', 'name'),\n ('Created At', 'created_at'),\n ('Last Used', 'last_used'),\n ('Max Files', 'max_files'),\n ('Max Size', 'max_size'),\n ]\n if access_key is None:\n q = 'query { vfolders { $fields } }'\n else:\n q = 'query($ak:String) { vfolders(access_key:$ak) { $fields } }'\n q = q.replace('$fields', ' '.join(item[1] for item in fields))\n v = {'ak': access_key}\n with Session() as session:\n try:\n resp = session.Admin.query(q, v)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n print(tabulate((item.values() for item in resp['vfolders']),\n headers=(item[0] for item in fields)))", "response": "List and manage virtual folders."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndisplays the current configuration.", "response": "def config():\n '''\n Shows the current configuration.\n '''\n config = get_config()\n print('Client version: {0}'.format(click.style(__version__, bold=True)))\n print('API endpoint: {0}'.format(click.style(str(config.endpoint), bold=True)))\n print('API version: {0}'.format(click.style(config.version, bold=True)))\n print('Access key: \"{0}\"'.format(click.style(config.access_key, bold=True)))\n masked_skey = config.secret_key[:6] + ('*' * 24) + config.secret_key[-10:]\n print('Secret key: \"{0}\"'.format(click.style(masked_skey, bold=True)))\n print('Signature hash type: {0}'.format(\n click.style(config.hash_type, bold=True)))\n print('Skip SSL certificate validation? {0}'.format(\n click.style(str(config.skip_sslcert_validation), bold=True)))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning takes string no input to issue RESTUL call to HP IMC API", "response": "def get_system_vendors(auth, url):\n \"\"\"Takes string no input to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries where each dictionary represents a single vendor\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> vendors = get_system_vendors(auth.creds, auth.url)\n\n >>> assert type(vendors) is list\n\n >>> assert 'name' in vendors[0]\n\n\n \"\"\"\n get_system_vendors_url = '/imcrs/plat/res/vendor?start=0&size=10000&orderBy=id&desc=false&total=false'\n f_url = url + get_system_vendors_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n system_vendors = (json.loads(r.text))\n return system_vendors['deviceVendor']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning takes string no input to issue RESTUL call to HP IMC API", "response": "def get_system_category(auth, url):\n \"\"\"Takes string no input to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries where each dictionary represents a single device category\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> categories = get_system_category(auth.creds, auth.url)\n\n >>> assert type(categories) is list\n\n >>> assert 'name' in categories[0]\n\n\n \"\"\"\n get_system_category_url = '/imcrs/plat/res/category?start=0&size=10000&orderBy=id&desc=false&total=false'\n f_url = url + get_system_category_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n system_category = (json.loads(r.text))\n return system_category['deviceCategory']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfunctioning takes string no input to issue RESTUL call to HP IMC API", "response": "def get_system_device_models(auth, url):\n \"\"\"Takes string no input to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries where each dictionary represents a single device model\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> device_models = get_system_device_models(auth.creds, auth.url)\n\n >>> assert type(device_models) is list\n\n >>> assert 'virtualDeviceName' in device_models[0]\n\n \"\"\"\n get_system_device_model_url = '/imcrs/plat/res/model?start=0&size=10000&orderBy=id&desc=false&total=false'\n f_url = url + get_system_device_model_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n system_device_model = (json.loads(r.text))\n return system_device_model['deviceModel']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfunctioning takes string no input to issue RESTUL call to HP IMC API", "response": "def get_system_series(auth, url):\n \"\"\"Takes string no input to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dictionaries where each dictionary represents a single device series\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> series = get_system_series(auth.creds, auth.url)\n\n >>> assert type(series) is list\n\n >>> assert 'name' in series[0]\n\n\n\n \"\"\"\n get_system_series_url = '/imcrs/plat/res/series?managedOnly=false&start=0&size=10000&orderBy=id&desc=false&total=false'\n f_url = url + get_system_series_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n system_series = (json.loads(r.text))\n return system_series['deviceSeries']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_series: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntake string input of IP address to issue RESTUL call to HP IMC\\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param network_address= str IPv4 Network Address :return: dictionary of device details :rtype: dict >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.device import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> dev_list = get_all_devs( auth.creds, auth.url, network_address= '10.11.') >>> assert type(dev_list) is list >>> assert 'sysName' in dev_list[0]", "response": "def get_all_devs(auth, url, network_address= None):\n \"\"\"Takes string input of IP address to issue RESTUL call to HP IMC\\n\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :param network_address= str IPv4 Network Address\n\n :return: dictionary of device details\n\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> dev_list = get_all_devs( auth.creds, auth.url, network_address= '10.11.')\n\n >>> assert type(dev_list) is list\n\n >>> assert 'sysName' in dev_list[0]\n\n\n \"\"\"\n\n if network_address != None:\n get_all_devs_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false&ip=\" + \\\n str(network_address) + \"&start=0&size=1000&orderBy=id&desc=false&total=false\"\n else:\n get_all_devs_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false&start=0&size=1000&orderBy=id&desc=false&total=false&exact=false\"\n\n f_url = url + get_all_devs_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_details = (json.loads(r.text))\n if len(dev_details) == 0:\n print(\"Device not found\")\n return \"Device not found\"\n else:\n return dev_details['device']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntaking string input of IP address to issue RESTUL call to HP IMC\\n :param ip_address: string object of dotted decimal notation of IPv4 address :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: dictionary of device details :rtype: dict >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.device import * >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\") >>> dev_1 = get_dev_details('10.101.0.221', auth.creds, auth.url) >>> assert type(dev_1) is dict >>> assert 'sysName' in dev_1 >>> dev_2 = get_dev_details('8.8.8.8', auth.creds, auth.url) Device not found >>> assert type(dev_2) is str", "response": "def get_dev_details(ip_address, auth, url):\n \"\"\"Takes string input of IP address to issue RESTUL call to HP IMC\\n\n\n :param ip_address: string object of dotted decimal notation of IPv4 address\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: dictionary of device details\n\n :rtype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> dev_1 = get_dev_details('10.101.0.221', auth.creds, auth.url)\n\n >>> assert type(dev_1) is dict\n\n >>> assert 'sysName' in dev_1\n\n >>> dev_2 = get_dev_details('8.8.8.8', auth.creds, auth.url)\n Device not found\n\n >>> assert type(dev_2) is str\n\n\n \"\"\"\n\n get_dev_details_url = \"/imcrs/plat/res/device?resPrivilegeFilter=false&ip=\" + \\\n str(ip_address) + \"&start=0&size=1000&orderBy=id&desc=false&total=false\"\n f_url = url + get_dev_details_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_details = (json.loads(r.text))\n if len(dev_details) == 0:\n print(\"Device not found\")\n return \"Device not found\"\n elif type(dev_details['device']) == list:\n for i in dev_details['device']:\n if i['ip'] == ip_address:\n dev_details = i\n return dev_details\n elif type(dev_details['device']) == dict:\n return dev_details['device']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_dev_interface(devid, auth, url):\n\n get_dev_interface_url = \"/imcrs/plat/res/device/\" + str(devid) + \\\n \"/interface?start=0&size=1000&desc=false&total=false\"\n f_url = url + get_dev_interface_url\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n int_list = (json.loads(r.text))['interface']\n return int_list\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_interface: An Error has occured\"", "response": "Function takes devid as input to RESTFUL call to HP IMC platform and returns list of device interfaces"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction takes devid of specific device and issues a RESTFUL call to gather the current IP - MAC learning entries on the target device.", "response": "def get_dev_mac_learn(devid, auth, url):\n '''\n function takes devid of specific device and issues a RESTFUL call to gather the current IP-MAC learning entries on\n the target device.\n\n :param devid: int value of the target device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dict objects which contain the mac learn table of target device id\n\n :rtype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> dev_mac_learn = get_dev_mac_learn('10', auth.creds, auth.url)\n\n >>> assert type(dev_mac_learn) is list\n\n >>> assert 'deviceId' in dev_mac_learn[0]\n\n '''\n get_dev_mac_learn_url='/imcrs/res/access/ipMacLearn/'+str(devid)\n f_url = url+get_dev_mac_learn_url\n try:\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n if r.status_code == 200:\n if len(r.text) < 1:\n mac_learn_query = {}\n return mac_learn_query\n else:\n mac_learn_query = (json.loads(r.text))['ipMacLearnResult']\n return mac_learn_query\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_dev_mac_learn: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction runs the specific commands on the target device and returns the output of the commands.", "response": "def run_dev_cmd(devid, cmd_list, auth, url):\n '''\n Function takes devid of target device and a sequential list of strings which define the specific commands to be run\n on the target device and returns a str object containing the output of the commands.\n :param devid: int devid of the target device\n\n :param cmd_list: list of strings\n\n :return: str containing the response of the commands\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> cmd_list = ['display version']\n\n >>> cmd_output = run_dev_cmd('10', cmd_list, auth.creds, auth.url)\n\n >>> assert type(cmd_output) is dict\n\n >>> assert 'cmdlist' in cmd_output\n\n >>> assert 'success' in cmd_output\n\n\n '''\n run_dev_cmd_url = '/imcrs/icc/confFile/executeCmd'\n f_url = url + run_dev_cmd_url\n cmd_list = _make_cmd_list(cmd_list)\n payload = '''{ \"deviceId\" : \"'''+str(devid) + '''\",\n \"cmdlist\" : { \"cmd\" :\n ['''+ cmd_list + ''']\n\n }\n }'''\n r = requests.post(f_url, data=payload, auth=auth, headers=HEADERS)\n if r.status_code == 200:\n if len(r.text) < 1:\n return ''\n else:\n return json.loads(r.text)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction takes devId of a specific device and the ifindex value assigned to a specific interface and issues a RESTFUL call to get the details of all interfaces on the target device.", "response": "def get_all_interface_details(devId, auth, url):\n \"\"\"\n function takes the devId of a specific device and the ifindex value assigned to a specific interface\n and issues a RESTFUL call to get the interface details\n file as known by the HP IMC Base Platform ICC module for the target device.\n\n :param devId: int or str value of the devId of the target device\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: list of dict objects which contains the details of all interfaces on the target device\n\n :retype: list\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> all_interface_details = get_all_interface_details('10', auth.creds, auth.url)\n\n >>> assert type(all_interface_details) is list\n\n >>> assert 'ifAlias' in all_interface_details[0]\n\n\n \"\"\"\n\n get_all_interface_details_url = \"/imcrs/plat/res/device/\" + str(devId) + \"/interface/?start=0&size=1000&desc=false&total=false\"\n f_url = url + get_all_interface_details_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_details = (json.loads(r.text))\n return dev_details['interface']\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_interface_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfunctions takes devId and ifIndex and issues a RESTFUL call to get the details of the target interface", "response": "def get_interface_details(devId, ifIndex, auth, url):\n \"\"\"\n function takes the devId of a specific device and the ifindex value assigned to a specific interface\n and issues a RESTFUL call to get the interface details\n file as known by the HP IMC Base Platform ICC module for the target device.\n\n :param devId: int or str value of the devId of the target device\n\n :param ifIndex: int or str value of the ifIndex of the target interface\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: dict which contains the details of the target interface\"\n\n :retype: dict\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> interface_details = get_interface_details('10', '1', auth.creds, auth.url)\n\n >>> assert type(interface_details) is dict\n\n >>> assert 'ifAlias' in interface_details\n\n\n \"\"\"\n get_interface_details_url = \"/imcrs/plat/res/device/\" + str(devId) + \"/interface/\" + str(ifIndex)\n f_url = url + get_interface_details_url\n payload = None\n # creates the URL using the payload variable as the contents\n r = requests.get(f_url, auth=auth, headers=HEADERS)\n # r.status_code\n try:\n if r.status_code == 200:\n dev_details = (json.loads(r.text))\n return dev_details\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" get_interface_details: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfunction to shut the specifie d interface on the target device.", "response": "def set_interface_down(devid, ifindex, auth, url):\n \"\"\"\n function takest devid and ifindex of specific device and interface and issues a RESTFUL call to \" shut\" the specifie\n d interface on the target device.\n :param devid: int or str value of the target device\n\n :param ifindex: int or str value of the target interface\n\n :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class\n\n :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass\n\n :return: HTTP status code 204 with no values.\n\n :rtype:int\n\n >>> from pyhpeimc.auth import *\n\n >>> from pyhpeimc.plat.device import *\n\n >>> auth = IMCAuth(\"http://\", \"10.101.0.203\", \"8080\", \"admin\", \"admin\")\n\n >>> int_down_response = set_interface_down('10', '9', auth.creds, auth.url)\n 204\n\n >>> assert type(int_down_response) is int\n\n >>> assert int_down_response is 204\n \"\"\"\n set_int_down_url = \"/imcrs/plat/res/device/\" + str(devid) + \"/interface/\" + str(ifindex) + \"/down\"\n f_url = url + set_int_down_url\n try:\n r = requests.put(f_url, auth=auth,\n headers=HEADERS) # creates the URL using the payload variable as the contents\n print(r.status_code)\n if r.status_code == 204:\n return r.status_code\n except requests.exceptions.RequestException as e:\n return \"Error:\\n\" + str(e) + \" set_inteface_down: An Error has occured\""} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def create(cls, name: str,\n default_for_unspecified: int,\n total_resource_slots: int,\n max_concurrent_sessions: int,\n max_containers_per_session: int,\n max_vfolder_count: int,\n max_vfolder_size: int,\n idle_timeout: int,\n allowed_vfolder_hosts: Sequence[str],\n fields: Iterable[str] = None) -> dict:\n \"\"\"\n Creates a new keypair resource policy with the given options.\n You need an admin privilege for this operation.\n \"\"\"\n if fields is None:\n fields = ('name',)\n q = 'mutation($name: String!, $input: CreateKeyPairResourcePolicyInput!) {' \\\n + \\\n ' create_keypair_resource_policy(name: $name, props: $input) {' \\\n ' ok msg resource_policy { $fields }' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n variables = {\n 'name': name,\n 'input': {\n 'default_for_unspecified': default_for_unspecified,\n 'total_resource_slots': total_resource_slots,\n 'max_concurrent_sessions': max_concurrent_sessions,\n 'max_containers_per_session': max_containers_per_session,\n 'max_vfolder_count': max_vfolder_count,\n 'max_vfolder_size': max_vfolder_size,\n 'idle_timeout': idle_timeout,\n 'allowed_vfolder_hosts': allowed_vfolder_hosts,\n },\n }\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n 'variables': variables,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['create_keypair_resource_policy']", "response": "Create a new keypair resource policy."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def list(cls, fields: Iterable[str] = None) -> Sequence[dict]:\n '''\n Lists the keypair resource policies.\n You need an admin privilege for this operation.\n '''\n if fields is None:\n fields = (\n 'name', 'created_at',\n 'total_resource_slots', 'max_concurrent_sessions',\n 'max_vfolder_count', 'max_vfolder_size',\n 'idle_timeout',\n )\n q = 'query {' \\\n ' keypair_resource_policies {' \\\n ' $fields' \\\n ' }' \\\n '}'\n q = q.replace('$fields', ' '.join(fields))\n rqst = Request(cls.session, 'POST', '/admin/graphql')\n rqst.set_json({\n 'query': q,\n })\n async with rqst.fetch() as resp:\n data = await resp.json()\n return data['keypair_resource_policies']", "response": "Lists the keypair resource policies."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef keypair():\n '''\n Show the server-side information of the currently configured access key.\n '''\n fields = [\n ('User ID', 'user_id'),\n ('Access Key', 'access_key'),\n ('Secret Key', 'secret_key'),\n ('Active?', 'is_active'),\n ('Admin?', 'is_admin'),\n ('Created At', 'created_at'),\n ('Last Used', 'last_used'),\n ('Res.Policy', 'resource_policy'),\n ('Rate Limit', 'rate_limit'),\n ('Concur.Limit', 'concurrency_limit'),\n ('Concur.Used', 'concurrency_used'),\n ]\n with Session() as session:\n try:\n kp = session.KeyPair(session.config.access_key)\n info = kp.info(fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n rows = []\n for name, key in fields:\n rows.append((name, info[key]))\n print(tabulate(rows, headers=('Field', 'Value')))", "response": "Show the server - side information of the currently configured access key."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef keypairs(ctx, user_id, is_active):\n '''\n List and manage keypairs.\n To show all keypairs or other user's, your access key must have the admin\n privilege.\n (admin privilege required)\n '''\n if ctx.invoked_subcommand is not None:\n return\n fields = [\n ('User ID', 'user_id'),\n ('Access Key', 'access_key'),\n ('Secret Key', 'secret_key'),\n ('Active?', 'is_active'),\n ('Admin?', 'is_admin'),\n ('Created At', 'created_at'),\n ('Last Used', 'last_used'),\n ('Res.Policy', 'resource_policy'),\n ('Rate Limit', 'rate_limit'),\n ('Concur.Limit', 'concurrency_limit'),\n ('Concur.Used', 'concurrency_used'),\n ]\n try:\n user_id = int(user_id)\n except (TypeError, ValueError):\n pass # string-based user ID for Backend.AI v1.4+\n with Session() as session:\n try:\n items = session.KeyPair.list(user_id, is_active,\n fields=(item[1] for item in fields))\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if len(items) == 0:\n print('There are no matching keypairs associated '\n 'with the user ID {0}'.format(user_id))\n return\n print(tabulate((item.values() for item in items),\n headers=(item[0] for item in fields)))", "response": "List and manage keypairs associated with a user."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add(user_id, resource_policy, admin, inactive, rate_limit):\n '''\n Add a new keypair.\n\n USER_ID: User ID of a new key pair.\n RESOURCE_POLICY: resource policy for new key pair.\n '''\n try:\n user_id = int(user_id)\n except ValueError:\n pass # string-based user ID for Backend.AI v1.4+\n with Session() as session:\n try:\n data = session.KeyPair.create(\n user_id,\n is_active=not inactive,\n is_admin=admin,\n resource_policy=resource_policy,\n rate_limit=rate_limit)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if not data['ok']:\n print_fail('KeyPair creation has failed: {0}'.format(data['msg']))\n sys.exit(1)\n item = data['keypair']\n print('Access Key: {0}'.format(item['access_key']))\n print('Secret Key: {0}'.format(item['secret_key']))", "response": "Add a new keypair."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating an existing keypair.", "response": "def update(access_key, resource_policy, is_admin, is_active, rate_limit):\n '''\n Update an existing keypair.\n\n ACCESS_KEY: Access key of an existing key pair.\n '''\n with Session() as session:\n try:\n data = session.KeyPair.update(\n access_key,\n is_active=is_active,\n is_admin=is_admin,\n resource_policy=resource_policy,\n rate_limit=rate_limit)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if not data['ok']:\n print_fail('KeyPair creation has failed: {0}'.format(data['msg']))\n sys.exit(1)\n print('Key pair is updated: ' + access_key + '.')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndelete an existing keypair.", "response": "def delete(access_key):\n \"\"\"\n Delete an existing keypair.\n\n ACCESSKEY: ACCESSKEY for a keypair to delete.\n \"\"\"\n with Session() as session:\n try:\n data = session.KeyPair.delete(access_key)\n except Exception as e:\n print_error(e)\n sys.exit(1)\n if not data['ok']:\n print_fail('KeyPair deletion has failed: {0}'.format(data['msg']))\n sys.exit(1)\n print('Key pair is deleted: ' + access_key + '.')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nperforms API auth test returning user and team", "response": "def login(self):\n \"\"\" perform API auth test returning user and team \"\"\"\n log.debug('performing auth test')\n test = self._get(urls['test'])\n user = User({ 'name': test['user'], 'id': test['user_id'] })\n self._refresh()\n return test['team'], user"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a User object for a given Slack ID or name", "response": "def user(self, match):\n \"\"\" Return User object for a given Slack ID or name \"\"\"\n if len(match) == 9 and match[0] == 'U':\n return self._lookup(User, 'id', match)\n return self._lookup(User, 'name', match)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef channel(self, match):\n if len(match) == 9 and match[0] in ('C','G','D'):\n return self._lookup(Channel, 'id', match)\n return self._lookup(Channel, 'name', match)", "response": "Return a Channel object for a given Slack ID or name"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrefresh internal directory cache", "response": "def _refresh(self):\n \"\"\" refresh internal directory cache \"\"\"\n log.debug('refreshing directory cache')\n self._users.update(list(self._user_gen()))\n self._channels.update(list(self._channel_gen()))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef match(self, attr, val):\n self._lock.acquire()\n try:\n for x in self:\n if getattr(x, attr) == val:\n return x\n finally:\n self._lock.release()", "response": "lookup object in directory with attribute matching value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _groups_of_size(iterable, n, fillvalue=None):\n # _groups_of_size('ABCDEFG', 3, 'x') --> ABC DEF Gxx\n args = [iter(iterable)] * n\n return zip_longest(fillvalue=fillvalue, *args)", "response": "Collect data into fixed - length chunks or blocks."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef slice_repr(slice_instance):\n if not isinstance(slice_instance, slice):\n raise TypeError('Unhandled type {}'.format(type(slice_instance)))\n start = slice_instance.start or ''\n stop = slice_instance.stop or ''\n step = slice_instance.step or ''\n\n msg = '{}:'.format(start)\n if stop:\n msg += '{}'.format(stop)\n if step:\n msg += ':'\n if step:\n msg += '{}'.format(step)\n return msg", "response": "Convert a slice into a string."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef biggus_chunk(chunk_key, biggus_array, masked):\n if masked:\n array = biggus_array.masked_array()\n else:\n array = biggus_array.ndarray()\n\n return biggus._init.Chunk(chunk_key, array)", "response": "A function that lazily evaluates a biggus. Chunk."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nproduces the nodes for the streams handler for the array.", "response": "def _make_stream_handler_nodes(self, dsk_graph, array, iteration_order,\n masked):\n \"\"\"\n Produce task graph entries for an array that comes from a biggus\n StreamsHandler.\n\n This is essentially every type of array that isn't already a thing on\n disk/in-memory. StreamsHandler arrays include all aggregations and\n elementwise operations.\n\n \"\"\"\n nodes = {}\n handler = array.streams_handler(masked)\n input_iteration_order = handler.input_iteration_order(iteration_order)\n\n def input_keys_transform(input_array, keys):\n if hasattr(input_array, 'streams_handler'):\n handler = input_array.streams_handler(masked)\n # Get the transformer of the input array, and apply it to the\n # keys.\n input_transformer = getattr(handler,\n 'output_keys', None)\n if input_transformer is not None:\n keys = input_transformer(keys)\n return keys\n\n sources_keys = []\n sources_chunks = []\n for input_array in array.sources:\n # Bring together all chunks that influence the same part of this\n # (resultant) array.\n source_chunks_by_key = {}\n sources_chunks.append(source_chunks_by_key)\n source_keys = []\n sources_keys.append(source_keys)\n\n # Make nodes for the source arrays (if they don't already exist)\n # before we do anything else.\n input_nodes = self._make_nodes(dsk_graph, input_array,\n input_iteration_order, masked)\n\n for chunk_id, task in input_nodes.items():\n chunk_keys = task[1]\n t_keys = chunk_keys\n t_keys = input_keys_transform(array, t_keys)\n source_keys.append(t_keys)\n this_key = str(t_keys)\n source_chunks_by_key.setdefault(this_key,\n []).append([chunk_id, task])\n\n sources_keys_grouped = key_grouper.group_keys(array.shape,\n *sources_keys)\n for slice_group, sources_keys_group in sources_keys_grouped.items():\n # Each group is entirely independent and can have its own task\n # without knowledge of results from items in other groups.\n\n t_keys = tuple(slice(*slice_tuple) for slice_tuple in slice_group)\n\n all_chunks = []\n for source_keys, source_chunks_by_key in zip(sources_keys_group,\n sources_chunks):\n dependencies = tuple(\n the_id\n for keys in source_keys\n for the_id, task in source_chunks_by_key[str(keys)])\n # Uniquify source_keys, but keep the order.\n dependencies = tuple(_unique_everseen(dependencies))\n\n def normalize_keys(keys, shape):\n result = []\n for key, dim_length in zip(keys, shape):\n result.append(key_grouper.normalize_slice(key,\n dim_length))\n return tuple(result)\n\n # If we don't have the same chunks for all inputs then we\n # should combine them before passing them on to the handler.\n # TODO: Fix slice equality to deal with 0 and None etc.\n if not all(t_keys == normalize_keys(keys, array.shape)\n for keys in source_keys):\n combined = self.collect(array[t_keys], masked, chunk=True)\n new_task = (combined, ) + dependencies\n new_id = ('chunk shape: {}\\n\\n{}'\n ''.format(array[t_keys].shape, uuid.uuid()))\n dsk_graph[new_id] = new_task\n dependencies = (new_id, )\n\n all_chunks.append(dependencies)\n\n pivoted = all_chunks\n\n sub_array = array[t_keys]\n handler = sub_array.streams_handler(masked)\n name = getattr(handler, 'nice_name', handler.__class__.__name__)\n\n if hasattr(handler, 'axis'):\n name += '\\n(axis={})'.format(handler.axis)\n # For ElementwiseStreams handlers, use the function that they wrap\n # (e.g \"add\")\n if hasattr(handler, 'operator'):\n name = handler.operator.__name__\n\n n_sources = len(array.sources)\n handler_of_chunks_fn = self.create_chunks_handler_fn(handler,\n n_sources,\n name)\n\n shape = sub_array.shape\n if all(key == slice(None) for key in t_keys):\n subset = ''\n else:\n pretty_index = ', '.join(map(slice_repr, t_keys))\n subset = 'target subset [{}]\\n'.format(pretty_index)\n\n # Flatten out the pivot so that dask can dereferences the IDs\n source_chunks = [item for sublist in pivoted for item in sublist]\n task = tuple([handler_of_chunks_fn, t_keys] + source_chunks)\n shape_repr = ', '.join(map(str, shape))\n chunk_id = 'chunk shape: ({})\\n\\n{}{}'.format(shape_repr,\n subset,\n uuid.uuid4())\n assert chunk_id not in dsk_graph\n dsk_graph[chunk_id] = task\n nodes[chunk_id] = task\n return nodes"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef lazy_chunk_creator(name):\n # TODO: Could this become a LazyChunk class?\n def biggus_chunk(chunk_key, biggus_array, masked):\n \"\"\"\n A function that lazily evaluates a biggus.Chunk. This is useful for\n passing through as a dask task so that we don't have to compute the\n chunk in order to compute the graph.\n\n \"\"\"\n if masked:\n array = biggus_array.masked_array()\n else:\n array = biggus_array.ndarray()\n\n return biggus._init.Chunk(chunk_key, array)\n biggus_chunk.__name__ = name\n return biggus_chunk", "response": "Create a lazy chunk creating function that lazily evaluates a biggus. Chunk."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_password(prompt='Password: ', confirm=False):\n\n # Are the arguments the expected type?\n # If not, raise 'securesystemslib.exceptions.FormatError'.\n securesystemslib.formats.TEXT_SCHEMA.check_match(prompt)\n securesystemslib.formats.BOOLEAN_SCHEMA.check_match(confirm)\n\n while True:\n # getpass() prompts the user for a password without echoing\n # the user input.\n password = getpass.getpass(prompt, sys.stderr)\n\n if not confirm:\n return password\n password2 = getpass.getpass('Confirm: ', sys.stderr)\n\n if password == password2:\n return password\n\n else:\n print('Mismatch; try again.')", "response": "Get a password from the user."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nimport a RSA private key from a PEM file.", "response": "def import_rsa_privatekey_from_file(filepath, password=None,\n scheme='rsassa-pss-sha256', prompt=False):\n \"\"\"\n \n Import the PEM file in 'filepath' containing the private key.\n\n If password is passed use passed password for decryption.\n If prompt is True use entered password for decryption.\n If no password is passed and either prompt is False or if the password\n entered at the prompt is an empty string, omit decryption, treating the\n key as if it is not encrypted.\n If password is passed and prompt is True, an error is raised. (See below.)\n\n The returned key is an object in the\n 'securesystemslib.formats.RSAKEY_SCHEMA' format.\n\n \n filepath:\n file, an RSA encrypted PEM file. Unlike the public RSA PEM\n key file, 'filepath' does not have an extension.\n\n password:\n The passphrase to decrypt 'filepath'.\n\n scheme:\n The signature scheme used by the imported key.\n\n prompt:\n If True the user is prompted for a passphrase to decrypt 'filepath'.\n Default is False.\n\n \n ValueError, if 'password' is passed and 'prompt' is True.\n\n ValueError, if 'password' is passed and it is an empty string.\n\n securesystemslib.exceptions.FormatError, if the arguments are improperly\n formatted.\n\n securesystemslib.exceptions.FormatError, if the entered password is\n improperly formatted.\n\n IOError, if 'filepath' can't be loaded.\n\n securesystemslib.exceptions.CryptoError, if a password is available\n and 'filepath' is not a valid key file encrypted using that password.\n\n securesystemslib.exceptions.CryptoError, if no password is available\n and 'filepath' is not a valid non-encrypted key file.\n\n \n The contents of 'filepath' are read, optionally decrypted, and returned.\n\n \n An RSA key object, conformant to 'securesystemslib.formats.RSAKEY_SCHEMA'.\n\n \"\"\"\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # Is 'scheme' properly formatted?\n securesystemslib.formats.RSA_SCHEME_SCHEMA.check_match(scheme)\n\n if password and prompt:\n raise ValueError(\"Passing 'password' and 'prompt' True is not allowed.\")\n\n # If 'password' was passed check format and that it is not empty.\n if password is not None:\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n\n # TODO: PASSWORD_SCHEMA should be securesystemslib.schema.AnyString(min=1)\n if not len(password):\n raise ValueError('Password must be 1 or more characters')\n\n elif prompt:\n # Password confirmation disabled here, which should ideally happen only\n # when creating encrypted key files (i.e., improve usability).\n # It is safe to specify the full path of 'filepath' in the prompt and not\n # worry about leaking sensitive information about the key's location.\n # However, care should be taken when including the full path in exceptions\n # and log files.\n # NOTE: A user who gets prompted for a password, can only signal that the\n # key is not encrypted by entering no password in the prompt, as opposed\n # to a programmer who can call the function with or without a 'password'.\n # Hence, we treat an empty password here, as if no 'password' was passed.\n password = get_password('Enter a password for an encrypted RSA'\n ' file \\'' + Fore.RED + filepath + Fore.RESET + '\\': ',\n confirm=False) or None\n\n if password is not None:\n # This check will not fail, because a mal-formatted passed password fails\n # above and an entered password will always be a string (see get_password)\n # However, we include it in case PASSWORD_SCHEMA or get_password changes.\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n\n else:\n logger.debug('No password was given. Attempting to import an'\n ' unencrypted file.')\n\n # Read the contents of 'filepath' that should be a PEM formatted private key.\n with open(filepath, 'rb') as file_object:\n pem_key = file_object.read().decode('utf-8')\n\n # Convert 'pem_key' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.\n # Raise 'securesystemslib.exceptions.CryptoError' if 'pem_key' is invalid.\n # If 'password' is None decryption will be omitted.\n rsa_key = securesystemslib.keys.import_rsakey_from_private_pem(pem_key,\n scheme, password)\n\n return rsa_key"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef import_rsa_publickey_from_file(filepath):\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # Read the contents of the key file that should be in PEM format and contains\n # the public portion of the RSA key.\n with open(filepath, 'rb') as file_object:\n rsa_pubkey_pem = file_object.read().decode('utf-8')\n\n # Convert 'rsa_pubkey_pem' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.\n try:\n rsakey_dict = securesystemslib.keys.import_rsakey_from_public_pem(rsa_pubkey_pem)\n\n except securesystemslib.exceptions.FormatError as e:\n raise securesystemslib.exceptions.Error('Cannot import improperly formatted'\n ' PEM file.' + repr(str(e)))\n\n return rsakey_dict", "response": "Imports the RSA public key stored in filepath into a dict key."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nimport the ed25519 public key from a file.", "response": "def import_ed25519_publickey_from_file(filepath):\n \"\"\"\n \n Load the ED25519 public key object (conformant to\n 'securesystemslib.formats.KEY_SCHEMA') stored in 'filepath'. Return\n 'filepath' in securesystemslib.formats.ED25519KEY_SCHEMA format.\n\n If the key object in 'filepath' contains a private key, it is discarded.\n\n \n filepath:\n .pub file, a public key file.\n\n \n securesystemslib.exceptions.FormatError, if 'filepath' is improperly\n formatted or is an unexpected key type.\n\n \n The contents of 'filepath' is read and saved.\n\n \n An ED25519 key object conformant to\n 'securesystemslib.formats.ED25519KEY_SCHEMA'.\n \"\"\"\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # ED25519 key objects are saved in json and metadata format. Return the\n # loaded key object in securesystemslib.formats.ED25519KEY_SCHEMA' format that\n # also includes the keyid.\n ed25519_key_metadata = securesystemslib.util.load_json_file(filepath)\n ed25519_key, junk = \\\n securesystemslib.keys.format_metadata_to_key(ed25519_key_metadata)\n\n # Raise an exception if an unexpected key type is imported. Redundant\n # validation of 'keytype'. 'securesystemslib.keys.format_metadata_to_key()'\n # should have fully validated 'ed25519_key_metadata'.\n if ed25519_key['keytype'] != 'ed25519': # pragma: no cover\n message = 'Invalid key type loaded: ' + repr(ed25519_key['keytype'])\n raise securesystemslib.exceptions.FormatError(message)\n\n return ed25519_key"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n if password and prompt:\n raise ValueError(\"Passing 'password' and 'prompt' True is not allowed.\")\n\n # If 'password' was passed check format and that it is not empty.\n if password is not None:\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n\n # TODO: PASSWORD_SCHEMA should be securesystemslib.schema.AnyString(min=1)\n if not len(password):\n raise ValueError('Password must be 1 or more characters')\n\n elif prompt:\n # Password confirmation disabled here, which should ideally happen only\n # when creating encrypted key files (i.e., improve usability).\n # It is safe to specify the full path of 'filepath' in the prompt and not\n # worry about leaking sensitive information about the key's location.\n # However, care should be taken when including the full path in exceptions\n # and log files.\n # NOTE: A user who gets prompted for a password, can only signal that the\n # key is not encrypted by entering no password in the prompt, as opposed\n # to a programmer who can call the function with or without a 'password'.\n # Hence, we treat an empty password here, as if no 'password' was passed.\n password = get_password('Enter a password for an encrypted RSA'\n ' file \\'' + Fore.RED + filepath + Fore.RESET + '\\': ',\n confirm=False)\n\n # If user sets an empty string for the password, explicitly set the\n # password to None, because some functions may expect this later.\n if len(password) == 0: # pragma: no cover\n password = None\n\n # Finally, regardless of password, try decrypting the key, if necessary.\n # Otherwise, load it straight from the disk.\n with open(filepath, 'rb') as file_object:\n json_str = file_object.read()\n return securesystemslib.keys.\\\n import_ed25519key_from_private_json(json_str, password=password)", "response": "Imports an ed25519 private key from a file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generate_and_write_ecdsa_keypair(filepath=None, password=None):\n\n # Generate a new ECDSA key object. The 'cryptography' library is currently\n # supported and performs the actual cryptographic operations.\n ecdsa_key = securesystemslib.keys.generate_ecdsa_key()\n\n if not filepath:\n filepath = os.path.join(os.getcwd(), ecdsa_key['keyid'])\n\n else:\n logger.debug('The filepath has been specified. Not using the key\\'s'\n ' KEYID as the default filepath.')\n\n # Does 'filepath' have the correct format?\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # If the caller does not provide a password argument, prompt for one.\n if password is None: # pragma: no cover\n\n # It is safe to specify the full path of 'filepath' in the prompt and not\n # worry about leaking sensitive information about the key's location.\n # However, care should be taken when including the full path in exceptions\n # and log files.\n password = get_password('Enter a password for the ECDSA'\n ' key (' + Fore.RED + filepath + Fore.RESET + '): ',\n confirm=True)\n\n else:\n logger.debug('The password has been specified. Not prompting for one')\n\n # Does 'password' have the correct format?\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n\n # If the parent directory of filepath does not exist,\n # create it (and all its parent directories, if necessary).\n securesystemslib.util.ensure_parent_dir(filepath)\n\n # Create a temporary file, write the contents of the public key, and move\n # to final destination.\n file_object = securesystemslib.util.TempFile()\n\n # Generate the ECDSA public key file contents in metadata format (i.e., does\n # not include the keyid portion).\n keytype = ecdsa_key['keytype']\n keyval = ecdsa_key['keyval']\n scheme = ecdsa_key['scheme']\n ecdsakey_metadata_format = securesystemslib.keys.format_keyval_to_metadata(\n keytype, scheme, keyval, private=False)\n\n file_object.write(json.dumps(ecdsakey_metadata_format).encode('utf-8'))\n\n # Write the public key (i.e., 'public', which is in PEM format) to\n # '.pub'. (1) Create a temporary file, (2) write the contents of\n # the public key, and (3) move to final destination.\n file_object.move(filepath + '.pub')\n\n # Write the encrypted key string, conformant to\n # 'securesystemslib.formats.ENCRYPTEDKEY_SCHEMA', to ''.\n file_object = securesystemslib.util.TempFile()\n # Raise 'securesystemslib.exceptions.CryptoError' if 'ecdsa_key' cannot be\n # encrypted.\n encrypted_key = securesystemslib.keys.encrypt_key(ecdsa_key, password)\n file_object.write(encrypted_key.encode('utf-8'))\n file_object.move(filepath)\n\n return filepath", "response": "This function generates an ECDSA keypair and writes it to the file at filepath."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef import_ecdsa_publickey_from_file(filepath):\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # ECDSA key objects are saved in json and metadata format. Return the\n # loaded key object in securesystemslib.formats.ECDSAKEY_SCHEMA' format that\n # also includes the keyid.\n ecdsa_key_metadata = securesystemslib.util.load_json_file(filepath)\n ecdsa_key, junk = \\\n securesystemslib.keys.format_metadata_to_key(ecdsa_key_metadata)\n\n # Raise an exception if an unexpected key type is imported. Redundant\n # validation of 'keytype'. 'securesystemslib.keys.format_metadata_to_key()'\n # should have fully validated 'ecdsa_key_metadata'.\n if ecdsa_key['keytype'] != 'ecdsa-sha2-nistp256': # pragma: no cover\n message = 'Invalid key type loaded: ' + repr(ecdsa_key['keytype'])\n raise securesystemslib.exceptions.FormatError(message)\n\n return ecdsa_key", "response": "Imports a public key from a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nimport an encrypted ECDSA key file in filepath and returns the object in the format of the ECDSAKEYSCHEMA format.", "response": "def import_ecdsa_privatekey_from_file(filepath, password=None):\n \"\"\"\n \n Import the encrypted ECDSA key file in 'filepath', decrypt it, and return\n the key object in 'securesystemslib.formats.ECDSAKEY_SCHEMA' format.\n\n The 'cryptography' library is currently supported and performs the actual\n cryptographic routine.\n\n \n filepath:\n file, an ECDSA encrypted key file.\n\n password:\n The password, or passphrase, to import the private key (i.e., the\n encrypted key file 'filepath' must be decrypted before the ECDSA key\n object can be returned.\n\n \n securesystemslib.exceptions.FormatError, if the arguments are improperly\n formatted or the imported key object contains an invalid key type (i.e.,\n not 'ecdsa-sha2-nistp256').\n\n securesystemslib.exceptions.CryptoError, if 'filepath' cannot be decrypted.\n\n \n 'password' is used to decrypt the 'filepath' key file.\n\n \n An ECDSA key object of the form: 'securesystemslib.formats.ECDSAKEY_SCHEMA'.\n \"\"\"\n\n # Does 'filepath' have the correct format?\n # Ensure the arguments have the appropriate number of objects and object\n # types, and that all dict keys are properly named.\n # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.\n securesystemslib.formats.PATH_SCHEMA.check_match(filepath)\n\n # If the caller does not provide a password argument, prompt for one.\n # Password confirmation disabled here, which should ideally happen only\n # when creating encrypted key files (i.e., improve usability).\n if password is None: # pragma: no cover\n\n # It is safe to specify the full path of 'filepath' in the prompt and not\n # worry about leaking sensitive information about the key's location.\n # However, care should be taken when including the full path in exceptions\n # and log files.\n password = get_password('Enter a password for the encrypted ECDSA'\n ' key (' + Fore.RED + filepath + Fore.RESET + '): ',\n confirm=False)\n\n # Does 'password' have the correct format?\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n\n # Store the encrypted contents of 'filepath' prior to calling the decryption\n # routine.\n encrypted_key = None\n\n with open(filepath, 'rb') as file_object:\n encrypted_key = file_object.read()\n\n # Decrypt the loaded key file, calling the 'cryptography' library to generate\n # the derived encryption key from 'password'. Raise\n # 'securesystemslib.exceptions.CryptoError' if the decryption fails.\n key_object = securesystemslib.keys.decrypt_key(encrypted_key.decode('utf-8'),\n password)\n\n # Raise an exception if an unexpected key type is imported.\n if key_object['keytype'] != 'ecdsa-sha2-nistp256':\n message = 'Invalid key type loaded: ' + repr(key_object['keytype'])\n raise securesystemslib.exceptions.FormatError(message)\n\n # Add \"keyid_hash_algorithms\" so that equal ecdsa keys with different keyids\n # can be associated using supported keyid_hash_algorithms.\n key_object['keyid_hash_algorithms'] = \\\n securesystemslib.settings.HASH_ALGORITHMS\n\n return key_object"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwaits until the element identified by locator has focus.", "response": "def wait_until_element_has_focus(self, locator, timeout=None):\n\t\t\"\"\"Waits until the element identified by `locator` has focus.\n\t\tYou might rather want to use `Element Focus Should Be Set`\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| timeout | maximum time to wait before the function throws an element not found error (default=None) | 5s |\"\"\"\n\n\t\tself._info(\"Waiting for focus on '%s'\" % (locator))\n\t\tself._wait_until_no_error(timeout, self._check_element_focus_exp, True, locator, timeout)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwait until the element identified by locator does not have focus.", "response": "def wait_until_element_does_not_have_focus(self, locator, timeout=None):\n\t\t\"\"\"Waits until the element identified by `locator` doesn't have focus.\n\t\tYou might rather want to use `Element Focus Should Not Be Set`\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| timeout | maximum time to wait before the function throws an element not found error (default=None) | 5s |\"\"\"\n\n\t\tself._info(\"Waiting until '%s' does not have focus\" % (locator))\n\t\tself._wait_until_no_error(timeout, self._check_element_focus_exp, False, locator, timeout)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef wait_until_element_value_is(self, locator, expected, strip=False, timeout=None):\n\n\t\tself._info(\"Waiting for '%s' value to be '%s'\" % (locator, expected))\n\t\tself._wait_until_no_error(timeout, self._check_element_value_exp, False, locator, expected, strip, timeout)", "response": "Waits until the element identified by locator value is exactly the expected value."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wait_until_element_value_contains(self, locator, expected, timeout=None):\n\n\t\tself._info(\"Waiting for '%s' value to contain '%s'\" % (locator, expected))\n\t\tself._wait_until_no_error(timeout, self._check_element_value_exp, True, locator, expected, False, timeout)", "response": "Waits until the element identified by locator contains the expected value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting focus on the element identified by locator.", "response": "def set_element_focus(self, locator):\n\t\t\"\"\"Sets focus on the element identified by `locator`. Should\n\t\tbe used with elements meant to have focus only, such as\n\t\ttext fields. This keywords also waits for the focus to be\n\t\tactive by calling the `Wait Until Element Has Focus` keyword.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\"\"\"\n\n\t\tself._info(\"Setting focus on element '%s'\" % (locator))\n\t\t\n\t\telement = self._element_find(locator, True, True)\n\t\telement.send_keys(Keys.NULL)\n\n\t\tself._wait_until_no_error(None, self._check_element_focus, True, locator)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nclear the input field identified by locator.", "response": "def clear_input_field(self, locator, method=0):\n\t\t\"\"\"Clears the text field identified by `locator`\n\n\t\tThe element.clear() method doesn't seem to work properly on\n\t\tall browsers, so this keyword was created to offer alternatives.\n\n\t\tThe `method` argument defines the method it should use in order\n\t\tto clear the target field.\n\n\t\t0 = Uses the selenium method by doing element.clear \\n\n\t\t1 = Sets focus on the field and presses CTRL + A, and then DELETE \\n\n\t\t2 = Repeatedly presses BACKSPACE until the field is empty\n\n\t\tThis keyword, when using a method other than '2' does not validate it\n\t\tsuccessfully cleared the field, you should handle this verification by yourself.\n\t\tWhen using the method '2', it presses delete until the field's value is empty.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| method | the clearing method that should be used | no example provided |\"\"\"\n\n\t\telement = self._element_find(locator, True, True)\n\t\t\n\t\tif (int(method) == 0):\n\n\t\t\tself._info(\"Clearing input on element '%s'\" % (locator))\n\t\t\telement.clear()\n\n\t\telif (int(method) == 1):\n\n\t\t\tself._info(\"Clearing input on element '%s' by pressing 'CTRL + A + DELETE'\" % (locator))\n\t\t\telement.send_keys(Keys.CONTROL + 'a')\n\t\t\telement.send_keys(Keys.DELETE)\n\n\t\telif (int(method) == 2):\n\n\t\t\tself._info(\"Clearing input on element '%s' by repeatedly pressing BACKSPACE\" % (locator))\n\t\t\twhile (len(element.get_attribute('value')) != 0):\n\n\t\t\t\telement.send_keys(Keys.BACKSPACE)\n\n\t\telse: element.clear()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef element_text_color_should_be(self, locator, expected):\n\n\t\tself._info(\"Verifying element '%s' has text color '%s'\" % (locator, expected))\n\t\tself._check_element_css_value(locator, 'color', expected)", "response": "Verifies the element identified by locator has the expected text color."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nverifies the element identified by locator has the expected background color.", "response": "def element_background_color_should_be(self, locator, expected):\n\t\t\"\"\"Verifies the element identified by `locator` has the expected\n\t\tbackground color (it verifies the CSS attribute background-color). Color should\n\t\tbe in RGBA format.\n\n\t\tExample of rgba format: rgba(RED, GREEN, BLUE, ALPHA)\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| expected | expected color | rgba(0, 128, 0, 1) |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' has background color '%s'\" % (locator, expected))\n\t\tself._check_element_css_value(locator, 'background-color', expected)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying the element identified by locator has the expected width.", "response": "def element_width_should_be(self, locator, expected):\n\t\t\"\"\"Verifies the element identified by `locator` has the expected\n\t\twidth. Expected width should be in pixels.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| expected | expected width | 800 |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' width is '%s'\" % (locator, expected))\n\t\tself._check_element_size(locator, 'width', expected)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nverifying the element identified by locator has the expected height.", "response": "def element_height_should_be(self, locator, expected):\n\t\t\"\"\"Verifies the element identified by `locator` has the expected\n\t\theight. Expected height should be in pixels.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| expected | expected height | 600 |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' height is '%s'\" % (locator, expected))\n\t\tself._check_element_size(locator, 'height', expected)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef element_value_should_be(self, locator, expected, strip=False):\n\n\t\tself._info(\"Verifying element '%s' value is '%s'\" % (locator, expected))\n\t\t\n\t\telement = self._element_find(locator, True, True)\n\t\tvalue = element.get_attribute('value')\n\n\t\tif (strip):\n\t\t\tvalue = value.strip()\n\t\t\n\t\tif str(value) == expected:\n\t\t\treturn\n\t\t\n\t\telse:\n\t\t\traise AssertionError(\"Element '%s' value was not '%s', it was '%s'\" % (locator, expected, value))", "response": "Verifies the element identified by locator has the expected value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying the element identified by locator is not the specified value.", "response": "def element_value_should_not_be(self, locator, value, strip=False):\n\t\t\"\"\"Verifies the element identified by `locator` is not the specified value.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| value | value it should not be | My Name Is Slim Shady |\n\t\t| strip | Boolean, determines whether it should strip the field's value before comparison or not | ${True} / ${False} |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' value is not '%s'\" % (locator, value))\n\t\t\n\t\telement = self._element_find(locator, True, True)\n\t\telem_value = str(element.get_attribute('value'))\n\n\t\tif (strip):\n\t\t\telem_value = elem_value.strip()\n\t\t\n\t\tif elem_value == value:\n\t\t\traise AssertionError(\"Value was '%s' for element '%s' while it shouldn't have\" % (elem_value, locator))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying the element identified by locator contains the expected value.", "response": "def element_value_should_contain(self, locator, expected):\n\t\t\"\"\"Verifies the element identified by `locator` contains the expected value.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| expected | expected value | Slim Shady |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' value contains '%s'\" % (locator, expected))\n\t\t\n\t\telement = self._element_find(locator, True, True)\n\t\tvalue = str(element.get_attribute('value'))\n\t\t\n\t\tif expected in value:\n\t\t\treturn\n\t\t\n\t\telse:\n\t\t\traise AssertionError(\"Value '%s' did not appear in element '%s'. It's value was '%s'\" % (expected, locator, value))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nverify the element identified by locator does not contain the specified value.", "response": "def element_value_should_not_contain(self, locator, value):\n\t\t\"\"\"Verifies the element identified by `locator` does not contain the specified value.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\n\t\t| value | value it should not contain | Slim Shady |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' value does not contain '%s'\" % (locator, value))\n\t\t\n\t\telement = self._element_find(locator, True, True)\n\t\telem_value = str(element.get_attribute('value'))\n\t\t\n\t\tif value in elem_value:\n\t\t\traise AssertionError(\"Value '%s' was found in element '%s' while it shouldn't have\" % (value, locator))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nverifies the element identified by locator has focus.", "response": "def element_focus_should_be_set(self, locator):\n\t\t\"\"\"Verifies the element identified by `locator` has focus.\n\n\t\t| *Argument* | *Description* | *Example* |\n\t\t| locator | Selenium 2 element locator | id=my_id |\"\"\"\n\n\t\tself._info(\"Verifying element '%s' focus is set\" % locator)\n\t\tself._check_element_focus(True, locator)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef element_focus_should_not_be_set(self, locator):\n\n\t\tself._info(\"Verifying element '%s' focus is not set\" % locator)\n\t\tself._check_element_focus(False, locator)", "response": "Verifies the element identified by locator has focus."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef element_css_attribute_should_be(self, locator, prop, expected):\n\n\t\tself._info(\"Verifying element '%s' has css attribute '%s' with a value of '%s'\" % (locator, prop, expected))\n\t\tself._check_element_css_value(locator, prop, expected)", "response": "Verifies the element identified by locator has the expected css attribute for the targeted prop."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef wait_until_page_contains_one_of_these_elements(self, timeout, *locators):\n\t\t\n\t\tself._wait_until_no_error(timeout, self._wait_for_at_least_one_element, locators)", "response": "Waits until at least one of the specified elements is found."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wait_until_page_does_not_contain_these_elements(self, timeout, *locators):\n\n\t\tself._wait_until_no_error(timeout, self._wait_for_elements_to_go_away, locators)", "response": "Waits until all of the specified elements are not found on the page."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tap_key(self, key, complementKey=None) :\n\n\t\tdriver = self._current_browser()\n\n\t\tif (complementKey is not None) :\n\t\t\tActionChains(driver).key_down(complementKey).send_keys(key).key_up(complementKey).perform()\n\n\t\telse :\n\t\t\tActionChains(driver).send_keys(key).perform()", "response": "Presses the specified key."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nclicking the element specified by locator until the operation succeeds.", "response": "def wait_until_element_is_clickable(self, locator, timeout=None):\n\t\t\"\"\"Clicks the element specified by `locator` until the operation succeeds. This should be\n\t\tused with buttons that are generated in real-time and that don't have their click handling available\n\t\timmediately. This keyword avoids unclickable element exceptions.\n\n\t\t| =Argument= | =Description= | =Example= |\n\t\t| locator | Selenium 2 element locator(s) | id=MyId |\n\t\t| timeout | maximum time to wait, if set to ${None} it will use Selenium's default timeout | 5s |\"\"\"\n\n\t\tself._wait_until_no_error(timeout, self._wait_for_click_to_succeed, locator)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _visitor_impl(self, arg):\n if (_qualname(type(self)), type(arg)) in _methods:\n method = _methods[(_qualname(type(self)), type(arg))]\n return method(self, arg)\n else:\n # if no visitor method found for this arg type,\n # search in parent arg type:\n arg_parent_type = arg.__class__.__bases__[0]\n while arg_parent_type != object:\n if (_qualname(type(self)), arg_parent_type) in _methods:\n method = _methods[(_qualname(type(self)), arg_parent_type)]\n return method(self, arg)\n else:\n arg_parent_type = arg_parent_type.__bases__[0]\n raise VisitorException('No visitor found for class ' + str(type(arg)))", "response": "Actual visitor method implementation."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef visitor(arg_type):\n\n def decorator(fn):\n declaring_class = _declaring_class(fn)\n _methods[(declaring_class, arg_type)] = fn\n\n # Replace all decorated methods with _visitor_impl\n return _visitor_impl\n\n return decorator", "response": "Decorator that creates a visitor method."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a full absolute URL based on the request host.", "response": "def absolute(parser, token):\n '''\n Returns a full absolute URL based on the request host.\n\n This template tag takes exactly the same paramters as url template tag.\n '''\n node = url(parser, token)\n return AbsoluteUrlNode(\n view_name=node.view_name,\n args=node.args,\n kwargs=node.kwargs,\n asvar=node.asvar\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef site(parser, token):\n '''\n Returns a full absolute URL based on the current site.\n\n This template tag takes exactly the same paramters as url template tag.\n '''\n node = url(parser, token)\n return SiteUrlNode(\n view_name=node.view_name,\n args=node.args,\n kwargs=node.kwargs,\n asvar=node.asvar\n )", "response": "Returns a full absolute URL based on the current site."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _wait_until_exp(self, timeout, error, function, *args):\n\t\n\t\terror = error.replace('', self._format_timeout(timeout))\n\t\n\t\tdef wait_func():\n\t\t\treturn None if function(*args) else error\n\t\t\t\n\t\tself._wait_until_no_error_exp(timeout, wait_func)", "response": "This method will wait until the timeout is reached or the function returns False."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef rst(filename):\n '''\n Load rst file and sanitize it for PyPI.\n Remove unsupported github tags:\n - code-block directive\n '''\n content = open(filename).read()\n return re.sub(r'\\.\\.\\s? code-block::\\s*(\\w|\\+)+', '::', content)", "response": "Load an rst file and sanitize it for PyPI."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a dictionary of callable methods of object obj.", "response": "def get_callable_method_dict(obj):\n \"\"\"Returns a dictionary of callable methods of object `obj`.\n\n @param obj: ZOS API Python COM object\n @return: a dictionary of callable methods\n \n Notes: \n the function only returns the callable attributes that are listed by dir() \n function. Properties are not returned.\n \"\"\"\n methodDict = {}\n for methodStr in dir(obj):\n method = getattr(obj, methodStr, 'none')\n if callable(method) and not methodStr.startswith('_'):\n methodDict[methodStr] = method\n return methodDict"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreplicates callable methods from a source object to a destination object.", "response": "def replicate_methods(srcObj, dstObj):\n \"\"\"Replicate callable methods from a `srcObj` to `dstObj` (generally a wrapper object). \n \n @param srcObj: source object\n @param dstObj: destination object of the same type.\n @return : none\n \n Implementer notes: \n 1. Once the methods are mapped from the `srcObj` to the `dstObj`, the method calls will \n not get \"routed\" through `__getattr__` method (if implemented) in `type(dstObj)` class.\n 2. An example of what a 'key' and 'value' look like:\n key: MakeSequential\n value: >\n \"\"\"\n # prevent methods that we intend to specialize from being mapped. The specialized \n # (overridden) methods are methods with the same name as the corresponding method in \n # the source ZOS API COM object written for each ZOS API COM object in an associated \n # python script such as i_analyses_methods.py for I_Analyses\n overridden_methods = get_callable_method_dict(type(dstObj)).keys()\n #overridden_attrs = [each for each in type(dstObj).__dict__.keys() if not each.startswith('_')]\n # \n\n def zos_wrapper_deco(func):\n def wrapper(*args, **kwargs):\n return wrapped_zos_object(func(*args, **kwargs))\n varnames = func.im_func.func_code.co_varnames # alternative is to use inspect.getargspec\n params = [par for par in varnames if par not in ('self', 'ret')] # removes 'self' and 'ret'\n wrapper.__doc__ = func.im_func.func_name + '(' + ', '.join(params) + ')' \n return wrapper \n #\n for key, value in get_callable_method_dict(srcObj).items():\n if key not in overridden_methods:\n setattr(dstObj, key, zos_wrapper_deco(value))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of properties that are only getters and setters", "response": "def get_properties(zos_obj):\n \"\"\"Returns a lists of properties bound to the object `zos_obj`\n\n @param zos_obj: ZOS API Python COM object\n @return prop_get: list of properties that are only getters\n @return prop_set: list of properties that are both getters and setters\n \"\"\"\n prop_get = set(zos_obj._prop_map_get_.keys())\n prop_set = set(zos_obj._prop_map_put_.keys())\n if prop_set.issubset(prop_get):\n prop_get = prop_get.difference(prop_set)\n else:\n msg = 'Assumption all getters are also setters is incorrect!'\n raise NotImplementedError(msg)\n return list(prop_get), list(prop_set)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef managed_wrapper_class_factory(zos_obj):\n cls_name = repr(zos_obj).split()[0].split('.')[-1] \n dispatch_attr = '_' + cls_name.lower() # protocol to be followed to store the ZOS COM object\n \n cdict = {} # class dictionary\n\n # patch the properties of the base objects \n base_cls_list = inheritance_dict.get(cls_name, None)\n if base_cls_list:\n for base_cls_name in base_cls_list:\n getters, setters = get_properties(_CastTo(zos_obj, base_cls_name))\n for each in getters:\n exec(\"p{} = ZOSPropMapper('{}', '{}', cast_to='{}')\".format(each, dispatch_attr, each, base_cls_name), globals(), cdict)\n for each in setters:\n exec(\"p{} = ZOSPropMapper('{}', '{}', setter=True, cast_to='{}')\".format(each, dispatch_attr, each, base_cls_name), globals(), cdict)\n\n # patch the property attributes of the given ZOS object\n getters, setters = get_properties(zos_obj)\n for each in getters:\n exec(\"p{} = ZOSPropMapper('{}', '{}')\".format(each, dispatch_attr, each), globals(), cdict)\n for each in setters:\n exec(\"p{} = ZOSPropMapper('{}', '{}', setter=True)\".format(each, dispatch_attr, each), globals(), cdict)\n \n def __init__(self, zos_obj):\n \n # dispatcher attribute\n cls_name = repr(zos_obj).split()[0].split('.')[-1] \n dispatch_attr = '_' + cls_name.lower() # protocol to be followed to store the ZOS COM object\n self.__dict__[dispatch_attr] = zos_obj\n self._dispatch_attr_value = dispatch_attr # used in __getattr__\n \n # Store base class object \n self._base_cls_list = inheritance_dict.get(cls_name, None)\n\n # patch the methods of the base class(s) of the given ZOS object\n if self._base_cls_list:\n for base_cls_name in self._base_cls_list:\n replicate_methods(_CastTo(zos_obj, base_cls_name), self)\n\n # patch the methods of given ZOS object \n replicate_methods(zos_obj, self)\n\n # mark object as wrapped to prevent it from being wrapped subsequently\n self._wrapped = True\n \n # Provide a way to make property calls without the prefix p\n def __getattr__(self, attrname):\n return wrapped_zos_object(getattr(self.__dict__[self._dispatch_attr_value], attrname))\n\n def __repr__(self):\n if type(self).__name__ == 'IZOSAPI_Application':\n repr_str = \"{.__name__}(NumberOfOpticalSystems = {})\".format(type(self), self.pNumberOfOpticalSystems)\n else:\n repr_str = \"{.__name__}\".format(type(self))\n return repr_str\n \n cdict['__init__'] = __init__\n cdict['__getattr__'] = __getattr__\n cdict['__repr__'] = __repr__\n \n # patch custom methods from python files imported as modules\n module_import_str = \"\"\"\ntry: \n from pyzos.zos_obj_override.{module:} import *\nexcept ImportError:\n pass\n\"\"\".format(module=cls_name.lower() + '_methods')\n exec(module_import_str, globals(), cdict)\n\n _ = cdict.pop('print_function', None)\n _ = cdict.pop('division', None)\n \n return type(cls_name, (), cdict)", "response": "Creates and returns a wrapper class of a ZOS object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef generate_public_and_private(scheme='ecdsa-sha2-nistp256'):\n\n # Does 'scheme' have the correct format?\n # Verify that 'scheme' is of the correct type, and that it's one of the\n # supported ECDSA . It must conform to\n # 'securesystemslib.formats.ECDSA_SCHEME_SCHEMA'. Raise\n # 'securesystemslib.exceptions.FormatError' if the check fails.\n securesystemslib.formats.ECDSA_SCHEME_SCHEMA.check_match(scheme)\n\n public_key = None\n private_key = None\n\n # An if-clause is strictly not needed, since 'ecdsa_sha2-nistp256' is the\n # only currently supported ECDSA signature scheme. Nevertheness, include the\n # conditional statement to accomodate any schemes that might be added.\n if scheme == 'ecdsa-sha2-nistp256':\n private_key = ec.generate_private_key(ec.SECP256R1, default_backend())\n public_key = private_key.public_key()\n\n # The ECDSA_SCHEME_SCHEMA.check_match() above should have detected any\n # invalid 'scheme'. This is a defensive check.\n else: #pragma: no cover\n raise securesystemslib.exceptions.UnsupportedAlgorithmError('An unsupported'\n ' scheme specified: ' + repr(scheme) + '.\\n Supported'\n ' algorithms: ' + repr(_SUPPORTED_ECDSA_SCHEMES))\n\n private_pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,\n format=serialization.PrivateFormat.TraditionalOpenSSL,\n encryption_algorithm=serialization.NoEncryption())\n\n public_pem = public_key.public_bytes(encoding=serialization.Encoding.PEM,\n format=serialization.PublicFormat.SubjectPublicKeyInfo)\n\n return public_pem.decode('utf-8'), private_pem.decode('utf-8')", "response": "Generate a pair of public and private ECDSA keys."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_signature(public_key, private_key, data, scheme='ecdsa-sha2-nistp256'):\n\n # Do 'public_key' and 'private_key' have the correct format?\n # This check will ensure that the arguments conform to\n # 'securesystemslib.formats.PEMECDSA_SCHEMA'. Raise\n # 'securesystemslib.exceptions.FormatError' if the check fails.\n securesystemslib.formats.PEMECDSA_SCHEMA.check_match(public_key)\n\n # Is 'private_key' properly formatted?\n securesystemslib.formats.PEMECDSA_SCHEMA.check_match(private_key)\n\n # Is 'scheme' properly formatted?\n securesystemslib.formats.ECDSA_SCHEME_SCHEMA.check_match(scheme)\n\n # 'ecdsa-sha2-nistp256' is the only currently supported ECDSA scheme, so this\n # if-clause isn't strictly needed. Nevertheless, the conditional statement\n # is included to accommodate multiple schemes that can potentially be added\n # in the future.\n if scheme == 'ecdsa-sha2-nistp256':\n try:\n private_key = load_pem_private_key(private_key.encode('utf-8'),\n password=None, backend=default_backend())\n\n signature = private_key.sign(data, ec.ECDSA(hashes.SHA256()))\n\n except TypeError as e:\n raise securesystemslib.exceptions.CryptoError('Could not create'\n ' signature: ' + str(e))\n\n # A defensive check for an invalid 'scheme'. The\n # ECDSA_SCHEME_SCHEMA.check_match() above should have already validated it.\n else: #pragma: no cover\n raise securesystemslib.exceptions.UnsupportedAlgorithmError('Unsupported'\n ' signature scheme is specified: ' + repr(scheme))\n\n return signature, scheme", "response": "Create a signature for a public key and private key."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef verify_signature(public_key, scheme, signature, data):\n\n # Are the arguments properly formatted?\n # If not, raise 'securesystemslib.exceptions.FormatError'.\n securesystemslib.formats.PEMECDSA_SCHEMA.check_match(public_key)\n securesystemslib.formats.ECDSA_SCHEME_SCHEMA.check_match(scheme)\n securesystemslib.formats.ECDSASIGNATURE_SCHEMA.check_match(signature)\n\n ecdsa_key = load_pem_public_key(public_key.encode('utf-8'),\n backend=default_backend())\n\n if not isinstance(ecdsa_key, ec.EllipticCurvePublicKey):\n raise securesystemslib.exceptions.FormatError('Invalid ECDSA public'\n ' key: ' + repr(public_key))\n\n else:\n logger.debug('Loaded a valid ECDSA public key.')\n\n # verify() raises an 'InvalidSignature' exception if 'signature'\n # is invalid.\n try:\n ecdsa_key.verify(signature, data, ec.ECDSA(hashes.SHA256()))\n return True\n\n except (TypeError, cryptography.exceptions.InvalidSignature):\n return False", "response": "Verifies that the signature of data was generated by the private key associated with public_key."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_ecdsa_public_and_private_from_pem(pem, password=None):\n\n # Does 'pem' have the correct format?\n # This check will ensure 'pem' conforms to\n # 'securesystemslib.formats.ECDSARSA_SCHEMA'.\n securesystemslib.formats.PEMECDSA_SCHEMA.check_match(pem)\n\n if password is not None:\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)\n password = password.encode('utf-8')\n\n else:\n logger.debug('The password/passphrase is unset. The PEM is expected'\n ' to be unencrypted.')\n\n public = None\n private = None\n\n # Generate the public and private ECDSA keys. The pyca/cryptography library\n # performs the actual import operation.\n try:\n private = load_pem_private_key(pem.encode('utf-8'), password=password,\n backend=default_backend())\n\n except (ValueError, cryptography.exceptions.UnsupportedAlgorithm) as e:\n raise securesystemslib.exceptions.CryptoError('Could not import private'\n ' PEM.\\n' + str(e))\n\n public = private.public_key()\n\n # Serialize public and private keys to PEM format.\n private = private.private_bytes(encoding=serialization.Encoding.PEM,\n format=serialization.PrivateFormat.TraditionalOpenSSL,\n encryption_algorithm=serialization.NoEncryption())\n\n public = public.public_bytes(encoding=serialization.Encoding.PEM,\n format=serialization.PublicFormat.SubjectPublicKeyInfo)\n\n return public.decode('utf-8'), private.decode('utf-8')", "response": "Create public and private ECDSA keys from a PEM formatted private key."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_ecdsa_encrypted_pem(private_pem, passphrase):\n\n # Does 'private_key' have the correct format?\n # Raise 'securesystemslib.exceptions.FormatError' if the check fails.\n securesystemslib.formats.PEMRSA_SCHEMA.check_match(private_pem)\n\n # Does 'passphrase' have the correct format?\n securesystemslib.formats.PASSWORD_SCHEMA.check_match(passphrase)\n\n encrypted_pem = None\n\n private = load_pem_private_key(private_pem.encode('utf-8'), password=None,\n backend=default_backend())\n\n encrypted_private_pem = \\\n private.private_bytes(encoding=serialization.Encoding.PEM,\n format=serialization.PrivateFormat.TraditionalOpenSSL,\n encryption_algorithm=serialization.BestAvailableEncryption(passphrase.encode('utf-8')))\n\n return encrypted_private_pem", "response": "Create a private part of the ECDSA key in PEM format."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nclean any processing data and prepare object for reuse", "response": "def reset(self):\n \"\"\"Clean any processing data, and prepare object for reuse\n \"\"\"\n self.current_table = None\n self.tables = []\n self.data = [{}]\n self.additional_data = {}\n self.lines = []\n self.set_state('document')\n self.current_file = None\n self.set_of_energies = set()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses a single line of a file into a set of key - value pairs.", "response": "def _parse_line(self, file):\n \"\"\"Parse single line (or more if particular keyword actually demands it)\n\n :param file:\n :type file: file\n \"\"\"\n\n line = self._strip_comments(file.readline())\n # check if the file ended\n if not line:\n return False\n\n # line was empty or it was a comment, continue\n if line.strip() == '':\n return True\n\n # retrieve keyword and its value\n reg = re.search(\"^\\*(?P[^:#]*)(:\\s*(?P.*)\\s*)?$\", line)\n if reg:\n key = reg.group('key').strip()\n value = reg.group('value')\n\n if key in self.mapping[self.current_state]:\n self.mapping[self.current_state][key](value)\n elif self.strict:\n raise BadFormat(\"unknown key: *%s\" % key)\n else:\n raise BadFormat(\"line can not be parsed: %s\" % line)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nset current parsing state to table create new table object and add it to data collection", "response": "def _set_table(self, data):\n \"\"\"Set current parsing state to 'table',\n create new table object and add it to tables collection\n \"\"\"\n self.set_state('table')\n self.current_table = HEPTable(index=len(self.tables) + 1)\n self.tables.append(self.current_table)\n self.data.append(self.current_table.metadata)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_table_data(self, data):\n header = data.split(':')\n\n self.current_table.data_header = header\n\n for i, h in enumerate(header):\n header[i] = h.strip()\n\n x_count = header.count('x')\n y_count = header.count('y')\n\n if not self.current_table.xheaders:\n raise BadFormat(\"*xheader line needs to appear before *data: %s\" % data)\n\n if not self.current_table.yheaders:\n raise BadFormat(\"*yheader line needs to appear before *data: %s\" % data)\n\n # use deepcopy to avoid references in yaml... may not be required, and will very probably be refactored\n # TODO - is this appropriate behavior, or are references in YAML files acceptable, they are certainly less human readable\n self.current_table.data = {'independent_variables': [{'header': self.current_table.xheaders[i] if i < len(self.current_table.xheaders) else copy.deepcopy(self.current_table.xheaders[-1]),\n 'values': []} for i in range(x_count)],\n 'dependent_variables': [{'header': self.current_table.yheaders[i] if i < len(self.current_table.yheaders) else copy.deepcopy(self.current_table.yheaders[-1]),\n 'qualifiers': [self.current_table.qualifiers[j][i] if i < len(self.current_table.qualifiers[j]) else copy.deepcopy(self.current_table.qualifiers[j][-1]) for j in range(len(self.current_table.qualifiers)) ],\n 'values': []} for i in range(y_count)]}\n\n xy_mapping = []\n\n current_x_count = 0\n current_y_count = 0\n\n for h in header:\n if h == 'x':\n xy_mapping.append(current_x_count)\n current_x_count += 1\n if h == 'y':\n xy_mapping.append(current_y_count)\n current_y_count += 1\n\n last_index = self.current_file.tell()\n line = self._strip_comments(self.current_file.readline())\n\n while line and not line.startswith('*'):\n data_entry_elements = line.split(';')[:-1] # split and also strip newline character at the end\n\n if len(data_entry_elements) == len(header):\n # this is kind of a big stretch... I assume that x is always first\n for i, h in enumerate(header):\n single_element = data_entry_elements[i].strip()\n\n # number patterns copied from old subs.pl parsing script\n pmnum1 = '[-+]?[\\d]+\\.?[\\d]*'\n pmnum2 = '[-+]?\\.[\\d]+'\n pmnum3 = '[-+]?[\\d]+\\.?[\\d]*\\s*[eE]+\\s*[+-]?\\s*[\\d]+'\n pmnum = '(' + pmnum1 + '|' + pmnum2 + '|' + pmnum3 + ')'\n\n # implement same regular expression matching as in old subs.pl parsing script\n\n if h == 'x': # independent variables\n\n r = re.search('^(?P' + pmnum + ')$', single_element)\n if r: # \"value\"\n single_element = {'value': r.group('value')}\n else:\n r = re.search('^(?P' + pmnum + ')\\s*\\(\\s*BIN\\s*=\\s*(?P' + pmnum + \\\n ')\\s+TO\\s+(?P' + pmnum + ')\\s*\\)$', single_element)\n if r: # \"value (BIN=low TO high)\"\n single_element = {'value': float(r.group('value')),\n 'low': float(r.group('low')), 'high': float(r.group('high'))}\n else:\n r = re.search('^(?P' + pmnum + ')\\s+TO\\s+(?P' + pmnum + ')$',\n single_element)\n if r: # \"low TO high\"\n single_element = {'low': float(r.group('low')), 'high': float(r.group('high'))}\n else: # everything else: don't try to convert to float\n single_element = {'value': single_element}\n\n # TO DO: subs.pl also parses other formats such as \"low high\", \"value low high\" (sorted),\n # \"value +- err\", and \"value -err_m, +err_p\". Do we need to support these formats here?\n # Probably not: unsupported formats will just be written as a text string.\n\n self.current_table.data['independent_variables'][xy_mapping[i]]['values'].append(single_element)\n\n # extract energy if SQRT(S) is one of the 'x' variables\n xheader = self.current_table.data['independent_variables'][xy_mapping[i]]['header']\n if xheader['name'].startswith('SQRT(S)') and lower(xheader['units']) in ('gev'):\n for energy in single_element.values():\n try:\n energy = float(energy)\n self.set_of_energies.add(energy)\n except:\n pass\n\n elif h == 'y': # dependent variable\n\n pmnum_pct = pmnum + '(\\s*PCT)?' # errors can possibly be given as percentages\n\n r = re.search('^(?P' + pmnum + ')\\s+(?P' + pmnum_pct + '|-)\\s*,\\s*(?P' +\n pmnum_pct + '|-)\\s*(?P\\(\\s*DSYS=[^()]+\\s*\\))?$', single_element)\n element = {'errors': []}\n if r: # asymmetric first error\n element['value'] = r.group('value').strip()\n err_p = r.group('err_p').strip().lstrip('+')\n if err_p == '-': err_p = '' # represent missing error as '-' in oldhepdata format\n err_p = err_p[:-3].strip() + '%' if err_p[-3:] == 'PCT' else err_p\n err_m = r.group('err_m').strip().lstrip('+')\n if err_m == '-': err_m = '' # represent missing error as '-' in oldhepdata format\n err_m = err_m[:-3].strip() + '%' if err_m[-3:] == 'PCT' else err_m\n if err_p and err_m and err_p[-1] != '%' and err_m[-1] == '%':\n err_p = err_p + '%'\n if not err_p and not err_m:\n raise ValueError(\"Both asymmetric errors cannot be '-': %s\" % line)\n if r.group('err_sys'):\n element['errors'] += [{'label': 'stat', 'asymerror': {'plus': err_p, 'minus': err_m}}]\n else:\n element['errors'] += [{'asymerror': {'plus': err_p, 'minus': err_m}}]\n\n else:\n r = re.search('^(?P' + pmnum + ')\\s*(\\+-\\s*(?P' +\n pmnum_pct + '))?\\s*(?P\\(\\s*DSYS=[^()]+\\s*\\))?$', single_element)\n if r: # symmetric first error\n element['value'] = r.group('value').strip()\n if r.group('error'):\n error = r.group('error').strip().lstrip('+')\n error = error[:-3].strip() + '%' if error[-3:] == 'PCT' else error\n if r.group('err_sys'):\n element['errors'] += [{'label': 'stat', 'symerror': error}]\n else:\n element['errors'] += [{'symerror': error}]\n else: # everything else\n element['value'] = single_element\n\n err_sys = []\n if r and r.group('err_sys'):\n err_sys = r.group('err_sys').strip(' \\t()').split('DSYS=')\n\n for err in err_sys + self.current_table.dserrors:\n err = err.strip(' \\t,')\n if not err:\n continue\n error = {}\n label = 'sys'\n r = re.search('^(\\+-)?\\s*(?P' + pmnum_pct + ')\\s*(\\:\\s*(?P