{"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef lock_key(key_name,\n stash,\n passphrase,\n backend):\n \"\"\"Lock a key to prevent it from being deleted, purged or modified\n\n `KEY_NAME` is the name of the key to lock\n \"\"\"\n stash = _get_stash(backend, stash, passphrase)\n\n try:\n click.echo('Locking key...')\n stash.lock(key_name=key_name)\n click.echo('Key locked successfully')\n except GhostError as ex:\n sys.exit(ex)", "response": "Lock a key in a stash"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef unlock_key(key_name,\n stash,\n passphrase,\n backend):\n \"\"\"Unlock a key to allow it to be modified, deleted or purged\n\n `KEY_NAME` is the name of the key to unlock\n \"\"\"\n stash = _get_stash(backend, stash, passphrase)\n\n try:\n click.echo('Unlocking key...')\n stash.unlock(key_name=key_name)\n click.echo('Key unlocked successfully')\n except GhostError as ex:\n sys.exit(ex)", "response": "Unlock a key in a stash"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nretrieve a key from the stash", "response": "def get_key(key_name,\n value_name,\n jsonify,\n no_decrypt,\n stash,\n passphrase,\n backend):\n \"\"\"Retrieve a key from the stash\n\n \\b\n `KEY_NAME` is the name of the key to retrieve\n `VALUE_NAME` is a single value to retrieve e.g. if the value\n of the key `test` is `a=b,b=c`, `ghost get test a`a will return\n `b`\n \"\"\"\n if value_name and no_decrypt:\n sys.exit('VALUE_NAME cannot be used in conjuction with --no-decrypt')\n\n stash = _get_stash(backend, stash, passphrase, quiet=jsonify or value_name)\n\n try:\n key = stash.get(key_name=key_name, decrypt=not no_decrypt)\n except GhostError as ex:\n sys.exit(ex)\n\n if not key:\n sys.exit('Key `{0}` not found'.format(key_name))\n if value_name:\n key = key['value'].get(value_name)\n if not key:\n sys.exit(\n 'Value name `{0}` could not be found under key `{1}`'.format(\n value_name, key_name))\n\n if jsonify or value_name:\n click.echo(\n json.dumps(key, indent=4, sort_keys=False).strip('\"'),\n nl=True)\n else:\n click.echo('Retrieving key...')\n click.echo('\\n' + _prettify_dict(key))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete_key(key_name, stash, passphrase, backend):\n stash = _get_stash(backend, stash, passphrase)\n\n for key in key_name:\n try:\n click.echo('Deleting key {0}...'.format(key))\n stash.delete(key_name=key)\n except GhostError as ex:\n sys.exit(ex)\n click.echo('Keys deleted successfully')", "response": "Delete a key from the stash"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_keys(key_name,\n max_suggestions,\n cutoff,\n jsonify,\n locked,\n key_type,\n stash,\n passphrase,\n backend):\n \"\"\"List all keys in the stash\n\n If `KEY_NAME` is provided, will look for keys containing `KEY_NAME`.\n If `KEY_NAME` starts with `~`, close matches will be provided according\n to `max_suggestions` and `cutoff`.\n \"\"\"\n stash = _get_stash(backend, stash, passphrase, quiet=jsonify)\n\n try:\n keys = stash.list(\n key_name=key_name,\n max_suggestions=max_suggestions,\n cutoff=cutoff,\n locked_only=locked,\n key_type=key_type)\n except GhostError as ex:\n sys.exit(ex)\n if jsonify:\n click.echo(json.dumps(keys, indent=4, sort_keys=True))\n elif not keys:\n click.echo('The stash is empty. Go on, put some keys in there...')\n else:\n click.echo('Listing all keys...')\n click.echo(_prettify_list(keys))", "response": "List all keys in the stash."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef purge_stash(force, stash, passphrase, backend):\n stash = _get_stash(backend, stash, passphrase)\n\n try:\n click.echo('Purging stash...')\n stash.purge(force)\n # Maybe we should verify that the list is empty\n # afterwards?\n click.echo('Purge complete!')\n except GhostError as ex:\n sys.exit(ex)", "response": "Purge the stash from all of its keys\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexport all keys to a file", "response": "def export_keys(output_path, stash, passphrase, backend):\n \"\"\"Export all keys to a file\n \"\"\"\n stash = _get_stash(backend, stash, passphrase)\n\n try:\n click.echo('Exporting stash to {0}...'.format(output_path))\n stash.export(output_path=output_path)\n click.echo('Export complete!')\n except GhostError as ex:\n sys.exit(ex)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef load_keys(key_file, origin_passphrase, stash, passphrase, backend):\n stash = _get_stash(backend, stash, passphrase)\n\n click.echo('Importing all keys from {0}...'.format(key_file))\n stash.load(origin_passphrase, key_file=key_file)\n click.echo('Import complete!')", "response": "Load all keys from an exported key file to the stash\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmigrate all keys from one stash to another.", "response": "def migrate_stash(source_stash_path,\n source_passphrase,\n source_backend,\n destination_stash_path,\n destination_passphrase,\n destination_backend):\n \"\"\"Migrate all keys from a source stash to a destination stash.\n\n `SOURCE_STASH_PATH` and `DESTINATION_STASH_PATH` are the paths\n to the stashs you wish to perform the migration on.\n \"\"\"\n click.echo('Migrating all keys from {0} to {1}...'.format(\n source_stash_path, destination_stash_path))\n\n try:\n migrate(\n src_path=source_stash_path,\n src_passphrase=source_passphrase,\n src_backend=source_backend,\n dst_path=destination_stash_path,\n dst_passphrase=destination_passphrase,\n dst_backend=destination_backend)\n except GhostError as ex:\n sys.exit(ex)\n click.echo('Migration complete!')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef ssh(key_name, no_tunnel, stash, passphrase, backend):\n # TODO: find_executable or raise\n def execute(command):\n try:\n click.echo('Executing: {0}'.format(' '.join(command)))\n subprocess.check_call(' '.join(command), shell=True)\n except subprocess.CalledProcessError:\n sys.exit(1)\n\n stash = _get_stash(backend, stash, passphrase)\n key = stash.get(key_name)\n\n if key:\n _assert_is_ssh_type_key(key)\n else:\n sys.exit('Key `{0}` not found'.format(key_name))\n\n conn_info = key['value']\n ssh_key_path = conn_info.get('ssh_key_path')\n ssh_key = conn_info.get('ssh_key')\n proxy_key_path = conn_info.get('proxy_key_path')\n proxy_key = conn_info.get('proxy_key')\n\n id_file = _write_tmp(ssh_key) if ssh_key else ssh_key_path\n conn_info['ssh_key_path'] = id_file\n\n if conn_info.get('proxy'):\n proxy_id_file = _write_tmp(proxy_key) if proxy_key else proxy_key_path\n conn_info['proxy_key_path'] = proxy_id_file\n\n ssh_command = _build_ssh_command(conn_info, no_tunnel)\n try:\n execute(ssh_command)\n finally:\n # If they're not equal, that means we've created a temp one which\n # should be deleted, else, it's a path to an existing key file.\n if id_file != ssh_key_path:\n click.echo('Removing temp ssh key file: {0}...'.format(id_file))\n os.remove(id_file)\n if conn_info.get('proxy') and proxy_id_file != proxy_key_path:\n click.echo('Removing temp proxy key file: {0}...'.format(\n proxy_id_file))\n os.remove(proxy_id_file)", "response": "Use an ssh type key to connect to a machine via ssh."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding the SSH command to connect to the server.", "response": "def _build_ssh_command(conn_info, no_tunnel=False):\n \"\"\"\n # TODO: Document clearly\n IndetityFile=\"~/.ssh/id_rsa\"\n ProxyCommand=\"ssh -i ~/.ssh/id_rsa proxy_IP nc HOST_IP HOST_PORT\"\n \"\"\"\n command = ['ssh', '-i', conn_info['ssh_key_path'], conn_info['conn']]\n\n if conn_info.get('tunnel') and not no_tunnel:\n command.insert(1, conn_info.get('tunnel'))\n # Tunnel\n command.insert(1, '-L')\n # No shell\n command.insert(1, '-N')\n if conn_info.get('proxy'):\n command.extend(_build_proxy_command(conn_info))\n if conn_info.get('extend'):\n command.append(conn_info.get('extend'))\n return command"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef put(self,\n name,\n value=None,\n modify=False,\n metadata=None,\n description='',\n encrypt=True,\n lock=False,\n key_type='secret',\n add=False):\n \"\"\"Put a key inside the stash\n\n if key exists and modify true: delete and create\n if key exists and modify false: fail\n if key doesn't exist and modify true: fail\n if key doesn't exist and modify false: create\n\n `name` is unique and cannot be changed.\n\n `value` must be provided if the key didn't already exist, otherwise,\n the previous value will be retained.\n\n `created_at` will be left unmodified if the key\n already existed. Otherwise, the current time will be used.\n\n `modified_at` will be changed to the current time\n if the field is being modified.\n\n `metadata` will be updated if provided. If it wasn't\n provided the field from the existing key will be used and the\n same goes for the `uid` which will be generated if it didn't\n previously exist.\n\n `lock` will lock the key to prevent it from being modified or deleted\n\n `add` allows to add values to an existing key instead of overwriting.\n\n Returns the id of the key in the database\n \"\"\"\n def assert_key_is_unlocked(existing_key):\n if existing_key and existing_key.get('lock'):\n raise GhostError(\n 'Key `{0}` is locked and therefore cannot be modified. '\n 'Unlock the key and try again'.format(name))\n\n def assert_value_provided_for_new_key(value, existing_key):\n if not value and not existing_key.get('value'):\n raise GhostError('You must provide a value for new keys')\n\n self._assert_valid_stash()\n self._validate_key_schema(value, key_type)\n if value and encrypt and not isinstance(value, dict):\n raise GhostError('Value must be of type dict')\n\n # TODO: This should be refactored. `_handle_existing_key` deletes\n # the key rather implicitly. It shouldn't do that.\n # `existing_key` will be an empty dict if it doesn't exist\n key = self._handle_existing_key(name, modify or add)\n assert_key_is_unlocked(key)\n assert_value_provided_for_new_key(value, key)\n\n new_key = dict(name=name, lock=lock)\n if value:\n # TODO: fix edge case in which encrypt is false and yet we might\n # try to add to an existing key. encrypt=false is only used when\n # `load`ing into a new stash, but someone might use it directly\n # from the API.\n if add:\n value = self._update_existing_key(key, value)\n new_key['value'] = self._encrypt(value) if encrypt else value\n else:\n new_key['value'] = key.get('value')\n\n # TODO: Treat a case in which we try to update an existing key\n # but don't provide a value in which nothing will happen.\n new_key['description'] = description or key.get('description')\n new_key['created_at'] = key.get('created_at') or _get_current_time()\n new_key['modified_at'] = _get_current_time()\n new_key['metadata'] = metadata or key.get('metadata')\n new_key['uid'] = key.get('uid') or str(uuid.uuid4())\n new_key['type'] = key.get('type') or key_type\n\n key_id = self._storage.put(new_key)\n\n audit(\n storage=self._storage.db_path,\n action='MODIFY' if (modify or add) else 'PUT',\n message=json.dumps(dict(\n key_name=new_key['name'],\n value='HIDDEN',\n description=new_key['description'],\n uid=new_key['uid'],\n metadata=json.dumps(new_key['metadata']),\n lock=new_key['lock'],\n type=new_key['type'])))\n\n return key_id", "response": "Put a key in the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(self, key_name, decrypt=True):\n self._assert_valid_stash()\n\n key = self._storage.get(key_name).copy()\n if not key.get('value'):\n return None\n if decrypt:\n key['value'] = self._decrypt(key['value'])\n\n audit(\n storage=self._storage.db_path,\n action='GET',\n message=json.dumps(dict(key_name=key_name)))\n\n return key", "response": "Return a key with its parameters if it was found."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of all keys.", "response": "def list(self,\n key_name=None,\n max_suggestions=100,\n cutoff=0.5,\n locked_only=False,\n key_type=None):\n \"\"\"Return a list of all keys.\n \"\"\"\n self._assert_valid_stash()\n\n key_list = [k for k in self._storage.list()\n if k['name'] != 'stored_passphrase' and\n (k.get('lock') if locked_only else True)]\n\n if key_type:\n # To maintain backward compatibility with keys without a type.\n # The default key type is secret, in which case we also look for\n # keys with no (None) types.\n types = ('secret', None) if key_type == 'secret' else [key_type]\n key_list = [k for k in key_list if k.get('type') in types]\n\n key_list = [k['name'] for k in key_list]\n if key_name:\n if key_name.startswith('~'):\n key_list = difflib.get_close_matches(\n key_name.lstrip('~'), key_list, max_suggestions, cutoff)\n else:\n key_list = [k for k in key_list if key_name in k]\n\n audit(\n storage=self._storage.db_path,\n action='LIST' + ('[LOCKED]' if locked_only else ''),\n message=json.dumps(dict()))\n\n return key_list"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes a key from the storage.", "response": "def delete(self, key_name):\n \"\"\"Delete a key if it exists.\n \"\"\"\n self._assert_valid_stash()\n\n if key_name == 'stored_passphrase':\n raise GhostError(\n '`stored_passphrase` is a reserved ghost key name '\n 'which cannot be deleted')\n\n # TODO: Optimize. We get from the storage twice here for no reason\n if not self.get(key_name):\n raise GhostError('Key `{0}` not found'.format(key_name))\n key = self._storage.get(key_name)\n if key.get('lock'):\n raise GhostError(\n 'Key `{0}` is locked and therefore cannot be deleted '\n 'Please unlock the key and try again'.format(key_name))\n\n deleted = self._storage.delete(key_name)\n\n audit(\n storage=self._storage.db_path,\n action='DELETE',\n message=json.dumps(dict(key_name=key_name)))\n\n if not deleted:\n raise GhostError('Failed to delete {0}'.format(key_name))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\npurges the stash from all keys", "response": "def purge(self, force=False, key_type=None):\n \"\"\"Purge the stash from all keys\n \"\"\"\n self._assert_valid_stash()\n\n if not force:\n raise GhostError(\n \"The `force` flag must be provided to perform a stash purge. \"\n \"I mean, you don't really want to just delete everything \"\n \"without precautionary measures eh?\")\n\n audit(\n storage=self._storage.db_path,\n action='PURGE',\n message=json.dumps(dict()))\n\n for key_name in self.list(key_type=key_type):\n self.delete(key_name)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef export(self, output_path=None, decrypt=False):\n self._assert_valid_stash()\n\n all_keys = []\n for key in self.list():\n # We `dict` this as a precaution as tinydb returns\n # a tinydb.database.Element instead of a dictionary\n # and well.. I ain't taking no chances\n all_keys.append(dict(self.get(key, decrypt=decrypt)))\n if all_keys:\n if output_path:\n with open(output_path, 'w') as output_file:\n output_file.write(json.dumps(all_keys, indent=4))\n return all_keys\n else:\n raise GhostError('There are no keys to export')", "response": "Export all keys in the stash to a list or a file"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef load(self, origin_passphrase, keys=None, key_file=None):\n # TODO: Handle keys not dict or key_file not json\n self._assert_valid_stash()\n\n # Check if both or none are provided (ahh, the mighty xor)\n if not (bool(keys) ^ bool(key_file)):\n raise GhostError(\n 'You must either provide a path to an exported stash file '\n 'or a list of key dicts to import')\n if key_file:\n with open(key_file) as stash_file:\n keys = json.loads(stash_file.read())\n\n # If the passphrases are the same, there's no reason to decrypt\n # and re-encrypt. We can simply pass the value.\n decrypt = origin_passphrase != self.passphrase\n if decrypt:\n # TODO: The fact that we need to create a stub stash just to\n # decrypt means we should probably have some encryptor class.\n stub = Stash(TinyDBStorage('stub'), origin_passphrase)\n # TODO: Handle existing keys when loading\n for key in keys:\n self.put(\n name=key['name'],\n value=stub._decrypt(key['value']) if decrypt else key['value'],\n metadata=key['metadata'],\n description=key['description'],\n lock=key.get('lock'),\n key_type=key.get('type'),\n encrypt=decrypt)", "response": "Load the keys from a file or a list of keys."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nturns a json serializable value into an encrypted hexa string.", "response": "def _encrypt(self, value):\n \"\"\"Turn a json serializable value into an jsonified, encrypted,\n hexa string.\n \"\"\"\n value = json.dumps(value)\n with warnings.catch_warnings():\n warnings.simplefilter(\"ignore\")\n encrypted_value = self.cipher.encrypt(value.encode('utf8'))\n hexified_value = binascii.hexlify(encrypted_value).decode('ascii')\n return hexified_value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a dictionary consisting of the key itself", "response": "def get(self, key_name):\n \"\"\"Return a dictionary consisting of the key itself\n\n e.g.\n {u'created_at': u'2016-10-10 08:31:53',\n u'description': None,\n u'metadata': None,\n u'modified_at': u'2016-10-10 08:31:53',\n u'name': u'aws',\n u'uid': u'459f12c0-f341-413e-9d7e-7410f912fb74',\n u'value': u'the_value'}\n\n \"\"\"\n result = self.db.search(Query().name == key_name)\n if not result:\n return {}\n return result[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete(self, key_name):\n self.db.remove(Query().name == key_name)\n return self.get(key_name) == {}", "response": "Delete the key and return true if the key was deleted otherwise false"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _construct_key(self, values):\n key = {}\n for column, value in zip(self.keys.columns, values):\n key.update({column.name: value})\n return key", "response": "Construct a key from a list of columns\n and a tuple of values\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef put(self, key):\n self._consul_request('PUT', self._key_url(key['name']), json=key)\n return key['name']", "response": "Put and return the only unique identifier possible"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef put(self, key):\n self.client.write(self._key_path(key['name']), **key)\n return self._key_path(key['name'])", "response": "Put and return the only unique identifier possible"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate an Elasticsearch index if necessary", "response": "def init(self):\n \"\"\"Create an Elasticsearch index if necessary\n \"\"\"\n # ignore 400 (IndexAlreadyExistsException) when creating an index\n self.es.indices.create(index=self.params['index'], ignore=400)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef init(self):\n try:\n self.client.create_bucket(\n Bucket=self.db_path,\n CreateBucketConfiguration=self.bucket_configuration)\n except botocore.exceptions.ClientError as e:\n # If the bucket already exists\n if 'BucketAlreadyOwnedByYou' not in str(\n e.response['Error']['Code']):\n raise e", "response": "Create a new bucket."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninserting the key into the database.", "response": "def put(self, key):\n \"\"\"Insert the key\n :return: Key name\n \"\"\"\n self.client.put_object(\n Body=json.dumps(key),\n Bucket=self.db_path,\n Key=key['name'])\n return key['name']"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list(self):\n response = self.client.list_objects_v2(Bucket=self.db_path)\n if u'Contents' in response:\n # Filter out everything but the key names\n keys = [key[u'Key'] for key in response[u'Contents']]\n keys_list = []\n\n for key_name in keys:\n key = self.get(key_name)\n keys_list.append(key)\n\n return keys_list\n return []", "response": "Lists the keys\n :return: Returns a list of all keys (not just key names, but rather\n the keys themselves)."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the key itself in a dictionary.", "response": "def get(self, key_name):\n \"\"\"Gets the key.\n :return: The key itself in a dictionary\n \"\"\"\n try:\n obj = self.client.get_object(\n Bucket=self.db_path,\n Key=key_name)['Body'].read().decode(\"utf-8\")\n\n return json.loads(obj)\n except botocore.exceptions.ClientError as e:\n if 'NoSuchKey' in str(e.response['Error']['Code']):\n return {}\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes the object with the given key name.", "response": "def delete(self, key_name):\n \"\"\"Delete the key.\n :return: True if it was deleted, False otherwise\n \"\"\"\n self.client.delete_object(\n Bucket=self.db_path,\n Key=key_name)\n\n return self.get(key_name) == {}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_initialized(self):\n try:\n return self.client.head_bucket(\n Bucket=self.db_path)['ResponseMetadata']['HTTPStatusCode'] \\\n == 200\n except botocore.exceptions.ClientError as e:\n # If a client error is thrown, then check that it was a 404 error.\n # If it was a 404 error, then the bucket does not exist.\n if 'NoSuchBucket' in str(e.response['Error']['Code']):\n return False\n raise e", "response": "Check if the resource in the database is initialized."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef terminal(port=default_port(), baud='9600'):\n testargs = ['nodemcu-uploader', port, baud]\n # TODO: modifying argv is no good\n sys.argv = testargs\n # resuse miniterm on main function\n miniterm.main()", "response": "Launch minterm from pyserial"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __set_baudrate(self, baud):\n log.info('Changing communication to %s baud', baud)\n self.__writeln(UART_SETUP.format(baud=baud))\n # Wait for the string to be sent before switching baud\n time.sleep(0.1)\n try:\n self._port.setBaudrate(baud)\n except AttributeError:\n #pySerial 2.7\n self._port.baudrate = baud", "response": "set baudrate of the serial port"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_timeout(self, timeout):\n timeout = int(timeout) # will raise on Error\n self._timeout = timeout == 0 and 999999 or timeout", "response": "Set the timeout for the communication with the device."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __clear_buffers(self):\n try:\n self._port.reset_input_buffer()\n self._port.reset_output_buffer()\n except AttributeError:\n #pySerial 2.7\n self._port.flushInput()\n self._port.flushOutput()", "response": "Clears the input and output buffers"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __expect(self, exp='> ', timeout=None):\n timeout_before = self._port.timeout\n timeout = timeout or self._timeout\n #do NOT set timeout on Windows\n if SYSTEM != 'Windows':\n # Checking for new data every 100us is fast enough\n if self._port.timeout != MINIMAL_TIMEOUT:\n self._port.timeout = MINIMAL_TIMEOUT\n\n end = time.time() + timeout\n\n # Finish as soon as either exp matches or we run out of time (work like dump, but faster on success)\n data = ''\n while not data.endswith(exp) and time.time() <= end:\n data += self._port.read()\n\n log.debug('expect returned: `{0}`'.format(data))\n if time.time() > end:\n raise CommunicationTimeout('Timeout waiting for data', data)\n\n if not data.endswith(exp) and len(exp) > 0:\n raise BadResponseException('Bad response.', exp, data)\n\n if SYSTEM != 'Windows':\n self._port.timeout = timeout_before\n\n return data", "response": "expect a response from nodemcu or timeout"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __write(self, output, binary=False):\n if not binary:\n log.debug('write: %s', output)\n else:\n log.debug('write binary: %s', hexify(output))\n self._port.write(output)\n self._port.flush()", "response": "write data on the nodemcu port."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __exchange(self, output, timeout=None):\n self.__writeln(output)\n self._port.flush()\n return self.__expect(timeout=timeout or self._timeout)", "response": "Write output to the port and wait for response"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef close(self):\n try:\n if self.baud != self.start_baud:\n self.__set_baudrate(self.start_baud)\n self._port.flush()\n self.__clear_buffers()\n except serial.serialutil.SerialException:\n pass\n log.debug('closing port')\n self._port.close()", "response": "restores the nodemcu baudrate and then closes the port"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef prepare(self):\n log.info('Preparing esp for transfer.')\n\n for func in LUA_FUNCTIONS:\n detected = self.__exchange('print({0})'.format(func))\n if detected.find('function:') == -1:\n break\n else:\n log.info('Preparation already done. Not adding functions again.')\n return True\n functions = RECV_LUA + '\\n' + SEND_LUA\n data = functions.format(baud=self._port.baudrate)\n ##change any \\r\\n to just \\n and split on that\n lines = data.replace('\\r', '').split('\\n')\n\n #remove some unneccesary spaces to conserve some bytes\n for line in lines:\n line = line.strip().replace(', ', ',').replace(' = ', '=')\n\n if len(line) == 0:\n continue\n\n resp = self.__exchange(line)\n #do some basic test of the result\n if ('unexpected' in resp) or ('stdin' in resp) or len(resp) > len(functions)+10:\n log.error('error when preparing \"%s\"', resp)\n return False\n return True", "response": "This method uploads the protocol functions nessecary to do binary transfer and returns True if successful False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef download_file(self, filename):\n res = self.__exchange('send(\"{filename}\")'.format(filename=filename))\n if ('unexpected' in res) or ('stdin' in res):\n log.error('Unexpected error downloading file: %s', res)\n raise Exception('Unexpected error downloading file')\n\n #tell device we are ready to receive\n self.__write('C')\n #we should get a NUL terminated filename to start with\n sent_filename = self.__expect(NUL).strip()\n log.info('receiveing ' + sent_filename)\n\n #ACK to start download\n self.__write(ACK, True)\n buf = ''\n\n data = ''\n chunk, buf = self.__read_chunk(buf)\n #read chunks until we get an empty which is the end\n while chunk != '':\n self.__write(ACK, True)\n data = data + chunk\n chunk, buf = self.__read_chunk(buf)\n return data", "response": "Download a file from device to local filesystem"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_file(self, filename, destination=''):\n if not destination:\n destination = filename\n log.info('Transferring %s to %s', filename, destination)\n data = self.download_file(filename)\n\n # Just in case, the filename may contain folder, so create it if needed.\n log.info(destination)\n if not os.path.exists(os.path.dirname(destination)):\n try:\n os.makedirs(os.path.dirname(destination))\n except OSError as e: # Guard against race condition\n if e.errno != errno.EEXIST:\n raise\n with open(destination, 'w') as fil:\n fil.write(data)", "response": "read a file from the device into a local file"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend a file to the device using the transfer protocol", "response": "def write_file(self, path, destination='', verify='none'):\n \"\"\"sends a file to the device using the transfer protocol\"\"\"\n filename = os.path.basename(path)\n if not destination:\n destination = filename\n\n log.info('Transferring %s as %s', path, destination)\n self.__writeln(\"recv()\")\n\n res = self.__expect('C> ')\n if not res.endswith('C> '):\n log.error('Error waiting for esp \"%s\"', res)\n raise CommunicationTimeout('Error waiting for device to start receiving', res)\n\n log.debug('sending destination filename \"%s\"', destination)\n self.__write(destination + '\\x00', True)\n if not self.__got_ack():\n log.error('did not ack destination filename')\n raise NoAckException('Device did not ACK destination filename')\n\n content = from_file(path)\n\n log.debug('sending %d bytes in %s', len(content), filename)\n pos = 0\n chunk_size = 128\n while pos < len(content):\n rest = len(content) - pos\n if rest > chunk_size:\n rest = chunk_size\n\n data = content[pos:pos+rest]\n if not self.__write_chunk(data):\n resp = self.__expect()\n log.error('Bad chunk response \"%s\" %s', resp, hexify(resp))\n raise BadResponseException('Bad chunk response', ACK, resp)\n\n pos += chunk_size\n\n log.debug('sending zero block')\n #zero size block\n self.__write_chunk('')\n if verify != 'none':\n self.verify_file(path, destination, verify)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef verify_file(self, path, destination, verify='none'):\n content = from_file(path)\n log.info('Verifying using %s...' % verify)\n if verify == 'raw':\n\n data = self.download_file(destination)\n if content != data:\n log.error('Raw verification failed.')\n raise VerificationError('Verification failed.')\n else:\n log.info('Verification successful. Contents are identical.')\n elif verify == 'sha1':\n #Calculate SHA1 on remote file. Extract just hash from result\n data = self.__exchange('shafile(\"'+destination+'\")').splitlines()[1]\n log.info('Remote SHA1: %s', data)\n\n #Calculate hash of local data\n filehashhex = hashlib.sha1(content.encode(ENCODING)).hexdigest()\n log.info('Local SHA1: %s', filehashhex)\n if data != filehashhex:\n log.error('SHA1 verification failed.')\n raise VerificationError('SHA1 Verification failed.')\n else:\n log.info('Verification successful. Checksums match')\n\n elif verify != 'none':\n raise Exception(verify + ' is not a valid verification method.')", "response": "Tries to verify if path has same checksum as destination."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exec_file(self, path):\n filename = os.path.basename(path)\n log.info('Execute %s', filename)\n\n content = from_file(path).replace('\\r', '').split('\\n')\n\n res = '> '\n for line in content:\n line = line.rstrip('\\n')\n retlines = (res + self.__exchange(line)).splitlines()\n # Log all but the last line\n res = retlines.pop()\n for lin in retlines:\n log.info(lin)\n # last line\n log.info(res)", "response": "execute the lines in the local file path"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn true if ACK is received", "response": "def __got_ack(self):\n \"\"\"Returns true if ACK is received\"\"\"\n log.debug('waiting for ack')\n res = self._port.read(1)\n log.debug('ack read %s', hexify(res))\n return res == ACK"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwriting lines one by one separated by \\ n to device", "response": "def write_lines(self, data):\n \"\"\"write lines, one by one, separated by \\n to device\"\"\"\n lines = data.replace('\\r', '').split('\\n')\n for line in lines:\n self.__exchange(line)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __write_chunk(self, chunk):\n log.debug('writing %d bytes chunk', len(chunk))\n data = BLOCK_START + chr(len(chunk)) + chunk\n if len(chunk) < 128:\n padding = 128 - len(chunk)\n log.debug('pad with %d characters', padding)\n data = data + (' ' * padding)\n log.debug(\"packet size %d\", len(data))\n self.__write(data)\n self._port.flush()\n return self.__got_ack()", "response": "formats and sends a chunk of data to the device according\n to transfer protocol"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread a chunk of data from the port.", "response": "def __read_chunk(self, buf):\n \"\"\"Read a chunk of data\"\"\"\n log.debug('reading chunk')\n timeout_before = self._port.timeout\n if SYSTEM != 'Windows':\n # Checking for new data every 100us is fast enough\n if self._port.timeout != MINIMAL_TIMEOUT:\n self._port.timeout = MINIMAL_TIMEOUT\n\n end = time.time() + timeout_before\n\n while len(buf) < 130 and time.time() <= end:\n buf = buf + self._port.read()\n\n if buf[0] != BLOCK_START or len(buf) < 130:\n log.debug('buffer binary: %s ', hexify(buf))\n raise Exception('Bad blocksize or start byte')\n\n if SYSTEM != 'Windows':\n self._port.timeout = timeout_before\n\n chunk_size = ord(buf[1])\n data = buf[2:chunk_size+2]\n buf = buf[130:]\n return (data, buf)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists files on the device", "response": "def file_list(self):\n \"\"\"list files on the device\"\"\"\n log.info('Listing files')\n res = self.__exchange(LIST_FILES)\n res = res.split('\\r\\n')\n # skip first and last lines\n res = res[1:-1]\n files = []\n for line in res:\n files.append(line.split('\\t'))\n return files"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef file_do(self, filename):\n log.info('Executing '+filename)\n res = self.__exchange('dofile(\"'+filename+'\")')\n log.info(res)\n return res", "response": "Execute a file on the device using dofile"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprint a file on the device to the console", "response": "def file_print(self, filename):\n \"\"\"Prints a file on the device to console\"\"\"\n log.info('Printing ' + filename)\n res = self.__exchange(PRINT_FILE.format(filename=filename))\n log.info(res)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef node_heap(self):\n log.info('Heap')\n res = self.__exchange('print(node.heap())')\n log.info(res)\n return int(res.split('\\r\\n')[1])", "response": "Show device heap size"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncompile a file specified by path on the device", "response": "def file_compile(self, path):\n \"\"\"Compiles a file specified by path on the device\"\"\"\n log.info('Compile '+path)\n cmd = 'node.compile(\"%s\")' % path\n res = self.__exchange(cmd)\n log.info(res)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef file_remove(self, path):\n log.info('Remove '+path)\n cmd = 'file.remove(\"%s\")' % path\n res = self.__exchange(cmd)\n log.info(res)\n return res", "response": "Removes a file on the device"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of sources and destinations from a source file.", "response": "def destination_from_source(sources, use_glob=True):\n \"\"\"\n Split each of the sources in the array on ':'\n First part will be source, second will be destination.\n Modifies the the original array to contain only sources\n and returns an array of destinations.\n \"\"\"\n destinations = []\n newsources = []\n for i in range(0, len(sources)):\n srcdst = sources[i].split(':')\n if len(srcdst) == 2:\n destinations.append(srcdst[1])\n newsources.append(srcdst[0]) #proper list assignment\n else:\n if use_glob:\n listing = glob.glob(srcdst[0])\n for filename in listing:\n newsources.append(filename)\n #always use forward slash at destination\n destinations.append(filename.replace('\\\\', '/'))\n else:\n newsources.append(srcdst[0])\n destinations.append(srcdst[0])\n\n return [newsources, destinations]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting files on target", "response": "def operation_list(uploader):\n \"\"\"List file on target\"\"\"\n files = uploader.file_list()\n for f in files:\n log.info(\"{file:30s} {size}\".format(file=f[0], size=f[1]))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef main_func():\n parser = argparse.ArgumentParser(\n description='NodeMCU Lua file uploader',\n prog='nodemcu-uploader'\n )\n\n parser.add_argument(\n '--verbose',\n help='verbose output',\n action='store_true',\n default=False)\n\n parser.add_argument(\n '--version',\n help='prints the version and exists',\n action='version',\n version='%(prog)s {version} (serial {serialversion})'.format(version=__version__, serialversion=serialversion)\n )\n\n parser.add_argument(\n '--port', '-p',\n help='Serial port device',\n default=Uploader.PORT)\n\n parser.add_argument(\n '--baud', '-b',\n help='Serial port baudrate',\n type=arg_auto_int,\n default=Uploader.BAUD)\n\n parser.add_argument(\n '--start_baud', '-B',\n help='Initial Serial port baudrate',\n type=arg_auto_int,\n default=Uploader.START_BAUD)\n\n parser.add_argument(\n '--timeout', '-t',\n help='Timeout for operations',\n type=arg_auto_int,\n default=Uploader.TIMEOUT)\n\n parser.add_argument(\n '--autobaud_time', '-a',\n help='Duration of the autobaud timer',\n type=float,\n default=Uploader.AUTOBAUD_TIME,\n )\n\n subparsers = parser.add_subparsers(\n dest='operation',\n help='Run nodemcu-uploader {command} -h for additional help')\n\n backup_parser = subparsers.add_parser(\n 'backup',\n help='Backup all the files on the nodemcu board')\n backup_parser.add_argument('path', help='Folder where to store the backup')\n\n\n upload_parser = subparsers.add_parser(\n 'upload',\n help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')\n\n upload_parser.add_argument(\n 'filename',\n nargs='+',\n help='Lua file to upload. Use colon to give alternate destination.'\n )\n\n upload_parser.add_argument(\n '--compile', '-c',\n help='If file should be uploaded as compiled',\n action='store_true',\n default=False\n )\n\n upload_parser.add_argument(\n '--verify', '-v',\n help='To verify the uploaded data.',\n action='store',\n nargs='?',\n choices=['none', 'raw', 'sha1'],\n default='none'\n )\n\n upload_parser.add_argument(\n '--dofile', '-e',\n help='If file should be run after upload.',\n action='store_true',\n default=False\n )\n\n upload_parser.add_argument(\n '--restart', '-r',\n help='If esp should be restarted',\n action='store_true',\n default=False\n )\n\n exec_parser = subparsers.add_parser(\n 'exec',\n help='Path to one or more files to be executed line by line.')\n\n exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')\n\n download_parser = subparsers.add_parser(\n 'download',\n help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')\n\n download_parser.add_argument('filename',\n nargs='+',\n help='Lua file to download. Use colon to give alternate destination.')\n\n\n file_parser = subparsers.add_parser(\n 'file',\n help='File functions')\n\n file_parser.add_argument(\n 'cmd',\n choices=('list', 'do', 'format', 'remove', 'print'),\n help=\"list=list files, do=dofile given path, format=formate file area, remove=remove given path\")\n\n file_parser.add_argument('filename', nargs='*', help='path for cmd')\n\n node_parse = subparsers.add_parser(\n 'node',\n help='Node functions')\n\n node_parse.add_argument('ncmd', choices=('heap', 'restart'), help=\"heap=print heap memory, restart=restart nodemcu\")\n\n subparsers.add_parser(\n 'terminal',\n help='Run pySerials miniterm'\n )\n\n args = parser.parse_args()\n\n default_level = logging.INFO\n if args.verbose:\n default_level = logging.DEBUG\n\n #formatter = logging.Formatter('%(message)s')\n\n logging.basicConfig(level=default_level, format='%(message)s')\n\n if args.operation == 'terminal':\n #uploader can not claim the port\n terminal(args.port, str(args.start_baud))\n return\n\n # let uploader user the default (short) timeout for establishing connection\n uploader = Uploader(args.port, args.baud, start_baud=args.start_baud, autobaud_time=args.autobaud_time)\n\n # and reset the timeout (if we have the uploader&timeout)\n if args.timeout:\n uploader.set_timeout(args.timeout)\n\n if args.operation == 'upload':\n operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,\n args.restart)\n\n elif args.operation == 'download':\n operation_download(uploader, args.filename)\n\n elif args.operation == 'exec':\n sources = args.filename\n for path in sources:\n uploader.exec_file(path)\n\n elif args.operation == 'file':\n operation_file(uploader, args.cmd, args.filename)\n\n elif args.operation == 'node':\n if args.ncmd == 'heap':\n uploader.node_heap()\n elif args.ncmd == 'restart':\n uploader.node_restart()\n\n elif args.operation == 'backup':\n uploader.backup(args.path)\n\n #no uploader related commands after this point\n uploader.close()", "response": "Main function for CLI"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndisplay a GenePattern Python Library content in a notebook without the need to import IPython at the top level.", "response": "def display(content):\n \"\"\"\n Display a widget, text or other media in a notebook without the need to import IPython at the top level.\n\n Also handles wrapping GenePattern Python Library content in widgets.\n :param content:\n :return:\n \"\"\"\n if isinstance(content, gp.GPServer):\n IPython.display.display(GPAuthWidget(content))\n elif isinstance(content, gp.GPTask):\n IPython.display.display(GPTaskWidget(content))\n elif isinstance(content, gp.GPJob):\n IPython.display.display(GPJobWidget(content))\n else:\n IPython.display.display(content)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef register(self, server, username, password):\n\n # Create the session\n session = gp.GPServer(server, username, password)\n\n # Validate username if not empty\n valid_username = username != \"\" and username is not None\n\n # Validate that the server is not already registered\n index = self._get_index(server)\n new_server = index == -1\n\n # Add the new session to the list\n if valid_username and new_server:\n self.sessions.append(session)\n\n # Replace old session is one exists\n if valid_username and not new_server:\n self.sessions[index] = session\n\n return session", "response": "Register a new GenePattern server session for the provided username and password. Return the newly created session."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get(self, server):\n\n # Handle indexes\n if isinstance(server, int):\n if server >= len(self.sessions):\n return None\n else:\n return self.sessions[server]\n\n # Handle server URLs\n index = self._get_index(server)\n if index == -1:\n return None\n else:\n return self.sessions[index]", "response": "Returns a registered GPServer object with a matching GenePattern server url or index\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_index(self, server_url):\n for i in range(len(self.sessions)):\n session = self.sessions[i]\n if session.url == server_url:\n return i\n return -1", "response": "Returns a registered GPServer object with a matching GenePattern server url"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\naccepting None or \u221e or datetime or numeric for target", "response": "def _accept(self, target):\n\t\t\"Accept None or \u221e or datetime or numeric for target\"\n\t\tif isinstance(target, datetime.timedelta):\n\t\t\ttarget = target.total_seconds()\n\n\t\tif target is None:\n\t\t\t# treat None as infinite target\n\t\t\ttarget = float('Inf')\n\n\t\treturn target"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts a numeric timestamp to a timezone - aware datetime.", "response": "def from_timestamp(ts):\n \"\"\"\n Convert a numeric timestamp to a timezone-aware datetime.\n\n A client may override this function to change the default behavior,\n such as to use local time or timezone-na\u00efve times.\n \"\"\"\n return datetime.datetime.utcfromtimestamp(ts).replace(tzinfo=pytz.utc)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconstruct a DelayedCommand to come due at time.", "response": "def at_time(cls, at, target):\n \"\"\"\n Construct a DelayedCommand to come due at `at`, where `at` may be\n a datetime or timestamp.\n \"\"\"\n at = cls._from_timestamp(at)\n cmd = cls.from_datetime(at)\n cmd.delay = at - now()\n cmd.target = target\n return cmd"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrely on pytz. localize to ensure new result honors DST.", "response": "def _localize(dt):\n \"\"\"\n Rely on pytz.localize to ensure new result honors DST.\n \"\"\"\n try:\n tz = dt.tzinfo\n return tz.localize(dt.replace(tzinfo=None))\n except AttributeError:\n return dt"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nschedule a command to run at a specific time each day.", "response": "def daily_at(cls, at, target):\n \"\"\"\n Schedule a command to run at a specific time each day.\n \"\"\"\n daily = datetime.timedelta(days=1)\n # convert when to the next datetime matching this time\n when = datetime.datetime.combine(datetime.date.today(), at)\n if when < now():\n when += daily\n return cls.at_time(cls._localize(when), daily, target)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef strptime(s, fmt, tzinfo=None):\n\tres = time.strptime(s, fmt)\n\treturn datetime.datetime(tzinfo=tzinfo, *res[:6])", "response": "A function to replace strptime in the time module."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef datetime_mod(dt, period, start=None):\n\tif start is None:\n\t\t# use midnight of the same day\n\t\tstart = datetime.datetime.combine(dt.date(), datetime.time())\n\t# calculate the difference between the specified time and the start date.\n\tdelta = dt - start\n\n\t# now aggregate the delta and the period into microseconds\n\t# Use microseconds because that's the highest precision of these time\n\t# pieces. Also, using microseconds ensures perfect precision (no floating\n\t# point errors).\n\tdef get_time_delta_microseconds(td):\n\t\treturn (td.days * seconds_per_day + td.seconds) * 1000000 + td.microseconds\n\tdelta, period = map(get_time_delta_microseconds, (delta, period))\n\toffset = datetime.timedelta(microseconds=delta % period)\n\t# the result is the original specified time minus the offset\n\tresult = dt - offset\n\treturn result", "response": "This function calculates the time which is the specified date or time truncated to the time delta_per_day + period."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef datetime_round(dt, period, start=None):\n\tresult = datetime_mod(dt, period, start)\n\tif abs(dt - result) >= period // 2:\n\t\tresult += period\n\treturn result", "response": "This function will return the datetime that is within the specified period."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the nearest year to now inferred from a Julian date.", "response": "def get_nearest_year_for_day(day):\n\t\"\"\"\n\tReturns the nearest year to now inferred from a Julian date.\n\t\"\"\"\n\tnow = time.gmtime()\n\tresult = now.tm_year\n\t# if the day is far greater than today, it must be from last year\n\tif day - now.tm_yday > 365 // 2:\n\t\tresult -= 1\n\t# if the day is far less than today, it must be for next year.\n\tif now.tm_yday - day > 365 // 2:\n\t\tresult += 1\n\treturn result"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_period_seconds(period):\n\tif isinstance(period, six.string_types):\n\t\ttry:\n\t\t\tname = 'seconds_per_' + period.lower()\n\t\t\tresult = globals()[name]\n\t\texcept KeyError:\n\t\t\tmsg = \"period not in (second, minute, hour, day, month, year)\"\n\t\t\traise ValueError(msg)\n\telif isinstance(period, numbers.Number):\n\t\tresult = period\n\telif isinstance(period, datetime.timedelta):\n\t\tresult = period.days * get_period_seconds('day') + period.seconds\n\telse:\n\t\traise TypeError('period must be a string or integer')\n\treturn result", "response": "get_period_seconds - Get the number of seconds in the specified period"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the format string that can be used for a given time period.", "response": "def get_date_format_string(period):\n\t\"\"\"\n\tFor a given period (e.g. 'month', 'day', or some numeric interval\n\tsuch as 3600 (in secs)), return the format string that can be\n\tused with strftime to format that time to specify the times\n\tacross that interval, but no more detailed.\n\tFor example,\n\n\t>>> get_date_format_string('month')\n\t'%Y-%m'\n\t>>> get_date_format_string(3600)\n\t'%Y-%m-%d %H'\n\t>>> get_date_format_string('hour')\n\t'%Y-%m-%d %H'\n\t>>> get_date_format_string(None)\n\tTraceback (most recent call last):\n\t\t...\n\tTypeError: period must be a string or integer\n\t>>> get_date_format_string('garbage')\n\tTraceback (most recent call last):\n\t\t...\n\tValueError: period not in (second, minute, hour, day, month, year)\n\t\"\"\"\n\t# handle the special case of 'month' which doesn't have\n\t# a static interval in seconds\n\tif isinstance(period, six.string_types) and period.lower() == 'month':\n\t\treturn '%Y-%m'\n\tfile_period_secs = get_period_seconds(period)\n\tformat_pieces = ('%Y', '-%m-%d', ' %H', '-%M', '-%S')\n\tseconds_per_second = 1\n\tintervals = (\n\t\tseconds_per_year,\n\t\tseconds_per_day,\n\t\tseconds_per_hour,\n\t\tseconds_per_minute,\n\t\tseconds_per_second,\n\t)\n\tmods = list(map(lambda interval: file_period_secs % interval, intervals))\n\tformat_pieces = format_pieces[: mods.index(0) + 1]\n\treturn ''.join(format_pieces)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndividing a timedelta by a float value", "response": "def divide_timedelta_float(td, divisor):\n\t\"\"\"\n\tDivide a timedelta by a float value\n\n\t>>> one_day = datetime.timedelta(days=1)\n\t>>> half_day = datetime.timedelta(days=.5)\n\t>>> divide_timedelta_float(one_day, 2.0) == half_day\n\tTrue\n\t>>> divide_timedelta_float(one_day, 2) == half_day\n\tTrue\n\t\"\"\"\n\t# td is comprised of days, seconds, microseconds\n\tdsm = [getattr(td, attr) for attr in ('days', 'seconds', 'microseconds')]\n\tdsm = map(lambda elem: elem / divisor, dsm)\n\treturn datetime.timedelta(*dsm)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses a string representing a span of time and return a datetime. timedelta object.", "response": "def parse_timedelta(str):\n\t\"\"\"\n\tTake a string representing a span of time and parse it to a time delta.\n\tAccepts any string of comma-separated numbers each with a unit indicator.\n\n\t>>> parse_timedelta('1 day')\n\tdatetime.timedelta(days=1)\n\n\t>>> parse_timedelta('1 day, 30 seconds')\n\tdatetime.timedelta(days=1, seconds=30)\n\n\t>>> parse_timedelta('47.32 days, 20 minutes, 15.4 milliseconds')\n\tdatetime.timedelta(days=47, seconds=28848, microseconds=15400)\n\n\tSupports weeks, months, years\n\n\t>>> parse_timedelta('1 week')\n\tdatetime.timedelta(days=7)\n\n\t>>> parse_timedelta('1 year, 1 month')\n\tdatetime.timedelta(days=395, seconds=58685)\n\n\tNote that months and years strict intervals, not aligned\n\tto a calendar:\n\n\t>>> now = datetime.datetime.now()\n\t>>> later = now + parse_timedelta('1 year')\n\t>>> diff = later.replace(year=now.year) - now\n\t>>> diff.seconds\n\t20940\n\t\"\"\"\n\tdeltas = (_parse_timedelta_part(part.strip()) for part in str.split(','))\n\treturn sum(deltas, datetime.timedelta())"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef divide_timedelta(td1, td2):\n\ttry:\n\t\treturn td1 / td2\n\texcept TypeError:\n\t\t# Python 3.2 gets division\n\t\t# http://bugs.python.org/issue2706\n\t\treturn td1.total_seconds() / td2.total_seconds()", "response": "Divide two timedelta objects into a single value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef date_range(start=None, stop=None, step=None):\n\tif step is None:\n\t\tstep = datetime.timedelta(days=1)\n\tif start is None:\n\t\tstart = datetime.datetime.now()\n\twhile start < stop:\n\t\tyield start\n\t\tstart += step", "response": "A built - in function range that returns a generator that yields dates from start to stop."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef construct_datetime(cls, *args, **kwargs):\n\t\tif len(args) == 1:\n\t\t\targ = args[0]\n\t\t\tmethod = cls.__get_dt_constructor(\n\t\t\t\ttype(arg).__module__,\n\t\t\t\ttype(arg).__name__,\n\t\t\t)\n\t\t\tresult = method(arg)\n\t\t\ttry:\n\t\t\t\tresult = result.replace(tzinfo=kwargs.pop('tzinfo'))\n\t\t\texcept KeyError:\n\t\t\t\tpass\n\t\t\tif kwargs:\n\t\t\t\tfirst_key = kwargs.keys()[0]\n\t\t\t\ttmpl = (\n\t\t\t\t\t\"{first_key} is an invalid keyword \"\n\t\t\t\t\t\"argument for this function.\"\n\t\t\t\t)\n\t\t\t\traise TypeError(tmpl.format(**locals()))\n\t\telse:\n\t\t\tresult = datetime.datetime(*args, **kwargs)\n\t\treturn result", "response": "Construct a datetime. datetime from a number of different time\n\ttypes found in python and pythonwin"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __common_triplet(input_string, consonants, vowels):\n output = consonants\n\n while len(output) < 3:\n try:\n output += vowels.pop(0)\n except IndexError:\n # If there are less wovels than needed to fill the triplet,\n # (e.g. for a surname as \"Fo'\" or \"Hu\" or the corean \"Y\")\n # fill it with 'X';\n output += 'X'\n\n return output[:3]", "response": "This function returns the common triplet of the input_string."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the list of consonants and vowels from the input_string.", "response": "def __consonants_and_vowels(input_string):\n \"\"\"__consonants_and_vowels(input_string) -> (string, list)\n\n Get the consonants as a string and the vowels as a list.\n \"\"\"\n input_string = input_string.upper().replace(' ', '')\n\n consonants = [ char for char in input_string if char in __CONSONANTS ]\n vowels = [ char for char in input_string if char in __VOWELS ]\n\n return \"\".join(consonants), vowels"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the common triplet of the given string.", "response": "def __surname_triplet(input_string):\n \"\"\"__surname_triplet(input_string) -> string\"\"\"\n consonants, vowels = __consonants_and_vowels(input_string)\n\n return __common_triplet(input_string, consonants, vowels)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __name_triplet(input_string):\n if input_string == '':\n # highly unlikely: no first name, like for instance some Indian persons\n # with only one name on the passport\n # pylint: disable=W0511\n return 'XXX' \n\n consonants, vowels = __consonants_and_vowels(input_string)\n \n if len(consonants) > 3:\n return \"%s%s%s\" % (consonants[0], consonants[2], consonants[3])\n\n return __common_triplet(input_string, consonants, vowels)", "response": "This function returns the name of the current passport based on the input string."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef control_code(input_string):\n assert len(input_string) == 15\n\n # building conversion tables for even and odd characters positions\n even_controlcode = {}\n\n for idx, char in enumerate(string.digits):\n even_controlcode[char] = idx\n\n for idx, char in enumerate(string.ascii_uppercase):\n even_controlcode[char] = idx\n\n values = [ 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8,\n 12, 14, 16, 10, 22, 25, 24, 23 ]\n\n odd_controlcode = {}\n\n for idx, char in enumerate(string.digits):\n odd_controlcode[char] = values[idx]\n\n for idx, char in enumerate(string.ascii_uppercase):\n odd_controlcode[char] = values[idx]\n\n # computing the code\n code = 0\n for idx, char in enumerate(input_string):\n if idx % 2 == 0:\n code += odd_controlcode[char]\n else:\n code += even_controlcode[char]\n \n return string.ascii_uppercase[code % 26]", "response": "Computes the control code for the given input_string."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build(surname, name, birthday, sex, municipality):\n\n # RCCMNL\n output = __surname_triplet(surname) + __name_triplet(name)\n\n # RCCMNL83\n output += str(birthday.year)[2:]\n\n # RCCMNL83S\n output += MONTHSCODE[birthday.month - 1]\n\n # RCCMNL83S18\n output += \"%02d\" % (sex.upper() == 'M' and birthday.day or 40 + birthday.day)\n\n # RCCMNL83S18D969 \n output += municipality\n\n # RCCMNL83S18D969H\n output += control_code(output)\n\n assert isvalid(output)\n\n return output", "response": "Builds the fiscal code for the given person data."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_birthday(code):\n assert isvalid(code)\n\n day = int(code[9:11])\n day = day < 32 and day or day - 40\n\n month = MONTHSCODE.index(code[8]) + 1\n year = int(code[6:8])\n\n return \"%02d-%02d-%02d\" % (day, month, year)", "response": "Returns the birthday of the person whose fiscal code is code."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npass in an Overpass query in Overpass QL.", "response": "def get(self, query, responseformat=\"geojson\", verbosity=\"body\", build=True):\n \"\"\"Pass in an Overpass query in Overpass QL.\"\"\"\n # Construct full Overpass query\n if build:\n full_query = self._construct_ql_query(\n query, responseformat=responseformat, verbosity=verbosity\n )\n else:\n full_query = query\n\n if self.debug:\n logging.getLogger().info(query)\n\n # Get the response from Overpass\n r = self._get_from_overpass(full_query)\n content_type = r.headers.get(\"content-type\")\n\n if self.debug:\n print(content_type)\n if content_type == \"text/csv\":\n result = []\n reader = csv.reader(StringIO(r.text), delimiter=\"\\t\")\n for row in reader:\n result.append(row)\n return result\n elif content_type in (\"text/xml\", \"application/xml\", \"application/osm3s+xml\"):\n return r.text\n elif content_type == \"application/json\":\n response = json.loads(r.text)\n\n if not build:\n return response\n\n # Check for valid answer from Overpass.\n # A valid answer contains an 'elements' key at the root level.\n if \"elements\" not in response:\n raise UnknownOverpassError(\"Received an invalid answer from Overpass.\")\n\n # If there is a 'remark' key, it spells trouble.\n overpass_remark = response.get(\"remark\", None)\n if overpass_remark and overpass_remark.startswith(\"runtime error\"):\n raise ServerRuntimeError(overpass_remark)\n\n if responseformat is not \"geojson\":\n return response\n\n # construct geojson\n return self._as_geojson(response[\"elements\"])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse(self, text):\n\n results = list()\n \n for oneline in text.split('\\n'):\n self._tagger.stdin.write(oneline+'\\n')\n while True:\n r = self._tagger.stdout.readline()[:-1]\n if not r:\n break\n results.append(tuple(r.split('\\t')))\n return results", "response": "Parse the text of a set of items."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_port(context, port):\n LOG.info(\"create_port for tenant %s\" % context.tenant_id)\n port_attrs = port[\"port\"]\n\n admin_only = [\"mac_address\", \"device_owner\", \"bridge\", \"admin_state_up\",\n \"use_forbidden_mac_range\", \"network_plugin\",\n \"instance_node_id\"]\n utils.filter_body(context, port_attrs, admin_only=admin_only)\n\n port_attrs = port[\"port\"]\n mac_address = utils.pop_param(port_attrs, \"mac_address\", None)\n use_forbidden_mac_range = utils.pop_param(port_attrs,\n \"use_forbidden_mac_range\", False)\n segment_id = utils.pop_param(port_attrs, \"segment_id\")\n fixed_ips = utils.pop_param(port_attrs, \"fixed_ips\")\n\n if \"device_id\" not in port_attrs:\n port_attrs['device_id'] = \"\"\n device_id = port_attrs['device_id']\n\n # NOTE(morgabra) This should be instance.node from nova, only needed\n # for ironic_driver.\n if \"instance_node_id\" not in port_attrs:\n port_attrs['instance_node_id'] = \"\"\n instance_node_id = port_attrs['instance_node_id']\n\n net_id = port_attrs[\"network_id\"]\n\n port_id = uuidutils.generate_uuid()\n\n net = db_api.network_find(context=context, limit=None, sorts=['id'],\n marker=None, page_reverse=False, fields=None,\n id=net_id, scope=db_api.ONE)\n\n if not net:\n raise n_exc.NetworkNotFound(net_id=net_id)\n _raise_if_unauthorized(context, net)\n\n # NOTE (Perkins): If a device_id is given, try to prevent multiple ports\n # from being created for a device already attached to the network\n if device_id:\n existing_ports = db_api.port_find(context,\n network_id=net_id,\n device_id=device_id,\n scope=db_api.ONE)\n if existing_ports:\n raise n_exc.BadRequest(\n resource=\"port\", msg=\"This device is already connected to the \"\n \"requested network via another port\")\n\n # Try to fail early on quotas and save ourselves some db overhead\n if fixed_ips:\n quota.QUOTAS.limit_check(context, context.tenant_id,\n fixed_ips_per_port=len(fixed_ips))\n\n if not STRATEGY.is_provider_network(net_id):\n # We don't honor segmented networks when they aren't \"shared\"\n segment_id = None\n port_count = db_api.port_count_all(context, network_id=[net_id],\n tenant_id=[context.tenant_id])\n quota.QUOTAS.limit_check(\n context, context.tenant_id,\n ports_per_network=port_count + 1)\n else:\n if not segment_id:\n raise q_exc.AmbiguousNetworkId(net_id=net_id)\n\n network_plugin = utils.pop_param(port_attrs, \"network_plugin\")\n if not network_plugin:\n network_plugin = net[\"network_plugin\"]\n port_attrs[\"network_plugin\"] = network_plugin\n\n ipam_driver = _get_ipam_driver(net, port=port_attrs)\n net_driver = _get_net_driver(net, port=port_attrs)\n # NOTE(morgabra) It's possible that we select a driver different than\n # the one specified by the network. However, we still might need to use\n # this for some operations, so we also fetch it and pass it along to\n # the backend driver we are actually using.\n base_net_driver = _get_net_driver(net)\n\n # TODO(anyone): security groups are not currently supported on port create.\n # Please see JIRA:NCP-801\n security_groups = utils.pop_param(port_attrs, \"security_groups\")\n if security_groups is not None:\n raise q_exc.SecurityGroupsNotImplemented()\n\n group_ids, security_groups = _make_security_group_list(context,\n security_groups)\n quota.QUOTAS.limit_check(context, context.tenant_id,\n security_groups_per_port=len(group_ids))\n addresses = []\n backend_port = None\n\n with utils.CommandManager().execute() as cmd_mgr:\n @cmd_mgr.do\n def _allocate_ips(fixed_ips, net, port_id, segment_id, mac,\n **kwargs):\n if fixed_ips:\n if (STRATEGY.is_provider_network(net_id) and\n not context.is_admin):\n raise n_exc.NotAuthorized()\n\n ips, subnets = split_and_validate_requested_subnets(context,\n net_id,\n segment_id,\n fixed_ips)\n kwargs[\"ip_addresses\"] = ips\n kwargs[\"subnets\"] = subnets\n\n ipam_driver.allocate_ip_address(\n context, addresses, net[\"id\"], port_id,\n CONF.QUARK.ipam_reuse_after, segment_id=segment_id,\n mac_address=mac, **kwargs)\n\n @cmd_mgr.undo\n def _allocate_ips_undo(addr, **kwargs):\n LOG.info(\"Rolling back IP addresses...\")\n if addresses:\n for address in addresses:\n try:\n with context.session.begin():\n ipam_driver.deallocate_ip_address(context, address,\n **kwargs)\n except Exception:\n LOG.exception(\"Couldn't release IP %s\" % address)\n\n @cmd_mgr.do\n def _allocate_mac(net, port_id, mac_address,\n use_forbidden_mac_range=False,\n **kwargs):\n mac = ipam_driver.allocate_mac_address(\n context, net[\"id\"], port_id, CONF.QUARK.ipam_reuse_after,\n mac_address=mac_address,\n use_forbidden_mac_range=use_forbidden_mac_range, **kwargs)\n return mac\n\n @cmd_mgr.undo\n def _allocate_mac_undo(mac, **kwargs):\n LOG.info(\"Rolling back MAC address...\")\n if mac:\n try:\n with context.session.begin():\n ipam_driver.deallocate_mac_address(context,\n mac[\"address\"])\n except Exception:\n LOG.exception(\"Couldn't release MAC %s\" % mac)\n\n @cmd_mgr.do\n def _allocate_backend_port(mac, addresses, net, port_id, **kwargs):\n backend_port = net_driver.create_port(\n context, net[\"id\"],\n port_id=port_id,\n security_groups=group_ids,\n device_id=device_id,\n instance_node_id=instance_node_id,\n mac_address=mac,\n addresses=addresses,\n base_net_driver=base_net_driver)\n _filter_backend_port(backend_port)\n return backend_port\n\n @cmd_mgr.undo\n def _allocate_back_port_undo(backend_port,\n **kwargs):\n LOG.info(\"Rolling back backend port...\")\n try:\n backend_port_uuid = None\n if backend_port:\n backend_port_uuid = backend_port.get(\"uuid\")\n net_driver.delete_port(context, backend_port_uuid)\n except Exception:\n LOG.exception(\n \"Couldn't rollback backend port %s\" % backend_port)\n\n @cmd_mgr.do\n def _allocate_db_port(port_attrs, backend_port, addresses, mac,\n **kwargs):\n port_attrs[\"network_id\"] = net[\"id\"]\n port_attrs[\"id\"] = port_id\n port_attrs[\"security_groups\"] = security_groups\n\n LOG.info(\"Including extra plugin attrs: %s\" % backend_port)\n port_attrs.update(backend_port)\n with context.session.begin():\n new_port = db_api.port_create(\n context, addresses=addresses, mac_address=mac[\"address\"],\n backend_key=backend_port[\"uuid\"], **port_attrs)\n\n return new_port\n\n @cmd_mgr.undo\n def _allocate_db_port_undo(new_port,\n **kwargs):\n LOG.info(\"Rolling back database port...\")\n if not new_port:\n return\n try:\n with context.session.begin():\n db_api.port_delete(context, new_port)\n except Exception:\n LOG.exception(\n \"Couldn't rollback db port %s\" % backend_port)\n\n # addresses, mac, backend_port, new_port\n mac = _allocate_mac(net, port_id, mac_address,\n use_forbidden_mac_range=use_forbidden_mac_range)\n _allocate_ips(fixed_ips, net, port_id, segment_id, mac)\n backend_port = _allocate_backend_port(mac, addresses, net, port_id)\n new_port = _allocate_db_port(port_attrs, backend_port, addresses, mac)\n\n return v._make_port_dict(new_port)", "response": "Create a port which is a connection point of a device."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates the values of a port.", "response": "def update_port(context, id, port):\n \"\"\"Update values of a port.\n\n : param context: neutron api request context\n : param id: UUID representing the port to update.\n : param port: dictionary with keys indicating fields to update.\n valid keys are those that have a value of True for 'allow_put'\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py.\n \"\"\"\n LOG.info(\"update_port %s for tenant %s\" % (id, context.tenant_id))\n port_db = db_api.port_find(context, id=id, scope=db_api.ONE)\n if not port_db:\n raise n_exc.PortNotFound(port_id=id)\n\n port_dict = port[\"port\"]\n fixed_ips = port_dict.pop(\"fixed_ips\", None)\n\n admin_only = [\"mac_address\", \"device_owner\", \"bridge\", \"admin_state_up\",\n \"device_id\"]\n always_filter = [\"network_id\", \"backend_key\", \"network_plugin\"]\n utils.filter_body(context, port_dict, admin_only=admin_only,\n always_filter=always_filter)\n\n # Pre-check the requested fixed_ips before making too many db trips.\n # Note that this is the only check we need, since this call replaces\n # the entirety of the IP addresses document if fixed_ips are provided.\n if fixed_ips:\n quota.QUOTAS.limit_check(context, context.tenant_id,\n fixed_ips_per_port=len(fixed_ips))\n\n new_security_groups = utils.pop_param(port_dict, \"security_groups\")\n if new_security_groups is not None:\n if (Capabilities.TENANT_NETWORK_SG not in\n CONF.QUARK.environment_capabilities):\n if not STRATEGY.is_provider_network(port_db[\"network_id\"]):\n raise q_exc.TenantNetworkSecurityGroupRulesNotEnabled()\n\n if new_security_groups is not None and not port_db[\"device_id\"]:\n raise q_exc.SecurityGroupsRequireDevice()\n\n group_ids, security_group_mods = _make_security_group_list(\n context, new_security_groups)\n quota.QUOTAS.limit_check(context, context.tenant_id,\n security_groups_per_port=len(group_ids))\n\n if fixed_ips is not None:\n # NOTE(mdietz): we want full control over IPAM since\n # we're allocating by subnet instead of\n # network.\n ipam_driver = ipam.IPAM_REGISTRY.get_strategy(\n ipam.QuarkIpamANY.get_name())\n\n addresses, subnet_ids = [], []\n ip_addresses = {}\n\n for fixed_ip in fixed_ips:\n subnet_id = fixed_ip.get(\"subnet_id\")\n ip_address = fixed_ip.get(\"ip_address\")\n if not (subnet_id or ip_address):\n raise n_exc.BadRequest(\n resource=\"fixed_ips\",\n msg=\"subnet_id or ip_address required\")\n\n if ip_address and not subnet_id:\n raise n_exc.BadRequest(\n resource=\"fixed_ips\",\n msg=\"subnet_id required for ip_address allocation\")\n\n if subnet_id and ip_address:\n ip_netaddr = None\n try:\n ip_netaddr = netaddr.IPAddress(ip_address).ipv6()\n except netaddr.AddrFormatError:\n raise n_exc.InvalidInput(\n error_message=\"Invalid format provided for ip_address\")\n ip_addresses[ip_netaddr] = subnet_id\n else:\n subnet_ids.append(subnet_id)\n\n port_ips = set([netaddr.IPAddress(int(a[\"address\"]))\n for a in port_db[\"ip_addresses\"]])\n new_ips = set([a for a in ip_addresses.keys()])\n\n ips_to_allocate = list(new_ips - port_ips)\n ips_to_deallocate = list(port_ips - new_ips)\n\n for ip in ips_to_allocate:\n if ip in ip_addresses:\n # NOTE: Fix for RM10187 - we were losing the list of IPs if\n # more than one IP was to be allocated. Track an\n # aggregate list instead, and add it to the running total\n # after each allocate\n allocated = []\n ipam_driver.allocate_ip_address(\n context, allocated, port_db[\"network_id\"],\n port_db[\"id\"], reuse_after=None, ip_addresses=[ip],\n subnets=[ip_addresses[ip]])\n addresses.extend(allocated)\n\n for ip in ips_to_deallocate:\n ipam_driver.deallocate_ips_by_port(\n context, port_db, ip_address=ip)\n\n for subnet_id in subnet_ids:\n ipam_driver.allocate_ip_address(\n context, addresses, port_db[\"network_id\"], port_db[\"id\"],\n reuse_after=CONF.QUARK.ipam_reuse_after,\n subnets=[subnet_id])\n\n # Need to return all existing addresses and the new ones\n if addresses:\n port_dict[\"addresses\"] = port_db[\"ip_addresses\"]\n port_dict[\"addresses\"].extend(addresses)\n\n # NOTE(morgabra) Updating network_plugin on port objects is explicitly\n # disallowed in the api, so we use whatever exists in the db.\n net_driver = _get_net_driver(port_db.network, port=port_db)\n base_net_driver = _get_net_driver(port_db.network)\n\n # TODO(anyone): What do we want to have happen here if this fails? Is it\n # ok to continue to keep the IPs but fail to apply security\n # groups? Is there a clean way to have a multi-status? Since\n # we're in a beta-y status, I'm going to let this sit for\n # a future patch where we have time to solve it well.\n kwargs = {}\n if new_security_groups is not None:\n # TODO(anyone): this is kind of silly (when testing), because it will\n # modify the incoming dict. Probably should be a copy or\n # something.\n kwargs[\"security_groups\"] = security_group_mods\n net_driver.update_port(context, port_id=port_db[\"backend_key\"],\n mac_address=port_db[\"mac_address\"],\n device_id=port_db[\"device_id\"],\n base_net_driver=base_net_driver,\n **kwargs)\n\n port_dict[\"security_groups\"] = security_group_mods\n\n with context.session.begin():\n port = db_api.port_update(context, port_db, **port_dict)\n\n # NOTE(mdietz): fix for issue 112, we wanted the IPs to be in\n # allocated_at order, so get a fresh object every time\n if port_db in context.session:\n context.session.expunge(port_db)\n port_db = db_api.port_find(context, id=id, scope=db_api.ONE)\n\n return v._make_port_dict(port_db)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nretrieves a port. : param context: neutron api request context : param id: UUID representing the port to fetch. : param fields: a list of strings that are valid keys in a port dictionary as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. Only these fields will be returned.", "response": "def get_port(context, id, fields=None):\n \"\"\"Retrieve a port.\n\n : param context: neutron api request context\n : param id: UUID representing the port to fetch.\n : param fields: a list of strings that are valid keys in a\n port dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_port %s for tenant %s fields %s\" %\n (id, context.tenant_id, fields))\n results = db_api.port_find(context, id=id, fields=fields,\n scope=db_api.ONE)\n\n if not results:\n raise n_exc.PortNotFound(port_id=id)\n\n return v._make_port_dict(results)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a list of ports for the user in the tenant.", "response": "def get_ports(context, limit=None, sorts=['id'], marker=None,\n page_reverse=False, filters=None, fields=None):\n \"\"\"Retrieve a list of ports.\n\n The contents of the list depends on the identity of the user\n making the request (as indicated by the context) as well as any\n filters.\n : param context: neutron api request context\n : param filters: a dictionary with keys that are valid keys for\n a port as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictionary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n : param fields: a list of strings that are valid keys in a\n port dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_ports for tenant %s filters %s fields %s\" %\n (context.tenant_id, filters, fields))\n if filters is None:\n filters = {}\n\n if \"ip_address\" in filters:\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n ips = []\n try:\n ips = [netaddr.IPAddress(ip) for ip in filters.pop(\"ip_address\")]\n except netaddr.AddrFormatError:\n raise n_exc.InvalidInput(\n error_message=\"Invalid format provided for ip_address\")\n query = db_api.port_find_by_ip_address(context, ip_address=ips,\n scope=db_api.ALL, **filters)\n ports = []\n for ip in query:\n ports.extend(ip.ports)\n else:\n ports = db_api.port_find(context, limit, sorts, marker,\n fields=fields, join_security_groups=True,\n **filters)\n return v._make_ports_list(ports, fields)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_ports_count(context, filters=None):\n LOG.info(\"get_ports_count for tenant %s filters %s\" %\n (context.tenant_id, filters))\n return db_api.port_count_all(context, join_security_groups=True, **filters)", "response": "Return the number of ports in the current context."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete_port(context, id):\n LOG.info(\"delete_port %s for tenant %s\" % (id, context.tenant_id))\n\n port = db_api.port_find(context, id=id, scope=db_api.ONE)\n if not port:\n raise n_exc.PortNotFound(port_id=id)\n\n if 'device_id' in port: # false is weird, but ignore that\n LOG.info(\"delete_port %s for tenant %s has device %s\" %\n (id, context.tenant_id, port['device_id']))\n\n backend_key = port[\"backend_key\"]\n mac_address = netaddr.EUI(port[\"mac_address\"]).value\n ipam_driver = _get_ipam_driver(port[\"network\"], port=port)\n ipam_driver.deallocate_mac_address(context, mac_address)\n ipam_driver.deallocate_ips_by_port(\n context, port, ipam_reuse_after=CONF.QUARK.ipam_reuse_after)\n\n net_driver = _get_net_driver(port[\"network\"], port=port)\n base_net_driver = _get_net_driver(port[\"network\"])\n net_driver.delete_port(context, backend_key, device_id=port[\"device_id\"],\n mac_address=port[\"mac_address\"],\n base_net_driver=base_net_driver)\n\n with context.session.begin():\n db_api.port_delete(context, port)", "response": "Delete a port.\n\n : param context: neutron api request context\n : param id: UUID representing the port to delete."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nassociate the flip with ports and creates the flip driver with the flip driver.", "response": "def _create_flip(context, flip, port_fixed_ips):\n \"\"\"Associates the flip with ports and creates it with the flip driver\n\n :param context: neutron api request context.\n :param flip: quark.db.models.IPAddress object representing a floating IP\n :param port_fixed_ips: dictionary of the structure:\n {\"\": {\"port\": ,\n \"fixed_ip\": \"\"}}\n :return: None\n \"\"\"\n if port_fixed_ips:\n context.session.begin()\n try:\n ports = [val['port'] for val in port_fixed_ips.values()]\n flip = db_api.port_associate_ip(context, ports, flip,\n port_fixed_ips.keys())\n\n for port_id in port_fixed_ips:\n fixed_ip = port_fixed_ips[port_id]['fixed_ip']\n flip = db_api.floating_ip_associate_fixed_ip(context, flip,\n fixed_ip)\n\n flip_driver = registry.DRIVER_REGISTRY.get_driver()\n\n flip_driver.register_floating_ip(flip, port_fixed_ips)\n context.session.commit()\n except Exception:\n context.session.rollback()\n raise\n\n # alexm: Notify from this method for consistency with _delete_flip\n billing.notify(context, billing.IP_ASSOC, flip)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _update_flip(context, flip_id, ip_type, requested_ports):\n # This list will hold flips that require notifications.\n # Using sets to avoid dups, if any.\n notifications = {\n billing.IP_ASSOC: set(),\n billing.IP_DISASSOC: set()\n }\n\n context.session.begin()\n try:\n flip = db_api.floating_ip_find(context, id=flip_id, scope=db_api.ONE)\n if not flip:\n if ip_type == ip_types.SCALING:\n raise q_exc.ScalingIpNotFound(id=flip_id)\n raise q_exc.FloatingIpNotFound(id=flip_id)\n current_ports = flip.ports\n\n # Determine what ports are being removed, being added, and remain\n req_port_ids = [request_port.get('port_id')\n for request_port in requested_ports]\n curr_port_ids = [curr_port.id for curr_port in current_ports]\n added_port_ids = [port_id for port_id in req_port_ids\n if port_id and port_id not in curr_port_ids]\n removed_port_ids = [port_id for port_id in curr_port_ids\n if port_id not in req_port_ids]\n remaining_port_ids = set(curr_port_ids) - set(removed_port_ids)\n\n # Validations just for floating ip types\n if (ip_type == ip_types.FLOATING and curr_port_ids and\n curr_port_ids == req_port_ids):\n d = dict(flip_id=flip_id, port_id=curr_port_ids[0])\n raise q_exc.PortAlreadyAssociatedToFloatingIp(**d)\n if (ip_type == ip_types.FLOATING and\n not curr_port_ids and not req_port_ids):\n raise q_exc.FloatingIpUpdateNoPortIdSupplied()\n\n # Validate that GW IP is not in use on the NW.\n flip_subnet = v._make_subnet_dict(flip.subnet)\n for added_port_id in added_port_ids:\n port = _get_port(context, added_port_id)\n nw = port.network\n nw_ports = v._make_ports_list(nw.ports)\n fixed_ips = [ip.get('ip_address') for p in nw_ports\n for ip in p.get('fixed_ips')]\n\n gw_ip = flip_subnet.get('gateway_ip')\n if gw_ip in fixed_ips:\n port_with_gateway_ip = None\n for port in nw_ports:\n for ip in port.get('fixed_ips'):\n if gw_ip in ip.get('ip_address'):\n port_with_gateway_ip = port\n break\n port_id = port_with_gateway_ip.get('id')\n network_id = port_with_gateway_ip.get('network_id')\n raise q_exc.FixedIpAllocatedToGatewayIp(port_id=port_id,\n network_id=network_id)\n port_fixed_ips = {}\n\n # Keep the ports and fixed ips that have not changed\n for port_id in remaining_port_ids:\n port = db_api.port_find(context, id=port_id, scope=db_api.ONE)\n fixed_ip = _get_flip_fixed_ip_by_port_id(flip, port_id)\n port_fixed_ips[port_id] = {'port': port, 'fixed_ip': fixed_ip}\n\n # Disassociate the ports and fixed ips from the flip that were\n # associated to the flip but are not anymore\n for port_id in removed_port_ids:\n port = db_api.port_find(context, id=port_id, scope=db_api.ONE)\n flip = db_api.port_disassociate_ip(context, [port], flip)\n notifications[billing.IP_DISASSOC].add(flip)\n fixed_ip = _get_flip_fixed_ip_by_port_id(flip, port_id)\n if fixed_ip:\n flip = db_api.floating_ip_disassociate_fixed_ip(\n context, flip, fixed_ip)\n\n # Validate the new ports with the flip and associate the new ports\n # and fixed ips with the flip\n for port_id in added_port_ids:\n port = db_api.port_find(context, id=port_id, scope=db_api.ONE)\n if not port:\n raise n_exc.PortNotFound(port_id=port_id)\n if any(ip for ip in port.ip_addresses\n if (ip.get('address_type') == ip_types.FLOATING)):\n raise q_exc.PortAlreadyContainsFloatingIp(port_id=port_id)\n if any(ip for ip in port.ip_addresses\n if (ip.get('address_type') == ip_types.SCALING)):\n raise q_exc.PortAlreadyContainsScalingIp(port_id=port_id)\n fixed_ip = _get_next_available_fixed_ip(port)\n LOG.info('new fixed ip: %s' % fixed_ip)\n if not fixed_ip:\n raise q_exc.NoAvailableFixedIpsForPort(port_id=port_id)\n port_fixed_ips[port_id] = {'port': port, 'fixed_ip': fixed_ip}\n flip = db_api.port_associate_ip(context, [port], flip, [port_id])\n notifications[billing.IP_ASSOC].add(flip)\n flip = db_api.floating_ip_associate_fixed_ip(context, flip,\n fixed_ip)\n\n flip_driver = registry.DRIVER_REGISTRY.get_driver()\n # If there are not any remaining ports and no new ones are being added,\n # remove the floating ip from unicorn\n if not remaining_port_ids and not added_port_ids:\n flip_driver.remove_floating_ip(flip)\n # If new ports are being added but there previously was not any ports,\n # then register a new floating ip with the driver because it is\n # assumed it does not exist\n elif added_port_ids and not curr_port_ids:\n flip_driver.register_floating_ip(flip, port_fixed_ips)\n else:\n flip_driver.update_floating_ip(flip, port_fixed_ips)\n context.session.commit()\n except Exception:\n context.session.rollback()\n raise\n\n # Send notifications for possible associate/disassociate events\n for notif_type, flip_set in notifications.iteritems():\n for flip in flip_set:\n billing.notify(context, notif_type, flip)\n\n # NOTE(blogan): ORM does not seem to update the model to the real state\n # of the database, so I'm doing an explicit refresh for now.\n context.session.refresh(flip)\n return flip", "response": "Update a floating IP object in the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nallocating or reallocate a floating IP.", "response": "def create_floatingip(context, content):\n \"\"\"Allocate or reallocate a floating IP.\n\n :param context: neutron api request context.\n :param content: dictionary describing the floating ip, with keys\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py. All keys will be populated.\n\n :returns: Dictionary containing details for the new floating IP. If values\n are declared in the fields parameter, then only those keys will be\n present.\n \"\"\"\n LOG.info('create_floatingip %s for tenant %s and body %s' %\n (id, context.tenant_id, content))\n network_id = content.get('floating_network_id')\n # TODO(blogan): Since the extension logic will reject any requests without\n # floating_network_id, is this still needed?\n if not network_id:\n raise n_exc.BadRequest(resource='floating_ip',\n msg='floating_network_id is required.')\n fixed_ip_address = content.get('fixed_ip_address')\n ip_address = content.get('floating_ip_address')\n port_id = content.get('port_id')\n port = None\n port_fixed_ip = {}\n\n network = _get_network(context, network_id)\n if port_id:\n port = _get_port(context, port_id)\n fixed_ip = _get_fixed_ip(context, fixed_ip_address, port)\n port_fixed_ip = {port.id: {'port': port, 'fixed_ip': fixed_ip}}\n flip = _allocate_ip(context, network, port, ip_address, ip_types.FLOATING)\n _create_flip(context, flip, port_fixed_ip)\n return v._make_floating_ip_dict(flip, port_id)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating a floating IP.", "response": "def update_floatingip(context, id, content):\n \"\"\"Update an existing floating IP.\n\n :param context: neutron api request context.\n :param id: id of the floating ip\n :param content: dictionary with keys indicating fields to update.\n valid keys are those that have a value of True for 'allow_put'\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py.\n\n :returns: Dictionary containing details for the new floating IP. If values\n are declared in the fields parameter, then only those keys will be\n present.\n \"\"\"\n\n LOG.info('update_floatingip %s for tenant %s and body %s' %\n (id, context.tenant_id, content))\n\n if 'port_id' not in content:\n raise n_exc.BadRequest(resource='floating_ip',\n msg='port_id is required.')\n\n requested_ports = []\n if content.get('port_id'):\n requested_ports = [{'port_id': content.get('port_id')}]\n flip = _update_flip(context, id, ip_types.FLOATING, requested_ports)\n return v._make_floating_ip_dict(flip)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_floatingip(context, id):\n\n LOG.info('delete_floatingip %s for tenant %s' % (id, context.tenant_id))\n\n _delete_flip(context, id, ip_types.FLOATING)", "response": "deallocate a floating IP."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_floatingip(context, id, fields=None):\n LOG.info('get_floatingip %s for tenant %s' % (id, context.tenant_id))\n\n filters = {'address_type': ip_types.FLOATING, '_deallocated': False}\n\n floating_ip = db_api.floating_ip_find(context, id=id, scope=db_api.ONE,\n **filters)\n\n if not floating_ip:\n raise q_exc.FloatingIpNotFound(id=id)\n\n return v._make_floating_ip_dict(floating_ip)", "response": "Get a floating IP."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_floatingips(context, filters=None, fields=None, sorts=['id'],\n limit=None, marker=None, page_reverse=False):\n \"\"\"Retrieve a list of floating ips.\n\n :param context: neutron api request context.\n :param filters: a dictionary with keys that are valid keys for\n a floating ip as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictionary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n :param fields: a list of strings that are valid keys in a\n floating IP dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n\n :returns: List of floating IPs that are accessible to the tenant who\n submits the request (as indicated by the tenant id of the context)\n as well as any filters.\n \"\"\"\n LOG.info('get_floatingips for tenant %s filters %s fields %s' %\n (context.tenant_id, filters, fields))\n\n floating_ips = _get_ips_by_type(context, ip_types.FLOATING,\n filters=filters, fields=fields)\n\n return [v._make_floating_ip_dict(flip) for flip in floating_ips]", "response": "Retrieve a list of floating IPs for a given tenant."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_floatingips_count(context, filters=None):\n LOG.info('get_floatingips_count for tenant %s filters %s' %\n (context.tenant_id, filters))\n\n if filters is None:\n filters = {}\n\n filters['_deallocated'] = False\n filters['address_type'] = ip_types.FLOATING\n count = db_api.ip_address_count_all(context, filters)\n\n LOG.info('Found %s floating ips for tenant %s' % (count,\n context.tenant_id))\n return count", "response": "Returns the number of floating IPs in the given context."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_scalingip(context, content):\n LOG.info('create_scalingip for tenant %s and body %s',\n context.tenant_id, content)\n network_id = content.get('scaling_network_id')\n ip_address = content.get('scaling_ip_address')\n requested_ports = content.get('ports', [])\n\n network = _get_network(context, network_id)\n port_fixed_ips = {}\n for req_port in requested_ports:\n port = _get_port(context, req_port['port_id'])\n fixed_ip = _get_fixed_ip(context, req_port.get('fixed_ip_address'),\n port)\n port_fixed_ips[port.id] = {\"port\": port, \"fixed_ip\": fixed_ip}\n scip = _allocate_ip(context, network, None, ip_address, ip_types.SCALING)\n _create_flip(context, scip, port_fixed_ips)\n return v._make_scaling_ip_dict(scip)", "response": "Allocate or reallocate a scaling IP."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating an existing scaling IP.", "response": "def update_scalingip(context, id, content):\n \"\"\"Update an existing scaling IP.\n\n :param context: neutron api request context.\n :param id: id of the scaling ip\n :param content: dictionary with keys indicating fields to update.\n valid keys are those that have a value of True for 'allow_put'\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py.\n\n :returns: Dictionary containing details for the new scaling IP. If values\n are declared in the fields parameter, then only those keys will be\n present.\n \"\"\"\n LOG.info('update_scalingip %s for tenant %s and body %s' %\n (id, context.tenant_id, content))\n requested_ports = content.get('ports', [])\n flip = _update_flip(context, id, ip_types.SCALING, requested_ports)\n return v._make_scaling_ip_dict(flip)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_scalingip(context, id):\n LOG.info('delete_scalingip %s for tenant %s' % (id, context.tenant_id))\n _delete_flip(context, id, ip_types.SCALING)", "response": "Deallocate a scaling IP."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting a scaling IP.", "response": "def get_scalingip(context, id, fields=None):\n \"\"\"Retrieve a scaling IP.\n\n :param context: neutron api request context.\n :param id: The UUID of the scaling IP.\n :param fields: a list of strings that are valid keys in a\n scaling IP dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n\n :returns: Dictionary containing details for the scaling IP. If values\n are declared in the fields parameter, then only those keys will be\n present.\n \"\"\"\n LOG.info('get_scalingip %s for tenant %s' % (id, context.tenant_id))\n filters = {'address_type': ip_types.SCALING, '_deallocated': False}\n scaling_ip = db_api.floating_ip_find(context, id=id, scope=db_api.ONE,\n **filters)\n if not scaling_ip:\n raise q_exc.ScalingIpNotFound(id=id)\n return v._make_scaling_ip_dict(scaling_ip)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_scalingips(context, filters=None, fields=None, sorts=['id'],\n limit=None, marker=None, page_reverse=False):\n \"\"\"Retrieve a list of scaling ips.\n\n :param context: neutron api request context.\n :param filters: a dictionary with keys that are valid keys for\n a scaling ip as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictionary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n :param fields: a list of strings that are valid keys in a\n scaling IP dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n\n :returns: List of scaling IPs that are accessible to the tenant who\n submits the request (as indicated by the tenant id of the context)\n as well as any filters.\n \"\"\"\n LOG.info('get_scalingips for tenant %s filters %s fields %s' %\n (context.tenant_id, filters, fields))\n scaling_ips = _get_ips_by_type(context, ip_types.SCALING,\n filters=filters, fields=fields)\n return [v._make_scaling_ip_dict(scip) for scip in scaling_ips]", "response": "Get a list of scaling ips for a given tenant."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_ip_address(context, id, ip_address):\n LOG.info(\"update_ip_address %s for tenant %s\" % (id, context.tenant_id))\n ports = []\n if 'ip_address' not in ip_address:\n raise n_exc.BadRequest(resource=\"ip_addresses\",\n msg=\"Invalid request body.\")\n with context.session.begin():\n db_address = db_api.ip_address_find(context, id=id, scope=db_api.ONE)\n if not db_address:\n raise q_exc.IpAddressNotFound(addr_id=id)\n iptype = db_address.address_type\n if iptype == ip_types.FIXED and not CONF.QUARK.ipaddr_allow_fixed_ip:\n raise n_exc.BadRequest(\n resource=\"ip_addresses\",\n msg=\"Fixed ips cannot be updated using this interface.\")\n\n reset = ip_address['ip_address'].get('reset_allocation_time', False)\n if reset and db_address['deallocated'] == 1:\n if context.is_admin:\n LOG.info(\"IP's deallocated time being manually reset\")\n db_address['deallocated_at'] = _get_deallocated_override()\n else:\n msg = \"Modification of reset_allocation_time requires admin\"\n raise webob.exc.HTTPForbidden(detail=msg)\n\n port_ids = ip_address['ip_address'].get('port_ids', None)\n\n if port_ids is not None and not port_ids:\n raise n_exc.BadRequest(\n resource=\"ip_addresses\",\n msg=\"Cannot be updated with empty port_id list\")\n\n if iptype == ip_types.SHARED:\n has_owner = db_address.has_any_shared_owner()\n\n if port_ids:\n if iptype == ip_types.FIXED and len(port_ids) > 1:\n raise n_exc.BadRequest(\n resource=\"ip_addresses\",\n msg=\"Fixed ips cannot be updated with more than one port.\")\n\n _raise_if_shared_and_enabled(ip_address, db_address)\n ports = db_api.port_find(context, tenant_id=context.tenant_id,\n id=port_ids, scope=db_api.ALL)\n # NOTE(name): could be considered inefficient because we're\n # converting to a list to check length. Maybe revisit\n if len(ports) != len(port_ids):\n raise n_exc.PortNotFound(port_id=port_ids)\n\n validate_and_fetch_segment(ports, db_address[\"network_id\"])\n validate_port_ip_quotas(context, db_address.network_id, ports)\n\n if iptype == ip_types.SHARED and has_owner:\n for assoc in db_address.associations:\n pid = assoc.port_id\n if pid not in port_ids and 'none' != assoc.service:\n raise q_exc.PortRequiresDisassociation()\n\n LOG.info(\"Updating IP address, %s, to only be used by the\"\n \"following ports: %s\" % (db_address.address_readable,\n [p.id for p in ports]))\n new_address = db_api.update_port_associations_for_ip(context,\n ports,\n db_address)\n elif iptype == ip_types.SHARED and has_owner:\n raise q_exc.PortRequiresDisassociation()\n elif 'deallocated' in ip_address['ip_address']\\\n and context.is_admin:\n # Verify no port associations\n if len(db_address.associations) != 0:\n exc_msg = (\"IP %s cannot be deallocated or allocated while\"\n \" still associated with ports: %s\"\n % (db_address['address_readable'],\n db_address.associations))\n raise q_exc.ActionNotAuthorized(msg=exc_msg)\n\n # NOTE: If an admin, allow a user to set deallocated to false\n # in order to reserve a deallocated IP. Alternatively, allow them\n # reverse that choice if a mistake was made.\n if ip_address['ip_address']['deallocated'] == 'False':\n db_address['deallocated'] = False\n else:\n db_address['deallocated'] = True\n return v._make_ip_dict(db_address, context.is_admin)\n else:\n ipam_driver.deallocate_ip_address(context, db_address)\n return v._make_ip_dict(db_address, context.is_admin)\n return v._make_ip_dict(new_address, context.is_admin)", "response": "Update an existing IP address."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes an ip address.", "response": "def delete_ip_address(context, id):\n \"\"\"Delete an ip address.\n\n : param context: neutron api request context\n : param id: UUID representing the ip address to delete.\n \"\"\"\n LOG.info(\"delete_ip_address %s for tenant %s\" % (id, context.tenant_id))\n with context.session.begin():\n ip_address = db_api.ip_address_find(\n context, id=id, scope=db_api.ONE)\n if not ip_address or ip_address.deallocated:\n raise q_exc.IpAddressNotFound(addr_id=id)\n\n iptype = ip_address.address_type\n if iptype == ip_types.FIXED and not CONF.QUARK.ipaddr_allow_fixed_ip:\n raise n_exc.BadRequest(\n resource=\"ip_addresses\",\n msg=\"Fixed ips cannot be updated using this interface.\")\n\n if ip_address.has_any_shared_owner():\n raise q_exc.PortRequiresDisassociation()\n\n db_api.update_port_associations_for_ip(context, [], ip_address)\n\n ipam_driver.deallocate_ip_address(context, ip_address)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_ports_for_ip_address(context, ip_id, limit=None, sorts=['id'],\n marker=None, page_reverse=False, filters=None,\n fields=None):\n \"\"\"Retrieve a list of ports.\n\n The contents of the list depends on the identity of the user\n making the request (as indicated by the context) as well as any\n filters.\n : param context: neutron api request context\n : param filters: a dictionary with keys that are valid keys for\n a port as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictionary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n : param fields: a list of strings that are valid keys in a\n port dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_ports for tenant %s filters %s fields %s\" %\n (context.tenant_id, filters, fields))\n addr = db_api.ip_address_find(context, id=ip_id, scope=db_api.ONE)\n if not addr:\n raise q_exc.IpAddressNotFound(addr_id=ip_id)\n\n if filters is None:\n filters = {}\n\n filters['ip_address_id'] = [ip_id]\n\n ports = db_api.port_find(context, limit, sorts, marker,\n fields=fields, join_security_groups=True,\n **filters)\n return v._make_ip_ports_list(addr, ports, fields)", "response": "Get a list of ports for the given IP address."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting a port for a given IP address.", "response": "def get_port_for_ip_address(context, ip_id, id, fields=None):\n \"\"\"Retrieve a port.\n\n : param context: neutron api request context\n : param id: UUID representing the port to fetch.\n : param fields: a list of strings that are valid keys in a\n port dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_port %s for tenant %s fields %s\" %\n (id, context.tenant_id, fields))\n addr = db_api.ip_address_find(context, id=ip_id, scope=db_api.ONE)\n if not addr:\n raise q_exc.IpAddressNotFound(addr_id=ip_id)\n\n filters = {'ip_address_id': [ip_id]}\n results = db_api.port_find(context, id=id, fields=fields,\n scope=db_api.ONE, **filters)\n\n if not results:\n raise n_exc.PortNotFound(port_id=id)\n\n return v._make_port_for_ip_dict(addr, results)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef update_port_for_ip_address(context, ip_id, id, port):\n LOG.info(\"update_port %s for tenant %s\" % (id, context.tenant_id))\n sanitize_list = ['service']\n with context.session.begin():\n addr = db_api.ip_address_find(context, id=ip_id, scope=db_api.ONE)\n if not addr:\n raise q_exc.IpAddressNotFound(addr_id=ip_id)\n port_db = db_api.port_find(context, id=id, scope=db_api.ONE)\n if not port_db:\n raise q_exc.PortNotFound(port_id=id)\n port_dict = {k: port['port'][k] for k in sanitize_list}\n\n require_da = False\n service = port_dict.get('service')\n\n if require_da and _shared_ip_and_active(addr, except_port=id):\n raise q_exc.PortRequiresDisassociation()\n addr.set_service_for_port(port_db, service)\n context.session.add(addr)\n return v._make_port_for_ip_dict(addr, port_db)", "response": "Update the values of a port for a given ip."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef is_isonet_vif(vif):\n nicira_iface_id = vif.record.get('other_config').get('nicira-iface-id')\n\n if nicira_iface_id:\n return True\n\n return False", "response": "Determine if a vif is on isonet"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef partition_vifs(xapi_client, interfaces, security_group_states):\n added = []\n updated = []\n removed = []\n\n for vif in interfaces:\n # Quark should not action on isonet vifs in regions that use FLIP\n if ('floating_ip' in CONF.QUARK.environment_capabilities and\n is_isonet_vif(vif)):\n continue\n\n vif_has_groups = vif in security_group_states\n if vif.tagged and vif_has_groups and\\\n security_group_states[vif][sg_cli.SECURITY_GROUP_ACK]:\n # Already ack'd these groups and VIF is tagged, reapply.\n # If it's not tagged, fall through and have it self-heal\n continue\n\n if vif.tagged:\n if vif_has_groups:\n updated.append(vif)\n else:\n removed.append(vif)\n else:\n if vif_has_groups:\n added.append(vif)\n # if not tagged and no groups, skip\n\n return added, updated, removed", "response": "Splits the VIFs into three explicit categories and one implicit category and one implicit category and one implicit category."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncomparing the groups that were successfully returned from xapi_client. update_interfaces call and returns the list of groups that have not changed since update.", "response": "def get_groups_to_ack(groups_to_ack, init_sg_states, curr_sg_states):\n \"\"\"Compares initial security group rules with current sg rules.\n\n Given the groups that were successfully returned from\n xapi_client.update_interfaces call, compare initial and current\n security group rules to determine if an update occurred during\n the window that the xapi_client.update_interfaces was executing.\n Return a list of vifs whose security group rules have not changed.\n \"\"\"\n security_groups_changed = []\n # Compare current security group rules with initial rules.\n for vif in groups_to_ack:\n initial_state = init_sg_states[vif][sg_cli.SECURITY_GROUP_HASH_ATTR]\n current_state = curr_sg_states[vif][sg_cli.SECURITY_GROUP_HASH_ATTR]\n bad_match_msg = ('security group rules were changed for vif \"%s\" while'\n ' executing xapi_client.update_interfaces.'\n ' Will not ack rule.' % vif)\n # If lists are different lengths, they're automatically different.\n if len(initial_state) != len(current_state):\n security_groups_changed.append(vif)\n LOG.info(bad_match_msg)\n elif len(initial_state) > 0:\n # Compare rules in equal length lists.\n for rule in current_state:\n if rule not in initial_state:\n security_groups_changed.append(vif)\n LOG.info(bad_match_msg)\n break\n\n # Only ack groups whose rules have not changed since update. If\n # rules do not match, do not add them to ret so the change\n # can be picked up on the next cycle.\n ret = [group for group in groups_to_ack\n if group not in security_groups_changed]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run():\n groups_client = sg_cli.SecurityGroupsClient()\n xapi_client = xapi.XapiClient()\n\n interfaces = set()\n while True:\n try:\n interfaces = xapi_client.get_interfaces()\n except Exception:\n LOG.exception(\"Unable to get instances/interfaces from xapi\")\n _sleep()\n continue\n\n try:\n sg_states = groups_client.get_security_group_states(interfaces)\n new_sg, updated_sg, removed_sg = partition_vifs(xapi_client,\n interfaces,\n sg_states)\n xapi_client.update_interfaces(new_sg, updated_sg, removed_sg)\n groups_to_ack = [v for v in new_sg + updated_sg if v.success]\n # NOTE(quade): This solves a race condition where a security group\n # rule may have changed between the time the sg_states were called\n # and when they were officially ack'd. It functions as a compare\n # and set. This is a fix until we get onto a proper messaging\n # queue. NCP-2287\n sg_sts_curr = groups_client.get_security_group_states(interfaces)\n groups_to_ack = get_groups_to_ack(groups_to_ack, sg_states,\n sg_sts_curr)\n # This list will contain all the security group rules that do not\n # match\n ack_groups(groups_client, groups_to_ack)\n\n except Exception:\n LOG.exception(\"Unable to get security groups from registry and \"\n \"apply them to xapi\")\n _sleep()\n continue\n\n _sleep()", "response": "Processes the current state of the current state of the current security group and updates them to the final modified list and applies flows to each new security group."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_tenant_quota(context, tenant_id):\n\n tenant_quotas = context.session.query(Quota)\n tenant_quotas = tenant_quotas.filter_by(tenant_id=tenant_id)\n tenant_quotas.delete()", "response": "Delete the quota entries for a given tenant_id."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates the CIDR for a subnet.", "response": "def _validate_subnet_cidr(context, network_id, new_subnet_cidr):\n \"\"\"Validate the CIDR for a subnet.\n\n Verifies the specified CIDR does not overlap with the ones defined\n for the other subnets specified for this network, or with any other\n CIDR if overlapping IPs are disabled.\n\n \"\"\"\n if neutron_cfg.cfg.CONF.allow_overlapping_ips:\n return\n\n try:\n new_subnet_ipset = netaddr.IPSet([new_subnet_cidr])\n except TypeError:\n LOG.exception(\"Invalid or missing cidr: %s\" % new_subnet_cidr)\n raise n_exc.BadRequest(resource=\"subnet\",\n msg=\"Invalid or missing cidr\")\n\n filters = {\n 'network_id': network_id,\n 'shared': [False]\n }\n # Using admin context here, in case we actually share networks later\n subnet_list = db_api.subnet_find(context=context.elevated(), **filters)\n\n for subnet in subnet_list:\n if (netaddr.IPSet([subnet.cidr]) & new_subnet_ipset):\n # don't give out details of the overlapping subnet\n err_msg = (_(\"Requested subnet with cidr: %(cidr)s for \"\n \"network: %(network_id)s overlaps with another \"\n \"subnet\") %\n {'cidr': new_subnet_cidr,\n 'network_id': network_id})\n LOG.error(_(\"Validation for CIDR: %(new_cidr)s failed - \"\n \"overlaps with subnet %(subnet_id)s \"\n \"(CIDR: %(cidr)s)\"),\n {'new_cidr': new_subnet_cidr,\n 'subnet_id': subnet.id,\n 'cidr': subnet.cidr})\n raise n_exc.InvalidInput(error_message=err_msg)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a subnet which represents a range of IP addresses.", "response": "def create_subnet(context, subnet):\n \"\"\"Create a subnet.\n\n Create a subnet which represents a range of IP addresses\n that can be allocated to devices\n\n : param context: neutron api request context\n : param subnet: dictionary describing the subnet, with keys\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py. All keys will be populated.\n \"\"\"\n LOG.info(\"create_subnet for tenant %s\" % context.tenant_id)\n net_id = subnet[\"subnet\"][\"network_id\"]\n\n with context.session.begin():\n net = db_api.network_find(context=context, limit=None, sorts=['id'],\n marker=None, page_reverse=False, fields=None,\n id=net_id, scope=db_api.ONE)\n if not net:\n raise n_exc.NetworkNotFound(net_id=net_id)\n\n sub_attrs = subnet[\"subnet\"]\n\n always_pop = [\"enable_dhcp\", \"ip_version\", \"first_ip\", \"last_ip\",\n \"_cidr\"]\n admin_only = [\"segment_id\", \"do_not_use\", \"created_at\",\n \"next_auto_assign_ip\"]\n utils.filter_body(context, sub_attrs, admin_only, always_pop)\n\n _validate_subnet_cidr(context, net_id, sub_attrs[\"cidr\"])\n\n cidr = netaddr.IPNetwork(sub_attrs[\"cidr\"])\n\n err_vals = {'cidr': sub_attrs[\"cidr\"], 'network_id': net_id}\n err = _(\"Requested subnet with cidr: %(cidr)s for \"\n \"network: %(network_id)s. Prefix is too small, must be a \"\n \"larger subnet. A prefix less than /%(prefix)s is required.\")\n\n if cidr.version == 6 and cidr.prefixlen > 64:\n err_vals[\"prefix\"] = 65\n err_msg = err % err_vals\n raise n_exc.InvalidInput(error_message=err_msg)\n elif cidr.version == 4 and cidr.prefixlen > 30:\n err_vals[\"prefix\"] = 31\n err_msg = err % err_vals\n raise n_exc.InvalidInput(error_message=err_msg)\n # Enforce subnet quotas\n net_subnets = get_subnets(context,\n filters=dict(network_id=net_id))\n if not context.is_admin:\n v4_count, v6_count = 0, 0\n for subnet in net_subnets:\n if netaddr.IPNetwork(subnet['cidr']).version == 6:\n v6_count += 1\n else:\n v4_count += 1\n\n if cidr.version == 6:\n tenant_quota_v6 = context.session.query(qdv.Quota).filter_by(\n tenant_id=context.tenant_id,\n resource='v6_subnets_per_network').first()\n if tenant_quota_v6 != -1:\n quota.QUOTAS.limit_check(\n context, context.tenant_id,\n v6_subnets_per_network=v6_count + 1)\n else:\n tenant_quota_v4 = context.session.query(qdv.Quota).filter_by(\n tenant_id=context.tenant_id,\n resource='v4_subnets_per_network').first()\n if tenant_quota_v4 != -1:\n quota.QUOTAS.limit_check(\n context, context.tenant_id,\n v4_subnets_per_network=v4_count + 1)\n\n # See RM981. The default behavior of setting a gateway unless\n # explicitly asked to not is no longer desirable.\n gateway_ip = utils.pop_param(sub_attrs, \"gateway_ip\")\n dns_ips = utils.pop_param(sub_attrs, \"dns_nameservers\", [])\n host_routes = utils.pop_param(sub_attrs, \"host_routes\", [])\n allocation_pools = utils.pop_param(sub_attrs, \"allocation_pools\", None)\n\n sub_attrs[\"network\"] = net\n new_subnet = db_api.subnet_create(context, **sub_attrs)\n\n cidrs = []\n alloc_pools = allocation_pool.AllocationPools(sub_attrs[\"cidr\"],\n allocation_pools)\n if isinstance(allocation_pools, list):\n cidrs = alloc_pools.get_policy_cidrs()\n\n quota.QUOTAS.limit_check(\n context,\n context.tenant_id,\n alloc_pools_per_subnet=len(alloc_pools))\n\n ip_policies.ensure_default_policy(cidrs, [new_subnet])\n new_subnet[\"ip_policy\"] = db_api.ip_policy_create(context,\n exclude=cidrs)\n\n quota.QUOTAS.limit_check(context, context.tenant_id,\n routes_per_subnet=len(host_routes))\n\n default_route = None\n for route in host_routes:\n netaddr_route = netaddr.IPNetwork(route[\"destination\"])\n if netaddr_route.value == routes.DEFAULT_ROUTE.value:\n if default_route:\n raise q_exc.DuplicateRouteConflict(\n subnet_id=new_subnet[\"id\"])\n\n default_route = route\n gateway_ip = default_route[\"nexthop\"]\n alloc_pools.validate_gateway_excluded(gateway_ip)\n\n new_subnet[\"routes\"].append(db_api.route_create(\n context, cidr=route[\"destination\"], gateway=route[\"nexthop\"]))\n\n quota.QUOTAS.limit_check(context, context.tenant_id,\n dns_nameservers_per_subnet=len(dns_ips))\n\n for dns_ip in dns_ips:\n new_subnet[\"dns_nameservers\"].append(db_api.dns_create(\n context, ip=netaddr.IPAddress(dns_ip)))\n\n # if the gateway_ip is IN the cidr for the subnet and NOT excluded by\n # policies, we should raise a 409 conflict\n if gateway_ip and default_route is None:\n alloc_pools.validate_gateway_excluded(gateway_ip)\n new_subnet[\"routes\"].append(db_api.route_create(\n context, cidr=str(routes.DEFAULT_ROUTE), gateway=gateway_ip))\n\n subnet_dict = v._make_subnet_dict(new_subnet)\n subnet_dict[\"gateway_ip\"] = gateway_ip\n\n return subnet_dict"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update_subnet(context, id, subnet):\n LOG.info(\"update_subnet %s for tenant %s\" %\n (id, context.tenant_id))\n\n with context.session.begin():\n subnet_db = db_api.subnet_find(context=context, limit=None,\n page_reverse=False, sorts=['id'],\n marker_obj=None, fields=None,\n id=id, scope=db_api.ONE)\n if not subnet_db:\n raise n_exc.SubnetNotFound(subnet_id=id)\n\n s = subnet[\"subnet\"]\n always_pop = [\"_cidr\", \"cidr\", \"first_ip\", \"last_ip\", \"ip_version\",\n \"segment_id\", \"network_id\"]\n admin_only = [\"do_not_use\", \"created_at\", \"tenant_id\",\n \"next_auto_assign_ip\", \"enable_dhcp\"]\n utils.filter_body(context, s, admin_only, always_pop)\n\n dns_ips = utils.pop_param(s, \"dns_nameservers\", [])\n host_routes = utils.pop_param(s, \"host_routes\", [])\n gateway_ip = utils.pop_param(s, \"gateway_ip\", None)\n allocation_pools = utils.pop_param(s, \"allocation_pools\", None)\n if not CONF.QUARK.allow_allocation_pool_update:\n if allocation_pools:\n raise n_exc.BadRequest(\n resource=\"subnets\",\n msg=\"Allocation pools cannot be updated.\")\n\n if subnet_db[\"ip_policy\"] is not None:\n ip_policy_cidrs = subnet_db[\"ip_policy\"].get_cidrs_ip_set()\n else:\n ip_policy_cidrs = netaddr.IPSet([])\n\n alloc_pools = allocation_pool.AllocationPools(\n subnet_db[\"cidr\"],\n policies=ip_policy_cidrs)\n else:\n alloc_pools = allocation_pool.AllocationPools(subnet_db[\"cidr\"],\n allocation_pools)\n original_pools = subnet_db.allocation_pools\n ori_pools = allocation_pool.AllocationPools(subnet_db[\"cidr\"],\n original_pools)\n # Check if the pools are growing or shrinking\n is_growing = _pool_is_growing(ori_pools, alloc_pools)\n if not CONF.QUARK.allow_allocation_pool_growth and is_growing:\n raise n_exc.BadRequest(\n resource=\"subnets\",\n msg=\"Allocation pools may not be updated to be larger \"\n \"do to configuration settings\")\n\n quota.QUOTAS.limit_check(\n context,\n context.tenant_id,\n alloc_pools_per_subnet=len(alloc_pools))\n if gateway_ip:\n alloc_pools.validate_gateway_excluded(gateway_ip)\n default_route = None\n for route in host_routes:\n netaddr_route = netaddr.IPNetwork(route[\"destination\"])\n if netaddr_route.value == routes.DEFAULT_ROUTE.value:\n default_route = route\n break\n\n if default_route is None:\n route_model = db_api.route_find(\n context, cidr=str(routes.DEFAULT_ROUTE), subnet_id=id,\n scope=db_api.ONE)\n if route_model:\n db_api.route_update(context, route_model,\n gateway=gateway_ip)\n else:\n db_api.route_create(context,\n cidr=str(routes.DEFAULT_ROUTE),\n gateway=gateway_ip, subnet_id=id)\n\n if dns_ips:\n subnet_db[\"dns_nameservers\"] = []\n quota.QUOTAS.limit_check(context, context.tenant_id,\n dns_nameservers_per_subnet=len(dns_ips))\n\n for dns_ip in dns_ips:\n subnet_db[\"dns_nameservers\"].append(db_api.dns_create(\n context,\n ip=netaddr.IPAddress(dns_ip)))\n\n if host_routes:\n subnet_db[\"routes\"] = []\n quota.QUOTAS.limit_check(context, context.tenant_id,\n routes_per_subnet=len(host_routes))\n\n for route in host_routes:\n subnet_db[\"routes\"].append(db_api.route_create(\n context, cidr=route[\"destination\"], gateway=route[\"nexthop\"]))\n if CONF.QUARK.allow_allocation_pool_update:\n if isinstance(allocation_pools, list):\n cidrs = alloc_pools.get_policy_cidrs()\n ip_policies.ensure_default_policy(cidrs, [subnet_db])\n subnet_db[\"ip_policy\"] = db_api.ip_policy_update(\n context, subnet_db[\"ip_policy\"], exclude=cidrs)\n # invalidate the cache\n db_api.subnet_update_set_alloc_pool_cache(context, subnet_db)\n subnet = db_api.subnet_update(context, subnet_db, **s)\n return v._make_subnet_dict(subnet)", "response": "Update the values of a subnet."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_subnet(context, id, fields=None):\n LOG.info(\"get_subnet %s for tenant %s with fields %s\" %\n (id, context.tenant_id, fields))\n subnet = db_api.subnet_find(context=context, limit=None,\n page_reverse=False, sorts=['id'],\n marker_obj=None, fields=None, id=id,\n join_dns=True, join_routes=True,\n scope=db_api.ONE)\n if not subnet:\n raise n_exc.SubnetNotFound(subnet_id=id)\n\n cache = subnet.get(\"_allocation_pool_cache\")\n if not cache:\n new_cache = subnet.allocation_pools\n db_api.subnet_update_set_alloc_pool_cache(context, subnet, new_cache)\n return v._make_subnet_dict(subnet)", "response": "Retrieve a subnet.\n\n : param context: neutron api request context\n : param id: UUID representing the subnet to fetch.\n : param fields: a list of strings that are valid keys in a\n subnet dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting a list of subnets for the user in the specified context.", "response": "def get_subnets(context, limit=None, page_reverse=False, sorts=['id'],\n marker=None, filters=None, fields=None):\n \"\"\"Retrieve a list of subnets.\n\n The contents of the list depends on the identity of the user\n making the request (as indicated by the context) as well as any\n filters.\n : param context: neutron api request context\n : param filters: a dictionary with keys that are valid keys for\n a subnet as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictiontary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n : param fields: a list of strings that are valid keys in a\n subnet dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_subnets for tenant %s with filters %s fields %s\" %\n (context.tenant_id, filters, fields))\n filters = filters or {}\n subnets = db_api.subnet_find(context, limit=limit,\n page_reverse=page_reverse, sorts=sorts,\n marker_obj=marker, join_dns=True,\n join_routes=True, join_pool=True, **filters)\n for subnet in subnets:\n cache = subnet.get(\"_allocation_pool_cache\")\n if not cache:\n db_api.subnet_update_set_alloc_pool_cache(\n context, subnet, subnet.allocation_pools)\n return v._make_subnets_list(subnets, fields=fields)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the number of subnets in the current tenant.", "response": "def get_subnets_count(context, filters=None):\n \"\"\"Return the number of subnets.\n\n The result depends on the identity of the user making the request\n (as indicated by the context) as well as any filters.\n : param context: neutron api request context\n : param filters: a dictionary with keys that are valid keys for\n a network as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictiontary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n\n NOTE: this method is optional, as it was not part of the originally\n defined plugin API.\n \"\"\"\n LOG.info(\"get_subnets_count for tenant %s with filters %s\" %\n (context.tenant_id, filters))\n return db_api.subnet_count_all(context, **filters)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_subnet(context, id):\n LOG.info(\"delete_subnet %s for tenant %s\" % (id, context.tenant_id))\n with context.session.begin():\n subnet = db_api.subnet_find(context, id=id, scope=db_api.ONE)\n if not subnet:\n raise n_exc.SubnetNotFound(subnet_id=id)\n\n if not context.is_admin:\n if STRATEGY.is_provider_network(subnet.network_id):\n if subnet.tenant_id == context.tenant_id:\n # A tenant can't delete subnets on provider network\n raise n_exc.NotAuthorized(subnet_id=id)\n else:\n # Raise a NotFound here because the foreign tenant\n # does not have to know about other tenant's subnet\n # existence.\n raise n_exc.SubnetNotFound(subnet_id=id)\n\n _delete_subnet(context, subnet)", "response": "Delete a subnet.\n\n : param context: neutron api request context\n : param id: UUID representing the subnet to delete."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _filter_update_security_group_rule(rule):\n '''Only two fields are allowed for modification:\n\n external_service and external_service_id\n '''\n allowed = ['external_service', 'external_service_id']\n filtered = {}\n for k, val in rule.iteritems():\n if k in allowed:\n if isinstance(val, basestring) and \\\n len(val) <= GROUP_NAME_MAX_LENGTH:\n filtered[k] = val\n return filtered", "response": "Only two fields are allowed for modification"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _perform_async_update_rule(context, id, db_sg_group, rule_id, action):\n rpc_reply = None\n sg_rpc = sg_rpc_api.QuarkSGAsyncProcessClient()\n ports = db_api.sg_gather_associated_ports(context, db_sg_group)\n if len(ports) > 0:\n rpc_reply = sg_rpc.start_update(context, id, rule_id, action)\n if rpc_reply:\n job_id = rpc_reply['job_id']\n job_api.add_job_to_context(context, job_id)\n else:\n LOG.error(\"Async update failed. Is the worker running?\")", "response": "Perform an asynchronous update of a security group rule."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_security_group_rule(context, security_group_rule):\n LOG.info(\"create_security_group for tenant %s\" %\n (context.tenant_id))\n with context.session.begin():\n rule = _validate_security_group_rule(\n context, security_group_rule[\"security_group_rule\"])\n rule[\"id\"] = uuidutils.generate_uuid()\n\n group_id = rule[\"security_group_id\"]\n group = db_api.security_group_find(context, id=group_id,\n scope=db_api.ONE)\n if not group:\n raise sg_ext.SecurityGroupNotFound(id=group_id)\n\n quota.QUOTAS.limit_check(\n context, context.tenant_id,\n security_rules_per_group=len(group.get(\"rules\", [])) + 1)\n\n new_rule = db_api.security_group_rule_create(context, **rule)\n if group:\n _perform_async_update_rule(context, group_id, group, new_rule.id,\n RULE_CREATE)\n return v._make_security_group_rule_dict(new_rule)", "response": "Creates a rule and updates the ports if enabled."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_security_group_rule(context, id, security_group_rule):\n '''Updates a rule and updates the ports'''\n LOG.info(\"update_security_group_rule for tenant %s\" %\n (context.tenant_id))\n new_rule = security_group_rule[\"security_group_rule\"]\n # Only allow updatable fields\n new_rule = _filter_update_security_group_rule(new_rule)\n\n with context.session.begin():\n rule = db_api.security_group_rule_find(context, id=id,\n scope=db_api.ONE)\n if not rule:\n raise sg_ext.SecurityGroupRuleNotFound(id=id)\n\n db_rule = db_api.security_group_rule_update(context, rule, **new_rule)\n\n group_id = db_rule.group_id\n group = db_api.security_group_find(context, id=group_id,\n scope=db_api.ONE)\n if not group:\n raise sg_ext.SecurityGroupNotFound(id=group_id)\n\n if group:\n _perform_async_update_rule(context, group_id, group, rule.id,\n RULE_UPDATE)\n\n return v._make_security_group_rule_dict(db_rule)", "response": "Updates a rule and updates the ports"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a rule and updates the ports if enabled.", "response": "def delete_security_group_rule(context, id):\n \"\"\"Deletes a rule and updates the ports (async) if enabled.\"\"\"\n LOG.info(\"delete_security_group %s for tenant %s\" %\n (id, context.tenant_id))\n with context.session.begin():\n rule = db_api.security_group_rule_find(context, id=id,\n scope=db_api.ONE)\n if not rule:\n raise sg_ext.SecurityGroupRuleNotFound(id=id)\n\n group = db_api.security_group_find(context, id=rule[\"group_id\"],\n scope=db_api.ONE)\n if not group:\n raise sg_ext.SecurityGroupNotFound(id=id)\n\n rule[\"id\"] = id\n db_api.security_group_rule_delete(context, rule)\n if group:\n _perform_async_update_rule(context, group.id, group, id, RULE_DELETE)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_data():\n\n output = []\n tables = get_tables()\n for table in tables:\n try:\n columns = get_columns(table)\n except sa.exc.NoSuchTableError:\n continue\n\n for column in columns:\n if column['name'] == 'tenant_id':\n output.append((table, column))\n\n return output", "response": "Returns a list of tuples that is built based on retrieved tables where column with name\n exists."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_public_net_id(self):\n for id, net_params in self.strategy.iteritems():\n if id == CONF.QUARK.public_net_id:\n return id\n return None", "response": "Returns the public net id"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _validate_allocation_pools(self):\n ip_pools = self._alloc_pools\n subnet_cidr = self._subnet_cidr\n\n LOG.debug(_(\"Performing IP validity checks on allocation pools\"))\n ip_sets = []\n for ip_pool in ip_pools:\n try:\n start_ip = netaddr.IPAddress(ip_pool['start'])\n end_ip = netaddr.IPAddress(ip_pool['end'])\n except netaddr.AddrFormatError:\n LOG.info(_(\"Found invalid IP address in pool: \"\n \"%(start)s - %(end)s:\"),\n {'start': ip_pool['start'],\n 'end': ip_pool['end']})\n raise n_exc_ext.InvalidAllocationPool(pool=ip_pool)\n if (start_ip.version != self._subnet_cidr.version or\n end_ip.version != self._subnet_cidr.version):\n LOG.info(_(\"Specified IP addresses do not match \"\n \"the subnet IP version\"))\n raise n_exc_ext.InvalidAllocationPool(pool=ip_pool)\n if end_ip < start_ip:\n LOG.info(_(\"Start IP (%(start)s) is greater than end IP \"\n \"(%(end)s)\"),\n {'start': ip_pool['start'], 'end': ip_pool['end']})\n raise n_exc_ext.InvalidAllocationPool(pool=ip_pool)\n if (start_ip < self._subnet_first_ip or\n end_ip > self._subnet_last_ip):\n LOG.info(_(\"Found pool larger than subnet \"\n \"CIDR:%(start)s - %(end)s\"),\n {'start': ip_pool['start'],\n 'end': ip_pool['end']})\n raise n_exc_ext.OutOfBoundsAllocationPool(\n pool=ip_pool,\n subnet_cidr=subnet_cidr)\n # Valid allocation pool\n # Create an IPSet for it for easily verifying overlaps\n ip_sets.append(netaddr.IPSet(netaddr.IPRange(\n ip_pool['start'],\n ip_pool['end']).cidrs()))\n\n LOG.debug(_(\"Checking for overlaps among allocation pools \"\n \"and gateway ip\"))\n ip_ranges = ip_pools[:]\n\n # Use integer cursors as an efficient way for implementing\n # comparison and avoiding comparing the same pair twice\n for l_cursor in xrange(len(ip_sets)):\n for r_cursor in xrange(l_cursor + 1, len(ip_sets)):\n if ip_sets[l_cursor] & ip_sets[r_cursor]:\n l_range = ip_ranges[l_cursor]\n r_range = ip_ranges[r_cursor]\n LOG.info(_(\"Found overlapping ranges: %(l_range)s and \"\n \"%(r_range)s\"),\n {'l_range': l_range, 'r_range': r_range})\n raise n_exc_ext.OverlappingAllocationPools(\n pool_1=l_range,\n pool_2=r_range,\n subnet_cidr=subnet_cidr)", "response": "Validate the IP allocation pools."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding job to neutron context for use later.", "response": "def add_job_to_context(context, job_id):\n \"\"\"Adds job to neutron context for use later.\"\"\"\n db_job = db_api.async_transaction_find(\n context, id=job_id, scope=db_api.ONE)\n if not db_job:\n return\n context.async_job = {\"job\": v._make_job_dict(db_job)}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_job(context, body):\n LOG.info(\"create_job for tenant %s\" % context.tenant_id)\n\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n job = body.get('job')\n if 'parent_id' in job:\n parent_id = job['parent_id']\n if not parent_id:\n raise q_exc.JobNotFound(job_id=parent_id)\n parent_job = db_api.async_transaction_find(\n context, id=parent_id, scope=db_api.ONE)\n if not parent_job:\n raise q_exc.JobNotFound(job_id=parent_id)\n tid = parent_id\n if parent_job.get('transaction_id'):\n tid = parent_job.get('transaction_id')\n job['transaction_id'] = tid\n\n if not job:\n raise n_exc.BadRequest(resource=\"job\", msg=\"Invalid request body.\")\n with context.session.begin(subtransactions=True):\n new_job = db_api.async_transaction_create(context, **job)\n return v._make_job_dict(new_job)", "response": "Creates a job with support for subjobs."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete an ip address.", "response": "def delete_job(context, id, **filters):\n \"\"\"Delete an ip address.\n\n : param context: neutron api request context\n : param id: UUID representing the ip address to delete.\n \"\"\"\n LOG.info(\"delete_ip_address %s for tenant %s\" % (id, context.tenant_id))\n\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n with context.session.begin():\n job = db_api.async_transaction_find(context, id=id, scope=db_api.ONE,\n **filters)\n if not job:\n raise q_exc.JobNotFound(job_id=id)\n db_api.async_transaction_delete(context, job)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _lswitch_select_open(self, context, switches=None, **kwargs):\n\n if switches is not None:\n for res in switches[\"results\"]:\n count = res[\"_relations\"][\"LogicalSwitchStatus\"][\"lport_count\"]\n if (self.limits['max_ports_per_switch'] == 0 or\n count < self.limits['max_ports_per_switch']):\n return res[\"uuid\"]\n return None", "response": "Selects an open lswitch for a network."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _add_default_tz_bindings(self, context, switch, network_id):\n default_tz = CONF.NVP.default_tz\n\n # If there is no default tz specified it's pointless to try\n # and add any additional default tz bindings.\n if not default_tz:\n LOG.warn(\"additional_default_tz_types specified, \"\n \"but no default_tz. Skipping \"\n \"_add_default_tz_bindings().\")\n return\n\n # This should never be called without a neutron network uuid,\n # we require it to bind some segment allocations.\n if not network_id:\n LOG.warn(\"neutron network_id not specified, skipping \"\n \"_add_default_tz_bindings()\")\n return\n\n for net_type in CONF.NVP.additional_default_tz_types:\n if net_type in TZ_BINDINGS:\n binding = TZ_BINDINGS[net_type]\n binding.add(context, switch, default_tz, network_id)\n else:\n LOG.warn(\"Unknown default tz type %s\" % (net_type))", "response": "Configure any additional default transport zone bindings."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_lswitch_ids_for_network(self, context, network_id):\n lswitches = self._lswitches_for_network(context, network_id).results()\n return [s['uuid'] for s in lswitches[\"results\"]]", "response": "Public interface for fetching lswitch ids for a given network."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef register_floating_ip(self, floating_ip, port_fixed_ips):\n url = CONF.QUARK.floating_ip_base_url\n timeout = CONF.QUARK.unicorn_api_timeout_seconds\n req = self._build_request_body(floating_ip, port_fixed_ips)\n\n try:\n LOG.info(\"Calling unicorn to register floating ip: %s %s\"\n % (url, req))\n r = requests.post(url, data=json.dumps(req), timeout=timeout)\n except Exception as e:\n LOG.error(\"Unhandled Exception caught when trying to register \"\n \"floating ip %s with the unicorn API. Error: %s\"\n % (floating_ip.id, e.message))\n raise ex.RegisterFloatingIpFailure(id=floating_ip.id)\n\n if r.status_code != 200 and r.status_code != 201:\n msg = \"Unexpected status from unicorn API: Status Code %s, \" \\\n \"Message: %s\" % (r.status_code, r.json())\n LOG.error(\"register_floating_ip: %s\" % msg)\n raise ex.RegisterFloatingIpFailure(id=floating_ip.id)", "response": "Register a floating IP with Unicorn."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving a floating ip from Unicorn.", "response": "def remove_floating_ip(self, floating_ip):\n \"\"\"Register a floating ip with Unicorn\n\n :param floating_ip: The quark.db.models.IPAddress to remove\n :return: None\n \"\"\"\n url = \"%s/%s\" % (CONF.QUARK.floating_ip_base_url,\n floating_ip.address_readable)\n timeout = CONF.QUARK.unicorn_api_timeout_seconds\n\n try:\n LOG.info(\"Calling unicorn to remove floating ip: %s\" % url)\n r = requests.delete(url, timeout=timeout)\n except Exception as e:\n LOG.error(\"Unhandled Exception caught when trying to un-register \"\n \"floating ip %s with the unicorn API. Error: %s\"\n % (floating_ip.id, e.message))\n raise ex.RemoveFloatingIpFailure(id=floating_ip.id)\n\n if r.status_code == 404:\n LOG.warn(\"The floating IP %s does not exist in the unicorn system.\"\n % floating_ip.address_readable)\n elif r.status_code != 204:\n msg = \"Unexpected status from unicorn API: Status Code %s, \" \\\n \"Message: %s\" % (r.status_code, r.json())\n LOG.error(\"remove_floating_ip: %s\" % msg)\n raise ex.RemoveFloatingIpFailure(id=floating_ip.id)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _load_worker_plugin_with_module(self, module, version):\n classes = inspect.getmembers(module, inspect.isclass)\n loaded = 0\n for cls_name, cls in classes:\n if hasattr(cls, 'versions'):\n if version not in cls.versions:\n continue\n else:\n continue\n if issubclass(cls, base_worker.QuarkAsyncPluginBase):\n LOG.debug(\"Loading plugin %s\" % cls_name)\n plugin = cls()\n self.plugins.append(plugin)\n loaded += 1\n LOG.debug(\"Found %d possible plugins and loaded %d\" %\n (len(classes), loaded))", "response": "Loads all worker plugins that have requsite properties."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _discover_via_entrypoints(self):\n emgr = extension.ExtensionManager(PLUGIN_EP, invoke_on_load=False)\n return ((ext.name, ext.plugin) for ext in emgr)", "response": "Looks for modules with amtching entry points."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlaunches configured rpc workers.", "response": "def serve_rpc(self):\n \"\"\"Launches configured # of workers per loaded plugin.\"\"\"\n if cfg.CONF.QUARK_ASYNC.rpc_workers < 1:\n cfg.CONF.set_override('rpc_workers', 1, \"QUARK_ASYNC\")\n\n try:\n rpc = service.RpcWorker(self.plugins)\n launcher = common_service.ProcessLauncher(CONF, wait_interval=1.0)\n launcher.launch_service(rpc, workers=CONF.QUARK_ASYNC.rpc_workers)\n\n return launcher\n except Exception:\n with excutils.save_and_reraise_exception():\n LOG.exception(_LE('Unrecoverable error: please check log for '\n 'details.'))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes eventlet and starts wait for workers to exit.", "response": "def start_api_and_rpc_workers(self):\n \"\"\"Initializes eventlet and starts wait for workers to exit.\n\n Spawns the workers returned from serve_rpc\n \"\"\"\n pool = eventlet.GreenPool()\n\n quark_rpc = self.serve_rpc()\n pool.spawn(quark_rpc.wait)\n\n pool.waitall()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _chunks(self, iterable, chunk_size):\n iterator = iter(iterable)\n chunk = list(itertools.islice(iterator, 0, chunk_size))\n while chunk:\n yield chunk\n chunk = list(itertools.islice(iterator, 0, chunk_size))", "response": "Yields data into chunks with size < = chunk_size."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _check_collisions(self, new_range, existing_ranges):\n def _contains(num, r1):\n return (num >= r1[0] and\n num <= r1[1])\n\n def _is_overlap(r1, r2):\n return (_contains(r1[0], r2) or\n _contains(r1[1], r2) or\n _contains(r2[0], r1) or\n _contains(r2[1], r1))\n\n for existing_range in existing_ranges:\n if _is_overlap(new_range, existing_range):\n return True\n return False", "response": "Check for overlapping ranges."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _try_allocate(self, context, segment_id, network_id):\n LOG.info(\"Attempting to allocate segment for network %s \"\n \"segment_id %s segment_type %s\"\n % (network_id, segment_id, self.segment_type))\n\n filter_dict = {\n \"segment_id\": segment_id,\n \"segment_type\": self.segment_type,\n \"do_not_use\": False\n }\n available_ranges = db_api.segment_allocation_range_find(\n context, scope=db_api.ALL, **filter_dict)\n available_range_ids = [r[\"id\"] for r in available_ranges]\n\n try:\n with context.session.begin(subtransactions=True):\n # Search for any deallocated segment ids for the\n # given segment.\n filter_dict = {\n \"deallocated\": True,\n \"segment_id\": segment_id,\n \"segment_type\": self.segment_type,\n \"segment_allocation_range_ids\": available_range_ids\n }\n\n # NOTE(morgabra) We select 100 deallocated segment ids from\n # the table here, and then choose 1 randomly. This is to help\n # alleviate the case where an uncaught exception might leave\n # an allocation active on a remote service but we do not have\n # a record of it locally. If we *do* end up choosing a\n # conflicted id, the caller should simply allocate another one\n # and mark them all as reserved. If a single object has\n # multiple reservations on the same segment, they will not be\n # deallocated, and the operator must resolve the conficts\n # manually.\n allocations = db_api.segment_allocation_find(\n context, lock_mode=True, **filter_dict).limit(100).all()\n\n if allocations:\n allocation = random.choice(allocations)\n\n # Allocate the chosen segment.\n update_dict = {\n \"deallocated\": False,\n \"deallocated_at\": None,\n \"network_id\": network_id\n }\n allocation = db_api.segment_allocation_update(\n context, allocation, **update_dict)\n LOG.info(\"Allocated segment %s for network %s \"\n \"segment_id %s segment_type %s\"\n % (allocation[\"id\"], network_id, segment_id,\n self.segment_type))\n return allocation\n except Exception:\n LOG.exception(\"Error in segment reallocation.\")\n\n LOG.info(\"Cannot find reallocatable segment for network %s \"\n \"segment_id %s segment_type %s\"\n % (network_id, segment_id, self.segment_type))", "response": "Try to allocate a new segment from the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_locks(context, network_ids, addresses):\n addresses_no_longer_null_routed = _find_addresses_to_be_unlocked(\n context, network_ids, addresses)\n LOG.info(\"Deleting %s lock holders on IPAddress with ids: %s\",\n len(addresses_no_longer_null_routed),\n [addr.id for addr in addresses_no_longer_null_routed])\n\n for address in addresses_no_longer_null_routed:\n lock_holder = None\n try:\n lock_holder = db_api.lock_holder_find(\n context, lock_id=address.lock_id, name=LOCK_NAME,\n scope=db_api.ONE)\n if lock_holder:\n db_api.lock_holder_delete(context, address, lock_holder)\n except Exception:\n LOG.exception(\"Failed to delete lock holder %s\", lock_holder)\n continue\n context.session.flush()", "response": "Deletes locks for each IP address that is no longer null - routed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_locks(context, network_ids, addresses):\n\n for address in addresses:\n address_model = None\n try:\n address_model = _find_or_create_address(\n context, network_ids, address)\n lock_holder = None\n if address_model.lock_id:\n lock_holder = db_api.lock_holder_find(\n context,\n lock_id=address_model.lock_id, name=LOCK_NAME,\n scope=db_api.ONE)\n\n if not lock_holder:\n LOG.info(\"Creating lock holder on IPAddress %s with id %s\",\n address_model.address_readable,\n address_model.id)\n db_api.lock_holder_create(\n context, address_model, name=LOCK_NAME, type=\"ip_address\")\n except Exception:\n LOG.exception(\"Failed to create lock holder on IPAddress %s\",\n address_model)\n continue\n context.session.flush()", "response": "Creates locks for each IP address that is null - routed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nselecting the appropriate IPAM strategy for the given network.", "response": "def select_ipam_strategy(self, network_id, network_strategy, **kwargs):\n \"\"\"Return relevant IPAM strategy name.\n\n :param network_id: neutron network id.\n :param network_strategy: default strategy for the network.\n\n NOTE(morgabra) This feels like a hack but I can't think of a better\n idea. The root problem is we can now attach ports to networks with\n a different backend driver/ipam strategy than the network speficies.\n\n We handle the the backend driver part with allowing network_plugin to\n be specified for port objects. This works pretty well because nova or\n whatever knows when we are hooking up an Ironic node so it can pass\n along that key during port_create().\n\n IPAM is a little trickier, especially in Ironic's case, because we\n *must* use a specific IPAM for provider networks. There isn't really\n much of an option other than involve the backend driver when selecting\n the IPAM strategy.\n \"\"\"\n LOG.info(\"Selecting IPAM strategy for network_id:%s \"\n \"network_strategy:%s\" % (network_id, network_strategy))\n\n net_type = \"tenant\"\n if STRATEGY.is_provider_network(network_id):\n net_type = \"provider\"\n\n strategy = self._ipam_strategies.get(net_type, {})\n default = strategy.get(\"default\")\n overrides = strategy.get(\"overrides\", {})\n\n # If we override a particular strategy explicitly, we use it.\n if network_strategy in overrides:\n LOG.info(\"Selected overridden IPAM strategy: %s\"\n % (overrides[network_strategy]))\n return overrides[network_strategy]\n\n # Otherwise, we are free to use an explicit default.\n if default:\n LOG.info(\"Selected default IPAM strategy for tenant \"\n \"network: %s\" % (default))\n return default\n\n # Fallback to the network-specified IPAM strategy\n LOG.info(\"Selected network strategy for tenant \"\n \"network: %s\" % (network_strategy))\n return network_strategy"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget extra network information.", "response": "def _get_base_network_info(self, context, network_id, base_net_driver):\n \"\"\"Return a dict of extra network information.\n\n :param context: neutron request context.\n :param network_id: neturon network id.\n :param net_driver: network driver associated with network_id.\n :raises IronicException: Any unexpected data fetching failures will\n be logged and IronicException raised.\n\n This driver can attach to networks managed by other drivers. We may\n need some information from these drivers, or otherwise inform\n downstream about the type of network we are attaching to. We can\n make these decisions here.\n \"\"\"\n driver_name = base_net_driver.get_name()\n net_info = {\"network_type\": driver_name}\n LOG.debug('_get_base_network_info: %s %s'\n % (driver_name, network_id))\n\n # If the driver is NVP, we need to look up the lswitch id we should\n # be attaching to.\n if driver_name == 'NVP':\n LOG.debug('looking up lswitch ids for network %s'\n % (network_id))\n lswitch_ids = base_net_driver.get_lswitch_ids_for_network(\n context, network_id)\n\n if not lswitch_ids or len(lswitch_ids) > 1:\n msg = ('lswitch id lookup failed, %s ids found.'\n % (len(lswitch_ids)))\n LOG.error(msg)\n raise IronicException(msg)\n\n lswitch_id = lswitch_ids.pop()\n LOG.info('found lswitch for network %s: %s'\n % (network_id, lswitch_id))\n net_info['lswitch_id'] = lswitch_id\n\n LOG.debug('_get_base_network_info finished: %s %s %s'\n % (driver_name, network_id, net_info))\n return net_info"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_port(self, context, network_id, port_id, **kwargs):\n LOG.info(\"create_port %s %s %s\" % (context.tenant_id, network_id,\n port_id))\n\n # sanity check\n if not kwargs.get('base_net_driver'):\n raise IronicException(msg='base_net_driver required.')\n base_net_driver = kwargs['base_net_driver']\n\n if not kwargs.get('device_id'):\n raise IronicException(msg='device_id required.')\n device_id = kwargs['device_id']\n\n if not kwargs.get('instance_node_id'):\n raise IronicException(msg='instance_node_id required.')\n instance_node_id = kwargs['instance_node_id']\n\n if not kwargs.get('mac_address'):\n raise IronicException(msg='mac_address is required.')\n mac_address = str(netaddr.EUI(kwargs[\"mac_address\"][\"address\"]))\n mac_address = mac_address.replace('-', ':')\n\n # TODO(morgabra): Change this when we enable security groups.\n if kwargs.get('security_groups'):\n msg = 'ironic driver does not support security group operations.'\n raise IronicException(msg=msg)\n\n # unroll the given address models into a fixed_ips list we can\n # pass downstream\n fixed_ips = []\n addresses = kwargs.get('addresses')\n if not isinstance(addresses, list):\n addresses = [addresses]\n for address in addresses:\n fixed_ips.append(self._make_fixed_ip_dict(context, address))\n\n body = {\n \"id\": port_id,\n \"network_id\": network_id,\n \"device_id\": device_id,\n \"device_owner\": kwargs.get('device_owner', ''),\n \"tenant_id\": context.tenant_id or \"quark\",\n \"roles\": context.roles,\n \"mac_address\": mac_address,\n \"fixed_ips\": fixed_ips,\n \"switch:hardware_id\": instance_node_id,\n \"dynamic_network\": not STRATEGY.is_provider_network(network_id)\n }\n\n net_info = self._get_base_network_info(\n context, network_id, base_net_driver)\n body.update(net_info)\n\n try:\n LOG.info(\"creating downstream port: %s\" % (body))\n port = self._create_port(context, body)\n LOG.info(\"created downstream port: %s\" % (port))\n return {\"uuid\": port['port']['id'],\n \"vlan_id\": port['port']['vlan_id']}\n except Exception as e:\n msg = \"failed to create downstream port. Exception: %s\" % (e)\n raise IronicException(msg=msg)", "response": "Create a port in a network."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates a port. :param context: neutron api request context. :param port_id: neutron port id. :param kwargs: optional kwargs. :raises IronicException: If the client is unable to update the downstream port for any reason, the exception will be logged and IronicException raised. TODO(morgabra) It does not really make sense in the context of Ironic to allow updating ports. fixed_ips and mac_address are burned in the configdrive on the host, and we otherwise cannot migrate a port between instances. Eventually we will need to support security groups, but for now it's a no-op on port data changes, and we need to rely on the API/Nova to not allow updating data on active ports.", "response": "def update_port(self, context, port_id, **kwargs):\n \"\"\"Update a port.\n\n :param context: neutron api request context.\n :param port_id: neutron port id.\n :param kwargs: optional kwargs.\n :raises IronicException: If the client is unable to update the\n downstream port for any reason, the exception will be logged\n and IronicException raised.\n\n TODO(morgabra) It does not really make sense in the context of Ironic\n to allow updating ports. fixed_ips and mac_address are burned in the\n configdrive on the host, and we otherwise cannot migrate a port between\n instances. Eventually we will need to support security groups, but for\n now it's a no-op on port data changes, and we need to rely on the\n API/Nova to not allow updating data on active ports.\n \"\"\"\n LOG.info(\"update_port %s %s\" % (context.tenant_id, port_id))\n\n # TODO(morgabra): Change this when we enable security groups.\n if kwargs.get(\"security_groups\"):\n msg = 'ironic driver does not support security group operations.'\n raise IronicException(msg=msg)\n\n return {\"uuid\": port_id}"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes a port. :param context: neutron api request context. :param port_id: neutron port id. :param kwargs: optional kwargs. :raises IronicException: If the client is unable to delete the downstream port for any reason, the exception will be logged and IronicException raised.", "response": "def delete_port(self, context, port_id, **kwargs):\n \"\"\"Delete a port.\n\n :param context: neutron api request context.\n :param port_id: neutron port id.\n :param kwargs: optional kwargs.\n :raises IronicException: If the client is unable to delete the\n downstream port for any reason, the exception will be logged\n and IronicException raised.\n \"\"\"\n LOG.info(\"delete_port %s %s\" % (context.tenant_id, port_id))\n try:\n self._delete_port(context, port_id)\n LOG.info(\"deleted downstream port: %s\" % (port_id))\n except Exception:\n LOG.error(\"failed deleting downstream port, it is now \"\n \"orphaned! port_id: %s\" % (port_id))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndiagnoses a port. :param context: neutron api request context. :param port_id: neutron port id. :param kwargs: optional kwargs. :raises IronicException: If the client is unable to fetch the downstream port for any reason, the exception will be logged and IronicException raised.", "response": "def diag_port(self, context, port_id, **kwargs):\n \"\"\"Diagnose a port.\n\n :param context: neutron api request context.\n :param port_id: neutron port id.\n :param kwargs: optional kwargs.\n :raises IronicException: If the client is unable to fetch the\n downstream port for any reason, the exception will be\n logged and IronicException raised.\n \"\"\"\n LOG.info(\"diag_port %s\" % port_id)\n try:\n port = self._client.show_port(port_id)\n except Exception as e:\n msg = \"failed fetching downstream port: %s\" % (str(e))\n LOG.exception(msg)\n raise IronicException(msg=msg)\n return {\"downstream_port\": port}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the tag on the given model object.", "response": "def set(self, model, value):\n \"\"\"Set tag on model object.\"\"\"\n self.validate(value)\n self._pop(model)\n value = self.serialize(value)\n model.tags.append(value)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get(self, model):\n for tag in model.tags:\n if self.is_tag(tag):\n value = self.deserialize(tag)\n try:\n self.validate(value)\n return value\n except TagValidationError:\n continue\n return None", "response": "Get a matching valid tag off the model."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npop all matching tags off the model and return them.", "response": "def _pop(self, model):\n \"\"\"Pop all matching tags off the model and return them.\"\"\"\n tags = []\n\n # collect any exsiting tags with matching prefix\n for tag in model.tags:\n if self.is_tag(tag):\n tags.append(tag)\n\n # remove collected tags from model\n if tags:\n for tag in tags:\n model.tags.remove(tag)\n\n return tags"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef pop(self, model):\n tags = self._pop(model)\n if tags:\n for tag in tags:\n value = self.deserialize(tag)\n try:\n self.validate(value)\n return value\n except TagValidationError:\n continue", "response": "Pop all matching tags off the port return a valid one."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndoing the given port have this tag?", "response": "def has_tag(self, model):\n \"\"\"Does the given port have this tag?\"\"\"\n for tag in model.tags:\n if self.is_tag(tag):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nvalidates a VLAN ID.", "response": "def validate(self, value):\n \"\"\"Validates a VLAN ID.\n\n :param value: The VLAN ID to validate against.\n :raises TagValidationError: Raised if the VLAN ID is invalid.\n \"\"\"\n try:\n vlan_id_int = int(value)\n assert vlan_id_int >= self.MIN_VLAN_ID\n assert vlan_id_int <= self.MAX_VLAN_ID\n except Exception:\n msg = (\"Invalid vlan_id. Got '%(vlan_id)s'. \"\n \"vlan_id should be an integer between %(min)d and %(max)d \"\n \"inclusive.\" % {'vlan_id': value,\n 'min': self.MIN_VLAN_ID,\n 'max': self.MAX_VLAN_ID})\n raise TagValidationError(value, msg)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_all(self, model):\n tags = {}\n for name, tag in self.tags.items():\n for mtag in model.tags:\n if tag.is_tag(mtag):\n tags[name] = tag.get(model)\n return tags", "response": "Get all known tags from a model."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nvalidate and set all known tags on a port.", "response": "def set_all(self, model, **tags):\n \"\"\"Validate and set all known tags on a port.\"\"\"\n for name, tag in self.tags.items():\n if name in tags:\n value = tags.pop(name)\n if value:\n try:\n tag.set(model, value)\n except TagValidationError as e:\n raise n_exc.BadRequest(\n resource=\"tags\",\n msg=\"%s\" % (e.message))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef serialize_rules(self, rules):\n # TODO(mdietz): If/when we support other rule types, this comment\n # will have to be revised.\n # Action and direction are static, for now. The implementation may\n # support 'deny' and 'egress' respectively in the future. We allow\n # the direction to be set to something else, technically, but current\n # plugin level call actually raises. It's supported here for unit\n # test purposes at this time\n serialized = []\n for rule in rules:\n direction = rule[\"direction\"]\n source = ''\n destination = ''\n if rule.get(\"remote_ip_prefix\"):\n prefix = rule[\"remote_ip_prefix\"]\n if direction == \"ingress\":\n source = self._convert_remote_network(prefix)\n else:\n if (Capabilities.EGRESS not in\n CONF.QUARK.environment_capabilities):\n raise q_exc.EgressSecurityGroupRulesNotEnabled()\n else:\n destination = self._convert_remote_network(prefix)\n\n optional_fields = {}\n\n # NOTE(mdietz): this will expand as we add more protocols\n protocol_map = protocols.PROTOCOL_MAP[rule[\"ethertype\"]]\n if rule[\"protocol\"] == protocol_map[\"icmp\"]:\n optional_fields[\"icmp type\"] = rule[\"port_range_min\"]\n optional_fields[\"icmp code\"] = rule[\"port_range_max\"]\n else:\n optional_fields[\"port start\"] = rule[\"port_range_min\"]\n optional_fields[\"port end\"] = rule[\"port_range_max\"]\n\n payload = {\"ethertype\": rule[\"ethertype\"],\n \"protocol\": rule[\"protocol\"],\n \"source network\": source,\n \"destination network\": destination,\n \"action\": \"allow\",\n \"direction\": direction}\n payload.update(optional_fields)\n serialized.append(payload)\n return serialized", "response": "Creates a payload for the redis server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef serialize_groups(self, groups):\n rules = []\n for group in groups:\n rules.extend(self.serialize_rules(group.rules))\n return rules", "response": "Returns a JSON representation of the given set of security groups."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwrites a series of security group rules to a redis server.", "response": "def apply_rules(self, device_id, mac_address, rules):\n \"\"\"Writes a series of security group rules to a redis server.\"\"\"\n LOG.info(\"Applying security group rules for device %s with MAC %s\" %\n (device_id, mac_address))\n\n rule_dict = {SECURITY_GROUP_RULE_KEY: rules}\n redis_key = self.vif_key(device_id, mac_address)\n # TODO(mdietz): Pipeline these. Requires some rewriting\n self.set_field(redis_key, SECURITY_GROUP_HASH_ATTR, rule_dict)\n self.set_field_raw(redis_key, SECURITY_GROUP_ACK, False)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_security_group_states(self, interfaces):\n LOG.debug(\"Getting security groups from Redis for {0}\".format(\n interfaces))\n interfaces = tuple(interfaces)\n vif_keys = [self.vif_key(vif.device_id, vif.mac_address)\n for vif in interfaces]\n\n # Retrieve all fields associated with this key, which should be\n # 'security groups ack' and 'security group rules'.\n sec_grp_all = self.get_fields_all(vif_keys)\n\n ret = {}\n # Associate the vif with the fields in a dictionary\n for vif, group in zip(interfaces, sec_grp_all):\n if group:\n ret[vif] = {SECURITY_GROUP_ACK: None,\n SECURITY_GROUP_HASH_ATTR: []}\n temp_ack = group[SECURITY_GROUP_ACK].lower()\n temp_rules = group[SECURITY_GROUP_HASH_ATTR]\n if temp_rules:\n temp_rules = json.loads(temp_rules)\n ret[vif][SECURITY_GROUP_HASH_ATTR] = temp_rules[\"rules\"]\n if \"true\" in temp_ack:\n ret[vif][SECURITY_GROUP_ACK] = True\n elif \"false\" in temp_ack:\n ret[vif][SECURITY_GROUP_ACK] = False\n else:\n ret.pop(vif, None)\n LOG.debug(\"Skipping bad ack value %s\" % temp_ack)\n\n return ret", "response": "Get the state of the security group for the given interfaces from Redis."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating the ack field of the security groups by setting the ack field of the security groups.", "response": "def update_group_states_for_vifs(self, vifs, ack):\n \"\"\"Updates security groups by setting the ack field\"\"\"\n vif_keys = [self.vif_key(vif.device_id, vif.mac_address)\n for vif in vifs]\n self.set_fields(vif_keys, SECURITY_GROUP_ACK, ack)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef run_migrations_offline():\n context.configure(url=neutron_config.database.connection)\n\n with context.begin_transaction():\n context.run_migrations()", "response": "Run migrations in offline mode."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning migrations in online mode.", "response": "def run_migrations_online():\n \"\"\"Run migrations in 'online' mode.\n\n In this scenario we need to create an Engine\n and associate a connection with the context.\n\n \"\"\"\n engine = create_engine(\n neutron_config.database.connection,\n poolclass=pool.NullPool)\n\n connection = engine.connect()\n context.configure(\n connection=connection,\n target_metadata=target_metadata)\n\n try:\n with context.begin_transaction():\n context.run_migrations()\n finally:\n connection.close()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build_payload(ipaddress,\n event_type,\n event_time=None,\n start_time=None,\n end_time=None):\n \"\"\"Method builds a payload out of the passed arguments.\n\n Parameters:\n `ipaddress`: the models.IPAddress object\n `event_type`: USAGE,CREATE,DELETE,SUSPEND,or UNSUSPEND\n `start_time`: startTime for cloudfeeds\n `end_time`: endTime for cloudfeeds\n Returns a dictionary suitable to notify billing.\n Message types mapping to cloud feeds for references:\n ip.exists - USAGE\n ip.add - CREATE\n ip.delete - DELETE\n ip.associate - UP\n ip.disassociate - DOWN\n Refer to: http://rax.io/cf-api for more details.\n \"\"\"\n # This is the common part of all message types\n payload = {\n 'event_type': unicode(event_type),\n 'tenant_id': unicode(ipaddress.used_by_tenant_id),\n 'ip_address': unicode(ipaddress.address_readable),\n 'ip_version': int(ipaddress.version),\n 'ip_type': unicode(ipaddress.address_type),\n 'id': unicode(ipaddress.id)\n }\n\n # Depending on the message type add the appropriate fields\n if event_type == IP_EXISTS:\n if start_time is None or end_time is None:\n raise ValueError('IP_BILL: {} start_time/end_time cannot be empty'\n .format(event_type))\n payload.update({\n 'startTime': unicode(convert_timestamp(start_time)),\n 'endTime': unicode(convert_timestamp(end_time))\n })\n elif event_type in [IP_ADD, IP_DEL, IP_ASSOC, IP_DISASSOC]:\n if event_time is None:\n raise ValueError('IP_BILL: {}: event_time cannot be NULL'\n .format(event_type))\n payload.update({\n 'eventTime': unicode(convert_timestamp(event_time)),\n 'subnet_id': unicode(ipaddress.subnet_id),\n 'network_id': unicode(ipaddress.network_id),\n 'public': True if ipaddress.network_id == PUBLIC_NETWORK_ID\n else False,\n })\n else:\n raise ValueError('IP_BILL: bad event_type: {}'.format(event_type))\n\n return payload", "response": "Method builds a payload for the passed IP object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef build_full_day_ips(query, period_start, period_end):\n # Filter out only IPv4 that have not been deallocated\n ip_list = query.\\\n filter(models.IPAddress.version == 4L).\\\n filter(models.IPAddress.network_id == PUBLIC_NETWORK_ID).\\\n filter(models.IPAddress.used_by_tenant_id is not None).\\\n filter(models.IPAddress.allocated_at != null()).\\\n filter(models.IPAddress.allocated_at < period_start).\\\n filter(or_(models.IPAddress._deallocated is False,\n models.IPAddress.deallocated_at == null(),\n models.IPAddress.deallocated_at >= period_end)).all()\n\n return ip_list", "response": "Method to build a full day IP list for the case 1 where the IP was allocated before the period start and the IP is still allocated after the period end."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef calc_periods(hour=0, minute=0):\n # Calculate the time intervals in a usable form\n period_end = datetime.datetime.utcnow().replace(hour=hour,\n minute=minute,\n second=0,\n microsecond=0)\n period_start = period_end - datetime.timedelta(days=1)\n\n # period end should be slightly before the midnight.\n # hence, we subtract a second\n # this will force period_end to store something like:\n # datetime.datetime(2016, 5, 19, 23, 59, 59, 999999)\n # instead of:\n # datetime.datetime(2016, 5, 20, 0, 0, 0, 0)\n period_end -= datetime.timedelta(seconds=1)\n\n return (period_start, period_end)", "response": "Calculates the start and end time intervals for a given time interval."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates the view for a job while calculating progress.", "response": "def _make_job_dict(job):\n \"\"\"Creates the view for a job while calculating progress.\n\n Since a root job does not have a transaction id (TID) it will return its\n id as the TID.\n \"\"\"\n body = {\"id\": job.get('id'),\n \"action\": job.get('action'),\n \"completed\": job.get('completed'),\n \"tenant_id\": job.get('tenant_id'),\n \"created_at\": job.get('created_at'),\n \"transaction_id\": job.get('transaction_id'),\n \"parent_id\": job.get('parent_id', None)}\n if not body['transaction_id']:\n body['transaction_id'] = job.get('id')\n completed = 0\n for sub in job.subtransactions:\n if sub.get('completed'):\n completed += 1\n pct = 100 if job.get('completed') else 0\n if len(job.subtransactions) > 0:\n pct = float(completed) / len(job.subtransactions) * 100.0\n body['transaction_percent'] = int(pct)\n body['completed_subtransactions'] = completed\n body['subtransactions'] = len(job.subtransactions)\n return body"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_mac_address_range(context, id, fields=None):\n LOG.info(\"get_mac_address_range %s for tenant %s fields %s\" %\n (id, context.tenant_id, fields))\n\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n\n mac_address_range = db_api.mac_address_range_find(\n context, id=id, scope=db_api.ONE)\n\n if not mac_address_range:\n raise q_exc.MacAddressRangeNotFound(\n mac_address_range_id=id)\n return v._make_mac_range_dict(mac_address_range)", "response": "Get a mac_address_range for a network."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes a mac_address_range. : param context: neutron api request context : param id: UUID representing the mac_address_range to delete.", "response": "def delete_mac_address_range(context, id):\n \"\"\"Delete a mac_address_range.\n\n : param context: neutron api request context\n : param id: UUID representing the mac_address_range to delete.\n \"\"\"\n LOG.info(\"delete_mac_address_range %s for tenant %s\" %\n (id, context.tenant_id))\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n\n with context.session.begin():\n mar = db_api.mac_address_range_find(context, id=id, scope=db_api.ONE)\n if not mar:\n raise q_exc.MacAddressRangeNotFound(\n mac_address_range_id=id)\n _delete_mac_address_range(context, mar)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef downgrade():\n with op.batch_alter_table(t2_name) as batch_op:\n batch_op.drop_column('do_not_use')\n\n with op.batch_alter_table(t1_name) as batch_op:\n batch_op.drop_column('enabled')", "response": "downgrade the tables to the original state"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndeleting a segment_allocation_range. : param context: neutron api request context : param id: UUID representing the segment_allocation_range to delete.", "response": "def delete_segment_allocation_range(context, sa_id):\n \"\"\"Delete a segment_allocation_range.\n\n : param context: neutron api request context\n : param id: UUID representing the segment_allocation_range to delete.\n \"\"\"\n LOG.info(\"delete_segment_allocation_range %s for tenant %s\" %\n (sa_id, context.tenant_id))\n if not context.is_admin:\n raise n_exc.NotAuthorized()\n\n with context.session.begin():\n sa_range = db_api.segment_allocation_range_find(\n context, id=sa_id, scope=db_api.ONE)\n if not sa_range:\n raise q_exc.SegmentAllocationRangeNotFound(\n segment_allocation_range_id=sa_id)\n _delete_segment_allocation_range(context, sa_range)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef filter_factory(global_conf, **local_conf):\n conf = global_conf.copy()\n conf.update(local_conf)\n\n def wrapper(app):\n return ResponseAsyncIdAdder(app, conf)\n\n return wrapper", "response": "Returns a WSGI filter app for use with paste. deploy."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the list of all used IPs in the current network.", "response": "def get_used_ips(session, **kwargs):\n \"\"\"Returns dictionary with keys segment_id and value used IPs count.\n\n Used IP address count is determined by:\n - allocated IPs\n - deallocated IPs whose `deallocated_at` is within the `reuse_after`\n window compared to the present time, excluding IPs that are accounted for\n in the current IP policy (because IP policy is mutable and deallocated IPs\n are not checked nor deleted on IP policy creation, thus deallocated IPs\n that don't fit the current IP policy can exist in the neutron database).\n \"\"\"\n LOG.debug(\"Getting used IPs...\")\n with session.begin():\n query = session.query(\n models.Subnet.segment_id,\n func.count(models.IPAddress.address))\n query = query.group_by(models.Subnet.segment_id)\n query = _filter(query, **kwargs)\n\n reuse_window = timeutils.utcnow() - datetime.timedelta(\n seconds=cfg.CONF.QUARK.ipam_reuse_after)\n # NOTE(asadoughi): This is an outer join instead of a regular join\n # to include subnets with zero IP addresses in the database.\n query = query.outerjoin(\n models.IPAddress,\n and_(models.Subnet.id == models.IPAddress.subnet_id,\n or_(not_(models.IPAddress.lock_id.is_(None)),\n models.IPAddress._deallocated.is_(None),\n models.IPAddress._deallocated == 0,\n models.IPAddress.deallocated_at > reuse_window)))\n\n query = query.outerjoin(\n models.IPPolicyCIDR,\n and_(\n models.Subnet.ip_policy_id == models.IPPolicyCIDR.ip_policy_id,\n models.IPAddress.address >= models.IPPolicyCIDR.first_ip,\n models.IPAddress.address <= models.IPPolicyCIDR.last_ip))\n # NOTE(asadoughi): (address is allocated) OR\n # (address is deallocated and not inside subnet's IP policy)\n query = query.filter(or_(\n models.IPAddress._deallocated.is_(None),\n models.IPAddress._deallocated == 0,\n models.IPPolicyCIDR.id.is_(None)))\n\n ret = ((segment_id, address_count)\n for segment_id, address_count in query.all())\n return dict(ret)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_unused_ips(session, used_ips_counts, **kwargs):\n LOG.debug(\"Getting unused IPs...\")\n with session.begin():\n query = session.query(\n models.Subnet.segment_id,\n models.Subnet)\n query = _filter(query, **kwargs)\n query = query.group_by(models.Subnet.segment_id, models.Subnet.id)\n\n ret = defaultdict(int)\n for segment_id, subnet in query.all():\n net_size = netaddr.IPNetwork(subnet._cidr).size\n ip_policy = subnet[\"ip_policy\"] or {\"size\": 0}\n ret[segment_id] += net_size - ip_policy[\"size\"]\n\n for segment_id in used_ips_counts:\n ret[segment_id] -= used_ips_counts[segment_id]\n\n return ret", "response": "Returns a dictionary with key segment_id and value unused IP address count."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a dict of VM OpaqueRef ( str ) -> xapi. VM.", "response": "def get_instances(self, session):\n \"\"\"Returns a dict of `VM OpaqueRef` (str) -> `xapi.VM`.\"\"\"\n LOG.debug(\"Getting instances from Xapi\")\n\n recs = session.xenapi.VM.get_all_records()\n\n # NOTE(asadoughi): Copied from xen-networking-scripts/utils.py\n is_inst = lambda r: (r['power_state'].lower() == 'running' and\n not r['is_a_template'] and\n not r['is_control_domain'] and\n ('nova_uuid' in r['other_config'] or\n r['name_label'].startswith('instance-')))\n instances = dict()\n for vm_ref, rec in recs.iteritems():\n if not is_inst(rec):\n continue\n instances[vm_ref] = VM(ref=vm_ref,\n uuid=rec[\"other_config\"][\"nova_uuid\"],\n vifs=rec[\"VIFs\"],\n dom_id=rec[\"domid\"])\n return instances"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_interfaces(self):\n LOG.debug(\"Getting interfaces from Xapi\")\n\n with self.sessioned() as session:\n instances = self.get_instances(session)\n recs = session.xenapi.VIF.get_all_records()\n\n interfaces = set()\n for vif_ref, rec in recs.iteritems():\n vm = instances.get(rec[\"VM\"])\n if not vm:\n continue\n device_id = vm.uuid\n interfaces.add(VIF(device_id, rec, vif_ref))\n return interfaces", "response": "Returns a set of VIFs from get_instances return value."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating the set of security groups on the specified interfaces.", "response": "def update_interfaces(self, added_sg, updated_sg, removed_sg):\n \"\"\"Handles changes to interfaces' security groups\n\n Calls refresh_interfaces on argument VIFs. Set security groups on\n added_sg's VIFs. Unsets security groups on removed_sg's VIFs.\n \"\"\"\n if not (added_sg or updated_sg or removed_sg):\n return\n\n with self.sessioned() as session:\n self._set_security_groups(session, added_sg)\n self._unset_security_groups(session, removed_sg)\n combined = added_sg + updated_sg + removed_sg\n self._refresh_interfaces(session, combined)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a network which represents an L2 network segment which can be used to create a set of subnets and ports associated with it.", "response": "def create_network(context, network):\n \"\"\"Create a network.\n\n Create a network which represents an L2 network segment which\n can have a set of subnets and ports associated with it.\n : param context: neutron api request context\n : param network: dictionary describing the network, with keys\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py. All keys will be populated.\n \"\"\"\n LOG.info(\"create_network for tenant %s\" % context.tenant_id)\n\n with context.session.begin():\n net_attrs = network[\"network\"]\n subs = net_attrs.pop(\"subnets\", [])\n # Enforce subnet quotas\n if not context.is_admin:\n if len(subs) > 0:\n v4_count, v6_count = 0, 0\n for s in subs:\n version = netaddr.IPNetwork(s['subnet']['cidr']).version\n if version == 6:\n v6_count += 1\n else:\n v4_count += 1\n if v4_count > 0:\n tenant_q_v4 = context.session.query(qdv.Quota).filter_by(\n tenant_id=context.tenant_id,\n resource='v4_subnets_per_network').first()\n if tenant_q_v4 != -1:\n quota.QUOTAS.limit_check(\n context,\n context.tenant_id,\n v4_subnets_per_network=v4_count)\n if v6_count > 0:\n tenant_q_v6 = context.session.query(qdv.Quota).filter_by(\n tenant_id=context.tenant_id,\n resource='v6_subnets_per_network').first()\n if tenant_q_v6 != -1:\n quota.QUOTAS.limit_check(\n context,\n context.tenant_id,\n v6_subnets_per_network=v6_count)\n # Generate a uuid that we're going to hand to the backend and db\n net_uuid = utils.pop_param(net_attrs, \"id\", None)\n net_type = None\n if net_uuid and context.is_admin:\n net = db_api.network_find(context=context, limit=None,\n sorts=['id'], marker=None,\n page_reverse=False, id=net_uuid,\n scope=db_api.ONE)\n net_type = utils.pop_param(net_attrs, \"network_plugin\", None)\n if net:\n raise q_exc.NetworkAlreadyExists(id=net_uuid)\n else:\n net_uuid = uuidutils.generate_uuid()\n\n # TODO(mdietz) this will be the first component registry hook, but\n # lets make it work first\n pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)\n\n ipam_strategy = utils.pop_param(net_attrs, \"ipam_strategy\", None)\n if not ipam_strategy or not context.is_admin:\n ipam_strategy = CONF.QUARK.default_ipam_strategy\n\n if not ipam.IPAM_REGISTRY.is_valid_strategy(ipam_strategy):\n raise q_exc.InvalidIpamStrategy(strat=ipam_strategy)\n net_attrs[\"ipam_strategy\"] = ipam_strategy\n\n # NOTE(mdietz) I think ideally we would create the providernet\n # elsewhere as a separate driver step that could be\n # kept in a plugin and completely removed if desired. We could\n # have a pre-callback/observer on the netdriver create_network\n # that gathers any additional parameters from the network dict\n\n default_net_type = net_type or CONF.QUARK.default_network_type\n net_driver = registry.DRIVER_REGISTRY.get_driver(default_net_type)\n net_driver.create_network(context, net_attrs[\"name\"],\n network_id=net_uuid, phys_type=pnet_type,\n phys_net=phys_net, segment_id=seg_id)\n\n net_attrs[\"id\"] = net_uuid\n net_attrs[\"tenant_id\"] = context.tenant_id\n net_attrs[\"network_plugin\"] = default_net_type\n new_net = db_api.network_create(context, **net_attrs)\n\n new_subnets = []\n for sub in subs:\n sub[\"subnet\"][\"network_id\"] = new_net[\"id\"]\n sub[\"subnet\"][\"tenant_id\"] = context.tenant_id\n s = db_api.subnet_create(context, **sub[\"subnet\"])\n new_subnets.append(s)\n new_net[\"subnets\"] = new_subnets\n\n # if not security_groups.get_security_groups(\n # context,\n # filters={\"id\": security_groups.DEFAULT_SG_UUID}):\n # security_groups._create_default_security_group(context)\n return v._make_network_dict(new_net)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the values of a network.", "response": "def update_network(context, id, network):\n \"\"\"Update values of a network.\n\n : param context: neutron api request context\n : param id: UUID representing the network to update.\n : param network: dictionary with keys indicating fields to update.\n valid keys are those that have a value of True for 'allow_put'\n as listed in the RESOURCE_ATTRIBUTE_MAP object in\n neutron/api/v2/attributes.py.\n \"\"\"\n LOG.info(\"update_network %s for tenant %s\" %\n (id, context.tenant_id))\n with context.session.begin():\n net = db_api.network_find(context, id=id, scope=db_api.ONE)\n if not net:\n raise n_exc.NetworkNotFound(net_id=id)\n net_dict = network[\"network\"]\n utils.pop_param(net_dict, \"network_plugin\")\n if not context.is_admin and \"ipam_strategy\" in net_dict:\n utils.pop_param(net_dict, \"ipam_strategy\")\n net = db_api.network_update(context, net, **net_dict)\n\n return v._make_network_dict(net)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nretrieves a network. : param context: neutron api request context : param id: UUID representing the network to fetch. : param fields: a list of strings that are valid keys in a network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. Only these fields will be returned.", "response": "def get_network(context, id, fields=None):\n \"\"\"Retrieve a network.\n\n : param context: neutron api request context\n : param id: UUID representing the network to fetch.\n : param fields: a list of strings that are valid keys in a\n network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_network %s for tenant %s fields %s\" %\n (id, context.tenant_id, fields))\n\n network = db_api.network_find(context=context, limit=None, sorts=['id'],\n marker=None, page_reverse=False,\n id=id, join_subnets=True, scope=db_api.ONE)\n if not network:\n raise n_exc.NetworkNotFound(net_id=id)\n return v._make_network_dict(network, fields=fields)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nretrieve a list of networks for the user in the tenant.", "response": "def get_networks(context, limit=None, sorts=['id'], marker=None,\n page_reverse=False, filters=None, fields=None):\n \"\"\"Retrieve a list of networks.\n\n The contents of the list depends on the identity of the user\n making the request (as indicated by the context) as well as any\n filters.\n : param context: neutron api request context\n : param filters: a dictionary with keys that are valid keys for\n a network as listed in the RESOURCE_ATTRIBUTE_MAP object\n in neutron/api/v2/attributes.py. Values in this dictiontary\n are an iterable containing values that will be used for an exact\n match comparison for that value. Each result returned by this\n function will have matched one of the values for each key in\n filters.\n : param fields: a list of strings that are valid keys in a\n network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP\n object in neutron/api/v2/attributes.py. Only these fields\n will be returned.\n \"\"\"\n LOG.info(\"get_networks for tenant %s with filters %s, fields %s\" %\n (context.tenant_id, filters, fields))\n filters = filters or {}\n nets = db_api.network_find(context, limit, sorts, marker, page_reverse,\n join_subnets=True, **filters) or []\n nets = [v._make_network_dict(net, fields=fields) for net in nets]\n return nets"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_networks_count(context, filters=None):\n LOG.info(\"get_networks_count for tenant %s filters %s\" %\n (context.tenant_id, filters))\n return db_api.network_count_all(context)", "response": "Return the number of networks in the current tenant."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeleting a network. : param context: neutron api request context : param id: UUID representing the network to delete.", "response": "def delete_network(context, id):\n \"\"\"Delete a network.\n\n : param context: neutron api request context\n : param id: UUID representing the network to delete.\n \"\"\"\n LOG.info(\"delete_network %s for tenant %s\" % (id, context.tenant_id))\n with context.session.begin():\n net = db_api.network_find(context=context, limit=None, sorts=['id'],\n marker=None, page_reverse=False, id=id,\n scope=db_api.ONE)\n if not net:\n raise n_exc.NetworkNotFound(net_id=id)\n if not context.is_admin:\n if STRATEGY.is_provider_network(net.id):\n raise n_exc.NotAuthorized(net_id=id)\n if net.ports:\n raise n_exc.NetworkInUse(net_id=id)\n net_driver = registry.DRIVER_REGISTRY.get_driver(net[\"network_plugin\"])\n net_driver.delete_network(context, id)\n for subnet in net[\"subnets\"]:\n subnets._delete_subnet(context, subnet)\n db_api.network_delete(context, net)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_case2(context):\n query = context.session.query(models.IPAddress)\n period_start, period_end = billing.calc_periods()\n ip_list = billing.build_full_day_ips(query, period_start, period_end)\n import random\n ind = random.randint(0, len(ip_list) - 1)\n address = ip_list[ind]\n address.allocated_at = datetime.datetime.utcnow() -\\\n datetime.timedelta(days=1)\n context.session.add(address)\n context.session.flush()", "response": "This method is a helper method for testing. It will create a case 2 entry in the database."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns the billing report. Optionally sends notifications to billing", "response": "def main(notify, hour, minute):\n \"\"\"Runs billing report. Optionally sends notifications to billing\"\"\"\n\n # Read the config file and get the admin context\n config_opts = ['--config-file', '/etc/neutron/neutron.conf']\n config.init(config_opts)\n # Have to load the billing module _after_ config is parsed so\n # that we get the right network strategy\n network_strategy.STRATEGY.load()\n billing.PUBLIC_NETWORK_ID = network_strategy.STRATEGY.get_public_net_id()\n config.setup_logging()\n context = neutron_context.get_admin_context()\n\n # A query to get all IPAddress objects from the db\n query = context.session.query(models.IPAddress)\n\n (period_start, period_end) = billing.calc_periods(hour, minute)\n\n full_day_ips = billing.build_full_day_ips(query,\n period_start,\n period_end)\n partial_day_ips = billing.build_partial_day_ips(query,\n period_start,\n period_end)\n\n if notify:\n # '==================== Full Day ============================='\n for ipaddress in full_day_ips:\n click.echo('start: {}, end: {}'.format(period_start, period_end))\n payload = billing.build_payload(ipaddress,\n billing.IP_EXISTS,\n start_time=period_start,\n end_time=period_end)\n billing.do_notify(context,\n billing.IP_EXISTS,\n payload)\n # '==================== Part Day ============================='\n for ipaddress in partial_day_ips:\n click.echo('start: {}, end: {}'.format(period_start, period_end))\n payload = billing.build_payload(ipaddress,\n billing.IP_EXISTS,\n start_time=ipaddress.allocated_at,\n end_time=period_end)\n billing.do_notify(context,\n billing.IP_EXISTS,\n payload)\n else:\n click.echo('Case 1 ({}):\\n'.format(len(full_day_ips)))\n for ipaddress in full_day_ips:\n pp(billing.build_payload(ipaddress,\n billing.IP_EXISTS,\n start_time=period_start,\n end_time=period_end))\n\n click.echo('\\n===============================================\\n')\n\n click.echo('Case 2 ({}):\\n'.format(len(partial_day_ips)))\n for ipaddress in partial_day_ips:\n pp(billing.build_payload(ipaddress,\n billing.IP_EXISTS,\n start_time=ipaddress.allocated_at,\n end_time=period_end))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconfigures all listeners here", "response": "def start_rpc_listeners(self):\n \"\"\"Configure all listeners here\"\"\"\n self._setup_rpc()\n if not self.endpoints:\n return []\n self.conn = n_rpc.create_connection()\n self.conn.create_consumer(self.topic, self.endpoints,\n fanout=False)\n return self.conn.consume_in_threads()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef context(self):\n if not self._context:\n self._context = context.get_admin_context()\n return self._context", "response": "Provides an admin context for workers."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstarting the async update process.", "response": "def update_sg(self, context, sg, rule_id, action):\n \"\"\"Begins the async update process.\"\"\"\n db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)\n if not db_sg:\n return None\n with context.session.begin():\n job_body = dict(action=\"%s sg rule %s\" % (action, rule_id),\n resource_id=rule_id,\n tenant_id=db_sg['tenant_id'])\n job_body = dict(job=job_body)\n job = job_api.create_job(context.elevated(), job_body)\n rpc_client = QuarkSGAsyncProducerClient()\n try:\n rpc_client.populate_subtasks(context, sg, job['id'])\n except om_exc.MessagingTimeout:\n LOG.error(\"Failed to create subtasks. Rabbit running?\")\n return None\n return {\"job_id\": job['id']}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef populate_subtasks(self, context, sg, parent_job_id):\n db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)\n if not db_sg:\n return None\n ports = db_api.sg_gather_associated_ports(context, db_sg)\n if len(ports) == 0:\n return {\"ports\": 0}\n for port in ports:\n job_body = dict(action=\"update port %s\" % port['id'],\n tenant_id=db_sg['tenant_id'],\n resource_id=port['id'],\n parent_id=parent_job_id)\n job_body = dict(job=job_body)\n job = job_api.create_job(context.elevated(), job_body)\n rpc_consumer = QuarkSGAsyncConsumerClient()\n try:\n rpc_consumer.update_port(context, port['id'], job['id'])\n except om_exc.MessagingTimeout:\n # TODO(roaet): Not too sure what can be done here other than\n # updating the job as a failure?\n LOG.error(\"Failed to update port. Rabbit running?\")\n return None", "response": "Populate the list of ports to be updated async."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_ports_for_sg(self, context, portid, jobid):\n port = db_api.port_find(context, id=portid, scope=db_api.ONE)\n if not port:\n LOG.warning(\"Port not found\")\n return\n net_driver = port_api._get_net_driver(port.network, port=port)\n base_net_driver = port_api._get_net_driver(port.network)\n sg_list = [sg for sg in port.security_groups]\n\n success = False\n error = None\n retries = 3\n retry_delay = 2\n for retry in xrange(retries):\n try:\n net_driver.update_port(context, port_id=port[\"backend_key\"],\n mac_address=port[\"mac_address\"],\n device_id=port[\"device_id\"],\n base_net_driver=base_net_driver,\n security_groups=sg_list)\n success = True\n error = None\n break\n except Exception as error:\n LOG.warning(\"Could not connect to redis, but retrying soon\")\n time.sleep(retry_delay)\n status_str = \"\"\n if not success:\n status_str = \"Port %s update failed after %d tries. Error: %s\" % (\n portid, retries, error)\n update_body = dict(completed=True, status=status_str)\n update_body = dict(job=update_body)\n job_api.update_job(context.elevated(), jobid, update_body)", "response": "Updates the ports through redis."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sg_gather_associated_ports(context, group):\n if not group:\n return None\n if not hasattr(group, \"ports\") or len(group.ports) <= 0:\n return []\n return group.ports", "response": "Gather all ports associated to security group."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate a security group rule.", "response": "def security_group_rule_update(context, rule, **kwargs):\n '''Updates a security group rule.\n\n NOTE(alexm) this is non-standard functionality.\n '''\n rule.update(kwargs)\n context.session.add(rule)\n return rule"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nquerying for segment allocations.", "response": "def segment_allocation_find(context, lock_mode=False, **filters):\n \"\"\"Query for segment allocations.\"\"\"\n range_ids = filters.pop(\"segment_allocation_range_ids\", None)\n\n query = context.session.query(models.SegmentAllocation)\n if lock_mode:\n query = query.with_lockmode(\"update\")\n\n query = query.filter_by(**filters)\n\n # Optionally filter by given list of range ids\n if range_ids:\n query.filter(\n models.SegmentAllocation.segment_allocation_range_id.in_(\n range_ids))\n return query"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend a command to Niko Home Control and returns the output of the system.", "response": "def send(self, s):\n \"\"\"\n Sends the given command to Niko Home Control and returns the output of\n the system.\n\n Aliases: write, put, sendall, send_all\n \"\"\"\n self._socket.send(s.encode())\n return self.read()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef if_(*args):\n for i in range(0, len(args) - 1, 2):\n if args[i]:\n return args[i + 1]\n if len(args) % 2:\n return args[-1]\n else:\n return None", "response": "Implements the if operator with support for multiple elseif - s."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef soft_equals(a, b):\n if isinstance(a, str) or isinstance(b, str):\n return str(a) == str(b)\n if isinstance(a, bool) or isinstance(b, bool):\n return bool(a) is bool(b)\n return a == b", "response": "Implements the '==' operator which does type JS - style coertion."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nimplements the == operator.", "response": "def hard_equals(a, b):\n \"\"\"Implements the '===' operator.\"\"\"\n if type(a) != type(b):\n return False\n return a == b"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nimplements the '<' operator with JS - style type coertion.", "response": "def less(a, b, *args):\n \"\"\"Implements the '<' operator with JS-style type coertion.\"\"\"\n types = set([type(a), type(b)])\n if float in types or int in types:\n try:\n a, b = float(a), float(b)\n except TypeError:\n # NaN\n return False\n return a < b and (not args or less(b, *args))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef less_or_equal(a, b, *args):\n return (\n less(a, b) or soft_equals(a, b)\n ) and (not args or less_or_equal(b, *args))", "response": "Implements the '<=' operator with JS - style type coertion."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts a string either to int float or a string.", "response": "def to_numeric(arg):\n \"\"\"\n Converts a string either to int or to float.\n This is important, because e.g. {\"!==\": [{\"+\": \"0\"}, 0.0]}\n \"\"\"\n if isinstance(arg, str):\n if '.' in arg:\n return float(arg)\n else:\n return int(arg)\n return arg"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nimplement the merge operator for merging lists.", "response": "def merge(*args):\n \"\"\"Implements the 'merge' operator for merging lists.\"\"\"\n ret = []\n for arg in args:\n if isinstance(arg, list) or isinstance(arg, tuple):\n ret += list(arg)\n else:\n ret.append(arg)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting variable value from data dictionary.", "response": "def get_var(data, var_name, not_found=None):\n \"\"\"Gets variable value from data dictionary.\"\"\"\n try:\n for key in str(var_name).split('.'):\n try:\n data = data[key]\n except TypeError:\n data = data[int(key)]\n except (KeyError, TypeError, ValueError):\n return not_found\n else:\n return data"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef missing(data, *args):\n not_found = object()\n if args and isinstance(args[0], list):\n args = args[0]\n ret = []\n for arg in args:\n if get_var(data, arg, not_found) is not_found:\n ret.append(arg)\n return ret", "response": "Implements the missing operator for finding missing variables."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nimplement the missing_some operator for finding missing variables.", "response": "def missing_some(data, min_required, args):\n \"\"\"Implements the missing_some operator for finding missing variables.\"\"\"\n if min_required < 1:\n return []\n found = 0\n not_found = object()\n ret = []\n for arg in args:\n if get_var(data, arg, not_found) is not_found:\n ret.append(arg)\n else:\n found += 1\n if found >= min_required:\n return []\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes the json - logic with given data.", "response": "def jsonLogic(tests, data=None):\n \"\"\"Executes the json-logic with given data.\"\"\"\n # You've recursed to a primitive, stop!\n if tests is None or not isinstance(tests, dict):\n return tests\n\n data = data or {}\n\n operator = list(tests.keys())[0]\n values = tests[operator]\n\n # Easy syntax for unary operators, like {\"var\": \"x\"} instead of strict\n # {\"var\": [\"x\"]}\n if not isinstance(values, list) and not isinstance(values, tuple):\n values = [values]\n\n # Recursion!\n values = [jsonLogic(val, data) for val in values]\n\n if operator == 'var':\n return get_var(data, *values)\n if operator == 'missing':\n return missing(data, *values)\n if operator == 'missing_some':\n return missing_some(data, *values)\n\n if operator not in operations:\n raise ValueError(\"Unrecognized operation %s\" % operator)\n\n return operations[operator](*values)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nperform an indentation on the current line of text.", "response": "def indent(self):\n \"\"\"\n Performs an indentation\n \"\"\"\n if not self.tab_always_indent:\n super(PyIndenterMode, self).indent()\n else:\n cursor = self.editor.textCursor()\n assert isinstance(cursor, QtGui.QTextCursor)\n if cursor.hasSelection():\n self.indent_selection(cursor)\n else:\n # simply insert indentation at the cursor position\n tab_len = self.editor.tab_length\n cursor.beginEditBlock()\n if self.editor.use_spaces_instead_of_tabs:\n cursor.insertText(tab_len * \" \")\n else:\n cursor.insertText('\\t')\n cursor.endEditBlock()\n self.editor.setTextCursor(cursor)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nperforming an un - indentation on the current item.", "response": "def unindent(self):\n \"\"\"\n Performs an un-indentation\n \"\"\"\n if self.tab_always_indent:\n cursor = self.editor.textCursor()\n if not cursor.hasSelection():\n cursor.select(cursor.LineUnderCursor)\n self.unindent_selection(cursor)\n else:\n super(PyIndenterMode, self).unindent()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _handle_indent_between_paren(self, column, line, parent_impl, tc):\n pre, post = parent_impl\n next_char = self._get_next_char(tc)\n prev_char = self._get_prev_char(tc)\n prev_open = prev_char in ['[', '(', '{']\n next_close = next_char in [']', ')', '}']\n (open_line, open_symbol_col), (close_line, close_col) = \\\n self._get_paren_pos(tc, column)\n open_line_txt = self._helper.line_text(open_line)\n open_line_indent = len(open_line_txt) - len(open_line_txt.lstrip())\n if prev_open:\n post = (open_line_indent + self.editor.tab_length) * ' '\n elif next_close and prev_char != ',':\n post = open_line_indent * ' '\n elif tc.block().blockNumber() == open_line:\n post = open_symbol_col * ' '\n\n # adapt indent if cursor on closing line and next line have same\n # indent -> PEP8 compliance\n if close_line and close_col:\n txt = self._helper.line_text(close_line)\n bn = tc.block().blockNumber()\n flg = bn == close_line\n next_indent = self._helper.line_indent(bn + 1) * ' '\n if flg and txt.strip().endswith(':') and next_indent == post:\n # | look at how the previous line ( ``':'):`` ) was\n # over-indented, this is actually what we are trying to\n # achieve here\n post += self.editor.tab_length * ' '\n\n # breaking string\n if next_char in ['\"', \"'\"]:\n tc.movePosition(tc.Left)\n is_string = self._helper.is_comment_or_string(tc, formats=['string'])\n if next_char in ['\"', \"'\"]:\n tc.movePosition(tc.Right)\n if is_string:\n trav = QTextCursor(tc)\n while self._helper.is_comment_or_string(\n trav, formats=['string']):\n trav.movePosition(trav.Left)\n trav.movePosition(trav.Right)\n symbol = '%s' % self._get_next_char(trav)\n pre += symbol\n post += symbol\n\n return pre, post", "response": "Handle indentation between parentheses and braces."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _at_block_start(tc, line):\n if tc.atBlockStart():\n return True\n column = tc.columnNumber()\n indentation = len(line) - len(line.lstrip())\n return column <= indentation", "response": "Return True if the cursor is at the start of a block."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef detect_encoding(self, path):\n with open(path, 'rb') as file:\n source = file.read()\n # take care of line encodings (not in jedi)\n source = source.replace(b'\\r', b'')\n source_str = str(source).replace('\\\\n', '\\n')\n\n byte_mark = ast.literal_eval(r\"b'\\xef\\xbb\\xbf'\")\n if source.startswith(byte_mark):\n # UTF-8 byte-order mark\n return 'utf-8'\n\n first_two_lines = re.match(r'(?:[^\\n]*\\n){0,2}', source_str).group(0)\n possible_encoding = re.search(r\"coding[=:]\\s*([-\\w.]+)\",\n first_two_lines)\n if possible_encoding:\n return possible_encoding.group(1)\n return 'UTF-8'", "response": "Detect encoding of the current language of the current language of the current language."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalling when the mode is activated or deactivated", "response": "def on_state_changed(self, state):\n \"\"\"\n Called when the mode is activated/deactivated\n \"\"\"\n if state:\n self.action.triggered.connect(self.comment)\n self.editor.add_action(self.action, sub_menu='Python')\n if 'pyqt5' in os.environ['QT_API'].lower():\n self.editor.key_pressed.connect(self.on_key_pressed)\n else:\n self.editor.remove_action(self.action, sub_menu='Python')\n self.action.triggered.disconnect(self.comment)\n if 'pyqt5' in os.environ['QT_API'].lower():\n self.editor.key_pressed.disconnect(self.on_key_pressed)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncomment the selected lines or the current lines if there is no selection.", "response": "def comment(self):\n \"\"\"\n Comments/Uncomments the selected lines or the current lines if there\n is no selection.\n \"\"\"\n cursor = self.editor.textCursor()\n # get the indent at which comment should be inserted and whether to\n # comment or uncomment the selected text\n indent, comment, nb_lines = self.get_operation()\n has_selection = cursor.hasSelection()\n if nb_lines > 1:\n self._move_cursor_to_selection_start(cursor)\n cursor.beginEditBlock()\n for i in range(nb_lines):\n self.comment_line(indent, cursor, comment)\n cursor.movePosition(cursor.NextBlock)\n cursor.endEditBlock()\n else:\n # comment a single line\n cursor.beginEditBlock()\n self.comment_line(indent, cursor, comment)\n if not has_selection:\n # move to the first non-whitespace character of the next line\n cursor.movePosition(cursor.NextBlock)\n text = cursor.block().text()\n indent = len(text) - len(text.lstrip())\n cursor.movePosition(cursor.Right, cursor.MoveAnchor, indent)\n cursor.endEditBlock()\n self.editor.setTextCursor(cursor)\n else:\n cursor.endEditBlock()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef setPlainText(self, txt, mimetype='text/x-python', encoding='utf-8'):\n try:\n self.syntax_highlighter.docstrings[:] = []\n self.syntax_highlighter.import_statements[:] = []\n except AttributeError:\n pass\n super(PyCodeEditBase, self).setPlainText(txt, mimetype, encoding)", "response": "Extends QCodeEdit. setPlainText to allow user to set plain text without mimetype."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_terminal_colors(self):\n self.color_scheme = self.create_color_scheme(\n background=self.syntax_highlighter.color_scheme.background,\n foreground=self.syntax_highlighter.color_scheme.formats['normal'].foreground().color())", "response": "Update terminal colors based on pygments color scheme colors\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef mouseMoveEvent(self, e):\n super(PyInteractiveConsole, self).mouseMoveEvent(e)\n cursor = self.cursorForPosition(e.pos())\n assert isinstance(cursor, QtGui.QTextCursor)\n p = cursor.positionInBlock()\n usd = cursor.block().userData()\n if usd and usd.start_pos_in_block <= p <= usd.end_pos_in_block:\n if QtWidgets.QApplication.overrideCursor() is None:\n QtWidgets.QApplication.setOverrideCursor(\n QtGui.QCursor(QtCore.Qt.PointingHandCursor))\n else:\n if QtWidgets.QApplication.overrideCursor() is not None:\n QtWidgets.QApplication.restoreOverrideCursor()", "response": "Overrides mouseMoveEvent to display a pointing hand cursor when the mouse cursor is over a file location"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef mousePressEvent(self, e):\n super(PyInteractiveConsole, self).mousePressEvent(e)\n cursor = self.cursorForPosition(e.pos())\n p = cursor.positionInBlock()\n usd = cursor.block().userData()\n if usd and usd.start_pos_in_block <= p <= usd.end_pos_in_block:\n if e.button() == QtCore.Qt.LeftButton:\n self.open_file_requested.emit(usd.filename, usd.line)", "response": "Emits open_file_requested if the press event occured over\n a file location string."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef detect_fold_level(self, prev_block, block):\n # Python is an indent based language so use indentation for folding\n # makes sense but we restrict new regions to indentation after a ':',\n # that way only the real logical blocks are displayed.\n lvl = super(PythonFoldDetector, self).detect_fold_level(\n prev_block, block)\n # cancel false indentation, indentation can only happen if there is\n # ':' on the previous line\n prev_lvl = TextBlockHelper.get_fold_lvl(prev_block)\n if prev_block and lvl > prev_lvl and not (\n self._strip_comments(prev_block).endswith(':')):\n lvl = prev_lvl\n lvl = self._handle_docstrings(block, lvl, prev_block)\n lvl = self._handle_imports(block, lvl, prev_block)\n return lvl", "response": "Detects the fold level of the current block."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef setup_actions(self):\n self.actionOpen.triggered.connect(self.on_open)\n self.actionNew.triggered.connect(self.on_new)\n self.actionSave.triggered.connect(self.on_save)\n self.actionSave_as.triggered.connect(self.on_save_as)\n self.actionQuit.triggered.connect(\n QtWidgets.QApplication.instance().quit)\n self.tabWidget.current_changed.connect(self.on_current_tab_changed)\n self.tabWidget.last_tab_closed.connect(self.on_last_tab_closed)\n self.actionAbout.triggered.connect(self.on_about)\n self.actionRun.triggered.connect(self.on_run)\n self.interactiveConsole.process_finished.connect(\n self.on_process_finished)\n self.actionConfigure_run.triggered.connect(self.on_configure_run)", "response": "Connects the slots to signals"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef open_file(self, path, line=None):\n editor = None\n if path:\n interpreter, pyserver, args = self._get_backend_parameters()\n editor = self.tabWidget.open_document(\n path, None, interpreter=interpreter, server_script=pyserver,\n args=args)\n if editor:\n self.setup_editor(editor)\n self.recent_files_manager.open_file(path)\n self.menu_recents.update_actions()\n if line is not None:\n TextHelper(self.tabWidget.current_widget()).goto_line(line)\n return editor", "response": "Opens a file and adds it to the menu."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_backend_parameters(self):\n frozen = hasattr(sys, 'frozen')\n interpreter = Settings().interpreter\n if frozen:\n interpreter = None\n pyserver = server.__file__ if interpreter is not None else 'server.exe'\n args = []\n return interpreter, pyserver, args", "response": "Gets the interpreter pyserver and args for the backend."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a new empty code editor to the tab widget", "response": "def on_new(self):\n \"\"\"\n Add a new empty code editor to the tab widget\n \"\"\"\n interpreter, pyserver, args = self._get_backend_parameters()\n self.setup_editor(self.tabWidget.create_new_document(\n extension='.py', interpreter=interpreter, server_script=pyserver,\n args=args))\n self.actionRun.setDisabled(True)\n self.actionConfigure_run.setDisabled(True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nopening file dialog and open the file if the dialog was accepted.", "response": "def on_open(self):\n \"\"\"\n Shows an open file dialog and open the file if the dialog was\n accepted.\n\n \"\"\"\n filename, filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Open')\n if filename:\n self.open_file(filename)\n self.actionRun.setEnabled(True)\n self.actionConfigure_run.setEnabled(True)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef on_save_as(self):\n path = self.tabWidget.current_widget().file.path\n path = os.path.dirname(path) if path else ''\n filename, filter = QtWidgets.QFileDialog.getSaveFileName(\n self, 'Save', path)\n if filename:\n self.tabWidget.save_current(filename)\n self.recent_files_manager.open_file(filename)\n self.menu_recents.update_actions()\n self.actionRun.setEnabled(True)\n self.actionConfigure_run.setEnabled(True)\n self._update_status_bar(self.tabWidget.current_widget())", "response": "Save the current editor document as."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef setup_mnu_panels(self, editor):\n for panel in editor.panels:\n if panel.dynamic:\n continue\n a = QtWidgets.QAction(self.menuModes)\n a.setText(panel.name)\n a.setCheckable(True)\n a.setChecked(panel.enabled)\n a.changed.connect(self.on_panel_state_changed)\n a.panel = weakref.proxy(panel)\n self.menuPanels.addAction(a)", "response": "Setup the panels menu for the current editor."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef on_current_tab_changed(self):\n self.menuEdit.clear()\n self.menuModes.clear()\n self.menuPanels.clear()\n editor = self.tabWidget.current_widget()\n self.menuEdit.setEnabled(editor is not None)\n self.menuModes.setEnabled(editor is not None)\n self.menuPanels.setEnabled(editor is not None)\n self.actionSave.setEnabled(editor is not None)\n self.actionSave_as.setEnabled(editor is not None)\n self.actionConfigure_run.setEnabled(editor is not None)\n self.actionRun.setEnabled(editor is not None)\n if editor is not None:\n self.setup_mnu_edit(editor)\n self.setup_mnu_modes(editor)\n self.setup_mnu_panels(editor)\n self.widgetOutline.set_editor(editor)\n self._update_status_bar(editor)", "response": "Update action states when the current tab has changed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef on_run(self):\n filename = self.tabWidget.current_widget().file.path\n wd = os.path.dirname(filename)\n args = Settings().get_run_config_for_file(filename)\n self.interactiveConsole.start_process(\n Settings().interpreter, args=[filename] + args, cwd=wd)\n self.dockWidget.show()\n self.actionRun.setEnabled(False)\n self.actionConfigure_run.setEnabled(False)", "response": "Run the current script"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef on_goto_out_of_doc(self, assignment):\n editor = self.open_file(assignment.module_path)\n if editor:\n TextHelper(editor).goto_line(assignment.line, assignment.column)", "response": "Open a new tab when goto goes out of the current document."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngoes to assignements worker.", "response": "def goto_assignments(request_data):\n \"\"\"\n Go to assignements worker.\n \"\"\"\n code = request_data['code']\n line = request_data['line'] + 1\n column = request_data['column']\n path = request_data['path']\n # encoding = request_data['encoding']\n encoding = 'utf-8'\n script = jedi.Script(code, line, column, path, encoding)\n try:\n definitions = script.goto_assignments()\n except jedi.NotFoundError:\n pass\n else:\n ret_val = [(d.module_path, d.line - 1 if d.line else None,\n d.column, d.full_name)\n for d in definitions]\n return ret_val"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef defined_names(request_data):\n global _old_definitions\n ret_val = []\n path = request_data['path']\n toplvl_definitions = jedi.names(\n request_data['code'], path, 'utf-8')\n for d in toplvl_definitions:\n definition = _extract_def(d, path)\n if d.type != 'import':\n ret_val.append(definition)\n ret_val = [d.to_dict() for d in ret_val]\n return ret_val", "response": "Returns the list of defined names for the document."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef quick_doc(request_data):\n code = request_data['code']\n line = request_data['line'] + 1\n column = request_data['column']\n path = request_data['path']\n # encoding = 'utf-8'\n encoding = 'utf-8'\n script = jedi.Script(code, line, column, path, encoding)\n try:\n definitions = script.goto_definitions()\n except jedi.NotFoundError:\n return []\n else:\n ret_val = [d.docstring() for d in definitions]\n return ret_val", "response": "Worker that returns the documentation of the symbol under cursor."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run_pep8(request_data):\n import pycodestyle\n from pyqode.python.backend.pep8utils import CustomChecker\n WARNING = 1\n code = request_data['code']\n path = request_data['path']\n max_line_length = request_data['max_line_length']\n ignore_rules = request_data['ignore_rules']\n ignore_rules += ['W291', 'W292', 'W293', 'W391']\n pycodestyle.MAX_LINE_LENGTH = max_line_length\n # setup our custom style guide with our custom checker which returns a list\n # of strings instread of spitting the results at stdout\n pep8style = pycodestyle.StyleGuide(parse_argv=False, config_file='',\n checker_class=CustomChecker)\n try:\n results = pep8style.input_file(path, lines=code.splitlines(True))\n except Exception:\n _logger().exception('Failed to run PEP8 analysis with data=%r'\n % request_data)\n return []\n else:\n messages = []\n for line_number, offset, code, text, doc in results:\n if code in ignore_rules:\n continue\n messages.append(('[PEP8] %s: %s' % (code, text), WARNING,\n line_number - 1))\n return messages", "response": "This function runs the PEP8 tool on the current editor text and returns a list of tuples."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the icon resource filename that corresponds to the given typename.", "response": "def icon_from_typename(name, icon_type):\n \"\"\"\n Returns the icon resource filename that corresponds to the given typename.\n\n :param name: name of the completion. Use to make the distinction between\n public and private completions (using the count of starting '_')\n :pram typename: the typename reported by jedi\n\n :returns: The associate icon resource filename or None.\n \"\"\"\n ICONS = {\n 'CLASS': ICON_CLASS,\n 'IMPORT': ICON_NAMESPACE,\n 'STATEMENT': ICON_VAR,\n 'FORFLOW': ICON_VAR,\n 'FORSTMT': ICON_VAR,\n 'WITHSTMT': ICON_VAR,\n 'GLOBALSTMT': ICON_VAR,\n 'MODULE': ICON_NAMESPACE,\n 'KEYWORD': ICON_KEYWORD,\n 'PARAM': ICON_VAR,\n 'ARRAY': ICON_VAR,\n 'INSTANCEELEMENT': ICON_VAR,\n 'INSTANCE': ICON_VAR,\n 'PARAM-PRIV': ICON_VAR,\n 'PARAM-PROT': ICON_VAR,\n 'FUNCTION': ICON_FUNC,\n 'DEF': ICON_FUNC,\n 'FUNCTION-PRIV': ICON_FUNC_PRIVATE,\n 'FUNCTION-PROT': ICON_FUNC_PROTECTED\n }\n ret_val = None\n icon_type = icon_type.upper()\n # jedi 0.8 introduced NamedPart class, which have a string instead of being\n # one\n if hasattr(name, \"string\"):\n name = name.string\n if icon_type == \"FORFLOW\" or icon_type == \"STATEMENT\":\n icon_type = \"PARAM\"\n if icon_type == \"PARAM\" or icon_type == \"FUNCTION\":\n if name.startswith(\"__\"):\n icon_type += \"-PRIV\"\n elif name.startswith(\"_\"):\n icon_type += \"-PROT\"\n if icon_type in ICONS:\n ret_val = ICONS[icon_type]\n elif icon_type:\n _logger().warning(\"Unimplemented completion icon_type: %s\", icon_type)\n return ret_val"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncomplete python code using jedi.", "response": "def complete(code, line, column, path, encoding, prefix):\n \"\"\"\n Completes python code using `jedi`_.\n\n :returns: a list of completion.\n \"\"\"\n ret_val = []\n try:\n script = jedi.Script(code, line + 1, column, path, encoding)\n completions = script.completions()\n print('completions: %r' % completions)\n except jedi.NotFoundError:\n completions = []\n for completion in completions:\n ret_val.append({\n 'name': completion.name,\n 'icon': icon_from_typename(\n completion.name, completion.type),\n 'tooltip': completion.description})\n return ret_val"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrequesting a go to the assignment.", "response": "def _check_word_cursor(self, tc=None):\n \"\"\"\n Request a go to assignment.\n\n :param tc: Text cursor which contains the text that we must look for\n its assignment. Can be None to go to the text that is under\n the text cursor.\n :type tc: QtGui.QTextCursor\n \"\"\"\n if not tc:\n tc = TextHelper(self.editor).word_under_cursor()\n\n request_data = {\n 'code': self.editor.toPlainText(),\n 'line': tc.blockNumber(),\n 'column': tc.columnNumber(),\n 'path': self.editor.file.path,\n 'encoding': self.editor.file.encoding\n }\n try:\n self.editor.backend.send_request(\n workers.goto_assignments, request_data,\n on_receive=self._on_results_available)\n except NotRunning:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _unique(self, seq):\n # order preserving\n checked = []\n for e in seq:\n present = False\n for c in checked:\n if str(c) == str(e):\n present = True\n break\n if not present:\n checked.append(e)\n return checked", "response": "Returns a list of unique items in the sequence."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nvalidate the list of directives.", "response": "def _validateDirectives(self, directiveList, checkFileName):\n\n if len(directiveList) == 0:\n raise ParsingException(\"'{file}' does not contain any CHECK directives\".format(file=checkFileName))\n\n from . import Directives\n \"\"\"\n We should enforce for every CHECK-NOT and CHECK-NOT-L directive that the next directive (if it exists) is\n a CHECK or CHECK-L directive\n \"\"\"\n last = len(directiveList) -1\n supportedDirectives = [ Directives.Check, Directives.CheckLiteral ]\n for (index,directive) in enumerate(directiveList):\n if isA(directive, [Directives.CheckNot, Directives.CheckNotLiteral]):\n if index < last:\n after = directiveList[index +1]\n if not isA(after, supportedDirectives):\n requiredTypes = \" or \".join( [ \"CHECK{suffix}\".format(suffix=d.directiveToken()) for d in supportedDirectives])\n raise ParsingException(\"{directive} must have a {requiredTypes} directive after it instead of a {bad}\".format(\n directive=directive,\n requiredTypes=requiredTypes,\n check=Directives.Check.directiveToken(),\n bad=after)\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _substituteCheckPattern(self, inputString, lineNumber, lastLineNumber, checkFileName, isForRegex):\n assert isinstance(inputString, str)\n assert isinstance(lineNumber, int)\n assert isinstance(lastLineNumber, int)\n assert isinstance(checkFileName, str)\n\n \"\"\"\n Do ${LINE}, ${LINE:+N}, and ${LINE:-N} substitutions.\n To escape prepend with slash\n \"\"\"\n sPattern = r'\\$\\{LINE(\\:(?P\\+|-)(?P\\d+))?\\}'\n matcher = re.compile(sPattern)\n result = \"\"\n loop = True\n start = 0\n end = len(inputString) # Not inclusive\n while loop:\n m = matcher.search(inputString, start, end)\n if not m:\n # No match so copy verbatim\n _logger.debug('Result is currently \"{}\"'.format(result))\n result += inputString[start:end]\n break # And we're done :)\n else:\n prevIndex = max(0, m.start() -1)\n _logger.debug('Previous character before match is at index {index} \"{char}\"'.format(index=prevIndex, char=inputString[prevIndex]))\n if inputString[prevIndex] == \"\\\\\":\n # User asked to escape\n _logger.debug('Substitution is escaped')\n _logger.debug('Result is currently \"{}\"'.format(result))\n result += inputString[start:prevIndex] # Copy before escaping character\n _logger.debug('Result is currently \"{}\"'.format(result))\n result += inputString[(prevIndex+1):m.end()] # Copy the ${LINE..} verbatim\n start = min(m.end(), end)\n _logger.debug('Result is currently \"{}\"'.format(result))\n _logger.debug('Next search is {start}:{end} = \"{ss}\"'.format(start=start, end=end, ss=inputString[start:end]))\n else:\n _logger.debug('Result is currently \"{}\"'.format(result))\n _logger.debug('Doing subsitution. Found at {begin}:{end} = {ss}'.format(begin=m.start(),end=m.end(), ss=inputString[m.start():m.end()]))\n result += inputString[start:m.start()] # Copy before substitution starts\n\n if m.groupdict()['sign'] == None:\n # No offset just substitute line number\n _logger.debug('No offset')\n result += str(lineNumber)\n else:\n offset = 1 if m.groupdict()['sign'] == '+' else -1\n offset *= int(m.groupdict()['offset'])\n _logger.debug('Offset is {}'.format(offset))\n\n requestedLineNumber = lineNumber + offset\n _logger.debug('Request line number to print is {}'.format(requestedLineNumber))\n\n if requestedLineNumber <= 0:\n raise ParsingException('{file}:{line}:{col} offset gives line number < 1'.format(file=checkFileName, line=lineNumber, col=m.start()))\n elif requestedLineNumber > lastLineNumber:\n raise ParsingException('{file}:{line}:{col} offset gives line number past the end of file'.format(file=checkFileName, line=lineNumber, col=m.start()))\n\n result += str(requestedLineNumber)\n\n start = min(m.end(),end)\n _logger.debug('Next search is {start}:{end} = \"{ss}\"'.format(start=start, end=end, ss=inputString[start:end]))\n\n \"\"\"\n Do simple ${...} substitutions\n \"\"\"\n\n\n # Do ${CHECKFILE_NAME} substitution\n basenameCheckFileName = os.path.basename(checkFileName)\n assert basenameCheckFileName.count('\\\\') == 0\n result = self._simpleSubstitution(\"CHECKFILE_NAME\", basenameCheckFileName, result)\n\n # Do ${CHECKFILE_ABS_PATH} substitution\n abspathCheckFileName = os.path.abspath(checkFileName)\n if isForRegex:\n # Note slash substitution is for Windows paths (e.g. \"c:\\mything\\foo.txt\") which can break regexes if we don't\n # correctly escape them.\n abspathCheckFileName = abspathCheckFileName.replace('\\\\', '\\\\\\\\')\n\n result = self._simpleSubstitution(\"CHECKFILE_ABS_PATH\", abspathCheckFileName, result)\n\n assert len(result) != 0\n return result", "response": "Substitute checkPattern in the inputString."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_metafile(bgen_filepath, metafile_filepath, verbose=True):\n if verbose:\n verbose = 1\n else:\n verbose = 0\n\n bgen_filepath = make_sure_bytes(bgen_filepath)\n metafile_filepath = make_sure_bytes(metafile_filepath)\n\n assert_file_exist(bgen_filepath)\n assert_file_readable(bgen_filepath)\n\n if exists(metafile_filepath):\n raise ValueError(f\"The file {metafile_filepath} already exists.\")\n\n with bgen_file(bgen_filepath) as bgen:\n nparts = _estimate_best_npartitions(lib.bgen_nvariants(bgen))\n metafile = lib.bgen_create_metafile(bgen, metafile_filepath, nparts, verbose)\n if metafile == ffi.NULL:\n raise RuntimeError(f\"Error while creating metafile: {metafile_filepath}.\")\n\n if lib.bgen_close_metafile(metafile) != 0:\n raise RuntimeError(f\"Error while closing metafile: {metafile_filepath}.\")", "response": "r Create variants metadata file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef match(self, subsetLines, offsetOfSubset, fileName):\n\n for (offset,l) in enumerate(subsetLines):\n column = l.find(self.literal)\n if column != -1:\n truePosition = offset + offsetOfSubset\n _logger.debug('Found match on line {}, col {}'.format(str(truePosition+ 1), column))\n _logger.debug('Line is {}'.format(l))\n self.matchLocation = CheckFileParser.FileLocation(fileName, truePosition +1)\n return truePosition\n\n # No Match found\n self.failed = True\n raise DirectiveException(self)", "response": "Match the line with the literal in the line that matches the pattern."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsearch through lines for match. A match is found.", "response": "def match(self, subsetLines, offsetOfSubset, fileName):\n \"\"\"\n Search through lines for match.\n Raise an Exception if a match\n \"\"\"\n for (offset,l) in enumerate(subsetLines):\n for t in self.regex:\n m = t.Regex.search(l)\n if m != None:\n truePosition = offset + offsetOfSubset\n _logger.debug('Found match on line {}'.format(str(truePosition+ 1)))\n _logger.debug('Line is {}'.format(l))\n self.failed = True\n self.matchLocation = CheckFileParser.FileLocation(fileName, truePosition +1)\n raise DirectiveException(self)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef isA(instance, typeList):\n return any(map(lambda iType: isinstance(instance,iType), typeList))", "response": "Return True if instance is an instance of any of the Directive\n types in typeList."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntouching a file. Taxonomy Credits to .", "response": "def _touch(fname, mode=0o666, dir_fd=None, **kwargs):\n \"\"\" Touch a file.\n\n Credits to .\n \"\"\"\n flags = os.O_CREAT | os.O_APPEND\n with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:\n os.utime(\n f.fileno() if os.utime in os.supports_fd else fname,\n dir_fd=None if os.supports_fd else dir_fd,\n **kwargs,\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntry to infer the correct library name.", "response": "def find_libname(self, name):\n \"\"\"Try to infer the correct library name.\"\"\"\n names = [\"{}.lib\", \"lib{}.lib\", \"{}lib.lib\"]\n names = [n.format(name) for n in names]\n dirs = self.get_library_dirs()\n for d in dirs:\n for n in names:\n if exists(join(d, n)):\n return n[:-4]\n msg = \"Could not find the {} library.\".format(name)\n raise ValueError(msg)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef split(self, X, y=None, groups=None):\n X, y, groups = indexable(X, y, groups)\n cgrs = [~r for r in X]\n\n structure_condition = defaultdict(set)\n\n for structure, condition in zip(cgrs, groups):\n structure_condition[structure].add(condition)\n\n train_data = defaultdict(list)\n test_data = []\n\n for n, (structure, condition) in enumerate(zip(cgrs, groups)):\n train_data[condition].append(n)\n if len(structure_condition[structure]) > 1:\n test_data.append(n)\n\n for condition, indexes in train_data.items():\n test_index = [index for index in indexes if index in test_data]\n if test_index:\n train_index = [i for cond, ind in train_data.items() if cond != condition for i in ind]\n yield array(train_index), array(test_index)", "response": "Generates indices to split data into training and test sets."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef molconvert_chemaxon(data):\n if isinstance(data, Path):\n with data.open('rb') as f:\n data = f.read()\n elif isinstance(data, StringIO):\n data = data.read().encode()\n elif isinstance(data, BytesIO):\n data = data.read()\n elif hasattr(data, 'read'): # check if data is open(filename, mode)\n data = data.read()\n if isinstance(data, str):\n data = data.encode()\n elif isinstance(data, str):\n data = data.encode()\n elif not isinstance(data, bytes):\n raise ValueError('invalid input')\n\n try:\n p = run(['molconvert', '-g', 'mrv'], input=data, stdout=PIPE)\n except FileNotFoundError as e:\n raise ConfigurationError from e\n\n if p.returncode != 0:\n raise ConfigurationError(p.stderr.decode())\n\n with BytesIO(p.stdout) as f, MRVread(f) as r:\n return iter2array(r)", "response": "molconvert wrapper for the chemaxon file atomconvert wrapper"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfitting distance - based AD.", "response": "def fit(self, X, y=None):\n \"\"\"Fit distance-based AD.\n\n Parameters\n ----------\n X : array-like or sparse matrix, shape (n_samples, n_features)\n The input samples. Use ``dtype=np.float32`` for maximum\n efficiency.\n\n Returns\n -------\n self : object\n Returns self.\n \"\"\"\n # Check data\n X = check_array(X)\n self.tree = BallTree(X, leaf_size=self.leaf_size, metric=self.metric)\n dist_train = self.tree.query(X, k=2)[0]\n if self.threshold == 'auto':\n self.threshold_value = 0.5 * sqrt(var(dist_train[:, 1])) + mean(dist_train[:, 1])\n elif self.threshold == 'cv':\n if y is None:\n raise ValueError(\"Y must be specified to find the optimal threshold.\")\n y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)\n self.threshold_value = 0\n score = 0\n Y_pred, Y_true, AD = [], [], []\n cv = KFold(n_splits=5, random_state=1, shuffle=True)\n for train_index, test_index in cv.split(X):\n x_train = safe_indexing(X, train_index)\n x_test = safe_indexing(X, test_index)\n y_train = safe_indexing(y, train_index)\n y_test = safe_indexing(y, test_index)\n data_test = safe_indexing(dist_train[:, 1], test_index)\n if self.reg_model is None:\n reg_model = RandomForestRegressor(n_estimators=500, random_state=1).fit(x_train, y_train)\n else:\n reg_model = clone(self.reg_model).fit(x_train, y_train)\n Y_pred.append(reg_model.predict(x_test))\n Y_true.append(y_test)\n AD.append(data_test)\n AD_ = unique(hstack(AD))\n for z in AD_:\n AD_new = hstack(AD) <= z\n if self.score == 'ba_ad':\n val = balanced_accuracy_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)\n elif self.score == 'rmse_ad':\n val = rmse_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)\n if val >= score:\n score = val\n self.threshold_value = z\n else:\n self.threshold_value = self.threshold\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef predict_proba(self, X):\n # Check is fit had been called\n check_is_fitted(self, ['tree'])\n # Check data\n X = check_array(X)\n return self.tree.query(X)[0].flatten()", "response": "Returns the value of the nearest neighbor from the training set."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef predict(self, X):\n # Check is fit had been called\n check_is_fitted(self, ['tree'])\n # Check data\n X = check_array(X)\n return self.tree.query(X)[0].flatten() <= self.threshold_value", "response": "Predict if a particular sample is an outlier or not."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfitting the regression model on the set of classes.", "response": "def fit(self, X, y=None):\n \"\"\"Learning is to find the inverse matrix for X and calculate the threshold.\n\n Parameters\n ----------\n X : array-like or sparse matrix, shape (n_samples, n_features)\n The input samples. Use ``dtype=np.float32`` for maximum\n efficiency.\n y : array-like, shape = [n_samples] or [n_samples, n_outputs]\n The target values (real numbers in regression).\n\n Returns\n -------\n self : object\n \"\"\"\n # Check that X have correct shape\n X = check_array(X)\n self.inverse_influence_matrix = self.__make_inverse_matrix(X)\n if self.threshold == 'auto':\n self.threshold_value = 3 * (1 + X.shape[1]) / X.shape[0]\n elif self.threshold == 'cv':\n if y is None:\n raise ValueError(\"Y must be specified to find the optimal threshold.\")\n y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)\n self.threshold_value = 0\n score = 0\n Y_pred, Y_true, AD = [], [], []\n cv = KFold(n_splits=5, random_state=1, shuffle=True)\n for train_index, test_index in cv.split(X):\n x_train = safe_indexing(X, train_index)\n x_test = safe_indexing(X, test_index)\n y_train = safe_indexing(y, train_index)\n y_test = safe_indexing(y, test_index)\n if self.reg_model is None:\n reg_model = RandomForestRegressor(n_estimators=500, random_state=1).fit(x_train, y_train)\n else:\n reg_model = clone(self.reg_model).fit(x_train, y_train)\n Y_pred.append(reg_model.predict(x_test))\n Y_true.append(y_test)\n ad_model = self.__make_inverse_matrix(x_train)\n AD.append(self.__find_leverages(x_test, ad_model))\n AD_ = unique(hstack(AD))\n for z in AD_:\n AD_new = hstack(AD) <= z\n if self.score == 'ba_ad':\n val = balanced_accuracy_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)\n elif self.score == 'rmse_ad':\n val = rmse_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)\n if val >= score:\n score = val\n self.threshold_value = z\n else:\n self.threshold_value = self.threshold\n return self"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef predict_proba(self, X):\n # Check is fit had been called\n check_is_fitted(self, ['inverse_influence_matrix'])\n # Check that X have correct shape\n X = check_array(X)\n return self.__find_leverages(X, self.inverse_influence_matrix)", "response": "Predict the distances for X to the center of the training set."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef predict(self, X):\n # Check is fit had been called\n check_is_fitted(self, ['inverse_influence_matrix'])\n # Check that X have correct shape\n X = check_array(X)\n return self.__find_leverages(X, self.inverse_influence_matrix) <= self.threshold_value", "response": "Predicts inside or outside AD for X."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_feature_names(self):\n return ['temperature', 'pressure'] + [f'solvent.{x}' for x in range(1, self.max_solvents + 1)] + \\\n [f'solvent_amount.{x}' for x in range(1, self.max_solvents + 1)]", "response": "Get feature names.\n\n Returns\n -------\n feature_names : list of strings\n Names of the features produced by transform."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfitting the object to the given data.", "response": "def fit(self, X, y=None):\n \"\"\"Find min and max values of every feature.\n\n Parameters\n ----------\n X : {array-like, sparse matrix}, shape (n_samples, n_features)\n The training input samples.\n y : Ignored\n not used, present for API consistency by convention.\n\n Returns\n -------\n self : object\n \"\"\"\n # Check that X have correct shape\n X = check_array(X)\n\n self._x_min = X.min(axis=0) # axis=0 will find the minimum values \u200b\u200bby columns (for each feature)\n self._x_max = X.max(axis=0) # axis=0 will find the minimum values \u200b\u200bby columns (for each feature)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef predict(self, X):\n # Check is fit had been called\n check_is_fitted(self, ['_x_min', '_x_max'])\n\n # Input validation\n X = check_array(X)\n return ((X - self._x_min).min(axis=1) >= 0) & ((self._x_max - X).min(axis=1) >= 0)", "response": "Predict if a particular sample is an outlier or not."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates indices to split the data into training and test sets.", "response": "def split(self, X, y=None, groups=None):\n \"\"\"Generate indices to split data into training and test set.\n Parameters\n ----------\n X : array-like, of length n_samples\n Training data, includes reaction's containers\n y : array-like, of length n_samples\n The target variable for supervised learning problems.\n groups : array-like, with shape (n_samples,)\n Group labels for the samples used while splitting the dataset into\n train/test set.\n Yields\n ------\n train : ndarray\n The training set indices for that split.\n test : ndarray\n The testing set indices for that split.\n \"\"\"\n X, y, groups = indexable(X, y, groups)\n cgrs = [~r for r in X]\n\n condition_structure = defaultdict(set)\n\n for structure, condition in zip(cgrs, groups):\n condition_structure[condition].add(structure)\n\n train_data = defaultdict(list)\n test_data = []\n\n for n, (structure, condition) in enumerate(zip(cgrs, groups)):\n train_data[structure].append(n)\n if len(condition_structure[condition]) > 1:\n test_data.append(n)\n\n if self.n_splits > len(train_data):\n raise ValueError(\"Cannot have number of splits n_splits=%d greater\"\n \" than the number of transformations: %d.\"\n % (self.n_splits, len(train_data)))\n\n structures_weight = sorted(((x, len(y)) for x, y in train_data.items()), key=lambda x: x[1], reverse=True)\n fold_mean_size = len(cgrs) // self.n_splits\n\n if structures_weight[0][1] > fold_mean_size:\n warning('You have transformation that greater fold size')\n\n for idx in range(self.n_repeats):\n train_folds = [[] for _ in range(self.n_splits)]\n for structure, structure_length in structures_weight:\n if self.shuffle:\n check_random_state(self.random_state).shuffle(train_folds)\n for fold in train_folds[:-1]:\n if len(fold) + structure_length <= fold_mean_size:\n fold.extend(train_data[structure])\n break\n else:\n roulette_param = (structure_length - fold_mean_size + len(fold)) / structure_length\n if random() > roulette_param:\n fold.extend(train_data[structure])\n break\n else:\n train_folds[-1].extend(train_data[structure])\n\n test_folds = [[] for _ in range(self.n_splits)]\n for test, train in zip(test_folds, train_folds):\n for index in train:\n if index in test_data:\n test.append(index)\n\n for i in range(self.n_splits):\n train_index = []\n for fold in train_folds[:i]:\n train_index.extend(fold)\n for fold in train_folds[i+1:]:\n train_index.extend(fold)\n test_index = test_folds[i]\n yield array(train_index), array(test_index)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndoing nothing and return the estimator unchanged", "response": "def fit(self, x, y=None):\n \"\"\"Do nothing and return the estimator unchanged\n\n This method is just there to implement the usual API and hence work in pipelines.\n \"\"\"\n if self._dtype is not None:\n iter2array(x, dtype=self._dtype)\n else:\n iter2array(x)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef finalize(self):\n if self.__head_less:\n warn(f'{self.__class__.__name__} configured to head less mode. finalize unusable')\n elif not self.__head_generate:\n warn(f'{self.__class__.__name__} already finalized or fitted')\n elif not self.__head_dict:\n raise NotFittedError(f'{self.__class__.__name__} instance is not fitted yet')\n else:\n if self.remove_rare_ratio:\n self.__clean_head(*self.__head_rare)\n self.__prepare_header()\n self.__head_rare = None\n self.__head_generate = False", "response": "finalize partial fitting procedure"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _reset(self):\n if not self.__head_less:\n if not self.__head_generate:\n self.__head_generate = True\n if self.__head_dict:\n self.__head_dump = self.__head_dict = None\n if self.__head_rare is not None:\n self.__head_rare = None\n\n self.delete_work_path()", "response": "Reset internal data - dependent state."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_feature_names(self):\n if self.__head_less:\n raise AttributeError(f'{self.__class__.__name__} instance configured to head less mode')\n elif not self.__head_dict:\n raise NotFittedError(f'{self.__class__.__name__} instance is not fitted yet')\n return list(self.__head_dict.values())", "response": "Get feature names.\n\n Returns\n -------\n feature_names : list of strings\n Names of the features produced by transform."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef fit(self, X):\n X = iter2array(X, dtype=ReactionContainer)\n self._train_signatures = {self.__get_signature(x) for x in X}\n return self", "response": "Fit structure - based AD. The training model memorizes the unique set of reaction signatures."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\npredicting reaction for given set of training sets.", "response": "def predict(self, X):\n \"\"\"Reaction is considered belonging to model\u2019s AD\n if its reaction signature coincides with ones used in training set.\n\n Parameters\n ----------\n X : after read rdf file\n\n Returns\n -------\n self : array contains True (reaction in AD) and False (reaction residing outside AD).\n \"\"\"\n check_is_fitted(self, ['_train_signatures'])\n X = iter2array(X, dtype=ReactionContainer)\n return array([self.__get_signature(x) in self._train_signatures for x in X])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse an expression into a list of identifiers and tokens", "response": "def __parser(expression):\n \"\"\" adopted from Paul McGuire example. http://pyparsing.wikispaces.com/file/view/fourFn.py\n \"\"\"\n expr_stack = []\n\n def push_first(strg, loc, toks):\n expr_stack.append(toks[0])\n\n def push_u_minus(strg, loc, toks):\n if toks and toks[0] == '-':\n expr_stack.append('unary -')\n\n point = Literal('.')\n _e = CaselessLiteral('E')\n fnumber = Combine(Word('+-' + nums, nums) +\n Optional(point + Optional(Word(nums))) +\n Optional(_e + Word('+-' + nums, nums)))\n ident = Word(alphas, alphas + nums + '_$')\n\n plus = Literal(\"+\")\n minus = Literal(\"-\")\n mult = Literal(\"*\")\n div = Literal(\"/\")\n lpar = Literal(\"(\").suppress()\n rpar = Literal(\")\").suppress()\n addop = plus | minus\n multop = mult | div\n expop = Literal(\"^\")\n _pi = CaselessLiteral(\"PI\")\n x = CaselessLiteral(\"X\")\n\n expr = Forward()\n atom = (Optional(\"-\") + (x | _pi | _e | fnumber | ident + lpar + expr + rpar).setParseAction(push_first) |\n (lpar + expr.suppress() + rpar)).setParseAction(push_u_minus)\n\n factor = Forward()\n factor << atom + ZeroOrMore((expop + factor).setParseAction(push_first))\n\n term = factor + ZeroOrMore((multop + factor).setParseAction(push_first))\n expr << term + ZeroOrMore((addop + term).setParseAction(push_first))\n\n expr.parseString(expression)\n return expr_stack"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef from_int(data):\n if not isinstance(data, int) and not isinstance(data, long):\n raise TypeError('Input must be integer')\n\n res = []\n while data > 0 or not res:\n for j in range(5):\n if not j % 2:\n res += CONSONANTS[(data & 0xf)]\n data >>= 4\n else:\n res += VOWELS[(data & 0x3)]\n data >>= 2\n if data > 0:\n res += '-'\n res.reverse()\n return ''.join(res)", "response": "Convert an integer to a string in the format of a sequence of numbers."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef to_int(data):\n if not isinstance(data, basestring):\n raise TypeError('Input must be string')\n\n res = 0\n for part in data.split('-'):\n if len(part) != 5:\n raise ValueError('Malformed proquint')\n for j in range(5):\n try:\n if not j % 2:\n res <<= 4\n res |= CONSONANTS.index(part[j])\n else:\n res <<= 2\n res |= VOWELS.index(part[j])\n except ValueError:\n raise ValueError('Unknown character \\'{!s}\\' in proquint'.format(part[j]))\n return res", "response": "decode a string into an integer"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget or create a shared key pair for certificate pushing.", "response": "def get_or_create_shared_key(cls, force_new=False):\n \"\"\"\n Create a shared public/private key pair for certificate pushing,\n if the settings allow.\n \"\"\"\n if force_new:\n with transaction.atomic():\n SharedKey.objects.filter(current=True).update(current=False)\n key = Key()\n return SharedKey.objects.create(public_key=key,\n private_key=key,\n current=True)\n # create a new shared key if one doesn't exist\n try:\n return SharedKey.objects.get(current=True)\n except SharedKey.DoesNotExist:\n key = Key()\n return SharedKey.objects.create(public_key=key,\n private_key=key,\n current=True)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _self_referential_fk(klass_model):\n for f in klass_model._meta.concrete_fields:\n if f.related_model:\n if issubclass(klass_model, f.related_model):\n return f.attname\n return None", "response": "Return whether this model has a self ref FK and the name for the field."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_or_create_current_instance(cls):\n\n # on Android, platform.platform() barfs, so we handle that safely here\n try:\n plat = platform.platform()\n except:\n plat = \"Unknown (Android?)\"\n\n kwargs = {\n \"platform\": plat,\n \"hostname\": platform.node(),\n \"sysversion\": sys.version,\n \"database\": DatabaseIDModel.get_or_create_current_database_id(),\n \"db_path\": os.path.abspath(settings.DATABASES['default']['NAME']),\n \"system_id\": os.environ.get(\"MORANGO_SYSTEM_ID\", \"\"),\n }\n\n # try to get the MAC address, but exclude it if it was a fake (random) address\n mac = uuid.getnode()\n if (mac >> 40) % 2 == 0: # 8th bit (of 48 bits, from left) is 1 if MAC is fake\n hashable_identifier = \"{}:{}\".format(kwargs['database'].id, mac)\n kwargs[\"node_id\"] = hashlib.sha1(hashable_identifier.encode('utf-8')).hexdigest()[:20]\n else:\n kwargs[\"node_id\"] = \"\"\n\n # do within transaction so we only ever have 1 current instance ID\n with transaction.atomic():\n InstanceIDModel.objects.filter(current=True).update(current=False)\n obj, created = InstanceIDModel.objects.get_or_create(**kwargs)\n obj.current = True\n obj.save()\n\n return obj, created", "response": "Get the instance model corresponding to the current system or create a new one if it does not exist."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef serialize(self):\n # NOTE: code adapted from https://github.com/django/django/blob/master/django/forms/models.py#L75\n opts = self._meta\n\n data = {}\n for f in opts.concrete_fields:\n if f.attname in self.morango_fields_not_to_serialize:\n continue\n if f.attname in self._morango_internal_fields_not_to_serialize:\n continue\n # case if model is morango mptt\n if f.attname in getattr(self, '_internal_mptt_fields_not_to_serialize', '_internal_fields_not_to_serialize'):\n continue\n if hasattr(f, 'value_from_object_json_compatible'):\n data[f.attname] = f.value_from_object_json_compatible(self)\n else:\n data[f.attname] = f.value_from_object(self)\n return data", "response": "Returns a dict of all concrete fields of the SyncableModel subclass except for those specifically blacklisted are returned in a dict."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef deserialize(cls, dict_model):\n kwargs = {}\n for f in cls._meta.concrete_fields:\n if f.attname in dict_model:\n kwargs[f.attname] = dict_model[f.attname]\n return cls(**kwargs)", "response": "Returns an unsaved class object based on the valid properties passed in."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_default(self):\n if self.has_default():\n if callable(self.default):\n default = self.default()\n if isinstance(default, uuid.UUID):\n return default.hex\n return default\n if isinstance(self.default, uuid.UUID):\n return self.default.hex\n return self.default\n return None", "response": "Returns the default value for this field."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_to_deleted_models(sender, instance=None, *args, **kwargs):\n if issubclass(sender, SyncableModel):\n instance._update_deleted_models()", "response": "Add a model to the list of deleted models."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmaking a request to the specified URL and returns the response as json object.", "response": "def make_request(self, url, method='get', headers=None, data=None,\n callback=None, errors=STRICT, verify=False, timeout=None, **params):\n \"\"\"\n Reusable method for performing requests.\n :param url - URL to request\n :param method - request method, default is 'get'\n :param headers - request headers\n :param data - post data\n :param callback - callback to be applied to response,\n default callback will parse response as json object.\n :param errors - specifies communication errors handling mode, possible\n values are:\n * strict (default) - throw an error as soon as one\n occurred\n * graceful - ignore certain errors, e.g. EmptyResponse\n * ignore - ignore all errors and return a result in\n any case.\n NOTE that it DOES NOT mean that no\n exceptions can be\n raised from this method, it mostly ignores\n communication\n related errors.\n * None or empty string equals to default\n :param verify - whether or not to verify SSL cert, default to False\n :param timeout - the timeout of the request in second, default to None\n :param params - additional query parameters for request\n \"\"\"\n error_modes = (STRICT, GRACEFUL, IGNORE)\n error_mode = errors or GRACEFUL\n if error_mode.lower() not in error_modes:\n raise ValueError(\n 'Possible values for errors argument are: %s'\n % ','.join(error_modes))\n\n if callback is None:\n callback = self._default_resp_callback\n\n request = getattr(requests, method.lower())\n log.debug('* Request URL: %s' % url)\n log.debug('* Request method: %s' % method)\n log.debug('* Request query params: %s' % params)\n log.debug('* Request headers: %s' % headers)\n log.debug('* Request timeout: %s' % timeout)\n\n r = request(\n url, headers=headers, data=data, verify=verify, timeout=timeout, params=params)\n\n log.debug('* r.url: %s' % r.url)\n\n try:\n r.raise_for_status()\n return callback(r)\n except Exception as e:\n return self._with_error_handling(r, e,\n error_mode, self.response_format)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef poll(self, url, initial_delay=2, delay=1, tries=20, errors=STRICT, is_complete_callback=None, **params):\n time.sleep(initial_delay)\n poll_response = None\n\n if is_complete_callback == None:\n is_complete_callback = self._default_poll_callback\n\n for n in range(tries):\n poll_response = self.make_request(url, headers=self._headers(),\n errors=errors, **params)\n\n if is_complete_callback(poll_response):\n return poll_response\n else:\n time.sleep(delay)\n\n if STRICT == errors:\n raise ExceededRetries(\n \"Failed to poll within {0} tries.\".format(tries))\n else:\n return poll_response", "response": "Poll the URL url and return the response."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck the condition in poll response to determine if it is complete and no subsequent poll requests should be done.", "response": "def _default_poll_callback(self, poll_resp):\n \"\"\"\n Checks the condition in poll response to determine if it is complete\n and no subsequent poll requests should be done.\n \"\"\"\n if poll_resp.parsed is None:\n return False\n success_list = ['UpdatesComplete', True, 'COMPLETE']\n status = None\n if self.response_format == 'xml':\n status = poll_resp.parsed.find('./Status').text\n elif self.response_format == 'json':\n status = poll_resp.parsed.get(\n 'Status', poll_resp.parsed.get('status'))\n if status is None:\n raise RuntimeError('Unable to get poll response status.')\n return status in success_list"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _fsic_queuing_calc(fsic1, fsic2):\n return {instance: fsic2.get(instance, 0) for instance, counter in six.iteritems(fsic1) if fsic2.get(instance, 0) < counter}", "response": "Calculate the lower counter between two fsics."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _serialize_into_store(profile, filter=None):\n # ensure that we write and retrieve the counter in one go for consistency\n current_id = InstanceIDModel.get_current_instance_and_increment_counter()\n\n with transaction.atomic():\n # create Q objects for filtering by prefixes\n prefix_condition = None\n if filter:\n prefix_condition = functools.reduce(lambda x, y: x | y, [Q(_morango_partition__startswith=prefix) for prefix in filter])\n\n # filter through all models with the dirty bit turned on\n syncable_dict = _profile_models[profile]\n for (_, klass_model) in six.iteritems(syncable_dict):\n new_store_records = []\n new_rmc_records = []\n klass_queryset = klass_model.objects.filter(_morango_dirty_bit=True)\n if prefix_condition:\n klass_queryset = klass_queryset.filter(prefix_condition)\n store_records_dict = Store.objects.in_bulk(id_list=klass_queryset.values_list('id', flat=True))\n for app_model in klass_queryset:\n try:\n store_model = store_records_dict[app_model.id]\n\n # if store record dirty and app record dirty, append store serialized to conflicting data\n if store_model.dirty_bit:\n store_model.conflicting_serialized_data = store_model.serialized + \"\\n\" + store_model.conflicting_serialized_data\n store_model.dirty_bit = False\n\n # set new serialized data on this store model\n ser_dict = json.loads(store_model.serialized)\n ser_dict.update(app_model.serialize())\n store_model.serialized = DjangoJSONEncoder().encode(ser_dict)\n\n # create or update instance and counter on the record max counter for this store model\n RecordMaxCounter.objects.update_or_create(defaults={'counter': current_id.counter},\n instance_id=current_id.id,\n store_model_id=store_model.id)\n\n # update last saved bys for this store model\n store_model.last_saved_instance = current_id.id\n store_model.last_saved_counter = current_id.counter\n # update deleted flags in case it was previously deleted\n store_model.deleted = False\n store_model.hard_deleted = False\n\n # update this model\n store_model.save()\n\n except KeyError:\n kwargs = {\n 'id': app_model.id,\n 'serialized': DjangoJSONEncoder().encode(app_model.serialize()),\n 'last_saved_instance': current_id.id,\n 'last_saved_counter': current_id.counter,\n 'model_name': app_model.morango_model_name,\n 'profile': app_model.morango_profile,\n 'partition': app_model._morango_partition,\n 'source_id': app_model._morango_source_id,\n }\n # check if model has FK pointing to it and add the value to a field on the store\n self_ref_fk = _self_referential_fk(klass_model)\n if self_ref_fk:\n self_ref_fk_value = getattr(app_model, self_ref_fk)\n kwargs.update({'_self_ref_fk': self_ref_fk_value or ''})\n # create store model and record max counter for the app model\n new_store_records.append(Store(**kwargs))\n new_rmc_records.append(RecordMaxCounter(store_model_id=app_model.id, instance_id=current_id.id, counter=current_id.counter))\n\n # bulk create store and rmc records for this class\n Store.objects.bulk_create(new_store_records)\n RecordMaxCounter.objects.bulk_create(new_rmc_records)\n\n # set dirty bit to false for all instances of this model\n klass_queryset.update(update_dirty_bit_to=False)\n\n # get list of ids of deleted models\n deleted_ids = DeletedModels.objects.filter(profile=profile).values_list('id', flat=True)\n # update last_saved_bys and deleted flag of all deleted store model instances\n deleted_store_records = Store.objects.filter(id__in=deleted_ids)\n deleted_store_records.update(dirty_bit=False, deleted=True, last_saved_instance=current_id.id, last_saved_counter=current_id.counter)\n # update rmcs counters for deleted models that have our instance id\n RecordMaxCounter.objects.filter(instance_id=current_id.id, store_model_id__in=deleted_ids).update(counter=current_id.counter)\n # get a list of deleted model ids that don't have an rmc for our instance id\n new_rmc_ids = deleted_store_records.exclude(recordmaxcounter__instance_id=current_id.id).values_list(\"id\", flat=True)\n # bulk create these new rmcs\n RecordMaxCounter.objects.bulk_create([RecordMaxCounter(store_model_id=r_id, instance_id=current_id.id, counter=current_id.counter) for r_id in new_rmc_ids])\n # clear deleted models table for this profile\n DeletedModels.objects.filter(profile=profile).delete()\n\n # handle logic for hard deletion models\n hard_deleted_ids = HardDeletedModels.objects.filter(profile=profile).values_list('id', flat=True)\n hard_deleted_store_records = Store.objects.filter(id__in=hard_deleted_ids)\n hard_deleted_store_records.update(hard_deleted=True, serialized='{}', conflicting_serialized_data='')\n HardDeletedModels.objects.filter(profile=profile).delete()\n\n # update our own database max counters after serialization\n if not filter:\n DatabaseMaxCounter.objects.update_or_create(instance_id=current_id.id, partition=\"\", defaults={'counter': current_id.counter})\n else:\n for f in filter:\n DatabaseMaxCounter.objects.update_or_create(instance_id=current_id.id, partition=f, defaults={'counter': current_id.counter})", "response": "Takes data from app layer and serializes the models into the store."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _deserialize_from_store(profile):\n # we first serialize to avoid deserialization merge conflicts\n _serialize_into_store(profile)\n\n fk_cache = {}\n with transaction.atomic():\n syncable_dict = _profile_models[profile]\n excluded_list = []\n # iterate through classes which are in foreign key dependency order\n for model_name, klass_model in six.iteritems(syncable_dict):\n # handle cases where a class has a single FK reference to itself\n self_ref_fk = _self_referential_fk(klass_model)\n query = Q(model_name=klass_model.morango_model_name)\n for klass in klass_model.morango_model_dependencies:\n query |= Q(model_name=klass.morango_model_name)\n if self_ref_fk:\n clean_parents = Store.objects.filter(dirty_bit=False, profile=profile).filter(query).char_ids_list()\n dirty_children = Store.objects.filter(dirty_bit=True, profile=profile) \\\n .filter(Q(_self_ref_fk__in=clean_parents) | Q(_self_ref_fk='')).filter(query)\n\n # keep iterating until size of dirty_children is 0\n while len(dirty_children) > 0:\n for store_model in dirty_children:\n try:\n app_model = store_model._deserialize_store_model(fk_cache)\n if app_model:\n with mute_signals(signals.pre_save, signals.post_save):\n app_model.save(update_dirty_bit_to=False)\n # we update a store model after we have deserialized it to be able to mark it as a clean parent\n store_model.dirty_bit = False\n store_model.save(update_fields=['dirty_bit'])\n except exceptions.ValidationError:\n # if the app model did not validate, we leave the store dirty bit set\n excluded_list.append(store_model.id)\n\n # update lists with new clean parents and dirty children\n clean_parents = Store.objects.filter(dirty_bit=False, profile=profile).filter(query).char_ids_list()\n dirty_children = Store.objects.filter(dirty_bit=True, profile=profile, _self_ref_fk__in=clean_parents).filter(query)\n else:\n # array for holding db values from the fields of each model for this class\n db_values = []\n fields = klass_model._meta.fields\n for store_model in Store.objects.filter(model_name=model_name, profile=profile, dirty_bit=True):\n try:\n app_model = store_model._deserialize_store_model(fk_cache)\n # if the model was not deleted add its field values to the list\n if app_model:\n for f in fields:\n value = getattr(app_model, f.attname)\n db_value = f.get_db_prep_value(value, connection)\n db_values.append(db_value)\n except exceptions.ValidationError:\n # if the app model did not validate, we leave the store dirty bit set\n excluded_list.append(store_model.id)\n\n if db_values:\n # number of rows to update\n num_of_rows = len(db_values) // len(fields)\n # create '%s' placeholders for a single row\n placeholder_tuple = tuple(['%s' for _ in range(len(fields))])\n # create list of the '%s' tuple placeholders based on number of rows to update\n placeholder_list = [str(placeholder_tuple) for _ in range(num_of_rows)]\n with connection.cursor() as cursor:\n DBBackend._bulk_insert_into_app_models(cursor, klass_model._meta.db_table, fields, db_values, placeholder_list)\n\n # clear dirty bit for all store models for this profile except for models that did not validate\n Store.objects.exclude(id__in=excluded_list).filter(profile=profile, dirty_bit=True).update(dirty_bit=False)", "response": "Deserializes data from the store into the application."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nqueue all data into the buffer.", "response": "def _queue_into_buffer(transfersession):\n \"\"\"\n Takes a chunk of data from the store to be put into the buffer to be sent to another morango instance.\n \"\"\"\n last_saved_by_conditions = []\n filter_prefixes = Filter(transfersession.filter)\n server_fsic = json.loads(transfersession.server_fsic)\n client_fsic = json.loads(transfersession.client_fsic)\n\n if transfersession.push:\n fsics = _fsic_queuing_calc(client_fsic, server_fsic)\n else:\n fsics = _fsic_queuing_calc(server_fsic, client_fsic)\n\n # if fsics are identical or receiving end has newer data, then there is nothing to queue\n if not fsics:\n return\n\n # create condition for all push FSICs where instance_ids are equal, but internal counters are higher than FSICs counters\n for instance, counter in six.iteritems(fsics):\n last_saved_by_conditions += [\"(last_saved_instance = '{0}' AND last_saved_counter > {1})\".format(instance, counter)]\n if fsics:\n last_saved_by_conditions = [_join_with_logical_operator(last_saved_by_conditions, 'OR')]\n\n partition_conditions = []\n # create condition for filtering by partitions\n for prefix in filter_prefixes:\n partition_conditions += [\"partition LIKE '{}%'\".format(prefix)]\n if filter_prefixes:\n partition_conditions = [_join_with_logical_operator(partition_conditions, 'OR')]\n\n # combine conditions\n fsic_and_partition_conditions = _join_with_logical_operator(last_saved_by_conditions + partition_conditions, 'AND')\n\n # filter by profile\n where_condition = _join_with_logical_operator([fsic_and_partition_conditions, \"profile = '{}'\".format(transfersession.sync_session.profile)], 'AND')\n\n # execute raw sql to take all records that match condition, to be put into buffer for transfer\n with connection.cursor() as cursor:\n queue_buffer = \"\"\"INSERT INTO {outgoing_buffer}\n (model_uuid, serialized, deleted, last_saved_instance, last_saved_counter, hard_deleted,\n model_name, profile, partition, source_id, conflicting_serialized_data, transfer_session_id, _self_ref_fk)\n SELECT id, serialized, deleted, last_saved_instance, last_saved_counter, hard_deleted, model_name, profile, partition, source_id, conflicting_serialized_data, '{transfer_session_id}', _self_ref_fk\n FROM {store} WHERE {condition}\"\"\".format(outgoing_buffer=Buffer._meta.db_table,\n transfer_session_id=transfersession.id,\n condition=where_condition,\n store=Store._meta.db_table)\n cursor.execute(queue_buffer)\n # take all record max counters that are foreign keyed onto store models, which were queued into the buffer\n queue_rmc_buffer = \"\"\"INSERT INTO {outgoing_rmcb}\n (instance_id, counter, transfer_session_id, model_uuid)\n SELECT instance_id, counter, '{transfer_session_id}', store_model_id\n FROM {record_max_counter} AS rmc\n INNER JOIN {outgoing_buffer} AS buffer ON rmc.store_model_id = buffer.model_uuid\n WHERE buffer.transfer_session_id = '{transfer_session_id}'\n \"\"\".format(outgoing_rmcb=RecordMaxCounterBuffer._meta.db_table,\n transfer_session_id=transfersession.id,\n record_max_counter=RecordMaxCounter._meta.db_table,\n outgoing_buffer=Buffer._meta.db_table)\n cursor.execute(queue_rmc_buffer)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntakes data from the buffers and merges into the store and record max counters.", "response": "def _dequeue_into_store(transfersession):\n \"\"\"\n Takes data from the buffers and merges into the store and record max counters.\n \"\"\"\n with connection.cursor() as cursor:\n DBBackend._dequeuing_delete_rmcb_records(cursor, transfersession.id)\n DBBackend._dequeuing_delete_buffered_records(cursor, transfersession.id)\n current_id = InstanceIDModel.get_current_instance_and_increment_counter()\n DBBackend._dequeuing_merge_conflict_buffer(cursor, current_id, transfersession.id)\n DBBackend._dequeuing_merge_conflict_rmcb(cursor, transfersession.id)\n DBBackend._dequeuing_update_rmcs_last_saved_by(cursor, current_id, transfersession.id)\n DBBackend._dequeuing_delete_mc_rmcb(cursor, transfersession.id)\n DBBackend._dequeuing_delete_mc_buffer(cursor, transfersession.id)\n DBBackend._dequeuing_insert_remaining_buffer(cursor, transfersession.id)\n DBBackend._dequeuing_insert_remaining_rmcb(cursor, transfersession.id)\n DBBackend._dequeuing_delete_remaining_rmcb(cursor, transfersession.id)\n DBBackend._dequeuing_delete_remaining_buffer(cursor, transfersession.id)\n if getattr(settings, 'MORANGO_DESERIALIZE_AFTER_DEQUEUING', True):\n _deserialize_from_store(transfersession.sync_session.profile)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef max_parameter_substitution():\n if os.path.isfile(SQLITE_VARIABLE_FILE_CACHE):\n return\n conn = sqlite3.connect(':memory:')\n low = 1\n high = 1000 # hard limit for SQLITE_MAX_VARIABLE_NUMBER \n conn.execute('CREATE TABLE T1 (id C1)')\n while low < high - 1:\n guess = (low + high) // 2\n try:\n statement = 'select * from T1 where id in (%s)' % ','.join(['?' for _ in range(guess)])\n values = [i for i in range(guess)]\n conn.execute(statement, values)\n except sqlite3.DatabaseError as ex:\n if 'too many SQL variables' in str(ex):\n high = guess\n else:\n raise\n else:\n low = guess\n conn.close()\n with open(SQLITE_VARIABLE_FILE_CACHE, 'w') as file:\n file.write(str(low))", "response": "SQLite has a limit on the maximum number of variables allowed for parameter substitution. This limit is usually 999 but we can t use the max number of variables allowed for parameter substitution."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nauthenticate the userargs and password against Django s auth backends.", "response": "def authenticate_credentials(self, userargs, password, request=None):\n \"\"\"\n Authenticate the userargs and password against Django auth backends.\n The \"userargs\" string may be just the username, or a querystring-encoded set of params.\n \"\"\"\n\n credentials = {\n 'password': password\n }\n\n if \"=\" not in userargs:\n # if it doesn't seem to be in querystring format, just use it as the username\n credentials[get_user_model().USERNAME_FIELD] = userargs\n else:\n # parse out the user args from querystring format into the credentials dict\n for arg in userargs.split(\"&\"):\n key, val = arg.split(\"=\")\n credentials[key] = val\n\n # authenticate the user via Django's auth backends\n user = authenticate(**credentials)\n\n if user is None:\n raise exceptions.AuthenticationFailed('Invalid credentials.')\n\n if not user.is_active:\n raise exceptions.AuthenticationFailed('User inactive or deleted.')\n\n return (user, None)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _multiple_self_ref_fk_check(class_model):\n self_fk = []\n for f in class_model._meta.concrete_fields:\n if f.related_model in self_fk:\n return True\n if f.related_model == class_model:\n self_fk.append(class_model)\n return False", "response": "Check whether a class has more than one FK reference to itself."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_syncable_models():\n\n import django.apps\n from morango.models import SyncableModel\n from morango.manager import SyncableModelManager\n from morango.query import SyncableModelQuerySet\n\n model_list = []\n for model_class in django.apps.apps.get_models():\n # several validation checks to assert models will be syncing correctly\n if issubclass(model_class, SyncableModel):\n name = model_class.__name__\n if _multiple_self_ref_fk_check(model_class):\n raise InvalidMorangoModelConfiguration(\"Syncing models with more than 1 self referential ForeignKey is not supported.\")\n try:\n from mptt import models\n from morango.utils.morango_mptt import MorangoMPTTModel, MorangoMPTTTreeManager, MorangoTreeQuerySet\n # mptt syncable model checks\n if issubclass(model_class, models.MPTTModel):\n if not issubclass(model_class, MorangoMPTTModel):\n raise InvalidMorangoModelConfiguration(\"{} that inherits from MPTTModel, should instead inherit from MorangoMPTTModel.\".format(name))\n if not isinstance(model_class.objects, MorangoMPTTTreeManager):\n raise InvalidMPTTManager(\"Manager for {} must inherit from MorangoMPTTTreeManager.\".format(name))\n if not isinstance(model_class.objects.none(), MorangoTreeQuerySet):\n raise InvalidMPTTQuerySet(\"Queryset for {} model must inherit from MorangoTreeQuerySet.\".format(name))\n except ImportError:\n pass\n # syncable model checks\n if not isinstance(model_class.objects, SyncableModelManager):\n raise InvalidSyncableManager(\"Manager for {} must inherit from SyncableModelManager.\".format(name))\n if not isinstance(model_class.objects.none(), SyncableModelQuerySet):\n raise InvalidSyncableQueryset(\"Queryset for {} model must inherit from SyncableModelQuerySet.\".format(name))\n if model_class._meta.many_to_many:\n raise UnsupportedFieldType(\"{} model with a ManyToManyField is not supported in morango.\")\n if not hasattr(model_class, 'morango_model_name'):\n raise InvalidMorangoModelConfiguration(\"{} model must define a morango_model_name attribute\".format(name))\n if not hasattr(model_class, 'morango_profile'):\n raise InvalidMorangoModelConfiguration(\"{} model must define a morango_profile attribute\".format(name))\n\n # create empty list to hold model classes for profile if not yet created\n profile = model_class.morango_profile\n _profile_models[profile] = _profile_models.get(profile, [])\n\n # don't sync models where morango_model_name is None\n if model_class.morango_model_name is not None:\n _insert_model_into_profile_dict(model_class, profile)\n\n # for each profile, create a dict mapping from morango model names to model class\n for profile, model_list in iteritems(_profile_models):\n syncable_models_dict = OrderedDict()\n for model_class in model_list:\n syncable_models_dict[model_class.morango_model_name] = model_class\n _profile_models[profile] = syncable_models_dict", "response": "Add syncable models to the internal list of models."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbulk insert into app - models.", "response": "def _bulk_insert_into_app_models(self, cursor, app_model, fields, db_values, placeholder_list):\n \"\"\"\n Example query:\n `REPLACE INTO model (F1,F2,F3) VALUES (%s, %s, %s), (%s, %s, %s), (%s, %s, %s)`\n where values=[1,2,3,4,5,6,7,8,9]\n \"\"\"\n # calculate and create equal sized chunks of data to insert incrementally\n num_of_rows_able_to_insert = self.SQLITE_MAX_VARIABLE_NUMBER // len(fields)\n num_of_values_able_to_insert = num_of_rows_able_to_insert * len(fields)\n value_chunks = [db_values[x:x + num_of_values_able_to_insert] for x in range(0, len(db_values), num_of_values_able_to_insert)]\n placeholder_chunks = [placeholder_list[x: x + num_of_rows_able_to_insert] for x in range(0, len(placeholder_list), num_of_rows_able_to_insert)]\n # insert data chunks\n fields = str(tuple(str(f.attname) for f in fields)).replace(\"'\", '')\n for values, params in zip(value_chunks, placeholder_chunks):\n placeholder_str = ', '.join(params).replace(\"'\", '')\n insert = \"\"\"REPLACE INTO {app_model} {fields}\n VALUES {placeholder_str}\n \"\"\".format(app_model=app_model, fields=fields, placeholder_str=placeholder_str)\n # use DB-APIs parameter substitution (2nd parameter expects a sequence)\n cursor.execute(insert, values)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fuzzyfinder(input, collection, accessor=lambda x: x, sort_results=True):\n suggestions = []\n input = str(input) if not isinstance(input, str) else input\n pat = '.*?'.join(map(re.escape, input))\n pat = '(?=({0}))'.format(pat) # lookahead regex to manage overlapping matches\n regex = re.compile(pat, re.IGNORECASE)\n for item in collection:\n r = list(regex.finditer(accessor(item)))\n if r:\n best = min(r, key=lambda x: len(x.group(1))) # find shortest match\n suggestions.append((len(best.group(1)), best.start(), accessor(item), item))\n\n if sort_results:\n return (z[-1] for z in sorted(suggestions))\n else:\n return (z[-1] for z in sorted(suggestions, key=lambda x: x[:2]))", "response": "Returns a generator that yields a list of fuzzy matches for the input string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_access_token(self, valid_in_hours=1, data=None):\n data = data or {}\n token = AccessToken(\n token=self.generate(),\n expires_at=expires_at(hours=valid_in_hours),\n data=data)\n return token", "response": "Creates an access token."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstore an OWS service in mongodb.", "response": "def save_service(self, service, overwrite=True):\n \"\"\"\n Stores an OWS service in mongodb.\n \"\"\"\n name = namesgenerator.get_sane_name(service.name)\n if not name:\n name = namesgenerator.get_random_name()\n if self.collection.count_documents({'name': name}) > 0:\n name = namesgenerator.get_random_name(retry=True)\n # check if service is already registered\n if self.collection.count_documents({'name': name}) > 0:\n if overwrite:\n self.collection.delete_one({'name': name})\n else:\n raise Exception(\"service name already registered.\")\n self.collection.insert_one(Service(\n name=name,\n url=baseurl(service.url),\n type=service.type,\n purl=service.purl,\n public=service.public,\n auth=service.auth,\n verify=service.verify))\n return self.fetch_by_name(name=name)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist all services in mongodb storage.", "response": "def list_services(self):\n \"\"\"\n Lists all services in mongodb storage.\n \"\"\"\n my_services = []\n for service in self.collection.find().sort('name', pymongo.ASCENDING):\n my_services.append(Service(service))\n return my_services"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fetch_by_name(self, name):\n service = self.collection.find_one({'name': name})\n if not service:\n raise ServiceNotFound\n return Service(service)", "response": "Gets a service by its name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fetch_by_url(self, url):\n service = self.collection.find_one({'url': url})\n if not service:\n raise ServiceNotFound\n return Service(service)", "response": "Gets a service by url."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a new object that is a proxy of the current object.", "response": "def owsproxy(request):\n \"\"\"\n TODO: use ows exceptions\n \"\"\"\n try:\n service_name = request.matchdict.get('service_name')\n extra_path = request.matchdict.get('extra_path')\n store = servicestore_factory(request.registry)\n service = store.fetch_by_name(service_name)\n except Exception as err:\n # TODO: Store impl should raise appropriate exception like not authorized\n return OWSAccessFailed(\"Could not find service {0} : {1}.\".format(service_name, err.message))\n else:\n return _send_request(request, service, extra_path, request_params=request.query_string)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef owsproxy_delegate(request):\n twitcher_url = request.registry.settings.get('twitcher.url')\n protected_path = request.registry.settings.get('twitcher.ows_proxy_protected_path', '/ows')\n url = twitcher_url + protected_path + '/proxy'\n if request.matchdict.get('service_name'):\n url += '/' + request.matchdict.get('service_name')\n if request.matchdict.get('access_token'):\n url += '/' + request.matchdict.get('service_name')\n url += '?' + urlparse.urlencode(request.params)\n LOGGER.debug(\"delegate to owsproxy: %s\", url)\n # forward request to target (without Host Header)\n # h = dict(request.headers)\n # h.pop(\"Host\", h)\n resp = requests.request(method=request.method.upper(), url=url, data=request.body,\n headers=request.headers, verify=False)\n return Response(resp.content, status=resp.status_code, headers=resp.headers)", "response": "Delegates owsproxy request to external twitcher service."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ows_security_tween_factory(handler, registry):\n\n security = owssecurity_factory(registry)\n\n def ows_security_tween(request):\n try:\n security.check_request(request)\n return handler(request)\n except OWSException as err:\n logger.exception(\"security check failed.\")\n return err\n except Exception as err:\n logger.exception(\"unknown error\")\n return OWSNoApplicableCode(\"{}\".format(err))\n\n return ows_security_tween", "response": "A tween factory which produces a tween which raises an exception\n if access to OWS service is not allowed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef main(global_config, **settings):\n from pyramid.config import Configurator\n\n config = Configurator(settings=settings)\n\n # include twitcher components\n config.include('twitcher.config')\n config.include('twitcher.frontpage')\n config.include('twitcher.rpcinterface')\n config.include('twitcher.owsproxy')\n\n # tweens/middleware\n # TODO: maybe add tween for exception handling or use unknown_failure view\n config.include('twitcher.tweens')\n\n config.scan()\n\n return config.make_wsgi_app()", "response": "This function returns a Pyramid WSGI application."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving a token from the cache.", "response": "def revoke_token(self, token):\n \"\"\"\n Implementation of :meth:`twitcher.api.ITokenManager.revoke_token`.\n \"\"\"\n try:\n self.store.delete_token(token)\n except Exception:\n LOGGER.exception('Failed to remove token.')\n return False\n else:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef revoke_all_tokens(self):\n try:\n self.store.clear_tokens()\n except Exception:\n LOGGER.exception('Failed to remove tokens.')\n return False\n else:\n return True", "response": "Removes all tokens from the store."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef register_service(self, url, data=None, overwrite=True):\n data = data or {}\n\n args = dict(data)\n args['url'] = url\n service = Service(**args)\n service = self.store.save_service(service, overwrite=overwrite)\n return service.params", "response": "Implementation of twitcher. api. IRegistry. register_service."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef unregister_service(self, name):\n try:\n self.store.delete_service(name=name)\n except Exception:\n LOGGER.exception('unregister failed')\n return False\n else:\n return True", "response": "Unregisters a service from the registry."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef clear_services(self):\n try:\n self.store.clear_services()\n except Exception:\n LOGGER.error('Clear services failed.')\n return False\n else:\n return True", "response": "Clear the services of the current user."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nincludes the XML - RPC interface in a Pyramid application.", "response": "def includeme(config):\n \"\"\" The callable makes it possible to include rpcinterface\n in a Pyramid application.\n\n Calling ``config.include(twitcher.rpcinterface)`` will result in this\n callable being called.\n\n Arguments:\n\n * ``config``: the ``pyramid.config.Configurator`` object.\n \"\"\"\n settings = config.registry.settings\n\n if asbool(settings.get('twitcher.rpcinterface', True)):\n LOGGER.debug('Twitcher XML-RPC Interface enabled.')\n\n # include twitcher config\n config.include('twitcher.config')\n\n # using basic auth\n config.include('twitcher.basicauth')\n\n # pyramid xml-rpc\n # http://docs.pylonsproject.org/projects/pyramid-rpc/en/latest/xmlrpc.html\n config.include('pyramid_rpc.xmlrpc')\n config.include('twitcher.db')\n config.add_xmlrpc_endpoint('api', '/RPC2')\n\n # register xmlrpc methods\n config.add_xmlrpc_method(RPCInterface, attr='generate_token', endpoint='api', method='generate_token')\n config.add_xmlrpc_method(RPCInterface, attr='revoke_token', endpoint='api', method='revoke_token')\n config.add_xmlrpc_method(RPCInterface, attr='revoke_all_tokens', endpoint='api', method='revoke_all_tokens')\n config.add_xmlrpc_method(RPCInterface, attr='register_service', endpoint='api', method='register_service')\n config.add_xmlrpc_method(RPCInterface, attr='unregister_service', endpoint='api', method='unregister_service')\n config.add_xmlrpc_method(RPCInterface, attr='get_service_by_name', endpoint='api', method='get_service_by_name')\n config.add_xmlrpc_method(RPCInterface, attr='get_service_by_url', endpoint='api', method='get_service_by_url')\n config.add_xmlrpc_method(RPCInterface, attr='clear_services', endpoint='api', method='clear_services')\n config.add_xmlrpc_method(RPCInterface, attr='list_services', endpoint='api', method='list_services')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef save_service(self, service, overwrite=True):\n name = namesgenerator.get_sane_name(service.name)\n if not name:\n name = namesgenerator.get_random_name()\n if name in self.name_index:\n name = namesgenerator.get_random_name(retry=True)\n # check if service is already registered\n if name in self.name_index:\n if overwrite:\n self._delete(name=name)\n else:\n raise Exception(\"service name already registered.\")\n self._insert(Service(\n name=name,\n url=baseurl(service.url),\n type=service.type,\n purl=service.purl,\n public=service.public,\n auth=service.auth,\n verify=service.verify))\n return self.fetch_by_name(name=name)", "response": "Save an OWS service in database."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists all services in memory storage.", "response": "def list_services(self):\n \"\"\"\n Lists all services in memory storage.\n \"\"\"\n my_services = []\n for service in self.name_index.values():\n my_services.append(Service(service))\n return my_services"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfetch a service by its name.", "response": "def fetch_by_name(self, name):\n \"\"\"\n Get service for given ``name`` from memory storage.\n \"\"\"\n service = self.name_index.get(name)\n if not service:\n raise ServiceNotFound\n return Service(service)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nretrieving a new private key and certificate request and returns the certificate.", "response": "def _retrieve_certificate(self, access_token, timeout=3):\n \"\"\"\n Generates a new private key and certificate request, submits the request to be\n signed by the SLCS CA and returns the certificate.\n \"\"\"\n logger.debug(\"Retrieve certificate with token.\")\n\n # Generate a new key pair\n key_pair = crypto.PKey()\n key_pair.generate_key(crypto.TYPE_RSA, 2048)\n private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, key_pair).decode(\"utf-8\")\n\n # Generate a certificate request using that key-pair\n cert_request = crypto.X509Req()\n\n # Create public key object\n cert_request.set_pubkey(key_pair)\n\n # Add the public key to the request\n cert_request.sign(key_pair, 'md5')\n der_cert_req = crypto.dump_certificate_request(crypto.FILETYPE_ASN1, cert_request)\n\n encoded_cert_req = base64.b64encode(der_cert_req)\n\n # Build the OAuth session object\n token = {'access_token': access_token, 'token_type': 'Bearer'}\n client = OAuth2Session(token=token)\n\n response = client.post(\n self.certificate_url,\n data={'certificate_request': encoded_cert_req},\n verify=False,\n timeout=timeout,\n )\n\n if response.ok:\n content = \"{} {}\".format(response.text, private_key)\n with open(self.esgf_credentials, 'w') as fh:\n fh.write(content)\n logger.debug('Fetched certificate successfully.')\n else:\n msg = \"Could not get certificate: {} {}\".format(response.status_code, response.reason)\n raise Exception(msg)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef tokenstore_factory(registry, database=None):\n database = database or 'mongodb'\n if database == 'mongodb':\n db = _mongodb(registry)\n store = MongodbTokenStore(db.tokens)\n else:\n store = MemoryTokenStore()\n return store", "response": "Returns an instance of TokenStore with the interface of twitcher. store. AccessTokenStore."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef servicestore_factory(registry, database=None):\n database = database or 'mongodb'\n if database == 'mongodb':\n db = _mongodb(registry)\n store = MongodbServiceStore(collection=db.services)\n else:\n store = MemoryServiceStore()\n return store", "response": "Returns an instance of the service store."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngenerate a random name from the list of adjectives and birds in this package formatted as adjective_surname. For example loving_sugarbird3.", "response": "def get_random_name(retry=False):\n \"\"\"\n generates a random name from the list of adjectives and birds in this package\n formatted as \"adjective_surname\". For example 'loving_sugarbird'. If retry is non-zero, a random\n integer between 0 and 100 will be added to the end of the name, e.g `loving_sugarbird3`\n \"\"\"\n name = \"%s_%s\" % (left[random.randint(0, len(left) - 1)], right[random.randint(0, len(right) - 1)])\n if retry is True:\n name = \"%s%d\" % (name, random.randint(0, 100))\n return name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget parameter in GET request.", "response": "def _get_param(self, param, allowed_values=None, optional=False):\n \"\"\"Get parameter in GET request.\"\"\"\n request_params = self._request_params()\n if param in request_params:\n value = request_params[param].lower()\n if allowed_values is not None:\n if value in allowed_values:\n self.params[param] = value\n else:\n raise OWSInvalidParameterValue(\"%s %s is not supported\" % (param, value), value=param)\n elif optional:\n self.params[param] = None\n else:\n raise OWSMissingParameterValue('Parameter \"%s\" is missing' % param, value=param)\n return self.params[param]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfinding requested version in GET request.", "response": "def _get_version(self):\n \"\"\"Find requested version in GET request.\"\"\"\n version = self._get_param(param=\"version\", allowed_values=allowed_versions[self.params['service']],\n optional=True)\n if version is None and self._get_request_type() != \"getcapabilities\":\n raise OWSMissingParameterValue('Parameter \"version\" is missing', value=\"version\")\n else:\n return version"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_service(self):\n if \"service\" in self.document.attrib:\n value = self.document.attrib[\"service\"].lower()\n if value in allowed_service_types:\n self.params[\"service\"] = value\n else:\n raise OWSInvalidParameterValue(\"Service %s is not supported\" % value, value=\"service\")\n else:\n raise OWSMissingParameterValue('Parameter \"service\" is missing', value=\"service\")\n return self.params[\"service\"]", "response": "Check mandatory service name parameter in POST request."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_request_type(self):\n value = self.document.tag.lower()\n if value in allowed_request_types[self.params['service']]:\n self.params[\"request\"] = value\n else:\n raise OWSInvalidParameterValue(\"Request type %s is not supported\" % value, value=\"request\")\n return self.params[\"request\"]", "response": "Find requested request type in POST request."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_version(self):\n if \"version\" in self.document.attrib:\n value = self.document.attrib[\"version\"].lower()\n if value in allowed_versions[self.params['service']]:\n self.params[\"version\"] = value\n else:\n raise OWSInvalidParameterValue(\"Version %s is not supported\" % value, value=\"version\")\n elif self._get_request_type() == \"getcapabilities\":\n self.params[\"version\"] = None\n else:\n raise OWSMissingParameterValue('Parameter \"version\" is missing', value=\"version\")\n return self.params[\"version\"]", "response": "Find requested version in POST request."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprovides a timzeone - aware object for a given datetime and timezone name", "response": "def localize_datetime(dt, tz_name='UTC'):\n \"\"\"Provide a timzeone-aware object for a given datetime and timezone name\n \"\"\"\n tz_aware_dt = dt\n if dt.tzinfo is None:\n utc = pytz.timezone('UTC')\n aware = utc.localize(dt)\n timezone = pytz.timezone(tz_name)\n tz_aware_dt = aware.astimezone(timezone)\n else:\n logger.warn('tzinfo already set')\n return tz_aware_dt"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning base url of given url", "response": "def baseurl(url):\n \"\"\"\n return baseurl of given url\n \"\"\"\n parsed_url = urlparse.urlparse(url)\n if not parsed_url.netloc or parsed_url.scheme not in (\"http\", \"https\"):\n raise ValueError('bad url')\n service_url = \"%s://%s%s\" % (parsed_url.scheme, parsed_url.netloc, parsed_url.path.strip())\n return service_url"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nverify ssl service certificate.", "response": "def verify(self):\n \"\"\"Verify ssl service certificate.\"\"\"\n value = self.get('verify', 'true')\n if isinstance(value, bool):\n verify = value\n elif value.lower() == 'true':\n verify = True\n elif value.lower() == 'false':\n verify = False\n else:\n verify = value\n return verify"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef tag(self, label, message=None):\n notify.warning('Unsupported SCM: Make sure you apply the \"{}\" tag after commit!{}'.format(\n label, ' [message={}]'.format(message) if message else '',\n ))", "response": "Tag the current workdir state with the given label."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a PEP - 440 dev version appendix to the main version number.", "response": "def pep440_dev_version(self, verbose=False, non_local=False):\n \"\"\"Return a PEP-440 dev version appendix to the main version number.\"\"\"\n # Always return a timestamp\n pep440 = '.dev{:%Y%m%d%H%M}'.format(datetime.now())\n\n if not non_local:\n build_number = os.environ.get('BUILD_NUMBER', 'n/a')\n if build_number.isdigit():\n pep440 += '+ci.{}'.format(build_number)\n if verbose:\n notify.info(\"Adding CI build ID #{} to version\".format(build_number))\n\n return pep440"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalls setup. py egg_info and return the parsed meta - data.", "response": "def get_egg_info(cfg, verbose=False):\n \"\"\"Call 'setup egg_info' and return the parsed meta-data.\"\"\"\n result = Bunch()\n setup_py = cfg.rootjoin('setup.py')\n if not os.path.exists(setup_py):\n return result\n\n egg_info = shell.capture(\"python {} egg_info\".format(setup_py), echo=True if verbose else None)\n for info_line in egg_info.splitlines():\n if info_line.endswith('PKG-INFO'):\n pkg_info_file = info_line.split(None, 1)[1]\n result['__file__'] = pkg_info_file\n with io.open(pkg_info_file, encoding='utf-8') as handle:\n lastkey = None\n for line in handle:\n if line.lstrip() != line:\n assert lastkey, \"Bad continuation in PKG-INFO file '{}': {}\".format(pkg_info_file, line)\n result[lastkey] += '\\n' + line\n else:\n lastkey, value = line.split(':', 1)\n lastkey = lastkey.strip().lower().replace('-', '_')\n value = value.strip()\n if lastkey in result:\n try:\n result[lastkey].append(value)\n except AttributeError:\n result[lastkey] = [result[lastkey], value]\n else:\n result[lastkey] = value\n\n for multikey in PKG_INFO_MULTIKEYS:\n if not isinstance(result.get(multikey, []), list):\n result[multikey] = [result[multikey]]\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nbumps a development version.", "response": "def bump(ctx, verbose=False, pypi=False):\n \"\"\"Bump a development version.\"\"\"\n cfg = config.load()\n scm = scm_provider(cfg.project_root, commit=False, ctx=ctx)\n\n # Check for uncommitted changes\n if not scm.workdir_is_clean():\n notify.warning(\"You have uncommitted changes, will create a time-stamped version!\")\n\n pep440 = scm.pep440_dev_version(verbose=verbose, non_local=pypi)\n\n # Rewrite 'setup.cfg' TODO: refactor to helper, see also release-prep\n # with util.rewrite_file(cfg.rootjoin('setup.cfg')) as lines:\n # ...\n setup_cfg = cfg.rootjoin('setup.cfg')\n if not pep440:\n notify.info(\"Working directory contains a release version!\")\n elif os.path.exists(setup_cfg):\n with io.open(setup_cfg, encoding='utf-8') as handle:\n data = handle.readlines()\n changed = False\n for i, line in enumerate(data):\n if re.match(r\"#? *tag_build *= *.*\", line):\n verb, _ = data[i].split('=', 1)\n data[i] = '{}= {}\\n'.format(verb, pep440)\n changed = True\n\n if changed:\n notify.info(\"Rewriting 'setup.cfg'...\")\n with io.open(setup_cfg, 'w', encoding='utf-8') as handle:\n handle.write(''.join(data))\n else:\n notify.warning(\"No 'tag_build' setting found in 'setup.cfg'!\")\n else:\n notify.warning(\"Cannot rewrite 'setup.cfg', none found!\")\n\n if os.path.exists(setup_cfg):\n # Update metadata and print version\n egg_info = shell.capture(\"python setup.py egg_info\", echo=True if verbose else None)\n for line in egg_info.splitlines():\n if line.endswith('PKG-INFO'):\n pkg_info_file = line.split(None, 1)[1]\n with io.open(pkg_info_file, encoding='utf-8') as handle:\n notify.info('\\n'.join(i for i in handle.readlines() if i.startswith('Version:')).strip())\n ctx.run(\"python setup.py -q develop\", echo=True if verbose else None)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npackage the project with PEX.", "response": "def pex(ctx, pyrun='', upload=False, opts=''):\n \"\"\"Package the project with PEX.\"\"\"\n cfg = config.load()\n\n # Build and check release\n ctx.run(\": invoke clean --all build test check\")\n\n # Get full version\n pkg_info = get_egg_info(cfg)\n # from pprint import pprint; pprint(dict(pkg_info))\n version = pkg_info.version if pkg_info else cfg.project.version\n\n # Build a PEX for each console entry-point\n pex_files = []\n # from pprint import pprint; pprint(cfg.project.entry_points)\n for script in cfg.project.entry_points['console_scripts']:\n script, entry_point = script.split('=', 1)\n script, entry_point = script.strip(), entry_point.strip()\n pex_file = cfg.rootjoin('bin', '{}-{}.pex'.format(script, version))\n cmd = ['pex', '-r', cfg.rootjoin('requirements.txt'), cfg.project_root, '-c', script, '-o', pex_file]\n if opts:\n cmd.append(opts)\n ctx.run(' '.join(cmd))\n\n # Warn about non-portable stuff\n non_universal = set()\n with closing(zipfile.ZipFile(pex_file, mode=\"r\")) as pex_contents:\n for pex_name in pex_contents.namelist(): # pylint: disable=no-member\n if pex_name.endswith('WHEEL') and '-py2.py3-none-any.whl' not in pex_name:\n non_universal.add(pex_name.split('.whl')[0].split('/')[-1])\n if non_universal:\n notify.warning(\"Non-universal or native wheels in PEX '{}':\\n {}\"\n .format(pex_file.replace(os.getcwd(), '.'), '\\n '.join(sorted(non_universal))))\n envs = [i.split('-')[-3:] for i in non_universal]\n envs = {i[0]: i[1:] for i in envs}\n if len(envs) > 1:\n envs = {k: v for k, v in envs.items() if not k.startswith('py')}\n env_id = []\n for k, v in sorted(envs.items()):\n env_id.append(k)\n env_id.extend(v)\n env_id = '-'.join(env_id)\n else:\n env_id = 'py2.py3-none-any'\n\n new_pex_file = pex_file.replace('.pex', '-{}.pex'.format(env_id))\n notify.info(\"Renamed PEX to '{}'\".format(os.path.basename(new_pex_file)))\n os.rename(pex_file, new_pex_file)\n pex_file = new_pex_file\n pex_files.append(pex_file)\n\n if not pex_files:\n notify.warning(\"No entry points found in project configuration!\")\n else:\n if pyrun:\n if any(pyrun.startswith(i) for i in ('http://', 'https://', 'file://')):\n pyrun_url = pyrun\n else:\n pyrun_cfg = dict(ctx.rituals.pyrun)\n pyrun_cfg.update(parse_qsl(pyrun.replace(os.pathsep, '&')))\n pyrun_url = (pyrun_cfg['base_url'] + '/' +\n pyrun_cfg['archive']).format(**pyrun_cfg)\n\n notify.info(\"Getting PyRun from '{}'...\".format(pyrun_url))\n with url_as_file(pyrun_url, ext='tgz') as pyrun_tarball:\n pyrun_tar = tarfile.TarFile.gzopen(pyrun_tarball)\n for pex_file in pex_files[:]:\n pyrun_exe = pyrun_tar.extractfile('./bin/pyrun')\n with open(pex_file, 'rb') as pex_handle:\n pyrun_pex_file = '{}{}-installer.sh'.format(\n pex_file[:-4], pyrun_url.rsplit('/egenix')[-1][:-4])\n with open(pyrun_pex_file, 'wb') as pyrun_pex:\n pyrun_pex.write(INSTALLER_BASH.replace('00000', '{:<5d}'.format(len(INSTALLER_BASH) + 1)))\n shutil.copyfileobj(pyrun_exe, pyrun_pex)\n shutil.copyfileobj(pex_handle, pyrun_pex)\n shutil.copystat(pex_file, pyrun_pex_file)\n notify.info(\"Wrote PEX installer to '{}'\".format(pretty_path(pyrun_pex_file)))\n pex_files.append(pyrun_pex_file)\n\n if upload:\n base_url = ctx.rituals.release.upload.base_url.rstrip('/')\n if not base_url:\n notify.failure(\"No base URL provided for uploading!\")\n\n for pex_file in pex_files:\n url = base_url + '/' + ctx.rituals.release.upload.path.lstrip('/').format(\n name=cfg.project.name, version=cfg.project.version, filename=os.path.basename(pex_file))\n notify.info(\"Uploading to '{}'...\".format(url))\n with io.open(pex_file, 'rb') as handle:\n reply = requests.put(url, data=handle.read())\n if reply.status_code in range(200, 300):\n notify.info(\"{status_code} {reason}\".format(**vars(reply)))\n else:\n notify.warning(\"{status_code} {reason}\".format(**vars(reply)))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprepare for a release.", "response": "def prep(ctx, commit=True):\n \"\"\"Prepare for a release.\"\"\"\n cfg = config.load()\n scm = scm_provider(cfg.project_root, commit=commit, ctx=ctx)\n\n # Check for uncommitted changes\n if not scm.workdir_is_clean():\n notify.failure(\"You have uncommitted changes, please commit or stash them!\")\n\n # TODO Check that changelog entry carries the current date\n\n # Rewrite 'setup.cfg'\n setup_cfg = cfg.rootjoin('setup.cfg')\n if os.path.exists(setup_cfg):\n with io.open(setup_cfg, encoding='utf-8') as handle:\n data = handle.readlines()\n changed = False\n for i, line in enumerate(data):\n if any(line.startswith(i) for i in ('tag_build', 'tag_date')):\n data[i] = '#' + data[i]\n changed = True\n if changed and commit:\n notify.info(\"Rewriting 'setup.cfg'...\")\n with io.open(setup_cfg, 'w', encoding='utf-8') as handle:\n handle.write(''.join(data))\n scm.add_file('setup.cfg')\n elif changed:\n notify.warning(\"WOULD rewrite 'setup.cfg', but --no-commit was passed\")\n else:\n notify.warning(\"Cannot rewrite 'setup.cfg', none found!\")\n\n # Update metadata and command stubs\n ctx.run('python setup.py -q develop -U')\n\n # Build a clean dist and check version number\n version = capture('python setup.py --version')\n ctx.run('invoke clean --all build --docs release.dist')\n for distfile in os.listdir('dist'):\n trailer = distfile.split('-' + version)[1]\n trailer, _ = os.path.splitext(trailer)\n if trailer and trailer[0] not in '.-':\n notify.failure(\"The version found in 'dist' seems to be\"\n \" a pre-release one! [{}{}]\".format(version, trailer))\n\n # Commit changes and tag the release\n scm.commit(ctx.rituals.release.commit.message.format(version=version))\n scm.tag(ctx.rituals.release.tag.name.format(version=version),\n ctx.rituals.release.tag.message.format(version=version))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nperforming source code checks via pylint.", "response": "def pylint(ctx, skip_tests=False, skip_root=False, reports=False):\n \"\"\"Perform source code checks via pylint.\"\"\"\n cfg = config.load()\n add_dir2pypath(cfg.project_root)\n if not os.path.exists(cfg.testjoin('__init__.py')):\n add_dir2pypath(cfg.testjoin())\n\n namelist = set()\n for package in cfg.project.get('packages', []):\n if '.' not in package:\n namelist.add(cfg.srcjoin(package))\n for module in cfg.project.get('py_modules', []):\n namelist.add(module + '.py')\n\n if not skip_tests:\n test_py = antglob.FileSet(cfg.testdir, '**/*.py')\n test_py = [cfg.testjoin(i) for i in test_py]\n if test_py:\n namelist |= set(test_py)\n\n if not skip_root:\n root_py = antglob.FileSet('.', '*.py')\n if root_py:\n namelist |= set(root_py)\n\n namelist = set([i[len(os.getcwd())+1:] if i.startswith(os.getcwd() + os.sep) else i for i in namelist])\n cmd = 'pylint'\n cmd += ' \"{}\"'.format('\" \"'.join(sorted(namelist)))\n cmd += ' --reports={0}'.format('y' if reports else 'n')\n for cfgfile in ('.pylintrc', 'pylint.rc', 'pylint.cfg', 'project.d/pylint.cfg'):\n if os.path.exists(cfgfile):\n cmd += ' --rcfile={0}'.format(cfgfile)\n break\n try:\n shell.run(cmd, report_error=False, runner=ctx.run)\n notify.info(\"OK - No problems found by pylint.\")\n except exceptions.Failure as exc:\n # Check bit flags within pylint return code\n if exc.result.return_code & 32:\n # Usage error (internal error in this code)\n notify.error(\"Usage error, bad arguments in {}?!\".format(repr(cmd)))\n raise\n else:\n bits = {\n 1: \"fatal\",\n 2: \"error\",\n 4: \"warning\",\n 8: \"refactor\",\n 16: \"convention\",\n }\n notify.warning(\"Some messages of type {} issued by pylint.\".format(\n \", \".join([text for bit, text in bits.items() if exc.result.return_code & bit])\n ))\n if exc.result.return_code & 3:\n notify.error(\"Exiting due to fatal / error message.\")\n raise"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck for uncommitted changes return True if everything is clean.", "response": "def workdir_is_clean(self, quiet=False):\n \"\"\" Check for uncommitted changes, return `True` if everything is clean.\n\n Inspired by http://stackoverflow.com/questions/3878624/.\n \"\"\"\n # Update the index\n self.run('git update-index -q --ignore-submodules --refresh', **RUN_KWARGS)\n unchanged = True\n\n # Disallow unstaged changes in the working tree\n try:\n self.run('git diff-files --quiet --ignore-submodules --', report_error=False, **RUN_KWARGS)\n except exceptions.Failure:\n unchanged = False\n if not quiet:\n notify.warning('You have unstaged changes!')\n self.run('git diff-files --name-status -r --ignore-submodules -- >&2', **RUN_KWARGS)\n\n # Disallow uncommitted changes in the index\n try:\n self.run('git diff-index --cached --quiet HEAD --ignore-submodules --', report_error=False, **RUN_KWARGS)\n except exceptions.Failure:\n unchanged = False\n if not quiet:\n notify.warning('Your index contains uncommitted changes!')\n self.run('git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2', **RUN_KWARGS)\n\n return unchanged"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntag the current workdir state with the given label.", "response": "def tag(self, label, message=None):\n \"\"\"Tag the current workdir state.\"\"\"\n options = ' -m \"{}\" -a'.format(message) if message else ''\n self.run_elective('git tag{} \"{}\"'.format(options, label))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a PEP - 440 dev version appendix to the main version number.", "response": "def pep440_dev_version(self, verbose=False, non_local=False):\n \"\"\" Return a PEP-440 dev version appendix to the main version number.\n\n Result is ``None`` if the workdir is in a release-ready state\n (i.e. clean and properly tagged).\n \"\"\"\n version = capture(\"python setup.py --version\", echo=verbose)\n if verbose:\n notify.info(\"setuptools version = '{}'\".format(version))\n\n now = '{:%Y%m%d!%H%M}'.format(datetime.now())\n tag = capture(\"git describe --long --tags --dirty='!{}'\".format(now), echo=verbose)\n if verbose:\n notify.info(\"git describe = '{}'\".format(tag))\n try:\n tag, date, time = tag.split('!')\n except ValueError:\n date = time = ''\n tag, commits, short_hash = tag.rsplit('-', 3)\n label = tag\n if re.match(r\"v[0-9]+(\\.[0-9]+)*\", label):\n label = label[1:]\n\n # Make a PEP-440 version appendix, the format is:\n # [N!]N(.N)*[{a|b|rc}N][.postN][.devN][+]\n if commits == '0' and label == version:\n pep440 = None\n else:\n local_part = [\n re.sub(r\"[^a-zA-Z0-9]+\", '.', label).strip('.'), # reduce to alphanum and dots\n short_hash,\n date + ('T' + time if time else ''),\n ]\n build_number = os.environ.get('BUILD_NUMBER', 'n/a')\n if build_number.isdigit():\n local_part.extend(['ci', build_number])\n if verbose:\n notify.info(\"Adding CI build ID #{} to version\".format(build_number))\n\n local_part = [i for i in local_part if i]\n pep440 = '.dev{}+{}'.format(commits, '.'.join(local_part).strip('.'))\n if non_local:\n pep440, _ = pep440.split('+', 1)\n\n return pep440"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndumping Jenkins project metadata for Jenkins Description Setter Plugin.", "response": "def description(_dummy_ctx, markdown=False):\n \"\"\"Dump project metadata for Jenkins Description Setter Plugin.\"\"\"\n cfg = config.load()\n markup = 'md' if markdown else 'html'\n description_file = cfg.rootjoin(\"build/project.{}\".format(markup))\n notify.banner(\"Creating {} file for Jenkins...\".format(description_file))\n\n long_description = cfg.project.long_description\n long_description = long_description.replace('\\n\\n', '

\\n

')\n long_description = re.sub(r'(\\W)``([^`]+)``(\\W)', r'\\1\\2\\3', long_description)\n\n text = DESCRIPTION_TEMPLATES[markup].format(\n keywords=', '.join(cfg.project.keywords),\n classifiers='\\n'.join(cfg.project.classifiers),\n classifiers_indented=' ' + '\\n '.join(cfg.project.classifiers),\n packages=', '.join(cfg.project.packages),\n long_description_html='

{}

'.format(long_description),\n ##data='\\n'.join([\"%s=%r\" % i for i in cfg.project.iteritems()]),\n **cfg)\n with io.open(description_file, 'w', encoding='utf-8') as handle:\n handle.write(text)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef capture(cmd, **kw):\n kw = kw.copy()\n kw['hide'] = 'out'\n if not kw.get('echo', False):\n kw['echo'] = False\n ignore_failures = kw.pop('ignore_failures', False)\n try:\n return invoke_run(cmd, **kw).stdout.strip()\n except exceptions.Failure as exc:\n if not ignore_failures:\n notify.error(\"Command `{}` failed with RC={}!\".format(cmd, exc.result.return_code,))\n raise", "response": "Run a command and return its stripped captured output."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef run(cmd, **kw):\n kw = kw.copy()\n kw.setdefault('warn', False) # make extra sure errors don't get silenced\n\n report_error = kw.pop('report_error', True)\n runner = kw.pop('runner', invoke_run)\n\n try:\n return runner(cmd, **kw)\n except exceptions.Failure as exc:\n sys.stdout.flush()\n sys.stderr.flush()\n if report_error:\n notify.error(\"Command `{}` failed with RC={}!\".format(cmd, exc.result.return_code,))\n raise\n finally:\n sys.stdout.flush()\n sys.stderr.flush()", "response": "Run a command and flush its output."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef auto_detect(workdir):\n # Any additions here also need a change to `SCM_PROVIDERS`!\n if os.path.isdir(os.path.join(workdir, '.git')) and os.path.isfile(os.path.join(workdir, '.git', 'HEAD')):\n return 'git'\n\n return 'unknown'", "response": "Detects the SCM used in the given directory."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef provider(workdir, commit=True, **kwargs):\n return SCM_PROVIDER[auto_detect(workdir)](workdir, commit=commit, **kwargs)", "response": "Factory for the correct SCM provider in workdir."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nexiting with error code and message.", "response": "def fail(message, exitcode=1):\n \"\"\"Exit with error code and message.\"\"\"\n sys.stderr.write('ERROR: {}\\n'.format(message))\n sys.stderr.flush()\n sys.exit(exitcode)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_pypi_auth(configfile='~/.pypirc'):\n pypi_cfg = ConfigParser()\n if pypi_cfg.read(os.path.expanduser(configfile)):\n try:\n user = pypi_cfg.get('pypi', 'username')\n pwd = pypi_cfg.get('pypi', 'password')\n return user, pwd\n except ConfigError:\n notify.warning(\"No PyPI credentials in '{}',\"\n \" will fall back to '~/.netrc'...\".format(configfile))\n return None", "response": "Read auth from pypi config file."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncontrols a running Sphinx autobuild process.", "response": "def watchdogctl(ctx, kill=False, verbose=True):\n \"\"\"Control / check a running Sphinx autobuild process.\"\"\"\n tries = 40 if kill else 0\n cmd = 'lsof -i TCP:{} -s TCP:LISTEN -S -Fp 2>/dev/null'.format(ctx.rituals.docs.watchdog.port)\n\n pidno = 0\n pidinfo = capture(cmd, ignore_failures=True)\n while pidinfo:\n pidline = next(filter(None, [re.match(r'^p(\\d+)$', x) for x in pidinfo.splitlines()]))\n if not pidline:\n raise ValueError(\"Standard lsof output expected (got {!r})\".format(pidinfo))\n pidno = int(pidline.group(1), 10)\n if verbose:\n ctx.run(\"ps uw {}\".format(pidno), echo=False)\n verbose = False\n\n tries -= 1\n if tries <= 0:\n break\n else:\n try:\n os.kill(pidno, 0)\n #except ProcessLookupError: # XXX Python3 only\n # break\n except OSError as exc: # Python2 has no ProcessLookupError\n if exc.errno == 3:\n break\n raise\n else:\n notify.info(\"Killing PID {}\".format(pidno))\n ctx.run(\"kill {}\".format(pidno), echo=False)\n time.sleep(.25)\n\n pid = capture(cmd, ignore_failures=True)\n\n return pidno"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef confluence(ctx, no_publish=False, clean=False, opts=''):\n cfg = config.load()\n\n if clean:\n ctx.run(\"invoke clean --docs\")\n\n cmd = ['sphinx-build', '-b', 'confluence']\n cmd.extend(['-E', '-a']) # force a full rebuild\n if opts:\n cmd.append(opts)\n cmd.extend(['.', ctx.rituals.docs.build + '_cf'])\n if no_publish:\n cmd.extend(['-Dconfluence_publish=False'])\n\n # Build docs\n notify.info(\"Starting Sphinx build...\")\n with pushd(ctx.rituals.docs.sources):\n ctx.run(' '.join(cmd), pty=True)", "response": "Build Sphinx docs and publish to Confluence."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nuploads a ZIP of built docs to the current directory.", "response": "def upload(ctx, browse=False, target=None, release='latest'):\n \"\"\"Upload a ZIP of built docs (by default to PyPI, else a WebDAV URL).\"\"\"\n cfg = config.load()\n uploader = DocsUploader(ctx, cfg, target)\n\n html_dir = os.path.join(ctx.rituals.docs.sources, ctx.rituals.docs.build)\n if not os.path.isdir(html_dir):\n notify.failure(\"No HTML docs dir found at '{}'!\".format(html_dir))\n\n url = uploader.upload(html_dir, release)\n notify.info(\"Uploaded docs to '{url}'!\".format(url=url or 'N/A'))\n if url and browse: # Open in browser?\n webbrowser.open_new_tab(url)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nprovides a zipped stream of the docs tree.", "response": "def _zipped(self, docs_base):\n \"\"\" Provide a zipped stream of the docs tree.\"\"\"\n with pushd(docs_base):\n with tempfile.NamedTemporaryFile(prefix='pythonhosted-', delete=False) as ziphandle:\n pass\n zip_name = shutil.make_archive(ziphandle.name, 'zip')\n\n notify.info(\"Uploading {:.1f} MiB from '{}' to '{}'...\"\n .format(os.path.getsize(zip_name) / 1024.0, zip_name, self.target))\n with io.open(zip_name, 'rb') as zipread:\n try:\n yield zipread\n finally:\n os.remove(ziphandle.name)\n os.remove(ziphandle.name + '.zip')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _to_webdav(self, docs_base, release):\n try:\n git_path = subprocess.check_output('git remote get-url origin 2>/dev/null', shell=True)\n except subprocess.CalledProcessError:\n git_path = ''\n else:\n git_path = git_path.decode('ascii').strip()\n git_path = git_path.replace('http://', '').replace('https://', '').replace('ssh://', '')\n git_path = re.search(r'[^:/]+?[:/](.+)', git_path)\n git_path = git_path.group(1).replace('.git', '') if git_path else ''\n url = None\n with self._zipped(docs_base) as handle:\n url_ns = dict(name=self.cfg.project.name, version=release, git_path=git_path)\n reply = requests.put(self.params['url'].format(**url_ns),\n data=handle.read(), headers={'Accept': 'application/json'})\n if reply.status_code in range(200, 300):\n notify.info(\"{status_code} {reason}\".format(**vars(reply)))\n try:\n data = reply.json()\n except ValueError as exc:\n notify.warning(\"Didn't get a JSON response! ({})\".format(exc))\n else:\n if 'downloadUri' in data: # Artifactory\n url = data['downloadUri'] + '!/index.html'\n elif reply.status_code == 301:\n url = reply.headers['location']\n else:\n data = self.cfg.copy()\n data.update(self.params)\n data.update(vars(reply))\n notify.error(\"{status_code} {reason} for PUT to {url}\".format(**data))\n\n if not url:\n notify.warning(\"Couldn't get URL from upload response!\")\n return url", "response": "Upload to WebDAV store."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef upload(self, docs_base, release):\n return getattr(self, '_to_' + self.target)(docs_base, release)", "response": "Upload docs in docs_base to the target of this uploader."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef search_file_upwards(name, base=None):\n base = base or os.getcwd()\n while base != os.path.dirname(base):\n if os.path.exists(os.path.join(base, name)):\n return base\n base = os.path.dirname(base)\n\n return None", "response": "Search for a file named name from cwd or given directory to root."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd given directory to PYTHONPATH.", "response": "def add_dir2pypath(path):\n \"\"\"Add given directory to PYTHONPATH, e.g. for pylint.\"\"\"\n py_path = os.environ.get('PYTHONPATH', '')\n if path not in py_path.split(os.pathsep):\n py_path = ''.join([path, os.pathsep if py_path else '', py_path])\n os.environ['PYTHONPATH'] = py_path"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef pushd(path):\n saved = os.getcwd()\n os.chdir(path)\n try:\n yield saved\n finally:\n os.chdir(saved)", "response": "A context that enters a given directory and restores the old state on exit."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrun a command or just echo it depending on commit.", "response": "def run_elective(self, cmd, *args, **kwargs):\n \"\"\"Run a command, or just echo it, depending on `commit`.\"\"\"\n if self._commit:\n return self.run(cmd, *args, **kwargs)\n else:\n notify.warning(\"WOULD RUN: {}\".format(cmd))\n kwargs = kwargs.copy()\n kwargs['echo'] = False\n return self.run('true', *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef banner(msg):\n if ECHO:\n _flush()\n sys.stderr.write(\"\\033[1;7;32;40m{}\\033[0m\\n\".format(msg))\n sys.stderr.flush()", "response": "Emit a banner just like Invoke s run."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef info(msg):\n _flush()\n sys.stdout.write(msg + '\\n')\n sys.stdout.flush()", "response": "Emit a normal message."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nemit a warning message.", "response": "def warning(msg):\n \"\"\"Emit a warning message.\"\"\"\n _flush()\n sys.stderr.write(\"\\033[1;7;33;40mWARNING: {}\\033[0m\\n\".format(msg))\n sys.stderr.flush()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef error(msg):\n _flush()\n sys.stderr.write(\"\\033[1;37;41mERROR: {}\\033[0m\\n\".format(msg))\n sys.stderr.flush()", "response": "Emit an error message to stderr."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget currently used devpi base URL.", "response": "def get_devpi_url(ctx):\n \"\"\"Get currently used 'devpi' base URL.\"\"\"\n cmd = 'devpi use --urls'\n lines = ctx.run(cmd, hide='out', echo=False).stdout.splitlines()\n for line in lines:\n try:\n line, base_url = line.split(':', 1)\n except ValueError:\n notify.warning('Ignoring \"{}\"!'.format(line))\n else:\n if line.split()[-1].strip() == 'simpleindex':\n return base_url.split('\\x1b')[0].strip().rstrip('/')\n\n raise LookupError(\"Cannot find simpleindex URL in '{}' output:\\n {}\".format(\n cmd, '\\n '.join(lines),\n ))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndetermine location of tasks. py.", "response": "def get_project_root():\n \"\"\" Determine location of `tasks.py`.\"\"\"\n try:\n tasks_py = sys.modules['tasks']\n except KeyError:\n return None\n else:\n return os.path.abspath(os.path.dirname(tasks_py.__file__))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load():\n cfg = Bunch(DEFAULTS)\n # TODO: override with contents of [rituals] section in setup.cfg\n\n cfg.project_root = get_project_root()\n if not cfg.project_root:\n raise RuntimeError(\"No tasks module is imported, cannot determine project root\")\n\n cfg.rootjoin = lambda *names: os.path.join(cfg.project_root, *names)\n cfg.srcjoin = lambda *names: cfg.rootjoin(cfg.srcdir, *names)\n cfg.testjoin = lambda *names: cfg.rootjoin(cfg.testdir, *names)\n cfg.cwd = os.getcwd()\n os.chdir(cfg.project_root)\n\n # this assumes an importable setup.py\n # TODO: maybe call \"python setup.py egg_info\" for metadata\n if cfg.project_root not in sys.path:\n sys.path.append(cfg.project_root)\n try:\n from setup import project # pylint: disable=no-name-in-module\n except ImportError:\n from setup import setup_args as project # pylint: disable=no-name-in-module\n cfg.project = Bunch(project)\n\n return cfg", "response": "Load and return a Bunch containing all the configuration values for the current project root."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a glob part to regex syntax.", "response": "def glob2re(part):\n \"\"\"Convert a path part to regex syntax.\"\"\"\n return \"[^/]*\".join(\n re.escape(bit).replace(r'\\[\\^', '[^').replace(r'\\[', '[').replace(r'\\]', ']')\n for bit in part.split(\"*\")\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_glob(pattern):\n if not pattern:\n return\n\n bits = pattern.split(\"/\")\n dirs, filename = bits[:-1], bits[-1]\n\n for dirname in dirs:\n if dirname == \"**\":\n yield \"(|.+/)\"\n else:\n yield glob2re(dirname) + \"/\"\n\n yield glob2re(filename)", "response": "Generate parts of regex transformed from glob pattern."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconvert the given glob spec to a compiled regex.", "response": "def compile_glob(spec):\n \"\"\"Convert the given glob `spec` to a compiled regex.\"\"\"\n parsed = \"\".join(parse_glob(spec))\n regex = \"^{0}$\".format(parsed)\n return re.compile(regex)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking patterns in order last match that includes or excludes path wins. Return None on undecided.", "response": "def included(self, path, is_dir=False):\n \"\"\"Check patterns in order, last match that includes or excludes `path` wins. Return `None` on undecided.\"\"\"\n inclusive = None\n for pattern in self.patterns:\n if pattern.is_dir == is_dir and pattern.matches(path):\n inclusive = pattern.inclusive\n\n #print('+++' if inclusive else '---', path, pattern)\n return inclusive"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef walk(self, **kwargs):\n lead = ''\n if 'with_root' in kwargs and kwargs.pop('with_root'):\n lead = self.root.rstrip(os.sep) + os.sep\n\n for base, dirs, files in os.walk(self.root, **kwargs):\n prefix = base[len(self.root):].lstrip(os.sep)\n bits = prefix.split(os.sep) if prefix else []\n\n for dirname in dirs[:]:\n path = '/'.join(bits + [dirname])\n inclusive = self.included(path, is_dir=True)\n if inclusive:\n yield lead + path + '/'\n elif inclusive is False:\n dirs.remove(dirname)\n\n for filename in files:\n path = '/'.join(bits + [filename])\n if self.included(path):\n yield lead + path", "response": "Like os. walk but generating paths relative to the root."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build(ctx, dput='', opts=''):\n # Get package metadata\n with io.open('debian/changelog', encoding='utf-8') as changes:\n metadata = re.match(r'^([^ ]+) \\(([^)]+)\\) ([^;]+); urgency=(.+)$', changes.readline().rstrip())\n if not metadata:\n notify.failure('Badly formatted top entry in changelog')\n name, version, _, _ = metadata.groups()\n\n # Build package\n ctx.run('dpkg-buildpackage {} {}'.format(ctx.rituals.deb.build.opts, opts))\n\n # Move created artifacts into \"dist\"\n if not os.path.exists('dist'):\n os.makedirs('dist')\n artifact_pattern = '{}?{}*'.format(name, re.sub(r'[^-_.a-zA-Z0-9]', '?', version))\n changes_files = []\n for debfile in glob.glob('../' + artifact_pattern):\n shutil.move(debfile, 'dist')\n if debfile.endswith('.changes'):\n changes_files.append(os.path.join('dist', os.path.basename(debfile)))\n ctx.run('ls -l dist/{}'.format(artifact_pattern))\n\n if dput:\n ctx.run('dput {} {}'.format(dput, ' '.join(changes_files)))", "response": "Build a DEB package."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncleans up the project files.", "response": "def clean(_dummy_ctx, docs=False, backups=False, bytecode=False, dist=False, # pylint: disable=redefined-outer-name\n all=False, venv=False, tox=False, extra=''): # pylint: disable=redefined-builtin\n \"\"\"Perform house-keeping.\"\"\"\n cfg = config.load()\n notify.banner(\"Cleaning up project files\")\n\n # Add patterns based on given parameters\n venv_dirs = ['bin', 'include', 'lib', 'share', 'local', '.venv']\n patterns = ['build/', 'pip-selfcheck.json']\n excludes = ['.git/', '.hg/', '.svn/', 'debian/*/']\n if docs or all:\n patterns.extend(['docs/_build/', 'doc/_build/'])\n if dist or all:\n patterns.append('dist/')\n if backups or all:\n patterns.extend(['**/*~'])\n if bytecode or all:\n patterns.extend([\n '**/*.py[co]', '**/__pycache__/', '*.egg-info/',\n cfg.srcjoin('*.egg-info/')[len(cfg.project_root)+1:],\n ])\n if venv:\n patterns.extend([i + '/' for i in venv_dirs])\n if tox:\n patterns.append('.tox/')\n else:\n excludes.append('.tox/')\n if extra:\n patterns.extend(shlex.split(extra))\n\n # Build fileset\n patterns = [antglob.includes(i) for i in patterns] + [antglob.excludes(i) for i in excludes]\n if not venv:\n # Do not scan venv dirs when not cleaning them\n patterns.extend([antglob.excludes(i + '/') for i in venv_dirs])\n fileset = antglob.FileSet(cfg.project_root, patterns)\n\n # Iterate over matches and remove them\n for name in fileset:\n notify.info('rm {0}'.format(name))\n if name.endswith('/'):\n shutil.rmtree(os.path.join(cfg.project_root, name))\n else:\n os.unlink(os.path.join(cfg.project_root, name))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef freeze(ctx, local=False):\n cmd = 'pip --disable-pip-version-check freeze{}'.format(' --local' if local else '')\n frozen = ctx.run(cmd, hide='out').stdout.replace('\\x1b', '#')\n with io.open('frozen-requirements.txt', 'w', encoding='ascii') as out:\n out.write(\"# Requirements frozen by 'pip freeze' on {}\\n\".format(isodate()))\n out.write(frozen)\n notify.info(\"Frozen {} requirements.\".format(len(frozen.splitlines()),))", "response": "Freeze currently installed requirements."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef isodate(datestamp=None, microseconds=False):\n datestamp = datestamp or datetime.datetime.now()\n if not microseconds:\n usecs = datetime.timedelta(microseconds=datestamp.microsecond)\n datestamp = datestamp - usecs\n return datestamp.isoformat(b' ' if PY2 else u' ')", "response": "Return current or given time formatted according to ISO - 8601."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwindowing allow application paths to be registered in the registry.", "response": "def _get_registered_executable(exe_name):\n \"\"\"Windows allow application paths to be registered in the registry.\"\"\"\n registered = None\n if sys.platform.startswith('win'):\n if os.path.splitext(exe_name)[1].lower() != '.exe':\n exe_name += '.exe'\n import _winreg # pylint: disable=import-error\n try:\n key = \"SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\\" + exe_name\n value = _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE, key)\n registered = (value, \"from HKLM\\\\\"+key)\n except _winreg.error:\n pass\n if registered and not os.path.exists(registered[0]):\n registered = None\n return registered"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nculling inappropriate matches. Possible reasons: - a duplicate of a previous match - not a disk file - not executable (non-Windows) If 'potential' is approved it is returned and added to 'matches'. Otherwise, None is returned.", "response": "def _cull(potential, matches, verbose=0):\n \"\"\"Cull inappropriate matches. Possible reasons:\n - a duplicate of a previous match\n - not a disk file\n - not executable (non-Windows)\n If 'potential' is approved it is returned and added to 'matches'.\n Otherwise, None is returned.\n \"\"\"\n for match in matches: # don't yield duplicates\n if _samefile(potential[0], match[0]):\n if verbose:\n sys.stderr.write(\"duplicate: %s (%s)\\n\" % potential)\n return None\n\n if not stat.S_ISREG(os.stat(potential[0]).st_mode):\n if verbose:\n sys.stderr.write(\"not a regular file: %s (%s)\\n\" % potential)\n elif not os.access(potential[0], os.X_OK):\n if verbose:\n sys.stderr.write(\"no executable access: %s (%s)\\n\" % potential)\n else:\n matches.append(potential)\n return potential\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a generator of full paths to the given command.", "response": "def whichgen(command, path=None, verbose=0, exts=None): # pylint: disable=too-many-branches, too-many-statements\n \"\"\"Return a generator of full paths to the given command.\n\n \"command\" is a the name of the executable to search for.\n \"path\" is an optional alternate path list to search. The default it\n to use the PATH environment variable.\n \"verbose\", if true, will cause a 2-tuple to be returned for each\n match. The second element is a textual description of where the\n match was found.\n \"exts\" optionally allows one to specify a list of extensions to use\n instead of the standard list for this system. This can\n effectively be used as an optimization to, for example, avoid\n stat's of \"foo.vbs\" when searching for \"foo\" and you know it is\n not a VisualBasic script but \".vbs\" is on PATHEXT. This option\n is only supported on Windows.\n\n This method returns a generator which yields either full paths to\n the given command or, if verbose, tuples of the form (, ).\n \"\"\"\n matches = []\n if path is None:\n using_given_path = 0\n path = os.environ.get(\"PATH\", \"\").split(os.pathsep)\n if sys.platform.startswith(\"win\"):\n path.insert(0, os.curdir) # implied by Windows shell\n else:\n using_given_path = 1\n\n # Windows has the concept of a list of extensions (PATHEXT env var).\n if sys.platform.startswith(\"win\"):\n if exts is None:\n exts = os.environ.get(\"PATHEXT\", \"\").split(os.pathsep)\n # If '.exe' is not in exts then obviously this is Win9x and\n # or a bogus PATHEXT, then use a reasonable default.\n for ext in exts:\n if ext.lower() == \".exe\":\n break\n else:\n exts = ['.COM', '.EXE', '.BAT']\n elif not isinstance(exts, list):\n raise TypeError(\"'exts' argument must be a list or None\")\n else:\n if exts is not None:\n raise WhichError(\"'exts' argument is not supported on platform '%s'\" % sys.platform)\n exts = []\n\n # File name cannot have path separators because PATH lookup does not\n # work that way.\n if os.sep in command or os.altsep and os.altsep in command:\n pass\n else:\n for i, dir_name in enumerate(path):\n # On windows the dir_name *could* be quoted, drop the quotes\n if sys.platform.startswith(\"win\") and len(dir_name) >= 2 and dir_name[0] == '\"' and dir_name[-1] == '\"':\n dir_name = dir_name[1:-1]\n for ext in ['']+exts:\n abs_name = os.path.abspath(os.path.normpath(os.path.join(dir_name, command+ext)))\n if os.path.isfile(abs_name):\n if using_given_path:\n from_where = \"from given path element %d\" % i\n elif not sys.platform.startswith(\"win\"):\n from_where = \"from PATH element %d\" % i\n elif i == 0:\n from_where = \"from current directory\"\n else:\n from_where = \"from PATH element %d\" % (i-1)\n match = _cull((abs_name, from_where), matches, verbose)\n if match:\n if verbose:\n yield match\n else:\n yield match[0]\n match = _get_registered_executable(command)\n if match is not None:\n match = _cull(match, matches, verbose)\n if match:\n if verbose:\n yield match\n else:\n yield match[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef which(command, path=None, verbose=0, exts=None):\n matched = whichgen(command, path, verbose, exts)\n try:\n match = next(matched)\n except StopIteration:\n raise WhichError(\"Could not find '%s' on the path.\" % command)\n else:\n return match", "response": "Return the full path to the first match of the given command on the path."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef step(self, key, chain):\n\n if chain == \"sending\":\n self.__previous_sending_chain_length = self.sending_chain_length\n\n self.__sending_chain = self.__SendingChain(key)\n\n if chain == \"receiving\":\n self.__receiving_chain = self.__ReceivingChain(key)", "response": "Perform a rachted step of the recovery process."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef decryptMessage(self, ciphertext, header, ad = None):\n\n if ad == None:\n ad = self.__ad\n\n # Try to decrypt the message using a previously saved message key\n plaintext = self.__decryptSavedMessage(ciphertext, header, ad)\n if plaintext:\n return plaintext\n\n # Check, whether the public key will trigger a dh ratchet step\n if self.triggersStep(header.dh_pub):\n # Save missed message keys for the current receiving chain\n self.__saveMessageKeys(header.pn)\n\n # Perform the step\n self.step(header.dh_pub)\n\n # Save missed message keys for the current receiving chain\n self.__saveMessageKeys(header.n)\n\n # Finally decrypt the message and return the plaintext\n return self.__decrypt(\n ciphertext,\n self.__skr.nextDecryptionKey(),\n header,\n ad\n )", "response": "Decrypt a message using this double ratchet session."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef encryptMessage(self, message, ad = None):\n\n if ad == None:\n ad = self.__ad\n\n # Prepare the header for this message\n header = Header(\n self.pub,\n self.__skr.sending_chain_length,\n self.__skr.previous_sending_chain_length\n )\n\n # Encrypt the message\n ciphertext = self.__aead.encrypt(\n message,\n self.__skr.nextEncryptionKey(),\n self._makeAD(header, ad)\n )\n\n return {\n \"header\" : header,\n \"ciphertext\" : ciphertext\n }", "response": "Encrypt a message using this double ratchet session."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nperforms a rachted step of the recovery process.", "response": "def step(self, other_pub):\n \"\"\"\n Perform a rachted step, calculating a new shared secret from the public key and\n deriving new chain keys from this secret.\n\n New Diffie-Hellman calculations are only performed if the public key is different\n from the previous one.\n\n :param other_pub: A bytes-like object encoding the public key of the other\n Diffie-Hellman ratchet to synchronize with.\n \"\"\"\n\n if self.triggersStep(other_pub):\n self.__wrapOtherPub(other_pub)\n self.__newRootKey(\"receiving\")\n\n self.__newRatchetKey()\n\n self.__newRootKey(\"sending\")"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef next(self, data):\n\n self.__length += 1\n\n result = self.__kdf.calculate(self.__key, data, 64)\n self.__key = result[:32]\n return result[32:]", "response": "Derive a new set of internal and output data from given input data and the current key and the data passed to the key derivation function."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a image file such as a png bitmap of jpeg file and convert it to a knitting pattern file.", "response": "def convert_image_to_knitting_pattern(path, colors=(\"white\", \"black\")):\n \"\"\"Load a image file such as a png bitmap of jpeg file and convert it\n to a :ref:`knitting pattern file `.\n\n :param list colors: a list of strings that should be used as\n :ref:`colors `.\n :param str path: ignore this. It is fulfilled by the loeder.\n\n Example:\n\n .. code:: python\n\n convert_image_to_knitting_pattern().path(\"image.png\").path(\"image.json\")\n \"\"\"\n image = PIL.Image.open(path)\n pattern_id = os.path.splitext(os.path.basename(path))[0]\n rows = []\n connections = []\n pattern_set = {\n \"version\": \"0.1\",\n \"type\": \"knitting pattern\",\n \"comment\": {\n \"source\": path\n },\n \"patterns\": [\n {\n \"name\": pattern_id,\n \"id\": pattern_id,\n \"rows\": rows,\n \"connections\": connections\n }\n ]}\n bbox = image.getbbox()\n if not bbox:\n return pattern_set\n white = image.getpixel((0, 0))\n min_x, min_y, max_x, max_y = bbox\n last_row_y = None\n for y in reversed(range(min_y, max_y)):\n instructions = []\n row = {\"id\": y, \"instructions\": instructions}\n rows.append(row)\n for x in range(min_x, max_x):\n if image.getpixel((x, y)) == white:\n color = colors[0]\n else:\n color = colors[1]\n instruction = {\"color\": color}\n instructions.append(instruction)\n if last_row_y is not None:\n connections.append({\"from\": {\"id\": last_row_y}, \"to\": {\"id\": y}})\n last_row_y = y\n return pattern_set"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconnecting this mesh to another one.", "response": "def connect_to(self, other_mesh):\n \"\"\"Create a connection to an other mesh.\n\n .. warning:: Both meshes need to be disconnected and one needs to be\n a consumed and the other a produced mesh. You can check if a\n connection is possible using :meth:`can_connect_to`.\n\n .. seealso:: :meth:`is_consumed`, :meth:`is_produced`,\n :meth:`can_connect_to`\n \"\"\"\n other_mesh.disconnect()\n self.disconnect()\n self._connect_to(other_mesh)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a loader for a knitting pattern set.", "response": "def new_knitting_pattern_set_loader(specification=DefaultSpecification()):\n \"\"\"Create a loader for a knitting pattern set.\n\n :param specification: a :class:`specification\n `\n for the knitting pattern set, default\n :class:`DefaultSpecification`\n \"\"\"\n parser = specification.new_parser(specification)\n loader = specification.new_loader(parser.knitting_pattern_set)\n return loader"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwalk the knitting pattern in a right - to - left fashion.", "response": "def walk(knitting_pattern):\n \"\"\"Walk the knitting pattern in a right-to-left fashion.\n\n :return: an iterable to walk the rows\n :rtype: list\n :param knittingpattern.KnittingPattern.KnittingPattern knitting_pattern: a\n knitting pattern to take the rows from\n \"\"\"\n rows_before = {} # key consumes from values\n free_rows = []\n walk = []\n for row in knitting_pattern.rows:\n rows_before_ = row.rows_before[:]\n if rows_before_:\n rows_before[row] = rows_before_\n else:\n free_rows.append(row)\n assert free_rows\n while free_rows:\n # print(\"free rows:\", free_rows)\n row = free_rows.pop(0)\n walk.append(row)\n assert row not in rows_before\n for freed_row in reversed(row.rows_after):\n todo = rows_before[freed_row]\n # print(\" freed:\", freed_row, todo)\n todo.remove(row)\n if not todo:\n del rows_before[freed_row]\n free_rows.insert(0, freed_row)\n assert not rows_before, \"everything is walked\"\n return walk"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef knitting_pattern(self, specification=None):\n from ..ParsingSpecification import new_knitting_pattern_set_loader\n if specification is None:\n loader = new_knitting_pattern_set_loader()\n else:\n loader = new_knitting_pattern_set_loader(specification)\n return loader.object(self.object())", "response": "loads a : class : knitting pattern\n from the dumped\n content\n\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the dump as a string", "response": "def string(self):\n \"\"\":return: the dump as a string\"\"\"\n if self.__text_is_expected:\n return self._string()\n else:\n return self._bytes().decode(self.__encoding)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _string(self):\n file = StringIO()\n self.__dump_to_file(file)\n file.seek(0)\n return file.read()", "response": "return the string from a : class : io. StringIO"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bytes(self):\n if self.__text_is_expected:\n return self.string().encode(self.__encoding)\n else:\n return self._bytes()", "response": "return the dump as bytes"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _bytes(self):\n file = BytesIO()\n self.__dump_to_file(file)\n file.seek(0)\n return file.read()", "response": "return a bytes object from a : class : io. BytesIO"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef file(self, file=None):\n if file is None:\n file = StringIO()\n self._file(file)\n return file", "response": "Saves the dump in text mode."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndumps the content to a file.", "response": "def _file(self, file):\n \"\"\"Dump the content to a `file`.\n \"\"\"\n if not self.__text_is_expected:\n file = BytesWrapper(file, self.__encoding)\n self.__dump_to_file(file)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _binary_file(self, file):\n if self.__text_is_expected:\n file = TextWrapper(file, self.__encoding)\n self.__dump_to_file(file)", "response": "Dump the ocntent into the file in binary mode."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsave the dump in a file named path.", "response": "def _path(self, path):\n \"\"\"Saves the dump in a file named `path`.\"\"\"\n mode, encoding = self._mode_and_encoding_for_open()\n with open(path, mode, encoding=encoding) as file:\n self.__dump_to_file(file)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _temporary_file(self, delete):\n file = NamedTemporaryFile(\"w+\", delete=delete,\n encoding=self.__encoding)\n self._file(file)\n return file", "response": "Returns a temporary file where the content is dumped to."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef temporary_path(self, extension=\"\"):\n path = NamedTemporaryFile(delete=False, suffix=extension).name\n self.path(path)\n return path", "response": "Saves the dump in a temporary file and returns its path."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _binary_temporary_file(self, delete):\n file = NamedTemporaryFile(\"wb+\", delete=delete)\n self._binary_file(file)\n return file", "response": "Returns a binary temporary file where the content is dumped to."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _convert_to_image_color(self, color):\n rgb = self._convert_color_to_rrggbb(color)\n return self._convert_rrggbb_to_image_color(rgb)", "response": "Converts a color that can be used by the image"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the pixel but convert the color before.", "response": "def _set_pixel_and_convert_color(self, x, y, color):\n \"\"\"set the pixel but convert the color before.\"\"\"\n if color is None:\n return\n color = self._convert_color_to_rrggbb(color)\n self._set_pixel(x, y, color)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the color of the pixel.", "response": "def _set_pixel(self, x, y, color):\n \"\"\"set the color of the pixel.\n\n :param color: must be a valid color in the form of \"#RRGGBB\".\n If you need to convert color, use `_set_pixel_and_convert_color()`.\n \"\"\"\n if not self.is_in_bounds(x, y):\n return\n rgb = self._convert_rrggbb_to_image_color(color)\n x -= self._min_x\n y -= self._min_y\n self._image.putpixel((x, y), rgb)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_pixel(self, x, y, color):\n self._set_pixel_and_convert_color(x, y, color)", "response": "set the pixel at x y to color"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn True if x y is inside the bounds of the base image.", "response": "def is_in_bounds(self, x, y):\n \"\"\"\n :return: whether ``(x, y)`` is inside the :ref:`bounds\n `\n :rtype: bool\n \"\"\"\n lower = self._min_x <= x and self._min_y <= y\n upper = self._max_x > x and self._max_y > y\n return lower and upper"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the pixel at the position of the color_in_grid to its color.", "response": "def set_color_in_grid(self, color_in_grid):\n \"\"\"Set the pixel at the position of the :paramref:`color_in_grid`\n to its color.\n\n :param color_in_grid: must have the following attributes:\n\n - ``color`` is the :ref:`color ` to set the pixel to\n - ``x`` is the x position of the pixel\n - ``y`` is the y position of the pixel\n\n .. seealso:: :meth:`set_pixel`, :meth:`set_colors_in_grid`\n \"\"\"\n self._set_pixel_and_convert_color(\n color_in_grid.x, color_in_grid.y, color_in_grid.color)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the id that identifies the instruction in this cache.", "response": "def get_instruction_id(self, instruction_or_id):\n \"\"\"The id that identifies the instruction in this cache.\n\n :param instruction_or_id: an :class:`instruction\n ` or an instruction id\n :return: a :func:`hashable ` object\n :rtype: tuple\n \"\"\"\n if isinstance(instruction_or_id, tuple):\n return _InstructionId(instruction_or_id)\n return _InstructionId(instruction_or_id.type,\n instruction_or_id.hex_color)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the SVG for an instruction.", "response": "def to_svg(self, instruction_or_id,\n i_promise_not_to_change_the_result=False):\n \"\"\"Return the SVG for an instruction.\n\n :param instruction_or_id: either an\n :class:`~knittingpattern.Instruction.Instruction` or an id\n returned by :meth:`get_instruction_id`\n :param bool i_promise_not_to_change_the_result:\n\n - :obj:`False`: the result is copied, you can alter it.\n - :obj:`True`: the result is directly from the cache. If you change\n the result, other calls of this function get the changed result.\n\n :return: an SVGDumper\n :rtype: knittingpattern.Dumper.SVGDumper\n \"\"\"\n return self._new_svg_dumper(lambda: self.instruction_to_svg_dict(\n instruction_or_id, not i_promise_not_to_change_the_result))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the SVG dict for the SVGBuilder.", "response": "def instruction_to_svg_dict(self, instruction_or_id, copy_result=True):\n \"\"\"Return the SVG dict for the SVGBuilder.\n\n :param instruction_or_id: the instruction or id, see\n :meth:`get_instruction_id`\n :param bool copy_result: whether to copy the result\n :rtype: dict\n\n The result is cached.\n \"\"\"\n instruction_id = self.get_instruction_id(instruction_or_id)\n if instruction_id in self._cache:\n result = self._cache[instruction_id]\n else:\n result = self._instruction_to_svg_dict(instruction_id)\n self._cache[instruction_id] = result\n if copy_result:\n result = deepcopy(result)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalls when there is a change in the instructions.", "response": "def _instructions_changed(self, change):\n \"\"\"Call when there is a change in the instructions.\"\"\"\n if change.adds():\n for index, instruction in change.items():\n if isinstance(instruction, dict):\n in_row = self._parser.instruction_in_row(self, instruction)\n self.instructions[index] = in_row\n else:\n instruction.transfer_to_row(self)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef last_consumed_mesh(self):\n for instruction in reversed(self.instructions):\n if instruction.consumes_meshes():\n return instruction.last_consumed_mesh\n raise IndexError(\"{} consumes no meshes\".format(self))", "response": "The last consumed mesh."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef first_produced_mesh(self):\n for instruction in self.instructions:\n if instruction.produces_meshes():\n return instruction.first_produced_mesh\n raise IndexError(\"{} produces no meshes\".format(self))", "response": "The first produced mesh."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef first_consumed_mesh(self):\n for instruction in self.instructions:\n if instruction.consumes_meshes():\n return instruction.first_consumed_mesh\n raise IndexError(\"{} consumes no meshes\".format(self))", "response": "The first consumed mesh."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef rows_before(self):\n rows_before = []\n for mesh in self.consumed_meshes:\n if mesh.is_produced():\n row = mesh.producing_row\n if rows_before not in rows_before:\n rows_before.append(row)\n return rows_before", "response": "The rows that produce the row\n instructions before this row."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef at(self, index):\n keys = list(self._items.keys())\n key = keys[index]\n return self[key]", "response": "Get the object at an index."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nload all files from a folder recursively.", "response": "def folder(self, folder):\n \"\"\"Load all files from a folder recursively.\n\n Depending on :meth:`chooses_path` some paths may not be loaded.\n Every loaded path is processed and returned part of the returned list.\n\n :param str folder: the folder to load the files from\n :rtype: list\n :return: a list of the results of the processing steps of the loaded\n files\n \"\"\"\n result = []\n for root, _, files in os.walk(folder):\n for file in files:\n path = os.path.join(root, file)\n if self._chooses_path(path):\n result.append(self.path(path))\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the absolute path for the folder relative to the module_location.", "response": "def _relative_to_absolute(self, module_location, folder):\n \"\"\":return: the absolute path for the `folder` relative to\n the module_location.\n :rtype: str\n \"\"\"\n if os.path.isfile(module_location):\n path = os.path.dirname(module_location)\n elif os.path.isdir(module_location):\n path = module_location\n else:\n module_folder = os.path.dirname(module_location)\n if module_folder:\n path = module_folder\n else:\n __import__(module_location)\n module = sys.modules[module_location]\n path = os.path.dirname(module.__file__)\n absolute_path = os.path.join(path, folder)\n return absolute_path"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nload a folder located relative to a module and return the processed result.", "response": "def relative_folder(self, module, folder):\n \"\"\"Load a folder located relative to a module and return the processed\n result.\n\n :param str module: can be\n\n - a path to a folder\n - a path to a file\n - a module name\n\n :param str folder: the path of a folder relative to :paramref:`module`\n :return: a list of the results of the processing\n :rtype: list\n\n Depending on :meth:`chooses_path` some paths may not be loaded.\n Every loaded path is processed and returned part of the returned list.\n You can use :meth:`choose_paths` to find out which paths are chosen to\n load.\n \"\"\"\n folder = self._relative_to_absolute(module, folder)\n return self.folder(folder)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef relative_file(self, module, file):\n path = self._relative_to_absolute(module, file)\n return self.path(path)", "response": "Load a file relative to a module."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef example(self, relative_path):\n example_path = os.path.join(\"examples\", relative_path)\n return self.relative_file(__file__, example_path)", "response": "Load an example from the knitting pattern examples."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef url(self, url, encoding=\"UTF-8\"):\n import urllib.request\n with urllib.request.urlopen(url) as file:\n webpage_content = file.read()\n webpage_content = webpage_content.decode(encoding)\n return self.string(webpage_content)", "response": "load and process the content behind a url"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef string(self, string):\n object_ = json.loads(string)\n return self.object(object_)", "response": "Load an object from a string and return the processed JSON content\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the value behind key in the specification.", "response": "def get(self, key, default=None):\n \"\"\"\n :return: the value behind :paramref:`key` in the specification.\n If no value was found, :paramref:`default` is returned.\n :param key: a :ref:`specification key `\n \"\"\"\n for base in self.__specification:\n if key in base:\n return base[key]\n return default"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndump a knitting pattern to a file.", "response": "def _dump_knitting_pattern(self, file):\n \"\"\"dump a knitting pattern to a file.\"\"\"\n knitting_pattern_set = self.__on_dump()\n knitting_pattern = knitting_pattern_set.patterns.at(0)\n layout = GridLayout(knitting_pattern)\n builder = AYABPNGBuilder(*layout.bounding_box)\n builder.set_colors_in_grid(layout.walk_instructions())\n builder.write_to_file(file)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating an iterable that contains each element once.", "response": "def unique(iterables):\n \"\"\"Create an iterable from the iterables that contains each element once.\n\n :return: an iterable over the iterables. Each element of the result\n appeared only once in the result. They are ordered by the first\n occurrence in the iterables.\n \"\"\"\n included_elements = set()\n\n def included(element):\n result = element in included_elements\n included_elements.add(element)\n return result\n return [element for elements in iterables for element in elements\n if not included(element)]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngo through the layout and build the SVG.", "response": "def build_SVG_dict(self):\n \"\"\"Go through the layout and build the SVG.\n\n :return: an xml dict that can be exported using a\n :class:`~knittingpattern.Dumper.XMLDumper`\n :rtype: dict\n \"\"\"\n zoom = self._zoom\n layout = self._layout\n builder = self._builder\n bbox = list(map(lambda f: f * zoom, layout.bounding_box))\n builder.bounding_box = bbox\n flip_x = bbox[2] + bbox[0] * 2\n flip_y = bbox[3] + bbox[1] * 2\n instructions = list(layout.walk_instructions(\n lambda i: (flip_x - (i.x + i.width) * zoom,\n flip_y - (i.y + i.height) * zoom,\n i.instruction)))\n instructions.sort(key=lambda x_y_i: x_y_i[2].render_z)\n for x, y, instruction in instructions:\n render_z = instruction.render_z\n z_id = (\"\" if not render_z else \"-{}\".format(render_z))\n layer_id = \"row-{}{}\".format(instruction.row.id, z_id)\n def_id = self._register_instruction_in_defs(instruction)\n scale = self._symbol_id_to_scale[def_id]\n group = {\n \"@class\": \"instruction\",\n \"@id\": \"instruction-{}\".format(instruction.id),\n \"@transform\": \"translate({},{}),scale({})\".format(\n x, y, scale)\n }\n builder.place_svg_use(def_id, layer_id, group)\n builder.insert_defs(self._instruction_type_color_to_symbol.values())\n return builder.get_svg_dict()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a definition for the specified instruction.", "response": "def _register_instruction_in_defs(self, instruction):\n \"\"\"Create a definition for the instruction.\n\n :return: the id of a symbol in the defs for the specified\n :paramref:`instruction`\n :rtype: str\n\n If no symbol yet exists in the defs for the :paramref:`instruction` a\n symbol is created and saved using :meth:`_make_symbol`.\n \"\"\"\n type_ = instruction.type\n color_ = instruction.color\n instruction_to_svg_dict = \\\n self._instruction_to_svg.instruction_to_svg_dict\n instruction_id = \"{}:{}\".format(type_, color_)\n defs_id = instruction_id + \":defs\"\n if instruction_id not in self._instruction_type_color_to_symbol:\n svg_dict = instruction_to_svg_dict(instruction)\n self._compute_scale(instruction_id, svg_dict)\n symbol = self._make_definition(svg_dict, instruction_id)\n self._instruction_type_color_to_symbol[defs_id] = \\\n symbol[DEFINITION_HOLDER].pop(\"defs\", {})\n self._instruction_type_color_to_symbol[instruction_id] = symbol\n return instruction_id"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a symbol out of the supplied SVG dictionary.", "response": "def _make_definition(self, svg_dict, instruction_id):\n \"\"\"Create a symbol out of the supplied :paramref:`svg_dict`.\n\n :param dict svg_dict: dictionary containing the SVG for the\n instruction currently processed\n :param str instruction_id: id that will be assigned to the symbol\n \"\"\"\n instruction_def = svg_dict[\"svg\"]\n blacklisted_elements = [\"sodipodi:namedview\", \"metadata\"]\n whitelisted_attributes = [\"@sodipodi:docname\"]\n symbol = OrderedDict({\"@id\": instruction_id})\n for content, value in instruction_def.items():\n if content.startswith('@'):\n if content in whitelisted_attributes:\n symbol[content] = value\n elif content not in blacklisted_elements:\n symbol[content] = value\n return {DEFINITION_HOLDER: symbol}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _compute_scale(self, instruction_id, svg_dict):\n bbox = list(map(float, svg_dict[\"svg\"][\"@viewBox\"].split()))\n scale = self._zoom / (bbox[3] - bbox[1])\n self._symbol_id_to_scale[instruction_id] = scale", "response": "Compute the scale of an instruction svg."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef to_svg(self, zoom):\n def on_dump():\n \"\"\"Dump the knitting pattern to the file.\n\n :return: the SVG XML structure as dictionary.\n \"\"\"\n knitting_pattern = self.patterns.at(0)\n layout = GridLayout(knitting_pattern)\n instruction_to_svg = default_instruction_svg_cache()\n builder = SVGBuilder()\n kp_to_svg = KnittingPatternToSVG(knitting_pattern, layout,\n instruction_to_svg, builder, zoom)\n return kp_to_svg.build_SVG_dict()\n return XMLDumper(on_dump)", "response": "Create an SVG from the knitting pattern set."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_new_pattern(self, id_, name=None):\n if name is None:\n name = id_\n pattern = self._parser.new_pattern(id_, name)\n self._patterns.append(pattern)\n return pattern", "response": "Add a new pattern to the set."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef to_svg(self, converter=None):\n if converter is None:\n from knittingpattern.convert.InstructionSVGCache import \\\n default_svg_cache\n converter = default_svg_cache()\n return converter.to_svg(self)", "response": "Return a SVGDumper for this instruction."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef transfer_to_row(self, new_row):\n if new_row != self._row:\n index = self.get_index_in_row()\n if index is not None:\n self._row.instructions.pop(index)\n self._row = new_row", "response": "Transfer this instruction to a new row."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_index_in_row(self):\n expected_index = self._cached_index_in_row\n instructions = self._row.instructions\n if expected_index is not None and \\\n 0 <= expected_index < len(instructions) and \\\n instructions[expected_index] is self:\n return expected_index\n for index, instruction_in_row in enumerate(instructions):\n if instruction_in_row is self:\n self._cached_index_in_row = index\n return index\n return None", "response": "Returns the index of the instruction in the row or None if the instruction is not in the row."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef next_instruction_in_row(self):\n index = self.index_in_row + 1\n if index >= len(self.row_instructions):\n return None\n return self.row_instructions[index]", "response": "The next instruction in the row."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the index of the first produced mesh in the row that consumes it.", "response": "def index_of_first_produced_mesh_in_row(self):\n \"\"\"Index of the first produced mesh in the row that consumes it.\n\n :return: an index of the first produced mesh of rows produced meshes\n :rtype: int\n\n .. note:: If the instruction :meth:`produces meshes\n `, this is the index of the first\n mesh the instruction produces in all the meshes of the row.\n If the instruction does not produce meshes, the index of the mesh is\n returned as if the instruction had produced a mesh.\n\n .. code::\n\n if instruction.produces_meshes():\n index = instruction.index_of_first_produced_mesh_in_row\n\n \"\"\"\n index = 0\n for instruction in self.row_instructions:\n if instruction is self:\n break\n index += instruction.number_of_produced_meshes\n else:\n self._raise_not_found_error()\n return index"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef index_of_first_consumed_mesh_in_row(self):\n index = 0\n for instruction in self.row_instructions:\n if instruction is self:\n break\n index += instruction.number_of_consumed_meshes\n else:\n self._raise_not_found_error()\n return index", "response": "The index of the first consumed mesh in this instruction in its row."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _start(self):\n self._instruction_library = self._spec.new_default_instructions()\n self._as_instruction = self._instruction_library.as_instruction\n self._id_cache = {}\n self._pattern_set = None\n self._inheritance_todos = []\n self._instruction_todos = []", "response": "Initialize the parsing process."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef knitting_pattern_set(self, values):\n self._start()\n pattern_collection = self._new_pattern_collection()\n self._fill_pattern_collection(pattern_collection, values)\n self._create_pattern_set(pattern_collection, values)\n return self._pattern_set", "response": "Parse a knitting pattern set."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _finish_inheritance(self):\n while self._inheritance_todos:\n prototype, parent_id = self._inheritance_todos.pop()\n parent = self._id_cache[parent_id]\n prototype.inherit_from(parent)", "response": "Finish those who still need to inherit."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfinish those who still need to inherit.", "response": "def _finish_instructions(self):\n \"\"\"Finish those who still need to inherit.\"\"\"\n while self._instruction_todos:\n row = self._instruction_todos.pop()\n instructions = row.get(INSTRUCTIONS, [])\n row.instructions.extend(instructions)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfilling a pattern collection.", "response": "def _fill_pattern_collection(self, pattern_collection, values):\n \"\"\"Fill a pattern collection.\"\"\"\n pattern = values.get(PATTERNS, [])\n for pattern_to_parse in pattern:\n parsed_pattern = self._pattern(pattern_to_parse)\n pattern_collection.append(parsed_pattern)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef instruction_in_row(self, row, specification):\n whole_instruction_ = self._as_instruction(specification)\n return self._spec.new_instruction_in_row(row, whole_instruction_)", "response": "Parse an instruction.\n from the row."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a new knitting pattern.", "response": "def new_pattern(self, id_, name, rows=None):\n \"\"\"Create a new knitting pattern.\n\n If rows is :obj:`None` it is replaced with the\n :meth:`new_row_collection`.\n \"\"\"\n if rows is None:\n rows = self.new_row_collection()\n return self._spec.new_pattern(id_, name, rows, self)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _rows(self, spec):\n rows = self.new_row_collection()\n for row in spec:\n rows.append(self._row(row))\n return rows", "response": "Parse a collection of rows."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _connect_rows(self, connections):\n for connection in connections:\n from_row_id = self._to_id(connection[FROM][ID])\n from_row = self._id_cache[from_row_id]\n from_row_start_index = connection[FROM].get(START, DEFAULT_START)\n from_row_number_of_possible_meshes = \\\n from_row.number_of_produced_meshes - from_row_start_index\n to_row_id = self._to_id(connection[TO][ID])\n to_row = self._id_cache[to_row_id]\n to_row_start_index = connection[TO].get(START, DEFAULT_START)\n to_row_number_of_possible_meshes = \\\n to_row.number_of_consumed_meshes - to_row_start_index\n meshes = min(from_row_number_of_possible_meshes,\n to_row_number_of_possible_meshes)\n # TODO: test all kinds of connections\n number_of_meshes = connection.get(MESHES, meshes)\n from_row_stop_index = from_row_start_index + number_of_meshes\n to_row_stop_index = to_row_start_index + number_of_meshes\n assert 0 <= from_row_start_index <= from_row_stop_index\n produced_meshes = from_row.produced_meshes[\n from_row_start_index:from_row_stop_index]\n assert 0 <= to_row_start_index <= to_row_stop_index\n consumed_meshes = to_row.consumed_meshes[\n to_row_start_index:to_row_stop_index]\n assert len(produced_meshes) == len(consumed_meshes)\n mesh_pairs = zip(produced_meshes, consumed_meshes)\n for produced_mesh, consumed_mesh in mesh_pairs:\n produced_mesh.connect_to(consumed_mesh)", "response": "Connect the parsed rows."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_type(self, values):\n if TYPE not in values:\n self._error(\"No pattern type given but should be \"\n \"\\\"{}\\\"\".format(KNITTING_PATTERN_TYPE))\n type_ = values[TYPE]\n if type_ != KNITTING_PATTERN_TYPE:\n self._error(\"Wrong pattern type. Type is \\\"{}\\\" \"\n \"but should be \\\"{}\\\"\"\n \"\".format(type_, KNITTING_PATTERN_TYPE))\n return type_", "response": "Returns the type of a knitting pattern set."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _create_pattern_set(self, pattern, values):\n type_ = self._get_type(values)\n version = self._get_version(values)\n comment = values.get(COMMENT)\n self._pattern_set = self._spec.new_pattern_set(\n type_, version, pattern, self, comment\n )", "response": "Create a new pattern set."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_row(self, id_):\n row = self._parser.new_row(id_)\n self._rows.append(row)\n return row", "response": "Add a new row to the pattern."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef write(self, bytes_):\n string = bytes_.decode(self._encoding)\n self._file.write(string)", "response": "Write bytes to the file."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a string to the file.", "response": "def write(self, string):\n \"\"\"Write a string to the file.\"\"\"\n bytes_ = string.encode(self._encoding)\n self._file.write(bytes_)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nplaces the SVG content at x y in the layer with the id layer_id.", "response": "def place(self, x, y, svg, layer_id):\n \"\"\"Place the :paramref:`svg` content at ``(x, y)`` position\n in the SVG, in a layer with the id :paramref:`layer_id`.\n\n :param float x: the x position of the svg\n :param float y: the y position of the svg\n :param str svg: the SVG to place at ``(x, y)``\n :param str layer_id: the id of the layer that this\n :paramref:`svg` should be placed inside\n\n \"\"\"\n content = xmltodict.parse(svg)\n self.place_svg_dict(x, y, content, layer_id)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef place_svg_dict(self, x, y, svg_dict, layer_id, group=None):\n if group is None:\n group = {}\n group_ = {\n \"@transform\": \"translate({},{})\".format(x, y),\n \"g\": list(svg_dict.values())\n }\n group_.update(group)\n layer = self._get_layer(layer_id)\n layer[\"g\"].append(group_)", "response": "Same as place but with a dictionary as svg_dict."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_layer(self, layer_id):\n if layer_id not in self._layer_id_to_layer:\n self._svg.setdefault(\"g\", [])\n layer = {\n \"g\": [],\n \"@inkscape:label\": layer_id,\n \"@id\": layer_id,\n \"@inkscape:groupmode\": \"layer\",\n \"@class\": \"row\"\n }\n self._layer_id_to_layer[layer_id] = layer\n self._svg[\"g\"].append(layer)\n return self._layer_id_to_layer[layer_id]", "response": "Returns the layer with the given id. If the layer with the given id does not exist it is created."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef insert_defs(self, defs):\n if self._svg[\"defs\"] is None:\n self._svg[\"defs\"] = {}\n for def_ in defs:\n for key, value in def_.items():\n if key.startswith(\"@\"):\n continue\n if key not in self._svg[\"defs\"]:\n self._svg[\"defs\"][key] = []\n if not isinstance(value, list):\n value = [value]\n self._svg[\"defs\"][key].extend(value)", "response": "Adds the defs to the SVG structure."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwriting the current SVG to the file - like object.", "response": "def write_to_file(self, file):\n \"\"\"Writes the current SVG to the :paramref:`file`.\n\n :param file: a file-like object\n \"\"\"\n xmltodict.unparse(self._structure, file, pretty=True)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _width(self):\n layout = self._instruction.get(GRID_LAYOUT)\n if layout is not None:\n width = layout.get(WIDTH)\n if width is not None:\n return width\n return self._instruction.number_of_consumed_meshes", "response": "For self. width return the number of consumed meshes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexpand the row into the list of items to be processed.", "response": "def _expand(self, row, consumed_position, passed):\n \"\"\"Add the arguments `(args, kw)` to `_walk` to the todo list.\"\"\"\n self._todo.append((row, consumed_position, passed))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _step(self, row, position, passed):\n if row in passed or not self._row_should_be_placed(row, position):\n return\n self._place_row(row, position)\n passed = [row] + passed\n # print(\"{}{} at\\t{} {}\".format(\" \" * len(passed), row, position,\n # passed))\n for i, produced_mesh in enumerate(row.produced_meshes):\n self._expand_produced_mesh(produced_mesh, i, position, passed)\n for i, consumed_mesh in enumerate(row.consumed_meshes):\n self._expand_consumed_mesh(consumed_mesh, i, position, passed)", "response": "Walk through the knitting pattern by expanding an row."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexpands the consumed meshes", "response": "def _expand_consumed_mesh(self, mesh, mesh_index, row_position, passed):\n \"\"\"expand the consumed meshes\"\"\"\n if not mesh.is_produced():\n return\n row = mesh.producing_row\n position = Point(\n row_position.x + mesh.index_in_producing_row - mesh_index,\n row_position.y - INSTRUCTION_HEIGHT\n )\n self._expand(row, position, passed)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexpands the produced meshes", "response": "def _expand_produced_mesh(self, mesh, mesh_index, row_position, passed):\n \"\"\"expand the produced meshes\"\"\"\n if not mesh.is_consumed():\n return\n row = mesh.consuming_row\n position = Point(\n row_position.x - mesh.index_in_consuming_row + mesh_index,\n row_position.y + INSTRUCTION_HEIGHT\n )\n self._expand(row, position, passed)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning whether or not this instruction should be placed at the given row.", "response": "def _row_should_be_placed(self, row, position):\n \"\"\":return: whether to place this instruction\"\"\"\n placed_row = self._rows_in_grid.get(row)\n return placed_row is None or placed_row.y < position.y"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nplaces the instruction on a grid", "response": "def _place_row(self, row, position):\n \"\"\"place the instruction on a grid\"\"\"\n self._rows_in_grid[row] = RowInGrid(row, position)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwalking through all the instructions that are _todo.", "response": "def _walk(self):\n \"\"\"Loop through all the instructions that are `_todo`.\"\"\"\n while self._todo:\n args = self._todo.pop(0)\n self._step(*args)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns an InstructionInGrid object for the instruction", "response": "def instruction_in_grid(self, instruction):\n \"\"\"Returns an `InstructionInGrid` object for the `instruction`\"\"\"\n row_position = self._rows_in_grid[instruction.row].xy\n x = instruction.index_of_first_consumed_mesh_in_row\n position = Point(row_position.x + x, row_position.y)\n return InstructionInGrid(instruction, position)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn True if this connection is visible if it is longer than 0.", "response": "def is_visible(self):\n \"\"\":return: is this connection is visible\n :rtype: bool\n\n A connection is visible if it is longer that 0.\"\"\"\n if self._start.y + 1 < self._stop.y:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\niterating over instructions. :return: an iterator over :class:`instructions in grid ` :param mapping: funcion to map the result .. code:: python for pos, c in layout.walk_instructions(lambda i: (i.xy, i.color)): print(\"color {} at {}\".format(c, pos))", "response": "def walk_instructions(self, mapping=identity):\n \"\"\"Iterate over instructions.\n\n :return: an iterator over :class:`instructions in grid\n `\n :param mapping: funcion to map the result\n\n .. code:: python\n\n for pos, c in layout.walk_instructions(lambda i: (i.xy, i.color)):\n print(\"color {} at {}\".format(c, pos))\n\n \"\"\"\n instructions = chain(*self.walk_rows(lambda row: row.instructions))\n return map(mapping, instructions)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef walk_rows(self, mapping=identity):\n row_in_grid = self._walk.row_in_grid\n return map(lambda row: mapping(row_in_grid(row)), self._rows)", "response": "Iterate over rows.\n\n ."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef walk_connections(self, mapping=identity):\n for start in self.walk_instructions():\n for stop_instruction in start.instruction.consuming_instructions:\n if stop_instruction is None:\n continue\n stop = self._walk.instruction_in_grid(stop_instruction)\n connection = Connection(start, stop)\n if connection.is_visible():\n # print(\"connection:\",\n # connection.start.instruction,\n # connection.stop.instruction)\n yield mapping(connection)", "response": "Iterate over connections between instructions."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nprocessing the :paramref:`path`. :param str path: the path to load an svg from", "response": "def _process_loaded_object(self, path):\n \"\"\"process the :paramref:`path`.\n\n :param str path: the path to load an svg from\n \"\"\"\n file_name = os.path.basename(path)\n name = os.path.splitext(file_name)[0]\n with open(path) as file:\n string = file.read()\n self._instruction_type_to_file_content[name] = string"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef instruction_to_svg_dict(self, instruction):\n instruction_type = instruction.type\n if instruction_type in self._instruction_type_to_file_content:\n svg = self._instruction_type_to_file_content[instruction_type]\n return self._set_fills_in_color_layer(svg, instruction.hex_color)\n return self.default_instruction_to_svg_dict(instruction)", "response": "Converts an instruction to an xml - dictionary with the same content as\n ."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _set_fills_in_color_layer(self, svg_string, color):\n structure = xmltodict.parse(svg_string)\n if color is None:\n return structure\n layers = structure[\"svg\"][\"g\"]\n if not isinstance(layers, list):\n layers = [layers]\n for layer in layers:\n if not isinstance(layer, dict):\n continue\n if layer.get(\"@inkscape:label\") == \"color\" and \\\n layer.get(\"@inkscape:groupmode\") == \"layer\":\n for key, elements in layer.items():\n if key.startswith(\"@\") or key.startswith(\"#\"):\n continue\n if not isinstance(elements, list):\n elements = [elements]\n for element in elements:\n style = element.get(\"@style\", None)\n if style:\n style = style.split(\";\")\n processed_style = []\n for style_element in style:\n if style_element.startswith(\"fill:\"):\n style_element = \"fill:\" + color\n processed_style.append(style_element)\n style = \";\".join(processed_style)\n element[\"@style\"] = style\n return structure", "response": "replaces fill colors in color layer with color"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef default_instruction_to_svg(self, instruction):\n svg_dict = self.default_instruction_to_svg_dict(instruction)\n return xmltodict.unparse(svg_dict)", "response": "As a method to get the default svg for an instruction."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns an xml - dictionary with the same content as the default. svg file.", "response": "def default_instruction_to_svg_dict(self, instruction):\n \"\"\"Returns an xml-dictionary with the same content as\n :meth:`default_instruction_to_svg`\n\n If no file ``default.svg`` was loaded, an empty svg-dict is returned.\n \"\"\"\n instruction_type = instruction.type\n default_type = \"default\"\n rep_str = \"{instruction.type}\"\n if default_type not in self._instruction_type_to_file_content:\n return {\"svg\": \"\"}\n default_svg = self._instruction_type_to_file_content[default_type]\n default_svg = default_svg.replace(rep_str, instruction_type)\n colored_svg = self._set_fills_in_color_layer(default_svg,\n instruction.hex_color)\n return colored_svg"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndumping to the file", "response": "def _dump_to_file(self, file):\n \"\"\"dump to the file\"\"\"\n xmltodict.unparse(self.object(), file, pretty=True)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd an instruction specification", "response": "def add_instruction(self, specification):\n \"\"\"Add an instruction specification\n\n :param specification: a specification with a key\n :data:`knittingpattern.Instruction.TYPE`\n\n .. seealso:: :meth:`as_instruction`\n \"\"\"\n instruction = self.as_instruction(specification)\n self._type_to_instruction[instruction.type] = instruction"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef as_instruction(self, specification):\n instruction = self._instruction_class(specification)\n type_ = instruction.type\n if type_ in self._type_to_instruction:\n instruction.inherit_from(self._type_to_instruction[type_])\n return instruction", "response": "Convert the specification into an instruction."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the log - determinant of the object.", "response": "def logdet(self):\n \"\"\"\n Log of \uff5cK\uff5c.\n\n Returns\n -------\n float\n Log-determinant of K.\n \"\"\"\n from numpy.linalg import slogdet\n\n K = self.value()\n\n sign, logdet = slogdet(K)\n if sign != 1.0:\n msg = \"The estimated determinant of K is not positive: \"\n msg += f\" ({sign}, {logdet}).\"\n raise RuntimeError(msg)\n\n return logdet"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the value of the COvariance matrix.", "response": "def value(self):\n \"\"\"\n Covariance matrix.\n\n Returns\n -------\n K : ndarray\n Matrix K = LL\u1d40 + \u03f5I, for a very small positive number \u03f5.\n \"\"\"\n K = dot(self.L, self.L.T)\n return K + self._epsilon * eye(K.shape[0])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef gradient(self):\n L = self.L\n self._grad_Lu[:] = 0\n\n for i in range(len(self._tril1[0])):\n row = self._tril1[0][i]\n col = self._tril1[1][i]\n self._grad_Lu[row, :, i] = L[:, col]\n self._grad_Lu[:, row, i] += L[:, col]\n\n m = len(self._tril1[0])\n for i in range(len(self._diag[0])):\n row = self._diag[0][i]\n col = self._diag[1][i]\n self._grad_Lu[row, :, m + i] = L[row, col] * L[:, col]\n self._grad_Lu[:, row, m + i] += L[row, col] * L[:, col]\n\n return {\"Lu\": self._grad_Lu}", "response": "Returns the gradient of the log likelihood matrix over the lower triangular part of L."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef Ge(self):\n\n from scipy.linalg import svd\n from numpy_sugar.linalg import ddot\n\n U, S, _ = svd(self._G, full_matrices=False, check_finite=False)\n if U.shape[1] < self._G.shape[1]:\n return ddot(U, S)\n return self._G", "response": "Returns the result of US from the SVD decomposition G = USV\u1d40."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nlistening to parameters change.", "response": "def listen(self, func):\n \"\"\"\n Listen to parameters change.\n\n Parameters\n ----------\n func : callable\n Function to be called when a parameter changes.\n \"\"\"\n self._C0.listen(func)\n self._C1.listen(func)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nproviding Lh and D.", "response": "def _LhD(self):\n \"\"\"\n Implements L\u2095 and D.\n\n Returns\n -------\n Lh : ndarray\n U\u2095\u1d40 S\u2081\u207b\u00bd U\u2081\u1d40.\n D : ndarray\n (S\u2095 \u2297 S\u2093 + I\u2095\u2093)\u207b\u00b9.\n \"\"\"\n from numpy_sugar.linalg import ddot\n\n self._init_svd()\n if self._cache[\"LhD\"] is not None:\n return self._cache[\"LhD\"]\n S1, U1 = self.C1.eigh()\n U1S1 = ddot(U1, 1 / sqrt(S1))\n Sh, Uh = eigh(U1S1.T @ self.C0.value() @ U1S1)\n self._cache[\"LhD\"] = {\n \"Lh\": (U1S1 @ Uh).T,\n \"D\": 1 / (kron(Sh, self._Sx) + 1),\n \"De\": 1 / (kron(Sh, self._Sxe) + 1),\n }\n return self._cache[\"LhD\"]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the value of the object as a numpy array.", "response": "def value(self):\n \"\"\"\n Covariance matrix K = C\u2080 \u2297 GG\u1d40 + C\u2081 \u2297 I.\n\n Returns\n -------\n K : ndarray\n C\u2080 \u2297 GG\u1d40 + C\u2081 \u2297 I.\n \"\"\"\n C0 = self._C0.value()\n C1 = self._C1.value()\n return kron(C0, self._GG) + kron(C1, self._I)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the gradient of the logarithm of the logarithm of the current logarithm of the given vector.", "response": "def gradient_dot(self, v):\n \"\"\"\n Implements \u2202K\u22c5v.\n\n Parameters\n ----------\n v : array_like\n Vector from \u2202K\u22c5v.\n\n Returns\n -------\n C0.Lu : ndarray\n \u2202K\u22c5v, where the gradient is taken over the C\u2080 parameters.\n C1.Lu : ndarray\n \u2202K\u22c5v, where the gradient is taken over the C\u2081 parameters.\n \"\"\"\n self._init_svd()\n V = unvec(v, (self.G.shape[0], -1) + v.shape[1:])\n r = {}\n\n C = self._C0.gradient()[\"Lu\"]\n r[\"C0.Lu\"] = tensordot(V.T @ self.G @ self.G.T, C, axes=([-2], [0]))\n r[\"C0.Lu\"] = r[\"C0.Lu\"].reshape(V.shape[2:] + (-1,) + (C.shape[-1],), order=\"F\")\n\n C = self._C1.gradient()[\"Lu\"]\n r[\"C1.Lu\"] = tensordot(V.T, C, axes=([-2], [0]))\n r[\"C1.Lu\"] = r[\"C1.Lu\"].reshape(V.shape[2:] + (-1,) + (C.shape[-1],), order=\"F\")\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the solution x to the product K.", "response": "def solve(self, v):\n \"\"\"\n Implements the product K\u207b\u00b9\u22c5v.\n\n Parameters\n ----------\n v : array_like\n Array to be multiplied.\n\n Returns\n -------\n x : ndarray\n Solution x to the equation K\u22c5x = y.\n \"\"\"\n from numpy_sugar.linalg import ddot\n\n self._init_svd()\n L = kron(self.Lh, self.Lx)\n return L.T @ ddot(self.D, L @ v, left=True)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef logdet(self):\n self._init_svd()\n return -log(self._De).sum() + self.G.shape[0] * self.C1.logdet()", "response": "Returns the log - determinant of the current object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef logdet_gradient(self):\n from numpy_sugar.linalg import dotd\n\n self._init_svd()\n\n dC0 = self._C0.gradient()[\"Lu\"]\n grad_C0 = zeros_like(self._C0.Lu)\n for i in range(self._C0.Lu.shape[0]):\n t = kron(dotd(self.Lh, dC0[..., i] @ self.Lh.T), self._diag_LxGGLxe)\n grad_C0[i] = (self._De * t).sum()\n\n dC1 = self._C1.gradient()[\"Lu\"]\n grad_C1 = zeros_like(self._C1.Lu)\n p = self._Sxe.shape[0]\n np = self._G.shape[0] - p\n for i in range(self._C1.Lu.shape[0]):\n t = (dotd(self.Lh, dC1[..., i] @ self.Lh.T) * np).sum()\n t1 = kron(dotd(self.Lh, dC1[..., i] @ self.Lh.T), eye(p))\n t += (self._De * t1).sum()\n grad_C1[i] = t\n\n return {\"C0.Lu\": grad_C0, \"C1.Lu\": grad_C1}", "response": "Calculates the log - derivative of the log - derivative of the current state of the object."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nimplementing L ( \u2202K ) L\u1d40v.", "response": "def LdKL_dot(self, v, v1=None):\n \"\"\"\n Implements L(\u2202K)L\u1d40v.\n\n The array v can have one or two dimensions and the first dimension has to have\n size n\u22c5p.\n\n Let vec(V) = v. We have\n\n L(\u2202K)L\u1d40\u22c5v = ((L\u2095\u2202C\u2080L\u2095\u1d40) \u2297 (L\u2093GG\u1d40L\u2093\u1d40))vec(V) = vec(L\u2093GG\u1d40L\u2093\u1d40VL\u2095\u2202C\u2080L\u2095\u1d40),\n\n when the derivative is over the parameters of C\u2080. Similarly,\n\n L(\u2202K)L\u1d40v = ((L\u2095\u2202C\u2081L\u2095\u1d40) \u2297 (L\u2093L\u2093\u1d40))vec(V) = vec(L\u2093L\u2093\u1d40VL\u2095\u2202C\u2081L\u2095\u1d40),\n\n over the parameters of C\u2081.\n \"\"\"\n self._init_svd()\n\n def dot(a, b):\n r = tensordot(a, b, axes=([1], [0]))\n if a.ndim > b.ndim:\n return r.transpose([0, 2, 1])\n return r\n\n Lh = self.Lh\n V = unvec(v, (self.Lx.shape[0], -1) + v.shape[1:])\n LdKL_dot = {\n \"C0.Lu\": empty((v.shape[0],) + v.shape[1:] + (self._C0.Lu.shape[0],)),\n \"C1.Lu\": empty((v.shape[0],) + v.shape[1:] + (self._C1.Lu.shape[0],)),\n }\n\n dC0 = self._C0.gradient()[\"Lu\"]\n for i in range(self._C0.Lu.shape[0]):\n t = dot(self._LxG, dot(self._LxG.T, dot(V, Lh @ dC0[..., i] @ Lh.T)))\n LdKL_dot[\"C0.Lu\"][..., i] = t.reshape((-1,) + t.shape[2:], order=\"F\")\n\n dC1 = self._C1.gradient()[\"Lu\"]\n for i in range(self._C1.Lu.shape[0]):\n t = dot(V, Lh @ dC1[..., i] @ Lh.T)\n LdKL_dot[\"C1.Lu\"][..., i] = t.reshape((-1,) + t.shape[2:], order=\"F\")\n\n return LdKL_dot"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rsolve(A, y):\n from numpy_sugar.linalg import rsolve as _rsolve\n\n try:\n beta = _rsolve(A, y)\n except LinAlgError:\n msg = \"Could not converge to solve Ax=y.\"\n msg += \" Setting x to zero.\"\n warnings.warn(msg, RuntimeWarning)\n beta = zeros(A.shape[0])\n\n return beta", "response": "Robust solve Ax = y.\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndrawing random samples from a multivariate normal distribution.", "response": "def multivariate_normal(random, mean, cov):\n \"\"\"\n Draw random samples from a multivariate normal distribution.\n\n Parameters\n ----------\n random : np.random.RandomState instance\n Random state.\n mean : array_like\n Mean of the n-dimensional distribution.\n cov : array_like\n Covariance matrix of the distribution. It must be symmetric and\n positive-definite for proper sampling.\n\n Returns\n -------\n out : ndarray\n The drawn sample.\n \"\"\"\n from numpy.linalg import cholesky\n\n L = cholesky(cov)\n return L @ random.randn(L.shape[0]) + mean"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the gradient of the covariance function derivatives.", "response": "def gradient(self):\n \"\"\"\n Sum of covariance function derivatives.\n\n Returns\n -------\n dict\n \u2202K\u2080 + \u2202K\u2081 + \u22ef\n \"\"\"\n grad = {}\n for i, f in enumerate(self._covariances):\n for varname, g in f.gradient().items():\n grad[f\"{self._name}[{i}].{varname}\"] = g\n return grad"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the value of the COvariance matrix.", "response": "def value(self):\n \"\"\"\n Covariance matrix.\n\n Returns\n -------\n K : ndarray\n s\u22c5XX\u1d40.\n \"\"\"\n X = self.X\n return self.scale * (X @ X.T)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef bernoulli_sample(\n offset,\n G,\n heritability=0.5,\n causal_variants=None,\n causal_variance=0,\n random_state=None,\n):\n r\"\"\"Bernoulli likelihood sampling.\n\n Sample according to\n\n .. math::\n\n \\mathbf y \\sim \\prod_{i=1}^n\n \\text{Bernoulli}(\\mu_i = \\text{logit}(z_i))\n \\mathcal N(~ o \\mathbf 1 + \\mathbf a^\\intercal \\boldsymbol\\alpha;\n ~ (h^2 - v_c)\\mathrm G^\\intercal\\mathrm G +\n (1-h^2-v_c)\\mathrm I ~)\n\n using the canonical Logit link function to define the conditional Bernoulli\n mean :math:`\\mu_i`.\n\n The causal :math:`\\mathbf a` covariates and the corresponding effect-sizes\n are randomly draw according to the following idea. The ``causal_variants``,\n if given, are first mean-zero and std-one normalized and then having\n its elements divided by the squared-root the the number of variances::\n\n causal_variants = _stdnorm(causal_variants, axis=0)\n causal_variants /= sqrt(causal_variants.shape[1])\n\n The causal effect-sizes :math:`\\boldsymbol\\alpha` are draw from\n :math:`\\{-1, +1\\}` and subsequently normalized for mean-zero and std-one\"\"\n\n Parameters\n ----------\n random_state : random_state\n Set the initial random state.\n\n Example\n -------\n\n .. doctest::\n\n >>> from glimix_core.random import bernoulli_sample\n >>> from numpy.random import RandomState\n >>> offset = 5\n >>> G = [[1, -1], [2, 1]]\n >>> bernoulli_sample(offset, G, random_state=RandomState(0))\n array([1., 1.])\n \"\"\"\n link = LogitLink()\n mean, cov = _mean_cov(\n offset, G, heritability, causal_variants, causal_variance, random_state\n )\n lik = BernoulliProdLik(link)\n sampler = GGPSampler(lik, mean, cov)\n\n return sampler.sample(random_state)", "response": "r Bernoulli likelihood sampling."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmaximise the marginal likelihood.", "response": "def fit(self, verbose=True, factr=1e5, pgtol=1e-7):\n r\"\"\"Maximise the marginal likelihood.\n\n Parameters\n ----------\n verbose : bool\n ``True`` for progress output; ``False`` otherwise.\n Defaults to ``True``.\n factr : float, optional\n The iteration stops when\n ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps`` is\n the machine precision.\n pgtol : float, optional\n The iteration will stop when ``max{|proj g_i | i = 1, ..., n} <= pgtol``\n where ``pg_i`` is the i-th component of the projected gradient.\n\n Notes\n -----\n Please, refer to :func:`scipy.optimize.fmin_l_bfgs_b` for further information\n about ``factr`` and ``pgtol``.\n \"\"\"\n self._maximize(verbose=verbose, factr=factr, pgtol=pgtol)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef covariance(self):\n from numpy_sugar.linalg import ddot, sum2diag\n\n Q0 = self._QS[0][0]\n S0 = self._QS[1]\n return sum2diag(dot(ddot(Q0, self.v0 * S0), Q0.T), self.v1)", "response": "rCovariance of the prior."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmaximise the marginal likelihood.", "response": "def fit(self, verbose=True, factr=1e5, pgtol=1e-7):\n r\"\"\"Maximise the marginal likelihood.\n\n Parameters\n ----------\n verbose : bool\n ``True`` for progress output; ``False`` otherwise.\n Defaults to ``True``.\n factr : float, optional\n The iteration stops when\n ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps`` is\n the machine precision.\n pgtol : float, optional\n The iteration will stop when ``max{|proj g_i | i = 1, ..., n} <= pgtol``\n where ``pg_i`` is the i-th component of the projected gradient.\n\n Notes\n -----\n Please, refer to :func:`scipy.optimize.fmin_l_bfgs_b` for further information\n about ``factr`` and ``pgtol``.\n \"\"\"\n self._verbose = verbose\n self._maximize(verbose=verbose, factr=factr, pgtol=pgtol)\n self._verbose = False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _bstar_set(beta, alpha, yTBy, yTBX, yTBM, XTBX, XTBM, MTBM):\n from numpy_sugar import epsilon\n\n r = yTBy\n r -= 2 * add.reduce([i @ beta for i in yTBX])\n r -= 2 * add.reduce([i @ alpha for i in yTBM])\n r += add.reduce([beta.T @ i @ beta for i in XTBX])\n r += 2 * add.reduce([beta.T @ i @ alpha for i in XTBM])\n r += add.reduce([alpha.T @ i @ alpha for i in MTBM])\n return clip(r, epsilon.tiny, inf)", "response": "Compute the bstar set."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef null_lml(self):\n n = self._nsamples\n scale = self.null_scale\n return (self._static_lml() - n * log(scale)) / 2", "response": "Returns the log of the null hypothesis."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the null beta matrix.", "response": "def null_beta(self):\n \"\"\"\n Optimal \ud835\udf37 according to the marginal likelihood.\n\n It is compute by solving the equation ::\n\n (X\u1d40BX)\ud835\udf37 = X\u1d40B\ud835\udc32.\n\n Returns\n -------\n beta : ndarray\n Optimal \ud835\udf37.\n \"\"\"\n ETBE = self._ETBE\n yTBX = self._yTBX\n\n A = sum(i.XTBX for i in ETBE)\n b = sum(yTBX)\n return rsolve(A, b)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef null_beta_covariance(self):\n A = sum(i @ j.T for (i, j) in zip(self._XTQDi, self._XTQ))\n return self.null_scale * pinv(A)", "response": "Covariance of the optimal \ud835\udf37 according to the marginal likelihood."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the null scale of the log likelihood.", "response": "def null_scale(self):\n \"\"\"\n Optimal s according to the marginal likelihood.\n\n The optimal s is given by ::\n\n s = n\u207b\u00b9\ud835\udc32\u1d40B(\ud835\udc32 - X\ud835\udf37),\n\n where \ud835\udf37 is optimal.\n\n Returns\n -------\n scale : float\n Optimal scale.\n \"\"\"\n n = self._nsamples\n beta = self.null_beta\n sqrdot = self._yTBy - dot(sum(self._yTBX), beta)\n return sqrdot / n"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef fast_scan(self, M, verbose=True):\n from tqdm import tqdm\n\n if M.ndim != 2:\n raise ValueError(\"`M` array must be bidimensional.\")\n p = M.shape[1]\n\n lmls = empty(p)\n effsizes0 = empty((p, self._XTQ[0].shape[0]))\n effsizes0_se = empty((p, self._XTQ[0].shape[0]))\n effsizes1 = empty(p)\n effsizes1_se = empty(p)\n scales = empty(p)\n\n if verbose:\n nchunks = min(p, 30)\n else:\n nchunks = min(p, 1)\n\n chunk_size = (p + nchunks - 1) // nchunks\n\n for i in tqdm(range(nchunks), desc=\"Scanning\", disable=not verbose):\n start = i * chunk_size\n stop = min(start + chunk_size, M.shape[1])\n\n r = self._fast_scan_chunk(M[:, start:stop])\n\n lmls[start:stop] = r[\"lml\"]\n effsizes0[start:stop, :] = r[\"effsizes0\"]\n effsizes0_se[start:stop, :] = r[\"effsizes0_se\"]\n effsizes1[start:stop] = r[\"effsizes1\"]\n effsizes1_se[start:stop] = r[\"effsizes1_se\"]\n scales[start:stop] = r[\"scale\"]\n\n return {\n \"lml\": lmls,\n \"effsizes0\": effsizes0,\n \"effsizes0_se\": effsizes0_se,\n \"effsizes1\": effsizes1,\n \"effsizes1_se\": effsizes1_se,\n \"scale\": scales,\n }", "response": "Fast scan for single - marker marginal likelihoods."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef scan(self, M):\n from numpy_sugar.linalg import ddot\n from numpy_sugar import is_all_finite\n\n M = asarray(M, float)\n\n if M.shape[1] == 0:\n return {\n \"lml\": self.null_lml(),\n \"effsizes0\": self.null_beta,\n \"effsizes0_se\": self.null_beta_se,\n \"effsizes1\": empty((0)),\n \"effsizes1_se\": empty((0)),\n \"scale\": self.null_scale,\n }\n\n if not is_all_finite(M):\n raise ValueError(\"M parameter has non-finite elements.\")\n\n MTQ = [dot(M.T, Q) for Q in self._QS[0] if Q.size > 0]\n yTBM = [dot(i, j.T) for (i, j) in zip(self._yTQDi, MTQ)]\n XTBM = [dot(i, j.T) for (i, j) in zip(self._XTQDi, MTQ)]\n D = self._D\n MTBM = [ddot(i, 1 / j) @ i.T for i, j in zip(MTQ, D) if j.min() > 0]\n\n return self._multicovariate_set(yTBM, XTBM, MTBM)", "response": "Scans the multicovariate set for a set of fixed - effect sizes and scale of the candidate set."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the log of the null hypothesis.", "response": "def null_lml(self):\n \"\"\"\n Log of the marginal likelihood for the null hypothesis.\n\n It is implemented as ::\n\n 2\u00b7log(p(Y)) = -n\u00b7p\u00b7log(2\ud835\udf0bs) - log\uff5cK\uff5c - n\u00b7p,\n\n for which s and \ud835\udea9 are optimal.\n\n Returns\n -------\n lml : float\n Log of the marginal likelihood.\n \"\"\"\n np = self._nsamples * self._ntraits\n scale = self.null_scale\n return self._static_lml() / 2 - np * safe_log(scale) / 2 - np / 2"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef null_scale(self):\n np = self._nsamples * self._ntraits\n b = vec(self.null_beta)\n mKiy = b.T @ self._MKiy\n sqrtdot = self._yKiy - mKiy\n scale = sqrtdot / np\n return scale", "response": "Returns the null scale of the logarithmic logarithm."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef scan(self, A1, X1):\n from numpy import empty\n from numpy.linalg import multi_dot\n from numpy_sugar import epsilon, is_all_finite\n from scipy.linalg import cho_solve\n\n A1 = asarray(A1, float)\n X1 = asarray(X1, float)\n\n if not is_all_finite(A1):\n raise ValueError(\"A1 parameter has non-finite elements.\")\n\n if not is_all_finite(X1):\n raise ValueError(\"X1 parameter has non-finite elements.\")\n\n if A1.shape[1] == 0:\n beta_se = sqrt(self.null_beta_covariance.diagonal())\n return {\n \"lml\": self.null_lml(),\n \"effsizes0\": unvec(self.null_beta, (self._ncovariates, -1)),\n \"effsizes0_se\": unvec(beta_se, (self._ncovariates, -1)),\n \"effsizes1\": empty((0,)),\n \"effsizes1_se\": empty((0,)),\n \"scale\": self.null_scale,\n }\n\n X1X1 = X1.T @ X1\n XX1 = self._X.T @ X1\n AWA1 = self._WA.T @ A1\n A1W = A1.T @ self._W\n GX1 = self._G.T @ X1\n\n MRiM1 = kron(AWA1, XX1)\n M1RiM1 = kron(A1W @ A1, X1X1)\n\n M1Riy = vec(multi_dot([X1.T, self._Y, A1W.T]))\n XRiM1 = kron(self._WL0.T @ A1, GX1)\n ZiXRiM1 = cho_solve(self._Lz, XRiM1)\n\n MRiXZiXRiM1 = self._XRiM.T @ ZiXRiM1\n M1RiXZiXRiM1 = XRiM1.T @ ZiXRiM1\n M1RiXZiXRiy = XRiM1.T @ self._ZiXRiy\n\n T0 = [[self._MRiM, MRiM1], [MRiM1.T, M1RiM1]]\n T1 = [[self._MRiXZiXRiM, MRiXZiXRiM1], [MRiXZiXRiM1.T, M1RiXZiXRiM1]]\n T2 = [self._MRiy, M1Riy]\n T3 = [self._MRiXZiXRiy, M1RiXZiXRiy]\n\n MKiM = block(T0) - block(T1)\n MKiy = block(T2) - block(T3)\n beta = rsolve(MKiM, MKiy)\n\n mKiy = beta.T @ MKiy\n cp = self._ntraits * self._ncovariates\n effsizes0 = unvec(beta[:cp], (self._ncovariates, self._ntraits))\n effsizes1 = unvec(beta[cp:], (X1.shape[1], A1.shape[1]))\n\n np = self._nsamples * self._ntraits\n sqrtdot = self._yKiy - mKiy\n scale = clip(sqrtdot / np, epsilon.tiny, inf)\n lml = self._static_lml() / 2 - np * safe_log(scale) / 2 - np / 2\n\n effsizes_se = sqrt(clip(scale * pinv(MKiM).diagonal(), epsilon.tiny, inf))\n effsizes0_se = unvec(effsizes_se[:cp], (self._ncovariates, self._ntraits))\n effsizes1_se = unvec(effsizes_se[cp:], (X1.shape[1], A1.shape[1]))\n\n return {\n \"lml\": lml,\n \"effsizes0\": effsizes0,\n \"effsizes1\": effsizes1,\n \"scale\": scale,\n \"effsizes0_se\": effsizes0_se,\n \"effsizes1_se\": effsizes1_se,\n }", "response": "This function scans the array A1 and X1 for the covariates and returns a dictionary of the values for the covariates."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_fast_scanner(self):\n terms = self._terms\n return KronFastScanner(self._Y, self._mean.A, self._mean.X, self._cov.Ge, terms)", "response": "Returns a new instance of the class designed to perform very fast association scan."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlog of the marginal likelihood.", "response": "def lml(self):\n \"\"\"\n Log of the marginal likelihood.\n\n Let \ud835\udc32 = vec(Y), M = A\u2297X, and H = M\u1d40K\u207b\u00b9M. The restricted log of the marginal\n likelihood is given by [R07]_::\n\n 2\u22c5log(p(\ud835\udc32)) = -(n\u22c5p - c\u22c5p) log(2\u03c0) + log(\uff5cM\u1d40M\uff5c) - log(\uff5cK\uff5c) - log(\uff5cH\uff5c)\n - (\ud835\udc32-\ud835\udc26)\u1d40 K\u207b\u00b9 (\ud835\udc32-\ud835\udc26),\n\n where \ud835\udc26 = M\ud835\udec3 for \ud835\udec3 = H\u207b\u00b9M\u1d40K\u207b\u00b9\ud835\udc32.\n\n For implementation purpose, let X = (L\u2080 \u2297 G) and R = (L\u2081 \u2297 I)(L\u2081 \u2297 I)\u1d40.\n The covariance can be written as::\n\n K = XX\u1d40 + R.\n\n From the Woodbury matrix identity, we have\n\n \ud835\udc32\u1d40K\u207b\u00b9\ud835\udc32 = \ud835\udc32\u1d40R\u207b\u00b9\ud835\udc32 - \ud835\udc32\u1d40R\u207b\u00b9XZ\u207b\u00b9X\u1d40R\u207b\u00b9\ud835\udc32,\n\n where Z = I + X\u1d40R\u207b\u00b9X. Note that R\u207b\u00b9 = (U\u2081S\u2081\u207b\u00b9U\u2081\u1d40) \u2297 I and ::\n\n X\u1d40R\u207b\u00b9\ud835\udc32 = (L\u2080\u1d40W \u2297 G\u1d40)\ud835\udc32 = vec(G\u1d40YWL\u2080),\n\n where W = U\u2081S\u2081\u207b\u00b9U\u2081\u1d40. The term G\u1d40Y can be calculated only once and it will form a\n r\u00d7p matrix. We similarly have ::\n\n X\u1d40R\u207b\u00b9M = (L\u2080\u1d40WA) \u2297 (G\u1d40X),\n\n for which G\u1d40X is pre-computed.\n\n The log-determinant of the covariance matrix is given by\n\n log(\uff5cK\uff5c) = log(\uff5cZ\uff5c) - log(\uff5cR\u207b\u00b9\uff5c) = log(\uff5cZ\uff5c) - 2\u00b7n\u00b7log(\uff5cU\u2081S\u2081\u207b\u00bd\uff5c).\n\n The log of the marginal likelihood can be rewritten as::\n\n 2\u22c5log(p(\ud835\udc32)) = -(n\u22c5p - c\u22c5p) log(2\u03c0) + log(\uff5cM\u1d40M\uff5c)\n - log(\uff5cZ\uff5c) + 2\u00b7n\u00b7log(\uff5cU\u2081S\u2081\u207b\u00bd\uff5c)\n - log(\uff5cM\u1d40R\u207b\u00b9M - M\u1d40R\u207b\u00b9XZ\u207b\u00b9X\u1d40R\u207b\u00b9M\uff5c)\n - \ud835\udc32\u1d40R\u207b\u00b9\ud835\udc32 + (\ud835\udc32\u1d40R\u207b\u00b9X)Z\u207b\u00b9(X\u1d40R\u207b\u00b9\ud835\udc32)\n - \ud835\udc26\u1d40R\u207b\u00b9\ud835\udc26 + (\ud835\udc26\u1d40R\u207b\u00b9X)Z\u207b\u00b9(X\u1d40R\u207b\u00b9\ud835\udc26)\n + 2\ud835\udc32\u1d40R\u207b\u00b9\ud835\udc26 - 2(\ud835\udc32\u1d40R\u207b\u00b9X)Z\u207b\u00b9(X\u1d40R\u207b\u00b9\ud835\udc26).\n\n Returns\n -------\n lml : float\n Log of the marginal likelihood.\n\n References\n ----------\n .. [R07] LaMotte, L. R. (2007). A direct derivation of the REML likelihood\n function. Statistical Papers, 48(2), 321-327.\n \"\"\"\n terms = self._terms\n yKiy = terms[\"yKiy\"]\n mKiy = terms[\"mKiy\"]\n mKim = terms[\"mKim\"]\n\n lml = -self._df * log2pi + self._logdet_MM - self._logdetK\n lml -= self._logdetH\n lml += -yKiy - mKim + 2 * mKiy\n\n return lml / 2"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gradient(self):\n self._update_approx()\n\n g = self._ep.lml_derivatives(self._X)\n ed = exp(-self.logitdelta)\n es = exp(self.logscale)\n\n grad = dict()\n grad[\"logitdelta\"] = g[\"delta\"] * (ed / (1 + ed)) / (1 + ed)\n grad[\"logscale\"] = g[\"scale\"] * es\n grad[\"beta\"] = g[\"mean\"]\n\n return grad", "response": "Gradient of the log of the marginal likelihood."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef gradient(self):\n L = self.L\n n = self.L.shape[0]\n grad = {\"Lu\": zeros((n, n, n * self._L.shape[1]))}\n for ii in range(self._L.shape[0] * self._L.shape[1]):\n row = ii // self._L.shape[1]\n col = ii % self._L.shape[1]\n grad[\"Lu\"][row, :, ii] = L[:, col]\n grad[\"Lu\"][:, row, ii] += L[:, col]\n\n return grad", "response": "Gradient of the log likelihood matrix."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef beta(self):\n from numpy_sugar.linalg import rsolve\n\n return rsolve(self._X[\"VT\"], rsolve(self._X[\"tX\"], self.mean()))", "response": "Calculates the beta of the current state of the object."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nestimating the covariance matrix of the optimal beta.", "response": "def beta_covariance(self):\n \"\"\"\n Estimates the covariance-matrix of the optimal beta.\n\n Returns\n -------\n beta-covariance : ndarray\n (X\u1d40(s((1-\ud835\udeff)K + \ud835\udeffI))\u207b\u00b9X)\u207b\u00b9.\n\n References\n ----------\n .. Rencher, A. C., & Schaalje, G. B. (2008). Linear models in statistics. John\n Wiley & Sons.\n \"\"\"\n from numpy_sugar.linalg import ddot\n\n tX = self._X[\"tX\"]\n Q = concatenate(self._QS[0], axis=1)\n S0 = self._QS[1]\n D = self.v0 * S0 + self.v1\n D = D.tolist() + [self.v1] * (len(self._y) - len(D))\n D = asarray(D)\n A = inv(tX.T @ (Q @ ddot(1 / D, Q.T @ tX)))\n VT = self._X[\"VT\"]\n H = lstsq(VT, A, rcond=None)[0]\n return lstsq(VT, H.T, rcond=None)[0]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndisabling parameter optimization. Parameters ---------- param : str Possible values are ``\"delta\"``, ``\"beta\"``, and ``\"scale\"``.", "response": "def fix(self, param):\n \"\"\"\n Disable parameter optimization.\n\n Parameters\n ----------\n param : str\n Possible values are ``\"delta\"``, ``\"beta\"``, and ``\"scale\"``.\n \"\"\"\n if param == \"delta\":\n super()._fix(\"logistic\")\n else:\n self._fix[param] = True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nenable parameter optimization. Parameters ---------- param : str Possible values are ``\"delta\"``, ``\"beta\"``, and ``\"scale\"``.", "response": "def unfix(self, param):\n \"\"\"\n Enable parameter optimization.\n\n Parameters\n ----------\n param : str\n Possible values are ``\"delta\"``, ``\"beta\"``, and ``\"scale\"``.\n \"\"\"\n if param == \"delta\":\n self._unfix(\"logistic\")\n else:\n self._fix[param] = False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fit(self, verbose=True):\n if not self._isfixed(\"logistic\"):\n self._maximize_scalar(desc=\"LMM\", rtol=1e-6, atol=1e-6, verbose=verbose)\n\n if not self._fix[\"beta\"]:\n self._update_beta()\n\n if not self._fix[\"scale\"]:\n self._update_scale()", "response": "Fit the marginal likelihood to the logistic distribution."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_fast_scanner(self):\n v0 = self.v0\n v1 = self.v1\n QS = (self._QS[0], v0 * self._QS[1])\n return FastScanner(self._y, self.X, QS, v1)", "response": "Returns a new instance of the class designed to perform very fast association scan."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the value of the object.", "response": "def value(self):\n \"\"\"\n Internal use only.\n \"\"\"\n if not self._fix[\"beta\"]:\n self._update_beta()\n\n if not self._fix[\"scale\"]:\n self._update_scale()\n\n return self.lml()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the log of the marginal likelihood.", "response": "def lml(self):\n \"\"\"\n Log of the marginal likelihood.\n\n Returns\n -------\n lml : float\n Log of the marginal likelihood.\n\n Notes\n -----\n The log of the marginal likelihood is given by ::\n\n 2\u22c5log(p(\ud835\udc32)) = -n\u22c5log(2\u03c0) - n\u22c5log(s) - log|D| - (Q\u1d40\ud835\udc32)\u1d40s\u207b\u00b9D\u207b\u00b9(Q\u1d40\ud835\udc32)\n + (Q\u1d40\ud835\udc32)\u1d40s\u207b\u00b9D\u207b\u00b9(Q\u1d40X\ud835\udf37)/2 - (Q\u1d40X\ud835\udf37)\u1d40s\u207b\u00b9D\u207b\u00b9(Q\u1d40X\ud835\udf37).\n\n By using the optimal \ud835\udf37, the log of the marginal likelihood can be rewritten\n as::\n\n 2\u22c5log(p(\ud835\udc32)) = -n\u22c5log(2\u03c0) - n\u22c5log(s) - log|D| + (Q\u1d40\ud835\udc32)\u1d40s\u207b\u00b9D\u207b\u00b9Q\u1d40(X\ud835\udf37-\ud835\udc32).\n\n\n In the extreme case where \ud835\udf37 is such that \ud835\udc32 = X\ud835\udf37, the maximum is attained as\n s\u21920.\n\n For optimals \ud835\udf37 and s, the log of the marginal likelihood can be further\n simplified to ::\n\n 2\u22c5log(p(\ud835\udc32; \ud835\udf37, s)) = -n\u22c5log(2\u03c0) - n\u22c5log s - log|D| - n.\n \"\"\"\n reml = (self._logdetXX() - self._logdetH()) / 2\n if self._optimal[\"scale\"]:\n lml = self._lml_optimal_scale()\n else:\n lml = self._lml_arbitrary_scale()\n return lml + reml"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the delta of the logistic value between K and I.", "response": "def delta(self):\n \"\"\"\n Variance ratio between ``K`` and ``I``.\n \"\"\"\n\n v = float(self._logistic.value)\n\n if v > 0.0:\n v = 1 / (1 + exp(-v))\n else:\n v = exp(v)\n v = v / (v + 1.0)\n\n return min(max(v, epsilon.tiny), 1 - epsilon.tiny)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the log determinant of X\u1d40X.", "response": "def _logdetXX(self):\n \"\"\"\n log(\uff5cX\u1d40X\uff5c).\n \"\"\"\n if not self._restricted:\n return 0.0\n\n ldet = slogdet(self._X[\"tX\"].T @ self._X[\"tX\"])\n if ldet[0] != 1.0:\n raise ValueError(\"The determinant of X\u1d40X should be positive.\")\n return ldet[1]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _logdetH(self):\n if not self._restricted:\n return 0.0\n ldet = slogdet(sum(self._XTQDiQTX) / self.scale)\n if ldet[0] != 1.0:\n raise ValueError(\"The determinant of H should be positive.\")\n return ldet[1]", "response": "Return the log - determinant of the current user s income."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _lml_optimal_scale(self):\n assert self._optimal[\"scale\"]\n\n n = len(self._y)\n lml = -self._df * log2pi - self._df - n * log(self.scale)\n lml -= sum(npsum(log(D)) for D in self._D)\n return lml / 2", "response": "Implementation for unrestricted LML :: _optimal_scale Returns ------- float DMatrix of the marginal likelihood for optimal scale."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _lml_arbitrary_scale(self):\n s = self.scale\n D = self._D\n n = len(self._y)\n lml = -self._df * log2pi - n * log(s)\n lml -= sum(npsum(log(d)) for d in D)\n d = (mTQ - yTQ for (mTQ, yTQ) in zip(self._mTQ, self._yTQ))\n lml -= sum((i / j) @ i for (i, j) in zip(d, D)) / s\n\n return lml / 2", "response": "Calculates the log of the marginal likelihood for arbitrary scale."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _df(self):\n if not self._restricted:\n return self.nsamples\n return self.nsamples - self._X[\"tX\"].shape[1]", "response": "Returns the number of samples in the freezing table."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_fast_scanner(self):\n from numpy_sugar.linalg import ddot, economic_qs, sum2diag\n\n y = self.eta / self.tau\n\n if self._QS is None:\n K = eye(y.shape[0]) / self.tau\n else:\n Q0 = self._QS[0][0]\n S0 = self._QS[1]\n K = dot(ddot(Q0, self.v0 * S0), Q0.T)\n K = sum2diag(K, 1 / self.tau)\n\n return FastScanner(y, self._X, economic_qs(K), self.v1)", "response": "rReturn a FastScanner for the current\n delta."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the value of the log of the marginal likelihood.", "response": "def value(self):\n r\"\"\"Log of the marginal likelihood.\n\n Formally,\n\n .. math::\n\n - \\frac{n}{2}\\log{2\\pi} - \\frac{1}{2} \\log{\\left|\n v_0 \\mathrm K + v_1 \\mathrm I + \\tilde{\\Sigma} \\right|}\n - \\frac{1}{2}\n \\left(\\tilde{\\boldsymbol\\mu} -\n \\mathrm X\\boldsymbol\\beta\\right)^{\\intercal}\n \\left( v_0 \\mathrm K + v_1 \\mathrm I +\n \\tilde{\\Sigma} \\right)^{-1}\n \\left(\\tilde{\\boldsymbol\\mu} -\n \\mathrm X\\boldsymbol\\beta\\right)\n\n Returns\n -------\n float\n :math:`\\log{p(\\tilde{\\boldsymbol\\mu})}`\n \"\"\"\n from numpy_sugar.linalg import ddot, sum2diag\n\n if self._cache[\"value\"] is not None:\n return self._cache[\"value\"]\n\n scale = exp(self.logscale)\n delta = 1 / (1 + exp(-self.logitdelta))\n\n v0 = scale * (1 - delta)\n v1 = scale * delta\n\n mu = self.eta / self.tau\n n = len(mu)\n if self._QS is None:\n K = zeros((n, n))\n else:\n Q0 = self._QS[0][0]\n S0 = self._QS[1]\n K = dot(ddot(Q0, S0), Q0.T)\n\n A = sum2diag(sum2diag(v0 * K, v1), 1 / self.tau)\n m = mu - self.mean()\n\n v = -n * log(2 * pi)\n v -= slogdet(A)[1]\n v -= dot(m, solve(A, m))\n\n self._cache[\"value\"] = v / 2\n\n return self._cache[\"value\"]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _initialize(self):\n if self._mean is None or self._cov is None:\n return\n\n Q = self._cov[\"QS\"][0][0]\n S = self._cov[\"QS\"][1]\n\n if S.size > 0:\n self.tau[:] = 1 / npsum((Q * sqrt(S)) ** 2, axis=1)\n else:\n self.tau[:] = 0.0\n self.eta[:] = self._mean\n self.eta[:] *= self.tau", "response": "r Initializes the mean and covariance of the posterior."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_engine_session(connection, echo=False, autoflush=None, autocommit=None, expire_on_commit=None,\n scopefunc=None):\n \"\"\"Build an engine and a session.\n\n :param str connection: An RFC-1738 database connection string\n :param bool echo: Turn on echoing SQL\n :param Optional[bool] autoflush: Defaults to True if not specified in kwargs or configuration.\n :param Optional[bool] autocommit: Defaults to False if not specified in kwargs or configuration.\n :param Optional[bool] expire_on_commit: Defaults to False if not specified in kwargs or configuration.\n :param scopefunc: Scoped function to pass to :func:`sqlalchemy.orm.scoped_session`\n :rtype: tuple[Engine,Session]\n\n From the Flask-SQLAlchemy documentation:\n\n An extra key ``'scopefunc'`` can be set on the ``options`` dict to\n specify a custom scope function. If it's not provided, Flask's app\n context stack identity is used. This will ensure that sessions are\n created and removed with the request/response cycle, and should be fine\n in most cases.\n \"\"\"\n if connection is None:\n raise ValueError('can not build engine when connection is None')\n\n engine = create_engine(connection, echo=echo)\n\n autoflush = autoflush if autoflush is not None else False\n autocommit = autocommit if autocommit is not None else False\n expire_on_commit = expire_on_commit if expire_on_commit is not None else True\n\n log.debug('auto flush: %s, auto commit: %s, expire on commmit: %s', autoflush, autocommit, expire_on_commit)\n\n #: A SQLAlchemy session maker\n session_maker = sessionmaker(\n bind=engine,\n autoflush=autoflush,\n autocommit=autocommit,\n expire_on_commit=expire_on_commit,\n )\n\n #: A SQLAlchemy session object\n session = scoped_session(\n session_maker,\n scopefunc=scopefunc\n )\n\n return engine, session", "response": "Build an engine and a SQLAlchemy session."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a default connection string.", "response": "def _get_connection(cls, connection: Optional[str] = None) -> str:\n \"\"\"Get a default connection string.\n\n Wraps :func:`bio2bel.utils.get_connection` and passing this class's :data:`module_name` to it.\n \"\"\"\n return get_connection(cls.module_name, connection=connection)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates an instance of the SMTP class based on the settings passed in.", "response": "def setup_smtp_factory(**settings):\n \"\"\" expects a dictionary with 'mail.' keys to create an appropriate smtplib.SMTP instance\"\"\"\n return CustomSMTP(\n host=settings.get('mail.host', 'localhost'),\n port=int(settings.get('mail.port', 25)),\n user=settings.get('mail.user'),\n password=settings.get('mail.password'),\n timeout=float(settings.get('mail.timeout', 60)),\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sendMultiPart(smtp, gpg_context, sender, recipients, subject, text, attachments):\n sent = 0\n for to in recipients:\n if not to.startswith('<'):\n uid = '<%s>' % to\n else:\n uid = to\n\n if not checkRecipient(gpg_context, uid):\n continue\n\n msg = MIMEMultipart()\n\n msg['From'] = sender\n msg['To'] = to\n msg['Subject'] = subject\n msg[\"Date\"] = formatdate(localtime=True)\n msg.preamble = u'This is an email in encrypted multipart format.'\n\n attach = MIMEText(str(gpg_context.encrypt(text.encode('utf-8'), uid, always_trust=True)))\n attach.set_charset('UTF-8')\n msg.attach(attach)\n\n for attachment in attachments:\n with open(attachment, 'rb') as fp:\n attach = MIMEBase('application', 'octet-stream')\n attach.set_payload(str(gpg_context.encrypt_file(fp, uid, always_trust=True)))\n attach.add_header('Content-Disposition', 'attachment', filename=basename('%s.pgp' % attachment))\n msg.attach(attach)\n\n # TODO: need to catch exception?\n # yes :-) we need to adjust the status accordingly (>500 so it will be destroyed)\n smtp.begin()\n smtp.sendmail(sender, to, msg.as_string())\n smtp.quit()\n sent += 1\n\n return sent", "response": "a helper method that composes and sends an email with attachments"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconnects and optionally authenticates a connection.", "response": "def begin(self):\n \"\"\" connects and optionally authenticates a connection.\"\"\"\n self.connect(self.host, self.port)\n if self.user:\n self.starttls()\n self.login(self.user, self.password)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a function that downloads the data for you or uses a cached version at the given path.", "response": "def make_downloader(url: str, path: str) -> Callable[[bool], str]: # noqa: D202\n \"\"\"Make a function that downloads the data for you, or uses a cached version at the given path.\n\n :param url: The URL of some data\n :param path: The path of the cached data, or where data is cached if it does not already exist\n :return: A function that downloads the data and returns the path of the data\n \"\"\"\n\n def download_data(force_download: bool = False) -> str:\n \"\"\"Download the data.\n\n :param force_download: If true, overwrites a previously cached file\n \"\"\"\n if os.path.exists(path) and not force_download:\n log.info('using cached data at %s', path)\n else:\n log.info('downloading %s to %s', url, path)\n urlretrieve(url, path)\n\n return path\n\n return download_data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef make_df_getter(data_url: str, data_path: str, **kwargs) -> Callable[[Optional[str], bool, bool], pd.DataFrame]:\n download_function = make_downloader(data_url, data_path)\n\n def get_df(url: Optional[str] = None, cache: bool = True, force_download: bool = False) -> pd.DataFrame:\n \"\"\"Get the data as a pandas DataFrame.\n\n :param url: The URL (or file path) to download.\n :param cache: If true, the data is downloaded to the file system, else it is loaded from the internet\n :param force_download: If true, overwrites a previously cached file\n \"\"\"\n if url is None and cache:\n url = download_function(force_download=force_download)\n\n return pd.read_csv(\n url or data_url,\n **kwargs\n )\n\n return get_df", "response": "Build a function that returns a pandas DataFrame."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef generate(self, **kwargs):\n '''\n Generate a :term:`URI` based on parameters passed.\n\n :param id: The id of the concept or collection.\n :param type: What we're generating a :term:`URI` for: `concept`\n or `collection`.\n :rtype: string\n '''\n if kwargs['type'] not in ['concept', 'collection']:\n raise ValueError('Type %s is invalid' % kwargs['type'])\n return (\n self.pattern % (self.vocabulary_id, kwargs['type'], kwargs['id'])\n ).lower()", "response": "Generate a : term : URI based on the passed parameters."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef has_address(start: int, data_length: int) -> bool:\n return bool(0x01 & start) or (start == 0x82 and data_length == 16)", "response": "Determines if the packet has an address encoded into it."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef decode_timestamp(data: str) -> datetime.datetime:\n year = 2000 + int(data[0:2])\n month = int(data[2:4])\n day = int(data[4:6])\n hour = int(data[6:8])\n minute = int(data[8:10])\n second = int(data[10:12])\n if minute == 60:\n minute = 0\n hour += 1\n\n return datetime.datetime(year=year, month=month, day=day, hour=hour,\n minute=minute, second=second)", "response": "Decode timestamp using bespoke decoder."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_application(connection: Optional[str] = None) -> Flask:\n app = Flask(__name__)\n\n flask_bootstrap.Bootstrap(app)\n Admin(app)\n\n connection = connection or DEFAULT_CACHE_CONNECTION\n engine, session = build_engine_session(connection)\n\n for name, add_admin in add_admins.items():\n url = '/{}'.format(name)\n add_admin(app, session, url=url, endpoint=name, name=name)\n log.debug('added %s - %s to %s', name, add_admin, url)\n\n app.register_blueprint(ui)\n\n return app", "response": "Create a Flask application."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nregisters a provider with the specified id or uri.", "response": "def register_provider(self, provider):\n '''\n Register a :class:`skosprovider.providers.VocabularyProvider`.\n\n :param skosprovider.providers.VocabularyProvider provider: The provider\n to register.\n :raises RegistryException: A provider with this id or uri has already \n been registered.\n '''\n if provider.get_vocabulary_id() in self.providers:\n raise RegistryException(\n 'A provider with this id has already been registered.'\n )\n self.providers[provider.get_vocabulary_id()] = provider\n if provider.concept_scheme.uri in self.concept_scheme_uri_map:\n raise RegistryException(\n 'A provider with URI %s has already been registered.' % provider.concept_scheme.uri\n )\n self.concept_scheme_uri_map[provider.concept_scheme.uri] = provider.get_vocabulary_id()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef remove_provider(self, id):\n '''\n Remove the provider with the given id or :term:`URI`.\n\n :param str id: The identifier for the provider.\n :returns: A :class:`skosprovider.providers.VocabularyProvider` or\n `False` if the id is unknown.\n '''\n if id in self.providers:\n p = self.providers.get(id, False)\n del self.providers[id]\n del self.concept_scheme_uri_map[p.concept_scheme.uri]\n return p\n elif id in self.concept_scheme_uri_map:\n id = self.concept_scheme_uri_map[id]\n return self.remove_provider(id)\n else:\n return False", "response": "Removes the provider with the given id or URI."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting a provider by id or uri.", "response": "def get_provider(self, id):\n '''\n Get a provider by id or :term:`uri`.\n\n :param str id: The identifier for the provider. This can either be the\n id with which it was registered or the :term:`uri` of the conceptscheme\n that the provider services.\n :returns: A :class:`skosprovider.providers.VocabularyProvider`\n or `False` if the id or uri is unknown.\n '''\n if id in self.providers:\n return self.providers.get(id, False)\n elif is_uri(id) and id in self.concept_scheme_uri_map:\n return self.providers.get(self.concept_scheme_uri_map[id], False)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_providers(self, **kwargs):\n '''Get all providers registered.\n\n If keyword `ids` is present, get only the providers with these ids.\n\n If keys `subject` is present, get only the providers that have this subject.\n\n .. code-block:: python\n\n # Get all providers with subject 'biology'\n registry.get_providers(subject='biology')\n\n # Get all providers with id 1 or 2\n registry.get_providers(ids=[1,2])\n\n # Get all providers with id 1 or 2 and subject 'biology'\n registry.get_providers(ids=[1,2], subject='biology']\n\n :param list ids: Only return providers with one of the Ids or :term:`URIs `.\n :param str subject: Only return providers with this subject.\n :returns: A list of :class:`providers `\n '''\n if 'ids' in kwargs:\n ids = [self.concept_scheme_uri_map.get(id, id) for id in kwargs['ids']]\n providers = [\n self.providers[k] for k in self.providers.keys() if k in ids\n ]\n else:\n providers = list(self.providers.values())\n if 'subject' in kwargs:\n providers = [p for p in providers if kwargs['subject'] in p.metadata['subject']]\n return providers", "response": "Get all providers registered."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find(self, query, **kwargs):\n '''Launch a query across all or a selection of providers.\n\n .. code-block:: python\n\n # Find anything that has a label of church in any provider.\n registry.find({'label': 'church'})\n\n # Find anything that has a label of church with the BUILDINGS provider.\n # Attention, this syntax was deprecated in version 0.3.0\n registry.find({'label': 'church'}, providers=['BUILDINGS'])\n\n # Find anything that has a label of church with the BUILDINGS provider.\n registry.find({'label': 'church'}, providers={'ids': ['BUILDINGS']})\n\n # Find anything that has a label of church with a provider\n # marked with the subject 'architecture'.\n registry.find({'label': 'church'}, providers={'subject': 'architecture'})\n\n # Find anything that has a label of church in any provider.\n # If possible, display the results with a Dutch label.\n registry.find({'label': 'church'}, language='nl')\n\n :param dict query: The query parameters that will be passed on to each\n :meth:`~skosprovider.providers.VocabularyProvider.find` method of\n the selected.\n :class:`providers `.\n :param dict providers: Optional. If present, it should be a dictionary.\n This dictionary can contain any of the keyword arguments available\n to the :meth:`get_providers` method. The query will then only\n be passed to the providers confirming to these arguments.\n :param string language: Optional. If present, it should be a\n :term:`language-tag`. This language-tag is passed on to the\n underlying providers and used when selecting the label to display\n for each concept.\n :returns: a list of :class:`dict`.\n Each dict has two keys: id and concepts.\n '''\n if 'providers' not in kwargs:\n providers = self.get_providers()\n else:\n pargs = kwargs['providers']\n if isinstance(pargs, list):\n providers = self.get_providers(ids=pargs)\n else:\n providers = self.get_providers(**pargs)\n kwarguments = {}\n if 'language' in kwargs:\n kwarguments['language'] = kwargs['language']\n return [{'id': p.get_vocabulary_id(), 'concepts': p.find(query, **kwarguments)}\n for p in providers]", "response": "Launch a query across all or a selection of providers."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_all(self, **kwargs):\n '''Get all concepts from all providers.\n\n .. code-block:: python\n\n # get all concepts in all providers.\n registry.get_all()\n\n # get all concepts in all providers.\n # If possible, display the results with a Dutch label.\n registry.get_all(language='nl')\n\n :param string language: Optional. If present, it should be a\n :term:`language-tag`. This language-tag is passed on to the\n underlying providers and used when selecting the label to display\n for each concept.\n\n :returns: a list of :class:`dict`.\n Each dict has two keys: id and concepts.\n '''\n kwarguments = {}\n if 'language' in kwargs:\n kwarguments['language'] = kwargs['language']\n return [{'id': p.get_vocabulary_id(), 'concepts': p.get_all(**kwarguments)}\n for p in self.providers.values()]", "response": "Get all concepts from all providers."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget a concept or collection by its uri. Returns a single concept or collection if one exists with this uri Returns False otherwise.", "response": "def get_by_uri(self, uri):\n '''Get a concept or collection by its uri.\n\n Returns a single concept or collection if one exists with this uri.\n Returns False otherwise.\n\n :param string uri: The uri to find a concept or collection for.\n :raises ValueError: The uri is invalid.\n :rtype: :class:`skosprovider.skos.Concept` or\n :class:`skosprovider.skos.Collection`\n '''\n if not is_uri(uri):\n raise ValueError('%s is not a valid URI.' % uri)\n # Check if there's a provider that's more likely to have the URI\n csuris = [csuri for csuri in self.concept_scheme_uri_map.keys() if uri.startswith(csuri)]\n for csuri in csuris:\n c = self.get_provider(csuri).get_by_uri(uri)\n if c:\n return c\n # Check all providers\n for p in self.providers.values():\n c = p.get_by_uri(uri)\n if c:\n return c\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef find_module(self, fullname, path=None):\n if not fullname.startswith(self._group_with_dot):\n return\n end_name = fullname[len(self._group_with_dot):]\n for entry_point in iter_entry_points(group=self.group, name=None):\n if entry_point.name == end_name:\n return self", "response": "Find a module if its name starts with self. group and is registered."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nload a module if its name starts with self. group and is registered.", "response": "def load_module(self, fullname):\n \"\"\"Load a module if its name starts with :code:`self.group` and is registered.\"\"\"\n if fullname in sys.modules:\n return sys.modules[fullname]\n end_name = fullname[len(self._group_with_dot):]\n for entry_point in iter_entry_points(group=self.group, name=end_name):\n mod = entry_point.load()\n sys.modules[fullname] = mod\n return mod"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef upload_theme():\n get_vars()\n with fab.settings():\n local_theme_path = path.abspath(\n path.join(fab.env['config_base'],\n fab.env.instance.config['local_theme_path']))\n rsync(\n '-av',\n '--delete',\n '%s/' % local_theme_path,\n '{{host_string}}:{themes_dir}/{ploy_theme_name}'.format(**AV)\n )\n briefkasten_ctl('restart')", "response": "Upload and or update the theme with the current git state"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef upload_pgp_keys():\n get_vars()\n upload_target = '/tmp/pgp_pubkeys.tmp'\n with fab.settings(fab.hide('running')):\n fab.run('rm -rf %s' % upload_target)\n fab.run('mkdir %s' % upload_target)\n local_key_path = path.join(fab.env['config_base'], fab.env.instance.config['local_pgpkey_path'])\n remote_key_path = '/var/briefkasten/pgp_pubkeys/'.format(**AV)\n rsync('-av', local_key_path, '{host_string}:%s' % upload_target)\n fab.run('chown -R %s %s' % (AV['appuser'], remote_key_path))\n fab.run('chmod 700 %s' % remote_key_path)\n with fab.shell_env(GNUPGHOME=remote_key_path):\n fab.sudo('''gpg --import %s/*.*''' % upload_target,\n user=AV['appuser'], shell_escape=False)\n fab.run('rm -rf %s' % upload_target)", "response": "Upload and update the PGP keys for editors"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbuild the backend and upload it to the remote server at the given index", "response": "def upload_backend(index='dev', user=None):\n \"\"\"\n Build the backend and upload it to the remote server at the given index\n \"\"\"\n get_vars()\n use_devpi(index=index)\n with fab.lcd('../application'):\n fab.local('make upload')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the backend from the given index at the given version on the target host.", "response": "def update_backend(use_pypi=False, index='dev', build=True, user=None, version=None):\n \"\"\"\n Install the backend from the given devpi index at the given version on the target host and restart the service.\n\n If version is None, it defaults to the latest version\n\n Optionally, build and upload the application first from local sources. This requires a\n full backend development environment on the machine running this command (pyramid etc.)\n \"\"\"\n get_vars()\n if value_asbool(build):\n upload_backend(index=index, user=user)\n with fab.cd('{apphome}'.format(**AV)):\n if value_asbool(use_pypi):\n command = 'bin/pip install --upgrade briefkasten'\n else:\n command = 'bin/pip install --upgrade --pre -i {ploy_default_publish_devpi}/briefkasten/{index}/+simple/ briefkasten'.format(\n index=index,\n user=user,\n **AV)\n if version:\n command = '%s==%s' % (command, version)\n fab.sudo(command)\n\n briefkasten_ctl('restart')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a sorted version of a list of concepts. Will leave the original list unsorted.", "response": "def _sort(self, concepts, sort=None, language='any', reverse=False):\n '''\n Returns a sorted version of a list of concepts. Will leave the original\n list unsorted.\n\n :param list concepts: A list of concepts and collections.\n :param string sort: What to sort on: `id`, `label` or `sortlabel`\n :param string language: Language to use when sorting on `label` or\n `sortlabel`.\n :param boolean reverse: Reverse the sort order?\n :rtype: list\n '''\n sorted = copy.copy(concepts)\n if sort:\n sorted.sort(key=methodcaller('_sortkey', sort, language), reverse=reverse)\n return sorted"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _include_in_find(self, c, query):\n '''\n :param c: A :class:`skosprovider.skos.Concept` or\n :class:`skosprovider.skos.Collection`.\n :param query: A dict that can be used to express a query.\n :rtype: boolean\n '''\n include = True\n if include and 'type' in query:\n include = query['type'] == c.type\n if include and 'label' in query:\n def finder(l, query):\n if not self.case_insensitive:\n return l.label.find(query['label'])\n else:\n return l.label.upper().find(query['label'].upper())\n include = any([finder(l, query) >= 0 for l in c.labels])\n if include and 'collection' in query:\n coll = self.get_by_id(query['collection']['id'])\n if not coll or not isinstance(coll, Collection):\n raise ValueError(\n 'You are searching for items in an unexisting collection.'\n )\n if 'depth' in query['collection'] and query['collection']['depth'] == 'all':\n members = self.expand(coll.id)\n else:\n members = coll.members\n include = any([True for id in members if str(id) == str(c.id)]) \n return include", "response": "Returns True if the item in the given collection is included in the find query."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_find_dict(self, c, **kwargs):\n '''\n Return a dict that can be used in the return list of the :meth:`find`\n method.\n\n :param c: A :class:`skosprovider.skos.Concept` or\n :class:`skosprovider.skos.Collection`.\n :rtype: dict\n '''\n language = self._get_language(**kwargs)\n return {\n 'id': c.id,\n 'uri': c.uri,\n 'type': c.type,\n 'label': None if c.label() is None else c.label(language).label\n }", "response": "Return a dict that can be used in the return list of the : meth : find method."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def update(self) -> None:\n _LOGGER.debug(\"Requesting state update from server (S00, S14)\")\n await asyncio.gather(\n # List unsealed Zones\n self.send_command('S00'),\n # Arming status update\n self.send_command('S14'),\n )", "response": "Update the state of the current system."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def _update_loop(self) -> None:\n await asyncio.sleep(self._update_interval)\n while not self._closed:\n await self.update()\n await asyncio.sleep(self._update_interval)", "response": "Schedule a state update to keep the connection alive"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_cli_to_bel_namespace(main: click.Group) -> click.Group: # noqa: D202\n\n @main.command()\n @click.option('-u', '--update', is_flag=True)\n @click.pass_obj\n def upload(manager: BELNamespaceManagerMixin, update):\n \"\"\"Upload names/identifiers to terminology store.\"\"\"\n namespace = manager.upload_bel_namespace(update=update)\n click.echo(f'uploaded [{namespace.id}] {namespace.keyword}')\n\n return main", "response": "Add a command to main function that uploads BEL Namespaces to terminology store."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds a clear_bel_namespace command to main.", "response": "def add_cli_clear_bel_namespace(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add a ``clear_bel_namespace`` command to main :mod:`click` function.\"\"\"\n\n @main.command()\n @click.pass_obj\n def drop(manager: BELNamespaceManagerMixin):\n \"\"\"Clear names/identifiers to terminology store.\"\"\"\n namespace = manager.drop_bel_namespace()\n\n if namespace:\n click.echo(f'namespace {namespace} was cleared')\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a write_bel_namespace command to main.", "response": "def add_cli_write_bel_namespace(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add a ``write_bel_namespace`` command to main :mod:`click` function.\"\"\"\n\n @main.command()\n @click.option('-d', '--directory', type=click.Path(file_okay=False, dir_okay=True), default=os.getcwd(),\n help='output directory')\n @click.pass_obj\n def write(manager: BELNamespaceManagerMixin, directory: str):\n \"\"\"Write a BEL namespace names/identifiers to terminology store.\"\"\"\n manager.write_directory(directory)\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds a write_bel_annotation command to main.", "response": "def add_cli_write_bel_annotation(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add a ``write_bel_annotation`` command to main :mod:`click` function.\"\"\"\n\n @main.command()\n @click.option('-d', '--directory', type=click.Path(file_okay=False, dir_okay=True), default=os.getcwd(),\n help='output directory')\n @click.pass_obj\n def write(manager: BELNamespaceManagerMixin, directory: str):\n \"\"\"Write a BEL annotation.\"\"\"\n with open(os.path.join(directory, manager.identifiers_namespace), 'w') as file:\n manager.write_bel_annotation(file)\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning an iterator over the models that are converted to the namespace.", "response": "def _iterate_namespace_models(self, **kwargs) -> Iterable:\n \"\"\"Return an iterator over the models to be converted to the namespace.\"\"\"\n return tqdm(\n self._get_query(self.namespace_model),\n total=self._count_model(self.namespace_model),\n **kwargs\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_default_namespace(self) -> Optional[Namespace]:\n return self._get_query(Namespace).filter(Namespace.url == self._get_namespace_url()).one_or_none()", "response": "Get the default BEL namespace if it exists."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a PyBEL generalized namespace entries to a set.", "response": "def _get_old_entry_identifiers(namespace: Namespace) -> Set[NamespaceEntry]:\n \"\"\"Convert a PyBEL generalized namespace entries to a set.\n\n Default to using the identifier, but can be overridden to use the name instead.\n\n >>> {term.identifier for term in namespace.entries}\n \"\"\"\n return {term.identifier for term in namespace.entries}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates an already - created namespace.", "response": "def _update_namespace(self, namespace: Namespace) -> None:\n \"\"\"Update an already-created namespace.\n\n Note: Only call this if namespace won't be none!\n \"\"\"\n old_entry_identifiers = self._get_old_entry_identifiers(namespace)\n new_count = 0\n skip_count = 0\n\n for model in self._iterate_namespace_models():\n if self._get_identifier(model) in old_entry_identifiers:\n continue\n\n entry = self._create_namespace_entry_from_model(model, namespace=namespace)\n if entry is None or entry.name is None:\n skip_count += 1\n continue\n\n new_count += 1\n self.session.add(entry)\n\n t = time.time()\n log.info('got %d new entries. skipped %d entries missing names. committing models', new_count, skip_count)\n self.session.commit()\n log.info('committed models in %.2f seconds', time.time() - t)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds this manager s namespace to the graph.", "response": "def add_namespace_to_graph(self, graph: BELGraph) -> Namespace:\n \"\"\"Add this manager's namespace to the graph.\"\"\"\n namespace = self.upload_bel_namespace()\n graph.namespace_url[namespace.keyword] = namespace.url\n\n # Add this manager as an annotation, too\n self._add_annotation_to_graph(graph)\n\n return namespace"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _add_annotation_to_graph(self, graph: BELGraph) -> None:\n if 'bio2bel' not in graph.annotation_list:\n graph.annotation_list['bio2bel'] = set()\n\n graph.annotation_list['bio2bel'].add(self.module_name)", "response": "Add this manager as an annotation to the graph."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nupload the BEL namespace to the PyBEL database.", "response": "def upload_bel_namespace(self, update: bool = False) -> Namespace:\n \"\"\"Upload the namespace to the PyBEL database.\n\n :param update: Should the namespace be updated first?\n \"\"\"\n if not self.is_populated():\n self.populate()\n\n namespace = self._get_default_namespace()\n\n if namespace is None:\n log.info('making namespace for %s', self._get_namespace_name())\n return self._make_namespace()\n\n if update:\n self._update_namespace(namespace)\n\n return namespace"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves the default namespace if it exists.", "response": "def drop_bel_namespace(self) -> Optional[Namespace]:\n \"\"\"Remove the default namespace if it exists.\"\"\"\n namespace = self._get_default_namespace()\n\n if namespace is not None:\n for entry in tqdm(namespace.entries, desc=f'deleting entries in {self._get_namespace_name()}'):\n self.session.delete(entry)\n self.session.delete(namespace)\n\n log.info('committing deletions')\n self.session.commit()\n return namespace"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite as a BEL namespace file.", "response": "def write_bel_namespace(self, file: TextIO, use_names: bool = False) -> None:\n \"\"\"Write as a BEL namespace file.\"\"\"\n if not self.is_populated():\n self.populate()\n\n if use_names and not self.has_names:\n raise ValueError\n\n values = (\n self._get_namespace_name_to_encoding(desc='writing names')\n if use_names else\n self._get_namespace_identifier_to_encoding(desc='writing identifiers')\n )\n\n write_namespace(\n namespace_name=self._get_namespace_name(),\n namespace_keyword=self._get_namespace_keyword(),\n namespace_query_url=self.identifiers_url,\n values=values,\n file=file,\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef write_bel_annotation(self, file: TextIO) -> None:\n if not self.is_populated():\n self.populate()\n\n values = self._get_namespace_name_to_encoding(desc='writing names')\n\n write_annotation(\n keyword=self._get_namespace_keyword(),\n citation_name=self._get_namespace_name(),\n description='',\n values=values,\n file=file,\n )", "response": "Write as a BEL annotation file."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nwrites a BEL namespace mapping file.", "response": "def write_bel_namespace_mappings(self, file: TextIO, **kwargs) -> None:\n \"\"\"Write a BEL namespace mapping file.\"\"\"\n json.dump(self._get_namespace_identifier_to_name(**kwargs), file, indent=2, sort_keys=True)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a BEL namespace for identifiers names name hash and mappings to the given directory.", "response": "def write_directory(self, directory: str) -> bool:\n \"\"\"Write a BEL namespace for identifiers, names, name hash, and mappings to the given directory.\"\"\"\n current_md5_hash = self.get_namespace_hash()\n md5_hash_path = os.path.join(directory, f'{self.module_name}.belns.md5')\n\n if not os.path.exists(md5_hash_path):\n old_md5_hash = None\n else:\n with open(md5_hash_path) as file:\n old_md5_hash = file.read().strip()\n\n if old_md5_hash == current_md5_hash:\n return False\n\n with open(os.path.join(directory, f'{self.module_name}.belns'), 'w') as file:\n self.write_bel_namespace(file, use_names=False)\n\n with open(md5_hash_path, 'w') as file:\n print(current_md5_hash, file=file)\n\n if self.has_names:\n with open(os.path.join(directory, f'{self.module_name}-names.belns'), 'w') as file:\n self.write_bel_namespace(file, use_names=True)\n\n with open(os.path.join(directory, f'{self.module_name}.belns.mapping'), 'w') as file:\n self.write_bel_namespace_mappings(file, desc='writing mapping')\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the namespace hash.", "response": "def get_namespace_hash(self, hash_fn=hashlib.md5) -> str:\n \"\"\"Get the namespace hash.\n\n Defaults to MD5.\n \"\"\"\n m = hash_fn()\n\n if self.has_names:\n items = self._get_namespace_name_to_encoding(desc='getting hash').items()\n else:\n items = self._get_namespace_identifier_to_encoding(desc='getting hash').items()\n\n for name, encoding in items:\n m.update(f'{name}:{encoding}'.encode('utf8'))\n return m.hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_cli(cls) -> click.Group:\n main = super().get_cli()\n\n if cls.is_namespace:\n @main.group()\n def belns():\n \"\"\"Manage BEL namespace.\"\"\"\n\n cls._cli_add_to_bel_namespace(belns)\n cls._cli_add_clear_bel_namespace(belns)\n cls._cli_add_write_bel_namespace(belns)\n\n if cls.is_annotation:\n @main.group()\n def belanno():\n \"\"\"Manage BEL annotation.\"\"\"\n\n cls._cli_add_write_bel_annotation(belanno)\n\n return main", "response": "Get a click. Group that adds BEL namespace commands."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_long_description():\n with codecs.open(os.path.join(HERE, 'README.rst'), encoding='utf-8') as f:\n long_description = f.read()\n return long_description", "response": "Get the long_description from the README. rst file. Assume UTF - 8 encoding."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef dropbox_post_factory(request):\n try:\n max_age = int(request.registry.settings.get('post_token_max_age_seconds'))\n except Exception:\n max_age = 300\n\n try:\n drop_id = parse_post_token(\n token=request.matchdict['token'],\n secret=request.registry.settings['post_secret'],\n max_age=max_age)\n except SignatureExpired:\n raise HTTPGone('dropbox expired')\n except Exception: # don't be too specific on the reason for the error\n raise HTTPNotFound('no such dropbox')\n dropbox = request.registry.settings['dropbox_container'].get_dropbox(drop_id)\n if dropbox.status_int >= 20:\n raise HTTPGone('dropbox already in processing, no longer accepts data')\n return dropbox", "response": "receives a UUID via the request and returns either a fresh or existing dropbox\n for it"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexpect the id of an existing dropbox and returns its instance", "response": "def dropbox_factory(request):\n \"\"\" expects the id of an existing dropbox and returns its instance\"\"\"\n try:\n return request.registry.settings['dropbox_container'].get_dropbox(request.matchdict['drop_id'])\n except KeyError:\n raise HTTPNotFound('no such dropbox')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_equal(a, b):\n if len(a) != len(b):\n return False\n\n result = 0\n for x, y in zip(a, b):\n result |= ord(x) ^ ord(y)\n return result == 0", "response": "a constant time comparison implementation taken from Codahale s RFC 2965"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_cli(cls) -> click.Group:\n group_help = 'Default connection at {}\\n\\nusing Bio2BEL v{}'.format(cls._get_connection(), get_version())\n\n @click.group(help=group_help)\n @click.option('-c', '--connection', default=cls._get_connection(),\n help='Defaults to {}'.format(cls._get_connection()))\n @click.pass_context\n def main(ctx, connection):\n \"\"\"Bio2BEL CLI.\"\"\"\n logging.basicConfig(level=logging.INFO, format=\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\")\n logging.getLogger('bio2bel.utils').setLevel(logging.WARNING)\n ctx.obj = cls(connection=connection)\n\n return main", "response": "Build a click CLI main function."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\npreserves the file ending but replace the name with a random token", "response": "def sanitize_filename(filename):\n \"\"\"preserve the file ending, but replace the name with a random token \"\"\"\n # TODO: fix broken splitext (it reveals everything of the filename after the first `.` - doh!)\n token = generate_drop_id()\n name, extension = splitext(filename)\n if extension:\n return '%s%s' % (token, extension)\n else:\n return token"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalls the external cleanser scripts to purge the meta data and then send the contents of the dropbox via email.", "response": "def process(self):\n \"\"\" Calls the external cleanser scripts to (optionally) purge the meta data and then\n send the contents of the dropbox via email.\n \"\"\"\n\n if self.num_attachments > 0:\n self.status = u'100 processor running'\n fs_dirty_archive = self._create_backup()\n # calling _process_attachments has the side-effect of updating `send_attachments`\n self._process_attachments()\n if self.status_int < 500 and not self.send_attachments:\n self._create_archive()\n\n if self.status_int >= 500 and self.status_int < 600:\n # cleansing failed\n # if configured, we need to move the uncleansed archive to\n # the appropriate folder and notify the editors\n if 'dropbox_dirty_archive_url_format' in self.settings:\n # create_archive\n shutil.move(\n fs_dirty_archive,\n '%s/%s.zip.pgp' % (self.container.fs_archive_dirty, self.drop_id))\n # update status\n # it's now considered 'successful-ish' again\n self.status = '490 cleanser failure but notify success'\n\n if self.status_int == 800:\n # at least one attachment was not supported\n # if configured, we need to move the uncleansed archive to\n # the appropriate folder and notify the editors\n if 'dropbox_dirty_archive_url_format' in self.settings:\n # create_archive\n shutil.move(\n fs_dirty_archive,\n '%s/%s.zip.pgp' % (self.container.fs_archive_dirty, self.drop_id))\n\n if self.status_int < 500 or self.status_int == 800:\n try:\n if self._notify_editors() > 0:\n if self.status_int < 500:\n self.status = '900 success'\n else:\n self.status = '605 smtp failure'\n except Exception:\n import traceback\n tb = traceback.format_exc()\n self.status = '610 smtp error (%s)' % tb\n\n self.cleanup()\n return self.status"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cleanup(self):\n try:\n remove(join(self.fs_path, u'message'))\n remove(join(self.fs_path, 'dirty.zip.pgp'))\n except OSError:\n pass\n shutil.rmtree(join(self.fs_path, u'clean'), ignore_errors=True)\n shutil.rmtree(join(self.fs_path, u'attach'), ignore_errors=True)", "response": "Removes all data except the status file and clean the directory."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a zip file from the drop and encrypts it to the editors.", "response": "def _create_encrypted_zip(self, source='dirty', fs_target_dir=None):\n \"\"\" creates a zip file from the drop and encrypts it to the editors.\n the encrypted archive is created inside fs_target_dir\"\"\"\n backup_recipients = [r for r in self.editors if checkRecipient(self.gpg_context, r)]\n\n # this will be handled by watchdog, no need to send for each drop\n if not backup_recipients:\n self.status = u'500 no valid keys at all'\n return self.status\n\n # calculate paths\n fs_backup = join(self.fs_path, '%s.zip' % source)\n if fs_target_dir is None:\n fs_backup_pgp = join(self.fs_path, '%s.zip.pgp' % source)\n else:\n fs_backup_pgp = join(fs_target_dir, '%s.zip.pgp' % self.drop_id)\n fs_source = dict(\n dirty=self.fs_dirty_attachments,\n clean=self.fs_cleansed_attachments\n )\n\n # create archive\n with ZipFile(fs_backup, 'w', ZIP_STORED) as backup:\n if exists(join(self.fs_path, 'message')):\n backup.write(join(self.fs_path, 'message'), arcname='message')\n for fs_attachment in fs_source[source]:\n backup.write(fs_attachment, arcname=split(fs_attachment)[-1])\n\n # encrypt archive\n with open(fs_backup, \"rb\") as backup:\n self.gpg_context.encrypt_file(\n backup,\n backup_recipients,\n always_trust=True,\n output=fs_backup_pgp\n )\n\n # cleanup\n remove(fs_backup)\n return fs_backup_pgp"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _create_archive(self):\n self.status = u'270 creating final encrypted backup of cleansed attachments'\n return self._create_encrypted_zip(source='clean', fs_target_dir=self.container.fs_archive_cleansed)", "response": "Creates an encrypted archive of the dropbox inside of the drop directory."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the number of bytes that the cleansed attachments take up on disk", "response": "def size_attachments(self):\n \"\"\"returns the number of bytes that the cleansed attachments take up on disk\"\"\"\n total_size = 0\n for attachment in self.fs_cleansed_attachments:\n total_size += stat(attachment).st_size\n return total_size"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef replies(self):\n fs_reply_path = join(self.fs_replies_path, 'message_001.txt')\n if exists(fs_reply_path):\n return [load(open(fs_reply_path, 'r'))]\n else:\n return []", "response": "returns a list of strings"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef message(self):\n try:\n with open(join(self.fs_path, u'message')) as message_file:\n return u''.join([line.decode('utf-8') for line in message_file.readlines()])\n except IOError:\n return u''", "response": "returns the user submitted text"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of absolute paths to the attachments that have been changed", "response": "def fs_dirty_attachments(self):\n \"\"\" returns a list of absolute paths to the attachements\"\"\"\n if exists(self.fs_attachment_container):\n return [join(self.fs_attachment_container, attachment)\n for attachment in listdir(self.fs_attachment_container)]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of absolute paths to the cleansed attachments", "response": "def fs_cleansed_attachments(self):\n \"\"\" returns a list of absolute paths to the cleansed attachements\"\"\"\n if exists(self.fs_cleansed_attachment_container):\n return [join(self.fs_cleansed_attachment_container, attachment)\n for attachment in listdir(self.fs_cleansed_attachment_container)]\n else:\n return []"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndestroys all cleanser slaves and rollback snapshots as well as the initial master snapshot", "response": "def reset_cleansers(confirm=True):\n \"\"\"destroys all cleanser slaves and their rollback snapshots, as well as the initial master\n snapshot - this allows re-running the jailhost deployment to recreate fresh cleansers.\"\"\"\n\n if value_asbool(confirm) and not yesno(\"\"\"\\nObacht!\n This will destroy any existing and or currently running cleanser jails.\n Are you sure that you want to continue?\"\"\"):\n exit(\"Glad I asked...\")\n\n get_vars()\n\n cleanser_count = AV['ploy_cleanser_count']\n # make sure no workers interfere:\n fab.run('ezjail-admin stop worker')\n # stop and nuke the cleanser slaves\n for cleanser_index in range(cleanser_count):\n cindex = '{:02d}'.format(cleanser_index + 1)\n fab.run('ezjail-admin stop cleanser_{cindex}'.format(cindex=cindex))\n with fab.warn_only():\n fab.run('zfs destroy tank/jails/cleanser_{cindex}@jdispatch_rollback'.format(cindex=cindex))\n fab.run('ezjail-admin delete -fw cleanser_{cindex}'.format(cindex=cindex))\n fab.run('umount -f /usr/jails/cleanser_{cindex}'.format(cindex=cindex))\n fab.run('rm -rf /usr/jails/cleanser_{cindex}'.format(cindex=cindex))\n\n with fab.warn_only():\n # remove master snapshot\n fab.run('zfs destroy -R tank/jails/cleanser@clonesource')\n\n # restart worker and cleanser to prepare for subsequent ansible configuration runs\n fab.run('ezjail-admin start worker')\n fab.run('ezjail-admin stop cleanser')\n fab.run('ezjail-admin start cleanser')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef reset_jails(confirm=True, keep_cleanser_master=True):\n if value_asbool(confirm) and not yesno(\"\"\"\\nObacht!\n This will destroy all existing and or currently running jails on the host.\n Are you sure that you want to continue?\"\"\"):\n exit(\"Glad I asked...\")\n\n reset_cleansers(confirm=False)\n\n jails = ['appserver', 'webserver', 'worker']\n if not value_asbool(keep_cleanser_master):\n jails.append('cleanser')\n\n with fab.warn_only():\n for jail in jails:\n fab.run('ezjail-admin delete -fw {jail}'.format(jail=jail))\n # remove authorized keys for no longer existing key (they are regenerated for each new worker)\n fab.run('rm /usr/jails/cleanser/usr/home/cleanser/.ssh/authorized_keys')", "response": "Stops deletes and re - creates all jails."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dict_dumper(provider):\n '''\n Dump a provider to a format that can be passed to a\n :class:`skosprovider.providers.DictionaryProvider`.\n\n :param skosprovider.providers.VocabularyProvider provider: The provider\n that wil be turned into a `dict`.\n :rtype: A list of dicts.\n\n .. versionadded:: 0.2.0\n '''\n ret = []\n for stuff in provider.get_all():\n c = provider.get_by_id(stuff['id'])\n labels = [l.__dict__ for l in c.labels]\n notes = [n.__dict__ for n in c.notes]\n sources = [s.__dict__ for s in c.sources]\n if isinstance(c, Concept):\n ret.append({\n 'id': c.id,\n 'uri': c.uri,\n 'type': c.type,\n 'labels': labels,\n 'notes': notes,\n 'sources': sources,\n 'narrower': c.narrower,\n 'broader': c.broader,\n 'related': c.related,\n 'member_of': c.member_of,\n 'subordinate_arrays': c.subordinate_arrays,\n 'matches': c.matches\n })\n elif isinstance(c, Collection):\n ret.append({\n 'id': c.id,\n 'uri': c.uri,\n 'type': c.type,\n 'labels': labels,\n 'notes': notes,\n 'sources': sources,\n 'members': c.members,\n 'member_of': c.member_of,\n 'superordinates': c.superordinates\n })\n return ret", "response": "Dumps a provider to a list of dicts."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_cli_flask(main: click.Group) -> click.Group: # noqa: D202\n\n @main.command()\n @click.option('-v', '--debug', is_flag=True)\n @click.option('-p', '--port')\n @click.option('-h', '--host')\n @click.option('-k', '--secret-key', default=os.urandom(8))\n @click.pass_obj\n def web(manager, debug, port, host, secret_key):\n \"\"\"Run the web app.\"\"\"\n if not manager.is_populated():\n click.echo('{} has not yet been populated'.format(manager.module_name))\n sys.exit(0)\n\n app = manager.get_flask_admin_app(url='/', secret_key=secret_key)\n app.run(debug=debug, host=host, port=port)\n\n return main", "response": "Add a web comand main."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding a Flask Admin interface to an application.", "response": "def _add_admin(self, app, **kwargs):\n \"\"\"Add a Flask Admin interface to an application.\n\n :param flask.Flask app: A Flask application\n :param kwargs: Keyword arguments are passed through to :class:`flask_admin.Admin`\n :rtype: flask_admin.Admin\n \"\"\"\n from flask_admin import Admin\n from flask_admin.contrib.sqla import ModelView\n\n admin = Admin(app, **kwargs)\n\n for flask_admin_model in self.flask_admin_models:\n if isinstance(flask_admin_model, tuple): # assume its a 2 tuple\n if len(flask_admin_model) != 2:\n raise TypeError\n\n model, view = flask_admin_model\n admin.add_view(view(model, self.session))\n\n else:\n admin.add_view(ModelView(flask_admin_model, self.session))\n\n return admin"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_flask_admin_app(self, url: Optional[str] = None, secret_key: Optional[str] = None):\n from flask import Flask\n\n app = Flask(__name__)\n\n if secret_key:\n app.secret_key = secret_key\n\n self._add_admin(app, url=(url or '/'))\n return app", "response": "Create a Flask application."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds a click. Group to use as a command line interface.", "response": "def get_cli(cls) -> click.Group:\n \"\"\"Add a :mod:`click` main function to use as a command line interface.\"\"\"\n main = super().get_cli()\n\n cls._cli_add_flask(main)\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhandle a system status event.", "response": "def _handle_system_status_event(self, event: SystemStatusEvent) -> None:\n \"\"\"\n DISARMED -> ARMED_AWAY -> EXIT_DELAY_START -> EXIT_DELAY_END\n (trip): -> ALARM -> OUTPUT_ON -> ALARM_RESTORE\n (disarm): -> DISARMED -> OUTPUT_OFF\n (disarm): -> DISARMED\n (disarm before EXIT_DELAY_END): -> DISARMED -> EXIT_DELAY_END\n\n TODO(NW): Check ALARM_RESTORE state transition to move back into ARMED_AWAY state\n \"\"\"\n if event.type == SystemStatusEvent.EventType.UNSEALED:\n return self._update_zone(event.zone, True)\n elif event.type == SystemStatusEvent.EventType.SEALED:\n return self._update_zone(event.zone, False)\n elif event.type == SystemStatusEvent.EventType.ALARM:\n return self._update_arming_state(ArmingState.TRIGGERED)\n elif event.type == SystemStatusEvent.EventType.ALARM_RESTORE:\n if self.arming_state != ArmingState.DISARMED:\n return self._update_arming_state(ArmingState.ARMED)\n elif event.type == SystemStatusEvent.EventType.ENTRY_DELAY_START:\n return self._update_arming_state(ArmingState.ENTRY_DELAY)\n elif event.type == SystemStatusEvent.EventType.ENTRY_DELAY_END:\n pass\n elif event.type == SystemStatusEvent.EventType.EXIT_DELAY_START:\n return self._update_arming_state(ArmingState.EXIT_DELAY)\n elif event.type == SystemStatusEvent.EventType.EXIT_DELAY_END:\n # Exit delay finished - if we were in the process of arming update\n # state to armed\n if self.arming_state == ArmingState.EXIT_DELAY:\n return self._update_arming_state(ArmingState.ARMED)\n elif event.type in Alarm.ARM_EVENTS:\n return self._update_arming_state(ArmingState.ARMING)\n elif event.type == SystemStatusEvent.EventType.DISARMED:\n return self._update_arming_state(ArmingState.DISARMED)\n elif event.type == SystemStatusEvent.EventType.ARMING_DELAYED:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dropbox_form(request):\n from briefkasten import generate_post_token\n token = generate_post_token(secret=request.registry.settings['post_secret'])\n return dict(\n action=request.route_url('dropbox_form_submit', token=token),\n fileupload_url=request.route_url('dropbox_fileupload', token=token),\n **defaults(request))", "response": "Generates a dropbox uid and renders the submission form with a signed version of that id"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dropbox_fileupload(dropbox, request):\n attachment = request.POST['attachment']\n attached = dropbox.add_attachment(attachment)\n return dict(\n files=[dict(\n name=attached,\n type=attachment.type,\n )]\n )", "response": "accepts a single file upload and adds it to the dropbox as attachment"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling the form submission redirects to the dropbox s status page", "response": "def dropbox_submission(dropbox, request):\n \"\"\" handles the form submission, redirects to the dropbox's status page.\"\"\"\n try:\n data = dropbox_schema.deserialize(request.POST)\n except Exception:\n return HTTPFound(location=request.route_url('dropbox_form'))\n\n # set the message\n dropbox.message = data.get('message')\n\n # recognize submission from watchdog\n if 'testing_secret' in dropbox.settings:\n dropbox.from_watchdog = is_equal(\n unicode(dropbox.settings['test_submission_secret']),\n data.pop('testing_secret', u''))\n\n # a non-js client might have uploaded an attachment via the form's fileupload field:\n if data.get('upload') is not None:\n dropbox.add_attachment(data['upload'])\n\n # now we can call the process method\n dropbox.submit()\n drop_url = request.route_url('dropbox_view', drop_id=dropbox.drop_id)\n print(\"Created dropbox %s\" % drop_url)\n return HTTPFound(location=drop_url)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a function that downloads and parses a GO OBO file and returns a MultiDiGraph.", "response": "def make_obo_getter(data_url: str,\n data_path: str,\n *,\n preparsed_path: Optional[str] = None,\n ) -> Callable[[Optional[str], bool, bool], MultiDiGraph]:\n \"\"\"Build a function that handles downloading OBO data and parsing it into a NetworkX object.\n\n :param data_url: The URL of the data\n :param data_path: The path where the data should get stored\n :param preparsed_path: The optional path to cache a pre-parsed json version\n \"\"\"\n download_function = make_downloader(data_url, data_path)\n\n def get_obo(url: Optional[str] = None, cache: bool = True, force_download: bool = False) -> MultiDiGraph:\n \"\"\"Download and parse a GO obo file with :mod:`obonet` into a MultiDiGraph.\n\n :param url: The URL (or file path) to download.\n :param cache: If true, the data is downloaded to the file system, else it is loaded from the internet\n :param force_download: If true, overwrites a previously cached file\n \"\"\"\n if preparsed_path is not None and os.path.exists(preparsed_path):\n return read_gpickle(preparsed_path)\n\n if url is None and cache:\n url = download_function(force_download=force_download)\n\n result = obonet.read_obo(url)\n\n if preparsed_path is not None:\n write_gpickle(result, preparsed_path)\n\n return result\n\n return get_obo"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef belns(keyword: str, file: TextIO, encoding: Optional[str], use_names: bool):\n directory = get_data_dir(keyword)\n obo_url = f'http://purl.obolibrary.org/obo/{keyword}.obo'\n obo_path = os.path.join(directory, f'{keyword}.obo')\n obo_cache_path = os.path.join(directory, f'{keyword}.obo.pickle')\n\n obo_getter = make_obo_getter(obo_url, obo_path, preparsed_path=obo_cache_path)\n graph = obo_getter()\n convert_obo_graph_to_belns(\n graph,\n file=file,\n encoding=encoding,\n use_names=use_names,\n )", "response": "Write as a BEL namespace."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef belanno(keyword: str, file: TextIO):\n directory = get_data_dir(keyword)\n obo_url = f'http://purl.obolibrary.org/obo/{keyword}.obo'\n obo_path = os.path.join(directory, f'{keyword}.obo')\n obo_cache_path = os.path.join(directory, f'{keyword}.obo.pickle')\n\n obo_getter = make_obo_getter(obo_url, obo_path, preparsed_path=obo_cache_path)\n graph = obo_getter()\n convert_obo_graph_to_belanno(\n graph,\n file=file,\n )", "response": "Write as a BEL annotation."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nhelp store an action.", "response": "def _store_helper(model: Action, session: Optional[Session] = None) -> None:\n \"\"\"Help store an action.\"\"\"\n if session is None:\n session = _make_session()\n\n session.add(model)\n session.commit()\n session.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates the tables for Bio2BEL.", "response": "def create_all(engine, checkfirst=True):\n \"\"\"Create the tables for Bio2BEL.\"\"\"\n Base.metadata.create_all(bind=engine, checkfirst=checkfirst)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef store_populate(cls, resource: str, session: Optional[Session] = None) -> 'Action':\n action = cls.make_populate(resource)\n _store_helper(action, session=session)\n return action", "response": "Store a populate event."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef store_populate_failed(cls, resource: str, session: Optional[Session] = None) -> 'Action':\n action = cls.make_populate_failed(resource)\n _store_helper(action, session=session)\n return action", "response": "Store a populate failed event."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef store_drop(cls, resource: str, session: Optional[Session] = None) -> 'Action':\n action = cls.make_drop(resource)\n _store_helper(action, session=session)\n return action", "response": "Store a drop event."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nensures the appropriate Bio2BEL data directory exists for the given module.", "response": "def get_data_dir(module_name: str) -> str:\n \"\"\"Ensure the appropriate Bio2BEL data directory exists for the given module, then returns the file path.\n\n :param module_name: The name of the module. Ex: 'chembl'\n :return: The module's data directory\n \"\"\"\n module_name = module_name.lower()\n data_dir = os.path.join(BIO2BEL_DIR, module_name)\n os.makedirs(data_dir, exist_ok=True)\n return data_dir"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbuild a module configuration class.", "response": "def get_module_config_cls(module_name: str) -> Type[_AbstractModuleConfig]: # noqa: D202\n \"\"\"Build a module configuration class.\"\"\"\n\n class ModuleConfig(_AbstractModuleConfig):\n NAME = f'bio2bel:{module_name}'\n FILES = DEFAULT_CONFIG_PATHS + [\n os.path.join(DEFAULT_CONFIG_DIRECTORY, module_name, 'config.ini')\n ]\n\n return ModuleConfig"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_connection(module_name: str, connection: Optional[str] = None) -> str:\n # 1. Use given connection\n if connection is not None:\n return connection\n\n module_name = module_name.lower()\n module_config_cls = get_module_config_cls(module_name)\n module_config = module_config_cls.load()\n\n return module_config.connection or config.connection", "response": "Returns the SQLAlchemy connection string for the given module."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all Bio2BEL modules.", "response": "def get_modules() -> Mapping:\n \"\"\"Get all Bio2BEL modules.\"\"\"\n modules = {}\n\n for entry_point in iter_entry_points(group='bio2bel', name=None):\n entry = entry_point.name\n\n try:\n modules[entry] = entry_point.load()\n except VersionConflict as exc:\n log.warning('Version conflict in %s: %s', entry, exc)\n continue\n except UnknownExtra as exc:\n log.warning('Unknown extra in %s: %s', entry, exc)\n continue\n except ImportError as exc:\n log.exception('Issue with importing module %s: %s', entry, exc)\n continue\n\n return modules"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef clear_cache(module_name: str, keep_database: bool = True) -> None:\n data_dir = get_data_dir(module_name)\n if not os.path.exists(data_dir):\n return\n for name in os.listdir(data_dir):\n if name in {'config.ini', 'cfg.ini'}:\n continue\n if name == 'cache.db' and keep_database:\n continue\n path = os.path.join(data_dir, name)\n if os.path.isdir(path):\n shutil.rmtree(path)\n else:\n os.remove(path)\n\n os.rmdir(data_dir)", "response": "Clear all downloaded files and database files."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd a populate command to main click function.", "response": "def add_cli_populate(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add a ``populate`` command to main :mod:`click` function.\"\"\"\n\n @main.command()\n @click.option('--reset', is_flag=True, help='Nuke database first')\n @click.option('--force', is_flag=True, help='Force overwrite if already populated')\n @click.pass_obj\n def populate(manager: AbstractManager, reset, force):\n \"\"\"Populate the database.\"\"\"\n if reset:\n click.echo('Deleting the previous instance of the database')\n manager.drop_all()\n click.echo('Creating new models')\n manager.create_all()\n\n if manager.is_populated() and not force:\n click.echo('Database already populated. Use --force to overwrite')\n sys.exit(0)\n\n manager.populate()\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a drop command to main click function.", "response": "def add_cli_drop(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add a ``drop`` command to main :mod:`click` function.\"\"\"\n\n @main.command()\n @click.confirmation_option(prompt='Are you sure you want to drop the db?')\n @click.pass_obj\n def drop(manager):\n \"\"\"Drop the database.\"\"\"\n manager.drop_all()\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_cli_cache(main: click.Group) -> click.Group: # noqa: D202\n\n @main.group()\n def cache():\n \"\"\"Manage cached data.\"\"\"\n\n @cache.command()\n @click.pass_obj\n def locate(manager):\n \"\"\"Print the location of the data directory.\"\"\"\n data_dir = get_data_dir(manager.module_name)\n click.echo(data_dir)\n\n @cache.command()\n @click.pass_obj\n def ls(manager):\n \"\"\"List files in the cache.\"\"\"\n data_dir = get_data_dir(manager.module_name)\n\n for path in os.listdir(data_dir):\n click.echo(path)\n\n @cache.command()\n @click.pass_obj\n def clear(manager):\n \"\"\"Clear all files from the cache.\"\"\"\n clear_cache(manager.module_name)\n\n return main", "response": "Add several commands to main click function for handling the cache."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_cli_summarize(main: click.Group) -> click.Group: # noqa: D202\n\n @main.command()\n @click.pass_obj\n def summarize(manager: AbstractManager):\n \"\"\"Summarize the contents of the database.\"\"\"\n if not manager.is_populated():\n click.secho(f'{manager.module_name} has not been populated', fg='red')\n sys.exit(1)\n\n for name, count in sorted(manager.summarize().items()):\n click.echo(f'{name.capitalize()}: {count}')\n\n return main", "response": "Add a summarize command to main."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_all(self, check_first: bool = True):\n self._metadata.create_all(self.engine, checkfirst=check_first)", "response": "Create the empty database."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndropping all tables from the database.", "response": "def drop_all(self, check_first: bool = True):\n \"\"\"Drop all tables from the database.\n\n :param bool check_first: Defaults to True, only issue DROPs for tables confirmed to be\n present in the target database. Defers to :meth:`sqlalchemy.sql.schema.MetaData.drop_all`\n \"\"\"\n self._metadata.drop_all(self.engine, checkfirst=check_first)\n self._store_drop()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_cli(cls) -> click.Group:\n main = super().get_cli()\n\n cls._cli_add_populate(main)\n cls._cli_add_drop(main)\n cls._cli_add_cache(main)\n cls._cli_add_summarize(main)\n\n return main", "response": "Get the click. Group to use as a command line interface."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef label(labels=[], language='any', sortLabel=False):\n '''\n Provide a label for a list of labels.\n\n The items in the list of labels are assumed to be either instances of\n :class:`Label`, or dicts with at least the key `label` in them. These will\n be passed to the :func:`dict_to_label` function.\n\n This method tries to find a label by looking if there's\n a pref label for the specified language. If there's no pref label,\n it looks for an alt label. It disregards hidden labels.\n\n While matching languages, preference will be given to exact matches. But,\n if no exact match is present, an inexact match will be attempted. This might\n be because a label in language `nl-BE` is being requested, but only `nl` or\n even `nl-NL` is present. Similarly, when requesting `nl`, a label with\n language `nl-NL` or even `nl-Latn-NL` will also be considered,\n providing no label is present that has an exact match with the\n requested language.\n\n If language 'any' was specified, all labels will be considered,\n regardless of language.\n\n To find a label without a specified language, pass `None` as language.\n\n If a language or None was specified, and no label could be found, this\n method will automatically try to find a label in some other language.\n\n Finally, if no label could be found, None is returned.\n\n :param string language: The preferred language to receive the label in. This\n should be a valid IANA language tag.\n :param boolean sortLabel: Should sortLabels be considered or not? If True,\n sortLabels will be preferred over prefLabels. Bear in mind that these\n are still language dependent. So, it's possible to have a different\n sortLabel per language.\n :rtype: A :class:`Label` or `None` if no label could be found.\n '''\n if not labels:\n return None\n if not language:\n language = 'und'\n labels = [dict_to_label(l) for l in labels]\n l = False\n if sortLabel:\n l = find_best_label_for_type(labels, language, 'sortLabel')\n if not l:\n l = find_best_label_for_type(labels, language, 'prefLabel')\n if not l:\n l = find_best_label_for_type(labels, language, 'altLabel')\n if l:\n return l\n else:\n return label(labels, 'any', sortLabel) if language != 'any' else None", "response": "Returns a new IANA label for a list of labels."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_best_label_for_type(labels, language, labeltype):\n '''\n Find the best label for a certain labeltype.\n\n :param list labels: A list of :class:`Label`.\n :param str language: An IANA language string, eg. `nl` or `nl-BE`.\n :param str labeltype: Type of label to look for, eg. `prefLabel`.\n '''\n typelabels = [l for l in labels if l.type == labeltype]\n if not typelabels:\n return False\n if language == 'any':\n return typelabels[0]\n exact = filter_labels_by_language(typelabels, language)\n if exact:\n return exact[0]\n inexact = filter_labels_by_language(typelabels, language, True)\n if inexact:\n return inexact[0]\n return False", "response": "Find the best label for a certain labeltype."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfilters a list of labels leaving only labels of a certain language.", "response": "def filter_labels_by_language(labels, language, broader=False):\n '''\n Filter a list of labels, leaving only labels of a certain language.\n\n :param list labels: A list of :class:`Label`.\n :param str language: An IANA language string, eg. `nl` or `nl-BE`.\n :param boolean broader: When true, will also match `nl-BE` when filtering\n on `nl`. When false, only exact matches are considered.\n '''\n if language == 'any':\n return labels\n if broader:\n language = tags.tag(language).language.format\n return [l for l in labels if tags.tag(l.language).language.format == language]\n else:\n language = tags.tag(language).format\n return [l for l in labels if tags.tag(l.language).format == language]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntransforming a dict with keys label type and language into a .", "response": "def dict_to_label(dict):\n '''\n Transform a dict with keys `label`, `type` and `language` into a\n :class:`Label`.\n\n Only the `label` key is mandatory. If `type` is not present, it will\n default to `prefLabel`. If `language` is not present, it will default\n to `und`.\n\n If the argument passed is not a dict, this method just\n returns the argument.\n '''\n try:\n return Label(\n dict['label'],\n dict.get('type', 'prefLabel'),\n dict.get('language', 'und')\n )\n except (KeyError, AttributeError, TypeError):\n return dict"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntransforming a dict with keys note type language and markup into a", "response": "def dict_to_note(dict):\n '''\n Transform a dict with keys `note`, `type` and `language` into a\n :class:`Note`.\n\n Only the `note` key is mandatory. If `type` is not present, it will\n default to `note`. If `language` is not present, it will default to `und`.\n If `markup` is not present it will default to `None`.\n\n If the argument passed is already a :class:`Note`, this method just returns\n the argument.\n '''\n if isinstance(dict, Note):\n return dict\n return Note(\n dict['note'],\n dict.get('type', 'note'),\n dict.get('language', 'und'),\n dict.get('markup')\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntransforming a dict with key citation into a Source.", "response": "def dict_to_source(dict):\n '''\n Transform a dict with key 'citation' into a :class:`Source`.\n\n If the argument passed is already a :class:`Source`, this method just\n returns the argument.\n '''\n\n if isinstance(dict, Source):\n return dict\n return Source(\n dict['citation'],\n dict.get('markup')\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a single sortkey for this conceptscheme.", "response": "def _sortkey(self, key='uri', language='any'):\n '''\n Provide a single sortkey for this conceptscheme.\n\n :param string key: Either `uri`, `label` or `sortlabel`.\n :param string language: The preferred language to receive the label in\n if key is `label` or `sortlabel`. This should be a valid IANA language tag.\n :rtype: :class:`str`\n '''\n if key == 'uri':\n return self.uri\n else:\n l = label(self.labels, language, key == 'sortlabel')\n return l.label.lower() if l else ''"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\niterates over instantiated managers.", "response": "def _iterate_managers(connection, skip):\n \"\"\"Iterate over instantiated managers.\"\"\"\n for idx, name, manager_cls in _iterate_manage_classes(skip):\n if name in skip:\n continue\n\n try:\n manager = manager_cls(connection=connection)\n except TypeError as e:\n click.secho(f'Could not instantiate {name}: {e}', fg='red')\n else:\n yield idx, name, manager"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef summarize(connection, skip):\n for idx, name, manager in _iterate_managers(connection, skip):\n click.secho(name, fg='cyan', bold=True)\n if not manager.is_populated():\n click.echo('\ud83d\udc4e unpopulated')\n continue\n\n if isinstance(manager, BELNamespaceManagerMixin):\n click.secho(f'Terms: {manager._count_model(manager.namespace_model)}', fg='green')\n\n if isinstance(manager, BELManagerMixin):\n try:\n click.secho(f'Relations: {manager.count_relations()}', fg='green')\n except TypeError as e:\n click.secho(str(e), fg='red')\n for field_name, count in sorted(manager.summarize().items()):\n click.echo(\n click.style('=> ', fg='white', bold=True) + f\"{field_name.replace('_', ' ').capitalize()}: {count}\"\n )", "response": "Summarize all available resources."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef sheet(connection, skip, file: TextIO):\n from tabulate import tabulate\n header = ['', 'Name', 'Description', 'Terms', 'Relations']\n rows = []\n\n for i, (idx, name, manager) in enumerate(_iterate_managers(connection, skip), start=1):\n try:\n if not manager.is_populated():\n continue\n except AttributeError:\n click.secho(f'{name} does not implement is_populated', fg='red')\n continue\n\n terms, relations = None, None\n if isinstance(manager, BELNamespaceManagerMixin):\n terms = manager._count_model(manager.namespace_model)\n\n if isinstance(manager, BELManagerMixin):\n try:\n relations = manager.count_relations()\n except TypeError as e:\n relations = str(e)\n\n rows.append((i, name, manager.__doc__.split('\\n')[0].strip().strip('.'), terms, relations))\n\n print(tabulate(\n rows,\n headers=header,\n # tablefmt=\"fancy_grid\",\n ))", "response": "Generate a summary sheet."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef write(connection, skip, directory, force):\n os.makedirs(directory, exist_ok=True)\n from .manager.namespace_manager import BELNamespaceManagerMixin\n for idx, name, manager in _iterate_managers(connection, skip):\n if not (isinstance(manager, AbstractManager) and isinstance(manager, BELNamespaceManagerMixin)):\n continue\n click.secho(name, fg='cyan', bold=True)\n if force:\n try:\n click.echo(f'dropping')\n manager.drop_all()\n click.echo('clearing cache')\n clear_cache(name)\n click.echo('populating')\n manager.populate()\n except Exception:\n click.secho(f'{name} failed', fg='red')\n continue\n\n try:\n r = manager.write_directory(directory)\n except TypeError as e:\n click.secho(f'error with {name}: {e}'.rstrip(), fg='red')\n else:\n if not r:\n click.echo('no update')", "response": "Write a BEL namespace names to terminology store."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef write(connection, skip, directory, force):\n os.makedirs(directory, exist_ok=True)\n from .manager.bel_manager import BELManagerMixin\n import pybel\n for idx, name, manager in _iterate_managers(connection, skip):\n if not isinstance(manager, BELManagerMixin):\n continue\n click.secho(name, fg='cyan', bold=True)\n path = os.path.join(directory, f'{name}.bel.pickle')\n if os.path.exists(path) and not force:\n click.echo('\ud83d\udc4d already exported')\n continue\n\n if not manager.is_populated():\n click.echo('\ud83d\udc4e unpopulated')\n else:\n graph = manager.to_bel()\n pybel.to_pickle(graph, path)\n pybel.to_json_path(graph, os.path.join(directory, f'{name}.bel.json'))", "response": "Write all BEL objects in a directory."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef web(connection, host, port):\n from bio2bel.web.application import create_application\n app = create_application(connection=connection)\n app.run(host=host, port=port)", "response": "Run a combine web interface."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_cli_to_bel(main: click.Group) -> click.Group: # noqa: D202\n\n @main.command()\n @click.option('-o', '--output', type=click.File('w'), default=sys.stdout)\n @click.option('-f', '--fmt', default='bel', show_default=True, help='BEL export format')\n @click.pass_obj\n def write(manager: BELManagerMixin, output: TextIO, fmt: str):\n \"\"\"Write as BEL Script.\"\"\"\n graph = manager.to_bel()\n graph.serialize(file=output, fmt=fmt)\n click.echo(graph.summary_str())\n\n return main", "response": "Add several command to main click function related to export to BEL."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds several commands to main click function related to export to BEL Commons.", "response": "def add_cli_upload_bel(main: click.Group) -> click.Group: # noqa: D202\n \"\"\"Add several command to main :mod:`click` function related to export to BEL.\"\"\"\n\n @main.command()\n @host_option\n @click.pass_obj\n def upload(manager: BELManagerMixin, host: str):\n \"\"\"Upload BEL to BEL Commons.\"\"\"\n graph = manager.to_bel()\n pybel.to_web(graph, host=host, public=True)\n\n return main"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncounting the number of BEL relations generated.", "response": "def count_relations(self) -> int:\n \"\"\"Count the number of BEL relations generated.\"\"\"\n if self.edge_model is ...:\n raise Bio2BELMissingEdgeModelError('edge_edge model is undefined/count_bel_relations is not overridden')\n elif isinstance(self.edge_model, list):\n return sum(self._count_model(m) for m in self.edge_model)\n else:\n return self._count_model(self.edge_model)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef to_indra_statements(self, *args, **kwargs):\n graph = self.to_bel(*args, **kwargs)\n return to_indra_statements(graph)", "response": "Dump this object as INDRA statements."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_cli(cls) -> click.Group:\n main = super().get_cli()\n\n @main.group()\n def bel():\n \"\"\"Manage BEL.\"\"\"\n\n cls._cli_add_to_bel(bel)\n cls._cli_add_upload_bel(bel)\n\n return main", "response": "Get a click. Group that adds added BEL commands to the BEL."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _convert_coordinatelist(input_obj):\n cdl = pgmagick.CoordinateList()\n for obj in input_obj:\n cdl.append(pgmagick.Coordinate(obj[0], obj[1]))\n return cdl", "response": "convert from list or tuple object to pgmagick. CoordinateList"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts from list or tuple object to pgmagick. VPathList", "response": "def _convert_vpathlist(input_obj):\n \"\"\"convert from 'list' or 'tuple' object to pgmagick.VPathList.\n\n :type input_obj: list or tuple\n \"\"\"\n vpl = pgmagick.VPathList()\n for obj in input_obj:\n # FIXME\n obj = pgmagick.PathMovetoAbs(pgmagick.Coordinate(obj[0], obj[1]))\n vpl.append(obj)\n return vpl"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_exif_info(self):\n _dict = {}\n for tag in _EXIF_TAGS:\n ret = self.img.attribute(\"EXIF:%s\" % tag)\n if ret and ret != 'unknown':\n _dict[tag] = ret\n return _dict", "response": "return exif - tag dict"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndraw a Bezier curve.", "response": "def bezier(self, points):\n \"\"\"Draw a Bezier-curve.\n\n :param points: ex.) ((5, 5), (6, 6), (7, 7))\n :type points: list\n \"\"\"\n coordinates = pgmagick.CoordinateList()\n for point in points:\n x, y = float(point[0]), float(point[1])\n coordinates.append(pgmagick.Coordinate(x, y))\n self.drawer.append(pgmagick.DrawableBezier(coordinates))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a color to the drawing", "response": "def color(self, x, y, paint_method):\n \"\"\"\n :param paint_method: 'point' or 'replace' or 'floodfill' or\n 'filltoborder' or 'reset'\n :type paint_method: str or pgmagick.PaintMethod\n \"\"\"\n paint_method = _convert_paintmethod(paint_method)\n color = pgmagick.DrawableColor(x, y, paint_method)\n self.drawer.append(color)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd an ellipse to the drawable", "response": "def ellipse(self, org_x, org_y, radius_x, radius_y, arc_start, arc_end):\n \"\"\"\n :param org_x: origination x axis\n :param org_y: origination y axis\n :param radius_x: radius x axis\n :param radius_y: radius y axis\n :param arc_start: arc start angle\n :param arc_end: arc end angle\n \"\"\"\n ellipse = pgmagick.DrawableEllipse(float(org_x), float(org_y),\n float(radius_x), float(radius_y),\n float(arc_start), float(arc_end))\n self.drawer.append(ellipse)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd an opacity to the drawable", "response": "def fill_opacity(self, opacity):\n \"\"\"\n :param opacity: 0.0 ~ 1.0\n \"\"\"\n opacity = pgmagick.DrawableFillOpacity(float(opacity))\n self.drawer.append(opacity)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef matte(self, x, y, paint_method):\n paint_method = _convert_paintmethod(paint_method)\n self.drawer.append(pgmagick.DrawableMatte(x, y, paint_method))", "response": "add a new darker drawable to the list of available drawers"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nscale Draw Object :param x: 0.0 ~ 1.0 :param y: 0.0 ~ 1.0", "response": "def scaling(self, x, y):\n \"\"\"Scaling Draw Object\n\n :param x: 0.0 ~ 1.0\n :param y: 0.0 ~ 1.0\n \"\"\"\n self.drawer.append(pgmagick.DrawableScaling(float(x), float(y)))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstrokes antialias on the current node", "response": "def stroke_antialias(self, flag=True):\n \"\"\"stroke antialias\n\n :param flag: True or False. (default is True)\n :type flag: bool\n \"\"\"\n antialias = pgmagick.DrawableStrokeAntialias(flag)\n self.drawer.append(antialias)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stroke_linecap(self, linecap):\n linecap = getattr(pgmagick.LineCap, \"%sCap\" % linecap.title())\n linecap = pgmagick.DrawableStrokeLineCap(linecap)\n self.drawer.append(linecap)", "response": "set to stroke linecap. linecap is defined as a string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting to stroke linejoin. linejoin can be undefined miter round bevel", "response": "def stroke_linejoin(self, linejoin):\n \"\"\"set to stroke linejoin.\n\n :param linejoin: 'undefined', 'miter', 'round', 'bevel'\n :type linejoin: str\n \"\"\"\n linejoin = getattr(pgmagick.LineJoin, \"%sJoin\" % linejoin.title())\n linejoin = pgmagick.DrawableStrokeLineJoin(linejoin)\n self.drawer.append(linejoin)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef text_antialias(self, flag=True):\n antialias = pgmagick.DrawableTextAntialias(flag)\n self.drawer.append(antialias)", "response": "text antialias is a wrapper around pgmagick. DrawableTextAntialias"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef text_decoration(self, decoration):\n if decoration.lower() == 'linethrough':\n d = pgmagick.DecorationType.LineThroughDecoration\n else:\n decoration_type_string = \"%sDecoration\" % decoration.title()\n d = getattr(pgmagick.DecorationType, \"%s\" % decoration_type_string)\n decoration = pgmagick.DrawableTextDecoration(d)\n self.drawer.append(decoration)", "response": "text decoration\n is no underline overline linethrough is no"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_version_from_pc(search_dirs, target):\n for dirname in search_dirs:\n for root, dirs, files in os.walk(dirname):\n for f in files:\n if f == target:\n file_path = os.path.join(root, target)\n _tmp = _grep(\"Version: \", file_path)\n version = _tmp.split()[1]\n print(\"Found version %s in file %s\" % (version, file_path))\n return version", "response": "get version from pc file"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning whether given library version supports given API version.", "response": "def library_supports_api(library_version, api_version, different_major_breaks_support=True):\n \"\"\"\n Returns whether api_version is supported by given library version.\n E. g. library_version (1,3,21) returns True for api_version (1,3,21), (1,3,19), (1,3,'x'), (1,2,'x'), (1, 'x')\n False for (1,3,24), (1,4,'x'), (2,'x')\n\n different_major_breaks_support - if enabled and library and api major versions are different always return False\n ex) with library_version (2,0,0) and for api_version(1,3,24) returns False if enabled, True if disabled\n \"\"\"\n assert isinstance(library_version, (tuple, list)) # won't work with e.g. generators\n assert len(library_version) == 3\n sequence_type = type(library_version) # assure we will compare same types\n api_version = sequence_type(0 if num == 'x' else num for num in api_version)\n if different_major_breaks_support and library_version[0] != api_version[0]:\n return False\n assert len(api_version) <= 3 # otherwise following comparision won't work as intended, e.g. (2, 0, 0) > (2, 0, 0, 0)\n return library_version >= api_version"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of accepting roles.", "response": "def get_roles_request(request):\n \"\"\"Returns a list of accepting roles.\"\"\"\n uuid_ = request.matchdict['uuid']\n user_id = request.matchdict.get('uid')\n\n args = [uuid_]\n if user_id is not None:\n fmt_conditional = \"AND user_id = %s\"\n args.append(user_id)\n else:\n fmt_conditional = \"\"\n\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT row_to_json(combined_rows) FROM (\nSELECT uuid, user_id AS uid, role_type AS role, accepted AS has_accepted\nFROM role_acceptances AS la\nWHERE uuid = %s {}\nORDER BY user_id ASC, role_type ASC\n) as combined_rows\"\"\".format(fmt_conditional), args)\n acceptances = [r[0] for r in cursor.fetchall()]\n\n if not acceptances:\n if user_id is not None:\n raise httpexceptions.HTTPNotFound()\n else:\n cursor.execute(\"\"\"\\\nSELECT TRUE FROM document_controls WHERE uuid = %s\"\"\", (uuid_,))\n try:\n cursor.fetchone()[0]\n except TypeError: # NoneType\n raise httpexceptions.HTTPNotFound()\n\n resp_value = acceptances\n if user_id is not None:\n resp_value = acceptances[0]\n return resp_value"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_roles_request(request):\n uuid_ = request.matchdict['uuid']\n\n posted_roles = request.json\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n remove_role_requests(cursor, uuid_, posted_roles)\n\n resp = request.response\n resp.status_int = 200\n return resp", "response": "Submission to remove a role acceptance request."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the ACL for the given content identified by uuid.", "response": "def get_acl(request):\n \"\"\"Returns the ACL for the given content identified by ``uuid``.\"\"\"\n uuid_ = request.matchdict['uuid']\n\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT TRUE FROM document_controls WHERE uuid = %s\"\"\", (uuid_,))\n try:\n # Check that it exists\n cursor.fetchone()[0]\n except TypeError:\n raise httpexceptions.HTTPNotFound()\n cursor.execute(\"\"\"\\\nSELECT row_to_json(combined_rows) FROM (\nSELECT uuid, user_id AS uid, permission\nFROM document_acl AS acl\nWHERE uuid = %s\nORDER BY user_id ASC, permission ASC\n) as combined_rows\"\"\", (uuid_,))\n acl = [r[0] for r in cursor.fetchall()]\n\n return acl"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef post_acl_request(request):\n uuid_ = request.matchdict['uuid']\n\n posted = request.json\n permissions = [(x['uid'], x['permission'],) for x in posted]\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT TRUE FROM document_controls WHERE uuid = %s::UUID\"\"\", (uuid_,))\n try:\n # Check that it exists\n cursor.fetchone()[0]\n except TypeError:\n if request.has_permission('publish.create-identifier'):\n cursor.execute(\"\"\"\\\nINSERT INTO document_controls (uuid) VALUES (%s)\"\"\", (uuid_,))\n else:\n raise httpexceptions.HTTPNotFound()\n upsert_acl(cursor, uuid_, permissions)\n\n resp = request.response\n resp.status_int = 202\n return resp", "response": "Submission to create an ACL."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_acl_request(request):\n uuid_ = request.matchdict['uuid']\n\n posted = request.json\n permissions = [(x['uid'], x['permission'],) for x in posted]\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n remove_acl(cursor, uuid_, permissions)\n\n resp = request.response\n resp.status_int = 200\n return resp", "response": "Submission to remove an ACL."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef processor(): # pragma: no cover\n registry = get_current_registry()\n settings = registry.settings\n connection_string = settings[CONNECTION_STRING]\n channels = _get_channels(settings)\n\n # Code adapted from\n # http://initd.org/psycopg/docs/advanced.html#asynchronous-notifications\n with psycopg2.connect(connection_string) as conn:\n conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)\n\n with conn.cursor() as cursor:\n for channel in channels:\n cursor.execute('LISTEN {}'.format(channel))\n logger.debug('Waiting for notifications on channel \"{}\"'\n .format(channel))\n\n registry.notify(ChannelProcessingStartUpEvent())\n\n rlist = [conn] # wait until ready for reading\n wlist = [] # wait until ready for writing\n xlist = [] # wait for an \"exceptional condition\"\n timeout = 5\n\n while True:\n if select.select(rlist, wlist, xlist, timeout) != ([], [], []):\n conn.poll()\n while conn.notifies:\n notif = conn.notifies.pop(0)\n logger.debug('Got NOTIFY: pid={} channel={} payload={}'\n .format(notif.pid, notif.channel,\n notif.payload))\n event = create_pg_notify_event(notif)\n try:\n registry.notify(event)\n except Exception:\n logger.exception('Logging an uncaught exception')", "response": "This function is used to run the asynchronous notifications on the configured channels."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngive a dbapi cursor lookup all the api keys and their information.", "response": "def lookup_api_key_info():\n \"\"\"Given a dbapi cursor, lookup all the api keys and their information.\"\"\"\n info = {}\n with db_connect() as conn:\n with conn.cursor() as cursor:\n cursor.execute(ALL_KEY_INFO_SQL_STMT)\n for row in cursor.fetchall():\n id, key, name, groups = row\n user_id = \"api_key:{}\".format(id)\n info[key] = dict(id=id, user_id=user_id,\n name=name, groups=groups)\n return info"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef includeme(config):\n api_key_authn_policy = APIKeyAuthenticationPolicy()\n config.include('openstax_accounts')\n openstax_authn_policy = config.registry.getUtility(\n IOpenstaxAccountsAuthenticationPolicy)\n\n # Set up api & user authentication policies.\n policies = [api_key_authn_policy, openstax_authn_policy]\n authn_policy = MultiAuthenticationPolicy(policies)\n config.set_authentication_policy(authn_policy)\n\n # Set up the authorization policy.\n authz_policy = ACLAuthorizationPolicy()\n config.set_authorization_policy(authz_policy)", "response": "Configuration include fuction for this module"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef expandvars_dict(settings):\n return dict(\n (key, os.path.expandvars(value))\n for key, value in settings.iteritems()\n )", "response": "Expands all environment variables in a settings dictionary."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninitialize the Sentry service with the current app s Sentry_DSN.", "response": "def initialize_sentry_integration(): # pragma: no cover\n \"\"\"\\\n Used to optionally initialize the Sentry service with this app.\n See https://docs.sentry.io/platforms/python/pyramid/\n\n \"\"\"\n # This function is not under coverage because it is boilerplate\n # from the Sentry documentation.\n try:\n import sentry_sdk\n from sentry_sdk.integrations.pyramid import PyramidIntegration\n from sentry_sdk.integrations.celery import CeleryIntegration\n except ImportError:\n warnings.warn(\n \"Sentry is not configured because the Sentry SDK \"\n \"(sentry_sdk package) is not installed\",\n UserWarning,\n )\n return # bail out early\n\n try:\n dsn = os.environ['SENTRY_DSN']\n except KeyError:\n warnings.warn(\n \"Sentry is not configured because SENTRY_DSN \"\n \"was not supplied.\",\n UserWarning,\n )\n else:\n sentry_sdk.init(\n dsn=dsn,\n integrations=[PyramidIntegration(), CeleryIntegration()],\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef task(**kwargs):\n\n def wrapper(wrapped):\n\n def callback(scanner, name, obj):\n celery_app = scanner.config.registry.celery_app\n celery_app.task(**kwargs)(obj)\n\n venusian.attach(wrapped, callback)\n return wrapped\n\n return wrapper", "response": "A function task decorator used in place of celery_app. task."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _make_celery_app(config):\n # Tack the pyramid config on the celery app for later use.\n config.registry.celery_app.conf['pyramid_config'] = config\n return config.registry.celery_app", "response": "This function creates a celery app that is used to run pyramid tasks."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprocessing post - publication events coming out of the database.", "response": "def post_publication_processing(event, cursor):\n \"\"\"Process post-publication events coming out of the database.\"\"\"\n module_ident, ident_hash = event.module_ident, event.ident_hash\n\n celery_app = get_current_registry().celery_app\n\n # Check baking is not already queued.\n cursor.execute('SELECT result_id::text '\n 'FROM document_baking_result_associations '\n 'WHERE module_ident = %s', (module_ident,))\n for result in cursor.fetchall():\n state = celery_app.AsyncResult(result[0]).state\n if state in ('QUEUED', 'STARTED', 'RETRY'):\n logger.debug('Already queued module_ident={} ident_hash={}'.format(\n module_ident, ident_hash))\n return\n\n logger.debug('Queued for processing module_ident={} ident_hash={}'.format(\n module_ident, ident_hash))\n recipe_ids = _get_recipe_ids(module_ident, cursor)\n update_module_state(cursor, module_ident, 'processing', recipe_ids[0])\n # Commit the state change before preceding.\n cursor.connection.commit()\n\n # Start of task\n # FIXME Looking up the task isn't the most clear usage here.\n task_name = 'cnxpublishing.subscribers.baking_processor'\n baking_processor = celery_app.tasks[task_name]\n result = baking_processor.delay(module_ident, ident_hash)\n baking_processor.backend.store_result(result.id, None, 'QUEUED')\n\n # Save the mapping between a celery task and this event.\n track_baking_proc_state(result, module_ident, cursor)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_archive_uri(uri):\n parsed = urlparse(uri)\n path = parsed.path.rstrip('/').split('/')\n ident_hash = path[-1]\n ident_hash = unquote(ident_hash)\n return ident_hash", "response": "Given an archive URI parse to a split ident - hash."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef declare_api_routes(config):\n add_route = config.add_route\n add_route('get-content', '/contents/{ident_hash}')\n add_route('get-resource', '/resources/{hash}')\n\n # User actions API\n add_route('license-request', '/contents/{uuid}/licensors')\n add_route('roles-request', '/contents/{uuid}/roles')\n add_route('acl-request', '/contents/{uuid}/permissions')\n\n # Publishing API\n add_route('publications', '/publications')\n add_route('get-publication', '/publications/{id}')\n add_route('publication-license-acceptance',\n '/publications/{id}/license-acceptances/{uid}')\n add_route('publication-role-acceptance',\n '/publications/{id}/role-acceptances/{uid}')\n # TODO (8-May-12017) Remove because the term collate is being phased out.\n add_route('collate-content', '/contents/{ident_hash}/collate-content')\n add_route('bake-content', '/contents/{ident_hash}/baked')\n\n # Moderation routes\n add_route('moderation', '/moderations')\n add_route('moderate', '/moderations/{id}')\n add_route('moderation-rss', '/feeds/moderations.rss')\n\n # API Key routes\n add_route('api-keys', '/api-keys')\n add_route('api-key', '/api-keys/{id}')", "response": "Declare the routes for the API."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef declare_browsable_routes(config):\n # This makes our routes slashed, which is good browser behavior.\n config.add_notfound_view(default_exceptionresponse_view,\n append_slash=True)\n\n add_route = config.add_route\n add_route('admin-index', '/a/')\n add_route('admin-moderation', '/a/moderation/')\n add_route('admin-api-keys', '/a/api-keys/')\n add_route('admin-add-site-messages', '/a/site-messages/',\n request_method='GET')\n add_route('admin-add-site-messages-POST', '/a/site-messages/',\n request_method='POST')\n add_route('admin-delete-site-messages', '/a/site-messages/',\n request_method='DELETE')\n add_route('admin-edit-site-message', '/a/site-messages/{id}/',\n request_method='GET')\n add_route('admin-edit-site-message-POST', '/a/site-messages/{id}/',\n request_method='POST')\n\n add_route('admin-content-status', '/a/content-status/')\n add_route('admin-content-status-single', '/a/content-status/{uuid}')\n\n add_route('admin-print-style', '/a/print-style/')\n add_route('admin-print-style-single', '/a/print-style/{style}')", "response": "Declare the routes that can be browsed by users."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _formatter_callback_factory(): # pragma: no cover\n includes = []\n exercise_url_template = '{baseUrl}/api/exercises?q={field}:\"{{itemCode}}\"'\n settings = get_current_registry().settings\n exercise_base_url = settings.get('embeddables.exercise.base_url', None)\n exercise_matches = [match.split(',', 1) for match in aslist(\n settings.get('embeddables.exercise.match', ''), flatten=False)]\n exercise_token = settings.get('embeddables.exercise.token', None)\n mathml_url = settings.get('mathmlcloud.url', None)\n memcache_servers = settings.get('memcache_servers')\n if memcache_servers:\n memcache_servers = memcache_servers.split()\n else:\n memcache_servers = None\n\n if exercise_base_url and exercise_matches:\n mc_client = None\n if memcache_servers:\n mc_client = memcache.Client(memcache_servers, debug=0)\n for (exercise_match, exercise_field) in exercise_matches:\n template = exercise_url_template.format(\n baseUrl=exercise_base_url, field=exercise_field)\n includes.append(exercise_callback_factory(exercise_match,\n template,\n mc_client,\n exercise_token,\n mathml_url))\n return includes", "response": "Returns a list of includes to be given to cnxepub. collation. collate."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbake the contents and persist those changes alongside the published content.", "response": "def bake(binder, recipe_id, publisher, message, cursor):\n \"\"\"Given a `Binder` as `binder`, bake the contents and\n persist those changes alongside the published content.\n\n \"\"\"\n recipe = _get_recipe(recipe_id, cursor)\n includes = _formatter_callback_factory()\n binder = collate_models(binder, ruleset=recipe, includes=includes)\n\n def flatten_filter(model):\n return (isinstance(model, cnxepub.CompositeDocument) or\n (isinstance(model, cnxepub.Binder) and\n model.metadata.get('type') == 'composite-chapter'))\n\n def only_documents_filter(model):\n return isinstance(model, cnxepub.Document) \\\n and not isinstance(model, cnxepub.CompositeDocument)\n\n for doc in cnxepub.flatten_to(binder, flatten_filter):\n publish_composite_model(cursor, doc, binder, publisher, message)\n\n for doc in cnxepub.flatten_to(binder, only_documents_filter):\n publish_collated_document(cursor, doc, binder)\n\n tree = cnxepub.model_to_tree(binder)\n publish_collated_tree(cursor, tree)\n\n return []"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef volcano(differential_dfs, title='Axial Volcano Plot', scripts_mode=\"CDN\", data_mode=\"directory\",\n organism=\"human\", q_value_column_name=\"q\", log2FC_column_name=\"logFC\",\n output_dir=\".\", filename=\"volcano.html\", version=this_version):\n \"\"\"\n Arguments:\n differential_dfs (dict or pandas.DataFrame): python dict of names to pandas dataframes, or a single dataframe, indexed by gene symbols which must have columns named log2FC and qval.\n title (str): The title of the plot (to be embedded in the html).\n scripts_mode (str): Choose from [`\"CDN\"`, `\"directory\"`, `\"inline\"`]:\n\n - `\"CDN\"` compiles a single HTML page with links to scripts hosted on a CDN,\n\n - `\"directory\"` compiles a directory with all scripts locally cached,\n\n - `\"inline\"` compiles a single HTML file with all scripts/styles inlined.\n\n data_mode (str): Choose from [\"directory\", \"inline\"]:\n\n - \"directory\" compiles a directory with all data locally cached,\n\n - \"inline\" compiles a single HTML file with all data inlined.\n\n organism (str): `\"human\"` or `\"mouse\"`\n q_value_column_name (str):\n log2FC_column_name (str):\n output_dir (str): the directory in which to output the file\n filename (str): the filename of the output file\n version (str): the version of the javascripts to use.\n Leave the default to pin the version, or choose \"latest\" to get updates,\n or choose part of the version string to get minor updates.\n Returns:\n Path: The filepath which the html was outputted to.\n \"\"\"\n\n output_dir = Path(output_dir)\n output_dir.mkdir(exist_ok=True, parents=True)\n\n # Data =======================\n\n if isinstance(differential_dfs, pd.DataFrame):\n differential_dfs = {'differential': differential_dfs}\n\n for name, df in differential_dfs.items():\n df = df[[q_value_column_name, log2FC_column_name]]\n df.columns = ['q', 'logFC']\n df = df.round(2)\n # TODO drop all zero rows\n _verify_differential_df(df)\n\n del differential_dfs[name]\n differential_dfs[_sanitize(name)] = df\n\n names_and_differentials = f\"var names_and_differentials = { '{'+ ','.join([_quote(name)+': '+df.to_json(orient='index') for name, df in differential_dfs.items()]) +'}' };\"\n\n data_block = _data_block(data_mode, [('names_and_differentials', names_and_differentials)], output_dir, include_gene_sets=False, organism=organism)\n\n # Scripts =======================\n\n scripts = third_party_scripts + [CDN_url(version)+\"js/util.js\", CDN_url(version)+\"js/GOrilla.js\", CDN_url(version)+\"js/volcano.js\"]\n\n scripts_block = _scripts_block(scripts, scripts_mode, output_dir)\n\n\n html = templateEnv.get_template('volcano.html.j2').render(title=title, scripts_block=scripts_block+'\\n'+data_block, organism=\"HOMO_SAPIENS\")\n\n (output_dir / filename).write_text(html)\n\n\n return (output_dir / filename).resolve()", "response": "Generate a new volcano file from a differential plot."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate a heatmap from a dataframe indexed by genes and sample attributes.", "response": "def heatmap(genes_by_samples_matrix, sample_attributes, title='Axial Heatmap', scripts_mode=\"CDN\", data_mode=\"directory\",\n organism=\"human\", separate_zscore_by=[\"system\"],\n output_dir=\".\", filename=\"heatmap.html\", version=this_version):\n \"\"\"\n Arguments:\n genes_by_samples_matrix (pandas.DataFrame): dataframe indexed by genes, columns are samples\n sample_attributes (pandas.DataFrame): dataframe indexed by samples, columns are sample attributes (e.g. classes)\n title (str): The title of the plot (to be embedded in the html).\n scripts_mode (str): Choose from [`\"CDN\"`, `\"directory\"`, `\"inline\"`]:\n\n - `\"CDN\"` compiles a single HTML page with links to scripts hosted on a CDN,\n\n - `\"directory\"` compiles a directory with all scripts locally cached,\n\n - `\"inline\"` compiles a single HTML file with all scripts/styles inlined.\n\n data_mode (str): Choose from [\"directory\", \"inline\"]:\n\n - \"directory\" compiles a directory with all data locally cached,\n\n - \"inline\" compiles a single HTML file with all data inlined.\n\n organism (str): `\"human\"` or `\"mouse\"`\n separate_zscore_by (list):\n output_dir (str): the directory in which to output the file\n filename (str): the filename of the output file\n version (str): the version of the javascripts to use.\n Leave the default to pin the version, or choose \"latest\" to get updates,\n or choose part of the version string to get minor updates.\n Returns:\n Path: The filepath which the html was outputted to.\n \"\"\"\n\n\n output_dir = Path(output_dir)\n output_dir.mkdir(exist_ok=True, parents=True)\n\n # Data =======================\n\n _verify_sample_by_genes_matrix(genes_by_samples_matrix)\n _verify_sample_attributes(genes_by_samples_matrix, sample_attributes)\n genes_by_samples_matrix = genes_by_samples_matrix.round(2)\n # TODO drop all zero rows\n\n matrix = f\"var matrix = {genes_by_samples_matrix.to_json(orient='columns')};\"\n classes = f\"var classes = {sample_attributes.to_json(orient='index')};\"\n\n data_block = _data_block(data_mode, [('matrix', matrix), ('classes', classes)], output_dir, organism=organism)\n\n # Scripts =======================\n\n scripts = third_party_scripts + [CDN_url(version)+\"js/util.js\", CDN_url(version)+\"js/reorder.js\", CDN_url(version)+\"js/heatmap.js\"]\n\n scripts_block = _scripts_block(scripts, scripts_mode, output_dir)\n\n\n html = templateEnv.get_template('heatmap.html.j2').render(title=title, scripts_block=scripts_block+'\\n'+data_block, separate_zscore_by=separate_zscore_by)\n\n (output_dir / filename).write_text(html)\n\n\n return (output_dir / filename).resolve()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef graph(networkx_graph, title='Axial Graph Visualization', scripts_mode=\"CDN\", data_mode=\"directory\",\n output_dir=\".\", filename=\"graph.html\", version=this_version):\n \"\"\"\n Arguments:\n networkx_graph (networkx.Graph): any instance of networkx.Graph\n title (str): The title of the plot (to be embedded in the html).\n scripts_mode (str): Choose from [`\"CDN\"`, `\"directory\"`, `\"inline\"`]:\n\n - `\"CDN\"` compiles a single HTML page with links to scripts hosted on a CDN,\n\n - `\"directory\"` compiles a directory with all scripts locally cached,\n\n - `\"inline\"` compiles a single HTML file with all scripts/styles inlined.\n\n data_mode (str): Choose from [\"directory\", \"inline\"]:\n\n - \"directory\" compiles a directory with all data locally cached,\n\n - \"inline\" compiles a single HTML file with all data inlined.\n\n output_dir (str): the directory in which to output the file\n filename (str): the filename of the output file\n version (str): the version of the javascripts to use.\n Leave the default to pin the version, or choose \"latest\" to get updates,\n or choose part of the version string to get minor updates.\n Returns:\n Path: The filepath which the html was outputted to.\n \"\"\"\n\n output_dir = Path(output_dir)\n output_dir.mkdir(exist_ok=True, parents=True)\n\n # Scripts =======================\n\n scripts = third_party_scripts + [CDN_url(version)+\"js/cola.min.js\", CDN_url(version)+\"js/graph.js\"]\n\n scripts_block = _scripts_block(scripts, scripts_mode, output_dir)\n\n # Data =======================\n\n graph_json = nx_json.node_link_data(networkx_graph)\n\n for node in graph_json['nodes']:\n for attr, val in node.items():\n if isinstance(val, numbers.Number):\n node[attr] = round(val, 2)\n for link in graph_json['links']:\n for attr, val in link.items():\n if isinstance(val, numbers.Number):\n link[attr] = round(val, 2)\n\n graph_json = f\"var graph = {json.dumps(graph_json)};\"\n\n data_block = _data_block(data_mode, [('graph', graph_json)], output_dir)\n\n html = templateEnv.get_template('graph.html.j2').render(title=title, scripts_block=scripts_block+'\\n'+data_block, nodes=networkx_graph.nodes())\n\n (output_dir / filename).write_text(html)\n\n return (output_dir / filename).resolve()", "response": "A function that generates the HTML for a single page of a graph."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef db_connect(connection_string=None, **kwargs):\n if connection_string is None:\n connection_string = get_current_registry().settings[CONNECTION_STRING]\n db_conn = psycopg2.connect(connection_string, **kwargs)\n try:\n with db_conn:\n yield db_conn\n finally:\n db_conn.close()", "response": "Function to supply a database connection object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef with_db_cursor(func):\n\n @functools.wraps(func)\n def wrapped(*args, **kwargs):\n if 'cursor' in kwargs or func.func_code.co_argcount == len(args):\n return func(*args, **kwargs)\n with db_connect() as db_connection:\n with db_connection.cursor() as cursor:\n kwargs['cursor'] = cursor\n return func(*args, **kwargs)\n\n return wrapped", "response": "Decorator that supplies a cursor to the function."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _role_type_to_db_type(type_):\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nWITH unnested_role_types AS (\n SELECT unnest(enum_range(NULL::role_types)) as role_type\n ORDER BY role_type ASC)\nSELECT array_agg(role_type)::text[] FROM unnested_role_types\"\"\")\n db_types = cursor.fetchone()[0]\n return dict(zip(cnxepub.ATTRIBUTED_ROLE_KEYS, db_types))[type_]", "response": "Translates a role type to a database compatible\n value for role_types."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a model s metadata iterate over the roles.", "response": "def _dissect_roles(metadata):\n \"\"\"Given a model's ``metadata``, iterate over the roles.\n Return values are the role identifier and role type as a tuple.\n \"\"\"\n for role_key in cnxepub.ATTRIBUTED_ROLE_KEYS:\n for user in metadata.get(role_key, []):\n if user['type'] != 'cnx-id':\n raise ValueError(\"Archive only accepts Connexions users.\")\n uid = parse_user_uri(user['id'])\n yield uid, role_key\n raise StopIteration()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nupdates or insert records for pending license acceptors.", "response": "def upsert_pending_licensors(cursor, document_id):\n \"\"\"Update or insert records for pending license acceptors.\"\"\"\n cursor.execute(\"\"\"\\\nSELECT \"uuid\", \"metadata\"\nFROM pending_documents\nWHERE id = %s\"\"\", (document_id,))\n uuid_, metadata = cursor.fetchone()\n acceptors = set([uid for uid, type_ in _dissect_roles(metadata)])\n\n # Acquire a list of existing acceptors.\n cursor.execute(\"\"\"\\\nSELECT \"user_id\", \"accepted\"\nFROM license_acceptances\nWHERE uuid = %s\"\"\", (uuid_,))\n existing_acceptors_mapping = dict(cursor.fetchall())\n\n # Who's not in the existing list?\n existing_acceptors = set(existing_acceptors_mapping.keys())\n new_acceptors = acceptors.difference(existing_acceptors)\n\n # Insert the new licensor acceptors.\n for acceptor in new_acceptors:\n cursor.execute(\"\"\"\\\nINSERT INTO license_acceptances\n (\"uuid\", \"user_id\", \"accepted\")\nVALUES (%s, %s, NULL)\"\"\", (uuid_, acceptor,))\n\n # Has everyone already accepted?\n cursor.execute(\"\"\"\\\nSELECT user_id\nFROM license_acceptances\nWHERE\n uuid = %s\n AND\n (accepted is UNKNOWN OR accepted is FALSE)\"\"\", (uuid_,))\n defectors = set(cursor.fetchall())\n\n if not defectors:\n # Update the pending document license acceptance state.\n cursor.execute(\"\"\"\\\nupdate pending_documents set license_accepted = 't'\nwhere id = %s\"\"\", (document_id,))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate or insert records for pending document role acceptance.", "response": "def upsert_pending_roles(cursor, document_id):\n \"\"\"Update or insert records for pending document role acceptance.\"\"\"\n cursor.execute(\"\"\"\\\nSELECT \"uuid\", \"metadata\"\nFROM pending_documents\nWHERE id = %s\"\"\", (document_id,))\n uuid_, metadata = cursor.fetchone()\n\n acceptors = set([(uid, _role_type_to_db_type(type_),)\n for uid, type_ in _dissect_roles(metadata)])\n\n # Upsert the user info.\n upsert_users(cursor, [x[0] for x in acceptors])\n\n # Acquire a list of existing acceptors.\n cursor.execute(\"\"\"\\\nSELECT user_id, role_type\nFROM role_acceptances\nWHERE uuid = %s\"\"\", (uuid_,))\n existing_roles = set([(r, t,) for r, t in cursor.fetchall()])\n\n # Who's not in the existing list?\n existing_acceptors = existing_roles\n new_acceptors = acceptors.difference(existing_acceptors)\n\n # Insert the new role acceptors.\n for acceptor, type_ in new_acceptors:\n cursor.execute(\"\"\"\\\nINSERT INTO role_acceptances\n (\"uuid\", \"user_id\", \"role_type\", \"accepted\")\n VALUES (%s, %s, %s, DEFAULT)\"\"\", (uuid_, acceptor, type_))\n\n # Has everyone already accepted?\n cursor.execute(\"\"\"\\\nSELECT user_id\nFROM role_acceptances\nWHERE\n uuid = %s\n AND\n (accepted is UNKNOWN OR accepted is FALSE)\"\"\", (uuid_,))\n defectors = set(cursor.fetchall())\n\n if not defectors:\n # Update the pending document license acceptance state.\n cursor.execute(\"\"\"\\\nupdate pending_documents set roles_accepted = 't'\nwhere id = %s\"\"\", (document_id,))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef obtain_licenses():\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT combined_row.url, row_to_json(combined_row) FROM (\n SELECT \"code\", \"version\", \"name\", \"url\", \"is_valid_for_publication\"\n FROM licenses) AS combined_row\"\"\")\n licenses = {r[0]: r[1] for r in cursor.fetchall()}\n return licenses", "response": "Obtain the licenses in a dictionary form keyed by url."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _validate_license(model):\n license_mapping = obtain_licenses()\n try:\n license_url = model.metadata['license_url']\n except KeyError:\n raise exceptions.MissingRequiredMetadata('license_url')\n try:\n license = license_mapping[license_url]\n except KeyError:\n raise exceptions.InvalidLicense(license_url)\n if not license['is_valid_for_publication']:\n raise exceptions.InvalidLicense(license_url)", "response": "Given the model check the license is one valid for publication."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nvalidates that all the metadata roles in the given model have valid information in them.", "response": "def _validate_roles(model):\n \"\"\"Given the model, check that all the metadata role values\n have valid information in them and any required metadata fields\n contain values.\n \"\"\"\n required_roles = (ATTRIBUTED_ROLE_KEYS[0], ATTRIBUTED_ROLE_KEYS[4],)\n for role_key in ATTRIBUTED_ROLE_KEYS:\n try:\n roles = model.metadata[role_key]\n except KeyError:\n if role_key in required_roles:\n raise exceptions.MissingRequiredMetadata(role_key)\n else:\n if role_key in required_roles and len(roles) == 0:\n raise exceptions.MissingRequiredMetadata(role_key)\n for role in roles:\n if role.get('type') != 'cnx-id':\n raise exceptions.InvalidRole(role_key, role)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nvalidating that the derived_from_uri is correctly set.", "response": "def _validate_derived_from(cursor, model):\n \"\"\"Given a database cursor and model, check the derived-from\n value accurately points to content in the archive.\n The value can be nothing or must point to existing content.\n \"\"\"\n derived_from_uri = model.metadata.get('derived_from_uri')\n if derived_from_uri is None:\n return # bail out early\n\n # Can we parse the value?\n try:\n ident_hash = parse_archive_uri(derived_from_uri)\n uuid_, version = split_ident_hash(ident_hash, split_version=True)\n except (ValueError, IdentHashSyntaxError, IdentHashShortId) as exc:\n raise exceptions.InvalidMetadata('derived_from_uri', derived_from_uri,\n original_exception=exc)\n # Is the ident-hash a valid pointer?\n args = [uuid_]\n table = 'modules'\n version_condition = ''\n if version != (None, None,):\n args.extend(version)\n table = 'modules'\n version_condition = \" AND major_version = %s\" \\\n \" AND minor_version {} %s\" \\\n .format(version[1] is None and 'is' or '=')\n cursor.execute(\"\"\"SELECT 't' FROM {} WHERE uuid::text = %s{}\"\"\"\n .format(table, version_condition), args)\n try:\n _exists = cursor.fetchone()[0] # noqa\n except TypeError: # None type\n raise exceptions.InvalidMetadata('derived_from_uri', derived_from_uri)\n\n # Assign the derived_from value so that we don't have to split it again.\n model.metadata['derived_from'] = ident_hash"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck the subjects against the subject vocabulary.", "response": "def _validate_subjects(cursor, model):\n \"\"\"Give a database cursor and model, check the subjects against\n the subject vocabulary.\n \"\"\"\n subject_vocab = [term[0] for term in acquire_subject_vocabulary(cursor)]\n subjects = model.metadata.get('subjects', [])\n invalid_subjects = [s for s in subjects if s not in subject_vocab]\n if invalid_subjects:\n raise exceptions.InvalidMetadata('subjects', invalid_subjects)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nvalidating the model using a series of checks on bits of the data.", "response": "def validate_model(cursor, model):\n \"\"\"Validates the model using a series of checks on bits of the data.\"\"\"\n # Check the license is one valid for publication.\n _validate_license(model)\n _validate_roles(model)\n\n # Other required metadata includes: title, summary\n required_metadata = ('title', 'summary',)\n for metadata_key in required_metadata:\n if model.metadata.get(metadata_key) in [None, '', []]:\n raise exceptions.MissingRequiredMetadata(metadata_key)\n\n # Ensure that derived-from values are either None\n # or point at a live record in the archive.\n _validate_derived_from(cursor, model)\n\n # FIXME Valid language code?\n\n # Are the given 'subjects'\n _validate_subjects(cursor, model)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if the given publisher of this publication is allowed to publish the content given by uuid.", "response": "def is_publication_permissible(cursor, publication_id, uuid_):\n \"\"\"Check the given publisher of this publication given\n by ``publication_id`` is allowed to publish the content given\n by ``uuid``.\n \"\"\"\n # Check the publishing user has permission to publish\n cursor.execute(\"\"\"\\\nSELECT 't'::boolean\nFROM\n pending_documents AS pd\n NATURAL JOIN document_acl AS acl\n JOIN publications AS p ON (pd.publication_id = p.id)\nWHERE\n p.id = %s\n AND\n pd.uuid = %s\n AND\n p.publisher = acl.user_id\n AND\n acl.permission = 'publish'\"\"\", (publication_id, uuid_,))\n try:\n is_allowed = cursor.fetchone()[0]\n except TypeError:\n is_allowed = False\n return is_allowed"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_pending_model(cursor, publication_id, model):\n # FIXME Too much happening here...\n assert isinstance(model, (cnxepub.Document, cnxepub.Binder,)), type(model)\n uri = model.get_uri('cnx-archive')\n\n if uri is not None:\n ident_hash = parse_archive_uri(uri)\n id, version = split_ident_hash(ident_hash, split_version=True)\n cursor.execute(\"\"\"\\\nSELECT major_version + 1 as next_version\nFROM latest_modules\nWHERE uuid = %s\nUNION ALL\nSELECT 1 as next_version\nORDER BY next_version DESC\nLIMIT 1\n\"\"\", (id,))\n next_major_version = cursor.fetchone()[0]\n if isinstance(model, cnxepub.Document):\n version = (next_major_version, None,)\n else: # ...assume it's a binder.\n version = (next_major_version, 1,)\n else:\n cursor.execute(\"\"\"\\\nWITH\ncontrol_insert AS (\n INSERT INTO document_controls (uuid) VALUES (DEFAULT) RETURNING uuid),\nacl_insert AS (\n INSERT INTO document_acl (uuid, user_id, permission)\n VALUES ((SELECT uuid FROM control_insert),\n (SELECT publisher FROM publications WHERE id = %s),\n 'publish'::permission_type))\nSELECT uuid FROM control_insert\"\"\", (publication_id,))\n id = cursor.fetchone()[0]\n if isinstance(model, cnxepub.Document):\n version = (1, None,)\n else: # ...assume it's a binder.\n version = (1, 1,)\n\n type_ = _get_type_name(model)\n model.id = str(id)\n model.metadata['version'] = '.'.join([str(v) for v in version if v])\n args = [publication_id, id, version[0], version[1], type_,\n json.dumps(model.metadata)]\n cursor.execute(\"\"\"\\\nINSERT INTO \"pending_documents\"\n (\"publication_id\", \"uuid\", \"major_version\", \"minor_version\", \"type\",\n \"license_accepted\", \"roles_accepted\", \"metadata\")\nVALUES (%s, %s, %s, %s, %s, 'f', 'f', %s)\nRETURNING \"id\", \"uuid\", module_version(\"major_version\", \"minor_version\")\n\"\"\", args)\n pending_id, uuid_, version = cursor.fetchone()\n pending_ident_hash = join_ident_hash(uuid_, version)\n\n # Assign the new ident-hash to the document for later use.\n request = get_current_request()\n path = request.route_path('get-content', ident_hash=pending_ident_hash)\n model.set_uri('cnx-archive', path)\n\n # Check if the publication is allowed for the publishing user.\n if not is_publication_permissible(cursor, publication_id, id):\n # Set the failure but continue the operation of inserting\n # the pending document.\n exc = exceptions.NotAllowed(id)\n exc.publication_id = publication_id\n exc.pending_document_id = pending_id\n exc.pending_ident_hash = pending_ident_hash\n set_publication_failure(cursor, exc)\n\n try:\n validate_model(cursor, model)\n except exceptions.PublicationException as exc:\n exc_info = sys.exc_info()\n exc.publication_id = publication_id\n exc.pending_document_id = pending_id\n exc.pending_ident_hash = pending_ident_hash\n try:\n set_publication_failure(cursor, exc)\n except BaseException:\n import traceback\n print(\"Critical data error. Immediate attention is \"\n \"required. On publication at '{}'.\"\n .format(publication_id),\n file=sys.stderr)\n # Print the critical exception.\n traceback.print_exc()\n # Raise the previous exception, so we know the original cause.\n raise exc_info[0], exc_info[1], exc_info[2]\n else:\n upsert_pending_licensors(cursor, pending_id)\n upsert_pending_roles(cursor, pending_id)\n notify_users(cursor, pending_id)\n return pending_ident_hash", "response": "Adds a pending model that is awaiting publication\n to the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_pending_model_content(cursor, publication_id, model):\n cursor.execute(\"\"\"\\\n SELECT id, ident_hash(uuid, major_version, minor_version)\n FROM pending_documents\n WHERE publication_id = %s AND uuid = %s\"\"\",\n (publication_id, model.id,))\n document_info = cursor.fetchone()\n\n def attach_info_to_exception(exc):\n \"\"\"Small cached function to grab the pending document id\n and hash to attach to the exception, which is useful when\n reading the json data on a response.\n \"\"\"\n exc.publication_id = publication_id\n exc.pending_document_id, exc.pending_ident_hash = document_info\n\n def mark_invalid_reference(reference):\n \"\"\"Set the publication to failure and attach invalid reference\n to the publication.\n \"\"\"\n exc = exceptions.InvalidReference(reference)\n attach_info_to_exception(exc)\n set_publication_failure(cursor, exc)\n\n for resource in getattr(model, 'resources', []):\n add_pending_resource(cursor, resource, document=model)\n\n if isinstance(model, cnxepub.Document):\n for reference in model.references:\n if reference.is_bound:\n reference.bind(reference.bound_model, '/resources/{}')\n elif reference.remote_type == cnxepub.INTERNAL_REFERENCE_TYPE:\n if reference.uri.startswith('#'):\n pass\n elif reference.uri.startswith('/contents'):\n ident_hash = parse_archive_uri(reference.uri)\n try:\n doc_pointer = lookup_document_pointer(\n ident_hash, cursor)\n except DocumentLookupError:\n mark_invalid_reference(reference)\n else:\n reference.bind(doc_pointer, \"/contents/{}\")\n else:\n mark_invalid_reference(reference)\n # else, it's a remote or cnx.org reference ...Do nothing.\n\n args = (psycopg2.Binary(model.content.encode('utf-8')),\n publication_id, model.id,)\n stmt = \"\"\"\\\n UPDATE \"pending_documents\"\n SET (\"content\") = (%s)\n WHERE \"publication_id\" = %s AND \"uuid\" = %s\"\"\"\n else:\n metadata = model.metadata.copy()\n # All document pointers in the tree are valid?\n document_pointers = [m for m in cnxepub.flatten_model(model)\n if isinstance(m, cnxepub.DocumentPointer)]\n document_pointer_ident_hashes = [\n (split_ident_hash(dp.ident_hash)[0],\n split_ident_hash(dp.ident_hash, split_version=True)[1][0],\n split_ident_hash(dp.ident_hash, split_version=True)[1][1],)\n # split_ident_hash(dp.ident_hash, split_version=True)[1][0],)\n for dp in document_pointers]\n document_pointer_ident_hashes = zip(*document_pointer_ident_hashes)\n\n if document_pointers:\n uuids, major_vers, minor_vers = document_pointer_ident_hashes\n cursor.execute(\"\"\"\\\nSELECT dp.uuid, module_version(dp.maj_ver, dp.min_ver) AS version,\n dp.uuid = m.uuid AS exists,\n m.portal_type = 'Module' AS is_document\nFROM (SELECT unnest(%s::uuid[]), unnest(%s::integer[]), unnest(%s::integer[]))\\\n AS dp(uuid, maj_ver, min_ver)\n LEFT JOIN modules AS m ON dp.uuid = m.uuid AND \\\n (dp.maj_ver = m.major_version OR dp.maj_ver is null)\"\"\",\n (list(uuids), list(major_vers), list(minor_vers),))\n valid_pointer_results = cursor.fetchall()\n for result_row in valid_pointer_results:\n uuid, version, exists, is_document = result_row\n if not (exists and is_document):\n dp = [dp for dp in document_pointers\n if dp.ident_hash == join_ident_hash(uuid, version)\n ][0]\n exc = exceptions.InvalidDocumentPointer(\n dp, exists=exists, is_document=is_document)\n attach_info_to_exception(exc)\n set_publication_failure(cursor, exc)\n\n # Insert the tree into the metadata.\n metadata['_tree'] = cnxepub.model_to_tree(model)\n args = (json.dumps(metadata),\n None, # TODO Render the HTML tree at ``model.content``.\n publication_id, model.id,)\n # Must pave over metadata because postgresql lacks built-in\n # json update functions.\n stmt = \"\"\"\\\n UPDATE \"pending_documents\"\n SET (\"metadata\", \"content\") = (%s, %s)\n WHERE \"publication_id\" = %s AND \"uuid\" = %s\"\"\"\n cursor.execute(stmt, args)", "response": "Adds the pending model s content to the pending model s content."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_publication_failure(cursor, exc):\n publication_id = exc.publication_id\n if publication_id is None:\n raise ValueError(\"Exception must have a ``publication_id`` value.\")\n cursor.execute(\"\"\"\\\nSELECT \"state_messages\"\nFROM publications\nWHERE id = %s\"\"\", (publication_id,))\n state_messages = cursor.fetchone()[0]\n if state_messages is None:\n state_messages = []\n entry = exc.__dict__\n entry['message'] = exc.message\n state_messages.append(entry)\n state_messages = json.dumps(state_messages)\n cursor.execute(\"\"\"\\\nUPDATE publications SET (\"state\", \"state_messages\") = (%s, %s)\nWHERE id = %s\"\"\", ('Failed/Error', state_messages, publication_id,))", "response": "Sets the publication as failed and append the failure message to the publication record."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a publication entry and makes each item a pending document.", "response": "def add_publication(cursor, epub, epub_file, is_pre_publication=False):\n \"\"\"Adds a publication entry and makes each item\n a pending document.\n \"\"\"\n publisher = epub[0].metadata['publisher']\n publish_message = epub[0].metadata['publication_message']\n epub_binary = psycopg2.Binary(epub_file.read())\n args = (publisher, publish_message, epub_binary, is_pre_publication,)\n cursor.execute(\"\"\"\\\nINSERT INTO publications\n (\"publisher\", \"publication_message\", \"epub\", \"is_pre_publication\")\nVALUES (%s, %s, %s, %s)\nRETURNING id\n\"\"\", args)\n publication_id = cursor.fetchone()[0]\n insert_mapping = {}\n\n models = set([])\n for package in epub:\n binder = cnxepub.adapt_package(package)\n if binder in models:\n continue\n for document in cnxepub.flatten_to_documents(binder):\n if document not in models:\n ident_hash = add_pending_model(\n cursor, publication_id, document)\n insert_mapping[document.id] = ident_hash\n models.add(document)\n # The binding object could be translucent/see-through,\n # (case for a binder that only contains loose-documents).\n # Otherwise we should also publish the the binder.\n if not binder.is_translucent:\n ident_hash = add_pending_model(cursor, publication_id, binder)\n insert_mapping[binder.id] = ident_hash\n models.add(binder)\n for model in models:\n # Now that all models have been given an identifier\n # we can write the content to the database.\n try:\n add_pending_model_content(cursor, publication_id, model)\n except ResourceFileExceededLimitError as e:\n e.publication_id = publication_id\n set_publication_failure(cursor, e)\n return publication_id, insert_mapping"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _check_pending_document_license_state(cursor, document_id):\n cursor.execute(\"\"\"\\\nSELECT BOOL_AND(accepted IS TRUE)\nFROM\n pending_documents AS pd,\n license_acceptances AS la\nWHERE\n pd.id = %s\n AND\n pd.uuid = la.uuid\"\"\",\n (document_id,))\n try:\n is_accepted = cursor.fetchone()[0]\n except TypeError:\n # There are no licenses associated with this document.\n is_accepted = True\n return is_accepted", "response": "Check the aggregate state on the pending document."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _check_pending_document_role_state(cursor, document_id):\n cursor.execute(\"\"\"\\\nSELECT BOOL_AND(accepted IS TRUE)\nFROM\n role_acceptances AS ra,\n pending_documents as pd\nWHERE\n pd.id = %s\n AND\n pd.uuid = ra.uuid\"\"\",\n (document_id,))\n try:\n is_accepted = cursor.fetchone()[0]\n except TypeError:\n # There are no roles to accept\n is_accepted = True\n return is_accepted", "response": "Check the aggregate state on the pending document."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating the state of the pending document.", "response": "def _update_pending_document_state(cursor, document_id, is_license_accepted,\n are_roles_accepted):\n \"\"\"Update the state of the document's state values.\"\"\"\n args = (bool(is_license_accepted), bool(are_roles_accepted),\n document_id,)\n cursor.execute(\"\"\"\\\nUPDATE pending_documents\nSET (license_accepted, roles_accepted) = (%s, %s)\nWHERE id = %s\"\"\",\n args)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncheck to see if the publication contains any revised models.", "response": "def is_revision_publication(publication_id, cursor):\n \"\"\"Checks to see if the publication contains any revised models.\n Revised in this context means that it is a new version of an\n existing piece of content.\n \"\"\"\n cursor.execute(\"\"\"\\\nSELECT 't'::boolean FROM modules\nWHERE uuid IN (SELECT uuid\n FROM pending_documents\n WHERE publication_id = %s)\nLIMIT 1\"\"\", (publication_id,))\n try:\n cursor.fetchone()[0]\n except TypeError: # NoneType\n has_revision_models = False\n else:\n has_revision_models = True\n return has_revision_models"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninvokes to update and acquire its current state. This is used to update the publication to archive.", "response": "def poke_publication_state(publication_id, cursor):\n \"\"\"Invoked to poke at the publication to update and acquire its current\n state. This is used to persist the publication to archive.\n \"\"\"\n cursor.execute(\"\"\"\\\nSELECT \"state\", \"state_messages\", \"is_pre_publication\", \"publisher\"\nFROM publications\nWHERE id = %s\"\"\", (publication_id,))\n row = cursor.fetchone()\n current_state, messages, is_pre_publication, publisher = row\n\n if current_state in END_N_INTERIM_STATES:\n # Bailout early, because the publication is either in progress\n # or has been completed.\n return current_state, messages\n\n # Check for acceptance...\n cursor.execute(\"\"\"\\\nSELECT\n pd.id, license_accepted, roles_accepted\nFROM publications AS p JOIN pending_documents AS pd ON p.id = pd.publication_id\nWHERE p.id = %s\n\"\"\", (publication_id,))\n pending_document_states = cursor.fetchall()\n publication_state_mapping = {}\n for document_state in pending_document_states:\n id, is_license_accepted, are_roles_accepted = document_state\n publication_state_mapping[id] = [is_license_accepted,\n are_roles_accepted]\n has_changed_state = False\n if is_license_accepted and are_roles_accepted:\n continue\n if not is_license_accepted:\n accepted = _check_pending_document_license_state(\n cursor, id)\n if accepted != is_license_accepted:\n has_changed_state = True\n is_license_accepted = accepted\n publication_state_mapping[id][0] = accepted\n if not are_roles_accepted:\n accepted = _check_pending_document_role_state(\n cursor, id)\n if accepted != are_roles_accepted:\n has_changed_state = True\n are_roles_accepted = accepted\n publication_state_mapping[id][1] = accepted\n if has_changed_state:\n _update_pending_document_state(cursor, id,\n is_license_accepted,\n are_roles_accepted)\n\n # Are all the documents ready for publication?\n state_lump = set([l and r for l, r in publication_state_mapping.values()])\n is_publish_ready = not (False in state_lump) and not (None in state_lump)\n change_state = \"Done/Success\"\n if not is_publish_ready:\n change_state = \"Waiting for acceptance\"\n\n # Does this publication need moderation? (ignore on pre-publication)\n # TODO Is this a revision publication? If so, it doesn't matter who the\n # user is, because they have been vetted by the previous publisher.\n # This has loopholes...\n if not is_pre_publication and is_publish_ready:\n # Has this publisher been moderated before?\n cursor.execute(\"\"\"\\\nSELECT is_moderated\nFROM users AS u LEFT JOIN publications AS p ON (u.username = p.publisher)\nWHERE p.id = %s\"\"\",\n (publication_id,))\n try:\n is_publisher_moderated = cursor.fetchone()[0]\n except TypeError:\n is_publisher_moderated = False\n\n # Are any of these documents a revision? Thus vetting of\n # the publisher was done by a vetted peer.\n if not is_publisher_moderated \\\n and not is_revision_publication(publication_id, cursor):\n # Hold up! This publish needs moderation.\n change_state = \"Waiting for moderation\"\n is_publish_ready = False\n\n # Publish the pending documents.\n if is_publish_ready:\n change_state = \"Done/Success\"\n if not is_pre_publication:\n publication_state = publish_pending(cursor, publication_id)\n else:\n cursor.execute(\"\"\"\\\nUPDATE publications\nSET state = %s\nWHERE id = %s\nRETURNING state, state_messages\"\"\", (change_state, publication_id,))\n publication_state, messages = cursor.fetchone()\n else:\n # `change_state` set prior to this...\n cursor.execute(\"\"\"\\\nUPDATE publications\nSET state = %s\nWHERE id = %s\nRETURNING state, state_messages\"\"\", (change_state, publication_id,))\n publication_state, messages = cursor.fetchone()\n\n return publication_state, messages"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks the publication s current state.", "response": "def check_publication_state(publication_id):\n \"\"\"Check the publication's current state.\"\"\"\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT \"state\", \"state_messages\"\nFROM publications\nWHERE id = %s\"\"\", (publication_id,))\n publication_state, publication_messages = cursor.fetchone()\n return publication_state, publication_messages"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngive a tree parse to a set of models", "response": "def _node_to_model(tree_or_item, metadata=None, parent=None,\n lucent_id=cnxepub.TRANSLUCENT_BINDER_ID):\n \"\"\"Given a tree, parse to a set of models\"\"\"\n if 'contents' in tree_or_item:\n # It is a binder.\n tree = tree_or_item\n binder = cnxepub.TranslucentBinder(metadata=tree)\n for item in tree['contents']:\n node = _node_to_model(item, parent=binder,\n lucent_id=lucent_id)\n if node.metadata['title'] != item['title']:\n binder.set_title_for_node(node, item['title'])\n result = binder\n else:\n # It is an item pointing at a document.\n item = tree_or_item\n result = cnxepub.DocumentPointer(item['id'], metadata=item)\n if parent is not None:\n parent.append(result)\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _reassemble_binder(id, tree, metadata):\n binder = cnxepub.Binder(id, metadata=metadata)\n for item in tree['contents']:\n node = _node_to_model(item, parent=binder)\n if node.metadata['title'] != item['title']:\n binder.set_title_for_node(node, item['title'])\n return binder", "response": "Reassemble a Binder object coming out of the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrite the pending documents to the *Connexions Archive*.", "response": "def publish_pending(cursor, publication_id):\n \"\"\"Given a publication id as ``publication_id``,\n write the documents to the *Connexions Archive*.\n \"\"\"\n cursor.execute(\"\"\"\\\nWITH state_update AS (\n UPDATE publications SET state = 'Publishing' WHERE id = %s\n)\nSELECT publisher, publication_message\nFROM publications\nWHERE id = %s\"\"\",\n (publication_id, publication_id,))\n publisher, message = cursor.fetchone()\n cursor.connection.commit()\n\n all_models = []\n\n from .publish import publish_model\n # Commit documents one at a time...\n type_ = cnxepub.Document.__name__\n cursor.execute(\"\"\"\\\nSELECT id, uuid, major_version, minor_version, metadata, content\nFROM pending_documents\nWHERE type = %s AND publication_id = %s\"\"\", (type_, publication_id,))\n for row in cursor.fetchall():\n # FIXME Oof, this is hideous!\n id, major_version, minor_version = row[1:4]\n id = str(id)\n version = '.'.join([str(x)\n for x in (major_version, minor_version,)\n if x is not None])\n metadata, content = row[-2:]\n content = content[:]\n metadata['version'] = version\n\n document = cnxepub.Document(id, content, metadata)\n for ref in document.references:\n if ref.uri.startswith('/resources/'):\n hash = ref.uri[len('/resources/'):]\n cursor.execute(\"\"\"\\\nSELECT data, media_type\nFROM pending_resources\nWHERE hash = %s\"\"\", (hash,))\n data, media_type = cursor.fetchone()\n document.resources.append(cnxepub.Resource(\n hash, io.BytesIO(data[:]), media_type, filename=hash))\n\n _ident_hash = publish_model(cursor, document, publisher, message) # noqa\n all_models.append(document)\n\n # And now the binders, one at a time...\n type_ = cnxepub.Binder.__name__\n cursor.execute(\"\"\"\\\nSELECT id, uuid, major_version, minor_version, metadata, content\nFROM pending_documents\nWHERE type = %s AND publication_id = %s\"\"\", (type_, publication_id,))\n for row in cursor.fetchall():\n id, major_version, minor_version, metadata = row[1:5]\n tree = metadata['_tree']\n binder = _reassemble_binder(str(id), tree, metadata)\n # Add the resources\n cursor.execute(\"\"\"\\\nSELECT hash, data, media_type, filename\nFROM pending_resources r\nJOIN pending_resource_associations a ON a.resource_id = r.id\nJOIN pending_documents d ON a.document_id = d.id\nWHERE ident_hash(uuid, major_version, minor_version) = %s\"\"\",\n (binder.ident_hash,))\n binder.resources = [\n cnxepub.Resource(r_hash,\n io.BytesIO(r_data[:]),\n r_media_type,\n filename=r_filename)\n for (r_hash, r_data, r_media_type, r_filename)\n in cursor.fetchall()]\n _ident_hash = publish_model(cursor, binder, publisher, message) # noqa\n all_models.append(binder)\n\n # Republish binders containing shared documents.\n from .publish import republish_binders\n _republished_ident_hashes = republish_binders(cursor, all_models) # noqa\n\n # Lastly, update the publication status.\n cursor.execute(\"\"\"\\\nUPDATE publications\nSET state = 'Done/Success'\nWHERE id = %s\nRETURNING state\"\"\", (publication_id,))\n state = cursor.fetchone()[0]\n return state"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\naccepts or deny the document license for the publication_id and user_id.", "response": "def accept_publication_license(cursor, publication_id, user_id,\n document_ids, is_accepted=False):\n \"\"\"Accept or deny the document license for the publication\n (``publication_id``) and user (at ``user_id``)\n for the documents (listed by id as ``document_ids``).\n \"\"\"\n cursor.execute(\"\"\"\\\nUPDATE license_acceptances AS la\nSET accepted = %s\nFROM pending_documents AS pd\nWHERE\n pd.publication_id = %s\n AND\n la.user_id = %s\n AND\n pd.uuid = ANY(%s::UUID[])\n AND\n pd.uuid = la.uuid\"\"\",\n (is_accepted, publication_id, user_id, document_ids,))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\naccept or deny the document role attribution for the publication_id and user_id.", "response": "def accept_publication_role(cursor, publication_id, user_id,\n document_ids, is_accepted=False):\n \"\"\"Accept or deny the document role attribution for the publication\n (``publication_id``) and user (at ``user_id``)\n for the documents (listed by id as ``document_ids``).\n \"\"\"\n cursor.execute(\"\"\"\\\nUPDATE role_acceptances AS ra\nSET accepted = %s\nFROM pending_documents AS pd\nWHERE\n pd.publication_id = %s\n AND\n ra.user_id = %s\n AND\n pd.uuid = ANY(%s::UUID[])\n AND\n pd.uuid = ra.uuid\"\"\",\n (is_accepted, publication_id, user_id, document_ids,))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef upsert_license_requests(cursor, uuid_, roles):\n if not isinstance(roles, (list, set, tuple,)):\n raise TypeError(\"``roles`` is an invalid type: {}\".format(type(roles)))\n\n acceptors = set([x['uid'] for x in roles])\n\n # Acquire a list of existing acceptors.\n cursor.execute(\"\"\"\\\nSELECT user_id, accepted FROM license_acceptances WHERE uuid = %s\"\"\",\n (uuid_,))\n existing_acceptors = cursor.fetchall()\n\n # Who's not in the existing list?\n new_acceptors = acceptors.difference([x[0] for x in existing_acceptors])\n\n # Insert the new licensor acceptors.\n if new_acceptors:\n args = []\n values_fmt = []\n for uid in new_acceptors:\n has_accepted = [x.get('has_accepted', None)\n for x in roles\n if uid == x['uid']][0]\n args.extend([uuid_, uid, has_accepted])\n values_fmt.append(\"(%s, %s, %s)\")\n values_fmt = ', '.join(values_fmt)\n cursor.execute(\"\"\"\\\nINSERT INTO license_acceptances (uuid, user_id, accepted)\nVALUES {}\"\"\".format(values_fmt), args)\n\n # Update any existing license acceptors\n acceptors = set([\n (x['uid'], x.get('has_accepted', None),)\n for x in roles\n # Prevent updating newly inserted records.\n if (x['uid'], x.get('has_accepted', None),) not in new_acceptors\n ])\n existing_acceptors = set([\n x for x in existing_acceptors\n # Prevent updating newly inserted records.\n if x[0] not in new_acceptors\n ])\n tobe_updated_acceptors = acceptors.difference(existing_acceptors)\n\n for uid, has_accepted in tobe_updated_acceptors:\n cursor.execute(\"\"\"\\\nUPDATE license_acceptances SET accepted = %s\nWHERE uuid = %s AND user_id = %s\"\"\", (has_accepted, uuid_, uid,))", "response": "Insert or update license requests for a given uuid and list of roles."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngives a uuid and list of uids remove the identified users s license acceptance entries.", "response": "def remove_license_requests(cursor, uuid_, uids):\n \"\"\"Given a ``uuid`` and list of ``uids`` (user identifiers)\n remove the identified users' license acceptance entries.\n \"\"\"\n if not isinstance(uids, (list, set, tuple,)):\n raise TypeError(\"``uids`` is an invalid type: {}\".format(type(uids)))\n\n acceptors = list(set(uids))\n\n # Remove the the entries.\n cursor.execute(\"\"\"\\\nDELETE FROM license_acceptances\nWHERE uuid = %s AND user_id = ANY(%s::text[])\"\"\", (uuid_, acceptors,))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ninserts or update a list of role requests.", "response": "def upsert_role_requests(cursor, uuid_, roles):\n \"\"\"Given a ``uuid`` and list of dicts containing the ``uid`` and\n ``role`` for creating a role acceptance entry. The ``roles`` dict\n can optionally contain a ``has_accepted`` value, which will default\n to true.\n \"\"\"\n if not isinstance(roles, (list, set, tuple,)):\n raise TypeError(\"``roles`` is an invalid type: {}\"\n .format(type(roles)))\n\n acceptors = set([(x['uid'], x['role'],) for x in roles])\n\n # Acquire a list of existing acceptors.\n cursor.execute(\"\"\"\\\nSELECT user_id, role_type, accepted\nFROM role_acceptances\nWHERE uuid = %s\"\"\", (uuid_,))\n existing_roles = cursor.fetchall()\n\n # Who's not in the existing list?\n existing_acceptors = set([(r, t,) for r, t, _ in existing_roles])\n new_acceptors = acceptors.difference(existing_acceptors)\n\n # Insert the new role acceptors.\n for acceptor, type_ in new_acceptors:\n has_accepted = [x.get('has_accepted', None)\n for x in roles\n if acceptor == x['uid'] and type_ == x['role']][0]\n cursor.execute(\"\"\"\\\nINSERT INTO role_acceptances (\"uuid\", \"user_id\", \"role_type\", \"accepted\")\nVALUES (%s, %s, %s, %s)\"\"\", (uuid_, acceptor, type_, has_accepted,))\n\n # Update any existing license acceptors\n acceptors = set([\n (x['uid'], x['role'], x.get('has_accepted', None),)\n for x in roles\n # Prevent updating newly inserted records.\n if (x['uid'], x.get('has_accepted', None),) not in new_acceptors\n ])\n existing_acceptors = set([\n x for x in existing_roles\n # Prevent updating newly inserted records.\n if (x[0], x[1],) not in new_acceptors\n ])\n tobe_updated_acceptors = acceptors.difference(existing_acceptors)\n\n for uid, type_, has_accepted in tobe_updated_acceptors:\n cursor.execute(\"\"\"\\\nUPDATE role_acceptances SET accepted = %s\nWHERE uuid = %s AND user_id = %s AND role_type = %s\"\"\",\n (has_accepted, uuid_, uid, type_,))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving the role requests for the given user identifiers and role.", "response": "def remove_role_requests(cursor, uuid_, roles):\n \"\"\"Given a ``uuid`` and list of dicts containing the ``uid``\n (user identifiers) and ``role`` for removal of the identified\n users' role acceptance entries.\n \"\"\"\n if not isinstance(roles, (list, set, tuple,)):\n raise TypeError(\"``roles`` is an invalid type: {}\".format(type(roles)))\n\n acceptors = set([(x['uid'], x['role'],) for x in roles])\n\n # Remove the the entries.\n for uid, role_type in acceptors:\n cursor.execute(\"\"\"\\\nDELETE FROM role_acceptances\nWHERE uuid = %s AND user_id = %s AND role_type = %s\"\"\",\n (uuid_, uid, role_type,))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef upsert_acl(cursor, uuid_, permissions):\n if not isinstance(permissions, (list, set, tuple,)):\n raise TypeError(\"``permissions`` is an invalid type: {}\"\n .format(type(permissions)))\n\n permissions = set(permissions)\n\n # Acquire the existin ACL.\n cursor.execute(\"\"\"\\\nSELECT user_id, permission\nFROM document_acl\nWHERE uuid = %s\"\"\", (uuid_,))\n existing = set([(r, t,) for r, t in cursor.fetchall()])\n\n # Who's not in the existing list?\n new_entries = permissions.difference(existing)\n\n # Insert the new permissions.\n for uid, permission in new_entries:\n cursor.execute(\"\"\"\\\nINSERT INTO document_acl\n (\"uuid\", \"user_id\", \"permission\")\nVALUES (%s, %s, %s)\"\"\", (uuid_, uid, permission))", "response": "Given a uuid and a set of permissions given as a\n tuple of uid and permission upsert them into the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngiving a uuid and a set of permissions remove these entries from the database.", "response": "def remove_acl(cursor, uuid_, permissions):\n \"\"\"Given a ``uuid`` and a set of permissions given as a tuple\n of ``uid`` and ``permission``, remove these entries from the database.\n \"\"\"\n if not isinstance(permissions, (list, set, tuple,)):\n raise TypeError(\"``permissions`` is an invalid type: {}\"\n .format(type(permissions)))\n\n permissions = set(permissions)\n\n # Remove the the entries.\n for uid, permission in permissions:\n cursor.execute(\"\"\"\\\nDELETE FROM document_acl\nWHERE uuid = %s AND user_id = %s AND permission = %s\"\"\",\n (uuid_, uid, permission,))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upsert_users(cursor, user_ids):\n accounts = get_current_registry().getUtility(IOpenstaxAccounts)\n\n def lookup_profile(username):\n profile = accounts.get_profile_by_username(username)\n # See structure documentation at:\n # https:///api/docs/v1/users/index\n if profile is None:\n raise UserFetchError(username)\n\n opt_attrs = ('first_name', 'last_name', 'full_name',\n 'title', 'suffix',)\n for attr in opt_attrs:\n profile.setdefault(attr, None)\n return profile\n\n _upsert_users(cursor, user_ids, lookup_profile)\n _upsert_persons(cursor, user_ids, lookup_profile)", "response": "Given a set of user identifiers and a cursor that returns a new user record in the database."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef notify_users(cursor, document_id):\n return\n\n registry = get_current_registry()\n accounts = registry.getUtility(IOpenstaxAccounts)\n cursor.execute(\"\"\"\\\nSELECT la.user_id\nFROM license_acceptances AS la\nWHERE\n la.uuid = (SELECT uuid FROM pending_documents WHERE id = %s)\n AND la.notified IS NULL AND (NOT la.accepted or la.accepted IS UNKNOWN)\n\"\"\", (document_id,))\n licensors = [x[0] for x in cursor.fetchall()]\n\n cursor.execute(\"\"\"\\\nSELECT user_id, array_agg(role_type)::text[]\nFROM role_acceptances AS ra\nWHERE\n ra.uuid = (SELECT uuid FROM pending_documents WHERE id = %s)\n AND ra.notified IS NULL AND (NOT ra.accepted or ra.accepted IS UNKNOWN)\nGROUP BY user_id\n\"\"\", (document_id,))\n roles = {u: r for u, r in cursor.fetchall()}\n\n needs_notified = set(licensors + roles.keys())\n\n for user_id in needs_notified:\n data = {\n 'user_id': user_id,\n 'full_name': None, # TODO\n 'licensor': user_id in licensors,\n 'roles': roles.get(user_id, []),\n }\n message = NOTIFICATION_TEMPLATE.render(**data)\n accounts.send_message(user_id, NOFIFICATION_SUBJECT, message)\n\n cursor.execute(\"\"\"\\\nUPDATE license_acceptances SET notified = CURRENT_TIMESTAMP\nWHERE\n uuid = (SELECT uuid FROM pending_documents WHERE id = %s)\n AND user_id = ANY (%s)\"\"\", (document_id, licensors,))\n # FIXME overwrites notified for all roles types a user might have.\n cursor.execute(\"\"\"\\\nUPDATE role_acceptances SET notified = CURRENT_TIMESTAMP\nWHERE\n uuid = (SELECT uuid FROM pending_documents WHERE id = %s)\n AND user_id = ANY (%s)\"\"\", (document_id, roles.keys(),))", "response": "Notify all users about their role and license acceptance for a piece of content associated with the given document_id."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_post_publications_state(cursor, module_ident, state_name,\n state_message=''): # pragma: no cover\n \"\"\"This sets the post-publication state in the database.\"\"\"\n cursor.execute(\"\"\"\\\nINSERT INTO post_publications\n (module_ident, state, state_message)\n VALUES (%s, %s, %s)\"\"\", (module_ident, state_name, state_message))", "response": "This function sets the post - publication state in the database."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_moderation(request):\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT row_to_json(combined_rows) FROM (\n SELECT id, created, publisher, publication_message,\n (select array_agg(row_to_json(pd))\n from pending_documents as pd\n where pd.publication_id = p.id) AS models\n FROM publications AS p\n WHERE state = 'Waiting for moderation') AS combined_rows\"\"\")\n moderations = [x[0] for x in cursor.fetchall()]\n\n return moderations", "response": "Return the list of publications that need moderation."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef includeme(config):\n settings = config.registry.settings\n session_factory = SignedCookieSessionFactory(settings['session_key'])\n config.set_session_factory(session_factory)", "response": "Configures the session manager"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef admin_print_styles(request):\n styles = []\n # This fetches all recipes that have been used to successfully bake a\n # current book plus all default recipes that have not yet been used\n # as well as \"bad\" books that are not \"current\" state, but would otherwise\n # be the latest/current for that book\n with db_connect(cursor_factory=DictCursor) as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\n WITH latest AS (SELECT print_style, recipe,\n count(*), count(nullif(stateid, 1)) as bad\n FROM modules m\n WHERE portal_type = 'Collection'\n AND recipe IS NOT NULL\n AND (\n baked IS NOT NULL OR (\n baked IS NULL AND stateid not in (1,8)\n )\n )\n AND ARRAY [major_version, minor_version] = (\n SELECT max(ARRAY[major_version,minor_version]) FROM\n modules where m.uuid= uuid)\n\n GROUP BY print_style, recipe\n ),\n defaults AS (SELECT print_style, fileid AS recipe\n FROM default_print_style_recipes d\n WHERE not exists (SELECT 1\n FROM latest WHERE latest.recipe = d.fileid)\n )\n SELECT coalesce(ps.print_style, '(custom)') as print_style,\n ps.title, coalesce(ps.recipe_type, 'web') as type,\n ps.revised, ps.tag, ps.commit_id, la.count, la.bad\n FROM latest la LEFT JOIN print_style_recipes ps ON\n la.print_style = ps.print_style AND\n la.recipe = ps.fileid\n UNION ALL\n SELECT ps.print_style, ps.title, ps.recipe_type,\n ps.revised, ps.tag, ps.commit_id, 0 AS count, 0 AS bad\n FROM defaults de JOIN print_style_recipes ps ON\n de.print_style = ps.print_style AND\n de.recipe = ps.fileid\n\n ORDER BY revised desc NULLS LAST, print_style\n\n \"\"\")\n for row in cursor.fetchall():\n styles.append({\n 'print_style': row['print_style'],\n 'title': row['title'],\n 'type': row['type'],\n 'revised': row['revised'],\n 'tag': row['tag'],\n 'commit_id': row['commit_id'],\n 'number': row['count'],\n 'bad': row['bad'],\n 'link': request.route_path('admin-print-style-single',\n style=row['print_style'])\n })\n return {'styles': styles}", "response": "Returns a dictionary of all unique print_styles and their latest tag revision and recipe_type."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn all books with any version of the given print style.", "response": "def admin_print_styles_single(request):\n \"\"\" Returns all books with any version of the given print style.\n\n Returns the print_style, recipe type, num books using the print_style,\n along with a dictionary of the book, author, revision date, recipe,\n tag of the print_style, and a link to the content.\n \"\"\"\n style = request.matchdict['style']\n # do db search to get file id and other info on the print_style\n with db_connect(cursor_factory=DictCursor) as db_conn:\n with db_conn.cursor() as cursor:\n\n if style != '(custom)':\n cursor.execute(\"\"\"\n SELECT fileid, recipe_type, title\n FROM default_print_style_recipes\n WHERE print_style=%s\n \"\"\", vars=(style,))\n info = cursor.fetchall()\n if len(info) < 1:\n current_recipe = None\n recipe_type = None\n status = None\n\n else:\n current_recipe = info[0]['fileid']\n recipe_type = info[0]['recipe_type']\n status = 'current'\n\n cursor.execute(\"\"\"\\\n SELECT name, authors, lm.revised, lm.recipe, psr.tag,\n f.sha1 as hash, psr.commit_id, uuid,\n ident_hash(uuid, major_version, minor_version)\n FROM modules as lm\n LEFT JOIN print_style_recipes as psr\n ON (psr.print_style = lm.print_style and\n psr.fileid = lm.recipe)\n LEFT JOIN files f ON psr.fileid = f.fileid\n WHERE lm.print_style=%s\n AND portal_type='Collection'\n AND ARRAY [major_version, minor_version] = (\n SELECT max(ARRAY[major_version,minor_version])\n FROM modules WHERE lm.uuid = uuid)\n\n ORDER BY psr.tag DESC;\n \"\"\", vars=(style,))\n else:\n current_recipe = '(custom)'\n recipe_type = '(custom)'\n cursor.execute(\"\"\"\\\n SELECT name, authors, lm.revised, lm.recipe, NULL as tag,\n f.sha1 as hash, NULL as commit_id, uuid,\n ident_hash(uuid, major_version, minor_version)\n FROM modules as lm\n JOIN files f ON lm.recipe = f.fileid\n WHERE portal_type='Collection'\n AND NOT EXISTS (\n SELECT 1 from print_style_recipes psr\n WHERE psr.fileid = lm.recipe)\n AND ARRAY [major_version, minor_version] = (\n SELECT max(ARRAY[major_version,minor_version])\n FROM modules WHERE lm.uuid = uuid)\n ORDER BY uuid, recipe, revised DESC;\n \"\"\", vars=(style,))\n status = '(custom)'\n\n collections = []\n for row in cursor.fetchall():\n recipe = row['recipe']\n if (status != '(custom)' and\n current_recipe is not None and\n recipe != current_recipe):\n status = 'stale'\n collections.append({\n 'title': row['name'].decode('utf-8'),\n 'authors': row['authors'],\n 'revised': row['revised'],\n 'recipe': row['hash'],\n 'recipe_link': request.route_path('get-resource',\n hash=row['hash']),\n 'tag': row['tag'],\n 'ident_hash': row['ident_hash'],\n 'link': request.route_path('get-content',\n ident_hash=row['ident_hash']),\n 'status': status,\n 'status_link': request.route_path(\n 'admin-content-status-single', uuid=row['uuid']),\n\n })\n return {'number': len(collections),\n 'collections': collections,\n 'print_style': style,\n 'recipe_type': recipe_type}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the list of API keys.", "response": "def get_api_keys(request):\n \"\"\"Return the list of API keys.\"\"\"\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT row_to_json(combined_rows) FROM (\n SELECT id, key, name, groups FROM api_keys\n) AS combined_rows\"\"\")\n api_keys = [x[0] for x in cursor.fetchall()]\n\n return api_keys"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_baking_statuses_sql(get_request):\n args = {}\n sort = get_request.get('sort', 'bpsa.created DESC')\n if (len(sort.split(\" \")) != 2 or\n sort.split(\" \")[0] not in SORTS_DICT.keys() or\n sort.split(\" \")[1] not in ARROW_MATCH.keys()):\n raise httpexceptions.HTTPBadRequest(\n 'invalid sort: {}'.format(sort))\n if sort == \"STATE ASC\" or sort == \"STATE DESC\":\n sort = 'bpsa.created DESC'\n uuid_filter = get_request.get('uuid', '').strip()\n author_filter = get_request.get('author', '').strip()\n latest_filter = get_request.get('latest', False)\n\n sql_filters = \"WHERE\"\n if latest_filter:\n sql_filters += \"\"\" ARRAY [m.major_version, m.minor_version] = (\n SELECT max(ARRAY[major_version,minor_version]) FROM\n modules where m.uuid= uuid) AND \"\"\"\n if uuid_filter != '':\n args['uuid'] = uuid_filter\n sql_filters += \" m.uuid=%(uuid)s AND \"\n if author_filter != '':\n author_filter = author_filter.decode('utf-8')\n sql_filters += \" %(author)s=ANY(m.authors) \"\n args[\"author\"] = author_filter\n\n if sql_filters.endswith(\"AND \"):\n sql_filters = sql_filters[:-4]\n if sql_filters == \"WHERE\":\n sql_filters = \"\"\n\n # FIXME celery AsyncResult API is soooo sloow that this page takes\n # 2 min. or more to load on production. As an workaround, this code\n # accesses the celery_taskmeta table directly. Need to remove that access\n # once we track enough state info ourselves. Want to track when queued,\n # started, ended, etc. for future monitoring of baking system performance\n # as well.\n # The 'limit 1' subselect is to ensure the \"oldest identical version\"\n # for recipes released as part of cnx-recipes (avoids one line per\n # identical recipe file in different releases, for a single baking job)\n\n statement = \"\"\"\n SELECT m.name, m.authors, m.uuid,\n module_version(m.major_version,m.minor_version)\n as current_version,\n m.print_style,\n CASE WHEN f.sha1 IS NOT NULL\n THEN coalesce(dps.print_style,'(custom)')\n ELSE dps.print_style\n END AS recipe_name,\n (select tag from print_style_recipes\n where print_style = m.print_style\n and fileid = m.recipe\n order by revised asc limit 1) as recipe_tag,\n coalesce(dps.fileid, m.recipe) as latest_recipe_id,\n m.recipe as recipe_id,\n f.sha1 as recipe,\n m.module_ident,\n ident_hash(m.uuid, m.major_version, m.minor_version),\n bpsa.created, ctm.traceback,\n CASE WHEN ctm.status = 'SUCCESS'\n AND ms.statename = 'fallback'\n THEN 'FALLBACK'\n ELSE ctm.status\n END as state\n FROM document_baking_result_associations AS bpsa\n INNER JOIN modules AS m USING (module_ident)\n INNER JOIN modulestates as ms USING (stateid)\n LEFT JOIN celery_taskmeta AS ctm\n ON bpsa.result_id = ctm.task_id::uuid\n LEFT JOIN default_print_style_recipes as dps\n ON dps.print_style = m.print_style\n LEFT JOIN latest_modules as lm\n ON lm.uuid=m.uuid\n LEFT JOIN files f on m.recipe = f.fileid\n {}\n ORDER BY {};\n \"\"\".format(sql_filters, sort)\n args.update({'sort': sort})\n return statement, args", "response": "Returns a SQL query that returns the list of baking statuses for the current project."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef admin_content_status(request):\n statement, sql_args = get_baking_statuses_sql(request.GET)\n\n states = []\n status_filters = request.params.getall('status_filter') or []\n state_icons = dict(STATE_ICONS)\n with db_connect(cursor_factory=DictCursor) as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(statement, vars=sql_args)\n for row in cursor.fetchall():\n message = ''\n state = row['state'] or 'PENDING'\n if status_filters and state not in status_filters:\n continue\n if state == 'FAILURE': # pragma: no cover\n if row['traceback'] is not None:\n message = row['traceback'].split(\"\\n\")[-2]\n latest_recipe = row['latest_recipe_id']\n current_recipe = row['recipe_id']\n if (current_recipe is not None and\n current_recipe != latest_recipe):\n state += ' stale_recipe'\n state_icon = state\n if state[:7] == \"SUCCESS\" and len(state) > 7:\n state_icon = 'unknown'\n states.append({\n 'title': row['name'].decode('utf-8'),\n 'authors': format_authors(row['authors']),\n 'uuid': row['uuid'],\n 'print_style': row['print_style'],\n 'print_style_link': request.route_path(\n 'admin-print-style-single', style=row['print_style']),\n 'recipe': row['recipe'],\n 'recipe_name': row['recipe_name'],\n 'recipe_tag': row['recipe_tag'],\n 'recipe_link': request.route_path(\n 'get-resource', hash=row['recipe']),\n 'created': row['created'],\n 'state': state,\n 'state_message': message,\n 'state_icon': state_icons.get(\n state_icon, DEFAULT_ICON),\n 'status_link': request.route_path(\n 'admin-content-status-single', uuid=row['uuid']),\n 'content_link': request.route_path(\n 'get-content', ident_hash=row['ident_hash'])\n })\n sort = request.params.get('sort', 'bpsa.created DESC')\n sort_match = SORTS_DICT[sort.split(' ')[0]]\n sort_arrow = ARROW_MATCH[sort.split(' ')[1]]\n if sort == \"STATE ASC\":\n states.sort(key=lambda x: x['state'])\n if sort == \"STATE DESC\":\n states.sort(key=lambda x: x['state'], reverse=True)\n\n num_entries = request.params.get('number', 100) or 100\n page = request.params.get('page', 1) or 1\n try:\n page = int(page)\n num_entries = int(num_entries)\n start_entry = (page - 1) * num_entries\n except ValueError:\n raise httpexceptions.HTTPBadRequest(\n 'invalid page({}) or entries per page({})'.\n format(page, num_entries))\n total_entries = len(states)\n states = states[start_entry: start_entry + num_entries]\n\n returns = sql_args\n returns.update({'start_entry': start_entry,\n 'num_entries': num_entries,\n 'page': page,\n 'total_entries': total_entries,\n 'states': states,\n 'sort_' + sort_match: sort_arrow,\n 'sort': sort,\n 'domain': request.host,\n 'latest_only': request.GET.get('latest', False),\n 'STATE_ICONS': STATE_ICONS,\n 'status_filters': status_filters or [\n i[0] for i in STATE_ICONS]})\n return returns", "response": "Returns a dictionary with the states and info of baking books and the filters from the GET request to pre - populate the form."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef admin_content_status_single(request):\n uuid = request.matchdict['uuid']\n try:\n UUID(uuid)\n except ValueError:\n raise httpexceptions.HTTPBadRequest(\n '{} is not a valid uuid'.format(uuid))\n\n statement, sql_args = get_baking_statuses_sql({'uuid': uuid})\n with db_connect(cursor_factory=DictCursor) as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(statement, sql_args)\n modules = cursor.fetchall()\n if len(modules) == 0:\n raise httpexceptions.HTTPBadRequest(\n '{} is not a book'.format(uuid))\n\n states = []\n collection_info = modules[0]\n\n for row in modules:\n message = ''\n state = row['state'] or 'PENDING'\n if state == 'FAILURE': # pragma: no cover\n if row['traceback'] is not None:\n message = row['traceback']\n latest_recipe = row['latest_recipe_id']\n current_recipe = row['recipe_id']\n if (latest_recipe is not None and\n current_recipe != latest_recipe):\n state += ' stale_recipe'\n states.append({\n 'version': row['current_version'],\n 'recipe': row['recipe'],\n 'created': str(row['created']),\n 'state': state,\n 'state_message': message,\n })\n\n return {'uuid': str(collection_info['uuid']),\n 'title': collection_info['name'].decode('utf-8'),\n 'authors': format_authors(collection_info['authors']),\n 'print_style': collection_info['print_style'],\n 'current_recipe': collection_info['recipe_id'],\n 'current_ident': collection_info['module_ident'],\n 'current_state': states[0]['state'],\n 'states': states}", "response": "Returns a dictionary with all the past baking statuses of a single book."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef admin_content_status_single_POST(request):\n args = admin_content_status_single(request)\n title = args['title']\n if args['current_state'] == 'SUCCESS':\n args['response'] = title + ' is not stale, no need to bake'\n return args\n\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"SELECT stateid FROM modules WHERE module_ident=%s\",\n vars=(args['current_ident'],))\n data = cursor.fetchall()\n if len(data) == 0:\n raise httpexceptions.HTTPBadRequest(\n 'invalid module_ident: {}'.format(args['current_ident']))\n if data[0][0] == 5 or data[0][0] == 6:\n args['response'] = title + ' is already baking/set to bake'\n return args\n\n cursor.execute(\"\"\"UPDATE modules SET stateid=5\n WHERE module_ident=%s\"\"\",\n vars=(args['current_ident'],))\n\n args['response'] = title + \" set to bake!\"\n\n return args", "response": "Retetriggers baking for a given book."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninsert the optional roles for the given module.", "response": "def _insert_optional_roles(cursor, model, ident):\n \"\"\"Inserts the optional roles if values for the optional roles\n exist.\n \"\"\"\n optional_roles = [\n # (, ,),\n ('translators', 4,),\n ('editors', 5,),\n ]\n for attr, role_id in optional_roles:\n roles = model.metadata.get(attr)\n if not roles:\n # Bail out, no roles for this type.\n continue\n usernames = [parse_user_uri(x['id']) for x in roles]\n cursor.execute(\"\"\"\\\nINSERT INTO moduleoptionalroles (module_ident, roleid, personids)\nVALUES (%s, %s, %s)\"\"\", (ident, role_id, usernames,))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninsert a module with the given metadata.", "response": "def _insert_metadata(cursor, model, publisher, message):\n \"\"\"Insert a module with the given ``metadata``.\"\"\"\n params = model.metadata.copy()\n params['publisher'] = publisher\n params['publication_message'] = message\n params['_portal_type'] = _model_to_portaltype(model)\n\n params['summary'] = str(cnxepub.DocumentSummaryFormatter(model))\n\n # Transform person structs to id lists for database array entry.\n for person_field in ATTRIBUTED_ROLE_KEYS:\n params[person_field] = [parse_user_uri(x['id'])\n for x in params.get(person_field, [])]\n params['parent_ident_hash'] = parse_parent_ident_hash(model)\n\n # Assign the id and version if one is known.\n if model.ident_hash is not None:\n uuid, version = split_ident_hash(model.ident_hash,\n split_version=True)\n params['_uuid'] = uuid\n params['_major_version'], params['_minor_version'] = version\n # Lookup legacy ``moduleid``.\n cursor.execute(\"SELECT moduleid FROM latest_modules WHERE uuid = %s\",\n (uuid,))\n # There is the chance that a uuid and version have been set,\n # but a previous publication does not exist. Therefore the\n # moduleid will not be found. This happens on a pre-publication.\n try:\n moduleid = cursor.fetchone()[0]\n except TypeError: # NoneType\n moduleid = None\n params['_moduleid'] = moduleid\n\n # Verify that uuid is reserved in document_contols. If not, add it.\n cursor.execute(\"SELECT * from document_controls where uuid = %s\",\n (uuid,))\n try:\n cursor.fetchone()[0]\n except TypeError: # NoneType\n cursor.execute(\"INSERT INTO document_controls (uuid) VALUES (%s)\",\n (uuid,))\n\n created = model.metadata.get('created', None)\n # Format the statement to accept the identifiers.\n stmt = MODULE_INSERTION_TEMPLATE.format(**{\n '__uuid__': \"%(_uuid)s::uuid\",\n '__major_version__': \"%(_major_version)s\",\n '__minor_version__': \"%(_minor_version)s\",\n '__moduleid__': moduleid is None and \"DEFAULT\" or \"%(_moduleid)s\",\n '__created__': created is None and \"DEFAULT\" or \"%(created)s\",\n })\n else:\n created = model.metadata.get('created', None)\n # Format the statement for defaults.\n stmt = MODULE_INSERTION_TEMPLATE.format(**{\n '__uuid__': \"DEFAULT\",\n '__major_version__': \"DEFAULT\",\n '__minor_version__': \"DEFAULT\",\n '__moduleid__': \"DEFAULT\",\n '__created__': created is None and \"DEFAULT\" or \"%(created)s\",\n })\n\n # Insert the metadata\n cursor.execute(stmt, params)\n module_ident, ident_hash = cursor.fetchone()\n # Insert optional roles\n _insert_optional_roles(cursor, model, module_ident)\n\n return module_ident, ident_hash"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_file_sha1(file):\n bits = file.read()\n file.seek(0)\n h = hashlib.new('sha1', bits).hexdigest()\n return h", "response": "Return the SHA1 hash of the given a file - like object as file."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ninsert the file and media_type into the files table.", "response": "def _insert_file(cursor, file, media_type):\n \"\"\"Upsert the ``file`` and ``media_type`` into the files table.\n Returns the ``fileid`` and ``sha1`` of the upserted file.\n\n \"\"\"\n resource_hash = _get_file_sha1(file)\n cursor.execute(\"SELECT fileid FROM files WHERE sha1 = %s\",\n (resource_hash,))\n try:\n fileid = cursor.fetchone()[0]\n except (IndexError, TypeError):\n cursor.execute(\"INSERT INTO files (file, media_type) \"\n \"VALUES (%s, %s)\"\n \"RETURNING fileid\",\n (psycopg2.Binary(file.read()), media_type,))\n fileid = cursor.fetchone()[0]\n return fileid, resource_hash"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninsert a resource into the modules_files table. This will create a new file entry and associates an existing one.", "response": "def _insert_resource_file(cursor, module_ident, resource):\n \"\"\"Insert a resource into the modules_files table. This will\n create a new file entry or associates an existing one.\n \"\"\"\n with resource.open() as file:\n fileid, _ = _insert_file(cursor, file, resource.media_type)\n\n # Is this file legitimately used twice within the same content?\n cursor.execute(\"\"\"\\\nselect\n (fileid = %s) as is_same_file\nfrom module_files\nwhere module_ident = %s and filename = %s\"\"\",\n (fileid, module_ident, resource.filename,))\n try:\n is_same_file = cursor.fetchone()[0]\n except TypeError: # NoneType\n is_same_file = None\n if is_same_file:\n # All is good, bail out.\n return\n elif is_same_file is not None: # pragma: no cover\n # This means the file is not the same, but a filename\n # conflict exists.\n # FFF At this time, it is impossible to get to this logic.\n raise Exception(\"filename conflict\")\n\n args = (module_ident, fileid, resource.filename,)\n cursor.execute(\"\"\"\\\nINSERT INTO module_files (module_ident, fileid, filename)\nVALUES (%s, %s, %s)\"\"\", args)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _insert_tree(cursor, tree, parent_id=None, index=0, is_collated=False):\n if isinstance(tree, dict):\n if tree['id'] == 'subcol':\n document_id = None\n title = tree['title']\n else:\n cursor.execute(\"\"\"\\\n SELECT module_ident, name\n FROM modules\n WHERE ident_hash(uuid,major_version,minor_version) = %s\n \"\"\", (tree['id'],))\n try:\n document_id, document_title = cursor.fetchone()\n except TypeError: # NoneType\n raise ValueError(\"Missing published document for '{}'.\"\n .format(tree['id']))\n if tree.get('title', None):\n title = tree['title']\n else:\n title = document_title\n # TODO We haven't settled on a flag (name or value)\n # to pin the node to a specific version.\n is_latest = True\n cursor.execute(TREE_NODE_INSERT,\n dict(document_id=document_id, parent_id=parent_id,\n title=title, child_order=index,\n is_latest=is_latest, is_collated=is_collated))\n node_id = cursor.fetchone()[0]\n if 'contents' in tree:\n _insert_tree(cursor, tree['contents'], parent_id=node_id,\n is_collated=is_collated)\n elif isinstance(tree, list):\n for tree_node in tree:\n _insert_tree(cursor, tree_node, parent_id=parent_id,\n index=tree.index(tree_node), is_collated=is_collated)", "response": "Inserts a binder tree into the archive."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\npublishes the given model and returns its ident_hash.", "response": "def publish_model(cursor, model, publisher, message):\n \"\"\"Publishes the ``model`` and return its ident_hash.\"\"\"\n publishers = publisher\n if isinstance(publishers, list) and len(publishers) > 1:\n raise ValueError(\"Only one publisher is allowed. '{}' \"\n \"were given: {}\"\n .format(len(publishers), publishers))\n module_ident, ident_hash = _insert_metadata(cursor, model,\n publisher, message)\n\n for resource in getattr(model, 'resources', []):\n _insert_resource_file(cursor, module_ident, resource)\n\n if isinstance(model, Document):\n html = bytes(cnxepub.DocumentContentFormatter(model))\n sha1 = hashlib.new('sha1', html).hexdigest()\n cursor.execute(\"SELECT fileid FROM files WHERE sha1 = %s\", (sha1,))\n try:\n fileid = cursor.fetchone()[0]\n except TypeError:\n file_args = {\n 'media_type': 'text/html',\n 'data': psycopg2.Binary(html),\n }\n cursor.execute(\"\"\"\\\n insert into files (file, media_type)\n VALUES (%(data)s, %(media_type)s)\n returning fileid\"\"\", file_args)\n fileid = cursor.fetchone()[0]\n args = {\n 'module_ident': module_ident,\n 'filename': 'index.cnxml.html',\n 'fileid': fileid,\n }\n cursor.execute(\"\"\"\\\n INSERT INTO module_files\n (module_ident, fileid, filename)\n VALUES\n (%(module_ident)s, %(fileid)s, %(filename)s)\"\"\", args)\n\n elif isinstance(model, Binder):\n tree = cnxepub.model_to_tree(model)\n tree = _insert_tree(cursor, tree)\n return ident_hash"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\npublishing the model and returns its ident_hash.", "response": "def publish_composite_model(cursor, model, parent_model, publisher, message):\n \"\"\"Publishes the ``model`` and return its ident_hash.\"\"\"\n if not (isinstance(model, CompositeDocument) or\n (isinstance(model, Binder) and\n model.metadata.get('type') == 'composite-chapter')):\n raise ValueError(\"This function only publishes Composite\"\n \"objects. '{}' was given.\".format(type(model)))\n if issequence(publisher) and len(publisher) > 1:\n raise ValueError(\"Only one publisher is allowed. '{}' \"\n \"were given: {}\"\n .format(len(publisher), publisher))\n module_ident, ident_hash = _insert_metadata(cursor, model,\n publisher, message)\n\n model.id, model.metadata['version'] = split_ident_hash(ident_hash)\n model.set_uri('cnx-archive', ident_hash)\n\n for resource in model.resources:\n _insert_resource_file(cursor, module_ident, resource)\n\n if isinstance(model, CompositeDocument):\n html = bytes(cnxepub.DocumentContentFormatter(model))\n fileid, _ = _insert_file(cursor, io.BytesIO(html), 'text/html')\n file_arg = {\n 'module_ident': module_ident,\n 'parent_ident_hash': parent_model.ident_hash,\n 'fileid': fileid,\n }\n cursor.execute(\"\"\"\\\n INSERT INTO collated_file_associations\n (context, item, fileid)\n VALUES\n ((SELECT module_ident FROM modules\n WHERE ident_hash(uuid, major_version, minor_version)\n = %(parent_ident_hash)s),\n %(module_ident)s, %(fileid)s)\"\"\", file_arg)\n\n return ident_hash"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\npublishing a given module s collated content in the context of the parent_model.", "response": "def publish_collated_document(cursor, model, parent_model):\n \"\"\"Publish a given `module`'s collated content in the context of\n the `parent_model`. Note, the model's content is expected to already\n have the collated content. This will just persist that content to\n the archive.\n\n \"\"\"\n html = bytes(cnxepub.DocumentContentFormatter(model))\n sha1 = hashlib.new('sha1', html).hexdigest()\n cursor.execute(\"SELECT fileid FROM files WHERE sha1 = %s\", (sha1,))\n try:\n fileid = cursor.fetchone()[0]\n except TypeError:\n file_args = {\n 'media_type': 'text/html',\n 'data': psycopg2.Binary(html),\n }\n cursor.execute(\"\"\"\\\n INSERT INTO files (file, media_type)\n VALUES (%(data)s, %(media_type)s)\n RETURNING fileid\"\"\", file_args)\n fileid = cursor.fetchone()[0]\n args = {\n 'module_ident_hash': model.ident_hash,\n 'parent_ident_hash': parent_model.ident_hash,\n 'fileid': fileid,\n }\n stmt = \"\"\"\\\nINSERT INTO collated_file_associations (context, item, fileid)\nVALUES\n ((SELECT module_ident FROM modules\n WHERE ident_hash(uuid, major_version, minor_version)\n = %(parent_ident_hash)s),\n (SELECT module_ident FROM modules\n WHERE ident_hash(uuid, major_version, minor_version)\n = %(module_ident_hash)s),\n %(fileid)s)\"\"\"\n cursor.execute(stmt, args)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\npublishing a given collated tree.", "response": "def publish_collated_tree(cursor, tree):\n \"\"\"Publish a given collated `tree` (containing newly added\n `CompositeDocument` objects and number inforation)\n alongside the original tree.\n\n \"\"\"\n tree = _insert_tree(cursor, tree, is_collated=True)\n return tree"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrepublish the Binders that share Documents in the publication context.", "response": "def republish_binders(cursor, models):\n \"\"\"Republish the Binders that share Documents in the publication context.\n This needs to be given all the models in the publication context.\"\"\"\n documents = set([])\n binders = set([])\n history_mapping = {} # : \n if not isinstance(models, (list, tuple, set,)):\n raise TypeError(\"``models`` Must be a sequence of model objects.\"\n \"We were given: {}\".format(models))\n for model in models:\n if isinstance(model, (cnxepub.Binder,)):\n binders.add(split_ident_hash(model.ident_hash)[0])\n for doc in cnxepub.flatten_to_documents(model):\n documents.add(split_ident_hash(doc.ident_hash))\n else:\n documents.add(split_ident_hash(model.ident_hash))\n\n to_be_republished = []\n # What binders are these documents a part of?\n for (uuid, version) in documents:\n ident_hash = join_ident_hash(uuid, version)\n previous_ident_hash = get_previous_publication(cursor, ident_hash)\n if previous_ident_hash is None:\n # Has no prior existence.\n continue\n else:\n history_mapping[previous_ident_hash] = ident_hash\n cursor.execute(\"\"\"\\\nWITH RECURSIVE t(nodeid, parent_id, documentid, path) AS (\n SELECT tr.nodeid, tr.parent_id, tr.documentid, ARRAY[tr.nodeid]\n FROM trees tr\n WHERE tr.documentid = (\n SELECT module_ident FROM modules\n WHERE ident_hash(uuid, major_version, minor_version) = %s)\nUNION ALL\n SELECT c.nodeid, c.parent_id, c.documentid, path || ARRAY[c.nodeid]\n FROM trees c JOIN t ON (c.nodeid = t.parent_id)\n WHERE not c.nodeid = ANY(t.path)\n)\nSELECT ident_hash(uuid, major_version, minor_version)\nFROM t JOIN latest_modules m ON (t.documentid = m.module_ident)\nWHERE t.parent_id IS NULL\n\"\"\",\n (previous_ident_hash,))\n to_be_republished.extend([split_ident_hash(x[0])\n for x in cursor.fetchall()])\n to_be_republished = set(to_be_republished)\n\n republished_ident_hashes = []\n # Republish the Collections set.\n for (uuid, version) in to_be_republished:\n if uuid in binders:\n # This binder is already in the publication context,\n # don't try to publish it again.\n continue\n ident_hash = join_ident_hash(uuid, version)\n bumped_version = bump_version(cursor, uuid, is_minor_bump=True)\n republished_ident_hash = republish_collection(cursor, ident_hash,\n version=bumped_version)\n # Set the identifier history.\n history_mapping[ident_hash] = republished_ident_hash\n rebuild_collection_tree(cursor, ident_hash, history_mapping)\n republished_ident_hashes.append(republished_ident_hash)\n\n return republished_ident_hashes"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_previous_publication(cursor, ident_hash):\n cursor.execute(\"\"\"\\\nWITH contextual_module AS (\n SELECT uuid, module_ident\n FROM modules\n WHERE ident_hash(uuid, major_version, minor_version) = %s)\nSELECT ident_hash(m.uuid, m.major_version, m.minor_version)\nFROM modules AS m JOIN contextual_module AS context ON (m.uuid = context.uuid)\nWHERE\n m.module_ident < context.module_ident\nORDER BY revised DESC\nLIMIT 1\"\"\", (ident_hash,))\n try:\n previous_ident_hash = cursor.fetchone()[0]\n except TypeError: # NoneType\n previous_ident_hash = None\n return previous_ident_hash", "response": "Get the previous publication of the given ident - hash."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nbump to the next available version of the given content identified by uuid.", "response": "def bump_version(cursor, uuid, is_minor_bump=False):\n \"\"\"Bump to the next version of the given content identified\n by ``uuid``. Returns the next available version as a version tuple,\n containing major and minor version.\n If ``is_minor_bump`` is ``True`` the version will minor bump. That is\n 1.2 becomes 1.3 in the case of Collections. And 2 becomes 3 for\n Modules regardless of this option.\n \"\"\"\n cursor.execute(\"\"\"\\\nSELECT portal_type, major_version, minor_version\nFROM latest_modules\nWHERE uuid = %s::uuid\"\"\", (uuid,))\n type_, major_version, minor_version = cursor.fetchone()\n incr = 1\n if type_ == 'Collection' and is_minor_bump:\n minor_version = minor_version + incr\n else:\n major_version = major_version + incr\n return (major_version, minor_version,)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef republish_collection(cursor, ident_hash, version):\n if not isinstance(version, (list, tuple,)):\n split_version = version.split('.')\n if len(split_version) == 1:\n split_version.append(None)\n version = tuple(split_version)\n major_version, minor_version = version\n\n cursor.execute(\"\"\"\\\nWITH previous AS (\n SELECT module_ident\n FROM modules\n WHERE ident_hash(uuid, major_version, minor_version) = %s),\ninserted AS (\n INSERT INTO modules\n (uuid, major_version, minor_version, revised,\n portal_type, moduleid,\n name, created, language,\n submitter, submitlog,\n abstractid, licenseid, parent, parentauthors,\n authors, maintainers, licensors,\n google_analytics, buylink,\n stateid, doctype)\n SELECT\n uuid, %s, %s, CURRENT_TIMESTAMP,\n portal_type, moduleid,\n name, created, language,\n submitter, submitlog,\n abstractid, licenseid, parent, parentauthors,\n authors, maintainers, licensors,\n google_analytics, buylink,\n stateid, doctype\n FROM modules AS m JOIN previous AS p ON (m.module_ident = p.module_ident)\n RETURNING\n ident_hash(uuid, major_version, minor_version) AS ident_hash,\n module_ident),\nkeywords AS (\n INSERT INTO modulekeywords (module_ident, keywordid)\n SELECT i.module_ident, keywordid\n FROM modulekeywords AS mk, inserted AS i, previous AS p\n WHERE mk.module_ident = p.module_ident),\ntags AS (\n INSERT INTO moduletags (module_ident, tagid)\n SELECT i.module_ident, tagid\n FROM moduletags AS mt, inserted AS i, previous AS p\n WHERE mt.module_ident = p.module_ident)\nSELECT ident_hash FROM inserted\"\"\",\n (ident_hash, major_version, minor_version,))\n repub_ident_hash = cursor.fetchone()[0]\n return repub_ident_hash", "response": "Republish the collection identified as ident_hash with the given version."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef rebuild_collection_tree(cursor, ident_hash, history_map):\n collection_tree_sql = \"\"\"\\\nWITH RECURSIVE t(nodeid, parent_id, documentid, title, childorder, latest,\n ident_hash, path) AS (\n SELECT\n tr.nodeid, tr.parent_id, tr.documentid,\n tr.title, tr.childorder, tr.latest,\n (SELECT ident_hash(uuid, major_version, minor_version)\n FROM modules\n WHERE module_ident = tr.documentid) AS ident_hash,\n ARRAY[tr.nodeid]\n FROM trees AS tr\n WHERE tr.documentid = (\n SELECT module_ident\n FROM modules\n WHERE ident_hash(uuid, major_version, minor_version) = %s)\n AND tr.is_collated = FALSE\nUNION ALL\n SELECT\n c.nodeid, c.parent_id, c.documentid, c.title, c.childorder, c.latest,\n (SELECT ident_hash(uuid, major_version, minor_version)\n FROM modules\n WHERE module_ident = c.documentid) AS ident_hash,\n path || ARRAY[c.nodeid]\n FROM trees AS c JOIN t ON (c.parent_id = t.nodeid)\n WHERE not c.nodeid = ANY(t.path) AND c.is_collated = FALSE\n)\nSELECT row_to_json(row) FROM (SELECT * FROM t) AS row\"\"\"\n\n tree_insert_sql = \"\"\"\\\nINSERT INTO trees\n (nodeid, parent_id,\n documentid,\n title, childorder, latest)\nVALUES\n (DEFAULT, %(parent_id)s,\n (SELECT module_ident\n FROM modules\n WHERE ident_hash(uuid, major_version, minor_version) = \\\n %(ident_hash)s),\n %(title)s, %(childorder)s, %(latest)s)\nRETURNING nodeid\"\"\"\n\n def get_tree():\n cursor.execute(collection_tree_sql, (ident_hash,))\n for row in cursor.fetchall():\n yield row[0]\n\n def insert(fields):\n cursor.execute(tree_insert_sql, fields)\n results = cursor.fetchone()[0]\n return results\n\n tree = {} # {: {...}, ...}\n children = {} # {: [, ...], : [...]}\n for node in get_tree():\n tree[node['nodeid']] = node\n children.setdefault(node['parent_id'], [])\n children[node['parent_id']].append(node['nodeid'])\n\n def build_tree(nodeid, parent_id):\n data = tree[nodeid]\n data['parent_id'] = parent_id\n if history_map.get(data['ident_hash']) is not None \\\n and (data['latest'] or parent_id is None):\n data['ident_hash'] = history_map[data['ident_hash']]\n new_nodeid = insert(data)\n for child_nodeid in children.get(nodeid, []):\n build_tree(child_nodeid, new_nodeid)\n\n root_node = children[None][0]\n build_tree(root_node, None)", "response": "Rebuild the tree based on the old tree and the new tree based on the new tree"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npublishes a new resource.", "response": "def publish(request):\n \"\"\"Accept a publication request at form value 'epub'\"\"\"\n if 'epub' not in request.POST:\n raise httpexceptions.HTTPBadRequest(\"Missing EPUB in POST body.\")\n\n is_pre_publication = asbool(request.POST.get('pre-publication'))\n epub_upload = request.POST['epub'].file\n try:\n epub = cnxepub.EPUB.from_file(epub_upload)\n except: # noqa: E722\n raise httpexceptions.HTTPBadRequest('Format not recognized.')\n\n # Make a publication entry in the database for status checking\n # the publication. This also creates publication entries for all\n # of the content in the EPUB.\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n epub_upload.seek(0)\n publication_id, publications = add_publication(\n cursor, epub, epub_upload, is_pre_publication)\n\n # Poke at the publication & lookup its state.\n state, messages = poke_publication_state(publication_id)\n\n response_data = {\n 'publication': publication_id,\n 'mapping': publications,\n 'state': state,\n 'messages': messages,\n }\n return response_data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_accept_license(request):\n publication_id = request.matchdict['id']\n user_id = request.matchdict['uid']\n\n # FIXME Is this an active publication?\n # TODO Verify the accepting user is the one making the request.\n\n # For each pending document, accept the license.\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\nSELECT row_to_json(combined_rows) FROM (\nSELECT\n pd.uuid AS id,\n ident_hash(pd.uuid, pd.major_version, pd.minor_version) \\\n AS ident_hash,\n accepted AS is_accepted\nFROM\n pending_documents AS pd\n NATURAL JOIN license_acceptances AS la\nWHERE pd.publication_id = %s AND user_id = %s\n) as combined_rows;\"\"\",\n (publication_id, user_id))\n user_documents = [r[0] for r in cursor.fetchall()]\n\n return {'publication_id': publication_id,\n 'user_id': user_id,\n 'documents': user_documents,\n }", "response": "This returns the JSON data for a user to view the license(s) that have been accepted for a publication."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nallowing the user to accept the license for a specific publication.", "response": "def post_accept_license(request):\n \"\"\"Allows the user (at ``uid``) to accept the license(s) for\n a publication (at ``id``).\n \"\"\"\n publication_id = request.matchdict['id']\n uid = request.matchdict['uid']\n\n # TODO Verify the accepting user is the one making the request.\n # They could be authenticated but not be the license acceptor.\n\n post_data = request.json\n accepted = []\n denied = []\n try:\n documents = post_data['documents']\n for doc_acceptance in documents:\n if doc_acceptance['is_accepted'] is None:\n continue\n elif doc_acceptance['is_accepted']:\n accepted.append(doc_acceptance['id'])\n else:\n denied.append(doc_acceptance['id'])\n except KeyError:\n raise httpexceptions.BadRequest(\"Posted data is invalid.\")\n\n # For each pending document, accept/deny the license.\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n accept_publication_license(cursor, publication_id, uid,\n accepted, True)\n accept_publication_license(cursor, publication_id, uid,\n denied, False)\n\n location = request.route_url('publication-license-acceptance',\n id=publication_id, uid=uid)\n # Poke publication to change state.\n poke_publication_state(publication_id)\n return httpexceptions.HTTPFound(location=location)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ninvoking the baking process - trigger post - publication", "response": "def bake_content(request):\n \"\"\"Invoke the baking process - trigger post-publication\"\"\"\n ident_hash = request.matchdict['ident_hash']\n try:\n id, version = split_ident_hash(ident_hash)\n except IdentHashError:\n raise httpexceptions.HTTPNotFound()\n\n if not version:\n raise httpexceptions.HTTPBadRequest('must specify the version')\n\n with db_connect() as db_conn:\n with db_conn.cursor() as cursor:\n cursor.execute(\"\"\"\\\nSELECT bool(portal_type = 'Collection'), stateid, module_ident\nFROM modules\nWHERE ident_hash(uuid, major_version, minor_version) = %s\n\"\"\", (ident_hash,))\n try:\n is_binder, stateid, module_ident = cursor.fetchone()\n except TypeError:\n raise httpexceptions.HTTPNotFound()\n if not is_binder:\n raise httpexceptions.HTTPBadRequest(\n '{} is not a book'.format(ident_hash))\n\n if stateid == 5:\n cursor.execute(\"\"\"\\\nSELECT pg_notify('post_publication',\n'{\"module_ident\": '||%s||',\n \"ident_hash\": \"'||%s||'\",\n \"timestamp\": \"'||CURRENT_TIMESTAMP||'\"}')\n\"\"\", (module_ident, ident_hash))\n else:\n cursor.execute(\"\"\"\\\nUPDATE modules SET stateid = 5\nWHERE ident_hash(uuid, major_version, minor_version) = %s\n\"\"\", (ident_hash,))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef includeme(config):\n global cache_manager\n settings = config.registry.settings\n cache_manager = CacheManager(**parse_cache_config_options(settings))", "response": "Configures the caching manager"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a Postgres Notification Event object from a Notify object.", "response": "def create_pg_notify_event(notif):\n \"\"\"A factory for creating a Postgres Notification Event\n (an object inheriting from `cnxpublishing.events.PGNotifyEvent`)\n given `notif`, a `psycopg2.extensions.Notify` object.\n\n \"\"\"\n # TODO Lookup registered events via getAllUtilitiesRegisteredFor\n # for class mapping.\n if notif.channel not in _CHANNEL_MAPPER:\n cls = _CHANNEL_MAPPER[None]\n else:\n cls = _CHANNEL_MAPPER[notif.channel]\n return cls(notif)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef map(self, from_obj, to_type, ignore_case=False, allow_none=False, excluded=None):\r\n if (from_obj is None) and allow_none:\r\n return None\r\n else:\r\n # one of the tests is explicitly checking for an attribute error on __dict__ if it's not set\r\n from_obj.__dict__\r\n\r\n inst = to_type()\r\n key_from = from_obj.__class__.__name__\r\n key_to = to_type.__name__\r\n\r\n def not_private(s):\r\n return not s.startswith('_')\r\n\r\n def not_excluded(s):\r\n return not (excluded and s in excluded)\r\n\r\n from_obj_attributes = getmembers(from_obj, lambda a: not isroutine(a))\r\n from_obj_dict = {k: v for k, v in from_obj_attributes\r\n if not_private(k) and not_excluded(k)}\r\n\r\n to_obj_attributes = getmembers(inst, lambda a: not isroutine(a))\r\n to_obj_dict = {k: v for k, v in to_obj_attributes if not_private(k)}\r\n\r\n if ignore_case:\r\n from_props = CaseDict(from_obj_dict)\r\n to_props = CaseDict(to_obj_dict)\r\n else:\r\n from_props = from_obj_dict\r\n to_props = to_obj_dict\r\n\r\n for prop in to_props:\r\n if self.mappings is not None \\\r\n and key_from in self.mappings \\\r\n and key_to in self.mappings[key_from]:\r\n if prop in self.mappings[key_from][key_to]:\r\n # take mapping function\r\n try:\r\n fnc = self.mappings[key_from][key_to][prop]\r\n if fnc is not None:\r\n setattr(inst, prop, fnc(from_obj))\r\n # none suppress mapping\r\n except Exception:\r\n raise ObjectMapperException(\"Invalid mapping function while setting property {0}.{1}\".\r\n format(inst.__class__.__name__, prop))\r\n\r\n else:\r\n # try find property with the same name in the source\r\n if prop in from_props:\r\n setattr(inst, prop, from_props[prop])\r\n # case when target attribute is not mapped (can be extended)\r\n else:\r\n raise ObjectMapperException(\"No mapping defined for {0} -> {1}\".format(key_from, key_to))\r\n\r\n return inst", "response": "Method for creating a new object from one object to another."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get(self, key, default=_sentinel):\n tup = self._data.get(key.lower())\n if tup is not None:\n return tup[1]\n elif default is not _sentinel:\n return default\n else:\n return None", "response": "Gets the value of the key from the key."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pop(self, key, default=_sentinel):\n if default is not _sentinel:\n tup = self._data.pop(key.lower(), default)\n else:\n tup = self._data.pop(key.lower())\n if tup is not default:\n return tup[1]\n else:\n return default", "response": "Removes the specified key and returns the corresponding value. If default is not given KeyError is raised."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reversals(series, left=False, right=False):\n series = iter(series)\n\n x_last, x = next(series), next(series)\n d_last = (x - x_last)\n\n if left:\n yield x_last\n for x_next in series:\n if x_next == x:\n continue\n d_next = x_next - x\n if d_last * d_next < 0:\n yield x\n x_last, x = x, x_next\n d_last = d_next\n if right:\n yield x_next", "response": "Iterate over the series of reversal points."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_cycles(series, left=False, right=False):\n points = deque()\n\n for x in reversals(series, left=left, right=right):\n points.append(x)\n while len(points) >= 3:\n # Form ranges X and Y from the three most recent points\n X = abs(points[-2] - points[-1])\n Y = abs(points[-3] - points[-2])\n\n if X < Y:\n # Read the next point\n break\n elif len(points) == 3:\n # Y contains the starting point\n # Count Y as one-half cycle and discard the first point\n yield points[0], points[1], 0.5\n points.popleft()\n else:\n # Count Y as one cycle and discard the peak and the valley of Y\n yield points[-3], points[-2], 1.0\n last = points.pop()\n points.pop()\n points.pop()\n points.append(last)\n else:\n # Count the remaining ranges as one-half cycles\n while len(points) > 1:\n yield points[0], points[1], 0.5\n points.popleft()", "response": "Iterate cycles in the series."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncounts the number of cycles in a series.", "response": "def count_cycles(series, ndigits=None, left=False, right=False):\n \"\"\"Count cycles in the series.\n\n Parameters\n ----------\n series : iterable sequence of numbers\n ndigits : int, optional\n Round cycle magnitudes to the given number of digits before counting.\n left: bool, optional\n If True, treat the first point in the series as a reversal.\n right: bool, optional\n If True, treat the last point in the series as a reversal.\n\n Returns\n -------\n A sorted list containing pairs of cycle magnitude and count.\n One-half cycles are counted as 0.5, so the returned counts may not be\n whole numbers.\n \"\"\"\n counts = defaultdict(float)\n round_ = _get_round_function(ndigits)\n\n for low, high, mult in extract_cycles(series, left=left, right=right):\n delta = round_(abs(high - low))\n counts[delta] += mult\n return sorted(counts.items())"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a FST that renders a given node.", "response": "def render(node, strict=False):\n \"\"\"Recipe to render a given FST node.\n\n The FST is composed of branch nodes which are either lists or dicts\n and of leaf nodes which are strings. Branch nodes can have other\n list, dict or leaf nodes as childs.\n\n To render a string, simply output it. To render a list, render each\n of its elements in order. To render a dict, you must follow the\n node's entry in the nodes_rendering_order dictionary and its\n dependents constraints.\n\n This function hides all this algorithmic complexity by returning\n a structured rendering recipe, whatever the type of node. But even\n better, you should subclass the RenderWalker which simplifies\n drastically working with the rendered FST.\n\n The recipe is a list of steps, each step correspond to a child and is actually a 3-uple composed of the following fields:\n\n - `key_type` is a string determining the type of the child in the second field (`item`) of the tuple. It can be one of:\n\n - 'constant': the child is a string\n - 'node': the child is a dict\n - 'key': the child is an element of a dict\n - 'list': the child is a list\n - 'formatting': the child is a list specialized in formatting\n\n - `item` is the child itself: either a string, a dict or a list.\n - `render_key` gives the key used to access this child from the parent node. It's a string if the node is a dict or a number if its a list.\n\n Please note that \"bool\" `key_types` are never rendered, that's why\n they are not shown here.\n \"\"\"\n if isinstance(node, list):\n return render_list(node)\n\n elif isinstance(node, dict):\n return render_node(node, strict=strict)\n\n else:\n raise NotImplementedError(\"You tried to render a %s. Only list and dicts can be rendered.\" % node.__class__.__name__)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the FST node located at the given path", "response": "def path_to_node(tree, path):\n \"\"\"FST node located at the given path\"\"\"\n if path is None:\n return None\n\n node = tree\n\n for key in path:\n node = child_by_key(node, key)\n\n return node"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef before_constant(self, constant, key):\n newlines_split = split_on_newlines(constant)\n\n for c in newlines_split:\n if is_newline(c):\n self.current.advance_line()\n # if target line is passed\n if self.current.line > self.target.line:\n return self.STOP\n\n else:\n advance_by = len(c)\n if self.is_on_targetted_node(advance_by):\n self.found_path = deepcopy(self.current_path)\n return self.STOP\n self.current.advance_columns(advance_by)", "response": "Determine if we re on the targetted node."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the prefix for a given multicodec name.", "response": "def get_prefix(multicodec):\n \"\"\"\n Returns prefix for a given multicodec\n\n :param str multicodec: multicodec codec name\n :return: the prefix for the given multicodec\n :rtype: byte\n :raises ValueError: if an invalid multicodec name is provided\n \"\"\"\n try:\n prefix = varint.encode(NAME_TABLE[multicodec])\n except KeyError:\n raise ValueError('{} multicodec is not supported.'.format(multicodec))\n return prefix"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_prefix(multicodec, bytes_):\n prefix = get_prefix(multicodec)\n return b''.join([prefix, bytes_])", "response": "Adds multicodec prefix to the given bytes input\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove prefix from a prefixed data bytes", "response": "def remove_prefix(bytes_):\n \"\"\"\n Removes prefix from a prefixed data\n\n :param bytes bytes_: multicodec prefixed data bytes\n :return: prefix removed data bytes\n :rtype: bytes\n \"\"\"\n prefix_int = extract_prefix(bytes_)\n prefix = varint.encode(prefix_int)\n return bytes_[len(prefix):]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_codec(bytes_):\n prefix = extract_prefix(bytes_)\n try:\n return CODE_TABLE[prefix]\n except KeyError:\n raise ValueError('Prefix {} not present in the lookup table'.format(prefix))", "response": "Gets the codec used for prefix the multicodec prefixed data"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef capture(\n target_url,\n user_agent=\"archiveis (https://github.com/pastpages/archiveis)\",\n proxies={}\n):\n \"\"\"\n Archives the provided URL using archive.is\n\n Returns the URL where the capture is stored.\n \"\"\"\n # Put together the URL that will save our request\n domain = \"http://archive.vn\"\n save_url = urljoin(domain, \"/submit/\")\n\n # Configure the request headers\n headers = {\n 'User-Agent': user_agent,\n \"host\": \"archive.vn\",\n }\n\n # Request a unique identifier for our activity\n logger.debug(\"Requesting {}\".format(domain + \"/\"))\n get_kwargs = dict(\n timeout=120,\n allow_redirects=True,\n headers=headers,\n )\n if proxies:\n get_kwargs['proxies'] = proxies\n response = requests.get(domain + \"/\", **get_kwargs)\n response.raise_for_status()\n\n # It will need to be parsed from the homepage response headers\n html = str(response.content)\n try:\n unique_id = html.split('name=\"submitid', 1)[1].split('value=\"', 1)[1].split('\"', 1)[0]\n logger.debug(\"Unique identifier: {}\".format(unique_id))\n except IndexError:\n logger.warn(\"Unable to extract unique identifier from archive.is. Submitting without it.\")\n unique_id = None\n\n # Send the capture request to archive.is with the unique id included\n data = {\n \"url\": target_url,\n \"anyway\": 1,\n }\n if unique_id:\n data.update({\"submitid\": unique_id})\n\n post_kwargs = dict(\n timeout=120,\n allow_redirects=True,\n headers=headers,\n data=data\n )\n if proxies:\n post_kwargs['proxies'] = proxies\n\n logger.debug(\"Requesting {}\".format(save_url))\n response = requests.post(save_url, **post_kwargs)\n response.raise_for_status()\n\n # There are a couple ways the header can come back\n if 'Refresh' in response.headers:\n memento = str(response.headers['Refresh']).split(';url=')[1]\n logger.debug(\"Memento from Refresh header: {}\".format(memento))\n return memento\n if 'Location' in response.headers:\n memento = response.headers['Location']\n logger.debug(\"Memento from Location header: {}\".format(memento))\n return memento\n logger.debug(\"Memento not found in response headers. Inspecting history.\")\n for i, r in enumerate(response.history):\n logger.debug(\"Inspecting history request #{}\".format(i))\n logger.debug(r.headers)\n if 'Location' in r.headers:\n memento = r.headers['Location']\n logger.debug(\"Memento from the Location header of {} history response: {}\".format(i+1, memento))\n return memento\n # If there's nothing at this point, throw an error\n logger.error(\"No memento returned by archive.is\")\n logger.error(\"Status code: {}\".format(response.status_code))\n logger.error(response.headers)\n logger.error(response.text)\n raise Exception(\"No memento returned by archive.is\")", "response": "Create a new object of the correct type for the given target_url."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\narchiving the provided URL using archive.is.", "response": "def cli(url, user_agent):\n \"\"\"\n Archives the provided URL using archive.is.\n \"\"\"\n kwargs = {}\n if user_agent:\n kwargs['user_agent'] = user_agent\n archive_url = capture(url, **kwargs)\n click.echo(archive_url)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the logo for a channel.", "response": "def get_channel_image(self, channel, img_size=300, skip_cache=False):\n \"\"\"Get the logo for a channel\"\"\"\n from bs4 import BeautifulSoup\n from wikipedia.exceptions import PageError\n import re\n import wikipedia\n wikipedia.set_lang('fr')\n\n if not channel:\n _LOGGER.error('Channel is not set. Could not retrieve image.')\n return\n\n # Check if the image is in cache\n if channel in self._cache_channel_img and not skip_cache:\n img = self._cache_channel_img[channel]\n _LOGGER.debug('Cache hit: %s -> %s', channel, img)\n return img\n\n channel_info = self.get_channel_info(channel)\n query = channel_info['wiki_page']\n if not query:\n _LOGGER.debug('Wiki page is not set for channel %s', channel)\n return\n _LOGGER.debug('Query: %s', query)\n # If there is a max image size defined use it.\n if 'max_img_size' in channel_info:\n if img_size > channel_info['max_img_size']:\n _LOGGER.info(\n 'Requested image size is bigger than the max, '\n 'setting it to %s', channel_info['max_img_size']\n )\n img_size = channel_info['max_img_size']\n try:\n page = wikipedia.page(query)\n _LOGGER.debug('Wikipedia article title: %s', page.title)\n soup = BeautifulSoup(page.html(), 'html.parser')\n images = soup.find_all('img')\n img_src = None\n for i in images:\n if i['alt'].startswith('Image illustrative'):\n img_src = re.sub(r'\\d+px', '{}px'.format(img_size),\n i['src'])\n img = 'https:{}'.format(img_src) if img_src else None\n # Cache result\n self._cache_channel_img[channel] = img\n return img\n except PageError:\n _LOGGER.error('Could not fetch channel image for %s', channel)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npress a key from the cache.", "response": "def press_key(self, key, mode=0):\n '''\n modes:\n 0 -> simple press\n 1 -> long press\n 2 -> release after long press\n '''\n if isinstance(key, str):\n assert key in KEYS, 'No such key: {}'.format(key)\n key = KEYS[key]\n _LOGGER.info('Press key %s', self.__get_key_name(key))\n return self.rq('01', OrderedDict([('key', key), ('mode', mode)]))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef forwards(self, orm):\n \"Write your forwards methods here.\"\n # Note: Remember to use orm['appname.ModelName'] rather than \"from appname.models...\"\n for translation in orm['people.PersonTranslation'].objects.all():\n if translation.language in ['en', 'de']:\n translation.roman_first_name = translation.first_name\n translation.roman_last_name = translation.last_name\n else:\n translation.non_roman_first_name = translation.first_name\n translation.non_roman_last_name = translation.last_name\n translation.save()", "response": "Write your forwards methods here."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite your forwards methods here.", "response": "def forwards(self, orm):\n \"Write your forwards methods here.\"\n for translation in orm['people.PersonTranslation'].objects.all():\n translation.person.roman_first_name = translation.roman_first_name\n translation.person.roman_last_name = translation.roman_last_name\n translation.person.non_roman_first_name = translation.non_roman_first_name\n translation.person.non_roman_last_name = translation.non_roman_last_name\n translation.person.save()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse a block node.", "response": "def parse(self, scope):\n \"\"\"Parse block node.\n args:\n scope (Scope): Current scope\n raises:\n SyntaxError\n returns:\n self\n \"\"\"\n if not self.parsed:\n scope.push()\n self.name, inner = self.tokens\n scope.current = self.name\n scope.real.append(self.name)\n if not self.name.parsed:\n self.name.parse(scope)\n if not inner:\n inner = []\n inner = list(utility.flatten([p.parse(scope) for p in inner if p]))\n self.parsed = []\n self.inner = []\n if not hasattr(self, \"inner_media_queries\"):\n self.inner_media_queries = []\n for p in inner:\n if p is not None:\n if isinstance(p, Block):\n if (len(scope) == 2 and p.tokens[1] is not None):\n p_is_mediaquery = p.name.tokens[0] == '@media'\n # Inner block @media ... { ... } is a nested media\n # query. But double-nested media queries have to be\n # removed and marked as well. While parsing \".foo\",\n # both nested \"@media print\" and double-nested\n # \"@media all\" will be handled as we have to\n # re-arrange the scope and block layout quite a bit:\n #\n # .foo {\n # @media print {\n # color: blue;\n # @media screen { font-size: 12em; }\n # }\n # }\n #\n # Expected result:\n #\n # @media print {\n # .foo { color: blue; }\n # }\n # @media print and screen {\n # .foo { font-size: 12 em; }\n # }\n append_list = []\n reparse_p = False\n for child in p.tokens[1]:\n if isinstance(child, Block) and child.name.raw(\n ).startswith(\"@media\"):\n # Remove child from the nested media query, it will be re-added to\n # the parent with 'merged' media query (see above example).\n p.tokens[1].remove(child)\n if p_is_mediaquery: # Media query inside a & block\n # Double-nested media query found. We remove it from 'p' and add\n # it to this block with a new 'name'.\n reparse_p = True\n part_a = p.name.tokens[2:][0][0][0]\n part_b = child.name.tokens[2:][0][0]\n new_ident_tokens = [\n '@media', ' ', [\n part_a, (' ', 'and', ' '),\n part_b\n ]\n ]\n # Parse child again with new @media $BLA {} part\n child.tokens[0] = Identifier(\n new_ident_tokens)\n child.parsed = None\n child = child.parse(scope)\n else:\n child.block_name = p.name\n append_list.append(child)\n if reparse_p:\n p.parsed = None\n p = p.parse(scope)\n if not p_is_mediaquery and not append_list:\n self.inner.append(p)\n else:\n append_list.insert(\n 0, p\n ) # This media query should occur before it's children\n for media_query in append_list:\n self.inner_media_queries.append(\n media_query)\n # NOTE(saschpe): The code is not recursive but we hope that people\n # wont use triple-nested media queries.\n else:\n self.inner.append(p)\n else:\n self.parsed.append(p)\n if self.inner_media_queries:\n # Nested media queries, we have to remove self from scope and\n # push all nested @media ... {} blocks.\n scope.remove_block(self, index=-2)\n for mb in self.inner_media_queries:\n # New inner block with current name and media block contents\n if hasattr(mb, 'block_name'):\n cb_name = mb.block_name\n else:\n cb_name = self.tokens[0]\n cb = Block([cb_name, mb.tokens[1]]).parse(scope)\n # Replace inner block contents with new block\n new_mb = Block([mb.tokens[0], [cb]]).parse(scope)\n self.inner.append(new_mb)\n scope.add_block(new_mb)\n scope.real.pop()\n scope.pop()\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef fmt(self, fills):\n f = \"%(identifier)s%(ws)s{%(nl)s%(proplist)s}%(eb)s\"\n out = []\n name = self.name.fmt(fills)\n if self.parsed and any(\n p for p in self.parsed\n if str(type(p)) != \"\"):\n fills.update({\n 'identifier':\n name,\n 'proplist':\n ''.join([p.fmt(fills) for p in self.parsed if p]),\n })\n out.append(f % fills)\n if hasattr(self, 'inner'):\n if self.name.subparse and len(self.inner) > 0: # @media\n inner = ''.join([p.fmt(fills) for p in self.inner])\n inner = inner.replace(fills['nl'],\n fills['nl'] + fills['tab']).rstrip(\n fills['tab'])\n if not fills['nl']:\n inner = inner.strip()\n fills.update({\n 'identifier': name,\n 'proplist': fills['tab'] + inner\n })\n out.append(f % fills)\n else:\n out.append(''.join([p.fmt(fills) for p in self.inner]))\n return ''.join(out)", "response": "Format a single block of CSS"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy(self):\n name, inner = self.tokens\n if inner:\n inner = [u.copy() if u else u for u in inner]\n if name:\n name = name.copy()\n return Block([name, inner], 0)", "response": "Return a full copy of self\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef copy_inner(self, scope):\n if self.tokens[1]:\n tokens = [u.copy() if u else u for u in self.tokens[1]]\n out = [p for p in tokens if p]\n utility.rename(out, scope, Block)\n return out\n return None", "response": "Copy the inner block contents."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse the node s tokens and return a new instance of the node class.", "response": "def parse(self, scope):\n \"\"\"Parse node\n args:\n scope (Scope): current scope\n raises:\n SyntaxError\n returns:\n self\n \"\"\"\n self.parsed = list(utility.flatten(self.tokens))\n if self.parsed[0] == '@import':\n if len(self.parsed) > 4:\n # Media @import\n self.parsed.insert(3, ' ')\n return self"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse function. We parse the tokens and return the mixed version of the tokens.", "response": "def parse(self, scope, error=False, depth=0):\n \"\"\" Parse function. We search for mixins\n first within current scope then fallback\n to global scope. The special scope.deferred\n is used when local scope mixins are called\n within parent mixins.\n If nothing is found we fallback to block-mixin\n as lessc.js allows calls to blocks and mixins to\n be inter-changable.\n clx: This method is a HACK that stems from\n poor design elsewhere. I will fix it\n when I have more time.\n args:\n scope (Scope): Current scope\n returns:\n mixed\n \"\"\"\n res = False\n ident, args = self.tokens\n ident.parse(scope)\n mixins = scope.mixins(ident.raw())\n\n if not mixins:\n ident.parse(None)\n mixins = scope.mixins(ident.raw())\n\n if depth > 64:\n raise SyntaxError('NameError `%s`' % ident.raw(True))\n\n if not mixins:\n if scope.deferred:\n store = [t for t in scope.deferred.parsed[-1]]\n i = 0\n while scope.deferred.parsed[-1]:\n scope.current = scope.deferred\n ident.parse(scope)\n mixins = scope.mixins(ident.raw())\n scope.current = None\n if mixins or i > 64:\n break\n scope.deferred.parsed[-1].pop()\n i += 1\n scope.deferred.parsed[-1] = store\n\n if not mixins:\n # Fallback to blocks\n block = scope.blocks(ident.raw())\n if not block:\n ident.parse(None)\n block = scope.blocks(ident.raw())\n if block:\n scope.current = scope.real[-1] if scope.real else None\n res = block.copy_inner(scope)\n scope.current = None\n\n if mixins:\n for mixin in mixins:\n scope.current = scope.real[-1] if scope.real else None\n res = mixin.call(scope, args)\n if res:\n # Add variables to scope to support\n # closures\n [scope.add_variable(v) for v in mixin.vars]\n scope.deferred = ident\n break\n\n if res:\n store = [t for t in scope.deferred.parsed[-1]\n ] if scope.deferred else False\n tmp_res = []\n for p in res:\n if p:\n if isinstance(p, Deferred):\n tmp_res.append(p.parse(scope, depth=depth + 1))\n else:\n tmp_res.append(p.parse(scope))\n res = tmp_res\n #res = [p.parse(scope, depth=depth+1) for p in res if p]\n while (any(t for t in res if isinstance(t, Deferred))):\n res = [p.parse(scope) for p in res if p]\n if store:\n scope.deferred.parsed[-1] = store\n\n if error and not res:\n raise SyntaxError('NameError `%s`' % ident.raw(True))\n return res"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncompiling all. less files in a directory and write them to outpath.", "response": "def ldirectory(inpath, outpath, args, scope):\n \"\"\"Compile all *.less files in directory\n Args:\n inpath (str): Path to compile\n outpath (str): Output directory\n args (object): Argparse Object\n scope (Scope): Scope object or None\n \"\"\"\n yacctab = 'yacctab' if args.debug else None\n if not outpath:\n sys.exit(\"Compile directory option needs -o ...\")\n else:\n if not os.path.isdir(outpath):\n if args.verbose:\n print(\"Creating '%s'\" % outpath, file=sys.stderr)\n if not args.dry_run:\n os.mkdir(outpath)\n less = glob.glob(os.path.join(inpath, '*.less'))\n f = formatter.Formatter(args)\n for lf in less:\n outf = os.path.splitext(os.path.basename(lf))\n minx = '.min' if args.min_ending else ''\n outf = \"%s/%s%s.css\" % (outpath, outf[0], minx)\n if not args.force and os.path.exists(outf):\n recompile = os.path.getmtime(outf) < os.path.getmtime(lf)\n else:\n recompile = True\n if recompile:\n print('%s -> %s' % (lf, outf))\n p = parser.LessParser(\n yacc_debug=(args.debug),\n lex_optimize=True,\n yacc_optimize=(not args.debug),\n scope=scope,\n tabfile=yacctab,\n verbose=args.verbose)\n p.parse(filename=lf, debuglevel=0)\n css = f.format(p)\n if not args.dry_run:\n with open(outf, 'w') as outfile:\n outfile.write(css)\n elif args.verbose:\n print('skipping %s, not modified' % lf, file=sys.stderr)\n sys.stdout.flush()\n if args.recurse:\n [\n ldirectory(\n os.path.join(inpath, name), os.path.join(outpath, name), args,\n scope) for name in os.listdir(inpath)\n if os.path.isdir(os.path.join(inpath, name))\n and not name.startswith('.') and not name == outpath\n ]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse(self, scope):\n self.keyframe, = [\n e[0] if isinstance(e, tuple) else e for e in self.tokens\n if str(e).strip()\n ]\n self.subparse = False\n return self", "response": "Parse the keyframe from the current scope."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef t_mediaquery_t_semicolon(self, t):\n r';'\n # This can happen only as part of a CSS import statement. The\n # \"mediaquery\" state is reused there. Ordinary media queries always\n # end at '{', i.e. when a block is opened.\n t.lexer.pop_state() # state mediaquery\n # We have to pop the 'import' state here because we already ate the\n # t_semicolon and won't trigger t_import_t_semicolon.\n t.lexer.pop_state() # state import\n return t", "response": "Parse the next token and return the next token after the semicolon."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef t_css_string(self, t):\n r'\"[^\"@]*\"|\\'[^\\'@]*\\''\n t.lexer.lineno += t.value.count('\\n')\n return t", "response": "Tokenizer for CSS string."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef t_istringapostrophe_css_string(self, t):\n r'[^\\'@]+'\n t.lexer.lineno += t.value.count('\\n')\n return t", "response": "Tokenizer for string apostrophe css"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef file(self, filename):\n with open(filename) as f:\n self.lexer.input(f.read())\n return self", "response": "Reads a file and parses it into the Lex file."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloads lexer with content from file which can be a path or a file object.", "response": "def input(self, file):\n \"\"\"\n Load lexer with content from `file` which can be a path or a file\n like object.\n \"\"\"\n if isinstance(file, string_types):\n with open(file) as f:\n self.lexer.input(f.read())\n else:\n self.lexer.input(file.read())"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef token(self):\n if self.next_:\n t = self.next_\n self.next_ = None\n return t\n while True:\n t = self.lexer.token()\n if not t:\n return t\n if t.type == 't_ws' and (\n self.pretok or\n (self.last and self.last.type not in self.significant_ws)):\n continue\n self.pretok = False\n if t.type == 't_bclose' and self.last and self.last.type not in ['t_bopen', 't_bclose'] and self.last.type != 't_semicolon' \\\n and not (hasattr(t, 'lexer') and (t.lexer.lexstate == 'escapequotes' or t.lexer.lexstate == 'escapeapostrophe')):\n self.next_ = t\n tok = lex.LexToken()\n tok.type = 't_semicolon'\n tok.value = ';'\n tok.lineno = t.lineno\n tok.lexpos = t.lexpos\n self.last = tok\n self.lexer.in_property_decl = False\n return tok\n self.last = t\n break\n return t", "response": "Returns the next token in the language."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse(self, scope):\n names = []\n name = []\n self._subp = ('@media', '@keyframes', '@-moz-keyframes',\n '@-webkit-keyframes', '@-ms-keyframes')\n if self.tokens and hasattr(self.tokens, 'parse'):\n self.tokens = list(\n utility.flatten([\n id.split() + [',']\n for id in self.tokens.parse(scope).split(',')\n ]))\n self.tokens.pop()\n if self.tokens and any(hasattr(t, 'parse') for t in self.tokens):\n tmp_tokens = []\n for t in self.tokens:\n if hasattr(t, 'parse'):\n tmp_tokens.append(t.parse(scope))\n else:\n tmp_tokens.append(t)\n self.tokens = list(utility.flatten(tmp_tokens))\n if self.tokens and self.tokens[0] in self._subp:\n name = list(utility.flatten(self.tokens))\n self.subparse = True\n else:\n self.subparse = False\n for n in utility.flatten(self.tokens):\n if n == '*':\n name.append('* ')\n elif n in '>+~':\n if name and name[-1] == ' ':\n name.pop()\n name.append('?%s?' % n)\n elif n == ',':\n names.append(name)\n name = []\n else:\n name.append(n)\n names.append(name)\n parsed = self.root(scope, names) if scope else names\n\n # Interpolated selectors need another step, we have to replace variables. Avoid reserved words though\n #\n # Example: '.@{var}' results in [['.', '@{var}']]\n # But: '@media print' results in [['@media', ' ', 'print']]\n #\n def replace_variables(tokens, scope):\n return [\n scope.swap(t)\n if (utility.is_variable(t) and not t in reserved.tokens) else t\n for t in tokens\n ]\n\n parsed = [\n list(utility.flatten(replace_variables(part, scope)))\n for part in parsed\n ]\n\n self.parsed = [[\n i for i, j in utility.pairwise(part)\n if i != ' ' or (j and '?' not in j)\n ] for part in parsed]\n return self", "response": "Parse node. Block identifiers are stored as\n strings with spaces replaced with ?\n args:\n scope (Scope): Current scope\n raises:\n SyntaxError\n returns:\n self"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef root(self, scope, names):\n parent = scope.scopename\n if parent:\n parent = parent[-1]\n if parent.parsed:\n parsed_names = []\n for name in names:\n ampersand_count = name.count('&')\n if ampersand_count:\n filtered_parts = []\n for part in parent.parsed:\n if part and part[0] not in self._subp:\n filtered_parts.append(part)\n permutations = list(\n utility.permutations_with_replacement(\n filtered_parts, ampersand_count))\n for permutation in permutations:\n parsed = []\n for name_part in name:\n if name_part == \"&\":\n parent_part = permutation.pop(0)\n if parsed and parsed[-1].endswith(']'):\n parsed.extend(' ')\n if parent_part[-1] == ' ':\n parent_part.pop()\n parsed.extend(parent_part)\n else:\n parsed.append(name_part)\n parsed_names.append(parsed)\n else:\n # NOTE(saschpe): Maybe this code can be expressed with permutations too?\n for part in parent.parsed:\n if part and part[0] not in self._subp:\n parsed = []\n if name[0] == \"@media\":\n parsed.extend(name)\n else:\n parsed.extend(part)\n if part[-1] != ' ':\n parsed.append(' ')\n parsed.extend(name)\n parsed_names.append(parsed)\n else:\n parsed_names.append(name)\n return parsed_names\n return names", "response": "Find the root of identifier from a list of names."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef copy(self):\n tokens = ([t for t in self.tokens]\n if isinstance(self.tokens, list) else self.tokens)\n return Identifier(tokens, 0)", "response": "Return a copy of self\n Identifier"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nformats identifier args: fills (dict): replacements returns: str (CSS)", "response": "def fmt(self, fills):\n \"\"\"Format identifier\n args:\n fills (dict): replacements\n returns:\n str (CSS)\n \"\"\"\n name = ',$$'.join(''.join(p).strip() for p in self.parsed)\n name = re.sub('\\?(.)\\?', '%(ws)s\\\\1%(ws)s', name) % fills\n return name.replace('$$', fills['nl']).replace(' ', ' ')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a block element to the log", "response": "def add_block(self, block):\n \"\"\"Add block element to scope\n Args:\n block (Block): Block object\n \"\"\"\n self[-1]['__blocks__'].append(block)\n self[-1]['__names__'].append(block.raw())"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remove_block(self, block, index=\"-1\"):\n self[index][\"__blocks__\"].remove(block)\n self[index][\"__names__\"].remove(block.raw())", "response": "Removes a block element from the scope\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding a mixin to the scope of the log.", "response": "def add_mixin(self, mixin):\n \"\"\"Add mixin to scope\n Args:\n mixin (Mixin): Mixin object\n \"\"\"\n raw = mixin.tokens[0][0].raw()\n if raw in self._mixins:\n self._mixins[raw].append(mixin)\n else:\n self._mixins[raw] = [mixin]"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsearches for variable by name. Searches scope top down and returns Variable object OR False", "response": "def variables(self, name):\n \"\"\"Search for variable by name. Searches scope top down\n Args:\n name (string): Search term\n Returns:\n Variable object OR False\n \"\"\"\n if isinstance(name, tuple):\n name = name[0]\n if name.startswith('@{'):\n name = '@' + name[2:-1]\n i = len(self)\n while i >= 0:\n i -= 1\n if name in self[i]['__variables__']:\n return self[i]['__variables__'][name]\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsearches mixins for name.", "response": "def mixins(self, name):\n \"\"\" Search mixins for name.\n Allow '>' to be ignored. '.a .b()' == '.a > .b()'\n Args:\n name (string): Search term\n Returns:\n Mixin object list OR False\n \"\"\"\n m = self._smixins(name)\n if m:\n return m\n return self._smixins(name.replace('?>?', ' '))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsearch for defined blocks recursively.", "response": "def blocks(self, name):\n \"\"\"\n Search for defined blocks recursively.\n Allow '>' to be ignored. '.a .b' == '.a > .b'\n Args:\n name (string): Search term\n Returns:\n Block object OR False\n \"\"\"\n b = self._blocks(name)\n if b:\n return b\n return self._blocks(name.replace('?>?', ' '))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _blocks(self, name):\n i = len(self)\n while i >= 0:\n i -= 1\n if name in self[i]['__names__']:\n for b in self[i]['__blocks__']:\n r = b.raw()\n if r and r == name:\n return b\n else:\n for b in self[i]['__blocks__']:\n r = b.raw()\n if r and name.startswith(r):\n b = utility.blocksearch(b, name)\n if b:\n return b\n return False", "response": "Search for blocks by name."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating the internal state of the object.", "response": "def update(self, scope, at=0):\n \"\"\"Update scope. Add another scope to this one.\n Args:\n scope (Scope): Scope object\n Kwargs:\n at (int): Level to update\n \"\"\"\n if hasattr(scope, '_mixins') and not at:\n self._mixins.update(scope._mixins)\n self[at]['__variables__'].update(scope[at]['__variables__'])\n self[at]['__blocks__'].extend(scope[at]['__blocks__'])\n self[at]['__names__'].extend(scope[at]['__names__'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nswapping variable name for variable value", "response": "def swap(self, name):\n \"\"\" Swap variable name for variable value\n Args:\n name (str): Variable name\n Returns:\n Variable value (Mixed)\n \"\"\"\n if name.startswith('@@'):\n var = self.variables(name[1:])\n if var is False:\n raise SyntaxError('Unknown variable %s' % name)\n name = '@' + utility.destring(var.value[0])\n var = self.variables(name)\n if var is False:\n raise SyntaxError('Unknown variable %s' % name)\n elif name.startswith('@{'):\n var = self.variables('@' + name[2:-1])\n if var is False:\n raise SyntaxError('Unknown escaped variable %s' % name)\n if isinstance(var.value[0], string_types):\n var.value[0] = utility.destring(var.value[0])\n else:\n var = self.variables(name)\n if var is False:\n raise SyntaxError('Unknown variable %s' % name)\n return var.value"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprocessing tokenslist flattening and parsing it", "response": "def process(self, tokens, scope):\n \"\"\" Process tokenslist, flattening and parsing it\n args:\n tokens (list): tokenlist\n scope (Scope): Current scope\n returns:\n list\n \"\"\"\n while True:\n tokens = list(utility.flatten(tokens))\n done = True\n if any(t for t in tokens if hasattr(t, 'parse')):\n tokens = [\n t.parse(scope) if hasattr(t, 'parse') else t\n for t in tokens\n ]\n done = False\n if any(\n t for t in tokens\n if (utility.is_variable(t)) or str(type(t)) ==\n \"\"):\n tokens = self.replace_variables(tokens, scope)\n done = False\n if done:\n break\n return tokens"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreplace variables in tokenlist tokens with variables in scope.", "response": "def replace_variables(self, tokens, scope):\n \"\"\" Replace variables in tokenlist\n args:\n tokens (list): tokenlist\n scope (Scope): Current scope\n returns:\n list\n \"\"\"\n list = []\n for t in tokens:\n if utility.is_variable(t):\n list.append(scope.swap(t))\n elif str(type(t)) == \"\":\n list.append(scope.swap(t.name))\n else:\n list.append(t)\n return list"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse(self, scope):\n if not self.parsed:\n if len(self.tokens) > 2:\n property, style, _ = self.tokens\n self.important = True\n else:\n property, style = self.tokens\n self.important = False\n self.property = ''.join(property)\n self.parsed = []\n if style:\n style = self.preprocess(style)\n self.parsed = self.process(style, scope)\n return self", "response": "Parse node - level properties and style."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef preprocess(self, style):\n if self.property == 'font':\n style = [\n ''.join(u.expression()) if hasattr(u, 'expression') else u\n for u in style\n ]\n else:\n style = [(u, ' ') if hasattr(u, 'expression') else u\n for u in style]\n return style", "response": "Hackish preprocessing from font shorthand tags."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nformat node with fills", "response": "def fmt(self, fills):\n \"\"\" Format node\n args:\n fills (dict): replacements\n returns:\n str\n \"\"\"\n f = \"%(tab)s%(property)s:%(ws)s%(style)s%(important)s;%(nl)s\"\n imp = ' !important' if self.important else ''\n if fills['nl']:\n self.parsed = [\n ',%s' % fills['ws'] if p == ',' else p for p in self.parsed\n ]\n style = ''.join([\n p.fmt(fills) if hasattr(p, 'fmt') else str(p) for p in self.parsed\n ])\n # IE cannot handle no space after url()\n style = re.sub(\"(url\\([^\\)]*\\))([^\\s,])\", \"\\\\1 \\\\2\", style)\n fills.update({\n 'property': self.property,\n 'style': style.strip(),\n 'important': imp\n })\n return f % fills"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse node args and scope.", "response": "def parse(self, scope):\n \"\"\"Parse node\n args:\n scope (Scope): current scope\n raises:\n SyntaxError\n returns:\n self\n \"\"\"\n self.name, args, self.guards = self.tokens[0]\n self.args = [a for a in utility.flatten(args) if a]\n self.body = Block([None, self.tokens[1]], 0)\n self.vars = list(\n utility.flatten([\n list(v.values()) for v in [s['__variables__'] for s in scope]\n ]))\n return self"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse the arguments to mixin.", "response": "def parse_args(self, args, scope):\n \"\"\"Parse arguments to mixin. Add them to scope\n as variables. Sets upp special variable @arguments\n as well.\n args:\n args (list): arguments\n scope (Scope): current scope\n raises:\n SyntaxError\n \"\"\"\n arguments = list(zip(args,\n [' '] * len(args))) if args and args[0] else None\n zl = itertools.zip_longest if sys.version_info[\n 0] == 3 else itertools.izip_longest\n if self.args:\n parsed = [\n v if hasattr(v, 'parse') else v for v in copy.copy(self.args)\n ]\n args = args if isinstance(args, list) else [args]\n vars = [\n self._parse_arg(var, arg, scope)\n for arg, var in zl([a for a in args], parsed)\n ]\n for var in vars:\n if var:\n var.parse(scope)\n if not arguments:\n arguments = [v.value for v in vars if v]\n if not arguments:\n arguments = ''\n Variable(['@arguments', None, arguments]).parse(scope)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _parse_arg(self, var, arg, scope):\n if isinstance(var, Variable):\n # kwarg\n if arg:\n if utility.is_variable(arg[0]):\n tmp = scope.variables(arg[0])\n if not tmp:\n return None\n val = tmp.value\n else:\n val = arg\n var = Variable(var.tokens[:-1] + [val])\n else:\n # arg\n if utility.is_variable(var):\n if arg is None:\n raise SyntaxError('Missing argument to mixin')\n elif utility.is_variable(arg[0]):\n tmp = scope.variables(arg[0])\n if not tmp:\n return None\n val = tmp.value\n else:\n val = arg\n var = Variable([var, None, val])\n else:\n return None\n return var", "response": "Parse a single argument to mixin.\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse_guards(self, scope):\n if self.guards:\n cor = True if ',' in self.guards else False\n for g in self.guards:\n if isinstance(g, list):\n res = (g[0].parse(scope)\n if len(g) == 1 else Expression(g).parse(scope))\n if cor:\n if res:\n return True\n elif not res:\n return False\n return True", "response": "Parse guards on mixin."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef call(self, scope, args=[]):\n ret = False\n if args:\n args = [[\n a.parse(scope) if isinstance(a, Expression) else a for a in arg\n ] if arg else arg for arg in args]\n try:\n self.parse_args(args, scope)\n except SyntaxError:\n pass\n else:\n if self.parse_guards(scope):\n body = self.body.copy()\n ret = body.tokens[1]\n if ret:\n utility.rename(ret, scope, Block)\n return ret", "response": "Parses a copy of the mixins body\n in the current scope and returns it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse(self, scope):\n self.name, _, self.value = self.tokens\n if isinstance(self.name, tuple):\n if len(self.name) > 1:\n self.name, pad = self.name\n self.value.append(pad)\n else:\n self.name = self.name[0]\n scope.add_variable(self)\n return self", "response": "Parse function\n args : Scope object containing the related attributes. Returns self."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse(self, scope):\n name = ''.join(self.tokens[0])\n parsed = self.process(self.tokens[1:], scope)\n\n if name == '%(':\n name = 'sformat'\n elif name in ('~', 'e'):\n name = 'escape'\n color = Color.Color()\n args = [\n t for t in parsed\n if not isinstance(t, string_types) or t not in '(),'\n ]\n if hasattr(self, name):\n try:\n return getattr(self, name)(*args)\n except ValueError:\n pass\n\n if hasattr(color, name):\n try:\n result = getattr(color, name)(*args)\n try:\n return result + ' '\n except TypeError:\n return result\n except ValueError:\n pass\n return name + ''.join([p for p in parsed])", "response": "Parse a node within scope."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef sformat(self, string, *args):\n format = string\n items = []\n m = re.findall('(%[asdA])', format)\n if m and not args:\n raise SyntaxError('Not enough arguments...')\n i = 0\n for n in m:\n v = {\n '%A': urlquote,\n '%s': utility.destring,\n }.get(n, str)(args[i])\n items.append(v)\n i += 1\n format = format.replace('%A', '%s')\n format = format.replace('%d', '%s')\n return format % tuple(items)", "response": "String format.\n args:\n string (str): string to format\n args (list): format options\n returns:\n str"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nam number args: string (str): match returns: bool", "response": "def isnumber(self, string, *args):\n \"\"\"Is number\n args:\n string (str): match\n returns:\n bool\n \"\"\"\n try:\n n, u = utility.analyze_number(string)\n except SyntaxError:\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nis url args: string (str): match returns: bool", "response": "def isurl(self, string, *args):\n \"\"\"Is url\n args:\n string (str): match\n returns:\n bool\n \"\"\"\n arg = utility.destring(string)\n regex = re.compile(\n r'^(?:http|ftp)s?://' # http:// or https://\n r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+'\n r'(?:[A-Z]{2,6}\\.?|[A-Z0-9-]{2,}\\.?)|' # domain...\n # localhost...\n r'localhost|'\n r'\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})' # ...or ip\n # optional port\n r'(?::\\d+)?'\n r'(?:/?|[/?]\\S+)$',\n re.IGNORECASE)\n return regex.match(arg)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef isstring(self, string, *args):\n regex = re.compile(r'\\'[^\\']*\\'|\"[^\"]*\"')\n return regex.match(string)", "response": "Is string containing a valid tag"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef increment(self, value, *args):\n n, u = utility.analyze_number(value)\n return utility.with_unit(n + 1, u)", "response": "Increment function for a log entry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding integers to a set of integers.", "response": "def add(self, *args):\n \"\"\" Add integers\n args:\n args (list): target\n returns:\n str\n \"\"\"\n if (len(args) <= 1):\n return 0\n return sum([int(v) for v in args])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the number of the log entry in the specified unit.", "response": "def round(self, value, *args):\n \"\"\" Round number\n args:\n value (str): target\n returns:\n str\n \"\"\"\n n, u = utility.analyze_number(value)\n return utility.with_unit(\n int(utility.away_from_zero_round(float(n))), u)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ceil(self, value, *args):\n n, u = utility.analyze_number(value)\n return utility.with_unit(int(math.ceil(n)), u)", "response": "ceil - Calculates the ceiling of a number in a set of units"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the value in a percentage.", "response": "def percentage(self, value, *args):\n \"\"\" Return percentage value\n args:\n value (str): target\n returns:\n str\n \"\"\"\n n, u = utility.analyze_number(value)\n n = int(n * 100.0)\n u = '%'\n return utility.with_unit(n, u)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprocess color expression returns string", "response": "def process(self, expression):\n \"\"\" Process color expression\n args:\n expression (tuple): color expression\n returns:\n str\n \"\"\"\n a, o, b = expression\n c1 = self._hextorgb(a)\n c2 = self._hextorgb(b)\n r = ['#']\n for i in range(3):\n v = self.operate(c1[i], c2[i], o)\n if v > 0xff:\n v = 0xff\n if v < 0:\n v = 0\n r.append(\"%02x\" % int(v))\n return ''.join(r)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndoing a operation on colors", "response": "def operate(self, left, right, operation):\n \"\"\" Do operation on colors\n args:\n left (str): left side\n right (str): right side\n operation (str): Operation\n returns:\n str\n \"\"\"\n operation = {\n '+': operator.add,\n '-': operator.sub,\n '*': operator.mul,\n '/': operator.truediv\n }.get(operation)\n return operation(left, right)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntranslates rgb to color string", "response": "def rgb(self, *args):\n \"\"\" Translate rgb(...) to color string\n raises:\n ValueError\n returns:\n str\n \"\"\"\n if len(args) == 4:\n args = args[:3]\n if len(args) == 3:\n try:\n return self._rgbatohex(list(map(int, args)))\n except ValueError:\n if all((a for a in args\n if a[-1] == '%' and 100 >= int(a[:-1]) >= 0)):\n return self._rgbatohex(\n [int(a[:-1]) * 255 / 100.0 for a in args])\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef rgba(self, *args):\n if len(args) == 4:\n try:\n falpha = float(list(args)[3])\n if falpha > 1:\n args = args[:3]\n if falpha == 0:\n values = self._rgbatohex_raw(list(map(int, args)))\n return \"rgba(%s)\" % ','.join([str(a) for a in values])\n return self._rgbatohex(list(map(int, args)))\n except ValueError:\n if all((a for a in args\n if a[-1] == '%' and 100 >= int(a[:-1]) >= 0)):\n alpha = list(args)[3]\n if alpha[-1] == '%' and float(alpha[:-1]) == 0:\n values = self._rgbatohex_raw(\n [int(a[:-1]) * 255 / 100.0 for a in args])\n return \"rgba(%s)\" % ','.join([str(a) for a in values])\n return self._rgbatohex(\n [int(a[:-1]) * 255 / 100.0 for a in args])\n raise ValueError('Illegal color values')", "response": "Translate rgba to color string"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef argb(self, *args):\n if len(args) == 1 and type(args[0]) is str:\n match = re.match(r'rgba\\((.*)\\)', args[0])\n if match:\n # NOTE(saschpe): Evil hack to cope with rgba(.., .., .., 0.5) passed through untransformed\n rgb = re.sub(r'\\s+', '', match.group(1)).split(',')\n else:\n rgb = list(self._hextorgb(args[0]))\n else:\n rgb = list(args)\n if len(rgb) == 3:\n return self._rgbatohex([255] + list(map(int, rgb)))\n elif len(rgb) == 4:\n rgb = [rgb.pop()] + rgb # Move Alpha to front\n try:\n fval = float(list(rgb)[0])\n if fval > 1:\n rgb = [255] + rgb[1:] # Clip invalid integer/float values\n elif 1 >= fval >= 0:\n rgb = [\n fval * 256\n ] + rgb[1:] # Convert 0-1 to 0-255 range for _rgbatohex\n else:\n rgb = [0] + rgb[1:] # Clip lower bound\n return self._rgbatohex(list(map(int, rgb)))\n except ValueError:\n if all((a for a in rgb\n if a[-1] == '%' and 100 >= int(a[:-1]) >= 0)):\n return self._rgbatohex(\n [int(a[:-1]) * 255 / 100.0 for a in rgb])\n raise ValueError('Illegal color values')", "response": "Translate argb to color string"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntranslating hsl to color string raises ValueError ValueError", "response": "def hsl(self, *args):\n \"\"\" Translate hsl(...) to color string\n raises:\n ValueError\n returns:\n str\n \"\"\"\n if len(args) == 4:\n return self.hsla(*args)\n elif len(args) == 3:\n h, s, l = args\n rgb = colorsys.hls_to_rgb(\n int(h) / 360.0, utility.pc_or_float(l), utility.pc_or_float(s))\n color = (utility.convergent_round(c * 255) for c in rgb)\n return self._rgbatohex(color)\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef hsla(self, *args):\n if len(args) == 4:\n h, s, l, a = args\n rgb = colorsys.hls_to_rgb(\n int(h) / 360.0, utility.pc_or_float(l), utility.pc_or_float(s))\n color = [float(utility.convergent_round(c * 255)) for c in rgb]\n color.append(utility.pc_or_float(a))\n return \"rgba(%s,%s,%s,%s)\" % tuple(color)\n raise ValueError('Illegal color values')", "response": "Translate hsla to color string raises ValueError ValueError"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the hue value of a color in a nutl.", "response": "def hue(self, color, *args):\n \"\"\" Return the hue value of a color\n args:\n color (str): color\n raises:\n ValueError\n returns:\n float\n \"\"\"\n if color:\n h, l, s = self._hextohls(color)\n return utility.convergent_round(h * 360.0, 3)\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the saturation value of a color in a nutime.", "response": "def saturation(self, color, *args):\n \"\"\" Return the saturation value of a color\n args:\n color (str): color\n raises:\n ValueError\n returns:\n float\n \"\"\"\n if color:\n h, l, s = self._hextohls(color)\n return s * 100.0\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlightens a color args: color (str): color diff (str): percentage returns: str", "response": "def lighten(self, color, diff, *args):\n \"\"\" Lighten a color\n args:\n color (str): color\n diff (str): percentage\n returns:\n str\n \"\"\"\n if color and diff:\n return self._ophsl(color, diff, 1, operator.add)\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndarkening a color args: color (str): color diff (str): percentage returns: str", "response": "def darken(self, color, diff, *args):\n \"\"\" Darken a color\n args:\n color (str): color\n diff (str): percentage\n returns:\n str\n \"\"\"\n if color and diff:\n return self._ophsl(color, diff, 1, operator.sub)\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nspins color by degree.", "response": "def spin(self, color, degree, *args):\n \"\"\" Spin color by degree. (Increase / decrease hue)\n args:\n color (str): color\n degree (str): percentage\n raises:\n ValueError\n returns:\n str\n \"\"\"\n if color and degree:\n if isinstance(degree, string_types):\n degree = float(degree.strip('%'))\n h, l, s = self._hextohls(color)\n h = ((h * 360.0) + degree) % 360.0\n h = 360.0 + h if h < 0 else h\n rgb = colorsys.hls_to_rgb(h / 360.0, l, s)\n color = (utility.convergent_round(c * 255) for c in rgb)\n return self._rgbatohex(color)\n raise ValueError('Illegal color values')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nformat CSS Hex color code.", "response": "def fmt(self, color):\n \"\"\" Format CSS Hex color code.\n uppercase becomes lowercase, 3 digit codes expand to 6 digit.\n args:\n color (str): color\n raises:\n ValueError\n returns:\n str\n \"\"\"\n if utility.is_color(color):\n color = color.lower().strip('#')\n if len(color) in [3, 4]:\n color = ''.join([c * 2 for c in color])\n return '#%s' % color\n raise ValueError('Cannot format non-color')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nflattening a list of items into a single element", "response": "def flatten(lst):\n \"\"\"Flatten list.\n Args:\n lst (list): List to flatten\n Returns:\n generator\n \"\"\"\n for elm in lst:\n if isinstance(elm, collections.Iterable) and not isinstance(\n elm, string_types):\n for sub in flatten(elm):\n yield sub\n else:\n yield elm"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pairwise(lst):\n if not lst:\n return\n length = len(lst)\n for i in range(length - 1):\n yield lst[i], lst[i + 1]\n yield lst[-1], None", "response": "Yields items i and i + 1 in lst. e. g. item i and item i + 1 in lst. e. g. item i and item i + 1 in lst. e. g. item i and item i + 1 in lst. e. g. item i + 1 is None is returned."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrename all sub - blocks moved under another block.", "response": "def rename(blocks, scope, stype):\n \"\"\" Rename all sub-blocks moved under another\n block. (mixins)\n Args:\n lst (list): block list\n scope (object): Scope object\n \"\"\"\n for p in blocks:\n if isinstance(p, stype):\n p.tokens[0].parse(scope)\n if p.tokens[1]:\n scope.push()\n scope.current = p.tokens[0]\n rename(p.tokens[1], scope, stype)\n scope.pop()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef blocksearch(block, name):\n if hasattr(block, 'tokens'):\n for b in block.tokens[1]:\n b = (b if hasattr(b, 'raw') and b.raw() == name else blocksearch(\n b, name))\n if b:\n return b\n return False", "response": "Recursive search for name in block"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreverses guard expression. not (@a > 5) -> (@a =< 5) Args: lst (list): Expression returns: list", "response": "def reverse_guard(lst):\n \"\"\" Reverse guard expression. not\n (@a > 5) -> (@a =< 5)\n Args:\n lst (list): Expression\n returns:\n list\n \"\"\"\n rev = {'<': '>=', '>': '=<', '>=': '<', '=<': '>'}\n return [rev[l] if l in rev else l for l in lst]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef debug_print(lst, lvl=0):\n pad = ''.join(['\\t.'] * lvl)\n t = type(lst)\n if t is list:\n for p in lst:\n debug_print(p, lvl)\n elif hasattr(lst, 'tokens'):\n print(pad, t)\n debug_print(list(flatten(lst.tokens)), lvl + 1)", "response": "Print scope tree with nesting level lvl."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef analyze_number(var, err=''):\n n, u = split_unit(var)\n if not isinstance(var, string_types):\n return (var, u)\n if is_color(var):\n return (var, 'color')\n if is_int(n):\n n = int(n)\n elif is_float(n):\n n = float(n)\n else:\n raise SyntaxError('%s \u00b4%s\u00b4' % (err, var))\n return (n, u)", "response": "Analyse number for type and split from unit\n 1px -> ( q px )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn number with unit", "response": "def with_unit(number, unit=None):\n \"\"\" Return number with unit\n args:\n number (mixed): Number\n unit (str): Unit\n returns:\n str\n \"\"\"\n if isinstance(number, tuple):\n number, unit = number\n if number == 0:\n return '0'\n if unit:\n number = str(number)\n if number.startswith('.'):\n number = '0' + number\n return \"%s%s\" % (number, unit)\n return number if isinstance(number, string_types) else str(number)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nam string CSS color", "response": "def is_color(value):\n \"\"\" Is string CSS color\n args:\n value (str): string\n returns:\n bool\n \"\"\"\n if not value or not isinstance(value, string_types):\n return False\n if value[0] == '#' and len(value) in [4, 5, 7, 9]:\n try:\n int(value[1:], 16)\n return True\n except ValueError:\n pass\n return False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_variable(value):\n if isinstance(value, string_types):\n return (value.startswith('@') or value.startswith('-@'))\n elif isinstance(value, tuple):\n value = ''.join(value)\n return (value.startswith('@') or value.startswith('-@'))\n return False", "response": "Check if string is LESS variable"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nam value float args: value (str): string returns: bool", "response": "def is_float(value):\n \"\"\" Is value float\n args:\n value (str): string\n returns:\n bool\n \"\"\"\n if not is_int(value):\n try:\n float(str(value))\n return True\n except (ValueError, TypeError):\n pass\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsplitting a number from its unit 1px -> q px", "response": "def split_unit(value):\n \"\"\" Split a number from its unit\n 1px -> (q, 'px')\n Args:\n value (str): input\n returns:\n tuple\n \"\"\"\n r = re.search('^(\\-?[\\d\\.]+)(.*)$', str(value))\n return r.groups() if r else ('', '')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrounds half - way away from zero.", "response": "def away_from_zero_round(value, ndigits=0):\n \"\"\"Round half-way away from zero.\n\n Python2's round() method.\n \"\"\"\n if sys.version_info[0] >= 3:\n p = 10**ndigits\n return float(math.floor((value * p) + math.copysign(0.5, value))) / p\n else:\n return round(value, ndigits)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pc_or_float(s):\n if isinstance(s, string_types) and '%' in s:\n return float(s.strip('%')) / 100.0\n return float(s)", "response": "Utility function to process strings that contain either percentiles or floats"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef permutations_with_replacement(iterable, r=None):\n pool = tuple(iterable)\n n = len(pool)\n r = n if r is None else r\n for indices in itertools.product(range(n), repeat=r):\n yield list(pool[i] for i in indices)", "response": "Returns successive r length permutations of elements in the iterable."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse a node name and return the node name.", "response": "def parse(self, scope):\n \"\"\" Parse Node\n args:\n scope (Scope): Scope object\n raises:\n SyntaxError\n returns:\n str\n \"\"\"\n assert (len(self.tokens) == 3)\n expr = self.process(self.tokens, scope)\n A, O, B = [\n e[0] if isinstance(e, tuple) else e for e in expr\n if str(e).strip()\n ]\n try:\n a, ua = utility.analyze_number(A, 'Illegal element in expression')\n b, ub = utility.analyze_number(B, 'Illegal element in expression')\n except SyntaxError:\n return ' '.join([str(A), str(O), str(B)])\n if (a is False or b is False):\n return ' '.join([str(A), str(O), str(B)])\n if ua == 'color' or ub == 'color':\n return color.Color().process((A, O, B))\n if a == 0 and O == '/':\n # NOTE(saschpe): The ugliest but valid CSS since sliced bread: 'font: 0/1 a;'\n return ''.join([str(A), str(O), str(B), ' '])\n out = self.operate(a, b, O)\n if isinstance(out, bool):\n return out\n return self.with_units(out, ua, ub)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn value with unit.", "response": "def with_units(self, val, ua, ub):\n \"\"\"Return value with unit.\n args:\n val (mixed): result\n ua (str): 1st unit\n ub (str): 2nd unit\n raises:\n SyntaxError\n returns:\n str\n \"\"\"\n if not val:\n return str(val)\n if ua or ub:\n if ua and ub:\n if ua == ub:\n return str(val) + ua\n else:\n # Nodejs version does not seem to mind mismatched\n # units within expressions. So we choose the first\n # as they do\n # raise SyntaxError(\"Error in expression %s != %s\" % (ua, ub))\n return str(val) + ua\n elif ua:\n return str(val) + ua\n elif ub:\n return str(val) + ub\n return repr(val)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef operate(self, vala, valb, oper):\n operation = {\n '+': operator.add,\n '-': operator.sub,\n '*': operator.mul,\n '/': operator.truediv,\n '=': operator.eq,\n '>': operator.gt,\n '<': operator.lt,\n '>=': operator.ge,\n '=<': operator.le,\n }.get(oper)\n if operation is None:\n raise SyntaxError(\"Unknown operation %s\" % oper)\n ret = operation(vala, valb)\n if oper in '+-*/' and int(ret) == ret:\n ret = int(ret)\n return ret", "response": "Perform an operation on the key - value pair."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing a file into a set of unique identifiers.", "response": "def parse(self, filename=None, file=None, debuglevel=0):\n \"\"\" Parse file.\n kwargs:\n filename (str): File to parse\n debuglevel (int): Parser debuglevel\n \"\"\"\n self.scope.push()\n\n if not file:\n # We use a path.\n file = filename\n else:\n # We use a stream and try to extract the name from the stream.\n if hasattr(file, 'name'):\n if filename is not None:\n raise AssertionError(\n 'names of file and filename are in conflict')\n filename = file.name\n else:\n filename = '(stream)'\n\n self.target = filename\n if self.verbose and not self.fail_with_exc:\n print('Compiling target: %s' % filename, file=sys.stderr)\n self.result = self.parser.parse(file, lexer=self.lex, debug=debuglevel)\n\n self.post_parse()\n self.register.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef post_parse(self):\n if self.result:\n out = []\n for pu in self.result:\n try:\n out.append(pu.parse(self.scope))\n except SyntaxError as e:\n self.handle_error(e, 0)\n self.result = list(utility.flatten(out))", "response": "Post parse cycle. nodejs version allows calls to mixins\n not yet defined or known to the parser. We defer all calls\n to mixins until after first cycle when all names are known."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_unit_list(self, p):\n if isinstance(p[1], list):\n if len(p) >= 3:\n if isinstance(p[2], list):\n p[1].extend(p[2])\n else:\n p[1].append(p[2])\n else:\n p[1] = [p[1]]\n p[0] = p[1]", "response": "unit_list - list of all units"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_statement_namespace(self, p):\n p[0] = Statement(list(p)[1:], p.lineno(1))\n p[0].parse(None)", "response": "Parse a statement in the form of a namespace."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_block(self, p):\n p[0] = Block(list(p)[1:-1], p.lineno(3))\n self.scope.pop()\n self.scope.add_block(p[0])", "response": "block_decl : block_open declaration_list brace_close"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreplaces block with new one.", "response": "def p_block_replace(self, p):\n \"\"\" block_decl : identifier t_semicolon\n \"\"\"\n m = p[1].parse(None)\n block = self.scope.blocks(m.raw())\n if block:\n p[0] = block.copy_inner(self.scope)\n else:\n # fallback to mixin. Allow calls to mixins without parens\n p[0] = Deferred(p[1], None, p.lineno(2))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_font_face_open(self, p):\n p[0] = Identifier([p[1], p[2]]).parse(self.scope)", "response": "P font face open section"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_open_mixin(self, p):\n p[1].parse(self.scope)\n self.scope.current = p[1]\n p[0] = [p[1], p[3]]\n if len(p) > 6:\n p[0].append(p[5])\n else:\n p[0].append(None)", "response": "open_mixin - Handles the mixin - level open - mixin command."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_mixin_guard_cond_list_aux(self, p):\n p[1].append(p[2])\n p[1].append(p[3])\n p[0] = p[1]", "response": "A mixin guard condition list auxiliary function."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_call_mixin(self, p):\n p[1].parse(None)\n p[0] = Deferred(p[1], p[3], p.lineno(4))", "response": "\\ x1b [ 1mNAME \\ x1b [ 0m mixin - Augment the mixin with a Deferred object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_declaration_list(self, p):\n if len(p) > 2:\n p[1].extend(p[2])\n p[0] = p[1]", "response": "A function to add the declaration_list to the given parameter list."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_variable_decl(self, p):\n p[0] = Variable(list(p)[1:-1], p.lineno(4))\n p[0].parse(self.scope)", "response": "variable_decl : variable t_colon style_list t_semicolon"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse the property declaration.", "response": "def p_property_decl(self, p):\n \"\"\" property_decl : prop_open style_list t_semicolon\n | prop_open style_list css_important t_semicolon\n | prop_open empty t_semicolon\n \"\"\"\n l = len(p)\n p[0] = Property(list(p)[1:-1], p.lineno(l - 1))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_identifier_list_aux(self, p):\n p[1].extend([p[2]])\n p[1].extend(p[3])\n p[0] = p[1]", "response": "A version of the identifier_list_aux method."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_identifier_group_op(self, p):\n p[1].extend([p[2]])\n if len(p) > 3:\n p[1].extend(p[3])\n p[0] = p[1]", "response": "A function to handle the identifier_group operation."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_ident_parts_aux(self, p):\n if isinstance(p[2], list):\n p[1].extend(p[2])\n else:\n p[1].append(p[2])\n p[0] = p[1]", "response": "A version of p_ident_parts_aux that adds the new version to the list of the set of available ident_parts."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_ident_parts(self, p):\n if not isinstance(p[1], list):\n p[1] = [p[1]]\n p[0] = p[1]", "response": "A helper function to set the ident_parts property in a dict."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_media_query_value(self, p):\n if utility.is_variable(p[1]):\n var = self.scope.variables(''.join(p[1]))\n if var:\n value = var.value[0]\n if hasattr(value, 'parse'):\n p[1] = value.parse(self.scope)\n else:\n p[1] = value\n if isinstance(p[1], Expression):\n p[0] = p[1].parse(self.scope)\n else:\n p[0] = p[1]", "response": "media_query_value : number\n | variable\n | word\n | color\n | expression"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_color(self, p):\n try:\n p[0] = Color().fmt(p[1])\n if len(p) > 2:\n p[0] = [p[0], p[2]]\n except ValueError:\n self.handle_error('Illegal color value `%s`' % p[1], p.lineno(1),\n 'W')\n p[0] = p[1]", "response": "color : css_color\n | css_color t_ws"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_error(self, t):\n if t:\n error_msg = \"E: %s line: %d, Syntax Error, token: `%s`, `%s`\" % \\\n (self.target, t.lineno, t.type, t.value)\n self.register.register(error_msg)\n while True:\n t = self.lex.token()\n if not t or t.value == '}':\n if len(self.scope) > 1:\n self.scope.pop()\n break\n self.parser.restart()\n return t", "response": "Internal error handler for the internal error handler."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef handle_error(self, e, line, t='E'):\n self.register.register(\"%s: line: %d: %s\\n\" % (t, line, e))", "response": "Custom error handler for the\n class."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing node s tokens and return a list of parsed tokens.", "response": "def parse(self, scope):\n \"\"\"Parse node\n args:\n scope (Scope): current scope\n raises:\n SyntaxError\n returns:\n parsed\n \"\"\"\n if not self.parsed:\n self.parsed = ''.join(self.process(self.tokens, scope))\n return self.parsed"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef NextPage(gh):\n header = dict(gh.getheaders())\n if 'Link' in header:\n parts = header['Link'].split(',')\n for part in parts:\n subparts = part.split(';')\n sub = subparts[1].split('=')\n if sub[0].strip() == 'rel':\n if sub[1] == '\"next\"':\n page = int(\n re.match(\n r'.*page=(\\d+).*', subparts[0],\n re.IGNORECASE | re.DOTALL | re.UNICODE\n ).groups()[0]\n )\n return page\n return 0", "response": "Checks if a GitHub call returned multiple pages of data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfetch GitHub token from git config and last CHANGELOG_GITHUB_TOKEN env variable.", "response": "def fetch_github_token(self):\n \"\"\"\n Fetch GitHub token. First try to use variable provided\n by --token option, otherwise try to fetch it from git config\n and last CHANGELOG_GITHUB_TOKEN env variable.\n\n :returns: Nothing\n \"\"\"\n\n if not self.options.token:\n try:\n for v in GH_CFG_VARS:\n cmd = ['git', 'config', '--get', '{0}'.format(v)]\n self.options.token = subprocess.Popen(\n cmd, stdout=subprocess.PIPE).communicate()[0].strip()\n if self.options.token:\n break\n except (subprocess.CalledProcessError, WindowsError):\n pass\n if not self.options.token:\n self.options.token = os.environ.get(CHANGELOG_GITHUB_TOKEN)\n if not self.options.token:\n print(NO_TOKEN_PROVIDED)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfetches all tags for a repository from Github.", "response": "def get_all_tags(self):\n \"\"\"\n Fetch all tags for repository from Github.\n\n :return: tags in repository\n :rtype: list\n \"\"\"\n\n verbose = self.options.verbose\n gh = self.github\n user = self.options.user\n repo = self.options.project\n if verbose:\n print(\"Fetching tags...\")\n\n tags = []\n page = 1\n while page > 0:\n if verbose > 2:\n print(\".\", end=\"\")\n rc, data = gh.repos[user][repo].tags.get(\n page=page, per_page=PER_PAGE_NUMBER)\n if rc == 200:\n tags.extend(data)\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n page = NextPage(gh)\n if verbose > 2:\n print(\".\")\n\n if len(tags) == 0:\n if not self.options.quiet:\n print(\"Warning: Can't find any tags in repo. Make sure, that \"\n \"you push tags to remote repo via 'git push --tags'\")\n exit()\n if verbose > 1:\n print(\"Found {} tag(s)\".format(len(tags)))\n return tags"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef fetch_closed_issues_and_pr(self):\n\n verbose = self.options.verbose\n gh = self.github\n user = self.options.user\n repo = self.options.project\n if verbose:\n print(\"Fetching closed issues and pull requests...\")\n\n data = []\n issues = []\n data = []\n page = 1\n while page > 0:\n if verbose > 2:\n print(\".\", end=\"\")\n rc, data = gh.repos[user][repo].issues.get(\n page=page, per_page=PER_PAGE_NUMBER,\n state='closed', filter='all'\n )\n if rc == 200:\n issues.extend(data)\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n if len(issues) >= self.options.max_issues:\n break\n page = NextPage(gh)\n self.first_issue = data[-1] if len(data) > 0 else []\n if verbose > 2:\n print(\".\")\n\n # separate arrays of issues and pull requests:\n prs = []\n iss = []\n for i in issues:\n if \"pull_request\" in i:\n prs.append(i)\n else:\n iss.append(i)\n if verbose > 1:\n print(\"\\treceived {} issues and {} pull requests.\".format(\n len(iss), len(prs))\n )\n return iss, prs", "response": "This method fetches all closed issues and pull requests and pure issues and pure issues."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfetches all closed pull requests. We need them to detect merged_at", "response": "def fetch_closed_pull_requests(self):\n \"\"\"\n Fetch all pull requests. We need them to detect \"merged_at\" parameter\n\n :rtype: list\n :return: all pull requests\n \"\"\"\n\n pull_requests = []\n verbose = self.options.verbose\n gh = self.github\n user = self.options.user\n repo = self.options.project\n if verbose:\n print(\"Fetching closed pull requests...\")\n page = 1\n while page > 0:\n if verbose > 2:\n print(\".\", end=\"\")\n\n if self.options.release_branch:\n rc, data = gh.repos[user][repo].pulls.get(\n page=page, per_page=PER_PAGE_NUMBER, state='closed',\n base=self.options.release_branch\n )\n else:\n rc, data = gh.repos[user][repo].pulls.get(\n page=page, per_page=PER_PAGE_NUMBER, state='closed',\n )\n\n if rc == 200:\n pull_requests.extend(data)\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n page = NextPage(gh)\n if verbose > 2:\n print(\".\")\n if verbose > 1:\n print(\"\\tfetched {} closed pull requests.\".format(\n len(pull_requests))\n )\n return pull_requests"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fetch_repo_creation_date(self):\n gh = self.github\n user = self.options.user\n repo = self.options.project\n rc, data = gh.repos[user][repo].get()\n if rc == 200:\n return REPO_CREATED_TAG_NAME, data[\"created_at\"]\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n return None, None", "response": "Fetch the creation date of the repository from GitHub."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfetch events for all issues and add them to self. events", "response": "def fetch_events_async(self, issues, tag_name):\n \"\"\"\n Fetch events for all issues and add them to self.events\n\n :param list issues: all issues\n :param str tag_name: name of the tag to fetch events for\n :returns: Nothing\n \"\"\"\n\n if not issues:\n return issues\n\n max_simultaneous_requests = self.options.max_simultaneous_requests\n verbose = self.options.verbose\n gh = self.github\n user = self.options.user\n repo = self.options.project\n self.events_cnt = 0\n if verbose:\n print(\"fetching events for {} {}... \".format(\n len(issues), tag_name)\n )\n\n def worker(issue):\n page = 1\n issue['events'] = []\n while page > 0:\n rc, data = gh.repos[user][repo].issues[\n issue['number']].events.get(\n page=page, per_page=PER_PAGE_NUMBER)\n if rc == 200:\n issue['events'].extend(data)\n self.events_cnt += len(data)\n else:\n self.raise_GitHubError(rc, data, gh.getheaders())\n page = NextPage(gh)\n\n threads = []\n cnt = len(issues)\n for i in range(0, (cnt // max_simultaneous_requests) + 1):\n for j in range(max_simultaneous_requests):\n idx = i * max_simultaneous_requests + j\n if idx == cnt:\n break\n t = threading.Thread(target=worker, args=(issues[idx],))\n threads.append(t)\n t.start()\n if verbose > 2:\n print(\".\", end=\"\")\n if not idx % PER_PAGE_NUMBER:\n print(\"\")\n for t in threads:\n t.join()\n if verbose > 2:\n print(\".\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfetch time of specified tag from repository.", "response": "def fetch_date_of_tag(self, tag):\n \"\"\"\n Fetch time for tag from repository.\n\n :param dict tag: dictionary with tag information\n :rtype: str\n :return: time of specified tag as ISO date string\n \"\"\"\n\n if self.options.verbose > 1:\n print(\"\\tFetching date for tag {}\".format(tag[\"name\"]))\n gh = self.github\n user = self.options.user\n repo = self.options.project\n\n rc, data = gh.repos[user][repo].git.commits[\n tag[\"commit\"][\"sha\"]].get()\n if rc == 200:\n return data[\"committer\"][\"date\"]\n self.raise_GitHubError(rc, data, gh.getheaders())"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef fetch_commit(self, event):\n\n gh = self.github\n user = self.options.user\n repo = self.options.project\n\n rc, data = gh.repos[user][repo].git.commits[\n event[\"commit_id\"]].get()\n if rc == 200:\n return data\n self.raise_GitHubError(rc, data, gh.getheaders())", "response": "Fetch the commit data for the specified event."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef run(self):\n if not self.options.project or not self.options.user:\n print(\"Project and/or user missing. \"\n \"For help run:\\n pygcgen --help\")\n return\n\n if not self.options.quiet:\n print(\"Generating changelog...\")\n\n log = None\n try:\n log = self.generator.compound_changelog()\n except ChangelogGeneratorError as err:\n print(\"\\n\\033[91m\\033[1m{}\\x1b[0m\".format(err.args[0]))\n exit(1)\n if not log:\n if not self.options.quiet:\n print(\"Empty changelog generated. {} not written.\".format(\n self.options.output)\n )\n return\n\n if self.options.no_overwrite:\n out = checkname(self.options.output)\n else:\n out = self.options.output\n\n with codecs.open(out, \"w\", \"utf-8\") as fh:\n fh.write(log)\n\n if not self.options.quiet:\n print(\"Done!\")\n print(\"Generated changelog written to {}\".format(out))", "response": "Generate a new entry point for one of the tags in the list of tags."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_heading(heading):\n\n heading_structures = [\n r\"^## \\[(?P.+?)\\]\\((?P.+?)\\)( \\((?P.+?)\\))?$\",\n r\"^## (?P.+?)( \\((?P.+?)\\))?$\",\n ]\n captures = {\"version\": None, \"url\": None, \"date\": None}\n\n for regexp in heading_structures:\n matches = re.match(regexp, heading)\n if matches:\n captures.update(matches.groupdict())\n break\n return captures", "response": "Parse a single heading and return a Hash containing version url and date."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse(data):\n\n sections = re.compile(\"^## .+$\", re.MULTILINE).split(data)\n headings = re.findall(\"^## .+?$\", data, re.MULTILINE)\n sections.pop(0)\n parsed = []\n\n def func(h, s):\n p = parse_heading(h)\n p[\"content\"] = s\n parsed.append(p)\n\n list(map(func, headings, sections))\n return parsed", "response": "Parse the given ChangeLog data into a list of Hashes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate the signal handler map based on self. signal_map", "response": "def _signal_handler_map(self):\n \"\"\" Create the signal handler map\n\n create a dictionary with signal:handler mapping based on\n self.signal_map\n\n :return: dict\n \"\"\"\n result = {}\n for signum, handler in self.signal_map.items():\n result[signum] = self._get_signal_handler(handler)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef open(self):\n if self.is_open:\n return\n try:\n os.chdir(self.working_directory)\n if self.chroot_directory:\n os.chroot(self.chroot_directory)\n os.setgid(self.gid)\n os.setuid(self.uid)\n os.umask(self.umask)\n except OSError as err:\n raise DaemonError('Setting up Environment failed: {0}'\n .format(err))\n\n if self.prevent_core:\n try:\n resource.setrlimit(resource.RLIMIT_CORE, (0, 0))\n except Exception as err:\n raise DaemonError('Could not disable core files: {0}'\n .format(err))\n\n if self.detach_process:\n try:\n if os.fork() > 0:\n os._exit(0)\n except OSError as err:\n raise DaemonError('First fork failed: {0}'.format(err))\n os.setsid()\n try:\n if os.fork() > 0:\n os._exit(0)\n except OSError as err:\n raise DaemonError('Second fork failed: {0}'.format(err))\n\n for (signal_number, handler) in self._signal_handler_map.items():\n signal.signal(signal_number, handler)\n\n close_filenos(self._files_preserve)\n\n redirect_stream(sys.stdin, self.stdin)\n redirect_stream(sys.stdout, self.stdout)\n redirect_stream(sys.stderr, self.stderr)\n\n if self.pidfile:\n self.pidfile.acquire()\n\n self._is_open = True", "response": "Open the process and return the unique identifier."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef user_and_project_from_git(self, options, arg0=None, arg1=None):\n user, project = self.user_project_from_option(options, arg0, arg1)\n if user and project:\n return user, project\n\n try:\n remote = subprocess.check_output(\n [\n 'git', 'config', '--get',\n 'remote.{0}.url'.format(options.git_remote)\n ]\n )\n except subprocess.CalledProcessError:\n return None, None\n except WindowsError:\n print(\"git binary not found.\")\n exit(1)\n else:\n return self.user_project_from_remote(remote)", "response": "Detects user and project from git."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntry to find user and project name from git remote command MimeType", "response": "def user_project_from_option(options, arg0, arg1):\n \"\"\"\n Try to find user and project name from git remote output\n\n @param [String] output of git remote command\n @return [Array] user and project\n \"\"\"\n\n site = options.github_site\n if arg0 and not arg1:\n # this match should parse strings such as\n # \"https://github.com/skywinder/Github-Changelog-Generator\"\n # or\n # \"skywinder/Github-Changelog-Generator\"\n # to user and project\n match = re.match(\n \"(?:.+{site}/)?(.+)/(.+)\".format(site=site),\n arg0\n )\n if not match:\n print(\"Can't detect user and name from first \"\n \"parameter: '{arg0}' -> exit'\".format(arg0=arg0))\n exit(1)\n return match.groups()\n return None, None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef user_project_from_remote(remote):\n\n # try to find repo in format:\n # origin\tgit@github.com:skywinder/Github-Changelog-Generator.git (fetch)\n # git@github.com:skywinder/Github-Changelog-Generator.git\n regex1 = br\".*(?:[:/])(?P(-|\\w|\\.)*)/\" \\\n br\"(?P(-|\\w|\\.)*)(\\.git).*\"\n match = re.match(regex1, remote)\n if match:\n return match.group(\"user\"), match.group(\"project\")\n\n # try to find repo in format:\n # origin\thttps://github.com/skywinder/ChangelogMerger (fetch)\n # https://github.com/skywinder/ChangelogMerger\n regex2 = r\".*/((?:-|\\w|\\.)*)/((?:-|\\w|\\.)*).*\"\n match = re.match(regex2, remote)\n if match:\n return match.group(\"user\"), match.group(\"project\")\n\n return None, None", "response": "Try to find user and project name from git remote output\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef timestring_to_datetime(timestring):\n with warnings.catch_warnings():\n warnings.filterwarnings(\"ignore\", category=UnicodeWarning)\n result = dateutil_parser(timestring)\n\n return result", "response": "Convert an ISO formated date and time string to a datetime object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching event for issues and pull requests", "response": "def fetch_events_for_issues_and_pr(self):\n \"\"\"\n Fetch event for issues and pull requests\n\n @return [Array] array of fetched issues\n \"\"\"\n\n # Async fetching events:\n self.fetcher.fetch_events_async(self.issues, \"issues\")\n self.fetcher.fetch_events_async(self.pull_requests, \"pull requests\")"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef detect_actual_closed_dates(self, issues, kind):\n\n if self.options.verbose:\n print(\"Fetching closed dates for {} {}...\".format(\n len(issues), kind)\n )\n all_issues = copy.deepcopy(issues)\n for issue in all_issues:\n if self.options.verbose > 2:\n print(\".\", end=\"\")\n if not issues.index(issue) % 30:\n print(\"\")\n self.find_closed_date_by_commit(issue)\n\n if not issue.get('actual_date', False):\n if issue.get('closed_at', False):\n print(\"Skipping closed non-merged issue: #{0} {1}\".format(\n issue[\"number\"], issue[\"title\"]))\n\n all_issues.remove(issue)\n\n if self.options.verbose > 2:\n print(\".\")\n return all_issues", "response": "Detects correct closed dates for issues that were closed by commits."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_closed_date_by_commit(self, issue):\n\n if not issue.get('events'):\n return\n # if it's PR -> then find \"merged event\", in case\n # of usual issue -> find closed date\n compare_string = \"merged\" if 'merged_at' in issue else \"closed\"\n # reverse! - to find latest closed event. (event goes in date order)\n # if it were reopened and closed again.\n issue['events'].reverse()\n found_date = False\n for event in issue['events']:\n if event[\"event\"] == compare_string:\n self.set_date_from_event(event, issue)\n found_date = True\n break\n if not found_date:\n # TODO: assert issues, that remain without\n # 'actual_date' hash for some reason.\n print(\"\\nWARNING: Issue without 'actual_date':\"\n \" #{0} {1}\".format(issue[\"number\"], issue[\"title\"]))", "response": "Find the closed date of a given issue by the commit."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_date_from_event(self, event, issue):\n\n if not event.get('commit_id', None):\n issue['actual_date'] = timestring_to_datetime(issue['closed_at'])\n return\n try:\n commit = self.fetcher.fetch_commit(event)\n issue['actual_date'] = timestring_to_datetime(\n commit['author']['date']\n )\n except ValueError:\n print(\"WARNING: Can't fetch commit {0}. \"\n \"It is probably referenced from another repo.\".\n format(event['commit_id']))\n issue['actual_date'] = timestring_to_datetime(issue['closed_at'])", "response": "Set the actual date from this issue."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nencapsulates characters to make markdown look as expected.", "response": "def encapsulate_string(raw_string):\n \"\"\"\n Encapsulate characters to make markdown look as expected.\n\n :param str raw_string: string to encapsulate\n :rtype: str\n :return: encapsulated input string\n \"\"\"\n\n raw_string.replace('\\\\', '\\\\\\\\')\n enc_string = re.sub(\"([<>*_()\\[\\]#])\", r\"\\\\\\1\", raw_string)\n return enc_string"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef compound_changelog(self):\n\n self.fetch_and_filter_tags()\n tags_sorted = self.sort_tags_by_date(self.filtered_tags)\n self.filtered_tags = tags_sorted\n self.fetch_and_filter_issues_and_pr()\n\n log = str(self.options.frontmatter) \\\n if self.options.frontmatter else u\"\"\n log += u\"{0}\\n\\n\".format(self.options.header)\n\n if self.options.unreleased_only:\n log += self.generate_unreleased_section()\n else:\n log += self.generate_log_for_all_tags()\n\n try:\n with open(self.options.base) as fh:\n log += fh.read()\n except (TypeError, IOError):\n pass\n return log", "response": "Generates a compound change log file for the current locale."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates formated list of issues for changelog.", "response": "def generate_sub_section(self, issues, prefix):\n \"\"\"\n Generate formated list of issues for changelog.\n\n :param list issues: Issues to put in sub-section.\n :param str prefix: Title of sub-section.\n :rtype: str\n :return: Generated ready-to-add sub-section.\n \"\"\"\n\n log = \"\"\n if issues:\n if not self.options.simple_list:\n log += u\"{0}\\n\\n\".format(prefix)\n for issue in issues:\n merge_string = self.get_string_for_issue(issue)\n log += u\"- {0}\\n\".format(merge_string)\n log += \"\\n\"\n return log"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngenerate a header for a tag section.", "response": "def generate_header(self, newer_tag_name, newer_tag_link,\n newer_tag_time,\n older_tag_link, project_url):\n \"\"\"\n Generate a header for a tag section with specific parameters.\n\n :param str newer_tag_name: Name (title) of newer tag.\n :param str newer_tag_link: Tag name of newer tag, used for links.\n Could be same as **newer_tag_name** or some\n specific value, like `HEAD`.\n :param datetime newer_tag_time: Date and time when\n newer tag was created.\n :param str older_tag_link: Tag name of older tag, used for links.\n :param str project_url: URL for current project.\n :rtype: str\n :return: Generated ready-to-add tag section.\n \"\"\"\n\n log = \"\"\n # Generate date string:\n # noinspection PyUnresolvedReferences\n time_string = newer_tag_time.strftime(self.options.date_format)\n\n # Generate tag name and link\n if self.options.release_url:\n release_url = self.options.release_url.format(newer_tag_link)\n else:\n release_url = u\"{project_url}/tree/{newer_tag_link}\".format(\n project_url=project_url, newer_tag_link=newer_tag_link)\n\n if not self.options.unreleased_with_date and \\\n newer_tag_name == self.options.unreleased_label:\n log += u\"## [{newer_tag_name}]({release_url})\\n\\n\".format(\n newer_tag_name=newer_tag_name, release_url=release_url)\n else:\n log += u\"## [{newer_tag_name}]({release_url}) \" \\\n u\"({time_string})\\n\".format(\n newer_tag_name=newer_tag_name,\n release_url=release_url,\n time_string=time_string\n )\n\n if self.options.compare_link \\\n and older_tag_link != REPO_CREATED_TAG_NAME:\n # Generate compare link\n log += u\"[Full Changelog]\"\n log += u\"({project_url}/compare/{older_tag_link}\".format(\n project_url=project_url,\n older_tag_link=older_tag_link,\n )\n log += u\"...{newer_tag_link})\\n\\n\".format(\n newer_tag_link=newer_tag_link\n )\n return log"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates log between two tags.", "response": "def generate_log_between_tags(self, older_tag, newer_tag):\n \"\"\"\n Generate log between 2 specified tags.\n\n :param dict older_tag: All issues before this tag's date will be\n excluded. May be special value, if new tag is\n the first tag. (Means **older_tag** is when\n the repo was created.)\n :param dict newer_tag: All issues after this tag's date will be\n excluded. May be title of unreleased section.\n :rtype: str\n :return: Generated ready-to-add tag section for newer tag.\n \"\"\"\n\n filtered_issues, filtered_pull_requests = \\\n self.filter_issues_for_tags(newer_tag, older_tag)\n\n older_tag_name = older_tag[\"name\"] if older_tag \\\n else self.detect_since_tag()\n\n if not filtered_issues and not filtered_pull_requests:\n # do not generate an unreleased section if it would be empty\n return \"\"\n return self.generate_log_for_tag(\n filtered_pull_requests, filtered_issues,\n newer_tag, older_tag_name)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\napplying all filters to issues and pull requests.", "response": "def filter_issues_for_tags(self, newer_tag, older_tag):\n \"\"\"\n Apply all filters to issues and pull requests.\n\n :param dict older_tag: All issues before this tag's date will be\n excluded. May be special value, if new tag is\n the first tag. (Means **older_tag** is when\n the repo was created.)\n :param dict newer_tag: All issues after this tag's date will be\n excluded. May be title of unreleased section.\n :rtype: list(dict), list(dict)\n :return: Filtered issues and pull requests.\n \"\"\"\n\n filtered_pull_requests = self.delete_by_time(self.pull_requests,\n older_tag, newer_tag)\n filtered_issues = self.delete_by_time(self.issues, older_tag,\n newer_tag)\n\n newer_tag_name = newer_tag[\"name\"] if newer_tag else None\n\n if self.options.filter_issues_by_milestone:\n # delete excess irrelevant issues (according milestones).Issue #22.\n filtered_issues = self.filter_by_milestone(\n filtered_issues, newer_tag_name, self.issues\n )\n filtered_pull_requests = self.filter_by_milestone(\n filtered_pull_requests, newer_tag_name, self.pull_requests\n )\n return filtered_issues, filtered_pull_requests"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngenerating a complete change log for all tags.", "response": "def generate_log_for_all_tags(self):\n \"\"\"\n The full cycle of generation for whole project.\n\n :rtype: str\n :return: The complete change log for released tags.\n \"\"\"\n\n if self.options.verbose:\n print(\"Generating log...\")\n self.issues2 = copy.deepcopy(self.issues)\n\n log1 = \"\"\n if self.options.with_unreleased:\n log1 = self.generate_unreleased_section()\n\n log = \"\"\n for index in range(len(self.filtered_tags) - 1):\n log += self.do_generate_log_for_all_tags_part1(log, index)\n\n if self.options.tag_separator and log1:\n log = log1 + self.options.tag_separator + log\n else:\n log = log1 + log\n\n if len(self.filtered_tags) != 0:\n log += self.do_generate_log_for_all_tags_part2(log)\n\n return log"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngenerates log for unreleased closed issues.", "response": "def generate_unreleased_section(self):\n \"\"\"\n Generate log for unreleased closed issues.\n\n :rtype: str\n :return: Generated ready-to-add unreleased section.\n \"\"\"\n if not self.filtered_tags:\n return \"\"\n now = datetime.datetime.utcnow()\n now = now.replace(tzinfo=dateutil.tz.tzutc())\n head_tag = {\"name\": self.options.unreleased_label}\n self.tag_times_dict[head_tag[\"name\"]] = now\n unreleased_log = self.generate_log_between_tags(\n self.filtered_tags[0], head_tag)\n return unreleased_log"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_string_for_issue(self, issue):\n\n encapsulated_title = self.encapsulate_string(issue['title'])\n try:\n title_with_number = u\"{0} [\\\\#{1}]({2})\".format(\n encapsulated_title, issue[\"number\"], issue[\"html_url\"]\n )\n except UnicodeEncodeError:\n # TODO: why did i add this? Is it needed?\n title_with_number = \"ERROR ERROR ERROR: #{0} {1}\".format(\n issue[\"number\"], issue['title']\n )\n print(title_with_number, '\\n', issue[\"html_url\"])\n return self.issue_line_with_user(title_with_number, issue)", "response": "Parse issue and generate single line formatted issue line."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef issue_line_with_user(self, line, issue):\n if not issue.get(\"pull_request\") or not self.options.author:\n return line\n\n if not issue.get(\"user\"):\n line += u\" (Null user)\"\n elif self.options.username_as_tag:\n line += u\" (@{0})\".format(\n issue[\"user\"][\"login\"]\n )\n else:\n line += u\" ([{0}]({1}))\".format(\n issue[\"user\"][\"login\"], issue[\"user\"][\"html_url\"]\n )\n return line", "response": "Adds author link to the author of the pull request."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngenerate log for a tag section.", "response": "def generate_log_for_tag(self,\n pull_requests,\n issues,\n newer_tag,\n older_tag_name):\n \"\"\"\n Generates log for tag section with header and body.\n\n :param list(dict) pull_requests: List of PR's in this tag section.\n :param list(dict) issues: List of issues in this tag section.\n :param dict newer_tag: Github data of tag for this section.\n :param str older_tag_name: Older tag, used for the links.\n May be special value, if **newer tag** is\n the first tag. (Means **older_tag** is when\n the repo was created.)\n :rtype: str\n :return: Ready-to-add and parsed tag section.\n \"\"\"\n\n newer_tag_link, newer_tag_name, \\\n newer_tag_time = self.detect_link_tag_time(newer_tag)\n\n github_site = \"https://github.com\" or self.options.github_endpoint\n project_url = \"{0}/{1}/{2}\".format(\n github_site, self.options.user, self.options.project)\n\n log = self.generate_header(newer_tag_name, newer_tag_link,\n newer_tag_time, older_tag_name, project_url)\n if self.options.issues:\n # Generate issues:\n log += self.issues_to_log(issues, pull_requests)\n if self.options.include_pull_request:\n # Generate pull requests:\n log += self.generate_sub_section(\n pull_requests, self.options.merge_prefix\n )\n return log"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef issues_to_log(self, issues, pull_requests):\n\n log = \"\"\n sections_a, issues_a = self.parse_by_sections(\n issues, pull_requests)\n\n for section, s_issues in sections_a.items():\n log += self.generate_sub_section(s_issues, section)\n log += self.generate_sub_section(issues_a, self.options.issue_prefix)\n return log", "response": "Generate ready - to - paste log from list of issues and pull requests."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_by_sections(self, issues, pull_requests):\n\n issues_a = []\n sections_a = OrderedDict()\n\n if not self.options.sections:\n return [sections_a, issues]\n for key in self.options.sections:\n sections_a.update({key: []})\n self.parse_by_sections_for_issues(issues, sections_a, issues_a)\n self.parse_by_sections_for_pr(pull_requests, sections_a)\n return [sections_a, issues_a]", "response": "This method sorts the issues and pull_requests by sections."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exclude_issues_by_labels(self, issues):\n if not self.options.exclude_labels:\n return copy.deepcopy(issues)\n\n remove_issues = set()\n exclude_labels = self.options.exclude_labels\n include_issues = []\n for issue in issues:\n for label in issue[\"labels\"]:\n if label[\"name\"] in exclude_labels:\n remove_issues.add(issue[\"number\"])\n break\n for issue in issues:\n if issue[\"number\"] not in remove_issues:\n include_issues.append(issue)\n return include_issues", "response": "Returns a list of issues that are not in the exclude - labels option."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef filter_by_milestone(self, filtered_issues, tag_name, all_issues):\n\n filtered_issues = self.remove_issues_in_milestones(filtered_issues)\n if tag_name:\n # add missed issues (according milestones)\n issues_to_add = self.find_issues_to_add(all_issues, tag_name)\n filtered_issues.extend(issues_to_add)\n return filtered_issues", "response": "Filter issues according to a milestone."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_issues_to_add(all_issues, tag_name):\n\n filtered = []\n for issue in all_issues:\n if issue.get(\"milestone\"):\n if issue[\"milestone\"][\"title\"] == tag_name:\n iss = copy.deepcopy(issue)\n filtered.append(iss)\n return filtered", "response": "Find all issues that should be in that tag according to milestone."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving issues that contain milestones with the same name as a tag.", "response": "def remove_issues_in_milestones(self, filtered_issues):\n \"\"\"\n :param list(dict) filtered_issues: Filtered issues.\n :rtype: list(dict)\n :return: List with removed issues, that contain milestones with\n same name as a tag.\n \"\"\"\n for issue in filtered_issues:\n # leave issues without milestones\n if issue[\"milestone\"]:\n # check, that this milestone is in tag list:\n for tag in self.filtered_tags:\n if tag[\"name\"] == issue[\"milestone\"][\"title\"]:\n filtered_issues.remove(issue)\n return filtered_issues"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfilters issues that belong to specified tag range.", "response": "def delete_by_time(self, issues, older_tag, newer_tag):\n \"\"\"\n Filter issues that belong to specified tag range.\n\n :param list(dict) issues: Issues to filter.\n :param dict older_tag: All issues before this tag's date will be\n excluded. May be special value, if **newer_tag**\n is the first tag. (Means **older_tag** is when\n the repo was created.)\n :param dict newer_tag: All issues after this tag's date will be\n excluded. May be title of unreleased section.\n :rtype: list(dict)\n :return: Filtered issues.\n \"\"\"\n\n if not older_tag and not newer_tag:\n # in case if no tags are specified - return unchanged array\n return copy.deepcopy(issues)\n\n newer_tag_time = self.get_time_of_tag(newer_tag)\n older_tag_time = self.get_time_of_tag(older_tag)\n filtered = []\n for issue in issues:\n if issue.get('actual_date'):\n rslt = older_tag_time < issue['actual_date'] <= newer_tag_time\n if rslt:\n filtered.append(copy.deepcopy(issue))\n return filtered"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfiltering issues by labels specified in self. options. include_labels.", "response": "def include_issues_by_labels(self, all_issues):\n \"\"\"\n Include issues with labels, specified in self.options.include_labels.\n\n :param list(dict) all_issues: All issues.\n :rtype: list(dict)\n :return: Filtered issues.\n \"\"\"\n\n included_by_labels = self.filter_by_include_labels(all_issues)\n wo_labels = self.filter_wo_labels(all_issues)\n il = set([f[\"number\"] for f in included_by_labels])\n wl = set([w[\"number\"] for w in wo_labels])\n filtered_issues = []\n for issue in all_issues:\n if issue[\"number\"] in il or issue[\"number\"] in wl:\n filtered_issues.append(issue)\n return filtered_issues"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfilter all issues that don t have labels.", "response": "def filter_wo_labels(self, all_issues):\n \"\"\"\n Filter all issues that don't have a label.\n\n :rtype: list(dict)\n :return: Issues without labels.\n \"\"\"\n\n issues_wo_labels = []\n if not self.options.add_issues_wo_labels:\n for issue in all_issues:\n if not issue['labels']:\n issues_wo_labels.append(issue)\n return issues_wo_labels"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef filter_by_include_labels(self, issues):\n\n if not self.options.include_labels:\n return copy.deepcopy(issues)\n filtered_issues = []\n include_labels = set(self.options.include_labels)\n for issue in issues:\n labels = [label[\"name\"] for label in issue[\"labels\"]]\n if include_labels.intersection(labels):\n filtered_issues.append(issue)\n return filtered_issues", "response": "Filter issues to include only issues with labels specified in include_labels."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfiltering issues for include and exclude labels.", "response": "def filter_by_labels(self, all_issues, kind):\n \"\"\"\n Filter issues for include/exclude labels.\n\n :param list(dict) all_issues: All issues.\n :param str kind: Either \"issues\" or \"pull requests\".\n :rtype: list(dict)\n :return: Filtered issues.\n \"\"\"\n\n filtered_issues = self.include_issues_by_labels(all_issues)\n filtered = self.exclude_issues_by_labels(filtered_issues)\n if self.options.verbose > 1:\n print(\"\\tremaining {}: {}\".format(kind, len(filtered)))\n return filtered"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef fetch_and_filter_tags(self):\n\n self.all_tags = self.fetcher.get_all_tags()\n self.filtered_tags = self.get_filtered_tags(self.all_tags)\n self.fetch_tags_dates()", "response": "Fetch and filter tags and sort them in time order."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef sort_tags_by_date(self, tags):\n\n if self.options.verbose:\n print(\"Sorting tags...\")\n tags.sort(key=lambda x: self.get_time_of_tag(x))\n tags.reverse()\n return tags", "response": "Sort all tags by date."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the date and time for a tag.", "response": "def get_time_of_tag(self, tag):\n \"\"\"\n Get date and time for tag, fetching it if not already cached.\n\n :param dict tag: Tag to get the datetime for.\n :rtype: datetime\n :return: datetime for specified tag.\n \"\"\"\n\n if not tag:\n raise ChangelogGeneratorError(\"tag is nil\")\n\n name_of_tag = tag[\"name\"]\n time_for_name = self.tag_times_dict.get(name_of_tag, None)\n if time_for_name:\n return time_for_name\n else:\n time_string = self.fetcher.fetch_date_of_tag(tag)\n try:\n self.tag_times_dict[name_of_tag] = \\\n timestring_to_datetime(time_string)\n except UnicodeWarning:\n print(\"ERROR ERROR:\", tag)\n self.tag_times_dict[name_of_tag] = \\\n timestring_to_datetime(time_string)\n return self.tag_times_dict[name_of_tag]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef detect_link_tag_time(self, tag):\n\n # if tag is nil - set current time\n newer_tag_time = self.get_time_of_tag(tag) if tag \\\n else datetime.datetime.now()\n\n # if it's future release tag - set this value\n if tag[\"name\"] == self.options.unreleased_label \\\n and self.options.future_release:\n newer_tag_name = self.options.future_release\n newer_tag_link = self.options.future_release\n elif tag[\"name\"] is not self.options.unreleased_label :\n # put unreleased label if there is no name for the tag\n newer_tag_name = tag[\"name\"]\n newer_tag_link = newer_tag_name\n else:\n newer_tag_name = self.options.unreleased_label\n newer_tag_link = \"HEAD\"\n return [newer_tag_link, newer_tag_name, newer_tag_time]", "response": "Detect link name and time for specified tag."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntrying to detect the newest tag from self. options. base otherwise return a special value indicating the creation of the repo.", "response": "def version_of_first_item(self):\n \"\"\"\n Try to detect the newest tag from self.options.base, otherwise\n return a special value indicating the creation of the repo.\n\n :rtype: str\n :return: Tag name to use as 'oldest' tag. May be special value,\n indicating the creation of the repo.\n \"\"\"\n try:\n sections = read_changelog(self.options)\n return sections[0][\"version\"]\n except(IOError, TypeError):\n return self.get_temp_tag_for_repo_creation()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the name of the tag that is used for the creation of the repo. If not already cached returns the creation date of the repo and returns the special value indicating the creation of the repo.", "response": "def get_temp_tag_for_repo_creation(self):\n \"\"\"\n If not already cached, fetch the creation date of the repo, cache it\n and return the special value indicating the creation of the repo.\n\n :rtype: str\n :return: value indicating the creation\n \"\"\"\n tag_date = self.tag_times_dict.get(REPO_CREATED_TAG_NAME, None)\n if not tag_date:\n tag_name, tag_date = self.fetcher.fetch_repo_creation_date()\n self.tag_times_dict[tag_name] = timestring_to_datetime(tag_date)\n return REPO_CREATED_TAG_NAME"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_filtered_tags(self, all_tags):\n\n filtered_tags = self.filter_since_tag(all_tags)\n if self.options.between_tags:\n filtered_tags = self.filter_between_tags(filtered_tags)\n if self.options.due_tag:\n filtered_tags = self.filter_due_tag(filtered_tags)\n return self.filter_excluded_tags(filtered_tags)", "response": "Return tags after filtering tags in lists provided by the user - supplied option between - tags & -- exclude - tags\n option."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfilter tags according since_tag option.", "response": "def filter_since_tag(self, all_tags):\n \"\"\"\n Filter tags according since_tag option.\n\n :param list(dict) all_tags: All tags.\n :rtype: list(dict)\n :return: Filtered tags.\n \"\"\"\n\n tag = self.detect_since_tag()\n if not tag or tag == REPO_CREATED_TAG_NAME:\n return copy.deepcopy(all_tags)\n\n filtered_tags = []\n tag_names = [t[\"name\"] for t in all_tags]\n try:\n idx = tag_names.index(tag)\n except ValueError:\n self.warn_if_tag_not_found(tag, \"since-tag\")\n return copy.deepcopy(all_tags)\n\n since_tag = all_tags[idx]\n since_date = self.get_time_of_tag(since_tag)\n for t in all_tags:\n tag_date = self.get_time_of_tag(t)\n if since_date <= tag_date:\n filtered_tags.append(t)\n return filtered_tags"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfiltering tags according due_tag option.", "response": "def filter_due_tag(self, all_tags):\n \"\"\"\n Filter tags according due_tag option.\n\n :param list(dict) all_tags: Pre-filtered tags.\n :rtype: list(dict)\n :return: Filtered tags.\n \"\"\"\n\n filtered_tags = []\n tag = self.options.due_tag\n tag_names = [t[\"name\"] for t in all_tags]\n try:\n idx = tag_names.index(tag)\n except ValueError:\n self.warn_if_tag_not_found(tag, \"due-tag\")\n return copy.deepcopy(all_tags)\n\n due_tag = all_tags[idx]\n due_date = self.get_time_of_tag(due_tag)\n for t in all_tags:\n tag_date = self.get_time_of_tag(t)\n if tag_date <= due_date:\n filtered_tags.append(t)\n return filtered_tags"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfilter tags according between_tags option.", "response": "def filter_between_tags(self, all_tags):\n \"\"\"\n Filter tags according between_tags option.\n\n :param list(dict) all_tags: Pre-filtered tags.\n :rtype: list(dict)\n :return: Filtered tags.\n \"\"\"\n\n tag_names = [t[\"name\"] for t in all_tags]\n between_tags = []\n for tag in self.options.between_tags:\n try:\n idx = tag_names.index(tag)\n except ValueError:\n raise ChangelogGeneratorError(\n \"ERROR: can't find tag {0}, specified with \"\n \"--between-tags option.\".format(tag))\n between_tags.append(all_tags[idx])\n\n between_tags = self.sort_tags_by_date(between_tags)\n\n if len(between_tags) == 1:\n # if option --between-tags was only 1 tag given, duplicate it\n # to generate the changelog only for that one tag.\n between_tags.append(between_tags[0])\n\n older = self.get_time_of_tag(between_tags[1])\n newer = self.get_time_of_tag(between_tags[0])\n\n for tag in all_tags:\n if older < self.get_time_of_tag(tag) < newer:\n between_tags.append(tag)\n if older == newer:\n between_tags.pop(0)\n return between_tags"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfiltering tags according exclude_tags and exclude_tags_regex option.", "response": "def filter_excluded_tags(self, all_tags):\n \"\"\"\n Filter tags according exclude_tags and exclude_tags_regex option.\n\n :param list(dict) all_tags: Pre-filtered tags.\n :rtype: list(dict)\n :return: Filtered tags.\n \"\"\"\n filtered_tags = copy.deepcopy(all_tags)\n if self.options.exclude_tags:\n filtered_tags = self.apply_exclude_tags(filtered_tags)\n if self.options.exclude_tags_regex:\n filtered_tags = self.apply_exclude_tags_regex(filtered_tags)\n return filtered_tags"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfilter tags according exclude_tags_regex option.", "response": "def apply_exclude_tags_regex(self, all_tags):\n \"\"\"\n Filter tags according exclude_tags_regex option.\n\n :param list(dict) all_tags: Pre-filtered tags.\n :rtype: list(dict)\n :return: Filtered tags.\n \"\"\"\n filtered = []\n for tag in all_tags:\n if not re.match(self.options.exclude_tags_regex, tag[\"name\"]):\n filtered.append(tag)\n if len(all_tags) == len(filtered):\n self.warn_if_nonmatching_regex()\n return filtered"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef apply_exclude_tags(self, all_tags):\n filtered = copy.deepcopy(all_tags)\n for tag in all_tags:\n if tag[\"name\"] not in self.options.exclude_tags:\n self.warn_if_tag_not_found(tag, \"exclude-tags\")\n else:\n filtered.remove(tag)\n return filtered", "response": "Filter tags according exclude_tags option."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef random_letters(n):\n return ''.join(random.SystemRandom().choice(string.ascii_letters) for _ in range(n))", "response": "Generate a random string from a - zA - ZA - ZA"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef random_numbers(n):\n return ''.join(random.SystemRandom().choice(string.digits) for _ in range(n))", "response": "Generate a random string from 0 - 9"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dict_search(d, k, v):\n for i in range(len(d)):\n if d[i][k] == v:\n return i\n return None", "response": "Search dictionary list by key and value"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef dict_merge(a, b, k):\n c = a.copy()\n for j in range(len(b)):\n flag = False\n for i in range(len(c)):\n if c[i][k] == b[j][k]:\n c[i] = b[j].copy()\n flag = True\n if not flag:\n c.append(b[j].copy())\n return c", "response": "Merge two dictionary lists\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dict_sort(d, k):\n return sorted(d.copy(), key=lambda i: i[k])", "response": "Sort a dictionary list by key"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dict_top(d, k, n, reverse=False):\n h = list()\n for i in range(len(d)):\n heappush(h, (-d[i][k] if reverse else d[i][k], i))\n r = list()\n while len(r) < n and len(h) > 0:\n _, i = heappop(h)\n r.append(d[i].copy())\n return r", "response": "Return the top n elements of a dictionary list sorted by key"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef dict_flatten(d):\n if type(d) != dict:\n return d\n else:\n dd = dict()\n for key, value in d.items():\n if type(value) == dict:\n for k, v in value.items():\n dd[key + '_' + k] = dict_flatten(v)\n else:\n dd[key] = value\n return dd", "response": "Flatten nested dict keys to underscore - connected keys"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dictionary with the values of a dict with certain type", "response": "def dict_format_type(d, source, formatter, include_list=True):\n \"\"\"\n Replace the values of a dict with certain type to other values\n :param d: the dictionary\n :param source: the source type, e.g., int\n :param formatter: the formatter method, e.g., return the string format of an int\n :param include_list: whether list should be formatted, otherwise list will be considered as source type\n :return: formatted dictionary\n \"\"\"\n if not isinstance(d, dict):\n if isinstance(d, source):\n return formatter(d)\n else:\n return d\n else:\n dd = dict()\n for key, value in d.items():\n if include_list and isinstance(value, list):\n dd[key] = [dict_format_type(i, source, formatter) for i in value]\n elif isinstance(value, dict):\n dd[key] = dict_format_type(value, source, formatter)\n elif isinstance(value, source):\n dd[key] = formatter(value)\n else:\n dd[key] = value\n return dd"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef dict_as_tuple_list(d, as_list=False):\n dd = list()\n for k, v in d.items():\n dd.append([k, v] if as_list else (k, v))\n return dd", "response": "Format a dictionary to a list of tuples"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tuple_search(t, i, v):\n for e in t:\n if e[i] == v:\n return e\n return None", "response": "Search the tuple array by index and value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_comment_telemetry(text):\n parsed = {}\n match = re.findall(r\"^(.*?)\\|([!-{]{4,14})\\|(.*)$\", text)\n\n if match and len(match[0][1]) % 2 == 0:\n text, telemetry, post = match[0]\n text += post\n\n temp = [0] * 7\n for i in range(7):\n temp[i] = base91.to_decimal(telemetry[i*2:i*2+2])\n\n parsed.update({\n 'telemetry': {\n 'seq': temp[0],\n 'vals': temp[1:6]\n }\n })\n\n if temp[6] != '':\n parsed['telemetry'].update({\n 'bits': \"{0:08b}\".format(temp[6] & 0xFF)[::-1]\n })\n\n return (text, parsed)", "response": "Parse base91 telemetry found in comment field\n Returns parsed text and telemetry"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nparses an APRS packet and returns a dict with decoded data", "response": "def parse(packet):\n \"\"\"\n Parses an APRS packet and returns a dict with decoded data\n\n - All attributes are in metric units\n \"\"\"\n\n if not isinstance(packet, string_type_parse):\n raise TypeError(\"Expected packet to be str/unicode/bytes, got %s\", type(packet))\n\n if len(packet) == 0:\n raise ParseError(\"packet is empty\", packet)\n\n # attempt to detect encoding\n if isinstance(packet, bytes):\n packet = _unicode_packet(packet)\n\n packet = packet.rstrip(\"\\r\\n\")\n logger.debug(\"Parsing: %s\", packet)\n\n # split into head and body\n try:\n (head, body) = packet.split(':', 1)\n except:\n raise ParseError(\"packet has no body\", packet)\n\n if len(body) == 0:\n raise ParseError(\"packet body is empty\", packet)\n\n parsed = {\n 'raw': packet,\n }\n\n # parse head\n try:\n parsed.update(parse_header(head))\n except ParseError as msg:\n raise ParseError(str(msg), packet)\n\n # parse body\n packet_type = body[0]\n body = body[1:]\n\n if len(body) == 0 and packet_type != '>':\n raise ParseError(\"packet body is empty after packet type character\", packet)\n\n # attempt to parse the body\n try:\n _try_toparse_body(packet_type, body, parsed)\n\n # capture ParseErrors and attach the packet\n except (UnknownFormat, ParseError) as exp:\n exp.packet = packet\n raise\n\n # if we fail all attempts to parse, try beacon packet\n if 'format' not in parsed:\n if not re.match(r\"^(AIR.*|ALL.*|AP.*|BEACON|CQ.*|GPS.*|DF.*|DGPS.*|\"\n \"DRILL.*|DX.*|ID.*|JAVA.*|MAIL.*|MICE.*|QST.*|QTH.*|\"\n \"RTCM.*|SKY.*|SPACE.*|SPC.*|SYM.*|TEL.*|TEST.*|TLM.*|\"\n \"WX.*|ZIP.*|UIDIGI)$\", parsed['to']):\n raise UnknownFormat(\"format is not supported\", packet)\n\n parsed.update({\n 'format': 'beacon',\n 'text': packet_type + body,\n })\n\n logger.debug(\"Parsed ok.\")\n return parsed"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef to_decimal(text):\n\n if not isinstance(text, string_type):\n raise TypeError(\"expected str or unicode, %s given\" % type(text))\n\n if findall(r\"[\\x00-\\x20\\x7c-\\xff]\", text):\n raise ValueError(\"invalid character in sequence\")\n\n text = text.lstrip('!')\n decimal = 0\n length = len(text) - 1\n for i, char in enumerate(text):\n decimal += (ord(char) - 33) * (91 ** (length - i))\n\n return decimal if text != '' else 0", "response": "Takes a base91 char string and returns a decimal number."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef from_decimal(number, width=1):\n text = []\n\n if not isinstance(number, int_type):\n raise TypeError(\"Expected number to be int, got %s\", type(number))\n elif not isinstance(width, int_type):\n raise TypeError(\"Expected width to be int, got %s\", type(number))\n elif number < 0:\n raise ValueError(\"Expected number to be positive integer\")\n elif number > 0:\n max_n = ceil(log(number) / log(91))\n\n for n in _range(int(max_n), -1, -1):\n quotient, number = divmod(number, 91**n)\n text.append(chr(33 + quotient))\n\n return \"\".join(text).lstrip('!').rjust(max(1, width), '!')", "response": "Takes a decimal and returns base91 char string."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a passcode for a CALLSIGN.", "response": "def passcode(callsign):\n \"\"\"\n Takes a CALLSIGN and returns passcode\n \"\"\"\n assert isinstance(callsign, str)\n\n callsign = callsign.split('-')[0].upper()\n\n code = 0x73e2\n for i, char in enumerate(callsign):\n code ^= ord(char) << (8 if not i % 2 else 0)\n\n return code & 0x7fff"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_header(head):\n try:\n (fromcall, path) = head.split('>', 1)\n except:\n raise ParseError(\"invalid packet header\")\n\n if (not 1 <= len(fromcall) <= 9 or\n not re.findall(r\"^[a-z0-9]{0,9}(\\-[a-z0-9]{1,8})?$\", fromcall, re.I)):\n\n raise ParseError(\"fromcallsign is invalid\")\n\n path = path.split(',')\n\n if len(path[0]) == 0:\n raise ParseError(\"no tocallsign in header\")\n\n tocall = path[0]\n path = path[1:]\n\n validate_callsign(tocall, \"tocallsign\")\n\n for digi in path:\n if not re.findall(r\"^[A-Z0-9\\-]{1,9}\\*?$\", digi, re.I):\n raise ParseError(\"invalid callsign in path\")\n\n parsed = {\n 'from': fromcall,\n 'to': tocall,\n 'path': path,\n }\n\n viacall = \"\"\n if len(path) >= 2 and re.match(r\"^q..$\", path[-2]):\n viacall = path[-1]\n\n parsed.update({'via': viacall})\n\n return parsed", "response": "Parses the header part of a packet into a dict containing the keys fromcall tocall path via."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_filter(self, filter_text):\n self.filter = filter_text\n\n self.logger.info(\"Setting filter to: %s\", self.filter)\n\n if self._connected:\n self._sendall(\"#filter %s\\r\\n\" % self.filter)", "response": "Set a specified aprs - is filter for this connection"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_login(self, callsign, passwd=\"-1\", skip_login=False):\n self.__dict__.update(locals())", "response": "Set the login for this object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef connect(self, blocking=False, retry=30):\n\n if self._connected:\n return\n\n while True:\n try:\n self._connect()\n if not self.skip_login:\n self._send_login()\n break\n except (LoginError, ConnectionError):\n if not blocking:\n raise\n\n self.logger.info(\"Retrying connection is %d seconds.\" % retry)\n time.sleep(retry)", "response": "Initiate connection to APRS server and attempt to login and return the new APRS session identifier."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nclosing the socket with the current socket_id.", "response": "def close(self):\n \"\"\"\n Closes the socket\n Called internally when Exceptions are raised\n \"\"\"\n\n self._connected = False\n self.buf = b''\n\n if self.sock is not None:\n self.sock.close()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sendall(self, line):\n if isinstance(line, APRSPacket):\n line = str(line)\n elif not isinstance(line, string_type):\n raise TypeError(\"Expected line to be str or APRSPacket, got %s\", type(line))\n if not self._connected:\n raise ConnectionError(\"not connected\")\n\n if line == \"\":\n return\n\n line = line.rstrip(\"\\r\\n\") + \"\\r\\n\"\n\n try:\n self.sock.setblocking(1)\n self.sock.settimeout(5)\n self._sendall(line)\n except socket.error as exp:\n self.close()\n raise ConnectionError(str(exp))", "response": "Send a line or multiple lines sperapted by '\\\\ r\\\\ n'"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef consumer(self, callback, blocking=True, immortal=False, raw=False):\n\n if not self._connected:\n raise ConnectionError(\"not connected to a server\")\n\n line = b''\n\n while True:\n try:\n for line in self._socket_readlines(blocking):\n if line[0:1] != b'#':\n if raw:\n callback(line)\n else:\n callback(self._parse(line))\n else:\n self.logger.debug(\"Server: %s\", line.decode('utf8'))\n except ParseError as exp:\n self.logger.log(11, \"%s\\n Packet: %s\", exp.message, exp.packet)\n except UnknownFormat as exp:\n self.logger.log(9, \"%s\\n Packet: %s\", exp.message, exp.packet)\n except LoginError as exp:\n self.logger.error(\"%s: %s\", exp.__class__.__name__, exp.message)\n except (KeyboardInterrupt, SystemExit):\n raise\n except (ConnectionDrop, ConnectionError):\n self.close()\n\n if not immortal:\n raise\n else:\n self.connect(blocking=blocking)\n continue\n except GenericError:\n pass\n except StopIteration:\n break\n except:\n self.logger.error(\"APRS Packet: %s\", line)\n raise\n\n if not blocking:\n break", "response": "This function is used to consume a single file from the server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _connect(self):\n\n self.logger.info(\"Attempting connection to %s:%s\", self.server[0], self.server[1])\n\n try:\n self._open_socket()\n\n peer = self.sock.getpeername()\n\n self.logger.info(\"Connected to %s\", str(peer))\n\n # 5 second timeout to receive server banner\n self.sock.setblocking(1)\n self.sock.settimeout(5)\n\n self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)\n\n banner = self.sock.recv(512)\n if is_py3:\n banner = banner.decode('latin-1')\n\n if banner[0] == \"#\":\n self.logger.debug(\"Banner: %s\", banner.rstrip())\n else:\n raise ConnectionError(\"invalid banner from server\")\n\n except ConnectionError as e:\n self.logger.error(str(e))\n self.close()\n raise\n except (socket.error, socket.timeout) as e:\n self.close()\n\n self.logger.error(\"Socket error: %s\" % str(e))\n if str(e) == \"timed out\":\n raise ConnectionError(\"no banner from server\")\n else:\n raise ConnectionError(e)\n\n self._connected = True", "response": "Connects to the server and returns the ID of the server."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _send_login(self):\n login_str = \"user {0} pass {1} vers aprslib {3}{2}\\r\\n\"\n login_str = login_str.format(\n self.callsign,\n self.passwd,\n (\" filter \" + self.filter) if self.filter != \"\" else \"\",\n __version__\n )\n\n self.logger.info(\"Sending login information\")\n\n try:\n self._sendall(login_str)\n self.sock.settimeout(5)\n test = self.sock.recv(len(login_str) + 100)\n if is_py3:\n test = test.decode('latin-1')\n test = test.rstrip()\n\n self.logger.debug(\"Server: %s\", test)\n\n _, _, callsign, status, _ = test.split(' ', 4)\n\n if callsign == \"\":\n raise LoginError(\"Server responded with empty callsign???\")\n if callsign != self.callsign:\n raise LoginError(\"Server: %s\" % test)\n if status != \"verified,\" and self.passwd != \"-1\":\n raise LoginError(\"Password is incorrect\")\n\n if self.passwd == \"-1\":\n self.logger.info(\"Login successful (receive only)\")\n else:\n self.logger.info(\"Login successful\")\n\n except LoginError as e:\n self.logger.error(str(e))\n self.close()\n raise\n except:\n self.close()\n self.logger.error(\"Failed to login\")\n raise LoginError(\"Failed to login\")", "response": "Sends a login string to the server and returns the ID of the server."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a UUID to binary blob and reconstruct the value", "response": "def db_value(self, value):\n \"\"\"\n Convert UUID to binary blob\n \"\"\"\n\n # ensure we have a valid UUID\n if not isinstance(value, UUID):\n value = UUID(value)\n\n # reconstruct for optimal indexing\n parts = str(value).split(\"-\")\n reordered = ''.join([parts[2], parts[1], parts[0], parts[3], parts[4]])\n value = binascii.unhexlify(reordered)\n return super(OrderedUUIDField, self).db_value(value)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef python_value(self, value):\n value = super(OrderedUUIDField, self).python_value(value)\n u = binascii.b2a_hex(value)\n value = u[8:16] + u[4:8] + u[0:4] + u[16:22] + u[22:32]\n return UUID(value.decode())", "response": "Convert binary blob to UUID instance"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef db_value(self, value):\n value = self.transform_value(value)\n return self.hhash.encrypt(value, \n salt_size=self.salt_size, rounds=self.rounds)", "response": "Convert the python value for storage in the database."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef python_value(self, value):\n value = coerce_to_bytes(value)\n obj = HashValue(value)\n obj.field = self\n return obj", "response": "Convert the database value to a pythonic value."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nregistering model with app", "response": "def register(self, model_cls):\n \"\"\"Register model(s) with app\"\"\"\n assert issubclass(model_cls, peewee.Model)\n assert not hasattr(model_cls._meta, 'database_manager')\n if model_cls in self:\n raise RuntimeError(\"Model already registered\")\n self.append(model_cls)\n model_cls._meta.database = self.dbm\n return model_cls"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef disconnect(self):\n for name, connection in self.items():\n if not connection.is_closed():\n connection.close()", "response": "Disconnect from all databases"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_database(self, model):\n for router in self.routers:\n r = router.get_database(model)\n if r is not None:\n return r\n return self.get('default')", "response": "Find matching database router"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef to_cursor_ref(self):\n fields = self._meta.get_primary_keys()\n assert fields\n values = {field.name:self.__data__[field.name] for field in fields}\n return values", "response": "Returns dict of values to uniquely reference this item"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef paginate_query(self, query, count, offset=None, sort=None):\n assert isinstance(query, peewee.Query)\n assert isinstance(count, int)\n assert isinstance(offset, (str, int, type(None)))\n assert isinstance(sort, (list, set, tuple, type(None)))\n\n # ensure our model has a primary key\n fields = query.model._meta.get_primary_keys()\n if len(fields) == 0:\n raise peewee.ProgrammingError(\n 'Cannot apply pagination on model without primary key')\n\n # ensure our model doesn't use a compound primary key\n if len(fields) > 1:\n raise peewee.ProgrammingError(\n 'Cannot apply pagination on model with compound primary key')\n\n # apply offset\n if offset is not None:\n query = query.where(fields[0] >= offset)\n\n # do we need to apply sorting?\n order_bys = []\n if sort:\n for field, direction in sort:\n # does this field have a valid sort direction?\n if not isinstance(direction, str):\n raise ValueError(\"Invalid sort direction on field '{}'\".format(field))\n\n direction = direction.lower().strip()\n if direction not in ['asc', 'desc']:\n raise ValueError(\"Invalid sort direction on field '{}'\".format(field))\n\n # apply sorting\n order_by = peewee.SQL(field)\n order_by = getattr(order_by, direction)()\n order_bys += [order_by]\n\n # add primary key ordering after user sorting\n order_bys += [fields[0].asc()]\n\n # apply ordering and limits\n query = query.order_by(*order_bys)\n query = query.limit(count)\n return query", "response": "Apply pagination to query\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\napplies user specified filters to query", "response": "def apply_filters(self, query, filters):\n \"\"\"\n Apply user specified filters to query\n \"\"\"\n assert isinstance(query, peewee.Query)\n assert isinstance(filters, dict)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list(self, filters, cursor, count):\n assert isinstance(filters, dict), \"expected filters type 'dict'\"\n assert isinstance(cursor, dict), \"expected cursor type 'dict'\"\n\n # start with our base query\n query = self.get_query()\n assert isinstance(query, peewee.Query)\n\n # XXX: convert and apply user specified filters\n #filters = {field.name: cursor[field.name] for field in fields}\n #query.where(\n\n paginator = self.get_paginator()\n assert isinstance(paginator, Pagination)\n\n # always include an extra row for next cursor position\n count += 1\n\n # apply pagination to query\n pquery = paginator.filter_query(query, cursor, count)\n items = [ item for item in pquery ]\n\n # determine next cursor position\n next_item = items.pop(1)\n next_cursor = next_item.to_cursor_ref()\n\n '''\n # is this field allowed for sort?\n if field not in self.sort_fields:\n raise ValueError(\"Cannot sort on field '{}'\".format(field))\n '''\n\n return items, next_cursor", "response": "List items from query\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nretrieving items from query", "response": "def retrieve(self, cursor):\n \"\"\"\n Retrieve items from query\n \"\"\"\n assert isinstance(cursor, dict), \"expected cursor type 'dict'\"\n\n # look for record in query\n query = self.get_query()\n assert isinstance(query, peewee.Query)\n\n query\n return query.get(**cursor)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef regenerate_signing_key(self, secret_key=None, region=None,\n service=None, date=None):\n \"\"\"\n Regenerate the signing key for this instance. Store the new key in\n signing_key property.\n\n Take scope elements of the new key from the equivalent properties\n (region, service, date) of the current AWS4Auth instance. Scope\n elements can be overridden for the new key by supplying arguments to\n this function. If overrides are supplied update the current AWS4Auth\n instance's equivalent properties to match the new values.\n\n If secret_key is not specified use the value of the secret_key property\n of the current AWS4Auth instance's signing key. If the existing signing\n key is not storing its secret key (i.e. store_secret_key was set to\n False at instantiation) then raise a NoSecretKeyError and do not\n regenerate the key. In order to regenerate a key which is not storing\n its secret key, secret_key must be supplied to this function.\n\n Use the value of the existing key's store_secret_key property when\n generating the new key. If there is no existing key, then default\n to setting store_secret_key to True for new key.\n\n \"\"\"\n if secret_key is None and (self.signing_key is None or\n self.signing_key.secret_key is None):\n raise NoSecretKeyError\n\n secret_key = secret_key or self.signing_key.secret_key\n region = region or self.region\n service = service or self.service\n date = date or self.date\n if self.signing_key is None:\n store_secret_key = True\n else:\n store_secret_key = self.signing_key.store_secret_key\n\n self.signing_key = AWS4SigningKey(secret_key, region, service, date,\n store_secret_key)\n\n self.region = region\n self.service = service\n self.date = self.signing_key.date", "response": "Generate a new signing key for this instance."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_request_date(cls, req):\n date = None\n for header in ['x-amz-date', 'date']:\n if header not in req.headers:\n continue\n try:\n date_str = cls.parse_date(req.headers[header])\n except DateFormatError:\n continue\n try:\n date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()\n except ValueError:\n continue\n else:\n break\n\n return date", "response": "Try to pull a date from the request by looking first at the x - amz - date header and then the date header."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing a date string and return an ISO ISO object.", "response": "def parse_date(date_str):\n \"\"\"\n Check if date_str is in a recognised format and return an ISO\n yyyy-mm-dd format version if so. Raise DateFormatError if not.\n\n Recognised formats are:\n * RFC 7231 (e.g. Mon, 09 Sep 2011 23:36:00 GMT)\n * RFC 850 (e.g. Sunday, 06-Nov-94 08:49:37 GMT)\n * C time (e.g. Wed Dec 4 00:00:00 2002)\n * Amz-Date format (e.g. 20090325T010101Z)\n * ISO 8601 / RFC 3339 (e.g. 2009-03-25T10:11:12.13-01:00)\n\n date_str -- Str containing a date and optional time\n\n \"\"\"\n months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',\n 'sep', 'oct', 'nov', 'dec']\n formats = {\n # RFC 7231, e.g. 'Mon, 09 Sep 2011 23:36:00 GMT'\n r'^(?:\\w{3}, )?(\\d{2}) (\\w{3}) (\\d{4})\\D.*$':\n lambda m: '{}-{:02d}-{}'.format(\n m.group(3),\n months.index(m.group(2).lower())+1,\n m.group(1)),\n # RFC 850 (e.g. Sunday, 06-Nov-94 08:49:37 GMT)\n # assumes current century\n r'^\\w+day, (\\d{2})-(\\w{3})-(\\d{2})\\D.*$':\n lambda m: '{}{}-{:02d}-{}'.format(\n str(datetime.date.today().year)[:2],\n m.group(3),\n months.index(m.group(2).lower())+1,\n m.group(1)),\n # C time, e.g. 'Wed Dec 4 00:00:00 2002'\n r'^\\w{3} (\\w{3}) (\\d{1,2}) \\d{2}:\\d{2}:\\d{2} (\\d{4})$':\n lambda m: '{}-{:02d}-{:02d}'.format(\n m.group(3),\n months.index(m.group(1).lower())+1,\n int(m.group(2))),\n # x-amz-date format dates, e.g. 20100325T010101Z\n r'^(\\d{4})(\\d{2})(\\d{2})T\\d{6}Z$':\n lambda m: '{}-{}-{}'.format(*m.groups()),\n # ISO 8601 / RFC 3339, e.g. '2009-03-25T10:11:12.13-01:00'\n r'^(\\d{4}-\\d{2}-\\d{2})(?:[Tt].*)?$':\n lambda m: m.group(1),\n }\n\n out_date = None\n for regex, xform in formats.items():\n m = re.search(regex, date_str)\n if m:\n out_date = xform(m)\n break\n if out_date is None:\n raise DateFormatError\n else:\n return out_date"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling a request whose date doesn t match the signing key scope date.", "response": "def handle_date_mismatch(self, req):\n \"\"\"\n Handle a request whose date doesn't match the signing key scope date.\n\n This AWS4Auth class implementation regenerates the signing key. See\n StrictAWS4Auth class if you would prefer an exception to be raised.\n\n req -- a requests prepared request object\n\n \"\"\"\n req_datetime = self.get_request_date(req)\n new_key_date = req_datetime.strftime('%Y%m%d')\n self.regenerate_signing_key(date=new_key_date)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nencoding the body of the request to bytes and update content - type header if required.", "response": "def encode_body(req):\n \"\"\"\n Encode body of request to bytes and update content-type if required.\n\n If the body of req is Unicode then encode to the charset found in\n content-type header if present, otherwise UTF-8, or ASCII if\n content-type is application/x-www-form-urlencoded. If encoding to UTF-8\n then add charset to content-type. Modifies req directly, does not\n return a modified copy.\n\n req -- Requests PreparedRequest object\n\n \"\"\"\n if isinstance(req.body, text_type):\n split = req.headers.get('content-type', 'text/plain').split(';')\n if len(split) == 2:\n ct, cs = split\n cs = cs.split('=')[1]\n req.body = req.body.encode(cs)\n else:\n ct = split[0]\n if (ct == 'application/x-www-form-urlencoded' or\n 'x-amz-' in ct):\n req.body = req.body.encode()\n else:\n req.body = req.body.encode('utf-8')\n req.headers['content-type'] = ct + '; charset=utf-8'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating the AWS authentication Canonical Request string.", "response": "def get_canonical_request(self, req, cano_headers, signed_headers):\n \"\"\"\n Create the AWS authentication Canonical Request string.\n\n req -- Requests PreparedRequest object. Should already\n include an x-amz-content-sha256 header\n cano_headers -- Canonical Headers section of Canonical Request, as\n returned by get_canonical_headers()\n signed_headers -- Signed Headers, as returned by\n get_canonical_headers()\n\n \"\"\"\n url = urlparse(req.url)\n path = self.amz_cano_path(url.path)\n # AWS handles \"extreme\" querystrings differently to urlparse\n # (see post-vanilla-query-nonunreserved test in aws_testsuite)\n split = req.url.split('?', 1)\n qs = split[1] if len(split) == 2 else ''\n qs = self.amz_cano_querystring(qs)\n payload_hash = req.headers['x-amz-content-sha256']\n req_parts = [req.method.upper(), path, qs, cano_headers,\n signed_headers, payload_hash]\n cano_req = '\\n'.join(req_parts)\n return cano_req"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate the canonical headers section of the Canonical Request and the signed headers.", "response": "def get_canonical_headers(cls, req, include=None):\n \"\"\"\n Generate the Canonical Headers section of the Canonical Request.\n\n Return the Canonical Headers and the Signed Headers strs as a tuple\n (canonical_headers, signed_headers).\n\n req -- Requests PreparedRequest object\n include -- List of headers to include in the canonical and signed\n headers. It's primarily included to allow testing against\n specific examples from Amazon. If omitted or None it\n includes host, content-type and any header starting 'x-amz-'\n except for x-amz-client context, which appears to break\n mobile analytics auth if included. Except for the\n x-amz-client-context exclusion these defaults are per the\n AWS documentation.\n\n \"\"\"\n if include is None:\n include = cls.default_include_headers\n include = [x.lower() for x in include]\n headers = req.headers.copy()\n # Temporarily include the host header - AWS requires it to be included\n # in the signed headers, but Requests doesn't include it in a\n # PreparedRequest\n if 'host' not in headers:\n headers['host'] = urlparse(req.url).netloc.split(':')[0]\n # Aggregate for upper/lowercase header name collisions in header names,\n # AMZ requires values of colliding headers be concatenated into a\n # single header with lowercase name. Although this is not possible with\n # Requests, since it uses a case-insensitive dict to hold headers, this\n # is here just in case you duck type with a regular dict\n cano_headers_dict = {}\n for hdr, val in headers.items():\n hdr = hdr.strip().lower()\n val = cls.amz_norm_whitespace(val).strip()\n if (hdr in include or '*' in include or\n ('x-amz-*' in include and hdr.startswith('x-amz-') and not\n hdr == 'x-amz-client-context')):\n vals = cano_headers_dict.setdefault(hdr, [])\n vals.append(val)\n # Flatten cano_headers dict to string and generate signed_headers\n cano_headers = ''\n signed_headers_list = []\n for hdr in sorted(cano_headers_dict):\n vals = cano_headers_dict[hdr]\n val = ','.join(sorted(vals))\n cano_headers += '{}:{}\\n'.format(hdr, val)\n signed_headers_list.append(hdr)\n signed_headers = ';'.join(signed_headers_list)\n return (cano_headers, signed_headers)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates the AWS4 auth string to sign for the request.", "response": "def get_sig_string(req, cano_req, scope):\n \"\"\"\n Generate the AWS4 auth string to sign for the request.\n\n req -- Requests PreparedRequest object. This should already\n include an x-amz-date header.\n cano_req -- The Canonical Request, as returned by\n get_canonical_request()\n\n \"\"\"\n amz_date = req.headers['x-amz-date']\n hsh = hashlib.sha256(cano_req.encode())\n sig_items = ['AWS4-HMAC-SHA256', amz_date, scope, hsh.hexdigest()]\n sig_string = '\\n'.join(sig_items)\n return sig_string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate the canonical path as per AWS4 auth requirements.", "response": "def amz_cano_path(self, path):\n \"\"\"\n Generate the canonical path as per AWS4 auth requirements.\n\n Not documented anywhere, determined from aws4_testsuite examples,\n problem reports and testing against the live services.\n\n path -- request path\n\n \"\"\"\n safe_chars = '/~'\n qs = ''\n fixed_path = path\n if '?' in fixed_path:\n fixed_path, qs = fixed_path.split('?', 1)\n fixed_path = posixpath.normpath(fixed_path)\n fixed_path = re.sub('/+', '/', fixed_path)\n if path.endswith('/') and not fixed_path.endswith('/'):\n fixed_path += '/'\n full_path = fixed_path\n # If Python 2, switch to working entirely in str as quote() has problems\n # with Unicode\n if PY2:\n full_path = full_path.encode('utf-8')\n safe_chars = safe_chars.encode('utf-8')\n qs = qs.encode('utf-8')\n # S3 seems to require unquoting first. 'host' service is used in\n # amz_testsuite tests\n if self.service in ['s3', 'host']:\n full_path = unquote(full_path)\n full_path = quote(full_path, safe=safe_chars)\n if qs:\n qm = b'?' if PY2 else '?'\n full_path = qm.join((full_path, qs))\n if PY2:\n full_path = unicode(full_path)\n return full_path"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing and format a query string as per AWS4 auth requirements.", "response": "def amz_cano_querystring(qs):\n \"\"\"\n Parse and format querystring as per AWS4 auth requirements.\n\n Perform percent quoting as needed.\n\n qs -- querystring\n\n \"\"\"\n safe_qs_amz_chars = '&=+'\n safe_qs_unresvd = '-_.~'\n # If Python 2, switch to working entirely in str\n # as quote() has problems with Unicode\n if PY2:\n qs = qs.encode('utf-8')\n safe_qs_amz_chars = safe_qs_amz_chars.encode()\n safe_qs_unresvd = safe_qs_unresvd.encode()\n qs = unquote(qs)\n space = b' ' if PY2 else ' '\n qs = qs.split(space)[0]\n qs = quote(qs, safe=safe_qs_amz_chars)\n qs_items = {}\n for name, vals in parse_qs(qs, keep_blank_values=True).items():\n name = quote(name, safe=safe_qs_unresvd)\n vals = [quote(val, safe=safe_qs_unresvd) for val in vals]\n qs_items[name] = vals\n qs_strings = []\n for name, vals in qs_items.items():\n for val in vals:\n qs_strings.append('='.join([name, val]))\n qs = '&'.join(sorted(qs_strings))\n if PY2:\n qs = unicode(qs)\n return qs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating the signing key string as bytes.", "response": "def generate_key(cls, secret_key, region, service, date,\n intermediates=False):\n \"\"\"\n Generate the signing key string as bytes.\n\n If intermediate is set to True, returns a 4-tuple containing the key\n and the intermediate keys:\n\n ( signing_key, date_key, region_key, service_key )\n\n The intermediate keys can be used for testing against examples from\n Amazon.\n\n \"\"\"\n init_key = ('AWS4' + secret_key).encode('utf-8')\n date_key = cls.sign_sha256(init_key, date)\n region_key = cls.sign_sha256(date_key, region)\n service_key = cls.sign_sha256(region_key, service)\n key = cls.sign_sha256(service_key, 'aws4_request')\n if intermediates:\n return (key, date_key, region_key, service_key)\n else:\n return key"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef sign_sha256(key, msg):\n if isinstance(msg, text_type):\n msg = msg.encode('utf-8')\n return hmac.new(key, msg, hashlib.sha256).digest()", "response": "Generate a SHA256 HMAC of the message msg."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting a datetime object into a valid STIX timestamp string.", "response": "def _format_datetime(dttm):\n \"\"\"Convert a datetime object into a valid STIX timestamp string.\n\n 1. Convert to timezone-aware\n 2. Convert to UTC\n 3. Format in ISO format\n 4. Ensure correct precision\n a. Add subsecond value if non-zero and precision not defined\n 5. Add \"Z\"\n\n \"\"\"\n\n if dttm.tzinfo is None or dttm.tzinfo.utcoffset(dttm) is None:\n # dttm is timezone-naive; assume UTC\n zoned = pytz.utc.localize(dttm)\n else:\n zoned = dttm.astimezone(pytz.utc)\n ts = zoned.strftime(\"%Y-%m-%dT%H:%M:%S\")\n ms = zoned.strftime(\"%f\")\n precision = getattr(dttm, \"precision\", None)\n if precision == \"second\":\n pass # Already precise to the second\n elif precision == \"millisecond\":\n ts = ts + \".\" + ms[:3]\n elif zoned.microsecond > 0:\n ts = ts + \".\" + ms.rstrip(\"0\")\n return ts + \"Z\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _ensure_datetime_to_string(maybe_dttm):\n if isinstance(maybe_dttm, datetime.datetime):\n maybe_dttm = _format_datetime(maybe_dttm)\n return maybe_dttm", "response": "Ensure that maybe_dttm is a datetime instance and return a STIX - compliant\n string representation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting API keyword args to a mapping of URL query parameters.", "response": "def _filter_kwargs_to_query_params(filter_kwargs):\n \"\"\"\n Convert API keyword args to a mapping of URL query parameters. Except for\n \"added_after\", all keywords are mapped to match filters, i.e. to a query\n parameter of the form \"match[]\". \"added_after\" is left alone, since\n it's a special filter, as defined in the spec.\n\n Each value can be a single value or iterable of values. \"version\" and\n \"added_after\" get special treatment, since they are timestamp-valued:\n datetime.datetime instances are supported and automatically converted to\n STIX-compliant strings. Other than that, all values must be strings. None\n values, empty lists, etc are silently ignored.\n\n Args:\n filter_kwargs: The filter information, as a mapping.\n\n Returns:\n query_params (dict): The query parameter map, mapping strings to\n strings.\n\n \"\"\"\n query_params = {}\n for kwarg, arglist in six.iteritems(filter_kwargs):\n # If user passes an empty list, None, etc, silently skip?\n if not arglist:\n continue\n\n # force iterability, for the sake of code uniformity\n if not hasattr(arglist, \"__iter__\") or \\\n isinstance(arglist, six.string_types):\n arglist = arglist,\n\n if kwarg == \"version\":\n query_params[\"match[version]\"] = \",\".join(\n _ensure_datetime_to_string(val) for val in arglist\n )\n\n elif kwarg == \"added_after\":\n if len(arglist) > 1:\n raise InvalidArgumentsError(\"No more than one value for filter\"\n \" 'added_after' may be given\")\n\n query_params[\"added_after\"] = \",\".join(\n _ensure_datetime_to_string(val) for val in arglist\n )\n\n else:\n query_params[\"match[\" + kwarg + \"]\"] = \",\".join(arglist)\n\n return query_params"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting a response to a JSON object.", "response": "def _to_json(resp):\n \"\"\"\n Factors out some JSON parse code with error handling, to hopefully improve\n error messages.\n\n :param resp: A \"requests\" library response\n :return: Parsed JSON.\n :raises: InvalidJSONError If JSON parsing failed.\n \"\"\"\n try:\n return resp.json()\n except ValueError as e:\n # Maybe better to report the original request URL?\n six.raise_from(InvalidJSONError(\n \"Invalid JSON was received from \" + resp.request.url\n ), e)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nupdate the status information of the current resource", "response": "def refresh(self, accept=MEDIA_TYPE_TAXII_V20):\n \"\"\"Updates Status information\"\"\"\n response = self.__raw = self._conn.get(self.url,\n headers={\"Accept\": accept})\n self._populate_fields(**response)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef wait_until_final(self, poll_interval=1, timeout=60):\n start_time = time.time()\n elapsed = 0\n while (self.status != \"complete\" and\n (timeout <= 0 or elapsed < timeout)):\n time.sleep(poll_interval)\n self.refresh()\n elapsed = time.time() - start_time", "response": "This function will wait until the status service is complete."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates the status information. Raises errors for required properties. Raises errors for required properties.", "response": "def _validate_status(self):\n \"\"\"Validates Status information. Raises errors for required\n properties.\"\"\"\n if not self.id:\n msg = \"No 'id' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if not self.status:\n msg = \"No 'status' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self.total_count is None:\n msg = \"No 'total_count' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self.success_count is None:\n msg = \"No 'success_count' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self.failure_count is None:\n msg = \"No 'failure_count' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self.pending_count is None:\n msg = \"No 'pending_count' in Status for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if len(self.successes) != self.success_count:\n msg = \"Found successes={}, but success_count={} in status '{}'\"\n raise ValidationError(msg.format(self.successes,\n self.success_count,\n self.id))\n\n if len(self.pendings) != self.pending_count:\n msg = \"Found pendings={}, but pending_count={} in status '{}'\"\n raise ValidationError(msg.format(self.pendings,\n self.pending_count,\n self.id))\n\n if len(self.failures) != self.failure_count:\n msg = \"Found failures={}, but failure_count={} in status '{}'\"\n raise ValidationError(msg.format(self.failures,\n self.failure_count,\n self.id))\n\n if (self.success_count + self.pending_count + self.failure_count !=\n self.total_count):\n msg = (\"(success_count={} + pending_count={} + \"\n \"failure_count={}) != total_count={} in status '{}'\")\n raise ValidationError(msg.format(self.success_count,\n self.pending_count,\n self.failure_count,\n self.total_count,\n self.id))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _validate_collection(self):\n if not self._id:\n msg = \"No 'id' in Collection for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if not self._title:\n msg = \"No 'title' in Collection for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self._can_read is None:\n msg = \"No 'can_read' in Collection for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self._can_write is None:\n msg = \"No 'can_write' in Collection for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self._id not in self.url:\n msg = \"The collection '{}' does not match the url for queries '{}'\"\n raise ValidationError(msg.format(self._id, self.url))", "response": "Validates the collection information. Raises errors for required\n properties."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nimplement the Get Objects endpoint ( section 5. 3. 1", "response": "def get_objects(self, accept=MEDIA_TYPE_STIX_V20, **filter_kwargs):\n \"\"\"Implement the ``Get Objects`` endpoint (section 5.3)\"\"\"\n self._verify_can_read()\n query_params = _filter_kwargs_to_query_params(filter_kwargs)\n return self._conn.get(self.objects_url, headers={\"Accept\": accept},\n params=query_params)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_object(self, obj_id, version=None, accept=MEDIA_TYPE_STIX_V20):\n self._verify_can_read()\n url = self.objects_url + str(obj_id) + \"/\"\n query_params = None\n if version:\n query_params = _filter_kwargs_to_query_params({\"version\": version})\n return self._conn.get(url, headers={\"Accept\": accept},\n params=query_params)", "response": "Implement the Get an Object endpoint ( section 5. 1"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nimplement the Add Objects endpoint.", "response": "def add_objects(self, bundle, wait_for_completion=True, poll_interval=1,\n timeout=60, accept=MEDIA_TYPE_TAXII_V20,\n content_type=MEDIA_TYPE_STIX_V20):\n \"\"\"Implement the ``Add Objects`` endpoint (section 5.4)\n\n Add objects to the collection. This may be performed either\n synchronously or asynchronously. To add asynchronously, set\n wait_for_completion to False. If False, the latter two args are\n unused. If the caller wishes to monitor the status of the addition,\n it may do so in its own way. To add synchronously, set\n wait_for_completion to True, and optionally set the poll and timeout\n intervals. After initiating the addition, the caller will block,\n and the TAXII \"status\" service will be polled until the timeout\n expires, or the operation completes.\n\n Args:\n bundle: A STIX bundle with the objects to add (string, dict, binary)\n wait_for_completion (bool): Whether to wait for the add operation\n to complete before returning\n poll_interval (int): If waiting for completion, how often to poll\n the status service (seconds)\n timeout (int): If waiting for completion, how long to poll until\n giving up (seconds). Use <= 0 to wait forever\n accept (str): media type to include in the ``Accept:`` header.\n content_type (str): media type to include in the ``Content-Type:``\n header.\n\n Returns:\n If ``wait_for_completion`` is False, a Status object corresponding\n to the initial status data returned from the service, is returned.\n The status may not yet be complete at this point.\n\n If ``wait_for_completion`` is True, a Status object corresponding\n to the completed operation is returned if it didn't time out;\n otherwise a Status object corresponding to the most recent data\n obtained before the timeout, is returned.\n\n \"\"\"\n self._verify_can_write()\n\n headers = {\n \"Accept\": accept,\n \"Content-Type\": content_type,\n }\n\n if isinstance(bundle, dict):\n json_text = json.dumps(bundle, ensure_ascii=False)\n data = json_text.encode(\"utf-8\")\n\n elif isinstance(bundle, six.text_type):\n data = bundle.encode(\"utf-8\")\n\n elif isinstance(bundle, six.binary_type):\n data = bundle\n\n else:\n raise TypeError(\"Don't know how to handle type '{}'\".format(\n type(bundle).__name__))\n\n status_json = self._conn.post(self.objects_url, headers=headers,\n data=data)\n\n status_url = urlparse.urljoin(\n self.url,\n \"../../status/{}\".format(status_json[\"id\"])\n )\n\n status = Status(url=status_url, conn=self._conn,\n status_info=status_json)\n\n if not wait_for_completion or status.status == \"complete\":\n return status\n\n status.wait_until_final(poll_interval, timeout)\n\n return status"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_manifest(self, accept=MEDIA_TYPE_TAXII_V20, **filter_kwargs):\n self._verify_can_read()\n query_params = _filter_kwargs_to_query_params(filter_kwargs)\n return self._conn.get(self.url + \"manifest/\",\n headers={\"Accept\": accept},\n params=query_params)", "response": "Implement the Get Object Manifests endpoint ( section 5. 6. 1. 1"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _validate_api_root(self):\n if not self._title:\n msg = \"No 'title' in API Root for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if not self._versions:\n msg = \"No 'versions' in API Root for request '{}'\"\n raise ValidationError(msg.format(self.url))\n\n if self._max_content_length is None:\n msg = \"No 'max_content_length' in API Root for request '{}'\"\n raise ValidationError(msg.format(self.url))", "response": "Validates API Root information. Raises errors for required\n properties."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the API Root s information and list of Collections", "response": "def refresh(self, accept=MEDIA_TYPE_TAXII_V20):\n \"\"\"Update the API Root's information and list of Collections\"\"\"\n self.refresh_information(accept)\n self.refresh_collections(accept)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef refresh_information(self, accept=MEDIA_TYPE_TAXII_V20):\n response = self.__raw = self._conn.get(self.url,\n headers={\"Accept\": accept})\n self._populate_fields(**response)\n self._loaded_information = True", "response": "Refreshes the properties of this object based on the response from the Get API Root Information endpoint."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef refresh_collections(self, accept=MEDIA_TYPE_TAXII_V20):\n url = self.url + \"collections/\"\n response = self._conn.get(url, headers={\"Accept\": accept})\n\n self._collections = []\n for item in response.get(\"collections\", []): # optional\n collection_url = url + item[\"id\"] + \"/\"\n collection = Collection(collection_url, conn=self._conn,\n collection_info=item)\n self._collections.append(collection)\n\n self._loaded_collections = True", "response": "Updates the list of Collections contained by this API Root."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates server information. Raises errors for required properties.", "response": "def _validate_server(self):\n \"\"\"Validates server information. Raises errors for required properties.\n \"\"\"\n if not self._title:\n msg = \"No 'title' in Server Discovery for request '{}'\"\n raise ValidationError(msg.format(self.url))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates the Server information and API Roots", "response": "def refresh(self):\n \"\"\"Update the Server information and list of API Roots\"\"\"\n response = self.__raw = self._conn.get(self.url)\n self._populate_fields(**response)\n self._loaded = True"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks that the server is returning a valid Content - Type.", "response": "def valid_content_type(self, content_type, accept):\n \"\"\"Check that the server is returning a valid Content-Type\n\n Args:\n content_type (str): ``Content-Type:`` header value\n accept (str): media type to include in the ``Accept:`` header.\n\n \"\"\"\n accept_tokens = accept.replace(' ', '').split(';')\n content_type_tokens = content_type.replace(' ', '').split(';')\n\n return (\n all(elem in content_type_tokens for elem in accept_tokens) and\n (content_type_tokens[0] == 'application/vnd.oasis.taxii+json' or\n content_type_tokens[0] == 'application/vnd.oasis.stix+json')\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nperform an HTTP GET request.", "response": "def get(self, url, headers=None, params=None):\n \"\"\"Perform an HTTP GET, using the saved requests.Session and auth info.\n If \"Accept\" isn't one of the given headers, a default TAXII mime type is\n used. Regardless, the response type is checked against the accept\n header value, and an exception is raised if they don't match.\n\n Args:\n url (str): URL to retrieve\n headers (dict): Any other headers to be added to the request.\n params: dictionary or bytes to be sent in the query string for the\n request. (optional)\n\n \"\"\"\n\n merged_headers = self._merge_headers(headers)\n\n if \"Accept\" not in merged_headers:\n merged_headers[\"Accept\"] = MEDIA_TYPE_TAXII_V20\n accept = merged_headers[\"Accept\"]\n\n resp = self.session.get(url, headers=merged_headers, params=params)\n\n resp.raise_for_status()\n\n content_type = resp.headers[\"Content-Type\"]\n\n if not self.valid_content_type(content_type=content_type, accept=accept):\n msg = \"Unexpected Response. Got Content-Type: '{}' for Accept: '{}'\"\n raise TAXIIServiceException(msg.format(content_type, accept))\n\n return _to_json(resp)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend a JSON POST request to the given URL.", "response": "def post(self, url, headers=None, params=None, **kwargs):\n \"\"\"Send a JSON POST request with the given request headers, additional\n URL query parameters, and the given JSON in the request body. The\n extra query parameters are merged with any which already exist in the\n URL. The 'json' and 'data' parameters may not both be given.\n\n Args:\n url (str): URL to retrieve\n headers (dict): Any other headers to be added to the request.\n params: dictionary or bytes to be sent in the query string for the\n request. (optional)\n json: json to send in the body of the Request. This must be a\n JSON-serializable object. (optional)\n data: raw request body data. May be a dictionary, list of tuples,\n bytes, or file-like object to send in the body of the Request.\n (optional)\n \"\"\"\n\n if len(kwargs) > 1:\n raise InvalidArgumentsError(\"Too many extra args ({} > 1)\".format(\n len(kwargs)))\n\n if kwargs:\n kwarg = next(iter(kwargs))\n if kwarg not in (\"json\", \"data\"):\n raise InvalidArgumentsError(\"Invalid kwarg: \" + kwarg)\n\n resp = self.session.post(url, headers=headers, params=params, **kwargs)\n resp.raise_for_status()\n return _to_json(resp)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmerge headers from different sources together.", "response": "def _merge_headers(self, call_specific_headers):\n \"\"\"\n Merge headers from different sources together. Headers passed to the\n post/get methods have highest priority, then headers associated with\n the connection object itself have next priority.\n\n :param call_specific_headers: A header dict from the get/post call, or\n None (the default for those methods).\n :return: A key-case-insensitive MutableMapping object which contains\n the merged headers. (This doesn't actually return a dict.)\n \"\"\"\n\n # A case-insensitive mapping is necessary here so that there is\n # predictable behavior. If a plain dict were used, you'd get keys in\n # the merged dict which differ only in case. The requests library\n # would merge them internally, and it would be unpredictable which key\n # is chosen for the final set of headers. Another possible approach\n # would be to upper/lower-case everything, but this seemed easier. On\n # the other hand, I don't know if CaseInsensitiveDict is public API...?\n\n # First establish defaults\n merged_headers = requests.structures.CaseInsensitiveDict({\n \"User-Agent\": self.user_agent\n })\n\n # Then overlay with specifics from post/get methods\n if call_specific_headers:\n merged_headers.update(call_specific_headers)\n\n # Special \"User-Agent\" header check, to ensure one is always sent.\n # The call-specific overlay could have null'd out that header.\n if not merged_headers.get(\"User-Agent\"):\n merged_headers[\"User-Agent\"] = self.user_agent\n\n return merged_headers"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the amount of memory available for use in a list of items.", "response": "def total_memory():\n \"\"\" Returns the the amount of memory available for use.\n\n The memory is obtained from MemTotal entry in /proc/meminfo.\n \n Notes\n =====\n This function is not very useful and not very portable. \n\n \"\"\"\n with file('/proc/meminfo', 'r') as f:\n for line in f:\n words = line.split()\n if words[0].upper() == 'MEMTOTAL:':\n return int(words[1]) * 1024\n raise IOError('MemTotal unknown')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cpu_count():\n num = os.getenv(\"OMP_NUM_THREADS\")\n if num is None:\n num = os.getenv(\"PBS_NUM_PPN\")\n try:\n return int(num)\n except:\n return multiprocessing.cpu_count()", "response": "Returns the default number of slave processes to be spawned."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef empty_like(array, dtype=None):\n array = numpy.asarray(array)\n if dtype is None: \n dtype = array.dtype\n return anonymousmemmap(array.shape, dtype)", "response": "Create an anonymous shared memory array from the shape of array."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef full_like(array, value, dtype=None):\n shared = empty_like(array, dtype)\n shared[:] = value\n return shared", "response": "Create a shared memory array filled with value."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef full(shape, value, dtype='f8'):\n shared = empty(shape, dtype)\n shared[:] = value\n return shared", "response": "Create a shared memory array of given shape and type filled with value."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncopy an array to the shared memory.", "response": "def copy(a):\n \"\"\" Copy an array to the shared memory. \n\n Notes\n -----\n copy is not always necessary because the private memory is always copy-on-write.\n\n Use :code:`a = copy(a)` to immediately dereference the old 'a' on private memory\n \"\"\"\n shared = anonymousmemmap(a.shape, dtype=a.dtype)\n shared[:] = a[:]\n return shared"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get(self, Q):\n while self.Errors.empty():\n try:\n return Q.get(timeout=1)\n except queue.Empty:\n # check if the process group is dead\n if not self.is_alive():\n # todo : can be graceful, in which\n # case the last item shall have been\n # flushed to Q.\n try:\n return Q.get(timeout=0)\n except queue.Empty:\n raise StopProcessGroup\n else:\n continue\n else:\n raise StopProcessGroup", "response": "Protected get. Get an item from Q."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nwaiting and join the child process.", "response": "def wait(self):\n \"\"\" Wait and join the child process. \n The return value of the function call is returned.\n If any exception occurred it is wrapped and raised.\n \"\"\"\n e, r = self.result.get()\n self.slave.join()\n self.slave = None\n self.result = None\n if isinstance(e, Exception):\n raise SlaveException(e, r)\n return r"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef map(self, func, sequence, reduce=None, star=False, minlength=0):\n def realreduce(r):\n if reduce:\n if isinstance(r, tuple):\n return reduce(*r)\n else:\n return reduce(r)\n return r\n\n def realfunc(i):\n if star: return func(*i)\n else: return func(i)\n\n if len(sequence) <= 0 or self.np == 0 or get_debug():\n # Do this in serial\n self.local = lambda : None\n self.local.rank = 0\n\n rt = [realreduce(realfunc(i)) for i in sequence]\n\n self.local = None\n return rt\n\n # never use more than len(sequence) processes\n np = min([self.np, len(sequence)])\n\n Q = self.backend.QueueFactory(64)\n R = self.backend.QueueFactory(64)\n self.ordered.reset()\n\n pg = ProcessGroup(main=self._main, np=np,\n backend=self.backend,\n args=(Q, R, sequence, realfunc))\n\n pg.start()\n\n L = []\n N = []\n def feeder(pg, Q, N):\n # will fail silently if any error occurs.\n j = 0\n try:\n for i, work in enumerate(sequence):\n if not hasattr(sequence, '__getitem__'):\n pg.put(Q, (i, work))\n else:\n pg.put(Q, (i, ))\n j = j + 1\n N.append(j)\n\n for i in range(np):\n pg.put(Q, None)\n except StopProcessGroup:\n return\n finally:\n pass\n feeder = threading.Thread(None, feeder, args=(pg, Q, N))\n feeder.start() \n\n # we run fetcher on main thread to catch exceptions\n # raised by reduce \n count = 0\n try:\n while True:\n try:\n capsule = pg.get(R)\n except queue.Empty:\n continue\n except StopProcessGroup:\n raise pg.get_exception()\n capsule = capsule[0], realreduce(capsule[1])\n heapq.heappush(L, capsule)\n count = count + 1\n if len(N) > 0 and count == N[0]: \n # if finished feeding see if all\n # results have been obtained\n break\n rt = []\n# R.close()\n# R.join_thread()\n while len(L) > 0:\n rt.append(heapq.heappop(L)[1])\n pg.join()\n feeder.join()\n assert N[0] == len(rt)\n return rt\n except BaseException as e:\n pg.killall()\n pg.join()\n feeder.join()\n raise", "response": "Map a function over a sequence of items and return the reduced result."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef savetxt2(fname, X, delimiter=' ', newline='\\n', comment_character='#',\n header='', save_dtype=False, fmt={}):\n\n \"\"\" format of table header:\n\n # ID [type]:name(index) .... * number of items\n\n user's header is not prefixed by comment_character\n\n name of nested dtype elements are split by .\n \"\"\"\n prefixfmt = {}\n for key in fmt:\n prefixfmt[key] = fmt[key]\n\n olddtype = X.dtype\n newdtype = flatten_dtype(numpy.dtype([('', (X.dtype, X.shape[1:]))]))\n X = X.view(dtype=newdtype)\n dtype = X.dtype\n X = numpy.atleast_1d(X.squeeze())\n header2 = _mkheader(dtype)\n fmtstr = _mkfmtstr(dtype, prefixfmt, delimiter, _default_fmt)\n if hasattr(fname, 'write'):\n fh = fname\n cleanup = lambda : None\n else:\n fh = file(fname, 'w+')\n cleanup = lambda : fh.close()\n try:\n fh.write (header)\n if header[:-1] != newline:\n fh.write(newline)\n fh.write (comment_character)\n fh.write ('!')\n fh.write (header2)\n fh.write (delimiter)\n fh.write ('*%d' % len(X))\n fh.write(newline)\n if save_dtype:\n fh.write (comment_character)\n fh.write ('?')\n fh.write (base64.b64encode(pickle.dumps(olddtype)))\n fh.write (newline)\n for row in X:\n fh.write(fmtstr % tuple(row))\n fh.write(newline)\n\n if hasattr(fh, 'flush'):\n fh.flush()\n finally:\n cleanup()", "response": "Save a numpy array to a file in 2 - line format."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef loadtxt2(fname, dtype=None, delimiter=' ', newline='\\n', comment_character='#',\n skiplines=0):\n \"\"\" Known issues delimiter and newline is not respected. \n string quotation with space is broken.\n \"\"\"\n dtypert = [None, None, None]\n def preparedtype(dtype):\n dtypert[0] = dtype\n flatten = flatten_dtype(dtype)\n dtypert[1] = flatten\n dtypert[2] = numpy.dtype([('a', (numpy.int8,\n flatten.itemsize))])\n buf = numpy.empty((), dtype=dtypert[1])\n converters = [_default_conv[flatten[name].char] for name in flatten.names]\n return buf, converters, flatten.names\n\n def fileiter(fh):\n converters = []\n buf = None\n\n if dtype is not None:\n buf, converters, names = preparedtype(dtype)\n yield None\n\n for lineno, line in enumerate(fh):\n if lineno < skiplines: continue\n if line[0] in comment_character:\n if buf is None and line[1] == '?':\n ddtype = pickle.loads(base64.b64decode(line[2:]))\n buf, converters, names = preparedtype(ddtype)\n yield None\n continue\n for word, c, name in zip(line.split(), converters, names):\n buf[name] = c(word)\n buf2 = buf.copy().view(dtype=dtypert[2])\n yield buf2\n\n if isinstance(fname, basestring):\n fh = file(fh, 'r')\n cleanup = lambda : fh.close()\n else:\n fh = iter(fname)\n cleanup = lambda : None\n try:\n i = fileiter(fh)\n i.next()\n return numpy.fromiter(i, dtype=dtypert[2]).view(dtype=dtypert[0]) \n finally:\n cleanup()", "response": "Load a text file into a list of objects."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nunpack a structured data - type into a list of nested numpy. ndarrays.", "response": "def flatten_dtype(dtype, _next=None):\n \"\"\" Unpack a structured data-type. \"\"\"\n types = []\n if _next is None: \n _next = [0, '']\n primary = True\n else:\n primary = False\n\n prefix = _next[1]\n\n if dtype.names is None:\n for i in numpy.ndindex(dtype.shape):\n if dtype.base == dtype:\n types.append(('%s%s' % (prefix, simplerepr(i)), dtype))\n _next[0] += 1\n else:\n _next[1] = '%s%s' % (prefix, simplerepr(i))\n types.extend(flatten_dtype(dtype.base, _next))\n else:\n for field in dtype.names:\n typ_fields = dtype.fields[field]\n if len(prefix) > 0:\n _next[1] = prefix + '.' + field\n else:\n _next[1] = '' + field\n flat_dt = flatten_dtype(typ_fields[0], _next)\n types.extend(flat_dt)\n\n _next[1] = prefix\n if primary:\n return numpy.dtype(types)\n else:\n return types"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nkilling all slaves and reap the monitor", "response": "def kill_all(self):\n \"\"\"kill all slaves and reap the monitor \"\"\"\n for pid in self.children:\n try:\n os.kill(pid, signal.SIGTRAP)\n except OSError:\n continue\n self.join()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef forloop(self, range, ordered=False, schedule=('static', 1)):\n\n if isinstance(schedule, tuple):\n schedule, chunk = schedule\n else:\n chunk = None\n if schedule == 'static':\n return self._StaticForLoop(range, ordered, chunk)\n elif schedule == 'dynamic':\n return self._DynamicForLoop(range, ordered, chunk, guided=False)\n elif schedule == 'guided':\n return self._DynamicForLoop(range, ordered, chunk, guided=True)\n else:\n raise \"schedule unknown\"", "response": "forloop for the given time range"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures the master exit from Barrier", "response": "def abort(self):\n \"\"\" ensure the master exit from Barrier \"\"\"\n self.mutex.release()\n self.turnstile.release()\n self.mutex.release()\n self.turnstile2.release()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread at most n array items move the cursor.", "response": "def read(self, n):\n \"\"\" return at most n array items, move the cursor. \n \"\"\"\n while len(self.pool) < n:\n self.cur = self.files.next()\n self.pool = numpy.append(self.pool,\n self.fetch(self.cur), axis=0)\n\n rt = self.pool[:n]\n if n == len(self.pool):\n self.pool = self.fetch(None)\n else:\n self.pool = self.pool[n:]\n return rt"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef call(self, args, axis=0, out=None, chunksize=1024 * 1024, **kwargs):\n if self.altreduce is not None:\n ret = [None]\n else:\n if out is None :\n if self.outdtype is not None:\n dtype = self.outdtype\n else:\n try:\n dtype = numpy.result_type(*[args[i] for i in self.ins] * 2)\n except:\n dtype = None\n out = sharedmem.empty(\n numpy.broadcast(*[args[i] for i in self.ins] * 2).shape,\n dtype=dtype)\n if axis != 0:\n for i in self.ins:\n args[i] = numpy.rollaxis(args[i], axis)\n out = numpy.rollaxis(out, axis)\n size = numpy.max([len(args[i]) for i in self.ins])\n with sharedmem.MapReduce() as pool:\n def work(i):\n sl = slice(i, i+chunksize)\n myargs = args[:]\n for j in self.ins:\n try: \n tmp = myargs[j][sl]\n a, b, c = sl.indices(len(args[j]))\n myargs[j] = tmp\n except Exception as e:\n print tmp\n print j, e\n pass\n if b == a: return None\n rt = self.ufunc(*myargs, **kwargs)\n if self.altreduce is not None:\n return rt\n else:\n out[sl] = rt\n def reduce(rt):\n if self.altreduce is None:\n return\n if ret[0] is None:\n ret[0] = rt\n elif rt is not None:\n ret[0] = self.altreduce(ret[0], rt)\n\n pool.map(work, range(0, size, chunksize), reduce=reduce)\n\n if self.altreduce is None:\n if axis != 0:\n out = numpy.rollaxis(out, 0, axis + 1)\n return out \n else:\n return ret[0]", "response": "Call the ufunc with the given arguments and return the result."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef adapt(cls, source, template):\n if not isinstance(template, packarray):\n raise TypeError('template must be a packarray')\n return cls(source, template.start, template.end)", "response": "adapt source to a packarray according to the layout of template"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparallels argsort for a set of integers", "response": "def argsort(data, out=None, chunksize=None, \n baseargsort=None, \n argmerge=None, np=None):\n \"\"\"\n parallel argsort, like numpy.argsort\n\n use sizeof(intp) * len(data) as scratch space\n\n use baseargsort for serial sort \n ind = baseargsort(data)\n\n use argmerge to merge\n def argmerge(data, A, B, out):\n ensure data[out] is sorted\n and out[:] = A join B\n\n TODO: shall try to use the inplace merge mentioned in \n http://keithschwarz.com/interesting/code/?dir=inplace-merge.\n \"\"\"\n if baseargsort is None:\n baseargsort = lambda x:x.argsort()\n\n if argmerge is None:\n argmerge = default_argmerge\n\n if chunksize is None:\n chunksize = 1024 * 1024 * 16\n\n if out is None:\n arg1 = numpy.empty(len(data), dtype='intp')\n out = arg1\n else:\n assert out.dtype == numpy.dtype('intp')\n assert len(out) == len(data)\n arg1 = out\n\n if np is None:\n np = sharedmem.cpu_count()\n\n if np <= 1 or len(data) < chunksize: \n out[:] = baseargsort(data)\n return out\n\n CHK = [slice(i, i + chunksize) for i in range(0, len(data), chunksize)]\n DUMMY = slice(len(data), len(data))\n if len(CHK) % 2: CHK.append(DUMMY)\n with sharedmem.TPool() as pool:\n def work(i):\n C = CHK[i]\n start, stop, step = C.indices(len(data))\n arg1[C] = baseargsort(data[C])\n arg1[C] += start\n pool.map(work, range(len(CHK)))\n \n arg2 = numpy.empty_like(arg1)\n \n flip = 0\n while len(CHK) > 1:\n with sharedmem.TPool() as pool:\n def work(i):\n C1 = CHK[i]\n C2 = CHK[i+1]\n start1, stop1, step1 = C1.indices(len(data))\n start2, stop2, step2 = C2.indices(len(data))\n # print 'argmerge', start1, stop1, start2, stop2\n assert start2 == stop1\n argmerge(data, arg1[C1], arg1[C2], arg2[start1:stop2])\n return slice(start1, stop2)\n CHK = pool.map(work, range(0, len(CHK), 2))\n arg1, arg2 = arg2, arg1\n flip = flip + 1\n if len(CHK) == 1: break\n if len(CHK) % 2: CHK.append(DUMMY)\n if flip % 2 != 0:\n # only even flips out ends up pointing to arg2 and needs to be\n # copied\n out[:] = arg1\n return out"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a random day of week name.", "response": "def day_of_week(abbr=False):\n \"\"\"Return a random (abbreviated if `abbr`) day of week name.\"\"\"\n if abbr:\n return random.choice(DAYS_ABBR)\n else:\n return random.choice(DAYS)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a random month name or month number if numerical.", "response": "def month(abbr=False, numerical=False):\n \"\"\"Return a random (abbreviated if `abbr`) month name or month number if\n `numerical`.\n \"\"\"\n if numerical:\n return random.randint(1, 12)\n else:\n if abbr:\n return random.choice(MONTHS_ABBR)\n else:\n return random.choice(MONTHS)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef year(past=False, min_delta=0, max_delta=20):\n return dt.date.today().year + _delta(past, min_delta, max_delta)", "response": "Return a random year."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a random dt. date object. Delta args are days.", "response": "def date(past=False, min_delta=0, max_delta=20):\n \"\"\"Return a random `dt.date` object. Delta args are days.\"\"\"\n timedelta = dt.timedelta(days=_delta(past, min_delta, max_delta))\n return dt.date.today() + timedelta"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nloads a dictionary file and return its contents as an array of strings.", "response": "def get_dictionary(dict_name):\n \"\"\"\n Load a dictionary file ``dict_name`` (if it's not cached) and return its\n contents as an array of strings.\n \"\"\"\n global dictionaries_cache\n\n if dict_name not in dictionaries_cache:\n try:\n dictionary_file = codecs.open(\n join(DICTIONARIES_PATH, dict_name), 'r', 'utf-8'\n )\n except IOError:\n None\n else:\n dictionaries_cache[dict_name] = dictionary_file.readlines()\n dictionary_file.close()\n\n return dictionaries_cache[dict_name]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a check digit of the given credit card number.", "response": "def check_digit(num):\n \"\"\"Return a check digit of the given credit card number.\n\n Check digit calculated using Luhn algorithm (\"modulus 10\")\n See: http://www.darkcoding.net/credit-card/luhn-formula/\n \"\"\"\n sum = 0\n\n # drop last digit, then reverse the number\n digits = str(num)[:-1][::-1]\n\n for i, n in enumerate(digits):\n # select all digits at odd positions starting from 1\n if (i + 1) % 2 != 0:\n digit = int(n) * 2\n if digit > 9:\n sum += (digit - 9)\n else:\n sum += digit\n else:\n sum += int(n)\n\n return ((divmod(sum, 10)[0] + 1) * 10 - sum) % 10"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a random credit card number.", "response": "def number(type=None, length=None, prefixes=None):\n \"\"\"\n Return a random credit card number.\n\n :param type: credit card type. Defaults to a random selection.\n :param length: length of the credit card number.\n Defaults to the length for the selected card type.\n :param prefixes: allowed prefixes for the card number.\n Defaults to prefixes for the selected card type.\n :return: credit card randomly generated number (int)\n \"\"\"\n # select credit card type\n if type and type in CARDS:\n card = type\n else:\n card = random.choice(list(CARDS.keys()))\n\n # select a credit card number's prefix\n if not prefixes:\n prefixes = CARDS[card]['prefixes']\n prefix = random.choice(prefixes)\n\n # select length of the credit card number, if it's not set\n if not length:\n length = CARDS[card]['length']\n\n # generate all digits but the last one\n result = str(prefix)\n\n for d in range(length - len(str(prefix))):\n result += str(basic.number())\n\n last_digit = check_digit(int(result))\n\n return int(result[:-1] + str(last_digit))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a random street number.", "response": "def street_number():\n \"\"\"Return a random street number.\"\"\"\n length = int(random.choice(string.digits[1:6]))\n return ''.join(random.sample(string.digits, length))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a random ZIP code either in a##### or a##### -#### format.", "response": "def zip_code():\n \"\"\"Return a random ZIP code, either in `#####` or `#####-####` format.\"\"\"\n format = '#####'\n if random.random() >= 0.5:\n format = '#####-####'\n\n result = ''\n for item in format:\n if item == '#':\n result += str(random.randint(0, 9))\n else:\n result += item\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a random phone number in the specified format.", "response": "def phone():\n \"\"\"Return a random phone number in `#-(###)###-####` format.\"\"\"\n format = '#-(###)###-####'\n\n result = ''\n for item in format:\n if item == '#':\n result += str(random.randint(0, 9))\n else:\n result += item\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a random job title.", "response": "def job_title():\n \"\"\"Return a random job title.\"\"\"\n result = random.choice(get_dictionary('job_titles')).strip()\n result = result.replace('#{N}', job_title_suffix())\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a random email text.", "response": "def body(quantity=2, separator='\\n\\n', wrap_start='', wrap_end='',\n html=False, sentences_quantity=3, as_list=False):\n \"\"\"Return a random email text.\"\"\"\n return lorem_ipsum.paragraphs(quantity=quantity, separator=separator,\n wrap_start=wrap_start, wrap_end=wrap_end,\n html=html,\n sentences_quantity=sentences_quantity,\n as_list=as_list)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef money(min=0, max=10):\n value = random.choice(range(min * 100, max * 100))\n return \"%1.2f\" % (float(value) / 100)", "response": "Return a str of decimal with two digits after a decimal mark."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef title(words_quantity=4):\n result = words(quantity=words_quantity)\n result += random.choice('?.!')\n return result.capitalize()", "response": "Return a random sentence to be used as e - mail subject."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a random paragraph.", "response": "def paragraph(separator='\\n\\n', wrap_start='', wrap_end='',\n html=False, sentences_quantity=3):\n \"\"\"Return a random paragraph.\"\"\"\n return paragraphs(quantity=1, separator=separator, wrap_start=wrap_start,\n wrap_end=wrap_end, html=html,\n sentences_quantity=sentences_quantity)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _to_lower_alpha_only(s):\n s = re.sub(r'\\n', ' ', s.lower())\n return re.sub(r'[^a-z\\s]', '', s)", "response": "Return a lowercased string with non alphabetic chars removed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef text(what=\"sentence\", *args, **kwargs):\n\n if what == \"character\":\n return character(*args, **kwargs)\n elif what == \"characters\":\n return characters(*args, **kwargs)\n elif what == \"word\":\n return word(*args, **kwargs)\n elif what == \"words\":\n return words(*args, **kwargs)\n elif what == \"sentence\":\n return sentence(*args, **kwargs)\n elif what == \"sentences\":\n return sentences(*args, **kwargs)\n elif what == \"paragraph\":\n return paragraph(*args, **kwargs)\n elif what == \"paragraphs\":\n return paragraphs(*args, **kwargs)\n elif what == \"title\":\n return title(*args, **kwargs)\n else:\n raise NameError('No such method')", "response": "An aggregator for all above defined public methods."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a random user name.", "response": "def user_name(with_num=False):\n \"\"\"Return a random user name.\n\n Basically it's lowercased result of\n :py:func:`~forgery_py.forgery.name.first_name()` with a number appended\n if `with_num`.\n \"\"\"\n result = first_name()\n if with_num:\n result += str(random.randint(63, 94))\n\n return result.lower()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef domain_name():\n result = random.choice(get_dictionary('company_names')).strip()\n result += '.' + top_level_domain()\n\n return result.lower()", "response": "Return a random domain name."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef email_address(user=None):\n if not user:\n user = user_name()\n else:\n user = user.strip().replace(' ', '_').lower()\n\n return user + '@' + domain_name()", "response": "Return a random e - mail address in a hopefully imaginary domain."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef account_number():\n account = [random.randint(1, 9) for _ in range(20)]\n return \"\".join(map(str, account))", "response": "Return a random bank account number."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef bik():\n return '04' + \\\n ''.join([str(random.randint(1, 9)) for _ in range(5)]) + \\\n str(random.randint(0, 49) + 50)", "response": "Return a random bank identification number."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef legal_inn():\n mask = [2, 4, 10, 3, 5, 9, 4, 6, 8]\n inn = [random.randint(1, 9) for _ in range(10)]\n weighted = [v * mask[i] for i, v in enumerate(inn[:-1])]\n inn[9] = sum(weighted) % 11 % 10\n return \"\".join(map(str, inn))", "response": "Return a random taxation ID number for a company."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a random government registration ID for a company.", "response": "def legal_ogrn():\n \"\"\"Return a random government registration ID for a company.\"\"\"\n ogrn = \"\".join(map(str, [random.randint(1, 9) for _ in range(12)]))\n ogrn += str((int(ogrn) % 11 % 10))\n return ogrn"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef person_inn():\n mask11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]\n mask12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]\n inn = [random.randint(1, 9) for _ in range(12)]\n\n # get the 11th digit of the INN\n weighted11 = [v * mask11[i] for i, v in enumerate(inn[:-2])]\n inn[10] = sum(weighted11) % 11 % 10\n\n # get the 12th digit of the INN\n weighted12 = [v * mask12[i] for i, v in enumerate(inn[:-1])]\n inn[11] = sum(weighted12) % 11 % 10\n\n return \"\".join(map(str, inn))", "response": "Return a random taxation ID number for a natural person."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning SHA1 hexdigest of a password optionally salted with a string.", "response": "def encrypt(password='password', salt=None):\n \"\"\"\n Return SHA1 hexdigest of a password (optionally salted with a string).\n\n\n \"\"\"\n if not salt:\n salt = str(datetime.utcnow())\n\n try:\n # available for python 2.7.8 and python 3.4+\n dk = hashlib.pbkdf2_hmac('sha1', password.encode(), salt.encode(), 100000)\n hexdigest = binascii.hexlify(dk).decode('utf-8')\n except AttributeError:\n # see https://pymotw.com/2/hashlib/\n # see https://docs.python.org/release/2.5/lib/module-hashlib.html\n dk = hashlib.sha1()\n dk.update(password.encode() + salt.encode())\n hexdigest = dk.hexdigest()\n return hexdigest"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a random string for use as a password.", "response": "def password(at_least=6, at_most=12, lowercase=True,\n uppercase=True, digits=True, spaces=False, punctuation=False):\n \"\"\"Return a random string for use as a password.\"\"\"\n return text(at_least=at_least, at_most=at_most, lowercase=lowercase,\n uppercase=uppercase, digits=digits, spaces=spaces,\n punctuation=punctuation)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef forwards(self, orm):\n \"Write your forwards methods here.\"\n # Note: Don't use \"from appname.models import ModelName\". \n # Use orm.ModelName to refer to models in this application,\n # and orm['appname.ModelName'] for models in other applications.\n print(\"Updating: JednostkaAdministracyjna\")\n ja_akt_stan=orm.JednostkaAdministracyjna.objects.all().aggregate(Max('stan_na'))['stan_na__max']\n orm.JednostkaAdministracyjna.objects.filter(stan_na__exact=ja_akt_stan).update(aktywny=True)\n orm.JednostkaAdministracyjna.objects.exclude(stan_na__exact=ja_akt_stan).update(aktywny=False)\n\n print(\"Updating: Miejscowosc\")\n m_akt_stan=orm.Miejscowosc.objects.all().aggregate(Max('stan_na'))['stan_na__max']\n orm.Miejscowosc.objects.filter(stan_na__exact=m_akt_stan).update(aktywny=True)\n orm.Miejscowosc.objects.exclude(stan_na__exact=m_akt_stan).update(aktywny=False)\n\n print(\"Updating: RodzajMiejsowosci\")\n rm_akt_stan=orm.RodzajMiejsowosci.objects.all().aggregate(Max('stan_na'))['stan_na__max']\n orm.RodzajMiejsowosci.objects.filter(stan_na__exact=rm_akt_stan).update(aktywny=True)\n orm.RodzajMiejsowosci.objects.exclude(stan_na__exact=rm_akt_stan).update(aktywny=False)\n\n print(\"Updating: Ulica\")\n u_akt_stan=orm.Ulica.objects.all().aggregate(Max('stan_na'))['stan_na__max']\n orm.Ulica.objects.filter(stan_na__exact=u_akt_stan).update(aktywny=True)\n orm.Ulica.objects.exclude(stan_na__exact=u_akt_stan).update(aktywny=False)", "response": "Write your forwards methods here."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef forwards(self, orm):\n \"Write your forwards methods here.\"\n # Note: Don't use \"from appname.models import ModelName\". \n # Use orm.ModelName to refer to models in this application,\n # and orm['appname.ModelName'] for models in other applications.\n LEN_TYPE = {\n 7: 'GMI',\n 4: 'POW',\n 2: 'WOJ',\n }\n for ja in orm.JednostkaAdministracyjna.objects.all():\n ja.typ = LEN_TYPE[len(ja.id)]\n ja.save()", "response": "Write your forwards methods here."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef case(*, to, **kwargs):\n\n if len(kwargs) != 1:\n raise ValueError(\"expect exactly one source string argument\")\n\n [(typ, string)] = kwargs.items()\n\n types = {'pascal', 'camel', 'snake', 'constant'}\n if typ not in types:\n raise ValueError(f\"source string keyword must be one of {types}\")\n if to not in types:\n raise ValueError(f\"\\\"to\\\" argument must be one of {types}\")\n\n def pascal_iter(string):\n yield from (m.group(0) for m in re.finditer(r'[A-Z][a-z0-9]*|[a-z0-9]+', string))\n\n def snake_iter(string):\n yield from (m.group(2) for m in re.finditer(r'(^|_)([A-Za-z0-9]+)', string))\n\n inputs = {\n 'pascal': pascal_iter,\n 'camel': pascal_iter,\n 'snake': snake_iter,\n 'constant': snake_iter,\n }\n\n def out_fun(sep, case=None, case_fst=None):\n if case is None:\n case = lambda x: x\n if case_fst is None:\n case_fst = case\n return lambda tokens: sep.join(case_fst(token) if i == 0 else case(token) for i, token in enumerate(tokens))\n\n outputs = {\n 'pascal': out_fun('', str.capitalize),\n 'camel': out_fun('', str.capitalize, str.lower),\n 'snake': out_fun('_', str.lower),\n 'constant': out_fun('_', str.upper),\n }\n\n tokens = inputs[typ](string)\n return outputs[to](tokens)", "response": "Converts an identifier from one case type to another."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef read_stream(schema, stream, *, buffer_size=io.DEFAULT_BUFFER_SIZE):\n reader = _lancaster.Reader(schema)\n buf = stream.read(buffer_size)\n remainder = b''\n while len(buf) > 0:\n values, n = reader.read_seq(buf)\n yield from values\n remainder = buf[n:]\n buf = stream.read(buffer_size)\n if len(buf) > 0 and len(remainder) > 0:\n ba = bytearray()\n ba.extend(remainder)\n ba.extend(buf)\n buf = memoryview(ba).tobytes()\n if len(remainder) > 0:\n raise EOFError('{} bytes remaining but could not continue reading '\n 'from stream'.format(len(remainder)))", "response": "Using a schema deserialize a stream of consecutive Avro values."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parse_user_defined_metric_classes(config_obj, metric_classes):\n user_defined_metric_list = config_obj.get('GLOBAL', 'user_defined_metrics').split()\n for udm_string in user_defined_metric_list:\n try:\n metric_name, metric_class_name, metric_file = udm_string.split(':')\n except ValueError:\n logger.error('Bad user defined metric specified')\n continue\n module_name = os.path.splitext(os.path.basename(metric_file))[0]\n try:\n new_module = imp.load_source(module_name, metric_file)\n new_class = getattr(new_module, metric_class_name)\n if metric_name in metric_classes.keys():\n logger.warn('Overriding pre-defined metric class definition for ', metric_name)\n metric_classes[metric_name] = new_class\n except ImportError:\n logger.error('Something wrong with importing a user defined metric class. Skipping metric: ', metric_name)\n continue", "response": "Parse the user defined metric class information\n Parse the user defined metric class information\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if a given string is in the correct URL format or not", "response": "def is_valid_url(url):\n \"\"\"\n Check if a given string is in the correct URL format or not\n\n :param str url:\n :return: True or False\n \"\"\"\n regex = re.compile(r'^(?:http|ftp)s?://'\n r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+(?:[A-Z]{2,6}\\.?|[A-Z0-9-]{2,}\\.?)|'\n r'localhost|'\n r'\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})'\n r'(?::\\d+)?'\n r'(?:/?|[/?]\\S+)$', re.IGNORECASE)\n if regex.match(url):\n logger.info(\"URL given as config\")\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef download_file(url):\n try:\n (local_file, headers) = urllib.urlretrieve(url)\n except:\n sys.exit(\"ERROR: Problem downloading config file. Please check the URL (\" + url + \"). Exiting...\")\n return local_file", "response": "Download a file pointed to by url to a temp file on local disk"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking the validity of metric_name in config", "response": "def is_valid_metric_name(metric_name):\n \"\"\"\n check the validity of metric_name in config; the metric_name will be used for creation of sub-dir, so only contains: alphabet, digits , '.', '-' and '_'\n :param str metric_name: metric_name\n :return: True if valid\n \"\"\"\n reg = re.compile('^[a-zA-Z0-9\\.\\-\\_]+$')\n if reg.match(metric_name) and not metric_name.startswith('.'):\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_run_time_period(run_steps):\n init_ts_start = get_standardized_timestamp('now', None)\n ts_start = init_ts_start\n ts_end = '0'\n for run_step in run_steps:\n if run_step.ts_start and run_step.ts_end:\n if run_step.ts_start < ts_start:\n ts_start = run_step.ts_start\n if run_step.ts_end > ts_end:\n ts_end = run_step.ts_end\n if ts_end == '0':\n ts_end = None\n if ts_start == init_ts_start:\n ts_start = None\n logger.info('get_run_time_period range returned ' + str(ts_start) + ' to ' + str(ts_end))\n return ts_start, ts_end", "response": "This method finds the time range which covers all the Run_Steps\n \u00bb and \u00bb runs."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextracts rule strings from a section Returns a tuple of the rule strings and kwargs", "response": "def get_rule_strings(config_obj, section):\n \"\"\"\n Extract rule strings from a section\n :param config_obj: ConfigParser object\n :param section: Section name\n :return: the rule strings\n \"\"\"\n rule_strings = {}\n kwargs = dict(config_obj.items(section))\n for key in kwargs.keys():\n if key.endswith('.sla'):\n rule_strings[key.replace('.sla', '')] = kwargs[key]\n del kwargs[key]\n return rule_strings, kwargs"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextracts SLA rules from diff config file.", "response": "def extract_diff_sla_from_config_file(obj, options_file):\n \"\"\"\n Helper function to parse diff config file, which contains SLA rules for diff comparisons\n \"\"\"\n rule_strings = {}\n config_obj = ConfigParser.ConfigParser()\n config_obj.optionxform = str\n config_obj.read(options_file)\n for section in config_obj.sections():\n rule_strings, kwargs = get_rule_strings(config_obj, section)\n for (key, val) in rule_strings.iteritems():\n set_sla(obj, section, key, val)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_basic_metric_options(config_obj, section):\n infile = {}\n aggr_hosts = None\n aggr_metrics = None\n ts_start = None\n ts_end = None\n precision = None\n hostname = \"localhost\"\n rule_strings = {}\n important_sub_metrics = None\n anomaly_detection_metrics = None\n\n try:\n if config_obj.has_option(section, 'important_sub_metrics'):\n important_sub_metrics = config_obj.get(section, 'important_sub_metrics').split()\n config_obj.remove_option(section, 'important_sub_metrics')\n\n if config_obj.has_option(section, 'hostname'):\n hostname = config_obj.get(section, 'hostname')\n config_obj.remove_option(section, 'hostname')\n\n # 'infile' is not mandatory for aggregate metrics\n if config_obj.has_option(section, 'infile'):\n infile = config_obj.get(section, 'infile').split()\n config_obj.remove_option(section, 'infile')\n\n label = sanitize_string_section_name(section)\n if config_obj.has_option(section, 'ts_start'):\n ts_start = get_standardized_timestamp(config_obj.get(section, 'ts_start'), None)\n config_obj.remove_option(section, 'ts_start')\n if config_obj.has_option(section, 'ts_end'):\n ts_end = get_standardized_timestamp(config_obj.get(section, 'ts_end'), None)\n config_obj.remove_option(section, 'ts_end')\n if config_obj.has_option(section, 'precision'):\n precision = config_obj.get(section, 'precision')\n config_obj.remove_option(section, 'precision')\n # support aggregate metrics, which take aggr_hosts and aggr_metrics\n if config_obj.has_option(section, 'aggr_hosts'):\n aggr_hosts = config_obj.get(section, 'aggr_hosts')\n config_obj.remove_option(section, 'aggr_hosts')\n if config_obj.has_option(section, 'aggr_metrics'):\n aggr_metrics = config_obj.get(section, 'aggr_metrics')\n config_obj.remove_option(section, 'aggr_metrics')\n if config_obj.has_option(section, 'anomaly_detection_metrics'):\n anomaly_detection_metrics = config_obj.get(section, 'anomaly_detection_metrics').split()\n config_obj.remove_option(section, 'anomaly_detection_metrics')\n rule_strings, other_options = get_rule_strings(config_obj, section)\n except ConfigParser.NoOptionError:\n logger.exception(\"Exiting.... some mandatory options are missing from the config file in section: \" + section)\n sys.exit()\n\n return (hostname, infile, aggr_hosts, aggr_metrics, label, ts_start, ts_end, precision, aggr_metrics, other_options,\n rule_strings, important_sub_metrics, anomaly_detection_metrics)", "response": "Parse the basic options from the config_obj section of the config_obj."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse a metric section and create a Metric object.", "response": "def parse_metric_section(config_obj, section, metric_classes, metrics, aggregate_metric_classes, outdir_default, resource_path):\n \"\"\"\n Parse a metric section and create a Metric object\n :param config_obj: ConfigParser object\n :param section: Section name\n :param metric_classes: List of valid metric types\n :param metrics: List of all regular metric objects (used by aggregate metric)\n :param aggregate_metric_classes: List of all valid aggregate metric types\n :param outdir_default: Default output directory\n :param resource_path: Default resource directory\n :return: An initialized Metric object\n \"\"\"\n (hostname, infile, aggr_hosts, aggr_metrics, label, ts_start, ts_end, precision, aggr_metrics, other_options,\n rule_strings, important_sub_metrics, anomaly_detection_metrics) = parse_basic_metric_options(config_obj, section)\n\n # TODO: Make user specify metric_type in config and not infer from section\n metric_type = section.split('-')[0]\n if metric_type in aggregate_metric_classes:\n new_metric = initialize_aggregate_metric(section, aggr_hosts, aggr_metrics, metrics, outdir_default, resource_path, label, ts_start, ts_end, rule_strings,\n important_sub_metrics, anomaly_detection_metrics, other_options)\n else:\n new_metric = initialize_metric(section, infile, hostname, aggr_metrics, outdir_default, resource_path, label, ts_start, ts_end, rule_strings,\n important_sub_metrics, anomaly_detection_metrics, other_options)\n if config_obj.has_option(section, 'ignore') and config_obj.getint(section, 'ignore') == 1:\n new_metric.ignore = True\n if config_obj.has_option(section, 'calc_metrics'):\n new_metric.calc_metrics = config_obj.get(section, 'calc_metrics')\n new_metric.precision = precision\n return new_metric"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parse_global_section(config_obj, section):\n ts_start = None\n ts_end = None\n if config_obj.has_option(section, 'ts_start'):\n ts_start = get_standardized_timestamp(config_obj.get(section, 'ts_start'), None)\n config_obj.remove_option(section, 'ts_start')\n if config_obj.has_option(section, 'ts_end'):\n ts_end = get_standardized_timestamp(config_obj.get(section, 'ts_end'), None)\n config_obj.remove_option(section, 'ts_end')\n return ts_start, ts_end", "response": "Parse GLOBAL section in the config file to return start and end times"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses a RUN - STEP section in the config file to return a Run_Step object.", "response": "def parse_run_step_section(config_obj, section):\n \"\"\"\n Parse a RUN-STEP section in the config to return a Run_Step object\n :param config_obj: ConfigParser objection\n :param section: Section name\n :return: an initialized Run_Step object\n \"\"\"\n kill_after_seconds = None\n try:\n run_cmd = config_obj.get(section, 'run_cmd')\n run_rank = int(config_obj.get(section, 'run_rank'))\n except ConfigParser.NoOptionError:\n logger.exception(\"Exiting.... some mandatory options are missing from the config file in section: \" + section)\n sys.exit()\n except ValueError:\n logger.error(\"Bad run_rank %s specified in section %s, should be integer. Exiting.\", config_obj.get(section, 'run_rank'), section)\n sys.exit()\n if config_obj.has_option(section, 'run_type'):\n run_type = config_obj.get(section, 'run_type')\n else:\n run_type = CONSTANTS.RUN_TYPE_WORKLOAD\n if config_obj.has_option(section, 'run_order'):\n run_order = config_obj.get(section, 'run_order')\n else:\n run_order = CONSTANTS.PRE_ANALYSIS_RUN\n if config_obj.has_option(section, 'call_type'):\n call_type = config_obj.get(section, 'call_type')\n else:\n call_type = 'local'\n if config_obj.has_option(section, 'kill_after_seconds'):\n try:\n kill_after_seconds = int(config_obj.get(section, 'kill_after_seconds'))\n except ValueError:\n logger.error(\"Bad kill_after_seconds %s specified in section %s, should be integer.\", config_obj.get(section, 'kill_after_seconds'), section)\n\n if call_type == 'local':\n run_step_obj = Local_Cmd(run_type, run_cmd, call_type, run_order, run_rank, kill_after_seconds=kill_after_seconds)\n else:\n logger.error('Unsupported RUN_STEP supplied, call_type should be local')\n run_step_obj = None\n return run_step_obj"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing the GRAPH section of the config file and returns a list of options extracted from it", "response": "def parse_graph_section(config_obj, section, outdir_default, indir_default):\n \"\"\"\n Parse the GRAPH section of the config to extract useful values\n :param config_obj: ConfigParser object\n :param section: Section name\n :param outdir_default: Default output directory passed in args\n :param indir_default: Default input directory passed in args\n :return: List of options extracted from the GRAPH section\n \"\"\"\n graph_timezone = None\n graphing_library = CONSTANTS.DEFAULT_GRAPHING_LIBRARY\n crossplots = []\n\n if config_obj.has_option(section, 'graphing_library'):\n graphing_library = config_obj.get(section, 'graphing_library')\n if config_obj.has_option(section, 'graphs'):\n graphs_string = config_obj.get(section, 'graphs')\n crossplots = graphs_string.split()\n # Supporting both outdir and output_dir\n if config_obj.has_option(section, 'outdir'):\n outdir_default = config_obj.get(section, 'outdir')\n if config_obj.has_option(section, 'output_dir'):\n outdir_default = config_obj.get(section, 'output_dir')\n if config_obj.has_option(section, 'input_dir'):\n indir_default = config_obj.get(section, 'input_dir')\n if config_obj.has_option(section, 'graph_timezone'):\n graph_timezone = config_obj.get(section, 'graph_timezone')\n if graph_timezone not in (\"UTC\", \"PST\", \"PDT\"):\n logger.warn('Unsupported timezone ' + graph_timezone + ' specified in option graph_timezone. Will use UTC instead')\n graph_timezone = \"UTC\"\n return graphing_library, crossplots, outdir_default, indir_default, graph_timezone"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef calculate_stats(data_list, stats_to_calculate=['mean', 'std'], percentiles_to_calculate=[]):\n stats_to_numpy_method_map = {\n 'mean': numpy.mean,\n 'avg': numpy.mean,\n 'std': numpy.std,\n 'standard_deviation': numpy.std,\n 'median': numpy.median,\n 'min': numpy.amin,\n 'max': numpy.amax\n }\n calculated_stats = {}\n calculated_percentiles = {}\n if len(data_list) == 0:\n return calculated_stats, calculated_percentiles\n for stat in stats_to_calculate:\n if stat in stats_to_numpy_method_map.keys():\n calculated_stats[stat] = stats_to_numpy_method_map[stat](data_list)\n else:\n logger.error(\"Unsupported stat : \" + str(stat))\n for percentile in percentiles_to_calculate:\n if isinstance(percentile, float) or isinstance(percentile, int):\n calculated_percentiles[percentile] = numpy.percentile(data_list, percentile)\n else:\n logger.error(\"Unsupported percentile requested (should be int or float): \" + str(percentile))\n return calculated_stats, calculated_percentiles", "response": "Calculate statistics for given data."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_valid_file(filename):\n if os.path.exists(filename):\n if not os.path.getsize(filename):\n logger.warning('%s : file is empty.', filename)\n return False\n else:\n logger.warning('%s : file does not exist.', filename)\n return False\n return True", "response": "Check if the file exists and is not empty."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndetecting the format of a timestamp string", "response": "def detect_timestamp_format(timestamp):\n \"\"\"\n Given an input timestamp string, determine what format is it likely in.\n\n :param string timestamp: the timestamp string for which we need to determine format\n :return: best guess timestamp format\n \"\"\"\n time_formats = {\n 'epoch': re.compile(r'^[0-9]{10}$'),\n 'epoch_ms': re.compile(r'^[0-9]{13}$'),\n 'epoch_fraction': re.compile(r'^[0-9]{10}\\.[0-9]{3,9}$'),\n '%Y-%m-%d %H:%M:%S': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y-%m-%dT%H:%M:%S': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y-%m-%d_%H:%M:%S': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y-%m-%d %H:%M:%S.%f': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y-%m-%dT%H:%M:%S.%f': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y-%m-%d_%H:%M:%S.%f': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y%m%d %H:%M:%S': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y%m%dT%H:%M:%S': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y%m%d_%H:%M:%S': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%Y%m%d %H:%M:%S.%f': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y%m%dT%H:%M:%S.%f': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y%m%d_%H:%M:%S.%f': re.compile(r'^[0-9]{4}[0-1][0-9][0-3][0-9]_[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%H:%M:%S': re.compile(r'^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$'),\n '%H:%M:%S.%f': re.compile(r'^[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+$'),\n '%Y-%m-%dT%H:%M:%S.%f%z': re.compile(r'^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]+[+-][0-9]{4}$')\n }\n for time_format in time_formats:\n if re.match(time_formats[time_format], timestamp):\n return time_format\n return 'unknown'"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngiving a timestamp string return a time stamp in the epoch ms format.", "response": "def get_standardized_timestamp(timestamp, ts_format):\n \"\"\"\n Given a timestamp string, return a time stamp in the epoch ms format. If no date is present in\n timestamp then today's date will be added as a prefix before conversion to epoch ms\n \"\"\"\n if not timestamp:\n return None\n if timestamp == 'now':\n timestamp = str(datetime.datetime.now())\n if not ts_format:\n ts_format = detect_timestamp_format(timestamp)\n try:\n if ts_format == 'unknown':\n logger.error('Unable to determine timestamp format for : %s', timestamp)\n return -1\n elif ts_format == 'epoch':\n ts = int(timestamp) * 1000\n elif ts_format == 'epoch_ms':\n ts = timestamp\n elif ts_format == 'epoch_fraction':\n ts = int(timestamp[:10]) * 1000 + int(timestamp[11:])\n elif ts_format in ('%H:%M:%S', '%H:%M:%S.%f'):\n date_today = str(datetime.date.today())\n dt_obj = datetime.datetime.strptime(date_today + ' ' + timestamp, '%Y-%m-%d ' + ts_format)\n ts = calendar.timegm(dt_obj.utctimetuple()) * 1000 + dt_obj.microsecond / 1000\n else:\n dt_obj = datetime.datetime.strptime(timestamp, ts_format)\n ts = calendar.timegm(dt_obj.utctimetuple()) * 1000 + dt_obj.microsecond / 1000\n except ValueError:\n return -1\n return str(ts)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_sla(obj, metric, sub_metric, rules):\n if not hasattr(obj, 'sla_map'):\n return False\n rules_list = rules.split()\n for rule in rules_list:\n if '<' in rule:\n stat, threshold = rule.split('<')\n sla = SLA(metric, sub_metric, stat, threshold, 'lt')\n elif '>' in rule:\n stat, threshold = rule.split('>')\n sla = SLA(metric, sub_metric, stat, threshold, 'gt')\n else:\n if hasattr(obj, 'logger'):\n obj.logger.error('Unsupported SLA type defined : ' + rule)\n sla = None\n obj.sla_map[metric][sub_metric][stat] = sla\n if hasattr(obj, 'sla_list'):\n obj.sla_list.append(sla) # TODO : remove this once report has grading done in the metric tables\n return True", "response": "Set the SLA for a metric and sub_metric in a report."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if all SLAs pass and save the SLA results in a file athorian", "response": "def check_slas(metric):\n \"\"\"\n Check if all SLAs pass\n :return: 0 (if all SLAs pass) or the number of SLAs failures\n \"\"\"\n if not hasattr(metric, 'sla_map'):\n return\n for metric_label in metric.sla_map.keys():\n for sub_metric in metric.sla_map[metric_label].keys():\n for stat_name in metric.sla_map[metric_label][sub_metric].keys():\n sla = metric.sla_map[metric_label][sub_metric][stat_name]\n if stat_name[0] == 'p' and hasattr(metric, 'calculated_percentiles'):\n if sub_metric in metric.calculated_percentiles.keys():\n percentile_num = int(stat_name[1:])\n if isinstance(percentile_num, float) or isinstance(percentile_num, int):\n if percentile_num in metric.calculated_percentiles[sub_metric].keys():\n if not sla.check_sla_passed(metric.calculated_percentiles[sub_metric][percentile_num]):\n logger.info(\"Failed SLA for \" + sub_metric)\n metric.status = CONSTANTS.SLA_FAILED\n if sub_metric in metric.calculated_stats.keys() and hasattr(metric, 'calculated_stats'):\n if stat_name in metric.calculated_stats[sub_metric].keys():\n if not sla.check_sla_passed(metric.calculated_stats[sub_metric][stat_name]):\n logger.info(\"Failed SLA for \" + sub_metric)\n metric.status = CONSTANTS.SLA_FAILED\n # Save SLA results in a file\n if len(metric.sla_map.keys()) > 0 and hasattr(metric, 'get_sla_csv'):\n sla_csv_file = metric.get_sla_csv()\n with open(sla_csv_file, 'w') as FH:\n for metric_label in metric.sla_map.keys():\n for sub_metric in metric.sla_map[metric_label].keys():\n for stat, sla in metric.sla_map[metric_label][sub_metric].items():\n FH.write('%s\\n' % (sla.get_csv_repr()))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_logging(logger, log_file, log_level):\n with open(log_file, 'w'):\n pass\n numeric_level = getattr(logging, log_level.upper(), None) if log_level else logging.INFO\n if not isinstance(numeric_level, int):\n raise ValueError('Invalid log level: %s' % log_level)\n logger.setLevel(logging.DEBUG)\n fh = logging.FileHandler(log_file)\n fh.setLevel(logging.DEBUG)\n ch = logging.StreamHandler()\n ch.setLevel(numeric_level)\n formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n fh.setFormatter(formatter)\n ch.setFormatter(formatter)\n logger.addHandler(fh)\n logger.addHandler(ch)\n return CONSTANTS.OK", "response": "Initialize the naarad logger."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes list of valid arguments accepted by Naarad CLI parameters", "response": "def get_argument_parser():\n \"\"\"\n Initialize list of valid arguments accepted by Naarad CLI\n :return: arg_parser: argeparse.ArgumentParser object initialized with naarad CLI parameters\n \"\"\"\n arg_parser = argparse.ArgumentParser()\n arg_parser.add_argument('-c', '--config', help=\"file with specifications for each metric and graphs\")\n arg_parser.add_argument('--start', help=\"Start time in the format of HH:MM:SS or YYYY-mm-dd_HH:MM:SS\")\n arg_parser.add_argument('--end', help=\"End time in the format of HH:MM:SS or YYYY-mm-dd_HH:MM:SS\")\n arg_parser.add_argument('-i', '--input_dir', help=\"input directory used to construct full path name of the metric infile\")\n arg_parser.add_argument('-o', '--output_dir', help=\"output directory where the plots and Report.html will be generated\")\n arg_parser.add_argument('-V', '--variables', action=\"append\",\n help=\"User defined variables (in form key=value) for substitution in the config file. \"\n \"Config should have the variable names in format %%(key)s\")\n arg_parser.add_argument('-s', '--show_config', help=\"Print config associated with the provided template name\", action=\"store_true\")\n arg_parser.add_argument('-l', '--log', help=\"log level\")\n arg_parser.add_argument('-d', '--diff', nargs=2,\n help=\"Specify the location of two naarad reports to diff separated by a space. Can be local or http(s) \"\n \"locations. The first report is used as a baseline.\", metavar=(\"report-1\", \"report-2\"))\n arg_parser.add_argument('-n', '--no_plots',\n help=\"Don't generate plot images. Useful when you only want SLA calculations. Note that on-demand charts can \"\n \"still be generated through client-charting.\", action=\"store_true\")\n arg_parser.add_argument('-e', '--exit_code', help=\"optional argument to enable exit_code for naarad\", action=\"store_true\")\n # TODO(Ritesh) : Print a list of all templates supported with descriptions\n # arg_parser.add_argument('-l', '--list_templates', help=\"List all template configs\", action=\"store_true\")\n return arg_parser"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_variables(args):\n variables_dict = {}\n if args.variables:\n for var in args.variables:\n words = var.split('=')\n variables_dict[words[0]] = words[1]\n return variables_dict", "response": "Return a dictionary of variables specified at CLI\n"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_arguments(args):\n if args.diff:\n if not args.output_dir:\n logger.error('No Output location specified')\n print_usage()\n sys.exit(0)\n # elif not (args.config and args.output_dir):\n elif not args.output_dir:\n print_usage()\n sys.exit(0)", "response": "Validate that the necessary arguments for normal or diff analysis are specified."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef initialize_metric(section, infile_list, hostname, aggr_metrics, output_directory, resource_path, label, ts_start, ts_end, rule_strings,\n important_sub_metrics, anomaly_detection_metrics, other_options):\n \"\"\"\n Initialize appropriate metric based on type of metric.\n :param: section: config section name or auto discovered metric type\n :param: infile_list: list of input log files for the metric\n :param: hostname: hostname associated with the logs origin\n :param: output_directory: report location\n :param: resource_path: resource path for report\n :param: label: label for config section or auto discovered metric type\n :param: ts_start: start time for analysis\n :param: ts_end: end time for analysis\n :param: rule_strings: list of slas\n :param: important_sub_metrics: list of important sub metrics\n :param: anomaly_detection_metrics: list of metrics to use for anomaly detection.\n :param: other_options: kwargs\n :return: metric object\n \"\"\"\n metric = None\n metric_type = section.split('-')[0]\n if metric_type in metric_classes:\n if 'SAR' in metric_type:\n metric = metric_classes['SAR'](section, infile_list, hostname, aggr_metrics, output_directory, resource_path, label, ts_start, ts_end,\n rule_strings, important_sub_metrics, anomaly_detection_metrics, **other_options)\n else:\n metric = metric_classes[metric_type](section, infile_list, hostname, aggr_metrics, output_directory, resource_path, label, ts_start, ts_end,\n rule_strings, important_sub_metrics, anomaly_detection_metrics, **other_options)\n else:\n metric = Metric(section, infile_list, hostname, aggr_metrics, output_directory, resource_path, label, ts_start, ts_end, rule_strings,\n important_sub_metrics, anomaly_detection_metrics, **other_options)\n return metric", "response": "Initialize appropriate metric based on type of metric."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef initialize_aggregate_metric(section, aggr_hosts, aggr_metrics, metrics, outdir_default, resource_path, label, ts_start, ts_end, rule_strings,\n important_sub_metrics, anomaly_detection_metrics, other_options):\n \"\"\"\n Initialize aggregate metric\n :param: section: config section name\n :param: aggr_hosts: list of hostnames to aggregate\n :param: aggr_metrics: list of metrics to aggregate\n :param: metrics: list of metric objects associated with the current naarad analysis\n :param: outdir_default: report location\n :param: resource_path: resource path for report\n :param: label: label for config section\n :param: ts_start: start time for analysis\n :param: ts_end: end time for analysis\n :param: rule_strings: list of slas\n :param: important_sub_metrics: list of important sub metrics\n :param: other_options: kwargs\n :return: metric object\n \"\"\"\n metric = None\n metric_type = section.split('-')[0]\n metric = aggregate_metric_classes[metric_type](section, aggr_hosts, aggr_metrics, metrics, outdir_default, resource_path, label, ts_start, ts_end,\n rule_strings, important_sub_metrics, anomaly_detection_metrics, **other_options)\n return metric", "response": "Initialize aggregate metric object based on the config section name."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsingle metric graphing function", "response": "def graph_csv(output_directory, resource_path, csv_file, plot_title, output_filename, y_label=None, precision=None, graph_height=\"600\", graph_width=\"1500\"):\n \"\"\" Single metric graphing function \"\"\"\n if not os.path.getsize(csv_file):\n return False, \"\"\n y_label = y_label or plot_title\n div_id = str(random.random())\n div_string = \"
\" % (div_id, graph_width, graph_height)\n script_string = \"\"\"\"\"\"\n\n with open(os.path.join(output_directory, output_filename + '.div'), 'w') as div_file:\n div_file.write(div_string + script_string)\n # TODO(ritesh): Also generate PNGs if someone needs them separately\n return True, os.path.join(output_directory, output_filename + '.div')"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\naggregating the count of data over time", "response": "def aggregate_count_over_time(self, metric_store, line_data, transaction_list, aggregate_timestamp):\n \"\"\"\n Organize and store the count of data from the log line into the metric store by metric type, transaction, timestamp\n\n :param dict metric_store: The metric store used to store all the parsed jmeter log data\n :param dict line_data: dict with the extracted k:v from the log line\n :param list transaction_list: list of transaction to be used for storing the metrics from given line\n :param string aggregate_timestamp: timestamp used for storing the raw data. This accounts for aggregation time period\n :return: None\n \"\"\"\n for transaction in transaction_list:\n if line_data.get('s') == 'true':\n all_qps = metric_store['qps']\n else:\n all_qps = metric_store['eqps']\n qps = all_qps[transaction]\n if aggregate_timestamp in qps:\n qps[aggregate_timestamp] += 1\n else:\n qps[aggregate_timestamp] = 1\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef aggregate_values_over_time(self, metric_store, line_data, transaction_list, metric_list, aggregate_timestamp):\n for metric in metric_list:\n for transaction in transaction_list:\n metric_data = reduce(defaultdict.__getitem__, [metric, transaction, aggregate_timestamp], metric_store)\n metric_data.append(float(line_data.get(metric)))\n return None", "response": "Aggregate the values of the log line into the metric store by the given timestamp."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate the time series for the various metrics averaged over the aggregation period being used for plots", "response": "def average_values_for_plot(self, metric_store, data, averaging_factor):\n \"\"\"\n Create the time series for the various metrics, averaged over the aggregation period being used for plots\n\n :param dict metric_store: The metric store used to store all the parsed jmeter log data\n :param dict data: Dict with all the metric data to be output to csv\n :param float averaging_factor: averaging factor to be used for calculating the average per second metrics\n :return: None\n \"\"\"\n for metric, transaction_store in metric_store.items():\n for transaction, time_store in transaction_store.items():\n for time_stamp, metric_data in sorted(time_store.items()):\n if metric in ['t', 'by']:\n data[self.get_csv(transaction, metric)].append(','.join([str(time_stamp), str(sum(map(float, metric_data)) / float(len(metric_data)))]))\n if metric == 'by':\n metric_store['thr'][transaction][time_stamp] = sum(map(float, metric_data)) / float(averaging_factor * 1024 * 1024 / 8.0)\n data[self.get_csv(transaction, 'thr')].append(','.join([str(time_stamp), str(metric_store['thr'][transaction][time_stamp])]))\n elif metric in ['qps', 'eqps']:\n data[self.get_csv(transaction, metric)].append(','.join([str(time_stamp), str(metric_data / float(averaging_factor))]))\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef calculate_key_stats(self, metric_store):\n stats_to_calculate = ['mean', 'std', 'median', 'min', 'max'] # TODO: get input from user\n percentiles_to_calculate = range(5, 101, 5) # TODO: get input from user\n percentiles_to_calculate.append(99)\n for transaction in metric_store['t'].keys():\n transaction_key = transaction + '.' + 'ResponseTime'\n # For ResponseTime and ResponseSize, each timestamp has a list of values associated with it.\n # Using heapq.merge to merge all the lists into a single list to be passed to numpy.\n self.calculated_stats[transaction_key], self.calculated_percentiles[transaction_key] = \\\n naarad.utils.calculate_stats(list(heapq.merge(*metric_store['t'][transaction].values())),\n stats_to_calculate, percentiles_to_calculate)\n self.update_summary_stats(transaction_key)\n transaction_key = transaction + '.' + 'qps'\n if len(metric_store['qps'][transaction].values()) > 0:\n self.calculated_stats[transaction_key], self.calculated_percentiles[transaction_key] = \\\n naarad.utils.calculate_stats(metric_store['qps'][transaction].values(),\n stats_to_calculate, percentiles_to_calculate)\n self.update_summary_stats(transaction_key)\n transaction_key = transaction + '.' + 'ResponseSize'\n self.calculated_stats[transaction_key], self.calculated_percentiles[transaction_key] = \\\n naarad.utils.calculate_stats(list(heapq.merge(*metric_store['by'][transaction].values())),\n stats_to_calculate, percentiles_to_calculate)\n self.update_summary_stats(transaction_key)\n if 'eqps' in metric_store.keys() and transaction in metric_store['eqps'].keys():\n transaction_key = transaction + '.' + 'ErrorsPerSecond'\n self.calculated_stats[transaction_key], self.calculated_percentiles[transaction_key] = \\\n naarad.utils.calculate_stats(metric_store['eqps'][transaction].values(),\n stats_to_calculate, percentiles_to_calculate)\n self.update_summary_stats(transaction + '.' + 'ErrorsPerSecond')\n transaction_key = transaction + '.' + 'DataThroughput'\n self.calculated_stats[transaction_key], self.calculated_percentiles[transaction_key] = \\\n naarad.utils.calculate_stats(metric_store['thr'][transaction].values(),\n stats_to_calculate, percentiles_to_calculate)\n self.update_summary_stats(transaction_key)\n return None", "response": "Calculate key statistics for given data and store in the class variables calculated_stats and calculated_percentiles"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse(self):\n file_status = True\n for infile in self.infile_list:\n file_status = file_status and naarad.utils.is_valid_file(infile)\n if not file_status:\n return False\n\n status = self.parse_xml_jtl(self.aggregation_granularity)\n gc.collect()\n return status", "response": "Parse the Jmeter file and calculate key stats"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_xml_jtl(self, granularity):\n data = defaultdict(list)\n processed_data = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))\n for input_file in self.infile_list:\n logger.info('Processing : %s', input_file)\n timestamp_format = None\n tree = ElementTree.parse(input_file)\n samples = tree.findall('./httpSample') + tree.findall('./sample')\n for sample in samples:\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(sample.get('ts'))\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(sample.get('ts'), timestamp_format)\n if ts == -1:\n continue\n ts = naarad.utils.reconcile_timezones(ts, self.timezone, self.graph_timezone)\n aggregate_timestamp, averaging_factor = self.get_aggregation_timestamp(ts, granularity)\n self.aggregate_count_over_time(processed_data, sample, [self._sanitize_label(sample.get('lb')), 'Overall_Summary'], aggregate_timestamp)\n self.aggregate_values_over_time(processed_data, sample, [self._sanitize_label(sample.get('lb')), 'Overall_Summary'], ['t', 'by'], aggregate_timestamp)\n logger.info('Finished parsing : %s', input_file)\n logger.info('Processing metrics for output to csv')\n self.average_values_for_plot(processed_data, data, averaging_factor)\n logger.info('Writing time series csv')\n for csv in data.keys():\n self.csv_files.append(csv)\n with open(csv, 'w') as csvf:\n csvf.write('\\n'.join(sorted(data[csv])))\n logger.info('Processing raw data for stats')\n self.calculate_key_stats(processed_data)\n return True", "response": "Parse the Jmeter workload output in XML format and extract overall and per transaction data and key statistics"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef put_values_into_data(self, values):\n for col, value in values.items():\n if col in self.column_csv_map:\n out_csv = self.column_csv_map[col]\n else:\n out_csv = self.get_csv(col) # column_csv_map[] is assigned in get_csv()\n self.data[out_csv] = []\n self.data[out_csv].append(self.ts + \",\" + value)", "response": "Take the col in values append value into self. data"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprocessing the top line of the log file", "response": "def process_top_line(self, words):\n \"\"\"\n Process the line starting with \"top\"\n Example log: top - 00:00:02 up 32 days, 7:08, 19 users, load average: 0.00, 0.00, 0.00\n \"\"\"\n self.ts_time = words[2]\n self.ts = self.ts_date + ' ' + self.ts_time\n self.ts = ts = naarad.utils.get_standardized_timestamp(self.ts, None)\n\n if self.ts_out_of_range(self.ts):\n self.ts_valid_lines = False\n else:\n self.ts_valid_lines = True\n up_days = int(words[4])\n up_hour_minute = words[6].split(':') # E.g. '4:02,'\n up_minutes = int(up_hour_minute[0]) * 60 + int(up_hour_minute[1].split(',')[0])\n uptime_minute = up_days * 24 * 60 + up_minutes # Converting days to minutes\n\n values = {}\n values['uptime_minute'] = str(uptime_minute)\n values['num_users'] = words[7]\n values['load_aver_1_minute'] = words[11][:-1]\n values['load_aver_5_minute'] = words[12][:-1]\n values['load_aver_15_minute'] = words[13]\n self.put_values_into_data(values)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprocesses the line starting with Tasks", "response": "def process_tasks_line(self, words):\n \"\"\"\n Process the line starting with \"Tasks:\"\n Example log: Tasks: 446 total, 1 running, 442 sleeping, 2 stopped, 1 zombie\n \"\"\"\n words = words[1:]\n length = len(words) / 2 # The number of pairs\n values = {}\n for offset in range(length):\n k = words[2 * offset + 1].strip(',')\n v = words[2 * offset]\n values['tasks_' + k] = v\n self.put_values_into_data(values)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef process_cpu_line(self, words):\n\n values = {}\n for word in words[1:]:\n val, key = word.split('%')\n values['cpu_' + key.strip(',')] = val\n self.put_values_into_data(values)", "response": "Process the line starting with Cpu ( s"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts a size such as 2343M to G", "response": "def convert_to_G(self, word):\n \"\"\"\n Given a size such as '2333M', return the converted value in G\n \"\"\"\n value = 0.0\n if word[-1] == 'G' or word[-1] == 'g':\n value = float(word[:-1])\n elif word[-1] == 'M' or word[-1] == 'm':\n value = float(word[:-1]) / 1000.0\n elif word[-1] == 'K' or word[-1] == 'k':\n value = float(word[:-1]) / 1000.0 / 1000.0\n else: # No unit\n value = float(word) / 1000.0 / 1000.0 / 1000.0\n return str(value)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef process_swap_line(self, words):\n words = words[1:]\n length = len(words) / 2 # The number of pairs\n values = {}\n for offset in range(length):\n k = words[2 * offset + 1].strip(',')\n v = self.convert_to_G(words[2 * offset])\n values['swap_' + k] = v\n self.put_values_into_data(values)", "response": "Process the line starting with Swap"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses individual command line.", "response": "def process_individual_command(self, words):\n \"\"\"\n process the individual lines like this:\n #PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND\n 29303 root 20 0 35300 2580 1664 R 3.9 0.0 0:00.02 top\n 11 root RT 0 0 0 0 S 1.9 0.0 0:18.87 migration/2\n 3702 root 20 0 34884 4192 1692 S 1.9 0.0 31:40.47 cf-serverd\n It does not record all processes due to memory concern; rather only records interested processes (based on user input of PID and COMMAND)\n \"\"\"\n pid_index = self.process_headers.index('PID')\n proces_index = self.process_headers.index('COMMAND')\n\n pid = words[pid_index]\n process = words[proces_index]\n if pid in self.PID or process in self.COMMAND:\n process_name = process.split('/')[0]\n\n values = {}\n for word_col in self.process_headers:\n word_index = self.process_headers.index(word_col)\n if word_col in ['VIRT', 'RES', 'SHR']: # These values need to convert to 'G'\n values[process_name + '_' + pid + '_' + word_col] = self.convert_to_G(words[word_index])\n elif word_col in ['PR', 'NI', '%CPU', '%MEM']: # These values will be assigned later or ignored\n values[process_name + '_' + pid + '_' + word_col.strip('%')] = words[word_index]\n\n uptime_index = self.process_headers.index('TIME+')\n uptime = words[uptime_index].split(':')\n uptime_sec = float(uptime[0]) * 60 + float(uptime[1])\n values[process_name + '_' + pid + '_' + 'TIME'] = str(uptime_sec)\n self.put_values_into_data(values)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse(self):\n\n for infile in self.infile_list:\n logger.info('Processing : %s', infile)\n status = True\n file_status = naarad.utils.is_valid_file(infile)\n if not file_status:\n return False\n\n with open(infile) as fh:\n for line in fh:\n words = line.split()\n if not words:\n continue\n\n # Pattern matches line of '2014-02-03'\n if re.match('^\\d\\d\\d\\d-\\d\\d-\\d\\d$', line):\n self.ts_date = words[0]\n continue\n\n prefix_word = words[0].strip()\n if prefix_word == 'top':\n self.process_top_line(words)\n self.saw_pid = False # Turn off the processing of individual process line\n elif self.ts_valid_lines:\n if prefix_word == 'Tasks:':\n self.process_tasks_line(words)\n elif prefix_word == 'Cpu(s):':\n self.process_cpu_line(words)\n elif prefix_word == 'Mem:':\n self.process_mem_line(words)\n elif prefix_word == 'Swap:':\n self.process_swap_line(words)\n elif prefix_word == 'PID':\n self.saw_pid = True # Turn on the processing of individual process line\n self.process_headers = words\n else: # Each individual process line\n if self.saw_pid and len(words) >= len(self.process_headers): # Only valid process lines\n self.process_individual_command(words)\n\n # Putting data in csv files;\n for out_csv in self.data.keys(): # All sub_metrics\n self.csv_files.append(out_csv)\n with open(out_csv, 'w') as fh:\n fh.write('\\n'.join(self.data[out_csv]))\n\n gc.collect()\n return status", "response": "Parse the top output file and return the status of the metric"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nbase function which takes a single url download it to outdir and return the local full path name of the downloaded file.", "response": "def handle_single_url(url, outdir, outfile=None):\n \"\"\"\n Base function which takes a single url, download it to outdir/outfile\n :param str url: a full/absolute url, e.g. http://www.cnn.com/log.zip\n :param str outdir: the absolute local directory. e.g. /home/user1/tmp/\n :param str outfile: (optional) filename stored in local directory. If outfile is not given, extract the filename from url\n :return: the local full path name of downloaded url\n \"\"\"\n if not url or type(url) != str \\\n or not outdir or type(outdir) != str:\n logger.error('passed in parameters %s %s are incorrect.' % (url, outdir))\n return\n\n if not naarad.utils.is_valid_url(url):\n logger.error(\"passed in url %s is incorrect.\" % url)\n return\n\n if not outfile:\n segs = url.split('/')\n outfile = segs[-1]\n outfile = urllib2.quote(outfile)\n\n output_file = os.path.join(outdir, outfile)\n if os.path.exists(output_file):\n logger.warn(\"the %s already exists!\" % outfile)\n\n with open(output_file, \"w\") as fh:\n try:\n response = urllib2.urlopen(url)\n fh.write(response.read())\n except urllib2.HTTPError:\n logger.error(\"got HTTPError when retrieving %s\" % url)\n return\n except urllib2.URLError:\n logger.error(\"got URLError when retrieving %s\" % url)\n return\n\n return output_file"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread response of specified url into memory and return to caller.", "response": "def stream_url(url):\n \"\"\"\n Read response of specified url into memory and return to caller. No persistence to disk.\n :return: response content if accessing the URL succeeds, False otherwise\n \"\"\"\n try:\n response = urllib2.urlopen(url)\n response_content = response.read()\n return response_content\n except (urllib2.URLError, urllib2.HTTPError) as e:\n logger.error('Unable to access requested URL: %s', url)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a list of urls from a seeding url", "response": "def get_urls_from_seed(url):\n \"\"\"\n get a list of urls from a seeding url, return a list of urls\n\n :param str url: a full/absolute url, e.g. http://www.cnn.com/logs/\n :return: a list of full/absolute urls.\n \"\"\"\n\n if not url or type(url) != str or not naarad.utils.is_valid_url(url):\n logger.error(\"get_urls_from_seed() does not have valid seeding url.\")\n return\n\n # Extract the host info of \"http://host:port/\" in case of href urls are elative urls (e.g., /path/gc.log)\n # Then join (host info and relative urls) to form the complete urls\n base_index = url.find('/', len(\"https://\")) # get the first \"/\" after http://\" or \"https://\"; handling both cases.\n base_url = url[:base_index] # base_url = \"http://host:port\" or https://host:port\" or http://host\" (where no port is given)\n\n # Extract the \"href\" denoted urls\n urls = []\n try:\n response = urllib2.urlopen(url)\n hp = HTMLLinkExtractor()\n hp.feed(response.read())\n urls = hp.links\n hp.close()\n except urllib2.HTTPError:\n logger.error(\"Got HTTPError when opening the url of %s\" % url)\n return urls\n\n # Check whether the url is relative or complete\n for i in range(len(urls)):\n if not urls[i].startswith(\"http://\") and not urls[i].startswith(\"https://\"): # a relative url ?\n urls[i] = base_url + urls[i]\n\n return urls"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef download_url_single(inputs, outdir, outfile=None):\n\n if not inputs or type(inputs) != str or not outdir or type(outdir) != str:\n logging.error(\"The call parameters are invalid.\")\n return\n else:\n if not os.path.exists(outdir):\n os.makedirs(outdir)\n\n output_file = handle_single_url(inputs, outdir, outfile)\n return output_file", "response": "Downloads a single http url to a local file\n"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndownload http ( s ) urls from a seed url and stores them in a local file.", "response": "def download_url_regex(inputs, outdir, regex=\".*\"):\n \"\"\"\n Downloads http(s) urls to a local files\n :param str inputs: Required, the seed url\n :param str outdir: Required. the local directory to put the downloadedfiles.\n :param str regex: Optional, a regex string. If not given, then all urls will be valid\n :return: A list of local full path names (downloaded from inputs)\n \"\"\"\n if not inputs or type(inputs) != str \\\n or not outdir or type(outdir) != str:\n logging.error(\"The call parameters are invalid.\")\n return\n else:\n if not os.path.exists(outdir):\n os.makedirs(outdir)\n\n output_files = []\n files = get_urls_from_seed(inputs)\n for f in files:\n if re.compile(regex).match(f):\n output_file = handle_single_url(f, outdir)\n output_files.append(output_file)\n\n return output_files"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreads data from a csv file into a dictionary.", "response": "def read_csv(csv_name):\n \"\"\"\n Read data from a csv file into a dictionary.\n :param str csv_name: path to a csv file.\n :return dict: a dictionary represents the data in file.\n \"\"\"\n data = {}\n if not isinstance(csv_name, (str, unicode)):\n raise exceptions.InvalidDataFormat('luminol.utils: csv_name has to be a string!')\n with open(csv_name, 'r') as csv_data:\n reader = csv.reader(csv_data, delimiter=',', quotechar='|')\n for row in reader:\n try:\n key = to_epoch(row[0])\n value = float(row[1])\n data[key] = value\n except ValueError:\n pass\n return data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning the command and infer time period for analysis phase.", "response": "def run(self):\n \"\"\"\n Run the command, infer time period to be used in metric analysis phase.\n :return: None\n \"\"\"\n cmd_args = shlex.split(self.run_cmd)\n logger.info('Local command RUN-STEP starting with rank %d', self.run_rank)\n logger.info('Running subprocess command with following args: ' + str(cmd_args))\n\n # TODO: Add try catch blocks. Kill process on CTRL-C\n # Infer time period for analysis. Assume same timezone between client and servers.\n self.ts_start = time.strftime(\"%Y-%m-%d %H:%M:%S\")\n try:\n self.process = subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1)\n if self.kill_after_seconds:\n self.timer = Timer(self.kill_after_seconds, self.kill)\n self.timer.start()\n # Using 2nd method here to stream output:\n # http://stackoverflow.com/questions/2715847/python-read-streaming-input-from-subprocess-communicate\n for line in iter(self.process.stdout.readline, b''):\n logger.info(line.strip())\n self.process.communicate()\n except KeyboardInterrupt:\n logger.warning('Handling keyboard interrupt (Ctrl-C)')\n self.kill()\n if self.timer:\n self.timer.cancel()\n self.ts_end = time.strftime(\"%Y-%m-%d %H:%M:%S\")\n logger.info('subprocess finished')\n logger.info('run_step started at ' + self.ts_start + ' and ended at ' + self.ts_end)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nkilling the run_step process.", "response": "def kill(self):\n \"\"\"\n If run_step needs to be killed, this method will be called\n :return: None\n \"\"\"\n try:\n logger.info('Trying to terminating run_step...')\n self.process.terminate()\n time_waited_seconds = 0\n while self.process.poll() is None and time_waited_seconds < CONSTANTS.SECONDS_TO_KILL_AFTER_SIGTERM:\n time.sleep(0.5)\n time_waited_seconds += 0.5\n if self.process.poll() is None:\n self.process.kill()\n logger.warning('Waited %d seconds for run_step to terminate. Killing now....', CONSTANTS.SECONDS_TO_KILL_AFTER_SIGTERM)\n except OSError, e:\n logger.error('Error while trying to kill the subprocess: %s', e)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy_local_includes(self):\n resource_folder = self.get_resources_location()\n for stylesheet in self.stylesheet_includes:\n if ('http' not in stylesheet) and naarad.utils.is_valid_file(os.path.join(resource_folder, stylesheet)):\n shutil.copy(os.path.join(resource_folder, stylesheet), self.resource_directory)\n\n for javascript in self.javascript_includes:\n if ('http' not in javascript) and naarad.utils.is_valid_file(os.path.join(resource_folder, javascript)):\n shutil.copy(os.path.join(resource_folder, javascript), self.resource_directory)\n\n return None", "response": "Copy local css and javascript includes from naarad resources to the report / resources directory"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef generate_client_charting_page(self, data_sources):\n if not os.path.exists(self.resource_directory):\n os.makedirs(self.resource_directory)\n self.copy_local_includes()\n template_loader = FileSystemLoader(self.get_resources_location())\n template_environment = Environment(loader=template_loader)\n client_html = template_environment.get_template(CONSTANTS.TEMPLATE_HEADER).render(custom_stylesheet_includes=CONSTANTS.STYLESHEET_INCLUDES,\n custom_javascript_includes=CONSTANTS.JAVASCRIPT_INCLUDES,\n resource_path=self.resource_path,\n report_title='naarad diff report') + '\\n'\n client_html += template_environment.get_template(CONSTANTS.TEMPLATE_DIFF_CLIENT_CHARTING).render(data_series=data_sources,\n resource_path=self.resource_path) + '\\n'\n client_html += template_environment.get_template(CONSTANTS.TEMPLATE_FOOTER).render()\n return client_html", "response": "Generate the client charting page for the diff report."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef generate_diff_html(self):\n if not os.path.exists(self.resource_directory):\n os.makedirs(self.resource_directory)\n self.copy_local_includes()\n div_html = ''\n for plot_div in sorted(self.plot_files):\n with open(plot_div, 'r') as div_file:\n div_html += '\\n' + div_file.read()\n template_loader = FileSystemLoader(self.get_resources_location())\n template_environment = Environment(loader=template_loader)\n template_environment.filters['sanitize_string'] = naarad.utils.sanitize_string\n diff_html = template_environment.get_template(CONSTANTS.TEMPLATE_HEADER).render(custom_stylesheet_includes=CONSTANTS.STYLESHEET_INCLUDES,\n custom_javascript_includes=CONSTANTS.JAVASCRIPT_INCLUDES,\n resource_path=self.resource_path,\n report_title='naarad diff report') + '\\n'\n diff_html += template_environment.get_template(CONSTANTS.TEMPLATE_DIFF_PAGE).render(diff_data=self.diff_data, plot_div_content=div_html,\n reports=self.reports, sla_failure_list=self.sla_failure_list,\n sla_map=self.sla_map) + '\\n'\n diff_html += template_environment.get_template(CONSTANTS.TEMPLATE_FOOTER).render()\n return diff_html", "response": "Generate the summary diff report html from template\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef discover(self, metafile):\n for report in self.reports:\n if report.remote_location == 'local':\n if naarad.utils.is_valid_file(os.path.join(os.path.join(report.location, self.resource_path), metafile)):\n with open(os.path.join(os.path.join(report.location, self.resource_path), metafile), 'r') as meta_file:\n if metafile == CONSTANTS.STATS_CSV_LIST_FILE:\n report.stats = meta_file.readlines()[0].split(',')\n elif metafile == CONSTANTS.PLOTS_CSV_LIST_FILE:\n report.datasource = meta_file.readlines()[0].split(',')\n elif metafile == CONSTANTS.CDF_PLOTS_CSV_LIST_FILE:\n report.cdf_datasource = meta_file.readlines()[0].split(',')\n else:\n report.status = 'NO_SUMMARY_STATS'\n self.status = 'ERROR'\n logger.error('Unable to access summary stats file for report :%s', report.label)\n return False\n else:\n stats_url = report.remote_location + '/' + self.resource_path + '/' + metafile\n meta_file_data = naarad.httpdownload.stream_url(stats_url)\n\n if meta_file_data:\n if metafile == CONSTANTS.STATS_CSV_LIST_FILE:\n report.stats = meta_file_data.split(',')\n elif metafile == CONSTANTS.PLOTS_CSV_LIST_FILE:\n report.datasource = meta_file_data.split(',')\n elif metafile == CONSTANTS.CDF_PLOTS_CSV_LIST_FILE:\n report.cdf_datasource = meta_file_data.split(',')\n else:\n report.status = 'NO_SUMMARY_STATS'\n self.status = 'ERROR'\n logger.error('No summary stats available for report :%s', report.label)\n return False\n return True", "response": "Discover what summary stats time series and CDF csv exist for the reports that need to be diffed."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef collect_datasources(self):\n report_count = 0\n if self.status != 'OK':\n return False\n diff_datasource = sorted(set(self.reports[0].datasource) & set(self.reports[1].datasource))\n if diff_datasource:\n self.reports[0].datasource = diff_datasource\n self.reports[1].datasource = diff_datasource\n else:\n self.status = 'NO_COMMON_STATS'\n logger.error('No common metrics were found between the two reports')\n return False\n for report in self.reports:\n report.label = report_count\n report_count += 1\n report.local_location = os.path.join(self.resource_directory, str(report.label))\n try:\n os.makedirs(report.local_location)\n except OSError as exeption:\n if exeption.errno != errno.EEXIST:\n raise\n if report.remote_location != 'local':\n naarad.httpdownload.download_url_list(map(lambda x: report.remote_location + '/' + self.resource_path + '/' + x + '.csv', report.datasource),\n report.local_location)\n else:\n for filename in report.datasource:\n try:\n shutil.copy(os.path.join(os.path.join(report.location, self.resource_path), filename + '.csv'), report.local_location)\n except IOError as exeption:\n continue\n return True", "response": "Collect the common metrics from both the diffed reports and download them to the diff report resources directory\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates CDF diff plots of the submetrics", "response": "def plot_diff(self, graphing_library='matplotlib'):\n \"\"\"\n Generate CDF diff plots of the submetrics\n \"\"\"\n diff_datasource = sorted(set(self.reports[0].datasource) & set(self.reports[1].datasource))\n graphed = False\n for submetric in diff_datasource:\n baseline_csv = naarad.utils.get_default_csv(self.reports[0].local_location, (submetric + '.percentiles'))\n current_csv = naarad.utils.get_default_csv(self.reports[1].local_location, (submetric + '.percentiles'))\n if (not (naarad.utils.is_valid_file(baseline_csv) & naarad.utils.is_valid_file(current_csv))):\n continue\n baseline_plot = PD(input_csv=baseline_csv, csv_column=1, series_name=submetric, y_label=submetric, precision=None, graph_height=600, graph_width=1200,\n graph_type='line', plot_label='baseline', x_label='Percentiles')\n current_plot = PD(input_csv=current_csv, csv_column=1, series_name=submetric, y_label=submetric, precision=None, graph_height=600, graph_width=1200,\n graph_type='line', plot_label='current', x_label='Percentiles')\n graphed, div_file = Diff.graphing_modules[graphing_library].graph_data_on_the_same_graph([baseline_plot, current_plot],\n os.path.join(self.output_directory, self.resource_path),\n self.resource_path, (submetric + '.diff'))\n if graphed:\n self.plot_files.append(div_file)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef check_sla(self, sla, diff_metric):\n try:\n if sla.display is '%':\n diff_val = float(diff_metric['percent_diff'])\n else:\n diff_val = float(diff_metric['absolute_diff'])\n except ValueError:\n return False\n if not (sla.check_sla_passed(diff_val)):\n self.sla_failures += 1\n self.sla_failure_list.append(DiffSLAFailure(sla, diff_metric))\n return True", "response": "Check whether the SLA has passed or failed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating a diff report from the reports specified.", "response": "def generate(self):\n \"\"\"\n Generate a diff report from the reports specified.\n :return: True/False : return status of whether the diff report generation succeeded.\n \"\"\"\n if (self.discover(CONSTANTS.STATS_CSV_LIST_FILE) and self.discover(CONSTANTS.PLOTS_CSV_LIST_FILE) and self.discover(CONSTANTS.CDF_PLOTS_CSV_LIST_FILE) and\n self.collect() and self.collect_datasources() and self.collect_cdf_datasources()):\n for stats in self.reports[0].stats:\n metric_label = stats.replace('.stats.csv', '')\n stats_0 = os.path.join(self.reports[0].local_location, stats)\n stats_1 = os.path.join(self.reports[1].local_location, stats)\n report0_stats = {}\n report1_stats = {}\n if naarad.utils.is_valid_file(stats_0) and naarad.utils.is_valid_file(stats_1):\n report0 = csv.DictReader(open(stats_0))\n for row in report0:\n report0_stats[row[CONSTANTS.SUBMETRIC_HEADER]] = row\n report0_stats['__headers__'] = report0._fieldnames\n report1 = csv.DictReader(open(stats_1))\n for row in report1:\n report1_stats[row[CONSTANTS.SUBMETRIC_HEADER]] = row\n report1_stats['__headers__'] = report1._fieldnames\n common_stats = sorted(set(report0_stats['__headers__']) & set(report1_stats['__headers__']))\n common_submetrics = sorted(set(report0_stats.keys()) & set(report1_stats.keys()))\n for submetric in common_submetrics:\n if submetric != '__headers__':\n for stat in common_stats:\n if stat != CONSTANTS.SUBMETRIC_HEADER:\n diff_metric = reduce(defaultdict.__getitem__, [stats.split('.')[0], submetric, stat], self.diff_data)\n diff_metric[0] = float(report0_stats[submetric][stat])\n diff_metric[1] = float(report1_stats[submetric][stat])\n diff_metric['absolute_diff'] = naarad.utils.normalize_float_for_display(diff_metric[1] - diff_metric[0])\n if diff_metric[0] == 0:\n if diff_metric['absolute_diff'] == '0.0':\n diff_metric['percent_diff'] = 0.0\n else:\n diff_metric['percent_diff'] = 'N/A'\n else:\n diff_metric['percent_diff'] = naarad.utils.normalize_float_for_display((diff_metric[1] - diff_metric[0]) * 100 / diff_metric[0])\n # check whether there is a SLA failure\n if ((metric_label in self.sla_map.keys()) and (submetric in self.sla_map[metric_label].keys()) and\n (stat in self.sla_map[metric_label][submetric].keys())):\n self.check_sla(self.sla_map[metric_label][submetric][stat], diff_metric)\n else:\n return False\n self.plot_diff()\n diff_html = ''\n if self.diff_data:\n diff_html = self.generate_diff_html()\n client_html = self.generate_client_charting_page(self.reports[0].datasource)\n if diff_html != '':\n with open(os.path.join(self.output_directory, CONSTANTS.DIFF_REPORT_FILE), 'w') as diff_file:\n diff_file.write(diff_html)\n with open(os.path.join(self.output_directory, CONSTANTS.CLIENT_CHARTING_FILE), 'w') as client_file:\n client_file.write(client_html)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a timestamp from the raw epoch time based on the granularity preferences passed in.", "response": "def get_aggregation_timestamp(self, timestamp, granularity='second'):\n \"\"\"\n Return a timestamp from the raw epoch time based on the granularity preferences passed in.\n\n :param string timestamp: timestamp from the log line\n :param string granularity: aggregation granularity used for plots.\n :return: string aggregate_timestamp: timestamp used for metrics aggregation in all functions\n \"\"\"\n if granularity is None or granularity.lower() == 'none':\n return int(timestamp), 1\n elif granularity == 'hour':\n return (int(timestamp) / (3600 * 1000)) * 3600 * 1000, 3600\n elif granularity == 'minute':\n return (int(timestamp) / (60 * 1000)) * 60 * 1000, 60\n else:\n return (int(timestamp) / 1000) * 1000, 1"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\naggregating the count of data over time.", "response": "def aggregate_count_over_time(self, metric_store, groupby_name, aggregate_timestamp):\n \"\"\"\n Organize and store the count of data from the log line into the metric store by columnm, group name, timestamp\n\n :param dict metric_store: The metric store used to store all the parsed the log data\n :param string groupby_name: the group name that the log line belongs to\n :param string aggregate_timestamp: timestamp used for storing the raw data. This accounts for aggregation time period\n :return: None\n \"\"\"\n all_qps = metric_store['qps']\n qps = all_qps[groupby_name]\n if aggregate_timestamp in qps:\n qps[aggregate_timestamp] += 1\n else:\n qps[aggregate_timestamp] = 1\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef aggregate_values_over_time(self, metric_store, data, groupby_name, column_name, aggregate_timestamp):\n # To add overall_summary one\n if self.groupby:\n metric_data = reduce(defaultdict.__getitem__, [column_name, 'Overall_summary', aggregate_timestamp], metric_store)\n metric_data.append(float(data))\n metric_data = reduce(defaultdict.__getitem__, [column_name, groupby_name, aggregate_timestamp], metric_store)\n metric_data.append(float(data))\n return None", "response": "Aggregate the values of a column over a given time."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef average_values_for_plot(self, metric_store, data, averaging_factor):\n for column, groups_store in metric_store.items():\n for group, time_store in groups_store.items():\n for time_stamp, column_data in sorted(time_store.items()):\n if column in ['qps']:\n if self.groupby:\n data[self.get_csv(column, group)].append(','.join([str(time_stamp), str(column_data / float(averaging_factor))]))\n else:\n data[self.get_csv(column)].append(','.join([str(time_stamp), str(column_data / float(averaging_factor))]))\n else:\n if self.groupby:\n data[self.get_csv(column, group)].append(','.join([str(time_stamp), str(sum(map(float, column_data)) / float(len(column_data)))]))\n else:\n data[self.get_csv(column)].append(','.join([str(time_stamp), str(sum(map(float, column_data)) / float(len(column_data)))]))\n return None", "response": "Create the time series for the various metrics averaged over the aggregation period being used for plots\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef calc_key_stats(self, metric_store):\n stats_to_calculate = ['mean', 'std', 'min', 'max'] # TODO: get input from user\n percentiles_to_calculate = range(0, 100, 1) # TODO: get input from user\n for column, groups_store in metric_store.items():\n for group, time_store in groups_store.items():\n data = metric_store[column][group].values()\n if self.groupby:\n column_name = group + '.' + column\n else:\n column_name = column\n if column.startswith('qps'):\n self.calculated_stats[column_name], self.calculated_percentiles[column_name] = naarad.utils.calculate_stats(data, stats_to_calculate, percentiles_to_calculate)\n else:\n self.calculated_stats[column_name], self.calculated_percentiles[column_name] = naarad.utils.calculate_stats(list(heapq.merge(*data)), stats_to_calculate,\n percentiles_to_calculate)\n self.update_summary_stats(column_name)", "response": "Calculate stats such as percentile and mean\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef calculate_stats(self):\n metric_type = self.metric_type.split('-')[0]\n if metric_type in naarad.naarad_imports.metric_classes or metric_type in naarad.naarad_imports.aggregate_metric_classes:\n self.calculate_other_metric_stats()\n else:\n self.calculate_base_metric_stats()", "response": "Calculate stats with different function depending on the metric type"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nplots time series for all sub - metrics.", "response": "def plot_timeseries(self, graphing_library='matplotlib'):\n \"\"\"\n plot timeseries for sub-metrics\n \"\"\"\n if self.groupby:\n plot_data = {}\n # plot time series data for submetrics\n for out_csv in sorted(self.csv_files, reverse=True):\n csv_filename = os.path.basename(out_csv)\n transaction_name = \".\".join(csv_filename.split('.')[1:-1])\n if transaction_name in self.anomalies.keys():\n highlight_regions = self.anomalies[transaction_name]\n else:\n highlight_regions = None\n # The last element is .csv, don't need that in the name of the chart\n column = csv_filename.split('.')[-2]\n transaction_name = ' '.join(csv_filename.split('.')[1:-2])\n plot = PD(input_csv=out_csv, csv_column=1, series_name=transaction_name + '.' + column,\n y_label=column + ' (' + self.sub_metric_description[column] + ')', precision=None, graph_height=500, graph_width=1200, graph_type='line',\n highlight_regions=highlight_regions)\n if transaction_name in plot_data:\n plot_data[transaction_name].append(plot)\n else:\n plot_data[transaction_name] = [plot]\n for transaction in plot_data:\n graphed, div_file = Metric.graphing_modules[graphing_library].graph_data(plot_data[transaction], self.resource_directory, self.resource_path,\n self.label + '.' + transaction)\n if graphed:\n self.plot_files.append(div_file)\n else:\n graphed = False\n for out_csv in self.csv_files:\n csv_filename = os.path.basename(out_csv)\n transaction_name = \".\".join(csv_filename.split('.')[1:-1])\n if transaction_name in self.anomalies.keys():\n highlight_regions = self.anomalies[transaction_name]\n else:\n highlight_regions = None\n # The last element is .csv, don't need that in the name of the chart\n column = self.csv_column_map[out_csv]\n column = naarad.utils.sanitize_string(column)\n graph_title = '.'.join(csv_filename.split('.')[0:-1])\n if self.sub_metric_description and column in self.sub_metric_description.keys():\n graph_title += ' (' + self.sub_metric_description[column] + ')'\n if self.sub_metric_unit and column in self.sub_metric_unit.keys():\n plot_data = [PD(input_csv=out_csv, csv_column=1, series_name=graph_title, y_label=column + ' (' + self.sub_metric_unit[column] + ')',\n precision=None, graph_height=600, graph_width=1200, graph_type='line', highlight_regions=highlight_regions)]\n else:\n plot_data = [PD(input_csv=out_csv, csv_column=1, series_name=graph_title, y_label=column, precision=None, graph_height=600, graph_width=1200,\n graph_type='line', highlight_regions=highlight_regions)]\n graphed, div_file = Metric.graphing_modules[graphing_library].graph_data(plot_data, self.resource_directory, self.resource_path, graph_title)\n if graphed:\n self.plot_files.append(div_file)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks whether the given sub metric is in the important_sub_metrics list", "response": "def check_important_sub_metrics(self, sub_metric):\n \"\"\"\n check whether the given sub metric is in important_sub_metrics list\n \"\"\"\n if not self.important_sub_metrics:\n return False\n if sub_metric in self.important_sub_metrics:\n return True\n items = sub_metric.split('.')\n if items[-1] in self.important_sub_metrics:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nplot CDF for important sub-metrics", "response": "def plot_cdf(self, graphing_library='matplotlib'):\n \"\"\"\n plot CDF for important sub-metrics\n \"\"\"\n graphed = False\n for percentile_csv in self.percentiles_files:\n csv_filename = os.path.basename(percentile_csv)\n # The last element is .csv, don't need that in the name of the chart\n column = self.csv_column_map[percentile_csv.replace(\".percentiles.\", \".\")]\n if not self.check_important_sub_metrics(column):\n continue\n column = naarad.utils.sanitize_string(column)\n graph_title = '.'.join(csv_filename.split('.')[0:-1])\n if self.sub_metric_description and column in self.sub_metric_description.keys():\n graph_title += ' (' + self.sub_metric_description[column] + ')'\n if self.sub_metric_unit and column in self.sub_metric_unit.keys():\n plot_data = [PD(input_csv=percentile_csv, csv_column=1, series_name=graph_title, x_label='Percentiles',\n y_label=column + ' (' + self.sub_metric_unit[column] + ')', precision=None, graph_height=600, graph_width=1200, graph_type='line')]\n else:\n plot_data = [PD(input_csv=percentile_csv, csv_column=1, series_name=graph_title, x_label='Percentiles', y_label=column, precision=None,\n graph_height=600, graph_width=1200, graph_type='line')]\n graphed, div_file = Metric.graphing_modules[graphing_library].graph_data_on_the_same_graph(plot_data, self.resource_directory,\n self.resource_path, graph_title)\n if graphed:\n self.plot_files.append(div_file)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates a graph for this metric", "response": "def graph(self, graphing_library='matplotlib'):\n \"\"\"\n graph generates two types of graphs\n 'time': generate a time-series plot for all submetrics (the x-axis is a time series)\n 'cdf': generate a CDF plot for important submetrics (the x-axis shows percentiles)\n \"\"\"\n logger.info('Using graphing_library {lib} for metric {name}'.format(lib=graphing_library, name=self.label))\n self.plot_cdf(graphing_library)\n self.plot_timeseries(graphing_library)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef detect_anomaly(self):\n if not self.anomaly_detection_metrics or len(self.anomaly_detection_metrics) <= 0:\n return\n for submetric in self.anomaly_detection_metrics:\n csv_file = self.get_csv(submetric)\n if naarad.utils.is_valid_file(csv_file):\n detector = anomaly_detector.AnomalyDetector(csv_file)\n anomalies = detector.get_anomalies()\n if len(anomalies) <= 0:\n return\n self.anomalies[submetric] = anomalies\n anomaly_csv_file = os.path.join(self.resource_directory, self.label + '.' + submetric + '.anomalies.csv')\n with open(anomaly_csv_file, 'w') as FH:\n for anomaly in anomalies:\n FH.write(\",\".join([str(anomaly.anomaly_score), str(anomaly.start_timestamp), str(anomaly.end_timestamp), str(anomaly.exact_timestamp)]))\n FH.write('\\n')", "response": "Detect anomalies in the timeseries data for the specified submetrics."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_tuple(self, fields):\n v1 = ''\n v2 = ''\n if len(fields) > 0:\n v1 = fields[0]\n if len(fields) > 1:\n v2 = fields[1]\n return v1, v2", "response": "returns a tuple with default values of '';"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _extract_input_connections(self):\n for con in self.connections:\n ends = con.strip().split('<->') # [host1:port1->host2]\n ends = filter(None, ends) # Remove '' elements\n if len(ends) == 0:\n continue\n if len(ends) > 0:\n host1, port1 = self._get_tuple(ends[0].split(':'))\n host2 = ''\n port2 = ''\n if len(ends) > 1:\n host2, port2 = self._get_tuple(ends[1].split(':'))\n self.input_connections.append((host1, port1, host2, port2))", "response": "Given a user input of interested connections extract the info and output a list of tuples."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _extract_input_processes(self):\n for proc in self.processes:\n ends = proc.split('/')\n pid, name = self._get_tuple(ends)\n self.input_processes.append((pid, name))", "response": "Given user input of interested processes extract the info and output a list of tuples."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndetermine whether host and port matches current connection.", "response": "def _match_host_port(self, host, port, cur_host, cur_port):\n \"\"\"\n Determine whether user-specified (host,port) matches current (cur_host, cur_port)\n :param host,port: The user input of (host,port)\n :param cur_host, cur_port: The current connection\n :return: True or Not\n \"\"\"\n # if host is '', true; if not '', it should prefix-match cur_host\n host_match = False\n if not host:\n host_match = True\n elif cur_host.startswith(host): # allow for partial match\n host_match = True\n\n # if port is '', true; if not '', it should exactly match cur_port\n port_match = False\n if not port:\n port_match = True\n elif port == cur_port:\n port_match = True\n\n return host_match and port_match"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _match_processes(self, pid, name, cur_process):\n cur_pid, cur_name = self._get_tuple(cur_process.split('/'))\n\n pid_match = False\n if not pid:\n pid_match = True\n elif pid == cur_pid:\n pid_match = True\n\n name_match = False\n if not name:\n name_match = True\n elif name == cur_name:\n name_match = True\n\n return pid_match and name_match", "response": "Determines whether the user - specified pid and name of the processes are in the current process."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck whether the connection is of interest or not.", "response": "def _check_connection(self, local_end, remote_end, process):\n \"\"\"\n Check whether the connection is of interest or not\n :param local_end: Local connection end point, e.g., 'host1:port1'\n :param remote_end: Remote connection end point, e.g., 'host2:port2'\n :param process: Current connection 's process info, e.g., '1234/firefox'\n :return: a tuple of (local_end, remote_end, True/False); e.g. ('host1_23232', 'host2_2222', True)\n \"\"\"\n # check tcp end points\n cur_host1, cur_port1 = self._get_tuple(local_end.split(':'))\n cur_host2, cur_port2 = self._get_tuple(remote_end.split(':'))\n\n # check whether the connection is interested or not by checking user input\n host_port_is_interested = False\n for (host1, port1, host2, port2) in self.input_connections:\n if self._match_host_port(host1, port1, cur_host1, cur_port1) and self._match_host_port(host2, port2, cur_host2, cur_port2):\n host_port_is_interested = True\n break\n if self._match_host_port(host1, port1, cur_host2, cur_port2) and self._match_host_port(host2, port2, cur_host1, cur_port1):\n host_port_is_interested = True\n break\n\n # check whether the connection is interested or not by checking process names given in the config\n process_is_interested = False\n for pid, name in self.input_processes:\n if self._match_processes(pid, name, process):\n process_is_interested = True\n break\n\n return cur_host1 + '_' + cur_port1, cur_host2 + '_' + cur_port2, host_port_is_interested and process_is_interested"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding the data point to the dictionary of data", "response": "def _add_data_line(self, data, col, value, ts):\n \"\"\"\n Append the data point to the dictionary of \"data\"\n :param data: The dictionary containing all data\n :param col: The sub-metric name e.g. 'host1_port1.host2_port2.SendQ'\n :param value: integer\n :param ts: timestamp\n :return: None\n \"\"\"\n if col in self.column_csv_map:\n out_csv = self.column_csv_map[col]\n else:\n out_csv = self.get_csv(col) # column_csv_map[] is assigned in get_csv()\n data[out_csv] = []\n data[out_csv].append(ts + \",\" + value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing the netstat output file and return the status of the metric parse", "response": "def parse(self):\n \"\"\"\n Parse the netstat output file\n :return: status of the metric parse\n \"\"\"\n # sample netstat output: 2014-04-02 15:44:02.86612\ttcp 9600 0 host1.localdomain.com.:21567 remote.remotedomain.com:51168 ESTABLISH pid/process\n data = {} # stores the data of each sub-metric\n for infile in self.infile_list:\n logger.info('Processing : %s', infile)\n timestamp_format = None\n with open(infile) as fh:\n for line in fh:\n if 'ESTABLISHED' not in line:\n continue\n words = line.split()\n if len(words) < 8 or words[2] != 'tcp':\n continue\n ts = words[0] + \" \" + words[1]\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(ts)\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(ts, timestamp_format)\n if self.ts_out_of_range(ts):\n continue\n # filtering based on user input; (local socket, remote socket, pid/process)\n local_end, remote_end, interested = self._check_connection(words[5], words[6], words[8])\n if interested:\n self._add_data_line(data, local_end + '.' + remote_end + '.RecvQ', words[3], ts)\n self._add_data_line(data, local_end + '.' + remote_end + '.SendQ', words[4], ts)\n # post processing, putting data in csv files;\n for csv in data.keys():\n self.csv_files.append(csv)\n with open(csv, 'w') as fh:\n fh.write('\\n'.join(sorted(data[csv])))\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_csv(self, cpu, device=None):\n cpu = naarad.utils.sanitize_string(cpu)\n if device is None:\n outcsv = os.path.join(self.resource_directory, \"{0}.{1}.csv\".format(self.label, cpu))\n self.csv_column_map[outcsv] = cpu\n else:\n device = naarad.utils.sanitize_string(device)\n outcsv = os.path.join(self.resource_directory, \"{0}.{1}.{2}.csv\".format(self.label, cpu, device))\n self.csv_column_map[outcsv] = cpu + '.' + device\n return outcsv", "response": "Returns the CSV file related to the given metric."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef find_header(self, infile):\n cpus = []\n for line in infile: # Pre-processing - Try to find header\n if not self.is_header_line(line):\n continue\n # Verifying correctness of the header\n cpu_header = line.split()\n for cpu_h in cpu_header[2:]:\n if not cpu_h.startswith('CPU'):\n cpus = [] # Bad header so reset to nothing\n break\n else:\n cpus.append(cpu_h)\n if len(cpus) > 0: # We found the header\n break\n return cpus", "response": "Parses the file and tries to find the header line."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses the input files and returns a new set of data structures.", "response": "def parse(self):\n \"\"\"\n Processes the files for each IRQ and each CPU in terms of the differences.\n Also produces accumulated interrupt count differences for each set of Ethernet IRQs.\n Generally Ethernet has 8 TxRx IRQs thus all are combined so that one can see the overall interrupts being generated by the NIC.\n\n Simplified Interrupt File Format: (See examples for example log)\n\n CPU0 CPU1\n 2014-10-29 00:27:42.15161 59: 29 2 IR-IO-APIC-edge timer\n 2014-10-29 00:27:42.15161 60: 2123 0 IR-PCI-MSI-edge eth0\n\n CPU0 CPU1\n 2014-10-29 00:27:42.15161 59: 29 2 IR-IO-APIC-edge timer\n 2014-10-29 00:27:42.15161 60: 2123 0 IR-PCI-MSI-edge eth0\n\n :returns: True or False whether parsing was successful or not.\n \"\"\"\n if not os.path.isdir(self.outdir):\n os.makedirs(self.outdir)\n if not os.path.isdir(self.resource_directory):\n os.makedirs(self.resource_directory)\n\n data = {}\n for input_file in self.infile_list:\n logger.info('Processing : %s', input_file)\n timestamp_format = None\n with open(input_file, 'r') as infile:\n # Get the header for this file\n cpus = self.find_header(infile)\n if len(cpus) == 0: # Make sure we have header otherwise go to next file\n logger.error(\"Header not found for file: %s\", input_file)\n continue\n\n # Parse the actual file after header\n prev_data = None # Stores the previous interval's log data\n curr_data = {} # Stores the current interval's log data\n eth_data = {}\n for line in infile:\n if self.is_header_line(line): # New section so save old and aggregate ETH\n prev_data = curr_data\n curr_data = {}\n # New section so store the collected Ethernet data\n # Example Aggregate metric: PROCINTERRUPTS.AGGREGATE.eth0\n for eth in eth_data:\n outcsv = self.get_csv('AGGREGATE', eth)\n if outcsv not in data:\n data[outcsv] = []\n data[outcsv].append(ts + ',' + str(eth_data[eth]))\n eth_data = {}\n continue\n\n words = line.split()\n if len(words) <= 4: # Does not have any CPU data so skip\n continue\n\n # Process timestamp or determine timestamp\n ts = words[0] + \" \" + words[1]\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(ts)\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(ts, timestamp_format)\n if self.ts_out_of_range(ts): # See if time is in range\n continue\n\n # Process data lines\n # Note that some IRQs such as ERR and MIS do not have device nor ascii name\n device = words[2].strip(':') # Get IRQ Number/Name\n if re.match(\"\\d+\", device):\n # Devices with digits need ASCII name if exists\n if (4 + len(cpus)) < len(words):\n device = words[4 + len(cpus)] + \"-IRQ\" + device\n else:\n device = \"IRQ\" + device\n else:\n # For devices with IRQ # that aren't digits then has description\n device = \"-\".join(words[(3 + len(cpus)):]) + \"-IRQ\" + device\n\n # Deal with each column worth of data\n for (cpu, datum) in zip(cpus, words[3:]):\n if self.CPUS and cpu not in self.CPUS: # Skip if config defines which CPUs to look at\n continue\n outcsv = self.get_csv(cpu, device)\n curr_data[outcsv] = int(datum)\n if outcsv in data:\n datum = int(datum) - prev_data[outcsv] # prev_data exists since outcsv exists in data\n else:\n data[outcsv] = []\n datum = 0 # First data point is set to 0\n # Store data point\n data[outcsv].append(ts + ',' + str(datum))\n\n # Deal with accumulating aggregate data for Ethernet\n m = re.search(\"(?Peth\\d)\", device)\n if m:\n eth = m.group('eth')\n if eth not in eth_data:\n eth_data[eth] = 0\n eth_data[eth] += datum\n\n # Post processing, putting data in csv files\n for csv in data.keys():\n self.csv_files.append(csv)\n with open(csv, 'w') as csvf:\n csvf.write('\\n'.join(sorted(data[csv])))\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses the vmstat file and return the status of the metric parse", "response": "def parse(self):\n \"\"\"\n Parse the vmstat file\n :return: status of the metric parse\n \"\"\"\n file_status = True\n for input_file in self.infile_list:\n file_status = file_status and naarad.utils.is_valid_file(input_file)\n if not file_status:\n return False\n status = True\n data = {} # stores the data of each column\n for input_file in self.infile_list:\n logger.info('Processing : %s', input_file)\n timestamp_format = None\n with open(input_file) as fh:\n for line in fh:\n words = line.split() # [0] is day; [1] is seconds; [2] is field name:; [3] is value [4] is unit\n if len(words) < 3:\n continue\n ts = words[0] + \" \" + words[1]\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(ts)\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(ts, timestamp_format)\n if self.ts_out_of_range(ts):\n continue\n col = words[2].strip(':')\n # only process sub_metrics specified in config.\n if self.sub_metrics and col not in self.sub_metrics:\n continue\n # add unit to metric description; most of the metrics have 'KB'; a few others do not have unit, they are in number of pages\n if len(words) > 4 and words[4]:\n unit = words[4]\n else:\n unit = 'pages'\n self.sub_metric_unit[col] = unit\n # stores the values in data[] before finally writing out\n if col in self.column_csv_map:\n out_csv = self.column_csv_map[col]\n else:\n out_csv = self.get_csv(col) # column_csv_map[] is assigned in get_csv()\n data[out_csv] = []\n data[out_csv].append(ts + \",\" + words[3])\n # post processing, putting data in csv files;\n for csv in data.keys():\n self.csv_files.append(csv)\n with open(csv, 'w') as fh:\n fh.write('\\n'.join(sorted(data[csv])))\n return status"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_innotop_mode_m(self):\n with open(self.infile, 'r') as infh:\n # Pre processing to figure out different headers\n max_row_quot = 0\n valrow = -1\n thisrowcolumns = {}\n data = {}\n last_ts = None\n while True:\n # 2012-05-11T00:00:02 master_host slave_sql_running time_behind_master slave_catchup_rate slave_open_temp_tables relay_log_pos last_error\n line1 = infh.readline()\n words = line1.split()\n # Skip next line\n infh.readline()\n is_header = True\n for word in words:\n if naarad.utils.is_number(word):\n last_ts = words[0].strip().replace('T', ' ')\n is_header = False\n break # from this loop\n if len(words) > 2 and is_header:\n thisrowcolumns[max_row_quot] = words[2:]\n for column in thisrowcolumns[max_row_quot]:\n data[column] = []\n max_row_quot += 1\n else:\n break\n # from pre-processing. All headers accounted for\n\n # Real Processing\n if not last_ts:\n logger.warn(\"last_ts not set, looks like there is no data in file %s\", self.infile)\n return True\n infh.seek(0)\n is_bad_line = False\n outfilehandlers = {}\n for line in infh:\n l = line.strip().split(' ', 1)\n # Blank line\n if len(l) <= 1:\n continue\n ts = l[0].strip().replace('T', ' ')\n if ts != last_ts:\n last_ts = ts\n valrow = -1\n nameval = l[1].strip().split('\\t', 1)\n try:\n words = nameval[1].split('\\t')\n except IndexError:\n logger.warn(\"Bad line: %s\", line)\n continue\n valrow += 1\n command = nameval[0]\n if command not in outfilehandlers:\n outfilehandlers[command] = {}\n quot = valrow % max_row_quot\n columns = thisrowcolumns[quot]\n for i in range(len(words)):\n if len(words) > len(columns):\n logger.warn(\"Mismatched number of columns: %s\", line)\n logger.warn(\"%d %d\", len(words), len(columns))\n break\n if words[i] in columns:\n logger.warn(\"Skipping line: %s\", line)\n valrow -= 1\n break\n if self.options and columns[i] not in self.options:\n continue\n if columns[i] not in outfilehandlers[command]:\n outfilehandlers[command][columns[i]] = open(self.get_csv_C(command, columns[i]), 'w')\n self.csv_files.append(self.get_csv_C(command, columns[i]))\n ts = naarad.utils.reconcile_timezones(ts, self.timezone, self.graph_timezone)\n outfilehandlers[command][columns[i]].write(ts + ',')\n outfilehandlers[command][columns[i]].write(words[i])\n outfilehandlers[command][columns[i]].write('\\n')\n for command in outfilehandlers:\n for column in outfilehandlers[command]:\n outfilehandlers[command][column].close()\n return True", "response": "Special parsing method for Innotop Replication Status results ( innotop mode M"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nhighlight a region on the matplotlib plot between start and end x - co -ordinates.", "response": "def highlight_region(plt, start_x, end_x):\n \"\"\"\n Highlight a region on the chart between the specified start and end x-co-ordinates.\n param pyplot plt: matplotlibk pyplot which contains the charts to be highlighted\n param string start_x : epoch time millis\n param string end_x : epoch time millis\n \"\"\"\n start_x = convert_to_mdate(start_x)\n end_x = convert_to_mdate(end_x)\n plt.axvspan(start_x, end_x, color=CONSTANTS.HIGHLIGHT_COLOR, alpha=CONSTANTS.HIGHLIGHT_ALPHA)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef graph_data_on_the_same_graph(list_of_plots, output_directory, resource_path, output_filename):\n maximum_yvalue = -float('inf')\n minimum_yvalue = float('inf')\n plots = curate_plot_list(list_of_plots)\n plot_count = len(plots)\n if plot_count == 0:\n return False, None\n graph_height, graph_width, graph_title = get_graph_metadata(plots)\n current_plot_count = 0\n fig, axis = plt.subplots()\n fig.set_size_inches(graph_width, graph_height)\n if plot_count < 2:\n fig.subplots_adjust(left=CONSTANTS.SUBPLOT_LEFT_OFFSET, bottom=CONSTANTS.SUBPLOT_BOTTOM_OFFSET, right=CONSTANTS.SUBPLOT_RIGHT_OFFSET)\n else:\n fig.subplots_adjust(left=CONSTANTS.SUBPLOT_LEFT_OFFSET, bottom=CONSTANTS.SUBPLOT_BOTTOM_OFFSET,\n right=CONSTANTS.SUBPLOT_RIGHT_OFFSET - CONSTANTS.Y_AXIS_OFFSET * (plot_count - 2))\n # Generate each plot on the graph\n for plot in plots:\n current_plot_count += 1\n logger.info('Processing: ' + plot.input_csv + ' [ ' + output_filename + ' ]')\n xval, yval = numpy.loadtxt(plot.input_csv, unpack=True, delimiter=',')\n axis.plot(xval, yval, linestyle='-', marker=None, color=get_current_color(current_plot_count), label=plot.plot_label)\n axis.legend()\n maximum_yvalue = max(maximum_yvalue, numpy.amax(yval) * (1.0 + CONSTANTS.ZOOM_FACTOR * current_plot_count))\n minimum_yvalue = min(minimum_yvalue, numpy.amin(yval) * (1.0 - CONSTANTS.ZOOM_FACTOR * current_plot_count))\n # Set properties of the plots\n axis.yaxis.set_ticks_position('left')\n axis.set_xlabel(plots[0].x_label)\n axis.set_ylabel(plots[0].y_label, fontsize=CONSTANTS.Y_LABEL_FONTSIZE)\n axis.set_ylim([minimum_yvalue, maximum_yvalue])\n axis.yaxis.grid(True)\n axis.xaxis.grid(True)\n axis.set_title(graph_title)\n plot_file_name = os.path.join(output_directory, output_filename + \".png\")\n fig.savefig(plot_file_name)\n plt.close()\n # Create html fragment to be used for creation of the report\n with open(os.path.join(output_directory, output_filename + '.div'), 'w') as div_file:\n div_file.write('

' + os.path.basename(plot_file_name) + '

')\n return True, os.path.join(output_directory, output_filename + '.div')", "response": "This function will plot a list of plots on the same graph."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute anomaly scores for the time series.", "response": "def _set_scores(self):\n \"\"\"\n Compute anomaly scores for the time series.\n \"\"\"\n anom_scores = {}\n self._compute_derivatives()\n derivatives_ema = utils.compute_ema(self.smoothing_factor, self.derivatives)\n for i, (timestamp, value) in enumerate(self.time_series_items):\n anom_scores[timestamp] = abs(self.derivatives[i] - derivatives_ema[i])\n stdev = numpy.std(anom_scores.values())\n if stdev:\n for timestamp in anom_scores.keys():\n anom_scores[timestamp] /= stdev\n self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_metric_name(self, metric_name):\n for metric_type in self.supported_sar_types:\n if metric_type in metric_name:\n return metric_type\n logger.error('Section [%s] does not contain a valid metric type, using type: \"SAR-generic\". Naarad works better '\n 'if it knows the metric type. Valid SAR metric names are: %s', metric_name, self.supported_sar_types)\n return 'SAR-generic'", "response": "Method to extract the SAR metric name from the given section name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _find_allowed_shift(self, timestamps):\n init_ts = timestamps[0]\n residual_timestamps = map(lambda ts: ts - init_ts, timestamps)\n n = len(residual_timestamps)\n return self._find_first_bigger(residual_timestamps, self.max_shift_milliseconds, 0, n)", "response": "Find the maximum allowed shift steps based on max_shift_milliseconds."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _find_first_bigger(self, timestamps, target, lower_bound, upper_bound):\n while lower_bound < upper_bound:\n pos = lower_bound + (upper_bound - lower_bound) / 2\n if timestamps[pos] > target:\n upper_bound = pos\n else:\n lower_bound = pos + 1\n return pos", "response": "This function searches for the first element in timestamps whose value is bigger than target."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_analysis(self, config):\n self._default_test_id += 1\n self._analyses[self._default_test_id] = _Analysis(ts_start=None, config=config, test_id=self._default_test_id)", "response": "Create Analysis and save in Naarad from config\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninitialize an analysis object and set ts_start for the analysis represented by test_id", "response": "def signal_start(self, config, test_id=None, **kwargs):\n \"\"\"\n Initialize an analysis object and set ts_start for the analysis represented by test_id\n :param test_id: integer that represents the analysis\n :param config: config can be a ConfigParser.ConfigParser object or a string specifying local or http(s) location\n for config\n :return: test_id\n \"\"\"\n if not test_id:\n self._default_test_id += 1\n test_id = self._default_test_id\n self._analyses[test_id] = _Analysis(naarad.utils.get_standardized_timestamp('now', None), config,\n test_id=test_id)\n if kwargs:\n if 'description' in kwargs.keys():\n self._analyses[test_id].description = kwargs['description']\n if 'input_directory' in kwargs.keys():\n self._analyses[test_id].input_directory = kwargs['input_directory']\n if 'output_directory' in kwargs.keys():\n self._analyses[test_id].output_directory = kwargs['output_directory']\n return test_id"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef signal_stop(self, test_id=None):\n if test_id is None:\n test_id = self._default_test_id\n if self._analyses[test_id].ts_end:\n return CONSTANTS.OK\n self._analyses[test_id].ts_end = naarad.utils.get_standardized_timestamp('now', None)\n return CONSTANTS.OK", "response": "Signal that the analysis is stopped."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of test_ids for which naarad analysis failed", "response": "def get_failed_analyses(self):\n \"\"\"\n Returns a list of test_id for which naarad analysis failed\n :return: list of test_ids\n \"\"\"\n failed_analyses = []\n for test_id in self._analyses.keys():\n if self._analyses[test_id].status != CONSTANTS.OK:\n failed_analyses.append(test_id)\n return failed_analyses"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the sla data for each metric in the metrics list", "response": "def _set_sla_data(self, test_id, metrics):\n \"\"\"\n Get sla data from each metric and set it in the _Analysis object specified by test_id to make it available\n for retrieval\n :return: currently always returns CONSTANTS.OK. Maybe enhanced in future to return additional status\n \"\"\"\n for metric in metrics:\n self._analyses[test_id].sla_data[metric.label] = metric.sla_map\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the summary stats data for each metric in the metrics list", "response": "def _set_stats_data(self, test_id, metrics):\n \"\"\"\n Get summary stats data from each metric and set it in the _Analysis object specified by test_id to make it available\n for retrieval\n :return: currently always returns CONSTANTS.OK. Maybe enhanced in future to return additional status\n \"\"\"\n for metric in metrics:\n self._analyses[test_id].stats_data[metric.label] = metric.summary_stats\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _create_output_directories(self, analysis):\n try:\n os.makedirs(analysis.output_directory)\n except OSError as exception:\n if exception.errno != errno.EEXIST:\n raise\n try:\n resource_directory = os.path.join(analysis.output_directory, analysis.resource_path)\n os.makedirs(resource_directory)\n except OSError as exception:\n if exception.errno != errno.EEXIST:\n raise", "response": "Create the necessary output and resource directories for the specified analysis."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute any pre run steps in the config.", "response": "def _run_pre(self, analysis, run_steps):\n \"\"\"\n If Naarad is run in CLI mode, execute any pre run steps specified in the config. ts_start/ts_end are set based on\n workload run steps if any.\n :param: analysis: The analysis object being processed\n :param: run_steps: list of post run steps\n \"\"\"\n workload_run_steps = []\n for run_step in sorted(run_steps, key=lambda step: step.run_rank):\n run_step.run()\n if run_step.run_type == CONSTANTS.RUN_TYPE_WORKLOAD:\n workload_run_steps.append(run_step)\n # Get analysis time period from workload run steps\n if len(workload_run_steps) > 0:\n analysis.ts_start, analysis.ts_end = naarad.utils.get_run_time_period(workload_run_steps)\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes any post run steps specified in the config", "response": "def _run_post(self, run_steps):\n \"\"\"\n If Naarad is run in CLI mode, execute any post run steps specified in the config\n :param: run_steps: list of post run steps\n \"\"\"\n for run_step in sorted(run_steps, key=lambda step: step.run_rank):\n run_step.run()\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _process_args(self, analysis, args):\n if args.exit_code:\n self.return_exit_code = args.exit_code\n if args.no_plots:\n self.skip_plots = args.no_plots\n if args.start:\n analysis.ts_start = naarad.utils.get_standardized_timestamp(args.start, None)\n if args.end:\n analysis.ts_end = naarad.utils.get_standardized_timestamp(args.end, None)\n if args.variables:\n analysis.variables = naarad.utils.get_variables(args)\n return CONSTANTS.OK", "response": "Process the command line arguments received by Naarad and update the analysis object with the values received by the analysis."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nruns all the analyses in the log files and save the results in the output_directory.", "response": "def analyze(self, input_directory, output_directory, **kwargs):\n \"\"\"\n Run all the analysis saved in self._analyses, sorted by test_id.\n This is useful when Naarad() is used by other programs and multiple analyses are run\n In naarad CLI mode, len(_analyses) == 1\n :param: input_directory: location of log files\n :param: output_directory: root directory for analysis output\n :param: **kwargs: Optional keyword args\n :return: int: status code.\n \"\"\"\n is_api_call = True\n if len(self._analyses) == 0:\n if 'config' not in kwargs.keys():\n return CONSTANTS.ERROR\n self.create_analysis(kwargs['config'])\n if 'args' in kwargs:\n self._process_args(self._analyses[0], kwargs['args'])\n is_api_call = False\n error_count = 0\n self._input_directory = input_directory\n self._output_directory = output_directory\n for test_id in sorted(self._analyses.keys()):\n # Setup\n if not self._analyses[test_id].input_directory:\n self._analyses[test_id].input_directory = input_directory\n if not self._analyses[test_id].output_directory:\n if len(self._analyses) > 1:\n self._analyses[test_id].output_directory = os.path.join(output_directory, str(test_id))\n else:\n self._analyses[test_id].output_directory = output_directory\n if('config' in kwargs.keys()) and (not self._analyses[test_id].config):\n self._analyses[test_id].config = kwargs['config']\n self._create_output_directories(self._analyses[test_id])\n # Actually run analysis\n self._analyses[test_id].status = self.run(self._analyses[test_id], is_api_call, **kwargs)\n if self._analyses[test_id].status != CONSTANTS.OK:\n error_count += 1\n if len(self._analyses) == 1:\n return self._analyses[0].status\n elif error_count > 0:\n return CONSTANTS.ERROR\n else:\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrunning the analysis object.", "response": "def run(self, analysis, is_api_call, **kwargs):\n \"\"\"\n :param analysis: Run naarad analysis for the specified analysis object\n :param **kwargs: Additional keyword args can be passed in here for future enhancements\n :return:\n \"\"\"\n threads = []\n crossplots = []\n report_args = {}\n metrics = defaultdict()\n run_steps = defaultdict(list)\n discovery_mode = False\n graph_timezone = None\n graphing_library = None\n\n if isinstance(analysis.config, str):\n if not naarad.utils.is_valid_file(analysis.config):\n return CONSTANTS.INVALID_CONFIG\n config_object = ConfigParser.ConfigParser(analysis.variables)\n config_object.optionxform = str\n config_object.read(analysis.config)\n elif isinstance(analysis.config, ConfigParser.ConfigParser):\n config_object = analysis.config\n else:\n if is_api_call:\n return CONSTANTS.INVALID_CONFIG\n else:\n metrics['metrics'] = naarad.utils.discover_by_name(analysis.input_directory, analysis.output_directory)\n if len(metrics['metrics']) == 0:\n logger.warning('Unable to auto detect metrics in the specified input directory: %s', analysis.input_directory)\n return CONSTANTS.ERROR\n else:\n discovery_mode = True\n metrics['aggregate_metrics'] = []\n if not discovery_mode:\n metrics, run_steps, crossplots, report_args, graph_timezone, graphing_library = self._process_naarad_config(config_object, analysis)\n\n if graphing_library is None:\n graphing_library = CONSTANTS.DEFAULT_GRAPHING_LIBRARY\n # If graphing libraries are not installed, skip static images\n if graphing_library not in self.available_graphing_modules.keys():\n logger.error(\"Naarad cannot import graphing library %s on your system. Will not generate static charts\", graphing_library)\n self.skip_plots = True\n\n if not is_api_call:\n self._run_pre(analysis, run_steps['pre'])\n for metric in metrics['metrics']:\n if analysis.ts_start:\n metric.ts_start = analysis.ts_start\n if analysis.ts_end:\n metric.ts_end = analysis.ts_end\n thread = threading.Thread(target=naarad.utils.parse_and_plot_single_metrics,\n args=(metric, graph_timezone, analysis.output_directory, analysis.input_directory, graphing_library, self.skip_plots))\n thread.start()\n threads.append(thread)\n for t in threads:\n t.join()\n for metric in metrics['aggregate_metrics']:\n thread = threading.Thread(target=naarad.utils.parse_and_plot_single_metrics,\n args=(metric, graph_timezone, analysis.output_directory, analysis.input_directory, graphing_library, self.skip_plots))\n thread.start()\n threads.append(thread)\n for t in threads:\n t.join()\n self._set_sla_data(analysis.test_id, metrics['metrics'] + metrics['aggregate_metrics'])\n self._set_stats_data(analysis.test_id, metrics['metrics'] + metrics['aggregate_metrics'])\n if len(crossplots) > 0 and not self.skip_plots:\n correlated_plots = naarad.utils.nway_plotting(crossplots, metrics['metrics'] + metrics['aggregate_metrics'],\n os.path.join(analysis.output_directory, analysis.resource_path),\n analysis.resource_path, graphing_library)\n else:\n correlated_plots = []\n rpt = reporting_modules['report'](None, analysis.output_directory, os.path.join(analysis.output_directory, analysis.resource_path), analysis.resource_path,\n metrics['metrics'] + metrics['aggregate_metrics'], correlated_plots=correlated_plots, **report_args)\n rpt.generate()\n if not is_api_call:\n self._run_post(run_steps['post'])\n\n if self.return_exit_code:\n for metric in metrics['metrics'] + metrics['aggregate_metrics']:\n if metric.status == CONSTANTS.SLA_FAILED:\n return CONSTANTS.SLA_FAILURE\n\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef diff(self, test_id_1, test_id_2, config=None, **kwargs):\n output_directory = os.path.join(self._output_directory, 'diff_' + str(test_id_1) + '_' + str(test_id_2))\n if kwargs:\n if 'output_directory' in kwargs.keys():\n output_directory = kwargs['output_directory']\n diff_report = Diff([NaaradReport(self._analyses[test_id_1].output_directory, None),\n NaaradReport(self._analyses[test_id_2].output_directory, None)],\n 'diff', output_directory, os.path.join(output_directory, self._resource_path),\n self._resource_path)\n if config:\n naarad.utils.extract_diff_sla_from_config_file(diff_report, config)\n diff_report.generate()\n if diff_report.sla_failures > 0:\n return CONSTANTS.SLA_FAILURE\n if diff_report.status != 'OK':\n return CONSTANTS.ERROR\n return CONSTANTS.OK", "response": "Creates a diff report using test_id_1 and test_id_2 as a baseline\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a diff report using report1 and report2 as a baseline", "response": "def diff_reports_by_location(self, report1_location, report2_location, output_directory, config=None, **kwargs):\n \"\"\"\n Create a diff report using report1 as a baseline\n :param: report1_location: report to be used as baseline\n :param: report2_location: report to compare against baseline\n :param: config file for diff (optional)\n :param: **kwargs: keyword arguments\n \"\"\"\n\n if kwargs:\n if 'output_directory' in kwargs.keys():\n output_directory = kwargs['output_directory']\n diff_report = Diff([NaaradReport(report1_location, None), NaaradReport(report2_location, None)], 'diff',\n output_directory, os.path.join(output_directory, self._resource_path), self._resource_path)\n if config:\n naarad.utils.extract_diff_sla_from_config_file(diff_report, config)\n diff_report.generate()\n if diff_report.sla_failures > 0:\n return CONSTANTS.SLA_FAILURE\n if diff_report.status != 'OK':\n return CONSTANTS.ERROR\n return CONSTANTS.OK"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _process_naarad_config(self, config, analysis):\n graph_timezone = None\n output_directory = analysis.output_directory\n resource_path = analysis.resource_path\n run_steps = defaultdict(list)\n metrics = defaultdict(list)\n indir_default = ''\n crossplots = []\n report_args = {}\n graphing_library = None\n ts_start, ts_end = None, None\n\n if config.has_section('GLOBAL'):\n ts_start, ts_end = naarad.utils.parse_global_section(config, 'GLOBAL')\n if config.has_option('GLOBAL', 'user_defined_metrics'):\n naarad.utils.parse_user_defined_metric_classes(config, metric_classes)\n config.remove_section('GLOBAL')\n\n if config.has_section('REPORT'):\n report_args = naarad.utils.parse_report_section(config, 'REPORT')\n config.remove_section('REPORT')\n\n for section in config.sections():\n # GRAPH section is optional\n if section == 'GRAPH':\n graphing_library, crossplots, outdir_default, indir_default, graph_timezone = \\\n naarad.utils.parse_graph_section(config, section, output_directory, indir_default)\n elif section.startswith('RUN-STEP'):\n run_step = naarad.utils.parse_run_step_section(config, section)\n if not run_step:\n logger.error('Ignoring section %s, could not parse it correctly', section)\n continue\n if run_step.run_order == CONSTANTS.PRE_ANALYSIS_RUN:\n run_steps['pre'].append(run_step)\n # DURING_ANALYSIS_RUN not supported yet\n elif run_step.run_order == CONSTANTS.DURING_ANALYSIS_RUN:\n run_steps['in'].append(run_step)\n elif run_step.run_order == CONSTANTS.POST_ANALYSIS_RUN:\n run_steps['post'].append(run_step)\n else:\n logger.error('Unknown RUN-STEP run_order specified')\n else:\n # section name is used to create sub-directories, so enforce it.\n if not naarad.utils.is_valid_metric_name(section):\n logger.critical('Section name %s is invalid! Only letters, digits, dot(.), dash(-), underscore(_) are allowed'\n % section)\n return CONSTANTS.CRITICAL_FAILURE\n if section == 'SAR-*':\n hostname, infile, label, ts_start, ts_end, precision, kwargs, rule_strings = \\\n naarad.utils.parse_basic_metric_options(config, section)\n sar_metrics = naarad.utils.get_all_sar_objects(metrics, infile, hostname, output_directory, label, ts_start,\n ts_end, None)\n for sar_metric in sar_metrics:\n if sar_metric.ts_start is None and (sar_metric.ts_end is None or sar_metric.ts_end > ts_start):\n sar_metric.ts_start = ts_start\n if sar_metric.ts_end is None and (sar_metric.ts_start is None or ts_end > sar_metric.ts_start):\n sar_metric.ts_end = ts_end\n metrics['metrics'].extend(sar_metrics)\n else:\n new_metric = naarad.utils.parse_metric_section(config, section, metric_classes, metrics['metrics'],\n aggregate_metric_classes, output_directory, resource_path)\n if new_metric.ts_start is None and (new_metric.ts_end is None or new_metric.ts_end > ts_start):\n new_metric.ts_start = ts_start\n if new_metric.ts_end is None and (new_metric.ts_start is None or ts_end > new_metric.ts_start):\n new_metric.ts_end = ts_end\n metric_type = section.split('-')[0]\n if metric_type in aggregate_metric_classes:\n metrics['aggregate_metrics'].append(new_metric)\n else:\n metrics['metrics'].append(new_metric)\n return metrics, run_steps, crossplots, report_args, graph_timezone, graphing_library", "response": "Processes the config file associated with a particular analysis and returns metrics run_steps and crossplots."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the vmstat file and return the status of the metric parse", "response": "def parse(self):\n \"\"\"\n Parse the vmstat file\n :return: status of the metric parse\n \"\"\"\n file_status = True\n for input_file in self.infile_list:\n file_status = file_status and naarad.utils.is_valid_file(input_file)\n if not file_status:\n return False\n\n status = True\n cur_zone = None\n cur_submetric = None\n cur_value = None\n data = {} # stores the data of each column\n for input_file in self.infile_list:\n logger.info('Processing : %s', input_file)\n timestamp_format = None\n with open(input_file) as fh:\n for line in fh:\n words = line.replace(',', ' ').split() # [0] is day; [1] is seconds; [2...] is field names:;\n if len(words) < 3:\n continue\n ts = words[0] + \" \" + words[1]\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(ts)\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(ts, timestamp_format)\n if self.ts_out_of_range(ts):\n continue\n if words[2] == 'Node': # Node 0 zone DMA\n cols = words[2:]\n cur_zone = '.'.join(cols)\n continue\n elif words[2] == 'pages': # pages free 3936\n cur_submetric = words[2] + '.' + words[3] # pages.free\n cur_value = words[4]\n elif words[2] in self.processed_sub_metrics:\n cur_submetric = 'pages' + '.' + words[2] # pages.min\n cur_value = words[3]\n elif words[2] in self.skipped_sub_metrics:\n continue\n else: # other useful submetrics\n cur_submetric = words[2]\n cur_value = words[3]\n col = cur_zone + '.' + cur_submetric # prefix with 'Node.0.zone.DMA.\n # only process zones specified in config\n if cur_zone and self.zones and cur_zone not in self.zones:\n continue\n self.sub_metric_unit[col] = 'pages' # The unit of the sub metric. For /proc/zoneinfo, they are all in pages\n # only process sub_metrics specified in config.\n if self.sub_metrics and cur_submetric and cur_submetric not in self.sub_metrics:\n continue\n if col in self.column_csv_map:\n out_csv = self.column_csv_map[col]\n else:\n out_csv = self.get_csv(col) # column_csv_map[] is assigned in get_csv()\n data[out_csv] = []\n data[out_csv].append(ts + \",\" + cur_value)\n # post processing, putting data in csv files;\n for csv in data.keys():\n self.csv_files.append(csv)\n with open(csv, 'w') as fh:\n fh.write('\\n'.join(sorted(data[csv])))\n return status"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef collect(self):\n\n for aggr_metric in self.aggr_metrics: # e.g., SAR-device.sda.await:count,sum,avg\n functions_aggr = []\n fields = aggr_metric.split(\":\")\n cur_metric_type = fields[0].split(\".\")[0] # e.g. SAR-device\n\n if len(fields) > 1: # The user has to specify the aggregate functions (i.e., :raw,count,sum,avg)\n func_user = ''.join(fields[1].split())\n functions_aggr.extend(func_user.split(\",\"))\n else: # no user input of aggregate functions\n return True\n\n cur_column = '.'.join(fields[0].split('.')[1:]) # e.g. sda.await or all.percent-sys\n\n # Store data points of various aggregation functions\n aggr_data = {}\n aggr_data['raw'] = [] # Store all the raw values\n aggr_data['sum'] = defaultdict(float) # Store the sum values for each timestamp\n aggr_data['count'] = defaultdict(int) # Store the count of each timestamp (i.e. qps)\n\n for metric in self.metrics: # Loop the list to find from all metrics to merge\n if metric.hostname in self.aggr_hosts and \\\n cur_column in metric.csv_column_map.values():\n file_csv = metric.get_csv(cur_column)\n timestamp_format = None\n with open(file_csv) as fh:\n for line in fh:\n aggr_data['raw'].append(line.rstrip())\n words = line.split(\",\")\n ts = words[0].split('.')[0] # In case of sub-seconds; we only want the value of seconds;\n if not timestamp_format or timestamp_format == 'unknown':\n timestamp_format = naarad.utils.detect_timestamp_format(ts)\n if timestamp_format == 'unknown':\n continue\n ts = naarad.utils.get_standardized_timestamp(ts, timestamp_format)\n aggr_data['sum'][ts] += float(words[1])\n aggr_data['count'][ts] += 1\n # \"raw\" csv file\n if 'raw' in functions_aggr:\n out_csv = self.get_csv(cur_column, 'raw')\n self.csv_files.append(out_csv)\n with open(out_csv, 'w') as fh:\n fh.write(\"\\n\".join(sorted(aggr_data['raw'])))\n\n # \"sum\" csv file\n if 'sum' in functions_aggr:\n out_csv = self.get_csv(cur_column, 'sum')\n self.csv_files.append(out_csv)\n with open(out_csv, 'w') as fh:\n for (k, v) in sorted(aggr_data['sum'].items()):\n fh.write(k + \",\" + str(v) + '\\n')\n\n # \"avg\" csv file\n if 'avg' in functions_aggr:\n out_csv = self.get_csv(cur_column, 'avg')\n self.csv_files.append(out_csv)\n with open(out_csv, 'w') as fh:\n for (k, v) in sorted(aggr_data['sum'].items()):\n fh.write(k + \",\" + str(v / aggr_data['count'][k]) + '\\n')\n\n # \"count\" csv file (qps)\n if 'count' in functions_aggr:\n out_csv = self.get_csv(cur_column, 'count')\n self.csv_files.append(out_csv)\n with open(out_csv, 'w') as fh:\n for (k, v) in sorted(aggr_data['count'].items()):\n fh.write(k + \",\" + str(v) + '\\n')\n\n gc.collect()\n return True", "response": "Collect the metrics from the given hosts and update the corresponding properties such as csv_column_map."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_times(self, native):\n start_time = 0\n end_time = 0\n launch_time = 0\n nus_update_time = 0\n\n for item in native:\n if item[CONSTANTS.LIA_TIMING_NAME] == CONSTANTS.LIA_APP_ON_CREATE and item[CONSTANTS.LIA_START] is not None:\n start_time = item[CONSTANTS.LIA_START][CONSTANTS.LIA_LONG]\n if item[CONSTANTS.LIA_TIMING_NAME] == CONSTANTS.LIA_NUS_UPDATE:\n if item[CONSTANTS.LIA_TIMING_VALUE] is not None:\n nus_update_time = item[CONSTANTS.LIA_TIMING_VALUE][CONSTANTS.LIA_LONG]\n if item[CONSTANTS.LIA_START] is not None:\n end_time = item[CONSTANTS.LIA_START][CONSTANTS.LIA_LONG]\n\n if start_time == 0 or end_time == 0:\n time_stamp = 0\n launch_time = 0\n else:\n time_stamp = start_time\n launch_time = end_time - start_time\n return (time_stamp, launch_time, nus_update_time)", "response": "get start time stamp launch time duration and nus update time duration from JSON object native"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nperform the Oct2Py speed analysis.", "response": "def run(self):\r\n \"\"\"Perform the Oct2Py speed analysis.\r\n\r\n Uses timeit to test the raw execution of an Octave command,\r\n Then tests progressively larger array passing.\r\n\r\n \"\"\"\r\n print('Oct2Py speed test')\r\n print('*' * 20)\r\n time.sleep(1)\r\n\r\n print('Raw speed: ')\r\n avg = timeit.timeit(self.raw_speed, number=10) / 10\r\n print(' {0:0.01f} usec per loop'.format(avg * 1e6))\r\n sides = [1, 10, 100, 1000]\r\n runs = [10, 10, 10, 5]\r\n for (side, nruns) in zip(sides, runs):\r\n self.array = np.reshape(np.arange(side ** 2), (-1))\r\n print('Put {0}x{1}: '.format(side, side))\r\n avg = timeit.timeit(self.large_array_put, number=nruns) / nruns\r\n print(' {0:0.01f} msec'.format(avg * 1e3))\r\n\r\n print('Get {0}x{1}: '.format(side, side))\r\n avg = timeit.timeit(self.large_array_get, number=nruns) / nruns\r\n print(' {0:0.01f} msec'.format(avg * 1e3))\r\n\r\n self.octave.exit()\r\n print('*' * 20)\r\n print('Test complete!')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nquitting this octave session and cleans up.", "response": "def exit(self):\r\n \"\"\"Quits this octave session and cleans up.\r\n \"\"\"\r\n if self._engine:\r\n self._engine.repl.terminate()\r\n self._engine = None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npush a variable or variable to the Octave session.", "response": "def push(self, name, var, timeout=None, verbose=True):\r\n \"\"\"\r\n Put a variable or variables into the Octave session.\r\n\r\n Parameters\r\n ----------\r\n name : str or list\r\n Name of the variable(s).\r\n var : object or list\r\n The value(s) to pass.\r\n timeout : float\r\n Time to wait for response from Octave (per line).\r\n **kwargs: Deprecated kwargs, ignored.\r\n\r\n Examples\r\n --------\r\n >>> from oct2py import octave\r\n >>> y = [1, 2]\r\n >>> octave.push('y', y)\r\n >>> octave.pull('y')\r\n array([[ 1., 2.]])\r\n >>> octave.push(['x', 'y'], ['spam', [1, 2, 3, 4]])\r\n >>> octave.pull(['x', 'y']) # doctest: +SKIP\r\n [u'spam', array([[1, 2, 3, 4]])]\r\n\r\n Notes\r\n -----\r\n Integer type arguments will be converted to floating point\r\n unless `convert_to_float=False`.\r\n\r\n \"\"\"\r\n if isinstance(name, (str, unicode)):\r\n name = [name]\r\n var = [var]\r\n\r\n for (n, v) in zip(name, var):\r\n self.feval('assignin', 'base', n, v, nout=0, timeout=timeout,\r\n verbose=verbose)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef pull(self, var, timeout=None, verbose=True):\r\n if isinstance(var, (str, unicode)):\r\n var = [var]\r\n outputs = []\r\n for name in var:\r\n exist = self._exist(name)\r\n if exist == 1:\r\n outputs.append(self.feval('evalin', 'base', name,\r\n timeout=timeout, verbose=verbose))\r\n else:\r\n outputs.append(self.get_pointer(name, timeout=timeout))\r\n\r\n if len(outputs) == 1:\r\n return outputs[0]\r\n return outputs", "response": "Retrieves a value or values from Octave."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_pointer(self, name, timeout=None):\r\n exist = self._exist(name)\r\n isobject = self._isobject(name, exist)\r\n\r\n if exist == 0:\r\n raise Oct2PyError('\"%s\" is undefined' % name)\r\n\r\n elif exist == 1:\r\n return _make_variable_ptr_instance(self, name)\r\n\r\n elif isobject:\r\n return self._get_user_class(name)\r\n\r\n elif exist in [2, 3, 5]:\r\n return self._get_function_ptr(name)\r\n\r\n raise Oct2PyError('Unknown type for object \"%s\"' % name)", "response": "Get a pointer to a named object in the Octave workspace."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef extract_figures(self, plot_dir, remove=False):\r\n figures = self._engine.extract_figures(plot_dir, remove)\r\n return figures", "response": "Extract the figures in the directory to IPython display objects."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef feval(self, func_path, *func_args, **kwargs):\r\n if not self._engine:\r\n raise Oct2PyError('Session is not open')\r\n\r\n nout = kwargs.get('nout', None)\r\n if nout is None:\r\n nout = 1\r\n\r\n plot_dir = kwargs.get('plot_dir')\r\n settings = dict(backend='inline' if plot_dir else self.backend,\r\n format=kwargs.get('plot_format'),\r\n name=kwargs.get('plot_name'),\r\n width=kwargs.get('plot_width'),\r\n height=kwargs.get('plot_height'),\r\n resolution=kwargs.get('plot_res'))\r\n self._engine.plot_settings = settings\r\n\r\n dname = osp.dirname(func_path)\r\n fname = osp.basename(func_path)\r\n func_name, ext = osp.splitext(fname)\r\n if ext and not ext == '.m':\r\n raise TypeError('Need to give path to .m file')\r\n\r\n if func_name == 'clear':\r\n raise Oct2PyError('Cannot use `clear` command directly, use' +\r\n ' eval(\"clear(var1, var2)\")')\r\n\r\n stream_handler = kwargs.get('stream_handler')\r\n verbose = kwargs.get('verbose', True)\r\n store_as = kwargs.get('store_as', '')\r\n timeout = kwargs.get('timeout', self.timeout)\r\n if not stream_handler:\r\n stream_handler = self.logger.info if verbose else self.logger.debug\r\n\r\n return self._feval(func_name, func_args, dname=dname, nout=nout,\r\n timeout=timeout, stream_handler=stream_handler,\r\n store_as=store_as, plot_dir=plot_dir)", "response": "Run a function in Octave and return the result."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nevaluate an Octave command or commands and return the result.", "response": "def eval(self, cmds, verbose=True, timeout=None, stream_handler=None,\r\n temp_dir=None, plot_dir=None, plot_name='plot', plot_format='svg',\r\n plot_width=None, plot_height=None, plot_res=None,\r\n nout=0, **kwargs):\r\n \"\"\"\r\n Evaluate an Octave command or commands.\r\n\r\n Parameters\r\n ----------\r\n cmds : str or list\r\n Commands(s) to pass to Octave.\r\n verbose : bool, optional\r\n Log Octave output at INFO level. If False, log at DEBUG level.\r\n stream_handler: callable, optional\r\n A function that is called for each line of output from the\r\n evaluation.\r\n timeout : float, optional\r\n Time to wait for response from Octave (per line). If not given,\r\n the instance `timeout` is used.\r\n nout : int, optional.\r\n The desired number of returned values, defaults to 0. If nout\r\n is 0, the `ans` will be returned as the return value.\r\n temp_dir: str, optional\r\n If specified, the session's MAT files will be created in the\r\n directory, otherwise a the instance `temp_dir` is used.\r\n a shared memory (tmpfs) path.\r\n plot_dir: str, optional\r\n If specificed, save the session's plot figures to the plot\r\n directory instead of displaying the plot window.\r\n plot_name : str, optional\r\n Saved plots will start with `plot_name` and\r\n end with \"_%%.xxx' where %% is the plot number and\r\n xxx is the `plot_format`.\r\n plot_format: str, optional\r\n The format in which to save the plot (PNG by default).\r\n plot_width: int, optional\r\n The plot with in pixels.\r\n plot_height: int, optional\r\n The plot height in pixels.\r\n plot_res: int, optional\r\n The plot resolution in pixels per inch.\r\n **kwargs Deprectated kwargs.\r\n\r\n Examples\r\n --------\r\n >>> from oct2py import octave\r\n >>> octave.eval('disp(\"hello\")') # doctest: +SKIP\r\n hello\r\n >>> x = octave.eval('round(quad(@sin, 0, pi/2));')\r\n >>> x\r\n 1.0\r\n\r\n >>> a = octave.eval('disp(\"hello\");1;') # doctest: +SKIP\r\n hello\r\n >>> a = octave.eval('disp(\"hello\");1;', verbose=False)\r\n >>> a\r\n 1.0\r\n\r\n >>> from oct2py import octave\r\n >>> lines = []\r\n >>> octave.eval('for i = 1:3; disp(i);end', \\\r\n stream_handler=lines.append)\r\n >>> lines # doctest: +SKIP\r\n [' 1', ' 2', ' 3']\r\n\r\n Returns\r\n -------\r\n out : object\r\n Octave \"ans\" variable, or None.\r\n\r\n Notes\r\n -----\r\n The deprecated `log` kwarg will temporarily set the `logger` level to\r\n `WARN`. Using the `logger` settings directly is preferred.\r\n The deprecated `return_both` kwarg will still work, but the preferred\r\n method is to use the `stream_handler`. If `stream_handler` is given,\r\n the `return_both` kwarg will be honored but will give an empty string\r\n as the reponse.\r\n\r\n Raises\r\n ------\r\n Oct2PyError\r\n If the command(s) fail.\r\n \"\"\"\r\n if isinstance(cmds, (str, unicode)):\r\n cmds = [cmds]\r\n\r\n prev_temp_dir = self.temp_dir\r\n self.temp_dir = temp_dir or self.temp_dir\r\n prev_log_level = self.logger.level\r\n\r\n if kwargs.get('log') is False:\r\n self.logger.setLevel(logging.WARN)\r\n\r\n for name in ['log', 'return_both']:\r\n if name not in kwargs:\r\n continue\r\n msg = 'Using deprecated `%s` kwarg, see docs on `Oct2Py.eval()`'\r\n warnings.warn(msg % name, stacklevel=2)\r\n\r\n return_both = kwargs.pop('return_both', False)\r\n lines = []\r\n if return_both and not stream_handler:\r\n stream_handler = lines.append\r\n\r\n ans = None\r\n for cmd in cmds:\r\n resp = self.feval('evalin', 'base', cmd,\r\n nout=nout, timeout=timeout,\r\n stream_handler=stream_handler,\r\n verbose=verbose, plot_dir=plot_dir,\r\n plot_name=plot_name, plot_format=plot_format,\r\n plot_width=plot_width, plot_height=plot_height,\r\n plot_res=plot_res)\r\n if resp is not None:\r\n ans = resp\r\n\r\n self.temp_dir = prev_temp_dir\r\n self.logger.setLevel(prev_log_level)\r\n\r\n if return_both:\r\n return '\\n'.join(lines), ans\r\n return ans"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef restart(self):\r\n if self._engine:\r\n self._engine.repl.terminate()\r\n\r\n executable = self._executable\r\n if executable:\r\n os.environ['OCTAVE_EXECUTABLE'] = executable\r\n if 'OCTAVE_EXECUTABLE' not in os.environ and 'OCTAVE' in os.environ:\r\n os.environ['OCTAVE_EXECUTABLE'] = os.environ['OCTAVE']\r\n\r\n self._engine = OctaveEngine(stdin_handler=self._handle_stdin,\r\n logger=self.logger)\r\n\r\n # Add local Octave scripts.\r\n self._engine.eval('addpath(\"%s\");' % HERE.replace(osp.sep, '/'))", "response": "Restart an Octave session in a clean state"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _feval(self, func_name, func_args=(), dname='', nout=0,\r\n timeout=None, stream_handler=None, store_as='', plot_dir=None):\r\n \"\"\"Run the given function with the given args.\r\n \"\"\"\r\n engine = self._engine\r\n if engine is None:\r\n raise Oct2PyError('Session is closed')\r\n\r\n # Set up our mat file paths.\r\n out_file = osp.join(self.temp_dir, 'writer.mat')\r\n out_file = out_file.replace(osp.sep, '/')\r\n in_file = osp.join(self.temp_dir, 'reader.mat')\r\n in_file = in_file.replace(osp.sep, '/')\r\n\r\n func_args = list(func_args)\r\n ref_indices = []\r\n for (i, value) in enumerate(func_args):\r\n if isinstance(value, OctavePtr):\r\n ref_indices.append(i + 1)\r\n func_args[i] = value.address\r\n ref_indices = np.array(ref_indices)\r\n\r\n # Save the request data to the output file.\r\n req = dict(func_name=func_name, func_args=tuple(func_args),\r\n dname=dname or '', nout=nout,\r\n store_as=store_as or '',\r\n ref_indices=ref_indices)\r\n\r\n write_file(req, out_file, oned_as=self._oned_as,\r\n convert_to_float=self.convert_to_float)\r\n\r\n # Set up the engine and evaluate the `_pyeval()` function.\r\n engine.stream_handler = stream_handler or self.logger.info\r\n if timeout is None:\r\n timeout = self.timeout\r\n\r\n try:\r\n engine.eval('_pyeval(\"%s\", \"%s\");' % (out_file, in_file),\r\n timeout=timeout)\r\n except KeyboardInterrupt as e:\r\n stream_handler(engine.repl.interrupt())\r\n raise\r\n except TIMEOUT:\r\n stream_handler(engine.repl.interrupt())\r\n raise Oct2PyError('Timed out, interrupting')\r\n except EOF:\r\n stream_handler(engine.repl.child.before)\r\n self.restart()\r\n raise Oct2PyError('Session died, restarting')\r\n\r\n # Read in the output.\r\n resp = read_file(in_file, self)\r\n if resp['err']:\r\n msg = self._parse_error(resp['err'])\r\n raise Oct2PyError(msg)\r\n\r\n result = resp['result'].ravel().tolist()\r\n if isinstance(result, list) and len(result) == 1:\r\n result = result[0]\r\n\r\n # Check for sentinel value.\r\n if (isinstance(result, Cell) and\r\n result.size == 1 and\r\n isinstance(result[0], string_types) and\r\n result[0] == '__no_value__'):\r\n result = None\r\n\r\n if plot_dir:\r\n self._engine.make_figures(plot_dir)\r\n\r\n return result", "response": "Run the given function with the given args."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _parse_error(self, err):\r\n self.logger.debug(err)\r\n stack = err.get('stack', [])\r\n if not err['message'].startswith('parse error:'):\r\n err['message'] = 'error: ' + err['message']\r\n errmsg = 'Octave evaluation error:\\n%s' % err['message']\r\n\r\n if not isinstance(stack, StructArray):\r\n return errmsg\r\n\r\n errmsg += '\\nerror: called from:'\r\n for item in stack[:-1]:\r\n errmsg += '\\n %(name)s at line %(line)d' % item\r\n try:\r\n errmsg += ', column %(column)d' % item\r\n except Exception:\r\n pass\r\n return errmsg", "response": "Create a traceback for an Octave evaluation error."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the documentation of a named function.", "response": "def _get_doc(self, name):\r\n \"\"\"\r\n Get the documentation of an Octave procedure or object.\r\n\r\n Parameters\r\n ----------\r\n name : str\r\n Function name to search for.\r\n\r\n Returns\r\n -------\r\n out : str\r\n Documentation string.\r\n\r\n Raises\r\n ------\r\n Oct2PyError\r\n If the procedure or object function has a syntax error.\r\n\r\n \"\"\"\r\n doc = 'No documentation for %s' % name\r\n\r\n engine = self._engine\r\n\r\n doc = engine.eval('help(\"%s\")' % name, silent=True)\r\n\r\n if 'syntax error:' in doc.lower():\r\n raise Oct2PyError(doc)\r\n\r\n if 'error:' in doc.lower():\r\n doc = engine.eval('type(\"%s\")' % name, silent=True)\r\n doc = '\\n'.join(doc.splitlines()[:3])\r\n\r\n default = self.feval.__doc__\r\n default = ' ' + default[default.find('func_args:'):]\r\n default = '\\n'.join([line[8:] for line in default.splitlines()])\r\n\r\n doc = '\\n'.join(doc.splitlines())\r\n doc = '\\n' + doc + '\\n\\nParameters\\n----------\\n' + default\r\n doc += '\\n**kwargs - Deprecated keyword arguments\\n\\n'\r\n doc += 'Notes\\n-----\\n'\r\n doc += 'Keyword arguments to dynamic functions are deprecated.\\n'\r\n doc += 'The `plot_*` kwargs will be ignored, but the rest will\\n'\r\n doc += 'used as key - value pairs as in version 3.x.\\n'\r\n doc += 'Use `set_plot_settings()` for plot settings, and use\\n'\r\n doc += '`func_args` directly for key - value pairs.'\r\n return doc"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _exist(self, name):\r\n cmd = 'exist(\"%s\")' % name\r\n resp = self._engine.eval(cmd, silent=True).strip()\r\n exist = int(resp.split()[-1])\r\n if exist == 0:\r\n msg = 'Value \"%s\" does not exist in Octave workspace'\r\n raise Oct2PyError(msg % name)\r\n return exist", "response": "Test whether a name exists and return the name code."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ntests whether the name is an object.", "response": "def _isobject(self, name, exist):\r\n \"\"\"Test whether the name is an object.\"\"\"\r\n if exist in [2, 5]:\r\n return False\r\n cmd = 'isobject(%s)' % name\r\n resp = self._engine.eval(cmd, silent=True).strip()\r\n return resp == 'ans = 1'"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets or create a function pointer of the given name.", "response": "def _get_function_ptr(self, name):\r\n \"\"\"Get or create a function pointer of the given name.\"\"\"\r\n func = _make_function_ptr_instance\r\n self._function_ptrs.setdefault(name, func(self, name))\r\n return self._function_ptrs[name]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets or create a user class of the given type.", "response": "def _get_user_class(self, name):\r\n \"\"\"Get or create a user class of the given type.\"\"\"\r\n self._user_classes.setdefault(name, _make_user_class(self, name))\r\n return self._user_classes[name]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _cleanup(self):\r\n self.exit()\r\n workspace = osp.join(os.getcwd(), 'octave-workspace')\r\n if osp.exists(workspace):\r\n os.remove(workspace)", "response": "Clean up resources used by the session."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nplaying a demo script showing most of the oct2py api features. Parameters ========== delay : float Time between each command in seconds.", "response": "def demo(delay=1, interactive=True):\r\n \"\"\"\r\n Play a demo script showing most of the oct2py api features.\r\n\r\n Parameters\r\n ==========\r\n delay : float\r\n Time between each command in seconds.\r\n\r\n \"\"\"\r\n script = \"\"\"\r\n #########################\r\n # Oct2Py demo\r\n #########################\r\n import numpy as np\r\n from oct2py import Oct2Py\r\n oc = Oct2Py()\r\n # basic commands\r\n print(oc.abs(-1))\r\n print(oc.upper('xyz'))\r\n # plotting\r\n oc.plot([1,2,3],'-o', 'linewidth', 2)\r\n raw_input('Press Enter to continue...')\r\n oc.close()\r\n xx = np.arange(-2*np.pi, 2*np.pi, 0.2)\r\n oc.surf(np.subtract.outer(np.sin(xx), np.cos(xx)))\r\n raw_input('Press Enter to continue...')\r\n oc.close()\r\n # getting help\r\n help(oc.svd)\r\n # single vs. multiple return values\r\n print(oc.svd(np.array([[1,2], [1,3]])))\r\n U, S, V = oc.svd([[1,2], [1,3]], nout=3)\r\n print(U, S, V)\r\n # low level constructs\r\n oc.eval(\"y=ones(3,3)\")\r\n print(oc.pull(\"y\"))\r\n oc.eval(\"x=zeros(3,3)\", verbose=True)\r\n t = oc.eval('rand(1, 2)', verbose=True)\r\n y = np.zeros((3,3))\r\n oc.push('y', y)\r\n print(oc.pull('y'))\r\n from oct2py import Struct\r\n y = Struct()\r\n y.b = 'spam'\r\n y.c.d = 'eggs'\r\n print(y.c['d'])\r\n print(y)\r\n #########################\r\n # Demo Complete!\r\n #########################\r\n \"\"\"\r\n if not PY2:\r\n script = script.replace('raw_input', 'input')\r\n\r\n for line in script.strip().split('\\n'):\r\n line = line.strip()\r\n if not 'input(' in line:\r\n time.sleep(delay)\r\n print(\">>> {0}\".format(line))\r\n time.sleep(delay)\r\n if not interactive:\r\n if 'plot' in line or 'surf' in line or 'input(' in line:\r\n line = 'print()'\r\n exec(line)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef kill_octave():\r\n import os\r\n if os.name == 'nt':\r\n os.system('taskkill /im octave /f')\r\n else:\r\n os.system('killall -9 octave')\r\n os.system('killall -9 octave-cli')\r\n octave.restart()", "response": "Kill all Octave instances."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstarting a number of threads and verify each has a unique Octave session. Parameters ========== nthreads : int Number of threads to use. Raises ====== Oct2PyError If the thread does not sucessfully demonstrate independence.", "response": "def thread_check(nthreads=3):\r\n \"\"\"\r\n Start a number of threads and verify each has a unique Octave session.\r\n\r\n Parameters\r\n ==========\r\n nthreads : int\r\n Number of threads to use.\r\n\r\n Raises\r\n ======\r\n Oct2PyError\r\n If the thread does not sucessfully demonstrate independence.\r\n\r\n \"\"\"\r\n print(\"Starting {0} threads at {1}\".format(nthreads,\r\n datetime.datetime.now()))\r\n threads = []\r\n for i in range(nthreads):\r\n thread = ThreadClass()\r\n thread.setDaemon(True)\r\n thread.start()\r\n threads.append(thread)\r\n for thread in threads:\r\n thread.join()\r\n print('All threads closed at {0}'.format(datetime.datetime.now()))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef run(self):\r\n octave = Oct2Py()\r\n # write the same variable name in each thread and read it back\r\n octave.push('name', self.getName())\r\n name = octave.pull('name')\r\n now = datetime.datetime.now()\r\n print(\"{0} got '{1}' at {2}\".format(self.getName(), name, now))\r\n octave.exit()\r\n try:\r\n assert self.getName() == name\r\n except AssertionError: # pragma: no cover\r\n raise Oct2PyError('Thread collision detected')\r\n return", "response": "Create a unique instance of Octave and verify namespace uniqueness."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_file(path, session=None):\r\n try:\r\n data = loadmat(path, struct_as_record=True)\r\n except UnicodeDecodeError as e:\r\n raise Oct2PyError(str(e))\r\n out = dict()\r\n for (key, value) in data.items():\r\n out[key] = _extract(value, session)\r\n return out", "response": "Read the data from the given file path."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef write_file(obj, path, oned_as='row', convert_to_float=True):\r\n data = _encode(obj, convert_to_float)\r\n try:\r\n # scipy.io.savemat is not thread-save.\r\n # See https://github.com/scipy/scipy/issues/7260\r\n with _WRITE_LOCK:\r\n savemat(path, data, appendmat=False, oned_as=oned_as,\r\n long_field_names=True)\r\n except KeyError: # pragma: no cover\r\n raise Exception('could not save mat file')", "response": "Save a Python object to an Octave file on the given path."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert the Octave values to values suitable for Python.", "response": "def _extract(data, session=None):\r\n \"\"\"Convert the Octave values to values suitable for Python.\r\n \"\"\"\r\n # Extract each item of a list.\r\n if isinstance(data, list):\r\n return [_extract(d, session) for d in data]\r\n\r\n # Ignore leaf objects.\r\n if not isinstance(data, np.ndarray):\r\n return data\r\n\r\n # Extract user defined classes.\r\n if isinstance(data, MatlabObject):\r\n cls = session._get_user_class(data.classname)\r\n return cls.from_value(data)\r\n\r\n # Extract struct data.\r\n if data.dtype.names:\r\n # Singular struct\r\n if data.size == 1:\r\n return _create_struct(data, session)\r\n # Struct array\r\n return StructArray(data, session)\r\n\r\n # Extract cells.\r\n if data.dtype.kind == 'O':\r\n return Cell(data, session)\r\n\r\n # Compress singleton values.\r\n if data.size == 1:\r\n return data.item()\r\n\r\n # Compress empty values.\r\n if data.size == 0:\r\n if data.dtype.kind in 'US':\r\n return ''\r\n return []\r\n\r\n # Return standard array.\r\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a struct from session data.", "response": "def _create_struct(data, session):\r\n \"\"\"Create a struct from session data.\r\n \"\"\"\r\n out = Struct()\r\n for name in data.dtype.names:\r\n item = data[name]\r\n # Extract values that are cells (they are doubly wrapped).\r\n if isinstance(item, np.ndarray) and item.dtype.kind == 'O':\r\n item = item.squeeze().tolist()\r\n out[name] = _extract(item, session)\r\n return out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting the Python values to values suitable to send to Octave.", "response": "def _encode(data, convert_to_float):\r\n \"\"\"Convert the Python values to values suitable to send to Octave.\r\n \"\"\"\r\n ctf = convert_to_float\r\n\r\n # Handle variable pointer.\r\n if isinstance(data, (OctaveVariablePtr)):\r\n return _encode(data.value, ctf)\r\n\r\n # Handle a user defined object.\r\n if isinstance(data, OctaveUserClass):\r\n return _encode(OctaveUserClass.to_value(data), ctf)\r\n\r\n # Handle a function pointer.\r\n if isinstance(data, (OctaveFunctionPtr, MatlabFunction)):\r\n raise Oct2PyError('Cannot write Octave functions')\r\n\r\n # Handle matlab objects.\r\n if isinstance(data, MatlabObject):\r\n view = data.view(np.ndarray)\r\n out = MatlabObject(data, data.classname)\r\n for name in out.dtype.names:\r\n out[name] = _encode(view[name], ctf)\r\n return out\r\n\r\n # Handle pandas series and dataframes\r\n if isinstance(data, (DataFrame, Series)):\r\n return _encode(data.values, ctf)\r\n\r\n # Extract and encode values from dict-like objects.\r\n if isinstance(data, dict):\r\n out = dict()\r\n for (key, value) in data.items():\r\n out[key] = _encode(value, ctf)\r\n return out\r\n\r\n # Send None as nan.\r\n if data is None:\r\n return np.NaN\r\n\r\n # Sets are treated like lists.\r\n if isinstance(data, set):\r\n return _encode(list(data), ctf)\r\n\r\n # Lists can be interpreted as numeric arrays or cell arrays.\r\n if isinstance(data, list):\r\n if _is_simple_numeric(data):\r\n return _encode(np.array(data), ctf)\r\n return _encode(tuple(data), ctf)\r\n\r\n # Tuples are handled as cells.\r\n if isinstance(data, tuple):\r\n obj = np.empty(len(data), dtype=object)\r\n for (i, item) in enumerate(data):\r\n obj[i] = _encode(item, ctf)\r\n return obj\r\n\r\n # Sparse data must be floating type.\r\n if isinstance(data, spmatrix):\r\n return data.astype(np.float64)\r\n\r\n # Return other data types unchanged.\r\n if not isinstance(data, np.ndarray):\r\n return data\r\n\r\n # Extract and encode data from object-like arrays.\r\n if data.dtype.kind in 'OV':\r\n out = np.empty(data.size, dtype=data.dtype)\r\n for (i, item) in enumerate(data.ravel()):\r\n if data.dtype.names:\r\n for name in data.dtype.names:\r\n out[i][name] = _encode(item[name], ctf)\r\n else:\r\n out[i] = _encode(item, ctf)\r\n return out.reshape(data.shape)\r\n\r\n # Complex 128 is the highest supported by savemat.\r\n if data.dtype.name == 'complex256':\r\n return data.astype(np.complex128)\r\n\r\n # Convert to float if applicable.\r\n if ctf and data.dtype.kind in 'ui':\r\n return data.astype(np.float64)\r\n\r\n # Return standard array.\r\n return data"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ntests if a list contains simple numeric data.", "response": "def _is_simple_numeric(data):\r\n \"\"\"Test if a list contains simple numeric data.\"\"\"\r\n for item in data:\r\n if isinstance(item, set):\r\n item = list(item)\r\n if isinstance(item, list):\r\n if not _is_simple_numeric(item):\r\n return False\r\n elif not isinstance(item, (int, float, complex)):\r\n return False\r\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_log(name=None):\r\n if name is None:\r\n name = 'oct2py'\r\n else:\r\n name = 'oct2py.' + name\r\n\r\n log = logging.getLogger(name)\r\n log.setLevel(logging.INFO)\r\n return log", "response": "Returns a console logger."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _make_user_class(session, name):\n attrs = session.eval('fieldnames(%s);' % name, nout=1).ravel().tolist()\n methods = session.eval('methods(%s);' % name, nout=1).ravel().tolist()\n ref = weakref.ref(session)\n\n doc = _DocDescriptor(ref, name)\n values = dict(__doc__=doc, _name=name, _ref=ref, _attrs=attrs,\n __module__='oct2py.dynamic')\n\n for method in methods:\n doc = _MethodDocDescriptor(ref, name, method)\n cls_name = '%s_%s' % (name, method)\n method_values = dict(__doc__=doc)\n method_cls = type(str(cls_name),\n (OctaveUserClassMethod,), method_values)\n values[method] = method_cls(ref, method, name)\n\n for attr in attrs:\n values[attr] = OctaveUserClassAttr(ref, attr, attr)\n\n return type(str(name), (OctaveUserClass,), values)", "response": "Make an Octave class for a given class name"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef from_value(cls, value):\n instance = OctaveUserClass.__new__(cls)\n instance._address = '%s_%s' % (instance._name, id(instance))\n instance._ref().push(instance._address, value)\n return instance", "response": "This is how an instance is created when we read a MatlabObject from a MAT file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting an OctaveUserClass instance to a value to send to Octave.", "response": "def to_value(cls, instance):\n \"\"\"Convert to a value to send to Octave.\"\"\"\n if not isinstance(instance, OctaveUserClass) or not instance._attrs:\n return dict()\n # Bootstrap a MatlabObject from scipy.io\n # From https://github.com/scipy/scipy/blob/93a0ea9e5d4aba1f661b6bb0e18f9c2d1fce436a/scipy/io/matlab/mio5.py#L435-L443\n # and https://github.com/scipy/scipy/blob/93a0ea9e5d4aba1f661b6bb0e18f9c2d1fce436a/scipy/io/matlab/mio5_params.py#L224\n dtype = []\n values = []\n for attr in instance._attrs:\n dtype.append((str(attr), object))\n values.append(getattr(instance, attr))\n struct = np.array([tuple(values)], dtype)\n return MatlabObject(struct, instance._name)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a pointer to the private object.", "response": "def to_pointer(cls, instance):\n \"\"\"Get a pointer to the private object.\n \"\"\"\n return OctavePtr(instance._ref, instance._name, instance._address)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef document_func_view(serializer_class=None,\n response_serializer_class=None,\n filter_backends=None,\n permission_classes=None,\n authentication_classes=None,\n doc_format_args=list(),\n doc_format_kwargs=dict()):\n \"\"\"\n Decorator to make functional view documentable via drf-autodocs\n \"\"\"\n def decorator(func):\n if serializer_class:\n func.cls.serializer_class = func.view_class.serializer_class = serializer_class\n if response_serializer_class:\n func.cls.response_serializer_class = func.view_class.response_serializer_class = response_serializer_class\n if filter_backends:\n func.cls.filter_backends = func.view_class.filter_backends = filter_backends\n if permission_classes:\n func.cls.permission_classes = func.view_class.permission_classes = permission_classes\n if authentication_classes:\n func.cls.authentication_classes = func.view_class.authentication_classes = authentication_classes\n if doc_format_args or doc_format_kwargs:\n func.cls.__doc__ = func.view_class.__doc__ = getdoc(func).format(*doc_format_args, **doc_format_kwargs)\n return func\n\n return decorator", "response": "Decorator to make functional view documentable via drf - autodocs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn True if file is a valid RAR file.", "response": "def is_rarfile(filename):\n \"\"\"Return true if file is a valid RAR file.\"\"\"\n mode = constants.RAR_OM_LIST_INCSPLIT\n archive = unrarlib.RAROpenArchiveDataEx(filename, mode=mode)\n try:\n handle = unrarlib.RAROpenArchiveEx(ctypes.byref(archive))\n except unrarlib.UnrarException:\n return False\n unrarlib.RARCloseArchive(handle)\n return (archive.OpenResult == constants.SUCCESS)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _read_header(self, handle):\n header_data = unrarlib.RARHeaderDataEx()\n try:\n res = unrarlib.RARReadHeaderEx(handle, ctypes.byref(header_data))\n rarinfo = RarInfo(header=header_data)\n except unrarlib.ArchiveEnd:\n return None\n except unrarlib.MissingPassword:\n raise RuntimeError(\"Archive is encrypted, password required\")\n except unrarlib.BadPassword:\n raise RuntimeError(\"Bad password for Archive\")\n except unrarlib.UnrarException as e:\n raise BadRarFile(str(e))\n\n return rarinfo", "response": "Read the current member header into a RarInfo object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _process_current(self, handle, op, dest_path=None, dest_name=None):\n unrarlib.RARProcessFileW(handle, op, dest_path, dest_name)", "response": "Process current member with op operation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload archive members metadata.", "response": "def _load_metadata(self, handle):\n \"\"\"Load archive members metadata.\"\"\"\n rarinfo = self._read_header(handle)\n while rarinfo:\n self.filelist.append(rarinfo)\n self.NameToInfo[rarinfo.filename] = rarinfo\n self._process_current(handle, constants.RAR_SKIP)\n rarinfo = self._read_header(handle)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _open(self, archive):\n try:\n handle = unrarlib.RAROpenArchiveEx(ctypes.byref(archive))\n except unrarlib.UnrarException:\n raise BadRarFile(\"Invalid RAR file.\")\n return handle", "response": "Open RAR archive file."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef open(self, member, pwd=None):\n if isinstance(member, RarInfo):\n member = member.filename\n\n archive = unrarlib.RAROpenArchiveDataEx(\n self.filename, mode=constants.RAR_OM_EXTRACT)\n handle = self._open(archive)\n\n password = pwd or self.pwd\n if password is not None:\n unrarlib.RARSetPassword(handle, b(password))\n\n # based on BrutuZ (https://github.com/matiasb/python-unrar/pull/4)\n # and Cubixmeister work\n data = _ReadIntoMemory()\n c_callback = unrarlib.UNRARCALLBACK(data._callback)\n unrarlib.RARSetCallback(handle, c_callback, 0)\n\n try:\n rarinfo = self._read_header(handle)\n while rarinfo is not None:\n if rarinfo.filename == member:\n self._process_current(handle, constants.RAR_TEST)\n break\n else:\n self._process_current(handle, constants.RAR_SKIP)\n rarinfo = self._read_header(handle)\n\n if rarinfo is None:\n data = None\n\n except unrarlib.MissingPassword:\n raise RuntimeError(\"File is encrypted, password required\")\n except unrarlib.BadPassword:\n raise RuntimeError(\"Bad password for File\")\n except unrarlib.BadDataError:\n if password is not None:\n raise RuntimeError(\"File CRC error or incorrect password\")\n else:\n raise RuntimeError(\"File CRC error\")\n except unrarlib.UnrarException as e:\n raise BadRarFile(\"Bad RAR archive data: %s\" % str(e))\n finally:\n self._close(handle)\n\n if data is None:\n raise KeyError('There is no item named %r in the archive' % member)\n\n # return file-like object\n return data.get_bytes()", "response": "Open an archive and return a file - like object for the specified member."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef namelist(self):\n names = []\n for member in self.filelist:\n names.append(member.filename)\n return names", "response": "Return a list of file names in the archive."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the instance of RarInfo given name.", "response": "def getinfo(self, name):\n \"\"\"Return the instance of RarInfo given 'name'.\"\"\"\n rarinfo = self.NameToInfo.get(name)\n if rarinfo is None:\n raise KeyError('There is no item named %r in the archive' % name)\n return rarinfo"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprints a table of contents for the RAR file.", "response": "def printdir(self):\n \"\"\"Print a table of contents for the RAR file.\"\"\"\n print(\"%-46s %19s %12s\" % (\"File Name\", \"Modified \", \"Size\"))\n for rarinfo in self.filelist:\n date = \"%d-%02d-%02d %02d:%02d:%02d\" % rarinfo.date_time[:6]\n print(\"%-46s %s %12d\" % (\n rarinfo.filename, date, rarinfo.file_size))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef extract(self, member, path=None, pwd=None):\n if isinstance(member, RarInfo):\n member = member.filename\n\n if path is None:\n path = os.getcwd()\n\n self._extract_members([member], path, pwd)\n return os.path.join(path, member)", "response": "Extract a member from the archive to the current working directory pwd is the pwd of the member."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef extractall(self, path=None, members=None, pwd=None):\n if members is None:\n members = self.namelist()\n self._extract_members(members, path, pwd)", "response": "Extract all members from the archive to the current working\n directory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _extract_members(self, members, targetpath, pwd):\n archive = unrarlib.RAROpenArchiveDataEx(\n self.filename, mode=constants.RAR_OM_EXTRACT)\n handle = self._open(archive)\n\n password = pwd or self.pwd\n if password is not None:\n unrarlib.RARSetPassword(handle, b(password))\n\n try:\n rarinfo = self._read_header(handle)\n while rarinfo is not None:\n if rarinfo.filename in members:\n self._process_current(\n handle, constants.RAR_EXTRACT, targetpath)\n else:\n self._process_current(handle, constants.RAR_SKIP)\n rarinfo = self._read_header(handle)\n except unrarlib.MissingPassword:\n raise RuntimeError(\"File is encrypted, password required\")\n except unrarlib.BadPassword:\n raise RuntimeError(\"Bad password for File\")\n except unrarlib.BadDataError:\n raise RuntimeError(\"File CRC Error\")\n except unrarlib.UnrarException as e:\n raise BadRarFile(\"Bad RAR archive data: %s\" % str(e))\n finally:\n self._close(handle)", "response": "Extracts the RarInfo objects members from the RAR archive at targetpath."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dostime_to_timetuple(dostime):\n dostime = dostime >> 16\n dostime = dostime & 0xffff\n day = dostime & 0x1f\n month = (dostime >> 5) & 0xf\n year = 1980 + (dostime >> 9)\n second = 2 * (dostime & 0x1f)\n minute = (dostime >> 5) & 0x3f\n hour = dostime >> 11\n return (year, month, day, hour, minute, second)", "response": "Convert a RAR archive member DOS time to a Python time tuple."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _c_func(func, restype, argtypes, errcheck=None):\n func.restype = restype\n func.argtypes = argtypes\n if errcheck is not None:\n func.errcheck = errcheck\n return func", "response": "Wrap c function setting prototype."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _load_savefile_header(file_h):\n try:\n raw_savefile_header = file_h.read(24)\n except UnicodeDecodeError:\n print(\"\\nMake sure the input file is opened in read binary, 'rb'\\n\")\n raise InvalidEncoding(\"Could not read file; it might not be opened in binary mode.\")\n\n # in case the capture file is not the same endianness as ours, we have to\n # use the correct byte order for the file header\n if raw_savefile_header[:4] in [struct.pack(\">I\", _MAGIC_NUMBER),\n struct.pack(\">I\", _MAGIC_NUMBER_NS)]:\n byte_order = b'big'\n unpacked = struct.unpack('>IhhIIII', raw_savefile_header)\n elif raw_savefile_header[:4] in [struct.pack(\"IIII', raw_packet_header)\n else:\n packet_header = struct.unpack(' 0:\n layers -= 1\n raw_packet = linklayer.clookup(hdrp[0].ll_type)(raw_packet_data,\n layers=layers)\n else:\n raw_packet = raw_packet_data\n\n packet = pcap_packet(hdrp, timestamp, timestamp_us, capture_len,\n packet_len, raw_packet)\n return packet", "response": "Reads a packet from the capture file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngiving a raw IPv4 address return it in dotted quad notation.", "response": "def parse_ipv4(address):\n \"\"\"\n Given a raw IPv4 address (i.e. as an unsigned integer), return it in\n dotted quad notation.\n \"\"\"\n raw = struct.pack('I', address)\n octets = struct.unpack('BBBB', raw)[::-1]\n ipv4 = b'.'.join([('%d' % o).encode('ascii') for o in bytearray(octets)])\n return ipv4"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nremove the IP packet layer yielding the transport layer.", "response": "def strip_ip(packet):\n \"\"\"\n Remove the IP packet layer, yielding the transport layer.\n \"\"\"\n if not isinstance(packet, IP):\n packet = IP(packet)\n payload = packet.payload\n\n return payload"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef strip_ethernet(packet):\n if not isinstance(packet, Ethernet):\n packet = Ethernet(packet)\n payload = packet.payload\n\n return payload", "response": "Strip the Ethernet frame from a packet."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef load_network(self, layers=1):\n if layers:\n ctor = payload_type(self.type)[0]\n if ctor:\n ctor = ctor\n payload = self.payload\n self.payload = ctor(payload, layers - 1)\n else:\n # if no type is found, do not touch the packet.\n pass", "response": "Load the network packet into the internal memory."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalls wifi packet discriminator and constructor.", "response": "def WIFI(frame, no_rtap=False):\n \"\"\"calls wifi packet discriminator and constructor.\n :frame: ctypes.Structure\n :no_rtap: Bool\n :return: packet object in success\n :return: int\n -1 on known error\n :return: int\n -2 on unknown error\n \"\"\"\n pack = None\n try:\n pack = WiHelper.get_wifi_packet(frame, no_rtap)\n except Exception as e:\n logging.exception(e)\n return pack"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndiscriminate Wi - Fi packet and creates packet object.", "response": "def get_wifi_packet(frame, no_rtap=False):\n \"\"\"Discriminates Wi-Fi packet and creates\n packet object.\n :frame: ctypes.Structure\n :no_rtap: Bool\n :return: obj\n Wi-Fi packet\n \"\"\"\n _, packet = WiHelper._strip_rtap(frame)\n frame_control = struct.unpack('BB', packet[:2])\n cat = (frame_control[0] >> 2) & 0b0011\n s_type = frame_control[0] >> 4\n\n if cat not in _CATEGORIES_.keys():\n logging.warning(\"unknown category: %d\" % (cat))\n return Unknown(frame, no_rtap)\n\n if s_type not in _SUBTYPES_[cat].keys():\n logging.warning(\"unknown subtype %d in %s category\" % (s_type, _CATEGORIES_[cat]))\n return Unknown(frame, no_rtap)\n\n if cat == 0:\n if s_type == 4:\n return ProbeReq(frame, no_rtap)\n elif s_type == 5:\n return ProbeResp(frame, no_rtap)\n elif s_type == 8:\n return Beacon(frame, no_rtap)\n else:\n return Management(frame, no_rtap)\n elif cat == 1:\n if s_type == 11:\n return RTS(frame, no_rtap)\n elif s_type == 12:\n return CTS(frame, no_rtap)\n elif s_type == 9:\n return BACK(frame, no_rtap)\n else:\n return Control(frame, no_rtap)\n elif cat == 2:\n if s_type == 8:\n return QosData(frame, no_rtap, parse_amsdu=True)\n else:\n return Data(frame, no_rtap)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _strip_rtap(frame):\n rtap_len = WiHelper.__get_rtap_len(frame)\n rtap = frame[:rtap_len]\n packet = frame[rtap_len:]\n return rtap, packet", "response": "strip injected radiotap header."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstrip the present bit from the given payload and return a namedtuple containing the fields that are present in the radiotap meta - data.", "response": "def strip_present(payload):\n \"\"\"strip(4 byte) radiotap.present. Those are flags that\n identify existence of incoming radiotap meta-data.\n :idx: int\n :return: str\n :return: namedtuple\n \"\"\"\n present = collections.namedtuple(\n 'present', ['tsft', 'flags', 'rate', 'channel', 'fhss',\n 'dbm_antsignal', 'dbm_antnoise', 'lock_quality',\n 'tx_attenuation', 'db_tx_attenuation', 'dbm_tx_power',\n 'antenna', 'db_antsignal', 'db_antnoise', 'rxflags',\n 'txflags', 'rts_retries', 'data_retries', 'xchannel',\n 'mcs', 'ampdu', 'vht', 'rtap_ns', 'ven_ns', 'ext'])\n\n val = struct.unpack('> 4\n mcs = (mcs_nss & 0xf0) >> 4\n coding = (coding & 2**i) >> i\n if i == 0:\n vht.user_0 = user(nss, mcs, coding)\n elif i == 1:\n vht.user_1 = user(nss, mcs, coding)\n elif i == 2:\n vht.user_2 = user(nss, mcs, coding)\n elif i == 3:\n vht.user_3 = user(nss, mcs, coding)\n\n return idx + 12, vht", "response": "strips the vht entry from the internal buffer and returns the index in the internal buffer."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_protocol(self):\n if self.present.mcs:\n return '.11n'\n\n if self.present.vht:\n return '.11ac'\n\n if self.present.channel and hasattr(self, 'chan'):\n if self.chan.five_g:\n if self.chan.ofdm:\n return '.11a'\n elif self.chan.two_g:\n if self.chan.cck:\n return '.11b'\n elif self.chan.ofdm or self.chan.dynamic:\n return '.11g'\n return 'None'", "response": "extract 802. 11 protocol from radiotap. channel. flags\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_shark_field(self, fields):\n keys, exist, out = None, {}, None\n\n if isinstance(fields, str):\n fields = [fields]\n elif not isinstance(fields, list):\n logging.error('invalid input type')\n return None\n\n out = dict.fromkeys(fields)\n\n if hasattr(self, '_shark_'):\n exist.update(self._shark_)\n\n if hasattr(self, '_s_shark_'):\n exist.update(self._s_shark_)\n\n if hasattr(self.radiotap, '_r_shark_'):\n exist.update(self.radiotap._r_shark_)\n\n keys = exist.keys()\n\n for elem in fields:\n if elem in keys:\n obj_field, tmp = exist[elem], None\n try:\n tmp = operator.attrgetter(obj_field)(self)\n except AttributeError:\n tmp = None\n if not tmp:\n try:\n tmp = operator.attrgetter(obj_field)(self.radiotap)\n except AttributeError:\n tmp = None\n out[elem] = tmp\n return out", "response": "get parameters via wireshark syntax."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_mac_addr(mac_addr):\n mac_addr = bytearray(mac_addr)\n mac = b':'.join([('%02x' % o).encode('ascii') for o in mac_addr])\n return mac", "response": "converts bytes to mac addr format\n 11 : 22 : 33. aa. cc. cz"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef strip_mac_addrs(self):\n qos_idx, seq_idx = 0, 0\n sa, ta, ra, da, bssid = None, None, None, None, None\n\n if self.to_ds == 1 and self.from_ds == 1:\n (ra, ta, da) = struct.unpack('!6s6s6s', self._packet[4:22])\n sa = struct.unpack('!6s', self._packet[24:30])[0]\n qos_idx = 30\n seq_idx = 22\n elif self.to_ds == 0 and self.from_ds == 1:\n (ra, ta, sa) = struct.unpack('!6s6s6s', self._packet[4:22])\n qos_idx = 24\n seq_idx = 22\n elif self.to_ds == 1 and self.from_ds == 0:\n (ra, ta, da) = struct.unpack('!6s6s6s', self._packet[4:22])\n qos_idx = 24\n seq_idx = 22\n elif self.to_ds == 0 and self.from_ds == 0:\n (ra, ta, bssid) = struct.unpack('!6s6s6s', self._packet[4:22])\n qos_idx = 24\n seq_idx = 22\n\n if ta is not None:\n ta = Wifi.get_mac_addr(ta)\n if ra is not None:\n ra = Wifi.get_mac_addr(ra)\n if sa is not None:\n sa = Wifi.get_mac_addr(sa)\n if da is not None:\n da = Wifi.get_mac_addr(da)\n if bssid is not None:\n bssid = Wifi.get_mac_addr(bssid)\n\n return seq_idx, qos_idx, sa, ta, ra, da, bssid", "response": "strips the MAC addresses from the packet and returns the corresponding entry in the sequence control structure."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef strip_seq_cntrl(self, idx):\n seq_cntrl = struct.unpack('H', self._packet[idx:idx + 2])[0]\n seq_num = seq_cntrl >> 4\n frag_num = seq_cntrl & 0x000f\n return seq_num, frag_num", "response": "strips the seq and fram number information."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef strip_qos_cntrl(self, idx, prot_type):\n qos_cntrl, = struct.unpack('H', self._packet[idx:idx + 2])\n qos_cntrl_bits = format(qos_cntrl, '016b')[::-1]\n qos_pri = qos_cntrl & 0x000f\n qos_bit = int(qos_cntrl_bits[5])\n qos_ack = int(qos_cntrl_bits[6:8], 2)\n amsdupresent = 0\n if prot_type == '.11ac':\n amsdupresent = int(qos_cntrl_bits[7])\n return 2, qos_pri, qos_bit, qos_ack, amsdupresent", "response": "strips the QoS control packet from the packet and returns the tuple."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef strip_ccmp(self, idx):\n ccmp_extiv = None\n if len(self._packet[idx:]) >= 8:\n raw_bytes = self._packet[idx:idx + 8]\n ccmp_extiv, = struct.unpack_from('Q', raw_bytes, 0)\n return 8, ccmp_extiv", "response": "strips the CCMP extended initialization vector from the packet at the given index"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstrip a single mac servis data unit from the packet at the given index", "response": "def strip_msdu(self, idx):\n \"\"\"strip single mac servis data unit(msdu)\n see -> https://mrncciew.com/2014/11/01/cwap-802-11-data-frame-aggregation/\n :idx: int\n :return: dict\n msdu\n :return: int\n number of processed bytes\n \"\"\"\n # length of msdu payload has to be multiple of 4,\n # this guaranteed with padding\n padding = 0\n len_payload = 0\n msdu = {\n 'llc': {},\n 'wlan.da': None,\n 'wlan.sa': None,\n 'payload': None,\n 'length': 0\n }\n\n (da_mac, sa_mac) = struct.unpack('!6s6s', self._packet[idx:idx + 12])\n msdu['wlan.da'] = Wifi.get_mac_addr(da_mac)\n msdu['wlan.sa'] = Wifi.get_mac_addr(sa_mac)\n idx += 12\n msdu['length'] = struct.unpack('!H', self._packet[idx:idx + 2])[0]\n idx += 2\n offset, msdu['llc'] = self.strip_llc(idx)\n idx += offset\n len_payload = msdu['length'] - offset\n msdu['payload'] = self._packet[idx:idx + len_payload]\n padding = 4 - (len_payload % 4)\n return msdu, msdu['length'] + padding + 12"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef strip_llc(self, idx):\n llc = {}\n snap = 170\n llc_dsap = struct.unpack('B', self._packet[idx:idx + 1])[0]\n llc['dsap.dsap'] = llc_dsap >> 1\n llc['dsap.ig'] = llc_dsap & 0b01\n idx += 1\n llc_ssap = struct.unpack('B', self._packet[idx:idx + 1])[0]\n llc['ssap.sap'] = llc_ssap >> 1\n llc['ssap.cr'] = llc_ssap & 0b01\n idx += 1\n if llc_dsap == snap and llc_ssap == snap:\n llc_control = struct.unpack('B', self._packet[idx:idx + 1])[0]\n llc['control.u_modifier_cmd'] = llc_control >> 2\n llc['control.ftype'] = llc_control & 0x03\n idx += 1\n llc['organization_code'] = self._packet[idx:idx + 3]\n idx += 3\n llc['type'] = self._packet[idx:idx + 2]\n return 8, llc\n else:\n return 4, llc", "response": "strips the 4 or 8 byte logical link control headers from the packet at the given index and returns the number of processed bytes and the object that is returned."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse_tagged_params(raw_tagged_params):\n fcs_len = 4 # wlan.fcs (4 bytes)\n idx = 0\n tagged_params = []\n while idx < len(raw_tagged_params) - fcs_len:\n tag_num, tag_len = struct.unpack('BB', raw_tagged_params[idx:idx + 2])\n idx += 2\n if len(raw_tagged_params) >= idx + tag_len:\n param = {}\n param['number'], param['length'] = tag_num, tag_len\n payload = raw_tagged_params[idx:idx + tag_len]\n if tag_num in MNGMT_TAGS:\n param['name'] = MNGMT_TAGS[tag_num]\n if MNGMT_TAGS[tag_num] == 'TAG_VENDOR_SPECIFIC_IE':\n param['payload'] = Management.parse_vendor_ie(payload)\n else:\n param['payload'] = payload\n else:\n param['name'] = None\n tagged_params.append(param)\n idx += tag_len\n else:\n logging.warning('out tag length header points out of boundary')\n log_msg = 'index: {p_idx}, pack_len: {p_len}'\n log_msg = log_msg.format(p_idx=idx + tag_len,\n p_len=len(raw_tagged_params))\n logging.warning(log_msg)\n return 1, tagged_params\n return 0, tagged_params", "response": "parse tagged information elements wlan_mgt. tag\n which has generic type - length - value structure\n type - length - value structure\n length - value structure\nosity - length of structure\nosity - payload of payload of payload of tag"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_fixed_capabils(payload):\n if len(payload) != 2:\n return None\n capabils = {}\n fix_cap = struct.unpack('H', payload)[0]\n cap_bits = format(fix_cap, '016b')[::-1]\n capabils['ess'] = int(cap_bits[0]) # Extended Service Set\n capabils['ibss'] = int(cap_bits[1]) # Independent Basic Service Set\n capabils['priv'] = int(cap_bits[4]) # Privacy\n capabils['short_preamble'] = int(cap_bits[5]) # Short Preamble\n capabils['pbcc'] = int(cap_bits[6]) # Packet Binary Convolutional Code\n capabils['chan_agility'] = int(cap_bits[7]) # Channel Agility\n capabils['spec_man'] = int(cap_bits[8]) # Spectrum Management\n capabils['short_slot'] = int(cap_bits[10]) # Short Slot Time\n capabils['apsd'] = int(cap_bits[11]) # Automatic Power Save Delivery\n capabils['radio_meas'] = int(cap_bits[12]) # Radio Measurement\n capabils['dss_ofdm'] = int(cap_bits[13]) # Direct Spread Spectrum\n capabils['del_back'] = int(cap_bits[14]) # Delayed Block Acknowledgement\n capabils['imm_back'] = int(cap_bits[15]) # Immediate Block Acknowledgement\n return capabils", "response": "Returns dict of fixed capabilities from wlan_mgt. fixed. capabilities\n ."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing vendor specific information element oui -> organizationally unique identifier", "response": "def parse_vendor_ie(payload):\n \"\"\"parse vendor specific information element\n oui -> organizationally unique identifier\n first 3 bytes of mac addresses\n see:https://www.wireshark.org/tools/oui-lookup.html\n strip wlan_mgt.tag.oui(3 bytes),\n wlan_mgt.tag.vendor.oui.type(1 byte)\n wlan_mgt.tag.vendor.data (varies)\n :payload: ctypes.structure\n :return: dict\n {'oui':00-11-22, 'oui_type':1, 'oui_data':ctypes.structure}\n \"\"\"\n output = {}\n oui = struct.unpack('BBB', payload[0:3])\n oui = b'-'.join([('%02x' % o).encode('ascii') for o in oui])\n oui_type = struct.unpack('B', payload[3:4])[0]\n oui_data = payload[4:]\n output['oui'] = oui.upper()\n output['oui_type'] = oui_type\n output['oui_data'] = oui_data\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstrip the fixed parameters from the payload and returns the timestamp interval and capabils", "response": "def strip_fixed_params(payload):\n \"\"\"strip(12 byte) wlan_mgt.fixed.all\n :payload: ctypes.structure\n :return: int\n timestamp\n :return: int\n beacon interval\n :return: dict\n capabilities\n \"\"\"\n if len(payload) != 12:\n return None, None, None\n idx = 0\n timestamp = Management.get_timestamp(payload[idx:idx + 8])\n idx += 8\n interval = Management.get_interval(payload[idx:idx + 2])\n idx += 2\n capabils = Management.get_fixed_capabils(payload[idx:idx + 2])\n return timestamp, interval, capabils"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_valid_mac_oui(mac_block):\n if len(mac_block) != 8:\n return 0\n if ':' in mac_block:\n if len(mac_block.split(':')) != 3:\n return 0\n elif '-' in mac_block:\n if len(mac_block.split('-')) != 3:\n return 0\n return 1", "response": "checks whether the given mac block is in format of\n 00 - 11 - 22 or 00 - 11 - 22 or 00 - 11 - 22 or 00 - 11 - 22."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_fixed_capabils(self, capabils):\n self.ess = capabils['ess']\n self.ibss = capabils['ibss']\n self.priv = capabils['priv']\n self.short_preamble = capabils['short_preamble']\n self.pbcc = capabils['pbcc']\n self.chan_agility = capabils['chan_agility']\n self.spec_man = capabils['spec_man']\n self.short_slot = capabils['short_slot']\n self.apsd = capabils['apsd']\n self.radio_meas = capabils['radio_meas']\n self.dss_ofdm = capabils['dss_ofdm']\n self.del_back = capabils['del_back']\n self.imm_back = capabils['imm_back']", "response": "set keys of capabils into fields of object"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_vendor_ies(self, mac_block=None, oui_type=None):\n vendor_ies = []\n if mac_block is not None:\n if Management.is_valid_mac_oui(mac_block):\n mac_block = mac_block.upper()\n if ':' in mac_block:\n mac_block.replace(':', '-')\n else:\n logging.warning(\"invalid oui macblock\")\n return None\n\n for elem in self.tagged_params:\n tag_num = elem['number']\n if MNGMT_TAGS[tag_num] == 'TAG_VENDOR_SPECIFIC_IE':\n if mac_block is None:\n vendor_ies.append(elem)\n elif elem['payload']['oui'] == mac_block.encode('ascii'):\n if oui_type is None:\n vendor_ies.append(elem)\n elif elem['payload']['oui_type'] == oui_type:\n vendor_ies.append(elem)\n return vendor_ies", "response": "get vendor information elements querying the tag"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_shark_field(self, fields):\n out = super(BACK, self).get_shark_field(fields)\n out.update({'acked_seqs': self.acked_seqs,\n 'bitmap_str': self.bitmap_str})\n return out", "response": "Returns a dictionary of the shark fields for the current object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstrip the cntrl from the payload and returns the ackpolicy and multitid", "response": "def strip_cntrl(payload):\n \"\"\"strip(2 byte) wlan.ba.control\n :payload: ctypes.structure\n :return: int\n multitid (tid: traffic indicator)\n :return: int\n ackpolicy\n \"\"\"\n cntrl = struct.unpack('H', payload)[0]\n cntrl_bits = format(cntrl, '016b')[::-1]\n ackpolicy = int(cntrl_bits[0])\n multitid = int(cntrl_bits[1])\n return ackpolicy, multitid"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef strip_ssc(payload):\n ssc = struct.unpack('H', payload)[0]\n ssc_seq = ssc >> 4\n ssc_frag = ssc & 0x000f\n return ssc_seq, ssc_frag", "response": "strips the SSC from the given payload and returns the sequence and fragment number of the SSC."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstrip 8 byte bitmap", "response": "def strip_bitmap_str(payload):\n \"\"\"strip(8 byte) wlan.ba.bm\n :payload: ctypes.structure\n :return: str\n bitmap\n \"\"\"\n bitmap = struct.unpack('BBBBBBBB', payload)\n bitmap_str = ''\n for elem in bitmap:\n bitmap_str += format(elem, '08b')[::-1]\n return bitmap_str"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nextract acknowledged sequences from bitmap and starting sequence number.", "response": "def extract_acked_seqs(bitmap, ssc_seq):\n \"\"\"extracts acknowledged sequences from bitmap and\n starting sequence number.\n :bitmap: str\n :ssc_seq: int\n :return: int[]\n acknowledged sequence numbers\n \"\"\"\n acked_seqs = []\n for idx, val in enumerate(bitmap):\n if int(val) == 1:\n seq = (ssc_seq + idx) % 4096\n acked_seqs.append(seq)\n return acked_seqs"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsends a request and gets a response from the Plivo REST API", "response": "def request(self, path, method=None, data={}):\n \"\"\"sends a request and gets a response from the Plivo REST API\n\n path: the URL (relative to the endpoint URL, after the /v1\n method: the HTTP method to use, defaults to POST\n data: for POST or PUT, a dict of data to send\n\n returns Plivo response in XML or raises an exception on error\n \"\"\"\n if not path:\n raise ValueError('Invalid path parameter')\n if method and method not in ['GET', 'POST', 'DELETE', 'PUT']:\n raise NotImplementedError(\n 'HTTP %s method not implemented' % method)\n\n if path[0] == '/':\n uri = self.url + path\n else:\n uri = self.url + '/' + path\n\n if APPENGINE:\n return json.loads(self._appengine_fetch(uri, data, method))\n return json.loads(self._urllib2_fetch(uri, data, method))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nrests Reload Plivo Config helper", "response": "def reload_config(self, call_params):\n \"\"\"REST Reload Plivo Config helper\n \"\"\"\n path = '/' + self.api_version + '/ReloadConfig/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nresting Reload Plivo Cache Config helper", "response": "def reload_cache_config(self, call_params):\n \"\"\"REST Reload Plivo Cache Config helper\n \"\"\"\n path = '/' + self.api_version + '/ReloadCacheConfig/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrest Group Call Helper", "response": "def group_call(self, call_params):\n \"\"\"REST GroupCalls Helper\n \"\"\"\n path = '/' + self.api_version + '/GroupCall/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrests Transfer Live Call Helper", "response": "def transfer_call(self, call_params):\n \"\"\"REST Transfer Live Call Helper\n \"\"\"\n path = '/' + self.api_version + '/TransferCall/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nrests Hangup All Live Calls Helper", "response": "def hangup_all_calls(self):\n \"\"\"REST Hangup All Live Calls Helper\n \"\"\"\n path = '/' + self.api_version + '/HangupAllCalls/'\n method = 'POST'\n return self.request(path, method)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef hangup_call(self, call_params):\n path = '/' + self.api_version + '/HangupCall/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "Hangup a live call"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrests Schedule Hangup Helper", "response": "def schedule_hangup(self, call_params):\n \"\"\"REST Schedule Hangup Helper\n \"\"\"\n path = '/' + self.api_version + '/ScheduleHangup/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cancel_scheduled_hangup(self, call_params):\n path = '/' + self.api_version + '/CancelScheduledHangup/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "This method cancels a scheduled hangup"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nresting Conference Mute helper", "response": "def conference_mute(self, call_params):\n \"\"\"REST Conference Mute helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceMute/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef play(self, call_params):\n path = '/' + self.api_version + '/Play/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Play something on a Call Helper\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef play_stop(self, call_params):\n path = '/' + self.api_version + '/PlayStop/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST PlayStop on a Call Helper\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrests Schedule playing something on a call Helper", "response": "def schedule_play(self, call_params):\n \"\"\"REST Schedule playing something on a call Helper\n \"\"\"\n path = '/' + self.api_version + '/SchedulePlay/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cancel_scheduled_play(self, call_params):\n path = '/' + self.api_version + '/CancelScheduledPlay/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "This method cancels a scheduled play"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrests Add soundtouch audio effects to a Call", "response": "def sound_touch(self, call_params):\n \"\"\"REST Add soundtouch audio effects to a Call\n \"\"\"\n path = '/' + self.api_version + '/SoundTouch/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrest Remove soundtouch audio effects on a Call", "response": "def sound_touch_stop(self, call_params):\n \"\"\"REST Remove soundtouch audio effects on a Call\n \"\"\"\n path = '/' + self.api_version + '/SoundTouchStop/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrest Send digits to a Call", "response": "def send_digits(self, call_params):\n \"\"\"REST Send digits to a Call\n \"\"\"\n path = '/' + self.api_version + '/SendDigits/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef conference_unmute(self, call_params):\n path = '/' + self.api_version + '/ConferenceUnmute/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Conference Unmute helper"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef conference_kick(self, call_params):\n path = '/' + self.api_version + '/ConferenceKick/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Conference Kick helper"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrests Conference Hangup helper", "response": "def conference_hangup(self, call_params):\n \"\"\"REST Conference Hangup helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceHangup/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrest Conference Deaf helper", "response": "def conference_deaf(self, call_params):\n \"\"\"REST Conference Deaf helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceDeaf/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrests Conference Undeaf helper", "response": "def conference_undeaf(self, call_params):\n \"\"\"REST Conference Undeaf helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceUndeaf/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef conference_record_start(self, call_params):\n path = '/' + self.api_version + '/ConferenceRecordStart/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Conference Record Start"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef conference_record_stop(self, call_params):\n path = '/' + self.api_version + '/ConferenceRecordStop/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "This method stops a conference record for a specific set of attributes."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef conference_play(self, call_params):\n path = '/' + self.api_version + '/ConferencePlay/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Conference Play helper"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresting Conference Speak helper", "response": "def conference_speak(self, call_params):\n \"\"\"REST Conference Speak helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceSpeak/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef conference_list(self, call_params):\n path = '/' + self.api_version + '/ConferenceList/'\n method = 'POST'\n return self.request(path, method, call_params)", "response": "REST Conference List Helper\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nresting Conference List Members Helper", "response": "def conference_list_members(self, call_params):\n \"\"\"REST Conference List Members Helper\n \"\"\"\n path = '/' + self.api_version + '/ConferenceListMembers/'\n method = 'POST'\n return self.request(path, method, call_params)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _xml(self, root):\n element = root.createElement(self.name)\n\n # Add attributes\n keys = self.attrs.keys()\n keys.sort()\n for a in keys:\n element.setAttribute(a, self.attrs[a])\n\n if self.body:\n text = root.createTextNode(self.body)\n element.appendChild(text)\n\n for c in self.elements:\n element.appendChild(c._xml(root))\n\n return element", "response": "Return an XML element representing this element."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nvalidate a request from plivo", "response": "def validateRequest(self, uri, postVars, expectedSignature):\n \"\"\"validate a request from plivo\n\n uri: the full URI that Plivo requested on your server\n postVars: post vars that Plivo sent with the request\n expectedSignature: signature in HTTP X-Plivo-Signature header\n\n returns true if the request passes validation, false if not\n \"\"\"\n\n # append the POST variables sorted by key to the uri\n s = uri\n for k, v in sorted(postVars.items()):\n s += k + v\n\n # compute signature and compare signatures\n return (base64.encodestring(hmac.new(self.auth_token, s, sha1).digest()).\\\n strip() == expectedSignature)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_node(self,label):\n '''Return a node with label. Create node if label is new'''\n try:\n n = self._nodes[label]\n except KeyError:\n n = Node()\n n['label'] = label\n self._nodes[label]=n\n return n", "response": "Return a node with label. Create node if it does not exist."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd an edge between nodes n1 and n2.", "response": "def add_edge(self, n1_label, n2_label,directed=False):\n \"\"\"\n Get or create edges using get_or_create_node\n \"\"\"\n n1 = self.add_node(n1_label)\n n2 = self.add_node(n2_label)\n e = Edge(n1, n2, directed)\n self._edges.append(e)\n return e"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse a DOM into a Graph object.", "response": "def parse_dom(dom):\n \"\"\"Parse dom into a Graph.\n\n :param dom: dom as returned by minidom.parse or minidom.parseString\n :return: A Graph representation\n \"\"\"\n root = dom.getElementsByTagName(\"graphml\")[0]\n graph = root.getElementsByTagName(\"graph\")[0]\n name = graph.getAttribute('id')\n\n g = Graph(name)\n\n # # Get attributes\n # attributes = []\n # for attr in root.getElementsByTagName(\"key\"):\n # attributes.append(attr)\n\n # Get nodes\n for node in graph.getElementsByTagName(\"node\"):\n n = g.add_node(id=node.getAttribute('id'))\n\n for attr in node.getElementsByTagName(\"data\"):\n if attr.firstChild:\n n[attr.getAttribute(\"key\")] = attr.firstChild.data\n else:\n n[attr.getAttribute(\"key\")] = \"\"\n\n # Get edges\n for edge in graph.getElementsByTagName(\"edge\"):\n source = edge.getAttribute('source')\n dest = edge.getAttribute('target')\n\n # source/target attributes refer to IDs: http://graphml.graphdrawing.org/xmlns/1.1/graphml-structure.xsd\n e = g.add_edge_by_id(source, dest)\n\n for attr in edge.getElementsByTagName(\"data\"):\n if attr.firstChild:\n e[attr.getAttribute(\"key\")] = attr.firstChild.data\n else:\n e[attr.getAttribute(\"key\")] = \"\"\n\n return g"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_string(self, string):\n dom = minidom.parseString(string)\n return self.parse_dom(dom)", "response": "Parse a string into a Graph.\n object"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the other node that is in the tree", "response": "def node(self, node):\n \"\"\"\n Return the other node\n \"\"\"\n\n if node == self.node1:\n return self.node2\n elif node == self.node2:\n return self.node1\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef consume(self, **kwargs):\n kwargs.setdefault('block', True)\n try:\n while True:\n msg = self.get(**kwargs)\n if msg is None:\n break\n yield msg\n except KeyboardInterrupt:\n print; return", "response": "Return a generator that yields whenever a message is waiting in the the\n queue. Will block if a KeyboardInterrupt is raised."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a message from the queue.", "response": "def get(self, block=False, timeout=None):\n \"\"\"Return a message from the queue. Example:\n \n >>> queue.get()\n 'my message'\n >>> queue.get()\n 'another message'\n \n :param block: whether or not to wait until a msg is available in\n the queue before returning; ``False`` by default\n :param timeout: when using :attr:`block`, if no msg is available\n for :attr:`timeout` in seconds, give up and return ``None``\n \"\"\"\n if block:\n if timeout is None:\n timeout = 0\n msg = self.__redis.blpop(self.key, timeout=timeout)\n if msg is not None:\n msg = msg[1]\n else:\n msg = self.__redis.lpop(self.key)\n if msg is not None and self.serializer is not None:\n msg = self.serializer.loads(msg)\n return msg"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nput one or more messages onto the queue.", "response": "def put(self, *msgs):\n \"\"\"Put one or more messages onto the queue. Example:\n \n >>> queue.put(\"my message\")\n >>> queue.put(\"another message\")\n \n To put messages onto the queue in bulk, which can be significantly\n faster if you have a large number of messages:\n \n >>> queue.put(\"my message\", \"another message\", \"third message\")\n \"\"\"\n if self.serializer is not None:\n msgs = map(self.serializer.dumps, msgs)\n self.__redis.rpush(self.key, *msgs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef worker(self, *args, **kwargs):\n def decorator(worker):\n @wraps(worker)\n def wrapper(*args):\n for msg in self.consume(**kwargs):\n worker(*args + (msg,))\n return wrapper\n if args:\n return decorator(*args)\n return decorator", "response": "Decorator for using a function as a queue worker."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef to_bytes(self, previous: bytes):\n # First, validate the lengths.\n if len(self.conditions) != len(self.body):\n raise exc.CompileError(\"Conditions and body length mismatch!\")\n\n bc = b\"\"\n\n prev_len = len(previous)\n\n # Loop over the conditions and bodies\n for condition, body in zip(self.conditions, self.body):\n # Generate the conditional data.\n cond_bytecode = condition.to_bytecode(previous)\n bc += cond_bytecode\n # Complex calculation. First, generate the bytecode for all tokens in the body. Then\n # we calculate the len() of that. We create a POP_JUMP_IF_FALSE operation that jumps\n # to the instructions after the body code + 3 for the pop call. This is done for all\n # chained IF calls, as if it was an elif call. Else calls are not possible to be\n # auto-generated, but it is possible to emulate them using an elif call that checks\n # for the opposite of the above IF.\n\n # Call the _compile_func method from compiler to compile the body.\n body_bc = compiler.compile_bytecode(body)\n\n bdyl = len(body_bc)\n # Add together the lengths.\n gen_len = prev_len + len(cond_bytecode) + bdyl + 1\n # Generate the POP_JUMP_IF_FALSE instruction\n bc += generate_simple_call(tokens.POP_JUMP_IF_FALSE, gen_len)\n # Add the body_bc\n bc += body_bc\n\n # Update previous_len\n prev_len = len(previous) + len(bc)\n\n return bc", "response": "Generate the Complex Code ahead. Comments have been added in as needed."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef to_bytes_35(self, previous: bytes):\n\n # Calculations ahead.\n bc = b\"\"\n\n # Calculate the length of the iterator.\n it_bc = util.generate_bytecode_from_obb(self.iterator, previous)\n bc += it_bc\n\n # Push a get_iter on.\n bc += util.generate_bytecode_from_obb(tokens.GET_ITER, b\"\")\n prev_len = len(previous) + len(bc)\n # Calculate the bytecode for the body.\n body_bc = b\"\"\n for op in self._body:\n # Add padding bytes to the bytecode to allow if blocks to work.\n padded_bc = previous\n # Add padding for SETUP_LOOP\n padded_bc += b\"\\x00\\x00\\x00\"\n padded_bc += bc\n # Add padding for FOR_ITER\n padded_bc += b\"\\x00\\x00\\x00\"\n # Add previous body\n padded_bc += body_bc\n body_bc += util.generate_bytecode_from_obb(op, padded_bc)\n\n # Add a JUMP_ABSOLUTE\n body_bc += util.generate_simple_call(tokens.JUMP_ABSOLUTE, prev_len + 3)\n\n # Add a POP_TOP\n body_bc += util.generate_bytecode_from_obb(tokens.POP_BLOCK, b\"\")\n\n # Calculate the right lengths.\n # Add a FOR_ITER, using len(body_bc)\n body_bc = util.generate_simple_call(tokens.FOR_ITER, len(body_bc) - 1) + body_bc\n # Add the SETUP_LOOP call\n bc = util.generate_simple_call(tokens.SETUP_LOOP, prev_len + len(body_bc) - 6) + bc + body_bc\n\n return bc", "response": "A to - bytes version of the internal method."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef to_bytes_36(self, previous: bytes):\n # Calculations ahead.\n bc = b\"\"\n\n # Calculate the length of the iterator.\n it_bc = util.generate_bytecode_from_obb(self.iterator, previous)\n bc += it_bc\n\n bc += util.ensure_instruction(tokens.GET_ITER)", "response": "A to - bytes specific to Python 3. 6 and above."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns the correct validator for given objects. Assumes all same type.", "response": "def validate_content(*objs):\n \"\"\"Runs the correct validator for given `obj`ects. Assumes all same type\"\"\"\n from .main import Collection, Module\n validator = {\n Collection: cnxml.validate_collxml,\n Module: cnxml.validate_cnxml,\n }[type(objs[0])]\n return validator(*[obj.file for obj in objs])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef validate_litezip(struct):\n msgs = []\n\n def _fmt_err(err):\n return (Path(err.filename), \"{}:{} -- {}: {}\".format(*(err[1:])))\n\n obj_by_type = {}\n for obj in struct:\n if not is_valid_identifier(obj.id):\n msg = (obj.file.parent,\n \"{} is not a valid identifier\".format(obj.id),)\n logger.info(\"{}: {}\".format(*msg))\n msgs.append(msg)\n obj_by_type.setdefault(type(obj), []).append(obj)\n\n for obtype in obj_by_type:\n content_msgs = list([_fmt_err(err) for err in\n validate_content(*obj_by_type[obtype])])\n for msg in content_msgs:\n logger.info(\"{}: {}\".format(*msg))\n msgs.extend(content_msgs)\n return msgs", "response": "Validate the given litezip as struct.\n Returns a list of validation messages."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure that an instruction is Python 3. 6 + compatible.", "response": "def ensure_instruction(instruction: int) -> bytes:\n \"\"\"\n Wraps an instruction to be Python 3.6+ compatible. This does nothing on Python 3.5 and below.\n\n This is most useful for operating on bare, single-width instructions such as\n ``RETURN_FUNCTION`` in a version portable way.\n\n :param instruction: The instruction integer to use.\n :return: A safe bytes object, if applicable.\n \"\"\"\n if PY36:\n return instruction.to_bytes(2, byteorder=\"little\")\n else:\n return instruction.to_bytes(1, byteorder=\"little\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating a simple call for the current node.", "response": "def generate_simple_call(opcode: int, index: int):\n \"\"\"\n Generates a simple call, with an index for something.\n\n :param opcode: The opcode to generate.\n :param index: The index to use as an argument.\n :return:\n \"\"\"\n bs = b\"\"\n # add the opcode\n bs += opcode.to_bytes(1, byteorder=\"little\")\n # Add the index\n if isinstance(index, int):\n if PY36:\n bs += index.to_bytes(1, byteorder=\"little\")\n else:\n bs += index.to_bytes(2, byteorder=\"little\")\n else:\n bs += index\n return bs"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef generate_bytecode_from_obb(obb: object, previous: bytes) -> bytes:\n # Generates bytecode from a specified object, be it a validator or an int or bytes even.\n if isinstance(obb, pyte.superclasses._PyteOp):\n return obb.to_bytes(previous)\n elif isinstance(obb, (pyte.superclasses._PyteAugmentedComparator,\n pyte.superclasses._PyteAugmentedValidator._FakeMathematicalOP)):\n return obb.to_bytes(previous)\n elif isinstance(obb, pyte.superclasses._PyteAugmentedValidator):\n obb.validate()\n return obb.to_load()\n elif isinstance(obb, int):\n return obb.to_bytes((obb.bit_length() + 7) // 8, byteorder=\"little\") or b''\n elif isinstance(obb, bytes):\n return obb\n else:\n raise TypeError(\"`{}` was not a valid bytecode-encodable item\".format(obb))", "response": "Generates a bytecode from an object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_const_info(const_index, const_list):\n argval = const_index\n if const_list is not None:\n try:\n argval = const_list[const_index]\n except IndexError:\n raise ValidationError(\"Consts value out of range: {}\".format(const_index)) from None\n return argval, repr(argval)", "response": "Helper to get optional details about a constant in a resource tree."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd arguments described in the usage string to the argument parser.", "response": "def add_to_parser(parser, usage, ignore_existing=False):\n \"\"\"\n Add arguments described in the usage string to the\n parser. View more details at the docs\n\n Args:\n parser (ArgumentParser): parser to add arguments to\n usage (str): Usage string in the format described above\n ignore_existing (bool): Ignore any arguments that have already been defined\n \"\"\"\n usage = '\\n' + usage\n first_line = [i for i in usage.split('\\n') if i][0]\n indent = ' ' * (len(first_line) - len(first_line.lstrip(' ')))\n usage = usage.replace('\\n' + indent, '\\n')\n usage = usage.replace('\\n...', '')\n\n defaults = {}\n description, *descriptors = usage.split('\\n:')\n description = description.replace('\\n', ' ').strip()\n if description and (not parser.description or not ignore_existing):\n parser.description = description\n for descriptor in descriptors:\n try:\n options, *info = descriptor.split('\\n')\n info = ' '.join(i for i in info if i).replace(' ', '')\n if options.count(' ') == 1:\n if options[0] == '-':\n short, long = options.split(' ')\n var_name = long.strip('-').replace('-', '_')\n parser.add_argument(short, long, dest=var_name, action='store_true', help=info)\n defaults[var_name] = False\n else:\n short, typ = options.split(' ')\n parser.add_argument(short, type=types[typ], help=info)\n else:\n short, long, typ, default = options.split(' ')\n info = info.rstrip() + '. Default: ' + default\n default = '' if default == '-' else default\n parser.add_argument(short, long, type=types[typ], default=default, help=info)\n except ArgumentError:\n if not ignore_existing:\n raise\n except Exception as e:\n print(e.__class__.__name__ + ': ' + str(e))\n print('While parsing:')\n print(descriptor)\n raise ValueError('Failed to create parser from usage string')"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a byte string containing the bytecode of the given list of Pyte objects.", "response": "def compile_bytecode(code: list) -> bytes:\n \"\"\"\n Compiles Pyte objects into a bytecode list.\n\n :param code: A list of objects to compile.\n :return: The computed bytecode.\n \"\"\"\n bc = b\"\"\n for i, op in enumerate(code):\n try:\n # Get the bytecode.\n if isinstance(op, _PyteOp) or isinstance(op, _PyteAugmentedComparator):\n bc_op = op.to_bytes(bc)\n elif isinstance(op, int):\n bc_op = op.to_bytes(1, byteorder=\"little\")\n elif isinstance(op, bytes):\n bc_op = op\n else:\n raise CompileError(\"Could not compile code of type {}\".format(type(op)))\n bc += bc_op\n except Exception as e:\n print(\"Fatal compiliation error on operator {i} ({op}).\".format(i=i, op=op))\n raise e\n\n return bc"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsimulates the actions of the stack.", "response": "def _simulate_stack(code: list) -> int:\n \"\"\"\n Simulates the actions of the stack, to check safety.\n\n This returns the maximum needed stack.\n \"\"\"\n\n max_stack = 0\n curr_stack = 0\n\n def _check_stack(ins):\n if curr_stack < 0:\n raise CompileError(\"Stack turned negative on instruction: {}\".format(ins))\n if curr_stack > max_stack:\n return curr_stack\n\n # Iterate over the bytecode.\n for instruction in code:\n assert isinstance(instruction, dis.Instruction)\n if instruction.arg is not None:\n try:\n effect = dis.stack_effect(instruction.opcode, instruction.arg)\n except ValueError as e:\n raise CompileError(\"Invalid opcode `{}` when compiling\"\n .format(instruction.opcode)) from e\n else:\n try:\n effect = dis.stack_effect(instruction.opcode)\n except ValueError as e:\n raise CompileError(\"Invalid opcode `{}` when compiling\"\n .format(instruction.opcode)) from e\n curr_stack += effect\n # Re-check the stack.\n _should_new_stack = _check_stack(instruction)\n if _should_new_stack:\n max_stack = _should_new_stack\n\n return max_stack"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef compile(code: list, consts: list, names: list, varnames: list,\n func_name: str = \"\",\n arg_count: int = 0, kwarg_defaults: Tuple[Any] = (), use_safety_wrapper: bool = True):\n \"\"\"\n Compiles a set of bytecode instructions into a working function, using Python's bytecode\n compiler.\n\n :param code: A list of bytecode instructions.\n :param consts: A list of constants to compile into the function.\n :param names: A list of names to compile into the function.\n :param varnames: A list of ``varnames`` to compile into the function.\n :param func_name: The name of the function to use.\n :param arg_count: The number of arguments this function takes. Must be ``<= len(varnames)``.\n :param kwarg_defaults: A tuple of defaults for kwargs.\n :param use_safety_wrapper: Use the safety wrapper? This hijacks SystemError to print better \\\n stack traces.\n \"\"\"\n varnames = tuple(varnames)\n consts = tuple(consts)\n names = tuple(names)\n\n # Flatten the code list.\n code = util.flatten(code)\n\n if arg_count > len(varnames):\n raise CompileError(\"arg_count > len(varnames)\")\n\n if len(kwarg_defaults) > len(varnames):\n raise CompileError(\"len(kwarg_defaults) > len(varnames)\")\n\n # Compile it.\n bc = compile_bytecode(code)\n\n dis.dis(bc)\n\n # Check for a final RETURN_VALUE.\n if PY36:\n # TODO: Add Python 3.6 check\n pass\n else:\n if bc[-1] != tokens.RETURN_VALUE:\n raise CompileError(\n \"No default RETURN_VALUE. Add a `pyte.tokens.RETURN_VALUE` to the end of your \"\n \"bytecode if you don't need one.\")\n\n # Set default flags\n flags = 1 | 2 | 64\n\n frame_data = inspect.stack()[1]\n\n if sys.version_info[0:2] > (3, 3):\n # Validate the stack.\n stack_size = _simulate_stack(dis._get_instructions_bytes(\n bc, constants=consts, names=names, varnames=varnames)\n )\n else:\n warnings.warn(\"Cannot check stack for safety.\")\n stack_size = 99\n\n # Generate optimization warnings.\n _optimize_warn_pass(dis._get_instructions_bytes(bc, constants=consts, names=names, varnames=varnames))\n\n obb = types.CodeType(\n arg_count, # Varnames - used for arguments.\n 0, # Kwargs are not supported yet\n len(varnames), # co_nlocals -> Non-argument local variables\n stack_size, # Auto-calculated\n flags, # 67 is default for a normal function.\n bc, # co_code - use the bytecode we generated.\n consts, # co_consts\n names, # co_names, used for global calls.\n varnames, # arguments\n frame_data[1], # use \n func_name, # co_name\n frame_data[2], # co_firstlineno, ignore this.\n b'', # https://svn.python.org/projects/python/trunk/Objects/lnotab_notes.txt\n (), # freevars - no idea what this does\n () # cellvars - used for nested functions - we don't use these.\n )\n # Update globals\n f_globals = frame_data[0].f_globals\n\n # Create a function type.\n f = types.FunctionType(obb, f_globals)\n f.__name__ = func_name\n f.__defaults__ = kwarg_defaults\n\n if use_safety_wrapper:\n def __safety_wrapper(*args, **kwargs):\n try:\n return f(*args, **kwargs)\n except SystemError as e:\n if 'opcode' not in ' '.join(e.args):\n # Re-raise any non opcode related errors.\n raise\n msg = \"Bytecode exception!\" \\\n \"\\nFunction {} returned an invalid opcode.\" \\\n \"\\nFunction dissection:\\n\\n\".format(f.__name__)\n # dis sucks and always prints to stdout\n # so we capture it\n file = io.StringIO()\n with contextlib.redirect_stdout(file):\n dis.dis(f)\n msg += file.getvalue()\n raise SystemError(msg) from e\n\n returned_func = __safety_wrapper\n returned_func.wrapped = f\n else:\n returned_func = f\n\n # return the func\n return returned_func", "response": "Compiles a list of bytecode instructions into a working function."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngiving the parsed xml to an ElementTree parse the id from the content.", "response": "def _parse_document_id(elm_tree):\n \"\"\"Given the parsed xml to an `ElementTree`,\n parse the id from the content.\n\n \"\"\"\n xpath = '//md:content-id/text()'\n return [x for x in elm_tree.xpath(xpath, namespaces=COLLECTION_NSMAP)][0]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _find_resources(directory, excludes=[]):\n return sorted([r for r in directory.glob('*')\n if True not in [e(r) for e in excludes]])", "response": "Find all the resource paths in the directory."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse the file structure given the path to a module directory.", "response": "def parse_module(path, excludes=None):\n \"\"\"Parse the file structure to a data structure given the path to\n a module directory.\n\n \"\"\"\n file = path / MODULE_FILENAME\n\n if not file.exists():\n raise MissingFile(file)\n id = _parse_document_id(etree.parse(file.open()))\n\n excludes = excludes or []\n excludes.extend([\n lambda filepath: filepath.name == MODULE_FILENAME,\n ])\n\n resources_paths = _find_resources(path, excludes=excludes)\n resources = tuple(_resource_from_path(res) for res in resources_paths)\n\n return Module(id, file, resources)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_collection(path, excludes=None):\n file = path / COLLECTION_FILENAME\n if not file.exists():\n raise MissingFile(file)\n id = _parse_document_id(etree.parse(file.open()))\n\n excludes = excludes or []\n excludes.extend([\n lambda filepath: filepath.name == COLLECTION_FILENAME,\n lambda filepath: filepath.is_dir(),\n ])\n resources_paths = _find_resources(path, excludes=excludes)\n resources = tuple(_resource_from_path(res) for res in resources_paths)\n\n return Collection(id, file, resources)", "response": "Parse a file structure given the path to\n a collection directory."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing a litezip file structure given the path to the litezip directory.", "response": "def parse_litezip(path):\n \"\"\"Parse a litezip file structure to a data structure given the path\n to the litezip directory.\n\n \"\"\"\n struct = [parse_collection(path)]\n struct.extend([parse_module(x) for x in path.iterdir()\n if x.is_dir() and x.name.startswith('m')])\n return tuple(sorted(struct))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef convert_completezip(path):\n for filepath in path.glob('**/index_auto_generated.cnxml'):\n filepath.rename(filepath.parent / 'index.cnxml')\n logger.debug('removed {}'.format(filepath))\n for filepath in path.glob('**/index.cnxml.html'):\n filepath.unlink()\n return parse_litezip(path)", "response": "Converts a completezip file structure to a litezip file structure."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a sequence of Instructions in a bytecode string.", "response": "def _get_instructions_bytes(code, varnames=None, names=None, constants=None,\n cells=None, linestarts=None, line_offset=0):\n \"\"\"Iterate over the instructions in a bytecode string.\n\n Generates a sequence of Instruction namedtuples giving the details of each\n opcode. Additional information about the code's runtime environment\n (e.g. variable names, constants) can be specified using optional\n arguments.\n\n \"\"\"\n labels = dis.findlabels(code)\n extended_arg = 0\n starts_line = None\n free = None\n # enumerate() is not an option, since we sometimes process\n # multiple elements on a single pass through the loop\n n = len(code)\n i = 0\n while i < n:\n op = code[i]\n offset = i\n if linestarts is not None:\n starts_line = linestarts.get(i, None)\n if starts_line is not None:\n starts_line += line_offset\n is_jump_target = i in labels\n i = i + 1\n arg = None\n argval = None\n argrepr = ''\n if op >= dis.HAVE_ARGUMENT:\n arg = code[i] + code[i + 1] * 256 + extended_arg\n extended_arg = 0\n i = i + 2\n if op == dis.EXTENDED_ARG:\n extended_arg = arg * 65536\n # Set argval to the dereferenced value of the argument when\n # availabe, and argrepr to the string representation of argval.\n # _disassemble_bytes needs the string repr of the\n # raw name index for LOAD_GLOBAL, LOAD_CONST, etc.\n argval = arg\n if op in dis.hasconst:\n argval, argrepr = dis._get_const_info(arg, constants)\n elif op in dis.hasname:\n argval, argrepr = dis._get_name_info(arg, names)\n elif op in dis.hasjrel:\n argval = i + arg\n argrepr = \"to \" + repr(argval)\n elif op in dis.haslocal:\n argval, argrepr = dis._get_name_info(arg, varnames)\n elif op in dis.hascompare:\n argval = dis.cmp_op[arg]\n argrepr = argval\n elif op in dis.hasfree:\n argval, argrepr = dis._get_name_info(arg, cells)\n elif op in dis.hasnargs:\n argrepr = \"%d positional, %d keyword pair\" % (code[i - 2], code[i - 1])\n yield dis.Instruction(dis.opname[op], op,\n arg, argval, argrepr,\n offset, starts_line, is_jump_target)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _disassemble(self, lineno_width=3, mark_as_current=False):\n fields = []\n # Column: Source code line number\n if lineno_width:\n if self.starts_line is not None:\n lineno_fmt = \"%%%dd\" % lineno_width\n fields.append(lineno_fmt % self.starts_line)\n else:\n fields.append(' ' * lineno_width)\n # Column: Current instruction indicator\n if mark_as_current:\n fields.append('-->')\n else:\n fields.append(' ')\n # Column: Jump target marker\n if self.is_jump_target:\n fields.append('>>')\n else:\n fields.append(' ')\n # Column: Instruction offset from start of code sequence\n fields.append(repr(self.offset).rjust(4))\n # Column: Opcode name\n fields.append(self.opname.ljust(20))\n # Column: Opcode argument\n if self.arg is not None:\n fields.append(repr(self.arg).rjust(5))\n # Column: Opcode argument details\n if self.argrepr:\n fields.append('(' + self.argrepr + ')')\n return ' '.join(fields).rstrip()", "response": "Format instruction details for inclusion in disassembly output\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the intersection of two lists. Assumes the lists are sorted by start positions.", "response": "def intersection(l1, l2):\n '''Returns intersection of two lists. Assumes the lists are sorted by start positions'''\n if len(l1) == 0 or len(l2) == 0:\n return []\n\n out = []\n l2_pos = 0\n\n for l in l1:\n while l2_pos < len(l2) and l2[l2_pos].end < l.start:\n l2_pos += 1\n\n if l2_pos == len(l2):\n break\n\n while l2_pos < len(l2) and l.intersects(l2[l2_pos]):\n out.append(l.intersection(l2[l2_pos]))\n l2_pos += 1\n\n l2_pos = max(0, l2_pos - 1)\n\n return out"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef merge_overlapping_in_list(l):\n '''Sorts list, merges any overlapping intervals, and also adjacent intervals. e.g.\n [0,1], [1,2] would be merge to [0,.2].'''\n i = 0\n l.sort()\n\n while i < len(l) - 1:\n u = l[i].union(l[i+1])\n if u is not None:\n l[i] = u\n l.pop(i+1)\n else:\n i += 1", "response": "Sorts list merges any overlapping intervals and also adjacent intervals. e. g. A list of all entries in the list are merged to [ 0 1 2 )."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove_contained_in_list(l):\n '''Sorts list in place, then removes any intervals that are completely\n contained inside another interval'''\n i = 0\n l.sort()\n\n while i < len(l) - 1:\n if l[i+1].contains(l[i]):\n l.pop(i)\n elif l[i].contains(l[i+1]):\n l.pop(i+1)\n else:\n i += 1", "response": "Sorts list in place then removes any intervals that are completely\n contained inside another interval"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the distance from the point to the interval. Zero if the point lies inside the interval.", "response": "def distance_to_point(self, p):\n '''Returns the distance from the point to the interval. Zero if the point lies inside the interval.'''\n if self.start <= p <= self.end:\n return 0\n else:\n return min(abs(self.start - p), abs(self.end - p))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef intersects(self, i):\n '''Returns true iff this interval intersects the interval i'''\n return self.start <= i.end and i.start <= self.end", "response": "Returns true iff this interval intersects the interval i"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning true iff this interval contains the interval i", "response": "def contains(self, i):\n '''Returns true iff this interval contains the interval i'''\n return self.start <= i.start and i.end <= self.end"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef union(self, i):\n '''If intervals intersect, returns their union, otherwise returns None'''\n if self.intersects(i) or self.end + 1 == i.start or i.end + 1 == self.start:\n return Interval(min(self.start, i.start), max(self.end, i.end))\n else:\n return None", "response": "Returns the union of two intervals."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef union_fill_gap(self, i):\n '''Like union, but ignores whether the two intervals intersect or not'''\n return Interval(min(self.start, i.start), max(self.end, i.end))", "response": "Like union but ignores whether the two intervals intersect or not"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef intersection(self, i):\n '''If intervals intersect, returns their intersection, otherwise returns None'''\n if self.intersects(i):\n return Interval(max(self.start, i.start), min(self.end, i.end))\n else:\n return None", "response": "Returns the intersection of two sets or None if there is no intersection."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreads a FASTA or FASTQ file and returns a dictionary of all the sequence IDs that are available in memory.", "response": "def file_reader(fname, read_quals=False):\n '''Iterates over a FASTA or FASTQ file, yielding the next sequence in the file until there are no more sequences'''\n f = utils.open_file_read(fname)\n line = f.readline()\n phylip_regex = re.compile('^\\s*[0-9]+\\s+[0-9]+$')\n gbk_regex = re.compile('^LOCUS\\s+\\S')\n\n if line.startswith('>'):\n seq = Fasta()\n previous_lines[f] = line\n elif line.startswith('##gff-version 3'):\n seq = Fasta()\n # if a GFF file, need to skip past all the annotation\n # and get to the fasta sequences at the end of the file\n while not line.startswith('>'):\n line = f.readline()\n if not line:\n utils.close(f)\n raise Error('No sequences found in GFF file \"' + fname + '\"')\n\n seq = Fasta()\n previous_lines[f] = line\n elif line.startswith('ID ') and line[5] != ' ':\n seq = Embl()\n previous_lines[f] = line\n elif gbk_regex.search(line):\n seq = Embl()\n previous_lines[f] = line\n elif line.startswith('@'):\n seq = Fastq()\n previous_lines[f] = line\n elif phylip_regex.search(line):\n # phylip format could be interleaved or not, need to look at next\n # couple of lines to figure that out. Don't expect these files to\n # be too huge, so just store all the sequences in memory\n number_of_seqs, bases_per_seq = line.strip().split()\n number_of_seqs = int(number_of_seqs)\n bases_per_seq = int(bases_per_seq)\n got_blank_line = False\n\n first_line = line\n seq_lines = []\n while 1:\n line = f.readline()\n if line == '':\n break\n elif line == '\\n':\n got_blank_line = True\n else:\n seq_lines.append(line.rstrip())\n utils.close(f)\n\n if len(seq_lines) == 1 or len(seq_lines) == number_of_seqs:\n sequential = True\n elif seq_lines[0][10] != ' ' and seq_lines[1][10] == ' ':\n sequential = True\n else:\n sequential = False\n\n # if the 11th char of second sequence line is a space, then the file is sequential, e.g.:\n # GAGCCCGGGC AATACAGGGT AT\n # as opposed to:\n # Salmo gairAAGCCTTGGC AGTGCAGGGT\n if sequential:\n current_id = None\n current_seq = ''\n for line in seq_lines:\n if len(current_seq) == bases_per_seq or len(current_seq) == 0:\n if current_id is not None:\n yield Fasta(current_id, current_seq.replace('-', ''))\n current_seq = ''\n current_id, new_bases = line[0:10].rstrip(), line.rstrip()[10:]\n else:\n new_bases = line.rstrip()\n\n current_seq += new_bases.replace(' ','')\n\n yield Fasta(current_id, current_seq.replace('-', ''))\n else:\n # seaview files start all seqs at pos >=12. Other files start\n # their sequence at the start of the line\n if seq_lines[number_of_seqs + 1][0] == ' ':\n first_gap_pos = seq_lines[0].find(' ')\n end_of_gap = first_gap_pos\n while seq_lines[0][end_of_gap] == ' ':\n end_of_gap += 1\n first_seq_base = end_of_gap\n else:\n first_seq_base = 10\n\n seqs = []\n for i in range(number_of_seqs):\n name, bases = seq_lines[i][0:first_seq_base].rstrip(), seq_lines[i][first_seq_base:]\n seqs.append(Fasta(name, bases))\n\n for i in range(number_of_seqs, len(seq_lines)):\n seqs[i%number_of_seqs].seq += seq_lines[i]\n\n for fa in seqs:\n fa.seq = fa.seq.replace(' ','').replace('-','')\n yield fa\n\n return\n elif line == '':\n utils.close(f)\n return\n else:\n utils.close(f)\n raise Error('Error determining file type from file \"' + fname + '\". First line is:\\n' + line.rstrip())\n\n try:\n while seq.get_next_from_file(f, read_quals):\n yield seq\n finally:\n utils.close(f)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef subseq(self, start, end):\n '''Returns Fasta object with the same name, of the bases from start to end, but not including end'''\n return Fasta(self.id, self.seq[start:end])", "response": "Returns a Fasta object with the same name of the bases from start to end"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the prefix and suffix of a capillary read e. g. xxxxx. p1k or xxxxx. q1k Returns a tuple ( prefix suffix )", "response": "def split_capillary_id(self):\n '''Gets the prefix and suffix of an name of a capillary read, e.g. xxxxx.p1k or xxxx.q1k. Returns a tuple (prefix, suffx)'''\n try:\n a = self.id.rsplit('.', 1)\n if a[1].startswith('p'):\n dir = 'fwd'\n elif a[1].startswith('q'):\n dir = 'rev'\n else:\n dir = 'unk'\n\n return {'prefix': a[0], 'dir': dir, 'suffix':a[1]}\n except:\n raise Error('Error in split_capillary_id() on ID', self.id)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef expand_nucleotides(self):\n '''Assumes sequence is nucleotides. Returns list of all combinations of redundant nucleotides. e.g. R is A or G, so CRT would have combinations CAT and CGT'''\n s = list(self.seq)\n for i in range(len(s)):\n if s[i] in redundant_nts:\n s[i] = ''.join(redundant_nts[s[i]])\n\n seqs = []\n for x in itertools.product(*s):\n seqs.append(Fasta(self.id + '.' + str(len(seqs) + 1), ''.join(x)))\n return seqs", "response": "Assumes sequence is nucleotides. Returns list of all combinations of redundant nucleotides. e. g. R is A or G so CRT would have combinations CAT and CGT."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef strip_illumina_suffix(self):\n '''Removes any trailing /1 or /2 off the end of the name'''\n if self.id.endswith('/1') or self.id.endswith('/2'):\n self.id = self.id[:-2]", "response": "Removes any trailing illumina suffix off the end of the name"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn true if the sequence is all Ns", "response": "def is_all_Ns(self, start=0, end=None):\n '''Returns true if the sequence is all Ns (upper or lower case)'''\n if end is not None:\n if start > end:\n raise Error('Error in is_all_Ns. Start coord must be <= end coord')\n end += 1\n else:\n end = len(self)\n\n if len(self) == 0:\n return False\n else:\n return re.search('[^Nn]', self.seq[start:end]) is None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_insertions(self, skip=10, window=1, test=False):\n '''Adds a random base within window bases around every skip bases. e.g. skip=10, window=1 means a random base added somwhere in theintervals [9,11], [19,21] ... '''\n assert 2 * window < skip\n new_seq = list(self.seq)\n for i in range(len(self) - skip, 0, -skip):\n pos = random.randrange(i - window, i + window + 1)\n base = random.choice(['A', 'C', 'G', 'T'])\n if test:\n base = 'N'\n new_seq.insert(pos, base)\n\n self.seq = ''.join(new_seq)", "response": "Adds a random base within window bases around every skip bases. e. g. a random base added somwhere in the intervals [ 9 11 19 21..."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef replace_bases(self, old, new):\n '''Replaces all occurrences of 'old' with 'new' '''\n self.seq = self.seq.replace(old, new)", "response": "Replaces all occurrences of old with new"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef replace_interval(self, start, end, new):\n '''Replaces the sequence from start to end with the sequence \"new\"'''\n if start > end or start > len(self) - 1 or end > len(self) - 1:\n raise Error('Error replacing bases ' + str(start) + '-' + str(end) + ' in sequence ' + self.id)\n\n self.seq = self.seq[0:start] + new + self.seq[end + 1:]", "response": "Replaces the sequence from start to end with the sequence new"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfinds the positions of all gaps in the sequence that are at least min_length long. Returns a list of Intervals. Coords are zero - based", "response": "def gaps(self, min_length = 1):\n '''Finds the positions of all gaps in the sequence that are at least min_length long. Returns a list of Intervals. Coords are zero-based'''\n gaps = []\n regex = re.compile('N+', re.IGNORECASE)\n for m in regex.finditer(self.seq):\n if m.span()[1] - m.span()[0] + 1 >= min_length:\n gaps.append(intervals.Interval(m.span()[0], m.span()[1] - 1))\n return gaps"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfind the coordinates of contigs i. e. everything that s not a gap ( N or n. Returns a list of Intervals. Coords are zero - based.", "response": "def contig_coords(self):\n '''Finds coords of contigs, i.e. everything that's not a gap (N or n). Returns a list of Intervals. Coords are zero-based'''\n # contigs are the opposite of gaps, so work out the coords from the gap coords\n gaps = self.gaps()\n\n if len(gaps) == 0:\n return [intervals.Interval(0, len(self) - 1)]\n\n coords = [0]\n for g in gaps:\n if g.start == 0:\n coords = [g.end + 1]\n else:\n coords += [g.start - 1, g.end + 1]\n\n if coords[-1] < len(self):\n coords.append(len(self) - 1)\n\n return [intervals.Interval(coords[i], coords[i+1]) for i in range(0, len(coords)-1,2)]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of ORFs that the sequence has starting on the given frame.", "response": "def orfs(self, frame=0, revcomp=False):\n '''Returns a list of ORFs that the sequence has, starting on the given\n frame. Each returned ORF is an interval.Interval object.\n If revomp=True, then finds the ORFs of the reverse complement\n of the sequence.'''\n assert frame in [0,1,2]\n if revcomp:\n self.revcomp()\n\n aa_seq = self.translate(frame=frame).seq.rstrip('X')\n if revcomp:\n self.revcomp()\n\n orfs = _orfs_from_aa_seq(aa_seq)\n for i in range(len(orfs)):\n if revcomp:\n start = len(self) - (orfs[i].end * 3 + 3) - frame\n end = len(self) - (orfs[i].start * 3) - 1 - frame\n else:\n start = orfs[i].start * 3 + frame\n end = orfs[i].end * 3 + 2 + frame\n\n orfs[i] = intervals.Interval(start, end)\n\n return orfs"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef is_complete_orf(self):\n '''Returns true iff length is >= 6, is a multiple of 3, and there is exactly one stop codon in the sequence and it is at the end'''\n if len(self) %3 != 0 or len(self) < 6:\n return False\n\n orfs = self.orfs()\n complete_orf = intervals.Interval(0, len(self) - 1)\n for orf in orfs:\n if orf == complete_orf:\n return True\n return False", "response": "Returns true iff length is > = 6 and there is exactly one stop codon in the sequence and it is at the end."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef looks_like_gene(self):\n '''Returns true iff: length >=6, length is a multiple of 3, first codon is start, last codon is a stop and has no other stop codons'''\n return self.is_complete_orf() \\\n and len(self) >= 6 \\\n and len(self) %3 == 0 \\\n and self.seq[0:3].upper() in genetic_codes.starts[genetic_code]", "response": "Returns true iff the sequence looks like a gene"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntrying to make into a gene sequence. Returns a tuple ( new sequence strand frame ). Returns None.", "response": "def make_into_gene(self):\n '''Tries to make into a gene sequence. Tries all three reading frames and both strands. Returns a tuple (new sequence, strand, frame) if it was successful. Otherwise returns None.'''\n for reverse in [True, False]:\n for frame in range(3):\n new_seq = copy.copy(self)\n if reverse:\n new_seq.revcomp()\n new_seq.seq = new_seq[frame:]\n if len(new_seq) % 3:\n new_seq.seq = new_seq.seq[:-(len(new_seq) % 3)]\n\n new_aa_seq = new_seq.translate()\n if len(new_aa_seq) >= 2 and new_seq[0:3] in genetic_codes.starts[genetic_code] and new_aa_seq[-1] == '*' and '*' not in new_aa_seq[:-1]:\n strand = '-' if reverse else '+'\n return new_seq, strand, frame\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove first start/'end bases off the start of the sequence", "response": "def trim(self, start, end):\n '''Removes first 'start'/'end' bases off the start/end of the sequence'''\n self.seq = self.seq[start:len(self.seq) - end]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a Fastq object.", "response": "def to_Fastq(self, qual_scores):\n '''Returns a Fastq object. qual_scores expected to be a list of numbers, like you would get in a .qual file'''\n if len(self) != len(qual_scores):\n raise Error('Error making Fastq from Fasta, lengths differ.', self.id)\n return Fastq(self.id, self.seq, ''.join([chr(max(0, min(x, 93)) + 33) for x in qual_scores]))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef translate(self, frame=0):\n '''Returns a Fasta sequence, translated into amino acids. Starts translating from 'frame', where frame expected to be 0,1 or 2'''\n return Fasta(self.id, ''.join([genetic_codes.codes[genetic_code].get(self.seq[x:x+3].upper(), 'X') for x in range(frame, len(self)-1-frame, 3)]))", "response": "Returns a Fasta sequence translated into amino acids. Starts translating from frame where frame expected to be 0 1 or 2"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the GC content for the sequence.", "response": "def gc_content(self, as_decimal=True):\n \"\"\"Returns the GC content for the sequence.\n Notes:\n This method ignores N when calculating the length of the sequence.\n It does not, however ignore other ambiguous bases. It also only\n includes the ambiguous base S (G or C). In this sense the method is\n conservative with its calculation.\n\n Args:\n as_decimal (bool): Return the result as a decimal. Setting to False\n will return as a percentage. i.e for the sequence GCAT it will\n return 0.5 by default and 50.00 if set to False.\n\n Returns:\n float: GC content calculated as the number of G, C, and S divided\n by the number of (non-N) bases (length).\n\n \"\"\"\n gc_total = 0.0\n num_bases = 0.0\n n_tuple = tuple('nN')\n accepted_bases = tuple('cCgGsS')\n\n # counter sums all unique characters in sequence. Case insensitive.\n for base, count in Counter(self.seq).items():\n\n # dont count N in the number of bases\n if base not in n_tuple:\n num_bases += count\n\n if base in accepted_bases: # S is a G or C\n gc_total += count\n\n gc_content = gc_total / num_bases\n\n if not as_decimal: # return as percentage\n gc_content *= 100\n\n return gc_content"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef subseq(self, start, end):\n '''Returns Fastq object with the same name, of the bases from start to end, but not including end'''\n return Fastq(self.id, self.seq[start:end], self.qual[start:end])", "response": "Returns a new Fastq object with the same name of the bases from start to end"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef trim(self, start, end):\n '''Removes first 'start'/'end' bases off the start/end of the sequence'''\n super().trim(start, end)\n self.qual = self.qual[start:len(self.qual) - end]", "response": "Removes first start/'end bases off the start of the sequence"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef trim_Ns(self):\n '''Removes any leading or trailing N or n characters from the sequence'''\n # get index of first base that is not an N\n i = 0\n while i < len(self) and self.seq[i] in 'nN':\n i += 1\n\n # strip off start of sequence and quality\n self.seq = self.seq[i:]\n self.qual = self.qual[i:]\n\n # strip the ends\n self.seq = self.seq.rstrip('Nn')\n self.qual = self.qual[:len(self.seq)]", "response": "Removes any leading or trailing N or n characters from the sequence"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreplacing the sequence from start to end with the sequence new", "response": "def replace_interval(self, start, end, new, qual_string):\n '''Replaces the sequence from start to end with the sequence \"new\"'''\n if len(new) != len(qual_string):\n raise Error('Length of new seq and qual string in replace_interval() must be equal. Cannot continue')\n super().replace_interval(start, end, new)\n self.qual = self.qual[0:start] + qual_string + self.qual[end + 1:]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a Fasta sequence translated into amino acids. Starts translating from frame where frame expected to be 0 1 2", "response": "def translate(self):\n '''Returns a Fasta sequence, translated into amino acids. Starts translating from 'frame', where frame expected to be 0,1 or 2'''\n fa = super().translate()\n return Fastq(fa.id, fa.seq, 'I'*len(fa.seq))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef acgtn_only(infile, outfile):\n '''Replace every non-acgtn (case insensitve) character with an N'''\n f = utils.open_file_write(outfile)\n for seq in sequences.file_reader(infile):\n seq.replace_non_acgt()\n print(seq, file=f)\n utils.close(f)", "response": "Replace every non - acgtn character with an N"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts a CAF file to fastq.", "response": "def caf_to_fastq(infile, outfile, min_length=0, trim=False):\n '''Convert a CAF file to fastq. Reads shorter than min_length are not output. If clipping information is in the CAF file (with a line Clipping QUAL ...) and trim=True, then trim the reads'''\n caf_reader = caf.file_reader(infile)\n fout = utils.open_file_write(outfile)\n\n for c in caf_reader:\n if trim:\n if c.clip_start is not None and c.clip_end is not None:\n c.seq.seq = c.seq.seq[c.clip_start:c.clip_end + 1]\n c.seq.qual = c.seq.qual[c.clip_start:c.clip_end + 1]\n else:\n print('Warning: no clipping info for sequence', c.id, file=sys.stderr)\n\n\n if len(c.seq) >= min_length:\n print(c.seq, file=fout)\n\n utils.close(fout)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef count_sequences(infile):\n '''Returns the number of sequences in a file'''\n seq_reader = sequences.file_reader(infile)\n n = 0\n for seq in seq_reader:\n n += 1\n return n", "response": "Returns the number of sequences in a file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef interleave(infile_1, infile_2, outfile, suffix1=None, suffix2=None):\n '''Makes interleaved file from two sequence files. If used, will append suffix1 onto end\n of every sequence name in infile_1, unless it already ends with suffix1. Similar for sufffix2.'''\n seq_reader_1 = sequences.file_reader(infile_1)\n seq_reader_2 = sequences.file_reader(infile_2)\n f_out = utils.open_file_write(outfile)\n\n for seq_1 in seq_reader_1:\n try:\n seq_2 = next(seq_reader_2)\n except:\n utils.close(f_out)\n raise Error('Error getting mate for sequence', seq_1.id, ' ... cannot continue')\n\n if suffix1 is not None and not seq_1.id.endswith(suffix1):\n seq_1.id += suffix1\n if suffix2 is not None and not seq_2.id.endswith(suffix2):\n seq_2.id += suffix2\n\n print(seq_1, file=f_out)\n print(seq_2, file=f_out)\n\n try:\n seq_2 = next(seq_reader_2)\n except:\n seq_2 = None\n\n if seq_2 is not None:\n utils.close(f_out)\n raise Error('Error getting mate for sequence', seq_2.id, ' ... cannot continue')\n\n utils.close(f_out)", "response": "Makes an interleaved file from two sequence files. If used will append suffix1 onto end\n of every sequence name in infile_1 unless it already ends with suffix2. Similar for sufffix2."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmakes a multi fasta file of random sequences all the same length", "response": "def make_random_contigs(contigs, length, outfile, name_by_letters=False, prefix='', seed=None, first_number=1):\n '''Makes a multi fasta file of random sequences, all the same length'''\n random.seed(a=seed)\n fout = utils.open_file_write(outfile)\n letters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n letters_index = 0\n\n for i in range(contigs):\n if name_by_letters:\n name = letters[letters_index]\n letters_index += 1\n if letters_index == len(letters):\n letters_index = 0\n else:\n name = str(i + first_number)\n\n fa = sequences.Fasta(prefix + name, ''.join([random.choice('ACGT') for x in range(length)]))\n print(fa, file=fout)\n\n utils.close(fout)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the mean length of the sequences in the input file. By default uses all sequences. By default uses all sequences.", "response": "def mean_length(infile, limit=None):\n '''Returns the mean length of the sequences in the input file. By default uses all sequences. To limit to the first N sequences, use limit=N'''\n total = 0\n count = 0\n seq_reader = sequences.file_reader(infile)\n for seq in seq_reader:\n total += len(seq)\n count += 1\n if limit is not None and count >= limit:\n break\n\n assert count > 0\n return total / count"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntake a multi fasta file and writes a new file that contains just one sequence with the original sequences catted together preserving their order", "response": "def merge_to_one_seq(infile, outfile, seqname='union'):\n '''Takes a multi fasta or fastq file and writes a new file that contains just one sequence, with the original sequences catted together, preserving their order'''\n seq_reader = sequences.file_reader(infile)\n seqs = []\n\n for seq in seq_reader:\n seqs.append(copy.copy(seq))\n\n new_seq = ''.join([seq.seq for seq in seqs])\n\n if type(seqs[0]) == sequences.Fastq:\n new_qual = ''.join([seq.qual for seq in seqs])\n seqs[:] = []\n merged = sequences.Fastq(seqname, new_seq, new_qual)\n else:\n merged = sequences.Fasta(seqname, new_seq)\n seqs[:] = []\n\n f = utils.open_file_write(outfile)\n print(merged, file=f)\n utils.close(f)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmaking a file of contigs from scaffolds by splitting at every N. contig.", "response": "def scaffolds_to_contigs(infile, outfile, number_contigs=False):\n '''Makes a file of contigs from scaffolds by splitting at every N.\n Use number_contigs=True to add .1, .2, etc onto end of each\n contig, instead of default to append coordinates.'''\n seq_reader = sequences.file_reader(infile)\n fout = utils.open_file_write(outfile)\n\n for seq in seq_reader:\n contigs = seq.contig_coords()\n counter = 1\n for contig in contigs:\n if number_contigs:\n name = seq.id + '.' + str(counter)\n counter += 1\n else:\n name = '.'.join([seq.id, str(contig.start + 1), str(contig.end + 1)])\n print(sequences.Fasta(name, seq[contig.start:contig.end+1]), file=fout)\n\n utils.close(fout)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef sort_by_size(infile, outfile, smallest_first=False):\n '''Sorts input sequence file by biggest sequence first, writes sorted output file. Set smallest_first=True to have smallest first'''\n seqs = {}\n file_to_dict(infile, seqs)\n seqs = list(seqs.values())\n seqs.sort(key=lambda x: len(x), reverse=not smallest_first)\n fout = utils.open_file_write(outfile)\n for seq in seqs:\n print(seq, file=fout)\n utils.close(fout)", "response": "Sorts input sequence file by biggest sequence first writes sorted output file."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsort input sequence file by sort - d - k1 1 writes sorted output file.", "response": "def sort_by_name(infile, outfile):\n '''Sorts input sequence file by sort -d -k1,1, writes sorted output file.'''\n seqs = {}\n file_to_dict(infile, seqs)\n #seqs = list(seqs.values())\n #seqs.sort()\n fout = utils.open_file_write(outfile)\n for name in sorted(seqs):\n print(seqs[name], file=fout)\n utils.close(fout)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef to_fastg(infile, outfile, circular=None):\n '''Writes a FASTG file in SPAdes format from input file. Currently only whether or not a sequence is circular is supported. Put circular=set of ids, or circular=filename to make those sequences circular in the output. Puts coverage=1 on all contigs'''\n if circular is None:\n to_circularise = set()\n elif type(circular) is not set:\n f = utils.open_file_read(circular)\n to_circularise = set([x.rstrip() for x in f.readlines()])\n utils.close(f)\n else:\n to_circularise = circular\n\n seq_reader = sequences.file_reader(infile)\n fout = utils.open_file_write(outfile)\n nodes = 1\n\n for seq in seq_reader:\n new_id = '_'.join([\n 'NODE', str(nodes),\n 'length', str(len(seq)),\n 'cov', '1',\n 'ID', seq.id\n ])\n\n if seq.id in to_circularise:\n seq.id = new_id + ':' + new_id + ';'\n print(seq, file=fout)\n seq.revcomp()\n seq.id = new_id + \"':\" + new_id + \"';\"\n print(seq, file=fout)\n else:\n seq.id = new_id + ';'\n print(seq, file=fout)\n seq.revcomp()\n seq.id = new_id + \"';\"\n print(seq, file=fout)\n\n nodes += 1\n\n utils.close(fout)", "response": "Writes a FASTG file in SPAdes format from input file. Currently only supports circular sequences."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a dictionary of positions of the start of each sequence in the file as sgi is a dictionary of name = > length of each sequence in the file.", "response": "def length_offsets_from_fai(fai_file):\n '''Returns a dictionary of positions of the start of each sequence, as\n if all the sequences were catted into one sequence.\n eg if file has three sequences, seq1 10bp, seq2 30bp, seq3 20bp, then\n the output would be: {'seq1': 0, 'seq2': 10, 'seq3': 40}'''\n positions = {}\n total_length = 0\n f = utils.open_file_read(fai_file)\n\n for line in f:\n try:\n (name, length) = line.rstrip().split()[:2]\n length = int(length)\n except:\n raise Error('Error reading the following line of fai file ' + fai_file + '\\n' + line)\n\n positions[name] = total_length\n total_length += length\n\n utils.close(f)\n return positions"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef split_by_base_count(infile, outfiles_prefix, max_bases, max_seqs=None):\n '''Splits a fasta/q file into separate files, file size determined by number of bases.\n\n Puts <= max_bases in each split file The exception is a single sequence >=max_bases\n is put in its own file. This does not split sequences.\n '''\n seq_reader = sequences.file_reader(infile)\n base_count = 0\n file_count = 1\n seq_count = 0\n fout = None\n if max_seqs is None:\n max_seqs = float('inf')\n\n for seq in seq_reader:\n if base_count == 0:\n fout = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n file_count += 1\n\n if base_count + len(seq) > max_bases or seq_count >= max_seqs:\n if base_count == 0:\n print(seq, file=fout)\n utils.close(fout)\n else:\n utils.close(fout)\n fout = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n print(seq, file=fout)\n base_count = len(seq)\n file_count += 1\n seq_count = 1\n else:\n base_count += len(seq)\n seq_count += 1\n print(seq, file=fout)\n\n utils.close(fout)", "response": "Splits a fasta file into separate files file size determined by number of bases."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef split_by_fixed_size(infile, outfiles_prefix, chunk_size, tolerance, skip_if_all_Ns=False):\n '''Splits fasta/q file into separate files, with up to (chunk_size + tolerance) bases in each file'''\n file_count = 1\n coords = []\n small_sequences = [] # sequences shorter than chunk_size\n seq_reader = sequences.file_reader(infile)\n f_coords = utils.open_file_write(outfiles_prefix + '.coords')\n\n for seq in seq_reader:\n if skip_if_all_Ns and seq.is_all_Ns():\n continue\n if len(seq) < chunk_size:\n small_sequences.append(copy.copy(seq))\n elif len(seq) <= chunk_size + tolerance:\n f = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n print(seq, file=f)\n utils.close(f)\n file_count += 1\n else:\n # make list of chunk coords\n chunks = [(x,x+chunk_size) for x in range(0, len(seq), chunk_size)]\n if chunks[-1][1] - 1 > len(seq):\n chunks[-1] = (chunks[-1][0], len(seq))\n if len(chunks) > 1 and (chunks[-1][1] - chunks[-1][0]) <= tolerance:\n chunks[-2] = (chunks[-2][0], chunks[-1][1])\n chunks.pop()\n\n # write one output file per chunk\n offset = 0\n for chunk in chunks:\n if not(skip_if_all_Ns and seq.is_all_Ns(start=chunk[0], end=chunk[1]-1)):\n f = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n chunk_id = seq.id + ':' + str(chunk[0]+1) + '-' + str(chunk[1])\n print(sequences.Fasta(chunk_id, seq[chunk[0]:chunk[1]]), file=f)\n print(chunk_id, seq.id, offset, sep='\\t', file=f_coords)\n utils.close(f)\n file_count += 1\n\n offset += chunk[1] - chunk[0]\n\n # write files of small sequences\n if len(small_sequences):\n f = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n file_count += 1\n base_count = 0\n for seq in small_sequences:\n if base_count > 0 and base_count + len(seq) > chunk_size + tolerance:\n utils.close(f)\n f = utils.open_file_write(outfiles_prefix + '.' + str(file_count))\n file_count += 1\n base_count = 0\n\n print(seq, file=f)\n base_count += len(seq)\n\n utils.close(f)", "response": "Splits a fasta file into separate files with up to chunk_size bases in each file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef split_by_fixed_size_onefile(infile, outfile, chunk_size, tolerance, skip_if_all_Ns=False):\n '''Splits each sequence in infile into chunks of fixed size, last chunk can be up to\n (chunk_size + tolerance) in length'''\n seq_reader = sequences.file_reader(infile)\n f_out = utils.open_file_write(outfile)\n for seq in seq_reader:\n for i in range(0, len(seq), chunk_size):\n if i + chunk_size + tolerance >= len(seq):\n end = len(seq)\n else:\n end = i + chunk_size\n\n subseq = seq.subseq(i, end)\n if not (skip_if_all_Ns and subseq.is_all_Ns()):\n subseq.id += '.' + str(i+1) + '_' + str(end)\n print(subseq, file=f_out)\n\n if end == len(seq):\n break\n\n utils.close(f_out)", "response": "Splits each sequence in infile into chunks of fixed size and writes the result to outfile."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning dictionary of length stats from an fai file. Keys are longest shortest mean total_length N50 number", "response": "def stats_from_fai(infile):\n '''Returns dictionary of length stats from an fai file. Keys are: longest, shortest, mean, total_length, N50, number'''\n f = utils.open_file_read(infile)\n try:\n lengths = sorted([int(line.split('\\t')[1]) for line in f], reverse=True)\n except:\n raise Error('Error getting lengths from fai file ' + infile)\n utils.close(f)\n\n stats = {}\n if len(lengths) > 0:\n stats['longest'] = max(lengths)\n stats['shortest'] = min(lengths)\n stats['total_length'] = sum(lengths)\n stats['mean'] = stats['total_length'] / len(lengths)\n stats['number'] = len(lengths)\n\n cumulative_length = 0\n for length in lengths:\n cumulative_length += length\n if cumulative_length >= 0.5 * stats['total_length']:\n stats['N50'] = length\n break\n else:\n stats = {x: 0 for x in ('longest', 'shortest', 'mean', 'N50', 'total_length', 'number')}\n\n return stats"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting input sequence file into a Boulder - IO format as used by primer3", "response": "def to_boulderio(infile, outfile):\n '''Converts input sequence file into a \"Boulder-IO format\", as used by primer3'''\n seq_reader = sequences.file_reader(infile)\n f_out = utils.open_file_write(outfile)\n\n for sequence in seq_reader:\n print(\"SEQUENCE_ID=\" + sequence.id, file=f_out)\n print(\"SEQUENCE_TEMPLATE=\" + sequence.seq, file=f_out)\n print(\"=\", file=f_out)\n\n utils.close(f_out)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef salted_hmac(key_salt, value, secret=None):\n if secret is None:\n secret = settings.SECRET_KEY\n\n key_salt = force_bytes(key_salt)\n secret = force_bytes(secret)\n\n # We need to generate a derived key from our base key. We can do this by\n # passing the key_salt and our base key through a pseudo-random function and\n # SHA1 works nicely.\n digest = hashes.Hash(\n settings.CRYPTOGRAPHY_DIGEST, backend=settings.CRYPTOGRAPHY_BACKEND)\n digest.update(key_salt + secret)\n key = digest.finalize()\n\n # If len(key_salt + secret) > sha_constructor().block_size, the above\n # line is redundant and could be replaced by key = key_salt + secret, since\n # the hmac module does the same thing for keys longer than the block size.\n # However, we need to ensure that we *always* do this.\n h = HMAC(\n key,\n settings.CRYPTOGRAPHY_DIGEST,\n backend=settings.CRYPTOGRAPHY_BACKEND)\n h.update(force_bytes(value))\n return h", "response": "Returns the HMAC - HASH of value using a different key generated from key_salt and secret."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nderive a key from a password and salt.", "response": "def pbkdf2(password, salt, iterations, dklen=0, digest=None):\n \"\"\"\n Implements PBKDF2 with the same API as Django's existing\n implementation, using cryptography.\n\n :type password: any\n :type salt: any\n :type iterations: int\n :type dklen: int\n :type digest: cryptography.hazmat.primitives.hashes.HashAlgorithm\n \"\"\"\n if digest is None:\n digest = settings.CRYPTOGRAPHY_DIGEST\n if not dklen:\n dklen = digest.digest_size\n password = force_bytes(password)\n salt = force_bytes(salt)\n kdf = PBKDF2HMAC(\n algorithm=digest,\n length=dklen,\n salt=salt,\n iterations=iterations,\n backend=settings.CRYPTOGRAPHY_BACKEND)\n return kdf.derive(password)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef encrypt(self, data):\n data = force_bytes(data)\n iv = os.urandom(16)\n return self._encrypt_from_parts(data, iv)", "response": "encrypt data with the key"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nencrypts the data and return the ciphertext.", "response": "def _encrypt_from_parts(self, data, iv):\n \"\"\"\n :type data: bytes\n :type iv: bytes\n :rtype: any\n \"\"\"\n padder = padding.PKCS7(algorithms.AES.block_size).padder()\n padded_data = padder.update(data) + padder.finalize()\n encryptor = Cipher(\n algorithms.AES(self._encryption_key), modes.CBC(iv),\n self._backend).encryptor()\n ciphertext = encryptor.update(padded_data) + encryptor.finalize()\n\n return self._signer.sign(iv + ciphertext)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef decrypt(self, data, ttl=None):\n data = self._signer.unsign(data, ttl)\n\n iv = data[:16]\n ciphertext = data[16:]\n decryptor = Cipher(\n algorithms.AES(self._encryption_key), modes.CBC(iv),\n self._backend).decryptor()\n plaintext_padded = decryptor.update(ciphertext)\n try:\n plaintext_padded += decryptor.finalize()\n except ValueError:\n raise InvalidToken\n\n # Remove padding\n unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()\n unpadded = unpadder.update(plaintext_padded)\n try:\n unpadded += unpadder.finalize()\n except ValueError:\n raise InvalidToken\n return unpadded", "response": ":type data: bytes\n :type ttl: int\n :rtype: bytes"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_encrypted_field(base_class):\n assert not isinstance(base_class, models.Field)\n field_name = 'Encrypted' + base_class.__name__\n if base_class not in FIELD_CACHE:\n FIELD_CACHE[base_class] = type(field_name,\n (EncryptedMixin, base_class), {\n 'base_class': base_class,\n })\n return FIELD_CACHE[base_class]", "response": "A get or create method for encrypted fields."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef encrypt(base_field, key=None, ttl=None):\n if not isinstance(base_field, models.Field):\n assert key is None\n assert ttl is None\n return get_encrypted_field(base_field)\n\n name, path, args, kwargs = base_field.deconstruct()\n kwargs.update({'key': key, 'ttl': ttl})\n return get_encrypted_field(base_field.__class__)(*args, **kwargs)", "response": "A decorator for creating encrypted model fields."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npickling data is serialized as base64", "response": "def value_to_string(self, obj):\n \"\"\"Pickled data is serialized as base64\"\"\"\n value = self.value_from_object(obj)\n return b64encode(self._dump(value)).decode('ascii')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nserializes the object to a base64 - encoded JSON string.", "response": "def dumps(obj,\n key=None,\n salt='django.core.signing',\n serializer=JSONSerializer,\n compress=False):\n \"\"\"\n Returns URL-safe, sha1 signed base64 compressed JSON string. If key is\n None, settings.SECRET_KEY is used instead.\n\n If compress is True (not the default) checks if compressing using zlib can\n save some space. Prepends a '.' to signify compression. This is included\n in the signature, to protect against zip bombs.\n\n Salt can be used to namespace the hash, so that a signed string is\n only valid for a given namespace. Leaving this at the default\n value or re-using a salt value across different parts of your\n application without good cause is a security risk.\n\n The serializer is expected to return a bytestring.\n \"\"\"\n data = serializer().dumps(obj)\n\n # Flag for if it's been compressed or not\n is_compressed = False\n\n if compress:\n # Avoid zlib dependency unless compress is being used\n compressed = zlib.compress(data)\n if len(compressed) < (len(data) - 1):\n data = compressed\n is_compressed = True\n base64d = b64_encode(data)\n if is_compressed:\n base64d = b'.' + base64d\n return TimestampSigner(key, salt=salt).sign(base64d)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef signature(self, value):\n h = HMAC(self.key, self.digest, backend=settings.CRYPTOGRAPHY_BACKEND)\n h.update(force_bytes(value))\n return h", "response": "Returns the signature of the value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving original value and check it wasn't signed more than max_age seconds ago. :type signed_value: bytes :type ttl: int | datetime.timedelta", "response": "def unsign(self, signed_value, ttl=None):\n \"\"\"\n Retrieve original value and check it wasn't signed more\n than max_age seconds ago.\n\n :type signed_value: bytes\n :type ttl: int | datetime.timedelta\n \"\"\"\n h_size, d_size = struct.calcsize('>cQ'), self.digest.digest_size\n fmt = '>cQ%ds%ds' % (len(signed_value) - h_size - d_size, d_size)\n try:\n version, timestamp, value, sig = struct.unpack(fmt, signed_value)\n except struct.error:\n raise BadSignature('Signature is not valid')\n if version != self.version:\n raise BadSignature('Signature version not supported')\n if ttl is not None:\n if isinstance(ttl, datetime.timedelta):\n ttl = ttl.total_seconds()\n # Check timestamp is not older than ttl\n age = abs(time.time() - timestamp)\n if age > ttl + _MAX_CLOCK_SKEW:\n raise SignatureExpired('Signature age %s > %s seconds' % (age,\n ttl))\n try:\n self.signature(signed_value[:-d_size]).verify(sig)\n except InvalidSignature:\n raise BadSignature(\n 'Signature \"%s\" does not match' % binascii.b2a_base64(sig))\n return value"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a PEP 386 - compliant version number from VERSION.", "response": "def get_version(version=None):\n \"\"\"\n Returns a PEP 386-compliant version number from VERSION.\n \"\"\"\n version = get_complete_version(version)\n\n # Now build the two parts of the version number:\n # main = X.Y[.Z]\n # sub = .devN - for pre-alpha releases\n # | {a|b|c}N - for alpha, beta and rc releases\n\n main = get_main_version(version)\n\n sub = ''\n if version[3] == 'alpha' and version[4] == 0:\n git_changeset = get_git_changeset()\n if git_changeset:\n sub = '.dev%s' % git_changeset\n\n elif version[3] != 'final':\n mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}\n sub = mapping[version[3]] + str(version[4])\n\n return str(main + sub)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_complete_version(version=None):\n if version is None:\n from django_cryptography import VERSION as version\n else:\n assert len(version) == 5\n assert version[3] in ('alpha', 'beta', 'rc', 'final')\n\n return version", "response": "Returns a tuple of django_cryptography version."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef enumeration(*args):\n\n assert len(args) > 0, 'at least one argument is required'\n if len(args) == 1:\n # assume the first argument defines the membership\n members = args[0]\n else:\n # assume the arguments are the members\n members = args\n def checker(value):\n if value not in members:\n raise ValueError(value)\n return checker", "response": "Return a value checker which raises a value error if the value is not in the enumeration of values."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef match_pattern(regex):\n\n prog = re.compile(regex)\n def checker(v):\n result = prog.match(v)\n if result is None:\n raise ValueError(v)\n return checker", "response": "Returns a value checker which checks that the value does\n match the supplied regular expression."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef search_pattern(regex):\n\n prog = re.compile(regex)\n def checker(v):\n result = prog.search(v)\n if result is None:\n raise ValueError(v)\n return checker", "response": "Returns a value checker which checks the supplied regular expression against the internal regex."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef number_range_inclusive(min, max, type=float):\n\n def checker(v):\n if type(v) < min or type(v) > max:\n raise ValueError(v)\n return checker", "response": "Returns a function which checks that the supplied value is inclusive."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a function which raises a ValueError if the supplied value is not a number range.", "response": "def number_range_exclusive(min, max, type=float):\n \"\"\"\n Return a value check function which raises a ValueError if the supplied\n value when cast as `type` is less than or equal to `min` or greater than\n or equal to `max`.\n\n \"\"\"\n\n def checker(v):\n if type(v) <= min or type(v) >= max:\n raise ValueError(v)\n return checker"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a value checker that checks if the supplied value is inclusive.", "response": "def datetime_range_inclusive(min, max, format):\n \"\"\"\n Return a value check function which raises a ValueError if the supplied\n value when converted to a datetime using the supplied `format` string is\n less than `min` or greater than `max`.\n\n \"\"\"\n\n dmin = datetime.strptime(min, format)\n dmax = datetime.strptime(max, format)\n def checker(v):\n dv = datetime.strptime(v, format)\n if dv < dmin or dv > dmax:\n raise ValueError(v)\n return checker"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite a list of problems as restructured text to a file.", "response": "def write_problems(problems, file, summarize=False, limit=0):\n \"\"\"\n Write problems as restructured text to a file (or stdout/stderr).\n\n \"\"\"\n w = file.write # convenience variable\n w(\"\"\"\n=================\nValidation Report\n=================\n\"\"\")\n counts = dict() # store problem counts per problem code\n total = 0\n for i, p in enumerate(problems):\n if limit and i >= limit:\n break # bail out\n if total == 0 and not summarize:\n w(\"\"\"\nProblems\n========\n\"\"\")\n total += 1\n code = p['code']\n if code in counts:\n counts[code] += 1\n else:\n counts[code] = 1\n if not summarize:\n ptitle = '\\n%s - %s\\n' % (p['code'], p['message'])\n w(ptitle)\n underline = ''\n for i in range(len(ptitle.strip())):\n underline += '-'\n underline += '\\n'\n w(underline)\n for k in sorted(p.viewkeys() - set(['code', 'message', 'context'])):\n w(':%s: %s\\n' % (k, p[k]))\n if 'context' in p:\n c = p['context']\n for k in sorted(c.viewkeys()):\n w(':%s: %s\\n' % (k, c[k]))\n\n w(\"\"\"\nSummary\n=======\n\nFound %s%s problem%s in total.\n\n\"\"\" % ('at least ' if limit else '', total, 's' if total != 1 else ''))\n for code in sorted(counts.viewkeys()):\n w(':%s: %s\\n' % (code, counts[code]))\n return total"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a header check to the list of header checks that can be performed for the current locale.", "response": "def add_header_check(self,\n code=HEADER_CHECK_FAILED,\n message=MESSAGES[HEADER_CHECK_FAILED]):\n \"\"\"\n Add a header check, i.e., check whether the header record is consistent\n with the expected field names.\n\n Arguments\n ---------\n\n `code` - problem code to report if the header record is not valid,\n defaults to `HEADER_CHECK_FAILED`\n\n `message` - problem message to report if a value is not valid\n\n \"\"\"\n\n t = code, message\n self._header_checks.append(t)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_record_length_check(self,\n code=RECORD_LENGTH_CHECK_FAILED,\n message=MESSAGES[RECORD_LENGTH_CHECK_FAILED],\n modulus=1):\n \"\"\"\n Add a record length check, i.e., check whether the length of a record is\n consistent with the number of expected fields.\n\n Arguments\n ---------\n\n `code` - problem code to report if a record is not valid, defaults to\n `RECORD_LENGTH_CHECK_FAILED`\n\n `message` - problem message to report if a record is not valid\n\n `modulus` - apply the check to every nth record, defaults to 1 (check\n every record)\n\n \"\"\"\n\n t = code, message, modulus\n self._record_length_checks.append(t)", "response": "Add a record length check to the list of record lengths checks that are consistent with the number of expected fields."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_value_check(self, field_name, value_check,\n code=VALUE_CHECK_FAILED,\n message=MESSAGES[VALUE_CHECK_FAILED],\n modulus=1):\n \"\"\"\n Add a value check function for the specified field.\n\n Arguments\n ---------\n\n `field_name` - the name of the field to attach the value check function\n to\n\n `value_check` - a function that accepts a single argument (a value) and\n raises a `ValueError` if the value is not valid\n\n `code` - problem code to report if a value is not valid, defaults to\n `VALUE_CHECK_FAILED`\n\n `message` - problem message to report if a value is not valid\n\n `modulus` - apply the check to every nth record, defaults to 1 (check\n every record)\n\n \"\"\"\n\n # guard conditions\n assert field_name in self._field_names, 'unexpected field name: %s' % field_name\n assert callable(value_check), 'value check must be a callable function'\n\n t = field_name, value_check, code, message, modulus\n self._value_checks.append(t)", "response": "Adds a value check function to the entry in the internal value check list for the specified field."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_value_predicate(self, field_name, value_predicate,\n code=VALUE_PREDICATE_FALSE,\n message=MESSAGES[VALUE_PREDICATE_FALSE],\n modulus=1):\n \"\"\"\n Add a value predicate function for the specified field.\n\n N.B., everything you can do with value predicates can also be done with\n value check functions, whether you use one or the other is a matter of\n style.\n\n Arguments\n ---------\n\n `field_name` - the name of the field to attach the value predicate\n function to\n\n `value_predicate` - a function that accepts a single argument (a value)\n and returns False if the value is not valid\n\n `code` - problem code to report if a value is not valid, defaults to\n `VALUE_PREDICATE_FALSE`\n\n `message` - problem message to report if a value is not valid\n\n `modulus` - apply the check to every nth record, defaults to 1 (check\n every record)\n\n \"\"\"\n\n assert field_name in self._field_names, 'unexpected field name: %s' % field_name\n assert callable(value_predicate), 'value predicate must be a callable function'\n\n t = field_name, value_predicate, code, message, modulus\n self._value_predicates.append(t)", "response": "Adds a value predicate function to the entry set."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a record check function to the internal list of record checks.", "response": "def add_record_check(self, record_check, modulus=1):\n \"\"\"\n Add a record check function.\n\n Arguments\n ---------\n\n `record_check` - a function that accepts a single argument (a record as\n a dictionary of values indexed by field name) and raises a\n `RecordError` if the record is not valid\n\n `modulus` - apply the check to every nth record, defaults to 1 (check\n every record)\n\n \"\"\"\n\n assert callable(record_check), 'record check must be a callable function'\n\n t = record_check, modulus\n self._record_checks.append(t)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds a record predicate function to the list of record checks that occur for this entry.", "response": "def add_record_predicate(self, record_predicate,\n code=RECORD_PREDICATE_FALSE,\n message=MESSAGES[RECORD_PREDICATE_FALSE],\n modulus=1):\n \"\"\"\n Add a record predicate function.\n\n N.B., everything you can do with record predicates can also be done with\n record check functions, whether you use one or the other is a matter of\n style.\n\n Arguments\n ---------\n\n `record_predicate` - a function that accepts a single argument (a record\n as a dictionary of values indexed by field name) and returns False if\n the value is not valid\n\n `code` - problem code to report if a record is not valid, defaults to\n `RECORD_PREDICATE_FALSE`\n\n `message` - problem message to report if a record is not valid\n\n `modulus` - apply the check to every nth record, defaults to 1 (check\n every record)\n\n \"\"\"\n\n assert callable(record_predicate), 'record predicate must be a callable function'\n\n t = record_predicate, code, message, modulus\n self._record_predicates.append(t)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding a unique check on a single column or combination of columns.", "response": "def add_unique_check(self, key,\n code=UNIQUE_CHECK_FAILED,\n message=MESSAGES[UNIQUE_CHECK_FAILED]):\n \"\"\"\n Add a unique check on a single column or combination of columns.\n\n Arguments\n ---------\n\n `key` - a single field name (string) specifying a field in which all\n values are expected to be unique, or a sequence of field names (tuple\n or list of strings) specifying a compound key\n\n `code` - problem code to report if a record is not valid, defaults to\n `UNIQUE_CHECK_FAILED`\n\n `message` - problem message to report if a record is not valid\n\n \"\"\"\n\n if isinstance(key, basestring):\n assert key in self._field_names, 'unexpected field name: %s' % key\n else:\n for f in key:\n assert f in self._field_names, 'unexpected field name: %s' % key\n t = key, code, message\n self._unique_checks.append(t)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nvalidating data and return a list of validation problems found.", "response": "def validate(self, data,\n expect_header_row=True,\n ignore_lines=0,\n summarize=False,\n limit=0,\n context=None,\n report_unexpected_exceptions=True):\n \"\"\"\n Validate `data` and return a list of validation problems found.\n\n Arguments\n ---------\n\n `data` - any source of row-oriented data, e.g., as provided by a\n `csv.reader`, or a list of lists of strings, or ...\n\n `expect_header_row` - does the data contain a header row (i.e., the\n first record is a list of field names)? Defaults to True.\n\n `ignore_lines` - ignore n lines (rows) at the beginning of the data\n\n `summarize` - only report problem codes, no other details\n\n `limit` - report at most n problems\n\n `context` - a dictionary of any additional information to be added to\n any problems found - useful if problems are being aggregated from\n multiple validators\n\n `report_unexpected_exceptions` - value check function, value predicates,\n record check functions, record predicates, and other user-supplied\n validation functions may raise unexpected exceptions. If this argument\n is true, any unexpected exceptions will be reported as validation\n problems; if False, unexpected exceptions will be handled silently.\n\n \"\"\"\n\n problems = list()\n problem_generator = self.ivalidate(data, expect_header_row,\n ignore_lines, summarize, context,\n report_unexpected_exceptions)\n for i, p in enumerate(problem_generator):\n if not limit or i < limit:\n problems.append(p)\n return problems"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nvalidate data and return an iterator over problems found.", "response": "def ivalidate(self, data,\n expect_header_row=True,\n ignore_lines=0,\n summarize=False,\n context=None,\n report_unexpected_exceptions=True):\n \"\"\"\n Validate `data` and return a iterator over problems found.\n\n Use this function rather than validate() if you expect a large number\n of problems.\n\n Arguments\n ---------\n\n `data` - any source of row-oriented data, e.g., as provided by a\n `csv.reader`, or a list of lists of strings, or ...\n\n `expect_header_row` - does the data contain a header row (i.e., the\n first record is a list of field names)? Defaults to True.\n\n `ignore_lines` - ignore n lines (rows) at the beginning of the data\n\n `summarize` - only report problem codes, no other details\n\n `context` - a dictionary of any additional information to be added to\n any problems found - useful if problems are being aggregated from\n multiple validators\n\n `report_unexpected_exceptions` - value check function, value predicates,\n record check functions, record predicates, and other user-supplied\n validation functions may raise unexpected exceptions. If this argument\n is true, any unexpected exceptions will be reported as validation\n problems; if False, unexpected exceptions will be handled silently.\n\n \"\"\"\n\n unique_sets = self._init_unique_sets() # used for unique checks\n for i, r in enumerate(data):\n if expect_header_row and i == ignore_lines:\n # r is the header row\n for p in self._apply_header_checks(i, r, summarize, context):\n yield p\n elif i >= ignore_lines:\n # r is a data row\n skip = False\n for p in self._apply_skips(i, r, summarize,\n report_unexpected_exceptions,\n context):\n if p is True:\n skip = True\n else:\n yield p\n if not skip:\n for p in self._apply_each_methods(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p # may yield a problem if an exception is raised\n for p in self._apply_value_checks(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_record_length_checks(i, r, summarize,\n context):\n yield p\n for p in self._apply_value_predicates(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_record_checks(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_record_predicates(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_unique_checks(i, r, unique_sets, summarize):\n yield p\n for p in self._apply_check_methods(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_assert_methods(i, r, summarize,\n report_unexpected_exceptions,\n context):\n yield p\n for p in self._apply_finally_assert_methods(summarize,\n report_unexpected_exceptions,\n context):\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ninitialise sets used for uniqueness checking.", "response": "def _init_unique_sets(self):\n \"\"\"Initialise sets used for uniqueness checking.\"\"\"\n\n ks = dict()\n for t in self._unique_checks:\n key = t[0]\n ks[key] = set() # empty set\n return ks"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nyielding the values from the value checks for the given record i.", "response": "def _apply_value_checks(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply value check functions on the given record `r`.\"\"\"\n\n for field_name, check, code, message, modulus in self._value_checks:\n if i % modulus == 0: # support sampling\n fi = self._field_names.index(field_name)\n if fi < len(r): # only apply checks if there is a value\n value = r[fi]\n try:\n check(value)\n except ValueError:\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['column'] = fi + 1\n p['field'] = field_name\n p['value'] = value\n p['record'] = r\n if context is not None: p['context'] = context\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['column'] = fi + 1\n p['field'] = field_name\n p['value'] = value\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (check.__name__,\n check.__doc__)\n if context is not None: p['context'] = context\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _apply_header_checks(self, i, r, summarize=False, context=None):\n\n for code, message in self._header_checks:\n if tuple(r) != self._field_names:\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['record'] = tuple(r)\n p['missing'] = set(self._field_names) - set(r)\n p['unexpected'] = set(r) - set(self._field_names)\n if context is not None: p['context'] = context\n yield p", "response": "Yields a list of dicts that are passed to the header checks on the given record r."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\napplying record length checks on the given record r.", "response": "def _apply_record_length_checks(self, i, r, summarize=False, context=None):\n \"\"\"Apply record length checks on the given record `r`.\"\"\"\n\n for code, message, modulus in self._record_length_checks:\n if i % modulus == 0: # support sampling\n if len(r) != len(self._field_names):\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n p['length'] = len(r)\n if context is not None: p['context'] = context\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _apply_value_predicates(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply value predicates on the given record `r`.\"\"\"\n\n for field_name, predicate, code, message, modulus in self._value_predicates:\n if i % modulus == 0: # support sampling\n fi = self._field_names.index(field_name)\n if fi < len(r): # only apply predicate if there is a value\n value = r[fi]\n try:\n valid = predicate(value)\n if not valid:\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['column'] = fi + 1\n p['field'] = field_name\n p['value'] = value\n p['record'] = r\n if context is not None: p['context'] = context\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['column'] = fi + 1\n p['field'] = field_name\n p['value'] = value\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (predicate.__name__,\n predicate.__doc__)\n if context is not None: p['context'] = context\n yield p", "response": "Apply value predicates on the given record i."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _apply_record_checks(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply record checks on `r`.\"\"\"\n\n for check, modulus in self._record_checks:\n if i % modulus == 0: # support sampling\n rdict = self._as_dict(r)\n try:\n check(rdict)\n except RecordError as e:\n code = e.code if e.code is not None else RECORD_CHECK_FAILED\n p = {'code': code}\n if not summarize:\n message = e.message if e.message is not None else MESSAGES[RECORD_CHECK_FAILED]\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n if context is not None: p['context'] = context\n if e.details is not None: p['details'] = e.details\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (check.__name__,\n check.__doc__)\n if context is not None: p['context'] = context\n yield p", "response": "Yields a list of record checks that can be performed on r."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\napply record predicates on r.", "response": "def _apply_record_predicates(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply record predicates on `r`.\"\"\"\n\n for predicate, code, message, modulus in self._record_predicates:\n if i % modulus == 0: # support sampling\n rdict = self._as_dict(r)\n try:\n valid = predicate(rdict)\n if not valid:\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n if context is not None: p['context'] = context\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (predicate.__name__,\n predicate.__doc__)\n if context is not None: p['context'] = context\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nyielding unique checks on r.", "response": "def _apply_unique_checks(self, i, r, unique_sets,\n summarize=False,\n context=None):\n \"\"\"Apply unique checks on `r`.\"\"\"\n\n for key, code, message in self._unique_checks:\n value = None\n values = unique_sets[key]\n if isinstance(key, basestring): # assume key is a field name\n fi = self._field_names.index(key)\n if fi >= len(r):\n continue\n value = r[fi]\n else: # assume key is a list or tuple, i.e., compound key\n value = []\n for f in key:\n fi = self._field_names.index(f)\n if fi >= len(r):\n break\n value.append(r[fi])\n value = tuple(value) # enable hashing\n if value in values:\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n p['key'] = key\n p['value'] = value\n if context is not None: p['context'] = context\n yield p\n values.add(value)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _apply_each_methods(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Invoke 'each' methods on `r`.\"\"\"\n\n for a in dir(self):\n if a.startswith('each'):\n rdict = self._as_dict(r)\n f = getattr(self, a)\n try:\n f(rdict)\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (f.__name__,\n f.__doc__)\n if context is not None: p['context'] = context\n yield p", "response": "Invoke each method on r."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nyields a generator of the values of the assert methods on r.", "response": "def _apply_assert_methods(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply 'assert' methods on `r`.\"\"\"\n\n for a in dir(self):\n if a.startswith('assert'):\n rdict = self._as_dict(r)\n f = getattr(self, a)\n try:\n f(rdict)\n except AssertionError as e:\n code = ASSERT_CHECK_FAILED\n message = MESSAGES[ASSERT_CHECK_FAILED]\n if len(e.args) > 0:\n custom = e.args[0]\n if isinstance(custom, (list, tuple)):\n if len(custom) > 0:\n code = custom[0]\n if len(custom) > 1:\n message = custom[1]\n else:\n code = custom\n p = {'code': code}\n if not summarize:\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n if context is not None: p['context'] = context\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (f.__name__,\n f.__doc__)\n if context is not None: p['context'] = context\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _apply_check_methods(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply 'check' methods on `r`.\"\"\"\n\n for a in dir(self):\n if a.startswith('check'):\n rdict = self._as_dict(r)\n f = getattr(self, a)\n try:\n f(rdict)\n except RecordError as e:\n code = e.code if e.code is not None else RECORD_CHECK_FAILED\n p = {'code': code}\n if not summarize:\n message = e.message if e.message is not None else MESSAGES[RECORD_CHECK_FAILED]\n p['message'] = message\n p['row'] = i + 1\n p['record'] = r\n if context is not None: p['context'] = context\n if e.details is not None: p['details'] = e.details\n yield p\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (f.__name__,\n f.__doc__)\n if context is not None: p['context'] = context\n yield p", "response": "Yields a list of dict with the details of all check methods on r."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _apply_skips(self, i, r,\n summarize=False,\n report_unexpected_exceptions=True,\n context=None):\n \"\"\"Apply skip functions on `r`.\"\"\"\n\n for skip in self._skips:\n try:\n result = skip(r)\n if result is True:\n yield True\n except Exception as e:\n if report_unexpected_exceptions:\n p = {'code': UNEXPECTED_EXCEPTION}\n if not summarize:\n p['message'] = MESSAGES[UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)\n p['row'] = i + 1\n p['record'] = r\n p['exception'] = e\n p['function'] = '%s: %s' % (skip.__name__,\n skip.__doc__)\n if context is not None: p['context'] = context\n yield p", "response": "Yields the result of applying the skip functions on r."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert the record to a dictionary using field names as keys.", "response": "def _as_dict(self, r):\n \"\"\"Convert the record to a dictionary using field names as keys.\"\"\"\n\n d = dict()\n for i, f in enumerate(self._field_names):\n d[f] = r[i] if i < len(r) else None\n return d"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating an example CSV validator for patient demographic data.", "response": "def create_validator():\n \"\"\"Create an example CSV validator for patient demographic data.\"\"\"\n\n field_names = (\n 'study_id', \n 'patient_id', \n 'gender', \n 'age_years', \n 'age_months',\n 'date_inclusion'\n )\n validator = CSVValidator(field_names)\n \n # basic header and record length checks\n validator.add_header_check('EX1', 'bad header')\n validator.add_record_length_check('EX2', 'unexpected record length')\n \n # some simple value checks\n validator.add_value_check('study_id', int, \n 'EX3', 'study id must be an integer')\n validator.add_value_check('patient_id', int, \n 'EX4', 'patient id must be an integer')\n validator.add_value_check('gender', enumeration('M', 'F'), \n 'EX5', 'invalid gender')\n validator.add_value_check('age_years', number_range_inclusive(0, 120, int), \n 'EX6', 'invalid age in years')\n validator.add_value_check('date_inclusion', datetime_string('%Y-%m-%d'),\n 'EX7', 'invalid date')\n \n # a more complicated record check\n def check_age_variables(r):\n age_years = int(r['age_years'])\n age_months = int(r['age_months'])\n valid = (age_months >= age_years * 12 and \n age_months % age_years < 12)\n if not valid:\n raise RecordError('EX8', 'invalid age variables')\n validator.add_record_check(check_age_variables)\n \n return validator"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef process_tables(app, docname, source):\n import markdown\n md = markdown.Markdown(extensions=['markdown.extensions.tables'])\n table_processor = markdown.extensions.tables.TableProcessor(md.parser)\n\n raw_markdown = source[0]\n blocks = re.split(r'\\n{2,}', raw_markdown)\n\n for i, block in enumerate(blocks):\n if table_processor.test(None, block):\n html = md.convert(block)\n styled = html.replace('', '
', 1) # apply styling\n blocks[i] = styled\n\n # re-assemble into markdown-with-tables-replaced\n # must replace element 0 for changes to persist\n source[0] = '\\n\\n'.join(blocks)", "response": "Convert markdown tables to html"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\npacks given values v1 v2... into given bytearray buf starting at bit offset offset.", "response": "def pack_into(fmt, buf, offset, *args, **kwargs):\n \"\"\"Pack given values v1, v2, ... into given bytearray `buf`, starting\n at given bit offset `offset`. Pack according to given format\n string `fmt`. Give `fill_padding` as ``False`` to leave padding\n bits in `buf` unmodified.\n\n \"\"\"\n\n return CompiledFormat(fmt).pack_into(buf,\n offset,\n *args,\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pack_into_dict(fmt, names, buf, offset, data, **kwargs):\n\n return CompiledFormatDict(fmt, names).pack_into(buf,\n offset,\n data,\n **kwargs)", "response": "Packs data into a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef byteswap(fmt, data, offset=0):\n\n data = BytesIO(data)\n data.seek(offset)\n data_swapped = BytesIO()\n\n for f in fmt:\n swapped = data.read(int(f))[::-1]\n data_swapped.write(swapped)\n\n return data_swapped.getvalue()", "response": "Swap bytes in data according to fmt starting at byte offset and return the result."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pack(self, *args):\n\n # Sanity check of the number of arguments.\n if len(args) < self._number_of_arguments:\n raise Error(\n \"pack expected {} item(s) for packing (got {})\".format(\n self._number_of_arguments,\n len(args)))\n\n return self.pack_any(args)", "response": "Packs the given items into a new set of items."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef pack_into(self, buf, offset, *args, **kwargs):\n\n # Sanity check of the number of arguments.\n if len(args) < self._number_of_arguments:\n raise Error(\n \"pack expected {} item(s) for packing (got {})\".format(\n self._number_of_arguments,\n len(args)))\n\n self.pack_into_any(buf, offset, args, **kwargs)", "response": "Pack the items into the buffer."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unpack_from(self, data, offset=0):\n\n return tuple([v[1] for v in self.unpack_from_any(data, offset)])", "response": "Unpack the bitstring from the given data."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\npacks the data dictionary into a single object.", "response": "def pack(self, data):\n \"\"\"See :func:`~bitstruct.pack_dict()`.\n\n \"\"\"\n\n try:\n return self.pack_any(data)\n except KeyError as e:\n raise Error('{} not found in data dictionary'.format(str(e)))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npack the data into the buffer.", "response": "def pack_into(self, buf, offset, data, **kwargs):\n \"\"\"See :func:`~bitstruct.pack_into_dict()`.\n\n \"\"\"\n\n try:\n self.pack_into_any(buf, offset, data, **kwargs)\n except KeyError as e:\n raise Error('{} not found in data dictionary'.format(str(e)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unpack_from(self, data, offset=0):\n\n return {info.name: v for info, v in self.unpack_from_any(data, offset)}", "response": "Unpacks the set of fields from the given data."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cli(ctx, version):\n\n # If no subcommand was given and the version flag is true, shows\n # Bottery version\n if not ctx.invoked_subcommand and version:\n click.echo(bottery.__version__)\n ctx.exit()\n\n # If no subcommand but neither the version flag, shows help message\n elif not ctx.invoked_subcommand:\n click.echo(ctx.get_help())\n ctx.exit()", "response": "Bottery version and help message"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef build_message(self, data):\n '''\n Return a Message instance according to the data received from\n Telegram API.\n https://core.telegram.org/bots/api#update\n '''\n message_data = data.get('message') or data.get('edited_message')\n\n if not message_data:\n return None\n\n edited = 'edited_message' in data\n return Message(\n id=message_data['message_id'],\n platform=self.platform,\n text=message_data.get('text', ''),\n user=TelegramUser(message_data['from']),\n chat=TelegramChat(message_data['chat']),\n timestamp=message_data['date'],\n raw=data,\n edited=edited,\n )", "response": "Build a Message instance according to the data received from Telegram API."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_chat_id(self, message):\n '''\n Telegram chat type can be either \"private\", \"group\", \"supergroup\" or\n \"channel\".\n Return user ID if it is of type \"private\", chat ID otherwise.\n '''\n if message.chat.type == 'private':\n return message.user.id\n\n return message.chat.id", "response": "Return Telegram chat ID if it is of type private otherwise return chat ID."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nbuilding a Message instance according to the data received from Facebook Messenger API.", "response": "def build_message(self, data):\n '''\n Return a Message instance according to the data received from\n Facebook Messenger API.\n '''\n if not data:\n return None\n\n return Message(\n id=data['message']['mid'],\n platform=self.platform,\n text=data['message']['text'],\n user=data['sender']['id'],\n timestamp=data['timestamp'],\n raw=data,\n chat=None, # TODO: Refactor build_messages and Message class\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget response from the view with async syntax.", "response": "async def _get_response(self, message):\n \"\"\"\n Get response running the view with await syntax if it is a\n coroutine function, otherwise just run it the normal way.\n \"\"\"\n\n view = self.discovery_view(message)\n if not view:\n return\n\n if inspect.iscoroutinefunction(view):\n response = await view(message)\n else:\n response = view(message)\n\n return self.prepare_response(response, message)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsearching for a registered view according to the new message.", "response": "def discovery_view(self, message):\n \"\"\"\n Use the new message to search for a registered view according\n to its pattern.\n \"\"\"\n for handler in self.registered_handlers:\n if handler.check(message):\n return handler.view\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nprocessing a new message and send it to the client.", "response": "async def message_handler(self, data):\n \"\"\"\n For each new message, build its platform specific message\n object and get a response.\n \"\"\"\n\n message = self.build_message(data)\n if not message:\n logger.error(\n '[%s] Unable to build Message with data, data=%s, error',\n self.engine_name,\n data\n )\n return\n\n logger.info('[%s] New message from %s: %s', self.engine_name,\n message.user, message.text)\n\n response = await self.get_response(message)\n if response:\n await self.send_response(response)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nunpacking a byte string to the given format.", "response": "def unpack(endian, fmt, data):\n \"\"\"Unpack a byte string to the given format. If the byte string\n contains more bytes than required for the given format, the function\n returns a tuple of values.\n \"\"\"\n if fmt == 's':\n # read data as an array of chars\n val = struct.unpack(''.join([endian, str(len(data)), 's']),\n data)[0]\n else:\n # read a number of values\n num = len(data) // struct.calcsize(fmt)\n val = struct.unpack(''.join([endian, str(num), fmt]), data)\n if len(val) == 1:\n val = val[0]\n return val"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef read_file_header(fd, endian):\n fields = [\n ('description', 's', 116),\n ('subsystem_offset', 's', 8),\n ('version', 'H', 2),\n ('endian_test', 's', 2)\n ]\n hdict = {}\n for name, fmt, num_bytes in fields:\n data = fd.read(num_bytes)\n hdict[name] = unpack(endian, fmt, data)\n hdict['description'] = hdict['description'].strip()\n v_major = hdict['version'] >> 8\n v_minor = hdict['version'] & 0xFF\n hdict['__version__'] = '%d.%d' % (v_major, v_minor)\n return hdict", "response": "Read mat 5 file header of the file fd."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_element_tag(fd, endian):\n data = fd.read(8)\n mtpn = unpack(endian, 'I', data[:4])\n # The most significant two bytes of mtpn will always be 0,\n # if they are not, this must be SDE format\n num_bytes = mtpn >> 16\n if num_bytes > 0:\n # small data element format\n mtpn = mtpn & 0xFFFF\n if num_bytes > 4:\n raise ParseError('Error parsing Small Data Element (SDE) '\n 'formatted data')\n data = data[4:4 + num_bytes]\n else:\n # regular element\n num_bytes = unpack(endian, 'I', data[4:])\n data = None\n return (mtpn, num_bytes, data)", "response": "Read data element tag."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_elements(fd, endian, mtps, is_name=False):\n mtpn, num_bytes, data = read_element_tag(fd, endian)\n if mtps and mtpn not in [etypes[mtp]['n'] for mtp in mtps]:\n raise ParseError('Got type {}, expected {}'.format(\n mtpn, ' / '.join('{} ({})'.format(\n etypes[mtp]['n'], mtp) for mtp in mtps)))\n if not data:\n # full format, read data\n data = fd.read(num_bytes)\n # Seek to next 64-bit boundary\n mod8 = num_bytes % 8\n if mod8:\n fd.seek(8 - mod8, 1)\n\n # parse data and return values\n if is_name:\n # names are stored as miINT8 bytes\n fmt = 's'\n val = [unpack(endian, fmt, s)\n for s in data.split(b'\\0') if s]\n if len(val) == 0:\n val = ''\n elif len(val) == 1:\n val = asstr(val[0])\n else:\n val = [asstr(s) for s in val]\n else:\n fmt = etypes[inv_etypes[mtpn]]['fmt']\n val = unpack(endian, fmt, data)\n return val", "response": "Read elements from the file and return them as a list of possible matrix data types."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef read_header(fd, endian):\n flag_class, nzmax = read_elements(fd, endian, ['miUINT32'])\n header = {\n 'mclass': flag_class & 0x0FF,\n 'is_logical': (flag_class >> 9 & 1) == 1,\n 'is_global': (flag_class >> 10 & 1) == 1,\n 'is_complex': (flag_class >> 11 & 1) == 1,\n 'nzmax': nzmax\n }\n header['dims'] = read_elements(fd, endian, ['miINT32'])\n header['n_dims'] = len(header['dims'])\n if header['n_dims'] != 2:\n raise ParseError('Only matrices with dimension 2 are supported.')\n header['name'] = read_elements(fd, endian, ['miINT8'], is_name=True)\n return header", "response": "Read and return the matrix header."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_var_header(fd, endian):\n mtpn, num_bytes = unpack(endian, 'II', fd.read(8))\n next_pos = fd.tell() + num_bytes\n\n if mtpn == etypes['miCOMPRESSED']['n']:\n # read compressed data\n data = fd.read(num_bytes)\n dcor = zlib.decompressobj()\n # from here, read of the decompressed data\n fd_var = BytesIO(dcor.decompress(data))\n del data\n fd = fd_var\n # Check the stream is not so broken as to leave cruft behind\n if dcor.flush() != b'':\n raise ParseError('Error in compressed data.')\n # read full tag from the uncompressed data\n mtpn, num_bytes = unpack(endian, 'II', fd.read(8))\n\n if mtpn != etypes['miMATRIX']['n']:\n raise ParseError('Expecting miMATRIX type number {}, '\n 'got {}'.format(etypes['miMATRIX']['n'], mtpn))\n # read the header\n header = read_header(fd, endian)\n return header, next_pos, fd", "response": "Read the var header of the next tag."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_numeric_array(fd, endian, header, data_etypes):\n if header['is_complex']:\n raise ParseError('Complex arrays are not supported')\n # read array data (stored as column-major)\n data = read_elements(fd, endian, data_etypes)\n if not isinstance(data, Sequence):\n # not an array, just a value\n return data\n # transform column major data continous array to\n # a row major array of nested lists\n rowcount = header['dims'][0]\n colcount = header['dims'][1]\n array = [list(data[c * rowcount + r] for c in range(colcount))\n for r in range(rowcount)]\n # pack and return the array\n return squeeze(array)", "response": "Read a numeric matrix."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads a cell array.", "response": "def read_cell_array(fd, endian, header):\n \"\"\"Read a cell array.\n Returns an array with rows of the cell array.\n \"\"\"\n array = [list() for i in range(header['dims'][0])]\n for row in range(header['dims'][0]):\n for col in range(header['dims'][1]):\n # read the matrix header and array\n vheader, next_pos, fd_var = read_var_header(fd, endian)\n varray = read_var_array(fd_var, endian, vheader)\n array[row].append(varray)\n # move on to next field\n fd.seek(next_pos)\n # pack and return the array\n if header['dims'][0] == 1:\n return squeeze(array[0])\n return squeeze(array)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreading a struct array.", "response": "def read_struct_array(fd, endian, header):\n \"\"\"Read a struct array.\n Returns a dict with fields of the struct array.\n \"\"\"\n # read field name length (unused, as strings are null terminated)\n field_name_length = read_elements(fd, endian, ['miINT32'])\n if field_name_length > 32:\n raise ParseError('Unexpected field name length: {}'.format(\n field_name_length))\n\n # read field names\n fields = read_elements(fd, endian, ['miINT8'], is_name=True)\n if isinstance(fields, basestring):\n fields = [fields]\n\n # read rows and columns of each field\n empty = lambda: [list() for i in range(header['dims'][0])]\n array = {}\n for row in range(header['dims'][0]):\n for col in range(header['dims'][1]):\n for field in fields:\n # read the matrix header and array\n vheader, next_pos, fd_var = read_var_header(fd, endian)\n data = read_var_array(fd_var, endian, vheader)\n if field not in array:\n array[field] = empty()\n array[field][row].append(data)\n # move on to next field\n fd.seek(next_pos)\n # pack the nested arrays\n for field in fields:\n rows = array[field]\n for i in range(header['dims'][0]):\n rows[i] = squeeze(rows[i])\n array[field] = squeeze(array[field])\n return array"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread a variable array of any supported type.", "response": "def read_var_array(fd, endian, header):\n \"\"\"Read variable array (of any supported type).\"\"\"\n mc = inv_mclasses[header['mclass']]\n\n if mc in numeric_class_etypes:\n return read_numeric_array(\n fd, endian, header,\n set(compressed_numeric).union([numeric_class_etypes[mc]])\n )\n elif mc == 'mxSPARSE_CLASS':\n raise ParseError('Sparse matrices not supported')\n elif mc == 'mxCHAR_CLASS':\n return read_char_array(fd, endian, header)\n elif mc == 'mxCELL_CLASS':\n return read_cell_array(fd, endian, header)\n elif mc == 'mxSTRUCT_CLASS':\n return read_struct_array(fd, endian, header)\n elif mc == 'mxOBJECT_CLASS':\n raise ParseError('Object classes not supported')\n elif mc == 'mxFUNCTION_CLASS':\n raise ParseError('Function classes not supported')\n elif mc == 'mxOPAQUE_CLASS':\n raise ParseError('Anonymous function classes not supported')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef eof(fd):\n b = fd.read(1)\n end = len(b) == 0\n if not end:\n curpos = fd.tell()\n fd.seek(curpos - 1)\n return end", "response": "Determine if end - of - file is reached for file fd."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef loadmat(filename, meta=False):\n\n if isinstance(filename, basestring):\n fd = open(filename, 'rb')\n else:\n fd = filename\n\n # Check mat file format is version 5\n # For 5 format we need to read an integer in the header.\n # Bytes 124 through 128 contain a version integer and an\n # endian test string\n fd.seek(124)\n tst_str = fd.read(4)\n little_endian = (tst_str[2:4] == b'IM')\n endian = ''\n if (sys.byteorder == 'little' and little_endian) or \\\n (sys.byteorder == 'big' and not little_endian):\n # no byte swapping same endian\n pass\n elif sys.byteorder == 'little':\n # byte swapping\n endian = '>'\n else:\n # byte swapping\n endian = '<'\n maj_ind = int(little_endian)\n # major version number\n maj_val = ord(tst_str[maj_ind]) if ispy2 else tst_str[maj_ind]\n if maj_val != 1:\n raise ParseError('Can only read from Matlab level 5 MAT-files')\n # the minor version number (unused value)\n # min_val = ord(tst_str[1 - maj_ind]) if ispy2 else tst_str[1 - maj_ind]\n\n mdict = {}\n if meta:\n # read the file header\n fd.seek(0)\n mdict['__header__'] = read_file_header(fd, endian)\n mdict['__globals__'] = []\n\n # read data elements\n while not eof(fd):\n hdr, next_position, fd_var = read_var_header(fd, endian)\n name = hdr['name']\n if name in mdict:\n raise ParseError('Duplicate variable name \"{}\" in mat file.'\n .format(name))\n\n # read the matrix\n mdict[name] = read_var_array(fd_var, endian, hdr)\n if meta and hdr['is_global']:\n mdict['__globals__'].append(name)\n\n # move on to next entry in file\n fd.seek(next_position)\n\n fd.close()\n return mdict", "response": "Load data from Matlab MAT - file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite data elements to the file.", "response": "def write_elements(fd, mtp, data, is_name=False):\n \"\"\"Write data element tag and data.\n\n The tag contains the array type and the number of\n bytes the array data will occupy when written to file.\n\n If data occupies 4 bytes or less, it is written immediately\n as a Small Data Element (SDE).\n \"\"\"\n fmt = etypes[mtp]['fmt']\n if isinstance(data, Sequence):\n if fmt == 's' or is_name:\n if isinstance(data, bytes):\n if is_name and len(data) > 31:\n raise ValueError(\n 'Name \"{}\" is too long (max. 31 '\n 'characters allowed)'.format(data))\n fmt = '{}s'.format(len(data))\n data = (data,)\n else:\n fmt = ''.join('{}s'.format(len(s)) for s in data)\n else:\n l = len(data)\n if l == 0:\n # empty array\n fmt = ''\n if l > 1:\n # more than one element to be written\n fmt = '{}{}'.format(l, fmt)\n else:\n data = (data,)\n num_bytes = struct.calcsize(fmt)\n if num_bytes <= 4:\n # write SDE\n if num_bytes < 4:\n # add pad bytes\n fmt += '{}x'.format(4 - num_bytes)\n fd.write(struct.pack('hh' + fmt, etypes[mtp]['n'],\n *chain([num_bytes], data)))\n return\n # write tag: element type and number of bytes\n fd.write(struct.pack('b3xI', etypes[mtp]['n'], num_bytes))\n # add pad bytes to fmt, if needed\n mod8 = num_bytes % 8\n if mod8:\n fmt += '{}x'.format(8 - mod8)\n # write data\n fd.write(struct.pack(fmt, *data))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrite variable data to file", "response": "def write_var_data(fd, data):\n \"\"\"Write variable data to file\"\"\"\n # write array data elements (size info)\n fd.write(struct.pack('b3xI', etypes['miMATRIX']['n'], len(data)))\n\n # write the data\n fd.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrite compressed variable data to file", "response": "def write_compressed_var_array(fd, array, name):\n \"\"\"Write compressed variable data to file\"\"\"\n bd = BytesIO()\n\n write_var_array(bd, array, name)\n\n data = zlib.compress(bd.getvalue())\n bd.close()\n\n # write array data elements (size info)\n fd.write(struct.pack('b3xI', etypes['miCOMPRESSED']['n'], len(data)))\n\n # write the compressed data\n fd.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef write_numeric_array(fd, header, array):\n # make a memory file for writing array data\n bd = BytesIO()\n\n # write matrix header to memory file\n write_var_header(bd, header)\n\n if not isinstance(array, basestring) and header['dims'][0] > 1:\n # list array data in column major order\n array = list(chain.from_iterable(izip(*array)))\n\n # write matrix data to memory file\n write_elements(bd, header['mtp'], array)\n\n # write the variable to disk file\n data = bd.getvalue()\n bd.close()\n write_var_data(fd, data)", "response": "Write the numeric array to disk"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwrites a variable array of any supported type.", "response": "def write_var_array(fd, array, name=''):\n \"\"\"Write variable array (of any supported type)\"\"\"\n header, array = guess_header(array, name)\n mc = header['mclass']\n if mc in numeric_class_etypes:\n return write_numeric_array(fd, header, array)\n elif mc == 'mxCHAR_CLASS':\n return write_char_array(fd, header, array)\n elif mc == 'mxCELL_CLASS':\n return write_cell_array(fd, header, array)\n elif mc == 'mxSTRUCT_CLASS':\n return write_struct_array(fd, header, array)\n else:\n raise ValueError('Unknown mclass {}'.format(mc))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if test is True for all array elements.", "response": "def isarray(array, test, dim=2):\n \"\"\"Returns True if test is True for all array elements.\n Otherwise, returns False.\n \"\"\"\n if dim > 1:\n return all(isarray(array[i], test, dim - 1)\n for i in range(len(array)))\n return all(test(i) for i in array)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nguessing the array header information.", "response": "def guess_header(array, name=''):\n \"\"\"Guess the array header information.\n Returns a header dict, with class, data type, and size information.\n \"\"\"\n header = {}\n\n if isinstance(array, Sequence) and len(array) == 1:\n # sequence with only one element, squeeze the array\n array = array[0]\n\n if isinstance(array, basestring):\n header.update({\n 'mclass': 'mxCHAR_CLASS', 'mtp': 'miUTF8',\n 'dims': (1 if len(array) > 0 else 0, len(array))})\n\n elif isinstance(array, Sequence) and len(array) == 0:\n # empty (int) array\n header.update({\n 'mclass': 'mxINT32_CLASS', 'mtp': 'miINT32', 'dims': (0, 0)})\n\n elif isinstance(array, Mapping):\n # test if cells (values) of all fields are of equal type and\n # have equal length\n field_types = [type(j) for j in array.values()]\n field_lengths = [1 if isinstance(j, (basestring, int, float))\n else len(j) for j in array.values()]\n if len(field_lengths) == 1:\n equal_lengths = True\n equal_types = True\n else:\n equal_lengths = not any(diff(field_lengths))\n equal_types = all([field_types[0] == f for f in field_types])\n\n # if of unqeual lengths or unequal types, then treat each value\n # as a cell in a 1x1 struct\n header.update({\n 'mclass': 'mxSTRUCT_CLASS',\n 'dims': (\n 1,\n field_lengths[0] if equal_lengths and equal_types else 1)}\n )\n\n elif isinstance(array, int):\n header.update({\n 'mclass': 'mxINT32_CLASS', 'mtp': 'miINT32', 'dims': (1, 1)})\n\n elif isinstance(array, float):\n header.update({\n 'mclass': 'mxDOUBLE_CLASS', 'mtp': 'miDOUBLE', 'dims': (1, 1)})\n\n elif isinstance(array, Sequence):\n\n if isarray(array, lambda i: isinstance(i, int), 1):\n # 1D int array\n header.update({\n 'mclass': 'mxINT32_CLASS', 'mtp': 'miINT32',\n 'dims': (1, len(array))})\n\n elif isarray(array, lambda i: isinstance(i, (int, float)), 1):\n # 1D double array\n header.update({\n 'mclass': 'mxDOUBLE_CLASS', 'mtp': 'miDOUBLE',\n 'dims': (1, len(array))})\n\n elif (isarray(array, lambda i: isinstance(i, Sequence), 1) and\n any(diff(len(s) for s in array))):\n # sequence of unequal length, assume cell array\n header.update({\n 'mclass': 'mxCELL_CLASS',\n 'dims': (1, len(array))\n })\n\n elif isarray(array, lambda i: isinstance(i, basestring), 1):\n # char array\n header.update({\n 'mclass': 'mxCHAR_CLASS', 'mtp': 'miUTF8',\n 'dims': (len(array), len(array[0]))})\n\n elif isarray(array, lambda i: isinstance(i, Sequence), 1):\n # 2D array\n\n if any(diff(len(j) for j in array)):\n # rows are of unequal length, make it a cell array\n header.update({\n 'mclass': 'mxCELL_CLASS',\n 'dims': (len(array), len(array[0]))})\n\n elif isarray(array, lambda i: isinstance(i, int)):\n # 2D int array\n header.update({\n 'mclass': 'mxINT32_CLASS', 'mtp': 'miINT32',\n 'dims': (len(array), len(array[0]))})\n\n elif isarray(array, lambda i: isinstance(i, (int, float))):\n # 2D double array\n header.update({\n 'mclass': 'mxDOUBLE_CLASS',\n 'mtp': 'miDOUBLE',\n 'dims': (len(array), len(array[0]))})\n\n elif isarray(array, lambda i: isinstance(\n i, (int, float, basestring, Sequence, Mapping))):\n # mixed contents, make it a cell array\n header.update({\n 'mclass': 'mxCELL_CLASS',\n 'dims': (1, len(array))})\n\n if not header:\n raise ValueError(\n 'Only dicts, two dimensional numeric, '\n 'and char arrays are currently supported')\n header['name'] = name\n return header, array"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsaves data to MAT - file.", "response": "def savemat(filename, data):\n \"\"\"Save data to MAT-file:\n\n savemat(filename, data)\n\n The filename argument is either a string with the filename, or\n a file like object.\n\n The parameter ``data`` shall be a dict with the variables.\n\n A ``ValueError`` exception is raised if data has invalid format, or if the\n data structure cannot be mapped to a known MAT array type.\n \"\"\"\n\n if not isinstance(data, Mapping):\n raise ValueError('Data should be a dict of variable arrays')\n\n if isinstance(filename, basestring):\n fd = open(filename, 'wb')\n else:\n fd = filename\n\n write_file_header(fd)\n\n # write variables\n for name, array in data.items():\n write_compressed_var_array(fd, array, name)\n\n fd.close()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _execute(self, command, data=None, unpack=True):\n if not data:\n data = {}\n if self.session_id is not None:\n data.setdefault('session_id', self.session_id)\n data = self._wrap_el(data)\n res = self.remote_invoker.execute(command, data)\n ret = WebDriverResult.from_object(res)\n ret.raise_for_status()\n ret.value = self._unwrap_el(ret.value)\n if not unpack:\n return ret\n return ret.value", "response": "Private method to execute command."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _unwrap_el(self, value):\n if isinstance(value, dict) and 'ELEMENT' in value:\n element_id = value.get('ELEMENT')\n return WebElement(element_id, self)\n elif isinstance(value, list) and not isinstance(value, str):\n return [self._unwrap_el(item) for item in value]\n else:\n return value", "response": "Convert the value field in the json response to WebElement Object\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert WebElement Object to dict", "response": "def _wrap_el(self, value):\n \"\"\"Convert WebElement Object to {'Element': 1234}\n\n Args:\n value(str|list|dict): The local value.\n\n Returns:\n The wrapped value.\n \"\"\"\n if isinstance(value, dict):\n return {k: self._wrap_el(v) for k, v in value.items()}\n elif isinstance(value, WebElement):\n return {'ELEMENT': value.element_id}\n elif isinstance(value, list) and not isinstance(value, str):\n return [self._wrap_el(item) for item in value]\n else:\n return value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a new session by desiredCapabilities Support : Android iOS Web Web", "response": "def init(self):\n \"\"\"Create Session by desiredCapabilities\n\n Support:\n Android iOS Web(WebView)\n\n Returns:\n WebDriver Object.\n \"\"\"\n resp = self._execute(Command.NEW_SESSION, {\n 'desiredCapabilities': self.desired_capabilities\n }, False)\n resp.raise_for_status()\n self.session_id = str(resp.session_id)\n self.capabilities = resp.value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nswitch focus to the given window.", "response": "def switch_to_window(self, window_name):\n \"\"\"Switch to the given window.\n\n Support:\n Web(WebView)\n\n Args:\n window_name(str): The window to change focus to.\n\n Returns:\n WebDriver Object.\n \"\"\"\n data = {\n 'name': window_name\n }\n self._execute(Command.SWITCH_TO_WINDOW, data)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_window_size(self, width, height, window_handle='current'):\n self._execute(Command.SET_WINDOW_SIZE, {\n 'width': int(width),\n 'height': int(height),\n 'window_handle': window_handle})", "response": "Sets the width and height of the current window."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_window_position(self, x, y, window_handle='current'):\n self._execute(Command.SET_WINDOW_POSITION, {\n 'x': int(x),\n 'y': int(y),\n 'window_handle': window_handle})", "response": "Sets the x y position of the current window."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndeprecate use element. touch move toX toY duration in seconds.", "response": "def move_to(self, element, x=0, y=0):\n \"\"\"Deprecated use element.touch('drag', { toX, toY, duration(s) }) instead.\n Move the mouse by an offset of the specificed element.\n\n Support:\n Android\n\n Args:\n element(WebElement): WebElement Object.\n x(float): X offset to move to, relative to the\n top-left corner of the element.\n y(float): Y offset to move to, relative to the\n top-left corner of the element.\n\n Returns:\n WebDriver object.\n \"\"\"\n self._execute(Command.MOVE_TO, {\n 'element': element.element_id,\n 'x': x,\n 'y': y\n })"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef flick(self, element, x, y, speed):\n self._execute(Command.FLICK, {\n 'element': element.element_id,\n 'x': x,\n 'y': y,\n 'speed': speed\n })", "response": "Flick an element on the touch screen using finger motion events."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nswitches focus to the specified frame.", "response": "def switch_to_frame(self, frame_reference=None):\n \"\"\"Switches focus to the specified frame, by index, name, or webelement.\n\n Support:\n Web(WebView)\n\n Args:\n frame_reference(None|int|WebElement):\n The identifier of the frame to switch to.\n None means to set to the default context.\n An integer representing the index.\n A webelement means that is an (i)frame to switch to.\n Otherwise throw an error.\n\n Returns:\n WebDriver Object.\n \"\"\"\n if frame_reference is not None and type(frame_reference) not in [int, WebElement]:\n raise TypeError('Type of frame_reference must be None or int or WebElement')\n self._execute(Command.SWITCH_TO_FRAME,\n {'id': frame_reference})"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute JavaScript Synchronously in current context.", "response": "def execute_script(self, script, *args):\n \"\"\"Execute JavaScript Synchronously in current context.\n\n Support:\n Web(WebView)\n\n Args:\n script: The JavaScript to execute.\n *args: Arguments for your JavaScript.\n\n Returns:\n Returns the return value of the function.\n \"\"\"\n return self._execute(Command.EXECUTE_SCRIPT, {\n 'script': script,\n 'args': list(args)})"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef execute_async_script(self, script, *args):\n return self._execute(Command.EXECUTE_ASYNC_SCRIPT, {\n 'script': script,\n 'args': list(args)})", "response": "Execute JavaScript Asynchronously in current context."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset a cookie. Support: Web(WebView) Args: cookie_dict: A dictionary contain keys: \"name\", \"value\", [\"path\"], [\"domain\"], [\"secure\"], [\"httpOnly\"], [\"expiry\"]. Returns: WebElement Object.", "response": "def add_cookie(self, cookie_dict):\n \"\"\"Set a cookie.\n\n Support:\n Web(WebView)\n\n Args:\n cookie_dict: A dictionary contain keys: \"name\", \"value\",\n [\"path\"], [\"domain\"], [\"secure\"], [\"httpOnly\"], [\"expiry\"].\n\n Returns:\n WebElement Object.\n \"\"\"\n if not isinstance(cookie_dict, dict):\n raise TypeError('Type of the cookie must be a dict.')\n if not cookie_dict.get(\n 'name', None\n ) or not cookie_dict.get(\n 'value', None):\n raise KeyError('Missing required keys, \\'name\\' and \\'value\\' must be provided.')\n self._execute(Command.ADD_COOKIE, {'cookie': cookie_dict})"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef save_screenshot(self, filename, quietly = False):\n imgData = self.take_screenshot()\n try:\n with open(filename, \"wb\") as f:\n f.write(b64decode(imgData.encode('ascii')))\n except IOError as err:\n if not quietly:\n raise err", "response": "Save the screenshot to a local file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfinding an element in the current context.", "response": "def element(self, using, value):\n \"\"\"Find an element in the current context.\n\n Support:\n Android iOS Web(WebView)\n\n Args:\n using(str): The element location strategy.\n value(str): The value of the location strategy.\n\n Returns:\n WebElement Object.\n\n Raises:\n WebDriverException.\n \"\"\"\n return self._execute(Command.FIND_ELEMENT, {\n 'using': using,\n 'value': value\n })"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef element_if_exists(self, using, value):\n try:\n self._execute(Command.FIND_ELEMENT, {\n 'using': using,\n 'value': value\n })\n return True\n except:\n return False", "response": "Check if an element in the current context exists."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if an element exists and return None if it does not exist.", "response": "def element_or_none(self, using, value):\n \"\"\"Check if an element in the current context.\n\n Support:\n Android iOS Web(WebView)\n\n Args:\n using(str): The element location strategy.\n value(str): The value of the location strategy.\n\n Returns:\n Return Element if the element does exists and return None otherwise.\n\n Raises:\n WebDriverException.\n \"\"\"\n try:\n return self._execute(Command.FIND_ELEMENT, {\n 'using': using,\n 'value': value\n })\n except:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef elements(self, using, value):\n return self._execute(Command.FIND_ELEMENTS, {\n 'using': using,\n 'value': value\n })", "response": "Find elements in the current context."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwaiting for the driver till satisfy the condition AttributeNames", "response": "def wait_for(\n self, timeout=10000, interval=1000,\n asserter=lambda x: x):\n \"\"\"Wait for driver till satisfy the given condition\n\n Support:\n Android iOS Web(WebView)\n\n Args:\n timeout(int): How long we should be retrying stuff.\n interval(int): How long between retries.\n asserter(callable): The asserter func to determine the result.\n\n Returns:\n Return the driver.\n\n Raises:\n WebDriverException.\n \"\"\"\n if not callable(asserter):\n raise TypeError('Asserter must be callable.')\n @retry(\n retry_on_exception=lambda ex: isinstance(ex, WebDriverException),\n stop_max_delay=timeout,\n wait_fixed=interval\n )\n def _wait_for(driver):\n asserter(driver)\n return driver\n\n return _wait_for(self)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef wait_for_element(\n self, using, value, timeout=10000,\n interval=1000, asserter=is_displayed):\n \"\"\"Wait for element till satisfy the given condition\n\n Support:\n Android iOS Web(WebView)\n\n Args:\n using(str): The element location strategy.\n value(str): The value of the location strategy.\n timeout(int): How long we should be retrying stuff.\n interval(int): How long between retries.\n asserter(callable): The asserter func to determine the result.\n\n Returns:\n Return the Element.\n\n Raises:\n WebDriverException.\n \"\"\"\n if not callable(asserter):\n raise TypeError('Asserter must be callable.')\n @retry(\n retry_on_exception=lambda ex: isinstance(ex, WebDriverException),\n stop_max_delay=timeout,\n wait_fixed=interval\n )\n def _wait_for_element(ctx, using, value):\n el = ctx.element(using, value)\n asserter(el)\n return el\n\n return _wait_for_element(self, using, value)", "response": "Wait for an element till satisfy the condition using."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef wait_for_elements(\n self, using, value, timeout=10000,\n interval=1000, asserter=is_displayed):\n \"\"\"Wait for elements till satisfy the given condition\n\n Support:\n Android iOS Web(WebView)\n\n Args:\n using(str): The element location strategy.\n value(str): The value of the location strategy.\n timeout(int): How long we should be retrying stuff.\n interval(int): How long between retries.\n asserter(callable): The asserter func to determine the result.\n\n Returns:\n Return the list of Element if any of them satisfy the condition.\n\n Raises:\n WebDriverException.\n \"\"\"\n if not callable(asserter):\n raise TypeError('Asserter must be callable.')\n @retry(\n retry_on_exception=lambda ex: isinstance(ex, WebDriverException),\n stop_max_delay=timeout,\n wait_fixed=interval\n )\n def _wait_for_elements(ctx, using, value):\n els = ctx.elements(using, value)\n if not len(els):\n raise WebDriverException('no such element')\n else:\n el = els[0]\n asserter(el)\n return els\n\n return _wait_for_elements(self, using, value)", "response": "Wait for elements till satisfy the condition."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef from_object(cls, obj):\n return cls(\n obj.get('sessionId', None),\n obj.get('status', 0),\n obj.get('value', None)\n )", "response": "The factory method to create WebDriverResult from a JSON object returned by server."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nraising WebDriverException if returned status is not zero.", "response": "def raise_for_status(self):\n \"\"\"Raise WebDriverException if returned status is not zero.\"\"\"\n if not self.status:\n return\n\n error = find_exception_by_code(self.status)\n message = None\n screen = None\n stacktrace = None\n\n if isinstance(self.value, str):\n message = self.value\n elif isinstance(self.value, dict):\n message = self.value.get('message', None)\n screen = self.value.get('screen', None)\n stacktrace = self.value.get('stacktrace', None)\n\n raise WebDriverException(error, message, screen, stacktrace)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding element_by alias and extension methods (if_exists or or_none.", "response": "def add_element_extension_method(Klass):\n \"\"\"Add element_by alias and extension' methods(if_exists/or_none).\"\"\"\n def add_element_method(Klass, using):\n locator = using.name.lower()\n find_element_name = \"element_by_\" + locator\n find_element_if_exists_name = \"element_by_\" + locator + \"_if_exists\"\n find_element_or_none_name = \"element_by_\" + locator + \"_or_none\"\n wait_for_element_name = \"wait_for_element_by_\" + locator\n\n find_elements_name = \"elements_by_\" + locator\n wait_for_elements_name = \"wait_for_elements_by_\" + locator\n\n def find_element(self, value):\n return self.element(using.value, value)\n\n find_element.__name__ = find_element_name\n find_element.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'element\\' method.\"\n )\n\n def find_element_if_exists(self, value):\n return self.element_if_exists(using.value, value)\n\n find_element_if_exists.__name__ = find_element_if_exists_name\n find_element_if_exists.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'element_if_exists\\' method.\"\n )\n\n def find_element_or_none(self, value):\n return self.element_or_none(using.value, value)\n\n find_element_or_none.__name__ = find_element_or_none_name\n find_element_or_none.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'element_or_none\\' method.\"\n )\n\n def wait_for_element_by(self, *args, **kwargs):\n return self.wait_for_element(using.value, *args, **kwargs)\n\n wait_for_element_by.__name__ = wait_for_element_name\n wait_for_element_by.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'wait_for_element\\' method.\"\n )\n\n def find_elements(self, value):\n return self.elements(using.value, value)\n\n find_elements.__name__ = find_elements_name\n find_elements.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'elements\\' method.\"\n )\n\n def wait_for_elements_available(self, *args, **kwargs):\n return self.wait_for_elements(using.value, *args, **kwargs)\n\n wait_for_elements_available.__name__ = wait_for_elements_name\n wait_for_elements_available.__doc__ = (\n \"Set parameter 'using' to '{0}'.\\n\".format(using.value) +\n \"See more in \\'wait_for_elements\\' method.\"\n )\n\n setattr(Klass, find_element_name, find_element)\n setattr(Klass, find_element_if_exists_name, find_element_if_exists)\n setattr(Klass, find_element_or_none_name, find_element_or_none)\n setattr(Klass, wait_for_element_name, wait_for_element_by)\n setattr(Klass, find_elements_name, find_elements)\n setattr(Klass, wait_for_elements_name, wait_for_elements_available)\n\n for locator in iter(Locator):\n add_element_method(Klass, locator)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts value to a list of key strokes", "response": "def value_to_key_strokes(value):\n \"\"\"Convert value to a list of key strokes\n >>> value_to_key_strokes(123)\n ['123']\n >>> value_to_key_strokes('123')\n ['123']\n >>> value_to_key_strokes([1, 2, 3])\n ['123']\n >>> value_to_key_strokes(['1', '2', '3'])\n ['123']\n\n Args:\n value(int|str|list)\n\n Returns:\n A list of string.\n \"\"\"\n result = ''\n if isinstance(value, Integral):\n value = str(value)\n\n for v in value:\n if isinstance(v, Keys):\n result += v.value\n elif isinstance(v, Integral):\n result += str(v)\n else:\n result += v\n return [result]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef value_to_single_key_strokes(value):\n result = []\n if isinstance(value, Integral):\n value = str(value)\n\n for v in value:\n if isinstance(v, Keys):\n result.append(v.value)\n elif isinstance(v, Integral):\n result.append(str(v))\n else:\n result.append(v)\n return result", "response": "Convert value to a list of key strokes."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nimplementing the check_unused_args in superclass.", "response": "def check_unused_args(self, used_args, args, kwargs):\n \"\"\"Implement the check_unused_args in superclass.\"\"\"\n for k, v in kwargs.items():\n if k in used_args:\n self._used_kwargs.update({k: v})\n else:\n self._unused_kwargs.update({k: v})"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclearing used and unused dicts before each formatting.", "response": "def vformat(self, format_string, args, kwargs):\n \"\"\"Clear used and unused dicts before each formatting.\"\"\"\n self._used_kwargs = {}\n self._unused_kwargs = {}\n return super(MemorizeFormatter, self).vformat(format_string, args, kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nformat a string by a map", "response": "def format_map(self, format_string, mapping):\n \"\"\"format a string by a map\n\n Args:\n format_string(str): A format string\n mapping(dict): A map to format the string\n\n Returns:\n A formatted string.\n\n Raises:\n KeyError: if key is not provided by the given map.\n \"\"\"\n return self.vformat(format_string, args=None, kwargs=mapping)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find_exception_by_code(code):\n errorName = None\n for error in WebDriverError:\n if error.value.code == code:\n errorName = error\n break\n return errorName", "response": "Find error name by error code."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef execute(self, command, data={}):\n method, uri = command\n try:\n path = self._formatter.format_map(uri, data)\n body = self._formatter.get_unused_kwargs()\n url = \"{0}{1}\".format(self._url, path)\n return self._request(method, url, body)\n except KeyError as err:\n LOGGER.debug(\n 'Endpoint {0} is missing argument {1}'.format(uri, err))\n raise", "response": "Execute a command on the remote server and return the json body."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _execute(self, command, data=None, unpack=True):\n if not data:\n data = {}\n data.setdefault('element_id', self.element_id)\n return self._driver._execute(command, data, unpack)", "response": "Private method to execute command with data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef element(self, using, value):\n return self._execute(Command.FIND_CHILD_ELEMENT, {\n 'using': using,\n 'value': value\n })", "response": "Find an element in the current element."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef element_or_none(self, using, value):\n try:\n return self._execute(Command.FIND_CHILD_ELEMENT, {\n 'using': using,\n 'value': value\n })\n except:\n return None", "response": "Check if an element in the current element or return None if it does not exist."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef elements(self, using, value):\n return self._execute(Command.FIND_CHILD_ELEMENTS, {\n 'using': using,\n 'value': value\n })", "response": "Find elements in the current element."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndeprecate use element. touch move to", "response": "def move_to(self, x=0, y=0):\n \"\"\"Deprecated use element.touch('drag', { toX, toY, duration(s) }) instead.\n Move the mouse by an offset of the specificed element.\n\n Support:\n Android\n\n Args:\n x(float): X offset to move to, relative to the\n top-left corner of the element.\n y(float): Y offset to move to, relative to the\n top-left corner of the element.\n\n Returns:\n WebElement object.\n \"\"\"\n self._driver.move_to(self, x, y)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nflicks on the touch screen using finger motion events.", "response": "def flick(self, x, y, speed):\n \"\"\"Deprecated use touch('drag', { fromX, fromY, toX, toY, duration(s) }) instead.\n Flick on the touch screen using finger motion events.\n This flickcommand starts at a particulat screen location.\n\n Support:\n iOS\n\n Args:\n x(float}: The x offset in pixels to flick by.\n y(float): The y offset in pixels to flick by.\n speed(float) The speed in pixels per seconds.\n\n Returns:\n WebElement object.\n \"\"\"\n self._driver.flick(self, x, y, speed)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef touch(self, name, args=None):\n if isinstance(name, list) and not isinstance(name, str):\n for obj in name:\n obj['element'] = self.element_id\n actions = name\n elif isinstance(name, str):\n if not args:\n args = {}\n args['type'] = name\n args['element'] = self.element_id\n actions = [args]\n else:\n raise TypeError('Invalid parameters.')\n self._driver._execute(Command.PERFORM_ACTIONS, {\n 'actions': actions\n })", "response": "Applies touch actions on devices. Such as tap doubleTap press pinch rotate drag."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nassert whether the target is displayed.", "response": "def is_displayed(target):\n \"\"\"Assert whether the target is displayed\n\n Args:\n target(WebElement): WebElement Object.\n\n Returns:\n Return True if the element is displayed or return False otherwise.\n \"\"\"\n is_displayed = getattr(target, 'is_displayed', None)\n if not is_displayed or not callable(is_displayed):\n raise TypeError('Target has no attribute \\'is_displayed\\' or not callable')\n if not is_displayed():\n raise WebDriverException('element not visible')"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nplugs in to Virtual USB Bus", "response": "def PlugIn(self):\n \"\"\"Take next available controller id and plug in to Virtual USB Bus\"\"\"\n ids = self.available_ids()\n if len(ids) == 0:\n raise MaxInputsReachedError('Max Inputs Reached')\n\n self.id = ids[0]\n\n _xinput.PlugIn(self.id)\n while self.id in self.available_ids():\n pass"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nunplug the controller from Virtual USB Bus and free up ID.", "response": "def UnPlug(self, force=False):\n \"\"\"Unplug controller from Virtual USB Bus and free up ID\"\"\"\n if force:\n _xinput.UnPlugForce(c_uint(self.id))\n else:\n _xinput.UnPlug(c_uint(self.id))\n while self.id not in self.available_ids():\n if self.id == 0:\n break"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nset a value on the controller.", "response": "def set_value(self, control, value=None):\n \"\"\"Set a value on the controller\n If percent is True all controls will accept a value between -1.0 and 1.0\n\n If not then:\n Triggers are 0 to 255\n Axis are -32768 to 32767\n\n Control List:\n AxisLx , Left Stick X-Axis\n AxisLy , Left Stick Y-Axis\n AxisRx , Right Stick X-Axis\n AxisRy , Right Stick Y-Axis\n BtnBack , Menu/Back Button\n BtnStart , Start Button\n BtnA , A Button\n BtnB , B Button\n BtnX , X Button\n BtnY , Y Button\n BtnThumbL , Left Thumbstick Click\n BtnThumbR , Right Thumbstick Click\n BtnShoulderL , Left Shoulder Button\n BtnShoulderR , Right Shoulder Button\n Dpad , Set Dpad Value (0 = Off, Use DPAD_### Constants)\n TriggerL , Left Trigger\n TriggerR , Right Trigger\n\n \"\"\"\n func = getattr(_xinput, 'Set' + control)\n\n if 'Axis' in control:\n target_type = c_short\n\n if self.percent:\n target_value = int(32767 * value)\n else:\n target_value = value\n elif 'Btn' in control:\n target_type = c_bool\n target_value = bool(value)\n elif 'Trigger' in control:\n target_type = c_byte\n\n if self.percent:\n target_value = int(255 * value)\n else:\n target_value = value\n elif 'Dpad' in control:\n target_type = c_int\n target_value = int(value)\n\n func(c_uint(self.id), target_type(target_value))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntest the functionality of the rController object", "response": "def main():\n \"\"\"Test the functionality of the rController object\"\"\"\n import time\n\n print('Testing controller in position 1:')\n print('Running 3 x 3 seconds tests')\n\n # Initialise Controller\n con = rController(1)\n\n # Loop printing controller state and buttons held\n for i in range(3):\n print('Waiting...')\n time.sleep(2.5)\n print('State: ', con.gamepad)\n print('Buttons: ', con.buttons)\n time.sleep(0.5)\n\n print('Done!')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gamepad(self):\n state = _xinput_state()\n _xinput.XInputGetState(self.ControllerID - 1, pointer(state))\n self.dwPacketNumber = state.dwPacketNumber\n\n return state.XINPUT_GAMEPAD", "response": "Returns the current gamepad state."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of buttons currently pressed", "response": "def buttons(self):\n \"\"\"Returns a list of buttons currently pressed\"\"\"\n return [name for name, value in rController._buttons.items()\n if self.gamepad.wButtons & value == value]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndecoding a 7 - bit ASCII header value into its actual value.", "response": "def maybe_decode_header(header):\n \"\"\"\n Decodes an encoded 7-bit ASCII header value into it's actual value.\n \"\"\"\n value, encoding = decode_header(header)[0]\n if encoding:\n return value.decode(encoding)\n else:\n return value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nimporting all available previews classes.", "response": "def autodiscover():\n \"\"\"\n Imports all available previews classes.\n \"\"\"\n from django.conf import settings\n for application in settings.INSTALLED_APPS:\n module = import_module(application)\n\n if module_has_submodule(module, 'emails'):\n emails = import_module('%s.emails' % application)\n try:\n import_module('%s.emails.previews' % application)\n except ImportError:\n # Only raise the exception if this module contains previews and\n # there was a problem importing them. (An emails module that\n # does not contain previews is not an error.)\n if module_has_submodule(emails, 'previews'):\n raise"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nregisters a new preview with the index.", "response": "def register(self, cls):\n \"\"\"\n Adds a preview to the index.\n \"\"\"\n preview = cls(site=self)\n logger.debug('Registering %r with %r', preview, self)\n index = self.__previews.setdefault(preview.module, {})\n index[cls.__name__] = preview"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a detail view for the given module and preview.", "response": "def detail_view(self, request, module, preview):\n \"\"\"\n Looks up a preview in the index, returning a detail view response.\n \"\"\"\n try:\n preview = self.__previews[module][preview]\n except KeyError:\n raise Http404 # The provided module/preview does not exist in the index.\n return preview.detail_view(request)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the URL to access this preview.", "response": "def url(self):\n \"\"\"\n The URL to access this preview.\n \"\"\"\n return reverse('%s:detail' % URL_NAMESPACE, kwargs={\n 'module': self.module,\n 'preview': type(self).__name__,\n })"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrender the message view to a response.", "response": "def detail_view(self, request):\n \"\"\"\n Renders the message view to a response.\n \"\"\"\n context = {\n 'preview': self,\n }\n\n kwargs = {}\n if self.form_class:\n if request.GET:\n form = self.form_class(data=request.GET)\n else:\n form = self.form_class()\n\n context['form'] = form\n if not form.is_bound or not form.is_valid():\n return render(request, 'mailviews/previews/detail.html', context)\n\n kwargs.update(form.get_message_view_kwargs())\n\n message_view = self.get_message_view(request, **kwargs)\n\n message = message_view.render_to_message()\n raw = message.message()\n headers = OrderedDict((header, maybe_decode_header(raw[header])) for header in self.headers)\n\n context.update({\n 'message': message,\n 'subject': message.subject,\n 'body': message.body,\n 'headers': headers,\n 'raw': raw.as_string(),\n })\n\n alternatives = getattr(message, 'alternatives', [])\n try:\n html = next(alternative[0] for alternative in alternatives\n if alternative[1] == 'text/html')\n context.update({\n 'html': html,\n 'escaped_html': b64encode(html.encode('utf-8')),\n })\n except StopIteration:\n pass\n\n return render(request, self.template_name, context)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef split_docstring(value):\n docstring = textwrap.dedent(getattr(value, '__doc__', ''))\n if not docstring:\n return None\n\n pieces = docstring.strip().split('\\n\\n', 1)\n try:\n body = pieces[1]\n except IndexError:\n body = None\n\n return Docstring(pieces[0], body)", "response": "Splits the docstring of the given value into its summary and body."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrendering and returns an unsent message with the provided context.", "response": "def render_to_message(self, extra_context=None, **kwargs):\n \"\"\"\n Renders and returns an unsent message with the provided context.\n\n Any extra keyword arguments passed will be passed through as keyword\n arguments to the message constructor.\n\n :param extra_context: Any additional context to use when rendering the\n templated content.\n :type extra_context: :class:`dict`\n :returns: A message instance.\n :rtype: :attr:`.message_class`\n \"\"\"\n if extra_context is None:\n extra_context = {}\n\n # Ensure our custom headers are added to the underlying message class.\n kwargs.setdefault('headers', {}).update(self.headers)\n\n context = self.get_context_data(**extra_context)\n return self.message_class(\n subject=self.render_subject(context),\n body=self.render_body(context),\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrender and sends an email message.", "response": "def send(self, extra_context=None, **kwargs):\n \"\"\"\n Renders and sends an email message.\n\n All keyword arguments other than ``extra_context`` are passed through\n as keyword arguments when constructing a new :attr:`message_class`\n instance for this message.\n\n This method exists primarily for convenience, and the proper\n rendering of your message should not depend on the behavior of this\n method. To alter how a message is created, override\n :meth:``render_to_message`` instead, since that should always be\n called, even if a message is not sent.\n\n :param extra_context: Any additional context data that will be used\n when rendering this message.\n :type extra_context: :class:`dict`\n \"\"\"\n message = self.render_to_message(extra_context=extra_context, **kwargs)\n return message.send()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef render_subject(self, context):\n rendered = self.subject_template.render(unescape(context))\n return rendered.strip()", "response": "Renders the subject for the given context."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrender and returns an unsent message with the given context.", "response": "def render_to_message(self, extra_context=None, *args, **kwargs):\n \"\"\"\n Renders and returns an unsent message with the given context.\n\n Any extra keyword arguments passed will be passed through as keyword\n arguments to the message constructor.\n\n :param extra_context: Any additional context to use when rendering\n templated content.\n :type extra_context: :class:`dict`\n :returns: A message instance.\n :rtype: :attr:`.message_class`\n \"\"\"\n message = super(TemplatedHTMLEmailMessageView, self)\\\n .render_to_message(extra_context, *args, **kwargs)\n\n if extra_context is None:\n extra_context = {}\n\n context = self.get_context_data(**extra_context)\n content = self.render_html_body(context)\n message.attach_alternative(content, mimetype='text/html')\n return message"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef timestamp(_, dt):\n 'get microseconds since 2000-01-01 00:00'\n # see http://stackoverflow.com/questions/2956886/\n dt = util.to_utc(dt)\n unix_timestamp = calendar.timegm(dt.timetuple())\n # timetuple doesn't maintain microseconds\n # see http://stackoverflow.com/a/14369386/519015\n val = ((unix_timestamp - psql_epoch) * 1000000) + dt.microsecond\n return ('iq', (8, val))", "response": "get microseconds since 2000 - 01 - 01 00 : 00"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a string that represents a numeric value in the ISO - 8601 format.", "response": "def numeric(_, n):\n \"\"\"\n NBASE = 1000\n ndigits = total number of base-NBASE digits\n weight = base-NBASE weight of first digit\n sign = 0x0000 if positive, 0x4000 if negative, 0xC000 if nan\n dscale = decimal digits after decimal place\n \"\"\"\n try:\n nt = n.as_tuple()\n except AttributeError:\n raise TypeError('numeric field requires Decimal value (got %r)' % n)\n digits = []\n if isinstance(nt.exponent, str):\n # NaN, Inf, -Inf\n ndigits = 0\n weight = 0\n sign = 0xC000\n dscale = 0\n else:\n decdigits = list(reversed(nt.digits + (nt.exponent % 4) * (0,)))\n weight = 0\n while decdigits:\n if any(decdigits[:4]):\n break\n weight += 1\n del decdigits[:4]\n while decdigits:\n digits.insert(0, ndig(decdigits[:4]))\n del decdigits[:4]\n ndigits = len(digits)\n weight += nt.exponent // 4 + ndigits - 1\n sign = nt.sign * 0x4000\n dscale = -min(0, nt.exponent)\n data = [ndigits, weight, sign, dscale] + digits\n return ('ihhHH%dH' % ndigits, [2 * len(data)] + data)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_default_subparser(self, name, args=None):\n subparser_found = False\n for arg in sys.argv[1:]:\n if arg in ['-h', '--help']: # global help if no subparser\n break\n else:\n for x in self._subparsers._actions:\n if not isinstance(x, argparse._SubParsersAction):\n continue\n for sp_name in x._name_parser_map.keys():\n if sp_name in sys.argv[1:]:\n subparser_found = True\n if not subparser_found:\n # insert default in first position, this implies no\n # global options without a sub_parsers specified\n if args is None:\n sys.argv.insert(1, name)\n else:\n args.insert(0, name)", "response": "set the default subparser for this command line"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a record to JSON - encode through redis.", "response": "def format(self, record):\n \"\"\"\n JSON-encode a record for serializing through redis.\n\n Convert date to iso format, and stringify any exceptions.\n \"\"\"\n data = record._raw.copy()\n\n # serialize the datetime date as utc string\n data['time'] = data['time'].isoformat()\n\n # stringify exception data\n if data.get('traceback'):\n data['traceback'] = self.formatException(data['traceback'])\n\n return json.dumps(data)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\npublish a record to redis logging channel", "response": "def emit(self, record):\n \"\"\"\n Publish record to redis logging channel\n \"\"\"\n try:\n self.redis_client.publish(self.channel, self.format(record))\n except redis.RedisError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef emit(self, record):\n try:\n if self.max_messages:\n p = self.redis_client.pipeline()\n p.rpush(self.key, self.format(record))\n p.ltrim(self.key, -self.max_messages, -1)\n p.execute()\n else:\n self.redis_client.rpush(self.key, self.format(record))\n except redis.RedisError:\n pass", "response": "Publish a record to the redis logging list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef require_template_debug(f):\n def _(*args, **kwargs):\n TEMPLATE_DEBUG = getattr(settings, 'TEMPLATE_DEBUG', False)\n return f(*args, **kwargs) if TEMPLATE_DEBUG else ''\n return _", "response": "Decorator that ensures that the TEMPLATE_DEBUG setting is set to True."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndisplays the details of the variable attributes in the terminal.", "response": "def _display_details(var_data):\n \"\"\"\n Given a dictionary of variable attribute data from get_details display the\n data in the terminal.\n \"\"\"\n meta_keys = (key for key in list(var_data.keys())\n if key.startswith('META_'))\n for key in meta_keys:\n display_key = key[5:].capitalize()\n pprint('{0}: {1}'.format(display_key, var_data.pop(key)))\n pprint(var_data)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_trace(context):\n try:\n import ipdb as pdb\n except ImportError:\n import pdb\n print(\"For best results, pip install ipdb.\")\n print(\"Variables that are available in the current context:\")\n render = lambda s: template.Template(s).render(context)\n availables = get_variables(context)\n pprint(availables)\n print('Type `availables` to show this list.')\n print('Type to access one.')\n print('Use render(\"template string\") to test template rendering')\n # Cram context variables into the local scope\n for var in availables:\n locals()[var] = context[var]\n pdb.set_trace()\n return ''", "response": "Start a pdb set_trace inside of the template with the given context. Uses ipdb if available."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstarts a pydevd settrace on the next page", "response": "def pydevd(context):\n \"\"\"\n Start a pydev settrace\n \"\"\"\n global pdevd_not_available\n if pdevd_not_available:\n return ''\n try:\n import pydevd\n except ImportError:\n pdevd_not_available = True\n return ''\n render = lambda s: template.Template(s).render(context)\n availables = get_variables(context)\n for var in availables:\n locals()[var] = context[var]\n #catch the case where no client is listening\n try:\n pydevd.settrace()\n except socket.error:\n pdevd_not_available = True\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _flatten(iterable):\n for i in iterable:\n if isinstance(i, Iterable) and not isinstance(i, string_types):\n for sub_i in _flatten(i):\n yield sub_i\n else:\n yield i", "response": "Given an iterable with nested iterables generate a flat iterable"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a variable in the context obtain the attributes and callables and their values where possible and add them in the var_data dictionary.", "response": "def get_details(var):\n \"\"\"\n Given a variable inside the context, obtain the attributes/callables,\n their values where possible, and the module name and class name if possible\n \"\"\"\n var_data = {}\n # Obtain module and class details if available and add them in\n module = getattr(var, '__module__', '')\n kls = getattr(getattr(var, '__class__', ''), '__name__', '')\n if module:\n var_data['META_module_name'] = module\n if kls:\n var_data['META_class_name'] = kls\n for attr in get_attributes(var):\n value = _get_detail_value(var, attr)\n if value is not None:\n var_data[attr] = value\n return var_data"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_detail_value(var, attr):\n value = getattr(var, attr)\n # Rename common Django class names\n kls = getattr(getattr(value, '__class__', ''), '__name__', '')\n if kls in ('ManyRelatedManager', 'RelatedManager', 'EmptyManager'):\n return kls\n if callable(value):\n return 'routine'\n return value", "response": "Returns the value of a variable and one of its attributes that are available inside of\n ."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngive a varaible return the list of attributes that are available inside the template", "response": "def get_attributes(var):\n \"\"\"\n Given a varaible, return the list of attributes that are available inside\n of a template\n \"\"\"\n is_valid = partial(is_valid_in_template, var)\n return list(filter(is_valid, dir(var)))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngiving a variable and an attribute returns True or False accordingly", "response": "def is_valid_in_template(var, attr):\n \"\"\"\n Given a variable and one of its attributes, determine if the attribute is\n accessible inside of a Django template and return True or False accordingly\n \"\"\"\n # Remove private variables or methods\n if attr.startswith('_'):\n return False\n # Remove any attributes that raise an acception when read\n try:\n value = getattr(var, attr)\n except:\n return False\n if isroutine(value):\n # Remove any routines that are flagged with 'alters_data'\n if getattr(value, 'alters_data', False):\n return False\n else:\n # Remove any routines that require arguments\n try:\n argspec = getargspec(value)\n num_args = len(argspec.args) if argspec.args else 0\n num_defaults = len(argspec.defaults) if argspec.defaults else 0\n if num_args - num_defaults > 1:\n return False\n except TypeError:\n # C extension callables are routines, but getargspec fails with\n # a TypeError when these are passed.\n pass\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nincrementing version number string version.", "response": "def version_bump(self, version, type=\"bug\"):\n \"\"\"\n Increment version number string 'version'.\n \n Type can be one of: major, minor, or bug \n \"\"\"\n parsed_version = LooseVersion(version).version\n total_components = max(3, len(parsed_version))\n \n bits = []\n for bit in parsed_version:\n try:\n bit = int(bit)\n except ValueError:\n continue\n \n bits.append(bit)\n \n indexes = {\n \"major\": 0,\n \"minor\": 1,\n \"bug\": 2,\n }\n \n bits += [0] * (3 - len(bits)) # pad to 3 digits\n \n # Increment the version\n bits[indexes[type]] += 1\n \n # Set the subsequent digits to 0\n for i in range(indexes[type] + 1, 3):\n bits[i] = 0\n \n return \".\".join(map(str, bits))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef determine_paths(self, package_name=None, create_package_dir=False, dry_run=False):\n \n # Give preference to the environment variable here as it will not \n # derefrence sym links\n self.project_dir = Path(os.getenv('PWD') or os.getcwd())\n \n # Try and work out the project name\n distribution = self.get_distribution()\n if distribution:\n # Get name from setup.py\n self.project_name = distribution.get_name()\n else:\n # ...failing that, use the current directory name\n self.project_name = self.project_dir.name\n \n # Descend into the 'src' directory to find the package \n # if necessary\n if os.path.isdir(self.project_dir / \"src\"):\n package_search_dir = self.project_dir / \"src\"\n else:\n package_search_dir = self.project_dir\n\n created_package_dir = False\n if not package_name:\n # Lets try and work out the package_name from the project_name\n package_name = self.project_name.replace(\"-\", \"_\")\n \n # Now do some fuzzy matching\n def get_matches(name):\n possibles = [n for n in os.listdir(package_search_dir) if os.path.isdir(package_search_dir / n)]\n return difflib.get_close_matches(name, possibles, n=1, cutoff=0.8)\n \n close = get_matches(package_name)\n \n # If no matches, try removing the first part of the package name\n # (e.g. django-guardian becomes guardian)\n if not close and \"_\" in package_name:\n short_package_name = \"_\".join(package_name.split(\"_\")[1:])\n close = get_matches(short_package_name)\n \n if not close:\n if create_package_dir:\n package_dir = package_search_dir / package_name\n # Gets set to true even during dry run\n created_package_dir = True\n if not dry_run:\n print(\"Creating package directory at %s\" % package_dir)\n os.mkdir(package_dir)\n else:\n print(\"Would have created package directory at %s\" % package_dir)\n else:\n raise CommandError(\"Could not guess the package name. Specify it using --name.\")\n else:\n package_name = close[0]\n \n self.package_name = package_name\n self.package_dir = package_search_dir / package_name\n\n if not os.path.exists(self.package_dir) and not created_package_dir:\n raise CommandError(\"Package directory did not exist at %s. Perhaps specify it using --name\" % self.package_dir)", "response": "Determine paths automatically and a little intelligently."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef check_integrity(sakefile, settings):\n sprint = settings[\"sprint\"]\n error = settings[\"error\"]\n sprint(\"Call to check_integrity issued\", level=\"verbose\")\n if not sakefile:\n error(\"Sakefile is empty\")\n return False\n # checking for duplicate targets\n if len(sakefile.keys()) != len(set(sakefile.keys())):\n error(\"Sakefile contains duplicate targets\")\n return False\n for target in sakefile:\n if target == \"all\":\n if not check_target_integrity(target, sakefile[\"all\"], all=True):\n error(\"Failed to accept target 'all'\")\n return False\n continue\n if \"formula\" not in sakefile[target]:\n if not check_target_integrity(target, sakefile[target],\n meta=True):\n errmes = \"Failed to accept meta-target '{}'\".format(target)\n error(errmes)\n return False\n for atom_target in sakefile[target]:\n if atom_target == \"help\":\n continue\n if not check_target_integrity(atom_target,\n sakefile[target][atom_target],\n parent=target):\n errmes = \"Failed to accept target '{}'\\n\".format(\n atom_target)\n error(errmes)\n return False\n continue\n if not check_target_integrity(target, sakefile[target]):\n errmes = \"Failed to accept target '{}'\\n\".format(target)\n error(errmes)\n return False\n return True", "response": "Checks the format of the Sakefile to ensure it conforms to the specification of the current language."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks the integrity of a specific target.", "response": "def check_target_integrity(key, values, meta=False, all=False, parent=None):\n \"\"\"\n Checks the integrity of a specific target. Gets called\n multiple times from check_integrity()\n\n Args:\n The target name\n The dictionary values of that target\n A boolean representing whether it is a meta-target\n A boolean representing whether it is the \"all\" target\n A string representing name of parent (default None)\n\n Returns:\n True is the target is conformant\n False if not\n \"\"\"\n\n # logic to audit \"all\" target\n if all:\n if not values:\n print(\"Warning: target 'all' is empty\")\n # will check if it has unrecognized target later\n return True\n\n errmes = \"target '{}' is not allowed to be missing a help message\\n\"\n\n # logic to audit a meta-target\n if meta:\n # check if help is missing\n if \"help\" not in values:\n sys.stderr.write(errmes.format(key))\n return False\n # checking if empty\n if len(values.keys()) == 1:\n sys.stderr.write(\"Meta-target '{}' is empty\\n\".format(key))\n return False\n return True\n\n # logic to audit any other target\n expected_fields = [\"dependencies\", \"help\", \"output\", \"formula\"]\n expected_fields = set(expected_fields)\n try:\n our_keys_set = set(values.keys())\n except:\n sys.stderr.write(\"Error processing target '{}'\\n\".format(key))\n sys.stderr.write(\"Are you sure '{}' is a meta-target?\\n\".format(\n parent))\n sys.stderr.write(\"If it's not, it's missing a formula\\n\")\n return False\n ignored_fields = set([field for field in our_keys_set\\\n if field.strip().startswith(\"(ignore)\")])\n difference = our_keys_set - expected_fields - ignored_fields\n if difference:\n print(\"The following fields were not recognized and will be ignored\")\n for item in difference:\n print(\" - \" + item)\n if \"help\" not in values:\n sys.stderr.write(errmes.format(key))\n return False\n # can't be missing formula either\n if \"formula\" not in values:\n sys.stderr.write(\"Target '{}' is missing formula\\n\".format(key))\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck if the current version of sake is in the from_store.", "response": "def check_shastore_version(from_store, settings):\n \"\"\"\n This function gives us the option to emit errors or warnings\n after sake upgrades\n \"\"\"\n sprint = settings[\"sprint\"]\n error = settings[\"error\"]\n\n sprint(\"checking .shastore version for potential incompatibilities\",\n level=\"verbose\")\n if not from_store or 'sake version' not in from_store:\n errmes = [\"Since you've used this project last, a new version of \",\n \"sake was installed that introduced backwards incompatible\",\n \" changes. Run 'sake clean', and rebuild before continuing\\n\"]\n errmes = \" \".join(errmes)\n error(errmes)\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn sha1 hash of the file supplied as an argument", "response": "def get_sha(a_file, settings=None):\n \"\"\"\n Returns sha1 hash of the file supplied as an argument\n \"\"\"\n if settings:\n error = settings[\"error\"]\n else:\n error = ERROR_FN\n try:\n BLOCKSIZE = 65536\n hasher = hashlib.sha1()\n with io.open(a_file, \"rb\") as fh:\n buf = fh.read(BLOCKSIZE)\n while len(buf) > 0:\n hasher.update(buf)\n buf = fh.read(BLOCKSIZE)\n the_hash = hasher.hexdigest()\n except IOError:\n errmes = \"File '{}' could not be read! Exiting!\".format(a_file)\n error(errmes)\n sys.exit(1)\n except:\n errmes = \"Unspecified error returning sha1 hash. Exiting!\"\n error(errmes)\n sys.exit(1)\n return the_hash"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef write_shas_to_shastore(sha_dict):\n if sys.version_info[0] < 3:\n fn_open = open\n else:\n fn_open = io.open\n with fn_open(\".shastore\", \"w\") as fh:\n fh.write(\"---\\n\")\n fh.write('sake version: {}\\n'.format(constants.VERSION))\n if sha_dict:\n fh.write(yaml.dump(sha_dict))\n fh.write(\"...\")", "response": "Writes a sha1 dictionary stored in memory to\nAttributeNames the. shastore file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef take_shas_of_all_files(G, settings):\n global ERROR_FN\n sprint = settings[\"sprint\"]\n error = settings[\"error\"]\n ERROR_FN = error\n sha_dict = {}\n all_files = []\n for target in G.nodes(data=True):\n sprint(\"About to take shas of files in target '{}'\".format(target[0]),\n level=\"verbose\")\n if 'dependencies' in target[1]:\n sprint(\"It has dependencies\", level=\"verbose\")\n deplist = []\n for dep in target[1]['dependencies']:\n glist = glob.glob(dep)\n if glist:\n for oneglob in glist:\n deplist.append(oneglob)\n else:\n deplist.append(dep)\n target[1]['dependencies'] = list(deplist)\n for dep in target[1]['dependencies']:\n sprint(\" - {}\".format(dep), level=\"verbose\")\n all_files.append(dep)\n if 'output' in target[1]:\n sprint(\"It has outputs\", level=\"verbose\")\n for out in acts.get_all_outputs(target[1]):\n sprint(\" - {}\".format(out), level=\"verbose\")\n all_files.append(out)\n if len(all_files):\n sha_dict['files'] = {}\n # check if files exist and de-dupe\n extant_files = []\n for item in all_files:\n if item not in extant_files and os.path.isfile(item):\n extant_files.append(item)\n pool = Pool()\n results = pool.map(get_sha, extant_files)\n pool.close()\n pool.join()\n for fn, sha in zip(extant_files, results):\n sha_dict['files'][fn] = {'sha': sha}\n return sha_dict\n sprint(\"No dependencies\", level=\"verbose\")", "response": "Takes sha1 hash of all dependencies and outputs of all targets and returns a dictionary where the keys are the filenames and the values are the sha1 hash of the files and the values are the sha1 hash of the files and the values are the sha1 hash of the files and the sha1 hash of the files."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef needs_to_run(G, target, in_mem_shas, from_store, settings):\n force = settings[\"force\"]\n sprint = settings[\"sprint\"]\n\n if(force):\n sprint(\"Target rebuild is being forced so {} needs to run\".format(target),\n level=\"verbose\")\n return True\n node_dict = get_the_node_dict(G, target)\n if 'output' in node_dict:\n for output in acts.get_all_outputs(node_dict):\n if not os.path.isfile(output):\n outstr = \"Output file '{}' is missing so it needs to run\"\n sprint(outstr.format(output), level=\"verbose\")\n return True\n if 'dependencies' not in node_dict:\n # if it has no dependencies, it always needs to run\n sprint(\"Target {} has no dependencies and needs to run\".format(target),\n level=\"verbose\")\n return True\n for dep in node_dict['dependencies']:\n # because the shas are updated after all targets build,\n # its possible that the dependency's sha doesn't exist\n # in the current \"in_mem\" dictionary. If this is the case,\n # then the target needs to run\n if ('files' in in_mem_shas and dep not in in_mem_shas['files'] or\n 'files' not in in_mem_shas):\n outstr = \"Dep '{}' doesn't exist in memory so it needs to run\"\n sprint(outstr.format(dep), level=\"verbose\")\n return True\n now_sha = in_mem_shas['files'][dep]['sha']\n if ('files' in from_store and dep not in from_store['files'] or\n 'files' not in from_store):\n outst = \"Dep '{}' doesn't exist in shastore so it needs to run\"\n sprint(outst.format(dep), level=\"verbose\")\n return True\n old_sha = from_store['files'][dep]['sha']\n if now_sha != old_sha:\n outstr = \"There's a mismatch for dep {} so it needs to run\"\n sprint(outstr.format(dep), level=\"verbose\")\n return True\n sprint(\"Target '{}' doesn't need to run\".format(target), level=\"verbose\")\n return False", "response": "Determines if a target needs to be run based on the current state of the target."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run_commands(commands, settings):\n sprint = settings[\"sprint\"]\n quiet = settings[\"quiet\"]\n error = settings[\"error\"]\n enhanced_errors = True\n the_shell = None\n if settings[\"no_enhanced_errors\"]:\n enhanced_errors = False\n if \"shell\" in settings:\n the_shell = settings[\"shell\"]\n windows_p = sys.platform == \"win32\"\n\n STDOUT = None\n STDERR = None\n if quiet:\n STDOUT = PIPE\n STDERR = PIPE\n\n commands = commands.rstrip()\n sprint(\"About to run commands '{}'\".format(commands), level=\"verbose\")\n if not quiet:\n sprint(commands)\n\n if the_shell:\n tmp = shlex.split(the_shell)\n the_shell = tmp[0]\n tmp = tmp[1:]\n if enhanced_errors and not windows_p:\n tmp.append(\"-e\")\n tmp.append(commands)\n commands = tmp\n else:\n if enhanced_errors and not windows_p:\n commands = [\"-e\", commands]\n\n p = Popen(commands, shell=True, stdout=STDOUT, stderr=STDERR,\n executable=the_shell)\n out, err = p.communicate()\n if p.returncode:\n if quiet:\n error(err.decode(locale.getpreferredencoding()))\n error(\"Command failed to run\")\n sys.exit(1)", "response": "Runs the commands supplied as an argument\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrap function that sends to commands in a target s formula to run_commands", "response": "def run_the_target(G, target, settings):\n \"\"\"\n Wrapper function that sends to commands in a target's 'formula'\n to run_commands()\n\n Args:\n The graph we are going to build\n The target to run\n The settings dictionary\n \"\"\"\n sprint = settings[\"sprint\"]\n sprint(\"Running target {}\".format(target))\n the_formula = get_the_node_dict(G, target)[\"formula\"]\n run_commands(the_formula, settings)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of nodes that are the direct ancestors of all of the nodes given as an argument.", "response": "def get_direct_ancestors(G, list_of_nodes):\n \"\"\"\n Returns a list of nodes that are the parents\n from all of the nodes given as an argument.\n This is for use in the parallel topo sort\n \"\"\"\n parents = []\n for item in list_of_nodes:\n anc = G.predecessors(item)\n for one in anc:\n parents.append(one)\n return parents"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of all sinks in a topo - sort graph.", "response": "def get_sinks(G):\n \"\"\"\n A sink is a node with no children.\n This means that this is the end of the line,\n and it should be run last in topo sort. This\n returns a list of all sinks in a graph\n \"\"\"\n sinks = []\n for node in G:\n if not len(list(G.successors(node))):\n sinks.append(node)\n return sinks"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of lists representing all the nodes that have no dependency relationship between any nodes in a layer.", "response": "def get_levels(G):\n \"\"\"\n For the parallel topo sort to work, the targets have\n to be executed in layers such that there is no\n dependency relationship between any nodes in a layer.\n What is returned is a list of lists representing all\n the layers, or levels\n \"\"\"\n levels = []\n ends = get_sinks(G)\n levels.append(ends)\n while get_direct_ancestors(G, ends):\n ends = get_direct_ancestors(G, ends)\n levels.append(ends)\n levels.reverse()\n return levels"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving redundant entries from the list of levels.", "response": "def remove_redundancies(levels):\n \"\"\"\n There are repeats in the output from get_levels(). We\n want only the earliest occurrence (after it's reversed)\n \"\"\"\n seen = []\n final = []\n for line in levels:\n new_line = []\n for item in line:\n if item not in seen:\n seen.append(item)\n new_line.append(item)\n final.append(new_line)\n return final"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parallel_run_these(G, list_of_targets, in_mem_shas, from_store,\n settings, dont_update_shas_of):\n \"\"\"\n The parallel equivalent of \"run_this_target()\"\n It receives a list of targets to execute in parallel.\n Unlike \"run_this_target()\" it has to update the shas\n (in memory and in the store) within the function.\n This is because one of the targets may fail but many can\n succeed, and those outputs need to be updated\n\n Args:\n G\n A graph\n A list of targets that we need to build in parallel\n The dictionary containing the in-memory sha store\n The dictionary containing the contents of the .shastore file\n The settings dictionary\n A list of outputs to not update shas of\n \"\"\"\n verbose = settings[\"verbose\"]\n quiet = settings[\"quiet\"]\n error = settings[\"error\"]\n sprint = settings[\"sprint\"]\n\n if len(list_of_targets) == 1:\n target = list_of_targets[0]\n sprint(\"Going to run target '{}' serially\".format(target),\n level=\"verbose\")\n run_the_target(G, target, settings)\n node_dict = get_the_node_dict(G, target)\n if \"output\" in node_dict:\n for output in acts.get_all_outputs(node_dict):\n if output not in dont_update_shas_of:\n in_mem_shas['files'][output] = {\"sha\": get_sha(output,\n settings)}\n in_mem_shas[output] = get_sha(output, settings)\n write_shas_to_shastore(in_mem_shas)\n if \"dependencies\" in node_dict:\n for dep in acts.get_all_dependencies(node_dict):\n if dep not in dont_update_shas_of:\n in_mem_shas['files'][dep] = {\"sha\": get_sha(dep, settings)}\n write_shas_to_shastore(in_mem_shas)\n return True\n a_failure_occurred = False\n out = \"Going to run these targets '{}' in parallel\"\n sprint(out.format(\", \".join(list_of_targets)))\n info = [(target, get_the_node_dict(G, target))\n for target in list_of_targets]\n commands = [item[1]['formula'].rstrip() for item in info]\n if not quiet:\n procs = [Popen(command, shell=True) for command in commands]\n else:\n procs = [Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\n for command in commands]\n for index, process in enumerate(procs):\n if process.wait():\n error(\"Target '{}' failed!\".format(info[index][0]))\n a_failure_occurred = True\n else:\n if \"output\" in info[index][1]:\n for output in acts.get_all_outputs(info[index][1]):\n if output not in dont_update_shas_of:\n in_mem_shas['files'][output] = {\"sha\": get_sha(output,\n settings)}\n write_shas_to_shastore(in_mem_shas)\n if \"dependencies\" in info[index][1]:\n for dep in acts.get_all_dependencies(info[index][1]):\n if dep not in dont_update_shas_of:\n in_mem_shas['files'][dep] = {\"sha\": get_sha(dep, settings)}\n write_shas_to_shastore(in_mem_shas)\n if a_failure_occurred:\n error(\"A command failed to run\")\n sys.exit(1)\n return True", "response": "This function runs the target in parallel and updates the in - memory shas of the nodes in the store."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmerging the files in from_store and in_mem_shas with the shas in in_mem_shas.", "response": "def merge_from_store_and_in_mems(from_store, in_mem_shas, dont_update_shas_of):\n \"\"\"\n If we don't merge the shas from the sha store and if we build a\n subgraph, the .shastore will only contain the shas of the files\n from the subgraph and the rest of the graph will have to be\n rebuilt\n \"\"\"\n if not from_store:\n for item in dont_update_shas_of:\n if item in in_mem_shas['files']:\n del in_mem_shas['files'][item]\n return in_mem_shas\n for key in from_store['files']:\n if key not in in_mem_shas['files'] and key not in dont_update_shas_of:\n in_mem_shas['files'][key] = from_store['files'][key]\n for item in dont_update_shas_of:\n if item in in_mem_shas['files']:\n del in_mem_shas['files'][item]\n return in_mem_shas"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef build_this_graph(G, settings, dont_update_shas_of=None):\n verbose = settings[\"verbose\"]\n quiet = settings[\"quiet\"]\n force = settings[\"force\"]\n recon = settings[\"recon\"]\n parallel = settings[\"parallel\"]\n error = settings[\"error\"]\n sprint = settings[\"sprint\"]\n\n if not dont_update_shas_of:\n dont_update_shas_of = []\n sprint(\"Checking that graph is directed acyclic\", level=\"verbose\")\n if not nx.is_directed_acyclic_graph(G):\n errmes = \"Dependency resolution is impossible; \"\n errmes += \"graph is not directed and acyclic\"\n errmes += \"\\nCheck the Sakefile\\n\"\n error(errmes)\n sys.exit(1)\n sprint(\"Dependency resolution is possible\", level=\"verbose\")\n in_mem_shas = take_shas_of_all_files(G, settings)\n from_store = {}\n if not os.path.isfile(\".shastore\"):\n write_shas_to_shastore(in_mem_shas)\n in_mem_shas = {}\n in_mem_shas['files'] = {}\n with io.open(\".shastore\", \"r\") as fh:\n shas_on_disk = fh.read()\n from_store = yaml.load(shas_on_disk)\n check_shastore_version(from_store, settings)\n if not from_store:\n write_shas_to_shastore(in_mem_shas)\n in_mem_shas = {}\n in_mem_shas['files'] = {}\n with io.open(\".shastore\", \"r\") as fh:\n shas_on_disk = fh.read()\n from_store = yaml.load(shas_on_disk)\n # parallel\n if parallel:\n for line in parallel_sort(G):\n line = sorted(line)\n out = \"Checking if targets '{}' need to be run\"\n sprint(out.format(\", \".join(line)), level=\"verbose\")\n to_build = []\n for item in line:\n if needs_to_run(G, item, in_mem_shas, from_store, settings):\n to_build.append(item)\n if to_build:\n if recon:\n if len(to_build) == 1:\n out = \"Would run target '{}'\"\n sprint(out.format(to_build[0]))\n else:\n out = \"Would run targets '{}' in parallel\"\n sprint(out.format(\", \".join(to_build)))\n continue\n parallel_run_these(G, to_build, in_mem_shas, from_store,\n settings, dont_update_shas_of)\n # not parallel\n else:\n # still have to use parallel_sort to make\n # build order deterministic (by sorting targets)\n targets = []\n for line in parallel_sort(G):\n for item in sorted(line):\n targets.append(item)\n for target in targets:\n outstr = \"Checking if target '{}' needs to be run\"\n sprint(outstr.format(target), level=\"verbose\")\n if needs_to_run(G, target, in_mem_shas, from_store, settings):\n if recon:\n sprint(\"Would run target: {}\".format(target))\n continue\n run_the_target(G, target, settings)\n node_dict = get_the_node_dict(G, target)\n if \"output\" in node_dict:\n for output in acts.get_all_outputs(node_dict):\n if output not in dont_update_shas_of:\n in_mem_shas['files'][output] = {\"sha\": get_sha(output,\n settings)}\n write_shas_to_shastore(in_mem_shas)\n if \"dependencies\" in node_dict:\n for dep in acts.get_all_dependencies(node_dict):\n if dep not in dont_update_shas_of:\n in_mem_shas['files'][dep] = {\"sha\": get_sha(dep,\n settings)}\n write_shas_to_shastore(in_mem_shas)\n\n if recon:\n return 0\n in_mem_shas = take_shas_of_all_files(G, settings)\n if in_mem_shas:\n in_mem_shas = merge_from_store_and_in_mems(from_store, in_mem_shas,\n dont_update_shas_of)\n write_shas_to_shastore(in_mem_shas)\n sprint(\"Done\", color=True)\n return 0", "response": "This function builds the tree from a graph G."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the print functions that can be used to print the current language.", "response": "def get_print_functions(settings):\n \"\"\"\n This returns the appropriate print functions\n in a tuple\n The print function are:\n - sprint - for standard printing\n - warn - for warnings\n - error - for errors\n This will all be the same if color is False.\n\n The returned print functions will contain an optional parameter\n that specifies the output level (verbose or not). If not verbose,\n the print function will ignore the message.\n \"\"\"\n verbose = settings[\"verbose\"]\n # the regular print doesn't use color by default\n # (even if color is True)\n def sprint(message, level=None, color=False):\n if level==\"verbose\" and not verbose:\n return\n # for colors\n prepend = \"\"\n postfix = \"\"\n if settings[\"color\"] and color:\n prepend = \"\\033[92m\"\n postfix = \"\\033[0m\"\n print(\"{}{}{}\".format(prepend, message, postfix))\n sys.stdout.flush()\n def warn(message, level=None, color=True):\n if level==\"verbose\" and not verbose:\n return\n # for colors\n prepend = \"\"\n postfix = \"\"\n if settings[\"color\"] and color:\n prepend = \"\\033[93m\"\n postfix = \"\\033[0m\"\n print(\"{}{}{}\".format(prepend, message, postfix))\n sys.stdout.flush()\n def error(message, level=None, color=True):\n # this condition does really make any sense but w/e\n if level==\"verbose\" and not verbose:\n return\n # for colors\n prepend = \"\"\n postfix = \"\"\n if settings[\"color\"] and color:\n prepend = \"\\033[91m\"\n postfix = \"\\033[0m\"\n print(\"{}{}{}\".format(prepend, message, postfix), file=sys.stderr)\n sys.stderr.flush()\n return sprint, warn, error"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef find_standard_sakefile(settings):\n error = settings[\"error\"]\n if settings[\"customsake\"]:\n custom = settings[\"customsake\"]\n if not os.path.isfile(custom):\n error(\"Specified sakefile '{}' doesn't exist\", custom)\n sys.exit(1)\n return custom\n # no custom specified, going over defaults in order\n for name in [\"Sakefile\", \"Sakefile.yaml\", \"Sakefile.yml\"]:\n if os.path.isfile(name):\n return name\n error(\"Error: there is no Sakefile to read\")\n sys.exit(1)", "response": "Returns the filename of the appropriate sakefile"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef clean_path(a_path, force_os=None, force_start=None):\n if not force_start:\n force_start = os.curdir\n if force_os == \"windows\":\n import ntpath\n return ntpath.relpath(ntpath.normpath(a_path),\n start=force_start)\n if force_os == \"posix\":\n import posixpath\n return posixpath.relpath(posixpath.normpath(a_path),\n start=force_start)\n return os.path.relpath(os.path.normpath(a_path),\n start=force_start)", "response": "This function is used to normalize the path of an output or\n dependency and provide the path in relative form."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a prettily formatted help string for the current language of the current language.", "response": "def get_help(sakefile):\n \"\"\"\n Returns the prettily formatted help strings (for printing)\n\n Args:\n A dictionary that is the parsed Sakefile (from sake.py)\n\n NOTE:\n the list sorting in this function is required for this\n function to be deterministic\n \"\"\"\n full_string = \"You can 'sake' one of the following...\\n\\n\"\n errmes = \"target '{}' is not allowed to not have help message\\n\"\n outerlines = []\n for target in sakefile:\n if target == \"all\":\n # this doesn't have a help message\n continue\n middle_lines = []\n if \"formula\" not in sakefile[target]:\n # this means it's a meta-target\n innerstr = \"{}:\\n - {}\\n\\n\".format(escp(target),\n sakefile[target][\"help\"])\n inner = []\n for atom_target in sakefile[target]:\n if atom_target == \"help\":\n continue\n inner.append(\" {}:\\n - {}\\n\\n\".format(escp(atom_target),\n sakefile[target][atom_target][\"help\"]))\n if inner:\n innerstr += '\\n'.join(sorted(inner))\n middle_lines.append(innerstr)\n else:\n middle_lines.append(\"{}:\\n - {}\\n\\n\".format(escp(target),\n sakefile[target][\"help\"]))\n if middle_lines:\n outerlines.append('\\n'.join(sorted(middle_lines)))\n\n if outerlines:\n full_string += '\\n'.join(sorted(outerlines))\n what_clean_does = \"remove all targets' outputs and start from scratch\"\n full_string += \"\\nclean:\\n - {}\\n\\n\".format(what_clean_does)\n what_visual_does = \"output visual representation of project's dependencies\"\n full_string += \"visual:\\n - {}\\n\".format(what_visual_does)\n full_string = re.sub(\"\\n{3,}\", \"\\n\\n\", full_string)\n return full_string"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef check_for_dep_in_outputs(dep, verbose, G):\n if verbose:\n print(\"checking dep {}\".format(dep))\n ret_list = []\n for node in G.nodes(data=True):\n if \"output\" not in node[1]:\n continue\n for out in node[1]['output']:\n if fnmatch.fnmatch(out, dep):\n ret_list.append(node[0])\n break\n return ret_list", "response": "Function to help construct_graph() identify dependencies in outputs"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_ties(G):\n # we are going to make a dictionary whose keys are every dependency\n # and whose values are a list of all targets that use that dependency.\n # after making the dictionary, values whose length is above one will\n # be called \"ties\"\n ties = []\n dep_dict = {}\n for node in G.nodes(data=True):\n if 'dependencies' in node[1]:\n for item in node[1]['dependencies']:\n if item not in dep_dict:\n dep_dict[item] = []\n dep_dict[item].append(node[0])\n for item in dep_dict:\n if len(list(set(dep_dict[item]))) > 1:\n ties.append(list(set(dep_dict[item])))\n return ties", "response": "Returns a list of all ties that are used by the dependency tree."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconstructs a NetworkX graph from a dictionary of sakefile.", "response": "def construct_graph(sakefile, settings):\n \"\"\"\n Takes the sakefile dictionary and builds a NetworkX graph\n\n Args:\n A dictionary that is the parsed Sakefile (from sake.py)\n The settings dictionary\n\n Returns:\n A NetworkX graph\n \"\"\"\n verbose = settings[\"verbose\"]\n sprint = settings[\"sprint\"]\n G = nx.DiGraph()\n sprint(\"Going to construct Graph\", level=\"verbose\")\n for target in sakefile:\n if target == \"all\":\n # we don't want this node\n continue\n if \"formula\" not in sakefile[target]:\n # that means this is a meta target\n for atomtarget in sakefile[target]:\n if atomtarget == \"help\":\n continue\n sprint(\"Adding '{}'\".format(atomtarget), level=\"verbose\")\n data_dict = sakefile[target][atomtarget]\n data_dict[\"parent\"] = target\n G.add_node(atomtarget, **data_dict)\n else:\n sprint(\"Adding '{}'\".format(target), level=\"verbose\")\n G.add_node(target, **sakefile[target])\n sprint(\"Nodes are built\\nBuilding connections\", level=\"verbose\")\n for node in G.nodes(data=True):\n sprint(\"checking node {} for dependencies\".format(node[0]),\n level=\"verbose\")\n # normalize all paths in output\n for k, v in node[1].items():\n if v is None: node[1][k] = []\n if \"output\" in node[1]:\n for index, out in enumerate(node[1]['output']):\n node[1]['output'][index] = clean_path(node[1]['output'][index])\n if \"dependencies\" not in node[1]:\n continue\n sprint(\"it has dependencies\", level=\"verbose\")\n connects = []\n # normalize all paths in dependencies\n for index, dep in enumerate(node[1]['dependencies']):\n dep = os.path.normpath(dep)\n shrt = \"dependencies\"\n node[1]['dependencies'][index] = clean_path(node[1][shrt][index])\n for node in G.nodes(data=True):\n connects = []\n if \"dependencies\" not in node[1]:\n continue\n for dep in node[1]['dependencies']:\n matches = check_for_dep_in_outputs(dep, verbose, G)\n if not matches:\n continue\n for match in matches:\n sprint(\"Appending {} to matches\".format(match), level=\"verbose\")\n connects.append(match)\n if connects:\n for connect in connects:\n G.add_edge(connect, node[0])\n return G"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_all_outputs(node_dict):\n outlist = []\n for item in node_dict['output']:\n glist = glob.glob(item)\n if glist:\n for oneglob in glist:\n outlist.append(oneglob)\n else:\n outlist.append(item)\n return outlist", "response": "This function takes a node dictionary and returns a list of all outputs."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of all the dependencies of a node.", "response": "def get_all_dependencies(node_dict):\n \"\"\"\n ...............................\n \"\"\"\n deplist = []\n for item in node_dict['dependencies']:\n glist = glob.glob(item)\n if glist:\n for oneglob in glist:\n deplist.append(oneglob)\n else:\n deplist.append(item)\n return deplist"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef clean_all(G, settings):\n quiet = settings[\"quiet\"]\n recon = settings[\"recon\"]\n sprint = settings[\"sprint\"]\n error = settings[\"error\"]\n all_outputs = []\n for node in G.nodes(data=True):\n if \"output\" in node[1]:\n for item in get_all_outputs(node[1]):\n all_outputs.append(item)\n all_outputs.append(\".shastore\")\n retcode = 0\n for item in sorted(all_outputs):\n if os.path.isfile(item):\n if recon:\n sprint(\"Would remove file: {}\".format(item))\n continue\n sprint(\"Attempting to remove file '{}'\", level=\"verbose\")\n try:\n os.remove(item)\n sprint(\"Removed file\", level=\"verbose\")\n except:\n errmes = \"Error: file '{}' failed to be removed\"\n error(errmes.format(item))\n retcode = 1\n if not retcode and not recon:\n sprint(\"All clean\", color=True)\n return retcode", "response": "Removes all the output files from all targets in the graph."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef write_dot_file(G, filename):\n with io.open(filename, \"w\") as fh:\n fh.write(\"strict digraph DependencyDiagram {\\n\")\n edge_list = G.edges()\n node_list = set(G.nodes())\n if edge_list:\n for edge in sorted(edge_list):\n source, targ = edge\n node_list = node_list - set(source)\n node_list = node_list - set(targ)\n line = '\"{}\" -> \"{}\";\\n'\n fh.write(line.format(source, targ))\n # draw nodes with no links\n if node_list:\n for node in sorted(node_list):\n line = '\"{}\"\\n'.format(node)\n fh.write(line)\n fh.write(\"}\")", "response": "Writes the graph G in dot file format for graphviz visualization."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef visualize(G, settings, filename=\"dependencies\", no_graphviz=False):\n error = settings[\"error\"]\n if no_graphviz:\n write_dot_file(G, filename)\n return 0\n write_dot_file(G, \"tempdot\")\n renderer = \"svg\"\n if re.search(\"\\.jpg$\", filename, re.IGNORECASE):\n renderer = \"jpg\"\n elif re.search(\"\\.jpeg$\", filename, re.IGNORECASE):\n renderer = \"jpg\"\n elif re.search(\"\\.svg$\", filename, re.IGNORECASE):\n renderer = \"svg\"\n elif re.search(\"\\.png$\", filename, re.IGNORECASE):\n renderer = \"png\"\n elif re.search(\"\\.gif$\", filename, re.IGNORECASE):\n renderer = \"gif\"\n elif re.search(\"\\.ps$\", filename, re.IGNORECASE):\n renderer = \"ps\"\n elif re.search(\"\\.pdf$\", filename, re.IGNORECASE):\n renderer = \"pdf\"\n else:\n renderer = \"svg\"\n filename += \".svg\"\n command = \"dot -T{} tempdot -o {}\".format(renderer, filename)\n p = Popen(command, shell=True)\n p.communicate()\n if p.returncode:\n errmes = \"Either graphviz is not installed, or its not on PATH\"\n os.remove(\"tempdot\")\n error(errmes)\n sys.exit(1)\n os.remove(\"tempdot\")\n return 0", "response": "Visualize a NetworkX DiGraph into a graphviz dot file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\npreparing transcriptiondata from the transcription sources.", "response": "def _make_package(args): # pragma: no cover\n \"\"\"Prepare transcriptiondata from the transcription sources.\"\"\"\n from lingpy.sequence.sound_classes import token2class\n from lingpy.data import Model\n\n columns = ['LATEX', 'FEATURES', 'SOUND', 'IMAGE', 'COUNT', 'NOTE']\n bipa = TranscriptionSystem('bipa')\n for src, rows in args.repos.iter_sources(type='td'):\n args.log.info('TranscriptionData {0} ...'.format(src['NAME']))\n uritemplate = URITemplate(src['URITEMPLATE']) if src['URITEMPLATE'] else None\n out = [['BIPA_GRAPHEME', 'CLTS_NAME', 'GENERATED', 'EXPLICIT',\n 'GRAPHEME', 'URL'] + columns]\n graphemes = set()\n for row in rows:\n if row['GRAPHEME'] in graphemes:\n args.log.warn('skipping duplicate grapheme: {0}'.format(row['GRAPHEME']))\n continue\n graphemes.add(row['GRAPHEME'])\n if not row['BIPA']:\n bipa_sound = bipa[row['GRAPHEME']]\n explicit = ''\n else:\n bipa_sound = bipa[row['BIPA']]\n explicit = '+'\n generated = '+' if bipa_sound.generated else ''\n if is_valid_sound(bipa_sound, bipa):\n bipa_grapheme = bipa_sound.s\n bipa_name = bipa_sound.name\n else:\n bipa_grapheme, bipa_name = '', ''\n url = uritemplate.expand(**row) if uritemplate else row.get('URL', '')\n out.append(\n [bipa_grapheme, bipa_name, generated, explicit, row['GRAPHEME'],\n url] + [\n row.get(c, '') for c in columns])\n found = len([o for o in out if o[0] != ''])\n args.log.info('... {0} of {1} graphemes found ({2:.0f}%)'.format(\n found, len(out), found / len(out) * 100))\n with UnicodeWriter(\n pkg_path('transcriptiondata', '{0}.tsv'.format(src['NAME'])), delimiter='\\t'\n ) as writer:\n writer.writerows(out)\n\n count = 0\n with UnicodeWriter(pkg_path('soundclasses', 'lingpy.tsv'), delimiter='\\t') as writer:\n writer.writerow(['CLTS_NAME', 'BIPA_GRAPHEME'] + SOUNDCLASS_SYSTEMS)\n for grapheme, sound in sorted(bipa.sounds.items()):\n if not sound.alias:\n writer.writerow(\n [sound.name, grapheme] + [token2class(\n grapheme, Model(cls)) for cls in SOUNDCLASS_SYSTEMS])\n count += 1\n args.log.info('SoundClasses: {0} written to file.'.format(count))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks the consistency of a given transcription system conversino", "response": "def is_valid_sound(sound, ts):\n \"\"\"Check the consistency of a given transcription system conversino\"\"\"\n if isinstance(sound, (Marker, UnknownSound)):\n return False\n s1 = ts[sound.name]\n s2 = ts[sound.s]\n return s1.name == s2.name and s1.s == s2.s"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef resolve_sound(self, sound):\n sound = sound if isinstance(sound, Sound) else self.system[sound]\n if sound.name in self.data:\n return '//'.join([x['grapheme'] for x in self.data[sound.name]])\n raise KeyError(\":td:resolve_sound: No sound could be found.\")", "response": "Function tries to identify a sound in the data and returns the grapheme of that sound."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _norm(self, string):\n nstring = norm(string)\n if \"/\" in string:\n s, t = string.split('/')\n nstring = t\n return self.normalize(nstring)", "response": "Extended normalization : normalize by list of norm - characers split\n by character \"/\"."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nnormalizing the string according to normalization list", "response": "def normalize(self, string):\n \"\"\"Normalize the string according to normalization list\"\"\"\n return ''.join([self._normalize.get(x, x) for x in nfd(string)])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse a sound from its name", "response": "def _from_name(self, string):\n \"\"\"Parse a sound from its name\"\"\"\n components = string.split(' ')\n if frozenset(components) in self.features:\n return self.features[frozenset(components)]\n rest, sound_class = components[:-1], components[-1]\n if sound_class in ['diphthong', 'cluster']:\n if string.startswith('from ') and 'to ' in string:\n extension = {'diphthong': 'vowel', 'cluster': 'consonant'}[sound_class]\n string_ = ' '.join(string.split(' ')[1:-1])\n from_, to_ = string_.split(' to ')\n v1, v2 = frozenset(from_.split(' ') + [extension]), frozenset(\n to_.split(' ') + [extension])\n if v1 in self.features and v2 in self.features:\n s1, s2 = (self.features[v1], self.features[v2])\n if sound_class == 'diphthong':\n return Diphthong.from_sounds(s1 + s2, s1, s2, self) # noqa: F405\n else:\n return Cluster.from_sounds(s1 + s2, s1, s2, self) # noqa: F405\n else:\n # try to generate the sounds if they are not there\n s1, s2 = self._from_name(from_ + ' ' + extension), self._from_name(\n to_ + ' ' + extension)\n if not (isinstance(\n s1, UnknownSound) or isinstance(s2, UnknownSound)): # noqa: F405\n if sound_class == 'diphthong':\n return Diphthong.from_sounds( # noqa: F405\n s1 + s2, s1, s2, self)\n return Cluster.from_sounds(s1 + s2, s1, s2, self) # noqa: F405\n raise ValueError('components could not be found in system')\n else:\n raise ValueError('name string is erroneously encoded')\n\n if sound_class not in self.sound_classes:\n raise ValueError('no sound class specified')\n\n args = {self._feature_values.get(comp, '?'): comp for comp in rest}\n if '?' in args:\n raise ValueError('string contains unknown features')\n args['grapheme'] = ''\n args['ts'] = self\n sound = self.sound_classes[sound_class](**args)\n if sound.featureset not in self.features:\n sound.generated = True\n return sound\n return self.features[sound.featureset]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse a string and return its features.", "response": "def _parse(self, string):\n \"\"\"Parse a string and return its features.\n\n :param string: A one-symbol string in NFD\n\n Notes\n -----\n Strategy is rather simple: we determine the base part of a string and\n then search left and right of this part for the additional features as\n expressed by the diacritics. Fails if a segment has more than one basic\n part.\n \"\"\"\n nstring = self._norm(string)\n\n # check whether sound is in self.sounds\n if nstring in self.sounds:\n sound = self.sounds[nstring]\n sound.normalized = nstring != string\n sound.source = string\n return sound\n\n match = list(self._regex.finditer(nstring))\n # if the match has length 2, we assume that we have two sounds, so we split\n # the sound and pass it on for separate evaluation (recursive function)\n if len(match) == 2:\n sound1 = self._parse(nstring[:match[1].start()])\n sound2 = self._parse(nstring[match[1].start():])\n # if we have ANY unknown sound, we mark the whole sound as unknown, if\n # we have two known sounds of the same type (vowel or consonant), we\n # either construct a diphthong or a cluster\n if 'unknownsound' not in (sound1.type, sound2.type) and \\\n sound1.type == sound2.type:\n # diphthong creation\n if sound1.type == 'vowel':\n return Diphthong.from_sounds( # noqa: F405\n string, sound1, sound2, self)\n elif sound1.type == 'consonant' and \\\n sound1.manner in ('stop', 'implosive', 'click', 'nasal') and \\\n sound2.manner in ('stop', 'implosive', 'affricate', 'fricative'):\n return Cluster.from_sounds( # noqa: F405\n string, sound1, sound2, self)\n return UnknownSound(grapheme=nstring, source=string, ts=self) # noqa: F405\n\n if len(match) != 1:\n # Either no match or more than one; both is considered an error.\n return UnknownSound(grapheme=nstring, source=string, ts=self) # noqa: F405\n\n pre, mid, post = nstring.partition(nstring[match[0].start():match[0].end()])\n base_sound = self.sounds[mid]\n if isinstance(base_sound, Marker): # noqa: F405\n assert pre or post\n return UnknownSound(grapheme=nstring, source=string, ts=self) # noqa: F405\n\n # A base sound with diacritics or a custom symbol.\n features = attr.asdict(base_sound)\n features.update(\n source=string,\n generated=True,\n normalized=nstring != string,\n base=base_sound.grapheme)\n\n # we construct two versions: the \"normal\" version and the version where\n # we search for aliases and normalize them (as our features system for\n # diacritics may well define aliases\n grapheme, sound = '', ''\n for dia in [p + EMPTY for p in pre]:\n feature = self.diacritics[base_sound.type].get(dia, {})\n if not feature:\n return UnknownSound( # noqa: F405\n grapheme=nstring, source=string, ts=self)\n features[self._feature_values[feature]] = feature\n # we add the unaliased version to the grapheme\n grapheme += dia[0]\n # we add the corrected version (if this is needed) to the sound\n sound += self.features[base_sound.type][feature][0]\n # add the base sound\n grapheme += base_sound.grapheme\n sound += base_sound.s\n for dia in [EMPTY + p for p in post]:\n feature = self.diacritics[base_sound.type].get(dia, {})\n # we are strict: if we don't know the feature, it's an unknown\n # sound\n if not feature:\n return UnknownSound( # noqa: F405\n grapheme=nstring, source=string, ts=self)\n features[self._feature_values[feature]] = feature\n grapheme += dia[1]\n sound += self.features[base_sound.type][feature][1]\n\n features['grapheme'] = sound\n new_sound = self.sound_classes[base_sound.type](**features)\n # check whether grapheme differs from re-generated sound\n if text_type(new_sound) != sound:\n new_sound.alias = True\n if grapheme != sound:\n new_sound.alias = True\n new_sound.grapheme = grapheme\n return new_sound"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction tries to identify a sound in the data and returns the next approximate sound class.", "response": "def resolve_sound(self, sound):\n \"\"\"Function tries to identify a sound in the data.\n\n Notes\n -----\n The function tries to resolve sounds to take a sound with less complex\n features in order to yield the next approximate sound class, if the\n transcription data are sound classes.\n \"\"\"\n sound = sound if isinstance(sound, Symbol) else self.system[sound]\n if sound.name in self.data:\n return self.data[sound.name]['grapheme']\n if not sound.type == 'unknownsound':\n if sound.type in ['diphthong', 'cluster']:\n return self.resolve_sound(sound.from_sound)\n name = [\n s for s in sound.name.split(' ') if\n self.system._feature_values.get(s, '') not in\n ['laminality', 'ejection', 'tone']]\n while len(name) >= 4:\n sound = self.system.get(' '.join(name))\n if sound and sound.name in self.data:\n return self.resolve_sound(sound)\n name.pop(0)\n raise KeyError(\":sc:resolve_sound: No sound could be found.\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ipfn_np(self, m, aggregates, dimensions, weight_col='total'):\n steps = len(aggregates)\n dim = len(m.shape)\n product_elem = []\n tables = [m]\n # TODO: do we need to persist all these dataframe? Or maybe we just need to persist the table_update and table_current\n # and then update the table_current to the table_update to the latest we have. And create an empty zero dataframe for table_update (Evelyn)\n for inc in range(steps - 1):\n tables.append(np.array(np.zeros(m.shape)))\n original = copy.copy(m)\n\n # Calculate the new weights for each dimension\n for inc in range(steps):\n if inc == (steps - 1):\n table_update = m\n table_current = tables[inc]\n else:\n table_update = tables[inc + 1]\n table_current = tables[inc]\n for dimension in dimensions[inc]:\n product_elem.append(range(m.shape[dimension]))\n for item in product(*product_elem):\n idx = self.index_axis_elem(dim, dimensions[inc], item)\n table_current_slice = table_current[idx]\n mijk = table_current_slice.sum()\n # TODO: Directly put it as xijk = aggregates[inc][item] (Evelyn)\n xijk = aggregates[inc]\n xijk = xijk[item]\n if mijk == 0:\n # table_current_slice += 1e-5\n # TODO: Basically, this part would remain 0 as always right? Cause if the sum of the slice is zero, then we only have zeros in this slice.\n # TODO: you could put it as table_update[idx] = table_current_slice (since multiplication on zero is still zero)\n table_update[idx] = table_current_slice\n else:\n # TODO: when inc == steps - 1, this part is also directly updating the dataframe m (Evelyn)\n # If we are not going to persist every table generated, we could still keep this part to directly update dataframe m\n table_update[idx] = table_current_slice * 1.0 * xijk / mijk\n # For debug purposes\n # if np.isnan(table_update).any():\n # print(idx)\n # sys.exit(0)\n product_elem = []\n\n # Check the convergence rate for each dimension\n max_conv = 0\n for inc in range(steps):\n # TODO: this part already generated before, we could somehow persist it. But it's not important (Evelyn)\n for dimension in dimensions[inc]:\n product_elem.append(range(m.shape[dimension]))\n for item in product(*product_elem):\n idx = self.index_axis_elem(dim, dimensions[inc], item)\n ori_ijk = aggregates[inc][item]\n m_slice = m[idx]\n m_ijk = m_slice.sum()\n # print('Current vs original', abs(m_ijk/ori_ijk - 1))\n if abs(m_ijk / ori_ijk - 1) > max_conv:\n max_conv = abs(m_ijk / ori_ijk - 1)\n\n product_elem = []\n\n return m, max_conv", "response": "Runs the ipfn method from a matrix m and returns a numpy array with the result of the ipfn method."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns the ipfn method from a dataframe df and returns a dictionary of the unique entries.", "response": "def ipfn_df(self, df, aggregates, dimensions, weight_col='total'):\n \"\"\"\n Runs the ipfn method from a dataframe df, aggregates/marginals and the dimension(s) preserved.\n For example:\n from ipfn import ipfn\n import pandas as pd\n age = [30, 30, 30, 30, 40, 40, 40, 40, 50, 50, 50, 50]\n distance = [10,20,30,40,10,20,30,40,10,20,30,40]\n m = [8., 4., 6., 7., 3., 6., 5., 2., 9., 11., 3., 1.]\n df = pd.DataFrame()\n df['age'] = age\n df['distance'] = distance\n df['total'] = m\n\n xip = df.groupby('age')['total'].sum()\n xip.loc[30] = 20\n xip.loc[40] = 18\n xip.loc[50] = 22\n xpj = df.groupby('distance')['total'].sum()\n xpj.loc[10] = 18\n xpj.loc[20] = 16\n xpj.loc[30] = 12\n xpj.loc[40] = 14\n dimensions = [['age'], ['distance']]\n aggregates = [xip, xpj]\n\n IPF = ipfn(df, aggregates, dimensions)\n df = IPF.iteration()\n\n print(df)\n print(df.groupby('age')['total'].sum(), xip)\"\"\"\n\n steps = len(aggregates)\n tables = [df]\n for inc in range(steps - 1):\n tables.append(df.copy())\n original = df.copy()\n\n # Calculate the new weights for each dimension\n inc = 0\n for features in dimensions:\n if inc == (steps - 1):\n table_update = df\n table_current = tables[inc]\n else:\n table_update = tables[inc + 1]\n table_current = tables[inc]\n\n tmp = table_current.groupby(features)[weight_col].sum()\n xijk = aggregates[inc]\n\n feat_l = []\n for feature in features:\n feat_l.append(np.unique(table_current[feature]))\n table_update.set_index(features, inplace=True)\n table_current.set_index(features, inplace=True)\n\n for feature in product(*feat_l):\n den = tmp.loc[feature]\n # calculate new weight for this iteration\n if den == 0:\n table_update.loc[feature, weight_col] =\\\n table_current.loc[feature, weight_col] *\\\n xijk.loc[feature]\n else:\n table_update.loc[feature, weight_col] = \\\n table_current.loc[feature, weight_col].astype(float) * \\\n xijk.loc[feature] / den\n\n table_update.reset_index(inplace=True)\n table_current.reset_index(inplace=True)\n inc += 1\n feat_l = []\n\n # Calculate the max convergence rate\n max_conv = 0\n inc = 0\n for features in dimensions:\n tmp = df.groupby(features)[weight_col].sum()\n ori_ijk = aggregates[inc]\n temp_conv = max(abs(tmp / ori_ijk - 1))\n if temp_conv > max_conv:\n max_conv = temp_conv\n inc += 1\n\n return df, max_conv"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrenders link as HTML output tag .\n \"\"\"\n self.obj = obj\n attrs = ' '.join([\n '%s=\"%s\"' % (attr_name, attr.resolve(obj))\n if isinstance(attr, Accessor)\n else '%s=\"%s\"' % (attr_name, attr)\n for attr_name, attr in self.attrs.items()\n ])\n return mark_safe(u'%s' % (attrs, self.text))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nresolving the object described by the accessor by traversing the attributes of the given context.", "response": "def resolve(self, context, quiet=True):\r\n \"\"\"\r\n Return an object described by the accessor by traversing the attributes\r\n of context.\r\n\r\n \"\"\"\r\n try:\r\n obj = context\r\n for level in self.levels:\r\n if isinstance(obj, dict):\r\n obj = obj[level]\r\n elif isinstance(obj, list) or isinstance(obj, tuple):\r\n obj = obj[int(level)]\r\n else:\r\n if callable(getattr(obj, level)):\r\n try:\r\n obj = getattr(obj, level)()\r\n except KeyError:\r\n obj = getattr(obj, level)\r\n else:\r\n # for model field that has choice set\r\n # use get_xxx_display to access\r\n display = 'get_%s_display' % level\r\n obj = getattr(obj, display)() if hasattr(obj, display) else getattr(obj, level)\r\n if not obj:\r\n break\r\n return obj\r\n except Exception as e:\r\n if quiet:\r\n return ''\r\n else:\r\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of all the header rows in order of the entries in the table.", "response": "def header_rows(self):\n \"\"\"\n [ [header1], [header3, header4] ]\n \"\"\"\n # TO BE FIX: refactor\n header_rows = []\n headers = [col.header for col in self.columns]\n for header in headers:\n if len(header_rows) <= header.row_order:\n header_rows.append([])\n header_rows[header.row_order].append(header)\n return header_rows"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting context data for datatable server - side response.", "response": "def get_context_data(self, **kwargs):\n \"\"\"\n Get context data for datatable server-side response.\n See http://www.datatables.net/usage/server-side\n \"\"\"\n sEcho = self.query_data[\"sEcho\"]\n\n context = super(BaseListView, self).get_context_data(**kwargs)\n queryset = context[\"object_list\"]\n if queryset is not None:\n total_length = self.get_queryset_length(queryset)\n queryset = self.filter_queryset(queryset)\n display_length = self.get_queryset_length(queryset)\n\n queryset = self.sort_queryset(queryset)\n queryset = self.paging_queryset(queryset)\n values_list = self.convert_queryset_to_values_list(queryset)\n context = {\n \"sEcho\": sEcho,\n \"iTotalRecords\": total_length,\n \"iTotalDisplayRecords\": display_length,\n \"aaData\": values_list,\n }\n else:\n context = {\n \"sEcho\": sEcho,\n \"iTotalRecords\": 0,\n \"iTotalDisplayRecords\": 0,\n \"aaData\": [],\n }\n return context"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_days_span(self, month_index):\n is_first_month = month_index == 0\n is_last_month = month_index == self.__len__() - 1\n\n y = int(self.start_date.year + (self.start_date.month + month_index) / 13)\n m = int((self.start_date.month + month_index) % 12 or 12)\n total = calendar.monthrange(y, m)[1]\n\n if is_first_month and is_last_month:\n return (self.end_date - self.start_date).days + 1\n else:\n if is_first_month:\n return total - self.start_date.day + 1\n elif is_last_month:\n return self.end_date.day\n else:\n return total", "response": "Calculate how many days the month spans."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalculates an IEEE 754 float from an array of 4 bytes", "response": "def _calculate_float(self, byte_array):\n \"\"\"Returns an IEEE 754 float from an array of 4 bytes\n\n :param byte_array: Expects an array of 4 bytes\n\n :type byte_array: array\n\n :rtype: float\n \"\"\"\n if len(byte_array) != 4:\n return None\n\n return struct.unpack('f', struct.pack('4B', *byte_array))[0]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _calculate_period(self, vals):\n ''' calculate the sampling period in seconds '''\n if len(vals) < 4:\n return None\n\n if self.firmware['major'] < 16:\n return ((vals[3] << 24) | (vals[2] << 16) | (vals[1] << 8) | vals[0]) / 12e6\n else:\n return self._calculate_float(vals)", "response": "calculate the sampling period in seconds"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef wait(self, **kwargs):\n\n if not callable(self.on):\n raise UserWarning('Your device does not support the self.on function, try without wait')\n\n if not callable(self.histogram):\n raise UserWarning('Your device does not support the self.histogram function, try without wait')\n\n self.on()\n while True:\n try:\n if self.histogram() is None:\n raise UserWarning('Could not load histogram, perhaps the device is not yet connected')\n\n except UserWarning as e:\n sleep(kwargs.get('check', 200) / 1000.)\n\n return self", "response": "Wait for the OPC to prepare itself for data transmission."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef calculate_bin_boundary(self, bb):\n\n return min(enumerate(OPC_LOOKUP), key = lambda x: abs(x[1] - bb))[0]", "response": "Calculates the adc value that corresponds to a specific bin boundary diameter in microns."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreading the information string for the OPC .", "response": "def read_info_string(self):\n \"\"\"Reads the information string for the OPC\n\n :rtype: string\n\n :Example:\n\n >>> alpha.read_info_string()\n 'OPC-N2 FirmwareVer=OPC-018.2....................BD'\n \"\"\"\n infostring = []\n\n # Send the command byte and sleep for 9 ms\n self.cnxn.xfer([0x3F])\n sleep(9e-3)\n\n # Read the info string by sending 60 empty bytes\n for i in range(60):\n resp = self.cnxn.xfer([0x00])[0]\n infostring.append(chr(resp))\n\n sleep(0.1)\n\n return ''.join(infostring)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking the connection between the Raspberry Pi and the OPC protocol.", "response": "def ping(self):\n \"\"\"Checks the connection between the Raspberry Pi and the OPC\n\n :rtype: Boolean\n \"\"\"\n b = self.cnxn.xfer([0xCF])[0] # send the command byte\n\n sleep(0.1)\n\n return True if b == 0xF3 else False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef on(self):\n b1 = self.cnxn.xfer([0x03])[0] # send the command byte\n sleep(9e-3) # sleep for 9 ms\n b2, b3 = self.cnxn.xfer([0x00, 0x01]) # send the following byte\n sleep(0.1)\n\n return True if b1 == 0xF3 and b2 == 0x03 else False", "response": "Turn ON the OPC"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef off(self):\n b1 = self.cnxn.xfer([0x03])[0] # send the command byte\n sleep(9e-3) # sleep for 9 ms\n b2 = self.cnxn.xfer([0x01])[0] # send the following two bytes\n sleep(0.1)\n\n return True if b1 == 0xF3 and b2 == 0x03 else False", "response": "Turn OFF the OPC"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreads the configuration variables and returns them as a dictionary.", "response": "def config(self):\n \"\"\"Read the configuration variables and returns them as a dictionary\n\n :rtype: dictionary\n\n :Example:\n\n >>> alpha.config()\n {\n 'BPD 13': 1.6499,\n 'BPD 12': 1.6499,\n 'BPD 11': 1.6499,\n 'BPD 10': 1.6499,\n 'BPD 15': 1.6499,\n 'BPD 14': 1.6499,\n 'BSVW 15': 1.0,\n ...\n }\n \"\"\"\n config = []\n data = {}\n\n # Send the command byte and sleep for 10 ms\n self.cnxn.xfer([0x3C])\n sleep(10e-3)\n\n # Read the config variables by sending 256 empty bytes\n for i in range(256):\n resp = self.cnxn.xfer([0x00])[0]\n config.append(resp)\n\n # Add the bin bounds to the dictionary of data [bytes 0-29]\n for i in range(0, 15):\n data[\"Bin Boundary {0}\".format(i)] = self._16bit_unsigned(config[2*i], config[2*i + 1])\n\n # Add the Bin Particle Volumes (BPV) [bytes 32-95]\n for i in range(0, 16):\n data[\"BPV {0}\".format(i)] = self._calculate_float(config[4*i + 32:4*i + 36])\n\n # Add the Bin Particle Densities (BPD) [bytes 96-159]\n for i in range(0, 16):\n data[\"BPD {0}\".format(i)] = self._calculate_float(config[4*i + 96:4*i + 100])\n\n # Add the Bin Sample Volume Weight (BSVW) [bytes 160-223]\n for i in range(0, 16):\n data[\"BSVW {0}\".format(i)] = self._calculate_float(config[4*i + 160: 4*i + 164])\n\n # Add the Gain Scaling Coefficient (GSC) and sample flow rate (SFR)\n data[\"GSC\"] = self._calculate_float(config[224:228])\n data[\"SFR\"] = self._calculate_float(config[228:232])\n\n # Add laser dac (LDAC) and Fan dac (FanDAC)\n data[\"LaserDAC\"] = config[232]\n data[\"FanDAC\"] = config[233]\n\n # If past firmware 15, add other things\n if self.firmware['major'] > 15.:\n data['TOF_SFR'] = config[234]\n\n sleep(0.1)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread the second set of configuration variables and return as a dictionary.", "response": "def config2(self):\n \"\"\"Read the second set of configuration variables and return as a dictionary.\n\n **NOTE: This method is supported by firmware v18+.**\n\n :rtype: dictionary\n\n :Example:\n\n >>> a.config2()\n {\n 'AMFanOnIdle': 0,\n 'AMIdleIntervalCount': 0,\n 'AMMaxDataArraysInFile': 61798,\n 'AMSamplingInterval': 1,\n 'AMOnlySavePMData': 0,\n 'AMLaserOnIdle': 0\n }\n \"\"\"\n config = []\n data = {}\n\n # Send the command byte and sleep for 10 ms\n self.cnxn.xfer([0x3D])\n sleep(10e-3)\n\n # Read the config variables by sending 256 empty bytes\n for i in range(9):\n resp = self.cnxn.xfer([0x00])[0]\n config.append(resp)\n\n data[\"AMSamplingInterval\"] = self._16bit_unsigned(config[0], config[1])\n data[\"AMIdleIntervalCount\"] = self._16bit_unsigned(config[2], config[3])\n data['AMFanOnIdle'] = config[4]\n data['AMLaserOnIdle'] = config[5]\n data['AMMaxDataArraysInFile'] = self._16bit_unsigned(config[6], config[7])\n data['AMOnlySavePMData'] = config[8]\n\n sleep(0.1)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef histogram(self, number_concentration=True):\n resp = []\n data = {}\n\n # Send the command byte\n self.cnxn.xfer([0x30])\n\n # Wait 10 ms\n sleep(10e-3)\n\n # read the histogram\n for i in range(62):\n r = self.cnxn.xfer([0x00])[0]\n resp.append(r)\n\n # convert to real things and store in dictionary!\n data['Bin 0'] = self._16bit_unsigned(resp[0], resp[1])\n data['Bin 1'] = self._16bit_unsigned(resp[2], resp[3])\n data['Bin 2'] = self._16bit_unsigned(resp[4], resp[5])\n data['Bin 3'] = self._16bit_unsigned(resp[6], resp[7])\n data['Bin 4'] = self._16bit_unsigned(resp[8], resp[9])\n data['Bin 5'] = self._16bit_unsigned(resp[10], resp[11])\n data['Bin 6'] = self._16bit_unsigned(resp[12], resp[13])\n data['Bin 7'] = self._16bit_unsigned(resp[14], resp[15])\n data['Bin 8'] = self._16bit_unsigned(resp[16], resp[17])\n data['Bin 9'] = self._16bit_unsigned(resp[18], resp[19])\n data['Bin 10'] = self._16bit_unsigned(resp[20], resp[21])\n data['Bin 11'] = self._16bit_unsigned(resp[22], resp[23])\n data['Bin 12'] = self._16bit_unsigned(resp[24], resp[25])\n data['Bin 13'] = self._16bit_unsigned(resp[26], resp[27])\n data['Bin 14'] = self._16bit_unsigned(resp[28], resp[29])\n data['Bin 15'] = self._16bit_unsigned(resp[30], resp[31])\n data['Bin1 MToF'] = self._calculate_mtof(resp[32])\n data['Bin3 MToF'] = self._calculate_mtof(resp[33])\n data['Bin5 MToF'] = self._calculate_mtof(resp[34])\n data['Bin7 MToF'] = self._calculate_mtof(resp[35])\n\n # Bins associated with firmware versions 14 and 15(?)\n if self.firmware['version'] < 16.:\n data['Temperature'] = self._calculate_temp(resp[36:40])\n data['Pressure'] = self._calculate_pressure(resp[40:44])\n data['Sampling Period'] = self._calculate_period(resp[44:48])\n data['Checksum'] = self._16bit_unsigned(resp[48], resp[49])\n data['PM1'] = self._calculate_float(resp[50:54])\n data['PM2.5'] = self._calculate_float(resp[54:58])\n data['PM10'] = self._calculate_float(resp[58:])\n\n else:\n data['SFR'] = self._calculate_float(resp[36:40])\n\n # Alright, we don't know whether it is temp or pressure since it switches..\n tmp = self._calculate_pressure(resp[40:44])\n if tmp > 98000:\n data['Temperature'] = None\n data['Pressure'] = tmp\n else:\n tmp = self._calculate_temp(resp[40:44])\n if tmp < 500:\n data['Temperature'] = tmp\n data['Pressure'] = None\n else:\n data['Temperature'] = None\n data['Pressure'] = None\n\n data['Sampling Period'] = self._calculate_float(resp[44:48])\n data['Checksum'] = self._16bit_unsigned(resp[48], resp[49])\n data['PM1'] = self._calculate_float(resp[50:54])\n data['PM2.5'] = self._calculate_float(resp[54:58])\n data['PM10'] = self._calculate_float(resp[58:])\n\n # Calculate the sum of the histogram bins\n histogram_sum = data['Bin 0'] + data['Bin 1'] + data['Bin 2'] + \\\n data['Bin 3'] + data['Bin 4'] + data['Bin 5'] + data['Bin 6'] + \\\n data['Bin 7'] + data['Bin 8'] + data['Bin 9'] + data['Bin 10'] + \\\n data['Bin 11'] + data['Bin 12'] + data['Bin 13'] + data['Bin 14'] + \\\n data['Bin 15']\n\n # Check that checksum and the least significant bits of the sum of histogram bins\n # are equivilant\n if (histogram_sum & 0x0000FFFF) != data['Checksum']:\n logger.warning(\"Data transfer was incomplete\")\n return None\n\n # If histogram is true, convert histogram values to number concentration\n if number_concentration is True:\n _conv_ = data['SFR'] * data['Sampling Period'] # Divider in units of ml (cc)\n\n data['Bin 0'] = data['Bin 0'] / _conv_\n data['Bin 1'] = data['Bin 1'] / _conv_\n data['Bin 2'] = data['Bin 2'] / _conv_\n data['Bin 3'] = data['Bin 3'] / _conv_\n data['Bin 4'] = data['Bin 4'] / _conv_\n data['Bin 5'] = data['Bin 5'] / _conv_\n data['Bin 6'] = data['Bin 6'] / _conv_\n data['Bin 7'] = data['Bin 7'] / _conv_\n data['Bin 8'] = data['Bin 8'] / _conv_\n data['Bin 9'] = data['Bin 9'] / _conv_\n data['Bin 10'] = data['Bin 10'] / _conv_\n data['Bin 11'] = data['Bin 11'] / _conv_\n data['Bin 12'] = data['Bin 12'] / _conv_\n data['Bin 13'] = data['Bin 13'] / _conv_\n data['Bin 14'] = data['Bin 14'] / _conv_\n data['Bin 15'] = data['Bin 15'] / _conv_\n\n sleep(0.1)\n\n return data", "response": "Read and reset the histogram. As of v1. 3. 0 histogram values are reported in number concentration by default."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef save_config_variables(self):\n command = 0x43\n byte_list = [0x3F, 0x3C, 0x3F, 0x3C, 0x43]\n success = [0xF3, 0x43, 0x3F, 0x3C, 0x3F, 0x3C]\n resp = []\n\n # Send the command byte and then wait for 10 ms\n r = self.cnxn.xfer([command])[0]\n sleep(10e-3)\n\n # append the response of the command byte to the List\n resp.append(r)\n\n # Send the rest of the config bytes\n for each in byte_list:\n r = self.cnxn.xfer([each])[0]\n resp.append(r)\n\n sleep(0.1)\n\n return True if resp == success else False", "response": "This method saves the configuration variables in non - volatile memory. This method is used in conjuction with write_config_variables."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_fan_power(self, power):\n # Check to make sure the value is a single byte\n if power > 255:\n raise ValueError(\"The fan power should be a single byte (0-255).\")\n\n # Send the command byte and wait 10 ms\n a = self.cnxn.xfer([0x42])[0]\n sleep(10e-3)\n\n # Send the next two bytes\n b = self.cnxn.xfer([0x00])[0]\n c = self.cnxn.xfer([power])[0]\n\n sleep(0.1)\n\n return True if a == 0xF3 and b == 0x42 and c == 0x00 else False", "response": "Set the fan power."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef toggle_laser(self, state):\n\n # Send the command byte and wait 10 ms\n a = self.cnxn.xfer([0x03])[0]\n\n sleep(10e-3)\n\n # If state is true, turn the laser ON, else OFF\n if state:\n b = self.cnxn.xfer([0x02])[0]\n else:\n b = self.cnxn.xfer([0x03])[0]\n\n sleep(0.1)\n\n return True if a == 0xF3 and b == 0x03 else False", "response": "Toggle the power state of the laser."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreading the status of the digital potential. Firmware v18 + only.", "response": "def read_pot_status(self):\n \"\"\"Read the status of the digital pot. Firmware v18+ only.\n The return value is a dictionary containing the following as\n unsigned 8-bit integers: FanON, LaserON, FanDACVal, LaserDACVal.\n\n :rtype: dict\n\n :Example:\n\n >>> alpha.read_pot_status()\n {\n 'LaserDACVal': 230,\n 'FanDACVal': 255,\n 'FanON': 0,\n 'LaserON': 0\n }\n \"\"\"\n # Send the command byte and wait 10 ms\n a = self.cnxn.xfer([0x13])[0]\n\n sleep(10e-3)\n\n # Build an array of the results\n res = []\n for i in range(4):\n res.append(self.cnxn.xfer([0x00])[0])\n\n sleep(0.1)\n\n return {\n 'FanON': res[0],\n 'LaserON': res[1],\n 'FanDACVal': res[2],\n 'LaserDACVal': res[3]\n }"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread the Serial Number string. This method is only available on OPC - N2 123456789.", "response": "def sn(self):\n \"\"\"Read the Serial Number string. This method is only available on OPC-N2\n firmware versions 18+.\n\n :rtype: string\n\n :Example:\n\n >>> alpha.sn()\n 'OPC-N2 123456789'\n \"\"\"\n string = []\n\n # Send the command byte and sleep for 9 ms\n self.cnxn.xfer([0x10])\n sleep(9e-3)\n\n # Read the info string by sending 60 empty bytes\n for i in range(60):\n resp = self.cnxn.xfer([0x00])[0]\n string.append(chr(resp))\n\n sleep(0.1)\n\n return ''.join(string)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading the firmware version of the OPC - N2. Firmware v18 + only.", "response": "def read_firmware(self):\n \"\"\"Read the firmware version of the OPC-N2. Firmware v18+ only.\n\n :rtype: dict\n\n :Example:\n\n >>> alpha.read_firmware()\n {\n 'major': 18,\n 'minor': 2,\n 'version': 18.2\n }\n \"\"\"\n # Send the command byte and sleep for 9 ms\n self.cnxn.xfer([0x12])\n sleep(10e-3)\n\n self.firmware['major'] = self.cnxn.xfer([0x00])[0]\n self.firmware['minor'] = self.cnxn.xfer([0x00])[0]\n\n # Build the firmware version\n self.firmware['version'] = float('{}.{}'.format(self.firmware['major'], self.firmware['minor']))\n\n sleep(0.1)\n\n return self.firmware"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreading the PM data and reset the histogram!", "response": "def pm(self):\n \"\"\"Read the PM data and reset the histogram\n\n **NOTE: This method is supported by firmware v18+.**\n\n :rtype: dictionary\n\n :Example:\n\n >>> alpha.pm()\n {\n 'PM1': 0.12,\n 'PM2.5': 0.24,\n 'PM10': 1.42\n }\n \"\"\"\n\n resp = []\n data = {}\n\n # Send the command byte\n self.cnxn.xfer([0x32])\n\n # Wait 10 ms\n sleep(10e-3)\n\n # read the histogram\n for i in range(12):\n r = self.cnxn.xfer([0x00])[0]\n resp.append(r)\n\n # convert to real things and store in dictionary!\n data['PM1'] = self._calculate_float(resp[0:4])\n data['PM2.5'] = self._calculate_float(resp[4:8])\n data['PM10'] = self._calculate_float(resp[8:])\n\n sleep(0.1)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef on(self):\n b1 = self.cnxn.xfer([0x0C])[0] # send the command byte\n sleep(9e-3) # sleep for 9 ms\n\n return True if b1 == 0xF3 else False", "response": "Turn ON the OPC"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef off(self):\n b1 = self.cnxn.xfer([0x03])[0] # send the command byte\n sleep(9e-3) # sleep for 9 ms\n\n return True if b1 == 0xF3 else False", "response": "Turn OFF the OPC"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_gsc_sfr(self):\n config = []\n data = {}\n\n # Send the command byte and sleep for 10 ms\n self.cnxn.xfer([0x33])\n sleep(10e-3)\n\n # Read the config variables by sending 256 empty bytes\n for i in range(8):\n resp = self.cnxn.xfer([0x00])[0]\n config.append(resp)\n\n data[\"GSC\"] = self._calculate_float(config[0:4])\n data[\"SFR\"] = self._calculate_float(config[4:])\n\n return data", "response": "Reads the gain - scaling coefficient and sample flow rate."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef read_bin_boundaries(self):\n config = []\n data = {}\n\n # Send the command byte and sleep for 10 ms\n self.cnxn.xfer([0x33])\n sleep(10e-3)\n\n # Read the config variables by sending 256 empty bytes\n for i in range(30):\n resp = self.cnxn.xfer([0x00])[0]\n config.append(resp)\n\n # Add the bin bounds to the dictionary of data [bytes 0-29]\n for i in range(0, 14):\n data[\"Bin Boundary {0}\".format(i)] = self._16bit_unsigned(config[2*i], config[2*i + 1])\n\n return data", "response": "Reads the 17 bin boundaries from the CXN and returns the dictionary with the bin boundaries."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreads the bin particle density.", "response": "def read_bin_particle_density(self):\n \"\"\"Read the bin particle density\n\n :returns: float\n \"\"\"\n config = []\n\n # Send the command byte and sleep for 10 ms\n self.cnxn.xfer([0x33])\n sleep(10e-3)\n\n # Read the config variables by sending 256 empty bytes\n for i in range(4):\n resp = self.cnxn.xfer([0x00])[0]\n config.append(resp)\n\n bpd = self._calculate_float(config)\n\n return bpd"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads and reset the histogram.", "response": "def read_histogram(self):\n \"\"\"Read and reset the histogram. The expected return is a dictionary\n containing the counts per bin, MToF for bins 1, 3, 5, and 7, temperature,\n pressure, the sampling period, the checksum, PM1, PM2.5, and PM10.\n\n **NOTE:** The sampling period for the OPCN1 seems to be incorrect.\n\n :returns: dictionary\n \"\"\"\n resp = []\n data = {}\n\n # command byte\n command = 0x30\n\n # Send the command byte\n self.cnxn.xfer([command])\n\n # Wait 10 ms\n sleep(10e-3)\n\n # read the histogram\n for i in range(62):\n r = self.cnxn.xfer([0x00])[0]\n resp.append(r)\n\n # convert to real things and store in dictionary!\n data['Bin 0'] = self._16bit_unsigned(resp[0], resp[1])\n data['Bin 1'] = self._16bit_unsigned(resp[2], resp[3])\n data['Bin 2'] = self._16bit_unsigned(resp[4], resp[5])\n data['Bin 3'] = self._16bit_unsigned(resp[6], resp[7])\n data['Bin 4'] = self._16bit_unsigned(resp[8], resp[9])\n data['Bin 5'] = self._16bit_unsigned(resp[10], resp[11])\n data['Bin 6'] = self._16bit_unsigned(resp[12], resp[13])\n data['Bin 7'] = self._16bit_unsigned(resp[14], resp[15])\n data['Bin 8'] = self._16bit_unsigned(resp[16], resp[17])\n data['Bin 9'] = self._16bit_unsigned(resp[18], resp[19])\n data['Bin 10'] = self._16bit_unsigned(resp[20], resp[21])\n data['Bin 11'] = self._16bit_unsigned(resp[22], resp[23])\n data['Bin 12'] = self._16bit_unsigned(resp[24], resp[25])\n data['Bin 13'] = self._16bit_unsigned(resp[26], resp[27])\n data['Bin 14'] = self._16bit_unsigned(resp[28], resp[29])\n data['Bin 15'] = self._16bit_unsigned(resp[30], resp[31])\n data['Bin1 MToF'] = self._calculate_mtof(resp[32])\n data['Bin3 MToF'] = self._calculate_mtof(resp[33])\n data['Bin5 MToF'] = self._calculate_mtof(resp[34])\n data['Bin7 MToF'] = self._calculate_mtof(resp[35])\n data['Temperature'] = self._calculate_temp(resp[36:40])\n data['Pressure'] = self._calculate_pressure(resp[40:44])\n data['Sampling Period'] = self._calculate_period(resp[44:48])\n data['Checksum'] = self._16bit_unsigned(resp[48], resp[49])\n data['PM1'] = self._calculate_float(resp[50:54])\n data['PM2.5'] = self._calculate_float(resp[54:58])\n data['PM10'] = self._calculate_float(resp[58:])\n\n # Calculate the sum of the histogram bins\n histogram_sum = data['Bin 0'] + data['Bin 1'] + data['Bin 2'] + \\\n data['Bin 3'] + data['Bin 4'] + data['Bin 5'] + data['Bin 6'] + \\\n data['Bin 7'] + data['Bin 8'] + data['Bin 9'] + data['Bin 10'] + \\\n data['Bin 11'] + data['Bin 12'] + data['Bin 13'] + data['Bin 14'] + \\\n data['Bin 15']\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start(self):\n\n self.receiver = self.Receiver(\n self.read,\n self.write,\n self.send_lock,\n self.senders,\n self.frames_received,\n callback=self.receive_callback,\n fcs_nack=self.fcs_nack,\n )\n\n self.receiver.start()", "response": "Starts HDLC controller s threads."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef stop(self):\n\n if self.receiver != None:\n self.receiver.join()\n\n for s in self.senders.values():\n s.join()", "response": "Stops HDLC controller s threads."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send(self, data):\n\n while len(self.senders) >= self.window:\n pass\n\n self.senders[self.new_seq_no] = self.Sender(\n self.write,\n self.send_lock,\n data,\n self.new_seq_no,\n timeout=self.sending_timeout,\n callback=self.send_callback,\n )\n\n self.senders[self.new_seq_no].start()\n self.new_seq_no = (self.new_seq_no + 1) % HDLController.MAX_SEQ_NO", "response": "This method sends a new data frame to the HDL controller."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cmp(self, other):\n if isinstance(other, Range):\n # other has tz, I dont, so replace the tz\n start = self.start.replace(tzinfo=other.start.tz) if other.start.tz and self.start.tz is None else self.start\n end = self.end.replace(tzinfo=other.end.tz) if other.end.tz and self.end.tz is None else self.end\n\n if start == other.start and end == other.end:\n return 0 \n elif start < other.start:\n return -1\n else:\n return 1\n\n elif isinstance(other, Date):\n if other.tz and self.start.tz is None:\n return 0 if other == self.start.replace(tzinfo=other.tz) else -1 if other > self.start.replace(tzinfo=other.start.tz) else 1\n return 0 if other == self.start else -1 if other > self.start else 1\n else:\n return self.cmp(Range(other, tz=self.start.tz))", "response": "Compare two sets of date objects."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncut this object from_start to the number requestd returns new instance of Range", "response": "def cut(self, by, from_start=True):\n \"\"\" Cuts this object from_start to the number requestd\n returns new instance\n \"\"\"\n s, e = copy(self.start), copy(self.end)\n if from_start:\n e = s + by\n else:\n s = e - by\n return Range(s, e)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a new instance of self with the next time.", "response": "def next(self, times=1):\n \"\"\"Returns a new instance of self\n times is not supported yet.\n \"\"\"\n return Range(copy(self.end),\n self.end + self.elapse, tz=self.start.tz)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a new instance of self with the previous entry in the time zone.", "response": "def prev(self, times=1):\n \"\"\"Returns a new instance of self\n times is not supported yet.\n \"\"\"\n return Range(self.start - self.elapse,\n copy(self.start), tz=self.start.tz)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nnoting returns a new Date obj", "response": "def replace(self, **k):\n \"\"\"Note returns a new Date obj\"\"\"\n if self.date != 'infinity':\n return Date(self.date.replace(**k))\n else:\n return Date('infinity')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadjusts the time from kwargs to timedelta Returns a new copy of self", "response": "def adjust(self, to):\n '''\n Adjusts the time from kwargs to timedelta\n **Will change this object**\n\n return new copy of self\n '''\n if self.date == 'infinity':\n return\n new = copy(self)\n if type(to) in (str, unicode):\n to = to.lower()\n res = TIMESTRING_RE.search(to)\n if res:\n rgroup = res.groupdict()\n if (rgroup.get('delta') or rgroup.get('delta_2')):\n i = int(text2num(rgroup.get('num', 'one'))) * (-1 if to.startswith('-') else 1)\n delta = (rgroup.get('delta') or rgroup.get('delta_2')).lower()\n if delta.startswith('y'):\n try:\n new.date = new.date.replace(year=(new.date.year + i))\n except ValueError:\n # day is out of range for month\n new.date = new.date + timedelta(days=(365 * i))\n elif delta.startswith('month'):\n if (new.date.month + i) > 12:\n new.date = new.date.replace(month=(i - (i / 12)),\n year=(new.date.year + 1 + (i / 12)))\n elif (new.date.month + i) < 1:\n new.date = new.date.replace(month=12, year=(new.date.year - 1))\n else:\n new.date = new.date.replace(month=(new.date.month + i))\n elif delta.startswith('q'):\n # NP\n pass\n elif delta.startswith('w'):\n new.date = new.date + timedelta(days=(7 * i))\n elif delta.startswith('s'):\n new.date = new.date + timedelta(seconds=i)\n else:\n new.date = new.date + timedelta(**{('days' if delta.startswith('d') else 'hours' if delta.startswith('h') else 'minutes' if delta.startswith('m') else 'seconds'): i})\n return new\n else:\n new.date = new.date + timedelta(seconds=int(to))\n return new\n\n raise TimestringInvalid('Invalid addition request')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfind all the timestrings within a block of text.", "response": "def findall(text):\n \"\"\"Find all the timestrings within a block of text.\n\n >>> timestring.findall(\"once upon a time, about 3 weeks ago, there was a boy whom was born on august 15th at 7:20 am. epic.\")\n [\n ('3 weeks ago,', ),\n ('august 15th at 7:20 am', )\n ]\n \"\"\"\n results = TIMESTRING_RE.findall(text)\n dates = []\n for date in results:\n if re.compile('((next|last)\\s(\\d+|couple(\\sof))\\s(weeks|months|quarters|years))|(between|from)', re.I).match(date[0]):\n dates.append((date[0].strip(), Range(date[0])))\n else:\n dates.append((date[0].strip(), Date(date[0])))\n return dates"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef authenticate(self, request):\n try:\n oauth_request = oauth_provider.utils.get_oauth_request(request)\n except oauth.Error as err:\n raise exceptions.AuthenticationFailed(err.message)\n\n if not oauth_request:\n return None\n\n oauth_params = oauth_provider.consts.OAUTH_PARAMETERS_NAMES\n\n found = any(param for param in oauth_params if param in oauth_request)\n missing = list(param for param in oauth_params if param not in oauth_request)\n\n if not found:\n # OAuth authentication was not attempted.\n return None\n\n if missing:\n # OAuth was attempted but missing parameters.\n msg = 'Missing parameters: %s' % (', '.join(missing))\n raise exceptions.AuthenticationFailed(msg)\n\n if not self.check_nonce(request, oauth_request):\n msg = 'Nonce check failed'\n raise exceptions.AuthenticationFailed(msg)\n\n try:\n consumer_key = oauth_request.get_parameter('oauth_consumer_key')\n consumer = oauth_provider_store.get_consumer(request, oauth_request, consumer_key)\n except oauth_provider.store.InvalidConsumerError:\n msg = 'Invalid consumer token: %s' % oauth_request.get_parameter('oauth_consumer_key')\n raise exceptions.AuthenticationFailed(msg)\n\n if consumer.status != oauth_provider.consts.ACCEPTED:\n msg = 'Invalid consumer key status: %s' % consumer.get_status_display()\n raise exceptions.AuthenticationFailed(msg)\n\n try:\n token_param = oauth_request.get_parameter('oauth_token')\n token = oauth_provider_store.get_access_token(request, oauth_request, consumer, token_param)\n except oauth_provider.store.InvalidTokenError:\n msg = 'Invalid access token: %s' % oauth_request.get_parameter('oauth_token')\n raise exceptions.AuthenticationFailed(msg)\n\n try:\n self.validate_token(request, consumer, token)\n except oauth.Error as err:\n raise exceptions.AuthenticationFailed(err.message)\n\n user = token.user\n\n if not user.is_active:\n msg = 'User inactive or deleted: %s' % user.username\n raise exceptions.AuthenticationFailed(msg)\n\n return (token.user, token)", "response": "Authenticate the user with the OAuth request."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef validate_token(self, request, consumer, token):\n oauth_server, oauth_request = oauth_provider.utils.initialize_server_request(request)\n oauth_server.verify_request(oauth_request, consumer, token)", "response": "Validate the token and raise an OAuth. Error exception if invalid."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef check_nonce(self, request, oauth_request):\n oauth_nonce = oauth_request['oauth_nonce']\n oauth_timestamp = oauth_request['oauth_timestamp']\n return check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp)", "response": "Checks the nonce of request and return True if valid."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nauthenticate the user with the request.", "response": "def authenticate(self, request):\n \"\"\"\n Returns two-tuple of (user, token) if authentication succeeds,\n or None otherwise.\n \"\"\"\n\n auth = get_authorization_header(request).split()\n\n if len(auth) == 1:\n msg = 'Invalid bearer header. No credentials provided.'\n raise exceptions.AuthenticationFailed(msg)\n elif len(auth) > 2:\n msg = 'Invalid bearer header. Token string should not contain spaces.'\n raise exceptions.AuthenticationFailed(msg)\n\n if auth and auth[0].lower() == b'bearer':\n access_token = auth[1]\n elif 'access_token' in request.POST:\n access_token = request.POST['access_token']\n elif 'access_token' in request.GET and self.allow_query_params_token:\n access_token = request.GET['access_token']\n else:\n return None\n\n return self.authenticate_credentials(request, access_token)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the signature of the current token", "response": "def getSHA1(self, token, timestamp, nonce, encrypt):\n \"\"\"\u7528SHA1\u7b97\u6cd5\u751f\u6210\u5b89\u5168\u7b7e\u540d\n @param token: \u7968\u636e\n @param timestamp: \u65f6\u95f4\u6233\n @param encrypt: \u5bc6\u6587\n @param nonce: \u968f\u673a\u5b57\u7b26\u4e32\n @return: \u5b89\u5168\u7b7e\u540d\n \"\"\"\n try:\n sortlist = [token, timestamp, nonce, encrypt]\n sortlist.sort()\n sha = hashlib.sha1()\n sha.update(\"\".join(sortlist))\n return WXBizMsgCrypt_OK, sha.hexdigest()\n except Exception:\n return WXBizMsgCrypt_ComputeSignature_Error, None"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextracts the encrypted and touser name from xmltext.", "response": "def extract(self, xmltext):\n \"\"\"\u63d0\u53d6\u51faxml\u6570\u636e\u5305\u4e2d\u7684\u52a0\u5bc6\u6d88\u606f\n @param xmltext: \u5f85\u63d0\u53d6\u7684xml\u5b57\u7b26\u4e32\n @return: \u63d0\u53d6\u51fa\u7684\u52a0\u5bc6\u6d88\u606f\u5b57\u7b26\u4e32\n \"\"\"\n try:\n xml_tree = ET.fromstring(xmltext)\n encrypt = xml_tree.find(\"Encrypt\")\n touser_name = xml_tree.find(\"ToUserName\")\n if touser_name != None:\n touser_name = touser_name.text\n return WXBizMsgCrypt_OK, encrypt.text, touser_name\n except Exception:\n return WXBizMsgCrypt_ParseXml_Error, None, None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef encode(self, text):\n text_length = len(text)\n # \u8ba1\u7b97\u9700\u8981\u586b\u5145\u7684\u4f4d\u6570\n amount_to_pad = self.block_size - (text_length % self.block_size)\n if amount_to_pad == 0:\n amount_to_pad = self.block_size\n # \u83b7\u5f97\u8865\u4f4d\u6240\u7528\u7684\u5b57\u7b26\n pad = chr(amount_to_pad)\n return text + pad * amount_to_pad", "response": "Encodes the given text into a base64 - encoded version of the key."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndecodes a string to a unicode object", "response": "def decode(self, decrypted):\n \"\"\"\u5220\u9664\u89e3\u5bc6\u540e\u660e\u6587\u7684\u8865\u4f4d\u5b57\u7b26\n @param decrypted: \u89e3\u5bc6\u540e\u7684\u660e\u6587\n @return: \u5220\u9664\u8865\u4f4d\u5b57\u7b26\u540e\u7684\u660e\u6587\n \"\"\"\n pad = ord(decrypted[-1])\n if pad < 1 or pad > 32:\n pad = 0\n return decrypted[:-pad]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nencrypting text with appid", "response": "def encrypt(self, text, appid):\n \"\"\"\u5bf9\u660e\u6587\u8fdb\u884c\u52a0\u5bc6\n @param text: \u9700\u8981\u52a0\u5bc6\u7684\u660e\u6587\n @return: \u52a0\u5bc6\u5f97\u5230\u7684\u5b57\u7b26\u4e32\n \"\"\"\n # 16\u4f4d\u968f\u673a\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u660e\u6587\u5f00\u5934\n text = self.get_random_str() + struct.pack(\n \"I\", socket.htonl(len(text))) + text + appid\n # \u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u586b\u5145\u65b9\u5f0f\u5bf9\u660e\u6587\u8fdb\u884c\u8865\u4f4d\u586b\u5145\n pkcs7 = PKCS7Encoder()\n text = pkcs7.encode(text)\n # \u52a0\u5bc6\n cryptor = AES.new(self.key, self.mode, self.key[:16])\n try:\n ciphertext = cryptor.encrypt(text)\n # \u4f7f\u7528BASE64\u5bf9\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\n return WXBizMsgCrypt_OK, base64.b64encode(ciphertext)\n except Exception:\n return WXBizMsgCrypt_EncryptAES_Error, None"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef deliveries(self):\n key = make_key(\n event=self.object.event,\n owner_name=self.object.owner.username,\n identifier=self.object.identifier\n )\n return redis.lrange(key, 0, 20)", "response": "Get the delivery log from Redis"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the possible events from settings. WEBHOOK_EVENTS", "response": "def event_choices(events):\n \"\"\" Get the possible events from settings \"\"\"\n if events is None:\n msg = \"Please add some events in settings.WEBHOOK_EVENTS.\"\n raise ImproperlyConfigured(msg)\n try:\n choices = [(x, x) for x in events]\n except TypeError:\n \"\"\" Not a valid iterator, so we raise an exception \"\"\"\n msg = \"settings.WEBHOOK_EVENTS must be an iterable object.\"\n raise ImproperlyConfigured(msg)\n return choices"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nnotifying the user of a new resource.", "response": "def notify(self, message):\n \"\"\"\n TODO: Add code to lpush to redis stack\n rpop when stack hits size 'X'\n \"\"\"\n data = dict(\n payload=self.payload,\n attempt=self.attempt,\n success=self.success,\n response_message=self.response_content,\n hash_value=self.hash_value,\n response_status=self.response.status_code,\n notification=message,\n created=timezone.now()\n )\n value = json.dumps(data, cls=StandardJSONEncoder)\n key = make_key(self.event, self.owner.username, self.identifier)\n redis.lpush(key, value)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef send(self, msg):\n # Create a sliplib Driver\n slipDriver = sliplib.Driver()\n\n # Package data in slip format\n slipData = slipDriver.send(msg)\n\n # Send data over serial port\n res = self._serialPort.write(slipData)\n\n # Return number of bytes transmitted over serial port\n return res", "response": "Encodes data to slip protocol and then sends it over the serial port."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef receive(self, length):\n\n # Create a sliplib Driver\n slipDriver = sliplib.Driver()\n\n # Receive data from serial port\n ret = self._serialPort.read(length)\n\n # Decode data from slip format, stores msgs in sliplib.Driver.messages\n temp = slipDriver.receive(ret)\n return iter(temp)", "response": "Reads in data from a serial port and decodes the SLIP packets and returns an iterator of the messages received."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef checkTUN(self):\n packet = self._TUN._tun.read(self._TUN._tun.mtu)\n return(packet)", "response": "Checks the TUN adapter for data and returns any that is found."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef monitorTUN(self):\n packet = self.checkTUN()\n\n if packet:\n try:\n # TODO Do I need to strip off [4:] before sending?\n ret = self._faraday.send(packet)\n return ret\n\n except AttributeError as error:\n # AttributeError was encounteredthreading.Event()\n print(\"AttributeError\")", "response": "Monitors the TUN adapter and sends data over serial port."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef checkSerial(self):\n for item in self.rxSerial(self._TUN._tun.mtu):\n # print(\"about to send: {0}\".format(item))\n try:\n self._TUN._tun.write(item)\n except pytun.Error as error:\n print(\"pytun error writing: {0}\".format(item))\n print(error)", "response": "Check the serial port for data to write to the TUN adapter."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nwrap function for TUN and serial port monitoring Wraps the necessary functions to loop over until self._isRunning threading.Event() is set(). This checks for data on the TUN/serial interfaces and then sends data over the appropriate interface. This function is automatically run when Threading.start() is called on the Monitor class.", "response": "def run(self):\n \"\"\"\n Wrapper function for TUN and serial port monitoring\n\n Wraps the necessary functions to loop over until self._isRunning\n threading.Event() is set(). This checks for data on the TUN/serial\n interfaces and then sends data over the appropriate interface. This\n function is automatically run when Threading.start() is called on the\n Monitor class.\n \"\"\"\n while self.isRunning.is_set():\n try:\n try:\n # self.checkTUN()\n self.monitorTUN()\n\n except timeout_decorator.TimeoutError as error:\n # No data received so just move on\n pass\n self.checkSerial()\n except KeyboardInterrupt:\n break"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _create_formsets(self, request, obj, change, index, is_template):\n \"Helper function to generate formsets for add/change_view.\"\n formsets = []\n inline_instances = []\n prefixes = defaultdict(int)\n get_formsets_args = [request]\n if change:\n get_formsets_args.append(obj)\n base_prefix = self.get_formset(request).get_default_prefix()\n for FormSet, inline in self.get_formsets_with_inlines(\n *get_formsets_args):\n prefix = base_prefix + '-' + FormSet.get_default_prefix()\n if not is_template:\n prefix += '-%s' % index\n prefixes[prefix] += 1\n if prefixes[prefix] != 1 or not prefix:\n prefix = \"%s-%s\" % (prefix, prefixes[prefix])\n formset_params = {\n 'instance': obj,\n 'prefix': prefix,\n 'queryset': inline.get_queryset(request),\n }\n if request.method == 'POST':\n formset_params.update({\n 'data': request.POST,\n 'files': request.FILES,\n 'save_as_new': '_saveasnew' in request.POST\n })\n formset = FormSet(**formset_params)\n formset.has_parent = True\n formsets.append(formset)\n inline_instances.append(inline)\n return formsets, inline_instances", "response": "Helper function to generate formsets for add / change_view."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_field_settings(self):\n field_settings = None\n if self.field_settings:\n if isinstance(self.field_settings, six.string_types):\n profiles = settings.CONFIG.get(self.PROFILE_KEY, {})\n field_settings = profiles.get(self.field_settings)\n else:\n field_settings = self.field_settings\n return field_settings", "response": "Get the field settings from the global config."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef value_from_datadict(self, *args, **kwargs):\n value = super(RichTextWidget, self).value_from_datadict(\n *args, **kwargs)\n if value is not None:\n value = self.get_sanitizer()(value)\n return value", "response": "Pass the submitted value through the sanitizer before returning it."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the field sanitizer.", "response": "def get_sanitizer(self):\n \"\"\"\n Get the field sanitizer.\n\n The priority is the first defined in the following order:\n - A sanitizer provided to the widget.\n - Profile (field settings) specific sanitizer, if defined in settings.\n - Global sanitizer defined in settings.\n - Simple no-op sanitizer which just returns the provided value.\n\n \"\"\"\n sanitizer = self.sanitizer\n\n if not sanitizer:\n default_sanitizer = settings.CONFIG.get(self.SANITIZER_KEY)\n field_settings = getattr(self, 'field_settings', None)\n if isinstance(field_settings, six.string_types):\n profiles = settings.CONFIG.get(self.SANITIZER_PROFILES_KEY, {})\n sanitizer = profiles.get(field_settings, default_sanitizer)\n else:\n sanitizer = default_sanitizer\n\n if isinstance(sanitizer, six.string_types):\n sanitizer = import_string(sanitizer)\n\n return sanitizer or noop"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef heapreplace_max(heap, item):\n returnitem = heap[0] # raises appropriate IndexError if heap is empty\n heap[0] = item\n _siftup_max(heap, 0)\n return returnitem", "response": "Maxheap version of a heappop followed by a heappush."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef heappush_max(heap, item):\n heap.append(item)\n _siftdown_max(heap, 0, len(heap) - 1)", "response": "Push item onto heap maintaining the heap invariant."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef heappushpop_max(heap, item):\n if heap and heap[0] > item:\n # if item >= heap[0], it will be popped immediately after pushed\n item, heap[0] = heap[0], item\n _siftup_max(heap, 0)\n return item", "response": "Fast version of a heappush followed by a heappop."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef validate_response(expected_responses):\n\n def internal_decorator(function):\n @wraps(function)\n async def wrapper(*args, **kwargs):\n\n response = await function(*args, **kwargs)\n\n for expected_response in expected_responses:\n if response.startswith(expected_response):\n return response\n\n raise QRTCommandException(\n \"Expected %s but got %s\" % (expected_responses, response)\n )\n\n return wrapper\n\n return internal_decorator", "response": "Decorator to validate responses from QTM"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconnect to a specific couldonite site.", "response": "async def connect(\n host,\n port=22223,\n version=\"1.19\",\n on_event=None,\n on_disconnect=None,\n timeout=5,\n loop=None,\n) -> QRTConnection:\n \"\"\"Async function to connect to QTM\n\n :param host: Address of the computer running QTM.\n :param port: Port number to connect to, should be the port configured for little endian.\n :param version: What version of the protocol to use, tested for 1.17 and above but could\n work with lower versions as well.\n :param on_disconnect: Function to be called when a disconnect from QTM occurs.\n :param on_event: Function to be called when there's an event from QTM.\n :param timeout: The default timeout time for calls to QTM.\n :param loop: Alternative event loop, will use asyncio default if None.\n\n :rtype: A :class:`.QRTConnection`\n \"\"\"\n loop = loop or asyncio.get_event_loop()\n\n try:\n _, protocol = await loop.create_connection(\n lambda: QTMProtocol(\n loop=loop, on_event=on_event, on_disconnect=on_disconnect\n ),\n host,\n port,\n )\n except (ConnectionRefusedError, TimeoutError, OSError) as exception:\n LOG.error(exception)\n return None\n\n try:\n await protocol.set_version(version)\n except QRTCommandException as exception:\n LOG.error(Exception)\n return None\n except TypeError as exception: # TODO: fix test requiring this (test_connect_set_version)\n LOG.error(exception)\n return None\n\n return QRTConnection(protocol, timeout=timeout)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def qtm_version(self):\n return await asyncio.wait_for(\n self._protocol.send_command(\"qtmversion\"), timeout=self._timeout\n )", "response": "Get the QTM version."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def byte_order(self):\n return await asyncio.wait_for(\n self._protocol.send_command(\"byteorder\"), timeout=self._timeout\n )", "response": "Get the byte order used when communicating with the ISO."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the latest state of QTM.", "response": "async def get_state(self):\n \"\"\"Get the latest state change of QTM. If the :func:`~qtm.connect` on_event\n callback was set the callback will be called as well.\n\n :rtype: A :class:`qtm.QRTEvent`\n \"\"\"\n await self._protocol.send_command(\"getstate\", callback=False)\n return await self._protocol.await_event()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwaiting for an event from QTM.", "response": "async def await_event(self, event=None, timeout=30):\n \"\"\"Wait for an event from QTM.\n\n :param event: A :class:`qtm.QRTEvent`\n to wait for a specific event. Otherwise wait for any event.\n\n :param timeout: Max time to wait for event.\n\n :rtype: A :class:`qtm.QRTEvent`\n \"\"\"\n return await self._protocol.await_event(event, timeout=timeout)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def get_parameters(self, parameters=None):\n\n if parameters is None:\n parameters = [\"all\"]\n else:\n for parameter in parameters:\n if not parameter in [\n \"all\",\n \"general\",\n \"3d\",\n \"6d\",\n \"analog\",\n \"force\",\n \"gazevector\",\n \"image\",\n \"skeleton\",\n \"skeleton:global\",\n ]:\n raise QRTCommandException(\"%s is not a valid parameter\" % parameter)\n\n cmd = \"getparameters %s\" % \" \".join(parameters)\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )", "response": "Get the settings for the requested component of QTM."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def get_current_frame(self, components=None) -> QRTPacket:\n\n if components is None:\n components = [\"all\"]\n else:\n _validate_components(components)\n\n cmd = \"getcurrentframe %s\" % \" \".join(components)\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )", "response": "Get the current frame from QTM."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def stream_frames(self, frames=\"allframes\", components=None, on_packet=None):\n\n if components is None:\n components = [\"all\"]\n else:\n _validate_components(components)\n\n self._protocol.set_on_packet(on_packet)\n\n cmd = \"streamframes %s %s\" % (frames, \" \".join(components))\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )", "response": "Stream measured frames from QTM until the connection is closed."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntakes control of the current user.", "response": "async def take_control(self, password):\n \"\"\"Take control of QTM.\n\n :param password: Password as entered in QTM.\n \"\"\"\n cmd = \"takecontrol %s\" % password\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreleases control of the current user s system.", "response": "async def release_control(self):\n \"\"\"Release control of QTM.\n \"\"\"\n\n cmd = \"releasecontrol\"\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nstarting RT from file.", "response": "async def start(self, rtfromfile=False):\n \"\"\"Start RT from file. You need to be in control of QTM to be able to do this.\n \"\"\"\n cmd = \"start\" + (\" rtfromfile\" if rtfromfile else \"\")\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloads a measurement. :param filename: Path to measurement you want to load.", "response": "async def load(self, filename):\n \"\"\"Load a measurement.\n\n :param filename: Path to measurement you want to load.\n \"\"\"\n cmd = \"load %s\" % filename\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def save(self, filename, overwrite=False):\n cmd = \"save %s%s\" % (filename, \" overwrite\" if overwrite else \"\")\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )", "response": "Save a measurement.\n\n :param filename: Filename you wish to save as.\n :param overwrite: If QTM should overwrite existing measurement."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload a project. :param project_path: Path to project you want to load.", "response": "async def load_project(self, project_path):\n \"\"\"Load a project.\n\n :param project_path: Path to project you want to load.\n \"\"\"\n cmd = \"loadproject %s\" % project_path\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets event in QTM.", "response": "async def set_qtm_event(self, event=None):\n \"\"\"Set event in QTM.\"\"\"\n cmd = \"event%s\" % (\"\" if event is None else \" \" + event)\n return await asyncio.wait_for(\n self._protocol.send_command(cmd), timeout=self._timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsend an XML document to the QTM RT.", "response": "async def send_xml(self, xml):\n \"\"\"Used to update QTM settings, see QTM RT protocol for more information.\n\n :param xml: XML document as a str. See QTM RT Documentation for details.\n \"\"\"\n return await asyncio.wait_for(\n self._protocol.send_command(xml, command_type=QRTPacketType.PacketXML),\n timeout=self._timeout,\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef data_received(self, data):\n self._received_data += data\n h_size = RTheader.size\n\n data = self._received_data\n size, type_ = RTheader.unpack_from(data, 0)\n\n while len(data) >= size:\n self._parse_received(data[h_size:size], type_)\n data = data[size:]\n\n if len(data) < h_size:\n break\n\n size, type_ = RTheader.unpack_from(data, 0)\n\n self._received_data = data", "response": "Parse the received data from QTM and route accordingly"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a single analog data channel.", "response": "def get_analog_single(\n self, component_info=None, data=None, component_position=None\n ):\n \"\"\"Get a single analog data channel.\"\"\"\n components = []\n append_components = components.append\n for _ in range(component_info.device_count):\n component_position, device = QRTPacket._get_exact(\n RTAnalogDeviceSingle, data, component_position\n )\n\n RTAnalogDeviceSamples.format = struct.Struct(\n RTAnalogDeviceSamples.format_str % device.channel_count\n )\n component_position, sample = QRTPacket._get_tuple(\n RTAnalogDeviceSamples, data, component_position\n )\n append_components((device, sample))\n return components"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_force_single(self, component_info=None, data=None, component_position=None):\n components = []\n append_components = components.append\n for _ in range(component_info.plate_count):\n component_position, plate = QRTPacket._get_exact(\n RTForcePlateSingle, data, component_position\n )\n component_position, force = QRTPacket._get_exact(\n RTForce, data, component_position\n )\n append_components((plate, force))\n return components", "response": "Get a single force data channel."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_6d_euler(self, component_info=None, data=None, component_position=None):\n components = []\n append_components = components.append\n for _ in range(component_info.body_count):\n component_position, position = QRTPacket._get_exact(\n RT6DBodyPosition, data, component_position\n )\n component_position, euler = QRTPacket._get_exact(\n RT6DBodyEuler, data, component_position\n )\n append_components((position, euler))\n return components", "response": "Get 6D data with euler rotations."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_3d_markers_residual(\n self, component_info=None, data=None, component_position=None\n ):\n \"\"\"Get 3D markers with residual.\"\"\"\n return self._get_3d_markers(\n RT3DMarkerPositionResidual, component_info, data, component_position\n )", "response": "Get 3D markers with residual."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting 3D markers without label.", "response": "def get_3d_markers_no_label(\n self, component_info=None, data=None, component_position=None\n ):\n \"\"\"Get 3D markers without label.\"\"\"\n return self._get_3d_markers(\n RT3DMarkerPositionNoLabel, component_info, data, component_position\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget 3D markers without label with residual.", "response": "def get_3d_markers_no_label_residual(\n self, component_info=None, data=None, component_position=None\n ):\n \"\"\"Get 3D markers without label with residual.\"\"\"\n return self._get_3d_markers(\n RT3DMarkerPositionNoLabelResidual, component_info, data, component_position\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_2d_markers(\n self, component_info=None, data=None, component_position=None, index=None\n ):\n \"\"\"Get 2D markers.\n\n :param index: Specify which camera to get 2D from, will be returned as\n first entry in the returned array.\n \"\"\"\n return self._get_2d_markers(\n data, component_info, component_position, index=index\n )", "response": "Get 2D markers.\n\n :param index: Specify which camera to get 2D from, will be returned as\n first entry in the returned array."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_2d_markers_linearized(\n self, component_info=None, data=None, component_position=None, index=None\n ):\n \"\"\"Get 2D linearized markers.\n\n :param index: Specify which camera to get 2D from, will be returned as\n first entry in the returned array.\n \"\"\"\n\n return self._get_2d_markers(\n data, component_info, component_position, index=index\n )", "response": "Get 2D linearized markers."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_skeletons(self, component_info=None, data=None, component_position=None):\n\n components = []\n append_components = components.append\n for _ in range(component_info.skeleton_count):\n component_position, info = QRTPacket._get_exact(\n RTSegmentCount, data, component_position\n )\n\n segments = []\n for __ in range(info.segment_count):\n component_position, segment = QRTPacket._get_exact(\n RTSegmentId, data, component_position\n )\n component_position, position = QRTPacket._get_exact(\n RTSegmentPosition, data, component_position\n )\n component_position, rotation = QRTPacket._get_exact(\n RTSegmentRotation, data, component_position\n )\n\n segments.append((segment.id, position, rotation))\n append_components(segments)\n return components", "response": "Get skeletons from the component info and data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def await_event(self, event=None, timeout=None):\n if self.event_future is not None:\n raise Exception(\"Can't wait on multiple events!\")\n\n result = await asyncio.wait_for(self._wait_loop(event), timeout)\n return result", "response": "Wait for an event or timeout."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef send_command(\n self, command, callback=True, command_type=QRTPacketType.PacketCommand\n ):\n \"\"\" Sends commands to QTM \"\"\"\n if self.transport is not None:\n cmd_length = len(command)\n LOG.debug(\"S: %s\", command)\n self.transport.write(\n struct.pack(\n RTCommand % cmd_length,\n RTheader.size + cmd_length + 1,\n command_type.value,\n command.encode(),\n b\"\\0\",\n )\n )\n\n future = self.loop.create_future()\n if callback:\n self.request_queue.append(future)\n else:\n future.set_result(None)\n return future\n\n raise QRTCommandException(\"Not connected!\")", "response": "Sends a command to the QTM."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def reboot(ip_address):\n _, protocol = await asyncio.get_event_loop().create_datagram_endpoint(\n QRebootProtocol,\n local_addr=(ip_address, 0),\n allow_broadcast=True,\n reuse_address=True,\n )\n\n LOG.info(\"Sending reboot on %s\", ip_address)\n protocol.send_reboot()", "response": "async function to reboot QTM cameras"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef on_packet(packet):\n print(\"Framenumber: {}\".format(packet.framenumber))\n header, markers = packet.get_3d_markers()\n print(\"Component info: {}\".format(header))\n for marker in markers:\n print(\"\\t\", marker)", "response": "Callback function that is called every time a data packet arrives from QTM"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def setup():\n connection = await qtm.connect(\"127.0.0.1\")\n if connection is None:\n return\n\n await connection.stream_frames(components=[\"3d\"], on_packet=on_packet)", "response": "Setup the connection to the server"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses a datagram from QTM and send it to the receiver.", "response": "def datagram_received(self, datagram, address):\n \"\"\" Parse response from QTM instances \"\"\"\n size, _ = RTheader.unpack_from(datagram, 0)\n info, = struct.unpack_from(\"{0}s\".format(size - 3 - 8), datagram, RTheader.size)\n base_port, = QRTDiscoveryBasePort.unpack_from(datagram, size - 2)\n\n if self.receiver is not None:\n self.receiver(QRTDiscoveryResponse(info, address[0], base_port))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef send_discovery_packet(self):\n if self.port is None:\n return\n\n self.transport.sendto(\n QRTDiscoveryP1.pack(\n QRTDiscoveryPacketSize, QRTPacketType.PacketDiscover.value\n )\n + QRTDiscoveryP2.pack(self.port),\n (\"\", 22226),\n )", "response": "Send discovery packet for QTM to respond to"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def choose_qtm_instance(interface):\n instances = {}\n print(\"Available QTM instances:\")\n async for i, qtm_instance in AsyncEnumerate(qtm.Discover(interface), start=1):\n instances[i] = qtm_instance\n print(\"{} - {}\".format(i, qtm_instance.info))\n\n try:\n\n choice = int(input(\"Connect to: \"))\n\n if choice not in instances:\n raise ValueError\n\n except ValueError:\n LOG.error(\"Invalid choice\")\n return None\n\n return instances[choice].host", "response": "List running QTM instances asks for input and return chosen QTM"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def package_receiver(queue):\n LOG.info(\"Entering package_receiver\")\n while True:\n packet = await queue.get()\n if packet is None:\n break\n\n LOG.info(\"Framenumber %s\", packet.framenumber)\n header, cameras = packet.get_2d_markers()\n LOG.info(\"Component info: %s\", header)\n\n for i, camera in enumerate(cameras, 1):\n LOG.info(\"Camera %d\", i)\n for marker in camera:\n LOG.info(\"\\t%s\", marker)\n\n LOG.info(\"Exiting package_receiver\")", "response": "Asynchronous function that processes queue until None is posted in queue"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def setup():\n\n connection = await qtm.connect(\"127.0.0.1\")\n\n if connection is None:\n return -1\n\n async with qtm.TakeControl(connection, \"password\"):\n\n state = await connection.get_state()\n if state != qtm.QRTEvent.EventConnected:\n await connection.new()\n try:\n await connection.await_event(qtm.QRTEvent.EventConnected, timeout=10)\n except asyncio.TimeoutError:\n LOG.error(\"Failed to start new measurement\")\n return -1\n\n queue = asyncio.Queue()\n\n receiver_future = asyncio.ensure_future(package_receiver(queue))\n\n await connection.stream_frames(components=[\"2d\"], on_packet=queue.put_nowait)\n\n asyncio.ensure_future(shutdown(30, connection, receiver_future, queue))", "response": "main function for the sequence of new items"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_body_index(xml_string):\n xml = ET.fromstring(xml_string)\n\n body_to_index = {}\n for index, body in enumerate(xml.findall(\"*/Body/Name\")):\n body_to_index[body.text.strip()] = index\n\n return body_to_index", "response": "Extract a name to index dictionary from 6dof settings xml"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntry to find executable in the directories listed in path.", "response": "def find_executable(executable, path=None):\n '''Try to find 'executable' in the directories listed in 'path' (a\n string listing directories separated by 'os.pathsep'; defaults to\n os.environ['PATH']).'''\n if path is None:\n path = os.environ['PATH']\n paths = path.split(os.pathsep)\n extlist = ['']\n if os.name == 'os2':\n ext = os.path.splitext(executable)\n # executable files on OS/2 can have an arbitrary extension, but\n # .exe is automatically appended if no dot is present in the name\n if not ext:\n executable = executable + \".exe\"\n elif sys.platform == 'win32':\n pathext = os.environ['PATHEXT'].lower().split(os.pathsep)\n ext = os.path.splitext(executable)\n if ext not in pathext:\n extlist = pathext\n for ext in extlist:\n execname = executable + ext\n if os.path.isfile(execname):\n return execname\n else:\n for pth in paths:\n fil = os.path.join(pth, execname)\n if os.path.isfile(fil):\n return fil\n break\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn true if substring is in string for files in specified path", "response": "def find_x(path1):\n '''Return true if substring is in string for files\n in specified path'''\n libs = os.listdir(path1)\n for lib_dir in libs:\n if \"doublefann\" in lib_dir:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef build_swig():\n '''Run SWIG with specified parameters'''\n print(\"Looking for FANN libs...\")\n find_fann()\n print(\"running SWIG...\")\n swig_bin = find_swig()\n swig_cmd = [swig_bin, '-c++', '-python', 'fann2/fann2.i']\n subprocess.Popen(swig_cmd).wait()", "response": "Run SWIG with specified parameters"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get(ctx, job):\n\n def get_experiment():\n try:\n response = PolyaxonClient().experiment.get_experiment(user, project_name, _experiment)\n cache.cache(config_manager=ExperimentManager, response=response)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not load experiment `{}` info.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_experiment_details(response)\n\n def get_experiment_job():\n try:\n response = PolyaxonClient().experiment_job.get_job(user,\n project_name,\n _experiment,\n _job)\n cache.cache(config_manager=ExperimentJobManager, response=response)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.resources:\n get_resources(response.resources.to_dict(), header=\"Job resources:\")\n\n response = Printer.add_status_color(response.to_light_dict(\n humanize_values=True,\n exclude_attrs=['uuid', 'definition', 'experiment', 'unique_name', 'resources']\n ))\n Printer.print_header(\"Job info:\")\n dict_tabulate(response)\n\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n\n if job:\n _job = get_experiment_job_or_local(job)\n get_experiment_job()\n else:\n get_experiment()", "response": "Get an entire sequence of items."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete(ctx):\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n if not click.confirm(\"Are sure you want to delete experiment `{}`\".format(_experiment)):\n click.echo('Existing without deleting experiment.')\n sys.exit(1)\n\n try:\n response = PolyaxonClient().experiment.delete_experiment(\n user, project_name, _experiment)\n # Purge caching\n ExperimentManager.purge()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 204:\n Printer.print_success(\"Experiment `{}` was delete successfully\".format(_experiment))", "response": "Delete experiment.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon experiment delete\n ```"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating an existing resource in the project.", "response": "def update(ctx, name, description, tags):\n \"\"\"Update experiment.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon experiment -xp 2 update --description=\"new description for my experiments\"\n ```\n\n \\b\n ```bash\n $ polyaxon experiment -xp 2 update --tags=\"foo, bar\" --name=\"unique-name\"\n ```\n \"\"\"\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n update_dict = {}\n\n if name:\n update_dict['name'] = name\n\n if description:\n update_dict['description'] = description\n\n tags = validate_tags(tags)\n if tags:\n update_dict['tags'] = tags\n\n if not update_dict:\n Printer.print_warning('No argument was provided to update the experiment.')\n sys.exit(0)\n\n try:\n response = PolyaxonClient().experiment.update_experiment(\n user, project_name, _experiment, update_dict)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not update experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Experiment updated.\")\n get_experiment_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstopping experiment. Uses [Caching](/references/polyaxon-cli/#caching) Examples: \\b ```bash $ polyaxon experiment stop ``` \\b ```bash $ polyaxon experiment -xp 2 stop ```", "response": "def stop(ctx, yes):\n \"\"\"Stop experiment.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon experiment stop\n ```\n\n \\b\n ```bash\n $ polyaxon experiment -xp 2 stop\n ```\n \"\"\"\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n if not yes and not click.confirm(\"Are sure you want to stop \"\n \"experiment `{}`\".format(_experiment)):\n click.echo('Existing without stopping experiment.')\n sys.exit(0)\n\n try:\n PolyaxonClient().experiment.stop(user, project_name, _experiment)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Experiment is being stopped.\")"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrestart the current node.", "response": "def restart(ctx, copy, file, u): # pylint:disable=redefined-builtin\n \"\"\"Restart experiment.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon experiment --experiment=1 restart\n ```\n \"\"\"\n config = None\n update_code = None\n if file:\n config = rhea.read(file)\n\n # Check if we need to upload\n if u:\n ctx.invoke(upload, sync=False)\n update_code = True\n\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n try:\n if copy:\n response = PolyaxonClient().experiment.copy(\n user, project_name, _experiment, config=config, update_code=update_code)\n Printer.print_success('Experiment was copied with id {}'.format(response.id))\n else:\n response = PolyaxonClient().experiment.restart(\n user, project_name, _experiment, config=config, update_code=update_code)\n Printer.print_success('Experiment was restarted with id {}'.format(response.id))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not restart experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the statuses of the specified experiment or job.", "response": "def statuses(ctx, job, page):\n \"\"\"Get experiment or experiment job statuses.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples getting experiment statuses:\n\n \\b\n ```bash\n $ polyaxon experiment statuses\n ```\n\n \\b\n ```bash\n $ polyaxon experiment -xp 1 statuses\n ```\n\n Examples getting experiment job statuses:\n\n \\b\n ```bash\n $ polyaxon experiment statuses -j 3\n ```\n\n \\b\n ```bash\n $ polyaxon experiment -xp 1 statuses --job 1\n ```\n \"\"\"\n\n def get_experiment_statuses():\n try:\n response = PolyaxonClient().experiment.get_statuses(\n user, project_name, _experiment, page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could get status for experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Statuses for experiment `{}`.'.format(_experiment))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No statuses found for experiment `{}`.'.format(_experiment))\n\n objects = list_dicts_to_tabulate(\n [Printer.add_status_color(o.to_light_dict(humanize_values=True), status_key='status')\n for o in response['results']])\n if objects:\n Printer.print_header(\"Statuses:\")\n objects.pop('experiment', None)\n dict_tabulate(objects, is_list_dict=True)\n\n def get_experiment_job_statuses():\n try:\n response = PolyaxonClient().experiment_job.get_statuses(user,\n project_name,\n _experiment,\n _job,\n page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get status for job `{}`.'.format(job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Statuses for Job `{}`.'.format(_job))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No statuses found for job `{}`.'.format(_job))\n\n objects = list_dicts_to_tabulate(\n [Printer.add_status_color(o.to_light_dict(humanize_values=True), status_key='status')\n for o in response['results']])\n if objects:\n Printer.print_header(\"Statuses:\")\n objects.pop('job', None)\n dict_tabulate(objects, is_list_dict=True)\n\n page = page or 1\n\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n\n if job:\n _job = get_experiment_job_or_local(job)\n get_experiment_job_statuses()\n else:\n get_experiment_statuses()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef resources(ctx, job, gpu):\n\n def get_experiment_resources():\n try:\n message_handler = Printer.gpu_resources if gpu else Printer.resources\n PolyaxonClient().experiment.resources(\n user, project_name, _experiment, message_handler=message_handler)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get resources for experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n def get_experiment_job_resources():\n try:\n message_handler = Printer.gpu_resources if gpu else Printer.resources\n PolyaxonClient().experiment_job.resources(user,\n project_name,\n _experiment,\n _job,\n message_handler=message_handler)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get resources for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n\n if job:\n _job = get_experiment_job_or_local(job)\n get_experiment_job_resources()\n else:\n get_experiment_resources()", "response": "Get all resources for a specific experiment or job."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget logs for a specific experiment or job.", "response": "def logs(ctx, job, past, follow, hide_time):\n \"\"\"Get experiment or experiment job logs.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples for getting experiment logs:\n\n \\b\n ```bash\n $ polyaxon experiment logs\n ```\n\n \\b\n ```bash\n $ polyaxon experiment -xp 10 -p mnist logs\n ```\n\n Examples for getting experiment job logs:\n\n \\b\n ```bash\n $ polyaxon experiment -xp 1 -j 1 logs\n ```\n \"\"\"\n\n def get_experiment_logs():\n if past:\n try:\n response = PolyaxonClient().experiment.logs(\n user, project_name, _experiment, stream=False)\n get_logs_handler(handle_job_info=True,\n show_timestamp=not hide_time,\n stream=False)(response.content.decode().split('\\n'))\n print()\n\n if not follow:\n return\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n if not follow:\n Printer.print_error(\n 'Could not get logs for experiment `{}`.'.format(_experiment))\n Printer.print_error(\n 'Error message `{}`.'.format(e))\n sys.exit(1)\n\n try:\n PolyaxonClient().experiment.logs(\n user,\n project_name,\n _experiment,\n message_handler=get_logs_handler(handle_job_info=True,\n show_timestamp=not hide_time))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get logs for experiment `{}`.'.format(_experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n def get_experiment_job_logs():\n if past:\n try:\n response = PolyaxonClient().experiment_job.logs(\n user,\n project_name,\n _experiment,\n _job,\n stream=False)\n get_logs_handler(handle_job_info=True,\n show_timestamp=not hide_time,\n stream=False)(response.content.decode().split('\\n'))\n print()\n\n if not follow:\n return\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n if not follow:\n Printer.print_error(\n 'Could not get logs for experiment `{}`.'.format(_experiment))\n Printer.print_error(\n 'Error message `{}`.'.format(e))\n sys.exit(1)\n\n try:\n PolyaxonClient().experiment_job.logs(\n user,\n project_name,\n _experiment,\n _job,\n message_handler=get_logs_handler(handle_job_info=True,\n show_timestamp=not hide_time))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get logs for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),\n ctx.obj.get('experiment'))\n\n if job:\n _job = get_experiment_job_or_local(job)\n get_experiment_job_logs()\n else:\n get_experiment_logs()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upload(sync=True): # pylint:disable=assign-to-new-keyword\n project = ProjectManager.get_config_or_raise()\n files = IgnoreManager.get_unignored_file_paths()\n try:\n with create_tarfile(files, project.name) as file_path:\n with get_files_in_current_directory('repo', [file_path]) as (files, files_size):\n try:\n PolyaxonClient().project.upload_repo(project.user,\n project.name,\n files,\n files_size,\n sync=sync)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error(\n 'Could not upload code for project `{}`.'.format(project.name))\n Printer.print_error('Error message `{}`.'.format(e))\n Printer.print_error(\n 'Check the project exists, '\n 'and that you have access rights, '\n 'this could happen as well when uploading large files.'\n 'If you are running a notebook and mounting the code to the notebook, '\n 'you should stop it before uploading.')\n sys.exit(1)\n Printer.print_success('Files uploaded.')\n except Exception as e:\n Printer.print_error(\"Could not upload the file.\")\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)", "response": "Upload code of the current directory while respecting the. polyaxonignore file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cluster(node):\n cluster_client = PolyaxonClient().cluster\n if node:\n try:\n node_config = cluster_client.get_node(node)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not load node `{}` info.'.format(node))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n get_node_info(node_config)\n else:\n try:\n cluster_config = cluster_client.get_cluster()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not load cluster info.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n get_cluster_info(cluster_config)", "response": "Get cluster and nodes info."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef clean_outputs(fn):\n\n @wraps(fn)\n def clean_outputs_wrapper(*args, **kwargs):\n try:\n return fn(*args, **kwargs)\n except SystemExit as e:\n sys.stdout = StringIO()\n sys.exit(e.code) # make sure we still exit with the proper code\n except Exception as e:\n sys.stdout = StringIO()\n raise e\n\n return clean_outputs_wrapper", "response": "Decorator for CLI with Sentry client handling."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get(ctx):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n try:\n response = PolyaxonClient().job.get_job(user, project_name, _job)\n cache.cache(config_manager=JobManager, response=response)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_job_details(response)", "response": "Get a single job."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete(ctx):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n if not click.confirm(\"Are sure you want to delete job `{}`\".format(_job)):\n click.echo('Existing without deleting job.')\n sys.exit(1)\n\n try:\n response = PolyaxonClient().job.delete_job(\n user, project_name, _job)\n # Purge caching\n JobManager.purge()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 204:\n Printer.print_success(\"Job `{}` was delete successfully\".format(_job))", "response": "Delete job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon job delete\n ```"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating job. Uses [Caching](/references/polyaxon-cli/#caching) Example: \\b ```bash $ polyaxon job -j 2 update --description=\"new description for my job\" ```", "response": "def update(ctx, name, description, tags):\n \"\"\"Update job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon job -j 2 update --description=\"new description for my job\"\n ```\n \"\"\"\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n update_dict = {}\n\n if name:\n update_dict['name'] = name\n\n if description:\n update_dict['description'] = description\n\n tags = validate_tags(tags)\n if tags:\n update_dict['tags'] = tags\n\n if not update_dict:\n Printer.print_warning('No argument was provided to update the job.')\n sys.exit(0)\n\n try:\n response = PolyaxonClient().job.update_job(\n user, project_name, _job, update_dict)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not update job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Job updated.\")\n get_job_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef stop(ctx, yes):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n if not yes and not click.confirm(\"Are sure you want to stop \"\n \"job `{}`\".format(_job)):\n click.echo('Existing without stopping job.')\n sys.exit(0)\n\n try:\n PolyaxonClient().job.stop(user, project_name, _job)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Job is being stopped.\")", "response": "Stop a running job."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nrestarts the current node.", "response": "def restart(ctx, copy, file, u): # pylint:disable=redefined-builtin\n \"\"\"Restart job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon job --job=1 restart\n ```\n \"\"\"\n config = None\n update_code = None\n if file:\n config = rhea.read(file)\n\n # Check if we need to upload\n if u:\n ctx.invoke(upload, sync=False)\n update_code = True\n\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n try:\n if copy:\n response = PolyaxonClient().job.copy(\n user, project_name, _job, config=config, update_code=update_code)\n else:\n response = PolyaxonClient().job.restart(\n user, project_name, _job, config=config, update_code=update_code)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not restart job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_job_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef statuses(ctx, page):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n page = page or 1\n try:\n response = PolyaxonClient().job.get_statuses(user, project_name, _job, page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get status for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Statuses for Job `{}`.'.format(_job))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No statuses found for job `{}`.'.format(_job))\n\n objects = list_dicts_to_tabulate(\n [Printer.add_status_color(o.to_light_dict(humanize_values=True), status_key='status')\n for o in response['results']])\n if objects:\n Printer.print_header(\"Statuses:\")\n objects.pop('job', None)\n dict_tabulate(objects, is_list_dict=True)", "response": "Get job statuses.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon job -j 2 statuses\n ```"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef resources(ctx, gpu):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n try:\n message_handler = Printer.gpu_resources if gpu else Printer.resources\n PolyaxonClient().job.resources(user,\n project_name,\n _job,\n message_handler=message_handler)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get resources for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)", "response": "Get job resources.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon job -j 2 resources\n ```\n\n For GPU resources\n\n \\b\n ```bash\n $ polyaxon job -j 2 resources --gpu\n ```"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets job logs. Uses [Caching](/references/polyaxon-cli/#caching) Examples: \\b ```bash $ polyaxon job -j 2 logs ``` \\b ```bash $ polyaxon job logs ```", "response": "def logs(ctx, past, follow, hide_time):\n \"\"\"Get job logs.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon job -j 2 logs\n ```\n\n \\b\n ```bash\n $ polyaxon job logs\n ```\n \"\"\"\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n\n if past:\n try:\n response = PolyaxonClient().job.logs(\n user, project_name, _job, stream=False)\n get_logs_handler(handle_job_info=False,\n show_timestamp=not hide_time,\n stream=False)(response.content.decode().split('\\n'))\n print()\n\n if not follow:\n return\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n if not follow:\n Printer.print_error('Could not get logs for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n try:\n PolyaxonClient().job.logs(\n user,\n project_name,\n _job,\n message_handler=get_logs_handler(handle_job_info=False, show_timestamp=not hide_time))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get logs for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef outputs(ctx):\n user, project_name, _job = get_job_or_local(ctx.obj.get('project'), ctx.obj.get('job'))\n try:\n PolyaxonClient().job.download_outputs(user, project_name, _job)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not download outputs for job `{}`.'.format(_job))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n Printer.print_success('Files downloaded.')", "response": "Download outputs for a specific job."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pprint(value):\n click.echo(\n json.dumps(value,\n sort_keys=True,\n indent=4,\n separators=(',', ': ')))", "response": "Prints as formatted JSON"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef whoami():\n try:\n user = PolyaxonClient().auth.get_user()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not load user info.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n click.echo(\"\\nUsername: {username}, Email: {email}\\n\".format(**user.to_dict()))", "response": "Show current logged Polyaxon user."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncommands for build jobs.", "response": "def build(ctx, project, build): # pylint:disable=redefined-outer-name\n \"\"\"Commands for build jobs.\"\"\"\n ctx.obj = ctx.obj or {}\n ctx.obj['project'] = project\n ctx.obj['build'] = build"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a build job.", "response": "def get(ctx):\n \"\"\"Get build job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon build -b 1 get\n ```\n\n \\b\n ```bash\n $ polyaxon build --build=1 --project=project_name get\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n try:\n response = PolyaxonClient().build_job.get_build(user, project_name, _build)\n cache.cache(config_manager=BuildJobManager, response=response)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get build job `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_build_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a build job.", "response": "def delete(ctx):\n \"\"\"Delete build job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon build delete\n ```\n\n \\b\n ```bash\n $ polyaxon build -b 2 delete\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n if not click.confirm(\"Are sure you want to delete build job `{}`\".format(_build)):\n click.echo('Existing without deleting build job.')\n sys.exit(1)\n\n try:\n response = PolyaxonClient().build_job.delete_build(\n user, project_name, _build)\n # Purge caching\n BuildJobManager.purge()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete job `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 204:\n Printer.print_success(\"Build job `{}` was deleted successfully\".format(_build))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate build. Uses [Caching](/references/polyaxon-cli/#caching) Example: \\b ```bash $ polyaxon build -b 2 update --description=\"new description for my build\" ```", "response": "def update(ctx, name, description, tags):\n \"\"\"Update build.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon build -b 2 update --description=\"new description for my build\"\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n update_dict = {}\n\n if name:\n update_dict['name'] = name\n\n if description:\n update_dict['description'] = description\n\n tags = validate_tags(tags)\n if tags:\n update_dict['tags'] = tags\n\n if not update_dict:\n Printer.print_warning('No argument was provided to update the build.')\n sys.exit(0)\n\n try:\n response = PolyaxonClient().build_job.update_build(\n user, project_name, _build, update_dict)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not update build `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Build updated.\")\n get_build_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nstops a build job.", "response": "def stop(ctx, yes):\n \"\"\"Stop build job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon build stop\n ```\n\n \\b\n ```bash\n $ polyaxon build -b 2 stop\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n if not yes and not click.confirm(\"Are sure you want to stop \"\n \"job `{}`\".format(_build)):\n click.echo('Existing without stopping build job.')\n sys.exit(0)\n\n try:\n PolyaxonClient().build_job.stop(user, project_name, _build)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop build job `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Build job is being stopped.\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nbookmark build job. Uses [Caching](/references/polyaxon-cli/#caching) Examples: \\b ```bash $ polyaxon build bookmark ``` \\b ```bash $ polyaxon build -b 2 bookmark ```", "response": "def bookmark(ctx):\n \"\"\"Bookmark build job.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon build bookmark\n ```\n\n \\b\n ```bash\n $ polyaxon build -b 2 bookmark\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n try:\n PolyaxonClient().build_job.bookmark(user, project_name, _build)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not bookmark build job `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Build job bookmarked.\")"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget build job resources.", "response": "def resources(ctx, gpu):\n \"\"\"Get build job resources.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon build -b 2 resources\n ```\n\n For GPU resources\n\n \\b\n ```bash\n $ polyaxon build -b 2 resources --gpu\n ```\n \"\"\"\n user, project_name, _build = get_build_or_local(ctx.obj.get('project'), ctx.obj.get('build'))\n try:\n message_handler = Printer.gpu_resources if gpu else Printer.resources\n PolyaxonClient().build_job.resources(user,\n project_name,\n _build,\n message_handler=message_handler)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get resources for build job `{}`.'.format(_build))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init(project, polyaxonfile):\n user, project_name = get_project_or_local(project)\n try:\n project_config = PolyaxonClient().project.get_project(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Make sure you have a project with this name `{}`'.format(project))\n Printer.print_error(\n 'You can a create new project with this command: '\n 'polyaxon project create '\n '--name={} [--description=...] [--tags=...]'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n init_project = False\n if ProjectManager.is_initialized():\n local_project = ProjectManager.get_config()\n click.echo('Warning! This project is already initialized with the following project:')\n with clint.textui.indent(4):\n clint.textui.puts('User: {}'.format(local_project.user))\n clint.textui.puts('Project: {}'.format(local_project.name))\n if click.confirm('Would you like to override this current config?', default=False):\n init_project = True\n else:\n init_project = True\n\n if init_project:\n ProjectManager.purge()\n ProjectManager.set_config(project_config, init=True)\n Printer.print_success('Project was initialized')\n else:\n Printer.print_header('Project config was not changed.')\n\n init_ignore = False\n if IgnoreManager.is_initialized():\n click.echo('Warning! Found a .polyaxonignore file.')\n if click.confirm('Would you like to override it?', default=False):\n init_ignore = True\n else:\n init_ignore = True\n\n if init_ignore:\n IgnoreManager.init_config()\n Printer.print_success('New .polyaxonignore file was created.')\n else:\n Printer.print_header('.polyaxonignore file was not changed.')\n\n if polyaxonfile:\n create_polyaxonfile()", "response": "Initialize a new polyaxonfile specification."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run(ctx, project, file, name, tags, description, ttl, u, l): # pylint:disable=redefined-builtin\n if not file:\n file = PolyaxonFile.check_default_path(path='.')\n if not file:\n file = ''\n specification = check_polyaxonfile(file, log=False).specification\n\n spec_cond = (specification.is_experiment or\n specification.is_group or\n specification.is_job or\n specification.is_build)\n if not spec_cond:\n Printer.print_error(\n 'This command expects an experiment, a group, a job, or a build specification,'\n 'received instead a `{}` specification'.format(specification.kind))\n if specification.is_notebook:\n click.echo('Please check \"polyaxon notebook --help\" to start a notebook.')\n elif specification.is_tensorboard:\n click.echo('Please check: \"polyaxon tensorboard --help\" to start a tensorboard.')\n sys.exit(1)\n\n # Check if we need to upload\n if u:\n if project:\n Printer.print_error('Uploading is not supported when switching project context!')\n click.echo('Please, either omit the `-u` option or `-p` / `--project=` option.')\n sys.exit(1)\n ctx.invoke(upload, sync=False)\n\n user, project_name = get_project_or_local(project)\n project_client = PolyaxonClient().project\n\n tags = validate_tags(tags)\n\n def run_experiment():\n click.echo('Creating an independent experiment.')\n experiment = ExperimentConfig(\n name=name,\n description=description,\n tags=tags,\n config=specification.parsed_data,\n ttl=ttl)\n try:\n response = PolyaxonClient().project.create_experiment(user,\n project_name,\n experiment)\n cache.cache(config_manager=ExperimentManager, response=response)\n Printer.print_success('Experiment `{}` was created'.format(response.id))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not create experiment.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n def run_group():\n click.echo('Creating an experiment group with the following definition:')\n experiments_def = specification.experiments_def\n get_group_experiments_info(**experiments_def)\n experiment_group = ExperimentGroupConfig(\n name=name,\n description=description,\n tags=tags,\n content=specification._data) # pylint:disable=protected-access\n try:\n response = project_client.create_experiment_group(user,\n project_name,\n experiment_group)\n cache.cache(config_manager=GroupManager, response=response)\n Printer.print_success('Experiment group {} was created'.format(response.id))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not create experiment group.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n def run_job():\n click.echo('Creating a job.')\n job = JobConfig(\n name=name,\n description=description,\n tags=tags,\n config=specification.parsed_data,\n ttl=ttl)\n try:\n response = project_client.create_job(user,\n project_name,\n job)\n cache.cache(config_manager=JobManager, response=response)\n Printer.print_success('Job {} was created'.format(response.id))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not create job.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n def run_build():\n click.echo('Creating a build.')\n job = JobConfig(\n name=name,\n description=description,\n tags=tags,\n config=specification.parsed_data,\n ttl=ttl)\n try:\n response = project_client.create_build(user,\n project_name,\n job)\n cache.cache(config_manager=BuildJobManager, response=response)\n Printer.print_success('Build {} was created'.format(response.id))\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not create build.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n logs = None\n if specification.is_experiment:\n run_experiment()\n logs = experiment_logs\n elif specification.is_group:\n run_group()\n elif specification.is_job:\n run_job()\n logs = job_logs\n elif specification.is_build:\n run_build()\n logs = build_logs\n\n # Check if we need to invoke logs\n if l and logs:\n ctx.obj = {'project': '{}/{}'.format(user, project_name)}\n ctx.invoke(logs)", "response": "Run a single polyaxon file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef projects(ctx, page):\n user = get_username_or_local(ctx.obj.get('username'))\n\n page = page or 1\n try:\n response = PolyaxonClient().bookmark.projects(username=user, page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error(\n 'Could not get bookmarked projects for user `{}`.'.format(user))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Bookmarked projects for user `{}`.'.format(user))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No bookmarked projects found for user `{}`.'.format(user))\n\n objects = [Printer.add_status_color(o.to_light_dict(humanize_values=True))\n for o in response['results']]\n objects = list_dicts_to_tabulate(objects)\n if objects:\n Printer.print_header(\"Projects:\")\n dict_tabulate(objects, is_list_dict=True)", "response": "List bookmarked projects for a user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving trailing spaces unless they are quoted with a backslash.", "response": "def _remove_trailing_spaces(line):\n \"\"\"Remove trailing spaces unless they are quoted with a backslash.\"\"\"\n while line.endswith(' ') and not line.endswith('\\\\ '):\n line = line[:-1]\n return line.replace('\\\\ ', ' ')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nyields all matching patterns for path.", "response": "def find_matching(cls, path, patterns):\n \"\"\"Yield all matching patterns for path.\"\"\"\n for pattern in patterns:\n if pattern.match(path):\n yield pattern"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks whether a path is ignored. For directories include a trailing slash.", "response": "def is_ignored(cls, path, patterns):\n \"\"\"Check whether a path is ignored. For directories, include a trailing slash.\"\"\"\n status = None\n for pattern in cls.find_matching(path, patterns):\n status = pattern.is_exclude\n return status"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _matches_patterns(path, patterns):\n for glob in patterns:\n try:\n if PurePath(path).match(glob):\n return True\n except TypeError:\n pass\n return False", "response": "Returns a if a path matches any pattern."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a whether a path should be ignored or not.", "response": "def _ignore_path(cls, path, ignore_list=None, white_list=None):\n \"\"\"Returns a whether a path should be ignored or not.\"\"\"\n ignore_list = ignore_list or []\n white_list = white_list or []\n return (cls._matches_patterns(path, ignore_list) and\n not cls._matches_patterns(path, white_list))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef group(ctx, project, group): # pylint:disable=redefined-outer-name\n ctx.obj = ctx.obj or {}\n ctx.obj['project'] = project\n ctx.obj['group'] = group", "response": "Commands for experiment groups."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets an experiment group by uuid.", "response": "def get(ctx):\n \"\"\"Get experiment group by uuid.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n \\b\n ```bash\n $ polyaxon group -g 13 get\n ```\n \"\"\"\n user, project_name, _group = get_project_group_or_local(ctx.obj.get('project'),\n ctx.obj.get('group'))\n try:\n response = PolyaxonClient().experiment_group.get_experiment_group(\n user, project_name, _group)\n cache.cache(config_manager=GroupManager, response=response)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get experiment group `{}`.'.format(_group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_group_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete(ctx):\n user, project_name, _group = get_project_group_or_local(ctx.obj.get('project'),\n ctx.obj.get('group'))\n\n if not click.confirm(\"Are sure you want to delete experiment group `{}`\".format(_group)):\n click.echo('Existing without deleting experiment group.')\n sys.exit(0)\n\n try:\n response = PolyaxonClient().experiment_group.delete_experiment_group(\n user, project_name, _group)\n # Purge caching\n GroupManager.purge()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete experiment group `{}`.'.format(_group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 204:\n Printer.print_success(\"Experiment group `{}` was delete successfully\".format(_group))", "response": "Delete an experiment group."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating an existing group.", "response": "def update(ctx, name, description, tags):\n \"\"\"Update experiment group.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon group -g 2 update --description=\"new description for this group\"\n ```\n\n \\b\n ```bash\n $ polyaxon update --tags=\"foo, bar\"\n ```\n \"\"\"\n user, project_name, _group = get_project_group_or_local(ctx.obj.get('project'),\n ctx.obj.get('group'))\n update_dict = {}\n\n if name:\n update_dict['name'] = name\n\n if description:\n update_dict['description'] = description\n\n tags = validate_tags(tags)\n if tags:\n update_dict['tags'] = tags\n\n if not update_dict:\n Printer.print_warning('No argument was provided to update the experiment group.')\n sys.exit(0)\n\n try:\n response = PolyaxonClient().experiment_group.update_experiment_group(\n user, project_name, _group, update_dict)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not update experiment group `{}`.'.format(_group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Experiment group updated.\")\n get_group_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop(ctx, yes, pending):\n user, project_name, _group = get_project_group_or_local(ctx.obj.get('project'),\n ctx.obj.get('group'))\n\n if not yes and not click.confirm(\"Are sure you want to stop experiments \"\n \"in group `{}`\".format(_group)):\n click.echo('Existing without stopping experiments in group.')\n sys.exit(0)\n\n try:\n PolyaxonClient().experiment_group.stop(user, project_name, _group, pending=pending)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop experiments in group `{}`.'.format(_group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Experiments in group are being stopped.\")", "response": "Stop all experiments in the specified group."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bookmark(ctx):\n user, project_name, _group = get_project_group_or_local(ctx.obj.get('project'),\n ctx.obj.get('group'))\n\n try:\n PolyaxonClient().experiment_group.bookmark(user, project_name, _group)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not bookmark group `{}`.'.format(_group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Experiments group is bookmarked.\")", "response": "Bookmark the current group."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset and get the global configurations.", "response": "def config(list): # pylint:disable=redefined-builtin\n \"\"\"Set and get the global configurations.\"\"\"\n if list:\n _config = GlobalConfigManager.get_config_or_default()\n Printer.print_header('Current config:')\n dict_tabulate(_config.to_dict())"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the global config values by keys.", "response": "def get(keys):\n \"\"\"Get the global config values by keys.\n\n Example:\n\n \\b\n ```bash\n $ polyaxon config get host http_port\n ```\n \"\"\"\n _config = GlobalConfigManager.get_config_or_default()\n\n if not keys:\n return\n\n print_values = {}\n for key in keys:\n if hasattr(_config, key):\n print_values[key] = getattr(_config, key)\n else:\n click.echo('Key `{}` is not recognised.'.format(key))\n\n dict_tabulate(print_values, )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set(verbose, # pylint:disable=redefined-builtin\n host,\n http_port,\n ws_port,\n use_https,\n verify_ssl):\n \"\"\"Set the global config values.\n\n Example:\n\n \\b\n ```bash\n $ polyaxon config set --hots=localhost http_port=80\n ```\n \"\"\"\n _config = GlobalConfigManager.get_config_or_default()\n\n if verbose is not None:\n _config.verbose = verbose\n\n if host is not None:\n _config.host = host\n\n if http_port is not None:\n _config.http_port = http_port\n\n if ws_port is not None:\n _config.ws_port = ws_port\n\n if use_https is not None:\n _config.use_https = use_https\n\n if verify_ssl is False:\n _config.verify_ssl = verify_ssl\n\n GlobalConfigManager.set_config(_config)\n Printer.print_success('Config was updated.')\n # Reset cli config\n CliConfigManager.purge()", "response": "Set the global config values."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nactivating a user. Example: \\b ```bash $ polyaxon user activate david ```", "response": "def activate(username):\n \"\"\"Activate a user.\n\n Example:\n\n \\b\n ```bash\n $ polyaxon user activate david\n ```\n \"\"\"\n try:\n PolyaxonClient().user.activate_user(username)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not activate user `{}`.'.format(username))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"User `{}` was activated successfully.\".format(username))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a user. Example: \\b ```bash $ polyaxon user delete david ```", "response": "def delete(username):\n \"\"\"Delete a user.\n\n Example:\n\n \\b\n ```bash\n $ polyaxon user delete david\n ```\n \"\"\"\n try:\n PolyaxonClient().user.delete_user(username)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete user `{}`.'.format(username))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"User `{}` was deleted successfully.\".format(username))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef teardown(file): # pylint:disable=redefined-builtin\n config = read_deployment_config(file)\n manager = DeployManager(config=config, filepath=file)\n exception = None\n try:\n if click.confirm('Would you like to execute pre-delete hooks?', default=True):\n manager.teardown(hooks=True)\n else:\n manager.teardown(hooks=False)\n except Exception as e:\n Printer.print_error('Polyaxon could not teardown the deployment.')\n exception = e\n\n if exception:\n Printer.print_error('Error message `{}`.'.format(exception))", "response": "Teardown a polyaxon deployment given a config file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_tarfile(files, project_name):\n fd, filename = tempfile.mkstemp(prefix=\"polyaxon_{}\".format(project_name), suffix='.tar.gz')\n with tarfile.open(filename, \"w:gz\") as tar:\n for f in files:\n tar.add(f)\n\n yield filename\n\n # clear\n os.close(fd)\n os.remove(filename)", "response": "Create a tar file based on the list of files passed"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprint the tensorboard url for project group and experiment.", "response": "def url(ctx):\n \"\"\"Prints the tensorboard url for project/experiment/experiment group.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples for project tensorboards:\n\n \\b\n ```bash\n $ polyaxon tensorboard url\n ```\n\n \\b\n ```bash\n $ polyaxon tensorboard -p mnist url\n ```\n\n Examples for experiment tensorboards:\n\n \\b\n ```bash\n $ polyaxon tensorboard -xp 1 url\n ```\n\n Examples for experiment group tensorboards:\n\n \\b\n ```bash\n $ polyaxon tensorboard -g 1 url\n ```\n \"\"\"\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n group = ctx.obj.get('group')\n experiment = ctx.obj.get('experiment')\n if experiment:\n try:\n response = PolyaxonClient().experiment.get_experiment(\n username=user,\n project_name=project_name,\n experiment_id=experiment)\n obj = 'experiment {}'.format(experiment)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get experiment `{}`.'.format(experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n elif group:\n try:\n response = PolyaxonClient().experiment_group.get_experiment_group(\n username=user,\n project_name=project_name,\n group_id=group)\n obj = 'group `{}`.'.format(group)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get group `{}`.'.format(group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n else:\n try:\n response = PolyaxonClient().project.get_project(\n username=user,\n project_name=project_name)\n obj = 'project `{}`.'.format(project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.has_tensorboard:\n click.echo(get_tensorboard_url(user=user,\n project_name=project_name,\n experiment=experiment,\n group=group))\n else:\n Printer.print_warning('This `{}` does not have a running tensorboard'.format(obj))\n click.echo('You can start tensorboard with this command: polyaxon tensorboard start --help')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef start(ctx, file): # pylint:disable=redefined-builtin\n specification = None\n job_config = None\n if file:\n specification = check_polyaxonfile(file, log=False).specification\n\n if specification:\n # pylint:disable=protected-access\n check_polyaxonfile_kind(specification=specification, kind=specification._TENSORBOARD)\n job_config = specification.parsed_data\n\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n group = ctx.obj.get('group')\n experiment = ctx.obj.get('experiment')\n if experiment:\n try:\n response = PolyaxonClient().experiment.start_tensorboard(\n username=user,\n project_name=project_name,\n experiment_id=experiment,\n job_config=job_config)\n obj = 'experiment `{}`'.format(experiment)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not start tensorboard experiment `{}`.'.format(experiment))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n elif group:\n try:\n response = PolyaxonClient().experiment_group.start_tensorboard(\n username=user,\n project_name=project_name,\n group_id=group,\n job_config=job_config)\n obj = 'group `{}`'.format(group)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not start tensorboard group `{}`.'.format(group))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n else:\n try:\n response = PolyaxonClient().project.start_tensorboard(\n username=user,\n project_name=project_name,\n job_config=job_config)\n obj = 'project `{}`'.format(project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not start tensorboard project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 200:\n Printer.print_header(\"A tensorboard for this {} is already running on:\".format(obj))\n click.echo(get_tensorboard_url(user=user,\n project_name=project_name,\n experiment=experiment,\n group=group))\n sys.exit(0)\n\n if response.status_code != 201:\n Printer.print_error('Something went wrong, Tensorboard was not created.')\n sys.exit(1)\n\n Printer.print_success('Tensorboard is being deployed for {}'.format(obj))\n clint.textui.puts(\"It may take some time before you can access tensorboard.\\n\")\n clint.textui.puts(\"Your tensorboard will be available on:\\n\")\n with clint.textui.indent(4):\n clint.textui.puts(get_tensorboard_url(user, project_name, experiment, group))", "response": "Start a tensorboard deployment for project group and resource."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef stop(ctx, yes):\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n group = ctx.obj.get('group')\n experiment = ctx.obj.get('experiment')\n\n if experiment:\n obj = 'experiment `{}`'.format(experiment)\n elif group:\n obj = 'group `{}`'.format(group)\n else:\n obj = 'project `{}/{}`'.format(user, project_name)\n\n if not yes and not click.confirm(\"Are sure you want to stop tensorboard \"\n \"for {}\".format(obj)):\n click.echo('Existing without stopping tensorboard.')\n sys.exit(1)\n\n if experiment:\n try:\n PolyaxonClient().experiment.stop_tensorboard(\n username=user,\n project_name=project_name,\n experiment_id=experiment)\n Printer.print_success('Tensorboard is being deleted')\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop tensorboard {}.'.format(obj))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n elif group:\n try:\n PolyaxonClient().experiment_group.stop_tensorboard(\n username=user,\n project_name=project_name,\n group_id=group)\n Printer.print_success('Tensorboard is being deleted')\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop tensorboard {}.'.format(obj))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n else:\n try:\n PolyaxonClient().project.stop_tensorboard(\n username=user,\n project_name=project_name)\n Printer.print_success('Tensorboard is being deleted')\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop tensorboard {}.'.format(obj))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)", "response": "Stops the tensorboard for the current project and experiment."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef check_cli_version():\n if not CliConfigManager.should_check():\n return\n\n server_version = get_server_version()\n current_version = get_current_version()\n CliConfigManager.reset(current_version=current_version,\n min_version=server_version.min_version)\n\n if LooseVersion(current_version) < LooseVersion(server_version.min_version):\n click.echo(\"\"\"Your version of CLI ({}) is no longer compatible with server.\"\"\".format(\n current_version))\n if click.confirm(\"Do you want to upgrade to \"\n \"version {} now?\".format(server_version.latest_version)):\n pip_upgrade()\n sys.exit(0)\n else:\n clint.textui.puts(\"Your can manually run:\")\n with clint.textui.indent(4):\n clint.textui.puts(\"pip install -U polyaxon-cli\")\n clint.textui.puts(\n \"to upgrade to the latest version `{}`\".format(server_version.latest_version))\n\n sys.exit(0)\n elif LooseVersion(current_version) < LooseVersion(server_version.latest_version):\n clint.textui.puts(\"New version of CLI ({}) is now available. To upgrade run:\".format(\n server_version.latest_version\n ))\n with clint.textui.indent(4):\n clint.textui.puts(\"pip install -U polyaxon-cli\")\n elif LooseVersion(current_version) > LooseVersion(server_version.latest_version):\n clint.textui.puts(\"You version of CLI ({}) is ahead of the latest version \"\n \"supported by Polyaxon Platform ({}) on your cluster, \"\n \"and might be incompatible.\".format(current_version,\n server_version.latest_version))", "response": "Check if the current CLI version satisfies the server requirements"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprint the current version of the cli and platform.", "response": "def version(cli, platform):\n \"\"\"Print the current version of the cli and platform.\"\"\"\n version_client = PolyaxonClient().version\n cli = cli or not any([cli, platform])\n if cli:\n try:\n server_version = version_client.get_cli_version()\n except AuthorizationError:\n session_expired()\n sys.exit(1)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get cli version.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n cli_version = get_version(PROJECT_CLI_NAME)\n Printer.print_header('Current cli version: {}.'.format(cli_version))\n Printer.print_header('Supported cli versions:')\n dict_tabulate(server_version.to_dict())\n\n if platform:\n try:\n platform_version = version_client.get_platform_version()\n except AuthorizationError:\n session_expired()\n sys.exit(1)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get platform version.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n chart_version = version_client.get_chart_version()\n Printer.print_header('Current platform version: {}.'.format(chart_version.version))\n Printer.print_header('Supported platform versions:')\n dict_tabulate(platform_version.to_dict())"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nopens dashboard in browser.", "response": "def dashboard(yes, url):\n \"\"\"Open dashboard in browser.\"\"\"\n dashboard_url = \"{}/app\".format(PolyaxonClient().api_config.http_host)\n if url:\n click.echo(dashboard_url)\n sys.exit(0)\n if not yes:\n click.confirm('Dashboard page will now open in your browser. Continue?',\n abort=True, default=True)\n\n click.launch(dashboard_url)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef grant(username):\n try:\n PolyaxonClient().user.grant_superuser(username)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not grant superuser role to user `{}`.'.format(username))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\n \"Superuser role was granted successfully to user `{}`.\".format(username))", "response": "Grant superuser role to a user."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef revoke(username):\n try:\n PolyaxonClient().user.revoke_superuser(username)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not revoke superuser role from user `{}`.'.format(username))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\n \"Superuser role was revoked successfully from user `{}`.\".format(username))", "response": "Revoke superuser role from a user."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nprints the url for this project.", "response": "def url(ctx):\n \"\"\"Prints the notebook url for this project.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon notebook url\n ```\n \"\"\"\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n try:\n response = PolyaxonClient().project.get_project(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.has_notebook:\n click.echo(get_notebook_url(user, project_name))\n else:\n Printer.print_warning(\n 'This project `{}` does not have a running notebook.'.format(project_name))\n click.echo('You can start a notebook with this command: polyaxon notebook start --help')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstarts a new notebook for this project.", "response": "def start(ctx, file, u): # pylint:disable=redefined-builtin\n \"\"\"Start a notebook deployment for this project.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon notebook start -f file -f file_override ...\n ```\n\n Example: upload before running\n\n \\b\n ```bash\n $ polyaxon -p user12/mnist notebook start -f file -u\n ```\n \"\"\"\n specification = None\n job_config = None\n if file:\n specification = check_polyaxonfile(file, log=False).specification\n\n # Check if we need to upload\n if u:\n ctx.invoke(upload, sync=False)\n\n if specification:\n # pylint:disable=protected-access\n check_polyaxonfile_kind(specification=specification, kind=specification._NOTEBOOK)\n job_config = specification.parsed_data\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n try:\n response = PolyaxonClient().project.start_notebook(user, project_name, job_config)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not start notebook project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 200:\n Printer.print_header(\"A notebook for this project is already running on:\")\n click.echo(get_notebook_url(user, project_name))\n sys.exit(0)\n\n if response.status_code != 201:\n Printer.print_error('Something went wrong, Notebook was not created.')\n sys.exit(1)\n\n Printer.print_success('Notebook is being deployed for project `{}`'.format(project_name))\n clint.textui.puts(\"It may take some time before you can access the notebook.\\n\")\n clint.textui.puts(\"Your notebook will be available on:\\n\")\n with clint.textui.indent(4):\n clint.textui.puts(get_notebook_url(user, project_name))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstop the notebook deployment for this project.", "response": "def stop(ctx, commit, yes):\n \"\"\"Stops the notebook deployment for this project if it exists.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n \"\"\"\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n if not yes and not click.confirm(\"Are sure you want to stop notebook \"\n \"for project `{}/{}`\".format(user, project_name)):\n click.echo('Existing without stopping notebook.')\n sys.exit(1)\n\n if commit is None:\n commit = True\n\n try:\n PolyaxonClient().project.stop_notebook(user, project_name, commit)\n Printer.print_success('Notebook is being deleted')\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not stop notebook project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd platform specific checks", "response": "def check(self):\n \"\"\"Add platform specific checks\"\"\"\n if not self.is_valid:\n raise PolyaxonDeploymentConfigError(\n 'Deployment type `{}` not supported'.format(self.deployment_type))\n check = False\n if self.is_kubernetes:\n check = self.check_for_kubernetes()\n elif self.is_docker_compose:\n check = self.check_for_docker_compose()\n elif self.is_docker:\n check = self.check_for_docker()\n elif self.is_heroku:\n check = self.check_for_heroku()\n if not check:\n raise PolyaxonDeploymentConfigError(\n 'Deployment `{}` is not valid'.format(self.deployment_type))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef install(self):\n if not self.is_valid:\n raise PolyaxonDeploymentConfigError(\n 'Deployment type `{}` not supported'.format(self.deployment_type))\n\n if self.is_kubernetes:\n self.install_on_kubernetes()\n elif self.is_docker_compose:\n self.install_on_docker_compose()\n elif self.is_docker:\n self.install_on_docker()\n elif self.is_heroku:\n self.install_on_heroku()", "response": "Install polyaxon using the current config to the correct platform."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupgrading the specified resource.", "response": "def upgrade(self):\n \"\"\"Upgrade deployment.\"\"\"\n if not self.is_valid:\n raise PolyaxonDeploymentConfigError(\n 'Deployment type `{}` not supported'.format(self.deployment_type))\n\n if self.is_kubernetes:\n self.upgrade_on_kubernetes()\n elif self.is_docker_compose:\n self.upgrade_on_docker_compose()\n elif self.is_docker:\n self.upgrade_on_docker()\n elif self.is_heroku:\n self.upgrade_on_heroku()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(ctx, name, description, tags, private, init):\n try:\n tags = tags.split(',') if tags else None\n project_dict = dict(name=name, description=description, is_public=not private, tags=tags)\n project_config = ProjectConfig.from_dict(project_dict)\n except ValidationError:\n Printer.print_error('Project name should contain only alpha numerical, \"-\", and \"_\".')\n sys.exit(1)\n\n try:\n _project = PolyaxonClient().project.create_project(project_config)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not create project `{}`.'.format(name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Project `{}` was created successfully.\".format(_project.name))\n\n if init:\n ctx.obj = {}\n ctx.invoke(init_project, project=name)", "response": "Create a new project."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list(page): # pylint:disable=redefined-builtin\n user = AuthConfigManager.get_value('username')\n if not user:\n Printer.print_error('Please login first. `polyaxon login --help`')\n\n page = page or 1\n try:\n response = PolyaxonClient().project.list_projects(user, page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get list of projects.')\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Projects for current user')\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No projects found for current user')\n\n objects = list_dicts_to_tabulate(\n [o.to_light_dict(\n humanize_values=True,\n exclude_attrs=['uuid', 'experiment_groups', 'experiments', 'description',\n 'num_experiments', 'num_independent_experiments',\n 'num_experiment_groups', 'num_jobs', 'num_builds', 'unique_name'])\n for o in response['results']])\n if objects:\n Printer.print_header(\"Projects:\")\n dict_tabulate(objects, is_list_dict=True)", "response": "List projects.\n\n Uses [Caching](/references/polyaxon-cli/#caching)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get(ctx):\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n try:\n response = PolyaxonClient().project.get_project(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n get_project_details(response)", "response": "Get current project by project_name or user or project_name."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete(ctx):\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n if not click.confirm(\"Are sure you want to delete project `{}/{}`\".format(user, project_name)):\n click.echo('Existing without deleting project.')\n sys.exit(1)\n\n try:\n response = PolyaxonClient().project.delete_project(user, project_name)\n local_project = ProjectManager.get_config()\n if local_project and (user, project_name) == (local_project.user, local_project.name):\n # Purge caching\n ProjectManager.purge()\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not delete project `{}/{}`.'.format(user, project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n if response.status_code == 204:\n Printer.print_success(\"Project `{}/{}` was delete successfully\".format(user, project_name))", "response": "Delete project.\n\n Uses [Caching](/references/polyaxon-cli/#caching)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating project. Uses [Caching](/references/polyaxon-cli/#caching) Example: \\b ```bash $ polyaxon update foobar --description=\"Image Classification with DL using TensorFlow\" ``` \\b ```bash $ polyaxon update mike1/foobar --description=\"Image Classification with DL using TensorFlow\" ``` \\b ```bash $ polyaxon update --tags=\"foo, bar\" ```", "response": "def update(ctx, name, description, tags, private):\n \"\"\"Update project.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Example:\n\n \\b\n ```bash\n $ polyaxon update foobar --description=\"Image Classification with DL using TensorFlow\"\n ```\n\n \\b\n ```bash\n $ polyaxon update mike1/foobar --description=\"Image Classification with DL using TensorFlow\"\n ```\n\n \\b\n ```bash\n $ polyaxon update --tags=\"foo, bar\"\n ```\n \"\"\"\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n update_dict = {}\n if name:\n update_dict['name'] = name\n\n if description:\n update_dict['description'] = description\n\n if private is not None:\n update_dict['is_public'] = not private\n\n tags = validate_tags(tags)\n if tags:\n update_dict['tags'] = tags\n\n if not update_dict:\n Printer.print_warning('No argument was provided to update the project.')\n sys.exit(1)\n\n try:\n response = PolyaxonClient().project.update_project(user, project_name, update_dict)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not update project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\"Project updated.\")\n get_project_details(response)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef groups(ctx, query, sort, page):\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n page = page or 1\n try:\n response = PolyaxonClient().project.list_experiment_groups(username=user,\n project_name=project_name,\n query=query,\n sort=sort,\n page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error(\n 'Could not get experiment groups for project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Experiment groups for project `{}/{}`.'.format(user, project_name))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No experiment groups found for project `{}/{}`.'.format(\n user, project_name))\n\n objects = [Printer.add_status_color(o.to_light_dict(humanize_values=True))\n for o in response['results']]\n objects = list_dicts_to_tabulate(objects)\n if objects:\n Printer.print_header(\"Experiment groups:\")\n objects.pop('project', None)\n objects.pop('user', None)\n dict_tabulate(objects, is_list_dict=True)", "response": "List experiment groups for this project."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist all experiments for this project.", "response": "def experiments(ctx, metrics, declarations, independent, group, query, sort, page):\n \"\"\"List experiments for this project.\n\n Uses [Caching](/references/polyaxon-cli/#caching)\n\n Examples:\n\n Get all experiments:\n\n \\b\n ```bash\n $ polyaxon project experiments\n ```\n\n Get all experiments with with status {created or running}, and\n creation date between 2018-01-01 and 2018-01-02, and declarations activation equal to sigmoid\n and metric loss less or equal to 0.2\n\n \\b\n ```bash\n $ polyaxon project experiments \\\n -q \"status:created|running, started_at:2018-01-01..2018-01-02, \\\n declarations.activation:sigmoid, metric.loss:<=0.2\"\n ```\n\n Get all experiments sorted by update date\n\n \\b\n ```bash\n $ polyaxon project experiments -s \"-updated_at\"\n ```\n \"\"\"\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n page = page or 1\n try:\n response = PolyaxonClient().project.list_experiments(username=user,\n project_name=project_name,\n independent=independent,\n group=group,\n metrics=metrics,\n declarations=declarations,\n query=query,\n sort=sort,\n page=page)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not get experiments for project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n meta = get_meta_response(response)\n if meta:\n Printer.print_header('Experiments for project `{}/{}`.'.format(user, project_name))\n Printer.print_header('Navigation:')\n dict_tabulate(meta)\n else:\n Printer.print_header('No experiments found for project `{}/{}`.'.format(user, project_name))\n\n if metrics:\n objects = get_experiments_with_metrics(response)\n elif declarations:\n objects = get_experiments_with_declarations(response)\n else:\n objects = [Printer.add_status_color(o.to_light_dict(humanize_values=True))\n for o in response['results']]\n objects = list_dicts_to_tabulate(objects)\n if objects:\n Printer.print_header(\"Experiments:\")\n objects.pop('project_name', None)\n dict_tabulate(objects, is_list_dict=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef git(ctx, url, private, sync): # pylint:disable=assign-to-new-keyword\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n def git_set_url():\n if private:\n click.echo('\\nSetting a private git repo \"{}\" on project: {} ...\\n'.format(\n url, project_name))\n else:\n click.echo('\\nSetting a public git repo \"{}\" on project: {} ...\\n'.format(\n url, project_name))\n\n try:\n PolyaxonClient().project.set_repo(user, project_name, url, not private)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not set git repo on project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success('Project was successfully initialized with `{}`.'.format(url))\n\n def git_sync_repo():\n try:\n response = PolyaxonClient().project.sync_repo(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not sync git repo on project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n click.echo(response.status_code)\n Printer.print_success('Project was successfully synced with latest changes.')\n\n if url:\n git_set_url()\n if sync:\n git_sync_repo()", "response": "Set or sync git repo on this project."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef ci(ctx, enable, disable): # pylint:disable=assign-to-new-keyword\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n\n def enable_ci():\n try:\n PolyaxonClient().project.enable_ci(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not enable CI on project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\n 'Polyaxon CI was successfully enabled on project: `{}`.'.format(project_name))\n\n def disable_ci():\n try:\n PolyaxonClient().project.disable_ci(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not disable CI on project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n\n Printer.print_success(\n 'Polyaxon CI was successfully disabled on project: `{}`.'.format(project_name))\n\n if enable:\n enable_ci()\n if disable:\n disable_ci()", "response": "Enables or disables the CI on this project."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef download(ctx):\n user, project_name = get_project_or_local(ctx.obj.get('project'))\n try:\n PolyaxonClient().project.download_repo(user, project_name)\n except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:\n Printer.print_error('Could not download code for project `{}`.'.format(project_name))\n Printer.print_error('Error message `{}`.'.format(e))\n sys.exit(1)\n Printer.print_success('Files downloaded.')", "response": "Download code of the current project."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef write(self,file,optstring=\"\",quote=False):\n classid = str(self.id)\n if quote: classid = '\"'+classid+'\"'\n # Only use a *single* space between tokens; both chimera's and pymol's DX parser\n # does not properly implement the OpenDX specs and produces garbage with multiple\n # spaces. (Chimera 1.4.1, PyMOL 1.3)\n file.write('object '+classid+' class '+str(self.name)+' '+\\\n optstring+'\\n')", "response": "write the object line ; additional args are packed in string"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef edges(self):\n return [self.delta[d,d] * numpy.arange(self.shape[d]+1) + self.origin[d]\\\n - 0.5*self.delta[d,d] for d in range(self.rank)]", "response": "Edges of the grid cells origin at centre of 0.. 0 grid cell."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite the class array section to a file.", "response": "def write(self, file):\n \"\"\"Write the *class array* section.\n\n Parameters\n ----------\n file : file\n\n Raises\n ------\n ValueError\n If the `dxtype` is not a valid type, :exc:`ValueError` is\n raised.\n\n \"\"\"\n if self.type not in self.dx_types:\n raise ValueError((\"DX type {} is not supported in the DX format. \\n\"\n \"Supported valus are: {}\\n\"\n \"Use the type= keyword argument.\").format(\n self.type, list(self.dx_types.keys())))\n typelabel = (self.typequote+self.type+self.typequote)\n DXclass.write(self,file,\n 'type {0} rank 0 items {1} data follows'.format(\n typelabel, self.array.size))\n # grid data, serialized as a C array (z fastest varying)\n # (flat iterator is equivalent to: for x: for y: for z: grid[x,y,z])\n # VMD's DX reader requires exactly 3 values per line\n fmt_string = \"{:d}\"\n if (self.array.dtype.kind == 'f' or self.array.dtype.kind == 'c'):\n precision = numpy.finfo(self.array.dtype).precision\n fmt_string = \"{:.\"+\"{:d}\".format(precision)+\"f}\"\n values_per_line = 3\n values = self.array.flat\n while 1:\n try:\n for i in range(values_per_line):\n file.write(fmt_string.format(next(values)) + \"\\t\")\n file.write('\\n')\n except StopIteration:\n file.write('\\n')\n break\n file.write('attribute \"dep\" string \"positions\"\\n')"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwriting the complete dx object to the file.", "response": "def write(self, filename):\n \"\"\"Write the complete dx object to the file.\n\n This is the simple OpenDX format which includes the data into\n the header via the 'object array ... data follows' statement.\n\n Only simple regular arrays are supported.\n\n The format should be compatible with VMD's dx reader plugin.\n \"\"\"\n # comments (VMD chokes on lines of len > 80, so truncate)\n maxcol = 80\n with open(filename,'w') as outfile:\n for line in self.comments:\n comment = '# '+str(line)\n outfile.write(comment[:maxcol]+'\\n')\n # each individual object\n for component,object in self.sorted_components():\n object.write(outfile)\n # the field object itself\n DXclass.write(self,outfile,quote=True)\n for component,object in self.sorted_components():\n outfile.write('component \"%s\" value %s\\n' % (component,str(object.id)))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread the classid from file.", "response": "def read(self,file):\n \"\"\"Read DX field from file.\n\n dx = OpenDX.field.read(dxfile)\n\n The classid is discarded and replaced with the one from the file.\n \"\"\"\n DXfield = self\n p = DXParser(file)\n p.parse(DXfield)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef histogramdd(self):\n shape = self.components['positions'].shape\n edges = self.components['positions'].edges()\n hist = self.components['data'].array.reshape(shape)\n return (hist,edges)", "response": "Return array data as ( edges grid ) i. e. a numpy nD histogram."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef value(self,ascode=None):\n if ascode is None:\n ascode = self.code\n return self.cast[ascode](self.text)", "response": "Return text cast to the correct type or the selected type"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ninitializing the corresponding DXclass from the data.", "response": "def initialize(self):\n \"\"\"Initialize the corresponding DXclass from the data.\n\n class = DXInitObject.initialize()\n \"\"\"\n return self.DXclasses[self.type](self.id,**self.args)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse(self,DXfield):\n\n self.DXfield = DXfield # OpenDX.field (used by comment parser)\n self.currentobject = None # containers for data\n self.objects = [] # |\n self.tokens = [] # token buffer\n with open(self.filename,'r') as self.dxfile:\n self.use_parser('general') # parse the whole file and populate self.objects\n\n # assemble field from objects\n for o in self.objects:\n if o.type == 'field':\n # Almost ignore the field object; VMD, for instance,\n # does not write components. To make this work\n # seamlessly I have to think harder how to organize\n # and use the data, eg preping the field object\n # properly and the initializing. Probably should also\n # check uniqueness of ids etc.\n DXfield.id = o.id\n continue\n c = o.initialize()\n self.DXfield.add(c.component,c)\n\n # free space\n del self.currentobject, self.objects", "response": "Parse the dx file and construct a DXField object with component classes."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __general(self):\n while 1: # main loop\n try:\n tok = self.__peek() # only peek, apply_parser() will consume\n except DXParserNoTokens:\n # save previous DXInitObject\n # (kludge in here as the last level-2 parser usually does not return\n # via the object parser)\n if self.currentobject and self.currentobject not in self.objects:\n self.objects.append(self.currentobject)\n return # stop parsing and finish\n # decision branches for all level-1 parsers:\n # (the only way to get out of the lower level parsers!)\n if tok.iscode('COMMENT'):\n self.set_parser('comment') # switch the state\n elif tok.iscode('WORD') and tok.equals('object'):\n self.set_parser('object') # switch the state\n elif self.__parser is self.__general:\n # Either a level-2 parser screwed up or some level-1\n # construct is not implemented. (Note: this elif can\n # be only reached at the beginning or after comments;\n # later we never formally switch back to __general\n # (would create inifinite loop)\n raise DXParseError('Unknown level-1 construct at '+str(tok))\n\n self.apply_parser()", "response": "Level - 1 parser and main loop."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlevels - 1 parser for comments. pattern", "response": "def __comment(self):\n \"\"\"Level-1 parser for comments.\n\n pattern: #.*\n Append comment (with initial '# ' stripped) to all comments.\n \"\"\"\n tok = self.__consume()\n self.DXfield.add_comment(tok.value())\n self.set_parser('general')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __object(self):\n self.__consume() # 'object'\n classid = self.__consume().text\n word = self.__consume().text\n if word != \"class\":\n raise DXParseError(\"reserved word %s should have been 'class'.\" % word)\n # save previous DXInitObject\n if self.currentobject:\n self.objects.append(self.currentobject)\n # setup new DXInitObject\n classtype = self.__consume().text\n self.currentobject = DXInitObject(classtype=classtype,classid=classid)\n\n self.use_parser(classtype)", "response": "Level-1 parser for objects.\n\n pattern: 'object' id 'class' type ...\n\n id ::= integer|string|'\"'white space string'\"'\n type ::= string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nleveling - 2 parser for gridpositions.", "response": "def __gridpositions(self):\n \"\"\"Level-2 parser for gridpositions.\n\n pattern:\n object 1 class gridpositions counts 97 93 99\n origin -46.5 -45.5 -48.5\n delta 1 0 0\n delta 0 1 0\n delta 0 0 1\n \"\"\"\n try:\n tok = self.__consume()\n except DXParserNoTokens:\n return\n\n if tok.equals('counts'):\n shape = []\n try:\n while True:\n # raises exception if not an int\n self.__peek().value('INTEGER')\n tok = self.__consume()\n shape.append(tok.value('INTEGER'))\n except (DXParserNoTokens, ValueError):\n pass\n if len(shape) == 0:\n raise DXParseError('gridpositions: no shape parameters')\n self.currentobject['shape'] = shape\n elif tok.equals('origin'):\n origin = []\n try:\n while (self.__peek().iscode('INTEGER') or\n self.__peek().iscode('REAL')):\n tok = self.__consume()\n origin.append(tok.value())\n except DXParserNoTokens:\n pass\n if len(origin) == 0:\n raise DXParseError('gridpositions: no origin parameters')\n self.currentobject['origin'] = origin\n elif tok.equals('delta'):\n d = []\n try:\n while (self.__peek().iscode('INTEGER') or\n self.__peek().iscode('REAL')):\n tok = self.__consume()\n d.append(tok.value())\n except DXParserNoTokens:\n pass\n if len(d) == 0:\n raise DXParseError('gridpositions: missing delta parameters')\n try:\n self.currentobject['delta'].append(d)\n except KeyError:\n self.currentobject['delta'] = [d]\n else:\n raise DXParseError('gridpositions: '+str(tok)+' not recognized.')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlevels - 2 parser for gridconnections.", "response": "def __gridconnections(self):\n \"\"\"Level-2 parser for gridconnections.\n\n pattern:\n object 2 class gridconnections counts 97 93 99\n \"\"\"\n try:\n tok = self.__consume()\n except DXParserNoTokens:\n return\n\n if tok.equals('counts'):\n shape = []\n try:\n while True:\n # raises exception if not an int\n self.__peek().value('INTEGER')\n tok = self.__consume()\n shape.append(tok.value('INTEGER'))\n except (DXParserNoTokens, ValueError):\n pass\n if len(shape) == 0:\n raise DXParseError('gridconnections: no shape parameters')\n self.currentobject['shape'] = shape\n else:\n raise DXParseError('gridconnections: '+str(tok)+' not recognized.')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __array(self):\n try:\n tok = self.__consume()\n except DXParserNoTokens:\n return\n\n if tok.equals('type'):\n tok = self.__consume()\n if not tok.iscode('STRING'):\n raise DXParseError('array: type was \"%s\", not a string.'%\\\n tok.text)\n self.currentobject['type'] = tok.value()\n elif tok.equals('rank'):\n tok = self.__consume()\n try:\n self.currentobject['rank'] = tok.value('INTEGER')\n except ValueError:\n raise DXParseError('array: rank was \"%s\", not an integer.'%\\\n tok.text)\n elif tok.equals('items'):\n tok = self.__consume()\n try:\n self.currentobject['size'] = tok.value('INTEGER')\n except ValueError:\n raise DXParseError('array: items was \"%s\", not an integer.'%\\\n tok.text)\n elif tok.equals('data'):\n tok = self.__consume()\n if not tok.iscode('STRING'):\n raise DXParseError('array: data was \"%s\", not a string.'%\\\n tok.text)\n if tok.text != 'follows':\n raise NotImplementedError(\\\n 'array: Only the \"data follows header\" format is supported.')\n if not self.currentobject['size']:\n raise DXParseError(\"array: missing number of items\")\n # This is the slow part. Once we get here, we are just\n # reading in a long list of numbers. Conversion to floats\n # will be done later when the numpy array is created.\n\n # Don't assume anything about whitespace or the number of elements per row\n self.currentobject['array'] = []\n while len(self.currentobject['array']) ':\n # try all endinaness and alignment options until we find something that looks sensible\n header = decode_header(flag)\n if header['rank'] == 3:\n break # only legal value according to spec\n header = None\n if header is None:\n raise TypeError(\"Cannot decode header --- corrupted or wrong format?\")\n for rec in self._header_struct:\n if not rec.is_legal_dict(header):\n warnings.warn(\"Key %s: Illegal value %r\" % (rec.key, header[rec.key]))\n return header", "response": "Read the header bytes and return a dictionary of the header values."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ndmeshgrid(*arrs):\n #arrs = tuple(reversed(arrs)) <-- wrong on stackoverflow.com\n arrs = tuple(arrs)\n lens = list(map(len, arrs))\n dim = len(arrs)\n\n sz = 1\n for s in lens:\n sz *= s\n\n ans = []\n for i, arr in enumerate(arrs):\n slc = [1] * dim\n slc[i] = lens[i]\n arr2 = numpy.asanyarray(arr).reshape(slc)\n for j, sz in enumerate(lens):\n if j != i:\n arr2 = arr2.repeat(sz, axis=j)\n ans.append(arr2)\n\n return tuple(ans)", "response": "Return a meshgrid for N dimensions."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef resample(self, edges):\n try:\n edges = edges.edges # can also supply another Grid\n except AttributeError:\n pass\n midpoints = self._midpoints(edges)\n coordinates = ndmeshgrid(*midpoints)\n # feed a meshgrid to generate all points\n newgrid = self.interpolated(*coordinates)\n return self.__class__(newgrid, edges)", "response": "Resample data to a new grid with the given edges."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompute and update all derived data for a specific entry.", "response": "def _update(self):\n \"\"\"compute/update all derived data\n\n Can be called without harm and is idem-potent.\n\n Updates these attributes and methods:\n :attr:`origin`\n the center of the cell with index 0,0,0\n :attr:`midpoints`\n centre coordinate of each grid cell\n :meth:`interpolated`\n spline interpolation function that can generated a value for\n coordinate\n \"\"\"\n self.delta = numpy.array(list(\n map(lambda e: (e[-1] - e[0]) / (len(e) - 1), self.edges)))\n self.midpoints = self._midpoints(self.edges)\n self.origin = numpy.array(list(map(lambda m: m[0], self.midpoints)))\n if self.__interpolated is not None:\n # only update if we are using it\n self.__interpolated = self._interpolationFunctionFactory()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef interpolated(self):\n if self.__interpolated is None:\n self.__interpolated = self._interpolationFunctionFactory()\n return self.__interpolated", "response": "A function that returns the interpolated function over the data grid."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef load(self, filename, file_format=None):\n loader = self._get_loader(filename, file_format=file_format)\n loader(filename)", "response": "Load a saved ( pickled or dx grid and edges from a file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _load_cpp4(self, filename):\n ccp4 = CCP4.CCP4()\n ccp4.read(filename)\n grid, edges = ccp4.histogramdd()\n self.__init__(grid=grid, edges=edges, metadata=self.metadata)", "response": "Initializes Grid from a CCP4 file."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninitialize a Grid from a OpenDX file.", "response": "def _load_dx(self, filename):\n \"\"\"Initializes Grid from a OpenDX file.\"\"\"\n dx = OpenDX.field(0)\n dx.read(filename)\n grid, edges = dx.histogramdd()\n self.__init__(grid=grid, edges=edges, metadata=self.metadata)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _load_plt(self, filename):\n g = gOpenMol.Plt()\n g.read(filename)\n grid, edges = g.histogramdd()\n self.__init__(grid=grid, edges=edges, metadata=self.metadata)", "response": "Initialize Grid from gOpenMol plt file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nexport density to file using the given format.", "response": "def export(self, filename, file_format=None, type=None, typequote='\"'):\n \"\"\"export density to file using the given format.\n\n The format can also be deduced from the suffix of the filename\n though the *format* keyword takes precedence.\n\n The default format for export() is 'dx'. Use 'dx' for\n visualization.\n\n Implemented formats:\n\n dx\n :mod:`OpenDX`\n pickle\n pickle (use :meth:`Grid.load` to restore); :meth:`Grid.save`\n is simpler than ``export(format='python')``.\n\n Parameters\n ----------\n\n filename : str\n name of the output file\n\n file_format : {'dx', 'pickle', None} (optional)\n output file format, the default is \"dx\"\n\n type : str (optional)\n for DX, set the output DX array type, e.g., \"double\" or \"float\".\n By default (``None``), the DX type is determined from the numpy\n dtype of the array of the grid (and this will typically result in\n \"double\").\n\n .. versionadded:: 0.4.0\n\n typequote : str (optional)\n For DX, set the character used to quote the type string;\n by default this is a double-quote character, '\"'.\n Custom parsers like the one from NAMD-GridForces (backend for MDFF)\n expect no quotes, and typequote='' may be used to appease them.\n\n .. versionadded:: 0.5.0\n\n \"\"\"\n exporter = self._get_exporter(filename, file_format=file_format)\n exporter(filename, type=type, typequote=typequote)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexport the grid object to a file.", "response": "def _export_python(self, filename, **kwargs):\n \"\"\"Pickle the Grid object\n\n The object is dumped as a dictionary with grid and edges: This\n is sufficient to recreate the grid object with __init__().\n \"\"\"\n data = dict(grid=self.grid, edges=self.edges, metadata=self.metadata)\n with open(filename, 'wb') as f:\n cPickle.dump(data, f, cPickle.HIGHEST_PROTOCOL)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexports the density grid to an OpenDX file.", "response": "def _export_dx(self, filename, type=None, typequote='\"', **kwargs):\n \"\"\"Export the density grid to an OpenDX file.\n\n The file format is the simplest regular grid array and it is\n also understood by VMD's and Chimera's DX reader; PyMOL\n requires the dx `type` to be set to \"double\".\n\n For the file format see\n http://opendx.sdsc.edu/docs/html/pages/usrgu068.htm#HDREDF\n\n \"\"\"\n root, ext = os.path.splitext(filename)\n filename = root + '.dx'\n\n comments = [\n 'OpenDX density file written by gridDataFormats.Grid.export()',\n 'File format: http://opendx.sdsc.edu/docs/html/pages/usrgu068.htm#HDREDF',\n 'Data are embedded in the header and tied to the grid positions.',\n 'Data is written in C array order: In grid[x,y,z] the axis z is fastest',\n 'varying, then y, then finally x, i.e. z is the innermost loop.'\n ]\n\n # write metadata in comments section\n if self.metadata:\n comments.append('Meta data stored with the python Grid object:')\n for k in self.metadata:\n comments.append(' ' + str(k) + ' = ' + str(self.metadata[k]))\n comments.append(\n '(Note: the VMD dx-reader chokes on comments below this line)')\n\n components = dict(\n positions=OpenDX.gridpositions(1, self.grid.shape, self.origin,\n self.delta),\n connections=OpenDX.gridconnections(2, self.grid.shape),\n data=OpenDX.array(3, self.grid, type=type, typequote=typequote),\n )\n dx = OpenDX.field('density', components=components, comments=comments)\n dx.write(filename)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the coordinates of the centers of all grid cells as an an iterator.", "response": "def centers(self):\n \"\"\"Returns the coordinates of the centers of all grid cells as an\n iterator.\"\"\"\n for idx in numpy.ndindex(self.grid.shape):\n yield self.delta * numpy.array(idx) + self.origin"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef check_compatible(self, other):\n if not (numpy.isreal(other) or self == other):\n raise TypeError(\n \"The argument can not be arithmetically combined with the grid. \"\n \"It must be a scalar or a grid with identical edges. \"\n \"Use Grid.resample(other.edges) to make a new grid that is \"\n \"compatible with other.\")\n return True", "response": "Checks if self and other are compatible with the grid."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _interpolationFunctionFactory(self, spline_order=None, cval=None):\n # for scipy >=0.9: should use scipy.interpolate.griddata\n # http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html#scipy.interpolate.griddata\n # (does it work for nD?)\n import scipy.ndimage\n\n if spline_order is None:\n # must be compatible with whatever :func:`scipy.ndimage.spline_filter` takes.\n spline_order = self.interpolation_spline_order\n if cval is None:\n cval = self.interpolation_cval\n\n data = self.grid\n if cval is None:\n cval = data.min()\n try:\n # masked arrays, fill with min: should keep spline happy\n _data = data.filled(cval)\n except AttributeError:\n _data = data\n\n coeffs = scipy.ndimage.spline_filter(_data, order=spline_order)\n x0 = self.origin\n dx = self.delta\n\n def _transform(cnew, c0, dc):\n return (numpy.atleast_1d(cnew) - c0) / dc\n\n def interpolatedF(*coordinates):\n \"\"\"B-spline function over the data grid(x,y,z).\n\n interpolatedF([x1,x2,...],[y1,y2,...],[z1,z2,...]) -> F[x1,y1,z1],F[x2,y2,z2],...\n\n Example usage for resampling::\n >>> XX,YY,ZZ = numpy.mgrid[40:75:0.5, 96:150:0.5, 20:50:0.5]\n >>> FF = _interpolationFunction(XX,YY,ZZ)\n \"\"\"\n _coordinates = numpy.array(\n [_transform(coordinates[i], x0[i], dx[i]) for i in range(len(\n coordinates))])\n return scipy.ndimage.map_coordinates(coeffs,\n _coordinates,\n prefilter=False,\n mode='nearest',\n cval=cval)\n # mode='wrap' would be ideal but is broken: https://github.com/scipy/scipy/issues/1323\n return interpolatedF", "response": "Returns a function F that interpolates any values on the grid."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef read(self, filename):\n if filename is not None:\n self.filename = filename\n with open(self.filename, 'rb') as ccp4:\n h = self.header = self._read_header(ccp4)\n nentries = h['nc'] * h['nr'] * h['ns']\n # Quick and dirty... slurp it all in one go.\n datafmt = h['bsaflag'] + str(nentries) + self._data_bintype\n a = np.array(struct.unpack(datafmt, ccp4.read(struct.calcsize(datafmt))))\n self.header['filename'] = self.filename\n # TODO: Account for the possibility that y-axis is fastest or\n # slowest index, which unfortunately is possible in CCP4.\n order = 'C' if h['mapc'] == 'z' else 'F'\n self.array = a.reshape(h['nc'], h['nr'], h['ns'], order=order)\n self.delta = self._delta()\n self.origin = np.zeros(3)\n self.rank = 3", "response": "Populate the instance from the ccp4 file filename."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _detect_byteorder(ccp4file):\n bsaflag = None\n ccp4file.seek(52 * 4)\n mapbin = ccp4file.read(4)\n for flag in '@=<>':\n mapstr = struct.unpack(flag + '4s', mapbin)[0].decode('utf-8')\n if mapstr.upper() == 'MAP ':\n bsaflag = flag\n break # Only possible value according to spec.\n else:\n raise TypeError(\n \"Cannot decode header --- corrupted or wrong format?\")\n ccp4file.seek(0)\n return bsaflag", "response": "Detect the byteorder of stream ccp4file and return format character."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _read_header(self, ccp4file):\n\n bsaflag = self._detect_byteorder(ccp4file)\n\n # Parse the top of the header (4-byte words, 1 to 25).\n nheader = struct.calcsize(self._headerfmt)\n names = [r.key for r in self._header_struct]\n bintopheader = ccp4file.read(25 * 4)\n\n def decode_header(header, bsaflag='@'):\n h = dict(zip(names, struct.unpack(bsaflag + self._headerfmt,\n header)))\n h['bsaflag'] = bsaflag\n return h\n\n header = decode_header(bintopheader, bsaflag)\n for rec in self._header_struct:\n if not rec.is_legal_dict(header):\n warnings.warn(\n \"Key %s: Illegal value %r\" % (rec.key, header[rec.key]))\n\n # Parse the latter half of the header (4-byte words, 26 to 256).\n if (header['lskflg']):\n skewmatrix = np.fromfile(ccp4file, dtype=np.float32, count=9)\n header['skwmat'] = skewmatrix.reshape((3, 3))\n header['skwtrn'] = np.fromfile(ccp4file, dtype=np.float32, count=3)\n else:\n header['skwmat'] = header['skwtrn'] = None\n ccp4file.seek(12 * 4, 1)\n ccp4file.seek(15 * 4, 1) # Skip future use section.\n ccp4file.seek(4, 1) # Skip map text, already used above to verify format.\n # TODO: Compare file specified endianness to one obtained above.\n endiancode = struct.unpack(bsaflag + '4b', ccp4file.read(4))\n header['endianness'] = 'little' if endiancode == (0x44, 0x41, 0, 0\n ) else 'big'\n header['arms'] = struct.unpack(bsaflag + 'f', ccp4file.read(4))[0]\n header['nlabl'] = struct.unpack(bsaflag + 'I', ccp4file.read(4))[0]\n if header['nlabl']:\n binlabel = ccp4file.read(80 * header['nlabl'])\n flag = bsaflag + str(80 * header['nlabl']) + 's'\n label = struct.unpack(flag, binlabel)[0]\n header['label'] = label.decode('utf-8').rstrip('\\x00')\n else:\n header['label'] = None\n ccp4file.seek(256 * 4)\n # TODO: Parse symmetry records, if any.\n return header", "response": "Read the header bytes and return a tuple of the class ID and the header bytes."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_data(self, **kwargs):\n limit = int(kwargs.get('limit', 288))\n end_date = kwargs.get('end_date', False)\n\n if end_date and isinstance(end_date, datetime.datetime):\n end_date = self.convert_datetime(end_date)\n\n if self.mac_address is not None:\n service_address = 'devices/%s' % self.mac_address\n self.api_instance.log('SERVICE ADDRESS: %s' % service_address)\n\n data = dict(limit=limit)\n\n # If endDate is left blank (not passed in), the most recent results will be returned.\n if end_date:\n data.update({'endDate': end_date})\n\n self.api_instance.log('DATA:')\n self.api_instance.log(data)\n\n return self.api_instance.api_call(service_address, **data)", "response": "Get the data for a specific device for a specific end date"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_devices(self):\n retn = []\n api_devices = self.api_call('devices')\n\n self.log('DEVICES:')\n self.log(api_devices)\n\n for device in api_devices:\n retn.append(AmbientWeatherStation(self, device))\n\n self.log('DEVICE INSTANCE LIST:')\n self.log(retn)\n\n return retn", "response": "Get all devices in the system"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_url(self, path, params={}, opts={}):\n\n if opts:\n warnings.warn('`opts` has been deprecated. Use `params` instead.',\n DeprecationWarning, stacklevel=2)\n params = params or opts\n if self._shard_strategy == SHARD_STRATEGY_CRC:\n crc = zlib.crc32(path.encode('utf-8')) & 0xffffffff\n index = crc % len(self._domains) # Deterministically choose domain\n domain = self._domains[index]\n\n elif self._shard_strategy == SHARD_STRATEGY_CYCLE:\n domain = self._domains[self._shard_next_index]\n self._shard_next_index = (\n self._shard_next_index + 1) % len(self._domains)\n\n else:\n domain = self._domains[0]\n\n scheme = \"https\" if self._use_https else \"http\"\n\n url_obj = UrlHelper(\n domain,\n path,\n scheme,\n sign_key=self._sign_key,\n include_library_param=self._include_library_param,\n params=params)\n\n return str(url_obj)", "response": "Create URL with supplied path and opts parameters dict."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets a url parameter.", "response": "def set_parameter(self, key, value):\n \"\"\"\n Set a url parameter.\n\n Parameters\n ----------\n key : str\n If key ends with '64', the value provided will be automatically\n base64 encoded.\n \"\"\"\n if value is None or isinstance(value, (int, float, bool)):\n value = str(value)\n\n if key.endswith('64'):\n value = urlsafe_b64encode(value.encode('utf-8'))\n value = value.replace(b('='), b(''))\n\n self._parameters[key] = value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstarting subscription manager for real time data.", "response": "async def rt_connect(self, loop):\n \"\"\"Start subscription manager for real time data.\"\"\"\n if self.sub_manager is not None:\n return\n self.sub_manager = SubscriptionManager(\n loop, \"token={}\".format(self._access_token), SUB_ENDPOINT\n )\n self.sub_manager.start()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def update_info(self, *_):\n query = gql(\n \"\"\"\n {\n viewer {\n name\n homes {\n subscriptions {\n status\n }\n id\n }\n }\n }\n \"\"\"\n )\n\n res = await self._execute(query)\n if res is None:\n return\n errors = res.get(\"errors\", [])\n if errors:\n msg = errors[0].get(\"message\", \"failed to login\")\n _LOGGER.error(msg)\n raise InvalidLogin(msg)\n\n data = res.get(\"data\")\n if not data:\n return\n viewer = data.get(\"viewer\")\n if not viewer:\n return\n self._name = viewer.get(\"name\")\n homes = viewer.get(\"homes\", [])\n self._home_ids = []\n for _home in homes:\n home_id = _home.get(\"id\")\n self._all_home_ids += [home_id]\n subs = _home.get(\"subscriptions\")\n if subs:\n status = subs[0].get(\"status\", \"ended\").lower()\n if not home_id or status != \"running\":\n continue\n self._home_ids += [home_id]", "response": "Update home info async."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn list of Tibber homes.", "response": "def get_homes(self, only_active=True):\n \"\"\"Return list of Tibber homes.\"\"\"\n return [self.get_home(home_id) for home_id in self.get_home_ids(only_active)]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def send_notification(self, title, message):\n query = gql(\n \"\"\"\n mutation{\n sendPushNotification(input: {\n title: \"%s\",\n message: \"%s\",\n }){\n successful\n pushedToNumberOfDevices\n }\n }\n \"\"\"\n % (title, message)\n )\n\n res = await self.execute(query)\n if not res:\n return False\n noti = res.get(\"sendPushNotification\", {})\n successful = noti.get(\"successful\", False)\n pushed_to_number_of_devices = noti.get(\"pushedToNumberOfDevices\", 0)\n _LOGGER.debug(\n \"send_notification: status %s, send to %s devices\",\n successful,\n pushed_to_number_of_devices,\n )\n return successful", "response": "Send a notification to the specified user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nupdate current price info async.", "response": "async def update_info(self):\n \"\"\"Update current price info async.\"\"\"\n query = gql(\n \"\"\"\n {\n viewer {\n home(id: \"%s\") {\n appNickname\n features {\n realTimeConsumptionEnabled\n }\n currentSubscription {\n status\n }\n address {\n address1\n address2\n address3\n city\n postalCode\n country\n latitude\n longitude\n }\n meteringPointData {\n consumptionEan\n energyTaxType\n estimatedAnnualConsumption\n gridCompany\n productionEan\n vatType\n }\n owner {\n name\n isCompany\n language\n contactInfo {\n email\n mobile\n }\n }\n timeZone\n subscriptions {\n id\n status\n validFrom\n validTo\n statusReason\n }\n currentSubscription {\n priceInfo {\n current {\n currency\n }\n }\n }\n }\n }\n }\n \"\"\"\n % self._home_id\n )\n self.info = await self._tibber_control.execute(query)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating current price info.", "response": "def sync_update_current_price_info(self):\n \"\"\"Update current price info.\"\"\"\n loop = asyncio.get_event_loop()\n task = loop.create_task(self.update_current_price_info())\n loop.run_until_complete(task)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def update_current_price_info(self):\n query = gql(\n \"\"\"\n {\n viewer {\n home(id: \"%s\") {\n currentSubscription {\n priceInfo {\n current {\n energy\n tax\n total\n startsAt\n }\n }\n }\n }\n }\n }\n \"\"\"\n % self.home_id\n )\n price_info_temp = await self._tibber_control.execute(query)\n if not price_info_temp:\n _LOGGER.error(\"Could not find current price info.\")\n return\n try:\n home = price_info_temp[\"viewer\"][\"home\"]\n current_subscription = home[\"currentSubscription\"]\n price_info = current_subscription[\"priceInfo\"][\"current\"]\n except (KeyError, TypeError):\n _LOGGER.error(\"Could not find current price info.\")\n return\n if price_info:\n self._current_price_info = price_info", "response": "Update the current price info."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating current price info.", "response": "def sync_update_price_info(self):\n \"\"\"Update current price info.\"\"\"\n loop = asyncio.get_event_loop()\n task = loop.create_task(self.update_price_info())\n loop.run_until_complete(task)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def update_price_info(self):\n query = gql(\n \"\"\"\n {\n viewer {\n home(id: \"%s\") {\n currentSubscription {\n priceInfo {\n current {\n energy\n tax\n total\n startsAt\n level\n }\n today {\n total\n startsAt\n level\n }\n tomorrow {\n total\n startsAt\n level\n }\n }\n }\n }\n }\n }\n \"\"\"\n % self.home_id\n )\n price_info_temp = await self._tibber_control.execute(query)\n if not price_info_temp:\n _LOGGER.error(\"Could not find price info.\")\n return\n self._price_info = {}\n self._level_info = {}\n for key in [\"current\", \"today\", \"tomorrow\"]:\n try:\n home = price_info_temp[\"viewer\"][\"home\"]\n current_subscription = home[\"currentSubscription\"]\n price_info = current_subscription[\"priceInfo\"][key]\n except (KeyError, TypeError):\n _LOGGER.error(\"Could not find price info for %s.\", key)\n continue\n if key == \"current\":\n self._current_price_info = price_info\n continue\n for data in price_info:\n self._price_info[data.get(\"startsAt\")] = data.get(\"total\")\n self._level_info[data.get(\"startsAt\")] = data.get(\"level\")", "response": "Update internal state of price info."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef price_unit(self):\n currency = self.currency\n consumption_unit = self.consumption_unit\n if not currency or not consumption_unit:\n _LOGGER.error(\"Could not find price_unit.\")\n return \" \"\n return currency + \"/\" + consumption_unit", "response": "Return the price unit."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def rt_subscribe(self, loop, async_callback):\n if self._subscription_id is not None:\n _LOGGER.error(\"Already subscribed.\")\n return\n await self._tibber_control.rt_connect(loop)\n document = gql(\n \"\"\"\n subscription{\n liveMeasurement(homeId:\"%s\"){\n timestamp\n power\n powerProduction\n accumulatedProduction\n accumulatedConsumption\n accumulatedCost\n currency\n minPower\n averagePower\n maxPower\n voltagePhase1\n voltagePhase2\n voltagePhase3\n currentPhase1\n currentPhase2\n currentPhase3\n lastMeterConsumption\n lastMeterProduction\n }\n }\n \"\"\"\n % self.home_id\n )\n sub_query = print_ast(document)\n\n self._subscription_id = await self._tibber_control.sub_manager.subscribe(\n sub_query, async_callback\n )", "response": "Connect to Tibber and subscribe to RT subscription."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def rt_unsubscribe(self):\n if self._subscription_id is None:\n _LOGGER.error(\"Not subscribed.\")\n return\n await self._tibber_control.sub_manager.unsubscribe(self._subscription_id)", "response": "Unsubscribe to Tibber rt subscription."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rt_subscription_running(self):\n return (\n self._tibber_control.sub_manager is not None\n and self._tibber_control.sub_manager.is_running\n and self._subscription_id is not None\n )", "response": "Is real time subscription running?"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def get_historic_data(self, n_data):\n query = gql(\n \"\"\"\n {\n viewer {\n home(id: \"%s\") {\n consumption(resolution: HOURLY, last: %s) {\n nodes {\n from\n totalCost\n consumption\n }\n }\n }\n }\n }\n \"\"\"\n % (self.home_id, n_data)\n )\n data = await self._tibber_control.execute(query)\n if not data:\n _LOGGER.error(\"Could not find current the data.\")\n return\n data = data[\"viewer\"][\"home\"][\"consumption\"]\n if data is None:\n self._data = []\n return\n self._data = data[\"nodes\"]", "response": "Get the historic data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cleanup_none(self):\n for (prop, default) in self.defaults.items():\n if getattr(self, prop) == '_None':\n setattr(self, prop, None)", "response": "Removes the temporary value set for None attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nbuilds the execution environment.", "response": "def build_environ(self, sock_file, conn):\n \"\"\" Build the execution environment. \"\"\"\n # Grab the request line\n request = self.read_request_line(sock_file)\n\n # Copy the Base Environment\n environ = self.base_environ.copy()\n\n # Grab the headers\n for k, v in self.read_headers(sock_file).items():\n environ[str('HTTP_'+k)] = v\n\n # Add CGI Variables\n environ['REQUEST_METHOD'] = request['method']\n environ['PATH_INFO'] = request['path']\n environ['SERVER_PROTOCOL'] = request['protocol']\n environ['SERVER_PORT'] = str(conn.server_port)\n environ['REMOTE_PORT'] = str(conn.client_port)\n environ['REMOTE_ADDR'] = str(conn.client_addr)\n environ['QUERY_STRING'] = request['query_string']\n if 'HTTP_CONTENT_LENGTH' in environ:\n environ['CONTENT_LENGTH'] = environ['HTTP_CONTENT_LENGTH']\n if 'HTTP_CONTENT_TYPE' in environ:\n environ['CONTENT_TYPE'] = environ['HTTP_CONTENT_TYPE']\n\n # Save the request method for later\n self.request_method = environ['REQUEST_METHOD']\n\n # Add Dynamic WSGI Variables\n if conn.ssl:\n environ['wsgi.url_scheme'] = 'https'\n environ['HTTPS'] = 'on'\n else:\n environ['wsgi.url_scheme'] = 'http'\n\n if environ.get('HTTP_TRANSFER_ENCODING', '') == 'chunked':\n environ['wsgi.input'] = ChunkedReader(sock_file)\n else:\n environ['wsgi.input'] = sock_file\n\n return environ"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite the data to the output socket.", "response": "def write(self, data, sections=None):\n \"\"\" Write the data to the output socket. \"\"\"\n\n if self.error[0]:\n self.status = self.error[0]\n data = b(self.error[1])\n\n if not self.headers_sent:\n self.send_headers(data, sections)\n\n if self.request_method != 'HEAD':\n try:\n if self.chunked:\n self.conn.sendall(b('%x\\r\\n%s\\r\\n' % (len(data), data)))\n else:\n self.conn.sendall(data)\n except socket.timeout:\n self.closeConnection = True\n except socket.error:\n # But some clients will close the connection before that\n # resulting in a socket error.\n self.closeConnection = True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef start_response(self, status, response_headers, exc_info=None):\n if exc_info:\n try:\n if self.headers_sent:\n # Re-raise original exception if headers sent\n # because this violates WSGI specification.\n raise\n finally:\n exc_info = None\n elif self.header_set:\n raise AssertionError(\"Headers already set!\")\n\n if PY3K and not isinstance(status, str):\n self.status = str(status, 'ISO-8859-1')\n else:\n self.status = status\n # Make sure headers are bytes objects\n try:\n self.header_set = Headers(response_headers)\n except UnicodeDecodeError:\n self.error = ('500 Internal Server Error',\n 'HTTP Headers should be bytes')\n self.err_log.error('Received HTTP Headers from client that contain'\n ' invalid characters for Latin-1 encoding.')\n\n return self.write_warning", "response": "Store the HTTP status and headers to be sent when self. write is called."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_bgp_neighbors(self):\n def generate_vrf_query(vrf_name):\n \"\"\"\n Helper to provide XML-query for the VRF-type we're interested in.\n \"\"\"\n if vrf_name == \"global\":\n rpc_command = '\\\n default\\\n \\\n '\n\n else:\n rpc_command = '\\\n default\\\n {vrf_name}\\\n \\\n '.format(vrf_name=vrf_name)\n return rpc_command\n\n \"\"\"\n Initial run to figure out what VRF's are available\n Decided to get this one from Configured-section\n because bulk-getting all instance-data to do the same could get ridiculously heavy\n Assuming we're always interested in the DefaultVRF\n \"\"\"\n\n active_vrfs = [\"global\"]\n\n rpc_command = '\\\n default\\\n '\n\n result_tree = ETREE.fromstring(self.device.make_rpc_call(rpc_command))\n\n for node in result_tree.xpath('.//ConfigVRF'):\n active_vrfs.append(napalm_base.helpers.find_txt(node, 'Naming/VRFName'))\n\n result = {}\n\n for vrf in active_vrfs:\n rpc_command = generate_vrf_query(vrf)\n result_tree = ETREE.fromstring(self.device.make_rpc_call(rpc_command))\n\n this_vrf = {}\n this_vrf['peers'] = {}\n\n if vrf == \"global\":\n this_vrf['router_id'] = napalm_base.helpers.convert(\n text_type, napalm_base.helpers.find_txt(result_tree,\n 'Get/Operational/BGP/InstanceTable/Instance/InstanceActive/DefaultVRF\\\n /GlobalProcessInfo/VRF/RouterID'))\n else:\n this_vrf['router_id'] = napalm_base.helpers.convert(\n text_type, napalm_base.helpers.find_txt(result_tree,\n 'Get/Operational/BGP/InstanceTable/Instance/InstanceActive/VRFTable/VRF\\\n /GlobalProcessInfo/VRF/RouterID'))\n\n neighbors = {}\n\n for neighbor in result_tree.xpath('.//Neighbor'):\n this_neighbor = {}\n this_neighbor['local_as'] = napalm_base.helpers.convert(\n int, napalm_base.helpers.find_txt(neighbor, 'LocalAS'))\n this_neighbor['remote_as'] = napalm_base.helpers.convert(\n int, napalm_base.helpers.find_txt(neighbor, 'RemoteAS'))\n this_neighbor['remote_id'] = napalm_base.helpers.convert(\n text_type, napalm_base.helpers.find_txt(neighbor, 'RouterID'))\n\n if napalm_base.helpers.find_txt(neighbor, 'ConnectionAdminStatus') is \"1\":\n this_neighbor['is_enabled'] = True\n try:\n this_neighbor['description'] = napalm_base.helpers.convert(\n text_type, napalm_base.helpers.find_txt(neighbor, 'Description'))\n except AttributeError:\n this_neighbor['description'] = u''\n\n this_neighbor['is_enabled'] = (\n napalm_base.helpers.find_txt(neighbor, 'ConnectionAdminStatus') == \"1\")\n\n if str(napalm_base.helpers.find_txt(neighbor, 'ConnectionAdminStatus')) is \"1\":\n this_neighbor['is_enabled'] = True\n else:\n this_neighbor['is_enabled'] = False\n\n if str(napalm_base.helpers.find_txt(neighbor, 'ConnectionState')) == \"BGP_ST_ESTAB\":\n this_neighbor['is_up'] = True\n this_neighbor['uptime'] = napalm_base.helpers.convert(\n int, napalm_base.helpers.find_txt(neighbor, 'ConnectionEstablishedTime'))\n else:\n this_neighbor['is_up'] = False\n this_neighbor['uptime'] = -1\n\n this_neighbor['address_family'] = {}\n\n if napalm_base.helpers.find_txt(neighbor,\n 'ConnectionRemoteAddress/AFI') == \"IPv4\":\n this_afi = \"ipv4\"\n elif napalm_base.helpers.find_txt(neighbor,\n 'ConnectionRemoteAddress/AFI') == \"IPv6\":\n this_afi = \"ipv6\"\n else:\n this_afi = napalm_base.helpers.find_txt(neighbor, 'ConnectionRemoteAddress/AFI')\n\n this_neighbor['address_family'][this_afi] = {}\n\n try:\n this_neighbor['address_family'][this_afi][\"received_prefixes\"] = \\\n napalm_base.helpers.convert(int,\n napalm_base.helpers.find_txt(\n neighbor, 'AFData/Entry/PrefixesAccepted'), 0) + \\\n napalm_base.helpers.convert(int,\n napalm_base.helpers.find_txt(\n neighbor, 'AFData/Entry/PrefixesDenied'), 0)\n this_neighbor['address_family'][this_afi][\"accepted_prefixes\"] = \\\n napalm_base.helpers.convert(int,\n napalm_base.helpers.find_txt(\n neighbor, 'AFData/Entry/PrefixesAccepted'), 0)\n this_neighbor['address_family'][this_afi][\"sent_prefixes\"] = \\\n napalm_base.helpers.convert(int,\n napalm_base.helpers.find_txt(\n neighbor, 'AFData/Entry/PrefixesAdvertised'), 0)\n except AttributeError:\n this_neighbor['address_family'][this_afi][\"received_prefixes\"] = -1\n this_neighbor['address_family'][this_afi][\"accepted_prefixes\"] = -1\n this_neighbor['address_family'][this_afi][\"sent_prefixes\"] = -1\n\n neighbor_ip = napalm_base.helpers.ip(\n napalm_base.helpers.find_txt(\n neighbor, 'Naming/NeighborAddress/IPV4Address') or\n napalm_base.helpers.find_txt(\n neighbor, 'Naming/NeighborAddress/IPV6Address')\n )\n\n neighbors[neighbor_ip] = this_neighbor\n\n this_vrf['peers'] = neighbors\n result[vrf] = this_vrf\n\n return result", "response": "Get all BGP neighbors for this instance."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\naggregating a list of prefixes.", "response": "def aggregate(l):\n \"\"\"Aggregate a `list` of prefixes.\n\n Keyword arguments:\n l -- a python list of prefixes\n\n Example use:\n >>> aggregate([\"10.0.0.0/8\", \"10.0.0.0/24\"])\n ['10.0.0.0/8']\n \"\"\"\n tree = radix.Radix()\n for item in l:\n try:\n tree.add(item)\n except (ValueError) as err:\n raise Exception(\"ERROR: invalid IP prefix: {}\".format(item))\n\n return aggregate_tree(tree).prefixes()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef aggregate_tree(l_tree):\n\n def _aggregate_phase1(tree):\n # phase1 removes any supplied prefixes which are superfluous because\n # they are already included in another supplied prefix. For example,\n # 2001:67c:208c:10::/64 would be removed if 2001:67c:208c::/48 was\n # also supplied.\n n_tree = radix.Radix()\n for prefix in tree.prefixes():\n if tree.search_worst(prefix).prefix == prefix:\n n_tree.add(prefix)\n return n_tree\n\n def _aggregate_phase2(tree):\n # phase2 identifies adjacent prefixes that can be combined under a\n # single, shorter-length prefix. For example, 2001:67c:208c::/48 and\n # 2001:67c:208d::/48 can be combined into the single prefix\n # 2001:67c:208c::/47.\n n_tree = radix.Radix()\n for rnode in tree:\n p = text(ip_network(text(rnode.prefix)).supernet())\n r = tree.search_covered(p)\n if len(r) == 2:\n if r[0].prefixlen == r[1].prefixlen == rnode.prefixlen:\n n_tree.add(p)\n else:\n n_tree.add(rnode.prefix)\n else:\n n_tree.add(rnode.prefix)\n return n_tree\n\n l_tree = _aggregate_phase1(l_tree)\n\n if len(l_tree.prefixes()) == 1:\n return l_tree\n\n while True:\n r_tree = _aggregate_phase2(l_tree)\n if l_tree.prefixes() == r_tree.prefixes():\n break\n else:\n l_tree = r_tree\n del r_tree\n\n return l_tree", "response": "Walk a py - radix tree and aggregate it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _ratio_metric(v1, v2, **_kwargs):\n return (((v1 - v2) / (v1 + v2)) ** 2) if v1 + v2 != 0 else 0", "response": "Metric for ratio data."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncomputes the coincidences of a single resource in a single resource.", "response": "def _coincidences(value_counts, value_domain, dtype=np.float64):\n \"\"\"Coincidence matrix.\n\n Parameters\n ----------\n value_counts : ndarray, with shape (N, V)\n Number of coders that assigned a certain value to a determined unit, where N is the number of units\n and V is the value count.\n\n value_domain : array_like, with shape (V,)\n Possible values V the units can take.\n If the level of measurement is not nominal, it must be ordered.\n\n dtype : data-type\n Result and computation data-type.\n\n Returns\n -------\n o : ndarray, with shape (V, V)\n Coincidence matrix.\n \"\"\"\n value_counts_matrices = value_counts.reshape(value_counts.shape + (1,))\n pairable = np.maximum(np.sum(value_counts, axis=1), 2)\n diagonals = np.tile(np.eye(len(value_domain)), (len(value_counts), 1, 1)) \\\n * value_counts.reshape((value_counts.shape[0], 1, value_counts.shape[1]))\n unnormalized_coincidences = value_counts_matrices * value_counts_matrices.transpose((0, 2, 1)) - diagonals\n return np.sum(np.divide(unnormalized_coincidences, (pairable - 1).reshape((-1, 1, 1)), dtype=dtype), axis=0)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the distances of the different possible values.", "response": "def _distances(value_domain, distance_metric, n_v):\n \"\"\"Distances of the different possible values.\n\n Parameters\n ----------\n value_domain : array_like, with shape (V,)\n Possible values V the units can take.\n If the level of measurement is not nominal, it must be ordered.\n\n distance_metric : callable\n Callable that return the distance of two given values.\n\n n_v : ndarray, with shape (V,)\n Number of pairable elements for each value.\n\n Returns\n -------\n d : ndarray, with shape (V, V)\n Distance matrix for each value pair.\n \"\"\"\n return np.array([[distance_metric(v1, v2, i1=i1, i2=i2, n_v=n_v)\n for i2, v2 in enumerate(value_domain)]\n for i1, v1 in enumerate(value_domain)])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _reliability_data_to_value_counts(reliability_data, value_domain):\n return np.array([[sum(1 for rate in unit if rate == v) for v in value_domain] for unit in reliability_data.T])", "response": "Return the value counts given the reliability data."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef alpha(reliability_data=None, value_counts=None, value_domain=None, level_of_measurement='interval',\n dtype=np.float64):\n \"\"\"Compute Krippendorff's alpha.\n\n See https://en.wikipedia.org/wiki/Krippendorff%27s_alpha for more information.\n\n Parameters\n ----------\n reliability_data : array_like, with shape (M, N)\n Reliability data matrix which has the rate the i coder gave to the j unit, where M is the number of raters\n and N is the unit count.\n Missing rates are represented with `np.nan`.\n If it's provided then `value_counts` must not be provided.\n\n value_counts : ndarray, with shape (N, V)\n Number of coders that assigned a certain value to a determined unit, where N is the number of units\n and V is the value count.\n If it's provided then `reliability_data` must not be provided.\n\n value_domain : array_like, with shape (V,)\n Possible values the units can take.\n If the level of measurement is not nominal, it must be ordered.\n If `reliability_data` is provided, then the default value is the ordered list of unique rates that appear.\n Else, the default value is `list(range(V))`.\n\n level_of_measurement : string or callable\n Steven's level of measurement of the variable.\n It must be one of 'nominal', 'ordinal', 'interval', 'ratio' or a callable.\n\n dtype : data-type\n Result and computation data-type.\n\n Returns\n -------\n alpha : `dtype`\n Scalar value of Krippendorff's alpha of type `dtype`.\n\n Examples\n --------\n >>> reliability_data = [[np.nan, np.nan, np.nan, np.nan, np.nan, 3, 4, 1, 2, 1, 1, 3, 3, np.nan, 3],\n ... [1, np.nan, 2, 1, 3, 3, 4, 3, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],\n ... [np.nan, np.nan, 2, 1, 3, 4, 4, np.nan, 2, 1, 1, 3, 3, np.nan, 4]]\n >>> print(round(alpha(reliability_data=reliability_data, level_of_measurement='nominal'), 6))\n 0.691358\n >>> print(round(alpha(reliability_data=reliability_data, level_of_measurement='interval'), 6))\n 0.810845\n >>> value_counts = np.array([[1, 0, 0, 0],\n ... [0, 0, 0, 0],\n ... [0, 2, 0, 0],\n ... [2, 0, 0, 0],\n ... [0, 0, 2, 0],\n ... [0, 0, 2, 1],\n ... [0, 0, 0, 3],\n ... [1, 0, 1, 0],\n ... [0, 2, 0, 0],\n ... [2, 0, 0, 0],\n ... [2, 0, 0, 0],\n ... [0, 0, 2, 0],\n ... [0, 0, 2, 0],\n ... [0, 0, 0, 0],\n ... [0, 0, 1, 1]])\n >>> print(round(alpha(value_counts=value_counts, level_of_measurement='nominal'), 6))\n 0.691358\n >>> # The following examples were extracted from\n >>> # https://www.statisticshowto.datasciencecentral.com/wp-content/uploads/2016/07/fulltext.pdf, page 8.\n >>> reliability_data = [[1, 2, 3, 3, 2, 1, 4, 1, 2, np.nan, np.nan, np.nan],\n ... [1, 2, 3, 3, 2, 2, 4, 1, 2, 5, np.nan, 3.],\n ... [np.nan, 3, 3, 3, 2, 3, 4, 2, 2, 5, 1, np.nan],\n ... [1, 2, 3, 3, 2, 4, 4, 1, 2, 5, 1, np.nan]]\n >>> print(round(alpha(reliability_data, level_of_measurement='ordinal'), 3))\n 0.815\n >>> print(round(alpha(reliability_data, level_of_measurement='ratio'), 3))\n 0.797\n \"\"\"\n if (reliability_data is None) == (value_counts is None):\n raise ValueError(\"Either reliability_data or value_counts must be provided, but not both.\")\n\n # Don't know if it's a list or numpy array. If it's the latter, the truth value is ambiguous. So, ask for None.\n if value_counts is None:\n if type(reliability_data) is not np.ndarray:\n reliability_data = np.array(reliability_data)\n\n value_domain = value_domain or np.unique(reliability_data[~np.isnan(reliability_data)])\n\n value_counts = _reliability_data_to_value_counts(reliability_data, value_domain)\n else: # elif reliability_data is None\n if value_domain:\n assert value_counts.shape[1] == len(value_domain), \\\n \"The value domain should be equal to the number of columns of value_counts.\"\n else:\n value_domain = tuple(range(value_counts.shape[1]))\n\n distance_metric = _distance_metric(level_of_measurement)\n\n o = _coincidences(value_counts, value_domain, dtype=dtype)\n n_v = np.sum(o, axis=0)\n n = np.sum(n_v)\n e = _random_coincidences(value_domain, n, n_v)\n d = _distances(value_domain, distance_metric, n_v)\n return 1 - np.sum(o * d) / np.sum(e * d)", "response": "Compute the alpha of a set of reliability data for a particular set of coders."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmaps to CDF_Inquire. inquire and returns pysatCDF instance. Not intended for regular direct use by user.", "response": "def inquire(self):\n \"\"\"Maps to fortran CDF_Inquire.\n\n Assigns parameters returned by CDF_Inquire\n to pysatCDF instance. Not intended\n for regular direct use by user.\n\n \"\"\"\n\n name = copy.deepcopy(self.fname)\n stats = fortran_cdf.inquire(name)\n\n # break out fortran output into something meaningful\n status = stats[0]\n if status == 0:\n self._num_dims = stats[1]\n self._dim_sizes = stats[2]\n self._encoding = stats[3]\n self._majority = stats[4]\n self._max_rec = stats[5]\n self._num_r_vars = stats[6]\n self._num_z_vars = stats[7]\n self._num_attrs = stats[8]\n else:\n raise IOError(fortran_cdf.statusreporter(status))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _read_all_z_variable_info(self):\n\n self.z_variable_info = {}\n self.z_variable_names_by_num = {}\n\n # call Fortran that grabs all of the basic stats on all of the\n # zVariables in one go.\n info = fortran_cdf.z_var_all_inquire(self.fname, self._num_z_vars,\n len(self.fname))\n status = info[0]\n data_types = info[1]\n num_elems = info[2]\n rec_varys = info[3]\n dim_varys = info[4]\n num_dims = info[5]\n dim_sizes = info[6]\n rec_nums = info[7]\n var_nums = info[8]\n var_names = info[9]\n\n if status == 0:\n for i in np.arange(len(data_types)):\n out = {}\n out['data_type'] = data_types[i]\n out['num_elems'] = num_elems[i]\n out['rec_vary'] = rec_varys[i]\n out['dim_varys'] = dim_varys[i]\n out['num_dims'] = num_dims[i]\n # only looking at first possible extra dimension\n out['dim_sizes'] = dim_sizes[i, :1]\n if out['dim_sizes'][0] == 0:\n out['dim_sizes'][0] += 1\n out['rec_num'] = rec_nums[i]\n out['var_num'] = var_nums[i]\n var_name = ''.join(var_names[i].astype('U'))\n out['var_name'] = var_name.rstrip()\n self.z_variable_info[out['var_name']] = out\n self.z_variable_names_by_num[out['var_num']] = var_name\n else:\n raise IOError(fortran_cdf.statusreporter(status))", "response": "Reads all CDF z - variable information from the file."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef load_all_variables(self):\n\n self.data = {}\n # need to add r variable names\n file_var_names = self.z_variable_info.keys()\n # collect variable information for each\n # organize it neatly for fortran call\n dim_sizes = []\n rec_nums = []\n data_types = []\n names = []\n for i, name in enumerate(file_var_names):\n dim_sizes.extend(self.z_variable_info[name]['dim_sizes'])\n rec_nums.append(self.z_variable_info[name]['rec_num'])\n data_types.append(self.z_variable_info[name]['data_type'])\n names.append(name.ljust(256))\n dim_sizes = np.array(dim_sizes)\n rec_nums = np.array(rec_nums)\n data_types = np.array(data_types)\n # individually load all variables by each data type\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['real4'],\n fortran_cdf.get_multi_z_real4)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['float'],\n fortran_cdf.get_multi_z_real4)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['real8'],\n fortran_cdf.get_multi_z_real8)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['double'],\n fortran_cdf.get_multi_z_real8)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['int4'],\n fortran_cdf.get_multi_z_int4)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['uint4'],\n fortran_cdf.get_multi_z_int4,\n data_offset=2 ** 32)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['int2'],\n fortran_cdf.get_multi_z_int2)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['uint2'],\n fortran_cdf.get_multi_z_int2,\n data_offset=2 ** 16)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['int1'],\n fortran_cdf.get_multi_z_int1)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['uint1'],\n fortran_cdf.get_multi_z_int1,\n data_offset=2 ** 8)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['byte'],\n fortran_cdf.get_multi_z_int1)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['epoch'],\n fortran_cdf.get_multi_z_real8,\n epoch=True)\n self._call_multi_fortran_z(names, data_types, rec_nums, 2 * dim_sizes,\n self.cdf_data_types['epoch16'],\n fortran_cdf.get_multi_z_epoch16,\n epoch16=True)\n self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,\n self.cdf_data_types['TT2000'],\n fortran_cdf.get_multi_z_tt2000,\n epoch=True)\n # mark data has been loaded\n self.data_loaded = True", "response": "Loads all variables from CDF."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _call_multi_fortran_z(self, names, data_types, rec_nums,\n dim_sizes, input_type_code, func,\n epoch=False, data_offset=None, epoch16=False):\n \"\"\"Calls fortran functions to load CDF variable data\n\n Parameters\n ----------\n names : list_like\n list of variables names\n data_types : list_like\n list of all loaded data type codes as used by CDF\n rec_nums : list_like\n list of record numbers in CDF file. Provided by variable_info\n dim_sizes :\n list of dimensions as provided by variable_info.\n input_type_code : int\n Specific type code to load\n func : function\n Fortran function via python interface that will be used for actual loading.\n epoch : bool\n Flag indicating type is epoch. Translates things to datetime standard.\n data_offset :\n Offset value to be applied to data. Required for unsigned integers in CDF.\n epoch16 : bool\n Flag indicating type is epoch16. Translates things to datetime standard.\n\n \n \"\"\"\n\n # isolate input type code variables from total supplied types\n idx, = np.where(data_types == input_type_code)\n\n if len(idx) > 0:\n # read all data of a given type at once\n max_rec = rec_nums[idx].max()\n sub_names = np.array(names)[idx]\n sub_sizes = dim_sizes[idx]\n status, data = func(self.fname, sub_names.tolist(),\n sub_sizes, sub_sizes.sum(), max_rec, len(sub_names))\n if status == 0:\n # account for quirks of CDF data storage for certain types\n if data_offset is not None:\n data = data.astype(int)\n idx, idy, = np.where(data < 0)\n data[idx, idy] += data_offset\n if epoch:\n # account for difference in seconds between\n # CDF epoch and python's epoch, leap year in there\n # (datetime(1971,1,2) - \n # datetime(1,1,1)).total_seconds()*1000\n data -= 62167219200000\n data = data.astype(' 0)\n\n data_types = data_types[idx]\n num_elems = num_elems[idx]\n entry_nums = entry_nums[idx]\n attr_nums = attr_nums[idx]\n attr_names = np.array(attr_names)[idx]\n # grad corresponding variable name for each attribute\n var_names = [self.z_variable_names_by_num[i].rstrip() for i in entry_nums]\n\n # the names that go along with this are already set up\n\n # in attr_names\n # chunk by data type, grab largest num_elems\n\n # get data back, shorten to num_elems, add to structure\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['real4'],\n fortran_cdf.get_multi_z_attr_real4)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['float'],\n fortran_cdf.get_multi_z_attr_real4)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['real8'],\n fortran_cdf.get_multi_z_attr_real8)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['double'],\n fortran_cdf.get_multi_z_attr_real8)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['byte'],\n fortran_cdf.get_multi_z_attr_int1)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['int1'],\n fortran_cdf.get_multi_z_attr_int1)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['uint1'],\n fortran_cdf.get_multi_z_attr_int1,\n data_offset=256)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['int2'],\n fortran_cdf.get_multi_z_attr_int2)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['uint2'],\n fortran_cdf.get_multi_z_attr_int2,\n data_offset=65536)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['int4'],\n fortran_cdf.get_multi_z_attr_int4)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['uint4'],\n fortran_cdf.get_multi_z_attr_int4,\n data_offset=2 ** 32)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['char'],\n fortran_cdf.get_multi_z_attr_char)\n self._call_multi_fortran_z_attr(attr_names, data_types, num_elems,\n entry_nums, attr_nums, var_names, self.cdf_data_types['uchar'],\n fortran_cdf.get_multi_z_attr_char)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _call_multi_fortran_z_attr(self, names, data_types, num_elems,\n entry_nums, attr_nums, var_names,\n input_type_code, func, data_offset=None):\n \"\"\"Calls Fortran function that reads attribute data.\n \n data_offset translates unsigned into signed.\n If number read in is negative, offset added.\n \"\"\"\n # isolate input type code variables\n idx, = np.where(data_types == input_type_code)\n\n if len(idx) > 0:\n # maximimum array dimension\n max_num = num_elems[idx].max()\n sub_num_elems = num_elems[idx]\n sub_names = np.array(names)[idx]\n sub_var_names = np.array(var_names)[idx]\n # zVariable numbers, 'entry' number\n sub_entry_nums = entry_nums[idx]\n # attribute number\n sub_attr_nums = attr_nums[idx]\n status, data = func(self.fname, sub_attr_nums, sub_entry_nums,\n len(sub_attr_nums), max_num, len(self.fname))\n if (status == 0).all():\n if data_offset is not None:\n data = data.astype(int)\n idx, idy, = np.where(data < 0)\n data[idx, idy] += data_offset\n self._process_return_multi_z_attr(data, sub_names,\n sub_var_names, sub_num_elems)\n else:\n # raise ValueError('CDF Error code :', status)\n idx, = np.where(status != 0)\n # raise first error\n raise IOError(fortran_cdf.statusreporter(status[idx][0]))", "response": "Calls the function that reads attribute data."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _process_return_multi_z_attr(self, data, attr_names, var_names, sub_num_elems):\n '''process and attach data from fortran_cdf.get_multi_*'''\n # process data\n\n for i, (attr_name, var_name, num_e) in enumerate(zip(attr_names, var_names, sub_num_elems)):\n if var_name not in self.meta.keys():\n self.meta[var_name] = {}\n if num_e == 1:\n self.meta[var_name][attr_name] = data[i, 0]\n else:\n if data[i].dtype == '|S1':\n self.meta[var_name][attr_name] = ''.join(data[i, 0:num_e].astype('U')).rstrip()\n else:\n self.meta[var_name][attr_name] = data[i, 0:num_e]", "response": "process and attach data from fortran_cdf. get_multi_*'"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef to_pysat(self, flatten_twod=True, units_label='UNITS', name_label='long_name',\n fill_label='FILLVAL', plot_label='FieldNam', \n min_label='ValidMin', max_label='ValidMax', \n notes_label='Var_Notes', desc_label='CatDesc',\n axis_label = 'LablAxis'):\n \"\"\"\n Exports loaded CDF data into data, meta for pysat module\n \n Notes\n -----\n The *_labels should be set to the values in the file, if present.\n Note that once the meta object returned from this function is attached\n to a pysat.Instrument object then the *_labels on the Instrument\n are assigned to the newly attached Meta object.\n \n The pysat Meta object will use data with labels that match the patterns\n in *_labels even if the case does not match.\n\n Parameters\n ----------\n flatten_twod : bool (True)\n If True, then two dimensional data is flattened across \n columns. Name mangling is used to group data, first column\n is 'name', last column is 'name_end'. In between numbers are \n appended 'name_1', 'name_2', etc. All data for a given 2D array\n may be accessed via, data.ix[:,'item':'item_end']\n If False, then 2D data is stored as a series of DataFrames, \n indexed by Epoch. data.ix[0, 'item']\n units_label : str\n Identifier within metadata for units. Defults to CDAWab standard.\n name_label : str\n Identifier within metadata for variable name. Defults to 'long_name',\n not normally present within CDAWeb files. If not, will use values\n from the variable name in the file.\n fill_label : str\n Identifier within metadata for Fill Values. Defults to CDAWab standard.\n plot_label : str\n Identifier within metadata for variable name used when plotting.\n Defults to CDAWab standard.\n min_label : str\n Identifier within metadata for minimim variable value. \n Defults to CDAWab standard.\n max_label : str\n Identifier within metadata for maximum variable value.\n Defults to CDAWab standard.\n notes_label : str\n Identifier within metadata for notes. Defults to CDAWab standard.\n desc_label : str\n Identifier within metadata for a variable description.\n Defults to CDAWab standard.\n axis_label : str\n Identifier within metadata for axis name used when plotting. \n Defults to CDAWab standard.\n \n \n Returns\n -------\n pandas.DataFrame, pysat.Meta\n Data and Metadata suitable for attachment to a pysat.Instrument\n object.\n \n \"\"\"\n\n import string\n import pysat\n import pandas\n\n # copy data\n cdata = self.data.copy()\n #\n # create pysat.Meta object using data above\n # and utilizing the attribute labels provided by the user\n meta = pysat.Meta(pysat.DataFrame.from_dict(self.meta, orient='index'),\n units_label=units_label, name_label=name_label,\n fill_label=fill_label, plot_label=plot_label,\n min_label=min_label, max_label=max_label,\n notes_label=notes_label, desc_label=desc_label,\n axis_label=axis_label)\n \n # account for different possible cases for Epoch, epoch, EPOCH, epOch\n lower_names = [name.lower() for name in meta.keys()] \n for name, true_name in zip(lower_names, meta.keys()):\n if name == 'epoch':\n meta.data.rename(index={true_name: 'Epoch'}, inplace=True)\n epoch = cdata.pop(true_name)\n cdata['Epoch'] = epoch\n\n # ready to format data, iterate over all of the data names\n # and put into a pandas DataFrame\n two_d_data = []\n drop_list = []\n for name in cdata.keys():\n temp = np.shape(cdata[name])\n # treat 2 dimensional data differently\n if len(temp) == 2:\n if not flatten_twod:\n # put 2D data into a Frame at each time\n # remove data from dict when adding to the DataFrame\n frame = pysat.DataFrame(cdata[name].flatten(), columns=[name])\n drop_list.append(name)\n\n step = temp[0]\n new_list = []\n new_index = np.arange(step)\n for i in np.arange(len(epoch)):\n new_list.append(frame.iloc[i*step:(i+1)*step, :])\n new_list[-1].index = new_index\n #new_frame = pandas.DataFrame.from_records(new_list, index=epoch, columns=[name])\n new_frame = pandas.Series(new_list, index=epoch, name=name)\n two_d_data.append(new_frame)\n\n else:\n # flatten 2D into series of 1D columns\n new_names = [name + '_{i}'.format(i=i) for i in np.arange(temp[0] - 2)]\n new_names.append(name + '_end')\n new_names.insert(0, name)\n # remove data from dict when adding to the DataFrame\n drop_list.append(name)\n frame = pysat.DataFrame(cdata[name].T,\n index=epoch,\n columns=new_names)\n two_d_data.append(frame)\n for name in drop_list:\n _ = cdata.pop(name)\n # all of the data left over is 1D, add as Series\n data = pysat.DataFrame(cdata, index=epoch)\n two_d_data.append(data)\n data = pandas.concat(two_d_data, axis=1)\n data.drop('Epoch', axis=1, inplace=True)\n return data, meta", "response": "Converts the CDF data into a pysat. Instrument object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _uptime_linux():\n # With procfs\n try:\n f = open('/proc/uptime', 'r')\n up = float(f.readline().split()[0])\n f.close()\n return up\n except (IOError, ValueError):\n pass\n\n # Without procfs (really?)\n try:\n libc = ctypes.CDLL('libc.so')\n except AttributeError:\n return None\n except OSError:\n # Debian and derivatives do the wrong thing because /usr/lib/libc.so\n # is a GNU ld script rather than an ELF object. To get around this, we\n # have to be more specific.\n # We don't want to use ctypes.util.find_library because that creates a\n # new process on Linux. We also don't want to try too hard because at\n # this point we're already pretty sure this isn't Linux.\n try:\n libc = ctypes.CDLL('libc.so.6')\n except OSError:\n return None\n\n if not hasattr(libc, 'sysinfo'):\n # Not Linux.\n return None\n\n buf = ctypes.create_string_buffer(128) # 64 suffices on 32-bit, whatever.\n if libc.sysinfo(buf) < 0:\n return None\n\n up = struct.unpack_from('@l', buf.raw)[0]\n if up < 0:\n up = None\n return up", "response": "Returns uptime in seconds or None on Linux."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn uptime in seconds on AmigaOS", "response": "def _uptime_amiga():\n \"\"\"Returns uptime in seconds or None, on AmigaOS.\"\"\"\n global __boottime\n try:\n __boottime = os.stat('RAM:').st_ctime\n return time.time() - __boottime\n except (NameError, OSError):\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns uptime on BeOS or Haiku.", "response": "def _uptime_beos():\n \"\"\"Returns uptime in seconds on None, on BeOS/Haiku.\"\"\"\n try:\n libroot = ctypes.CDLL('libroot.so')\n except (AttributeError, OSError):\n return None\n\n if not hasattr(libroot, 'system_time'):\n return None\n\n libroot.system_time.restype = ctypes.c_int64\n return libroot.system_time() / 1000000."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _uptime_bsd():\n global __boottime\n try:\n libc = ctypes.CDLL('libc.so')\n except AttributeError:\n return None\n except OSError:\n # OS X; can't use ctypes.util.find_library because that creates\n # a new process on Linux, which is undesirable.\n try:\n libc = ctypes.CDLL('libc.dylib')\n except OSError:\n return None\n\n if not hasattr(libc, 'sysctlbyname'):\n # Not BSD.\n return None\n\n # Determine how much space we need for the response.\n sz = ctypes.c_uint(0)\n libc.sysctlbyname('kern.boottime', None, ctypes.byref(sz), None, 0)\n if sz.value != struct.calcsize('@LL'):\n # Unexpected, let's give up.\n return None\n\n # For real now.\n buf = ctypes.create_string_buffer(sz.value)\n libc.sysctlbyname('kern.boottime', buf, ctypes.byref(sz), None, 0)\n sec, usec = struct.unpack('@LL', buf.raw)\n\n # OS X disagrees what that second value is.\n if usec > 1000000:\n usec = 0.\n\n __boottime = sec + usec / 1000000.\n up = time.time() - __boottime\n if up < 0:\n up = None\n return up", "response": "Returns uptime in seconds or None on BSD."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn uptime in seconds or None on MINIX.", "response": "def _uptime_minix():\n \"\"\"Returns uptime in seconds or None, on MINIX.\"\"\"\n try:\n f = open('/proc/uptime', 'r')\n up = float(f.read())\n f.close()\n return up\n except (IOError, ValueError):\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _uptime_plan9():\n # Apparently Plan 9 only has Python 2.2, which I'm not prepared to\n # support. Maybe some Linuxes implement /dev/time, though, someone was\n # talking about it somewhere.\n try:\n # The time file holds one 32-bit number representing the sec-\n # onds since start of epoch and three 64-bit numbers, repre-\n # senting nanoseconds since start of epoch, clock ticks, and\n # clock frequency.\n # -- cons(3)\n f = open('/dev/time', 'r')\n s, ns, ct, cf = f.read().split()\n f.close()\n return float(ct) / float(cf)\n except (IOError, ValueError):\n return None", "response": "Returns uptime in seconds or None on Plan 9."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns uptime in seconds or None on Solaris.", "response": "def _uptime_solaris():\n \"\"\"Returns uptime in seconds or None, on Solaris.\"\"\"\n global __boottime\n try:\n kstat = ctypes.CDLL('libkstat.so')\n except (AttributeError, OSError):\n return None\n\n # kstat doesn't have uptime, but it does have boot time.\n # Unfortunately, getting at it isn't perfectly straightforward.\n # First, let's pretend to be kstat.h\n\n # Constant\n KSTAT_STRLEN = 31 # According to every kstat.h I could find.\n\n # Data structures\n class anon_union(ctypes.Union):\n # The ``value'' union in kstat_named_t actually has a bunch more\n # members, but we're only using it for boot_time, so we only need\n # the padding and the one we're actually using.\n _fields_ = [('c', ctypes.c_char * 16),\n ('time', ctypes.c_int)]\n\n class kstat_named_t(ctypes.Structure):\n _fields_ = [('name', ctypes.c_char * KSTAT_STRLEN),\n ('data_type', ctypes.c_char),\n ('value', anon_union)]\n\n # Function signatures\n kstat.kstat_open.restype = ctypes.c_void_p\n kstat.kstat_lookup.restype = ctypes.c_void_p\n kstat.kstat_lookup.argtypes = [ctypes.c_void_p,\n ctypes.c_char_p,\n ctypes.c_int,\n ctypes.c_char_p]\n kstat.kstat_read.restype = ctypes.c_int\n kstat.kstat_read.argtypes = [ctypes.c_void_p,\n ctypes.c_void_p,\n ctypes.c_void_p]\n kstat.kstat_data_lookup.restype = ctypes.POINTER(kstat_named_t)\n kstat.kstat_data_lookup.argtypes = [ctypes.c_void_p,\n ctypes.c_char_p]\n\n # Now, let's do something useful.\n\n # Initialise kstat control structure.\n kc = kstat.kstat_open()\n if not kc:\n return None\n\n # We're looking for unix:0:system_misc:boot_time.\n ksp = kstat.kstat_lookup(kc, 'unix', 0, 'system_misc')\n if ksp and kstat.kstat_read(kc, ksp, None) != -1:\n data = kstat.kstat_data_lookup(ksp, 'boot_time')\n if data:\n __boottime = data.contents.value.time\n\n # Clean-up.\n kstat.kstat_close(kc)\n\n if __boottime is not None:\n return time.time() - __boottime\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _uptime_syllable():\n global __boottime\n try:\n __boottime = os.stat('/dev/pty/mst/pty0').st_mtime\n return time.time() - __boottime\n except (NameError, OSError):\n return None", "response": "Returns uptime in seconds on Syllable."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning uptime in seconds on Windows.", "response": "def _uptime_windows():\n \"\"\"\n Returns uptime in seconds or None, on Windows. Warning: may return\n incorrect answers after 49.7 days on versions older than Vista.\n \"\"\"\n if hasattr(ctypes, 'windll') and hasattr(ctypes.windll, 'kernel32'):\n lib = ctypes.windll.kernel32\n else:\n try:\n # Windows CE uses the cdecl calling convention.\n lib = ctypes.CDLL('coredll.lib')\n except (AttributeError, OSError):\n return None\n\n if hasattr(lib, 'GetTickCount64'):\n # Vista/Server 2008 or later.\n lib.GetTickCount64.restype = ctypes.c_uint64\n return lib.GetTickCount64() / 1000.\n if hasattr(lib, 'GetTickCount'):\n # WinCE and Win2k or later; gives wrong answers after 49.7 days.\n lib.GetTickCount.restype = ctypes.c_uint32\n return lib.GetTickCount() / 1000.\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning uptime in seconds if even remotely possible or None if not.", "response": "def uptime():\n \"\"\"Returns uptime in seconds if even remotely possible, or None if not.\"\"\"\n if __boottime is not None:\n return time.time() - __boottime\n\n return {'amiga': _uptime_amiga,\n 'aros12': _uptime_amiga,\n 'beos5': _uptime_beos,\n 'cygwin': _uptime_linux,\n 'darwin': _uptime_osx,\n 'haiku1': _uptime_beos,\n 'linux': _uptime_linux,\n 'linux-armv71': _uptime_linux,\n 'linux2': _uptime_linux,\n 'mac': _uptime_mac,\n 'minix3': _uptime_minix,\n 'riscos': _uptime_riscos,\n 'sunos5': _uptime_solaris,\n 'syllable': _uptime_syllable,\n 'win32': _uptime_windows,\n 'wince': _uptime_windows}.get(sys.platform, _uptime_bsd)() or \\\n _uptime_bsd() or _uptime_plan9() or _uptime_linux() or \\\n _uptime_windows() or _uptime_solaris() or _uptime_beos() or \\\n _uptime_amiga() or _uptime_riscos() or _uptime_posix() or \\\n _uptime_syllable() or _uptime_mac() or _uptime_osx()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns boot time if remotely possible or None if not.", "response": "def boottime():\n \"\"\"Returns boot time if remotely possible, or None if not.\"\"\"\n global __boottime\n\n if __boottime is None:\n up = uptime()\n if up is None:\n return None\n if __boottime is None:\n _boottime_linux()\n\n if datetime is None:\n raise RuntimeError('datetime module required.')\n\n return datetime.fromtimestamp(__boottime or time.time() - up)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _initfile(path, data=\"dict\"):\n data = {} if data.lower() == \"dict\" else []\n # The file will need to be created if it doesn't exist\n if not os.path.exists(path): # The file doesn't exist\n # Raise exception if the directory that should contain the file doesn't\n # exist\n dirname = os.path.dirname(path)\n if dirname and not os.path.exists(dirname):\n raise IOError(\n (\"Could not initialize empty JSON file in non-existant \"\n \"directory '{}'\").format(os.path.dirname(path))\n )\n # Write an empty file there\n with open(path, \"w\") as f:\n json.dump(data, f)\n return True\n elif os.path.getsize(path) == 0: # The file is empty\n with open(path, \"w\") as f:\n json.dump(data, f)\n else: # The file exists and contains content\n return False", "response": "Initialize an empty JSON file."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nover-writes the file with new data.", "response": "def data(self, data):\n \"\"\"Overwrite the file with new data. You probably shouldn't do\n this yourself, it's easy to screw up your whole file with this.\"\"\"\n if self.is_caching:\n self.cache = data\n else:\n fcontents = self.file_contents\n with open(self.path, \"w\") as f:\n try:\n # Write the file. Keep user settings about indentation, etc\n indent = self.indent if self.pretty else None\n json.dump(data, f, sort_keys=self.sort_keys, indent=indent)\n except Exception as e:\n # Rollback to prevent data loss\n f.seek(0)\n f.truncate()\n f.write(fcontents)\n # And re-raise the exception\n raise e\n self._updateType()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _updateType(self):\n data = self._data()\n # Change type if needed\n if isinstance(data, dict) and isinstance(self, ListFile):\n self.__class__ = DictFile\n elif isinstance(data, list) and isinstance(self, DictFile):\n self.__class__ = ListFile", "response": "Make sure that the class behaves like the data structure that it contains a dict and that we don t get a DictFile."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninitializes a new file that starts out with some data. Pass data as a list dict or JSON string.", "response": "def with_data(path, data):\n \"\"\"Initialize a new file that starts out with some data. Pass data\n as a list, dict, or JSON string.\n \"\"\"\n # De-jsonize data if necessary\n if isinstance(data, str):\n data = json.loads(data)\n\n # Make sure this is really a new file\n if os.path.exists(path):\n raise ValueError(\"File exists, not overwriting data. Set the \"\n \"'data' attribute on a normally-initialized \"\n \"'livejson.File' instance if you really \"\n \"want to do this.\")\n else:\n f = File(path)\n f.data = data\n return f"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef is_configured(self, project, **kwargs):\n params = self.get_option\n return bool(params('server_host', project) and params('server_port', project))", "response": "Check if the plugin is configured."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a dict representation of the object.", "response": "def as_dict(self):\n \"\"\"\n ping statistics.\n\n Returns:\n |dict|:\n\n Examples:\n >>> import pingparsing\n >>> parser = pingparsing.PingParsing()\n >>> parser.parse(ping_result)\n >>> parser.as_dict()\n {\n \"destination\": \"google.com\",\n \"packet_transmit\": 60,\n \"packet_receive\": 60,\n \"packet_loss_rate\": 0.0,\n \"packet_loss_count\": 0,\n \"rtt_min\": 61.425,\n \"rtt_avg\": 99.731,\n \"rtt_max\": 212.597,\n \"rtt_mdev\": 27.566,\n \"packet_duplicate_rate\": 0.0,\n \"packet_duplicate_count\": 0\n }\n \"\"\"\n\n return {\n \"destination\": self.destination,\n \"packet_transmit\": self.packet_transmit,\n \"packet_receive\": self.packet_receive,\n \"packet_loss_count\": self.packet_loss_count,\n \"packet_loss_rate\": self.packet_loss_rate,\n \"rtt_min\": self.rtt_min,\n \"rtt_avg\": self.rtt_avg,\n \"rtt_max\": self.rtt_max,\n \"rtt_mdev\": self.rtt_mdev,\n \"packet_duplicate_count\": self.packet_duplicate_count,\n \"packet_duplicate_rate\": self.packet_duplicate_rate,\n }"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a tuple containing the current ping statistics.", "response": "def as_tuple(self):\n \"\"\"\n ping statistics.\n\n Returns:\n |namedtuple|:\n\n Examples:\n >>> import pingparsing\n >>> parser = pingparsing.PingParsing()\n >>> parser.parse(ping_result)\n >>> parser.as_tuple()\n PingResult(destination='google.com', packet_transmit=60, packet_receive=60, packet_loss_rate=0.0, packet_loss_count=0, rtt_min=61.425, rtt_avg=99.731, rtt_max=212.597, rtt_mdev=27.566, packet_duplicate_rate=0.0, packet_duplicate_count=0)\n \"\"\"\n\n from collections import namedtuple\n\n ping_result = self.as_dict()\n\n return namedtuple(\"PingStatsTuple\", ping_result.keys())(**ping_result)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsends ICMP packets. :return: ``ping`` command execution result. :rtype: :py:class:`.PingResult` :raises ValueError: If parameters not valid.", "response": "def ping(self):\n \"\"\"\n Sending ICMP packets.\n\n :return: ``ping`` command execution result.\n :rtype: :py:class:`.PingResult`\n :raises ValueError: If parameters not valid.\n \"\"\"\n\n self.__validate_ping_param()\n\n ping_proc = subprocrunner.SubprocessRunner(self.__get_ping_command())\n ping_proc.run()\n\n return PingResult(ping_proc.stdout, ping_proc.stderr, ping_proc.returncode)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse(self, ping_message):\n\n try:\n # accept PingResult instance as an input\n if typepy.is_not_null_string(ping_message.stdout):\n ping_message = ping_message.stdout\n except AttributeError:\n pass\n\n logger.debug(\"parsing ping result: {}\".format(ping_message))\n\n self.__parser = NullPingParser()\n\n if typepy.is_null_string(ping_message):\n logger.debug(\"ping_message is empty\")\n self.__stats = PingStats()\n\n return self.__stats\n\n ping_lines = _to_unicode(ping_message).splitlines()\n parser_class_list = (\n LinuxPingParser,\n WindowsPingParser,\n MacOsPingParser,\n AlpineLinuxPingParser,\n )\n\n for parser_class in parser_class_list:\n self.__parser = parser_class()\n try:\n self.__stats = self.__parser.parse(ping_lines)\n return self.__stats\n except ParseError as e:\n if e.reason != ParseErrorReason.HEADER_NOT_FOUND:\n raise e\n except pp.ParseException:\n pass\n\n self.__parser = NullPingParser()\n\n return self.__stats", "response": "Parse the ping command output."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef send_confirmation(self):\n confirmation = EmailConfirmation.objects.create(email=self)\n confirmation.send()", "response": "Send a verification email for the email address."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send_duplicate_notification(self):\n email_utils.send_email(\n from_email=settings.DEFAULT_FROM_EMAIL,\n recipient_list=[self.email],\n subject=_(\"Registration Attempt\"),\n template_name=\"rest_email_auth/emails/duplicate-email\",\n )\n\n logger.info(\"Sent duplicate email notification to: %s\", self.email)", "response": "Send a duplicate notification about a duplicate signup."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_primary(self):\n query = EmailAddress.objects.filter(is_primary=True, user=self.user)\n query = query.exclude(pk=self.pk)\n\n # The transaction is atomic so there is never a gap where a user\n # has no primary email address.\n with transaction.atomic():\n query.update(is_primary=False)\n\n self.is_primary = True\n self.save()\n\n logger.info(\n \"Set %s as the primary email address for %s.\",\n self.email,\n self.user,\n )", "response": "Set this email address as the user s primary email address."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmarks the instance s email as verified.", "response": "def confirm(self):\n \"\"\"\n Mark the instance's email as verified.\n \"\"\"\n self.email.is_verified = True\n self.email.save()\n\n signals.email_verified.send(email=self.email, sender=self.__class__)\n\n logger.info(\"Verified email address: %s\", self.email.email)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_expired(self):\n expiration_time = self.created_at + datetime.timedelta(days=1)\n\n return timezone.now() > expiration_time", "response": "Determines if the confirmation has expired."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsending a verification email to the user.", "response": "def send(self):\n \"\"\"\n Send a verification email to the user.\n \"\"\"\n context = {\n \"verification_url\": app_settings.EMAIL_VERIFICATION_URL.format(\n key=self.key\n )\n }\n\n email_utils.send_email(\n context=context,\n from_email=settings.DEFAULT_FROM_EMAIL,\n recipient_list=[self.email.email],\n subject=_(\"Please Verify Your Email Address\"),\n template_name=\"rest_email_auth/emails/verify-email\",\n )\n\n logger.info(\n \"Sent confirmation email to %s for user #%d\",\n self.email.email,\n self.email.user.id,\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _create(cls, model_class, *args, **kwargs):\n manager = cls._get_manager(model_class)\n\n return manager.create_user(*args, **kwargs)", "response": "Create a new user instance of the specified type."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create(self, validated_data):\n email_query = models.EmailAddress.objects.filter(\n email=self.validated_data[\"email\"]\n )\n\n if email_query.exists():\n email = email_query.get()\n\n email.send_duplicate_notification()\n else:\n email = super(EmailSerializer, self).create(validated_data)\n email.send_confirmation()\n\n user = validated_data.get(\"user\")\n query = models.EmailAddress.objects.filter(\n is_primary=True, user=user\n )\n\n if not query.exists():\n email.set_primary()\n\n return email", "response": "Create a new email and send a confirmation to it."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update(self, instance, validated_data):\n is_primary = validated_data.pop(\"is_primary\", False)\n\n instance = super(EmailSerializer, self).update(\n instance, validated_data\n )\n\n if is_primary:\n instance.set_primary()\n\n return instance", "response": "Updates the instance with the data provided."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef validate_email(self, email):\n user, domain = email.rsplit(\"@\", 1)\n email = \"@\".join([user, domain.lower()])\n\n if self.instance and email and self.instance.email != email:\n raise serializers.ValidationError(\n _(\n \"Existing emails may not be edited. Create a new one \"\n \"instead.\"\n )\n )\n\n return email", "response": "Validate the provided email address."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef validate_is_primary(self, is_primary):\n # TODO: Setting 'is_primary' to 'False' should probably not be\n # allowed.\n if is_primary and not (self.instance and self.instance.is_verified):\n raise serializers.ValidationError(\n _(\n \"Unverified email addresses may not be used as the \"\n \"primary address.\"\n )\n )\n\n return is_primary", "response": "Validate the provided is_primary parameter."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef validate(self, data):\n user = self._confirmation.email.user\n\n if (\n app_settings.EMAIL_VERIFICATION_PASSWORD_REQUIRED\n and not user.check_password(data[\"password\"])\n ):\n raise serializers.ValidationError(\n _(\"The provided password is invalid.\")\n )\n\n # Add email to returned data\n data[\"email\"] = self._confirmation.email.email\n\n return data", "response": "Validate the provided data."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_key(self, key):\n try:\n confirmation = models.EmailConfirmation.objects.select_related(\n \"email__user\"\n ).get(key=key)\n except models.EmailConfirmation.DoesNotExist:\n raise serializers.ValidationError(\n _(\"The provided verification key is invalid.\")\n )\n\n if confirmation.is_expired:\n raise serializers.ValidationError(\n _(\"That verification code has expired.\")\n )\n\n # Cache confirmation instance\n self._confirmation = confirmation\n\n return key", "response": "Validate the provided confirmation key."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef save(self):\n try:\n email = models.EmailAddress.objects.get(\n email=self.validated_data[\"email\"], is_verified=True\n )\n except models.EmailAddress.DoesNotExist:\n return None\n\n token = models.PasswordResetToken.objects.create(email=email)\n token.send()\n\n return token", "response": "Send out a password reset token if the provided data is valid and returns the associated password reset token."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef save(self):\n token = models.PasswordResetToken.objects.get(\n key=self.validated_data[\"key\"]\n )\n\n token.email.user.set_password(self.validated_data[\"password\"])\n token.email.user.save()\n\n logger.info(\"Reset password for %s\", token.email.user)\n\n token.delete()", "response": "Save the user s password if the provided information is valid."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nvalidate the provided reset key.", "response": "def validate_key(self, key):\n \"\"\"\n Validate the provided reset key.\n\n Returns:\n The validated key.\n\n Raises:\n serializers.ValidationError:\n If the provided key does not exist.\n \"\"\"\n if not models.PasswordResetToken.valid_tokens.filter(key=key).exists():\n raise serializers.ValidationError(\n _(\"The provided reset token does not exist, or is expired.\")\n )\n\n return key"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a new user from the data passed to the serializer.", "response": "def create(self, validated_data):\n \"\"\"\n Create a new user from the data passed to the serializer.\n\n If the provided email has not been verified yet, the user is\n created and a verification email is sent to the address.\n Otherwise we send a notification to the email address that\n someone attempted to register with an email that's already been\n verified.\n\n Args:\n validated_data (dict):\n The data passed to the serializer after it has been\n validated.\n\n Returns:\n A new user created from the provided data.\n \"\"\"\n email = validated_data.pop(\"email\")\n password = validated_data.pop(\"password\")\n\n # We don't save the user instance yet in case the provided email\n # address already exists.\n user = get_user_model()(**validated_data)\n user.set_password(password)\n\n # We set an ephemeral email property so that it is included in\n # the data returned by the serializer.\n user.email = email\n\n email_query = models.EmailAddress.objects.filter(email=email)\n\n if email_query.exists():\n existing_email = email_query.get()\n existing_email.send_duplicate_notification()\n else:\n user.save()\n\n email_instance = models.EmailAddress.objects.create(\n email=email, user=user\n )\n email_instance.send_confirmation()\n\n signals.user_registered.send(sender=self.__class__, user=user)\n\n return user"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef validate_email(self, email):\n user, domain = email.rsplit(\"@\", 1)\n\n return \"@\".join([user, domain.lower()])", "response": "Validate the provided email address."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nre-sending a verification email to the provided address.", "response": "def save(self):\n \"\"\"\n Resend a verification email to the provided address.\n\n If the provided email is already verified no action is taken.\n \"\"\"\n try:\n email = models.EmailAddress.objects.get(\n email=self.validated_data[\"email\"], is_verified=False\n )\n\n logger.debug(\n \"Resending verification email to %s\",\n self.validated_data[\"email\"],\n )\n\n email.send_confirmation()\n except models.EmailAddress.DoesNotExist:\n logger.debug(\n \"Not resending verification email to %s because the address \"\n \"doesn't exist in the database.\",\n self.validated_data[\"email\"],\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a new email address.", "response": "def create(self, *args, **kwargs):\n \"\"\"\n Create a new email address.\n \"\"\"\n is_primary = kwargs.pop(\"is_primary\", False)\n\n with transaction.atomic():\n email = super(EmailAddressManager, self).create(*args, **kwargs)\n\n if is_primary:\n email.set_primary()\n\n return email"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_queryset(self):\n oldest = timezone.now() - app_settings.PASSWORD_RESET_EXPIRATION\n queryset = super(ValidPasswordResetTokenManager, self).get_queryset()\n\n return queryset.filter(created_at__gt=oldest)", "response": "Return all expired password reset tokens."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhandle execution of the command.", "response": "def handle(self, *args, **kwargs):\n \"\"\"\n Handle execution of the command.\n \"\"\"\n cutoff = timezone.now()\n cutoff -= app_settings.CONFIRMATION_EXPIRATION\n cutoff -= app_settings.CONFIRMATION_SAVE_PERIOD\n\n queryset = models.EmailConfirmation.objects.filter(\n created_at__lte=cutoff\n )\n\n count = queryset.count()\n\n queryset.delete()\n\n if count:\n self.stdout.write(\n self.style.SUCCESS(\n \"Removed {count} old email confirmation(s)\".format(\n count=count\n )\n )\n )\n else:\n self.stdout.write(\"No email confirmations to remove.\")"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_user(self, user_id):\n try:\n return get_user_model().objects.get(id=user_id)\n except get_user_model().DoesNotExist:\n return None", "response": "Get a user by their ID."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nattempts to authenticate a set of credentials.", "response": "def authenticate(self, request, email=None, password=None, username=None):\n \"\"\"\n Attempt to authenticate a set of credentials.\n\n Args:\n request:\n The request associated with the authentication attempt.\n email:\n The user's email address.\n password:\n The user's password.\n username:\n An alias for the ``email`` field. This is provided for\n compatability with Django's built in authentication\n views.\n\n Returns:\n The user associated with the provided credentials if they\n are valid. Returns ``None`` otherwise.\n \"\"\"\n email = email or username\n\n try:\n email_instance = models.EmailAddress.objects.get(\n is_verified=True, email=email\n )\n except models.EmailAddress.DoesNotExist:\n return None\n\n user = email_instance.user\n\n if user.check_password(password):\n return user\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nauthenticates for the given username and password.", "response": "def authenticate(username, password, service='login', encoding='utf-8',\n resetcred=True):\n \"\"\"Returns True if the given username and password authenticate for the\n given service. Returns False otherwise.\n\n ``username``: the username to authenticate\n\n ``password``: the password in plain text\n\n ``service``: the PAM service to authenticate against.\n Defaults to 'login'\n\n The above parameters can be strings or bytes. If they are strings,\n they will be encoded using the encoding given by:\n\n ``encoding``: the encoding to use for the above parameters if they\n are given as strings. Defaults to 'utf-8'\n\n ``resetcred``: Use the pam_setcred() function to\n reinitialize the credentials.\n Defaults to 'True'.\n \"\"\"\n\n if sys.version_info >= (3,):\n if isinstance(username, str):\n username = username.encode(encoding)\n if isinstance(password, str):\n password = password.encode(encoding)\n if isinstance(service, str):\n service = service.encode(encoding)\n\n @conv_func\n def my_conv(n_messages, messages, p_response, app_data):\n \"\"\"Simple conversation function that responds to any\n prompt where the echo is off with the supplied password\"\"\"\n # Create an array of n_messages response objects\n addr = calloc(n_messages, sizeof(PamResponse))\n p_response[0] = cast(addr, POINTER(PamResponse))\n for i in range(n_messages):\n if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:\n pw_copy = strdup(password)\n p_response.contents[i].resp = cast(pw_copy, c_char_p)\n p_response.contents[i].resp_retcode = 0\n return 0\n\n handle = PamHandle()\n conv = PamConv(my_conv, 0)\n retval = pam_start(service, username, byref(conv), byref(handle))\n\n if retval != 0:\n # TODO: This is not an authentication error, something\n # has gone wrong starting up PAM\n return False\n\n retval = pam_authenticate(handle, 0)\n auth_success = (retval == 0)\n\n # Re-initialize credentials (for Kerberos users, etc)\n # Don't check return code of pam_setcred(), it shouldn't matter\n # if this fails\n if auth_success and resetcred:\n retval = pam_setcred(handle, PAM_REINITIALIZE_CRED)\n\n pam_end(handle, retval)\n\n return auth_success"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsaves the provided data using the class serializer.", "response": "def post(self, request):\n \"\"\"\n Save the provided data using the class' serializer.\n\n Args:\n request:\n The request being made.\n\n Returns:\n An ``APIResponse`` instance. If the request was successful\n the response will have a 200 status code and contain the\n serializer's data. Otherwise a 400 status code and the\n request's errors will be returned.\n \"\"\"\n serializer = self.get_serializer(data=request.data)\n\n if serializer.is_valid():\n serializer.save()\n\n return Response(serializer.data)\n\n return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns an HTML tree block describing the given object.", "response": "def get_repr(self, obj, referent=None):\n \"\"\"Return an HTML tree block describing the given object.\"\"\"\n objtype = type(obj)\n typename = str(objtype.__module__) + \".\" + objtype.__name__\n prettytype = typename.replace(\"__builtin__.\", \"\")\n\n name = getattr(obj, \"__name__\", \"\")\n if name:\n prettytype = \"%s %r\" % (prettytype, name)\n\n key = \"\"\n if referent:\n key = self.get_refkey(obj, referent)\n url = reverse('dowser_trace_object', args=(\n typename,\n id(obj)\n ))\n return ('%s '\n '%s%s
'\n '%s'\n % (url, id(obj), prettytype, key, get_repr(obj, 100))\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_refkey(self, obj, referent):\n if isinstance(obj, dict):\n for k, v in obj.items():\n if v is referent:\n return \" (via its %r key)\" % k\n\n for k in dir(obj) + ['__dict__']:\n if getattr(obj, k, None) is referent:\n return \" (via its %r attribute)\" % k\n return \"\"", "response": "Return the dict key or attribute name of obj which refers to\n referent."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nwalks the object tree ignoring duplicates and circular refs.", "response": "def walk(self, maxresults=100, maxdepth=None):\n \"\"\"Walk the object tree, ignoring duplicates and circular refs.\"\"\"\n log.debug(\"step\")\n self.seen = {}\n self.ignore(self, self.__dict__, self.obj, self.seen, self._ignore)\n\n # Ignore the calling frame, its builtins, globals and locals\n self.ignore_caller()\n self.maxdepth = maxdepth\n count = 0\n log.debug(\"will iterate results\")\n for result in self._gen(self.obj):\n log.debug(\"will yeld\")\n yield result\n count += 1\n if maxresults and count >= maxresults:\n yield 0, 0, \"==== Max results reached ====\"\n return"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef print_tree(self, maxresults=100, maxdepth=None):\n self.ignore_caller()\n for depth, refid, rep in self.walk(maxresults, maxdepth):\n print((\"%9d\" % refid), (\" \" * depth * 2), rep)", "response": "Walk the object tree and print each branch."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwalks the object tree and print each branch.", "response": "def print_tree(self, maxresults=100, maxdepth=None):\n \"\"\"Walk the object tree, pretty-printing each branch.\"\"\"\n self.ignore_caller()\n for trail in self.walk(maxresults, maxdepth):\n print(trail)\n if self.stops:\n print(\"%s paths stopped because max depth reached\" % self.stops)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_finders():\n if hasattr(settings, 'MEDIA_FIXTURES_FILES_FINDERS'):\n finders = settings.MEDIA_FIXTURES_FILES_FINDERS\n else:\n finders = (\n 'django_media_fixtures.finders.AppDirectoriesFinder',\n )\n\n for finder_path in finders:\n yield get_finder(finder_path)", "response": "Yields a generator of all media fixtures finders."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the media fixtures files finder class described by import_path.", "response": "def get_finder(import_path):\n \"\"\"\n Imports the media fixtures files finder class described by import_path, where\n import_path is the full Python path to the class.\n \"\"\"\n Finder = import_string(import_path)\n if not issubclass(Finder, BaseFinder):\n raise ImproperlyConfigured('Finder \"%s\" is not a subclass of \"%s\"' %\n (Finder, BaseFinder))\n return Finder()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef find(self, path, all=False):\n matches = []\n for prefix, root in self.locations:\n if root not in searched_locations:\n searched_locations.append(root)\n matched_path = self.find_location(root, path, prefix)\n if matched_path:\n if not all:\n return matched_path\n matches.append(matched_path)\n return matches", "response": "Find the files in the extra locations that are located at the given path."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfinds a requested media file in a location returning the absolute path.", "response": "def find_location(self, root, path, prefix=None):\n \"\"\"\n Finds a requested media file in a location, returning the found\n absolute path (or ``None`` if no match).\n \"\"\"\n if prefix:\n prefix = '%s%s' % (prefix, os.sep)\n if not path.startswith(prefix):\n return None\n path = path[len(prefix):]\n path = safe_join(root, path)\n if os.path.exists(path):\n return path"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting all files in all locations.", "response": "def list(self, ignore_patterns):\n \"\"\"\n List all files in all locations.\n \"\"\"\n for prefix, root in self.locations:\n storage = self.storages[root]\n for path in utils.get_files(storage, ignore_patterns):\n yield path, storage"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list(self, ignore_patterns):\n for storage in six.itervalues(self.storages):\n if storage.exists(''): # check if storage location exists\n for path in utils.get_files(storage, ignore_patterns):\n yield path, storage", "response": "List all files in all app storages."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind the files in the app directories and return the list of matches.", "response": "def find(self, path, all=False):\n \"\"\"\n Looks for files in the app directories.\n \"\"\"\n matches = []\n for app in self.apps:\n app_location = self.storages[app].location\n if app_location not in searched_locations:\n searched_locations.append(app_location)\n match = self.find_in_app(app, path)\n if match:\n if not all:\n return match\n matches.append(match)\n return matches"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_in_app(self, app, path):\n storage = self.storages.get(app, None)\n if storage:\n # only try to find a file if the source dir actually exists\n if storage.exists(path):\n matched_path = storage.path(path)\n if matched_path:\n return matched_path", "response": "Find a requested media file in an app s media fixtures locations."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_options(self, **options):\n self.interactive = options['interactive']\n self.verbosity = options['verbosity']\n self.symlink = options['link']\n self.clear = options['clear']\n self.dry_run = options['dry_run']\n ignore_patterns = options['ignore_patterns']\n if options['use_default_ignore_patterns']:\n ignore_patterns += ['CVS', '.*', '*~']\n self.ignore_patterns = list(set(ignore_patterns))\n self.post_process = options['post_process']", "response": "Set instance variables based on an options dict"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncollect the media files and store them in a dictionary.", "response": "def collect(self):\n \"\"\"\n Perform the bulk of the work of collectmedia.\n\n Split off from handle() to facilitate testing.\n \"\"\"\n if self.symlink and not self.local:\n raise CommandError(\"Can't symlink to a remote destination.\")\n\n if self.clear:\n self.clear_dir('')\n\n if self.symlink:\n handler = self.link_file\n else:\n handler = self.copy_file\n\n found_files = OrderedDict()\n for finder in get_finders():\n for path, storage in finder.list(self.ignore_patterns):\n # Prefix the relative path if the source storage contains it\n if getattr(storage, 'prefix', None):\n prefixed_path = os.path.join(storage.prefix, path)\n else:\n prefixed_path = path\n\n if prefixed_path not in found_files:\n found_files[prefixed_path] = (storage, path)\n handler(path, prefixed_path, storage)\n\n # Here we check if the storage backend has a post_process\n # method and pass it the list of modified files.\n if self.post_process and hasattr(self.storage, 'post_process'):\n processor = self.storage.post_process(found_files,\n dry_run=self.dry_run)\n for original_path, processed_path, processed in processor:\n if isinstance(processed, Exception):\n self.stderr.write(\"Post-processing '%s' failed!\" % original_path)\n # Add a blank line before the traceback, otherwise it's\n # too easy to miss the relevant part of the error message.\n self.stderr.write(\"\")\n raise processed\n if processed:\n self.log(\"Post-processed '%s' as '%s'\" %\n (original_path, processed_path), level=1)\n self.post_processed_files.append(original_path)\n else:\n self.log(\"Skipped post-processing '%s'\" % original_path)\n\n return {\n 'modified': self.copied_files + self.symlinked_files,\n 'unmodified': self.unmodified_files,\n 'post_processed': self.post_processed_files,\n }"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes the given relative path using the destination storage backend.", "response": "def clear_dir(self, path):\n \"\"\"\n Deletes the given relative path using the destination storage backend.\n \"\"\"\n dirs, files = self.storage.listdir(path)\n for f in files:\n fpath = os.path.join(path, f)\n if self.dry_run:\n self.log(\"Pretending to delete '%s'\" %\n smart_text(fpath), level=1)\n else:\n self.log(\"Deleting '%s'\" % smart_text(fpath), level=1)\n self.storage.delete(fpath)\n for d in dirs:\n self.clear_dir(os.path.join(path, d))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_file(self, path, prefixed_path, source_storage):\n if self.storage.exists(prefixed_path):\n try:\n # When was the target file modified last time?\n target_last_modified = \\\n self.storage.modified_time(prefixed_path)\n except (OSError, NotImplementedError, AttributeError):\n # The storage doesn't support ``modified_time`` or failed\n pass\n else:\n try:\n # When was the source file modified last time?\n source_last_modified = source_storage.modified_time(path)\n except (OSError, NotImplementedError, AttributeError):\n pass\n else:\n # The full path of the target file\n if self.local:\n full_path = self.storage.path(prefixed_path)\n else:\n full_path = None\n # Skip the file if the source file is younger\n # Avoid sub-second precision (see #14665, #19540)\n if (target_last_modified.replace(microsecond=0)\n >= source_last_modified.replace(microsecond=0)):\n if not ((self.symlink and full_path\n and not os.path.islink(full_path)) or\n (not self.symlink and full_path\n and os.path.islink(full_path))):\n if prefixed_path not in self.unmodified_files:\n self.unmodified_files.append(prefixed_path)\n self.log(\"Skipping '%s' (not modified)\" % path)\n return False\n # Then delete the existing file if really needed\n if self.dry_run:\n self.log(\"Pretending to delete '%s'\" % path)\n else:\n self.log(\"Deleting '%s'\" % path)\n self.storage.delete(prefixed_path)\n return True", "response": "Checks if the target file should be deleted if it exists and if it should be deleted if it should be deleted if it already exists and if it should be deleted if it should be deleted if it should be deleted if it already exists and if it should be deleted if it should be deleted if it should be deleted if it already exists."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef link_file(self, path, prefixed_path, source_storage):\n # Skip this file if it was already copied earlier\n if prefixed_path in self.symlinked_files:\n return self.log(\"Skipping '%s' (already linked earlier)\" % path)\n # Delete the target file if needed or break\n if not self.delete_file(path, prefixed_path, source_storage):\n return\n # The full path of the source file\n source_path = source_storage.path(path)\n # Finally link the file\n if self.dry_run:\n self.log(\"Pretending to link '%s'\" % source_path, level=1)\n else:\n self.log(\"Linking '%s'\" % source_path, level=1)\n full_path = self.storage.path(prefixed_path)\n try:\n os.makedirs(os.path.dirname(full_path))\n except OSError:\n pass\n try:\n if os.path.lexists(full_path):\n os.unlink(full_path)\n os.symlink(source_path, full_path)\n except AttributeError:\n import platform\n raise CommandError(\"Symlinking is not supported by Python %s.\" %\n platform.python_version())\n except NotImplementedError:\n import platform\n raise CommandError(\"Symlinking is not supported in this \"\n \"platform (%s).\" % platform.platform())\n except OSError as e:\n raise CommandError(e)\n if prefixed_path not in self.symlinked_files:\n self.symlinked_files.append(prefixed_path)", "response": "Link the file at the given path to the source file at the given prefixed_path."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncopies a file from source_storage to the target storage.", "response": "def copy_file(self, path, prefixed_path, source_storage):\n \"\"\"\n Attempt to copy ``path`` with storage\n \"\"\"\n # Skip this file if it was already copied earlier\n if prefixed_path in self.copied_files:\n return self.log(\"Skipping '%s' (already copied earlier)\" % path)\n # Delete the target file if needed or break\n if not self.delete_file(path, prefixed_path, source_storage):\n return\n # The full path of the source file\n source_path = source_storage.path(path)\n # Finally start copying\n if self.dry_run:\n self.log(\"Pretending to copy '%s'\" % source_path, level=1)\n else:\n self.log(\"Copying '%s'\" % source_path, level=1)\n with source_storage.open(path) as source_file:\n self.storage.save(prefixed_path, source_file)\n self.copied_files.append(prefixed_path)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef reorderChild(self, parent, newitem):\n source = self.getItem(parent).childItems\n target = newitem.childItems\n\n i = 0\n while i < len(source):\n\n if source[i] == target[i]:\n i += 1\n continue\n else:\n i0 = i\n j0 = source.index(target[i0])\n j = j0 + 1\n while j < len(source):\n if source[j] == target[j - j0 + i0]:\n j += 1\n continue\n else:\n break\n self.moveRows(parent, i0, j0, j - j0)\n i += j - j0", "response": "Reorder a list to match target by moving a sequence at a time."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef moveRows(self, parent, index_to, index_from, length):\n source = self.getItem(parent).childItems\n\n self.beginMoveRows(\n parent, index_from, index_from + length - 1, parent, index_to\n )\n\n sublist = [source.pop(index_from) for _ in range(length)]\n\n for _ in range(length):\n source.insert(index_to, sublist.pop())\n\n self.endMoveRows()", "response": "Move a sub sequence in a list\n parent to index_to."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cur_space(self, name=None):\n if name is None:\n return self._impl.model.currentspace.interface\n else:\n self._impl.model.currentspace = self._impl.spaces[name]\n return self.cur_space()", "response": "Set the current space to the space named name and return it."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef new_space(self, name=None, bases=None, formula=None, refs=None):\n space = self._impl.model.currentspace = self._impl.new_space(\n name=name, bases=get_impls(bases), formula=formula, refs=refs\n )\n\n return space.interface", "response": "Create a new child space."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef import_module(self, module=None, recursive=False, **params):\n if module is None:\n if \"module_\" in params:\n warnings.warn(\n \"Parameter 'module_' is deprecated. Use 'module' instead.\")\n module = params.pop(\"module_\")\n else:\n raise ValueError(\"no module specified\")\n\n if \"bases\" in params:\n params[\"bases\"] = get_impls(params[\"bases\"])\n\n space = (\n self._impl.model.currentspace\n ) = self._impl.new_space_from_module(\n module, recursive=recursive, **params\n )\n return get_interfaces(space)", "response": "Create a child space from a module object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef new_space_from_module(self, module, recursive=False, **params):\n if \"bases\" in params:\n params[\"bases\"] = get_impls(params[\"bases\"])\n\n space = (\n self._impl.model.currentspace\n ) = self._impl.new_space_from_module(\n module, recursive=recursive, **params\n )\n return get_interfaces(space)", "response": "Create a new child space from a module object."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new child space from an Excel file.", "response": "def new_space_from_excel(\n self,\n book,\n range_,\n sheet=None,\n name=None,\n names_row=None,\n param_cols=None,\n space_param_order=None,\n cells_param_order=None,\n transpose=False,\n names_col=None,\n param_rows=None,\n ):\n \"\"\"Create a child space from an Excel range.\n\n To use this method, ``openpyxl`` package must be installed.\n\n Args:\n book (str): Path to an Excel file.\n range_ (str): Range expression, such as \"A1\", \"$G4:$K10\",\n or named range \"NamedRange1\".\n sheet (str): Sheet name (case ignored).\n name (str, optional): Name of the space. Defaults to ``SpaceN``,\n where ``N`` is a number determined automatically.\n names_row (optional): an index number indicating\n what row contains the names of cells and parameters.\n Defaults to the top row (0).\n param_cols (optional): a sequence of index numbers\n indicating parameter columns.\n Defaults to only the leftmost column ([0]).\n names_col (optional): an index number, starting from 0,\n indicating what column contains additional parameters.\n param_rows (optional): a sequence of index numbers, starting from\n 0, indicating rows of additional parameters, in case cells are\n defined in two dimensions.\n transpose (optional): Defaults to ``False``.\n If set to ``True``, \"row(s)\" and \"col(s)\" in the parameter\n names are interpreted inversely, i.e.\n all indexes passed to \"row(s)\" parameters are interpreted\n as column indexes,\n and all indexes passed to \"col(s)\" parameters as row indexes.\n space_param_order: a sequence to specify space parameters and\n their orders. The elements of the sequence denote the indexes\n of ``param_cols`` elements, and optionally the index of\n ``param_rows`` elements shifted by the length of\n ``param_cols``. The elements of this parameter and\n ``cell_param_order`` must not overlap.\n cell_param_order (optional): a sequence to reorder the parameters.\n The elements of the sequence denote the indexes of\n ``param_cols`` elements, and optionally the index of\n ``param_rows`` elements shifted by the length of\n ``param_cols``. The elements of this parameter and\n ``cell_space_order`` must not overlap.\n\n Returns:\n The new child space created from the Excel range.\n \"\"\"\n\n space = self._impl.new_space_from_excel(\n book,\n range_,\n sheet,\n name,\n names_row,\n param_cols,\n space_param_order,\n cells_param_order,\n transpose,\n names_col,\n param_rows,\n )\n\n return get_interfaces(space)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef restore_state(self, system):\n\n for space in self._spaces.values():\n space.restore_state(system)", "response": "Called after unpickling to restore some attributes manually."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef new_space(\n self,\n name=None,\n bases=None,\n formula=None,\n *,\n refs=None,\n source=None,\n is_derived=False,\n prefix=\"\"\n ):\n \"\"\"Create a new child space.\n\n Args:\n name (str): Name of the space. If omitted, the space is\n created automatically.\n bases: If specified, the new space becomes a derived space of\n the `base` space.\n formula: Function whose parameters used to set space parameters.\n refs: a mapping of refs to be added.\n arguments: ordered dict of space parameter names to their values.\n source: A source module from which cell definitions are read.\n prefix: Prefix to the autogenerated name when name is None.\n \"\"\"\n from modelx.core.space import StaticSpaceImpl\n\n if name is None:\n name = self.spacenamer.get_next(self.namespace, prefix)\n\n if name in self.namespace:\n raise ValueError(\"Name '%s' already exists.\" % name)\n\n if not prefix and not is_valid_name(name):\n raise ValueError(\"Invalid name '%s'.\" % name)\n\n space = self._new_space(\n name=name,\n formula=formula,\n refs=refs,\n source=source,\n is_derived=is_derived,\n )\n self._set_space(space)\n\n self.model.spacegraph.add_space(space)\n\n # Set up direct base spaces and mro\n if bases is not None:\n if isinstance(bases, StaticSpaceImpl):\n bases = [bases]\n\n space.add_bases(bases)\n\n return space", "response": "Create a new child space."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a node from arguments and return it", "response": "def get_node(obj, args, kwargs):\n \"\"\"Create a node from arguments and return it\"\"\"\n\n if args is None and kwargs is None:\n return (obj,)\n\n if kwargs is None:\n kwargs = {}\n return obj, _bind_args(obj, args, kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef node_get_args(node):\n obj = node[OBJ]\n key = node[KEY]\n boundargs = obj.formula.signature.bind(*key)\n boundargs.apply_defaults()\n return boundargs.arguments", "response": "Return an ordered mapping from params to args"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a modelx object from its full name.", "response": "def get_object(name: str):\n \"\"\"Get a modelx object from its full name.\"\"\"\n # TODO: Duplicate of system.get_object\n elms = name.split(\".\")\n parent = get_models()[elms.pop(0)]\n while len(elms) > 0:\n obj = elms.pop(0)\n parent = getattr(parent, obj)\n\n return parent"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_node(name: str, args: str):\n obj = get_object(name)\n args = ast.literal_eval(args)\n if not isinstance(args, tuple):\n args = (args,)\n\n return obj.node(*args)", "response": "Get node from object name and arg string\n\n Not Used. Left for future reference purpose."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cur_model(model=None):\n if model is None:\n if _system.currentmodel is not None:\n return _system.currentmodel.interface\n else:\n return None\n else:\n if isinstance(model, _Model):\n _system.currentmodel = model._impl\n else:\n _system.currentmodel = _system.models[model]\n\n return _system.currentmodel.interface", "response": "Get and or set the current model."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting and / or set the current space of the current model.", "response": "def cur_space(space=None):\n \"\"\"Get and/or set the current space of the current model.\n\n If ``name`` is given, the current space of the current model is\n set to ``name`` and return it.\n If ``name`` is not given, the current space of the current model\n is returned.\n \"\"\"\n if space is None:\n if _system.currentmodel is not None:\n if _system.currentmodel.currentspace is not None:\n return _system.currentmodel.currentspace.interface\n else:\n return None\n else:\n return None\n else:\n if isinstance(space, _Space):\n cur_model(space.model)\n _system.currentmodel.currentspace = space._impl\n else:\n _system.currentmodel.currentspace = _system.currentmodel.spaces[\n space\n ]\n\n return cur_space()"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhook to override default showwarning.", "response": "def custom_showwarning(\n message, category, filename=\"\", lineno=-1, file=None, line=None\n):\n \"\"\"Hook to override default showwarning.\n\n https://stackoverflow.com/questions/2187269/python-print-only-the-message-on-warnings\n \"\"\"\n\n if file is None:\n file = sys.stderr\n if file is None:\n # sys.stderr is None when run with pythonw.exe:\n # warnings get lost\n return\n text = \"%s: %s\\n\" % (category.__name__, message)\n try:\n file.write(text)\n except OSError:\n # the file (probably stderr) is invalid - this warning gets lost.\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef excepthook(self, except_type, exception, traceback):\n if except_type is DeepReferenceError:\n print(exception.msg)\n else:\n self.default_excepthook(except_type, exception, traceback)", "response": "Custom exception hook to replace sys. excepthook\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tracemessage(self, maxlen=6):\n result = \"\"\n for i, value in enumerate(self):\n result += \"{0}: {1}\\n\".format(i, get_node_repr(value))\n\n result = result.strip(\"\\n\")\n lines = result.split(\"\\n\")\n\n if maxlen and len(lines) > maxlen:\n i = int(maxlen / 2)\n lines = lines[:i] + [\"...\"] + lines[-(maxlen - i) :]\n result = \"\\n\".join(lines)\n\n return result", "response": "Returns a string representation of the log entry."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef setup_ipython(self):\n if self.is_ipysetup:\n return\n\n from ipykernel.kernelapp import IPKernelApp\n\n self.shell = IPKernelApp.instance().shell # None in PyCharm console\n\n if not self.shell and is_ipython():\n self.shell = get_ipython()\n\n if self.shell:\n shell_class = type(self.shell)\n shell_class.default_showtraceback = shell_class.showtraceback\n shell_class.showtraceback = custom_showtraceback\n self.is_ipysetup = True\n else:\n raise RuntimeError(\"IPython shell not found.\")", "response": "Monkey patch shell s error handler to include the custom showtraceback method of the IPython kernel."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef restore_ipython(self):\n if not self.is_ipysetup:\n return\n\n shell_class = type(self.shell)\n shell_class.showtraceback = shell_class.default_showtraceback\n del shell_class.default_showtraceback\n\n self.is_ipysetup = False", "response": "Restore default IPython showtraceback"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrestores Python settings to the original states", "response": "def restore_python(self):\n \"\"\"Restore Python settings to the original states\"\"\"\n orig = self.orig_settings\n sys.setrecursionlimit(orig[\"sys.recursionlimit\"])\n\n if \"sys.tracebacklimit\" in orig:\n sys.tracebacklimit = orig[\"sys.tracebacklimit\"]\n else:\n if hasattr(sys, \"tracebacklimit\"):\n del sys.tracebacklimit\n\n if \"showwarning\" in orig:\n warnings.showwarning = orig[\"showwarning\"]\n\n orig.clear()\n threading.stack_size()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_object(self, name):\n\n parts = name.split(\".\")\n\n model_name = parts.pop(0)\n return self.models[model_name].get_object(\".\".join(parts))", "response": "Retrieve an object by its absolute name."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_modeltree(model=None):\n if model is None:\n model = mx.cur_model()\n treemodel = ModelTreeModel(model._baseattrs)\n view = QTreeView()\n view.setModel(treemodel)\n view.setWindowTitle(\"Model %s\" % model.name)\n view.setAlternatingRowColors(True)\n return view", "response": "Alias to : func : get_tree."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef show_tree(model=None):\n if model is None:\n model = mx.cur_model()\n view = get_modeltree(model)\n app = QApplication.instance()\n if not app:\n raise RuntimeError(\"QApplication does not exist.\")\n view.show()\n app.exec_()", "response": "Display the model tree window."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget interfaces from their implementations.", "response": "def get_interfaces(impls):\n \"\"\"Get interfaces from their implementations.\"\"\"\n if impls is None:\n return None\n\n elif isinstance(impls, OrderMixin):\n result = OrderedDict()\n for name in impls.order:\n result[name] = impls[name].interface\n return result\n\n elif isinstance(impls, Mapping):\n return {name: impls[name].interface for name in impls}\n\n elif isinstance(impls, Sequence):\n return [impl.interface for impl in impls]\n\n else:\n return impls.interface"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_impls(interfaces):\n if interfaces is None:\n return None\n\n elif isinstance(interfaces, Mapping):\n return {name: interfaces[name]._impl for name in interfaces}\n\n elif isinstance(interfaces, Sequence):\n return [interfaces._impl for interfaces in interfaces]\n\n else:\n return interfaces._impl", "response": "Get impls from their interfaces."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating all LazyEvals in self. lzy_evals enough to update all owned LazyEvals.", "response": "def update_lazyevals(self):\n \"\"\"Update all LazyEvals in self\n\n self.lzy_evals must be set to LazyEval object(s) enough to\n update all owned LazyEval objects.\n \"\"\"\n if self.lazy_evals is None:\n return\n elif isinstance(self.lazy_evals, LazyEval):\n self.lazy_evals.get_updated()\n else:\n for lz in self.lazy_evals:\n lz.get_updated()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _baseattrs(self):\n\n result = {\n \"type\": type(self).__name__,\n \"id\": id(self),\n \"name\": self.name,\n \"fullname\": self.fullname,\n \"repr\": self._get_repr(),\n }\n\n return result", "response": "A dict of members expressed in literals"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _to_attrdict(self, attrs=None):\n result = self._baseattrs\n\n for attr in attrs:\n if hasattr(self, attr):\n result[attr] = getattr(self, attr)._to_attrdict(attrs)\n\n return result", "response": "Convert the attribute list to a dictionary of attributes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _update_data(self):\n\n func = self.owner.formula.func\n codeobj = func.__code__\n name = func.__name__ # self.cells.name # func.__name__\n namespace_impl = self.owner._namespace_impl.get_updated()\n namespace = namespace_impl.interfaces\n selfnode = get_node(self.owner, None, None)\n\n for name in self.owner.formula.srcnames:\n if name in namespace_impl and isinstance(\n namespace_impl[name], ReferenceImpl\n ):\n refnode = get_node(namespace_impl[name], None, None)\n self.owner.model.lexdep.add_path([selfnode, refnode])\n\n closure = func.__closure__ # None normally.\n if closure is not None: # pytest fails without this.\n closure = create_closure(self.owner.interface)\n\n self.altfunc = FunctionType(\n codeobj, namespace, name=name, closure=closure\n )", "response": "Update altfunc with new data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef convert_args(args, kwargs):\n\n found = False\n for arg in args:\n if isinstance(arg, Cells):\n found = True\n break\n\n if found:\n args = tuple(\n arg.value if isinstance(arg, Cells) else arg for arg in args\n )\n\n if kwargs is not None:\n for key, arg in kwargs.items():\n if isinstance(arg, Cells):\n kwargs[key] = arg.value\n\n return args, kwargs", "response": "If args and kwargs contains Cells convert them to their values."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef shareable_parameters(cells):\n result = []\n for c in cells.values():\n params = c.formula.parameters\n\n for i in range(min(len(result), len(params))):\n if params[i] != result[i]:\n return None\n\n for i in range(len(result), len(params)):\n result.append(params[i])\n\n return result", "response": "Returns a list of parameters that are shareable among multiple cells."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmake a copy of itself and return it.", "response": "def copy(self, space=None, name=None):\n \"\"\"Make a copy of itself and return it.\"\"\"\n return Cells(space=space, name=name, formula=self.formula)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a CellNode object for the given arguments.", "response": "def node(self, *args, **kwargs):\n \"\"\"Return a :class:`CellNode` object for the given arguments.\"\"\"\n return CellNode(get_node(self._impl, *convert_args(args, kwargs)))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _baseattrs(self):\n\n result = super()._baseattrs\n result[\"params\"] = \", \".join(self.parameters)\n return result", "response": "A dict of members expressed in literals"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef value(self):\n if self.has_value:\n return self._impl[OBJ].get_value(self._impl[KEY])\n else:\n raise ValueError(\"Value not found\")", "response": "Return the value of the cells."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _baseattrs(self):\n\n result = {\n \"type\": type(self).__name__,\n \"obj\": self.cells._baseattrs,\n \"args\": self.args,\n \"value\": self.value if self.has_value else None,\n \"predslen\": len(self.preds),\n \"succslen\": len(self.succs),\n \"repr_parent\": self.cells._impl.repr_parent(),\n \"repr\": self.cells._get_repr(),\n }\n\n return result", "response": "A dict of members expressed in literals"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreordering a list to match target by moving a sequence at a time.", "response": "def reorder_list(source, targetorder):\n \"\"\"Reorder a list to match target by moving a sequence at a time.\n\n Written for QtAbstractItemModel.moveRows.\n \"\"\"\n i = 0\n while i < len(source):\n\n if source[i] == targetorder[i]:\n i += 1\n continue\n else:\n i0 = i\n j0 = source.index(targetorder[i0])\n j = j0 + 1\n while j < len(source):\n if source[j] == targetorder[j - j0 + i0]:\n j += 1\n continue\n else:\n break\n move_elements(source, i0, j0, j - j0)\n i += j - j0"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmoves a sub sequence in a list", "response": "def move_elements(source, index_to, index_from, length):\n \"\"\"Move a sub sequence in a list\"\"\"\n\n sublist = [source.pop(index_from) for _ in range(length)]\n\n for _ in range(length):\n source.insert(index_to, sublist.pop())"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert column name to index.", "response": "def _get_col_index(name):\n \"\"\"Convert column name to index.\"\"\"\n\n index = string.ascii_uppercase.index\n col = 0\n for c in name.upper():\n col = col * 26 + index(c) + 1\n return col"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_range(book, range_, sheet):\n\n filename = None\n if isinstance(book, str):\n filename = book\n book = opxl.load_workbook(book, data_only=True)\n elif isinstance(book, opxl.Workbook):\n pass\n else:\n raise TypeError\n\n if _is_range_address(range_):\n sheet_names = [name.upper() for name in book.sheetnames]\n index = sheet_names.index(sheet.upper())\n data = book.worksheets[index][range_]\n else:\n data = _get_namedrange(book, range_, sheet)\n if data is None:\n raise ValueError(\n \"Named range '%s' not found in %s\" % (range_, filename or book)\n )\n\n return data", "response": "Return a range as nested dict of openpyxl cells."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef read_range(filepath, range_expr, sheet=None, dict_generator=None):\n\n def default_generator(cells):\n for row_ind, row in enumerate(cells):\n for col_ind, cell in enumerate(row):\n yield (row_ind, col_ind), cell.value\n\n book = opxl.load_workbook(filepath, data_only=True)\n\n if _is_range_address(range_expr):\n sheet_names = [name.upper() for name in book.sheetnames]\n index = sheet_names.index(sheet.upper())\n cells = book.worksheets[index][range_expr]\n else:\n cells = _get_namedrange(book, range_expr, sheet)\n\n # In case of a single cell, return its value.\n if isinstance(cells, opxl.cell.Cell):\n return cells.value\n\n if dict_generator is None:\n dict_generator = default_generator\n\n gen = dict_generator(cells)\n return {keyval[0]: keyval[1] for keyval in gen}", "response": "Read values from an Excel file into a nested list containing the values of the specified range."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_namedrange(book, rangename, sheetname=None):\n\n def cond(namedef):\n\n if namedef.type.upper() == \"RANGE\":\n if namedef.name.upper() == rangename.upper():\n\n if sheetname is None:\n if not namedef.localSheetId:\n return True\n\n else: # sheet local name\n sheet_id = [sht.upper() for sht in book.sheetnames].index(\n sheetname.upper()\n )\n\n if namedef.localSheetId == sheet_id:\n return True\n\n return False\n\n def get_destinations(name_def):\n \"\"\"Workaround for the bug in DefinedName.destinations\"\"\"\n\n from openpyxl.formula import Tokenizer\n from openpyxl.utils.cell import SHEETRANGE_RE\n\n if name_def.type == \"RANGE\":\n tok = Tokenizer(\"=\" + name_def.value)\n for part in tok.items:\n if part.subtype == \"RANGE\":\n m = SHEETRANGE_RE.match(part.value)\n if m.group(\"quoted\"):\n sheet_name = m.group(\"quoted\")\n else:\n sheet_name = m.group(\"notquoted\")\n\n yield sheet_name, m.group(\"cells\")\n\n namedef = next(\n (item for item in book.defined_names.definedName if cond(item)), None\n )\n\n if namedef is None:\n return None\n\n dests = get_destinations(namedef)\n xlranges = []\n\n sheetnames_upper = [name.upper() for name in book.sheetnames]\n\n for sht, addr in dests:\n if sheetname:\n sht = sheetname\n index = sheetnames_upper.index(sht.upper())\n xlranges.append(book.worksheets[index][addr])\n\n if len(xlranges) == 1:\n return xlranges[0]\n else:\n return xlranges", "response": "Get a named range from a workbook."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_mro(self, space):\n seqs = [self.get_mro(base) for base\n in self.get_bases(space)] + [list(self.get_bases(space))]\n res = []\n while True:\n non_empty = list(filter(None, seqs))\n\n if not non_empty:\n # Nothing left to process, we're done.\n res.insert(0, space)\n return res\n\n for seq in non_empty: # Find merge candidates among seq heads.\n candidate = seq[0]\n not_head = [s for s in non_empty if candidate in s[1:]]\n if not_head:\n # Reject the candidate.\n candidate = None\n else:\n break\n\n if not candidate:\n raise TypeError(\n \"inconsistent hierarchy, no C3 MRO is possible\")\n\n res.append(candidate)\n\n for seq in non_empty:\n # Remove candidate.\n if seq[0] == candidate:\n del seq[0]", "response": "Calculate the Method Resolution Order of bases using the C3 algorithm."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a new code object by altering some of the attributes of the code object.", "response": "def _alter_code(code, **attrs):\n \"\"\"Create a new code object by altering some of ``code`` attributes\n\n Args:\n code: code objcect\n attrs: a mapping of names of code object attrs to their values\n \"\"\"\n\n PyCode_New = ctypes.pythonapi.PyCode_New\n\n PyCode_New.argtypes = (\n ctypes.c_int,\n ctypes.c_int,\n ctypes.c_int,\n ctypes.c_int,\n ctypes.c_int,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.py_object,\n ctypes.c_int,\n ctypes.py_object)\n\n PyCode_New.restype = ctypes.py_object\n\n args = [\n [code.co_argcount, 'co_argcount'],\n [code.co_kwonlyargcount, 'co_kwonlyargcount'],\n [code.co_nlocals, 'co_nlocals'],\n [code.co_stacksize, 'co_stacksize'],\n [code.co_flags, 'co_flags'],\n [code.co_code, 'co_code'],\n [code.co_consts, 'co_consts'],\n [code.co_names, 'co_names'],\n [code.co_varnames, 'co_varnames'],\n [code.co_freevars, 'co_freevars'],\n [code.co_cellvars, 'co_cellvars'],\n [code.co_filename, 'co_filename'],\n [code.co_name, 'co_name'],\n [code.co_firstlineno, 'co_firstlineno'],\n [code.co_lnotab, 'co_lnotab']]\n\n for arg in args:\n if arg[1] in attrs:\n arg[0] = attrs[arg[1]]\n\n return PyCode_New(\n args[0][0], # code.co_argcount,\n args[1][0], # code.co_kwonlyargcount,\n args[2][0], # code.co_nlocals,\n args[3][0], # code.co_stacksize,\n args[4][0], # code.co_flags,\n args[5][0], # code.co_code,\n args[6][0], # code.co_consts,\n args[7][0], # code.co_names,\n args[8][0], # code.co_varnames,\n args[9][0], # code.co_freevars,\n args[10][0], # code.co_cellvars,\n args[11][0], # code.co_filename,\n args[12][0], # code.co_name,\n args[13][0], # code.co_firstlineno,\n args[14][0])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a new function with modified local variables.", "response": "def alter_freevars(func, globals_=None, **vars):\n \"\"\"Replace local variables with free variables\n\n Warnings:\n This function does not work.\n \"\"\"\n\n if globals_ is None:\n globals_ = func.__globals__\n\n frees = tuple(vars.keys())\n oldlocs = func.__code__.co_names\n newlocs = tuple(name for name in oldlocs if name not in frees)\n\n code = _alter_code(func.__code__,\n co_freevars=frees,\n co_names=newlocs,\n co_flags=func.__code__.co_flags | inspect.CO_NESTED)\n closure = _create_closure(*vars.values())\n\n return FunctionType(code, globals_, closure=closure)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfixes the lamdaline of a source string.", "response": "def fix_lamdaline(source):\n \"\"\"Remove the last redundant token from lambda expression\n\n lambda x: return x)\n ^\n Return string without irrelevant tokens\n returned from inspect.getsource on lamda expr returns\n \"\"\"\n\n # Using undocumented generate_tokens due to a tokenize.tokenize bug\n # See https://bugs.python.org/issue23297\n strio = io.StringIO(source)\n gen = tokenize.generate_tokens(strio.readline)\n\n tkns = []\n try:\n for t in gen:\n tkns.append(t)\n except tokenize.TokenError:\n pass\n\n # Find the position of 'lambda'\n lambda_pos = [(t.type, t.string) for t in tkns].index(\n (tokenize.NAME, \"lambda\")\n )\n\n # Ignore tokes before 'lambda'\n tkns = tkns[lambda_pos:]\n\n # Find the position of th las OP\n lastop_pos = (\n len(tkns) - 1 - [t.type for t in tkns[::-1]].index(tokenize.OP)\n )\n lastop = tkns[lastop_pos]\n\n # Remove OP from the line\n fiedlineno = lastop.start[0]\n fixedline = lastop.line[: lastop.start[1]] + lastop.line[lastop.end[1] :]\n\n tkns = tkns[:lastop_pos]\n\n fixedlines = \"\"\n last_lineno = 0\n for t in tkns:\n if last_lineno == t.start[0]:\n continue\n elif t.start[0] == fiedlineno:\n fixedlines += fixedline\n last_lineno = t.start[0]\n else:\n fixedlines += t.line\n last_lineno = t.start[0]\n\n return fixedlines"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfinding the first FuncDef ast object in source", "response": "def find_funcdef(source):\n \"\"\"Find the first FuncDef ast object in source\"\"\"\n\n try:\n module_node = compile(\n source, \"\", mode=\"exec\", flags=ast.PyCF_ONLY_AST\n )\n except SyntaxError:\n return find_funcdef(fix_lamdaline(source))\n\n for node in ast.walk(module_node):\n if isinstance(node, ast.FunctionDef) or isinstance(node, ast.Lambda):\n return node\n\n raise ValueError(\"function definition not found\")"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_params(source):\n\n funcdef = find_funcdef(source)\n params = []\n for node in ast.walk(funcdef.args):\n if isinstance(node, ast.arg):\n if node.arg not in params:\n params.append(node.arg)\n\n return params", "response": "Extract parameters from a function definition"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef extract_names(source):\n if source is None:\n return None\n\n source = dedent(source)\n funcdef = find_funcdef(source)\n params = extract_params(source)\n names = []\n\n if isinstance(funcdef, ast.FunctionDef):\n stmts = funcdef.body\n elif isinstance(funcdef, ast.Lambda):\n stmts = [funcdef.body]\n else:\n raise ValueError(\"must not happen\")\n\n for stmt in stmts:\n for node in ast.walk(stmt):\n if isinstance(node, ast.Name):\n if node.id not in names and node.id not in params:\n names.append(node.id)\n\n return names", "response": "Extract names from a function definition in the source."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntruing if src is a function definition", "response": "def is_funcdef(src):\n \"\"\"True if src is a function definition\"\"\"\n\n module_node = ast.parse(dedent(src))\n\n if len(module_node.body) == 1 and isinstance(\n module_node.body[0], ast.FunctionDef\n ):\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving decorators from function definition", "response": "def remove_decorator(source: str):\n \"\"\"Remove decorators from function definition\"\"\"\n lines = source.splitlines()\n atok = asttokens.ASTTokens(source, parse=True)\n\n for node in ast.walk(atok.tree):\n if isinstance(node, ast.FunctionDef):\n break\n\n if node.decorator_list:\n deco_first = node.decorator_list[0]\n deco_last = node.decorator_list[-1]\n line_first = atok.tokens[deco_first.first_token.index - 1].start[0]\n line_last = atok.tokens[deco_last.last_token.index + 1].start[0]\n\n lines = lines[:line_first - 1] + lines[line_last:]\n\n return \"\\n\".join(lines) + \"\\n\""} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef replace_funcname(source: str, name: str):\n\n lines = source.splitlines()\n atok = asttokens.ASTTokens(source, parse=True)\n\n for node in ast.walk(atok.tree):\n if isinstance(node, ast.FunctionDef):\n break\n\n i = node.first_token.index\n for i in range(node.first_token.index, node.last_token.index):\n if (atok.tokens[i].type == token.NAME\n and atok.tokens[i].string == \"def\"):\n break\n\n lineno, col_begin = atok.tokens[i + 1].start\n lineno_end, col_end = atok.tokens[i + 1].end\n\n assert lineno == lineno_end\n\n lines[lineno-1] = (\n lines[lineno-1][:col_begin] + name + lines[lineno-1][col_end:]\n )\n\n return \"\\n\".join(lines) + \"\\n\"", "response": "Replace function name in source with name"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntruing if only one lambda expression is included", "response": "def has_lambda(src):\n \"\"\"True if only one lambda expression is included\"\"\"\n\n module_node = ast.parse(dedent(src))\n lambdaexp = [node for node in ast.walk(module_node)\n if isinstance(node, ast.Lambda)]\n\n return bool(lambdaexp)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreload the source function from the source module.", "response": "def _reload(self, module=None):\n \"\"\"Reload the source function from the source module.\n\n **Internal use only**\n Update the source function of the formula.\n This method is used to updated the underlying formula\n when the source code of the module in which the source function\n is read from is modified.\n\n If the formula was not created from a module, an error is raised.\n If ``module_`` is not given, the source module of the formula is\n reloaded. If ``module_`` is given and matches the source module,\n then the module_ is used without being reloaded.\n If ``module_`` is given and does not match the source module of\n the formula, an error is raised.\n\n Args:\n module_: A ``ModuleSource`` object\n\n Returns:\n self\n \"\"\"\n if self.module is None:\n raise RuntimeError\n elif module is None:\n import importlib\n\n module = ModuleSource(importlib.reload(module))\n elif module.name != self.module:\n raise RuntimeError\n\n if self.name in module.funcs:\n func = module.funcs[self.name]\n self.__init__(func=func)\n else:\n self.__init__(func=NULL_FORMULA)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets long description from README. rst", "response": "def get_description():\n \"\"\"Get long description from README.\"\"\"\n with open(path.join(here, 'README.rst'), 'r') as f:\n data = f.read()\n return data"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert the cells in the view into a DataFrame object.", "response": "def to_frame(self, *args):\n \"\"\"Convert the cells in the view into a DataFrame object.\n\n If ``args`` is not given, this method returns a DataFrame that\n has an Index or a MultiIndex depending of the number of\n cells parameters and columns each of which corresponds to each\n cells included in the view.\n\n ``args`` can be given to calculate cells values and limit the\n DataFrame indexes to the given arguments.\n\n The cells in this view may have different number of parameters,\n but parameters shared among multiple cells\n must appear in the same position in all the parameter lists.\n For example,\n Having ``foo()``, ``bar(x)`` and ``baz(x, y=1)`` is okay\n because the shared parameter ``x`` is always the first parameter,\n but this method does not work if the view has ``quz(x, z=2, y=1)``\n cells in addition to the first three cells, because ``y`` appears\n in different positions.\n\n Args:\n args(optional): multiple arguments,\n or an iterator of arguments to the cells.\n \"\"\"\n if sys.version_info < (3, 6, 0):\n from collections import OrderedDict\n\n impls = OrderedDict()\n for name, obj in self.items():\n impls[name] = obj._impl\n else:\n impls = get_impls(self)\n\n return _to_frame_inner(impls, args)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef new_cells(self, name=None, formula=None):\n # Outside formulas only\n return self._impl.new_cells(name, formula).interface", "response": "Create a new cells in the space."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a cells from a module.", "response": "def import_funcs(self, module):\n \"\"\"Create a cells from a module.\"\"\"\n # Outside formulas only\n newcells = self._impl.new_cells_from_module(module)\n return get_interfaces(newcells)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates multiple cells from an Excel file.", "response": "def new_cells_from_excel(\n self,\n book,\n range_,\n sheet=None,\n names_row=None,\n param_cols=None,\n param_order=None,\n transpose=False,\n names_col=None,\n param_rows=None,\n ):\n \"\"\"Create multiple cells from an Excel range.\n\n This method reads values from a range in an Excel file,\n create cells and populate them with the values in the range.\n To use this method, ``openpyxl`` package must be installed.\n\n The Excel file to read data from is specified by ``book``\n parameters. The ``range_`` can be a range address, such as \"$G4:$K10\",\n or a named range. In case a range address is given,\n ``sheet`` must also be given.\n\n By default, cells data are interpreted as being laid out side-by-side.\n ``names_row`` is a row index (starting from 0) to specify the\n row that contains the names of cells and parameters.\n Cells and parameter names must be contained in a single row.\n ``param_cols`` accepts a sequence (such as list or tuple) of\n column indexes (starting from 0) that indicate columns that\n contain cells arguments.\n\n **2-dimensional cells definitions**\n\n The optional ``names_col`` and ``param_rows`` parameters are used,\n when data for one cells spans more than one column.\n In such cases, the cells data is 2-dimensional, and\n there must be parameter row(s) across the columns\n that contain arguments of the parameters.\n A sequence of row indexes that indicate parameter rows\n is passed to ``param_rows``.\n The names of those parameters must be contained in the\n same rows as parameter values (arguments), and\n ``names_col`` is to indicate the column position at which\n the parameter names are defined.\n\n **Horizontal arrangement**\n\n By default, cells data are interpreted as being placed\n side-by-side, regardless of whether one cells corresponds\n to a single column or multiple columns.\n ``transpose`` parameter is used to alter this orientation,\n and if it is set to ``True``, cells values are\n interpreted as being placed one above the other.\n \"row(s)\" and \"col(s)\" in the parameter\n names are interpreted inversely, i.e.\n all indexes passed to \"row(s)\" parameters are interpreted\n as column indexes,\n and all indexes passed to \"col(s)\" parameters as row indexes.\n\n\n Args:\n book (str): Path to an Excel file.\n range_ (str): Range expression, such as \"A1\", \"$G4:$K10\",\n or named range \"NamedRange1\".\n sheet (str): Sheet name (case ignored).\n names_row (optional): an index number indicating\n what row contains the names of cells and parameters.\n Defaults to the top row (0).\n param_cols (optional): a sequence of index numbers\n indicating parameter columns.\n Defaults to only the leftmost column ([0]).\n names_col (optional): an index number, starting from 0,\n indicating what column contains additional parameters.\n param_rows (optional): a sequence of index numbers, starting from\n 0, indicating rows of additional parameters, in case cells are\n defined in two dimensions.\n transpose (optional): Defaults to ``False``.\n If set to ``True``, \"row(s)\" and \"col(s)\" in the parameter\n names are interpreted inversely, i.e.\n all indexes passed to \"row(s)\" parameters are interpreted\n as column indexes,\n and all indexes passed to \"col(s)\" parameters as row indexes.\n param_order (optional): a sequence to reorder the parameters.\n The elements of the sequence are the indexes of ``param_cols``\n elements, and optionally the index of ``param_rows`` elements\n shifted by the length of ``param_cols``.\n \"\"\"\n return self._impl.new_cells_from_excel(\n book,\n range_,\n sheet,\n names_row,\n param_cols,\n param_order,\n transpose,\n names_col,\n param_rows,\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_object(self, name):\n\n parts = name.split(\".\")\n child = parts.pop(0)\n\n if parts:\n return self.spaces[child].get_object(\".\".join(parts))\n else:\n return self._namespace_impl[child]", "response": "Retrieve an object by a dotted name relative to the space."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_dynamic_base(self, bases_):\n bases = tuple(\n base.bases[0] if base.is_dynamic() else base for base in bases_\n )\n\n if len(bases) == 1:\n return bases[0]\n\n elif len(bases) > 1:\n return self.model.get_dynamic_base(bases)\n\n else:\n RuntimeError(\"must not happen\")", "response": "Create or get the base space from a list of spaces\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a new dynamic root space.", "response": "def _new_dynspace(\n self,\n name=None,\n bases=None,\n formula=None,\n refs=None,\n arguments=None,\n source=None,\n ):\n \"\"\"Create a new dynamic root space.\"\"\"\n\n if name is None:\n name = self.spacenamer.get_next(self.namespace)\n\n if name in self.namespace:\n raise ValueError(\"Name '%s' already exists.\" % name)\n\n if not is_valid_name(name):\n raise ValueError(\"Invalid name '%s'.\" % name)\n\n space = RootDynamicSpaceImpl(\n parent=self,\n name=name,\n formula=formula,\n refs=refs,\n source=source,\n arguments=arguments,\n )\n space.is_derived = False\n self._set_space(space)\n\n if bases: # i.e. not []\n dynbase = self._get_dynamic_base(bases)\n space._dynbase = dynbase\n dynbase._dynamic_subs.append(space)\n\n return space"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_dynspace(self, args, kwargs=None):\n\n node = get_node(self, *convert_args(args, kwargs))\n key = node[KEY]\n\n if key in self.param_spaces:\n return self.param_spaces[key]\n\n else:\n last_self = self.system.self\n self.system.self = self\n\n try:\n space_args = self.eval_formula(node)\n\n finally:\n self.system.self = last_self\n\n if space_args is None:\n space_args = {\"bases\": [self]} # Default\n else:\n if \"bases\" in space_args:\n bases = get_impls(space_args[\"bases\"])\n if isinstance(bases, StaticSpaceImpl):\n space_args[\"bases\"] = [bases]\n elif bases is None:\n space_args[\"bases\"] = [self] # Default\n else:\n space_args[\"bases\"] = bases\n else:\n space_args[\"bases\"] = [self]\n\n space_args[\"arguments\"] = node_get_args(node)\n space = self._new_dynspace(**space_args)\n self.param_spaces[key] = space\n space.inherit(clear_value=False)\n return space", "response": "Create a dynamic root space with the given arguments."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalls after unpickling to restore some attributes manually.", "response": "def restore_state(self, system):\n \"\"\"Called after unpickling to restore some attributes manually.\"\"\"\n super().restore_state(system)\n BaseSpaceContainerImpl.restore_state(self, system)\n\n for cells in self._cells.values():\n cells.restore_state(system)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef new_cells_from_excel(\n self,\n book,\n range_,\n sheet=None,\n names_row=None,\n param_cols=None,\n param_order=None,\n transpose=False,\n names_col=None,\n param_rows=None,\n ):\n \"\"\"Create multiple cells from an Excel range.\n\n Args:\n book (str): Path to an Excel file.\n range_ (str): Range expression, such as \"A1\", \"$G4:$K10\",\n or named range \"NamedRange1\".\n sheet (str): Sheet name (case ignored).\n names_row: Cells names in a sequence, or an integer number, or\n a string expression indicating row (or column depending on\n ```orientation```) to read cells names from.\n param_cols: a sequence of them\n indicating parameter columns (or rows depending on ```\n orientation```)\n param_order: a sequence of integers representing\n the order of params and extra_params.\n transpose: in which direction 'vertical' or 'horizontal'\n names_col: a string or a list of names of the extra params.\n param_rows: integer or string expression, or a sequence of them\n indicating row (or column) to be interpreted as parameters.\n \"\"\"\n import modelx.io.excel as xl\n\n cellstable = xl.CellsTable(\n book,\n range_,\n sheet,\n names_row,\n param_cols,\n param_order,\n transpose,\n names_col,\n param_rows,\n )\n\n if cellstable.param_names:\n sig = \"=None, \".join(cellstable.param_names) + \"=None\"\n else:\n sig = \"\"\n\n blank_func = \"def _blank_func(\" + sig + \"): pass\"\n\n for cellsdata in cellstable.items():\n cells = self.new_cells(name=cellsdata.name, formula=blank_func)\n for args, value in cellsdata.items():\n cells.set_value(args, value)", "response": "Create multiple cells from an Excel file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_attr(self, name, value):\n if not is_valid_name(name):\n raise ValueError(\"Invalid name '%s'\" % name)\n\n if name in self.namespace:\n if name in self.refs:\n if name in self.self_refs:\n self.new_ref(name, value)\n else:\n raise KeyError(\"Ref '%s' cannot be changed\" % name)\n\n elif name in self.cells:\n if self.cells[name].is_scalar():\n self.cells[name].set_value((), value)\n else:\n raise AttributeError(\"Cells '%s' is not a scalar.\" % name)\n else:\n raise ValueError\n else:\n self.new_ref(name, value)", "response": "Implementation of attribute setting by user script\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef del_attr(self, name):\n if name in self.namespace:\n if name in self.cells:\n self.del_cells(name)\n elif name in self.spaces:\n self.del_space(name)\n elif name in self.refs:\n self.del_ref(name)\n else:\n raise RuntimeError(\"Must not happen\")\n else:\n raise KeyError(\"'%s' not found in Space '%s'\" % (name, self.name))", "response": "Implementation of attribute deletion by user script\n Called by user script\n . name by user script\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef del_cells(self, name):\n if name in self.cells:\n cells = self.cells[name]\n self.cells.del_item(name)\n self.inherit()\n self.model.spacegraph.update_subspaces(self)\n\n elif name in self.dynamic_spaces:\n cells = self.dynamic_spaces.pop(name)\n self.dynamic_spaces.set_update()\n\n else:\n raise KeyError(\"Cells '%s' does not exist\" % name)\n\n NullImpl(cells)", "response": "Implementation of cells deletion\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef cellsiter_to_dataframe(cellsiter, args, drop_allna=True):\n from modelx.core.cells import shareable_parameters\n\n if len(args):\n indexes = shareable_parameters(cellsiter)\n else:\n indexes = get_all_params(cellsiter.values())\n\n result = None\n\n for cells in cellsiter.values():\n df = cells_to_dataframe(cells, args)\n\n if drop_allna and df.isnull().all().all():\n continue # Ignore all NA or empty\n\n if df.index.names != [None]:\n if isinstance(df.index, pd.MultiIndex):\n if _pd_ver < (0, 20):\n df = _reset_naindex(df)\n\n df = df.reset_index()\n\n missing_params = set(indexes) - set(df)\n\n for params in missing_params:\n df[params] = np.nan\n\n if result is None:\n result = df\n else:\n try:\n result = pd.merge(result, df, how=\"outer\")\n except MergeError:\n # When no common column exists, i.e. all cells are scalars.\n result = pd.concat([result, df], axis=1)\n except ValueError:\n # When common columns are not coercible (numeric vs object),\n # Make the numeric column object type\n cols = set(result.columns) & set(df.columns)\n for col in cols:\n\n # When only either of them has object dtype\n if (\n len(\n [\n str(frame[col].dtype)\n for frame in (result, df)\n if str(frame[col].dtype) == \"object\"\n ]\n )\n == 1\n ):\n\n if str(result[col].dtype) == \"object\":\n frame = df\n else:\n frame = result\n frame[[col]] = frame[col].astype(\"object\")\n\n # Try again\n result = pd.merge(result, df, how=\"outer\")\n\n if result is None:\n return pd.DataFrame()\n else:\n return result.set_index(indexes) if indexes else result", "response": "Convert multiple cells to a DataFrame."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts a CellImpl into a Series.", "response": "def cells_to_series(cells, args):\n \"\"\"Convert a CellImpl into a Series.\n\n `args` must be a sequence of argkeys.\n\n `args` can be longer or shorter then the number of cell's parameters.\n If shorter, then defaults are filled if any, else raise error.\n If longer, then redundant args are ignored.\n \"\"\"\n\n paramlen = len(cells.formula.parameters)\n is_multidx = paramlen > 1\n\n if len(cells.data) == 0:\n data = {}\n indexes = None\n\n elif paramlen == 0: # Const Cells\n data = list(cells.data.values())\n indexes = [np.nan]\n\n else:\n\n if len(args) > 0:\n defaults = tuple(\n param.default\n for param in cells.formula.signature.parameters.values()\n )\n updated_args = []\n for arg in args:\n\n if len(arg) > paramlen:\n arg = arg[:paramlen]\n elif len(arg) < paramlen:\n arg += defaults[len(arg) :]\n\n updated_args.append(arg)\n\n items = [\n (arg, cells.data[arg])\n for arg in updated_args\n if arg in cells.data\n ]\n else:\n items = [(key, value) for key, value in cells.data.items()]\n\n if not is_multidx: # Peel 1-element tuple\n items = [(key[0], value) for key, value in items]\n\n if len(items) == 0:\n indexes, data = None, {}\n else:\n indexes, data = zip(*items)\n if is_multidx:\n indexes = pd.MultiIndex.from_tuples(indexes)\n\n result = pd.Series(data=data, name=cells.name, index=indexes)\n\n if indexes is not None and any(i is not np.nan for i in indexes):\n result.index.names = list(cells.formula.parameters)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove all descendants of source.", "response": "def clear_descendants(self, source, clear_source=True):\n \"\"\"Remove all descendants of(reachable from) `source`.\n\n Args:\n source: Node descendants\n clear_source(bool): Remove origin too if True.\n Returns:\n set: The removed nodes.\n \"\"\"\n desc = nx.descendants(self, source)\n if clear_source:\n desc.add(source)\n self.remove_nodes_from(desc)\n return desc"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving all nodes with obj and their descendants.", "response": "def clear_obj(self, obj):\n \"\"\"\"Remove all nodes with `obj` and their descendants.\"\"\"\n obj_nodes = self.get_nodes_with(obj)\n removed = set()\n for node in obj_nodes:\n if self.has_node(node):\n removed.update(self.clear_descendants(node))\n return removed"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns nodes with obj.", "response": "def get_nodes_with(self, obj):\n \"\"\"Return nodes with `obj`.\"\"\"\n result = set()\n\n if nx.__version__[0] == \"1\":\n nodes = self.nodes_iter()\n else:\n nodes = self.nodes\n\n for node in nodes:\n if node[OBJ] == obj:\n result.add(node)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_path(self, nodes, **attr):\n if nx.__version__[0] == \"1\":\n return super().add_path(nodes, **attr)\n else:\n return nx.add_path(self, nodes, **attr)", "response": "In replacement for deprecated add_path method"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrenaming the model itself", "response": "def rename(self, name):\n \"\"\"Rename the model itself\"\"\"\n self._impl.system.rename_model(new_name=name, old_name=self.name)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrename the current object.", "response": "def rename(self, name):\n \"\"\"Rename self. Must be called only by its system.\"\"\"\n if is_valid_name(name):\n if name not in self.system.models:\n self.name = name\n return True # Rename success\n else: # Model name already exists\n return False\n else:\n raise ValueError(\"Invalid name '%s'.\" % name)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nclears values and nodes calculated from source.", "response": "def clear_descendants(self, source, clear_source=True):\n \"\"\"Clear values and nodes calculated from `source`.\"\"\"\n removed = self.cellgraph.clear_descendants(source, clear_source)\n for node in removed:\n del node[OBJ].data[node[KEY]]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef clear_obj(self, obj):\n removed = self.cellgraph.clear_obj(obj)\n for node in removed:\n del node[OBJ].data[node[KEY]]", "response": "Clear values and nodes of obj and their dependants."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_object(self, name):\n parts = name.split(\".\")\n space = self.spaces[parts.pop(0)]\n if parts:\n return space.get_object(\".\".join(parts))\n else:\n return space", "response": "Retrieve an object by a dotted name relative to the model."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalling after unpickling to restore some attributes manually.", "response": "def restore_state(self, system):\n \"\"\"Called after unpickling to restore some attributes manually.\"\"\"\n Impl.restore_state(self, system)\n BaseSpaceContainerImpl.restore_state(self, system)\n mapping = {}\n for node in self.cellgraph:\n if isinstance(node, tuple):\n name, key = node\n else:\n name, key = node, None\n cells = self.get_object(name)\n mapping[node] = get_node(cells, key, None)\n\n self.cellgraph = nx.relabel_nodes(self.cellgraph, mapping)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates of get a base space for a tuple of bases", "response": "def get_dynamic_base(self, bases: tuple):\n \"\"\"Create of get a base space for a tuple of bases\"\"\"\n\n try:\n return self._dynamic_bases_inverse[bases]\n except KeyError:\n name = self._dynamic_base_namer.get_next(self._dynamic_bases)\n base = self._new_space(name=name)\n self.spacegraph.add_space(base)\n self._dynamic_bases[name] = base\n self._dynamic_bases_inverse[bases] = base\n base.add_bases(bases)\n return base"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check_mro(self, bases):\n\n try:\n self.add_node(\"temp\")\n for base in bases:\n nx.DiGraph.add_edge(self, base, \"temp\")\n result = self.get_mro(\"temp\")[1:]\n\n finally:\n self.remove_node(\"temp\")\n\n return result", "response": "Check if C3 MRO is possible with given bases"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of command names supported by the current user.", "response": "def get_command_names():\n \"\"\"\n Returns a list of command names supported\n \"\"\"\n ret = []\n for f in os.listdir(COMMAND_MODULE_PATH):\n if os.path.isfile(os.path.join(COMMAND_MODULE_PATH, f)) and f.endswith(COMMAND_MODULE_SUFFIX):\n ret.append(f[:-len(COMMAND_MODULE_SUFFIX)])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dictionary value for the specified key.", "response": "def get(vals, key, default_val=None):\n \"\"\"\n Returns a dictionary value\n \"\"\"\n val = vals\n for part in key.split('.'):\n if isinstance(val, dict):\n val = val.get(part, None)\n if val is None:\n return default_val\n else:\n return default_val\n return val"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef parse_option_settings(option_settings):\n ret = []\n for namespace, params in list(option_settings.items()):\n for key, value in list(params.items()):\n ret.append((namespace, key, value))\n return ret", "response": "Parses option_settings as they are defined in the configuration file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing an environment config into a single dict.", "response": "def parse_env_config(config, env_name):\n \"\"\"\n Parses an environment config\n \"\"\"\n all_env = get(config, 'app.all_environments', {})\n env = get(config, 'app.environments.' + str(env_name), {})\n return merge_dict(all_env, env)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_archive(directory, filename, config={}, ignore_predicate=None, ignored_files=['.git', '.svn']):\n with zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_DEFLATED) as zip_file:\n root_len = len(os.path.abspath(directory))\n\n # create it\n out(\"Creating archive: \" + str(filename))\n for root, dirs, files in os.walk(directory, followlinks=True):\n archive_root = os.path.abspath(root)[root_len + 1:]\n for f in files:\n fullpath = os.path.join(root, f)\n archive_name = os.path.join(archive_root, f)\n\n # ignore the file we're creating\n if filename in fullpath:\n continue\n\n # ignored files\n if ignored_files is not None:\n for name in ignored_files:\n if fullpath.endswith(name):\n out(\"Skipping: \" + str(name))\n continue\n\n # do predicate\n if ignore_predicate is not None:\n if not ignore_predicate(archive_name):\n out(\"Skipping: \" + str(archive_name))\n continue\n\n out(\"Adding: \" + str(archive_name))\n zip_file.write(fullpath, archive_name, zipfile.ZIP_DEFLATED)\n\n return filename", "response": "Creates an archive from a directory and returns the filename that was created."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_config_files_to_archive(directory, filename, config={}):\n with zipfile.ZipFile(filename, 'a') as zip_file:\n for conf in config:\n for conf, tree in list(conf.items()):\n if 'yaml' in tree:\n content = yaml.dump(tree['yaml'], default_flow_style=False)\n else:\n content = tree.get('content', '')\n out(\"Adding file \" + str(conf) + \" to archive \" + str(filename))\n file_entry = zipfile.ZipInfo(conf)\n file_entry.external_attr = tree.get('permissions', 0o644) << 16 \n zip_file.writestr(file_entry, content)\n\n return filename", "response": "Adds configuration files to an existing archive"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef swap_environment_cnames(self, from_env_name, to_env_name):\n self.ebs.swap_environment_cnames(source_environment_name=from_env_name,\n destination_environment_name=to_env_name)", "response": "Swaps the environment names of a specific environment."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upload_archive(self, filename, key, auto_create_bucket=True):\n try:\n bucket = self.s3.get_bucket(self.aws.bucket)\n if ((\n self.aws.region != 'us-east-1' and self.aws.region != 'eu-west-1') and bucket.get_location() != self.aws.region) or (\n self.aws.region == 'us-east-1' and bucket.get_location() != '') or (\n self.aws.region == 'eu-west-1' and bucket.get_location() != 'eu-west-1'):\n raise Exception(\"Existing bucket doesn't match region\")\n except S3ResponseError:\n bucket = self.s3.create_bucket(self.aws.bucket, location=self.aws.region)\n\n def __report_upload_progress(sent, total):\n if not sent:\n sent = 0\n if not total:\n total = 0\n out(\"Uploaded \" + str(sent) + \" bytes of \" + str(total) \\\n + \" (\" + str(int(float(max(1, sent)) / float(total) * 100)) + \"%)\")\n\n # upload the new version\n k = Key(bucket)\n k.key = self.aws.bucket_path + key\n k.set_metadata('time', str(time()))\n k.set_contents_from_filename(filename, cb=__report_upload_progress, num_cb=10)", "response": "Uploads an application archive version to s3"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_application(self, description=None):\n out(\"Creating application \" + str(self.app_name))\n self.ebs.create_application(self.app_name, description=description)", "response": "Creates an application and sets the helpers current\n app_name to the application created by this application."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_application(self):\n out(\"Deleting application \" + str(self.app_name))\n self.ebs.delete_application(self.app_name, terminate_env_by_force=True)", "response": "Delete the application from the ebs"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns whether or not the given app_name exists.", "response": "def application_exists(self):\n \"\"\"\n Returns whether or not the given app_name exists\n \"\"\"\n response = self.ebs.describe_applications(application_names=[self.app_name])\n return len(response['DescribeApplicationsResponse']['DescribeApplicationsResult']['Applications']) > 0"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a new environment in the cluster.", "response": "def create_environment(self, env_name, version_label=None,\n solution_stack_name=None, cname_prefix=None, description=None,\n option_settings=None, tier_name='WebServer', tier_type='Standard', tier_version='1.1'):\n \"\"\"\n Creates a new environment\n \"\"\"\n out(\"Creating environment: \" + str(env_name) + \", tier_name:\" + str(tier_name) + \", tier_type:\" + str(tier_type))\n self.ebs.create_environment(self.app_name, env_name,\n version_label=version_label,\n solution_stack_name=solution_stack_name,\n cname_prefix=cname_prefix,\n description=description,\n option_settings=option_settings,\n tier_type=tier_type,\n tier_name=tier_name,\n tier_version=tier_version)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef environment_exists(self, env_name):\n response = self.ebs.describe_environments(application_name=self.app_name, environment_names=[env_name],\n include_deleted=False)\n return len(response['DescribeEnvironmentsResponse']['DescribeEnvironmentsResult']['Environments']) > 0 \\\n and response['DescribeEnvironmentsResponse']['DescribeEnvironmentsResult']['Environments'][0][\n 'Status'] != 'Terminated'", "response": "Returns whether or not the given environment exists"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_environments(self):\n response = self.ebs.describe_environments(application_name=self.app_name, include_deleted=False)\n return response['DescribeEnvironmentsResponse']['DescribeEnvironmentsResult']['Environments']", "response": "Returns the list of environments in the current environment"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate an application version of an environment.", "response": "def update_environment(self, environment_name, description=None, option_settings=[], tier_type=None, tier_name=None,\n tier_version='1.0'):\n \"\"\"\n Updates an application version\n \"\"\"\n out(\"Updating environment: \" + str(environment_name))\n messages = self.ebs.validate_configuration_settings(self.app_name, option_settings,\n environment_name=environment_name)\n messages = messages['ValidateConfigurationSettingsResponse']['ValidateConfigurationSettingsResult']['Messages']\n ok = True\n for message in messages:\n if message['Severity'] == 'error':\n ok = False\n out(\"[\" + message['Severity'] + \"] \" + str(environment_name) + \" - '\" \\\n + message['Namespace'] + \":\" + message['OptionName'] + \"': \" + message['Message'])\n self.ebs.update_environment(\n environment_name=environment_name,\n description=description,\n option_settings=option_settings,\n tier_type=tier_type,\n tier_name=tier_name,\n tier_version=tier_version)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns an environment name for the given cname", "response": "def environment_name_for_cname(self, env_cname):\n \"\"\"\n Returns an environment name for the given cname\n \"\"\"\n envs = self.get_environments()\n for env in envs:\n if env['Status'] != 'Terminated' \\\n and 'CNAME' in env \\\n and env['CNAME'] \\\n and env['CNAME'].lower().startswith(env_cname.lower() + '.'):\n return env['EnvironmentName']\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeploys a version to an environment", "response": "def deploy_version(self, environment_name, version_label):\n \"\"\"\n Deploys a version to an environment\n \"\"\"\n out(\"Deploying \" + str(version_label) + \" to \" + str(environment_name))\n self.ebs.update_environment(environment_name=environment_name, version_label=version_label)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_versions(self):\n response = self.ebs.describe_application_versions(application_name=self.app_name)\n return response['DescribeApplicationVersionsResponse']['DescribeApplicationVersionsResult']['ApplicationVersions']", "response": "Returns the available versions of the application"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_application_version(self, version_label, key):\n out(\"Creating application version \" + str(version_label) + \" for \" + str(key))\n self.ebs.create_application_version(self.app_name, version_label,\n s3_bucket=self.aws.bucket, s3_key=self.aws.bucket_path+key)", "response": "Creates an application version"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_unused_versions(self, versions_to_keep=10):\n\n # get versions in use\n environments = self.ebs.describe_environments(application_name=self.app_name, include_deleted=False)\n environments = environments['DescribeEnvironmentsResponse']['DescribeEnvironmentsResult']['Environments']\n versions_in_use = []\n for env in environments:\n versions_in_use.append(env['VersionLabel'])\n\n # get all versions\n versions = self.ebs.describe_application_versions(application_name=self.app_name)\n versions = versions['DescribeApplicationVersionsResponse']['DescribeApplicationVersionsResult'][\n 'ApplicationVersions']\n versions = sorted(versions, reverse=True, key=functools.cmp_to_key(lambda x, y: (x['DateCreated'] > y['DateCreated']) - (x['DateCreated'] < y['DateCreated'])))\n\n # delete versions in use\n for version in versions[versions_to_keep:]:\n if version['VersionLabel'] in versions_in_use:\n out(\"Not deleting \" + version[\"VersionLabel\"] + \" because it is in use\")\n else:\n out(\"Deleting unused version: \" + version[\"VersionLabel\"])\n self.ebs.delete_application_version(application_name=self.app_name,\n version_label=version['VersionLabel'])\n sleep(2)", "response": "Deletes unused versions in the specified list of versions_to_keep."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndescribing the events from the given environment.", "response": "def describe_events(self, environment_name, next_token=None, start_time=None):\n \"\"\"\n Describes events from the given environment\n \"\"\"\n events = self.ebs.describe_events(\n application_name=self.app_name,\n environment_name=environment_name,\n next_token=next_token,\n start_time=start_time + 'Z')\n\n return (events['DescribeEventsResponse']['DescribeEventsResult']['Events'], events['DescribeEventsResponse']['DescribeEventsResult']['NextToken'])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwait for an environment to have the given version_label and to be in the green state.", "response": "def wait_for_environments(self, environment_names, health=None, status=None, version_label=None,\n include_deleted=True, use_events=True):\n \"\"\"\n Waits for an environment to have the given version_label\n and to be in the green state\n \"\"\"\n\n # turn into a list\n if not isinstance(environment_names, (list, tuple)):\n environment_names = [environment_names]\n environment_names = environment_names[:]\n\n # print some stuff\n s = \"Waiting for environment(s) \" + (\", \".join(environment_names)) + \" to\"\n if health is not None:\n s += \" have health \" + health\n else:\n s += \" have any health\"\n if version_label is not None:\n s += \" and have version \" + version_label\n if status is not None:\n s += \" and have status \" + status\n out(s)\n\n started = time()\n seen_events = list()\n\n for env_name in environment_names:\n (events, next_token) = self.describe_events(env_name, start_time=datetime.now().isoformat())\n for event in events:\n seen_events.append(event)\n\n while True:\n # bail if they're all good\n if len(environment_names) == 0:\n break\n\n # wait\n sleep(10)\n\n # # get the env\n environments = self.ebs.describe_environments(\n application_name=self.app_name,\n environment_names=environment_names,\n include_deleted=include_deleted)\n\n environments = environments['DescribeEnvironmentsResponse']['DescribeEnvironmentsResult']['Environments']\n if len(environments) <= 0:\n raise Exception(\"Couldn't find any environments\")\n\n # loop through and wait\n for env in environments[:]:\n env_name = env['EnvironmentName']\n\n # the message\n msg = \"Environment \" + env_name + \" is \" + str(env['Health'])\n if version_label is not None:\n msg = msg + \" and has version \" + str(env['VersionLabel'])\n if status is not None:\n msg = msg + \" and has status \" + str(env['Status'])\n\n # what we're doing\n good_to_go = True\n if health is not None:\n good_to_go = good_to_go and str(env['Health']) == health\n if status is not None:\n good_to_go = good_to_go and str(env['Status']) == status\n if version_label is not None:\n good_to_go = good_to_go and str(env['VersionLabel']) == version_label\n\n # allow a certain number of Red samples before failing\n if env['Status'] == 'Ready' and env['Health'] == 'Red':\n if 'RedCount' not in env:\n env['RedCount'] = 0\n\n env['RedCount'] += 1\n if env['RedCount'] > MAX_RED_SAMPLES:\n out('Deploy failed')\n raise Exception('Ready and red')\n\n # log it\n if good_to_go:\n out(msg + \" ... done\")\n environment_names.remove(env_name)\n else:\n out(msg + \" ... waiting\")\n\n # log events\n (events, next_token) = self.describe_events(env_name, start_time=datetime.now().isoformat())\n for event in events:\n if event not in seen_events:\n out(\"[\"+event['Severity']+\"] \"+event['Message'])\n seen_events.append(event)\n\n # check the time\n elapsed = time() - started\n if elapsed > self.wait_time_secs:\n message = \"Wait time for environment(s) {environments} to be {health} expired\".format(\n environments=\" and \".join(environment_names), health=(health or \"Green\")\n )\n raise Exception(message)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_arguments(parser):\n parser.add_argument('-o', '--old-environment', help='Old environment name', required=True)\n parser.add_argument('-n', '--new-environment', help='New environment name', required=True)", "response": "Adds arguments for the swap urls command\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nswap old and new URLs.", "response": "def execute(helper, config, args):\n \"\"\"\n Swaps old and new URLs.\n If old_environment was active, new_environment will become the active environment\n \"\"\"\n old_env_name = args.old_environment\n new_env_name = args.new_environment\n\n # swap C-Names\n out(\"Assuming that {} is the currently active environment...\".format(old_env_name))\n out(\"Swapping environment cnames: {} will become active, {} will become inactive.\".format(new_env_name,\n old_env_name))\n helper.swap_environment_cnames(old_env_name, new_env_name)\n helper.wait_for_environments([old_env_name, new_env_name], status='Ready', include_deleted=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef execute(helper, config, args):\n env = parse_env_config(config, args.environment)\n option_settings = env.get('option_settings', {})\n settings = parse_option_settings(option_settings)\n for setting in settings:\n out(str(setting))", "response": "dump command dumps things"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cached_property(f):\n @property\n @functools.wraps(f)\n def wrapped(self, name=f.__name__):\n try:\n cache = self.__cache__\n except AttributeError:\n self.__cache__ = cache = {}\n try:\n return cache[name]\n except KeyError:\n cache[name] = rv = f(self)\n return rv\n return wrapped", "response": "A property decorator that caches the result of a function."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprints the current state of the current application.", "response": "def execute(helper, config, args):\n \"\"\"\n Lists environments\n \"\"\"\n envs = config.get('app', {}).get('environments', [])\n out(\"Parsed environments:\")\n for name, conf in list(envs.items()):\n out('\\t'+name)\n envs = helper.get_environments()\n out(\"Deployed environments:\")\n for env in envs:\n if env['Status'] != 'Terminated':\n out('\\t'+str(env['EnvironmentName'])+' ('+str(env['Status'])+', '+str(env['CNAME'])+')')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete an application and wait for all environments to terminate", "response": "def execute(helper, config, args):\n \"\"\"\n Deletes an environment\n \"\"\"\n helper.delete_application()\n\n # wait\n if not args.dont_wait:\n\n # get environments\n environment_names = []\n for env in helper.get_environments():\n environment_names.append(env['EnvironmentName'])\n\n # wait for them\n helper.wait_for_environments(environment_names, status='Terminated')\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef execute(helper, config, args):\n\n env_config = parse_env_config(config, args.environment)\n environments_to_wait_for_term = []\n environments = helper.get_environments()\n\n for env in environments:\n if env['EnvironmentName'] == args.environment:\n if env['Status'] != 'Ready':\n out(\"Unable to delete \" + env['EnvironmentName']\n + \" because it's not in status Ready (\"\n + env['Status'] + \")\")\n else:\n out(\"Deleting environment: \"+env['EnvironmentName'])\n helper.delete_environment(env['EnvironmentName'])\n environments_to_wait_for_term.append(env['EnvironmentName'])\n\n if not args.dont_wait:\n helper.wait_for_environments(environments_to_wait_for_term,\n status='Terminated',\n include_deleted=True)\n\n out(\"Environment deleted\")\n return 0", "response": "Deletes an environment and waits for the termination of the environment"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef execute(helper, config, args):\n version_label = args.version_label\n env_config = parse_env_config(config, args.environment)\n env_name = args.environment\n\n # upload or build an archive\n version_label = upload_application_archive(\n helper, env_config, archive=args.archive,\n directory=args.directory, version_label=version_label)\n\n import datetime\n start_time = datetime.datetime.utcnow().isoformat() + 'Z'\n # deploy it\n helper.deploy_version(env_name, version_label)\n\n # wait\n if not args.dont_wait:\n helper.wait_for_environments(env_name, status='Ready',\n version_label=version_label,\n include_deleted=False)\n\n # update it\n env = parse_env_config(config, env_name)\n option_settings = parse_option_settings(env.get('option_settings', {}))\n helper.update_environment(env_name,\n description=env.get('description', None),\n option_settings=option_settings,\n tier_type=env.get('tier_type'),\n tier_name=env.get('tier_name'),\n tier_version=env.get('tier_version'))\n\n # wait\n if not args.dont_wait:\n helper.wait_for_environments(env_name, health='Green',\n status='Ready', version_label=version_label,\n include_deleted=False)\n\n events = helper.ebs.describe_events(start_time=start_time, environment_name=env_name)\n import json\n if args.log_events_to_file:\n with open('ebs_events.json', 'w+') as f:\n json.dump(events, f)\n\n # delete unused\n helper.delete_unused_versions(versions_to_keep=int(get(config, 'app.versions_to_keep', 10)))", "response": "Deploy or upload an application to an environment\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd the arguments for the deploy command", "response": "def add_arguments(parser):\n \"\"\"\n adds arguments for the deploy command\n \"\"\"\n parser.add_argument('-e', '--environment', help='Environment name', required=True)\n parser.add_argument('-w', '--dont-wait', help='Skip waiting for the init to finish', action='store_true')\n parser.add_argument('-l', '--version-label', help='Version label', required=False)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeploying to an environment", "response": "def execute(helper, config, args):\n \"\"\"\n Deploys to an environment\n \"\"\"\n env_config = parse_env_config(config, args.environment)\n cname_prefix = env_config.get('cname_prefix', None)\n env_name = args.environment\n\n # change version\n if args.version_label:\n helper.deploy_version(env_name, args.version_label)\n if not args.dont_wait:\n helper.wait_for_environments(env_name, status='Ready', version_label=args.version_label)\n\n # update it\n env = parse_env_config(config, env_name)\n option_settings = parse_option_settings(env.get('option_settings', {}))\n helper.update_environment(env_name,\n description=env.get('description', None),\n option_settings=option_settings,\n tier_type=env.get('tier_type'),\n tier_name=env.get('tier_name'),\n tier_version=env.get('tier_version'))\n\n # wait\n if not args.dont_wait:\n helper.wait_for_environments(env_name, health='Green', status='Ready', version_label=args.version_label)\n\n # delete unused\n helper.delete_unused_versions(versions_to_keep=int( get(config, 'app.versions_to_keep', 10) ))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\njoining a Hangul letter from Korean phonemes.", "response": "def join_phonemes(*args):\n \"\"\"Joins a Hangul letter from Korean phonemes.\"\"\"\n # Normalize arguments as onset, nucleus, coda.\n if len(args) == 1:\n # tuple of (onset, nucleus[, coda])\n args = args[0]\n if len(args) == 2:\n args += (CODAS[0],)\n try:\n onset, nucleus, coda = args\n except ValueError:\n raise TypeError('join_phonemes() takes at most 3 arguments')\n offset = (\n (ONSETS.index(onset) * NUM_NUCLEUSES + NUCLEUSES.index(nucleus)) *\n NUM_CODAS + CODAS.index(coda)\n )\n return unichr(FIRST_HANGUL_OFFSET + offset)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsplit Korean phonemes from a single letter.", "response": "def split_phonemes(letter, onset=True, nucleus=True, coda=True):\n \"\"\"Splits Korean phonemes as known as \"\uc790\uc18c\" from a Hangul letter.\n\n :returns: (onset, nucleus, coda)\n :raises ValueError: `letter` is not a Hangul single letter.\n\n \"\"\"\n if len(letter) != 1 or not is_hangul(letter):\n raise ValueError('Not Hangul letter: %r' % letter)\n offset = ord(letter) - FIRST_HANGUL_OFFSET\n phonemes = [None] * 3\n if onset:\n phonemes[0] = ONSETS[offset // (NUM_NUCLEUSES * NUM_CODAS)]\n if nucleus:\n phonemes[1] = NUCLEUSES[(offset // NUM_CODAS) % NUM_NUCLEUSES]\n if coda:\n phonemes[2] = CODAS[offset % NUM_CODAS]\n return tuple(phonemes)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef combine_words(word1, word2):\n if word1 and word2 and is_consonant(word2[0]):\n onset, nucleus, coda = split_phonemes(word1[-1])\n if not coda:\n glue = join_phonemes(onset, nucleus, word2[0])\n return word1[:-1] + glue + word2[1:]\n return word1 + word2", "response": "Combines two words into one single word."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a regex pattern which can be used to index the given particles.", "response": "def index_particles(particles):\n \"\"\"Indexes :class:`Particle` objects. It returns a regex pattern which\n matches to any particle morphs and a dictionary indexes the given particles\n by regex groups.\n \"\"\"\n patterns, indices = [], {}\n for x, p in enumerate(particles):\n group = u'_%d' % x\n indices[group] = x\n patterns.append(u'(?P<%s>%s)' % (group, p.regex_pattern()))\n pattern = re.compile(u'|'.join(patterns))\n return pattern, indices"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwait for an environment to be healthy", "response": "def execute(helper, config, args):\n \"\"\"\n Waits for an environment to be healthy\n \"\"\"\n helper.wait_for_environments(args.environment, health=args.health)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef execute(helper, config, args):\n versions = helper.get_versions()\n out(\"Deployed versions:\")\n for version in versions:\n out(version)", "response": "List all versions of the current node"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd command line arguments for the init command", "response": "def add_arguments(parser):\n \"\"\"\n Args for the init command\n \"\"\"\n parser.add_argument('-e', '--environment', help='Environment name', required=False, nargs='+')\n parser.add_argument('-w', '--dont-wait', help='Skip waiting for the app to be deleted', action='store_true')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef execute(helper, config, args):\n environment_name = args.environment\n\n (events, next_token) = helper.describe_events(environment_name, start_time=datetime.now().isoformat())\n\n # swap C-Names\n for event in events:\n print((\"[\"+event['Severity']+\"] \"+event['Message']))", "response": "describe recent events for an environment"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrebuilds an environment and waits for all environments to be ready.", "response": "def execute(helper, config, args):\n \"\"\"\n Rebuilds an environment\n \"\"\"\n env_config = parse_env_config(config, args.environment)\n helper.rebuild_environment(args.environment)\n\n # wait\n if not args.dont_wait:\n helper.wait_for_environments(args.environment, health='Green', status='Ready')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates all reasonable tolerant particle morphs.", "response": "def generate_tolerances(morph1, morph2):\n \"\"\"Generates all reasonable tolerant particle morphs::\n\n >>> set(generate_tolerances(u'\uc774', u'\uac00'))\n set([u'\uc774(\uac00)', u'(\uc774)\uac00', u'\uac00(\uc774)', u'(\uac00)\uc774'])\n >>> set(generate_tolerances(u'\uc774\uba74', u'\uba74'))\n set([u'(\uc774)\uba74'])\n\n \"\"\"\n if morph1 == morph2:\n # Tolerance not required.\n return\n if not (morph1 and morph2):\n # Null allomorph exists.\n yield u'(%s)' % (morph1 or morph2)\n return\n len1, len2 = len(morph1), len(morph2)\n if len1 != len2:\n longer, shorter = (morph1, morph2) if len1 > len2 else (morph2, morph1)\n if longer.endswith(shorter):\n # Longer morph ends with shorter morph.\n yield u'(%s)%s' % (longer[:-len(shorter)], shorter)\n return\n # Find common suffix between two morphs.\n for x, (let1, let2) in enumerate(zip(reversed(morph1), reversed(morph2))):\n if let1 != let2:\n break\n if x:\n # They share the common suffix.\n x1, x2 = len(morph1) - x, len(morph2) - x\n common_suffix = morph1[x1:]\n morph1, morph2 = morph1[:x1], morph2[:x2]\n else:\n # No similarity with each other.\n common_suffix = ''\n for morph1, morph2 in [(morph1, morph2), (morph2, morph1)]:\n yield u'%s(%s)%s' % (morph1, morph2, common_suffix)\n yield u'(%s)%s%s' % (morph1, morph2, common_suffix)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nresolve a tolerance style of the given particle morph.", "response": "def parse_tolerance_style(style, registry=None):\n \"\"\"Resolves a tolerance style of the given tolerant particle morph::\n\n >>> parse_tolerance_style(u'\uc740(\ub294)')\n 0\n >>> parse_tolerance_style(u'(\uc740)\ub294')\n 1\n >>> parse_tolerance_style(OPTIONAL_MORPH2_AND_MORPH1)\n 3\n\n \"\"\"\n if isinstance(style, integer_types):\n return style\n if registry is None:\n from . import registry\n particle = registry.parse(style)\n if len(particle.tolerances) != 4:\n raise ValueError('Set tolerance style by general allomorphic particle')\n return particle.tolerances.index(style)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist available solution stacks", "response": "def execute(helper, config, args):\n \"\"\"\n Lists solution stacks\n \"\"\"\n out(\"Available solution stacks\")\n for stack in helper.list_available_solution_stacks():\n out(\" \"+str(stack))\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd the arguments for the deploy command to the argument parser", "response": "def add_arguments(parser):\n \"\"\"\n adds arguments for the deploy command\n \"\"\"\n parser.add_argument('-e', '--environment', help='Environment name', required=True)\n parser.add_argument('-w', '--dont-wait', help='Skip waiting', action='store_true')\n parser.add_argument('-a', '--archive', help='Archive file', required=False)\n parser.add_argument('-d', '--directory', help='Directory', required=False)\n parser.add_argument('-l', '--version-label', help='Version label', required=False)\n parser.add_argument('-t', '--termination-delay',\n help='Delay termination of old environment by this number of seconds',\n type=int, required=False)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef execute(helper, config, args):\n version_label = args.version_label\n archive = args.archive\n\n # get the environment configuration\n env_config = parse_env_config(config, args.environment)\n option_settings = parse_option_settings(env_config.get('option_settings', {}))\n cname_prefix = env_config.get('cname_prefix', None)\n\n # no zdt for anything but web server\n tier_name = env_config.get('tier_name', 'WebServer')\n if tier_name != 'WebServer':\n raise Exception(\n \"Only able to do zero downtime deployments for \"\n \"WebServer tiers, can't do them for %s\" % (tier_name, ))\n\n # find an available environment name\n out(\"Determining new environment name...\")\n new_env_name = None\n if not helper.environment_exists(args.environment):\n new_env_name = args.environment\n else:\n for i in range(10):\n temp_env_name = args.environment + '-' + str(i)\n if not helper.environment_exists(temp_env_name):\n new_env_name = temp_env_name\n break\n if new_env_name is None:\n raise Exception(\"Unable to determine new environment name\")\n out(\"New environment name will be \" + new_env_name)\n\n # find an available cname name\n out(\"Determining new environment cname...\")\n new_env_cname = None\n for i in range(10):\n temp_cname = cname_prefix + '-' + str(i)\n if not helper.environment_name_for_cname(temp_cname):\n new_env_cname = temp_cname\n break\n if new_env_cname is None:\n raise Exception(\"Unable to determine new environment cname\")\n out(\"New environment cname will be \" + new_env_cname)\n\n # upload or build an archive\n version_label = upload_application_archive(\n helper, env_config, archive=args.archive, directory=args.directory, version_label=version_label)\n\n # create the new environment\n helper.create_environment(new_env_name,\n solution_stack_name=env_config.get('solution_stack_name'),\n cname_prefix=new_env_cname,\n description=env_config.get('description', None),\n option_settings=option_settings,\n version_label=version_label,\n tier_name=tier_name,\n tier_type=env_config.get('tier_type'),\n tier_version=env_config.get('tier_version'))\n helper.wait_for_environments(new_env_name, status='Ready', health='Green', include_deleted=False)\n\n # find existing environment name\n old_env_name = helper.environment_name_for_cname(cname_prefix)\n if old_env_name is None:\n raise Exception(\"Unable to find current environment with cname: \" + cname_prefix)\n out(\"Current environment name is \" + old_env_name)\n\n # swap C-Names\n out(\"Swapping environment cnames\")\n helper.swap_environment_cnames(old_env_name, new_env_name)\n helper.wait_for_environments([old_env_name, new_env_name], status='Ready', include_deleted=False)\n\n # delete the old environment\n if args.termination_delay:\n out(\"Termination delay specified, sleeping for {} seconds...\".format(args.termination_delay))\n time.sleep(args.termination_delay)\n out(\"Deleting old environment {}\".format(old_env_name))\n helper.delete_environment(old_env_name)\n\n # delete unused\n helper.delete_unused_versions(versions_to_keep=int(get(config, 'app.versions_to_keep', 10)))", "response": "Execute the zdt deployment"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a word which removes insignificant letters at the end of the given .", "response": "def filter_only_significant(word):\n \"\"\"Gets a word which removes insignificant letters at the end of the given\n word::\n\n >>> pick_significant(u'\ub125\uc2a8(\ucf54\ub9ac\uc544)')\n \ub125\uc2a8\n >>> pick_significant(u'\uba54\uc774\ud50c\uc2a4\ud1a0\ub9ac...')\n \uba54\uc774\ud50c\uc2a4\ud1a0\ub9ac\n\n \"\"\"\n if not word:\n return word\n # Unwrap a complete parenthesis.\n if word.startswith(u'(') and word.endswith(u')'):\n return filter_only_significant(word[1:-1])\n x = len(word)\n while x > 0:\n x -= 1\n c = word[x]\n # Skip a complete parenthesis.\n if c == u')':\n m = INSIGNIFICANT_PARENTHESIS_PATTERN.search(word[:x + 1])\n if m is not None:\n x = m.start()\n continue\n # Skip unreadable characters such as punctuations.\n unicode_category = unicodedata.category(c)\n if not SIGNIFICANT_UNICODE_CATEGORY_PATTERN.match(unicode_category):\n continue\n break\n return word[:x + 1]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npick only a coda from a Hangul letter. It returns None if the letter is not Hangul.", "response": "def pick_coda_from_letter(letter):\n \"\"\"Picks only a coda from a Hangul letter. It returns ``None`` if the\n given letter is not Hangul.\n \"\"\"\n try:\n __, __, coda = \\\n split_phonemes(letter, onset=False, nucleus=False, coda=True)\n except ValueError:\n return None\n else:\n return coda"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npicks only a coda from a decimal.", "response": "def pick_coda_from_decimal(decimal):\n \"\"\"Picks only a coda from a decimal.\"\"\"\n decimal = Decimal(decimal)\n __, digits, exp = decimal.as_tuple()\n if exp < 0:\n return DIGIT_CODAS[digits[-1]]\n __, digits, exp = decimal.normalize().as_tuple()\n index = bisect_right(EXP_INDICES, exp) - 1\n if index < 0:\n return DIGIT_CODAS[digits[-1]]\n else:\n return EXP_CODAS[EXP_INDICES[index]]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfetches a deposit identifier.", "response": "def deposit_fetcher(record_uuid, data):\n \"\"\"Fetch a deposit identifier.\n\n :param record_uuid: Record UUID.\n :param data: Record content.\n :returns: A :class:`invenio_pidstore.fetchers.FetchedPID` that contains\n data['_deposit']['id'] as pid_value.\n \"\"\"\n return FetchedPID(\n provider=DepositProvider,\n pid_type=DepositProvider.pid_type,\n pid_value=str(data['_deposit']['id']),\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nminting a deposit identifier.", "response": "def deposit_minter(record_uuid, data):\n \"\"\"Mint a deposit identifier.\n\n A PID with the following characteristics is created:\n\n .. code-block:: python\n\n {\n \"object_type\": \"rec\",\n \"object_uuid\": record_uuid,\n \"pid_value\": \"\",\n \"pid_type\": \"depid\",\n }\n\n The following deposit meta information are updated:\n\n .. code-block:: python\n\n deposit['_deposit'] = {\n \"id\": \"\",\n \"status\": \"draft\",\n }\n\n :param record_uuid: Record UUID.\n :param data: Record content.\n :returns: A :class:`invenio_pidstore.models.PersistentIdentifier` object.\n \"\"\"\n provider = DepositProvider.create(\n object_type='rec',\n object_uuid=record_uuid,\n pid_value=uuid.uuid4().hex,\n )\n data['_deposit'] = {\n 'id': provider.pid.pid_value,\n 'status': 'draft',\n }\n return provider.pid"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef admin_permission_factory():\n try:\n pkg_resources.get_distribution('invenio-access')\n from invenio_access.permissions import DynamicPermission as Permission\n except pkg_resources.DistributionNotFound:\n from flask_principal import Permission\n\n return Permission(action_admin_access)", "response": "Factory for creating a permission for an admin deposit - admin - access."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating Invenio - Deposit - UI blueprint.", "response": "def create_blueprint(endpoints):\n \"\"\"Create Invenio-Deposit-UI blueprint.\n\n See: :data:`invenio_deposit.config.DEPOSIT_RECORDS_UI_ENDPOINTS`.\n\n :param endpoints: List of endpoints configuration.\n :returns: The configured blueprint.\n \"\"\"\n from invenio_records_ui.views import create_url_rule\n\n blueprint = Blueprint(\n 'invenio_deposit_ui',\n __name__,\n static_folder='../static',\n template_folder='../templates',\n url_prefix='',\n )\n\n @blueprint.errorhandler(PIDDeletedError)\n def tombstone_errorhandler(error):\n \"\"\"Render tombstone page.\"\"\"\n return render_template(\n current_app.config['DEPOSIT_UI_TOMBSTONE_TEMPLATE'],\n pid=error.pid,\n record=error.record or {},\n ), 410\n\n for endpoint, options in (endpoints or {}).items():\n options = deepcopy(options)\n options.pop('jsonschema', None)\n options.pop('schemaform', None)\n blueprint.add_url_rule(**create_url_rule(endpoint, **options))\n\n @blueprint.route('/deposit')\n @login_required\n def index():\n \"\"\"List user deposits.\"\"\"\n return render_template(current_app.config['DEPOSIT_UI_INDEX_TEMPLATE'])\n\n @blueprint.route('/deposit/new')\n @login_required\n def new():\n \"\"\"Create new deposit.\"\"\"\n deposit_type = request.values.get('type')\n return render_template(\n current_app.config['DEPOSIT_UI_NEW_TEMPLATE'],\n record={'_deposit': {'id': None}},\n jsonschema=current_deposit.jsonschemas[deposit_type],\n schemaform=current_deposit.schemaforms[deposit_type],\n )\n\n return blueprint"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndefault view method. Sends ``record_viewed`` signal and renders template.", "response": "def default_view_method(pid, record, template=None):\n \"\"\"Default view method.\n\n Sends ``record_viewed`` signal and renders template.\n \"\"\"\n record_viewed.send(\n current_app._get_current_object(),\n pid=pid,\n record=record,\n )\n\n deposit_type = request.values.get('type')\n\n return render_template(\n template,\n pid=pid,\n record=record,\n jsonschema=current_deposit.jsonschemas[deposit_type],\n schemaform=current_deposit.schemaforms[deposit_type],\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create(cls, object_type=None, object_uuid=None, **kwargs):\n assert 'pid_value' in kwargs\n kwargs.setdefault('status', cls.default_status)\n return super(DepositProvider, cls).create(\n object_type=object_type, object_uuid=object_uuid, **kwargs)", "response": "Create a new deposit identifier."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef extract_actions_from_class(record_class):\n for name in dir(record_class):\n method = getattr(record_class, name, None)\n if method and getattr(method, '__deposit_action__', False):\n yield method.__name__", "response": "Extract actions from class."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nbases permission factory that checks OAuth2 scope and can_method.", "response": "def check_oauth2_scope(can_method, *myscopes):\n \"\"\"Base permission factory that check OAuth2 scope and can_method.\n\n :param can_method: Permission check function that accept a record in input\n and return a boolean.\n :param myscopes: List of scopes required to permit the access.\n :returns: A :class:`flask_principal.Permission` factory.\n \"\"\"\n def check(record, *args, **kwargs):\n @require_api_auth()\n @require_oauth_scopes(*myscopes)\n def can(self):\n return can_method(record)\n\n return type('CheckOAuth2Scope', (), {'can': can})()\n return check"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck if a given record is indexed.", "response": "def can_elasticsearch(record):\n \"\"\"Check if a given record is indexed.\n\n :param record: A record object.\n :returns: If the record is indexed returns `True`, otherwise `False`.\n \"\"\"\n search = request._methodview.search_class()\n search = search.get_record(str(record.id))\n return search.count() == 1"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_error_handlers(blueprint):\n blueprint.errorhandler(PIDInvalidAction)(create_api_errorhandler(\n status=403, message='Invalid action'\n ))\n records_rest_error_handlers(blueprint)", "response": "Create error handlers on blueprint."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate Invenio - Deposit - REST blueprint.", "response": "def create_blueprint(endpoints):\n \"\"\"Create Invenio-Deposit-REST blueprint.\n\n See: :data:`invenio_deposit.config.DEPOSIT_REST_ENDPOINTS`.\n\n :param endpoints: List of endpoints configuration.\n :returns: The configured blueprint.\n \"\"\"\n blueprint = Blueprint(\n 'invenio_deposit_rest',\n __name__,\n url_prefix='',\n )\n create_error_handlers(blueprint)\n\n for endpoint, options in (endpoints or {}).items():\n options = deepcopy(options)\n\n if 'files_serializers' in options:\n files_serializers = options.get('files_serializers')\n files_serializers = {mime: obj_or_import_string(func)\n for mime, func in files_serializers.items()}\n del options['files_serializers']\n else:\n files_serializers = {}\n\n if 'record_serializers' in options:\n serializers = options.get('record_serializers')\n serializers = {mime: obj_or_import_string(func)\n for mime, func in serializers.items()}\n else:\n serializers = {}\n\n file_list_route = options.pop(\n 'file_list_route',\n '{0}/files'.format(options['item_route'])\n )\n file_item_route = options.pop(\n 'file_item_route',\n '{0}/files/'.format(options['item_route'])\n )\n\n options.setdefault('search_class', DepositSearch)\n search_class = obj_or_import_string(options['search_class'])\n\n # records rest endpoints will use the deposit class as record class\n options.setdefault('record_class', Deposit)\n record_class = obj_or_import_string(options['record_class'])\n\n # backward compatibility for indexer class\n options.setdefault('indexer_class', None)\n\n for rule in records_rest_url_rules(endpoint, **options):\n blueprint.add_url_rule(**rule)\n\n search_class_kwargs = {}\n if options.get('search_index'):\n search_class_kwargs['index'] = options['search_index']\n\n if options.get('search_type'):\n search_class_kwargs['doc_type'] = options['search_type']\n\n ctx = dict(\n read_permission_factory=obj_or_import_string(\n options.get('read_permission_factory_imp')\n ),\n create_permission_factory=obj_or_import_string(\n options.get('create_permission_factory_imp')\n ),\n update_permission_factory=obj_or_import_string(\n options.get('update_permission_factory_imp')\n ),\n delete_permission_factory=obj_or_import_string(\n options.get('delete_permission_factory_imp')\n ),\n record_class=record_class,\n search_class=partial(search_class, **search_class_kwargs),\n default_media_type=options.get('default_media_type'),\n )\n\n deposit_actions = DepositActionResource.as_view(\n DepositActionResource.view_name.format(endpoint),\n serializers=serializers,\n pid_type=options['pid_type'],\n ctx=ctx,\n )\n\n blueprint.add_url_rule(\n '{0}/actions/'.format(\n options['item_route'],\n ','.join(extract_actions_from_class(record_class)),\n ),\n view_func=deposit_actions,\n methods=['POST'],\n )\n\n deposit_files = DepositFilesResource.as_view(\n DepositFilesResource.view_name.format(endpoint),\n serializers=files_serializers,\n pid_type=options['pid_type'],\n ctx=ctx,\n )\n\n blueprint.add_url_rule(\n file_list_route,\n view_func=deposit_files,\n methods=['GET', 'POST', 'PUT'],\n )\n\n deposit_file = DepositFileResource.as_view(\n DepositFileResource.view_name.format(endpoint),\n serializers=files_serializers,\n pid_type=options['pid_type'],\n ctx=ctx,\n )\n\n blueprint.add_url_rule(\n file_item_route,\n view_func=deposit_file,\n methods=['GET', 'PUT', 'DELETE'],\n )\n return blueprint"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef post(self, pid, record, action):\n record = getattr(record, action)(pid=pid)\n\n db.session.commit()\n # Refresh the PID and record metadata\n db.session.refresh(pid)\n db.session.refresh(record.model)\n post_action.send(current_app._get_current_object(), action=action,\n pid=pid, deposit=record)\n response = self.make_response(pid, record,\n 202 if action == 'publish' else 201)\n endpoint = '.{0}_item'.format(pid.pid_type)\n location = url_for(endpoint, pid_value=pid.pid_value, _external=True)\n response.headers.extend(dict(Location=location))\n return response", "response": "Handle deposit action.\n\n After the action is executed, a\n :class:`invenio_deposit.signals.post_action` signal is sent.\n\n Permission required: `update_permission_factory`.\n\n :param pid: Pid object (from url).\n :param record: Record object resolved from the pid.\n :param action: The action to execute."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get(self, pid, record):\n return self.make_response(obj=record.files, pid=pid, record=record)", "response": "Get files.\n\n Permission required: `read_permission_factory`.\n\n :param pid: Pid object (from url).\n :param record: Record object resolved from the pid.\n :returns: The files."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nhandle POST deposit files. Permission required: `update_permission_factory`. :param pid: Pid object (from url). :param record: Record object resolved from the pid.", "response": "def post(self, pid, record):\n \"\"\"Handle POST deposit files.\n\n Permission required: `update_permission_factory`.\n\n :param pid: Pid object (from url).\n :param record: Record object resolved from the pid.\n \"\"\"\n # load the file\n uploaded_file = request.files['file']\n # file name\n key = secure_filename(\n request.form.get('name') or uploaded_file.filename\n )\n # check if already exists a file with this name\n if key in record.files:\n raise FileAlreadyExists()\n # add it to the deposit\n record.files[key] = uploaded_file.stream\n record.commit()\n db.session.commit()\n return self.make_response(\n obj=record.files[key].obj, pid=pid, record=record, status=201)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef put(self, pid, record):\n try:\n ids = [data['id'] for data in json.loads(\n request.data.decode('utf-8'))]\n except KeyError:\n raise WrongFile()\n\n record.files.sort_by(*ids)\n record.commit()\n db.session.commit()\n return self.make_response(obj=record.files, pid=pid, record=record)", "response": "Handle the sort of the files through the PUT deposit files."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget file. Permission required: `read_permission_factory`. :param pid: Pid object (from url). :param record: Record object resolved from the pid. :param key: Unique identifier for the file in the deposit. :param version_id: File version. Optional. If no version is provided, the last version is retrieved. :returns: the file content.", "response": "def get(self, pid, record, key, version_id, **kwargs):\n \"\"\"Get file.\n\n Permission required: `read_permission_factory`.\n\n :param pid: Pid object (from url).\n :param record: Record object resolved from the pid.\n :param key: Unique identifier for the file in the deposit.\n :param version_id: File version. Optional. If no version is provided,\n the last version is retrieved.\n :returns: the file content.\n \"\"\"\n try:\n obj = record.files[str(key)].get_version(version_id=version_id)\n return self.make_response(\n obj=obj or abort(404), pid=pid, record=record)\n except KeyError:\n abort(404)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef put(self, pid, record, key):\n try:\n data = json.loads(request.data.decode('utf-8'))\n new_key = data['filename']\n except KeyError:\n raise WrongFile()\n new_key_secure = secure_filename(new_key)\n if not new_key_secure or new_key != new_key_secure:\n raise WrongFile()\n try:\n obj = record.files.rename(str(key), new_key_secure)\n except KeyError:\n abort(404)\n record.commit()\n db.session.commit()\n return self.make_response(obj=obj, pid=pid, record=record)", "response": "Handle the file rename through the PUT deposit file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling DELETE deposit file.", "response": "def delete(self, pid, record, key):\n \"\"\"Handle DELETE deposit file.\n\n Permission required: `update_permission_factory`.\n\n :param pid: Pid object (from url).\n :param record: Record object resolved from the pid.\n :param key: Unique identifier for the file in the deposit.\n \"\"\"\n try:\n del record.files[str(key)]\n record.commit()\n db.session.commit()\n return make_response('', 204)\n except KeyError:\n abort(404, 'The specified object does not exist or has already '\n 'been deleted.')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload records from file.", "response": "def records():\n \"\"\"Load records.\"\"\"\n import pkg_resources\n from dojson.contrib.marc21 import marc21\n from dojson.contrib.marc21.utils import create_record, split_blob\n from flask_login import login_user, logout_user\n from invenio_accounts.models import User\n from invenio_deposit.api import Deposit\n\n users = User.query.all()\n\n # pkg resources the demodata\n data_path = pkg_resources.resource_filename(\n 'invenio_records', 'data/marc21/bibliographic.xml'\n )\n with open(data_path) as source:\n with current_app.test_request_context():\n indexer = RecordIndexer()\n with db.session.begin_nested():\n for index, data in enumerate(split_blob(source.read()),\n start=1):\n login_user(users[index % len(users)])\n # do translate\n record = marc21.do(create_record(data))\n # create record\n indexer.index(Deposit.create(record))\n logout_user()\n db.session.commit()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nloading deposit JSON schemas.", "response": "def jsonschemas(self):\n \"\"\"Load deposit JSON schemas.\"\"\"\n _jsonschemas = {\n k: v['jsonschema']\n for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()\n if 'jsonschema' in v\n }\n return defaultdict(\n lambda: self.app.config['DEPOSIT_DEFAULT_JSONSCHEMA'], _jsonschemas\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nload deposit schema forms.", "response": "def schemaforms(self):\n \"\"\"Load deposit schema forms.\"\"\"\n _schemaforms = {\n k: v['schemaform']\n for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()\n if 'schemaform' in v\n }\n return defaultdict(\n lambda: self.app.config['DEPOSIT_DEFAULT_SCHEMAFORM'], _schemaforms\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_app(self, app):\n self.init_config(app)\n app.register_blueprint(ui.create_blueprint(\n app.config['DEPOSIT_RECORDS_UI_ENDPOINTS']\n ))\n app.extensions['invenio-deposit'] = _DepositState(app)\n if app.config['DEPOSIT_REGISTER_SIGNALS']:\n post_action.connect(index_deposit_after_publish, sender=app,\n weak=False)", "response": "Flask application initialization.\n\n Initialize the UI endpoints. Connect all signals if\n `DEPOSIT_REGISTER_SIGNALS` is ``True``.\n\n :param app: An instance of :class:`flask.Flask`."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninitializes the Flask application.", "response": "def init_app(self, app):\n \"\"\"Flask application initialization.\n\n Initialize the REST endpoints. Connect all signals if\n `DEPOSIT_REGISTER_SIGNALS` is True.\n\n :param app: An instance of :class:`flask.Flask`.\n \"\"\"\n self.init_config(app)\n blueprint = rest.create_blueprint(\n app.config['DEPOSIT_REST_ENDPOINTS']\n )\n\n # FIXME: This is a temporary fix. This means that\n # invenio-records-rest's endpoint_prefixes cannot be used before\n # the first request or in other processes, ex: Celery tasks.\n @app.before_first_request\n def extend_default_endpoint_prefixes():\n \"\"\"Extend redirects between PID types.\"\"\"\n endpoint_prefixes = utils.build_default_endpoint_prefixes(\n dict(app.config['DEPOSIT_REST_ENDPOINTS'])\n )\n current_records_rest = app.extensions['invenio-records-rest']\n overlap = set(endpoint_prefixes.keys()) & set(\n current_records_rest.default_endpoint_prefixes\n )\n if overlap:\n raise RuntimeError(\n 'Deposit wants to override endpoint prefixes {0}.'.format(\n ', '.join(overlap)\n )\n )\n current_records_rest.default_endpoint_prefixes.update(\n endpoint_prefixes\n )\n\n app.register_blueprint(blueprint)\n app.extensions['invenio-deposit-rest'] = _DepositState(app)\n if app.config['DEPOSIT_REGISTER_SIGNALS']:\n post_action.connect(index_deposit_after_publish, sender=app,\n weak=False)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef deposit_links_factory(pid):\n links = default_links_factory(pid)\n\n def _url(name, **kwargs):\n \"\"\"URL builder.\"\"\"\n endpoint = '.{0}_{1}'.format(\n current_records_rest.default_endpoint_prefixes[pid.pid_type],\n name,\n )\n return url_for(endpoint, pid_value=pid.pid_value, _external=True,\n **kwargs)\n\n links['files'] = _url('files')\n\n ui_endpoint = current_app.config.get('DEPOSIT_UI_ENDPOINT')\n if ui_endpoint is not None:\n links['html'] = ui_endpoint.format(\n host=request.host,\n scheme=request.scheme,\n pid_value=pid.pid_value,\n )\n\n deposit_cls = Deposit\n if 'pid_value' in request.view_args:\n deposit_cls = request.view_args['pid_value'].data[1].__class__\n\n for action in extract_actions_from_class(deposit_cls):\n links[action] = _url('actions', action=action)\n return links", "response": "Factory for record links generation."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nloads minter from PIDStore registry based on given value.", "response": "def process_minter(value):\n \"\"\"Load minter from PIDStore registry based on given value.\n\n :param value: Name of the minter.\n :returns: The minter.\n \"\"\"\n try:\n return current_pidstore.minters[value]\n except KeyError:\n raise click.BadParameter(\n 'Unknown minter {0}. Please use one of {1}.'.format(\n value, ', '.join(current_pidstore.minters.keys())\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading schema from JSONSchema registry based on given value.", "response": "def process_schema(value):\n \"\"\"Load schema from JSONSchema registry based on given value.\n\n :param value: Schema path, relative to the directory when it was\n registered.\n :returns: The schema absolute path.\n \"\"\"\n schemas = current_app.extensions['invenio-jsonschemas'].schemas\n try:\n return schemas[value]\n except KeyError:\n raise click.BadParameter(\n 'Unknown schema {0}. Please use one of:\\n {1}'.format(\n value, '\\n'.join(schemas.keys())\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef json_serializer(pid, data, *args):\n if data is not None:\n response = Response(\n json.dumps(data.dumps()),\n mimetype='application/json'\n )\n else:\n response = Response(mimetype='application/json')\n return response", "response": "Builds a JSON Flask response using the given data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nserializing a object version to a dictionary.", "response": "def file_serializer(obj):\n \"\"\"Serialize a object.\n\n :param obj: A :class:`invenio_files_rest.models.ObjectVersion` instance.\n :returns: A dictionary with the fields to serialize.\n \"\"\"\n return {\n \"id\": str(obj.file_id),\n \"filename\": obj.key,\n \"filesize\": obj.file.size,\n \"checksum\": obj.file.checksum,\n }"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef json_file_response(obj=None, pid=None, record=None, status=None):\n from invenio_records_files.api import FilesIterator\n\n if isinstance(obj, FilesIterator):\n return json_files_serializer(obj, status=status)\n else:\n return json_file_serializer(obj, status=status)", "response": "Returns a Flask response with JSON data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef index_deposit_after_publish(sender, action=None, pid=None, deposit=None):\n if action == 'publish':\n _, record = deposit.fetch_published()\n index_record.delay(str(record.id))", "response": "Index the record after publishing."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef has_status(method=None, status='draft'):\n if method is None:\n return partial(has_status, status=status)\n\n @wraps(method)\n def wrapper(self, *args, **kwargs):\n \"\"\"Check current deposit status.\"\"\"\n if status != self.status:\n raise PIDInvalidAction()\n\n return method(self, *args, **kwargs)\n return wrapper", "response": "Decorator to check that the current deposit has a defined status."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\npreserves fields in deposit. :param method: Function to execute. (Default: ``None``) :param result: If `True` returns the result of method execution, otherwise `self`. (Default: ``True``) :param fields: List of fields to preserve (default: ``('_deposit',)``).", "response": "def preserve(method=None, result=True, fields=None):\n \"\"\"Preserve fields in deposit.\n\n :param method: Function to execute. (Default: ``None``)\n :param result: If `True` returns the result of method execution,\n otherwise `self`. (Default: ``True``)\n :param fields: List of fields to preserve (default: ``('_deposit',)``).\n \"\"\"\n if method is None:\n return partial(preserve, result=result, fields=fields)\n\n fields = fields or ('_deposit', )\n\n @wraps(method)\n def wrapper(self, *args, **kwargs):\n \"\"\"Check current deposit status.\"\"\"\n data = {field: self[field] for field in fields if field in self}\n result_ = method(self, *args, **kwargs)\n replace = result_ if result else self\n for field in data:\n replace[field] = data[field]\n return result_\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns an instance of deposit PID.", "response": "def pid(self):\n \"\"\"Return an instance of deposit PID.\"\"\"\n pid = self.deposit_fetcher(self.id, self)\n return PersistentIdentifier.get(pid.pid_type,\n pid.pid_value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert deposit schema to a valid record schema.", "response": "def record_schema(self):\n \"\"\"Convert deposit schema to a valid record schema.\"\"\"\n schema_path = current_jsonschemas.url_to_path(self['$schema'])\n schema_prefix = current_app.config['DEPOSIT_JSONSCHEMAS_PREFIX']\n if schema_path and schema_path.startswith(schema_prefix):\n return current_jsonschemas.path_to_url(\n schema_path[len(schema_prefix):]\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting record schema to a valid deposit schema.", "response": "def build_deposit_schema(self, record):\n \"\"\"Convert record schema to a valid deposit schema.\n\n :param record: The record used to build deposit schema.\n :returns: The absolute URL to the schema or `None`.\n \"\"\"\n schema_path = current_jsonschemas.url_to_path(record['$schema'])\n schema_prefix = current_app.config['DEPOSIT_JSONSCHEMAS_PREFIX']\n if schema_path:\n return current_jsonschemas.path_to_url(\n schema_prefix + schema_path\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a tuple with PID and published record.", "response": "def fetch_published(self):\n \"\"\"Return a tuple with PID and published record.\"\"\"\n pid_type = self['_deposit']['pid']['type']\n pid_value = self['_deposit']['pid']['value']\n\n resolver = Resolver(\n pid_type=pid_type, object_type='rec',\n getter=partial(self.published_record_class.get_record,\n with_deleted=True)\n )\n return resolver.resolve(pid_value)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmerge changes with latest published version.", "response": "def merge_with_published(self):\n \"\"\"Merge changes with latest published version.\"\"\"\n pid, first = self.fetch_published()\n lca = first.revisions[self['_deposit']['pid']['revision_id']]\n # ignore _deposit and $schema field\n args = [lca.dumps(), first.dumps(), self.dumps()]\n for arg in args:\n del arg['$schema'], arg['_deposit']\n args.append({})\n m = Merger(*args)\n try:\n m.run()\n except UnresolvedConflictsException:\n raise MergeConflict()\n return patch(m.unified_patches, lca)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef commit(self, *args, **kwargs):\n return super(Deposit, self).commit(*args, **kwargs)", "response": "Store changes on current instance in database and index it."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create(cls, data, id_=None):\n data.setdefault('$schema', current_jsonschemas.path_to_url(\n current_app.config['DEPOSIT_DEFAULT_JSONSCHEMA']\n ))\n if '_deposit' not in data:\n id_ = id_ or uuid.uuid4()\n cls.deposit_minter(id_, data)\n\n data['_deposit'].setdefault('owners', list())\n if current_user and current_user.is_authenticated:\n creator_id = int(current_user.get_id())\n\n if creator_id not in data['_deposit']['owners']:\n data['_deposit']['owners'].append(creator_id)\n\n data['_deposit']['created_by'] = creator_id\n\n return super(Deposit, cls).create(data, id_=id_)", "response": "Create a new deposit."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _process_files(self, record_id, data):\n if self.files:\n assert not self.files.bucket.locked\n self.files.bucket.locked = True\n snapshot = self.files.bucket.snapshot(lock=True)\n data['_files'] = self.files.dumps(bucket=snapshot.id)\n yield data\n db.session.add(RecordsBuckets(\n record_id=record_id, bucket_id=snapshot.id\n ))\n else:\n yield data", "response": "Add files in record during first publishing."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _publish_new(self, id_=None):\n minter = current_pidstore.minters[\n current_app.config['DEPOSIT_PID_MINTER']\n ]\n id_ = id_ or uuid.uuid4()\n record_pid = minter(id_, self)\n\n self['_deposit']['pid'] = {\n 'type': record_pid.pid_type,\n 'value': record_pid.pid_value,\n 'revision_id': 0,\n }\n\n data = dict(self.dumps())\n data['$schema'] = self.record_schema\n\n with self._process_files(id_, data):\n record = self.published_record_class.create(data, id_=id_)\n\n return record", "response": "Publish a new record."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npublish the deposit after editing.", "response": "def _publish_edited(self):\n \"\"\"Publish the deposit after for editing.\"\"\"\n record_pid, record = self.fetch_published()\n if record.revision_id == self['_deposit']['pid']['revision_id']:\n data = dict(self.dumps())\n else:\n data = self.merge_with_published()\n\n data['$schema'] = self.record_schema\n data['_deposit'] = self['_deposit']\n record = record.__class__(data, model=record.model)\n return record"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\npublish a new version of a deposit.", "response": "def publish(self, pid=None, id_=None):\n \"\"\"Publish a deposit.\n\n If it's the first time:\n\n * it calls the minter and set the following meta information inside\n the deposit:\n\n .. code-block:: python\n\n deposit['_deposit'] = {\n 'type': pid_type,\n 'value': pid_value,\n 'revision_id': 0,\n }\n\n * A dump of all information inside the deposit is done.\n\n * A snapshot of the files is done.\n\n Otherwise, published the new edited version.\n In this case, if in the mainwhile someone already published a new\n version, it'll try to merge the changes with the latest version.\n\n .. note:: no need for indexing as it calls `self.commit()`.\n\n Status required: ``'draft'``.\n\n :param pid: Force the new pid value. (Default: ``None``)\n :param id_: Force the new uuid value as deposit id. (Default: ``None``)\n :returns: Returns itself.\n \"\"\"\n pid = pid or self.pid\n\n if not pid.is_registered():\n raise PIDInvalidAction()\n\n self['_deposit']['status'] = 'published'\n\n if self['_deposit'].get('pid') is None: # First publishing\n self._publish_new(id_=id_)\n else: # Update after edit\n record = self._publish_edited()\n record.commit()\n self.commit()\n return self"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating selected keys. :param record: The record to prepare.", "response": "def _prepare_edit(self, record):\n \"\"\"Update selected keys.\n\n :param record: The record to prepare.\n \"\"\"\n data = record.dumps()\n # Keep current record revision for merging.\n data['_deposit']['pid']['revision_id'] = record.revision_id\n data['_deposit']['status'] = 'draft'\n data['$schema'] = self.build_deposit_schema(record)\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nedits the current object.", "response": "def edit(self, pid=None):\n \"\"\"Edit deposit.\n\n #. The signal :data:`invenio_records.signals.before_record_update`\n is sent before the edit execution.\n\n #. The following meta information are saved inside the deposit:\n\n .. code-block:: python\n\n deposit['_deposit']['pid'] = record.revision_id\n deposit['_deposit']['status'] = 'draft'\n deposit['$schema'] = deposit_schema_from_record_schema\n\n #. The signal :data:`invenio_records.signals.after_record_update` is\n sent after the edit execution.\n\n #. The deposit index is updated.\n\n Status required: `published`.\n\n .. note:: the process fails if the pid has status\n :attr:`invenio_pidstore.models.PIDStatus.REGISTERED`.\n\n :param pid: Force a pid object. (Default: ``None``)\n :returns: A new Deposit object.\n \"\"\"\n pid = pid or self.pid\n\n with db.session.begin_nested():\n before_record_update.send(\n current_app._get_current_object(), record=self)\n\n record_pid, record = self.fetch_published()\n assert PIDStatus.REGISTERED == record_pid.status\n assert record['_deposit'] == self['_deposit']\n\n self.model.json = self._prepare_edit(record)\n\n flag_modified(self.model, 'json')\n db.session.merge(self.model)\n\n after_record_update.send(\n current_app._get_current_object(), record=self)\n return self.__class__(self.model.json, model=self.model)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef discard(self, pid=None):\n pid = pid or self.pid\n\n with db.session.begin_nested():\n before_record_update.send(\n current_app._get_current_object(), record=self)\n\n _, record = self.fetch_published()\n self.model.json = deepcopy(record.model.json)\n self.model.json['$schema'] = self.build_deposit_schema(record)\n\n flag_modified(self.model, 'json')\n db.session.merge(self.model)\n\n after_record_update.send(\n current_app._get_current_object(), record=self)\n return self.__class__(self.model.json, model=self.model)", "response": "Discards the current record."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete deposit. Status required: ``'draft'``. :param force: Force deposit delete. (Default: ``True``) :param pid: Force pid object. (Default: ``None``) :returns: A new Deposit object.", "response": "def delete(self, force=True, pid=None):\n \"\"\"Delete deposit.\n\n Status required: ``'draft'``.\n\n :param force: Force deposit delete. (Default: ``True``)\n :param pid: Force pid object. (Default: ``None``)\n :returns: A new Deposit object.\n \"\"\"\n pid = pid or self.pid\n\n if self['_deposit'].get('pid'):\n raise PIDInvalidAction()\n if pid:\n pid.delete()\n return super(Deposit, self).delete(force=force)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nclear only drafts. Status required: ``'draft'``. Meta information inside `_deposit` are preserved.", "response": "def clear(self, *args, **kwargs):\n \"\"\"Clear only drafts.\n\n Status required: ``'draft'``.\n\n Meta information inside `_deposit` are preserved.\n \"\"\"\n super(Deposit, self).clear(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates only drafts. Status required : drafts. Meta information inside _deposit are preserved.", "response": "def update(self, *args, **kwargs):\n \"\"\"Update only drafts.\n\n Status required: ``'draft'``.\n\n Meta information inside `_deposit` are preserved.\n \"\"\"\n super(Deposit, self).update(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef patch(self, *args, **kwargs):\n return super(Deposit, self).patch(*args, **kwargs)", "response": "Patch only drafts.\n Status required."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef files(self):\n files_ = super(Deposit, self).files\n\n if files_:\n sort_by_ = files_.sort_by\n\n def sort_by(*args, **kwargs):\n \"\"\"Only in draft state.\"\"\"\n if 'draft' != self.status:\n raise PIDInvalidAction()\n return sort_by_(*args, **kwargs)\n\n files_.sort_by = sort_by\n\n return files_", "response": "List of files inside the deposit."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert a reStructuredText into its node", "response": "def rst2node(doc_name, data):\n \"\"\"Converts a reStructuredText into its node\n \"\"\"\n if not data:\n return\n parser = docutils.parsers.rst.Parser()\n document = docutils.utils.new_document('<%s>' % doc_name)\n document.settings = docutils.frontend.OptionParser().get_default_values()\n document.settings.tab_width = 4\n document.settings.pep_references = False\n document.settings.rfc_references = False\n document.settings.env = Env()\n parser.parse(data, document)\n if len(document.children) == 1:\n return document.children[0]\n else:\n par = docutils.nodes.paragraph()\n for child in document.children:\n par += child\n return par"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef setup(app):\n if 'http' not in app.domains:\n httpdomain.setup(app)\n\n app.add_directive('autopyramid', RouteDirective)", "response": "Hook the directives when Sphinx asks for it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _parse_response(self, response):\n if not self._raise_errors:\n return response\n\n is_4xx_error = str(response.status_code)[0] == '4'\n is_5xx_error = str(response.status_code)[0] == '5'\n content = response.content\n\n if response.status_code == 403:\n raise AuthenticationError(content)\n elif is_4xx_error:\n raise APIError(content)\n elif is_5xx_error:\n raise ServerError(content)\n\n return response", "response": "Parses the response and raises appropriate errors if raise_errors is set to True."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _api_request(self, endpoint, http_method, *args, **kwargs):\n logger.debug(' > Sending API request to endpoint: %s' % endpoint)\n\n auth = self._build_http_auth()\n\n headers = self._build_request_headers(kwargs.get('headers'))\n logger.debug('\\theaders: %s' % headers)\n\n path = self._build_request_path(endpoint)\n logger.debug('\\tpath: %s' % path)\n\n data = self._build_payload(kwargs.get('payload'))\n if not data:\n data = kwargs.get('data')\n logger.debug('\\tdata: %s' % data)\n\n req_kw = dict(\n auth=auth,\n headers=headers,\n timeout=kwargs.get('timeout', self.DEFAULT_TIMEOUT)\n )\n\n # do some error handling\n if (http_method == self.HTTP_POST):\n if (data):\n r = requests.post(path, data=data, **req_kw)\n else:\n r = requests.post(path, **req_kw)\n elif http_method == self.HTTP_PUT:\n if (data):\n r = requests.put(path, data=data, **req_kw)\n else:\n r = requests.put(path, **req_kw)\n elif http_method == self.HTTP_DELETE:\n r = requests.delete(path, **req_kw)\n else:\n r = requests.get(path, **req_kw)\n\n logger.debug('\\tresponse code:%s' % r.status_code)\n try:\n logger.debug('\\tresponse: %s' % r.json())\n except:\n logger.debug('\\tresponse: %s' % r.content)\n\n return self._parse_response(r)", "response": "Private method for making API requests"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_log(self, log_id, timeout=None):\n return self._api_request(\n self.GET_LOG_ENDPOINT % log_id,\n self.HTTP_GET,\n timeout=timeout\n )", "response": "Get a specific log entry"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting log events for a specific log entry", "response": "def get_log_events(self, log_id, timeout=None):\n \"\"\" API call to get a specific log entry \"\"\"\n return self._api_request(\n self.GET_LOG_EVENTS_ENDPOINT % log_id,\n self.HTTP_GET,\n timeout=timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a list of templates", "response": "def templates(self, timeout=None):\n \"\"\" API call to get a list of templates \"\"\"\n return self._api_request(\n self.TEMPLATES_ENDPOINT,\n self.HTTP_GET,\n timeout=timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_new_version(\n self,\n name,\n subject,\n text='',\n template_id=None,\n html=None,\n locale=None,\n timeout=None\n ):\n \"\"\" API call to create a new version of a template \"\"\"\n if(html):\n payload = {\n 'name': name,\n 'subject': subject,\n 'html': html,\n 'text': text\n }\n else:\n payload = {\n 'name': name,\n 'subject': subject,\n 'text': text\n }\n\n if locale:\n url = self.TEMPLATES_SPECIFIC_LOCALE_VERSIONS_ENDPOINT % (\n template_id,\n locale\n )\n else:\n url = self.TEMPLATES_NEW_VERSION_ENDPOINT % template_id\n\n return self._api_request(\n url,\n self.HTTP_POST,\n payload=payload,\n timeout=timeout\n )", "response": "This method creates a new version of a template."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update_template_version(\n self,\n name,\n subject,\n template_id,\n version_id,\n text='',\n html=None,\n timeout=None\n ):\n \"\"\" API call to update a template version \"\"\"\n if(html):\n payload = {\n 'name': name,\n 'subject': subject,\n 'html': html,\n 'text': text\n }\n else:\n payload = {\n 'name': name,\n 'subject': subject,\n 'text': text\n }\n\n return self._api_request(\n self.TEMPLATES_VERSION_ENDPOINT % (template_id, version_id),\n self.HTTP_PUT,\n payload=payload,\n timeout=timeout\n )", "response": "Update a template version"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets list of snippets from the API", "response": "def snippets(self, timeout=None):\n \"\"\" API call to get list of snippets \"\"\"\n return self._api_request(\n self.SNIPPETS_ENDPOINT,\n self.HTTP_GET,\n timeout=timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_snippet(self, snippet_id, timeout=None):\n return self._api_request(\n self.SNIPPET_ENDPOINT % (snippet_id),\n self.HTTP_GET,\n timeout=timeout\n )", "response": "Get a specific Snippet"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_snippet(self, name, body, timeout=None):\n payload = {\n 'name': name,\n 'body': body\n }\n return self._api_request(\n self.SNIPPETS_ENDPOINT,\n self.HTTP_POST,\n payload=payload,\n timeout=timeout\n )", "response": "API call to create a Snippet"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmaking a dictionary with filename and base64 file data", "response": "def _make_file_dict(self, f):\n \"\"\"Make a dictionary with filename and base64 file data\"\"\"\n if isinstance(f, dict):\n file_obj = f['file']\n if 'filename' in f:\n file_name = f['filename']\n else:\n file_name = file_obj.name\n else:\n file_obj = f\n file_name = f.name\n\n b64_data = base64.b64encode(file_obj.read())\n return {\n 'id': file_name,\n 'data': b64_data.decode() if six.PY3 else b64_data,\n }"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsend an email to the specified recipient.", "response": "def send(\n self,\n email_id,\n recipient,\n email_data=None,\n sender=None,\n cc=None,\n bcc=None,\n tags=[],\n headers={},\n esp_account=None,\n locale=None,\n email_version_name=None,\n inline=None,\n files=[],\n timeout=None\n ):\n \"\"\" API call to send an email \"\"\"\n if not email_data:\n email_data = {}\n\n # for backwards compatibility, will be removed\n if isinstance(recipient, string_types):\n warnings.warn(\n \"Passing email directly for recipient is deprecated\",\n DeprecationWarning)\n recipient = {'address': recipient}\n\n payload = {\n 'email_id': email_id,\n 'recipient': recipient,\n 'email_data': email_data\n }\n\n if sender:\n payload['sender'] = sender\n if cc:\n if not type(cc) == list:\n logger.error(\n 'kwarg cc must be type(list), got %s' % type(cc))\n payload['cc'] = cc\n if bcc:\n if not type(bcc) == list:\n logger.error(\n 'kwarg bcc must be type(list), got %s' % type(bcc))\n payload['bcc'] = bcc\n\n if tags:\n if not type(tags) == list:\n logger.error(\n 'kwarg tags must be type(list), got %s' % (type(tags)))\n payload['tags'] = tags\n\n if headers:\n if not type(headers) == dict:\n logger.error(\n 'kwarg headers must be type(dict), got %s' % (\n type(headers)\n )\n )\n payload['headers'] = headers\n\n if esp_account:\n if not isinstance(esp_account, string_types):\n logger.error(\n 'kwarg esp_account must be a string, got %s' % (\n type(esp_account)\n )\n )\n payload['esp_account'] = esp_account\n\n if locale:\n if not isinstance(locale, string_types):\n logger.error(\n 'kwarg locale must be a string, got %s' % (type(locale))\n )\n payload['locale'] = locale\n\n if email_version_name:\n if not isinstance(email_version_name, string_types):\n logger.error(\n 'kwarg email_version_name must be a string, got %s' % (\n type(email_version_name)))\n payload['version_name'] = email_version_name\n\n if inline:\n payload['inline'] = self._make_file_dict(inline)\n\n if files:\n payload['files'] = [self._make_file_dict(f) for f in files]\n\n return self._api_request(\n self.SEND_ENDPOINT,\n self.HTTP_POST,\n payload=payload,\n timeout=timeout\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _api_request(self, endpoint, http_method, *args, **kwargs):\n logger.debug(' > Queing batch api request for endpoint: %s' % endpoint)\n\n path = self._build_request_path(endpoint, absolute=False)\n logger.debug('\\tpath: %s' % path)\n\n data = None\n if 'payload' in kwargs:\n data = kwargs['payload']\n logger.debug('\\tdata: %s' % data)\n\n command = {\n \"path\": path,\n \"method\": http_method\n }\n if data:\n command['body'] = data\n\n self._commands.append(command)", "response": "Private method for api requests"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexecute all currently queued batch commands", "response": "def execute(self, timeout=None):\n \"\"\"Execute all currently queued batch commands\"\"\"\n logger.debug(' > Batch API request (length %s)' % len(self._commands))\n\n auth = self._build_http_auth()\n\n headers = self._build_request_headers()\n logger.debug('\\tbatch headers: %s' % headers)\n\n logger.debug('\\tbatch command length: %s' % len(self._commands))\n\n path = self._build_request_path(self.BATCH_ENDPOINT)\n\n data = json.dumps(self._commands, cls=self._json_encoder)\n r = requests.post(\n path,\n auth=auth,\n headers=headers,\n data=data,\n timeout=(self.DEFAULT_TIMEOUT if timeout is None else timeout)\n )\n\n self._commands = []\n\n logger.debug('\\tresponse code:%s' % r.status_code)\n try:\n logger.debug('\\tresponse: %s' % r.json())\n except:\n logger.debug('\\tresponse: %s' % r.content)\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_group_tabs(self):\n if self.tab_group is None:\n raise ImproperlyConfigured(\n \"%s requires a definition of 'tab_group'\" %\n self.__class__.__name__)\n group_members = [t for t in self._registry if t.tab_group == self.tab_group]\n return [t() for t in group_members]", "response": "Return all other tabs that are members of the tab s tab_group."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprocessing and prepares tabs.", "response": "def _process_tabs(self, tabs, current_tab, group_current_tab):\n \"\"\"\n Process and prepare tabs.\n\n This includes steps like updating references to the current tab,\n filtering out hidden tabs, sorting tabs etc...\n\n Args:\n tabs:\n The list of tabs to process.\n current_tab:\n The reference to the currently loaded tab.\n group_current_tab:\n The reference to the active tab in the current tab group. For\n parent tabs, this is different than for the current tab group.\n\n Returns:\n Processed list of tabs. Note that the method may have side effects.\n\n \"\"\"\n # Update references to the current tab\n for t in tabs:\n t.current_tab = current_tab\n t.group_current_tab = group_current_tab\n\n # Filter out hidden tabs\n tabs = list(filter(lambda t: t.tab_visible, tabs))\n\n # Sort remaining tabs in-place\n tabs.sort(key=lambda t: t.weight)\n\n return tabs"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding tab information to the context.", "response": "def get_context_data(self, **kwargs):\n \"\"\"\n Adds tab information to context.\n\n To retrieve a list of all group tab instances, use\n ``{{ tabs }}`` in your template.\n\n The id of the current tab is added as ``current_tab_id`` to the\n template context.\n\n If the current tab has a parent tab the parent's id is added to\n the template context as ``parent_tab_id``. Instances of all tabs\n of the parent level are added as ``parent_tabs`` to the context.\n\n If the current tab has children they are added to the template\n context as ``child_tabs``.\n\n \"\"\"\n context = super(TabView, self).get_context_data(**kwargs)\n\n # Update the context with kwargs, TemplateView doesn't do this.\n context.update(kwargs)\n\n # Add tabs and \"current\" references to context\n process_tabs_kwargs = {\n 'tabs': self.get_group_tabs(),\n 'current_tab': self,\n 'group_current_tab': self,\n }\n context['tabs'] = self._process_tabs(**process_tabs_kwargs)\n context['current_tab_id'] = self.tab_id\n\n # Handle parent tabs\n if self.tab_parent is not None:\n # Verify that tab parent is valid\n if self.tab_parent not in self._registry:\n msg = '%s has no attribute _is_tab' % self.tab_parent.__class__.__name__\n raise ImproperlyConfigured(msg)\n\n # Get parent tab instance\n parent = self.tab_parent()\n\n # Add parent tabs to context\n process_parents_kwargs = {\n 'tabs': parent.get_group_tabs(),\n 'current_tab': self,\n 'group_current_tab': parent,\n }\n context['parent_tabs'] = self._process_tabs(**process_parents_kwargs)\n context['parent_tab_id'] = parent.tab_id\n\n # Handle child tabs\n if self.tab_id in self._children:\n process_children_kwargs = {\n 'tabs': [t() for t in self._children[self.tab_id]],\n 'current_tab': self,\n 'group_current_tab': None,\n }\n context['child_tabs'] = self._process_tabs(**process_children_kwargs)\n\n return context"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a string into a valid python attribute name.", "response": "def normalize_name(s):\n \"\"\"Convert a string into a valid python attribute name.\n This function is called to convert ASCII strings to something that can pass as\n python attribute name, to be used with namedtuples.\n\n >>> str(normalize_name('class'))\n 'class_'\n >>> str(normalize_name('a-name'))\n 'a_name'\n >>> str(normalize_name('a n\\u00e4me'))\n 'a_name'\n >>> str(normalize_name('Name'))\n 'Name'\n >>> str(normalize_name(''))\n '_'\n >>> str(normalize_name('1'))\n '_1'\n \"\"\"\n s = s.replace('-', '_').replace('.', '_').replace(' ', '_')\n if s in keyword.kwlist:\n return s + '_'\n s = '_'.join(slug(ss, lowercase=False) for ss in s.split('_'))\n if not s:\n s = '_'\n if s[0] not in string.ascii_letters + '_':\n s = '_' + s\n return s"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef schema(tg):\n tables = {}\n for tname, table in tg.tabledict.items():\n t = TableSpec.from_table_metadata(table)\n tables[t.name] = t\n for at in t.many_to_many.values():\n tables[at.name] = at\n\n # We must determine the order in which tables must be created!\n ordered = OrderedDict()\n i = 0\n\n # We loop through the tables repeatedly, and whenever we find one, which has all\n # referenced tables already in ordered, we move it from tables to ordered.\n while tables and i < 100:\n i += 1\n for table in list(tables.keys()):\n if all((ref[1] in ordered) or ref[1] == table for ref in tables[table].foreign_keys):\n # All referenced tables are already created (or self-referential).\n ordered[table] = tables.pop(table)\n break\n if tables: # pragma: no cover\n raise ValueError('there seem to be cyclic dependencies between the tables')\n\n return list(ordered.values())", "response": "Convert the table and column descriptions of a TableGroup into a pair of tables and reference tables."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef write(self, _force=False, _exists_ok=False, **items):\n if self.fname and self.fname.exists():\n raise ValueError('db file already exists, use force=True to overwrite')\n\n with self.connection() as db:\n for table in self.tables:\n db.execute(table.sql(translate=self.translate))\n\n db.execute('PRAGMA foreign_keys = ON;')\n db.commit()\n\n refs = defaultdict(list) # collects rows in association tables.\n for t in self.tables:\n if t.name not in items:\n continue\n rows, keys = [], []\n cols = {c.name: c for c in t.columns}\n for i, row in enumerate(items[t.name]):\n pk = row[t.primary_key[0]] \\\n if t.primary_key and len(t.primary_key) == 1 else None\n values = []\n for k, v in row.items():\n if k in t.many_to_many:\n assert pk\n at = t.many_to_many[k]\n atkey = tuple([at.name] + [c.name for c in at.columns])\n for vv in v:\n fkey, context = self.association_table_context(t, k, vv)\n refs[atkey].append((pk, fkey, context))\n else:\n col = cols[k]\n if isinstance(v, list):\n # Note: This assumes list-valued columns are of datatype string!\n v = (col.separator or ';').join(\n col.convert(vv) for vv in v)\n else:\n v = col.convert(v) if v is not None else None\n if i == 0:\n keys.append(col.name)\n values.append(v)\n rows.append(tuple(values))\n insert(db, self.translate, t.name, keys, *rows)\n\n for atkey, rows in refs.items():\n insert(db, self.translate, atkey[0], atkey[1:], *rows)\n\n db.commit()", "response": "Writes the items to the database."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef iterrows(lines_or_file, namedtuples=False, dicts=False, encoding='utf-8', **kw):\n if namedtuples and dicts:\n raise ValueError('either namedtuples or dicts can be chosen as output format')\n elif namedtuples:\n _reader = NamedTupleReader\n elif dicts:\n _reader = UnicodeDictReader\n else:\n _reader = UnicodeReader\n\n with _reader(lines_or_file, encoding=encoding, **fix_kw(kw)) as r:\n for item in r:\n yield item", "response": "A generator function that yields the rows of a n - tuple or dict."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef filter_rows_as_dict(fname, filter_, **kw):\n filter_ = DictFilter(filter_)\n rewrite(fname, filter_, **kw)\n return filter_.removed", "response": "Rewrite a dsv file with filtering the rows."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef dump_grid(grid):\n header = 'ver:%s' % dump_str(str(grid._version), version=grid._version)\n if bool(grid.metadata):\n header += ' ' + dump_meta(grid.metadata, version=grid._version)\n columns = dump_columns(grid.column, version=grid._version)\n rows = dump_rows(grid)\n return '\\n'.join([header, columns] + rows + [''])", "response": "Dump a single grid to its ZINC representation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing the given Zinc text and returns the equivalent data.", "response": "def parse(grid_str, mode=MODE_ZINC, charset='utf-8'):\n '''\n Parse the given Zinc text and return the equivalent data.\n '''\n # Decode incoming text (or python3 will whine!)\n if isinstance(grid_str, six.binary_type):\n grid_str = grid_str.decode(encoding=charset)\n\n # Split the separate grids up, the grammar definition has trouble splitting\n # them up normally. This will truncate the newline off the end of the last\n # row.\n _parse = functools.partial(parse_grid, mode=mode,\n charset=charset)\n if mode == MODE_JSON:\n if isinstance(grid_str, six.string_types):\n grid_data = json.loads(grid_str)\n else:\n grid_data = grid_str\n if isinstance(grid_data, dict):\n return _parse(grid_data)\n else:\n return list(map(_parse, grid_data))\n else:\n return list(map(_parse, GRID_SEP.split(grid_str.rstrip())))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef append(self, key, value=MARKER, replace=True):\n '''\n Append the item to the metadata.\n '''\n return self.add_item(key, value, replace=replace)", "response": "Append the item to the metadata.\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef extend(self, items, replace=True):\n '''\n Append the items to the metadata.\n '''\n if isinstance(items, dict) or isinstance(items, SortableDict):\n items = list(items.items())\n\n for (key, value) in items:\n self.append(key, value, replace=replace)", "response": "Append the items to the metadata."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef regular_polygon(cls, center, radius, n_vertices, start_angle=0, **kwargs):\n angles = (np.arange(n_vertices) * 2 * np.pi / n_vertices) + start_angle\n return cls(center + radius * np.array([np.cos(angles), np.sin(angles)]).T, **kwargs)", "response": "Construct a regular polygon."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconstruct a circle. Parameters ---------- center : array-like radius : float n_vertices : int, optional Number of points to draw. Decrease for performance, increase for appearance. kwargs Other keyword arguments are passed to the |Shape| constructor.", "response": "def circle(cls, center, radius, n_vertices=50, **kwargs):\n \"\"\"Construct a circle.\n\n Parameters\n ----------\n center : array-like\n radius : float\n n_vertices : int, optional\n Number of points to draw.\n Decrease for performance, increase for appearance.\n kwargs\n Other keyword arguments are passed to the |Shape| constructor.\n\n \"\"\"\n return cls.regular_polygon(center, radius, n_vertices, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rectangle(cls, vertices, **kwargs):\n bottom_left, top_right = vertices\n top_left = [bottom_left[0], top_right[1]]\n bottom_right = [top_right[0], bottom_left[1]]\n return cls([bottom_left, bottom_right, top_right, top_left], **kwargs)", "response": "Shortcut for creating a rectangle aligned with the screen axes from only two corners."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a |Shape| from a dictionary specification.", "response": "def from_dict(cls, spec):\n \"\"\"Create a |Shape| from a dictionary specification.\n\n Parameters\n ----------\n spec : dict\n A dictionary with either the fields ``'center'`` and ``'radius'`` (for a circle),\n ``'center'``, ``'radius'``, and ``'n_vertices'`` (for a regular polygon),\n or ``'vertices'``.\n If only two vertices are given, they are assumed to be lower left and top right corners of a rectangle.\n Other fields are interpreted as keyword arguments.\n\n \"\"\"\n spec = spec.copy()\n center = spec.pop('center', None)\n radius = spec.pop('radius', None)\n if center and radius:\n return cls.circle(center, radius, **spec)\n\n vertices = spec.pop('vertices')\n if len(vertices) == 2:\n return cls.rectangle(vertices, **spec)\n\n return cls(vertices, **spec)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrotates the shape in - place.", "response": "def rotate(self, angle, center=None):\n \"\"\"Rotate the shape, in-place.\n\n Parameters\n ----------\n angle : float\n Angle to rotate, in radians counter-clockwise.\n center : array-like, optional\n Point about which to rotate.\n If not passed, the center of the shape will be used.\n\n \"\"\"\n args = [angle]\n if center is not None:\n args.extend(center)\n self.poly.rotate(*args)\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nflips the shape in the x direction in - place.", "response": "def flip_x(self, center=None):\n \"\"\"Flip the shape in the x direction, in-place.\n\n Parameters\n ----------\n center : array-like, optional\n Point about which to flip.\n If not passed, the center of the shape will be used.\n\n \"\"\"\n if center is None:\n self.poly.flip()\n else:\n self.poly.flip(center[0])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef flip_y(self, center=None):\n if center is None:\n self.poly.flop()\n else:\n self.poly.flop(center[1])\n return self", "response": "Flip the shape in the y direction in - place."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nflipping the shape in an arbitrary direction.", "response": "def flip(self, angle, center=None):\n \"\"\" Flip the shape in an arbitrary direction.\n\n Parameters\n ----------\n angle : array-like\n The angle, in radians counter-clockwise from the horizontal axis,\n defining the angle about which to flip the shape (of a line through `center`).\n center : array-like, optional\n The point about which to flip.\n If not passed, the center of the shape will be used.\n\n \"\"\"\n return self.rotate(-angle, center=center).flip_y(center=center).rotate(angle, center=center)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef draw(self):\n if self.enabled:\n self._vertex_list.colors = self._gl_colors\n self._vertex_list.vertices = self._gl_vertices\n self._vertex_list.draw(pyglet.gl.GL_TRIANGLES)", "response": "Draw the shape in the current OpenGL context."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the shape s position by moving it forward according to its velocity.", "response": "def update(self, dt):\n \"\"\"Update the shape's position by moving it forward according to its velocity.\n\n Parameters\n ----------\n dt : float\n\n \"\"\"\n self.translate(dt * self.velocity)\n self.rotate(dt * self.angular_velocity)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmaps the official Haystack timezone list to those recognised by pytz.", "response": "def _map_timezones():\n \"\"\"\n Map the official Haystack timezone list to those recognised by pytz.\n \"\"\"\n tz_map = {}\n todo = HAYSTACK_TIMEZONES_SET.copy()\n for full_tz in pytz.all_timezones:\n # Finished case:\n if not bool(todo): # pragma: no cover\n # This is nearly impossible for us to cover, and an unlikely case.\n break\n\n # Case 1: exact match\n if full_tz in todo:\n tz_map[full_tz] = full_tz # Exact match\n todo.discard(full_tz)\n continue\n\n # Case 2: suffix match after '/'\n if '/' not in full_tz:\n continue\n\n (prefix, suffix) = full_tz.split('/',1)\n # Case 2 exception: full timezone contains more than one '/' -> ignore\n if '/' in suffix:\n continue\n\n if suffix in todo:\n tz_map[suffix] = full_tz\n todo.discard(suffix)\n continue\n\n return tz_map"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nretrieves the Haystack timezone", "response": "def timezone(haystack_tz, version=LATEST_VER):\n \"\"\"\n Retrieve the Haystack timezone\n \"\"\"\n tz_map = get_tz_map(version=version)\n try:\n tz_name = tz_map[haystack_tz]\n except KeyError:\n raise ValueError('%s is not a recognised timezone on this host' \\\n % haystack_tz)\n return pytz.timezone(tz_name)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef timezone_name(dt, version=LATEST_VER):\n tz_rmap = get_tz_rmap(version=version)\n if dt.tzinfo is None:\n raise ValueError('%r has no timezone' % dt)\n\n # Easy case: pytz timezone.\n try:\n tz_name = dt.tzinfo.zone\n return tz_rmap[tz_name]\n except KeyError:\n # Not in timezone map\n pass\n except AttributeError:\n # Not a pytz-compatible tzinfo\n pass\n\n # Hard case, try to find one that's equivalent. Hopefully we don't get\n # many of these. Start by getting the current timezone offset, and a\n # timezone-na\u00efve copy of the timestamp.\n offset = dt.utcoffset()\n dt_notz = dt.replace(tzinfo=None)\n\n if offset == datetime.timedelta(0):\n # UTC?\n return 'UTC'\n\n for olson_name, haystack_name in list(tz_rmap.items()):\n if pytz.timezone(olson_name).utcoffset(dt_notz) == offset:\n return haystack_name\n\n raise ValueError('Unable to get timezone of %r' % dt)", "response": "Determine an appropriate timezone name for the given datetime object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _unescape(s, uri=False):\n out = ''\n while len(s) > 0:\n c = s[0]\n if c == '\\\\':\n # Backslash escape\n esc_c = s[1]\n\n if esc_c in ('u', 'U'):\n # Unicode escape\n out += six.unichr(int(s[2:6], base=16))\n s = s[6:]\n continue\n else:\n if esc_c == 'b':\n out += '\\b'\n elif esc_c == 'f':\n out += '\\f'\n elif esc_c == 'n':\n out += '\\n'\n elif esc_c == 'r':\n out += '\\r'\n elif esc_c == 't':\n out += '\\t'\n else:\n if uri and (esc_c == '#'):\n # \\# is passed through with backslash.\n out += '\\\\'\n # Pass through\n out += esc_c\n s = s[2:]\n continue\n else:\n out += c\n s = s[1:]\n return out", "response": "Iterative parser for string escapes."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_grid(grid_data):\n try:\n # Split the grid up.\n grid_parts = NEWLINE_RE.split(grid_data)\n if len(grid_parts) < 2:\n raise ZincParseException('Malformed grid received',\n grid_data, 1, 1)\n\n # Grid and column metadata are the first two lines.\n grid_meta_str = grid_parts.pop(0)\n col_meta_str = grid_parts.pop(0)\n\n # First element is the grid metadata\n ver_match = VERSION_RE.match(grid_meta_str)\n if ver_match is None:\n raise ZincParseException(\n 'Could not determine version from %r' % grid_meta_str,\n grid_data, 1, 1)\n version = Version(ver_match.group(1))\n\n # Now parse the rest of the grid accordingly\n try:\n grid_meta = hs_gridMeta[version].parseString(grid_meta_str, parseAll=True)[0]\n except pp.ParseException as pe:\n # Raise a new exception with the appropriate line number.\n raise ZincParseException(\n 'Failed to parse grid metadata: %s' % pe,\n grid_data, 1, pe.col)\n except: # pragma: no cover\n # Report an error to the log if we fail to parse something.\n LOG.debug('Failed to parse grid meta: %r', grid_meta_str)\n raise\n\n try:\n col_meta = hs_cols[version].parseString(col_meta_str, parseAll=True)[0]\n except pp.ParseException as pe:\n # Raise a new exception with the appropriate line number.\n raise ZincParseException(\n 'Failed to parse column metadata: %s' \\\n % reformat_exception(pe, 2),\n grid_data, 2, pe.col)\n except: # pragma: no cover\n # Report an error to the log if we fail to parse something.\n LOG.debug('Failed to parse column meta: %r', col_meta_str)\n raise\n\n row_grammar = hs_row[version]\n def _parse_row(row_num_and_data):\n (row_num, row) = row_num_and_data\n line_num = row_num + 3\n\n try:\n return dict(zip(col_meta.keys(),\n row_grammar.parseString(row, parseAll=True)[0].asList()))\n except pp.ParseException as pe:\n # Raise a new exception with the appropriate line number.\n raise ZincParseException(\n 'Failed to parse row: %s' \\\n % reformat_exception(pe, line_num),\n grid_data, line_num, pe.col)\n except: # pragma: no cover\n # Report an error to the log if we fail to parse something.\n LOG.debug('Failed to parse row: %r', row)\n raise\n\n g = Grid(version=grid_meta.pop('ver'),\n metadata=grid_meta,\n columns=list(col_meta.items()))\n g.extend(map(_parse_row, filter(lambda gp : bool(gp[1]), enumerate(grid_parts))))\n return g\n except:\n LOG.debug('Failing grid: %r', grid_data)\n raise", "response": "Parses the incoming grid and returns a list of objects."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse a Zinc scalar in Zinc format.", "response": "def parse_scalar(scalar_data, version):\n \"\"\"\n Parse a Project Haystack scalar in ZINC format.\n \"\"\"\n try:\n return hs_scalar[version].parseString(scalar_data, parseAll=True)[0]\n except pp.ParseException as pe:\n # Raise a new exception with the appropriate line number.\n raise ZincParseException(\n 'Failed to parse scalar: %s' % reformat_exception(pe),\n scalar_data, 1, pe.col)\n except:\n LOG.debug('Failing scalar data: %r (version %r)',\n scalar_data, version)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_item(self, key, value, after=False, index=None, pos_key=None,\n replace=True):\n \"\"\"\n Add an item at a specific location, possibly replacing the\n existing item.\n\n If after is True, we insert *after* the given index, otherwise we\n insert before.\n\n The position is specified using either index or pos_key, the former\n specifies the position from the start of the array (base 0). pos_key\n specifies the name of another key, and positions the new key relative\n to that key.\n\n When replacing, the position will be left un-changed unless a location\n is specified explicitly.\n \"\"\"\n if self._validate_fn:\n self._validate_fn(value)\n\n if (index is not None) and (pos_key is not None):\n raise ValueError('Either specify index or pos_key, not both.')\n elif pos_key is not None:\n try:\n index = self.index(pos_key)\n except ValueError:\n raise KeyError('%r not found' % pos_key)\n\n if after and (index is not None):\n # insert inserts *before* index, so increment by one.\n index += 1\n\n if key in self._values:\n if not replace:\n raise KeyError('%r is duplicate' % key)\n\n if index is not None:\n # We are re-locating.\n del self[key]\n else:\n # We are updating\n self._values[key] = value\n return\n\n if index is not None:\n # Place at given position\n self._order.insert(index, key)\n else:\n # Place at end\n self._order.append(key)\n self._values[key] = value", "response": "Add an item to the internal dictionary."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndump the given grids in the specified over - the - wire format.", "response": "def dump(grids, mode=MODE_ZINC):\n \"\"\"\n Dump the given grids in the specified over-the-wire format.\n \"\"\"\n if isinstance(grids, Grid):\n return dump_grid(grids, mode=mode)\n _dump = functools.partial(dump_grid, mode=mode)\n if mode == MODE_ZINC:\n return '\\n'.join(map(_dump, grids))\n elif mode == MODE_JSON:\n return '[%s]' % ','.join(map(_dump, grids))\n else: # pragma: no cover\n raise NotImplementedError('Format not implemented: %s' % mode)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts a unit to a haystack unit.", "response": "def to_haystack(unit):\n \"\"\"\n Some parsing tweaks to fit pint units / handling of edge cases.\n \"\"\"\n unit = str(unit)\n global HAYSTACK_CONVERSION\n global PINT_CONVERSION\n if unit == 'per_minute' or \\\n unit == '/min' or \\\n unit == 'per_second' or \\\n unit == '/s' or \\\n unit == 'per_hour' or \\\n unit == '/h' or \\\n unit == None:\n return ''\n # Those units are not units... they are impossible to fit anywhere in Pint\n \n for pint_value, haystack_value in PINT_CONVERSION:\n unit = unit.replace(pint_value, haystack_value)\n for haystack_value, pint_value in HAYSTACK_CONVERSION:\n if pint_value == '':\n continue\n unit = unit.replace(pint_value, haystack_value)\n return unit"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconverting a given unit to a pint unit.", "response": "def to_pint(unit):\n \"\"\"\n Some parsing tweaks to fit pint units / handling of edge cases.\n \"\"\"\n global HAYSTACK_CONVERSION\n if unit == 'per_minute' or \\\n unit == '/min' or \\\n unit == 'per_second' or \\\n unit == '/s' or \\\n unit == 'per_hour' or \\\n unit == '/h' or \\\n unit == None:\n return ''\n # Those units are not units... they are impossible to fit anywhere in Pint\n for haystack_value, pint_value in HAYSTACK_CONVERSION:\n unit = unit.replace(haystack_value, pint_value)\n return unit"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndefines the units that are missing in project - haystack.", "response": "def define_haystack_units():\n \"\"\"\n Missing units found in project-haystack\n Added to the registry\n \"\"\"\n ureg = UnitRegistry()\n ureg.define('% = [] = percent')\n ureg.define('pixel = [] = px = dot = picture_element = pel')\n ureg.define('decibel = [] = dB')\n ureg.define('ppu = [] = parts_per_unit')\n ureg.define('ppm = [] = parts_per_million')\n ureg.define('ppb = [] = parts_per_billion')\n ureg.define('%RH = [] = percent_relative_humidity = percentRH')\n ureg.define('cubic_feet = ft ** 3 = cu_ft')\n ureg.define('cfm = cu_ft * minute = liter_per_second / 0.4719475')\n ureg.define('cfh = cu_ft * hour')\n ureg.define('cfs = cu_ft * second')\n ureg.define('VAR = volt * ampere')\n ureg.define('kVAR = 1000 * volt * ampere')\n ureg.define('MVAR = 1000000 * volt * ampere')\n ureg.define('inH2O = in_H2O')\n ureg.define('dry_air = []')\n ureg.define('gas = []')\n ureg.define('energy_efficiency_ratio = [] = EER')\n ureg.define('coefficient_of_performance = [] = COP')\n ureg.define('data_center_infrastructure_efficiency = [] = DCIE')\n ureg.define('power_usage_effectiveness = [] = PUE')\n ureg.define('formazin_nephelometric_unit = [] = fnu')\n ureg.define('nephelometric_turbidity_units = [] = ntu')\n ureg.define('power_factor = [] = PF')\n ureg.define('degree_day_celsius = [] = degdaysC')\n ureg.define('degree_day_farenheit = degree_day_celsius * 9 / 5 = degdaysF')\n ureg.define('footcandle = lumen / sq_ft = ftcd')\n ureg.define('Nm = newton * meter')\n ureg.define('%obsc = [] = percent_obscuration = percentobsc')\n ureg.define('cycle = []')\n ureg.define('cph = cycle / hour')\n ureg.define('cpm = cycle / minute')\n ureg.define('cps = cycle / second')\n ureg.define('hecto_cubic_foot = 100 * cubic_foot')\n ureg.define('tenths_second = second / 10')\n ureg.define('hundredths_second = second / 100')\n\n #ureg.define('irradiance = W / sq_meter = irr')\n # In the definition of project haystack, there's a redundancy as irr = W/m^2\n # no need to use : watts_per_square_meter_irradiance\n \n # CURRENCY\n # I know...we won'T be able to convert right now !\n ureg.define('australian_dollar = [] = AUD')\n ureg.define('british_pound = [] = GBP = \u00a3')\n ureg.define('canadian_dollar = [] = CAD')\n ureg.define('chinese_yuan = [] = CNY = \u5143')\n ureg.define('emerati_dirham = [] = AED')\n ureg.define('euro = [] = EUR = \u20ac')\n ureg.define('indian_rupee = [] = INR = \u20b9')\n ureg.define('japanese_yen = [] = JPY = \u00a5')\n ureg.define('russian_ruble = [] = RUB = \u0440\u0443\u0431')\n ureg.define('south_korean_won = [] = KRW = \u20a9')\n ureg.define('swedish_krona = [] = SEK = kr')\n ureg.define('swiss_franc = [] = CHF = Fr')\n ureg.define('taiwan_dollar = [] = TWD')\n ureg.define('us_dollar = [] = USD = $')\n ureg.define('new_israeli_shekel = [] = NIS')\n\n return ureg"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndetecting the version used from the row content or validate against the version if given.", "response": "def _detect_or_validate(self, val):\n '''\n Detect the version used from the row content, or validate against\n the version if given.\n '''\n if isinstance(val, list) \\\n or isinstance(val, dict) \\\n or isinstance(val, SortableDict) \\\n or isinstance(val, Grid):\n # Project Haystack 3.0 type.\n self._assert_version(VER_3_0)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nassert that the grid version is equal to or above the given value.", "response": "def _assert_version(self, version):\n '''\n Assert that the grid version is equal to or above the given value.\n If no version is set, set the version.\n '''\n if self.nearest_version < version:\n if self._version_given:\n raise ValueError(\n 'Data type requires version %s' \\\n % version)\n else:\n self._version = version"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _cmp(self, other):\n if not isinstance(other, Version):\n other = Version(other)\n\n num1 = self.version_nums\n num2 = other.version_nums\n\n # Pad both to be the same length\n ver_len = max(len(num1), len(num2))\n num1 += tuple([0 for n in range(len(num1), ver_len)])\n num2 += tuple([0 for n in range(len(num2), ver_len)])\n\n # Compare the versions\n for (p1, p2) in zip(num1, num2):\n if p1 < p2:\n return -1\n elif p1 > p2:\n return 1\n\n # All the same, compare the extra strings.\n # If a version misses the extra part; we consider that as coming *before*.\n if self.version_extra is None:\n if other.version_extra is None:\n return 0\n else:\n return -1\n elif other.version_extra is None:\n return 1\n elif self.version_extra == other.version_extra:\n return 0\n elif self.version_extra < other.version_extra:\n return -1\n else:\n return 1", "response": "Compares two Project Haystack version strings and returns a value of 0 if self < other 0 if self > other"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef nearest(self, ver):\n if not isinstance(ver, Version):\n ver = Version(ver)\n\n if ver in OFFICIAL_VERSIONS:\n return ver\n\n # We might not have an exact match for that.\n # See if we have one that's newer than the grid we're looking at.\n versions = list(OFFICIAL_VERSIONS)\n versions.sort(reverse=True)\n best = None\n for candidate in versions:\n # Due to ambiguities, we might have an exact match and not know it.\n # '2.0' will not hash to the same value as '2.0.0', but both are\n # equivalent.\n if candidate == ver:\n # We can't beat this, make a note of the match for later\n return candidate\n\n # If we have not seen a better candidate, and this is older\n # then we may have to settle for that.\n if (best is None) and (candidate < ver):\n warnings.warn('This version of hszinc does not yet '\\\n 'support version %s, please seek a newer version '\\\n 'or file a bug. Closest (older) version supported is %s.'\\\n % (ver, candidate))\n return candidate\n\n # Probably the best so far, but see if we can go closer\n if candidate > ver:\n best = candidate\n\n # Unhappy path, no best option? This should not happen.\n assert best is not None\n warnings.warn('This version of hszinc does not yet '\\\n 'support version %s, please seek a newer version '\\\n 'or file a bug. Closest (newer) version supported is %s.'\\\n % (ver, best))\n return best", "response": "Retrieve the official version nearest the one given."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nencrypt a file with gpg and random generated password", "response": "def encrypt_files(selected_host, only_link, file_name):\n \"\"\"\n Encrypts file with gpg and random generated password\n \"\"\"\n if ENCRYPTION_DISABLED:\n print('For encryption please install gpg')\n exit()\n passphrase = '%030x' % random.randrange(16**30)\n source_filename = file_name\n cmd = 'gpg --batch --symmetric --cipher-algo AES256 --passphrase-fd 0 ' \\\n '--output - {}'.format(source_filename)\n encrypted_output = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE)\n encrypted_data = encrypted_output.communicate(passphrase.encode())[0]\n return upload_files(encrypted_data, selected_host, only_link, file_name)+'#'+passphrase"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking if the file size is greater than max_size", "response": "def check_max_filesize(chosen_file, max_size):\n \"\"\"\n Checks file sizes for host\n \"\"\"\n if os.path.getsize(chosen_file) > max_size:\n return False\n else:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntake the arguments and returns a string that can be used to create a new resource.", "response": "def parse_arguments(args, clone_list):\n \"\"\"\n Makes parsing arguments a function.\n \"\"\"\n returned_string=\"\"\n host_number = args.host\n if args.show_list:\n print(generate_host_string(clone_list, \"Available hosts: \"))\n exit()\n if args.decrypt:\n for i in args.files:\n print(decrypt_files(i))\n exit()\n if args.files:\n for i in args.files:\n if args.limit_size:\n if args.host == host_number and host_number is not None:\n if not check_max_filesize(i, clone_list[host_number][3]):\n host_number = None\n for n, host in enumerate(clone_list):\n if not check_max_filesize(i, host[3]):\n clone_list[n] = None\n if not clone_list:\n print('None of the clones is able to support so big file.')\n if args.no_cloudflare:\n if args.host == host_number and host_number is not None and not clone_list[host_number][4]:\n print(\"This host uses Cloudflare, please choose different host.\")\n exit(1)\n else:\n for n, host in enumerate(clone_list):\n if not host[4]:\n clone_list[n] = None\n clone_list = list(filter(None, clone_list))\n if host_number is None or args.host != host_number:\n host_number = random.randrange(0, len(clone_list))\n while True:\n try:\n if args.encrypt:\n returned_string = encrypt_files(clone_list[host_number], args.only_link, i)\n else:\n returned_string = upload_files(open(i, 'rb'), \\\n clone_list[host_number], args.only_link, i)\n if args.only_link:\n print(returned_string[0])\n else:\n print(returned_string)\n except IndexError:\n #print('Selected server (' + clone_list[host_number][0] + ') is offline.')\n #print('Trying other host.')\n host_number = random.randrange(0, len(clone_list))\n continue\n except IsADirectoryError:\n print('limf does not support directory upload, if you want to upload ' \\\n 'every file in directory use limf {}/*.'.format(i.replace('/', '')))\n \n if args.log:\n with open(os.path.expanduser(args.logfile), \"a+\") as logfile:\n if args.only_link:\n logfile.write(returned_string[1])\n else:\n logfile.write(returned_string)\n logfile.write(\"\\n\")\n break\n else:\n print(\"limf: try 'limf -h' for more information\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef upload_files(selected_file, selected_host, only_link, file_name):\n try:\n answer = requests.post(\n url=selected_host[0]+\"upload.php\",\n files={'files[]':selected_file})\n file_name_1 = re.findall(r'\"url\": *\"((h.+\\/){0,1}(.+?))\"[,\\}]', \\\n answer.text.replace(\"\\\\\", \"\"))[0][2]\n if only_link:\n return [selected_host[1]+file_name_1, \"{}: {}{}\".format(file_name, selected_host[1], file_name_1)]\n else:\n return \"{}: {}{}\".format(file_name, selected_host[1], file_name_1)\n except requests.exceptions.ConnectionError:\n print(file_name + ' couldn\\'t be uploaded to ' + selected_host[0])", "response": "Uploads selected file to the host"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nserving the Swagger UI page", "response": "def swagger_ui_template_view(request):\n \"\"\"\n Serves Swagger UI page, default Swagger UI config is used but you can\n override the callable that generates the `