{"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nimport the class for the given class name.", "response": "def import_class(class_path):\n '''\n Imports the class for the given class name.\n '''\n module_name, class_name = class_path.rsplit(\".\", 1)\n module = import_module(module_name)\n claz = getattr(module, class_name)\n return claz"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _executor(self):\n '''\n Creating an ExecutorPool is a costly operation. Executor needs to be instantiated only once.\n '''\n if self.EXECUTE_PARALLEL is False:\n executor_path = \"batch_requests.concurrent.executor.SequentialExecutor\"\n executor_class = import_class(executor_path)\n return executor_class()\n else:\n executor_path = self.CONCURRENT_EXECUTOR\n executor_class = import_class(executor_path)\n return executor_class(self.NUM_WORKERS)", "response": "Creating an ExecutorPool is a costly operation. Executor needs to be instantiated only once."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nmaking a new label for a key in the bucket.", "response": "def make_label(self, path):\n \"\"\"\n this borrows too much from the internals of ofs\n maybe expose different parts of the api?\n \"\"\"\n from datetime import datetime\n from StringIO import StringIO\n path = path.lstrip(\"/\")\n bucket, label = path.split(\"/\", 1)\n\n bucket = self.ofs._require_bucket(bucket)\n key = self.ofs._get_key(bucket, label)\n if key is None:\n key = bucket.new_key(label)\n self.ofs._update_key_metadata(key, { '_creation_time': str(datetime.utcnow()) })\n key.set_contents_from_file(StringIO(''))\n key.close()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the host and headers for the remote", "response": "def get_proxy_config(self, headers, path):\n \"\"\"\n stub. this really needs to be a call to the remote\n restful interface to get the appropriate host and\n headers to use for this upload\n \"\"\"\n self.ofs.conn.add_aws_auth_header(headers, 'PUT', path)\n from pprint import pprint\n pprint(headers)\n host = self.ofs.conn.server_name()\n return host, headers"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef proxy_upload(self, path, filename, content_type=None, content_encoding=None,\n cb=None, num_cb=None):\n \"\"\"\n This is the main function that uploads. We assume the bucket\n and key (== path) exists. What we do here is simple. Calculate\n the headers we will need, (e.g. md5, content-type, etc). Then\n we ask the self.get_proxy_config method to fill in the authentication\n information and tell us which remote host we should talk to\n for the upload. From there, the rest is ripped from\n boto.key.Key.send_file\n \"\"\"\n from boto.connection import AWSAuthConnection\n import mimetypes\n from hashlib import md5\n import base64\n\n BufferSize = 65536 ## set to something very small to make sure\n ## chunking is working properly\n fp = open(filename)\n\n headers = { 'Content-Type': content_type }\n\n if content_type is None:\n content_type = mimetypes.guess_type(filename)[0] or \"text/plain\"\n headers['Content-Type'] = content_type\n if content_encoding is not None:\n headers['Content-Encoding'] = content_encoding\n\n m = md5()\n fp.seek(0)\n s = fp.read(BufferSize)\n while s:\n m.update(s)\n s = fp.read(BufferSize)\n self.size = fp.tell()\n fp.seek(0)\n\n self.md5 = m.hexdigest()\n headers['Content-MD5'] = base64.encodestring(m.digest()).rstrip('\\n')\n headers['Content-Length'] = str(self.size)\n\n headers['Expect'] = '100-Continue'\n\n host, headers = self.get_proxy_config(headers, path)\n\n ### how to do this same thing with curl instead...\n print(\"curl -i --trace-ascii foo.log -T %s -H %s https://%s%s\" % (\n filename,\n \" -H \".join(\"'%s: %s'\" % (k,v) for k,v in headers.items()),\n host, path\n ))\n\n def sender(http_conn, method, path, data, headers):\n http_conn.putrequest(method, path)\n for key in headers:\n http_conn.putheader(key, headers[key])\n http_conn.endheaders()\n fp.seek(0)\n http_conn.set_debuglevel(0) ### XXX set to e.g. 4 to see what going on\n if cb:\n if num_cb > 2:\n cb_count = self.size / BufferSize / (num_cb-2)\n elif num_cb < 0:\n cb_count = -1\n else:\n cb_count = 0\n i = total_bytes = 0\n cb(total_bytes, self.size)\n l = fp.read(BufferSize)\n while len(l) > 0:\n http_conn.send(l)\n if cb:\n total_bytes += len(l)\n i += 1\n if i == cb_count or cb_count == -1:\n cb(total_bytes, self.size)\n i = 0\n l = fp.read(BufferSize)\n if cb:\n cb(total_bytes, self.size)\n response = http_conn.getresponse()\n body = response.read()\n fp.seek(0)\n if response.status == 500 or response.status == 503 or \\\n response.getheader('location'):\n # we'll try again\n return response\n elif response.status >= 200 and response.status <= 299:\n self.etag = response.getheader('etag')\n if self.etag != '\"%s\"' % self.md5:\n raise Exception('ETag from S3 did not match computed MD5')\n return response\n else:\n #raise provider.storage_response_error(\n # response.status, response.reason, body)\n raise Exception(response.status, response.reason, body)\n\n awsc = AWSAuthConnection(host,\n aws_access_key_id=\"key_id\",\n aws_secret_access_key=\"secret\")\n awsc._mexe('PUT', path, None, headers, sender=sender)", "response": "This function is the main function that uploads a file to a bucket and returns a dictionary of the key - value pairs that we will use to store the file in the bucket."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef fetch_all_mood_stations(self, terr=KKBOXTerritory.TAIWAN):\n '''\n Fetches all mood stations.\n\n :param terr: the current territory.\n :return: API response.\n :rtype: dict\n\n See `https://docs-en.kkbox.codes/v1.1/reference#moodstations`.\n '''\n url = 'https://api.kkbox.com/v1.1/mood-stations'\n url += '?' + url_parse.urlencode({'territory': terr})\n return self.http._post_data(url, None, self.http._headers_with_access_token())", "response": "Fetches all mood stations."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfetches a mood station by given ID.", "response": "def fetch_mood_station(self, station_id, terr=KKBOXTerritory.TAIWAN):\n '''\n Fetches a mood station by given ID.\n\n :param station_id: the station ID\n :param terr: the current territory.\n :return: API response.\n :rtype: dict\n\n See `https://docs-en.kkbox.codes/v1.1/reference#moodstations-station_id`.\n '''\n url = 'https://api.kkbox.com/v1.1/mood-stations/%s' % station_id\n url += '?' + url_parse.urlencode({'territory': terr})\n return self.http._post_data(url, None, self.http._headers_with_access_token())"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfetch next page based on previously fetched data.", "response": "def fetch_next_page(self, data):\n '''\n Fetches next page based on previously fetched data.\n Will get the next page url from data['paging']['next'].\n\n :param data: previously fetched API response.\n :type data: dict \n :return: API response.\n :rtype: dict\n '''\n next_url = data['paging']['next']\n if next_url != None:\n next_data = self.http._post_data(next_url, None, self.http._headers_with_access_token())\n return next_data\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfetch data from specific url.", "response": "def fetch_data(self, url):\n ''' \n Fetches data from specific url.\n\n :return: The response.\n :rtype: dict\n '''\n return self.http._post_data(url, None, self.http._headers_with_access_token())"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching a shared playlist by given ID.", "response": "def fetch_shared_playlist(self, playlist_id, terr=KKBOXTerritory.TAIWAN):\n '''\n Fetches a shared playlist by given ID.\n\n :param playlist_id: the playlist ID.\n :type playlist_id: str\n :param terr: the current territory.\n :return: API response.\n :rtype: dictcd\n\n See `https://docs-en.kkbox.codes/v1.1/reference#sharedplaylists-playlist_id`.\n '''\n url = 'https://api.kkbox.com/v1.1/shared-playlists/%s' % playlist_id\n url += '?' + url_parse.urlencode({'territory': terr})\n return self.http._post_data(url, None, self.http._headers_with_access_token())"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a FirewallRule object based on a server uuid and rule position.", "response": "def get_firewall_rule(self, server_uuid, firewall_rule_position, server_instance=None):\n \"\"\"\n Return a FirewallRule object based on server uuid and rule position.\n \"\"\"\n url = '/server/{0}/firewall_rule/{1}'.format(server_uuid, firewall_rule_position)\n res = self.get_request(url)\n return FirewallRule(**res['firewall_rule'])"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget all firewall rules based on a server instance or uuid.", "response": "def get_firewall_rules(self, server):\n \"\"\"\n Return all FirewallRule objects based on a server instance or uuid.\n \"\"\"\n server_uuid, server_instance = uuid_and_instance(server)\n\n url = '/server/{0}/firewall_rule'.format(server_uuid)\n res = self.get_request(url)\n\n return [\n FirewallRule(server=server_instance, **firewall_rule)\n for firewall_rule in res['firewall_rules']['firewall_rule']\n ]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_firewall_rule(self, server, firewall_rule_body):\n server_uuid, server_instance = uuid_and_instance(server)\n\n url = '/server/{0}/firewall_rule'.format(server_uuid)\n body = {'firewall_rule': firewall_rule_body}\n res = self.post_request(url, body)\n\n return FirewallRule(server=server_instance, **res['firewall_rule'])", "response": "Create a new firewall rule for a given server uuid."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a firewall rule based on a server uuid and rule position.", "response": "def delete_firewall_rule(self, server_uuid, firewall_rule_position):\n \"\"\"\n Delete a firewall rule based on a server uuid and rule position.\n \"\"\"\n url = '/server/{0}/firewall_rule/{1}'.format(server_uuid, firewall_rule_position)\n return self.request('DELETE', url)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef post(self, data):\n uri = '{}/sinkhole'.format(self.client.remote)\n self.logger.debug(uri)\n\n if PYVERSION == 2:\n try:\n data = data.decode('utf-8')\n except Exception:\n data = data.decode('latin-1')\n\n data = {\n 'message': data\n }\n\n body = self.client.post(uri, data)\n return body", "response": "POST a raw SMTP message to the Sinkhole API"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the lowered method. Capitalize headers, prepend HTTP_ and change - to _.", "response": "def pre_process_method_headers(method, headers):\n '''\n Returns the lowered method.\n Capitalize headers, prepend HTTP_ and change - to _.\n '''\n method = method.lower()\n\n # Standard WSGI supported headers\n _wsgi_headers = [\"content_length\", \"content_type\", \"query_string\",\n \"remote_addr\", \"remote_host\", \"remote_user\",\n \"request_method\", \"server_name\", \"server_port\"]\n\n _transformed_headers = {}\n\n # For every header, replace - to _, prepend http_ if necessary and convert\n # to upper case.\n for header, value in headers.items():\n\n header = header.replace(\"-\", \"_\")\n header = \"http_{header}\".format(\n header=header) if header.lower() not in _wsgi_headers else header\n _transformed_headers.update({header.upper(): value})\n\n return method, _transformed_headers"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef headers_to_include_from_request(curr_request):\n '''\n Define headers that needs to be included from the current request.\n '''\n return {\n h: v for h, v in curr_request.META.items() if h in _settings.HEADERS_TO_INCLUDE}", "response": "Define headers that need to be included from the current request."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_wsgi_request_object(curr_request, method, url, headers, body):\n '''\n Based on the given request parameters, constructs and returns the WSGI request object.\n '''\n x_headers = headers_to_include_from_request(curr_request)\n method, t_headers = pre_process_method_headers(method, headers)\n\n # Add default content type.\n if \"CONTENT_TYPE\" not in t_headers:\n t_headers.update({\"CONTENT_TYPE\": _settings.DEFAULT_CONTENT_TYPE})\n\n # Override existing batch requests headers with the new headers passed for this request.\n x_headers.update(t_headers)\n\n content_type = x_headers.get(\"CONTENT_TYPE\", _settings.DEFAULT_CONTENT_TYPE)\n\n # Get hold of request factory to construct the request.\n _request_factory = BatchRequestFactory()\n _request_provider = getattr(_request_factory, method)\n\n secure = _settings.USE_HTTPS\n\n request = _request_provider(url, data=body, secure=secure,\n content_type=content_type, **x_headers)\n\n return request", "response": "Constructs and returns the WSGI request object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _base_environ(self, **request):\n '''\n Override the default values for the wsgi environment variables.\n '''\n # This is a minimal valid WSGI environ dictionary, plus:\n # - HTTP_COOKIE: for cookie support,\n # - REMOTE_ADDR: often useful, see #8551.\n # See http://www.python.org/dev/peps/pep-3333/#environ-variables\n\n environ = {\n 'HTTP_COOKIE': self.cookies.output(header='', sep='; '),\n 'PATH_INFO': str('/'),\n 'REMOTE_ADDR': str('127.0.0.1'),\n 'REQUEST_METHOD': str('GET'),\n 'SCRIPT_NAME': str(''),\n 'SERVER_NAME': str('localhost'),\n 'SERVER_PORT': str('8000'),\n 'SERVER_PROTOCOL': str('HTTP/1.1'),\n 'wsgi.version': (1, 0),\n 'wsgi.url_scheme': str('http'),\n 'wsgi.input': FakePayload(b''),\n 'wsgi.errors': self.errors,\n 'wsgi.multiprocess': True,\n 'wsgi.multithread': True,\n 'wsgi.run_once': False,\n }\n environ.update(self.defaults)\n environ.update(request)\n return environ", "response": "Returns a dictionary of the wsgi environment variables."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nperforms a request to a given endpoint in UpCloud s API.", "response": "def request(self, method, endpoint, body=None, timeout=-1):\n \"\"\"\n Perform a request with a given body to a given endpoint in UpCloud's API.\n\n Handles errors with __error_middleware.\n \"\"\"\n if method not in set(['GET', 'POST', 'PUT', 'DELETE']):\n raise Exception('Invalid/Forbidden HTTP method')\n\n url = '/' + self.api_v + endpoint\n headers = {\n 'Authorization': self.token,\n 'Content-Type': 'application/json'\n }\n\n if body:\n json_body_or_None = json.dumps(body)\n else:\n json_body_or_None = None\n\n call_timeout = timeout if timeout != -1 else self.timeout\n\n APIcall = getattr(requests, method.lower())\n res = APIcall('https://api.upcloud.com' + url,\n data=json_body_or_None,\n headers=headers,\n timeout=call_timeout)\n\n if res.text:\n res_json = res.json()\n else:\n res_json = {}\n\n return self.__error_middleware(res, res_json)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef post_request(self, endpoint, body=None, timeout=-1):\n return self.request('POST', endpoint, body, timeout)", "response": "Perform a POST request to a given endpoint."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef put_stream(self, bucket, label, stream_object, params={}):\n ''' Create a new file to swift object storage. '''\n self.claim_bucket(bucket) \n self.connection.put_object(bucket, label, stream_object,\n headers=self._convert_to_meta(params))", "response": "Create a new file to swift object storage."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef h(gbm, array_or_frame, indices_or_columns = 'all'):\n\n if indices_or_columns == 'all':\n if gbm.max_depth < array_or_frame.shape[1]:\n raise \\\n Exception(\n \"gbm.max_depth == {} < array_or_frame.shape[1] == {}, so indices_or_columns must not be 'all'.\"\n .format(gbm.max_depth, array_or_frame.shape[1])\n )\n else:\n if gbm.max_depth < len(indices_or_columns):\n raise \\\n Exception(\n \"gbm.max_depth == {}, so indices_or_columns must contain at most {} {}.\"\n .format(gbm.max_depth, gbm.max_depth, \"element\" if gbm.max_depth == 1 else \"elements\")\n )\n check_args_contd(array_or_frame, indices_or_columns)\n\n arr, model_inds = get_arr_and_model_inds(array_or_frame, indices_or_columns)\n\n width = arr.shape[1]\n f_vals = {}\n for n in range(width, 0, -1):\n for inds in itertools.combinations(range(width), n):\n f_vals[inds] = compute_f_vals(gbm, model_inds, arr, inds)\n\n return compute_h_val(f_vals, arr, tuple(range(width)))", "response": "This function computes Friedman and Popescu s H statistic in order to look for an interaction among the variables represented by the elements of the array or frame."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\npurposing Compute Friedman and Popescu's two-variable H statistic, in order to look for an interaction in the passed gradient- boosting model between each pair of variables represented by the elements of the passed array or frame and specified by the passed indices or columns. See Jerome H. Friedman and Bogdan E. Popescu, 2008, \"Predictive learning via rule ensembles\", Ann. Appl. Stat. 2:916-954, http://projecteuclid.org/download/pdfview_1/euclid.aoas/1223908046, s. 8.1. ARGUMENTS gbm should be a scikit-learn gradient-boosting model (instance of sklearn.ensemble.GradientBoostingClassifier or sklearn.ensemble.GradientBoostingRegressor) that has been fitted to array_or_frame (and a target, not used here). array_or_frame should be a two-dimensional NumPy array or a pandas data frame (instance of numpy.ndarray or pandas .DataFrame). indices_or_columns is optional, with default value 'all'. It should be 'all' or a list of indices of columns of array_or_frame if array_or_frame is a NumPy array or a list of columns of array_or_frame if array_or_frame is a pandas data frame. If it is 'all', then all columns of array_or_frame are used. RETURNS A dict whose keys are pairs (2-tuples) of indices or columns and whose values are the H statistic of the pairs of variables or NaN if a computation is spoiled by weak main effects and rounding errors. H varies from 0 to 1. The larger H, the stronger the evidence for an interaction between a pair of variables. EXAMPLE Friedman and Popescu's (2008) formula (44) for every j and k corresponds to h_all_pairs(F, x) NOTES 1. Per Friedman and Popescu, only variables with strong main effects should be examined for interactions. Strengths of main effects are available as gbm.feature_importances_ once gbm has been fitted. 2. Per Friedman and Popescu, collinearity among variables can lead to interactions in gbm that are not present in the target function. To forestall such spurious interactions, check for strong correlations among variables before fitting gbm.", "response": "def h_all_pairs(gbm, array_or_frame, indices_or_columns = 'all'):\n \"\"\"\n PURPOSE\n\n Compute Friedman and Popescu's two-variable H statistic, in order to look for an interaction in the passed gradient-\n boosting model between each pair of variables represented by the elements of the passed array or frame and specified\n by the passed indices or columns.\n\n See Jerome H. Friedman and Bogdan E. Popescu, 2008, \"Predictive learning via rule ensembles\", Ann. Appl. Stat.\n 2:916-954, http://projecteuclid.org/download/pdfview_1/euclid.aoas/1223908046, s. 8.1.\n\n\n ARGUMENTS\n\n gbm should be a scikit-learn gradient-boosting model (instance of sklearn.ensemble.GradientBoostingClassifier or\n sklearn.ensemble.GradientBoostingRegressor) that has been fitted to array_or_frame (and a target, not used here).\n\n array_or_frame should be a two-dimensional NumPy array or a pandas data frame (instance of numpy.ndarray or pandas\n .DataFrame).\n\n indices_or_columns is optional, with default value 'all'. It should be 'all' or a list of indices of columns of\n array_or_frame if array_or_frame is a NumPy array or a list of columns of array_or_frame if array_or_frame is a\n pandas data frame. If it is 'all', then all columns of array_or_frame are used.\n\n\n RETURNS\n\n A dict whose keys are pairs (2-tuples) of indices or columns and whose values are the H statistic of the pairs of\n variables or NaN if a computation is spoiled by weak main effects and rounding errors.\n\n H varies from 0 to 1. The larger H, the stronger the evidence for an interaction between a pair of variables.\n\n\n EXAMPLE\n\n Friedman and Popescu's (2008) formula (44) for every j and k corresponds to\n\n h_all_pairs(F, x)\n\n\n NOTES\n\n 1. Per Friedman and Popescu, only variables with strong main effects should be examined for interactions. Strengths \n of main effects are available as gbm.feature_importances_ once gbm has been fitted.\n\n 2. Per Friedman and Popescu, collinearity among variables can lead to interactions in gbm that are not present in\n the target function. To forestall such spurious interactions, check for strong correlations among variables before\n fitting gbm.\n \"\"\"\n\n if gbm.max_depth < 2:\n raise Exception(\"gbm.max_depth must be at least 2.\")\n check_args_contd(array_or_frame, indices_or_columns)\n\n arr, model_inds = get_arr_and_model_inds(array_or_frame, indices_or_columns)\n\n width = arr.shape[1]\n f_vals = {}\n for n in [2, 1]:\n for inds in itertools.combinations(range(width), n):\n f_vals[inds] = compute_f_vals(gbm, model_inds, arr, inds)\n\n h_vals = {}\n for inds in itertools.combinations(range(width), 2):\n h_vals[inds] = compute_h_val(f_vals, arr, inds)\n if indices_or_columns != 'all':\n h_vals = {tuple(model_inds[(inds,)]): h_vals[inds] for inds in h_vals.keys()}\n if not isinstance(array_or_frame, np.ndarray):\n all_cols = array_or_frame.columns.values\n h_vals = {tuple(all_cols[(inds,)]): h_vals[inds] for inds in h_vals.keys()}\n\n return h_vals"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get(self, q, limit=None):\n uri = '{}/predict?q={}'.format(self.client.remote, q)\n self.logger.debug(uri)\n\n body = self.client.get(uri)\n return body['score']", "response": "Performs a search against the predict endpoint and returns the score of the item"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exists(self, bucket, label):\n '''Whether a given bucket:label object already exists.'''\n fn = self._zf(bucket, label)\n try:\n self.z.getinfo(fn)\n return True\n except KeyError:\n return False", "response": "Whether a given bucket label object already exists."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_labels(self, bucket):\n '''List labels for the given bucket. Due to zipfiles inherent arbitrary ordering,\n this is an expensive operation, as it walks the entire archive searching for individual\n 'buckets'\n\n :param bucket: bucket to list labels for.\n :return: iterator for the labels in the specified bucket.\n '''\n for name in self.z.namelist():\n container, label = self._nf(name.encode(\"utf-8\"))\n if container == bucket and label != MD_FILE:\n yield label", "response": "List the labels for the given bucket."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting all buckets managed by this OFS instance.", "response": "def list_buckets(self):\n '''List all buckets managed by this OFS instance. Like list_labels, this also\n walks the entire archive, yielding the bucketnames. A local set is retained so that\n duplicates aren't returned so this will temporarily pull the entire list into memory\n even though this is a generator and will slow as more buckets are added to the set.\n\n :return: iterator for the buckets.\n '''\n buckets = set()\n for name in self.z.namelist():\n bucket, _ = self._nf(name)\n if bucket not in buckets:\n buckets.add(bucket)\n yield bucket"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a bitstream for the given bucket and label combination.", "response": "def get_stream(self, bucket, label, as_stream=True):\n '''Get a bitstream for the given bucket:label combination.\n\n :param bucket: the bucket to use.\n :return: bitstream as a file-like object\n '''\n if self.mode == \"w\":\n raise OFSException(\"Cannot read from archive in 'w' mode\")\n elif self.exists(bucket, label):\n fn = self._zf(bucket, label)\n if as_stream:\n return self.z.open(fn)\n else:\n return self.z.read(fn)\n else:\n raise OFSFileNotFound"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_url(self, bucket, label):\n '''Get a URL that should point at the bucket:labelled resource. Aimed to aid web apps by allowing them to redirect to an open resource, rather than proxy the bitstream.\n\n :param bucket: the bucket to use.\n :param label: the label of the resource to get\n :return: a string URI - eg 'zip:file:///home/.../foo.zip!/bucket/label'\n '''\n if self.exists(bucket, label):\n root = \"zip:file//%s\" % os.path.abspath(self.zipfile)\n fn = self._zf(bucket, label)\n return \"!/\".join(root, fn)\n else:\n raise OFSFileNotFound", "response": "Get a URL that should point at the bucket labelled resource."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef put_stream(self, bucket, label, stream_object, params=None, replace=True, add_md=True):\n '''Put a bitstream (stream_object) for the specified bucket:label identifier.\n\n :param bucket: as standard\n :param label: as standard\n :param stream_object: file-like object to read from or bytestring.\n :param params: update metadata with these params (see `update_metadata`)\n '''\n if self.mode == \"r\":\n raise OFSException(\"Cannot write into archive in 'r' mode\")\n else:\n params = params or {}\n fn = self._zf(bucket, label)\n params['_creation_date'] = datetime.now().isoformat().split(\".\")[0] ## '2010-07-08T19:56:47'\n params['_label'] = label\n if self.exists(bucket, label) and replace==True:\n # Add then Replace? Let's see if that works...\n #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)\n zinfo = self.z.getinfo(fn)\n size, chksum = self._write(self.z, bucket, label, stream_object)\n self._del_stream(zinfo)\n #z.close()\n params['_content_length'] = size\n if chksum:\n params['_checksum'] = chksum\n else:\n #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)\n size, chksum = self._write(self.z, bucket, label, stream_object)\n #z.close()\n params['_content_length'] = size\n if chksum:\n params['_checksum'] = chksum\n if add_md:\n params = self.update_metadata(bucket, label, params)\n return params", "response": "Put a bitstream ( stream_object for the specified bucket label identifier."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef del_stream(self, bucket, label):\n '''Delete a bitstream. This needs more testing - file deletion in a zipfile\n is problematic. Alternate method is to create second zipfile without the files\n in question, which is not a nice method for large zip archives.\n '''\n if self.exists(bucket, label):\n name = self._zf(bucket, label)\n #z = ZipFile(self.zipfile, self.mode, self.compression, self.allowZip64)\n self._del_stream(name)", "response": "Delete a bitstream. This needs more testing - file deletion in a zipfile\n is problematic."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the metadata for this bucket and label identifier.", "response": "def get_metadata(self, bucket, label):\n '''Get the metadata for this bucket:label identifier.\n '''\n if self.mode !=\"w\":\n try:\n jsn = self._get_bucket_md(bucket)\n except OFSFileNotFound:\n # No MD found...\n return {}\n except OFSException as e:\n raise OFSException(e)\n if label in jsn:\n return jsn[label]\n else:\n return {}\n else:\n raise OFSException(\"Cannot read md from archive in 'w' mode\")"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update_metadata(self, bucket, label, params):\n '''Update the metadata with the provided dictionary of params.\n\n :param parmams: dictionary of key values (json serializable).\n '''\n if self.mode !=\"r\":\n try:\n payload = self._get_bucket_md(bucket)\n except OFSFileNotFound:\n # No MD found... create it\n payload = {}\n for l in self.list_labels(bucket):\n payload[l] = {}\n payload[l]['_label'] = l\n if not self.quiet:\n print(\"Had to create md file for %s\" % bucket)\n except OFSException as e:\n raise OFSException(e)\n if not label in payload:\n payload[label] = {}\n payload[label].update(params)\n self.put_stream(bucket, MD_FILE, json.dumps(payload).encode('utf-8'), params={}, replace=True, add_md=False)\n return payload[label]\n else:\n raise OFSException(\"Cannot update MD in archive in 'r' mode\")", "response": "Update the metadata with the provided dictionary of params."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef del_metadata_keys(self, bucket, label, keys):\n '''Delete the metadata corresponding to the specified keys.\n '''\n if self.mode !=\"r\":\n try:\n payload = self._get_bucket_md(bucket)\n except OFSFileNotFound:\n # No MD found...\n raise OFSFileNotFound(\"Couldn't find a md file for %s bucket\" % bucket)\n except OFSException as e:\n raise OFSException(e)\n if payload.has_key(label):\n for key in [x for x in keys if payload[label].has_key(x)]:\n del payload[label][key]\n self.put_stream(bucket, MD_FILE, json.dumps(payload), params={}, replace=True, add_md=False)\n else:\n raise OFSException(\"Cannot update MD in archive in 'r' mode\")", "response": "Delete the metadata corresponding to the specified keys."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_response(wsgi_request):\n '''\n Given a WSGI request, makes a call to a corresponding view\n function and returns the response.\n '''\n service_start_time = datetime.now()\n # Get the view / handler for this request\n view, args, kwargs = resolve(wsgi_request.path_info)\n\n kwargs.update({\"request\": wsgi_request})\n\n # Let the view do his task.\n try:\n resp = view(*args, **kwargs)\n except Exception as exc:\n resp = HttpResponseServerError(content=exc.message)\n\n headers = dict(resp._headers.values())\n # Convert HTTP response into simple dict type.\n d_resp = {\"status_code\": resp.status_code, \"reason_phrase\": resp.reason_phrase,\n \"headers\": headers}\n try:\n d_resp.update({\"body\": resp.content})\n except ContentNotRenderedError:\n resp.render()\n d_resp.update({\"body\": resp.content})\n\n # Check if we need to send across the duration header.\n if _settings.ADD_DURATION_HEADER:\n d_resp['headers'].update({_settings.DURATION_HEADER_NAME: (datetime.now() - service_start_time).seconds})\n\n return d_resp", "response": "Given a WSGI request returns a response dict."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nextract the individual requests and create a new WSGIRequest object for each.", "response": "def get_wsgi_requests(request):\n '''\n For the given batch request, extract the individual requests and create\n WSGIRequest object for each.\n '''\n valid_http_methods = [\"get\", \"post\", \"put\", \"patch\", \"delete\", \"head\", \"options\", \"connect\", \"trace\"]\n requests = json.loads(request.body)\n\n if type(requests) not in (list, tuple):\n raise BadBatchRequest(\"The body of batch request should always be list!\")\n\n # Max limit check.\n no_requests = len(requests)\n\n if no_requests > _settings.MAX_LIMIT:\n raise BadBatchRequest(\"You can batch maximum of %d requests.\" % (_settings.MAX_LIMIT))\n\n # We could mutate the current request with the respective parameters, but mutation is ghost in the dark,\n # so lets avoid. Construct the new WSGI request object for each request.\n\n def construct_wsgi_from_data(data):\n '''\n Given the data in the format of url, method, body and headers, construct a new\n WSGIRequest object.\n '''\n url = data.get(\"url\", None)\n method = data.get(\"method\", None)\n\n if url is None or method is None:\n raise BadBatchRequest(\"Request definition should have url, method defined.\")\n\n if method.lower() not in valid_http_methods:\n raise BadBatchRequest(\"Invalid request method.\")\n\n body = data.get(\"body\", \"\")\n headers = data.get(\"headers\", {})\n return get_wsgi_request_object(request, method, url, headers, body)\n\n return [construct_wsgi_from_data(data) for data in requests]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef handle_batch_requests(request, *args, **kwargs):\n '''\n A view function to handle the overall processing of batch requests.\n '''\n batch_start_time = datetime.now()\n try:\n # Get the Individual WSGI requests.\n wsgi_requests = get_wsgi_requests(request)\n except BadBatchRequest as brx:\n return HttpResponseBadRequest(content=brx.message)\n\n # Fire these WSGI requests, and collect the response for the same.\n response = execute_requests(wsgi_requests)\n\n # Evrything's done, return the response.\n resp = HttpResponse(\n content=json.dumps(response), content_type=\"application/json\")\n\n if _settings.ADD_DURATION_HEADER:\n resp.__setitem__(_settings.DURATION_HEADER_NAME, str((datetime.now() - batch_start_time).seconds))\n return resp", "response": "A view function to handle the overall processing of batch requests."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef search(self, keyword, types=[], terr=KKBOXTerritory.TAIWAN):\n '''\n Searches within KKBOX's database.\n\n :param keyword: the keyword.\n :type keyword: str\n :param types: the search types.\n :return: list\n :param terr: the current territory.\n :return: API response.\n :rtype: dict\n\n See `https://docs-en.kkbox.codes/v1.1/reference#search_1`.\n '''\n url = 'https://api.kkbox.com/v1.1/search'\n url += '?' + url_parse.urlencode({'q': keyword, 'territory': terr})\n if len(types) > 0:\n url += '&type=' + ','.join(types)\n return self.http._post_data(url, None, self.http._headers_with_access_token())", "response": "Search within KKBOX s database."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsaving the current state of the current IP address.", "response": "def save(self):\n \"\"\"\n IPAddress can only change its PTR record. Saves the current state, PUT /ip_address/uuid.\n \"\"\"\n body = {'ip_address': {'ptr_record': self.ptr_record}}\n data = self.cloud_manager.request('PUT', '/ip_address/' + self.address, body)\n self._reset(**data['ip_address'])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _create_ip_address_objs(ip_addresses, cloud_manager):\n # ip-addresses might be provided as a flat array or as a following dict:\n # {'ip_addresses': {'ip_address': [...]}} || {'ip_address': [...]}\n\n if 'ip_addresses' in ip_addresses:\n ip_addresses = ip_addresses['ip_addresses']\n\n if 'ip_address' in ip_addresses:\n ip_addresses = ip_addresses['ip_address']\n\n return [\n IPAddress(cloud_manager=cloud_manager, **ip_addr)\n for ip_addr in ip_addresses\n ]", "response": "Create IPAddress objects from API response data."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nresets the objects attributes.", "response": "def _reset(self, **kwargs):\n \"\"\"\n Reset the objects attributes.\n\n Accepts servers as either unflattened or flattened UUID strings or Server objects.\n \"\"\"\n super(Tag, self)._reset(**kwargs)\n\n # backup name for changing it (look: Tag.save)\n self._api_name = self.name\n\n # flatten { servers: { server: [] } }\n if 'server' in self.servers:\n self.servers = kwargs['servers']['server']\n\n # convert UUIDs into server objects\n if self.servers and isinstance(self.servers[0], six.string_types):\n self.servers = [Server(uuid=server, populated=False) for server in self.servers]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_servers(self, populate=False, tags_has_one=None, tags_has_all=None):\n if tags_has_all and tags_has_one:\n raise Exception('only one of (tags_has_all, tags_has_one) is allowed.')\n\n request = '/server'\n if tags_has_all:\n tags_has_all = [str(tag) for tag in tags_has_all]\n taglist = ':'.join(tags_has_all)\n request = '/server/tag/{0}'.format(taglist)\n\n if tags_has_one:\n tags_has_one = [str(tag) for tag in tags_has_one]\n taglist = ','.join(tags_has_one)\n request = '/server/tag/{0}'.format(taglist)\n\n servers = self.get_request(request)['servers']['server']\n\n server_list = list()\n for server in servers:\n server_list.append(Server(server, cloud_manager=self))\n\n if populate:\n for server_instance in server_list:\n server_instance.populate()\n\n return server_list", "response": "Returns a list of Server instances."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_server(self, UUID):\n server, IPAddresses, storages = self.get_server_data(UUID)\n\n return Server(\n server,\n ip_addresses=IPAddresses,\n storage_devices=storages,\n populated=True,\n cloud_manager=self\n )", "response": "Returns a populated Server instance."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a Server instance by its IP.", "response": "def get_server_by_ip(self, ip_address):\n \"\"\"\n Return a (populated) Server instance by its IP.\n\n Uses GET '/ip_address/x.x.x.x' to retrieve machine UUID using IP-address.\n \"\"\"\n data = self.get_request('/ip_address/{0}'.format(ip_address))\n UUID = data['ip_address']['server']\n return self.get_server(UUID)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_server(self, server):\n if isinstance(server, Server):\n body = server.prepare_post_body()\n else:\n server = Server._create_server_obj(server, cloud_manager=self)\n body = server.prepare_post_body()\n\n res = self.post_request('/server', body)\n\n server_to_return = server\n server_to_return._reset(\n res['server'],\n cloud_manager=self,\n populated=True\n )\n return server_to_return", "response": "Create a server and its storages based on a given server object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmodifying the server with the given UUID.", "response": "def modify_server(self, UUID, **kwargs):\n \"\"\"\n modify_server allows updating the server's updateable_fields.\n\n Note: Server's IP-addresses and Storages are managed by their own add/remove methods.\n \"\"\"\n body = dict()\n body['server'] = {}\n for arg in kwargs:\n if arg not in Server.updateable_fields:\n Exception('{0} is not an updateable field'.format(arg))\n body['server'][arg] = kwargs[arg]\n\n res = self.request('PUT', '/server/{0}'.format(UUID), body)\n server = res['server']\n\n # Populate subobjects\n IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),\n cloud_manager=self)\n\n storages = Storage._create_storage_objs(server.pop('storage_devices'),\n cloud_manager=self)\n\n return Server(\n server,\n ip_addresses=IPAddresses,\n storage_devices=storages,\n populated=True,\n cloud_manager=self\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_server_data(self, UUID):\n data = self.get_request('/server/{0}'.format(UUID))\n server = data['server']\n\n # Populate subobjects\n IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),\n cloud_manager=self)\n\n storages = Storage._create_storage_objs(server.pop('storage_devices'),\n cloud_manager=self)\n\n return server, IPAddresses, storages", "response": "Returns the server data in Python dict."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef feed(f, limit=25):\n if '/' not in f:\n raise ValueError('feed name must be formatted like: '\n 'csirtgadgets/scanners')\n\n user, f = f.split('/')\n\n return Feed().show(user, f, limit=limit)", "response": "Pull a feed by name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate an indicator in a feed", "response": "def indicator_create(f, i):\n \"\"\"\n Create an indicator in a feed\n :param f: feed name (eg: wes/test)\n :param i: indicator dict (eg: {'indicator': 'example.com', 'tags': ['ssh'],\n 'description': 'this is a test'})\n :return: dict of indicator\n \"\"\"\n if '/' not in f:\n raise ValueError('feed name must be formatted like: '\n 'csirtgadgets/scanners')\n\n if not i:\n raise ValueError('missing indicator dict')\n\n u, f = f.split('/')\n\n i['user'] = u\n i['feed'] = f\n\n ret = Indicator(i).submit()\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef convert_from_file(file):\n if isinstance(file, six_string_types):\n with open(file, 'rb') as f:\n return _internal_convert(f)\n else:\n return _internal_convert(file)", "response": "Reads the content of file in IDX format converts it into numpy. ndarray and\nBridge returns it."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _internal_convert(inp):\n '''\n Converts file in IDX format provided by file-like input into numpy.ndarray\n and returns it.\n '''\n\n # Read the \"magic number\" - 4 bytes.\n try:\n mn = struct.unpack('>BBBB', inp.read(4))\n except struct.error:\n raise FormatError(struct.error)\n\n # First two bytes are always zero, check it.\n if mn[0] != 0 or mn[1] != 0:\n msg = (\"Incorrect first two bytes of the magic number: \" +\n \"0x{0:02X} 0x{1:02X}\".format(mn[0], mn[1]))\n raise FormatError(msg)\n\n # 3rd byte is the data type code.\n dtype_code = mn[2]\n if dtype_code not in _DATA_TYPES_IDX:\n msg = \"Incorrect data type code: 0x{0:02X}\".format(dtype_code)\n raise FormatError(msg)\n\n # 4th byte is the number of dimensions.\n dims = int(mn[3])\n\n # See possible data types description.\n dtype, dtype_s, el_size = _DATA_TYPES_IDX[dtype_code]\n\n # 4-byte integer for length of each dimension.\n try:\n dims_sizes = struct.unpack('>' + 'I' * dims, inp.read(4 * dims))\n except struct.error as e:\n raise FormatError('Dims sizes: {0}'.format(e))\n\n # Full length of data.\n full_length = reduce(operator.mul, dims_sizes, 1)\n\n # Create a numpy array from the data\n try:\n result_array = numpy.frombuffer(\n inp.read(full_length * el_size),\n dtype=numpy.dtype(dtype)\n ).reshape(dims_sizes)\n except ValueError as e:\n raise FormatError('Error creating numpy array: {0}'.format(e))\n\n # Check for superfluous data.\n if len(inp.read(1)) > 0:\n raise FormatError('Superfluous data detected.')\n\n return result_array", "response": "Convert an IDX file into a numpy array."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwriting the contents of the numpy. ndarray ndarr to file in IDX format.", "response": "def convert_to_file(file, ndarr):\n \"\"\"\n Writes the contents of the numpy.ndarray ndarr to file in IDX format.\n file is a file-like object (with write() method) or a file name.\n \"\"\"\n if isinstance(file, six_string_types):\n with open(file, 'wb') as fp:\n _internal_write(fp, ndarr)\n else:\n _internal_write(file, ndarr)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef convert_to_string(ndarr):\n with contextlib.closing(BytesIO()) as bytesio:\n _internal_write(bytesio, ndarr)\n return bytesio.getvalue()", "response": "Converts a numpy. ndarray ndarr to bytes in IDX format and returns it."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite numpy. ndarray arr to a file - like object in IDX format.", "response": "def _internal_write(out_stream, arr):\n \"\"\"\n Writes numpy.ndarray arr to a file-like object (with write() method) in\n IDX format.\n \"\"\"\n\n if arr.size == 0:\n raise FormatError('Cannot encode empty array.')\n\n try:\n type_byte, struct_lib_type = _DATA_TYPES_NUMPY[str(arr.dtype)]\n except KeyError:\n raise FormatError('numpy ndarray type not supported by IDX format.')\n\n if arr.ndim > _MAX_IDX_DIMENSIONS:\n raise FormatError(\n 'IDX format cannot encode array with dimensions > 255')\n\n if max(arr.shape) > _MAX_AXIS_LENGTH:\n raise FormatError('IDX format cannot encode array with more than ' +\n str(_MAX_AXIS_LENGTH) + ' elements along any axis')\n\n # Write magic number\n out_stream.write(struct.pack('BBBB', 0, 0, type_byte, arr.ndim))\n\n # Write array dimensions\n out_stream.write(struct.pack('>' + 'I' * arr.ndim, *arr.shape))\n\n # Horrible hack to deal with horrible bug when using struct.pack to encode\n # unsigned ints in 2.7 and lower, see http://bugs.python.org/issue2263\n if sys.version_info < (2, 7) and str(arr.dtype) == 'uint8':\n arr_as_list = [int(i) for i in arr.reshape(-1)]\n out_stream.write(struct.pack('>' + struct_lib_type * arr.size,\n *arr_as_list))\n else:\n # Write array contents - note that the limit to number of arguments\n # doesn't apply to unrolled arguments\n out_stream.write(struct.pack('>' + struct_lib_type * arr.size,\n *arr.reshape(-1)))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfetching an access token by client credentials.", "response": "def fetch_access_token_by_client_credentials(self):\n '''\n There are three ways to let you start using KKBOX's Open/Partner\n API. The first way among them is to generate a client\n credential to fetch an access token to let KKBOX identify\n you. It allows you to access public data from KKBOX such as\n public albums, playlists and so on.\n\n However, you cannot use client credentials to access private\n data of a user. You have to let users to log-in into KKBOX and\n grant permissions for you to do so. You cannot use client\n credentials to do media playback either, since it requires a\n Premium Membership.\n\n :return: an access token\n :rtype: :class:`kkbox_sdk.KKBOXAccessToken`\n\n See `https://docs-en.kkbox.codes/docs/appendix-a`.\n '''\n client_credential_base = '%s:%s' % (self.client_id, self.client_secret)\n try:\n client_credentials = base64.b64encode(\n bytes(client_credential_base, 'utf-8'))\n except:\n client_credentials = base64.b64encode(client_credential_base)\n client_credentials = client_credentials.decode('utf-8')\n headers = {'Authorization': 'Basic ' + client_credentials,\n 'Content-type': 'application/x-www-form-urlencoded'}\n post_parameters = {'grant_type': 'client_credentials',\n 'scope': 'user_profile user_territory'}\n json_object = self.http._post_data(KKBOXOAuth.OAUTH_TOKEN_URL, post_parameters,\n headers)\n self.access_token = KKBOXAccessToken(**json_object)\n return self.access_token"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_OS_UUID(cls, os):\n if os in cls.templates:\n return cls.templates[os]\n\n uuid_regexp = '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'\n if re.search(uuid_regexp, os):\n return os\n\n raise Exception((\n \"Invalid OS -- valid options are: 'CentOS 6.5', 'CentOS 7.0', \"\n \"'Debian 7.8', 'Debian 8.0' ,'Ubuntu 12.04', 'Ubuntu 14.04', 'Ubuntu 16.04', \"\n \"'Windows 2008', 'Windows 2012'\"\n ))", "response": "Validate Storage OS and its UUID."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes the requests in parallel in an asynchronous way.", "response": "def execute(self, requests, resp_generator, *args, **kwargs):\n '''\n Calls the resp_generator for all the requests in parallel in an asynchronous way.\n '''\n result_futures = [self.executor_pool.submit(resp_generator, req, *args, **kwargs) for req in requests]\n resp = [res_future.result() for res_future in result_futures]\n return resp"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef execute(self, requests, resp_generator, *args, **kwargs):\n '''\n Calls the resp_generator for all the requests in sequential order.\n '''\n return [resp_generator(request) for request in requests]", "response": "Calls the resp_generator for all the requests in sequential order."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets up basic logging for the current user.", "response": "def setup_logging(args):\n \"\"\"\n Sets up basic logging\n\n :param args: ArgParse arguments\n :return: nothing. sets logger up globally\n \"\"\"\n loglevel = logging.WARNING\n if args.verbose:\n loglevel = logging.INFO\n if args.debug:\n loglevel = logging.DEBUG\n\n console = logging.StreamHandler()\n logging.getLogger('').setLevel(loglevel)\n console.setFormatter(logging.Formatter(LOG_FORMAT))\n logging.getLogger('').addHandler(console)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_ip(self, address):\n res = self.get_request('/ip_address/' + address)\n return IPAddress(cloud_manager=self, **res['ip_address'])", "response": "Get an IPAddress object with the IP address string from the API."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all IP objects from the API.", "response": "def get_ips(self):\n \"\"\"\n Get all IPAddress objects from the API.\n \"\"\"\n res = self.get_request('/ip_address')\n IPs = IPAddress._create_ip_address_objs(res['ip_addresses'], cloud_manager=self)\n return IPs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nattach a new ( random ) IPAddress to the given server.", "response": "def attach_ip(self, server, family='IPv4'):\n \"\"\"\n Attach a new (random) IPAddress to the given server (object or UUID).\n \"\"\"\n body = {\n 'ip_address': {\n 'server': str(server),\n 'family': family\n }\n }\n\n res = self.request('POST', '/ip_address', body)\n return IPAddress(cloud_manager=self, **res['ip_address'])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef modify_ip(self, ip_addr, ptr_record):\n body = {\n 'ip_address': {\n 'ptr_record': ptr_record\n }\n }\n\n res = self.request('PUT', '/ip_address/' + str(ip_addr), body)\n return IPAddress(cloud_manager=self, **res['ip_address'])", "response": "Modify an IP address."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef new(self, user, name, description=None):\n uri = self.client.remote + '/users/{0}/feeds'.format(user)\n\n data = {\n 'feed': {\n 'name': name,\n 'description': description\n }\n }\n\n resp = self.client.post(uri, data)\n return resp", "response": "Creates a new Feed object with the given user and name and description."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nremove a feed from the cache.", "response": "def delete(self, user, name):\n \"\"\"\n Removes a feed\n\n :param user: feed username\n :param name: feed name\n :return: true/false\n \"\"\"\n\n uri = self.client.remote + '/users/{}/feeds/{}'.format(user, name)\n\n resp = self.client.session.delete(uri)\n return resp.status_code"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of Feeds from the API", "response": "def index(self, user):\n \"\"\"\n Returns a list of Feeds from the API\n\n :param user: feed username\n :return: list\n\n Example:\n ret = feed.index('csirtgadgets')\n \"\"\"\n uri = self.client.remote + '/users/{0}/feeds'.format(user)\n return self.client.get(uri)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef show(self, user, name, limit=None, lasttime=None):\n uri = self.client.remote + '/users/{0}/feeds/{1}'.format(user, name)\n return self.client.get(uri, params={'limit': limit, 'lasttime': lasttime})", "response": "Returns a specific Feed from the API\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a Codenerix context", "response": "def codenerix(request):\n '''\n Codenerix CONTEXT\n '''\n # Get values\n DEBUG = getattr(settings, 'DEBUG', False)\n VERSION = getattr(settings, 'VERSION', _('WARNING: No version set to this code, add VERSION contant to your configuration'))\n \n # Set environment\n return {\n 'DEBUG': DEBUG,\n 'VERSION': VERSION,\n 'CODENERIX_VERSION': __version__,\n }"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\noverride django - bakery to skip profiles that raise 404", "response": "def build_object(self, obj):\n \"\"\"Override django-bakery to skip profiles that raise 404\"\"\"\n try:\n build_path = self.get_build_path(obj)\n self.request = self.create_request(build_path)\n self.request.user = AnonymousUser()\n self.set_kwargs(obj)\n self.build_file(build_path, self.get_content())\n except Http404:\n # cleanup directory\n self.unbuild_object(obj)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef make_schedule_row(schedule_day, slot, seen_items):\n row = ScheduleRow(schedule_day, slot)\n skip = {}\n expanding = {}\n all_items = list(slot.scheduleitem_set\n .select_related('talk', 'page', 'venue')\n .all())\n\n for item in all_items:\n if item in seen_items:\n # Inc rowspan\n seen_items[item]['rowspan'] += 1\n # Note that we need to skip this during colspan checks\n skip[item.venue] = seen_items[item]\n continue\n scheditem = {'item': item, 'rowspan': 1, 'colspan': 1}\n row.items[item.venue] = scheditem\n seen_items[item] = scheditem\n if item.expand:\n expanding[item.venue] = []\n\n empty = []\n expanding_right = None\n skipping = 0\n skip_item = None\n for venue in schedule_day.venues:\n if venue in skip:\n # We need to skip all the venues this item spans over\n skipping = 1\n skip_item = skip[venue]\n continue\n if venue in expanding:\n item = row.items[venue]\n for empty_venue in empty:\n row.items.pop(empty_venue)\n item['colspan'] += 1\n empty = []\n expanding_right = item\n elif venue in row.items:\n empty = []\n expanding_right = None\n elif expanding_right:\n expanding_right['colspan'] += 1\n elif skipping > 0 and skipping < skip_item['colspan']:\n skipping += 1\n else:\n skipping = 0\n empty.append(venue)\n row.items[venue] = {'item': None, 'rowspan': 1, 'colspan': 1}\n\n return row", "response": "Create a row for the schedule table."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nallowing adding a render_description parameter", "response": "def get_context_data(self, **kwargs):\n \"\"\"Allow adding a 'render_description' parameter\"\"\"\n context = super(ScheduleXmlView, self).get_context_data(**kwargs)\n if self.request.GET.get('render_description', None) == '1':\n context['render_description'] = True\n else:\n context['render_description'] = False\n return context"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a iCal file from the schedule", "response": "def get(self, request):\n \"\"\"Create a iCal file from the schedule\"\"\"\n # Heavily inspired by https://djangosnippets.org/snippets/2223/ and\n # the icalendar documentation\n calendar = Calendar()\n site = get_current_site(request)\n calendar.add('prodid', '-//%s Schedule//%s//' % (site.name, site.domain))\n calendar.add('version', '2.0')\n\n # Since we don't need to format anything here, we can just use a list\n # of schedule items\n for item in ScheduleItem.objects.all():\n sched_event = Event()\n sched_event.add('dtstamp', item.last_updated)\n sched_event.add('summary', item.get_title())\n sched_event.add('location', item.venue.name)\n sched_event.add('dtstart', item.get_start_datetime())\n sched_event.add('duration', datetime.timedelta(minutes=item.get_duration_minutes()))\n sched_event.add('class', 'PUBLIC')\n sched_event.add('uid', '%s@%s' % (item.pk, site.domain))\n calendar.add_component(sched_event)\n response = HttpResponse(calendar.to_ical(), content_type=\"text/calendar\")\n response['Content-Disposition'] = 'attachment; filename=schedule.ics'\n return response"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlook up a page by url which is a tree of slugs", "response": "def slug(request, url):\n \"\"\"Look up a page by url (which is a tree of slugs)\"\"\"\n page = None\n\n if url:\n for slug in url.split('/'):\n if not slug:\n continue\n try:\n page = Page.objects.get(slug=slug, parent=page)\n except Page.DoesNotExist:\n raise Http404\n else:\n try:\n page = Page.objects.get(slug='index', parent=None)\n except Page.DoesNotExist:\n return TemplateView.as_view(\n template_name='wafer/index.html')(request)\n\n if 'edit' in request.GET:\n if not request.user.has_perm('pages.change_page'):\n raise PermissionDenied\n return EditPage.as_view()(request, pk=page.id)\n\n if 'compare' in request.GET:\n if not request.user.has_perm('pages.change_page'):\n raise PermissionDenied\n return ComparePage.as_view()(request, pk=page.id)\n\n return ShowPage.as_view()(request, pk=page.id)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build_object(self, obj):\n if not obj.exclude_from_static:\n super(ShowPage, self).build_object(obj)", "response": "Override django - bakery to skip pages marked exclude_from_static"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build_object(self, obj):\n try:\n super(TalkView, self).build_object(obj)\n except PermissionDenied:\n # We cleanup the directory created\n self.unbuild_object(obj)", "response": "Override django - bakery to skip talks that raise 403"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_object(self, *args, **kwargs):\n '''Only talk owners can see talks, unless they've been accepted'''\n object_ = super(TalkView, self).get_object(*args, **kwargs)\n if not object_.can_view(self.request.user):\n raise PermissionDenied\n return object_", "response": "Only talk owners can see talks unless they've been accepted"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete(self, request, *args, **kwargs):\n talk = self.get_object()\n talk.status = WITHDRAWN\n talk.save()\n revisions.set_user(self.request.user)\n revisions.set_comment(\"Talk Withdrawn\")\n return HttpResponseRedirect(self.success_url)", "response": "Override delete to only withdraw"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build_queryset(self):\n paths = [(os.path.join(self.build_prefix, 'index.html'), {})]\n self.request = None\n queryset = self.get_queryset()\n paginator = self.get_paginator(queryset, self.get_paginate_by(queryset))\n for page in paginator.page_range:\n paths.append(\n (os.path.join(self.build_prefix, 'page',\n '%d' % page, 'index.html'), {'page': page}))\n for build_path, kwargs in paths:\n self.request = self.create_request(build_path)\n # Add a user with no permissions\n self.request.user = AnonymousUser()\n # Fake context so views work as expected\n self.kwargs = kwargs\n self.prep_directory(build_path)\n target_path = os.path.join(settings.BUILD_DIR, build_path)\n self.build_file(target_path, self.get_content())", "response": "Override django - bakery s build logic to fake pagination."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of groups that are available in the current language.", "response": "def get_groups(self, gs=None, processed=[], initial=True):\n '''\n <--------------------------------------- 12 columns ------------------------------------>\n <--- 6 columns ---> <--- 6 columns --->\n ------------------------------------------ ------------------------------------------\n | Info | | Personal |\n |==========================================| |==========================================|\n | ----------------- ------------------ | | |\n | | Passport | | Name | | | Phone Zipcode |\n | |=================| | [.....] [.....] | | | [...........................] [.......] |\n | | CID Country | | <- 6 -> <- 6 -> | | | <--- 8 columns ---> <-4 col-> |\n | | [.....] [.....] | | | | | |\n | | <- 6 -> <- 6 -> | ----------------- | | Address |\n | ----------------- | | [.....................................] |\n ------------------------------------------ | <--- 12 columns ---> |\n | [..] number |\n | <--- 12 columns ---> |\n | |\n ------------------------------------------\n group = [\n (_('Info'),(6,'#8a6d3b','#fcf8e3','center'),\n (_('Identification'),6,\n [\"cid\",6],\n [\"country\",6],\n ),\n (None,6,\n [\"name\",None,6],\n [\"surname\",None,6,False],\n ),\n ),\n (_('Personal'),6,\n [\"phone\",None,8],\n [\"zipcode\",None,4],\n [\"address\",None,12],\n [\"number\",None,12, True],\n ),\n ]\n\n Group: it is defined as tuple with 3 or more elements:\n Grammar: (, , , , ..., )\n If is None: no name will be given to the group and no panel decoration will be shown\n If is None: default of 6 will be used\n\n :\n it can be an integer that represent the size in columns\n it can be a tuple with several attributes where each element represents:\n (,'#','#','')\n\n :\n it can be a Group\n it can be a Field\n\n Examples:\n ('Info', 6, [\"name\",6], [\"surname\",6]) -> Info panel using 6 columns with 2 boxes 6 columns for each with name and surname inputs\n ('Info', (6,None,'#fcf8e3','center'), [\"name\",6], [\"surname\",6]) -> Info panel using 6 columns with a yellow brackground in centered title, 2 boxes, 6 columns for each with name and surname inputs\n ('Info', 12, ('Name', 6, [\"name\",12]), ('Surname',6, [\"surname\",12])) -> Info panel using 12 columns with 2 panels inside\n of 6 columns each named \"Name\" and \"Surname\" and inside each of them an input \"name\" and \"surname\" where it belongs.\n\n Field: must be a list with at least 1 element in it:\n Grammar: [, ,