{"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef discovery_redis(self):\n # Install the bundle\n self.context.install_bundle(\"pelix.remote.discovery.redis\").start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_DISCOVERY_REDIS,\n \"pelix-discovery-redis\",\n {\n \"application.id\": \"sample.rs\",\n \"redis.host\": self.arguments.redis_host,\n \"redis.port\": self.arguments.redis_port,\n },\n )", "response": "Installs the Redis discovery bundles and instantiates components\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall the ZooKeeper discovery bundles and instantiates components", "response": "def discovery_zookeeper(self):\n \"\"\"\n Installs the ZooKeeper discovery bundles and instantiates components\n \"\"\"\n # Install the bundle\n self.context.install_bundle(\"pelix.remote.discovery.zookeeper\").start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_DISCOVERY_ZOOKEEPER,\n \"pelix-discovery-zookeeper\",\n {\n \"application.id\": \"sample.rs\",\n \"zookeeper.hosts\": self.arguments.zk_hosts,\n \"zookeeper.prefix\": self.arguments.zk_prefix,\n },\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninstalling the JSON - RPC transport bundles and instantiates components", "response": "def transport_jsonrpc(self):\n \"\"\"\n Installs the JSON-RPC transport bundles and instantiates components\n \"\"\"\n # Install the bundle\n self.context.install_bundle(\"pelix.remote.json_rpc\").start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_TRANSPORT_JSONRPC_EXPORTER, \"pelix-jsonrpc-exporter\"\n )\n ipopo.add(\n rs.FACTORY_TRANSPORT_JSONRPC_IMPORTER, \"pelix-jsonrpc-importer\"\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninstalls the JABSORB - RPC transport bundles and instantiates components", "response": "def transport_jabsorbrpc(self):\n \"\"\"\n Installs the JABSORB-RPC transport bundles and instantiates components\n \"\"\"\n # Install the bundle\n self.context.install_bundle(\n \"pelix.remote.transport.jabsorb_rpc\"\n ).start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_TRANSPORT_JABSORBRPC_EXPORTER,\n \"pelix-jabsorbrpc-exporter\",\n )\n ipopo.add(\n rs.FACTORY_TRANSPORT_JABSORBRPC_IMPORTER,\n \"pelix-jabsorbrpc-importer\",\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef transport_mqttrpc(self):\n # Install the bundle\n self.context.install_bundle(\"pelix.remote.transport.mqtt_rpc\").start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_TRANSPORT_MQTTRPC_EXPORTER,\n \"pelix-mqttrpc-exporter\",\n {\n \"mqtt.host\": self.arguments.mqtt_host,\n \"mqtt.port\": self.arguments.mqtt_port,\n },\n )\n ipopo.add(\n rs.FACTORY_TRANSPORT_MQTTRPC_IMPORTER,\n \"pelix-mqttrpc-importer\",\n {\n \"mqtt.host\": self.arguments.mqtt_host,\n \"mqtt.port\": self.arguments.mqtt_port,\n },\n )", "response": "Creates the MQTT - RPC transport bundles and instantiates components\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ninstalls the XML - RPC transport bundles and instantiates components", "response": "def transport_xmlrpc(self):\n \"\"\"\n Installs the XML-RPC transport bundles and instantiates components\n \"\"\"\n # Install the bundle\n self.context.install_bundle(\"pelix.remote.xml_rpc\").start()\n\n with use_waiting_list(self.context) as ipopo:\n # Instantiate the discovery\n ipopo.add(\n rs.FACTORY_TRANSPORT_XMLRPC_EXPORTER, \"pelix-xmlrpc-exporter\"\n )\n ipopo.add(\n rs.FACTORY_TRANSPORT_XMLRPC_IMPORTER, \"pelix-xmlrpc-importer\"\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_handlers(self, component_context, instance):\n # Extract information from the context\n requirements = component_context.get_handler(\n ipopo_constants.HANDLER_REQUIRES_BEST\n )\n requires_filters = component_context.properties.get(\n ipopo_constants.IPOPO_REQUIRES_FILTERS, None\n )\n\n # Prepare requirements\n requirements = self._prepare_requirements(\n requirements, requires_filters\n )\n\n # Set up the runtime dependency handlers\n return [\n BestDependency(field, requirement)\n for field, requirement in requirements.items()\n ]", "response": "Returns the list of handlers associated to the given component."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncall when a service is arrived in the framework.", "response": "def on_service_arrival(self, svc_ref):\n \"\"\"\n Called when a service has been registered in the framework\n\n :param svc_ref: A service reference\n \"\"\"\n with self._lock:\n new_ranking = svc_ref.get_property(SERVICE_RANKING)\n if self._current_ranking is not None:\n if new_ranking > self._current_ranking:\n # New service with better ranking: use it\n self._pending_ref = svc_ref\n old_ref = self.reference\n old_value = self._value\n\n # Clean up like for a departure\n self._current_ranking = None\n self._value = None\n self.reference = None\n\n # Unbind (new binding will be done afterwards)\n self._ipopo_instance.unbind(self, old_value, old_ref)\n else:\n # No ranking yet: inject the service\n self.reference = svc_ref\n self._value = self._context.get_service(svc_ref)\n self._current_ranking = new_ranking\n self._pending_ref = None\n\n self._ipopo_instance.bind(self, self._value, self.reference)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef on_service_departure(self, svc_ref):\n with self._lock:\n if svc_ref is self.reference:\n # Injected service going away...\n service = self._value\n\n # Clear the instance values\n self._current_ranking = None\n self._value = None\n self.reference = None\n\n if self.requirement.immediate_rebind:\n # Look for a replacement\n self._pending_ref = self._context.get_service_reference(\n self.requirement.specification, self.requirement.filter\n )\n else:\n self._pending_ref = None\n\n self._ipopo_instance.unbind(self, service, svc_ref)", "response": "Called when a service is deactivated from the framework"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef on_service_modify(self, svc_ref, old_properties):\n with self._lock:\n if self.reference is None:\n # A previously registered service now matches our filter\n return self.on_service_arrival(svc_ref)\n else:\n # Check if the ranking changed the service to inject\n best_ref = self._context.get_service_reference(\n self.requirement.specification, self.requirement.filter\n )\n if best_ref is self.reference:\n # Still the best service: notify the property modification\n if svc_ref is self.reference:\n # Call update only if necessary\n self._ipopo_instance.update(\n self, self._value, svc_ref, old_properties\n )\n else:\n # A new service is now the best: start a departure loop\n self.on_service_departure(self.reference)\n\n return None", "response": "Called when a service has been modified in the framework"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_properties(self):\n with self.__lock:\n if self.__deleted:\n raise ValueError(\"{0} has been deleted\".format(self.__pid))\n\n elif not self.__updated:\n # Fresh configuration\n return None\n\n # Filter a copy of the properties\n props = self.__properties.copy()\n\n try:\n del props[services.CONFIG_PROP_BUNDLE_LOCATION]\n except KeyError:\n # Ignore\n pass\n\n return props", "response": "Returns the properties of this Configuration object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __properties_update(self, properties):\n if not properties:\n # Nothing to do\n return False\n\n with self.__lock:\n # Make a copy of the properties\n properties = properties.copy()\n\n # Override properties\n properties[services.CONFIG_PROP_PID] = self.__pid\n\n if self.__location:\n properties[\n services.CONFIG_PROP_BUNDLE_LOCATION\n ] = self.__location\n\n if self.__factory_pid:\n properties[\n services.CONFIG_PROP_FACTORY_PID\n ] = self.__factory_pid\n\n # See if new properties are different\n if properties == self.__properties:\n return False\n\n # Store the copy (before storing data)\n self.__properties = properties\n self.__updated = True\n\n # Store the data\n # it will cause FileInstall to update this configuration again, but\n # this will ignored because self.__properties has already been\n # saved\n self.__persistence.store(self.__pid, properties)\n return True", "response": "Internal method to update the properties of the current configuration."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nupdate the properties of this Configuration object.", "response": "def update(self, properties=None):\n # pylint: disable=W0212\n \"\"\"\n If called without properties, only notifies listeners\n\n Update the properties of this Configuration object.\n Stores the properties in persistent storage after adding or overwriting\n the following properties:\n\n * \"service.pid\" : is set to be the PID of this configuration.\n * \"service.factoryPid\" : if this is a factory configuration it is set\n to the factory PID else it is not set.\n\n These system properties are all of type String.\n\n If the corresponding Managed Service/Managed Service Factory is\n registered, its updated method must be called asynchronously.\n Else, this callback is delayed until aforementioned registration\n occurs.\n\n Also initiates an asynchronous call to all ConfigurationListeners with\n a ConfigurationEvent.CM_UPDATED event.\n\n :param properties: the new set of properties for this configuration\n :raise IOError: Error storing the configuration\n \"\"\"\n with self.__lock:\n # Update properties\n if self.__properties_update(properties):\n # Update configurations, if something changed\n self.__config_admin._update(self)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete(self, directory_updated=False):\n # pylint: disable=W0212\n \"\"\"\n Delete this configuration\n\n :param directory_updated: If True, tell ConfigurationAdmin to not\n recall the directory of this deletion\n (internal use only)\n \"\"\"\n with self.__lock:\n if self.__deleted:\n # Nothing to do\n return\n\n # Update status\n self.__deleted = True\n\n # Notify ConfigurationAdmin, notify services only if the\n # configuration had been updated before\n self.__config_admin._delete(self, self.__updated, directory_updated)\n\n # Remove the file\n self.__persistence.delete(self.__pid)\n\n # Clean up\n if self.__properties:\n self.__properties.clear()\n\n self.__persistence = None\n self.__pid = None", "response": "Delete this configuration from the persistence and properties."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef matches(self, ldap_filter):\n if not self.is_valid():\n # Do not test invalid configurations\n return False\n\n return ldap_filter.matches(self.__properties)", "response": "Tests if this configuration matches the given LDAP filter."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef walk_modules(path):\n if path is None or not os.path.isdir(path):\n return\n\n yielded = set()\n try:\n file_names = os.listdir(path)\n except OSError:\n # ignore unreadable directories like import does\n file_names = []\n\n # handle packages before same-named modules\n file_names.sort()\n\n for filename in file_names:\n modname = inspect.getmodulename(filename)\n if modname == \"__init__\" or modname in yielded:\n continue\n\n file_path = os.path.join(path, filename)\n is_package = False\n\n if not modname and os.path.isdir(file_path) and \".\" not in filename:\n modname = filename\n try:\n dir_contents = os.listdir(file_path)\n except OSError:\n # ignore unreadable directories like import does\n dir_contents = []\n\n for sub_filename in dir_contents:\n sub_name = inspect.getmodulename(sub_filename)\n if sub_name == \"__init__\":\n is_package = True\n break\n else:\n # not a package\n continue\n\n if modname and \".\" not in modname:\n yielded.add(modname)\n yield modname, is_package", "response": "Generator to walk through a folder and yields all loadable packages and modules."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_framework(\n bundles,\n properties=None,\n auto_start=False,\n wait_for_stop=False,\n auto_delete=False,\n):\n # type: (Union[list, tuple], dict, bool, bool, bool) -> Framework\n \"\"\"\n Creates a Pelix framework, installs the given bundles and returns its\n instance reference.\n If *auto_start* is True, the framework will be started once all bundles\n will have been installed\n If *wait_for_stop* is True, the method will return only when the framework\n will have stopped. This requires *auto_start* to be True.\n If *auto_delete* is True, the framework will be deleted once it has\n stopped, and the method will return None.\n This requires *wait_for_stop* and *auto_start* to be True.\n\n :param bundles: Bundles to initially install (shouldn't be empty if\n *wait_for_stop* is True)\n :param properties: Optional framework properties\n :param auto_start: If True, the framework will be started immediately\n :param wait_for_stop: If True, the method will return only when the\n framework will have stopped\n :param auto_delete: If True, deletes the framework once it stopped.\n :return: The framework instance\n :raise ValueError: Only one framework can run at a time\n \"\"\"\n # Test if a framework already exists\n if FrameworkFactory.is_framework_running(None):\n raise ValueError(\"A framework is already running\")\n\n # Create the framework\n framework = FrameworkFactory.get_framework(properties)\n\n # Install bundles\n context = framework.get_bundle_context()\n for bundle in bundles:\n context.install_bundle(bundle)\n\n if auto_start:\n # Automatically start the framework\n framework.start()\n\n if wait_for_stop:\n # Wait for the framework to stop\n try:\n framework.wait_for_stop(None)\n except KeyboardInterrupt:\n # Stop keyboard interruptions\n if framework.get_state() == Bundle.ACTIVE:\n framework.stop()\n\n if auto_delete:\n # Delete the framework\n FrameworkFactory.delete_framework(framework)\n framework = None\n\n return framework", "response": "Creates a Pelix framework and returns its base object."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _package_exists(path):\n # type: (str) -> bool\n \"\"\"\n Checks if the given Python path matches a valid file or a valid container\n file\n\n :param path: A Python path\n :return: True if the module or its container exists\n \"\"\"\n while path:\n if os.path.exists(path):\n return True\n else:\n path = os.path.dirname(path)\n\n return False", "response": "Checks if the given Python path matches a valid file or a valid container\n file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nnormalizes sys. path to avoid the use of relative folders", "response": "def normalize_path():\n \"\"\"\n Normalizes sys.path to avoid the use of relative folders\n \"\"\"\n # Normalize Python paths\n whole_path = [\n os.path.abspath(path) for path in sys.path if os.path.exists(path)\n ]\n\n # Keep the \"dynamic\" current folder indicator and add the \"static\"\n # current path\n # Use an OrderedDict to have a faster lookup (path not in whole_set)\n whole_set = collections.OrderedDict(((\"\", 1), (os.getcwd(), 1)))\n\n # Add original path entries\n for path in whole_path:\n if path not in whole_set:\n whole_set[path] = 1\n\n # Set the new content of sys.path (still ordered thanks to OrderedDict)\n sys.path = list(whole_set)\n\n # Normalize paths in loaded modules\n for module_ in sys.modules.values():\n try:\n module_.__path__ = [\n os.path.abspath(path)\n for path in module_.__path__\n if _package_exists(path)\n ]\n except AttributeError:\n # builtin modules don't have a __path__\n pass\n except ImportError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nretrieve the requested method of the activator or returns None if no method is available.", "response": "def __get_activator_method(self, method_name):\n \"\"\"\n Retrieves the requested method of the activator, or returns None\n\n :param method_name: A method name\n :return: A method, or None\n \"\"\"\n # Get the activator\n activator = getattr(self.__module, ACTIVATOR, None)\n if activator is None:\n # Get the old activator\n activator = getattr(self.__module, ACTIVATOR_LEGACY, None)\n if activator is not None:\n # Old activator found: print a deprecation warning\n _logger.warning(\n \"Bundle %s uses the deprecated '%s' to declare\"\n \" its activator. Use @BundleActivator instead.\",\n self.__name,\n ACTIVATOR_LEGACY,\n )\n return getattr(activator, method_name, None)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _fire_bundle_event(self, kind):\n # type: (int) -> None\n \"\"\"\n Fires a bundle event of the given kind\n\n :param kind: Kind of event\n \"\"\"\n self.__framework._dispatcher.fire_bundle_event(BundleEvent(kind, self))", "response": "Fires a bundle event of the given kind"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning this bundle s ServiceReference list for all services it has registered or an empty list if the bundle is uninstalled.", "response": "def get_registered_services(self):\n # type: () -> List[ServiceReference]\n \"\"\"\n Returns this bundle's ServiceReference list for all services it has\n registered or an empty list\n\n The list is valid at the time of the call to this method, however, as\n the Framework is a very dynamic environment, services can be modified\n or unregistered at any time.\n\n :return: An array of ServiceReference objects\n :raise BundleException: If the bundle has been uninstalled\n \"\"\"\n if self._state == Bundle.UNINSTALLED:\n raise BundleException(\n \"Can't call 'get_registered_services' on an \"\n \"uninstalled bundle\"\n )\n return self.__framework._registry.get_bundle_registered_services(self)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning this bundle s ServiceReference list for all services it is using or an empty list if the bundle is uninstalled.", "response": "def get_services_in_use(self):\n # type: () -> List[ServiceReference]\n \"\"\"\n Returns this bundle's ServiceReference list for all services it is\n using or an empty list.\n A bundle is considered to be using a service if its use count for that\n service is greater than zero.\n\n The list is valid at the time of the call to this method, however, as\n the Framework is a very dynamic environment, services can be modified\n or unregistered at any time.\n\n :return: An array of ServiceReference objects\n :raise BundleException: If the bundle has been uninstalled\n \"\"\"\n if self._state == Bundle.UNINSTALLED:\n raise BundleException(\n \"Can't call 'get_services_in_use' on an uninstalled bundle\"\n )\n return self.__framework._registry.get_bundle_imported_services(self)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the bundle version using the version of the module.", "response": "def get_version(self):\n # type: () -> str\n \"\"\"\n Retrieves the bundle version, using the ``__version__`` or\n ``__version_info__`` attributes of its module.\n\n :return: The bundle version, \"0.0.0\" by default\n \"\"\"\n # Get the version value\n version = getattr(self.__module, \"__version__\", None)\n if version:\n return version\n\n # Convert the __version_info__ entry\n info = getattr(self.__module, \"__version_info__\", None)\n if info:\n return \".\".join(str(part) for part in __version_info__)\n\n # No version\n return \"0.0.0\""} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstart the bundle. Does nothing if the bundle is already starting or active. :raise BundleException: The framework is not yet started or the bundle activator failed.", "response": "def start(self):\n \"\"\"\n Starts the bundle. Does nothing if the bundle is already starting or\n active.\n\n :raise BundleException: The framework is not yet started or the bundle\n activator failed.\n \"\"\"\n if self.__framework._state not in (Bundle.STARTING, Bundle.ACTIVE):\n # Framework is not running\n raise BundleException(\n \"Framework must be started before its bundles\"\n )\n\n with self._lock:\n if self._state in (Bundle.ACTIVE, Bundle.STARTING):\n # Already started bundle, do nothing\n return\n\n # Store the bundle current state\n previous_state = self._state\n\n # Starting...\n self._state = Bundle.STARTING\n self._fire_bundle_event(BundleEvent.STARTING)\n\n # Call the activator, if any\n starter = self.__get_activator_method(\"start\")\n if starter is not None:\n try:\n # Call the start method\n starter(self.__context)\n except (FrameworkException, BundleException):\n # Restore previous state\n self._state = previous_state\n\n # Re-raise directly Pelix exceptions\n _logger.exception(\n \"Pelix error raised by %s while starting\", self.__name\n )\n raise\n except Exception as ex:\n # Restore previous state\n self._state = previous_state\n\n # Raise the error\n _logger.exception(\n \"Error raised by %s while starting\", self.__name\n )\n raise BundleException(ex)\n\n # Bundle is now active\n self._state = Bundle.ACTIVE\n self._fire_bundle_event(BundleEvent.STARTED)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstop the bundle. Does nothing if the bundle is already stopped. :raise BundleException: The bundle activator failed.", "response": "def stop(self):\n \"\"\"\n Stops the bundle. Does nothing if the bundle is already stopped.\n\n :raise BundleException: The bundle activator failed.\n \"\"\"\n if self._state != Bundle.ACTIVE:\n # Invalid state\n return\n\n exception = None\n with self._lock:\n # Store the bundle current state\n previous_state = self._state\n\n # Stopping...\n self._state = Bundle.STOPPING\n self._fire_bundle_event(BundleEvent.STOPPING)\n\n # Call the activator, if any\n stopper = self.__get_activator_method(\"stop\")\n if stopper is not None:\n try:\n # Call the start method\n stopper(self.__context)\n except (FrameworkException, BundleException) as ex:\n # Restore previous state\n self._state = previous_state\n\n # Re-raise directly Pelix exceptions\n _logger.exception(\n \"Pelix error raised by %s while stopping\", self.__name\n )\n exception = ex\n except Exception as ex:\n _logger.exception(\n \"Error raised by %s while stopping\", self.__name\n )\n # Store the exception (raised after service clean up)\n exception = BundleException(ex)\n\n # Hide remaining services\n self.__framework._hide_bundle_services(self)\n\n # Intermediate bundle event : activator should have cleaned up\n # everything, but some element could stay (iPOPO components, ...)\n self._fire_bundle_event(BundleEvent.STOPPING_PRECLEAN)\n\n # Remove remaining services (the hard way)\n self.__unregister_services()\n\n # Cleanup service usages\n self.__framework._unget_used_services(self)\n\n # Bundle is now stopped and all its services have been unregistered\n self._state = Bundle.RESOLVED\n self._fire_bundle_event(BundleEvent.STOPPED)\n\n # Raise the exception, if any\n # pylint: disable=E0702\n # Pylint seems to miss the \"is not None\" check below\n if exception is not None:\n raise exception"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate the bundle with the latest information from the module.", "response": "def update(self):\n \"\"\"\n Updates the bundle\n \"\"\"\n with self._lock:\n # Was it active ?\n restart = self._state == Bundle.ACTIVE\n\n # Send the update event\n self._fire_bundle_event(BundleEvent.UPDATE_BEGIN)\n\n try:\n # Stop the bundle\n self.stop()\n except:\n # Something wrong occurred, notify listeners\n self._fire_bundle_event(BundleEvent.UPDATE_FAILED)\n raise\n\n # Change the source file age\n module_stat = None\n module_file = getattr(self.__module, \"__file__\", None)\n if module_file is not None and os.path.isfile(module_file):\n try:\n module_stat = os.stat(module_file)\n\n # Change modification time to bypass weak time resolution\n # of the underlying file system\n os.utime(\n module_file,\n (module_stat.st_atime, module_stat.st_mtime + 1),\n )\n except OSError:\n # Can't touch the file\n _logger.warning(\n \"Failed to update the modification time of '%s'. \"\n \"The bundle update might not reflect the latest \"\n \"changes.\",\n module_file,\n )\n\n # Clean up the module constants (otherwise kept by reload)\n # Keep special members (__name__, __file__, ...)\n old_content = self.__module.__dict__.copy()\n for name in list(self.__module.__dict__):\n if not (name.startswith(\"__\") and name.endswith(\"__\")):\n del self.__module.__dict__[name]\n\n try:\n # Reload the module\n reload_module(self.__module)\n except (ImportError, SyntaxError) as ex:\n # Exception raised if the file is unreadable\n _logger.exception(\"Error updating %s: %s\", self.__name, ex)\n\n # Reset module content\n self.__module.__dict__.clear()\n self.__module.__dict__.update(old_content)\n\n if module_stat is not None:\n try:\n # Reset times\n os.utime(\n module_file,\n (module_stat.st_atime, module_stat.st_mtime),\n )\n except OSError:\n # Shouldn't occur, since we succeeded before the update\n _logger.debug(\n \"Failed to reset the modification time of '%s'\",\n module_file,\n )\n\n if restart:\n try:\n # Re-start the bundle\n self.start()\n except:\n # Something wrong occurred, notify listeners\n self._fire_bundle_event(BundleEvent.UPDATE_FAILED)\n raise\n\n # Bundle update finished\n self._fire_bundle_event(BundleEvent.UPDATED)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_property(self, name, value):\n # type: (str, object) -> bool\n \"\"\"\n Adds a property to the framework **if it is not yet set**.\n\n If the property already exists (same name), then nothing is done.\n Properties can't be updated.\n\n :param name: The property name\n :param value: The value to set\n :return: True if the property was stored, else False\n \"\"\"\n with self.__properties_lock:\n if name in self.__properties:\n # Already stored property\n return False\n\n self.__properties[name] = value\n return True", "response": "Adds a property to the framework if it is not already set."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of service references matching the given filter.", "response": "def find_service_references(\n self, clazz=None, ldap_filter=None, only_one=False\n ):\n # type: (Optional[str], Optional[str], bool) -> Optional[List[ServiceReference]]\n \"\"\"\n Finds all services references matching the given filter.\n\n :param clazz: Class implemented by the service\n :param ldap_filter: Service filter\n :param only_one: Return the first matching service reference only\n :return: A list of found reference, or None\n :raise BundleException: An error occurred looking for service\n references\n \"\"\"\n return self._registry.find_service_references(\n clazz, ldap_filter, only_one\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_bundle_by_id(self, bundle_id):\n # type: (int) -> Union[Bundle, Framework]\n \"\"\"\n Retrieves the bundle with the given ID\n\n :param bundle_id: ID of an installed bundle\n :return: The requested bundle\n :raise BundleException: The ID is invalid\n \"\"\"\n if bundle_id == 0:\n # \"System bundle\"\n return self\n\n with self.__bundles_lock:\n if bundle_id not in self.__bundles:\n raise BundleException(\"Invalid bundle ID {0}\".format(bundle_id))\n\n return self.__bundles[bundle_id]", "response": "Returns the bundle with the given ID or raises BundleException if the bundle is not installed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_bundle_by_name(self, bundle_name):\n # type: (str) -> Optional[Bundle]\n \"\"\"\n Retrieves the bundle with the given name\n\n :param bundle_name: Name of the bundle to look for\n :return: The requested bundle, None if not found\n \"\"\"\n if bundle_name is None:\n # Nothing to do\n return None\n\n if bundle_name is self.get_symbolic_name():\n # System bundle requested\n return self\n\n with self.__bundles_lock:\n for bundle in self.__bundles.values():\n if bundle_name == bundle.get_symbolic_name():\n # Found !\n return bundle\n\n # Not found...\n return None", "response": "Returns the bundle with the given name or None if not found."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_bundles(self):\n # type: () -> List[Bundle]\n \"\"\"\n Returns the list of all installed bundles\n\n :return: the list of all installed bundles\n \"\"\"\n with self.__bundles_lock:\n return [\n self.__bundles[bundle_id]\n for bundle_id in sorted(self.__bundles.keys())\n ]", "response": "Returns the list of all installed bundles"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_property(self, name):\n # type: (str) -> object\n \"\"\"\n Retrieves a framework or system property. As framework properties don't\n change while it's running, this method don't need to be protected.\n\n :param name: The property name\n \"\"\"\n with self.__properties_lock:\n return self.__properties.get(name, os.getenv(name))", "response": "Retrieves a framework or system property."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the service corresponding to the given bundle and reference.", "response": "def get_service(self, bundle, reference):\n # type: (Bundle, ServiceReference) -> Any\n \"\"\"\n Retrieves the service corresponding to the given reference\n\n :param bundle: The bundle requiring the service\n :param reference: A service reference\n :return: The requested service\n :raise BundleException: The service could not be found\n :raise TypeError: The argument is not a ServiceReference object\n \"\"\"\n if not isinstance(bundle, Bundle):\n raise TypeError(\"First argument must be a Bundle object\")\n elif not isinstance(reference, ServiceReference):\n raise TypeError(\"Second argument must be a ServiceReference object\")\n\n try:\n # Unregistering service, just give it\n return self.__unregistering_services[reference]\n except KeyError:\n return self._registry.get_service(bundle, reference)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef install_bundle(self, name, path=None):\n # type: (str, str) -> Bundle\n \"\"\"\n Installs the bundle with the given name\n\n *Note:* Before Pelix 0.5.0, this method returned the ID of the\n installed bundle, instead of the Bundle object.\n\n **WARNING:** The behavior of the loading process is subject to changes,\n as it does not allow to safely run multiple frameworks in the same\n Python interpreter, as they might share global module values.\n\n :param name: A bundle name\n :param path: Preferred path to load the module\n :return: The installed Bundle object\n :raise BundleException: Something happened\n \"\"\"\n with self.__bundles_lock:\n # A bundle can't be installed twice\n for bundle in self.__bundles.values():\n if bundle.get_symbolic_name() == name:\n _logger.debug(\"Already installed bundle: %s\", name)\n return bundle\n\n # Load the module\n try:\n if path:\n # Use the given path in priority\n sys.path.insert(0, path)\n\n try:\n # The module has already been loaded\n module_ = sys.modules[name]\n except KeyError:\n # Load the module\n # __import__(name) -> package level\n # import_module -> module level\n module_ = importlib.import_module(name)\n except (ImportError, IOError) as ex:\n # Error importing the module\n raise BundleException(\n \"Error installing bundle {0}: {1}\".format(name, ex)\n )\n finally:\n if path:\n # Clean up the path. The loaded module(s) might\n # have changed the path content, so do not use an\n # index\n sys.path.remove(path)\n\n # Add the module to sys.modules, just to be sure\n sys.modules[name] = module_\n\n # Compute the bundle ID\n bundle_id = self.__next_bundle_id\n\n # Prepare the bundle object and its context\n bundle = Bundle(self, bundle_id, name, module_)\n\n # Store the bundle\n self.__bundles[bundle_id] = bundle\n\n # Update the bundle ID counter\n self.__next_bundle_id += 1\n\n # Fire the bundle installed event\n event = BundleEvent(BundleEvent.INSTALLED, bundle)\n self._dispatcher.fire_bundle_event(event)\n return bundle", "response": "Installs a bundle with the given name and returns the ID of the bundle object."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninstall all the modules found in the given path and returns a list of bundles and the list of failed modules names.", "response": "def install_package(self, path, recursive=False, prefix=None):\n # type: (str, bool, str) -> tuple\n \"\"\"\n Installs all the modules found in the given package\n\n :param path: Path of the package (folder)\n :param recursive: If True, install the sub-packages too\n :param prefix: (**internal**) Prefix for all found modules\n :return: A 2-tuple, with the list of installed bundles and the list\n of failed modules names\n :raise ValueError: Invalid path\n \"\"\"\n if not path:\n raise ValueError(\"Empty path\")\n elif not is_string(path):\n raise ValueError(\"Path must be a string\")\n\n # Use an absolute path\n path = os.path.abspath(path)\n if not os.path.exists(path):\n raise ValueError(\"Nonexistent path: {0}\".format(path))\n\n # Create a simple visitor\n def visitor(fullname, is_package, module_path):\n # pylint: disable=W0613\n \"\"\"\n Package visitor: accepts everything in recursive mode,\n else avoids packages\n \"\"\"\n return recursive or not is_package\n\n # Set up the prefix if needed\n if prefix is None:\n prefix = os.path.basename(path)\n\n bundles = set() # type: Set[Bundle]\n failed = set() # type: Set[str]\n\n with self.__bundles_lock:\n try:\n # Install the package first, resolved from the parent directory\n bundles.add(self.install_bundle(prefix, os.path.dirname(path)))\n\n # Visit the package\n visited, sub_failed = self.install_visiting(\n path, visitor, prefix\n )\n\n # Update the sets\n bundles.update(visited)\n failed.update(sub_failed)\n except BundleException as ex:\n # Error loading the module\n _logger.warning(\"Error loading package %s: %s\", prefix, ex)\n failed.add(prefix)\n\n return bundles, failed"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef install_visiting(self, path, visitor, prefix=None):\n # Validate the path\n if not path:\n raise ValueError(\"Empty path\")\n elif not is_string(path):\n raise ValueError(\"Path must be a string\")\n\n # Validate the visitor\n if visitor is None:\n raise ValueError(\"No visitor method given\")\n\n # Use an absolute path\n path = os.path.abspath(path)\n if not os.path.exists(path):\n raise ValueError(\"Inexistent path: {0}\".format(path))\n\n # Set up the prefix if needed\n if prefix is None:\n prefix = os.path.basename(path)\n\n bundles = set()\n failed = set()\n\n with self.__bundles_lock:\n # Walk through the folder to find modules\n for name, is_package in walk_modules(path):\n # Ignore '__main__' modules\n if name == \"__main__\":\n continue\n\n # Compute the full name of the module\n fullname = \".\".join((prefix, name)) if prefix else name\n try:\n if visitor(fullname, is_package, path):\n if is_package:\n # Install the package\n bundles.add(self.install_bundle(fullname, path))\n\n # Visit the package\n sub_path = os.path.join(path, name)\n sub_bundles, sub_failed = self.install_visiting(\n sub_path, visitor, fullname\n )\n bundles.update(sub_bundles)\n failed.update(sub_failed)\n else:\n # Install the bundle\n bundles.add(self.install_bundle(fullname, path))\n except BundleException as ex:\n # Error loading the module\n _logger.warning(\"Error visiting %s: %s\", fullname, ex)\n\n # Try the next module\n failed.add(fullname)\n continue\n\n return bundles, failed", "response": "Installs all the modules found in the given path if they are accepted by the visitor."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nregister a service and calls the listeners of the given service.", "response": "def register_service(\n self,\n bundle,\n clazz,\n service,\n properties,\n send_event,\n factory=False,\n prototype=False,\n ):\n # type: (Bundle, Union[List[Any], type, str], object, dict, bool, bool, bool) -> ServiceRegistration\n \"\"\"\n Registers a service and calls the listeners\n\n :param bundle: The bundle registering the service\n :param clazz: Name(s) of the interface(s) implemented by service\n :param service: The service to register\n :param properties: Service properties\n :param send_event: If not, doesn't trigger a service registered event\n :param factory: If True, the given service is a service factory\n :param prototype: If True, the given service is a prototype service\n factory (the factory argument is considered True)\n :return: A ServiceRegistration object\n :raise BundleException: An error occurred while registering the service\n \"\"\"\n if bundle is None or service is None or not clazz:\n raise BundleException(\"Invalid registration parameters\")\n\n if not isinstance(properties, dict):\n # Be sure we have a valid dictionary\n properties = {}\n else:\n # Use a copy of the given properties\n properties = properties.copy()\n\n # Prepare the class specification\n if not isinstance(clazz, (list, tuple)):\n # Make a list from the single class\n clazz = [clazz]\n\n # Test the list content\n classes = []\n for svc_clazz in clazz:\n if inspect.isclass(svc_clazz):\n # Keep the type name\n svc_clazz = svc_clazz.__name__\n\n if not svc_clazz or not is_string(svc_clazz):\n # Invalid class name\n raise BundleException(\n \"Invalid class name: {0}\".format(svc_clazz)\n )\n\n # Class OK\n classes.append(svc_clazz)\n\n # Make the service registration\n registration = self._registry.register(\n bundle, classes, properties, service, factory, prototype\n )\n\n # Update the bundle registration information\n bundle._registered_service(registration)\n\n if send_event:\n # Call the listeners\n event = ServiceEvent(\n ServiceEvent.REGISTERED, registration.get_reference()\n )\n self._dispatcher.fire_service_event(event)\n\n return registration"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef start(self):\n # type: () -> bool\n \"\"\"\n Starts the framework\n\n :return: True if the bundle has been started, False if it was already\n running\n :raise BundleException: A bundle failed to start\n \"\"\"\n with self._lock:\n if self._state in (Bundle.STARTING, Bundle.ACTIVE):\n # Already started framework\n return False\n\n # Reset the stop event\n self._fw_stop_event.clear()\n\n # Starting...\n self._state = Bundle.STARTING\n self._dispatcher.fire_bundle_event(\n BundleEvent(BundleEvent.STARTING, self)\n )\n\n # Start all registered bundles (use a copy, just in case...)\n for bundle in self.__bundles.copy().values():\n try:\n bundle.start()\n except FrameworkException as ex:\n # Important error\n _logger.exception(\n \"Important error starting bundle: %s\", bundle\n )\n if ex.needs_stop:\n # Stop the framework (has to be in active state)\n self._state = Bundle.ACTIVE\n self.stop()\n return False\n except BundleException:\n # A bundle failed to start : just log\n _logger.exception(\"Error starting bundle: %s\", bundle)\n\n # Bundle is now active\n self._state = Bundle.ACTIVE\n return True", "response": "Starts the framework and returns True if the framework has been started False otherwise."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstops the framework and all its associated services.", "response": "def stop(self):\n # type: () -> bool\n \"\"\"\n Stops the framework\n\n :return: True if the framework stopped, False it wasn't running\n \"\"\"\n with self._lock:\n if self._state != Bundle.ACTIVE:\n # Invalid state\n return False\n\n # Hide all services (they will be deleted by bundle.stop())\n for bundle in self.__bundles.values():\n self._registry.hide_bundle_services(bundle)\n\n # Stopping...\n self._state = Bundle.STOPPING\n self._dispatcher.fire_bundle_event(\n BundleEvent(BundleEvent.STOPPING, self)\n )\n\n # Notify listeners that the bundle is stopping\n self._dispatcher.fire_framework_stopping()\n\n bid = self.__next_bundle_id - 1\n while bid > 0:\n bundle = self.__bundles.get(bid)\n bid -= 1\n\n if bundle is None or bundle.get_state() != Bundle.ACTIVE:\n # Ignore inactive bundle\n continue\n\n try:\n bundle.stop()\n except Exception as ex:\n # Just log exceptions\n _logger.exception(\n \"Error stopping bundle %s: %s\",\n bundle.get_symbolic_name(),\n ex,\n )\n\n # Framework is now stopped\n self._state = Bundle.RESOLVED\n self._dispatcher.fire_bundle_event(\n BundleEvent(BundleEvent.STOPPED, self)\n )\n\n # All bundles have been stopped, release \"wait_for_stop\"\n self._fw_stop_event.set()\n\n # Force the registry clean up\n self._registry.clear()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete(self, force=False):\n if not force and self._state not in (\n Bundle.INSTALLED,\n Bundle.RESOLVED,\n Bundle.STOPPING,\n ):\n _logger.warning(\"Trying to delete an active framework\")\n return False\n\n return FrameworkFactory.delete_framework(self)", "response": "Deletes the current framework"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nend the uninstallation of the given bundle (must be called by Bundle) :param bundle: The bundle to uninstall :raise BundleException: Invalid bundle", "response": "def uninstall_bundle(self, bundle):\n # type: (Bundle) -> None\n \"\"\"\n Ends the uninstallation of the given bundle (must be called by Bundle)\n\n :param bundle: The bundle to uninstall\n :raise BundleException: Invalid bundle\n \"\"\"\n if bundle is None:\n # Do nothing\n return\n\n with self.__bundles_lock:\n # Stop the bundle first\n bundle.stop()\n\n bundle_id = bundle.get_bundle_id()\n if bundle_id not in self.__bundles:\n raise BundleException(\"Invalid bundle {0}\".format(bundle))\n\n # Notify listeners\n self._dispatcher.fire_bundle_event(\n BundleEvent(BundleEvent.UNINSTALLED, bundle)\n )\n\n # Remove it from the dictionary\n del self.__bundles[bundle_id]\n\n # Remove it from the system => avoid unintended behaviors and\n # forces a complete module reload if it is re-installed\n name = bundle.get_symbolic_name()\n try:\n del sys.modules[name]\n except KeyError:\n # Ignore\n pass\n\n try:\n # Clear reference in parent\n parent, basename = name.rsplit(\".\", 1)\n if parent:\n delattr(sys.modules[parent], basename)\n except (KeyError, AttributeError, ValueError):\n # Ignore errors\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef unregister_service(self, registration):\n # type: (ServiceRegistration) -> bool\n \"\"\"\n Unregisters the given service\n\n :param registration: A ServiceRegistration to the service to unregister\n :raise BundleException: Invalid reference\n \"\"\"\n # Get the Service Reference\n reference = registration.get_reference()\n\n # Remove the service from the registry\n svc_instance = self._registry.unregister(reference)\n\n # Keep a track of the unregistering reference\n self.__unregistering_services[reference] = svc_instance\n\n # Call the listeners\n event = ServiceEvent(ServiceEvent.UNREGISTERING, reference)\n self._dispatcher.fire_service_event(event)\n\n # Update the bundle registration information\n bundle = reference.get_bundle()\n bundle._unregistered_service(registration)\n\n # Remove the unregistering reference\n del self.__unregistering_services[reference]\n return True", "response": "Unregisters the given service from the registry"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the framework state.", "response": "def update(self):\n \"\"\"\n Stops and starts the framework, if the framework is active.\n\n :raise BundleException: Something wrong occurred while stopping or\n starting the framework.\n \"\"\"\n with self._lock:\n if self._state == Bundle.ACTIVE:\n self.stop()\n self.start()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwaits for the framework to stop.", "response": "def wait_for_stop(self, timeout=None):\n # type: (Optional[int]) -> bool\n \"\"\"\n Waits for the framework to stop. Does nothing if the framework bundle\n is not in ACTIVE state.\n\n Uses a threading.Condition object\n\n :param timeout: The maximum time to wait (in seconds)\n :return: True if the framework has stopped, False if the timeout raised\n \"\"\"\n if self._state != Bundle.ACTIVE:\n # Inactive framework, ignore the call\n return True\n\n self._fw_stop_event.wait(timeout)\n\n with self._lock:\n # If the timeout raised, we should be in another state\n return self._state == Bundle.RESOLVED"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves a service object from the registry.", "response": "def unget_service(self, service):\n # type: (Any) -> bool\n \"\"\"\n Releases a service object for the associated service.\n\n :param service: An instance of a service returned by ``get_service()``\n :return: True if the bundle usage has been removed\n \"\"\"\n return self.__registry.unget_service(\n self.__bundle, self.__reference, service\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_service_listener(\n self, listener, ldap_filter=None, specification=None\n ):\n \"\"\"\n Registers a service listener\n\n The service listener must have a method with the following prototype::\n\n def service_changed(self, event):\n '''\n Called by Pelix when some service properties changes\n\n event: A ServiceEvent object\n '''\n # ...\n\n :param bundle_context: This bundle context\n :param listener: The listener to register\n :param ldap_filter: Filter that must match the service properties\n (optional, None to accept all services)\n :param specification: The specification that must provide the service\n (optional, None to accept all services)\n :return: True if the listener has been successfully registered\n \"\"\"\n return self.__framework._dispatcher.add_service_listener(\n self, listener, specification, ldap_filter\n )", "response": "Adds a service listener to the bundle context."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the bundle object associated with the given ID.", "response": "def get_bundle(self, bundle_id=None):\n # type: (Union[Bundle, int]) -> Bundle\n \"\"\"\n Retrieves the :class:`~pelix.framework.Bundle` object for the bundle\n matching the given ID (int). If no ID is given (None), the bundle\n associated to this context is returned.\n\n :param bundle_id: A bundle ID (optional)\n :return: The requested :class:`~pelix.framework.Bundle` object\n :raise BundleException: The given ID doesn't exist or is invalid\n \"\"\"\n if bundle_id is None:\n # Current bundle\n return self.__bundle\n elif isinstance(bundle_id, Bundle):\n # Got a bundle (compatibility with older install_bundle())\n bundle_id = bundle_id.get_bundle_id()\n\n return self.__framework.get_bundle_by_id(bundle_id)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a ServiceReference object for a service that implements and was registered under the specified class.", "response": "def get_service_reference(self, clazz, ldap_filter=None):\n # type: (Optional[str], Optional[str]) -> Optional[ServiceReference]\n \"\"\"\n Returns a ServiceReference object for a service that implements and\n was registered under the specified class\n\n :param clazz: The class name with which the service was registered.\n :param ldap_filter: A filter on service properties\n :return: A service reference, None if not found\n \"\"\"\n result = self.__framework.find_service_references(\n clazz, ldap_filter, True\n )\n try:\n return result[0]\n except TypeError:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_service_references(self, clazz, ldap_filter=None):\n # type: (Optional[str], Optional[str]) -> Optional[List[ServiceReference]]\n \"\"\"\n Returns the service references for services that were registered under\n the specified class by this bundle and matching the given filter\n\n :param clazz: The class name with which the service was registered.\n :param ldap_filter: A filter on service properties\n :return: The list of references to the services registered by the\n calling bundle and matching the filters.\n \"\"\"\n refs = self.__framework.find_service_references(clazz, ldap_filter)\n if refs:\n for ref in refs:\n if ref.get_bundle() is not self.__bundle:\n refs.remove(ref)\n\n return refs", "response": "Returns the list of service references that were registered under the specified class by this bundle and matching the given filter."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ninstalls all the modules found in the given path.", "response": "def install_package(self, path, recursive=False):\n # type: (str, bool) -> tuple\n \"\"\"\n Installs all the modules found in the given package (directory).\n It is a utility method working like\n :meth:`~pelix.framework.BundleContext.install_visiting`, with a visitor\n accepting every module found.\n\n :param path: Path of the package (folder)\n :param recursive: If True, installs the modules found in sub-directories\n :return: A 2-tuple, with the list of installed bundles\n (:class:`~pelix.framework.Bundle`) and the list of the names\n of the modules which import failed.\n :raise ValueError: The given path is invalid\n \"\"\"\n return self.__framework.install_package(path, recursive)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nregisters a service in the current bundle.", "response": "def register_service(\n self,\n clazz,\n service,\n properties,\n send_event=True,\n factory=False,\n prototype=False,\n ):\n # type: (Union[List[Any], type, str], object, dict, bool, bool, bool) -> ServiceRegistration\n \"\"\"\n Registers a service\n\n :param clazz: Class or Classes (list) implemented by this service\n :param service: The service instance\n :param properties: The services properties (dictionary)\n :param send_event: If not, doesn't trigger a service registered event\n :param factory: If True, the given service is a service factory\n :param prototype: If True, the given service is a prototype service\n factory (the factory argument is considered True)\n :return: A ServiceRegistration object\n :raise BundleException: An error occurred while registering the service\n \"\"\"\n return self.__framework.register_service(\n self.__bundle,\n clazz,\n service,\n properties,\n send_event,\n factory,\n prototype,\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef unget_service(self, reference):\n # type: (ServiceReference) -> bool\n \"\"\"\n Disables a reference to the service\n\n :return: True if the bundle was using this reference, else False\n \"\"\"\n # Lose the dependency\n return self.__framework._registry.unget_service(\n self.__bundle, reference\n )", "response": "Disables a reference to the service"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the current framework instance.", "response": "def get_framework(cls, properties=None):\n # type: (Optional[dict]) -> Framework\n \"\"\"\n If it doesn't exist yet, creates a framework with the given properties,\n else returns the current framework instance.\n\n :return: A Pelix instance\n \"\"\"\n if cls.__singleton is None:\n # Normalize sys.path\n normalize_path()\n cls.__singleton = Framework(properties)\n\n return cls.__singleton"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving the framework singleton from the class.", "response": "def delete_framework(cls, framework=None):\n # type: (Optional[Framework]) -> bool\n # pylint: disable=W0212\n \"\"\"\n Removes the framework singleton\n\n :return: True on success, else False\n \"\"\"\n if framework is None:\n framework = cls.__singleton\n\n if framework is cls.__singleton:\n # Stop the framework\n try:\n framework.stop()\n except:\n _logger.exception(\"Error stopping the framework\")\n\n # Uninstall its bundles\n bundles = framework.get_bundles()\n for bundle in bundles:\n try:\n bundle.uninstall()\n except:\n _logger.exception(\n \"Error uninstalling bundle %s\",\n bundle.get_symbolic_name(),\n )\n\n # Clear the event dispatcher\n framework._dispatcher.clear()\n\n # Clear the singleton\n cls.__singleton = None\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_matching_interfaces(object_class, exported_intfs):\n # type: (List[str], Optional[List[str]]) -> Optional[List[str]]\n \"\"\"\n Returns the list of interfaces matching the export property\n\n :param object_class: The specifications of the service\n :param exported_intfs: The declared exported interfaces\n :return: The list of declared exported interfaces\n \"\"\"\n if object_class is None or exported_intfs is None:\n return None\n\n if isinstance(exported_intfs, str) and exported_intfs == \"*\":\n return object_class\n\n # after this exported_intfs will be list\n exported_intfs = get_string_plus_property_value(exported_intfs)\n if len(exported_intfs) == 1 and exported_intfs[0] == \"*\":\n return object_class\n\n return exported_intfs", "response": "Returns the list of interfaces that are matching the exported property"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_prop_value(name, props, default=None):\n # type: (str, Dict[str, Any], Any) -> Any\n \"\"\"\n Returns the value of a property or the default one\n\n :param name: Name of a property\n :param props: Dictionary of properties\n :param default: Default value\n :return: The value of the property or the default one\n \"\"\"\n if not props:\n return default\n\n try:\n return props[name]\n except KeyError:\n return default", "response": "Returns the value of a property or the default value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_prop_if_null(name, props, if_null):\n # type: (str, Dict[str, Any], Any) -> None\n \"\"\"\n Updates the value of a property if the previous one was None\n\n :param name: Name of the property\n :param props: Dictionary of properties\n :param if_null: Value to insert if the previous was None\n \"\"\"\n value = get_prop_value(name, props)\n if value is None:\n props[name] = if_null", "response": "Updates the value of a property if it is None."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_string_plus_property_value(value):\n # type: (Any) -> Optional[List[str]]\n \"\"\"\n Converts a string or list of string into a list of strings\n\n :param value: A string or a list of strings\n :return: A list of strings or None\n \"\"\"\n if value:\n if isinstance(value, str):\n return [value]\n if isinstance(value, list):\n return value\n if isinstance(value, tuple):\n return list(value)\n\n return None", "response": "Converts a string or list of strings into a list of strings or None if the value is not a string or a list of strings"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_string_plus_property(name, props, default=None):\n # type: (str, Dict[str, Any], Optional[Any]) -> Any\n \"\"\"\n Returns the value of the given property or the default value\n\n :param name: A property name\n :param props: A dictionary of properties\n :param default: Value to return if the property doesn't exist\n :return: The property value or the default one\n \"\"\"\n val = get_string_plus_property_value(get_prop_value(name, props, default))\n return default if val is None else val", "response": "Returns the value of the given property or the default value if the property doesn t exist"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_exported_interfaces(svc_ref, overriding_props=None):\n # type: (ServiceReference, Optional[Dict[str, Any]]) -> Optional[List[str]]\n \"\"\"\n Looks for the interfaces exported by a service\n\n :param svc_ref: Service reference\n :param overriding_props: Properties overriding service ones\n :return: The list of exported interfaces\n \"\"\"\n # first check overriding_props for service.exported.interfaces\n exported_intfs = get_prop_value(\n SERVICE_EXPORTED_INTERFACES, overriding_props\n )\n # then check svc_ref property\n if not exported_intfs:\n exported_intfs = svc_ref.get_property(SERVICE_EXPORTED_INTERFACES)\n\n if not exported_intfs:\n return None\n\n return get_matching_interfaces(\n svc_ref.get_property(constants.OBJECTCLASS), exported_intfs\n )", "response": "Returns the list of interfaces exported by a service."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates that the exported interfaces are all provided by the service", "response": "def validate_exported_interfaces(object_class, exported_intfs):\n # type: (List[str], List[str]) -> bool\n \"\"\"\n Validates that the exported interfaces are all provided by the service\n\n :param object_class: The specifications of a service\n :param exported_intfs: The exported specifications\n :return: True if the exported specifications are all provided by the service\n \"\"\"\n if (\n not exported_intfs\n or not isinstance(exported_intfs, list)\n or not exported_intfs\n ):\n return False\n else:\n for exintf in exported_intfs:\n if exintf not in object_class:\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the package version of interfaces", "response": "def get_package_versions(intfs, props):\n # type: (List[str], Dict[str, Any]) -> List[Tuple[str, str]]\n \"\"\"\n Gets the package version of interfaces\n\n :param intfs: A list of interfaces\n :param props: A dictionary containing endpoint package versions\n :return: A list of tuples (package name, version)\n \"\"\"\n result = []\n for intf in intfs:\n pkg_name = get_package_from_classname(intf)\n if pkg_name:\n key = ENDPOINT_PACKAGE_VERSION_ + pkg_name\n val = props.get(key, None)\n if val:\n result.append((key, val))\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_rsa_props(\n object_class,\n exported_cfgs,\n remote_intents=None,\n ep_svc_id=None,\n fw_id=None,\n pkg_vers=None,\n service_intents=None,\n):\n \"\"\"\n Constructs a dictionary of RSA properties from the given arguments\n\n :param object_class: Service specifications\n :param exported_cfgs: Export configurations\n :param remote_intents: Supported remote intents\n :param ep_svc_id: Endpoint service ID\n :param fw_id: Remote Framework ID\n :param pkg_vers: Version number of the specification package\n :param service_intents: Service intents\n :return: A dictionary of properties\n \"\"\"\n results = {}\n if not object_class:\n raise ArgumentError(\n \"object_class\", \"object_class must be an [] of Strings\"\n )\n results[\"objectClass\"] = object_class\n if not exported_cfgs:\n raise ArgumentError(\n \"exported_cfgs\", \"exported_cfgs must be an array of Strings\"\n )\n results[REMOTE_CONFIGS_SUPPORTED] = exported_cfgs\n results[SERVICE_IMPORTED_CONFIGS] = exported_cfgs\n if remote_intents:\n results[REMOTE_INTENTS_SUPPORTED] = remote_intents\n if service_intents:\n results[SERVICE_INTENTS] = service_intents\n if not ep_svc_id:\n ep_svc_id = get_next_rsid()\n results[ENDPOINT_SERVICE_ID] = ep_svc_id\n results[SERVICE_ID] = ep_svc_id\n if not fw_id:\n # No framework ID means an error\n fw_id = \"endpoint-in-error\"\n results[ENDPOINT_FRAMEWORK_UUID] = fw_id\n if pkg_vers:\n if isinstance(pkg_vers, type(tuple())):\n pkg_vers = [pkg_vers]\n for pkg_ver in pkg_vers:\n results[pkg_ver[0]] = pkg_ver[1]\n results[ENDPOINT_ID] = create_uuid()\n results[SERVICE_IMPORTED] = \"true\"\n return results", "response": "Constructs a dictionary of RSA properties from the given arguments."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ecf_props(ep_id, ep_id_ns, rsvc_id=None, ep_ts=None):\n results = {}\n if not ep_id:\n raise ArgumentError(\"ep_id\", \"ep_id must be a valid endpoint id\")\n results[ECF_ENDPOINT_ID] = ep_id\n if not ep_id_ns:\n raise ArgumentError(\"ep_id_ns\", \"ep_id_ns must be a valid namespace\")\n results[ECF_ENDPOINT_CONTAINERID_NAMESPACE] = ep_id_ns\n if not rsvc_id:\n rsvc_id = get_next_rsid()\n results[ECF_RSVC_ID] = rsvc_id\n if not ep_ts:\n ep_ts = time_since_epoch()\n results[ECF_ENDPOINT_TIMESTAMP] = ep_ts\n return results", "response": "Returns a dictionary of properties that are required to create an Endpoint Container ID and a namespace."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_extra_props(props):\n # type: (Dict[str, Any]) -> Dict[str, Any]\n \"\"\"\n Returns the extra properties, *i.e.* non-ECF, non-RSA properties\n\n :param props: A dictionary of properties\n :return: A filtered dictionary\n \"\"\"\n return {\n key: value\n for key, value in props.items()\n if key not in ECFPROPNAMES\n and key not in RSA_PROP_NAMES\n and not key.startswith(ENDPOINT_PACKAGE_VERSION_)\n }", "response": "Returns the extra properties that are not part of an ECF or RSA container."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_edef_props(\n object_class,\n exported_cfgs,\n ep_namespace,\n ep_id,\n ecf_ep_id,\n ep_rsvc_id,\n ep_ts,\n remote_intents=None,\n fw_id=None,\n pkg_ver=None,\n service_intents=None,\n):\n \"\"\"\n Prepares the EDEF properties of an endpoint, merge of RSA and ECF\n properties\n \"\"\"\n osgi_props = get_rsa_props(\n object_class,\n exported_cfgs,\n remote_intents,\n ep_rsvc_id,\n fw_id,\n pkg_ver,\n service_intents,\n )\n ecf_props = get_ecf_props(ecf_ep_id, ep_namespace, ep_rsvc_id, ep_ts)\n return merge_dicts(osgi_props, ecf_props)", "response": "Get the properties of an EDEF object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_dot_properties(prefix, props, remove_prefix):\n # type: (str, Dict[str, Any], bool) -> Dict[str, Any]\n \"\"\"\n Gets the properties starting with the given prefix\n \"\"\"\n result_props = {}\n if props:\n dot_keys = [x for x in props.keys() if x.startswith(prefix + \".\")]\n for dot_key in dot_keys:\n if remove_prefix:\n new_key = dot_key[len(prefix) + 1 :]\n else:\n new_key = dot_key\n result_props[new_key] = props.get(dot_key)\n return result_props", "response": "Returns the properties starting with the given prefix."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncopy all properties with non - reserved names from props to target.", "response": "def copy_non_reserved(props, target):\n # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]\n \"\"\"\n Copies all properties with non-reserved names from ``props`` to ``target``\n\n :param props: A dictionary of properties\n :param target: Another dictionary\n :return: The target dictionary\n \"\"\"\n target.update(\n {\n key: value\n for key, value in props.items()\n if not is_reserved_property(key)\n }\n )\n return target"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef copy_non_ecf(props, target):\n # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]\n \"\"\"\n Copies non-ECF properties from ``props`` to ``target``\n\n :param props: An input dictionary\n :param target: The dictionary to copy non-ECF properties to\n :return: The ``target`` dictionary\n \"\"\"\n target.update(\n {key: value for key, value in props.items() if key not in ECFPROPNAMES}\n )\n return target", "response": "Copies non - ECF properties from props to target"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd the given item to the given set.", "response": "def set_append(input_set, item):\n # type: (set, Any) -> set\n \"\"\"\n Appends in-place the given item to the set.\n If the item is a list, all elements are added to the set.\n\n :param input_set: An existing set\n :param item: The item or list of items to add\n :return: The given set\n \"\"\"\n if item:\n if isinstance(item, (list, tuple)):\n input_set.update(item)\n else:\n input_set.add(item)\n return input_set"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a RemoteServiceAdminEvent object from an ImportRegistration", "response": "def fromimportreg(cls, bundle, import_reg):\n # type: (Bundle, ImportRegistration) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from an ImportRegistration\n \"\"\"\n exc = import_reg.get_exception()\n if exc:\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.IMPORT_ERROR,\n bundle,\n import_reg.get_import_container_id(),\n import_reg.get_remoteservice_id(),\n None,\n None,\n exc,\n import_reg.get_description(),\n )\n\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.IMPORT_REGISTRATION,\n bundle,\n import_reg.get_import_container_id(),\n import_reg.get_remoteservice_id(),\n import_reg.get_import_reference(),\n None,\n None,\n import_reg.get_description(),\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fromexportreg(cls, bundle, export_reg):\n # type: (Bundle, ExportRegistration) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from an ExportRegistration\n \"\"\"\n exc = export_reg.get_exception()\n if exc:\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.EXPORT_ERROR,\n bundle,\n export_reg.get_export_container_id(),\n export_reg.get_remoteservice_id(),\n None,\n None,\n exc,\n export_reg.get_description(),\n )\n\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.EXPORT_REGISTRATION,\n bundle,\n export_reg.get_export_container_id(),\n export_reg.get_remoteservice_id(),\n None,\n export_reg.get_export_reference(),\n None,\n export_reg.get_description(),\n )", "response": "Creates a RemoteServiceAdminEvent object from an ExportRegistration object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef fromexportupdate(cls, bundle, export_reg):\n # type: (Bundle, ExportRegistration) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from the update of an\n ExportRegistration\n \"\"\"\n exc = export_reg.get_exception()\n if exc:\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.EXPORT_ERROR,\n bundle,\n export_reg.get_export_container_id(),\n export_reg.get_remoteservice_id(),\n None,\n export_reg.get_export_reference(),\n None,\n export_reg.get_description(),\n )\n\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.EXPORT_UPDATE,\n bundle,\n export_reg.get_export_container_id(),\n export_reg.get_remoteservice_id(),\n None,\n export_reg.get_export_reference(),\n None,\n export_reg.get_description(),\n )", "response": "Creates a RemoteServiceAdminEvent object from an ExportRegistration"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef fromimportupdate(cls, bundle, import_reg):\n # type: (Bundle, ImportRegistration) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from the update of an\n ImportRegistration\n \"\"\"\n exc = import_reg.get_exception()\n if exc:\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.IMPORT_ERROR,\n bundle,\n import_reg.get_import_container_id(),\n import_reg.get_remoteservice_id(),\n None,\n None,\n exc,\n import_reg.get_description(),\n )\n\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.IMPORT_UPDATE,\n bundle,\n import_reg.get_import_container_id(),\n import_reg.get_remoteservice_id(),\n import_reg.get_import_reference(),\n None,\n None,\n import_reg.get_description(),\n )", "response": "Creates a RemoteServiceAdminEvent object from an ImportRegistration."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a RemoteServiceAdminEvent object from the departure of an ImportRegistration", "response": "def fromimportunreg(\n cls, bundle, cid, rsid, import_ref, exception, endpoint\n ):\n # type: (Bundle, Tuple[str, str], Tuple[Tuple[str, str], int], ImportReference, Optional[Tuple[Any, Any, Any]], EndpointDescription) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from the departure of an\n ImportRegistration\n \"\"\"\n return RemoteServiceAdminEvent(\n typ=RemoteServiceAdminEvent.IMPORT_UNREGISTRATION,\n bundle=bundle,\n cid=cid,\n rsid=rsid,\n import_ref=import_ref,\n exception=exception,\n endpoint=endpoint,\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fromexportunreg(\n cls, bundle, exporterid, rsid, export_ref, exception, endpoint\n ):\n # type: (Bundle, Tuple[str, str], Tuple[Tuple[str, str], int], ExportReference, Optional[Tuple[Any, Any, Any]], EndpointDescription) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from the departure of an\n ExportRegistration\n \"\"\"\n return RemoteServiceAdminEvent(\n typ=RemoteServiceAdminEvent.EXPORT_UNREGISTRATION,\n bundle=bundle,\n cid=exporterid,\n rsid=rsid,\n export_ref=export_ref,\n exception=exception,\n endpoint=endpoint,\n )", "response": "Creates a RemoteServiceAdminEvent object from the departure of an ExportRegistration\n ."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fromimporterror(cls, bundle, importerid, rsid, exception, endpoint):\n # type: (Bundle, Tuple[str, str], Tuple[Tuple[str, str], int], Optional[Tuple[Any, Any, Any]], EndpointDescription) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from an import error\n \"\"\"\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.IMPORT_ERROR,\n bundle,\n importerid,\n rsid,\n None,\n None,\n exception,\n endpoint,\n )", "response": "Creates a RemoteServiceAdminEvent object from an import error."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef fromexporterror(cls, bundle, exporterid, rsid, exception, endpoint):\n # type: (Bundle, Tuple[str, str], Tuple[Tuple[str, str], int], Optional[Tuple[Any, Any, Any]], EndpointDescription) -> RemoteServiceAdminEvent\n \"\"\"\n Creates a RemoteServiceAdminEvent object from an export error\n \"\"\"\n return RemoteServiceAdminEvent(\n RemoteServiceAdminEvent.EXPORT_ERROR,\n bundle,\n exporterid,\n rsid,\n None,\n None,\n exception,\n endpoint,\n )", "response": "Creates a RemoteServiceAdminEvent object from an export error."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _try_instantiate(self, ipopo, factory, component):\n # type: (Any, str, str) -> None\n \"\"\"\n Tries to instantiate a component from the queue. Hides all exceptions.\n\n :param ipopo: The iPOPO service\n :param factory: Component factory\n :param component: Component name\n \"\"\"\n try:\n # Get component properties\n with self.__lock:\n properties = self.__queue[factory][component]\n except KeyError:\n # Component not in queue\n return\n else:\n try:\n # Try instantiation\n ipopo.instantiate(factory, component, properties)\n except TypeError:\n # Unknown factory: try later\n pass\n except ValueError as ex:\n # Already known component\n _logger.error(\"Component already running: %s\", ex)\n except Exception as ex:\n # Other error\n _logger.exception(\"Error instantiating component: %s\", ex)", "response": "Tries to instantiate a component from the queue. Hides all exceptions."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _start(self):\n try:\n # Try to register to factory events\n with use_ipopo(self.__context) as ipopo:\n ipopo.add_listener(self)\n except BundleException:\n # Service not yet present\n pass\n\n # Register the iPOPO service listener\n self.__context.add_service_listener(self, specification=SERVICE_IPOPO)", "response": "Starts the instantiation queue."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _stop(self):\n # Unregisters the iPOPO service listener\n self.__context.remove_service_listener(self)\n\n try:\n # Try to register to factory events\n with use_ipopo(self.__context) as ipopo:\n ipopo.remove_listener(self)\n except BundleException:\n # Service not present anymore\n pass", "response": "Stops the instantiation queue"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclearing all references to this object", "response": "def _clear(self):\n \"\"\"\n Clear all references (called by its bundle activator)\n \"\"\"\n self.__names.clear()\n self.__queue.clear()\n self.__context = None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nhandle an event about the iPOPO service", "response": "def service_changed(self, event):\n # type: (ServiceEvent) -> None\n \"\"\"\n Handles an event about the iPOPO service\n \"\"\"\n kind = event.get_kind()\n if kind == ServiceEvent.REGISTERED:\n # iPOPO service registered: register to factory events\n with use_ipopo(self.__context) as ipopo:\n ipopo.add_listener(self)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef handle_ipopo_event(self, event):\n # type: (IPopoEvent) -> None\n \"\"\"\n Handles an iPOPO event\n\n :param event: iPOPO event bean\n \"\"\"\n kind = event.get_kind()\n if kind == IPopoEvent.REGISTERED:\n # A factory has been registered\n try:\n with use_ipopo(self.__context) as ipopo:\n factory = event.get_factory_name()\n\n with self.__lock:\n # Copy the list of components names for this factory\n components = self.__queue[factory].copy()\n\n for component in components:\n self._try_instantiate(ipopo, factory, component)\n except BundleException:\n # iPOPO not yet started\n pass\n except KeyError:\n # No components for this new factory\n pass", "response": "Handles an iPOPO event."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add(self, factory, component, properties=None):\n # type: (str, str, dict) -> None\n \"\"\"\n Enqueues the instantiation of the given component\n\n :param factory: Factory name\n :param component: Component name\n :param properties: Component properties\n :raise ValueError: Component name already reserved in the queue\n :raise Exception: Error instantiating the component\n \"\"\"\n with self.__lock:\n if component in self.__names:\n raise ValueError(\n \"Component name already queued: {0}\".format(component)\n )\n\n # Normalize properties\n if properties is None:\n properties = {}\n\n # Store component description\n self.__names[component] = factory\n self.__queue.setdefault(factory, {})[component] = properties\n\n try:\n with use_ipopo(self.__context) as ipopo:\n # Try to instantiate the component right now\n self._try_instantiate(ipopo, factory, component)\n except BundleException:\n # iPOPO not yet started\n pass", "response": "Adds a new component to the queue."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove(self, component):\n # type: (str) -> None\n \"\"\"\n Kills/Removes the component with the given name\n\n :param component: A component name\n :raise KeyError: Unknown component\n \"\"\"\n with self.__lock:\n # Find its factory\n factory = self.__names.pop(component)\n components = self.__queue[factory]\n\n # Clear the queue\n del components[component]\n if not components:\n # No more component for this factory\n del self.__queue[factory]\n\n # Kill the component\n try:\n with use_ipopo(self.__context) as ipopo:\n # Try to instantiate the component right now\n ipopo.kill(component)\n except (BundleException, ValueError):\n # iPOPO not yet started or component not instantiated\n pass", "response": "Removes the component with the given name from the queue."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _create_server(\n shell,\n server_address,\n port,\n cert_file=None,\n key_file=None,\n key_password=None,\n ca_file=None,\n):\n \"\"\"\n Creates the TCP console on the given address and port\n\n :param shell: The remote shell handler\n :param server_address: Server bound address\n :param port: Server port\n :param cert_file: Path to the server certificate\n :param key_file: Path to the server private key\n :param key_password: Password for the key file\n :param ca_file: Path to Certificate Authority to authenticate clients\n :return: A tuple: Server thread, TCP server object, Server active flag\n \"\"\"\n # Set up the request handler creator\n active_flag = SharedBoolean(True)\n\n def request_handler(*rh_args):\n \"\"\"\n Constructs a RemoteConsole as TCP request handler\n \"\"\"\n return RemoteConsole(shell, active_flag, *rh_args)\n\n # Set up the server\n server = ThreadingTCPServerFamily(\n (server_address, port),\n request_handler,\n cert_file,\n key_file,\n key_password,\n ca_file,\n )\n\n # Set flags\n server.daemon_threads = True\n server.allow_reuse_address = True\n\n # Activate the server\n server.server_bind()\n server.server_activate()\n\n # Serve clients\n server_thread = threading.Thread(\n target=server.serve_forever, name=\"RemoteShell-{0}\".format(port)\n )\n server_thread.daemon = True\n server_thread.start()\n\n return server_thread, server, active_flag", "response": "Create a TCP server on the given address and port."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrunning a Python interpreter console and blocks until the user exits it.", "response": "def _run_interpreter(variables, banner):\n \"\"\"\n Runs a Python interpreter console and blocks until the user exits it.\n\n :param variables: Interpreters variables (locals)\n :param banner: Start-up banners\n \"\"\"\n # Script-only imports\n import code\n\n try:\n import readline\n import rlcompleter\n\n readline.set_completer(rlcompleter.Completer(variables).complete)\n readline.parse_and_bind(\"tab: complete\")\n except ImportError:\n # readline is not available: ignore\n pass\n\n # Start the console\n shell = code.InteractiveConsole(variables)\n shell.interact(banner)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef main(argv=None):\n # Prepare arguments\n parser = argparse.ArgumentParser(\n prog=\"pelix.shell.remote\",\n parents=[make_common_parser()],\n description=\"Pelix Remote Shell ({} SSL support)\".format(\n \"with\" if ssl is not None else \"without\"\n ),\n )\n\n # Remote shell options\n group = parser.add_argument_group(\"Remote Shell options\")\n group.add_argument(\n \"-a\",\n \"--address\",\n default=\"localhost\",\n help=\"The remote shell binding address\",\n )\n group.add_argument(\n \"-p\",\n \"--port\",\n type=int,\n default=9000,\n help=\"The remote shell binding port\",\n )\n\n if ssl is not None:\n # Remote Shell TLS options\n group = parser.add_argument_group(\"TLS Options\")\n group.add_argument(\"--cert\", help=\"Path to the server certificate file\")\n group.add_argument(\n \"--key\",\n help=\"Path to the server key file \"\n \"(can be omitted if the key is in the certificate)\",\n )\n group.add_argument(\n \"--key-password\",\n help=\"Password of the server key.\"\n \"Set to '-' for a password request.\",\n )\n group.add_argument(\n \"--ca-chain\",\n help=\"Path to the CA chain file to authenticate clients\",\n )\n\n # Local options\n group = parser.add_argument_group(\"Local options\")\n group.add_argument(\n \"--no-input\",\n action=\"store_true\",\n help=\"Run without input (for daemon mode)\",\n )\n\n # Parse them\n args = parser.parse_args(argv)\n\n # Handle arguments\n init = handle_common_arguments(args)\n\n # Set the initial bundles\n bundles = [\n \"pelix.ipopo.core\",\n \"pelix.shell.core\",\n \"pelix.shell.ipopo\",\n \"pelix.shell.remote\",\n ]\n bundles.extend(init.bundles)\n\n # Start a Pelix framework\n framework = pelix.framework.create_framework(\n utilities.remove_duplicates(bundles), init.properties\n )\n framework.start()\n context = framework.get_bundle_context()\n\n # Instantiate configured components\n init.instantiate_components(framework.get_bundle_context())\n\n # Instantiate a Remote Shell, if necessary\n with use_ipopo(context) as ipopo:\n rshell_name = \"remote-shell\"\n try:\n ipopo.get_instance_details(rshell_name)\n except ValueError:\n # Component doesn't exist, we can instantiate it.\n\n if ssl is not None:\n # Copy parsed arguments\n ca_chain = args.ca_chain\n cert = args.cert\n key = args.key\n\n # Normalize the TLS key file password argument\n if args.key_password == \"-\":\n import getpass\n\n key_password = getpass.getpass(\n \"Password for {}: \".format(args.key or args.cert)\n )\n else:\n key_password = args.key_password\n else:\n # SSL support is missing:\n # Ensure the SSL arguments are defined but set to None\n ca_chain = None\n cert = None\n key = None\n key_password = None\n\n # Setup the component\n rshell = ipopo.instantiate(\n pelix.shell.FACTORY_REMOTE_SHELL,\n rshell_name,\n {\n \"pelix.shell.address\": args.address,\n \"pelix.shell.port\": args.port,\n \"pelix.shell.ssl.ca\": ca_chain,\n \"pelix.shell.ssl.cert\": cert,\n \"pelix.shell.ssl.key\": key,\n \"pelix.shell.ssl.key_password\": key_password,\n },\n )\n\n # Avoid loose reference to the password\n del key_password\n else:\n logging.error(\n \"A remote shell component (%s) is already \"\n \"configured. Abandon.\",\n rshell_name,\n )\n return 1\n\n # Prepare a banner\n host, port = rshell.get_access()\n try:\n if args.no_input:\n # No input required: just print the access to the shell\n print(\"Remote shell bound to:\", host, \"- port:\", port)\n\n try:\n while not framework.wait_for_stop(1):\n # Awake from wait every second to let KeyboardInterrupt\n # exception to raise\n pass\n except KeyboardInterrupt:\n print(\"Got Ctrl+C: exiting.\")\n return 127\n else:\n # Prepare interpreter variables\n variables = {\n \"__name__\": \"__console__\",\n \"__doc__\": None,\n \"__package__\": None,\n \"framework\": framework,\n \"context\": context,\n \"use_ipopo\": use_ipopo,\n }\n\n banner = (\n \"{lines}\\nPython interpreter with Pelix Remote Shell\\n\"\n \"Remote shell bound to: {host}:{port}\\n{lines}\\n\"\n \"Python version: {version}\\n\".format(\n lines=\"-\" * 80, version=sys.version, host=host, port=port\n )\n )\n\n # Run an interpreter\n _run_interpreter(variables, banner)\n finally:\n # Stop the framework\n framework.stop()", "response": "Entry point for the script."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend data to the client.", "response": "def send(self, data):\n \"\"\"\n Tries to send data to the client.\n\n :param data: Data to be sent\n :return: True if the data was sent, False on error\n \"\"\"\n if data is not None:\n data = data.encode(\"UTF-8\")\n\n try:\n self.wfile.write(data)\n self.wfile.flush()\n return True\n\n except IOError:\n # An error occurred, mask it\n # -> This allows to handle the command even if the client has been\n # disconnect (i.e. \"echo stop 0 | nc localhost 9000\")\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\naccepting a new client. Sets up SSL wrapping if necessary.", "response": "def get_request(self):\n \"\"\"\n Accepts a new client. Sets up SSL wrapping if necessary.\n\n :return: A tuple: (client socket, client address tuple)\n \"\"\"\n # Accept the client\n client_socket, client_address = self.socket.accept()\n\n if ssl is not None and self.cert_file:\n # Setup an SSL context to accept clients with a certificate\n # signed by a known chain of authority.\n # Other clients will be rejected during handshake.\n context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)\n try:\n # Force a valid/signed client-side certificate\n context.verify_mode = ssl.CERT_REQUIRED\n\n # Load the server certificate\n context.load_cert_chain(\n certfile=self.cert_file,\n keyfile=self.key_file,\n password=self.key_password,\n )\n\n if self.ca_file:\n # Load the given authority chain\n context.load_verify_locations(self.ca_file)\n else:\n # Load the default chain if none given\n context.load_default_certs(ssl.Purpose.CLIENT_AUTH)\n except Exception as ex:\n # Explicitly log the error as the default behaviour hides it\n _logger.error(\"Error setting up the SSL context: %s\", ex)\n raise\n\n try:\n # SSL handshake\n client_stream = context.wrap_socket(\n client_socket, server_side=True\n )\n except ssl.SSLError as ex:\n # Explicitly log the exception before re-raising it\n _logger.warning(\n \"Error during SSL handshake with %s: %s\", client_address, ex\n )\n raise\n else:\n # Nothing to do, use the raw socket\n client_stream = client_socket\n\n return client_stream, client_address"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nstarts a new thread to process the request.", "response": "def process_request(self, request, client_address):\n \"\"\"\n Starts a new thread to process the request, adding the client address\n in its name.\n \"\"\"\n thread = threading.Thread(\n name=\"RemoteShell-{0}-Client-{1}\".format(\n self.server_address[1], client_address[:2]\n ),\n target=self.process_request_thread,\n args=(request, client_address),\n )\n thread.daemon = self.daemon_threads\n thread.start()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef display_hook(prompt, session, context, matches, longest_match_len):\n # type: (str, ShellSession, BundleContext, List[str], int) -> None\n \"\"\"\n Displays the available services matches and the service details\n\n :param prompt: Shell prompt string\n :param session: Current shell session (for display)\n :param context: BundleContext of the shell\n :param matches: List of words matching the substitution\n :param longest_match_len: Length of the largest match\n \"\"\"\n # Prepare a line pattern for each match (-1 for the trailing space)\n match_pattern = \"{{0: <{}}} from {{1}}\".format(longest_match_len - 1)\n\n # Sort matching names\n matches = sorted(match for match in matches)\n\n # Print the match and the associated name\n session.write_line()\n with use_ipopo(context) as ipopo:\n for factory_name in matches:\n # Remove the spaces added for the completion\n factory_name = factory_name.strip()\n bnd = ipopo.get_factory_bundle(factory_name)\n session.write_line(\n match_pattern, factory_name, bnd.get_symbolic_name()\n )\n\n # Print the prompt, then current line\n session.write(prompt)\n session.write_line_no_feed(readline.get_line_buffer())\n readline.redisplay()", "response": "Display the available services matches and the service details."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef complete(\n self, config, prompt, session, context, current_arguments, current\n ):\n # type: (CompletionInfo, str, ShellSession, BundleContext, List[str], str) -> List[str]\n \"\"\"\n Returns the list of services IDs matching the current state\n\n :param config: Configuration of the current completion\n :param prompt: Shell prompt (for re-display)\n :param session: Shell session (to display in shell)\n :param context: Bundle context of the Shell bundle\n :param current_arguments: Current arguments (without the command itself)\n :param current: Current word\n :return: A list of matches\n \"\"\"\n # Register a method to display helpful completion\n self.set_display_hook(self.display_hook, prompt, session, context)\n\n # Return a list of component factories\n with use_ipopo(context) as ipopo:\n return [\n \"{} \".format(factory)\n for factory in ipopo.get_factories()\n if factory.startswith(current)\n ]", "response": "Completes the current word with the current word."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef display_hook(prompt, session, context, matches, longest_match_len):\n # type: (str, ShellSession, BundleContext, List[str], int) -> None\n \"\"\"\n Displays the available services matches and the service details\n\n :param prompt: Shell prompt string\n :param session: Current shell session (for display)\n :param context: BundleContext of the shell\n :param matches: List of words matching the substitution\n :param longest_match_len: Length of the largest match\n \"\"\"\n # Prepare a line pattern for each match (-1 for the trailing space)\n match_pattern = \"{{0: <{}}} from {{1}}\".format(longest_match_len - 1)\n\n # Sort matching names\n matches = sorted(match for match in matches)\n\n # Print the match and the associated name\n session.write_line()\n with use_ipopo(context) as ipopo:\n for name in matches:\n # Remove the spaces added for the completion\n name = name.strip()\n details = ipopo.get_instance_details(name)\n description = \"of {factory} ({state})\".format(**details)\n session.write_line(match_pattern, name, description)\n\n # Print the prompt, then current line\n session.write(prompt)\n session.write_line_no_feed(readline.get_line_buffer())\n readline.redisplay()", "response": "Display the available services matches and the service details"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncompleting the current state of the current word.", "response": "def complete(\n self, config, prompt, session, context, current_arguments, current\n ):\n # type: (CompletionInfo, str, ShellSession, BundleContext, List[str], str) -> List[str]\n \"\"\"\n Returns the list of services IDs matching the current state\n\n :param config: Configuration of the current completion\n :param prompt: Shell prompt (for re-display)\n :param session: Shell session (to display in shell)\n :param context: Bundle context of the Shell bundle\n :param current_arguments: Current arguments (without the command itself)\n :param current: Current word\n :return: A list of matches\n \"\"\"\n # Register a method to display helpful completion\n self.set_display_hook(self.display_hook, prompt, session, context)\n\n # Return a list of component factories\n with use_ipopo(context) as ipopo:\n return [\n \"{} \".format(name)\n for name, _, _ in ipopo.get_instances()\n if name.startswith(current)\n ]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef complete(\n self, config, prompt, session, context, current_arguments, current\n ):\n # type: (CompletionInfo, str, ShellSession, BundleContext, List[str], str) -> List[str]\n \"\"\"\n Returns the list of services IDs matching the current state\n\n :param config: Configuration of the current completion\n :param prompt: Shell prompt (for re-display)\n :param session: Shell session (to display in shell)\n :param context: Bundle context of the Shell bundle\n :param current_arguments: Current arguments (without the command itself)\n :param current: Current word\n :return: A list of matches\n \"\"\"\n with use_ipopo(context) as ipopo:\n try:\n # Find the factory name\n for idx, completer_id in enumerate(config.completers):\n if completer_id == FACTORY:\n factory_name = current_arguments[idx]\n break\n else:\n # No factory completer found in signature\n for idx, completer_id in enumerate(config.completers):\n if completer_id == COMPONENT:\n name = current_arguments[idx]\n details = ipopo.get_instance_details(name)\n factory_name = details[\"factory\"]\n break\n else:\n # No factory name can be found\n return []\n\n # Get the details about this factory\n details = ipopo.get_factory_details(factory_name)\n properties = details[\"properties\"]\n except (IndexError, ValueError):\n # No/unknown factory name\n return []\n else:\n return [\n \"{}=\".format(key)\n for key in properties\n if key.startswith(current)\n ]", "response": "Returns a list of services IDs that match the current word."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nretrieves the value of a header.", "response": "def get_header(self, name, default=None):\n \"\"\"\n Retrieves the value of a header\n \"\"\"\n return self._handler.headers.get(name, default)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nending the headers part", "response": "def end_headers(self):\n \"\"\"\n Ends the headers part\n \"\"\"\n # Send them all at once\n for name, value in self._headers.items():\n self._handler.send_header(name, value)\n\n self._handler.end_headers()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlog an error message with the specified arguments.", "response": "def log_error(self, message, *args, **kwargs):\n # pylint: disable=W0221\n \"\"\"\n Log server error\n \"\"\"\n self._service.log(logging.ERROR, message, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlogs a request to the server.", "response": "def log_request(self, code=\"-\", size=\"-\"):\n \"\"\"\n Logs a request to the server\n \"\"\"\n self._service.log(logging.DEBUG, '\"%s\" %s', self.requestline, code)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef send_no_servlet_response(self):\n # Use the helper to send the error page\n response = _HTTPServletResponse(self)\n response.send_content(404, self._service.make_not_found_page(self.path))", "response": "Default response sent when no servlet is found for the requested path"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef send_exception(self, response):\n # Get a formatted stack trace\n stack = traceback.format_exc()\n\n # Log the error\n self.log_error(\n \"Error handling request upon: %s\\n%s\\n\", self.path, stack\n )\n\n # Send the page\n response.send_content(\n 500, self._service.make_exception_page(self.path, stack)\n )", "response": "Send an exception page with a 500 error code."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef server_bind(self):\n TCPServer.server_bind(self)\n host, port = self.socket.getsockname()[:2]\n self.server_port = port\n try:\n self.server_name = socket.getfqdn(host)\n except ValueError:\n # Use the local host name in case of error, like CPython does\n self.server_name = socket.gethostname()", "response": "Override server_bind to store the server name even in CPython."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef encode_list(key, list_):\n # type: (str, Iterable) -> Dict[str, str]\n \"\"\"\n Converts a list into a space-separated string and puts it in a dictionary\n\n :param key: Dictionary key to store the list\n :param list_: A list of objects\n :return: A dictionary key->string or an empty dictionary\n \"\"\"\n if not list_:\n return {}\n return {key: \" \".join(str(i) for i in list_)}", "response": "Converts a list into a space - separated string and puts it in a dictionary\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the package name of the given module name", "response": "def package_name(package):\n # type: (str) -> str\n \"\"\"\n Returns the package name of the given module name\n \"\"\"\n if not package:\n return \"\"\n\n lastdot = package.rfind(\".\")\n if lastdot == -1:\n return package\n\n return package[:lastdot]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef encode_osgi_props(ed):\n # type: (EndpointDescription) -> Dict[str, str]\n \"\"\"\n Prepares a dictionary of OSGi properties for the given EndpointDescription\n \"\"\"\n result_props = {}\n intfs = ed.get_interfaces()\n result_props[OBJECTCLASS] = \" \".join(intfs)\n for intf in intfs:\n pkg_name = package_name(intf)\n ver = ed.get_package_version(pkg_name)\n if ver and not ver == (0, 0, 0):\n result_props[ENDPOINT_PACKAGE_VERSION_] = \".\".join(\n str(v) for v in ver\n )\n\n result_props[ENDPOINT_ID] = ed.get_id()\n result_props[ENDPOINT_SERVICE_ID] = \"{0}\".format(ed.get_service_id())\n result_props[ENDPOINT_FRAMEWORK_UUID] = ed.get_framework_uuid()\n imp_configs = ed.get_imported_configs()\n if imp_configs:\n result_props[SERVICE_IMPORTED_CONFIGS] = \" \".join(\n ed.get_imported_configs()\n )\n intents = ed.get_intents()\n if intents:\n result_props[SERVICE_INTENTS] = \" \".join(intents)\n remote_configs = ed.get_remote_configs_supported()\n if remote_configs:\n result_props[REMOTE_CONFIGS_SUPPORTED] = \" \".join(remote_configs)\n remote_intents = ed.get_remote_intents_supported()\n if remote_intents:\n result_props[REMOTE_INTENTS_SUPPORTED] = \" \".join(remote_intents)\n return result_props", "response": "Encodes the properties of the given EndpointDescription into a dictionary of OSGi properties."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndecodes a space - separated list of properties", "response": "def decode_list(input_props, name):\n # type: (Dict[str, str], str) -> List[str]\n \"\"\"\n Decodes a space-separated list\n \"\"\"\n val_str = input_props.get(name, None)\n if val_str:\n return val_str.split(\" \")\n return []"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decode_osgi_props(input_props):\n # type: (Dict[str, Any]) -> Dict[str, Any]\n \"\"\"\n Decodes the OSGi properties of the given endpoint properties\n \"\"\"\n result_props = {}\n intfs = decode_list(input_props, OBJECTCLASS)\n result_props[OBJECTCLASS] = intfs\n for intf in intfs:\n package_key = ENDPOINT_PACKAGE_VERSION_ + package_name(intf)\n intfversionstr = input_props.get(package_key, None)\n if intfversionstr:\n result_props[package_key] = intfversionstr\n result_props[ENDPOINT_ID] = input_props[ENDPOINT_ID]\n result_props[ENDPOINT_SERVICE_ID] = input_props[ENDPOINT_SERVICE_ID]\n result_props[ENDPOINT_FRAMEWORK_UUID] = input_props[ENDPOINT_FRAMEWORK_UUID]\n imp_configs = decode_list(input_props, SERVICE_IMPORTED_CONFIGS)\n if imp_configs:\n result_props[SERVICE_IMPORTED_CONFIGS] = imp_configs\n intents = decode_list(input_props, SERVICE_INTENTS)\n if intents:\n result_props[SERVICE_INTENTS] = intents\n remote_configs = decode_list(input_props, REMOTE_CONFIGS_SUPPORTED)\n if remote_configs:\n result_props[REMOTE_CONFIGS_SUPPORTED] = remote_configs\n remote_intents = decode_list(input_props, REMOTE_INTENTS_SUPPORTED)\n if remote_intents:\n result_props[REMOTE_INTENTS_SUPPORTED] = remote_intents\n return result_props", "response": "Decodes the OSGi properties of the given endpoint properties into a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef decode_endpoint_props(input_props):\n # type: (Dict) -> Dict[str, Any]\n \"\"\"\n Decodes the endpoint properties from the given dictionary\n \"\"\"\n ed_props = decode_osgi_props(input_props)\n ed_props[ECF_ENDPOINT_CONTAINERID_NAMESPACE] = input_props[\n ECF_ENDPOINT_CONTAINERID_NAMESPACE\n ]\n ed_props[ECF_RSVC_ID] = int(input_props[ECF_RSVC_ID])\n ed_props[ECF_ENDPOINT_ID] = input_props[ECF_ENDPOINT_ID]\n ed_props[ECF_ENDPOINT_TIMESTAMP] = int(input_props[ECF_ENDPOINT_TIMESTAMP])\n target_id = input_props.get(ECF_ENDPOINT_CONNECTTARGET_ID, None)\n if target_id:\n ed_props[ECF_ENDPOINT_CONNECTTARGET_ID] = target_id\n id_filters = decode_list(input_props, ECF_ENDPOINT_IDFILTER_IDS)\n if id_filters:\n ed_props[ECF_ENDPOINT_IDFILTER_IDS] = id_filters\n rs_filter = input_props.get(ECF_ENDPOINT_REMOTESERVICE_FILTER, None)\n if rs_filter:\n ed_props[ECF_ENDPOINT_REMOTESERVICE_FILTER] = rs_filter\n async_intfs = input_props.get(ECF_SERVICE_EXPORTED_ASYNC_INTERFACES, None)\n if async_intfs:\n if async_intfs == \"*\":\n ed_props[ECF_SERVICE_EXPORTED_ASYNC_INTERFACES] = async_intfs\n else:\n async_intfs = decode_list(\n input_props, ECF_SERVICE_EXPORTED_ASYNC_INTERFACES\n )\n if async_intfs:\n ed_props[ECF_SERVICE_EXPORTED_ASYNC_INTERFACES] = async_intfs\n\n for key in input_props.keys():\n if not is_reserved_property(key):\n val = input_props.get(key, None)\n if val:\n ed_props[key] = val\n return ed_props", "response": "Decodes the endpoint properties from the given dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef encode_endpoint_props(ed):\n props = encode_osgi_props(ed)\n props[ECF_RSVC_ID] = \"{0}\".format(ed.get_remoteservice_id()[1])\n props[ECF_ENDPOINT_ID] = \"{0}\".format(ed.get_container_id()[1])\n props[ECF_ENDPOINT_CONTAINERID_NAMESPACE] = \"{0}\".format(\n ed.get_container_id()[0]\n )\n props[ECF_ENDPOINT_TIMESTAMP] = \"{0}\".format(ed.get_timestamp())\n ctid = ed.get_connect_target_id()\n if ctid:\n props[ECF_ENDPOINT_CONNECTTARGET_ID] = \"{0}\".format(ctid)\n id_filters = ed.get_id_filters()\n if id_filters:\n props[ECF_ENDPOINT_IDFILTER_IDS] = \" \".join([x[1] for x in id_filters])\n rs_filter = ed.get_remoteservice_filter()\n if rs_filter:\n props[ECF_ENDPOINT_REMOTESERVICE_FILTER] = ed.get_remoteservice_filter()\n async_intfs = ed.get_async_interfaces()\n if async_intfs:\n props[ECF_SERVICE_EXPORTED_ASYNC_INTERFACES] = \" \".join(async_intfs)\n\n all_props = ed.get_properties()\n other_props = {\n key: all_props[key]\n for key in all_props.keys()\n if not is_reserved_property(key)\n }\n return merge_dicts(props, other_props)", "response": "Encodes the properties of the given EndpointDescription into a dictionary of properties."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nproviding the version of the given package name.", "response": "def get_package_version(self, package):\n # type: (str) -> Tuple[int, int, int]\n \"\"\"\n Provides the version of the given package name.\n\n :param package: The name of the package\n :return: The version of the specified package as a tuple or (0,0,0)\n \"\"\"\n name = \"{0}{1}\".format(ENDPOINT_PACKAGE_VERSION_, package)\n try:\n # Get the version string\n version = self._properties[name]\n # Split dots ('.')\n return tuple(version.split(\".\"))\n except KeyError:\n # No version\n return 0, 0, 0"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_same_service(self, endpoint):\n # type: (EndpointDescription) -> bool\n \"\"\"\n Tests if this endpoint and the given one have the same framework UUID\n and service ID\n\n :param endpoint: Another endpoint\n :return: True if both endpoints represent the same remote service\n \"\"\"\n return (\n self.get_framework_uuid() == endpoint.get_framework_uuid()\n and self.get_service_id() == endpoint.get_service_id()\n )", "response": "Tests if this endpoint and the given one have the same framework UUID\n and service ID\n and framework ID\n and service ID\n ."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef generate_id(cls, prefix=\"pelix-\"):\n if not prefix:\n # Normalize string\n prefix = \"\"\n else:\n # Truncate long prefixes\n prefix = prefix[:8]\n\n # Prepare the missing part\n nb_bytes = (23 - len(prefix)) // 2\n\n random_bytes = os.urandom(nb_bytes)\n if sys.version_info[0] >= 3:\n random_ints = [char for char in random_bytes]\n else:\n random_ints = [ord(char) for char in random_bytes]\n\n random_id = \"\".join(\"{0:02x}\".format(value) for value in random_ints)\n return \"{0}{1}\".format(prefix, random_id)", "response": "Generates a random MQTT client ID"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_will(self, topic, payload, qos=0, retain=False):\n self.__mqtt.will_set(topic, payload, qos, retain=retain)", "response": "Sets up the will message for the given topic and payload."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef connect(self, host=\"localhost\", port=1883, keepalive=60):\n # Disconnect first (it also stops the timer)\n self.disconnect()\n\n # Prepare the connection\n self.__mqtt.connect(host, port, keepalive)\n\n # Start the MQTT loop\n self.__mqtt.loop_start()", "response": "Connects to the MQTT server."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef disconnect(self):\n # Stop the timer\n self.__stop_timer()\n\n # Unlock all publishers\n for event in self.__in_flight.values():\n event.set()\n\n # Disconnect from the server\n self.__mqtt.disconnect()\n\n # Stop the MQTT loop thread\n # Use a thread to avoid a dead lock in Paho\n thread = threading.Thread(target=self.__mqtt.loop_stop)\n thread.daemon = True\n thread.start()\n\n # Give it some time\n thread.join(4)", "response": "Disconnects from the MQTT server and unlocks all publishers and queues."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend a message through the MQTT connection and waits for the message to be published.", "response": "def publish(self, topic, payload, qos=0, retain=False, wait=False):\n \"\"\"\n Sends a message through the MQTT connection\n\n :param topic: Message topic\n :param payload: Message content\n :param qos: Quality of Service\n :param retain: Retain flag\n :param wait: If True, prepares an event to wait for the message to be\n published\n :return: The local message ID, None on error\n \"\"\"\n result = self.__mqtt.publish(topic, payload, qos, retain)\n if wait and not result[0]:\n # Publish packet sent, wait for it to return\n self.__in_flight[result[1]] = threading.Event()\n _logger.debug(\"Waiting for publication of %s\", topic)\n\n return result[1]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nstarts the reconnection timer", "response": "def __start_timer(self, delay):\n \"\"\"\n Starts the reconnection timer\n\n :param delay: Delay (in seconds) before calling the reconnection method\n \"\"\"\n self.__timer = threading.Timer(delay, self.__reconnect)\n self.__timer.daemon = True\n self.__timer.start()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nattempting to reconnect to the MQTT server and returns None", "response": "def __reconnect(self):\n \"\"\"\n Tries to connect to the MQTT server\n \"\"\"\n # Cancel the timer, if any\n self.__stop_timer()\n\n try:\n # Try to reconnect the server\n result_code = self.__mqtt.reconnect()\n if result_code:\n # Something wrong happened\n message = \"Error connecting the MQTT server: {0} ({1})\".format(\n result_code, paho.error_string(result_code)\n )\n _logger.error(message)\n raise ValueError(message)\n except Exception as ex:\n # Something went wrong: log it\n _logger.error(\"Exception connecting server: %s\", ex)\n finally:\n # Prepare a reconnection timer. It will be cancelled by the\n # on_connect callback\n self.__start_timer(10)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __on_connect(self, client, userdata, flags, result_code):\n # pylint: disable=W0613\n \"\"\"\n Client connected to the server\n\n :param client: Connected Paho client\n :param userdata: User data (unused)\n :param flags: Response flags sent by the broker\n :param result_code: Connection result code (0: success, others: error)\n \"\"\"\n if result_code:\n # result_code != 0: something wrong happened\n _logger.error(\n \"Error connecting the MQTT server: %s (%d)\",\n paho.connack_string(result_code),\n result_code,\n )\n else:\n # Connection is OK: stop the reconnection timer\n self.__stop_timer()\n\n # Notify the caller, if any\n if self.on_connect is not None:\n try:\n self.on_connect(self, result_code)\n except Exception as ex:\n _logger.exception(\"Error notifying MQTT listener: %s\", ex)", "response": "Callback function called by the broker when a connection is made."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __on_message(self, client, userdata, msg):\n # pylint: disable=W0613\n \"\"\"\n A message has been received from a server\n\n :param client: Client that received the message\n :param userdata: User data (unused)\n :param msg: A MQTTMessage bean\n \"\"\"\n # Notify the caller, if any\n if self.on_message is not None:\n try:\n self.on_message(self, msg)\n except Exception as ex:\n _logger.exception(\"Error notifying MQTT listener: %s\", ex)", "response": "Called by the client when a message is received from a server."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall when a message is published by a server.", "response": "def __on_publish(self, client, userdata, mid):\n # pylint: disable=W0613\n \"\"\"\n A message has been published by a server\n\n :param client: Client that received the message\n :param userdata: User data (unused)\n :param mid: Message ID\n \"\"\"\n try:\n self.__in_flight[mid].set()\n except KeyError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef to_import_properties(properties):\n # type: (dict) -> dict\n \"\"\"\n Returns a dictionary where export properties have been replaced by import\n ones\n\n :param properties: A dictionary of service properties (with export keys)\n :return: A dictionary with import properties\n \"\"\"\n # Copy the given dictionary\n props = properties.copy()\n\n # Add the \"imported\" property\n props[pelix.remote.PROP_IMPORTED] = True\n\n # Remote service ID\n try:\n props[pelix.remote.PROP_ENDPOINT_SERVICE_ID] = props.pop(\n pelix.constants.SERVICE_ID\n )\n except KeyError:\n # No service ID\n pass\n\n # Replace the \"export configs\"\n configs = props.pop(pelix.remote.PROP_EXPORTED_CONFIGS, None)\n if configs:\n props[pelix.remote.PROP_IMPORTED_CONFIGS] = configs\n\n # Clear other export properties\n for key in (\n pelix.remote.PROP_EXPORTED_INTENTS,\n pelix.remote.PROP_EXPORTED_INTENTS_EXTRA,\n pelix.remote.PROP_EXPORTED_INTERFACES,\n ):\n try:\n del props[key]\n except KeyError:\n # Key wasn't there\n pass\n\n return props", "response": "Converts a dictionary of export properties to a dictionary of service properties that have been replaced by import properties."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef compute_exported_specifications(svc_ref):\n # type: (pelix.framework.ServiceReference) -> List[str]\n \"\"\"\n Computes the list of specifications exported by the given service\n\n :param svc_ref: A ServiceReference\n :return: The list of exported specifications (or an empty list)\n \"\"\"\n if svc_ref.get_property(pelix.remote.PROP_EXPORT_NONE):\n # The export of this service is explicitly forbidden, stop here\n return []\n\n # Service specifications\n specs = svc_ref.get_property(pelix.constants.OBJECTCLASS)\n\n # Exported specifications\n exported_specs = svc_ref.get_property(pelix.remote.PROP_EXPORTED_INTERFACES)\n\n if exported_specs and exported_specs != \"*\":\n # A set of specifications is exported, replace \"objectClass\"\n iterable_exports = pelix.utilities.to_iterable(exported_specs, False)\n all_exported_specs = [\n spec for spec in specs if spec in iterable_exports\n ]\n else:\n # Export everything\n all_exported_specs = pelix.utilities.to_iterable(specs)\n\n # Authorized and rejected specifications\n export_only_specs = pelix.utilities.to_iterable(\n svc_ref.get_property(pelix.remote.PROP_EXPORT_ONLY), False\n )\n\n if export_only_specs:\n # Filter specifications (keep authorized specifications)\n return [\n spec for spec in all_exported_specs if spec in export_only_specs\n ]\n\n # Filter specifications (reject)\n rejected_specs = pelix.utilities.to_iterable(\n svc_ref.get_property(pelix.remote.PROP_EXPORT_REJECT), False\n )\n return [spec for spec in all_exported_specs if spec not in rejected_specs]", "response": "Computes the list of exported specifications for the given service"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconvert python:/ name specifications to name. Keeps the other specifications as is.", "response": "def extract_specifications(specifications, properties):\n # type: (Any[str, List[str]], dict) -> List[str]\n \"\"\"\n Converts \"python:/name\" specifications to \"name\". Keeps the other\n specifications as is.\n\n :param specifications: The specifications found in a remote registration\n :param properties: Service properties\n :return: The filtered specifications (as a list)\n \"\"\"\n all_specs = set(pelix.utilities.to_iterable(specifications))\n try:\n synonyms = pelix.utilities.to_iterable(\n properties[pelix.remote.PROP_SYNONYMS], False\n )\n all_specs.update(synonyms)\n except KeyError:\n # No synonyms property\n pass\n\n filtered_specs = set()\n for original in all_specs:\n try:\n # Extract information\n lang, spec = _extract_specification_parts(original)\n if lang == PYTHON_LANGUAGE:\n # Language match: keep the name only\n filtered_specs.add(spec)\n else:\n # Keep the name as is\n filtered_specs.add(original)\n except ValueError:\n # Ignore invalid specifications\n pass\n\n return list(filtered_specs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntransform the interfaces names into URI strings with the interface implementation language as a scheme.", "response": "def format_specifications(specifications):\n # type: (Iterable[str]) -> List[str]\n \"\"\"\n Transforms the interfaces names into URI strings, with the interface\n implementation language as a scheme.\n\n :param specifications: Specifications to transform\n :return: The transformed names\n \"\"\"\n transformed = set()\n for original in specifications:\n try:\n lang, spec = _extract_specification_parts(original)\n transformed.add(_format_specification(lang, spec))\n except ValueError:\n # Ignore invalid specifications\n pass\n\n return list(transformed)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _extract_specification_parts(specification):\n # type: (str) -> Tuple[str, str]\n \"\"\"\n Extract the language and the interface from a \"language:/interface\"\n interface name\n\n :param specification: The formatted interface name\n :return: A (language, interface name) tuple\n :raise ValueError: Invalid specification content\n \"\"\"\n try:\n # Parse the URI-like string\n parsed = urlparse(specification)\n except:\n # Invalid URL\n raise ValueError(\"Invalid specification URL: {0}\".format(specification))\n\n # Extract the interface name\n interface = parsed.path\n\n # Extract the language, if given\n language = parsed.scheme\n if not language:\n # Simple name, without scheme\n language = PYTHON_LANGUAGE\n else:\n # Formatted name: un-escape it, without the starting '/'\n interface = _unescape_specification(interface[1:])\n\n return language, interface", "response": "Extracts the language and interface from a language - specific specification."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn merged properties with the properties of the current object class and service ID.", "response": "def get_properties(self):\n # type: () -> dict\n \"\"\"\n Returns merged properties\n\n :return: Endpoint merged properties\n \"\"\"\n # Get service properties\n properties = self.__reference.get_properties()\n\n # Merge with local properties\n properties.update(self.__properties)\n\n # Some properties can't be merged\n for key in pelix.constants.OBJECTCLASS, pelix.constants.SERVICE_ID:\n properties[key] = self.__reference.get_property(key)\n\n # Force the exported configurations\n properties[pelix.remote.PROP_EXPORTED_CONFIGS] = self.configurations\n\n return properties"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_import_properties(self):\n # type: () -> dict\n \"\"\"\n Returns the properties of this endpoint where export properties have\n been replaced by import ones\n\n :return: A dictionary with import properties\n \"\"\"\n # Convert merged properties\n props = to_import_properties(self.get_properties())\n\n # Add the framework UID\n props[pelix.remote.PROP_ENDPOINT_FRAMEWORK_UUID] = self.__fw_uid\n return props", "response": "Returns the properties of this endpoint where export properties have\n been replaced by import ones"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck that the given dictionary doesn t have export keys and has import keys.", "response": "def __check_properties(props):\n # type: (dict) -> None\n \"\"\"\n Checks that the given dictionary doesn't have export keys and has\n import keys\n\n :param props: Properties to validate\n :raise ValueError: Invalid properties\n \"\"\"\n # Mandatory properties\n mandatory = (\n pelix.remote.PROP_ENDPOINT_ID,\n pelix.remote.PROP_IMPORTED_CONFIGS,\n pelix.constants.OBJECTCLASS,\n )\n for key in mandatory:\n if key not in props:\n raise ValueError(\"Missing property: {0}\".format(key))\n\n # Export/Import properties\n props_export = (\n pelix.remote.PROP_EXPORTED_CONFIGS,\n pelix.remote.PROP_EXPORTED_INTERFACES,\n )\n\n for key in props_export:\n if key in props:\n raise ValueError(\"Export property found: {0}\".format(key))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprovides the version of the given package name.", "response": "def get_package_version(self, package):\n # type: (str) -> Tuple[int, ...]\n \"\"\"\n Provides the version of the given package name.\n\n :param package: The name of the package\n :return: The version of the specified package as a tuple or (0,0,0)\n \"\"\"\n name = \"{0}{1}\".format(\n pelix.remote.PROP_ENDPOINT_PACKAGE_VERSION_, package\n )\n try:\n # Get the version string\n version = self.__properties[name]\n\n # Split dots ('.')\n return tuple(version.split(\".\"))\n except KeyError:\n # No version\n return 0, 0, 0"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef matches(self, ldap_filter):\n # type: (Any[str, pelix.ldapfilter.LDAPFilter]) -> bool\n \"\"\"\n Tests the properties of this EndpointDescription against the given\n filter\n\n :param ldap_filter: A filter\n :return: True if properties matches the filter\n \"\"\"\n return pelix.ldapfilter.get_ldap_filter(ldap_filter).matches(\n self.__properties\n )", "response": "Tests if the given ldap_filter matches the properties of this EndpointDescription."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef to_import(self):\n # type: () -> ImportEndpoint\n \"\"\"\n Converts an EndpointDescription bean to an ImportEndpoint\n\n :return: An ImportEndpoint bean\n \"\"\"\n # Properties\n properties = self.get_properties()\n\n # Framework UUID\n fw_uid = self.get_framework_uuid()\n\n # Endpoint name\n try:\n # From Pelix UID\n name = properties[pelix.remote.PROP_ENDPOINT_NAME]\n except KeyError:\n # Generated\n name = \"{0}.{1}\".format(fw_uid, self.get_service_id())\n\n # Configuration / kind\n configurations = self.get_configuration_types()\n\n # Interfaces\n specifications = self.get_interfaces()\n\n return ImportEndpoint(\n self.get_id(),\n fw_uid,\n configurations,\n name,\n specifications,\n properties,\n )", "response": "Converts an EndpointDescription bean to an ImportEndpoint bean"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts an ExportEndpoint bean to an EndpointDescription object.", "response": "def from_export(cls, endpoint):\n # type: (ExportEndpoint) -> EndpointDescription\n \"\"\"\n Converts an ExportEndpoint bean to an EndpointDescription\n\n :param endpoint: An ExportEndpoint bean\n :return: An EndpointDescription bean\n \"\"\"\n assert isinstance(endpoint, ExportEndpoint)\n\n # Service properties\n properties = endpoint.get_properties()\n\n # Set import keys\n properties[pelix.remote.PROP_ENDPOINT_ID] = endpoint.uid\n properties[pelix.remote.PROP_IMPORTED_CONFIGS] = endpoint.configurations\n properties[\n pelix.remote.PROP_EXPORTED_INTERFACES\n ] = endpoint.specifications\n\n # Remove export keys\n for key in (\n pelix.remote.PROP_EXPORTED_CONFIGS,\n pelix.remote.PROP_EXPORTED_INTERFACES,\n pelix.remote.PROP_EXPORTED_INTENTS,\n pelix.remote.PROP_EXPORTED_INTENTS_EXTRA,\n ):\n try:\n del properties[key]\n except KeyError:\n pass\n\n # Other information\n properties[pelix.remote.PROP_ENDPOINT_NAME] = endpoint.name\n properties[\n pelix.remote.PROP_ENDPOINT_FRAMEWORK_UUID\n ] = endpoint.framework\n\n return EndpointDescription(None, properties)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _parse_description(self, node):\n # type: (ElementTree.Element) -> EndpointDescription\n \"\"\"\n Parse an endpoint description node\n\n :param node: The endpoint description node\n :return: The parsed EndpointDescription bean\n :raise KeyError: Attribute missing\n :raise ValueError: Invalid description\n \"\"\"\n endpoint = {}\n for prop_node in node.findall(TAG_PROPERTY):\n name, value = self._parse_property(prop_node)\n endpoint[name] = value\n\n return EndpointDescription(None, endpoint)", "response": "Parses an endpoint description node and returns a EndpointDescription object"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse a property node and returns a name and value tuple.", "response": "def _parse_property(self, node):\n # type: (ElementTree.Element) -> Tuple[str, Any]\n \"\"\"\n Parses a property node\n\n :param node: The property node\n :return: A (name, value) tuple\n :raise KeyError: Attribute missing\n \"\"\"\n # Get information\n name = node.attrib[ATTR_NAME]\n vtype = node.attrib.get(ATTR_VALUE_TYPE, TYPE_STRING)\n\n # Look for a value as a single child node\n try:\n value_node = next(iter(node))\n value = self._parse_value_node(vtype, value_node)\n except StopIteration:\n # Value is an attribute\n value = self._convert_value(vtype, node.attrib[ATTR_VALUE])\n\n return name, value"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_value_node(self, vtype, node):\n # type: (str, ElementTree.Element) -> Any\n \"\"\"\n Parses a value node\n\n :param vtype: The value type\n :param node: The value node\n :return: The parsed value\n \"\"\"\n kind = node.tag\n if kind == TAG_XML:\n # Raw XML value\n return next(iter(node))\n\n elif kind == TAG_LIST or kind == TAG_ARRAY:\n # List\n return [\n self._convert_value(vtype, value_node.text)\n for value_node in node.findall(TAG_VALUE)\n ]\n\n elif kind == TAG_SET:\n # Set\n return set(\n self._convert_value(vtype, value_node.text)\n for value_node in node.findall(TAG_VALUE)\n )\n\n else:\n # Unknown\n raise ValueError(\"Unknown value tag: {0}\".format(kind))", "response": "Parses a value node and returns the parsed value"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing an EDEF XML string containing an EndpointDescription.", "response": "def parse(self, xml_str):\n # type: (str) -> List[EndpointDescription]\n \"\"\"\n Parses an EDEF XML string\n\n :param xml_str: An XML string\n :return: The list of parsed EndpointDescription\n \"\"\"\n # Parse the document\n root = ElementTree.fromstring(xml_str)\n if root.tag != TAG_ENDPOINT_DESCRIPTIONS:\n raise ValueError(\"Not an EDEF XML: {0}\".format(root.tag))\n\n # Parse content\n return [\n self._parse_description(node)\n for node in root.findall(TAG_ENDPOINT_DESCRIPTION)\n ]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _make_endpoint(self, root_node, endpoint):\n # type: (ElementTree.Element, EndpointDescription) -> None\n \"\"\"\n Converts the given endpoint bean to an XML Element\n\n :param root_node: The XML root Element\n :param endpoint: An EndpointDescription bean\n \"\"\"\n endpoint_node = ElementTree.SubElement(\n root_node, TAG_ENDPOINT_DESCRIPTION\n )\n\n for name, value in endpoint.get_properties().items():\n # Compute value type\n vtype = self._get_type(name, value)\n\n # Prepare the property node\n prop_node = ElementTree.SubElement(\n endpoint_node, TAG_PROPERTY, {ATTR_NAME: name}\n )\n\n if vtype == XML_VALUE:\n # Special case, we have to store the value as a child\n # without a value-type attribute\n prop_node.append(value)\n continue\n\n # Set the value type\n prop_node.set(ATTR_VALUE_TYPE, vtype)\n\n # Compute value node or attribute\n if isinstance(value, tuple):\n # Array\n self._add_container(prop_node, TAG_ARRAY, value)\n\n elif isinstance(value, list):\n # List\n self._add_container(prop_node, TAG_ARRAY, value)\n\n elif isinstance(value, set):\n # Set\n self._add_container(prop_node, TAG_SET, value)\n\n elif isinstance(value, type(root_node)):\n # XML (direct addition)\n prop_node.append(value)\n\n else:\n # Simple value -> Attribute\n prop_node.set(ATTR_VALUE, str(value))", "response": "Converts the given endpoint bean to an XML Element"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting the given endpoint description beans into an XML Element containing the XML document.", "response": "def _make_xml(self, endpoints):\n # type: (List[EndpointDescription]) -> ElementTree.Element\n \"\"\"\n Converts the given endpoint description beans into an XML Element\n\n :param endpoints: A list of EndpointDescription beans\n :return: A string containing an XML document\n \"\"\"\n root = ElementTree.Element(TAG_ENDPOINT_DESCRIPTIONS)\n for endpoint in endpoints:\n self._make_endpoint(root, endpoint)\n\n # Prepare pretty-printing\n self._indent(root)\n return root"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef write(self, endpoints, filename):\n # type: (List[EndpointDescription], str) -> None\n \"\"\"\n Writes the given endpoint descriptions to the given file\n\n :param endpoints: A list of EndpointDescription beans\n :param filename: Name of the file where to write the XML\n :raise IOError: Error writing the file\n \"\"\"\n with open(filename, \"w\") as filep:\n filep.write(self.to_string(endpoints))", "response": "Writes the given endpoint descriptions to the given file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntesting if the current attribute value is shared by a parent of the given base class.", "response": "def is_from_parent(cls, attribute_name, value=None):\n # type: (type, str, bool) -> bool\n \"\"\"\n Tests if the current attribute value is shared by a parent of the given\n class.\n\n Returns None if the attribute value is None.\n\n :param cls: Child class with the requested attribute\n :param attribute_name: Name of the attribute to be tested\n :param value: The exact value in the child class (optional)\n :return: True if the attribute value is shared with a parent class\n \"\"\"\n if value is None:\n try:\n # Get the current value\n value = getattr(cls, attribute_name)\n except AttributeError:\n # No need to go further: the attribute does not exist\n return False\n\n for base in cls.__bases__:\n # Look for the value in each parent class\n try:\n return getattr(base, attribute_name) is value\n except AttributeError:\n pass\n\n # Attribute value not found in parent classes\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the factory context object associated to a factory class. Creates it if needed.", "response": "def get_factory_context(cls):\n # type: (type) -> FactoryContext\n \"\"\"\n Retrieves the factory context object associated to a factory. Creates it\n if needed\n\n :param cls: The factory class\n :return: The factory class context\n \"\"\"\n context = getattr(cls, constants.IPOPO_FACTORY_CONTEXT, None)\n\n if context is None:\n # Class not yet manipulated\n context = FactoryContext()\n elif is_from_parent(cls, constants.IPOPO_FACTORY_CONTEXT):\n # Create a copy the context\n context = context.copy(True)\n # * Manipulation has not been applied yet\n context.completed = False\n else:\n # Nothing special to do\n return context\n\n # Context has been created or copied, inject the new bean\n setattr(cls, constants.IPOPO_FACTORY_CONTEXT, context)\n return context"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_method_description(method):\n # type: (Callable) -> str\n \"\"\"\n Retrieves a description of the given method. If possible, the description\n contains the source file name and line.\n\n :param method: A method\n :return: A description of the method (at least its name)\n :raise AttributeError: Given object has no __name__ attribute\n \"\"\"\n try:\n try:\n line_no = inspect.getsourcelines(method)[1]\n except IOError:\n # Error reading the source file\n line_no = -1\n\n return \"'{method}' ({file}:{line})\".format(\n method=method.__name__, file=inspect.getfile(method), line=line_no\n )\n except TypeError:\n # Method can't be inspected\n return \"'{0}'\".format(method.__name__)", "response": "Returns a description of the given method."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntest if the decorated method has sufficient number of parameters.", "response": "def validate_method_arity(method, *needed_args):\n # type: (Callable, *str) -> None\n \"\"\"\n Tests if the decorated method has a sufficient number of parameters.\n\n :param method: The method to be tested\n :param needed_args: The name (for description only) of the needed\n arguments, without \"self\".\n :return: Nothing\n :raise TypeError: Invalid number of parameter\n \"\"\"\n nb_needed_args = len(needed_args)\n\n # Test the number of parameters\n arg_spec = get_method_arguments(method)\n method_args = arg_spec.args\n\n try:\n # Remove the self argument when present\n if method_args[0] == \"self\":\n del method_args[0]\n except IndexError:\n pass\n\n nb_args = len(method_args)\n\n if arg_spec.varargs is not None:\n # Variable arguments\n if nb_args != 0:\n # Other arguments detected\n raise TypeError(\n \"When using '*args', the decorated {0} method must only \"\n \"accept the 'self' argument\".format(\n get_method_description(method)\n )\n )\n elif arg_spec.keywords is not None:\n raise TypeError(\"Methods using '**kwargs' are not handled\")\n elif nb_args != nb_needed_args:\n # \"Normal\" arguments\n raise TypeError(\n \"The decorated method {0} must accept exactly {1} parameters: \"\n \"(self, {2})\".format(\n get_method_description(method),\n nb_needed_args + 1,\n \", \".join(needed_args),\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _ipopo_setup_callback(cls, context):\n # type: (type, FactoryContext) -> None\n \"\"\"\n Sets up the class _callback dictionary\n\n :param cls: The class to handle\n :param context: The factory class context\n \"\"\"\n assert inspect.isclass(cls)\n assert isinstance(context, FactoryContext)\n\n if context.callbacks is not None:\n callbacks = context.callbacks.copy()\n else:\n callbacks = {}\n\n functions = inspect.getmembers(cls, inspect.isroutine)\n\n for _, func in functions:\n if not hasattr(func, constants.IPOPO_METHOD_CALLBACKS):\n # No attribute, get the next member\n continue\n\n method_callbacks = getattr(func, constants.IPOPO_METHOD_CALLBACKS)\n if not isinstance(method_callbacks, list):\n # Invalid content\n _logger.warning(\n \"Invalid callback information %s in %s\",\n constants.IPOPO_METHOD_CALLBACKS,\n get_method_description(func),\n )\n continue\n\n # Keeping it allows inheritance : by removing it, only the first\n # child will see the attribute -> Don't remove it\n\n # Store the call backs\n for _callback in method_callbacks:\n if _callback in callbacks and not is_from_parent(\n cls, callbacks[_callback].__name__, callbacks[_callback]\n ):\n _logger.warning(\n \"Redefining the callback %s in class '%s'.\\n\"\n \"\\tPrevious callback : %s\\n\"\n \"\\tNew callback : %s\",\n _callback,\n cls.__name__,\n get_method_description(callbacks[_callback]),\n get_method_description(func),\n )\n\n callbacks[_callback] = func\n\n # Update the factory context\n context.callbacks.clear()\n context.callbacks.update(callbacks)", "response": "Sets up the class _callback dictionary for the class cls."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset up the class _field_callback dictionary for the class cls.", "response": "def _ipopo_setup_field_callback(cls, context):\n # type: (type, FactoryContext) -> None\n \"\"\"\n Sets up the class _field_callback dictionary\n\n :param cls: The class to handle\n :param context: The factory class context\n \"\"\"\n assert inspect.isclass(cls)\n assert isinstance(context, FactoryContext)\n\n if context.field_callbacks is not None:\n callbacks = context.field_callbacks.copy()\n else:\n callbacks = {}\n\n functions = inspect.getmembers(cls, inspect.isroutine)\n for name, func in functions:\n if not hasattr(func, constants.IPOPO_METHOD_FIELD_CALLBACKS):\n # No attribute, get the next member\n continue\n\n method_callbacks = getattr(func, constants.IPOPO_METHOD_FIELD_CALLBACKS)\n if not isinstance(method_callbacks, list):\n # Invalid content\n _logger.warning(\n \"Invalid attribute %s in %s\",\n constants.IPOPO_METHOD_FIELD_CALLBACKS,\n name,\n )\n continue\n\n # Keeping it allows inheritance : by removing it, only the first\n # child will see the attribute -> Don't remove it\n\n # Store the call backs\n for kind, field, if_valid in method_callbacks:\n fields_cbs = callbacks.setdefault(field, {})\n\n if kind in fields_cbs and not is_from_parent(\n cls, fields_cbs[kind][0].__name__\n ):\n _logger.warning(\n \"Redefining the callback %s in '%s'. \"\n \"Previous callback : '%s' (%s). \"\n \"New callback : %s\",\n kind,\n name,\n fields_cbs[kind][0].__name__,\n fields_cbs[kind][0],\n func,\n )\n\n fields_cbs[kind] = (func, if_valid)\n\n # Update the factory context\n context.field_callbacks.clear()\n context.field_callbacks.update(callbacks)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _append_object_entry(obj, list_name, entry):\n # type: (Any, str, Any) -> None\n \"\"\"\n Appends the given entry in the given object list.\n Creates the list field if needed.\n\n :param obj: The object that contains the list\n :param list_name: The name of the list member in *obj*\n :param entry: The entry to be added to the list\n :raise ValueError: Invalid attribute content\n \"\"\"\n # Get the list\n obj_list = getattr(obj, list_name, None)\n if obj_list is None:\n # We'll have to create it\n obj_list = []\n setattr(obj, list_name, obj_list)\n\n assert isinstance(obj_list, list)\n\n # Set up the property, if needed\n if entry not in obj_list:\n obj_list.append(entry)", "response": "Adds the given entry to the given object list."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a property that will be set on the iPOPO class field with the given name and default value.", "response": "def _ipopo_class_field_property(name, value, methods_prefix):\n # type: (str, Any, str) -> property\n \"\"\"\n Sets up an iPOPO field property, using Python property() capabilities\n\n :param name: The property name\n :param value: The property default value\n :param methods_prefix: The common prefix of the getter and setter injected\n methods\n :return: A generated Python property()\n \"\"\"\n # The property lock\n lock = threading.RLock()\n\n # Prepare the methods names\n getter_name = \"{0}{1}\".format(methods_prefix, constants.IPOPO_GETTER_SUFFIX)\n setter_name = \"{0}{1}\".format(methods_prefix, constants.IPOPO_SETTER_SUFFIX)\n\n local_holder = Holder(value)\n\n def get_value(self):\n \"\"\"\n Retrieves the property value, from the iPOPO dictionaries\n \"\"\"\n getter = getattr(self, getter_name, None)\n if getter is not None:\n # Use the component getter\n with lock:\n return getter(self, name)\n else:\n # Use the local holder\n return local_holder.value\n\n def set_value(self, new_value):\n \"\"\"\n Sets the property value and trigger an update event\n\n :param new_value: The new property value\n \"\"\"\n setter = getattr(self, setter_name, None)\n if setter is not None:\n # Use the component setter\n with lock:\n setter(self, name, new_value)\n else:\n # Change the local holder\n local_holder.value = new_value\n\n return property(get_value, set_value)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_specifications(specifications):\n if not specifications or specifications is object:\n raise ValueError(\"No specifications given\")\n elif inspect.isclass(specifications):\n if Provides.USE_MODULE_QUALNAME:\n if sys.version_info < (3, 3, 0):\n raise ValueError(\n \"Qualified name capability requires Python 3.3+\"\n )\n\n # Get the name of the class\n if not specifications.__module__:\n return [specifications.__qualname__]\n\n return [\n \"{0}.{1}\".format(\n specifications.__module__, specifications.__qualname__\n )\n ]\n else:\n # Legacy behavior\n return [specifications.__name__]\n elif is_string(specifications):\n # Specification name\n specifications = specifications.strip()\n if not specifications:\n raise ValueError(\"Empty specification given\")\n return [specifications]\n elif isinstance(specifications, (list, tuple)):\n # List given: normalize its content\n results = []\n for specification in specifications:\n results.extend(_get_specifications(specification))\n return results\n else:\n raise ValueError(\n \"Unhandled specifications type : {0}\".format(\n type(specifications).__name__\n )\n )", "response": "Returns the list of strings corresponding to the given specifications."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef Bind(method):\n # pylint: disable=C0103\n \"\"\"\n The ``@Bind`` callback decorator is called when a component is bound to a\n dependency.\n\n The decorated method must accept the injected service object and its\n :class:`~pelix.framework.ServiceReference` as arguments::\n\n @Bind\n def bind_method(self, service, service_reference):\n '''\n service: The injected service instance.\n service_reference: The injected service ServiceReference\n '''\n # ...\n\n If the service is a required one, the bind callback is called **before**\n the component is validated.\n\n The service reference can be stored *if it is released on unbind*.\n\n Exceptions raised by a bind callback are ignored.\n\n :param method: The decorated method\n :raise TypeError: The decorated element is not a valid function\n \"\"\"\n if not inspect.isroutine(method):\n raise TypeError(\"@Bind can only be applied on functions\")\n\n # Tests the number of parameters\n validate_method_arity(method, \"service\", \"service_reference\")\n\n _append_object_entry(\n method, constants.IPOPO_METHOD_CALLBACKS, constants.IPOPO_CALLBACK_BIND\n )\n return method", "response": "Decorator for binding a component to a base class."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef Unbind(method):\n # pylint: disable=C0103\n \"\"\"\n The ``@Unbind`` callback decorator is called when a component dependency is\n unbound.\n\n The decorated method must accept the injected service object and its\n :class:`~pelix.framework.ServiceReference` as arguments::\n\n @Unbind\n def unbind_method(self, service, service_reference):\n '''\n service: The previously injected service instance.\n service_reference: Its ServiceReference\n '''\n # ...\n\n If the service is a required one, the unbind callback is called **after**\n the component has been invalidated.\n\n Exceptions raised by an unbind callback are ignored.\n\n :param method: The decorated method\n :raise TypeError: The decorated element is not a valid function\n \"\"\"\n if not isinstance(method, types.FunctionType):\n raise TypeError(\"@Unbind can only be applied on functions\")\n\n # Tests the number of parameters\n validate_method_arity(method, \"service\", \"service_reference\")\n\n _append_object_entry(\n method,\n constants.IPOPO_METHOD_CALLBACKS,\n constants.IPOPO_CALLBACK_UNBIND,\n )\n return method", "response": "Decorator to mark a method as unbound."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef PostRegistration(method):\n # pylint: disable=C0103\n \"\"\"\n The service post-registration callback decorator is called after a service\n of the component has been registered to the framework.\n\n The decorated method must accept the\n :class:`~pelix.framework.ServiceReference` of the registered\n service as argument::\n\n @PostRegistration\n def callback_method(self, service_reference):\n '''\n service_reference: The ServiceReference of the provided service\n '''\n # ...\n\n :param method: The decorated method\n :raise TypeError: The decorated element is not a valid function\n \"\"\"\n if not isinstance(method, types.FunctionType):\n raise TypeError(\"@PostRegistration can only be applied on functions\")\n\n # Tests the number of parameters\n validate_method_arity(method, \"service_reference\")\n _append_object_entry(\n method,\n constants.IPOPO_METHOD_CALLBACKS,\n constants.IPOPO_CALLBACK_POST_REGISTRATION,\n )\n return method", "response": "Decorator for registering a new service with the framework."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bundlestate_to_str(state):\n states = {\n pelix.Bundle.INSTALLED: \"INSTALLED\",\n pelix.Bundle.ACTIVE: \"ACTIVE\",\n pelix.Bundle.RESOLVED: \"RESOLVED\",\n pelix.Bundle.STARTING: \"STARTING\",\n pelix.Bundle.STOPPING: \"STOPPING\",\n pelix.Bundle.UNINSTALLED: \"UNINSTALLED\",\n }\n\n return states.get(state, \"Unknown state ({0})\".format(state))", "response": "Converts a bundle state integer to a string"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating an ASCII table according to the given headers and lines.", "response": "def make_table(headers, lines, prefix=None):\n \"\"\"\n Generates an ASCII table according to the given headers and lines\n\n :param headers: List of table headers (N-tuple)\n :param lines: List of table lines (N-tuples)\n :param prefix: Optional prefix for each line\n :return: The ASCII representation of the table\n :raise ValueError: Different number of columns between headers and\n lines\n \"\"\"\n # Normalize the prefix\n prefix = str(prefix or \"\")\n\n # Maximum lengths\n lengths = [len(title) for title in headers]\n\n # Store the number of columns (0-based)\n nb_columns = len(lengths) - 1\n\n # Lines\n str_lines = []\n for idx, line in enumerate(lines):\n # Recompute lengths\n str_line = []\n str_lines.append(str_line)\n column = -1\n\n try:\n for column, entry in enumerate(line):\n str_entry = str(entry)\n str_line.append(str_entry)\n\n if len(str_entry) > lengths[column]:\n lengths[column] = len(str_entry)\n\n except IndexError:\n # Line too small/big\n raise ValueError(\n \"Different sizes for header and lines \"\n \"(line {0})\".format(idx + 1)\n )\n\n except (TypeError, AttributeError):\n # Invalid type of line\n raise ValueError(\n \"Invalid type of line: %s\", type(line).__name__\n )\n\n else:\n if column != nb_columns:\n # Check if all lines have the same number of columns\n raise ValueError(\n \"Different sizes for header and lines \"\n \"(line {0})\".format(idx + 1)\n )\n\n # Prepare the head (centered text)\n format_str = \"{0}|\".format(prefix)\n for column, length in enumerate(lengths):\n format_str += \" {%d:^%d} |\" % (column, length)\n\n head_str = format_str.format(*headers)\n\n # Prepare the separator, according the length of the headers string\n separator = \"{0}{1}\".format(prefix, \"-\" * (len(head_str) - len(prefix)))\n idx = head_str.find(\"|\")\n while idx != -1:\n separator = \"+\".join((separator[:idx], separator[idx + 1 :]))\n idx = head_str.find(\"|\", idx + 1)\n\n # Prepare the output\n output = [separator, head_str, separator.replace(\"-\", \"=\")]\n\n # Compute the lines\n format_str = format_str.replace(\"^\", \"<\")\n for line in str_lines:\n output.append(format_str.format(*line))\n output.append(separator)\n\n # Force the last end of line\n output.append(\"\")\n\n # Join'em\n return \"\\n\".join(output)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef bind_handler(self, svc_ref):\n if svc_ref in self._bound_references:\n # Already bound service\n return False\n\n # Get the service\n handler = self._context.get_service(svc_ref)\n\n # Get its name space\n namespace = handler.get_namespace()\n commands = []\n\n # Register all service methods directly\n for command, method in handler.get_methods():\n self.register_command(namespace, command, method)\n commands.append(command)\n\n # Store the reference\n self._bound_references[svc_ref] = handler\n self._reference_commands[svc_ref] = (namespace, commands)\n return True", "response": "Called if a command service has been found. Registers the methods of this service."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef unbind_handler(self, svc_ref):\n if svc_ref not in self._bound_references:\n # Unknown reference\n return False\n\n # Unregister its commands\n namespace, commands = self._reference_commands[svc_ref]\n for command in commands:\n self.unregister(namespace, command)\n\n # Release the service\n self._context.unget_service(svc_ref)\n del self._bound_references[svc_ref]\n del self._reference_commands[svc_ref]\n return True", "response": "Unregisters the commands for a specific service."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef var_set(self, session, **kwargs):\n if not kwargs:\n session.write_line(\n self._utils.make_table(\n (\"Name\", \"Value\"), session.variables.items()\n )\n )\n else:\n for name, value in kwargs.items():\n name = name.strip()\n session.set(name, value)\n session.write_line(\"{0}={1}\", name, value)", "response": "set answer = 42"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef bundle_details(self, io_handler, bundle_id):\n bundle = None\n\n try:\n # Convert the given ID into an integer\n bundle_id = int(bundle_id)\n except ValueError:\n # Not an integer, suppose it's a bundle name\n for bundle in self._context.get_bundles():\n if bundle.get_symbolic_name() == bundle_id:\n break\n else:\n # Bundle not found\n bundle = None\n else:\n # Integer ID: direct access\n try:\n bundle = self._context.get_bundle(bundle_id)\n except constants.BundleException:\n pass\n\n if bundle is None:\n # No matching bundle\n io_handler.write_line(\"Unknown bundle ID: {0}\", bundle_id)\n return False\n\n lines = [\n \"ID......: {0}\".format(bundle.get_bundle_id()),\n \"Name....: {0}\".format(bundle.get_symbolic_name()),\n \"Version.: {0}\".format(bundle.get_version()),\n \"State...: {0}\".format(\n self._utils.bundlestate_to_str(bundle.get_state())\n ),\n \"Location: {0}\".format(bundle.get_location()),\n \"Published services:\",\n ]\n try:\n services = bundle.get_registered_services()\n if services:\n for svc_ref in services:\n lines.append(\"\\t{0}\".format(svc_ref))\n else:\n lines.append(\"\\tn/a\")\n except constants.BundleException as ex:\n # Bundle in a invalid state\n lines.append(\"\\tError: {0}\".format(ex))\n\n lines.append(\"Services used by this bundle:\")\n try:\n services = bundle.get_services_in_use()\n if services:\n for svc_ref in services:\n lines.append(\"\\t{0}\".format(svc_ref))\n else:\n lines.append(\"\\tn/a\")\n except constants.BundleException as ex:\n # Bundle in a invalid state\n lines.append(\"\\tError: {0}\".format(ex))\n\n lines.append(\"\")\n io_handler.write(\"\\n\".join(lines))\n return None", "response": "Prints the details of the bundle with the given ID or name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef service_details(self, io_handler, service_id):\n svc_ref = self._context.get_service_reference(\n None, \"({0}={1})\".format(constants.SERVICE_ID, service_id)\n )\n if svc_ref is None:\n io_handler.write_line(\"Service not found: {0}\", service_id)\n return False\n\n lines = [\n \"ID............: {0}\".format(\n svc_ref.get_property(constants.SERVICE_ID)\n ),\n \"Rank..........: {0}\".format(\n svc_ref.get_property(constants.SERVICE_RANKING)\n ),\n \"Specifications: {0}\".format(\n svc_ref.get_property(constants.OBJECTCLASS)\n ),\n \"Bundle........: {0}\".format(svc_ref.get_bundle()),\n \"Properties....:\",\n ]\n for key, value in sorted(svc_ref.get_properties().items()):\n lines.append(\"\\t{0} = {1}\".format(key, value))\n\n lines.append(\"Bundles using this service:\")\n for bundle in svc_ref.get_using_bundles():\n lines.append(\"\\t{0}\".format(bundle))\n\n lines.append(\"\")\n io_handler.write(\"\\n\".join(lines))\n return None", "response": "Prints the details of the service with the given ID."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting the services in the framework. Possibility to filter on an exact specification.", "response": "def services_list(self, io_handler, specification=None):\n \"\"\"\n Lists the services in the framework. Possibility to filter on an exact\n specification.\n \"\"\"\n # Head of the table\n headers = (\"ID\", \"Specifications\", \"Bundle\", \"Ranking\")\n\n # Lines\n references = (\n self._context.get_all_service_references(specification, None) or []\n )\n\n # Construct the list of services\n lines = [\n [\n str(entry)\n for entry in (\n ref.get_property(constants.SERVICE_ID),\n ref.get_property(constants.OBJECTCLASS),\n ref.get_bundle(),\n ref.get_property(constants.SERVICE_RANKING),\n )\n ]\n for ref in references\n ]\n\n if not lines and specification:\n # No matching service found\n io_handler.write_line(\"No service provides '{0}'\", specification)\n return False\n\n # Print'em all\n io_handler.write(self._utils.make_table(headers, lines))\n io_handler.write_line(\"{0} services registered\", len(lines))\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist the properties of the framework", "response": "def properties_list(self, io_handler):\n \"\"\"\n Lists the properties of the framework\n \"\"\"\n # Get the framework\n framework = self._context.get_framework()\n\n # Head of the table\n headers = (\"Property Name\", \"Value\")\n\n # Lines\n lines = [item for item in framework.get_properties().items()]\n\n # Sort lines\n lines.sort()\n\n # Print the table\n io_handler.write(self._utils.make_table(headers, lines))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprint the value of the given property.", "response": "def property_value(self, io_handler, name):\n \"\"\"\n Prints the value of the given property, looking into\n framework properties then environment variables.\n \"\"\"\n value = self._context.get_property(name)\n if value is None:\n # Avoid printing \"None\"\n value = \"\"\n\n io_handler.write_line(str(value))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef environment_list(self, io_handler):\n # Head of the table\n headers = (\"Environment Variable\", \"Value\")\n\n # Lines\n lines = [item for item in os.environ.items()]\n\n # Sort lines\n lines.sort()\n\n # Print the table\n io_handler.write(self._utils.make_table(headers, lines))", "response": "Lists the framework process environment variables"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists the active threads and their current code line.", "response": "def threads_list(io_handler, max_depth=1):\n \"\"\"\n Lists the active threads and their current code line\n \"\"\"\n # Normalize maximum depth\n try:\n max_depth = int(max_depth)\n if max_depth < 1:\n max_depth = None\n except (ValueError, TypeError):\n max_depth = None\n\n # pylint: disable=W0212\n try:\n # Extract frames\n frames = sys._current_frames()\n\n # Get the thread ID -> Thread mapping\n names = threading._active.copy()\n except AttributeError:\n io_handler.write_line(\"sys._current_frames() is not available.\")\n return\n\n # Sort by thread ID\n thread_ids = sorted(frames.keys())\n lines = []\n for thread_id in thread_ids:\n # Get the corresponding stack\n stack = frames[thread_id]\n\n # Try to get the thread name\n try:\n name = names[thread_id].name\n except KeyError:\n name = \"\"\n\n # Construct the code position\n lines.append(\"Thread ID: {0} - Name: {1}\".format(thread_id, name))\n lines.append(\"Stack Trace:\")\n\n trace_lines = []\n depth = 0\n frame = stack\n while frame is not None and (\n max_depth is None or depth < max_depth\n ):\n # Store the line information\n trace_lines.append(format_frame_info(frame))\n\n # Previous frame...\n frame = frame.f_back\n depth += 1\n\n # Reverse the lines\n trace_lines.reverse()\n\n # Add them to the printed lines\n lines.extend(trace_lines)\n lines.append(\"\")\n\n lines.append(\"\")\n\n # Sort the lines\n io_handler.write(\"\\n\".join(lines))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprints details about the thread with the given ID.", "response": "def thread_details(io_handler, thread_id, max_depth=0):\n \"\"\"\n Prints details about the thread with the given ID (not its name)\n \"\"\"\n # Normalize maximum depth\n try:\n max_depth = int(max_depth)\n if max_depth < 1:\n max_depth = None\n except (ValueError, TypeError):\n max_depth = None\n\n # pylint: disable=W0212\n try:\n # Get the stack\n thread_id = int(thread_id)\n stack = sys._current_frames()[thread_id]\n except KeyError:\n io_handler.write_line(\"Unknown thread ID: {0}\", thread_id)\n except ValueError:\n io_handler.write_line(\"Invalid thread ID: {0}\", thread_id)\n except AttributeError:\n io_handler.write_line(\"sys._current_frames() is not available.\")\n else:\n # Get the name\n try:\n name = threading._active[thread_id].name\n except KeyError:\n name = \"\"\n\n lines = [\n \"Thread ID: {0} - Name: {1}\".format(thread_id, name),\n \"Stack trace:\",\n ]\n\n trace_lines = []\n depth = 0\n frame = stack\n while frame is not None and (\n max_depth is None or depth < max_depth\n ):\n # Store the line information\n trace_lines.append(format_frame_info(frame))\n\n # Previous frame...\n frame = frame.f_back\n depth += 1\n\n # Reverse the lines\n trace_lines.reverse()\n\n # Add them to the printed lines\n lines.extend(trace_lines)\n\n lines.append(\"\")\n io_handler.write(\"\\n\".join(lines))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef log_level(io_handler, level=None, name=None):\n # Get the logger\n logger = logging.getLogger(name)\n\n # Normalize the name\n if not name:\n name = \"Root\"\n\n if not level:\n # Level not given: print the logger level\n io_handler.write_line(\n \"{0} log level: {1} (real: {2})\",\n name,\n logging.getLevelName(logger.getEffectiveLevel()),\n logging.getLevelName(logger.level),\n )\n else:\n # Set the logger level\n try:\n logger.setLevel(level.upper())\n io_handler.write_line(\"New level for {0}: {1}\", name, level)\n except ValueError:\n io_handler.write_line(\"Invalid log level: {0}\", level)", "response": "Prints or changes the log level for the current object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchanging the working directory of the user.", "response": "def change_dir(self, session, path):\n \"\"\"\n Changes the working directory\n \"\"\"\n if path == \"-\":\n # Previous directory\n path = self._previous_path or \".\"\n\n try:\n previous = os.getcwd()\n os.chdir(path)\n except IOError as ex:\n # Can't change directory\n session.write_line(\"Error changing directory: {0}\", ex)\n else:\n # Store previous path\n self._previous_path = previous\n session.write_line(os.getcwd())"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __get_bundle(self, io_handler, bundle_id):\n try:\n bundle_id = int(bundle_id)\n return self._context.get_bundle(bundle_id)\n except (TypeError, ValueError):\n io_handler.write_line(\"Invalid bundle ID: {0}\", bundle_id)\n except constants.BundleException:\n io_handler.write_line(\"Unknown bundle: {0}\", bundle_id)", "response": "Reads the bundle with the given ID. Writes errors\n through the I/O handler."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nstarting the bundles with the given IDs.", "response": "def start(self, io_handler, bundle_id, *bundles_ids):\n \"\"\"\n Starts the bundles with the given IDs. Stops on first failure.\n \"\"\"\n for bid in (bundle_id,) + bundles_ids:\n try:\n # Got an int => it's a bundle ID\n bid = int(bid)\n except ValueError:\n # Got something else, we will try to install it first\n bid = self.install(io_handler, bid)\n\n bundle = self.__get_bundle(io_handler, bid)\n if bundle is not None:\n io_handler.write_line(\n \"Starting bundle {0} ({1})...\",\n bid,\n bundle.get_symbolic_name(),\n )\n bundle.start()\n else:\n return False\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop(self, io_handler, bundle_id, *bundles_ids):\n for bid in (bundle_id,) + bundles_ids:\n bundle = self.__get_bundle(io_handler, bid)\n if bundle is not None:\n io_handler.write_line(\n \"Stopping bundle {0} ({1})...\",\n bid,\n bundle.get_symbolic_name(),\n )\n bundle.stop()\n else:\n return False\n\n return None", "response": "Stops the bundles with the given IDs."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef install(self, io_handler, module_name):\n bundle = self._context.install_bundle(module_name)\n io_handler.write_line(\"Bundle ID: {0}\", bundle.get_bundle_id())\n return bundle.get_bundle_id()", "response": "Installs the bundle with the given module name and returns the bundle ID."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _prepare_configs(configs, requires_filters, temporal_timeouts):\n if not isinstance(requires_filters, dict):\n requires_filters = {}\n\n if not isinstance(temporal_timeouts, dict):\n temporal_timeouts = {}\n\n if not requires_filters and not temporal_timeouts:\n # No explicit configuration given\n return configs\n\n # We need to change a part of the requirements\n new_configs = {}\n for field, config in configs.items():\n # Extract values from tuple\n requirement, timeout = config\n explicit_filter = requires_filters.get(field)\n explicit_timeout = temporal_timeouts.get(field)\n\n # Convert the timeout value\n try:\n explicit_timeout = int(explicit_timeout)\n if explicit_timeout <= 0:\n explicit_timeout = timeout\n except (ValueError, TypeError):\n explicit_timeout = timeout\n\n if not explicit_filter and not explicit_timeout:\n # Nothing to do\n new_configs[field] = config\n else:\n try:\n # Store an updated copy of the requirement\n requirement_copy = requirement.copy()\n if explicit_filter:\n requirement_copy.set_filter(explicit_filter)\n new_configs[field] = (requirement_copy, explicit_timeout)\n except (TypeError, ValueError):\n # No information for this one, or invalid filter:\n # keep the factory requirement\n new_configs[field] = config\n\n return new_configs", "response": "Prepare the given configurations for the component."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of handlers associated to the given component.", "response": "def get_handlers(self, component_context, instance):\n \"\"\"\n Sets up service providers for the given component\n\n :param component_context: The ComponentContext bean\n :param instance: The component instance\n :return: The list of handlers associated to the given component\n \"\"\"\n # Extract information from the context\n configs = component_context.get_handler(\n ipopo_constants.HANDLER_TEMPORAL\n )\n requires_filters = component_context.properties.get(\n ipopo_constants.IPOPO_REQUIRES_FILTERS, None\n )\n temporal_timeouts = component_context.properties.get(\n ipopo_constants.IPOPO_TEMPORAL_TIMEOUTS, None\n )\n\n # Prepare requirements\n new_configs = self._prepare_configs(\n configs, requires_filters, temporal_timeouts\n )\n\n # Return handlers\n return [\n TemporalDependency(field, requirement, timeout)\n for field, (requirement, timeout) in new_configs.items()\n ]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef clear(self):\n # Cancel timer\n self.__cancel_timer()\n self.__timer = None\n self.__timer_args = None\n\n self.__still_valid = False\n self._value = None\n super(TemporalDependency, self).clear()", "response": "Clears up the manager."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalling when a service is arrived in the framework.", "response": "def on_service_arrival(self, svc_ref):\n \"\"\"\n Called when a service has been registered in the framework\n\n :param svc_ref: A service reference\n \"\"\"\n with self._lock:\n if self.reference is None:\n # Inject the service\n service = self._context.get_service(svc_ref)\n self.reference = svc_ref\n self._value.set_service(service)\n self.__still_valid = True\n\n # Cancel timer\n self.__cancel_timer()\n\n # Bind the service\n self._ipopo_instance.bind(self, self._value, self.reference)\n return True\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef on_service_departure(self, svc_ref):\n with self._lock:\n if svc_ref is self.reference:\n # Forget about the service\n self._value.unset_service()\n\n # Clear the reference\n self.reference = None\n\n # Look for a replacement\n self._pending_ref = self._context.get_service_reference(\n self.requirement.specification, self.requirement.filter\n )\n\n if self._pending_ref is None:\n # No replacement found yet, wait a little\n self.__still_valid = True\n self.__timer_args = (self._value, svc_ref)\n self.__timer = threading.Timer(\n self.__timeout, self.__unbind_call, (False,)\n )\n self.__timer.start()\n\n else:\n # Notify iPOPO immediately\n self._ipopo_instance.unbind(self, self._value, svc_ref)\n return True\n\n return None", "response": "Called when a service is unregistered from the framework"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __cancel_timer(self):\n if self.__timer is not None:\n self.__timer.cancel()\n self.__unbind_call(True)\n\n self.__timer_args = None\n self.__timer = None", "response": "Cancels the timer and calls its target method immediately"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __unbind_call(self, still_valid):\n with self._lock:\n if self.__timer is not None:\n # Timeout expired, we're not valid anymore\n self.__timer = None\n self.__still_valid = still_valid\n self._ipopo_instance.unbind(\n self, self.__timer_args[0], self.__timer_args[1]\n )", "response": "Unbinds the iPOPO method from the iPOPO instance."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling a POST request.", "response": "def do_POST(self, request, response):\n # pylint: disable=C0103\n \"\"\"\n Handle a POST request\n\n :param request: The HTTP request bean\n :param response: The HTTP response handler\n \"\"\"\n # Get the request JSON content\n data = jsonrpclib.loads(to_str(request.read_data()))\n\n # Convert from Jabsorb\n data = jabsorb.from_jabsorb(data)\n\n # Dispatch\n try:\n result = self._unmarshaled_dispatch(data, self._simple_dispatch)\n except NoMulticallResult:\n # No result (never happens, but who knows...)\n result = None\n\n if result is not None:\n # Convert result to Jabsorb\n if \"result\" in result:\n result[\"result\"] = jabsorb.to_jabsorb(result[\"result\"])\n\n # Store JSON\n result = jsonrpclib.jdumps(result)\n else:\n # It was a notification\n result = \"\"\n\n # Send the result\n response.send_content(200, result, \"application/json-rpc\")"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _rest_dispatch(self, request, response):\n # type: (AbstractHTTPServletRequest, AbstractHTTPServletResponse) -> None\n \"\"\"\n Dispatches the request\n\n :param request: Request bean\n :param response: Response bean\n \"\"\"\n # Extract request information\n http_verb = request.get_command()\n sub_path = request.get_sub_path()\n\n # Find the best matching method, according to the number of\n # readable arguments\n max_valid_args = -1\n best_method = None\n best_args = None\n best_match = None\n\n for route, method in self.__routes.get(http_verb, {}).items():\n # Parse the request path\n match = route.match(sub_path)\n if not match:\n continue\n\n # Count the number of valid arguments\n method_args = self.__methods_args[method]\n nb_valid_args = 0\n for name in method_args:\n try:\n match.group(name)\n nb_valid_args += 1\n except IndexError:\n # Argument not found\n pass\n\n if nb_valid_args > max_valid_args:\n # Found a better match\n max_valid_args = nb_valid_args\n best_method = method\n best_args = method_args\n best_match = match\n\n if best_method is None:\n # No match: return a 404 plain text error\n response.send_content(\n 404,\n \"No method to handle path {0}\".format(sub_path),\n \"text/plain\",\n )\n else:\n # Found a method\n # ... convert arguments\n kwargs = {}\n if best_args:\n for name, converter in best_args.items():\n try:\n str_value = best_match.group(name)\n except IndexError:\n # Argument is missing: do nothing\n pass\n else:\n if str_value:\n # Keep the default value when an argument is\n # missing, i.e. don't give it in kwargs\n if converter is not None:\n # Convert the argument\n kwargs[name] = converter(str_value)\n else:\n # Use the string value as is\n kwargs[name] = str_value\n\n # Prepare positional arguments\n extra_pos_args = []\n if kwargs:\n # Ignore the first two parameters (request and response)\n method_args = get_method_arguments(best_method).args[:2]\n for pos_arg in method_args:\n try:\n extra_pos_args.append(kwargs.pop(pos_arg))\n except KeyError:\n pass\n\n # ... call the method (exceptions will be handled by the server)\n best_method(request, response, *extra_pos_args, **kwargs)", "response": "Dispatches the request to the appropriate HTTP method and arguments."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _setup_rest_dispatcher(self):\n for _, method in inspect.getmembers(self, inspect.isroutine):\n try:\n config = getattr(method, HTTP_ROUTE_ATTRIBUTE)\n except AttributeError:\n # Not a REST method\n continue\n\n for route in config[\"routes\"]:\n pattern, arguments = self.__convert_route(route)\n self.__methods_args.setdefault(method, {}).update(arguments)\n for http_verb in config[\"methods\"]:\n self.__routes.setdefault(http_verb, {})[pattern] = method", "response": "Setups the dispatcher for REST methods"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a route string into a regex pattern and a dictionary associating arguments names and converters.", "response": "def __convert_route(route):\n # type: (str) -> Tuple[Pattern[str], Dict[str, Callable[[str], Any]]]\n \"\"\"\n Converts a route pattern into a regex.\n The result is a tuple containing the regex pattern to match and a\n dictionary associating arguments names and their converter (if any)\n\n A route can be: \"/hello//\"\n\n :param route: A route string, i.e. a path with type markers\n :return: A tuple (pattern, {argument name: converter})\n \"\"\"\n arguments = {} # type: Dict[str, Callable[[str], Any]]\n last_idx = 0\n final_pattern = []\n match_iter = _MARKER_PATTERN.finditer(route)\n for match_pattern in match_iter:\n # Copy intermediate string\n final_pattern.append(route[last_idx : match_pattern.start()])\n last_idx = match_pattern.end() + 1\n\n # Extract type declaration\n match_type = _TYPED_MARKER_PATTERN.match(match_pattern.group())\n if not match_type:\n raise ValueError(\n \"Invalid argument declaration: {0}\".format(\n match_pattern.group()\n )\n )\n\n name, kind = match_type.groups()\n if kind:\n kind = kind.lower()\n\n # Choose a pattern for each type (can raise a KeyError)\n regex = TYPE_PATTERNS[kind]\n\n # Keep track of argument name and converter\n arguments[name] = TYPE_CONVERTERS.get(kind)\n\n # Generate the regex pattern for this part\n final_pattern.append(\"((?P<\")\n final_pattern.append(match_type.group(1))\n final_pattern.append(\">\")\n final_pattern.append(regex)\n final_pattern.append(\")/?)?\")\n\n # Copy trailing string\n final_pattern.append(route[last_idx:])\n\n # Ensure we don't accept trailing values\n final_pattern.append(\"$\")\n return re.compile(\"\".join(final_pattern)), arguments"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntest if the given service event can be handled or ignored.", "response": "def check_event(self, event):\n # type: (ServiceEvent) -> bool\n \"\"\"\n Tests if the given service event must be handled or ignored, based\n on the state of the iPOPO service and on the content of the event.\n\n :param event: A service event\n :return: True if the event can be handled, False if it must be ignored\n \"\"\"\n with self._lock:\n if self.state == StoredInstance.KILLED:\n # This call may have been blocked by the internal state lock,\n # ignore it\n return False\n\n return self.__safe_handlers_callback(\"check_event\", event)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef bind(self, dependency, svc, svc_ref):\n # type: (Any, Any, ServiceReference) -> None\n \"\"\"\n Called by a dependency manager to inject a new service and update the\n component life cycle.\n \"\"\"\n with self._lock:\n self.__set_binding(dependency, svc, svc_ref)\n self.check_lifecycle()", "response": "Bind a new service into the dependency manager."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncall by a dependency manager when the properties of an injected dependency have been updated.", "response": "def update(self, dependency, svc, svc_ref, old_properties, new_value=False):\n # type: (Any, Any, ServiceReference, dict, bool) -> None\n \"\"\"\n Called by a dependency manager when the properties of an injected\n dependency have been updated.\n\n :param dependency: The dependency handler\n :param svc: The injected service\n :param svc_ref: The reference of the injected service\n :param old_properties: Previous properties of the dependency\n :param new_value: If True, inject the new value of the handler\n \"\"\"\n with self._lock:\n self.__update_binding(\n dependency, svc, svc_ref, old_properties, new_value\n )\n self.check_lifecycle()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unbind(self, dependency, svc, svc_ref):\n # type: (Any, Any, ServiceReference) -> None\n \"\"\"\n Called by a dependency manager to remove an injected service and to\n update the component life cycle.\n \"\"\"\n with self._lock:\n # Invalidate first (if needed)\n self.check_lifecycle()\n\n # Call unbind() and remove the injection\n self.__unset_binding(dependency, svc, svc_ref)\n\n # Try a new configuration\n if self.update_bindings():\n self.check_lifecycle()", "response": "Unbinds an injection from the component and updates the component life cycle."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_controller_state(self, name, value):\n # type: (str, bool) -> None\n \"\"\"\n Sets the state of the controller with the given name\n\n :param name: The name of the controller\n :param value: The new value of the controller\n \"\"\"\n with self._lock:\n self._controllers_state[name] = value\n self.__safe_handlers_callback(\"on_controller_change\", name, value)", "response": "Sets the state of the controller with the given name and value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update_property(self, name, old_value, new_value):\n # type: (str, Any, Any) -> None\n \"\"\"\n Handles a property changed event\n\n :param name: The changed property name\n :param old_value: The previous property value\n :param new_value: The new property value\n \"\"\"\n with self._lock:\n self.__safe_handlers_callback(\n \"on_property_change\", name, old_value, new_value\n )", "response": "Handles a property changed event."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nhandle an hidden property changed event.", "response": "def update_hidden_property(self, name, old_value, new_value):\n # type: (str, Any, Any) -> None\n \"\"\"\n Handles an hidden property changed event\n\n :param name: The changed property name\n :param old_value: The previous property value\n :param new_value: The new property value\n \"\"\"\n with self._lock:\n self.__safe_handlers_callback(\n \"on_hidden_property_change\", name, old_value, new_value\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_handlers(self, kind=None):\n with self._lock:\n if kind is not None:\n try:\n return self._handlers[kind][:]\n except KeyError:\n return []\n\n return self.__all_handlers.copy()", "response": "Returns the handlers of the given kind."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef check_lifecycle(self):\n with self._lock:\n # Validation flags\n was_valid = self.state == StoredInstance.VALID\n can_validate = self.state not in (\n StoredInstance.VALIDATING,\n StoredInstance.VALID,\n )\n\n # Test the validity of all handlers\n handlers_valid = self.__safe_handlers_callback(\n \"is_valid\", break_on_false=True\n )\n\n if was_valid and not handlers_valid:\n # A dependency is missing\n self.invalidate(True)\n elif (\n can_validate and handlers_valid and self._ipopo_service.running\n ):\n # We're all good\n self.validate(True)", "response": "Tests if the state of the component is valid and if it is not valid and if it is not valid and if it is not valid and if it is not valid validate it."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the bindings of the given component .", "response": "def update_bindings(self):\n # type: () -> bool\n \"\"\"\n Updates the bindings of the given component\n\n :return: True if the component can be validated\n \"\"\"\n with self._lock:\n all_valid = True\n for handler in self.get_handlers(handlers_const.KIND_DEPENDENCY):\n # Try to bind\n self.__safe_handler_callback(handler, \"try_binding\")\n\n # Update the validity flag\n all_valid &= self.__safe_handler_callback(\n handler, \"is_valid\", only_boolean=True, none_as_true=True\n )\n return all_valid"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef retry_erroneous(self, properties_update):\n # type: (dict) -> int\n \"\"\"\n Removes the ERRONEOUS state from a component and retries a validation\n\n :param properties_update: A dictionary to update component properties\n :return: The new state of the component\n \"\"\"\n with self._lock:\n if self.state != StoredInstance.ERRONEOUS:\n # Not in erroneous state: ignore\n return self.state\n\n # Update properties\n if properties_update:\n self.context.properties.update(properties_update)\n\n # Reset state\n self.state = StoredInstance.INVALID\n self.error_trace = None\n\n # Retry\n self.check_lifecycle()\n\n # Check if the component is still erroneous\n return self.state", "response": "Updates the component s ERRONEOUS state and retries a validation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\napplying the component invalidation.", "response": "def invalidate(self, callback=True):\n # type: (bool) -> bool\n \"\"\"\n Applies the component invalidation.\n\n :param callback: If True, call back the component before the\n invalidation\n :return: False if the component wasn't valid\n \"\"\"\n with self._lock:\n if self.state != StoredInstance.VALID:\n # Instance is not running...\n return False\n\n # Change the state\n self.state = StoredInstance.INVALID\n\n # Call the handlers\n self.__safe_handlers_callback(\"pre_invalidate\")\n\n # Call the component\n if callback:\n # pylint: disable=W0212\n self.__safe_validation_callback(\n constants.IPOPO_CALLBACK_INVALIDATE\n )\n\n # Trigger an \"Invalidated\" event\n self._ipopo_service._fire_ipopo_event(\n constants.IPopoEvent.INVALIDATED,\n self.factory_name,\n self.name,\n )\n\n # Call the handlers\n self.__safe_handlers_callback(\"post_invalidate\")\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef kill(self):\n # type: () -> bool\n \"\"\"\n This instance is killed : invalidate it if needed, clean up all members\n\n When this method is called, this StoredInstance object must have\n been removed from the registry\n\n :return: True if the component has been killed, False if it already was\n \"\"\"\n with self._lock:\n # Already dead...\n if self.state == StoredInstance.KILLED:\n return False\n\n try:\n self.invalidate(True)\n except:\n self._logger.exception(\n \"%s: Error invalidating the instance\", self.name\n )\n\n # Now that we are nearly clean, be sure we were in a good registry\n # state\n assert not self._ipopo_service.is_registered_instance(self.name)\n\n # Stop all handlers (can tell to unset a binding)\n for handler in self.get_handlers():\n results = self.__safe_handler_callback(handler, \"stop\")\n if results:\n try:\n for binding in results:\n self.__unset_binding(\n handler, binding[0], binding[1]\n )\n except Exception as ex:\n self._logger.exception(\n \"Error stopping handler '%s': %s\", handler, ex\n )\n\n # Call the handlers\n self.__safe_handlers_callback(\"clear\")\n\n # Change the state\n self.state = StoredInstance.KILLED\n\n # Trigger the event\n # pylint: disable=W0212\n self._ipopo_service._fire_ipopo_event(\n constants.IPopoEvent.KILLED, self.factory_name, self.name\n )\n\n # Clean up members\n self._handlers.clear()\n self.__all_handlers.clear()\n self._handlers = None\n self.__all_handlers = None\n self.context = None\n self.instance = None\n self._ipopo_service = None\n return True", "response": "This instance is killed : invalidate it if needed, clean up all members\n\n When this method is called, this StoredInstance object must have\n been removed from the registry\n\n :return: True if the component has been killed, False if it already was"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nvalidate the iPOPO component.", "response": "def validate(self, safe_callback=True):\n # type: (bool) -> bool\n \"\"\"\n Ends the component validation, registering services\n\n :param safe_callback: If True, calls the component validation callback\n :return: True if the component has been validated, else False\n :raise RuntimeError: You try to awake a dead component\n \"\"\"\n with self._lock:\n if self.state in (\n StoredInstance.VALID,\n StoredInstance.VALIDATING,\n StoredInstance.ERRONEOUS,\n ):\n # No work to do (yet)\n return False\n\n if self.state == StoredInstance.KILLED:\n raise RuntimeError(\"{0}: Zombies !\".format(self.name))\n\n # Clear the error trace\n self.error_trace = None\n\n # Call the handlers\n self.__safe_handlers_callback(\"pre_validate\")\n\n if safe_callback:\n # Safe call back needed and not yet passed\n self.state = StoredInstance.VALIDATING\n\n # Call @ValidateComponent first, then @Validate\n if not self.__safe_validation_callback(\n constants.IPOPO_CALLBACK_VALIDATE\n ):\n # Stop there if the callback failed\n self.state = StoredInstance.VALID\n self.invalidate(True)\n\n # Consider the component has erroneous\n self.state = StoredInstance.ERRONEOUS\n return False\n\n # All good\n self.state = StoredInstance.VALID\n\n # Call the handlers\n self.__safe_handlers_callback(\"post_validate\")\n\n # We may have caused a framework error, so check if iPOPO is active\n if self._ipopo_service is not None:\n # pylint: disable=W0212\n # Trigger the iPOPO event (after the service _registration)\n self._ipopo_service._fire_ipopo_event(\n constants.IPopoEvent.VALIDATED, self.factory_name, self.name\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalls the registered method in the component for the given event and returns the result.", "response": "def __callback(self, event, *args, **kwargs):\n # type: (str, *Any, **Any) -> Any\n \"\"\"\n Calls the registered method in the component for the given event\n\n :param event: An event (IPOPO_CALLBACK_VALIDATE, ...)\n :return: The callback result, or None\n :raise Exception: Something went wrong\n \"\"\"\n comp_callback = self.context.get_callback(event)\n if not comp_callback:\n # No registered callback\n return True\n\n # Call it\n result = comp_callback(self.instance, *args, **kwargs)\n if result is None:\n # Special case, if the call back returns nothing\n return True\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __validation_callback(self, event):\n # type: (str) -> Any\n \"\"\"\n Specific handling for the ``@ValidateComponent`` and\n ``@InvalidateComponent`` callback, as it requires checking arguments\n count and order\n\n :param event: The kind of life-cycle callback (in/validation)\n :return: The callback result, or None\n :raise Exception: Something went wrong\n \"\"\"\n comp_callback = self.context.get_callback(event)\n if not comp_callback:\n # No registered callback\n return True\n\n # Get the list of arguments\n try:\n args = getattr(comp_callback, constants.IPOPO_VALIDATE_ARGS)\n except AttributeError:\n raise TypeError(\n \"@ValidateComponent callback is missing internal description\"\n )\n\n # Associate values to arguments\n mapping = {\n constants.ARG_BUNDLE_CONTEXT: self.bundle_context,\n constants.ARG_COMPONENT_CONTEXT: self.context,\n constants.ARG_PROPERTIES: self.context.properties.copy(),\n }\n mapped_args = [mapping[arg] for arg in args]\n\n # Call it\n result = comp_callback(self.instance, *mapped_args)\n if result is None:\n # Special case, if the call back returns nothing\n return True\n\n return result", "response": "This method handles the validation of the life - cycle event."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall the registered method in the component for the given field event and args and kwargs.", "response": "def __field_callback(self, field, event, *args, **kwargs):\n # type: (str, str, *Any, **Any) -> Any\n \"\"\"\n Calls the registered method in the component for the given field event\n\n :param field: A field name\n :param event: An event (IPOPO_CALLBACK_VALIDATE, ...)\n :return: The callback result, or None\n :raise Exception: Something went wrong\n \"\"\"\n # Get the field callback info\n cb_info = self.context.get_field_callback(field, event)\n if not cb_info:\n # No registered callback\n return True\n\n # Extract information\n callback, if_valid = cb_info\n\n if if_valid and self.state != StoredInstance.VALID:\n # Don't call the method if the component state isn't satisfying\n return True\n\n # Call it\n result = callback(self.instance, field, *args, **kwargs)\n if result is None:\n # Special case, if the call back returns nothing\n return True\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef safe_callback(self, event, *args, **kwargs):\n # type: (str, *Any, **Any) -> Any\n \"\"\"\n Calls the registered method in the component for the given event,\n ignoring raised exceptions\n\n :param event: An event (IPOPO_CALLBACK_VALIDATE, ...)\n :return: The callback result, or None\n \"\"\"\n if self.state == StoredInstance.KILLED:\n # Invalid state\n return None\n\n try:\n return self.__callback(event, *args, **kwargs)\n except FrameworkException as ex:\n # Important error\n self._logger.exception(\n \"Critical error calling back %s: %s\", self.name, ex\n )\n\n # Kill the component\n self._ipopo_service.kill(self.name)\n\n if ex.needs_stop:\n # Framework must be stopped...\n self._logger.error(\n \"%s said that the Framework must be stopped.\", self.name\n )\n self.bundle_context.get_framework().stop()\n return False\n except:\n self._logger.exception(\n \"Component '%s': error calling callback method for event %s\",\n self.name,\n event,\n )\n return False", "response": "Calls the registered method in the component for the given event ignoring raised exceptions and returns the result."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __safe_validation_callback(self, event):\n # type: (str) -> Any\n \"\"\"\n Calls the ``@ValidateComponent`` or ``@InvalidateComponent`` callback,\n ignoring raised exceptions\n\n :param event: The kind of life-cycle callback (in/validation)\n :return: The callback result, or None\n \"\"\"\n if self.state == StoredInstance.KILLED:\n # Invalid state\n return None\n\n try:\n return self.__validation_callback(event)\n except FrameworkException as ex:\n # Important error\n self._logger.exception(\n \"Critical error calling back %s: %s\", self.name, ex\n )\n\n # Kill the component\n self._ipopo_service.kill(self.name)\n\n # Store the exception as it is a validation error\n self.error_trace = traceback.format_exc()\n\n if ex.needs_stop:\n # Framework must be stopped...\n self._logger.error(\n \"%s said that the Framework must be stopped.\", self.name\n )\n self.bundle_context.get_framework().stop()\n return False\n except:\n self._logger.exception(\n \"Component '%s': error calling @ValidateComponent callback\",\n self.name,\n )\n\n # Store the exception as it is a validation error\n self.error_trace = traceback.format_exc()\n\n return False", "response": "Calls the validation callback ignoring raised exceptions and return False if the callback returns False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalling the given method with the given arguments in the given handler.", "response": "def __safe_handler_callback(self, handler, method_name, *args, **kwargs):\n # type: (Any, str, *Any, **Any) -> Any\n \"\"\"\n Calls the given method with the given arguments in the given handler.\n Logs exceptions, but doesn't propagate them.\n\n Special arguments can be given in kwargs:\n\n * 'none_as_true': If set to True and the method returned None or\n doesn't exist, the result is considered as True.\n If set to False, None result is kept as is.\n Default is False.\n * 'only_boolean': If True, the result can only be True or False, else\n the result is the value returned by the method.\n Default is False.\n\n :param handler: The handler to call\n :param method_name: The name of the method to call\n :param args: List of arguments for the method to call\n :param kwargs: Dictionary of arguments for the method to call and to\n control the call\n :return: The method result, or None on error\n \"\"\"\n if handler is None or method_name is None:\n return None\n\n # Behavior flags\n only_boolean = kwargs.pop(\"only_boolean\", False)\n none_as_true = kwargs.pop(\"none_as_true\", False)\n\n # Get the method for each handler\n try:\n method = getattr(handler, method_name)\n except AttributeError:\n # Method not found\n result = None\n else:\n try:\n # Call it\n result = method(*args, **kwargs)\n except Exception as ex:\n # No result\n result = None\n\n # Log error\n self._logger.exception(\n \"Error calling handler '%s': %s\", handler, ex\n )\n\n if result is None and none_as_true:\n # Consider None (nothing returned) as True\n result = True\n\n if only_boolean:\n # Convert to a boolean result\n return bool(result)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalling the given method with the given arguments in all handlers and returns True or False.", "response": "def __safe_handlers_callback(self, method_name, *args, **kwargs):\n # type: (str, *Any, **Any) -> bool\n \"\"\"\n Calls the given method with the given arguments in all handlers.\n Logs exceptions, but doesn't propagate them.\n Methods called in handlers must return None, True or False.\n\n Special parameters can be given in kwargs:\n\n * 'exception_as_error': if it is set to True and an exception is raised\n by a handler, then this method will return False. By default, this\n flag is set to False and exceptions are ignored.\n * 'break_on_false': if it set to True, the loop calling the handler\n will stop after an handler returned False. By default, this flag\n is set to False, and all handlers are called.\n\n :param method_name: Name of the method to call\n :param args: List of arguments for the method to call\n :param kwargs: Dictionary of arguments for the method to call and the\n behavior of the call\n :return: True if all handlers returned True (or None), else False\n \"\"\"\n if self.state == StoredInstance.KILLED:\n # Nothing to do\n return False\n\n # Behavior flags\n exception_as_error = kwargs.pop(\"exception_as_error\", False)\n break_on_false = kwargs.pop(\"break_on_false\", False)\n\n result = True\n for handler in self.get_handlers():\n # Get the method for each handler\n try:\n method = getattr(handler, method_name)\n except AttributeError:\n # Ignore missing methods\n pass\n else:\n try:\n # Call it\n res = method(*args, **kwargs)\n if res is not None and not res:\n # Ignore 'None' results\n result = False\n except Exception as ex:\n # Log errors\n self._logger.exception(\n \"Error calling handler '%s': %s\", handler, ex\n )\n\n # We can consider exceptions as errors or ignore them\n result = result and not exception_as_error\n\n if not handler and break_on_false:\n # The loop can stop here\n break\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __set_binding(self, dependency, service, reference):\n # type: (Any, Any, ServiceReference) -> None\n \"\"\"\n Injects a service in the component\n\n :param dependency: The dependency handler\n :param service: The injected service\n :param reference: The reference of the injected service\n \"\"\"\n # Set the value\n setattr(self.instance, dependency.get_field(), dependency.get_value())\n\n # Call the component back\n self.safe_callback(constants.IPOPO_CALLBACK_BIND, service, reference)\n\n self.__safe_field_callback(\n dependency.get_field(),\n constants.IPOPO_CALLBACK_BIND_FIELD,\n service,\n reference,\n )", "response": "Injects a service in the component\n and sets the value of the component\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __update_binding(\n self, dependency, service, reference, old_properties, new_value\n ):\n # type: (Any, Any, ServiceReference, dict, bool) -> None\n \"\"\"\n Calls back component binding and field binding methods when the\n properties of an injected dependency have been updated.\n\n :param dependency: The dependency handler\n :param service: The injected service\n :param reference: The reference of the injected service\n :param old_properties: Previous properties of the dependency\n :param new_value: If True, inject the new value of the handler\n \"\"\"\n if new_value:\n # Set the value\n setattr(\n self.instance, dependency.get_field(), dependency.get_value()\n )\n\n # Call the component back\n self.__safe_field_callback(\n dependency.get_field(),\n constants.IPOPO_CALLBACK_UPDATE_FIELD,\n service,\n reference,\n old_properties,\n )\n\n self.safe_callback(\n constants.IPOPO_CALLBACK_UPDATE, service, reference, old_properties\n )", "response": "Updates the value of the injected dependency with the new value of the injected dependency."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __unset_binding(self, dependency, service, reference):\n # type: (Any, Any, ServiceReference) -> None\n \"\"\"\n Removes a service from the component\n\n :param dependency: The dependency handler\n :param service: The injected service\n :param reference: The reference of the injected service\n \"\"\"\n # Call the component back\n self.__safe_field_callback(\n dependency.get_field(),\n constants.IPOPO_CALLBACK_UNBIND_FIELD,\n service,\n reference,\n )\n\n self.safe_callback(constants.IPOPO_CALLBACK_UNBIND, service, reference)\n\n # Update the injected field\n setattr(self.instance, dependency.get_field(), dependency.get_value())\n\n # Unget the service\n self.bundle_context.unget_service(reference)", "response": "Unbinds a service from the component"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_boolean(value):\n if not value:\n return False\n\n try:\n # Lower string to check known \"false\" value\n value = value.lower()\n return value not in (\"none\", \"0\", \"false\", \"no\")\n except AttributeError:\n # Not a string, but has a value\n return True", "response": "Parses a value into a boolean value corresponding to the given value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlooking for the property keys in the filter string", "response": "def _find_keys(self):\n \"\"\"\n Looks for the property keys in the filter string\n\n :return: A list of property keys\n \"\"\"\n formatter = string.Formatter()\n return [\n val[1] for val in formatter.parse(self._original_filter) if val[1]\n ]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the filter according to the new properties", "response": "def update_filter(self):\n \"\"\"\n Update the filter according to the new properties\n\n :return: True if the filter changed, else False\n :raise ValueError: The filter is invalid\n \"\"\"\n # Consider the filter invalid\n self.valid_filter = False\n\n try:\n # Format the new filter\n filter_str = self._original_filter.format(\n **self._component_context.properties\n )\n except KeyError as ex:\n # An entry is missing: abandon\n logging.warning(\"Missing filter value: %s\", ex)\n raise ValueError(\"Missing filter value\")\n\n try:\n # Parse the new LDAP filter\n new_filter = ldapfilter.get_ldap_filter(filter_str)\n except (TypeError, ValueError) as ex:\n logging.warning(\"Error parsing filter: %s\", ex)\n raise ValueError(\"Error parsing filter\")\n\n # The filter is valid\n self.valid_filter = True\n\n # Compare to the \"old\" one\n if new_filter != self.requirement.filter:\n # Replace the requirement filter\n self.requirement.filter = new_filter\n return True\n\n # Same filter\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef on_property_change(self, name, old_value, new_value):\n # pylint: disable=W0613\n \"\"\"\n A component property has been updated\n\n :param name: Name of the property\n :param old_value: Previous value of the property\n :param new_value: New value of the property\n \"\"\"\n if name in self._keys:\n try:\n if self.update_filter():\n # This is a key for the filter and the filter has changed\n # => Force the handler to update its dependency\n self._reset()\n except ValueError:\n # Invalid filter: clear all references, this will invalidate\n # the component\n for svc_ref in self.get_bindings():\n self.on_service_departure(svc_ref)", "response": "Called when a component property has been changed"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nresets the filter to the original value", "response": "def _reset(self):\n \"\"\"\n Called when the filter has been changed\n \"\"\"\n with self._lock:\n # Start listening to services with the new filter\n self.stop()\n self.start()\n\n for svc_ref in self.get_bindings():\n # Check if the current reference matches the filter\n if not self.requirement.filter.matches(\n svc_ref.get_properties()\n ):\n # Not the case: emulate a service departure\n # The instance life cycle will be updated as well\n self.on_service_departure(svc_ref)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set(self, data=None):\n self.__data = data\n self.__exception = None\n self.__event.set()", "response": "Sets the event to the given data."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef raise_exception(self, exception):\n self.__data = None\n self.__exception = exception\n self.__event.set()", "response": "Raise an exception in wait"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwaits for the event or for the timeout.", "response": "def wait(self, timeout=None):\n \"\"\"\n Waits for the event or for the timeout\n\n :param timeout: Wait timeout (in seconds)\n :return: True if the event as been set, else False\n \"\"\"\n # The 'or' part is for Python 2.6\n result = self.__event.wait(timeout)\n # pylint: disable=E0702\n # Pylint seems to miss the \"is None\" check below\n if self.__exception is None:\n return result\n else:\n raise self.__exception"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nnotifies the callback of the execution of the execution of the resource.", "response": "def __notify(self):\n \"\"\"\n Notify the given callback about the result of the execution\n \"\"\"\n if self.__callback is not None:\n try:\n self.__callback(\n self._done_event.data,\n self._done_event.exception,\n self.__extra,\n )\n except Exception as ex:\n self._logger.exception(\"Error calling back method: %s\", ex)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting a callback method to be called when the result has been computed or in case of exception.", "response": "def set_callback(self, method, extra=None):\n \"\"\"\n Sets a callback method, called once the result has been computed or in\n case of exception.\n\n The callback method must have the following signature:\n ``callback(result, exception, extra)``.\n\n :param method: The method to call back in the end of the execution\n :param extra: Extra parameter to be given to the callback method\n \"\"\"\n self.__callback = method\n self.__extra = extra\n if self._done_event.is_set():\n # The execution has already finished\n self.__notify()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexecutes the given method and stores its result.", "response": "def execute(self, method, args, kwargs):\n \"\"\"\n Execute the given method and stores its result.\n The result is considered \"done\" even if the method raises an exception\n\n :param method: The method to execute\n :param args: Method positional arguments\n :param kwargs: Method keyword arguments\n :raise Exception: The exception raised by the method\n \"\"\"\n # Normalize arguments\n if args is None:\n args = []\n\n if kwargs is None:\n kwargs = {}\n\n try:\n # Call the method\n result = method(*args, **kwargs)\n except Exception as ex:\n # Something went wrong: propagate to the event and to the caller\n self._done_event.raise_exception(ex)\n raise\n else:\n # Store the result\n self._done_event.set(result)\n finally:\n # In any case: notify the call back (if any)\n self.__notify()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef result(self, timeout=None):\n if self._done_event.wait(timeout):\n return self._done_event.data\n else:\n raise OSError(\"Timeout raised\")", "response": "Waits up to timeout for the result of the threaded job. Returns immediately the result if the job has already been done."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef start(self):\n if not self._done_event.is_set():\n # Stop event not set: we're running\n return\n\n # Clear the stop event\n self._done_event.clear()\n\n # Compute the number of threads to start to handle pending tasks\n nb_pending_tasks = self._queue.qsize()\n if nb_pending_tasks > self._max_threads:\n nb_threads = self._max_threads\n nb_pending_tasks = self._max_threads\n elif nb_pending_tasks < self._min_threads:\n nb_threads = self._min_threads\n else:\n nb_threads = nb_pending_tasks\n\n # Create the threads\n for _ in range(nb_pending_tasks):\n self.__nb_pending_task += 1\n self.__start_thread()\n for _ in range(nb_threads - nb_pending_tasks):\n self.__start_thread()", "response": "Starts the thread pool."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __start_thread(self):\n with self.__lock:\n if self.__nb_threads >= self._max_threads:\n # Can't create more threads\n return False\n\n if self._done_event.is_set():\n # We're stopped: do nothing\n return False\n\n # Prepare thread and start it\n name = \"{0}-{1}\".format(self._logger.name, self._thread_id)\n self._thread_id += 1\n\n thread = threading.Thread(target=self.__run, name=name)\n thread.daemon = True\n try:\n self.__nb_threads += 1\n thread.start()\n self._threads.append(thread)\n return True\n except (RuntimeError, OSError):\n self.__nb_threads -= 1\n return False", "response": "Starts a new thread and returns True if it is started False otherwise."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstop the thread pool. Does nothing if the thread pool is already stopped.", "response": "def stop(self):\n \"\"\"\n Stops the thread pool. Does nothing if the pool is already stopped.\n \"\"\"\n if self._done_event.is_set():\n # Stop event set: we're stopped\n return\n\n # Set the stop event\n self._done_event.set()\n\n with self.__lock:\n # Add something in the queue (to unlock the join())\n try:\n for _ in self._threads:\n self._queue.put(self._done_event, True, self._timeout)\n except queue.Full:\n # There is already something in the queue\n pass\n\n # Copy the list of threads to wait for\n threads = self._threads[:]\n\n # Join threads outside the lock\n for thread in threads:\n while thread.is_alive():\n # Wait 3 seconds\n thread.join(3)\n if thread.is_alive():\n # Thread is still alive: something might be wrong\n self._logger.warning(\n \"Thread %s is still alive...\", thread.name\n )\n\n # Clear storage\n del self._threads[:]\n self.clear()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nqueues a task in the pool :param method: Method to call :return: A FutureResult object, to get the result of the task :raise ValueError: Invalid method :raise Full: The task queue is full", "response": "def enqueue(self, method, *args, **kwargs):\n \"\"\"\n Queues a task in the pool\n\n :param method: Method to call\n :return: A FutureResult object, to get the result of the task\n :raise ValueError: Invalid method\n :raise Full: The task queue is full\n \"\"\"\n if not hasattr(method, \"__call__\"):\n raise ValueError(\n \"{0} has no __call__ member.\".format(method.__name__)\n )\n\n # Prepare the future result object\n future = FutureResult(self._logger)\n\n # Use a lock, as we might be \"resetting\" the queue\n with self.__lock:\n # Add the task to the queue\n self._queue.put((method, args, kwargs, future), True, self._timeout)\n self.__nb_pending_task += 1\n\n if self.__nb_pending_task > self.__nb_threads:\n # All threads are taken: start a new one\n self.__start_thread()\n\n return future"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef clear(self):\n with self.__lock:\n # Empty the current queue\n try:\n while True:\n self._queue.get_nowait()\n self._queue.task_done()\n except queue.Empty:\n # Queue is now empty\n pass\n\n # Wait for the tasks currently executed\n self.join()", "response": "Empties the current queue content. Returns once the queue content has been emptied."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef join(self, timeout=None):\n if self._queue.empty():\n # Nothing to wait for...\n return True\n elif timeout is None:\n # Use the original join\n self._queue.join()\n return True\n else:\n # Wait for the condition\n with self._queue.all_tasks_done:\n self._queue.all_tasks_done.wait(timeout)\n return not bool(self._queue.unfinished_tasks)", "response": "Waits for all the tasks to be executed and returns True if the queue is empty False otherwise"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __run(self):\n already_cleaned = False\n try:\n while not self._done_event.is_set():\n try:\n # Wait for an action (blocking)\n task = self._queue.get(True, self._timeout)\n if task is self._done_event:\n # Stop event in the queue: get out\n self._queue.task_done()\n return\n except queue.Empty:\n # Nothing to do yet\n pass\n else:\n with self.__lock:\n self.__nb_active_threads += 1\n # Extract elements\n method, args, kwargs, future = task\n try:\n # Call the method\n future.execute(method, args, kwargs)\n except Exception as ex:\n self._logger.exception(\n \"Error executing %s: %s\", method.__name__, ex\n )\n finally:\n # Mark the action as executed\n self._queue.task_done()\n\n # Thread is not active anymore\n with self.__lock:\n self.__nb_pending_task -= 1\n self.__nb_active_threads -= 1\n\n # Clean up thread if necessary\n with self.__lock:\n extra_threads = self.__nb_threads - self.__nb_active_threads\n if (\n self.__nb_threads > self._min_threads\n and extra_threads > self._queue.qsize()\n ):\n # No more work for this thread\n # if there are more non active_thread than task\n # and we're above the minimum number of threads:\n # stop this one\n self.__nb_threads -= 1\n\n # To avoid a race condition: decrease the number of\n # threads here and mark it as already accounted for\n already_cleaned = True\n return\n finally:\n # Always clean up\n with self.__lock:\n # Thread stops: clean up references\n try:\n self._threads.remove(threading.current_thread())\n except ValueError:\n pass\n\n if not already_cleaned:\n self.__nb_threads -= 1", "response": "The main loop of the main loop."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef docid(url, encoding='ascii'):\n\n if not isinstance(url, bytes):\n url = url.encode(encoding)\n\n parser = _URL_PARSER\n idx = 0\n for _c in url:\n if _c not in _HEX:\n if not (_c == _SYM_MINUS and (idx == _DOMAINID_LENGTH\n or idx == _HOSTID_LENGTH + 1)):\n return parser.parse(url, idx)\n idx += 1\n if idx > 4:\n break\n _l = len(url)\n if _l == _DOCID_LENGTH:\n parser = _DOCID_PARSER\n elif _l == _READABLE_DOCID_LENGTH \\\n and url[_DOMAINID_LENGTH] == _SYM_MINUS \\\n and url[_HOSTID_LENGTH + 1] == _SYM_MINUS:\n parser = _R_DOCID_PARSER\n else:\n parser = _PARSER\n\n return parser.parse(url, idx)", "response": "Get the DocID from a URL."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef init_app(app):\n formatter = LocalVarFormatter()\n\n error_log_file = app.config.get('LOGFILE', 'error.log')\n if error_log_file: # Specify a falsy value in config to disable the log file\n file_handler = logging.FileHandler(error_log_file)\n file_handler.setFormatter(formatter)\n file_handler.setLevel(logging.WARNING)\n app.logger.addHandler(file_handler)\n\n if app.config.get('ADMIN_NUMBERS'):\n if all(key in app.config for key in ['SMS_EXOTEL_SID', 'SMS_EXOTEL_TOKEN', 'SMS_EXOTEL_FROM',\n 'SMS_TWILIO_SID', 'SMS_TWILIO_TOKEN', 'SMS_TWILIO_FROM']):\n\n # A little trickery because directly creating\n # an SMSHandler object didn't work\n logging.handlers.SMSHandler = SMSHandler\n\n sms_handler = logging.handlers.SMSHandler(\n app_name=app.name,\n exotel_sid=app.config['SMS_EXOTEL_SID'],\n exotel_token=app.config['SMS_EXOTEL_TOKEN'],\n exotel_from=app.config['SMS_EXOTEL_FROM'],\n twilio_sid=app.config['SMS_TWILIO_SID'],\n twilio_token=app.config['SMS_TWILIO_TOKEN'],\n twilio_from=app.config['SMS_TWILIO_FROM'],\n phonenumbers=app.config['ADMIN_NUMBERS'])\n sms_handler.setLevel(logging.ERROR)\n app.logger.addHandler(sms_handler)\n\n if app.config.get('SLACK_LOGGING_WEBHOOKS'):\n logging.handlers.SlackHandler = SlackHandler\n slack_handler = logging.handlers.SlackHandler(\n app_name=app.name, webhooks=app.config['SLACK_LOGGING_WEBHOOKS'])\n slack_handler.setFormatter(formatter)\n slack_handler.setLevel(logging.NOTSET)\n app.logger.addHandler(slack_handler)\n\n if app.config.get('ADMINS'):\n # MAIL_DEFAULT_SENDER is the new setting for default mail sender in Flask-Mail\n # DEFAULT_MAIL_SENDER is the old setting. We look for both\n mail_sender = app.config.get('MAIL_DEFAULT_SENDER') or app.config.get(\n 'DEFAULT_MAIL_SENDER', 'logs@example.com')\n if isinstance(mail_sender, (list, tuple)):\n mail_sender = mail_sender[1] # Get email from (name, email)\n if app.config.get('MAIL_USERNAME') and app.config.get('MAIL_PASSWORD'):\n credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])\n else:\n credentials = None\n mail_handler = logging.handlers.SMTPHandler(app.config.get('MAIL_SERVER', 'localhost'),\n mail_sender,\n app.config['ADMINS'],\n '%s failure' % app.name,\n credentials=credentials)\n mail_handler.setFormatter(formatter)\n mail_handler.setLevel(logging.ERROR)\n app.logger.addHandler(mail_handler)", "response": "Initializes an application with the given app."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\noverriding the default format method to remove cache of the specified record.", "response": "def format(self, record):\n \"\"\"\n Format the specified record as text. Overrides\n :meth:`logging.Formatter.format` to remove cache of\n :attr:`record.exc_text` unless it was produced by this formatter.\n \"\"\"\n if record.exc_info:\n if record.exc_text:\n if \"Stack frames (most recent call first)\" not in record.exc_text:\n record.exc_text = None\n return super(LocalVarFormatter, self).format(record)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstrip unwanted markup out of HTML.", "response": "def sanitize_html(value, valid_tags=VALID_TAGS, strip=True):\n \"\"\"\n Strips unwanted markup out of HTML.\n \"\"\"\n return bleach.clean(value, tags=list(VALID_TAGS.keys()), attributes=VALID_TAGS, strip=strip)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef word_count(text, html=True):\n if html:\n text = _tag_re.sub(' ', text)\n text = _strip_re.sub('', text)\n text = _punctuation_re.sub(' ', text)\n return len(text.split())", "response": "Returns the number of words in the given text."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsimplifying text to allow comparison.", "response": "def simplify_text(text):\n \"\"\"\n Simplify text to allow comparison.\n\n >>> simplify_text(\"Awesome Coder wanted at Awesome Company\")\n 'awesome coder wanted at awesome company'\n >>> simplify_text(\"Awesome Coder, wanted at Awesome Company! \")\n 'awesome coder wanted at awesome company'\n >>> simplify_text(u\"Awesome Coder, wanted at Awesome Company! \") == 'awesome coder wanted at awesome company'\n True\n \"\"\"\n if isinstance(text, six.text_type):\n if six.PY3: # pragma: no cover\n text = text.translate(text.maketrans(\"\", \"\", string.punctuation)).lower()\n else: # pragma: no cover\n text = six.text_type(text.encode('utf-8').translate(string.maketrans(\"\", \"\"), string.punctuation).lower(), 'utf-8')\n else:\n text = text.translate(string.maketrans(\"\", \"\"), string.punctuation).lower()\n return \" \".join(text.split())"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nextract named entities from a list of text blocks.", "response": "def extract_named_entities(text_blocks):\n \"\"\"\n Return a list of named entities extracted from provided text blocks (list of text strings).\n \"\"\"\n sentences = []\n for text in text_blocks:\n sentences.extend(nltk.sent_tokenize(text))\n\n tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]\n tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]\n chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)\n\n def extract_entity_names(t):\n entity_names = []\n\n if hasattr(t, 'label'):\n if t.label() == 'NE':\n entity_names.append(' '.join([child[0] for child in t]))\n else:\n for child in t:\n entity_names.extend(extract_entity_names(child))\n\n return entity_names\n\n entity_names = []\n for tree in chunked_sentences:\n entity_names.extend(extract_entity_names(tree))\n\n return set(entity_names)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_alembic_revision(path=None):\n config = Config()\n try:\n config.set_main_option(\"script_location\", path or \"migrations\")\n script = ScriptDirectory.from_config(config)\n head = script.get_current_head()\n # create alembic table\n metadata, alembic_version = alembic_table_metadata()\n metadata.create_all()\n item = manager.db.session.query(alembic_version).first()\n if item and item.version_num != head:\n item.version_num = head\n else:\n item = alembic_version.insert().values(version_num=head)\n item.compile()\n conn = manager.db.engine.connect()\n conn.execute(item)\n manager.db.session.commit()\n stdout.write(\"alembic head is set to %s \\n\" % head)\n except CommandError as e:\n stdout.write(e.message)", "response": "Create alembic table to latest revision number"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating database tables from sqlalchemy models", "response": "def createdb():\n \"\"\"Create database tables from sqlalchemy models\"\"\"\n manager.db.engine.echo = True\n manager.db.create_all()\n set_alembic_revision()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_manager(app, db, **kwargs):\n manager.app = app\n manager.db = db\n manager.context = kwargs\n manager.add_command('db', MigrateCommand)\n manager.add_command('clean', Clean())\n manager.add_command('showurls', ShowUrls())\n manager.add_command('shell', Shell(make_context=shell_context))\n manager.add_command('plainshell', Shell(make_context=shell_context,\n use_ipython=False, use_bpython=False))\n return manager", "response": "Initializes the base manager for the base object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init_app(app, env=None):\n # Make current_auth available to app templates\n app.jinja_env.globals['current_auth'] = current_auth\n # Make the current view available to app templates\n app.jinja_env.globals['current_view'] = current_view\n # Disable Flask-SQLAlchemy events.\n # Apps that want it can turn it back on in their config\n app.config.setdefault('SQLALCHEMY_TRACK_MODIFICATIONS', False)\n # Load config from the app's settings.py\n load_config_from_file(app, 'settings.py')\n # Load additional settings from the app's environment-specific config file\n if not env:\n env = environ.get('FLASK_ENV', 'DEVELOPMENT') # Uppercase for compatibility with Flask-Environments\n additional = _additional_config.get(env.lower()) # Lowercase because that's how we define it\n if additional:\n load_config_from_file(app, additional)\n\n logger.init_app(app)", "response": "Initializes a Flask application based on the environment."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_jinja_environment(self):\n options = dict(self.jinja_options)\n if 'autoescape' not in options:\n options['autoescape'] = self.select_jinja_autoescape\n rv = SandboxedEnvironment(self, **options)\n rv.globals.update(\n url_for=url_for,\n get_flashed_messages=get_flashed_messages,\n config=self.config, # FIXME: Sandboxed templates shouldn't access full config\n # request, session and g are normally added with the\n # context processor for efficiency reasons but for imported\n # templates we also want the proxies in there.\n request=request,\n session=session,\n g=g # FIXME: Similarly with g: no access for sandboxed templates\n )\n rv.filters['tojson'] = _tojson_filter\n return rv", "response": "Creates the Jinja2 environment based on the passed in options and returns a SandboxedEnvironment object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef for_tsquery(text):\n tokens = [_token_map.get(t, t) for t in _tsquery_tokens_re.split(\n _whitespace_re.sub(' ', text.replace(\"'\", \" \").replace('\"', ' ').replace('\\0', '')))]\n tokens = [t if t in ('&', '|', '!', ':*', '(', ')', ' ') else \"'\" + t.strip() + \"'\"\n for t in tokens]\n tokens = [t for t in tokens if t not in ('', ' ', \"''\")]\n if not tokens:\n return ''\n counterlength = len(tokens)\n counter = 1\n while counter < counterlength:\n if tokens[counter] == '!' and tokens[counter - 1] not in ('&', '|', '('):\n tokens.insert(counter, '&')\n counter += 1\n counterlength += 1\n elif tokens[counter] == '(' and tokens[counter - 1] not in ('&', '|', '!'):\n tokens.insert(counter, '&')\n counter += 1\n counterlength += 1\n elif tokens[counter] == ')' and tokens[counter - 1] == '(':\n # Empty ()\n tokens.pop(counter)\n tokens.pop(counter - 1)\n counter -= 2\n counterlength -= 2\n # Pop the join with previous segment too\n if tokens and tokens[counter] in ('&', '|'):\n tokens.pop(counter)\n counter -= 1\n counterlength -= 1\n elif tokens and counter == 0 and tokens[counter] == '!':\n tokens.pop(counter)\n counter -= 1\n counterlength -= 1\n elif tokens and counter > 0 and tokens[counter - 1:counter + 1] in (['&', '!'], ['|', '!']):\n tokens.pop(counter)\n tokens.pop(counter - 1)\n counter -= 2\n counterlength -= 2\n elif tokens[counter].startswith(\"'\") and tokens[counter - 1] not in ('&', '|', '!', '('):\n tokens.insert(counter, '&')\n counter += 1\n counterlength += 1\n elif (\n tokens[counter] in ('&', '|') and tokens[counter - 1] in ('&', '|')) or (\n tokens[counter] == '!' and tokens[counter - 1] not in ('&', '|')) or (\n tokens[counter] == ':*' and not tokens[counter - 1].startswith(\"'\")):\n # Invalid token: is a dupe or follows a token it shouldn't follow\n tokens.pop(counter)\n counter -= 1\n counterlength -= 1\n counter += 1\n while tokens and tokens[0] in ('&', '|', ':*', ')', '!', '*'):\n tokens.pop(0) # Can't start with a binary or suffix operator\n if tokens:\n while tokens and tokens[-1] in ('&', '|', '!', '('):\n tokens.pop(-1) # Can't end with a binary or prefix operator\n if not tokens:\n return '' # Did we just eliminate all tokens?\n missing_brackets = sum([1 if t == '(' else -1 for t in tokens if t in ('(', ')')])\n if missing_brackets > 0:\n tokens.append(')' * missing_brackets)\n elif missing_brackets < 0:\n tokens.insert(0, '(' * -missing_brackets)\n return ''.join(tokens)", "response": "r Tokenize text into a valid PostgreSQL to_tsquery query."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the database identity for this model.", "response": "def id(cls):\n \"\"\"\n Database identity for this model, used for foreign key references from other models\n \"\"\"\n if cls.__uuid_primary_key__:\n return immutable(Column(UUIDType(binary=False), default=uuid_.uuid4, primary_key=True, nullable=False))\n else:\n return immutable(Column(Integer, primary_key=True, nullable=False))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef url_id(cls):\n if cls.__uuid_primary_key__:\n def url_id_func(self):\n \"\"\"The URL id, UUID primary key rendered as a hex string\"\"\"\n return self.id.hex\n\n def url_id_is(cls):\n return SqlHexUuidComparator(cls.id)\n\n url_id_func.__name__ = 'url_id'\n url_id_property = hybrid_property(url_id_func)\n url_id_property = url_id_property.comparator(url_id_is)\n return url_id_property\n else:\n def url_id_func(self):\n \"\"\"The URL id, integer primary key rendered as a string\"\"\"\n return six.text_type(self.id)\n\n def url_id_expression(cls):\n \"\"\"The URL id, integer primary key\"\"\"\n return cls.id\n\n url_id_func.__name__ = 'url_id'\n url_id_property = hybrid_property(url_id_func)\n url_id_property = url_id_property.expression(url_id_expression)\n return url_id_property", "response": "The URL id of the class."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef uuid(cls):\n if hasattr(cls, '__uuid_primary_key__') and cls.__uuid_primary_key__:\n return synonym('id')\n else:\n return immutable(Column(UUIDType(binary=False), default=uuid_.uuid4, unique=True, nullable=False))", "response": "Returns the UUID column or synonym to existing : attr : id column if that is a UUID"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns public URL to this instance for a given action.", "response": "def url_for(self, action='view', **kwargs):\n \"\"\"\n Return public URL to this instance for a given action (default 'view')\n \"\"\"\n app = current_app._get_current_object() if current_app else None\n if app is not None and action in self.url_for_endpoints.get(app, {}):\n endpoint, paramattrs, _external = self.url_for_endpoints[app][action]\n else:\n try:\n endpoint, paramattrs, _external = self.url_for_endpoints[None][action]\n except KeyError:\n raise BuildError(action, kwargs, 'GET')\n params = {}\n for param, attr in list(paramattrs.items()):\n if isinstance(attr, tuple):\n # attr is a tuple containing:\n # 1. ('parent', 'name') --> self.parent.name\n # 2. ('**entity', 'name') --> kwargs['entity'].name\n if attr[0].startswith('**'):\n item = kwargs.pop(attr[0][2:])\n attr = attr[1:]\n else:\n item = self\n for subattr in attr:\n item = getattr(item, subattr)\n params[param] = item\n elif callable(attr):\n params[param] = attr(self)\n else:\n params[param] = getattr(self, attr)\n if _external is not None:\n params['_external'] = _external\n params.update(kwargs) # Let kwargs override params\n\n # url_for from flask\n return url_for(endpoint, **params)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef is_url_for(cls, _action, _endpoint=None, _app=None, _external=None, **paramattrs):\n def decorator(f):\n if 'url_for_endpoints' not in cls.__dict__:\n cls.url_for_endpoints = {None: {}} # Stick it into the class with the first endpoint\n cls.url_for_endpoints.setdefault(_app, {})\n\n for keyword in paramattrs:\n if isinstance(paramattrs[keyword], six.string_types) and '.' in paramattrs[keyword]:\n paramattrs[keyword] = tuple(paramattrs[keyword].split('.'))\n cls.url_for_endpoints[_app][_action] = _endpoint or f.__name__, paramattrs, _external\n return f\n return decorator", "response": "Decorator that registers the view as a url_for target."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef register_view_for(cls, app, action, classview, attr):\n if 'view_for_endpoints' not in cls.__dict__:\n cls.view_for_endpoints = {}\n cls.view_for_endpoints.setdefault(app, {})[action] = (classview, attr)", "response": "Register a classview and viewhandler for a given app and action."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef view_for(self, action='view'):\n app = current_app._get_current_object()\n view, attr = self.view_for_endpoints[app][action]\n return getattr(view(self), attr)", "response": "Return the classview viewhandler that handles the specified action"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the classview that contains the viewhandler for the specified action", "response": "def classview_for(self, action='view'):\n \"\"\"\n Return the classview that contains the viewhandler for the specified action\n \"\"\"\n app = current_app._get_current_object()\n return self.view_for_endpoints[app][action][0](self)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _set_fields(self, fields):\n for f in fields:\n if hasattr(self, f):\n setattr(self, f, fields[f])\n else:\n raise TypeError(\"'{arg}' is an invalid argument for {instance_type}\".format(arg=f, instance_type=self.__class__.__name__))", "response": "Helper method for upsert in the various subclasses."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef title(cls):\n if cls.__title_length__ is None:\n column_type = UnicodeText()\n else:\n column_type = Unicode(cls.__title_length__)\n return Column(column_type, nullable=False)", "response": "The title of this object"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninsert or update an instance of the class with the given name.", "response": "def upsert(cls, name, **fields):\n \"\"\"Insert or update an instance\"\"\"\n instance = cls.get(name)\n if instance:\n instance._set_fields(fields)\n else:\n instance = cls(name=name, **fields)\n instance = failsafe_add(cls.query.session, instance, name=name)\n return instance"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting an instance matching the parent and name", "response": "def get(cls, parent, name):\n \"\"\"Get an instance matching the parent and name\"\"\"\n return cls.query.filter_by(parent=parent, name=name).one_or_none()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating an abbreviated title by subtracting the parent s title from this instance s title.", "response": "def short_title(self):\n \"\"\"\n Generates an abbreviated title by subtracting the parent's title from this instance's title.\n \"\"\"\n if self.title and self.parent is not None and hasattr(self.parent, 'title') and self.parent.title:\n if self.title.startswith(self.parent.title):\n short = self.title[len(self.parent.title):].strip()\n match = _punctuation_re.match(short)\n if match:\n short = short[match.end():].strip()\n if short:\n return short\n return self.title"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of permissions for this model plus permissions inherited from the parent.", "response": "def permissions(self, actor, inherited=None):\n \"\"\"\n Permissions for this model, plus permissions inherited from the parent.\n \"\"\"\n if inherited is not None:\n return inherited | super(BaseScopedNameMixin, self).permissions(actor)\n elif self.parent is not None and isinstance(self.parent, PermissionMixin):\n return self.parent.permissions(actor) | super(BaseScopedNameMixin, self).permissions(actor)\n else:\n return super(BaseScopedNameMixin, self).permissions(actor)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get(cls, parent, url_id):\n return cls.query.filter_by(parent=parent, url_id=url_id).one_or_none()", "response": "Get an instance matching the parent and url_id"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef make_id(self):\n if self.url_id is None: # Set id only if empty\n self.url_id = select([func.coalesce(func.max(self.__class__.url_id + 1), 1)],\n self.__class__.parent == self.parent)", "response": "Create a new URL id that is unique to the parent container"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_timestamp_columns():\n return (\n Column('created_at', DateTime, default=func.utcnow(), nullable=False),\n Column('updated_at', DateTime, default=func.utcnow(), onupdate=func.utcnow(), nullable=False),\n )", "response": "Return two columns created_at and updated_at with appropriate defaults"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd a new instance to the database and commits a new one if the transaction fails.", "response": "def failsafe_add(_session, _instance, **filters):\n \"\"\"\n Add and commit a new instance in a nested transaction (using SQL SAVEPOINT),\n gracefully handling failure in case a conflicting entry is already in the\n database (which may occur due to parallel requests causing race conditions\n in a production environment with multiple workers).\n\n Returns the instance saved to database if no error occurred, or loaded from\n database using the provided filters if an error occurred. If the filters fail\n to load from the database, the original IntegrityError is re-raised, as it\n is assumed to imply that the commit failed because of missing or invalid\n data, not because of a duplicate entry.\n\n However, when no filters are provided, nothing is returned and IntegrityError\n is also suppressed as there is no way to distinguish between data validation\n failure and an existing conflicting record in the database. Use this option\n when failures are acceptable but the cost of verification is not.\n\n Usage: ``failsafe_add(db.session, instance, **filters)`` where filters\n are the parameters passed to ``Model.query.filter_by(**filters).one()``\n to load the instance.\n\n You must commit the transaction as usual after calling ``failsafe_add``.\n\n :param _session: Database session\n :param _instance: Instance to commit\n :param filters: Filters required to load existing instance from the\n database in case the commit fails (required)\n :return: Instance that is in the database\n \"\"\"\n if _instance in _session:\n # This instance is already in the session, most likely due to a\n # save-update cascade. SQLAlchemy will flush before beginning a\n # nested transaction, which defeats the purpose of nesting, so\n # remove it for now and add it back inside the SAVEPOINT.\n _session.expunge(_instance)\n _session.begin_nested()\n try:\n _session.add(_instance)\n _session.commit()\n if filters:\n return _instance\n except IntegrityError as e:\n _session.rollback()\n if filters:\n try:\n return _session.query(_instance.__class__).filter_by(**filters).one()\n except NoResultFound: # Do not trap the other exception, MultipleResultsFound\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_primary_relationship(parent, childrel, child, parentrel, parentcol):\n\n parent_table_name = parent.__tablename__\n child_table_name = child.__tablename__\n primary_table_name = parent_table_name + '_' + child_table_name + '_primary'\n parent_id_columns = [c.name for c in inspect(parent).primary_key]\n child_id_columns = [c.name for c in inspect(child).primary_key]\n\n primary_table_columns = (\n [Column(\n parent_table_name + '_' + name,\n None,\n ForeignKey(parent_table_name + '.' + name, ondelete='CASCADE'),\n primary_key=True,\n nullable=False) for name in parent_id_columns]\n + [Column(\n child_table_name + '_' + name,\n None,\n ForeignKey(child_table_name + '.' + name, ondelete='CASCADE'),\n nullable=False) for name in child_id_columns]\n + list(make_timestamp_columns())\n )\n\n primary_table = Table(primary_table_name, parent.metadata, *primary_table_columns)\n rel = relationship(child, uselist=False, secondary=primary_table)\n setattr(parent, childrel, rel)\n\n @event.listens_for(rel, 'set')\n def _validate_child(target, value, oldvalue, initiator):\n if value and getattr(value, parentrel) != target:\n raise ValueError(\"The target is not affiliated with this parent\")\n\n # XXX: To support multi-column primary keys, update this SQL function\n event.listen(primary_table, 'after_create',\n DDL('''\n CREATE FUNCTION %(function)s() RETURNS TRIGGER AS $$\n DECLARE\n target RECORD;\n BEGIN\n IF (NEW.%(rhs)s IS NOT NULL) THEN\n SELECT %(parentcol)s INTO target FROM %(child_table_name)s WHERE %(child_id_column)s = NEW.%(rhs)s;\n IF (target.%(parentcol)s != NEW.%(lhs)s) THEN\n RAISE foreign_key_violation USING MESSAGE = 'The target is not affiliated with this parent';\n END IF;\n END IF;\n RETURN NEW;\n END;\n $$ LANGUAGE plpgsql;\n CREATE TRIGGER %(trigger)s BEFORE INSERT OR UPDATE\n ON %(table)s\n FOR EACH ROW EXECUTE PROCEDURE %(function)s();\n ''',\n context={\n 'table': primary_table_name,\n 'function': '%s_validate' % primary_table_name,\n 'trigger': '%s_trigger' % primary_table_name,\n 'parentcol': parentcol,\n 'child_table_name': child_table_name,\n 'child_id_column': child_id_columns[0],\n 'lhs': '%s_%s' % (parent_table_name, parent_id_columns[0]),\n 'rhs': '%s_%s' % (child_table_name, child_id_columns[0]),\n }\n ).execute_if(dialect='postgresql')\n )\n\n event.listen(primary_table, 'before_drop',\n DDL('''\n DROP TRIGGER %(trigger)s ON %(table)s;\n DROP FUNCTION %(function)s();\n ''',\n context={\n 'table': primary_table_name,\n 'trigger': '%s_trigger' % primary_table_name,\n 'function': '%s_validate' % primary_table_name,\n }\n ).execute_if(dialect='postgresql')\n )", "response": "This function adds a primary relationship between two models."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting the default value for a column when it s first accessed rather than first committed to the database.", "response": "def auto_init_default(column):\n \"\"\"\n Set the default value for a column when it's first accessed rather than\n first committed to the database.\n \"\"\"\n if isinstance(column, ColumnProperty):\n default = column.columns[0].default\n else:\n default = column.default\n\n @event.listens_for(column, 'init_scalar', retval=True, propagate=True)\n def init_scalar(target, value, dict_):\n # A subclass may override the column and not provide a default. Watch out for that.\n if default:\n if default.is_callable:\n value = default.arg(None)\n elif default.is_scalar:\n value = default.arg\n else:\n raise NotImplementedError(\"Can't invoke pre-default for a SQL-level column default\")\n dict_[column.key] = value\n return value"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef requestargs(*vars, **config):\n if config and list(config.keys()) != ['source']:\n raise TypeError(\"Unrecognised parameters: %s\" % repr(config.keys()))\n\n def inner(f):\n namefilt = [(name[:-2], filt, True) if name.endswith('[]') else (name, filt, False)\n for name, filt in\n [(v[0], v[1]) if isinstance(v, (list, tuple)) else (v, None) for v in vars]]\n\n if config and config.get('source') == 'form':\n def datasource():\n return request.form if request else {}\n elif config and config.get('source') == 'query':\n def datasource():\n return request.args if request else {}\n else:\n def datasource():\n return request.values if request else {}\n\n @wraps(f)\n def decorated_function(*args, **kw):\n values = datasource()\n for name, filt, is_list in namefilt:\n # Process name if\n # (a) it's not in the function's parameters, and\n # (b) is in the form/query\n if name not in kw and name in values:\n try:\n if is_list:\n kw[name] = values.getlist(name, type=filt)\n else:\n kw[name] = values.get(name, type=filt)\n except ValueError as e:\n raise RequestValueError(e)\n try:\n return f(*args, **kw)\n except TypeError as e:\n raise RequestTypeError(e)\n return decorated_function\n return inner", "response": "This decorator loads parameters from request. values if not specified in the\n function."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_models(*chain, **kwargs):\n def inner(f):\n @wraps(f)\n def decorated_function(*args, **kw):\n permissions = None\n permission_required = kwargs.get('permission')\n url_check_attributes = kwargs.get('urlcheck', [])\n if isinstance(permission_required, six.string_types):\n permission_required = set([permission_required])\n elif permission_required is not None:\n permission_required = set(permission_required)\n result = {}\n for models, attributes, parameter in chain:\n if not isinstance(models, (list, tuple)):\n models = (models,)\n item = None\n for model in models:\n query = model.query\n url_check = False\n url_check_paramvalues = {}\n for k, v in attributes.items():\n if callable(v):\n query = query.filter_by(**{k: v(result, kw)})\n else:\n if '.' in v:\n first, attrs = v.split('.', 1)\n val = result.get(first)\n for attr in attrs.split('.'):\n val = getattr(val, attr)\n else:\n val = result.get(v, kw.get(v))\n query = query.filter_by(**{k: val})\n if k in url_check_attributes:\n url_check = True\n url_check_paramvalues[k] = (v, val)\n item = query.first()\n if item is not None:\n # We found it, so don't look in additional models\n break\n if item is None:\n abort(404)\n\n if hasattr(item, 'redirect_view_args'):\n # This item is a redirect object. Redirect to destination\n view_args = dict(request.view_args)\n view_args.update(item.redirect_view_args())\n location = url_for(request.endpoint, **view_args)\n if request.query_string:\n location = location + u'?' + request.query_string.decode()\n return redirect(location, code=307)\n\n if permission_required:\n permissions = item.permissions(current_auth.actor, inherited=permissions)\n addlperms = kwargs.get('addlperms') or []\n if callable(addlperms):\n addlperms = addlperms() or []\n permissions.update(addlperms)\n if g: # XXX: Deprecated\n g.permissions = permissions\n if request:\n add_auth_attribute('permissions', permissions)\n if url_check and request.method == 'GET': # Only do urlcheck redirects on GET requests\n url_redirect = False\n view_args = None\n for k, v in url_check_paramvalues.items():\n uparam, uvalue = v\n if getattr(item, k) != uvalue:\n url_redirect = True\n if view_args is None:\n view_args = dict(request.view_args)\n view_args[uparam] = getattr(item, k)\n if url_redirect:\n location = url_for(request.endpoint, **view_args)\n if request.query_string:\n location = location + u'?' + request.query_string.decode()\n return redirect(location, code=302)\n if parameter.startswith('g.'):\n parameter = parameter[2:]\n setattr(g, parameter, item)\n result[parameter] = item\n if permission_required and not permission_required & permissions:\n abort(403)\n if kwargs.get('kwargs'):\n return f(*args, kwargs=kw, **result)\n else:\n return f(*args, **result)\n return decorated_function\n return inner", "response": "Decorator to load a chain of models from the given parameters."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts the parameter into a dictionary before calling jsonify", "response": "def dict_jsonify(param):\n \"\"\"Convert the parameter into a dictionary before calling jsonify, if it's not already one\"\"\"\n if not isinstance(param, dict):\n param = dict(param)\n return jsonify(param)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dict_jsonp(param):\n if not isinstance(param, dict):\n param = dict(param)\n return jsonp(param)", "response": "Convert the parameter into a dictionary before calling jsonp"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd CORS headers to the decorated view function. :param origins: Allowed origins (see below) :param methods: A list of allowed HTTP methods :param headers: A list of allowed HTTP headers :param max_age: Duration in seconds for which the CORS response may be cached The :obj:`origins` parameter may be one of: 1. A callable that receives the origin as a parameter. 2. A list of origins. 3. ``*``, indicating that this resource is accessible by any origin. Example use:: from flask import Flask, Response from coaster.views import cors app = Flask(__name__) @app.route('/any') @cors('*') def any_origin(): return Response() @app.route('/static', methods=['GET', 'POST']) @cors(['https://hasgeek.com'], methods=['GET'], headers=['Content-Type', 'X-Requested-With'], max_age=3600) def static_list(): return Response() def check_origin(origin): # check if origin should be allowed return True @app.route('/callable') @cors(check_origin) def callable_function(): return Response()", "response": "def cors(origins,\n methods=['HEAD', 'OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'],\n headers=['Accept', 'Accept-Language', 'Content-Language', 'Content-Type', 'X-Requested-With'],\n max_age=None):\n \"\"\"\n Adds CORS headers to the decorated view function.\n\n :param origins: Allowed origins (see below)\n :param methods: A list of allowed HTTP methods\n :param headers: A list of allowed HTTP headers\n :param max_age: Duration in seconds for which the CORS response may be cached\n\n The :obj:`origins` parameter may be one of:\n\n 1. A callable that receives the origin as a parameter.\n 2. A list of origins.\n 3. ``*``, indicating that this resource is accessible by any origin.\n\n Example use::\n\n from flask import Flask, Response\n from coaster.views import cors\n\n app = Flask(__name__)\n\n @app.route('/any')\n @cors('*')\n def any_origin():\n return Response()\n\n @app.route('/static', methods=['GET', 'POST'])\n @cors(['https://hasgeek.com'], methods=['GET'], headers=['Content-Type', 'X-Requested-With'],\n max_age=3600)\n def static_list():\n return Response()\n\n def check_origin(origin):\n # check if origin should be allowed\n return True\n\n @app.route('/callable')\n @cors(check_origin)\n def callable_function():\n return Response()\n \"\"\"\n def inner(f):\n @wraps(f)\n def wrapper(*args, **kwargs):\n origin = request.headers.get('Origin')\n if request.method not in methods:\n abort(405)\n\n if origins == '*':\n pass\n elif is_collection(origins) and origin in origins:\n pass\n elif callable(origins) and origins(origin):\n pass\n else:\n abort(403)\n\n if request.method == 'OPTIONS':\n # pre-flight request\n resp = Response()\n else:\n result = f(*args, **kwargs)\n resp = make_response(result) if not isinstance(result,\n (Response, WerkzeugResponse, current_app.response_class)) else result\n\n resp.headers['Access-Control-Allow-Origin'] = origin if origin else ''\n resp.headers['Access-Control-Allow-Methods'] = ', '.join(methods)\n resp.headers['Access-Control-Allow-Headers'] = ', '.join(headers)\n if max_age:\n resp.headers['Access-Control-Max-Age'] = str(max_age)\n # Add 'Origin' to the Vary header since response will vary by origin\n if 'Vary' in resp.headers:\n vary_values = [item.strip() for item in resp.headers['Vary'].split(',')]\n if 'Origin' not in vary_values:\n vary_values.append('Origin')\n resp.headers['Vary'] = ', '.join(vary_values)\n else:\n resp.headers['Vary'] = 'Origin'\n\n return resp\n return wrapper\n return inner"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nviews decorator that requires a certain permission to be present in ``current_auth.permissions`` before the view is allowed to proceed. Aborts with ``403 Forbidden`` if the permission is not present. The decorated view will have an ``is_available`` method that can be called to perform the same test. :param permission: Permission that is required. If a collection type is provided, any one permission must be available", "response": "def requires_permission(permission):\n \"\"\"\n View decorator that requires a certain permission to be present in\n ``current_auth.permissions`` before the view is allowed to proceed.\n Aborts with ``403 Forbidden`` if the permission is not present.\n\n The decorated view will have an ``is_available`` method that can be called\n to perform the same test.\n\n :param permission: Permission that is required. If a collection type is\n provided, any one permission must be available\n \"\"\"\n def inner(f):\n def is_available_here():\n if not hasattr(current_auth, 'permissions'):\n return False\n elif is_collection(permission):\n return bool(current_auth.permissions.intersection(permission))\n else:\n return permission in current_auth.permissions\n\n def is_available(context=None):\n result = is_available_here()\n if result and hasattr(f, 'is_available'):\n # We passed, but we're wrapping another test, so ask there as well\n return f.is_available(context)\n return result\n\n @wraps(f)\n def wrapper(*args, **kwargs):\n add_auth_attribute('login_required', True)\n if not is_available_here():\n abort(403)\n return f(*args, **kwargs)\n\n wrapper.requires_permission = permission\n wrapper.is_available = is_available\n return wrapper\n return inner"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if the item is a collection class.", "response": "def is_collection(item):\n \"\"\"\n Returns True if the item is a collection class: list, tuple, set, frozenset\n or any other class that resembles one of these (using abstract base classes).\n\n >>> is_collection(0)\n False\n >>> is_collection(0.1)\n False\n >>> is_collection('')\n False\n >>> is_collection({})\n False\n >>> is_collection({}.keys())\n True\n >>> is_collection([])\n True\n >>> is_collection(())\n True\n >>> is_collection(set())\n True\n >>> is_collection(frozenset())\n True\n >>> from coaster.utils import InspectableSet\n >>> is_collection(InspectableSet({1, 2}))\n True\n \"\"\"\n return not isinstance(item, six.string_types) and isinstance(item, (collections.Set, collections.Sequence))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef buid():\n if six.PY3: # pragma: no cover\n return urlsafe_b64encode(uuid.uuid4().bytes).decode('utf-8').rstrip('=')\n else: # pragma: no cover\n return six.text_type(urlsafe_b64encode(uuid.uuid4().bytes).rstrip('='))", "response": "Return a random id that is exactly 22 characters long and by encoding a UUID4 in URL - safe Base64."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef uuid1mc_from_datetime(dt):\n fields = list(uuid1mc().fields)\n if isinstance(dt, datetime):\n timeval = time.mktime(dt.timetuple()) + dt.microsecond / 1e6\n else:\n # Assume we got an actual timestamp\n timeval = dt\n\n # The following code is borrowed from the UUID module source:\n nanoseconds = int(timeval * 1e9)\n # 0x01b21dd213814000 is the number of 100-ns intervals between the\n # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.\n timestamp = int(nanoseconds // 100) + 0x01b21dd213814000\n time_low = timestamp & 0xffffffff\n time_mid = (timestamp >> 32) & 0xffff\n time_hi_version = (timestamp >> 48) & 0x0fff\n\n fields[0] = time_low\n fields[1] = time_mid\n fields[2] = time_hi_version\n\n return uuid.UUID(fields=tuple(fields))", "response": "Return a UUID1 with a random multicast MAC id and a timestamp matching the given datetime object or timestamp value."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert a UUID object to a 22 - char BUID string", "response": "def uuid2buid(value):\n \"\"\"\n Convert a UUID object to a 22-char BUID string\n\n >>> u = uuid.UUID('33203dd2-f2ef-422f-aeb0-058d6f5f7089')\n >>> uuid2buid(u)\n 'MyA90vLvQi-usAWNb19wiQ'\n \"\"\"\n if six.PY3: # pragma: no cover\n return urlsafe_b64encode(value.bytes).decode('utf-8').rstrip('=')\n else:\n return six.text_type(urlsafe_b64encode(value.bytes).rstrip('='))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef newpin(digits=4):\n randnum = randint(0, 10 ** digits)\n while len(str(randnum)) > digits:\n randnum = randint(0, 10 ** digits)\n return (u'%%0%dd' % digits) % randnum", "response": "Return a random numeric string with the specified number of digits."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef make_name(text, delim=u'-', maxlength=50, checkused=None, counter=2):\n name = text.replace('@', delim)\n name = unidecode(name).replace('@', 'a') # We don't know why unidecode uses '@' for 'a'-like chars\n name = six.text_type(delim.join([_strip_re.sub('', x) for x in _punctuation_re.split(name.lower()) if x != '']))\n if isinstance(text, six.text_type):\n # Unidecode returns str. Restore to a unicode string if original was unicode\n name = six.text_type(name)\n candidate = name[:maxlength]\n if candidate.endswith(delim):\n candidate = candidate[:-1]\n if checkused is None:\n return candidate\n existing = checkused(candidate)\n while existing:\n candidate = name[:maxlength - len(str(counter))] + str(counter)\n counter += 1\n existing = checkused(candidate)\n return candidate", "response": "u Generate an ASCII name slug."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef make_password(password, encoding='BCRYPT'):\n if encoding not in ['PLAIN', 'SSHA', 'BCRYPT']:\n raise ValueError(\"Unknown encoding %s\" % encoding)\n if encoding == 'PLAIN':\n if isinstance(password, str) and six.PY2:\n password = six.text_type(password, 'utf-8')\n return '{PLAIN}%s' % password\n elif encoding == 'SSHA':\n # SSHA is a modification of the SHA digest scheme with a salt\n # starting at byte 20 of the base64-encoded string.\n # Source: http://developer.netscape.com/docs/technote/ldap/pass_sha.html\n # This implementation is from Zope2's AccessControl.AuthEncoding.\n\n salt = ''\n for n in range(7):\n salt += chr(randrange(256))\n # b64encode accepts only bytes in Python 3, so salt also has to be encoded\n salt = salt.encode('utf-8') if six.PY3 else salt\n if isinstance(password, six.text_type):\n password = password.encode('utf-8')\n else:\n password = str(password)\n b64_encoded = b64encode(hashlib.sha1(password + salt).digest() + salt)\n b64_encoded = b64_encoded.decode('utf-8') if six.PY3 else b64_encoded\n return '{SSHA}%s' % b64_encoded\n elif encoding == 'BCRYPT':\n # BCRYPT is the recommended hash for secure passwords\n password_hashed = bcrypt.hashpw(\n password.encode('utf-8') if isinstance(password, six.text_type) else password,\n bcrypt.gensalt())\n if six.PY3: # pragma: no cover\n password_hashed = password_hashed.decode('utf-8')\n return '{BCRYPT}%s' % password_hashed", "response": "Make a password with PLAIN SSHA or BCRYPT encoding."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncompares a user s password with a user s attempt.", "response": "def check_password(reference, attempt):\n \"\"\"\n Compare a reference password with the user attempt.\n\n >>> check_password('{PLAIN}foo', 'foo')\n True\n >>> check_password(u'{PLAIN}bar', 'bar')\n True\n >>> check_password(u'{UNKNOWN}baz', 'baz')\n False\n >>> check_password(u'no-encoding', u'no-encoding')\n False\n >>> check_password(u'{SSHA}q/uVU8r15k/9QhRi92CWUwMJu2DM6TUSpp25', u're-foo')\n True\n >>> check_password(u'{BCRYPT}$2b$12$NfKivgz7njR3/rWZ56EsDe7..PPum.fcmFLbdkbP.chtMTcS1s01C', 'foo')\n True\n \"\"\"\n if reference.startswith(u'{PLAIN}'):\n if reference[7:] == attempt:\n return True\n elif reference.startswith(u'{SSHA}'):\n # In python3 b64decode takes inputtype as bytes as opposed to str in python 2, and returns\n # binascii.Error as opposed to TypeError\n if six.PY3: # pragma: no cover\n try:\n if isinstance(reference, six.text_type):\n ref = b64decode(reference[6:].encode('utf-8'))\n else:\n ref = b64decode(reference[6:])\n except binascii.Error:\n return False # Not Base64\n else: # pragma: no cover\n try:\n ref = b64decode(reference[6:])\n except TypeError:\n return False # Not Base64\n if isinstance(attempt, six.text_type):\n attempt = attempt.encode('utf-8')\n salt = ref[20:]\n b64_encoded = b64encode(hashlib.sha1(attempt + salt).digest() + salt)\n if six.PY3: # pragma: no cover\n # type(b64_encoded) is bytes and can't be compared with type(reference) which is str\n compare = six.text_type('{SSHA}%s' % b64_encoded.decode('utf-8') if type(b64_encoded) is bytes else b64_encoded)\n else: # pragma: no cover\n compare = six.text_type('{SSHA}%s' % b64_encoded)\n return compare == reference\n elif reference.startswith(u'{BCRYPT}'):\n # bcrypt.hashpw() accepts either a unicode encoded string or the basic string (python 2)\n if isinstance(attempt, six.text_type) or isinstance(reference, six.text_type):\n attempt = attempt.encode('utf-8')\n reference = reference.encode('utf-8')\n if six.PY3: # pragma: no cover\n return bcrypt.hashpw(attempt, reference[8:]) == reference[8:]\n else: # pragma: no cover\n return bcrypt.hashpw(\n attempt.encode('utf-8') if isinstance(attempt, six.text_type) else attempt,\n str(reference[8:])) == reference[8:]\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef format_currency(value, decimals=2):\n number, decimal = ((u'%%.%df' % decimals) % value).split(u'.')\n parts = []\n while len(number) > 3:\n part, number = number[-3:], number[:-3]\n parts.append(part)\n parts.append(number)\n parts.reverse()\n if int(decimal) == 0:\n return u','.join(parts)\n else:\n return u','.join(parts) + u'.' + decimal", "response": "Return a number suitably formatted for display as currency."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn md5sum of data as a 32 - character string.", "response": "def md5sum(data):\n \"\"\"\n Return md5sum of data as a 32-character string.\n\n >>> md5sum('random text')\n 'd9b9bec3f4cc5482e7c5ef43143e563a'\n >>> md5sum(u'random text')\n 'd9b9bec3f4cc5482e7c5ef43143e563a'\n >>> len(md5sum('random text'))\n 32\n \"\"\"\n if six.PY3: # pragma: no cover\n return hashlib.md5(data.encode('utf-8')).hexdigest()\n else: # pragma: no cover\n return hashlib.md5(data).hexdigest()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a datetime matching the starting point of a specified ISO week in the specified timezone.", "response": "def isoweek_datetime(year, week, timezone='UTC', naive=False):\n \"\"\"\n Returns a datetime matching the starting point of a specified ISO week\n in the specified timezone (default UTC). Returns a naive datetime in\n UTC if requested (default False).\n\n >>> isoweek_datetime(2017, 1)\n datetime.datetime(2017, 1, 2, 0, 0, tzinfo=)\n >>> isoweek_datetime(2017, 1, 'Asia/Kolkata')\n datetime.datetime(2017, 1, 1, 18, 30, tzinfo=)\n >>> isoweek_datetime(2017, 1, 'Asia/Kolkata', naive=True)\n datetime.datetime(2017, 1, 1, 18, 30)\n >>> isoweek_datetime(2008, 1, 'Asia/Kolkata')\n datetime.datetime(2007, 12, 30, 18, 30, tzinfo=)\n \"\"\"\n naivedt = datetime.combine(isoweek.Week(year, week).day(0), datetime.min.time())\n if isinstance(timezone, six.string_types):\n tz = pytz.timezone(timezone)\n else:\n tz = timezone\n dt = tz.localize(naivedt).astimezone(pytz.UTC)\n if naive:\n return dt.replace(tzinfo=None)\n else:\n return dt"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a UTC datetime matching the midnight for the given datetime.", "response": "def midnight_to_utc(dt, timezone=None, naive=False):\n \"\"\"\n Returns a UTC datetime matching the midnight for the given date or datetime.\n\n >>> from datetime import date\n >>> midnight_to_utc(datetime(2017, 1, 1))\n datetime.datetime(2017, 1, 1, 0, 0, tzinfo=)\n >>> midnight_to_utc(pytz.timezone('Asia/Kolkata').localize(datetime(2017, 1, 1)))\n datetime.datetime(2016, 12, 31, 18, 30, tzinfo=)\n >>> midnight_to_utc(datetime(2017, 1, 1), naive=True)\n datetime.datetime(2017, 1, 1, 0, 0)\n >>> midnight_to_utc(pytz.timezone('Asia/Kolkata').localize(datetime(2017, 1, 1)), naive=True)\n datetime.datetime(2016, 12, 31, 18, 30)\n >>> midnight_to_utc(date(2017, 1, 1))\n datetime.datetime(2017, 1, 1, 0, 0, tzinfo=)\n >>> midnight_to_utc(date(2017, 1, 1), naive=True)\n datetime.datetime(2017, 1, 1, 0, 0)\n >>> midnight_to_utc(date(2017, 1, 1), timezone='Asia/Kolkata')\n datetime.datetime(2016, 12, 31, 18, 30, tzinfo=)\n >>> midnight_to_utc(datetime(2017, 1, 1), timezone='Asia/Kolkata')\n datetime.datetime(2016, 12, 31, 18, 30, tzinfo=)\n >>> midnight_to_utc(pytz.timezone('Asia/Kolkata').localize(datetime(2017, 1, 1)), timezone='UTC')\n datetime.datetime(2017, 1, 1, 0, 0, tzinfo=)\n \"\"\"\n if timezone:\n if isinstance(timezone, six.string_types):\n tz = pytz.timezone(timezone)\n else:\n tz = timezone\n elif isinstance(dt, datetime) and dt.tzinfo:\n tz = dt.tzinfo\n else:\n tz = pytz.UTC\n\n utc_dt = tz.localize(datetime.combine(dt, datetime.min.time())).astimezone(pytz.UTC)\n if naive:\n return utc_dt.replace(tzinfo=None)\n return utc_dt"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a boolean from any of a range of values.", "response": "def getbool(value):\n \"\"\"\n Returns a boolean from any of a range of values. Returns None for\n unrecognized values. Numbers other than 0 and 1 are considered\n unrecognized.\n\n >>> getbool(True)\n True\n >>> getbool(1)\n True\n >>> getbool('1')\n True\n >>> getbool('t')\n True\n >>> getbool(2)\n >>> getbool(0)\n False\n >>> getbool(False)\n False\n >>> getbool('n')\n False\n \"\"\"\n value = str(value).lower()\n if value in ['1', 't', 'true', 'y', 'yes']:\n return True\n elif value in ['0', 'f', 'false', 'n', 'no']:\n return False\n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef require_one_of(_return=False, **kwargs):\n\n # Two ways to count number of non-None parameters:\n #\n # 1. sum([1 if v is not None else 0 for v in kwargs.values()])\n #\n # Using a list comprehension instead of a generator comprehension as the\n # parameter to `sum` is faster on both Python 2 and 3.\n #\n # 2. len(kwargs) - kwargs.values().count(None)\n #\n # This is 2x faster than the first method under Python 2.7. Unfortunately,\n # it doesn't work in Python 3 because `kwargs.values()` is a view that doesn't\n # have a `count` method. It needs to be cast into a tuple/list first, but\n # remains faster despite the cast's slowdown. Tuples are faster than lists.\n\n if six.PY3: # pragma: no cover\n count = len(kwargs) - tuple(kwargs.values()).count(None)\n else: # pragma: no cover\n count = len(kwargs) - kwargs.values().count(None)\n\n if count == 0:\n raise TypeError(\"One of these parameters is required: \" + ', '.join(kwargs.keys()))\n elif count != 1:\n raise TypeError(\"Only one of these parameters is allowed: \" + ', '.join(kwargs.keys()))\n\n if _return:\n keys, values = zip(*[(k, 1 if v is not None else 0) for k, v in kwargs.items()])\n k = keys[values.index(1)]\n return k, kwargs[k]", "response": "Validator that raises a TypeError if one and only one parameter is not specified."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef unicode_http_header(value):\n if six.PY3: # pragma: no cover\n # email.header.decode_header expects strings, not bytes. Your input data may be in bytes.\n # Since these bytes are almost always ASCII, calling `.decode()` on it without specifying\n # a charset should work fine.\n if isinstance(value, six.binary_type):\n value = value.decode()\n return u''.join([six.text_type(s, e or 'iso-8859-1') if not isinstance(s, six.text_type) else s\n for s, e in decode_header(value)])", "response": "r Convert an ASCII HTTP header string into a unicode string with the appropriate encoding applied."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of timezones sorted by offset from UTC.", "response": "def sorted_timezones():\n \"\"\"\n Return a list of timezones sorted by offset from UTC.\n \"\"\"\n def hourmin(delta):\n if delta.days < 0:\n hours, remaining = divmod(86400 - delta.seconds, 3600)\n else:\n hours, remaining = divmod(delta.seconds, 3600)\n minutes, remaining = divmod(remaining, 60)\n return hours, minutes\n\n now = datetime.utcnow()\n # Make a list of country code mappings\n timezone_country = {}\n for countrycode in pytz.country_timezones:\n for timezone in pytz.country_timezones[countrycode]:\n timezone_country[timezone] = countrycode\n\n # Make a list of timezones, discarding the US/* and Canada/* zones since they aren't reliable for\n # DST, and discarding UTC and GMT since timezones in that zone have their own names\n timezones = [(pytz.timezone(tzname).utcoffset(now, is_dst=False), tzname) for tzname in pytz.common_timezones\n if not tzname.startswith('US/') and not tzname.startswith('Canada/') and tzname not in ('GMT', 'UTC')]\n # Sort timezones by offset from UTC and their human-readable name\n presorted = [(delta, '%s%s - %s%s (%s)' % (\n (delta.days < 0 and '-') or (delta.days == 0 and delta.seconds == 0 and ' ') or '+',\n '%02d:%02d' % hourmin(delta),\n (pytz.country_names[timezone_country[name]] + ': ') if name in timezone_country else '',\n name.replace('_', ' '),\n pytz.timezone(name).tzname(now, is_dst=False)), name) for delta, name in timezones]\n presorted.sort()\n # Return a list of (timezone, label) with the timezone offset included in the label.\n return [(name, label) for (delta, label, name) in presorted]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef namespace_from_url(url):\n parsed = urlparse(url)\n if parsed.hostname is None or parsed.hostname in ['localhost', 'localhost.localdomain'] or (\n _ipv4_re.search(parsed.hostname)):\n return None\n\n namespace = parsed.hostname.split('.')\n namespace.reverse()\n if namespace and not namespace[0]:\n namespace.pop(0)\n if namespace and namespace[-1] == 'www':\n namespace.pop(-1)\n return type(url)('.'.join(namespace))", "response": "Construct a dotted namespace string from a URL."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef base_domain_matches(d1, d2):\n r1 = tldextract.extract(d1)\n r2 = tldextract.extract(d2)\n # r1 and r2 contain subdomain, domain and suffix.\n # We want to confirm that domain and suffix match.\n return r1.domain == r2.domain and r1.suffix == r2.suffix", "response": "Check if two domains have the same base domain."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef MarkdownColumn(name, deferred=False, group=None, **kwargs):\n return composite(MarkdownComposite,\n Column(name + '_text', UnicodeText, **kwargs),\n Column(name + '_html', UnicodeText, **kwargs),\n deferred=deferred, group=group or name\n )", "response": "Create a composite column that autogenerates HTML from Markdown text and stores data in db columns named with _html and _text prefixes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting plain dictionaries to MutableDict.", "response": "def coerce(cls, key, value):\n \"\"\"Convert plain dictionaries to MutableDict.\"\"\"\n\n if not isinstance(value, MutableDict):\n if isinstance(value, dict):\n return MutableDict(value)\n elif isinstance(value, six.string_types):\n # Assume JSON string\n if value:\n return MutableDict(simplejson.loads(value, use_decimal=True))\n else:\n return MutableDict() # Empty value is an empty dict\n\n # this call will raise ValueError\n return Mutable.coerce(key, value)\n else:\n return value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef require(self, *namespecs):\n blacklist = set([n[1:] for n in namespecs if n.startswith('!')])\n not_blacklist = [n for n in namespecs if not n.startswith('!')]\n return Bundle(*[bundle for name, version, bundle\n in self._require_recursive(*not_blacklist) if name not in blacklist])", "response": "Return a bundle of the requested assets and their dependencies."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the state manager current state label for the current state", "response": "def _state_invalid(self):\n \"\"\"\n If the state is invalid for the transition, return details on what didn't match\n\n :return: Tuple of (state manager, current state, label for current state)\n \"\"\"\n for statemanager, conditions in self.statetransition.transitions.items():\n current_state = getattr(self.obj, statemanager.propname)\n if conditions['from'] is None:\n state_valid = True\n else:\n mstate = conditions['from'].get(current_state)\n state_valid = mstate and mstate(self.obj)\n if state_valid and conditions['if']:\n state_valid = all(v(self.obj) for v in conditions['if'])\n if not state_valid:\n return statemanager, current_state, statemanager.lenum.get(current_state)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd a group of managed states.", "response": "def add_state_group(self, name, *states):\n \"\"\"\n Add a group of managed states. Groups can be specified directly in the\n :class:`~coaster.utils.classes.LabeledEnum`. This method is only useful\n for grouping a conditional state with existing states. It cannot be\n used to form a group of groups.\n\n :param str name: Name of this group\n :param states: :class:`ManagedState` instances to be grouped together\n \"\"\"\n # See `_add_state_internal` for explanation of the following\n if hasattr(self, name):\n raise AttributeError(\n \"State group name %s conflicts with existing attribute in the state manager\" % name)\n mstate = ManagedStateGroup(name, self, states)\n self.states[name] = mstate\n setattr(self, name, mstate)\n setattr(self, 'is_' + name.lower(), mstate)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a conditional state that combines an existing state with a validator that must also pass.", "response": "def add_conditional_state(self, name, state, validator, class_validator=None, cache_for=None, label=None):\n \"\"\"\n Add a conditional state that combines an existing state with a validator\n that must also pass. The validator receives the object on which the property\n is present as a parameter.\n\n :param str name: Name of the new state\n :param ManagedState state: Existing state that this is based on\n :param validator: Function that will be called with the host object as a parameter\n :param class_validator: Function that will be called when the state is queried\n on the class instead of the instance. Falls back to ``validator`` if not specified.\n Receives the class as the parameter\n :param cache_for: Integer or function that indicates how long ``validator``'s\n result can be cached (not applicable to ``class_validator``). ``None`` implies\n no cache, ``0`` implies indefinite cache (until invalidated by a transition)\n and any other integer is the number of seconds for which to cache the assertion\n :param label: Label for this state (string or 2-tuple)\n\n TODO: `cache_for`'s implementation is currently pending a test case demonstrating\n how it will be used.\n \"\"\"\n # We'll accept a ManagedState with grouped values, but not a ManagedStateGroup\n if not isinstance(state, ManagedState):\n raise TypeError(\"Not a managed state: %s\" % repr(state))\n elif state.statemanager != self:\n raise ValueError(\"State %s is not associated with this state manager\" % repr(state))\n if isinstance(label, tuple) and len(label) == 2:\n label = NameTitle(*label)\n self._add_state_internal(name, state.value, label=label,\n validator=validator, class_validator=class_validator, cache_for=cache_for)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef transition(self, from_, to, if_=None, **data):\n def decorator(f):\n if isinstance(f, StateTransition):\n f.add_transition(self, from_, to, if_, data)\n st = f\n else:\n st = StateTransition(f, self, from_, to, if_, data)\n self.transitions.append(st.name)\n return st\n\n return decorator", "response": "Decorator to add a transition to one state to another."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndecorate a method that may be called if the given state is currently active. Registers a transition internally, but does not change the state. :param from_: Required state to allow this call (can be a state group) :param if_: Validator(s) that, given the object, must all return True for the call to proceed :param data: Additional metadata, stored on the `StateTransition` object as a :attr:`data` attribute", "response": "def requires(self, from_, if_=None, **data):\n \"\"\"\n Decorates a method that may be called if the given state is currently active.\n Registers a transition internally, but does not change the state.\n\n :param from_: Required state to allow this call (can be a state group)\n :param if_: Validator(s) that, given the object, must all return True for the call to proceed\n :param data: Additional metadata, stored on the `StateTransition` object as a :attr:`data` attribute\n \"\"\"\n return self.transition(from_, None, if_, **data)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a SQL CHECK constraint string given a column name and a LabeledEnum", "response": "def check_constraint(column, lenum, **kwargs):\n \"\"\"\n Returns a SQL CHECK constraint string given a column name and a\n :class:`~coaster.utils.classes.LabeledEnum`.\n\n Alembic may not detect the CHECK constraint when autogenerating\n migrations, so you may need to do this manually using the Python\n console to extract the SQL string::\n\n from coaster.sqlalchemy import StateManager\n from your_app.models import YOUR_ENUM\n\n print str(StateManager.check_constraint('your_column', YOUR_ENUM).sqltext)\n\n :param str column: Column name\n :param LabeledEnum lenum: :class:`~coaster.utils.classes.LabeledEnum` to retrieve valid values from\n :param kwargs: Additional options passed to CheckConstraint\n \"\"\"\n return CheckConstraint(\n str(column_constructor(column).in_(lenum.keys()).compile(compile_kwargs={'literal_binds': True})),\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the best match for this scalar state.", "response": "def bestmatch(self):\n \"\"\"\n Best matching current scalar state (direct or conditional), only\n applicable when accessed via an instance.\n \"\"\"\n if self.obj is not None:\n for mstate in self.statemanager.all_states_by_value[self.value]:\n msw = mstate(self.obj, self.cls) # This returns a wrapper\n if msw: # If the wrapper evaluates to True, it's our best match\n return msw"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef current(self):\n if self.obj is not None:\n return {name: mstate(self.obj, self.cls)\n for name, mstate in self.statemanager.states.items()\n if mstate(self.obj, self.cls)}", "response": "Return a dictionary of all states and state groups that are currently active."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef transitions(self, current=True):\n if current and isinstance(self.obj, RoleMixin):\n proxy = self.obj.current_access()\n else:\n proxy = {}\n current = False # In case the host object is not a RoleMixin\n return OrderedDict((name, transition) for name, transition in\n # Retrieve transitions from the host object to activate the descriptor.\n ((name, getattr(self.obj, name)) for name in self.statemanager.transitions)\n if transition.is_available and (name in proxy if current else True))", "response": "Returns the available transitions for the current state."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef transitions_for(self, roles=None, actor=None, anchors=[]):\n proxy = self.obj.access_for(roles, actor, anchors)\n return {name: transition for name, transition in self.transitions(current=False).items()\n if name in proxy}", "response": "Returns a dictionary of names of all states that are available for the specified roles actor and anchors."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngive an iterable of instances groups them by state using the state manager s state_by_value property as keys.", "response": "def group(self, items, keep_empty=False):\n \"\"\"\n Given an iterable of instances, groups them by state using :class:`ManagedState` instances\n as dictionary keys. Returns an `OrderedDict` that preserves the order of states from\n the source :class:`~coaster.utils.classes.LabeledEnum`.\n\n :param bool keep_empty: If ``True``, empty states are included in the result\n \"\"\"\n cls = self.cls if self.cls is not None else type(self.obj) # Class of the item being managed\n groups = OrderedDict()\n for mstate in self.statemanager.states_by_value.values():\n # Ensure we sort groups using the order of states in the source LabeledEnum.\n # We'll discard the unused states later.\n groups[mstate] = []\n # Now process the items by state\n for item in items:\n # Use isinstance instead of `type(item) != cls` to account for subclasses\n if not isinstance(item, cls):\n raise TypeError(\"Item %s is not an instance of type %s\" % (repr(item), repr(self.cls)))\n statevalue = self.statemanager._value(item)\n mstate = self.statemanager.states_by_value[statevalue]\n groups[mstate].append(item)\n if not keep_empty:\n for key, value in list(groups.items()):\n if not value:\n del groups[key]\n return groups"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef gfm(text):\n def indent_code(matchobj):\n syntax = matchobj.group(1)\n code = matchobj.group(2)\n if syntax:\n result = ' :::' + syntax + '\\n'\n else:\n result = ''\n # The last line will be blank since it had the closing \"```\". Discard it\n # when indenting the lines.\n return result + '\\n'.join([' ' + line for line in code.split('\\n')[:-1]])\n\n use_crlf = text.find('\\r') != -1\n if use_crlf:\n text = text.replace('\\r\\n', '\\n')\n\n # Render GitHub-style ```code blocks``` into Markdown-style 4-space indented blocks\n text = CODEPATTERN_RE.sub(indent_code, text)\n\n text, code_blocks = remove_pre_blocks(text)\n text, inline_blocks = remove_inline_code_blocks(text)\n\n # Prevent foo_bar_baz from ending up with an italic word in the middle.\n def italic_callback(matchobj):\n s = matchobj.group(0)\n # don't mess with URLs:\n if 'http:' in s or 'https:' in s:\n return s\n\n return s.replace('_', r'\\_')\n\n # fix italics for code blocks\n text = ITALICSPATTERN_RE.sub(italic_callback, text)\n\n # linkify naked URLs\n # wrap the URL in brackets: http://foo -> [http://foo](http://foo)\n text = NAKEDURL_RE.sub(r'\\1[\\2](\\2)\\3', text)\n\n # In very clear cases, let newlines become
tags.\n def newline_callback(matchobj):\n if len(matchobj.group(1)) == 1:\n return matchobj.group(0).rstrip() + ' \\n'\n else:\n return matchobj.group(0)\n\n text = NEWLINE_RE.sub(newline_callback, text)\n\n # now restore removed code blocks\n removed_blocks = code_blocks + inline_blocks\n for removed_block in removed_blocks:\n text = text.replace('{placeholder}', removed_block, 1)\n\n if use_crlf:\n text = text.replace('\\n', '\\r\\n')\n\n return text", "response": "Render a Markdown text into a list of nodes."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef markdown(text, html=False, valid_tags=GFM_TAGS):\n if text is None:\n return None\n if html:\n return Markup(sanitize_html(markdown_convert_html(gfm(text)), valid_tags=valid_tags))\n else:\n return Markup(markdown_convert_text(gfm(text)))", "response": "Return Markdown rendered text using GitHub Flavoured Markdown and syntax - highlighting enabled."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef rulejoin(class_rule, method_rule):\n if method_rule.startswith('/'):\n return method_rule\n else:\n return class_rule + ('' if class_rule.endswith('/') or not method_rule else '/') + method_rule", "response": "Join class and method rules. Used internally by the class view decorators and the method view decorators."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nviewing method decorator that checks the URL of the loaded object in ``self.obj`` against the URL in the request (using ``self.obj.url_for(__name__)``). If the URLs do not match, and the request is a ``GET``, it issues a redirect to the correct URL. Usage:: @route('/doc/') class MyModelView(UrlForView, InstanceLoader, ModelView): model = MyModel route_model_map = {'document': 'url_id_name'} @route('') @url_change_check @render_with(json=True) def view(self): return self.obj.current_access() If the decorator is required for all view handlers in the class, use :class:`UrlChangeCheck`.", "response": "def url_change_check(f):\n \"\"\"\n View method decorator that checks the URL of the loaded object in\n ``self.obj`` against the URL in the request (using\n ``self.obj.url_for(__name__)``). If the URLs do not match,\n and the request is a ``GET``, it issues a redirect to the correct URL.\n Usage::\n\n @route('/doc/')\n class MyModelView(UrlForView, InstanceLoader, ModelView):\n model = MyModel\n route_model_map = {'document': 'url_id_name'}\n\n @route('')\n @url_change_check\n @render_with(json=True)\n def view(self):\n return self.obj.current_access()\n\n If the decorator is required for all view handlers in the class, use\n :class:`UrlChangeCheck`.\n \"\"\"\n @wraps(f)\n def wrapper(self, *args, **kwargs):\n if request.method == 'GET' and self.obj is not None:\n correct_url = self.obj.url_for(f.__name__, _external=True)\n if correct_url != request.base_url:\n if request.query_string:\n correct_url = correct_url + '?' + request.query_string.decode()\n return redirect(correct_url) # TODO: Decide if this should be 302 (default) or 301\n return f(self, *args, **kwargs)\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nregisters routes for a given app and class.", "response": "def init_app(self, app, cls, callback=None):\n \"\"\"\n Register routes for a given app and :class:`ClassView` class. At the\n time of this call, we will always be in the view class even if we were\n originally defined in a base class. :meth:`ClassView.init_app`\n ensures this. :meth:`init_app` therefore takes the liberty of adding\n additional attributes to ``self``:\n\n * :attr:`wrapped_func`: The function wrapped with all decorators added by the class\n * :attr:`view_func`: The view function registered as a Flask view handler\n * :attr:`endpoints`: The URL endpoints registered to this view handler\n \"\"\"\n def view_func(**view_args):\n # view_func does not make any reference to variables from init_app to avoid creating\n # a closure. Instead, the code further below sticks all relevant variables into\n # view_func's namespace.\n\n # Instantiate the view class. We depend on its __init__ requiring no parameters\n viewinst = view_func.view_class()\n # Declare ourselves (the ViewHandler) as the current view. The wrapper makes\n # equivalence tests possible, such as ``self.current_handler == self.index``\n viewinst.current_handler = ViewHandlerWrapper(view_func.view, viewinst, view_func.view_class)\n # Place view arguments in the instance, in case they are needed outside the dispatch process\n viewinst.view_args = view_args\n # Place the view instance on the request stack for :obj:`current_view` to discover\n _request_ctx_stack.top.current_view = viewinst\n # Call the view instance's dispatch method. View classes can customise this for\n # desired behaviour.\n return viewinst.dispatch_request(view_func.wrapped_func, view_args)\n\n # Decorate the wrapped view function with the class's desired decorators.\n # Mixin classes may provide their own decorators, and all of them will be applied.\n # The oldest defined decorators (from mixins) will be applied first, and the\n # class's own decorators last. Within the list of decorators, we reverse the list\n # again, so that a list specified like this:\n #\n # __decorators__ = [first, second]\n #\n # Has the same effect as writing this:\n #\n # @first\n # @second\n # def myview(self):\n # pass\n wrapped_func = self.func\n for base in reversed(cls.__mro__):\n if '__decorators__' in base.__dict__:\n for decorator in reversed(base.__dict__['__decorators__']):\n wrapped_func = decorator(wrapped_func)\n wrapped_func.__name__ = self.name # See below\n\n # Make view_func resemble the underlying view handler method...\n view_func = update_wrapper(view_func, wrapped_func)\n # ...but give view_func the name of the method in the class (self.name),\n # self.name will differ from __name__ only if the view handler method\n # was defined outside the class and then added to the class with a\n # different name.\n view_func.__name__ = self.name\n\n # Stick `wrapped_func` and `cls` into view_func to avoid creating a closure.\n view_func.wrapped_func = wrapped_func\n view_func.view_class = cls\n view_func.view = self\n\n # Keep a copy of these functions (we already have self.func)\n self.wrapped_func = wrapped_func\n self.view_func = view_func\n\n for class_rule, class_options in cls.__routes__:\n for method_rule, method_options in self.routes:\n use_options = dict(method_options)\n use_options.update(class_options)\n endpoint = use_options.pop('endpoint', self.endpoint)\n self.endpoints.add(endpoint)\n use_rule = rulejoin(class_rule, method_rule)\n app.add_url_rule(use_rule, endpoint, view_func, **use_options)\n if callback:\n callback(use_rule, endpoint, view_func, **use_options)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nindicates whether this view is available in the current context", "response": "def is_available(self):\n \"\"\"Indicates whether this view is available in the current context\"\"\"\n if hasattr(self._viewh.wrapped_func, 'is_available'):\n return self._viewh.wrapped_func.is_available(self._obj)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dispatch_request(self, view, view_args):\n # Call the :meth:`before_request` method\n resp = self.before_request()\n if resp:\n return self.after_request(make_response(resp))\n # Call the view handler method, then pass the response to :meth:`after_response`\n return self.after_request(make_response(view(self, **view_args)))", "response": "Dispatches a request to the specified view and returns the response."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if any view handler in the class is currently available via its is_available method.", "response": "def is_available(self):\n \"\"\"\n Returns `True` if *any* view handler in the class is currently\n available via its `is_available` method.\n \"\"\"\n if self.is_always_available:\n return True\n for viewname in self.__views__:\n if getattr(self, viewname).is_available():\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_route_for(cls, _name, rule, **options):\n setattr(cls, _name, route(rule, **options)(cls.__get_raw_attr(_name)))", "response": "Add a route for an existing method or view. Useful for modifying routes\n ."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nregister views on an app.", "response": "def init_app(cls, app, callback=None):\n \"\"\"\n Register views on an app. If :attr:`callback` is specified, it will\n be called after ``app.``:meth:`~flask.Flask.add_url_rule`, with the same\n parameters.\n \"\"\"\n processed = set()\n cls.__views__ = set()\n cls.is_always_available = False\n for base in cls.__mro__:\n for name, attr in base.__dict__.items():\n if name in processed:\n continue\n processed.add(name)\n if isinstance(attr, ViewHandler):\n if base != cls: # Copy ViewHandler instances into subclasses\n # TODO: Don't do this during init_app. Use a metaclass\n # and do this when the class is defined.\n attr = attr.copy_for_subclass()\n setattr(cls, name, attr)\n attr.__set_name__(cls, name) # Required for Python < 3.6\n cls.__views__.add(name)\n attr.init_app(app, cls, callback=callback)\n if not hasattr(attr.wrapped_func, 'is_available'):\n cls.is_always_available = True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndispatches a request to the specified view method and returns the response.", "response": "def dispatch_request(self, view, view_args):\n \"\"\"\n View dispatcher that calls :meth:`before_request`, :meth:`loader`,\n :meth:`after_loader`, the view, and then :meth:`after_request`.\n\n :param view: View method wrapped in specified decorators.\n :param dict view_args: View arguments, to be passed on to the view method\n \"\"\"\n # Call the :meth:`before_request` method\n resp = self.before_request()\n if resp:\n return self.after_request(make_response(resp))\n # Load the database model\n self.obj = self.loader(**view_args)\n # Trigger pre-view processing of the loaded object\n resp = self.after_loader()\n if resp:\n return self.after_request(make_response(resp))\n # Call the view handler method, then pass the response to :meth:`after_response`\n return self.after_request(make_response(view(self)))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef declared_attr_roles(rw=None, call=None, read=None, write=None):\n def inner(f):\n @wraps(f)\n def attr(cls):\n # Pass f(cls) as a parameter to with_roles.inner to avoid the test for\n # iterables within with_roles. We have no idea about the use cases for\n # declared_attr in downstream code. There could be a declared_attr\n # that returns a list that should be accessible via the proxy.\n return with_roles(rw=rw, call=call, read=read, write=write)(f(cls))\n return attr\n warnings.warn(\"declared_attr_roles is deprecated; use with_roles\", stacklevel=2)\n return inner", "response": "Decorator for the class that has declared_attr_roles."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconfiguring the class s roles based on the base classes.", "response": "def __configure_roles(mapper, cls):\n \"\"\"\n Run through attributes of the class looking for role decorations from\n :func:`with_roles` and add them to :attr:`cls.__roles__`\n \"\"\"\n # Don't mutate ``__roles__`` in the base class.\n # The subclass must have its own.\n # Since classes may specify ``__roles__`` directly without\n # using :func:`with_roles`, we must preserve existing content.\n if '__roles__' not in cls.__dict__:\n # If the following line is confusing, it's because reading an\n # attribute on an object invokes the Method Resolution Order (MRO)\n # mechanism to find it on base classes, while writing always writes\n # to the current object.\n cls.__roles__ = deepcopy(cls.__roles__)\n\n # An attribute may be defined more than once in base classes. Only handle the first\n processed = set()\n\n # Loop through all attributes in this and base classes, looking for role annotations\n for base in cls.__mro__:\n for name, attr in base.__dict__.items():\n if name in processed or name.startswith('__'):\n continue\n\n if isinstance(attr, collections.Hashable) and attr in __cache__:\n data = __cache__[attr]\n del __cache__[attr]\n elif isinstance(attr, InstrumentedAttribute) and attr.property in __cache__:\n data = __cache__[attr.property]\n del __cache__[attr.property]\n elif hasattr(attr, '_coaster_roles'):\n data = attr._coaster_roles\n else:\n data = None\n if data is not None:\n for role in data.get('call', []):\n cls.__roles__.setdefault(role, {}).setdefault('call', set()).add(name)\n for role in data.get('read', []):\n cls.__roles__.setdefault(role, {}).setdefault('read', set()).add(name)\n for role in data.get('write', []):\n cls.__roles__.setdefault(role, {}).setdefault('write', set()).add(name)\n processed.add(name)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef current_roles(self):\n return InspectableSet(self.roles_for(actor=current_auth.actor, anchors=current_auth.anchors))", "response": "Return a InspectableSet containing currently\n available roles on this object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef access_for(self, roles=None, actor=None, anchors=[]):\n if roles is None:\n roles = self.roles_for(actor=actor, anchors=anchors)\n elif actor is not None or anchors:\n raise TypeError('If roles are specified, actor/anchors must not be specified')\n return RoleAccessProxy(self, roles=roles)", "response": "Returns a proxy object that limits read and write access to attributes\n based on the actor s roles."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwraps :meth:`access_for` with :obj:`~coaster.auth.current_auth` to return a proxy for the currently authenticated user.", "response": "def current_access(self):\n \"\"\"\n Wraps :meth:`access_for` with :obj:`~coaster.auth.current_auth` to\n return a proxy for the currently authenticated user.\n \"\"\"\n return self.access_for(actor=current_auth.actor, anchors=current_auth.anchors)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_current_url():\n if current_app.config.get('SERVER_NAME') and (\n # Check current hostname against server name, ignoring port numbers, if any (split on ':')\n request.environ['HTTP_HOST'].split(':', 1)[0] != current_app.config['SERVER_NAME'].split(':', 1)[0]):\n return request.url\n\n url = url_for(request.endpoint, **request.view_args)\n query = request.query_string\n if query:\n return url + '?' + query.decode()\n else:\n return url", "response": "Return the current URL including the query string as a relative path."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_next_url(referrer=False, external=False, session=False, default=__marker):\n if session:\n next_url = request_session.pop('next', None) or request.args.get('next', '')\n else:\n next_url = request.args.get('next', '')\n if next_url and not external:\n next_url = __clean_external_url(next_url)\n if next_url:\n return next_url\n\n if default is __marker:\n usedefault = False\n else:\n usedefault = True\n\n if referrer and request.referrer:\n if external:\n return request.referrer\n else:\n return __clean_external_url(request.referrer) or (default if usedefault else __index_url())\n else:\n return default if usedefault else __index_url()", "response": "Get the next URL to redirect to. Don t return external URLs unless explicitly asked for."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a JSON response with a callback wrapper if asked for.", "response": "def jsonp(*args, **kw):\n \"\"\"\n Returns a JSON response with a callback wrapper, if asked for.\n Consider using CORS instead, as JSONP makes the client app insecure.\n See the :func:`~coaster.views.decorators.cors` decorator.\n \"\"\"\n data = json.dumps(dict(*args, **kw), indent=2)\n callback = request.args.get('callback', request.args.get('jsonp'))\n if callback and __jsoncallback_re.search(callback) is not None:\n data = callback + u'(' + data + u');'\n mimetype = 'application/javascript'\n else:\n mimetype = 'application/json'\n return Response(data, mimetype=mimetype)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngiving an absolute URL retrieve the matching endpoint name or view arguments.", "response": "def endpoint_for(url, method=None, return_rule=False, follow_redirects=True):\n \"\"\"\n Given an absolute URL, retrieve the matching endpoint name (or rule) and\n view arguments. Requires a current request context to determine runtime\n environment.\n\n :param str method: HTTP method to use (defaults to GET)\n :param bool return_rule: Return the URL rule instead of the endpoint name\n :param bool follow_redirects: Follow redirects to final endpoint\n :return: Tuple of endpoint name or URL rule or `None`, view arguments\n \"\"\"\n parsed_url = urlsplit(url)\n if not parsed_url.netloc:\n # We require an absolute URL\n return None, {}\n\n # Take the current runtime environment...\n environ = dict(request.environ)\n # ...but replace the HTTP host with the URL's host...\n environ['HTTP_HOST'] = parsed_url.netloc\n # ...and the path with the URL's path (after discounting the app path, if not hosted at root).\n environ['PATH_INFO'] = parsed_url.path[len(environ.get('SCRIPT_NAME', '')):]\n # Create a new request with this environment...\n url_request = current_app.request_class(environ)\n # ...and a URL adapter with the new request.\n url_adapter = current_app.create_url_adapter(url_request)\n\n # Run three hostname tests, one of which must pass:\n\n # 1. Does the URL map have host matching enabled? If so, the URL adapter will validate the hostname.\n if current_app.url_map.host_matching:\n pass\n\n # 2. If not, does the domain match? url_adapter.server_name will prefer app.config['SERVER_NAME'],\n # but if that is not specified, it will take it from the environment.\n elif parsed_url.netloc == url_adapter.server_name:\n pass\n\n # 3. If subdomain matching is enabled, does the subdomain match?\n elif current_app.subdomain_matching and parsed_url.netloc.endswith('.' + url_adapter.server_name):\n pass\n\n # If no test passed, we don't have a matching endpoint.\n else:\n return None, {}\n\n # Now retrieve the endpoint or rule, watching for redirects or resolution failures\n try:\n return url_adapter.match(parsed_url.path, method, return_rule=return_rule)\n except RequestRedirect as r:\n # A redirect typically implies `/folder` -> `/folder/`\n # This will not be a redirect response from a view, since the view isn't being called\n if follow_redirects:\n return endpoint_for(r.new_url, method=method, return_rule=return_rule, follow_redirects=follow_redirects)\n except (NotFound, MethodNotAllowed):\n pass\n # If we got here, no endpoint was found.\n return None, {}"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a set of permissions for this workflow.", "response": "def permissions(self):\n \"\"\"\n Permissions for this workflow. Plays nice with\n :meth:`coaster.views.load_models` and\n :class:`coaster.sqlalchemy.PermissionMixin` to determine the available\n permissions to the current user.\n \"\"\"\n perms = set(super(DocumentWorkflow, self).permissions())\n if g:\n if hasattr(g, 'permissions'):\n perms.update(g.permissions or [])\n if hasattr(self.document, 'permissions'):\n perms = self.document.permissions(current_auth.actor, perms)\n return perms"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconfigure the annotations of the class.", "response": "def __configure_annotations(mapper, cls):\n \"\"\"\n Run through attributes of the class looking for annotations from\n :func:`annotation_wrapper` and add them to :attr:`cls.__annotations__`\n and :attr:`cls.__annotations_by_attr__`\n \"\"\"\n annotations = {}\n annotations_by_attr = {}\n\n # An attribute may be defined more than once in base classes. Only handle the first\n processed = set()\n\n # Loop through all attributes in the class and its base classes, looking for annotations\n for base in cls.__mro__:\n for name, attr in base.__dict__.items():\n if name in processed or name.startswith('__'):\n continue\n\n # 'data' is a list of string annotations\n if isinstance(attr, collections.Hashable) and attr in __cache__:\n data = __cache__[attr]\n del __cache__[attr]\n elif isinstance(attr, InstrumentedAttribute) and attr.property in __cache__:\n data = __cache__[attr.property]\n del __cache__[attr.property]\n elif hasattr(attr, '_coaster_annotations'):\n data = attr._coaster_annotations\n else:\n data = None\n if data is not None:\n annotations_by_attr.setdefault(name, []).extend(data)\n for a in data:\n annotations.setdefault(a, []).append(name)\n processed.add(name)\n\n # Classes specifying ``__annotations__`` directly isn't supported,\n # so we don't bother preserving existing content, if any.\n if annotations:\n cls.__annotations__ = annotations\n if annotations_by_attr:\n cls.__annotations_by_attr__ = annotations_by_attr\n annotations_configured.send(cls)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef annotation_wrapper(annotation, doc=None):\n def decorator(attr):\n __cache__.setdefault(attr, []).append(annotation)\n # Also mark the annotation on the object itself. This will\n # fail if the object has a restrictive __slots__, but it's\n # required for some objects like Column because SQLAlchemy copies\n # them in subclasses, changing their hash and making them\n # undiscoverable via the cache.\n try:\n if not hasattr(attr, '_coaster_annotations'):\n setattr(attr, '_coaster_annotations', [])\n attr._coaster_annotations.append(annotation)\n except AttributeError:\n pass\n return attr\n\n decorator.__name__ = decorator.name = annotation\n decorator.__doc__ = doc\n return decorator", "response": "A decorator that adds an annotation to an attribute."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_auth_attribute(attr, value, actor=False):\n if attr in ('actor', 'anchors', 'is_anonymous', 'not_anonymous', 'is_authenticated', 'not_authenticated'):\n raise AttributeError(\"Attribute name %s is reserved by current_auth\" % attr)\n\n # Invoking current_auth will also create it on the local stack. We can\n # then proceed to set attributes on it.\n ca = current_auth._get_current_object()\n # Since :class:`CurrentAuth` overrides ``__setattr__``, we need to use :class:`object`'s.\n object.__setattr__(ca, attr, value)\n\n if attr == 'user':\n # Special-case 'user' for compatibility with Flask-Login\n _request_ctx_stack.top.user = value\n # A user is always an actor\n actor = True\n\n if actor:\n object.__setattr__(ca, 'actor', value)", "response": "Helper function for login managers. Adds authorization attributes to the current_auth object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef build_attributes(cls, attributes, namespace):\n config_path = attributes.get('config_path')\n tokens = {}\n\n def build_config_key(value_def, config_key):\n key = value_def.config_key or config_key\n return '%s.%s' % (config_path, key) if config_path else key\n\n def build_token(name, value_def):\n config_key = build_config_key(value_def, name)\n value_token = ValueToken.from_definition(\n value_def, namespace, config_key)\n getters.register_value_proxy(namespace, value_token, value_def.help)\n tokens[name] = value_token\n return name, build_property(value_token)\n\n def build_attr(name, attribute):\n if not isinstance(attribute, ValueTypeDefinition):\n return name, attribute\n return build_token(name, attribute)\n\n attributes = dict(build_attr(*item)\n for item in six.iteritems(attributes))\n attributes['_tokens'] = tokens\n return attributes", "response": "Build a dictionary of attributes with ValueTokens replaced by a\n property which returns the config value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cache_as_field(cache_name):\n def cache_wrapper(func):\n @functools.wraps(func)\n def inner_wrapper(self, *args, **kwargs):\n value = getattr(self, cache_name, UndefToken)\n if value != UndefToken:\n return value\n\n ret = func(self, *args, **kwargs)\n setattr(self, cache_name, ret)\n return ret\n return inner_wrapper\n return cache_wrapper", "response": "Cache a functions return value as the field cache_name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef extract_value(proxy):\n value = proxy.namespace.get(proxy.config_key, proxy.default)\n if value is UndefToken:\n raise errors.ConfigurationError(\"%s is missing value for: %s\" %\n (proxy.namespace, proxy.config_key))\n\n try:\n return proxy.validator(value)\n except errors.ValidationError as e:\n raise errors.ConfigurationError(\"%s failed to validate %s: %s\" %\n (proxy.namespace, proxy.config_key, e))", "response": "Given a value proxy type Retrieve a value from a namespace raising an exception if no value is found raising an exception if the value does not validate."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef build_reader(validator, reader_namespace=config.DEFAULT):\n def reader(config_key, default=UndefToken, namespace=None):\n config_namespace = config.get_namespace(namespace or reader_namespace)\n return validator(_read_config(config_key, config_namespace, default))\n return reader", "response": "A factory method for creating a custom config reader from a validation\n function."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_namespaces_from_names(name, all_names):\n names = configuration_namespaces.keys() if all_names else [name]\n for name in names:\n yield get_namespace(name)", "response": "Return a generator which yields namespace objects."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_namespace(name):\n if name not in configuration_namespaces:\n configuration_namespaces[name] = ConfigNamespace(name)\n return configuration_namespaces[name]", "response": "Return a ConfigNamespace by name creating it if it does not exist."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreload one or all namespaces.", "response": "def reload(name=DEFAULT, all_names=False):\n \"\"\"Reload one or all :class:`ConfigNamespace`. Reload clears the cache of\n :mod:`staticconf.schema` and :mod:`staticconf.getters`, allowing them to\n pickup the latest values in the namespace.\n\n Defaults to reloading just the DEFAULT namespace.\n\n :param name: the name of the :class:`ConfigNamespace` to reload\n :param all_names: If True, reload all namespaces, and ignore `name`\n \"\"\"\n for namespace in get_namespaces_from_names(name, all_names):\n for value_proxy in namespace.get_value_proxies():\n value_proxy.reset()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef validate(name=DEFAULT, all_names=False):\n for namespace in get_namespaces_from_names(name, all_names):\n all(value_proxy.get_value() for value_proxy in namespace.get_value_proxies())", "response": "Validate all registered keys or values in a given namespace."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompare two dictionaries for duplicate keys.", "response": "def has_duplicate_keys(config_data, base_conf, raise_error):\n \"\"\"Compare two dictionaries for duplicate keys. if raise_error is True\n then raise on exception, otherwise log return True.\"\"\"\n duplicate_keys = set(base_conf) & set(config_data)\n if not duplicate_keys:\n return\n msg = \"Duplicate keys in config: %s\" % duplicate_keys\n if raise_error:\n raise errors.ConfigurationError(msg)\n log.info(msg)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding a compare_func that can be passed to MTimeComparator.", "response": "def build_compare_func(err_logger=None):\n \"\"\"Returns a compare_func that can be passed to MTimeComparator.\n\n The returned compare_func first tries os.path.getmtime(filename),\n then calls err_logger(filename) if that fails. If err_logger is None,\n then it does nothing. err_logger is always called within the context of\n an OSError raised by os.path.getmtime(filename). Information on this\n error can be retrieved by calling sys.exc_info inside of err_logger.\"\"\"\n def compare_func(filename):\n try:\n return os.path.getmtime(filename)\n except OSError:\n if err_logger is not None:\n err_logger(filename)\n return -1\n return compare_func"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreconstructs the nested structure of this object s configuration and return it as a dict.", "response": "def get_config_dict(self):\n \"\"\"Reconstruct the nested structure of this object's configuration\n and return it as a dict.\n \"\"\"\n config_dict = {}\n for dotted_key, value in self.get_config_values().items():\n subkeys = dotted_key.split('.')\n d = config_dict\n for key in subkeys:\n d = d.setdefault(key, value if key == subkeys[-1] else {})\n return config_dict"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef view_help(self):\n def format_desc(desc):\n return \"%s (Type: %s, Default: %s)\\n%s\" % (\n desc.name,\n desc.validator.__name__.replace('validate_', ''),\n desc.default,\n desc.help or '')\n\n def format_namespace(key, desc_list):\n return \"\\nNamespace: %s\\n%s\" % (\n key,\n '\\n'.join(sorted(format_desc(desc) for desc in desc_list)))\n\n def namespace_cmp(item):\n name, _ = item\n return chr(0) if name == DEFAULT else name\n\n return '\\n'.join(format_namespace(*desc) for desc in\n sorted(six.iteritems(self.descriptions),\n key=namespace_cmp))", "response": "Return a help message describing all the statically configured keys."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef reload_if_changed(self, force=False):\n if (force or self.should_check) and self.file_modified():\n return self.reload()", "response": "Reloads the configuration of the object if the file has changed."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a configuration file into a new namespace.", "response": "def load(\n cls,\n filename,\n namespace,\n loader_func,\n min_interval=0,\n comparators=None,\n ):\n \"\"\"Create a new :class:`ConfigurationWatcher` and load the initial\n configuration by calling `loader_func`.\n\n :param filename: a filename or list of filenames to monitor for changes\n :param namespace: the name of a namespace to use when loading\n configuration. All config data from `filename` will\n end up in a :class:`ConfigNamespace` with this name\n :param loader_func: a function which accepts two arguments and uses\n loader functions from :mod:`staticconf.loader` to\n load configuration data into a namespace. The\n arguments are `filename` and `namespace`\n :param min_interval: minimum number of seconds to wait between calls to\n :func:`os.path.getmtime` to check if a file has\n been modified.\n :param comparators: a list of classes which support the\n :class:`IComparator` interface which are used to determine if a config\n file has been modified. See ConfigurationWatcher::__init__.\n :returns: a :class:`ConfigFacade`\n \"\"\"\n watcher = ConfigurationWatcher(\n build_loader_callable(loader_func, filename, namespace=namespace),\n filename,\n min_interval=min_interval,\n reloader=ReloadCallbackChain(namespace=namespace),\n comparators=comparators,\n )\n watcher.load_config()\n return cls(watcher)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _validate_iterable(iterable_type, value):\n if isinstance(value, six.string_types):\n msg = \"Invalid iterable of type(%s): %s\"\n raise ValidationError(msg % (type(value), value))\n\n try:\n return iterable_type(value)\n except TypeError:\n raise ValidationError(\"Invalid iterable: %s\" % (value))", "response": "Convert the iterable to iterable_type or raise a Configuration\n exception."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build_list_type_validator(item_validator):\n def validate_list_of_type(value):\n return [item_validator(item) for item in validate_list(value)]\n return validate_list_of_type", "response": "Build a function which validates that the value is a list of items\n which are validated using item_validator."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build_map_type_validator(item_validator):\n def validate_mapping(value):\n return dict(item_validator(item) for item in validate_list(value))\n return validate_mapping", "response": "Return a function which validates that the value is a mapping of\n items. The function should return pairs of items that will be\n ."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nregisters a value proxy with the namespace and add the help_text.", "response": "def register_value_proxy(namespace, value_proxy, help_text):\n \"\"\"Register a value proxy with the namespace, and add the help_text.\"\"\"\n namespace.register_proxy(value_proxy)\n config.config_help.add(\n value_proxy.config_key, value_proxy.validator, value_proxy.default,\n namespace.get_name(), help_text)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_getter(validator, getter_namespace=None):\n def proxy_register(key_name, default=UndefToken, help=None, namespace=None):\n name = namespace or getter_namespace or config.DEFAULT\n namespace = config.get_namespace(name)\n return proxy_factory.build(validator, namespace, key_name, default, help)\n\n return proxy_register", "response": "Create a getter function for retrieving values from the config cache."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef build(self, validator, namespace, config_key, default, help):\n proxy_attrs = validator, namespace, config_key, default\n proxy_key = repr(proxy_attrs)\n if proxy_key in self.proxies:\n return self.proxies[proxy_key]\n\n value_proxy = proxy.ValueProxy(*proxy_attrs)\n register_value_proxy(namespace, value_proxy, help)\n return self.proxies.setdefault(proxy_key, value_proxy)", "response": "Build or retrieve a ValueProxy from the attributes."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef minimizeSPSA(func, x0, args=(), bounds=None, niter=100, paired=True,\n a=1.0, alpha=0.602, c=1.0, gamma=0.101,\n disp=False, callback=None):\n \"\"\"\n Minimization of an objective function by a simultaneous perturbation\n stochastic approximation algorithm.\n\n This algorithm approximates the gradient of the function by finite differences\n along stochastic directions Deltak. The elements of Deltak are drawn from\n +- 1 with probability one half. The gradient is approximated from the \n symmetric difference f(xk + ck*Deltak) - f(xk - ck*Deltak), where the evaluation\n step size ck is scaled according ck = c/(k+1)**gamma.\n The algorithm takes a step of size ak = a/(0.01*niter+k+1)**alpha along the\n negative gradient.\n \n\n See Spall, IEEE, 1998, 34, 817-823 for guidelines about how to choose the algorithm's\n parameters (a, alpha, c, gamma).\n\n Parameters\n ----------\n func: callable\n objective function to be minimized:\n called as `func(x, *args)`,\n if `paired=True`, then called with keyword argument `seed` additionally\n x0: array-like\n initial guess for parameters \n args: tuple\n extra arguments to be supplied to func\n bounds: array-like\n bounds on the variables\n niter: int\n number of iterations after which to terminate the algorithm\n paired: boolean\n calculate gradient for same random seeds\n a: float\n scaling parameter for step size\n alpha: float\n scaling exponent for step size\n c: float\n scaling parameter for evaluation step size\n gamma: float\n scaling exponent for evaluation step size \n disp: boolean\n whether to output status updates during the optimization\n callback: callable\n called after each iteration, as callback(xk), where xk are the current parameters\n\n Returns\n -------\n `scipy.optimize.OptimizeResult` object\n \"\"\"\n A = 0.01 * niter\n\n if bounds is not None:\n bounds = np.asarray(bounds)\n project = lambda x: np.clip(x, bounds[:, 0], bounds[:, 1])\n\n if args is not None:\n # freeze function arguments\n def funcf(x, **kwargs):\n return func(x, *args, **kwargs)\n\n N = len(x0)\n x = x0\n for k in range(niter):\n ak = a/(k+1.0+A)**alpha\n ck = c/(k+1.0)**gamma\n Deltak = np.random.choice([-1, 1], size=N)\n fkwargs = dict()\n if paired:\n fkwargs['seed'] = np.random.randint(0, np.iinfo(np.uint32).max)\n if bounds is None:\n grad = (funcf(x + ck*Deltak, **fkwargs) - funcf(x - ck*Deltak, **fkwargs)) / (2*ck*Deltak)\n x -= ak*grad\n else:\n # ensure evaluation points are feasible\n xplus = project(x + ck*Deltak)\n xminus = project(x - ck*Deltak)\n grad = (funcf(xplus, **fkwargs) - funcf(xminus, **fkwargs)) / (xplus-xminus)\n x = project(x - ak*grad)\n # print 100 status updates if disp=True\n if disp and (k % (niter//100)) == 0:\n print(x)\n if callback is not None:\n callback(x)\n message = 'terminated after reaching max number of iterations'\n return OptimizeResult(fun=funcf(x), x=x, nit=niter, nfev=2*niter, message=message, success=True)", "response": "Minimization of an objective function by a simultaneous perturbation of a single object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfinding root by bysection search.", "response": "def bisect(func, a, b, xtol=1e-6, errorcontrol=True,\n testkwargs=dict(), outside='extrapolate',\n ascending=None,\n disp=False):\n \"\"\"Find root by bysection search.\n\n If the function evaluation is noisy then use `errorcontrol=True` for adaptive\n sampling of the function during the bisection search.\n\n Parameters\n ----------\n func: callable\n Function of which the root should be found. If `errorcontrol=True`\n then the function should be derived from `AverageBase`.\n a, b: float\n initial interval\n xtol: float\n target tolerance for interval size\n errorcontrol: boolean\n if true, assume that function is derived from `AverageBase`.\n testkwargs: only for `errorcontrol=True`\n see `AverageBase.test0`\n outside: ['extrapolate', 'raise']\n How to handle the case where f(a) and f(b) have same sign,\n i.e. where the root lies outside of the interval.\n If 'raise' throws a `BisectException`.\n ascending: allow passing in directly whether function is ascending or not\n if ascending=True then it is assumed without check that f(a) < 0 and f(b) > 0\n if ascending=False then it is assumed without check that f(a) > 0 and f(b) < 0\n\n Returns\n -------\n float, root of function\n \"\"\"\n search = True\n # check whether function is ascending or not\n if ascending is None:\n if errorcontrol:\n testkwargs.update(dict(type_='smaller', force=True))\n fa = func.test0(a, **testkwargs)\n fb = func.test0(b, **testkwargs)\n else:\n fa = func(a) < 0\n fb = func(b) < 0\n if fa and not fb:\n ascending = True\n elif fb and not fa:\n ascending = False\n else:\n if disp:\n print('Warning: func(a) and func(b) do not have opposing signs -> no search done')\n if outside == 'raise':\n raise BisectException()\n search = False\n\n # refine interval until it has reached size xtol, except if root outside\n while (b-a > xtol) and search:\n mid = (a+b)/2.0\n if ascending:\n if ((not errorcontrol) and (func(mid) < 0)) or \\\n (errorcontrol and func.test0(mid, **testkwargs)):\n a = mid \n else:\n b = mid\n else:\n if ((not errorcontrol) and (func(mid) < 0)) or \\\n (errorcontrol and func.test0(mid, **testkwargs)):\n b = mid \n else:\n a = mid\n if disp:\n print('bisect bounds', a, b)\n # interpolate linearly to get zero\n if errorcontrol:\n ya, yb = func(a)[0], func(b)[0]\n else:\n ya, yb = func(a), func(b)\n m = (yb-ya) / (b-a)\n res = a-ya/m\n if disp:\n print('bisect final value', res)\n return res"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndocument : selection_set fragment_list", "response": "def p_document_shorthand_with_fragments(self, p):\n \"\"\"\n document : selection_set fragment_list\n \"\"\"\n p[0] = Document(definitions=[Query(selections=p[1])] + p[2])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse an operation definition", "response": "def p_operation_definition1(self, p):\n \"\"\"\n operation_definition : operation_type name variable_definitions directives selection_set\n \"\"\"\n p[0] = self.operation_cls(p[1])(\n selections=p[5],\n name=p[2],\n variable_definitions=p[3],\n directives=p[4],\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse the 2nd parameter of the operation definition", "response": "def p_operation_definition2(self, p):\n \"\"\"\n operation_definition : operation_type name variable_definitions selection_set\n \"\"\"\n p[0] = self.operation_cls(p[1])(\n selections=p[4],\n name=p[2],\n variable_definitions=p[3],\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse the 3rd party operation definition.", "response": "def p_operation_definition3(self, p):\n \"\"\"\n operation_definition : operation_type name directives selection_set\n \"\"\"\n p[0] = self.operation_cls(p[1])(\n selections=p[4],\n name=p[2],\n directives=p[3],\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_operation_definition4(self, p):\n p[0] = self.operation_cls(p[1])(selections=p[3], name=p[2])", "response": "P 3. 4. 2. 2."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_operation_definition5(self, p):\n p[0] = self.operation_cls(p[1])(\n selections=p[4],\n variable_definitions=p[2],\n directives=p[3],\n )", "response": "Parse the 5 part of the operation definition."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_operation_definition6(self, p):\n p[0] = self.operation_cls(p[1])(\n selections=p[3],\n variable_definitions=p[2],\n )", "response": "P 6 - level operation definition"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_operation_definition7(self, p):\n p[0] = self.operation_cls(p[1])(\n selections=p[3],\n directives=p[2],\n )", "response": "Parse the 7th section of the resource definition."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfields : alias name arguments directives selection_set", "response": "def p_field_all(self, p):\n \"\"\"\n field : alias name arguments directives selection_set\n \"\"\"\n p[0] = Field(name=p[2], alias=p[1], arguments=p[3], directives=p[4],\n selections=p[5])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the optional 1. 0 field.", "response": "def p_field_optional1_1(self, p):\n \"\"\"\n field : name arguments directives selection_set\n \"\"\"\n p[0] = Field(name=p[1], arguments=p[2], directives=p[3],\n selections=p[5])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_field_optional1_2(self, p):\n p[0] = Field(name=p[2], alias=p[1], directives=p[3], selections=p[5])", "response": "Parse the optional 1. 2 section of the spec."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_field_optional1_3(self, p):\n p[0] = Field(name=p[2], alias=p[1], arguments=p[3], selections=p[4])", "response": "Parse the optional 1. 3. 1. 2 field."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing the optional 1. 4 section of the log message.", "response": "def p_field_optional1_4(self, p):\n \"\"\"\n field : alias name arguments directives\n \"\"\"\n p[0] = Field(name=p[2], alias=p[1], arguments=p[3], directives=p[4])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_field_optional2_1(self, p):\n p[0] = Field(name=p[1], directives=p[2], selections=p[3])", "response": "Parse the optional 2. 1 section of the spec."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfield : name arguments selection_set", "response": "def p_field_optional2_2(self, p):\n \"\"\"\n field : name arguments selection_set\n \"\"\"\n p[0] = Field(name=p[1], arguments=p[2], selections=p[3])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_field_optional2_3(self, p):\n p[0] = Field(name=p[1], arguments=p[2], directives=p[3])", "response": "Parse the optional 2. 3. 1. 2."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nparse optional 2. 4. 1. 2.", "response": "def p_field_optional2_4(self, p):\n \"\"\"\n field : alias name selection_set\n \"\"\"\n p[0] = Field(name=p[2], alias=p[1], selections=p[3])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse an optional 2. 5 field.", "response": "def p_field_optional2_5(self, p):\n \"\"\"\n field : alias name directives\n \"\"\"\n p[0] = Field(name=p[2], alias=p[1], directives=p[3])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_field_optional2_6(self, p):\n p[0] = Field(name=p[2], alias=p[1], arguments=p[3])", "response": "Parse the optional 2. 6. 1. 1 Field."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_directive(self, p):\n arguments = p[3] if len(p) == 4 else None\n p[0] = Directive(name=p[2], arguments=arguments)", "response": "parse a tag directive"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_const_object_field_list(self, p):\n obj = p[1].copy()\n obj.update(p[2])\n p[0] = obj", "response": "A function to update the const_object_field_list field of a class."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsort a list of triples by relation name.", "response": "def alphanum_order(triples):\n \"\"\"\n Sort a list of triples by relation name.\n\n Embedded integers are sorted numerically, but otherwise the sorting\n is alphabetic.\n \"\"\"\n return sorted(\n triples,\n key=lambda t: [\n int(t) if t.isdigit() else t\n for t in re.split(r'([0-9]+)', t.relation or '')\n ]\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nserialize the graph g from top to PENMAN notation.", "response": "def encode(g, top=None, cls=PENMANCodec, **kwargs):\n \"\"\"\n Serialize the graph *g* from *top* to PENMAN notation.\n\n Args:\n g: the Graph object\n top: the node identifier for the top of the serialized graph; if\n unset, the original top of *g* is used\n cls: serialization codec class\n kwargs: keyword arguments passed to the constructor of *cls*\n Returns:\n the PENMAN-serialized string of the Graph *g*\n Example:\n\n >>> encode(Graph([('h', 'instance', 'hi')]))\n (h / hi)\n \"\"\"\n codec = cls(**kwargs)\n return codec.encode(g, top=top)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load(source, triples=False, cls=PENMANCodec, **kwargs):\n decode = cls(**kwargs).iterdecode\n if hasattr(source, 'read'):\n return list(decode(source.read()))\n else:\n with open(source) as fh:\n return list(decode(fh.read()))", "response": "Deserialize a list of PENMAN - encoded graphs from a file - like object."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dump(graphs, file, triples=False, cls=PENMANCodec, **kwargs):\n text = dumps(graphs, triples=triples, cls=cls, **kwargs)\n\n if hasattr(file, 'write'):\n print(text, file=file)\n else:\n with open(file, 'w') as fh:\n print(text, file=fh)", "response": "Serialize each graph in graphs to PENMAN and write to file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nserializing graphs to the PENMAN format.", "response": "def dumps(graphs, triples=False, cls=PENMANCodec, **kwargs):\n \"\"\"\n Serialize each graph in *graphs* to the PENMAN format.\n\n Args:\n graphs: an iterable of Graph objects\n triples: if True, write graphs as triples instead of as PENMAN\n Returns:\n the string of serialized graphs\n \"\"\"\n codec = cls(**kwargs)\n strings = [codec.encode(g, triples=triples) for g in graphs]\n return '\\n\\n'.join(strings)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nserializes the graph g from top to PENMAN notation.", "response": "def encode(self, g, top=None, triples=False):\n \"\"\"\n Serialize the graph *g* from *top* to PENMAN notation.\n\n Args:\n g: the Graph object\n top: the node identifier for the top of the serialized\n graph; if unset, the original top of *g* is used\n triples: if True, serialize as a conjunction of logical triples\n Returns:\n the PENMAN-serialized string of the Graph *g*\n Example:\n\n >>> codec = PENMANCodec()\n >>> codec.encode(Graph([('h', 'instance', 'hi')]))\n (h / hi)\n >>> codec.encode(Graph([('h', 'instance', 'hi')]),\n ... triples=True)\n instance(h, hi)\n \"\"\"\n if len(g.triples()) == 0:\n raise EncodeError('Cannot encode empty graph.')\n if triples:\n return self._encode_triple_conjunction(g, top=top)\n else:\n return self._encode_penman(g, top=top)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef handle_triple(self, lhs, relation, rhs):\n relation = relation.replace(':', '', 1) # remove leading :\n\n if self.is_relation_inverted(relation): # deinvert\n source, target, inverted = rhs, lhs, True\n relation = self.invert_relation(relation)\n else:\n source, target, inverted = lhs, rhs, False\n\n source = _default_cast(source)\n target = _default_cast(target)\n\n if relation == '': # set empty relations to None\n relation = None\n\n return Triple(source, relation, target, inverted)", "response": "Process a triple from lhs to rhs and return a new triple."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a Graph object from a list of triples.", "response": "def triples_to_graph(self, triples, top=None):\n \"\"\"\n Create a Graph from *triples* considering codec configuration.\n\n The Graph class does not know about information in the codec,\n so if Graph instantiation depends on special `TYPE_REL` or\n `TOP_VAR` values, use this function instead of instantiating\n a Graph object directly. This is also where edge\n normalization (de-inversion) and value type conversion occur\n (via handle_triple()).\n\n Args:\n triples: an iterable of (lhs, relation, rhs) triples\n top: node identifier of the top node\n Returns:\n a Graph object\n \"\"\"\n inferred_top = triples[0][0] if triples else None\n ts = []\n for triple in triples:\n if triple[0] == self.TOP_VAR and triple[1] == self.TOP_REL:\n inferred_top = triple[2]\n else:\n ts.append(self.handle_triple(*triple))\n top = self.handle_triple(self.TOP_VAR, self.TOP_REL, top).target\n return Graph(ts, top=top or inferred_top)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if the relation is inverted.", "response": "def is_relation_inverted(self, relation):\n \"\"\"\n Return True if *relation* is inverted.\n \"\"\"\n return (\n relation in self._deinversions or\n (relation.endswith('-of') and relation not in self._inversions)\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ninvert or deinvert relation.", "response": "def invert_relation(self, relation):\n \"\"\"\n Invert or deinvert *relation*.\n \"\"\"\n if self.is_relation_inverted(relation):\n rel = self._deinversions.get(relation, relation[:-3])\n else:\n rel = self._inversions.get(relation, relation + '-of')\n if rel is None:\n raise PenmanError(\n 'Cannot (de)invert {}; not allowed'.format(relation)\n )\n return rel"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef triples(self, source=None, relation=None, target=None):\n triplematch = lambda t: (\n (source is None or source == t.source) and\n (relation is None or relation == t.relation) and\n (target is None or target == t.target)\n )\n return list(filter(triplematch, self._triples))", "response": "Return a list of triples filtered by their source relation or target."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef edges(self, source=None, relation=None, target=None):\n edgematch = lambda e: (\n (source is None or source == e.source) and\n (relation is None or relation == e.relation) and\n (target is None or target == e.target)\n )\n variables = self.variables()\n edges = [t for t in self._triples if t.target in variables]\n return list(filter(edgematch, edges))", "response": "Return a list of edges that are in the terminal graph."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of all attributes that are in the current context.", "response": "def attributes(self, source=None, relation=None, target=None):\n \"\"\"\n Return attributes filtered by their *source*, *relation*, or *target*.\n\n Attributes don't include triples where the target is a nonterminal.\n \"\"\"\n attrmatch = lambda a: (\n (source is None or source == a.source) and\n (relation is None or relation == a.relation) and\n (target is None or target == a.target)\n )\n variables = self.variables()\n attrs = [t for t in self.triples() if t.target not in variables]\n return list(filter(attrmatch, attrs))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a mapping of variables to their re - entrancy count.", "response": "def reentrancies(self):\n \"\"\"\n Return a mapping of variables to their re-entrancy count.\n\n A re-entrancy is when more than one edge selects a node as its\n target. These graphs are rooted, so the top node always has an\n implicit entrancy. Only nodes with re-entrancies are reported,\n and the count is only for the entrant edges beyond the first.\n Also note that these counts are for the interpreted graph, not\n for the linearized form, so inverted edges are always\n re-entrant.\n \"\"\"\n entrancies = defaultdict(int)\n entrancies[self.top] += 1 # implicit entrancy to top\n for t in self.edges():\n entrancies[t.target] += 1\n return dict((v, cnt - 1) for v, cnt in entrancies.items() if cnt >= 2)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check_1d(inp):\n if isinstance(inp, list):\n return check_1d(np.array(inp))\n if isinstance(inp, np.ndarray):\n if inp.ndim == 1: # input is a vector\n return inp", "response": "Checks input to be a vector. Converts lists to np. ndarray."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck input to be a matrix. Converts lists of lists to np. ndarray.", "response": "def check_2d(inp):\n \"\"\"\n Check input to be a matrix. Converts lists of lists to np.ndarray.\n\n Also allows the input to be a scipy sparse matrix.\n \n Parameters\n ----------\n inp : obj\n Input matrix\n\n Returns\n -------\n numpy.ndarray, scipy.sparse or None\n Input matrix or None\n\n Examples\n --------\n >>> check_2d([[0, 1], [2, 3]])\n [[0, 1], [2, 3]]\n\n >>> check_2d('test')\n None\n\n \"\"\"\n if isinstance(inp, list):\n return check_2d(np.array(inp))\n if isinstance(inp, (np.ndarray, np.matrixlib.defmatrix.matrix)):\n if inp.ndim == 2: # input is a dense matrix\n return inp\n if sps.issparse(inp):\n if inp.ndim == 2: # input is a sparse matrix\n return inp"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef graph_to_laplacian(G, normalized=True):\n try:\n import networkx as nx\n if isinstance(G, nx.Graph):\n if normalized:\n return nx.normalized_laplacian_matrix(G)\n else:\n return nx.laplacian_matrix(G)\n except ImportError:\n pass\n try:\n import graph_tool.all as gt\n if isinstance(G, gt.Graph):\n if normalized:\n return gt.laplacian_type(G, normalized=True)\n else:\n return gt.laplacian(G)\n except ImportError:\n pass\n try:\n import igraph as ig\n if isinstance(G, ig.Graph):\n if normalized:\n return np.array(G.laplacian(normalized=True))\n else:\n return np.array(G.laplacian())\n except ImportError:\n pass", "response": "Converts a graph from popular Python packages to Laplacian representation."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a sparse or dence adjacency matrix to Laplacian.", "response": "def mat_to_laplacian(mat, normalized):\n \"\"\"\n Converts a sparse or dence adjacency matrix to Laplacian.\n \n Parameters\n ----------\n mat : obj\n Input adjacency matrix. If it is a Laplacian matrix already, return it.\n normalized : bool\n Whether to use normalized Laplacian.\n Normalized and unnormalized Laplacians capture different properties of graphs, e.g. normalized Laplacian spectrum can determine whether a graph is bipartite, but not the number of its edges. We recommend using normalized Laplacian.\n\n Returns\n -------\n obj\n Laplacian of the input adjacency matrix\n\n Examples\n --------\n >>> mat_to_laplacian(numpy.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]), False)\n [[ 2, -1, -1], [-1, 2, -1], [-1, -1, 2]]\n\n \"\"\"\n if sps.issparse(mat):\n if np.all(mat.diagonal()>=0): # Check diagonal\n if np.all((mat-sps.diags(mat.diagonal())).data <= 0): # Check off-diagonal elements\n return mat\n else:\n if np.all(np.diag(mat)>=0): # Check diagonal\n if np.all(mat - np.diag(mat) <= 0): # Check off-diagonal elements\n return mat\n deg = np.squeeze(np.asarray(mat.sum(axis=1)))\n if sps.issparse(mat):\n L = sps.diags(deg) - mat\n else:\n L = np.diag(deg) - mat\n if not normalized:\n return L\n with np.errstate(divide='ignore'):\n sqrt_deg = 1.0 / np.sqrt(deg)\n sqrt_deg[sqrt_deg==np.inf] = 0\n if sps.issparse(mat):\n sqrt_deg_mat = sps.diags(sqrt_deg)\n else:\n sqrt_deg_mat = np.diag(sqrt_deg)\n return sqrt_deg_mat.dot(L).dot(sqrt_deg_mat)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating Laplacian spectrum using lower and upper parts of the eigenspectrum.", "response": "def updown_linear_approx(eigvals_lower, eigvals_upper, nv):\n \"\"\"\n Approximates Laplacian spectrum using upper and lower parts of the eigenspectrum.\n \n Parameters\n ----------\n eigvals_lower : numpy.ndarray\n Lower part of the spectrum, sorted\n eigvals_upper : numpy.ndarray\n Upper part of the spectrum, sorted\n nv : int\n Total number of nodes (eigenvalues) in the graph.\n\n Returns\n -------\n numpy.ndarray\n Vector of approximated eigenvalues\n\n Examples\n --------\n >>> updown_linear_approx([1, 2, 3], [7, 8, 9], 9)\n array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n \"\"\"\n nal = len(eigvals_lower)\n nau = len(eigvals_upper)\n if nv < nal + nau:\n raise ValueError('Number of supplied eigenvalues ({0} lower and {1} upper) is higher than number of nodes ({2})!'.format(nal, nau, nv))\n ret = np.zeros(nv)\n ret[:nal] = eigvals_lower\n ret[-nau:] = eigvals_upper\n ret[nal-1:-nau+1] = np.linspace(eigvals_lower[-1], eigvals_upper[0], nv-nal-nau+2)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef eigenvalues_auto(mat, n_eivals='auto'):\n do_full = True\n n_lower = 150\n n_upper = 150\n nv = mat.shape[0]\n if n_eivals == 'auto':\n if mat.shape[0] > 1024:\n do_full = False\n if n_eivals == 'full':\n do_full = True\n if isinstance(n_eivals, int):\n n_lower = n_upper = n_eivals\n do_full = False\n if isinstance(n_eivals, tuple):\n n_lower, n_upper = n_eivals\n do_full = False\n if do_full and sps.issparse(mat):\n mat = mat.todense()\n if sps.issparse(mat):\n if n_lower == n_upper:\n tr_eivals = spsl.eigsh(mat, 2*n_lower, which='BE', return_eigenvectors=False)\n return updown_linear_approx(tr_eivals[:n_upper], tr_eivals[n_upper:], nv)\n else:\n lo_eivals = spsl.eigsh(mat, n_lower, which='SM', return_eigenvectors=False)[::-1]\n up_eivals = spsl.eigsh(mat, n_upper, which='LM', return_eigenvectors=False)\n return updown_linear_approx(lo_eivals, up_eivals, nv)\n else:\n if do_full:\n return spl.eigvalsh(mat)\n else:\n lo_eivals = spl.eigvalsh(mat, eigvals=(0, n_lower-1))\n up_eivals = spl.eigvalsh(mat, eigvals=(nv-n_upper-1, nv-1))\n return updown_linear_approx(lo_eivals, up_eivals, nv)", "response": "A function that computes the spectrum of a given Laplacian matrix and returns the vector of approximated eigenvalues."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef netlsd(inp, timescales=np.logspace(-2, 2, 250), kernel='heat', eigenvalues='auto', normalization='empty', normalized_laplacian=True):\n if kernel not in {'heat', 'wave'}:\n raise AttributeError('Unirecognized kernel type: expected one of [\\'heat\\', \\'wave\\'], got {0}'.format(kernel))\n if not isinstance(normalized_laplacian, bool):\n raise AttributeError('Unknown Laplacian type: expected bool, got {0}'.format(normalized_laplacian))\n if not isinstance(eigenvalues, (int, tuple, str)):\n raise AttributeError('Unirecognized requested eigenvalue number: expected type of [\\'str\\', \\'tuple\\', or \\'int\\'], got {0}'.format(type(eigenvalues)))\n if not isinstance(timescales, np.ndarray):\n raise AttributeError('Unirecognized timescales data type: expected np.ndarray, got {0}'.format(type(timescales)))\n if timescales.ndim != 1:\n raise AttributeError('Unirecognized timescales dimensionality: expected a vector, got {0}-d array'.format(timescales.ndim))\n if normalization not in {'complete', 'empty', 'none', True, False, None}:\n if not isinstance(normalization, np.ndarray):\n raise AttributeError('Unirecognized normalization type: expected one of [\\'complete\\', \\'empty\\', None or np.ndarray], got {0}'.format(normalization))\n if normalization.ndim != 1:\n raise AttributeError('Unirecognized normalization dimensionality: expected a vector, got {0}-d array'.format(normalization.ndim))\n if timescales.shape[0] != normalization.shape[0]:\n raise AttributeError('Unirecognized normalization dimensionality: expected {0}-length vector, got length {1}'.format(timescales.shape[0], normalization.shape[0]))\n\n eivals = check_1d(inp)\n if eivals is None:\n mat = check_2d(inp)\n if mat is None:\n mat = graph_to_laplacian(inp, normalized_laplacian)\n if mat is None:\n raise ValueError('Unirecognized input type: expected one of [\\'np.ndarray\\', \\'scipy.sparse\\', \\'networkx.Graph\\',\\' graph_tool.Graph,\\' or \\'igraph.Graph\\'], got {0}'.format(type(inp)))\n else:\n mat = mat_to_laplacian(inp, normalized_laplacian)\n eivals = eigenvalues_auto(mat, eigenvalues)\n if kernel == 'heat':\n return _hkt(eivals, timescales, normalization, normalized_laplacian)\n else:\n return _wkt(eivals, timescales, normalization, normalized_laplacian)", "response": "This function computes the NetLSD signature from some input time series and kernels."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef heat(inp, timescales=np.logspace(-2, 2, 250), eigenvalues='auto', normalization='empty', normalized_laplacian=True):\n return netlsd(inp, timescales, 'heat', eigenvalues, normalization, normalized_laplacian)", "response": "Compute the heat kernel trace from some input time series timescales and normalization."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef wave(inp, timescales=np.linspace(0, 2*np.pi, 250), eigenvalues='auto', normalization='empty', normalized_laplacian=True):\n return netlsd(inp, timescales, 'wave', eigenvalues, normalization, normalized_laplacian)", "response": "This function computes the wave kernel trace from some input time series timescales and normalization."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _hkt(eivals, timescales, normalization, normalized_laplacian):\n nv = eivals.shape[0]\n hkt = np.zeros(timescales.shape)\n for idx, t in enumerate(timescales):\n hkt[idx] = np.sum(np.exp(-t * eivals))\n if isinstance(normalization, np.ndarray):\n return hkt / normalization\n if normalization == 'empty' or normalization == True:\n return hkt / nv\n if normalization == 'complete':\n if normalized_laplacian:\n return hkt / (1 + (nv - 1) * np.exp(-timescales))\n else:\n return hkt / (1 + nv * np.exp(-nv * timescales))\n return hkt", "response": "Compute the heat kernel trace from given eigenvalues timescales and normalization."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncomputing the wave kernel trace from given eigenvalues timescales and normalization.", "response": "def _wkt(eivals, timescales, normalization, normalized_laplacian):\n \"\"\"\n Computes wave kernel trace from given eigenvalues, timescales, and normalization.\n\n For precise definition, please refer to \"NetLSD: Hearing the Shape of a Graph\" by A. Tsitsulin, D. Mottin, P. Karras, A. Bronstein, E. M\u00fcller. Published at KDD'18.\n \n Parameters\n ----------\n eivals : numpy.ndarray\n Eigenvalue vector\n timescales : numpy.ndarray\n Vector of discrete timesteps for the kernel computation\n normalization : str or numpy.ndarray\n Either 'empty', 'complete' or None.\n If None or any ther value, return unnormalized wave kernel trace.\n For the details how 'empty' and 'complete' are computed, please refer to the paper.\n If np.ndarray, they are treated as exact normalization constants\n normalized_laplacian: bool\n Defines whether the eigenvalues came from the normalized Laplacian. It only affects 'complete' normalization.\n\n Returns\n -------\n numpy.ndarray\n Wave kernel trace signature\n\n \"\"\"\n nv = eivals.shape[0]\n wkt = np.zeros(timescales.shape)\n for idx, t in enumerate(timescales):\n wkt[idx] = np.sum(np.exp(-1j * t * eivals))\n if isinstance(normalization, np.ndarray):\n return hkt / normalization\n if normalization == 'empty' or normalization == True:\n return wkt / nv\n if normalization == 'complete':\n if normalized_laplacian:\n return wkt / (1 + (nv - 1) * np.cos(timescales))\n else:\n return wkt / (1 + (nv - 1) * np.cos(nv * timescales))\n return wkt"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves all the elements from the list.", "response": "def clear(self):\n \"\"\"Remove all the elements from the list.\"\"\"\n self._len = 0\n del self._maxes[:]\n del self._lists[:]\n del self._keys[:]\n del self._index[:]"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove the first occurrence of val from the internal list of the internal list of the internal list of the internal list of the internal list of the internal list.", "response": "def discard(self, val):\n \"\"\"\n Remove the first occurrence of *val*.\n\n If *val* is not a member, does nothing.\n \"\"\"\n _maxes = self._maxes\n\n if not _maxes:\n return\n\n key = self._key(val)\n pos = bisect_left(_maxes, key)\n\n if pos == len(_maxes):\n return\n\n _keys = self._keys\n _lists = self._lists\n idx = bisect_left(_keys[pos], key)\n\n len_keys = len(_keys)\n len_sublist = len(_keys[pos])\n\n while True:\n if _keys[pos][idx] != key:\n return\n if _lists[pos][idx] == val:\n self._delete(pos, idx)\n return\n idx += 1\n if idx == len_sublist:\n pos += 1\n if pos == len_keys:\n return\n len_sublist = len(_keys[pos])\n idx = 0"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove first occurrence of val from the internal list. Raises ValueError if val is not present.", "response": "def remove(self, val):\n \"\"\"\n Remove first occurrence of *val*.\n\n Raises ValueError if *val* is not present.\n \"\"\"\n _maxes = self._maxes\n\n if not _maxes:\n raise ValueError('{0} not in list'.format(repr(val)))\n\n key = self._key(val)\n pos = bisect_left(_maxes, key)\n\n if pos == len(_maxes):\n raise ValueError('{0} not in list'.format(repr(val)))\n\n _keys = self._keys\n _lists = self._lists\n idx = bisect_left(_keys[pos], key)\n\n len_keys = len(_keys)\n len_sublist = len(_keys[pos])\n\n while True:\n if _keys[pos][idx] != key:\n raise ValueError('{0} not in list'.format(repr(val)))\n if _lists[pos][idx] == val:\n self._delete(pos, idx)\n return\n idx += 1\n if idx == len_sublist:\n pos += 1\n if pos == len_keys:\n raise ValueError('{0} not in list'.format(repr(val)))\n len_sublist = len(_keys[pos])\n idx = 0"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _delete(self, pos, idx):\n _maxes, _lists, _keys, _index = self._maxes, self._lists, self._keys, self._index\n\n keys_pos = _keys[pos]\n lists_pos = _lists[pos]\n\n del keys_pos[idx]\n del lists_pos[idx]\n self._len -= 1\n\n len_keys_pos = len(keys_pos)\n\n if len_keys_pos > self._half:\n\n _maxes[pos] = keys_pos[-1]\n\n if len(_index) > 0:\n child = self._offset + pos\n while child > 0:\n _index[child] -= 1\n child = (child - 1) >> 1\n _index[0] -= 1\n\n elif len(_keys) > 1:\n\n if not pos:\n pos += 1\n\n prev = pos - 1\n _keys[prev].extend(_keys[pos])\n _lists[prev].extend(_lists[pos])\n _maxes[prev] = _keys[prev][-1]\n\n del _keys[pos]\n del _lists[pos]\n del _maxes[pos]\n del _index[:]\n\n self._expand(prev)\n\n elif len_keys_pos:\n\n _maxes[pos] = keys_pos[-1]\n\n else:\n\n del _keys[pos]\n del _lists[pos]\n del _maxes[pos]\n del _index[:]", "response": "Delete the item at the given position."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting an index pair to a single index that corresponds to the position of the value in the sorted list at the given position.", "response": "def _loc(self, pos, idx):\n \"\"\"Convert an index pair (alpha, beta) into a single index that corresponds to\n the position of the value in the sorted list.\n\n Most queries require the index be built. Details of the index are\n described in self._build_index.\n\n Indexing requires traversing the tree from a leaf node to the root. The\n parent of each node is easily computable at (pos - 1) // 2.\n\n Left-child nodes are always at odd indices and right-child nodes are\n always at even indices.\n\n When traversing up from a right-child node, increment the total by the\n left-child node.\n\n The final index is the sum from traversal and the index in the sublist.\n\n For example, using the index from self._build_index:\n\n _index = 14 5 9 3 2 4 5\n _offset = 3\n\n Tree:\n\n 14\n 5 9\n 3 2 4 5\n\n Converting index pair (2, 3) into a single index involves iterating like\n so:\n\n 1. Starting at the leaf node: offset + alpha = 3 + 2 = 5. We identify\n the node as a left-child node. At such nodes, we simply traverse to\n the parent.\n\n 2. At node 9, position 2, we recognize the node as a right-child node\n and accumulate the left-child in our total. Total is now 5 and we\n traverse to the parent at position 0.\n\n 3. Iteration ends at the root.\n\n Computing the index is the sum of the total and beta: 5 + 3 = 8.\n \"\"\"\n if not pos:\n return idx\n\n _index = self._index\n\n if not len(_index):\n self._build_index()\n\n total = 0\n\n # Increment pos to point in the index to len(self._lists[pos]).\n\n pos += self._offset\n\n # Iterate until reaching the root of the index tree at pos = 0.\n\n while pos:\n\n # Right-child nodes are at odd indices. At such indices\n # account the total below the left child node.\n\n if not (pos & 1):\n total += _index[pos - 1]\n\n # Advance pos to the parent node.\n\n pos = (pos - 1) >> 1\n\n return total + idx"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef islice(self, start=None, stop=None, reverse=False):\n _len = self._len\n\n if not _len:\n return iter(())\n\n start, stop, step = self._slice(slice(start, stop))\n\n if start >= stop:\n return iter(())\n\n _pos = self._pos\n\n min_pos, min_idx = _pos(start)\n\n if stop == _len:\n max_pos = len(self._lists) - 1\n max_idx = len(self._lists[-1])\n else:\n max_pos, max_idx = _pos(stop)\n\n return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)", "response": "Returns an iterator that slices self from start to stop index inclusive and exclusive respectively."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef irange(self, minimum=None, maximum=None, inclusive=(True, True),\n reverse=False):\n \"\"\"\n Create an iterator of values between `minimum` and `maximum`.\n\n `inclusive` is a pair of booleans that indicates whether the minimum\n and maximum ought to be included in the range, respectively. The\n default is (True, True) such that the range is inclusive of both\n minimum and maximum.\n\n Both `minimum` and `maximum` default to `None` which is automatically\n inclusive of the start and end of the list, respectively.\n\n When `reverse` is `True` the values are yielded from the iterator in\n reverse order; `reverse` defaults to `False`.\n \"\"\"\n minimum = self._key(minimum) if minimum is not None else None\n maximum = self._key(maximum) if maximum is not None else None\n return self.irange_key(\n min_key=minimum, max_key=maximum,\n inclusive=inclusive, reverse=reverse,\n )", "response": "Create an iterator of values between minimum and maximum."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef copy(self):\n return self.__class__(self, key=self._key, load=self._load)", "response": "Return a shallow copy of the sorted list."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef append(self, val):\n _maxes, _lists, _keys = self._maxes, self._lists, self._keys\n\n key = self._key(val)\n\n if not _maxes:\n _maxes.append(key)\n _keys.append([key])\n _lists.append([val])\n self._len = 1\n return\n\n pos = len(_keys) - 1\n\n if key < _keys[pos][-1]:\n msg = '{0} not in sort order at index {1}'.format(repr(val), self._len)\n raise ValueError(msg)\n\n _maxes[pos] = key\n _keys[pos].append(key)\n _lists[pos].append(val)\n self._len += 1\n self._expand(pos)", "response": "Append the element val to the list. Raises a ValueError if the element val would violate the sort order."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nextend the list by appending all elements from the given list. Raises a ValueError if the sort order is violated.", "response": "def extend(self, values):\n \"\"\"\n Extend the list by appending all elements from the *values*. Raises a\n ValueError if the sort order would be violated.\n \"\"\"\n _maxes, _keys, _lists, _load = self._maxes, self._keys, self._lists, self._load\n\n if not isinstance(values, list):\n values = list(values)\n\n keys = list(map(self._key, values))\n\n if any(keys[pos - 1] > keys[pos]\n for pos in range(1, len(keys))):\n raise ValueError('given sequence not in sort order')\n\n offset = 0\n\n if _maxes:\n if keys[0] < _keys[-1][-1]:\n msg = '{0} not in sort order at index {1}'.format(repr(values[0]), self._len)\n raise ValueError(msg)\n\n if len(_keys[-1]) < self._half:\n _lists[-1].extend(values[:_load])\n _keys[-1].extend(keys[:_load])\n _maxes[-1] = _keys[-1][-1]\n offset = _load\n\n len_keys = len(_keys)\n\n for idx in range(offset, len(keys), _load):\n _lists.append(values[idx:(idx + _load)])\n _keys.append(keys[idx:(idx + _load)])\n _maxes.append(_keys[-1][-1])\n\n _index = self._index\n\n if len_keys == len(_keys):\n len_index = len(_index)\n if len_index > 0:\n len_values = len(values)\n child = len_index - 1\n while child:\n _index[child] += len_values\n child = (child - 1) >> 1\n _index[0] += len_values\n else:\n del _index[:]\n\n self._len += len(values)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove and returns the item at the specified index.", "response": "def pop(self, idx=-1):\n \"\"\"\n Remove and return item at *idx* (default last). Raises IndexError if\n list is empty or index is out of range. Negative indices are supported,\n as for slice indices.\n \"\"\"\n if (idx < 0 and -idx > self._len) or (idx >= self._len):\n raise IndexError('pop index out of range')\n\n pos, idx = self._pos(idx)\n val = self._lists[pos][idx]\n self._delete(pos, idx)\n\n return val"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfunction decorator for methods not implemented in Python 2. 6.", "response": "def not26(func):\n \"\"\"Function decorator for methods not implemented in Python 2.6.\"\"\"\n\n @wraps(func)\n def errfunc(*args, **kwargs):\n raise NotImplementedError\n\n if hexversion < 0x02070000:\n return errfunc\n else:\n return func"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef copy(self):\n return self.__class__(self._key, self._load, self._iteritems())", "response": "Return a shallow copy of the sorted dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves the entry with the given key from the dictionary. If the key is not in the dictionary a KeyError is raised. If the default is given a KeyError is raised.", "response": "def pop(self, key, default=_NotGiven):\n \"\"\"\n If *key* is in the dictionary, remove it and return its value,\n else return *default*. If *default* is not given and *key* is not in\n the dictionary, a KeyError is raised.\n \"\"\"\n if key in self:\n self._list_remove(key)\n return self._pop(key)\n else:\n if default is _NotGiven:\n raise KeyError(key)\n else:\n return default"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the value of the key to the given default value. If the key is not in the dictionary insert it with a value of default. If the key is not in the dictionary insert it with a value of None.", "response": "def setdefault(self, key, default=None):\n \"\"\"\n If *key* is in the dictionary, return its value. If not, insert *key*\n with a value of *default* and return *default*. *default* defaults to\n ``None``.\n \"\"\"\n if key in self:\n return self[key]\n else:\n self._setitem(key, default)\n self._list_add(key)\n return default"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the index of the key value in the keyview.", "response": "def index(self, value, start=None, stop=None):\n \"\"\"\n Return the smallest *k* such that `keysview[k] == value` and `start <= k\n < end`. Raises `KeyError` if *value* is not present. *stop* defaults\n to the end of the set. *start* defaults to the beginning. Negative\n indexes are supported, as for slice indices.\n \"\"\"\n return self._list.index(value, start, stop)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_summary(self):\n if not self.ignore_self:\n res = summary.summarize(muppy.get_objects())\n else:\n # If the user requested the data required to store summaries to be\n # ignored in the summaries, we need to identify all objects which\n # are related to each summary stored.\n # Thus we build a list of all objects used for summary storage as\n # well as a dictionary which tells us how often an object is\n # referenced by the summaries.\n # During this identification process, more objects are referenced,\n # namely int objects identifying referenced objects as well as the\n # correspondind count.\n # For all these objects it will be checked wether they are\n # referenced from outside the monitor's scope. If not, they will be\n # subtracted from the snapshot summary, otherwise they are\n # included (as this indicates that they are relevant to the\n # application).\n\n all_of_them = [] # every single object\n ref_counter = {} # how often it is referenced; (id(o), o) pairs\n def store_info(o):\n all_of_them.append(o)\n if id(o) in ref_counter:\n ref_counter[id(o)] += 1\n else:\n ref_counter[id(o)] = 1\n\n # store infos on every single object related to the summaries\n store_info(self.summaries)\n for k, v in self.summaries.items():\n store_info(k)\n summary._traverse(v, store_info)\n\n # do the summary\n res = summary.summarize(muppy.get_objects())\n\n # remove ids stored in the ref_counter\n for _id in ref_counter:\n # referenced in frame, ref_counter, ref_counter.keys()\n if len(gc.get_referrers(_id)) == (3):\n summary._subtract(res, _id)\n for o in all_of_them:\n # referenced in frame, summary, all_of_them\n if len(gc.get_referrers(o)) == (ref_counter[id(o)] + 2):\n summary._subtract(res, o)\n\n return res", "response": "Create a summary for the object which is referenced by the object."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the diff between two summaries.", "response": "def diff(self, summary1=None, summary2=None):\n \"\"\"Compute diff between to summaries.\n\n If no summary is provided, the diff from the last to the current\n summary is used. If summary1 is provided the diff from summary1\n to the current summary is used. If summary1 and summary2 are\n provided, the diff between these two is used.\n\n \"\"\"\n res = None\n if summary2 is None:\n self.s1 = self.create_summary()\n if summary1 is None:\n res = summary.get_diff(self.s0, self.s1)\n else:\n res = summary.get_diff(summary1, self.s1)\n self.s0 = self.s1\n else:\n if summary1 is not None:\n res = summary.get_diff(summary1, summary2)\n else:\n raise ValueError(\"You cannot provide summary2 without summary1.\")\n return summary._sweep(res)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef print_diff(self, summary1=None, summary2=None):\n summary.print_(self.diff(summary1=summary1, summary2=summary2))", "response": "Compute diff between to summaries and print it."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_objects(self, ignore=[]):\n def remove_ignore(objects, ignore=[]):\n # remove all objects listed in the ignore list\n res = []\n for o in objects:\n if not compat.object_in_list(o, ignore):\n res.append(o)\n return res\n\n tmp = gc.get_objects()\n ignore.append(inspect.currentframe()) #PYCHOK change ignore\n ignore.append(self) #PYCHOK change ignore\n if hasattr(self, 'o0'): ignore.append(self.o0) #PYCHOK change ignore\n if hasattr(self, 'o1'): ignore.append(self.o1) #PYCHOK change ignore\n ignore.append(ignore) #PYCHOK change ignore\n ignore.append(remove_ignore) #PYCHOK change ignore\n # this implies that referenced objects are also ignored\n tmp = remove_ignore(tmp, ignore)\n res = []\n for o in tmp:\n # gc.get_objects returns only container objects, but we also want\n # the objects referenced by them\n refs = muppy.get_referents(o)\n for ref in refs:\n if not muppy._is_containerobject(ref):\n # we already got the container objects, now we only add\n # non-container objects\n res.append(ref)\n res.extend(tmp)\n res = muppy._remove_duplicates(res)\n if ignore is not None:\n # repeat to filter out objects which may have been referenced\n res = remove_ignore(res, ignore)\n # manual cleanup, see comment above\n del ignore[:]\n return res", "response": "Get all currently existing objects."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_diff(self, ignore=[]):\n # ignore this and the caller frame\n ignore.append(inspect.currentframe()) #PYCHOK change ignore\n self.o1 = self._get_objects(ignore)\n diff = muppy.get_diff(self.o0, self.o1)\n self.o0 = self.o1\n # manual cleanup, see comment above\n del ignore[:] #PYCHOK change ignore\n return diff", "response": "Get the diff to the last time the state of objects was measured."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprints the diff to the last time the state of objects was measured.", "response": "def print_diff(self, ignore=[]):\n \"\"\"Print the diff to the last time the state of objects was measured.\n\n keyword arguments\n ignore -- list of objects to ignore\n \"\"\"\n # ignore this and the caller frame\n ignore.append(inspect.currentframe()) #PYCHOK change ignore\n diff = self.get_diff(ignore)\n print(\"Added objects:\")\n summary.print_(summary.summarize(diff['+']))\n print(\"Removed objects:\")\n summary.print_(summary.summarize(diff['-']))\n # manual cleanup, see comment above\n del ignore[:]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef hamming(seq1, seq2, normalized=False):\n\tL = len(seq1)\n\tif L != len(seq2):\n\t\traise ValueError(\"expected two strings of the same length\")\n\tif L == 0:\n\t\treturn 0.0 if normalized else 0 # equal\n\tdist = sum(c1 != c2 for c1, c2 in zip(seq1, seq2))\n\tif normalized:\n\t\treturn dist / float(L)\n\treturn dist", "response": "Compute the Hamming distance between the two sequences seq1 and seq2."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncomputing the Jaccard distance between the two sequences seq1 and seq2.", "response": "def jaccard(seq1, seq2):\n\t\"\"\"Compute the Jaccard distance between the two sequences `seq1` and `seq2`.\n\tThey should contain hashable items.\n\t\n\tThe return value is a float between 0 and 1, where 0 means equal, and 1 totally different.\n\t\"\"\"\n\tset1, set2 = set(seq1), set(seq2)\n\treturn 1 - len(set1 & set2) / float(len(set1 | set2))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sorensen(seq1, seq2):\n\tset1, set2 = set(seq1), set(seq2)\n\treturn 1 - (2 * len(set1 & set2) / float(len(set1) + len(set2)))", "response": "Compute the Sorensen distance between the two sequences seq1 and seq2."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef lcsubstrings(seq1, seq2, positions=False):\n\tL1, L2 = len(seq1), len(seq2)\n\tms = []\n\tmlen = last = 0\n\tif L1 < L2:\n\t\tseq1, seq2 = seq2, seq1\n\t\tL1, L2 = L2, L1\n\t\n\tcolumn = array('L', range(L2))\n\t\n\tfor i in range(L1):\n\t\tfor j in range(L2):\n\t\t\told = column[j]\n\t\t\tif seq1[i] == seq2[j]:\n\t\t\t\tif i == 0 or j == 0:\n\t\t\t\t\tcolumn[j] = 1\n\t\t\t\telse:\n\t\t\t\t\tcolumn[j] = last + 1\n\t\t\t\tif column[j] > mlen:\n\t\t\t\t\tmlen = column[j]\n\t\t\t\t\tms = [(i, j)]\n\t\t\t\telif column[j] == mlen:\n\t\t\t\t\tms.append((i, j))\n\t\t\telse:\n\t\t\t\tcolumn[j] = 0\n\t\t\tlast = old\n\t\n\tif positions:\n\t\treturn (mlen, tuple((i - mlen + 1, j - mlen + 1) for i, j in ms if ms))\n\treturn set(seq1[i - mlen + 1:i + 1] for i, _ in ms if ms)", "response": "Find the longest common substring in the sequences seq1 and seq2."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef background_color(self, node, depth):\n if self.color_mapping is None:\n self.color_mapping = {}\n color = self.color_mapping.get(node.key)\n if color is None:\n depth = len(self.color_mapping)\n red = (depth * 10) % 255\n green = 200 - ((depth * 5) % 200)\n blue = (depth * 25) % 200\n self.color_mapping[node.key] = color = wx.Colour(red, green, blue)\n return color", "response": "Create a color for each node"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef SetPercentage(self, percent, total):\n self.percentageView = percent\n self.total = total", "response": "Set whether to display percentage values and total for doing so."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef runprofilerandshow(funcname, profilepath, argv='', *args, **kwargs):\n '''\n Run a functions profiler and show it in a GUI visualisation using RunSnakeRun\n Note: can also use calibration for more exact results\n '''\n functionprofiler.runprofile(funcname+'(\\''+argv+'\\')', profilepath, *args, **kwargs)\n print 'Showing profile (windows should open in the background)'; sys.stdout.flush();\n functionprofiler.browseprofilegui(profilepath)", "response": "Run a functions profiler and show it in a GUI visualisation using RunSnakeRun\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmake a call graph Note: be sure to install GraphViz prior to printing the dot graph!", "response": "def callgraph(func):\n ''' Makes a call graph\n Note: be sure to install GraphViz prior to printing the dot graph!\n '''\n import pycallgraph\n @functools.wraps(func)\n def wrapper(*args, **kwargs):\n pycallgraph.start_trace()\n func(*args, **kwargs)\n pycallgraph.save_dot('callgraph.log')\n pycallgraph.make_dot_graph('callgraph.png')\n #pycallgraph.make_dot_graph('callgraph.jpg', format='jpg', tool='neato')\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a long integer to a byte string.", "response": "def _long2bytes(n, blocksize=0):\r\n \"\"\"Convert a long integer to a byte string.\r\n\r\n If optional blocksize is given and greater than zero, pad the front\r\n of the byte string with binary zeros so that the length is a multiple\r\n of blocksize.\r\n \"\"\"\r\n\r\n # After much testing, this algorithm was deemed to be the fastest.\r\n s = ''\r\n pack = struct.pack\r\n while n > 0:\r\n ### CHANGED FROM '>I' TO '> 32\r\n\r\n # Strip off leading zeros.\r\n for i in range(len(s)):\r\n if s[i] <> '\\000':\r\n break\r\n else:\r\n # Only happens when n == 0.\r\n s = '\\000'\r\n i = 0\r\n\r\n s = s[i:]\r\n\r\n # Add back some pad bytes. This could be done more efficiently\r\n # w.r.t. the de-padding being done above, but sigh...\r\n if blocksize > 0 and len(s) % blocksize:\r\n s = (blocksize - len(s) % blocksize) * '\\000' + s\r\n\r\n return s"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwraps for calling functions F G H and I from Appl. Crypto.", "response": "def XX(func, a, b, c, d, x, s, ac):\r\n \"\"\"Wrapper for call distribution to functions F, G, H and I.\r\n\r\n This replaces functions FF, GG, HH and II from \"Appl. Crypto.\r\n Rotation is separate from addition to prevent recomputation\r\n (now summed-up in one function).\r\n \"\"\"\r\n\r\n res = 0L\r\n res = res + a + func(b, c, d)\r\n res = res + x \r\n res = res + ac\r\n res = res & 0xffffffffL\r\n res = _rotateLeft(res, s)\r\n res = res & 0xffffffffL\r\n res = res + b\r\n\r\n return res & 0xffffffffL"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init(self):\r\n \"Initialize the message-digest and set all fields to zero.\"\r\n\r\n self.length = 0L\r\n self.input = []\r\n\r\n # Load magic initialization constants.\r\n self.A = 0x67452301L\r\n self.B = 0xefcdab89L\r\n self.C = 0x98badcfeL\r\n self.D = 0x10325476L", "response": "Initialize the message - digest and set all fields to zero."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntransforming the input array into a new array of the same size.", "response": "def _transform(self, inp):\r\n \"\"\"Basic MD5 step transforming the digest based on the input.\r\n\r\n Note that if the Mysterious Constants are arranged backwards\r\n in little-endian order and decrypted with the DES they produce\r\n OCCULT MESSAGES!\r\n \"\"\"\r\n\r\n a, b, c, d = A, B, C, D = self.A, self.B, self.C, self.D\r\n\r\n # Round 1.\r\n\r\n S11, S12, S13, S14 = 7, 12, 17, 22\r\n\r\n a = XX(F, a, b, c, d, inp[ 0], S11, 0xD76AA478L) # 1 \r\n d = XX(F, d, a, b, c, inp[ 1], S12, 0xE8C7B756L) # 2 \r\n c = XX(F, c, d, a, b, inp[ 2], S13, 0x242070DBL) # 3 \r\n b = XX(F, b, c, d, a, inp[ 3], S14, 0xC1BDCEEEL) # 4 \r\n a = XX(F, a, b, c, d, inp[ 4], S11, 0xF57C0FAFL) # 5 \r\n d = XX(F, d, a, b, c, inp[ 5], S12, 0x4787C62AL) # 6 \r\n c = XX(F, c, d, a, b, inp[ 6], S13, 0xA8304613L) # 7 \r\n b = XX(F, b, c, d, a, inp[ 7], S14, 0xFD469501L) # 8 \r\n a = XX(F, a, b, c, d, inp[ 8], S11, 0x698098D8L) # 9 \r\n d = XX(F, d, a, b, c, inp[ 9], S12, 0x8B44F7AFL) # 10 \r\n c = XX(F, c, d, a, b, inp[10], S13, 0xFFFF5BB1L) # 11 \r\n b = XX(F, b, c, d, a, inp[11], S14, 0x895CD7BEL) # 12 \r\n a = XX(F, a, b, c, d, inp[12], S11, 0x6B901122L) # 13 \r\n d = XX(F, d, a, b, c, inp[13], S12, 0xFD987193L) # 14 \r\n c = XX(F, c, d, a, b, inp[14], S13, 0xA679438EL) # 15 \r\n b = XX(F, b, c, d, a, inp[15], S14, 0x49B40821L) # 16 \r\n\r\n # Round 2.\r\n\r\n S21, S22, S23, S24 = 5, 9, 14, 20\r\n\r\n a = XX(G, a, b, c, d, inp[ 1], S21, 0xF61E2562L) # 17 \r\n d = XX(G, d, a, b, c, inp[ 6], S22, 0xC040B340L) # 18 \r\n c = XX(G, c, d, a, b, inp[11], S23, 0x265E5A51L) # 19 \r\n b = XX(G, b, c, d, a, inp[ 0], S24, 0xE9B6C7AAL) # 20 \r\n a = XX(G, a, b, c, d, inp[ 5], S21, 0xD62F105DL) # 21 \r\n d = XX(G, d, a, b, c, inp[10], S22, 0x02441453L) # 22 \r\n c = XX(G, c, d, a, b, inp[15], S23, 0xD8A1E681L) # 23 \r\n b = XX(G, b, c, d, a, inp[ 4], S24, 0xE7D3FBC8L) # 24 \r\n a = XX(G, a, b, c, d, inp[ 9], S21, 0x21E1CDE6L) # 25 \r\n d = XX(G, d, a, b, c, inp[14], S22, 0xC33707D6L) # 26 \r\n c = XX(G, c, d, a, b, inp[ 3], S23, 0xF4D50D87L) # 27 \r\n b = XX(G, b, c, d, a, inp[ 8], S24, 0x455A14EDL) # 28 \r\n a = XX(G, a, b, c, d, inp[13], S21, 0xA9E3E905L) # 29 \r\n d = XX(G, d, a, b, c, inp[ 2], S22, 0xFCEFA3F8L) # 30 \r\n c = XX(G, c, d, a, b, inp[ 7], S23, 0x676F02D9L) # 31 \r\n b = XX(G, b, c, d, a, inp[12], S24, 0x8D2A4C8AL) # 32 \r\n\r\n # Round 3.\r\n\r\n S31, S32, S33, S34 = 4, 11, 16, 23\r\n\r\n a = XX(H, a, b, c, d, inp[ 5], S31, 0xFFFA3942L) # 33 \r\n d = XX(H, d, a, b, c, inp[ 8], S32, 0x8771F681L) # 34 \r\n c = XX(H, c, d, a, b, inp[11], S33, 0x6D9D6122L) # 35 \r\n b = XX(H, b, c, d, a, inp[14], S34, 0xFDE5380CL) # 36 \r\n a = XX(H, a, b, c, d, inp[ 1], S31, 0xA4BEEA44L) # 37 \r\n d = XX(H, d, a, b, c, inp[ 4], S32, 0x4BDECFA9L) # 38 \r\n c = XX(H, c, d, a, b, inp[ 7], S33, 0xF6BB4B60L) # 39 \r\n b = XX(H, b, c, d, a, inp[10], S34, 0xBEBFBC70L) # 40 \r\n a = XX(H, a, b, c, d, inp[13], S31, 0x289B7EC6L) # 41 \r\n d = XX(H, d, a, b, c, inp[ 0], S32, 0xEAA127FAL) # 42 \r\n c = XX(H, c, d, a, b, inp[ 3], S33, 0xD4EF3085L) # 43 \r\n b = XX(H, b, c, d, a, inp[ 6], S34, 0x04881D05L) # 44 \r\n a = XX(H, a, b, c, d, inp[ 9], S31, 0xD9D4D039L) # 45 \r\n d = XX(H, d, a, b, c, inp[12], S32, 0xE6DB99E5L) # 46 \r\n c = XX(H, c, d, a, b, inp[15], S33, 0x1FA27CF8L) # 47 \r\n b = XX(H, b, c, d, a, inp[ 2], S34, 0xC4AC5665L) # 48 \r\n\r\n # Round 4.\r\n\r\n S41, S42, S43, S44 = 6, 10, 15, 21\r\n\r\n a = XX(I, a, b, c, d, inp[ 0], S41, 0xF4292244L) # 49 \r\n d = XX(I, d, a, b, c, inp[ 7], S42, 0x432AFF97L) # 50 \r\n c = XX(I, c, d, a, b, inp[14], S43, 0xAB9423A7L) # 51 \r\n b = XX(I, b, c, d, a, inp[ 5], S44, 0xFC93A039L) # 52 \r\n a = XX(I, a, b, c, d, inp[12], S41, 0x655B59C3L) # 53 \r\n d = XX(I, d, a, b, c, inp[ 3], S42, 0x8F0CCC92L) # 54 \r\n c = XX(I, c, d, a, b, inp[10], S43, 0xFFEFF47DL) # 55 \r\n b = XX(I, b, c, d, a, inp[ 1], S44, 0x85845DD1L) # 56 \r\n a = XX(I, a, b, c, d, inp[ 8], S41, 0x6FA87E4FL) # 57 \r\n d = XX(I, d, a, b, c, inp[15], S42, 0xFE2CE6E0L) # 58 \r\n c = XX(I, c, d, a, b, inp[ 6], S43, 0xA3014314L) # 59 \r\n b = XX(I, b, c, d, a, inp[13], S44, 0x4E0811A1L) # 60 \r\n a = XX(I, a, b, c, d, inp[ 4], S41, 0xF7537E82L) # 61 \r\n d = XX(I, d, a, b, c, inp[11], S42, 0xBD3AF235L) # 62 \r\n c = XX(I, c, d, a, b, inp[ 2], S43, 0x2AD7D2BBL) # 63 \r\n b = XX(I, b, c, d, a, inp[ 9], S44, 0xEB86D391L) # 64 \r\n\r\n A = (A + a) & 0xffffffffL\r\n B = (B + b) & 0xffffffffL\r\n C = (C + c) & 0xffffffffL\r\n D = (D + d) & 0xffffffffL\r\n\r\n self.A, self.B, self.C, self.D = A, B, C, D"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update(self, inBuf):\r\n leninBuf = long(len(inBuf))\r\n\r\n # Compute number of bytes mod 64.\r\n index = (self.count[0] >> 3) & 0x3FL\r\n\r\n # Update number of bits.\r\n self.count[0] = self.count[0] + (leninBuf << 3)\r\n if self.count[0] < (leninBuf << 3):\r\n self.count[1] = self.count[1] + 1\r\n self.count[1] = self.count[1] + (leninBuf >> 29)\r\n\r\n partLen = 64 - index\r\n\r\n if leninBuf >= partLen:\r\n self.input[index:] = map(None, inBuf[:partLen])\r\n self._transform(_bytelist2long(self.input))\r\n i = partLen\r\n while i + 63 < leninBuf:\r\n self._transform(_bytelist2long(map(None, inBuf[i:i+64])))\r\n i = i + 64\r\n else:\r\n self.input = map(None, inBuf[i:leninBuf])\r\n else:\r\n i = 0\r\n self.input = self.input + map(None, inBuf)", "response": "Add to the current message and update the md5 object with the string arg."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nterminates the message - digest computation and return digest.", "response": "def digest(self):\r\n \"\"\"Terminate the message-digest computation and return digest.\r\n\r\n Return the digest of the strings passed to the update()\r\n method so far. This is a 16-byte string which may contain\r\n non-ASCII characters, including null bytes.\r\n \"\"\"\r\n\r\n A = self.A\r\n B = self.B\r\n C = self.C\r\n D = self.D\r\n input = [] + self.input\r\n count = [] + self.count\r\n\r\n index = (self.count[0] >> 3) & 0x3fL\r\n\r\n if index < 56:\r\n padLen = 56 - index\r\n else:\r\n padLen = 120 - index\r\n\r\n padding = ['\\200'] + ['\\000'] * 63\r\n self.update(padding[:padLen])\r\n\r\n # Append length (before padding).\r\n bits = _bytelist2long(self.input[:56]) + count\r\n\r\n self._transform(bits)\r\n\r\n # Store state in digest.\r\n digest = _long2bytes(self.A << 96, 16)[:4] + \\\r\n _long2bytes(self.B << 64, 16)[4:8] + \\\r\n _long2bytes(self.C << 32, 16)[8:12] + \\\r\n _long2bytes(self.D, 16)[12:]\r\n\r\n self.A = A \r\n self.B = B\r\n self.C = C\r\n self.D = D\r\n self.input = input \r\n self.count = count \r\n\r\n return digest"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef hexdigest(self):\r\n\r\n d = map(None, self.digest())\r\n d = map(ord, d)\r\n d = map(lambda x:\"%02x\" % x, d)\r\n d = string.join(d, '')\r\n\r\n return d", "response": "Terminate and return digest in HEX form."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning value used to compare size of this node", "response": "def value( self, node, parent=None ):\n \"\"\"Return value used to compare size of this node\"\"\"\n # this is the *weighted* size/contribution of the node \n try:\n return node['contribution']\n except KeyError, err:\n contribution = int(node.get('totsize',0)/float( len(node.get('parents',())) or 1))\n node['contribution'] = contribution\n return contribution"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef label( self, node ):\n result = []\n if node.get('type'):\n result.append( node['type'] )\n if node.get('name' ):\n result.append( node['name'] )\n elif node.get('value') is not None:\n result.append( unicode(node['value'])[:32])\n if 'module' in node and not node['module'] in result:\n result.append( ' in %s'%( node['module'] ))\n if node.get( 'size' ):\n result.append( '%s'%( mb( node['size'] )))\n if node.get( 'totsize' ):\n result.append( '(%s)'%( mb( node['totsize'] )))\n parent_count = len( node.get('parents',()))\n if parent_count > 1:\n result.append( '/%s refs'%( parent_count ))\n return \" \".join(result)", "response": "Return textual description of this node"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nretrieving the set of parents for the given node", "response": "def parents( self, node ):\n \"\"\"Retrieve/calculate the set of parents for the given node\"\"\"\n if 'index' in node:\n index = node['index']()\n parents = list(meliaeloader.children( node, index, 'parents' ))\n return parents \n return []"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef best_parent( self, node, tree_type=None ):\n parents = self.parents(node)\n selected_parent = None\n if node['type'] == 'type':\n module = \".\".join( node['name'].split( '.' )[:-1] )\n if module:\n for mod in parents:\n if mod['type'] == 'module' and mod['name'] == module:\n selected_parent = mod \n if parents and selected_parent is None:\n parents.sort( key = lambda x: self.value(node, x) )\n return parents[-1]\n return selected_parent", "response": "Choose the best parent for a given node"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef Gooey(f=None,\n advanced=True,\n language='english',\n show_config=True,\n program_name=None,\n program_description=None,\n default_size=(610, 530),\n required_cols=2,\n optional_cols=2,\n dump_build_config=False,\n monospace_display=False):\n '''\n Decorator for client code's main function.\n Serializes argparse data to JSON for use with the Gooey front end\n '''\n\n params = locals()\n\n def build(payload):\n def run_gooey(self, args=None, namespace=None):\n source_path = sys.argv[0]\n build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)\n\n if dump_build_config:\n config_path = os.path.join(os.getcwd(), 'gooey_config.json')\n print( 'Writing Build Config to: {}'.format(config_path))\n with open(config_path, 'w') as f:\n f.write(json.dumps(build_spec, indent=2))\n application.run(build_spec)\n\n def inner2(*args, **kwargs):\n ArgumentParser.original_parse_args = ArgumentParser.parse_args\n ArgumentParser.parse_args = run_gooey\n return payload(*args, **kwargs)\n\n inner2.__name__ = payload.__name__\n return inner2\n\n def run_without_gooey(func):\n return lambda: func()\n\n if IGNORE_COMMAND in sys.argv:\n sys.argv.remove(IGNORE_COMMAND)\n if callable(f):\n return run_without_gooey(f)\n return run_without_gooey\n\n if callable(f):\n return build(f)\n return build", "response": "A function that returns a Gooey client code s main function."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbuild a list of wx Component objects from a Json widget list", "response": "def build_components(widget_list):\n '''\n :param widget_list: list of dicts containing widget info (name, type, etc..)\n :return: ComponentList\n\n Converts the Json widget information into concrete wx Widget types\n '''\n required_args, optional_args = partition(widget_list, is_required)\n checkbox_args, general_args = partition(map(build_widget, optional_args), is_checkbox)\n\n required_args = map(build_widget, required_args)\n optional_args = general_args + checkbox_args\n\n return ComponentList(required_args, optional_args)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _merge_asized(base, other, level=0):\n ref2key = lambda ref: ref.name.split(':')[0]\n base.size += other.size\n base.flat += other.flat\n if level > 0:\n base.name = ref2key(base)\n # Add refs from other to base. Any new refs are appended.\n base.refs = list(base.refs) # we may need to append items\n refs = {}\n for ref in base.refs:\n refs[ref2key(ref)] = ref\n for ref in other.refs:\n key = ref2key(ref)\n if key in refs:\n _merge_asized(refs[key], ref, level=level+1)\n else:\n # Don't modify existing Asized instances => deepcopy\n base.refs.append(deepcopy(ref))\n base.refs[-1].name = key", "response": "Merge two Asized instances into base."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmerge the snapshot size information of multiple tracked objects at time tref into merged.", "response": "def _merge_objects(tref, merged, obj):\n \"\"\"\n Merge the snapshot size information of multiple tracked objects. The\n tracked object `obj` is scanned for size information at time `tref`.\n The sizes are merged into **Asized** instance `merged`.\n \"\"\"\n size = None\n for (timestamp, tsize) in obj.snapshots:\n if timestamp == tref:\n size = tsize\n if size:\n _merge_asized(merged, size)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting the stack - trace to a nice readable format.", "response": "def _format_trace(trace):\n \"\"\"\n Convert the (stripped) stack-trace to a nice readable format. The stack\n trace `trace` is a list of frame records as returned by\n **inspect.stack** but without the frame objects.\n Returns a string.\n \"\"\"\n lines = []\n for fname, lineno, func, src, _ in trace:\n if src:\n for line in src:\n lines.append(' '+line.strip()+'\\n')\n lines.append(' %s:%4d in %s\\n' % (fname, lineno, func))\n return ''.join(lines)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef load_stats(self, fdump):\n if isinstance(fdump, type('')):\n fdump = open(fdump, 'rb')\n self.index = pickle.load(fdump)\n self.snapshots = pickle.load(fdump)\n self.sorted = []", "response": "Load the data from a dump file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef dump_stats(self, fdump, close=True):\n if self.tracker:\n self.tracker.stop_periodic_snapshots()\n\n if isinstance(fdump, type('')):\n fdump = open(fdump, 'wb')\n pickle.dump(self.index, fdump, protocol=pickle.HIGHEST_PROTOCOL)\n pickle.dump(self.snapshots, fdump, protocol=pickle.HIGHEST_PROTOCOL)\n if close:\n fdump.close()", "response": "Dump the logged data to a file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _init_sort(self):\n if not self.sorted:\n # Identify the snapshot that tracked the largest amount of memory.\n tmax = None\n maxsize = 0\n for snapshot in self.snapshots:\n if snapshot.tracked_total > maxsize:\n tmax = snapshot.timestamp\n for key in list(self.index.keys()):\n for tobj in self.index[key]:\n tobj.classname = key\n tobj.size = tobj.get_max_size()\n tobj.tsize = tobj.get_size_at_time(tmax)\n self.sorted.extend(self.index[key])", "response": "Initialize the data to be sorted."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsorting the tracked objects according to the supplied criteria.", "response": "def sort_stats(self, *args):\n \"\"\"\n Sort the tracked objects according to the supplied criteria. The\n argument is a string identifying the basis of a sort (example: 'size'\n or 'classname'). When more than one key is provided, then additional\n keys are used as secondary criteria when there is equality in all keys\n selected before them. For example, ``sort_stats('name', 'size')`` will\n sort all the entries according to their class name, and resolve all\n ties (identical class names) by sorting by size. The criteria are\n fields in the tracked object instances. Results are stored in the\n ``self.sorted`` list which is used by ``Stats.print_stats()`` and other\n methods. The fields available for sorting are:\n\n 'classname'\n the name with which the class was registered\n 'name'\n the classname\n 'birth'\n creation timestamp\n 'death'\n destruction timestamp\n 'size'\n the maximum measured size of the object\n 'tsize'\n the measured size during the largest snapshot\n 'repr'\n string representation of the object\n\n Note that sorts on size are in descending order (placing most memory\n consuming items first), whereas name, repr, and creation time searches\n are in ascending order (alphabetical).\n\n The function returns self to allow calling functions on the result::\n\n stats.sort_stats('size').reverse_order().print_stats()\n \"\"\"\n\n criteria = ('classname', 'tsize', 'birth', 'death',\n 'name', 'repr', 'size')\n\n if not set(criteria).issuperset(set(args)):\n raise ValueError(\"Invalid sort criteria\")\n\n if not args:\n args = criteria\n\n def args_to_tuple(obj):\n keys = []\n for attr in args:\n attribute = getattr(obj, attr)\n if attr in ('tsize', 'size'):\n attribute = -attribute\n keys.append(attribute)\n return tuple(keys)\n\n self._init_sort()\n self.sorted.sort(key=args_to_tuple)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef annotate_snapshot(self, snapshot):\n if hasattr(snapshot, 'classes'):\n return\n\n snapshot.classes = {}\n\n for classname in list(self.index.keys()):\n total = 0\n active = 0\n merged = Asized(0, 0)\n for tobj in self.index[classname]:\n _merge_objects(snapshot.timestamp, merged, tobj)\n total += tobj.get_size_at_time(snapshot.timestamp)\n if tobj.birth < snapshot.timestamp and \\\n (tobj.death is None or tobj.death > snapshot.timestamp):\n active += 1\n\n try:\n pct = total * 100.0 / snapshot.total\n except ZeroDivisionError: # pragma: no cover\n pct = 0\n try:\n avg = total / active\n except ZeroDivisionError:\n avg = 0\n\n snapshot.classes[classname] = dict(sum=total,\n avg=avg,\n pct=pct,\n active=active)\n snapshot.classes[classname]['merged'] = merged", "response": "Annotate a snapshot with the statistical data."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _print_refs(self, refs, total, prefix=' ',\n level=1, minsize=0, minpct=0.1):\n \"\"\"\n Print individual referents recursively.\n \"\"\"\n lrefs = list(refs)\n lrefs.sort(key=lambda x: x.size)\n lrefs.reverse()\n for ref in lrefs:\n if ref.size > minsize and (ref.size*100.0/total) > minpct:\n self.stream.write('%-50s %-14s %3d%% [%d]\\n' % (\n trunc(prefix+str(ref.name), 50),\n pp(ref.size),\n int(ref.size*100.0/total),\n level\n ))\n self._print_refs(ref.refs, total, prefix=prefix+' ',\n level=level+1)", "response": "Print all the referents in the tree."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef print_object(self, tobj):\n if tobj.death:\n self.stream.write('%-32s ( free ) %-35s\\n' % (\n trunc(tobj.name, 32, left=1), trunc(tobj.repr, 35)))\n else:\n self.stream.write('%-32s 0x%08x %-35s\\n' % (\n trunc(tobj.name, 32, left=1),\n tobj.id,\n trunc(tobj.repr, 35)\n ))\n if tobj.trace:\n self.stream.write(_format_trace(tobj.trace))\n for (timestamp, size) in tobj.snapshots:\n self.stream.write(' %-30s %s\\n' % (\n pp_timestamp(timestamp), pp(size.size)\n ))\n self._print_refs(size.refs, size.size)\n if tobj.death is not None:\n self.stream.write(' %-30s finalize\\n' % (\n pp_timestamp(tobj.death),\n ))", "response": "Print the gathered information of the object tobj in human - readable format."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite tracked objects to stdout.", "response": "def print_stats(self, clsname=None, limit=1.0):\n \"\"\"\n Write tracked objects to stdout. The output can be filtered and\n pruned. Only objects are printed whose classname contain the substring\n supplied by the `clsname` argument. The output can be pruned by\n passing a `limit` value.\n\n :param clsname: Only print objects whose classname contain the given\n substring.\n :param limit: If `limit` is a float smaller than one, only the supplied\n percentage of the total tracked data is printed. If `limit` is\n bigger than one, this number of tracked objects are printed.\n Tracked objects are first filtered, and then pruned (if specified).\n \"\"\"\n if self.tracker:\n self.tracker.stop_periodic_snapshots()\n\n if not self.sorted:\n self.sort_stats()\n\n _sorted = self.sorted\n\n if clsname:\n _sorted = [to for to in _sorted if clsname in to.classname]\n\n if limit < 1.0:\n limit = max(1, int(len(self.sorted) * limit))\n _sorted = _sorted[:int(limit)]\n\n # Emit per-instance data\n for tobj in _sorted:\n self.print_object(tobj)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprints summary for each class in the current instance.", "response": "def print_summary(self):\n \"\"\"\n Print per-class summary for each snapshot.\n \"\"\"\n # Emit class summaries for each snapshot\n classlist = self.tracked_classes\n\n fobj = self.stream\n\n fobj.write('---- SUMMARY '+'-'*66+'\\n')\n for snapshot in self.snapshots:\n self.annotate_snapshot(snapshot)\n fobj.write('%-35s %11s %12s %12s %5s\\n' % (\n trunc(snapshot.desc, 35),\n 'active',\n pp(snapshot.asizeof_total),\n 'average',\n 'pct'\n ))\n for classname in classlist:\n info = snapshot.classes.get(classname)\n fobj.write(' %-33s %11d %12s %12s %4d%%\\n' % (\n trunc(classname, 33),\n info['active'],\n pp(info['sum']),\n pp(info['avg']),\n info['pct']\n ))\n fobj.write('-'*79+'\\n')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprint all the referents in the tree.", "response": "def _print_refs(self, fobj, refs, total, level=1, minsize=0, minpct=0.1):\n \"\"\"\n Print individual referents recursively.\n \"\"\"\n lrefs = list(refs)\n lrefs.sort(key=lambda x: x.size)\n lrefs.reverse()\n if level == 1:\n fobj.write('\\n')\n for ref in lrefs:\n if ref.size > minsize and (ref.size*100.0/total) > minpct:\n data = dict(level=level,\n name=trunc(str(ref.name), 128),\n size=pp(ref.size),\n pct=ref.size*100.0/total)\n fobj.write(self.refrow % data)\n self._print_refs(fobj, ref.refs, total, level=level+1)\n if level == 1:\n fobj.write(\"
\\n\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef print_class_details(self, fname, classname):\n fobj = open(fname, \"w\")\n fobj.write(self.header % (classname, self.style))\n\n fobj.write(\"

%s

\\n\" % (classname))\n\n sizes = [tobj.get_max_size() for tobj in self.index[classname]]\n total = 0\n for s in sizes:\n total += s\n data = {'cnt': len(self.index[classname]), 'cls': classname}\n data['avg'] = pp(total / len(sizes))\n data['max'] = pp(max(sizes))\n data['min'] = pp(min(sizes))\n fobj.write(self.class_summary % data)\n\n fobj.write(self.charts[classname])\n\n fobj.write(\"

Coalesced Referents per Snapshot

\\n\")\n for snapshot in self.snapshots:\n if classname in snapshot.classes:\n merged = snapshot.classes[classname]['merged']\n fobj.write(self.class_snapshot % {\n 'name': snapshot.desc, 'cls':classname, 'total': pp(merged.size)\n })\n if merged.refs:\n self._print_refs(fobj, merged.refs, merged.size)\n else:\n fobj.write('

No per-referent sizes recorded.

\\n')\n\n fobj.write(\"

Instances

\\n\")\n for tobj in self.index[classname]:\n fobj.write('\\n')\n fobj.write('\\n' % (tobj.name, tobj.id))\n if tobj.repr:\n fobj.write(\"\\n\" % tobj.repr)\n fobj.write(\"\\n\" % (pp_timestamp(tobj.birth), pp_timestamp(tobj.death)))\n if tobj.trace:\n trace = \"
%s
\" % (_format_trace(tobj.trace))\n fobj.write(\"\\n\" % trace)\n for (timestamp, size) in tobj.snapshots:\n fobj.write(\"\" % pp_timestamp(timestamp))\n if not size.refs:\n fobj.write(\"\\n\" % pp(size.size))\n else:\n fobj.write(\"\\n\")\n fobj.write(\"
Instance%s at 0x%08x
Representation%s 
Lifetime%s - %s
Instantiation%s
%s%s
%s\" % pp(size.size))\n self._print_refs(fobj, size.refs, size.size)\n fobj.write(\"
\\n\")\n\n fobj.write(self.footer)\n fobj.close()", "response": "Print detailed statistics and instances for the class classname."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate the title page.", "response": "def create_title_page(self, filename, title=''):\n \"\"\"\n Output the title page.\n \"\"\"\n fobj = open(filename, \"w\")\n fobj.write(self.header % (title, self.style))\n\n fobj.write(\"

%s

\\n\" % title)\n fobj.write(\"

Memory distribution over time

\\n\")\n fobj.write(self.charts['snapshots'])\n\n fobj.write(\"

Snapshots statistics

\\n\")\n fobj.write('\\n')\n\n classlist = list(self.index.keys())\n classlist.sort()\n\n for snapshot in self.snapshots:\n fobj.write('\\n')\n\n fobj.write(\"
\\n')\n fobj.write('\\n')\n fobj.write(\"

%s snapshot at %s

\\n\" % (\n snapshot.desc or 'Untitled',\n pp_timestamp(snapshot.timestamp)\n ))\n\n data = {}\n data['sys'] = pp(snapshot.system_total.vsz)\n data['tracked'] = pp(snapshot.tracked_total)\n data['asizeof'] = pp(snapshot.asizeof_total)\n data['overhead'] = pp(getattr(snapshot, 'overhead', 0))\n\n fobj.write(self.snapshot_summary % data)\n\n if snapshot.tracked_total:\n fobj.write(self.snapshot_cls_header)\n for classname in classlist:\n data = snapshot.classes[classname].copy()\n data['cls'] = '%s' % (self.relative_path(self.links[classname]), classname)\n data['sum'] = pp(data['sum'])\n data['avg'] = pp(data['avg'])\n fobj.write(self.snapshot_cls % data)\n fobj.write('
')\n fobj.write('
\\n')\n if snapshot.tracked_total:\n fobj.write(self.charts[snapshot])\n fobj.write('
\\n\")\n fobj.write(self.footer)\n fobj.close()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a chart that depicts the lifetime of the instance registered with the given classname. The output is written to filename.", "response": "def create_lifetime_chart(self, classname, filename=''):\n \"\"\"\n Create chart that depicts the lifetime of the instance registered with\n `classname`. The output is written to `filename`.\n \"\"\"\n try:\n from pylab import figure, title, xlabel, ylabel, plot, savefig\n except ImportError:\n return HtmlStats.nopylab_msg % (classname+\" lifetime\")\n\n cnt = []\n for tobj in self.index[classname]:\n cnt.append([tobj.birth, 1])\n if tobj.death:\n cnt.append([tobj.death, -1])\n cnt.sort()\n for i in range(1, len(cnt)):\n cnt[i][1] += cnt[i-1][1]\n #if cnt[i][0] == cnt[i-1][0]:\n # del cnt[i-1]\n\n x = [t for [t,c] in cnt]\n y = [c for [t,c] in cnt]\n\n figure()\n xlabel(\"Execution time [s]\")\n ylabel(\"Instance #\")\n title(\"%s instances\" % classname)\n plot(x, y, 'o')\n savefig(filename)\n\n return self.chart_tag % (os.path.basename(filename))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_snapshot_chart(self, filename=''):\n try:\n from pylab import figure, title, xlabel, ylabel, plot, fill, legend, savefig\n import matplotlib.mlab as mlab\n except ImportError:\n return self.nopylab_msg % (\"memory allocation\")\n\n classlist = self.tracked_classes\n\n times = [snapshot.timestamp for snapshot in self.snapshots]\n base = [0] * len(self.snapshots)\n poly_labels = []\n polys = []\n for cn in classlist:\n pct = [snapshot.classes[cn]['pct'] for snapshot in self.snapshots]\n if max(pct) > 3.0:\n sz = [float(fp.classes[cn]['sum'])/(1024*1024) for fp in self.snapshots]\n sz = [sx+sy for sx, sy in zip(base, sz)]\n xp, yp = mlab.poly_between(times, base, sz)\n polys.append( ((xp, yp), {'label': cn}) )\n poly_labels.append(cn)\n base = sz\n\n figure()\n title(\"Snapshot Memory\")\n xlabel(\"Execution Time [s]\")\n ylabel(\"Virtual Memory [MiB]\")\n\n sizes = [float(fp.asizeof_total)/(1024*1024) for fp in self.snapshots]\n plot(times, sizes, 'r--', label='Total')\n sizes = [float(fp.tracked_total)/(1024*1024) for fp in self.snapshots]\n plot(times, sizes, 'b--', label='Tracked total')\n\n for (args, kwds) in polys:\n fill(*args, **kwds)\n legend(loc=2)\n savefig(filename)\n\n return self.chart_tag % (self.relative_path(filename))", "response": "Create a chart that depicts the memory allocation over time apportioned to\n the tracked classes."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a pie chart that depicts the distribution of the allocated memory for a given snapshot.", "response": "def create_pie_chart(self, snapshot, filename=''):\n \"\"\"\n Create a pie chart that depicts the distribution of the allocated memory\n for a given `snapshot`. The chart is saved to `filename`.\n \"\"\"\n try:\n from pylab import figure, title, pie, axes, savefig\n from pylab import sum as pylab_sum\n except ImportError:\n return self.nopylab_msg % (\"pie_chart\")\n\n # Don't bother illustrating a pie without pieces.\n if not snapshot.tracked_total:\n return ''\n\n classlist = []\n sizelist = []\n for k, v in list(snapshot.classes.items()):\n if v['pct'] > 3.0:\n classlist.append(k)\n sizelist.append(v['sum'])\n sizelist.insert(0, snapshot.asizeof_total - pylab_sum(sizelist))\n classlist.insert(0, 'Other')\n #sizelist = [x*0.01 for x in sizelist]\n\n title(\"Snapshot (%s) Memory Distribution\" % (snapshot.desc))\n figure(figsize=(8,8))\n axes([0.1, 0.1, 0.8, 0.8])\n pie(sizelist, labels=classlist)\n savefig(filename, dpi=50)\n\n return self.chart_tag % (self.relative_path(filename))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate HTML page and additional files in a directory derived from fname.", "response": "def create_html(self, fname, title=\"ClassTracker Statistics\"):\n \"\"\"\n Create HTML page `fname` and additional files in a directory derived\n from `fname`.\n \"\"\"\n # Create a folder to store the charts and additional HTML files.\n self.basedir = os.path.dirname(os.path.abspath(fname))\n self.filesdir = os.path.splitext(fname)[0] + '_files'\n if not os.path.isdir(self.filesdir):\n os.mkdir(self.filesdir)\n self.filesdir = os.path.abspath(self.filesdir)\n self.links = {}\n\n # Annotate all snapshots in advance\n self.annotate()\n\n # Create charts. The tags to show the images are returned and stored in\n # the self.charts dictionary. This allows to return alternative text if\n # the chart creation framework is not available.\n self.charts = {}\n fn = os.path.join(self.filesdir, 'timespace.png')\n self.charts['snapshots'] = self.create_snapshot_chart(fn)\n\n for fp, idx in zip(self.snapshots, list(range(len(self.snapshots)))):\n fn = os.path.join(self.filesdir, 'fp%d.png' % (idx))\n self.charts[fp] = self.create_pie_chart(fp, fn)\n\n for cn in list(self.index.keys()):\n fn = os.path.join(self.filesdir, cn.replace('.', '_')+'-lt.png')\n self.charts[cn] = self.create_lifetime_chart(cn, fn)\n\n # Create HTML pages first for each class and then the index page.\n for cn in list(self.index.keys()):\n fn = os.path.join(self.filesdir, cn.replace('.', '_')+'.html')\n self.links[cn] = fn\n self.print_class_details(fn, cn)\n\n self.create_title_page(fname, title=title)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef select_from(self, parent_path):\n path_cls = type(parent_path)\n is_dir = path_cls.is_dir\n exists = path_cls.exists\n listdir = parent_path._accessor.listdir\n return self._select_from(parent_path, is_dir, exists, listdir)", "response": "Iterate over all child paths of parent_path matched by this\n selector. This can contain parent_path itself."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef relative_to(self, *other):\n # For the purpose of this method, drive and root are considered\n # separate parts, i.e.:\n # Path('c:/').relative_to('c:') gives Path('/')\n # Path('c:/').relative_to('/') raise ValueError\n if not other:\n raise TypeError(\"need at least one argument\")\n parts = self._parts\n drv = self._drv\n root = self._root\n if root:\n abs_parts = [drv, root] + parts[1:]\n else:\n abs_parts = parts\n to_drv, to_root, to_parts = self._parse_args(other)\n if to_root:\n to_abs_parts = [to_drv, to_root] + to_parts[1:]\n else:\n to_abs_parts = to_parts\n n = len(to_abs_parts)\n cf = self._flavour.casefold_parts\n if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):\n formatted = self._format_parsed_parts(to_drv, to_root, to_parts)\n raise ValueError(\"{!r} does not start with {!r}\"\n .format(str(self), str(formatted)))\n return self._from_parsed_parts('', root if n == 1 else '',\n abs_parts[n:])", "response": "Return the relative path to another path identified by the passed\n arguments."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\niterate over this subtree and yield all existing files of any kind matching the given pattern.", "response": "def glob(self, pattern):\n \"\"\"Iterate over this subtree and yield all existing files (of any\n kind, including directories) matching the given pattern.\n \"\"\"\n pattern = self._flavour.casefold(pattern)\n drv, root, pattern_parts = self._flavour.parse_parts((pattern,))\n if drv or root:\n raise NotImplementedError(\"Non-relative patterns are unsupported\")\n selector = _make_selector(tuple(pattern_parts))\n for p in selector.select_from(self):\n yield p"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwriting the data to the file in bytes mode.", "response": "def write_bytes(self, data):\n \"\"\"\n Open the file in bytes mode, write to it, and close the file.\n \"\"\"\n if not isinstance(data, six.binary_type):\n raise TypeError(\n 'data must be %s, not %s' %\n (six.binary_type.__class__.__name__, data.__class__.__name__))\n with self.open(mode='wb') as f:\n return f.write(data)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite the data to the log file in text mode.", "response": "def write_text(self, data, encoding=None, errors=None):\n \"\"\"\n Open the file in text mode, write to it, and close the file.\n \"\"\"\n if not isinstance(data, six.text_type):\n raise TypeError(\n 'data must be %s, not %s' %\n (six.text_type.__class__.__name__, data.__class__.__name__))\n with self.open(mode='w', encoding=encoding, errors=errors) as f:\n return f.write(data)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef relpath_posix(recwalk_result, pardir, fromwinpath=False):\n ''' Helper function to convert all paths to relative posix like paths (to ease comparison) '''\n return recwalk_result[0], path2unix(os.path.join(os.path.relpath(recwalk_result[0], pardir),recwalk_result[1]), nojoin=True, fromwinpath=fromwinpath)", "response": "Helper function to convert all paths to relative posix like paths"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsorts a dict of paths parts and organically to the paths alphabetical order", "response": "def sort_dict_of_paths(d):\n \"\"\" Sort a dict containing paths parts (ie, paths divided in parts and stored as a list). Top paths will be given precedence over deeper paths. \"\"\"\n # Find the path that is the deepest, and count the number of parts\n max_rec = max(len(x) if x else 0 for x in d.values())\n # Pad other paths with empty parts to fill in, so that all paths will have the same number of parts (necessary to compare correctly, else deeper paths may get precedence over top ones, since the folder name will be compared to filenames!)\n for key in d.keys():\n if d[key]:\n d[key] = ['']*(max_rec-len(d[key])) + d[key]\n # Sort the dict relatively to the paths alphabetical order\n d_sort = sorted(d.items(), key=lambda x: x[1])\n return d_sort"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sort_group(d, return_only_first=False):\n ''' Sort a dictionary of relative paths and cluster equal paths together at the same time '''\n # First, sort the paths in order (this must be a couple: (parent_dir, filename), so that there's no ambiguity because else a file at root will be considered as being after a folder/file since the ordering is done alphabetically without any notion of tree structure).\n d_sort = sort_dict_of_paths(d)\n # Pop the first item in the ordered list\n base_elt = (-1, None)\n while (base_elt[1] is None and d_sort):\n base_elt = d_sort.pop(0)\n # No element, then we just return\n if base_elt[1] is None:\n return None\n # Else, we will now group equivalent files together (remember we are working on multiple directories, so we can have multiple equivalent relative filepaths, but of course the absolute filepaths are different).\n else:\n # Init by creating the first group and pushing the first ordered filepath into the first group\n lst = []\n lst.append([base_elt])\n if d_sort:\n # For each subsequent filepath\n for elt in d_sort:\n # If the filepath is not empty (generator died)\n if elt[1] is not None:\n # If the filepath is the same to the latest grouped filepath, we add it to the same group\n if elt[1] == base_elt[1]:\n lst[-1].append(elt)\n # Else the filepath is different: we create a new group, add the filepath to this group, and replace the latest grouped filepath\n else:\n if return_only_first: break # break here if we only need the first group\n lst.append([elt])\n base_elt = elt # replace the latest grouped filepath\n return lst", "response": "Sort a dictionary of relative paths and cluster equal paths together at the same time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef majority_vote_byte_scan(relfilepath, fileslist, outpath, blocksize=65535, default_char_null=False):\n '''Takes a list of files in string format representing the same data, and disambiguate by majority vote: for position in string, if the character is not the same accross all entries, we keep the major one. If none, it will be replaced by a null byte (because we can't know if any of the entries are correct about this character).\n relfilepath is the filename or the relative file path relative to the parent directory (ie, this is the relative path so that we can compare the files from several directories).'''\n # The idea of replication combined with ECC was a bit inspired by this paper: Friedman, Roy, Yoav Kantor, and Amir Kantor. \"Combining Erasure-Code and Replication Redundancy Schemes for Increased Storage and Repair Efficiency in P2P Storage Systems.\", 2013, Technion, Computer Science Department, Technical Report CS-2013-03\n # But it is a very well known concept in redundancy engineering, usually called triple-modular redundancy (which is here extended to n-modular since we can supply any number of files we want, not just three).\n # Preference in case of ambiguity is always given to the file of the first folder.\n\n fileshandles = []\n for filepath in fileslist:\n if filepath:\n # Already a file handle? Just store it in the fileshandles list\n if hasattr(filepath, 'read'):\n fileshandles.append(filepath)\n # Else it's a string filepath, open the file\n else:\n fileshandles.append(open(filepath, 'rb'))\n\n # Create and open output (merged) file, except if we were already given a file handle\n if hasattr(outpath, 'write'):\n outfile = outpath\n else:\n outpathfull = os.path.join(outpath, relfilepath)\n pardir = os.path.dirname(outpathfull)\n if not os.path.exists(pardir):\n os.makedirs(pardir)\n outfile = open(outpathfull, 'wb')\n\n # Cannot vote if there's not at least 3 files!\n # In this case, just copy the file from the first folder, verbatim\n if len(fileshandles) < 3:\n # If there's at least one input file, then copy it verbatim to the output folder\n if fileshandles:\n create_dir_if_not_exist(os.path.dirname(outpathfull))\n buf = 1\n while (buf):\n buf = fileshandles[0].read()\n outfile.write(buf)\n outfile.flush()\n return (1, \"Error with file %s: only %i copies available, cannot vote (need at least 3)! Copied the first file from the first folder, verbatim.\" % (relfilepath, len(fileshandles)))\n\n errors = []\n entries = [1]*len(fileshandles) # init with 0 to start the while loop\n while (entries.count('') < len(fileshandles)):\n final_entry = []\n # Read a block from all input files into memory\n for i in xrange(len(fileshandles)):\n entries[i] = fileshandles[i].read(blocksize)\n\n # End of file for all files, we exit\n if entries.count('') == len(fileshandles):\n break\n # Else if there's only one file, just copy the file's content over\n elif len(entries) == 1:\n final_entry = entries[0]\n\n # Else, do the majority vote\n else:\n # Walk along each column (imagine the strings being rows in a matrix, then we pick one column at each iteration = all characters at position i of each string), so that we can compare these characters easily\n for i in xrange(max(len(entry) for entry in entries)):\n hist = {} # kind of histogram, we just memorize how many times a character is presented at the position i in each string TODO: use collections.Counter instead of dict()?\n # Extract the character at position i of each string and compute the histogram at the same time (number of time this character appear among all strings at this position i)\n for entry in entries:\n # Check if we are not beyond the current entry's length\n if i < len(entry): # TODO: check this line, this should allow the vote to continue even if some files are shorter than others\n # Extract the character and use it to contribute to the histogram\n # TODO: add warning message when one file is not of the same size as the others\n key = str(ord(entry[i])) # convert to the ascii value to avoid any funky problem with encoding in dict keys\n hist[key] = hist.get(key, 0) + 1 # increment histogram for this value. If it does not exists, use 0. (essentially equivalent to hist[key] += 1 but with exception management if key did not already exists)\n # If there's only one character (it's the same accross all strings at position i), then it's an exact match, we just save the character and we can skip to the next iteration\n if len(hist) == 1:\n final_entry.append(chr(int(hist.iterkeys().next())))\n continue\n # Else, the character is different among different entries, we will pick the major one (mode)\n elif len(hist) > 1:\n # Sort the dict by value (and reverse because we want the most frequent first)\n skeys = sorted(hist, key=hist.get, reverse=True)\n # Ambiguity! If each entries present a different character (thus the major has only an occurrence of 1), then it's too ambiguous and we just set a null byte to signal that\n if hist[skeys[0]] == 1:\n if default_char_null:\n if default_char_null is True:\n final_entry.append(\"\\x00\")\n else:\n final_entry.append(default_char_null)\n else:\n # Use the entry of the first file that is still open\n first_char = ''\n for entry in entries:\n # Found the first file that has a character at this position: store it and break loop\n if i < len(entry):\n first_char = entry[i]\n break\n # Use this character in spite of ambiguity\n final_entry.append(first_char)\n errors.append(outfile.tell() + i) # Print an error indicating the characters that failed\n # Else if there is a tie (at least two characters appear with the same frequency), then we just pick one of them\n elif hist[skeys[0]] == hist[skeys[1]]:\n final_entry.append(chr(int(skeys[0]))) # TODO: find a way to account for both characters. Maybe return two different strings that will both have to be tested? (eg: maybe one has a tampered hash, both will be tested and if one correction pass the hash then it's ok we found the correct one)\n # Else we have a clear major character that appear in more entries than any other character, then we keep this one\n else:\n final_entry.append(chr(int(skeys[0]))) # alternative one-liner: max(hist.iteritems(), key=operator.itemgetter(1))[0]\n continue\n # Concatenate to a string (this is faster than using a string from the start and concatenating at each iteration because Python strings are immutable so Python has to copy over the whole string, it's in O(n^2)\n final_entry = ''.join(final_entry)\n # Commit to output file\n outfile.write(final_entry)\n outfile.flush()\n\n # Errors signaling\n if errors:\n error_msg = \"Unrecoverable corruptions (because of ambiguity) in file %s on characters: %s.\" % (relfilepath, [hex(int(x)) for x in errors]) # Signal to user that this file has unrecoverable corruptions (he may try to fix the bits manually or with his own script)\n return (1, error_msg) # return an error\n # Close all input files\n for fh in fileshandles:\n fh.close()\n # Close output file\n if outfile != outpath: # close only if we were not given a file handle in the first place\n outfile.flush()\n outfile.close()\n return (0, None)", "response": "Takes a list of files in string format representing the same data and disambiguates by majority vote."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef synchronize_files(inputpaths, outpath, database=None, tqdm_bar=None, report_file=None, ptee=None, verbose=False):\n ''' Main function to synchronize files contents by majority vote\n The main job of this function is to walk through the input folders and align the files, so that we can compare every files across every folders, one by one.\n The whole trick here is to align files, so that we don't need to memorize all the files in memory and we compare all equivalent files together: to do that, we ensure that we walk through the input directories in alphabetical order, and we pick the relative filepath at the top of the alphabetical order, this ensures the alignment of files between different folders, without memorizing the whole trees structures.\n '''\n # (Generator) Files Synchronization Algorithm:\n # Needs a function stable_dir_walking, which will walk through directories recursively but in always the same order on all platforms (same order for files but also for folders), whatever order it is, as long as it is stable.\n # Until there's no file in any of the input folders to be processed:\n # - curfiles <- load first file for each folder by using stable_dir_walking on each input folder.\n # - curfiles_grouped <- group curfiles_ordered:\n # * curfiles_ordered <- order curfiles alphabetically (need to separate the relative parent directory and the filename, to account for both without ambiguity)\n # * curfiles_grouped <- empty list\n # * curfiles_grouped[0] = add first element in curfiles_ordered\n # * last_group = 0\n # * for every subsequent element nextelt in curfiles_ordered:\n # . if nextelt == curfiles_grouped[last_group][0]: add nextelt into curfiles_grouped[last_group] (the latest group in curfiles_grouped)\n # . else: create a new group in curfiles_grouped (last_group += 1) and add nextelt into curfiles_grouped[last_group]\n # At this stage, curfiles_grouped[0] should contain a group of files with the same relative filepath from different input folders, and since we used stable_dir_walking, we are guaranteed that this file is the next to be processed in alphabetical order.\n # - Majority vote byte-by-byte for each of curfiles_grouped[0], and output winning byte to the output file.\n # - Update files list alignment: we will now ditch files in curfiles_grouped[0] from curfiles, and replace by the next files respectively from each respective folder. Since we processed in alphabetical (or whatever) order, the next loaded files will match the files in other curfiles_grouped groups that we could not process before.\n # At this point (after the loop), all input files have been processed in order, without maintaining the whole files list in memory, just one file per input folder.\n\n # Init files walking generator for each inputpaths\n recgen = [recwalk(path, sorting=True) for path in inputpaths]\n curfiles = {}\n recgen_exhausted = {}\n recgen_exhausted_count = 0\n nbpaths = len(inputpaths)\n retcode = 0\n\n if not ptee: ptee = sys.stdout\n\n # Open report file and write header\n if report_file is not None:\n rfile = open(report_file, 'wb')\n r_writer = csv.writer(rfile, delimiter='|', lineterminator='\\n', quotechar='\"')\n r_header = [\"filepath\"] + [\"dir%i\" % (i+1) for i in xrange(nbpaths)] + [\"hash-correct\", \"error_code\", \"errors\"]\n r_length = len(r_header)\n r_writer.writerow(r_header)\n\n # Initialization: load the first batch of files, one for each folder\n for i in xrange(len(recgen)):\n recgen_exhausted[i] = False\n try:\n if curfiles.get(i, None) is None:\n curfiles[i] = relpath_posix(recgen[i].next(), inputpaths[i])[1]\n except StopIteration:\n recgen_exhausted[i] = True\n recgen_exhausted_count += 1\n\n # Files lists alignment loop\n while recgen_exhausted_count < nbpaths:\n errcode = 0\n errmsg = None\n\n # Init a new report's row\n if report_file: r_row = [\"-\"] * r_length\n\n # -- Group equivalent relative filepaths together\n #print curfiles # debug\n curfiles_grouped = sort_group(curfiles, True)\n\n # -- Extract first group of equivalent filepaths (this allows us to process with the same alphabetical order on all platforms)\n # Note that the remaining files in other groups will be processed later, because their alphabetical order is higher to the first group, this means that the first group is to be processed now\n to_process = curfiles_grouped[0]\n #print to_process # debug\n\n # -- Byte-by-byte majority vote on the first group of files\n # Need the relative filepath also (note that there's only one since it's a group of equivalent relative filepaths, only the absolute path is different between files of a same group)\n relfilepath = path2unix(os.path.join(*to_process[0][1]))\n if report_file: r_row[0] = relfilepath\n if verbose: ptee.write(\"- Processing file %s.\" % relfilepath)\n # Generate output path\n outpathfull = os.path.join(outpath, relfilepath)\n create_dir_if_not_exist(os.path.dirname(outpathfull))\n # Initialize the list of absolute filepaths\n fileslist = []\n for elt in to_process:\n i = elt[0]\n fileslist.append(os.path.join(inputpaths[i], os.path.join(*elt[1])))\n if report_file: r_row[i+1] = 'X' # put an X in the report file below each folder that contains this file\n # If there's only one file, just copy it over\n if len(to_process) == 1:\n shutil.copyfile(fileslist[0], outpathfull)\n id = to_process[0][0]\n if report_file: r_row[id+1] = 'O'\n # Else, merge by majority vote\n else:\n # Before-merge check using rfigc database, if provided\n # If one of the files in the input folders is already correct, just copy it over\n correct_file = None\n if database:\n for id, filepath in enumerate(fileslist):\n if rfigc.main(\"-i \\\"%s\\\" -d \\\"%s\\\" -m --silent\" % (filepath, database)) == 0:\n correct_file = filepath\n correct_id = to_process[id][0]\n break\n\n # If one correct file was found, copy it over\n if correct_file:\n create_dir_if_not_exist(os.path.dirname(outpathfull))\n shutil.copyfile(correct_file, outpathfull)\n if report_file:\n r_row[correct_id+1] = \"O\"\n r_row[-3] = \"OK\"\n # Else, we need to do the majority vote merge\n else:\n # Do the majority vote merge\n errcode, errmsg = majority_vote_byte_scan(relfilepath, fileslist, outpath)\n\n # After-merge/move check using rfigc database, if provided\n if database:\n if rfigc.main(\"-i \\\"%s\\\" -d \\\"%s\\\" -m --silent\" % (outpathfull, database)) == 1:\n errcode = 1\n r_row[-3] = \"KO\"\n if not errmsg: errmsg = ''\n errmsg += \" File could not be totally repaired according to rfigc database.\"\n else:\n if report_file:\n r_row[-3] = \"OK\"\n if errmsg: errmsg += \" But merged file is correct according to rfigc database.\"\n\n # Display errors if any\n if errcode:\n if report_file:\n r_row[-2] = \"KO\"\n r_row[-1] = errmsg\n ptee.write(errmsg)\n retcode = 1\n else:\n if report_file: r_row[-2] = \"OK\"\n\n # Save current report's row\n if report_file:\n r_writer.writerow(r_row)\n\n # -- Update files lists alignment (ie, retrieve new files but while trying to keep the alignment)\n for elt in to_process: # for files of the first group (the ones we processed)\n i = elt[0]\n # Walk their respective folders and load up the next file\n try:\n if not recgen_exhausted.get(i, False):\n curfiles[i] = relpath_posix(recgen[i].next(), inputpaths[i])[1]\n # If there's no file left in this folder, mark this input folder as exhausted and continue with the others\n except StopIteration:\n curfiles[i] = None\n recgen_exhausted[i] = True\n recgen_exhausted_count += 1\n if tqdm_bar: tqdm_bar.update()\n if tqdm_bar: tqdm_bar.close()\n\n # Closing report file\n if report_file:\n # Write list of directories and legend\n rfile.write(\"\\n=> Input directories:\")\n for id, ipath in enumerate(inputpaths):\n rfile.write(\"\\n\\t- dir%i = %s\" % ((id+1), ipath))\n rfile.write(\"\\n=> Output directory: %s\" % outpath)\n rfile.write(\"\\n=> Legend: X=existing/selected for majority vote, O=only used this file, - = not existing, OK = check correct, KO = check incorrect (file was not recovered)\\n\")\n # Close the report file handle\n rfile.close()\n\n return retcode", "response": "This function is used to synchronize files contents by majority. It will recursively walk through the input folders and align the files with the same filepath."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a tree of referrers of the root object.", "response": "def get_tree(self):\n \"\"\"Get a tree of referrers of the root object.\"\"\"\n self.ignore.append(inspect.currentframe())\n return self._get_tree(self.root, self.maxdepth)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef print_tree(self, tree=None):\n if tree is None:\n self._print(self.root, '', '')\n else:\n self._print(tree, '', '')", "response": "Print referrers tree to console."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncomputes and print a new line of the tree.", "response": "def _print(self, tree, prefix, carryon):\n \"\"\"Compute and print a new line of the tree.\n\n This is a recursive function.\n\n arguments\n tree -- tree to print\n prefix -- prefix to the current line to print\n carryon -- prefix which is used to carry on the vertical lines\n\n \"\"\"\n level = prefix.count(self.cross) + prefix.count(self.vline)\n len_children = 0\n if isinstance(tree , _Node):\n len_children = len(tree.children)\n\n # add vertex\n prefix += str(tree)\n # and as many spaces as the vertex is long\n carryon += self.space * len(str(tree))\n if (level == self.maxdepth) or (not isinstance(tree, _Node)) or\\\n (len_children == 0):\n self.stream.write(prefix+'\\n')\n return\n else:\n # add in between connections\n prefix += self.hline\n carryon += self.space\n # if there is more than one branch, add a cross\n if len(tree.children) > 1:\n prefix += self.cross\n carryon += self.vline\n prefix += self.hline\n carryon += self.space\n\n if len_children > 0:\n # print the first branch (on the same line)\n self._print(tree.children[0], prefix, carryon)\n for b in range(1, len_children):\n # the caryon becomes the prefix for all following children\n prefix = carryon[:-2] + self.cross + self.hline\n # remove the vlines for any children of last branch\n if b == (len_children-1):\n carryon = carryon[:-2] + 2*self.space\n self._print(tree.children[b], prefix, carryon)\n # leave a free line before the next branch\n if b == (len_children-1):\n if len(carryon.strip(' ')) == 0:\n return\n self.stream.write(carryon[:-2].rstrip()+'\\n')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef print_tree(self, filename, tree=None):\n old_stream = self.stream\n self.stream = open(filename, 'w')\n try:\n super(FileBrowser, self).print_tree(tree=tree)\n finally:\n self.stream.close()\n self.stream = old_stream", "response": "Print referrers tree to file."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate interactive browser window. keyword arguments standalone -- Set to true", "response": "def main(self, standalone=False):\n \"\"\"Create interactive browser window.\n\n keyword arguments\n standalone -- Set to true, if the browser is not attached to other\n windows\n\n \"\"\"\n window = _Tkinter.Tk()\n sc = _TreeWidget.ScrolledCanvas(window, bg=\"white\",\\\n highlightthickness=0, takefocus=1)\n sc.frame.pack(expand=1, fill=\"both\")\n item = _ReferrerTreeItem(window, self.get_tree(), self)\n node = _TreeNode(sc.canvas, None, item)\n node.expand()\n if standalone:\n window.mainloop()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the value of the DropDown option in the order of the argument.", "response": "def getValue(self):\n '''\n Returns\n str(option_string * DropDown Value)\n e.g.\n -vvvvv\n '''\n dropdown_value = self.widget.GetValue()\n if not str(dropdown_value).isdigit():\n return ''\n arg = str(self.option_string).replace('-', '')\n repeated_args = arg * int(dropdown_value)\n return '-' + repeated_args"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef format_meter(n, total, elapsed, ncols=None, prefix='',\n unit=None, unit_scale=False, ascii=False):\n \"\"\"\n Return a string-based progress bar given some parameters\n\n Parameters\n ----------\n n : int\n Number of finished iterations.\n total : int\n The expected total number of iterations. If None, only basic progress\n statistics are displayed (no ETA).\n elapsed : float\n Number of seconds passed since start.\n ncols : int, optional\n The width of the entire output message. If sepcified, dynamically\n resizes the progress meter [default: None]. The fallback meter\n width is 10.\n prefix : str, optional\n Prefix message (included in total width).\n unit : str, optional\n String that will be used to define the unit of each iteration.\n [default: \"it\"]\n unit_scale : bool, optional\n If set, the number of iterations will be reduced/scaled\n automatically and a metric prefix following the\n International System of Units standard will be added\n (kilo, mega, etc.). [default: False]\n ascii : bool, optional\n If not set, use unicode (smooth blocks) to fill the meter\n [default: False]. The fallback is to use ASCII characters (1-9 #).\n\n Returns\n -------\n out : Formatted meter and stats, ready to display.\n \"\"\"\n\n # in case the total is wrong (n is above the total), then\n # we switch to the mode without showing the total prediction\n # (since ETA would be wrong anyway)\n if total and n > total:\n total = None\n\n elapsed_str = format_interval(elapsed)\n if elapsed:\n if unit_scale:\n rate = format_sizeof(n / elapsed, suffix='')\n else:\n rate = '{0:5.2f}'.format(n / elapsed)\n else:\n rate = '?'\n\n rate_unit = unit if unit else 'it'\n if not unit:\n unit = ''\n\n n_fmt = str(n)\n total_fmt = str(total)\n if unit_scale:\n n_fmt = format_sizeof(n, suffix='')\n if total:\n total_fmt = format_sizeof(total, suffix='')\n\n if total:\n frac = n / total\n percentage = frac * 100\n\n remaining_str = format_interval(elapsed * (total-n) / n) if n else '?'\n\n l_bar = '{1}{0:.0f}%|'.format(percentage, prefix) if prefix else \\\n '{0:3.0f}%|'.format(percentage)\n r_bar = '| {0}/{1}{2} [{3}<{4}, {5} {6}/s]'.format(\n n_fmt, total_fmt, unit, elapsed_str, remaining_str,\n rate, rate_unit)\n\n if ncols == 0:\n bar = ''\n else:\n N_BARS = max(1, ncols - len(l_bar) - len(r_bar)) if ncols \\\n else 10\n\n if ascii:\n bar_length, frac_bar_length = divmod(\n int(frac * N_BARS * 10), 10)\n\n bar = '#'*bar_length\n frac_bar = chr(48 + frac_bar_length) if frac_bar_length \\\n else ' '\n\n else:\n bar_length, frac_bar_length = divmod(int(frac * N_BARS * 8), 8)\n\n bar = _unich(0x2588)*bar_length\n frac_bar = _unich(0x2590 - frac_bar_length) \\\n if frac_bar_length else ' '\n\n if bar_length < N_BARS:\n full_bar = bar + frac_bar + \\\n ' ' * max(N_BARS - bar_length - 1, 0) # spacing\n else:\n full_bar = bar + \\\n ' ' * max(N_BARS - bar_length, 0) # spacing\n\n return l_bar + full_bar + r_bar\n\n else: # no progressbar nor ETA, just progress statistics\n return '{0}{1} [{2}, {3} {4}/s]'.format(\n n_fmt, unit, elapsed_str, rate, rate_unit)", "response": "Returns a string - based progress bar given some parameters."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef getIcon( data ):\n import cStringIO\n stream = cStringIO.StringIO(data)\n image = wx.ImageFromStream(stream)\n icon = wx.EmptyIcon()\n icon.CopyFromBitmap(wx.BitmapFromImage(image))\n return icon", "response": "Return the data from the resource as a wxIcon"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef main():\n logging.basicConfig(level=logging.INFO)\n app = RunSnakeRunApp(0)\n app.MainLoop()", "response": "Main loop for the application"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef CreateControls(self, config_parser):\n self.CreateMenuBar()\n self.SetupToolBar()\n self.CreateStatusBar()\n self.leftSplitter = wx.SplitterWindow(\n self\n )\n self.rightSplitter = wx.SplitterWindow(\n self.leftSplitter\n )\n self.listControl = listviews.DataView(\n self.leftSplitter,\n columns = PROFILE_VIEW_COLUMNS,\n name='mainlist',\n )\n self.squareMap = squaremap.SquareMap(\n self.rightSplitter,\n padding = 6,\n labels = True,\n adapter = self.adapter,\n square_style = True,\n )\n self.tabs = wx.Notebook(\n self.rightSplitter,\n )\n\n self.CreateSourceWindow(self.tabs)\n \n self.calleeListControl = listviews.DataView(\n self.tabs,\n columns = PROFILE_VIEW_COLUMNS,\n name='callee',\n )\n self.allCalleeListControl = listviews.DataView(\n self.tabs,\n columns = PROFILE_VIEW_COLUMNS,\n name='allcallee',\n )\n self.allCallerListControl = listviews.DataView(\n self.tabs,\n columns = PROFILE_VIEW_COLUMNS,\n name='allcaller',\n )\n self.callerListControl = listviews.DataView(\n self.tabs,\n columns = PROFILE_VIEW_COLUMNS,\n name='caller',\n )\n self.ProfileListControls = [\n self.listControl,\n self.calleeListControl,\n self.allCalleeListControl,\n self.callerListControl,\n self.allCallerListControl,\n ]\n self.tabs.AddPage(self.calleeListControl, _('Callees'), True)\n self.tabs.AddPage(self.allCalleeListControl, _('All Callees'), False)\n self.tabs.AddPage(self.callerListControl, _('Callers'), False)\n self.tabs.AddPage(self.allCallerListControl, _('All Callers'), False)\n if editor:\n self.tabs.AddPage(self.sourceCodeControl, _('Source Code'), False)\n self.rightSplitter.SetSashSize(10)\n # calculate size as proportional value for initial display...\n self.LoadState( config_parser )\n width, height = self.GetSizeTuple()\n rightsplit = 2 * (height // 3)\n leftsplit = width // 3\n self.rightSplitter.SplitHorizontally(self.squareMap, self.tabs,\n rightsplit)\n self.leftSplitter.SplitVertically(self.listControl, self.rightSplitter,\n leftsplit)\n squaremap.EVT_SQUARE_HIGHLIGHTED(self.squareMap,\n self.OnSquareHighlightedMap)\n squaremap.EVT_SQUARE_SELECTED(self.listControl,\n self.OnSquareSelectedList)\n squaremap.EVT_SQUARE_SELECTED(self.squareMap, self.OnSquareSelectedMap)\n squaremap.EVT_SQUARE_ACTIVATED(self.squareMap, self.OnNodeActivated)\n for control in self.ProfileListControls:\n squaremap.EVT_SQUARE_ACTIVATED(control, self.OnNodeActivated)\n squaremap.EVT_SQUARE_HIGHLIGHTED(control,\n self.OnSquareHighlightedList)\n self.moreSquareViewItem.Check(self.squareMap.square_style)", "response": "Create our control classes"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate our menu - bar for triggering operations", "response": "def CreateMenuBar(self):\n \"\"\"Create our menu-bar for triggering operations\"\"\"\n menubar = wx.MenuBar()\n menu = wx.Menu()\n menu.Append(ID_OPEN, _('&Open Profile'), _('Open a cProfile file'))\n menu.Append(ID_OPEN_MEMORY, _('Open &Memory'), _('Open a Meliae memory-dump file'))\n menu.AppendSeparator()\n menu.Append(ID_EXIT, _('&Close'), _('Close this RunSnakeRun window'))\n menubar.Append(menu, _('&File'))\n menu = wx.Menu()\n# self.packageMenuItem = menu.AppendCheckItem(\n# ID_PACKAGE_VIEW, _('&File View'),\n# _('View time spent by package/module')\n# )\n self.percentageMenuItem = menu.AppendCheckItem(\n ID_PERCENTAGE_VIEW, _('&Percentage View'),\n _('View time spent as percent of overall time')\n )\n self.rootViewItem = menu.Append(\n ID_ROOT_VIEW, _('&Root View (Home)'),\n _('View the root of the tree')\n )\n self.backViewItem = menu.Append(\n ID_BACK_VIEW, _('&Back'), _('Go back in your viewing history')\n )\n self.upViewItem = menu.Append(\n ID_UP_VIEW, _('&Up'),\n _('Go \"up\" to the parent of this node with the largest cumulative total')\n )\n self.moreSquareViewItem = menu.AppendCheckItem(\n ID_MORE_SQUARE, _('&Hierarchic Squares'),\n _('Toggle hierarchic squares in the square-map view')\n )\n\n # This stuff isn't really all that useful for profiling,\n # it's more about how to generate graphics to describe profiling...\n self.deeperViewItem = menu.Append(\n ID_DEEPER_VIEW, _('&Deeper'), _('View deeper squaremap views')\n )\n self.shallowerViewItem = menu.Append(\n ID_SHALLOWER_VIEW, _('&Shallower'), _('View shallower squaremap views')\n )\n# wx.ToolTip.Enable(True)\n menubar.Append(menu, _('&View'))\n \n self.viewTypeMenu =wx.Menu( )\n menubar.Append(self.viewTypeMenu, _('View &Type'))\n \n self.SetMenuBar(menubar)\n\n wx.EVT_MENU(self, ID_EXIT, lambda evt: self.Close(True))\n wx.EVT_MENU(self, ID_OPEN, self.OnOpenFile)\n wx.EVT_MENU(self, ID_OPEN_MEMORY, self.OnOpenMemory)\n \n wx.EVT_MENU(self, ID_PERCENTAGE_VIEW, self.OnPercentageView)\n wx.EVT_MENU(self, ID_UP_VIEW, self.OnUpView)\n wx.EVT_MENU(self, ID_DEEPER_VIEW, self.OnDeeperView)\n wx.EVT_MENU(self, ID_SHALLOWER_VIEW, self.OnShallowerView)\n wx.EVT_MENU(self, ID_ROOT_VIEW, self.OnRootView)\n wx.EVT_MENU(self, ID_BACK_VIEW, self.OnBackView)\n wx.EVT_MENU(self, ID_MORE_SQUARE, self.OnMoreSquareToggle)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef CreateSourceWindow(self, tabs):\n if editor and self.sourceCodeControl is None:\n self.sourceCodeControl = wx.py.editwindow.EditWindow(\n self.tabs, -1\n )\n self.sourceCodeControl.SetText(u\"\")\n self.sourceFileShown = None\n self.sourceCodeControl.setDisplayLineNumbers(True)", "response": "Create our source - view window for tabs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating the toolbar for common actions", "response": "def SetupToolBar(self):\n \"\"\"Create the toolbar for common actions\"\"\"\n tb = self.CreateToolBar(self.TBFLAGS)\n tsize = (24, 24)\n tb.ToolBitmapSize = tsize\n open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR,\n tsize)\n tb.AddLabelTool(ID_OPEN, \"Open\", open_bmp, shortHelp=\"Open\",\n longHelp=\"Open a (c)Profile trace file\")\n if not osx:\n tb.AddSeparator()\n# self.Bind(wx.EVT_TOOL, self.OnOpenFile, id=ID_OPEN)\n self.rootViewTool = tb.AddLabelTool(\n ID_ROOT_VIEW, _(\"Root View\"),\n wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR, tsize),\n shortHelp=_(\"Display the root of the current view tree (home view)\")\n )\n self.rootViewTool = tb.AddLabelTool(\n ID_BACK_VIEW, _(\"Back\"),\n wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR, tsize),\n shortHelp=_(\"Back to the previously activated node in the call tree\")\n )\n self.upViewTool = tb.AddLabelTool(\n ID_UP_VIEW, _(\"Up\"),\n wx.ArtProvider.GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR, tsize),\n shortHelp=_(\"Go one level up the call tree (highest-percentage parent)\")\n )\n if not osx:\n tb.AddSeparator()\n # TODO: figure out why the control is sizing the label incorrectly on Linux\n self.percentageViewTool = wx.CheckBox(tb, -1, _(\"Percent \"))\n self.percentageViewTool.SetToolTip(wx.ToolTip(\n _(\"Toggle display of percentages in list views\")))\n tb.AddControl(self.percentageViewTool)\n wx.EVT_CHECKBOX(self.percentageViewTool,\n self.percentageViewTool.GetId(), self.OnPercentageView)\n\n self.viewTypeTool= wx.Choice( tb, -1, choices= getattr(self.loader,'ROOTS',[]) )\n self.viewTypeTool.SetToolTip(wx.ToolTip(\n _(\"Switch between different hierarchic views of the data\")))\n wx.EVT_CHOICE( self.viewTypeTool, self.viewTypeTool.GetId(), self.OnViewTypeTool )\n tb.AddControl( self.viewTypeTool )\n tb.Realize()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef ConfigureViewTypeChoices( self, event=None ):\n self.viewTypeTool.SetItems( getattr( self.loader, 'ROOTS', [] ))\n if self.loader and self.viewType in self.loader.ROOTS:\n self.viewTypeTool.SetSelection( self.loader.ROOTS.index( self.viewType ))\n \n # configure the menu with the available choices...\n def chooser( typ ):\n def Callback( event ):\n if typ != self.viewType:\n self.viewType = typ \n self.OnRootView( event )\n return Callback\n # Clear all previous items\n for item in self.viewTypeMenu.GetMenuItems():\n self.viewTypeMenu.DeleteItem( item )\n if self.loader and self.loader.ROOTS:\n for root in self.loader.ROOTS:\n item = wx.MenuItem( \n self.viewTypeMenu, -1, root.title(), \n _(\"View hierarchy by %(name)s\")%{\n 'name': root.title(),\n },\n kind=wx.ITEM_RADIO,\n )\n item.SetCheckable( True )\n self.viewTypeMenu.AppendItem( item )\n item.Check( root == self.viewType )\n wx.EVT_MENU( self, item.GetId(), chooser( root ))", "response": "Configure the set of view types in the toolbar and menus"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrequesting to open a new profile file", "response": "def OnOpenFile(self, event):\n \"\"\"Request to open a new profile file\"\"\"\n dialog = wx.FileDialog(self, style=wx.OPEN|wx.FD_MULTIPLE)\n if dialog.ShowModal() == wx.ID_OK:\n paths = dialog.GetPaths()\n if self.loader:\n # we've already got a displayed data-set, open new window...\n frame = MainFrame()\n frame.Show(True)\n frame.load(*paths)\n else:\n self.load(*paths)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef OnOpenMemory(self, event):\n dialog = wx.FileDialog(self, style=wx.OPEN)\n if dialog.ShowModal() == wx.ID_OK:\n path = dialog.GetPath()\n if self.loader:\n # we've already got a displayed data-set, open new window...\n frame = MainFrame()\n frame.Show(True)\n frame.load_memory(path)\n else:\n self.load_memory(path)", "response": "Request to open a new profile file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef SetPackageView(self, directoryView):\n self.directoryView = not self.directoryView\n self.packageMenuItem.Check(self.directoryView)\n self.packageViewTool.SetValue(self.directoryView)\n if self.loader:\n self.SetModel(self.loader)\n self.RecordHistory()", "response": "Sets whether to use directory based view"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting whether to display percentage or absolute values", "response": "def SetPercentageView(self, percentageView):\n \"\"\"Set whether to display percentage or absolute values\"\"\"\n self.percentageView = percentageView\n self.percentageMenuItem.Check(self.percentageView)\n self.percentageViewTool.SetValue(self.percentageView)\n total = self.adapter.value( self.loader.get_root( self.viewType ) )\n for control in self.ProfileListControls:\n control.SetPercentage(self.percentageView, total)\n self.adapter.SetPercentage(self.percentageView, total)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrequesting to move up the hierarchy to highest - weight parent", "response": "def OnUpView(self, event):\n \"\"\"Request to move up the hierarchy to highest-weight parent\"\"\"\n node = self.activated_node\n parents = []\n selected_parent = None\n \n if node:\n if hasattr( self.adapter, 'best_parent' ):\n selected_parent = self.adapter.best_parent( node )\n else:\n parents = self.adapter.parents( node )\n if parents:\n if not selected_parent:\n parents.sort(key = lambda a: self.adapter.value(node, a))\n selected_parent = parents[-1]\n class event:\n node = selected_parent\n self.OnNodeActivated(event)\n else:\n self.SetStatusText(_('No parents for the currently selected node: %(node_name)s')\n % dict(node_name=self.adapter.label(node)))\n else:\n self.SetStatusText(_('No currently selected node'))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nbacks view event handler", "response": "def OnBackView(self, event):\n \"\"\"Request to move backward in the history\"\"\"\n self.historyIndex -= 1\n try:\n self.RestoreHistory(self.history[self.historyIndex])\n except IndexError, err:\n self.SetStatusText(_('No further history available'))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef OnRootView(self, event):\n self.adapter, tree, rows = self.RootNode()\n self.squareMap.SetModel(tree, self.adapter)\n self.RecordHistory()\n self.ConfigureViewTypeChoices()", "response": "Reset view to the root of the tree"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef OnNodeActivated(self, event):\n self.activated_node = self.selected_node = event.node\n self.squareMap.SetModel(event.node, self.adapter)\n self.squareMap.SetSelected( event.node )\n if editor:\n if self.SourceShowFile(event.node):\n if hasattr(event.node,'lineno'):\n self.sourceCodeControl.GotoLine(event.node.lineno)\n self.RecordHistory()", "response": "Double - click or enter on a node in some control..."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow the given file in the source - code view ( attempt it anyway", "response": "def SourceShowFile(self, node):\n \"\"\"Show the given file in the source-code view (attempt it anyway)\"\"\"\n filename = self.adapter.filename( node )\n if filename and self.sourceFileShown != filename:\n try:\n data = open(filename).read()\n except Exception, err:\n # TODO: load from zips/eggs? What about .pyc issues?\n return None\n else:\n #self.sourceCodeControl.setText(data)\n self.sourceCodeControl.ClearAll()\n self.sourceCodeControl.AppendText( data )\n return filename"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef OnSquareSelected(self, event):\n self.selected_node = event.node\n self.calleeListControl.integrateRecords(self.adapter.children( event.node) )\n self.callerListControl.integrateRecords(self.adapter.parents( event.node) )", "response": "Square selected event handler"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef OnMoreSquareToggle( self, event ):\n self.squareMap.square_style = not self.squareMap.square_style\n self.squareMap.Refresh()\n self.moreSquareViewItem.Check(self.squareMap.square_style)", "response": "More Square toggle event handler"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef RecordHistory(self):\n if not self.restoringHistory:\n record = self.activated_node\n if self.historyIndex < -1:\n try:\n del self.history[self.historyIndex+1:]\n except AttributeError, err:\n pass\n if (not self.history) or record != self.history[-1]:\n self.history.append(record)\n del self.history[:-200]\n self.historyIndex = -1", "response": "Record the given node in the history - set."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef load(self, *filenames):\n if len(filenames) == 1:\n if os.path.basename( filenames[0] ) == 'index.coldshot':\n return self.load_coldshot( os.path.dirname( filenames[0]) )\n elif os.path.isdir( filenames[0] ):\n return self.load_coldshot( filenames[0] )\n try:\n self.loader = pstatsloader.PStatsLoader(*filenames)\n self.ConfigureViewTypeChoices()\n self.SetModel( self.loader )\n self.viewType = self.loader.ROOTS[0]\n self.SetTitle(_(\"Run Snake Run: %(filenames)s\")\n % {'filenames': ', '.join(filenames)[:120]})\n except (IOError, OSError, ValueError,MemoryError), err:\n self.SetStatusText(\n _('Failure during load of %(filenames)s: %(err)s'\n ) % dict(\n filenames=\" \".join([repr(x) for x in filenames]),\n err=err\n ))", "response": "Load our dataset (iteratively)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef SetModel(self, loader):\n self.loader = loader\n self.adapter, tree, rows = self.RootNode()\n self.listControl.integrateRecords(rows.values())\n self.activated_node = tree\n self.squareMap.SetModel(tree, self.adapter)\n self.RecordHistory()", "response": "Set our overall model and populate sub - controls"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn our current root node and appropriate adapter for it", "response": "def RootNode(self):\n \"\"\"Return our current root node and appropriate adapter for it\"\"\"\n tree = self.loader.get_root( self.viewType )\n adapter = self.loader.get_adapter( self.viewType )\n rows = self.loader.get_rows( self.viewType )\n \n adapter.SetPercentage(self.percentageView, adapter.value( tree ))\n \n return adapter, tree, rows"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving window state to be restored on the next run...", "response": "def SaveState( self, config_parser ):\n \"\"\"Retrieve window state to be restored on the next run...\"\"\"\n if not config_parser.has_section( 'window' ):\n config_parser.add_section( 'window' )\n if self.IsMaximized():\n config_parser.set( 'window', 'maximized', str(True))\n else:\n config_parser.set( 'window', 'maximized', str(False))\n size = self.GetSizeTuple()\n position = self.GetPositionTuple()\n config_parser.set( 'window', 'width', str(size[0]) )\n config_parser.set( 'window', 'height', str(size[1]) )\n config_parser.set( 'window', 'x', str(position[0]) )\n config_parser.set( 'window', 'y', str(position[1]) )\n \n for control in self.ProfileListControls:\n control.SaveState( config_parser )\n\n return config_parser"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef LoadState( self, config_parser ):\n if not config_parser:\n return\n if (\n not config_parser.has_section( 'window' ) or (\n config_parser.has_option( 'window','maximized' ) and \n config_parser.getboolean( 'window', 'maximized' )\n )\n ):\n self.Maximize(True)\n try:\n width,height,x,y = [\n config_parser.getint( 'window',key )\n for key in ['width','height','x','y']\n ]\n self.SetPosition( (x,y))\n self.SetSize( (width,height))\n except ConfigParser.NoSectionError, err:\n # the file isn't written yet, so don't even warn...\n pass\n except Exception, err:\n # this is just convenience, if it breaks in *any* way, ignore it...\n log.error(\n \"Unable to load window preferences, ignoring: %s\", traceback.format_exc()\n )\n\n try:\n font_size = config_parser.getint('window', 'font_size')\n except Exception:\n pass # use the default, by default\n else:\n font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)\n font.SetPointSize(font_size)\n for ctrl in self.ProfileListControls:\n ctrl.SetFont(font)\n \n for control in self.ProfileListControls:\n control.LoadState( config_parser )\n \n self.config = config_parser\n wx.EVT_CLOSE( self, self.OnCloseWindow )", "response": "Load our window state from the given config_parser instance"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncompute the edit distance between two sequences and return it.", "response": "def fast_comp(seq1, seq2, transpositions=False):\n\t\"\"\"Compute the distance between the two sequences `seq1` and `seq2` up to a\n\tmaximum of 2 included, and return it. If the edit distance between the two\n\tsequences is higher than that, -1 is returned.\n\t\n\tIf `transpositions` is `True`, transpositions will be taken into account for\n\tthe computation of the distance. This can make a difference, e.g.:\n\n\t\t>>> fast_comp(\"abc\", \"bac\", transpositions=False)\n\t\t2\n\t\t>>> fast_comp(\"abc\", \"bac\", transpositions=True)\n\t\t1\n\t\n\tThis is faster than `levenshtein` by an order of magnitude, but on the\n\tother hand is of limited use.\n\n\tThe algorithm comes from `http://writingarchives.sakura.ne.jp/fastcomp`.\n\tI've added transpositions support to the original code.\n\t\"\"\"\n\treplace, insert, delete = \"r\", \"i\", \"d\"\n\n\tL1, L2 = len(seq1), len(seq2)\n\tif L1 < L2:\n\t\tL1, L2 = L2, L1\n\t\tseq1, seq2 = seq2, seq1\n\n\tldiff = L1 - L2\n\tif ldiff == 0:\n\t\tmodels = (insert+delete, delete+insert, replace+replace)\n\telif ldiff == 1:\n\t\tmodels = (delete+replace, replace+delete)\n\telif ldiff == 2:\n\t\tmodels = (delete+delete,)\n\telse:\n\t\treturn -1\n\n\tres = 3\n\tfor model in models:\n\t\ti = j = c = 0\n\t\twhile (i < L1) and (j < L2):\n\t\t\tif seq1[i] != seq2[j]:\n\t\t\t\tc = c+1\n\t\t\t\tif 2 < c:\n\t\t\t\t\tbreak\n \n\t\t\t\tif transpositions and ldiff != 2 \\\n \tand i < L1 - 1 and j < L2 - 1 \\\n \tand seq1[i+1] == seq2[j] and seq1[i] == seq2[j+1]:\n\t\t\t\t\ti, j = i+2, j+2\n\t\t\t\telse:\n\t\t\t\t\tcmd = model[c-1]\n\t\t\t\t\tif cmd == delete:\n\t\t\t\t\t\ti = i+1\n\t\t\t\t\telif cmd == insert:\n\t\t\t\t\t\tj = j+1\n\t\t\t\t\telse:\n\t\t\t\t\t\tassert cmd == replace\n\t\t\t\t\t\ti,j = i+1, j+1\n\t\t\telse:\n\t\t\t\ti,j = i+1, j+1\n\n\t\tif 2 < c:\n\t\t\tcontinue\n\t\telif i < L1:\n\t\t\tif L1-i <= model[c:].count(delete):\n\t\t\t\tc = c + (L1-i)\n\t\t\telse:\n\t\t\t\tcontinue\n\t\telif j < L2:\n\t\t\tif L2-j <= model[c:].count(insert):\n\t\t\t\tc = c + (L2-j)\n\t\t\telse:\n\t\t\t\tcontinue\n\n\t\tif c < res:\n\t\t\tres = c\n\n\tif res == 3:\n\t\tres = -1\n\treturn res"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncheck if a path is an actual file that exists'''", "response": "def is_file(dirname):\n '''Checks if a path is an actual file that exists'''\n if not os.path.isfile(dirname):\n msg = \"{0} is not an existing file\".format(dirname)\n raise argparse.ArgumentTypeError(msg)\n else:\n return dirname"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_dir(dirname):\n '''Checks if a path is an actual directory that exists'''\n if not os.path.isdir(dirname):\n msg = \"{0} is not a directory\".format(dirname)\n raise argparse.ArgumentTypeError(msg)\n else:\n return dirname", "response": "Checks if a path is an actual directory that exists'''"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck if a path is an actual directory that exists or a file", "response": "def is_dir_or_file(dirname):\n '''Checks if a path is an actual directory that exists or a file'''\n if not os.path.isdir(dirname) and not os.path.isfile(dirname):\n msg = \"{0} is not a directory nor a file\".format(dirname)\n raise argparse.ArgumentTypeError(msg)\n else:\n return dirname"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef fullpath(relpath):\n '''Relative path to absolute'''\n if (type(relpath) is object or type(relpath) is file):\n relpath = relpath.name\n return os.path.abspath(os.path.expanduser(relpath))", "response": "Relative path to absolute"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef recwalk(inputpath, sorting=True):\n '''Recursively walk through a folder. This provides a mean to flatten out the files restitution (necessary to show a progress bar). This is a generator.'''\n # If it's only a single file, return this single file\n if os.path.isfile(inputpath):\n abs_path = fullpath(inputpath)\n yield os.path.dirname(abs_path), os.path.basename(abs_path)\n # Else if it's a folder, walk recursively and return every files\n else:\n for dirpath, dirs, files in walk(inputpath):\t\n if sorting:\n files.sort()\n dirs.sort() # sort directories in-place for ordered recursive walking\n for filename in files:\n yield (dirpath, filename)", "response": "Recursively walk through a folder and return all files and directories in the folder."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a path given in any format converts to posix path format fromwinpath forces the input path to be recognized as a Windows path", "response": "def path2unix(path, nojoin=False, fromwinpath=False):\n '''From a path given in any format, converts to posix path format\n fromwinpath=True forces the input path to be recognized as a Windows path (useful on Unix machines to unit test Windows paths)'''\n if fromwinpath:\n pathparts = list(PureWindowsPath(path).parts)\n else:\n pathparts = list(PurePath(path).parts)\n if nojoin:\n return pathparts\n else:\n return posixpath.join(*pathparts)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_next_entry(file, entrymarker=\"\\xFE\\xFF\\xFE\\xFF\\xFE\\xFF\\xFE\\xFF\\xFE\\xFF\", only_coord=True, blocksize=65535):\n '''Find or read the next ecc entry in a given ecc file.\n Call this function multiple times with the same file handle to get subsequent markers positions (this is not a generator but it works very similarly, because it will continue reading from the file's current cursor position -- this can be used advantageously if you want to read only a specific entry by seeking before supplying the file handle).\n This will read any string length between two entrymarkers.\n The reading is very tolerant, so it will always return any valid entry (but also scrambled entries if any, but the decoding will ensure everything's ok).\n `file` is a file handle, not the path to the file.'''\n found = False\n start = None # start and end vars are the relative position of the starting/ending entrymarkers in the current buffer\n end = None\n startcursor = None # startcursor and endcursor are the absolute position of the starting/ending entrymarkers inside the database file\n endcursor = None\n buf = 1\n # Sanity check: cannot screen the file's content if the window is of the same size as the pattern to match (the marker)\n if blocksize <= len(entrymarker): blocksize = len(entrymarker) + 1\n # Continue the search as long as we did not find at least one starting marker and one ending marker (or end of file)\n while (not found and buf):\n # Read a long block at once, we will readjust the file cursor after\n buf = file.read(blocksize)\n # Find the start marker (if not found already)\n if start is None or start == -1:\n start = buf.find(entrymarker); # relative position of the starting marker in the currently read string\n if start >= 0 and not startcursor: # assign startcursor only if it's empty (meaning that we did not find the starting entrymarker, else if found we are only looking for \n startcursor = file.tell() - len(buf) + start # absolute position of the starting marker in the file\n if start >= 0: start = start + len(entrymarker)\n # If we have a starting marker, we try to find a subsequent marker which will be the ending of our entry (if the entry is corrupted we don't care: it won't pass the entry_to_dict() decoding or subsequent steps of decoding and we will just pass to the next ecc entry). This allows to process any valid entry, no matter if previous ones were scrambled.\n if startcursor is not None and startcursor >= 0:\n end = buf.find(entrymarker, start)\n if end < 0 and len(buf) < blocksize: # Special case: we didn't find any ending marker but we reached the end of file, then we are probably in fact just reading the last entry (thus there's no ending marker for this entry)\n end = len(buf) # It's ok, we have our entry, the ending marker is just the end of file\n # If we found an ending marker (or if end of file is reached), then we compute the absolute cursor value and put the file reading cursor back in position, just before the next entry (where the ending marker is if any)\n if end >= 0:\n endcursor = file.tell() - len(buf) + end\n # Make sure we are not redetecting the same marker as the start marker\n if endcursor > startcursor:\n file.seek(endcursor)\n found = True\n else:\n end = -1\n encursor = None\n #print(\"Start:\", start, startcursor)\n #print(\"End: \", end, endcursor)\n # Stop criterion to avoid infinite loop: in the case we could not find any entry in the rest of the file and we reached the EOF, we just quit now\n if len(buf) < blocksize: break\n # Did not find the full entry in one buffer? Reinit variables for next iteration, but keep in memory startcursor.\n if start > 0: start = 0 # reset the start position for the end buf find at next iteration (ie: in the arithmetic operations to compute the absolute endcursor position, the start entrymarker won't be accounted because it was discovered in a previous buffer).\n if not endcursor: file.seek(file.tell()-len(entrymarker)) # Try to fix edge case where blocksize stops the buffer exactly in the middle of the ending entrymarker. The starting marker should always be ok because it should be quite close (or generally immediately after) the previous entry, but the end depends on the end of the current entry (size of the original file), thus the buffer may miss the ending entrymarker. should offset file.seek(-len(entrymarker)) before searching for ending.\n\n if found: # if an entry was found, we seek to the beginning of the entry and then either read the entry from file or just return the markers positions (aka the entry bounds)\n file.seek(startcursor + len(entrymarker))\n if only_coord:\n # Return only coordinates of the start and end markers\n # Note: it is useful to just return the reading positions and not the entry itself because it can get quite huge and may overflow memory, thus we will read each ecc blocks on request using a generator.\n return [startcursor + len(entrymarker), endcursor]\n else:\n # Return the full entry's content\n return file.read(endcursor - startcursor - len(entrymarker))\n else:\n # Nothing found (or no new entry to find, we've already found them all), so we return None\n return None", "response": "Find or read the next entry in a given ecc file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef remove_if_exist(path): # pragma: no cover\n if os.path.exists(path):\n if os.path.isdir(path):\n shutil.rmtree(path)\n return True\n elif os.path.isfile(path):\n os.remove(path)\n return True\n return False", "response": "Delete a file or a directory recursively if it exists else no exception is raised"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncopy a file or a directory tree deleting the destination before processing", "response": "def copy_any(src, dst, only_missing=False): # pragma: no cover\n \"\"\"Copy a file or a directory tree, deleting the destination before processing\"\"\"\n if not only_missing:\n remove_if_exist(dst)\n if os.path.exists(src):\n if os.path.isdir(src):\n if not only_missing:\n shutil.copytree(src, dst, symlinks=False, ignore=None)\n else:\n for dirpath, filepath in recwalk(src):\n srcfile = os.path.join(dirpath, filepath)\n relpath = os.path.relpath(srcfile, src)\n dstfile = os.path.join(dst, relpath)\n if not os.path.exists(dstfile):\n create_dir_if_not_exist(os.path.dirname(dstfile))\n shutil.copyfile(srcfile, dstfile)\n shutil.copystat(srcfile, dstfile)\n return True\n elif os.path.isfile(src) and (not only_missing or not os.path.exists(dst)):\n shutil.copyfile(src, dst)\n shutil.copystat(src, dst)\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef group_files_by_size(fileslist, multi): # pragma: no cover\n ''' Cluster files into the specified number of groups, where each groups total size is as close as possible to each other.\n\n Pseudo-code (O(n^g) time complexity):\n Input: number of groups G per cluster, list of files F with respective sizes\n - Order F by descending size\n - Until F is empty:\n - Create a cluster X\n - A = Pop first item in F\n - Put A in X[0] (X[0] is thus the first group in cluster X)\n For g in 1..len(G)-1 :\n - B = Pop first item in F\n - Put B in X[g]\n - group_size := size(B)\n If group_size != size(A):\n While group_size < size(A):\n - Find next item C in F which size(C) <= size(A) - group_size\n - Put C in X[g]\n - group_size := group_size + size(C)\n '''\n flord = OrderedDict(sorted(fileslist.items(), key=lambda x: x[1], reverse=True))\n if multi <= 1:\n fgrouped = {}\n i = 0\n for x in flord.keys():\n i += 1\n fgrouped[i] = [[x]]\n return fgrouped\n\n fgrouped = {}\n i = 0\n while flord:\n i += 1\n fgrouped[i] = []\n big_key, big_value = flord.popitem(0)\n fgrouped[i].append([big_key])\n for j in xrange(multi-1):\n cluster = []\n if not flord: break\n child_key, child_value = flord.popitem(0)\n cluster.append(child_key)\n if child_value == big_value:\n fgrouped[i].append(cluster)\n continue\n else:\n diff = big_value - child_value\n for key, value in flord.iteritems():\n if value <= diff:\n cluster.append(key)\n del flord[key]\n if value == diff:\n break\n else:\n child_value += value\n diff = big_value - child_value\n fgrouped[i].append(cluster)\n return fgrouped", "response": "Group files into the specified number of groups where each group has the same size."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a files list with sizes output a list where the files are grouped in nbgroups per cluster.", "response": "def group_files_by_size_fast(fileslist, nbgroups, mode=1): # pragma: no cover\n '''Given a files list with sizes, output a list where the files are grouped in nbgroups per cluster.\n\n Pseudo-code for algorithm in O(n log(g)) (thank's to insertion sort or binary search trees)\n See for more infos: http://cs.stackexchange.com/questions/44406/fast-algorithm-for-clustering-groups-of-elements-given-their-size-time/44614#44614\n For each file:\n - If to-fill list is empty or file.size > first-key(to-fill):\n * Create cluster c with file in first group g1\n * Add to-fill[file.size].append([c, g2], [c, g3], ..., [c, gn])\n - Else:\n * ksize = first-key(to-fill)\n * c, g = to-fill[ksize].popitem(0)\n * Add file to cluster c in group g\n * nsize = ksize - file.size\n * if nsize > 0:\n . to-fill[nsize].append([c, g])\n . sort to-fill if not an automatic ordering structure\n '''\n ftofill = SortedList()\n ftofill_pointer = {}\n fgrouped = [] # [] or {}\n ford = sorted(fileslist.iteritems(), key=lambda x: x[1])\n last_cid = -1\n while ford:\n fname, fsize = ford.pop()\n #print \"----\\n\"+fname, fsize\n #if ftofill: print \"beforebranch\", fsize, ftofill[-1]\n #print ftofill\n if not ftofill or fsize > ftofill[-1]:\n last_cid += 1\n #print \"Branch A: create cluster %i\" % last_cid\n fgrouped.append([])\n #fgrouped[last_cid] = []\n fgrouped[last_cid].append([fname])\n if mode==0:\n for g in xrange(nbgroups-1, 0, -1):\n fgrouped[last_cid].append([])\n if not fsize in ftofill_pointer:\n ftofill_pointer[fsize] = []\n ftofill_pointer[fsize].append((last_cid, g))\n ftofill.add(fsize)\n else:\n for g in xrange(1, nbgroups):\n try:\n fgname, fgsize = ford.pop()\n #print \"Added to group %i: %s %i\" % (g, fgname, fgsize)\n except IndexError:\n break\n fgrouped[last_cid].append([fgname])\n diff_size = fsize - fgsize\n if diff_size > 0:\n if not diff_size in ftofill_pointer:\n ftofill_pointer[diff_size] = []\n ftofill_pointer[diff_size].append((last_cid, g))\n ftofill.add(diff_size)\n else:\n #print \"Branch B\"\n ksize = ftofill.pop()\n c, g = ftofill_pointer[ksize].pop()\n #print \"Assign to cluster %i group %i\" % (c, g)\n fgrouped[c][g].append(fname)\n nsize = ksize - fsize\n if nsize > 0:\n if not nsize in ftofill_pointer:\n ftofill_pointer[nsize] = []\n ftofill_pointer[nsize].append((c, g))\n ftofill.add(nsize)\n return fgrouped"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef group_files_by_size_simple(fileslist, nbgroups): # pragma: no cover\n ford = sorted(fileslist.iteritems(), key=lambda x: x[1], reverse=True)\n ford = [[x[0]] for x in ford]\n return [group for group in grouper(nbgroups, ford)]", "response": "Simple and fast files grouping strategy"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef grouped_count_sizes(fileslist, fgrouped): # pragma: no cover\n '''Compute the total size per group and total number of files. Useful to check that everything is OK.'''\n fsizes = {}\n total_files = 0\n allitems = None\n if isinstance(fgrouped, dict):\n allitems = fgrouped.iteritems()\n elif isinstance(fgrouped, list):\n allitems = enumerate(fgrouped)\n for fkey, cluster in allitems:\n fsizes[fkey] = []\n for subcluster in cluster:\n tot = 0\n if subcluster is not None:\n for fname in subcluster:\n tot += fileslist[fname]\n total_files += 1\n fsizes[fkey].append(tot)\n return fsizes, total_files", "response": "Compute the total size per group and total number of files. Useful to check that everything is OK."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef GetOptions(self):\n values = [c.GetValue()\n for c in chain(*self.widgets)\n if c.GetValue() is not None]\n return ' '.join(values)", "response": "Returns the collective values from all of the available widgets in the current panel"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef GetValue(self):\n '''\n Positionals have no associated options_string,\n so only the supplied arguments are returned.\n The order is assumed to be the same as the order\n of declaration in the client code\n\n Returns\n \"argument_value\"\n '''\n self.AssertInitialization('Positional')\n if str(self._widget.GetValue()) == EMPTY:\n return None\n return self._widget.GetValue()", "response": "Returns the value of the argument."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the value of the choice argument", "response": "def GetValue(self):\n '''\n Returns\n \"--option_name argument\"\n '''\n self.AssertInitialization('Choice')\n if self._widget.GetValue() == self._DEFAULT_VALUE:\n return None\n return ' '.join(\n [self._action.option_strings[0] if self._action.option_strings else '', # get the verbose copy if available\n self._widget.GetValue()])"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the value of the option.", "response": "def GetValue(self):\n '''\n General options are key/value style pairs (conceptually).\n Thus the name of the option, as well as the argument to it\n are returned\n e.g.\n >>> myscript --outfile myfile.txt\n returns\n \"--Option Value\"\n '''\n self.AssertInitialization('Optional')\n value = self._widget.GetValue()\n if not value or len(value) <= 0:\n return None\n return ' '.join(\n [self._action.option_strings[0], # get the verbose copy if available\n value])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the value of the option.", "response": "def GetValue(self):\n '''\n Flag options have no param associated with them.\n Thus we only need the name of the option.\n e.g\n >>> Python -v myscript\n returns\n Options name for argument (-v)\n '''\n if not self._widget.GetValue() or len(self._widget.GetValue()) <= 0:\n return None\n else:\n return self._action.option_strings[0]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef Update(self, size):\n '''\n Custom wrapper calculator to account for the\n increased size of the _msg widget after being\n inlined with the wx.CheckBox\n '''\n if self._msg is None:\n return\n help_msg = self._msg\n width, height = size\n content_area = int((width / 3) * .70)\n\n wiggle_room = range(int(content_area - content_area * .05), int(content_area + content_area * .05))\n if help_msg.Size[0] not in wiggle_room:\n self._msg.SetLabel(self._msg.GetLabelText().replace('\\n', ' '))\n self._msg.Wrap(content_area)", "response": "Update the _msg widget with the given size"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the value of the DropDown option.", "response": "def GetValue(self):\n '''\n NOTE: Added on plane. Cannot remember exact implementation\n of counter objects. I believe that they count sequentail\n pairings of options\n e.g.\n -vvvvv\n But I'm not sure. That's what I'm going with for now.\n\n Returns\n str(action.options_string[0]) * DropDown Value\n '''\n dropdown_value = self._widget.GetValue()\n if not str(dropdown_value).isdigit():\n return None\n arg = str(self._action.option_strings[0]).replace('-', '')\n repeated_args = arg * int(dropdown_value)\n return '-' + repeated_args"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the full path to the language file", "response": "def get_path(language):\n ''' Returns the full path to the language file '''\n filename = language.lower() + '.json'\n lang_file_path = os.path.join(_DEFAULT_DIR, filename)\n if not os.path.exists(lang_file_path):\n raise IOError('Could not find {} language file'.format(language))\n return lang_file_path"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nopen and return the supplied json file", "response": "def load(filename):\n ''' Open and return the supplied json file '''\n global _DICTIONARY\n try:\n json_file = filename + '.json'\n with open(os.path.join(_DEFAULT_DIR, json_file), 'rb') as f:\n _DICTIONARY = json.load(f)\n except IOError:\n raise IOError('Language file not found. Make sure that your ',\n 'translation file is in the languages directory, ')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef safe_repr(obj, clip=None):\n try:\n s = repr(obj)\n if not clip or len(s) <= clip:\n return s\n else:\n return s[:clip-4]+'..'+s[-2:]\n except:\n return 'N/A'", "response": "Convert object to string representation yielding the same result a repr and returns N / A instead of raising the exception."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef trunc(obj, max, left=0):\n s = str(obj)\n s = s.replace('\\n', '|')\n if len(s) > max:\n if left:\n return '...'+s[len(s)-max+3:]\n else:\n return s[:(max-3)]+'...'\n else:\n return s", "response": "Convert obj to string eliminate newlines and truncate the string to max characters."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a friendly timestamp represented as a string.", "response": "def pp_timestamp(t):\n \"\"\"\n Get a friendly timestamp represented as a string.\n \"\"\"\n if t is None:\n return ''\n h, m, s = int(t / 3600), int(t / 60 % 60), t % 60\n return \"%02d:%02d:%05.2f\" % (h, m, s)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef print_stats(self, stream=None):\n if not stream: # pragma: no cover\n stream = sys.stdout\n self.metadata.sort(key=lambda x: -x.size)\n stream.write('%-10s %8s %-12s %-46s\\n' % ('id', 'size', 'type', 'representation'))\n for g in self.metadata:\n stream.write('0x%08x %8d %-12s %-46s\\n' % (g.id, g.size, trunc(g.type, 12),\n trunc(g.str, 46)))\n stream.write('Garbage: %8d collected objects (%s in cycles): %12s\\n' % \\\n (self.count, self.num_in_cycles, pp(self.total_size)))", "response": "Log annotated garbage objects to console or file."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a set of profiled file names.", "response": "def getFilenameSet(self):\n \"\"\"\n Returns a set of profiled file names.\n\n Note: \"file name\" is used loosely here. See python documentation for\n co_filename, linecache module and PEP302. It may not be a valid\n filesystem path.\n \"\"\"\n result = set(self.file_dict)\n # Ignore profiling code. __file__ does not always provide consistent\n # results with f_code.co_filename (ex: easy_install with zipped egg),\n # so inspect current frame instead.\n # XXX: assumes all of pprofile code resides in a single file.\n result.discard(inspect.currentframe().f_code.co_filename)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef annotate(self, out, filename=None, commandline=None, relative_path=False):\n file_dict = self.file_dict\n total_time = self.total_time\n if commandline is not None:\n print >> out, 'Command line:', commandline\n print >> out, 'Total duration: %gs' % total_time\n if not total_time:\n return\n def percent(value, scale):\n if scale == 0:\n return 0\n return value * 100 / float(scale)\n for name in self._getFileNameList(filename):\n file_timing = file_dict[name]\n file_total_time = file_timing.getTotalTime()\n call_list_by_line = file_timing.getCallListByLine()\n print >> out, 'File:', name\n print >> out, 'File duration: %gs (%.2f%%)' % (file_total_time,\n percent(file_total_time, total_time))\n print >> out, _ANNOTATE_HEADER\n print >> out, _ANNOTATE_HORIZONTAL_LINE\n for lineno, _, _, hits, duration, line in self._iterFile(name,\n call_list_by_line):\n if hits:\n time_per_hit = duration / hits\n else:\n time_per_hit = 0\n print >> out, _ANNOTATE_FORMAT % {\n 'lineno': lineno,\n 'hits': hits,\n 'time': duration,\n 'time_per_hit': time_per_hit,\n 'percent': percent(duration, total_time),\n 'line': line,\n },\n for _, _, hits, duration, callee_file, callee_line, \\\n callee_name in call_list_by_line.get(lineno, ()):\n print >> out, _ANNOTATE_CALL_FORMAT % {\n 'hits': hits,\n 'time': duration,\n 'time_per_hit': duration / hits,\n 'percent': percent(duration, total_time),\n 'callee_file': callee_file,\n 'callee_line': callee_line,\n 'callee_name': callee_name,\n }", "response": "Dump annotated source code with current profiling statistics to out."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwriting data to stdout and file.", "response": "def write(self, data, end=\"\\n\", flush=True):\n \"\"\" Output data to stdout and/or file \"\"\"\n if not self.nostdout:\n self.stdout.write(data+end)\n if self.file is not None:\n self.file.write(data+end)\n if flush:\n self.flush()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nforces commit changes to the file and stdout", "response": "def flush(self):\n \"\"\" Force commit changes to the file and stdout \"\"\"\n if not self.nostdout:\n self.stdout.flush()\n if self.file is not None:\n self.file.flush()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncomputes the absolute Levenshtein distance between two sequences seq1 and seq2.", "response": "def levenshtein(seq1, seq2, normalized=False, max_dist=-1):\n\t\"\"\"Compute the absolute Levenshtein distance between the two sequences\n\t`seq1` and `seq2`.\n\t\n\tThe Levenshtein distance is the minimum number of edit operations necessary\n\tfor transforming one sequence into the other. The edit operations allowed are:\n\t\n\t\t* deletion: ABC -> BC, AC, AB\n\t\t* insertion: ABC -> ABCD, EABC, AEBC..\n\t\t* substitution: ABC -> ABE, ADC, FBC..\n\t\n\tThe `max_dist` parameter controls at which moment we should stop computing the\n\tdistance between the provided sequences. If it is a negative integer, the\n\tdistance will be computed until the sequences are exhausted; otherwise, the\n\tcomputation will stop at the moment the calculated distance is higher than\n\t`max_dist`, and then return -1. For example:\n\t\n\t\t>>> levenshtein(\"abc\", \"abcd\", max_dist=1) # dist = 1\n\t\t1\n\t\t>>> levenshtein(\"abc\", \"abcde\", max_dist=1) # dist = 2\n\t\t-1\n\t\n\tThis can be a time saver if you're not interested in the exact distance, but\n\tonly need to check if the distance between the given sequences is below a\n\tgiven threshold.\n\t\n\tThe `normalized` parameter is here for backward compatibility; providing\n\tit will result in a call to `nlevenshtein`, which should be used directly\n\tinstead. \n\t\"\"\"\n\tif normalized:\n\t\treturn nlevenshtein(seq1, seq2, method=1)\n\t\t\n\tif seq1 == seq2:\n\t\treturn 0\n\t\n\tlen1, len2 = len(seq1), len(seq2)\n\tif max_dist >= 0 and abs(len1 - len2) > max_dist:\n\t\treturn -1\n\tif len1 == 0:\n\t\treturn len2\n\tif len2 == 0:\n\t\treturn len1\n\tif len1 < len2:\n\t\tlen1, len2 = len2, len1\n\t\tseq1, seq2 = seq2, seq1\n\t\n\tcolumn = array('L', range(len2 + 1))\n\t\n\tfor x in range(1, len1 + 1):\n\t\tcolumn[0] = x\n\t\tlast = x - 1\n\t\tfor y in range(1, len2 + 1):\n\t\t\told = column[y]\n\t\t\tcost = int(seq1[x - 1] != seq2[y - 1])\n\t\t\tcolumn[y] = min(column[y] + 1, column[y - 1] + 1, last + cost)\n\t\t\tlast = old\n\t\tif max_dist >= 0 and min(column) > max_dist:\n\t\t\treturn -1\n\t\n\tif max_dist >= 0 and column[len2] > max_dist:\n\t\t# stay consistent, even if we have the exact distance\n\t\treturn -1\n\treturn column[len2]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncomputing the normalized Levenshtein distance between seq1 and seq2.", "response": "def nlevenshtein(seq1, seq2, method=1):\n\t\"\"\"Compute the normalized Levenshtein distance between `seq1` and `seq2`.\n\t\n\tTwo normalization methods are provided. For both of them, the normalized\n\tdistance will be a float between 0 and 1, where 0 means equal and 1\n\tcompletely different. The computation obeys the following patterns:\n\t\n\t\t0.0 if seq1 == seq2\n\t\t1.0 if len(seq1) == 0 or len(seq2) == 0\n\t\tedit distance / factor otherwise\n\t\n\tThe `method` parameter specifies which normalization factor should be used.\n\tIt can have the value 1 or 2, which correspond to the following:\n\t\n\t\t1: the length of the shortest alignment between the sequences\n\t\t (that is, the length of the longest sequence)\n\t\t2: the length of the longest alignment between the sequences\n\t\n\tWhich normalization factor should be chosen is a matter of taste. The first\n\tone is cheap to compute. The second one is more costly, but it accounts\n\tbetter than the first one for parallelisms of symbols between the sequences.\n\t\t\n\tFor the rationale behind the use of the second method, see:\n\tHeeringa, \"Measuring Dialect Pronunciation Differences using Levenshtein\n\tDistance\", 2004, p. 130 sq, which is available online at:\n\thttp://www.let.rug.nl/~heeringa/dialectology/thesis/thesis.pdf\n\t\"\"\"\n\t\n\tif seq1 == seq2:\n\t\treturn 0.0\n\tlen1, len2 = len(seq1), len(seq2)\n\tif len1 == 0 or len2 == 0:\n\t\treturn 1.0\n\tif len1 < len2: # minimize the arrays size\n\t\tlen1, len2 = len2, len1\n\t\tseq1, seq2 = seq2, seq1\n\t\n\tif method == 1:\n\t\treturn levenshtein(seq1, seq2) / float(len1)\n\tif method != 2:\n\t\traise ValueError(\"expected either 1 or 2 for `method` parameter\")\n\t\n\tcolumn = array('L', range(len2 + 1))\n\tlength = array('L', range(len2 + 1))\n\t\n\tfor x in range(1, len1 + 1):\n\t\n\t\tcolumn[0] = length[0] = x\n\t\tlast = llast = x - 1\n\t\t\n\t\tfor y in range(1, len2 + 1):\n\t\t\n\t\t\t# dist\n\t\t\told = column[y]\n\t\t\tic = column[y - 1] + 1\n\t\t\tdc = column[y] + 1\n\t\t\trc = last + (seq1[x - 1] != seq2[y - 1])\n\t\t\tcolumn[y] = min(ic, dc, rc)\n\t\t\tlast = old\n\t\t\t\n\t\t\t# length\n\t\t\tlold = length[y]\n\t\t\tlic = length[y - 1] + 1 if ic == column[y] else 0\n\t\t\tldc = length[y] + 1 if dc == column[y] else 0\n\t\t\tlrc = llast + 1 if rc == column[y] else 0\n\t\t\tlength[y] = max(ldc, lic, lrc)\n\t\t\tllast = lold\n\t\n\treturn column[y] / float(length[y])"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndetermining all parents of a node in our tree", "response": "def parents(self, node):\n \"\"\"Determine all parents of node in our tree\"\"\"\n return [\n parent for parent in\n getattr( node, 'parents', [] )\n if getattr(parent, 'tree', self.TREE) == self.TREE\n ]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting string reference to object. Stores a weak reference in a dictionary using the object s id as the key.", "response": "def get_ref(obj):\n \"\"\"\n Get string reference to object. Stores a weak reference in a dictionary\n using the object's id as the key. If the object cannot be weakly\n referenced (e.g. dictionaries, frame objects), store a strong references\n in a classic dictionary.\n\n Returns the object's id as a string.\n \"\"\"\n oid = id(obj)\n try:\n server.id2ref[oid] = obj\n except TypeError:\n server.id2obj[oid] = obj\n return str(oid)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_obj(ref):\n oid = int(ref)\n return server.id2ref.get(oid) or server.id2obj[oid]", "response": "Get object from string reference."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef tracker_class(clsname):\n stats = server.stats\n if not stats:\n bottle.redirect('/tracker')\n stats.annotate()\n return dict(stats=stats, clsname=clsname)", "response": "Get class instance details."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting reference cycle details.", "response": "def garbage_cycle(index):\n \"\"\"Get reference cycle details.\"\"\"\n graph = _compute_garbage_graphs()[int(index)]\n graph.reduce_to_cycles()\n objects = graph.metadata\n objects.sort(key=lambda x: -x.size)\n return dict(objects=objects, index=index)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_graph(graph, filename):\n try:\n rendered = graph.rendered_file\n except AttributeError:\n try:\n graph.render(os.path.join(server.tmpdir, filename), format='png')\n rendered = filename\n except OSError:\n rendered = None\n graph.rendered_file = rendered\n return rendered", "response": "Retrieve or render a graph."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef garbage_graph(index):\n graph = _compute_garbage_graphs()[int(index)]\n reduce_graph = bottle.request.GET.get('reduce', '')\n if reduce_graph:\n graph = graph.reduce_to_cycles()\n if not graph:\n return None\n filename = 'garbage%so%s.png' % (index, reduce_graph)\n rendered_file = _get_graph(graph, filename)\n if rendered_file:\n bottle.send_file(rendered_file, root=server.tmpdir)\n else:\n return None", "response": "Get graph representation of reference cycle."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef start_profiler(host='localhost', port=8090, tracker=None, stats=None,\n debug=False, **kwargs):\n \"\"\"\n Start the web server to show profiling data. The function suspends the\n Python application (the current thread) until the web server is stopped.\n\n The only way to stop the server is to signal the running thread, e.g. press\n Ctrl+C in the console. If this isn't feasible for your application use\n `start_in_background` instead.\n\n During the execution of the web server, profiling data is (lazily) cached\n to improve performance. For example, garbage graphs are rendered when the\n garbage profiling data is requested and are simply retransmitted upon later\n requests.\n\n :param host: the host where the server shall run, default is localhost\n :param port: server listens on the specified port, default is 8090 to allow\n coexistance with common web applications\n :param tracker: `ClassTracker` instance, browse profiling data (on-line\n analysis)\n :param stats: `Stats` instance, analyze `ClassTracker` profiling dumps\n (useful for off-line analysis)\n \"\"\"\n if tracker and not stats:\n server.stats = tracker.stats\n else:\n server.stats = stats\n try:\n server.tmpdir = mkdtemp(prefix='pympler')\n server.server = PymplerServer(host=host, port=port, **kwargs)\n bottle.debug(debug)\n bottle.run(server=server.server)\n finally:\n rmtree(server.tmpdir)", "response": "Start the profiling server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _winreg_getShellFolder( name ):\n k = _winreg.OpenKey(\n _winreg.HKEY_CURRENT_USER,\n r\"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\"\n )\n try:\n # should check that it's valid? How?\n return _winreg.QueryValueEx( k, name )[0]\n finally:\n _winreg.CloseKey( k )", "response": "Get a shell folder by string name from the registry"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef appdatadirectory( ):\n if shell:\n # on Win32 and have Win32all extensions, best-case\n return shell_getShellFolder(shellcon.CSIDL_APPDATA)\n if _winreg:\n # on Win32, but no Win32 shell com available, this uses\n # a direct registry access, likely to fail on Win98/Me\n return _winreg_getShellFolder( 'AppData' )\n # okay, what if for some reason _winreg is missing? would we want to allow ctypes?\n ## default case, look for name in environ...\n for name in ['APPDATA', 'HOME']:\n if name in os.environ:\n return os.path.join( os.environ[name], '.config' )\n # well, someone's being naughty, see if we can get ~ to expand to a directory...\n possible = os.path.abspath(os.path.expanduser( '~/.config' ))\n if os.path.exists( possible ):\n return possible\n raise OSError( \"\"\"Unable to determine user's application-data directory, no ${HOME} or ${APPDATA} in environment\"\"\" )", "response": "Attempt to retrieve the current user s application - data directory"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of all known objects excluding frame objects.", "response": "def get_objects(remove_dups=True, include_frames=False):\n \"\"\"Return a list of all known objects excluding frame objects.\n\n If (outer) frame objects shall be included, pass `include_frames=True`. In\n order to prevent building reference cycles, the current frame object (of\n the caller of get_objects) is ignored. This will not prevent creating\n reference cycles if the object list is passed up the call-stack. Therefore,\n frame objects are not included by default.\n\n Keyword arguments:\n remove_dups -- if True, all duplicate objects will be removed.\n include_frames -- if True, includes frame objects.\n \"\"\"\n gc.collect()\n\n # Do not initialize local variables before calling gc.get_objects or those\n # will be included in the list. Furthermore, ignore frame objects to\n # prevent reference cycles.\n tmp = gc.get_objects()\n tmp = [o for o in tmp if not isframe(o)]\n\n res = []\n for o in tmp:\n # gc.get_objects returns only container objects, but we also want\n # the objects referenced by them\n refs = get_referents(o)\n for ref in refs:\n if not _is_containerobject(ref):\n # we already got the container objects, now we only add\n # non-container objects\n res.append(ref)\n res.extend(tmp)\n if remove_dups:\n res = _remove_duplicates(res)\n\n if include_frames:\n for sf in stack()[2:]:\n res.append(sf[0])\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the total size of all elements in objects.", "response": "def get_size(objects):\n \"\"\"Compute the total size of all elements in objects.\"\"\"\n res = 0\n for o in objects:\n try:\n res += _getsizeof(o)\n except AttributeError:\n print(\"IGNORING: type=%s; o=%s\" % (str(type(o)), str(o)))\n return res"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_diff(left, right):\n res = {'+': [], '-': []}\n\n def partition(objects):\n \"\"\"Partition the passed object list.\"\"\"\n res = {}\n for o in objects:\n t = type(o)\n if type(o) not in res:\n res[t] = []\n res[t].append(o)\n return res\n\n def get_not_included(foo, bar):\n \"\"\"Compare objects from foo with objects defined in the values of\n bar (set of partitions).\n Returns a list of all objects included in list, but not dict values.\n \"\"\"\n res = []\n for o in foo:\n if not compat.object_in_list(type(o), bar):\n res.append(o)\n elif not compat.object_in_list(o, bar[type(o)]):\n res.append(o)\n return res\n\n # Create partitions of both lists. This will reduce the time required for\n # the comparison\n left_objects = partition(left)\n right_objects = partition(right)\n # and then do the diff\n res['+'] = get_not_included(right, left_objects)\n res['-'] = get_not_included(left, right_objects)\n return res", "response": "Get the difference of two lists."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfiltering objects by type minimum size and maximum size.", "response": "def filter(objects, Type=None, min=-1, max=-1): #PYCHOK muppy filter\n \"\"\"Filter objects.\n\n The filter can be by type, minimum size, and/or maximum size.\n\n Keyword arguments:\n Type -- object type to filter by\n min -- minimum object size\n max -- maximum object size\n\n \"\"\"\n res = []\n if min > max:\n raise ValueError(\"minimum must be smaller than maximum\")\n if Type is not None:\n res = [o for o in objects if isinstance(o, Type)]\n if min > -1:\n res = [o for o in res if _getsizeof(o) < min]\n if max > -1:\n res = [o for o in res if _getsizeof(o) > max]\n return res"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all referents of an object up to a certain level.", "response": "def get_referents(object, level=1):\n \"\"\"Get all referents of an object up to a certain level.\n\n The referents will not be returned in a specific order and\n will not contain duplicate objects. Duplicate objects will be removed.\n\n Keyword arguments:\n level -- level of indirection to which referents considered.\n\n This function is recursive.\n\n \"\"\"\n res = gc.get_referents(object)\n level -= 1\n if level > 0:\n for o in res:\n res.extend(get_referents(o, level))\n res = _remove_duplicates(res)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_usage(function, *args):\n # The usage of a function is calculated by creating one summary of all\n # objects before the function is invoked and afterwards. These summaries\n # are compared and the diff is returned.\n # This function works in a 2-steps process. Before the actual function is\n # invoked an empty dummy function is measurement to identify the overhead\n # involved in the measuring process. This overhead then is subtracted from\n # the measurement performed on the passed function. The result reflects the\n # actual usage of a function call.\n # Also, a measurement is performed twice, allowing the adjustment to\n # initializing things, e.g. modules\n\n res = None\n\n def _get_summaries(function, *args):\n \"\"\"Get a 2-tuple containing one summary from before, and one summary\n from after the function has been invoked.\n\n \"\"\"\n s_before = summary.summarize(get_objects())\n function(*args)\n s_after = summary.summarize(get_objects())\n return (s_before, s_after)\n\n def _get_usage(function, *args):\n \"\"\"Get the usage of a function call.\n This function is to be used only internally. The 'real' get_usage\n function is a wrapper around _get_usage, but the workload is done\n here.\n\n \"\"\"\n res = []\n # init before calling\n (s_before, s_after) = _get_summaries(function, *args)\n # ignore all objects used for the measurement\n ignore = []\n if s_before != s_after:\n ignore.append(s_before)\n for row in s_before:\n # ignore refs from summary and frame (loop)\n if len(gc.get_referrers(row)) == 2:\n ignore.append(row)\n for item in row:\n # ignore refs from summary and frame (loop)\n if len(gc.get_referrers(item)) == 2:\n ignore.append(item)\n for o in ignore:\n s_after = summary._subtract(s_after, o)\n res = summary.get_diff(s_before, s_after)\n return summary._sweep(res)\n\n # calibrate; twice for initialization\n def noop(): pass\n offset = _get_usage(noop)\n offset = _get_usage(noop)\n # perform operation twice to handle objects possibly used in\n # initialisation\n tmp = _get_usage(function, *args)\n tmp = _get_usage(function, *args)\n tmp = summary.get_diff(offset, tmp)\n tmp = summary._sweep(tmp)\n if len(tmp) != 0:\n res = tmp\n return res", "response": "This function is used only internally. It is used by the real get_usage function."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _remove_duplicates(objects):\n seen = {}\n result = []\n for item in objects:\n marker = id(item)\n if marker in seen:\n continue\n seen[marker] = 1\n result.append(item)\n return result", "response": "Remove duplicate objects.\n\n Inspired by http://www.peterbe.com/plog/uniqifiers-benchmark"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_optionals_without_choices(self, actions):\n boolean_actions = (\n _StoreConstAction, _StoreFalseAction,\n _StoreTrueAction\n )\n return [action\n for action in actions\n if action.option_strings\n and not action.choices\n and not isinstance(action, _CountAction)\n and not isinstance(action, _HelpAction)\n and type(action) not in boolean_actions]", "response": "Returns a list of all actions which are not optional but without required choices."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns all instances of flag type options.", "response": "def get_flag_style_optionals(self, actions):\n \"\"\"\n Gets all instances of \"flag\" type options.\n i.e. options which either store a const, or\n store boolean style options (e.g. StoreTrue).\n Types:\n _StoreTrueAction\n _StoreFalseAction\n _StoreConst\n \"\"\"\n return [action\n for action in actions\n if isinstance(action, _StoreTrueAction)\n or isinstance(action, _StoreFalseAction)\n or isinstance(action, _StoreConstAction)]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef resize_bitmap(parent, _bitmap, target_height):\n '''\n Resizes a bitmap to a height of 89 pixels (the\n size of the top panel), while keeping aspect ratio\n in tact\n '''\n image = wx.ImageFromBitmap(_bitmap)\n _width, _height = image.GetSize()\n if _height < target_height:\n return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))\n ratio = float(_width) / _height\n image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH)\n return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))", "response": "Resizes a bitmap to a height of 89 pixels while keeping aspect ratio\n in tact\n"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef ilevenshtein(seq1, seqs, max_dist=-1):\n\tfor seq2 in seqs:\n\t\tdist = levenshtein(seq1, seq2, max_dist=max_dist)\n\t\tif dist != -1:\n\t\t\tyield dist, seq2", "response": "Compute the Levenshtein distance between the sequence seq1 and the series\n\tsequences seqs."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn an iterator over all the sequences in seqs which distance from the seq1 is lower or equal to 2.", "response": "def ifast_comp(seq1, seqs, transpositions=False):\n\t\"\"\"Return an iterator over all the sequences in `seqs` which distance from\n\t`seq1` is lower or equal to 2. The sequences which distance from the\n\treference sequence is higher than that are dropped.\n\t\n\t\t`seq1`: the reference sequence.\n\t\t`seqs`: a series of sequences (can be a generator)\n\t\t`transpositions` has the same sense than in `fast_comp`.\n\t\n\tThe return value is a series of pairs (distance, sequence).\n\t\n\tYou might want to call `sorted()` on the iterator to get the results in a\n\tsignificant order:\n\t\n\t\t>>> g = ifast_comp(\"foo\", [\"fo\", \"bar\", \"foob\", \"foo\", \"foobaz\"])\n\t\t>>> sorted(g)\n\t\t[(0, 'foo'), (1, 'fo'), (1, 'foob')]\n\t\"\"\"\n\tfor seq2 in seqs:\n\t\tdist = fast_comp(seq1, seq2, transpositions)\n\t\tif dist != -1:\n\t\t\tyield dist, seq2"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef summarize(objects):\n count = {}\n total_size = {}\n for o in objects:\n otype = _repr(o)\n if otype in count:\n count[otype] += 1\n total_size[otype] += _getsizeof(o)\n else:\n count[otype] = 1\n total_size[otype] = _getsizeof(o)\n rows = []\n for otype in count:\n rows.append([otype, count[otype], total_size[otype]])\n return rows", "response": "Summarize an objects list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the difference of two summaries.", "response": "def get_diff(left, right):\n \"\"\"Get the difference of two summaries.\n\n Subtracts the values of the right summary from the values of the left\n summary.\n If similar rows appear on both sides, the are included in the summary with\n 0 for number of elements and total size.\n If the number of elements of a row of the diff is 0, but the total size is\n not, it means that objects likely have changed, but not there number, thus\n resulting in a changed size.\n\n \"\"\"\n res = []\n for row_r in right:\n found = False\n for row_l in left:\n if row_r[0] == row_l[0]:\n res.append([row_r[0], row_r[1] - row_l[1], row_r[2] - row_l[2]])\n found = True\n if not found:\n res.append(row_r)\n\n for row_l in left:\n found = False\n for row_r in right:\n if row_l[0] == row_r[0]:\n found = True\n if not found:\n res.append([row_l[0], -row_l[1], -row_l[2]])\n return res"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef print_(rows, limit=15, sort='size', order='descending'):\n localrows = []\n for row in rows:\n localrows.append(list(row))\n # input validation\n sortby = ['type', '#', 'size']\n if sort not in sortby:\n raise ValueError(\"invalid sort, should be one of\" + str(sortby))\n orders = ['ascending', 'descending']\n if order not in orders:\n raise ValueError(\"invalid order, should be one of\" + str(orders))\n # sort rows\n if sortby.index(sort) == 0:\n if order == \"ascending\":\n localrows.sort(key=lambda x: _repr(x[0]))\n elif order == \"descending\":\n localrows.sort(key=lambda x: _repr(x[0]), reverse=True)\n else:\n if order == \"ascending\":\n localrows.sort(key=lambda x: x[sortby.index(sort)])\n elif order == \"descending\":\n localrows.sort(key=lambda x: x[sortby.index(sort)], reverse=True)\n # limit rows\n localrows = localrows[0:limit]\n for row in localrows:\n row[2] = stringutils.pp(row[2])\n # print rows\n localrows.insert(0,[\"types\", \"# objects\", \"total size\"])\n _print_table(localrows)", "response": "Print the rows as a summary."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprint a list of lists as a pretty table.", "response": "def _print_table(rows, header=True):\n \"\"\"Print a list of lists as a pretty table.\n\n Keyword arguments:\n header -- if True the first row is treated as a table header\n\n inspired by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662\n \"\"\"\n border = \"=\"\n # vertical delimiter\n vdelim = \" | \"\n # padding nr. of spaces are left around the longest element in the\n # column\n padding = 1\n # may be left,center,right\n justify = 'right'\n justify = {'left' : str.ljust,\n 'center' : str.center,\n 'right' : str.rjust}[justify.lower()]\n # calculate column widths (longest item in each col\n # plus \"padding\" nr of spaces on both sides)\n cols = zip(*rows)\n colWidths = [max([len(str(item))+2*padding for item in col]) for col in cols]\n borderline = vdelim.join([w*border for w in colWidths])\n for row in rows:\n print(vdelim.join([justify(str(item),width) for (item,width) in zip(row,colWidths)]))\n if header:\n print(borderline)\n header=False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the string representation of the object o.", "response": "def _repr(o, verbosity=1):\n \"\"\"Get meaning object representation.\n\n This function should be used when the simple str(o) output would result in\n too general data. E.g. \" 0:\n # Compute both hashes at the same time\n hasher_md5.update(buf)\n hasher_sha1.update(buf)\n # Load the next data block from file\n buf = afile.read(blocksize)\n return (hasher_md5.hexdigest(), hasher_sha1.hexdigest())"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the degree of the polynomial", "response": "def get_degree(self, poly=None):\n '''Returns the degree of the polynomial'''\n if not poly:\n return self.degree\n #return len(self.coefficients) - 1\n elif poly and hasattr(\"coefficients\", poly):\n return len(poly.coefficients) - 1\n else:\n while poly and poly[-1] == 0:\n poly.pop() # normalize\n return len(poly)-1"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncompute the multiplication between two polynomials only at the specified coefficient", "response": "def mul_at(self, other, k):\n '''Compute the multiplication between two polynomials only at the specified coefficient (this is a lot cheaper than doing the full polynomial multiplication and then extract only the required coefficient)'''\n if k > (self.degree + other.degree) or k > self.degree: return 0 # optimization: if the required coefficient is above the maximum coefficient of the resulting polynomial, we can already predict that and just return 0\n\n term = 0\n\n for i in _range(min(len(self), len(other))):\n coef1 = self.coefficients[-(k-i+1)]\n coef2 = other.coefficients[-(i+1)]\n if coef1 == 0 or coef2 == 0: continue # log(0) is undefined, skip (and in addition it's a nice optimization)\n term += coef1 * coef2\n return term"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmultiplies a polynomial with a scalar", "response": "def scale(self, scalar):\n '''Multiply a polynomial with a scalar'''\n return self.__class__([self.coefficients[i] * scalar for i in _range(len(self))])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfasting division by using Extended Synthetic Division", "response": "def _fastdivmod(dividend, divisor):\n '''Fast polynomial division by using Extended Synthetic Division (aka Horner's method). Also works with non-monic polynomials.\n A nearly exact same code is explained greatly here: http://research.swtch.com/field and you can also check the Wikipedia article and the Khan Academy video.'''\n # Note: for RS encoding, you should supply divisor = mprime (not m, you need the padded message)\n msg_out = list(dividend) # Copy the dividend\n normalizer = divisor[0] # precomputing for performance\n for i in _range(len(dividend)-(len(divisor)-1)):\n msg_out[i] /= normalizer # for general polynomial division (when polynomials are non-monic), the usual way of using synthetic division is to divide the divisor g(x) with its leading coefficient (call it a). In this implementation, this means:we need to compute: coef = msg_out[i] / gen[0]. For more infos, see http://en.wikipedia.org/wiki/Synthetic_division\n coef = msg_out[i] # precaching\n if coef != 0: # log(0) is undefined, so we need to avoid that case explicitly (and it's also a good optimization)\n for j in _range(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisior, because it's only used to normalize the dividend coefficient\n if divisor[j] != 0: # log(0) is undefined so we need to avoid that case\n msg_out[i + j] += -divisor[j] * coef\n\n # The resulting msg_out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder has necessarily the same degree as the divisor -- not length but degree == length-1 -- since it's what we couldn't divide from the dividend), so we compute the index where this separation is, and return the quotient and remainder.\n separator = -(len(divisor)-1)\n return Polynomial(msg_out[:separator]), Polynomial(msg_out[separator:])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfast polynomial division by using Extended Synthetic Division and optimized for GF2int computations.", "response": "def _gffastdivmod(dividend, divisor):\n '''Fast polynomial division by using Extended Synthetic Division and optimized for GF(2^p) computations (so it is not generic, must be used with GF2int).\n Transposed from the reedsolomon library: https://github.com/tomerfiliba/reedsolomon\n BEWARE: it works only for monic divisor polynomial! (which is always the case with Reed-Solomon's generator polynomials)'''\n\n msg_out = list(dividend) # Copy the dividend list and pad with 0 where the ecc bytes will be computed\n for i in _range(len(dividend)-(len(divisor)-1)):\n coef = msg_out[i] # precaching\n if coef != 0: # log(0) is undefined, so we need to avoid that case explicitly (and it's also a good optimization)\n for j in _range(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisior, because it's only used to normalize the dividend coefficient (which is here useless since the divisor, the generator polynomial, is always monic)\n #if divisor[j] != 0: # log(0) is undefined so we need to check that, but it slow things down in fact and it's useless in our case (reed-solomon encoding) since we know that all coefficients in the generator are not 0\n msg_out[i + j] ^= divisor[j] * coef # equivalent to the more mathematically correct (but xoring directly is faster): msg_out[i + j] += -divisor[j] * coef\n # Note: we could speed things up a bit if we could inline the table lookups, but the Polynomial class is generic, it doesn't know anything about the underlying fields and their operators. Good OOP design, bad for performances in Python because of function calls and the optimizations we can't do (such as precomputing gf_exp[divisor]). That's what is done in reedsolo lib, this is one of the reasons it is faster.\n\n # The resulting msg_out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder has necessarily the same degree as the divisor -- not length but degree == length-1 -- since it's what we couldn't divide from the dividend), so we compute the index where this separation is, and return the quotient and remainder.\n separator = -(len(divisor)-1)\n return Polynomial(msg_out[:separator]), Polynomial(msg_out[separator:])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef evaluate(self, x):\n '''Evaluate this polynomial at value x, returning the result (which is the sum of all evaluations at each term).'''\n # Holds the sum over each term in the polynomial\n #c = 0\n\n # Holds the current power of x. This is multiplied by x after each term\n # in the polynomial is added up. Initialized to x^0 = 1\n #p = 1\n\n #for term in self.coefficients[::-1]:\n # c = c + term * p\n # p = p * x\n #return c\n\n # Faster alternative using Horner's Scheme\n y = self[0]\n for i in _range(1, len(self)):\n y = y * x + self.coefficients[i]\n return y", "response": "Evaluate this polynomial at value x returning the result."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncompute the formal derivative of the polynomial", "response": "def derive(self):\n '''Compute the formal derivative of the polynomial: sum(i*coeff[i] x^(i-1))'''\n #res = [0] * (len(self)-1) # pre-allocate the list, it will be one item shorter because the constant coefficient (x^0) will be removed\n #for i in _range(2, len(self)+1): # start at 2 to skip the first coeff which is useless since it's a constant (x^0) so we +1, and because we work in reverse (lower coefficients are on the right) so +1 again\n #res[-(i-1)] = (i-1) * self[-i] # self[-i] == coeff[i] and i-1 is the x exponent (eg: x^1, x^2, x^3, etc.)\n #return Polynomial(res)\n\n # One liner way to do it (also a bit faster too)\n #return Polynomial( [(i-1) * self[-i] for i in _range(2, len(self)+1)][::-1] )\n # Another faster version\n L = len(self)-1\n return Polynomial( [(L-i) * self[i] for i in _range(0, len(self)-1)] )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngeneralizing feature scaling (useful for variable error correction rate calculation)", "response": "def feature_scaling(x, xmin, xmax, a=0, b=1):\n '''Generalized feature scaling (useful for variable error correction rate calculation)'''\n return a + float(x - xmin) * (b - a) / (xmax - xmin)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef entry_fields(file, entry_pos, field_delim=\"\\xFF\"):\n '''From an ecc entry position (a list with starting and ending positions), extract the metadata fields (filename, filesize, ecc for both), and the starting/ending positions of the ecc stream (containing variably encoded blocks of hash and ecc per blocks of the original file's header)'''\n # Read the the beginning of the ecc entry\n blocksize = 65535\n file.seek(entry_pos[0])\n entry = file.read(blocksize)\n entry = entry.lstrip(field_delim) # if there was some slight adjustment error (example: the last ecc block of the last file was the field_delim, then we will start with a field_delim, and thus we need to remove the trailing field_delim which is useless and will make the field detection buggy). This is not really a big problem for the previous file's ecc block: the missing ecc characters (which were mistaken for a field_delim), will just be missing (so we will lose a bit of resiliency for the last block of the previous file, but that's not a huge issue, the correction can still rely on the other characters).\n # TODO: do in a while loop in case the filename is really big (bigger than blocksize) - or in case we add intra-ecc for filename\n\n # Find metadata fields delimiters positions\n # TODO: automate this part, just give in argument the number of field_delim to find, and the func will find the x field_delims (the number needs to be specified in argument because the field_delim can maybe be found wrongly inside the ecc stream, which we don't want)\n first = entry.find(field_delim)\n second = entry.find(field_delim, first+len(field_delim))\n third = entry.find(field_delim, second+len(field_delim))\n fourth = entry.find(field_delim, third+len(field_delim))\n # Note: we do not try to find all the field delimiters because we optimize here: we just walk the string to find the exact number of field_delim we are looking for, and after we stop, no need to walk through the whole string.\n\n # Extract the content of the fields\n # Metadata fields\n relfilepath = entry[:first]\n filesize = entry[first+len(field_delim):second]\n relfilepath_ecc = entry[second+len(field_delim):third]\n filesize_ecc = entry[third+len(field_delim):fourth]\n # Ecc stream field (aka ecc blocks)\n ecc_field_pos = [entry_pos[0]+fourth+len(field_delim), entry_pos[1]] # return the starting and ending position of the rest of the ecc track, which contains blocks of hash/ecc of the original file's content.\n\n # Place the cursor at the beginning of the ecc_field\n file.seek(ecc_field_pos[0])\n\n # Try to convert to an int, an error may happen\n try:\n filesize = int(filesize)\n except Exception, e:\n print(\"Exception when trying to detect the filesize in ecc field (it may be corrupted), skipping: \")\n print(e)\n #filesize = 0 # avoid setting to 0, we keep as an int so that we can try to fix using intra-ecc\n\n # entries = [ {\"message\":, \"ecc\":, \"hash\":}, etc.]\n return {\"relfilepath\": relfilepath, \"relfilepath_ecc\": relfilepath_ecc, \"filesize\": filesize, \"filesize_ecc\": filesize_ecc, \"ecc_field_pos\": ecc_field_pos}", "response": "Extract the metadata fields from an ecc entry position."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef stream_entry_assemble(hasher, file, eccfile, entry_fields, max_block_size, header_size, resilience_rates, constantmode=False):\n '''From an entry with its parameters (filename, filesize), assemble a list of each block from the original file along with the relative hash and ecc for easy processing later.'''\n # Cut the header and the ecc entry into blocks, and then assemble them so that we can easily process block by block\n eccfile.seek(entry_fields[\"ecc_field_pos\"][0])\n curpos = file.tell()\n ecc_curpos = eccfile.tell()\n while (ecc_curpos < entry_fields[\"ecc_field_pos\"][1]): # continue reading the input file until we reach the position of the previously detected ending marker\n # Compute the current rate, depending on where we are inside the input file (headers? later stage?)\n if curpos < header_size or constantmode: # header stage: constant rate\n rate = resilience_rates[0]\n else: # later stage 2 or 3: progressive rate\n rate = feature_scaling(curpos, header_size, entry_fields[\"filesize\"], resilience_rates[1], resilience_rates[2]) # find the rate for the current stream of data (interpolate between stage 2 and stage 3 rates depending on the cursor position in the file)\n # From the rate, compute the ecc parameters\n ecc_params = compute_ecc_params(max_block_size, rate, hasher)\n # Extract the message block from input file, given the computed ecc parameters\n mes = file.read(ecc_params[\"message_size\"])\n if len(mes) == 0: return # quit if message is empty (reached end-of-file), this is a safeguard if ecc pos ending was miscalculated (we thus only need the starting position to be correct)\n buf = eccfile.read(ecc_params[\"hash_size\"]+ecc_params[\"ecc_size\"])\n hash = buf[:ecc_params[\"hash_size\"]]\n ecc = buf[ecc_params[\"hash_size\"]:]\n\n yield {\"message\": mes, \"hash\": hash, \"ecc\": ecc, \"rate\": rate, \"ecc_params\": ecc_params, \"curpos\": curpos, \"ecc_curpos\": ecc_curpos}\n # Prepare for the next iteration of the loop\n curpos = file.tell()\n ecc_curpos = eccfile.tell()\n # Just a quick safe guard against ecc ending marker misdetection\n file.seek(0, os.SEEK_END) # alternative way of finding the total size: go to the end of the file\n size = file.tell()\n if curpos < size: print(\"WARNING: end of ecc track reached but not the end of file! Either the ecc ending marker was misdetected, or either the file hash changed! Some blocks maybe may not have been properly checked!\")", "response": "Assemble a list of each entry from the original file along with the relative hash and ecc for easy processing later."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates a stream of hash and ecc blocks of variable encoding rate and size given a file.", "response": "def stream_compute_ecc_hash(ecc_manager, hasher, file, max_block_size, header_size, resilience_rates):\n '''Generate a stream of hash/ecc blocks, of variable encoding rate and size, given a file.'''\n curpos = file.tell() # init the reading cursor at the beginning of the file\n # Find the total size to know when to stop\n #size = os.fstat(file.fileno()).st_size # old way of doing it, doesn't work with StringIO objects\n file.seek(0, os.SEEK_END) # alternative way of finding the total size: go to the end of the file\n size = file.tell()\n file.seek(0, curpos) # place the reading cursor back at the beginning of the file\n # Main encoding loop\n while curpos < size: # Continue encoding while we do not reach the end of the file\n # Calculating the encoding rate\n if curpos < header_size: # if we are still reading the file's header, we use a constant rate\n rate = resilience_rates[0]\n else: # else we use a progressive rate for the rest of the file the we calculate on-the-fly depending on our current reading cursor position in the file\n rate = feature_scaling(curpos, header_size, size, resilience_rates[1], resilience_rates[2]) # find the rate for the current stream of data (interpolate between stage 2 and stage 3 rates depending on the cursor position in the file)\n\n # Compute the ecc parameters given the calculated rate\n ecc_params = compute_ecc_params(max_block_size, rate, hasher)\n #ecc_manager = ECCMan(max_block_size, ecc_params[\"message_size\"]) # not necessary to create an ecc manager anymore, as it is very costly. Now we can specify a value for k on the fly (tables for all possible values of k are pre-generated in the reed-solomon libraries)\n\n # Compute the ecc and hash for the current message block\n mes = file.read(ecc_params[\"message_size\"])\n hash = hasher.hash(mes)\n ecc = ecc_manager.encode(mes, k=ecc_params[\"message_size\"])\n #print(\"mes %i (%i) - ecc %i (%i) - hash %i (%i)\" % (len(mes), message_size, len(ecc), ecc_params[\"ecc_size\"], len(hash), ecc_params[\"hash_size\"])) # DEBUGLINE\n\n # Return the result\n yield [hash, ecc, ecc_params]\n # Prepare for next iteration\n curpos = file.tell()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef compute_ecc_hash_from_string(string, ecc_manager, hasher, max_block_size, resilience_rate):\n '''Generate a concatenated string of ecc stream of hash/ecc blocks, of constant encoding rate, given a string.\n NOTE: resilience_rate here is constant, you need to supply only one rate, between 0.0 and 1.0. The encoding rate will then be constant, like in header_ecc.py.'''\n fpfile = StringIO(string)\n ecc_stream = ''.join( [str(x[1]) for x in stream_compute_ecc_hash(ecc_manager, hasher, fpfile, max_block_size, len(string), [resilience_rate])] ) # \"hack\" the function by tricking it to always use a constant rate, by setting the header_size=len(relfilepath), and supplying the resilience_rate_intra instead of resilience_rate_s1 (the one for header)\n return ecc_stream", "response": "Generate a concatenated string of ecc stream of hash blocks of constant encoding rate given a string."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef ecc_correct_intra_stream(ecc_manager_intra, ecc_params_intra, hasher_intra, resilience_rate_intra, field, ecc, enable_erasures=False, erasures_char=\"\\x00\", only_erasures=False, max_block_size=65535):\n # convert strings to StringIO object so that we can trick our ecc reading functions that normally works only on files\n fpfile = StringIO(field)\n fpfile_ecc = StringIO(ecc)\n fpentry_p = {\"ecc_field_pos\": [0, len(field)]} # create a fake entry_pos so that the ecc reading function works correctly\n # Prepare variables\n field_correct = [] # will store each block of the corrected (or already correct) filepath\n fcorrupted = False # check if field was corrupted\n fcorrected = True # check if field was corrected (if it was corrupted)\n errmsg = ''\n # Decode each block of the filepath\n for e in stream_entry_assemble(hasher_intra, fpfile, fpfile_ecc, fpentry_p, max_block_size, len(field), [resilience_rate_intra], constantmode=True):\n # Check if this block of the filepath is OK, if yes then we just copy it over\n if ecc_manager_intra.check(e[\"message\"], e[\"ecc\"]):\n field_correct.append(e[\"message\"])\n else: # Else this block is corrupted, we will try to fix it using the ecc\n fcorrupted = True\n # Repair the message block and the ecc\n try:\n repaired_block, repaired_ecc = ecc_manager_intra.decode(e[\"message\"], e[\"ecc\"], enable_erasures=enable_erasures, erasures_char=erasures_char, only_erasures=only_erasures)\n except (ReedSolomonError, RSCodecError), exc: # the reedsolo lib may raise an exception when it can't decode. We ensure that we can still continue to decode the rest of the file, and the other files.\n repaired_block = None\n repaired_ecc = None\n errmsg += \"- Error: metadata field at offset %i: %s\\n\" % (entry_pos[0], exc)\n # Check if the block was successfully repaired: if yes then we copy the repaired block...\n if repaired_block is not None and ecc_manager_intra.check(repaired_block, repaired_ecc):\n field_correct.append(repaired_block)\n else: # ... else it failed, then we copy the original corrupted block and report an error later\n field_correct.append(e[\"message\"])\n fcorrected = False\n # Join all the blocks into one string to build the final filepath\n if isinstance(field_correct[0], bytearray): field_correct = [str(x) for x in field_correct] # workaround when using --ecc_algo 3 or 4, because we get a list of bytearrays instead of str\n field = ''.join(field_correct)\n # Report errors\n return (field, fcorrupted, fcorrected, errmsg)", "response": "Correct an intra - field with its corresponding intra - ecc if necessary"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the memory usage of a process or piece of code.", "response": "def memory_usage(proc=-1, interval=.1, timeout=None, run_in_place=False):\n \"\"\"\n Return the memory usage of a process or piece of code\n\n Parameters\n ----------\n proc : {int, string, tuple}, optional\n The process to monitor. Can be given by a PID, by a string\n containing a filename or by a tuple. The tuple should contain\n three values (f, args, kw) specifies to run the function\n f(*args, **kw). Set to -1 (default) for current process.\n\n interval : float, optional\n\n timeout : float, optional\n\n\n run_in_place : boolean, optional. False by default\n If False fork the process and retrieve timings from a different\n process. You shouldn't need to change this unless you are affected\n by this (http://blog.vene.ro/2012/07/04/on-why-my-memit-fails-on-osx)\n bug.\n\n Returns\n -------\n mm : list of integers, size less than num\n memory usage, in KB\n \"\"\"\n ret = []\n\n if timeout is not None:\n max_iter = timeout / interval\n elif isinstance(proc, int):\n # external process and no timeout\n max_iter = 1\n else:\n # for a Python function wait until it finishes\n max_iter = float('inf')\n\n if str(proc).endswith('.py'):\n filename = _find_script(proc)\n with open(filename) as f:\n proc = f.read()\n raise NotImplementedError\n\n if isinstance(proc, (list, tuple)):\n\n if len(proc) == 1:\n f, args, kw = (proc[0], (), {})\n elif len(proc) == 2:\n f, args, kw = (proc[0], proc[1], {})\n elif len(proc) == 3:\n f, args, kw = (proc[0], proc[1], proc[2])\n else:\n raise ValueError\n try:\n import multiprocessing\n except ImportError:\n print ('WARNING: cannot import module `multiprocessing`. Forcing to'\n ' run inplace.')\n # force inplace\n run_in_place = True\n if run_in_place:\n import threading\n main_thread = threading.Thread(target=f, args=args, kwargs=kw)\n else:\n main_thread = multiprocessing.Process(target=f, args=args, kwargs=kw)\n i = 0\n main_thread.start()\n pid = getattr(main_thread, 'pid', os.getpid())\n while i < max_iter and main_thread.is_alive():\n m = _get_memory(pid)\n ret.append(m)\n time.sleep(interval)\n i += 1\n main_thread.join()\n else:\n # external process\n if proc == -1:\n proc = os.getpid()\n if max_iter == -1:\n max_iter = 1\n for _ in range(max_iter):\n ret.append(_get_memory(proc))\n time.sleep(interval)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfinds the script in the PATH.", "response": "def _find_script(script_name):\n \"\"\" Find the script.\n\n If the input is not a file, then $PATH will be searched.\n \"\"\"\n if os.path.isfile(script_name):\n return script_name\n path = os.getenv('PATH', os.defpath).split(os.pathsep)\n for dir in path:\n if dir == '':\n continue\n fn = os.path.join(dir, script_name)\n if os.path.isfile(fn):\n return fn\n\n print >> sys.stderr, 'Could not find script {0}'.format(script_name)\n raise SystemExit(1)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef magic_memit(self, line=''):\n opts, stmt = self.parse_options(line, 'r:t:i', posix=False, strict=False)\n repeat = int(getattr(opts, 'r', 1))\n if repeat < 1:\n repeat == 1\n timeout = int(getattr(opts, 't', 0))\n if timeout <= 0:\n timeout = None\n run_in_place = hasattr(opts, 'i')\n\n mem_usage = memory_usage((_func_exec, (stmt, self.shell.user_ns)), timeout=timeout,\n run_in_place=run_in_place)\n\n if mem_usage:\n print('maximum of %d: %f MB per loop' % (repeat, max(mem_usage)))\n else:\n print('ERROR: could not read memory usage, try with a lower interval or more iterations')", "response": "Measure memory usage of a Python statement."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a Python function to the line profiling information.", "response": "def add_function(self, func):\n \"\"\" Record line profiling information for the given Python function.\n \"\"\"\n try:\n # func_code does not exist in Python3\n code = func.__code__\n except AttributeError:\n import warnings\n warnings.warn(\"Could not extract a code object for the object %r\"\n % (func,))\n return\n if code not in self.code_map:\n self.code_map[code] = {}\n self.functions.append(func)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef wrap_function(self, func):\n\n def f(*args, **kwds):\n self.enable_by_count()\n try:\n result = func(*args, **kwds)\n finally:\n self.disable_by_count()\n return result\n return f", "response": "Wrap a function to profile it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef runctx(self, cmd, globals, locals):\n self.enable_by_count()\n try:\n exec(cmd, globals, locals)\n finally:\n self.disable_by_count()\n return self", "response": "Profile a single executable statement in the given namespaces."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprofiles a single function call.", "response": "def runcall(self, func, *args, **kw):\n \"\"\" Profile a single function call.\n \"\"\"\n # XXX where is this used ? can be removed ?\n self.enable_by_count()\n try:\n return func(*args, **kw)\n finally:\n self.disable_by_count()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndisable the profiler if the number of disable requests matches the number of enable requests.", "response": "def disable_by_count(self):\n \"\"\" Disable the profiler if the number of disable requests matches the\n number of enable requests.\n \"\"\"\n if self.enable_count > 0:\n self.enable_count -= 1\n if self.enable_count == 0:\n self.disable()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses a makefile to find commands and substitute variables. Expects a makefile with only aliases and a line return between each command and each alias. Returns a dict with a list of commands for each alias.", "response": "def parse_makefile_aliases(filepath):\n '''\n Parse a makefile to find commands and substitute variables. Expects a\n makefile with only aliases and a line return between each command.\n\n Returns a dict, with a list of commands for each alias.\n '''\n\n # -- Parsing the Makefile using ConfigParser\n # Adding a fake section to make the Makefile a valid Ini file\n ini_str = '[root]\\n'\n with open(filepath, 'r') as fd:\n ini_str = ini_str + fd.read().replace('@make ', '')\n ini_fp = StringIO.StringIO(ini_str)\n # Parse using ConfigParser\n config = ConfigParser.RawConfigParser()\n config.readfp(ini_fp)\n # Fetch the list of aliases\n aliases = config.options('root')\n\n # -- Extracting commands for each alias\n commands = {}\n for alias in aliases:\n # strip the first line return, and then split by any line return\n commands[alias] = config.get('root', alias).lstrip('\\n').split('\\n')\n\n # -- Commands substitution\n # Loop until all aliases are substituted by their commands:\n # Check each command of each alias, and if there is one command that is to\n # be substituted by an alias, try to do it right away. If this is not\n # possible because this alias itself points to other aliases , then stop\n # and put the current alias back in the queue to be processed again later.\n\n # Create the queue of aliases to process\n aliases_todo = commands.keys()\n # Create the dict that will hold the full commands\n commands_new = {}\n # Loop until we have processed all aliases\n while aliases_todo:\n # Pick the first alias in the queue\n alias = aliases_todo.pop(0)\n # Create a new entry in the resulting dict\n commands_new[alias] = []\n # For each command of this alias\n for cmd in commands[alias]:\n # Ignore self-referencing (alias points to itself)\n if cmd == alias:\n pass\n # Substitute full command\n elif cmd in aliases and cmd in commands_new:\n # Append all the commands referenced by the alias\n commands_new[alias].extend(commands_new[cmd])\n # Delay substituting another alias, waiting for the other alias to\n # be substituted first\n elif cmd in aliases and cmd not in commands_new:\n # Delete the current entry to avoid other aliases\n # to reference this one wrongly (as it is empty)\n del commands_new[alias]\n aliases_todo.append(alias)\n break\n # Full command (no aliases)\n else:\n commands_new[alias].append(cmd)\n commands = commands_new\n del commands_new\n\n # -- Prepending prefix to avoid conflicts with standard setup.py commands\n # for alias in commands.keys():\n # commands['make_'+alias] = commands[alias]\n # del commands[alias]\n\n return commands"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nhacks run function which installs the trace.", "response": "def __run(self):\n \"\"\"Hacked run function, which installs the trace.\"\"\"\n sys.settrace(self.globaltrace)\n self.__run_backup()\n self.run = self.__run_backup"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef codepoint_included(self, codepoint):\n if self.codepoints == None:\n return True\n for cp in self.codepoints:\n mismatch = False\n for i in range(len(cp)):\n if (cp[i] is not None) and (cp[i] != codepoint[i]):\n mismatch = True\n break\n if not mismatch:\n return True\n return False", "response": "Check if codepoint is included in the current codepoints."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprofiling method used to profile matching codepoints and events.", "response": "def profile(self, frame, event, arg): #PYCHOK arg requ. to match signature\n \"\"\"Profiling method used to profile matching codepoints and events.\"\"\"\n if (self.events == None) or (event in self.events):\n frame_info = inspect.getframeinfo(frame)\n cp = (frame_info[0], frame_info[2], frame_info[1])\n if self.codepoint_included(cp):\n objects = muppy.get_objects()\n size = muppy.get_size(objects)\n if cp not in self.memories:\n self.memories[cp] = [0,0,0,0]\n self.memories[cp][0] = 1\n self.memories[cp][1] = size\n self.memories[cp][2] = size\n else:\n self.memories[cp][0] += 1\n if self.memories[cp][1] > size:\n self.memories[cp][1] = size\n if self.memories[cp][2] < size:\n self.memories[cp][2] = size"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef runprofile(mainfunction, output, timeout = 0, calibrate=False):\n '''\n Run the functions profiler and save the result\n If timeout is greater than 0, the profile will automatically stops after timeout seconds\n '''\n if noprofiler == True:\n print('ERROR: profiler and/or pstats library missing ! Please install it (probably package named python-profile) before running a profiling !')\n return False\n # This is the main function for profiling\n def _profile():\n profile.run(mainfunction, output)\n print('=> RUNNING FUNCTIONS PROFILER\\n\\n'); sys.stdout.flush();\n # Calibrate the profiler (only use this if the profiler produces some funny stuff, but calibration can also produce even more funny stuff with the latest cProfile of Python v2.7! So you should only enable calibration if necessary)\n if calibrate:\n print('Calibrating the profiler...'); sys.stdout.flush();\n cval = calibrateprofile()\n print('Calibration found value : %s' % cval); sys.stdout.flush();\n print('Initializing the profiler...'); sys.stdout.flush();\n # Run in timeout mode (if the function cannot ends by itself, this is the best mode: the function must ends for the profile to be saved)\n if timeout > 0:\n pthread = KThread(target=_profile) # we open the function with the profiler, in a special killable thread (see below why)\n print('Will now run the profiling and terminate it in %s seconds. Results will be saved in %s' % (str(timeout), str(output))); sys.stdout.flush();\n print('\\nCountdown:'); sys.stdout.flush();\n for i in range(0,5):\n print(str(5-i))\n sys.stdout.flush()\n time.sleep(1)\n print('0\\nStarting to profile...'); sys.stdout.flush();\n pthread.start() # starting the thread\n time.sleep(float(timeout)) # after this amount of seconds, the thread gets killed and the profiler will end its job\n print('\\n\\nFinishing the profile and saving to the file %s' % str(output)); sys.stdout.flush();\n pthread.kill() # we must end the main function in order for the profiler to output its results (if we didn't launch a thread and just closed the process, it would have done no result)\n # Run in full length mode (we run the function until it ends)\n else:\n print(\"Running the profiler, please wait until the process terminates by itself (if you forcequit before, the profile won't be saved)\")\n _profile()\n print('=> Functions Profile done !')\n return True", "response": "Runs profiling and saves the result in a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncalibrate the profiler and return the final value", "response": "def calibrateprofile():\n '''\n Calibrate the profiler (necessary to have non negative and more exact values)\n '''\n pr = profile.Profile()\n calib = []\n crepeat = 10\n for i in range(crepeat):\n calib.append(pr.calibrate(10000))\n final = sum(calib) / crepeat\n profile.Profile.bias = final # Apply computed bias to all Profile instances created hereafter\n return final"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses a profile log and print the result on screen", "response": "def parseprofile(profilelog, out):\n '''\n Parse a profile log and print the result on screen\n '''\n file = open(out, 'w') # opening the output file\n print('Opening the profile in %s...' % profilelog)\n p = pstats.Stats(profilelog, stream=file) # parsing the profile with pstats, and output everything to the file\n\n print('Generating the stats, please wait...')\n file.write(\"=== All stats:\\n\")\n p.strip_dirs().sort_stats(-1).print_stats()\n file.write(\"=== Cumulative time:\\n\")\n p.sort_stats('cumulative').print_stats(100)\n file.write(\"=== Time:\\n\")\n p.sort_stats('time').print_stats(100)\n file.write(\"=== Time + cumulative time:\\n\")\n p.sort_stats('time', 'cum').print_stats(.5, 'init')\n file.write(\"=== Callees:\\n\")\n p.print_callees()\n file.write(\"=== Callers:\\n\")\n p.print_callers()\n #p.print_callers(.5, 'init')\n #p.add('fooprof')\n file.close()\n print('Stats generated and saved to %s.' % out)\n print('Everything is done. Exiting')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef browseprofile(profilelog):\n '''\n Browse interactively a profile log in console\n '''\n print('Starting the pstats profile browser...\\n')\n try:\n browser = ProfileBrowser(profilelog)\n print >> browser.stream, \"Welcome to the profile statistics browser. Type help to get started.\"\n browser.cmdloop()\n print >> browser.stream, \"Goodbye.\"\n except KeyboardInterrupt:\n pass", "response": "Browse profile log in console"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef browseprofilegui(profilelog):\n '''\n Browse interactively a profile log in GUI using RunSnakeRun and SquareMap\n '''\n from runsnakerun import runsnake # runsnakerun needs wxPython lib, if it's not available then we can pass if we don't want a GUI. RunSnakeRun is only used for GUI visualisation, not for profiling (and you can still use pstats for console browsing)\n app = runsnake.RunSnakeRunApp(0)\n app.OnInit(profilelog)\n #app.OnInit()\n app.MainLoop()", "response": "Browse a profile log in GUI using RunSnakeRun and SquareMap\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of primes < n", "response": "def rwh_primes1(n):\n # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188\n ''' Returns a list of primes < n '''\n sieve = [True] * (n/2)\n for i in _range(3,int(n**0.5)+1,2):\n if sieve[i/2]:\n sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)\n return [2] + [2*i+1 for i in _range(1,n/2) if sieve[i]]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_prime_polynomials(generator=2, c_exp=8, fast_primes=False, single=False):\n '''Compute the list of prime polynomials for the given generator and galois field characteristic exponent.'''\n # fast_primes will output less results but will be significantly faster.\n # single will output the first prime polynomial found, so if all you want is to just find one prime polynomial to generate the LUT for Reed-Solomon to work, then just use that.\n\n # A prime polynomial (necessarily irreducible) is necessary to reduce the multiplications in the Galois Field, so as to avoid overflows.\n # Why do we need a \"prime polynomial\"? Can't we just reduce modulo 255 (for GF(2^8) for example)? Because we need the values to be unique.\n # For example: if the generator (alpha) = 2 and c_exp = 8 (GF(2^8) == GF(256)), then the generated Galois Field (0, 1, \u03b1, \u03b1^1, \u03b1^2, ..., \u03b1^(p-1)) will be galois field it becomes 0, 1, 2, 4, 8, 16, etc. However, upon reaching 128, the next value will be doubled (ie, next power of 2), which will give 256. Then we must reduce, because we have overflowed above the maximum value of 255. But, if we modulo 255, this will generate 256 == 1. Then 2, 4, 8, 16, etc. giving us a repeating pattern of numbers. This is very bad, as it's then not anymore a bijection (ie, a non-zero value doesn't have a unique index). That's why we can't just modulo 255, but we need another number above 255, which is called the prime polynomial.\n # Why so much hassle? Because we are using precomputed look-up tables for multiplication: instead of multiplying a*b, we precompute alpha^a, alpha^b and alpha^(a+b), so that we can just use our lookup table at alpha^(a+b) and get our result. But just like in our original field we had 0,1,2,...,p-1 distinct unique values, in our \"LUT\" field using alpha we must have unique distinct values (we don't care that they are different from the original field as long as they are unique and distinct). That's why we need to avoid duplicated values, and to avoid duplicated values we need to use a prime irreducible polynomial.\n\n # Here is implemented a bruteforce approach to find all these prime polynomials, by generating every possible prime polynomials (ie, every integers between field_charac+1 and field_charac*2), and then we build the whole Galois Field, and we reject the candidate prime polynomial if it duplicates even one value or if it generates a value above field_charac (ie, cause an overflow).\n # Note that this algorithm is slow if the field is too big (above 12), because it's an exhaustive search algorithm. There are probabilistic approaches, and almost surely prime approaches, but there is no determistic polynomial time algorithm to find irreducible monic polynomials. More info can be found at: http://people.mpi-inf.mpg.de/~csaha/lectures/lec9.pdf\n # Another faster algorithm may be found at Adleman, Leonard M., and Hendrik W. Lenstra. \"Finding irreducible polynomials over finite fields.\" Proceedings of the eighteenth annual ACM symposium on Theory of computing. ACM, 1986.\n\n # Prepare the finite field characteristic (2^p - 1), this also represent the maximum possible value in this field\n root_charac = 2 # we're in GF(2)\n field_charac = int(root_charac**c_exp - 1)\n field_charac_next = int(root_charac**(c_exp+1) - 1)\n\n prim_candidates = []\n if fast_primes:\n prim_candidates = rwh_primes1(field_charac_next) # generate maybe prime polynomials and check later if they really are irreducible\n prim_candidates = [x for x in prim_candidates if x > field_charac] # filter out too small primes\n else:\n prim_candidates = _range(field_charac+2, field_charac_next, root_charac) # try each possible prime polynomial, but skip even numbers (because divisible by 2 so necessarily not irreducible)\n\n # Start of the main loop\n correct_primes = []\n for prim in prim_candidates: # try potential candidates primitive irreducible polys\n seen = bytearray(field_charac+1) # memory variable to indicate if a value was already generated in the field (value at index x is set to 1) or not (set to 0 by default)\n conflict = False # flag to know if there was at least one conflict\n\n # Second loop, build the whole Galois Field\n x = GF2int(1)\n for i in _range(field_charac):\n # Compute the next value in the field (ie, the next power of alpha/generator)\n x = x.multiply(generator, prim, field_charac+1)\n\n # Rejection criterion: if the value overflowed (above field_charac) or is a duplicate of a previously generated power of alpha, then we reject this polynomial (not prime)\n if x > field_charac or seen[x] == 1:\n conflict = True\n break\n # Else we flag this value as seen (to maybe detect future duplicates), and we continue onto the next power of alpha\n else:\n seen[x] = 1\n\n # End of the second loop: if there's no conflict (no overflow nor duplicated value), this is a prime polynomial!\n if not conflict: \n correct_primes.append(prim)\n if single: return prim\n\n # Return the list of all prime polynomials\n return correct_primes", "response": "Compute the list of prime polynomials for the given generator and galois field characteristic exponent."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef init_lut(generator=3, prim=0x11b, c_exp=8):\n '''Precompute the logarithm and anti-log (look-up) tables for faster computation later, using the provided primitive polynomial.\n These tables are used for multiplication/division since addition/substraction are simple XOR operations inside GF of characteristic 2.\n The basic idea is quite simple: since b**(log_b(x), log_b(y)) == x * y given any number b (the base or generator of the logarithm), then we can use any number b to precompute logarithm and anti-log (exponentiation) tables to use for multiplying two numbers x and y.\n That's why when we use a different base/generator number, the log and anti-log tables are drastically different, but the resulting computations are the same given any such tables.\n For more infos, see https://en.wikipedia.org/wiki/Finite_field_arithmetic#Implementation_tricks\n '''\n # generator is the generator number (the \"increment\" that will be used to walk through the field by multiplication, this must be a prime number). This is basically the base of the logarithm/anti-log tables. Also often noted \"alpha\" in academic books.\n # prim is the primitive/prime (binary) polynomial and must be irreducible (it can't represented as the product of two smaller polynomials). It's a polynomial in the binary sense: each bit is a coefficient, but in fact it's an integer between 0 and 255, and not a list of gf values. For more infos: http://research.swtch.com/field\n # note that the choice of generator or prime polynomial doesn't matter very much: any two finite fields of size p^n have identical structure, even if they give the individual elements different names (ie, the coefficients of the codeword will be different, but the final result will be the same: you can always correct as many errors/erasures with any choice for those parameters). That's why it makes sense to refer to all the finite fields, and all decoders based on Reed-Solomon, of size p^n as one concept: GF(p^n). It can however impact sensibly the speed (because some parameters will generate sparser tables).\n\n global GF2int_exptable, GF2int_logtable, GF2_charac, GF2_c_exp\n GF2_charac = int(2**c_exp - 1)\n GF2_c_exp = int(c_exp)\n exptable = [-1] * (GF2_charac+1) # anti-log (exponential) table. The first two elements will always be [GF2int(1), generator]\n logtable = [-1] * (GF2_charac+1) # log table, log[0] is impossible and thus unused\n\n # Construct the anti-log table\n # It's basically the cumulative product of 1 by the generator number, on and on and on until you have walked through the whole field.\n # That's why exptable is always dense (all entries are filled), but logtable may be sparse (lots of empty values, because multiple logtable's entries point to the same exptable's entry).\n g = GF2int(1)\n for i in range(GF2_charac+1): # note that the last item of exptable will always be equal to the first item in the table, because g^p==g^0 because of the modulo p (we're in a finite field!).\n exptable[i] = g # compute anti-log for this value and store it in a table\n #logtable[g] = i # compute logtable at the same time as exptable (but log[1] will always be equal to g^255, which may be weird when compared to lists of logtables online but this is equivalent)\n g = g.multiply(generator, prim, GF2_charac+1) # equivalent to: g = generator**(i+1)\n\n # Construct the log table\n # Ignore the last element of the field because fields wrap back around.\n # The log of 1 can have two values: either g^0 (the exact value change depending on parameters) or it could be 255 (g^255=1) because of the wraparound\n # Note that either way, this does not change anything any output later (ie, the ecc symbols will be the same either way).\n for i, x in enumerate(exptable[:-1]):\n logtable[x] = i\n\n # Optimization: convert to integer arrays\n GF2int_exptable = array.array('i', exptable)\n GF2int_logtable = array.array('i', logtable)\n return GF2int_exptable, GF2int_logtable", "response": "Initialize the logarithm and anti - log tables for fast computation later using the provided primitive polynomial."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a Galois Field s number into a nice polynomial", "response": "def _to_binpoly(x):\n '''Convert a Galois Field's number into a nice polynomial'''\n if x <= 0: return \"0\"\n b = 1 # init to 2^0 = 1\n c = [] # stores the degrees of each term of the polynomials\n i = 0 # counter for b = 2^i\n while x > 0:\n b = (1 << i) # generate a number power of 2: 2^0, 2^1, 2^2, ..., 2^i. Equivalent to b = 2^i\n if x & b : # then check if x is divisible by the power of 2. Equivalent to x % 2^i == 0\n # If yes, then...\n c.append(i) # append this power (i, the exponent, gives us the coefficient)\n x ^= b # and compute the remainder of x / b\n i = i+1 # increment to compute the next power of 2\n return \" + \".join([\"x^%i\" % y for y in c[::-1]])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef loads( source ):\n source = source.strip()\n assert source.startswith( '{' )\n assert source.endswith( '}' )\n source = source[1:-1]\n result = {}\n for match in attr.finditer( source ):\n key = match.group('key')\n if match.group( 'list' ) is not None:\n value = [ \n int(x) \n for x in match.group( 'list' ).strip().replace(',',' ').split() \n ]\n elif match.group( 'int' ) is not None:\n value = int( match.group( 'int' ))\n elif match.group( 'string' ) is not None:\n def deescape( match ):\n return unichr( int( match.group(0)[2:], 16 ))\n value = match.group('string').decode( 'utf-8' )\n value = escape.sub( \n deescape,\n value,\n )\n value = simple_escape.sub(\n lambda x: x.group(1),\n value,\n )\n else:\n raise RuntimeError( \"Matched something we don't know how to process:\", match.groupdict() )\n result[key] = value\n return result", "response": "Load json structure from meliae from source\n \n Supports only the required structures to support loading meliae memory dumps\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nencode a given string or list of values with reed - solomon encoding. Returns a list of values or a string.", "response": "def encode(self, message, poly=False, k=None, return_string=True):\n '''Encode a given string or list of values (between 0 and gf2_charac)\n with reed-solomon encoding. Returns a list of values (or a string if return_string is true)\n with the k message bytes and n-k parity bytes at the end.\n \n If a message is < k bytes long, it is assumed to be padded at the front\n with null bytes.\n\n The sequence returned is always n bytes long.\n\n If poly is not False, returns the encoded Polynomial object instead of\n the polynomial translated back to a string (useful for debugging)\n '''\n n = self.n\n if not k: k = self.k\n\n if len(message)>k:\n raise ValueError(\"Message length is max %d. Message was %d\" % (k,\n len(message)))\n\n # If we were given a string, convert to a list (important to support fields above 2^8)\n if isinstance(message, _str):\n message = [ord(x) for x in message]\n\n # Encode message as a polynomial:\n m = Polynomial([GF2int(x) for x in message])\n\n # Shift polynomial up by n-k by multiplying by x^(n-k) to reserve the first n-k coefficients for the ecc. This effectively pad the message with \\0 bytes for the lower coefficients (where the ecc will be placed).\n mprime = m * Polynomial([GF2int(1)] + [GF2int(0)]*(n-k))\n\n # mprime = q*g + b for some q\n # so let's find b, the code word (ecc block):\n b = mprime % self.g[k]\n\n # Subtract out b, so now c = q*g, which is a way of xoring mprime and code word b, which is a way of just saying that we append the polynomial ecc code to the original message (we replace the padded 0 bytes of mprime with the code word)\n c = mprime - b\n # Since c is a multiple of g, it has (at least) n-k roots: \u03b1^1 through\n # \u03b1^(n-k)\n\n if not poly:\n # Turn the polynomial c back into a string\n ret = self._list_rjust(c.coefficients, n, 0) # rjust is useful for the nostrip feature\n if return_string and self.gf2_charac < 256:\n ret = self._list2str(ret)\n else:\n ret = c\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nverify the codeword is valid by testing that the codeword r divides g returns True or False", "response": "def check(self, r, k=None):\n '''Verifies the codeword is valid by testing that the codeword (message+ecc) as a polynomial code divides g\n returns True/False\n '''\n n = self.n\n if not k: k = self.k\n #h = self.h[k]\n g = self.g[k]\n\n # If we were given a string, convert to a list (important to support fields above 2^8)\n if isinstance(r, _str):\n r = [ord(x) for x in r]\n\n # Turn r into a polynomial\n c = Polynomial([GF2int(x) for x in r])\n\n # This works too, but takes longer. Both checks are just as valid.\n #return (c*h)%gtimesh == Polynomial(x0=0)\n\n # Since all codewords are multiples of g, checking that codeword c divides g\n # suffices for validating a codeword.\n return c % g == Polynomial(x0=0)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfasting check if there s any error in a message + ecc.", "response": "def check_fast(self, r, k=None):\n '''Fast check if there's any error in a message+ecc. Can be used before decoding, in addition to hashes to detect if the message was tampered, or after decoding to check that the message was fully recovered.\n returns True/False\n '''\n n = self.n\n if not k: k = self.k\n #h = self.h[k]\n g = self.g[k]\n\n # If we were given a string, convert to a list (important to support fields above 2^8)\n if isinstance(r, _str):\n r = [ord(x) for x in r]\n\n # Turn r into a polynomial\n r = Polynomial([GF2int(x) for x in r])\n\n # Compute the syndromes:\n sz = self._syndromes(r, k=k)\n\n # Checking that the syndrome is all 0 is sufficient to check if there are no more any errors in the decoded message\n #return all(int(x) == 0 for x in sz)\n return sz.coefficients.count(GF2int(0)) == len(sz)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decode(self, r, nostrip=False, k=None, erasures_pos=None, only_erasures=False, return_string=True):\n '''Given a received string or byte array or list r of values between\n 0 and gf2_charac, attempts to decode it. If it's a valid codeword, or\n if there are no more than (n-k)/2 errors, the repaired message is returned.\n\n A message always has k bytes, if a message contained less it is left\n padded with null bytes. When decoded, these leading null bytes are\n stripped, but that can cause problems if decoding binary data. When\n nostrip is True, messages returned are always k bytes long. This is\n useful to make sure no data is lost when decoding binary data.\n \n Theoretically, we have R(x) = C(x) + E(x) + V(x), where R is the received message, C is the correct message without errors nor erasures, E are the errors and V the erasures. Thus the goal is to compute E and V from R, so that we can compute: C(x) = R(x) - E(x) - V(x), and then we have our original message! The main problem of decoding is to solve the so-called Key Equation, here we use Berlekamp-Massey.\n \n When stated in the language of spectral estimation, consists of a Fourier transform (syndrome computer), followed by a spectral analysis (Berlekamp-Massey or Euclidian algorithm), followed by an inverse Fourier transform (Chien search).\n (see Blahut, \"Algebraic Codes for Data Transmission\", 2003, chapter 7.6 Decoding in Time Domain).\n '''\n n = self.n\n if not k: k = self.k\n\n # If we were given a string, convert to a list (important to support fields above 2^8)\n if isinstance(r, _str):\n r = [ord(x) for x in r]\n\n # Turn r into a polynomial\n rp = Polynomial([GF2int(x) for x in r])\n\n if erasures_pos:\n # Convert string positions to coefficients positions for the algebra to work (see _find_erasures_locator(), ecc characters represent the first coefficients while the message is put last, so it's exactly the reverse of the string positions where the message is first and the ecc is last, thus it's just like if you read the message+ecc string in reverse)\n erasures_pos = [len(r)-1-x for x in erasures_pos]\n # Set erasures characters to null bytes\n # Note that you can just leave the original characters as they are, you don't need to set erased characters to null bytes for the decoding to work, but note that it won't help either (ie, fake erasures, meaning characters that were detected as erasures but actually aren't, will still \"consume\" one ecc symbol, even if you don't set them to null byte, this is because the syndrome is limited to n-k and thus you can't decode above this bound without a clever trick).\n # Example string containing a fake erasure: \"hello sam\" -> \"ooooo sam\" with erasures_pos = [0, 1, 2, 3, 4]. Here in fact the last erasure is fake because the original character also was \"o\" so if we detect \"o\" as an erasure, we will end up with one fake erasure. But setting it to null byte or not, it will still use up one ecc symbol, it will always be counted as a real erasure. If you're below the n-k bound, then the doceding will be ok. If you're above, then you can't do anything, the decoding won't work. Maybe todo: try to find a clever list decoding algorithm to account for fake erasures....\n # Note: commented out so that the resulting omega (error evaluator polynomial) is the same as the erasure evaluator polynomial when decoding the same number of errors or erasures (ie, decoding 3 erasures only will give the same result as 3 errors only, with of course the errors/erasures on the same characters).\n #for erasure in erasures_pos:\n #rp[erasure] = GF2int(0)\n\n # Compute the syndromes:\n sz = self._syndromes(rp, k=k)\n\n if sz.coefficients.count(GF2int(0)) == len(sz): # the code is already valid, there's nothing to do\n # The last n-k bytes are parity\n ret = r[:-(n-k)]\n ecc = r[-(n-k):]\n if not nostrip:\n ret = self._list_lstrip(r[:-(n-k)], 0)\n if return_string and self.gf2_charac < 256:\n ret = self._list2str(ret)\n ecc = self._list2str(ecc) \n return ret, ecc\n\n # Erasures locator polynomial computation\n erasures_loc = None\n erasures_eval = None\n erasures_count = 0\n if erasures_pos:\n erasures_count = len(erasures_pos)\n # Compute the erasure locator polynomial\n erasures_loc = self._find_erasures_locator(erasures_pos)\n # Compute the erasure evaluator polynomial\n erasures_eval = self._find_error_evaluator(sz, erasures_loc, k=k)\n\n if only_erasures:\n sigma = erasures_loc\n omega = erasures_eval\n else:\n # Find the error locator polynomial and error evaluator polynomial\n # using the Berlekamp-Massey algorithm\n # if erasures were supplied, BM will generate the errata (errors-and-erasures) locator and evaluator polynomials\n sigma, omega = self._berlekamp_massey(sz, k=k, erasures_loc=erasures_loc, erasures_eval=erasures_eval, erasures_count=erasures_count)\n omega = self._find_error_evaluator(sz, sigma, k=k) # we want to make sure that omega is correct (we know that sigma is always correct, but omega not really)\n\n # Now use Chien's procedure to find the error locations\n # j is an array of integers representing the positions of the errors, 0\n # being the rightmost byte\n # X is a corresponding array of GF(2^8) values where X_i = alpha^(j_i)\n X, j = self._chien_search(sigma)\n\n # Sanity check: Cannot guarantee correct decoding of more than n-k errata (Singleton Bound, n-k being the minimum distance), and we cannot even check if it's correct (the syndrome will always be all 0 if we try to decode above the bound), thus it's better to just return the input as-is.\n if len(j) > n-k:\n ret = r[:-(n-k)]\n ecc = r[-(n-k):]\n if not nostrip:\n ret = self._list_lstrip(r[:-(n-k)], 0)\n if return_string and self.gf2_charac < 256:\n ret = self._list2str(ret)\n ecc = self._list2str(ecc) \n return ret, ecc\n\n # And finally, find the error magnitudes with Forney's Formula\n # Y is an array of GF(2^8) values corresponding to the error magnitude\n # at the position given by the j array\n Y = self._forney(omega, X)\n\n # Put the error and locations together to form the error polynomial\n # Note that an alternative would be to compute the error-spectrum polynomial E(x) which satisfies E(x)*Sigma(x) = 0 (mod x^n - 1) = Omega(x)(x^n - 1) -- see Blahut, Algebraic codes for data transmission\n Elist = [GF2int(0)] * self.gf2_charac\n if len(Y) >= len(j): # failsafe: if the number of erratas is higher than the number of coefficients in the magnitude polynomial, we failed!\n for i in _range(self.gf2_charac): # FIXME? is this really necessary to go to self.gf2_charac? len(rp) wouldn't be just enough? (since the goal is anyway to substract E to rp)\n if i in j:\n Elist[i] = Y[j.index(i)]\n E = Polynomial( Elist[::-1] ) # reverse the list because we used the coefficient degrees (j) instead of the error positions\n else:\n E = Polynomial()\n\n # And we get our real codeword!\n c = rp - E # Remember what we wrote above: R(x) = C(x) + E(x), so here to get back the original codeword C(x) = R(x) - E(x) ! (V(x) the erasures are here is included inside E(x))\n\n if len(c) > len(r): c = rp # failsafe: in case the correction went totally wrong (we repaired padded null bytes instead of the message! thus we end up with a longer message than what we should have), then we just return the uncorrected message. Note: we compare the length of c with r on purpose, that's not an error: if we compare with rp, if the first few characters were erased (null bytes) in r, then in rp the Polynomial will automatically skip them, thus the length will always be smaller in that case.\n\n # Split the polynomial into two parts: the corrected message and the corrected ecc\n ret = c.coefficients[:-(n-k)]\n ecc = c.coefficients[-(n-k):]\n\n if nostrip:\n # Polynomial objects don't store leading 0 coefficients, so we\n # actually need to pad this to k bytes\n ret = self._list_rjust(ret, k, 0)\n\n if return_string and self.gf2_charac < 256: # automatically disable return_string if the field is above 255 (chr would fail, so it's up to the user to define the mapping)\n # Form it back into a string \n ret = self._list2str(ret)\n ecc = self._list2str(ecc)\n\n return ret, ecc", "response": "Given a received string or byte array or list r attempts to decode it."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _list_lstrip(self, L, val=0):\n '''Left strip the specified value'''\n for i in _range(len(L)):\n if L[i] != val:\n return L[i:]", "response": "Left strip the specified value from the specified list."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nleaves pad with the specified value to obtain a list of the specified width", "response": "def _list_rjust(self, L, width, fillchar=0):\n '''Left pad with the specified value to obtain a list of the specified width (length)'''\n length = max(0, width - len(L))\n return [fillchar]*length + L"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _syndromes(self, r, k=None):\n '''Given the received codeword r in the form of a Polynomial object,\n computes the syndromes and returns the syndrome polynomial.\n Mathematically, it's essentially equivalent to a Fourrier Transform (Chien search being the inverse).\n '''\n n = self.n\n if not k: k = self.k\n # Note the + [GF2int(0)] : we add a 0 coefficient for the lowest degree (the constant). This effectively shifts the syndrome, and will shift every computations depending on the syndromes (such as the errors locator polynomial, errors evaluator polynomial, etc. but not the errors positions).\n # This is not necessary as anyway syndromes are defined such as there are only non-zero coefficients (the only 0 is the shift of the constant here) and subsequent computations will/must account for the shift by skipping the first iteration (eg, the often seen range(1, n-k+1)), but you can also avoid prepending the 0 coeff and adapt every subsequent computations to start from 0 instead of 1.\n return Polynomial( [r.evaluate( GF2int(self.generator)**(l+self.fcr) ) for l in _range(n-k-1, -1, -1)] + [GF2int(0)], keep_zero=True )", "response": "Given the received codeword r computes the syndromes and returns the syndrome polynomial."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfinding the erasures locator polynomial from the erasures positions.", "response": "def _find_erasures_locator(self, erasures_pos):\n '''Compute the erasures locator polynomial from the erasures positions (the positions must be relative to the x coefficient, eg: \"hello worldxxxxxxxxx\" is tampered to \"h_ll_ worldxxxxxxxxx\" with xxxxxxxxx being the ecc of length n-k=9, here the string positions are [1, 4], but the coefficients are reversed since the ecc characters are placed as the first coefficients of the polynomial, thus the coefficients of the erased characters are n-1 - [1, 4] = [18, 15] = erasures_loc to be specified as an argument.'''\n # See: http://ocw.usu.edu/Electrical_and_Computer_Engineering/Error_Control_Coding/lecture7.pdf and Blahut, Richard E. \"Transform techniques for error control codes.\" IBM Journal of Research and development 23.3 (1979): 299-315. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.600&rep=rep1&type=pdf and also a MatLab implementation here: http://www.mathworks.com/matlabcentral/fileexchange/23567-reed-solomon-errors-and-erasures-decoder/content//RS_E_E_DEC.m\n erasures_loc = Polynomial([GF2int(1)]) # just to init because we will multiply, so it must be 1 so that the multiplication starts correctly without nulling any term\n # erasures_loc is very simple to compute: erasures_loc = prod(1 - x*alpha[j]**i) for i in erasures_pos and where alpha is the alpha chosen to evaluate polynomials (here in this library it's gf(3)). To generate c*x where c is a constant, we simply generate a Polynomial([c, 0]) where 0 is the constant and c is positionned to be the coefficient for x^1. See https://en.wikipedia.org/wiki/Forney_algorithm#Erasures\n for i in erasures_pos:\n erasures_loc = erasures_loc * (Polynomial([GF2int(1)]) - Polynomial([GF2int(self.generator)**i, 0]))\n return erasures_loc"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _berlekamp_massey(self, s, k=None, erasures_loc=None, erasures_eval=None, erasures_count=0):\n '''Computes and returns the errata (errors+erasures) locator polynomial (sigma) and the\n error evaluator polynomial (omega) at the same time.\n If the erasures locator is specified, we will return an errors-and-erasures locator polynomial and an errors-and-erasures evaluator polynomial, else it will compute only errors. With erasures in addition to errors, it can simultaneously decode up to v+2e <= (n-k) where v is the number of erasures and e the number of errors.\n Mathematically speaking, this is equivalent to a spectral analysis (see Blahut, \"Algebraic Codes for Data Transmission\", 2003, chapter 7.6 Decoding in Time Domain).\n The parameter s is the syndrome polynomial (syndromes encoded in a\n generator function) as returned by _syndromes.\n\n Notes:\n The error polynomial:\n E(x) = E_0 + E_1 x + ... + E_(n-1) x^(n-1)\n\n j_1, j_2, ..., j_s are the error positions. (There are at most s\n errors)\n\n Error location X_i is defined: X_i = \u03b1^(j_i)\n that is, the power of \u03b1 (alpha) corresponding to the error location\n\n Error magnitude Y_i is defined: E_(j_i)\n that is, the coefficient in the error polynomial at position j_i\n\n Error locator polynomial:\n sigma(z) = Product( 1 - X_i * z, i=1..s )\n roots are the reciprocals of the error locations\n ( 1/X_1, 1/X_2, ...)\n\n Error evaluator polynomial omega(z) is here computed at the same time as sigma, but it can also be constructed afterwards using the syndrome and sigma (see _find_error_evaluator() method).\n\n It can be seen that the algorithm tries to iteratively solve for the error locator polynomial by\n solving one equation after another and updating the error locator polynomial. If it turns out that it\n cannot solve the equation at some step, then it computes the error and weights it by the last\n non-zero discriminant found, and delays the weighted result to increase the polynomial degree\n by 1. Ref: \"Reed Solomon Decoder: TMS320C64x Implementation\" by Jagadeesh Sankaran, December 2000, Application Report SPRA686\n\n The best paper I found describing the BM algorithm for errata (errors-and-erasures) evaluator computation is in \"Algebraic Codes for Data Transmission\", Richard E. Blahut, 2003.\n '''\n # For errors-and-erasures decoding, see: \"Algebraic Codes for Data Transmission\", Richard E. Blahut, 2003 and (but it's less complete): Blahut, Richard E. \"Transform techniques for error control codes.\" IBM Journal of Research and development 23.3 (1979): 299-315. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.600&rep=rep1&type=pdf and also a MatLab implementation here: http://www.mathworks.com/matlabcentral/fileexchange/23567-reed-solomon-errors-and-erasures-decoder/content//RS_E_E_DEC.m\n # also see: Blahut, Richard E. \"A universal Reed-Solomon decoder.\" IBM Journal of Research and Development 28.2 (1984): 150-158. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.84.2084&rep=rep1&type=pdf\n # and another good alternative book with concrete programming examples: Jiang, Yuan. A practical guide to error-control coding using Matlab. Artech House, 2010.\n n = self.n\n if not k: k = self.k\n\n # Initialize, depending on if we include erasures or not:\n if erasures_loc:\n sigma = [ Polynomial(erasures_loc.coefficients) ] # copy erasures_loc by creating a new Polynomial, so that we initialize the errata locator polynomial with the erasures locator polynomial.\n B = [ Polynomial(erasures_loc.coefficients) ]\n omega = [ Polynomial(erasures_eval.coefficients) ] # to compute omega (the evaluator polynomial) at the same time, we also need to initialize it with the partial erasures evaluator polynomial\n A = [ Polynomial(erasures_eval.coefficients) ] # TODO: fix the initial value of the evaluator support polynomial, because currently the final omega is not correct (it contains higher order terms that should be removed by the end of BM)\n else:\n sigma = [ Polynomial([GF2int(1)]) ] # error locator polynomial. Also called Lambda in other notations.\n B = [ Polynomial([GF2int(1)]) ] # this is the error locator support/secondary polynomial, which is a funky way to say that it's just a temporary variable that will help us construct sigma, the error locator polynomial\n omega = [ Polynomial([GF2int(1)]) ] # error evaluator polynomial. We don't need to initialize it with erasures_loc, it will still work, because Delta is computed using sigma, which itself is correctly initialized with erasures if needed.\n A = [ Polynomial([GF2int(0)]) ] # this is the error evaluator support/secondary polynomial, to help us construct omega\n L = [ 0 ] # update flag: necessary variable to check when updating is necessary and to check bounds (to avoid wrongly eliminating the higher order terms). For more infos, see https://www.cs.duke.edu/courses/spring11/cps296.3/decoding_rs.pdf\n M = [ 0 ] # optional variable to check bounds (so that we do not mistakenly overwrite the higher order terms). This is not necessary, it's only an additional safe check. For more infos, see the presentation decoding_rs.pdf by Andrew Brown in the doc folder.\n\n # Fix the syndrome shifting: when computing the syndrome, some implementations may prepend a 0 coefficient for the lowest degree term (the constant). This is a case of syndrome shifting, thus the syndrome will be bigger than the number of ecc symbols (I don't know what purpose serves this shifting). If that's the case, then we need to account for the syndrome shifting when we use the syndrome such as inside BM, by skipping those prepended coefficients.\n # Another way to detect the shifting is to detect the 0 coefficients: by definition, a syndrome does not contain any 0 coefficient (except if there are no errors/erasures, in this case they are all 0). This however doesn't work with the modified Forney syndrome (that we do not use in this lib but it may be implemented in the future), which set to 0 the coefficients corresponding to erasures, leaving only the coefficients corresponding to errors.\n synd_shift = 0\n if len(s) > (n-k): synd_shift = len(s) - (n-k)\n\n # Polynomial constants:\n ONE = Polynomial(z0=GF2int(1))\n ZERO = Polynomial(z0=GF2int(0))\n Z = Polynomial(z1=GF2int(1)) # used to shift polynomials, simply multiply your poly * Z to shift\n\n # Precaching\n s2 = ONE + s\n\n # Iteratively compute the polynomials n-k-erasures_count times. The last ones will be correct (since the algorithm refines the error/errata locator polynomial iteratively depending on the discrepancy, which is kind of a difference-from-correctness measure).\n for l in _range(0, n-k-erasures_count): # skip the first erasures_count iterations because we already computed the partial errata locator polynomial (by initializing with the erasures locator polynomial)\n K = erasures_count+l+synd_shift # skip the FIRST erasures_count iterations (not the last iterations, that's very important!)\n\n # Goal for each iteration: Compute sigma[l+1] and omega[l+1] such that\n # (1 + s)*sigma[l] == omega[l] in mod z^(K)\n\n # For this particular loop iteration, we have sigma[l] and omega[l],\n # and are computing sigma[l+1] and omega[l+1]\n\n # First find Delta, the non-zero coefficient of z^(K) in\n # (1 + s) * sigma[l]\n # Note that adding 1 to the syndrome s is not really necessary, you can do as well without.\n # This delta is valid for l (this iteration) only\n Delta = ( s2 * sigma[l] ).get_coefficient(K) # Delta is also known as the Discrepancy, and is always a scalar (not a polynomial).\n # Make it a polynomial of degree 0, just for ease of computation with polynomials sigma and omega.\n Delta = Polynomial(x0=Delta)\n\n # Can now compute sigma[l+1] and omega[l+1] from\n # sigma[l], omega[l], B[l], A[l], and Delta\n sigma.append( sigma[l] - Delta * Z * B[l] )\n omega.append( omega[l] - Delta * Z * A[l] )\n\n # Now compute the next support polynomials B and A\n # There are two ways to do this\n # This is based on a messy case analysis on the degrees of the four polynomials sigma, omega, A and B in order to minimize the degrees of A and B. For more infos, see https://www.cs.duke.edu/courses/spring10/cps296.3/decoding_rs_scribe.pdf\n # In fact it ensures that the degree of the final polynomials aren't too large.\n if Delta == ZERO or 2*L[l] > K+erasures_count \\\n or (2*L[l] == K+erasures_count and M[l] == 0):\n #if Delta == ZERO or len(sigma[l+1]) <= len(sigma[l]): # another way to compute when to update, and it doesn't require to maintain the update flag L\n # Rule A\n B.append( Z * B[l] )\n A.append( Z * A[l] )\n L.append( L[l] )\n M.append( M[l] )\n\n elif (Delta != ZERO and 2*L[l] < K+erasures_count) \\\n or (2*L[l] == K+erasures_count and M[l] != 0):\n # elif Delta != ZERO and len(sigma[l+1]) > len(sigma[l]): # another way to compute when to update, and it doesn't require to maintain the update flag L\n # Rule B\n B.append( sigma[l] // Delta )\n A.append( omega[l] // Delta )\n L.append( K - L[l] ) # the update flag L is tricky: in Blahut's schema, it's mandatory to use `L = K - L - erasures_count` (and indeed in a previous draft of this function, if you forgot to do `- erasures_count` it would lead to correcting only 2*(errors+erasures) <= (n-k) instead of 2*errors+erasures <= (n-k)), but in this latest draft, this will lead to a wrong decoding in some cases where it should correctly decode! Thus you should try with and without `- erasures_count` to update L on your own implementation and see which one works OK without producing wrong decoding failures.\n M.append( 1 - M[l] )\n\n else:\n raise Exception(\"Code shouldn't have gotten here\")\n\n # Hack to fix the simultaneous computation of omega, the errata evaluator polynomial: because A (the errata evaluator support polynomial) is not correctly initialized (I could not find any info in academic papers). So at the end, we get the correct errata evaluator polynomial omega + some higher order terms that should not be present, but since we know that sigma is always correct and the maximum degree should be the same as omega, we can fix omega by truncating too high order terms.\n if omega[-1].degree > sigma[-1].degree: omega[-1] = Polynomial(omega[-1].coefficients[-(sigma[-1].degree+1):])\n\n # Debuglines, uncomment to show the result of every iterations\n #print \"SIGMA BM\"\n #for i,x in enumerate(sigma):\n #print i, \":\", x\n\n # Return the last result of the iterations (since BM compute iteratively, the last iteration being correct - it may already be before, but we're not sure)\n return sigma[-1], omega[-1]", "response": "Computes and returns the errata locator polynomial and error evaluator polynomial at the same time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _berlekamp_massey_fast(self, s, k=None, erasures_loc=None, erasures_eval=None, erasures_count=0):\n '''Faster implementation of errata (errors-and-erasures) Berlekamp-Massey.\n Returns the error locator polynomial (sigma) and the\n error evaluator polynomial (omega) with a faster implementation.\n '''\n n = self.n\n if not k: k = self.k\n\n # Initialize, depending on if we include erasures or not:\n if erasures_loc:\n sigma = Polynomial(erasures_loc.coefficients) # copy erasures_loc by creating a new Polynomial, so that we initialize the errata locator polynomial with the erasures locator polynomial.\n sigmaprev = Polynomial(sigma.coefficients)\n B = Polynomial(sigma.coefficients)\n omega = Polynomial(erasures_eval.coefficients) # to compute omega (the evaluator polynomial) at the same time, we also need to initialize it with the partial erasures evaluator polynomial\n omegaprev = Polynomial(omega.coefficients)\n A = Polynomial(omega.coefficients) # TODO: fix the initial value of the evaluator support polynomial, because currently the final omega is not correct (it contains higher order terms that should be removed by the end of BM)\n else:\n sigma = sigmaprev = Polynomial([GF2int(1)]) # error locator polynomial. Also called Lambda in other notations.\n sigmaprev = Polynomial([GF2int(1)]) # we need the previous iteration to compute the next value of the support polynomials\n B = Polynomial([GF2int(1)]) # this is the error locator support/secondary polynomial, which is a funky way to say that it's just a temporary variable that will help us construct sigma, the error locator polynomial\n omega = omegaprev = Polynomial([GF2int(1)]) # error evaluator polynomial. We don't need to initialize it with erasures_loc, it will still work, because Delta is computed using sigma, which itself is correctly initialized with erasures if needed.\n omegaprev = Polynomial([GF2int(1)])\n A = Polynomial([GF2int(0)]) # this is the error evaluator support/secondary polynomial, to help us construct omega\n L = 0 # update flag: necessary variable to check when updating is necessary and to check bounds (to avoid wrongly eliminating the higher order terms). For more infos, see https://www.cs.duke.edu/courses/spring11/cps296.3/decoding_rs.pdf\n #M = 0 # optional variable to check bounds (so that we do not mistakenly overwrite the higher order terms). This is not necessary, it's only an additional safe check. For more infos, see the presentation decoding_rs.pdf by Andrew Brown in the doc folder.\n\n # Fix the syndrome shifting: when computing the syndrome, some implementations may prepend a 0 coefficient for the lowest degree term (the constant). This is a case of syndrome shifting, thus the syndrome will be bigger than the number of ecc symbols (I don't know what purpose serves this shifting). If that's the case, then we need to account for the syndrome shifting when we use the syndrome such as inside BM, by skipping those prepended coefficients.\n # Another way to detect the shifting is to detect the 0 coefficients: by definition, a syndrome does not contain any 0 coefficient (except if there are no errors/erasures, in this case they are all 0). This however doesn't work with the modified Forney syndrome (that we do not use in this lib but it may be implemented in the future), which set to 0 the coefficients corresponding to erasures, leaving only the coefficients corresponding to errors.\n synd_shift = 0\n if len(s) > (n-k): synd_shift = len(s) - (n-k)\n\n # Polynomial constants:\n ONE = Polynomial([GF2int(1)])\n ZERO = GF2int(0)\n Z = Polynomial([GF2int(1), GF2int(0)]) # used to shift polynomials, simply multiply your poly * Z to shift\n\n # Precaching\n s2 = ONE+s\n\n # Iteratively compute the polynomials n-k-erasures_count times. The last ones will be correct (since the algorithm refines the error/errata locator polynomial iteratively depending on the discrepancy, which is kind of a difference-from-correctness measure).\n for l in _range(n-k-erasures_count): # skip the first erasures_count iterations because we already computed the partial errata locator polynomial (by initializing with the erasures locator polynomial)\n K = erasures_count+l+synd_shift # skip the FIRST erasures_count iterations (not the last iterations, that's very important!)\n\n # Goal for each iteration: Compute sigma[l+1] and omega[l+1] such that\n # (1 + s)*sigma[l] == omega[l] in mod z^(K)\n\n # For this particular loop iteration, we have sigma[l] and omega[l],\n # and are computing sigma[l+1] and omega[l+1]\n\n # First find Delta, the non-zero coefficient of z^(K) in\n # (1 + s) * sigma[l]\n # Note that adding 1 to the syndrome s is not really necessary, you can do as well without.\n # This delta is valid for l (this iteration) only\n Delta = s2.mul_at(sigma, K) # Delta is also known as the Discrepancy, and is always a scalar (not a polynomial). We just need one coefficient at a specific degree, so we can optimize by computing only the polynomial multiplication at this term, and skip the others.\n\n # Can now compute sigma[l+1] and omega[l+1] from\n # sigma[l], omega[l], B[l], A[l], and Delta\n sigmaprev = sigma\n omegaprev = omega\n sigma = sigma - (Z * B).scale(Delta)\n omega = omega - (Z * A).scale(Delta)\n\n # Now compute the next support polynomials B and A\n # There are two ways to do this\n # This is based on a messy case analysis on the degrees of the four polynomials sigma, omega, A and B in order to minimize the degrees of A and B. For more infos, see https://www.cs.duke.edu/courses/spring10/cps296.3/decoding_rs_scribe.pdf\n # In fact it ensures that the degree of the final polynomials aren't too large.\n if Delta == ZERO or 2*L > K+erasures_count:\n #or (2*L == K+erasures_count and M == 0):\n #if Delta == ZERO or len(sigma) <= len(sigmaprev): # another way to compute when to update, and it doesn't require to maintain the update flag L\n # Rule A\n B = Z * B\n A = Z * A\n #L = L\n #M = M\n\n else:\n #elif (Delta != ZERO and 2*L < K+erasures_count) \\\n # or (2*L == K+erasures_count and M != 0):\n # elif Delta != ZERO and len(sigma) > len(sigmaprev): # another way to compute when to update, and it doesn't require to maintain the update flag L\n # Rule B\n B = sigmaprev.scale(Delta.inverse())\n A = omegaprev.scale(Delta.inverse())\n L = K - L # the update flag L is tricky: in Blahut's schema, it's mandatory to use `L = K - L - erasures_count` (and indeed in a previous draft of this function, if you forgot to do `- erasures_count` it would lead to correcting only 2*(errors+erasures) <= (n-k) instead of 2*errors+erasures <= (n-k)), but in this latest draft, this will lead to a wrong decoding in some cases where it should correctly decode! Thus you should try with and without `- erasures_count` to update L on your own implementation and see which one works OK without producing wrong decoding failures.\n #M = 1 - M\n\n #else:\n # raise Exception(\"Code shouldn't have gotten here\")\n\n # Hack to fix the simultaneous computation of omega, the errata evaluator polynomial: because A (the errata evaluator support polynomial) is not correctly initialized (I could not find any info in academic papers). So at the end, we get the correct errata evaluator polynomial omega + some higher order terms that should not be present, but since we know that sigma is always correct and the maximum degree should be the same as omega, we can fix omega by truncating too high order terms.\n if omega.degree > sigma.degree: omega = Polynomial(omega.coefficients[-(sigma.degree+1):])\n\n # Return the last result of the iterations (since BM compute iteratively, the last iteration being correct - it may already be before, but we're not sure)\n return sigma, omega", "response": "Faster implementation of errata ( errors - and - erasures ) Berlekamp - Massey."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _find_error_evaluator(self, synd, sigma, k=None):\n '''Compute the error (or erasures if you supply sigma=erasures locator polynomial) evaluator polynomial Omega from the syndrome and the error/erasures/errata locator Sigma. Omega is already computed at the same time as Sigma inside the Berlekamp-Massey implemented above, but in case you modify Sigma, you can recompute Omega afterwards using this method, or just ensure that Omega computed by BM is correct given Sigma (as long as syndrome and sigma are correct, omega will be correct).'''\n n = self.n\n if not k: k = self.k\n\n # Omega(x) = [ (1 + Synd(x)) * Error_loc(x) ] mod x^(n-k+1)\n # NOTE: I don't know why we do 1+Synd(x) here, from docs it seems just Synd(x) is enough (and in practice if you remove the \"ONE +\" it will still decode correcty) as advised by Blahut in Algebraic Codes for Data Transmission, but it seems it's an implementation detail here.\n #ONE = Polynomial([GF2int(1)])\n #return ((ONE + synd) * sigma) % Polynomial([GF2int(1)] + [GF2int(0)] * (n-k+1)) # NOT CORRECT: in practice it works flawlessly with this implementation (primitive polynomial = 3), but if you use another primitive like in reedsolo lib, it doesn't work! Thus, I guess that adding ONE is not correct for the general case.\n return (synd * sigma) % Polynomial([GF2int(1)] + [GF2int(0)] * (n-k+1))", "response": "Compute the error or erasures evaluator polynomial Omega from the syndrome and the error / errata locator polynomial Omega from the syndrome and the error / erasures locator polynomial Sigma."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncomputing the error evaluator polynomial Omega from the syndrome and the error / errata locator polynomial Omega.", "response": "def _find_error_evaluator_fast(self, synd, sigma, k=None):\n '''Compute the error (or erasures if you supply sigma=erasures locator polynomial) evaluator polynomial Omega from the syndrome and the error/erasures/errata locator Sigma. Omega is already computed at the same time as Sigma inside the Berlekamp-Massey implemented above, but in case you modify Sigma, you can recompute Omega afterwards using this method, or just ensure that Omega computed by BM is correct given Sigma (as long as syndrome and sigma are correct, omega will be correct).'''\n n = self.n\n if not k: k = self.k\n\n # Omega(x) = [ Synd(x) * Error_loc(x) ] mod x^(n-k+1) -- From Blahut, Algebraic codes for data transmission, 2003\n return (synd * sigma)._gffastmod(Polynomial([GF2int(1)] + [GF2int(0)] * (n-k+1)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _chien_search(self, sigma):\n '''Recall the definition of sigma, it has s roots. To find them, this\n function evaluates sigma at all 2^(c_exp-1) (ie: 255 for GF(2^8)) non-zero points to find the roots\n The inverse of the roots are X_i, the error locations\n\n Returns a list X of error locations, and a corresponding list j of\n error positions (the discrete log of the corresponding X value) The\n lists are up to s elements large.\n \n This is essentially an inverse Fourrier transform.\n\n Important technical math note: This implementation is not actually\n Chien's search. Chien's search is a way to evaluate the polynomial\n such that each evaluation only takes constant time. This here simply\n does 255 evaluations straight up, which is much less efficient.\n Said differently, we simply do a bruteforce search by trial substitution to find the zeros of this polynomial, which identifies the error locations.\n '''\n # TODO: find a more efficient algorithm, this is the slowest part of the whole decoding process (~2.5 ms, while any other part is only ~400microsec). Could try the Pruned FFT from \"Simple Algorithms for BCH Decoding\", by Jonathan Hong and Martin Vetterli, IEEE Transactions on Communications, Vol.43, No.8, August 1995\n X = []\n j = []\n p = GF2int(self.generator)\n # Try for each possible location\n for l in _range(1, self.gf2_charac+1): # range 1:256 is important: if you use range 0:255, if the last byte of the ecc symbols is corrupted, it won't be correctable! You need to use the range 1,256 to include this last byte.\n #l = (i+self.fcr)\n # These evaluations could be more efficient, but oh well\n if sigma.evaluate( p**l ) == 0: # If it's 0, then bingo! It's an error location\n # Compute the error location polynomial X (will be directly used to compute the errors magnitudes inside the Forney algorithm)\n X.append( p**(-l) )\n # Compute the coefficient position (not the error position, it's actually the reverse: we compute the degree of the term where the error is located. To get the error position, just compute n-1-j).\n # This is different than the notes, I think the notes were in error\n # Notes said j values were just l, when it's actually 255-l\n j.append(self.gf2_charac - l)\n\n # Sanity check: the number of errors/errata positions found should be exactly the same as the length of the errata locator polynomial\n errs_nb = len(sigma) - 1 # compute the exact number of errors/errata that this error locator should find\n if len(j) != errs_nb:\n raise RSCodecError(\"Too many (or few) errors found by Chien Search for the errata locator polynomial!\")\n\n return X, j", "response": "Recall the definition of sigma it has s roots. To find them it has s roots. To find them it has s roots. To find them it has s roots and the error positions of the error locations and j of the error positions of the roots."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _chien_search_fast(self, sigma):\n '''Real chien search, we reuse the previous polynomial evaluation and just multiply by a constant polynomial. This should be faster, but it seems it's just the same speed as the other bruteforce version. However, it should easily be parallelizable.'''\n # TODO: doesn't work when fcr is different than 1 (X values are incorrectly \"shifted\"...)\n # TODO: try to mix this approach with the optimized walk on only interesting values, implemented in _chien_search_faster()\n X = []\n j = []\n p = GF2int(self.generator)\n if not hasattr(self, 'const_poly'): self.const_poly = [GF2int(self.generator)**(i+self.fcr) for i in _range(self.gf2_charac, -1, -1)] # constant polynomial that will allow us to update the previous polynomial evaluation to get the next one\n const_poly = self.const_poly # caching for more efficiency since it never changes\n ev_poly, ev = sigma.evaluate_array( p**1 ) # compute the first polynomial evaluation\n # Try for each possible location\n for l in _range(1, self.gf2_charac+1): # range 1:256 is important: if you use range 0:255, if the last byte of the ecc symbols is corrupted, it won't be correctable! You need to use the range 1,256 to include this last byte.\n #l = (i+self.fcr)\n\n # Check if it's a root for the polynomial\n if ev == 0: # If it's 0, then bingo! It's an error location\n # Compute the error location polynomial X (will be directly used to compute the errors magnitudes inside the Forney algorithm)\n X.append( p**(-l) )\n # Compute the coefficient position (not the error position, it's actually the reverse: we compute the degree of the term where the error is located. To get the error position, just compute n-1-j).\n # This is different than the notes, I think the notes were in error\n # Notes said j values were just l, when it's actually 255-l\n j.append(self.gf2_charac - l)\n\n # Update the polynomial evaluation for the next iteration\n # we simply multiply each term[k] with alpha^k (where here alpha = p = GF2int(generator)).\n # For more info, see the presentation by Andrew Brown, or this one: http://web.ntpu.edu.tw/~yshan/BCH_decoding.pdf\n # TODO: parallelize this loop\n for i in _range(1, len(ev_poly)+1): # TODO: maybe the fcr != 1 fix should be put here?\n ev_poly[-i] *= const_poly[-i]\n # Compute the new evaluation by just summing\n ev = sum(ev_poly)\n\n return X, j", "response": "Real chien search for the next set of ecc symbols."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncompute the error magnitudes for a given polynomial X and k.", "response": "def _old_forney(self, omega, X, k=None):\n '''Computes the error magnitudes (only works with errors or erasures under t = floor((n-k)/2), not with erasures above (n-k)//2)'''\n # XXX Is floor division okay here? Should this be ceiling?\n if not k: k = self.k\n t = (self.n - k) // 2\n\n Y = []\n\n for l, Xl in enumerate(X):\n\n # Compute the sequence product and multiply its inverse in\n prod = GF2int(1) # just to init the product (1 is the neutral term for multiplication)\n Xl_inv = Xl.inverse()\n for ji in _range(t): # do not change to _range(len(X)) as can be seen in some papers, it won't give the correct result! (sometimes yes, but not always)\n if ji == l:\n continue\n if ji < len(X):\n Xj = X[ji]\n else: # if above the maximum degree of the polynomial, then all coefficients above are just 0 (that's logical...)\n Xj = GF2int(0)\n prod = prod * (Xl - Xj)\n #if (ji != l):\n # prod = prod * (GF2int(1) - X[ji]*(Xl.inverse()))\n\n # Compute Yl\n Yl = Xl**t * omega.evaluate(Xl_inv) * Xl_inv * prod.inverse()\n\n Y.append(Yl)\n return Y"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _forney(self, omega, X):\n '''Computes the error magnitudes. Works also with erasures and errors+erasures beyond the (n-k)//2 bound, here the bound is 2*e+v <= (n-k-1) with e the number of errors and v the number of erasures.'''\n # XXX Is floor division okay here? Should this be ceiling?\n\n Y = [] # the final result, the error/erasures polynomial (contain the values that we should minus on the received message to get the repaired message)\n Xlength = len(X)\n for l, Xl in enumerate(X):\n\n Xl_inv = Xl.inverse()\n\n # Compute the formal derivative of the error locator polynomial (see Blahut, Algebraic codes for data transmission, pp 196-197).\n # the formal derivative of the errata locator is used as the denominator of the Forney Algorithm, which simply says that the ith error value is given by error_evaluator(gf_inverse(Xi)) / error_locator_derivative(gf_inverse(Xi)). See Blahut, Algebraic codes for data transmission, pp 196-197.\n sigma_prime_tmp = [1 - Xl_inv * X[j] for j in _range(Xlength) if j != l] # TODO? maybe a faster way would be to precompute sigma_prime = sigma[len(sigma) & 1:len(sigma):2] and then just do sigma_prime.evaluate(X[j]) ? (like in reedsolo.py)\n # compute the product\n sigma_prime = 1\n for coef in sigma_prime_tmp:\n sigma_prime = sigma_prime * coef\n # equivalent to: sigma_prime = functools.reduce(mul, sigma_prime, 1)\n\n # Compute Yl\n # This is a more faithful translation of the theoretical equation contrary to the old forney method. Here it is exactly copy/pasted from the included presentation decoding_rs.pdf: Yl = omega(Xl.inverse()) / prod(1 - Xj*Xl.inverse()) for j in len(X) (in the paper it's for j in s, but it's useless when len(X) < s because we compute neutral terms 1 for nothing, and wrong when correcting more than s erasures or erasures+errors since it prevents computing all required terms).\n # Thus here this method works with erasures too because firstly we fixed the equation to be like the theoretical one (don't know why it was modified in _old_forney(), if it's an optimization, it doesn't enhance anything), and secondly because we removed the product bound on s, which prevented computing errors and erasures above the s=(n-k)//2 bound.\n # The best resource I have found for the correct equation is https://en.wikipedia.org/wiki/Forney_algorithm -- note that in the article, fcr is defined as c.\n Yl = - (Xl**(1-self.fcr) * omega.evaluate(Xl_inv) / sigma_prime) # sigma_prime is the denominator of the Forney algorithm\n\n Y.append(Yl)\n return Y", "response": "Compute the error magnitudes. Works also with erasures and errors + erasures beyond the 2 bound here the bound is 2 * e + v < = n - k"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef update(self):\n try:\n p = Popen(['/bin/ps', '-p%s' % self.pid, '-o', 'rss,vsz'],\n stdout=PIPE, stderr=PIPE)\n except OSError: # pragma: no cover\n pass\n else:\n s = p.communicate()[0].split()\n if p.returncode == 0 and len(s) >= 2: # pragma: no branch\n self.vsz = int(s[-1]) * 1024\n self.rss = int(s[-2]) * 1024\n return True\n return False", "response": "Update the virtual and resident size of the current process via ps. Returns True if it was\n successful."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the internal state of the object.", "response": "def update(self):\n \"\"\"\n Get virtual size of current process by reading the process' stat file.\n This should work for Linux.\n \"\"\"\n try:\n stat = open('/proc/self/stat')\n status = open('/proc/self/status')\n except IOError: # pragma: no cover\n return False\n else:\n stats = stat.read().split()\n self.vsz = int( stats[22] )\n self.rss = int( stats[23] ) * self.pagesize\n self.pagefaults = int( stats[11] )\n\n for entry in status.readlines():\n key, value = entry.split(':')\n size_in_bytes = lambda x: int(x.split()[0]) * 1024\n if key == 'VmData':\n self.data_segment = size_in_bytes(value)\n elif key == 'VmExe':\n self.code_segment = size_in_bytes(value)\n elif key == 'VmLib':\n self.shared_segment = size_in_bytes(value)\n elif key == 'VmStk':\n self.stack_segment = size_in_bytes(value)\n key = self.key_map.get(key)\n if key:\n self.os_specific.append((key, value.strip()))\n\n\n stat.close()\n status.close()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef CreateControls(self):\n wx.EVT_LIST_COL_CLICK(self, self.GetId(), self.OnReorder)\n wx.EVT_LIST_ITEM_SELECTED(self, self.GetId(), self.OnNodeSelected)\n wx.EVT_MOTION(self, self.OnMouseMove)\n wx.EVT_LIST_ITEM_ACTIVATED(self, self.GetId(), self.OnNodeActivated)\n self.CreateColumns()", "response": "Create our sub - controls"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef CreateColumns( self ):\n self.SetItemCount(0)\n # clear any current columns...\n for i in range( self.GetColumnCount())[::-1]:\n self.DeleteColumn( i )\n # now create\n for i, column in enumerate(self.columns):\n column.index = i\n self.InsertColumn(i, column.name)\n if not windows or column.targetWidth is None:\n self.SetColumnWidth(i, wx.LIST_AUTOSIZE)\n else:\n self.SetColumnWidth(i, column.targetWidth)", "response": "Create our column definitions from current self. columns"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the columns to a set of values other than the originals and recreates column controls", "response": "def SetColumns( self, columns, sortOrder=None ):\n \"\"\"Set columns to a set of values other than the originals and recreates column controls\"\"\"\n self.columns = columns \n self.sortOrder = [(x.defaultOrder,x) for x in self.columns if x.sortDefault]\n self.CreateColumns()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef OnNodeActivated(self, event):\n try:\n node = self.sorted[event.GetIndex()]\n except IndexError, err:\n log.warn(_('Invalid index in node activated: %(index)s'),\n index=event.GetIndex())\n else:\n wx.PostEvent(\n self,\n squaremap.SquareActivationEvent(node=node, point=None,\n map=None)\n )", "response": "Event handler for node activated event"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef OnNodeSelected(self, event):\n try:\n node = self.sorted[event.GetIndex()]\n except IndexError, err:\n log.warn(_('Invalid index in node selected: %(index)s'),\n index=event.GetIndex())\n else:\n if node is not self.selected_node:\n wx.PostEvent(\n self,\n squaremap.SquareSelectionEvent(node=node, point=None,\n map=None)\n )", "response": "Event handler for node selection"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting this node to indicated status", "response": "def SetIndicated(self, node):\n \"\"\"Set this node to indicated status\"\"\"\n self.indicated_node = node\n self.indicated = self.NodeToIndex(node)\n self.Refresh(False)\n return self.indicated"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef SetSelected(self, node):\n self.selected_node = node\n index = self.NodeToIndex(node)\n if index != -1:\n self.Focus(index)\n self.Select(index, True)\n return index", "response": "Set our selected node"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall when a new record is reordered", "response": "def OnReorder(self, event):\n \"\"\"Given a request to reorder, tell us to reorder\"\"\"\n column = self.columns[event.GetColumn()]\n return self.ReorderByColumn( column )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreorder the set of records by a column.", "response": "def ReorderByColumn( self, column ):\n \"\"\"Reorder the set of records by column\"\"\"\n # TODO: store current selection and re-select after sorting...\n single_column = self.SetNewOrder( column )\n self.reorder( single_column = True )\n self.Refresh()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef SetNewOrder( self, column ):\n if column.sortOn:\n # multiple sorts for the click...\n columns = [self.columnByAttribute(attr) for attr in column.sortOn]\n diff = [(a, b) for a, b in zip(self.sortOrder, columns)\n if b is not a[1]]\n if not diff:\n self.sortOrder[0] = (not self.sortOrder[0][0], column)\n else:\n self.sortOrder = [\n (c.defaultOrder, c) for c in columns\n ] + [(a, b) for (a, b) in self.sortOrder if b not in columns]\n return False\n else:\n if column is self.sortOrder[0][1]:\n # reverse current major order\n self.sortOrder[0] = (not self.sortOrder[0][0], column)\n else:\n self.sortOrder = [(column.defaultOrder, column)] + [\n (a, b)\n for (a, b) in self.sortOrder if b is not column\n ]\n return True", "response": "Sets new sorting order based on column."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef reorder(self, single_column=False):\n if single_column:\n columns = self.sortOrder[:1]\n else:\n columns = self.sortOrder\n for ascending,column in columns[::-1]:\n # Python 2.2+ guarantees stable sort, so sort by each column in reverse \n # order will order by the assigned columns \n self.sorted.sort( key=column.get, reverse=(not ascending))", "response": "Force a reorder of the displayed items"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef integrateRecords(self, functions):\n self.SetItemCount(len(functions))\n self.sorted = functions[:]\n self.reorder()\n self.Refresh()", "response": "Integrate records from the loader"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef OnGetItemAttr(self, item):\n if self.indicated > -1 and item == self.indicated:\n return self.indicated_attribute\n return None", "response": "Called when the user has requested the attribute of the item."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef OnGetItemText(self, item, col):\n # TODO: need to format for rjust and the like...\n try:\n column = self.columns[col]\n value = column.get(self.sorted[item])\n except IndexError, err:\n return None\n else:\n if value is None:\n return u''\n if column.percentPossible and self.percentageView and self.total:\n value = value / float(self.total) * 100.00\n if column.format:\n try:\n return column.format % (value,)\n except Exception, err:\n log.warn('Column %s could not format %r value: %r',\n column.name, type(value), value\n )\n value = column.get(self.sorted[item] )\n if isinstance(value,(unicode,str)):\n return value\n return unicode(value)\n else:\n if isinstance(value,(unicode,str)):\n return value\n return unicode(value)", "response": "Retrieve text for the item and column respectively"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nencodes the input data with reed - solomon error correction in 223 byte blocks and outputs each block along with 32 parity bytes to a new file by .", "response": "def encode(input, output_filename):\n \"\"\"Encodes the input data with reed-solomon error correction in 223 byte\n blocks, and outputs each block along with 32 parity bytes to a new file by\n the given filename.\n\n input is a file-like object\n\n The outputted image will be in png format, and will be 255 by x pixels with\n one color channel. X is the number of 255 byte blocks from the input. Each\n block of data will be one row, therefore, the data can be recovered if no\n more than 16 pixels per row are altered.\n \"\"\"\n coder = rs.RSCoder(255,223)\n\n output = []\n\n while True:\n block = input.read(223)\n if not block: break\n code = coder.encode_fast(block)\n output.append(code)\n sys.stderr.write(\".\")\n\n sys.stderr.write(\"\\n\")\n\n out = Image.new(\"L\", (rowstride,len(output)))\n out.putdata(\"\".join(output))\n out.save(output_filename)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef redirect(url, code=303):\n scriptname = request.environ.get('SCRIPT_NAME', '').rstrip('/') + '/'\n location = urljoin(request.url, urljoin(scriptname, url))\n raise HTTPResponse(\"\", status=code, header=dict(Location=location))", "response": "Abort execution and causes a 303 redirect"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse_date(ims):\n try:\n ts = email.utils.parsedate_tz(ims)\n return time.mktime(ts[:8] + (0,)) - (ts[9] or 0) - time.timezone\n except (TypeError, ValueError, IndexError):\n return None", "response": "Parse rfc1123 rfc850 and asctime timestamps and return UTC epoch."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run(app=None, server=WSGIRefServer, host='127.0.0.1', port=8080,\n interval=1, reloader=False, **kargs):\n \"\"\" Runs bottle as a web server. \"\"\"\n app = app if app else default_app()\n quiet = bool(kargs.get('quiet', False))\n # Instantiate server, if it is a class instead of an instance\n if isinstance(server, type):\n server = server(host=host, port=port, **kargs)\n if not isinstance(server, ServerAdapter):\n raise RuntimeError(\"Server must be a subclass of WSGIAdapter\")\n if not quiet and isinstance(server, ServerAdapter): # pragma: no cover\n if not reloader or os.environ.get('BOTTLE_CHILD') == 'true':\n print(\"Bottle server starting up (using %s)...\" % repr(server))\n print(\"Listening on http://%s:%d/\" % (server.host, server.port))\n print(\"Use Ctrl-C to quit.\")\n print()\n else:\n print(\"Bottle auto reloader starting up...\")\n try:\n if reloader and interval:\n reloader_run(server, app, interval)\n else:\n server.run(app)\n except KeyboardInterrupt:\n if not quiet: # pragma: no cover\n print(\"Shutting Down...\")", "response": "Runs bottle as a web server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrender a template string.", "response": "def template(tpl, template_adapter=SimpleTemplate, **kwargs):\n '''\n Get a rendered template as a string iterator.\n You can use a name, a filename or a template string as first parameter.\n '''\n if tpl not in TEMPLATES or DEBUG:\n settings = kwargs.get('template_settings',{})\n lookup = kwargs.get('template_lookup', TEMPLATE_PATH)\n if isinstance(tpl, template_adapter):\n TEMPLATES[tpl] = tpl\n if settings: TEMPLATES[tpl].prepare(settings)\n elif \"\\n\" in tpl or \"{\" in tpl or \"%\" in tpl or '$' in tpl:\n TEMPLATES[tpl] = template_adapter(source=tpl, lookup=lookup, settings=settings)\n else:\n TEMPLATES[tpl] = template_adapter(name=tpl, lookup=lookup, settings=settings)\n if not TEMPLATES[tpl]:\n abort(500, 'Template (%s) not found' % tpl)\n kwargs['abort'] = abort\n kwargs['request'] = request\n kwargs['response'] = response\n return TEMPLATES[tpl].render(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of ( type value ) tokens.", "response": "def tokens(self):\n \"\"\" Return a list of (type, value) tokens. \"\"\"\n if not self._tokens:\n self._tokens = list(self.tokenise(self.route))\n return self._tokens"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef tokenise(cls, route):\n ''' Split a string into an iterator of (type, value) tokens. '''\n match = None\n for match in cls.syntax.finditer(route):\n pre, name, rex = match.groups()\n if pre: yield ('TXT', pre.replace('\\\\:',':'))\n if rex and name: yield ('VAR', (rex, name))\n elif name: yield ('VAR', (cls.default, name))\n elif rex: yield ('ANON', rex)\n if not match:\n yield ('TXT', route.replace('\\\\:',':'))\n elif match.end() < len(route):\n yield ('TXT', route[match.end():].replace('\\\\:',':'))", "response": "Split a string into an iterator of ( type value ) tokens."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a regexp pattern with named groups", "response": "def group_re(self):\n ''' Return a regexp pattern with named groups '''\n out = ''\n for token, data in self.tokens():\n if token == 'TXT': out += re.escape(data)\n elif token == 'VAR': out += '(?P<%s>%s)' % (data[1], data[0])\n elif token == 'ANON': out += '(?:%s)' % data\n return out"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a format string with named fields.", "response": "def format_str(self):\n ''' Return a format string with named fields. '''\n if self.static:\n return self.route.replace('%','%%')\n out, i = '', 0\n for token, value in self.tokens():\n if token == 'TXT': out += value.replace('%','%%')\n elif token == 'ANON': out += '%%(anon%d)s' % i; i+=1\n elif token == 'VAR': out += '%%(%s)s' % value[1]\n return out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn true if the route contains dynamic parts", "response": "def is_dynamic(self):\n ''' Return true if the route contains dynamic parts '''\n if not self._static:\n for token, value in self.tokens():\n if token != 'TXT':\n return True\n self._static = True\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmatch an URL and returns a tuple of handler target groups", "response": "def match(self, uri):\n ''' Matches an URL and returns a (handler, target) tuple '''\n if uri in self.static:\n return self.static[uri], {}\n for combined, subroutes in self.dynamic:\n match = combined.match(uri)\n if not match: continue\n target, groups = subroutes[match.lastindex - 1]\n groups = groups.match(uri).groupdict() if groups else {}\n return target, groups\n return None, {}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef build(self, route_name, **args):\n ''' Builds an URL out of a named route and some parameters.'''\n try:\n return self.named[route_name] % args\n except KeyError:\n raise RouteBuildError(\"No route found with name '%s'.\" % route_name)", "response": "Builds an URL out of a named route and some parameters."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nregistering a new output filter. Whenever bottle hits a handler output matching ftype func is applied to it.", "response": "def add_filter(self, ftype, func):\n ''' Register a new output filter. Whenever bottle hits a handler output\n matching `ftype`, `func` is applyed to it. '''\n if not isinstance(ftype, type):\n raise TypeError(\"Expected type object, got %s\" % type(ftype))\n self.castfilter = [(t, f) for (t, f) in self.castfilter if t != ftype]\n self.castfilter.append((ftype, func))\n self.castfilter.sort()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfinds a callback bound to a path and a specific HTTP method.", "response": "def match_url(self, path, method='GET'):\n \"\"\" Find a callback bound to a path and a specific HTTP method.\n Return (callback, param) tuple or (None, {}).\n method: HEAD falls back to GET. HEAD and GET fall back to ALL.\n \"\"\"\n path = path.strip().lstrip('/')\n handler, param = self.routes.match(method + ';' + path)\n if handler: return handler, param\n if method == 'HEAD':\n handler, param = self.routes.match('GET;' + path)\n if handler: return handler, param\n handler, param = self.routes.match('ANY;' + path)\n if handler: return handler, param\n return None, {}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_url(self, routename, **kargs):\n return '/' + self.routes.build(routename, **kargs).split(';', 1)[1]", "response": "Return a string that matches a named route"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbinding a new WSGI enviroment and clear out all previously computed content.", "response": "def bind(self, environ, app=None):\n \"\"\" Bind a new WSGI enviroment and clear out all previously computed\n attributes.\n \n This is done automatically for the global `bottle.request`\n instance on every request.\n \"\"\"\n if isinstance(environ, Request): # Recycle already parsed content\n for key in self.__dict__: #TODO: Test this\n setattr(self, key, getattr(environ, key))\n self.app = app\n return\n self._GET = self._POST = self._GETPOST = self._COOKIES = None\n self._body = self._header = None\n self.environ = environ\n self.app = app\n # These attributes are used anyway, so it is ok to compute them here\n self.path = '/' + environ.get('PATH_INFO', '/').lstrip('/')\n self.method = environ.get('REQUEST_METHOD', 'GET').upper()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nshift some levels of PATH_INFO into SCRIPT_NAME and return the moved part. count defaults to 1", "response": "def path_shift(self, count=1):\n ''' Shift some levels of PATH_INFO into SCRIPT_NAME and return the\n moved part. count defaults to 1'''\n #/a/b/ /c/d --> 'a','b' 'c','d'\n if count == 0: return ''\n pathlist = self.path.strip('/').split('/')\n scriptlist = self.environ.get('SCRIPT_NAME','/').strip('/').split('/')\n if pathlist and pathlist[0] == '': pathlist = []\n if scriptlist and scriptlist[0] == '': scriptlist = []\n if count > 0 and count <= len(pathlist):\n moved = pathlist[:count]\n scriptlist = scriptlist + moved\n pathlist = pathlist[count:]\n elif count < 0 and count >= -len(scriptlist):\n moved = scriptlist[count:]\n pathlist = moved + pathlist\n scriptlist = scriptlist[:count]\n else:\n empty = 'SCRIPT_NAME' if count < 0 else 'PATH_INFO'\n raise AssertionError(\"Cannot shift. Nothing left from %s\" % empty)\n self['PATH_INFO'] = self.path = '/' + '/'.join(pathlist) \\\n + ('/' if self.path.endswith('/') and pathlist else '')\n self['SCRIPT_NAME'] = '/' + '/'.join(scriptlist)\n return '/'.join(moved)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the full URL as requested by the client.", "response": "def url(self):\n \"\"\" Full URL as requested by the client (computed).\n\n This value is constructed out of different environment variables\n and includes scheme, host, port, scriptname, path and query string. \n \"\"\"\n scheme = self.environ.get('wsgi.url_scheme', 'http')\n host = self.environ.get('HTTP_X_FORWARDED_HOST', self.environ.get('HTTP_HOST', None))\n if not host:\n host = self.environ.get('SERVER_NAME')\n port = self.environ.get('SERVER_PORT', '80')\n if scheme + port not in ('https443', 'http80'):\n host += ':' + port\n parts = (scheme, host, urlquote(self.fullpath), self.query_string, '')\n return urlunsplit(parts)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef POST(self):\n if self._POST is None:\n save_env = dict() # Build a save environment for cgi\n for key in ('REQUEST_METHOD', 'CONTENT_TYPE', 'CONTENT_LENGTH'):\n if key in self.environ:\n save_env[key] = self.environ[key]\n save_env['QUERY_STRING'] = '' # Without this, sys.argv is called!\n if TextIOWrapper:\n fb = TextIOWrapper(self.body, encoding='ISO-8859-1')\n else:\n fb = self.body\n data = cgi.FieldStorage(fp=fb, environ=save_env)\n self._POST = MultiDict()\n for item in data.list:\n self._POST[item.name] = item if item.filename else item.value\n return self._POST", "response": "The HTTP POST body parsed into a MultiDict."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef params(self):\n if self._GETPOST is None:\n self._GETPOST = MultiDict(self.GET)\n self._GETPOST.update(dict(self.POST))\n return self._GETPOST", "response": "A combined MultiDict with POST and GET parameters."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_cookie(self, *args):\n value = self.COOKIES.get(*args)\n sec = self.app.config['securecookie.key']\n dec = cookie_decode(value, sec)\n return dec or value", "response": "Return the ( decoded ) value of a cookie."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef copy(self):\n ''' Returns a copy of self '''\n copy = Response(self.app)\n copy.status = self.status\n copy.headers = self.headers.copy()\n copy.content_type = self.content_type\n return copy", "response": "Returns a copy of self"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a wsgi conform list of header value pairs.", "response": "def wsgiheader(self):\n ''' Returns a wsgi conform list of header/value pairs. '''\n for c in list(self.COOKIES.values()):\n if c.OutputString() not in self.headers.getall('Set-Cookie'):\n self.headers.append('Set-Cookie', c.OutputString())\n return list(self.headers.iterallitems())"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_cookie(self, key, value, **kargs):\n if not isinstance(value, str):\n sec = self.app.config['securecookie.key']\n value = cookie_encode(value, sec).decode('ascii') #2to3 hack\n self.COOKIES[key] = value\n for k, v in kargs.items():\n self.COOKIES[key][k.replace('_', '-')] = v", "response": "Add a new cookie with various options."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nparses the source file and returns the AST of the source file.", "response": "def parse_source_file(file_name):\n \"\"\"\n Parses the AST of Python file for lines containing\n references to the argparse module.\n\n returns the collection of ast objects found.\n\n Example client code:\n\n 1. parser = ArgumentParser(desc=\"My help Message\")\n 2. parser.add_argument('filename', help=\"Name of the file to load\")\n 3. parser.add_argument('-f', '--format', help='Format of output \\nOptions: ['md', 'html']\n 4. args = parser.parse_args()\n\n Variables:\n * nodes \t\t\t\t\t\t\t\t\tPrimary syntax tree object\n *\targparse_assignments \tThe assignment of the ArgumentParser (line 1 in example code)\n * add_arg_assignments Calls to add_argument() (lines 2-3 in example code)\n * parser_var_name\t\t\t\t\tThe instance variable of the ArgumentParser (line 1 in example code)\n * ast_source\t\t\t\t\t\t\tThe curated collection of all parser related nodes in the client code\n \"\"\"\n\n nodes = ast.parse(_openfile(file_name))\n\n module_imports = get_nodes_by_instance_type(nodes, _ast.Import)\n specific_imports = get_nodes_by_instance_type(nodes, _ast.ImportFrom)\n\n assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)\n call_objects = get_nodes_by_instance_type(nodes, _ast.Call)\n\n argparse_assignments = get_nodes_by_containing_attr(assignment_objs, 'ArgumentParser')\n add_arg_assignments = get_nodes_by_containing_attr(call_objects, 'add_argument')\n parse_args_assignment = get_nodes_by_containing_attr(call_objects, 'parse_args')\n\n ast_argparse_source = chain(\n module_imports,\n specific_imports,\n argparse_assignments,\n add_arg_assignments\n # parse_args_assignment\n )\n return ast_argparse_source"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the memory usage of a process or piece of code.", "response": "def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,\n include_children=False, max_usage=False, retval=False,\n stream=None):\n \"\"\"\n Return the memory usage of a process or piece of code\n\n Parameters\n ----------\n proc : {int, string, tuple, subprocess.Popen}, optional\n The process to monitor. Can be given by an integer/string\n representing a PID, by a Popen object or by a tuple\n representing a Python function. The tuple contains three\n values (f, args, kw) and specifies to run the function\n f(*args, **kw).\n Set to -1 (default) for current process.\n\n interval : float, optional\n Interval at which measurements are collected.\n\n timeout : float, optional\n Maximum amount of time (in seconds) to wait before returning.\n\n max_usage : bool, optional\n Only return the maximum memory usage (default False)\n\n retval : bool, optional\n For profiling python functions. Save the return value of the profiled\n function. Return value of memory_usage becomes a tuple:\n (mem_usage, retval)\n\n timestamps : bool, optional\n if True, timestamps of memory usage measurement are collected as well.\n\n stream : File\n if stream is a File opened with write access, then results are written\n to this file instead of stored in memory and returned at the end of\n the subprocess. Useful for long-running processes.\n Implies timestamps=True.\n\n Returns\n -------\n mem_usage : list of floating-poing values\n memory usage, in MiB. It's length is always < timeout / interval\n if max_usage is given, returns the two elements maximum memory and\n number of measurements effectuated\n ret : return value of the profiled function\n Only returned if retval is set to True\n \"\"\"\n if stream is not None:\n timestamps = True\n\n if not max_usage:\n ret = []\n else:\n ret = -1\n\n if timeout is not None:\n max_iter = int(timeout / interval)\n elif isinstance(proc, int):\n # external process and no timeout\n max_iter = 1\n else:\n # for a Python function wait until it finishes\n max_iter = float('inf')\n\n if hasattr(proc, '__call__'):\n proc = (proc, (), {})\n if isinstance(proc, (list, tuple)):\n if len(proc) == 1:\n f, args, kw = (proc[0], (), {})\n elif len(proc) == 2:\n f, args, kw = (proc[0], proc[1], {})\n elif len(proc) == 3:\n f, args, kw = (proc[0], proc[1], proc[2])\n else:\n raise ValueError\n\n while True:\n child_conn, parent_conn = Pipe() # this will store MemTimer's results\n p = MemTimer(os.getpid(), interval, child_conn, timestamps=timestamps,\n max_usage=max_usage, include_children=include_children)\n p.start()\n parent_conn.recv() # wait until we start getting memory\n returned = f(*args, **kw)\n parent_conn.send(0) # finish timing\n ret = parent_conn.recv()\n n_measurements = parent_conn.recv()\n if retval:\n ret = ret, returned\n p.join(5 * interval)\n if n_measurements > 4 or interval < 1e-6:\n break\n interval /= 10.\n elif isinstance(proc, subprocess.Popen):\n # external process, launched from Python\n line_count = 0\n while True:\n if not max_usage:\n mem_usage = _get_memory(proc.pid, timestamps=timestamps,\n include_children=include_children)\n if stream is not None:\n stream.write(\"MEM {0:.6f} {1:.4f}\\n\".format(*mem_usage))\n else:\n ret.append(mem_usage)\n else:\n ret = max([ret,\n _get_memory(proc.pid,\n include_children=include_children)])\n time.sleep(interval)\n line_count += 1\n # flush every 50 lines. Make 'tail -f' usable on profile file\n if line_count > 50:\n line_count = 0\n if stream is not None:\n stream.flush()\n if timeout is not None:\n max_iter -= 1\n if max_iter == 0:\n break\n if proc.poll() is not None:\n break\n else:\n # external process\n if max_iter == -1:\n max_iter = 1\n counter = 0\n while counter < max_iter:\n counter += 1\n if not max_usage:\n mem_usage = _get_memory(proc, timestamps=timestamps,\n include_children=include_children)\n if stream is not None:\n stream.write(\"MEM {0:.6f} {1:.4f}\\n\".format(*mem_usage))\n else:\n ret.append(mem_usage)\n else:\n ret = max([ret,\n _get_memory(proc, include_children=include_children)\n ])\n\n time.sleep(interval)\n # Flush every 50 lines.\n if counter % 50 == 0 and stream is not None:\n stream.flush()\n if stream:\n return None\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _find_script(script_name):\n if os.path.isfile(script_name):\n return script_name\n path = os.getenv('PATH', os.defpath).split(os.pathsep)\n for folder in path:\n if not folder:\n continue\n fn = os.path.join(folder, script_name)\n if os.path.isfile(fn):\n return fn\n\n sys.stderr.write('Could not find script {0}\\n'.format(script_name))\n raise SystemExit(1)", "response": "Find the script in the system path."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef magic_mprun(self, parameter_s=''):\n try:\n from StringIO import StringIO\n except ImportError: # Python 3.x\n from io import StringIO\n\n # Local imports to avoid hard dependency.\n from distutils.version import LooseVersion\n import IPython\n ipython_version = LooseVersion(IPython.__version__)\n if ipython_version < '0.11':\n from IPython.genutils import page\n from IPython.ipstruct import Struct\n from IPython.ipapi import UsageError\n else:\n from IPython.core.page import page\n from IPython.utils.ipstruct import Struct\n from IPython.core.error import UsageError\n\n # Escape quote markers.\n opts_def = Struct(T=[''], f=[])\n parameter_s = parameter_s.replace('\"', r'\\\"').replace(\"'\", r\"\\'\")\n opts, arg_str = self.parse_options(parameter_s, 'rf:T:c', list_all=True)\n opts.merge(opts_def)\n global_ns = self.shell.user_global_ns\n local_ns = self.shell.user_ns\n\n # Get the requested functions.\n funcs = []\n for name in opts.f:\n try:\n funcs.append(eval(name, global_ns, local_ns))\n except Exception as e:\n raise UsageError('Could not find function %r.\\n%s: %s' % (name,\n e.__class__.__name__, e))\n\n include_children = 'c' in opts\n profile = LineProfiler(include_children=include_children)\n for func in funcs:\n profile(func)\n\n # Add the profiler to the builtins for @profile.\n try:\n import builtins\n except ImportError: # Python 3x\n import __builtin__ as builtins\n\n if 'profile' in builtins.__dict__:\n had_profile = True\n old_profile = builtins.__dict__['profile']\n else:\n had_profile = False\n old_profile = None\n builtins.__dict__['profile'] = profile\n\n try:\n try:\n profile.runctx(arg_str, global_ns, local_ns)\n message = ''\n except SystemExit:\n message = \"*** SystemExit exception caught in code being profiled.\"\n except KeyboardInterrupt:\n message = (\"*** KeyboardInterrupt exception caught in code being \"\n \"profiled.\")\n finally:\n if had_profile:\n builtins.__dict__['profile'] = old_profile\n\n # Trap text output.\n stdout_trap = StringIO()\n show_results(profile, stdout_trap)\n output = stdout_trap.getvalue()\n output = output.rstrip()\n\n if ipython_version < '0.11':\n page(output, screen_lines=self.shell.rc.screen_length)\n else:\n page(output)\n print(message,)\n\n text_file = opts.T[0]\n if text_file:\n with open(text_file, 'w') as pfile:\n pfile.write(output)\n print('\\n*** Profile printout saved to text file %s. %s' % (text_file,\n message))\n\n return_value = None\n if 'r' in opts:\n return_value = profile\n\n return return_value", "response": "Execute a magic statement under the line - by - line memory profiler module."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmeasures memory usage of a Python statement.", "response": "def magic_memit(self, line=''):\n \"\"\"Measure memory usage of a Python statement\n\n Usage, in line mode:\n %memit [-rti] statement\n\n Options:\n -r: repeat the loop iteration times and take the best result.\n Default: 1\n\n -t: timeout after seconds. Default: None\n\n -i: Get time information at an interval of I times per second.\n Defaults to 0.1 so that there is ten measurements per second.\n\n -c: If present, add the memory usage of any children process to the report.\n\n Examples\n --------\n ::\n\n In [1]: import numpy as np\n\n In [2]: %memit np.zeros(1e7)\n maximum of 1: 76.402344 MiB per loop\n\n In [3]: %memit np.ones(1e6)\n maximum of 1: 7.820312 MiB per loop\n\n In [4]: %memit -r 10 np.empty(1e8)\n maximum of 10: 0.101562 MiB per loop\n\n \"\"\"\n opts, stmt = self.parse_options(line, 'r:t:i:c', posix=False, strict=False)\n repeat = int(getattr(opts, 'r', 1))\n if repeat < 1:\n repeat == 1\n timeout = int(getattr(opts, 't', 0))\n if timeout <= 0:\n timeout = None\n interval = float(getattr(opts, 'i', 0.1))\n include_children = 'c' in opts\n\n # I've noticed we get less noisier measurements if we run\n # a garbage collection first\n import gc\n gc.collect()\n\n mem_usage = 0\n counter = 0\n baseline = memory_usage()[0]\n while counter < repeat:\n counter += 1\n tmp = memory_usage((_func_exec, (stmt, self.shell.user_ns)),\n timeout=timeout, interval=interval, max_usage=True,\n include_children=include_children)\n mem_usage = max(mem_usage, tmp[0])\n\n if mem_usage:\n print('peak memory: %.02f MiB, increment: %.02f MiB' %\n (mem_usage, mem_usage - baseline))\n else:\n print('ERROR: could not read memory usage, try with a lower interval '\n 'or more iterations')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef timestamp(self, name=\"\"):\n # Make a fake function\n func = lambda x: x\n func.__module__ = \"\"\n func.__name__ = name\n self.add_function(func)\n timestamps = []\n self.functions[func].append(timestamps)\n # A new object is required each time, since there can be several\n # nested context managers.\n return _TimeStamperCM(timestamps)", "response": "Returns a context manager for timestamping a block of code."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef wrap_function(self, func):\n def f(*args, **kwds):\n # Start time\n timestamps = [_get_memory(os.getpid(), timestamps=True)]\n self.functions[func].append(timestamps)\n try:\n result = func(*args, **kwds)\n finally:\n # end time\n timestamps.append(_get_memory(os.getpid(), timestamps=True))\n return result\n return f", "response": "Wrap a function to timestamp it."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a Python function to the line profiling information.", "response": "def add_function(self, func):\n \"\"\" Record line profiling information for the given Python function.\n \"\"\"\n try:\n # func_code does not exist in Python3\n code = func.__code__\n except AttributeError:\n warnings.warn(\"Could not extract a code object for the object %r\"\n % func)\n else:\n self.add_code(code)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef run(self, cmd):\n # TODO: can this be removed ?\n import __main__\n main_dict = __main__.__dict__\n return self.runctx(cmd, main_dict, main_dict)", "response": "Profile a single executable statement in the main namespace."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef trace_memory_usage(self, frame, event, arg):\n if (event in ('call', 'line', 'return')\n and frame.f_code in self.code_map):\n if event != 'call':\n # \"call\" event just saves the lineno but not the memory\n mem = _get_memory(-1, include_children=self.include_children)\n # if there is already a measurement for that line get the max\n old_mem = self.code_map[frame.f_code].get(self.prevline, 0)\n self.code_map[frame.f_code][self.prevline] = max(mem, old_mem)\n self.prevline = frame.f_lineno\n\n if self._original_trace_function is not None:\n (self._original_trace_function)(frame, event, arg)\n\n return self.trace_memory_usage", "response": "Callback for sys. settrace"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_root( self, key ):\n if key not in self.roots:\n function = getattr( self, 'load_%s'%(key,) )()\n self.roots[key] = function\n return self.roots[key]", "response": "Retrieve a given declared root by root - type - key"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the set of rows for the type - key", "response": "def get_rows( self, key ):\n \"\"\"Get the set of rows for the type-key\"\"\"\n if key not in self.roots:\n self.get_root( key )\n if key == 'location':\n return self.location_rows \n else:\n return self.rows"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nbuilding a squaremap - compatible model from a pstats class", "response": "def load( self, stats ):\n \"\"\"Build a squaremap-compatible model from a pstats class\"\"\"\n rows = self.rows\n for func, raw in stats.iteritems():\n try:\n rows[func] = row = PStatRow( func,raw )\n except ValueError, err:\n log.info( 'Null row: %s', func )\n for row in rows.itervalues():\n row.weave( rows )\n return self.find_root( rows )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_root( self, rows ):\n maxes = sorted( rows.values(), key = lambda x: x.cumulative )\n if not maxes:\n raise RuntimeError( \"\"\"Null results!\"\"\" )\n root = maxes[-1]\n roots = [root]\n for key,value in rows.items():\n if not value.parents:\n log.debug( 'Found node root: %s', value )\n if value not in roots:\n roots.append( value )\n if len(roots) > 1:\n root = PStatGroup(\n directory='*',\n filename='*',\n name=_(\"\"),\n children= roots,\n )\n root.finalize()\n self.rows[ root.key ] = root\n self.roots['functions'] = root\n return root", "response": "Attempt to find a reasonable root node from a list of rows."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _load_location( self ):\n directories = {}\n files = {}\n root = PStatLocation( '/', 'PYTHONPATH' )\n self.location_rows = self.rows.copy()\n for child in self.rows.values():\n current = directories.get( child.directory )\n directory, filename = child.directory, child.filename\n if current is None:\n if directory == '':\n current = root\n else:\n current = PStatLocation( directory, '' )\n self.location_rows[ current.key ] = current\n directories[ directory ] = current\n if filename == '~':\n filename = ''\n file_current = files.get( (directory,filename) )\n if file_current is None:\n file_current = PStatLocation( directory, filename )\n self.location_rows[ file_current.key ] = file_current\n files[ (directory,filename) ] = file_current\n current.children.append( file_current )\n file_current.children.append( child )\n # now link the directories...\n for key,value in directories.items():\n if value is root:\n continue\n found = False\n while key:\n new_key,rest = os.path.split( key )\n if new_key == key:\n break\n key = new_key\n parent = directories.get( key )\n if parent:\n if value is not parent:\n parent.children.append( value )\n found = True\n break\n if not found:\n root.children.append( value )\n # lastly, finalize all of the directory records...\n root.finalize()\n return root", "response": "Build a squaremap - compatible model for location - based hierarchy of directories and files."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef finalize( self, already_done=None ):\n if already_done is None:\n already_done = {}\n if already_done.has_key( self ):\n return True\n already_done[self] = True\n self.filter_children()\n children = self.children\n for child in children:\n if hasattr( child, 'finalize' ):\n child.finalize( already_done)\n child.parents.append( self )\n self.calculate_totals( self.children, self.local_children )", "response": "Finalize our values taken from our children"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef calculate_totals( self, children, local_children=None ):\n for field,local_field in (('recursive','calls'),('cumulative','local')):\n values = []\n for child in children:\n if isinstance( child, PStatGroup ) or not self.LOCAL_ONLY:\n values.append( getattr( child, field, 0 ) )\n elif isinstance( child, PStatRow ) and self.LOCAL_ONLY:\n values.append( getattr( child, local_field, 0 ) )\n value = sum( values )\n setattr( self, field, value )\n if self.recursive:\n self.cumulativePer = self.cumulative/float(self.recursive)\n else:\n self.recursive = 0\n if local_children:\n for field in ('local','calls'):\n value = sum([ getattr( child, field, 0 ) for child in children] )\n setattr( self, field, value )\n if self.calls:\n self.localPer = self.local / self.calls\n else:\n self.local = 0\n self.calls = 0\n self.localPer = 0", "response": "Calculate our cumulative totals from children and local children"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfiltering our children into regular and local children sets", "response": "def filter_children( self ):\n \"\"\"Filter our children into regular and local children sets\"\"\"\n real_children = []\n for child in self.children:\n if child.name == '':\n self.local_children.append( child )\n else:\n real_children.append( child )\n self.children = real_children"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn set of two boxes where first is the fraction given", "response": "def split_box( fraction, x,y, w,h ):\n \"\"\"Return set of two boxes where first is the fraction given\"\"\"\n if w >= h:\n new_w = int(w*fraction)\n if new_w:\n return (x,y,new_w,h),(x+new_w,y,w-new_w,h)\n else:\n return None,None\n else:\n new_h = int(h*fraction)\n if new_h:\n return (x,y,w,new_h),(x,y+new_h,w,h-new_h)\n else:\n return None,None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef split_by_value( total, nodes, headdivisor=2.0 ):\n head_sum,tail_sum = 0,0\n divider = 0\n for node in nodes[::-1]:\n if head_sum < total/headdivisor:\n head_sum += node[0]\n divider -= 1\n else:\n break\n return (head_sum,nodes[divider:]),(total-head_sum,nodes[:divider])", "response": "Produce a tuple of head and tail for a binary partition of the total number of nodes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef findNode(class_, hot_map, targetNode, parentNode=None):\n ''' Find the target node in the hot_map. '''\n for index, (rect, node, children) in enumerate(hot_map):\n if node == targetNode:\n return parentNode, hot_map, index\n result = class_.findNode(children, targetNode, node)\n if result:\n return result\n return None", "response": "Find the target node in the hot_map."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef firstChild(hot_map, index):\n ''' Return the first child of the node indicated by index. '''\n children = hot_map[index][2]\n if children:\n return children[0][1]\n else:\n return hot_map[index][1]", "response": "Return the first child of the node indicated by index."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef nextChild(hotmap, index):\n ''' Return the next sibling of the node indicated by index. '''\n nextChildIndex = min(index + 1, len(hotmap) - 1)\n return hotmap[nextChildIndex][1]", "response": "Return the next sibling of the node indicated by index."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the very last node in the hot map.", "response": "def lastNode(class_, hot_map):\n ''' Return the very last node (recursively) in the hot map. '''\n children = hot_map[-1][2]\n if children:\n return class_.lastNode(children)\n else:\n return hot_map[-1][1]"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling mouse - move event by selecting a given element", "response": "def OnMouse( self, event ):\n \"\"\"Handle mouse-move event by selecting a given element\"\"\"\n node = HotMapNavigator.findNodeAtPosition(self.hot_map, event.GetPosition())\n self.SetHighlight( node, event.GetPosition() )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef OnClickRelease( self, event ):\n node = HotMapNavigator.findNodeAtPosition(self.hot_map, event.GetPosition())\n self.SetSelected( node, event.GetPosition() )", "response": "Release over a given square in the map"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef OnDoubleClick(self, event):\n node = HotMapNavigator.findNodeAtPosition(self.hot_map, event.GetPosition())\n if node:\n wx.PostEvent( self, SquareActivationEvent( node=node, point=event.GetPosition(), map=self ) )", "response": "Double click on a given square in the map"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef SetSelected( self, node, point=None, propagate=True ):\n if node == self.selectedNode:\n return\n self.selectedNode = node\n self.UpdateDrawing()\n if node:\n wx.PostEvent( self, SquareSelectionEvent( node=node, point=point, map=self ) )", "response": "Set the given node selected in the square - map"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef SetHighlight( self, node, point=None, propagate=True ):\n if node == self.highlightedNode:\n return\n self.highlightedNode = node\n # TODO: restrict refresh to the squares for previous node and new node...\n self.UpdateDrawing()\n if node and propagate:\n wx.PostEvent( self, SquareHighlightEvent( node=node, point=point, map=self ) )", "response": "Set the currently - highlighted node"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets our model object.", "response": "def SetModel( self, model, adapter=None ):\n \"\"\"Set our model object (root of the tree)\"\"\"\n self.model = model\n if adapter is not None:\n self.adapter = adapter\n self.UpdateDrawing()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef Draw(self, dc):\n ''' Draw the tree map on the device context. '''\n self.hot_map = []\n dc.BeginDrawing()\n brush = wx.Brush( self.BackgroundColour )\n dc.SetBackground( brush )\n dc.Clear()\n if self.model:\n self.max_depth_seen = 0\n font = self.FontForLabels(dc)\n dc.SetFont(font)\n self._em_size_ = dc.GetFullTextExtent( 'm', font )[0]\n w, h = dc.GetSize()\n self.DrawBox( dc, self.model, 0,0,w,h, hot_map = self.hot_map )\n dc.EndDrawing()", "response": "Draw the tree map on the device context."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the default GUI font scaled for printing if necessary.", "response": "def FontForLabels(self, dc):\n ''' Return the default GUI font, scaled for printing if necessary. '''\n font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)\n scale = dc.GetPPI()[0] / wx.ScreenDC().GetPPI()[0]\n font.SetPointSize(scale*font.GetPointSize())\n return font"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef BrushForNode( self, node, depth=0 ):\n if node == self.selectedNode:\n color = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)\n elif node == self.highlightedNode:\n color = wx.Colour( red=0, green=255, blue=0 )\n else:\n color = self.adapter.background_color(node, depth)\n if not color:\n red = (depth * 10)%255\n green = 255-((depth * 5)%255)\n blue = (depth * 25)%255\n color = wx.Colour( red, green, blue )\n return wx.Brush( color )", "response": "Create brush to use to display the given node"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef PenForNode( self, node, depth=0 ):\n if node == self.selectedNode:\n return self.SELECTED_PEN\n return self.DEFAULT_PEN", "response": "Determine the pen to use to display the given node"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndetermines the text foreground color to use to display the label of the given node", "response": "def TextForegroundForNode(self, node, depth=0):\n \"\"\"Determine the text foreground color to use to display the label of\n the given node\"\"\"\n if node == self.selectedNode:\n fg_color = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)\n else:\n fg_color = self.adapter.foreground_color(node, depth)\n if not fg_color:\n fg_color = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)\n return fg_color"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndraw a model - node s box and all children nodes.", "response": "def DrawBox( self, dc, node, x,y,w,h, hot_map, depth=0 ):\n \"\"\"Draw a model-node's box and all children nodes\"\"\"\n log.debug( 'Draw: %s to (%s,%s,%s,%s) depth %s',\n node, x,y,w,h, depth,\n )\n if self.max_depth and depth > self.max_depth:\n return\n self.max_depth_seen = max( (self.max_depth_seen,depth))\n dc.SetBrush( self.BrushForNode( node, depth ) )\n dc.SetPen( self.PenForNode( node, depth ) )\n # drawing offset by margin within the square...\n dx,dy,dw,dh = x+self.margin,y+self.margin,w-(self.margin*2),h-(self.margin*2)\n if sys.platform == 'darwin':\n # Macs don't like drawing small rounded rects...\n if w < self.padding*2 or h < self.padding*2:\n dc.DrawRectangle( dx,dy,dw,dh )\n else:\n dc.DrawRoundedRectangle( dx,dy,dw,dh, self.padding )\n else:\n dc.DrawRoundedRectangle( dx,dy,dw,dh, self.padding*3 )\n# self.DrawIconAndLabel(dc, node, x, y, w, h, depth)\n children_hot_map = []\n hot_map.append( (wx.Rect( int(x),int(y),int(w),int(h)), node, children_hot_map ) )\n x += self.padding\n y += self.padding\n w -= self.padding*2\n h -= self.padding*2\n \n\n empty = self.adapter.empty( node )\n icon_drawn = False\n if self.max_depth and depth == self.max_depth:\n self.DrawIconAndLabel(dc, node, x, y, w, h, depth)\n icon_drawn = True\n elif empty:\n # is a fraction of the space which is empty...\n log.debug( ' empty space fraction: %s', empty )\n new_h = h * (1.0-empty)\n self.DrawIconAndLabel(dc, node, x, y, w, h-new_h, depth)\n icon_drawn = True\n y += (h-new_h)\n h = new_h\n\n if w >self.padding*2 and h> self.padding*2:\n children = self.adapter.children( node )\n if children:\n log.debug( ' children: %s', children )\n self.LayoutChildren( dc, children, node, x,y,w,h, children_hot_map, depth+1 )\n else:\n log.debug( ' no children' )\n if not icon_drawn:\n self.DrawIconAndLabel(dc, node, x, y, w, h, depth)\n else:\n log.debug( ' not enough space: children skipped' )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef DrawIconAndLabel(self, dc, node, x, y, w, h, depth):\n ''' Draw the icon, if any, and the label, if any, of the node. '''\n if w-2 < self._em_size_//2 or h-2 < self._em_size_ //2:\n return\n dc.SetClippingRegion(x+1, y+1, w-2, h-2) # Don't draw outside the box\n try:\n icon = self.adapter.icon(node, node==self.selectedNode)\n if icon and h >= icon.GetHeight() and w >= icon.GetWidth():\n iconWidth = icon.GetWidth() + 2\n dc.DrawIcon(icon, x+2, y+2)\n else:\n iconWidth = 0\n if self.labels and h >= dc.GetTextExtent('ABC')[1]:\n dc.SetTextForeground(self.TextForegroundForNode(node, depth))\n dc.DrawText(self.adapter.label(node), x + iconWidth + 2, y+2)\n finally:\n dc.DestroyClippingRegion()", "response": "Draw the icon and label of the selected node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef overall( self, node ):\n return sum( [self.value(value,node) for value in self.children(node)] )", "response": "Calculate overall size of the node including children and empty space"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef children_sum( self, children,node ):\n return sum( [self.value(value,node) for value in children] )", "response": "Calculate children s total sum"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalculating empty space as a fraction of total space", "response": "def empty( self, node ):\n \"\"\"Calculate empty space as a fraction of total space\"\"\"\n overall = self.overall( node )\n if overall:\n return (overall - self.children_sum( self.children(node), node))/float(overall)\n return 0"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate the internal counter of the internal counter of the internal counter.", "response": "def update(self, n=1):\n \"\"\"\n Manually update the progress bar, useful for streams such as reading files (set init(total=filesize) and then in the reading loop, use update(len(current_buffer)) )\n\n Parameters\n ----------\n n : int\n Increment to add to the internal counter of iterations.\n \"\"\"\n if n < 1:\n n = 1\n self.n += n\n\n delta_it = self.n - self.last_print_n\n if delta >= self.miniters:\n # We check the counter first, to reduce the overhead of time.time()\n cur_t = time.time()\n if cur_t - self.last_print_t >= self.mininterval:\n self.sp.print_status(format_meter(self.n, self.total, cur_t-self.start_t, self.ncols, self.prefix, self.unit, self.unit_format, self.ascii))\n if self.dynamic_miniters: self.miniters = max(self.miniters, delta_it)\n self.last_print_n = self.n\n self.last_print_t = cur_t"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall this method to force print the last progress bar update based on the latest n value", "response": "def close(self):\n \"\"\"\n Call this method to force print the last progress bar update based on the latest n value\n \"\"\"\n if self.leave:\n if self.last_print_n < self.n:\n cur_t = time.time()\n self.sp.print_status(format_meter(self.n, self.total, cur_t-self.start_t, self.ncols, self.prefix, self.unit, self.unit_format, self.ascii))\n self.file.write('\\n')\n else:\n self.sp.print_status('')\n self.file.write('\\r')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add(self, value):\n if value not in self._set:\n self._set.add(value)\n self._list.add(value)", "response": "Add the element value to the set."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef copy(self):\n return self.__class__(key=self._key, load=self._load, _set=set(self._set))", "response": "Create a shallow copy of the sorted set."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a new set with elements in the set that are not in the given iterable.", "response": "def difference(self, *iterables):\n \"\"\"\n Return a new set with elements in the set that are not in the\n *iterables*.\n \"\"\"\n diff = self._set.difference(*iterables)\n new_set = self.__class__(key=self._key, load=self._load, _set=diff)\n return new_set"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef intersection(self, *iterables):\n comb = self._set.intersection(*iterables)\n new_set = self.__class__(key=self._key, load=self._load, _set=comb)\n return new_set", "response": "Return a new set with elements common to the set and all iterables."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a new set with elements in either self or that but not both.", "response": "def symmetric_difference(self, that):\n \"\"\"\n Return a new set with elements in either *self* or *that* but not both.\n \"\"\"\n diff = self._set.symmetric_difference(that)\n new_set = self.__class__(key=self._key, load=self._load, _set=diff)\n return new_set"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cookie_decode(data, key):\n ''' Verify and decode an encoded string. Return an object or None'''\n if isinstance(data, unicode): data = data.encode('ascii') #2to3 hack\n if cookie_is_encoded(data):\n sig, msg = data.split(u'?'.encode('ascii'),1) #2to3 hack\n if sig[1:] == base64.b64encode(hmac.new(key, msg).digest()):\n return pickle.loads(base64.b64decode(msg))\n return None", "response": "Verify and decode an encoded string. Return an object or None."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef cookie_is_encoded(data):\n ''' Verify and decode an encoded string. Return an object or None'''\n return bool(data.startswith(u'!'.encode('ascii')) and u'?'.encode('ascii') in data)", "response": "Verify and decode an encoded string. Return an object or None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef tonativefunc(enc='utf-8'):\n ''' Returns a function that turns everything into 'native' strings using enc '''\n if sys.version_info >= (3,0,0):\n return lambda x: x.decode(enc) if isinstance(x, bytes) else str(x)\n return lambda x: x.encode(enc) if isinstance(x, unicode) else str(x)", "response": "Returns a function that turns everything into native strings using enc"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add(self, *a, **ka):\n route = a[0] if a and isinstance(a[0], Route) else Route(*a, **ka)\n self.routes.append(route)\n if route.name:\n self.named[route.name] = route.format_str()\n if route.static:\n self.static[route.route] = route.target\n return\n gpatt = route.group_re()\n fpatt = route.flat_re()\n try:\n gregexp = re.compile('^(%s)$' % gpatt) if '(?P' in gpatt else None\n combined = '%s|(^%s$)' % (self.dynamic[-1][0].pattern, fpatt)\n self.dynamic[-1] = (re.compile(combined), self.dynamic[-1][1])\n self.dynamic[-1][1].append((route.target, gregexp))\n except (AssertionError, IndexError), e: # AssertionError: Too many groups\n self.dynamic.append((re.compile('(^%s$)'%fpatt),[(route.target, gregexp)]))\n except re.error, e:\n raise RouteSyntaxError(\"Could not add Route: %s (%s)\" % (route, e))", "response": "Adds a route - > target pair or a Route object to the Router."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmounting a Bottle application to a specific URL prefix.", "response": "def mount(self, app, script_path):\n ''' Mount a Bottle application to a specific URL prefix '''\n if not isinstance(app, Bottle):\n raise TypeError('Only Bottle instances are supported for now.')\n script_path = '/'.join(filter(None, script_path.split('/')))\n path_depth = script_path.count('/') + 1\n if not script_path:\n raise TypeError('Empty script_path. Perhaps you want a merge()?')\n for other in self.mounts:\n if other.startswith(script_path):\n raise TypeError('Conflict with existing mount: %s' % other)\n @self.route('/%s/:#.*#' % script_path, method=\"ANY\")\n def mountpoint():\n request.path_shift(path_depth)\n return app.handle(request.path, request.method)\n self.mounts[script_path] = app"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexecuting the handler bound to the specified url and method and return the result as a new HTTP response object.", "response": "def handle(self, url, method):\n \"\"\" Execute the handler bound to the specified url and method and return\n its output. If catchall is true, exceptions are catched and returned as\n HTTPError(500) objects. \"\"\"\n if not self.serve:\n return HTTPError(503, \"Server stopped\")\n\n handler, args = self.match_url(url, method)\n if not handler:\n return HTTPError(404, \"Not found:\" + url)\n\n try:\n return handler(**args)\n except HTTPResponse, e:\n return e\n except Exception, e:\n if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\\\n or not self.catchall:\n raise\n return HTTPError(500, 'Unhandled exception', e, format_exc(10))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntry to convert the output of a WSGI request into something WSGI compatible and set correct HTTP headers when possible.", "response": "def _cast(self, out, request, response, peek=None):\n \"\"\" Try to convert the parameter into something WSGI compatible and set\n correct HTTP headers when possible.\n Support: False, str, unicode, dict, HTTPResponse, HTTPError, file-like,\n iterable of strings and iterable of unicodes\n \"\"\"\n # Filtered types (recursive, because they may return anything)\n for testtype, filterfunc in self.castfilter:\n if isinstance(out, testtype):\n return self._cast(filterfunc(out), request, response)\n\n # Empty output is done here\n if not out:\n response.headers['Content-Length'] = 0\n return []\n # Join lists of byte or unicode strings. Mixed lists are NOT supported\n if isinstance(out, list) and isinstance(out[0], (StringType, unicode)):\n out = out[0][0:0].join(out) # b'abc'[0:0] -> b''\n # Encode unicode strings\n if isinstance(out, unicode):\n out = out.encode(response.charset)\n # Byte Strings are just returned\n if isinstance(out, StringType):\n response.headers['Content-Length'] = str(len(out))\n return [out]\n # HTTPError or HTTPException (recursive, because they may wrap anything)\n if isinstance(out, HTTPError):\n out.apply(response)\n return self._cast(self.error_handler.get(out.status, repr)(out), request, response)\n if isinstance(out, HTTPResponse):\n out.apply(response)\n return self._cast(out.output, request, response)\n\n # Cast Files into iterables\n if hasattr(out, 'read') and 'wsgi.file_wrapper' in request.environ:\n out = request.environ.get('wsgi.file_wrapper',\n lambda x, y: iter(lambda: x.read(y), ''))(out, 1024*64)\n\n # Handle Iterables. We peek into them to detect their inner type.\n try:\n out = iter(out)\n first = out.next()\n while not first:\n first = out.next()\n except StopIteration:\n return self._cast('', request, response)\n except HTTPResponse, e:\n first = e\n except Exception, e:\n first = HTTPError(500, 'Unhandled exception', e, format_exc(10))\n if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\\\n or not self.catchall:\n raise\n # These are the inner types allowed in iterator or generator objects.\n if isinstance(first, HTTPResponse):\n return self._cast(first, request, response)\n if isinstance(first, StringType):\n return itertools.chain([first], out)\n if isinstance(first, unicode):\n return itertools.imap(lambda x: x.encode(response.charset),\n itertools.chain([first], out))\n return self._cast(HTTPError(500, 'Unsupported response type: %s'\\\n % type(first)), request, response)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a HeaderDict filled with request headers.", "response": "def header(self):\n ''' :class:`HeaderDict` filled with request headers.\n\n HeaderDict keys are case insensitive str.title()d\n '''\n if self._header is None:\n self._header = HeaderDict()\n for key, value in self.environ.iteritems():\n if key.startswith('HTTP_'):\n key = key[5:].replace('_','-').title()\n self._header[key] = value\n return self._header"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef COOKIES(self):\n if self._COOKIES is None:\n raw_dict = SimpleCookie(self.environ.get('HTTP_COOKIE',''))\n self._COOKIES = {}\n for cookie in raw_dict.itervalues():\n self._COOKIES[cookie.key] = cookie.value\n return self._COOKIES", "response": "Returns a dictionary of cookie information parsed into a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_cookie(self, key, value, **kargs):\n if not isinstance(value, basestring):\n sec = self.app.config['securecookie.key']\n value = cookie_encode(value, sec).decode('ascii') #2to3 hack\n self.COOKIES[key] = value\n for k, v in kargs.iteritems():\n self.COOKIES[key][k.replace('_', '-')] = v", "response": "Add a new cookie with various options."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tamper_file_at(path, pos=0, replace_str=None):\n if not replace_str:\n replace_str = \"\\x00\"\n try:\n with open(path, \"r+b\") as fh:\n if pos < 0: # if negative, we calculate the position backward from the end of file\n fsize = os.fstat(fh.fileno()).st_size\n pos = fsize + pos\n fh.seek(pos)\n fh.write(replace_str)\n except IOError:\n return False\n finally:\n try:\n fh.close()\n except Exception:\n pass\n return True", "response": "Tamper a file at the given position and using the given string"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef tamper_dir(inputpath, *args, **kwargs):\n silent = kwargs.get('silent', False)\n if 'silent' in kwargs: del kwargs['silent']\n\n filescount = 0\n for _ in tqdm(recwalk(inputpath), desc='Precomputing', disable=silent):\n filescount += 1\n\n files_tampered = 0\n tamper_count = 0\n total_size = 0\n for dirname, filepath in tqdm(recwalk(inputpath), total=filescount, leave=True, desc='Tamper file n.', disable=silent):\n tcount, tsize = tamper_file(os.path.join(dirname, filepath), *args, **kwargs)\n if tcount > 0:\n tamper_count += tcount\n files_tampered += 1\n total_size += tsize\n return [files_tampered, filescount, tamper_count, total_size]", "response": "Randomly tamper the files content in a directory tree recursively."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsaving current stack trace as formatted string.", "response": "def _save_trace(self):\n \"\"\"\n Save current stack trace as formatted string.\n \"\"\"\n stack_trace = stack()\n try:\n self.trace = []\n for frm in stack_trace[5:]: # eliminate our own overhead\n self.trace.insert(0, frm[1:])\n finally:\n del stack_trace"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstore timestamp and current size for later evaluation.", "response": "def track_size(self, ts, sizer):\n \"\"\"\n Store timestamp and current size for later evaluation.\n The 'sizer' is a stateful sizing facility that excludes other tracked\n objects.\n \"\"\"\n obj = self.ref()\n self.snapshots.append(\n (ts, sizer.asized(obj, detail=self._resolution_level))\n )\n if obj is not None:\n self.repr = safe_repr(obj, clip=128)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_size_at_time(self, timestamp):\n size = 0\n for (t, s) in self.snapshots:\n if t == timestamp:\n size = s.size\n return size", "response": "Get the size of the object at a specific time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef run(self):\n self.stop = False\n while not self.stop:\n self.tracker.create_snapshot()\n sleep(self.interval)", "response": "Loop until a stop signal is set."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the total size of the process in bytes.", "response": "def total(self):\n \"\"\"\n Return the total (virtual) size of the process in bytes. If process\n information is not available, get the best number available, even if it\n is a poor approximation of reality.\n \"\"\"\n if self.system_total.available:\n return self.system_total.vsz\n elif self.asizeof_total: # pragma: no cover\n return self.asizeof_total\n else: # pragma: no cover\n return self.tracked_total"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning timestamped label for this snapshot or a raw timestamp.", "response": "def label(self):\n \"\"\"Return timestamped label for this snapshot, or a raw timestamp.\"\"\"\n if not self.desc:\n return \"%.3fs\" % self.timestamp\n return \"%s (%.3fs)\" % (self.desc, self.timestamp)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninjecting constructor for tracked classes. Call the actual constructor of the object and track the object. Attach to the object before calling the constructor to track the object with the parameters of the most specialized class.", "response": "def _tracker(self, _observer_, _self_, *args, **kwds):\n \"\"\"\n Injected constructor for tracked classes.\n Call the actual constructor of the object and track the object.\n Attach to the object before calling the constructor to track the object with\n the parameters of the most specialized class.\n \"\"\"\n self.track_object(_self_,\n name=_observer_.name,\n resolution_level=_observer_.detail,\n keep=_observer_.keep,\n trace=_observer_.trace)\n _observer_.init(_self_, *args, **kwds)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninjects a constructor into the class.", "response": "def _inject_constructor(self, cls, func, name, resolution_level, keep,\n trace):\n \"\"\"\n Modifying Methods in Place - after the recipe 15.7 in the Python\n Cookbook by Ken Seehof. The original constructors may be restored\n later.\n \"\"\"\n try:\n constructor = cls.__init__\n except AttributeError:\n def constructor(self, *_args, **_kwargs):\n pass\n\n # Possible name clash between keyword arguments of the tracked class'\n # constructor and the curried arguments of the injected constructor.\n # Therefore, the additional argument has a 'magic' name to make it less\n # likely that an argument name clash occurs.\n self._observers[cls] = _ClassObserver(constructor,\n name,\n resolution_level,\n keep,\n trace)\n cls.__init__ = instancemethod(\n lambda *args, **kwds: func(self._observers[cls], *args, **kwds),\n None,\n cls\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmodifying settings of a tracked class", "response": "def _track_modify(self, cls, name, detail, keep, trace):\n \"\"\"\n Modify settings of a tracked class\n \"\"\"\n self._observers[cls].modify(name, detail, keep, trace)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _restore_constructor(self, cls):\n cls.__init__ = self._observers[cls].init\n del self._observers[cls]", "response": "Restores the original constructor lose track of class.\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchanges tracking options for the already tracked object instance.", "response": "def track_change(self, instance, resolution_level=0):\n \"\"\"\n Change tracking options for the already tracked object 'instance'.\n If instance is not tracked, a KeyError will be raised.\n \"\"\"\n tobj = self.objects[id(instance)]\n tobj.set_resolution_level(resolution_level)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef track_object(self, instance, name=None, resolution_level=0, keep=False, trace=False):\n\n # Check if object is already tracked. This happens if track_object is\n # called multiple times for the same object or if an object inherits\n # from multiple tracked classes. In the latter case, the most\n # specialized class wins. To detect id recycling, the weak reference\n # is checked. If it is 'None' a tracked object is dead and another one\n # takes the same 'id'.\n if id(instance) in self.objects and \\\n self.objects[id(instance)].ref() is not None:\n return\n\n tobj = TrackedObject(instance, resolution_level=resolution_level, trace=trace)\n\n if name is None:\n name = instance.__class__.__name__\n if not name in self.index:\n self.index[name] = []\n self.index[name].append(tobj)\n self.objects[id(instance)] = tobj\n\n if keep:\n self._keepalive.append(instance)", "response": "Track an object and sample size and lifetime information."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef track_class(self, cls, name=None, resolution_level=0, keep=False, trace=False):\n if not isclass(cls):\n raise TypeError(\"only class objects can be tracked\")\n if name is None:\n name = cls.__module__ + '.' + cls.__name__\n if self._is_tracked(cls):\n self._track_modify(cls, name, resolution_level, keep, trace)\n else:\n self._inject_constructor(cls, self._tracker, name, resolution_level, keep, trace)", "response": "Track all objects of the given class."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef detach_all_classes(self):\n classes = list(self._observers.keys())\n for cls in classes:\n self.detach_class(cls)", "response": "Detach from all tracked classes."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndetaches from all tracked classes and objects and removes the original constructor and cleans up the tracking lists.", "response": "def detach_all(self):\n \"\"\"\n Detach from all tracked classes and objects.\n Restore the original constructors and cleanse the tracking lists.\n \"\"\"\n self.detach_all_classes()\n self.objects.clear()\n self.index.clear()\n self._keepalive[:] = []"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nstarting a thread which takes snapshots periodically.", "response": "def start_periodic_snapshots(self, interval=1.0):\n \"\"\"\n Start a thread which takes snapshots periodically. The `interval` specifies\n the time in seconds the thread waits between taking snapshots. The thread is\n started as a daemon allowing the program to exit. If periodic snapshots are\n already active, the interval is updated.\n \"\"\"\n if not self._periodic_thread:\n self._periodic_thread = PeriodicThread(self, interval, name='BackgroundMonitor')\n self._periodic_thread.setDaemon(True)\n self._periodic_thread.start()\n else:\n self._periodic_thread.interval = interval"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop_periodic_snapshots(self):\n if self._periodic_thread and self._periodic_thread.isAlive():\n self._periodic_thread.stop = True\n self._periodic_thread.join()\n self._periodic_thread = None", "response": "Stop the periodic snapshots of the current resource."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a snapshot of the current state of the class tracker.", "response": "def create_snapshot(self, description='', compute_total=False):\n \"\"\"\n Collect current per instance statistics and saves total amount of\n memory associated with the Python process.\n\n If `compute_total` is `True`, the total consumption of all objects\n known to *asizeof* is computed. The latter might be very slow if many\n objects are mapped into memory at the time the snapshot is taken.\n Therefore, `compute_total` is set to `False` by default.\n\n The overhead of the `ClassTracker` structure is also computed.\n\n Snapshots can be taken asynchronously. The function is protected with a\n lock to prevent race conditions.\n \"\"\"\n\n try:\n # TODO: It is not clear what happens when memory is allocated or\n # released while this function is executed but it will likely lead\n # to inconsistencies. Either pause all other threads or don't size\n # individual objects in asynchronous mode.\n self.snapshot_lock.acquire()\n\n timestamp = _get_time()\n\n sizer = asizeof.Asizer()\n objs = [tobj.ref() for tobj in list(self.objects.values())]\n sizer.exclude_refs(*objs)\n\n # The objects need to be sized in a deterministic order. Sort the\n # objects by its creation date which should at least work for non-parallel\n # execution. The \"proper\" fix would be to handle shared data separately.\n tracked_objects = list(self.objects.values())\n tracked_objects.sort(key=lambda x: x.birth)\n for tobj in tracked_objects:\n tobj.track_size(timestamp, sizer)\n\n snapshot = Snapshot()\n\n snapshot.timestamp = timestamp\n snapshot.tracked_total = sizer.total\n if compute_total:\n snapshot.asizeof_total = asizeof.asizeof(all=True, code=True)\n snapshot.system_total = pympler.process.ProcessMemoryInfo()\n snapshot.desc = str(description)\n\n # Compute overhead of all structures, use sizer to exclude tracked objects(!)\n snapshot.overhead = 0\n if snapshot.tracked_total:\n snapshot.overhead = sizer.asizeof(self)\n if snapshot.asizeof_total:\n snapshot.asizeof_total -= snapshot.overhead\n\n self.snapshots.append(snapshot)\n\n finally:\n self.snapshot_lock.release()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_required(action):\n '''_actions which are positional or possessing the `required` flag '''\n return not action.option_strings and not isinstance(action, _SubParsersAction) or action.required == True", "response": "Return True if the action is required."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nextracts the metadata fields from a raw ecc entry.", "response": "def entry_fields(entry, field_delim=\"\\xFF\"):\n '''From a raw ecc entry (a string), extract the metadata fields (filename, filesize, ecc for both), and the rest being blocks of hash and ecc per blocks of the original file's header'''\n entry = entry.lstrip(field_delim) # if there was some slight adjustment error (example: the last ecc block of the last file was the field_delim, then we will start with a field_delim, and thus we need to remove the trailing field_delim which is useless and will make the field detection buggy). This is not really a big problem for the previous file's ecc block: the missing ecc characters (which were mistaken for a field_delim), will just be missing (so we will lose a bit of resiliency for the last block of the previous file, but that's not a huge issue, the correction can still rely on the other characters).\n\n # Find metadata fields delimiters positions\n # TODO: automate this part, just give in argument the number of field_delim to find, and the func will find the x field_delims (the number needs to be specified in argument because the field_delim can maybe be found wrongly inside the ecc stream, which we don't want)\n first = entry.find(field_delim)\n second = entry.find(field_delim, first+len(field_delim))\n third = entry.find(field_delim, second+len(field_delim))\n fourth = entry.find(field_delim, third+len(field_delim))\n # Note: we do not try to find all the field delimiters because we optimize here: we just walk the string to find the exact number of field_delim we are looking for, and after we stop, no need to walk through the whole string.\n\n # Extract the content of the fields\n # Metadata fields\n relfilepath = entry[:first]\n filesize = entry[first+len(field_delim):second]\n relfilepath_ecc = entry[second+len(field_delim):third]\n filesize_ecc = entry[third+len(field_delim):fourth]\n # Ecc stream field (aka ecc blocks)\n ecc_field = entry[fourth+len(field_delim):]\n\n # Try to convert to an int, an error may happen\n try:\n filesize = int(filesize)\n except Exception, e:\n print(\"Exception when trying to detect the filesize in ecc field (it may be corrupted), skipping: \")\n print(e)\n #filesize = 0 # avoid setting to 0, we keep as an int so that we can try to fix using intra-ecc\n\n # entries = [ {\"message\":, \"ecc\":, \"hash\":}, etc.]\n #print(entry)\n #print(len(entry))\n return {\"relfilepath\": relfilepath, \"relfilepath_ecc\": relfilepath_ecc, \"filesize\": filesize, \"filesize_ecc\": filesize_ecc, \"ecc_field\": ecc_field}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef entry_assemble(entry_fields, ecc_params, header_size, filepath, fileheader=None):\n '''From an entry with its parameters (filename, filesize), assemble a list of each block from the original file along with the relative hash and ecc for easy processing later.'''\n # Extract the header from the file\n if fileheader is None:\n with open(filepath, 'rb') as file: # filepath is the absolute path to the original file (the one with maybe corruptions, NOT the output repaired file!)\n # Compute the size of the buffer to read: either header_size if possible, but if the file is smaller than that then we will read the whole file.\n if entry_fields[\"filesize\"] > 0 and entry_fields[\"filesize\"] < header_size:\n fileheader = file.read(entry_fields[\"filesize\"])\n else:\n fileheader = file.read(header_size)\n\n # Cut the header and the ecc entry into blocks, and then assemble them so that we can easily process block by block\n entry_asm = []\n for i, j in itertools.izip(xrange(0, len(fileheader), ecc_params[\"message_size\"]), xrange(0, len(entry_fields[\"ecc_field\"]), ecc_params[\"hash_size\"] + ecc_params[\"ecc_size\"])):\n # Extract each fields from each block\n mes = fileheader[i:i+ecc_params[\"message_size\"]]\n hash = entry_fields[\"ecc_field\"][j:j+ecc_params[\"hash_size\"]]\n ecc = entry_fields[\"ecc_field\"][j+ecc_params[\"hash_size\"]:j+ecc_params[\"hash_size\"]+ecc_params[\"ecc_size\"]]\n entry_asm.append({\"message\": mes, \"hash\": hash, \"ecc\": ecc})\n\n # Return a list of fields for each block\n return entry_asm", "response": "Assemble a list of each block from the original file along with the relative hash and ecc for easy processing later."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef compute_ecc_hash(ecc_manager, hasher, buf, max_block_size, rate, message_size=None, as_string=False):\n '''Split a string in blocks given max_block_size and compute the hash and ecc for each block, and then return a nice list with both for easy processing.'''\n result = []\n # If required parameters were not provided, we compute them\n if not message_size:\n ecc_params = compute_ecc_params(max_block_size, rate, hasher)\n message_size = ecc_params[\"message_size\"]\n # Split the buffer string in blocks (necessary for Reed-Solomon encoding because it's limited to 255 characters max)\n for i in xrange(0, len(buf), message_size):\n # Compute the message block\n mes = buf[i:i+message_size]\n # Compute the ecc\n ecc = ecc_manager.encode(mes)\n # Compute the hash\n hash = hasher.hash(mes)\n #crc = zlib.crc32(mes) # DEPRECATED: CRC is not resilient enough\n #print(\"mes %i (%i) - ecc %i (%i) - hash %i (%i)\" % (len(mes), message_size, len(ecc), ecc_params[\"ecc_size\"], len(hash), ecc_params[\"hash_size\"])) # DEBUGLINE\n\n # Return the result (either in string for easy writing into a file, or in a list for easy post-processing)\n if as_string:\n result.append(\"%s%s\" % (str(hash),str(ecc)))\n else:\n result.append([hash, ecc])\n return result", "response": "Split a string in blocks given max_block_size and compute the hash and ecc for each block and then return a nice list with both for easy writing into a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncorrecting an intra - field with its corresponding intra - ecc if necessary", "response": "def ecc_correct_intra(ecc_manager_intra, ecc_params_intra, field, ecc, enable_erasures=False, erasures_char=\"\\x00\", only_erasures=False):\n \"\"\" Correct an intra-field with its corresponding intra-ecc if necessary \"\"\"\n fentry_fields = {\"ecc_field\": ecc}\n field_correct = [] # will store each block of the corrected (or already correct) filepath\n fcorrupted = False # check if field was corrupted\n fcorrected = True # check if field was corrected (if it was corrupted)\n errmsg = ''\n # Decode each block of the filepath\n for e in entry_assemble(fentry_fields, ecc_params_intra, len(field), '', field):\n # Check if this block of the filepath is OK, if yes then we just copy it over\n if ecc_manager_intra.check(e[\"message\"], e[\"ecc\"]):\n field_correct.append(e[\"message\"])\n else: # Else this block is corrupted, we will try to fix it using the ecc\n fcorrupted = True\n # Repair the message block and the ecc\n try:\n repaired_block, repaired_ecc = ecc_manager_intra.decode(e[\"message\"], e[\"ecc\"], enable_erasures=enable_erasures, erasures_char=erasures_char, only_erasures=only_erasures)\n except (ReedSolomonError, RSCodecError), exc: # the reedsolo lib may raise an exception when it can't decode. We ensure that we can still continue to decode the rest of the file, and the other files.\n repaired_block = None\n repaired_ecc = None\n errmsg += \"- Error: metadata field at offset %i: %s\\n\" % (entry_pos[0], exc)\n # Check if the block was successfully repaired: if yes then we copy the repaired block...\n if repaired_block is not None and ecc_manager_intra.check(repaired_block, repaired_ecc):\n field_correct.append(repaired_block)\n else: # ... else it failed, then we copy the original corrupted block and report an error later\n field_correct.append(e[\"message\"])\n fcorrected = False\n # Join all the blocks into one string to build the final filepath\n if isinstance(field_correct[0], bytearray): field_correct = [str(x) for x in field_correct] # workaround when using --ecc_algo 3 or 4, because we get a list of bytearrays instead of str\n field = ''.join(field_correct)\n # Report errors\n return (field, fcorrupted, fcorrected, errmsg)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _eliminate_leafs(self, graph):\n result = []\n idset = set([id(x) for x in graph])\n for n in graph:\n refset = set([id(x) for x in get_referents(n)])\n if refset.intersection(idset):\n result.append(n)\n return result", "response": "Eliminate leaf objects - that are objects not referencing any other\n objects in the list graph. Returns the list of objects without the leafs."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _reduce_to_cycles(self):\n cycles = self.objects[:]\n cnt = 0\n while cnt != len(cycles):\n cnt = len(cycles)\n cycles = self._eliminate_leafs(cycles)\n self.objects = cycles\n return len(self.objects)", "response": "Reduce the set of objects to only those\n that build cycles. Return the number of objects involved in reference\n ."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreducing the set of objects to only those those that build cycles. Return the reduced graph.", "response": "def reduce_to_cycles(self):\n \"\"\"\n Iteratively eliminate leafs to reduce the set of objects to only those\n that build cycles. Return the reduced graph. If there are no cycles,\n None is returned.\n \"\"\"\n if not self._reduced:\n reduced = copy(self)\n reduced.objects = self.objects[:]\n reduced.metadata = []\n reduced.edges = []\n self.num_in_cycles = reduced._reduce_to_cycles()\n reduced.num_in_cycles = self.num_in_cycles\n if self.num_in_cycles:\n reduced._get_edges()\n reduced._annotate_objects()\n for meta in reduced.metadata:\n meta.cycle = True\n else:\n reduced = None\n self._reduced = reduced\n return self._reduced"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_edges(self):\n idset = set([id(x) for x in self.objects])\n self.edges = set([])\n for n in self.objects:\n refset = set([id(x) for x in get_referents(n)])\n for ref in refset.intersection(idset):\n label = ''\n members = None\n if isinstance(n, dict):\n members = n.items()\n if not members:\n members = named_refs(n)\n for (k, v) in members:\n if id(v) == ref:\n label = k\n break\n self.edges.add(_Edge(id(n), ref, label))", "response": "Compute the edges for the reference graph."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nannotates the objects belonging to separate graphs with their respective groups.", "response": "def _annotate_groups(self):\n \"\"\"\n Annotate the objects belonging to separate (non-connected) graphs with\n individual indices.\n \"\"\"\n g = {}\n for x in self.metadata:\n g[x.id] = x\n\n idx = 0\n for x in self.metadata:\n if not hasattr(x, 'group'):\n x.group = idx\n idx += 1\n neighbors = set()\n for e in self.edges:\n if e.src == x.id:\n neighbors.add(e.dst)\n if e.dst == x.id:\n neighbors.add(e.src)\n for nb in neighbors:\n g[nb].group = min(x.group, getattr(g[nb], 'group', idx))\n\n # Assign the edges to the respective groups. Both \"ends\" of the edge\n # should share the same group so just use the first object's group.\n for e in self.edges:\n e.group = g[e.src].group\n\n self._max_group = idx"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn True if the group is non - empty. Otherwise returns False.", "response": "def _filter_group(self, group):\n \"\"\"\n Eliminate all objects but those which belong to `group`.\n ``self.objects``, ``self.metadata`` and ``self.edges`` are modified.\n Returns `True` if the group is non-empty. Otherwise returns `False`.\n \"\"\"\n self.metadata = [x for x in self.metadata if x.group == group]\n group_set = set([x.id for x in self.metadata])\n self.objects = [obj for obj in self.objects if id(obj) in group_set]\n self.count = len(self.metadata)\n if self.metadata == []:\n return False\n\n self.edges = [e for e in self.edges if e.group == group]\n\n del self._max_group\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsplitting the reference graph into sub - graphs.", "response": "def split(self):\n \"\"\"\n Split the graph into sub-graphs. Only connected objects belong to the\n same graph. `split` yields copies of the Graph object. Shallow copies\n are used that only replicate the meta-information, but share the same\n object list ``self.objects``.\n\n >>> from pympler.refgraph import ReferenceGraph\n >>> a = 42\n >>> b = 'spam'\n >>> c = {a: b}\n >>> t = (1,2,3)\n >>> rg = ReferenceGraph([a,b,c,t])\n >>> for subgraph in rg.split():\n ... print subgraph.index\n 0\n 1\n \"\"\"\n self._annotate_groups()\n index = 0\n\n for group in range(self._max_group):\n subgraph = copy(self)\n subgraph.metadata = self.metadata[:]\n subgraph.edges = self.edges.copy()\n\n if subgraph._filter_group(group):\n subgraph.total_size = sum([x.size for x in subgraph.metadata])\n subgraph.index = index\n index += 1\n yield subgraph"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef split_and_sort(self):\n graphs = list(self.split())\n graphs.sort(key=lambda x: -len(x.metadata))\n for index, graph in enumerate(graphs):\n graph.index = index\n return graphs", "response": "Split the graphs into sub graphs and sort the graphs by the number of nodes."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _annotate_objects(self):\n self.metadata = []\n sizer = Asizer()\n sizes = sizer.asizesof(*self.objects)\n self.total_size = sizer.total\n for obj, sz in zip(self.objects, sizes):\n md = _MetaObject()\n md.size = sz\n md.id = id(obj)\n try:\n md.type = obj.__class__.__name__\n except (AttributeError, ReferenceError): # pragma: no cover\n md.type = type(obj).__name__\n md.str = safe_repr(obj, clip=128)\n self.metadata.append(md)", "response": "Extract meta - data describing the stored objects."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a string representation of the object with graphviz.", "response": "def _get_graphviz_data(self):\n \"\"\"\n Emit a graph representing the connections between the objects described\n within the metadata list. The text representation can be transformed to\n a graph with graphviz. Returns a string.\n \"\"\"\n s = []\n header = '// Process this file with graphviz\\n'\n s.append( header)\n s.append('digraph G {\\n')\n s.append(' node [shape=box];\\n')\n for md in self.metadata:\n label = trunc(md.str, 48).replace('\"', \"'\")\n extra = ''\n if md.type == 'instancemethod':\n extra = ', color=red'\n elif md.type == 'frame':\n extra = ', color=orange'\n s.append(' \"X%s\" [ label = \"%s\\\\n%s\" %s ];\\n' % \\\n (hex(md.id)[1:], label, md.type, extra))\n for e in self.edges:\n extra = ''\n if e.label == '__dict__':\n extra = ',weight=100'\n s.append(' X%s -> X%s [label=\"%s\"%s];\\n' % \\\n (hex(e.src)[1:], hex(e.dst)[1:], e.label, extra))\n\n s.append('}\\n')\n return \"\".join(s)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef render(self, filename, cmd='dot', format='ps', unflatten=False):\n if self.objects == []:\n return False\n\n data = self._get_graphviz_data()\n\n options = ('-Nfontsize=10',\n '-Efontsize=10',\n '-Nstyle=filled',\n '-Nfillcolor=#E5EDB8',\n '-Ncolor=#CCCCCC')\n cmdline = (cmd, '-T%s' % format, '-o', filename) + options\n\n if unflatten:\n p1 = Popen(('unflatten', '-l7'), stdin=PIPE, stdout=PIPE,\n **popen_flags)\n p2 = Popen(cmdline, stdin=p1.stdout, **popen_flags)\n p1.communicate(encode4pipe(data))\n p2.communicate()\n return p2.returncode == 0\n else:\n p = Popen(cmdline, stdin=PIPE, **popen_flags)\n p.communicate(encode4pipe(data))\n return p.returncode == 0", "response": "Render the object to filename using graphviz."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef write_graph(self, filename):\n f = open(filename, 'w')\n f.write(self._get_graphviz_data())\n f.close()", "response": "Write raw graph data to a file"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the parsed result in the form of a tree of Frame objects where the first element is the name of the frame and the second is the time of the frame.", "response": "def root_frame(self):\n \"\"\"\n Returns the parsed results in the form of a tree of Frame objects\n \"\"\"\n if not hasattr(self, '_root_frame'):\n self._root_frame = Frame()\n\n # define a recursive function that builds the hierarchy of frames given the\n # stack of frame identifiers\n def frame_for_stack(stack):\n if len(stack) == 0:\n return self._root_frame\n\n parent = frame_for_stack(stack[:-1])\n frame_name = stack[-1]\n\n if not frame_name in parent.children_dict:\n parent.add_child(Frame(frame_name, parent))\n\n return parent.children_dict[frame_name]\n\n for stack, self_time in self.stack_self_time.items():\n frame_for_stack(stack).self_time = self_time\n\n return self._root_frame"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reset_trace():\n global call_dict\n global call_stack\n global func_count\n global func_count_max\n global func_time\n global func_time_max\n global call_stack_timer\n\n call_dict = {}\n\n # current call stack\n call_stack = ['__main__']\n\n # counters for each function\n func_count = {}\n func_count_max = 0\n\n # accumative time per function\n func_time = {}\n func_time_max = 0\n\n # keeps track of the start time of each call on the stack\n call_stack_timer = []", "response": "Resets all collected statistics. This is run automatically by the module load_module and reset_trace."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn True if the file_name is in the lib directory.", "response": "def is_module_stdlib(file_name):\n \"\"\"Returns True if the file_name is in the lib directory.\"\"\"\n # TODO: Move these calls away from this function so it doesn't have to run\n # every time.\n lib_path = sysconfig.get_python_lib()\n path = os.path.split(lib_path)\n if path[1] == 'site-packages':\n lib_path = path[0]\n return file_name.lower().startswith(lib_path.lower())"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef start_trace(reset=True, filter_func=None, time_filter_func=None):\n global trace_filter\n global time_filter\n if reset:\n reset_trace()\n\n if filter_func:\n trace_filter = filter_func\n else:\n trace_filter = GlobbingFilter(exclude=['pycallgraph.*'])\n\n if time_filter_func:\n time_filter = time_filter_func\n else:\n time_filter = GlobbingFilter()\n\n sys.settrace(tracer)", "response": "Starts a trace for the current call graph."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef tracer(frame, event, arg):\n global func_count_max\n global func_count\n global trace_filter\n global time_filter\n global call_stack\n global func_time\n global func_time_max\n\n if event == 'call':\n keep = True\n code = frame.f_code\n\n # Stores all the parts of a human readable name of the current call.\n full_name_list = []\n\n # Work out the module name\n module = inspect.getmodule(code)\n if module:\n module_name = module.__name__\n module_path = module.__file__\n if not settings['include_stdlib'] \\\n and is_module_stdlib(module_path):\n keep = False\n if module_name == '__main__':\n module_name = ''\n else:\n module_name = ''\n if module_name:\n full_name_list.append(module_name)\n\n # Work out the class name.\n try:\n class_name = frame.f_locals['self'].__class__.__name__\n full_name_list.append(class_name)\n except (KeyError, AttributeError):\n class_name = ''\n\n # Work out the current function or method\n func_name = code.co_name\n if func_name == '?':\n func_name = '__main__'\n full_name_list.append(func_name)\n\n # Create a readable representation of the current call\n full_name = '.'.join(full_name_list)\n\n # Load the trace filter, if any. 'keep' determines if we should ignore\n # this call\n if keep and trace_filter:\n keep = trace_filter(call_stack, module_name, class_name,\n func_name, full_name)\n\n # Store the call information\n if keep:\n\n if call_stack:\n fr = call_stack[-1]\n else:\n fr = None\n if fr not in call_dict:\n call_dict[fr] = {}\n if full_name not in call_dict[fr]:\n call_dict[fr][full_name] = 0\n call_dict[fr][full_name] += 1\n\n if full_name not in func_count:\n func_count[full_name] = 0\n func_count[full_name] += 1\n if func_count[full_name] > func_count_max:\n func_count_max = func_count[full_name]\n\n call_stack.append(full_name)\n call_stack_timer.append(time.time())\n\n else:\n call_stack.append('')\n call_stack_timer.append(None)\n\n if event == 'return':\n if call_stack:\n full_name = call_stack.pop(-1)\n if call_stack_timer:\n t = call_stack_timer.pop(-1)\n else:\n t = None\n if t and time_filter(stack=call_stack, full_name=full_name):\n if full_name not in func_time:\n func_time[full_name] = 0\n call_time = (time.time() - t)\n func_time[full_name] += call_time\n if func_time[full_name] > func_time_max:\n func_time_max = func_time[full_name]\n\n return tracer", "response": "This is the internal function that is called every time a call is made. It is called every time a call is made. It keeps track of relationships between calls between calls."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a string containing a DOT file.", "response": "def get_dot(stop=True):\n \"\"\"Returns a string containing a DOT file. Setting stop to True will cause\n the trace to stop.\n \"\"\"\n defaults = []\n nodes = []\n edges = []\n\n\n # define default attributes\n for comp, comp_attr in graph_attributes.items():\n attr = ', '.join( '%s = \"%s\"' % (attr, val)\n for attr, val in comp_attr.items() )\n defaults.append( '\\t%(comp)s [ %(attr)s ];\\n' % locals() )\n\n # define nodes\n for func, hits in func_count.items():\n calls_frac, total_time_frac, total_time = _frac_calculation(func, hits)\n col = settings['node_colour'](calls_frac, total_time_frac)\n attribs = ['%s=\"%s\"' % a for a in settings['node_attributes'].items()]\n node_str = '\"%s\" [%s];' % (func, ', '.join(attribs))\n nodes.append( node_str % locals() )\n\n # define edges\n for fr_key, fr_val in call_dict.items():\n if not fr_key: continue\n for to_key, to_val in fr_val.items():\n calls_frac, total_time_frac, totla_time = \\\n _frac_calculation(to_key, to_val)\n col = settings['edge_colour'](calls_frac, total_time_frac)\n edge = '[ color = \"%s\", label=\"%s\" ]' % (col, to_val)\n edges.append('\"%s\"->\"%s\" %s;' % (fr_key, to_key, edge))\n\n defaults = '\\n\\t'.join( defaults )\n nodes = '\\n\\t'.join( nodes )\n edges = '\\n\\t'.join( edges )\n\n dot_fmt = (\"digraph G {\\n\"\n \"\t%(defaults)s\\n\\n\"\n \"\t%(nodes)s\\n\\n\"\n \"\t%(edges)s\\n}\\n\"\n )\n return dot_fmt % locals()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_gdf(stop=True):\n ret = ['nodedef>name VARCHAR, label VARCHAR, hits INTEGER, ' + \\\n 'calls_frac DOUBLE, total_time_frac DOUBLE, ' + \\\n 'total_time DOUBLE, color VARCHAR, width DOUBLE']\n for func, hits in func_count.items():\n calls_frac, total_time_frac, total_time = _frac_calculation(func, hits)\n col = settings['node_colour'](calls_frac, total_time_frac)\n color = ','.join([str(round(float(c) * 255)) for c in col.split()])\n ret.append('%s,%s,%s,%s,%s,%s,\\'%s\\',%s' % (func, func, hits, \\\n calls_frac, total_time_frac, total_time, color, \\\n math.log(hits * 10)))\n ret.append('edgedef>node1 VARCHAR, node2 VARCHAR, color VARCHAR')\n for fr_key, fr_val in call_dict.items():\n if fr_key == '':\n continue\n for to_key, to_val in fr_val.items():\n calls_frac, total_time_frac, total_time = \\\n _frac_calculation(to_key, to_val)\n col = settings['edge_colour'](calls_frac, total_time_frac)\n color = ','.join([str(round(float(c) * 255)) for c in col.split()])\n ret.append('%s,%s,\\'%s\\'' % (fr_key, to_key, color))\n ret = '\\n'.join(ret)\n return ret", "response": "Returns a string containing a GDF file."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a graph from the dot language file specified by filename.", "response": "def make_dot_graph(filename, format='png', tool='dot', stop=True):\n \"\"\"Creates a graph using a Graphviz tool that supports the dot language. It\n will output into a file specified by filename with the format specified.\n Setting stop to True will stop the current trace.\n \"\"\"\n if stop:\n stop_trace()\n\n dot_data = get_dot()\n\n # normalize filename\n regex_user_expand = re.compile('\\A~')\n if regex_user_expand.match(filename):\n filename = os.path.expanduser(filename)\n else:\n filename = os.path.expandvars(filename) # expand, just in case\n\n if format == 'dot':\n f = open(filename, 'w')\n f.write(dot_data)\n f.close()\n\n else:\n # create a temporary file to be used for the dot data\n fd, tempname = tempfile.mkstemp()\n with os.fdopen(fd, 'w') as f:\n f.write(dot_data)\n\n cmd = '%(tool)s -T%(format)s -o%(filename)s %(tempname)s' % locals()\n try:\n ret = os.system(cmd)\n if ret:\n raise PyCallGraphException( \\\n 'The command \"%(cmd)s\" failed with error ' \\\n 'code %(ret)i.' % locals())\n finally:\n os.unlink(tempname)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef make_gdf_graph(filename, stop=True):\n if stop:\n stop_trace()\n\n try:\n f = open(filename, 'w')\n f.write(get_gdf())\n finally:\n if f: f.close()", "response": "Create a graph in simple GDF format suitable for feeding into Gephi"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshim to run 32 - bit on 64 - bit mac as a sub - process", "response": "def macshim():\n \"\"\"Shim to run 32-bit on 64-bit mac as a sub-process\"\"\"\n import subprocess, sys\n subprocess.call([\n sys.argv[0] + '32'\n ]+sys.argv[1:], \n env={\"VERSIONER_PYTHON_PREFER_32_BIT\":\"yes\"}\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef file_supports_color(file_obj):\n plat = sys.platform\n supported_platform = plat != 'Pocket PC' and (plat != 'win32' or\n 'ANSICON' in os.environ)\n\n is_a_tty = hasattr(file_obj, 'isatty') and file_obj.isatty()\n if not supported_platform or not is_a_tty:\n return False\n return True", "response": "Returns True if the running system s terminal supports color and False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the ecc parameters for the ECCMan object.", "response": "def compute_ecc_params(max_block_size, rate, hasher):\n '''Compute the ecc parameters (size of the message, size of the hash, size of the ecc). This is an helper function to easily compute the parameters from a resilience rate to instanciate an ECCMan object.'''\n #message_size = max_block_size - int(round(max_block_size * rate * 2, 0)) # old way to compute, wasn't really correct because we applied the rate on the total message+ecc size, when we should apply the rate to the message size only (that is not known beforehand, but we want the ecc size (k) = 2*rate*message_size or in other words that k + k * 2 * rate = n)\n message_size = int(round(float(max_block_size) / (1 + 2*rate), 0))\n ecc_size = max_block_size - message_size\n hash_size = len(hasher) # 32 when we use MD5\n return {\"message_size\": message_size, \"ecc_size\": ecc_size, \"hash_size\": hash_size}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef detect_reedsolomon_parameters(message, mesecc_orig, gen_list=[2, 3, 5], c_exp=8):\n '''Use an exhaustive search to automatically find the correct parameters for the ReedSolomon codec from a sample message and its encoded RS code.\n Arguments: message is the sample message, eg, \"hello world\" ; mesecc_orig is the message variable encoded with RS block appended at the end.\n '''\n # Description: this is basically an exhaustive search where we will try every possible RS parameter, then try to encode the sample message, and see if the resulting RS code is close to the supplied code.\n # All variables except the Galois Field's exponent are automatically generated and searched.\n # To compare with the supplied RS code, we compute the Hamming distance, so that even if the RS code is tampered, we can still find the closest set of RS parameters to decode this message.\n # The goal is to provide users a function so that they can use the \"hello world\" sample string in generated ECC files to recover their RS parameters in case they forget them. But users can use any sample message: for example, if they have an untampered file and its relative ecc track, they can use the ecc track as the mesecc_orig and their original file as the sample message.\n\n from .reedsolomon import reedsolo as reedsolop # need to import another time the reedsolo library for detect_reedsolomon_parameters to work (because we need to reinit all the tables, and they are declared module-wide, so this would conflict with decoding)\n\n # Init the variables\n n = len(mesecc_orig)\n k = len(message)\n field_charac = int((2**c_exp) - 1)\n maxval1 = max([ord(x) if isinstance(x, basestring) else x for x in message ])\n maxval2 = max([ord(x) if isinstance(x, basestring) else x for x in mesecc_orig])\n maxval = max([maxval1, maxval2])\n if (maxval > field_charac):\n raise ValueError(\"The specified field's exponent is wrong, the message contains values (%i) above the field's cardinality (%i)!\" % (maxval, field_charac))\n\n # Prepare the variable that will store the result\n best_match = {\"hscore\": -1, \"params\": [{\"gen_nb\": 0, \"prim\": 0, \"fcr\": 0}]}\n\n # Exhaustively search by generating every combination of values for the RS parameters and test the Hamming distance\n for gen_nb in gen_list:\n prim_list = reedsolop.find_prime_polys(generator=gen_nb, c_exp=c_exp, fast_primes=False, single=False)\n for prim in prim_list:\n reedsolop.init_tables(prim)\n for fcr in xrange(field_charac):\n #g = reedsolop.rs_generator_poly_all(n, fcr=fcr, generator=gen_nb)\n # Generate a RS code from the sample message using the current combination of RS parameters\n mesecc = reedsolop.rs_encode_msg(message, n-k, fcr=fcr)\n # Compute the Hamming distance\n h = hamming(mesecc, mesecc_orig)\n # If the Hamming distance is lower than the previous best match (or if it's the first try), save this set of parameters\n if best_match[\"hscore\"] == -1 or h <= best_match[\"hscore\"]:\n # If the distance is strictly lower than for the previous match, then we replace the previous match with the current one\n if best_match[\"hscore\"] == -1 or h < best_match[\"hscore\"]:\n best_match[\"hscore\"] = h\n best_match[\"params\"] = [{\"gen_nb\": gen_nb, \"prim\": prim, \"fcr\": fcr}]\n # Else there is an ambiguity: the Hamming distance is the same as for the previous best match, so we keep the previous set of parameters but we append the current set\n elif h == best_match[\"hscore\"]:\n best_match[\"params\"].append({\"gen_nb\": gen_nb, \"prim\": prim, \"fcr\": fcr})\n # If Hamming distance is 0, then we have found a perfect match (the current set of parameters allow to generate the exact same RS code from the sample message), so we stop here\n if h == 0: break\n\n # Printing the results to the user\n if best_match[\"hscore\"] >= 0 and best_match[\"hscore\"] < len(mesecc_orig):\n perfect_match_str = \" (0=perfect match)\" if best_match[\"hscore\"]==0 else \"\"\n result = ''\n result += \"Found closest set of parameters, with Hamming distance %i%s:\\n\" % (best_match[\"hscore\"], perfect_match_str)\n for param in best_match[\"params\"]:\n result += \"gen_nb=%s prim=%s(%s) fcr=%s\\n\" % (param[\"gen_nb\"], param[\"prim\"], hex(param[\"prim\"]), param[\"fcr\"])\n return result\n else:\n return \"Parameters could not be automatically detected...\"", "response": "Detects the correct parameters for the ReedSolomon codec from a sample message and its encoded RS code."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef encode(self, message, k=None):\n '''Encode one message block (up to 255) into an ecc'''\n if not k: k = self.k\n message, _ = self.pad(message, k=k)\n if self.algo == 1:\n mesecc = self.ecc_manager.encode(message, k=k)\n elif self.algo == 2:\n mesecc = self.ecc_manager.encode_fast(message, k=k)\n elif self.algo == 3 or self.algo == 4:\n mesecc = rs_encode_msg(message, self.n-k, fcr=self.fcr, gen=self.g[self.n-k])\n #mesecc = rs_encode_msg_precomp(message, self.n-k, fcr=self.fcr, gen=self.g[self.n-k])\n\n ecc = mesecc[len(message):]\n return ecc", "response": "Encode one message block into an ecc"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrepairing a message and its ecc also given the message and its ecc", "response": "def decode(self, message, ecc, k=None, enable_erasures=False, erasures_char=\"\\x00\", only_erasures=False):\n '''Repair a message and its ecc also, given the message and its ecc (both can be corrupted, we will still try to fix both of them)'''\n if not k: k = self.k\n\n # Optimization, use bytearray\n if isinstance(message, _str):\n message = bytearray([ord(x) for x in message])\n ecc = bytearray([ord(x) for x in ecc])\n\n # Detect erasures positions and replace with null bytes (replacing erasures with null bytes is necessary for correct syndrome computation)\n # Note that this must be done before padding, else we risk counting the padded null bytes as erasures!\n erasures_pos = None\n if enable_erasures:\n # Concatenate to find erasures in the whole codeword\n mesecc = message + ecc\n # Convert char to a int (because we use a bytearray)\n if isinstance(erasures_char, _str): erasures_char = ord(erasures_char)\n # Find the positions of the erased characters\n erasures_pos = [i for i in xrange(len(mesecc)) if mesecc[i] == erasures_char]\n # Failing case: no erasures could be found and we want to only correct erasures, then we return the message as-is\n if only_erasures and not erasures_pos: return message, ecc\n\n # Pad with null bytes if necessary\n message, pad = self.pad(message, k=k)\n ecc, _ = self.rpad(ecc, k=k) # fill ecc with null bytes if too small (maybe the field delimiters were misdetected and this truncated the ecc? But we maybe still can correct if the truncation is less than the resilience rate)\n\n # If the message was left padded, then we need to update the positions of the erasures\n if erasures_pos and pad:\n len_pad = len(pad)\n erasures_pos = [x+len_pad for x in erasures_pos]\n\n # Decoding\n if self.algo == 1:\n msg_repaired, ecc_repaired = self.ecc_manager.decode(message + ecc, nostrip=True, k=k, erasures_pos=erasures_pos, only_erasures=only_erasures) # Avoid automatic stripping because we are working with binary streams, thus we should manually strip padding only when we know we padded\n elif self.algo == 2:\n msg_repaired, ecc_repaired = self.ecc_manager.decode_fast(message + ecc, nostrip=True, k=k, erasures_pos=erasures_pos, only_erasures=only_erasures)\n elif self.algo == 3:\n #msg_repaired, ecc_repaired = self.ecc_manager.decode_fast(message + ecc, nostrip=True, k=k, erasures_pos=erasures_pos, only_erasures=only_erasures)\n msg_repaired, ecc_repaired = reedsolo.rs_correct_msg_nofsynd(bytearray(message + ecc), self.n-k, fcr=self.fcr, generator=self.gen_nb, erase_pos=erasures_pos, only_erasures=only_erasures)\n msg_repaired = bytearray(msg_repaired)\n ecc_repaired = bytearray(ecc_repaired)\n elif self.algo == 4:\n msg_repaired, ecc_repaired = reedsolo.rs_correct_msg(bytearray(message + ecc), self.n-k, fcr=self.fcr, generator=self.gen_nb, erase_pos=erasures_pos, only_erasures=only_erasures)\n msg_repaired = bytearray(msg_repaired)\n ecc_repaired = bytearray(ecc_repaired)\n\n if pad: # Strip the null bytes if we padded the message before decoding\n msg_repaired = msg_repaired[len(pad):len(msg_repaired)]\n return msg_repaired, ecc_repaired"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef pad(self, message, k=None):\n '''Automatically left pad with null bytes a message if too small, or leave unchanged if not necessary. This allows to keep track of padding and strip the null bytes after decoding reliably with binary data. Equivalent to shortening (shortened reed-solomon code).'''\n if not k: k = self.k\n pad = None\n if len(message) < k:\n #pad = \"\\x00\" * (k-len(message))\n pad = bytearray(k-len(message))\n message = pad + message\n return [message, pad]", "response": "Automatically left pad with null bytes a message if too small or leave unchanged if not necessary."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rpad(self, ecc, k=None):\n '''Automatically right pad with null bytes an ecc to fill for missing bytes if too small, or leave unchanged if not necessary. This can be used as a workaround for field delimiter misdetection. Equivalent to puncturing (punctured reed-solomon code).'''\n if not k: k = self.k\n pad = None\n if len(ecc) < self.n-k:\n print(\"Warning: the ecc field may have been truncated (entrymarker or field_delim misdetection?).\")\n #pad = \"\\x00\" * (self.n-k-len(ecc))\n pad = bytearray(self.n-k-len(ecc))\n ecc = ecc + pad\n return [ecc, pad]", "response": "Automatically right pad with null bytes an ecc to fill for missing bytes or leave unchanged if not necessary."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef check(self, message, ecc, k=None):\n '''Check if there's any error in a message+ecc. Can be used before decoding, in addition to hashes to detect if the message was tampered, or after decoding to check that the message was fully recovered.'''\n if not k: k = self.k\n message, _ = self.pad(message, k=k)\n ecc, _ = self.rpad(ecc, k=k)\n if self.algo == 1 or self.algo == 2:\n return self.ecc_manager.check_fast(message + ecc, k=k)\n elif self.algo == 3 or self.algo == 4:\n return reedsolo.rs_check(bytearray(message + ecc), self.n-k, fcr=self.fcr, generator=self.gen_nb)", "response": "Check if there s any error in a message + ecc. Can be used before decoding and addition to hashes to detect if the message was fully recovered."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprovides a description for each algorithm available useful to print in ecc file.", "response": "def description(self):\n '''Provide a description for each algorithm available, useful to print in ecc file'''\n if 0 < self.algo <= 3:\n return \"Reed-Solomon with polynomials in Galois field of characteristic %i (2^%i) with generator=%s, prime poly=%s and first consecutive root=%s.\" % (self.field_charac, self.c_exp, self.gen_nb, hex(self.prim), self.fcr)\n elif self.algo == 4:\n return \"Reed-Solomon with polynomials in Galois field of characteristic %i (2^%i) under US FAA ADSB UAT RS FEC standard with generator=%s, prime poly=%s and first consecutive root=%s.\" % (self.field_charac, self.c_exp, self.gen_nb, hex(self.prim), self.fcr)\n else:\n return \"No description for this ECC algorithm.\""} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef profile(fn=None, skip=0, filename=None, immediate=False, dirs=False,\n sort=None, entries=40,\n profiler=('cProfile', 'profile', 'hotshot')):\n \"\"\"Mark `fn` for profiling.\n\n If `skip` is > 0, first `skip` calls to `fn` will not be profiled.\n\n If `immediate` is False, profiling results will be printed to\n sys.stdout on program termination. Otherwise results will be printed\n after each call.\n\n If `dirs` is False only the name of the file will be printed.\n Otherwise the full path is used.\n\n `sort` can be a list of sort keys (defaulting to ['cumulative',\n 'time', 'calls']). The following ones are recognized::\n\n 'calls' -- call count\n 'cumulative' -- cumulative time\n 'file' -- file name\n 'line' -- line number\n 'module' -- file name\n 'name' -- function name\n 'nfl' -- name/file/line\n 'pcalls' -- call count\n 'stdname' -- standard name\n 'time' -- internal time\n\n `entries` limits the output to the first N entries.\n\n `profiler` can be used to select the preferred profiler, or specify a\n sequence of them, in order of preference. The default is ('cProfile'.\n 'profile', 'hotshot').\n\n If `filename` is specified, the profile stats will be stored in the\n named file. You can load them pstats.Stats(filename).\n\n Usage::\n\n def fn(...):\n ...\n fn = profile(fn, skip=1)\n\n If you are using Python 2.4, you should be able to use the decorator\n syntax::\n\n @profile(skip=3)\n def fn(...):\n ...\n\n or just ::\n\n @profile\n def fn(...):\n ...\n\n \"\"\"\n if fn is None: # @profile() syntax -- we are a decorator maker\n def decorator(fn):\n return profile(fn, skip=skip, filename=filename,\n immediate=immediate, dirs=dirs,\n sort=sort, entries=entries,\n profiler=profiler)\n return decorator\n # @profile syntax -- we are a decorator.\n if isinstance(profiler, str):\n profiler = [profiler]\n for p in profiler:\n if p in AVAILABLE_PROFILERS:\n profiler_class = AVAILABLE_PROFILERS[p]\n break\n else:\n raise ValueError('only these profilers are available: %s'\n % ', '.join(AVAILABLE_PROFILERS))\n fp = profiler_class(fn, skip=skip, filename=filename,\n immediate=immediate, dirs=dirs,\n sort=sort, entries=entries)\n # fp = HotShotFuncProfile(fn, skip=skip, filename=filename, ...)\n # or HotShotFuncProfile\n # We cannot return fp or fp.__call__ directly as that would break method\n # definitions, instead we need to return a plain function.\n def new_fn(*args, **kw):\n return fp(*args, **kw)\n new_fn.__doc__ = fn.__doc__\n new_fn.__name__ = fn.__name__\n new_fn.__dict__ = fn.__dict__\n new_fn.__module__ = fn.__module__\n return new_fn", "response": "Profile a file or file with the specified number of entries."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef coverage(fn):\n fp = TraceFuncCoverage(fn) # or HotShotFuncCoverage\n # We cannot return fp or fp.__call__ directly as that would break method\n # definitions, instead we need to return a plain function.\n def new_fn(*args, **kw):\n return fp(*args, **kw)\n new_fn.__doc__ = fn.__doc__\n new_fn.__name__ = fn.__name__\n new_fn.__dict__ = fn.__dict__\n new_fn.__module__ = fn.__module__\n return new_fn", "response": "Mark a function as line coverage analysis."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmarks fn for line coverage analysis.", "response": "def coverage_with_hotshot(fn):\n \"\"\"Mark `fn` for line coverage analysis.\n\n Uses the 'hotshot' module for fast coverage analysis.\n\n BUG: Produces inaccurate results.\n\n See the docstring of `coverage` for usage examples.\n \"\"\"\n fp = HotShotFuncCoverage(fn)\n # We cannot return fp or fp.__call__ directly as that would break method\n # definitions, instead we need to return a plain function.\n def new_fn(*args, **kw):\n return fp(*args, **kw)\n new_fn.__doc__ = fn.__doc__\n new_fn.__name__ = fn.__name__\n new_fn.__dict__ = fn.__dict__\n new_fn.__module__ = fn.__module__\n return new_fn"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef timecall(fn=None, immediate=True, timer=time.time):\n if fn is None: # @timecall() syntax -- we are a decorator maker\n def decorator(fn):\n return timecall(fn, immediate=immediate, timer=timer)\n return decorator\n # @timecall syntax -- we are a decorator.\n fp = FuncTimer(fn, immediate=immediate, timer=timer)\n # We cannot return fp or fp.__call__ directly as that would break method\n # definitions, instead we need to return a plain function.\n def new_fn(*args, **kw):\n return fp(*args, **kw)\n new_fn.__doc__ = fn.__doc__\n new_fn.__name__ = fn.__name__\n new_fn.__dict__ = fn.__dict__\n new_fn.__module__ = fn.__module__\n return new_fn", "response": "Wrap a function to print its execution time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef print_stats(self):\n funcname = self.fn.__name__\n filename = self.fn.__code__.co_filename\n lineno = self.fn.__code__.co_firstlineno\n print(\"\")\n print(\"*** PROFILER RESULTS ***\")\n print(\"%s (%s:%s)\" % (funcname, filename, lineno))\n if self.skipped:\n skipped = \"(%d calls not profiled)\" % self.skipped\n else:\n skipped = \"\"\n print(\"function called %d times%s\" % (self.ncalls, skipped))\n print(\"\")\n stats = self.stats\n if self.filename:\n stats.dump_stats(self.filename)\n if not self.dirs:\n stats.strip_dirs()\n stats.sort_stats(*self.sort)\n stats.print_stats(self.entries)", "response": "Print profile information to sys. stdout."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreset accumulated profiler statistics.", "response": "def reset_stats(self):\n \"\"\"Reset accumulated profiler statistics.\"\"\"\n # Note: not using self.Profile, since pstats.Stats() fails then\n self.stats = pstats.Stats(Profile())\n self.ncalls = 0\n self.skipped = 0"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef atexit(self):\n funcname = self.fn.__name__\n filename = self.fn.__code__.co_filename\n lineno = self.fn.__code__.co_firstlineno\n print(\"\")\n print(\"*** COVERAGE RESULTS ***\")\n print(\"%s (%s:%s)\" % (funcname, filename, lineno))\n print(\"function called %d times\" % self.ncalls)\n print(\"\")\n fs = FuncSource(self.fn)\n for (filename, lineno), count in self.tracer.counts.items():\n if filename != fs.filename:\n continue\n fs.mark(lineno, count)\n print(fs)\n never_executed = fs.count_never_executed()\n if never_executed:\n print(\"%d lines were not executed.\" % never_executed)", "response": "Stop profiling and print profile information to sys. stderr."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find_source_lines(self):\n strs = trace.find_strings(self.filename)\n lines = trace.find_lines_from_code(self.fn.__code__, strs)\n self.firstcodelineno = sys.maxint\n for lineno in lines:\n self.firstcodelineno = min(self.firstcodelineno, lineno)\n self.sourcelines.setdefault(lineno, 0)\n if self.firstcodelineno == sys.maxint:\n self.firstcodelineno = self.firstlineno", "response": "Find all executable source lines in fn as executed 0 times."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef mark(self, lineno, count=1):\n self.sourcelines[lineno] = self.sourcelines.get(lineno, 0) + count", "response": "Mark a given source line as executed count times."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncount statements that were never executed.", "response": "def count_never_executed(self):\n \"\"\"Count statements that were never executed.\"\"\"\n lineno = self.firstlineno\n counter = 0\n for line in self.source:\n if self.sourcelines.get(lineno) == 0:\n if not self.blank_rx.match(line):\n counter += 1\n lineno += 1\n return counter"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd the element val to the list.", "response": "def add(self, val):\n \"\"\"Add the element *val* to the list.\"\"\"\n _maxes, _lists = self._maxes, self._lists\n\n if _maxes:\n pos = bisect_right(_maxes, val)\n\n if pos == len(_maxes):\n pos -= 1\n _maxes[pos] = val\n _lists[pos].append(val)\n else:\n insort(_lists[pos], val)\n\n self._expand(pos)\n else:\n _maxes.append(val)\n _lists.append([val])\n\n self._len += 1"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving first occurrence of val. Raises ValueError if val is not present.", "response": "def remove(self, val):\n \"\"\"\n Remove first occurrence of *val*.\n\n Raises ValueError if *val* is not present.\n \"\"\"\n _maxes = self._maxes\n\n if not _maxes:\n raise ValueError('{0} not in list'.format(repr(val)))\n\n pos = bisect_left(_maxes, val)\n\n if pos == len(_maxes):\n raise ValueError('{0} not in list'.format(repr(val)))\n\n _lists = self._lists\n idx = bisect_left(_lists[pos], val)\n if _lists[pos][idx] == val:\n self._delete(pos, idx)\n else:\n raise ValueError('{0} not in list'.format(repr(val)))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _delete(self, pos, idx):\n _maxes, _lists, _index = self._maxes, self._lists, self._index\n\n lists_pos = _lists[pos]\n\n del lists_pos[idx]\n self._len -= 1\n\n len_lists_pos = len(lists_pos)\n\n if len_lists_pos > self._half:\n\n _maxes[pos] = lists_pos[-1]\n\n if _index:\n child = self._offset + pos\n while child > 0:\n _index[child] -= 1\n child = (child - 1) >> 1\n _index[0] -= 1\n\n elif len(_lists) > 1:\n\n if not pos:\n pos += 1\n\n prev = pos - 1\n _lists[prev].extend(_lists[pos])\n _maxes[prev] = _lists[prev][-1]\n\n del _maxes[pos]\n del _lists[pos]\n del _index[:]\n\n self._expand(prev)\n\n elif len_lists_pos:\n\n _maxes[pos] = lists_pos[-1]\n\n else:\n\n del _maxes[pos]\n del _lists[pos]\n del _index[:]", "response": "Delete the item at the given position."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nextending the list by appending all elements from the given list. Raises a ValueError if the sort order is violated.", "response": "def extend(self, values):\n \"\"\"\n Extend the list by appending all elements from the *values*. Raises a\n ValueError if the sort order would be violated.\n \"\"\"\n _maxes, _lists, _load = self._maxes, self._lists, self._load\n\n if not isinstance(values, list):\n values = list(values)\n\n if any(values[pos - 1] > values[pos]\n for pos in range(1, len(values))):\n raise ValueError('given sequence not in sort order')\n\n offset = 0\n\n if _maxes:\n if values[0] < _lists[-1][-1]:\n msg = '{0} not in sort order at index {1}'.format(repr(values[0]), self._len)\n raise ValueError(msg)\n\n if len(_lists[-1]) < self._half:\n _lists[-1].extend(values[:_load])\n _maxes[-1] = _lists[-1][-1]\n offset = _load\n\n len_lists = len(_lists)\n\n for idx in range(offset, len(values), _load):\n _lists.append(values[idx:(idx + _load)])\n _maxes.append(_lists[-1][-1])\n\n _index = self._index\n\n if len_lists == len(_lists):\n len_index = len(_index)\n if len_index > 0:\n len_values = len(values)\n child = len_index - 1\n while child:\n _index[child] += len_values\n child = (child - 1) >> 1\n _index[0] += len_values\n else:\n del _index[:]\n\n self._len += len(values)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninserting the element val into the list at idx. Raises a ValueError if the element val is not in the sort order.", "response": "def insert(self, idx, val):\n \"\"\"\n Insert the element *val* into the list at *idx*. Raises a ValueError if\n the *val* at *idx* would violate the sort order.\n \"\"\"\n _maxes, _lists, _len = self._maxes, self._lists, self._len\n\n if idx < 0:\n idx += _len\n if idx < 0:\n idx = 0\n if idx > _len:\n idx = _len\n\n if not _maxes:\n # The idx must be zero by the inequalities above.\n _maxes.append(val)\n _lists.append([val])\n self._len = 1\n return\n\n if not idx:\n if val > _lists[0][0]:\n msg = '{0} not in sort order at index {1}'.format(repr(val), 0)\n raise ValueError(msg)\n else:\n _lists[0].insert(0, val)\n self._expand(0)\n self._len += 1\n return\n\n if idx == _len:\n pos = len(_lists) - 1\n if _lists[pos][-1] > val:\n msg = '{0} not in sort order at index {1}'.format(repr(val), _len)\n raise ValueError(msg)\n else:\n _lists[pos].append(val)\n _maxes[pos] = _lists[pos][-1]\n self._expand(pos)\n self._len += 1\n return\n\n pos, idx = self._pos(idx)\n idx_before = idx - 1\n if idx_before < 0:\n pos_before = pos - 1\n idx_before = len(_lists[pos_before]) - 1\n else:\n pos_before = pos\n\n before = _lists[pos_before][idx_before]\n if before <= val <= _lists[pos][idx]:\n _lists[pos].insert(idx, val)\n self._expand(pos)\n self._len += 1\n else:\n msg = '{0} not in sort order at index {1}'.format(repr(val), idx)\n raise ValueError(msg)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the list by adding all elements from iterable.", "response": "def update(self, iterable):\n \"\"\"Update the list by adding all elements from *iterable*.\"\"\"\n _maxes, _lists, _keys = self._maxes, self._lists, self._keys\n values = sorted(iterable, key=self._key)\n\n if _maxes:\n if len(values) * 4 >= self._len:\n values.extend(chain.from_iterable(_lists))\n values.sort(key=self._key)\n self._clear()\n else:\n _add = self.add\n for val in values:\n _add(val)\n return\n\n _load, _index = self._load, self._index\n _lists.extend(values[pos:(pos + _load)]\n for pos in range(0, len(values), _load))\n _keys.extend(list(map(self._key, _list)) for _list in _lists)\n _maxes.extend(sublist[-1] for sublist in _keys)\n self._len = len(values)\n del _index[:]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the username of the current object.", "response": "def set_username_ibm(self, username_ibm):\n \"\"\"\n Parameters\n ----------\n username_ibm : str\n\n Raises\n ------\n Exception\n If mode is not `ibm`\n \"\"\"\n if self.get_mode() == \"ibm\":\n self.__username_ibm = username_ibm\n else:\n raise Exception(\n \"Mode is {}, whereas it must be `ibm`\".format(\n self.get_moder()))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the password of the current locale.", "response": "def set_password_ibm(self, password_ibm):\n \"\"\"\n Parameters\n ----------\n password_ibm : str\n\n Raises\n ------\n Exception\n If mode is not `ibm`\n \"\"\"\n if self.get_mode() == \"ibm\":\n self.__password_ibm = password_ibm\n else:\n raise Exception(\n \"Mode is {}, whereas it must be `ibm`\".format(self.get_mode()))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of all audio files in the source directory.", "response": "def _list_audio_files(self, sub_dir=\"\"):\n \"\"\"\n Parameters\n ----------\n sub_dir : one of `needed_directories`, optional\n Default is \"\", which means it'll look through all of subdirs.\n\n Returns\n -------\n audio_files : [str]\n A list whose elements are basenames of the present audiofiles whose\n formats are `wav`\n \"\"\"\n audio_files = list()\n for possibly_audio_file in os.listdir(\"{}/{}\".format(self.src_dir,\n sub_dir)):\n file_format = ''.join(possibly_audio_file.split('.')[-1])\n if file_format.lower() == \"wav\":\n audio_files.append(possibly_audio_file)\n return audio_files"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_audio_channels(self, audio_abs_path):\n channel_num = int(\n subprocess.check_output(\n (\"\"\"sox --i {} | grep \"{}\" | awk -F \" : \" '{{print $2}}'\"\"\"\n ).format(audio_abs_path, \"Channels\"),\n shell=True, universal_newlines=True).rstrip())\n return channel_num", "response": "Get the number of audio channels from the audio file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_audio_sample_rate(self, audio_abs_path):\n sample_rate = int(\n subprocess.check_output(\n (\"\"\"sox --i {} | grep \"{}\" | awk -F \" : \" '{{print $2}}'\"\"\"\n ).format(audio_abs_path, \"Sample Rate\"),\n shell=True, universal_newlines=True).rstrip())\n return sample_rate", "response": "Get the audio sample rate from the audio file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the audio sample bit from the cache file.", "response": "def _get_audio_sample_bit(self, audio_abs_path):\n \"\"\"\n Parameters\n ----------\n audio_abs_path : str\n\n Returns\n -------\n sample_bit : int\n \"\"\"\n sample_bit = int(\n subprocess.check_output(\n (\"\"\"sox --i {} | grep \"{}\" | awk -F \" : \" '{{print $2}}' | \"\"\"\n \"\"\"grep -oh \"^[^-]*\" \"\"\").format(audio_abs_path, \"Precision\"),\n shell=True, universal_newlines=True).rstrip())\n return sample_bit"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the total number of seconds until the audio file has been downloaded.", "response": "def _get_audio_duration_seconds(self, audio_abs_path):\n \"\"\"\n Parameters\n ----------\n audio_abs_path : str\n\n Returns\n -------\n total_seconds : int\n \"\"\"\n HHMMSS_duration = subprocess.check_output(\n (\"\"\"sox --i {} | grep \"{}\" | awk -F \" : \" '{{print $2}}' | \"\"\"\n \"\"\"grep -oh \"^[^=]*\" \"\"\").format(\n audio_abs_path, \"Duration\"),\n shell=True, universal_newlines=True).rstrip()\n total_seconds = sum(\n [float(x) * 60 ** (2 - i)\n for i, x in enumerate(HHMMSS_duration.split(\":\"))])\n return total_seconds"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the audio bit rate for the given audio file.", "response": "def _get_audio_bit_rate(self, audio_abs_path):\n \"\"\"\n Parameters\n -----------\n audio_abs_path : str\n\n Returns\n -------\n bit_rate : int\n \"\"\"\n bit_Rate_formatted = subprocess.check_output(\n \"\"\"sox --i {} | grep \"{}\" | awk -F \" : \" '{{print $2}}'\"\"\".format(\n audio_abs_path, \"Bit Rate\"),\n shell=True, universal_newlines=True).rstrip()\n bit_rate = (lambda x:\n int(x[:-1]) * 10 ** 3 if x[-1].lower() == \"k\" else\n int(x[:-1]) * 10 ** 6 if x[-1].lower() == \"m\" else\n int(x[:-1]) * 10 ** 9 if x[-1].lower() == \"g\" else\n int(x))(bit_Rate_formatted)\n return bit_rate"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _seconds_to_HHMMSS(seconds):\n less_than_second = seconds - floor(seconds)\n minutes, seconds = divmod(floor(seconds), 60)\n hours, minutes = divmod(minutes, 60)\n return \"{}H{}M{}S.{}\".format(hours, minutes, seconds, less_than_second)", "response": "Returns a string which is the hour minute second representation of the intput seconds."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nextracting the audio segments from the audio file.", "response": "def _audio_segment_extractor(self, audio_abs_path, segment_abs_path,\n starting_second, duration):\n \"\"\"\n Parameters\n -----------\n audio_abs_path : str\n segment_abs_path : str\n starting_second : int\n duration : int\n \"\"\"\n\n subprocess.Popen([\"sox\", str(audio_abs_path), str(segment_abs_path),\n \"trim\", str(starting_second), str(duration)],\n universal_newlines=True).communicate()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _split_audio_by_duration(self, audio_abs_path,\n results_abs_path, duration_seconds):\n \"\"\"\n Calculates the length of each segment and passes it to\n self._audio_segment_extractor\n\n Parameters\n ----------\n audio_abs_path : str\n results_abs_path : str\n A place for adding digits needs to be added prior the the format\n decleration i.e. name%03.wav. Here, we've added `*` at staging\n step, which we'll replace.\n duration_seconds : int\n \"\"\"\n total_seconds = self._get_audio_duration_seconds(audio_abs_path)\n current_segment = 0\n while current_segment <= total_seconds // duration_seconds + 1:\n if current_segment + duration_seconds > total_seconds:\n ending_second = total_seconds\n else:\n ending_second = current_segment + duration_seconds\n self._audio_segment_extractor(\n audio_abs_path,\n results_abs_path.replace(\"*\", \"{:03d}\".format(\n current_segment)),\n starting_second=current_segment, duration=(ending_second -\n current_segment))\n current_segment += 1", "response": "Splits the audio file into individual segments."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsplitting the audio file into individual items and splits them into individual items.", "response": "def _split_audio_by_size(self, audio_abs_path, results_abs_path,\n chunk_size):\n \"\"\"\n Calculates the duration of the name.wav in order for all splits have\n the size of chunk_size except possibly the last split (which will be\n smaller) and then passes the duration to `split_audio_by_duration`\n\n Parameters\n ----------\n audio_abs_path : str\n results_abs_path : str\n A place for adding digits needs to be added prior the the format\n decleration i.e. name%03.wav\n chunk_size : int\n Should be in bytes\n \"\"\"\n sample_rate = self._get_audio_sample_rate(audio_abs_path)\n sample_bit = self._get_audio_sample_bit(audio_abs_path)\n channel_num = self._get_audio_channels(audio_abs_path)\n duration = 8 * chunk_size / reduce(lambda x, y: int(x) * int(y),\n [sample_rate, sample_bit,\n channel_num])\n self._split_audio_by_duration(audio_abs_path, results_abs_path,\n duration)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmoves the audio file if the format is `wav` to `filtered` directory. Parameters ---------- basename : str A basename of `/home/random-guy/some-audio-file.wav` is `some-audio-file.wav`", "response": "def _filtering_step(self, basename):\n \"\"\"\n Moves the audio file if the format is `wav` to `filtered` directory.\n\n Parameters\n ----------\n basename : str\n A basename of `/home/random-guy/some-audio-file.wav` is\n `some-audio-file.wav`\n \"\"\"\n name = ''.join(basename.split('.')[:-1])\n # May cause problems if wav is not less than 9 channels.\n if basename.split('.')[-1] == \"wav\":\n if self.get_verbosity():\n print(\"Found wave! Copying to {}/filtered/{}\".format(\n self.src_dir, basename))\n subprocess.Popen([\"cp\", \"{}/{}.wav\".format(self.src_dir, name),\n \"{}/filtered/{}.wav\".format(self.src_dir, name)],\n universal_newlines=True).communicate()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck the size of audio file, splits it if it's needed to manage api limit and then moves to `staged` directory while appending `*` to the end of the filename for self.split_audio_by_duration to replace it by a number. Parameters ---------- basename : str A basename of `/home/random-guy/some-audio-file.wav` is `some-audio-file.wav`", "response": "def _staging_step(self, basename):\n \"\"\"\n Checks the size of audio file, splits it if it's needed to manage api\n limit and then moves to `staged` directory while appending `*` to\n the end of the filename for self.split_audio_by_duration to replace\n it by a number.\n\n Parameters\n ----------\n basename : str\n A basename of `/home/random-guy/some-audio-file.wav` is\n `some-audio-file.wav`\n \"\"\"\n name = ''.join(basename.split('.')[:-1])\n\n if self.get_mode() == \"ibm\":\n # Checks the file size. It's better to use 95% of the allocated\n # size per file since the upper limit is not always respected.\n total_size = os.path.getsize(\"{}/filtered/{}.wav\".format(\n self.src_dir, name))\n if total_size >= self.ibm_api_limit_bytes:\n if self.get_verbosity():\n print((\"{}'s size over API limit ({}). Splitting\").format(\n name, self.ibm_api_limit_bytes))\n self._split_audio_by_size(\n \"{}/filtered/{}.wav\".format(self.src_dir, name),\n \"{}/staging/{}*.wav\".format(self.src_dir, name),\n self.ibm_api_limit_bytes * 95 / 100)\n else:\n if self.get_verbosity():\n print(\"{}'s size is fine. Moving to staging dir'\".format(\n name))\n subprocess.Popen((\n \"mv {}/filtered/{}.wav {}/staging/{}000.wav\").format(\n self.src_dir, name, self.src_dir, name),\n shell=True,\n universal_newlines=True).communicate()\n\n elif self.get_mode() == \"cmu\":\n if self.get_verbosity():\n print(\"Converting {} to a readable wav\".format(basename))\n ffmpeg = os.path.basename(find_executable(\"ffmpeg\") or\n find_executable(\"avconv\"))\n if ffmpeg is None:\n raise Exception((\"Either ffmpeg or avconv is needed. \"\n \"Neither is installed or accessible\"))\n try:\n # ffmpeg log levels:\n # https://ffmpeg.org/ffmpeg.html#Generic-options\n ffmpeg_log_level = \"8\" # fatal errors.\n if self.get_verbosity():\n ffmpeg_log_level = \"32\" # info `default for ffmpeg`\n subprocess.check_call([\n str(ffmpeg), \"-y\", \"-i\", \"{}/filtered/{}.wav\".format(\n self.src_dir, str(name)), \"-acodec\", \"pcm_s16le\",\n \"-ac\", \"1\", \"-ar\", \"16000\", \"{}/staging/{}000.wav\".format(\n self.src_dir, name),\n \"-v\", ffmpeg_log_level], universal_newlines=True)\n except subprocess.CalledProcessError as e:\n print(e)\n if os.path.exists(\"{}/staging/{}000.wav\".format(\n self.src_dir, name)):\n if self.get_verbosity():\n print((\"{}/filtered/{} was converted to \"\n \"{}/staging/{}000.wav Now removing the copy of \"\n \"{} in filtered sub directory\").format(\n self.src_dir, basename,\n self.src_dir, name, basename))\n subprocess.Popen([\n \"rm\", \"{}/filtered/{}\".format(self.src_dir, basename)],\n universal_newlines=True).communicate()\n else:\n raise Exception(\"Something went wrong with ffmpeg conversion!\")"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprepares and stages the audio files.", "response": "def _prepare_audio(self, basename, replace_already_indexed=False):\n \"\"\"\n Prepares and stages the audio file to be indexed.\n\n Parameters\n ----------\n basename : str, None\n A basename of `/home/random-guy/some-audio-file.wav` is\n `some-audio-file.wav`\n If basename is `None`, it'll prepare all the audio files.\n \"\"\"\n if basename is not None:\n if basename in self.get_timestamps():\n if self.get_verbosity():\n print(\"File specified was already indexed. Reindexing...\")\n del self.__timestamps[basename]\n self._filtering_step(basename)\n self._staging_step(basename)\n else:\n for audio_basename in self._list_audio_files():\n if audio_basename in self.__timestamps:\n if replace_already_indexed:\n if self.get_verbosity():\n print(\"Already indexed {}. Reindexing...\".format(\n audio_basename))\n del self.__timestamps[audio_basename]\n else:\n if self.get_verbosity():\n print(\"Already indexed {}. Skipping...\".format(\n audio_basename))\n continue\n self._filtering_step(audio_basename)\n self._staging_step(audio_basename)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nindex audio with pocketsphinx. Beware that the output would not be sufficiently accurate. Use this only if you don't want to upload your files to IBM. Parameters ----------- basename : str, optional A specific basename to be indexed and is placed in src_dir E.g. `audio.wav`. If `None` is selected, all the valid audio files would be indexed. Default is `None`. Raises ------ OSError If the output of pocketsphinx command results in an error.", "response": "def _index_audio_cmu(self, basename=None, replace_already_indexed=False):\n \"\"\"\n Indexes audio with pocketsphinx. Beware that the output would not be\n sufficiently accurate. Use this only if you don't want to upload your\n files to IBM.\n\n Parameters\n -----------\n basename : str, optional\n A specific basename to be indexed and is placed in src_dir\n E.g. `audio.wav`.\n\n If `None` is selected, all the valid audio files would be indexed.\n Default is `None`.\n\n Raises\n ------\n OSError\n If the output of pocketsphinx command results in an error.\n \"\"\"\n self._prepare_audio(basename=basename,\n replace_already_indexed=replace_already_indexed)\n\n for staging_audio_basename in self._list_audio_files(\n sub_dir=\"staging\"):\n original_audio_name = ''.join(\n staging_audio_basename.split('.')[:-1])[:-3]\n pocketsphinx_command = ''.join([\n \"pocketsphinx_continuous\", \"-infile\",\n str(\"{}/staging/{}\".format(\n self.src_dir, staging_audio_basename)),\n \"-time\", \"yes\", \"-logfn\", \"/dev/null\"])\n try:\n if self.get_verbosity():\n print(\"Now indexing {}\".format(staging_audio_basename))\n output = subprocess.check_output([\n \"pocketsphinx_continuous\", \"-infile\",\n str(\"{}/staging/{}\".format(\n self.src_dir, staging_audio_basename)),\n \"-time\", \"yes\", \"-logfn\", \"/dev/null\"\n ], universal_newlines=True).split('\\n')\n str_timestamps_with_sil_conf = list(map(\n lambda x: x.split(\" \"), filter(None, output[1:])))\n # Timestamps are putted in a list of a single element. To match\n # Watson's output.\n self.__timestamps_unregulated[\n original_audio_name + \".wav\"] = [(\n self._timestamp_extractor_cmu(\n staging_audio_basename,\n str_timestamps_with_sil_conf))]\n if self.get_verbosity():\n print(\"Done indexing {}\".format(staging_audio_basename))\n except OSError as e:\n if self.get_verbosity():\n print(e, \"The command was: {}\".format(\n pocketsphinx_command))\n self.__errors[(time(), staging_audio_basename)] = e\n self._timestamp_regulator()\n\n if self.get_verbosity():\n print(\"Finished indexing procedure\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _timestamp_extractor_cmu(self, staging_audio_basename,\n str_timestamps_with_sil_conf):\n \"\"\"\n Parameters\n ----------\n str_timestamps_with_sil_conf : [[str, str, str, str]]\n Of the form [[word, starting_sec, ending_sec, confidence]]\n\n Returns\n -------\n timestamps : [[str, float, float]]\n \"\"\"\n filter_untimed = filter(lambda x: len(x) == 4,\n str_timestamps_with_sil_conf)\n if filter_untimed != str_timestamps_with_sil_conf:\n self.__errors[\n (time(), staging_audio_basename)\n ] = str_timestamps_with_sil_conf\n str_timestamps = [\n str_timestamp[:-1]\n for str_timestamp in filter_untimed\n if not any([letter in {\"<\", \">\", \"/\"}\n for letter in ''.join(str_timestamp)])]\n timestamps = list([\n _WordBlock(\n word=re.findall(\"^[^\\(]+\", x[0])[0],\n start=round(float(x[1]), 2),\n end=round(float(x[2]), 2)\n ) for x in str_timestamps])\n return timestamps", "response": "Extract the CMU timestamps from the given string."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _index_audio_ibm(self, basename=None, replace_already_indexed=False,\n continuous=True, model=\"en-US_BroadbandModel\",\n word_confidence=True, word_alternatives_threshold=0.9,\n profanity_filter_for_US_results=False):\n \"\"\"\n Implements a search-suitable interface for Watson speech API.\n\n Some explaination of the parameters here have been taken from [1]_\n\n Parameters\n ----------\n basename : str, optional\n A specific basename to be indexed and is placed in src_dir\n e.g `audio.wav`.\n\n If `None` is selected, all the valid audio files would be indexed.\n Default is `None`.\n\n replace_already_indexed : bool\n `True`, To reindex some audio file that's already in the\n timestamps.\n\n Default is `False`.\n\n continuous : bool\n Indicates whether multiple final results that represent consecutive\n phrases separated by long pauses are returned.\n If true, such phrases are returned; if false (the default),\n recognition ends after the first end-of-speech (EOS) incident is\n detected.\n\n Default is `True`.\n model : {\n 'ar-AR_BroadbandModel',\n 'en-UK_BroadbandModel'\n 'en-UK_NarrowbandModel',\n 'en-US_BroadbandModel', (the default)\n 'en-US_NarrowbandModel',\n 'es-ES_BroadbandModel',\n 'es-ES_NarrowbandModel',\n 'fr-FR_BroadbandModel',\n 'ja-JP_BroadbandModel',\n 'ja-JP_NarrowbandModel',\n 'pt-BR_BroadbandModel',\n 'pt-BR_NarrowbandModel',\n 'zh-CN_BroadbandModel',\n 'zh-CN_NarrowbandModel'\n }\n The identifier of the model to be used for the recognition\n\n Default is 'en-US_BroadbandModel'\n word_confidence : bool\n Indicates whether a confidence measure in the range of 0 to 1 is\n returned for each word.\n\n The default is True. (It's False in the original)\n word_alternatives_threshold : numeric\n A confidence value that is the lower bound for identifying a\n hypothesis as a possible word alternative (also known as\n \"Confusion Networks\"). An alternative word is considered if its\n confidence is greater than or equal to the threshold. Specify a\n probability between 0 and 1 inclusive.\n\n Default is `0.9`.\n profanity_filter_for_US_results : bool\n Indicates whether profanity filtering is performed on the\n transcript. If true, the service filters profanity from all output\n by replacing inappropriate words with a series of asterisks.\n\n If false, the service returns results with no censoring. Applies\n to US English transcription only.\n\n Default is `False`.\n\n References\n ----------\n .. [1] : https://ibm.com/watson/developercloud/speech-to-text/api/v1/\n \"\"\"\n params = {'continuous': continuous,\n 'model': model,\n 'word_alternatives_threshold': word_alternatives_threshold,\n 'word_confidence': word_confidence,\n 'timestamps': True,\n 'inactivity_timeout': str(-1),\n 'profanity_filter': profanity_filter_for_US_results}\n\n self._prepare_audio(basename=basename,\n replace_already_indexed=replace_already_indexed)\n\n for staging_audio_basename in self._list_audio_files(\n sub_dir=\"staging\"):\n original_audio_name = ''.join(\n staging_audio_basename.split('.')[:-1])[:-3]\n with open(\"{}/staging/{}\".format(\n self.src_dir, staging_audio_basename), \"rb\") as f:\n if self.get_verbosity():\n print(\"Uploading {}...\".format(staging_audio_basename))\n response = requests.post(\n url=(\"https://stream.watsonplatform.net/\"\n \"speech-to-text/api/v1/recognize\"),\n auth=(self.get_username_ibm(), self.get_password_ibm()),\n headers={'content-type': 'audio/wav'},\n data=f.read(),\n params=params)\n if self.get_verbosity():\n print(\"Indexing {}...\".format(staging_audio_basename))\n self.__timestamps_unregulated[\n original_audio_name + \".wav\"].append(\n self._timestamp_extractor_ibm(\n staging_audio_basename, json.loads(response.text)))\n if self.get_verbosity():\n print(\"Done indexing {}\".format(staging_audio_basename))\n self._timestamp_regulator()\n\n if self.get_verbosity():\n print(\"Indexing procedure finished\")", "response": "This method is used to index the audio files in src_dir_wav."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nextract the timestamps of the original audio file.", "response": "def _timestamp_extractor_ibm(self, staging_audio_basename, audio_json):\n \"\"\"\n Parameters\n ----------\n audio_json : {str: [{str: [{str: str or nuneric}]}]}\n Refer to Watson Speech API refrence [1]_\n\n Returns\n -------\n [[str, float, float]]\n A list whose members are lists. Each member list has three\n elements. First one is a word. Second is the starting second and\n the third is the ending second of that word in the original\n audio file.\n \"\"\"\n try:\n timestamps_of_sentences = [\n audio_json['results'][i]['alternatives'][0]['timestamps']\n for i in range(len(audio_json['results']))]\n return [\n _WordBlock(\n word=word_block[0],\n start=round(float(word_block[1]), 2),\n end=round(float(word_block[2]), 2)\n ) for sentence_block in timestamps_of_sentences\n for word_block in sentence_block]\n except KeyError:\n self.__errors[(time(), staging_audio_basename)] = audio_json\n if self.get_verbosity():\n print(audio_json)\n print(\"The resulting request from Watson was unintelligible.\")\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall the correct indexer function based on the mode. If mode is `ibm`, _indexer_audio_ibm is called which is an interface for Watson. Note that some of the explaination of _indexer_audio_ibm's arguments is from [1]_ If mode is `cmu`, _indexer_audio_cmu is called which is an interface for PocketSphinx Beware that the output would not be sufficiently accurate. Use this only if you don't want to upload your files to IBM. Parameters ---------- mode : {\"ibm\", \"cmu\"} basename : str, optional A specific basename to be indexed and is placed in src_dir e.g `audio.wav`. If `None` is selected, all the valid audio files would be indexed. Default is `None`. replace_already_indexed : bool `True`, To reindex some audio file that's already in the timestamps. Default is `False`. continuous : bool Valid Only if mode is `ibm` Indicates whether multiple final results that represent consecutive phrases separated by long pauses are returned. If true, such phrases are returned; if false (the default), recognition ends after the first end-of-speech (EOS) incident is detected. Default is `True`. model : { 'ar-AR_BroadbandModel', 'en-UK_BroadbandModel' 'en-UK_NarrowbandModel', 'en-US_BroadbandModel', (the default) 'en-US_NarrowbandModel', 'es-ES_BroadbandModel', 'es-ES_NarrowbandModel', 'fr-FR_BroadbandModel', 'ja-JP_BroadbandModel', 'ja-JP_NarrowbandModel', 'pt-BR_BroadbandModel', 'pt-BR_NarrowbandModel', 'zh-CN_BroadbandModel', 'zh-CN_NarrowbandModel' } Valid Only if mode is `ibm` The identifier of the model to be used for the recognition Default is 'en-US_BroadbandModel' word_confidence : bool Valid Only if mode is `ibm` Indicates whether a confidence measure in the range of 0 to 1 is returned for each word. The default is True. (It's False in the original) word_alternatives_threshold : numeric Valid Only if mode is `ibm` A confidence value that is the lower bound for identifying a hypothesis as a possible word alternative (also known as \"Confusion Networks\"). An alternative word is considered if its confidence is greater than or equal to the threshold. Specify a probability between 0 and 1 inclusive. Default is `0.9`. profanity_filter_for_US_results : bool Valid Only if mode is `ibm` Indicates whether profanity filtering is performed on the transcript. If true, the service filters profanity from all output by replacing inappropriate words with a series of asterisks. If false, the service returns results with no censoring. Applies to US English transcription only. Default is `False`. Raises ------ OSError Valid only if mode is `cmu`. If the output of pocketsphinx command results in an error. References ---------- .. [1] : https://ibm.com/watson/developercloud/speech-to-text/api/v1/ Else if mode is `cmu`, then _index_audio_cmu would be called:", "response": "def index_audio(self, *args, **kwargs):\n \"\"\"\n Calls the correct indexer function based on the mode.\n\n If mode is `ibm`, _indexer_audio_ibm is called which is an interface\n for Watson. Note that some of the explaination of _indexer_audio_ibm's\n arguments is from [1]_\n\n If mode is `cmu`, _indexer_audio_cmu is called which is an interface\n for PocketSphinx Beware that the output would not be sufficiently\n accurate. Use this only if you don't want to upload your files to IBM.\n\n Parameters\n ----------\n mode : {\"ibm\", \"cmu\"}\n\n basename : str, optional\n\n A specific basename to be indexed and is placed in src_dir\n e.g `audio.wav`.\n\n If `None` is selected, all the valid audio files would be indexed.\n Default is `None`.\n\n replace_already_indexed : bool\n\n `True`, To reindex some audio file that's already in the\n timestamps.\n\n Default is `False`.\n\n continuous : bool\n\n Valid Only if mode is `ibm`\n\n Indicates whether multiple final results that represent consecutive\n phrases separated by long pauses are returned.\n If true, such phrases are returned; if false (the default),\n recognition ends after the first end-of-speech (EOS) incident is\n detected.\n\n Default is `True`.\n\n model : {\n 'ar-AR_BroadbandModel',\n 'en-UK_BroadbandModel'\n 'en-UK_NarrowbandModel',\n 'en-US_BroadbandModel', (the default)\n 'en-US_NarrowbandModel',\n 'es-ES_BroadbandModel',\n 'es-ES_NarrowbandModel',\n 'fr-FR_BroadbandModel',\n 'ja-JP_BroadbandModel',\n 'ja-JP_NarrowbandModel',\n 'pt-BR_BroadbandModel',\n 'pt-BR_NarrowbandModel',\n 'zh-CN_BroadbandModel',\n 'zh-CN_NarrowbandModel'\n }\n\n Valid Only if mode is `ibm`\n\n The identifier of the model to be used for the recognition\n\n Default is 'en-US_BroadbandModel'\n\n word_confidence : bool\n\n Valid Only if mode is `ibm`\n\n Indicates whether a confidence measure in the range of 0 to 1 is\n returned for each word.\n\n The default is True. (It's False in the original)\n\n word_alternatives_threshold : numeric\n\n Valid Only if mode is `ibm`\n\n A confidence value that is the lower bound for identifying a\n hypothesis as a possible word alternative (also known as\n \"Confusion Networks\"). An alternative word is considered if its\n confidence is greater than or equal to the threshold. Specify a\n probability between 0 and 1 inclusive.\n\n Default is `0.9`.\n\n profanity_filter_for_US_results : bool\n\n Valid Only if mode is `ibm`\n\n Indicates whether profanity filtering is performed on the\n transcript. If true, the service filters profanity from all output\n by replacing inappropriate words with a series of asterisks.\n\n If false, the service returns results with no censoring. Applies\n to US English transcription only.\n\n Default is `False`.\n\n Raises\n ------\n OSError\n\n Valid only if mode is `cmu`.\n\n If the output of pocketsphinx command results in an error.\n\n References\n ----------\n .. [1] : https://ibm.com/watson/developercloud/speech-to-text/api/v1/\n\n Else if mode is `cmu`, then _index_audio_cmu would be called:\n \"\"\"\n with _Subdirectory_Managing_Decorator(\n self.src_dir, self._needed_directories):\n if self.get_mode() == \"ibm\":\n self._index_audio_ibm(*args, **kwargs)\n elif self.get_mode() == \"cmu\":\n self._index_audio_cmu(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmakes a dictionary whose keys are audio file basenames and whose values are a list of word blocks from unregulated timestamps and updates the main timestamp attribute. After all done, purges unregulated ones. In case the audio file was large enough to be splitted, it adds seconds to correct timing and in case the timestamp was manually loaded, it leaves it alone. Note that the difference between self.__timestamps and self.__timestamps_unregulated is that in the regulated version, right after the word, a list of word blocks must appear. However in the unregulated version, after a word, a list of individual splits containing word blocks would appear!", "response": "def _timestamp_regulator(self):\n \"\"\"\n Makes a dictionary whose keys are audio file basenames and whose\n values are a list of word blocks from unregulated timestamps and\n updates the main timestamp attribute. After all done, purges\n unregulated ones.\n In case the audio file was large enough to be splitted, it adds seconds\n to correct timing and in case the timestamp was manually loaded, it\n leaves it alone.\n\n Note that the difference between self.__timestamps and\n self.__timestamps_unregulated is that in the regulated version,\n right after the word, a list of word blocks must appear. However in the\n unregulated version, after a word, a list of individual splits\n containing word blocks would appear!\n \"\"\"\n unified_timestamps = _PrettyDefaultDict(list)\n staged_files = self._list_audio_files(sub_dir=\"staging\")\n for timestamp_basename in self.__timestamps_unregulated:\n if len(self.__timestamps_unregulated[timestamp_basename]) > 1:\n # File has been splitted\n timestamp_name = ''.join(timestamp_basename.split('.')[:-1])\n staged_splitted_files_of_timestamp = list(\n filter(lambda staged_file: (\n timestamp_name == staged_file[:-3] and\n all([(x in set(map(str, range(10))))\n for x in staged_file[-3:]])), staged_files))\n if len(staged_splitted_files_of_timestamp) == 0:\n self.__errors[(time(), timestamp_basename)] = {\n \"reason\": \"Missing staged file\",\n \"current_staged_files\": staged_files}\n continue\n staged_splitted_files_of_timestamp.sort()\n unified_timestamp = list()\n for staging_digits, splitted_file in enumerate(\n self.__timestamps_unregulated[timestamp_basename]):\n prev_splits_sec = 0\n if int(staging_digits) != 0:\n prev_splits_sec = self._get_audio_duration_seconds(\n \"{}/staging/{}{:03d}\".format(\n self.src_dir, timestamp_name,\n staging_digits - 1))\n for word_block in splitted_file:\n unified_timestamp.append(\n _WordBlock(\n word=word_block.word,\n start=round(word_block.start +\n prev_splits_sec, 2),\n end=round(word_block.end +\n prev_splits_sec, 2)))\n unified_timestamps[\n str(timestamp_basename)] += unified_timestamp\n else:\n unified_timestamps[\n timestamp_basename] += self.__timestamps_unregulated[\n timestamp_basename][0]\n\n self.__timestamps.update(unified_timestamps)\n self.__timestamps_unregulated = _PrettyDefaultDict(list)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nwrites the corrected timestamps to a file.", "response": "def save_indexed_audio(self, indexed_audio_file_abs_path):\n \"\"\"\n Writes the corrected timestamps to a file. Timestamps are a python\n dictionary.\n\n Parameters\n ----------\n indexed_audio_file_abs_path : str\n \"\"\"\n with open(indexed_audio_file_abs_path, \"wb\") as f:\n pickle.dump(self.get_timestamps(), f, pickle.HIGHEST_PROTOCOL)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load_indexed_audio(self, indexed_audio_file_abs_path):\n with open(indexed_audio_file_abs_path, \"rb\") as f:\n self.__timestamps = pickle.load(f)", "response": "Loads the indexed audio file into the internal dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _is_subsequence_of(self, sub, sup):\n return bool(re.search(\".*\".join(sub), sup))", "response": "Returns True if the subsequence of the supsequence of the current set of keys."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _partial_search_validator(self, sub, sup, anagram=False,\n subsequence=False, supersequence=False):\n \"\"\"\n It's responsible for validating the partial results of `search` method.\n If it returns True, the search would return its result. Else, search\n method would discard what it found and look for others.\n\n First, checks to see if all elements of `sub` is in `sup` with at least\n the same frequency and then checks to see if every element `sub`\n appears in `sup` with the same order (index-wise).\n If advanced control sturctures are specified, the containment condition\n won't be checked.\n The code for index checking is from [1]_.\n\n Parameters\n ----------\n sub : list\n sup : list\n anagram : bool, optional\n Default is `False`\n subsequence : bool, optional\n Default is `False`\n supersequence : bool, optional\n Default is `False`\n\n Returns\n -------\n bool\n\n References\n ----------\n .. [1] : `\n https://stackoverflow.com/questions/35964155/checking-if-list-is-a-sublist`\n \"\"\"\n def get_all_in(one, another):\n for element in one:\n if element in another:\n yield element\n\n def containment_check(sub, sup):\n return (set(Counter(sub).keys()).issubset(\n set(Counter(sup).keys())))\n\n def containment_freq_check(sub, sup):\n return (all([Counter(sub)[element] <= Counter(sup)[element]\n for element in Counter(sub)]))\n\n def extra_freq_check(sub, sup, list_of_tups):\n # Would be used for matching anagrams, subsequences etc.\n return (len(list_of_tups) > 0 and\n all([Counter(sub)[tup[0]] <= Counter(sup)[tup[1]]\n for tup in list_of_tups]))\n\n # Regarding containment checking while having extra conditions,\n # there's no good way to map each anagram or subseuqnece etc. that was\n # found to the query word, without making it more complicated than\n # it already is, because a query word can be anagram/subsequence etc.\n # to multiple words of the timestamps yet finding the one with the\n # right index would be the problem.\n # Therefore we just approximate the solution by just counting\n # the elements.\n if len(sub) > len(sup):\n return False\n\n for pred, func in set([(anagram, self._is_anagram_of),\n (subsequence, self._is_subsequence_of),\n (supersequence, self._is_supersequence_of)]):\n if pred:\n pred_seive = [(sub_key, sup_key)\n for sub_key in set(Counter(sub).keys())\n for sup_key in set(Counter(sup).keys())\n if func(sub_key, sup_key)]\n if not extra_freq_check(sub, sup, pred_seive):\n return False\n\n if (\n not any([anagram, subsequence, supersequence]) and\n (not containment_check(sub, sup) or\n not containment_freq_check(sub, sup))\n ):\n return False\n\n for x1, x2 in zip(get_all_in(sup, sub), get_all_in(sub, sup)):\n if x1 != x2:\n return False\n\n return True", "response": "This method is responsible for validating the partial results of a list of elements."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating a generator that searches for the given query within the source directory.", "response": "def search_gen(self, query, audio_basename=None, case_sensitive=False,\n subsequence=False, supersequence=False, timing_error=0.0,\n anagram=False, missing_word_tolerance=0):\n \"\"\"\n A generator that searches for the `query` within the audiofiles of the\n src_dir.\n\n Parameters\n ----------\n query : str\n A string that'll be searched. It'll be splitted on spaces and then\n each word gets sequentially searched.\n audio_basename : str, optional\n Search only within the given audio_basename.\n\n Default is `None`\n case_sensitive : bool, optional\n Default is `False`\n subsequence : bool, optional\n `True` if it's not needed for the exact word be detected and larger\n strings that contain the given one are fine.\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n supersequence : bool, optional\n `True` if it's not needed for the exact word be detected and\n smaller strings that are contained within the given one are fine.\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n anagram : bool, optional\n `True` if it's acceptable for a complete permutation of the word to\n be found. e.g. \"abcde\" would be acceptable for \"edbac\".\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n timing_error : None or float, optional\n Sometimes other words (almost always very small) would be detected\n between the words of the `query`. This parameter defines the\n timing difference/tolerance of the search.\n\n Default is 0.0 i.e. No timing error is tolerated.\n missing_word_tolerance : int, optional\n The number of words that can be missed within the result.\n For example, if the query is \"Some random text\" and the tolerance\n value is `1`, then \"Some text\" would be a valid response.\n Note that the first and last words cannot be missed. Also,\n there'll be an error if the value is more than the number of\n available words. For the example above, any value more than 1\n would have given an error (since there's only one word i.e.\n \"random\" that can be missed)\n\n Default is 0.\n\n Yields\n ------\n {\"File Name\": str, \"Query\": `query`, \"Result\": (float, float)}\n The result of the search is returned as a tuple which is the value\n of the \"Result\" key. The first element of the tuple is the\n starting second of `query` and the last element is the ending\n second of `query`\n\n Raises\n ------\n AssertionError\n If `missing_word_tolerance` value is more than the total number of\n words in the query minus 2 (since the first and the last word\n cannot be removed)\n \"\"\"\n def case_sensitivity_handler(case_sensitive=case_sensitive):\n\n def get_query_words(query, case_sensitive=case_sensitive):\n query_words = list(\n filter(None, ''.join(\n filter(lambda char: char in (ascii_letters + \" \"),\n list(query))).split(\" \")))\n if case_sensitive:\n return query_words\n return [q.lower() for q in query_words]\n\n def get_timestamps(case_sensitive=case_sensitive):\n timestamps = self.get_timestamps().copy()\n if not case_sensitive:\n return {\n audio_basename: [\n _WordBlock(word=word_block.word.lower(),\n start=word_block.start,\n end=word_block.end)\n for word_block in timestamps[audio_basename]]\n for audio_basename in timestamps}\n return timestamps\n\n return locals()\n\n query_words = case_sensitivity_handler()[\"get_query_words\"](query)\n timestamps = case_sensitivity_handler()[\"get_timestamps\"]()\n\n assert abs(missing_word_tolerance -\n (len(query_words) - 2)) >= 0, (\n \"The number of words that can be missing must be less than \"\n \"the total number of words within the query minus the first and \"\n \"the last word.\"\n )\n\n for audio_filename in (\n (lambda: (timestamps.keys() if audio_basename is None else\n [audio_basename]))()):\n result = list()\n missed_words_so_far = 0\n query_cursor = 0\n try:\n for word_block in timestamps[audio_filename]:\n if (\n # When the query is identical\n (word_block.word == query_words[query_cursor]) or\n # When the query is a subsequence of what's\n # available\n (subsequence and\n self._is_subsequence_of(query_words[query_cursor],\n word_block.word)) or\n # When the query is a supersequence of what's\n # available\n (supersequence and self._is_supersequence_of(\n query_words[query_cursor], word_block.word)) or\n # When query is a permutation of what's available.\n (anagram and self._is_anagram_of(\n query_words[query_cursor], word_block.word))\n ):\n result.append(word_block)\n\n if timing_error is not None:\n try:\n if round(result[-1].start -\n result[-2].end, 4) > timing_error:\n result = list()\n query_cursor = 0\n except IndexError:\n pass\n\n if self._partial_search_validator(\n query_words, [x.word for x in result],\n anagram=anagram,\n subsequence=subsequence,\n supersequence=supersequence):\n yield {\n \"File Name\": audio_filename,\n \"Query\": query,\n \"Result\": tuple([result[0].start,\n result[-1].end])}\n result = list()\n query_cursor = 0\n\n else:\n query_cursor += 1\n\n elif missed_words_so_far > missing_word_tolerance:\n result = list()\n query_cursor = 0\n\n elif (missing_word_tolerance > 0) and (len(result) > 0):\n result.append(word_block)\n missed_words_so_far += 1\n\n except KeyError:\n # This is needed for the case where no timestamp is present.\n pass\n\n except IndexError:\n # This is needed when multiple timestamps are present, and\n # advanced control structures like `missed_word_tolerance` are\n # non-zero. In that case, it can search to the end of the first\n # timestamp looking to complete its partial result and since\n # there are no more `word_block`s left, it returns an error.\n # `continue` should be used to reset the partial result and\n # move to the next timestamp.\n continue"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef search_all(self, queries, audio_basename=None, case_sensitive=False,\n subsequence=False, supersequence=False, timing_error=0.0,\n anagram=False, missing_word_tolerance=0):\n \"\"\"\n Returns a dictionary of all results of all of the queries for all of\n the audio files.\n All the specified parameters work per query.\n\n Parameters\n ----------\n queries : [str] or str\n A list of the strings that'll be searched. If type of queries is\n `str`, it'll be insterted into a list within the body of the\n method.\n audio_basename : str, optional\n Search only within the given audio_basename.\n\n Default is `None`.\n case_sensitive : bool\n Default is `False`\n subsequence : bool, optional\n `True` if it's not needed for the exact word be detected and larger\n strings that contain the given one are fine.\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n supersequence : bool, optional\n `True` if it's not needed for the exact word be detected and\n smaller strings that are contained within the given one are fine.\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n anagram : bool, optional\n `True` if it's acceptable for a complete permutation of the word to\n be found. e.g. \"abcde\" would be acceptable for \"edbac\".\n\n If the query is a sentences with multiple words, it'll be\n considered for each word, not the whole sentence.\n\n Default is `False`.\n timing_error : None or float, optional\n Sometimes other words (almost always very small) would be detected\n between the words of the `query`. This parameter defines the\n timing difference/tolerance of the search.\n\n Default is 0.0 i.e. No timing error is tolerated.\n missing_word_tolerance : int, optional\n The number of words that can be missed within the result.\n For example, if the query is \"Some random text\" and the tolerance\n value is `1`, then \"Some text\" would be a valid response.\n Note that the first and last words cannot be missed. Also,\n there'll be an error if the value is more than the number of\n available words. For the example above, any value more than 1\n would have given an error (since there's only one word i.e.\n \"random\" that can be missed)\n\n Default is 0.\n\n Returns\n -------\n search_results : {str: {str: [(float, float)]}}\n A dictionary whose keys are queries and whose values are\n dictionaries whose keys are all the audiofiles in which the query\n is present and whose values are a list whose elements are 2-tuples\n whose first element is the starting second of the query and whose\n values are the ending second. e.g.\n {\"apple\": {\"fruits.wav\" : [(1.1, 1.12)]}}\n\n Raises\n ------\n TypeError\n if `queries` is neither a list nor a str\n \"\"\"\n\n search_gen_rest_of_kwargs = {\n \"audio_basename\": audio_basename,\n \"case_sensitive\": case_sensitive,\n \"subsequence\": subsequence,\n \"supersequence\": supersequence,\n \"timing_error\": timing_error,\n \"anagram\": anagram,\n \"missing_word_tolerance\": missing_word_tolerance}\n\n if not isinstance(queries, (list, str)):\n raise TypeError(\"Invalid query type.\")\n if type(queries) is not list:\n queries = [queries]\n search_results = _PrettyDefaultDict(lambda: _PrettyDefaultDict(list))\n for query in queries:\n search_gen = self.search_gen(query=query,\n **search_gen_rest_of_kwargs)\n for search_result in search_gen:\n search_results[query][\n search_result[\"File Name\"]].append(search_result[\"Result\"])\n return search_results", "response": "This method searches all of the audio files for all of the specified words."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef search_regexp(self, pattern, audio_basename=None):\n\n def indexes_in_transcript_to_start_end_second(index_tup,\n audio_basename):\n \"\"\"\n Calculates the word block index by having the beginning and ending\n index of the matched result from the transcription\n\n Parameters\n ----------\n index_tup : (int, tup)\n index_tup is of the form tuple(index_start, index_end)\n audio_basename : str\n\n Retrun\n ------\n [float, float]\n The time of the output of the matched result. Derived from two\n separate word blocks belonging to the beginning and the end of\n the index_start and index_end.\n \"\"\"\n space_indexes = [i for i, x in enumerate(\n transcription[audio_basename]) if x == \" \"]\n space_indexes.sort(reverse=True)\n index_start, index_end = index_tup\n # re.finditer returns the ending index by one more\n index_end -= 1\n while transcription[audio_basename][index_start] == \" \":\n index_start += 1\n while transcription[audio_basename][index_end] == \" \":\n index_end -= 1\n block_number_start = 0\n block_number_end = len(space_indexes)\n for block_cursor, space_index in enumerate(space_indexes):\n if index_start > space_index:\n block_number_start = (len(space_indexes) - block_cursor)\n break\n for block_cursor, space_index in enumerate(space_indexes):\n if index_end > space_index:\n block_number_end = (len(space_indexes) - block_cursor)\n break\n return (timestamps[audio_basename][block_number_start].start,\n timestamps[audio_basename][block_number_end].end)\n\n timestamps = self.get_timestamps()\n if audio_basename is not None:\n timestamps = {audio_basename: timestamps[audio_basename]}\n transcription = {\n audio_basename: ' '.join(\n [word_block.word for word_block in timestamps[audio_basename]]\n ) for audio_basename in timestamps}\n match_map = map(\n lambda audio_basename: tuple((\n audio_basename,\n re.finditer(pattern, transcription[audio_basename]))),\n transcription.keys())\n search_results = _PrettyDefaultDict(lambda: _PrettyDefaultDict(list))\n for audio_basename, match_iter in match_map:\n for match in match_iter:\n search_results[match.group()][audio_basename].append(\n tuple(indexes_in_transcript_to_start_end_second(\n match.span(), audio_basename)))\n return search_results", "response": "Search the word_blocks of the given pattern and returns a dictionary of the word_blocks that match the given pattern."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rwh_primes1(n):\n # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188\n ''' Returns a list of primes < n '''\n sieve = [True] * (n/2)\n for i in xrange(3,int(n**0.5)+1,2):\n if sieve[i/2]:\n sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)\n return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]", "response": "Returns a list of primes < n"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute the list of prime polynomials for the given generator and galois field characteristic exponent.", "response": "def find_prime_polys(generator=2, c_exp=8, fast_primes=False, single=False):\n '''Compute the list of prime polynomials for the given generator and galois field characteristic exponent.'''\n # fast_primes will output less results but will be significantly faster.\n # single will output the first prime polynomial found, so if all you want is to just find one prime polynomial to generate the LUT for Reed-Solomon to work, then just use that.\n\n # A prime polynomial (necessarily irreducible) is necessary to reduce the multiplications in the Galois Field, so as to avoid overflows.\n # Why do we need a \"prime polynomial\"? Can't we just reduce modulo 255 (for GF(2^8) for example)? Because we need the values to be unique.\n # For example: if the generator (alpha) = 2 and c_exp = 8 (GF(2^8) == GF(256)), then the generated Galois Field (0, 1, \u03b1, \u03b1^1, \u03b1^2, ..., \u03b1^(p-1)) will be galois field it becomes 0, 1, 2, 4, 8, 16, etc. However, upon reaching 128, the next value will be doubled (ie, next power of 2), which will give 256. Then we must reduce, because we have overflowed above the maximum value of 255. But, if we modulo 255, this will generate 256 == 1. Then 2, 4, 8, 16, etc. giving us a repeating pattern of numbers. This is very bad, as it's then not anymore a bijection (ie, a non-zero value doesn't have a unique index). That's why we can't just modulo 255, but we need another number above 255, which is called the prime polynomial.\n # Why so much hassle? Because we are using precomputed look-up tables for multiplication: instead of multiplying a*b, we precompute alpha^a, alpha^b and alpha^(a+b), so that we can just use our lookup table at alpha^(a+b) and get our result. But just like in our original field we had 0,1,2,...,p-1 distinct unique values, in our \"LUT\" field using alpha we must have unique distinct values (we don't care that they are different from the original field as long as they are unique and distinct). That's why we need to avoid duplicated values, and to avoid duplicated values we need to use a prime irreducible polynomial.\n\n # Here is implemented a bruteforce approach to find all these prime polynomials, by generating every possible prime polynomials (ie, every integers between field_charac+1 and field_charac*2), and then we build the whole Galois Field, and we reject the candidate prime polynomial if it duplicates even one value or if it generates a value above field_charac (ie, cause an overflow).\n # Note that this algorithm is slow if the field is too big (above 12), because it's an exhaustive search algorithm. There are probabilistic approaches, and almost surely prime approaches, but there is no determistic polynomial time algorithm to find irreducible monic polynomials. More info can be found at: http://people.mpi-inf.mpg.de/~csaha/lectures/lec9.pdf\n # Another faster algorithm may be found at Adleman, Leonard M., and Hendrik W. Lenstra. \"Finding irreducible polynomials over finite fields.\" Proceedings of the eighteenth annual ACM symposium on Theory of computing. ACM, 1986.\n\n # Prepare the finite field characteristic (2^p - 1), this also represent the maximum possible value in this field\n root_charac = 2 # we're in GF(2)\n field_charac = int(root_charac**c_exp - 1)\n field_charac_next = int(root_charac**(c_exp+1) - 1)\n\n prim_candidates = []\n if fast_primes:\n prim_candidates = rwh_primes1(field_charac_next) # generate maybe prime polynomials and check later if they really are irreducible\n prim_candidates = [x for x in prim_candidates if x > field_charac] # filter out too small primes\n else:\n prim_candidates = xrange(field_charac+2, field_charac_next, root_charac) # try each possible prime polynomial, but skip even numbers (because divisible by 2 so necessarily not irreducible)\n\n # Start of the main loop\n correct_primes = []\n for prim in prim_candidates: # try potential candidates primitive irreducible polys\n seen = bytearray(field_charac+1) # memory variable to indicate if a value was already generated in the field (value at index x is set to 1) or not (set to 0 by default)\n conflict = False # flag to know if there was at least one conflict\n\n # Second loop, build the whole Galois Field\n x = 1\n for i in xrange(field_charac):\n # Compute the next value in the field (ie, the next power of alpha/generator)\n x = gf_mult_noLUT(x, generator, prim, field_charac+1)\n\n # Rejection criterion: if the value overflowed (above field_charac) or is a duplicate of a previously generated power of alpha, then we reject this polynomial (not prime)\n if x > field_charac or seen[x] == 1:\n conflict = True\n break\n # Else we flag this value as seen (to maybe detect future duplicates), and we continue onto the next power of alpha\n else:\n seen[x] = 1\n\n # End of the second loop: if there's no conflict (no overflow nor duplicated value), this is a prime polynomial!\n if not conflict: \n correct_primes.append(prim)\n if single: return prim\n\n # Return the list of all prime polynomials\n return correct_primes"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef gf_poly_mul(p, q):\n '''Multiply two polynomials, inside Galois Field (but the procedure is generic). Optimized function by precomputation of log.'''\n # Pre-allocate the result array\n r = bytearray(len(p) + len(q) - 1)\n # Precompute the logarithm of p\n lp = [gf_log[p[i]] for i in xrange(len(p))]\n # Compute the polynomial multiplication (just like the outer product of two vectors, we multiply each coefficients of p with all coefficients of q)\n for j in xrange(len(q)):\n qj = q[j] # optimization: load the coefficient once\n if qj != 0: # log(0) is undefined, we need to check that\n lq = gf_log[qj] # Optimization: precache the logarithm of the current coefficient of q\n for i in xrange(len(p)):\n if p[i] != 0: # log(0) is undefined, need to check that...\n r[i + j] ^= gf_exp[lp[i] + lq] # equivalent to: r[i + j] = gf_add(r[i+j], gf_mul(p[i], q[j]))\n return r", "response": "Multiply two polynomials inside Galois Field. Optimized function by precomputation of log."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmultiply two polynomials inside Galois Field", "response": "def gf_poly_mul_simple(p, q): # simple equivalent way of multiplying two polynomials without precomputation, but thus it's slower\n '''Multiply two polynomials, inside Galois Field'''\n # Pre-allocate the result array\n r = bytearray(len(p) + len(q) - 1)\n # Compute the polynomial multiplication (just like the outer product of two vectors, we multiply each coefficients of p with all coefficients of q)\n for j in xrange(len(q)):\n for i in xrange(len(p)):\n r[i + j] ^= gf_mul(p[i], q[j]) # equivalent to: r[i + j] = gf_add(r[i+j], gf_mul(p[i], q[j])) -- you can see it's your usual polynomial multiplication\n return r"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfast polynomial division by using Extended Synthetic Division and optimized for GF ( 2^p computations.", "response": "def gf_poly_div(dividend, divisor):\n '''Fast polynomial division by using Extended Synthetic Division and optimized for GF(2^p) computations (doesn't work with standard polynomials outside of this galois field).'''\n # CAUTION: this function expects polynomials to follow the opposite convention at decoding: the terms must go from the biggest to lowest degree (while most other functions here expect a list from lowest to biggest degree). eg: 1 + 2x + 5x^2 = [5, 2, 1], NOT [1, 2, 5]\n\n msg_out = bytearray(dividend) # Copy the dividend list and pad with 0 where the ecc bytes will be computed\n #normalizer = divisor[0] # precomputing for performance\n for i in xrange(len(dividend) - (len(divisor)-1)):\n #msg_out[i] /= normalizer # for general polynomial division (when polynomials are non-monic), the usual way of using synthetic division is to divide the divisor g(x) with its leading coefficient (call it a). In this implementation, this means:we need to compute: coef = msg_out[i] / gen[0]. For more infos, see http://en.wikipedia.org/wiki/Synthetic_division\n coef = msg_out[i] # precaching\n if coef != 0: # log(0) is undefined, so we need to avoid that case explicitly (and it's also a good optimization). In fact if you remove it, it should still work because gf_mul() will take care of the condition. But it's still a good practice to put the condition here.\n for j in xrange(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisior, because it's only used to normalize the dividend coefficient\n if divisor[j] != 0: # log(0) is undefined\n msg_out[i + j] ^= gf_mul(divisor[j], coef) # equivalent to the more mathematically correct (but xoring directly is faster): msg_out[i + j] += -divisor[j] * coef\n\n # The resulting msg_out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder has necessarily the same degree as the divisor -- not length but degree == length-1 -- since it's what we couldn't divide from the dividend), so we compute the index where this separation is, and return the quotient and remainder.\n separator = -(len(divisor)-1)\n return msg_out[:separator], msg_out[separator:]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef gf_poly_square(poly):\n '''Linear time implementation of polynomial squaring. For details, see paper: \"A fast software implementation for arithmetic operations in GF (2n)\". De Win, E., Bosselaers, A., Vandenberghe, S., De Gersem, P., & Vandewalle, J. (1996, January). In Advances in Cryptology - Asiacrypt'96 (pp. 65-76). Springer Berlin Heidelberg.'''\n length = len(poly)\n out = bytearray(2*length - 1)\n for i in xrange(length-1):\n p = poly[i]\n k = 2*i\n if p != 0:\n #out[k] = gf_exp[(2*gf_log[p]) % field_charac] # not necessary to modulo (2^r)-1 since gf_exp is duplicated up to 510.\n out[k] = gf_exp[2*gf_log[p]]\n #else: # not necessary since the output is already initialized to an array of 0\n #out[k] = 0\n out[2*length-2] = gf_exp[2*gf_log[poly[length-1]]]\n if out[0] == 0: out[0] = 2*poly[1] - 1\n return out", "response": "Linear time implementation of polynomial squaring."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nevaluate a polynomial in GF ( 2^p given the value for x. This is based on Horner s scheme for maximum efficiency.", "response": "def gf_poly_eval(poly, x):\n '''Evaluates a polynomial in GF(2^p) given the value for x. This is based on Horner's scheme for maximum efficiency.'''\n y = poly[0]\n for i in xrange(1, len(poly)):\n y = gf_mul(y, x) ^ poly[i]\n return y"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates an irreducible generator polynomial", "response": "def rs_generator_poly(nsym, fcr=0, generator=2):\n '''Generate an irreducible generator polynomial (necessary to encode a message into Reed-Solomon)'''\n g = bytearray([1])\n for i in xrange(nsym):\n g = gf_poly_mul(g, [1, gf_pow(generator, i+fcr)])\n return g"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngenerates all irreducible generator polynomials up to max_nsym.", "response": "def rs_generator_poly_all(max_nsym, fcr=0, generator=2):\n '''Generate all irreducible generator polynomials up to max_nsym (usually you can use n, the length of the message+ecc). Very useful to reduce processing time if you want to encode using variable schemes and nsym rates.'''\n g_all = {}\n g_all[0] = g_all[1] = [1]\n for nsym in xrange(max_nsym):\n g_all[nsym] = rs_generator_poly(nsym, fcr, generator)\n return g_all"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalculates the syndromes polynomial for a given codeword msg.", "response": "def rs_calc_syndromes(msg, nsym, fcr=0, generator=2):\n '''Given the received codeword msg and the number of error correcting symbols (nsym), computes the syndromes polynomial.\n Mathematically, it's essentially equivalent to a Fourrier Transform (Chien search being the inverse).\n '''\n # Note the \"[0] +\" : we add a 0 coefficient for the lowest degree (the constant). This effectively shifts the syndrome, and will shift every computations depending on the syndromes (such as the errors locator polynomial, errors evaluator polynomial, etc. but not the errors positions).\n # This is not necessary as anyway syndromes are defined such as there are only non-zero coefficients (the only 0 is the shift of the constant here) and subsequent computations will/must account for the shift by skipping the first iteration (eg, the often seen range(1, n-k+1)), but you can also avoid prepending the 0 coeff and adapt every subsequent computations to start from 0 instead of 1.\n return [0] + [gf_poly_eval(msg, gf_pow(generator, i+fcr)) for i in xrange(nsym)]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef rs_correct_errata(msg_in, synd, err_pos, fcr=0, generator=2): # err_pos is a list of the positions of the errors/erasures/errata\n '''Forney algorithm, computes the values (error magnitude) to correct the input message.'''\n global field_charac\n msg = bytearray(msg_in)\n # calculate errata locator polynomial to correct both errors and erasures (by combining the errors positions given by the error locator polynomial found by BM with the erasures positions given by caller)\n coef_pos = [len(msg) - 1 - p for p in err_pos] # need to convert the positions to coefficients degrees for the errata locator algo to work (eg: instead of [0, 1, 2] it will become [len(msg)-1, len(msg)-2, len(msg) -3])\n err_loc = rs_find_errata_locator(coef_pos, generator)\n # calculate errata evaluator polynomial (often called Omega or Gamma in academic papers)\n err_eval = rs_find_error_evaluator(synd[::-1], err_loc, len(err_loc)-1)[::-1]\n\n # Second part of Chien search to get the error location polynomial X from the error positions in err_pos (the roots of the error locator polynomial, ie, where it evaluates to 0)\n X = [] # will store the position of the errors\n for i in xrange(len(coef_pos)):\n l = field_charac - coef_pos[i]\n X.append( gf_pow(generator, -l) )\n\n # Forney algorithm: compute the magnitudes\n E = bytearray(len(msg)) # will store the values that need to be corrected (substracted) to the message containing errors. This is sometimes called the error magnitude polynomial.\n Xlength = len(X)\n for i, Xi in enumerate(X):\n\n Xi_inv = gf_inverse(Xi)\n\n # Compute the formal derivative of the error locator polynomial (see Blahut, Algebraic codes for data transmission, pp 196-197).\n # the formal derivative of the errata locator is used as the denominator of the Forney Algorithm, which simply says that the ith error value is given by error_evaluator(gf_inverse(Xi)) / error_locator_derivative(gf_inverse(Xi)). See Blahut, Algebraic codes for data transmission, pp 196-197.\n err_loc_prime_tmp = []\n for j in xrange(Xlength):\n if j != i:\n err_loc_prime_tmp.append( gf_sub(1, gf_mul(Xi_inv, X[j])) )\n # compute the product, which is the denominator of the Forney algorithm (errata locator derivative)\n err_loc_prime = 1\n for coef in err_loc_prime_tmp:\n err_loc_prime = gf_mul(err_loc_prime, coef)\n # equivalent to: err_loc_prime = functools.reduce(gf_mul, err_loc_prime_tmp, 1)\n\n # Compute y (evaluation of the errata evaluator polynomial)\n # This is a more faithful translation of the theoretical equation contrary to the old forney method. Here it is exactly copy/pasted from the included presentation decoding_rs.pdf: Yl = omega(Xl.inverse()) / prod(1 - Xj*Xl.inverse()) for j in len(X) (in the paper it's for j in s, but it's useless when len(X) < s because we compute neutral terms 1 for nothing, and wrong when correcting more than s erasures or erasures+errors since it prevents computing all required terms).\n # Thus here this method works with erasures too because firstly we fixed the equation to be like the theoretical one (don't know why it was modified in _old_forney(), if it's an optimization, it doesn't enhance anything), and secondly because we removed the product bound on s, which prevented computing errors and erasures above the s=(n-k)//2 bound.\n y = gf_poly_eval(err_eval[::-1], Xi_inv) # numerator of the Forney algorithm (errata evaluator evaluated)\n y = gf_mul(gf_pow(Xi, 1-fcr), y) # adjust to fcr parameter\n \n # Compute the magnitude\n magnitude = gf_div(y, err_loc_prime) # magnitude value of the error, calculated by the Forney algorithm (an equation in fact): dividing the errata evaluator with the errata locator derivative gives us the errata magnitude (ie, value to repair) the ith symbol\n E[err_pos[i]] = magnitude # store the magnitude for this error into the magnitude polynomial\n\n # Apply the correction of values to get our message corrected! (note that the ecc bytes also gets corrected!)\n # (this isn't the Forney algorithm, we just apply the result of decoding here)\n msg = gf_poly_add(msg, E) # equivalent to Ci = Ri - Ei where Ci is the correct message, Ri the received (senseword) message, and Ei the errata magnitudes (minus is replaced by XOR since it's equivalent in GF(2^p)). So in fact here we substract from the received message the errors magnitude, which logically corrects the value to what it should be.\n return msg", "response": "Forney algorithm computes the values to correct the input message."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rs_find_error_locator(synd, nsym, erase_loc=None, erase_count=0):\n '''Find error/errata locator and evaluator polynomials with Berlekamp-Massey algorithm'''\n # The idea is that BM will iteratively estimate the error locator polynomial.\n # To do this, it will compute a Discrepancy term called Delta, which will tell us if the error locator polynomial needs an update or not\n # (hence why it's called discrepancy: it tells us when we are getting off board from the correct value).\n\n # Init the polynomials\n if erase_loc: # if the erasure locator polynomial is supplied, we init with its value, so that we include erasures in the final locator polynomial\n err_loc = bytearray(erase_loc)\n old_loc = bytearray(erase_loc)\n else:\n err_loc = bytearray([1]) # This is the main variable we want to fill, also called Sigma in other notations or more formally the errors/errata locator polynomial.\n old_loc = bytearray([1]) # BM is an iterative algorithm, and we need the errata locator polynomial of the previous iteration in order to update other necessary variables.\n #L = 0 # update flag variable, not needed here because we use an alternative equivalent way of checking if update is needed (but using the flag could potentially be faster depending on if using length(list) is taking linear time in your language, here in Python it's constant so it's as fast.\n\n # Fix the syndrome shifting: when computing the syndrome, some implementations may prepend a 0 coefficient for the lowest degree term (the constant). This is a case of syndrome shifting, thus the syndrome will be bigger than the number of ecc symbols (I don't know what purpose serves this shifting). If that's the case, then we need to account for the syndrome shifting when we use the syndrome such as inside BM, by skipping those prepended coefficients.\n # Another way to detect the shifting is to detect the 0 coefficients: by definition, a syndrome does not contain any 0 coefficient (except if there are no errors/erasures, in this case they are all 0). This however doesn't work with the modified Forney syndrome, which set to 0 the coefficients corresponding to erasures, leaving only the coefficients corresponding to errors.\n synd_shift = 0\n if len(synd) > nsym: synd_shift = len(synd) - nsym\n\n for i in xrange(nsym-erase_count): # generally: nsym-erase_count == len(synd), except when you input a partial erase_loc and using the full syndrome instead of the Forney syndrome, in which case nsym-erase_count is more correct (len(synd) will fail badly with IndexError).\n if erase_loc: # if an erasures locator polynomial was provided to init the errors locator polynomial, then we must skip the FIRST erase_count iterations (not the last iterations, this is very important!)\n K = erase_count+i+synd_shift\n else: # if erasures locator is not provided, then either there's no erasures to account or we use the Forney syndromes, so we don't need to use erase_count nor erase_loc (the erasures have been trimmed out of the Forney syndromes).\n K = i+synd_shift\n\n # Compute the discrepancy Delta\n # Here is the close-to-the-books operation to compute the discrepancy Delta: it's a simple polynomial multiplication of error locator with the syndromes, and then we get the Kth element.\n #delta = gf_poly_mul(err_loc[::-1], synd)[K] # theoretically it should be gf_poly_add(synd[::-1], [1])[::-1] instead of just synd, but it seems it's not absolutely necessary to correctly decode.\n # But this can be optimized: since we only need the Kth element, we don't need to compute the polynomial multiplication for any other element but the Kth. Thus to optimize, we compute the polymul only at the item we need, skipping the rest (avoiding a nested loop, thus we are linear time instead of quadratic).\n # This optimization is actually described in several figures of the book \"Algebraic codes for data transmission\", Blahut, Richard E., 2003, Cambridge university press.\n delta = synd[K]\n for j in xrange(1, len(err_loc)):\n delta ^= gf_mul(err_loc[-(j+1)], synd[K - j]) # delta is also called discrepancy. Here we do a partial polynomial multiplication (ie, we compute the polynomial multiplication only for the term of degree K). Should be equivalent to brownanrs.polynomial.mul_at().\n #print \"delta\", K, delta, list(gf_poly_mul(err_loc[::-1], synd)) # debugline\n\n # Shift polynomials to compute the next degree\n old_loc = old_loc + bytearray([0])\n\n # Iteratively estimate the errata locator and evaluator polynomials\n if delta != 0: # Update only if there's a discrepancy\n if len(old_loc) > len(err_loc): # Rule B (rule A is implicitly defined because rule A just says that we skip any modification for this iteration)\n #if 2*L <= K+erase_count: # equivalent to len(old_loc) > len(err_loc), as long as L is correctly computed\n # Computing errata locator polynomial Sigma\n new_loc = gf_poly_scale(old_loc, delta)\n old_loc = gf_poly_scale(err_loc, gf_inverse(delta)) # effectively we are doing err_loc * 1/delta = err_loc // delta\n err_loc = new_loc\n # Update the update flag\n #L = K - L # the update flag L is tricky: in Blahut's schema, it's mandatory to use `L = K - L - erase_count` (and indeed in a previous draft of this function, if you forgot to do `- erase_count` it would lead to correcting only 2*(errors+erasures) <= (n-k) instead of 2*errors+erasures <= (n-k)), but in this latest draft, this will lead to a wrong decoding in some cases where it should correctly decode! Thus you should try with and without `- erase_count` to update L on your own implementation and see which one works OK without producing wrong decoding failures.\n\n # Update with the discrepancy\n err_loc = gf_poly_add(err_loc, gf_poly_scale(old_loc, delta))\n\n # Check if the result is correct, that there's not too many errors to correct\n err_loc = list(itertools.dropwhile(lambda x: x == 0, err_loc)) # drop leading 0s, else errs will not be of the correct size\n errs = len(err_loc) - 1\n if (errs-erase_count) * 2 + erase_count > nsym:\n raise ReedSolomonError(\"Too many errors to correct\")\n\n return err_loc", "response": "Find error and errata locator polynomials for a syndrometric symbol."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nfinds the error locator polynomial from the erasures and errors positions.", "response": "def rs_find_errata_locator(e_pos, generator=2):\n '''Compute the erasures/errors/errata locator polynomial from the erasures/errors/errata positions (the positions must be relative to the x coefficient, eg: \"hello worldxxxxxxxxx\" is tampered to \"h_ll_ worldxxxxxxxxx\" with xxxxxxxxx being the ecc of length n-k=9, here the string positions are [1, 4], but the coefficients are reversed since the ecc characters are placed as the first coefficients of the polynomial, thus the coefficients of the erased characters are n-1 - [1, 4] = [18, 15] = erasures_loc to be specified as an argument.'''\n # See: http://ocw.usu.edu/Electrical_and_Computer_Engineering/Error_Control_Coding/lecture7.pdf and Blahut, Richard E. \"Transform techniques for error control codes.\" IBM Journal of Research and development 23.3 (1979): 299-315. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.600&rep=rep1&type=pdf and also a MatLab implementation here: http://www.mathworks.com/matlabcentral/fileexchange/23567-reed-solomon-errors-and-erasures-decoder/content//RS_E_E_DEC.m\n e_loc = [1] # just to init because we will multiply, so it must be 1 so that the multiplication starts correctly without nulling any term\n # erasures_loc is very simple to compute: erasures_loc = prod(1 - x*alpha**i) for i in erasures_pos and where alpha is the alpha chosen to evaluate polynomials (here in this library it's gf(3)). To generate c*x where c is a constant, we simply generate a Polynomial([c, 0]) where 0 is the constant and c is positionned to be the coefficient for x^1.\n for i in e_pos:\n e_loc = gf_poly_mul( e_loc, gf_poly_add([1], [gf_pow(generator, i), 0]) )\n return e_loc"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute the error evaluator polynomial Omega from the syndrome and the error locator polynomial.", "response": "def rs_find_error_evaluator(synd, err_loc, nsym):\n '''Compute the error (or erasures if you supply sigma=erasures locator polynomial, or errata) evaluator polynomial Omega from the syndrome and the error/erasures/errata locator Sigma. Omega is already computed at the same time as Sigma inside the Berlekamp-Massey implemented above, but in case you modify Sigma, you can recompute Omega afterwards using this method, or just ensure that Omega computed by BM is correct given Sigma.'''\n # Omega(x) = [ Synd(x) * Error_loc(x) ] mod x^(n-k+1)\n _, remainder = gf_poly_div( gf_poly_mul(synd, err_loc), ([1] + [0]*(nsym+1)) ) # first multiply syndromes * errata_locator, then do a polynomial division to truncate the polynomial to the required length\n\n # Faster way that is equivalent\n #remainder = gf_poly_mul(synd, err_loc) # first multiply the syndromes with the errata locator polynomial\n #remainder = remainder[len(remainder)-(nsym+1):] # then divide by a polynomial of the length we want, which is equivalent to slicing the list (which represents the polynomial)\n\n return remainder"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef rs_find_errors(err_loc, nmess, generator=2):\n '''Find the roots (ie, where evaluation = zero) of error polynomial by bruteforce trial, this is a sort of Chien's search (but less efficient, Chien's search is a way to evaluate the polynomial such that each evaluation only takes constant time).'''\n # nmess = length of whole codeword (message + ecc symbols)\n errs = len(err_loc) - 1\n err_pos = []\n for i in xrange(nmess): # normally we should try all 2^8 possible values, but here we optimize to just check the interesting symbols\n if gf_poly_eval(err_loc, gf_pow(generator, i)) == 0: # It's a 0? Bingo, it's a root of the error locator polynomial, in other terms this is the location of an error\n err_pos.append(nmess - 1 - i)\n # Sanity check: the number of errors/errata positions found should be exactly the same as the length of the errata locator polynomial\n if len(err_pos) != errs:\n # TODO: to decode messages+ecc with length n > 255, we may try to use a bruteforce approach: the correct positions ARE in the final array j, but the problem is because we are above the Galois Field's range, there is a wraparound so that for example if j should be [0, 1, 2, 3], we will also get [255, 256, 257, 258] (because 258 % 255 == 3, same for the other values), so we can't discriminate. The issue is that fixing any errs_nb errors among those will always give a correct output message (in the sense that the syndrome will be all 0), so we may not even be able to check if that's correct or not, so I'm not sure the bruteforce approach may even be possible.\n raise ReedSolomonError(\"Too many (or few) errors found by Chien Search for the errata locator polynomial!\")\n return err_pos", "response": "Find the roots of the error locator polynomial by Chien s search."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncorrects a message using the Chien Search algorithm.", "response": "def rs_correct_msg(msg_in, nsym, fcr=0, generator=2, erase_pos=None, only_erasures=False):\n '''Reed-Solomon main decoding function'''\n global field_charac\n if len(msg_in) > field_charac:\n # Note that it is in fact possible to encode/decode messages that are longer than field_charac, but because this will be above the field, this will generate more error positions during Chien Search than it should, because this will generate duplicate values, which should normally be prevented thank's to the prime polynomial reduction (eg, because it can't discriminate between error at position 1 or 256, both being exactly equal under galois field 2^8). So it's really not advised to do it, but it's possible (but then you're not guaranted to be able to correct any error/erasure on symbols with a position above the length of field_charac -- if you really need a bigger message without chunking, then you should better enlarge c_exp so that you get a bigger field).\n raise ValueError(\"Message is too long (%i when max is %i)\" % (len(msg_in), field_charac))\n\n msg_out = bytearray(msg_in) # copy of message\n # erasures: set them to null bytes for easier decoding (but this is not necessary, they will be corrected anyway, but debugging will be easier with null bytes because the error locator polynomial values will only depend on the errors locations, not their values)\n if erase_pos is None:\n erase_pos = []\n else:\n for e_pos in erase_pos:\n msg_out[e_pos] = 0\n # check if there are too many erasures to correct (beyond the Singleton bound)\n if len(erase_pos) > nsym: raise ReedSolomonError(\"Too many erasures to correct\")\n # prepare the syndrome polynomial using only errors (ie: errors = characters that were either replaced by null byte or changed to another character, but we don't know their positions)\n synd = rs_calc_syndromes(msg_out, nsym, fcr, generator)\n # check if there's any error/erasure in the input codeword. If not (all syndromes coefficients are 0), then just return the codeword as-is.\n if max(synd) == 0:\n return msg_out[:-nsym], msg_out[-nsym:] # no errors\n \n # Find errors locations\n if only_erasures:\n err_pos = []\n else:\n # compute the Forney syndromes, which hide the erasures from the original syndrome (so that BM will just have to deal with errors, not erasures)\n fsynd = rs_forney_syndromes(synd, erase_pos, len(msg_out), generator)\n # compute the error locator polynomial using Berlekamp-Massey\n err_loc = rs_find_error_locator(fsynd, nsym, erase_count=len(erase_pos))\n # locate the message errors using Chien search (or bruteforce search)\n err_pos = rs_find_errors(err_loc[::-1], len(msg_out), generator)\n if err_pos is None:\n raise ReedSolomonError(\"Could not locate error\")\n\n # Find errors values and apply them to correct the message\n # compute errata evaluator and errata magnitude polynomials, then correct errors and erasures\n msg_out = rs_correct_errata(msg_out, synd, (erase_pos + err_pos), fcr, generator) # note that we here use the original syndrome, not the forney syndrome (because we will correct both errors and erasures, so we need the full syndrome)\n # check if the final message is fully repaired\n synd = rs_calc_syndromes(msg_out, nsym, fcr, generator)\n if max(synd) > 0:\n raise ReedSolomonError(\"Could not correct message\")\n # return the successfully decoded message\n return msg_out[:-nsym], msg_out[-nsym:]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rs_correct_msg_nofsynd(msg_in, nsym, fcr=0, generator=2, erase_pos=None, only_erasures=False):\n '''Reed-Solomon main decoding function, without using the modified Forney syndromes'''\n global field_charac\n if len(msg_in) > field_charac:\n raise ValueError(\"Message is too long (%i when max is %i)\" % (len(msg_in), field_charac))\n\n msg_out = bytearray(msg_in) # copy of message\n # erasures: set them to null bytes for easier decoding (but this is not necessary, they will be corrected anyway, but debugging will be easier with null bytes because the error locator polynomial values will only depend on the errors locations, not their values)\n if erase_pos is None:\n erase_pos = []\n else:\n for e_pos in erase_pos:\n msg_out[e_pos] = 0\n # check if there are too many erasures\n if len(erase_pos) > nsym: raise ReedSolomonError(\"Too many erasures to correct\")\n # prepare the syndrome polynomial using only errors (ie: errors = characters that were either replaced by null byte or changed to another character, but we don't know their positions)\n synd = rs_calc_syndromes(msg_out, nsym, fcr, generator)\n # check if there's any error/erasure in the input codeword. If not (all syndromes coefficients are 0), then just return the codeword as-is.\n if max(synd) == 0:\n return msg_out[:-nsym], msg_out[-nsym:] # no errors\n\n # prepare erasures locator and evaluator polynomials\n erase_loc = None\n #erase_eval = None\n erase_count = 0\n if erase_pos:\n erase_count = len(erase_pos)\n erase_pos_reversed = [len(msg_out)-1-eras for eras in erase_pos]\n erase_loc = rs_find_errata_locator(erase_pos_reversed, generator=generator)\n #erase_eval = rs_find_error_evaluator(synd[::-1], erase_loc, len(erase_loc)-1)\n\n # prepare errors/errata locator polynomial\n if only_erasures:\n err_loc = erase_loc[::-1]\n #err_eval = erase_eval[::-1]\n else:\n err_loc = rs_find_error_locator(synd, nsym, erase_loc=erase_loc, erase_count=erase_count)\n err_loc = err_loc[::-1]\n #err_eval = rs_find_error_evaluator(synd[::-1], err_loc[::-1], len(err_loc)-1)[::-1] # find error/errata evaluator polynomial (not really necessary since we already compute it at the same time as the error locator poly in BM)\n\n # locate the message errors\n err_pos = rs_find_errors(err_loc, len(msg_out), generator) # find the roots of the errata locator polynomial (ie: the positions of the errors/errata)\n if err_pos is None:\n raise ReedSolomonError(\"Could not locate error\")\n\n # compute errata evaluator and errata magnitude polynomials, then correct errors and erasures\n msg_out = rs_correct_errata(msg_out, synd, err_pos, fcr=fcr, generator=generator)\n # check if the final message is fully repaired\n synd = rs_calc_syndromes(msg_out, nsym, fcr, generator)\n if max(synd) > 0:\n raise ReedSolomonError(\"Could not correct message\")\n # return the successfully decoded message\n return msg_out[:-nsym], msg_out[-nsym:]", "response": "This function is used to correct the message using the modified Forney syndromes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef rs_check(msg, nsym, fcr=0, generator=2):\n '''Returns true if the message + ecc has no error of false otherwise (may not always catch a wrong decoding or a wrong message, particularly if there are too many errors -- above the Singleton bound --, but it usually does)'''\n return ( max(rs_calc_syndromes(msg, nsym, fcr, generator)) == 0 )", "response": "Returns true if the message + ecc has no error of false otherwise"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef encode(self, data):\n '''Encode a message (ie, add the ecc symbols) using Reed-Solomon, whatever the length of the message because we use chunking'''\n if isinstance(data, str):\n data = bytearray(data, \"latin-1\")\n chunk_size = self.nsize - self.nsym\n enc = bytearray()\n for i in xrange(0, len(data), chunk_size):\n chunk = data[i:i+chunk_size]\n enc.extend(rs_encode_msg(chunk, self.nsym, fcr=self.fcr, generator=self.generator))\n return enc", "response": "Encode a message using Reed - Solomon"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef decode(self, data, erase_pos=None, only_erasures=False):\n '''Repair a message, whatever its size is, by using chunking'''\n # erase_pos is a list of positions where you know (or greatly suspect at least) there is an erasure (ie, wrong character but you know it's at this position). Just input the list of all positions you know there are errors, and this method will automatically split the erasures positions to attach to the corresponding data chunk.\n if isinstance(data, str):\n data = bytearray(data, \"latin-1\")\n dec = bytearray()\n for i in xrange(0, len(data), self.nsize):\n # Split the long message in a chunk\n chunk = data[i:i+self.nsize]\n # Extract the erasures for this chunk\n e_pos = []\n if erase_pos:\n # First extract the erasures for this chunk (all erasures below the maximum chunk length)\n e_pos = [x for x in erase_pos if x <= self.nsize]\n # Then remove the extract erasures from the big list and also decrement all subsequent positions values by nsize (the current chunk's size) so as to prepare the correct alignment for the next iteration\n erase_pos = [x - (self.nsize+1) for x in erase_pos if x > self.nsize]\n # Decode/repair this chunk!\n dec.extend(rs_correct_msg(chunk, self.nsym, fcr=self.fcr, generator=self.generator, erase_pos=e_pos, only_erasures=only_erasures)[0])\n return dec", "response": "Decode a message from a byte string."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef recurse( record, index, stop_types=STOP_TYPES,already_seen=None, type_group=False ):\n if already_seen is None:\n already_seen = set()\n if record['address'] not in already_seen:\n already_seen.add(record['address'])\n if 'refs' in record:\n for child in children( record, index, stop_types=stop_types ):\n if child['address'] not in already_seen:\n for descendant in recurse( \n child, index, stop_types, \n already_seen=already_seen, type_group=type_group,\n ):\n yield descendant\n yield record", "response": "Recursive generator for the tree traversal of a dictionary record."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfinds all loops within the index and replace with loop records", "response": "def find_loops( record, index, stop_types = STOP_TYPES, open=None, seen = None ):\n \"\"\"Find all loops within the index and replace with loop records\"\"\"\n if open is None:\n open = []\n if seen is None:\n seen = set()\n for child in children( record, index, stop_types = stop_types ):\n if child['type'] in stop_types or child['type'] == LOOP_TYPE:\n continue\n if child['address'] in open:\n # loop has been found \n start = open.index( child['address'] )\n new = frozenset( open[start:] )\n if new not in seen:\n seen.add(new)\n yield new\n elif child['address'] in seen:\n continue \n else:\n seen.add( child['address'])\n open.append( child['address'] )\n for loop in find_loops( child, index, stop_types=stop_types, open=open, seen=seen ):\n yield loop \n open.pop( -1 )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef promote_loops( loops, index, shared ):\n for loop in loops:\n loop = list(loop)\n members = [index[addr] for addr in loop]\n external_parents = list(set([\n addr for addr in sum([shared.get(addr,[]) for addr in loop],[])\n if addr not in loop \n ]))\n if external_parents:\n if len(external_parents) == 1:\n # potentially a loop that's been looped...\n parent = index.get( external_parents[0] )\n if parent['type'] == LOOP_TYPE:\n continue \n # we haven't already been looped...\n loop_addr = new_address( index )\n shared[loop_addr] = external_parents\n loop_record = index[loop_addr] = {\n 'address': loop_addr,\n 'refs': loop,\n 'parents': external_parents,\n 'type': LOOP_TYPE,\n 'size': 0,\n }\n for member in members:\n # member's references must *not* point to loop...\n member['refs'] = [\n ref for ref in member['refs']\n if ref not in loop \n ]\n # member's parents are *just* the loop\n member['parents'][:] = [loop_addr]\n # each referent to loop holds a single reference to the loop rather than many to children\n for parent in external_parents:\n parent = index[parent]\n for member in members:\n rewrite_references( parent['refs'], member['address'], None )\n parent['refs'].append( loop_addr )", "response": "Turn loops into objects that can be processed normally"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nretrieve children records for given record", "response": "def children( record, index, key='refs', stop_types=STOP_TYPES ):\n \"\"\"Retrieve children records for given record\"\"\"\n result = []\n for ref in record.get( key,[]):\n try:\n record = index[ref]\n except KeyError, err:\n #print 'No record for %s address %s in %s'%(key, ref, record['address'] )\n pass # happens when an unreachable references a reachable that has been compressed out...\n else:\n if record['type'] not in stop_types:\n result.append( record )\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef children_types( record, index, key='refs', stop_types=STOP_TYPES ):\n types = {}\n for child in children( record, index, key, stop_types=stop_types ):\n types.setdefault(child['type'],[]).append( child )\n return types", "response": "Produce dictionary mapping type - key to instances for all children"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a recursive - cost hierarchy based on the overall record and the shared dictionary.", "response": "def recurse_module( overall_record, index, shared, stop_types=STOP_TYPES, already_seen=None, min_size=0 ):\n \"\"\"Creates a has-a recursive-cost hierarchy\n \n Mutates objects in-place to produce a hierarchy of memory usage based on \n reference-holding cost assignment\n \"\"\"\n for record in recurse( \n overall_record, index, \n stop_types=stop_types, \n already_seen=already_seen, \n type_group=True,\n ):\n # anything with a totsize we've already processed...\n if record.get('totsize') is not None:\n continue \n rinfo = record \n rinfo['module'] = overall_record.get('name',NON_MODULE_REFS )\n if not record['refs']:\n rinfo['rsize'] = 0\n rinfo['children'] = []\n else:\n # TODO: provide a flag to coalesce based on e.g. type at each level or throughout...\n rinfo['children'] = rinfo_children = list ( children( record, index, stop_types=stop_types ) )\n rinfo['rsize'] = sum([\n (\n child.get('totsize',0.0)/float(len(shared.get( child['address'], [])) or 1)\n )\n for child in rinfo_children\n ], 0.0 )\n rinfo['totsize'] = record['size'] + rinfo['rsize']\n \n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rewrite_refs( targets, old,new, index, key='refs', single_ref=False ):\n for parent in targets:\n if not isinstance( parent, dict ):\n try:\n parent = index[parent]\n except KeyError, err:\n continue \n rewrite_references( parent[key], old, new, single_ref=single_ref )", "response": "Rewrite key in all targets to replace old with new"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef rewrite_references( sequence, old, new, single_ref=False ):\n old,new = as_id(old),as_id(new)\n to_delete = []\n for i,n in enumerate(sequence):\n if n == old:\n if new is None:\n to_delete.append( i )\n else:\n sequence[i] = new \n if single_ref:\n new = None\n elif n == new and single_ref:\n new = None\n if to_delete:\n to_delete.reverse()\n for i in to_delete:\n del sequence[i]\n return sequence", "response": "Rewrite parents to point to new in old\n \n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a sub - set of children who are simple in the sense of group_children", "response": "def simple( child, shared, parent ):\n \"\"\"Return sub-set of children who are \"simple\" in the sense of group_children\"\"\"\n return (\n not child.get('refs',())\n and (\n not shared.get(child['address'])\n or \n shared.get(child['address']) == [parent['address']]\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngroups children of a single object into a single tree of objects with long children - lists", "response": "def group_children( index, shared, min_kids=10, stop_types=STOP_TYPES, delete_children=True ):\n \"\"\"Collect like-type children into sub-groups of objects for objects with long children-lists\n \n Only group if:\n \n * there are more than X children of type Y\n * children are \"simple\"\n * individual children have no children themselves\n * individual children have no other parents...\n \"\"\"\n to_compress = []\n \n for to_simplify in list(iterindex( index )):\n if not isinstance( to_simplify, dict ):\n continue\n for typ,kids in children_types( to_simplify, index, stop_types=stop_types ).items():\n kids = [k for k in kids if k and simple(k,shared, to_simplify)]\n if len(kids) >= min_kids:\n # we can group and compress out...\n to_compress.append( (to_simplify,typ,kids))\n \n for to_simplify,typ,kids in to_compress:\n typ_address = new_address(index)\n kid_addresses = [k['address'] for k in kids]\n index[typ_address] = {\n 'address': typ_address,\n 'type': MANY_TYPE,\n 'name': typ,\n 'size': sum( [k.get('size',0) for k in kids], 0),\n 'parents': [to_simplify['address']],\n }\n \n shared[typ_address] = index[typ_address]['parents']\n to_simplify['refs'][:] = [typ_address]\n \n if delete_children:\n for address in kid_addresses:\n try:\n del index[address]\n except KeyError, err: \n pass # already compressed out\n try:\n del shared[address]\n except KeyError, err:\n pass # already compressed out\n index[typ_address]['refs'] = []\n else:\n index[typ_address]['refs'] = kid_addresses"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef simplify_dicts( index, shared, simplify_dicts=SIMPLIFY_DICTS, always_compress=ALWAYS_COMPRESS_DICTS ):\n \n # things which will have their dictionaries compressed out\n \n to_delete = set()\n \n for to_simplify in iterindex(index):\n if to_simplify['address'] in to_delete:\n continue \n if to_simplify['type'] in simplify_dicts and not 'compressed' in to_simplify:\n refs = to_simplify['refs']\n for ref in refs:\n child = index.get( ref )\n if child is not None and child['type'] == 'dict':\n child_referrers = child['parents'][:]\n if len(child_referrers) == 1 or to_simplify['type'] in always_compress:\n \n to_simplify['compressed'] = True\n to_simplify['refs'] = child['refs']\n to_simplify['size'] += child['size']\n \n # rewrite anything *else* that was pointing to child to point to us...\n while to_simplify['address'] in child_referrers:\n child_referrers.remove( to_simplify['address'] )\n if child_referrers:\n rewrite_refs( \n child_referrers, \n child['address'],\n to_simplify['address'], \n index, single_ref=True\n )\n \n # now rewrite grandchildren to point to root obj instead of dict\n for grandchild in child['refs']:\n grandchild = index[grandchild]\n parent_set = grandchild['parents']\n if parent_set:\n rewrite_references( \n parent_set, \n child,\n to_simplify,\n single_ref = True,\n )\n assert parent_set\n to_delete.add( child['address'] )\n for item in to_delete:\n del index[item]\n del shared[item]\n \n return index", "response": "Eliminate noise dictionary records from the overall index."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find_reachable( modules, index, shared, stop_types=STOP_TYPES ):\n reachable = set()\n already_seen = set()\n for module in modules:\n for child in recurse( module, index, stop_types=stop_types, already_seen=already_seen):\n reachable.add( child['address'] )\n return reachable", "response": "Find all reachable objects from given root nodes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef deparent_unreachable( reachable, shared ):\n for id,shares in shared.iteritems():\n if id in reachable: # child is reachable\n filtered = [\n x \n for x in shares \n if x in reachable # only those parents which are reachable\n ]\n if len(filtered) != len(shares):\n shares[:] = filtered", "response": "Eliminate all parent - links from unreachable objects from shared"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef bind_parents( index, shared ):\n for v in iterindex( index ):\n v['parents'] = shared.get( v['address'], [] )", "response": "Bind parents on all items in index"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfinding appropriate root objects from which to recurse the hierarchies", "response": "def find_roots( disconnected, index, shared ):\n \"\"\"Find appropriate \"root\" objects from which to recurse the hierarchies\n \n Will generate a synthetic root for anything which doesn't have any parents...\n \"\"\"\n log.warn( '%s disconnected objects in %s total objects', len(disconnected), len(index))\n natural_roots = [x for x in disconnected if x.get('refs') and not x.get('parents')]\n log.warn( '%s objects with no parents at all' ,len(natural_roots))\n for natural_root in natural_roots:\n recurse_module(\n natural_root, index, shared\n )\n yield natural_root\n rest = [x for x in disconnected if x.get( 'totsize' ) is None]\n un_found = {\n 'type': 'module',\n 'name': '',\n 'children': rest,\n 'parents': [ ],\n 'size': 0,\n 'totsize': sum([x['size'] for x in rest],0),\n 'address': new_address( index ),\n }\n index[un_found['address']] = un_found\n yield un_found"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nretrieving the given root by type - key", "response": "def get_root( self, key ):\n \"\"\"Retrieve the given root by type-key\"\"\"\n if key not in self.roots:\n root,self.rows = load( self.filename, include_interpreter = self.include_interpreter )\n self.roots[key] = root\n return self.roots[key]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef record_action(self, method_name, *args, **kwargs):\n self.actions.append((method_name, args, kwargs))", "response": "Record the method - calling action."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef play_actions(self, target):\n for method_name, args, kwargs in self.actions:\n method = getattr(target, method_name)\n method(*args, **kwargs)", "response": "Play record actions on the target object."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef route(self, host, rule, **options):\n def decorator(func):\n endpoint = \"{func.__module__}:{func.__name__}\".format(func=func)\n self.record_action(\"add_url_rule\", host, rule, endpoint, **options)\n return func\n return decorator", "response": "The decorator to register wrapped function as the brownant app."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting the input value into bytes type.", "response": "def to_bytes_safe(text, encoding=\"utf-8\"):\n \"\"\"Convert the input value into bytes type.\n\n If the input value is string type and could be encode as UTF-8 bytes, the\n encoded value will be returned. Otherwise, the encoding has failed, the\n origin value will be returned as well.\n\n :param text: the input value which could be string or bytes.\n :param encoding: the expected encoding be used while converting the string\n input into bytes.\n :rtype: :class:`~__builtin__.bytes`\n \"\"\"\n if not isinstance(text, (bytes, text_type)):\n raise TypeError(\"must be string type\")\n\n if isinstance(text, text_type):\n return text.encode(encoding)\n\n return text"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_attr(self, obj, name):\n attr_name = self.attr_names[name]\n return getattr(obj, attr_name)", "response": "Get the attribute of the target object with the configured attribute\n name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_url_rule(self, host, rule_string, endpoint, **options):\n rule = Rule(rule_string, host=host, endpoint=endpoint, **options)\n self.url_map.add(rule)", "response": "Add a url rule to the app instance."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse_url(self, url_string):\n url = urllib.parse.urlparse(url_string)\n url = self.validate_url(url)\n url_adapter = self.url_map.bind(server_name=url.hostname,\n url_scheme=url.scheme,\n path_info=url.path)\n query_args = url_decode(url.query)\n return url, url_adapter, query_args", "response": "Parse the URL string with the url_map and return the url adapter and query_args."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef validate_url(self, url):\n # fix up the non-ascii path\n url_path = to_bytes_safe(url.path)\n url_path = urllib.parse.quote(url_path, safe=b\"/%\")\n\n # fix up the non-ascii query\n url_query = to_bytes_safe(url.query)\n url_query = urllib.parse.quote(url_query, safe=b\"?=&\")\n\n url = urllib.parse.ParseResult(url.scheme, url.netloc, url_path,\n url.params, url_query, url.fragment)\n\n # validate the components of URL\n has_hostname = url.hostname is not None and len(url.hostname) > 0\n has_http_scheme = url.scheme in (\"http\", \"https\")\n has_path = not len(url.path) or url.path.startswith(\"/\")\n\n if not (has_hostname and has_http_scheme and has_path):\n raise NotSupported(\"invalid url: %s\" % repr(url))\n\n return url", "response": "Validate the :class:`~urllib.parse.ParseResult` object.\n\n This method will make sure the :meth:`~brownant.app.BrownAnt.parse_url`\n could work as expected even meet a unexpected URL string.\n\n :param url: the parsed url.\n :type url: :class:`~urllib.parse.ParseResult`"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef dispatch_url(self, url_string):\n url, url_adapter, query_args = self.parse_url(url_string)\n\n try:\n endpoint, kwargs = url_adapter.match()\n except NotFound:\n raise NotSupported(url_string)\n except RequestRedirect as e:\n new_url = \"{0.new_url}?{1}\".format(e, url_encode(query_args))\n return self.dispatch_url(new_url)\n\n try:\n handler = import_string(endpoint)\n request = Request(url=url, args=query_args)\n return handler(request, **kwargs)\n except RequestRedirect as e:\n return self.dispatch_url(e.new_url)", "response": "Dispatch the URL string to the target endpoint function."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmount a supported site to this app instance.", "response": "def mount_site(self, site):\n \"\"\"Mount a supported site to this app instance.\n\n :param site: the site instance be mounted.\n \"\"\"\n if isinstance(site, string_types):\n site = import_string(site)\n site.play_actions(target=self)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ping(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"ping\"], *args, **kwargs)", "response": "Ping Server\n\n Respond without doing anything.\n This endpoint is used to check that the service is up.\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef githubWebHookConsumer(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"githubWebHookConsumer\"], *args, **kwargs)", "response": "Consume GitHub WebHook events"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef badge(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"badge\"], *args, **kwargs)", "response": "Badge is a simple wrapper for the badge method"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\npost a comment on a given GitHub Issue or Pull Request For a given Issue or Pull Request of a repository, this will write a new message. This method takes input: ``v1/create-comment.json#`` This method is ``experimental``", "response": "def createComment(self, *args, **kwargs):\n \"\"\"\n Post a comment on a given GitHub Issue or Pull Request\n\n For a given Issue or Pull Request of a repository, this will write a new message.\n\n This method takes input: ``v1/create-comment.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"createComment\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef main():\n print()\n print(\"-~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~-\")\n print(lorem_gotham_title().center(50))\n print(\"-~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~-\")\n print()\n poem = lorem_gotham()\n for n in range(16):\n if n in (4, 8, 12):\n print()\n print(next(poem))\n print()", "response": "I provide a command - line interface for this module\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def listWorkerTypes(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"listWorkerTypes\"], *args, **kwargs)", "response": "This method returns a list of worker types known to be managed by this client."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning an instance Request an instance of a worker type This method takes input: ``v1/run-instance-request.json#`` This method is ``experimental``", "response": "async def runInstance(self, *args, **kwargs):\n \"\"\"\n Run an instance\n\n Request an instance of a worker type\n\n This method takes input: ``v1/run-instance-request.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"runInstance\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def workerTypeStats(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"workerTypeStats\"], *args, **kwargs)", "response": "This method returns the resource stats for a workerType"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlook up the resource health for a workerType Return a view of the health of a given worker type This method gives output: ``v1/health.json#`` This method is ``experimental``", "response": "async def workerTypeHealth(self, *args, **kwargs):\n \"\"\"\n Look up the resource health for a workerType\n\n Return a view of the health of a given worker type\n\n This method gives output: ``v1/health.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"workerTypeHealth\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlooking up the most recent errors of a workerType Return a list of the most recent errors encountered by a worker type This method gives output: ``v1/errors.json#`` This method is ``experimental``", "response": "async def workerTypeErrors(self, *args, **kwargs):\n \"\"\"\n Look up the most recent errors of a workerType\n\n Return a list of the most recent errors encountered by a worker type\n\n This method gives output: ``v1/errors.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"workerTypeErrors\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def workerTypeState(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"workerTypeState\"], *args, **kwargs)", "response": "This method returns the state of a given worker type"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring a KeyPair for a given worker type exists. Idempotently ensure that a KeyPair for a given worker type exists.", "response": "async def ensureKeyPair(self, *args, **kwargs):\n \"\"\"\n Ensure a KeyPair for a given worker type exists\n\n Idempotently ensure that a keypair of a given name exists\n\n This method takes input: ``v1/create-key-pair.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"ensureKeyPair\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nremove a KeyPair from the cluster", "response": "async def removeKeyPair(self, *args, **kwargs):\n \"\"\"\n Ensure a KeyPair for a given worker type does not exist\n\n Ensure that a keypair of a given name does not exist.\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"removeKeyPair\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def terminateInstance(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"terminateInstance\"], *args, **kwargs)", "response": "Terminate an instance in a specified region"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrequesting prices for EC2 Return a list of possible prices for EC2 This method takes input: ``v1/prices-request.json#`` This method gives output: ``v1/prices.json#`` This method is ``experimental``", "response": "async def getSpecificPrices(self, *args, **kwargs):\n \"\"\"\n Request prices for EC2\n\n Return a list of possible prices for EC2\n\n This method takes input: ``v1/prices-request.json#``\n\n This method gives output: ``v1/prices.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"getSpecificPrices\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def getHealth(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getHealth\"], *args, **kwargs)", "response": "Get health of the EC2 account"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def getRecentErrors(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getRecentErrors\"], *args, **kwargs)", "response": "This method returns a list of recent errors encountered in the provisioner across all worker types"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the list of regions managed by this ec2 - manager", "response": "async def regions(self, *args, **kwargs):\n \"\"\"\n See the list of regions managed by this ec2-manager\n\n This method is only for debugging the ec2-manager\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"regions\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def amiUsage(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"amiUsage\"], *args, **kwargs)", "response": "This method returns the list of AMIs and their usage by returning a list of objects in the form of a list of strings"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsee the current EBS volume usage list Lists current EBS volume usage by returning a list of objects that are uniquely defined by {region, volumetype, state} in the form: { region: string, volumetype: string, state: string, totalcount: integer, totalgb: integer, touched: timestamp (last time that information was updated), } This method is ``experimental``", "response": "async def ebsUsage(self, *args, **kwargs):\n \"\"\"\n See the current EBS volume usage list\n\n Lists current EBS volume usage by returning a list of objects\n that are uniquely defined by {region, volumetype, state} in the form:\n {\n region: string,\n volumetype: string,\n state: string,\n totalcount: integer,\n totalgb: integer,\n touched: timestamp (last time that information was updated),\n }\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"ebsUsage\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def sqsStats(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"sqsStats\"], *args, **kwargs)", "response": "This method returns statistics on the sqs queues"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\npurge the SQS queues", "response": "async def purgeQueues(self, *args, **kwargs):\n \"\"\"\n Purge the SQS queues\n\n This method is only for debugging the ec2-manager\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"purgeQueues\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef rotating_cube(degree_change=3, frame_rate=3):\n degrees = 0\n while True:\n t1 = time.time()\n\n with Frame() as frame:\n oval_width = frame.width\n oval_height = frame.height / 3.0\n cube_height = int(oval_height * 2)\n\n (p1_x, p1_y) = ellipse_point(degrees, oval_width, oval_height)\n (p2_x, p2_y) = ellipse_point(degrees + 90, oval_width, oval_height)\n (p3_x, p3_y) = ellipse_point(degrees + 180, oval_width, oval_height)\n (p4_x, p4_y) = ellipse_point(degrees + 270, oval_width, oval_height)\n degrees = (degrees + degree_change) % 360\n\n # connect square thing at top\n frame.line(p1_x, p1_y, p2_x, p2_y)\n frame.line(p2_x, p2_y, p3_x, p3_y)\n frame.line(p3_x, p3_y, p4_x, p4_y)\n frame.line(p4_x, p4_y, p1_x, p1_y)\n\n # connect top to bottom\n frame.line(p1_x, p1_y, p1_x, p1_y + cube_height)\n frame.line(p2_x, p2_y, p2_x, p2_y + cube_height)\n frame.line(p3_x, p3_y, p3_x, p3_y + cube_height)\n frame.line(p4_x, p4_y, p4_x, p4_y + cube_height)\n\n # connect square thing at bottom\n frame.line(p1_x, p1_y + cube_height, p2_x, p2_y + cube_height)\n frame.line(p2_x, p2_y + cube_height, p3_x, p3_y + cube_height)\n frame.line(p3_x, p3_y + cube_height, p4_x, p4_y + cube_height)\n frame.line(p4_x, p4_y + cube_height, p1_x, p1_y + cube_height)\n\n elapsed = (time.time() - t1)\n time.sleep(abs(1.0 / frame_rate - elapsed))", "response": "Rotating a single cube program."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef line(self, x0, y0, x1, y1, c='*'):\n steep = abs(y1 - y0) > abs(x1 - x0)\n if steep:\n (x0, y0) = (y0, x0)\n (x1, y1) = (y1, x1)\n if x0 > x1:\n (x0, x1) = (x1, x0)\n (y0, y1) = (y1, y0)\n deltax = x1 - x0\n deltay = abs(y1 - y0)\n error = deltax / 2\n y = y0\n if y0 < y1:\n ystep = 1\n else:\n ystep = -1\n for x in range(x0, x1 - 1):\n if steep:\n self[y, x] = c\n else:\n self[x, y] = c\n error = error - deltay\n if error < 0:\n y = y + ystep\n error = error + deltax", "response": "r Draws a line in the region of the area defined by x0 y0 x1 y1."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef terminateWorkerType(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"terminateWorkerType\"], *args, **kwargs)", "response": "Terminate all resources from a worker type"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrequest prices for EC2 Return a list of possible prices for EC2 This method gives output: ``v1/prices.json#`` This method is ``experimental``", "response": "def getPrices(self, *args, **kwargs):\n \"\"\"\n Request prices for EC2\n\n Return a list of possible prices for EC2\n\n This method gives output: ``v1/prices.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"getPrices\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef allState(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"allState\"], *args, **kwargs)", "response": "This method returns the entire internal state of the ec2 - manager"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets Task Definition This end - point will return the task - definition.", "response": "def task(self, *args, **kwargs):\n \"\"\"\n Get Task Definition\n\n This end-point will return the task-definition. Notice that the task\n definition may have been modified by queue, if an optional property is\n not specified the queue may provide a default value.\n\n This method gives output: ``v1/task.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"task\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef defineTask(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"defineTask\"], *args, **kwargs)", "response": "This is only present for legacy use."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nschedules a task for execution.", "response": "def scheduleTask(self, *args, **kwargs):\n \"\"\"\n Schedule Defined Task\n\n scheduleTask will schedule a task to be executed, even if it has\n unresolved dependencies. A task would otherwise only be scheduled if\n its dependencies were resolved.\n\n This is useful if you have defined a task that depends on itself or on\n some other task that has not been resolved, but you wish the task to be\n scheduled immediately.\n\n This will announce the task as pending and workers will be allowed to\n claim it and resolve the task.\n\n **Note** this operation is **idempotent** and will not fail or complain\n if called with a `taskId` that is already scheduled, or even resolved.\n To reschedule a task previously resolved, use `rerunTask`.\n\n This method gives output: ``v1/task-status-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"scheduleTask\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrerun a previously resolved task", "response": "def rerunTask(self, *args, **kwargs):\n \"\"\"\n Rerun a Resolved Task\n\n This method _reruns_ a previously resolved task, even if it was\n _completed_. This is useful if your task completes unsuccessfully, and\n you just want to run it from scratch again. This will also reset the\n number of `retries` allowed.\n\n This method is deprecated in favour of creating a new task with the same\n task definition (but with a new taskId).\n\n Remember that `retries` in the task status counts the number of runs that\n the queue have started because the worker stopped responding, for example\n because a spot node died.\n\n **Remark** this operation is idempotent, if you try to rerun a task that\n is not either `failed` or `completed`, this operation will just return\n the current task status.\n\n This method gives output: ``v1/task-status-response.json#``\n\n This method is ``deprecated``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"rerunTask\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncancels a scheduled task", "response": "def cancelTask(self, *args, **kwargs):\n \"\"\"\n Cancel Task\n\n This method will cancel a task that is either `unscheduled`, `pending` or\n `running`. It will resolve the current run as `exception` with\n `reasonResolved` set to `canceled`. If the task isn't scheduled yet, ie.\n it doesn't have any runs, an initial run will be added and resolved as\n described above. Hence, after canceling a task, it cannot be scheduled\n with `queue.scheduleTask`, but a new run can be created with\n `queue.rerun`. These semantics is equivalent to calling\n `queue.scheduleTask` immediately followed by `queue.cancelTask`.\n\n **Remark** this operation is idempotent, if you try to cancel a task that\n isn't `unscheduled`, `pending` or `running`, this operation will just\n return the current task status.\n\n This method gives output: ``v1/task-status-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"cancelTask\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreporting Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed Report Run Failed", "response": "def reportFailed(self, *args, **kwargs):\n \"\"\"\n Report Run Failed\n\n Report a run failed, resolving the run as `failed`. Use this to resolve\n a run that failed because the task specific code behaved unexpectedly.\n For example the task exited non-zero, or didn't produce expected output.\n\n Do not use this if the task couldn't be run because if malformed\n payload, or other unexpected condition. In these cases we have a task\n exception, which should be reported with `reportException`.\n\n This method gives output: ``v1/task-status-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"reportFailed\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef reportException(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"reportException\"], *args, **kwargs)", "response": "Report a run as an exception."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef createArtifact(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"createArtifact\"], *args, **kwargs)", "response": "This method creates an artifact for a specific run of a task. This method is used by the Task s createArtifact method."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef completeArtifact(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"completeArtifact\"], *args, **kwargs)", "response": "This endpoint finalises an artifact upload and returns the response."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a list of all active provisioners Get all active provisioners. The term \"provisioner\" is taken broadly to mean anything with a provisionerId. This does not necessarily mean there is an associated service performing any provisioning activity. The response is paged. If this end-point returns a `continuationToken`, you should call the end-point again with the `continuationToken` as a query-string option. By default this end-point will list up to 1000 provisioners in a single page. You may limit this with the query-string parameter `limit`. This method gives output: ``v1/list-provisioners-response.json#`` This method is ``experimental``", "response": "def listProvisioners(self, *args, **kwargs):\n \"\"\"\n Get a list of all active provisioners\n\n Get all active provisioners.\n\n The term \"provisioner\" is taken broadly to mean anything with a provisionerId.\n This does not necessarily mean there is an associated service performing any\n provisioning activity.\n\n The response is paged. If this end-point returns a `continuationToken`, you\n should call the end-point again with the `continuationToken` as a query-string\n option. By default this end-point will list up to 1000 provisioners in a single\n page. You may limit this with the query-string parameter `limit`.\n\n This method gives output: ``v1/list-provisioners-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"listProvisioners\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef getProvisioner(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"getProvisioner\"], *args, **kwargs)", "response": "Get an active provisioning"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeclaring a provisioner. This is a simple API call to update a provisioner with some details about it.", "response": "def declareProvisioner(self, *args, **kwargs):\n \"\"\"\n Update a provisioner\n\n Declare a provisioner, supplying some details about it.\n\n `declareProvisioner` allows updating one or more properties of a provisioner as long as the required scopes are\n possessed. For example, a request to update the `aws-provisioner-v1`\n provisioner with a body `{description: 'This provisioner is great'}` would require you to have the scope\n `queue:declare-provisioner:aws-provisioner-v1#description`.\n\n The term \"provisioner\" is taken broadly to mean anything with a provisionerId.\n This does not necessarily mean there is an associated service performing any\n provisioning activity.\n\n This method takes input: ``v1/update-provisioner-request.json#``\n\n This method gives output: ``v1/provisioner-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"declareProvisioner\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget Number of Pending Tasks", "response": "def pendingTasks(self, *args, **kwargs):\n \"\"\"\n Get Number of Pending Tasks\n\n Get an approximate number of pending tasks for the given `provisionerId`\n and `workerType`.\n\n The underlying Azure Storage Queues only promises to give us an estimate.\n Furthermore, we cache the result in memory for 20 seconds. So consumers\n should be no means expect this to be an accurate number.\n It is, however, a solid estimate of the number of pending tasks.\n\n This method gives output: ``v1/pending-tasks-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"pendingTasks\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nquarantines a worker Quarantine a worker This method takes input: ``v1/quarantine-worker-request.json#`` This method gives output: ``v1/worker-response.json#`` This method is ``experimental``", "response": "def quarantineWorker(self, *args, **kwargs):\n \"\"\"\n Quarantine a worker\n\n Quarantine a worker\n\n This method takes input: ``v1/quarantine-worker-request.json#``\n\n This method gives output: ``v1/worker-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"quarantineWorker\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef declareWorker(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"declareWorker\"], *args, **kwargs)", "response": "This method allows you to declare a worker in the worker manager. It is intended to be used by the worker manager to update one or more properties of a worker."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def findTask(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"findTask\"], *args, **kwargs)", "response": "Find Indexed Task\n Find Indexed Task\n Find Indexed Task\n Find Indexed Task\n Find Indexed Task\n Find Indexed Task\nTTtl Find Indexed Task\nTTtl Find Indexed Task\n"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def listNamespaces(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"listNamespaces\"], *args, **kwargs)", "response": "This method returns the list of namespaces immediately under a given namespace."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting Tasks List the tasks immediately under a given namespace. This endpoint lists up to 1000 tasks. If more tasks are present, a `continuationToken` will be returned, which can be given in the next request. For the initial request, the payload should be an empty JSON object. **Remark**, this end-point is designed for humans browsing for tasks, not services, as that makes little sense. This method gives output: ``v1/list-tasks-response.json#`` This method is ``stable``", "response": "async def listTasks(self, *args, **kwargs):\n \"\"\"\n List Tasks\n\n List the tasks immediately under a given namespace.\n\n This endpoint\n lists up to 1000 tasks. If more tasks are present, a\n `continuationToken` will be returned, which can be given in the next\n request. For the initial request, the payload should be an empty JSON\n object.\n\n **Remark**, this end-point is designed for humans browsing for tasks, not\n services, as that makes little sense.\n\n This method gives output: ``v1/list-tasks-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"listTasks\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def insertTask(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"insertTask\"], *args, **kwargs)", "response": "Insert a task into the index."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def findArtifactFromTask(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"findArtifactFromTask\"], *args, **kwargs)", "response": "Find an artifact from a task by its index path and redirect to the artifact on the most recent run with the given name."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def allPurgeRequests(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"allPurgeRequests\"], *args, **kwargs)", "response": "All Purge Requests This endpoint returns a list of all open purge requests for the specified master and provisioner."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npurging Requests for a particular workerType", "response": "async def purgeRequests(self, *args, **kwargs):\n \"\"\"\n Open Purge Requests for a provisionerId/workerType pair\n\n List of caches that need to be purged if they are from before\n a certain time. This is safe to be used in automation from\n workers.\n\n This method gives output: ``v1/purge-cache-request-list.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"purgeRequests\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef write(self, text):\n escape_parts = re.compile('\\x01?\\x1b\\\\[([0-9;]*)m\\x02?')\n chunks = escape_parts.split(text)\n i = 0\n for chunk in chunks:\n if chunk != '':\n if i % 2 == 0:\n self.stream.write(chunk)\n else:\n c = chunk.split(';')\n r = Magic.rdisplay(c)\n self.display(**r) #see caveat 0\n self.flush()\n i += 1", "response": "Parses the text and prints proper output to the terminal\n \n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef getch(self):\n return NotImplemented\n fno = stdout.fileno()\n mode = self.termios.tcgetattr(fno)\n try:\n self.tty.setraw(fno, self.termios.TCSANOW)\n ch = self.read(1)\n finally:\n self.termios.tcsetattr(fno, self.termios.TCSANOW, mode)\n return ch", "response": "Return a single character from the terminal."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndisplays the codes using ANSI escapes", "response": "def display(self, codes=[], fg=None, bg=None):\n \"\"\"Displays the codes using ANSI escapes\n \"\"\"\n codes, fg, bg = Magic.displayformat(codes, fg, bg)\n self.stream.write(Magic.display(codes, fg, bg))\n self.flush()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmoves the term to a new location", "response": "def move(self, place, distance = 1):\n \"\"\"see doc in Term class\"\"\"\n for d in range(distance):\n self.stream.write(self._get_cap('move '+place))\n self.flush()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef clear(self, scope = 'screen'):\n if scope == 'line':\n self.clear('beginning of line')\n self.clear('end of line')\n else: self.stream.write(self._get_cap('clear '+scope))\n self.flush()", "response": "see doc in Term class"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_size(self):\n self.curses.setupterm()\n return self.curses.tigetnum('cols'), self.curses.tigetnum('lines')", "response": "return the number of columns and lines"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef display(self, codes=[], fg=None, bg=None):\n codes, fg, bg = Magic.displayformat(codes, fg, bg)\n color = 0\n for c in codes:\n try:\n f = getattr(self, '_display_' + c)\n out = f()\n if out: color |= out\n except AttributeError:\n pass\n cfg, cfgi, cbg, cbgi = self._split_attributes(\n self._get_console_info()['attributes'])\n if self.reverse_input:\n cfg, cbg = (cbg // 0x10), (cfg * 0x10)\n cfgi, cbgi = (cbgi // 0x10), (cfgi * 0x10)\n if fg != None:\n color |= self.FG[fg]\n self.real_fg = self.FG[fg]\n else: color |= cfg\n if bg != None:\n color |= self.BG[bg]\n else: color |= cbg\n color |= (cfgi | cbgi)\n fg, fgi, bg, bgi = self._split_attributes(color)\n if self.dim_output:\n # intense black\n fg = 0\n fgi = self.FG_INTENSITY\n if self.reverse_output:\n fg, bg = (bg // 0x10), (fg * 0x10)\n fgi, bgi = (bgi // 0x10), (fgi * 0x10)\n self.reverse_input = True\n if self.hidden_output:\n fg = (bg // 0x10)\n fgi = (bgi // 0x10)\n self._set_attributes(fg | fgi | bg | bgi)", "response": "Displays the codes using Windows kernel calls."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsees doc in Term class", "response": "def get_size(self):\n \"\"\"see doc in Term class\"\"\"\n attr = self._get_console_info()\n cols = attr['window']['right'] - attr['window']['left'] + 1\n lines = attr['window']['bottom'] - attr['window']['top'] + 1\n return cols, lines"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsplits the attributes into foreground background and background intensity.", "response": "def _split_attributes(self, attrs):\n \"\"\"Spilt attribute code\n \n Takes an attribute code and returns a tuple containing\n foreground (fg), foreground intensity (fgi), background (bg), and\n background intensity (bgi)\n \n Attributes can be joined using ``fg | fgi | bg | bgi``\n \"\"\"\n fg = attrs & self.FG_ALL\n fgi = attrs & self.FG_INTENSITY\n bg = attrs & self.BG_ALL\n bgi = attrs & self.BG_INTENSITY\n return fg, fgi, bg, bgi"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmoves the term to a specific location", "response": "def move(self, place, distance = 1):\n \"\"\"see doc in Term class\"\"\"\n x, y = self._get_position()\n if place == 'up':\n y -= distance\n elif place == 'down':\n for i in range(distance): print\n nx, ny = self._get_position()\n y = ny\n self.move('beginning of line')\n elif place == 'left':\n x -= distance\n elif place == 'right':\n x += distance\n elif place == 'beginning of line':\n x = 0\n elif place == 'beginning of screen':\n x = 0\n y = self._get_console_info()['window']['top']\n else:\n raise ValueError(\"invalid place to move\")\n self._set_position((x, y))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsees doc in Term class According to http://support.microsoft.com/kb/99261 the best way to clear the console is to write out empty spaces", "response": "def clear(self, scope = 'screen'):\n \"\"\"see doc in Term class\n \n According to http://support.microsoft.com/kb/99261 the best way\n to clear the console is to write out empty spaces\n \"\"\"\n #TODO: clear attributes too\n if scope == 'screen':\n bos = (0, self._get_console_info()['window']['top'])\n cols, lines = self.get_size()\n length = cols * lines\n self._clear_console(length, bos)\n self.move('beginning of screen')\n elif scope == ' beginning of line':\n pass\n elif scope == 'end of line':\n curx, cury = self._get_position()\n cols, lines = self.get_size()\n coord = (curx, cury)\n length = cols - curx\n self._clear_console(length, coord)\n elif scope == 'end of screen':\n curx, cury = self._get_position()\n coord = (curx, cury)\n cols, lines = self.get_size()\n length = (lines - cury) * cols - curx\n self._clear_console(length, coord)\n elif scope == 'line':\n curx, cury = self._get_position()\n coord = (0, cury)\n cols, lines = self.get_size()\n self._clear_console(cols, coord)\n self._set_position((curx, cury))\n elif scope == 'left':\n self.move('left')\n self.write(' ')\n elif scope == 'right':\n self.write(' ')\n self.move('left')\n else:\n raise ValueError(\"invalid scope to clear\")"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_title(self):\n #TODO: unicode support\n strbuffer = self.ctypes.create_string_buffer(1024)\n size = self.ctypes.c_short(1024)\n #unicode versions are (Get|Set)ConsolTitleW\n self.ctypes.windll.kernel32.GetConsoleTitleA(strbuffer, size)\n return strbuffer.value", "response": "Get the title of the current console entry."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the coord of the current log entry.", "response": "def _get_coord(self, coord):\n \"\"\" It's a hack, see fixcoord in pyreadline's console.py (revision \n 1289)\n \"\"\"\n x, y = coord\n return self.ctypes.c_int(y << 16 | x)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmake sure all arguments are valid", "response": "def displayformat(codes=[], fg=None, bg=None):\n \"\"\"Makes sure all arguments are valid\"\"\"\n if isinstance(codes, basestring):\n codes = [codes]\n else:\n codes = list(codes)\n for code in codes:\n if code not in Magic.DISPLAY.keys():\n raise ValueError(\"'%s' not a valid display value\" % code)\n for color in (fg, bg):\n if color != None:\n if color not in Magic.COLORS.keys():\n raise ValueError(\"'%s' not a valid color\" % color)\n return [codes, fg, bg]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread a list of codes and generates dict", "response": "def rdisplay(codes):\n \"\"\"Reads a list of codes and generates dict\n \n >>> Magic.rdisplay([])\n {}\n >>> result = Magic.rdisplay([1,2,34,46])\n >>> sorted(result.keys())\n ['bg', 'codes', 'fg']\n >>> sorted(result['codes'])\n ['bright', 'dim']\n >>> result['bg']\n 'cyan'\n >>> result['fg']\n 'blue'\n \"\"\"\n dcodes = []\n fg = bg = None\n for code in codes:\n code = int(code)\n offset = code // 10\n decimal = code % 10\n if offset == 3 and decimal in Magic.COLORS.values(): fg = decimal\n elif offset == 4 and decimal in Magic.COLORS.values(): bg = decimal\n elif code in Magic.DISPLAY.values(): dcodes.append(code)\n else: pass # drop unhandled values\n r = {}\n if len(codes): r['codes'] = [Magic.rDISPLAY[c] for c in dcodes]\n if fg != None: r['fg'] = Magic.rCOLORS[fg]\n if bg != None: r['bg'] = Magic.rCOLORS[bg]\n return r"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists Clients with the same prefix", "response": "def listClients(self, *args, **kwargs):\n \"\"\"\n List Clients\n\n Get a list of all clients. With `prefix`, only clients for which\n it is a prefix of the clientId are returned.\n\n By default this end-point will try to return up to 1000 clients in one\n request. But it **may return less, even none**.\n It may also return a `continuationToken` even though there are no more\n results. However, you can only be sure to have seen all results if you\n keep calling `listClients` with the last `continuationToken` until you\n get a result without a `continuationToken`.\n\n This method gives output: ``v1/list-clients-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"listClients\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets information about a single client", "response": "def client(self, *args, **kwargs):\n \"\"\"\n Get Client\n\n Get information about a single client.\n\n This method gives output: ``v1/get-client-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"client\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate Client Create a new client and get the `accessToken` for this client. You should store the `accessToken` from this API call as there is no other way to retrieve it. If you loose the `accessToken` you can call `resetAccessToken` to reset it, and a new `accessToken` will be returned, but you cannot retrieve the current `accessToken`. If a client with the same `clientId` already exists this operation will fail. Use `updateClient` if you wish to update an existing client. The caller's scopes must satisfy `scopes`. This method takes input: ``v1/create-client-request.json#`` This method gives output: ``v1/create-client-response.json#`` This method is ``stable``", "response": "def createClient(self, *args, **kwargs):\n \"\"\"\n Create Client\n\n Create a new client and get the `accessToken` for this client.\n You should store the `accessToken` from this API call as there is no\n other way to retrieve it.\n\n If you loose the `accessToken` you can call `resetAccessToken` to reset\n it, and a new `accessToken` will be returned, but you cannot retrieve the\n current `accessToken`.\n\n If a client with the same `clientId` already exists this operation will\n fail. Use `updateClient` if you wish to update an existing client.\n\n The caller's scopes must satisfy `scopes`.\n\n This method takes input: ``v1/create-client-request.json#``\n\n This method gives output: ``v1/create-client-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"createClient\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating Client Update an exisiting client. The `clientId` and `accessToken` cannot be updated, but `scopes` can be modified. The caller's scopes must satisfy all scopes being added to the client in the update operation. If no scopes are given in the request, the client's scopes remain unchanged This method takes input: ``v1/create-client-request.json#`` This method gives output: ``v1/get-client-response.json#`` This method is ``stable``", "response": "def updateClient(self, *args, **kwargs):\n \"\"\"\n Update Client\n\n Update an exisiting client. The `clientId` and `accessToken` cannot be\n updated, but `scopes` can be modified. The caller's scopes must\n satisfy all scopes being added to the client in the update operation.\n If no scopes are given in the request, the client's scopes remain\n unchanged\n\n This method takes input: ``v1/create-client-request.json#``\n\n This method gives output: ``v1/get-client-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"updateClient\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nenabling a client that was disabled with disableClient.", "response": "def enableClient(self, *args, **kwargs):\n \"\"\"\n Enable Client\n\n Enable a client that was disabled with `disableClient`. If the client\n is already enabled, this does nothing.\n\n This is typically used by identity providers to re-enable clients that\n had been disabled when the corresponding identity's scopes changed.\n\n This method gives output: ``v1/get-client-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"enableClient\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef disableClient(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"disableClient\"], *args, **kwargs)", "response": "Disable Client\n\n Disable a client. If the client is already disabled, this does nothing.\n\n This is typically used by identity providers to disable clients when the\n corresponding identity's scopes no longer satisfy the client's scopes.\n\n This method gives output: ``v1/get-client-response.json#``\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef deleteClient(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"deleteClient\"], *args, **kwargs)", "response": "Delete Client\n\n Delete a client, please note that any roles related to this client must\n be deleted independently.\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef listRoles(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"listRoles\"], *args, **kwargs)", "response": "This method returns a list of all roles in the system"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting Role IDs If no limit is given, the roleIds of all roles are returned. Since this list may become long, callers can use the `limit` and `continuationToken` query arguments to page through the responses. This method gives output: ``v1/list-role-ids-response.json#`` This method is ``stable``", "response": "def listRoleIds(self, *args, **kwargs):\n \"\"\"\n List Role IDs\n\n If no limit is given, the roleIds of all roles are returned. Since this\n list may become long, callers can use the `limit` and `continuationToken`\n query arguments to page through the responses.\n\n This method gives output: ``v1/list-role-ids-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"listRoleIds\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef listRoles2(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"listRoles2\"], *args, **kwargs)", "response": "This is the base API endpoint for listing the roles in the second level of the user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new role", "response": "def createRole(self, *args, **kwargs):\n \"\"\"\n Create Role\n\n Create a new role.\n\n The caller's scopes must satisfy the new role's scopes.\n\n If there already exists a role with the same `roleId` this operation\n will fail. Use `updateRole` to modify an existing role.\n\n Creation of a role that will generate an infinite expansion will result\n in an error response.\n\n This method takes input: ``v1/create-role-request.json#``\n\n This method gives output: ``v1/get-role-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"createRole\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef updateRole(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"updateRole\"], *args, **kwargs)", "response": "This is a private API call to update a role. This is a private API call."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef deleteRole(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"deleteRole\"], *args, **kwargs)", "response": "Delete Role\n\n Delete a role. This operation will succeed regardless of whether or not\n the role exists.\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef expandScopesGet(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"expandScopesGet\"], *args, **kwargs)", "response": "This is a convenience method that returns an expanded copy of the given scopeset with implied by any\n roles included."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nexpanding Scopes Return an expanded copy of the given scopeset, with scopes implied by any roles included. This method takes input: ``v1/scopeset.json#`` This method gives output: ``v1/scopeset.json#`` This method is ``stable``", "response": "def expandScopes(self, *args, **kwargs):\n \"\"\"\n Expand Scopes\n\n Return an expanded copy of the given scopeset, with scopes implied by any\n roles included.\n\n This method takes input: ``v1/scopeset.json#``\n\n This method gives output: ``v1/scopeset.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"expandScopes\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef currentScopes(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"currentScopes\"], *args, **kwargs)", "response": "Get the expanded scopes available in the request"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets Temporary Read/Write Credentials S3 Get temporary AWS credentials for `read-write` or `read-only` access to a given `bucket` and `prefix` within that bucket. The `level` parameter can be `read-write` or `read-only` and determines which type of credentials are returned. Please note that the `level` parameter is required in the scope guarding access. The bucket name must not contain `.`, as recommended by Amazon. This method can only allow access to a whitelisted set of buckets. To add a bucket to that whitelist, contact the Taskcluster team, who will add it to the appropriate IAM policy. If the bucket is in a different AWS account, you will also need to add a bucket policy allowing access from the Taskcluster account. That policy should look like this: ``` { \"Version\": \"2012-10-17\", \"Statement\": [ { \"Sid\": \"allow-taskcluster-auth-to-delegate-access\", \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::692406183521:root\" }, \"Action\": [ \"s3:ListBucket\", \"s3:GetObject\", \"s3:PutObject\", \"s3:DeleteObject\", \"s3:GetBucketLocation\" ], \"Resource\": [ \"arn:aws:s3:::\", \"arn:aws:s3:::/*\" ] } ] } ``` The credentials are set to expire after an hour, but this behavior is subject to change. Hence, you should always read the `expires` property from the response, if you intend to maintain active credentials in your application. Please note that your `prefix` may not start with slash `/`. Such a prefix is allowed on S3, but we forbid it here to discourage bad behavior. Also note that if your `prefix` doesn't end in a slash `/`, the STS credentials may allow access to unexpected keys, as S3 does not treat slashes specially. For example, a prefix of `my-folder` will allow access to `my-folder/file.txt` as expected, but also to `my-folder.txt`, which may not be intended. Finally, note that the `PutObjectAcl` call is not allowed. Passing a canned ACL other than `private` to `PutObject` is treated as a `PutObjectAcl` call, and will result in an access-denied error from AWS. This limitation is due to a security flaw in Amazon S3 which might otherwise allow indefinite access to uploaded objects. **EC2 metadata compatibility**, if the querystring parameter `?format=iam-role-compat` is given, the response will be compatible with the JSON exposed by the EC2 metadata service. This aims to ease compatibility for libraries and tools built to auto-refresh credentials. For details on the format returned by EC2 metadata service see: [EC2 User Guide](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#instance-metadata-security-credentials). This method gives output: ``v1/aws-s3-credentials-response.json#`` This method is ``stable``", "response": "def awsS3Credentials(self, *args, **kwargs):\n \"\"\"\n Get Temporary Read/Write Credentials S3\n\n Get temporary AWS credentials for `read-write` or `read-only` access to\n a given `bucket` and `prefix` within that bucket.\n The `level` parameter can be `read-write` or `read-only` and determines\n which type of credentials are returned. Please note that the `level`\n parameter is required in the scope guarding access. The bucket name must\n not contain `.`, as recommended by Amazon.\n\n This method can only allow access to a whitelisted set of buckets. To add\n a bucket to that whitelist, contact the Taskcluster team, who will add it to\n the appropriate IAM policy. If the bucket is in a different AWS account, you\n will also need to add a bucket policy allowing access from the Taskcluster\n account. That policy should look like this:\n\n ```\n {\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Sid\": \"allow-taskcluster-auth-to-delegate-access\",\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::692406183521:root\"\n },\n \"Action\": [\n \"s3:ListBucket\",\n \"s3:GetObject\",\n \"s3:PutObject\",\n \"s3:DeleteObject\",\n \"s3:GetBucketLocation\"\n ],\n \"Resource\": [\n \"arn:aws:s3:::\",\n \"arn:aws:s3:::/*\"\n ]\n }\n ]\n }\n ```\n\n The credentials are set to expire after an hour, but this behavior is\n subject to change. Hence, you should always read the `expires` property\n from the response, if you intend to maintain active credentials in your\n application.\n\n Please note that your `prefix` may not start with slash `/`. Such a prefix\n is allowed on S3, but we forbid it here to discourage bad behavior.\n\n Also note that if your `prefix` doesn't end in a slash `/`, the STS\n credentials may allow access to unexpected keys, as S3 does not treat\n slashes specially. For example, a prefix of `my-folder` will allow\n access to `my-folder/file.txt` as expected, but also to `my-folder.txt`,\n which may not be intended.\n\n Finally, note that the `PutObjectAcl` call is not allowed. Passing a canned\n ACL other than `private` to `PutObject` is treated as a `PutObjectAcl` call, and\n will result in an access-denied error from AWS. This limitation is due to a\n security flaw in Amazon S3 which might otherwise allow indefinite access to\n uploaded objects.\n\n **EC2 metadata compatibility**, if the querystring parameter\n `?format=iam-role-compat` is given, the response will be compatible\n with the JSON exposed by the EC2 metadata service. This aims to ease\n compatibility for libraries and tools built to auto-refresh credentials.\n For details on the format returned by EC2 metadata service see:\n [EC2 User Guide](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#instance-metadata-security-credentials).\n\n This method gives output: ``v1/aws-s3-credentials-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"awsS3Credentials\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef azureTables(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"azureTables\"], *args, **kwargs)", "response": "This method returns a list of all tables in an Account managed by Auth\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets Shared-Access-Signature for Azure Table Get a shared access signature (SAS) string for use with a specific Azure Table Storage table. The `level` parameter can be `read-write` or `read-only` and determines which type of credentials are returned. If level is read-write, it will create the table if it doesn't already exist. This method gives output: ``v1/azure-table-access-response.json#`` This method is ``stable``", "response": "def azureTableSAS(self, *args, **kwargs):\n \"\"\"\n Get Shared-Access-Signature for Azure Table\n\n Get a shared access signature (SAS) string for use with a specific Azure\n Table Storage table.\n\n The `level` parameter can be `read-write` or `read-only` and determines\n which type of credentials are returned. If level is read-write, it will create the\n table if it doesn't already exist.\n\n This method gives output: ``v1/azure-table-access-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"azureTableSAS\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef azureContainers(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"azureContainers\"], *args, **kwargs)", "response": "This method returns a list of all containers in an Account managed by Auth\n\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets Shared-Access-Signature for Azure Container Get a shared access signature (SAS) string for use with a specific Azure Blob Storage container. The `level` parameter can be `read-write` or `read-only` and determines which type of credentials are returned. If level is read-write, it will create the container if it doesn't already exist. This method gives output: ``v1/azure-container-response.json#`` This method is ``stable``", "response": "def azureContainerSAS(self, *args, **kwargs):\n \"\"\"\n Get Shared-Access-Signature for Azure Container\n\n Get a shared access signature (SAS) string for use with a specific Azure\n Blob Storage container.\n\n The `level` parameter can be `read-write` or `read-only` and determines\n which type of credentials are returned. If level is read-write, it will create the\n container if it doesn't already exist.\n\n This method gives output: ``v1/azure-container-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"azureContainerSAS\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sentryDSN(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"sentryDSN\"], *args, **kwargs)", "response": "Sentry DSN for a Sentry Project"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef statsumToken(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"statsumToken\"], *args, **kwargs)", "response": "This method returns a Statsum Token for Statsum Project"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting a client token for the Websocktunnel service Get a temporary token suitable for use connecting to a [websocktunnel](https://github.com/taskcluster/websocktunnel) server. The resulting token will only be accepted by servers with a matching audience value. Reaching such a server is the callers responsibility. In general, a server URL or set of URLs should be provided to the caller as configuration along with the audience value. The token is valid for a limited time (on the scale of hours). Callers should refresh it before expiration. This method gives output: ``v1/websocktunnel-token-response.json#`` This method is ``stable``", "response": "def websocktunnelToken(self, *args, **kwargs):\n \"\"\"\n Get a client token for the Websocktunnel service\n\n Get a temporary token suitable for use connecting to a\n [websocktunnel](https://github.com/taskcluster/websocktunnel) server.\n\n The resulting token will only be accepted by servers with a matching audience\n value. Reaching such a server is the callers responsibility. In general,\n a server URL or set of URLs should be provided to the caller as configuration\n along with the audience value.\n\n The token is valid for a limited time (on the scale of hours). Callers should\n refresh it before expiration.\n\n This method gives output: ``v1/websocktunnel-token-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"websocktunnelToken\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef h1(title, line=OVERLINE):\n width = utils.term.width\n printy(bold(title.center(width)).as_utf8)\n printy(bold((line * width)[:width]).as_utf8)", "response": "Prints bold text with line beneath it spanning width of terminal\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_color(color):\n if isinstance(color, basestring):\n color = grapefruit.Color.NewFromHtml(color)\n if isinstance(color, int):\n (r, g, b) = xterm256.xterm_to_rgb(color)\n elif hasattr(color, 'rgb'):\n (r, g, b) = [int(c * 255.0) for c in color.rgb]\n else:\n (r, g, b) = color\n assert isinstance(r, int) and 0 <= r <= 255\n assert isinstance(g, int) and 0 <= g <= 255\n assert isinstance(b, int) and 0 <= b <= 255\n return (r, g, b)", "response": "r Converts a color into an r g b tuple"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprint a section of the terminal", "response": "def section(title, bar=OVERLINE, strm=sys.stdout):\n \"\"\"Helper function for testing demo routines\n \"\"\"\n width = utils.term.width\n printy(bold(title.center(width)))\n printy(bold((bar * width)[:width]))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef workerTypeUpdated(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'worker-type-updated',\n 'name': 'workerTypeUpdated',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'http://schemas.taskcluster.net/aws-provisioner/v1/worker-type-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "This is a simple workerTypeUpdated message. It is a simple function that returns a message that is published to the master."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef workerTypeRemoved(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'worker-type-removed',\n 'name': 'workerTypeRemoved',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'http://schemas.taskcluster.net/aws-provisioner/v1/worker-type-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "This method is used to publish a WorkerType Removed Message to this base class. It is used to remove a WorkerType from a Taskcluster topic."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef jobs(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'jobs',\n 'name': 'jobs',\n 'routingKey': [\n {\n 'multipleWords': False,\n 'name': 'destination',\n },\n {\n 'multipleWords': False,\n 'name': 'project',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/pulse-job.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "This is an alias for the jobs method in Treeherder. It is used to create and publish a new Job Messages to Treeherder."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a set of temporary credentials for the given clientId and accessToken.", "response": "def createTemporaryCredentials(clientId, accessToken, start, expiry, scopes, name=None):\n \"\"\" Create a set of temporary credentials\n\n Callers should not apply any clock skew; clock drift is accounted for by\n auth service.\n\n clientId: the issuing clientId\n accessToken: the issuer's accessToken\n start: start time of credentials (datetime.datetime)\n expiry: expiration time of credentials, (datetime.datetime)\n scopes: list of scopes granted\n name: credential name (optional)\n\n Returns a dictionary in the form:\n { 'clientId': str, 'accessToken: str, 'certificate': str}\n \"\"\"\n\n for scope in scopes:\n if not isinstance(scope, six.string_types):\n raise exceptions.TaskclusterFailure('Scope must be string')\n\n # Credentials can only be valid for 31 days. I hope that\n # this is validated on the server somehow...\n\n if expiry - start > datetime.timedelta(days=31):\n raise exceptions.TaskclusterFailure('Only 31 days allowed')\n\n # We multiply times by 1000 because the auth service is JS and as a result\n # uses milliseconds instead of seconds\n cert = dict(\n version=1,\n scopes=scopes,\n start=calendar.timegm(start.utctimetuple()) * 1000,\n expiry=calendar.timegm(expiry.utctimetuple()) * 1000,\n seed=utils.slugId().encode('ascii') + utils.slugId().encode('ascii'),\n )\n\n # if this is a named temporary credential, include the issuer in the certificate\n if name:\n cert['issuer'] = utils.toStr(clientId)\n\n sig = ['version:' + utils.toStr(cert['version'])]\n if name:\n sig.extend([\n 'clientId:' + utils.toStr(name),\n 'issuer:' + utils.toStr(clientId),\n ])\n sig.extend([\n 'seed:' + utils.toStr(cert['seed']),\n 'start:' + utils.toStr(cert['start']),\n 'expiry:' + utils.toStr(cert['expiry']),\n 'scopes:'\n ] + scopes)\n sigStr = '\\n'.join(sig).encode()\n\n if isinstance(accessToken, six.text_type):\n accessToken = accessToken.encode()\n sig = hmac.new(accessToken, sigStr, hashlib.sha256).digest()\n\n cert['signature'] = utils.encodeStringForB64Header(sig)\n\n newToken = hmac.new(accessToken, cert['seed'], hashlib.sha256).digest()\n newToken = utils.makeB64UrlSafe(utils.encodeStringForB64Header(newToken)).replace(b'=', b'')\n\n return {\n 'clientId': name or clientId,\n 'accessToken': newToken,\n 'certificate': utils.dumpJson(cert),\n }"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmakes an ext for Hawk authentication", "response": "def makeHawkExt(self):\n \"\"\" Make an 'ext' for Hawk authentication \"\"\"\n o = self.options\n c = o.get('credentials', {})\n if c.get('clientId') and c.get('accessToken'):\n ext = {}\n cert = c.get('certificate')\n if cert:\n if six.PY3 and isinstance(cert, six.binary_type):\n cert = cert.decode()\n if isinstance(cert, six.string_types):\n cert = json.loads(cert)\n ext['certificate'] = cert\n\n if 'authorizedScopes' in o:\n ext['authorizedScopes'] = o['authorizedScopes']\n\n # .encode('base64') inserts a newline, which hawk doesn't\n # like but doesn't strip itself\n return utils.makeB64UrlSafe(utils.encodeStringForB64Header(utils.dumpJson(ext)).strip())\n else:\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbuilding a signed URL.", "response": "def buildSignedUrl(self, methodName, *args, **kwargs):\n \"\"\" Build a signed URL. This URL contains the credentials needed to access\n a resource.\"\"\"\n\n if 'expiration' in kwargs:\n expiration = kwargs['expiration']\n del kwargs['expiration']\n else:\n expiration = self.options['signedUrlExpiration']\n\n expiration = int(time.time() + expiration) # Mainly so that we throw if it's not a number\n\n requestUrl = self.buildUrl(methodName, *args, **kwargs)\n\n if not self._hasCredentials():\n raise exceptions.TaskclusterAuthFailure('Invalid Hawk Credentials')\n\n clientId = utils.toStr(self.options['credentials']['clientId'])\n accessToken = utils.toStr(self.options['credentials']['accessToken'])\n\n def genBewit():\n # We need to fix the output of get_bewit. It returns a url-safe base64\n # encoded string, which contains a list of tokens separated by '\\'.\n # The first one is the clientId, the second is an int, the third is\n # url-safe base64 encoded MAC, the fourth is the ext param.\n # The problem is that the nested url-safe base64 encoded MAC must be\n # base64 (i.e. not url safe) or server-side will complain.\n\n # id + '\\\\' + exp + '\\\\' + mac + '\\\\' + options.ext;\n resource = mohawk.base.Resource(\n credentials={\n 'id': clientId,\n 'key': accessToken,\n 'algorithm': 'sha256',\n },\n method='GET',\n ext=utils.toStr(self.makeHawkExt()),\n url=requestUrl,\n timestamp=expiration,\n nonce='',\n # content='',\n # content_type='',\n )\n bewit = mohawk.bewit.get_bewit(resource)\n return bewit.rstrip('=')\n\n bewit = genBewit()\n\n if not bewit:\n raise exceptions.TaskclusterFailure('Did not receive a bewit')\n\n u = urllib.parse.urlparse(requestUrl)\n\n qs = u.query\n if qs:\n qs += '&'\n qs += 'bewit=%s' % bewit\n\n return urllib.parse.urlunparse((\n u.scheme,\n u.netloc,\n u.path,\n u.params,\n qs,\n u.fragment,\n ))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconstructing a URL for the given route on this service based on the rootUrl", "response": "def _constructUrl(self, route):\n \"\"\"Construct a URL for the given route on this service, based on the\n rootUrl\"\"\"\n return liburls.api(\n self.options['rootUrl'],\n self.serviceName,\n self.apiVersion,\n route.rstrip('/'))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _processArgs(self, entry, *_args, **_kwargs):\n\n # We need the args to be a list so we can mutate them\n args = list(_args)\n kwargs = copy.deepcopy(_kwargs)\n\n reqArgs = entry['args']\n routeParams = {}\n\n query = {}\n payload = None\n kwApiArgs = {}\n\n paginationHandler = None\n paginationLimit = None\n\n # There are three formats for calling methods:\n # 1. method(v1, v1, payload)\n # 2. method(payload, k1=v1, k2=v2)\n # 3. method(payload=payload, query=query, params={k1: v1, k2: v2})\n if len(kwargs) == 0:\n if 'input' in entry and len(args) == len(reqArgs) + 1:\n payload = args.pop()\n if len(args) != len(reqArgs):\n log.debug(args)\n log.debug(reqArgs)\n raise exceptions.TaskclusterFailure('Incorrect number of positional arguments')\n log.debug('Using method(v1, v2, payload) calling convention')\n else:\n # We're considering kwargs which are the api route parameters to be\n # called 'flat' because they're top level keys. We're special\n # casing calls which have only api-arg kwargs and possibly a payload\n # value and handling them directly.\n isFlatKwargs = True\n if len(kwargs) == len(reqArgs):\n for arg in reqArgs:\n if not kwargs.get(arg, False):\n isFlatKwargs = False\n break\n if 'input' in entry and len(args) != 1:\n isFlatKwargs = False\n if 'input' not in entry and len(args) != 0:\n isFlatKwargs = False\n else:\n pass # We're using payload=, query= and param=\n else:\n isFlatKwargs = False\n\n # Now we're going to handle the two types of kwargs. The first is\n # 'flat' ones, which are where the api params\n if isFlatKwargs:\n if 'input' in entry:\n payload = args.pop()\n kwApiArgs = kwargs\n log.debug('Using method(payload, k1=v1, k2=v2) calling convention')\n warnings.warn(\n \"The method(payload, k1=v1, k2=v2) calling convention will soon be deprecated\",\n PendingDeprecationWarning\n )\n else:\n kwApiArgs = kwargs.get('params', {})\n payload = kwargs.get('payload', None)\n query = kwargs.get('query', {})\n paginationHandler = kwargs.get('paginationHandler', None)\n paginationLimit = kwargs.get('paginationLimit', None)\n log.debug('Using method(payload=payload, query=query, params={k1: v1, k2: v2}) calling convention')\n\n if 'input' in entry and isinstance(payload, type(None)):\n raise exceptions.TaskclusterFailure('Payload is required')\n\n # These all need to be rendered down to a string, let's just check that\n # they are up front and fail fast\n for arg in args:\n if not isinstance(arg, six.string_types) and not isinstance(arg, int):\n raise exceptions.TaskclusterFailure(\n 'Positional arg \"%s\" to %s is not a string or int' % (arg, entry['name']))\n\n for name, arg in six.iteritems(kwApiArgs):\n if not isinstance(arg, six.string_types) and not isinstance(arg, int):\n raise exceptions.TaskclusterFailure(\n 'KW arg \"%s: %s\" to %s is not a string or int' % (name, arg, entry['name']))\n\n if len(args) > 0 and len(kwApiArgs) > 0:\n raise exceptions.TaskclusterFailure('Specify either positional or key word arguments')\n\n # We know for sure that if we don't give enough arguments that the call\n # should fail. We don't yet know if we should fail because of two many\n # arguments because we might be overwriting positional ones with kw ones\n if len(reqArgs) > len(args) + len(kwApiArgs):\n raise exceptions.TaskclusterFailure(\n '%s takes %d args, only %d were given' % (\n entry['name'], len(reqArgs), len(args) + len(kwApiArgs)))\n\n # We also need to error out when we have more positional args than required\n # because we'll need to go through the lists of provided and required args\n # at the same time. Not disqualifying early means we'll get IndexErrors if\n # there are more positional arguments than required\n if len(args) > len(reqArgs):\n raise exceptions.TaskclusterFailure('%s called with too many positional args',\n entry['name'])\n\n i = 0\n for arg in args:\n log.debug('Found a positional argument: %s', arg)\n routeParams[reqArgs[i]] = arg\n i += 1\n\n log.debug('After processing positional arguments, we have: %s', routeParams)\n\n routeParams.update(kwApiArgs)\n\n log.debug('After keyword arguments, we have: %s', routeParams)\n\n if len(reqArgs) != len(routeParams):\n errMsg = '%s takes %s args, %s given' % (\n entry['name'],\n ','.join(reqArgs),\n routeParams.keys())\n log.error(errMsg)\n raise exceptions.TaskclusterFailure(errMsg)\n\n for reqArg in reqArgs:\n if reqArg not in routeParams:\n errMsg = '%s requires a \"%s\" argument which was not provided' % (\n entry['name'], reqArg)\n log.error(errMsg)\n raise exceptions.TaskclusterFailure(errMsg)\n\n return routeParams, payload, query, paginationHandler, paginationLimit", "response": "This method takes a single entry and returns what the internal state of the object is."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngiving a route like \"/task/ artifacts and a mapping like {\"taskId\": 12345 } return a string like \"/task/ artifacts", "response": "def _subArgsInRoute(self, entry, args):\n \"\"\" Given a route like \"/task//artifacts\" and a mapping like\n {\"taskId\": \"12345\"}, return a string like \"/task/12345/artifacts\"\n \"\"\"\n\n route = entry['route']\n\n for arg, val in six.iteritems(args):\n toReplace = \"<%s>\" % arg\n if toReplace not in route:\n raise exceptions.TaskclusterFailure(\n 'Arg %s not found in route for %s' % (arg, entry['name']))\n val = urllib.parse.quote(str(val).encode(\"utf-8\"), '')\n route = route.replace(\"<%s>\" % arg, val)\n\n return route.lstrip('/')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _hasCredentials(self):\n cred = self.options.get('credentials')\n return (\n cred and\n 'clientId' in cred and\n 'accessToken' in cred and\n cred['clientId'] and\n cred['accessToken']\n )", "response": "Return True if credentials is given"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _makeHttpRequest(self, method, route, payload):\n\n url = self._constructUrl(route)\n log.debug('Full URL used is: %s', url)\n\n hawkExt = self.makeHawkExt()\n\n # Serialize payload if given\n if payload is not None:\n payload = utils.dumpJson(payload)\n\n # Do a loop of retries\n retry = -1 # we plus first in the loop, and attempt 1 is retry 0\n retries = self.options['maxRetries']\n while retry < retries:\n retry += 1\n # if this isn't the first retry then we sleep\n if retry > 0:\n time.sleep(utils.calculateSleepTime(retry))\n # Construct header\n if self._hasCredentials():\n sender = mohawk.Sender(\n credentials={\n 'id': self.options['credentials']['clientId'],\n 'key': self.options['credentials']['accessToken'],\n 'algorithm': 'sha256',\n },\n ext=hawkExt if hawkExt else {},\n url=url,\n content=payload if payload else '',\n content_type='application/json' if payload else '',\n method=method,\n )\n\n headers = {'Authorization': sender.request_header}\n else:\n log.debug('Not using hawk!')\n headers = {}\n if payload:\n # Set header for JSON if payload is given, note that we serialize\n # outside this loop.\n headers['Content-Type'] = 'application/json'\n\n log.debug('Making attempt %d', retry)\n try:\n response = utils.makeSingleHttpRequest(method, url, payload, headers)\n except requests.exceptions.RequestException as rerr:\n if retry < retries:\n log.warn('Retrying because of: %s' % rerr)\n continue\n # raise a connection exception\n raise exceptions.TaskclusterConnectionError(\n \"Failed to establish connection\",\n superExc=rerr\n )\n\n # Handle non 2xx status code and retry if possible\n status = response.status_code\n if status == 204:\n return None\n\n # Catch retryable errors and go to the beginning of the loop\n # to do the retry\n if 500 <= status and status < 600 and retry < retries:\n log.warn('Retrying because of a %s status code' % status)\n continue\n\n # Throw errors for non-retryable errors\n if status < 200 or status >= 300:\n data = {}\n try:\n data = response.json()\n except Exception:\n pass # Ignore JSON errors in error messages\n # Find error message\n message = \"Unknown Server Error\"\n if isinstance(data, dict):\n message = data.get('message')\n else:\n if status == 401:\n message = \"Authentication Error\"\n elif status == 500:\n message = \"Internal Server Error\"\n # Raise TaskclusterAuthFailure if this is an auth issue\n if status == 401:\n raise exceptions.TaskclusterAuthFailure(\n message,\n status_code=status,\n body=data,\n superExc=None\n )\n # Raise TaskclusterRestFailure for all other issues\n raise exceptions.TaskclusterRestFailure(\n message,\n status_code=status,\n body=data,\n superExc=None\n )\n\n # Try to load JSON\n try:\n return response.json()\n except ValueError:\n return {\"response\": response}\n\n # This code-path should be unreachable\n assert False, \"Error from last retry should have been raised!\"", "response": "This method is used to make an HTTP request for the Taskcluster API endpoint. It will attempt to make a single HTTP request."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def _makeHttpRequest(self, method, route, payload):\n\n url = self._constructUrl(route)\n log.debug('Full URL used is: %s', url)\n\n hawkExt = self.makeHawkExt()\n\n # Serialize payload if given\n if payload is not None:\n payload = utils.dumpJson(payload)\n\n # Do a loop of retries\n retry = -1 # we plus first in the loop, and attempt 1 is retry 0\n retries = self.options['maxRetries']\n while retry < retries:\n retry += 1\n # if this isn't the first retry then we sleep\n if retry > 0:\n snooze = float(retry * retry) / 10.0\n log.info('Sleeping %0.2f seconds for exponential backoff', snooze)\n await asyncio.sleep(utils.calculateSleepTime(retry))\n # Construct header\n if self._hasCredentials():\n sender = mohawk.Sender(\n credentials={\n 'id': self.options['credentials']['clientId'],\n 'key': self.options['credentials']['accessToken'],\n 'algorithm': 'sha256',\n },\n ext=hawkExt if hawkExt else {},\n url=url,\n content=payload if payload else '',\n content_type='application/json' if payload else '',\n method=method,\n )\n\n headers = {'Authorization': sender.request_header}\n else:\n log.debug('Not using hawk!')\n headers = {}\n if payload:\n # Set header for JSON if payload is given, note that we serialize\n # outside this loop.\n headers['Content-Type'] = 'application/json'\n\n log.debug('Making attempt %d', retry)\n try:\n response = await asyncutils.makeSingleHttpRequest(\n method, url, payload, headers, session=self.session\n )\n except aiohttp.ClientError as rerr:\n if retry < retries:\n log.warn('Retrying because of: %s' % rerr)\n continue\n # raise a connection exception\n raise exceptions.TaskclusterConnectionError(\n \"Failed to establish connection\",\n superExc=rerr\n )\n\n status = response.status\n if status == 204:\n return None\n\n # Catch retryable errors and go to the beginning of the loop\n # to do the retry\n if 500 <= status and status < 600 and retry < retries:\n log.warn('Retrying because of a %s status code' % status)\n continue\n\n # Throw errors for non-retryable errors\n if status < 200 or status >= 300:\n # Parse messages from errors\n data = {}\n try:\n data = await response.json()\n except Exception:\n pass # Ignore JSON errors in error messages\n # Find error message\n message = \"Unknown Server Error\"\n if isinstance(data, dict):\n message = data.get('message')\n else:\n if status == 401:\n message = \"Authentication Error\"\n elif status == 500:\n message = \"Internal Server Error\"\n else:\n message = \"Unknown Server Error %s\\n%s\" % (str(status), str(data)[:1024])\n # Raise TaskclusterAuthFailure if this is an auth issue\n if status == 401:\n raise exceptions.TaskclusterAuthFailure(\n message,\n status_code=status,\n body=data,\n superExc=None\n )\n # Raise TaskclusterRestFailure for all other issues\n raise exceptions.TaskclusterRestFailure(\n message,\n status_code=status,\n body=data,\n superExc=None\n )\n\n # Try to load JSON\n try:\n await response.release()\n return await response.json()\n except (ValueError, aiohttp.client_exceptions.ContentTypeError):\n return {\"response\": response}\n\n # This code-path should be unreachable\n assert False, \"Error from last retry should have been raised!\"", "response": "This method is used to make an HTTP request for the API endpoint. It will attempt to make a new HTTP request."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists worker types with details Return a list of worker types, including some summary information about current capacity for each. While this list includes all defined worker types, there may be running EC2 instances for deleted worker types that are not included here. The list is unordered. This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/list-worker-types-summaries-response.json#`` This method is ``stable``", "response": "def listWorkerTypeSummaries(self, *args, **kwargs):\n \"\"\"\n List worker types with details\n\n Return a list of worker types, including some summary information about\n current capacity for each. While this list includes all defined worker types,\n there may be running EC2 instances for deleted worker types that are not\n included here. The list is unordered.\n\n This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/list-worker-types-summaries-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"listWorkerTypeSummaries\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting Worker Type Last Modified Time This method is provided to allow workers to see when they were last modified. The value provided through UserData can be compared against this value to see if changes have been made If the worker type definition has not been changed, the date should be identical as it is the same stored value. This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/get-worker-type-last-modified.json#`` This method is ``stable``", "response": "def workerTypeLastModified(self, *args, **kwargs):\n \"\"\"\n Get Worker Type Last Modified Time\n\n This method is provided to allow workers to see when they were\n last modified. The value provided through UserData can be\n compared against this value to see if changes have been made\n If the worker type definition has not been changed, the date\n should be identical as it is the same stored value.\n\n This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/get-worker-type-last-modified.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"workerTypeLastModified\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef removeWorkerType(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"removeWorkerType\"], *args, **kwargs)", "response": "This method deletes a worker type definition from the storage table. This method is only applicable to background workers."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getSecret(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"getSecret\"], *args, **kwargs)", "response": "Get a Secret from storage"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef instanceStarted(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"instanceStarted\"], *args, **kwargs)", "response": "Report an instance starting by giving its id as well\n as its security token"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting AWS State for a given worker type", "response": "def state(self, *args, **kwargs):\n \"\"\"\n Get AWS State for a worker type\n\n Return the state of a given workertype as stored by the provisioner.\n This state is stored as three lists: 1 for running instances, 1 for\n pending requests. The `summary` property contains an updated summary\n similar to that returned from `listWorkerTypeSummaries`.\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"state\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def set(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"set\"], *args, **kwargs)", "response": "Set Secret\n\n Set the secret associated with some key. If the secret already exists, it is\n updated instead.\n\n This method takes input: ``v1/secret.json#``\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete a secret from a key set", "response": "async def remove(self, *args, **kwargs):\n \"\"\"\n Delete Secret\n\n Delete the secret associated with some key.\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"remove\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a specific key from the secret store", "response": "async def get(self, *args, **kwargs):\n \"\"\"\n Read Secret\n\n Read the secret associated with some key. If the secret has recently\n expired, the response code 410 is returned. If the caller lacks the\n scope necessary to get the secret, the call will fail with a 403 code\n regardless of whether the secret exists.\n\n This method gives output: ``v1/secret.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"get\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting input from the command line and validates it.", "response": "def input_object(prompt_text, cast = None, default = None,\n prompt_ext = ': ', castarg = [], castkwarg = {}):\n \"\"\"Gets input from the command line and validates it.\n \n prompt_text\n A string. Used to prompt the user. Do not include a trailing\n space.\n \n prompt_ext\n Added on to the prompt at the end. At the moment this must not\n include any control stuff because it is send directly to\n raw_input\n \n cast\n This can be any callable object (class, function, type, etc). It\n simply calls the cast with the given arguements and returns the \n result. If a ValueError is raised, it\n will output an error message and prompt the user again.\n\n Because some builtin python objects don't do casting in the way\n that we might like you can easily write a wrapper function that\n looks and the input and returns the appropriate object or exception.\n Look in the cast submodule for examples.\n \n If cast is None, then it will do nothing (and you will have a string)\n \n default\n function returns this value if the user types nothing in. This is\n can be used to cancel the input so-to-speek\n \n castarg, castkwarg\n list and dictionary. Extra arguments passed on to the cast.\n \"\"\"\n while True:\n stdout.write(prompt_text)\n value = stdout.raw_input(prompt_ext)\n if value == '': return default\n try:\n if cast != None: value = cast(value, *castarg, **castkwarg)\n except ValueError as details:\n if cast in NICE_INPUT_ERRORS: # see comment above this constant\n stderr.write(ERROR_MESSAGE % (NICE_INPUT_ERRORS[cast] % details))\n else: stderr.write(ERROR_MESSAGE % (DEFAULT_INPUT_ERRORS % str(details)))\n continue\n return value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef query(question, values, default=None, list_values = False, ignorecase = True ):\n values = list(values)\n for i in range(len(values)):\n if not isinstance(values[i], dict):\n values[i] = {'values': [values[i]]}\n try:\n import readline, rlcomplete\n wordlist = [ str(v) for value in values\n for v in value['values']]\n completer = rlcomplete.ListCompleter(wordlist, ignorecase)\n readline.parse_and_bind(\"tab: complete\")\n readline.set_completer(completer.complete)\n except ImportError:\n pass\n valuelist = []\n for item in values:\n entry = ( display('bright', item.get('fg'), item.get('bg')) +\n str(item['values'][0]) + display(['default']) )\n if str(item['values'][0]) == str(default): entry = '['+entry+']'\n if list_values: entry += ' : ' + item['desc']\n valuelist.append(entry)\n if list_values: question += os.linesep + os.linesep.join(valuelist) + os.linesep\n else: question += ' (' + '/'.join(valuelist) + ')'\n return input_object(question, cast = query_cast, default=default,\n castarg=[values,ignorecase])", "response": "This function is used to query the user for a specific language of the language of the user."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getHookStatus(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"getHookStatus\"], *args, **kwargs)", "response": "This endpoint returns the current status of the hook."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a hook This endpoint will create a new hook. The caller's credentials must include the role that will be used to create the task. That role must satisfy task.scopes as well as the necessary scopes to add the task to the queue. This method takes input: ``v1/create-hook-request.json#`` This method gives output: ``v1/hook-definition.json#`` This method is ``stable``", "response": "def createHook(self, *args, **kwargs):\n \"\"\"\n Create a hook\n\n This endpoint will create a new hook.\n\n The caller's credentials must include the role that will be used to\n create the task. That role must satisfy task.scopes as well as the\n necessary scopes to add the task to the queue.\n\n This method takes input: ``v1/create-hook-request.json#``\n\n This method gives output: ``v1/hook-definition.json#``\n\n This method is ``stable``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"createHook\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef listLastFires(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"listLastFires\"], *args, **kwargs)", "response": "This endpoint returns information about the last few times this hook has been fired"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntask Defined Messages When a task is created or just defined a message is posted to this exchange. This message exchange is mainly useful when tasks are scheduled by a scheduler that uses `defineTask` as this does not make the task `pending`. Thus, no `taskPending` message is published. Please, note that messages are also published on this exchange if defined using `createTask`. This exchange outputs: ``v1/task-defined-message.json#``This exchange takes the following keys: * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required) * taskId: `taskId` for the task this message concerns (required) * runId: `runId` of latest run for the task, `_` if no run is exists for the task. * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. * provisionerId: `provisionerId` this task is targeted at. (required) * workerType: `workerType` this task must run on. (required) * schedulerId: `schedulerId` this task was created by. (required) * taskGroupId: `taskGroupId` this task was created in. (required) * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.", "response": "def taskDefined(self, *args, **kwargs):\n \"\"\"\n Task Defined Messages\n\n When a task is created or just defined a message is posted to this\n exchange.\n\n This message exchange is mainly useful when tasks are scheduled by a\n scheduler that uses `defineTask` as this does not make the task\n `pending`. Thus, no `taskPending` message is published.\n Please, note that messages are also published on this exchange if defined\n using `createTask`.\n\n This exchange outputs: ``v1/task-defined-message.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskId: `taskId` for the task this message concerns (required)\n\n * runId: `runId` of latest run for the task, `_` if no run is exists for the task.\n\n * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task.\n\n * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task.\n\n * provisionerId: `provisionerId` this task is targeted at. (required)\n\n * workerType: `workerType` this task must run on. (required)\n\n * schedulerId: `schedulerId` this task was created by. (required)\n\n * taskGroupId: `taskGroupId` this task was created in. (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-defined',\n 'name': 'taskDefined',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-defined-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntask Pending Messages When a task becomes `pending` a message is posted to this exchange. This is useful for workers who doesn't want to constantly poll the queue for new tasks. The queue will also be authority for task states and claims. But using this exchange workers should be able to distribute work efficiently and they would be able to reduce their polling interval significantly without affecting general responsiveness. This exchange outputs: ``v1/task-pending-message.json#``This exchange takes the following keys: * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required) * taskId: `taskId` for the task this message concerns (required) * runId: `runId` of latest run for the task, `_` if no run is exists for the task. (required) * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. * provisionerId: `provisionerId` this task is targeted at. (required) * workerType: `workerType` this task must run on. (required) * schedulerId: `schedulerId` this task was created by. (required) * taskGroupId: `taskGroupId` this task was created in. (required) * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.", "response": "def taskPending(self, *args, **kwargs):\n \"\"\"\n Task Pending Messages\n\n When a task becomes `pending` a message is posted to this exchange.\n\n This is useful for workers who doesn't want to constantly poll the queue\n for new tasks. The queue will also be authority for task states and\n claims. But using this exchange workers should be able to distribute work\n efficiently and they would be able to reduce their polling interval\n significantly without affecting general responsiveness.\n\n This exchange outputs: ``v1/task-pending-message.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskId: `taskId` for the task this message concerns (required)\n\n * runId: `runId` of latest run for the task, `_` if no run is exists for the task. (required)\n\n * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task.\n\n * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task.\n\n * provisionerId: `provisionerId` this task is targeted at. (required)\n\n * workerType: `workerType` this task must run on. (required)\n\n * schedulerId: `schedulerId` this task was created by. (required)\n\n * taskGroupId: `taskGroupId` this task was created in. (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-pending',\n 'name': 'taskPending',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-pending-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef taskRunning(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'task-running',\n 'name': 'taskRunning',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-running-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "This method is used to create the Task Running Messages in the base class. It is called by the worker to create the task running message."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef artifactCreated(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'artifact-created',\n 'name': 'artifactCreated',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/artifact-created-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "The createArtifact method is called when the queue is created. This method is called when the queue is created and the message is published to the queue."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntask Completed Messages When a task is successfully completed by a worker a message is posted this exchange. This message is routed using the `runId`, `workerGroup` and `workerId` that completed the task. But information about additional runs is also available from the task status structure. This exchange outputs: ``v1/task-completed-message.json#``This exchange takes the following keys: * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required) * taskId: `taskId` for the task this message concerns (required) * runId: `runId` of latest run for the task, `_` if no run is exists for the task. (required) * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. (required) * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. (required) * provisionerId: `provisionerId` this task is targeted at. (required) * workerType: `workerType` this task must run on. (required) * schedulerId: `schedulerId` this task was created by. (required) * taskGroupId: `taskGroupId` this task was created in. (required) * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.", "response": "def taskCompleted(self, *args, **kwargs):\n \"\"\"\n Task Completed Messages\n\n When a task is successfully completed by a worker a message is posted\n this exchange.\n This message is routed using the `runId`, `workerGroup` and `workerId`\n that completed the task. But information about additional runs is also\n available from the task status structure.\n\n This exchange outputs: ``v1/task-completed-message.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskId: `taskId` for the task this message concerns (required)\n\n * runId: `runId` of latest run for the task, `_` if no run is exists for the task. (required)\n\n * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. (required)\n\n * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. (required)\n\n * provisionerId: `provisionerId` this task is targeted at. (required)\n\n * workerType: `workerType` this task must run on. (required)\n\n * schedulerId: `schedulerId` this task was created by. (required)\n\n * taskGroupId: `taskGroupId` this task was created in. (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-completed',\n 'name': 'taskCompleted',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-completed-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ntasks Failed Messages When a task ran, but failed to complete successfully a message is posted to this exchange. This is same as worker ran task-specific code, but the task specific code exited non-zero. This exchange outputs: ``v1/task-failed-message.json#``This exchange takes the following keys: * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required) * taskId: `taskId` for the task this message concerns (required) * runId: `runId` of latest run for the task, `_` if no run is exists for the task. * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. * provisionerId: `provisionerId` this task is targeted at. (required) * workerType: `workerType` this task must run on. (required) * schedulerId: `schedulerId` this task was created by. (required) * taskGroupId: `taskGroupId` this task was created in. (required) * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.", "response": "def taskFailed(self, *args, **kwargs):\n \"\"\"\n Task Failed Messages\n\n When a task ran, but failed to complete successfully a message is posted\n to this exchange. This is same as worker ran task-specific code, but the\n task specific code exited non-zero.\n\n This exchange outputs: ``v1/task-failed-message.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskId: `taskId` for the task this message concerns (required)\n\n * runId: `runId` of latest run for the task, `_` if no run is exists for the task.\n\n * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task.\n\n * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task.\n\n * provisionerId: `provisionerId` this task is targeted at. (required)\n\n * workerType: `workerType` this task must run on. (required)\n\n * schedulerId: `schedulerId` this task was created by. (required)\n\n * taskGroupId: `taskGroupId` this task was created in. (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-failed',\n 'name': 'taskFailed',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-failed-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntask Exception Messages Whenever Taskcluster fails to run a message is posted to this exchange. This happens if the task isn't completed before its `deadl\u00ecne`, all retries failed (i.e. workers stopped responding), the task was canceled by another entity, or the task carried a malformed payload. The specific _reason_ is evident from that task status structure, refer to the `reasonResolved` property for the last run. This exchange outputs: ``v1/task-exception-message.json#``This exchange takes the following keys: * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required) * taskId: `taskId` for the task this message concerns (required) * runId: `runId` of latest run for the task, `_` if no run is exists for the task. * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task. * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task. * provisionerId: `provisionerId` this task is targeted at. (required) * workerType: `workerType` this task must run on. (required) * schedulerId: `schedulerId` this task was created by. (required) * taskGroupId: `taskGroupId` this task was created in. (required) * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.", "response": "def taskException(self, *args, **kwargs):\n \"\"\"\n Task Exception Messages\n\n Whenever Taskcluster fails to run a message is posted to this exchange.\n This happens if the task isn't completed before its `deadl\u00ecne`,\n all retries failed (i.e. workers stopped responding), the task was\n canceled by another entity, or the task carried a malformed payload.\n\n The specific _reason_ is evident from that task status structure, refer\n to the `reasonResolved` property for the last run.\n\n This exchange outputs: ``v1/task-exception-message.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskId: `taskId` for the task this message concerns (required)\n\n * runId: `runId` of latest run for the task, `_` if no run is exists for the task.\n\n * workerGroup: `workerGroup` of latest run for the task, `_` if no run is exists for the task.\n\n * workerId: `workerId` of latest run for the task, `_` if no run is exists for the task.\n\n * provisionerId: `provisionerId` this task is targeted at. (required)\n\n * workerType: `workerType` this task must run on. (required)\n\n * schedulerId: `schedulerId` this task was created by. (required)\n\n * taskGroupId: `taskGroupId` this task was created in. (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-exception',\n 'name': 'taskException',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskId',\n },\n {\n 'multipleWords': False,\n 'name': 'runId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerGroup',\n },\n {\n 'multipleWords': False,\n 'name': 'workerId',\n },\n {\n 'multipleWords': False,\n 'name': 'provisionerId',\n },\n {\n 'multipleWords': False,\n 'name': 'workerType',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-exception-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntask Group Resolved Messages", "response": "def taskGroupResolved(self, *args, **kwargs):\n \"\"\"\n Task Group Resolved Messages\n\n A message is published on task-group-resolved whenever all submitted\n tasks (whether scheduled or unscheduled) for a given task group have\n been resolved, regardless of whether they resolved as successful or\n not. A task group may be resolved multiple times, since new tasks may\n be submitted against an already resolved task group.\n\n This exchange outputs: ``v1/task-group-resolved.json#``This exchange takes the following keys:\n\n * routingKeyKind: Identifier for the routing-key kind. This is always `'primary'` for the formalized routing key. (required)\n\n * taskGroupId: `taskGroupId` for the task-group this message concerns (required)\n\n * schedulerId: `schedulerId` for the task-group this message concerns (required)\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified.\n \"\"\"\n\n ref = {\n 'exchange': 'task-group-resolved',\n 'name': 'taskGroupResolved',\n 'routingKey': [\n {\n 'constant': 'primary',\n 'multipleWords': False,\n 'name': 'routingKeyKind',\n },\n {\n 'multipleWords': False,\n 'name': 'taskGroupId',\n },\n {\n 'multipleWords': False,\n 'name': 'schedulerId',\n },\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/task-group-resolved.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef matchuserhome(prefix):\n if not prefix.startswith('~'):\n raise ValueError(\"prefix must start with ~\")\n try: import pwd\n except ImportError:\n try: import winpwd as pwd\n except ImportError: return []\n return ['~' + u[0] for u in pwd.getpwall() if u[0].startswith(prefix[1:])]", "response": "To find matches that start with prefix. This function returns a list of possible matches in form of [ ~user ~user."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef completelist(self, text):\n path = os.path.expanduser(text)\n if len(path) == 0 or path[0] != os.path.sep:\n path = os.path.join(os.getcwd(), path)\n if text == '~':\n dpath = dtext = ''\n bpath = '~'\n files = ['~/']\n elif text.startswith('~') and text.find('/', 1) < 0:\n return self.matchuserhome(text)\n else:\n dtext = os.path.dirname(text)\n dpath = os.path.dirname(path)\n bpath = os.path.basename(path)\n files = os.listdir(dpath)\n if bpath =='':\n matches = [self.buildpath(text, f) for f in files if not f.startswith('.')]\n else:\n matches = [self.buildpath(dtext, f) for f in files if f.startswith(bpath)]\n if len(matches) == 0 and os.path.basename(path)=='..':\n files = os.listdir(path)\n matches = [os.path.join(text, f) for f in files]\n return matches", "response": "Return a list of potential matches for completion\n \n n. b. you want to complete to a file in the current working directory that starts with ~"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def status(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"status\"], *args, **kwargs)", "response": "Get task status\n\n Get task status structure from `taskId`\n\n This method gives output: ``v1/task-status-response.json#``\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def listTaskGroup(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"listTaskGroup\"], *args, **kwargs)", "response": "List Task Group\n\n List tasks sharing the same `taskGroupId`.\n\n As a task-group may contain an unbounded number of tasks, this end-point\n may return a `continuationToken`. To continue listing tasks you must call\n the `listTaskGroup` again with the `continuationToken` as the\n query-string option `continuationToken`.\n\n By default this end-point will try to return up to 1000 members in one\n request. But it **may return less**, even if more tasks are available.\n It may also return a `continuationToken` even though there are no more\n results. However, you can only be sure to have seen all results if you\n keep calling `listTaskGroup` with the last `continuationToken` until you\n get a result without a `continuationToken`.\n\n If you are not interested in listing all the members at once, you may\n use the query-string option `limit` to return fewer.\n\n This method gives output: ``v1/list-task-group-response.json#``\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist Dependent Tasks This method returns a list of tasks that depend on the given taskId.", "response": "async def listDependentTasks(self, *args, **kwargs):\n \"\"\"\n List Dependent Tasks\n\n List tasks that depend on the given `taskId`.\n\n As many tasks from different task-groups may dependent on a single tasks,\n this end-point may return a `continuationToken`. To continue listing\n tasks you must call `listDependentTasks` again with the\n `continuationToken` as the query-string option `continuationToken`.\n\n By default this end-point will try to return up to 1000 tasks in one\n request. But it **may return less**, even if more tasks are available.\n It may also return a `continuationToken` even though there are no more\n results. However, you can only be sure to have seen all results if you\n keep calling `listDependentTasks` with the last `continuationToken` until\n you get a result without a `continuationToken`.\n\n If you are not interested in listing all the tasks at once, you may\n use the query-string option `limit` to return fewer.\n\n This method gives output: ``v1/list-dependent-tasks-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"listDependentTasks\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def createTask(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"createTask\"], *args, **kwargs)", "response": "Create a new task in the scheduler."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nclaiming pending tasks for the given provisionerId and workerType queue.", "response": "async def claimWork(self, *args, **kwargs):\n \"\"\"\n Claim Work\n\n Claim pending task(s) for the given `provisionerId`/`workerType` queue.\n\n If any work is available (even if fewer than the requested number of\n tasks, this will return immediately. Otherwise, it will block for tens of\n seconds waiting for work. If no work appears, it will return an emtpy\n list of tasks. Callers should sleep a short while (to avoid denial of\n service in an error condition) and call the endpoint again. This is a\n simple implementation of \"long polling\".\n\n This method takes input: ``v1/claim-work-request.json#``\n\n This method gives output: ``v1/claim-work-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"claimWork\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def reclaimTask(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"reclaimTask\"], *args, **kwargs)", "response": "Reclaim a task from a specific runId."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def getArtifact(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getArtifact\"], *args, **kwargs)", "response": "Get an artifact from a specific run."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget Artifacts from Run", "response": "async def listArtifacts(self, *args, **kwargs):\n \"\"\"\n Get Artifacts from Run\n\n Returns a list of artifacts and associated meta-data for a given run.\n\n As a task may have many artifacts paging may be necessary. If this\n end-point returns a `continuationToken`, you should call the end-point\n again with the `continuationToken` as the query-string option:\n `continuationToken`.\n\n By default this end-point will list up-to 1000 artifacts in a single page\n you may limit this with the query-string parameter `limit`.\n\n This method gives output: ``v1/list-artifacts-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"listArtifacts\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def getWorkerType(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getWorkerType\"], *args, **kwargs)", "response": "This method returns a worker - type from a provisioner."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nupdate a worker-type Declare a workerType, supplying some details about it. `declareWorkerType` allows updating one or more properties of a worker-type as long as the required scopes are possessed. For example, a request to update the `gecko-b-1-w2008` worker-type within the `aws-provisioner-v1` provisioner with a body `{description: 'This worker type is great'}` would require you to have the scope `queue:declare-worker-type:aws-provisioner-v1/gecko-b-1-w2008#description`. This method takes input: ``v1/update-workertype-request.json#`` This method gives output: ``v1/workertype-response.json#`` This method is ``experimental``", "response": "async def declareWorkerType(self, *args, **kwargs):\n \"\"\"\n Update a worker-type\n\n Declare a workerType, supplying some details about it.\n\n `declareWorkerType` allows updating one or more properties of a worker-type as long as the required scopes are\n possessed. For example, a request to update the `gecko-b-1-w2008` worker-type within the `aws-provisioner-v1`\n provisioner with a body `{description: 'This worker type is great'}` would require you to have the scope\n `queue:declare-worker-type:aws-provisioner-v1/gecko-b-1-w2008#description`.\n\n This method takes input: ``v1/update-workertype-request.json#``\n\n This method gives output: ``v1/workertype-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"declareWorkerType\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def listWorkers(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"listWorkers\"], *args, **kwargs)", "response": "This method returns a list of all active workers of a workerType."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def getWorker(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getWorker\"], *args, **kwargs)", "response": "Get a worker from a worker - type."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef file(value, **kwarg):\n #a bit weird, but I don't want to hard code default values\n try:\n f = open(value, **kwarg)\n except IOError as e:\n raise ValueError(\"unable to open %s : %s\" % (path.abspath(value), e))\n return f", "response": "returns a file object"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef calculateSleepTime(attempt):\n if attempt <= 0:\n return 0\n\n # We subtract one to get exponents: 1, 2, 3, 4, 5, ..\n delay = float(2 ** (attempt - 1)) * float(DELAY_FACTOR)\n # Apply randomization factor\n delay = delay * (RANDOMIZATION_FACTOR * (random.random() * 2 - 1) + 1)\n # Always limit with a maximum delay\n return min(delay, MAX_DELAY)", "response": "Calculate sleep time based on the attempt number."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fromNow(offset, dateObj=None):\n\n # We want to handle past dates as well as future\n future = True\n offset = offset.lstrip()\n if offset.startswith('-'):\n future = False\n offset = offset[1:].lstrip()\n if offset.startswith('+'):\n offset = offset[1:].lstrip()\n\n # Parse offset\n m = r.match(offset)\n if m is None:\n raise ValueError(\"offset string: '%s' does not parse\" % offset)\n\n # In order to calculate years and months we need to calculate how many days\n # to offset the offset by, since timedelta only goes as high as weeks\n days = 0\n hours = 0\n minutes = 0\n seconds = 0\n if m.group('years'):\n years = int(m.group('years'))\n days += 365 * years\n if m.group('months'):\n months = int(m.group('months'))\n days += 30 * months\n days += int(m.group('days') or 0)\n hours += int(m.group('hours') or 0)\n minutes += int(m.group('minutes') or 0)\n seconds += int(m.group('seconds') or 0)\n\n # Offset datetime from utc\n delta = datetime.timedelta(\n weeks=int(m.group('weeks') or 0),\n days=days,\n hours=hours,\n minutes=minutes,\n seconds=seconds,\n )\n\n if not dateObj:\n dateObj = datetime.datetime.utcnow()\n\n return dateObj + delta if future else dateObj - delta", "response": "Generate a datetime. datetime instance which is offset using a string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dumpJson(obj, **kwargs):\n def handleDateAndBinaryForJs(x):\n if six.PY3 and isinstance(x, six.binary_type):\n x = x.decode()\n if isinstance(x, datetime.datetime) or isinstance(x, datetime.date):\n return stringDate(x)\n else:\n return x\n d = json.dumps(obj, separators=(',', ':'), default=handleDateAndBinaryForJs, **kwargs)\n assert '\\n' not in d\n return d", "response": "Match JS s JSON. stringify."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmakes a base64 string URL Safe", "response": "def makeB64UrlSafe(b64str):\n \"\"\" Make a base64 string URL Safe \"\"\"\n if isinstance(b64str, six.text_type):\n b64str = b64str.encode()\n # see RFC 4648, sec. 5\n return b64str.replace(b'+', b'-').replace(b'/', b'_')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef encodeStringForB64Header(s):\n if isinstance(s, six.text_type):\n s = s.encode()\n return base64.encodestring(s).strip().replace(b'\\n', b'')", "response": "Encodes a string for use in a base64 - encoded HTTP header."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stableSlugId():\n _cache = {}\n\n def closure(name):\n if name not in _cache:\n _cache[name] = slugId()\n return _cache[name]\n\n return closure", "response": "Returns a closure which can be used to generate stable slugIds for a single node."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns True if all of the required scope sets in the list of assumed scopes are satisfied.", "response": "def scopeMatch(assumedScopes, requiredScopeSets):\n \"\"\"\n Take a list of a assumed scopes, and a list of required scope sets on\n disjunctive normal form, and check if any of the required scope sets are\n satisfied.\n\n Example:\n\n requiredScopeSets = [\n [\"scopeA\", \"scopeB\"],\n [\"scopeC\"]\n ]\n\n In this case assumed_scopes must contain, either:\n \"scopeA\" AND \"scopeB\", OR just \"scopeC\".\n \"\"\"\n for scopeSet in requiredScopeSets:\n for requiredScope in scopeSet:\n for scope in assumedScopes:\n if scope == requiredScope:\n # requiredScope satisifed, no need to check more scopes\n break\n if scope.endswith(\"*\") and requiredScope.startswith(scope[:-1]):\n # requiredScope satisifed, no need to check more scopes\n break\n else:\n # requiredScope not satisfied, stop checking scopeSet\n break\n else:\n # scopeSet satisfied, so we're happy\n return True\n # none of the requiredScopeSets were satisfied\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef makeHttpRequest(method, url, payload, headers, retries=MAX_RETRIES, session=None):\n retry = -1\n response = None\n while retry < retries:\n retry += 1\n # if this isn't the first retry then we sleep\n if retry > 0:\n snooze = float(retry * retry) / 10.0\n log.info('Sleeping %0.2f seconds for exponential backoff', snooze)\n time.sleep(snooze)\n\n # Seek payload to start, if it is a file\n if hasattr(payload, 'seek'):\n payload.seek(0)\n\n log.debug('Making attempt %d', retry)\n try:\n response = makeSingleHttpRequest(method, url, payload, headers, session)\n except requests.exceptions.RequestException as rerr:\n if retry < retries:\n log.warn('Retrying because of: %s' % rerr)\n continue\n # raise a connection exception\n raise rerr\n # Handle non 2xx status code and retry if possible\n try:\n response.raise_for_status()\n except requests.exceptions.RequestException:\n pass\n status = response.status_code\n if 500 <= status and status < 600 and retry < retries:\n if retry < retries:\n log.warn('Retrying because of: %d status' % status)\n continue\n else:\n raise exceptions.TaskclusterRestFailure(\"Unknown Server Error\", superExc=None)\n return response\n\n # This code-path should be unreachable\n assert False, \"Error from last retry should have been raised!\"", "response": "Make an HTTP request and retry it until success."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if certificate is expired", "response": "def isExpired(certificate):\n \"\"\" Check if certificate is expired \"\"\"\n if isinstance(certificate, six.string_types):\n certificate = json.loads(certificate)\n expiry = certificate.get('expiry', 0)\n return expiry < int(time.time() * 1000) + 20 * 60"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfetches root URL and credentials from the standard TASKCLUSTER_\u2026 environment variables and return them in a format suitable for passing to a client constructor.", "response": "def optionsFromEnvironment(defaults=None):\n \"\"\"Fetch root URL and credentials from the standard TASKCLUSTER_\u2026\n environment variables and return them in a format suitable for passing to a\n client constructor.\"\"\"\n options = defaults or {}\n credentials = options.get('credentials', {})\n\n rootUrl = os.environ.get('TASKCLUSTER_ROOT_URL')\n if rootUrl:\n options['rootUrl'] = rootUrl\n\n clientId = os.environ.get('TASKCLUSTER_CLIENT_ID')\n if clientId:\n credentials['clientId'] = clientId\n\n accessToken = os.environ.get('TASKCLUSTER_ACCESS_TOKEN')\n if accessToken:\n credentials['accessToken'] = accessToken\n\n certificate = os.environ.get('TASKCLUSTER_CERTIFICATE')\n if certificate:\n credentials['certificate'] = certificate\n\n if credentials:\n options['credentials'] = credentials\n\n return options"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef xterm_to_rgb(xcolor):\n assert 0 <= xcolor <= 255\n if xcolor < 16:\n # basic colors\n return BASIC16[xcolor]\n elif 16 <= xcolor <= 231:\n # color cube\n xcolor -= 16\n return (CUBE_STEPS[xcolor // 36 % 6],\n CUBE_STEPS[xcolor // 6 % 6],\n CUBE_STEPS[xcolor % 6])\n elif 232 <= xcolor <= 255:\n # gray tone\n c = 8 + (xcolor - 232) * 0x0A\n return (c, c, c)", "response": "Convert an xterm Color ID to an RGB value."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef rgb_to_xterm(r, g, b):\n if r < 5 and g < 5 and b < 5:\n return 16\n best_match = 0\n smallest_distance = 10000000000\n for c in range(16, 256):\n d = (COLOR_TABLE[c][0] - r) ** 2 + \\\n (COLOR_TABLE[c][1] - g) ** 2 + \\\n (COLOR_TABLE[c][2] - b) ** 2\n if d < smallest_distance:\n smallest_distance = d\n best_match = c\n return best_match", "response": "Quantize RGB values to an xterm 256 - color ID\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef compile_speedup():\n import os\n import ctypes\n from os.path import join, dirname, getmtime, exists, expanduser\n # library = join(dirname(__file__), '_xterm256.so')\n library = expanduser('~/.xterm256.so')\n sauce = join(dirname(__file__), '_xterm256.c')\n if not exists(library) or getmtime(sauce) > getmtime(library):\n build = \"gcc -fPIC -shared -o %s %s\" % (library, sauce)\n if (os.system(build + \" >/dev/null 2>&1\") != 0):\n raise OSError(\"GCC error\")\n xterm256_c = ctypes.cdll.LoadLibrary(library)\n xterm256_c.init()\n def xterm_to_rgb(xcolor):\n res = xterm256_c.xterm_to_rgb_i(xcolor)\n return ((res >> 16) & 0xFF, (res >> 8) & 0xFF, res & 0xFF)\n return (xterm256_c.rgb_to_xterm, xterm_to_rgb)", "response": "Try to compile the C version of this module."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef main():\n import optparse\n parser = optparse.OptionParser()\n parser.add_option(\n \"-w\", \"--width\", dest=\"width\", type=\"int\", default=None,\n help=(\"Width of printed image in characters. Default: %default\"))\n (options, args) = parser.parse_args(args=sys.argv[1:])\n for imgpath in args:\n for line in Image(imgpath, options.width):\n printy(line)", "response": "Main function for the catulous - image command."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef reduce(self, colors):\n need_reset = False\n line = []\n for color, items in itertools.groupby(colors):\n if color is None:\n if need_reset:\n line.append(\"\\x1b[49m\")\n need_reset = False\n line.append(self.pad * len(list(items)))\n elif color == \"EOL\":\n if need_reset:\n line.append(\"\\x1b[49m\")\n need_reset = False\n yield \"\".join(line)\n else:\n line.pop()\n yield \"\".join(line)\n line = []\n else:\n need_reset = True\n line.append(\"\\x1b[48;5;%dm%s\" % (\n color, self.pad * len(list(items))))", "response": "Reduces the color codes into optimized text."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef convert(self):\n (width, height) = self.img.size\n bgcolor = utils.term.bgcolor\n self.img.load()\n for y in range(height):\n for x in range(width):\n rgba = self.img.getpixel((x, y))\n if len(rgba) == 4 and rgba[3] == 0:\n yield None\n elif len(rgba) == 3 or rgba[3] == 255:\n yield xterm256.rgb_to_xterm(*rgba[:3])\n else:\n color = grapefruit.Color.NewFromRgb(\n *[c / 255.0 for c in rgba])\n rgba = grapefruit.Color.AlphaBlend(color, bgcolor).rgb\n yield xterm256.rgb_to_xterm(\n *[int(c * 255.0) for c in rgba])\n yield \"EOL\"", "response": "Yields xterm color codes for each pixel in image\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget Taskcluster credentials given a suitable `access_token` Given an OIDC `access_token` from a trusted OpenID provider, return a set of Taskcluster credentials for use on behalf of the identified user. This method is typically not called with a Taskcluster client library and does not accept Hawk credentials. The `access_token` should be given in an `Authorization` header: ``` Authorization: Bearer abc.xyz ``` The `access_token` is first verified against the named :provider, then passed to the provider's APIBuilder to retrieve a user profile. That profile is then used to generate Taskcluster credentials appropriate to the user. Note that the resulting credentials may or may not include a `certificate` property. Callers should be prepared for either alternative. The given credentials will expire in a relatively short time. Callers should monitor this expiration and refresh the credentials if necessary, by calling this endpoint again, if they have expired. This method gives output: ``v1/oidc-credentials-response.json#`` This method is ``experimental``", "response": "def oidcCredentials(self, *args, **kwargs):\n \"\"\"\n Get Taskcluster credentials given a suitable `access_token`\n\n Given an OIDC `access_token` from a trusted OpenID provider, return a\n set of Taskcluster credentials for use on behalf of the identified\n user.\n\n This method is typically not called with a Taskcluster client library\n and does not accept Hawk credentials. The `access_token` should be\n given in an `Authorization` header:\n ```\n Authorization: Bearer abc.xyz\n ```\n\n The `access_token` is first verified against the named\n :provider, then passed to the provider's APIBuilder to retrieve a user\n profile. That profile is then used to generate Taskcluster credentials\n appropriate to the user. Note that the resulting credentials may or may\n not include a `certificate` property. Callers should be prepared for either\n alternative.\n\n The given credentials will expire in a relatively short time. Callers should\n monitor this expiration and refresh the credentials if necessary, by calling\n this endpoint again, if they have expired.\n\n This method gives output: ``v1/oidc-credentials-response.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"oidcCredentials\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist of Builds A paginated list of builds that have been run in Taskcluster. Can be filtered on various git-specific fields. This method gives output: ``v1/build-list.json#`` This method is ``experimental``", "response": "async def builds(self, *args, **kwargs):\n \"\"\"\n List of Builds\n\n A paginated list of builds that have been run in\n Taskcluster. Can be filtered on various git-specific\n fields.\n\n This method gives output: ``v1/build-list.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"builds\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def repository(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"repository\"], *args, **kwargs)", "response": "Get Repository Info\n\n Returns any repository metadata that is\n useful within Taskcluster related services.\n\n This method gives output: ``v1/repository.json#``\n\n This method is ``experimental``"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a status for a given changeset.", "response": "async def createStatus(self, *args, **kwargs):\n \"\"\"\n Post a status against a given changeset\n\n For a given changeset (SHA) of a repository, this will attach a \"commit status\"\n on github. These statuses are links displayed next to each revision.\n The status is either OK (green check) or FAILURE (red cross),\n made of a custom title and link.\n\n This method takes input: ``v1/create-status.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"createStatus\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def email(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"email\"], *args, **kwargs)", "response": "Send an email to a specific address"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def pulse(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"pulse\"], *args, **kwargs)", "response": "This method is used to publish a message on a pulse with the given routingKey."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def resetAccessToken(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"resetAccessToken\"], *args, **kwargs)", "response": "Reset an existing accessToken"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def role(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"role\"], *args, **kwargs)", "response": "Get information about a single role"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def azureAccounts(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"azureAccounts\"], *args, **kwargs)", "response": "This method returns a list of all Azure accounts managed by Taskcluster Auth."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nauthenticates Hawk Request Validate the request signature given on input and return list of scopes that the authenticating client has. This method is used by other services that wish rely on Taskcluster credentials for authentication. This way we can use Hawk without having the secret credentials leave this service. This method takes input: ``v1/authenticate-hawk-request.json#`` This method gives output: ``v1/authenticate-hawk-response.json#`` This method is ``stable``", "response": "async def authenticateHawk(self, *args, **kwargs):\n \"\"\"\n Authenticate Hawk Request\n\n Validate the request signature given on input and return list of scopes\n that the authenticating client has.\n\n This method is used by other services that wish rely on Taskcluster\n credentials for authentication. This way we can use Hawk without having\n the secret credentials leave this service.\n\n This method takes input: ``v1/authenticate-hawk-request.json#``\n\n This method gives output: ``v1/authenticate-hawk-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"authenticateHawk\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist hook groups This endpoint will return a list of all hook groups with at least one hook. This method gives output: ``v1/list-hook-groups-response.json#`` This method is ``stable``", "response": "async def listHookGroups(self, *args, **kwargs):\n \"\"\"\n List hook groups\n\n This endpoint will return a list of all hook groups with at least one hook.\n\n This method gives output: ``v1/list-hook-groups-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"listHookGroups\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def listHooks(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"listHooks\"], *args, **kwargs)", "response": "This endpoint returns a list of all the hook definitions within a given hook group."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def hook(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"hook\"], *args, **kwargs)", "response": "Get the hook definition for the given hookGroupId and hookId"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\nasync def updateHook(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"updateHook\"], *args, **kwargs)", "response": "This endpoint updates a hook in the specified hierarchy."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a hook This endpoint will remove a hook definition. This method is ``stable``", "response": "async def removeHook(self, *args, **kwargs):\n \"\"\"\n Delete a hook\n\n This endpoint will remove a hook definition.\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"removeHook\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def triggerHook(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"triggerHook\"], *args, **kwargs)", "response": "Trigger a hook that creates a new task from a hook definition."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def getTriggerToken(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getTriggerToken\"], *args, **kwargs)", "response": "This method returns a unique secret token for the specified hook."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreset a trigger token for a given hook", "response": "async def resetTriggerToken(self, *args, **kwargs):\n \"\"\"\n Reset a trigger token\n\n Reset the token for triggering a given hook. This invalidates token that\n may have been issued via getTriggerToken with a new token.\n\n This method gives output: ``v1/trigger-token-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"resetTriggerToken\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntriggering a hook with a valid token.", "response": "async def triggerHookWithToken(self, *args, **kwargs):\n \"\"\"\n Trigger a hook with a token\n\n This endpoint triggers a defined hook with a valid token.\n\n The HTTP payload must match the hooks `triggerSchema`. If it does, it is\n provided as the `payload` property of the JSON-e context used to render the\n task template.\n\n This method takes input: ``v1/trigger-hook.json#``\n\n This method gives output: ``v1/trigger-hook-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"triggerHookWithToken\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates new Worker Type Create a worker type. A worker type contains all the configuration needed for the provisioner to manage the instances. Each worker type knows which regions and which instance types are allowed for that worker type. Remember that Capacity is the number of concurrent tasks that can be run on a given EC2 resource and that Utility is the relative performance rate between different instance types. There is no way to configure different regions to have different sets of instance types so ensure that all instance types are available in all regions. This function is idempotent. Once a worker type is in the provisioner, a back ground process will begin creating instances for it based on its capacity bounds and its pending task count from the Queue. It is the worker's responsibility to shut itself down. The provisioner has a limit (currently 96hours) for all instances to prevent zombie instances from running indefinitely. The provisioner will ensure that all instances created are tagged with aws resource tags containing the provisioner id and the worker type. If provided, the secrets in the global, region and instance type sections are available using the secrets api. If specified, the scopes provided will be used to generate a set of temporary credentials available with the other secrets. This method takes input: ``http://schemas.taskcluster.net/aws-provisioner/v1/create-worker-type-request.json#`` This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/get-worker-type-response.json#`` This method is ``stable``", "response": "async def createWorkerType(self, *args, **kwargs):\n \"\"\"\n Create new Worker Type\n\n Create a worker type. A worker type contains all the configuration\n needed for the provisioner to manage the instances. Each worker type\n knows which regions and which instance types are allowed for that\n worker type. Remember that Capacity is the number of concurrent tasks\n that can be run on a given EC2 resource and that Utility is the relative\n performance rate between different instance types. There is no way to\n configure different regions to have different sets of instance types\n so ensure that all instance types are available in all regions.\n This function is idempotent.\n\n Once a worker type is in the provisioner, a back ground process will\n begin creating instances for it based on its capacity bounds and its\n pending task count from the Queue. It is the worker's responsibility\n to shut itself down. The provisioner has a limit (currently 96hours)\n for all instances to prevent zombie instances from running indefinitely.\n\n The provisioner will ensure that all instances created are tagged with\n aws resource tags containing the provisioner id and the worker type.\n\n If provided, the secrets in the global, region and instance type sections\n are available using the secrets api. If specified, the scopes provided\n will be used to generate a set of temporary credentials available with\n the other secrets.\n\n This method takes input: ``http://schemas.taskcluster.net/aws-provisioner/v1/create-worker-type-request.json#``\n\n This method gives output: ``http://schemas.taskcluster.net/aws-provisioner/v1/get-worker-type-response.json#``\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"createWorkerType\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def updateWorkerType(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"updateWorkerType\"], *args, **kwargs)", "response": "This method is used to update the worker type definition for a specific worker type. This method is used to update the worker type definition for a specific worker type."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def workerType(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"workerType\"], *args, **kwargs)", "response": "This method returns a copy of the worker type definition. This method is deprecated in favor of the new worker type definition."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def createSecret(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"createSecret\"], *args, **kwargs)", "response": "Create a new Secret in the secret storage."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving a Secret from storage", "response": "async def removeSecret(self, *args, **kwargs):\n \"\"\"\n Remove a Secret\n\n Remove a secret. After this call, a call to `getSecret` with the given\n token will return no information.\n\n It is very important that the consumer of a\n secret delete the secret from storage before handing over control\n to untrusted processes to prevent credential and/or secret leakage.\n\n This method is ``stable``\n \"\"\"\n\n return await self._makeApiCall(self.funcinfo[\"removeSecret\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def getLaunchSpecs(self, *args, **kwargs):\n\n return await self._makeApiCall(self.funcinfo[\"getLaunchSpecs\"], *args, **kwargs)", "response": "This method returns all possible launch specifications for this worker type."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def makeHttpRequest(method, url, payload, headers, retries=utils.MAX_RETRIES, session=None):\n retry = -1\n response = None\n implicit = False\n if session is None:\n implicit = True\n session = aiohttp.ClientSession()\n\n def cleanup():\n if implicit:\n loop = asyncio.get_event_loop()\n loop.run_until_complete(session.close())\n\n try:\n while True:\n retry += 1\n # if this isn't the first retry then we sleep\n if retry > 0:\n snooze = float(retry * retry) / 10.0\n log.info('Sleeping %0.2f seconds for exponential backoff', snooze)\n await asyncio.sleep(snooze)\n\n # Seek payload to start, if it is a file\n if hasattr(payload, 'seek'):\n payload.seek(0)\n\n log.debug('Making attempt %d', retry)\n try:\n with async_timeout.timeout(60):\n response = await makeSingleHttpRequest(method, url, payload, headers, session)\n except aiohttp.ClientError as rerr:\n if retry < retries:\n log.warn('Retrying because of: %s' % rerr)\n continue\n # raise a connection exception\n raise rerr\n except ValueError as rerr:\n log.warn('ValueError from aiohttp: redirect to non-http or https')\n raise rerr\n except RuntimeError as rerr:\n log.warn('RuntimeError from aiohttp: session closed')\n raise rerr\n # Handle non 2xx status code and retry if possible\n status = response.status\n if 500 <= status and status < 600 and retry < retries:\n if retry < retries:\n log.warn('Retrying because of: %d status' % status)\n continue\n else:\n raise exceptions.TaskclusterRestFailure(\"Unknown Server Error\", superExc=None)\n return response\n finally:\n cleanup()\n # This code-path should be unreachable\n assert False, \"Error from last retry should have been raised!\"", "response": "Make an HTTP request and retry it until success."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef resolve_font(name):\n if os.path.exists(name):\n return os.path.abspath(name)\n fonts = get_font_files()\n if name in fonts:\n return fonts[name]\n raise FontNotFound(\"Can't find %r :'( Try adding it to ~/.fonts\" % name)", "response": "Turns font names into absolute filenames"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_font_files():\n roots = [\n '/usr/share/fonts/truetype', # where ubuntu puts fonts\n '/usr/share/fonts', # where fedora puts fonts\n os.path.expanduser('~/.fonts'), # custom user fonts\n os.path.abspath(os.path.join(os.path.dirname(__file__), 'fonts')),\n ]\n result = {}\n for root in roots:\n for path, dirs, names in os.walk(root):\n for name in names:\n if name.endswith(('.ttf', '.otf')):\n result[name[:-4]] = os.path.join(path, name)\n return result", "response": "Returns a list of all font files we could find"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef main():\n import optparse\n parser = optparse.OptionParser()\n parser.add_option(\n \"-l\", \"--list\", dest=\"list\", action=\"store_true\", default=False,\n help=(\"List available fonts\"))\n parser.add_option(\n \"-S\", \"--skew\", dest=\"skew\", type=\"int\", default=None,\n help=(\"Apply skew effect (measured in pixels) to make it look \"\n \"extra cool. For example, Fabulous' logo logo is skewed \"\n \"by 5 pixels. Default: %default\"))\n parser.add_option(\n \"-C\", \"--color\", dest=\"color\", default=\"#0099ff\",\n help=(\"Color of your text. This can be specified as you would \"\n \"using HTML/CSS. Default: %default\"))\n parser.add_option(\n \"-B\", \"--term-color\", dest=\"term_color\", default=None,\n help=(\"If you terminal background isn't black, please change \"\n \"this value to the proper background so semi-transparent \"\n \"pixels will blend properly.\"))\n parser.add_option(\n \"-F\", \"--font\", dest=\"font\", default='NotoSans-Bold',\n help=(\"Name of font file, or absolute path to one. Use the --list \"\n \"flag to see what fonts are available. Fabulous bundles the \"\n \"NotoSans-Bold and NotoEmoji-Regular fonts, which are guaranteed \"\n \"to work. Default: %default\"))\n parser.add_option(\n \"-Z\", \"--size\", dest=\"fsize\", type=\"int\", default=23,\n help=(\"Size of font in points. Default: %default\"))\n parser.add_option(\n \"-s\", \"--shadow\", dest=\"shadow\", action=\"store_true\", default=False,\n help=(\"Size of font in points. Default: %default\"))\n (options, args) = parser.parse_args(args=sys.argv[1:])\n if options.list:\n print(\"\\n\".join(sorted(get_font_files())))\n return\n if options.term_color:\n utils.term.bgcolor = options.term_color\n text = \" \".join(args)\n if not isinstance(text, unicode):\n text = text.decode('utf-8')\n for line in text.split(\"\\n\"):\n fab_text = Text(line, skew=options.skew, color=options.color,\n font=options.font, fsize=options.fsize,\n shadow=options.shadow)\n for chunk in fab_text:\n printy(chunk)", "response": "Main function for the fabulous - text command."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef purgeCache(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"purgeCache\"], *args, **kwargs)", "response": "Purge Worker Cache\n\n Publish a purge-cache message to purge caches named `cacheName` with\n `provisionerId` and `workerType` in the routing-key. Workers should\n be listening for this message and purge caches when they see it.\n\n This method takes input: ``v1/purge-cache-request.json#``\n\n This method is ``stable``"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_by_step(raw):\n # make sure it is unicode\n delta = ord(raw[1]) - ord(raw[0])\n\n for i in range(2, len(raw)):\n if ord(raw[i]) - ord(raw[i-1]) != delta:\n return False\n\n return True", "response": "If the password is alphabet step by step."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking if the password is common used.", "response": "def is_common_password(raw, freq=0):\n \"\"\"If the password is common used.\n\n 10k top passwords: https://xato.net/passwords/more-top-worst-passwords/\n \"\"\"\n frequent = WORDS.get(raw, 0)\n if freq:\n return frequent > freq\n return bool(frequent)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking the safety level of a password.", "response": "def check(raw, length=8, freq=0, min_types=3, level=STRONG):\n \"\"\"Check the safety level of the password.\n\n :param raw: raw text password.\n :param length: minimal length of the password.\n :param freq: minimum frequency.\n :param min_types: minimum character family.\n :param level: minimum level to validate a password.\n \"\"\"\n raw = to_unicode(raw)\n if level > STRONG:\n level = STRONG\n\n if len(raw) < length:\n return Strength(False, 'terrible', 'password is too short')\n\n if is_asdf(raw) or is_by_step(raw):\n return Strength(False, 'simple', 'password has a pattern')\n\n if is_common_password(raw, freq=freq):\n return Strength(False, 'simple', 'password is too common')\n\n types = 0\n\n if LOWER.search(raw):\n types += 1\n\n if UPPER.search(raw):\n types += 1\n\n if NUMBER.search(raw):\n types += 1\n\n if MARKS.search(raw):\n types += 1\n\n if types < 2:\n return Strength(level <= SIMPLE, 'simple', 'password is too simple')\n\n if types < min_types:\n return Strength(level <= MEDIUM, 'medium',\n 'password is good enough, but not strong')\n\n return Strength(True, 'strong', 'password is perfect')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _make_request(self, url, method=\"get\", data=None, extra_headers=None):\n attempts = 0\n\n while attempts < 1:\n # Authenticate first if not authenticated already\n if not self._is_authenticated:\n self._authenticate()\n # Make the request and check for authentication errors\n # This allows us to catch session timeouts for long standing connections\n try:\n return self._send_request(url, method, data, extra_headers)\n except HTTPError as e:\n if e.response.status_code == 403:\n logger.info(\"Authenticated session against NetMRI timed out. Retrying.\")\n self._is_authenticated = False\n attempts += 1\n else:\n # re-raise other HTTP errors\n raise", "response": "Prepares the request and retries if issues are detected."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nperforming a given request and returns a json object containing the response", "response": "def _send_request(self, url, method=\"get\", data=None, extra_headers=None):\n \"\"\"Performs a given request and returns a json object\n\n Args:\n url (str): URL of the request\n method (str): Any of \"get\", \"post\", \"delete\"\n data (any): Possible extra data to send with the request\n extra_headers (dict): Possible extra headers to send along in the request\n Returns:\n dict\n \"\"\"\n headers = {'Content-type': 'application/json'}\n if isinstance(extra_headers, dict):\n headers.update(extra_headers)\n\n if not data or \"password\" not in data:\n logger.debug(\"Sending {method} request to {url} with data {data}\".format(\n method=method.upper(), url=url, data=data)\n )\n r = self.session.request(method, url, headers=headers, data=data)\n r.raise_for_status()\n return r.json()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_api_version(self):\n url = \"{base_url}/api/server_info\".format(base_url=self._base_url())\n server_info = self._make_request(url=url, method=\"get\")\n return server_info[\"latest_api_version\"]", "response": "Fetches the most recent API version"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _authenticate(self):\n url = \"{base_url}/api/authenticate\".format(base_url=self._base_url())\n data = json.dumps({'username': self.username, \"password\": self.password})\n # Bypass authentication check in make_request by using _send_request\n logger.debug(\"Authenticating against NetMRI\")\n self._send_request(url, method=\"post\", data=data)\n self._is_authenticated = True", "response": "Perform an authentication against NetMRI"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndetermining the controller name for the object s type", "response": "def _controller_name(self, objtype):\n \"\"\"Determines the controller name for the object's type\n\n Args:\n objtype (str): The object type\n\n Returns:\n A string with the controller name\n \"\"\"\n # would be better to use inflect.pluralize here, but would add a dependency\n if objtype.endswith('y'):\n return objtype[:-1] + 'ies'\n\n if objtype[-1] in 'sx' or objtype[-2:] in ['sh', 'ch']:\n return objtype + 'es'\n\n if objtype.endswith('an'):\n return objtype[:-2] + 'en'\n\n return objtype + 's'"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates the URL for the specified object.", "response": "def _object_url(self, objtype, objid):\n \"\"\"Generate the URL for the specified object\n\n Args:\n objtype (str): The object's type\n objid (int): The objects ID\n\n Returns:\n A string containing the URL of the object\n \"\"\"\n return \"{base_url}/api/{api_version}/{controller}/{obj_id}\".format(\n base_url=self._base_url(),\n api_version=self.api_version,\n controller=self._controller_name(objtype),\n obj_id=objid\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngenerate the URL for the requested method", "response": "def _method_url(self, method_name):\n \"\"\"Generate the URL for the requested method\n\n Args:\n method_name (str): Name of the method\n\n Returns:\n A string containing the URL of the method\n \"\"\"\n return \"{base_url}/api/{api}/{method}\".format(\n base_url=self._base_url(),\n api=self.api_version,\n method=method_name\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef api_request(self, method_name, params):\n url = self._method_url(method_name)\n data = json.dumps(params)\n return self._make_request(url=url, method=\"post\", data=data)", "response": "Execute an arbitrary method."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nqueries for a specific resource by ID.", "response": "def show(self, objtype, objid):\n \"\"\"Query for a specific resource by ID\n\n Args:\n objtype (str): object type, e.g. 'device', 'interface'\n objid (int): object ID (DeviceID, etc.)\n Returns:\n A dict with that object\n Raises:\n requests.exceptions.HTTPError\n \"\"\"\n url = self._object_url(objtype, int(objid))\n return self._make_request(url, method=\"get\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef irc(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"irc\"], *args, **kwargs)", "response": "Post IRC Message\n\n Post a message on IRC to a specific channel or user, or a specific user\n on a specific channel.\n\n Success of this API method does not imply the message was successfully\n posted. This API method merely inserts the IRC message into a queue\n that will be processed by a background process.\n This allows us to re-send the message in face of connection issues.\n\n However, if the user isn't online the message will be dropped without\n error. We maybe improve this behavior in the future. For now just keep\n in mind that IRC is a best-effort service.\n\n This method takes input: ``v1/irc-request.json#``\n\n This method is ``experimental``"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef addDenylistAddress(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"addDenylistAddress\"], *args, **kwargs)", "response": "This is the base method for adding a new denylist address to the given address."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete Denylisted Address Delete the specified address from the notification denylist. This method takes input: ``v1/notification-address.json#`` This method is ``experimental``", "response": "def deleteDenylistAddress(self, *args, **kwargs):\n \"\"\"\n Delete Denylisted Address\n\n Delete the specified address from the notification denylist.\n\n This method takes input: ``v1/notification-address.json#``\n\n This method is ``experimental``\n \"\"\"\n\n return self._makeApiCall(self.funcinfo[\"deleteDenylistAddress\"], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list(self, *args, **kwargs):\n\n return self._makeApiCall(self.funcinfo[\"list\"], *args, **kwargs)", "response": "This method returns a list of all the denylisted addresses."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef clientCreated(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'client-created',\n 'name': 'clientCreated',\n 'routingKey': [\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/client-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "This is the client Created Messages\n Message that a new client has been created. This is a simple function that returns a new message that has been created."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef roleUpdated(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'role-updated',\n 'name': 'roleUpdated',\n 'routingKey': [\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/role-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "Role Updated Messages\n Role Updated Messages\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef roleDeleted(self, *args, **kwargs):\n\n ref = {\n 'exchange': 'role-deleted',\n 'name': 'roleDeleted',\n 'routingKey': [\n {\n 'multipleWords': True,\n 'name': 'reserved',\n },\n ],\n 'schema': 'v1/role-message.json#',\n }\n return self._makeTopicExchange(ref, *args, **kwargs)", "response": "Role Deleted Messages\n\n Message that a new role has been deleted.\n\n This exchange outputs: ``v1/role-message.json#``This exchange takes the following keys:\n\n * reserved: Space reserved for future routing-key entries, you should always match this entry with `#`. As automatically done by our tooling, if not specified."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef dimensions(self):\n try:\n call = fcntl.ioctl(self.termfd, termios.TIOCGWINSZ, \"\\000\" * 8)\n except IOError:\n return (79, 40)\n else:\n height, width = struct.unpack(\"hhhh\", call)[:2]\n return (width, height)", "response": "Returns terminal dimensions as a tuple."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef sp_msg(cmd, pipe=None, data=None):\n msg = [SP_HEADER, cmd]\n if pipe is not None:\n msg.append(pipe)\n if data is not None:\n msg.append(data)\n return msg", "response": "Produces skypipe protocol multipart message"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef stream_skypipe_output(endpoint, name=None):\n name = name or ''\n socket = ctx.socket(zmq.DEALER)\n socket.connect(endpoint)\n try:\n socket.send_multipart(sp_msg(SP_CMD_LISTEN, name))\n\n while True:\n msg = socket.recv_multipart()\n try:\n data = parse_skypipe_data_stream(msg, name)\n if data:\n yield data\n except EOFError:\n raise StopIteration()\n\n finally:\n socket.send_multipart(sp_msg(SP_CMD_UNLISTEN, name))\n socket.close()", "response": "Generator for reading skypipe data from the given endpoint."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_skypipe_data_stream(msg, for_pipe):\n header = str(msg.pop(0))\n command = str(msg.pop(0))\n pipe_name = str(msg.pop(0))\n data = str(msg.pop(0))\n if header != SP_HEADER: return\n if pipe_name != for_pipe: return\n if command != SP_CMD_DATA: return\n if data == SP_DATA_EOF:\n raise EOFError()\n else:\n return data", "response": "May return data from skypipe message or raises EOFError"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a context manager for streaming data into skypipe", "response": "def skypipe_input_stream(endpoint, name=None):\n \"\"\"Returns a context manager for streaming data into skypipe\"\"\"\n name = name or ''\n class context_manager(object):\n def __enter__(self):\n self.socket = ctx.socket(zmq.DEALER)\n self.socket.connect(endpoint)\n return self\n\n def send(self, data):\n data_msg = sp_msg(SP_CMD_DATA, name, data)\n self.socket.send_multipart(data_msg)\n\n def __exit__(self, *args, **kwargs):\n eof_msg = sp_msg(SP_CMD_DATA, name, SP_DATA_EOF)\n self.socket.send_multipart(eof_msg)\n self.socket.close()\n\n return context_manager()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stream_stdin_lines():\n stdin = os.fdopen(sys.stdin.fileno(), 'r', 0)\n while True:\n line = stdin.readline()\n if line:\n yield line\n else:\n break", "response": "Generator for unbuffered lines reading from STDIN"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrun the skypipe client", "response": "def run(endpoint, name=None):\n \"\"\"Runs the skypipe client\"\"\"\n try:\n if os.isatty(0):\n # output mode\n for data in stream_skypipe_output(endpoint, name):\n sys.stdout.write(data)\n sys.stdout.flush()\n\n else:\n # input mode\n with skypipe_input_stream(endpoint, name) as stream:\n for line in stream_stdin_lines():\n stream.send(line)\n\n except KeyboardInterrupt:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nvalidates the time inversion of the time range.", "response": "def validate_time_inversion(self):\n \"\"\"\n Check time inversion of the time range.\n\n :raises ValueError:\n If |attr_start_datetime| is\n bigger than |attr_end_datetime|.\n :raises TypeError:\n Any one of |attr_start_datetime| and |attr_end_datetime|,\n or both is inappropriate datetime value.\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:10:00+0900\", \"2015-03-22T10:00:00+0900\")\n try:\n time_range.validate_time_inversion()\n except ValueError:\n print \"time inversion\"\n :Output:\n .. parsed-literal::\n\n time inversion\n \"\"\"\n\n if not self.is_set():\n # for python2/3 compatibility\n raise TypeError\n\n if self.start_datetime > self.end_datetime:\n raise ValueError(\n \"time inversion found: {:s} > {:s}\".format(\n str(self.start_datetime), str(self.end_datetime)\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning |str| formatted with |attr_start_datetime| as formatted with |attr_start_time_format|.", "response": "def get_start_time_str(self):\n \"\"\"\n :return:\n |attr_start_datetime| as |str| formatted with\n |attr_start_time_format|.\n Return |NaT| if the invalid value or the invalid format.\n :rtype: str\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n print(time_range.get_start_time_str())\n time_range.start_time_format = \"%Y/%m/%d %H:%M:%S\"\n print(time_range.get_start_time_str())\n :Output:\n .. parsed-literal::\n\n 2015-03-22T10:00:00+0900\n 2015/03/22 10:00:00\n \"\"\"\n\n try:\n return self.start_datetime.strftime(self.start_time_format)\n except AttributeError:\n return self.NOT_A_TIME_STR"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_end_time_str(self):\n\n try:\n return self.end_datetime.strftime(self.end_time_format)\n except AttributeError:\n return self.NOT_A_TIME_STR", "response": "Return |str| formatted with |attr_end_datetime|."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the start time of the time range.", "response": "def set_start_datetime(self, value, timezone=None):\n \"\"\"\n Set the start time of the time range.\n\n :param value: |param_start_datetime|\n :type value: |datetime|/|str|\n :raises ValueError: If the value is invalid as a |datetime| value.\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange()\n print(time_range)\n time_range.set_start_datetime(\"2015-03-22T10:00:00+0900\")\n print(time_range)\n :Output:\n .. parsed-literal::\n\n NaT - NaT\n 2015-03-22T10:00:00+0900 - NaT\n \"\"\"\n\n if value is None:\n self.__start_datetime = None\n return\n\n try:\n self.__start_datetime = typepy.type.DateTime(\n value, strict_level=typepy.StrictLevel.MIN, timezone=timezone\n ).convert()\n except typepy.TypeConversionError as e:\n raise ValueError(e)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets the end time of the time range.", "response": "def set_end_datetime(self, value, timezone=None):\n \"\"\"\n Set the end time of the time range.\n\n :param datetime.datetime/str value: |param_end_datetime|\n :raises ValueError: If the value is invalid as a |datetime| value.\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange()\n print(time_range)\n time_range.set_end_datetime(\"2015-03-22T10:10:00+0900\")\n print(time_range)\n :Output:\n .. parsed-literal::\n\n NaT - NaT\n NaT - 2015-03-22T10:10:00+0900\n \"\"\"\n\n if value is None:\n self.__end_datetime = None\n return\n\n try:\n self.__end_datetime = typepy.type.DateTime(\n value, strict_level=typepy.StrictLevel.MIN, timezone=timezone\n ).convert()\n except typepy.TypeConversionError as e:\n raise ValueError(e)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets start and end times for the current object.", "response": "def set_time_range(self, start, end):\n \"\"\"\n :param datetime.datetime/str start: |param_start_datetime|\n :param datetime.datetime/str end: |param_end_datetime|\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange()\n print(time_range)\n time_range.set_time_range(\"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n print(time_range)\n :Output:\n .. parsed-literal::\n\n NaT - NaT\n 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900\n \"\"\"\n\n self.set_start_datetime(start)\n self.set_end_datetime(end)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns an iterator object that yields the current time in the sequence of ISO - 8601 ISO 8601 entries.", "response": "def range(self, step):\n \"\"\"\n Return an iterator object.\n\n :param step: Step of iteration.\n :type step: |timedelta|/dateutil.relativedelta.relativedelta\n :return: iterator\n :rtype: iterator\n\n :Sample Code:\n .. code:: python\n\n import datetime\n from datetimerange import DateTimeRange\n\n time_range = DateTimeRange(\"2015-01-01T00:00:00+0900\", \"2015-01-04T00:00:00+0900\")\n for value in time_range.range(datetime.timedelta(days=1)):\n print(value)\n :Output:\n .. parsed-literal::\n\n 2015-01-01 00:00:00+09:00\n 2015-01-02 00:00:00+09:00\n 2015-01-03 00:00:00+09:00\n 2015-01-04 00:00:00+09:00\n \"\"\"\n\n if self.__compare_timedelta(step, 0) == 0:\n raise ValueError(\"step must be not zero\")\n\n is_inversion = False\n try:\n self.validate_time_inversion()\n except ValueError:\n is_inversion = True\n\n if not is_inversion:\n if self.__compare_timedelta(step, seconds=0) < 0:\n raise ValueError(\"invalid step: expect greater than 0, actual={}\".format(step))\n else:\n if self.__compare_timedelta(step, seconds=0) > 0:\n raise ValueError(\"invalid step: expect less than 0, actual={}\".format(step))\n\n current_datetime = self.start_datetime\n while current_datetime <= self.end_datetime:\n yield current_datetime\n current_datetime = current_datetime + step"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef intersection(self, x):\n\n self.validate_time_inversion()\n x.validate_time_inversion()\n\n if any([x.start_datetime in self, self.start_datetime in x]):\n start_datetime = max(self.start_datetime, x.start_datetime)\n end_datetime = min(self.end_datetime, x.end_datetime)\n else:\n start_datetime = None\n end_datetime = None\n\n return DateTimeRange(\n start_datetime=start_datetime,\n end_datetime=end_datetime,\n start_time_format=self.start_time_format,\n end_time_format=self.end_time_format,\n )", "response": "Return a new time range that overlaps the input and the current time range."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntruncate the entire time of the current object.", "response": "def truncate(self, percentage):\n \"\"\"\n Truncate ``percentage`` / 2 [%] of whole time from first and last time.\n\n :param float percentage: Percentage of truncate.\n\n :Sample Code:\n .. code:: python\n\n from datetimerange import DateTimeRange\n time_range = DateTimeRange(\n \"2015-03-22T10:00:00+0900\", \"2015-03-22T10:10:00+0900\")\n time_range.is_output_elapse = True\n print(time_range)\n time_range.truncate(10)\n print(time_range)\n :Output:\n .. parsed-literal::\n\n 2015-03-22T10:00:00+0900 - 2015-03-22T10:10:00+0900 (0:10:00)\n 2015-03-22T10:00:30+0900 - 2015-03-22T10:09:30+0900 (0:09:00)\n \"\"\"\n\n self.validate_time_inversion()\n\n if percentage < 0:\n raise ValueError(\"discard_percent must be greater or equal to zero: \" + str(percentage))\n\n if percentage == 0:\n return\n\n discard_time = self.timedelta // int(100) * int(percentage / 2)\n\n self.__start_datetime += discard_time\n self.__end_datetime -= discard_time"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef wait_for(text, finish=None, io=None):\n if finish:\n finish.set()\n time.sleep(0.1) # threads, sigh\n if not io:\n io = sys.stdout\n finish = threading.Event()\n io.write(text)\n def _wait():\n while not finish.is_set():\n io.write('.')\n io.flush()\n finish.wait(timeout=1)\n io.write('\\n')\n threading.Thread(target=_wait).start()\n return finish", "response": "Displays dots until the event is set"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef lookup_endpoint(cli):\n url = '/applications/{0}/environment'.format(APPNAME)\n environ = cli.user.get(url).item\n port = environ['DOTCLOUD_SATELLITE_ZMQ_PORT']\n host = socket.gethostbyname(environ['DOTCLOUD_SATELLITE_ZMQ_HOST'])\n return \"tcp://{0}:{1}\".format(host, port)", "response": "Looks up the application endpoint from dotcloud"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef setup_dotcloud_account(cli):\n client = RESTClient(endpoint=cli.client.endpoint)\n client.authenticator = NullAuth()\n urlmap = client.get('/auth/discovery').item\n username = cli.prompt('dotCloud email')\n password = cli.prompt('Password', noecho=True)\n credential = {'token_url': urlmap.get('token'),\n 'key': CLIENT_KEY, 'secret': CLIENT_SECRET}\n try:\n token = cli.authorize_client(urlmap.get('token'), credential, username, password)\n except Exception as e:\n cli.die('Username and password do not match. Try again.')\n token['url'] = credential['token_url']\n config = GlobalConfig()\n config.data = {'token': token}\n config.save()\n cli.global_config = GlobalConfig() # reload\n cli.setup_auth()\n cli.get_keys()", "response": "Sets up the user and password for dotcloud"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef setup(cli):\n if not cli.global_config.loaded:\n setup_dotcloud_account(cli)\n discover_satellite(cli)\n cli.success(\"Skypipe is ready for action\")", "response": "Setup skypipe for the current project"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlooking to make sure a satellite exists and returns endpoint", "response": "def discover_satellite(cli, deploy=True, timeout=5):\n \"\"\"Looks to make sure a satellite exists, returns endpoint\n\n First makes sure we have dotcloud account credentials. Then it looks\n up the environment for the satellite app. This will contain host and\n port to construct an endpoint. However, if app doesn't exist, or\n endpoint does not check out, we call `launch_satellite` to deploy,\n which calls `discover_satellite` again when finished. Ultimately we\n return a working endpoint. If deploy is False it will not try to\n deploy.\n \"\"\"\n if not cli.global_config.loaded:\n cli.die(\"Please setup skypipe by running `skypipe --setup`\")\n\n try:\n endpoint = lookup_endpoint(cli)\n ok = client.check_skypipe_endpoint(endpoint, timeout)\n if ok:\n return endpoint\n else:\n return launch_satellite(cli) if deploy else None\n except (RESTAPIError, KeyError):\n return launch_satellite(cli) if deploy else None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlaunching a new satellite app over any existing app", "response": "def launch_satellite(cli):\n \"\"\"Deploys a new satellite app over any existing app\"\"\"\n\n cli.info(\"Launching skypipe satellite:\")\n\n finish = wait_for(\" Pushing to dotCloud\")\n\n # destroy any existing satellite\n destroy_satellite(cli)\n\n # create new satellite app\n url = '/applications'\n try:\n cli.user.post(url, {\n 'name': APPNAME,\n 'flavor': 'sandbox'\n })\n except RESTAPIError as e:\n if e.code == 409:\n cli.die('Application \"{0}\" already exists.'.format(APPNAME))\n else:\n cli.die('Creating application \"{0}\" failed: {1}'.format(APPNAME, e))\n class args: application = APPNAME\n #cli._connect(args)\n\n # push satellite code\n protocol = 'rsync'\n url = '/applications/{0}/push-endpoints{1}'.format(APPNAME, '')\n endpoint = cli._select_endpoint(cli.user.get(url).items, protocol)\n class args: path = satellite_path\n cli.push_with_rsync(args, endpoint)\n\n # tell dotcloud to deploy, then wait for it to finish\n revision = None\n clean = False\n url = '/applications/{0}/deployments'.format(APPNAME)\n response = cli.user.post(url, {'revision': revision, 'clean': clean})\n deploy_trace_id = response.trace_id\n deploy_id = response.item['deploy_id']\n\n\n original_stdout = sys.stdout\n\n finish = wait_for(\" Waiting for deployment\", finish, original_stdout)\n\n try:\n sys.stdout = StringIO()\n res = cli._stream_deploy_logs(APPNAME, deploy_id,\n deploy_trace_id=deploy_trace_id, follow=True)\n if res != 0:\n return res\n except KeyboardInterrupt:\n cli.error('You\\'ve closed your log stream with Ctrl-C, ' \\\n 'but the deployment is still running in the background.')\n cli.error('If you aborted because of an error ' \\\n '(e.g. the deployment got stuck), please e-mail\\n' \\\n 'support@dotcloud.com and mention this trace ID: {0}'\n .format(deploy_trace_id))\n cli.error('If you want to continue following your deployment, ' \\\n 'try:\\n{0}'.format(\n cli._fmt_deploy_logs_command(deploy_id)))\n cli.die()\n except RuntimeError:\n # workaround for a bug in the current dotcloud client code\n pass\n finally:\n sys.stdout = original_stdout\n\n finish = wait_for(\" Satellite coming online\", finish)\n\n endpoint = lookup_endpoint(cli)\n ok = client.check_skypipe_endpoint(endpoint, 120)\n \n finish.set()\n time.sleep(0.1) # sigh, threads\n\n if ok:\n return endpoint\n else:\n cli.die(\"Satellite failed to come online\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a byte string representation of the given string.", "response": "def bytes_(s, encoding='utf-8', errors='strict'):\n \"\"\" If ``s`` is an instance of ``text_type``, return\n ``s.encode(encoding, errors)``, otherwise return ``s``\"\"\"\n if isinstance(s, text_type): # pragma: no cover\n return s.encode(encoding, errors)\n return s"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall the pg_dump command to create a db", "response": "def pg_backup(self, pg_dump_exe='pg_dump', exclude_schema=None):\n \"\"\"Call the pg_dump command to create a db backup\n\n Parameters\n ----------\n pg_dump_exe: str\n the pg_dump command path\n exclude_schema: str[]\n list of schemas to be skipped\n \"\"\"\n\n command = [\n pg_dump_exe, '-Fc', '-f', self.file,\n 'service={}'.format(self.pg_service)\n ]\n if exclude_schema:\n command.append(' '.join(\"--exclude-schema={}\".format(schema) for schema in exclude_schema))\n\n subprocess.check_output(command, stderr=subprocess.STDOUT)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalling the pg_restore command to restore a db", "response": "def pg_restore(self, pg_restore_exe='pg_restore', exclude_schema=None):\n \"\"\"Call the pg_restore command to restore a db backup\n\n Parameters\n ----------\n pg_restore_exe: str\n the pg_restore command path\n \"\"\"\n\n command = [\n pg_restore_exe, '-d',\n 'service={}'.format(self.pg_service),\n '--no-owner'\n ]\n\n if exclude_schema:\n exclude_schema_available = False\n try:\n pg_version = subprocess.check_output(['pg_restore','--version'])\n pg_version = str(pg_version).replace('\\\\n', '').replace(\"'\", '').split(' ')[-1]\n exclude_schema_available = LooseVersion(pg_version) >= LooseVersion(\"10.0\")\n except subprocess.CalledProcessError as e:\n print(\"*** Could not get pg_restore version:\\n\", e.stderr)\n if exclude_schema_available:\n command.append(' '.join(\"--exclude-schema={}\".format(schema) for schema in exclude_schema))\n command.append(self.file)\n\n try:\n subprocess.check_output(command)\n except subprocess.CalledProcessError as e:\n print(\"*** pg_restore failed:\\n\", command, '\\n', e.stderr)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef exists_table_upgrades(self):\n\n query = \"\"\"\n SELECT EXISTS (\n SELECT 1\n FROM information_schema.tables\n WHERE table_schema = '{}'\n AND table_name = '{}'\n );\n \"\"\".format(self.upgrades_table[:self.upgrades_table.index('.')],\n self.upgrades_table[self.upgrades_table.index('.')+1:])\n\n self.cursor.execute(query)\n return self.cursor.fetchone()[0]", "response": "Return if the upgrades table exists Returns False if the upgrades table don t exists Returns True if the upgrades table exists False if the upgrades table doesn t exist"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __get_delta_files(self):\n files = [(d, f) for d in self.dirs for f in listdir(d) if isfile(join(d, f))]\n\n deltas = OrderedDict()\n for d, f in files:\n file_ = join(d, f)\n\n if not Delta.is_valid_delta_name(file_):\n continue\n\n delta = Delta(file_)\n\n if d not in deltas:\n deltas[d] = []\n deltas[d].append(delta)\n\n # sort delta objects in each bucket\n for d in deltas:\n deltas[d].sort(key=lambda x: (x.get_version(), x.get_priority(), x.get_name()))\n\n return deltas", "response": "Search for delta files and return a dict of Delta objects keyed by directory names."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __run_delta_sql(self, delta):\n\n self.__run_sql_file(delta.get_file())\n self.__update_upgrades_table(delta)", "response": "Execute the delta sql file on the database"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __run_delta_py(self, delta):\n\n self.__run_py_file(delta.get_file(), delta.get_name())\n self.__update_upgrades_table(delta)", "response": "Execute the delta py file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __run_pre_all(self):\n\n # if the list of delta dirs is [delta1, delta2] the pre scripts of delta2 are\n # executed before the pre scripts of delta1\n\n for d in reversed(self.dirs):\n pre_all_py_path = os.path.join(d, 'pre-all.py')\n if os.path.isfile(pre_all_py_path):\n print(' Applying pre-all.py...', end=' ')\n self.__run_py_file(pre_all_py_path, 'pre-all')\n print('OK')\n\n pre_all_sql_path = os.path.join(d, 'pre-all.sql')\n if os.path.isfile(pre_all_sql_path):\n print(' Applying pre-all.sql...', end=' ')\n self.__run_sql_file(pre_all_sql_path)\n print('OK')", "response": "Execute the pre - all. py and pre - all. sql files if they exist"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexecute the post - all. py and post - all. sql files if they exist.", "response": "def __run_post_all(self):\n \"\"\"Execute the post-all.py and post-all.sql files if they exist\"\"\"\n\n # if the list of delta dirs is [delta1, delta2] the post scripts of delta1 are\n # executed before the post scripts of delta2\n\n for d in self.dirs:\n post_all_py_path = os.path.join(d, 'post-all.py')\n if os.path.isfile(post_all_py_path):\n print(' Applying post-all.py...', end=' ')\n self.__run_py_file(post_all_py_path, 'post-all')\n print('OK')\n\n post_all_sql_path = os.path.join(d, 'post-all.sql')\n if os.path.isfile(post_all_sql_path):\n print(' Applying post-all.sql...', end=' ')\n self.__run_sql_file(post_all_sql_path)\n print('OK')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __run_sql_file(self, filepath):\n\n with open(filepath, 'r') as delta_file:\n sql = delta_file.read()\n if self.variables:\n self.cursor.execute(sql, self.variables)\n else:\n self.cursor.execute(sql)\n self.connection.commit()", "response": "Execute the sql file at the passed path"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes the python file at the passed path and return the ID of the object that was executed", "response": "def __run_py_file(self, filepath, module_name):\n \"\"\"Execute the python file at the passed path\n\n Parameters\n ----------\n filepath: str\n the path of the file to execute\n module_name: str\n the name of the python module\n \"\"\"\n\n # Import the module\n spec = importlib.util.spec_from_file_location(module_name, filepath)\n delta_py = importlib.util.module_from_spec(spec)\n spec.loader.exec_module(delta_py)\n\n # Get the python file's directory path\n # Note: we add a separator for backward compatibility, as existing DeltaPy subclasses\n # may assume that delta_dir ends with a separator\n dir_ = dirname(filepath) + os.sep\n\n # Search for subclasses of DeltaPy\n for name in dir(delta_py):\n obj = getattr(delta_py, name)\n if inspect.isclass(obj) and not obj == DeltaPy and issubclass(\n obj, DeltaPy):\n\n delta_py_inst = obj(\n self.current_db_version(), dir_, self.dirs, self.pg_service,\n self.upgrades_table, variables=self.variables)\n delta_py_inst.run()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprinting info about found delta files and about already made upgrades", "response": "def show_info(self):\n \"\"\"Print info about found delta files and about already made upgrades\"\"\"\n\n deltas = self.__get_delta_files()\n\n table = [['Version', 'Name', 'Type', 'Status']]\n\n for dir_ in deltas:\n print('delta files in dir: ', dir_)\n\n for delta in deltas[dir_]:\n line = [delta.get_version(), delta.get_name()]\n if delta.get_type() == DeltaType.PRE_PYTHON:\n line.append('pre py')\n elif delta.get_type() == DeltaType.PRE_SQL:\n line.append('pre sql')\n elif delta.get_type() == DeltaType.PYTHON:\n line.append('delta py')\n elif delta.get_type() == DeltaType.SQL:\n line.append('delta sql')\n elif delta.get_type() == DeltaType.POST_PYTHON:\n line.append('post py')\n elif delta.get_type() == DeltaType.POST_SQL:\n line.append('post sql')\n\n if self.__is_applied(delta):\n line.append('Applied')\n else:\n line.append('Pending')\n\n table.append(line)\n\n self.__print_table(table)\n\n print('')\n print('Applied upgrades in database')\n\n query = \"\"\"SELECT\n version,\n description,\n type,\n installed_by,\n installed_on,\n success\n FROM {}\n \"\"\".format(self.upgrades_table)\n\n self.cursor.execute(query)\n records = self.cursor.fetchall()\n\n table = [['Version', 'Name', 'Type', 'Installed by', 'Installed on',\n 'Status']]\n\n for i in records:\n line = [str(i[0]), str(i[1])]\n delta_type = i[2]\n if delta_type == 0:\n line.append('baseline')\n elif delta_type == DeltaType.PRE_PYTHON:\n line.append('pre py')\n elif delta_type == DeltaType.PRE_SQL:\n line.append('pre sql')\n elif delta_type == DeltaType.PYTHON:\n line.append('delta py')\n elif delta_type == DeltaType.SQL:\n line.append('delta sql')\n elif delta_type == DeltaType.POST_PYTHON:\n line.append('post py')\n elif delta_type == DeltaType.POST_SQL:\n line.append('post sql')\n\n line.append(str(i[3]))\n line.append(str(i[4]))\n\n success = str(i[5])\n if success == 'True':\n line.append('Success')\n else:\n line.append('Failed')\n\n table.append(line)\n\n self.__print_table(table)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprint a list in tabular format Taxonomy", "response": "def __print_table(table):\n \"\"\"Print a list in tabular format\n Based on https://stackoverflow.com/a/8356620\"\"\"\n\n col_width = [max(len(x) for x in col) for col in zip(*table)]\n print(\"| \" + \" | \".join(\"{:{}}\".format(x, col_width[i])\n for i, x in enumerate(table[0])) + \" |\")\n print(\"| \" + \" | \".join(\"{:{}}\".format('-' * col_width[i], col_width[i])\n for i, x in enumerate(table[0])) + \" |\")\n for line in table[1:]:\n print(\"| \" + \" | \".join(\"{:{}}\".format(x, col_width[i])\n for i, x in enumerate(line)) + \" |\")"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __is_applied(self, delta):\n\n query = \"\"\"\n SELECT id FROM {}\n WHERE version = '{}'\n AND checksum = '{}'\n AND success = 'TRUE'\n \"\"\".format(\n self.upgrades_table, delta.get_version(), delta.get_checksum())\n\n self.cursor.execute(query)\n if not self.cursor.fetchone():\n return False\n else:\n return True", "response": "Verifies if the delta file is already applied on the database."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __update_upgrades_table(self, delta):\n\n query = \"\"\"\n INSERT INTO {} (\n --id,\n version,\n description,\n type,\n script,\n checksum,\n installed_by,\n --installed_on,\n execution_time,\n success\n ) VALUES(\n '{}',\n '{}',\n {},\n '{}',\n '{}',\n '{}',\n 1,\n TRUE\n ) \"\"\".format(\n self.upgrades_table, delta.get_version(), delta.get_name(),\n delta.get_type(), delta.get_file(), delta.get_checksum(),\n self.__get_dbuser())\n\n self.cursor.execute(query)\n self.connection.commit()", "response": "Adds a new record to the upgrades table about the applied delta"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate the upgrades table", "response": "def create_upgrades_table(self):\n \"\"\"Create the upgrades information table\"\"\"\n\n query = \"\"\"CREATE TABLE IF NOT EXISTS {}\n (\n id serial NOT NULL,\n version character varying(50),\n description character varying(200) NOT NULL,\n type integer NOT NULL,\n script character varying(1000) NOT NULL,\n checksum character varying(32) NOT NULL,\n installed_by character varying(100) NOT NULL,\n installed_on timestamp without time zone NOT NULL DEFAULT now(),\n execution_time integer NOT NULL,\n success boolean NOT NULL,\n PRIMARY KEY (id)\n )\n \"\"\".format(self.upgrades_table)\n\n self.cursor.execute(query)\n self.connection.commit()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_baseline(self, version):\n pattern = re.compile(r\"^\\d+\\.\\d+\\.\\d+$\")\n if not re.match(pattern, version):\n raise ValueError('Wrong version format')\n\n query = \"\"\"\n INSERT INTO {} (\n version,\n description,\n type,\n script,\n checksum,\n installed_by,\n execution_time,\n success\n ) VALUES(\n '{}',\n '{}',\n {},\n '{}',\n '{}',\n '{}',\n 1,\n TRUE\n ) \"\"\".format(self.upgrades_table, version, 'baseline', 0,\n '', '', self.__get_dbuser())\n self.cursor.execute(query)\n self.connection.commit()", "response": "Set the baseline into the creation information table\n ."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_valid_delta_name(file):\n filename = basename(file)\n pattern = re.compile(Delta.FILENAME_PATTERN)\n if re.match(pattern, filename):\n return True\n return False", "response": "Return if a file has a valid delta file name"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_checksum(self):\n with open(self.file, 'rb') as f:\n cs = md5(f.read()).hexdigest()\n return cs", "response": "Return the md5 checksum of the delta file."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the type of the delta file.", "response": "def get_type(self):\n \"\"\"Return the type of the delta file.\n\n Returns\n -------\n type: int\n \"\"\"\n\n ext = self.match.group(5)\n\n if ext == 'pre.py':\n return DeltaType.PRE_PYTHON\n elif ext == 'pre.sql':\n return DeltaType.PRE_SQL\n elif ext == 'py':\n return DeltaType.PYTHON\n elif ext == 'sql':\n return DeltaType.SQL\n elif ext == 'post.py':\n return DeltaType.POST_PYTHON\n elif ext == 'post.sql':\n return DeltaType.POST_SQL"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the priority of the file in the current language.", "response": "def get_priority(self) -> int:\n \"\"\"\n Rerturns the priority of the file from 1 (pre) to 3 (post)\n :return: the priority\n \"\"\"\n dtype = self.get_type()\n if dtype & DeltaType.PRE:\n return 1\n elif dtype & DeltaType.POST:\n return 3\n else:\n return 2"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrun all the checks functions.", "response": "def run_checks(self):\n \"\"\"Run all the checks functions.\n\n Returns\n -------\n bool\n True if all the checks are true\n False otherwise\n dict\n Dictionary of lists of differences\n \"\"\"\n\n result = True\n differences_dict = {}\n\n if 'tables' not in self.ignore_list:\n tmp_result, differences_dict['tables'] = self.check_tables()\n result = False if not tmp_result else result\n if 'columns' not in self.ignore_list:\n tmp_result, differences_dict['columns'] = self.check_columns(\n 'views' not in self.ignore_list)\n result = False if not tmp_result else result\n if 'constraints' not in self.ignore_list:\n tmp_result, differences_dict['constraints'] = \\\n self.check_constraints()\n result = False if not tmp_result else result\n if 'views' not in self.ignore_list:\n tmp_result, differences_dict['views'] = self.check_views()\n result = False if not tmp_result else result\n if 'sequences' not in self.ignore_list:\n tmp_result, differences_dict['sequences'] = self.check_sequences()\n result = False if not tmp_result else result\n if 'indexes' not in self.ignore_list:\n tmp_result, differences_dict['indexes'] = self.check_indexes()\n result = False if not tmp_result else result\n if 'triggers' not in self.ignore_list:\n tmp_result, differences_dict['triggers'] = self.check_triggers()\n result = False if not tmp_result else result\n if 'functions' not in self.ignore_list:\n tmp_result, differences_dict['functions'] = self.check_functions()\n result = False if not tmp_result else result\n if 'rules' not in self.ignore_list:\n tmp_result, differences_dict['rules'] = self.check_rules()\n result = False if not tmp_result else result\n if self.verbose_level == 0:\n differences_dict = None\n return result, differences_dict"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef check_columns(self, check_views=True):\n if check_views:\n query = \"\"\"WITH table_list AS (\n SELECT table_schema, table_name\n FROM information_schema.tables\n WHERE table_schema NOT IN {}\n AND table_schema NOT LIKE 'pg\\_%'\n ORDER BY table_schema,table_name\n )\n SELECT isc.table_schema, isc.table_name, column_name,\n column_default, is_nullable, data_type,\n character_maximum_length::text, numeric_precision::text,\n numeric_precision_radix::text, datetime_precision::text\n FROM information_schema.columns isc,\n table_list tl\n WHERE isc.table_schema = tl.table_schema\n AND isc.table_name = tl.table_name\n ORDER BY isc.table_schema, isc.table_name, column_name\n \"\"\".format(self.exclude_schema)\n\n else:\n query = \"\"\"WITH table_list AS (\n SELECT table_schema, table_name\n FROM information_schema.tables\n WHERE table_schema NOT IN {}\n AND table_schema NOT LIKE 'pg\\_%'\n AND table_type NOT LIKE 'VIEW'\n ORDER BY table_schema,table_name\n )\n SELECT isc.table_schema, isc.table_name, column_name,\n column_default, is_nullable, data_type,\n character_maximum_length::text, numeric_precision::text,\n numeric_precision_radix::text, datetime_precision::text\n FROM information_schema.columns isc,\n table_list tl\n WHERE isc.table_schema = tl.table_schema\n AND isc.table_name = tl.table_name\n ORDER BY isc.table_schema, isc.table_name, column_name\n \"\"\".format(self.exclude_schema)\n\n return self.__check_equals(query)", "response": "Check if the columns in all tables and views are equals."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck if the rules are equal.", "response": "def check_rules(self):\n \"\"\"Check if the rules are equals.\n\n Returns\n -------\n bool\n True if the rules are the same\n False otherwise\n list\n A list with the differences\n \"\"\"\n query = \"\"\"\n select n.nspname as rule_schema,\n c.relname as rule_table,\n case r.ev_type\n when '1' then 'SELECT'\n when '2' then 'UPDATE'\n when '3' then 'INSERT'\n when '4' then 'DELETE'\n else 'UNKNOWN'\n end as rule_event\n from pg_rewrite r\n join pg_class c on r.ev_class = c.oid\n left join pg_namespace n on n.oid = c.relnamespace\n left join pg_description d on r.oid = d.objoid\n WHERE n.nspname NOT IN {excl}\n AND r.rulename != '_RETURN'\n AND n.nspname NOT LIKE 'pg\\_%'\n ORDER BY n.nspname, c.relname, rule_event\n \"\"\".format(excl=self.exclude_schema)\n\n return self.__check_equals(query)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __check_equals(self, query):\n self.cur1.execute(query)\n records1 = self.cur1.fetchall()\n\n self.cur2.execute(query)\n records2 = self.cur2.fetchall()\n\n result = True\n differences = []\n\n d = difflib.Differ()\n records1 = [str(x) for x in records1]\n records2 = [str(x) for x in records2]\n\n for line in d.compare(records1, records2):\n if line[0] in ('-', '+'):\n result = False\n if self.verbose_level == 1:\n differences.append(line[0:79])\n elif self.verbose_level == 2:\n differences.append(line)\n\n return result, differences", "response": "Check if the query results on the two databases are equals."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ask_for_confirmation(prompt=None, resp=False):\n global input\n if prompt is None:\n prompt = 'Confirm'\n\n if resp:\n prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')\n else:\n prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')\n\n while True:\n # Fix for Python2. In python3 raw_input() is now input()\n try:\n input = raw_input\n except NameError:\n pass\n ans = input(prompt)\n if not ans:\n return resp\n if ans not in ['y', 'Y', 'n', 'N']:\n print('please enter y or n.')\n continue\n if ans == 'y' or ans == 'Y':\n return True\n if ans == 'n' or ans == 'N':\n return False", "response": "Prompts the user for confirmation from the user."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef handle_target(self, request, controller_args, controller_kwargs):\n self.request = request\n super(AuthDecorator, self).handle_target(request, controller_args, controller_kwargs)\n del self.request", "response": "This is called by the base class to set the request attribute for the current request and then call the base class s handle_target method."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get(self, uri, query=None, **kwargs):\n return self.fetch('get', uri, query, **kwargs)", "response": "make a GET request"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmake a POST request", "response": "def post(self, uri, body=None, **kwargs):\n \"\"\"make a POST request\"\"\"\n return self.fetch('post', uri, kwargs.pop(\"query\", {}), body, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmaking a DELETE request", "response": "def delete(self, uri, query=None, **kwargs):\n \"\"\"make a DELETE request\"\"\"\n return self.fetch('delete', uri, query, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nwrapping method that all the top level methods use to actually get the request", "response": "def fetch(self, method, uri, query=None, body=None, **kwargs):\n \"\"\"\n wrapper method that all the top level methods (get, post, etc.) use to actually\n make the request\n \"\"\"\n if not query: query = {}\n fetch_url = self.get_fetch_url(uri, query)\n\n args = [fetch_url]\n\n kwargs.setdefault(\"timeout\", self.timeout)\n kwargs[\"headers\"] = self.get_fetch_headers(method, kwargs.get(\"headers\", {}))\n\n if body:\n if self.is_json(kwargs[\"headers\"]):\n kwargs['json'] = self.get_fetch_body(body)\n else:\n kwargs['data'] = self.get_fetch_body(body)\n\n res = self.get_fetch_request(method, *args, **kwargs)\n #res = requests.request(method, *args, **kwargs)\n res = self.get_fetch_response(res)\n self.response = res\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_fetch_headers(self, method, headers):\n all_headers = self.headers.copy()\n if headers:\n all_headers.update(headers)\n return Headers(all_headers)", "response": "merge headers with global class headers"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_fetch_request(self, method, fetch_url, *args, **kwargs):\n return requests.request(method, fetch_url, *args, **kwargs)", "response": "This method is handy if you want to modify the request right before passing it\n to requests. request"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_fetch_response(self, res):\n res.code = res.status_code\n res.headers = Headers(res.headers)\n res._body = None\n res.body = ''\n body = res.content\n if body:\n if self.is_json(res.headers):\n res._body = res.json()\n else:\n res._body = body\n\n res.body = String(body, res.encoding)\n\n return res", "response": "get the response object from the server"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns true if content_type is a json content type", "response": "def is_json(self, headers):\n \"\"\"return true if content_type is a json content type\"\"\"\n ret = False\n ct = headers.get(\"content-type\", \"\").lower()\n if ct:\n ret = ct.lower().rfind(\"json\") >= 0\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the description of this endpoint", "response": "def desc(self):\n \"\"\"return the description of this endpoint\"\"\"\n doc = None\n def visit_FunctionDef(node):\n \"\"\" https://docs.python.org/2/library/ast.html#ast.NodeVisitor.visit \"\"\"\n if node.name != self.method_name:\n return\n\n doc = ast.get_docstring(node)\n raise StopIteration(doc if doc else \"\")\n\n target = self.controller.controller_class\n try:\n node_iter = ast.NodeVisitor()\n node_iter.visit_FunctionDef = visit_FunctionDef\n node_iter.visit(ast.parse(inspect.getsource(target)))\n\n except StopIteration as e:\n doc = str(e)\n\n if not doc: doc = \"\"\n return doc"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef params(self):\n ret = {}\n for rd in self.decorators:\n args = rd.args\n kwargs = rd.kwargs\n if param in rd:\n is_required = kwargs.get('required', 'default' not in kwargs)\n ret[args[0]] = {'required': is_required, 'other_names': args[1:], 'options': kwargs}\n\n return ret", "response": "return information about the params that the given http option takes"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all the decorators of all the option methods in the class", "response": "def decorators(self):\n \"\"\"Get all the decorators of all the option methods in the class\n\n http://stackoverflow.com/questions/5910703/ specifically, I used this\n answer http://stackoverflow.com/a/9580006\n \"\"\"\n res = collections.defaultdict(list)\n mmap = {}\n\n def get_val(na, default=None):\n ret = None\n if isinstance(na, ast.Num):\n repr_n = repr(na.n)\n val = na.n\n vtype = float if '.' in repr_n else int\n ret = vtype(val)\n\n elif isinstance(na, ast.Str):\n ret = str(na.s)\n\n elif isinstance(na, ast.Name):\n # http://stackoverflow.com/questions/12700893/\n ret = getattr(builtins, na.id, None)\n if not ret:\n ret = na.id\n if ret == 'True':\n ret = True\n elif ret == 'False':\n ret = False\n\n elif isinstance(na, ast.Dict):\n if na.keys:\n ret = {get_val(na_[0]): get_val(na_[1]) for na_ in zip(na.keys, na.values)}\n else:\n ret = {}\n\n elif isinstance(na, (ast.List, ast.Tuple)):\n if na.elts:\n ret = [get_val(na_) for na_ in na.elts]\n else:\n ret = []\n\n if isinstance(na, ast.Tuple):\n ret = tuple(ret)\n\n else:\n ret = default\n\n return ret\n\n def is_super(childnode, parentnode):\n \"\"\"returns true if child node has a super() call to parent node\"\"\"\n ret = False\n for n in childnode.body:\n if not isinstance(n, ast.Expr): continue\n\n try:\n func = n.value.func\n func_name = func.attr\n if func_name == parentnode.name:\n ret = isinstance(func.value, ast.Call)\n break\n\n except AttributeError as e:\n ret = False\n\n return ret\n\n def visit_FunctionDef(node):\n \"\"\" https://docs.python.org/2/library/ast.html#ast.NodeVisitor.visit \"\"\"\n\n add_decs = True\n if node.name in res:\n add_decs = is_super(mmap[node.name], node)\n\n mmap[node.name] = node\n\n if add_decs:\n for n in node.decorator_list:\n d = {}\n name = ''\n args = []\n kwargs = {}\n if isinstance(n, ast.Call):\n name = n.func.attr if isinstance(n.func, ast.Attribute) else n.func.id\n for an in n.args:\n args.append(get_val(an))\n\n for an in n.keywords:\n kwargs[an.arg] = get_val(an.value)\n\n else:\n name = n.attr if isinstance(n, ast.Attribute) else n.id\n\n d = {\n \"name\": name,\n \"args\": args,\n \"kwargs\": kwargs\n }\n m = self.module\n decor = getattr(m, name, None)\n if decor:\n d[\"decorator\"] = decor\n\n #res[node.name].append((name, args, kwargs))\n res[node.name].append(self.decorator_class(**d))\n\n node_iter = ast.NodeVisitor()\n node_iter.visit_FunctionDef = visit_FunctionDef\n for target_cls in inspect.getmro(self.controller_class):\n if target_cls == Controller: break\n node_iter.visit(ast.parse(inspect.getsource(target_cls)))\n\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef desc(self):\n doc = inspect.getdoc(self.controller_class)\n if not doc: doc = ''\n return doc", "response": "return the description of this endpoint"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dictionary of all the http methods that this class supports", "response": "def methods(self):\n \"\"\"\n return the supported http method options that this class supports\n return what http method options this endpoint supports (eg, POST, GET)\n\n http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html\n\n :returns: dict, each http method (eg, GET, POST) will have a key with the value\n being every method from the controller that can satisfy the http method\n \"\"\"\n ret = {}\n method_regex = re.compile(r\"^[A-Z][A-Z0-9]+(_|$)\")\n controller_methods = inspect.getmembers(self.controller_class)\n for controller_method_name, controller_method in controller_methods:\n if controller_method_name.startswith('_'): continue\n\n if method_regex.match(controller_method_name):\n method = self.method_class(\n controller_method_name,\n controller_method,\n controller=self\n )\n ret.setdefault(method.name, [])\n ret[method.name].append(method)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a call object that has endpoints understandable request and response tuples", "response": "def create_call(self, raw_request, **kwargs):\n \"\"\"create a call object that has endpoints understandable request and response\n instances\"\"\"\n req = self.create_request(raw_request, **kwargs)\n res = self.create_response(**kwargs)\n rou = self.create_router(**kwargs)\n c = self.call_class(req, res, rou)\n return c"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef target(self, request, key, limit, ttl):\n ret = True\n if key:\n #backend = self.create_backend()\n #method = getattr(backend, \"normalize_limit\", None)\n #if method:\n # limit = method(request, limit)\n #method = getattr(backend, \"normalize_ttl\", None)\n #if method:\n # ttl = method(request, ttl)\n #ret = backend.target(request, key, limit, ttl)\n ret = super(RateLimitDecorator, self).target(request, key, limit, ttl)\n else:\n logger.warn(\"No ratelimit key found for {}\".format(request.path))\n\n return ret", "response": "this will only run the request if the key has a value"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef decorate(self, func, limit=0, ttl=0, *anoop, **kwnoop):\n self.limit = int(limit)\n self.ttl = int(ttl)\n return super(RateLimitDecorator, self).decorate(func, target=None, *anoop, **kwnoop)", "response": "decorate with limit and ttl"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nmakes limit and ttl required", "response": "def decorate(self, func, limit, ttl, *anoop, **kwnoop):\n \"\"\"make limit and ttl required\"\"\"\n return super(ratelimit, self).decorate(func, limit, ttl, *anoop, **kwnoop)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef encode(cls, s):\n b = ByteString(s)\n be = base64.b64encode(b).strip()\n return String(be)", "response": "converts a plain text string to base64 encoded string"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decode(cls, s):\n b = ByteString(s)\n bd = base64.b64decode(b)\n return String(bd)", "response": "decodes a base64 string to plain text"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_type(cls, val):\n mt = \"\"\n index = val.rfind(\".\")\n if index == -1:\n val = \"fake.{}\".format(val)\n elif index == 0:\n val = \"fake{}\".format(val)\n\n mt = mimetypes.guess_type(val)[0]\n if mt is None:\n mt = \"\"\n\n return mt", "response": "return the mimetype from the given string value\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _sort(self, a, b):\n '''\n sort the headers according to rfc 2616 so when __iter__ is called, the accept media types are\n in order from most preferred to least preferred\n '''\n ret = 0\n\n # first we check q, higher values win:\n if a[1] != b[1]:\n ret = cmp(a[1], b[1])\n else:\n found = False\n for i in range(2):\n ai = a[0][i]\n bi = b[0][i]\n if ai == '*':\n if bi != '*':\n ret = -1\n found = True\n break\n else:\n # both *, more verbose params win\n ret = cmp(len(a[2]), len(b[2]))\n found = True\n break\n elif bi == '*':\n ret = 1\n found = True\n break\n\n if not found:\n ret = cmp(len(a[2]), len(b[2]))\n\n return ret", "response": "sort the accept headers according to rfc 2616 so when __iter__ is called the accept media types are sorted according to rfc 2616 so when __iter__ is called the accept media types are sorted by most preferred to least preferred."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\niterating all the accept media types that match media_type and yield all matching things", "response": "def filter(self, media_type, **params):\n \"\"\"\n iterate all the accept media types that match media_type\n\n media_type -- string -- the media type to filter by\n **params -- dict -- further filter by key: val\n\n return -- generator -- yields all matching media type info things\n \"\"\"\n mtype, msubtype = self._split_media_type(media_type)\n for x in self.__iter__():\n # all the params have to match to make the media type valid\n matched = True\n for k, v in params.items():\n if x[2].get(k, None) != v:\n matched = False\n break\n\n if matched:\n if x[0][0] == '*':\n if x[0][1] == '*':\n yield x\n\n elif x[0][1] == msubtype:\n yield x\n\n elif mtype == '*':\n if msubtype == '*':\n yield x\n\n elif x[0][1] == msubtype:\n yield x\n\n elif x[0][0] == mtype:\n if msubtype == '*':\n yield x\n\n elif x[0][1] == '*':\n yield x\n\n elif x[0][1] == msubtype:\n yield x"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_request(self, raw_request, **kwargs):\n r = self.request_class()\n for k, v in raw_request.items():\n if k.startswith('HTTP_'):\n r.set_header(k[5:], v)\n else:\n r.environ[k] = v\n\n r.method = raw_request['REQUEST_METHOD']\n r.path = raw_request['PATH_INFO']\n r.query = raw_request['QUERY_STRING']\n\n # handle headers not prefixed with http\n for k, t in {'CONTENT_TYPE': None, 'CONTENT_LENGTH': int}.items():\n v = r.environ.pop(k, None)\n if v:\n r.set_header(k, t(v) if t else v)\n\n if 'wsgi.input' in raw_request:\n\n if \"CONTENT_LENGTH\" in raw_request and int(r.get_header(\"CONTENT_LENGTH\", 0)) <= 0:\n r.body_kwargs = {}\n\n else:\n if r.get_header('transfer-encoding', \"\").lower().startswith('chunked'):\n raise IOError(\"Server does not support chunked requests\")\n\n else:\n r.body_input = raw_request['wsgi.input']\n\n else:\n r.body_kwargs = {}\n\n return r", "response": "create a new request object from a WSGI request object"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef application(self, v):\n self._application = v\n self.backend.set_app(v)", "response": "allow overriding of the application factory"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef find_module_path():\n master_modname = __name__.split(\".\", 1)[0]\n master_module = sys.modules[master_modname]\n #return os.path.dirname(os.path.realpath(os.path.join(inspect.getsourcefile(endpoints), \"..\")))\n path = os.path.dirname(inspect.getsourcefile(master_module))\n return path", "response": "find where the master module is located"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _convert_string_name(self, k):\n k = String(k, \"iso-8859-1\")\n klower = k.lower().replace('_', '-')\n bits = klower.split('-')\n return \"-\".join((bit.title() for bit in bits))", "response": "converts things like FOO_BAR to Foo - Bar which is the normal form"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef uri(self):\n uristring = self.path\n if self.query:\n uristring += \"?{}\".format(self.query)\n if self.fragment:\n uristring += \"#{}\".format(self.fragment)\n\n return uristring", "response": "return the uri which is everything but base"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_query(cls, query):\n if not query: return {}\n\n d = {}\n # https://docs.python.org/2/library/urlparse.html\n for k, kv in urlparse.parse_qs(query, True, strict_parsing=True).items():\n #k = k.rstrip(\"[]\") # strip out php type array designated variables\n if len(kv) > 1:\n d[k] = kv\n else:\n d[k] = kv[0]\n\n return d", "response": "return name = val&name2 = val2 strings into dict"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _normalize_params(self, *paths, **query_kwargs):\n kwargs = {}\n\n if paths:\n fragment = paths[-1]\n if fragment:\n if fragment.startswith(\"#\"):\n kwargs[\"fragment\"] = fragment\n paths.pop(-1)\n\n kwargs[\"path\"] = \"/\".join(self.normalize_paths(*paths))\n\n kwargs[\"query_kwargs\"] = query_kwargs\n return kwargs", "response": "this handles the special case of the helper methods"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef split_hostname_from_port(cls, hostname):\n bits = hostname.split(\":\", 2)\n p = None\n d = bits[0]\n if len(bits) == 2:\n p = int(bits[1])\n\n return d, p", "response": "given a hostname and port return a tuple ( hostname port"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef controller(self, *paths, **query_kwargs):\n kwargs = self._normalize_params(*paths, **query_kwargs)\n if self.controller_path:\n if \"path\" in kwargs:\n paths = self.normalize_paths(self.controller_path, kwargs[\"path\"])\n kwargs[\"path\"] = \"/\".join(paths)\n else:\n kwargs[\"path\"] = self.controller_path\n return self.create(self.root, **kwargs)", "response": "create a new url object using the controller path as the base path"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef base(self, *paths, **query_kwargs):\n kwargs = self._normalize_params(*paths, **query_kwargs)\n if self.path:\n if \"path\" in kwargs:\n paths = self.normalize_paths(self.path, kwargs[\"path\"])\n kwargs[\"path\"] = \"/\".join(paths)\n else:\n kwargs[\"path\"] = self.path\n return self.create(self.root, **kwargs)", "response": "create a new url object using the current base path as a base"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef host(self, *paths, **query_kwargs):\n kwargs = self._normalize_params(*paths, **query_kwargs)\n return self.create(self.root, **kwargs)", "response": "create a new url object using the host"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef accept_encoding(self):\n # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset\n ret = \"\"\n accept_encoding = self.get_header(\"Accept-Charset\", \"\")\n if accept_encoding:\n bits = re.split(r\"\\s+\", accept_encoding)\n bits = bits[0].split(\";\")\n ret = bits[0]\n return ret", "response": "The encoding the client requested the response to use"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef encoding(self):\n encoding = None\n ct = self.get_header('content-type')\n if ct:\n ah = AcceptHeader(ct)\n if ah.media_types:\n encoding = ah.media_types[0][2].get(\"charset\", None)\n\n return encoding", "response": "the character encoding of the request"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef access_token(self):\n access_token = self.get_auth_bearer()\n if not access_token:\n access_token = self.query_kwargs.get('access_token', '')\n if not access_token:\n access_token = self.body_kwargs.get('access_token', '')\n\n return access_token", "response": "return an Oauth 2. 0 Bearer access token if it can be found"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntrying and get Oauth 2. 0 client id and secret from the request body or query string", "response": "def client_tokens(self):\n \"\"\"try and get Oauth 2.0 client id and secret first from basic auth header,\n then from GET or POST parameters\n\n return -- tuple -- client_id, client_secret\n \"\"\"\n client_id, client_secret = self.get_auth_basic()\n if not client_id and not client_secret:\n client_id = self.query_kwargs.get('client_id', '')\n client_secret = self.query_kwargs.get('client_secret', '')\n if not client_id and not client_secret:\n client_id = self.body_kwargs.get('client_id', '')\n client_secret = self.body_kwargs.get('client_secret', '')\n\n return client_id, client_secret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ips(self):\n r = []\n names = ['X_FORWARDED_FOR', 'CLIENT_IP', 'X_REAL_IP', 'X_FORWARDED', \n 'X_CLUSTER_CLIENT_IP', 'FORWARDED_FOR', 'FORWARDED', 'VIA',\n 'REMOTE_ADDR']\n\n for name in names:\n vs = self.get_header(name, '')\n if vs:\n r.extend(map(lambda v: v.strip(), vs.split(',')))\n\n vs = self.environ.get(name, '')\n if vs:\n r.extend(map(lambda v: v.strip(), vs.split(',')))\n\n return r", "response": "return all the possible ips of this request this will include public and private ips"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ip(self):\n r = ''\n\n # this was compiled from here:\n # https://github.com/un33k/django-ipware\n # http://www.ietf.org/rfc/rfc3330.txt (IPv4)\n # http://www.ietf.org/rfc/rfc5156.txt (IPv6)\n # https://en.wikipedia.org/wiki/Reserved_IP_addresses\n format_regex = re.compile(r'\\s')\n ip_regex = re.compile(r'^(?:{})'.format(r'|'.join([\n r'0\\.', # reserved for 'self-identification'\n r'10\\.', # class A\n r'169\\.254', # link local block\n r'172\\.(?:1[6-9]|2[0-9]|3[0-1])\\.', # class B\n r'192\\.0\\.2\\.', # documentation/examples\n r'192\\.168', # class C\n r'255\\.{3}', # broadcast address\n r'2001\\:db8', # documentation/examples\n r'fc00\\:', # private\n r'fe80\\:', # link local unicast\n r'ff00\\:', # multicast\n r'127\\.', # localhost\n r'\\:\\:1' # localhost\n ])))\n\n ips = self.ips\n for ip in ips:\n if not format_regex.search(ip) and not ip_regex.match(ip):\n r = ip\n break\n\n return r", "response": "return the public ip address"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the full request url as an Url instance", "response": "def url(self):\n \"\"\"return the full request url as an Url() instance\"\"\"\n scheme = self.scheme\n host = self.host\n path = self.path\n query = self.query\n port = self.port\n\n # normalize the port\n host_domain, host_port = Url.split_hostname_from_port(host)\n if host_port:\n port = host_port\n\n controller_path = \"\"\n if self.controller_info:\n controller_path = self.controller_info.get(\"path\", \"\")\n\n u = Url(\n scheme=scheme,\n hostname=host,\n path=path,\n query=query,\n port=port,\n controller_path=controller_path,\n )\n return u"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nquerying part of a url", "response": "def query(self):\n \"\"\"query_string part of a url (eg, http://host.com/path?query=string)\"\"\"\n self._query = query = \"\"\n\n query_kwargs = self.query_kwargs\n if query_kwargs: query = urlencode(query_kwargs, doseq=True)\n return query"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef query_kwargs(self):\n self._query_kwargs = query_kwargs = {}\n query = self.query\n if query: query_kwargs = self._parse_query_str(query)\n return query_kwargs", "response": "Returns a dictionary of keyword arguments for the query"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef body(self):\n body = None\n if self.body_input:\n body = self.body_input.read(int(self.get_header('content-length', -1)))\n\n return body", "response": "return the raw version of the body"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef body_kwargs(self):\n body_kwargs = {}\n ct = self.get_header(\"content-type\")\n if ct:\n ct = ct.lower()\n if ct.rfind(\"json\") >= 0:\n body = self.body\n if body:\n body_kwargs = json.loads(body)\n\n else:\n if self.body_input:\n body = RequestBody(\n fp=self.body_input,\n headers=self.headers,\n environ=self.environ\n #environ=self.raw_request\n )\n body_kwargs = dict(body)\n\n else:\n body = self.body\n if body:\n body_kwargs = self._parse_query_str(body)\n\n return body_kwargs", "response": "return the kwargs for the request body"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncombine GET and POST params to be passed to the controller", "response": "def kwargs(self):\n \"\"\"combine GET and POST params to be passed to the controller\"\"\"\n kwargs = dict(self.query_kwargs)\n kwargs.update(self.body_kwargs)\n\n return kwargs"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef version(self, content_type=\"*/*\"):\n v = \"\"\n accept_header = self.get_header('accept', \"\")\n if accept_header:\n a = AcceptHeader(accept_header)\n for mt in a.filter(content_type):\n v = mt[2].get(\"version\", \"\")\n if v: break\n\n return v", "response": "get the version of the post object"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the bearer token in the authorization header if it exists", "response": "def get_auth_bearer(self):\n \"\"\"return the bearer token in the authorization header if it exists\"\"\"\n access_token = ''\n auth_header = self.get_header('authorization')\n if auth_header:\n m = re.search(r\"^Bearer\\s+(\\S+)$\", auth_header, re.I)\n if m: access_token = m.group(1)\n\n return access_token"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_auth_basic(self):\n username = ''\n password = ''\n auth_header = self.get_header('authorization')\n if auth_header:\n m = re.search(r\"^Basic\\s+(\\S+)$\", auth_header, re.I)\n if m:\n auth_str = Base64.decode(m.group(1))\n username, password = auth_str.split(':', 1)\n\n return username, password", "response": "return the username and password of a basic auth header if it exists"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef normalize_body(self, b):\n if b is None: return ''\n\n if self.is_json():\n # TODO ???\n # I don't like this, if we have a content type but it isn't one\n # of the supported ones we were returning the exception, which threw\n # Jarid off, but now it just returns a string, which is not best either\n # my thought is we could have a body_type_subtype method that would \n # make it possible to easily handle custom types\n # eg, \"application/json\" would become: self.body_application_json(b, is_error)\n b = json.dumps(b, cls=ResponseBody)\n\n else:\n # just return a string representation of body if no content type\n b = String(b, self.encoding)\n\n return b", "response": "return the body as a string formatted to the appropriate content type\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef normalize_target_params(self, request, controller_args, controller_kwargs):\n return [], dict(\n request=request,\n controller_args=controller_args, \n controller_kwargs=controller_kwargs\n )", "response": "this method is used to normalize the target params for the target method"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef decorate(self, func, target, *anoop, **kwnoop):\n if target:\n self.target = target\n\n def decorated(decorated_self, *args, **kwargs):\n self.handle_target(\n request=decorated_self.request,\n controller_args=args,\n controller_kwargs=kwargs\n )\n return func(decorated_self, *args, **kwargs)\n\n return decorated", "response": "decorate the passed in func calling target when func is called\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nnormalize the flags to make sure needed values are there", "response": "def normalize_flags(self, flags):\n \"\"\"normalize the flags to make sure needed values are there\n\n after this method is called self.flags is available\n\n :param flags: the flags that will be normalized\n \"\"\"\n flags['type'] = flags.get('type', None)\n paction = flags.get('action', 'store')\n if paction == 'store_false':\n flags['default'] = True \n flags['type'] = bool\n\n elif paction == 'store_true':\n flags['default'] = False\n flags['type'] = bool\n\n prequired = False if 'default' in flags else flags.get('required', True)\n\n flags[\"action\"] = paction\n flags[\"required\"] = prequired\n self.flags = flags"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef normalize_type(self, names):\n self.name = names[0]\n self.is_kwarg = False\n self.is_arg = False\n self.names = []\n\n try:\n # http://stackoverflow.com/a/16488383/5006 uses ask forgiveness because\n # of py2/3 differences of integer check\n self.index = int(self.name)\n self.name = \"\"\n self.is_arg = True\n\n except ValueError:\n self.is_kwarg = True\n self.names = names", "response": "Decide if this param is an arg or a kwarg and set appropriate internal flags"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef normalize_param(self, slf, args, kwargs):\n if self.is_kwarg:\n kwargs = self.normalize_kwarg(slf.request, kwargs)\n else:\n args = self.normalize_arg(slf.request, args)\n return slf, args, kwargs", "response": "this function will try to find the param and\n put it in args if it has a default and stuff"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_kwarg(self, request, names, required, default, kwargs):\n val = default\n found_name = ''\n for name in names:\n if name in kwargs:\n val = kwargs[name]\n found_name = name\n break\n\n if not found_name and required:\n raise ValueError(\"required param {} does not exist\".format(self.name))\n\n return found_name, val", "response": "actually try to retrieve names key from params dict\n\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef normalize_val(self, request, val):\n flags = self.flags\n paction = flags['action']\n ptype = flags['type']\n pchoices = flags.get('choices', None)\n allow_empty = flags.get('allow_empty', False)\n min_size = flags.get('min_size', None)\n max_size = flags.get('max_size', None)\n regex = flags.get('regex', None)\n\n if paction in set(['store_list']):\n if isinstance(val, list) and len(val) > 1:\n raise ValueError(\"too many values for param\")\n\n if isinstance(val, basestring):\n val = list(val.split(','))\n\n else:\n val = list(val)\n\n elif paction in set(['append', 'append_list']):\n if not isinstance(val, list):\n val = [val]\n\n if paction == 'append_list':\n vs = []\n for v in val:\n if isinstance(v, basestring):\n vs.extend(v.split(','))\n else:\n vs.append(v)\n\n val = vs\n\n else:\n if paction not in set(['store', 'store_false', 'store_true']):\n raise RuntimeError('unknown param action {}'.format(paction))\n\n if regex:\n failed = False\n if isinstance(regex, basestring):\n if not re.search(regex, val): failed = True\n else:\n if not regex.search(val): failed = True\n\n if failed:\n raise ValueError(\"param failed regex check\")\n\n if ptype:\n if isinstance(val, list) and ptype != list:\n val = list(map(ptype, val))\n\n else:\n if isinstance(ptype, type):\n if issubclass(ptype, bool):\n if val in set(['true', 'True', '1']):\n val = True\n elif val in set(['false', 'False', '0']):\n val = False\n else:\n val = ptype(val)\n\n elif issubclass(ptype, str):\n charset = request.encoding\n if is_py2:\n val = ptype(ByteString(val, charset))\n else:\n val = ptype(String(val, charset))\n\n# if charset and isinstance(val, unicode):\n# val = val.encode(charset)\n# else:\n# val = ptype(val)\n\n else:\n val = ptype(val)\n\n else:\n val = ptype(val)\n\n if pchoices:\n if isinstance(val, list) and ptype != list:\n for v in val:\n if v not in pchoices:\n raise ValueError(\"param value {} not in choices {}\".format(v, pchoices))\n\n else:\n if val not in pchoices:\n raise ValueError(\"param value {} not in choices {}\".format(val, pchoices))\n\n # at some point this if statement is just going to be too ridiculous\n # FieldStorage check is because of this bug https://bugs.python.org/issue19097\n if not isinstance(val, cgi.FieldStorage):\n if not allow_empty and val is not False and not val:\n if 'default' not in flags:\n raise ValueError(\"param was empty\")\n\n if min_size is not None:\n failed = False\n if isinstance(val, (int, float)):\n if val < min_size: failed = True\n else:\n if len(val) < min_size: failed = True\n\n if failed:\n raise ValueError(\"param was smaller than {}\".format(min_size))\n\n if max_size is not None:\n failed = False\n if isinstance(val, (int, float)):\n if val > max_size: failed = True\n else:\n if len(val) > max_size: failed = True\n\n if failed:\n raise ValueError(\"param was bigger than {}\".format(max_size))\n\n return val", "response": "This will take the value and make sure it meets expectations"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef connect(self, path=\"\", headers=None, query=None, timeout=0, **kwargs):\n ret = None\n ws_url = self.get_fetch_url(path, query)\n ws_headers = self.get_fetch_headers(\"GET\", headers)\n ws_headers = ['{}: {}'.format(h[0], h[1]) for h in ws_headers.items() if h[1]]\n timeout = self.get_timeout(timeout=timeout, **kwargs)\n\n self.set_trace(kwargs.pop(\"trace\", False))\n #pout.v(websocket_url, websocket_headers, self.query_kwargs, self.headers)\n\n try:\n logger.debug(\"{} connecting to {}\".format(self.client_id, ws_url))\n self.ws = websocket.create_connection(\n ws_url,\n header=ws_headers,\n timeout=timeout,\n sslopt={'cert_reqs':ssl.CERT_NONE},\n )\n\n ret = self.recv_callback(callback=lambda r: r.uuid == \"CONNECT\")\n if ret.code >= 400:\n raise IOError(\"Failed to connect with code {}\".format(ret.code))\n\n# self.headers = headers\n# self.query_kwargs = query_kwargs\n\n except websocket.WebSocketTimeoutException:\n raise IOError(\"Failed to connect within {} seconds\".format(timeout))\n\n except websocket.WebSocketException as e:\n raise IOError(\"Failed to connect with error: {}\".format(e))\n\n except socket.error as e:\n # this is an IOError, I just wanted to be aware of that, most common\n # problem is: [Errno 111] Connection refused\n raise\n\n return ret", "response": "Connect to the most common ACS - related API."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fetch(self, method, path, query=None, body=None, timeout=0, **kwargs):\n ret = None\n if not query: query = {}\n if not body: body = {}\n query.update(body) # body takes precedence\n body = query\n\n self.send_count += 1\n payload = self.get_fetch_request(method, path, body)\n attempts = 1\n max_attempts = self.attempts\n success = False\n\n while not success:\n kwargs['timeout'] = timeout\n try:\n try:\n if not self.connected: self.connect(path)\n with self.wstimeout(**kwargs) as timeout:\n kwargs['timeout'] = timeout\n\n logger.debug('{} send {} attempt {}/{} with timeout {}'.format(\n self.client_id,\n payload.uuid,\n attempts,\n max_attempts,\n timeout\n ))\n\n sent_bits = self.ws.send(payload.payload)\n logger.debug('{} sent {} bytes'.format(self.client_id, sent_bits))\n if sent_bits:\n ret = self.fetch_response(payload, **kwargs)\n if ret:\n success = True\n\n except websocket.WebSocketConnectionClosedException as e:\n self.ws.shutdown()\n raise IOError(\"connection is not open but reported it was open: {}\".format(e))\n\n except (IOError, TypeError) as e:\n logger.debug('{} error on send attempt {}: {}'.format(self.client_id, attempts, e))\n success = False\n\n finally:\n if not success:\n attempts += 1\n if attempts > max_attempts:\n raise\n\n else:\n timeout *= 2\n if (attempts / max_attempts) > 0.50:\n logger.debug(\n \"{} closing and re-opening connection for next attempt\".format(self.client_id)\n )\n self.close()\n\n return ret", "response": "send a message to the server"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef fetch_response(self, req_payload, **kwargs):\n if req_payload.uuid:\n uuids = set([req_payload.uuid, \"CONNECT\"])\n def callback(res_payload):\n #pout.v(req_payload, res_payload)\n #ret = req_payload.uuid == res_payload.uuid or res_payload.uuid == \"CONNECT\"\n ret = res_payload.uuid in uuids\n if ret:\n logger.debug('{} received {} response for {}'.format(\n self.client_id,\n res_payload.code,\n res_payload.uuid,\n ))\n return ret\n\n res_payload = self.recv_callback(callback, **kwargs)\n\n return res_payload", "response": "fetch_response - send a response to the server"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nping the server and check the status of the user s agent.", "response": "def ping(self, timeout=0, **kwargs):\n \"\"\"THIS DOES NOT WORK, UWSGI DOES NOT RESPOND TO PINGS\"\"\"\n\n # http://stackoverflow.com/a/2257449/5006\n def rand_id(size=8, chars=string.ascii_uppercase + string.digits):\n return ''.join(random.choice(chars) for _ in range(size))\n\n payload = rand_id()\n self.ws.ping(payload)\n opcode, data = self.recv_raw(timeout, [websocket.ABNF.OPCODE_PONG], **kwargs)\n if data != payload:\n raise IOError(\"Pinged server but did not receive correct pong\")"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreceiving messages and validate them with the callback", "response": "def recv_callback(self, callback, **kwargs):\n \"\"\"receive messages and validate them with the callback, if the callback \n returns True then the message is valid and will be returned, if False then\n this will try and receive another message until timeout is 0\"\"\"\n payload = None\n timeout = self.get_timeout(**kwargs)\n full_timeout = timeout\n while timeout > 0.0:\n kwargs['timeout'] = timeout\n start = time.time()\n payload = self.recv(**kwargs)\n if callback(payload):\n break\n payload = None\n stop = time.time()\n elapsed = stop - start\n timeout -= elapsed\n\n if not payload:\n raise IOError(\"recv_callback timed out in {}\".format(full_timeout))\n\n return payload"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a new controller instance for the request and response.", "response": "def create_controller(self):\n \"\"\"Create a controller to handle the request\n\n :returns: Controller, this Controller instance should be able to handle\n the request\n \"\"\"\n body = None\n req = self.request\n res = self.response\n rou = self.router\n con = None\n\n controller_info = {}\n try:\n controller_info = rou.find(req, res)\n\n except IOError as e:\n logger.warning(str(e), exc_info=True)\n raise CallError(\n 408,\n \"The client went away before the request body was retrieved.\"\n )\n\n except (ImportError, AttributeError, TypeError) as e:\n exc_info = sys.exc_info()\n logger.warning(str(e), exc_info=exc_info)\n raise CallError(\n 404,\n \"{} not found because of {} \\\"{}\\\" on {}:{}\".format(\n req.path,\n exc_info[0].__name__,\n str(e),\n os.path.basename(exc_info[2].tb_frame.f_code.co_filename),\n exc_info[2].tb_lineno\n )\n )\n\n else:\n con = controller_info['class_instance']\n\n return con"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef handle(self):\n body = None\n req = self.request\n res = self.response\n rou = self.router\n con = None\n start = time.time()\n\n try:\n con = self.create_controller()\n con.call = self\n self.controller = con\n if not self.quiet:\n con.log_start(start)\n\n # the controller handle method will manipulate self.response, it first\n # tries to find a handle_HTTP_METHOD method, if it can't find that it\n # will default to the handle method (which is implemented on Controller).\n # method arguments are passed in so child classes can add decorators\n # just like the HTTP_METHOD that will actually handle the request\n controller_args, controller_kwargs = con.find_method_params()\n controller_method = getattr(con, \"handle_{}\".format(req.method), None)\n if not controller_method:\n controller_method = getattr(con, \"handle\")\n\n if not self.quiet:\n logger.debug(\"Using handle method: {}.{}\".format(\n con.__class__.__name__,\n controller_method.__name__\n ))\n controller_method(*controller_args, **controller_kwargs)\n\n except Exception as e:\n self.handle_error(e) # this will manipulate self.response\n\n finally:\n if res.code == 204:\n res.headers.pop('Content-Type', None)\n res.body = None # just to be sure since body could've been \"\"\n\n if con:\n if not self.quiet:\n con.log_stop(start)\n\n return res", "response": "Called from the interface to actually handle the request."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef handle_error(self, e, **kwargs):\n req = self.request\n res = self.response\n con = self.controller\n\n if isinstance(e, CallStop):\n logger.info(str(e), exc_info=True)\n res.code = e.code\n res.add_headers(e.headers)\n res.body = e.body\n\n elif isinstance(e, Redirect):\n logger.info(str(e), exc_info=True)\n res.code = e.code\n res.add_headers(e.headers)\n res.body = None\n\n elif isinstance(e, (AccessDenied, CallError)):\n logger.warning(str(e), exc_info=True)\n res.code = e.code\n res.add_headers(e.headers)\n res.body = e\n\n elif isinstance(e, NotImplementedError):\n logger.warning(str(e), exc_info=True)\n res.code = 501\n res.body = e\n\n elif isinstance(e, TypeError):\n e_msg = unicode(e)\n if e_msg.startswith(req.method) and 'argument' in e_msg:\n logger.debug(e_msg, exc_info=True)\n logger.warning(\n \" \".join([\n \"Either the path arguments ({} args) or the keyword arguments\",\n \"({} args) for {}.{} do not match the {} handling method's\",\n \"definition\"\n ]).format(\n len(req.controller_info[\"method_args\"]),\n len(req.controller_info[\"method_kwargs\"]),\n req.controller_info['module_name'],\n req.controller_info['class_name'],\n req.method\n )\n )\n res.code = 405\n\n else:\n logger.exception(e)\n res.code = 500\n\n res.body = e\n\n else:\n logger.exception(e)\n res.code = 500\n res.body = e\n\n if con:\n error_method = getattr(con, \"handle_{}_error\".format(req.method), None)\n if not error_method:\n error_method = getattr(con, \"handle_error\")\n\n logger.debug(\"Using error method: {}.{}\".format(\n con.__class__.__name__,\n error_method.__name__\n ))\n error_method(e, **kwargs)", "response": "This method will set the response body and code to the error code of the current resource and set the response body to the error code of the current resource."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef module_names(self):\n controller_prefix = self.controller_prefix\n _module_name_cache = self._module_name_cache\n if controller_prefix in _module_name_cache:\n return _module_name_cache[controller_prefix]\n\n module = self.get_module(controller_prefix)\n\n if hasattr(module, \"__path__\"):\n # path attr exists so this is a package\n modules = self.find_modules(module.__path__[0], controller_prefix)\n\n else:\n # we have a lonely .py file\n modules = set([controller_prefix])\n\n _module_name_cache.setdefault(controller_prefix, {})\n _module_name_cache[controller_prefix] = modules\n\n return modules", "response": "get all the modules in the controller_prefix\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef modules(self):\n for modname in self.module_names:\n module = importlib.import_module(modname)\n yield module", "response": "Returns an iterator of the actual modules not just their names\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef find_modules(self, path, prefix):\n\n modules = set([prefix])\n\n # https://docs.python.org/2/library/pkgutil.html#pkgutil.iter_modules\n for module_info in pkgutil.iter_modules([path]):\n # we want to ignore any \"private\" modules\n if module_info[1].startswith('_'): continue\n\n module_prefix = \".\".join([prefix, module_info[1]])\n if module_info[2]:\n # module is a package\n submodules = self.find_modules(os.path.join(path, module_info[1]), module_prefix)\n modules.update(submodules)\n else:\n modules.add(module_prefix)\n\n return modules", "response": "recursive method that will find all the submodules of the given module\n at the given path with prefix with path"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_module_name(self, path_args):\n controller_prefix = self.controller_prefix\n cset = self.module_names\n module_name = controller_prefix\n mod_name = module_name\n while path_args:\n mod_name += \".\" + path_args[0]\n if mod_name in cset:\n module_name = mod_name\n path_args.pop(0)\n else:\n break\n\n return module_name, path_args", "response": "returns the module_name and remaining path args."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_class(self, module, class_name):\n # let's get the class\n class_object = getattr(module, class_name, None)\n if not class_object or not issubclass(class_object, Controller):\n class_object = None\n\n return class_object", "response": "try and get the class_name from the module and make sure it is a valid\n controller"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef OPTIONS(self, *args, **kwargs):\n if not self.cors:\n raise CallError(405)\n\n req = self.request\n\n origin = req.get_header('origin')\n if not origin:\n raise CallError(400, 'Need Origin header') \n call_headers = [\n ('Access-Control-Request-Headers', 'Access-Control-Allow-Headers'),\n ('Access-Control-Request-Method', 'Access-Control-Allow-Methods')\n ]\n for req_header, res_header in call_headers:\n v = req.get_header(req_header)\n if v:\n self.response.set_header(res_header, v)\n else:\n raise CallError(400, 'Need {} header'.format(req_header))\n\n other_headers = {\n 'Access-Control-Allow-Credentials': 'true',\n 'Access-Control-Max-Age': 3600\n }\n self.response.add_headers(other_headers)", "response": "Handles CORS requests for this controller."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nhandling the request and returns the response", "response": "def handle(self, *controller_args, **controller_kwargs):\n \"\"\"handles the request and returns the response\n\n This should set any response information directly onto self.response\n\n this method has the same signature as the request handling methods\n (eg, GET, POST) so subclasses can override this method and add decorators\n\n :param *controller_args: tuple, the path arguments that will be passed to\n the request handling method (eg, GET, POST)\n :param **controller_kwargs: dict, the query and body params merged together\n \"\"\"\n req = self.request\n res = self.response\n res.set_header('Content-Type', \"{};charset={}\".format(\n self.content_type,\n self.encoding\n ))\n\n encoding = req.accept_encoding\n res.encoding = encoding if encoding else self.encoding\n\n res_method_name = \"\"\n controller_methods = self.find_methods()\n #controller_args, controller_kwargs = self.find_method_params()\n for controller_method_name, controller_method in controller_methods:\n try:\n logger.debug(\"Attempting to handle request with {}.{}.{}\".format(\n req.controller_info['module_name'],\n req.controller_info['class_name'],\n controller_method_name\n ))\n res.body = controller_method(\n *controller_args,\n **controller_kwargs\n )\n res_method_name = controller_method_name\n break\n\n except VersionError as e:\n logger.debug(\"Request {}.{}.{} failed version check [{} not in {}]\".format(\n req.controller_info['module_name'],\n req.controller_info['class_name'],\n controller_method_name,\n e.request_version,\n e.versions\n ))\n\n except RouteError:\n logger.debug(\"Request {}.{}.{} failed routing check\".format(\n req.controller_info['module_name'],\n req.controller_info['class_name'],\n controller_method_name\n ))\n\n if not res_method_name:\n # https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1\n # An origin server SHOULD return the status code 405 (Method Not Allowed)\n # if the method is known by the origin server but not allowed for the\n # requested resource\n raise CallError(405, \"Could not find a method to satisfy {}\".format(\n req.path\n ))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef find_methods(self):\n methods = []\n req = self.request\n method_name = req.method.upper()\n method_names = set()\n\n members = inspect.getmembers(self)\n for member_name, member in members:\n if member_name.startswith(method_name):\n if member:\n methods.append((member_name, member))\n method_names.add(member_name)\n\n if len(methods) == 0:\n # https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1\n # and 501 (Not Implemented) if the method is unrecognized or not\n # implemented by the origin server\n logger.warning(\"No methods to handle {} found\".format(method_name), exc_info=True)\n raise CallError(501, \"{} {} not implemented\".format(req.method, req.path))\n\n elif len(methods) > 1 and method_name in method_names:\n raise ValueError(\n \" \".join([\n \"A multi method {} request should not have any methods named {}.\",\n \"Instead, all {} methods should use use an appropriate decorator\",\n \"like @route or @version and have a unique name starting with {}_\"\n ]).format(\n method_name,\n method_name,\n method_name,\n method_name\n )\n )\n\n return methods", "response": "Find the methods that could satisfy this request. This will go through and find any methods that start with the request. method GET or POST. This will find any methods that start with the request. method GET or POST. This will find any methods that start with the request. method GET or POST."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the method params", "response": "def find_method_params(self):\n \"\"\"Return the method params\n\n :returns: tuple (args, kwargs) that will be passed as *args, **kwargs\n \"\"\"\n req = self.request\n args = req.controller_info[\"method_args\"]\n kwargs = req.controller_info[\"method_kwargs\"]\n return args, kwargs"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef log_start(self, start):\n if not logger.isEnabledFor(logging.INFO): return\n\n try:\n req = self.request\n\n logger.info(\"REQUEST {} {}?{}\".format(req.method, req.path, req.query))\n logger.info(datetime.datetime.strftime(datetime.datetime.utcnow(), \"DATE %Y-%m-%dT%H:%M:%S.%f\"))\n\n ip = req.ip\n if ip:\n logger.info(\"\\tIP ADDRESS: {}\".format(ip))\n\n if 'authorization' in req.headers:\n logger.info('AUTH {}'.format(req.headers['authorization']))\n\n ignore_hs = set([\n 'accept-language',\n 'accept-encoding',\n 'connection',\n 'authorization',\n 'host',\n 'x-forwarded-for'\n ])\n hs = [\"Request Headers...\"]\n for k, v in req.headers.items():\n if k not in ignore_hs:\n hs.append(\"\\t{}: {}\".format(k, v))\n\n logger.info(os.linesep.join(hs))\n\n except Exception as e:\n logger.warn(e, exc_info=True)", "response": "log all the headers and stuff at the start of the request"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef log_stop(self, start):\n if not logger.isEnabledFor(logging.INFO): return\n\n stop = time.time()\n get_elapsed = lambda start, stop, multiplier, rnd: round(abs(stop - start) * float(multiplier), rnd)\n elapsed = get_elapsed(start, stop, 1000.00, 1)\n total = \"%0.1f ms\" % (elapsed)\n logger.info(\"RESPONSE {} {} in {}\".format(self.response.code, self.response.status, total))", "response": "log a summary line on how the request went"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef build_lane_from_yaml(path):\n # Open\n with open(path, 'rb') as yaml_definition:\n definition = yaml.load(yaml_definition)\n\n # Validate schema\n try:\n validate_schema(definition)\n except SchemaError as exc:\n raise LaneSchemaError(**exc.__dict__)\n\n def build(lb_def, branch=False):\n \"\"\"Function to recursively build the `sparklanes.Lane` object from a YAML definition\"\"\"\n init_kwargs = {k: lb_def[k] for k in (a for a in ('run_parallel', 'name') if a in lb_def)}\n lane_or_branch = Lane(**init_kwargs) if not branch else Branch(**init_kwargs)\n\n for task in lb_def['tasks']:\n if 'branch' in task:\n branch_def = task['branch']\n lane_or_branch.add(build(branch_def, True))\n else:\n sep = task['class'].rfind('.')\n if sep == -1:\n raise LaneImportError('Class must include its parent module')\n mdl = task['class'][:sep]\n cls_ = task['class'][sep + 1:]\n\n try:\n cls = getattr(import_module(mdl), cls_)\n except ImportError:\n raise LaneImportError('Could not find module %s' % mdl)\n except AttributeError:\n raise LaneImportError('Could not find class %s' % cls_)\n\n args = task['args'] if 'args' in task else []\n args = [args] if not isinstance(args, list) else args\n kwargs = task['kwargs'] if 'kwargs' in task else {}\n lane_or_branch.add(cls, *args, **kwargs)\n\n return lane_or_branch\n\n return build(definition['lane'])", "response": "Builds a Sparklanes. Lane object from a YAML file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if a class is a task and if it has been decorated with Sparklanes. Task and if it has the supplied args and kwargs match the signature of the task s entry method.", "response": "def __validate_task(self, cls, entry_mtd_name, args, kwargs):\n \"\"\"Checks if a class is a task, i.e. if it has been decorated with `sparklanes.Task`, and if\n the supplied args/kwargs match the signature of the task's entry method.\n\n Parameters\n ----------\n cls : LaneTask\n entry_mtd_name : str\n Name of the method, which is called when the task is run\n args : list\n kwargs : dict\n \"\"\"\n if not isclass(cls) or not issubclass(cls, LaneTask):\n raise TypeError('Tried to add non-Task `%s` to a Lane. Are you sure the task was '\n 'decorated with `sparklanes.Task`?' % str(cls))\n\n validate_params(cls, entry_mtd_name, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add(self, cls_or_branch, *args, **kwargs):\n if isinstance(cls_or_branch, Branch):\n self.tasks.append(cls_or_branch) # Add branch with already validated tasks\n else:\n # Validate\n self.__validate_task(cls_or_branch, '__init__', args, kwargs)\n # Append\n self.tasks.append({'cls_or_branch': cls_or_branch, 'args': args, 'kwargs': kwargs})\n\n return self", "response": "Adds a task or branch to the lane."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexecuting the tasks in the lane in the order in which they have been added.", "response": "def run(self):\n \"\"\"Executes the tasks in the lane in the order in which they have been added, unless\n `self.run_parallel` is True, then a thread is spawned for each task and executed in\n parallel (note that task threads are still spawned in the order in which they were added).\n \"\"\"\n logger = make_default_logger(INTERNAL_LOGGER_NAME)\n logger.info('\\n%s\\nExecuting `%s`\\n%s\\n', '-'*80, self.name, '-'*80)\n logger.info('\\n%s', str(self))\n\n threads = []\n\n if not self.tasks:\n raise LaneExecutionError('No tasks to execute!')\n\n for task_def_or_branch in self.tasks:\n if isinstance(task_def_or_branch, Branch):\n task_def_or_branch.run()\n elif isinstance(task_def_or_branch['cls_or_branch'], Branch): # Nested Branch\n task_def_or_branch['cls_or_branch'].run()\n else:\n task = task_def_or_branch['cls_or_branch'](*task_def_or_branch['args'],\n **task_def_or_branch['kwargs'])\n if self.run_parallel:\n threads.append(LaneTaskThread(task))\n else:\n task()\n\n if threads:\n for thread in threads:\n thread.start()\n for thread in threads:\n thread.join()\n\n logger.info('\\n%s\\nFinished executing `%s`\\n%s', '-'*80, self.name, '-'*80)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the historical tank data.", "response": "def get_historical_data(nmr_problems):\n \"\"\"Get the historical tank data.\n\n Args:\n nmr_problems (int): the number of problems\n\n Returns:\n tuple: (observations, nmr_tanks_ground_truth)\n \"\"\"\n observations = np.tile(np.array([[10, 256, 202, 97]]), (nmr_problems, 1))\n nmr_tanks_ground_truth = np.ones((nmr_problems,)) * 276\n return observations, nmr_tanks_ground_truth"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_simulated_data(nmr_problems):\n # The number of tanks we observe per problem\n nmr_observed_tanks = 10\n\n # Generate some maximum number of tanks. Basically the ground truth of the estimation problem.\n nmr_tanks_ground_truth = normal(nmr_problems, 1, mean=250, std=30, ctype='uint')\n\n # Generate some random tank observations\n observations = uniform(nmr_problems, nmr_observed_tanks, low=0, high=nmr_tanks_ground_truth, ctype='uint')\n\n return observations, nmr_tanks_ground_truth", "response": "Simulate some data. This returns the simulated tank observations and the corresponding ground truth maximum number of tanks."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nestimate and return the upper triangular elements of the Hessian of the given function at the given parameters.", "response": "def estimate_hessian(objective_func, parameters,\n lower_bounds=None, upper_bounds=None,\n step_ratio=2, nmr_steps=5,\n max_step_sizes=None,\n data=None, cl_runtime_info=None):\n \"\"\"Estimate and return the upper triangular elements of the Hessian of the given function at the given parameters.\n\n This calculates the Hessian using central difference (using a 2nd order Taylor expansion) with a Richardson\n extrapolation over the proposed sequence of steps. If enough steps are given, we apply a Wynn epsilon extrapolation\n on top of the Richardson extrapolated results. If more steps are left, we return the estimate with the lowest error,\n taking into account outliers using a median filter.\n\n The Hessian is evaluated at the steps:\n\n .. math::\n \\quad ((f(x + d_j e_j + d_k e_k) - f(x + d_j e_j - d_k e_k)) -\n (f(x - d_j e_j + d_k e_k) - f(x - d_j e_j - d_k e_k)) /\n (4 d_j d_k)\n\n where :math:`e_j` is a vector where element :math:`j` is one and the rest are zero\n and :math:`d_j` is a scalar spacing :math:`steps_j`.\n\n Steps are generated according to an exponentially diminishing ratio, defined as:\n\n steps = max_step * step_ratio**-i, i = 0,1,..,nmr_steps-1.\n\n Where the maximum step can be provided. For example, a maximum step of 2 with a step ratio of 2, computed for\n 4 steps gives: [2.0, 1.0, 0.5, 0.25]. If lower and upper bounds are given, we use as maximum step size the largest\n step size that fits between the Hessian point and the boundaries.\n\n The steps define the order of the estimation, with 2 steps resulting in a O(h^2) estimate, 3 steps resulting in a\n O(h^4) estimate and 4 or more steps resulting in a O(h^6) derivative estimate.\n\n Args:\n objective_func (mot.lib.cl_function.CLFunction): The function we want to differentiate.\n A CL function with the signature:\n\n .. code-block:: c\n\n double (local const mot_float_type* const x, void* data);\n\n The objective function has the same signature as the minimization function in MOT. For the numerical\n hessian, the ``objective_list`` parameter is ignored.\n\n parameters (ndarray): The parameters at which to evaluate the gradient. A (d, p) matrix with d problems,\n and p parameters\n lower_bounds (list or None): a list of length (p,) for p parameters with the lower bounds.\n Each element of the list can be a scalar or a vector (of the same length as the number\n of problem instances). To disable bounds for this parameter use -np.inf.\n upper_bounds (list or None): a list of length (p,) for p parameters with the upper bounds.\n Each element of the list can be a scalar or a vector (of the same length as the number\n of problem instances). To disable bounds for this parameter use np.inf.\n step_ratio (float): the ratio at which the steps diminish.\n nmr_steps (int): the number of steps we will generate. We will calculate the derivative for each of these\n step sizes and extrapolate the best step size from among them. The minimum number of steps is 1.\n max_step_sizes (float or ndarray or None): the maximum step size, or the maximum step size per parameter.\n If None is given, we use 0.1 for all parameters. If a float is given, we use that for all parameters.\n If a list is given, it should be of the same length as the number of parameters.\n data (mot.lib.kernel_data.KernelData): the user provided data for the ``void* data`` pointer.\n cl_runtime_info (mot.configuration.CLRuntimeInfo): the runtime information\n\n Returns:\n ndarray: per problem instance a vector with the upper triangular elements of the Hessian matrix.\n This array can hold NaN's, for elements where the Hessian failed to approximate.\n \"\"\"\n if len(parameters.shape) == 1:\n parameters = parameters[None, :]\n\n nmr_voxels = parameters.shape[0]\n nmr_params = parameters.shape[1]\n nmr_derivatives = nmr_params * (nmr_params + 1) // 2\n\n initial_step = _get_initial_step(parameters, lower_bounds, upper_bounds, max_step_sizes)\n\n kernel_data = {\n 'parameters': Array(parameters, ctype='mot_float_type'),\n 'initial_step': Array(initial_step, ctype='float'),\n 'derivatives': Zeros((nmr_voxels, nmr_derivatives), 'double'),\n 'errors': Zeros((nmr_voxels, nmr_derivatives), 'double'),\n 'x_tmp': LocalMemory('mot_float_type', nmr_params),\n 'data': data,\n 'scratch': LocalMemory('double', nmr_steps + (nmr_steps - 1) + nmr_steps)\n }\n\n hessian_kernel = SimpleCLFunction.from_string('''\n void _numdiff_hessian(\n global mot_float_type* parameters,\n global float* initial_step,\n global double* derivatives,\n global double* errors,\n local mot_float_type* x_tmp,\n void* data,\n local double* scratch){\n\n if(get_local_id(0) == 0){\n for(uint i = 0; i < ''' + str(nmr_params) + '''; i++){\n x_tmp[i] = parameters[i];\n }\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n\n double f_x_input = ''' + objective_func.get_cl_function_name() + '''(x_tmp, data);\n\n // upper triangle loop\n uint coord_ind = 0;\n for(int i = 0; i < ''' + str(nmr_params) + '''; i++){\n for(int j = i; j < ''' + str(nmr_params) + '''; j++){\n _numdiff_hessian_element(\n data, x_tmp, f_x_input, i, j, initial_step, \n derivatives + coord_ind, errors + coord_ind, scratch);\n\n coord_ind++;\n }\n }\n }\n ''', dependencies=[objective_func,\n _get_numdiff_hessian_element_func(objective_func, nmr_steps, step_ratio)])\n\n hessian_kernel.evaluate(kernel_data, nmr_voxels, use_local_reduction=True, cl_runtime_info=cl_runtime_info)\n\n return kernel_data['derivatives'].get_data()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a function to compute one element of the Hessian matrix.", "response": "def _get_numdiff_hessian_element_func(objective_func, nmr_steps, step_ratio):\n \"\"\"Return a function to compute one element of the Hessian matrix.\"\"\"\n return SimpleCLFunction.from_string('''\n /**\n * Compute the Hessian using (possibly) multiple steps with various interpolations. \n */ \n void _numdiff_hessian_element(\n void* data, local mot_float_type* x_tmp, mot_float_type f_x_input,\n uint px, uint py, global float* initial_step, global double* derivative, \n global double* error, local double* scratch){\n\n const uint nmr_steps = ''' + str(nmr_steps) + ''';\n uint nmr_steps_remaining = nmr_steps;\n\n local double* scratch_ind = scratch;\n local double* steps = scratch_ind; scratch_ind += nmr_steps; \n local double* errors = scratch_ind; scratch_ind += nmr_steps - 1;\n local double* steps_tmp = scratch_ind; scratch_ind += nmr_steps;\n \n if(get_local_id(0) == 0){\n for(int i = 0; i < nmr_steps - 1; i++){\n errors[i] = 0;\n }\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n \n _numdiff_hessian_steps(data, x_tmp, f_x_input, px, py, steps, initial_step);\n\n if(nmr_steps_remaining > 1){\n nmr_steps_remaining = _numdiff_hessian_richardson_extrapolation(steps); \n barrier(CLK_LOCAL_MEM_FENCE);\n }\n \n if(nmr_steps_remaining >= 3){\n nmr_steps_remaining = _numdiff_wynn_extrapolation(steps, errors, nmr_steps_remaining);\n barrier(CLK_LOCAL_MEM_FENCE); \n }\n \n if(nmr_steps_remaining > 1){\n _numdiff_find_best_step(steps, errors, steps_tmp, nmr_steps_remaining);\n barrier(CLK_LOCAL_MEM_FENCE);\n }\n \n if(get_local_id(0) == 0){\n *derivative = steps[0];\n *error = errors[0];\n }\n }\n ''', dependencies=[\n _get_numdiff_hessian_steps_func(objective_func, nmr_steps, step_ratio),\n _get_numdiff_hessian_richardson_extrapolation_func(nmr_steps, step_ratio),\n _get_numdiff_wynn_extrapolation_func(),\n _get_numdiff_find_best_step_func()\n ])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_numdiff_hessian_steps_func(objective_func, nmr_steps, step_ratio):\n return SimpleCLFunction.from_string('''\n /**\n * Compute one element of the Hessian for a number of steps.\n * \n * This uses the initial steps in the data structure, indexed by the parameters to change (px, py).\n *\n * Args:\n * data: the data container\n * x_tmp: the array with the input parameters, needs to be writable, although it will return\n * the same values.\n * f_x_input: the objective function value at the original set of parameters \n * px: the index of the first parameter to perturbate\n * py: the index of the second parameter to perturbate\n * steps: storage location for the output steps\n * initial_step: the initial steps, array of same length as x_temp\n */\n void _numdiff_hessian_steps(void* data, local mot_float_type* x_tmp, \n mot_float_type f_x_input,\n uint px, uint py, \n local double* steps, \n global float* initial_step){\n\n double step_x;\n double step_y;\n double tmp;\n bool is_first_workitem = get_local_id(0) == 0;\n\n if(px == py){\n for(uint step_ind = 0; step_ind < ''' + str(nmr_steps) + '''; step_ind++){\n step_x = initial_step[px] / pown(''' + str(float(step_ratio)) + ''', step_ind);\n\n tmp = (\n _numdiff_hessian_eval_step_mono(data, x_tmp, px, 2 * step_x)\n + _numdiff_hessian_eval_step_mono(data, x_tmp, px, -2 * step_x)\n - 2 * f_x_input\n ) / (4 * step_x * step_x); \n\n if(is_first_workitem){\n steps[step_ind] = tmp;\n }\n }\n }\n else{\n for(uint step_ind = 0; step_ind < ''' + str(nmr_steps) + '''; step_ind++){\n step_x = initial_step[px] / pown(''' + str(float(step_ratio)) + ''', step_ind);\n step_y = initial_step[py] / pown(''' + str(float(step_ratio)) + ''', step_ind);\n\n tmp = (\n _numdiff_hessian_eval_step_bi(data, x_tmp, px, step_x, py, step_y)\n - _numdiff_hessian_eval_step_bi(data, x_tmp, px, step_x, py, -step_y)\n - _numdiff_hessian_eval_step_bi(data, x_tmp, px, -step_x, py, step_y)\n + _numdiff_hessian_eval_step_bi(data, x_tmp, px, -step_x, py, -step_y)\n ) / (4 * step_x * step_y);\n\n if(is_first_workitem){\n steps[step_ind] = tmp;\n } \n }\n }\n }\n ''', dependencies=[SimpleCLFunction.from_string('''\n /**\n * Evaluate the model with a perturbation in one dimensions.\n *\n * Args:\n * data: the data container\n * x_tmp: the array with the input parameters, needs to be writable, although it will return\n * the same values.\n * perturb_dim0: the index (into the x_tmp parameters) of the parameter to perturbate\n * perturb_0: the added perturbation of the index corresponding to ``perturb_dim_0``\n *\n * Returns:\n * the function evaluated at the parameters plus their perturbation.\n */\n double _numdiff_hessian_eval_step_mono(\n void* data, local mot_float_type* x_tmp, \n uint perturb_dim_0, mot_float_type perturb_0){\n\n mot_float_type old_0;\n double return_val;\n\n if(get_local_id(0) == 0){\n old_0 = x_tmp[perturb_dim_0];\n x_tmp[perturb_dim_0] += perturb_0;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n\n return_val = ''' + objective_func.get_cl_function_name() + '''(x_tmp, data);\n barrier(CLK_LOCAL_MEM_FENCE);\n\n if(get_local_id(0) == 0){\n x_tmp[perturb_dim_0] = old_0;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n\n return return_val;\n }\n '''), SimpleCLFunction.from_string('''\n /**\n * Evaluate the model with a perturbation in two dimensions.\n *\n * Args:\n * data: the data container\n * x_tmp: the array with the input parameters, needs to be writable, although it will return\n * the same values.\n * perturb_dim_0: the index (into the x_tmp parameters) of the first parameter to perturbate\n * perturb_0: the added perturbation of the index corresponding to ``perturb_dim_0``\n * perturb_dim_1: the index (into the x_tmp parameters) of the second parameter to perturbate\n * perturb_1: the added perturbation of the index corresponding to ``perturb_dim_1``\n *\n * Returns:\n * the function evaluated at the parameters plus their perturbation.\n */\n double _numdiff_hessian_eval_step_bi(\n void* data, local mot_float_type* x_tmp, \n uint perturb_dim_0, mot_float_type perturb_0,\n uint perturb_dim_1, mot_float_type perturb_1){\n\n mot_float_type old_0;\n mot_float_type old_1;\n double return_val;\n\n if(get_local_id(0) == 0){\n old_0 = x_tmp[perturb_dim_0];\n old_1 = x_tmp[perturb_dim_1];\n\n x_tmp[perturb_dim_0] += perturb_0;\n x_tmp[perturb_dim_1] += perturb_1;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n\n return_val = ''' + objective_func.get_cl_function_name() + '''(x_tmp, data);\n barrier(CLK_LOCAL_MEM_FENCE);\n\n if(get_local_id(0) == 0){\n x_tmp[perturb_dim_0] = old_0;\n x_tmp[perturb_dim_1] = old_1;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n\n return return_val;\n }\n ''')])", "response": "Get a function to compute the multiple step sizes for a single element of the Hessian."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget an initial step size to use for every parameter. This chooses the step sizes based on the maximum step size and the lower and upper bounds. Args: parameters (ndarray): The parameters at which to evaluate the gradient. A (d, p) matrix with d problems, p parameters and n samples. lower_bounds (list): lower bounds upper_bounds (list): upper bounds max_step_sizes (list or None): the maximum step size, or the maximum step size per parameter. Defaults to 0.1 Returns: ndarray: for every problem instance the vector with the initial step size for each parameter.", "response": "def _get_initial_step(parameters, lower_bounds, upper_bounds, max_step_sizes):\n \"\"\"Get an initial step size to use for every parameter.\n\n This chooses the step sizes based on the maximum step size and the lower and upper bounds.\n\n Args:\n parameters (ndarray): The parameters at which to evaluate the gradient. A (d, p) matrix with d problems,\n p parameters and n samples.\n lower_bounds (list): lower bounds\n upper_bounds (list): upper bounds\n max_step_sizes (list or None): the maximum step size, or the maximum step size per parameter. Defaults to 0.1\n\n Returns:\n ndarray: for every problem instance the vector with the initial step size for each parameter.\n \"\"\"\n nmr_params = parameters.shape[1]\n\n initial_step = np.zeros_like(parameters)\n\n if max_step_sizes is None:\n max_step_sizes = 0.1\n if isinstance(max_step_sizes, Number):\n max_step_sizes = [max_step_sizes] * nmr_params\n max_step_sizes = np.array(max_step_sizes)\n\n for ind in range(parameters.shape[1]):\n minimum_allowed_step = np.minimum(np.abs(parameters[:, ind] - lower_bounds[ind]),\n np.abs(upper_bounds[ind] - parameters[:, ind]))\n initial_step[:, ind] = np.minimum(minimum_allowed_step, max_step_sizes[ind])\n\n return initial_step / 2."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef apply(self):\n self._old_config = {k: v for k, v in _config.items()}\n self._apply()", "response": "Apply the current action to the current runtime configuration."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresetting the current configuration to the previous state.", "response": "def unapply(self):\n \"\"\"Reset the current configuration to the previous state.\"\"\"\n for key, value in self._old_config.items():\n _config[key] = value"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nassigns an attribute reference to all subsequent tasks.", "response": "def cache(self, name, val, overwrite=True):\n \"\"\"Assigns an attribute reference to all subsequent tasks. For example, if a task caches a\n DataFrame `df` using `self.cache('some_df', df)`, all tasks that follow can access the\n DataFrame using `self.some_df`. Note that manually assigned attributes that share the same\n name have precedence over cached attributes.\n\n Parameters\n ----------\n name : str\n Name of the attribute\n val\n Attribute value\n overwrite : bool\n Indicates if the attribute shall be overwritten, or not (if `False`, and\n a cached attribute with the given name already exists, `sparklanes.errors.CacheError`\n will be thrown).\n \"\"\"\n if name in TaskCache.cached and not overwrite:\n raise CacheError('Object with name `%s` already in cache.' % name)\n TaskCache.cached[name] = val"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef run(self):\n self.exc = None\n try:\n self.task()\n except BaseException:\n self.exc = sys.exc_info()", "response": "Overwrites threading. Thread. run to allow handling of exceptions thrown by threads\n from within the main app."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef join(self, timeout=None):\n Thread.join(self, timeout=timeout)\n if self.exc:\n msg = \"Thread '%s' threw an exception `%s`: %s\" \\\n % (self.getName(), self.exc[0].__name__, self.exc[1])\n new_exc = LaneExecutionError(msg)\n\n if PY3:\n raise new_exc.with_traceback(self.exc[2]) # pylint: disable=no-member\n else:\n raise (new_exc.__class__, new_exc, self.exc[2])", "response": "Overwrites threading. Thread. join to allow handling of exceptions thrown by threads\n from within the main app."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef mock_decorator(*args, **kwargs):\n def _called_decorator(dec_func):\n @wraps(dec_func)\n def _decorator(*args, **kwargs):\n return dec_func()\n return _decorator\n return _called_decorator", "response": "Mocks a function to be called by the user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmock all modules starting with one of the mock_modules names.", "response": "def import_mock(name, *args, **kwargs):\n \"\"\"Mock all modules starting with one of the mock_modules names.\"\"\"\n if any(name.startswith(s) for s in mock_modules):\n return MockModule()\n return orig_import(name, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef apply_cl_function(cl_function, kernel_data, nmr_instances, use_local_reduction=False, cl_runtime_info=None):\n cl_runtime_info = cl_runtime_info or CLRuntimeInfo()\n cl_environments = cl_runtime_info.cl_environments\n\n for param in cl_function.get_parameters():\n if param.name not in kernel_data:\n names = [param.name for param in cl_function.get_parameters()]\n missing_names = [name for name in names if name not in kernel_data]\n raise ValueError('Some parameters are missing an input value, '\n 'required parameters are: {}, missing inputs are: {}'.format(names, missing_names))\n\n if cl_function.get_return_type() != 'void':\n kernel_data['_results'] = Zeros((nmr_instances,), cl_function.get_return_type())\n\n workers = []\n for ind, cl_environment in enumerate(cl_environments):\n worker = _ProcedureWorker(cl_environment, cl_runtime_info.compile_flags,\n cl_function, kernel_data, cl_runtime_info.double_precision, use_local_reduction)\n workers.append(worker)\n\n def enqueue_batch(batch_size, offset):\n items_per_worker = [batch_size // len(cl_environments) for _ in range(len(cl_environments) - 1)]\n items_per_worker.append(batch_size - sum(items_per_worker))\n\n for ind, worker in enumerate(workers):\n worker.calculate(offset, offset + items_per_worker[ind])\n offset += items_per_worker[ind]\n worker.cl_queue.flush()\n\n for worker in workers:\n worker.cl_queue.finish()\n\n return offset\n\n total_offset = 0\n for batch_start, batch_end in split_in_batches(nmr_instances, 1e4 * len(workers)):\n total_offset = enqueue_batch(batch_end - batch_start, total_offset)\n\n if cl_function.get_return_type() != 'void':\n return kernel_data['_results'].get_data()", "response": "This function will run the given function on the given set of data instances and then execute the given function on the given set of data instances."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef from_string(cls, cl_function, dependencies=()):\n return_type, function_name, parameter_list, body = split_cl_function(cl_function)\n return SimpleCLFunction(return_type, function_name, parameter_list, body, dependencies=dependencies)", "response": "Parse the given string into a SimpleCLFunction object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_parameter_signatures(self):\n declarations = []\n for p in self.get_parameters():\n new_p = p.get_renamed(p.name.replace('.', '_'))\n declarations.append(new_p.get_declaration())\n return declarations", "response": "Get the list of signatures of the parameters for the CL function declaration."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the CL code for all the dependencies.", "response": "def _get_cl_dependency_code(self):\n \"\"\"Get the CL code for all the CL code for all the dependencies.\n\n Returns:\n str: The CL code with the actual code.\n \"\"\"\n code = ''\n for d in self._dependencies:\n code += d.get_cl_code() + \"\\n\"\n return code"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the list of kernel arguments for loading the kernel data elements into the kernel.", "response": "def _get_kernel_arguments(self):\n \"\"\"Get the list of kernel arguments for loading the kernel data elements into the kernel.\n\n This will use the sorted keys for looping through the kernel input items.\n\n Returns:\n list of str: the list of parameter definitions\n \"\"\"\n declarations = []\n for name, data in self._kernel_data.items():\n declarations.extend(data.get_kernel_parameters('_' + name))\n return declarations"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_scalar_arg_dtypes(self):\n dtypes = []\n for name, data in self._kernel_data.items():\n dtypes.extend(data.get_scalar_arg_dtypes())\n return dtypes", "response": "Get the location and types of the input scalars."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate and initializes a new `SparkContext` (the old one will be stopped). Argument signature is copied from `pyspark.SparkContext `_.", "response": "def set_sc(cls, master=None, appName=None, sparkHome=None, pyFiles=None, environment=None,\n batchSize=0, serializer=PickleSerializer(), conf=None, gateway=None, jsc=None,\n profiler_cls=BasicProfiler):\n \"\"\"Creates and initializes a new `SparkContext` (the old one will be stopped).\n Argument signature is copied from `pyspark.SparkContext\n `_.\n \"\"\"\n if cls.sc is not None:\n cls.sc.stop()\n cls.sc = SparkContext(master, appName, sparkHome, pyFiles, environment, batchSize,\n serializer,\n conf, gateway, jsc, profiler_cls)\n cls.__init_spark()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_spark(cls, master=None, appName=None, conf=None, hive_support=False):\n sess = SparkSession.builder\n if master:\n sess.master(master)\n if appName:\n sess.appName(appName)\n if conf:\n sess.config(conf=conf)\n if hive_support:\n sess.enableHiveSupport()\n\n cls.spark = sess.getOrCreate()", "response": "Creates and initializes a new SparkSession."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _package_and_submit(args):\n args = _parse_and_validate_args(args)\n\n logging.debug(args)\n dist = __make_tmp_dir()\n try:\n __package_dependencies(dist_dir=dist, additional_reqs=args['requirements'],\n silent=args['silent'])\n __package_app(tasks_pkg=args['package'],\n dist_dir=dist,\n custom_main=args['main'],\n extra_data=args['extra_data'])\n __run_spark_submit(lane_yaml=args['yaml'],\n dist_dir=dist,\n spark_home=args['spark_home'],\n spark_args=args['spark_args'],\n silent=args['silent'])\n\n except Exception as exc:\n __clean_up(dist)\n raise exc\n __clean_up(dist)", "response": "Packages and submits a job to Spark."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing and validate the given arguments.", "response": "def _parse_and_validate_args(args):\n \"\"\"\n Parse and validate arguments. During validation, it is checked whether the given\n files/directories exist, while also converting relative paths to absolute ones.\n\n Parameters\n ----------\n args (List): Command-line arguments\n \"\"\"\n class ExtendAction(argparse.Action):\n def __call__(self, parser, namespace, values, option_string=None):\n if getattr(namespace, self.dest, None) is None:\n setattr(namespace, self.dest, [])\n getattr(namespace, self.dest).extend(values)\n\n parser = argparse.ArgumentParser(description='Submitting a lane to spark.')\n parser.add_argument('-y', '--yaml', type=str, required=True,\n help='Path to the yaml definition file.')\n parser.add_argument('-p', '--package', type=str, required=True,\n help='Path to the python package containing your tasks.')\n parser.add_argument('-r', '--requirements', type=str, required=False,\n help='Path to a `requirements.txt` specifying any additional dependencies '\n 'of your tasks.')\n parser.add_argument('-e', '--extra-data', nargs='*', required=False, action=ExtendAction,\n help='Path to any additional files or directories that should be packaged '\n 'and sent to Spark.')\n parser.add_argument('-m', '--main', type=str, required=False,\n help='Path to a custom main python file')\n parser.add_argument('-d', '--spark-home', type=str, required=False,\n help='Custom path to the directory containing your Spark installation. If '\n 'none is given, sparklanes will try to use the `spark-submit` command '\n 'from your PATH')\n parser.add_argument('-s', '--spark-args', nargs='*', required=False,\n help='Any additional arguments that should be sent to Spark via '\n 'spark-submit. '\n '(e.g. `--spark-args executor-memory=20G total-executor-cores=100`)')\n parser.add_argument('--silent', help='If set, no output will be sent to console',\n action='store_true')\n args = parser.parse_args(args).__dict__\n\n # Check/fix files/dirs\n for param in ('package', 'spark_home'):\n args[param] = __validate_and_fix_path(args[param], check_dir=True)\n for param in ('yaml', 'requirements', 'main'):\n args[param] = __validate_and_fix_path(args[param], check_file=True)\n if args['extra_data']:\n for i in range(len(args['extra_data'])):\n args['extra_data'][i] = __validate_and_fix_path(args['extra_data'][i],\n check_file=True, check_dir=True)\n\n # Check if python package\n if not os.path.isfile(os.path.join(args['package'], '__init__.py')):\n raise SystemExit('Could not confirm `%s` is a python package. Make sure it contains an '\n '`__init__.py`.')\n\n # Check/fix spark args\n if args['spark_args']:\n args['spark_args'] = __validate_and_fix_spark_args(args['spark_args'])\n\n return args"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if a file or directory exists and converts relative paths to absolute ones", "response": "def __validate_and_fix_path(path, check_file=False, check_dir=False):\n \"\"\"Check if a file/directory exists and converts relative paths to absolute ones\"\"\"\n # pylint: disable=superfluous-parens\n if path is None:\n return path\n else:\n if not (os.path.isfile(path) if check_file else False) \\\n and not (os.path.isdir(path) if check_dir else False):\n raise SystemExit('Path `%s` does not exist' % path)\n if not os.path.isabs(path):\n path = os.path.abspath(os.path.join(os.path.abspath(os.curdir), path))\n\n return path"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __validate_and_fix_spark_args(spark_args):\n pattern = re.compile(r'[\\w\\-_]+=.+')\n fixed_args = []\n for arg in spark_args:\n if arg not in SPARK_SUBMIT_FLAGS:\n if not pattern.match(arg):\n raise SystemExit('Spark argument `%s` does not seem to be in the correct format '\n '`ARG_NAME=ARG_VAL`, and is also not recognized to be one of the'\n 'valid spark-submit flags (%s).' % (arg, str(SPARK_SUBMIT_FLAGS)))\n eq_pos = arg.find('=')\n fixed_args.append('--' + arg[:eq_pos])\n fixed_args.append(arg[eq_pos + 1:])\n else:\n fixed_args.append('--' + arg)\n\n return fixed_args", "response": "Validate and fix spark arguments."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\npackaging the app s dependencies from pip and packages them to spark", "response": "def __package_dependencies(dist_dir, additional_reqs, silent):\n \"\"\"\n Installs the app's dependencies from pip and packages them (as zip), to be submitted to spark.\n\n Parameters\n ----------\n dist_dir (str): Path to directory where the packaged libs shall be located\n additional_reqs (str): Path to a requirements.txt, containing any of the app's additional\n requirements\n silent (bool): Flag indicating whether pip output should be printed to console\n \"\"\"\n logging.info('Packaging dependencies')\n libs_dir = os.path.join(dist_dir, 'libs')\n if not os.path.isdir(libs_dir):\n os.mkdir(libs_dir)\n\n # Get requirements\n req_txt = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'requirements-submit.txt')\n with open(req_txt, 'r') as req:\n requirements = req.read().splitlines()\n if additional_reqs:\n with open(additional_reqs, 'r') as req:\n for row in req:\n requirements.append(row)\n\n # Remove duplicates\n requirements = list(set(requirements))\n\n # Install\n devnull = open(os.devnull, 'w')\n outp = {'stderr': STDOUT, 'stdout': devnull} if silent else {}\n for pkg in requirements:\n cmd = ['pip', 'install', pkg, '-t', libs_dir]\n logging.debug('Calling `%s`', str(cmd))\n call(cmd, **outp)\n devnull.close()\n\n # Package\n shutil.make_archive(libs_dir, 'zip', libs_dir, './')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __package_app(tasks_pkg, dist_dir, custom_main=None, extra_data=None):\n logging.info('Packaging application')\n\n # Package tasks\n tasks_dir_splits = os.path.split(os.path.realpath(tasks_pkg))\n shutil.make_archive(os.path.join(dist_dir, 'tasks'),\n 'zip',\n tasks_dir_splits[0],\n tasks_dir_splits[1])\n\n # Package main.py\n if custom_main is None:\n from . import _main\n main_path = _main.__file__\n if main_path[-3:] == 'pyc':\n main_path = main_path[:-1]\n shutil.copy(os.path.realpath(main_path),\n os.path.join(dist_dir, 'main.py'))\n else:\n shutil.copy(os.path.realpath(custom_main),\n os.path.join(dist_dir, 'main.py'))\n\n # Package _framework\n shutil.make_archive(os.path.join(dist_dir, '_framework'),\n 'zip',\n os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..'),\n './sparklanes/')\n\n # Package extra data\n if extra_data:\n for dat in extra_data:\n real_path = os.path.realpath(dat)\n target = os.path.join(dist_dir, os.path.split(real_path)[1])\n if os.path.isfile(real_path):\n shutil.copy(real_path, target)\n elif os.path.isdir(real_path):\n shutil.copytree(real_path, target)\n else:\n raise IOError('File `%s` not found at `%s`.' % (dat, real_path))", "response": "Packages the tasks_pkg to dist_dir. Also copies the main. py file to spark."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __run_spark_submit(lane_yaml, dist_dir, spark_home, spark_args, silent):\n # spark-submit binary\n cmd = ['spark-submit' if spark_home is None else os.path.join(spark_home, 'bin/spark-submit')]\n\n # Supplied spark arguments\n if spark_args:\n cmd += spark_args\n\n # Packaged App & lane\n cmd += ['--py-files', 'libs.zip,_framework.zip,tasks.zip', 'main.py']\n cmd += ['--lane', lane_yaml]\n\n logging.info('Submitting to Spark')\n logging.debug(str(cmd))\n\n # Submit\n devnull = open(os.devnull, 'w')\n outp = {'stderr': STDOUT, 'stdout': devnull} if silent else {}\n call(cmd, cwd=dist_dir, env=MY_ENV, **outp)\n devnull.close()", "response": "Submits the packaged application to Spark using a spark - submit subprocess"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds include guards to the given string.", "response": "def add_include_guards(cl_str, guard_name=None):\n \"\"\"Add include guards to the given string.\n\n If you are including the same body of CL code multiple times in a Kernel, it is important to add include\n guards (https://en.wikipedia.org/wiki/Include_guard) around them to prevent the kernel from registering the function\n twice.\n\n Args:\n cl_str (str): the piece of CL code as a string to which we add the include guards\n guard_name (str): the name of the C pre-processor guard. If not given we use the MD5 hash of the\n given cl string.\n\n Returns:\n str: the same string but then with include guards around them.\n \"\"\"\n if not guard_name:\n guard_name = 'GUARD_' + hashlib.md5(cl_str.encode('utf-8')).hexdigest()\n\n return '''\n # ifndef {guard_name}\n # define {guard_name}\n {func_str}\n # endif // {guard_name}\n '''.format(func_str=cl_str, guard_name=guard_name)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the numpy datatype of the given CL data type string.", "response": "def ctype_to_dtype(cl_type, mot_float_type='float'):\n \"\"\"Get the numpy dtype of the given cl_type string.\n\n Args:\n cl_type (str): the CL data type to match, for example 'float' or 'float4'.\n mot_float_type (str): the C name of the ``mot_float_type``. The dtype will be looked up recursively.\n\n Returns:\n dtype: the numpy datatype\n \"\"\"\n if is_vector_ctype(cl_type):\n raw_type, vector_length = split_vector_ctype(cl_type)\n\n if raw_type == 'mot_float_type':\n if is_vector_ctype(mot_float_type):\n raw_type, _ = split_vector_ctype(mot_float_type)\n else:\n raw_type = mot_float_type\n\n vector_type = raw_type + str(vector_length)\n return getattr(cl_array.vec, vector_type)\n else:\n if cl_type == 'mot_float_type':\n cl_type = mot_float_type\n\n data_types = [\n ('char', np.int8),\n ('uchar', np.uint8),\n ('short', np.int16),\n ('ushort', np.uint16),\n ('int', np.int32),\n ('uint', np.uint32),\n ('long', np.int64),\n ('ulong', np.uint64),\n ('float', np.float32),\n ('double', np.float64),\n ]\n for ctype, dtype in data_types:\n if ctype == cl_type:\n return dtype"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef convert_data_to_dtype(data, data_type, mot_float_type='float'):\n scalar_dtype = ctype_to_dtype(data_type, mot_float_type)\n\n if isinstance(data, numbers.Number):\n data = scalar_dtype(data)\n\n if is_vector_ctype(data_type):\n shape = data.shape\n dtype = ctype_to_dtype(data_type, mot_float_type)\n ve = np.zeros(shape[:-1], dtype=dtype)\n\n if len(shape) == 1:\n for vector_ind in range(shape[0]):\n ve[0][vector_ind] = data[vector_ind]\n elif len(shape) == 2:\n for i in range(data.shape[0]):\n for vector_ind in range(data.shape[1]):\n ve[i][vector_ind] = data[i, vector_ind]\n elif len(shape) == 3:\n for i in range(data.shape[0]):\n for j in range(data.shape[1]):\n for vector_ind in range(data.shape[2]):\n ve[i, j][vector_ind] = data[i, j, vector_ind]\n\n return np.require(ve, requirements=['C', 'A', 'O'])\n return np.require(data, scalar_dtype, ['C', 'A', 'O'])", "response": "Convert the given input data to the correct numpy type."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef split_vector_ctype(ctype):\n if not is_vector_ctype(ctype):\n raise ValueError('The given ctype is not a vector type.')\n for vector_length in [2, 3, 4, 8, 16]:\n if ctype.endswith(str(vector_length)):\n vector_str_len = len(str(vector_length))\n return ctype[:-vector_str_len], int(ctype[-vector_str_len:])", "response": "Split a vector ctype into a raw ctype and the vector length."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef device_type_from_string(cl_device_type_str):\n cl_device_type_str = cl_device_type_str.upper()\n if hasattr(cl.device_type, cl_device_type_str):\n return getattr(cl.device_type, cl_device_type_str)\n return None", "response": "Converts a string like gpu to a pyopencl device type string."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the model floating point type definition.", "response": "def get_float_type_def(double_precision, include_complex=True):\n \"\"\"Get the model floating point type definition.\n\n Args:\n double_precision (boolean): if True we will use the double type for the mot_float_type type.\n Else, we will use the single precision float type for the mot_float_type type.\n include_complex (boolean): if we include support for complex numbers\n\n Returns:\n str: defines the mot_float_type types, the epsilon and the MIN and MAX values.\n \"\"\"\n if include_complex:\n with open(os.path.abspath(resource_filename('mot', 'data/opencl/complex.h')), 'r') as f:\n complex_number_support = f.read()\n else:\n complex_number_support = ''\n\n scipy_constants = '''\n #define MACHEP DBL_EPSILON\n #define MAXLOG log(DBL_MAX)\n #define LANCZOS_G 6.024680040776729583740234375 /* taken from Scipy */\n #define EULER 0.577215664901532860606512090082402431 /* Euler constant, from Scipy */\n '''\n\n if double_precision:\n return '''\n #if __OPENCL_VERSION__ <= CL_VERSION_1_1\n #pragma OPENCL EXTENSION cl_khr_fp64 : enable\n #endif\n\n #define PYOPENCL_DEFINE_CDOUBLE\n\n typedef double mot_float_type;\n typedef double2 mot_float_type2;\n typedef double4 mot_float_type4;\n typedef double8 mot_float_type8;\n typedef double16 mot_float_type16;\n\n #define MOT_EPSILON DBL_EPSILON\n #define MOT_MIN DBL_MIN\n #define MOT_MAX DBL_MAX\n ''' + scipy_constants + complex_number_support\n else:\n return '''\n #if __OPENCL_VERSION__ <= CL_VERSION_1_1\n #pragma OPENCL EXTENSION cl_khr_fp64 : enable\n #endif\n\n typedef float mot_float_type;\n typedef float2 mot_float_type2;\n typedef float4 mot_float_type4;\n typedef float8 mot_float_type8;\n typedef float16 mot_float_type16;\n\n #define MOT_EPSILON FLT_EPSILON\n #define MOT_MIN FLT_MIN\n #define MOT_MAX FLT_MAX\n ''' + scipy_constants + complex_number_support"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef topological_sort(data):\n\n def check_self_dependencies(input_data):\n \"\"\"Check if there are self dependencies within a node.\n\n Self dependencies are for example: ``{'a': ('a',)}``.\n\n Args:\n input_data (dict): the input data. Of a structure similar to {key: (list of values), ...}.\n\n Raises:\n ValueError: if there are indeed self dependencies\n \"\"\"\n for k, v in input_data.items():\n if k in v:\n raise ValueError('Self-dependency, {} depends on itself.'.format(k))\n\n def prepare_input_data(input_data):\n \"\"\"Prepares the input data by making sets of the dependencies. This automatically removes redundant items.\n\n Args:\n input_data (dict): the input data. Of a structure similar to {key: (list of values), ...}.\n\n Returns:\n dict: a copy of the input dict but with sets instead of lists for the dependencies.\n \"\"\"\n return {k: set(v) for k, v in input_data.items()}\n\n def find_items_without_dependencies(input_data):\n \"\"\"This searches the dependencies of all the items for items that have no dependencies.\n\n For example, suppose the input is: ``{'a': ('b',)}``, then ``a`` depends on ``b`` and ``b`` depends on nothing.\n This class returns ``(b,)`` in this example.\n\n Args:\n input_data (dict): the input data. Of a structure similar to {key: (list of values), ...}.\n\n Returns:\n list: the list of items without any dependency.\n \"\"\"\n return list(reduce(set.union, input_data.values()) - set(input_data.keys()))\n\n def add_empty_dependencies(data):\n items_without_dependencies = find_items_without_dependencies(data)\n data.update({item: set() for item in items_without_dependencies})\n\n def get_sorted(input_data):\n data = input_data\n while True:\n ordered = set(item for item, dep in data.items() if len(dep) == 0)\n if not ordered:\n break\n yield ordered\n data = {item: (dep - ordered) for item, dep in data.items() if item not in ordered}\n\n if len(data) != 0:\n raise ValueError('Cyclic dependencies exist '\n 'among these items: {}'.format(', '.join(repr(x) for x in data.items())))\n\n check_self_dependencies(data)\n\n if not len(data):\n return []\n\n data_copy = prepare_input_data(data)\n add_empty_dependencies(data_copy)\n\n result = []\n for d in get_sorted(data_copy):\n try:\n d = sorted(d)\n except TypeError:\n d = list(d)\n\n result.extend(d)\n return result", "response": "Topological sort the given dictionary structure."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ntests if the given value is a scalar.", "response": "def is_scalar(value):\n \"\"\"Test if the given value is a scalar.\n\n This function also works with memory mapped array values, in contrast to the numpy is_scalar method.\n\n Args:\n value: the value to test for being a scalar value\n\n Returns:\n boolean: if the given value is a scalar or not\n \"\"\"\n return np.isscalar(value) or (isinstance(value, np.ndarray) and (len(np.squeeze(value).shape) == 0))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if all elements in the given value are equal to each other.", "response": "def all_elements_equal(value):\n \"\"\"Checks if all elements in the given value are equal to each other.\n\n If the input is a single value the result is trivial. If not, we compare all the values to see\n if they are exactly the same.\n\n Args:\n value (ndarray or number): a numpy array or a single number.\n\n Returns:\n bool: true if all elements are equal to each other, false otherwise\n \"\"\"\n if is_scalar(value):\n return True\n return np.array(value == value.flatten()[0]).all()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting a single value out of the given value.", "response": "def get_single_value(value):\n \"\"\"Get a single value out of the given value.\n\n This is meant to be used after a call to :func:`all_elements_equal` that returned True. With this\n function we return a single number from the input value.\n\n Args:\n value (ndarray or number): a numpy array or a single number.\n\n Returns:\n number: a single number from the input\n\n Raises:\n ValueError: if not all elements are equal\n \"\"\"\n if not all_elements_equal(value):\n raise ValueError('Not all values are equal to each other.')\n\n if is_scalar(value):\n return value\n return value.item(0)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndisables all logging temporarily. A context manager that will prevent any logging messages triggered during the body from being processed. Args: highest_level: the maximum logging level that is being blocked", "response": "def all_logging_disabled(highest_level=logging.CRITICAL):\n \"\"\"Disable all logging temporarily.\n\n A context manager that will prevent any logging messages triggered during the body from being processed.\n\n Args:\n highest_level: the maximum logging level that is being blocked\n \"\"\"\n previous_level = logging.root.manager.disable\n logging.disable(highest_level)\n try:\n yield\n finally:\n logging.disable(previous_level)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsplits the total number of elements into batches of the specified maximum size.", "response": "def split_in_batches(nmr_elements, max_batch_size):\n \"\"\"Split the total number of elements into batches of the specified maximum size.\n\n Examples::\n split_in_batches(30, 8) -> [(0, 8), (8, 15), (16, 23), (24, 29)]\n\n for batch_start, batch_end in split_in_batches(2000, 100):\n array[batch_start:batch_end]\n\n Yields:\n tuple: the start and end point of the next batch\n \"\"\"\n offset = 0\n elements_left = nmr_elements\n while elements_left > 0:\n next_batch = (offset, offset + min(elements_left, max_batch_size))\n yield next_batch\n\n batch_size = min(elements_left, max_batch_size)\n elements_left -= batch_size\n offset += batch_size"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntransforming a covariance matrix into a correlations matrix.", "response": "def covariance_to_correlations(covariance):\n \"\"\"Transform a covariance matrix into a correlations matrix.\n\n This can be seen as dividing a covariance matrix by the outer product of the diagonal.\n\n As post processing we replace the infinities and the NaNs with zeros and clip the result to [-1, 1].\n\n Args:\n covariance (ndarray): a matrix of shape (n, p, p) with for n problems the covariance matrix of shape (p, p).\n\n Returns:\n ndarray: the correlations matrix\n \"\"\"\n diagonal_ind = np.arange(covariance.shape[1])\n diagonal_els = covariance[:, diagonal_ind, diagonal_ind]\n result = covariance / np.sqrt(diagonal_els[:, :, None] * diagonal_els[:, None, :])\n result[np.isinf(result)] = 0\n return np.clip(np.nan_to_num(result), -1, 1)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef multiprocess_mapping(func, iterable):\n if os.name == 'nt': # In Windows there is no fork.\n return list(map(func, iterable))\n try:\n p = multiprocessing.Pool()\n return_data = list(p.imap(func, iterable))\n p.close()\n p.join()\n return return_data\n except OSError:\n return list(map(func, iterable))", "response": "Multiprocess mapping the given function on the given iterable."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_cl_function(cl_code, dependencies=()):\n from mot.lib.cl_function import SimpleCLFunction\n\n def separate_cl_functions(input_str):\n \"\"\"Separate all the OpenCL functions.\n\n This creates a list of strings, with for each function found the OpenCL code.\n\n Args:\n input_str (str): the string containing one or more functions.\n\n Returns:\n list: a list of strings, with one string per found CL function.\n \"\"\"\n class Semantics:\n\n def __init__(self):\n self._functions = []\n\n def result(self, ast):\n return self._functions\n\n def arglist(self, ast):\n return '({})'.format(', '.join(ast))\n\n def function(self, ast):\n def join(items):\n result = ''\n for item in items:\n if isinstance(item, str):\n result += item\n else:\n result += join(item)\n return result\n\n self._functions.append(join(ast).strip())\n return ast\n\n return _extract_cl_functions_parser.parse(input_str, semantics=Semantics())\n\n functions = separate_cl_functions(cl_code)\n return SimpleCLFunction.from_string(functions[-1], dependencies=list(dependencies or []) + [\n SimpleCLFunction.from_string(s) for s in functions[:-1]])", "response": "Parse the given OpenCL string to a single SimpleCLFunction."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsplits a CL function into a return type function name parameter list and body.", "response": "def split_cl_function(cl_str):\n \"\"\"Split an CL function into a return type, function name, parameters list and the body.\n\n Args:\n cl_str (str): the CL code to parse and plit into components\n\n Returns:\n tuple: string elements for the return type, function name, parameter list and the body\n \"\"\"\n class Semantics:\n\n def __init__(self):\n self._return_type = ''\n self._function_name = ''\n self._parameter_list = []\n self._cl_body = ''\n\n def result(self, ast):\n return self._return_type, self._function_name, self._parameter_list, self._cl_body\n\n def address_space(self, ast):\n self._return_type = ast.strip() + ' '\n return ast\n\n def data_type(self, ast):\n self._return_type += ''.join(ast).strip()\n return ast\n\n def function_name(self, ast):\n self._function_name = ast.strip()\n return ast\n\n def arglist(self, ast):\n if ast != '()':\n self._parameter_list = ast\n return ast\n\n def body(self, ast):\n def join(items):\n result = ''\n for item in items:\n if isinstance(item, str):\n result += item\n else:\n result += join(item)\n return result\n\n self._cl_body = join(ast).strip()[1:-1]\n return ast\n\n return _split_cl_function_parser.parse(cl_str, semantics=Semantics())"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_default_logger(name=INTERNAL_LOGGER_NAME, level=logging.INFO,\n fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s'):\n \"\"\"Create a logger with the default configuration\"\"\"\n logger = logging.getLogger(name)\n logger.setLevel(level)\n if not logger.handlers:\n handler = logging.StreamHandler(sys.stderr)\n handler.setLevel(level)\n formatter = logging.Formatter(fmt)\n handler.setFormatter(formatter)\n logger.addHandler(handler)\n\n return logger", "response": "Create a logger with the default configuration"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking if the device associated with this environment is a GPU.", "response": "def is_gpu(self):\n \"\"\"Check if the device associated with this environment is a GPU.\n\n Returns:\n boolean: True if the device is an GPU, false otherwise.\n \"\"\"\n return self._device.get_info(cl.device_info.TYPE) == cl.device_type.GPU"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef is_cpu(self):\n return self._device.get_info(cl.device_info.TYPE) == cl.device_type.CPU", "response": "Check if the device associated with this environment is a CPU."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef single_device(cl_device_type='GPU', platform=None, fallback_to_any_device_type=False):\n if isinstance(cl_device_type, str):\n cl_device_type = device_type_from_string(cl_device_type)\n\n device = None\n\n if platform is None:\n platforms = cl.get_platforms()\n else:\n platforms = [platform]\n\n for platform in platforms:\n devices = platform.get_devices(device_type=cl_device_type)\n\n for dev in devices:\n if device_supports_double(dev):\n try:\n env = CLEnvironment(platform, dev)\n return [env]\n except cl.RuntimeError:\n pass\n\n if not device:\n if fallback_to_any_device_type:\n return cl.get_platforms()[0].get_devices()\n else:\n raise ValueError('No devices of the specified type ({}) found.'.format(\n cl.device_type.to_string(cl_device_type)))\n\n raise ValueError('No suitable OpenCL device found.')", "response": "Get a list containing a single environment for a given OpenCL device type on the given platform."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef all_devices(cl_device_type=None, platform=None):\n if isinstance(cl_device_type, str):\n cl_device_type = device_type_from_string(cl_device_type)\n\n runtime_list = []\n\n if platform is None:\n platforms = cl.get_platforms()\n else:\n platforms = [platform]\n\n for platform in platforms:\n if cl_device_type:\n devices = platform.get_devices(device_type=cl_device_type)\n else:\n devices = platform.get_devices()\n\n for device in devices:\n if device_supports_double(device):\n env = CLEnvironment(platform, device)\n runtime_list.append(env)\n\n return runtime_list", "response": "Get multiple device environments optionally only of the indicated type."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a list of device environments that is suitable for use in MOT. Basically this gets the total list of devices using all_devices() and applies a filter on it. This filter does the following: 1) if the 'AMD Accelerated Parallel Processing' is available remove all environments using the 'Clover' platform. More things may be implemented in the future. Args: preferred_device_type (str): the preferred device type, one of 'CPU', 'GPU' or 'APU'. If no devices of this type can be found, we will use any other device available. Returns: list of CLEnvironment: List with the CL device environments.", "response": "def smart_device_selection(preferred_device_type=None):\n \"\"\"Get a list of device environments that is suitable for use in MOT.\n\n Basically this gets the total list of devices using all_devices() and applies a filter on it.\n\n This filter does the following:\n 1) if the 'AMD Accelerated Parallel Processing' is available remove all environments using the 'Clover'\n platform.\n\n More things may be implemented in the future.\n\n Args:\n preferred_device_type (str): the preferred device type, one of 'CPU', 'GPU' or 'APU'.\n If no devices of this type can be found, we will use any other device available.\n\n Returns:\n list of CLEnvironment: List with the CL device environments.\n \"\"\"\n cl_environments = CLEnvironmentFactory.all_devices(cl_device_type=preferred_device_type)\n platform_names = [env.platform.name for env in cl_environments]\n has_amd_pro_platform = any('AMD Accelerated Parallel Processing' in name for name in platform_names)\n\n if has_amd_pro_platform:\n return list(filter(lambda env: 'Clover' not in env.platform.name, cl_environments))\n\n if preferred_device_type is not None and not len(cl_environments):\n return CLEnvironmentFactory.all_devices()\n \n return cl_environments"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef multivariate_ess(samples, batch_size_generator=None):\n samples_generator = _get_sample_generator(samples)\n return np.array(multiprocess_mapping(_MultivariateESSMultiProcessing(batch_size_generator), samples_generator()))", "response": "r Estimates the multivariate Effective Sample Size for the samples of every problem."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef univariate_ess(samples, method='standard_error', **kwargs):\n samples_generator = _get_sample_generator(samples)\n return np.array(multiprocess_mapping(_UnivariateESSMultiProcessing(method, **kwargs), samples_generator()))", "response": "r Estimates the univariate Effective Sample Size for the samples of every problem."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a sample generator from the given polymorphic input.", "response": "def _get_sample_generator(samples):\n \"\"\"Get a sample generator from the given polymorphic input.\n\n Args:\n samples (ndarray, dict or generator): either an matrix of shape (d, p, n) with d problems, p parameters and\n n samples, or a dictionary with for every parameter a matrix with shape (d, n) or, finally,\n a generator function that yields sample arrays of shape (p, n).\n\n Returns:\n generator: a generator that yields a matrix of size (p, n) for every problem in the input.\n \"\"\"\n if isinstance(samples, Mapping):\n def samples_generator():\n for ind in range(samples[list(samples.keys())[0]].shape[0]):\n yield np.array([samples[s][ind, :] for s in sorted(samples)])\n elif isinstance(samples, np.ndarray):\n def samples_generator():\n for ind in range(samples.shape[0]):\n yield samples[ind]\n else:\n samples_generator = samples\n return samples_generator"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef estimate_univariate_ess_standard_error(chain, batch_size_generator=None, compute_method=None):\n sigma = (monte_carlo_standard_error(chain, batch_size_generator=batch_size_generator,\n compute_method=compute_method) ** 2 * len(chain))\n lambda_ = np.var(chain, dtype=np.float64)\n return len(chain) * (lambda_ / sigma)", "response": "r Estimates the univariate ESS using the standard error method."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef minimum_multivariate_ess(nmr_params, alpha=0.05, epsilon=0.05):\n tmp = 2.0 / nmr_params\n log_min_ess = tmp * np.log(2) + np.log(np.pi) - tmp * (np.log(nmr_params) + gammaln(nmr_params / 2)) \\\n + np.log(chi2.ppf(1 - alpha, nmr_params)) - 2 * np.log(epsilon)\n return int(round(np.exp(log_min_ess)))", "response": "r Calculates the minimum multivariate Effective Sample Size for a given set of free parameters."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef multivariate_ess_precision(nmr_params, multi_variate_ess, alpha=0.05):\n tmp = 2.0 / nmr_params\n log_min_ess = tmp * np.log(2) + np.log(np.pi) - tmp * (np.log(nmr_params) + gammaln(nmr_params / 2)) \\\n + np.log(chi2.ppf(1 - alpha, nmr_params)) - np.log(multi_variate_ess)\n return np.sqrt(np.exp(log_min_ess))", "response": "r Calculates the precision given the multivariate Effective Sample Size and the confidence region of the MCMC sample."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef estimate_multivariate_ess_sigma(samples, batch_size):\n sample_means = np.mean(samples, axis=1, dtype=np.float64)\n nmr_params, chain_length = samples.shape\n\n nmr_batches = int(np.floor(chain_length / batch_size))\n sigma = np.zeros((nmr_params, nmr_params))\n\n nmr_offsets = chain_length - nmr_batches * batch_size + 1\n\n for offset in range(nmr_offsets):\n batches = np.reshape(samples[:, np.array(offset + np.arange(0, nmr_batches * batch_size), dtype=np.int)].T,\n [batch_size, nmr_batches, nmr_params], order='F')\n\n batch_means = np.squeeze(np.mean(batches, axis=0, dtype=np.float64))\n\n Z = batch_means - sample_means\n\n for x, y in itertools.product(range(nmr_params), range(nmr_params)):\n sigma[x, y] += np.sum(Z[:, x] * Z[:, y])\n\n return sigma * batch_size / (nmr_batches - 1) / nmr_offsets", "response": "r Estimates the multivariate ESS sigma matrix for a set of samples."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef monte_carlo_standard_error(chain, batch_size_generator=None, compute_method=None):\n batch_size_generator = batch_size_generator or SquareRootSingleBatch()\n compute_method = compute_method or BatchMeansMCSE()\n\n batch_sizes = batch_size_generator.get_univariate_ess_batch_sizes(len(chain))\n\n return np.min(list(compute_method.compute_standard_error(chain, b) for b in batch_sizes))", "response": "Compute Monte Carlo standard errors for the expectations\n ArcGIS Markov chain."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef fit_gaussian(samples, ddof=0):\n if len(samples.shape) == 1:\n return np.mean(samples), np.std(samples, ddof=ddof)\n return np.mean(samples, axis=1), np.std(samples, axis=1, ddof=ddof)", "response": "Calculates the mean and standard deviation of the given samples."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncompute the circular mean for samples in a range of samples.", "response": "def fit_circular_gaussian(samples, high=np.pi, low=0):\n \"\"\"Compute the circular mean for samples in a range\n\n Args:\n samples (ndarray): a one or two dimensional array. If one dimensional we calculate the fit using all\n values. If two dimensional, we fit the Gaussian for every set of samples over the first dimension.\n high (float): The maximum wrap point\n low (float): The minimum wrap point\n \"\"\"\n cl_func = SimpleCLFunction.from_string('''\n void compute(global mot_float_type* samples,\n global mot_float_type* means,\n global mot_float_type* stds,\n int nmr_samples,\n int low,\n int high){ \n \n double cos_mean = 0;\n double sin_mean = 0;\n double ang;\n\n for(uint i = 0; i < nmr_samples; i++){\n ang = (samples[i] - low)*2*M_PI / (high - low);\n\n cos_mean += (cos(ang) - cos_mean) / (i + 1);\n sin_mean += (sin(ang) - sin_mean) / (i + 1);\n }\n\n double R = hypot(cos_mean, sin_mean);\n if(R > 1){\n R = 1;\n }\n\n double stds = 1/2. * sqrt(-2 * log(R));\n\n double res = atan2(sin_mean, cos_mean);\n if(res < 0){\n res += 2 * M_PI;\n }\n\n *(means) = res*(high - low)/2.0/M_PI + low;\n *(stds) = ((high - low)/2.0/M_PI) * sqrt(-2*log(R));\n }\n ''')\n\n def run_cl(samples):\n data = {'samples': Array(samples, 'mot_float_type'),\n 'means': Zeros(samples.shape[0], 'mot_float_type'),\n 'stds': Zeros(samples.shape[0], 'mot_float_type'),\n 'nmr_samples': Scalar(samples.shape[1]),\n 'low': Scalar(low),\n 'high': Scalar(high),\n }\n\n cl_func.evaluate(data, samples.shape[0])\n return data['means'].get_data(), data['stds'].get_data()\n\n if len(samples.shape) == 1:\n mean, std = run_cl(samples[None, :])\n return mean[0], std[0]\n return run_cl(samples)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfits a truncated Gaussian distribution on the given samples.", "response": "def fit_truncated_gaussian(samples, lower_bounds, upper_bounds):\n \"\"\"Fits a truncated gaussian distribution on the given samples.\n\n This will do a maximum likelihood estimation of a truncated Gaussian on the provided samples, with the\n truncation points given by the lower and upper bounds.\n\n Args:\n samples (ndarray): a one or two dimensional array. If one dimensional we fit the truncated Gaussian on all\n values. If two dimensional, we calculate the truncated Gaussian for every set of samples over the\n first dimension.\n lower_bounds (ndarray or float): the lower bound, either a scalar or a lower bound per problem (first index of\n samples)\n upper_bounds (ndarray or float): the upper bound, either a scalar or an upper bound per problem (first index of\n samples)\n\n Returns:\n mean, std: the mean and std of the fitted truncated Gaussian\n \"\"\"\n if len(samples.shape) == 1:\n return _TruncatedNormalFitter()((samples, lower_bounds, upper_bounds))\n\n def item_generator():\n for ind in range(samples.shape[0]):\n if is_scalar(lower_bounds):\n lower_bound = lower_bounds\n else:\n lower_bound = lower_bounds[ind]\n\n if is_scalar(upper_bounds):\n upper_bound = upper_bounds\n else:\n upper_bound = upper_bounds[ind]\n\n yield (samples[ind], lower_bound, upper_bound)\n\n results = np.array(multiprocess_mapping(_TruncatedNormalFitter(), item_generator()))\n return results[:, 0], results[:, 1]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncompute the overlapping coefficient of two Gaussian continuous_distributions.", "response": "def gaussian_overlapping_coefficient(means_0, stds_0, means_1, stds_1, lower=None, upper=None):\n \"\"\"Compute the overlapping coefficient of two Gaussian continuous_distributions.\n\n This computes the :math:`\\int_{-\\infty}^{\\infty}{\\min(f(x), g(x))\\partial x}` where\n :math:`f \\sim \\mathcal{N}(\\mu_0, \\sigma_0^{2})` and :math:`f \\sim \\mathcal{N}(\\mu_1, \\sigma_1^{2})` are normally\n distributed variables.\n\n This will compute the overlap for each element in the first dimension.\n\n Args:\n means_0 (ndarray): the set of means of the first distribution\n stds_0 (ndarray): the set of stds of the fist distribution\n means_1 (ndarray): the set of means of the second distribution\n stds_1 (ndarray): the set of stds of the second distribution\n lower (float): the lower limit of the integration. If not set we set it to -inf.\n upper (float): the upper limit of the integration. If not set we set it to +inf.\n \"\"\"\n if lower is None:\n lower = -np.inf\n if upper is None:\n upper = np.inf\n\n def point_iterator():\n for ind in range(means_0.shape[0]):\n yield np.squeeze(means_0[ind]), np.squeeze(stds_0[ind]), np.squeeze(means_1[ind]), np.squeeze(stds_1[ind])\n\n return np.array(list(multiprocess_mapping(_ComputeGaussianOverlap(lower, upper), point_iterator())))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef truncated_normal_log_likelihood(params, low, high, data):\n mu = params[0]\n sigma = params[1]\n if sigma == 0:\n return np.inf\n ll = np.sum(norm.logpdf(data, mu, sigma))\n ll -= len(data) * np.log((norm.cdf(high, mu, sigma) - norm.cdf(low, mu, sigma)))\n return -ll", "response": "Calculate the log likelihood of the truncated normal distribution."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef truncated_normal_ll_gradient(params, low, high, data):\n if params[1] == 0:\n return np.array([np.inf, np.inf])\n\n return np.array([_TruncatedNormalFitter.partial_derivative_mu(params[0], params[1], low, high, data),\n _TruncatedNormalFitter.partial_derivative_sigma(params[0], params[1], low, high, data)])", "response": "Returns the gradient of the log likelihood of the truncated normal at the given position."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef partial_derivative_mu(mu, sigma, low, high, data):\n pd_mu = np.sum(data - mu) / sigma ** 2\n pd_mu -= len(data) * ((norm.pdf(low, mu, sigma) - norm.pdf(high, mu, sigma))\n / (norm.cdf(high, mu, sigma) - norm.cdf(low, mu, sigma)))\n return -pd_mu", "response": "The partial derivative of the log - likelihood at a given point."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_kernel_data(self):\n return {\n 'nmsimplex_scratch': LocalMemory(\n 'mot_float_type', self._nmr_parameters * 2 + (self._nmr_parameters + 1) ** 2 + 1),\n 'initial_simplex_scale': LocalMemory('mot_float_type', self._nmr_parameters)\n }", "response": "Get the kernel data needed for this optimization routine to work."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_kernel_data(self):\n return {\n 'subplex_scratch_float': LocalMemory(\n 'mot_float_type', 4 + self._var_replace_dict['NMR_PARAMS'] * 2\n + self._var_replace_dict['MAX_SUBSPACE_LENGTH'] * 2\n + (self._var_replace_dict['MAX_SUBSPACE_LENGTH'] * 2\n + self._var_replace_dict['MAX_SUBSPACE_LENGTH']+1)**2 + 1),\n 'subplex_scratch_int': LocalMemory(\n 'int', 2 + self._var_replace_dict['NMR_PARAMS']\n + (self._var_replace_dict['NMR_PARAMS'] // self._var_replace_dict['MIN_SUBSPACE_LENGTH'])),\n 'initial_simplex_scale': LocalMemory('mot_float_type', self._var_replace_dict['NMR_PARAMS'])\n }", "response": "Get the kernel data needed for this optimization routine to work."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the kernel data needed for this optimization routine to work.", "response": "def get_kernel_data(self):\n \"\"\"Get the kernel data needed for this optimization routine to work.\"\"\"\n return {\n 'scratch_mot_float_type': LocalMemory(\n 'mot_float_type', 8 +\n 2 * self._var_replace_dict['NMR_OBSERVATIONS'] +\n 5 * self._var_replace_dict['NMR_PARAMS'] +\n self._var_replace_dict['NMR_PARAMS'] * self._var_replace_dict['NMR_OBSERVATIONS']),\n 'scratch_int': LocalMemory('int', self._var_replace_dict['NMR_PARAMS'])\n }"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _bounds_to_array(bounds):\n elements = []\n for value in bounds:\n if all_elements_equal(value):\n elements.append(Scalar(get_single_value(value), ctype='mot_float_type'))\n else:\n elements.append(Array(value, ctype='mot_float_type', as_scalar=True))\n return CompositeArray(elements, 'mot_float_type', address_space='local')", "response": "Create a CompositeArray to hold the bounds."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a dictionary with the default options for the given minimization method.", "response": "def get_minimizer_options(method):\n \"\"\"Return a dictionary with the default options for the given minimization method.\n\n Args:\n method (str): the name of the method we want the options off\n\n Returns:\n dict: a dictionary with the default options\n \"\"\"\n if method == 'Powell':\n return {'patience': 2,\n 'patience_line_search': None,\n 'reset_method': 'EXTRAPOLATED_POINT'}\n\n elif method == 'Nelder-Mead':\n return {'patience': 200,\n 'alpha': 1.0, 'beta': 0.5, 'gamma': 2.0, 'delta': 0.5, 'scale': 0.1,\n 'adaptive_scales': True}\n\n elif method == 'Levenberg-Marquardt':\n return {'patience': 250, 'step_bound': 100.0, 'scale_diag': 1, 'usertol_mult': 30}\n\n elif method == 'Subplex':\n return {'patience': 10,\n 'patience_nmsimplex': 100,\n 'alpha': 1.0, 'beta': 0.5, 'gamma': 2.0, 'delta': 0.5, 'scale': 1.0, 'psi': 0.0001, 'omega': 0.01,\n 'adaptive_scales': True,\n 'min_subspace_length': 'auto',\n 'max_subspace_length': 'auto'}\n\n raise ValueError('Could not find the specified method \"{}\".'.format(method))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _clean_options(method, provided_options):\n provided_options = provided_options or {}\n default_options = get_minimizer_options(method)\n\n result = {}\n\n for name, default in default_options.items():\n if name in provided_options:\n result[name] = provided_options[name]\n else:\n result[name] = default_options[name]\n return result", "response": "Clean the given input options."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nminimize the Powell algorithm.", "response": "def _minimize_powell(func, x0, cl_runtime_info, lower_bounds, upper_bounds,\n constraints_func=None, data=None, options=None):\n \"\"\"\n Options:\n patience (int): Used to set the maximum number of iterations to patience*(number_of_parameters+1)\n reset_method (str): one of 'EXTRAPOLATED_POINT' or 'RESET_TO_IDENTITY' lower case or upper case.\n patience_line_search (int): the patience of the searching algorithm. Defaults to the\n same patience as for the Powell algorithm itself.\n \"\"\"\n options = options or {}\n nmr_problems = x0.shape[0]\n nmr_parameters = x0.shape[1]\n\n penalty_data, penalty_func = _get_penalty_function(nmr_parameters, constraints_func)\n\n eval_func = SimpleCLFunction.from_string('''\n double evaluate(local mot_float_type* x, void* data){\n double penalty = _mle_penalty(\n x, \n ((_powell_eval_func_data*)data)->data,\n ((_powell_eval_func_data*)data)->lower_bounds,\n ((_powell_eval_func_data*)data)->upper_bounds,\n ''' + str(options.get('penalty_weight', 1e30)) + ''', \n ((_powell_eval_func_data*)data)->penalty_data\n );\n \n double func_val = ''' + func.get_cl_function_name() + '''(x, ((_powell_eval_func_data*)data)->data, 0);\n \n if(isnan(func_val)){\n return INFINITY;\n }\n \n return func_val + penalty;\n }\n ''', dependencies=[func, penalty_func])\n\n optimizer_func = Powell(eval_func, nmr_parameters, **_clean_options('Powell', options))\n\n kernel_data = {'model_parameters': Array(x0, ctype='mot_float_type', mode='rw'),\n 'data': Struct({'data': data,\n 'lower_bounds': lower_bounds,\n 'upper_bounds': upper_bounds,\n 'penalty_data': penalty_data}, '_powell_eval_func_data')}\n kernel_data.update(optimizer_func.get_kernel_data())\n\n return_code = optimizer_func.evaluate(\n kernel_data, nmr_problems,\n use_local_reduction=all(env.is_gpu for env in cl_runtime_info.cl_environments),\n cl_runtime_info=cl_runtime_info)\n\n return OptimizeResults({'x': kernel_data['model_parameters'].get_data(),\n 'status': return_code})"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting a numerical differentiated Jacobian function. This computes the Jacobian of the observations (function vector) with respect to the parameters. Args: eval_func (mot.lib.cl_function.CLFunction): the evaluation function nmr_params (int): the number of parameters nmr_observations (int): the number of observations (the length of the function vector). Returns: mot.lib.cl_function.CLFunction: CL function for numerically estimating the Jacobian.", "response": "def _lm_numdiff_jacobian(eval_func, nmr_params, nmr_observations):\n \"\"\"Get a numerical differentiated Jacobian function.\n\n This computes the Jacobian of the observations (function vector) with respect to the parameters.\n\n Args:\n eval_func (mot.lib.cl_function.CLFunction): the evaluation function\n nmr_params (int): the number of parameters\n nmr_observations (int): the number of observations (the length of the function vector).\n\n Returns:\n mot.lib.cl_function.CLFunction: CL function for numerically estimating the Jacobian.\n \"\"\"\n return SimpleCLFunction.from_string(r'''\n /**\n * Compute the Jacobian for use in the Levenberg-Marquardt optimization routine.\n *\n * This should place the output in the ``fjac`` matrix.\n *\n * Parameters:\n *\n * model_parameters: (nmr_params,) the current point around which we want to know the Jacobian\n * data: the current modeling data, used by the objective function\n * fvec: (nmr_observations,), the function values corresponding to the current model parameters\n * fjac: (nmr_parameters, nmr_observations), the memory location for the Jacobian\n */\n void _lm_numdiff_jacobian(local mot_float_type* model_parameters,\n void* data,\n local mot_float_type* fvec,\n local mot_float_type* const fjac){\n \n const uint nmr_params = ''' + str(nmr_params) + ''';\n const uint nmr_observations = ''' + str(nmr_observations) + ''';\n \n local mot_float_type* lower_bounds = ((_lm_eval_func_data*)data)->lower_bounds;\n local mot_float_type* upper_bounds = ((_lm_eval_func_data*)data)->upper_bounds;\n local mot_float_type* jacobian_x_tmp = ((_lm_eval_func_data*)data)->jacobian_x_tmp;\n \n mot_float_type step_size = 30 * MOT_EPSILON;\n \n for (int i = 0; i < nmr_params; i++) {\n if(model_parameters[i] + step_size > upper_bounds[i]){\n _lm_numdiff_jacobian_backwards(model_parameters, i, step_size, data, fvec, \n fjac + i*nmr_observations, jacobian_x_tmp);\n }\n else if(model_parameters[i] - step_size < lower_bounds[i]){\n _lm_numdiff_jacobian_forwards(model_parameters, i, step_size, data, fvec, \n fjac + i*nmr_observations, jacobian_x_tmp);\n }\n else{\n _lm_numdiff_jacobian_centered(model_parameters, i, step_size, data, fvec, \n fjac + i*nmr_observations, jacobian_x_tmp);\n }\n }\n }\n ''', dependencies=[eval_func, SimpleCLFunction.from_string('''\n void _lm_numdiff_jacobian_centered(\n local mot_float_type* model_parameters,\n uint px,\n float step_size,\n void* data,\n local mot_float_type* fvec,\n local mot_float_type* const fjac,\n local mot_float_type* fjac_tmp){\n \n const uint nmr_observations = ''' + str(nmr_observations) + ''';\n \n uint observation_ind;\n mot_float_type temp;\n \n uint local_id = get_local_id(0);\n uint workgroup_size = get_local_size(0);\n \n // F(x + h) \n if(get_local_id(0) == 0){\n temp = model_parameters[px];\n model_parameters[px] = temp + step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac);\n \n \n // F(x - h)\n if(get_local_id(0) == 0){\n model_parameters[px] = temp - step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac_tmp);\n \n \n // combine\n for(int i = 0; i < (nmr_observations + workgroup_size - 1) / workgroup_size; i++){\n observation_ind = i * workgroup_size + local_id;\n if(observation_ind < nmr_observations){\n fjac[observation_ind] = (fjac[observation_ind] - fjac_tmp[observation_ind]) / (2 * step_size);\n }\n }\n \n // restore parameter vector\n if(get_local_id(0) == 0){\n model_parameters[px] = temp;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n }\n '''), SimpleCLFunction.from_string('''\n void _lm_numdiff_jacobian_backwards(\n local mot_float_type* model_parameters,\n uint px,\n float step_size,\n void* data,\n local mot_float_type* fvec,\n local mot_float_type* const fjac,\n local mot_float_type* fjac_tmp){\n\n const uint nmr_observations = ''' + str(nmr_observations) + ''';\n \n uint observation_ind;\n mot_float_type temp;\n \n uint local_id = get_local_id(0);\n uint workgroup_size = get_local_size(0);\n \n // F(x - h) \n if(get_local_id(0) == 0){\n temp = model_parameters[px];\n model_parameters[px] = temp - step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac);\n \n \n // F(x - 2*h)\n if(get_local_id(0) == 0){\n model_parameters[px] = temp - 2 * step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac_tmp);\n \n // combine\n for(int i = 0; i < (nmr_observations + workgroup_size - 1) / workgroup_size; i++){\n observation_ind = i * workgroup_size + local_id;\n if(observation_ind < nmr_observations){\n fjac[observation_ind] = ( 3 * fvec[observation_ind] \n - 4 * fjac[observation_ind] \n + fjac_tmp[observation_ind]) / (2 * step_size);\n }\n }\n \n // restore parameter vector\n if(get_local_id(0) == 0){\n model_parameters[px] = temp;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n }\n '''), SimpleCLFunction.from_string('''\n void _lm_numdiff_jacobian_forwards(\n local mot_float_type* model_parameters,\n uint px,\n float step_size,\n void* data,\n local mot_float_type* fvec,\n local mot_float_type* const fjac,\n local mot_float_type* fjac_tmp){\n\n const uint nmr_observations = ''' + str(nmr_observations) + ''';\n \n uint observation_ind;\n mot_float_type temp;\n \n uint local_id = get_local_id(0);\n uint workgroup_size = get_local_size(0);\n \n // F(x + h) \n if(get_local_id(0) == 0){\n temp = model_parameters[px];\n model_parameters[px] = temp + step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac);\n \n \n // F(x + 2*h)\n if(get_local_id(0) == 0){\n model_parameters[px] = temp + 2 * step_size;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n ''' + eval_func.get_cl_function_name() + '''(model_parameters, data, fjac);\n \n // combine\n for(int i = 0; i < (nmr_observations + workgroup_size - 1) / workgroup_size; i++){\n observation_ind = i * workgroup_size + local_id;\n if(observation_ind < nmr_observations){\n fjac[observation_ind] = (- 3 * fvec[observation_ind] \n + 4 * fjac[observation_ind] \n - fjac_tmp[observation_ind]) / (2 * step_size);\n }\n }\n \n // restore parameter vector\n if(get_local_id(0) == 0){\n model_parameters[px] = temp;\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n }\n ''')])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_penalty_function(nmr_parameters, constraints_func=None):\n dependencies = []\n data_requirements = {'scratch': LocalMemory('double', 1)}\n constraints_code = ''\n\n if constraints_func and constraints_func.get_nmr_constraints() > 0:\n nmr_constraints = constraints_func.get_nmr_constraints()\n dependencies.append(constraints_func)\n data_requirements['constraints'] = LocalMemory('mot_float_type', nmr_constraints)\n\n constraints_code = '''\n local mot_float_type* constraints = ((_mle_penalty_data*)scratch_data)->constraints;\n \n ''' + constraints_func.get_cl_function_name() + '''(x, data, constraints);\n \n for(int i = 0; i < ''' + str(nmr_constraints) + '''; i++){\n *penalty_sum += pown(max((mot_float_type)0, constraints[i]), 2); \n }\n '''\n\n data = Struct(data_requirements, '_mle_penalty_data')\n func = SimpleCLFunction.from_string('''\n double _mle_penalty(\n local mot_float_type* x,\n void* data, \n local mot_float_type* lower_bounds, \n local mot_float_type* upper_bounds,\n float penalty_weight,\n void* scratch_data){\n \n local double* penalty_sum = ((_mle_penalty_data*)scratch_data)->scratch;\n \n if(get_local_id(0) == 0){\n *penalty_sum = 0;\n \n // boundary conditions\n for(int i = 0; i < ''' + str(nmr_parameters) + '''; i++){\n if(isfinite(upper_bounds[i])){\n *penalty_sum += pown(max((mot_float_type)0, x[i] - upper_bounds[i]), 2); \n }\n if(isfinite(lower_bounds[i])){\n *penalty_sum += pown(max((mot_float_type)0, lower_bounds[i] - x[i]), 2);\n }\n }\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n \n // constraints\n ''' + constraints_code + '''\n \n return penalty_weight * *penalty_sum;\n }\n ''', dependencies=dependencies)\n return data, func", "response": "Returns a function to compute the penalty term for the boundary conditions."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef from_string(cls, cl_function, dependencies=(), nmr_constraints=None):\n return_type, function_name, parameter_list, body = split_cl_function(cl_function)\n return SimpleConstraintFunction(return_type, function_name, parameter_list, body, dependencies=dependencies,\n nmr_constraints=nmr_constraints)", "response": "Parse the given string into a SimpleCLFunction object."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nvalidates the schema of a dict containing a top - level lane or a branch - level lane.", "response": "def validate_schema(yaml_def, branch=False):\n \"\"\"Validates the schema of a dict\n\n Parameters\n ----------\n yaml_def : dict\n dict whose schema shall be validated\n branch : bool\n Indicates whether `yaml_def` is a dict of a top-level lane, or of a branch\n inside a lane (needed for recursion)\n\n Returns\n -------\n bool\n True if validation was successful\n \"\"\"\n schema = Schema({\n 'lane' if not branch else 'branch': {\n Optional('name'): str,\n Optional('run_parallel'): bool,\n 'tasks': list\n }\n })\n\n schema.validate(yaml_def)\n from schema import And, Use\n task_schema = Schema({\n 'class': str,\n Optional('kwargs'): Or({str: object}),\n Optional('args'): Or([object], And(Use(lambda a: isinstance(a, dict)), False))\n })\n\n def validate_tasks(tasks): # pylint: disable=missing-docstring\n for task in tasks:\n try:\n Schema({'branch': dict}).validate(task)\n validate_schema(task, True)\n except SchemaError:\n task_schema.validate(task)\n\n return True\n\n return validate_tasks(yaml_def['lane']['tasks'] if not branch else yaml_def['branch']['tasks'])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef validate_params(cls, mtd_name, *args, **kwargs):\n mtd = getattr(cls, mtd_name)\n\n py3_mtd_condition = (not (inspect.isfunction(mtd) or inspect.ismethod(mtd))\n and hasattr(cls, mtd_name))\n py2_mtd_condition = (not inspect.ismethod(mtd)\n and not isinstance(cls.__dict__[mtd_name], staticmethod))\n if (PY3 and py3_mtd_condition) or (PY2 and py2_mtd_condition):\n raise TypeError('Attribute `%s` of class `%s` must be a method. Got type `%s` instead.'\n % (mtd_name, cls.__name__, type(mtd)))\n\n req_params, opt_params = arg_spec(cls, mtd_name)\n n_params = len(req_params) + len(opt_params)\n n_args_kwargs = len(args) + len(kwargs)\n\n for k in kwargs:\n if k not in req_params and k not in opt_params:\n raise TaskInitializationError('kwarg `%s` is not a parameter of callable `%s`.'\n % (k, mtd.__name__))\n\n if n_args_kwargs < len(req_params):\n raise TaskInitializationError('Not enough args/kwargs supplied for callable `%s`. '\n 'Required args: %s' % (mtd.__name__, str(req_params)))\n if len(args) > n_params or n_args_kwargs > n_params or len(kwargs) > n_params:\n raise TaskInitializationError('Too many args/kwargs supplied for callable `%s`. '\n 'Required args: %s' % (mtd.__name__, str(req_params)))\n\n redundant_p = [p for p in kwargs if p not in req_params[len(args):] + opt_params]\n if redundant_p:\n raise TaskInitializationError('Supplied one or more kwargs that in the signature of '\n 'callable `%s`. Redundant kwargs: %s'\n % (mtd.__name__, str(redundant_p)))\n\n needed_kwargs = req_params[len(args):]\n if not all([True if p in kwargs else False for p in needed_kwargs]):\n raise TaskInitializationError('Not enough args/kwargs supplied for callable `%s`. '\n 'Required args: %s' % (mtd.__name__, str(req_params)))", "response": "Validates if the given args and kwargs match the method signature."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncross - version argument signature inspection.", "response": "def arg_spec(cls, mtd_name):\n \"\"\"Cross-version argument signature inspection\n\n Parameters\n ----------\n cls : class\n mtd_name : str\n Name of the method to be inspected\n\n Returns\n -------\n required_params : list of str\n List of required, positional parameters\n optional_params : list of str\n List of optional parameters, i.e. parameters with a default value\n \"\"\"\n mtd = getattr(cls, mtd_name)\n\n required_params = []\n optional_params = []\n\n if hasattr(inspect, 'signature'): # Python 3\n params = inspect.signature(mtd).parameters # pylint: disable=no-member\n\n for k in params.keys():\n if params[k].default == inspect.Parameter.empty: # pylint: disable=no-member\n # Python 3 does not make a difference between unbound methods and functions, so the\n # only way to distinguish if the first argument is of a regular method, or a class\n # method, is to look for the conventional argument name. Yikes.\n if not (params[k].name == 'self' or params[k].name == 'cls'):\n required_params.append(k)\n else:\n optional_params.append(k)\n else: # Python 2\n params = inspect.getargspec(mtd) # pylint: disable=deprecated-method\n num = len(params[0]) if params[0] else 0\n n_opt = len(params[3]) if params[3] else 0\n n_req = (num - n_opt) if n_opt <= num else 0\n for i in range(0, n_req):\n required_params.append(params[0][i])\n for i in range(n_req, num):\n optional_params.append(params[0][i])\n\n if inspect.isroutine(getattr(cls, mtd_name)):\n bound_mtd = cls.__dict__[mtd_name]\n if not isinstance(bound_mtd, staticmethod):\n del required_params[0]\n\n return required_params, optional_params"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndrawing random samples from the Uniform distribution.", "response": "def uniform(nmr_distributions, nmr_samples, low=0, high=1, ctype='float', seed=None):\n \"\"\"Draw random samples from the Uniform distribution.\n\n Args:\n nmr_distributions (int): the number of unique continuous_distributions to create\n nmr_samples (int): The number of samples to draw\n low (double): The minimum value of the random numbers\n high (double): The minimum value of the random numbers\n ctype (str): the C type of the output samples\n seed (float): the seed for the RNG\n\n Returns:\n ndarray: A two dimensional numpy array as (nmr_distributions, nmr_samples).\n \"\"\"\n if is_scalar(low):\n low = np.ones((nmr_distributions, 1)) * low\n if is_scalar(high):\n high = np.ones((nmr_distributions, 1)) * high\n\n kernel_data = {'low': Array(low, as_scalar=True),\n 'high': Array(high, as_scalar=True)}\n\n kernel = SimpleCLFunction.from_string('''\n void compute(double low, double high, global uint* rng_state, global ''' + ctype + '''* samples){\n \n rand123_data rand123_rng_data = rand123_initialize_data((uint[]){\n rng_state[0], rng_state[1], rng_state[2], rng_state[3], \n rng_state[4], rng_state[5], 0});\n void* rng_data = (void*)&rand123_rng_data;\n\n for(uint i = 0; i < ''' + str(nmr_samples) + '''; i++){\n double4 randomnr = rand4(rng_data);\n samples[i] = (''' + ctype + ''')(low + randomnr.x * (high - low));\n }\n }\n ''', dependencies=[Rand123()])\n\n return _generate_samples(kernel, nmr_distributions, nmr_samples, ctype, kernel_data, seed=seed)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndrawing random samples from the Gaussian distribution.", "response": "def normal(nmr_distributions, nmr_samples, mean=0, std=1, ctype='float', seed=None):\n \"\"\"Draw random samples from the Gaussian distribution.\n\n Args:\n nmr_distributions (int): the number of unique continuous_distributions to create\n nmr_samples (int): The number of samples to draw\n mean (float or ndarray): The mean of the distribution\n std (float or ndarray): The standard deviation or the distribution\n ctype (str): the C type of the output samples\n seed (float): the seed for the RNG\n\n Returns:\n ndarray: A two dimensional numpy array as (nmr_distributions, nmr_samples).\n \"\"\"\n if is_scalar(mean):\n mean = np.ones((nmr_distributions, 1)) * mean\n if is_scalar(std):\n std = np.ones((nmr_distributions, 1)) * std\n\n kernel_data = {'mean': Array(mean, as_scalar=True),\n 'std': Array(std, as_scalar=True)}\n\n kernel = SimpleCLFunction.from_string('''\n void compute(double mean, double std, global uint* rng_state, global ''' + ctype + '''* samples){\n rand123_data rand123_rng_data = rand123_initialize_data((uint[]){\n rng_state[0], rng_state[1], rng_state[2], rng_state[3], \n rng_state[4], rng_state[5], 0});\n void* rng_data = (void*)&rand123_rng_data;\n\n for(uint i = 0; i < ''' + str(nmr_samples) + '''; i++){\n double4 randomnr = randn4(rng_data);\n samples[i] = (''' + ctype + ''')(mean + randomnr.x * std);\n }\n }\n ''', dependencies=[Rand123()])\n\n return _generate_samples(kernel, nmr_distributions, nmr_samples, ctype, kernel_data, seed=seed)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ntaking additional samples from the given likelihood and prior using this sampler.", "response": "def sample(self, nmr_samples, burnin=0, thinning=1):\n \"\"\"Take additional samples from the given likelihood and prior, using this sampler.\n\n This method can be called multiple times in which the sample state is stored in between.\n\n Args:\n nmr_samples (int): the number of samples to return\n burnin (int): the number of samples to discard before returning samples\n thinning (int): how many sample we wait before storing a new one. This will draw extra samples such that\n the total number of samples generated is ``nmr_samples * (thinning)`` and the number of samples\n stored is ``nmr_samples``. If set to one or lower we store every sample after the burn in.\n\n Returns:\n SamplingOutput: the sample output object\n \"\"\"\n if not thinning or thinning < 1:\n thinning = 1\n if not burnin or burnin < 0:\n burnin = 0\n\n max_samples_per_batch = max(1000 // thinning, 100)\n\n with self._logging(nmr_samples, burnin, thinning):\n if burnin > 0:\n for batch_start, batch_end in split_in_batches(burnin, max_samples_per_batch):\n self._sample(batch_end - batch_start, return_output=False)\n if nmr_samples > 0:\n outputs = []\n for batch_start, batch_end in split_in_batches(nmr_samples, max_samples_per_batch):\n outputs.append(self._sample(batch_end - batch_start, thinning=thinning))\n return SimpleSampleOutput(*[np.concatenate([o[ind] for o in outputs], axis=-1) for ind in range(3)])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _sample(self, nmr_samples, thinning=1, return_output=True):\n kernel_data = self._get_kernel_data(nmr_samples, thinning, return_output)\n sample_func = self._get_compute_func(nmr_samples, thinning, return_output)\n sample_func.evaluate(kernel_data, self._nmr_problems,\n use_local_reduction=all(env.is_gpu for env in self._cl_runtime_info.cl_environments),\n cl_runtime_info=self._cl_runtime_info)\n self._sampling_index += nmr_samples * thinning\n if return_output:\n return (kernel_data['samples'].get_data(),\n kernel_data['log_likelihoods'].get_data(),\n kernel_data['log_priors'].get_data())", "response": "Sample the given number of samples with the given thinning."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ninitializing the likelihood and prior using the given positions.", "response": "def _initialize_likelihood_prior(self, positions, log_likelihoods, log_priors):\n \"\"\"Initialize the likelihood and the prior using the given positions.\n\n This is a general method for computing the log likelihoods and log priors for given positions.\n\n Subclasses can use this to instantiate secondary chains as well.\n \"\"\"\n func = SimpleCLFunction.from_string('''\n void compute(global mot_float_type* chain_position,\n global mot_float_type* log_likelihood,\n global mot_float_type* log_prior,\n local mot_float_type* x_tmp,\n void* data){\n \n bool is_first_work_item = get_local_id(0) == 0;\n \n if(is_first_work_item){\n for(uint i = 0; i < ''' + str(self._nmr_params) + '''; i++){\n x_tmp[i] = chain_position[i];\n }\n *log_prior = _computeLogPrior(x_tmp, data);\n }\n barrier(CLK_LOCAL_MEM_FENCE);\n \n *log_likelihood = _computeLogLikelihood(x_tmp, data);\n }\n ''', dependencies=[self._get_log_prior_cl_func(), self._get_log_likelihood_cl_func()])\n\n kernel_data = {\n 'chain_position': Array(positions, 'mot_float_type', mode='rw', ensure_zero_copy=True),\n 'log_likelihood': Array(log_likelihoods, 'mot_float_type', mode='rw', ensure_zero_copy=True),\n 'log_prior': Array(log_priors, 'mot_float_type', mode='rw', ensure_zero_copy=True),\n 'x_tmp': LocalMemory('mot_float_type', self._nmr_params),\n 'data': self._data\n }\n\n func.evaluate(kernel_data, self._nmr_problems,\n use_local_reduction=all(env.is_gpu for env in self._cl_runtime_info.cl_environments),\n cl_runtime_info=self._cl_runtime_info)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_kernel_data(self, nmr_samples, thinning, return_output):\n kernel_data = {\n 'data': self._data,\n 'method_data': self._get_mcmc_method_kernel_data(),\n 'nmr_iterations': Scalar(nmr_samples * thinning, ctype='ulong'),\n 'iteration_offset': Scalar(self._sampling_index, ctype='ulong'),\n 'rng_state': Array(self._rng_state, 'uint', mode='rw', ensure_zero_copy=True),\n 'current_chain_position': Array(self._current_chain_position, 'mot_float_type',\n mode='rw', ensure_zero_copy=True),\n 'current_log_likelihood': Array(self._current_log_likelihood, 'mot_float_type',\n mode='rw', ensure_zero_copy=True),\n 'current_log_prior': Array(self._current_log_prior, 'mot_float_type',\n mode='rw', ensure_zero_copy=True),\n }\n\n if return_output:\n kernel_data.update({\n 'samples': Zeros((self._nmr_problems, self._nmr_params, nmr_samples), ctype='mot_float_type'),\n 'log_likelihoods': Zeros((self._nmr_problems, nmr_samples), ctype='mot_float_type'),\n 'log_priors': Zeros((self._nmr_problems, nmr_samples), ctype='mot_float_type'),\n })\n return kernel_data", "response": "This method is used to get the kernel data for the current entry of the current entry in the MCMC sampler."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the MCMC algorithm as a computable function.", "response": "def _get_compute_func(self, nmr_samples, thinning, return_output):\n \"\"\"Get the MCMC algorithm as a computable function.\n\n Args:\n nmr_samples (int): the number of samples we will draw\n thinning (int): the thinning factor we want to use\n return_output (boolean): if the kernel should return output\n\n Returns:\n mot.lib.cl_function.CLFunction: the compute function\n \"\"\"\n cl_func = '''\n void compute(global uint* rng_state, \n global mot_float_type* current_chain_position,\n global mot_float_type* current_log_likelihood,\n global mot_float_type* current_log_prior,\n ulong iteration_offset, \n ulong nmr_iterations, \n ''' + ('''global mot_float_type* samples, \n global mot_float_type* log_likelihoods,\n global mot_float_type* log_priors,''' if return_output else '') + '''\n void* method_data, \n void* data){\n \n bool is_first_work_item = get_local_id(0) == 0;\n \n rand123_data rand123_rng_data = rand123_initialize_data((uint[]){\n rng_state[0], rng_state[1], rng_state[2], rng_state[3], \n rng_state[4], rng_state[5], 0, 0});\n void* rng_data = (void*)&rand123_rng_data;\n \n for(ulong i = 0; i < nmr_iterations; i++){\n '''\n if return_output:\n cl_func += '''\n if(is_first_work_item){\n if(i % ''' + str(thinning) + ''' == 0){\n log_likelihoods[i / ''' + str(thinning) + '''] = *current_log_likelihood;\n log_priors[i / ''' + str(thinning) + '''] = *current_log_prior;\n \n for(uint j = 0; j < ''' + str(self._nmr_params) + '''; j++){\n samples[(ulong)(i / ''' + str(thinning) + ''') // remove the interval\n + j * ''' + str(nmr_samples) + ''' // parameter index\n ] = current_chain_position[j];\n }\n }\n }\n '''\n cl_func += '''\n _advanceSampler(method_data, data, i + iteration_offset, rng_data, \n current_chain_position, current_log_likelihood, current_log_prior);\n }\n\n if(is_first_work_item){\n uint state[8];\n rand123_data_to_array(rand123_rng_data, state);\n for(uint i = 0; i < 6; i++){\n rng_state[i] = state[i];\n }\n }\n }\n '''\n return SimpleCLFunction.from_string(\n cl_func,\n dependencies=[Rand123(), self._get_log_prior_cl_func(),\n self._get_log_likelihood_cl_func(),\n SimpleCLCodeObject(self._get_state_update_cl_func(nmr_samples, thinning, return_output))])"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the CL log prior compute function.", "response": "def _get_log_prior_cl_func(self):\n \"\"\"Get the CL log prior compute function.\n\n Returns:\n str: the compute function for computing the log prior.\n \"\"\"\n return SimpleCLFunction.from_string('''\n mot_float_type _computeLogPrior(local const mot_float_type* x, void* data){\n return ''' + self._log_prior_func.get_cl_function_name() + '''(x, data);\n }\n ''', dependencies=[self._log_prior_func])"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the CL log likelihood compute function.", "response": "def _get_log_likelihood_cl_func(self):\n \"\"\"Get the CL log likelihood compute function.\n\n This uses local reduction to compute the log likelihood for every observation in CL local space.\n The results are then summed in the first work item and returned using a pointer.\n\n Returns:\n str: the CL code for the log likelihood compute func.\n \"\"\"\n return SimpleCLFunction.from_string('''\n double _computeLogLikelihood(local const mot_float_type* current_position, void* data){\n return ''' + self._ll_func.get_cl_function_name() + '''(current_position, data);\n }\n ''', dependencies=[self._ll_func])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the mcmc method kernel data elements. Used by _get_mcmc_method_kernel_data.", "response": "def _get_mcmc_method_kernel_data_elements(self):\n \"\"\"Get the mcmc method kernel data elements. Used by :meth:`_get_mcmc_method_kernel_data`.\"\"\"\n return {'proposal_stds': Array(self._proposal_stds, 'mot_float_type', mode='rw', ensure_zero_copy=True),\n 'x_tmp': LocalMemory('mot_float_type', nmr_items=1 + self._nmr_params)}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncalculate and return the log likelihood of the given model for the given parameters.", "response": "def compute_log_likelihood(ll_func, parameters, data=None, cl_runtime_info=None):\n \"\"\"Calculate and return the log likelihood of the given model for the given parameters.\n\n This calculates the log likelihoods for every problem in the model (typically after optimization),\n or a log likelihood for every sample of every model (typically after sample). In the case of the first (after\n optimization), the parameters must be an (d, p) array for d problems and p parameters. In the case of the\n second (after sample), you must provide this function with a matrix of shape (d, p, n) with d problems,\n p parameters and n samples.\n\n Args:\n ll_func (mot.lib.cl_function.CLFunction): The log-likelihood function. A CL function with the signature:\n\n .. code-block:: c\n\n double (local const mot_float_type* const x, void* data);\n\n parameters (ndarray): The parameters to use in the evaluation of the model. This is either an (d, p) matrix\n or (d, p, n) matrix with d problems, p parameters and n samples.\n data (mot.lib.kernel_data.KernelData): the user provided data for the ``void* data`` pointer.\n cl_runtime_info (mot.configuration.CLRuntimeInfo): the runtime information\n\n Returns:\n ndarray: per problem the log likelihood, or, per problem and per sample the log likelihood.\n \"\"\"\n\n def get_cl_function():\n nmr_params = parameters.shape[1]\n\n if len(parameters.shape) > 2:\n return SimpleCLFunction.from_string('''\n void compute(global mot_float_type* parameters, \n global mot_float_type* log_likelihoods,\n void* data){\n \n local mot_float_type x[''' + str(nmr_params) + '''];\n\n for(uint sample_ind = 0; sample_ind < ''' + str(parameters.shape[2]) + '''; sample_ind++){\n for(uint i = 0; i < ''' + str(nmr_params) + '''; i++){\n x[i] = parameters[i *''' + str(parameters.shape[2]) + ''' + sample_ind];\n }\n \n double ll = ''' + ll_func.get_cl_function_name() + '''(x, data);\n if(get_local_id(0) == 0){\n log_likelihoods[sample_ind] = ll;\n }\n }\n }\n ''', dependencies=[ll_func])\n\n return SimpleCLFunction.from_string('''\n void compute(local mot_float_type* parameters, \n global mot_float_type* log_likelihoods,\n void* data){\n \n double ll = ''' + ll_func.get_cl_function_name() + '''(parameters, data);\n if(get_local_id(0) == 0){\n *(log_likelihoods) = ll;\n }\n }\n ''', dependencies=[ll_func])\n\n kernel_data = {'data': data,\n 'parameters': Array(parameters, 'mot_float_type', mode='r')}\n\n shape = parameters.shape\n if len(shape) > 2:\n kernel_data.update({\n 'log_likelihoods': Zeros((shape[0], shape[2]), 'mot_float_type'),\n })\n else:\n kernel_data.update({\n 'log_likelihoods': Zeros((shape[0],), 'mot_float_type'),\n })\n\n get_cl_function().evaluate(kernel_data, parameters.shape[0], use_local_reduction=True,\n cl_runtime_info=cl_runtime_info)\n\n return kernel_data['log_likelihoods'].get_data()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef compute_objective_value(objective_func, parameters, data=None, cl_runtime_info=None):\n return objective_func.evaluate({'data': data, 'parameters': Array(parameters, 'mot_float_type', mode='r')},\n parameters.shape[0], use_local_reduction=True, cl_runtime_info=cl_runtime_info)", "response": "Calculate and return the objective function value of the given model for the given parameters."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_log(username):\n redis = get_redis_client()\n log_key = 'log:{}'.format(username)\n raw_log = redis.lrange(log_key, 0, -1)\n log = []\n for raw_item in raw_log:\n item = json.loads(raw_item.decode())\n item['datetime'] = convert_timestamp(item.pop('time'))\n log.append(item)\n return log", "response": "Get a list of page views for a given user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nobtaining an access token that can be passed to a websocket client.", "response": "def get_token(username, length=20, timeout=20):\n \"\"\"\n Obtain an access token that can be passed to a websocket client.\n \"\"\"\n redis = get_redis_client()\n token = get_random_string(length)\n token_key = 'token:{}'.format(token)\n redis.set(token_key, username)\n redis.expire(token_key, timeout)\n return token"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_log(self, request, response):\n return {\n 'method': request.method,\n 'path': request.get_full_path(),\n 'code': response.status_code,\n 'time': time.time(),\n }", "response": "Return a dict of data to log for a given request and response."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndownloads a song from a Google Music library.", "response": "def download(self, song):\n\t\t\"\"\"Download a song from a Google Music library.\n\n\t\tParameters:\n\t\t\tsong (dict): A song dict.\n\n\t\tReturns:\n\t\t\ttuple: Song content as bytestring, suggested filename.\n\t\t\"\"\"\n\n\t\tsong_id = song['id']\n\n\t\tresponse = self._call(\n\t\t\tmm_calls.Export,\n\t\t\tself.uploader_id,\n\t\t\tsong_id)\n\t\taudio = response.body\n\t\tsuggested_filename = unquote(\n\t\t\tresponse.headers['Content-Disposition'].split(\"filename*=UTF-8''\")[-1]\n\t\t)\n\n\t\treturn (audio, suggested_filename)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the uploaded track count and allowance.", "response": "def quota(self):\n\t\t\"\"\"Get the uploaded track count and allowance.\n\n\t\tReturns:\n\t\t\ttuple: Number of uploaded tracks, number of tracks allowed.\n\t\t\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmm_calls.ClientState,\n\t\t\tself.uploader_id\n\t\t)\n\t\tclient_state = response.body.clientstate_response\n\n\t\treturn (client_state.total_track_count, client_state.locker_track_limit)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a listing of Music Library songs.", "response": "def songs(self, *, uploaded=True, purchased=True):\n\t\t\"\"\"Get a listing of Music Library songs.\n\n\t\tReturns:\n\t\t\tlist: Song dicts.\n\t\t\"\"\"\n\n\t\tif not uploaded and not purchased:\n\t\t\traise ValueError(\"'uploaded' and 'purchased' cannot both be False.\")\n\n\t\tif purchased and uploaded:\n\t\t\tsong_list = []\n\t\t\tfor chunk in self.songs_iter(export_type=1):\n\t\t\t\tsong_list.extend(chunk)\n\t\telif purchased:\n\t\t\tsong_list = []\n\t\t\tfor chunk in self.songs_iter(export_type=2):\n\t\t\t\tsong_list.extend(chunk)\n\t\telif uploaded:\n\t\t\tpurchased_songs = []\n\t\t\tfor chunk in self.songs_iter(export_type=2):\n\t\t\t\tpurchased_songs.extend(chunk)\n\n\t\t\tsong_list = [\n\t\t\t\tsong\n\t\t\t\tfor chunk in self.songs_iter(export_type=1)\n\t\t\t\tfor song in chunk\n\t\t\t\tif song not in purchased_songs\n\t\t\t]\n\n\t\treturn song_list"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a paged iterator of Music Library songs.", "response": "def songs_iter(self, *, continuation_token=None, export_type=1):\n\t\t\"\"\"Get a paged iterator of Music Library songs.\n\n\t\tParameters:\n\t\t\tcontinuation_token (str, Optional): The token of the page to return.\n\t\t\t\tDefault: Not sent to get first page.\n\t\t\texport_type (int, Optional): The type of tracks to return. 1 for all tracks, 2 for promotional and purchased.\n\t\t\t\tDefault: ``1``\n\n\t\tYields:\n\t\t\tlist: Song dicts.\n\t\t\"\"\"\n\n\t\tdef track_info_to_dict(track_info):\n\t\t\treturn dict(\n\t\t\t\t(field.name, value)\n\t\t\t\tfor field, value in track_info.ListFields()\n\t\t\t)\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmm_calls.ExportIDs,\n\t\t\t\tself.uploader_id,\n\t\t\t\tcontinuation_token=continuation_token,\n\t\t\t\texport_type=export_type\n\t\t\t)\n\n\t\t\titems = [\n\t\t\t\ttrack_info_to_dict(track_info)\n\t\t\t\tfor track_info in response.body.download_track_info\n\t\t\t]\n\n\t\t\tif items:\n\t\t\t\tyield items\n\n\t\t\tcontinuation_token = response.body.continuation_token\n\n\t\t\tif not continuation_token:\n\t\t\t\tbreak"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef upload(\n\t\tself,\n\t\tsong,\n\t\t*,\n\t\talbum_art_path=None,\n\t\tno_sample=False\n\t):\n\t\t\"\"\"Upload a song to a Google Music library.\n\n\t\tParameters:\n\t\t\tsong (os.PathLike or str or audio_metadata.Format):\n\t\t\t\tThe path to an audio file or an instance of :class:`audio_metadata.Format`.\n\t\t\talbum_art_path (os.PathLike or str, Optional):\n\t\t\t\tThe relative filename or absolute filepath to external album art.\n\t\t\tno_sample(bool, Optional):\n\t\t\t\tDon't generate an audio sample from song;\n\t\t\t\tsend empty audio sample.\n\t\t\t\tDefault: Create an audio sample using ffmpeg/avconv.\n\n\t\tReturns:\n\t\t\tdict: A result dict with keys: ``'filepath'``, ``'success'``, ``'reason'``, and ``'song_id'`` (if successful).\n\t\t\"\"\"\n\n\t\tif not isinstance(song, audio_metadata.Format):\n\t\t\ttry:\n\t\t\t\tsong = audio_metadata.load(song)\n\t\t\texcept audio_metadata.UnsupportedFormat:\n\t\t\t\traise ValueError(\"'song' must be FLAC, MP3, or WAV.\")\n\n\t\tif album_art_path:\n\t\t\talbum_art_path = Path(album_art_path).resolve()\n\n\t\t\tif album_art_path.is_file():\n\t\t\t\twith album_art_path.open('rb') as image_file:\n\t\t\t\t\texternal_art = image_file.read()\n\t\t\telse:\n\t\t\t\texternal_art = None\n\t\telse:\n\t\t\texternal_art = None\n\n\t\tresult = {'filepath': Path(song.filepath)}\n\n\t\ttrack_info = mm_calls.Metadata.get_track_info(song)\n\t\tresponse = self._call(\n\t\t\tmm_calls.Metadata,\n\t\t\tself.uploader_id,\n\t\t\t[track_info]\n\t\t)\n\n\t\tmetadata_response = response.body.metadata_response\n\n\t\tif metadata_response.signed_challenge_info: # Sample requested.\n\t\t\tsample_request = metadata_response.signed_challenge_info[0]\n\n\t\t\ttry:\n\t\t\t\ttrack_sample = mm_calls.Sample.generate_sample(\n\t\t\t\t\tsong,\n\t\t\t\t\ttrack_info,\n\t\t\t\t\tsample_request,\n\t\t\t\t\texternal_art=external_art,\n\t\t\t\t\tno_sample=no_sample\n\t\t\t\t)\n\t\t\t\tresponse = self._call(\n\t\t\t\t\tmm_calls.Sample,\n\t\t\t\t\tself.uploader_id,\n\t\t\t\t\t[track_sample]\n\t\t\t\t)\n\t\t\t\ttrack_sample_response = response.body.sample_response.track_sample_response[0]\n\t\t\texcept (OSError, ValueError, subprocess.CalledProcessError):\n\t\t\t\traise # TODO\n\t\telse:\n\t\t\ttrack_sample_response = metadata_response.track_sample_response[0]\n\n\t\tresponse_code = track_sample_response.response_code\n\n\t\tif response_code == upload_pb2.TrackSampleResponse.MATCHED:\n\t\t\tresult.update(\n\t\t\t\t{\n\t\t\t\t\t'success': True,\n\t\t\t\t\t'reason': 'Matched',\n\t\t\t\t\t'song_id': track_sample_response.server_track_id\n\t\t\t\t}\n\t\t\t)\n\t\telif response_code == upload_pb2.TrackSampleResponse.UPLOAD_REQUESTED:\n\t\t\tserver_track_id = track_sample_response.server_track_id\n\n\t\t\tself._call(\n\t\t\t\tmm_calls.UploadState,\n\t\t\t\tself.uploader_id,\n\t\t\t\t'START'\n\t\t\t)\n\n\t\t\tattempts = 0\n\t\t\tshould_retry = True\n\n\t\t\twhile should_retry and attempts <= 10:\n\t\t\t\t# Call with tenacity.retry_with to disable automatic retries.\n\t\t\t\tresponse = self._call.retry_with(stop=stop_after_attempt(1))(\n\t\t\t\t\tself,\n\t\t\t\t\tmm_calls.ScottyAgentPost,\n\t\t\t\t\tself.uploader_id,\n\t\t\t\t\tserver_track_id,\n\t\t\t\t\ttrack_info,\n\t\t\t\t\tsong,\n\t\t\t\t\texternal_art=external_art,\n\t\t\t\t\ttotal_song_count=1,\n\t\t\t\t\ttotal_uploaded_count=0\n\t\t\t\t)\n\n\t\t\t\tsession_response = response.body\n\n\t\t\t\tif 'sessionStatus' in session_response:\n\t\t\t\t\tbreak\n\n\t\t\t\ttry:\n\t\t\t\t\t# WHY, GOOGLE?! WHY???????????\n\t\t\t\t\tstatus_code = session_response['errorMessage']['additionalInfo'][\n\t\t\t\t\t\t'uploader_service.GoogleRupioAdditionalInfo'\n\t\t\t\t\t]['completionInfo']['customerSpecificInfo'][\n\t\t\t\t\t\t'ResponseCode'\n\t\t\t\t\t]\n\t\t\t\texcept KeyError:\n\t\t\t\t\tstatus_code = None\n\n\t\t\t\tif status_code == 503: # Upload server still syncing.\n\t\t\t\t\tshould_retry = True\n\t\t\t\t\treason = \"Server syncing\"\n\t\t\t\telif status_code == 200: # Song is already uploaded.\n\t\t\t\t\tshould_retry = False\n\t\t\t\t\treason = \"Already uploaded\"\n\t\t\t\telif status_code == 404: # Rejected.\n\t\t\t\t\tshould_retry = False\n\t\t\t\t\treason = \"Rejected\"\n\t\t\t\telse:\n\t\t\t\t\tshould_retry = True\n\t\t\t\t\treason = \"Unkown error\"\n\n\t\t\t\tattempts += 1\n\n\t\t\t\ttime.sleep(2) # Give the server time to sync.\n\t\t\telse:\n\t\t\t\tresult.update(\n\t\t\t\t\t{\n\t\t\t\t\t\t'success': False,\n\t\t\t\t\t\t'reason': f'Could not get upload session: {reason}'\n\t\t\t\t\t}\n\t\t\t\t)\n\n\t\t\tif 'success' not in result:\n\t\t\t\ttransfer = session_response['sessionStatus']['externalFieldTransfers'][0]\n\n\t\t\t\tupload_url = transfer['putInfo']['url']\n\t\t\t\tcontent_type = transfer.get('content_type', 'audio/mpeg')\n\t\t\t\toriginal_content_type = track_info.original_content_type\n\n\t\t\t\ttranscode = (\n\t\t\t\t\tisinstance(song, audio_metadata.WAV)\n\t\t\t\t\tor original_content_type != locker_pb2.Track.MP3\n\t\t\t\t)\n\n\t\t\t\tif (\n\t\t\t\t\ttranscode\n\t\t\t\t\tor original_content_type == locker_pb2.Track.MP3\n\t\t\t\t):\n\t\t\t\t\tif transcode:\n\t\t\t\t\t\taudio_file = transcode_to_mp3(song, quality='320k')\n\t\t\t\t\telse:\n\t\t\t\t\t\twith open(song.filepath, 'rb') as f:\n\t\t\t\t\t\t\taudio_file = f.read()\n\n\t\t\t\t\t# Google Music allows a maximum file size of 300 MiB.\n\t\t\t\t\tif len(audio_file) >= 300 * 1024 * 1024:\n\t\t\t\t\t\tresult.update(\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t'success': False,\n\t\t\t\t\t\t\t\t'reason': 'Maximum allowed file size is 300 MiB.'\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t)\n\t\t\t\t\telse:\n\t\t\t\t\t\tupload_response = self._call(\n\t\t\t\t\t\t\tmm_calls.ScottyAgentPut,\n\t\t\t\t\t\t\tupload_url,\n\t\t\t\t\t\t\taudio_file,\n\t\t\t\t\t\t\tcontent_type=content_type\n\t\t\t\t\t\t).body\n\n\t\t\t\t\t\tif upload_response.get('sessionStatus', {}).get('state'):\n\t\t\t\t\t\t\tresult.update(\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t'success': True,\n\t\t\t\t\t\t\t\t\t'reason': 'Uploaded',\n\t\t\t\t\t\t\t\t\t'song_id': track_sample_response.server_track_id\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tresult.update(\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t'success': False,\n\t\t\t\t\t\t\t\t\t'reason': upload_response # TODO: Better error details.\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t)\n\t\t\t\telse:\n\t\t\t\t\t# Do not upload files if transcode option set to False.\n\t\t\t\t\tresult.update(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'success': False,\n\t\t\t\t\t\t\t'reason': 'Transcoding disabled for file type.'\n\t\t\t\t\t\t}\n\t\t\t\t\t)\n\n\t\t\t\tself._call(mm_calls.UploadState, self.uploader_id, 'STOPPED')\n\t\telse:\n\t\t\tresponse_codes = upload_pb2._TRACKSAMPLERESPONSE.enum_types[0]\n\t\t\tresponse_type = response_codes.values_by_number[\n\t\t\t\ttrack_sample_response.response_code\n\t\t\t].name\n\n\t\t\treason = response_type\n\n\t\t\tresult.update(\n\t\t\t\t{\n\t\t\t\t\t'success': False,\n\t\t\t\t\t'reason': f'{reason}'\n\t\t\t\t}\n\t\t\t)\n\n\t\t\tif response_type == 'ALREADY_EXISTS':\n\t\t\t\tresult['song_id'] = track_sample_response.server_track_id\n\n\t\treturn result", "response": "Upload a song to a Google Music library."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlog in to Google Music.", "response": "def login(self, username, *, token=None):\n\t\t\"\"\"Log in to Google Music.\n\n\t\tParameters:\n\t\t\tusername (str, Optional): Your Google Music username.\n\t\t\t\tUsed for keeping stored OAuth tokens for multiple accounts separate.\n\t\t\tdevice_id (str, Optional): A mobile device ID or music manager uploader ID.\n\t\t\t\tDefault: MAC address is used.\n\t\t\ttoken (dict, Optional): An OAuth token compatible with ``requests-oauthlib``.\n\n\t\tReturns:\n\t\t\tbool: ``True`` if successfully authenticated, ``False`` if not.\n\t\t\"\"\"\n\n\t\tself._username = username\n\t\tself._oauth(username, token=token)\n\n\t\treturn self.is_authenticated"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlogs in to Google Music with a different user.", "response": "def switch_user(self, username='', *, token=None):\n\t\t\"\"\"Log in to Google Music with a different user.\n\n\t\tParameters:\n\t\t\tusername (str, Optional): Your Google Music username.\n\t\t\t\tUsed for keeping stored OAuth tokens for multiple accounts separate.\n\t\t\ttoken (dict, Optional): An OAuth token compatible with ``requests-oauthlib``.\n\n\t\tReturns:\n\t\t\tbool: ``True`` if successfully authenticated, ``False`` if not.\n\t\t\"\"\"\n\n\t\tif self.logout():\n\t\t\treturn self.login(username, token=token)\n\n\t\treturn False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gen_tau(S, K, delta):\n pivot = floor(K/S)\n return [S/K * 1/d for d in range(1, pivot)] \\\n + [S/K * log(S/delta)] \\\n + [0 for d in range(pivot, K)]", "response": "The Robust part of the RSD"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef gen_mu(K, delta, c):\n\n S = c * log(K/delta) * sqrt(K) \n tau = gen_tau(S, K, delta)\n rho = gen_rho(K)\n normalizer = sum(rho) + sum(tau)\n return [(rho[d] + tau[d])/normalizer for d in range(K)]", "response": "The Robust Soliton Distribution on the degree of \n transmitted blocks\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef gen_rsd_cdf(K, delta, c):\n\n mu = gen_mu(K, delta, c)\n return [sum(mu[:d+1]) for d in range(K)]", "response": "The CDF of the RSD on block degree precomputed for\n sampling speed"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_next(self):\n\n self.state = PRNG_A * self.state % PRNG_M\n return self.state", "response": "Executes the next iteration of the PRNG\n evolution process and returns the result."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsample degree given the precomputed distributions above and the linear PRNG output .", "response": "def _sample_d(self):\n \"\"\"Samples degree given the precomputed\n distributions above and the linear PRNG output\n \"\"\"\n\n p = self._get_next() / PRNG_MAX_RAND\n for ix, v in enumerate(self.cdf):\n if v > p:\n return ix + 1\n return ix + 1"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run(fn, blocksize, seed, c, delta):\n\n with open(fn, 'rb') as f:\n for block in encode.encoder(f, blocksize, seed, c, delta):\n sys.stdout.buffer.write(block)", "response": "Run the encoder until the file is broken"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_subscribed(self):\n\n\t\tsubscribed = next(\n\t\t\t(\n\t\t\t\tconfig_item['value'] == 'true'\n\t\t\t\tfor config_item in self.config()\n\t\t\t\tif config_item['key'] == 'isNautilusUser'\n\t\t\t),\n\t\t\tNone\n\t\t)\n\n\t\tif subscribed:\n\t\t\tself.tier = 'aa'\n\t\telse:\n\t\t\tself.tier = 'fr'\n\n\t\treturn subscribed", "response": "Returns the subscription status of the account linked to the MobileClient instance."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef album(self, album_id, *, include_description=True, include_songs=True):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.FetchAlbum,\n\t\t\talbum_id,\n\t\t\tinclude_description=include_description,\n\t\t\tinclude_tracks=include_songs\n\t\t)\n\t\talbum_info = response.body\n\n\t\treturn album_info", "response": "Get information about an album."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget information about an artist.", "response": "def artist(\n\t\tself, artist_id, *, include_albums=True, num_related_artists=5, num_top_tracks=5\n\t):\n\t\t\"\"\"Get information about an artist.\n\n\t\tParameters:\n\t\t\tartist_id (str): An artist ID. Artist IDs start with an 'A'.\n\t\t\tinclude_albums (bool, Optional): Include albums by the artist in returned dict.\n\t\t\t\tDefault: ``True``.\n\t\t\tnum_related_artists (int, Optional): Include up to given number of related artists in returned dict.\n\t\t\t\tDefault: ``5``.\n\t\t\tnum_top_tracks (int, Optional): Include up to given number of top tracks in returned dict.\n\t\t\t\tDefault: ``5``.\n\n\t\tReturns:\n\t\t\tdict: Artist information.\n\t\t\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.FetchArtist,\n\t\t\tartist_id,\n\t\t\tinclude_albums=include_albums,\n\t\t\tnum_related_artists=num_related_artists,\n\t\t\tnum_top_tracks=num_top_tracks\n\t\t)\n\t\tartist_info = response.body\n\n\t\treturn artist_info"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef browse_podcasts(self, podcast_genre_id='JZCpodcasttopchartall'):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.PodcastBrowse,\n\t\t\tpodcast_genre_id=podcast_genre_id\n\t\t)\n\t\tpodcast_series_list = response.body.get('series', [])\n\n\t\treturn podcast_series_list", "response": "Get the podcasts for a genre from the Podcasts browse tab."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef browse_podcasts_genres(self):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.PodcastBrowseHierarchy\n\t\t)\n\t\tgenres = response.body.get('groups', [])\n\n\t\treturn genres", "response": "Get the genres from the Podcasts browse tab dropdown."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the stations for a category from Browse Stations.", "response": "def browse_stations(self, station_category_id):\n\t\t\"\"\"Get the stations for a category from Browse Stations.\n\n\t\tParameters:\n\t\t\tstation_category_id (str): A station category ID as\n\t\t\t\tfound with :meth:`browse_stations_categories`.\n\n\t\tReturns:\n\t\t\tlist: Station dicts.\n\t\t\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.BrowseStations,\n\t\t\tstation_category_id\n\t\t)\n\t\tstations = response.body.get('stations', [])\n\n\t\treturn stations"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting the categories from Browse Stations.", "response": "def browse_stations_categories(self):\n\t\t\"\"\"Get the categories from Browse Stations.\n\n\t\tReturns:\n\t\t\tlist: Station categories that can contain subcategories.\n\t\t\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.BrowseStationCategories\n\t\t)\n\t\tstation_categories = response.body.get('root', {}).get('subcategories', [])\n\n\t\treturn station_categories"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef config(self):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.Config\n\t\t)\n\t\tconfig_list = response.body.get('data', {}).get('entries', [])\n\n\t\treturn config_list", "response": "Get a listing of mobile client configuration settings."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef device_set(self, device):\n\n\t\tif device['id'].startswith('0x'):\n\t\t\tself.device_id = device['id'][2:]\n\t\telif device['id'].startswith('ios:'):\n\t\t\tself.device_id = device['id'].replace(':', '')\n\t\telse:\n\t\t\tself.device_id = device['id']", "response": "Set device used by MobileClient instance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef devices(self):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.DeviceManagementInfo\n\t\t)\n\t\tregistered_devices = response.body.get('data', {}).get('items', [])\n\n\t\treturn registered_devices", "response": "Get a listing of devices registered to the Google Music account."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting a listing of song genres.", "response": "def explore_genres(self, parent_genre_id=None):\n\t\t\"\"\"Get a listing of song genres.\n\n\t\tParameters:\n\t\t\tparent_genre_id (str, Optional): A genre ID.\n\t\t\t\tIf given, a listing of this genre's sub-genres is returned.\n\n\t\tReturns:\n\t\t\tlist: Genre dicts.\n\t\t\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.ExploreGenres,\n\t\t\tparent_genre_id\n\t\t)\n\t\tgenre_list = response.body.get('genres', [])\n\n\t\treturn genre_list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef explore_tabs(self, *, num_items=100, genre_id=None):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.ExploreTabs,\n\t\t\tnum_items=num_items,\n\t\t\tgenre_id=genre_id\n\t\t)\n\t\ttab_list = response.body.get('tabs', [])\n\n\t\texplore_tabs = {}\n\t\tfor tab in tab_list:\n\t\t\texplore_tabs[tab['tab_type'].lower()] = tab\n\n\t\treturn explore_tabs", "response": "Get a listing of explore tabs."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a listing of items dismissed from Listen Now tab.", "response": "def listen_now_dismissed_items(self):\n\t\t\"\"\"Get a listing of items dismissed from Listen Now tab.\"\"\"\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.ListenNowGetDismissedItems\n\t\t)\n\t\tdismissed_items = response.body.get('items', [])\n\n\t\treturn dismissed_items"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef listen_now_items(self):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.ListenNowGetListenNowItems\n\t\t)\n\t\tlisten_now_item_list = response.body.get('listennow_items', [])\n\n\t\tlisten_now_items = defaultdict(list)\n\t\tfor item in listen_now_item_list:\n\t\t\ttype_ = f\"{ListenNowItemType(item['type']).name}s\"\n\t\t\tlisten_now_items[type_].append(item)\n\n\t\treturn dict(listen_now_items)", "response": "Get a listing of Listen Now items."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets information about a playlist song.", "response": "def playlist_song(self, playlist_song_id):\n\t\t\"\"\"Get information about a playlist song.\n\n\t\tNote:\n\t\t\tThis returns the playlist entry information only.\n\t\t\tFor full song metadata, use :meth:`song` with\n\t\t\tthe ``'trackId'`` field.\n\n\t\tParameters:\n\t\t\tplaylist_song_id (str): A playlist song ID.\n\n\t\tReturns:\n\t\t\tdict: Playlist song information.\n\t\t\"\"\"\n\n\t\tplaylist_song_info = next(\n\t\t\t(\n\t\t\t\tplaylist_song\n\t\t\t\tfor playlist in self.playlists(include_songs=True)\n\t\t\t\tfor playlist_song in playlist['tracks']\n\t\t\t\tif playlist_song['id'] == playlist_song_id\n\t\t\t),\n\t\t\tNone\n\t\t)\n\n\t\treturn playlist_song_info"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef playlist_song_add(\n\t\tself,\n\t\tsong,\n\t\tplaylist,\n\t\t*,\n\t\tafter=None,\n\t\tbefore=None,\n\t\tindex=None,\n\t\tposition=None\n\t):\n\t\t\"\"\"Add a song to a playlist.\n\n\t\tNote:\n\t\t\t* Provide no optional arguments to add to end.\n\t\t\t* Provide playlist song dicts for ``after`` and/or ``before``.\n\t\t\t* Provide a zero-based ``index``.\n\t\t\t* Provide a one-based ``position``.\n\n\t\t\tSongs are inserted *at* given index or position.\n\t\t\tIt's also possible to add to the end by using\n\t\t\t``len(songs)`` for index or ``len(songs) + 1`` for position.\n\n\t\tParameters:\n\t\t\tsong (dict): A song dict.\n\t\t\tplaylist (dict): A playlist dict.\n\t\t\tafter (dict, Optional): A playlist song dict ``songs`` will follow.\n\t\t\tbefore (dict, Optional): A playlist song dict ``songs`` will precede.\n\t\t\tindex (int, Optional): The zero-based index position to insert ``song``.\n\t\t\tposition (int, Optional): The one-based position to insert ``song``.\n\n\t\tReturns:\n\t\t\tdict: Playlist dict including songs.\n\t\t\"\"\"\n\n\t\tprev, next_ = get_ple_prev_next(\n\t\t\tself.playlist_songs(playlist),\n\t\t\tafter=after,\n\t\t\tbefore=before,\n\t\t\tindex=index,\n\t\t\tposition=position\n\t\t)\n\n\t\tif 'storeId' in song:\n\t\t\tsong_id = song['storeId']\n\t\telif 'trackId' in song:\n\t\t\tsong_id = song['trackId']\n\t\telse:\n\t\t\tsong_id = song['id']\n\n\t\tmutation = mc_calls.PlaylistEntriesBatch.create(\n\t\t\tsong_id, playlist['id'],\n\t\t\tpreceding_entry_id=prev.get('id'),\n\t\t\tfollowing_entry_id=next_.get('id')\n\t\t)\n\t\tself._call(mc_calls.PlaylistEntriesBatch, mutation)\n\n\t\treturn self.playlist(playlist['id'], include_songs=True)", "response": "Add a song to a playlist."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef playlist_songs_add(\n\t\tself,\n\t\tsongs,\n\t\tplaylist,\n\t\t*,\n\t\tafter=None,\n\t\tbefore=None,\n\t\tindex=None,\n\t\tposition=None\n\t):\n\t\t\"\"\"Add songs to a playlist.\n\n\t\tNote:\n\t\t\t* Provide no optional arguments to add to end.\n\t\t\t* Provide playlist song dicts for ``after`` and/or ``before``.\n\t\t\t* Provide a zero-based ``index``.\n\t\t\t* Provide a one-based ``position``.\n\n\t\t\tSongs are inserted *at* given index or position.\n\t\t\tIt's also possible to add to the end by using\n\t\t\t``len(songs)`` for index or ``len(songs) + 1`` for position.\n\n\t\tParameters:\n\t\t\tsongs (list): A list of song dicts.\n\t\t\tplaylist (dict): A playlist dict.\n\t\t\tafter (dict, Optional): A playlist song dict ``songs`` will follow.\n\t\t\tbefore (dict, Optional): A playlist song dict ``songs`` will precede.\n\t\t\tindex (int, Optional): The zero-based index position to insert ``songs``.\n\t\t\tposition (int, Optional): The one-based position to insert ``songs``.\n\n\t\tReturns:\n\t\t\tdict: Playlist dict including songs.\n\t\t\"\"\"\n\n\t\tplaylist_songs = self.playlist_songs(playlist)\n\n\t\tprev, next_ = get_ple_prev_next(\n\t\t\tplaylist_songs,\n\t\t\tafter=after,\n\t\t\tbefore=before,\n\t\t\tindex=index,\n\t\t\tposition=position\n\t\t)\n\n\t\tsongs_len = len(songs)\n\t\tfor i, song in enumerate(songs):\n\t\t\tif 'storeId' in song:\n\t\t\t\tsong_id = song['storeId']\n\t\t\telif 'trackId' in song:\n\t\t\t\tsong_id = song['trackId']\n\t\t\telse:\n\t\t\t\tsong_id = song['id']\n\n\t\t\tmutation = mc_calls.PlaylistEntriesBatch.create(\n\t\t\t\tsong_id, playlist['id'],\n\t\t\t\tpreceding_entry_id=prev.get('id'),\n\t\t\t\tfollowing_entry_id=next_.get('id')\n\t\t\t)\n\t\t\tresponse = self._call(mc_calls.PlaylistEntriesBatch, mutation)\n\t\t\tresult = response.body['mutate_response'][0]\n\n\t\t\t# TODO: Proper exception on failure.\n\t\t\tif result['response_code'] != 'OK':\n\t\t\t\tbreak\n\n\t\t\tif i < songs_len - 1:\n\t\t\t\twhile True:\n\t\t\t\t\tprev = self.playlist_song(result['id'])\n\t\t\t\t\tif prev:\n\t\t\t\t\t\tbreak\n\n\t\treturn self.playlist(playlist['id'], include_songs=True)", "response": "Add songs to a playlist."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef playlist_song_delete(self, playlist_song):\n\n\t\tself.playlist_songs_delete([playlist_song])\n\n\t\treturn self.playlist(playlist_song['playlistId'], include_songs=True)", "response": "Delete a song from a playlist."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef playlist_songs_delete(self, playlist_songs):\n\n\t\tif not more_itertools.all_equal(\n\t\t\tplaylist_song['playlistId']\n\t\t\tfor playlist_song in playlist_songs\n\t\t):\n\t\t\traise ValueError(\n\t\t\t\t\"All 'playlist_songs' must be from the same playlist.\"\n\t\t\t)\n\n\t\tmutations = [mc_calls.PlaylistEntriesBatch.delete(playlist_song['id']) for playlist_song in playlist_songs]\n\t\tself._call(mc_calls.PlaylistEntriesBatch, mutations)\n\n\t\treturn self.playlist(playlist_songs[0]['playlistId'], include_songs=True)", "response": "Delete songs from playlist."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmove a song in a playlist.", "response": "def playlist_song_move(\n\t\tself,\n\t\tplaylist_song,\n\t\t*,\n\t\tafter=None,\n\t\tbefore=None,\n\t\tindex=None,\n\t\tposition=None\n\t):\n\t\t\"\"\"Move a song in a playlist.\n\n\t\tNote:\n\t\t\t* Provide no optional arguments to move to end.\n\t\t\t* Provide playlist song dicts for ``after`` and/or ``before``.\n\t\t\t* Provide a zero-based ``index``.\n\t\t\t* Provide a one-based ``position``.\n\n\t\t\tSongs are inserted *at* given index or position.\n\t\t\tIt's also possible to move to the end by using\n\t\t\t``len(songs)`` for index or ``len(songs) + 1`` for position.\n\n\t\tParameters:\n\t\t\tplaylist_song (dict): A playlist song dict.\n\t\t\tafter (dict, Optional): A playlist song dict ``songs`` will follow.\n\t\t\tbefore (dict, Optional): A playlist song dict ``songs`` will precede.\n\t\t\tindex (int, Optional): The zero-based index position to insert ``song``.\n\t\t\tposition (int, Optional): The one-based position to insert ``song``.\n\n\t\tReturns:\n\t\t\tdict: Playlist dict including songs.\n\t\t\"\"\"\n\n\t\tplaylist_songs = self.playlist(\n\t\t\tplaylist_song['playlistId'],\n\t\t\tinclude_songs=True\n\t\t)['tracks']\n\n\t\tprev, next_ = get_ple_prev_next(\n\t\t\tplaylist_songs,\n\t\t\tafter=after,\n\t\t\tbefore=before,\n\t\t\tindex=index,\n\t\t\tposition=position\n\t\t)\n\n\t\tmutation = mc_calls.PlaylistEntriesBatch.update(\n\t\t\tplaylist_song,\n\t\t\tpreceding_entry_id=prev.get('id'),\n\t\t\tfollowing_entry_id=next_.get('id')\n\t\t)\n\t\tself._call(mc_calls.PlaylistEntriesBatch, mutation)\n\n\t\treturn self.playlist(playlist_song['playlistId'], include_songs=True)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmoving songs in a playlist.", "response": "def playlist_songs_move(\n\t\tself,\n\t\tplaylist_songs,\n\t\t*,\n\t\tafter=None,\n\t\tbefore=None,\n\t\tindex=None,\n\t\tposition=None\n\t):\n\t\t\"\"\"Move songs in a playlist.\n\n\t\tNote:\n\t\t\t* Provide no optional arguments to move to end.\n\t\t\t* Provide playlist song dicts for ``after`` and/or ``before``.\n\t\t\t* Provide a zero-based ``index``.\n\t\t\t* Provide a one-based ``position``.\n\n\t\t\tSongs are inserted *at* given index or position.\n\t\t\tIt's also possible to move to the end by using\n\t\t\t``len(songs)`` for index or ``len(songs) + 1`` for position.\n\n\t\tParameters:\n\t\t\tplaylist_songs (list): A list of playlist song dicts.\n\t\t\tafter (dict, Optional): A playlist song dict ``songs`` will follow.\n\t\t\tbefore (dict, Optional): A playlist song dict ``songs`` will precede.\n\t\t\tindex (int, Optional): The zero-based index position to insert ``songs``.\n\t\t\tposition (int, Optional): The one-based position to insert ``songs``.\n\n\t\tReturns:\n\t\t\tdict: Playlist dict including songs.\n\t\t\"\"\"\n\n\t\tif not more_itertools.all_equal(\n\t\t\tplaylist_song['playlistId']\n\t\t\tfor playlist_song in playlist_songs\n\t\t):\n\t\t\traise ValueError(\n\t\t\t\t\"All 'playlist_songs' must be from the same playlist.\"\n\t\t\t)\n\n\t\tplaylist = self.playlist(\n\t\t\tplaylist_songs[0]['playlistId'],\n\t\t\tinclude_songs=True\n\t\t)\n\n\t\tprev, next_ = get_ple_prev_next(\n\t\t\tplaylist['tracks'],\n\t\t\tafter=after,\n\t\t\tbefore=before,\n\t\t\tindex=index,\n\t\t\tposition=position\n\t\t)\n\n\t\tplaylist_songs_len = len(playlist_songs)\n\t\tfor i, playlist_song in enumerate(playlist_songs):\n\t\t\tmutation = mc_calls.PlaylistEntriesBatch.update(\n\t\t\t\tplaylist_song,\n\t\t\t\tpreceding_entry_id=prev.get('id'),\n\t\t\t\tfollowing_entry_id=next_.get('id')\n\t\t\t)\n\t\t\tresponse = self._call(mc_calls.PlaylistEntriesBatch, mutation)\n\t\t\tresult = response.body['mutate_response'][0]\n\n\t\t\t# TODO: Proper exception on failure.\n\t\t\tif result['response_code'] != 'OK':\n\t\t\t\tbreak\n\n\t\t\tif i < playlist_songs_len - 1:\n\t\t\t\twhile True:\n\t\t\t\t\tprev = self.playlist_song(result['id'])\n\t\t\t\t\tif prev:\n\t\t\t\t\t\tbreak\n\n\t\treturn self.playlist(playlist_songs[0]['playlistId'], include_songs=True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a listing of songs from a playlist.", "response": "def playlist_songs(self, playlist):\n\t\t\"\"\"Get a listing of songs from a playlist.\n\n\t\tParamters:\n\t\t\tplaylist (dict): A playlist dict.\n\n\t\tReturns:\n\t\t\tlist: Playlist song dicts.\n\t\t\"\"\"\n\n\t\tplaylist_type = playlist.get('type')\n\n\t\tplaylist_song_list = []\n\t\tif playlist_type in ('USER_GENERATED', None):\n\t\t\tstart_token = None\n\t\t\tplaylist_song_list = []\n\t\t\twhile True:\n\t\t\t\tresponse = self._call(\n\t\t\t\t\tmc_calls.PlaylistEntryFeed,\n\t\t\t\t\tmax_results=49995,\n\t\t\t\t\tstart_token=start_token\n\t\t\t\t)\n\t\t\t\titems = response.body.get('data', {}).get('items', [])\n\n\t\t\t\tif items:\n\t\t\t\t\tplaylist_song_list.extend(items)\n\n\t\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\t\tif start_token is None:\n\t\t\t\t\tbreak\n\t\telif playlist_type == 'SHARED':\n\t\t\tplaylist_share_token = playlist['shareToken']\n\n\t\t\tstart_token = None\n\t\t\tplaylist_song_list = []\n\t\t\twhile True:\n\t\t\t\tresponse = self._call(\n\t\t\t\t\tmc_calls.PlaylistEntriesShared,\n\t\t\t\t\tplaylist_share_token,\n\t\t\t\t\tmax_results=49995,\n\t\t\t\t\tstart_token=start_token\n\t\t\t\t)\n\t\t\t\tentry = response.body['entries'][0]\n\t\t\t\titems = entry.get('playlistEntry', [])\n\n\t\t\t\tif items:\n\t\t\t\t\tplaylist_song_list.extend(items)\n\n\t\t\t\tstart_token = entry.get('nextPageToken')\n\t\t\t\tif start_token is None:\n\t\t\t\t\tbreak\n\n\t\tplaylist_song_list.sort(key=itemgetter('absolutePosition'))\n\n\t\treturn playlist_song_list"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef playlist(self, playlist_id, *, include_songs=False):\n\n\t\tplaylist_info = next(\n\t\t\t(\n\t\t\t\tplaylist\n\t\t\t\tfor playlist in self.playlists(include_songs=include_songs)\n\t\t\t\tif playlist['id'] == playlist_id\n\t\t\t),\n\t\t\tNone\n\t\t)\n\n\t\treturn playlist_info", "response": "Get information about a playlist."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a playlist. Parameters: name (str): Name to give the playlist. description (str): Description to give the playlist. make_public (bool, Optional): If ``True`` and account has a subscription, make playlist public. Default: ``False`` songs (list, Optional): A list of song dicts to add to the playlist. Returns: dict: Playlist information.", "response": "def playlist_create(\n\t\tself,\n\t\tname,\n\t\tdescription='',\n\t\t*,\n\t\tmake_public=False,\n\t\tsongs=None\n\t):\n\t\t\"\"\"Create a playlist.\n\n\t\tParameters:\n\t\t\tname (str): Name to give the playlist.\n\t\t\tdescription (str): Description to give the playlist.\n\t\t\tmake_public (bool, Optional): If ``True`` and account has a subscription,\n\t\t\t\tmake playlist public.\n\t\t\t\tDefault: ``False``\n\t\t\tsongs (list, Optional): A list of song dicts to add to the playlist.\n\n\t\tReturns:\n\t\t\tdict: Playlist information.\n\t\t\"\"\"\n\n\t\tshare_state = 'PUBLIC' if make_public else 'PRIVATE'\n\n\t\tplaylist = self._call(\n\t\t\tmc_calls.PlaylistsCreate,\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tshare_state\n\t\t).body\n\n\t\tif songs:\n\t\t\tplaylist = self.playlist_songs_add(songs, playlist)\n\n\t\treturn playlist"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef playlist_edit(self, playlist, *, name=None, description=None, public=None):\n\n\t\tif all(\n\t\t\tvalue is None\n\t\t\tfor value in (name, description, public)\n\t\t):\n\t\t\traise ValueError(\n\t\t\t\t'At least one of name, description, or public must be provided'\n\t\t\t)\n\n\t\tplaylist_id = playlist['id']\n\t\tplaylist = self.playlist(playlist_id)\n\n\t\tname = name if name is not None else playlist['name']\n\t\tdescription = (\n\t\t\tdescription if description is not None else playlist['description']\n\t\t)\n\t\tshare_state = 'PUBLIC' if public else playlist['accessControlled']\n\n\t\tplaylist = self._call(\n\t\t\tmc_calls.PlaylistsUpdate,\n\t\t\tplaylist_id,\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tshare_state\n\t\t).body\n\n\t\treturn playlist", "response": "Edit playlist(s).\n\n\t\tParameters:\n\t\t\tplaylist (dict): A playlist dict.\n\t\t\tname (str): Name to give the playlist.\n\t\t\tdescription (str, Optional): Description to give the playlist.\n\t\t\tmake_public (bool, Optional): If ``True`` and account has a subscription,\n\t\t\t\tmake playlist public.\n\t\t\t\tDefault: ``False``\n\n\t\tReturns:\n\t\t\tdict: Playlist information."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsubscribe to a public playlist.", "response": "def playlist_subscribe(self, playlist):\n\t\t\"\"\"Subscribe to a public playlist.\n\n\t\tParameters:\n\t\t\tplaylist (dict): A public playlist dict.\n\n\t\tReturns:\n\t\t\tdict: Playlist information.\n\t\t\"\"\"\n\n\t\tmutation = mc_calls.PlaylistBatch.create(\n\t\t\tplaylist['name'],\n\t\t\tplaylist['description'],\n\t\t\t'SHARED',\n\t\t\towner_name=playlist.get('ownerName', ''),\n\t\t\tshare_token=playlist['shareToken']\n\t\t)\n\n\t\tresponse_body = self._call(\n\t\t\tmc_calls.PlaylistBatch,\n\t\t\tmutation\n\t\t).body\n\n\t\tplaylist_id = response_body['mutate_response'][0]['id']\n\n\t\treturn self.playlist(playlist_id)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a listing of library playlists.", "response": "def playlists(self, *, include_songs=False):\n\t\t\"\"\"Get a listing of library playlists.\n\n\t\tParameters:\n\t\t\tinclude_songs (bool, Optional): Include songs in the returned playlist dicts.\n\t\t\t\tDefault: ``False``.\n\n\t\tReturns:\n\t\t\tlist: A list of playlist dicts.\n\t\t\"\"\"\n\n\t\tplaylist_list = []\n\t\tfor chunk in self.playlists_iter(page_size=49995):\n\t\t\tfor playlist in chunk:\n\t\t\t\tif include_songs:\n\t\t\t\t\tplaylist['tracks'] = self.playlist_songs(playlist)\n\n\t\t\t\tplaylist_list.append(playlist)\n\n\t\treturn playlist_list"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a paged iterator of library playlists.", "response": "def playlists_iter(self, *, start_token=None, page_size=250):\n\t\t\"\"\"Get a paged iterator of library playlists.\n\n\t\tParameters:\n\t\t\tstart_token (str): The token of the page to return.\n\t\t\t\tDefault: Not sent to get first page.\n\t\t\tpage_size (int, Optional): The maximum number of results per returned page.\n\t\t\t\tMax allowed is ``49995``.\n\t\t\t\tDefault: ``250``\n\n\t\tYields:\n\t\t\tlist: Playlist dicts.\n\t\t\"\"\"\n\n\t\tstart_token = None\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.PlaylistFeed,\n\t\t\t\tmax_results=page_size,\n\t\t\t\tstart_token=start_token\n\t\t\t)\n\t\t\titems = response.body.get('data', {}).get('items', [])\n\n\t\t\tif items:\n\t\t\t\tyield items\n\n\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\tif start_token is None:\n\t\t\t\tbreak"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget information about a podcast series.", "response": "def podcast(self, podcast_series_id, *, max_episodes=50):\n\t\t\"\"\"Get information about a podcast series.\n\n\t\tParameters:\n\t\t\tpodcast_series_id (str): A podcast series ID.\n\t\t\tmax_episodes (int, Optional): Include up to given number of episodes in returned dict.\n\t\t\t\tDefault: ``50``\n\n\t\tReturns:\n\t\t\tdict: Podcast series information.\n\t\t\"\"\"\n\n\t\tpodcast_info = self._call(\n\t\t\tmc_calls.PodcastFetchSeries,\n\t\t\tpodcast_series_id,\n\t\t\tmax_episodes=max_episodes\n\t\t).body\n\n\t\treturn podcast_info"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef podcasts(self, *, device_id=None):\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tpodcast_list = []\n\t\tfor chunk in self.podcasts_iter(device_id=device_id, page_size=49995):\n\t\t\tpodcast_list.extend(chunk)\n\n\t\treturn podcast_list", "response": "Get a listing of subsribed podcast series."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef podcasts_iter(self, *, device_id=None, page_size=250):\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tstart_token = None\n\t\tprev_items = None\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.PodcastSeries,\n\t\t\t\tdevice_id,\n\t\t\t\tmax_results=page_size,\n\t\t\t\tstart_token=start_token\n\t\t\t)\n\t\t\titems = response.body.get('data', {}).get('items', [])\n\n\t\t\t# Google does some weird shit.\n\t\t\tif items != prev_items:\n\t\t\t\tsubscribed_podcasts = [\n\t\t\t\t\titem\n\t\t\t\t\tfor item in items\n\t\t\t\t\tif item.get('userPreferences', {}).get('subscribed')\n\t\t\t\t]\n\n\t\t\t\tyield subscribed_podcasts\n\n\t\t\t\tprev_items = items\n\t\t\telse:\n\t\t\t\tbreak\n\n\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\tif start_token is None:\n\t\t\t\tbreak", "response": "Get a paged iterator of subscribed podcast series."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef podcast_episode(self, podcast_episode_id):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.PodcastFetchEpisode,\n\t\t\tpodcast_episode_id\n\t\t)\n\t\tpodcast_episode_info = [\n\t\t\tpodcast_episode\n\t\t\tfor podcast_episode in response.body\n\t\t\tif not podcast_episode['deleted']\n\t\t]\n\n\t\treturn podcast_episode_info", "response": "Get information about a podcast episode."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef podcast_episodes(self, *, device_id=None):\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tpodcast_episode_list = []\n\t\tfor chunk in self.podcast_episodes_iter(\n\t\t\tdevice_id=device_id,\n\t\t\tpage_size=49995\n\t\t):\n\t\t\tpodcast_episode_list.extend(chunk)\n\n\t\treturn podcast_episode_list", "response": "Get a listing of podcast episodes for all subscribed podcasts."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef podcast_episodes_iter(self, *, device_id=None, page_size=250):\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tstart_token = None\n\t\tprev_items = None\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.PodcastEpisode,\n\t\t\t\tdevice_id,\n\t\t\t\tmax_results=page_size,\n\t\t\t\tstart_token=start_token\n\t\t\t)\n\t\t\titems = response.body.get('data', {}).get('items', [])\n\n\t\t\t# Google does some weird shit.\n\t\t\tif items != prev_items:\n\t\t\t\tyield items\n\n\t\t\t\tprev_items = items\n\t\t\telse:\n\t\t\t\tbreak\n\n\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\tif start_token is None:\n\t\t\t\tbreak", "response": "Get a paged iterator of podcast episode for all subscribed podcasts."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsearches Google Music and library for content.", "response": "def search(self, query, *, max_results=100, **kwargs):\n\t\t\"\"\"Search Google Music and library for content.\n\n\t\tParameters:\n\t\t\tquery (str): Search text.\n\t\t\tmax_results (int, Optional): Maximum number of results per type per\n\t\t\t\tlocation to retrieve. I.e up to 100 Google and 100 library\n\t\t\t\tfor a total of 200 for the default value.\n\t\t\t\tGoogle only accepts values up to 100.\n\t\t\t\tDefault: ``100``\n\t\t\tkwargs (bool, Optional): Any of ``albums``, ``artists``, ``genres``,\n\t\t\t\t``playlists``, ``podcasts``, ``situations``, ``songs``, ``stations``,\n\t\t\t\t``videos`` set to ``True`` will include that result type in the\n\t\t\t\treturned dict.\n\t\t\t\tSetting none of them will include all result types in the returned dict.\n\n\t\tReturns:\n\t\t\tdict: A dict of results separated into keys: ``'albums'``, ``'artists'``,\n\t\t\t\t``'genres'``, ``'playlists'``, ```'podcasts'``, ``'situations'``,\n\t\t\t\t``'songs'``, ``'stations'``, ``'videos'``.\n\n\t\tNote:\n\t\t\tFree account search is restricted so may not contain hits for all result types.\n\t\t\"\"\"\n\n\t\tresults = defaultdict(list)\n\n\t\tfor type_, results_ in self.search_library(\n\t\t\tquery,\n\t\t\tmax_results=max_results,\n\t\t\t**kwargs\n\t\t).items():\n\t\t\tresults[type_].extend(results_)\n\n\t\tfor type_, results_ in self.search_google(\n\t\t\tquery,\n\t\t\tmax_results=max_results,\n\t\t\t**kwargs\n\t\t).items():\n\t\t\tresults[type_].extend(results_)\n\n\t\treturn dict(results)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef search_google(self, query, *, max_results=100, **kwargs):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.Query,\n\t\t\tquery,\n\t\t\tmax_results=max_results,\n\t\t\t**kwargs\n\t\t)\n\n\t\tclusters = response.body.get('clusterDetail', [])\n\t\tresults = defaultdict(list)\n\n\t\tfor cluster in clusters:\n\t\t\tresult_type = QueryResultType(cluster['cluster']['type']).name\n\n\t\t\tentries = cluster.get('entries', [])\n\t\t\tif len(entries) > 0:\n\t\t\t\tfor entry in entries:\n\t\t\t\t\titem_key = next(\n\t\t\t\t\t\tkey\n\t\t\t\t\t\tfor key in entry\n\t\t\t\t\t\tif key not in ['cluster', 'score', 'type']\n\t\t\t\t\t)\n\t\t\t\t\tresults[f\"{result_type}s\"].append(entry[item_key])\n\n\t\treturn dict(results)", "response": "Search Google Music for content."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef search_library(self, query, *, max_results=100, **kwargs):\n\n\t\tdef match_fields(item, fields):\n\t\t\treturn any(\n\t\t\t\tquery.casefold() in item.get(field, '').casefold()\n\t\t\t\tfor field in fields\n\t\t\t)\n\n\t\ttypes = [\n\t\t\t(\n\t\t\t\t'playlists',\n\t\t\t\t['description', 'name'],\n\t\t\t\tself.playlists\n\t\t\t),\n\t\t\t(\n\t\t\t\t'podcasts',\n\t\t\t\t['author', 'description', 'title'],\n\t\t\t\tself.podcasts\n\t\t\t),\n\t\t\t(\n\t\t\t\t'songs',\n\t\t\t\t['album', 'albumArtist', 'artist', 'composer', 'genre', 'title'],\n\t\t\t\tself.songs\n\t\t\t),\n\t\t\t(\n\t\t\t\t'stations',\n\t\t\t\t['byline', 'description', 'name'],\n\t\t\t\tself.stations\n\t\t\t),\n\t\t]\n\n\t\tresults = {}\n\n\t\tfor type_, fields, func in types:\n\t\t\tif (not kwargs) or (type_ in kwargs):\n\t\t\t\tresults[type_] = [\n\t\t\t\t\titem\n\t\t\t\t\tfor item in func()\n\t\t\t\t\tif match_fields(item, fields)\n\t\t\t\t][:max_results]\n\n\t\treturn results", "response": "Search Google Music for content."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef search_suggestion(self, query):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.QuerySuggestion,\n\t\t\tquery\n\t\t)\n\t\tsuggested_queries = response.body.get('suggested_queries', [])\n\n\t\treturn [\n\t\t\tsuggested_query['suggestion_string']\n\t\t\tfor suggested_query in suggested_queries\n\t\t]", "response": "Get search query suggestions for query."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget a listing of album songs.", "response": "def shuffle_album(\n\t\tself, album, *, num_songs=100, only_library=False, recently_played=None\n\t):\n\t\t\"\"\"Get a listing of album shuffle/mix songs.\n\n\t\tParameters:\n\t\t\talbum (dict): An album dict.\n\t\t\tnum_songs (int, Optional): The maximum number of songs to return from the station.\n\t\t\t\tDefault: ``100``\n\t\t\tonly_library (bool, Optional): Only return content from library.\n\t\t\t\tDefault: False\n\t\t\trecently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'}\n\t\t\t\twhere ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song.\n\n\t\tReturns:\n\t\t\tlist: List of album shuffle/mix songs.\n\t\t\"\"\"\n\n\t\tstation_info = {\n\t\t\t'seed': {\n\t\t\t\t'albumId': album['albumId'],\n\t\t\t\t'seedType': StationSeedType.album.value\n\t\t\t},\n\t\t\t'num_entries': num_songs,\n\t\t\t'library_content_only': only_library,\n\t\t}\n\n\t\tif recently_played is not None:\n\t\t\tstation_info['recently_played'] = recently_played\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.RadioStationFeed,\n\t\t\tstation_infos=[station_info]\n\t\t)\n\t\tstation_feed = response.body.get('data', {}).get('stations', [])\n\n\t\ttry:\n\t\t\tstation = station_feed[0]\n\t\texcept IndexError:\n\t\t\tstation = {}\n\n\t\treturn station.get('tracks', [])"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a listing of artist shuffle/mix songs. Parameters: artist (dict): An artist dict. num_songs (int, Optional): The maximum number of songs to return from the station. Default: ``100`` only_library (bool, Optional): Only return content from library. Default: False recently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'} where ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song. only_artist (bool, Optional): If ``True``, only return songs from the artist, else return songs from artist and related artists. Default: ``False`` Returns: list: List of artist shuffle/mix songs.", "response": "def shuffle_artist(\n\t\tself,\n\t\tartist,\n\t\t*,\n\t\tnum_songs=100,\n\t\tonly_library=False,\n\t\trecently_played=None,\n\t\tonly_artist=False\n\t):\n\t\t\"\"\"Get a listing of artist shuffle/mix songs.\n\n\t\tParameters:\n\t\t\tartist (dict): An artist dict.\n\t\t\tnum_songs (int, Optional): The maximum number of songs to return from the station.\n\t\t\t\tDefault: ``100``\n\t\t\tonly_library (bool, Optional): Only return content from library.\n\t\t\t\tDefault: False\n\t\t\trecently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'}\n\t\t\t\twhere ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song.\n\t\t\tonly_artist (bool, Optional): If ``True``, only return songs from the artist,\n\t\t\t\t\telse return songs from artist and related artists.\n\t\t\t\t\tDefault: ``False``\n\n\t\tReturns:\n\t\t\tlist: List of artist shuffle/mix songs.\n\t\t\"\"\"\n\n\t\tstation_info = {\n\t\t\t'num_entries': num_songs,\n\t\t\t'library_content_only': only_library\n\t\t}\n\n\t\tif only_artist:\n\t\t\tstation_info['seed'] = {\n\t\t\t\t'artistId': artist['artistId'],\n\t\t\t\t'seedType': StationSeedType.artist_only.value\n\t\t\t}\n\t\telse:\n\t\t\tstation_info['seed'] = {\n\t\t\t\t'artistId': artist['artistId'],\n\t\t\t\t'seedType': StationSeedType.artist_related.value\n\t\t\t}\n\n\t\tif recently_played is not None:\n\t\t\tstation_info['recently_played'] = recently_played\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.RadioStationFeed,\n\t\t\tstation_infos=[station_info]\n\t\t)\n\t\tstation_feed = response.body.get('data', {}).get('stations', [])\n\n\t\ttry:\n\t\t\tstation = station_feed[0]\n\t\texcept IndexError:\n\t\t\tstation = {}\n\n\t\treturn station.get('tracks', [])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef shuffle_song(\n\t\tself, song, *, num_songs=100, only_library=False, recently_played=None\n\t):\n\t\t\"\"\"Get a listing of song shuffle/mix songs.\n\n\t\tParameters:\n\t\t\tsong (dict): A song dict.\n\t\t\tnum_songs (int, Optional): The maximum number of songs to return from the station.\n\t\t\t\tDefault: ``100``\n\t\t\tonly_library (bool, Optional): Only return content from library.\n\t\t\t\tDefault: False\n\t\t\trecently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'}\n\t\t\t\twhere ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song.\n\n\t\tReturns:\n\t\t\tlist: List of artist shuffle/mix songs.\n\t\t\"\"\"\n\n\t\tstation_info = {\n\t\t\t'num_entries': num_songs,\n\t\t\t'library_content_only': only_library\n\t\t}\n\n\t\tif 'storeId' in song:\n\t\t\tstation_info['seed'] = {\n\t\t\t\t'trackId': song['storeId'],\n\t\t\t\t'seedType': StationSeedType.store_track.value\n\t\t\t}\n\t\telse:\n\t\t\tstation_info['seed'] = {\n\t\t\t\t'trackLockerId': song['id'],\n\t\t\t\t'seedType': StationSeedType.library_track.value\n\t\t\t}\n\n\t\tif recently_played is not None:\n\t\t\tstation_info['recently_played'] = recently_played\n\n\t\tresponse = self._call(mc_calls.RadioStationFeed, station_infos=[station_info])\n\t\tstation_feed = response.body.get('data', {}).get('stations', [])\n\n\t\ttry:\n\t\t\tstation = station_feed[0]\n\t\texcept IndexError:\n\t\t\tstation = {}\n\n\t\treturn station.get('tracks', [])", "response": "Get a listing of a song shuffle or mix songs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef situations(self, *, tz_offset=None):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.ListenNowSituations,\n\t\t\ttz_offset\n\t\t)\n\t\tsituation_list = response.body.get('situations', [])\n\n\t\treturn situation_list", "response": "Get a listing of situations."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets information about a song.", "response": "def song(self, song_id):\n\t\t\"\"\"Get information about a song.\n\n\t\tParameters:\n\t\t\tsong_id (str): A song ID.\n\n\t\tReturns:\n\t\t\tdict: Song information.\n\t\t\"\"\"\n\n\t\tif song_id.startswith('T'):\n\t\t\tsong_info = self._call(\n\t\t\t\tmc_calls.FetchTrack,\n\t\t\t\tsong_id\n\t\t\t).body\n\t\telse:\n\t\t\tsong_info = next(\n\t\t\t\t(\n\t\t\t\t\tsong\n\t\t\t\t\tfor song in self.songs()\n\t\t\t\t\tif song['id'] == song_id\n\t\t\t\t),\n\t\t\t\tNone\n\t\t\t)\n\n\t\treturn song_info"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding store songs to your library.", "response": "def songs_add(self, songs):\n\t\t\"\"\"Add store songs to your library.\n\n\t\tParameters:\n\t\t\tsongs (list): A list of store song dicts.\n\n\t\tReturns:\n\t\t\tlist: Songs' library IDs.\n\t\t\"\"\"\n\n\t\tmutations = [mc_calls.TrackBatch.add(song) for song in songs]\n\t\tresponse = self._call(\n\t\t\tmc_calls.TrackBatch,\n\t\t\tmutations\n\t\t)\n\n\t\tsuccess_ids = [\n\t\t\tres['id']\n\t\t\tfor res in response.body['mutate_response']\n\t\t\tif res['response_code'] == 'OK'\n\t\t]\n\n\t\treturn success_ids"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef songs_delete(self, songs):\n\n\t\tmutations = [mc_calls.TrackBatch.delete(song['id']) for song in songs]\n\t\tresponse = self._call(\n\t\t\tmc_calls.TrackBatch,\n\t\t\tmutations\n\t\t)\n\n\t\tsuccess_ids = [\n\t\t\tres['id']\n\t\t\tfor res in response.body['mutate_response']\n\t\t\tif res['response_code'] == 'OK'\n\t\t]\n\n\t\t# TODO: Report failures.\n\t\t# failure_ids = [\n\t\t# \tres['id']\n\t\t# \tfor res in response.body['mutate_response']\n\t\t# \tif res['response_code'] != 'OK'\n\t\t# ]\n\n\t\treturn success_ids", "response": "Delete songs from library."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds play to song play count.", "response": "def song_play(self, song):\n\t\t\"\"\"Add play to song play count.\n\n\t\tParameters:\n\t\t\tsong (dict): A song dict.\n\n\t\tReturns:\n\t\t\tbool: ``True`` if successful, ``False`` if not.\n\t\t\"\"\"\n\n\t\tif 'storeId' in song:\n\t\t\tsong_id = song['storeId']\n\t\telif 'trackId' in song:\n\t\t\tsong_id = song['trackId']\n\t\telse:\n\t\t\tsong_id = song['id']\n\n\t\tsong_duration = song['durationMillis']\n\n\t\tevent = mc_calls.ActivityRecordRealtime.play(song_id, song_duration)\n\t\tresponse = self._call(\n\t\t\tmc_calls.ActivityRecordRealtime,\n\t\t\tevent\n\t\t)\n\n\t\treturn True if response.body['eventResults'][0]['code'] == 'OK' else False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrates song. Parameters: song (dict): A song dict. rating (int): 0 (not rated), 1 (thumbs down), or 5 (thumbs up). Returns: bool: ``True`` if successful, ``False`` if not.", "response": "def song_rate(self, song, rating):\n\t\t\"\"\"Rate song.\n\n\t\tParameters:\n\t\t\tsong (dict): A song dict.\n\t\t\trating (int): 0 (not rated), 1 (thumbs down), or 5 (thumbs up).\n\n\t\tReturns:\n\t\t\tbool: ``True`` if successful, ``False`` if not.\n\t\t\"\"\"\n\n\t\tif 'storeId' in song:\n\t\t\tsong_id = song['storeId']\n\t\telif 'trackId' in song:\n\t\t\tsong_id = song['trackId']\n\t\telse:\n\t\t\tsong_id = song['id']\n\n\t\tevent = mc_calls.ActivityRecordRealtime.rate(song_id, rating)\n\t\tresponse = self._call(\n\t\t\tmc_calls.ActivityRecordRealtime,\n\t\t\tevent\n\t\t)\n\n\t\treturn True if response.body['eventResults'][0]['code'] == 'OK' else False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef songs(self):\n\n\t\tsong_list = []\n\t\tfor chunk in self.songs_iter(page_size=49995):\n\t\t\tsong_list.extend(chunk)\n\n\t\treturn song_list", "response": "Get a listing of library songs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef songs_iter(self, *, page_size=250):\n\n\t\tstart_token = None\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.TrackFeed,\n\t\t\t\tmax_results=page_size,\n\t\t\t\tstart_token=start_token\n\t\t\t)\n\t\t\titems = response.body.get('data', {}).get('items', [])\n\n\t\t\tif items:\n\t\t\t\tyield items\n\n\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\tif start_token is None:\n\t\t\t\tbreak", "response": "Get a paged iterator of library songs."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets information about a station.", "response": "def station(self, station_id, *, num_songs=25, recently_played=None):\n\t\t\"\"\"Get information about a station.\n\n\t\tParameters:\n\t\t\tstation_id (str): A station ID. Use 'IFL' for I'm Feeling Lucky.\n\t\t\tnum_songs (int, Optional): The maximum number of songs to return from the station.\n\t\t\t\tDefault: ``25``\n\t\t\trecently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'}\n\t\t\t\twhere ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song.\n\n\t\tReturns:\n\t\t\tdict: Station information.\n\t\t\"\"\"\n\n\t\tstation_info = {\n\t\t\t'station_id': station_id,\n\t\t\t'num_entries': num_songs,\n\t\t\t'library_content_only': False\n\t\t}\n\n\t\tif recently_played is not None:\n\t\t\tstation_info['recently_played'] = recently_played\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.RadioStationFeed,\n\t\t\tstation_infos=[station_info]\n\t\t)\n\t\tstation_feed = response.body.get('data', {}).get('stations', [])\n\n\t\ttry:\n\t\t\tstation = station_feed[0]\n\t\texcept IndexError:\n\t\t\tstation = {}\n\n\t\treturn station"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef station_feed(self, *, num_songs=25, num_stations=4):\n\n\t\tresponse = self._call(\n\t\t\tmc_calls.RadioStationFeed,\n\t\t\tnum_entries=num_songs,\n\t\t\tnum_stations=num_stations\n\t\t)\n\t\tstation_feed = response.body.get('data', {}).get('stations', [])\n\n\t\treturn station_feed", "response": "Generate stations.\n\n\t\tNote:\n\t\t\tA Google Music subscription is required.\n\n\t\tParameters:\n\t\t\tnum_songs (int, Optional): The total number of songs to return. Default: ``25``\n\t\t\tnum_stations (int, Optional): The number of stations to return when no station_infos is provided.\n\t\t\t\tDefault: ``5``\n\n\t\tReturns:\n\t\t\tlist: Station information dicts."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a listing of songs from a station.", "response": "def station_songs(self, station, *, num_songs=25, recently_played=None):\n\t\t\"\"\"Get a listing of songs from a station.\n\n\t\tParameters:\n\t\t\tstation (str): A station dict.\n\t\t\tnum_songs (int, Optional): The maximum number of songs to return from the station. Default: ``25``\n\t\t\trecently_played (list, Optional): A list of dicts in the form of {'id': '', 'type'}\n\t\t\t\twhere ``id`` is a song ID and ``type`` is 0 for a library song and 1 for a store song.\n\n\t\tReturns:\n\t\t\tlist: Station song dicts.\n\t\t\"\"\"\n\n\t\tstation_id = station['id']\n\n\t\tstation = self.station(\n\t\t\tstation_id,\n\t\t\tnum_songs=num_songs,\n\t\t\trecently_played=recently_played\n\t\t)\n\n\t\treturn station.get('tracks', [])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef stations(self, *, generated=True, library=True):\n\n\t\tstation_list = []\n\t\tfor chunk in self.stations_iter(page_size=49995):\n\t\t\tfor station in chunk:\n\t\t\t\tif (\n\t\t\t\t\t(generated and not station.get('inLibrary'))\n\t\t\t\t\tor (library and station.get('inLibrary'))\n\t\t\t\t):\n\t\t\t\t\tstation_list.append(station)\n\n\t\treturn station_list", "response": "Get a listing of library stations."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stations_iter(self, *, page_size=250):\n\n\t\tstart_token = None\n\n\t\twhile True:\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.RadioStation,\n\t\t\t\tmax_results=page_size,\n\t\t\t\tstart_token=start_token\n\t\t\t)\n\t\t\tyield response.body.get('data', {}).get('items', [])\n\n\t\t\tstart_token = response.body.get('nextPageToken')\n\t\t\tif start_token is None:\n\t\t\t\tbreak", "response": "Get a paged iterator of library stations."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget MP3 stream of a podcast episode library song station_song or store song.", "response": "def stream(self, item, *, device_id=None, quality='hi', session_token=None):\n\t\t\"\"\"Get MP3 stream of a podcast episode, library song, station_song, or store song.\n\n\t\tNote:\n\t\t\tStreaming requires a ``device_id`` from a valid, linked mobile device.\n\n\t\tParameters:\n\t\t\titem (str): A podcast episode, library song, station_song, or store song.\n\t\t\t\tA Google Music subscription is required to stream store songs.\n\t\t\tdevice_id (str, Optional): A mobile device ID.\n\t\t\t\tDefault: Use ``device_id`` of the :class:`MobileClient` instance.\n\t\t\tquality (str, Optional): Stream quality is one of ``'hi'`` (320Kbps), ``'med'`` (160Kbps), or ``'low'`` (128Kbps).\n\t\t\t\tDefault: ``'hi'``.\n\t\t\tsession_token (str): Session token from a station dict required for unsubscribed users to stream a station song.\n\t\t\t\tstation['sessionToken'] as returend by :meth:`station` only exists for free accounts.\n\n\t\tReturns:\n\t\t\tbytes: An MP3 file.\n\t\t\"\"\"\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tstream_url = self.stream_url(\n\t\t\titem,\n\t\t\tdevice_id=device_id,\n\t\t\tquality=quality,\n\t\t\tsession_token=session_token\n\t\t)\n\t\tresponse = self.session.get(stream_url)\n\t\taudio = response.content\n\n\t\treturn audio"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget a URL to stream a podcast episode library song station_song or store song.", "response": "def stream_url(self, item, *, device_id=None, quality='hi', session_token=None):\n\t\t\"\"\"Get a URL to stream a podcast episode, library song, station_song, or store song.\n\n\t\tNote:\n\t\t\tStreaming requires a ``device_id`` from a valid, linked mobile device.\n\n\t\tParameters:\n\t\t\titem (str): A podcast episode, library song, station_song, or store song.\n\t\t\t\tA Google Music subscription is required to stream store songs.\n\t\t\tdevice_id (str, Optional): A mobile device ID.\n\t\t\t\tDefault: Use ``device_id`` of the :class:`MobileClient` instance.\n\t\t\tquality (str, Optional): Stream quality is one of ``'hi'`` (320Kbps), ``'med'`` (160Kbps), or ``'low'`` (128Kbps).\n\t\t\t\tDefault: ``'hi'``.\n\t\t\tsession_token (str): Session token from a station dict required for unsubscribed users to stream a station song.\n\t\t\t\tstation['sessionToken'] as returend by :meth:`station` only exists for free accounts.\n\n\t\tReturns:\n\t\t\tstr: A URL to an MP3 file.\n\t\t\"\"\"\n\n\t\tif device_id is None:\n\t\t\tdevice_id = self.device_id\n\n\t\tif 'episodeId' in item: # Podcast episode.\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.PodcastEpisodeStreamURL,\n\t\t\t\titem['episodeId'],\n\t\t\t\tquality=quality,\n\t\t\t\tdevice_id=device_id\n\t\t\t)\n\t\telif 'wentryid' in item: # Free account station song.\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.RadioStationTrackStreamURL,\n\t\t\t\titem['storeId'],\n\t\t\t\titem['wentryid'],\n\t\t\t\tsession_token,\n\t\t\t\tquality=quality,\n\t\t\t\tdevice_id=device_id\n\t\t\t)\n\t\telif 'trackId' in item: # Playlist song.\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.TrackStreamURL,\n\t\t\t\titem['trackId'],\n\t\t\t\tquality=quality,\n\t\t\t\tdevice_id=device_id\n\t\t\t)\n\t\telif 'storeId' in item and self.is_subscribed: # Store song.\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.TrackStreamURL,\n\t\t\t\titem['storeId'],\n\t\t\t\tquality=quality,\n\t\t\t\tdevice_id=device_id\n\t\t\t)\n\t\telif 'id' in item: # Library song.\n\t\t\tresponse = self._call(\n\t\t\t\tmc_calls.TrackStreamURL,\n\t\t\t\titem['id'],\n\t\t\t\tquality=quality,\n\t\t\t\tdevice_id=device_id\n\t\t\t)\n\t\telse:\n\t\t\t# TODO: Create an exception for not being subscribed or use a better builtin exception for this case.\n\t\t\tif 'storeId' in item and not self.is_subscribed:\n\t\t\t\tmsg = \"Can't stream a store song without a subscription.\"\n\t\t\telse:\n\t\t\t\tmsg = \"Item does not contain an ID field.\"\n\n\t\t\traise ValueError(msg)\n\n\t\ttry:\n\t\t\tstream_url = response.headers['Location']\n\t\texcept KeyError:\n\t\t\tstream_url = response.body['url']\n\n\t\treturn stream_url"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a listing of Thumbs Up songs.", "response": "def thumbs_up_songs(self, *, library=True, store=True):\n\t\t\"\"\"Get a listing of 'Thumbs Up' store songs.\n\n\t\tParameters:\n\t\t\tlibrary (bool, Optional): Include 'Thumbs Up' songs from library.\n\t\t\t\tDefault: True\n\t\t\tgenerated (bool, Optional): Include 'Thumbs Up' songs from store.\n\t\t\t\tDefault: True\n\n\t\tReturns:\n\t\t\tlist: Dicts of 'Thumbs Up' songs.\n\t\t\"\"\"\n\n\t\tthumbs_up_songs = []\n\n\t\tif library is True:\n\t\t\tthumbs_up_songs.extend(\n\t\t\t\tsong\n\t\t\t\tfor song in self.songs()\n\t\t\t\tif song.get('rating', '0') == '5'\n\t\t\t)\n\n\t\tif store is True:\n\t\t\tresponse = self._call(mc_calls.EphemeralTop)\n\t\t\tthumbs_up_songs.extend(response.body.get('data', {}).get('items', []))\n\n\t\treturn thumbs_up_songs"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef top_charts(self):\n\n\t\tresponse = self._call(mc_calls.BrowseTopChart)\n\t\ttop_charts = response.body\n\n\t\treturn top_charts", "response": "Get a listing of the default top charts."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef top_charts_for_genre(self, genre_id):\n\n\t\tresponse = self._call(mc_calls.BrowseTopChartForGenre, genre_id)\n\t\ttop_chart_for_genre = response.body\n\n\t\treturn top_chart_for_genre", "response": "Get a listing of top charts for a top chart genre."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef top_charts_genres(self):\n\n\t\tresponse = self._call(mc_calls.BrowseTopChartGenres)\n\t\ttop_chart_genres = response.body.get('genres', [])\n\n\t\treturn top_chart_genres", "response": "Get a listing of genres from the browse top charts tab."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread from stream applying the LT decoding algorithm to incoming encoded blocks until sufficiently many blocks have been received.", "response": "def run(stream=sys.stdin.buffer):\n \"\"\"Reads from stream, applying the LT decoding algorithm\n to incoming encoded blocks until sufficiently many blocks\n have been received to reconstruct the entire file.\n \"\"\"\n payload = decode.decode(stream)\n sys.stdout.write(payload.decode('utf8'))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _split_file(f, blocksize):\n\n f_bytes = f.read()\n blocks = [int.from_bytes(f_bytes[i:i+blocksize].ljust(blocksize, b'0'), sys.byteorder) \n for i in range(0, len(f_bytes), blocksize)]\n return len(f_bytes), blocks", "response": "Split file into blocksize chunks"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ngenerate an infinite sequence of blocks to transmit to the receiver", "response": "def encoder(f, blocksize, seed=None, c=sampler.DEFAULT_C, delta=sampler.DEFAULT_DELTA):\n \"\"\"Generates an infinite sequence of blocks to transmit\n to the receiver\n \"\"\"\n\n # Generate seed if not provided\n if seed is None:\n seed = randint(0, 1 << 31 - 1)\n\n # get file blocks\n filesize, blocks = _split_file(f, blocksize)\n\n # init stream vars\n K = len(blocks)\n prng = sampler.PRNG(params=(K, delta, c))\n prng.set_seed(seed)\n\n # block generation loop\n while True:\n blockseed, d, ix_samples = prng.get_src_blocks()\n block_data = 0\n for ix in ix_samples:\n block_data ^= blocks[ix]\n\n # Generate blocks of XORed data in network byte order\n block = (filesize, blocksize, blockseed, int.to_bytes(block_data, blocksize, sys.byteorder))\n yield pack('!III%ss'%blocksize, *block)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreading block data from network into integer type", "response": "def _read_block(blocksize, stream):\n \"\"\"Read block data from network into integer type\n \"\"\"\n blockdata = stream.read(blocksize)\n return int.from_bytes(blockdata, 'big')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef read_blocks(stream):\n while True:\n header = _read_header(stream)\n block = _read_block(header[1], stream)\n yield (header, block)", "response": "Generate parsed blocks from input stream"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a new check node and edges between that node and all the source nodes it connects resolving all messages passes as a result.", "response": "def add_block(self, nodes, data):\n \"\"\"Adds a new check node and edges between that node and all\n source nodes it connects, resolving all message passes that\n become possible as a result.\n \"\"\"\n\n # We can eliminate this source node\n if len(nodes) == 1:\n to_eliminate = list(self.eliminate(next(iter(nodes)), data))\n\n # Recursively eliminate all nodes that can now be resolved\n while len(to_eliminate):\n other, check = to_eliminate.pop()\n to_eliminate.extend(self.eliminate(other, check))\n else:\n\n # Pass messages from already-resolved source nodes\n for node in list(nodes):\n if node in self.eliminated:\n nodes.remove(node)\n data ^= self.eliminated[node]\n\n # Resolve if we are left with a single non-resolved source node\n if len(nodes) == 1:\n return self.add_block(nodes, data)\n else:\n\n # Add edges for all remaining nodes to this check\n check = CheckNode(nodes, data)\n for node in nodes:\n self.checks[node].append(check)\n\n # Are we done yet?\n return len(self.eliminated) >= self.num_blocks"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef eliminate(self, node, data):\n\n # Cache resolved value\n self.eliminated[node] = data\n others = self.checks[node]\n del self.checks[node]\n\n # Pass messages to all associated checks\n for check in others:\n check.check ^= data\n check.src_nodes.remove(node)\n\n # Yield all nodes that can now be resolved\n if len(check.src_nodes) == 1:\n yield (next(iter(check.src_nodes)), check.check)", "response": "Resolves a source node passing the message to all associated checks\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate and authenticate a Google Music mobile client.", "response": "def mobileclient(username=None, device_id=None, *, token=None, locale='en_US'):\n\t\"\"\"Create and authenticate a Google Music mobile client.\n\n\t>>> import google_music\n\t>>> mc = google_music.mobileclient('username')\n\n\tParameters:\n\t\tusername (str, Optional): Your Google Music username.\n\t\t\tThis is used to store OAuth credentials for different accounts separately.\n\t\tdevice_id (str, Optional): A mobile device ID. Default: MAC address is used.\n\t\ttoken (dict, Optional): An OAuth token compatible with ``requests-oauthlib``.\n\t\tlocale (str, Optional): `ICU `__ locale used to localize some\n\t\t\tresponses. This must be a locale supported by Android. Default: `'en_US'``.\n\n\tReturns:\n\t\tMobileClient: An authenticated :class:`~google_music.MobileClient` instance.\n\t\"\"\"\n\n\treturn MobileClient(\n\t\tusername,\n\t\tdevice_id,\n\t\ttoken=token,\n\t\tlocale=locale\n\t)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _dendropy_to_dataframe(\n tree,\n add_node_labels=True,\n use_uids=True):\n \"\"\"Convert Dendropy tree to Pandas dataframe.\"\"\"\n # Maximum distance from root.\n tree.max_distance_from_root()\n\n # Initialize the data object.\n idx = []\n data = {\n 'type': [],\n 'id': [],\n 'parent': [],\n 'length': [],\n 'label': [],\n 'distance': []}\n\n if use_uids:\n data['uid'] = []\n\n # Add labels to internal nodes if set to true.\n if add_node_labels:\n for i, node in enumerate(tree.internal_nodes()):\n node.label = str(i)\n\n for node in tree.nodes():\n # Get node type\n if node.is_leaf():\n type_ = 'leaf'\n label = str(node.taxon.label).replace(' ', '_')\n elif node.is_internal():\n type_ = 'node'\n label = str(node.label)\n\n # Set node label and parent.\n id_ = label\n parent_node = node.parent_node\n length = node.edge_length\n distance = node.distance_from_root()\n\n # Is this node a root?\n if parent_node is None and length is None:\n parent_label = None\n parent_node = None\n length = 0\n distance = 0\n type_ = 'root'\n\n # Set parent node label\n elif parent_node.is_internal():\n parent_label = str(parent_node.label)\n\n else:\n raise Exception(\"Subtree is not attached to tree?\")\n\n # Add this node to the data.\n data['type'].append(type_)\n data['id'].append(id_)\n data['parent'].append(parent_label)\n data['length'].append(length)\n data['label'].append(label)\n data['distance'].append(distance)\n\n if use_uids:\n data['uid'].append(get_random_id(10))\n\n # Construct dataframe.\n df = pandas.DataFrame(data)\n return df", "response": "Convert Dendropy tree to Pandas dataframe."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _read(\n filename=None,\n data=None,\n schema=None,\n add_node_labels=True,\n use_uids=True\n ):\n \"\"\"Read a phylogenetic tree into a phylopandas.DataFrame.\n\n The resulting DataFrame has the following columns:\n - name: label for each taxa or node.\n - id: unique id (created by phylopandas) given to each node.\n - type: type of node (leaf, internal, or root).\n - parent: parent id. necessary for constructing trees.\n - length: length of branch from parent to node.\n - distance: distance from root.\n\n Parameters\n ----------\n filename: str (default is None)\n newick file to read into DataFrame.\n\n data: str (default is None)\n newick string to parse and read into DataFrame.\n\n add_node_labels: bool\n If true, labels the internal nodes with numbers.\n\n Returns\n -------\n df: phylopandas.DataFrame.\n \"\"\"\n if filename is not None:\n # Use Dendropy to parse tree.\n tree = dendropy.Tree.get(\n path=filename,\n schema=schema,\n preserve_underscores=True)\n elif data is not None:\n tree = dendropy.Tree.get(\n data=data,\n schema=schema,\n preserve_underscores=True)\n else:\n raise Exception('No tree given?')\n\n df = _dendropy_to_dataframe(\n tree, \n add_node_labels=add_node_labels,\n use_uids=use_uids\n )\n return df", "response": "Read a phylogenetic tree into a phylopandas. DataFrame."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _read_function(schema):\n def func(\n filename=None,\n data=None,\n add_node_labels=True,\n use_uids=True,\n **kwargs):\n # Use generic write class to write data.\n return _read(\n filename=filename,\n data=data,\n schema=schema,\n add_node_labels=add_node_labels,\n use_uids=use_uids,\n **kwargs\n )\n # Update docs\n func.__doc__ = _read_doc_template(schema)\n return func", "response": "Add a read method for named schema to a class."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef pandas_df_to_biopython_seqrecord(\n df,\n id_col='uid',\n sequence_col='sequence',\n extra_data=None,\n alphabet=None,\n ):\n \"\"\"Convert pandas dataframe to biopython seqrecord for easy writing.\n\n Parameters\n ----------\n df : Dataframe\n Pandas dataframe to convert\n\n id_col : str\n column in dataframe to use as sequence label\n\n sequence_col str:\n column in dataframe to use as sequence data\n\n extra_data : list\n extra columns to use in sequence description line\n\n alphabet :\n biopython Alphabet object\n\n Returns\n -------\n seq_records :\n List of biopython seqrecords.\n \"\"\"\n seq_records = []\n\n for i, row in df.iterrows():\n # Tries getting sequence data. If a TypeError at the seqrecord\n # creation is thrown, it is assumed that this row does not contain\n # sequence data and therefore the row is ignored.\n try:\n # Get sequence\n seq = Seq(row[sequence_col], alphabet=alphabet)\n\n # Get id\n id = row[id_col]\n\n # Build a description\n description = \"\"\n if extra_data is not None:\n description = \" \".join([row[key] for key in extra_data])\n\n # Build a record\n record = SeqRecord(\n seq=seq,\n id=id,\n description=description,\n )\n seq_records.append(record)\n except TypeError:\n pass\n\n return seq_records", "response": "Convert pandas dataframe to biopython seqrecord for easy writing."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pandas_series_to_biopython_seqrecord(\n series,\n id_col='uid',\n sequence_col='sequence',\n extra_data=None,\n alphabet=None\n ):\n \"\"\"Convert pandas series to biopython seqrecord for easy writing.\n\n Parameters\n ----------\n series : Series\n Pandas series to convert\n\n id_col : str\n column in dataframe to use as sequence label\n\n sequence_col : str\n column in dataframe to use as sequence data\n\n extra_data : list\n extra columns to use in sequence description line\n\n Returns\n -------\n seq_records :\n List of biopython seqrecords.\n \"\"\"\n # Get sequence\n seq = Seq(series[sequence_col], alphabet=alphabet)\n\n # Get id\n id = series[id_col]\n\n # Build a description\n description = \"\"\n if extra_data is not None:\n description = \" \".join([series[key] for key in extra_data])\n\n # Build a record\n record = SeqRecord(\n seq=seq,\n id=id,\n description=description,\n )\n\n seq_records = [record]\n return seq_records", "response": "Convert pandas series to biopython seqrecord for easy writing."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _write_method(schema):\n def method(\n self,\n filename=None,\n schema=schema,\n id_col='uid',\n sequence_col='sequence',\n extra_data=None,\n alphabet=None,\n **kwargs):\n # Use generic write class to write data.\n return _write(\n self._data,\n filename=filename,\n schema=schema,\n id_col=id_col,\n sequence_col=sequence_col,\n extra_data=extra_data,\n alphabet=alphabet,\n **kwargs\n )\n # Update docs\n method.__doc__ = _write_doc_template(schema)\n return method", "response": "Add a write method to a class."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a write method to a class.", "response": "def _write_function(schema):\n \"\"\"Add a write method for named schema to a class.\n \"\"\"\n def func(\n data,\n filename=None,\n schema=schema,\n id_col='uid',\n sequence_col='sequence',\n extra_data=None,\n alphabet=None,\n **kwargs):\n # Use generic write class to write data.\n return _write(\n data,\n filename=filename,\n schema=schema,\n id_col=id_col,\n sequence_col=sequence_col,\n extra_data=extra_data,\n alphabet=alphabet,\n **kwargs\n )\n # Update docs\n func.__doc__ = _write_doc_template(schema)\n return func"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _read(\n filename,\n schema,\n seq_label='sequence',\n alphabet=None,\n use_uids=True,\n **kwargs):\n \"\"\"Use BioPython's sequence parsing module to convert any file format to\n a Pandas DataFrame.\n\n The resulting DataFrame has the following columns:\n - name\n - id\n - description\n - sequence\n \"\"\"\n # Check Alphabet if given\n if alphabet is None:\n alphabet = Bio.Alphabet.Alphabet()\n\n elif alphabet in ['dna', 'rna', 'protein', 'nucleotide']:\n alphabet = getattr(Bio.Alphabet, 'generic_{}'.format(alphabet))\n\n else:\n raise Exception(\n \"The alphabet is not recognized. Must be 'dna', 'rna', \"\n \"'nucleotide', or 'protein'.\")\n\n kwargs.update(alphabet=alphabet)\n\n # Prepare DataFrame fields.\n data = {\n 'id': [],\n seq_label: [],\n 'description': [],\n 'label': []\n }\n if use_uids:\n data['uid'] = []\n\n # Parse Fasta file.\n for i, s in enumerate(SeqIO.parse(filename, format=schema, **kwargs)):\n data['id'].append(s.id)\n data[seq_label].append(str(s.seq))\n data['description'].append(s.description)\n data['label'].append(s.name)\n\n if use_uids:\n data['uid'].append(get_random_id(10))\n\n # Port to DataFrame.\n return pd.DataFrame(data)", "response": "Use BioPython s sequence parsing module to convert any file format to a Pandas DataFrame."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a read method to a class.", "response": "def _read_method(schema):\n \"\"\"Add a write method for named schema to a class.\n \"\"\"\n def func(\n self,\n filename,\n seq_label='sequence',\n alphabet=None,\n combine_on='uid',\n use_uids=True,\n **kwargs):\n # Use generic write class to write data.\n df0 = self._data\n df1 = _read(\n filename=filename,\n schema=schema,\n seq_label=seq_label,\n alphabet=alphabet,\n use_uids=use_uids,\n **kwargs\n )\n return df0.phylo.combine(df1, on=combine_on)\n\n # Update docs\n func.__doc__ = _read_doc_template(schema)\n return func"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a read method for named schema to a class.", "response": "def _read_function(schema):\n \"\"\"Add a write method for named schema to a class.\n \"\"\"\n def func(\n filename,\n seq_label='sequence',\n alphabet=None,\n use_uids=True,\n **kwargs):\n # Use generic write class to write data.\n return _read(\n filename=filename,\n schema=schema,\n seq_label=seq_label,\n alphabet=alphabet,\n use_uids=use_uids,\n **kwargs\n )\n # Update docs\n func.__doc__ = _read_doc_template(schema)\n return func"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread BLAST XML format.", "response": "def read_blast_xml(filename, **kwargs):\n \"\"\"Read BLAST XML format.\"\"\"\n # Read file.\n with open(filename, 'r') as f:\n blast_record = NCBIXML.read(f)\n\n # Prepare DataFrame fields.\n data = {'accession': [],\n 'hit_def': [],\n 'hit_id': [],\n 'title': [],\n 'length': [],\n 'e_value': [],\n 'sequence': []}\n\n # Get alignments from blast result.\n for i, s in enumerate(blast_record.alignments):\n data['accession'] = s.accession\n data['hit_def'] = s.hit_def\n data['hit_id'] = s.hit_id\n data['title'] = s.title\n data['length'] = s.length\n data['e_value'] = s.hsps[0].expect\n data['sequence'] = s.hsps[0].sbjct\n\n # Port to DataFrame.\n return pd.DataFrame(data)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _pandas_df_to_dendropy_tree(\n df,\n taxon_col='uid',\n taxon_annotations=[],\n node_col='uid',\n node_annotations=[],\n branch_lengths=True,\n ):\n \"\"\"Turn a phylopandas dataframe into a dendropy tree.\n\n Parameters\n ----------\n df : DataFrame\n DataFrame containing tree data.\n\n taxon_col : str (optional)\n Column in dataframe to label the taxon. If None, the index will be used.\n\n taxon_annotations : str\n List of columns to annotation in the tree taxon.\n\n node_col : str (optional)\n Column in dataframe to label the nodes. If None, the index will be used.\n\n node_annotations : str\n List of columns to annotation in the node taxon.\n\n branch_lengths : bool\n If True, inclues branch lengths.\n \"\"\"\n if isinstance(taxon_col, str) is False:\n raise Exception(\"taxon_col must be a string.\")\n\n if isinstance(node_col, str) is False:\n raise Exception(\"taxon_col must be a string.\")\n\n # Construct a list of nodes from dataframe.\n taxon_namespace = dendropy.TaxonNamespace()\n nodes = {}\n for idx in df.index:\n # Get node data.\n data = df.loc[idx]\n\n # Get taxon for node (if leaf node).\n taxon = None\n if data['type'] == 'leaf':\n taxon = dendropy.Taxon(label=data[taxon_col])\n # Add annotations data.\n for ann in taxon_annotations:\n taxon.annotations.add_new(ann, data[ann])\n taxon_namespace.add_taxon(taxon)\n\n # Get label for node.\n label = data[node_col]\n\n # Get edge length.\n edge_length = None\n if branch_lengths is True:\n edge_length = data['length']\n\n # Build a node\n n = dendropy.Node(\n taxon=taxon,\n label=label,\n edge_length=edge_length\n )\n \n # Add node annotations\n for ann in node_annotations:\n n.annotations.add_new(ann, data[ann])\n\n nodes[idx] = n\n\n # Build branching pattern for nodes.\n root = None\n for idx, node in nodes.items():\n # Get node data.\n data = df.loc[idx]\n\n # Get children nodes\n children_idx = df[df['parent'] == data['id']].index\n children_nodes = [nodes[i] for i in children_idx]\n\n # Set child nodes\n nodes[idx].set_child_nodes(children_nodes)\n\n # Check if this is root.\n if data['parent'] is None:\n root = nodes[idx]\n\n # Build tree.\n tree = dendropy.Tree(\n seed_node=root,\n taxon_namespace=taxon_namespace\n )\n return tree", "response": "Turn a phylopandas dataframe into a dendropy tree."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a phylopandas tree dataframe to various formats.", "response": "def _write(\n df,\n filename=None,\n schema='newick',\n taxon_col='uid',\n taxon_annotations=[],\n node_col='uid',\n node_annotations=[],\n branch_lengths=True,\n **kwargs\n ):\n \"\"\"Write a phylopandas tree DataFrame to various formats.\n\n Parameters\n ----------\n df : DataFrame\n DataFrame containing tree data.\n\n filename : str\n filepath to write out tree. If None, will return string.\n\n schema : str\n tree format to write out.\n\n taxon_col : str (optional)\n Column in dataframe to label the taxon. If None, the index will be used.\n\n taxon_annotations : str\n List of columns to annotation in the tree taxon.\n\n node_col : str (optional)\n Column in dataframe to label the nodes. If None, the index will be used.\n\n node_annotations : str\n List of columns to annotation in the node taxon.\n\n branch_lengths : bool\n If True, inclues branch lengths.\n \"\"\"\n tree = _pandas_df_to_dendropy_tree(\n df,\n taxon_col=taxon_col,\n taxon_annotations=taxon_annotations,\n node_col=node_col,\n node_annotations=node_annotations,\n branch_lengths=branch_lengths,\n )\n\n # Write out format\n print(schema)\n if filename is not None:\n tree.write(path=filename, schema=schema, suppress_annotations=False, **kwargs)\n else:\n return tree.as_string(schema=schema)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _write_method(schema):\n def method(\n self,\n filename=None,\n schema=schema,\n taxon_col='uid',\n taxon_annotations=[],\n node_col='uid',\n node_annotations=[],\n branch_lengths=True,\n **kwargs):\n # Use generic write class to write data.\n return _write(\n self._data,\n filename=filename,\n schema=schema,\n taxon_col=taxon_col,\n taxon_annotations=taxon_annotations,\n node_col=node_col,\n node_annotations=node_annotations,\n branch_lengths=branch_lengths,\n **kwargs\n )\n # Update docs\n method.__doc__ = _write_doc_template(schema)\n return method", "response": "Add a write method to a class."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds a write method to a class.", "response": "def _write_function(schema):\n \"\"\"Add a write method for named schema to a class.\n \"\"\"\n def func(\n data,\n filename=None,\n schema=schema,\n taxon_col='uid',\n taxon_annotations=[],\n node_col='uid',\n node_annotations=[],\n branch_lengths=True,\n **kwargs):\n # Use generic write class to write data.\n return _write(\n data,\n filename=filename,\n schema=schema,\n taxon_col=taxon_col,\n taxon_annotations=taxon_annotations,\n node_col=node_col,\n node_annotations=node_annotations,\n branch_lengths=branch_lengths,\n **kwargs\n )\n # Update docs\n func.__doc__ = _write_doc_template(schema)\n return func"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates a random alpha - numerical id.", "response": "def get_random_id(length):\n \"\"\"Generate a random, alpha-numerical id.\"\"\"\n alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits\n return ''.join(random.choice(alphabet) for _ in range(length))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_folder(self, folder):\n self.folder = folder\n self.templates.directories[0] = folder\n self.app.root_path = folder", "response": "Sets the folder where the files to serve are located."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlaunch a development web server.", "response": "def run(self, host='0.0.0.0', port=8080):\n \"\"\"\n Launch a development web server.\n \"\"\"\n waitress.serve(self.app, host=host, port=port)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the content of a file in the specified folder", "response": "def get(self, path):\n \"\"\"\n Get the content of a file, indentified by its path relative to the folder configured\n in PyGreen. If the file extension is one of the extensions that should be processed\n through Mako, it will be processed.\n \"\"\"\n data = self.app.test_client().get(\"/%s\" % path).data\n return data"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates a complete static version of the web site.", "response": "def gen_static(self, output_folder):\n \"\"\"\n Generates a complete static version of the web site. It will stored in\n output_folder.\n \"\"\"\n files = []\n for l in self.file_listers:\n files += l()\n for f in files:\n _logger.info(\"generating %s\" % f)\n content = self.get(f)\n loc = os.path.join(output_folder, f)\n d = os.path.dirname(loc)\n if not os.path.exists(d):\n os.makedirs(d)\n with open(loc, \"wb\") as file_:\n file_.write(content)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cli(self, cmd_args=None):\n logging.basicConfig(level=logging.INFO, format='%(message)s')\n\n parser = argparse.ArgumentParser(description='PyGreen, micro web framework/static web site generator')\n subparsers = parser.add_subparsers(dest='action')\n\n parser_serve = subparsers.add_parser('serve', help='serve the web site')\n parser_serve.add_argument('-p', '--port', type=int, default=8080, help='port to serve on')\n parser_serve.add_argument('-f', '--folder', default=\".\", help='folder containg files to serve')\n parser_serve.add_argument('-d', '--disable-templates', action='store_true', default=False, help='just serve static files, do not use Mako')\n def serve():\n if args.disable_templates:\n self.template_exts = set([])\n self.run(port=args.port)\n parser_serve.set_defaults(func=serve)\n\n parser_gen = subparsers.add_parser('gen', help='generate a static version of the site')\n parser_gen.add_argument('output', help='folder to store the files')\n parser_gen.add_argument('-f', '--folder', default=\".\", help='folder containing files to generate')\n def gen():\n self.gen_static(args.output)\n parser_gen.set_defaults(func=gen)\n\n args = parser.parse_args(cmd_args)\n self.set_folder(args.folder)\n print(parser.description)\n print(\"\")\n args.func()", "response": "Command line interface of PyGreen."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef init(self, switch_configuration, terminal_controller, logger, piping_processor, *args):\n\n self.switch_configuration = switch_configuration\n self.terminal_controller = terminal_controller\n self.logger = logger\n self.piping_processor = piping_processor\n self.sub_processor = None\n self.continuing_to = None\n self.is_done = False\n self.replace_input = False\n self.awaiting_keystroke = False", "response": "Initializes the object with the given switch configuration."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _gccalc(lon, lat, azimuth, maxdist=None):\n glat1 = lat * np.pi / 180.\n glon1 = lon * np.pi / 180.\n s = maxdist / 1.852243\n faz = azimuth * np.pi / 180.\n \n EPS = 0.00000000005\n if ((np.abs(np.cos(glat1)) < EPS) and not (np.abs(np.sin(faz)) < EPS)):\n raise CourseException(\"Only North-South courses are meaningful\")\n\n a = 6378.137 / 1.852243\n f = 1 / 298.257223563\n r = 1 - f\n tu = r * np.tan(glat1)\n sf = np.sin(faz)\n cf = np.cos(faz)\n if (cf == 0):\n b = 0.\n else:\n b = 2. * np.arctan2 (tu, cf)\n\n cu = 1. / np.sqrt(1 + tu * tu)\n su = tu * cu\n sa = cu * sf\n c2a = 1 - sa * sa\n x = 1. + np.sqrt(1. + c2a * (1. / (r * r) - 1.))\n x = (x - 2.) / x\n c = 1. - x\n c = (x * x / 4. + 1.) / c\n d = (0.375 * x * x - 1.) * x\n tu = s / (r * a * c)\n y = tu\n c = y + 1\n while (np.abs (y - c) > EPS):\n sy = np.sin(y)\n cy = np.cos(y)\n cz = np.cos(b + y)\n e = 2. * cz * cz - 1.\n c = y\n x = e * cy\n y = e + e - 1.\n y = (((sy * sy * 4. - 3.) * y * cz * d / 6. + x) *\n d / 4. - cz) * sy * d + tu\n\n b = cu * cy * cf - su * sy\n c = r * np.sqrt(sa * sa + b * b)\n d = su * cy + cu * sy * cf\n glat2 = (np.arctan2(d, c) + np.pi) % (2*np.pi) - np.pi\n c = cu * cy - su * sy * cf\n x = np.arctan2(sy * sf, c)\n c = ((-3. * c2a + 4.) * f + 4.) * c2a * f / 16.\n d = ((e * cy * c + cz) * sy * c + y) * sa\n glon2 = ((glon1 + x - (1. - c) * d * f + np.pi) % (2*np.pi)) - np.pi\n\n baz = (np.arctan2(sa, b) + np.pi) % (2 * np.pi)\n\n glon2 *= 180./np.pi\n glat2 *= 180./np.pi\n baz *= 180./np.pi\n return (glon2, glat2, baz)", "response": "This function calculates the base of the GC algorithm."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns lon lat tuples of a circle which matches the chosen Basemap projection", "response": "def circle(m, centerlon, centerlat, radius, *args, **kwargs):\n \"\"\"\n Return lon, lat tuples of a \"circle\" which matches the chosen Basemap projection\n Takes the following arguments:\n m = basemap instance\n centerlon = originating lon\n centrelat = originating lat\n radius = radius\n\n \"\"\"\n glon1 = centerlon\n glat1 = centerlat\n X = []\n Y = []\n for azimuth in range(0, 360):\n glon2, glat2, baz = _gccalc(glon1, glat1, azimuth, radius)\n X.append(glon2)\n Y.append(glat2)\n X.append(X[0])\n Y.append(Y[0])\n\n proj_x, proj_y = m(X,Y)\n return zip(proj_x, proj_y)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nregister a new mime type.", "response": "def register_mime(shortname, mime_types):\n \"\"\"\n Register a new mime type.\n Usage example:\n mimerender.register_mime('svg', ('application/x-svg', 'application/svg+xml',))\n After this you can do:\n @mimerender.mimerender(svg=render_svg)\n def GET(...\n ...\n \"\"\"\n if shortname in _MIME_TYPES:\n raise MimeRenderException('\"%s\" has already been registered'%shortname)\n _MIME_TYPES[shortname] = mime_types"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef wsgi_wrap(app):\n '''\n Wraps a standard wsgi application e.g.:\n def app(environ, start_response)\n It intercepts the start_response callback and grabs the results from it\n so it can return the status, headers, and body as a tuple\n '''\n @wraps(app)\n def wrapped(environ, start_response):\n status_headers = [None, None]\n def _start_response(status, headers):\n status_headers[:] = [status, headers]\n body = app(environ, _start_response)\n ret = body, status_headers[0], status_headers[1]\n return ret\n return wrapped", "response": "A decorator that wraps a wsgi application e. g. returns a status headers and body as a tuple\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef map_exceptions(self, mapping, *args, **kwargs):\n @self.__call__(*args, **kwargs)\n def helper(e, status):\n return dict(exception=e), status\n\n def wrap(target):\n @wraps(target)\n def wrapper(*args, **kwargs):\n try:\n return target(*args, **kwargs)\n except BaseException as e:\n for klass, status in mapping:\n if isinstance(e, klass):\n return helper(e, status)\n raise\n return wrapper\n return wrap", "response": "A decorator which maps exceptions to the internal state of the object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef relative(self):\n\t\t\n\t\tscheme = self.scheme\n\t\t\n\t\tif not scheme:\n\t\t\treturn True\n\t\t\n\t\treturn scheme.is_relative(self)", "response": "Identify if this URI is relative to some current context."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nattempt to resolve a new URI given an updated URI partial or complete.", "response": "def resolve(self, uri=None, **parts):\n\t\t\"\"\"Attempt to resolve a new URI given an updated URI, partial or complete.\"\"\"\n\t\t\n\t\tif uri:\n\t\t\tresult = self.__class__(urljoin(str(self), str(uri)))\n\t\telse:\n\t\t\tresult = self.__class__(self)\n\t\t\n\t\tfor part, value in parts.items():\n\t\t\tif part not in self.__all_parts__:\n\t\t\t\traise TypeError(\"Unknown URI component: \" + part)\n\t\t\t\n\t\t\tsetattr(result, part, value)\n\t\t\n\t\treturn result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef replace(self, year=None, month=None, day=None):\n\n return JalaliDate(\n year if year else self.year,\n month if month else self.month,\n day if day else self.day\n )", "response": "Replaces the given arguments on this instance and returns a new instance."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalculating the corresponding date in the gregorian calendar.", "response": "def todate(self):\n \"\"\"\n Calculates the corresponding day in the gregorian calendar. this is the main use case of this library.\n\n :return: Corresponding date in gregorian calendar.\n :rtype: :py:class:`datetime.date`\n \"\"\"\n arr = get_gregorian_date_from_julian_day(self.tojulianday())\n return datetime.date(int(arr[0]), int(arr[1]), int(arr[2]))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef timetuple(self):\n return time.struct_time((\n self.year,\n self.month,\n self.day,\n 0,\n 0,\n 0,\n self.weekday(),\n self.dayofyear(),\n -1\n ))", "response": "Returns a time. struct_time object for the current object."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef weekofyear(self, first_day_of_week=SATURDAY):\n first_day_of_year = self.firstdayofyear()\n days = (self - first_day_of_year).days\n offset = first_day_of_week - first_day_of_year.weekday()\n if offset < 0:\n offset += 7\n\n if days < offset:\n return 0\n\n return int((days - offset) / 7 + 1)", "response": "returns the week number of the year."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new instance of the class cls from the given timestamp.", "response": "def fromtimestamp(cls, timestamp, tz=None):\n \"\"\"\n Creates a new :py:class:`khayyam.JalaliDatetime` instance from the given posix timestamp.\n\n If optional argument tz is :py:obj:`None` or not specified, the timestamp is converted to\n the platform's local date and time, and the returned datetime object is naive.\n\n Else tz must be an instance of a class :py:class:`datetime.tzinfo` subclass,\n and the timestamp is converted to tz's time zone. In this case the result is\n equivalent to `tz.fromutc(JalaliDatetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))`.\n\n This method may raise `ValueError`, if the timestamp is out of the range of values\n supported by the platform C localtime() or gmtime() functions.\n It's common for this to be restricted to years in 1970 through 2038.\n\n Note that on non-POSIX systems that include leap seconds in their\n notion of a timestamp, leap seconds are ignored by fromtimestamp(), and then\n it's possible to have two timestamps differing by a second that yield\n identical datetime objects. See also :py:class:`khayyam.JalaliDatetime.utcfromtimestamp`.\n\n .. testsetup:: api-datetime-fromtimestamp\n\n import khayyam\n from khayyam import JalaliDatetime\n\n .. doctest:: api-datetime-fromtimestamp\n\n >>> JalaliDatetime.fromtimestamp(1313132131.21232)\n khayyam.JalaliDatetime(1390, 5, 21, 11, 25, 31, 212320, Jomeh)\n\n :param timestamp: float the posix timestamp, i.e 1014324234.23423423.\n :param tz: :py:class:`datetime.tzinfo` The optional timezone to get local date & time from the given timestamp.\n :return: The local date and time corresponding to the POSIX timestamp, such as is returned by :py:func:`time.time()`.\n :rtype: :py:class:`khayyam.JalaliDatetime`\n \"\"\"\n return cls(datetime.fromtimestamp(timestamp, tz=tz))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a new jalali datetime object whose date members are equal to the given date object s and whose _time members are equal to the given time object s.", "response": "def combine(cls, date, _time):\n \"\"\"\n Return a new jalali datetime object whose date members are equal to the given date object's, and whose _time\n and tzinfo members are equal to the given _time object's.\n For any datetime object d, d == datetime.combine(d.date(), d.timetz()). If date is a datetime object, its _time\n and tzinfo members are ignored.\n\n :param date: :py:class:`khayyam.JalaliDate` the date object to combine.\n :param _time: :py:class:`datetime.time` the time object to combine.\n :return: the combined jalali date & time object.\n :rtype: :py:class:`khayyam.JalaliDatetime`\n \"\"\"\n if isinstance(date, (JalaliDatetime, khayyam.JalaliDate)):\n date = date.todate()\n return cls(datetime.combine(date, _time))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef todatetime(self):\n arr = get_gregorian_date_from_julian_day(self.tojulianday())\n return datetime(int(arr[0]), int(arr[1]), int(arr[2]), self.hour, self.minute, self.second, self.microsecond,\n self.tzinfo)", "response": "Converts the current instance to the python builtins datetime instance."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef date(self):\n return khayyam.JalaliDate(self.year, self.month, self.day)", "response": "Return a new object with same year month and day."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef replace(self, year=None, month=None, day=None, hour=None,\n minute=None, second=None, microsecond=None, tzinfo=None):\n \"\"\"\n Return a :py:class:`khayyam.JalaliDatetime` instance with the same attributes, except for those attributes\n given new values by whichever keyword arguments are specified. Note that tzinfo=None can be specified to create\n a naive datetime from an aware datetime with no conversion of date and time data, without adjusting the date\n the and time based tzinfo.\n\n :param year: int\n :param month: int\n :param day: int\n :param hour: int\n :param minute: int\n :param second: int\n :param microsecond: int\n :param tzinfo: :py:class:`datetime.tzinfo`\n :rtype: :py:class:`khayyam.JalaliDatetime`\n \"\"\"\n year, month, day = self._validate(\n year if year else self.year,\n month if month else self.month,\n day if day else self.day\n )\n\n result = JalaliDatetime(\n year,\n month,\n day,\n self.hour if hour is None else hour,\n self.minute if minute is None else minute,\n self.second if second is None else second,\n self.microsecond if microsecond is None else microsecond,\n tzinfo if tzinfo != self.tzinfo else self.tzinfo\n )\n return result", "response": "Returns a new instance of the same attributes except for those attributes\n given new values by whichever keyword arguments are specified."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a new object with new time zone information set to tz.", "response": "def astimezone(self, tz):\n \"\"\"\n Return a :py:class:`khayyam.JalaliDatetime` object with new :py:meth:`khayyam.JalaliDatetime.tzinfo` attribute\n tz, adjusting the date and time data so the result is the same UTC time as self, but in *tz*\u2018s local time.\n\n *tz* must be an instance of a :py:class:`datetime.tzinfo` subclass, and\n its :py:meth:`datetime.tzinfo.utcoffset()` and :py:meth:`datetime.tzinfo.dst()` methods must not\n return :py:obj:`None`. *self* must be aware (`self.tzinfo` must not be `None`, and `self.utcoffset()` must\n not return `None`).\n\n If `self.tzinfo` is `tz`, `self.astimezone(tz)` is equal to `self`: no adjustment of date or time data is\n performed. Else the result is local time in time zone `tz`, representing the same UTC time as `self`:\n after `astz = dt.astimezone(tz), astz - astz.utcoffset()` will usually have the same date and time data as\n `dt - dt.utcoffset()`. The discussion of class :py:class:`datetime.tzinfo` explains the cases at Daylight\n Saving Time transition boundaries where this cannot be achieved (an issue only if `tz` models both\n standard and daylight time).\n\n If you merely want to attach a time zone object `tz` to a datetime dt without adjustment of date and time data,\n use `dt.replace(tzinfo=tz)`. If you merely want to remove the time zone object from an aware datetime dt\n without conversion of date and time data, use `dt.replace(tzinfo=None)`.\n\n Note that the default :py:meth:`datetime.tzinfo.fromutc()` method can be overridden in a\n :py:class:`datetime.tzinfo` subclass to affect the result returned\n by :py:meth:`khayyam.JalaliDatetime.astimezone()`. Ignoring error\n cases, :py:meth:`khayyam.JalaliDatetime.astimezone()` acts like:\n\n .. code-block:: python\n :emphasize-lines: 3,5\n\n def astimezone(self, tz): # doctest: +SKIP\n\n if self.tzinfo is tz:\n return self\n if self.tzinfo:\n utc = self - self.utcoffset()\n else:\n utc = self\n return tz.fromutc(utc.replace(tzinfo=tz))\n\n\n :param tz: :py:class:`datetime.tzinfo`\n :rtype: :py:class:`khayyam.JalaliDatetime`\n \"\"\"\n if self.tzinfo is tz:\n return self\n if self.tzinfo:\n utc = self - self.utcoffset()\n else:\n utc = self\n return tz.fromutc(utc.replace(tzinfo=tz))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef hour12(self):\n res = self.hour\n if res > 12:\n res -= 12\n elif res == 0:\n res = 12\n return res", "response": "Return The hour value between 1 - 12. use khayyam. JalaliDatetime. ampmascii to determineante meridiem and post meridiem. use khayyam. JalaliDatetime. ampmascii to determine post meridiem. use khayyam. jcjalaliDatetime. ampmascii to determineante meridiem and post meridiem."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the Linear Prediction Coefficients for the input signal and the given order.", "response": "def lpc_ref(signal, order):\n \"\"\"Compute the Linear Prediction Coefficients.\n\n Return the order + 1 LPC coefficients for the signal. c = lpc(x, k) will\n find the k+1 coefficients of a k order linear filter:\n\n xp[n] = -c[1] * x[n-2] - ... - c[k-1] * x[n-k-1]\n\n Such as the sum of the squared-error e[i] = xp[i] - x[i] is minimized.\n\n Parameters\n ----------\n signal: array_like\n input signal\n order : int\n LPC order (the output will have order + 1 items)\n\n Notes\n ----\n This is just for reference, as it is using the direct inversion of the\n toeplitz matrix, which is really slow\"\"\"\n if signal.ndim > 1:\n raise ValueError(\"Array of rank > 1 not supported yet\")\n if order > signal.size:\n raise ValueError(\"Input signal must have a lenght >= lpc order\")\n\n if order > 0:\n p = order + 1\n r = np.zeros(p, 'float32')\n # Number of non zero values in autocorrelation one needs for p LPC\n # coefficients\n nx = np.min([p, signal.size])\n x = np.correlate(signal, signal, 'full')\n r[:nx] = x[signal.size - 1:signal.size + order]\n phi = np.dot(sp.linalg.inv(sp.linalg.toeplitz(r[:-1])), -r[1:])\n return np.concatenate(([1.], phi))\n else:\n return np.ones(1, dtype='float32')"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompute the biased autocorrelation of x along the given axis.", "response": "def acorr_lpc(x, axis=-1):\n \"\"\"Compute autocorrelation of x along the given axis.\n\n This compute the biased autocorrelation estimator (divided by the size of\n input signal)\n\n Notes\n -----\n The reason why we do not use acorr directly is for speed issue.\"\"\"\n if not np.isrealobj(x):\n raise ValueError(\"Complex input not supported yet\")\n\n maxlag = x.shape[axis]\n nfft = int(2 ** nextpow2(2 * maxlag - 1))\n\n if axis != -1:\n x = np.swapaxes(x, -1, axis)\n a = _acorr_last_axis(x, nfft, maxlag)\n if axis != -1:\n a = np.swapaxes(a, -1, axis)\n return a"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncomputing the Linear Prediction Coefficients.", "response": "def lpc(signal, order, axis=-1):\n \"\"\"Compute the Linear Prediction Coefficients.\n\n Return the order + 1 LPC coefficients for the signal. c = lpc(x, k) will\n find the k+1 coefficients of a k order linear filter:\n\n xp[n] = -c[1] * x[n-2] - ... - c[k-1] * x[n-k-1]\n\n Such as the sum of the squared-error e[i] = xp[i] - x[i] is minimized.\n\n Parameters\n ----------\n signal: array_like\n input signal\n order : int\n LPC order (the output will have order + 1 items)\n\n Returns\n -------\n a : array-like\n the solution of the inversion.\n e : array-like\n the prediction error.\n k : array-like\n reflection coefficients.\n\n Notes\n -----\n This uses Levinson-Durbin recursion for the autocorrelation matrix\n inversion, and fft for the autocorrelation computation.\n\n For small order, particularly if order << signal size, direct computation\n of the autocorrelation is faster: use levinson and correlate in this case.\"\"\"\n n = signal.shape[axis]\n if order > n:\n raise ValueError(\"Input signal must have length >= order\")\n\n r = acorr_lpc(signal, axis)\n return levinson_1d(r, order)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntake in an explicit mapping of full paths to. wav files to have acoustic similarity computed.", "response": "def acoustic_similarity_mapping(path_mapping, analysis_function, distance_function, stop_check=None, call_back=None, multiprocessing=True):\n \"\"\"Takes in an explicit mapping of full paths to .wav files to have\n acoustic similarity computed.\n\n Parameters\n ----------\n path_mapping : iterable of iterables\n Explicit mapping of full paths of .wav files, in the form of a\n list of tuples to be compared.\n\n\n Returns\n -------\n dict\n Returns a list of tuples corresponding to the `path_mapping` input,\n with a new final element in the tuple being the similarity/distance\n score for that mapping.\n\n \"\"\"\n\n num_cores = int((3 * cpu_count()) / 4)\n segments = set()\n for x in path_mapping:\n segments.update(x)\n if multiprocessing:\n cache = generate_cache_mp(segments, analysis_function, num_cores, call_back, stop_check)\n asim = calculate_distances_mp(path_mapping, cache, distance_function, num_cores, call_back, stop_check)\n else:\n cache = generate_cache_th(segments, analysis_function, num_cores, call_back, stop_check)\n asim = calculate_distances_th(path_mapping, cache, distance_function, num_cores, call_back, stop_check)\n return asim"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntaking in an explicit mapping of full paths to. wav files to have acoustic similarity computed.", "response": "def axb_mapping(path_mapping, analysis_function, distance_function, stop_check=None, call_back=None, multiprocessing=True):\n \"\"\"Takes in an explicit mapping of full paths to .wav files to have\n acoustic similarity computed.\n\n Parameters\n ----------\n path_mapping : iterable of iterables\n Explicit mapping of full paths of .wav files, in the form of a\n list of tuples to be compared.\n\n\n Returns\n -------\n dict\n Returns a list of tuples corresponding to the `path_mapping` input,\n with a new final element in the tuple being the similarity/distance\n score for that mapping.\n\n \"\"\"\n\n num_cores = int((3 * cpu_count()) / 4)\n segments = set()\n for x in path_mapping:\n segments.update(x)\n if multiprocessing:\n cache = generate_cache_mp(segments, analysis_function, num_cores, call_back, stop_check)\n asim = calculate_axb_ratio_mp(path_mapping, cache, distance_function, num_cores, call_back, stop_check)\n else:\n cache = generate_cache_th(segments, analysis_function, num_cores, call_back, stop_check)\n asim = calculate_axb_ratio_th(path_mapping, cache, distance_function, num_cores, call_back, stop_check)\n return asim"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef acoustic_similarity_directories(directories, analysis_function, distance_function, stop_check=None, call_back=None, multiprocessing=True):\n\n files = []\n\n if call_back is not None:\n call_back('Mapping directories...')\n call_back(0, len(directories))\n cur = 0\n for d in directories:\n if not os.path.isdir(d):\n continue\n if stop_check is not None and stop_check():\n return\n if call_back is not None:\n cur += 1\n if cur % 3 == 0:\n call_back(cur)\n\n files += [os.path.join(d, x) for x in os.listdir(d) if x.lower().endswith('.wav')]\n\n if len(files) == 0:\n raise (ConchError(\"The directories specified do not contain any wav files\"))\n\n if call_back is not None:\n call_back('Mapping directories...')\n call_back(0, len(files) * len(files))\n cur = 0\n path_mapping = list()\n for x in files:\n for y in files:\n if stop_check is not None and stop_check():\n return\n if call_back is not None:\n cur += 1\n if cur % 20 == 0:\n call_back(cur)\n if not x.lower().endswith('.wav'):\n continue\n if not y.lower().endswith('.wav'):\n continue\n if x == y:\n continue\n path_mapping.append((x, y))\n\n result = acoustic_similarity_mapping(path_mapping, analysis_function, distance_function, stop_check, call_back, multiprocessing)\n return result", "response": "Analyze many directories and return a list of acoustic similarity files."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompute the distance between two representations using Dynamic Time Warping.", "response": "def dtw_distance(rep_one, rep_two, norm=True):\n \"\"\"Computes the distance between two representations with the same\n number of filters using Dynamic Time Warping.\n\n Parameters\n ----------\n rep_one : 2D array\n First representation to compare. First dimension is time in frames\n or samples and second dimension is the features.\n rep_two : 2D array\n Second representation to compare. First dimension is time in frames\n or samples and second dimension is the features.\n\n Returns\n -------\n float\n Distance of dynamically time warping `rep_one` to `rep_two`.\n\n \"\"\"\n if not isinstance(rep_one, np.ndarray):\n rep_one = rep_one.to_array()\n if not isinstance(rep_two, np.ndarray):\n rep_two = rep_two.to_array()\n assert (rep_one.shape[1] == rep_two.shape[1])\n distMat = generate_distance_matrix(rep_one, rep_two)\n return regularDTW(distMat, norm=norm)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef generate_distance_matrix(source, target, weights=None):\n if weights is None:\n weights = ones((source.shape[1], 1))\n sLen = source.shape[0]\n tLen = target.shape[0]\n distMat = zeros((sLen, tLen))\n for i in range(sLen):\n for j in range(tLen):\n distMat[i, j] = euclidean(source[i, :], target[j, :])\n return distMat", "response": "Generates a local distance matrix for use in dynamic time warping."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nusing a local distance matrix to perform dynamic time warping.", "response": "def regularDTW(distMat, norm=True):\n \"\"\"Use a local distance matrix to perform dynamic time warping.\n\n Parameters\n ----------\n distMat : 2D array\n Local distance matrix.\n\n Returns\n -------\n float\n Total unweighted distance of the optimal path through the\n local distance matrix.\n\n \"\"\"\n sLen, tLen = distMat.shape\n totalDistance = zeros((sLen, tLen))\n totalDistance[0:sLen, 0:tLen] = distMat\n\n minDirection = zeros((sLen, tLen))\n\n for i in range(1, sLen):\n totalDistance[i, 0] = totalDistance[i, 0] + totalDistance[i - 1, 0]\n\n for j in range(1, tLen):\n totalDistance[0, j] = totalDistance[0, j] + totalDistance[0, j - 1]\n\n for i in range(1, sLen):\n for j in range(1, tLen):\n # direction,minPrevDistance = min(enumerate([totalDistance[i,j],totalDistance[i,j+1],totalDistance[i+1,j]]), key=operator.itemgetter(1))\n # totalDistance[i+1,j+1] = totalDistance[i+1,j+1] + minPrevDistance\n # minDirection[i,j] = direction\n minDirection[i, j], totalDistance[i, j] = min(\n enumerate([totalDistance[i - 1, j - 1] + 2 * totalDistance[i, j],\n totalDistance[i - 1, j] + totalDistance[i, j],\n totalDistance[i, j - 1] + totalDistance[i, j]]), key=operator.itemgetter(1))\n if norm:\n return totalDistance[sLen - 1, tLen - 1] / (sLen + tLen)\n return totalDistance[sLen - 1, tLen - 1]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nfilter the signal x with the FIR filter described by the filter described by the overlap - add method.", "response": "def fftfilt(b, x, *n):\n \"\"\"Filter the signal x with the FIR filter described by the\n coefficients in b using the overlap-add method. If the FFT\n length n is not specified, it and the overlap-add block length\n are selected so as to minimize the computational cost of\n the filtering operation.\"\"\"\n\n N_x = len(x)\n N_b = len(b)\n\n # Determine the FFT length to use:\n if len(n):\n\n # Use the specified FFT length (rounded up to the nearest\n # power of 2), provided that it is no less than the filter\n # length:\n n = n[0]\n if n != int(n) or n <= 0:\n raise ValueError('n must be a nonnegative integer')\n if n < N_b:\n n = N_b\n N_fft = 2 ** nextpow2(n)\n else:\n\n if N_x > N_b:\n\n # When the filter length is smaller than the signal,\n # choose the FFT length and block size that minimize the\n # FLOPS cost. Since the cost for a length-N FFT is\n # (N/2)*log2(N) and the filtering operation of each block\n # involves 2 FFT operations and N multiplications, the\n # cost of the overlap-add method for 1 length-N block is\n # N*(1+log2(N)). For the sake of efficiency, only FFT\n # lengths that are powers of 2 are considered:\n N = 2 ** np.arange(np.ceil(np.log2(N_b)), np.floor(np.log2(N_x)))\n cost = np.ceil(N_x / (N - N_b + 1)) * N * (np.log2(N) + 1)\n if len(cost) > 0:\n N_fft = N[np.argmin(cost)]\n else:\n N_fft = 2 ** nextpow2(N_b + N_x - 1)\n\n else:\n\n # When the filter length is at least as long as the signal,\n # filter the signal using a single block:\n N_fft = 2 ** nextpow2(N_b + N_x - 1)\n\n N_fft = int(N_fft)\n\n # Compute the block length:\n L = int(N_fft - N_b + 1)\n\n # Compute the transform of the filter:\n H = fft(b, N_fft)\n\n y = np.zeros(N_x, np.float32)\n i = 0\n while i <= N_x:\n il = np.min([i + L, N_x])\n k = np.min([i + N_fft, N_x])\n yt = ifft(fft(x[i:il], N_fft) * H, N_fft) # Overlap..\n y[i:k] = y[i:k] + yt[:k - i] # and add\n i += L\n return y"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert a spectrum into a cepstrum via type - III DCT.", "response": "def dct_spectrum(spec):\n \"\"\"Convert a spectrum into a cepstrum via type-III DCT (following HTK).\n\n Parameters\n ----------\n spec : array\n Spectrum to perform a DCT on.\n\n Returns\n -------\n array\n Cepstrum of the input spectrum.\n\n \"\"\"\n ncep = spec.shape[0]\n dctm = np.zeros((ncep, ncep))\n for i in range(ncep):\n dctm[i, :] = np.cos(i * np.arange(1, 2 * ncep, 2) / (2 * ncep) * np.pi) * np.sqrt(2 / ncep)\n dctm *= 0.230258509299405\n cep = np.dot(dctm, (10 * np.log10(spec + np.spacing(1))))\n return cep"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconstruct a filterbank for a set of mel - frequency filters.", "response": "def construct_filterbank(num_filters, nfft, sr, min_freq, max_freq):\n \"\"\"Constructs a mel-frequency filter bank.\n\n Parameters\n ----------\n nfft : int\n Number of points in the FFT.\n\n Returns\n -------\n array\n Filter bank to multiply an FFT spectrum to create a mel-frequency\n spectrum.\n\n \"\"\"\n\n min_mel = freq_to_mel(min_freq)\n max_mel = freq_to_mel(max_freq)\n mel_points = np.linspace(min_mel, max_mel, num_filters + 2)\n bin_freqs = mel_to_freq(mel_points)\n # bins = round((nfft - 1) * bin_freqs / sr)\n\n fftfreqs = np.arange(int(nfft / 2 + 1)) / nfft * sr\n\n fbank = np.zeros((num_filters, int(nfft / 2 + 1)))\n for i in range(num_filters):\n fs = bin_freqs[i + np.arange(3)]\n fs = fs[1] + (fs - fs[1])\n loslope = (fftfreqs - fs[0]) / (fs[1] - fs[0])\n highslope = (fs[2] - fftfreqs) / (fs[2] - fs[1])\n fbank[i, :] = np.maximum(np.zeros(loslope.shape), np.minimum(loslope, highslope))\n return fbank.transpose()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef windowed_iterable(self):\n # Seek to offset\n effective_offset = max(0,self.item_view.iterable_index)\n for i,item in enumerate(self.iterable):\n if i=(effective_offset+self.item_view.iterable_fetch_size):\n return\n yield item", "response": "Yields the items in the iterable that are in the window."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrefreshes the items of the pattern. This method destroys the old items and creates and initializes the new items.", "response": "def refresh_items(self):\n \"\"\" Refresh the items of the pattern.\n\n This method destroys the old items and creates and initializes\n the new items.\n\n \"\"\"\n old_items = self.items[:]# if self._dirty else []\n old_iter_data = self._iter_data# if self._dirty else {}\n iterable = self.windowed_iterable\n pattern_nodes = self.pattern_nodes\n new_iter_data = sortedmap()\n new_items = []\n\n if iterable is not None and len(pattern_nodes) > 0:\n for loop_index, loop_item in enumerate(iterable):\n iteration = old_iter_data.get(loop_item)\n if iteration is not None:\n new_iter_data[loop_item] = iteration\n new_items.append(iteration)\n old_items.remove(iteration)\n continue\n iteration = []\n new_iter_data[loop_item] = iteration\n new_items.append(iteration)\n for nodes, key, f_locals in pattern_nodes:\n with new_scope(key, f_locals) as f_locals:\n f_locals['loop_index'] = loop_index\n f_locals['loop_item'] = loop_item\n for node in nodes:\n child = node(None)\n if isinstance(child, list):\n iteration.extend(child)\n else:\n iteration.append(child)\n \n # Add to old items list\n #self.old_items.extend(old_items)\n \n #if self._dirty:\n for iteration in old_items:\n for old in iteration:\n if not old.is_destroyed:\n old.destroy()\n\n if len(new_items) > 0:\n expanded = []\n recursive_expand(sum(new_items, []), expanded)\n self.parent.insert_children(self, expanded)\n\n self.items = new_items# if self._dirty else new_items+old_items\n self._iter_data = new_iter_data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_widget(self):\n widget = QDoubleSpinBox(self.parent_widget())\n widget.setKeyboardTracking(False)\n self.widget = widget", "response": "Create the underlying QDoubleSpinBox widget."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef initialize(self):\n super(Block, self).initialize()\n if self.block:\n self.block.parent.insert_children(self.block, self.children)", "response": "A reimplemented initializer.\n\n This method will add the include objects to the parent of the\n include and ensure that they are initialized."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the index of the resource table.", "response": "def _update_index(self):\n \"\"\" Update the reference to the index within the table \"\"\" \n d = self.declaration\n self.index = self.view.model.index(d.row, d.column)\n if self.delegate:\n self._refresh_count += 1\n timed_call(self._loading_interval, self._update_delegate)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the delegate cell widget.", "response": "def _update_delegate(self):\n \"\"\" Update the delegate cell widget. This is deferred so it\n does not get called until the user is done scrolling. \n \"\"\"\n self._refresh_count -= 1\n if self._refresh_count != 0:\n return\n try:\n delegate = self.delegate\n if not self.is_visible():\n return\n # The table destroys when it goes out of view\n # so we always have to make a new one\n delegate.create_widget()\n delegate.init_widget()\n \n # Set the index widget\n self.view.widget.setIndexWidget(self.index, delegate.widget)\n except RuntimeError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef data_changed(self, change):\n index = self.index\n if index:\n self.view.model.dataChanged.emit(index, index)", "response": "Notify the model that the data has changed in this cell!"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef GetHandle(self):\r\n ''' returns an the identifier of the GUI widget.\r\n It must be an integer\r\n '''\r\n win_id = self.winId() # this returns either an int or voitptr\r\n\r\n if \"%s\"%type(win_id) == \"\": # PySide\r\n ### with PySide, self.winId() does not return an integer\r\n if sys.platform == \"win32\":\r\n ## Be careful, this hack is py27 specific\r\n ## does not work with python31 or higher\r\n ## since the PyCObject api was changed\r\n import ctypes\r\n ctypes.pythonapi.PyCObject_AsVoidPtr.restype = ctypes.c_void_p\r\n ctypes.pythonapi.PyCObject_AsVoidPtr.argtypes = [\r\n ctypes.py_object]\r\n win_id = ctypes.pythonapi.PyCObject_AsVoidPtr(win_id)\r\n elif type(win_id) is not int: #PyQt4 or 5\r\n ## below integer cast may be required because self.winId() can\r\n ## returns a sip.voitptr according to the PyQt version used\r\n ## as well as the python version\r\n win_id = int(win_id)\r\n return win_id", "response": "Returns the handle of the most recent available entry in the GUI."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a generator that yields the set of entities that are in the same topology.", "response": "def _map_shapes_and_ancestors(self, topoTypeA, topoTypeB, topologicalEntity):\r\n '''\r\n using the same method\r\n @param topoTypeA:\r\n @param topoTypeB:\r\n @param topologicalEntity:\r\n '''\r\n topo_set = set()\r\n _map = TopTools_IndexedDataMapOfShapeListOfShape()\r\n topexp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map)\r\n results = _map.FindFromKey(topologicalEntity)\r\n if results.IsEmpty():\r\n yield None\r\n\r\n topology_iterator = TopTools_ListIteratorOfListOfShape(results)\r\n while topology_iterator.More():\r\n\r\n topo_entity = self.topoFactory[topoTypeB](topology_iterator.Value())\r\n\r\n # return the entity if not in set\r\n # to assure we're not returning entities several times\r\n if not topo_entity in topo_set:\r\n if self.ignore_orientation:\r\n unique = True\r\n for i in topo_set:\r\n if i.IsSame(topo_entity):\r\n unique = False\r\n break\r\n if unique:\r\n yield topo_entity\r\n else:\r\n yield topo_entity\r\n\r\n topo_set.add(topo_entity)\r\n topology_iterator.Next()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _number_shapes_ancestors(self, topoTypeA, topoTypeB, topologicalEntity):\r\n '''returns the number of shape ancestors\r\n If you want to know how many edges a faces has:\r\n _number_shapes_ancestors(self, TopAbs_EDGE, TopAbs_FACE, edg)\r\n will return the number of edges a faces has\r\n @param topoTypeA:\r\n @param topoTypeB:\r\n @param topologicalEntity:\r\n '''\r\n topo_set = set()\r\n _map = TopTools_IndexedDataMapOfShapeListOfShape()\r\n topexp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map)\r\n results = _map.FindFromKey(topologicalEntity)\r\n if results.IsEmpty():\r\n return None\r\n topology_iterator = TopTools_ListIteratorOfListOfShape(results)\r\n while topology_iterator.More():\r\n topo_set.add(topology_iterator.Value())\r\n topology_iterator.Next()\r\n return len(topo_set)", "response": "returns the number of shape ancestors"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef init_layout(self):\r\n for child in self.children():\r\n self.child_added(child)\r\n self.update_shape({})", "response": "Initialize the layout of the toolkit shape."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef init_widget(self):\n super(QtKeyEvent, self).init_widget()\n d = self.declaration\n widget = self.widget\n self._keyPressEvent = widget.keyPressEvent\n self._keyReleaseEvent = widget.keyReleaseEvent\n self.set_enabled(d.enabled)\n self.set_keys(d.keys)", "response": "The KeyEvent uses the parent_widget as it s widget"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_keys(self, keys):\n codes = {}\n for key in keys:\n parts = [k.strip().lower() for k in key.split(\"+\")]\n code = KEYS.get(parts[-1])\n modifier = Qt.KeyboardModifiers()\n if code is None:\n raise KeyError(\"Invalid key code '{}'\".format(key))\n if len(parts) > 1:\n for mod in parts[:-1]:\n mod_code = MODIFIERS.get(mod)\n if mod_code is None:\n raise KeyError(\"Invalid key modifier '{}'\"\n .format(mod_code))\n modifier |= mod_code\n if code not in codes:\n codes[code] = []\n codes[code].append(modifier)\n self.codes = codes", "response": "Parse all the key codes and save them in the codes attribute"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _update_proxy(self, change):\n if change['name'] == 'items':\n self._update_visible_area()\n \n super(TableView, self)._update_proxy(change)", "response": "An observer which sends state change to the proxy."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _teardown_features(self):\n features = self._features\n if not features:\n return\n if features & Feature.FocusTraversal:\n self.unhook_focus_traversal()\n if features & Feature.FocusEvents:\n self.unhook_focus_events()\n if features & Feature.DragEnabled:\n self.unhook_drag()\n if features & Feature.DropEnabled:\n self.unhook_drop()\n\n features = self._extra_features\n if features & GraphicFeature.WheelEvent:\n self.unhook_wheel()\n if features & GraphicFeature.DrawEvent:\n self.unhook_draw()", "response": "Teardowns the advanced widget feature handlers."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nhandles a custom tab focus request. This method is called when focus is being set on the proxy as a result of a user-implemented focus traversal handler. This can be reimplemented by subclasses as needed. Parameters ---------- reason : Qt.FocusReason The reason value for the focus request. Returns ------- result : bool True if focus was set, False otherwise.", "response": "def tab_focus_request(self, reason):\n \"\"\" Handle a custom tab focus request.\n\n This method is called when focus is being set on the proxy\n as a result of a user-implemented focus traversal handler.\n This can be reimplemented by subclasses as needed.\n\n Parameters\n ----------\n reason : Qt.FocusReason\n The reason value for the focus request.\n\n Returns\n -------\n result : bool\n True if focus was set, False otherwise.\n\n \"\"\"\n widget = self.focus_target()\n if not widget.focusPolicy & Qt.TabFocus:\n return False\n if not widget.isEnabled():\n return False\n if not widget.isVisibleTo(widget.window()):\n return False\n widget.setFocus(reason)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall the hooks for focus events.", "response": "def hook_focus_events(self):\n \"\"\" Install the hooks for focus events.\n\n This method may be overridden by subclasses as needed.\n\n \"\"\"\n widget = self.widget\n widget.focusInEvent = self.focusInEvent\n widget.focusOutEvent = self.focusOutEvent"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ninstall the hooks for drag operations.", "response": "def hook_drag(self):\n \"\"\" Install the hooks for drag operations.\n\n \"\"\"\n widget = self.widget\n widget.mousePressEvent = self.mousePressEvent\n widget.mouseMoveEvent = self.mouseMoveEvent\n widget.mouseReleaseEvent = self.mouseReleaseEvent"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef unhook_drag(self):\n widget = self.widget\n del widget.mousePressEvent\n del widget.mouseMoveEvent\n del widget.mouseReleaseEvent", "response": "Remove the hooks for drag operations."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef mousePressEvent(self, event):\n if event.button() == Qt.LeftButton:\n self._drag_origin = event.pos()\n widget = self.widget\n type(widget).mousePressEvent(widget, event)", "response": "Handle the mouse press event for a drag operation."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nhandles the mouse move event for a drag operation.", "response": "def mouseMoveEvent(self, event):\n \"\"\" Handle the mouse move event for a drag operation.\n\n \"\"\"\n #if event.buttons() & Qt.LeftButton and self._drag_origin is not None:\n #dist = (event.pos() - self._drag_origin).manhattanLength()\n #if dist >= QApplication.startDragDistance():\n #self.do_drag(event.widget())\n #self._drag_origin = None\n #return # Don't returns\n widget = self.widget\n type(widget).mouseMoveEvent(widget, event)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling the mouse release event for the drag operation.", "response": "def mouseReleaseEvent(self, event):\n \"\"\" Handle the mouse release event for the drag operation.\n\n \"\"\"\n if event.button() == Qt.LeftButton:\n self._drag_origin = None\n widget = self.widget\n type(widget).mouseReleaseEvent(widget, event)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall hooks for drop operations.", "response": "def hook_drop(self):\n \"\"\" Install hooks for drop operations.\n\n \"\"\"\n widget = self.widget\n widget.setAcceptDrops(True)\n widget.dragEnterEvent = self.dragEnterEvent\n widget.dragMoveEvent = self.dragMoveEvent\n widget.dragLeaveEvent = self.dragLeaveEvent\n widget.dropEvent = self.dropEvent"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef unhook_drop(self):\n widget = self.widget\n widget.setAcceptDrops(False)\n del widget.dragEnterEvent\n del widget.dragMoveEvent\n del widget.dragLeaveEvent\n del widget.dropEvent", "response": "Remove hooks for drop operations."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nperforming the drag operation for the current window.", "response": "def do_drag(self, widget):\n \"\"\" Perform the drag operation for the widget.\n \n Parameters\n ----------\n widget: QWidget\n A reference to the viewport widget.\n\n \"\"\"\n drag_data = self.declaration.drag_start()\n if drag_data is None:\n return\n #widget = self.widget\n qdrag = QDrag(widget)\n qdrag.setMimeData(drag_data.mime_data.q_data())\n if drag_data.image is not None:\n qimg = get_cached_qimage(drag_data.image)\n qdrag.setPixmap(QPixmap.fromImage(qimg))\n #else:\n #if __version_info__ < (5, ):\n #qdrag.setPixmap(QPixmap.grabWidget(self.widget))\n #else:\n #qdrag.setPixmap(widget.grab())\n if drag_data.hotspot:\n qdrag.setHotSpot(QPoint(*drag_data.hotspot))\n else:\n cursor_position = widget.mapFromGlobal(QCursor.pos())\n qdrag.setHotSpot(cursor_position)\n default = Qt.DropAction(drag_data.default_drop_action)\n supported = Qt.DropActions(drag_data.supported_actions)\n qresult = qdrag.exec_(supported, default)\n self.declaration.drag_end(drag_data, DropAction(int(qresult)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef draw(self, painter, options, widget):\n self.declaration.draw(painter, options, widget)", "response": "Handle the draw event for the\n widget."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the shared widget action for this widget.", "response": "def get_action(self, create=False):\n \"\"\" Get the shared widget action for this widget.\n\n This API is used to support widgets in tool bars and menus.\n\n Parameters\n ----------\n create : bool, optional\n Whether to create the action if it doesn't already exist.\n The default is False.\n\n Returns\n -------\n result : QWidgetAction or None\n The cached widget action or None, depending on arguments.\n\n \"\"\"\n action = self._widget_action\n if action is None and create:\n action = self._widget_action = QWidgetAction(None)\n action.setDefaultWidget(self.widget)\n return action"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mousePressEvent(self, event):\n self.declaration.mouse_press_event(event)\n super(QtGraphicsView, self).mousePressEvent(event)", "response": "Handle mouse press events for a drag operation."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef mouseMoveEvent(self, event):\n self.declaration.mouse_move_event(event)\n super(QtGraphicsView, self).mouseMoveEvent(event)", "response": "Handle the mouse move event for a drag operation."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef mouseReleaseEvent(self, event):\n self.declaration.mouse_release_event(event)\n super(QtGraphicsView, self).mouseReleaseEvent(event)", "response": "Handle the mouse release event for the drag operation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the viewport widget.", "response": "def set_renderer(self, renderer):\n \"\"\" Set the viewport widget. \n \n \"\"\"\n viewport = None\n if renderer == 'opengl':\n from enaml.qt.QtWidgets import QOpenGLWidget\n viewport = QOpenGLWidget()\n elif renderer == 'default':\n try:\n from enaml.qt.QtWidgets import QOpenGLWidget\n viewport = QOpenGLWidget()\n except ImportError as e:\n warnings.warn(\n \"QOpenGLWidget could not be imported: {}\".format(e))\n self.widget.setViewport(viewport)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef on_selection_changed(self):\n d = self.declaration\n selection = self.scene.selectedItems()\n self._guards |= 0x01\n try:\n d.selected_items = [item.ref().declaration for item in selection\n if item.ref()]\n finally:\n self._guards &= ~0x01", "response": "Callback invoked one the selection has changed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_background(self, background):\n scene = self.scene\n scene.setBackgroundBrush(QColor.fromRgba(background.argb))", "response": "Set the background color of the current object."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nscales the zoom but keep in the min and max zoom bounds.", "response": "def scale_view(self, x, y):\n \"\"\" Scale the zoom but keep in in the min and max zoom bounds.\n\n \"\"\"\n d = self.declaration\n factor = self.widget.transform().scale(x, y).mapRect(\n QRectF(0, 0, 1, 1)).width()\n if (d.min_zoom > factor > d.max_zoom):\n return\n self.widget.scale(x, y)\n return factor"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef destroy(self):\n self._teardown_features()\n focus_registry.unregister(self.widget)\n widget = self.widget\n if widget is not None:\n del self.widget\n super(QtGraphicsItem, self).destroy()\n # If a QWidgetAction was created for this widget, then it has\n # taken ownership of the widget and the widget will be deleted\n # when the QWidgetAction is garbage collected. This means the\n # superclass destroy() method must run before the reference to\n # the QWidgetAction is dropped.\n del self._widget_action", "response": "Destroys the underlying QWidget object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parent_widget(self):\n parent = self.parent()\n if parent is not None and isinstance(parent, QtGraphicsItem):\n return parent.widget", "response": "Reimplemented to only return GraphicsItems"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init_layout(self):\n self.widget = QGraphicsProxyWidget(self.parent_widget())\n widget = self.widget\n for item in self.child_widgets():\n widget.setWidget(item)\n break\n super(QtGraphicsWidget, self).init_widget()\n super(QtGraphicsWidget, self).init_layout()", "response": "Create the widget in the layout pass after the child widget has been created and intialized."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _dequeue_update(self,change):\r\n self._update_count -=1\r\n if self._update_count !=0:\r\n return\r\n self.update_shape(change)", "response": "Dequeue an update for the specified duplicate."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates the toolkit shape for the object.", "response": "def create_shape(self):\r\n \"\"\" Create the toolkit shape for the proxy object.\r\n\r\n This method is called during the top-down pass, just before the\r\n 'init_shape()' method is called. This method should create the\r\n toolkit widget and assign it to the 'widget' attribute.\r\n\r\n \"\"\"\r\n d = self.declaration\r\n if d.shape1 and d.shape2:\r\n self.shape = self._do_operation(d.shape1, d.shape2)\r\n else:\r\n self.shape = None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures the widget is shown.", "response": "def show(self):\n \"\"\" Ensure the widget is shown.\n Calling this method will also set the widget visibility to True.\n \"\"\"\n self.visible = True\n if self.proxy_is_active:\n self.proxy.ensure_visible()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef hide(self):\n self.visible = False\n if self.proxy_is_active:\n self.proxy.ensure_hidden()", "response": "Ensure the widget is hidden."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the items at the given position", "response": "def get_item_at(self, *args, **kwargs):\n \"\"\" Return the items at the given position \"\"\"\n return self.proxy.get_item_at(coerce_point(*args, **kwargs))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncenters on the given item or point.", "response": "def center_on(self, item):\n \"\"\" Center on the given item or point. \"\"\"\n if not isinstance(item, GraphicsItem):\n item = coerce_point(item)\n self.proxy.center_on(item)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _custom_token_stream(self):\n token_stream = default_make_token_stream(self)\n for processor in _token_stream_processors:\n token_stream = processor(token_stream)\n return token_stream", "response": "A wrapper for the BaseEnamlLexer s make_token_stream which allows the token stream to be customized by adding the token_stream_processors list to the token_stream."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef convert_enamldef_def_to_func(token_stream):\n in_enamldef = False\n depth = 0\n for tok in token_stream:\n if tok.type == 'ENAMLDEF':\n in_enamldef = True\n elif tok.type == 'INDENT':\n depth += 1\n elif in_enamldef and tok.type == 'DEF':\n # Since functions are not allowed on the RHS we can\n # transform the token type to a NAME so it's picked up by the\n # parser as a decl_funcdef instead of funcdef\n tok.type = 'NAME'\n tok.value = 'func'\n elif tok.type == 'DEDENT':\n depth -= 1\n if depth == 0:\n in_enamldef = False\n yield tok", "response": "A token stream processor which processes all enaml declarative functions \n to allow using def instead of func."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef child_added(self, child):\n super(AbstractItemView, self).child_added(child)\n self.get_member('_items').reset(self)", "response": "Reset the item cache when a child is added"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresetting the item cache when a child is removed", "response": "def child_removed(self, child):\n \"\"\" Reset the item cache when a child is removed \"\"\"\n super(AbstractItemView, self).child_removed(child)\n self.get_member('_items').reset(self)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef child_added(self, child):\n super(AbstractWidgetItemGroup, self).child_added(child)\n self.get_member('_items').reset(self)", "response": "Reset the item cache when a child is added"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef child_removed(self, child):\n super(AbstractWidgetItemGroup, self).child_removed(child)\n self.get_member('_items').reset(self)", "response": "Reset the item cache when a child is removed"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _update_proxy(self, change):\n if change['name'] in ['row', 'column']:\n super(AbstractWidgetItem, self)._update_proxy(change)\n else:\n self.proxy.data_changed(change)", "response": "An observer which sends state change to the proxy."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef init_signal(self):\r\n signal.signal(signal.SIGINT, lambda sig, frame: self.exit(-2))\r\n # need a timer, so that QApplication doesn't block until a real\r\n # Qt event fires (can require mouse movement)\r\n # timer trick from http://stackoverflow.com/q/4938723/938949\r\n timer = QtCore.QTimer()\r\n # Let the interpreter run each 200 ms:\r\n timer.timeout.connect(lambda: None)\r\n timer.start(200)\r\n # hold onto ref, so the timer doesn't get cleaned up\r\n self._sigint_timer = timer", "response": "allow clean shutdown on sigint"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _update_position(self, change):\r\n if change['type']!='update':\r\n return\r\n pt = gp_Pnt(self.x,self.y,self.z)\r\n if not pt.IsEqual(self.position,self.tolerance):\r\n self.position = pt", "response": "Update the position of the object based on the current values of the attributes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _update_xyz(self, change):\r\n self.x,self.y,self.z = self.position.X(),self.position.Y(),self.position.Z()", "response": "Update x y z values based on the current position"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _update_state(self, change):\r\n self._block_updates = True\r\n try:\r\n self.position = self.axis.Location()\r\n self.direction = self.axis.Direction()\r\n finally:\r\n self._block_updates = False", "response": "Update position and direction of the current object with the new values."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nrefreshes the multi - axis of the plot.", "response": "def _refresh_multi_axis(self):\n \"\"\" If linked axis' are used, setup and link them \"\"\"\n d = self.declaration\n \n #: Create a separate viewbox\n self.viewbox = pg.ViewBox()\n \n #: If this is the first nested plot, use the parent right axis\n _plots = [c for c in self.parent().children() if isinstance(c,AbstractQtPlotItem)]\n i = _plots.index(self)\n if i==0:\n self.axis = self.widget.getAxis('right') \n self.widget.showAxis('right')\n else:\n self.axis = pg.AxisItem('right')\n self.axis.setZValue(-10000)\n \n #: Add new axis to scene\n self.widget.layout.addItem(self.axis,2,i+2)\n \n #: Link x axis to the parent axis\n self.viewbox.setXLink(self.widget.vb)\n \n #: Link y axis to the view\n self.axis.linkToView(self.viewbox)\n \n #: Set axis label\n self.axis.setLabel(d.label_right)\n \n #: Add Viewbox to parent scene\n self.parent().parent_widget().scene().addItem(self.viewbox)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_range_x(self,val):\n d = self.declaration\n if d.auto_range[0]:\n return\n self.widget.setXRange(*val,padding=0)", "response": "Set visible range of x data."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets visible range of y data.", "response": "def set_range_y(self,val):\n \"\"\" Set visible range of y data. \n \n Note: Padding must be 0 or it will create an infinite loop\n \n \"\"\"\n d = self.declaration\n if d.auto_range[1]:\n return\n self.widget.setYRange(*val,padding=0)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of child TreeViewColumns including this item as the first column", "response": "def _get_columns(self):\n \"\"\" List of child TreeViewColumns including \n this item as the first column\n \"\"\"\n return [self] + [c for c in self.children\n if isinstance(c, TreeViewColumn)]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the row and column numbers of child items.", "response": "def _update_rows(self):\n \"\"\" Update the row and column numbers of child items. \"\"\"\n for row, item in enumerate(self._items):\n item.row = row # Row is the Parent item\n item.column = 0\n \n for column, item in enumerate(self._columns):\n item.row = self.row # Row is the Parent item\n item.column = column"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef setDeclaration(self, declaration):\n assert isinstance(declaration.proxy, ProxyAbstractItemView), \\\n \"The model declaration must be a QtAbstractItemView subclass. \" \\\n \"Got {]\".format(declaration)\n\n self.declaration = declaration", "response": "Set the declaration this model will use for rendering the headers. \n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nretrieving the data for the item at the given index", "response": "def data(self, index, role):\n \"\"\" Retrieve the data for the item at the given index\n \"\"\"\n item = self.itemAt(index)\n if not item:\n return None\n d = item.declaration\n if role == Qt.DisplayRole:\n return d.text\n elif role == Qt.ToolTipRole:\n return d.tool_tip\n elif role == Qt.CheckStateRole and d.checkable:\n return d.checked and Qt.Checked or Qt.Unchecked\n elif role == Qt.DecorationRole and d.icon:\n return get_cached_qicon(d.icon)\n elif role == Qt.EditRole and d.editable:\n return d.text\n elif role == Qt.StatusTipRole:\n return d.status_tip\n elif role == Qt.TextAlignmentRole:\n h, v = d.text_alignment\n return TEXT_H_ALIGNMENTS[h] | TEXT_V_ALIGNMENTS[v]\n elif role == Qt.ForegroundRole and d.foreground:\n return get_cached_qcolor(d.foreground)\n elif role == Qt.BackgroundRole and d.background:\n return get_cached_qcolor(d.background)\n #elif role == Qt.SizeHintRole and d.minimum_size:\n # return d.minimum_size\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef setData(self, index, value, role=Qt.EditRole):\n item = self.itemAt(index)\n if not item:\n return False\n d = item.declaration\n if role == Qt.CheckStateRole:\n checked = value == Qt.Checked\n if checked != d.checked:\n d.checked = checked\n d.toggled(checked)\n return True\n elif role == Qt.EditRole:\n if value != d.text:\n d.text = value\n return True\n return super(QAbstractAtomItemModel, self).setData(index, value, role)", "response": "Set the data for the item at the given index to the given value."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the data for the item with the given orientation and role.", "response": "def headerData(self, index, orientation, role):\n \"\"\" QHeaderView respects the following item data roles: \n TextAlignmentRole, \n DisplayRole, \n FontRole, \n DecorationRole, \n ForegroundRole, \n BackgroundRole.\n \"\"\"\n d = self.declaration\n if orientation == Qt.Horizontal and role == Qt.DisplayRole:\n try:\n return d.horizontal_headers[index] \\\n if d.horizontal_headers else index\n except IndexError:\n return index\n elif orientation == Qt.Vertical and role == Qt.DisplayRole:\n try:\n return d.vertical_headers[index] \\\n if d.vertical_headers else index\n except IndexError:\n return index\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninitialize the signals for the current locale.", "response": "def init_signals(self):\n \"\"\" Connect signals \"\"\"\n self.widget.activated.connect(self.on_item_activated)\n self.widget.clicked.connect(self.on_item_clicked)\n self.widget.doubleClicked.connect(self.on_item_double_clicked)\n self.widget.entered.connect(self.on_item_entered)\n self.widget.pressed.connect(self.on_item_pressed)\n self.widget.customContextMenuRequested.connect(\n self.on_custom_context_menu_requested)\n self.selection_model = self.widget.selectionModel()\n self.selection_model.selectionChanged.connect(\n self.on_selection_changed)\n self.widget.horizontalScrollBar().valueChanged.connect(\n self.on_horizontal_scrollbar_moved)\n self.widget.verticalScrollBar().valueChanged.connect(\n self.on_vertical_scrollbar_moved)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_items(self, items):\n self._pending_view_refreshes +=1\n timed_call(self._pending_timeout, self._refresh_layout)", "response": "Set the items of the view."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _refresh_layout(self):\n self._pending_view_refreshes -= 1\n if self._pending_view_refreshes == 0:\n try:\n self.model.layoutChanged.emit()\n self.on_layout_refreshed()\n except RuntimeError:\n # View can be destroyed before we get here\n return\n self._refresh_sizes()", "response": "Refreshes the layout of the items in the items table."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef index(self, row, column, parent):\n item = parent.internalPointer()\n #: If the parent is None\n d = self.declaration if item is None else item.declaration\n if row < len(d._items):\n proxy = d._items[row].proxy\n assert isinstance(proxy, QtTreeViewItem), \\\n \"Invalid item {}\".format(proxy)\n else:\n proxy = d.proxy\n return self.createIndex(row, column, proxy)", "response": "Creates an index for the given row column and parent."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _default_view(self):\n parent = self.parent()\n if isinstance(parent, QtTreeView):\n return parent\n return parent.view", "response": "Return the default view of the item."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef search(cls, five9, filters):\n return cls._name_search(five9.configuration.getWebConnectors, filters)", "response": "Search for a record on the remote and return the results."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef read(cls, five9, external_id):\n results = cls.search(five9, {cls.__uid_field__: external_id})\n if not results:\n return None\n return results[0]", "response": "Return a record singleton for the ID."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the current memory record with the given data dictionary.", "response": "def update(self, data):\n \"\"\"Update the current memory record with the given data dict.\n\n Args:\n data (dict): Data dictionary to update the record attributes with.\n \"\"\"\n for key, value in data.items():\n setattr(self, key, value)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalls the remote method with data and optionally refresh the record.", "response": "def _call_and_serialize(cls, method, data, refresh=False):\n \"\"\"Call the remote method with data, and optionally refresh.\n\n Args:\n method (callable): The method on the Authenticated Five9 object\n that should be called.\n data (dict): A data dictionary that will be passed as the first\n and only position argument to ``method``.\n refresh (bool, optional): Set to ``True`` to get the record data\n from Five9 before returning the record.\n\n Returns:\n BaseModel: The newly created record. If ``refresh`` is ``True``,\n this will be fetched from Five9. Otherwise, it's the data\n record that was sent to the server.\n \"\"\"\n method(data)\n if refresh:\n return cls.read(method.__self__, data[cls.__uid_field__])\n else:\n return cls.deserialize(cls._get_non_empty_dict(data))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_name_filters(cls, filters):\n filters = filters.get(cls.__uid_field__)\n if not filters:\n filters = '.*'\n elif not isinstance(filters, string_types):\n filters = r'(%s)' % ('|'.join(filters))\n return filters", "response": "Return a regex filter for the UID column only."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_non_empty_dict(cls, mapping):\n res = {}\n for key, value in mapping.items():\n if hasattr(value, 'items'):\n value = cls._get_non_empty_dict(value)\n elif isinstance(value, list):\n value = cls._get_non_empty_list(value)\n if value not in [[], {}, None]:\n res[key] = value\n return res", "response": "Return the mapping without any None values ( recursive )."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of the input excluding all None values.", "response": "def _get_non_empty_list(cls, iter):\n \"\"\"Return a list of the input, excluding all ``None`` values.\"\"\"\n res = []\n for value in iter:\n if hasattr(value, 'items'):\n value = cls._get_non_empty_dict(value) or None\n if value is not None:\n res.append(value)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _name_search(cls, method, filters):\n filters = cls._get_name_filters(filters)\n return [\n cls.deserialize(cls._zeep_to_dict(row)) for row in method(filters)\n ]", "response": "Helper for search methods that use name filters."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a zeep object to a dictionary.", "response": "def _zeep_to_dict(cls, obj):\n \"\"\"Convert a zeep object to a dictionary.\"\"\"\n res = serialize_object(obj)\n res = cls._get_non_empty_dict(res)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __check_field(self, key):\n if not self._props.get(key):\n raise KeyError(\n 'The field \"%s\" does not exist on \"%s\"' % (\n key, self.__class__.__name__,\n ),\n )", "response": "Raises a KeyError if the field doesn t exist on the object."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn an authenticated connection for use open new if required.", "response": "def supervisor(self):\n \"\"\"Return an authenticated connection for use, open new if required.\n\n Returns:\n SupervisorWebService: New or existing session with the Five9\n Statistics API.\n \"\"\"\n supervisor = self._cached_client('supervisor')\n if not self._api_supervisor_session:\n self._api_supervisor_session = self.__create_supervisor_session(\n supervisor,\n )\n return supervisor"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_mapping(record, keys):\n\n ordered = OrderedDict()\n field_mappings = []\n\n for key, value in record.items():\n ordered[key] = value\n field_mappings.append({\n 'columnNumber': len(ordered), # Five9 is not zero indexed.\n 'fieldName': key,\n 'key': key in keys,\n })\n\n return {\n 'field_mappings': field_mappings,\n 'data': ordered,\n 'fields': list(ordered.values()),\n }", "response": "Create a field mapping for use in API updates and creates."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_response(fields, records):\n data = [i['values']['data'] for i in records]\n return [\n {fields[idx]: row for idx, row in enumerate(d)}\n for d in data\n ]", "response": "Parse an API response into usable objects."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_criteria(cls, query):\n criteria = []\n for name, value in query.items():\n if isinstance(value, list):\n for inner_value in value:\n criteria += cls.create_criteria({name: inner_value})\n else:\n criteria.append({\n 'criteria': {\n 'field': name,\n 'value': value,\n },\n })\n return criteria or None", "response": "Create a criteria from a dictionary containing a query."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning an authenticated SOAP client.", "response": "def _get_authenticated_client(self, wsdl):\n \"\"\"Return an authenticated SOAP client.\n\n Returns:\n zeep.Client: Authenticated API client.\n \"\"\"\n return zeep.Client(\n wsdl % quote(self.username),\n transport=zeep.Transport(\n session=self._get_authenticated_session(),\n ),\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn an authenticated requests session.", "response": "def _get_authenticated_session(self):\n \"\"\"Return an authenticated requests session.\n\n Returns:\n requests.Session: Authenticated session for use.\n \"\"\"\n session = requests.Session()\n session.auth = self.auth\n return session"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __create_supervisor_session(self, supervisor):\n session_params = {\n 'forceLogoutSession': self.force_logout_session,\n 'rollingPeriod': self.rolling_period,\n 'statisticsRange': self.statistics_range,\n 'shiftStart': self.__to_milliseconds(\n self.shift_start_hour,\n ),\n 'timeZone': self.__to_milliseconds(\n self.time_zone_offset,\n ),\n }\n supervisor.setSessionParameters(session_params)\n return session_params", "response": "Create a new supervisor session on the supervisor service."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nuse this to decorate methods that expect a model.", "response": "def model(method):\n \"\"\"Use this to decorate methods that expect a model.\"\"\"\n def wrapper(self, *args, **kwargs):\n if self.__model__ is None:\n raise ValidationError(\n 'You cannot perform CRUD operations without selecting a '\n 'model first.',\n )\n return method(self, *args, **kwargs)\n return wrapper"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nuse this to decorate methods that expect a record set.", "response": "def recordset(method):\n \"\"\"Use this to decorate methods that expect a record set.\"\"\"\n def wrapper(self, *args, **kwargs):\n if self.__records__ is None:\n raise ValidationError(\n 'There are no records in the set.',\n )\n return method(self, *args, **kwargs)\n return Api.model(wrapper)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate the data on the remote optionally refreshing.", "response": "def create(self, data, refresh=False):\n \"\"\"Create the data on the remote, optionally refreshing.\"\"\"\n self.__model__.create(self.__five9__, data)\n if refresh:\n return self.read(data[self.__model__.__name__])\n else:\n return self.new(data)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef new(self, data):\n data = self.__model__._get_non_empty_dict(data)\n return self.__class__(\n self.__five9__,\n self.__model__,\n records=[self.__model__.deserialize(data)],\n )", "response": "Create a new memory record but do not create on the remote."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef search(self, filters):\n records = self.__model__.search(self.__five9__, filters)\n return self.__class__(\n self.__five9__, self.__model__, records,\n )", "response": "Search Five9 given a filter."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create(cls, five9, data, refresh=False):\n return cls._call_and_serialize(\n five9.configuration.createDisposition, data, refresh,\n )", "response": "Create a record on Five9."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsearches for a record on the remote and return the results.", "response": "def search(cls, five9, filters):\n \"\"\"Search for a record on the remote and return the results.\n\n Args:\n five9 (five9.Five9): The authenticated Five9 remote.\n filters (dict): A dictionary of search parameters, keyed by the\n name of the field to search. This should conform to the\n schema defined in :func:`five9.Five9.create_criteria`.\n\n Returns:\n list[BaseModel]: A list of records representing the result.\n \"\"\"\n return cls._name_search(five9.configuration.getDispositions, filters)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef authentication_required(meth):\n\n def check(cls, *args, **kwargs):\n if cls.authenticated:\n return meth(cls, *args, **kwargs)\n raise Error(\"Authentication required\")\n\n return check", "response": "Simple class method decorator that checks if the client is currently connected."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreading a block of size bytes from the server.", "response": "def __read_block(self, size):\n \"\"\"Read a block of 'size' bytes from the server.\n\n An internal buffer is used to read data from the server. If\n enough data is available from it, we return that data.\n\n Eventually, we try to grab the missing part from the server\n for Client.read_timeout seconds. If no data can be\n retrieved, it is considered as a fatal error and an 'Error'\n exception is raised.\n\n :param size: number of bytes to read\n :rtype: string\n :returns: the read block (can be empty)\n \"\"\"\n buf = b\"\"\n if len(self.__read_buffer):\n limit = (\n size if size <= len(self.__read_buffer) else\n len(self.__read_buffer)\n )\n buf = self.__read_buffer[:limit]\n self.__read_buffer = self.__read_buffer[limit:]\n size -= limit\n if not size:\n return buf\n try:\n buf += self.sock.recv(size)\n except (socket.timeout, ssl.SSLError):\n raise Error(\"Failed to read %d bytes from the server\" % size)\n self.__dprint(buf)\n return buf"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __read_line(self):\n ret = b\"\"\n while True:\n try:\n pos = self.__read_buffer.index(CRLF)\n ret = self.__read_buffer[:pos]\n self.__read_buffer = self.__read_buffer[pos + len(CRLF):]\n break\n except ValueError:\n pass\n try:\n nval = self.sock.recv(self.read_size)\n self.__dprint(nval)\n if not len(nval):\n break\n self.__read_buffer += nval\n except (socket.timeout, ssl.SSLError):\n raise Error(\"Failed to read data from the server\")\n\n if len(ret):\n m = self.__size_expr.match(ret)\n if m:\n raise Literal(int(m.group(1)))\n\n m = self.__respcode_expr.match(ret)\n if m:\n if m.group(1) == b\"BYE\":\n raise Error(\"Connection closed by server\")\n if m.group(1) == b\"NO\":\n self.__parse_error(m.group(2))\n raise Response(m.group(1), m.group(2))\n return ret", "response": "Read one line from the server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __read_response(self, nblines=-1):\n resp, code, data = (b\"\", None, None)\n cpt = 0\n while True:\n try:\n line = self.__read_line()\n except Response as inst:\n code = inst.code\n data = inst.data\n break\n except Literal as inst:\n resp += self.__read_block(inst.value)\n if not resp.endswith(CRLF):\n resp += self.__read_line() + CRLF\n continue\n if not len(line):\n continue\n resp += line + CRLF\n cpt += 1\n if nblines != -1 and cpt == nblines:\n break\n\n return (code, data, resp)", "response": "Read a response from the server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __prepare_args(self, args):\n ret = []\n for a in args:\n if isinstance(a, six.binary_type):\n if self.__size_expr.match(a):\n ret += [a]\n else:\n ret += [b'\"' + a + b'\"']\n continue\n ret += [bytes(str(a).encode(\"utf-8\"))]\n return ret", "response": "Format command arguments before sending them."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend a command to the server.", "response": "def __send_command(\n self, name, args=None, withcontent=False, extralines=None,\n nblines=-1):\n \"\"\"Send a command to the server.\n\n If args is not empty, we concatenate the given command with\n the content of this list. If extralines is not empty, they are\n sent one by one to the server. (CLRF are automatically\n appended to them)\n\n We wait for a response just after the command has been sent.\n\n :param name: the command to sent\n :param args: a list of arguments for this command\n :param withcontent: tells the function to return the server's response\n or not\n :param extralines: a list of extra lines to sent after the command\n :param nblines: the number of response lines to read (all by default)\n\n :returns: a tuple of the form (code, data[, response])\n\n \"\"\"\n tosend = name.encode(\"utf-8\")\n if args:\n tosend += b\" \" + b\" \".join(self.__prepare_args(args))\n self.__dprint(b\"Command: \" + tosend)\n self.sock.sendall(tosend + CRLF)\n if extralines:\n for l in extralines:\n self.sock.sendall(l + CRLF)\n code, data, content = self.__read_response(nblines)\n\n if isinstance(code, six.binary_type):\n code = code.decode(\"utf-8\")\n if isinstance(data, six.binary_type):\n data = data.decode(\"utf-8\")\n\n if withcontent:\n return (code, data, content)\n return (code, data)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing an error received from the server.", "response": "def __parse_error(self, text):\n \"\"\"Parse an error received from the server.\n\n if text corresponds to a size indication, we grab the\n remaining content from the server.\n\n Otherwise, we try to match an error of the form \\(\\w+\\)?\\s*\".+\"\n\n On succes, the two public members errcode and errmsg are\n filled with the parsing results.\n\n :param text: the response to parse\n \"\"\"\n m = self.__size_expr.match(text)\n if m is not None:\n self.errcode = b\"\"\n self.errmsg = self.__read_block(int(m.group(1)) + 2)\n return\n\n m = self.__error_expr.match(text)\n if m is None:\n raise Error(\"Bad error message\")\n if m.group(1) is not None:\n self.errcode = m.group(1).strip(b\"()\")\n else:\n self.errcode = b\"\"\n self.errmsg = m.group(2).strip(b'\"')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _digest_md5_authentication(self, login, password, authz_id=\"\"):\n code, data, challenge = \\\n self.__send_command(\"AUTHENTICATE\", [b\"DIGEST-MD5\"],\n withcontent=True, nblines=1)\n dmd5 = DigestMD5(challenge, \"sieve/%s\" % self.srvaddr)\n\n code, data, challenge = self.__send_command(\n '\"%s\"' % dmd5.response(login, password, authz_id),\n withcontent=True, nblines=1\n )\n if not challenge:\n return False\n if not dmd5.check_last_challenge(login, password, challenge):\n self.errmsg = \"Bad challenge received from server\"\n return False\n code, data = self.__send_command('\"\"')\n if code == \"OK\":\n return True\n return False", "response": "SASL DIGEST - MD5 authentication"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nauthenticate the user with the specified authentication mechanism.", "response": "def __authenticate(self, login, password, authz_id=b\"\", authmech=None):\n \"\"\"AUTHENTICATE command\n\n Actually, it is just a wrapper to the real commands (one by\n mechanism). We try all supported mechanisms (from the\n strongest to the weakest) until we find one supported by the\n server.\n\n Then we try to authenticate (only once).\n\n :param login: username\n :param password: clear password\n :param authz_id: authorization ID\n :param authmech: prefered authentication mechanism\n :return: True on success, False otherwise\n \"\"\"\n if \"SASL\" not in self.__capabilities:\n raise Error(\"SASL not supported by the server\")\n srv_mechanisms = self.get_sasl_mechanisms()\n\n if authmech is None or authmech not in SUPPORTED_AUTH_MECHS:\n mech_list = SUPPORTED_AUTH_MECHS\n else:\n mech_list = [authmech]\n\n for mech in mech_list:\n if mech not in srv_mechanisms:\n continue\n mech = mech.lower().replace(\"-\", \"_\")\n auth_method = getattr(self, \"_%s_authentication\" % mech)\n if auth_method(login, password, authz_id):\n self.authenticated = True\n return True\n return False\n\n self.errmsg = b\"No suitable mechanism found\"\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __starttls(self, keyfile=None, certfile=None):\n if not self.has_tls_support():\n raise Error(\"STARTTLS not supported by the server\")\n code, data = self.__send_command(\"STARTTLS\")\n if code != \"OK\":\n return False\n try:\n nsock = ssl.wrap_socket(self.sock, keyfile, certfile)\n except ssl.SSLError as e:\n raise Error(\"SSL error: %s\" % str(e))\n self.sock = nsock\n self.__capabilities = {}\n self.__get_capabilities()\n return True", "response": "This method is used to start TLS on the server."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_sieve_capabilities(self):\n if isinstance(self.__capabilities[\"SIEVE\"], six.string_types):\n self.__capabilities[\"SIEVE\"] = self.__capabilities[\"SIEVE\"].split()\n return self.__capabilities[\"SIEVE\"]", "response": "Returns the SIEVE extensions supported by the server."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef connect(\n self, login, password, authz_id=b\"\", starttls=False,\n authmech=None):\n \"\"\"Establish a connection with the server.\n\n This function must be used. It read the server capabilities\n and wraps calls to STARTTLS and AUTHENTICATE commands.\n\n :param login: username\n :param password: clear password\n :param starttls: use a TLS connection or not\n :param authmech: prefered authenticate mechanism\n :rtype: boolean\n \"\"\"\n try:\n self.sock = socket.create_connection((self.srvaddr, self.srvport))\n self.sock.settimeout(Client.read_timeout)\n except socket.error as msg:\n raise Error(\"Connection to server failed: %s\" % str(msg))\n\n if not self.__get_capabilities():\n raise Error(\"Failed to read capabilities from server\")\n if starttls and not self.__starttls():\n return False\n if self.__authenticate(login, password, authz_id, authmech):\n return True\n return False", "response": "Establish a connection with the server."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef capability(self):\n code, data, capabilities = (\n self.__send_command(\"CAPABILITY\", withcontent=True))\n if code == \"OK\":\n return capabilities\n return None", "response": "Ask server capabilities.\n\n See MANAGESIEVE specifications, section 2.4 This command does\n not affect capabilities recorded by this client.\n\n :rtype: string"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nasking for available space.", "response": "def havespace(self, scriptname, scriptsize):\n \"\"\"Ask for available space.\n\n See MANAGESIEVE specifications, section 2.5\n\n :param scriptname: script's name\n :param scriptsize: script's size\n :rtype: boolean\n \"\"\"\n code, data = self.__send_command(\n \"HAVESPACE\", [scriptname.encode(\"utf-8\"), scriptsize])\n if code == \"OK\":\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist available scripts. See MANAGESIEVE specifications, section 2.7 :returns: a 2-uple (active script, [script1, ...])", "response": "def listscripts(self):\n \"\"\"List available scripts.\n\n See MANAGESIEVE specifications, section 2.7\n\n :returns: a 2-uple (active script, [script1, ...])\n \"\"\"\n code, data, listing = self.__send_command(\n \"LISTSCRIPTS\", withcontent=True)\n if code == \"NO\":\n return None\n ret = []\n active_script = None\n for l in listing.splitlines():\n if self.__size_expr.match(l):\n continue\n m = re.match(br'\"([^\"]+)\"\\s*(.+)', l)\n if m is None:\n ret += [l.strip(b'\"').decode(\"utf-8\")]\n continue\n script = m.group(1).decode(\"utf-8\")\n if self.__active_expr.match(m.group(2)):\n active_script = script\n continue\n ret += [script]\n self.__dprint(ret)\n return (active_script, ret)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef getscript(self, name):\n code, data, content = self.__send_command(\n \"GETSCRIPT\", [name.encode(\"utf-8\")], withcontent=True)\n if code == \"OK\":\n lines = content.splitlines()\n if self.__size_expr.match(lines[0]) is not None:\n lines = lines[1:]\n return u\"\\n\".join([line.decode(\"utf-8\") for line in lines])\n return None", "response": "Download a script from the server"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupload a script to the server See MANAGESIEVE specifications, section 2.6 :param name: script's name :param content: script's content :rtype: boolean", "response": "def putscript(self, name, content):\n \"\"\"Upload a script to the server\n\n See MANAGESIEVE specifications, section 2.6\n\n :param name: script's name\n :param content: script's content\n :rtype: boolean\n \"\"\"\n content = tools.to_bytes(content)\n content = tools.to_bytes(\"{%d+}\" % len(content)) + CRLF + content\n code, data = (\n self.__send_command(\"PUTSCRIPT\", [name.encode(\"utf-8\"), content]))\n if code == \"OK\":\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef deletescript(self, name):\n code, data = self.__send_command(\n \"DELETESCRIPT\", [name.encode(\"utf-8\")])\n if code == \"OK\":\n return True\n return False", "response": "Delete a script from the server"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef renamescript(self, oldname, newname):\n if \"VERSION\" in self.__capabilities:\n code, data = self.__send_command(\n \"RENAMESCRIPT\",\n [oldname.encode(\"utf-8\"), newname.encode(\"utf-8\")])\n if code == \"OK\":\n return True\n return False\n\n (active_script, scripts) = self.listscripts()\n condition = (\n oldname != active_script and\n (scripts is None or oldname not in scripts)\n )\n if condition:\n self.errmsg = b\"Old script does not exist\"\n return False\n if newname in scripts:\n self.errmsg = b\"New script already exists\"\n return False\n oldscript = self.getscript(oldname)\n if oldscript is None:\n return False\n if not self.putscript(newname, oldscript):\n return False\n if active_script == oldname:\n if not self.setactive(newname):\n return False\n if not self.deletescript(oldname):\n return False\n return True", "response": "Rename a script on the server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef setactive(self, scriptname):\n code, data = self.__send_command(\n \"SETACTIVE\", [scriptname.encode(\"utf-8\")])\n if code == \"OK\":\n return True\n return False", "response": "Define the active script in the current active script."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef checkscript(self, content):\n if \"VERSION\" not in self.__capabilities:\n raise NotImplementedError(\n \"server does not support CHECKSCRIPT command\")\n content = tools.to_bytes(content)\n content = tools.to_bytes(\"{%d+}\" % len(content)) + CRLF + content\n code, data = self.__send_command(\"CHECKSCRIPT\", [content])\n if code == \"OK\":\n return True\n return False", "response": "Check whether a script is valid."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nanalyses some data in text and yield a tuple of the last two - tuple containing its name and parsed value.", "response": "def scan(self, text):\n \"\"\"Analyse some data\n\n Analyse the passed content. Each time a token is recognized, a\n 2-uple containing its name and parsed value is raised (via\n yield).\n\n On error, a ParseError exception is raised.\n\n :param text: a binary string containing the data to parse\n \"\"\"\n self.pos = 0\n self.text = text\n while self.pos < len(text):\n m = self.wsregexp.match(text, self.pos)\n if m is not None:\n self.pos = m.end()\n continue\n\n m = self.regexp.match(text, self.pos)\n if m is None:\n raise ParseError(\"unknown token %s\" % text[self.pos:])\n\n self.pos = m.end()\n yield (m.lastgroup, m.group(m.lastgroup))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreset the parser s internal variables to an initial state. Useful when creating a new parser.", "response": "def __reset_parser(self):\n \"\"\"Reset parser's internal variables\n\n Restore the parser to an initial state. Useful when creating a\n new parser or reusing an existing one.\n \"\"\"\n self.result = []\n self.hash_comments = []\n\n self.__cstate = None\n self.__curcommand = None\n self.__curstringlist = None\n self.__expected = None\n self.__opened_blocks = 0\n RequireCommand.loaded_extensions = []"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __up(self, onlyrecord=False):\n if self.__curcommand.must_follow is not None:\n if not self.__curcommand.parent:\n prevcmd = self.result[-1] if len(self.result) else None\n else:\n prevcmd = self.__curcommand.parent.children[-2] \\\n if len(self.__curcommand.parent.children) >= 2 else None\n if prevcmd is None or prevcmd.name not in self.__curcommand.must_follow:\n raise ParseError(\"the %s command must follow an %s command\" %\n (self.__curcommand.name,\n \" or \".join(self.__curcommand.must_follow)))\n\n if not self.__curcommand.parent:\n # collect current amount of hash comments for later\n # parsing into names and desciptions\n self.__curcommand.hash_comments = self.hash_comments\n self.hash_comments = []\n self.result += [self.__curcommand]\n\n if not onlyrecord:\n while self.__curcommand:\n self.__curcommand = self.__curcommand.parent\n # Make sure to detect all done tests (including 'not' ones).\n condition = (\n self.__curcommand and\n self.__curcommand.get_type() == \"test\" and\n self.__curcommand.iscomplete()\n )\n if condition:\n continue\n break", "response": "Return to the current command s parent and if onlyrecord is True then only record the new command into its parent."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __check_command_completion(self, testsemicolon=True):\n if not self.__curcommand.iscomplete():\n return True\n\n ctype = self.__curcommand.get_type()\n if ctype == \"action\" or \\\n (ctype == \"control\" and\n not self.__curcommand.accept_children):\n if testsemicolon:\n self.__set_expected(\"semicolon\")\n return True\n\n while self.__curcommand.parent:\n cmd = self.__curcommand\n self.__curcommand = self.__curcommand.parent\n if self.__curcommand.get_type() in [\"control\", \"test\"]:\n if self.__curcommand.iscomplete():\n if self.__curcommand.get_type() == \"control\":\n break\n continue\n if not self.__curcommand.check_next_arg(\"test\", cmd, add=False):\n return False\n if not self.__curcommand.iscomplete():\n if self.__curcommand.variable_args_nb:\n self.__set_expected(\"comma\", \"right_parenthesis\")\n break\n\n return True", "response": "Checks if the command is complete and if it is not then returns False."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __command(self, ttype, tvalue):\n if self.__cstate is None:\n if ttype == \"right_cbracket\":\n self.__up()\n self.__opened_blocks -= 1\n self.__cstate = None\n return True\n\n if ttype != \"identifier\":\n return False\n command = get_command_instance(\n tvalue.decode(\"ascii\"), self.__curcommand)\n if command.get_type() == \"test\":\n raise ParseError(\n \"%s may not appear as a first command\" % command.name)\n if command.get_type() == \"control\" and command.accept_children \\\n and command.has_arguments():\n self.__set_expected(\"identifier\")\n if self.__curcommand is not None:\n if not self.__curcommand.addchild(command):\n raise ParseError(\"%s unexpected after a %s\" %\n (tvalue, self.__curcommand.name))\n self.__curcommand = command\n self.__cstate = self.__arguments\n\n return True\n\n if self.__cstate(ttype, tvalue):\n return True\n\n if ttype == \"left_cbracket\":\n self.__opened_blocks += 1\n self.__cstate = None\n return True\n\n if ttype == \"semicolon\":\n self.__cstate = None\n if not self.__check_command_completion(testsemicolon=False):\n return False\n self.__curcommand.complete_cb()\n self.__up()\n return True\n return False", "response": "This method is called by the parser when a command is detected."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse(self, text):\n if isinstance(text, text_type):\n text = text.encode(\"utf-8\")\n\n self.__reset_parser()\n try:\n for ttype, tvalue in self.lexer.scan(text):\n if ttype == \"hash_comment\":\n self.hash_comments += [tvalue.strip()]\n continue\n if ttype == \"bracket_comment\":\n continue\n if self.__expected is not None:\n if ttype not in self.__expected:\n if self.lexer.pos < len(text):\n msg = (\n \"%s found while %s expected near '%s'\"\n % (ttype, \"|\".join(self.__expected),\n text[self.lexer.pos])\n )\n else:\n msg = (\n \"%s found while %s expected at end of file\"\n % (ttype, \"|\".join(self.__expected))\n )\n raise ParseError(msg)\n self.__expected = None\n\n if not self.__command(ttype, tvalue):\n msg = (\n \"unexpected token '%s' found near '%s'\"\n % (tvalue.decode(), text.decode()[self.lexer.pos])\n )\n raise ParseError(msg)\n if self.__opened_blocks:\n self.__set_expected(\"right_cbracket\")\n if self.__expected is not None:\n raise ParseError(\"end of script reached while %s expected\" %\n \"|\".join(self.__expected))\n\n except (ParseError, CommandError) as e:\n self.error = \"line %d: %s\" % (self.lexer.curlineno(), str(e))\n return False\n return True", "response": "The entry point of the parser."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse_file(self, name):\n with open(name, \"rb\") as fp:\n return self.parse(fp.read())", "response": "Parse the content of a file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dump(self, target=sys.stdout):\n for r in self.result:\n r.dump(target=target)", "response": "Dump the parsing tree."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_commands(cmds):\n if not isinstance(cmds, Iterable):\n cmds = [cmds]\n\n for command in cmds:\n if command.__name__.endswith(\"Command\"):\n globals()[command.__name__] = command", "response": "Adds one or more commands to the module namespace."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntry to guess and create the appropriate command instance based on the command name.", "response": "def get_command_instance(name, parent=None, checkexists=True):\n \"\"\"Try to guess and create the appropriate command instance\n\n Given a command name (encountered by the parser), construct the\n associated class name and, if known, return a new instance.\n\n If the command is not known or has not been loaded using require,\n an UnknownCommand exception is raised.\n\n :param name: the command's name\n :param parent: the eventual parent command\n :return: a new class instance\n \"\"\"\n cname = \"%sCommand\" % name.lower().capitalize()\n gl = globals()\n condition = (\n cname not in gl or\n (checkexists and gl[cname].extension and\n gl[cname].extension not in RequireCommand.loaded_extensions)\n )\n if condition:\n raise UnknownCommand(name)\n return gl[cname](parent)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef tosieve(self, indentlevel=0, target=sys.stdout):\n self.__print(self.name, indentlevel, nocr=True, target=target)\n if self.has_arguments():\n for arg in self.args_definition:\n if not arg[\"name\"] in self.arguments:\n continue\n target.write(\" \")\n value = self.arguments[arg[\"name\"]]\n atype = arg[\"type\"]\n if \"tag\" in atype:\n target.write(value)\n if arg[\"name\"] in self.extra_arguments:\n value = self.extra_arguments[arg[\"name\"]]\n atype = arg[\"extra_arg\"][\"type\"]\n target.write(\" \")\n else:\n continue\n\n if type(value) == list:\n if self.__get_arg_type(arg[\"name\"]) == [\"testlist\"]:\n target.write(\"(\")\n for t in value:\n t.tosieve(target=target)\n if value.index(t) != len(value) - 1:\n target.write(\", \")\n target.write(\")\")\n else:\n target.write(\n \"[{}]\".format(\", \".join(\n ['\"%s\"' % v.strip('\"') for v in value])\n )\n )\n continue\n if isinstance(value, Command):\n value.tosieve(indentlevel, target=target)\n continue\n\n if \"string\" in atype:\n target.write(value)\n if not value.startswith('\"') and not value.startswith(\"[\"):\n target.write(\"\\n\")\n else:\n target.write(value)\n\n if not self.accept_children:\n if self.get_type() != \"test\":\n target.write(\";\\n\")\n return\n if self.get_type() != \"control\":\n return\n target.write(\" {\\n\")\n for ch in self.children:\n ch.tosieve(indentlevel + 4, target=target)\n self.__print(\"}\", indentlevel, target=target)", "response": "Generate the sieve syntax corresponding to this command."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndisplay the command ongoing and pending commands.", "response": "def dump(self, indentlevel=0, target=sys.stdout):\n \"\"\"Display the command\n\n Pretty printing of this command and its eventual arguments and\n children. (recursively)\n\n :param indentlevel: integer that indicates indentation level to apply\n \"\"\"\n self.__print(self, indentlevel, target=target)\n indentlevel += 4\n if self.has_arguments():\n for arg in self.args_definition:\n if not arg[\"name\"] in self.arguments:\n continue\n value = self.arguments[arg[\"name\"]]\n atype = arg[\"type\"]\n if \"tag\" in atype:\n self.__print(str(value), indentlevel, target=target)\n if arg[\"name\"] in self.extra_arguments:\n value = self.extra_arguments[arg[\"name\"]]\n atype = arg[\"extra_arg\"][\"type\"]\n else:\n continue\n if type(value) == list:\n if self.__get_arg_type(arg[\"name\"]) == [\"testlist\"]:\n for t in value:\n t.dump(indentlevel, target)\n else:\n self.__print(\"[\" + (\",\".join(value)) + \"]\",\n indentlevel, target=target)\n continue\n if isinstance(value, Command):\n value.dump(indentlevel, target)\n continue\n self.__print(str(value), indentlevel, target=target)\n for ch in self.children:\n ch.dump(indentlevel, target)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef addchild(self, child):\n if not self.accept_children:\n return False\n self.children += [child]\n return True", "response": "Add a new child to the command sequence."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef iscomplete(self, atype=None, avalue=None):\n if self.variable_args_nb:\n return False\n if self.required_args == -1:\n self.required_args = 0\n for arg in self.args_definition:\n if arg.get(\"required\", False):\n self.required_args += 1\n return (\n (not self.curarg or\n \"extra_arg\" not in self.curarg or\n (\"valid_for\" in self.curarg[\"extra_arg\"] and\n atype and atype in self.curarg[\"extra_arg\"][\"type\"] and\n avalue not in self.curarg[\"extra_arg\"][\"valid_for\"])) and\n (self.rargs_cnt == self.required_args)\n )", "response": "Check if the command is complete."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks if value is allowed for arg COOKIE", "response": "def __is_valid_value_for_arg(self, arg, value, check_extension=True):\n \"\"\"Check if value is allowed for arg\n\n Some commands only allow a limited set of values. The method\n always returns True for methods that do not provide such a\n set.\n\n :param arg: the argument's name\n :param value: the value to check\n :param check_extension: check if value requires an extension\n :return: True on succes, False otherwise\n \"\"\"\n if \"values\" not in arg and \"extension_values\" not in arg:\n return True\n if \"values\" in arg and value.lower() in arg[\"values\"]:\n return True\n if \"extension_values\" in arg:\n extension = arg[\"extension_values\"].get(value.lower())\n if extension:\n condition = (\n check_extension and\n extension not in RequireCommand.loaded_extensions\n )\n if condition:\n raise ExtensionNotLoaded(extension)\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncheck if type is valid based on input type list string is special because it can be used for stringlist", "response": "def __is_valid_type(self, typ, typlist):\n \"\"\" Check if type is valid based on input type list\n \"string\" is special because it can be used for stringlist\n\n :param typ: the type to check\n :param typlist: the list of type to check\n :return: True on success, False otherwise\n \"\"\"\n typ_is_str = typ == \"string\"\n str_list_in_typlist = \"stringlist\" in typlist\n\n return typ in typlist or (typ_is_str and str_list_in_typlist)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef args_as_tuple(self):\n value = self.arguments[\"header-names\"]\n if isinstance(value, list):\n value = \"[{}]\".format(\n \",\".join('\"{}\"'.format(item) for item in value))\n if not value.startswith(\"[\"):\n return ('exists', value.strip('\"'))\n return (\"exists\", ) + tuple(tools.to_list(value))", "response": "Return the arguments as a tuple."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn arguments as a list.", "response": "def args_as_tuple(self):\n \"\"\"Return arguments as a list.\"\"\"\n if \",\" in self.arguments[\"header-names\"]:\n result = tuple(tools.to_list(self.arguments[\"header-names\"]))\n else:\n result = (self.arguments[\"header-names\"].strip('\"'),)\n result = result + (self.arguments[\"match-type\"],)\n if \",\" in self.arguments[\"key-list\"]:\n result = result + tuple(\n tools.to_list(self.arguments[\"key-list\"], unquote=False))\n else:\n result = result + (self.arguments[\"key-list\"].strip('\"'),)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef args_as_tuple(self):\n result = (\"body\", )\n result = result + (\n self.arguments[\"body-transform\"], self.arguments[\"match-type\"])\n if self.arguments[\"key-list\"].startswith(\"[\"):\n result = result + tuple(\n tools.to_list(self.arguments[\"key-list\"]))\n else:\n result = result + (self.arguments[\"key-list\"].strip('\"'),)\n return result", "response": "Return arguments as a list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndeal with optional stringlist before a required one.", "response": "def reassign_arguments(self):\n \"\"\"Deal with optional stringlist before a required one.\"\"\"\n condition = (\n \"variable-list\" in self.arguments and\n \"list-of-flags\" not in self.arguments\n )\n if condition:\n self.arguments[\"list-of-flags\"] = (\n self.arguments.pop(\"variable-list\"))\n self.rargs_cnt = 1"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn arguments as a list.", "response": "def args_as_tuple(self):\n \"\"\"Return arguments as a list.\"\"\"\n result = (\"currentdate\", )\n result += (\n \":zone\",\n self.extra_arguments[\"zone\"].strip('\"'),\n self.arguments[\"match-type\"],\n )\n if self.arguments[\"match-type\"] in [\":count\", \":value\"]:\n result += (self.extra_arguments[\"match-type\"].strip('\"'), )\n result += (self.arguments[\"date-part\"].strip('\"'), )\n value = self.arguments[\"key-list\"]\n if isinstance(value, list):\n # FIXME\n value = \"[{}]\".format(\n \",\".join('\"{}\"'.format(item) for item in value))\n if value.startswith(\"[\"):\n result = result + tuple(tools.to_list(value))\n else:\n result = result + (value.strip('\"'),)\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef to_bytes(s, encoding=\"utf-8\"):\n if isinstance(s, six.binary_type):\n return s\n if six.PY3:\n return bytes(s, encoding)\n return s.encode(encoding)", "response": "Convert a string to bytes."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a string representing a list to real list.", "response": "def to_list(stringlist, unquote=True):\n \"\"\"Convert a string representing a list to real list.\"\"\"\n stringlist = stringlist[1:-1]\n return [\n string.strip('\"') if unquote else string\n for string in stringlist.split(\",\")\n ]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef monkeypatch_i18n():\n import jinja2.ext\n from puente.ext import PuenteI18nExtension\n\n jinja2.ext.InternationalizationExtension = PuenteI18nExtension\n jinja2.ext.i18n = PuenteI18nExtension", "response": "Monkeypatch the i18n translation for the trans blocks in the Jinja2 template."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generate_keywords(additional_keywords=None):\n # Shallow copy\n keywords = dict(BABEL_KEYWORDS)\n\n keywords.update({\n '_lazy': None,\n 'gettext_lazy': None,\n 'ugettext_lazy': None,\n 'gettext_noop': None,\n 'ugettext_noop': None,\n\n 'ngettext_lazy': (1, 2),\n 'ungettext_lazy': (1, 2),\n\n 'npgettext': ((1, 'c'), 2, 3),\n 'pgettext_lazy': ((1, 'c'), 2),\n 'npgettext_lazy': ((1, 'c'), 2, 3),\n })\n\n # Add specified keywords\n if additional_keywords:\n for key, val in additional_keywords.items():\n keywords[key] = val\n return keywords", "response": "Generates gettext keywords list for the current language."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef collapse_whitespace(message):\n return u' '.join(map(lambda s: s.strip(),\n filter(None, message.strip().splitlines())))", "response": "Collapses consecutive whitespace into a single space"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef generate_options_map():\n try:\n return settings.PUENTE['JINJA2_CONFIG']\n except KeyError:\n pass\n\n # If using Django 1.8+, we can skim the TEMPLATES for a backend that we\n # know about and extract the settings from that.\n for tmpl_config in getattr(settings, 'TEMPLATES', []):\n try:\n backend = tmpl_config['BACKEND']\n except KeyError:\n continue\n\n if backend == 'django_jinja.backend.Jinja2':\n extensions = tmpl_config.get('OPTIONS', {}).get('extensions', [])\n return {\n '**.*': {\n 'extensions': ','.join(extensions),\n 'silent': 'False',\n }\n }\n\n # If this is Django 1.7 and Jingo, try to grab extensions from\n # JINJA_CONFIG.\n if getattr(settings, 'JINJA_CONFIG'):\n jinja_config = settings.JINJA_CONFIG\n if callable(jinja_config):\n jinja_config = jinja_config()\n return {\n '**.*': {\n 'extensions': ','.join(jinja_config['extensions']),\n 'silent': 'False',\n }\n }\n\n raise CommandError(\n 'No valid jinja2 config found in settings. See configuration '\n 'documentation.'\n )", "response": "Generate an options_map that can be used to extract the contents of a Jinja2 environment."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef extract_command(outputdir, domain_methods, text_domain, keywords,\n comment_tags, base_dir, project, version,\n msgid_bugs_address):\n \"\"\"Extracts strings into .pot files\n\n :arg domain: domains to generate strings for or 'all' for all domains\n :arg outputdir: output dir for .pot files; usually\n locale/templates/LC_MESSAGES/\n :arg domain_methods: DOMAIN_METHODS setting\n :arg text_domain: TEXT_DOMAIN settings\n :arg keywords: KEYWORDS setting\n :arg comment_tags: COMMENT_TAGS setting\n :arg base_dir: BASE_DIR setting\n :arg project: PROJECT setting\n :arg version: VERSION setting\n :arg msgid_bugs_address: MSGID_BUGS_ADDRESS setting\n\n \"\"\"\n # Must monkeypatch first to fix i18n extensions stomping issues!\n monkeypatch_i18n()\n\n # Create the outputdir if it doesn't exist\n outputdir = os.path.abspath(outputdir)\n if not os.path.isdir(outputdir):\n print('Creating output dir %s ...' % outputdir)\n os.makedirs(outputdir)\n\n domains = domain_methods.keys()\n\n def callback(filename, method, options):\n if method != 'ignore':\n print(' %s' % filename)\n\n # Extract string for each domain\n for domain in domains:\n print('Extracting all strings in domain %s...' % domain)\n\n methods = domain_methods[domain]\n\n catalog = Catalog(\n header_comment='',\n project=project,\n version=version,\n msgid_bugs_address=msgid_bugs_address,\n charset='utf-8',\n )\n extracted = extract_from_dir(\n base_dir,\n method_map=methods,\n options_map=generate_options_map(),\n keywords=keywords,\n comment_tags=comment_tags,\n callback=callback,\n )\n\n for filename, lineno, msg, cmts, ctxt in extracted:\n catalog.add(msg, None, [(filename, lineno)], auto_comments=cmts,\n context=ctxt)\n\n with open(os.path.join(outputdir, '%s.pot' % domain), 'wb') as fp:\n write_po(fp, catalog, width=80)\n\n print('Done')", "response": "Extracts strings into. pot files for a specific language."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef merge_command(create, backup, base_dir, domain_methods, languages):\n locale_dir = os.path.join(base_dir, 'locale')\n\n # Verify existence of msginit and msgmerge\n if not call(['which', 'msginit'], stdout=PIPE) == 0:\n raise CommandError('You do not have gettext installed.')\n\n if not call(['which', 'msgmerge'], stdout=PIPE) == 0:\n raise CommandError('You do not have gettext installed.')\n\n if languages and isinstance(languages[0], (tuple, list)):\n # Django's LANGUAGES setting takes a value like:\n #\n # LANGUAGES = (\n # ('de', _('German')),\n # ('en', _('English')),\n # )\n #\n # but we only want the language codes, so we pull the first\n # part from all the tuples.\n languages = [lang[0] for lang in languages]\n\n if create:\n for lang in languages:\n d = os.path.join(locale_dir, lang.replace('-', '_'),\n 'LC_MESSAGES')\n if not os.path.exists(d):\n os.makedirs(d)\n\n domains = domain_methods.keys()\n for domain in domains:\n print('Merging %s strings to each locale...' % domain)\n domain_pot = os.path.join(locale_dir, 'templates', 'LC_MESSAGES',\n '%s.pot' % domain)\n if not os.path.isfile(domain_pot):\n raise CommandError('Can not find %s.pot' % domain)\n\n for locale in os.listdir(locale_dir):\n if ((not os.path.isdir(os.path.join(locale_dir, locale)) or\n locale.startswith('.') or\n locale == 'templates')):\n continue\n\n domain_po = os.path.join(locale_dir, locale, 'LC_MESSAGES',\n '%s.po' % domain)\n\n if not os.path.isfile(domain_po):\n print(' Can not find (%s). Creating...' % domain_po)\n p1 = Popen([\n 'msginit',\n '--no-translator',\n '--locale=%s' % locale,\n '--input=%s' % domain_pot,\n '--output-file=%s' % domain_po,\n '--width=200'\n ])\n p1.communicate()\n\n print('Merging %s.po for %s' % (domain, locale))\n with open(domain_pot) as domain_pot_file:\n if locale == 'en_US':\n # Create an English translation catalog, then merge\n with TemporaryFile('w+t') as enmerged:\n p2 = Popen(['msgen', '-'], stdin=domain_pot_file,\n stdout=enmerged)\n p2.communicate()\n _msgmerge(domain_po, enmerged, backup)\n else:\n _msgmerge(domain_po, domain_pot_file, backup)\n\n print('Domain %s finished' % domain)\n\n print('All finished')", "response": "This function will merge the gettext files into one or more languages."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _msgmerge(po_path, pot_file, backup):\n pot_file.seek(0)\n command = [\n 'msgmerge',\n '--update',\n '--width=200',\n '--backup=%s' % ('simple' if backup else 'off'),\n po_path,\n '-'\n ]\n p3 = Popen(command, stdin=pot_file)\n p3.communicate()", "response": "Merge an existing. po file with new translations."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run(self): # No \"_\" in the name, but nevertheless, running in the backed\n self.preRun_()\n self.running=True\n \n while(self.running):\n self.cycle_()\n self.handleSignal_()\n \n self.postRun_()", "response": "This function is called by the daemon to start the process."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating the shared memory client immediately after fork", "response": "def preRun_(self):\n \"\"\"Create the shared memory client immediately after fork\n \"\"\"\n self.report(\"preRun_\")\n super().preRun_()\n self.client = ShmemRGBClient(\n name=self.shmem_name,\n n_ringbuffer=self.n_buffer, # size of ring buffer\n width=self.image_dimensions[0],\n height=self.image_dimensions[1],\n # client timeouts if nothing has been received in 1000 milliseconds\n mstimeout=1000,\n verbose=False\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nactivate the shared mem info.", "response": "def activate_(self, n_buffer, image_dimensions, shmem_name):\n \"\"\"Shared mem info is given. Now we can create the shmem client\n \"\"\"\n self.active = True\n self.image_dimensions = image_dimensions\n self.client = ShmemRGBClient(\n name =shmem_name,\n n_ringbuffer =n_buffer, # size of ring buffer\n width =image_dimensions[0],\n height =image_dimensions[1],\n # client timeouts if nothing has been received in 1000 milliseconds\n mstimeout =1000,\n verbose =False\n )\n self.postActivate_()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef deactivate_(self):\n self.preDeactivate_()\n self.active = False\n self.image_dimensions = None\n self.client = None", "response": "Deactivate the current thread."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntell the external analyzer to reset itself", "response": "def reset(self):\n \"\"\"Tell the external analyzer to reset itself\n \"\"\"\n self.report(\"sending reset\")\n try:\n self.p.stdin.write(bytes(\"T\\n\",\"utf-8\"))\n self.p.stdin.flush()\n except IOError:\n self.report(\"could not send reset command\")"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntelling the process to exit the current process.", "response": "def close(self):\n \"\"\"Tell the process to exit\n \"\"\"\n try:\n self.p.stdin.write(bytes(\"X\\n\",\"utf-8\"))\n self.p.stdin.flush()\n except IOError:\n self.report(\"could not send exit command\")\n self.p.wait() # wait until the process is closed\n try:\n os.remove(self.tmpfile) # clean up the temporary file\n except FileNotFoundError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef readStdout(self):\n btt = bytes()\n while True:\n bt = self.p.stdout.read(1)\n if bt:\n btt += bt\n else:\n # print(\"!\")\n break\n \"\"\"\n if (bt == \"\\n\"):\n break\n \"\"\"\n return btt[0:-1].decode(\"utf-8\")", "response": "Reads the stdout of the current process and returns the bytes."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating temporary file for image dumps and the analyzer itself", "response": "def postActivate_(self):\n \"\"\"Create temporary file for image dumps and the analyzer itself\n \"\"\"\n self.tmpfile = os.path.join(constant.tmpdir,\"valkka-\"+str(os.getpid())) # e.g. \"/tmp/valkka-10968\" \n self.analyzer = ExternalDetector(\n executable = self.executable,\n image_dimensions = self.image_dimensions,\n tmpfile = self.tmpfile\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating custom pages of file storage type.", "response": "def generate_pages_by_file():\n \"\"\"Generates custom pages of 'file' storage type.\"\"\"\n from veripress import app\n from veripress.model import storage\n from veripress.model.parsers import get_standard_format_name\n from veripress.helpers import traverse_directory\n\n deploy_dir = get_deploy_dir()\n\n def copy_file(src, dst):\n makedirs(os.path.dirname(dst), mode=0o755, exist_ok=True)\n shutil.copyfile(src, dst)\n\n with app.app_context(), app.test_client() as client:\n root_path = os.path.join(app.instance_path, 'pages')\n for path in traverse_directory(root_path):\n # e.g. 'a/b/c/index.md'\n rel_path = os.path.relpath(path, root_path)\n # e.g. ('a/b/c/index', '.md')\n filename, ext = os.path.splitext(rel_path)\n if get_standard_format_name(ext[1:]) is not None:\n # is source of custom page\n rel_url = filename.replace(os.path.sep, '/') + '.html'\n page = storage.get_page(rel_url, include_draft=False)\n if page is not None:\n # it's not a draft, so generate the html page\n makedirs(os.path.join(deploy_dir,\n os.path.dirname(rel_path)),\n mode=0o755, exist_ok=True)\n with open(os.path.join(deploy_dir, filename + '.html'),\n 'wb') as f:\n f.write(client.get('/' + rel_url).data)\n if app.config['PAGE_SOURCE_ACCESSIBLE']:\n copy_file(path, os.path.join(deploy_dir, rel_path))\n else:\n # is other direct files\n copy_file(path, os.path.join(deploy_dir, rel_path))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndefine column patterns and collections", "response": "def define(self):\n \"\"\"Define column patterns and collections\n \"\"\"\n self.collections = []\n\n self.camera_collection = \\\n SimpleCollection(filename=os.path.join(self.directory, \"devices.dat\"),\n row_classes=[\n DataModel.EmptyRow,\n DataModel.RTSPCameraRow,\n DataModel.USBCameraRow\n ]\n )\n self.collections.append(self.camera_collection)\n\n self.config_collection = \\\n SimpleCollection(filename=os.path.join(self.directory, \"config.dat\"),\n row_classes=[ # we could dump here all kinds of info related to different kind of configuration forms\n DataModel.MemoryConfigRow\n ]\n )\n self.collections.append(self.config_collection)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a list of RTSPCameraDevices by ID.", "response": "def getDevicesById(self): # , query):\n \"\"\"\n rows = self.camera_collection.get(query)\n devices_by_id = {}\n for row in rows:\n row.pop(\"classname\")\n device = DataModel.RTSPCameraDevice(**row)\n devices_by_id[device._id] = device\n return devices_by_id\n \"\"\"\n rows = self.camera_collection.get()\n devices_by_id = {}\n for row in rows:\n classname=row.pop(\"classname\")\n if (classname == \"RTSPCameraRow\"):\n device = DataModel.RTSPCameraDevice(**row)\n elif (classname == \"USBCameraRow\"):\n device = DataModel.USBCameraDevice(**row)\n else:\n device = None\n if (device):\n devices_by_id[device._id] = device\n return devices_by_id"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninitializing the LicensePlateDetector object.", "response": "def init(self):\n \"\"\"Init alpr\n \n The LicensePlateDetector object gets instantiated in the multiprocess, so the library is imported in the multiprocess (i.e. \"other side of the fork\") as well\n \"\"\"\n # some modules might need to be imported \"on the other side of the fork\"\n # .. but the, when importing this module, the import is not tested\n #\n # \n # from openalpr import Alpr\n from valkka.mvision.alpr.openalpr_fix import Alpr\n self.movement = MovementDetector()\n self.alpr = Alpr(self.country, self.conf_file, self.runtime_data)\n if not self.alpr.is_loaded():\n self.alpr = None\n return\n self.alpr.set_top_n(self.top_n)\n self.reset()\n \n \"\"\"\n # test in ipython:\n from valkka.mvision.alpr.openalpr_fix import Alpr\n country=\"eu\"\n conf_file=\"/usr/share/openalpr/config/openalpr.defaults.conf\"\n runtime_data=\"/usr/share/openalpr/runtime_data\"\n a = Alpr(country, conf_file, runtime_data)\n a.is_loaded() \n \"\"\""} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef postActivate_(self):\n self.analyzer = LicensePlateDetector(**self.analyzer_pars) # this is called after the fork (i.e. after the multiprocess has been spawned)\n self.report(\"analyzer object=\", self.analyzer)", "response": "This is called after the fork has been called."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef inject(self):\n if self.is_injected():\n return False\n\n with open(self, mode=\"a+\") as fp:\n fp.seek(0)\n fp.write(\"\\n\".join([\n \"\",\n \"try:\",\n \" import pout\",\n \"except ImportError:\",\n \" pass\",\n \"else:\",\n \" pout.inject()\",\n \"\",\n ]))\n\n return True", "response": "inject code into sitecustomize. py that will inject pout into the builtins\n so it will be available globally"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef fix_relative_url(self, publish_type, rel_url):\n if publish_type == 'post':\n return self.fix_post_relative_url(rel_url), False\n elif publish_type == 'page':\n return self.fix_page_relative_url(rel_url)\n else:\n raise ValueError(\n 'Publish type \"{}\" is not supported'.format(publish_type))", "response": "Fix post or page relative url to a standard uniform format."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfix post relative url to a standard uniform format.", "response": "def fix_post_relative_url(rel_url):\n \"\"\"\n Fix post relative url to a standard, uniform format.\n\n Possible input:\n - 2016/7/8/my-post\n - 2016/07/08/my-post.html\n - 2016/8/09/my-post/\n - 2016/8/09/my-post/index\n - 2016/8/09/my-post/index.htm\n - 2016/8/09/my-post/index.html\n\n :param rel_url: relative url to fix\n :return: fixed relative url, or None if cannot recognize\n \"\"\"\n m = re.match(\n r'^(?P\\d{4})/(?P\\d{1,2})/(?P\\d{1,2})/'\n r'(?P[^/]+?)'\n r'(?:(?:\\.html)|(?:/(?Pindex(?:\\.html?)?)?))?$',\n rel_url\n )\n if not m:\n return None\n\n year, month, day, post_name = m.groups()[:4]\n try:\n d = date(year=int(year), month=int(month), day=int(day))\n return '/'.join((d.strftime('%Y/%m/%d'), post_name,\n 'index.html' if m.group('index') else ''))\n except (TypeError, ValueError):\n # the date is invalid\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfilter result with given filter functions.", "response": "def _filter_result(result, filter_functions=None):\n \"\"\"\n Filter result with given filter functions.\n\n :param result: an iterable object\n :param filter_functions: some filter functions\n :return: a filter object (filtered result)\n \"\"\"\n if filter_functions is not None:\n for filter_func in filter_functions:\n result = filter(filter_func, result)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_posts_with_limits(self, include_draft=False, **limits):\n filter_funcs = []\n\n for attr in ('title', 'layout', 'author',\n 'email', 'tags', 'categories'):\n if limits.get(attr):\n filter_set = set(to_list(limits.get(attr)))\n\n def get_filter_func(filter_set_, attr_):\n return lambda p: filter_set_.intersection(\n to_list(getattr(p, attr_)))\n\n filter_funcs.append(get_filter_func(filter_set, attr))\n\n for attr in ('created', 'updated'):\n interval = limits.get(attr)\n if isinstance(interval, (list, tuple)) and len(interval) == 2 \\\n and isinstance(interval[0], date) and isinstance(\n interval[1], date):\n # [start date(time), end date(time)]\n start, end = interval\n start = to_datetime(start)\n if not isinstance(end, datetime):\n # 'end' is a date,\n # we should convert it to 00:00:00 of the next day,\n # so that posts of that day will be included\n end = datetime.strptime(\n '%04d-%02d-%02d' % (end.year, end.month, end.day),\n '%Y-%m-%d')\n end += timedelta(days=1)\n\n def get_filter_func(attr_, start_dt, end_dt):\n return lambda p: start_dt <= getattr(p, attr_) < end_dt\n\n filter_funcs.append(get_filter_func(attr, start, end))\n\n return self.get_posts(include_draft=include_draft,\n filter_functions=filter_funcs)", "response": "Get all posts and filter them as needed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsearching for a query text.", "response": "def search_for(self, query, include_draft=False):\n \"\"\"\n Search for a query text.\n\n :param query: keyword to query\n :param include_draft: return draft posts/pages or not\n :return: an iterable object of posts and pages (if allowed).\n \"\"\"\n query = query.lower()\n if not query:\n return []\n\n def contains_query_keyword(post_or_page):\n contains = query in post_or_page.title.lower() \\\n or query in Markup(\n get_parser(post_or_page.format).parse_whole(\n post_or_page.raw_content)\n ).striptags().lower()\n return contains\n\n return filter(contains_query_keyword,\n chain(self.get_posts(include_draft=include_draft),\n self.get_pages(include_draft=include_draft)\n if current_app.config[\n 'ALLOW_SEARCH_PAGES'] else []))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef fix_page_relative_url(rel_url):\n rel_url = rel_url.lstrip('/') # trim all heading '/'\n endswith_slash = rel_url.endswith('/')\n rel_url = rel_url.rstrip('/') + (\n '/' if endswith_slash else '') # preserve only one trailing '/'\n if not rel_url or rel_url == '/':\n return None, False\n\n file_path = os.path.join(current_app.instance_path, 'pages',\n rel_url.replace('/', os.path.sep))\n if rel_url.endswith('/'):\n index_html_file_path = os.path.join(file_path, 'index.html')\n if os.path.isfile(index_html_file_path):\n # index.html exists\n return index_html_file_path, True\n return rel_url, False\n elif os.path.isfile(file_path):\n ext = os.path.splitext(file_path)[1][1:]\n if get_standard_format_name(ext) is not None:\n # is source of custom page\n if current_app.config['PAGE_SOURCE_ACCESSIBLE']:\n return file_path, True\n else:\n # is other direct files\n return file_path, True\n elif os.path.isdir(file_path):\n return rel_url + '/', False\n\n sp = rel_url.rsplit('/', 1)\n m = re.match(r'(.+)\\.html?', sp[-1])\n if m:\n sp[-1] = m.group(1) + '.html'\n else:\n sp[-1] += '.html'\n return '/'.join(sp), False", "response": "Fix page relative url to a standard uniform format."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef search_file(search_root, search_filename,\n instance_relative_root=False):\n \"\"\"\n Search for a filename in a specific search root dir.\n\n :param search_root: root dir to search\n :param search_filename: filename to search (no extension)\n :param instance_relative_root: search root is relative to instance path\n :return: tuple(full_file_path, extension without heading dot)\n \"\"\"\n if instance_relative_root:\n search_root = os.path.join(current_app.instance_path, search_root)\n file_path = None\n file_ext = None\n for file in os.listdir(search_root):\n filename, ext = os.path.splitext(file)\n if filename == search_filename and ext and ext != '.':\n file_path = os.path.join(search_root, filename + ext)\n file_ext = ext[1:] # remove heading '.' (dot)\n break\n return file_path, file_ext", "response": "Search for a filename in a specific search root dir."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread yaml head and raw content from a file.", "response": "def read_file(file_path):\n \"\"\"\n Read yaml head and raw body content from a file.\n\n :param file_path: file path\n :return: tuple(meta, raw_content)\n \"\"\"\n with open(file_path, 'r', encoding='utf-8') as f:\n whole = f.read().strip()\n\n if whole.startswith('---'):\n # may has yaml meta info, so we try to split it out\n sp = re.split(r'-{3,}', whole.lstrip('-'), maxsplit=1)\n if len(sp) == 2:\n # do have yaml meta info, so we read it\n return yaml.load(sp[0]), sp[1].lstrip()\n return {}, whole"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all posts from the filesystem.", "response": "def get_posts(self, include_draft=False, filter_functions=None):\n \"\"\"\n Get all posts from filesystem.\n\n :param include_draft: return draft posts or not\n :param filter_functions: filter to apply BEFORE result being sorted\n :return: an iterable of Post objects (the first is the latest post)\n \"\"\"\n\n def posts_generator(path):\n \"\"\"Loads valid posts one by one in the given path.\"\"\"\n if os.path.isdir(path):\n for file in os.listdir(path):\n filename, ext = os.path.splitext(file)\n format_name = get_standard_format_name(ext[1:])\n if format_name is not None and re.match(\n r'\\d{4}-\\d{2}-\\d{2}-.+', filename):\n # the format is supported and the filename is valid,\n # so load this post\n post = Post()\n post.format = format_name\n post.meta, post.raw_content = FileStorage.read_file(\n os.path.join(path, file))\n post.rel_url = filename.replace('-', '/', 3) + '/'\n post.unique_key = '/post/' + post.rel_url\n yield post\n\n posts_path = os.path.join(current_app.instance_path, 'posts')\n result = filter(lambda p: include_draft or not p.is_draft,\n posts_generator(posts_path))\n result = self._filter_result(result, filter_functions)\n\n return sorted(result, key=lambda p: p.created, reverse=True)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a post object for given relative url from filesystem.", "response": "def get_post(self, rel_url, include_draft=False):\n \"\"\"\n Get post for given relative url from filesystem.\n\n Possible input:\n - 2017/01/01/my-post/\n - 2017/01/01/my-post/index.html\n\n :param rel_url: relative url\n :param include_draft: return draft post or not\n :return: a Post object\n \"\"\"\n raw_rel_url = str(rel_url)\n if rel_url.endswith('/index.html'):\n rel_url = rel_url.rsplit('/', 1)[\n 0] + '/' # remove the trailing 'index.html'\n post_filename = rel_url[:-1].replace('/', '-')\n\n post_file_path, post_file_ext = FileStorage.search_instance_file(\n 'posts', post_filename)\n if post_file_path is None or post_file_ext is None or \\\n get_standard_format_name(post_file_ext) is None:\n # no such post\n return None\n\n # construct the post object\n post = Post()\n post.rel_url = raw_rel_url\n # 'rel_url' contains no trailing 'index.html'\n post.unique_key = '/post/' + rel_url\n post.format = get_standard_format_name(post_file_ext)\n post.meta, post.raw_content = FileStorage.read_file(post_file_path)\n return post if include_draft or not post.is_draft else None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_tags(self):\n posts = self.get_posts(include_draft=True)\n result = {}\n for post in posts:\n for tag_name in set(post.tags):\n result[tag_name] = result.setdefault(\n tag_name, Pair(0, 0)) + Pair(1, 0 if post.is_draft else 1)\n return list(result.items())", "response": "Get all tags and post count of each tag."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget all categories and post count of each category.", "response": "def get_categories(self):\n \"\"\"\n Get all categories and post count of each category.\n\n :return dict_item(category_name, Pair(count_all, count_published))\n \"\"\"\n posts = self.get_posts(include_draft=True)\n result = {}\n for post in posts:\n for category_name in set(post.categories):\n result[category_name] = result.setdefault(\n category_name,\n Pair(0, 0)) + Pair(1, 0 if post.is_draft else 1)\n return list(result.items())"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_pages(self, include_draft=False):\n\n def pages_generator(pages_root_path):\n for file_path in traverse_directory(pages_root_path,\n yield_dir=False):\n rel_path = os.path.relpath(file_path, pages_root_path)\n rel_path, ext = os.path.splitext(rel_path)\n if not ext or ext == '.' or get_standard_format_name(\n ext[1:]) is None:\n continue # pragma: no cover\n\n if rel_path.endswith(os.path.sep + 'index'):\n rel_path = rel_path[:-len('index')]\n else:\n rel_path += '.html'\n page = self.get_page(rel_path.replace(os.path.sep, '/'),\n include_draft=include_draft)\n if page is not None:\n yield page\n\n pages_path = os.path.join(current_app.instance_path, 'pages')\n return list(pages_generator(pages_path))", "response": "Get all custom pages and return a list of Page objects."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_page(self, rel_url, include_draft=False):\n page_dir = os.path.dirname(rel_url.replace('/', os.path.sep))\n page_path = os.path.join(current_app.instance_path, 'pages', page_dir)\n if not os.path.isdir(page_path):\n # no such directory\n return None\n\n page_filename = rel_url[len(page_dir):].lstrip('/')\n if not page_filename:\n page_filename = 'index'\n else:\n page_filename = os.path.splitext(page_filename)[0]\n\n page_file_path, page_file_ext = FileStorage.search_file(page_path,\n page_filename)\n if page_file_path is None or page_file_ext is None or \\\n get_standard_format_name(page_file_ext) is None:\n # no such page\n return None\n\n page = Page()\n page.rel_url = rel_url\n page.unique_key = '/' + (\n rel_url.rsplit('/', 1)[0] + '/' if rel_url.endswith(\n '/index.html') else rel_url)\n page.format = get_standard_format_name(page_file_ext)\n page.meta, page.raw_content = FileStorage.read_file(page_file_path)\n return page if include_draft or not page.is_draft else None", "response": "Get custom page for given relative url from filesystem."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_widgets(self, position=None, include_draft=False):\n\n def widgets_generator(path):\n \"\"\"Loads valid widgets one by one in the given path.\"\"\"\n if os.path.isdir(path):\n for file in os.listdir(path):\n _, ext = os.path.splitext(file)\n format_name = get_standard_format_name(ext[1:])\n if format_name is not None:\n # the format is supported, so load it\n widget = Widget()\n widget.format = format_name\n widget.meta, widget.raw_content = \\\n FileStorage.read_file(os.path.join(path, file))\n yield widget\n\n widgets_path = os.path.join(current_app.instance_path, 'widgets')\n positions = to_list(position) if position is not None else position\n result = filter(\n lambda w: (w.position in positions\n if positions is not None else True) and\n (include_draft or not w.is_draft),\n widgets_generator(widgets_path))\n return sorted(result, key=lambda w: (w.position, w.order))", "response": "Get widgets for given position from filesystem."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef custom_render_template(template_name_or_list, **context):\n response_str = render_template(\n functools.reduce(lambda x, y: x + [os.path.join('custom', y), y],\n to_list(template_name_or_list), []),\n **context\n )\n if hasattr(g, 'status_code'):\n status_code = g.status_code\n else:\n status_code = 200\n return response_str, status_code", "response": "Render a custom template."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef templated(template=None, *templates):\n\n def decorator(func):\n @functools.wraps(func)\n def wrapper(*args, **kwargs):\n template_ = template\n if template_ is None:\n template_ = request.endpoint.split('.', 1)[1].replace(\n '.', '/') + '.html'\n context = func(*args, **kwargs)\n if context is None:\n context = {}\n elif not isinstance(context, dict):\n return context\n return custom_render_template(\n list(chain(to_list(template_), templates)), **context)\n\n return wrapper\n\n return decorator", "response": "Decorator for views that returns a function with one or more default template name."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncopy all content in src directory to dst directory.", "response": "def copy_folder_content(src, dst):\n \"\"\"\n Copy all content in src directory to dst directory.\n The src and dst must exist.\n \"\"\"\n for file in os.listdir(src):\n file_path = os.path.join(src, file)\n dst_file_path = os.path.join(dst, file)\n if os.path.isdir(file_path):\n shutil.copytree(file_path, dst_file_path)\n else:\n shutil.copyfile(file_path, dst_file_path)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves all content in the given folder.", "response": "def remove_folder_content(path, ignore_hidden_file=False):\n \"\"\"\n Remove all content in the given folder.\n \"\"\"\n for file in os.listdir(path):\n if ignore_hidden_file and file.startswith('.'):\n continue\n\n file_path = os.path.join(path, file)\n if os.path.isdir(file_path):\n shutil.rmtree(file_path)\n else:\n os.remove(file_path)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef makedirs(path, mode=0o777, exist_ok=False):\n os.makedirs(path, mode, exist_ok)", "response": "A wrapper of os. makedirs that creates the specified path."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef unload(self):\n\n if self.loaded:\n self.loaded = False\n self._openalprpy_lib.dispose(self.alpr_pointer)", "response": "Unloads OpenALPR from memory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef recognize_array(self, byte_array):\n if type(byte_array) != bytes:\n raise TypeError(\"Expected a byte array (string in Python 2, bytes in Python 3)\")\n pb = ctypes.cast(byte_array, ctypes.POINTER(ctypes.c_ubyte))\n ptr = self._recognize_array_func(self.alpr_pointer, pb, len(byte_array))\n json_data = ctypes.cast(ptr, ctypes.c_char_p).value\n json_data = _convert_from_charp(json_data)\n response_obj = json.loads(json_data)\n self._free_json_mem_func(ctypes.c_void_p(ptr))\n return response_obj", "response": "This causes OpenALPR to attempt to recognize an image passed in as a byte array."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef recognize_ndarray(self, ndarray):\n if self._recognize_raw_image_func is None:\n raise RuntimeError('NumPy missing')\n height, width = ndarray.shape[:2]\n bpp = ndarray.shape[2] if len(ndarray.shape) > 2 else 1\n ptr = self._recognize_raw_image_func(self.alpr_pointer, ndarray.flatten(), bpp, width, height)\n json_data = ctypes.cast(ptr, ctypes.c_char_p).value\n json_data = _convert_from_charp(json_data)\n \n # there is a bug in the openalpr python bindings\n # sometimes there are real numbers with \",\" as the decimal point..!\n # print(\"openalpr_lib : recognize_ndarray : json_data =\", json_data)\n p = re.compile('\\d(\\,)\\d')\n json_data = p.subn(\".\", json_data)[0]\n \n response_obj = json.loads(json_data)\n self._free_json_mem_func(ctypes.c_void_p(ptr))\n return response_obj", "response": "This causes OpenALPR to attempt to recognize an image passed in as a numpy array."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the country for detecting license plates.", "response": "def set_country(self, country):\n \"\"\"\n This sets the country for detecting license plates. For example,\n setting country to \"us\" for United States or \"eu\" for Europe.\n\n :param country: A unicode/ascii string (Python 2/3) or bytes array (Python 3)\n :return: None\n \"\"\"\n country = _convert_to_charp(country)\n self._set_country_func(self.alpr_pointer, country)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the prewarp configuration used to skew images in OpenALPR before processing.", "response": "def set_prewarp(self, prewarp):\n \"\"\"\n Updates the prewarp configuration used to skew images in OpenALPR before\n processing.\n\n :param prewarp: A unicode/ascii string (Python 2/3) or bytes array (Python 3)\n :return: None\n \"\"\"\n prewarp = _convert_to_charp(prewarp)\n self._set_prewarp_func(self.alpr_pointer, prewarp)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning True if this call string is complete meaning it has a function name and balanced parens", "response": "def is_complete(self):\n \"\"\"Return True if this call string is complete, meaning it has a function\n name and balanced parens\"\"\"\n try:\n [t for t in self.tokens]\n ret = True\n logger.debug('CallString [{}] is complete'.format(self.strip()))\n\n except tokenize.TokenError:\n logger.debug('CallString [{}] is NOT complete'.format(self.strip()))\n ret = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _find_calls(self, ast_tree, called_module, called_func):\n '''\n scan the abstract source tree looking for possible ways to call the called_module\n and called_func\n\n since -- 7-2-12 -- Jay\n\n example -- \n # import the module a couple ways:\n import pout\n from pout import v\n from pout import v as voom\n import pout as poom\n\n # this function would return: ['pout.v', 'v', 'voom', 'poom.v']\n\n module finder might be useful someday\n link -- http://docs.python.org/library/modulefinder.html\n link -- http://stackoverflow.com/questions/2572582/return-a-list-of-imported-python-modules-used-in-a-script\n\n ast_tree -- _ast.* instance -- the internal ast object that is being checked, returned from compile()\n with ast.PyCF_ONLY_AST flag\n called_module -- string -- we are checking the ast for imports of this module\n called_func -- string -- we are checking the ast for aliases of this function\n\n return -- set -- the list of possible calls the ast_tree could make to call the called_func\n ''' \n s = set()\n\n # always add the default call, the set will make sure there are no dupes...\n s.add(\"{}.{}\".format(called_module, called_func))\n\n if hasattr(ast_tree, 'name'):\n if ast_tree.name == called_func:\n # the function is defined in this module\n s.add(called_func)\n\n if hasattr(ast_tree, 'body'):\n # further down the rabbit hole we go\n if isinstance(ast_tree.body, Iterable):\n for ast_body in ast_tree.body:\n s.update(self._find_calls(ast_body, called_module, called_func))\n\n elif hasattr(ast_tree, 'names'):\n # base case\n if hasattr(ast_tree, 'module'):\n # we are in a from ... import ... statement\n if ast_tree.module == called_module:\n for ast_name in ast_tree.names:\n if ast_name.name == called_func:\n s.add(unicode(ast_name.asname if ast_name.asname is not None else ast_name.name))\n\n else:\n # we are in a import ... statement\n for ast_name in ast_tree.names:\n if hasattr(ast_name, 'name') and (ast_name.name == called_module):\n call = \"{}.{}\".format(\n ast_name.asname if ast_name.asname is not None else ast_name.name,\n called_func\n )\n s.add(call)\n\n return s", "response": "finds possible calls for a given module and function"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_arg_info(self):\n '''\n get all the info of a method call\n\n this will find what arg names you passed into the method and tie them to their passed in values,\n it will also find file and line number\n\n return -- dict -- a bunch of info on the call\n '''\n ret_dict = {\n 'args': [],\n #'frame': None,\n 'line': 'Unknown',\n 'file': 'Unknown',\n 'arg_names': []\n }\n arg_vals = self.arg_vals\n #modname = self.modname\n\n c = self.call\n ret_dict.update(c.info)\n\n if len(arg_vals) > 0:\n args = []\n\n if len(ret_dict['arg_names']) > 0:\n # match the found arg names to their respective values\n for i, arg_name in enumerate(ret_dict['arg_names']):\n args.append({'name': arg_name, 'val': arg_vals[i]})\n\n else:\n # we can't autodiscover the names, in an interactive shell session?\n for i, arg_val in enumerate(arg_vals):\n args.append({'name': 'Unknown {}'.format(i), 'val': arg_val})\n\n ret_dict['args'] = args\n\n return ret_dict", "response": "get all the info of a method call in a bunch of info"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _find_entry_call(self, frames):\n\n back_i = 0\n pout_path = self._get_src_file(self.modname)\n for frame_i, frame in enumerate(frames):\n if frame[1] == pout_path:\n back_i = frame_i\n\n return Call(frames[back_i])", "response": "find the correct entry in the frames list"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef url_rule(blueprint_or_app, rules,\n endpoint=None, view_func=None, **options):\n \"\"\"\n Add one or more url rules to the given Flask blueprint or app.\n\n :param blueprint_or_app: Flask blueprint or app\n :param rules: a single rule string or a list of rules\n :param endpoint: endpoint\n :param view_func: view function\n :param options: other options\n \"\"\"\n for rule in to_list(rules):\n blueprint_or_app.add_url_rule(rule,\n endpoint=endpoint,\n view_func=view_func,\n **options)", "response": "Adds one or more url rules to the given Flask blueprint or app."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef to_list(item_or_list):\n if isinstance(item_or_list, list):\n return item_or_list\n elif isinstance(item_or_list, (str, bytes)):\n return [item_or_list]\n elif isinstance(item_or_list, Iterable):\n return list(item_or_list)\n else:\n return [item_or_list]", "response": "Convert a single item a tuple a generator or anything else to a list."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts a date object to a datetime object.", "response": "def to_datetime(date_or_datetime):\n \"\"\"\n Convert a date object to a datetime object,\n or return as it is if it's not a date object.\n\n :param date_or_datetime: date or datetime object\n :return: a datetime object\n \"\"\"\n if isinstance(date_or_datetime, date) and \\\n not isinstance(date_or_datetime, datetime):\n d = date_or_datetime\n return datetime.strptime(\n '%04d-%02d-%02d' % (d.year, d.month, d.day), '%Y-%m-%d')\n return date_or_datetime"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a timezone string to a timezone object.", "response": "def timezone_from_str(tz_str):\n \"\"\"\n Convert a timezone string to a timezone object.\n\n :param tz_str: string with format 'Asia/Shanghai' or 'UTC\u00b1[hh]:[mm]'\n :return: a timezone object (tzinfo)\n \"\"\"\n m = re.match(r'UTC([+|-]\\d{1,2}):(\\d{2})', tz_str)\n if m:\n # in format 'UTC\u00b1[hh]:[mm]'\n delta_h = int(m.group(1))\n delta_m = int(m.group(2)) if delta_h >= 0 else -int(m.group(2))\n return timezone(timedelta(hours=delta_h, minutes=delta_m))\n\n # in format 'Asia/Shanghai'\n try:\n return pytz.timezone(tz_str)\n except pytz.exceptions.UnknownTimeZoneError:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntraversing through a directory recursively.", "response": "def traverse_directory(dir_path, yield_dir=False):\n \"\"\"\n Traverse through a directory recursively.\n\n :param dir_path: directory path\n :param yield_dir: yield subdirectory or not\n :return: a generator\n \"\"\"\n if not os.path.isdir(dir_path):\n return\n\n for item in os.listdir(dir_path):\n new_path = os.path.join(dir_path, item)\n if os.path.isdir(new_path):\n if yield_dir:\n yield new_path + os.path.sep\n yield from traverse_directory(new_path, yield_dir)\n else:\n yield new_path"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse TOC of HTML content.", "response": "def parse_toc(html_content):\n \"\"\"\n Parse TOC of HTML content if the SHOW_TOC config is true.\n\n :param html_content: raw HTML content\n :return: tuple(processed HTML, toc list, toc HTML unordered list)\n \"\"\"\n from flask import current_app\n from veripress.model.toc import HtmlTocParser\n\n if current_app.config['SHOW_TOC']:\n toc_parser = HtmlTocParser()\n toc_parser.feed(html_content)\n toc_html = toc_parser.toc_html(\n depth=current_app.config['TOC_DEPTH'],\n lowest_level=current_app.config['TOC_LOWEST_LEVEL'])\n toc = toc_parser.toc(\n depth=current_app.config['TOC_DEPTH'],\n lowest_level=current_app.config['TOC_LOWEST_LEVEL'])\n return toc_parser.html, toc, toc_html\n else:\n return html_content, None, None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a new string with indent_count spaces added to the beginning of each line of self.", "response": "def indent(self, indent_count):\n '''\n add whitespace to the beginning of each line of val\n\n link -- http://code.activestate.com/recipes/66055-changing-the-indentation-of-a-multi-line-string/\n\n val -- string\n indent -- integer -- how much whitespace we want in front of each line of val\n\n return -- string -- val with more whitespace\n '''\n if indent_count < 1: return self\n\n s = [(\"\\t\" * indent_count) + line for line in self.splitlines(False)]\n s = \"\\n\".join(s)\n return type(self)(s)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating H264 stream Input image output video file time in seconds", "response": "def genH264(infile, outfile, T):\n \"\"\"Generate H264 stream\n \n Input image, output video file, time in seconds\n \n Example:\n \n ::\n \n genH264(\"/home/sampsa/python3/tests/lprtest/RealImages/IMG_20170308_093511.jpg\",\"testi.mkv\", 10)\n \n \"\"\"\n x264opts=\"-x264opts keyint=10:min-keyint=10:bframes=0\"\n h264_dummystream_flavor=\"-pix_fmt yuv420p -vprofile main\" \n\n opts=\"-vcodec h264 \"+h264_dummystream_flavor+\" \"+x264opts+\" -fflags +genpts -r 25 -t \"+str(T)\n com=\"ffmpeg -y -loop 1 -fflags +genpts -r 25 -i \"+infile+\" \"+opts+\" \"+outfile\n print(com)\n os.system(com)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef index(self, row, column, parent):\n\n if not self.hasIndex(row, column, parent):\n return QtCore.QModelIndex()\n\n if not parent.isValid():\n parentItem = self.root\n else:\n # So, here we go from QModelIndex to the actual object .. ?\n parentItem = parent.internalPointer()\n\n # the only place where a child item is queried\n childItem = parentItem.getChild(row)\n if childItem:\n # return self.createIndex(row, column)\n return self.createIndex(row, column, childItem)\n \"\"\"\n # .. that one does not work for PySide 5.12+\n TypeError: 'PySide2.QtCore.QAbstractItemModel.createIndex' called with wrong argument types:\n PySide2.QtCore.QAbstractItemModel.createIndex(int, int, ServerListItem)\n Supported signatures:\n PySide2.QtCore.QAbstractItemModel.createIndex(int, int, quintptr = 0)\n PySide2.QtCore.QAbstractItemModel.createIndex(int, int, void = nullptr)\n \"\"\"\n else:\n return QtCore.QModelIndex()", "response": "Returns the index of the item in the model specified by the given row column and parent index."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parent(self, index):\n if not index.isValid():\n return QtCore.QModelIndex()\n\n childItem = index.internalPointer()\n # the only place where the parent item is queried\n parentItem = childItem.getParent()\n\n if parentItem == self.root:\n return QtCore.QModelIndex()\n\n return self.createIndex(parentItem.row(), 0, parentItem)", "response": "Returns the parent of the model item with the given index."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef columnCount(self, parent):\n # print(\"columnCount:\",self)\n if parent.isValid():\n return parent.internalPointer().columnCount()\n else:\n return self.root.columnCount()", "response": "Returns the number of columns for the given parent."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the number of rows under the given parent.", "response": "def rowCount(self, parent):\n \"\"\" Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning the number of children of parent.\n \"\"\"\n\n if parent.column() > 0:\n return 0\n\n if not parent.isValid():\n parentItem = self.root\n else:\n parentItem = parent.internalPointer()\n\n return parentItem.childCount()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef flags(self, index):\n\n if not index.isValid():\n return QtCore.Qt.NoItemFlags\n\n item = index.internalPointer()\n\n # return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable |\n # QtCore.Qt.ItemIsDragEnabled\n return item.getFlags()", "response": "Returns the item flags for the given index."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the full value with the path also", "response": "def full_value(self):\n \"\"\"Returns the full value with the path also (ie, name = value (path))\n\n :returns: String\n \"\"\"\n s = self.name_value()\n s += self.path_value()\n s += \"\\n\\n\"\n return s"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _printstr(self, args, call_info=None):\n # unicode sandwich, everything printed should be a byte string\n s = \"\\n\"\n\n for arg in args:\n #s += arg.encode('utf-8', 'pout.replace')\n s += arg\n\n if call_info:\n s += \"({}:{})\\n\\n\".format(self._get_path(call_info['file']), call_info['line'])\n\n return s", "response": "this gets all the args ready to be printed see self. _print"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _str(self, name, val):\n '''\n return a string version of name = val that can be printed\n\n example -- \n _str('foo', 'bar') # foo = bar\n\n name -- string -- the variable name that was passed into one of the public methods\n val -- mixed -- the variable at name's value\n\n return -- string\n '''\n s = ''\n v = Value(val)\n\n if name:\n logger.debug(\"{} is type {}\".format(name, v.typename))\n try:\n count = len(val)\n s = \"{} ({}) = {}\".format(name, count, v.string_value())\n\n except (TypeError, KeyError, AttributeError) as e:\n logger.info(e, exc_info=True)\n s = \"{} = {}\".format(name, v.string_value())\n\n else:\n s = v.string_value()\n\n return s", "response": "return a string version of name = val that can be printed"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn all the loggers that should be activated", "response": "def loggers(self):\n \"\"\"Return all the loggers that should be activated\"\"\"\n ret = []\n if self.logger_name:\n if isinstance(self.logger_name, logging.Logger):\n ret.append((self.logger_name.name, self.logger_name))\n else:\n ret.append((self.logger_name, logging.getLogger(self.logger_name)))\n\n else:\n ret = list(logging.Logger.manager.loggerDict.items())\n ret.append((\"root\", logging.getLogger()))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a nicely formatted backtrace", "response": "def _get_backtrace(self, frames, inspect_packages=False, depth=0):\n '''\n get a nicely formatted backtrace\n\n since -- 7-6-12\n\n frames -- list -- the frame_tuple frames to format\n inpsect_packages -- boolean -- by default, this only prints code of packages that are not \n in the pythonN directories, that cuts out a lot of the noise, set this to True if you\n want a full stacktrace\n depth -- integer -- how deep you want the stack trace to print (ie, if you only care about\n the last three calls, pass in depth=3 so you only get the last 3 rows of the stack)\n\n return -- list -- each line will be a nicely formatted entry of the backtrace\n '''\n calls = []\n #count = 1\n\n #for count, f in enumerate(frames[1:], 1):\n for count, f in enumerate(frames, 1):\n #prev_f = frames[i]\n #called_module = inspect.getmodule(prev_f[0]).__name__\n #called_func = prev_f[3]\n\n call = self.call_class(f)\n s = self._get_call_summary(call, inspect_packages=inspect_packages, index=count)\n calls.append(s)\n #count += 1\n\n if depth and (count > depth):\n break\n\n # reverse the order on return so most recent is on the bottom\n return calls[::-1]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a nicely formatted string synopsis of the call", "response": "def _get_call_summary(self, call, index=0, inspect_packages=True):\n '''\n get a call summary\n\n a call summary is a nicely formatted string synopsis of the call\n\n handy for backtraces\n\n since -- 7-6-12\n\n call_info -- dict -- the dict returned from _get_call_info()\n index -- integer -- set to something above 0 if you would like the summary to be numbered\n inspect_packages -- boolean -- set to True to get the full format even for system frames\n\n return -- string\n '''\n call_info = call.info\n inspect_regex = re.compile(r'[\\\\\\\\/]python\\d(?:\\.\\d+)?', re.I)\n\n # truncate the filepath if it is super long\n f = call_info['file']\n if len(f) > 75:\n f = \"{}...{}\".format(f[0:30], f[-45:])\n\n if inspect_packages or not inspect_regex.search(call_info['file']): \n\n s = \"{}:{}\\n\\n{}\\n\\n\".format(\n f,\n call_info['line'],\n String(call_info['call']).indent(1)\n )\n\n else:\n\n s = \"{}:{}\\n\".format(\n f,\n call_info['line']\n )\n\n if index > 0:\n s = \"{:02d} - {}\".format(index, s)\n\n return s"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting the video stream to the given device.", "response": "def setDevice(self, device): \n \"\"\"Sets the video stream\n \n :param device: A rather generic device class. In this case DataModel.RTSPCameraDevice.\n \"\"\"\n self.report(\"setDevice :\", device)\n \n if (self.mvision_process == None):\n return\n \n if (not device and not self.device): # None can be passed as an argument when the device has not been set yet\n return\n \n if (self.device):\n if self.device == device:\n self.report(\"setDevice : same device\")\n return\n \n if self.filterchain: # there's video already\n self.clearDevice()\n \n self.device = device\n self.video.setDevice(self.device) # inform the video widget so it can start drags\n \n # ManagedFilterChain.addViewPort accepts ViewPort instance\n self.filterchain = self.filterchain_group.get(_id = self.device._id)\n \n if self.filterchain:\n self.viewport.setXScreenNum(self.n_xscreen)\n self.viewport.setWindowId (int(self.video.winId()))\n self.filterchain.addViewPort(self.viewport)\n \n # now the shared mem / semaphore part :\n self.shmem_name = self.filterchain.getShmem()\n print(self.pre, \"setDevice : got shmem name\", self.shmem_name)\n \n self.mvision_widget = self.mvision_process.getWidget()\n self.mvision_widget.setParent(self.main_widget)\n self.main_layout.addWidget(self.mvision_widget)\n \n self.mvision_process.activate(\n n_buffer = constant.shmem_n_buffer,\n image_dimensions = constant.shmem_image_dimensions,\n shmem_name = self.shmem_name\n )\n \n self.thread.addProcess(self.mvision_process)\n \n # is there a signal giving the bounding boxes..? let's connect it\n if hasattr(self.mvision_process.signals,\"bboxes\"):\n print(self.pre, \"setDevice : connecting bboxes signal\")\n self.mvision_process.signals.bboxes.connect(self.set_bounding_boxes_slot)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving the current stream and remove the current filterchain", "response": "def clearDevice(self):\n \"\"\"Remove the current stream\n \"\"\"\n print(self.pre, \"clearDevice: \")\n \n self.report(\"clearDevice\")\n if not self.device:\n return\n if (self.mvision_process==None):\n return\n \n self.filterchain.delViewPort(self.viewport)\n self.filterchain.releaseShmem(self.shmem_name)\n\n self.mvision_process.deactivate() # put process back to sleep ..\n \n self.main_layout.removeWidget(self.mvision_widget)\n \n self.filterchain = None\n self.device = None\n \n self.video.update()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parser(format_name, ext_names=None):\n\n def decorator(cls):\n format_name_lower = format_name.lower()\n if ext_names is None:\n _ext_format_mapping[format_name_lower] = format_name_lower\n else:\n for ext in to_list(ext_names):\n _ext_format_mapping[ext.lower()] = format_name_lower\n _format_parser_mapping[format_name_lower] = cls()\n return cls\n\n return decorator", "response": "Decorator to register a parser class for a given format name."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_preview(self, raw_content):\n if self._read_more_exp is None:\n return self.parse_whole(raw_content), False\n\n sp = self._read_more_exp.split(raw_content, maxsplit=1)\n if len(sp) == 2 and sp[0]:\n has_more_content = True\n result = sp[0].rstrip()\n else:\n has_more_content = False\n result = raw_content\n # since the preview part contains no read_more_sep,\n # we can safely use the parse_whole method\n return self.parse_whole(result), has_more_content", "response": "Parse the preview part of the content and return the parsed string and whether there is more content or not."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves the first read_more_sep that occurs in raw_content.", "response": "def remove_read_more_sep(self, raw_content):\n \"\"\"\n Removes the first read_more_sep that occurs in raw_content.\n Subclasses should call this method to preprocess raw_content.\n \"\"\"\n if self._read_more_exp is None:\n return raw_content\n\n sp = self._read_more_exp.split(raw_content, maxsplit=1)\n if len(sp) == 2 and sp[0]:\n result = '\\n\\n'.join((sp[0].rstrip(), sp[1].lstrip()))\n else:\n result = raw_content\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef v(*args, **kwargs):\n '''\n print the name = values of any passed in variables\n\n this prints out the passed in name, the value, and the file:line where the v()\n method was called so you can easily find it and remove it later\n\n example -- \n foo = 1\n bar = [1, 2, 3]\n out.v(foo, bar)\n \"\"\" prints out:\n foo = 1\n\n bar = \n [\n 0: 1,\n 1: 2,\n 2: 3\n ]\n\n (/file:line)\n \"\"\"\n\n *args -- list -- the variables you want to see pretty printed for humans\n '''\n if not args:\n raise ValueError(\"you didn't pass any arguments to print out\")\n\n with Reflect.context(args, **kwargs) as r:\n instance = V_CLASS(r, stream, **kwargs)\n instance()", "response": "prints out the name value and file line where the v method was called"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef vs(*args, **kwargs):\n if not args:\n raise ValueError(\"you didn't pass any arguments to print out\")\n\n with Reflect.context(args, **kwargs) as r:\n instance = V_CLASS(r, stream, **kwargs)\n instance.writeline(instance.value())", "response": "prints out the sequence of variable names or file positions of the current node"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef s(*args, **kwargs):\n if not args:\n raise ValueError(\"you didn't pass any arguments to print out\")\n\n with Reflect.context(args, **kwargs) as r:\n instance = V_CLASS(r, stream, **kwargs)\n return instance.full_value().strip()", "response": "exactly like v but returns the string instead of printing it out\n since 10 - 15 - 2015\n return -- str\n since 10 - 15 - 2015\n return -- str\n since 10 - 15 - 2015\n return -- str\n since 10 - 15 - 2015\n return -- str\n since 10 - 15 - 2015\n return -- str\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef r(*args, **kwargs):\n\n if len(args) <= 0:\n raise ValueError(\"you didn't pass any arguments to print out\")\n\n with Reflect.context(args, **kwargs) as r:\n instance = R_CLASS(r, stream, **kwargs)\n instance()", "response": "Similar to pout. v but gets rid of name and file information so it can be used\n in loops and stuff"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexits the sequence of objects", "response": "def x(*args, **kwargs):\n '''\n same as sys.exit(1) but prints out where it was called from before exiting\n\n I just find this really handy for debugging sometimes\n\n since -- 2013-5-9\n\n exit_code -- int -- if you want it something other than 1\n '''\n with Reflect.context(args, **kwargs) as r:\n instance = V_CLASS(r, stream, **kwargs)\n if args:\n instance()\n\n else:\n instance.writelines([\n 'exit at line {}\\n'.format(instance.reflect.info[\"line\"]),\n instance.path_value()\n ])\n\n exit_code = 1\n sys.exit(exit_code)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprinting the count of items in the", "response": "def h(count=0, **kwargs):\n '''\n prints \"here count\"\n\n example -- \n h(1) # here 1 (/file:line)\n h() # here line (/file:line)\n\n count -- integer -- the number you want to put after \"here\"\n '''\n with Reflect.context(**kwargs) as r:\n kwargs[\"count\"] = count\n instance = H_CLASS(r, stream, **kwargs)\n instance()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a big text break", "response": "def b(*args, **kwargs):\n '''\n create a big text break, you just kind of have to run it and see\n\n since -- 2013-5-9\n\n *args -- 1 arg = title if string, rows if int\n 2 args = title, int\n 3 args = title, int, sep\n '''\n with Reflect.context(**kwargs) as r:\n kwargs[\"args\"] = args\n instance = B_CLASS(r, stream, **kwargs)\n instance()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndumping the alphabetical tree of characters", "response": "def c(*args, **kwargs):\n '''\n kind of like od -c on the command line, basically it dumps each character and info\n about that char\n\n since -- 2013-5-9\n\n *args -- tuple -- one or more strings to dump\n '''\n with Reflect.context(**kwargs) as r:\n kwargs[\"args\"] = args\n instance = C_CLASS(r, stream, **kwargs)\n instance()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef j(*args, **kwargs):\n if not args:\n raise ValueError(\"you didn't pass any arguments to print out\")\n\n with Reflect.context(args, **kwargs) as r:\n instance = J_CLASS(r, stream, **kwargs)\n instance()", "response": "dump json\n since 2013 - 9 - 10"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprinting out memory usage at this point in time http://docs. python. org / 2. 7. 0", "response": "def m(name='', **kwargs):\n \"\"\"\n Print out memory usage at this point in time\n\n http://docs.python.org/2/library/resource.html\n http://stackoverflow.com/a/15448600/5006\n http://stackoverflow.com/questions/110259/which-python-memory-profiler-is-recommended\n \"\"\"\n with Reflect.context(**kwargs) as r:\n kwargs[\"name\"] = name\n instance = M_CLASS(r, stream, **kwargs)\n instance()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsleeps for a given number of seconds", "response": "def sleep(seconds, **kwargs):\n '''\n same as time.sleep(seconds) but prints out where it was called before sleeping\n and then again after finishing sleeping\n\n I just find this really handy for debugging sometimes\n\n since -- 2017-4-27\n\n :param seconds: float|int, how many seconds to sleep\n '''\n if seconds <= 0.0:\n raise ValueError(\"Invalid seconds {}\".format(seconds))\n\n with Reflect.context(**kwargs) as r:\n instance = V_CLASS(r, stream, **kwargs)\n instance.writeline(\"Sleeping {} second{} at {}\".format(\n seconds,\n \"s\" if seconds != 1.0 else \"\",\n instance.path_value()\n ))\n\n time.sleep(seconds)\n instance.writelines([\"...Done Sleeping\\n\", instance.path_value()])"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprints a backtrace of the current stack trace of the current object", "response": "def t(inspect_packages=False, depth=0, **kwargs):\n '''\n print a backtrace\n\n since -- 7-6-12\n\n inpsect_packages -- boolean -- by default, this only prints code of packages that are not \n in the pythonN directories, that cuts out a lot of the noise, set this to True if you\n want a full stacktrace\n depth -- integer -- how deep you want the stack trace to print (ie, if you only care about\n the last three calls, pass in depth=3 so you only get the last 3 rows of the stack)\n '''\n\n\n #frame = inspect.currentframe()\n\n try:\n frames = inspect.stack()\n kwargs[\"frames\"] = frames\n kwargs[\"inspect_packages\"] = inspect_packages\n kwargs[\"depth\"] = depth\n\n with Reflect.context(**kwargs) as r:\n instance = T_CLASS(r, stream, **kwargs)\n instance()\n\n finally:\n del frames"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninject pout into the builtins module so it can be called from anywhere without having to be explicitely imported", "response": "def inject():\n \"\"\"Injects pout into the builtins module so it can be called from anywhere without\n having to be explicitely imported, this is really just for convenience when\n debugging\n\n https://stackoverflow.com/questions/142545/python-how-to-make-a-cross-module-variable\n \"\"\"\n try:\n from .compat import builtins\n\n module = sys.modules[__name__]\n setattr(builtins, __name__, module)\n #builtins.pout = pout\n\n except ImportError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef getH264V4l2(verbose=False):\n import glob\n from subprocess import Popen, PIPE\n \n cams=[]\n\n for device in glob.glob(\"/sys/class/video4linux/*\"):\n devname=device.split(\"/\")[-1]\n devfile=os.path.join(\"/dev\",devname)\n \n lis=(\"v4l2-ctl --list-formats -d \"+devfile).split()\n\n p = Popen(lis, stdout=PIPE, stderr=PIPE)\n # p.communicate()\n # print(dir(p))\n # print(p.returncode)\n # print(p.stderr.read().decode(\"utf-8\"))\n st = p.stdout.read().decode(\"utf-8\")\n # print(st)\n \n if (st.lower().find(\"h264\")>-1):\n namefile=os.path.join(device, \"name\")\n # print(namefile)\n f=open(namefile, \"r\"); name=f.read(); f.close()\n fullname = name.strip() + \" (\"+devname+\")\"\n cams.append((devfile, fullname))\n\n if (verbose):\n for cam in cams:\n print(cam)\n \n return cams", "response": "Find all V4l2 cameras with H264 encoding and return a list of tuples with.."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef send_static_file(self, filename):\n if self.config['MODE'] == 'api-only':\n # if 'api-only' mode is set, we should not send static files\n abort(404)\n\n theme_static_folder = getattr(self, 'theme_static_folder', None)\n if theme_static_folder:\n try:\n return send_from_directory(theme_static_folder, filename)\n except NotFound:\n pass\n return super(CustomFlask, self).send_static_file(filename)", "response": "Send static files from the static folder in the current selected theme prior to the global static folder."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef setDevice(self, device): \n print(self.pre, \"setDevice :\", device)\n \n if (not device and not self.device): # None can be passed as an argument when the device has not been set yet\n return\n \n if (self.device):\n if self.device == device:\n print(self.pre, \"setDevice : same device\")\n return\n \n if self.filterchain: # there's video already\n self.clearDevice()\n \n self.device = device\n self.video.setDevice(self.device) # inform the video widget so it can start drags\n \n # ManagedFilterChain.addViewPort accepts ViewPort instance\n self.filterchain = self.filterchain_group.get(_id = self.device._id)\n \n if self.filterchain:\n self.viewport.setXScreenNum(self.n_xscreen)\n self.viewport.setWindowId (int(self.video.winId()))\n self.filterchain.addViewPort(self.viewport)", "response": "Sets the video stream to the given device."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef clearDevice(self):\n print(self.pre, \"clearDevice\")\n if not self.device:\n return\n \n self.filterchain.delViewPort(self.viewport)\n \n self.filterchain = None\n self.device = None\n \n self.video.update()", "response": "Remove the current stream and clear the filterchain"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef handle_left_double_click(self, info):\n if (self.double_click_focus == False): # turn focus on\n print(self.pre, \"handle_left_double_click: focus on\")\n self.cb_focus()\n else: # turn focus off\n print(self.pre, \"handle_left_double_click: focus off\")\n self.cb_unfocus()\n self.double_click_focus = not(\n self.double_click_focus)", "response": "This function is called when the left button is double - clicked with the left button."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget storage object of current app context.", "response": "def get_storage():\n \"\"\"\n Get storage object of current app context,\n will create a new one if not exists.\n\n :return: a storage object\n :raise: ConfigurationError: storage type in config is not supported\n \"\"\"\n storage_ = getattr(g, '_storage', None)\n if storage_ is None:\n storage_type = current_app.config['STORAGE_TYPE']\n if storage_type == 'file':\n storage_ = g._storage = storages.FileStorage()\n else:\n raise ConfigurationError(\n 'Storage type \"{}\" is not supported.'.format(storage_type))\n return storage_"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncall this slot chooses the form to be shown :param element: an object that has *_id* and *classname* attributes :param element_old: an object that has *_id* and *classname* attributes This slot is typically connected to List classes, widget attribute's, currentItemChanged method (List.widget is QListWidget that has currentItemChanged slot), so the element and element_old parameters are QListWidgetItem instances with extra attributes \"_id\" and \"_classname\" attached. Queries the database for element._id", "response": "def chooseForm_slot(self, element, element_old):\n \"\"\"Calling this slot chooses the form to be shown\n\n :param element: an object that has *_id* and *classname* attributes\n :param element_old: an object that has *_id* and *classname* attributes\n\n This slot is typically connected to List classes, widget attribute's, currentItemChanged method (List.widget is QListWidget that has currentItemChanged slot), so the element and element_old parameters are QListWidgetItem instances with extra attributes \"_id\" and \"_classname\" attached.\n\n Queries the database for element._id\n \"\"\"\n\n self.current_slot = None\n\n if (verbose):\n # enable this if you're unsure what's coming here..\n print(self.pre, \"chooseForm_slot :\", element)\n if (isinstance(element, type(None))):\n self.current_row = None\n self.element = None\n else:\n # print(self.pre,\"chooseForm_slot :\",element)\n assert(hasattr(element, \"_id\"))\n assert(hasattr(element, \"classname\"))\n try:\n self.current_row = self.row_instance_by_name[element.classname]\n except KeyError:\n print(\n self.pre,\n \"chooseForm_slot : no such classname for this FormSet : \",\n element.classname)\n self.current_row = None\n self.element = None\n else:\n self.resetForm()\n self.current_row.get(self.collection, element._id)\n self.element = element\n self.current_slot = self.current_row.get_column_value(\"slot\")\n\n self.showCurrent()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nkeeping updating the dropdown list.", "response": "def update_dropdown_list_slot(self):\n \"\"\"Keep updating the dropdown list. Say, don't let the user choose USB devices if none is available\n \"\"\"\n self.dropdown_widget.clear() # this will trigger dropdown_changed_slot\n self.row_instance_by_index = []\n for i, key in enumerate(self.row_instance_by_name.keys()):\n row_instance = self.row_instance_by_name[key]\n if (row_instance.isActive()):\n self.row_instance_by_index.append(row_instance)\n display_name = row_instance.getName()\n self.dropdown_widget.insertItem(i, display_name)\n row_instance.updateWidget()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef read(self):\n self.reset()\n for dic in self.datamodel.camera_collection.get(): # TODO: search directly for RTSPCameraRow\n if (self.verbose): print(self.pre, \"read : dic\", dic)\n \n if (dic[\"classname\"] == DataModel.RTSPCameraRow.__name__):\n affinity = -1\n if self.cpu_scheme:\n affinity = self.cpu_scheme.getAV()\n \n dic.pop(\"classname\")\n device = DataModel.RTSPCameraDevice(**dic) # a neat object with useful methods\n \n print(\"FilterChainGroup : read : slot =\", device.getLiveMainSlot())\n print(\"FilterChainGroup : read : address =\", device.getMainAddress())\n print(\"FilterChainGroup : read : _id =\", device._id)\n \n # chain = ManagedFilterchain( # decoding and branching the stream happens here\n # chain = ManagedFilterchain2( # decoding and branching the stream happens here\n chain = LiveManagedFilterchain( # decoding and branching the stream happens here\n livethread = self.livethread,\n openglthreads\n = self.gpu_handler.openglthreads,\n address = device.getMainAddress(),\n slot = device.getLiveMainSlot(),\n _id = device._id,\n affinity = affinity,\n msreconnect = 10000,\n # verbose = True,\n verbose =False,\n \n shmem_image_dimensions = constant.shmem_image_dimensions,\n shmem_n_buffer = constant.shmem_n_buffer,\n shmem_image_interval = constant.shmem_image_interval\n )\n self.chains.append(chain) # important .. otherwise chain will go out of context and get garbage collected\n \n elif (dic[\"classname\"] == DataModel.USBCameraRow.__name__):\n affinity = -1\n if self.cpu_scheme:\n affinity = self.cpu_scheme.getAV()\n \n dic.pop(\"classname\")\n device = DataModel.USBCameraDevice(**dic) # a neat object with useful methods\n \n print(\"FilterChainGroup : read : slot =\", device.getLiveMainSlot())\n print(\"FilterChainGroup : read : address =\", device.getMainAddress())\n print(\"FilterChainGroup : read : _id =\", device._id)\n \n chain = USBManagedFilterchain( # decoding and branching the stream happens here\n usbthread = self.usbthread,\n openglthreads\n = self.gpu_handler.openglthreads,\n address = device.getMainAddress(),\n slot = device.getLiveMainSlot(),\n _id = device._id,\n affinity = affinity,\n msreconnect = 10000,\n # verbose = True,\n verbose =False,\n \n shmem_image_dimensions = constant.shmem_image_dimensions,\n shmem_n_buffer = constant.shmem_n_buffer,\n shmem_image_interval = constant.shmem_image_interval\n )\n self.chains.append(chain)", "response": "Reads all devices from the database and creates a list of filterchains that are created for each."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef update(self):\n raise(AssertionError(\"out of date\"))\n \n new_ids = []\n old_ids = []\n \n # collect old ip addresses\n for chain in self.chains:\n if (self.verbose): print(self.pre, \"old :\", chain, chain.get__id(), chain.get_address(), chain._id)\n old_ids.append(chain.get__id())\n \n # collect devices\n devices = []\n for dic in self.datamodel.camera_collection.get():\n if (self.verbose): print(self.pre, \"update : dic\", dic)\n if (dic[\"classname\"] == DataModel.RTSPCameraRow.__name__):\n devices.append(dic)\n \n devices_by_id={}\n for dic in devices: # DataModel.RTSPCameraRow instances\n _id = dic[\"_id\"]\n new_ids.append(_id)\n devices_by_id[_id] = dic\n \n if (self.verbose):\n print(self.pre, \"update : new_ids =\", new_ids)\n print(self.pre, \"update : old_ids =\", old_ids)\n \n add_list = list(set(new_ids).difference(set(old_ids))) # cams to be added\n rem_list = list(set(old_ids).difference(set(new_ids))) # cams to be removed\n \n if (self.verbose):\n print(self.pre, \"update : add_list =\", add_list)\n print(self.pre, \"update : rem_list =\", rem_list)\n \n # purge removed chains\n for i, chain in enumerate(self.chains):\n if (chain.get__id() in rem_list):\n chain_ = self.chains.pop(i)\n if (self.verbose): print(self.pre, \"closing chain\", chain_)\n chain_.close()\n \n # add new chains\n for new_address in add_list:\n dic = devices_by_id[new_address]\n chain = ManagedFilterchain( # decoding and branching the stream happens here\n livethread = self.livethread,\n openglthreads\n = self.gpu_handler.openglthreads,\n address = DataModel.RTSPCameraRow.getMainAddressFromDict(dic),\n slot = dic[\"slot\"],\n _id = dic[\"_id\"],\n # affinity = a,\n msreconnect = 10000,\n verbose = True\n )\n if (self.verbose): print(self.pre, \"adding chain\", chain)\n self.chains.append(chain)", "response": "Reads all devices from the database and updates the filterchains with new ones."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(self, **kwargs):\n for chain in self.chains:\n for key in kwargs:\n getter_name = \"get_\"+key\n # scan all possible getters\n if (hasattr(chain, getter_name)):\n getter = getattr(chain, getter_name) # e.g. \"get_address\"\n if (getter() == kwargs[key]):\n return chain\n return None", "response": "Find correct filterchain based on generic variables\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef getDevice(self, **kwargs): \n filterchain = self.get(**kwargs)\n \n if not filterchain:\n return None\n \n # get filterchain init parameters that are compatible with RTSPCameraDevice input parameters\n pars = filterchain.getParDic(DataModel.RTSPCameraDevice.parameter_defs) \n # .. and instantiate an RTSPCameraDevice with those parameters\n device = DataModel.RTSPCameraDevice(**pars)\n \n print(self.pre, \"getDevice :\", pars, device)\n \n return device", "response": "Like get but returns a Device instance"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef to_dict(self):\n return {k: getattr(self, k) for k in filter(\n lambda k: not k.startswith('_') and k != 'to_dict', dir(self))}", "response": "Convert attributes and properties to a dict."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting this object to a dict object for serialization.", "response": "def to_dict(self):\n \"\"\"Convert self to a dict object for serialization.\"\"\"\n return {\n 'level': self.level,\n 'id': self.id,\n 'text': self.text,\n 'inner_html': self.inner_html,\n 'children': [child.to_dict() for child in self.children]\n }"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the table of currently fed HTML string.", "response": "def toc(self, depth=6, lowest_level=6):\n \"\"\"\n Get table of content of currently fed HTML string.\n\n :param depth: the depth of TOC\n :param lowest_level: the allowed lowest level of header tag\n :return: a list representing the TOC\n \"\"\"\n depth = min(max(depth, 0), 6)\n depth = 6 if depth == 0 else depth\n lowest_level = min(max(lowest_level, 1), 6)\n toc = self._root.to_dict()['children']\n\n def traverse(curr_toc, dep, lowest_lvl, curr_depth=1):\n if curr_depth > dep:\n # clear all items of this depth and exit the recursion\n curr_toc.clear()\n return\n\n items_to_remove = []\n for item in curr_toc:\n if item['level'] > lowest_lvl:\n # record item with low header level, for removing it later\n items_to_remove.append(item)\n else:\n traverse(item['children'], dep, lowest_lvl, curr_depth + 1)\n [curr_toc.remove(item) for item in items_to_remove]\n\n traverse(toc, depth, lowest_level)\n return toc"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef toc_html(self, depth=6, lowest_level=6):\n toc = self.toc(depth=depth, lowest_level=lowest_level)\n if not toc:\n return ''\n\n def map_toc_list(toc_list):\n result = ''\n if toc_list:\n result += '
    \\n'\n result += ''.join(\n map(lambda x: '
  • '\n '{}{}'\n '
  • \\n'.format(\n x['id'], x['inner_html'],\n map_toc_list(x['children'])),\n toc_list)\n )\n result += '
'\n return result\n\n return map_toc_list(toc)", "response": "Return the TOC of currently fed HTML string in form of HTML string."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the level of the header tag.", "response": "def _get_level(tag):\n \"\"\"\n Match the header level in the given tag name,\n or None if it's not a header tag.\n \"\"\"\n m = re.match(r'^h([123456])$', tag, flags=re.IGNORECASE)\n if not m:\n return None\n return int(m.group(1))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef requiredGPU_MB(self, n):\n from darknet.core import darknet_with_cuda\n if (darknet_with_cuda()): # its using cuda\n free = getFreeGPU_MB()\n print(\"Yolo: requiredGPU_MB: required, free\", n, free)\n if (free == -1): # could not detect ..\n return True\n return (free>=n)\n else:\n return True", "response": "Check if the required GPU memory in MBytes is available."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef postActivate_(self):\n if (self.requiredGPU_MB(self.required_mb)):\n self.analyzer = YoloV3Analyzer(verbose = self.verbose)\n else:\n self.warning_message = \"WARNING: not enough GPU memory!\"\n self.analyzer = None", "response": "Whatever you need to do after creating the shmem client\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the widget that will be used to display the textual information for the current locale.", "response": "def getWidget(self):\n \"\"\"Some ideas for your widget:\n - Textual information (alert, license place number)\n - Check boxes : if checked, send e-mail to your mom when the analyzer spots something\n - .. or send an sms to yourself\n - You can include the cv2.imshow window to the widget to see how the analyzer proceeds\n \"\"\"\n self.widget = QtWidgets.QTextEdit()\n self.widget.setStyleSheet(style.detector_test)\n self.widget.setReadOnly(True)\n self.signals.objects.connect(self.objects_slot)\n return self.widget"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the type of the object in the current module", "response": "def typename(self):\n '''\n get the type of val\n\n there are multiple places where we want to know if val is an object, or a string, or whatever,\n this method allows us to find out that information\n\n since -- 7-10-12\n\n val -- mixed -- the value to check\n\n return -- string -- the type\n '''\n t = 'DEFAULT'\n # http://docs.python.org/2/library/types.html\n# func_types = (\n# types.FunctionType,\n# types.BuiltinFunctionType,\n# types.MethodType,\n# types.UnboundMethodType,\n# types.BuiltinFunctionType,\n# types.BuiltinMethodType,\n# classmethod\n# )\n\n if self.is_primitive():\n t = 'DEFAULT'\n\n elif self.is_dict():\n t = 'DICT'\n\n elif self.is_list():\n t = 'LIST'\n\n elif self.is_array():\n t = 'ARRAY'\n\n elif self.is_tuple():\n t = 'TUPLE'\n\n elif self.is_type():\n t = 'TYPE'\n\n elif self.is_binary():\n t = 'BINARY'\n\n elif self.is_str():\n t = 'STRING'\n\n elif self.is_exception():\n t = 'EXCEPTION'\n\n elif self.is_module():\n # this has to go before the object check since a module will pass the object tests\n t = 'MODULE'\n\n elif self.is_callable():\n t = 'FUNCTION'\n\n # not doing this one since it can cause the class instance to do unexpected\n # things just to print it out\n #elif isinstance(val, property):\n # uses the @property decorator and the like\n #t = 'PROPERTY'\n\n elif self.is_dict_proxy():\n # maybe we have a dict proxy?\n t = 'DICT_PROXY'\n\n elif self.is_generator():\n t = 'GENERATOR'\n\n elif self.is_set():\n t = 'SET'\n\n elif self.is_object():\n t = 'OBJECT'\n\n# elif isinstance(val, func_types) and hasattr(val, '__call__'):\n# # this has to go after object because lots of times objects can be classified as functions\n# # http://stackoverflow.com/questions/624926/\n# t = 'FUNCTION'\n\n elif self.is_regex():\n t = 'REGEX'\n\n else:\n t = 'DEFAULT'\n\n return t"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbeing the value a primitive type?", "response": "def is_primitive(self):\n \"\"\"is the value a built-in type?\"\"\"\n if is_py2:\n return isinstance(\n self.val, \n (\n types.NoneType,\n types.BooleanType,\n types.IntType,\n types.LongType,\n types.FloatType\n )\n )\n\n else:\n return isinstance(\n self.val,\n (\n type(None),\n bool,\n int,\n float\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nturning an iteratable value into a string representation", "response": "def _str_iterator(self, iterator, name_callback=None, prefix=\"\\n\", left_paren='[', right_paren=']', depth=0):\n '''\n turn an iteratable value into a string representation\n\n iterator -- iterator -- the value to be iterated through\n name_callback -- callback -- if not None, a function that will take the key of each iteration\n prefix -- string -- what will be prepended to the generated value\n left_paren -- string -- what will open the generated value\n right_paren -- string -- what will close the generated value\n depth -- integer -- how deep into recursion we are\n\n return -- string\n '''\n indent = 1 if depth > 0 else 0\n\n s = []\n s.append('{}{}'.format(prefix, self._add_indent(left_paren, indent)))\n\n s_body = []\n\n for k, v in iterator:\n k = k if name_callback is None else name_callback(k)\n v = Value(v, depth+1)\n try:\n # TODO -- right here we should check some flag or something to\n # see if lists should render objects\n if k is None:\n s_body.append(\"{}\".format(v))\n else:\n s_body.append(\"{}: {}\".format(k, v))\n\n except RuntimeError as e:\n # I've never gotten this to work\n s_body.append(\"{}: ... Recursion error ...\".format(k))\n\n except UnicodeError as e:\n print(v.val)\n print(type(v.val))\n\n s_body = \",\\n\".join(s_body)\n s_body = self._add_indent(s_body, indent + 1)\n\n s.append(s_body)\n s.append(\"{}\".format(self._add_indent(right_paren, indent)))\n\n return \"\\n\".join(s)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding whitespace to the beginning of each line of val with more whitespace", "response": "def _add_indent(self, val, indent_count):\n '''\n add whitespace to the beginning of each line of val\n\n link -- http://code.activestate.com/recipes/66055-changing-the-indentation-of-a-multi-line-string/\n\n val -- string\n indent -- integer -- how much whitespace we want in front of each line of val\n\n return -- string -- val with more whitespace\n '''\n if isinstance(val, Value):\n val = val.string_value()\n\n return String(val).indent(indent_count)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwraps around getattr that suppresses any exception raised", "response": "def _getattr(self, val, key, default_val):\n \"\"\"wrapper around global getattr(...) method that suppresses any exception raised\"\"\"\n try:\n ret = getattr(val, key, default_val)\n\n except Exception as e:\n logger.exception(e)\n ret = default_val\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the full name of the object", "response": "def _get_name(self, val, src_file, default='Unknown'):\n '''\n get the full namespaced (module + class) name of the val object\n\n since -- 6-28-12\n\n val -- mixed -- the value (everything is an object) object\n default -- string -- the default name if a decent name can't be found programmatically\n\n return -- string -- the full.module.Name\n '''\n module_name = ''\n if src_file:\n module_name = '{}.'.format(self._getattr(val, '__module__', default)).lstrip('.')\n\n class_name = self._getattr(val, '__name__', None)\n if not class_name:\n class_name = default\n cls = self._getattr(val, '__class__', None)\n if cls:\n class_name = self._getattr(cls, '__name__', default)\n\n full_name = \"{}{}\".format(module_name, class_name)\n\n return full_name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the source file path since 7 - 19 - 12 val return the string", "response": "def _get_src_file(self, val, default='Unknown'):\n '''\n return the source file path\n\n since -- 7-19-12\n\n val -- mixed -- the value whose path you want\n\n return -- string -- the path, or something like 'Unknown' if you can't find the path\n '''\n path = default\n\n try:\n # http://stackoverflow.com/questions/6761337/inspect-getfile-vs-inspect-getsourcefile\n # first try and get the actual source file\n source_file = inspect.getsourcefile(val)\n if not source_file:\n # get the raw file since val doesn't have a source file (could be a .pyc or .so file)\n source_file = inspect.getfile(val)\n\n if source_file:\n path = os.path.realpath(source_file)\n\n except TypeError as e:\n path = default\n\n return path"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef makeWidget(self, qscreen: QtGui.QScreen):\n # (re)create the widget, do the same for children\n # how children are placed on the parent widget, depends on the subclass\n self.window = self.ContainerWindow(\n self.signals, self.title, self.parent)\n\n # send to correct x-screen\n self.window.show()\n self.window.windowHandle().setScreen(qscreen)\n self.n_xscreen = self.gpu_handler.getXScreenNum(qscreen) # the correct x-screen number must be passed upstream, to the VideoContainer\n\n # continue window / widget construction in the correct x screen\n self.main_widget = self.ContainerWidget(self.window)\n self.main_layout = QtWidgets.QVBoxLayout(self.main_widget)\n self.window.setCentralWidget(self.main_widget)\n\n # add here any extra turf to the widget you want in addition to the\n # grid\n\n # create the grid\n self.grid_widget = self.GridWidget(self.main_widget)\n self.main_layout.addWidget(self.grid_widget)\n\n self.grid_layout = QtWidgets.QGridLayout(self.grid_widget)\n self.grid_layout.setHorizontalSpacing(2)\n self.grid_layout.setVerticalSpacing(2)\n # ( int left, int top, int right, int bottom )\n self.grid_layout.setContentsMargins(0, 0, 0, 0)\n\n class ScreenMenu(QuickMenu):\n title = \"Change Screen\"\n elements = [\n QuickMenuElement(title=\"Screen 1\"),\n QuickMenuElement(title=\"Screen 2\")\n ]\n \n \"\"\" TODO: activate after gpu-hopping has been debugged\n self.screenmenu = ScreenMenu(self.window)\n self.screenmenu.screen_1.triggered.connect(self.test_slot)\n self.screenmenu.screen_2.triggered.connect(self.test_slot)\n \"\"\"\n\n if (len(self.gpu_handler.true_screens) > 1):\n # so, there's more than a single x screen: create a button for\n # changing x-screens\n self.button = QtWidgets.QPushButton(\n \"Change Screen\", self.main_widget)\n self.main_layout.addWidget(self.button)\n self.button.setSizePolicy(\n QtWidgets.QSizePolicy.Minimum,\n QtWidgets.QSizePolicy.Minimum)\n self.button.clicked.connect(self.change_xscreen_slot)\n\n self.placeChildren()", "response": "create the widget and add it to the main_widget and grid_widget"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef close(self):\n if (self.closed):\n return\n print(self.pre, \"close\")\n for child in self.children:\n child.close()\n self.openglthread = None\n self.gpu_handler = None\n self.closed = True\n self.window.unSetPropagate() # we don't want the window to send the close signal .. which would call this *again* (through close_slot)\n self.window.close()", "response": "Called by the main gui to close the containers."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nserialize information about the widget", "response": "def serialize(self):\n \"\"\"Serialize information about the widget: coordinates, size, which cameras are selected.\n \"\"\"\n ids = []\n for child in self.children:\n device = child.getDevice()\n if device:\n ids.append(device._id) # e.g. DataModel.RTSPCameraDevice._id\n else:\n ids.append(None)\n\n # gather all information to re-construct this RootVideoContainer\n dic = { # these are used when re-instantiating the view\n \"classname\": self.__class__.__name__,\n \"kwargs\": { # parameters that we're used to instantiate this class\n \"title\" : self.title,\n \"n_xscreen\" : self.n_xscreen,\n \"child_class\" : self.child_class,\n \"child_class_pars\" : self.get_child_class_pars() # serialize only relevant child class pars\n }, \n # these parameters are used by deserialize\n \"x\": self.window.x(),\n \"y\": self.window.y(),\n \"width\": self.window.width(),\n \"height\": self.window.height(),\n \"ids\": ids\n }\n\n return dic"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nresetting the analyzer state", "response": "def reset(self):\n \"\"\"Reset analyzer state\n \"\"\"\n self.prevframe = None\n self.wasmoving = False\n self.t0 = 0\n self.ismoving = False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the widget that will be used to display some information about the current locale.", "response": "def getWidget(self):\n \"\"\"Some ideas for your widget:\n - Textual information (alert, license place number)\n - Check boxes : if checked, send e-mail to your mom when the analyzer spots something\n - .. or send an sms to yourself\n - You can include the cv2.imshow window to the widget to see how the analyzer proceeds\n \"\"\"\n widget = QtWidgets.QLabel(\"NO MOVEMENT YET\")\n widget.setStyleSheet(style.detector_test)\n self.signals.start_move.connect(lambda : widget.setText(\"MOVEMENT START\"))\n self.signals.stop_move. connect(lambda : widget.setText(\"MOVEMENT STOP\"))\n return widget"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rule(rules, strict_slashes=False, api_func=None, *args, **kwargs):\n return url_rule(api_blueprint, rules, strict_slashes=strict_slashes,\n view_func=json_api(api_func) if api_func else None,\n *args, **kwargs)", "response": "Add a API route to the blueprint."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfind out which screens are virtual and adds them to self. true_screens", "response": "def findXScreens(self):\n qapp = QtCore.QCoreApplication.instance()\n if not qapp: # QApplication has not been started\n return\n \n screens = qapp.screens()\n \"\"\"\n let's find out which screens are virtual\n\n screen, siblings:\n\n One big virtual desktop:\n\n A [A, B, C]\n B [A, B, C]\n C [A, B, C]\n\n A & B in one xscreen, C in another:\n\n A [A, B]\n B [A, B]\n C [C]\n\n \"\"\"\n virtual_screens = set()\n for screen in screens:\n # if screen has been deemed as \"virtual\", don't check its siblings\n if (screen not in virtual_screens):\n siblings = screen.virtualSiblings()\n # remove the current screen under scrutiny from the siblings\n # list\n virtual_screens.update(set(siblings).difference(set([screen])))\n # .. the ones left over are virtual\n\n # print(\"GPUHandler: findXScreens: virtual screens\",virtual_screens)\n true_screens = list(set(screens) - virtual_screens)\n\n # sort'em\n for screen in true_screens:\n self.true_screens.insert(screens.index(screen), screen)\n\n print(\"GPUHandler: findXScreens: true screens:\", self.true_screens)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef readDB(self):\n self.dm = DataModel(directory = tools.getConfigDir())\n if (self.first_start):\n print(pre, \"readDB : first start\")\n self.dm.clearAll()\n self.dm.saveAll()\n\n # If camera collection is corrupt\n if not self.dm.checkCameraCollection():\n self.dm.clearCameraCollection()", "response": "Reads the data model and saves it to disk"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating some member functions that are needed for the class.", "response": "def generateMethods(self):\n \"\"\"Generate some member functions\n \"\"\"\n for i in range(1, 5):\n # adds member function grid_ixi_slot(self)\n self.make_grid_slot(i, i)\n\n for cl in self.mvision_classes:\n self.make_mvision_slot(cl)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef QCapsulate(self, widget, name, blocking = False, nude = False):\n\n class QuickWindow(QtWidgets.QMainWindow):\n\n class Signals(QtCore.QObject):\n close = QtCore.Signal()\n show = QtCore.Signal()\n\n def __init__(self, blocking = False, parent = None, nude = False):\n super().__init__(parent)\n self.propagate = True # send signals or not\n self.setStyleSheet(style.main_gui)\n if (blocking):\n self.setWindowModality(QtCore.Qt.ApplicationModal)\n if (nude):\n # http://doc.qt.io/qt-5/qt.html#WindowType-enum\n # TODO: create a widget for a proper splashscreen (omitting X11 and centering manually)\n # self.setWindowFlags(QtCore.Qt.Popup) # Qt 5.9+ : setFlags()\n # self.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.WindowStaysOnTopHint)\n self.setWindowFlags(QtCore.Qt.Dialog)\n self.signals = self.Signals()\n \n\n def closeEvent(self, e):\n if (self.propagate):\n self.signals.close.emit()\n e.accept()\n \n def showEvent(self, e):\n if (self.propagate):\n self.signals.show.emit()\n e.accept()\n \n def setPropagate(self):\n self.propagate = True\n \n def unSetPropagate(self):\n self.propagate = False\n \n\n win = QuickWindow(blocking = blocking, nude = nude)\n win.setCentralWidget(widget)\n win.setLayout(QtWidgets.QHBoxLayout())\n win.setWindowTitle(name)\n return win", "response": "Returns a QMainWindow that encapsulates the given QWidget and returns it."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef updateCameraTree(self):\n self.treelist.reset_()\n\n self.server = ServerListItem(\n name = \"Localhost\", ip = \"127.0.0.1\", parent = self.root)\n \"\"\"\n self.server1 = ServerListItem(\n name=\"First Server\", ip=\"192.168.1.20\", parent=self.root)\n \"\"\"\n \"\"\"\n self.camera1 = RTSPCameraListItem(camera=RTSPCameraDevice(\n ip=\"192.168.1.4\", username=\"admin\", password=\"1234\"), parent=self.server1)\n self.camera2 = RTSPCameraListItem(camera=RTSPCameraDevice(\n ip=\"192.168.1.4\", username=\"admin\", password=\"1234\"), parent=self.server1)\n \"\"\"\n devices = []\n\n for row in self.dm.camera_collection.get():\n # print(pre, \"makeCameraTree : row\", row)\n if (row[\"classname\"] == DataModel.RTSPCameraRow.__name__):\n row.pop(\"classname\")\n devices.append(\n RTSPCameraListItem(\n camera = DataModel.RTSPCameraDevice(**row),\n parent = self.server\n )\n )\n elif (row[\"classname\"] == DataModel.USBCameraRow.__name__):\n row.pop(\"classname\")\n devices.append(\n USBCameraListItem(\n camera = DataModel.USBCameraDevice(**row),\n parent = self.server\n )\n )\n \n self.treelist.update()\n self.treelist.expandAll()", "response": "Update the internal list of all the camera entries in the database."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconnects the events to the menu bar and the menu bar.", "response": "def makeLogic(self):\n # *** When camera list has been closed, re-create the cameralist tree and update filterchains ***\n # self.manage_cameras_win.signals.close.connect(self.updateCameraTree) # now put into save_camera_config_slot\n \n # self.manage_cameras_win.signals.close.connect(self.filterchain_group.update) # TODO: use this once fixed\n # self.manage_cameras_win.signals.close.connect(self.filterchain_group.read) # TODO: eh.. lets be sure of this .. (are we releasing slots in the LiveThread etc.)\n \n # self.manage_cameras_win.signals.close.connect(self.save_camera_config_slot)\n # self.manage_memory_container.signals.save.connect(self.save_memory_conf_slot)\n\n # *** Menu bar connections ***\n # the self.filemenu.exit attribute was autogenerated\n self.filemenu.exit. triggered.connect(self.exit_slot)\n self.filemenu.save_window_layout. triggered.connect(\n self.save_window_layout_slot)\n self.filemenu.load_window_layout. triggered.connect(\n self.load_window_layout_slot)\n\n \"\"\"\n self.configmenu.manage_cameras. triggered.connect(\n self.manage_cameras_slot)\n self.configmenu.memory_usage. triggered.connect(\n self.memory_usage_slot)\n \"\"\"\n \n self.configmenu.configuration_dialog.triggered.connect(self.config_dialog_slot)\n \n self.viewmenu.camera_list. triggered.connect(self.camera_list_slot)\n self.aboutmenu.about_valkka_live. triggered.connect(self.about_slot)\n\n # *** Connect autogenerated menu calls into autogenerated slot functions ***\n for i in range(1, 5):\n # gets member function grid_ixi_slot\n slot_func = getattr(self, \"grid_%ix%i_slot\" % (i, i))\n # gets member function grid_ixi from self.viewmenu.video_grid\n menu_func = getattr(self.viewmenu.video_grid,\n \"grid_%ix%i\" % (i, i))\n menu_func.triggered.connect(slot_func)\n # i.e., like this : self.viewmenu.video_grid.grid_1x1.triggered.connect(slot_func)\n\n\n # *** autogenerated machine vision menu and slots ***\n for cl in self.mvision_classes:\n getattr(self.mvisionmenu,cl.name).triggered.connect(getattr(self,cl.name+\"_slot\"))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef serializeContainers(self):\n\n \"\"\" each serialized container looks like this:\n dic={# these are used when re-instantiating the view\n \"classname\" : self.__class__.__name__,\n \"kwargs\" : {}, # parameters that we're used to instantiate this class\n # these parameters are used by deserialize\n \"x\" : self.window.x(),\n \"y\" : self.window.y(),\n \"width\" : self.window.width(),\n \"height\" : self.window.height(),\n \"streams\" : streams\n }\n \"\"\"\n container_list = []\n mvision_container_list = []\n \n for container in self.containers:\n print(\"gui: serialize containers : container=\", container)\n container_list.append(container.serialize())\n \n for container in self.mvision_containers:\n mvision_container_list.append(container.serialize())\n \n return {\"container_list\" : container_list, \"mvision_container_list\" : mvision_container_list}", "response": "Serializes the current view of open video grids"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef startProcesses(self):\n self.process_map = {} # each key is a list of started multiprocesses\n # self.process_avail = {} # count instances\n \n for mvision_class in self.mvision_classes:\n name = mvision_class.name\n tag = mvision_class.tag\n num = mvision_class.max_instances \n if (tag not in self.process_map):\n self.process_map[tag] = []\n # self.process_avail[tag] = num\n for n in range(0, num):\n p = mvision_class()\n p.start()\n self.process_map[tag].append(p)", "response": "Create and start python multiprocesses\nAttributeNames\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef make_grid_slot(self, n, m):\n def slot_func():\n cont = container.VideoContainerNxM(gpu_handler=self.gpu_handler, \n filterchain_group=self.filterchain_group, \n n_dim=n, \n m_dim=m)\n cont.signals.closing.connect(self.rem_container_slot)\n self.containers.append(cont)\n setattr(self, \"grid_%ix%i_slot\" % (n, m), slot_func)", "response": "Create a n x m video grid and add it to the list of video containers that are used by the grid."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmapping to pout.inject on the command line, makes it easy to make pout global without having to actually import it in your python environment .. since:: 2018-08-13 :param args: Namespace, the parsed CLI arguments passed into the application :returns: int, the return code of the CLI", "response": "def main_inject(args):\n \"\"\"\n mapped to pout.inject on the command line, makes it easy to make pout global\n without having to actually import it in your python environment\n\n .. since:: 2018-08-13\n\n :param args: Namespace, the parsed CLI arguments passed into the application\n :returns: int, the return code of the CLI\n \"\"\"\n ret = 0\n\n try:\n filepath = SiteCustomizeFile()\n if filepath.is_injected():\n logger.info(\"Pout has already been injected into {}\".format(filepath))\n\n else:\n if filepath.inject():\n logger.info(\"Injected pout into {}\".format(filepath))\n else:\n logger.info(\"Failed to inject pout into {}\".format(filepath))\n\n except IOError as e:\n ret = 1\n logger.info(str(e))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef main_info(args):\n if args.site_packages:\n logger.info(SitePackagesDir())\n\n else:\n logger.info(\"Python executable: {}\".format(sys.executable))\n logger.info(\"Python version: {}\".format(platform.python_version()))\n logger.info(\"Python site-packages: {}\".format(SitePackagesDir()))\n logger.info(\"Python sitecustomize: {}\".format(SiteCustomizeFile()))\n # https://stackoverflow.com/questions/4152963/get-the-name-of-current-script-with-python\n #logger.info(\"Pout executable: {}\".format(subprocess.check_output([\"which\", \"pout\"])))\n logger.info(\"Pout executable: {}\".format(os.path.abspath(os.path.expanduser(str(sys.argv[0])))))\n logger.info(\"Pout version: {}\".format(pout.__version__))\n\n filepath = SiteCustomizeFile()\n logger.info(\"Pout injected: {}\".format(filepath.is_injected()))", "response": "Prints out info about the pout installation\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalling the supplied function with the supplied arguments catching and returning any exception that it throws.", "response": "def catch(func, *args, **kwargs):\n \"\"\"\n Call the supplied function with the supplied arguments,\n catching and returning any exception that it throws.\n\n Arguments:\n func: the function to run.\n *args: positional arguments to pass into the function.\n **kwargs: keyword arguments to pass into the function.\n Returns:\n If the function throws an exception, return the exception.\n If the function does not throw an exception, return None.\n \"\"\"\n try:\n func(*args, **kwargs)\n except Exception as e:\n return e"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef time(func, *args, **kwargs):\n start_time = time_module.time()\n func(*args, **kwargs)\n end_time = time_module.time()\n return end_time - start_time", "response": "This function is used to get the execution time of a function in the order of execution time."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstarting a Pdb instance at the calling frame with stdout routed to sys. stdout__.", "response": "def set_trace():\n \"\"\"Start a Pdb instance at the calling frame, with stdout routed to sys.__stdout__.\"\"\"\n # https://github.com/nose-devs/nose/blob/master/nose/tools/nontrivial.py\n pdb.Pdb(stdout=sys.__stdout__).set_trace(sys._getframe().f_back)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef lmom_fit(self, data=[], lmom_ratios=[]):\n n_min = self.numargs + 2\n if len(data) > 0:\n if len(data) <= n_min:\n raise ValueError(\"At least {} data points must be provided.\".format(n_min))\n lmom_ratios = lm.lmom_ratios(data, nmom=n_min)\n elif not lmom_ratios:\n raise Exception(\"Either `data` or `lmom_ratios` must be provided.\")\n elif len(lmom_ratios) < n_min:\n raise ValueError(\"At least {} number of L-moments must be provided.\".format(n_min))\n\n return self._lmom_fit(lmom_ratios)", "response": "Fit the distribution function to the given data or given L - moments."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompute the distribution s L - moments e. g. l1 l2 l3 l4 l5...", "response": "def lmom(self, *args, nmom=5, **kwds):\n \"\"\"\n Compute the distribution's L-moments, e.g. l1, l2, l3, l4, ..\n\n :param args: Distribution parameters in order of shape(s), loc, scale\n :type args: float\n :param nmom: Number of moments to calculate\n :type nmom: int\n :param kwds: Distribution parameters as named arguments. See :attr:`rv_continous.shapes` for names of shape\n parameters\n :type kwds: float\n :returns: List of L-moments\n :rtype: list\n \"\"\"\n ratios = self.lmom_ratios(*args, nmom=nmom, **kwds)\n moments = ratios[0:2]\n moments += [ratio * moments[1] for ratio in ratios[2:]]\n return moments"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef lmom_ratios(self, *args, nmom=5, **kwds):\n if nmom > 20:\n return ValueError(\"Parameter nmom too large. Max of 20.\")\n\n shapes, loc, scale = self._parse_args(*args, **kwds)\n\n if scale <= 0:\n return ValueError(\"Invalid scale parameter.\")\n\n return self._lmom_ratios(*shapes, loc=loc, scale=scale, nmom=nmom)", "response": "Compute the distribution s L - moment ratios e. g. l1 l2 t3 t4..."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef run():\n plugin_list = load_plugins()\n\n module = sys.modules['__main__']\n plugin_list.insert(0, ObjectSupplier(module))\n\n return run_with_plugins(plugin_list)", "response": "Run all the test classes in the main module."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncarrying out a test run with the supplied list of plugin instances.", "response": "def run_with_plugins(plugin_list):\n \"\"\"\n Carry out a test run with the supplied list of plugin instances.\n The plugins are expected to identify the object to run.\n\n Parameters:\n plugin_list: a list of plugin instances (objects which implement some subset of PluginInterface)\n Returns: exit code as an integer.\n The default behaviour (which may be overridden by plugins) is to return a 0\n exit code if the test run succeeded, and 1 if it failed.\n \"\"\"\n composite = core.PluginComposite(plugin_list)\n\n to_run = composite.get_object_to_run()\n\n test_run = core.TestRun(to_run, composite)\n test_run.run()\n\n return composite.get_exit_code()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef initialise(self, args=None, env=None, file=None, cwd=None):\n self.spec_file = args and args.specs or None\n self.cwd = cwd or os.getcwd()\n self.file = file\n return self.spec_file is not None", "response": "Initializes the object with the given arguments."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nestimates nmom number of L - moments from a sample data.", "response": "def lmom_ratios(data, nmom=5):\n \"\"\"\n Estimate `nmom` number of L-moments from a sample `data`.\n\n :param data: Sequence of (sample) data\n :type data: list or array-like sequence\n :param nmom: number of L-moments to estimate\n :type nmom: int\n :return: L-moment ratios like this: l1, l2, t3, t4, t5, .. . As in: items 3 and higher are L-moment ratios.\n :rtype: list\n \"\"\"\n\n if nmom <= 5:\n return _samlmusmall(data, nmom)\n else:\n return _samlmularge(data, nmom)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmaking a request to the API and return the response.", "response": "def _run(self, url_path, headers=None, **kwargs):\n \"\"\"\n Requests API\n \"\"\"\n url = self._construct_url(url_path)\n\n payload = kwargs\n payload.update({'api_token': self.api_token})\n\n return self._make_request(url, payload, headers)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nkeeps for backwards compatibility of this client See \"self.clear_reference_language\"", "response": "def _apiv1_run(self, action, headers=None, **kwargs):\n \"\"\"\n Kept for backwards compatibility of this client\n See \"self.clear_reference_language\"\n \"\"\"\n warnings.warn(\n \"POEditor API v1 is deprecated. Use POEditorAPI._run method to call API v2\",\n DeprecationWarning, stacklevel=2\n )\n\n url = \"https://poeditor.com/api/\"\n payload = kwargs\n payload.update({'action': action, 'api_token': self.api_token})\n\n return self._make_request(url, payload, headers)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nformats the data returned by the detail view", "response": "def _project_formatter(self, data):\n \"\"\"\n Project object\n \"\"\"\n open_ = False if not data['open'] or data['open'] == '0' else True\n public = False if not data['public'] or data['public'] == '0' else True\n output = {\n 'created': parse_datetime(data['created']),\n 'id': int(data['id']),\n 'name': data['name'],\n 'open': open_,\n 'public': public,\n }\n\n # the detail view returns more info than the list view\n # see https://poeditor.com/docs/api#projects_view\n for key in ['description', 'reference_language', 'terms']:\n if key in data:\n output[key] = data[key]\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the list of projects owned by user.", "response": "def list_projects(self):\n \"\"\"\n Returns the list of projects owned by user.\n \"\"\"\n data = self._run(\n url_path=\"projects/list\"\n )\n projects = data['result'].get('projects', [])\n return [self._project_formatter(item) for item in projects]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a new project. Returns the id of the project.", "response": "def create_project(self, name, description=None):\n \"\"\"\n creates a new project. Returns the id of the project (if successful)\n \"\"\"\n description = description or ''\n data = self._run(\n url_path=\"projects/add\",\n name=name,\n description=description\n )\n return data['result']['project']['id']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the project settings.", "response": "def update_project(self, project_id, name=None, description=None,\n reference_language=None):\n \"\"\"\n Updates project settings (name, description, reference language)\n If optional parameters are not sent, their respective fields are not updated.\n \"\"\"\n kwargs = {}\n if name is not None:\n kwargs['name'] = name\n if description is not None:\n kwargs['description'] = description\n if reference_language is not None:\n kwargs['reference_language'] = reference_language\n\n data = self._run(\n url_path=\"projects/update\",\n id=project_id,\n **kwargs\n )\n return data['result']['project']['id']"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef view_project_details(self, project_id):\n data = self._run(\n url_path=\"projects/view\",\n id=project_id\n )\n return self._project_formatter(data['result']['project'])", "response": "Returns project s details."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn project languages percentage of translation done for each and the datetime when the last change was made.", "response": "def list_project_languages(self, project_id):\n \"\"\"\n Returns project languages, percentage of translation done for each and the\n datetime (UTC - ISO 8601) when the last change was made.\n \"\"\"\n data = self._run(\n url_path=\"languages/list\",\n id=project_id\n )\n return data['result'].get('languages', [])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a new language to a project", "response": "def add_language_to_project(self, project_id, language_code):\n \"\"\"\n Adds a new language to project\n \"\"\"\n self._run(\n url_path=\"languages/add\",\n id=project_id,\n language=language_code\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the project terms. Lets you change the text context reference plural and tags. Lets you change the text context reference plural and tags. Lets you change the text context reference plural and tags. Lets you change the text context reference plural and tags.", "response": "def update_terms(self, project_id, data, fuzzy_trigger=None):\n \"\"\"\n Updates project terms. Lets you change the text, context, reference, plural and tags.\n\n >>> data = [\n {\n \"term\": \"Add new list\",\n \"context\": \"\",\n \"new_term\": \"Save list\",\n \"new_context\": \"\",\n \"reference\": \"\\/projects\",\n \"plural\": \"\",\n \"comment\": \"\",\n \"tags\": [\n \"first_tag\",\n \"second_tag\"\n ]\n },\n {\n \"term\": \"Display list\",\n \"context\": \"\",\n \"new_term\": \"Show list\",\n \"new_context\": \"\"\n }\n ]\n \"\"\"\n kwargs = {}\n if fuzzy_trigger is not None:\n kwargs['fuzzy_trigger'] = fuzzy_trigger\n\n data = self._run(\n url_path=\"terms/update\",\n id=project_id,\n data=json.dumps(data),\n **kwargs\n )\n return data['result']['terms']"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd comments to existing terms.", "response": "def add_comment(self, project_id, data):\n \"\"\"\n Adds comments to existing terms.\n >>> data = [\n {\n \"term\": \"Add new list\",\n \"context\": \"\",\n \"comment\": \"This is a button\"\n },\n {\n \"term\": \"one project found\",\n \"context\": \"\",\n \"comment\": \"Make sure you translate the plural forms\"\n },\n {\n \"term\": \"Show all projects\",\n \"context\": \"\",\n \"comment\": \"This is a button\"\n }\n ]\n \"\"\"\n data = self._run(\n url_path=\"terms/add_comment\",\n id=project_id,\n data=json.dumps(data)\n )\n return data['result']['terms']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the language of a project.", "response": "def update_project_language(self, project_id, language_code, data, fuzzy_trigger=None):\n \"\"\"\n Inserts / overwrites translations.\n >>> data = [\n {\n \"term\": \"Projects\",\n \"context\": \"project list\",\n \"translation\": {\n \"content\": \"Des projets\",\n \"fuzzy\": 0\n }\n }\n ]\n \"\"\"\n kwargs = {}\n if fuzzy_trigger is not None:\n kwargs['fuzzy_trigger'] = fuzzy_trigger\n\n data = self._run(\n url_path=\"languages/update\",\n id=project_id,\n language=language_code,\n data=json.dumps(data),\n **kwargs\n )\n return data['result']['translations']"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef export(self, project_id, language_code, file_type='po', filters=None,\n tags=None, local_file=None):\n \"\"\"\n Return terms / translations\n\n filters - filter by self._filter_by\n tags - filter results by tags;\n local_file - save content into it. If None, save content into\n random temp file.\n\n >>> tags = 'name-of-tag'\n >>> tags = [\"name-of-tag\"]\n >>> tags = [\"name-of-tag\", \"name-of-another-tag\"]\n\n >>> filters = 'translated'\n >>> filters = [\"translated\"]\n >>> filters = [\"translated\", \"not_fuzzy\"]\n \"\"\"\n if file_type not in self.FILE_TYPES:\n raise POEditorArgsException(\n 'content_type: file format {}'.format(self.FILE_TYPES))\n\n if filters and isinstance(filters, str) and filters not in self.FILTER_BY:\n raise POEditorArgsException(\n \"filters - filter results by {}\".format(self.FILTER_BY))\n elif filters and set(filters).difference(set(self.FILTER_BY)):\n raise POEditorArgsException(\n \"filters - filter results by {}\".format(self.FILTER_BY))\n\n data = self._run(\n url_path=\"projects/export\",\n id=project_id,\n language=language_code,\n type=file_type,\n filters=filters,\n tags=tags\n )\n # The link of the file (expires after 10 minutes).\n file_url = data['result']['url']\n\n # Download file content:\n res = requests.get(file_url, stream=True)\n if not local_file:\n tmp_file = tempfile.NamedTemporaryFile(\n delete=False, suffix='.{}'.format(file_type))\n tmp_file.close()\n local_file = tmp_file.name\n\n with open(local_file, 'w+b') as po_file:\n for data in res.iter_content(chunk_size=1024):\n po_file.write(data)\n return file_url, local_file", "response": "Export the terms and translations of a single language code into a single file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _upload(self, project_id, updating, file_path, language_code=None,\n overwrite=False, sync_terms=False, tags=None, fuzzy_trigger=None):\n \"\"\"\n Internal: updates terms / translations\n\n File uploads are limited to one every 30 seconds\n \"\"\"\n options = [\n self.UPDATING_TERMS,\n self.UPDATING_TERMS_TRANSLATIONS,\n self.UPDATING_TRANSLATIONS\n ]\n if updating not in options:\n raise POEditorArgsException(\n 'Updating arg must be in {}'.format(options)\n )\n\n options = [\n self.UPDATING_TERMS_TRANSLATIONS,\n self.UPDATING_TRANSLATIONS\n ]\n if language_code is None and updating in options:\n raise POEditorArgsException(\n 'Language code is required only if updating is '\n 'terms_translations or translations)'\n )\n\n if updating == self.UPDATING_TRANSLATIONS:\n tags = None\n sync_terms = None\n\n # Special content type:\n tags = tags or ''\n language_code = language_code or ''\n sync_terms = '1' if sync_terms else '0'\n overwrite = '1' if overwrite else '0'\n fuzzy_trigger = '1' if fuzzy_trigger else '0'\n project_id = str(project_id)\n\n with open(file_path, 'r+b') as local_file:\n data = self._run(\n url_path=\"projects/upload\",\n id=project_id,\n language=language_code,\n file=local_file,\n updating=updating,\n tags=tags,\n sync_terms=sync_terms,\n overwrite=overwrite,\n fuzzy_trigger=fuzzy_trigger\n )\n return data['result']", "response": "Upload a file to the ACS Academy."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_terms(self, project_id, file_path=None, language_code=None,\n overwrite=False, sync_terms=False, tags=None, fuzzy_trigger=None):\n \"\"\"\n Updates terms\n\n overwrite: set it to True if you want to overwrite translations\n sync_terms: set it to True if you want to sync your terms (terms that\n are not found in the uploaded file will be deleted from project\n and the new ones added). Ignored if updating = translations\n tags: Add tags to the project terms; available when updating terms or terms_translations;\n you can use the following keys: \"all\" - for the all the imported terms, \"new\" - for\n the terms which aren't already in the project, \"obsolete\" - for the terms which are\n in the project but not in the imported file and \"overwritten_translations\" - for the\n terms for which translations change\n fuzzy_trigger: set it to True to mark corresponding translations from the\n other languages as fuzzy for the updated values\n \"\"\"\n return self._upload(\n project_id=project_id,\n updating=self.UPDATING_TERMS,\n file_path=file_path,\n language_code=language_code,\n overwrite=overwrite,\n sync_terms=sync_terms,\n tags=tags,\n fuzzy_trigger=fuzzy_trigger\n )", "response": "Update the set of terms that are not already in the project."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_terms_translations(self, project_id, file_path=None,\n language_code=None, overwrite=False,\n sync_terms=False, tags=None, fuzzy_trigger=None):\n \"\"\"\n Updates terms translations\n\n overwrite: set it to True if you want to overwrite translations\n sync_terms: set it to True if you want to sync your terms (terms that\n are not found in the uploaded file will be deleted from project\n and the new ones added). Ignored if updating = translations\n tags: Add tags to the project terms; available when updating terms or terms_translations;\n you can use the following keys: \"all\" - for the all the imported terms, \"new\" - for\n the terms which aren't already in the project, \"obsolete\" - for the terms which are\n in the project but not in the imported file and \"overwritten_translations\" - for the\n terms for which translations change\n fuzzy_trigger: set it to True to mark corresponding translations from the\n other languages as fuzzy for the updated values\n \"\"\"\n return self._upload(\n project_id=project_id,\n updating=self.UPDATING_TERMS_TRANSLATIONS,\n file_path=file_path,\n language_code=language_code,\n overwrite=overwrite,\n sync_terms=sync_terms,\n tags=tags,\n fuzzy_trigger=fuzzy_trigger\n )", "response": "Updates the terms translations for the specified project."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the translations of the current language for the current language.", "response": "def update_translations(self, project_id, file_path=None,\n language_code=None, overwrite=False, fuzzy_trigger=None):\n \"\"\"\n Updates translations\n\n overwrite: set it to True if you want to overwrite definitions\n fuzzy_trigger: set it to True to mark corresponding translations from the\n other languages as fuzzy for the updated values\n \"\"\"\n return self._upload(\n project_id=project_id,\n updating=self.UPDATING_TRANSLATIONS,\n file_path=file_path,\n language_code=language_code,\n overwrite=overwrite,\n fuzzy_trigger=fuzzy_trigger\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the list of contributors in the specified project.", "response": "def list_contributors(self, project_id=None, language_code=None):\n \"\"\"\n Returns the list of contributors\n \"\"\"\n data = self._run(\n url_path=\"contributors/list\",\n id=project_id,\n language=language_code\n )\n return data['result'].get('contributors', [])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a contributor to a project language_code", "response": "def add_contributor(self, project_id, name, email, language_code):\n \"\"\"\n Adds a contributor to a project language\n \"\"\"\n self._run(\n url_path=\"contributors/add\",\n id=project_id,\n name=name,\n email=email,\n language=language_code\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd a contributor to a project language", "response": "def add_administrator(self, project_id, name, email):\n \"\"\"\n Adds a contributor to a project language\n \"\"\"\n self._run(\n url_path=\"contributors/add\",\n id=project_id,\n name=name,\n email=email,\n admin=True\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remove_contributor(self, project_id, email, language):\n self._run(\n url_path=\"contributors/remove\",\n id=project_id,\n email=email,\n language=language\n )\n return True", "response": "Removes a contributor from the cache."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_stations(html):\n html = html.replace('SLs.sls=', '').replace(';SLs.showSuggestion();', '')\n html = json.loads(html)\n return html['suggestions']", "response": "Parses the stations from the given HTML."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse the delay from the details view and save the parsed data", "response": "def parse_delay(data):\n \"\"\"\n Prase the delay\n \"\"\"\n # parse data from the details view\n rsp = requests.get(data['details'])\n soup = BeautifulSoup(rsp.text, \"html.parser\")\n\n # get departure delay\n delay_departure_raw = soup.find('div', class_=\"routeStart\").find('span', class_=[\"delay\", \"delayOnTime\"])\n if delay_departure_raw:\n delay_departure = calculate_delay(data['departure'],\n delay_departure_raw.text)\n else:\n delay_departure = 0\n\n # get arrival delay\n delay_arrival_raw = soup.find('div', class_=[\"routeEnd\",\"routeEndAdditional\"]).find('span', class_=[\"delay\", \"delayOnTime\"])\n if delay_arrival_raw:\n delay_arrival = calculate_delay(data['arrival'],\n delay_arrival_raw.text)\n else:\n delay_arrival = 0\n\n # save the parsed data\n if delay_departure + delay_arrival == 0:\n data['ontime'] = True\n else:\n data['ontime'] = False\n data['delay'] = {\n 'delay_departure': int(delay_departure),\n 'delay_arrival': int(delay_arrival)\n }\n\n # TODO: this should not be hardcoded!\n data['canceled'] = False\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncalculating the delay between original and delayed", "response": "def calculate_delay(original, delay):\n \"\"\"\n Calculate the delay\n \"\"\"\n original = datetime.strptime(original, '%H:%M')\n delayed = datetime.strptime(delay, '%H:%M')\n diff = delayed - original\n return diff.total_seconds() // 60"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef stations(self, station, limit=10):\n query = {\n 'start': 1,\n 'S': station + '?',\n 'REQ0JourneyStopsB': limit\n }\n rsp = requests.get('http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn', params=query)\n return parse_stations(rsp.text)", "response": "Returns a list of stations for a given station"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nfinds all connections between two stations", "response": "def connections(self, origin, destination, dt=datetime.now(), only_direct=False):\n \"\"\"\n Find connections between two stations\n\n Args:\n origin (str): origin station\n destination (str): destination station\n dt (datetime): date and time for query\n only_direct (bool): only direct connections\n \"\"\"\n query = {\n 'S': origin,\n 'Z': destination,\n 'date': dt.strftime(\"%d.%m.%y\"),\n 'time': dt.strftime(\"%H:%M\"),\n 'start': 1,\n 'REQ0JourneyProduct_opt0': 1 if only_direct else 0\n }\n rsp = requests.get('http://mobile.bahn.de/bin/mobil/query.exe/dox?', params=query)\n return parse_connections(rsp.text)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nscattering elements of sequence into n blocks.", "response": "def _scatter(sequence, n):\n \"\"\"Scatters elements of ``sequence`` into ``n`` blocks.\"\"\"\n chunklen = int(math.ceil(float(len(sequence)) / float(n)))\n\n return [\n sequence[ i*chunklen : (i+1)*chunklen ] for i in range(n)\n ]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef lease(self, seconds=600, num_tasks=1, tag=None):\n tag = tag if tag else None\n tasks = self._api.lease(\n numTasks=num_tasks, \n seconds=seconds,\n groupByTag=(tag is not None),\n tag=tag,\n )\n\n if not len(tasks):\n raise QueueEmpty\n\n task = tasks[0]\n return totask(task)", "response": "Acquires a lease on the topmost N unowned tasks in the specified queue."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef purge(self):\n try:\n return self._api.purge()\n except AttributeError:\n while True:\n lst = self.list()\n if len(lst) == 0:\n break\n\n for task in lst:\n self.delete(task)\n self.wait()\n return self", "response": "Deletes all tasks in the queue."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef poll(\n self, lease_seconds=LEASE_SECONDS, tag=None, \n verbose=False, execute_args=[], execute_kwargs={}, \n stop_fn=None, backoff_exceptions=[], min_backoff_window=30, \n max_backoff_window=120, log_fn=None\n ):\n \"\"\"\n Poll a queue until a stop condition is reached (default forever). Note\n that this function is not thread safe as it requires a global variable\n to intercept SIGINT.\n lease_seconds: each task should be leased for this many seconds\n tag: if specified, query for only tasks that match this tag\n execute_args / execute_kwargs: pass these arguments to task execution\n backoff_exceptions: A list of exceptions that instead of causing a crash,\n instead cause the polling to back off for an increasing exponential \n random window.\n min_backoff_window: The minimum sized window (in seconds) to select a \n random backoff time. \n max_backoff_window: The window doubles each retry. This is the maximum value\n in seconds.\n stop_fn: A boolean returning function that accepts no parameters. When\n it returns True, the task execution loop will terminate. It is evaluated\n once after every task.\n log_fn: Feed error messages to this function, default print (when verbose is enabled).\n verbose: print out the status of each step\n Return: number of tasks executed\n \"\"\"\n global LOOP\n\n if not callable(stop_fn) and stop_fn is not None:\n raise ValueError(\"stop_fn must be a callable. \" + str(stop_fn))\n elif not callable(stop_fn):\n stop_fn = lambda: False\n\n def random_exponential_window_backoff(n):\n n = min(n, min_backoff_window)\n # 120 sec max b/c on avg a request every ~250msec if 500 containers \n # in contention which seems like a quite reasonable volume of traffic \n # to handle\n high = min(2 ** n, max_backoff_window) \n return random.uniform(0, high)\n\n def printv(*args, **kwargs):\n if verbose:\n print(*args, **kwargs)\n\n LOOP = True\n\n def sigint_handler(signum, frame):\n global LOOP\n printv(\"Interrupted. Exiting after this task completes...\")\n LOOP = False\n \n prev_sigint_handler = signal.getsignal(signal.SIGINT)\n signal.signal(signal.SIGINT, sigint_handler)\n\n if log_fn is None:\n log_fn = printv\n\n tries = 0\n executed = 0\n\n backoff = False\n backoff_exceptions = tuple(list(backoff_exceptions) + [ QueueEmpty ])\n\n while LOOP:\n task = 'unknown' # for error message prior to leasing\n try:\n task = self.lease(seconds=int(lease_seconds))\n tries += 1\n printv(task)\n task.execute(*execute_args, **execute_kwargs)\n executed += 1\n printv(\"Delete enqueued task...\")\n self.delete(task)\n log_fn('INFO', task , \"succesfully executed\")\n tries = 0\n except backoff_exceptions:\n backoff = True\n except Exception as e:\n printv('ERROR', task, \"raised {}\\n {}\".format(e , traceback.format_exc()))\n raise #this will restart the container in kubernetes\n \n if stop_fn():\n break\n\n if backoff:\n time.sleep(random_exponential_window_backoff(tries))\n\n backoff = False\n\n printv(\"Task execution loop exited.\")\n signal.signal(signal.SIGINT, prev_sigint_handler)\n\n return executed", "response": "Poll a queue until a stop condition is reached."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef insert(self, task, args=[], kwargs={}, delay_seconds=0):\n body = {\n \"payload\": task.payload(),\n \"queueName\": self._queue_name,\n \"groupByTag\": True,\n \"tag\": task.__class__.__name__\n }\n\n def cloud_insertion(api):\n api.insert(body, delay_seconds)\n\n if len(self._threads):\n self.put(cloud_insertion)\n else:\n cloud_insertion(self._api)\n\n return self", "response": "Insert a task into an existing queue."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndelete a task from a TaskQueue.", "response": "def delete(self, task_id):\n \"\"\"Deletes a task from a TaskQueue.\"\"\"\n if isinstance(task_id, RegisteredTask):\n task_id = task_id.id\n\n def cloud_delete(api):\n api.delete(task_id)\n\n if len(self._threads):\n self.put(cloud_delete)\n else:\n cloud_delete(self._api)\n\n return self"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef insert(self, task, args=[], kwargs={}, delay_seconds=0):\n body = {\n \"payload\": task.payload(),\n \"queueName\": self._queue_name,\n \"groupByTag\": True,\n \"tag\": task.__class__.__name__\n }\n\n def cloud_insertion():\n self._api.insert(body, delay_seconds)\n\n self._pool.spawn(cloud_insertion)\n\n return self", "response": "Insert a task into the queue."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck if this is a user upload from the user", "response": "def is_upload(action):\n \"\"\"Checks if this should be a user upload\n\n :param action:\n :return: True if this is a file we intend to upload from the user\n \"\"\"\n return 'r' in action.type._mode and (action.default is None or\n getattr(action.default, 'name') not in (sys.stderr.name, sys.stdout.name))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef to_django(self):\n exclude = {'name', 'model'}\n field_module = 'models'\n django_kwargs = {}\n if self.node_attrs['model'] == 'CharField':\n django_kwargs['max_length'] = 255\n django_kwargs['blank'] = not self.node_attrs['required']\n try:\n django_kwargs['default'] = self.node_attrs['value']\n except KeyError:\n pass\n return u'{0} = {1}.{2}({3})'.format(self.node_attrs['name'], field_module, self.node_attrs['model'],\n ', '.join(['{0}={1}'.format(i,v) for i,v in six.iteritems(django_kwargs)]),)", "response": "Convert the node attributes to a django representation"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a modified dict where all the keys that are anything but str get converted to str.", "response": "def str_dict_keys(a_dict):\n \"\"\"return a modified dict where all the keys that are anything but str get\n converted to str.\n E.g.\n\n >>> result = str_dict_keys({u'name': u'Peter', u'age': 99, 1: 2})\n >>> # can't compare whole dicts in doctests\n >>> result['name']\n u'Peter'\n >>> result['age']\n 99\n >>> result[1]\n 2\n\n The reason for this is that in Python <= 2.6.4 doing\n ``MyClass(**{u'name': u'Peter'})`` would raise a TypeError\n\n Note that only unicode types are converted to str types.\n The reason for that is you might have a class that looks like this::\n\n class Option(object):\n def __init__(self, foo=None, bar=None, **kwargs):\n ...\n\n And it's being used like this::\n\n Option(**{u'foo':1, u'bar':2, 3:4})\n\n Then you don't want to change that {3:4} part which becomes part of\n `**kwargs` inside the __init__ method.\n Using integers as parameter keys is a silly example but the point is that\n due to the python 2.6.4 bug only unicode keys are converted to str.\n \"\"\"\n new_dict = {}\n for key in a_dict:\n if six.PY2 and isinstance(key, six.text_type):\n new_dict[str(key)] = a_dict[key]\n else:\n new_dict[key] = a_dict[key]\n return new_dict"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef str_to_boolean(input_str):\n if not isinstance(input_str, six.string_types):\n raise ValueError(input_str)\n input_str = str_quote_stripper(input_str)\n return input_str.lower() in (\"true\", \"t\", \"1\", \"y\", \"yes\")", "response": "a conversion function for boolean\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef str_to_python_object(input_str):\n if not input_str:\n return None\n if six.PY3 and isinstance(input_str, six.binary_type):\n input_str = to_str(input_str)\n if not isinstance(input_str, six.string_types):\n # gosh, we didn't get a string, we can't convert anything but strings\n # we're going to assume that what we got is actually what was wanted\n # as the output\n return input_str\n input_str = str_quote_stripper(input_str)\n if '.' not in input_str and input_str in known_mapping_str_to_type:\n return known_mapping_str_to_type[input_str]\n parts = [x.strip() for x in input_str.split('.') if x.strip()]\n try:\n try:\n # first try as a complete module\n package = __import__(input_str)\n except ImportError:\n # it must be a class from a module\n if len(parts) == 1:\n # since it has only one part, it must be a class from __main__\n parts = ('__main__', input_str)\n package = __import__('.'.join(parts[:-1]), globals(), locals(), [])\n obj = package\n for name in parts[1:]:\n obj = getattr(obj, name)\n return obj\n except AttributeError as x:\n raise CannotConvertError(\"%s cannot be found\" % input_str)\n except ImportError as x:\n raise CannotConvertError(str(x))", "response": "a conversion that will import a module and class name\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert a comma delimited list of class names into actual classes within a numbered namespace", "response": "def str_to_classes_in_namespaces(\n template_for_namespace=\"cls%d\",\n name_of_class_option='cls',\n instantiate_classes=False\n):\n \"\"\"take a comma delimited list of class names, convert each class name\n into an actual class as an option within a numbered namespace. This\n function creates a closure over a new function. That new function,\n in turn creates a class derived from RequiredConfig. The inner function,\n 'class_list_converter', populates the InnerClassList with a Namespace for\n each of the classes in the class list. In addition, it puts the each class\n itself into the subordinate Namespace. The requirement discovery mechanism\n of configman then reads the InnerClassList's requried config, pulling in\n the namespaces and associated classes within.\n\n For example, if we have a class list like this: \"Alpha, Beta\", then this\n converter will add the following Namespaces and options to the\n configuration:\n\n \"cls0\" - the subordinate Namespace for Alpha\n \"cls0.cls\" - the option containing the class Alpha itself\n \"cls1\" - the subordinate Namespace for Beta\n \"cls1.cls\" - the option containing the class Beta itself\n\n Optionally, the 'class_list_converter' inner function can embue the\n InnerClassList's subordinate namespaces with aggregates that will\n instantiate classes from the class list. This is a convenience to the\n programmer who would otherwise have to know ahead of time what the\n namespace names were so that the classes could be instantiated within the\n context of the correct namespace. Remember the user could completely\n change the list of classes at run time, so prediction could be difficult.\n\n \"cls0\" - the subordinate Namespace for Alpha\n \"cls0.cls\" - the option containing the class Alpha itself\n \"cls0.cls_instance\" - an instance of the class Alpha\n \"cls1\" - the subordinate Namespace for Beta\n \"cls1.cls\" - the option containing the class Beta itself\n \"cls1.cls_instance\" - an instance of the class Beta\n\n parameters:\n template_for_namespace - a template for the names of the namespaces\n that will contain the classes and their\n associated required config options. The\n namespaces will be numbered sequentially. By\n default, they will be \"cls1\", \"cls2\", etc.\n class_option_name - the name to be used for the class option within\n the nested namespace. By default, it will choose:\n \"cls1.cls\", \"cls2.cls\", etc.\n instantiate_classes - a boolean to determine if there should be an\n aggregator added to each namespace that\n instantiates each class. If True, then each\n Namespace will contain elements for the class, as\n well as an aggregator that will instantiate the\n class.\n \"\"\"\n\n # these are only used within this method. No need to pollute the module\n # scope with them and avoid potential circular imports\n from configman.namespace import Namespace\n from configman.required_config import RequiredConfig\n\n #--------------------------------------------------------------------------\n def class_list_converter(class_list_str):\n \"\"\"This function becomes the actual converter used by configman to\n take a string and convert it into the nested sequence of Namespaces,\n one for each class in the list. It does this by creating a proxy\n class stuffed with its own 'required_config' that's dynamically\n generated.\"\"\"\n if isinstance(class_list_str, six.string_types):\n class_list = [x.strip() for x in class_list_str.split(',')]\n if class_list == ['']:\n class_list = []\n else:\n raise TypeError('must be derivative of %s' % six.string_types)\n\n #======================================================================\n class InnerClassList(RequiredConfig):\n \"\"\"This nested class is a proxy list for the classes. It collects\n all the config requirements for the listed classes and places them\n each into their own Namespace.\n \"\"\"\n # we're dynamically creating a class here. The following block of\n # code is actually adding class level attributes to this new class\n required_config = Namespace() # 1st requirement for configman\n subordinate_namespace_names = [] # to help the programmer know\n # what Namespaces we added\n namespace_template = template_for_namespace # save the template\n # for future reference\n class_option_name = name_of_class_option # save the class's option\n # name for the future\n # for each class in the class list\n for namespace_index, a_class in enumerate(class_list):\n # figure out the Namespace name\n namespace_name = template_for_namespace % namespace_index\n subordinate_namespace_names.append(namespace_name)\n # create the new Namespace\n required_config[namespace_name] = Namespace()\n # add the option for the class itself\n required_config[namespace_name].add_option(\n name_of_class_option,\n #doc=a_class.__doc__ # not helpful if too verbose\n default=a_class,\n from_string_converter=class_converter\n )\n if instantiate_classes:\n # add an aggregator to instantiate the class\n required_config[namespace_name].add_aggregation(\n \"%s_instance\" % name_of_class_option,\n lambda c, lc, a: lc[name_of_class_option](lc)\n )\n\n @classmethod\n def to_str(cls):\n \"\"\"this method takes this inner class object and turns it back\n into the original string of classnames. This is used\n primarily as for the output of the 'help' option\"\"\"\n return ', '.join(\n py_obj_to_str(v[name_of_class_option].value)\n for v in cls.get_required_config().values()\n if isinstance(v, Namespace)\n )\n\n return InnerClassList # result of class_list_converter\n return class_list_converter"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef str_to_list(\n input_str,\n item_converter=lambda x: x,\n item_separator=',',\n list_to_collection_converter=None,\n):\n \"\"\" a conversion function for list\n \"\"\"\n if not isinstance(input_str, six.string_types):\n raise ValueError(input_str)\n input_str = str_quote_stripper(input_str)\n result = [\n item_converter(x.strip())\n for x in input_str.split(item_separator) if x.strip()\n ]\n if list_to_collection_converter is not None:\n return list_to_collection_converter(result)\n return result", "response": "a conversion function for list\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef arbitrary_object_to_string(a_thing):\n # is it None?\n if a_thing is None:\n return ''\n # is it already a string?\n if isinstance(a_thing, six.string_types):\n return a_thing\n if six.PY3 and isinstance(a_thing, six.binary_type):\n try:\n return a_thing.decode('utf-8')\n except UnicodeDecodeError:\n pass\n # does it have a to_str function?\n try:\n return a_thing.to_str()\n except (AttributeError, KeyError, TypeError):\n # AttributeError - no to_str function?\n # KeyError - DotDict has no to_str?\n # TypeError - problem converting\n # nope, no to_str function\n pass\n # is this a type proxy?\n try:\n return arbitrary_object_to_string(a_thing.a_type)\n except (AttributeError, KeyError, TypeError):\n #\n # nope, no a_type property\n pass\n # is it a built in?\n try:\n return known_mapping_type_to_str[a_thing]\n except (KeyError, TypeError):\n # nope, not a builtin\n pass\n # is it something from a loaded module?\n try:\n if a_thing.__module__ not in ('__builtin__', 'builtins', 'exceptions'):\n if a_thing.__module__ == \"__main__\":\n module_name = (\n sys.modules['__main__']\n .__file__[:-3]\n .replace('/', '.')\n .strip('.')\n )\n else:\n module_name = a_thing.__module__\n return \"%s.%s\" % (module_name, a_thing.__name__)\n except AttributeError:\n # nope, not one of these\n pass\n # maybe it has a __name__ attribute?\n try:\n return a_thing.__name__\n except AttributeError:\n # nope, not one of these\n pass\n # punt and see what happens if we just cast it to string\n return str(a_thing)", "response": "take a python object of some sort and convert it into a human readable string."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef to_source(node, indent_with=' ' * 4, add_line_information=False):\n generator = SourceGenerator(indent_with, add_line_information)\n generator.visit(node)\n return ''.join(str(s) for s in generator.result)", "response": "This function can convert a node tree into a python sourcecode string."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef connection(self, name=None):\n if not name:\n name = threading.currentThread().getName()\n if name in self.pool:\n return self.pool[name]\n self.pool[name] = FakeDatabaseConnection(self.dsn)\n return self.pool[name]", "response": "return a named connection."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef close_connection(self, connection, force=False):\n if force:\n print('PostgresPooled - delegating connection closure')\n try:\n super(PostgresPooled, self).close_connection(connection,\n force)\n except self.operational_exceptions:\n print('PostgresPooled - failed closing')\n for name, conn in self.pool.iteritems():\n if conn is connection:\n break\n del self.pool[name]\n else:\n print('PostgresPooled - refusing to close connection')", "response": "overriding the baseclass function this routine will decline to\n close a connection at the end of a transaction context. This routine will close the connection at the end of a transaction context."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes a function within the context of a transaction", "response": "def do_transaction(self, function, *args, **kwargs):\n \"\"\"execute a function within the context of a transaction\"\"\"\n with self.config.db_transaction() as trans:\n function(trans, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsleeping for the specified number of seconds logging every wait_log_interval seconds with progress info.", "response": "def responsive_sleep(self, seconds, wait_reason=''):\n \"\"\"Sleep for the specified number of seconds, logging every\n 'wait_log_interval' seconds with progress info.\"\"\"\n for x in range(int(seconds)):\n if (self.config.wait_log_interval and\n not x % self.config.wait_log_interval):\n print('%s: %dsec of %dsec' % (wait_reason,\n x,\n seconds))\n time.sleep(1.0)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef do_transaction(self, function, *args, **kwargs):\n for wait_in_seconds in self.backoff_generator():\n try:\n with self.config.db_transaction() as trans:\n function(trans, *args, **kwargs)\n trans.commit()\n break\n except self.config.db_transaction.operational_exceptions:\n pass\n print(('failure in transaction - retry in %s seconds' %\n wait_in_seconds))\n self.responsive_sleep(wait_in_seconds,\n \"waiting for retry after failure in \"\n \"transaction\")", "response": "execute a function within the context of a transaction"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _expand_files(self, file_name, original_path, indent=\"\"):\n expanded_file_contents = []\n with open(file_name) as f:\n for a_line in f:\n match = ConfigObjWithIncludes._include_re.match(a_line)\n if match:\n include_file = match.group(2)\n include_file = os.path.join(\n original_path,\n include_file\n )\n new_lines = self._expand_files(\n include_file,\n os.path.dirname(include_file),\n indent + match.group(1)\n )\n expanded_file_contents.extend(new_lines)\n else:\n expanded_file_contents.append(indent + a_line.rstrip())\n return expanded_file_contents", "response": "This function recursively finds all the contents of a file and returns a list of the contents of the file."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_values(self, config_manager, ignore_mismatches, obj_hook=DotDict):\n if self.delayed_parser_instantiation:\n try:\n app = config_manager._get_option('admin.application')\n source = \"%s%s\" % (app.value.app_name, file_name_extension)\n self.config_obj = configobj.ConfigObj(source)\n self.delayed_parser_instantiation = False\n except AttributeError:\n # we don't have enough information to get the ini file\n # yet. we'll ignore the error for now\n return obj_hook() # return empty dict of the obj_hook type\n if isinstance(self.config_obj, obj_hook):\n return self.config_obj\n return obj_hook(initializer=self.config_obj)", "response": "Return a nested dictionary representing the values in the ini file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef error(self, message):\n if (\n \"not allowed\" in message\n or \"ignored\" in message\n or \"expected\" in message\n or \"invalid\" in message\n or self.add_help\n ):\n # when we have \"help\" then we must also have proper error\n # processing. Without \"help\", we suppress the errors by\n # doing nothing here\n super(IntermediateConfigmanParser, self).error(message)", "response": "we can t suppress errors by the expansion process."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef configman_keys(a_mapping):\n configmanized_keys_dict = DotDict()\n for k, v in iteritems_breadth_first(a_mapping):\n if '__' in k and k != k.upper():\n k = k.replace('__', '.')\n configmanized_keys_dict[k] = v\n return configmanized_keys_dict", "response": "return a DotDict that is a copy of the provided mapping with keys\n transformed into a configman compatible form"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_key_translating_dot_dict(\n new_class_name,\n translation_tuples,\n base_class=DotDict\n):\n \"\"\"this function will generate a DotDict derivative class that has key\n translation built in. If the key is not found, translations (as specified\n by the translation_tuples) are performed on the key and the lookup is\n tried again. Only on failure of this second lookup will the KeyError\n exception be raised.\n\n parameters:\n new_class_name - the name of the returned class\n translation_tuples - a sequence of 2-tuples of the form:\n (original_substring, substitution_string)\n base_class - the baseclass on which this new class is to be based\n \"\"\"\n #==========================================================================\n class DotDictWithKeyTranslations(base_class):\n\n def __init__(self, *args, **kwargs):\n self.__dict__['_translation_tuples'] = translation_tuples\n super(DotDictWithKeyTranslations, self).__init__(*args, **kwargs)\n\n #----------------------------------------------------------------------\n @memoize()\n def _translate_key(self, key):\n for original, replacement in self._translation_tuples:\n key = key.replace(original, replacement)\n return key\n\n #----------------------------------------------------------------------\n def assign(self, key, value):\n super(DotDictWithKeyTranslations, self).assign(\n self._translate_key(key),\n value\n )\n\n #----------------------------------------------------------------------\n def __setattr__(self, key, value):\n super(DotDictWithKeyTranslations, self).__setattr__(\n self._translate_key(key),\n value\n )\n\n #----------------------------------------------------------------------\n def __getattr__(self, key):\n alt_key = self._translate_key(key)\n if alt_key == key:\n return super(DotDictWithKeyTranslations, self).__getattr__(key)\n try:\n return getattr(self, alt_key)\n except KeyError:\n raise KeyError(key)\n\n #----------------------------------------------------------------------\n def __delattr__(self, key):\n super(DotDictWithKeyTranslations, self).__delattr__(\n self._translate_key(key)\n )\n\n if six.PY2:\n new_class_name = six.binary_type(new_class_name)\n DotDictWithKeyTranslations.__name__ = new_class_name\n return DotDictWithKeyTranslations", "response": "This function creates a new class that has key translation built in."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef memoize(max_cache_size=1000):\n def wrapper(f):\n @wraps(f)\n def fn(*args, **kwargs):\n if kwargs:\n key = (args, tuple(kwargs.items()))\n else:\n key = args\n try:\n return fn.cache[key]\n except KeyError:\n if fn.count >= max_cache_size:\n fn.cache = {}\n fn.count = 0\n result = f(*args, **kwargs)\n fn.cache[key] = result\n fn.count += 1\n return result\n except TypeError:\n return f(*args, **kwargs)\n fn.cache = {}\n fn.count = 0\n return fn\n return wrapper", "response": "A function decorator that creates a cache that can be used to store the result of a function."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy(self):\n o = Option(\n name=self.name,\n default=self.default,\n doc=self.doc,\n from_string_converter=self.from_string_converter,\n to_string_converter=self.to_string_converter,\n value=self.value,\n short_form=self.short_form,\n exclude_from_print_conf=self.exclude_from_print_conf,\n exclude_from_dump_conf=self.exclude_from_dump_conf,\n is_argument=self.is_argument,\n likely_to_be_changed=self.likely_to_be_changed,\n not_for_definition=self.not_for_definition,\n reference_value_from=self.reference_value_from,\n secret=self.secret,\n has_changed=self.has_changed,\n foreign_data=self.foreign_data,\n )\n return o", "response": "returns a copy of the option"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a config as a context that calls close on every item when it goes out of scope", "response": "def context(self, mapping_class=DotDictWithAcquisition):\n \"\"\"return a config as a context that calls close on every item when\n it goes out of scope\"\"\"\n config = None\n try:\n config = self.get_config(mapping_class=mapping_class)\n yield config\n finally:\n if config:\n self._walk_and_close(config)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nprinting a summary of the current command line options.", "response": "def output_summary(self, output_stream=sys.stdout):\n \"\"\"outputs a usage tip and the list of acceptable commands.\n This is useful as the output of the 'help' option.\n\n parameters:\n output_stream - an open file-like object suitable for use as the\n target of a print function\n \"\"\"\n if self.app_name or self.app_description:\n print('Application: ', end='', file=output_stream)\n if self.app_name:\n print(self.app_name, self.app_version, file=output_stream)\n if self.app_description:\n print(self.app_description, file=output_stream)\n if self.app_name or self.app_description:\n print('', file=output_stream)\n\n names_list = self.get_option_names()\n print(\n \"usage:\\n%s [OPTIONS]... \" % self.app_invocation_name,\n end='', file=output_stream\n )\n bracket_count = 0\n # this section prints the non-switch command line arguments\n for key in names_list:\n an_option = self.option_definitions[key]\n if an_option.is_argument:\n if an_option.default is None:\n # there's no option, assume the user must set this\n print(an_option.name, end='', file=output_stream)\n elif (\n inspect.isclass(an_option.value)\n or inspect.ismodule(an_option.value)\n ):\n # this is already set and it could have expanded, most\n # likely this is a case where a sub-command has been\n # loaded and we're looking to show the help for it.\n # display show it as a constant already provided rather\n # than as an option the user must provide\n print(an_option.default, end='', file=output_stream)\n else:\n # this is an argument that the user may alternatively\n # provide\n print(\"[ %s\" % an_option.name, end='', file=output_stream)\n bracket_count += 1\n print(']' * bracket_count, '\\n', file=output_stream)\n\n names_list.sort()\n if names_list:\n print('OPTIONS:', file=output_stream)\n\n pad = ' ' * 4\n\n for name in names_list:\n if name in self.options_banned_from_help:\n continue\n option = self._get_option(name)\n\n line = ' ' * 2 # always start with 2 spaces\n if option.short_form:\n line += '-%s, ' % option.short_form\n line += '--%s' % name\n line += '\\n'\n\n doc = option.doc if option.doc is not None else ''\n if doc:\n line += '%s%s\\n' % (pad, doc)\n try:\n value = option.value\n type_of_value = type(value)\n converter_function = to_string_converters[type_of_value]\n default = converter_function(value)\n except KeyError:\n default = option.value\n if default is not None:\n if (\n (option.secret or 'password' in name.lower()) and\n not self.option_definitions.admin.expose_secrets.default\n ):\n default = '*********'\n if name not in ('help',):\n # don't bother with certain dead obvious ones\n line += '%s(default: %s)\\n' % (pad, default)\n\n print(line, file=output_stream)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef print_conf(self):\n\n config_file_type = self._get_option('admin.print_conf').value\n\n @contextlib.contextmanager\n def stdout_opener():\n yield sys.stdout\n\n skip_keys = [\n k for (k, v)\n in six.iteritems(self.option_definitions)\n if isinstance(v, Option) and v.exclude_from_print_conf\n ]\n self.write_conf(config_file_type, stdout_opener, skip_keys=skip_keys)", "response": "write a config file to the pathname specified in the parameter."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwrites a config file to the pathname specified in the parameter.", "response": "def dump_conf(self, config_pathname=None):\n \"\"\"write a config file to the pathname specified in the parameter. The\n file extention determines the type of file written and must match a\n registered type.\n\n parameters:\n config_pathname - the full path and filename of the target config\n file.\"\"\"\n\n if not config_pathname:\n config_pathname = self._get_option('admin.dump_conf').value\n\n opener = functools.partial(open, config_pathname, 'w')\n config_file_type = os.path.splitext(config_pathname)[1][1:]\n\n skip_keys = [\n k for (k, v)\n in six.iteritems(self.option_definitions)\n if isinstance(v, Option) and v.exclude_from_dump_conf\n ]\n\n self.write_conf(config_file_type, opener, skip_keys=skip_keys)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwrites a configuration file to a file - like object.", "response": "def write_conf(self, config_file_type, opener, skip_keys=None):\n \"\"\"write a configuration file to a file-like object.\n\n parameters:\n config_file_type - a string containing a registered file type OR\n a for_XXX module from the value_source\n package. Passing in an string that is\n unregistered will result in a KeyError\n opener - a callable object or function that returns a file like\n object that works as a context in a with statement.\"\"\"\n\n blocked_keys = self.keys_blocked_from_output\n if skip_keys:\n blocked_keys.extend(skip_keys)\n\n if blocked_keys:\n option_defs = self.option_definitions.safe_copy()\n for a_blocked_key in blocked_keys:\n try:\n del option_defs[a_blocked_key]\n except (AttributeError, KeyError):\n # okay that key isn't here\n pass\n # remove empty namespaces\n all_keys = [k for k in\n option_defs.keys_breadth_first(include_dicts=True)]\n for key in all_keys:\n candidate = option_defs[key]\n if (isinstance(candidate, Namespace) and not len(candidate)):\n del option_defs[key]\n else:\n option_defs = self.option_definitions\n\n # find all of the secret options and overwrite their values with\n # '*' * 16\n if not self.option_definitions.admin.expose_secrets.default:\n for a_key in option_defs.keys_breadth_first():\n an_option = option_defs[a_key]\n if (\n (not a_key.startswith('admin'))\n and isinstance(an_option, Option)\n and an_option.secret\n ):\n # force the option to be a string of *\n option_defs[a_key].value = '*' * 16\n option_defs[a_key].from_string_converter = str\n\n dispatch_request_to_write(config_file_type, option_defs, opener)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef log_config(self, logger):\n\n logger.info(\"app_name: %s\", self.app_name)\n logger.info(\"app_version: %s\", self.app_version)\n logger.info(\"current configuration:\")\n config = [(key, self.option_definitions[key].value)\n for key in self.option_definitions.keys_breadth_first()\n if key not in self.keys_blocked_from_output]\n config.sort()\n for key, val in config:\n if (\n self.option_definitions[key].secret\n or 'password' in key.lower()\n ):\n logger.info('%s: *********', key)\n else:\n try:\n logger.info('%s: %s', key,\n to_string_converters[type(key)](val))\n except KeyError:\n logger.info('%s: %s', key, val)", "response": "write out the current configuration to a log - like object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_option_names(self):\n return [x for x in self.option_definitions.keys_breadth_first()\n if isinstance(self.option_definitions[x], Option)]", "response": "returns a list of fully qualified option names."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _create_reference_value_options(self, keys, finished_keys):\n # a set of known reference_value_from_links\n set_of_reference_value_option_names = set()\n for key in keys:\n if key in finished_keys:\n continue\n an_option = self.option_definitions[key]\n if an_option.reference_value_from:\n\n fully_qualified_reference_name = '.'.join((\n an_option.reference_value_from,\n an_option.name\n ))\n if fully_qualified_reference_name in keys:\n continue # this referenced value has already been defined\n # no need to repeat it - skip on to the next key\n reference_option = an_option.copy()\n reference_option.reference_value_from = None\n reference_option.name = fully_qualified_reference_name\n # wait, aren't we setting a fully qualified dotted name into\n # the name field? Yes, 'add_option' below sees that\n # full pathname and does the right thing with it to ensure\n # that the reference_option is created within the\n # correct namespace\n set_of_reference_value_option_names.add(\n fully_qualified_reference_name\n )\n self.option_definitions.add_option(reference_option)\n\n for a_reference_value_option_name in set_of_reference_value_option_names:\n for x in range(a_reference_value_option_name.count('.')):\n namespace_path = \\\n a_reference_value_option_name.rsplit('.', x + 1)[0]\n self.option_definitions[namespace_path].ref_value_namespace()\n\n return set_of_reference_value_option_names", "response": "This method creates the reference_value_from links and name fields for the option definitions."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks for mismatches from value sources", "response": "def _check_for_mismatches(self, known_keys):\n \"\"\"check for bad options from value sources\"\"\"\n for a_value_source in self.values_source_list:\n try:\n if a_value_source.always_ignore_mismatches:\n continue\n except AttributeError:\n # ok, this values source doesn't have the concept\n # always igoring mismatches, we won't tolerate mismatches\n pass\n # we want to fetch the keys from the value sources so that we can\n # check for mismatches. Commandline value sources, are different,\n # we never want to allow unmatched keys from the command line.\n # By detecting if this value source is a command line source, we\n # can employ the command line's own mismatch detection. The\n # boolean 'allow_mismatches' controls application of the tollerance\n # for mismatches.\n if hasattr(a_value_source, 'command_line_value_source'):\n allow_mismatches = False\n else:\n allow_mismatches = True\n # make a set of all the keys from a value source in the form\n # of strings like this: 'x.y.z'\n value_source_mapping = a_value_source.get_values(\n self,\n allow_mismatches,\n self.value_source_object_hook\n )\n value_source_keys_set = set([\n k for k in\n DotDict(value_source_mapping).keys_breadth_first()\n ])\n # make a set of the keys that didn't match any of the known\n # keys in the requirements\n unmatched_keys = value_source_keys_set.difference(known_keys)\n # some of the unmatched keys may actually be ok because the were\n # used during acquisition.\n # remove keys of the form 'y.z' if they match a known key of the\n # form 'x.y.z'\n for key in unmatched_keys.copy():\n key_is_okay = six.moves.reduce(\n lambda x, y: x or y,\n (known_key.endswith(key) for known_key in known_keys)\n )\n if key_is_okay:\n unmatched_keys.remove(key)\n # anything left in the unmatched_key set is a badly formed key.\n # issue a warning\n if unmatched_keys:\n if self.option_definitions.admin.strict.default:\n # raise hell...\n if len(unmatched_keys) > 1:\n raise NotAnOptionError(\n \"%s are not valid Options\" % unmatched_keys\n )\n elif len(unmatched_keys) == 1:\n raise NotAnOptionError(\n \"%s is not a valid Option\" % unmatched_keys.pop()\n )\n else:\n warnings.warn(\n 'Invalid options: %s' % ', '.join(sorted(unmatched_keys))\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _generate_config(self, mapping_class):\n config = mapping_class()\n self._walk_config_copy_values(\n self.option_definitions,\n config,\n mapping_class\n )\n return config", "response": "This routine generates a copy of the DotDict based config"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef close(self):\n print(\"PGPooledTransaction - shutting down connection pool\")\n for name, conn in self.pool.iteritems():\n conn.close()\n print(\"PGPooledTransaction - connection %s closed\" % name)", "response": "close all pooled connections"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find_action_name_by_value(registry, target_action_instance):\n target_type = type(target_action_instance)\n for key, value in six.iteritems(registry['action']):\n if value is target_type:\n if key is None:\n return 'store'\n return key\n return None", "response": "This routine finds the name of an action class with a human readable\n string is exposed externally only at the time of argument definitions."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_args_and_values(parser, an_action):\n args = inspect.getargspec(an_action.__class__.__init__).args\n kwargs = dict(\n (an_attr, getattr(an_action, an_attr))\n for an_attr in args\n if (\n an_attr not in ('self', 'required')\n and getattr(an_action, an_attr) is not None\n )\n )\n action_name = find_action_name_by_value(\n parser._optionals._registries,\n an_action\n )\n if 'required' in kwargs:\n del kwargs['required']\n kwargs['action'] = action_name\n if 'option_strings' in kwargs:\n args = tuple(kwargs['option_strings'])\n del kwargs['option_strings']\n else:\n args = ()\n return args, kwargs", "response": "this rountine attempts to reconstruct the kwargs that were used in the action object"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_namespace(self, name, a_namespace):\n # save a local copy of the namespace\n self.namespaces[name] = a_namespace\n # iterate through the namespace branding each of the options with the\n # name of the subparser to which they belong\n for k in a_namespace.keys_breadth_first():\n an_option = a_namespace[k]\n if not an_option.foreign_data:\n an_option.foreign_data = DotDict()\n an_option.foreign_data['argparse.owning_subparser_name'] = name", "response": "add a namespace to the argparse object"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_required_config(self):\n required_config = Namespace()\n # add current options to a copy of required config\n for k, v in iteritems_breadth_first(self.required_config):\n required_config[k] = v\n # get any option found in any subparsers\n try:\n subparser_namespaces = (\n self.configman_subparsers_option.foreign_data\n .argparse.subprocessor_from_string_converter\n )\n subparsers = (\n self._argparse_subparsers._configman_option.foreign_data\n .argparse.subparsers\n )\n # each subparser needs to have its configman options set up\n # in the subparser's configman option. This routine copies\n # the required_config of each subparser into the\n # SubparserFromStringConverter defined above.\n for subparser_name, subparser_data in six.iteritems(subparsers):\n subparser_namespaces.add_namespace(\n subparser_name,\n subparser_data.subparser.get_required_config()\n )\n except AttributeError:\n # there is no subparser\n pass\n return required_config", "response": "This method returns the required_config dictionary that is a copy of the namespace rather than the actual embedded\n namespace."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_argument(self, *args, **kwargs):\n # pull out each of the argument definition components from the args\n # so that we can deal with them one at a time in a well labeled manner\n # In this section, variables beginning with the prefix \"argparse\" are\n # values that define Action object. Variables that begin with\n # \"configman\" are the arguments to create configman Options.\n argparse_action_name = kwargs.get('action', None)\n argparse_dest = kwargs.get('dest', None)\n argparse_const = kwargs.get('const', None)\n argparse_default = kwargs.get('default', None)\n if argparse_default is argparse.SUPPRESS:\n # we'll be forcing all options to have the attribute of\n # argparse.SUPPRESS later. It's our way of making sure that\n # argparse returns only values that the user explicitly added to\n # the command line.\n argparse_default = None\n argparse_nargs = kwargs.get('nargs', None)\n argparse_type = kwargs.get('type', None)\n argparse_suppress_help = kwargs.pop('suppress_help', False)\n if argparse_suppress_help:\n configman_doc = kwargs.get('help', '')\n kwargs['help'] = argparse.SUPPRESS\n else:\n argparse_help = kwargs.get('help', '')\n if argparse_help == argparse.SUPPRESS:\n configman_doc = ''\n else:\n configman_doc = argparse_help\n\n # we need to make sure that all arguments that the user has not\n # explicily set on the command line have this attribute. This means\n # that when the argparse parser returns the command line values, it\n # will not return values that the user did not mention on the command\n # line. The defaults that otherwise would have been returned will be\n # handled by configman.\n kwargs['default'] = argparse.SUPPRESS\n # forward all parameters to the underlying base class to create a\n # normal argparse action object.\n an_action = super(ArgumentParser, self).add_argument(\n *args,\n **kwargs\n )\n argparse_option_strings = an_action.option_strings\n\n # get a human readable string that identifies the type of the argparse\n # action class that was created\n if argparse_action_name is None:\n argparse_action_name = find_action_name_by_value(\n self._optionals._registries,\n an_action\n )\n\n configman_is_argument = False\n\n # each of argparse's Action types must be handled separately.\n #--------------------------------------------------------------------\n # STORE\n if argparse_action_name == 'store':\n if argparse_dest is None:\n configman_name, configman_is_argument = self._get_option_name(\n args\n )\n if not configman_name:\n configman_name = args[0]\n else:\n configman_name = argparse_dest\n configman_is_argument = not argparse_option_strings\n configman_default = argparse_default\n if argparse_nargs and argparse_nargs in \"1?\":\n if argparse_type:\n configman_from_string = argparse_type\n elif argparse_default:\n configman_from_string = (\n str_to_instance_of_type_converters.get(\n type(argparse_default),\n str\n )\n )\n else:\n configman_from_string = str\n elif argparse_nargs and argparse_type:\n configman_from_string = partial(\n str_to_list,\n item_converter=argparse_type,\n item_separator=' ',\n )\n elif argparse_nargs and argparse_default:\n configman_from_string = partial(\n str_to_list,\n item_converter=str_to_instance_of_type_converters.get(\n type(argparse_default),\n str\n ),\n item_separator=' ',\n )\n elif argparse_nargs:\n configman_from_string = partial(\n str_to_list,\n item_converter=str,\n item_separator=' ',\n )\n elif argparse_type:\n configman_from_string = argparse_type\n elif argparse_default:\n configman_from_string = str_to_instance_of_type_converters.get(\n type(argparse_default),\n str\n )\n else:\n configman_from_string = str\n configman_to_string = to_str\n\n #--------------------------------------------------------------------\n # STORE_CONST\n elif (\n argparse_action_name == 'store_const'\n or argparse_action_name == 'count'\n ):\n if argparse_dest is None:\n configman_name, configman_is_argument = self._get_option_name(\n args\n )\n if not configman_name:\n configman_name = args[0]\n else:\n configman_name = argparse_dest\n configman_default = argparse_default\n if argparse_type:\n configman_from_string = argparse_type\n else:\n configman_from_string = str_to_instance_of_type_converters.get(\n type(argparse_const),\n str\n )\n configman_to_string = to_str\n\n #--------------------------------------------------------------------\n # STORE_TRUE / STORE_FALSE\n elif (\n argparse_action_name == 'store_true'\n or argparse_action_name == 'store_false'\n ):\n if argparse_dest is None:\n configman_name, configman_is_argument = self._get_option_name(\n args\n )\n if not configman_name:\n configman_name = args[0]\n else:\n configman_name = argparse_dest\n configman_default = argparse_default\n configman_from_string = boolean_converter\n configman_to_string = to_str\n\n #--------------------------------------------------------------------\n # APPEND\n elif argparse_action_name == 'append':\n if argparse_dest is None:\n configman_name, configman_is_argument = self._get_option_name(\n args\n )\n if not configman_name:\n configman_name = args[0]\n else:\n configman_name = argparse_dest\n configman_default = argparse_default\n if argparse_type:\n configman_from_string = argparse_type\n else:\n configman_from_string = str\n configman_to_string = to_str\n\n #--------------------------------------------------------------------\n # APPEND_CONST\n elif argparse_action_name == 'append_const':\n if argparse_dest is None:\n configman_name, configman_is_argument = self._get_option_name(\n args\n )\n if not configman_name:\n configman_name = args[0]\n else:\n configman_name = argparse_dest\n configman_default = argparse_default\n if argparse_type:\n configman_from_string = argparse_type\n else:\n configman_from_string = str_to_instance_of_type_converters.get(\n type(argparse_const),\n str\n )\n configman_to_string = to_str\n\n #--------------------------------------------------------------------\n # VERSION\n elif argparse_action_name == 'version':\n return an_action\n\n #--------------------------------------------------------------------\n # OTHER\n else:\n configman_name = argparse_dest\n\n # configman uses the switch name as the name of the key inwhich to\n # store values. argparse is able to use different names for each.\n # this means that configman may encounter repeated targets. Rather\n # than overwriting Options with new ones with the same name, configman\n # renames them by appending the '$' character.\n while configman_name in self.required_config:\n configman_name = \"%s$\" % configman_name\n configman_not_for_definition = configman_name.endswith('$')\n\n # it's finally time to create the configman Option object and add it\n # to the required_config.\n self.required_config.add_option(\n name=configman_name,\n default=configman_default,\n doc=configman_doc,\n from_string_converter=configman_from_string,\n to_string_converter=configman_to_string,\n #short_form=configman_short_form,\n is_argument=configman_is_argument,\n not_for_definition=configman_not_for_definition,\n # we're going to save the args & kwargs that created the\n # argparse Action. This enables us to perfectly reproduce the\n # the original Action object later during the configman overlay\n # process.\n foreign_data=DotDict({\n 'argparse.flags.subcommand': False,\n 'argparse.args': args,\n 'argparse.kwargs': kwargs,\n 'argparse.owning_subparser_name': self.subparser_name,\n })\n )\n return an_action", "response": "This method adds an argument to the required_config\n section of the configman object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_subparsers(self, *args, **kwargs):\n\n kwargs['parser_class'] = self.__class__\n kwargs['action'] = ConfigmanSubParsersAction\n\n subparser_action = super(ArgumentParser, self).add_subparsers(\n *args,\n **kwargs\n )\n self._argparse_subparsers = subparser_action\n\n if \"dest\" not in kwargs or kwargs['dest'] is None:\n kwargs['dest'] = 'subcommand'\n configman_name = kwargs['dest']\n configman_default = None\n configman_doc = kwargs.get('help', '')\n subprocessor_from_string_converter = SubparserFromStringConverter()\n configman_to_string = str\n configman_is_argument = True\n configman_not_for_definition = True\n\n # it's finally time to create the configman Option object and add it\n # to the required_config.\n self.required_config.add_option(\n name=configman_name,\n default=configman_default,\n doc=configman_doc,\n from_string_converter=subprocessor_from_string_converter,\n to_string_converter=configman_to_string,\n is_argument=configman_is_argument,\n not_for_definition=configman_not_for_definition,\n # we're going to save the input parameters that created the\n # argparse Action. This enables us to perfectly reproduce the\n # the original Action object later during the configman overlay\n # process.\n foreign_data=DotDict({\n 'argparse.flags.subcommand': subparser_action,\n 'argparse.args': args,\n 'argparse.kwargs': kwargs,\n 'argparse.subparsers': DotDict(),\n 'argparse.subprocessor_from_string_converter':\n subprocessor_from_string_converter\n })\n )\n\n self.configman_subparsers_option = self.required_config[configman_name]\n subparser_action.add_configman_option(self.configman_subparsers_option)\n\n return subparser_action", "response": "When adding a subparser we need to create the configman Option object for the subparser and pack it s foreign data section with the original args & kwargs."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_known_args(self, args=None, namespace=None):\n # load the config_manager within the scope of the method that uses it\n # so that we avoid circular references in the outer scope\n from configman.config_manager import ConfigurationManager\n configuration_manager = ConfigurationManager(\n definition_source=[self.get_required_config()],\n values_source_list=self.value_source_list,\n argv_source=args,\n app_name=self.prog,\n app_version=self.version,\n app_description=self.description,\n use_auto_help=False,\n )\n conf = configuration_manager.get_config(\n mapping_class=create_key_translating_dot_dict(\n \"HyphenUnderscoreDict\",\n (('-', '_'),)\n )\n )\n return conf", "response": "this method hijacks the normal argparse Namespace generation and shimming configman into the process. The return value will be a\n configman DotDict rather than an argparse Namespace."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sequence_to_string(\n a_list,\n open_bracket_char='[',\n close_bracket_char=']',\n delimiter=\", \"\n):\n \"\"\"a dedicated function that turns a list into a comma delimited string\n of items converted. This method will flatten nested lists.\"\"\"\n return \"%s%s%s\" % (\n open_bracket_char,\n delimiter.join(\n local_to_str(x)\n for x in a_list\n ),\n close_bracket_char\n )", "response": "a dedicated function that turns a list into a comma delimited string\n of items converted."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a type return a tuple of the module - path type_name", "response": "def get_import_for_type(t):\n \"\"\"given a type, return a tuple of the (module-path, type_name)\n or (None, None) if it is a built in.\"\"\"\n t_as_string = to_str(t)\n if not is_identifier(t_as_string):\n # this class expanded into something other than a single identifier\n # we can ignore it. This is the case when we encounter something\n # like the configman.converter.str_to_classes_in_namespaces\n # InnerClassList. We can safely ignore these things here.\n return (None, None)\n if '.' in t_as_string:\n parts = t_as_string.split('.')\n return ('.'.join(parts[:-1]), parts[-1])\n else:\n if t_as_string in known_mapping_str_to_type:\n return (None, None)\n return (None, t_as_string)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef write(source_mapping, output_stream=sys.stdout):\n # a set of classes, modules and/or functions that are values in\n # configman options. These values will have to be imported in the\n # module that this method is writing.\n set_of_classes_needing_imports = set()\n # once symbols are imported, they are in the namespace of the module,\n # but that's not where we want them. We only want them to be values\n # in configman Options. This set will be used to make a list of\n # these symbols, to forewarn a future configman that reads this\n # module, that it can ignore these symbols. This will prevent that\n # future configman from issuing a \"mismatced symbols\" warinng.\n symbols_to_ignore = set()\n\n # look ahead to see what sort of imports we're going to have to do\n for key in source_mapping.keys_breadth_first():\n value = source_mapping[key]\n\n if isinstance(value, Aggregation):\n # Aggregations don't get included, skip on\n continue\n\n if '.' in key:\n # this indicates that there are things in nested namespaces,\n # we will use the DotDict class to represent namespaces\n set_of_classes_needing_imports.add(DotDict)\n\n option = None\n if isinstance(value, Option):\n # it's the value inside the option, not the option itself\n # that is of interest to us\n option = value\n value = option.value\n\n if value is None:\n # we don't need in import anything having to do with None\n continue\n\n if isclass(value) or ismodule(value) or isfunction(value):\n # we know we need to import any of these types\n set_of_classes_needing_imports.add(value)\n else:\n try:\n # perhaps the value is an instance of a class? If so,\n # we'll likely need to import that class, but only if\n # we don't have a way to convert a string to that class\n set_of_classes_needing_imports.add(value.__class__)\n except AttributeError:\n # it's not a class instance, we can skip on\n pass\n\n # for everyone of the imports that we're going to have to create\n # we need to know the dotted module pathname and the name of the\n # of the class/module/function. This routine make a list of 3-tuples\n # class, dotted_module_path, class_name\n class_and_module_path_and_class_name = []\n for a_class in set_of_classes_needing_imports:\n module_path, class_name = get_import_for_type(a_class)\n if (not module_path) and (not class_name):\n continue\n class_and_module_path_and_class_name.append(\n (a_class, module_path, class_name)\n )\n\n # using the collection of 3-tuples, create a lookup mapping where a\n # class is the key to a 2-tuple of the dotted_module_path & class_name.\n # This is also the appropriate time to detect any name collisions\n # and create a mapping of aliases, so we can resolve name collisions.\n class_name_by_module_path_list = defaultdict(list)\n alias_by_class = {}\n previously_used_names = set()\n for (\n a_class,\n a_module_path,\n class_name\n ) in class_and_module_path_and_class_name:\n if class_name:\n if class_name in previously_used_names:\n new_class_name_alias = \"%s_%s\" % (\n a_module_path.replace('.', '_'),\n class_name\n )\n alias_by_class[a_class] = new_class_name_alias\n previously_used_names.add(new_class_name_alias)\n else:\n previously_used_names.add(class_name)\n class_name_by_module_path_list[a_module_path].append(\n (a_class, class_name)\n )\n\n # start writing the output module\n print(\"# generated Python configman file\\n\", file=output_stream)\n\n # the first section that we're going to write is imports of the form:\n # from X import Y\n # and\n # from X import (\n # A,\n # B,\n # )\n sorted_list = [x.value for x in sorted([OrderableObj(x) for x in\n class_name_by_module_path_list.keys()])]\n for a_module_path in sorted_list:\n print(a_module_path)\n # if there is no module path, then it is something that we don't\n # need to import. If the module path begins with underscore then\n # it is private and we ought not step into that mire. If that\n # causes the output module to fail, it is up to the implementer\n # of the configman option to have created an approprate\n # \"from_string\" & \"to_string\" configman Option function references.\n if a_module_path is None or a_module_path.startswith('_'):\n continue\n list_of_class_names = \\\n class_name_by_module_path_list[a_module_path]\n if len(list_of_class_names) > 1:\n output_line = \"from %s import (\\n\" % a_module_path\n sorted_list = [x.value for x in sorted([OrderableTuple(x)\n for x in list_of_class_names])]\n for a_class, a_class_name in sorted_list:\n if a_class in alias_by_class:\n output_line = \"%s\\n %s as %s,\" % (\n output_line,\n a_class_name,\n alias_by_class[a_class]\n )\n symbols_to_ignore.add(alias_by_class[a_class])\n else:\n output_line = \"%s %s,\\n\" % (\n output_line,\n a_class_name\n )\n symbols_to_ignore.add(a_class_name)\n\n output_line = output_line + ')'\n print(output_line.strip(), file=output_stream)\n else:\n a_class, a_class_name = list_of_class_names[0]\n output_line = \"from %s import %s\" % (\n a_module_path,\n a_class_name\n )\n if a_class in alias_by_class:\n output_line = \"%s as %s\" % (\n output_line,\n alias_by_class[a_class]\n )\n symbols_to_ignore.add(alias_by_class[a_class])\n else:\n symbols_to_ignore.add(a_class_name)\n print(output_line.strip(), file=output_stream)\n print('', file=output_stream)\n\n # The next section to write will be the imports of the form:\n # import X\n sorted_list = [x.value for x in sorted([OrderableObj(x) for x in\n class_name_by_module_path_list.keys()])]\n for a_module_path in sorted_list:\n list_of_class_names = \\\n class_name_by_module_path_list[a_module_path]\n a_class, a_class_name = list_of_class_names[0]\n if a_module_path:\n continue\n import_str = (\"import %s\" % a_class_name).strip()\n symbols_to_ignore.add(a_class_name)\n print(import_str, file=output_stream)\n\n # See the explanation of 'symbols_to_ignore' above\n if symbols_to_ignore:\n print(\n \"\\n\" \\\n \"# the following symbols will be ignored by configman when\\n\" \\\n \"# this module is used as a value source. This will\\n\" \\\n \"# suppress the mismatch warning since these symbols are\\n\" \\\n \"# values for options, not option names themselves.\",\n file=output_stream\n )\n print(\"ignore_symbol_list = [\", file=output_stream)\n for a_symbol in sorted(symbols_to_ignore):\n print(' \"%s\",' % a_symbol, file=output_stream)\n print(']\\n', file=output_stream)\n\n # finally, as the last step, we need to write out the keys and values\n # will be used by a future configman as Options and values.\n sorted_keys = sorted(\n source_mapping.keys_breadth_first(include_dicts=True)\n )\n for key in sorted_keys:\n value = source_mapping[key]\n if isinstance(value, Namespace):\n ValueSource.write_namespace(key, value, output_stream)\n elif isinstance(value, Option):\n ValueSource.write_option(\n key,\n value,\n alias_by_class,\n output_stream\n )\n elif isinstance(value, Aggregation):\n # skip Aggregations\n continue\n else:\n ValueSource.write_bare_value(key, value, output_stream)", "response": "This method writes a Python module respresenting all the keys\n and values known to configman."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef datetime_from_ISO_string(s):\n try:\n return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')\n except ValueError:\n try:\n return datetime.datetime.strptime(s, '%Y-%m-%d')\n except ValueError:\n return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')", "response": "Take an ISO date string of the form YYYY - MM - DDTHH - MM - SS. S\n and convert it into an instance of datetime. datetime\nIVA."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef str_to_timedelta(input_str):\n try:\n input_str = input_str.replace(' ', ':')\n except (TypeError, AttributeError):\n from configman.converters import to_str\n raise TypeError('%s should have been a string' % to_str(input_str))\n days, hours, minutes, seconds = 0, 0, 0, 0\n details = input_str.split(':')\n if len(details) >= 4:\n days = int(details[-4])\n if len(details) >= 3:\n hours = int(details[-3])\n if len(details) >= 2:\n minutes = int(details[-2])\n if len(details) >= 1:\n seconds = int(details[-1])\n return datetime.timedelta(days=days,\n hours=hours,\n minutes=minutes,\n seconds=seconds)", "response": "a string conversion function for timedelta for strings in the format\n DD HHMMSS or D HHMMSS or D HHMMSS or D HHMMSS"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef timedelta_to_str(aTimedelta):\n days = aTimedelta.days\n temp_seconds = aTimedelta.seconds\n hours = int(temp_seconds / 3600)\n minutes = int((temp_seconds - hours * 3600) / 60)\n seconds = temp_seconds - hours * 3600 - minutes * 60\n return '%d %02d:%02d:%02d' % (days, hours, minutes, seconds)", "response": "a conversion function for time deltas to string in the form DDHHMMSS"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_source_file(file_name):\n with open(file_name, 'r') as f:\n s = f.read()\n\n nodes = ast.parse(s)\n\n module_imports = get_nodes_by_instance_type(nodes, _ast.Import)\n specific_imports = get_nodes_by_instance_type(nodes, _ast.ImportFrom)\n\n assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)\n call_objects = get_nodes_by_instance_type(nodes, _ast.Call)\n\n argparse_assignments = get_nodes_by_containing_attr(assignment_objs, 'ArgumentParser')\n group_arg_assignments = get_nodes_by_containing_attr(assignment_objs, 'add_argument_group')\n add_arg_assignments = get_nodes_by_containing_attr(call_objects, 'add_argument')\n parse_args_assignment = get_nodes_by_containing_attr(call_objects, 'parse_args')\n # there are cases where we have custom argparsers, such as subclassing ArgumentParser. The above\n # will fail on this. However, we can use the methods known to ArgumentParser to do a duck-type like\n # approach to finding what is the arg parser\n if not argparse_assignments:\n aa_references = set([i.func.value.id for i in chain(add_arg_assignments, parse_args_assignment)])\n argparse_like_objects = [getattr(i.value.func, 'id', None) for p_ref in aa_references for i in get_nodes_by_containing_attr(assignment_objs, p_ref)]\n argparse_like_objects = filter(None, argparse_like_objects)\n argparse_assignments = [get_nodes_by_containing_attr(assignment_objs, i) for i in argparse_like_objects]\n # for now, we just choose one\n try:\n argparse_assignments = argparse_assignments[0]\n except IndexError:\n pass\n\n\n # get things that are assigned inside ArgumentParser or its methods\n argparse_assigned_variables = get_node_args_and_keywords(assignment_objs, argparse_assignments, 'ArgumentParser')\n add_arg_assigned_variables = get_node_args_and_keywords(assignment_objs, add_arg_assignments, 'add_argument')\n parse_args_assigned_variables = get_node_args_and_keywords(assignment_objs, parse_args_assignment, 'parse_args')\n\n ast_argparse_source = chain(\n module_imports,\n specific_imports,\n argparse_assigned_variables,\n add_arg_assigned_variables,\n parse_args_assigned_variables,\n argparse_assignments,\n group_arg_assignments,\n add_arg_assignments,\n )\n return ast_argparse_source", "response": "Parses the source file and returns the grammar tree."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd an option to the namespace. This can take two forms of name args and kwargs are the parameters of the option and the name of the object.", "response": "def add_option(self, name, *args, **kwargs):\n \"\"\"add an option to the namespace. This can take two forms:\n 'name' is a string representing the name of an option and the\n kwargs are its parameters, or 'name' is an instance of an Option\n object\n \"\"\"\n if isinstance(name, Option):\n an_option = name\n name = an_option.name\n else:\n an_option = Option(name, *args, **kwargs)\n\n current_namespace = self\n name_parts = name.split('.')\n for a_path_component in name_parts[:-1]:\n if a_path_component not in current_namespace:\n current_namespace[a_path_component] = Namespace()\n current_namespace = current_namespace[a_path_component]\n an_option.name = name_parts[-1]\n\n setattr(current_namespace, an_option.name, an_option)\n return an_option"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmeasuring the cumulative Regression sum of Squares for each individual component and the sum of Squares for each individual component.", "response": "def _cummulativefit(self, x, y):\n \"\"\"\n Measure the cumulative Regression sum of Squares for each individual component.\n\n :param x: Data matrix to fit the PLS model.\n :type x: numpy.ndarray, shape [n_samples, n_features]\n :param y: Data matrix to fit the PLS model.\n :type y: numpy.ndarray, shape [n_samples, n_features]\n :return: dictionary object containing the total Regression Sum of Squares and the Sum of Squares\n per components, for both the X and Y data blocks.\n :rtype: dict\n \"\"\"\n if y.ndim == 1:\n y = y.reshape(-1, 1)\n if x.ndim == 1:\n x = x.reshape(-1, 1)\n\n xscaled = self.x_scaler.fit_transform(x)\n yscaled = self.y_scaler.fit_transform(y)\n\n ssx_comp = list()\n ssy_comp = list()\n\n # Obtain residual sum of squares for whole data set and per component\n SSX = np.sum(xscaled ** 2)\n SSY = np.sum(yscaled ** 2)\n ssx_comp = list()\n ssy_comp = list()\n\n for curr_comp in range(1, self.ncomps + 1):\n model = self._reduce_ncomps(curr_comp)\n\n ypred = self.y_scaler.transform(model.predict(x, y=None))\n xpred = self.x_scaler.transform(model.predict(x=None, y=y))\n rssy = np.sum((yscaled - ypred) ** 2)\n rssx = np.sum((xscaled - xpred) ** 2)\n ssx_comp.append(rssx)\n ssy_comp.append(rssy)\n\n cumulative_fit = {'SSX': SSX, 'SSY': SSY, 'SSXcomp': np.array(ssx_comp), 'SSYcomp': np.array(ssy_comp)}\n\n return cumulative_fit"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nplot the leverages of the current sample index", "response": "def plot_leverages(self):\n \"\"\"\n Leverage (h) per observation, with a red line plotted at y = 1/Number of samples (expected\n :return: Plot with observation leverages (h)\n \"\"\"\n plt.figure()\n lev = self.leverages()\n plt.xlabel('Sample Index')\n plt.ylabel('Leverage')\n plt.bar(left=range(lev.size), height=lev)\n plt.hlines(y=1/lev.size, xmin=0, xmax=lev.size, colors='r', linestyles='--')\n plt.show()\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _handle_zeros_in_scale(scale, copy=True):\n\n # if we are fitting on 1D arrays, scale might be a scalar\n if numpy.isscalar(scale):\n if scale == .0:\n scale = 1.\n return scale\n elif isinstance(scale, numpy.ndarray):\n if copy:\n # New array to avoid side-effects\n scale = scale.copy()\n scale[scale == 0.0] = 1.0\n return scale", "response": "Handles zero - based scalers."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _reset(self):\n\n # Checking one attribute is enough, because they are all set together\n # in partial_fit\n if hasattr(self, 'scale_'):\n del self.scale_\n del self.n_samples_seen_\n del self.mean_\n del self.var_", "response": "Reset internal data - dependent state of the scaler."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncomputes the mean and standard deviation from a dataset to use in future scaling operations.", "response": "def fit(self, X, y=None):\n \"\"\"\n Compute the mean and standard deviation from a dataset to use in future scaling operations.\n\n :param X: Data matrix to scale.\n :type X: numpy.ndarray, shape [n_samples, n_features]\n :param y: Passthrough for Scikit-learn ``Pipeline`` compatibility.\n :type y: None\n :return: Fitted object.\n :rtype: pyChemometrics.ChemometricsScaler\n \"\"\"\n\n # Reset internal state before fitting\n self._reset()\n return self.partial_fit(X, y)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef partial_fit(self, X, y=None):\n\n X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,\n warn_on_dtype=True, estimator=self, dtype=FLOAT_DTYPES)\n\n # Even in the case of `with_mean=False`, we update the mean anyway\n # This is needed for the incremental computation of the var\n # See incr_mean_variance_axis and _incremental_mean_variance_axis\n\n if sparse.issparse(X):\n if self.with_mean:\n raise ValueError(\n \"Cannot center sparse matrices: pass `with_mean=False` \"\n \"instead. See docstring for motivation and alternatives.\")\n if self.with_std:\n # First pass\n if not hasattr(self, 'n_samples_seen_'):\n self.mean_, self.var_ = mean_variance_axis(X, axis=0)\n self.n_samples_seen_ = X.shape[0]\n # Next passes\n else:\n self.mean_, self.var_, self.n_samples_seen_ = \\\n incr_mean_variance_axis(X, axis=0,\n last_mean=self.mean_,\n last_var=self.var_,\n last_n=self.n_samples_seen_)\n else:\n self.mean_ = None\n self.var_ = None\n else:\n # First pass\n if not hasattr(self, 'n_samples_seen_'):\n self.mean_ = .0\n self.n_samples_seen_ = 0\n if self.with_std:\n self.var_ = .0\n else:\n self.var_ = None\n\n self.mean_, self.var_, self.n_samples_seen_ = \\\n _incremental_mean_and_var(X, self.mean_, self.var_,\n self.n_samples_seen_)\n\n if self.with_std:\n self.scale_ = _handle_zeros_in_scale(numpy.sqrt(self.var_)) ** self.scale_power\n else:\n self.scale_ = None\n\n return self", "response": "Performs online computation of mean and standard deviation on X for later scaling."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef transform(self, X, y=None, copy=None):\n check_is_fitted(self, 'scale_')\n\n copy = copy if copy is not None else self.copy\n\n X = check_array(X, accept_sparse='csr', copy=copy, warn_on_dtype=True,\n estimator=self, dtype=FLOAT_DTYPES)\n\n if sparse.issparse(X):\n if self.with_mean:\n raise ValueError(\n \"Cannot center sparse matrices: pass `with_mean=False` \"\n \"instead. See docstring for motivation and alternatives.\")\n if self.scale_ is not None:\n inplace_column_scale(X, 1 / self.scale_)\n else:\n if self.with_mean:\n X -= self.mean_\n if self.with_std:\n X /= self.scale_\n return X", "response": "Perform standardization by centering and scaling using the parameters."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef inverse_transform(self, X, copy=None):\n check_is_fitted(self, 'scale_')\n\n copy = copy if copy is not None else self.copy\n if sparse.issparse(X):\n if self.with_mean:\n raise ValueError(\n \"Cannot uncenter sparse matrices: pass `with_mean=False` \"\n \"instead See docstring for motivation and alternatives.\")\n if not sparse.isspmatrix_csr(X):\n X = X.tocsr()\n copy = False\n if copy:\n X = X.copy()\n if self.scale_ is not None:\n inplace_column_scale(X, self.scale_)\n else:\n X = numpy.asarray(X)\n if copy:\n X = X.copy()\n if self.with_std:\n X *= self.scale_\n if self.with_mean:\n X += self.mean_\n\n return X", "response": "Inverse transform the data matrix X to the original representation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncalculating the leverages for each observation in the hierarchy.", "response": "def leverages(self, block='X'):\n \"\"\"\n Calculate the leverages for each observation\n :return:\n :rtype:\n \"\"\"\n # TODO check with matlab and simca\n try:\n if block == 'X':\n return np.dot(self.scores_t, np.dot(np.linalg.inv(np.dot(self.scores_t.T, self.scores_t), self.scores_t.T)))\n elif block == 'Y':\n return np.dot(self.scores_u, np.dot(np.linalg.inv(np.dot(self.scores_u.T, self.scores_u), self.scores_u.T)))\n else:\n raise ValueError\n except ValueError as verr:\n raise ValueError('block option must be either X or Y')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _recurse_replace(obj, key, new_key, sub, remove):\n if isinstance(obj, list):\n return [_recurse_replace(x, key, new_key, sub, remove) for x in obj]\n if isinstance(obj, dict):\n for k, v in list(obj.items()):\n if k == key and v in sub:\n obj[new_key] = sub[v]\n if remove:\n del obj[key]\n else:\n obj[k] = _recurse_replace(v, key, new_key, sub, remove)\n return obj", "response": "Recursive helper for replace_by_key"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreplaces values that match a key and return a new key.", "response": "def replace_by_key(pif, key, subs, new_key=None, remove=False):\n \"\"\"Replace values that match a key\n\n Deeply traverses the pif object, looking for `key` and\n replacing values in accordance with `subs`. If `new_key`\n is set, the replaced values are assigned to that key. If\n `remove` is `True`, the old `key` pairs are removed.\n \"\"\"\n if not new_key:\n new_key = key\n remove = False\n orig = pif.as_dictionary()\n new = _recurse_replace(orig, to_camel_case(key), to_camel_case(new_key), subs, remove)\n return pypif.pif.loads(json.dumps(new))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a property by name", "response": "def get_propety_by_name(pif, name):\n \"\"\"Get a property by name\"\"\"\n warn(\"This method has been deprecated in favor of get_property_by_name\")\n return next((x for x in pif.properties if x.name == name), None)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_property_by_name(pif, name):\n return next((x for x in pif.properties if x.name == name), None)", "response": "Get a property by name"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef new_keypair(key, value, ambig, unambig):\n if key in ambig:\n return\n\n if key in unambig and value != unambig[key]:\n ambig.add(key)\n del unambig[key]\n return\n\n unambig[key] = value\n return", "response": "Check new keypair against existing unambiguous dict"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds information about decodings of a child object.", "response": "def add_child_ambig(child_ambig, child_unambig, ambig, unambig):\n \"\"\"\n Add information about decodings of a child object\n\n :param child_ambig: ambiguous set from child\n :param child_unambig: unambiguous set from child\n :param ambig: set of keys storing ambig decodings\n :param unambig: dictionary storing unambiguous decodings\n :return:\n \"\"\"\n for k in child_ambig:\n ambig.add(k)\n if k in unambig:\n del unambig[k]\n\n for k, v in child_unambig.items():\n new_keypair(k, v, ambig, unambig)\n\n return"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_client(site=None):\n if 'CITRINATION_API_KEY' not in environ:\n raise ValueError(\"'CITRINATION_API_KEY' is not set as an environment variable\")\n if not site:\n site = environ.get(\"CITRINATION_SITE\", \"https://citrination.com\")\n return CitrinationClient(environ['CITRINATION_API_KEY'], site)", "response": "Get a citrination client"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_uids(pifs, uids=None):\n if not uids:\n uids = [str(hash(dumps(x))) for x in pifs]\n for pif, uid in zip(pifs, uids):\n pif.uid = uid\n return pifs", "response": "Set the UIDs in a PIF explicitly if the list of UIDs is passed in\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconstructs the URL for a PIF on a site", "response": "def get_url(pif, dataset, version=1, site=\"https://citrination.com\"):\n \"\"\"\n Construct the URL of a PIF on a site\n :param pif: to construct URL for\n :param dataset: the pif will belong to\n :param version: of the PIF (default: 1)\n :param site: for the dataset (default: https://citrination.com)\n :return: the URL as a string\n \"\"\"\n return \"{site}/datasets/{dataset}/version/{version}/pif/{uid}\".format(\n uid=pif.uid, version=version, dataset=dataset, site=site\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef calculate_ideal_atomic_percent(pif):\n if not isinstance(pif, ChemicalSystem):\n return pif\n if not pif.chemical_formula:\n return pif\n else:\n expanded_formula_no_special_char = _expand_formula_(\n pif.chemical_formula)\n element_array = _create_emprical_compositional_array_(\n expanded_formula_no_special_char)\n appended_e_array = _add_atomic_percents_(element_array)\n for e in appended_e_array:\n # Checks if a Composition element decribing that element already\n # exists.\n if _get_element_in_pif_composition_(pif, e[\"symbol\"]):\n # If it exists, it removes the old Composition object, and\n # inserts a new one with ideal atomic percent added.\n in_pif = _get_element_in_pif_composition_(pif, e[\"symbol\"])\n comp = in_pif[0]\n pif.composition.pop(in_pif[1])\n comp.idealAtomicPercent = e[\"atomic_percent\"]\n pif.composition.append(comp)\n else:\n # If not, it creates a new Composition object with the element\n # and ideal atomic percent.\n comp = Composition()\n comp.element = e[\"symbol\"]\n comp.idealAtomicPercent = e[\"atomic_percent\"]\n pif.composition.append(comp)\n return pif", "response": "Calculates ideal atomic percents from a chemical formula string from a pif. Returns an appended pif with composition elements modified or added."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncalculate ideal atomic weight percents from a chemical formula string from a pif. Returns an appended pif with composition elements modified or added.", "response": "def calculate_ideal_weight_percent(pif):\n \"\"\"\n Calculates ideal atomic weight percents from a chemical formula string from a pif. Returns an appended pif with composition elements modified or added.\n\n :param pif: a ChemicalSystem pif\n :return: modified pif object\n \"\"\"\n if not isinstance(pif, ChemicalSystem):\n return pif\n if not pif.chemical_formula:\n return pif\n else:\n expanded_formula_no_special_char = _expand_formula_(\n pif.chemical_formula)\n element_array = _create_emprical_compositional_array_(\n expanded_formula_no_special_char)\n appended_e_array = _add_ideal_atomic_weights_(element_array)\n a_array_with_pcts = _add_ideal_weight_percent_(appended_e_array)\n for e in a_array_with_pcts:\n # Checks if a Composition element decribing that element already\n # exists.\n if _get_element_in_pif_composition_(pif, e[\"symbol\"]):\n # If it exists, it removes the old Composition object, and\n # inserts a new one with ideal atomic weight percent added\n in_pif = _get_element_in_pif_composition_(pif, e[\"symbol\"])\n comp = in_pif[0]\n pif.composition.pop(in_pif[1])\n comp.idealWeightPercent = e[\"weight_percent\"]\n pif.composition.append(comp)\n else:\n # If not, it creates a new Composition object with the element\n # and ideal atomic weight percent.\n comp = Composition()\n comp.element = e[\"symbol\"]\n comp.idealWeightPercent = e[\"weight_percent\"]\n pif.composition.append(comp)\n return pif"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexpand a chemical formula string into a non - emperical but expanded form.", "response": "def _expand_formula_(formula_string):\n \"\"\"\n Accounts for the many ways a user may write a formula string, and returns an expanded chemical formula string.\n Assumptions:\n -The Chemical Formula string it is supplied is well-written, and has no hanging parethneses\n -The number of repeats occurs after the elemental symbol or ) ] character EXCEPT in the case of a hydrate where it is assumed to be in front of the first element\n -All hydrates explicitly use the \u00b7 symbol\n -Only (, (,[, ], ., \u00b7 are \"important\" symbols to intrepreting the string.\n -IONS ARE NOT HANDLED\n\n :param formula_string: a messy chemical formula string\n :return: a non-emperical but expanded formula string\n \"\"\"\n formula_string = re.sub(r'[^A-Za-z0-9\\(\\)\\[\\]\\\u00b7\\.]+', '', formula_string)\n hydrate_pos = formula_string.find('\u00b7')\n if hydrate_pos >= 0:\n formula_string = _expand_hydrate_(hydrate_pos, formula_string)\n search_result = re.search(\n r'(?:[\\(\\[]([A-Za-z0-9]+)[\\)\\]](\\d*))',\n formula_string)\n if search_result is None:\n return formula_string\n this_start = search_result.start()\n this_end = search_result.end()\n this_string = search_result.group()\n this_expansion_array = re.findall(\n r'(?:[\\(\\[]([A-Za-z0-9]+)[\\)\\]](\\d*))', this_string)\n for a in this_expansion_array:\n if a[1] == \"\":\n a = (a[0], 1)\n parenth_expanded = \"\"\n multiplier = float(a[1])\n element_array = re.findall('[A-Z][^A-Z]*', a[0])\n for e in element_array:\n occurance_array = re.findall('[0-9][^0-9]*', e)\n if len(occurance_array) == 0:\n occurance_array.append(1)\n for o in occurance_array:\n symbol = re.findall('[A-Z][a-z]*', e)\n total_num = float(o) * multiplier\n if total_num.is_integer():\n total_num = int(total_num)\n total_str = str(total_num)\n if total_str == \"1\":\n total_str = \"\"\n new_string = symbol[0] + total_str\n parenth_expanded += new_string\n formula_string = formula_string[0:this_start] + \\\n parenth_expanded + formula_string[this_end:]\n return _expand_formula_(formula_string)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _expand_hydrate_(hydrate_pos, formula_string):\n hydrate = formula_string[hydrate_pos + 1:]\n hydrate_string = \"\"\n multiplier = float(re.search(r'^[\\d\\.]+', hydrate).group())\n element_array = re.findall('[A-Z][^A-Z]*', hydrate)\n for e in element_array:\n occurance_array = re.findall('[0-9][^0-9]*', e)\n if len(occurance_array) == 0:\n occurance_array.append(1)\n for o in occurance_array:\n symbol = re.findall('[A-Z][a-z]*', e)\n total_num = float(o) * multiplier\n if total_num.is_integer():\n total_num = int(total_num)\n total_str = str(total_num)\n if total_str == \"1\":\n total_str = \"\"\n new_string = symbol[0] + total_str\n hydrate_string += new_string\n return formula_string[:hydrate_pos] + hydrate_string", "response": "Expands the hydrate portion of a chemical formula string to all elements of the chemical formula."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _create_compositional_array_(expanded_chemical_formaula_string):\n element_array = re.findall(\n '[A-Z][^A-Z]*',\n expanded_chemical_formaula_string)\n split_element_array = []\n for s in element_array:\n m = re.match(r\"([a-zA-Z]+)([0-9\\.]*)\", s, re.I)\n if m:\n items = m.groups()\n if items[1] == \"\":\n items = (items[0], 1)\n this_e = {\"symbol\": items[0], \"occurances\": float(items[1])}\n\n split_element_array.append(this_e)\n return split_element_array", "response": "Splits an expanded chemical formula string into an array of dictionaries containing information about each element."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconsolidate an elemental array of element dictionaries with no repeating elements.", "response": "def _consolidate_elemental_array_(elemental_array):\n \"\"\"\n Accounts for non-empirical chemical formulas by taking in the compositional array generated by _create_compositional_array_() and returning a consolidated array of dictionaries with no repeating elements\n\n :param elemental_array: an elemental array generated from _create_compositional_array_()\n :return: an array of element dictionaries\n \"\"\"\n condensed_array = []\n for e in elemental_array:\n exists = False\n for k in condensed_array:\n if k[\"symbol\"] == e[\"symbol\"]:\n exists = True\n k[\"occurances\"] += e[\"occurances\"]\n break\n if not exists:\n condensed_array.append(e)\n return condensed_array"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _add_ideal_atomic_weights_(elemental_array):\n for a in elemental_array:\n this_atomic_weight = elements_data[a[\"symbol\"]][\"atomic_weight\"]\n a[\"weight\"] = a[\"occurances\"] * this_atomic_weight\n return elemental_array", "response": "Adds the atomic weight property to each of the dictionaries in elemental_array"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd the weight_percent property to each of the dictionaries in elemental_array", "response": "def _add_ideal_weight_percent_(elemental_array):\n \"\"\"\n Adds the \"weight_percent\" property to each of the dictionaries in elemental_array\n\n :param elemental_array: an array of dictionaries containing information about the elements in the system\n :return: the appended elemental_array\n \"\"\"\n t_mass = _calculate_total_mass_(elemental_array)\n for a in elemental_array:\n a[\"weight_percent\"] = a[\"weight\"] / t_mass * 100\n return elemental_array"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _add_atomic_percents_(elemental_array):\n n_atoms = _calculate_n_atoms_(elemental_array)\n for e in elemental_array:\n e[\"atomic_percent\"] = e[\"occurances\"] / n_atoms * 100\n return elemental_array", "response": "Adds ideal atomic percents to the emperical compositional element array generated using _create_emprical_compositional_array_"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the Composition object and the position in the composition array if the element in question if in the composition array is equal to the atomic symbol of the element in question if in the composition array otherwise returns False", "response": "def _get_element_in_pif_composition_(pif, elemental_symbol):\n \"\"\"\n If the element in question if in the composition array in the pif, it returns that Composition object and the position in the composition array otherwise it returns False\n\n :param pif: ChemicalSystem Pif in question\n :param elemental_symbol: string of the atomic symbol of the element in question\n :return: either False if not found in the composition or the Compositional object along with its index in the composition array in the pif\n \"\"\"\n if pif.composition is None:\n pif.composition = []\n for i, c in enumerate(pif.composition):\n if c.element == elemental_symbol or c.element.lower(\n ) == elements_data[elemental_symbol][\"name\"].lower():\n return [c, i]\n i += 1\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_name_string(full_name):\n name = Name()\n if \",\" in full_name:\n toks = full_name.split(\",\")\n name.family = toks[0]\n name.given = \",\".join(toks[1:]).strip()\n else:\n toks = full_name.split()\n name.given = toks[0]\n name.family = \" \".join(toks[1:]).strip()\n return name", "response": "Parse a full name into a Name object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef creator_to_person(creator):\n name = Name()\n if \"creatorName\" in creator:\n name = parse_name_string(creator[\"creatorName\"])\n if \"familyName\" in creator:\n name.family = creator[\"familyName\"]\n if \"givenName\" in creator:\n name.given = creator[\"givenName\"]\n\n person = Person(name=name, tags=creator.get(\"affiliations\"))\n return person", "response": "Parse the creator block in datacite into a Person object"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nparses a top - level datacite dictionary into a Reference object", "response": "def datacite_to_pif_reference(dc):\n \"\"\"\n Parse a top-level datacite dictionary into a Reference\n :param dc: dictionary containing datacite metadata\n :return: Reference corresponding to that datacite entry\n \"\"\"\n ref = Reference()\n if dc.get('identifier', {}).get('identifierType') == \"DOI\":\n ref.doi = dc.get('identifier', {}).get('identifier')\n ref.title = dc.get('title')\n ref.publisher = dc.get('publisher')\n ref.year = dc.get('publicationYear')\n\n ref.authors = [creator_to_person(x).name for x in dc.get('creators', [])] or None\n\n return ref"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nevaluate a query and return a list of MDF records", "response": "def query_to_mdf_records(query=None, dataset_id=None, mdf_acl=None):\n \"\"\"Evaluate a query and return a list of MDF records\n\n If a datasetID is specified by there is no query, a simple\n whole dataset query is formed for the user\n \"\"\"\n if not query and not dataset_id:\n raise ValueError(\"Either query or dataset_id must be specified\")\n if query and dataset_id:\n raise ValueError(\"Both query and dataset_id were specified; pick one or the other.\")\n if not query:\n query = PifSystemReturningQuery(\n query=DataQuery(\n dataset=DatasetQuery(\n id=Filter(equal=dataset_id)\n )\n ),\n size = 10000 # Don't pull down all the results by default\n )\n\n client = get_client()\n\n if not mdf_acl:\n raise ValueError('Access controls (mdf_acl) must be specified. Use [\"public\"] for public access')\n\n pif_result = client.pif_search(query)\n if len(pif_result.hits) == 0:\n return []\n\n example_uid = pif_result.hits[0].system.uid\n dataset_query = DatasetReturningQuery(\n query=DataQuery(\n system=PifSystemQuery(\n uid=Filter(equal=example_uid)\n )\n ),\n size = 1 # we only expect one dataset to hit\n )\n\n dataset_result = client.dataset_search(dataset_query)\n\n records = []\n for hit in pif_result.hits:\n records.append(pif_to_mdf_record(hit.system, dataset_result.hits[0], mdf_acl))\n\n return records"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pif_to_mdf_record(pif_obj, dataset_hit, mdf_acl):\n res = {}\n res[\"mdf\"] = _to_meta_data(pif_obj, dataset_hit, mdf_acl)\n res[res[\"mdf\"][\"source_name\"]] = _to_user_defined(pif_obj)\n return dumps(res)", "response": "Convert a PIF into a MDF record"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _to_meta_data(pif_obj, dataset_hit, mdf_acl):\n pif = pif_obj.as_dictionary()\n dataset = dataset_hit.as_dictionary()\n mdf = {}\n try:\n if pif.get(\"names\"):\n mdf[\"title\"] = pif[\"names\"][0]\n else:\n mdf[\"title\"] = \"Citrine PIF \" + str(pif[\"uid\"])\n\n if pif.get(\"chemicalFormula\"):\n mdf[\"composition\"] = pif[\"chemicalFormula\"]\n elif pif.get(\"composition\"):\n mdf[\"composition\"] = ''.join([comp[\"element\"] for comp in pif[\"composition\"] if comp[\"element\"]])\n if not mdf[\"composition\"]:\n mdf.pop(\"composition\")\n\n mdf[\"acl\"] = mdf_acl\n mdf[\"source_name\"] = _construct_new_key(dataset[\"name\"])\n\n if pif.get(\"contacts\"):\n mdf[\"data_contact\"] = []\n for contact in pif[\"contacts\"]:\n data_c = {\n \"given_name\": contact[\"name\"][\"given\"], #REQ\n \"family_name\": contact[\"name\"][\"family\"] #REQ\n }\n if contact.get(\"email\"):\n data_c[\"email\"] = contact.get(\"email\", \"\")\n if contact.get(\"orcid\"):\n data_c[\"orcid\"] = contact.get(\"orcid\", \"\")\n mdf[\"data_contact\"].append(data_c)\n if not mdf[\"data_contact\"]:\n mdf.pop(\"data_contact\")\n \n mdf[\"data_contributor\"] = [{}] \n if \"owner\" in dataset:\n name = dataset[\"owner\"].split()\n contributor = {\n \"given_name\": name[0],\n \"family_name\": name[1],\n \"email\": dataset[\"email\"]\n }\n mdf[\"data_contributor\"] = [contributor]\n\n mdf[\"links\"] = {\n \"landing_page\": \"https://citrination.com/datasets/{}\".format(dataset[\"id\"]),\n \"publication\": []\n }\n if pif.get(\"references\"):\n mdf[\"author\"] = []\n mdf[\"citation\"] = []\n for ref in pif[\"references\"]:\n if ref.get(\"doi\"):\n mdf[\"citation\"].append(ref[\"doi\"]) #TODO: Make actual citation\n mdf[\"links\"][\"publication\"].append(ref[\"doi\"])\n if ref.get(\"authors\"):\n for author in ref[\"authors\"]:\n if author.get(\"given\") and author.get(\"family\"):\n mdf[\"author\"].append({\n \"given_name\": author[\"given\"],\n \"family_name\": author[\"family\"]\n })\n # Remove fields if blank\n if not mdf[\"author\"]:\n mdf.pop(\"author\")\n if not mdf[\"citation\"]:\n mdf.pop(\"citation\")\n if not mdf[\"links\"][\"publication\"]:\n mdf[\"links\"].pop(\"publication\")\n\n if pif.get(\"licenses\", [{}])[0].get(\"url\"):\n mdf[\"license\"] = pif[\"licenses\"][0][\"url\"]\n if pif.get(\"tags\"):\n mdf[\"tags\"] = pif[\"tags\"]\n\n # If required MDF metadata is missing from PIF, abort\n except KeyError as e:\n print(\"Error: Required MDF metadata\", str(e), \"not found in PIF\", pif[\"uid\"])\n return None\n\n return mdf", "response": "Convert the meta - data from the PIF into the MDF."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread the system in the PIF to populate the user - defined portion of the system.", "response": "def _to_user_defined(pif_obj):\n \"\"\"Read the systems in the PIF to populate the user-defined portion\"\"\"\n res = {}\n\n # make a read view to flatten the hierarchy\n rv = ReadView(pif_obj)\n\n # Iterate over the keys in the read view\n for k in rv.keys():\n name, value = _extract_key_value(rv[k].raw)\n # add any objects that can be extracted\n if name and value is not None:\n res[name] = value\n\n # Grab interesting values not in the ReadView\n pif = pif_obj.as_dictionary()\n\n elements = {}\n if pif.get(\"composition\"):\n for comp in pif[\"composition\"]:\n if comp.get(\"actualAtomicPercent\"):\n elements[comp[\"element\"]] = float(comp[\"actualAtomicPercent\"][\"value\"])\n elif comp.get(\"actualWeightPercent\"):\n elements[comp[\"element\"]] = float(comp[\"actualWeightPercent\"][\"value\"])\n if elements:\n res[\"elemental_percent\"] = elements\n elif pif.get(\"chemicalFormula\"):\n symbol = \"\"\n num = \"\"\n # Chemical formulae are comprised of letters, numbers, and potentially characters we don't care about\n for char in pif[\"chemicalFormula\"]:\n # Uppercase char indicates beginning of new symbol\n if char.isupper():\n # If there is already a symbol in holding, process it\n if symbol:\n try:\n elements[symbol] = int(num)\n # If num is a float, raises ValueError\n except ValueError:\n elements[symbol] = float(num) if num else 1\n symbol = \"\"\n num = \"\"\n symbol += char\n # Lowercase chars or digits are continuations of a symbol\n elif char.islower():\n symbol += char\n elif char.isdigit():\n num += char\n elif char == \".\":\n num += char\n # All other chars are not useful\n if elements:\n res[\"elemental_proportion\"] = elements\n return res"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconstructing a MDF safe key from the name and units", "response": "def _construct_new_key(name, units=None):\n \"\"\"Construct an MDF safe key from the name and units\"\"\"\n to_replace = [\"/\", \"\\\\\", \"*\", \"^\", \"#\", \" \", \"\\n\", \"\\t\", \",\", \".\", \")\", \"(\", \"'\", \"`\", \"-\"]\n to_remove = [\"$\", \"{\", \"}\"]\n\n cat = name\n if units:\n cat = \"_\".join([name, units])\n for c in to_replace:\n cat = cat.replace(c, \"_\")\n for c in to_remove:\n cat = cat.replace(c, \"\")\n\n cat = re.sub('_+','_', cat)\n\n return cat"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _extract_key_value(obj):\n key = None; value = None\n\n # Parse a Value object, which includes Properties\n if isinstance(obj, Value):\n key = _construct_new_key(obj.name, obj.units)\n value = []\n if obj.scalars:\n value = [(val.value if isinstance(val, Scalar) else val)\n for val in obj.scalars]\n elif obj.vectors and len(obj.vectors) == 1:\n value = [(val.value if isinstance(val, Scalar) else val)\n for val in obj.vectors[0]]\n if len(value) == 1:\n value = value[0]\n elif len(value) == 0:\n value = None\n\n # If there is a process step, pul out its name as the value\n # TODO: resolve duplicates\n if isinstance(obj, ProcessStep):\n key = \"Processing\"\n value = obj.name\n \n return key, value", "response": "Extract the value from the object and make a descriptive key"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef process_input_buffer():\n from polysh.control_commands_helpers import handle_control_command\n data = the_stdin_thread.input_buffer.get()\n remote_dispatcher.log(b'> ' + data)\n\n if data.startswith(b':'):\n try:\n handle_control_command(data[1:-1].decode())\n except UnicodeDecodeError as e:\n console_output(b'Could not decode command.')\n return\n\n if data.startswith(b'!'):\n try:\n retcode = subprocess.call(data[1:], shell=True)\n except OSError as e:\n if e.errno == errno.EINTR:\n console_output(b'Child was interrupted\\n')\n retcode = 0\n else:\n raise\n if retcode > 128 and retcode <= 192:\n retcode = 128 - retcode\n if retcode > 0:\n console_output('Child returned {:d}\\n'.format(retcode).encode())\n elif retcode < 0:\n console_output('Child was terminated by signal {:d}\\n'.format(\n -retcode).encode())\n return\n\n for r in dispatchers.all_instances():\n try:\n r.dispatch_command(data)\n except asyncore.ExitNow as e:\n raise e\n except Exception as msg:\n raise msg\n console_output('{} for {}, disconnecting\\n'.format(\n str(msg), r.display_name).encode())\n r.disconnect()\n else:\n if r.enabled and r.state is remote_dispatcher.STATE_IDLE:\n r.change_state(remote_dispatcher.STATE_RUNNING)", "response": "Send the content of the input buffer to all remote processes"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef write_main_socket(c):\n the_stdin_thread.socket_write.send(c)\n while True:\n try:\n the_stdin_thread.socket_write.recv(1)\n except socket.error as e:\n if e.errno != errno.EINTR:\n raise\n else:\n break", "response": "Synchronous write to the main socket wait for ACK"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntry to get the PID of the stdin thread otherwise get the whole process ID", "response": "def get_stdin_pid(cached_result=None):\n \"\"\"Try to get the PID of the stdin thread, otherwise get the whole process\n ID\"\"\"\n if cached_result is None:\n try:\n tasks = os.listdir('/proc/self/task')\n except OSError as e:\n if e.errno != errno.ENOENT:\n raise\n cached_result = os.getpid()\n else:\n tasks.remove(str(os.getpid()))\n assert len(tasks) == 1\n cached_result = int(tasks[0])\n return cached_result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef interrupt_stdin_thread():\n dupped_stdin = os.dup(0) # Backup the stdin fd\n assert not the_stdin_thread.interrupt_asked # Sanity check\n the_stdin_thread.interrupt_asked = True # Not user triggered\n os.lseek(tempfile_fd, 0, 0) # Rewind in the temp file\n os.dup2(tempfile_fd, 0) # This will make raw_input() return\n pid = get_stdin_pid()\n os.kill(pid, signal.SIGWINCH) # Try harder to wake up raw_input()\n the_stdin_thread.out_of_raw_input.wait() # Wait for this return\n the_stdin_thread.interrupt_asked = False # Restore sanity\n os.dup2(dupped_stdin, 0) # Restore stdin\n os.close(dupped_stdin)", "response": "Interrupt the stdin thread."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add(self, data):\n assert isinstance(data, bytes)\n with self.lock:\n self.buf += data", "response": "Add data to the buffer"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get(self):\n data = b''\n with self.lock:\n data, self.buf = self.buf, b''\n\n return data", "response": "Get the content of the buffer"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nhandle all the available character commands in the socket.", "response": "def handle_read(self):\n \"\"\"Handle all the available character commands in the socket\"\"\"\n while True:\n try:\n c = self.recv(1)\n except socket.error as e:\n if e.errno == errno.EWOULDBLOCK:\n return\n else:\n raise\n else:\n self._do(c)\n self.socket.setblocking(True)\n self.send(b'A')\n self.socket.setblocking(False)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef decker_sum(a, b):\n x = a + b\n y = b - (x - a) if abs(a) > abs(b) else a - (x - b)\n return x, y", "response": "Compute the knuth sum of two numbers."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ksum(p, K=2):\n # Don't override the input data.\n q = p.copy()\n distill(q, K - 1)\n return numpy.sum(q[:-1], axis=0) + q[-1]", "response": "Sum the data in a sequence of K elements."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nenables or disable the specified shells.", "response": "def toggle_shells(command, enable):\n \"\"\"Enable or disable the specified shells. If the command would have\n no effect, it changes all other shells to the inverse enable value.\"\"\"\n selection = list(selected_shells(command))\n if command and command != '*' and selection:\n for i in selection:\n if i.state != remote_dispatcher.STATE_DEAD and i.enabled != enable:\n break\n else:\n toggle_shells('*', not enable)\n\n for i in selection:\n if i.state != remote_dispatcher.STATE_DEAD:\n i.set_enabled(enable)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef selected_shells(command):\n if not command or command == '*':\n for i in dispatchers.all_instances():\n yield i\n return\n selected = set()\n instance_found = False\n for pattern in command.split():\n found = False\n for expanded_pattern in expand_syntax(pattern):\n for i in dispatchers.all_instances():\n instance_found = True\n if fnmatch(i.display_name, expanded_pattern):\n found = True\n if i not in selected:\n selected.add(i)\n yield i\n if instance_found and not found:\n console_output('{} not found\\n'.format(pattern).encode())", "response": "Iterator over the shells with names matching the patterns."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef complete_shells(line, text, predicate=lambda i: True):\n res = [i.display_name + ' ' for i in dispatchers.all_instances() if\n i.display_name.startswith(text) and\n predicate(i) and\n ' ' + i.display_name + ' ' not in line]\n return res", "response": "Return the shell names to include in the completion"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef kdot(x, y, K=2):\n xx = x.reshape(-1, x.shape[-1])\n yy = y.reshape(y.shape[0], -1)\n\n xx = numpy.ascontiguousarray(xx)\n yy = numpy.ascontiguousarray(yy)\n\n r = _accupy.kdot_helper(xx, yy).reshape((-1,) + x.shape[:-1] + y.shape[1:])\n return ksum(r, K - 1)", "response": "Algorithm 5. 10. Dot product algorithm in K - fold working precision"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef kill_all():\n for i in dispatchers.all_instances():\n try:\n os.kill(-i.pid, signal.SIGKILL)\n except OSError:\n # The process was already dead, no problem\n pass", "response": "Kill all the remote shells"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef main():\n\n sentry_dsn = os.environ.get('POLYSH_SENTRY_DSN')\n\n if sentry_dsn:\n from raven import Client\n client = Client(\n dsn=sentry_dsn,\n release='.'.join(map(str, VERSION)),\n ignore_exceptions=[\n KeyboardInterrupt\n ]\n )\n\n try:\n run()\n except Exception:\n client.captureException()\n\n else:\n run()", "response": "Wrapper around run to setup sentry"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nhandling a read chunk of data from the socket.", "response": "def _handle_read_chunk(self):\n \"\"\"Some data can be read\"\"\"\n new_data = b''\n buffer_length = len(self.read_buffer)\n try:\n while buffer_length < self.MAX_BUFFER_SIZE:\n try:\n piece = self.recv(4096)\n except OSError as e:\n if e.errno == errno.EAGAIN:\n # End of the available data\n break\n elif e.errno == errno.EIO and new_data:\n # Hopefully we could read an error message before the\n # actual termination\n break\n else:\n raise\n\n if not piece:\n # A closed connection is indicated by signaling a read\n # condition, and having recv() return 0.\n break\n\n new_data += piece\n buffer_length += len(piece)\n\n finally:\n new_data = new_data.replace(b'\\r', b'\\n')\n self.read_buffer += new_data\n return new_data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\naugmenting the buffer with stuff to write when possible", "response": "def dispatch_write(self, buf):\n \"\"\"Augment the buffer with stuff to write when possible\"\"\"\n self.write_buffer += buf\n if len(self.write_buffer) > self.MAX_BUFFER_SIZE:\n console_output('Buffer too big ({:d}) for {}\\n'.format(\n len(self.write_buffer), str(self)).encode())\n raise asyncore.ExitNow(1)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef safe_write(buf):\n assert isinstance(buf, bytes)\n while True:\n try:\n os.write(1, buf)\n break\n except IOError as e:\n if e.errno != errno.EINTR:\n raise", "response": "Write a buffer to the internal memory."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nuse instead of print to clear the status information before printing", "response": "def console_output(msg, logging_msg=None):\n \"\"\"Use instead of print, to clear the status information before printing\"\"\"\n assert isinstance(msg, bytes)\n assert isinstance(logging_msg, bytes) or logging_msg is None\n\n from polysh import remote_dispatcher\n\n remote_dispatcher.log(logging_msg or msg)\n if remote_dispatcher.options.interactive:\n from polysh.stdin import the_stdin_thread\n the_stdin_thread.no_raw_input()\n global last_status_length\n if last_status_length:\n safe_write('\\r{}\\r'.format(\n last_status_length * ' ').encode())\n last_status_length = 0\n safe_write(msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a tuple with the number of awaited processes and the total number of all active resources.", "response": "def count_awaited_processes():\n \"\"\"Return a tuple with the number of awaited processes and the total\n number\"\"\"\n awaited = 0\n total = 0\n for i in all_instances():\n if i.enabled:\n total += 1\n if i.state is not remote_dispatcher.STATE_IDLE:\n awaited += 1\n return awaited, total"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update_terminal_size():\n w, h = terminal_size()\n w = max(w - display_names.max_display_name_length - 2, min(w, 10))\n # python bug http://python.org/sf/1112949 on amd64\n # from ajaxterm.py\n bug = struct.unpack('i', struct.pack('I', termios.TIOCSWINSZ))[0]\n packed_size = struct.pack('HHHH', h, w, 0, 0)\n term_size = w, h\n for i in all_instances():\n if i.enabled and i.term_size != term_size:\n i.term_size = term_size\n fcntl.ioctl(i.fd, bug, packed_size)", "response": "Propagate the terminal size to the remote shells accounting for the longest name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nturn a 2 - dimension list of bytes into a 1 - dimension list of bytes with correct spacing", "response": "def format_info(info_list):\n \"\"\"Turn a 2-dimension list of bytes into a 1-dimension list of bytes with\n correct spacing\"\"\"\n\n max_lengths = []\n if info_list:\n nr_columns = len(info_list[0])\n else:\n nr_columns = 0\n for i in range(nr_columns):\n max_lengths.append(max([len(info[i]) for info in info_list]))\n\n flattened_info_list = []\n for info_id in range(len(info_list)):\n info = info_list[info_id]\n for str_id in range(len(info) - 1):\n # Don't justify the last column (i.e. the last printed line)\n # as it can get much longer in some shells than in others\n orig_str = info[str_id]\n indent = max_lengths[str_id] - len(orig_str)\n info[str_id] = orig_str + indent * b' '\n flattened_info_list.append(b' '.join(info) + b'\\n')\n\n return flattened_info_list"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef generate_ill_conditioned_dot_product(n, c, dps=100):\n # Algorithm 6.1 from\n #\n # ACCURATE SUM AND DOT PRODUCT,\n # TAKESHI OGITA, SIEGFRIED M. RUMP, AND SHIN'ICHI OISHI.\n assert n >= 6\n n2 = round(n / 2)\n x = numpy.zeros(n)\n y = numpy.zeros(n)\n\n b = math.log2(c)\n # vector of exponents between 0 and b/2:\n e = numpy.rint(numpy.random.rand(n2) * b / 2).astype(int)\n # make sure exponents b/2 and 0 actually occur in e\n # vectors x,y\n e[0] = round(b / 2) + 1\n e[-1] = 0\n\n # generate first half of vectors x, y\n rx, ry = numpy.random.rand(2, n2)\n x[:n2] = (2 * rx - 1) * 2 ** e\n y[:n2] = (2 * ry - 1) * 2 ** e\n\n def dot_exact(x, y):\n mp.dps = dps\n # convert to list first, see\n # \n return mp.fdot(x.tolist(), y.tolist())\n\n # for i=n2+1:n and v=1:i,\n # generate x_i, y_i such that (*) x(v)\u2019*y(v) ~ 2^e(i-n2)\n # generate exponents for second half\n e = numpy.rint(numpy.linspace(b / 2, 0, n - n2)).astype(int)\n rx, ry = numpy.random.rand(2, n2)\n for i in range(n2, n):\n # x_i random with generated exponent\n x[i] = (2 * rx[i - n2] - 1) * 2 ** e[i - n2]\n # y_i according to (*)\n y[i] = (\n (2 * ry[i - n2] - 1) * 2 ** e[i - n2] - dot_exact(x[: i + 1], y[: i + 1])\n ) / x[i]\n\n x, y = numpy.random.permutation((x, y))\n # the true dot product rounded to nearest floating point\n d = dot_exact(x, y)\n # the actual condition number\n C = 2 * dot_exact(abs(x), abs(y)) / abs(d)\n\n return x, y, d, C", "response": "Generate a random illumination conditioned DOT product."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the number of RemoteDispatcher.handle_read() calls made by this iteration", "response": "def main_loop_iteration(timeout=None):\n \"\"\"Return the number of RemoteDispatcher.handle_read() calls made by this\n iteration\"\"\"\n prev_nr_read = nr_handle_read\n asyncore.loop(count=1, timeout=timeout, use_poll=True)\n return nr_handle_read - prev_nr_read"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlaunch the ssh command in the child process", "response": "def launch_ssh(self, name, port):\n \"\"\"Launch the ssh command in the child process\"\"\"\n if options.user:\n name = '%s@%s' % (options.user, name)\n evaluated = options.ssh % {'host': name, 'port': port}\n if evaluated == options.ssh:\n evaluated = '%s %s' % (evaluated, name)\n os.execlp('/bin/sh', 'sh', '-c', evaluated)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef change_state(self, state):\n if state is not self.state:\n if self.debug:\n self.print_debug(b'state => ' + STATE_NAMES[state].encode())\n if self.state is STATE_NOT_STARTED:\n self.read_in_state_not_started = b''\n self.state = state", "response": "Change the state of the remote process logging the change"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef disconnect(self):\n try:\n os.kill(-self.pid, signal.SIGKILL)\n except OSError:\n # The process was already dead, no problem\n pass\n self.read_buffer = b''\n self.write_buffer = b''\n self.set_enabled(False)\n if self.read_in_state_not_started:\n self.print_lines(self.read_in_state_not_started)\n self.read_in_state_not_started = b''\n if options.abort_error and self.state is STATE_NOT_STARTED:\n raise asyncore.ExitNow(1)\n self.change_state(STATE_DEAD)", "response": "Disconnects from the remote process."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconfigure the terminal to use the termios module.", "response": "def configure_tty(self):\n \"\"\"We don't want \\n to be replaced with \\r\\n, and we disable the echo\"\"\"\n attr = termios.tcgetattr(self.fd)\n attr[1] &= ~termios.ONLCR # oflag\n attr[3] &= ~termios.ECHO # lflag\n termios.tcsetattr(self.fd, termios.TCSANOW, attr)\n # unsetopt zle prevents Zsh from resetting the tty\n return b'unsetopt zle 2> /dev/null;stty -echo -onlcr -ctlecho;'"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_prompt(self):\n # No right prompt\n command_line = b'PS2=;RPS1=;RPROMPT=;'\n command_line += b'PROMPT_COMMAND=;'\n command_line += b'TERM=ansi;'\n command_line += b'unset HISTFILE;'\n prompt1, prompt2 = callbacks.add(b'prompt', self.seen_prompt_cb, True)\n command_line += b'PS1=\"' + prompt1 + b'\"\"' + prompt2 + b'\\n\"\\n'\n return command_line", "response": "Set the prompt for the process holding the lock."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling the fast case of reading the entries in the log file.", "response": "def handle_read_fast_case(self, data):\n \"\"\"If we are in a fast case we'll avoid the long processing of each\n line\"\"\"\n if self.state is not STATE_RUNNING or callbacks.any_in(data):\n # Slow case :-(\n return False\n\n last_nl = data.rfind(b'\\n')\n if last_nl == -1:\n # No '\\n' in data => slow case\n return False\n self.read_buffer = data[last_nl + 1:]\n self.print_lines(data[:last_nl])\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nhandle a single line of the read buffer.", "response": "def handle_read(self):\n \"\"\"We got some output from a remote shell, this is one of the state\n machine\"\"\"\n if self.state == STATE_DEAD:\n return\n global nr_handle_read\n nr_handle_read += 1\n new_data = self._handle_read_chunk()\n if self.debug:\n self.print_debug(b'==> ' + new_data)\n if self.handle_read_fast_case(self.read_buffer):\n return\n lf_pos = new_data.find(b'\\n')\n if lf_pos >= 0:\n # Optimization: we knew there were no '\\n' in the previous read\n # buffer, so we searched only in the new_data and we offset the\n # found index by the length of the previous buffer\n lf_pos += len(self.read_buffer) - len(new_data)\n elif self.state is STATE_NOT_STARTED and \\\n options.password is not None and \\\n b'password:' in self.read_buffer.lower():\n self.dispatch_write('{}\\n'.format(options.password).encode())\n self.read_buffer = b''\n return\n while lf_pos >= 0:\n # For each line in the buffer\n line = self.read_buffer[:lf_pos + 1]\n if callbacks.process(line):\n pass\n elif self.state in (STATE_IDLE, STATE_RUNNING):\n self.print_lines(line)\n elif self.state is STATE_NOT_STARTED:\n self.read_in_state_not_started += line\n if b'The authenticity of host' in line:\n msg = line.strip(b'\\n') + b' Closing connection.'\n self.disconnect()\n elif b'REMOTE HOST IDENTIFICATION HAS CHANGED' in line:\n msg = b'Remote host identification has changed.'\n else:\n msg = None\n\n if msg:\n self.print_lines(msg + b' Consider manually connecting or '\n b'using ssh-keyscan.')\n\n # Go to the next line in the buffer\n self.read_buffer = self.read_buffer[lf_pos + 1:]\n if self.handle_read_fast_case(self.read_buffer):\n return\n lf_pos = self.read_buffer.find(b'\\n')\n if self.state is STATE_NOT_STARTED and not self.init_string_sent:\n self.dispatch_write(self.init_string)\n self.init_string_sent = True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef print_unfinished_line(self):\n if self.state is STATE_RUNNING:\n if not callbacks.process(self.read_buffer):\n self.print_lines(self.read_buffer)\n self.read_buffer = b''", "response": "Print the unfinished line of the log."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef handle_write(self):\n num_sent = self.send(self.write_buffer)\n if self.debug:\n if self.state is not STATE_NOT_STARTED or options.password is None:\n self.print_debug(b'<== ' + self.write_buffer[:num_sent])\n self.write_buffer = self.write_buffer[num_sent:]", "response": "Let s write as much as we can."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlogs some debugging information to the console", "response": "def print_debug(self, msg):\n \"\"\"Log some debugging information to the console\"\"\"\n assert isinstance(msg, bytes)\n state = STATE_NAMES[self.state].encode()\n console_output(b'[dbg] ' + self.display_name.encode() + b'[' + state +\n b']: ' + msg + b'\\n')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_info(self):\n return [self.display_name.encode(),\n self.enabled and b'enabled' or b'disabled',\n STATE_NAMES[self.state].encode() + b':',\n self.last_printed_line.strip()]", "response": "Return a list with all information available about this process"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndispatches a buffer to the appropriate handler.", "response": "def dispatch_write(self, buf):\n \"\"\"There is new stuff to write when possible\"\"\"\n if self.state != STATE_DEAD and self.enabled:\n super().dispatch_write(buf)\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nchange the name of the shell possibly updating the maximum name length", "response": "def change_name(self, new_name):\n \"\"\"Change the name of the shell, possibly updating the maximum name\n length\"\"\"\n if not new_name:\n name = self.hostname\n else:\n name = new_name.decode()\n self.display_name = display_names.change(\n self.display_name, name)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsending to the remote shell its new name to be shell expanded", "response": "def rename(self, name):\n \"\"\"Send to the remote shell, its new name to be shell expanded\"\"\"\n if name:\n # defug callback add?\n rename1, rename2 = callbacks.add(\n b'rename', self.change_name, False)\n self.dispatch_command(b'/bin/echo \"' + rename1 + b'\"\"' + rename2 +\n b'\"' + name + b'\\n')\n else:\n self.change_name(self.hostname.encode())"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef terminal_size(): # decide on *some* terminal size\n cr = _ioctl_GWINSZ(0) or _ioctl_GWINSZ(\n 1) or _ioctl_GWINSZ(2) # try open fds\n if not cr: # ...then ctty\n try:\n fd = os.open(os.ctermid(), os.O_RDONLY)\n cr = _ioctl_GWINSZ(fd)\n os.close(fd)\n except BaseException:\n pass\n if not cr: # env vars or finally defaults\n try:\n cr = os.environ['LINES'], os.environ['COLUMNS']\n except BaseException:\n cr = 25, 80\n return int(cr[1]), int(cr[0])", "response": "Return the number of lines and columns of a terminal."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef complete(text, state):\n global completion_results\n if state == 0:\n line = readline.get_line_buffer()\n if line.startswith(':'):\n # Control command completion\n completion_results = complete_control_command(line, text)\n else:\n if line.startswith('!') and text and line.startswith(text):\n dropped_exclam = True\n text = text[1:]\n else:\n dropped_exclam = False\n completion_results = []\n # Complete local paths\n completion_results += complete_local_path(text)\n # Complete from history\n l = len(text)\n completion_results += [w + ' ' for w in history_words if\n len(w) > l and w.startswith(text)]\n if readline.get_begidx() == 0:\n # Completing first word from $PATH\n completion_results += [w + ' ' for w in user_commands_in_path\n if len(w) > l and w.startswith(text)]\n completion_results = remove_dupes(completion_results)\n if dropped_exclam:\n completion_results = ['!' + r for r in completion_results]\n\n if state < len(completion_results):\n return completion_results[state]\n completion_results = None\n return None", "response": "Completes the given text with the next possible completion."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef Popen(*args, **kwargs):\n\n read_line = None\n if 'read_line' in kwargs:\n read_line = kwargs['read_line']\n del kwargs['read_line']\n\n p = subprocess.Popen(*args, **kwargs)\n wait_stdout = None\n wait_stderr = None\n\n if p.stdout:\n wait_stdout = sys.stdout.attach(p.stdout, read_line=read_line)\n if p.stderr:\n wait_stderr = sys.stderr.attach(p.stderr)\n\n original_wait = p.wait\n def wait():\n original_wait()\n\n if wait_stdout:\n wait_stdout()\n if wait_stderr:\n wait_stderr()\n\n p.wait = wait\n\n return p", "response": "Executes a command using subprocess. Popen and redirects output to AETROS and stdout API calls."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef context():\n job = JobBackend()\n\n offline = False\n if '1' == os.getenv('AETROS_OFFLINE', ''):\n offline = True\n\n if os.getenv('AETROS_JOB_ID'):\n job.load(os.getenv('AETROS_JOB_ID'))\n if not offline:\n job.connect()\n else:\n job.create()\n if not offline:\n job.connect()\n\n job.start(offline=offline)\n\n return job", "response": "Returns a JobBackend instance which connects to AETROS Trainer\n based on model name in aetros. yml or environment variable AETROS_MODEL_NAME environment variable."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall by the process when SIGINT is received.", "response": "def on_sigint(self, sig, frame):\n \"\"\"\n We got SIGINT signal.\n \"\"\"\n\n if self.stop_requested or self.stop_requested_force:\n # signal has already been sent or we force a shutdown.\n # handles the keystroke 2x CTRL+C to force an exit.\n self.stop_requested_force = True\n self.logger.warning('Force stopped: ' + str(sig))\n\n # just kill the process, we don't care about the results\n self.on_force_exit()\n os._exit(1)\n # with force_exit we really close the process, killing it in unknown state\n # self.fail('Force stopped', force_exit=True)\n # return\n\n if self.is_master_process():\n self.logger.warning('Received signal '+str(sig)+'. Send again to force stop. Stopping ...')\n else:\n self.logger.debug(\"Got child signal \" + str(sig))\n\n self.stop_requested = True\n\n # the default SIGINT handle in python is not always installed, so we can't rely on the\n # KeyboardInterrupt exception to be thrown.\n # thread.interrupt_main would call sigint again.\n # the shutdown listener will do the rest like committing rest memory files into Git and closing connections.\n sys.exit(0 if self.in_early_stop else 1)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef external_aborted(self, params):\n self.ended = True\n self.running = False\n\n # When the server sends an abort signal, we really have to close immediately,\n # since for example the job has been already deleted.\n # without touching the git and client any further\n os._exit(1)", "response": "This is called when the server sends an external abort signal."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef external_stop(self, force):\n\n # only the master processes handles the regular stop signal from the server, sending a SIGINT to\n # all its child (means to us, non-master process)\n if not self.is_master_process():\n if force:\n # make sure even the subprocess dies really on force\n os._exit(1)\n\n return\n\n self.logger.warning(\"Received stop signal by server.\")\n if not self.stop_requested_force:\n self.stop_requested_force = force\n\n raise_sigint()", "response": "Stop signal by server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef step(self, step, total, label='STEP', speed_label='STEPS/S', size=1):\n\n self.lock.acquire()\n try:\n time_diff = time.time() - self.last_step_time\n\n if self.last_step > step:\n # it restarted\n self.last_step = 0\n\n made_steps_since_last_call = step - self.last_step\n self.last_step = step\n\n self.made_steps_since_last_sync += made_steps_since_last_call\n self.made_steps_size_since_last_sync += made_steps_since_last_call * size\n\n if time_diff >= 1 or step == total: # only each second or last batch\n self.set_system_info('step', step, True)\n self.set_system_info('steps', total, True)\n\n steps_per_second = self.made_steps_since_last_sync / time_diff\n samples_per_second = self.made_steps_size_since_last_sync / time_diff\n self.last_step_time = time.time()\n\n if size:\n self.report_speed(samples_per_second)\n\n epochs_per_second = steps_per_second / total # all batches\n self.set_system_info('epochsPerSecond', epochs_per_second, True)\n\n current_epochs = self.current_epoch if self.current_epoch else 1\n total_epochs = self.total_epochs if self.total_epochs else 1\n\n self.made_steps_since_last_sync = 0\n self.made_steps_size_since_last_sync = 0\n\n eta = 0\n if step < total:\n # time to end this epoch\n if steps_per_second != 0:\n eta = (total - step) / steps_per_second\n\n # time until all epochs are done\n if total_epochs - current_epochs > 0:\n if epochs_per_second != 0:\n eta += (total_epochs - (current_epochs)) / epochs_per_second\n\n self.git.store_file('aetros/job/times/eta.json', simplejson.dumps(eta))\n\n if label and self.step_label != label:\n self.set_system_info('stepLabel', label, True)\n self.step_label = label\n\n if speed_label and self.step_speed_label != speed_label:\n self.set_system_info('stepSpeedLabel', speed_label, True)\n self.step_speed_label = speed_label\n finally:\n self.lock.release()", "response": "Increase the step indicator."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a new loss channel.", "response": "def create_loss_channel(self, name='loss', xaxis=None, yaxis=None, layout=None):\n \"\"\"\n :param name: string\n :return: JobLossGraph\n \"\"\"\n\n return JobLossChannel(self, name, xaxis, yaxis, layout)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_channel(self, name, traces=None,\n main=False, kpi=False, kpiTrace=0, max_optimization=True,\n type=JobChannel.NUMBER,\n xaxis=None, yaxis=None, layout=None):\n \"\"\"\n :param name: str\n :param traces: None|list : per default create a trace based on \"name\".\n :param main: bool : whether this channel is visible in the job list as column for better comparison.\n\n :param kpi: bool : whether this channel is the KPI (key performance indicator).\n Used for hyperparameter optimization. Only one channel can be a kpi. Only first trace used.\n :param kpiTrace: bool : if you have multiple traces, define which is the KPI. 0 based index.\n\n :param max_optimization: bool : whether the optimization maximizes or minmizes the kpi. Use max_optimization=False to\n tell the optimization algorithm that his channel minimizes a kpi, for instance the loss of a model.\n\n :param type: str : One of JobChannel.NUMBER, JobChannel.TEXT\n :param xaxis: dict\n :param yaxis: dict\n :param layout: dict\n \"\"\"\n return JobChannel(self, name, traces, main, kpi, kpiTrace, max_optimization, type, xaxis, yaxis, layout)", "response": "Create a new channel based on the given parameters."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncall by the daemon to handle shutdown.", "response": "def on_shutdown(self):\n \"\"\"\n Shutdown routine. Sets the last progress (done, aborted, failed) and tries to send last logs and git commits.\n Also makes sure the ssh connection is closed (thus, the job marked as offline).\n\n Is triggered by atexit.register().\n \"\"\"\n\n self.in_shutdown = True\n\n self.logger.debug('on_shutdown, stopped=%s, ended=%s, early_stop=%s, stop_requested=%s'\n % (str(self.stopped), str(self.ended), str(self.in_early_stop), str(self.stop_requested)))\n if self.stopped or self.ended:\n # make really sure, ssh connection closed\n self.client.close()\n return\n\n if self.in_early_stop:\n self.done()\n return\n\n if self.stop_requested:\n # when SIGINT has been triggered\n if self.stop_requested_force:\n if not self.is_master_process():\n # if not master process, we just stop everything. status/progress is set by master\n self.stop(force_exit=True)\n else:\n # master process\n self.fail('Force stopped.', force_exit=True)\n else:\n if not self.is_master_process():\n # if not master process, we just stop everything. status/progress is set by master\n self.stop()\n else:\n # master process\n self.abort()\n\n return\n\n if hasattr(sys, 'last_value'):\n # sys.last_value contains a exception, when there was an uncaught one\n if isinstance(sys.last_value, KeyboardInterrupt):\n # can only happen when KeyboardInterrupt has been raised manually\n # since the one from the default sigint handler will never reach here\n # since we catch the sigint signal and sys.exit() before the default sigint handler\n # is able to raise KeyboardInterrupt\n self.abort()\n else:\n self.fail(type(sys.last_value).__name__ + ': ' + str(sys.last_value))\n\n elif self.running:\n self.done()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef fail(self, message=None, force_exit=False):\n global last_exit_code\n\n if not last_exit_code:\n last_exit_code = 1\n\n with self.git.batch_commit('FAILED'):\n self.set_status('FAILED', add_section=False)\n self.git.commit_json_file('FAIL_MESSAGE', 'aetros/job/crash/error', str(message) if message else '')\n if isinstance(sys.stderr, GeneralLogger):\n self.git.commit_json_file('FAIL_MESSAGE_LAST_LOG', 'aetros/job/crash/last_message', sys.stderr.last_messages)\n\n self.logger.debug('Crash report stored in commit ' + self.git.get_head_commit())\n self.stop(JOB_STATUS.PROGRESS_STATUS_FAILED, force_exit=force_exit)", "response": "Mark the job as failed saves the given error message and force exists the process when force_exit = True."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef write_log(self, message):\n if self.stream_log and not self.ended:\n # points to the Git stream write\n self.stream_log.write(message)\n return True", "response": "Write a message to the Git log."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets an arbitrary status visible in the big wheel of the job view.", "response": "def set_status(self, status, add_section=True):\n \"\"\"\n Set an arbitrary status, visible in the big wheel of the job view.\n \"\"\"\n status = str(status)\n\n if add_section:\n self.section(status)\n\n self.job_add_status('status', status)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create(self, create_info=None, hyperparameter=None, server='local', insights=False):\n if not create_info:\n create_info = {\n 'server': server,\n 'config': {\n 'insights': insights,\n 'command': ' '.join(sys.argv)\n }\n }\n config = find_config(self.config_path, logger=self.logger)\n\n if not config['model']:\n raise Exception('AETROS config file (aetros.yml) not found.')\n\n # first transform simple format in the full definition with parameter types\n # (string, number, group, choice_group, etc)\n full_hyperparameters = lose_parameters_to_full(config['parameters'])\n\n # now extract hyperparameters from full definition, and overwrite stuff using\n # incoming_hyperparameter if available\n hyperparameter = extract_parameters(full_hyperparameters, hyperparameter)\n create_info['config']['parameters'] = hyperparameter\n\n self.job = create_info\n\n if 'server' not in self.job and server:\n # setting this disables server assignment\n self.job['server'] = server\n\n self.job['optimization'] = None\n self.job['type'] = 'custom'\n\n if 'parameters' not in self.job['config']:\n self.job['config']['parameters'] = {}\n\n if 'insights' not in self.job['config']:\n self.job['config']['insights'] = insights\n\n self.job['created'] = time.time()\n self.git.create_job_id(self.job)\n\n self.logger.debug(\"Job created with Git ref \" + self.git.ref_head)\n\n return self.job_id", "response": "Creates a new job in git and pushes it to the local store."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads hyperparameter from job configuration.", "response": "def get_parameter(self, path, default=None, return_group=False):\n \"\"\"\n Reads hyperparameter from job configuration. If nothing found use given default.\n\n :param path: str \n :param default: *\n :param return_group: If true and path is a choice_group, we return the dict instead of the group name.\n :return: *\n \"\"\"\n value = read_parameter_by_path(self.job['config']['parameters'], path, return_group)\n\n if value is None:\n return default\n\n return value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load(self, job_id):\n self.git.read_job(job_id, checkout=self.is_master_process())\n self.load_job_from_ref()", "response": "Load a job into index and work - tree restarts its ref and sets as current."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_job_from_ref(self):\n if not self.job_id:\n raise Exception('Job not loaded yet. Use load(id) first.')\n\n if not os.path.exists(self.git.work_tree + '/aetros/job.json'):\n raise Exception('Could not load aetros/job.json from git repository. Make sure you have created the job correctly.')\n\n with open(self.git.work_tree + '/aetros/job.json') as f:\n self.job = simplejson.loads(f.read(), object_pairs_hook=collections.OrderedDict)\n\n if not self.job:\n raise Exception('Could not parse aetros/job.json from git repository. Make sure you have created the job correctly.')\n\n self.logger.debug('job: ' + str(self.job))", "response": "Loads the job. json into self. job"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a JobModel instance with current loaded job data attached.", "response": "def get_job_model(self):\n \"\"\"\n Returns a new JobModel instance with current loaded job data attached.\n :return: JobModel\n \"\"\"\n if not self.job:\n raise Exception('Job not loaded yet. Use load(id) first.')\n\n return JobModel(self.job_id, self.job, self.home_config['storage_dir'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nlisting all files in the working directory.", "response": "def file_list(self):\n \"\"\"\n Lists all files in the working directory.\n \"\"\"\n blacklist = ['.git', 'aetros']\n working_tree = self.git.work_tree\n\n def recursive(path='.'):\n if os.path.basename(path) in blacklist:\n return 0, 0\n\n if os.path.isdir(path):\n files = []\n for file in os.listdir(path):\n if path and path != '.':\n file = path + '/' + file\n\n added_files = recursive(file)\n files += added_files\n\n return files\n else:\n if path.endswith('.pyc'):\n return []\n\n if is_ignored(path, self.config['ignore']):\n return []\n\n return [os.path.relpath(path, working_tree)]\n\n return recursive(working_tree)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds all files in a working_tree to the list of files in the working_tree.", "response": "def add_files(self, working_tree, report=False):\n \"\"\"\n Commits all files from limited in aetros.yml. `files` is a whitelist, `exclude_files` is a blacklist.\n If both are empty, we commit all files smaller than 10MB.\n :return:\n \"\"\"\n blacklist = ['.git']\n\n def add_resursiv(path = '.', report=report):\n if os.path.basename(path) in blacklist:\n return 0, 0\n\n if working_tree + '/aetros' == path:\n # ignore in work_tree the folder ./aetros/, as it could be\n # that we checked out a job and start it again.\n return 0, 0\n\n if os.path.isdir(path):\n files = 0\n size = 0\n for file in os.listdir(path):\n if path and path != '.':\n file = path + '/' + file\n\n added_files, added_size = add_resursiv(file)\n files += added_files\n size += added_size\n\n return files, size\n else:\n if path.endswith('.pyc'):\n return 0, 0\n\n relative_path = os.path.relpath(path, working_tree)\n\n if is_ignored(relative_path, self.config['ignore']):\n return 0, 0\n\n self.logger.debug(\"added file to job \" + relative_path)\n if report:\n print(\"Added job file: \" + relative_path)\n\n self.git.add_file_path_in_work_tree(path, working_tree, verbose=False)\n\n return 1, os.path.getsize(path)\n\n return add_resursiv(working_tree, report=report)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_embedding_word2vec(self, x, path, dimensions=None, header_with_dimensions=True):\n if path.endswith('.txt'):\n if not os.path.exists(path):\n raise Exception(\"Given word2vec file does not exist: \" + path)\n\n f = open(path, 'r')\n\n if not header_with_dimensions and not dimensions:\n raise Exception('Either the word2vec file should contain the dimensions as header or it needs to be'\n 'specified manually using dimensions=[x,y] argument.')\n\n if header_with_dimensions:\n line = f.readline()\n if ' ' not in line:\n raise Exception('Given word2vec file should have in first line the dimensions, e.g.: 1000 200')\n dimensions = np.fromstring(line, dtype=np.uint, sep=' ').tolist()\n\n labels = ''\n vectors = ''\n line_pos = 1 if header_with_dimensions else 0\n\n if len(dimensions) != 2:\n raise Exception('dimensions invalid shape. e.g. [200, 32] => 200 rows, 32 cols.')\n\n for line in iter(f.readline, ''):\n line_pos += 1\n space_pos = line.find(' ')\n if -1 == space_pos:\n message = 'Given word2vec does not have correct format in line ' + str(line_pos)\n message += '\\nGot: ' + str(line)\n raise Exception(message)\n\n labels += line[:space_pos] + '\\n'\n vectors += line[space_pos+1:] + ' '\n\n vectors = np.fromstring(vectors, dtype=np.float32, sep=' ').tobytes()\n else:\n raise Exception(\"Given word2vec is not a .txt file. Other file formats are not supported.\")\n\n info = {\n 'dimensions': dimensions\n }\n\n name = os.path.basename(path)\n self._ensure_insight(x)\n remote_path = 'aetros/job/insight/'+str(x)+'/embedding/'\n with self.git.batch_commit('INSIGHT_EMBEDDING ' + str(x)):\n self.git.commit_file('WORD2VEC', remote_path + name + '/tensor.bytes', vectors)\n self.git.commit_file('WORD2VEC', remote_path + name + '/metadata.tsv', labels)\n self.git.commit_file('WORD2VEC', remote_path + name + '/info.json', simplejson.dumps(info))", "response": "Add an embedding word2vec file to the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a new embedding to the list of images and vectors.", "response": "def add_embedding_path(self, x, dimensions, vectors_path, metadata=None, image_shape=None, image=None):\n \"\"\"\n Adds a new embedding with optional metadata.\n \n Example how to generate vectors based on 2D numpy array:\n\n # 4 vectors, each size of 3\n vectors = [\n [2.3, 4.0, 33],\n [2.4, 4.2, 44],\n [2.5, 3.9, 34],\n [5.5, 200.2, 66]\n ]\n\n metadata = [\n # header, only necessary when more then on column\n # can be anything.\n ['label', 'count'],\n\n # for each vector from above an entry.\n ['red', '123'],\n ['white', '143'],\n ['yellow', '344'],\n ['house', '24'],\n ]\n\n numpy.array(vectors, dtype=numpy.float32).tofile('vectors.bytes')\n numpy.savetxt('metadata.tsv', numpy.array(metadata), delimiter='\\t', fmt='%s')\n\n job.add_embedding_path([4, 3], 'vectors.bytes', 'metadata.tsv')\n\n Metadata format example:\n\n Label\\tCount\\n\n red\\t4\\n\n yellow\\t6\\n\n\n :param x: The x axis of the insights. \n :param dimensions: 2D List of dimension, e.g [200, 20], means 200 vectors and each vector has size of 20.\n \n :param vectors_path: A path to a floats64 bytes file, no separators, sum(dimensions)*floats64 long.\n Example: If dimensions [200, 20] then the tensor file has 200*20 float32 bytes in it\n \n :param metadata: A TSV file. If only one column long (=no tab separator per line), then there's no need for a header.\n If you have more than one column, use the first line as header.\n \n :param image_shape: Size of the image of each vector.\n :param image: Path to an image sprite.\n :return: \n \"\"\"\n if not os.path.exists(vectors_path):\n raise Exception(\"Given embedding vectors file does not exist: \" + vectors_path)\n\n if metadata and not os.path.exists(metadata):\n raise Exception(\"Given embedding metadata file does not exist: \" + metadata)\n\n name = os.path.basename(vectors_path)\n self._ensure_insight(x)\n remote_path = 'aetros/job/insight/'+str(x)+'/embedding/'\n\n info = {\n 'dimensions': dimensions,\n 'image_shape': image_shape,\n 'image': os.path.basename(image) if image else None,\n }\n\n with self.git.lock_write():\n self.git.add_file_path(remote_path + name + '/tensor.bytes', vectors_path)\n self.git.add_file_path(remote_path + name + '/metadata.tsv', metadata)\n self.git.add_file(remote_path + name + '/info.json', simplejson.dumps(info))\n\n if image:\n self.git.add_file(remote_path + name + '/' + os.path.basename(image), image)\n\n self.git.commit_index('INSIGHT_EMBEDDING ' + str(x))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse sane directory output usually ls - l", "response": "def split_file_info(fileinfo):\n \"\"\" Parse sane directory output usually ls -l\n Adapted from https://gist.github.com/tobiasoberrauch/2942716\n \"\"\"\n current_year = datetime.datetime.now().strftime('%Y')\n files = []\n for line in fileinfo:\n parts = re.split(\n r'^([\\-dbclps])' + # Directory flag [1]\n r'([\\-rwxs]{9})\\s+' + # Permissions [2]\n r'(\\d+)\\s+' + # Number of items [3]\n r'([a-zA-Z0-9_-]+)\\s+' + # File owner [4]\n r'([a-zA-Z0-9_-]+)\\s+' + # File group [5]\n r'(\\d+)\\s+' + # File size in bytes [6]\n r'(\\w{3}\\s+\\d{1,2})\\s+' + # 3-char month and 1/2-char day of the month [7]\n r'(\\d{1,2}:\\d{1,2}|\\d{4})\\s+' + # Time or year (need to check conditions) [+= 7]\n r'(.+)$', # File/directory name [8]\n line\n )\n\n date = parts[7]\n time = parts[8] if ':' in parts[8] else '00:00'\n year = parts[8] if ':' not in parts[8] else current_year\n dt_obj = parser.parse(\"%s %s %s\" % (date, year, time))\n\n files.append({\n 'directory': parts[1],\n 'perms': parts[2],\n 'items': parts[3],\n 'owner': parts[4],\n 'group': parts[5],\n 'size': int(parts[6]),\n 'date': date,\n 'time': time,\n 'year': year,\n 'name': parts[9],\n 'datetime': dt_obj\n })\n return files"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get(self, remote, local=None):\n if isinstance(local, file_type): # open file, leave open\n local_file = local\n elif local is None: # return string\n local_file = buffer_type()\n else: # path to file, open, write/close return None\n local_file = open(local, 'wb')\n\n self.conn.retrbinary(\"RETR %s\" % remote, local_file.write)\n\n if isinstance(local, file_type):\n pass\n elif local is None:\n contents = local_file.getvalue()\n local_file.close()\n return contents\n else:\n local_file.close()\n\n return None", "response": "Gets the file from FTP server"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nput a local file on the FTP server and return the size of the file.", "response": "def put(self, local, remote, contents=None, quiet=False):\n \"\"\" Puts a local file (or contents) on to the FTP server\n\n local can be:\n a string: path to inpit file\n a file: opened for reading\n None: contents are pushed\n \"\"\"\n remote_dir = os.path.dirname(remote)\n remote_file = os.path.basename(local)\\\n if remote.endswith('/') else os.path.basename(remote)\n\n if contents:\n # local is ignored if contents is set\n local_file = buffer_type(contents)\n elif isinstance(local, file_type):\n local_file = local\n else:\n local_file = open(local, 'rb')\n current = self.conn.pwd()\n self.descend(remote_dir, force=True)\n\n size = 0\n try:\n self.conn.storbinary('STOR %s' % remote_file, local_file)\n size = self.conn.size(remote_file)\n except:\n if not quiet:\n raise\n finally:\n local_file.close()\n self.conn.cwd(current)\n return size"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list(self, remote='.', extra=False, remove_relative_paths=False):\n if extra:\n self.tmp_output = []\n self.conn.dir(remote, self._collector)\n directory_list = split_file_info(self.tmp_output)\n else:\n directory_list = self.conn.nlst(remote)\n\n if remove_relative_paths:\n return list(filter(self.is_not_relative_path, directory_list))\n\n return directory_list", "response": "Return a list of all the files in the local directory."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndescend the current directory with the remote directory.", "response": "def descend(self, remote, force=False):\n \"\"\" Descend, possibly creating directories as needed \"\"\"\n remote_dirs = remote.split('/')\n for directory in remote_dirs:\n try:\n self.conn.cwd(directory)\n except Exception:\n if force:\n self.conn.mkd(directory)\n self.conn.cwd(directory)\n return self.conn.pwd()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete(self, remote):\n try:\n self.conn.delete(remote)\n except Exception:\n return False\n else:\n return True", "response": "Delete a file from server"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchanges working directory on server", "response": "def cd(self, remote):\n \"\"\" Change working directory on server \"\"\"\n try:\n self.conn.cwd(remote)\n except Exception:\n return False\n else:\n return self.pwd()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstart a job with all logging of a job_id", "response": "def start(logger, full_id, fetch=True, env=None, volumes=None, cpus=None, memory=None, gpu_devices=None, offline=False):\n \"\"\"\n Starts the job with all logging of a job_id\n \"\"\"\n\n owner, name, id = unpack_full_job_id(full_id)\n\n if isinstance(sys.stdout, GeneralLogger):\n # we don't want to have stuff written to stdout before in job's log\n sys.stdout.clear_buffer()\n\n job_backend = JobBackend(model_name=owner + '/' + name)\n\n if fetch:\n job_backend.fetch(id)\n\n job_backend.restart(id)\n job_backend.start(collect_system=False, offline=offline)\n job_backend.set_status('PREPARE', add_section=False)\n\n job = job_backend.get_job_model()\n\n if not cpus:\n cpus = job.get_cpu()\n\n if not memory:\n memory = job.get_memory()\n\n if not gpu_devices and job.get_gpu():\n # if requested 2 GPUs and we have 3 GPUs with id [0,1,2], gpus should be [0,1]\n gpu_devices = []\n for i in range(0, job.get_gpu()):\n gpu_devices.append(i)\n\n start_command(logger, job_backend, env, volumes, cpus=cpus, memory=memory, gpu_devices=gpu_devices, offline=offline)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef fromimage(im, flatten=False, mode=None):\n if not Image.isImageType(im):\n raise TypeError(\"Input is not a PIL image.\")\n\n if mode is not None:\n if mode != im.mode:\n im = im.convert(mode)\n elif im.mode == 'P':\n # Mode 'P' means there is an indexed \"palette\". If we leave the mode\n # as 'P', then when we do `a = array(im)` below, `a` will be a 2-D\n # containing the indices into the palette, and not a 3-D array\n # containing the RGB or RGBA values.\n if 'transparency' in im.info:\n im = im.convert('RGBA')\n else:\n im = im.convert('RGB')\n\n if flatten:\n im = im.convert('F')\n elif im.mode == '1':\n # Workaround for crash in PIL. When im is 1-bit, the call array(im)\n # can cause a seg. fault, or generate garbage. See\n # https://github.com/scipy/scipy/issues/2138 and\n # https://github.com/python-pillow/Pillow/issues/350.\n #\n # This converts im from a 1-bit image to an 8-bit image.\n im = im.convert('L')\n\n a = array(im)\n return a", "response": "Returns a copy of a PIL image as a numpy array."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,\n mode=None, channel_axis=None):\n \"\"\"Takes a numpy array and returns a PIL image.\n\n The mode of the PIL image depends on the array shape and the `pal` and\n `mode` keywords.\n\n For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values\n (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode\n is given as 'F' or 'I' in which case a float and/or integer array is made.\n\n Notes\n -----\n For 3-D arrays, the `channel_axis` argument tells which dimension of the\n array holds the channel data.\n\n For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'\n by default or 'YCbCr' if selected.\n\n The numpy array must be either 2 dimensional or 3 dimensional.\n\n \"\"\"\n data = asarray(arr)\n if iscomplexobj(data):\n raise ValueError(\"Cannot convert a complex-valued array.\")\n shape = list(data.shape)\n valid = len(shape) == 2 or ((len(shape) == 3) and\n ((3 in shape) or (4 in shape)))\n if not valid:\n raise ValueError(\"'arr' does not have a suitable array shape for \"\n \"any mode.\")\n if len(shape) == 2:\n shape = (shape[1], shape[0]) # columns show up first\n if mode == 'F':\n data32 = data.astype(numpy.float32)\n image = Image.frombytes(mode, shape, data32.tostring())\n return image\n if mode in [None, 'L', 'P']:\n bytedata = bytescale(data, high=high, low=low,\n cmin=cmin, cmax=cmax)\n image = Image.frombytes('L', shape, bytedata.tostring())\n if pal is not None:\n image.putpalette(asarray(pal, dtype=uint8).tostring())\n # Becomes a mode='P' automagically.\n elif mode == 'P': # default gray-scale\n pal = (arange(0, 256, 1, dtype=uint8)[:, newaxis] *\n ones((3,), dtype=uint8)[newaxis, :])\n image.putpalette(asarray(pal, dtype=uint8).tostring())\n return image\n if mode == '1': # high input gives threshold for 1\n bytedata = (data > high)\n image = Image.frombytes('1', shape, bytedata.tostring())\n return image\n if cmin is None:\n cmin = amin(ravel(data))\n if cmax is None:\n cmax = amax(ravel(data))\n data = (data*1.0 - cmin)*(high - low)/(cmax - cmin) + low\n if mode == 'I':\n data32 = data.astype(numpy.uint32)\n image = Image.frombytes(mode, shape, data32.tostring())\n else:\n raise ValueError(_errstr)\n return image\n\n # if here then 3-d array with a 3 or a 4 in the shape length.\n # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'\n if channel_axis is None:\n if (3 in shape):\n ca = numpy.flatnonzero(asarray(shape) == 3)[0]\n else:\n ca = numpy.flatnonzero(asarray(shape) == 4)\n if len(ca):\n ca = ca[0]\n else:\n raise ValueError(\"Could not find channel dimension.\")\n else:\n ca = channel_axis\n\n numch = shape[ca]\n if numch not in [3, 4]:\n raise ValueError(\"Channel axis dimension is not valid.\")\n\n bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)\n if ca == 2:\n strdata = bytedata.tostring()\n shape = (shape[1], shape[0])\n elif ca == 1:\n strdata = transpose(bytedata, (0, 2, 1)).tostring()\n shape = (shape[2], shape[0])\n elif ca == 0:\n strdata = transpose(bytedata, (1, 2, 0)).tostring()\n shape = (shape[2], shape[1])\n if mode is None:\n if numch == 3:\n mode = 'RGB'\n else:\n mode = 'RGBA'\n\n if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:\n raise ValueError(_errstr)\n\n if mode in ['RGB', 'YCbCr']:\n if numch != 3:\n raise ValueError(\"Invalid array shape for mode.\")\n if mode in ['RGBA', 'CMYK']:\n if numch != 4:\n raise ValueError(\"Invalid array shape for mode.\")\n\n # Here we know data and mode is correct\n image = Image.frombytes(mode, shape, strdata)\n return image", "response": "Takes a numpy array and returns a PIL image."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef imresize(arr, size, interp='bilinear', mode=None):\n im = toimage(arr, mode=mode)\n ts = type(size)\n if issubdtype(ts, int):\n percent = size / 100.0\n size = tuple((array(im.size)*percent).astype(int))\n elif issubdtype(type(size), float):\n size = tuple((array(im.size)*size).astype(int))\n else:\n size = (size[1], size[0])\n func = {'nearest': 0, 'lanczos': 1, 'bilinear': 2, 'bicubic': 3, 'cubic': 3}\n imnew = im.resize(size, resample=func[interp])\n return fromimage(imnew)", "response": "Resize an image.\n\n Parameters\n ----------\n arr : ndarray\n The array of image to be resized.\n\n size : int, float or tuple\n * int - Percentage of current size.\n * float - Fraction of current size.\n * tuple - Size of the output image.\n\n interp : str, optional\n Interpolation to use for re-sizing ('nearest', 'lanczos', 'bilinear', 'bicubic'\n or 'cubic').\n\n mode : str, optional\n The PIL image mode ('P', 'L', etc.) to convert `arr` before resizing.\n\n Returns\n -------\n imresize : ndarray\n The resized array of image.\n\n See Also\n --------\n toimage : Implicitly used to convert `arr` according to `mode`.\n scipy.ndimage.zoom : More generic implementation that does not use PIL."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconnects to a specific channel.", "response": "def connect(self, channel):\n \"\"\"\n In the write-thread we detect that no connection is living anymore and try always again.\n Up to the 3 connection try, we report to user. We keep trying but in silence.\n Also, when more than 10 connection tries are detected, we delay extra 15 seconds.\n \"\"\"\n if self.connection_tries > 10:\n time.sleep(10)\n\n if self.in_connecting[channel]:\n return False\n\n self.in_connecting[channel] = True\n\n self.logger.debug('[%s] Wanna connect ...' % (channel, ))\n\n try:\n if self.is_connected(channel) or self.online is False:\n if self.is_connected(channel):\n self.logger.debug('[%s] Already connected' % (channel, ))\n if self.online is False:\n self.logger.debug('[%s] self.online=False' % (channel, ))\n\n return True\n\n self.channel_lock[channel].acquire()\n\n self.connected[channel] = None\n self.registered[channel] = None\n self.ssh_stream[channel] = False\n self.ssh_channel[channel] = False\n messages = None\n stderrdata = ''\n\n try:\n if not self.ssh_stream[channel]:\n self.logger.debug('[%s] Open ssh connection' % (channel, ))\n self.ssh_stream[channel] = create_ssh_stream(self.config, exit_on_failure=False)\n\n self.logger.debug('[%s] open channel' % (channel, ))\n\n self.ssh_channel[channel] = self.ssh_stream[channel].get_transport().open_session()\n self.ssh_channel[channel].exec_command('stream')\n except (KeyboardInterrupt, SystemExit):\n raise\n except Exception as e:\n self.connected[channel] = False\n self.registered[channel] = False\n self.logger.debug('[%s] connection failed: %s' % (channel, str(e)))\n return False\n finally:\n self.channel_lock[channel].release()\n\n if self.ssh_channel[channel]:\n messages = self.wait_for_at_least_one_message(channel)\n\n if not messages:\n stderrdata = self.ssh_channel[channel].recv_stderr().decode(\"utf-8\").strip()\n self.connected[channel] = False\n self.registered[channel] = False\n else:\n self.logger.debug('[%s] opened and received %d messages' % (channel, len(messages)))\n self.connected[channel] = True\n self.registered[channel] = self.on_connect(self.was_connected_once[channel], channel)\n self.connected_since[channel] = time.time()\n\n if channel == '' and self.registered[channel] and self.was_connected_once[channel]:\n self.logger.info(\"Successfully reconnected.\")\n\n if not self.registered[channel]:\n # make sure to close channel and connection first\n try:\n self.ssh_channel[channel] and self.ssh_channel[channel].close()\n except: pass\n\n try:\n self.ssh_stream[channel] and self.ssh_stream[channel].close()\n except: pass\n\n self.logger.debug(\"[%s] Client: registration failed. stderrdata: %s\" % (channel, stderrdata))\n self.connected[channel] = False\n\n try:\n self.logger.debug('[%s] Client: ssh_tream close due to registration failure' % (channel, ))\n self.ssh_stream[channel].close()\n except (KeyboardInterrupt, SystemExit):\n raise\n\n self.connection_tries += 1\n if not self.was_connected_once[channel] and self.go_offline_on_first_failed_attempt:\n # initial try needs to be online, otherwise we go offline\n self.go_offline()\n\n if stderrdata:\n if 'Connection refused' not in stderrdata and 'Permission denied' not in stderrdata:\n self.logger.error(stderrdata)\n\n if 'Permission denied' in stderrdata:\n if self.connection_tries < 3:\n self.logger.warning(\"Access denied. Did you setup your SSH public key correctly \"\n \"and saved it in your AETROS Trainer user account?\")\n\n self.close()\n sys.exit(1)\n\n self.connection_error(channel, \"Connection error during connecting to %s: %s\" % (self.host, str(stderrdata)))\n else:\n self.was_connected_once[channel] = True\n\n except Exception as error:\n self.connection_error(channel, error)\n finally:\n self.in_connecting[channel] = False\n\n return self.is_connected(channel)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _end_channel(self, channel):\n self.stop_on_empty_queue[channel] = True\n\n # by joining the we wait until its loop finishes.\n # it won't loop forever since we've set self.stop_on_empty_queue=True\n write_thread = self.thread_write_instances[channel]\n\n thread_join_non_blocking(write_thread)", "response": "End the channel and wait for the write thread to finish."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef wait_sending_last_messages(self):\n if self.active and self.online is not False:\n self.logger.debug(\"client sends last %s messages ...\"\n % ([str(i) + ':' + str(len(x)) for i, x in six.iteritems(self.queues)],))\n\n for channel, messages in six.iteritems(self.queues):\n for idx, message in enumerate(messages):\n self.logger.debug(\"[%s] %d: %s\" % (channel, idx, str(message)[0:120]))\n\n # send all missing messages\n\n # by joining we wait until its loop finish.\n # it won't loop forever since we've set self.stop_on_empty_queue=True\n for channel in six.iterkeys(self.ssh_channel):\n if channel != '':\n self._end_channel(channel)\n\n # last is control channel\n self._end_channel('')", "response": "Send all messages to the last available channel."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwaiting until all queues of channels are empty.", "response": "def wait_until_queue_empty(self, channels, report=True, clear_end=True):\n \"\"\"\n Waits until all queues of channels are empty.\n \"\"\"\n state = {'message': ''}\n\n self.logger.debug(\"wait_until_queue_empty: report=%s %s\"\n % (str(report), str([channel+':'+str(len(self.queues[channel])) for channel in channels]), ))\n queues = []\n for channel in channels:\n queues += self.queues[channel][:]\n\n def print_progress():\n if report:\n self.logger.debug(\"all_empty=%s\" % (str(all_empty),))\n\n sys.__stderr__.write('\\b' * len(state['message']))\n sys.__stderr__.write(\"\\033[K\")\n\n state['message'] = \"%.2f kB/s // %.2fkB of %.2fkB // %.2f%%\" \\\n % (self.bytes_speed / 1024, self.bytes_sent / 1024, self.bytes_total / 1024,\n (self.bytes_sent / self.bytes_total * 100) if self.bytes_total else 0)\n\n sys.__stderr__.write(state['message'])\n sys.__stderr__.flush()\n\n while True:\n all_empty = all(m['_sent'] for m in queues)\n\n print_progress()\n\n if all_empty:\n break\n\n time.sleep(0.2)\n\n print_progress()\n\n if report and clear_end:\n sys.__stderr__.write('\\b' * len(state['message']))\n sys.__stderr__.write(\"\\033[K\")\n sys.__stderr__.flush()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nread until we receive at least one message. Return all found messages.", "response": "def wait_for_at_least_one_message(self, channel):\n \"\"\"\n Reads until we receive at least one message we can unpack. Return all found messages.\n \"\"\"\n\n unpacker = msgpack.Unpacker(encoding='utf-8')\n\n while True:\n try:\n start = time.time()\n chunk = self.ssh_channel[channel].recv(1024)\n end = time.time()\n self.read_speeds.append( len(chunk) / (end-start) )\n if len(self.read_speeds) > 20:\n self.read_speeds = self.read_speeds[10:]\n\n if chunk == b'':\n # happens only when connection broke. If nothing is to be received, it hangs instead.\n self.connection_error(channel, 'Connection broken w')\n return False\n except Exception as error:\n self.connection_error(channel, error)\n raise\n\n unpacker.feed(chunk)\n\n messages = [m for m in unpacker]\n if messages:\n return messages"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read(self, channel):\n\n if not self.ssh_channel[channel].recv_ready():\n return\n\n try:\n start = time.time()\n chunk = self.ssh_channel[channel].recv(1024)\n end = time.time()\n\n self.read_speeds.append(len(chunk) / (end-start))\n if len(self.read_speeds) > 20:\n self.read_speeds = self.read_speeds[10:]\n\n except Exception as error:\n self.connection_error(channel, error)\n raise\n\n if chunk == b'':\n # socket connection broken\n self.connection_error(channel, 'Connection broken')\n return None\n\n # self.read_buffer.seek(0, 2) #make sure we write at the end\n self.read_unpacker.feed(chunk)\n\n # self.read_buffer.seek(0)\n messages = [m for m in self.read_unpacker]\n\n return messages if messages else None", "response": "Reads from the socket and returns the message."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nraises the SIGINT signal in the current process and all sub - processes.", "response": "def raise_sigint():\n \"\"\"\n Raising the SIGINT signal in the current process and all sub-processes.\n\n os.kill() only issues a signal in the current process (without subprocesses).\n CTRL+C on the console sends the signal to the process group (which we need).\n \"\"\"\n if hasattr(signal, 'CTRL_C_EVENT'):\n # windows. Need CTRL_C_EVENT to raise the signal in the whole process group\n os.kill(os.getpid(), signal.CTRL_C_EVENT)\n else:\n # unix.\n pgid = os.getpgid(os.getpid())\n if pgid == 1:\n os.kill(os.getpid(), signal.SIGINT)\n else:\n os.killpg(os.getpgid(os.getpid()), signal.SIGINT)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef human_size(size_bytes, precision=0):\n if size_bytes == 1:\n # because I really hate unnecessary plurals\n return \"1 byte\"\n\n suffixes_table = [('bytes',0),('KB',0),('MB',1),('GB',2),('TB',2), ('PB',2)]\n\n num = float(size_bytes)\n for suffix, precision in suffixes_table:\n if num < 1024.0:\n break\n num /= 1024.0\n\n if precision == 0:\n formatted_size = \"%d\" % num\n else:\n formatted_size = str(round(num, ndigits=precision))\n\n return \"%s %s\" % (formatted_size, suffix)", "response": "Format a size in bytes into a human readable file size e. g. 1 byte KB GB TB PB"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconvert an array to an image.", "response": "def array_to_img(x, scale=True):\n \"\"\"\n x should be shape (channels, width, height)\n \"\"\"\n from PIL import Image\n if x.ndim != 3:\n raise Exception('Unsupported shape : ', str(x.shape), '. Need (channels, width, height)')\n if scale:\n x += max(-np.min(x), 0)\n x /= np.max(x)\n x *= 255\n if x.shape[0] == 3:\n # RGB\n if x.dtype != 'uint8':\n x = x.astype('uint8')\n return Image.fromarray(x.astype('uint8'), 'RGB')\n elif x.shape[0] == 1:\n # grayscale\n if x.dtype != 'uint8':\n x = x.astype('uint8')\n return Image.fromarray(x.reshape(x.shape[1], x.shape[2]), 'L')\n else:\n raise Exception('Unsupported channel number: ', x.shape[0])"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_generator_validation_nb(self, number):\n\n self.nb_val_samples = number\n diff_to_batch = number % self.get_batch_size()\n if diff_to_batch > 0:\n self.nb_val_samples += self.get_batch_size() - diff_to_batch\n\n import keras\n if '1' != keras.__version__[0]:\n self.nb_val_samples = self.nb_val_samples // self.get_batch_size()", "response": "Sets self. nb_val_samples which is used in model. fit if input is a generator\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets self. samples_per_epoch which is used in model. fit if input is a generator ArcGIS object.", "response": "def set_generator_training_nb(self, number):\n \"\"\"\n sets self.samples_per_epoch which is used in model.fit if input is a generator\n :param number:\n :return:\n \"\"\"\n\n self.samples_per_epoch = number\n diff_to_batch = number % self.get_batch_size()\n if diff_to_batch > 0:\n self.samples_per_epoch += self.get_batch_size() - diff_to_batch"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef attach(self, buffer, read_line=None):\n\n bid = id(buffer)\n self.attach_last_messages[bid] = b''\n\n def reader():\n current_line = b''\n\n def handle_line(buf):\n if chunk == b'':\n return\n\n if read_line and callable(read_line):\n res = read_line(buf)\n if res is False:\n return False\n\n elif res is not None:\n buf = res\n if hasattr(buf, 'encode'):\n buf = buf.encode('utf-8')\n\n self.attach_last_messages[bid] += buf\n\n if len(self.attach_last_messages[bid]) > 21 * 1024:\n self.attach_last_messages[bid] = self.attach_last_messages[bid][-20 * 1024:]\n\n self.write(buf)\n\n flush_char = b'\\n'\n\n while True:\n try:\n # needs to be 1 so we fetch data in near real-time\n chunk = buffer.read(1)\n if chunk == b'':\n if current_line:\n handle_line(current_line)\n return\n\n current_line += chunk\n\n while flush_char in current_line:\n pos = current_line.find(flush_char)\n line = current_line[:pos+1]\n current_line = current_line[pos+1:]\n handle_line(line)\n\n # todo, periodically flush by '\\r' only (progress bars for example)\n # and make sure only necessary data is sent (by applying \\r and \\b control characters)\n\n except (KeyboardInterrupt, SystemExit):\n raise\n except Exception:\n # we need to make sure, we continue to read otherwise the process of this buffer\n # will block and we have a stuck process.\n sys.__stderr__.write(traceback.format_exc() + '\\n')\n sys.__stderr__.flush()\n\n thread = Thread(target=reader)\n thread.daemon = True\n thread.start()\n\n def wait():\n thread_join_non_blocking(thread)\n self.send_buffer()\n\n return wait", "response": "Reads a buffer and sends it to the logger and self. job_backend."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn an upscaled image array for the given ratio.", "response": "def upscale(image, ratio):\n \"\"\"\n return upscaled image array\n Arguments:\n image -- a (H,W,C) numpy.ndarray\n ratio -- scaling factor (>1)\n \"\"\"\n if not isinstance(image, np.ndarray):\n raise ValueError('Expected ndarray')\n if ratio < 1:\n raise ValueError('Ratio must be greater than 1 (ratio=%f)' % ratio)\n width = int(math.floor(image.shape[1] * ratio))\n height = int(math.floor(image.shape[0] * ratio))\n channels = image.shape[2]\n out = np.ndarray((height, width, channels), dtype=np.uint8)\n for x, y in np.ndindex((width, height)):\n out[y, x] = image[int(math.floor(y / ratio)), int(math.floor(x / ratio))]\n return out"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef embed_image_html(image):\n if image is None:\n return None\n elif isinstance(image, PIL.Image.Image):\n pass\n elif isinstance(image, np.ndarray):\n image = PIL.Image.fromarray(image)\n else:\n raise ValueError('image must be a PIL.Image or a np.ndarray')\n\n # Read format from the image\n fmt = image.format\n if not fmt:\n # default to JPEG\n fmt = 'jpeg'\n else:\n fmt = fmt.lower()\n\n string_buf = StringIO()\n image.save(string_buf, format=fmt)\n data = string_buf.getvalue().encode('base64').replace('\\n', '')\n return 'data:image/%s;base64,%s' % (fmt, data)", "response": "Returns an image embedded in HTML base64 format"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd bounding boxes to an image", "response": "def add_bboxes_to_image(image, bboxes, color='red', width=1):\n \"\"\"\n Draw rectangles on the image for the bounding boxes\n Returns a PIL.Image\n Arguments:\n image -- input image\n bboxes -- bounding boxes in the [((l, t), (r, b)), ...] format\n Keyword arguments:\n color -- color to draw the rectangles\n width -- line width of the rectangles\n Example:\n image = Image.open(filename)\n add_bboxes_to_image(image, bboxes[filename], width=2, color='#FF7700')\n image.show()\n \"\"\"\n def expanded_bbox(bbox, n):\n \"\"\"\n Grow the bounding box by n pixels\n \"\"\"\n l = min(bbox[0][0], bbox[1][0])\n r = max(bbox[0][0], bbox[1][0])\n t = min(bbox[0][1], bbox[1][1])\n b = max(bbox[0][1], bbox[1][1])\n return ((l - n, t - n), (r + n, b + n))\n\n from PIL import Image, ImageDraw\n draw = ImageDraw.Draw(image)\n for bbox in bboxes:\n for n in range(width):\n draw.rectangle(expanded_bbox(bbox, n), outline=color)\n\n return image"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a vis_square for the given layer data", "response": "def get_layer_vis_square(data,\n allow_heatmap=True,\n normalize=True,\n min_img_dim=100,\n max_width=1200,\n channel_order='RGB',\n colormap='jet',\n ):\n \"\"\"\n Returns a vis_square for the given layer data\n Arguments:\n data -- a np.ndarray\n Keyword arguments:\n allow_heatmap -- if True, convert single channel images to heatmaps\n normalize -- whether to normalize the data when visualizing\n max_width -- maximum width for the vis_square\n \"\"\"\n if channel_order not in ['RGB', 'BGR']:\n raise ValueError('Unsupported channel_order %s' % channel_order)\n if data.ndim == 1:\n # interpret as 1x1 grayscale images\n # (N, 1, 1)\n data = data[:, np.newaxis, np.newaxis]\n elif data.ndim == 2:\n # interpret as 1x1 grayscale images\n # (N, 1, 1)\n data = data.reshape((data.shape[0] * data.shape[1], 1, 1))\n elif data.ndim == 3:\n if data.shape[0] == 3:\n # interpret as a color image\n # (1, H, W,3)\n if channel_order == 'BGR':\n data = data[[2, 1, 0], ...] # BGR to RGB (see issue #59)\n data = data.transpose(1, 2, 0)\n data = data[np.newaxis, ...]\n else:\n # interpret as grayscale images\n # (N, H, W)\n pass\n elif data.ndim == 4:\n if data.shape[0] == 3:\n # interpret as HxW color images\n # (N, H, W, 3)\n data = data.transpose(1, 2, 3, 0)\n if channel_order == 'BGR':\n data = data[:, :, :, [2, 1, 0]] # BGR to RGB (see issue #59)\n elif data.shape[1] == 3:\n # interpret as HxW color images\n # (N, H, W, 3)\n data = data.transpose(0, 2, 3, 1)\n if channel_order == 'BGR':\n data = data[:, :, :, [2, 1, 0]] # BGR to RGB (see issue #59)\n else:\n # interpret as HxW grayscale images\n # (N, H, W)\n data = data.reshape((data.shape[0] * data.shape[1], data.shape[2], data.shape[3]))\n else:\n raise RuntimeError('unrecognized data shape: %s' % (data.shape,))\n\n return get_layer_vis_square_raw(data,\n allow_heatmap,\n normalize,\n min_img_dim,\n max_width,\n colormap,\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_color_map(name):\n redmap = [0]\n greenmap = [0]\n bluemap = [0]\n if name == 'white':\n # essentially a noop\n redmap = [0, 1]\n greenmap = [0, 1]\n bluemap = [0, 1]\n elif name == 'simple':\n redmap = [0, 1, 1, 1]\n greenmap = [0, 0, 1, 1]\n bluemap = [0, 0, 0, 1]\n elif name == 'hot':\n redmap = [0, 0.03968253968253968, 0.07936507936507936, 0.119047619047619, 0.1587301587301587, 0.1984126984126984, 0.2380952380952381, 0.2777777777777778, 0.3174603174603174, 0.3571428571428571, 0.3968253968253968, 0.4365079365079365, 0.4761904761904762, 0.5158730158730158, 0.5555555555555556, 0.5952380952380952,\n 0.6349206349206349, 0.6746031746031745, 0.7142857142857142, 0.753968253968254, 0.7936507936507936, 0.8333333333333333, 0.873015873015873, 0.9126984126984127, 0.9523809523809523, 0.992063492063492, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n greenmap = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03174603174603163, 0.0714285714285714, 0.1111111111111112, 0.1507936507936507, 0.1904761904761905, 0.23015873015873, 0.2698412698412698, 0.3095238095238093, 0.3492063492063491, 0.3888888888888888, 0.4285714285714284,\n 0.4682539682539679, 0.5079365079365079, 0.5476190476190477, 0.5873015873015872, 0.6269841269841268, 0.6666666666666665, 0.7063492063492065, 0.746031746031746, 0.7857142857142856, 0.8253968253968254, 0.8650793650793651, 0.9047619047619047, 0.9444444444444442, 0.984126984126984, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n bluemap = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.04761904761904745, 0.1269841269841265,\n 0.2063492063492056, 0.2857142857142856, 0.3650793650793656, 0.4444444444444446, 0.5238095238095237, 0.6031746031746028, 0.6825396825396828, 0.7619047619047619, 0.8412698412698409, 0.92063492063492, 1]\n elif name == 'rainbow':\n redmap = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9365079365079367, 0.8571428571428572, 0.7777777777777777, 0.6984126984126986, 0.6190476190476191, 0.53968253968254, 0.4603174603174605, 0.3809523809523814, 0.3015873015873018, 0.2222222222222223, 0.1428571428571432,\n 0.06349206349206415, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03174603174603208, 0.08465608465608465, 0.1375661375661377, 0.1904761904761907, 0.2433862433862437, 0.2962962962962963, 0.3492063492063493, 0.4021164021164023, 0.4550264550264553, 0.5079365079365079, 0.5608465608465609, 0.6137566137566139, 0.666666666666667]\n greenmap = [0, 0.03968253968253968, 0.07936507936507936, 0.119047619047619, 0.1587301587301587, 0.1984126984126984, 0.2380952380952381, 0.2777777777777778, 0.3174603174603174, 0.3571428571428571, 0.3968253968253968, 0.4365079365079365, 0.4761904761904762, 0.5158730158730158, 0.5555555555555556, 0.5952380952380952, 0.6349206349206349, 0.6746031746031745, 0.7142857142857142, 0.753968253968254, 0.7936507936507936,\n 0.8333333333333333, 0.873015873015873, 0.9126984126984127, 0.9523809523809523, 0.992063492063492, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9841269841269842, 0.9047619047619047, 0.8253968253968256, 0.7460317460317465, 0.666666666666667, 0.587301587301587, 0.5079365079365079, 0.4285714285714288, 0.3492063492063493, 0.2698412698412698, 0.1904761904761907, 0.1111111111111116, 0.03174603174603208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n bluemap = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01587301587301582, 0.09523809523809534, 0.1746031746031744, 0.2539682539682535,\n 0.333333333333333, 0.412698412698413, 0.4920634920634921, 0.5714285714285712, 0.6507936507936507, 0.7301587301587302, 0.8095238095238093, 0.8888888888888884, 0.9682539682539679, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n elif name == 'winter':\n greenmap = [0, 1]\n bluemap = [1, 0.5]\n else:\n if name != 'jet':\n print('Warning: colormap \"%s\" not supported. Using jet instead.' % name)\n redmap = [0, 0, 0, 0, 0.5, 1, 1, 1, 0.5]\n greenmap = [0, 0, 0.5, 1, 1, 1, 0.5, 0, 0]\n bluemap = [0.5, 1, 1, 1, 0.5, 0, 0, 0, 0]\n return 255.0 * np.array(redmap), 255.0 * np.array(greenmap), 255.0 * np.array(bluemap)", "response": "Returns a colormap as a tuple."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef prepare_index_file(self):\n if os.getenv('AETROS_GIT_INDEX_FILE'):\n self.index_path = os.getenv('AETROS_GIT_INDEX_FILE')\n return\n\n import tempfile\n h, path = tempfile.mkstemp('aetros-git', '', self.temp_path)\n\n self.index_path = path\n\n # we give git a unique file path for that index. However, git expect it to be non-existent for empty indexes.\n # empty file would lead to \"fatal: index file smaller than expected\"\n os.close(h)\n os.unlink(self.index_path)\n\n self.logger.debug('GIT_INDEX_FILE created at ' + self.index_path)", "response": "Prepare the index file for the current job."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef fetch_job(self, job_id, checkout=False):\n self.job_id = job_id\n\n self.logger.debug(\"Git fetch job reference %s\" % (self.ref_head, ))\n out, code, err = self.command_exec(['ls-remote', 'origin', self.ref_head])\n\n if code:\n self.logger.error('Could not find the job ' + job_id + ' on the server. Are you online and does the job exist?')\n sys.exit(1)\n\n try:\n self.command_exec(['fetch', '-f', '-n', 'origin', self.ref_head+':'+self.ref_head])\n except Exception:\n self.logger.error(\"Could not load job information for \" + job_id + '. You need to be online to start pre-configured jobs.')\n raise\n\n self.read_job(job_id, checkout)", "response": "Fetch the current job from origin and read its tree into working director."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads the head and reads the tree into index and checkout the work - tree when checkout = True.", "response": "def read_job(self, job_id, checkout=False):\n \"\"\"\n Reads head and reads the tree into index,\n and checkout the work-tree when checkout=True.\n\n This does not fetch the job from the actual server. It needs to be in the local git already.\n \"\"\"\n self.job_id = job_id\n\n commit = self.get_head_commit()\n self.logger.debug('Job ref points to ' + commit)\n self.command_exec(['read-tree', self.ref_head])\n\n if checkout:\n self.logger.debug('Working directory in ' + self.work_tree)\n\n # make sure we have checked out all files we have added until now. Important for simple models,\n # so we have the actual model.py and dataset scripts.\n if os.path.exists(self.work_tree):\n shutil.rmtree(self.work_tree)\n\n os.makedirs(self.work_tree)\n\n # make the working tree reflect exactly the tree of ref_head.\n # since we removed the dir before, we have exactly the tree of the reference\n # '--', '.' is important to not update HEAD\n self.command_exec(['--work-tree', self.work_tree, 'checkout', self.ref_head, '--', '.'])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_job_id(self, data):\n self.add_file('aetros/job.json', simplejson.dumps(data, indent=4))\n tree_id = self.write_tree()\n\n self.job_id = self.command_exec(['commit-tree', '-m', \"JOB_CREATED\", tree_id])[0].decode('utf-8').strip()\n\n out, code, err = self.command_exec(['show-ref', self.ref_head], allowed_to_fail=True)\n if not code:\n self.logger.warning(\"Generated job id already exists, because exact same experiment values given. Ref \" + self.ref_head)\n\n self.command_exec(['update-ref', self.ref_head, self.job_id])\n\n # make sure we have checkedout all files we have added until now. Important for simple models, so we have the\n # actual model.py and dataset scripts.\n if not os.path.exists(self.work_tree):\n os.makedirs(self.work_tree)\n\n # updates index and working tree\n # '--', '.' is important to not update HEAD\n self.command_exec(['--work-tree', self.work_tree, 'checkout', self.ref_head, '--', '.'])\n\n # every caller needs to make sure to call git.push\n return self.job_id", "response": "Create a new job id and a reference."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start_push_sync(self):\n self.active_thread = True\n self.active_push = True\n\n self.thread_push_instance = Thread(target=self.thread_push)\n self.thread_push_instance.daemon = True\n self.thread_push_instance.start()", "response": "Starts the detection of unsynced Git data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop(self):\n self.active_thread = False\n\n if self.thread_push_instance and self.thread_push_instance.isAlive():\n self.thread_push_instance.join()\n\n with self.batch_commit('STREAM_END'):\n for path, handle in six.iteritems(self.streamed_files.copy()):\n # open again and read full content\n full_path = os.path.normpath(self.temp_path + '/stream-blob/' + self.job_id + '/' + path)\n self.logger.debug('Git stream end for file: ' + full_path)\n\n del self.streamed_files[path]\n\n # make sure its written to the disk\n try:\n self.stream_files_lock.acquire()\n if not handle.closed:\n handle.flush()\n handle.close()\n finally:\n self.stream_files_lock.release()\n\n with open(full_path, 'r') as f:\n self.commit_file(path, path, f.read())\n\n if not self.keep_stream_files:\n os.unlink(full_path)\n\n with self.batch_commit('STORE_END'):\n for path, bar in six.iteritems(self.store_files.copy()):\n full_path = os.path.normpath(self.temp_path + '/store-blob/' + self.job_id + '/' + path)\n self.logger.debug('Git store end for file: ' + full_path)\n\n del self.store_files[path]\n\n try:\n self.stream_files_lock.acquire()\n self.commit_file(path, path, open(full_path, 'r').read())\n finally:\n self.stream_files_lock.release()\n\n if not self.keep_stream_files:\n os.unlink(full_path)", "response": "Stops the git push thread and commits all streamed files followed by a final git push."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef store_file(self, path, data, fast_lane=True):\n\n self.stream_files_lock.acquire()\n try:\n\n full_path = os.path.normpath(self.temp_path + '/store-blob/' + self.job_id + '/' + path)\n if not os.path.exists(os.path.dirname(full_path)):\n os.makedirs(os.path.dirname(full_path))\n\n if hasattr(data, 'encode'):\n data = data.encode(\"utf-8\", 'replace')\n\n already_set = path in self.store_files and self.store_files[path] == data\n\n if is_debug3():\n sys.__stderr__.write('git:store_file(%s, %s, %s), already_set=%s\\n'\n % (str(path), str(data)[0:180], str(fast_lane), str(already_set)))\n\n if already_set:\n return\n\n open(full_path, 'wb').write(data)\n self.store_files[path] = data\n\n if self.client.online is not False:\n self.client.send({'type': 'store-blob', 'path': path, 'data': data}, channel='' if fast_lane else 'files')\n finally:\n self.stream_files_lock.release()", "response": "Store the file at the given path and data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstream a file to the server.", "response": "def stream_file(self, path, fast_lane=True):\n \"\"\"\n Create a temp file, stream it to the server if online and append its content using the write() method. \n This makes sure that we have all newest data of this file on the server directly.\n \n At the end of the job, the content the server received is stored as git blob on the server. It is then committed \n locally and pushed. Git detects that the server already has the version (through the continuous streaming)\n and won't push it again. Very handy for rather large files that will append over time (like channel data, logs)\n \n Example:\n \n self.log_stream = git.stream_file('log.txt')\n \n self.log_stream.write(\"new line\\n\");\n self.log_stream.write(\"another line\\n\");\n \"\"\"\n\n # create temp file\n # open temp file\n\n # register stream file and write locally\n # on end() git_commit that file locally\n\n # create socket connection to server\n # stream file to server\n # on end() send server end signal, so he can store its content in git as blob as well.\n # A git push would detect that both sides have the same content already,\n # except when server connection broke between start() and end().\n # Result -> already transmitted logs/channel data (probably many MBs) won't transfered twice\n # when doing a git-push.\n\n # return handler to write to this file\n\n full_path = os.path.normpath(self.temp_path + '/stream-blob/' + self.job_id + '/' + path)\n if not os.path.exists(os.path.dirname(full_path)):\n os.makedirs(os.path.dirname(full_path))\n\n handle = open(full_path, 'wb')\n self.streamed_files[path] = handle\n\n class Stream():\n def __init__(self, git):\n self.git = git\n\n def write(self, data):\n if path not in self.git.streamed_files:\n # already committed to server\n return\n\n if hasattr(data, 'encode'):\n data = data.encode(\"utf-8\", 'replace')\n\n try:\n self.git.stream_files_lock.acquire()\n if not handle.closed:\n handle.write(data)\n handle.flush()\n except IOError as e:\n handle.close()\n\n if 'No space left' in e.__str__():\n sys.stderr.write(traceback.format_exc() + '\\n')\n self.git.logger.error(e.__str__())\n finally:\n self.git.stream_files_lock.release()\n\n if self.git.client.online is not False:\n self.git.client.send({'type': 'stream-blob', 'path': path, 'data': data}, channel='' if fast_lane else 'files')\n\n return Stream(self)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd new entry to the current index.", "response": "def add_index(self, mode, blob_id, path):\n \"\"\"\n Add new entry to the current index\n :param tree: \n :return: \n \"\"\"\n self.command_exec(['update-index', '--add', '--cacheinfo', mode, blob_id, path])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd a new file to the storage and add its tree entry into the index.", "response": "def add_file(self, git_path, content):\n \"\"\"\n Add a new file as blob in the storage and add its tree entry into the index.\n \n :param git_path: str\n :param content: str\n \"\"\"\n blob_id = self.write_blob(content)\n self.add_index('100644', blob_id, git_path)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a new file to the storage and add its tree entry into the index.", "response": "def add_file_path_in_work_tree(self, path, work_tree, verbose=True):\n \"\"\"\n Add a new file as blob in the storage and add its tree entry into the index.\n \"\"\"\n args = ['--work-tree', work_tree, 'add', '-f']\n if verbose:\n args.append('--verbose')\n args.append(path)\n self.command_exec(args, show_output=verbose)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a new file as blob in the storage and commit the index.", "response": "def commit_file(self, message, path, content):\n \"\"\"\n Add a new file as blob in the storage, add its tree entry into the index and commit the index.\n\n :param message: str\n :param path: str\n :param content: str\n :return: \n \"\"\"\n if self.git_batch_commit:\n self.add_file(path, content)\n self.git_batch_commit_messages.append(message)\n else:\n with self.lock_write():\n if self.job_id:\n self.read_tree(self.ref_head)\n\n self.add_file(path, content)\n\n return self.commit_index(message)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef diff_objects(self, latest_commit_sha):\n base = ['git', '--bare', '--git-dir', self.git_path]\n\n object_shas = []\n summary = {'commits': [], 'trees': [], 'files': []}\n\n def read_parents_and_tree_from(commit):\n if commit in self.synced_object_shas or commit in object_shas:\n # this commit has already been synced or read\n return None, None\n\n self.synced_object_shas[commit] = True\n summary['commits'].append(commit)\n object_shas.append(commit)\n\n object_content = subprocess.check_output(base + ['cat-file', '-p', commit]).decode('utf-8').strip()\n parents = []\n tree = ''\n for line in object_content.splitlines():\n if line.startswith('tree '):\n tree = line[len('tree '):]\n if line.startswith('parent '):\n parents.append(line[len('parent '):])\n\n return parents, tree\n\n def collect_files_from_tree(tree):\n if tree in self.synced_object_shas or tree in object_shas:\n # we have exactly this tree already synced or read, meaning all its objects as well\n return\n\n self.synced_object_shas[tree] = True\n summary['trees'].append(tree)\n object_shas.append(tree)\n\n object_content = subprocess.check_output(base + ['ls-tree', '-r', '-t', tree]).decode('utf-8').strip()\n\n for line in object_content.splitlines():\n exploded = line.split(' ')\n\n if len(exploded) < 3:\n sys.stderr.write(\"Error: Wrong line format of ls-tree for %s: %s\\n\" % (tree, line,))\n sys.exit(1)\n\n object_to_add = str(exploded[2][:40])\n path = str(exploded[2][41:])\n\n if object_to_add in self.synced_object_shas or object_to_add in object_shas:\n # have it already in the list or already synced\n continue\n\n object_shas.append(object_to_add)\n self.synced_object_shas[object_to_add] = True\n summary['files'].append([object_to_add, path])\n\n commits_to_check = [latest_commit_sha]\n\n while len(commits_to_check):\n sha = commits_to_check.pop(0)\n parents, tree = read_parents_and_tree_from(sha)\n\n if parents:\n for parent in parents:\n if parent not in commits_to_check:\n commits_to_check.append(parent)\n\n if tree:\n collect_files_from_tree(tree)\n\n is_debug2() and self.logger.debug(\"shas_to_check %d: %s \" % (len(object_shas), str(object_shas),))\n\n if not object_shas:\n return [], summary\n\n try:\n is_debug2() and self.logger.debug(\"Do git-cat-file-check.sh\")\n\n ssh_stream = create_ssh_stream(read_home_config(), exit_on_failure=False)\n channel = ssh_stream.get_transport().open_session()\n channel.exec_command('git-cat-file-check.sh \"%s\"' % (self.model_name + '.git',))\n channel.sendall('\\n'.join(object_shas))\n channel.shutdown_write()\n\n def readall(c):\n content = b''\n while True:\n try:\n chunk = c.recv(1024)\n if chunk == b'':\n break\n content += chunk\n except (KeyboardInterrupt, SystemExit):\n return\n\n return content\n\n missing_objects = readall(channel).decode('utf-8').splitlines()\n channel.close()\n ssh_stream.close()\n\n # make sure we have in summary only SHAs we actually will sync\n for stype in six.iterkeys(summary):\n ids = summary[stype][:]\n for sha in ids:\n if stype == 'files':\n if sha[0] not in missing_objects:\n summary[stype].remove(sha)\n else:\n if sha not in missing_objects:\n summary[stype].remove(sha)\n\n return missing_objects, summary\n except (KeyboardInterrupt, SystemExit):\n raise\n except Exception as e:\n self.logger.error(\"Failed to generate diff_objects: %s\" % (str(e),))\n for sha in object_shas:\n if sha in self.synced_object_shas:\n del self.synced_object_shas[sha]\n return None, None", "response": "Push all changes to origin based on objects on commits."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef commit_index(self, message):\n tree_id = self.write_tree()\n\n args = ['commit-tree', tree_id, '-p', self.ref_head]\n\n # todo, this can end in a race-condition with other processes adding commits\n commit = self.command_exec(args, message)[0].decode('utf-8').strip()\n self.command_exec(['update-ref', self.ref_head, commit])\n\n return commit", "response": "Commit the current index."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreads the given path of current ref_head and returns its content as utf - 8", "response": "def contents(self, path):\n \"\"\"\n Reads the given path of current ref_head and returns its content as utf-8\n \"\"\"\n try:\n out, code, err = self.command_exec(['cat-file', '-p', self.ref_head+':'+path])\n if not code:\n return out.decode('utf-8')\n except Exception:\n pass\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstarting training of a job.", "response": "def job_start(job_backend, trainer, keras_callback):\n \"\"\"\n Starts the training of a job. Needs job_prepare() first.\n\n :type job_backend: JobBackend\n :type trainer: Trainer\n :return:\n \"\"\"\n\n job_backend.set_status('STARTING')\n job_model = job_backend.get_job_model()\n\n model_provider = job_model.get_model_provider()\n\n job_backend.set_status('LOAD DATA')\n datasets = job_model.get_datasets(trainer)\n\n print('trainer.input_shape = %s\\n' % (simplejson.dumps(trainer.input_shape, default=invalid_json_values),))\n print('trainer.classes = %s\\n' % (simplejson.dumps(trainer.classes, default=invalid_json_values),))\n\n multiple_inputs = len(datasets) > 1\n insights_x = [] if multiple_inputs else None\n\n for dataset_name in job_model.get_input_dataset_names():\n dataset = datasets[dataset_name]\n\n if is_generator(dataset['X_train']):\n batch_x, batch_y = dataset['X_train'].next()\n\n if multiple_inputs:\n insights_x.append(batch_x[0])\n else:\n insights_x = batch_x[0]\n else:\n if multiple_inputs:\n insights_x.append(dataset['X_train'][0])\n else:\n insights_x = dataset['X_train'][0]\n\n keras_callback.insights_x = insights_x\n print('Insights sample shape', keras_callback.insights_x.shape)\n keras_callback.write(\"Possible data keys '%s'\\n\" % \"','\".join(list(datasets.keys())))\n\n data_train = model_provider.get_training_data(trainer, datasets)\n data_validation = model_provider.get_validation_data(trainer, datasets)\n\n keras_callback.set_validation_data(data_validation, trainer.nb_val_samples)\n\n trainer.set_status('CONSTRUCT')\n model = model_provider.get_model(trainer)\n trainer.set_model(model)\n\n trainer.set_status('COMPILING')\n loss = model_provider.get_loss(trainer)\n optimizer = model_provider.get_optimizer(trainer)\n model_provider.compile(trainer, model, loss, optimizer)\n model.summary()\n\n trainer.callbacks.append(keras_callback)\n model_provider.train(trainer, model, data_train, data_validation)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_images_in_memory(job_model, dataset, node, trainer):\n concurrent = psutil.cpu_count()\n\n dataset_config = dataset['config']\n controller = {'running': True}\n q = Queue(concurrent)\n\n result = {\n 'X_train': [],\n 'Y_train': [],\n 'X_test': [],\n 'Y_test': []\n }\n\n images = []\n max = 0\n\n path = job_model.get_dataset_downloads_dir(dataset)\n if 'path' in dataset['config']:\n path = dataset['config']['path']\n\n classes_count = 0\n category_map = {}\n classes = []\n\n trainer.set_status('LOAD IMAGES INTO MEMORY')\n\n try:\n for i in range(concurrent):\n t = ImageReadWorker(q, job_model, node, path, images, controller)\n t.daemon = True\n t.start()\n\n for validation_or_training in ['validation', 'training']:\n if os.path.isdir(os.path.normpath(path + '/' + validation_or_training)):\n for category_name in os.listdir(os.path.normpath(path + '/' + validation_or_training)):\n if os.path.isdir(os.path.normpath(path + '/' + validation_or_training + '/' + category_name)):\n\n if category_name not in category_map:\n category_map[category_name] = classes_count\n if 'classes' in dataset_config and 'category_' in category_name:\n category_idx = int(category_name.replace('category_', ''))\n category_map[category_name] = category_idx\n target_category = dataset_config['classes'][category_idx]\n classes.append(target_category['title'] or 'Class %s' % (category_idx, ))\n else:\n classes.append(category_name)\n\n classes_count += 1\n\n for id in os.listdir(os.path.normpath(path + '/' + validation_or_training + '/' + category_name)):\n file_path = os.path.join(path, validation_or_training, category_name, id)\n q.put([file_path, validation_or_training == 'validation', category_name])\n max += 1\n\n q.join()\n controller['running'] = False\n\n train_images = []\n test_images = []\n\n for v in images:\n image, validation, category_dir = v\n if validation is True:\n test_images.append([image, category_map[category_dir]])\n else:\n train_images.append([image, category_map[category_dir]])\n\n train_datagen = None\n augmentation = bool(get_option(dataset_config, 'augmentation', False))\n if augmentation:\n train_datagen = get_image_data_augmentor_from_dataset(dataset)\n\n train = InMemoryDataGenerator(train_datagen, train_images, classes_count, job_model.job['config']['batchSize'])\n\n test = InMemoryDataGenerator(None, test_images, classes_count, job_model.job['config']['batchSize'])\n\n nb_sample = len(train_images)\n trainer.set_info('Dataset size', {'training': nb_sample, 'validation': len(test_images)})\n trainer.set_generator_training_nb(nb_sample)\n trainer.set_generator_validation_nb(len(test_images))\n\n trainer.logger.info((\"Found %d classes, %d images (%d in training [%saugmented], %d in validation). Read all images into memory from %s\" %\n (classes_count, max, len(train_images), 'not ' if augmentation is False else '', len(test_images), path)))\n\n if classes_count == 0:\n trainer.logger.warning(\"Could not find any classes. Does the directory contains images?\")\n sys.exit(1)\n\n trainer.output_size = classes_count\n trainer.set_info('classes', classes)\n trainer.classes = classes\n\n result['X_train'] = train\n result['Y_train'] = train\n result['X_test'] = test\n result['Y_test'] = test\n\n return result\n\n except KeyboardInterrupt:\n controller['running'] = False\n sys.exit(1)", "response": "Reads all images into memory and applies augmentation if enabled"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the list of devices that are available on the device system.", "response": "def get_ordered_devices():\n \"\"\"\n Default CUDA_DEVICE_ORDER is not compatible with nvidia-docker.\n Nvidia-Docker is using CUDA_DEVICE_ORDER=PCI_BUS_ID.\n\n https://github.com/NVIDIA/nvidia-docker/wiki/nvidia-docker#gpu-isolation\n \"\"\"\n\n libcudart = get_libcudart()\n\n devices = {}\n for i in range(0, get_installed_devices()):\n gpu = get_device_properties(i)\n\n pciBusId = ctypes.create_string_buffer(64)\n libcudart.cudaDeviceGetPCIBusId(ctypes.byref(pciBusId), 64, i)\n full_id = pciBusId.value.decode('utf-8')\n gpu['fullId'] = full_id\n devices[full_id] = gpu\n\n ordered = []\n\n i = 0\n for key in sorted(devices):\n devices[key]['id'] = i\n ordered.append(devices[key])\n i += 1\n\n del libcudart\n\n return ordered"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sorted_nicely(l):\n convert = lambda text: int(text) if text.isdigit() else text\n alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]\n return sorted(l, key = alphanum_key)", "response": "Sort the given iterable in natural order."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the part number equivalent to the aracore. n.", "response": "def to_pn(self, sub_letter=None):\n \"\"\"\n Returns the part number equivalent. For instance,\n a '1k' would still be '1k', but a\n '1.2k' would, instead, be a '1k2'\n :return:\n \"\"\"\n string = str(self)\n if '.' not in string:\n return string\n\n # take care of the case of when there is no scaling unit\n if not string[-1].isalpha():\n if sub_letter is not None:\n return string.replace('.', sub_letter)\n\n return string\n\n letter = string[-1]\n return string.replace('.', letter)[:-1]"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns two lists - the last N items of li whose total size is less than MAX_SIZE - the rest of the original list li", "response": "def reduce_list_size(li):\n \"\"\"Return two lists\n - the last N items of li whose total size is less than MAX_SIZE\n - the rest of the original list li\n \"\"\"\n # sys.getsizeof is nearly useless. All our data is stringable so rather\n # use that as a measure of size.\n size = len(repr(li))\n keep = li\n toss = []\n n = len(li)\n decrement_by = max(n / 10, 10)\n while (size >= MAX_SIZE) and (n > 0):\n n -= decrement_by\n toss = li[:-n]\n keep = li[-n:]\n size = len(repr(keep))\n return keep, toss"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninspect request for objects in _ultracache and set appropriate entries in Django s cache.", "response": "def cache_meta(request, cache_key, start_index=0):\n \"\"\"Inspect request for objects in _ultracache and set appropriate entries\n in Django's cache.\"\"\"\n\n path = request.get_full_path()\n # todo: cache headers on the request since they never change during the\n # request.\n\n # Reduce headers to the subset as defined by the settings\n headers = OrderedDict()\n for k, v in sorted(request.META.items()):\n if (k == \"HTTP_COOKIE\") and CONSIDER_COOKIES:\n cookie = SimpleCookie()\n cookie.load(v)\n headers[\"cookie\"] = \"; \".join([\n \"%s=%s\" % (k, morsel.value) for k, morsel \\\n in sorted(cookie.items()) if k in CONSIDER_COOKIES\n ])\n elif k.startswith(\"HTTP_\"):\n k = k[5:].replace(\"_\", \"-\").lower()\n if k in CONSIDER_HEADERS:\n headers[k] = v\n\n # Lists needed for cache.get_many\n to_set_get_keys = []\n to_set_paths_get_keys = []\n to_set_content_types_get_keys = []\n to_set_content_types_paths_get_keys = []\n\n # Dictionaries needed for cache.set_many\n to_set = {}\n to_set_paths = {}\n to_set_content_types = {}\n to_set_content_types_paths = {}\n\n to_delete = []\n to_set_objects = []\n\n for ctid, obj_pk in request._ultracache[start_index:]:\n # The object appears in these cache entries. If the object is modified\n # then these cache entries are deleted.\n key = \"ucache-%s-%s\" % (ctid, obj_pk)\n if key not in to_set_get_keys:\n to_set_get_keys.append(key)\n\n # The object appears in these paths. If the object is modified then any\n # caches that are read from when browsing to this path are cleared.\n key = \"ucache-pth-%s-%s\" % (ctid, obj_pk)\n if key not in to_set_paths_get_keys:\n to_set_paths_get_keys.append(key)\n\n # The content type appears in these cache entries. If an object of this\n # content type is created then these cache entries are cleared.\n key = \"ucache-ct-%s\" % ctid\n if key not in to_set_content_types_get_keys:\n to_set_content_types_get_keys.append(key)\n\n # The content type appears in these paths. If an object of this content\n # type is created then any caches that are read from when browsing to\n # this path are cleared.\n key = \"ucache-ct-pth-%s\" % ctid\n if key not in to_set_content_types_paths_get_keys:\n to_set_content_types_paths_get_keys.append(key)\n\n # A list of objects that contribute to a cache entry\n tu = (ctid, obj_pk)\n if tu not in to_set_objects:\n to_set_objects.append(tu)\n\n # todo: rewrite to handle absence of get_many\n di = cache.get_many(to_set_get_keys)\n for key in to_set_get_keys:\n v = di.get(key, None)\n keep = []\n if v is not None:\n keep, toss = reduce_list_size(v)\n if toss:\n to_set[key] = keep\n to_delete.extend(toss)\n if cache_key not in keep:\n if key not in to_set:\n to_set[key] = keep\n to_set[key] = to_set[key] + [cache_key]\n if to_set == di:\n to_set = {}\n\n di = cache.get_many(to_set_paths_get_keys)\n for key in to_set_paths_get_keys:\n v = di.get(key, None)\n keep = []\n if v is not None:\n keep, toss = reduce_list_size(v)\n if toss:\n to_set_paths[key] = keep\n if [path, headers] not in keep:\n if key not in to_set_paths:\n to_set_paths[key] = keep\n to_set_paths[key] = to_set_paths[key] + [[path, headers]]\n if to_set_paths == di:\n to_set_paths = {}\n\n di = cache.get_many(to_set_content_types_get_keys)\n for key in to_set_content_types_get_keys:\n v = di.get(key, None)\n keep = []\n if v is not None:\n keep, toss = reduce_list_size(v)\n if toss:\n to_set_content_types[key] = keep\n to_delete.extend(toss)\n if cache_key not in keep:\n if key not in to_set_content_types:\n to_set_content_types[key] = keep\n to_set_content_types[key] = to_set_content_types[key] + [cache_key]\n if to_set_content_types == di:\n to_set_content_types = {}\n\n di = cache.get_many(to_set_content_types_paths_get_keys)\n for key in to_set_content_types_paths_get_keys:\n v = di.get(key, None)\n keep = []\n if v is not None:\n keep, toss = reduce_list_size(v)\n if toss:\n to_set_content_types_paths[key] = keep\n if [path, headers] not in keep:\n if key not in to_set_content_types_paths:\n to_set_content_types_paths[key] = keep\n to_set_content_types_paths[key] = to_set_content_types_paths[key] \\\n + [[path, headers]]\n if to_set_content_types_paths == di:\n to_set_content_types_paths = {}\n\n # Deletion must happen first because set may set some of these keys\n if to_delete:\n try:\n cache.delete_many(to_delete)\n except NotImplementedError:\n for k in to_delete:\n cache.delete(k)\n\n # Do one set_many\n di = {}\n di.update(to_set)\n del to_set\n di.update(to_set_paths)\n del to_set_paths\n di.update(to_set_content_types)\n del to_set_content_types\n di.update(to_set_content_types_paths)\n del to_set_content_types_paths\n\n if to_set_objects:\n di[cache_key + \"-objs\"] = to_set_objects\n\n if di:\n try:\n cache.set_many(di, 86400)\n except NotImplementedError:\n for k, v in di.items():\n cache.set(k, v, 86400)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the TRUSTEE indy - sdk role for an anchor acting in an AnchorSmith capacity.", "response": "def least_role() -> Role:\n \"\"\"\n Return the TRUSTEE indy-sdk role for an anchor acting in an AnchorSmith capacity.\n\n :return: TRUSTEE role\n \"\"\"\n\n LOGGER.debug('AnchorSmith.least_role >>>')\n\n rv = Role.TRUSTEE.token()\n\n LOGGER.debug('AnchorSmith.least_role <<< %s', rv)\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def send_nym(self, did: str, verkey: str = None, alias: str = None, role: Role = None) -> None:\n\n LOGGER.debug(\n 'AnchorSmith.send_nym >>> did: %s, verkey: %s, alias: %s, role: %s', did, verkey, alias, role)\n\n if not ok_did(did):\n LOGGER.debug('AnchorSmith.send_nym dict:\n \"\"\"\n Set wallets as configured for setnym operation.\n\n :param an_data: dict mapping profiles to anchor data\n :return: dict mapping anchor names to wallet objects\n \"\"\"\n\n w_mgr = WalletManager()\n rv = {}\n for profile in an_data:\n w_cfg = {'id': an_data[profile].name}\n if an_data[profile].wallet_type:\n w_cfg['storage_type'] = an_data[profile].wallet_type\n if an_data[profile].seed:\n w_cfg['seed'] = an_data[profile].seed\n if an_data[profile].did:\n w_cfg['did'] = an_data[profile].did\n if an_data[profile].wallet_create:\n try:\n await w_mgr.create(w_cfg, access=an_data[profile].wallet_access)\n except ExtantWallet:\n pass\n rv[profile] = w_mgr.get(w_cfg, access=an_data[profile].wallet_access)\n\n return rv"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset nym for the current node in the ledger.", "response": "async def setnym(ini_path: str) -> int:\n \"\"\"\n Set configuration. Open pool, trustee anchor, and wallet of anchor whose nym to send.\n Register exit hooks to close pool and trustee anchor.\n\n Engage trustee anchor to send nym for VON anchor, if it differs on the ledger from configuration.\n\n :param ini_path: path to configuration file\n :return: 0 for OK, 1 for failure\n \"\"\"\n\n config = inis2dict(ini_path)\n if config['Trustee Anchor']['name'] == config['VON Anchor']['name']:\n raise ExtantWallet('Wallet names must differ between VON Anchor and Trustee Anchor')\n\n cfg_van_role = config['VON Anchor'].get('role', None) or None # nudge empty value from '' to None\n if not ok_role(cfg_van_role):\n raise BadRole('Configured role {} is not valid'.format(cfg_van_role))\n\n pool_data = NodePoolData(\n config['Node Pool']['name'],\n config['Node Pool'].get('genesis.txn.path', None) or None)\n\n an_data = {\n 'tan': AnchorData(\n Role.TRUSTEE,\n config['Trustee Anchor']['name'],\n config['Trustee Anchor'].get('seed', None) or None,\n config['Trustee Anchor'].get('did', None) or None,\n config['Trustee Anchor'].get('wallet.create', '0').lower() in ['1', 'true', 'yes'],\n config['Trustee Anchor'].get('wallet.type', None) or None,\n config['Trustee Anchor'].get('wallet.access', None) or None),\n 'van': AnchorData(\n Role.get(cfg_van_role),\n config['VON Anchor']['name'],\n config['VON Anchor'].get('seed', None) or None,\n config['VON Anchor'].get('did', None) or None,\n config['VON Anchor'].get('wallet.create', '0').lower() in ['1', 'true', 'yes'],\n config['VON Anchor'].get('wallet.type', None) or None,\n config['VON Anchor'].get('wallet.access', None) or None)\n }\n\n an_wallet = await _set_wallets(an_data)\n\n p_mgr = NodePoolManager()\n if pool_data.name not in await p_mgr.list():\n if pool_data.genesis_txn_path:\n await p_mgr.add_config(pool_data.name, pool_data.genesis_txn_path)\n else:\n raise AbsentPool('Node pool {} has no ledger configuration, but {} specifies no genesis txn path'.format(\n pool_data.name,\n ini_path))\n\n async with an_wallet['tan'] as w_tan, (\n an_wallet['van']) as w_van, (\n p_mgr.get(pool_data.name)) as pool, (\n TrusteeAnchor(w_tan, pool)) as tan, (\n NominalAnchor(w_van, pool)) as van:\n\n send_verkey = van.verkey\n try:\n nym_role = await tan.get_nym_role(van.did)\n if an_data['van'].role == nym_role:\n return 0 # ledger is as per configuration\n send_verkey = None # only owner can touch verkey\n if nym_role != Role.USER: # only remove role when it is not already None on the ledger\n await tan.send_nym(van.did, send_verkey, van.wallet.name, Role.ROLE_REMOVE)\n except AbsentNym:\n pass # cryptonym not there yet, fall through\n\n await tan.send_nym(van.did, send_verkey, van.wallet.name, an_data['van'].role)\n\n return 0"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef main(args: Sequence[str] = None) -> int:\n\n logging.basicConfig(\n level=logging.INFO,\n format='%(asctime)-15s | %(levelname)-8s | %(message)s',\n datefmt='%Y-%m-%d %H:%M:%S')\n logging.getLogger('von_anchor').setLevel(logging.WARNING)\n logging.getLogger('indy').setLevel(logging.ERROR)\n\n if args is None:\n args = sys.argv[1:]\n\n if len(sys.argv) == 2:\n try:\n return do_wait(setnym(sys.argv[1]))\n except VonAnchorError as vax:\n print(str(vax))\n return 1\n else:\n usage()\n return 1", "response": "Main line for script."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns schema identifier for input origin DID schema name and schema version.", "response": "def schema_id(origin_did: str, name: str, version: str) -> str:\n \"\"\"\n Return schema identifier for input origin DID, schema name, and schema version.\n\n :param origin_did: DID of schema originator\n :param name: schema name\n :param version: schema version\n :return: schema identifier\n \"\"\"\n\n return '{}:2:{}:{}'.format(origin_did, name, version)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ok_did(token: str) -> bool:\n\n return bool(re.match('[{}]{{21,22}}$'.format(B58), token or ''))", "response": "Checks if input token looks like a valid distributed identifier."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning schema key for schema identifier components.", "response": "def schema_key(s_id: str) -> SchemaKey:\n \"\"\"\n Return schema key (namedtuple) convenience for schema identifier components.\n\n :param s_id: schema identifier\n :return: schema key (namedtuple) object\n \"\"\"\n\n s_key = s_id.split(':')\n s_key.pop(1) # take out indy-sdk schema marker: 2 marks indy-sdk schema id\n\n return SchemaKey(*s_key)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cred_def_id(issuer_did: str, schema_seq_no: int, protocol: Protocol = None) -> str:\n\n return (protocol or Protocol.DEFAULT).cred_def_id(issuer_did, schema_seq_no)", "response": "Returns the credential definition identifier for input issuer DID and schema sequence number."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn whether input token looks like a credential definition identifier from input issuer DID.", "response": "def ok_cred_def_id(token: str, issuer_did: str = None) -> bool:\n \"\"\"\n Whether input token looks like a valid credential definition identifier from input issuer DID (default any); i.e.,\n :3:CL:: for protocol >= 1.4, or\n :3:CL: for protocol == 1.3.\n\n :param token: candidate string\n :param issuer_did: issuer DID to match, if specified\n :return: whether input token looks like a valid credential definition identifier\n \"\"\"\n\n cd_id_m = re.match('([{}]{{21,22}}):3:CL:[1-9][0-9]*(:.+)?$'.format(B58), token or '')\n return bool(cd_id_m) and ((not issuer_did) or cd_id_m.group(1) == issuer_did)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef cred_def_id2seq_no(cd_id: str) -> int:\n\n if ok_cred_def_id(cd_id):\n return int(cd_id.split(':')[3]) # sequence number is token at 0-based position 3\n raise BadIdentifier('Bad credential definition identifier {}'.format(cd_id))", "response": "Given a credential definition identifier return its schema sequence number. Raise BadIdentifier on input that is not a credential definition identifier."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngives a credential definition identifier and a tag return the corresponding revocation registry identifier.", "response": "def rev_reg_id(cd_id: str, tag: Union[str, int]) -> str:\n \"\"\"\n Given a credential definition identifier and a tag, return the corresponding\n revocation registry identifier, repeating the issuer DID from the\n input identifier.\n\n :param cd_id: credential definition identifier\n :param tag: tag to use\n :return: revocation registry identifier\n \"\"\"\n\n return '{}:4:{}:CL_ACCUM:{}'.format(cd_id.split(\":\", 1)[0], cd_id, tag)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ok_rev_reg_id(token: str, issuer_did: str = None) -> bool:\n\n rr_id_m = re.match(\n '([{0}]{{21,22}}):4:([{0}]{{21,22}}):3:CL:[1-9][0-9]*(:.+)?:CL_ACCUM:.+$'.format(B58),\n token or '')\n return bool(rr_id_m) and ((not issuer_did) or (rr_id_m.group(1) == issuer_did and rr_id_m.group(2) == issuer_did))", "response": "Checks if input token looks like a revocation registry identifier from input issuer DID."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ngive a revocation registry identifier return its corresponding credential definition identifier. Raise BadIdentifier if input is not a revocation registry identifier.", "response": "def rev_reg_id2cred_def_id(rr_id: str) -> str:\n \"\"\"\n Given a revocation registry identifier, return its corresponding credential definition identifier.\n Raise BadIdentifier if input is not a revocation registry identifier.\n\n :param rr_id: revocation registry identifier\n :return: credential definition identifier\n \"\"\"\n\n if ok_rev_reg_id(rr_id):\n return ':'.join(rr_id.split(':')[2:-2]) # rev reg id comprises (prefixes)::(suffixes)\n raise BadIdentifier('Bad revocation registry identifier {}'.format(rr_id))"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngives a revocation registry identifier return its corresponding credential definition identifier and tag. Raise BadIdentifier if input is not a revocation registry identifier.", "response": "def rev_reg_id2cred_def_id_tag(rr_id: str) -> (str, str):\n \"\"\"\n Given a revocation registry identifier, return its corresponding credential definition identifier and\n (stringified int) tag. Raise BadIdentifier if input is not a revocation registry identifier.\n\n :param rr_id: revocation registry identifier\n :return: credential definition identifier and tag\n \"\"\"\n\n if ok_rev_reg_id(rr_id):\n return (\n ':'.join(rr_id.split(':')[2:-2]), # rev reg id comprises (prefixes)::(suffixes)\n str(rr_id.split(':')[-1]) # tag is last token\n )\n raise BadIdentifier('Bad revocation registry identifier {}'.format(rr_id))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef iter_briefs(briefs: Union[dict, Sequence[dict]]) -> tuple:\n\n if isinstance(briefs, dict):\n if all(ok_wallet_reft(k) for k in briefs):\n return tuple(briefs.values())\n return (briefs,)\n return tuple(briefs)", "response": "Given a cred - briefs dict or sequence of cred - briefs return tuple with all contained cred - briefs."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef box_ids(briefs: Union[dict, Sequence[dict]], cred_ids: Union[Sequence[str], str] = None) -> dict:\n\n rv = {}\n for brief in iter_briefs(briefs):\n cred_info = brief.get('cred_info', {}) or brief # briefs could be cred-infos or cred-briefs\n cred_id = cred_info['referent']\n if ((cred_id not in rv) and (not cred_ids or cred_id in [cred_ids, [cred_ids]][isinstance(cred_ids, str)])):\n rv[cred_id] = {\n 'schema_id': cred_info['schema_id'],\n 'cred_def_id': cred_info['cred_def_id'],\n 'rev_reg_id': cred_info['rev_reg_id']\n }\n\n return rv", "response": "Given one or more cred - briefs and cred - ids and optional sequence of credential identifiers return a dict mapping each credential identifier to its corresponding box ids structure."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving all creds out of the input json structure that do not match any of the input credential identifiers.", "response": "def prune_creds_json(creds: dict, cred_ids: set) -> str:\n \"\"\"\n Strip all creds out of the input json structure that do not match any of the input credential identifiers.\n\n :param creds: indy-sdk creds structure\n :param cred_ids: the set of credential identifiers of interest\n :return: the reduced creds json\n \"\"\"\n\n rv = deepcopy(creds)\n for key in ('attrs', 'predicates'):\n for attr_uuid, creds_by_uuid in rv[key].items():\n rv[key][attr_uuid] = [cred for cred in creds_by_uuid if cred['cred_info']['referent'] in cred_ids]\n\n empties = [attr_uuid for attr_uuid in rv[key] if not rv[key][attr_uuid]]\n for attr_uuid in empties:\n del rv[key][attr_uuid]\n\n return json.dumps(rv)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef proof_req_infos2briefs(proof_req: dict, infos: Union[dict, Sequence[dict]]) -> list:\n\n rv = []\n refts = proof_req_attr_referents(proof_req)\n for info in iter_briefs(infos):\n if info['cred_def_id'] not in refts:\n continue\n brief = {\n 'cred_info': info,\n 'interval': {}\n }\n fro = None\n to = None\n for uuid in refts[info['cred_def_id']].values():\n interval = proof_req['requested_attributes'][uuid].get('non_revoked', {})\n if 'from' in interval:\n fro = min(fro or interval['from'], interval['from'])\n if 'to' in interval:\n to = max(to or interval['to'], interval['to'])\n\n if to:\n brief['interval']['to'] = to\n if fro:\n brief['interval']['from'] = fro\n if not brief['interval']:\n brief['interval'] = None\n\n rv.append(brief)\n\n return rv", "response": "Given a proof request and corresponding cred - info s return a list of cred - briefs."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef proof_req_briefs2req_creds(proof_req: dict, briefs: Union[dict, Sequence[dict]]) -> dict:\n\n rv = {\n 'self_attested_attributes': {},\n 'requested_attributes': {},\n 'requested_predicates': {}\n }\n\n attr_refts = proof_req_attr_referents(proof_req)\n pred_refts = proof_req_pred_referents(proof_req)\n for brief in iter_briefs(briefs):\n cred_info = brief['cred_info']\n timestamp = (brief['interval'] or {}).get('to', None)\n for attr in cred_info['attrs']:\n if attr in attr_refts.get(cred_info['cred_def_id'], {}):\n req_attr = {\n 'cred_id': cred_info['referent'],\n 'revealed': attr not in pred_refts.get(cred_info['cred_def_id'], {}),\n 'timestamp': timestamp\n }\n if not timestamp:\n req_attr.pop('timestamp')\n rv['requested_attributes'][attr_refts[cred_info['cred_def_id']][attr]] = req_attr\n if attr in pred_refts.get(cred_info['cred_def_id'], {}):\n for uuid in pred_refts[cred_info['cred_def_id']][attr]:\n req_pred = {\n 'cred_id': cred_info['referent'],\n 'timestamp': timestamp\n }\n if not timestamp:\n req_pred.pop('timestamp')\n rv['requested_predicates'][uuid] = req_pred\n return rv", "response": "Given a proof request and cred - brief dict return a requested - creds structure."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef creds_display(creds: Union[dict, Sequence[dict]], filt: dict = None, filt_dflt_incl: bool = False) -> dict:\n\n def _add(briefs):\n nonlocal rv, filt\n for brief in briefs:\n cred_info = brief.get('cred_info', {}) or brief # briefs could be cred-infos or cred-briefs\n if cred_info['referent'] in rv:\n continue\n cred_cd_id = cred_info['cred_def_id']\n if (not filt) or (filt_dflt_incl and cred_cd_id not in filt):\n rv[cred_info['referent']] = cred_info\n continue\n if filt and cred_cd_id in filt:\n if ({k: str(filt[cred_cd_id][k]) for k in filt[cred_cd_id]}.items() <= cred_info['attrs'].items()):\n rv[cred_info['referent']] = cred_info\n\n rv = {}\n if filt is None:\n filt = {}\n if isinstance(creds, dict):\n if all(ok_wallet_reft(k) for k in creds):\n _add(creds.values())\n else:\n for uuid2briefs in (creds.get('attrs', {}), creds.get('predicates', {})):\n for briefs in uuid2briefs.values():\n _add(briefs)\n else:\n _add(creds)\n return rv", "response": "Return a human - legible summary of the given creds."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef proof_req2wql_all(proof_req: dict, x_cd_ids: Union[str, Sequence[str]] = None) -> dict:\n\n rv = {}\n attr_refts = proof_req_attr_referents(proof_req)\n for cd_id in [k for k in attr_refts if k not in ([x_cd_ids] if isinstance(x_cd_ids, str) else x_cd_ids or [])]:\n rv[set(attr_refts[cd_id].values()).pop()] = {\"cred_def_id\": cd_id}\n\n return rv", "response": "Given a proof request and a list of cred def ids to omit return an extra WQL query dict that will find all corresponding credentials in search."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a nested dict mapping cred def id to name and attribute referents by cred def id and attribute.", "response": "def proof_req_attr_referents(proof_req: dict) -> dict:\n \"\"\"\n Given a proof request with all requested attributes having cred def id restrictions,\n return its attribute referents by cred def id and attribute.\n\n The returned structure can be useful in populating the extra WQL query parameter\n in the credential search API.\n\n :param proof_req: proof request with all requested attribute specifications having cred def id restriction; e.g.,\n\n ::\n\n {\n 'name\": 'proof_req',\n 'version': '0.0',\n 'requested_attributes': {\n '18_greenLevel_uuid': {\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:18:tag'\n }\n ],\n 'name': 'greenLevel',\n 'non_revoked': {\n 'to': 1532367957,\n 'from': 1532367957\n }\n },\n '18_legalName_uuid': {\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:18:tag'\n }\n ],\n 'name': 'legalName',\n 'non_revoked': {\n 'to': 1532367957,\n 'from': 1532367957\n }\n },\n '15_id_uuid': { # this specification will not show up in response: no cred def id restriction :-(\n 'name': 'id',\n 'non_revoked': {\n 'to': 1532367957,\n 'from': 1532367957\n }\n }\n }\n 'requested_predicates': {\n }\n }\n\n :return: nested dict mapping cred def id to name to proof request referent; e.g.,\n\n ::\n\n {\n 'WgWxqztrNooG92RXvxSTWv:3:CL:18:tag': {\n 'legalName': '18_legalName_uuid'\n 'greenLevel': '18_greenLevel_uuid'\n }\n }\n\n \"\"\"\n\n rv = {}\n for uuid, spec in proof_req['requested_attributes'].items():\n cd_id = None\n for restriction in spec.get('restrictions', []):\n cd_id = restriction.get('cred_def_id', None)\n if cd_id:\n break\n if not cd_id:\n continue\n if cd_id not in rv: # cd_id of None is not OK\n rv[cd_id] = {}\n rv[cd_id][spec['name']] = uuid\n\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ngive a proof request return its predicate referents by cred def id and attribute mapping a predicate and a limit.", "response": "def proof_req_pred_referents(proof_req: dict) -> dict:\n \"\"\"\n Given a proof request with all requested predicates having cred def id restrictions,\n return its predicate referents by cred def id and attribute, mapping a predicate and a limit.\n\n The returned structure can be useful in downstream processing to filter cred-infos for predicates.\n\n :param proof_req: proof request with all requested predicate specifications having cred def id restriction; e.g.,\n\n ::\n\n {\n 'name': 'proof_req',\n 'version': '0.0',\n 'requested_attributes': {\n ...\n }\n 'requested_predicates': {\n '194_highscore_GE_uuid': {\n 'name': 'highscore',\n 'p_type': '>=',\n 'p_value': '100000',\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:194:tag'\n }\n ],\n 'non_revoked': {\n ...\n }\n },\n '194_level_GE_uuid': {\n 'name': 'level',\n 'p_type': '>=',\n 'p_value': '10',\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:194:tag'\n }\n ],\n 'non_revoked': {\n ...\n }\n },\n '194_attempts_LE_uuid': {\n 'name': 'attempts',\n 'p_type': '<=',\n 'p_value': '3',\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:194:tag'\n }\n ],\n 'non_revoked': {\n ...\n }\n },\n '198_employees_LT_uuid': {\n 'name': 'employees',\n 'p_type': '<',\n 'p_value': '100',\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:198:tag'\n }\n ],\n 'non_revoked': {\n ...\n }\n },\n '198_employees_GE_uuid': {\n 'name': 'employees',\n 'p_type': '>=',\n 'p_value': '50',\n 'restrictions': [\n {\n 'cred_def_id': 'WgWxqztrNooG92RXvxSTWv:3:CL:198:tag'\n }\n ],\n 'non_revoked': {\n ...\n }\n },\n }\n }\n\n :return: nested dict mapping cred def id to name to proof request referent to predicate and limit; e.g.,\n\n ::\n\n {\n 'WgWxqztrNooG92RXvxSTWv:3:CL:194:tag': {\n 'highscore': {\n '194_level_GE_uuid': ['>=', 100000]\n },\n 'level': {\n '194_level_GE_uuid': ['>=', 10]\n },\n 'attempts': {\n '194_attempts_LE_uuid': ['<=', 3]\n }\n },\n 'WgWxqztrNooG92RXvxSTWv:3:CL:198:tag': {\n 'employees': { # may have many preds per attr, but always 1 uuid and 1 relation per pred\n '198_LT_employees_uuid': ['<=', 100]\n '198_GE_employees_uuid': ['>=', 50]\n }\n }\n }\n\n \"\"\"\n\n rv = {}\n for uuid, spec in proof_req['requested_predicates'].items():\n cd_id = None\n for restriction in spec.get('restrictions', []):\n cd_id = restriction.get('cred_def_id', None)\n if cd_id:\n break\n if not cd_id:\n continue\n if cd_id not in rv: # cd_id of None is not OK\n rv[cd_id] = {}\n if spec['name'] not in rv[cd_id]:\n rv[cd_id][spec['name']] = {}\n rv[cd_id][spec['name']][uuid] = [spec['p_type'], Predicate.to_int(spec['p_value'])]\n\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef revoc_info(briefs: Union[dict, Sequence[dict]], filt: dict = None) -> dict:\n\n rv = {}\n for brief in iter_briefs(briefs):\n cred_info = brief.get('cred_info', {}) or brief # briefs could be cred-infos or cred-briefs\n (rr_id, cr_id) = (cred_info['rev_reg_id'], cred_info['cred_rev_id'])\n if (rr_id, cr_id) in rv or rr_id is None or cr_id is None:\n continue\n if not filt:\n rv[(rr_id, cr_id)] = cred_info['attrs']\n continue\n if ({attr: str(filt[attr]) for attr in filt}.items() <= cred_info['attrs'].items()):\n rv[(rr_id, cr_id)] = cred_info['attrs']\n\n return rv", "response": "Given a cred - brief cred - info or sequence of either return a dict mapping keys to values of interest."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nfetches revealed attributes from input proof and return dict mapping cred - ids to dicts each mapping attribute names to raw values for processing in further creds downstream.", "response": "def revealed_attrs(proof: dict) -> dict:\n \"\"\"\n Fetch revealed attributes from input proof and return dict mapping credential definition identifiers\n to dicts, each dict mapping attribute names to (raw) values, for processing in further creds downstream.\n\n :param proof: indy-sdk proof as dict\n :return: dict mapping cred-ids to dicts, each mapping revealed attribute names to (raw) values\n \"\"\"\n\n rv = {}\n for sub_index in range(len(proof['identifiers'])):\n cd_id = proof['identifiers'][sub_index]['cred_def_id']\n rv[cd_id] = ({ # uses von_anchor convention for uuid (referent) construction: will break on foreign anchor's\n '_'.join(uuid.split('_')[1:-1]): proof['requested_proof']['revealed_attrs'][uuid]['raw']\n for uuid in proof['requested_proof']['revealed_attrs']\n if proof['requested_proof']['revealed_attrs'][uuid]['sub_proof_index'] == sub_index})\n\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef config(self, value: dict) -> None:\n\n self._config = value or {}\n validate_config('verifier', self._config)", "response": "Set the configuration dict."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def _build_rr_state_json(self, rr_id: str, timestamp: int) -> (str, int):\n\n LOGGER.debug('_Verifier._build_rr_state_json >>> rr_id: %s, timestamp: %s', rr_id, timestamp)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Verifier._build_rr_state_json str:\n \"\"\"\n Build and return indy-sdk proof request for input attributes and non-revocation intervals by cred def id.\n\n :param cd_id2spec: dict mapping cred def ids to:\n\n - (optionally) 'attrs': lists of names of attributes of interest (omit for all, empty list or None for none)\n - (optionally) '>=': (pred) inclusive int lower-bounds of interest (omit, empty list, or None for none)\n - (optionally) '>': (pred) exclusive int lower-bounds of interest (omit, empty list, or None for none)\n - (optionally) '<=': (pred) inclusive int upper-bounds of interest (omit, empty list, or None for none)\n - (optionally) '<': (pred) exclusive int upper-bounds of interest (omit, empty list, or None for none)\n - (optionally), 'interval': either\n - (2-tuple) pair of epoch second counts marking 'from' and 'to' timestamps, or\n - | single epoch second count to set 'from' and 'to' the same; default\n | (now, now) for cred defs supporting revocation or None otherwise; e.g.,\n\n ::\n\n {\n 'Vx4E82R17q...:3:CL:16:tag': {\n 'attrs': [ # request attrs 'name' and 'favouriteDrink' from this cred def's schema\n 'name',\n 'favouriteDrink'\n ],\n '>=': { # request predicate score>=80 from this cred def\n 'score': 80\n }\n '<=': { # request ranking <=10 from this cred def\n 'ranking': 10\n }\n 'interval': 1528116008 # same instant for all attrs and preds of corresponding schema\n },\n 'R17v42T4pk...:3:CL:19:tag': None, # request all attrs, no preds, default intervals on all attrs\n 'e3vc5K168n...:3:CL:23:tag': {}, # request all attrs, no preds, default intervals on all attrs\n 'Z9ccax812j...:3:CL:27:tag': { # request all attrs, no preds, this interval on all attrs\n 'interval': (1528112408, 1528116008)\n },\n '9cHbp54C8n...:3:CL:37:tag': { # request no attrs and some predicates; specify interval\n 'attrs': [], # or equivalently, 'attrs': None\n '>=': {\n 'employees': '50' # nicety: implementation converts to int for caller\n },\n '>=': {\n 'revenue': '10000000' # nicety: implementation converts to int for caller\n 'ebidta': 0\n }\n 'interval': (1528029608, 1528116008)\n },\n '6caBcmLi33...:3:CL:41:tag': { # all attrs, one pred, default intervals to now on attrs & pred\n '>': {\n 'regEpoch': 1514782800\n }\n },\n ...\n }\n\n :return: indy-sdk proof request json\n \"\"\"\n\n LOGGER.debug('Verifier.build_proof_req_json >>> cd_id2spec: %s', cd_id2spec)\n\n cd_id2schema = {}\n now = int(time())\n rv = {\n 'nonce': str(int(time())),\n 'name': 'proof_req',\n 'version': '0.0',\n 'requested_attributes': {},\n 'requested_predicates': {}\n }\n\n for cd_id in cd_id2spec:\n if not ok_cred_def_id(cd_id):\n LOGGER.debug('Verifier.build_proof_req_json int:\n\n LOGGER.debug('Verifier.load_cache_for_verification >>> archive: %s', archive)\n\n rv = int(time())\n for s_id in self.config.get('archive-verifier-caches-on-close', {}).get('schema_id', {}):\n if ok_schema_id(s_id):\n with SCHEMA_CACHE.lock:\n await self.get_schema(s_id)\n else:\n LOGGER.info('Not archiving schema for specified bad id %s', s_id)\n for cd_id in self.config.get('archive-verifier-caches-on-close', {}).get('cred_def_id', {}):\n if ok_cred_def_id(cd_id):\n with CRED_DEF_CACHE.lock:\n await self.get_cred_def(cd_id)\n else:\n LOGGER.info('Not archiving cred def for specified bad id %s', cd_id)\n for rr_id in self.config.get('archive-verifier-caches-on-close', {}).get('rev_reg_id', {}):\n if ok_rev_reg_id(rr_id):\n await self.get_rev_reg_def(rr_id)\n with REVO_CACHE.lock:\n revo_cache_entry = REVO_CACHE.get(rr_id, None)\n if revo_cache_entry:\n try:\n await revo_cache_entry.get_state_json(self._build_rr_state_json, rv, rv)\n except ClosedPool:\n LOGGER.warning(\n 'Verifier %s is offline from pool %s, cannot update revo cache reg state for %s to %s',\n self.name,\n self.pool.name,\n rr_id,\n rv)\n except AbsentPool:\n LOGGER.warning(\n 'Verifier %s has no pool, cannot update revo cache reg state for %s to %s',\n self.name,\n rr_id,\n rv)\n else:\n LOGGER.info('Not archiving rev reg for specified bad id %s', rr_id)\n\n if archive:\n ArchivableCaches.archive(self.dir_cache)\n LOGGER.debug('Verifier.load_cache_for_verification <<< %s', rv)\n return rv", "response": "Load schema cred def revocation cache and revocation registry for verification."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nopening current object and parse cache from archive if parse - caches - on - open is set to True.", "response": "async def open(self) -> 'Verifier':\n \"\"\"\n Explicit entry. Perform ancestor opening operations,\n then parse cache from archive if so configured, and\n synchronize revocation registry to tails tree content.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('Verifier.open >>>')\n\n await super().open()\n if self.config.get('parse-caches-on-open', False):\n ArchivableCaches.parse(self.dir_cache)\n\n LOGGER.debug('Verifier.open <<<')\n return self"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nclose the current object.", "response": "async def close(self) -> None:\n \"\"\"\n Explicit exit. If so configured, populate cache to prove for any creds on schemata,\n cred defs, and rev regs marked of interest in configuration at initialization,\n archive cache, and purge prior cache archives.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('Verifier.close >>>')\n\n if self.config.get('archive-verifier-caches-on-close', {}):\n await self.load_cache_for_verification(True)\n ArchivableCaches.purge_archives(self.dir_cache, True)\n\n await BaseAnchor.close(self)\n\n LOGGER.debug('Verifier.close <<<')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check_encoding(proof_req: dict, proof: dict) -> bool:\n\n LOGGER.debug('Verifier.check_encoding <<< proof_req: %s, proof: %s', proof_req, proof)\n\n cd_id2proof_id = {} # invert proof['identifiers'] per cd_id\n p_preds = {} # cd_id and attr to bound\n for idx in range(len(proof['identifiers'])):\n cd_id = proof['identifiers'][idx]['cred_def_id']\n cd_id2proof_id[cd_id] = idx # since at most 1 cred per cred def\n p_preds[cd_id] = {\n ge_proof['predicate']['attr_name']: ge_proof['predicate']['value']\n for ge_proof in proof['proof']['proofs'][idx]['primary_proof']['ge_proofs']\n }\n\n for (uuid, req_attr) in proof_req['requested_attributes'].items(): # proof req xref proof per revealed attr\n canon_attr = canon(req_attr['name'])\n proof_ident_idx = cd_id2proof_id[req_attr['restrictions'][0]['cred_def_id']]\n enco = proof['proof']['proofs'][proof_ident_idx]['primary_proof']['eq_proof']['revealed_attrs'].get(\n canon_attr)\n if not enco:\n continue # requested but declined from revelation in proof: must appear in a predicate\n if enco != proof['requested_proof']['revealed_attrs'][uuid]['encoded']:\n LOGGER.debug('Verifier.check_proof_encoding <<< False')\n return False\n if enco != encode(proof['requested_proof']['revealed_attrs'][uuid]['raw']):\n LOGGER.debug('Verifier.check_proof_encoding <<< False')\n return False\n\n for (uuid, req_pred) in proof_req['requested_predicates'].items(): # proof req xref proof per pred\n canon_attr = canon(req_pred['name'])\n if p_preds[req_pred['restrictions'][0]['cred_def_id']].get(canon_attr) != req_pred['p_value']:\n LOGGER.debug('Verifier.check_proof_encoding <<< False')\n return False\n\n LOGGER.debug('Verifier.check_proof_encoding <<< True')\n return True", "response": "Check if the raw values of the encodings of the creddef entries in the proof are cross - referenced against their encodings."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(value: str) -> 'Protocol':\n\n for pktype in PublicKeyType:\n if value in (pktype.ver_type, pktype.authn_type):\n return pktype\n return None", "response": "Returns the protocol corresponding to input version value."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn dict representation of public key.", "response": "def to_dict(self):\n \"\"\"\n Return dict representation of public key to embed in DID document.\n \"\"\"\n\n return {\n 'id': self.id,\n 'type': str(self.type.ver_type),\n 'controller': canon_ref(self.did, self.controller),\n **self.type.specification(self.value)\n }"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def main(wallet_name: str) -> None:\n\n logging.basicConfig(level=logging.WARN, format='%(levelname)-8s | %(name)-12s | %(message)s')\n logging.getLogger('indy').setLevel(logging.ERROR)\n\n path_start = join(RevRegBuilder.dir_tails_sentinel(wallet_name), '.start')\n\n with open(path_start, 'r') as fh_start:\n start_data = json.loads(fh_start.read())\n remove(path_start)\n\n logging.getLogger(__name__).setLevel(start_data['logging']['level'])\n for path_log in start_data['logging']['paths']:\n logging.getLogger(__name__).addHandler(logging.FileHandler(path_log))\n\n wallet = WalletManager().get(\n {\n 'id': wallet_name,\n 'storage_type': start_data['wallet']['storage_type'],\n **start_data['wallet']['config'],\n },\n access=start_data['wallet']['access_creds'].get('key', None))\n async with wallet, RevRegBuilder(wallet, rrbx=True) as rrban:\n await rrban.serve()", "response": "Main function for revocation registry builder operating in external process on behalf of issuer agent."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _start_data_json(self) -> str:\n\n rv = {\n 'logging': {\n 'paths': []\n },\n 'wallet': {\n }\n }\n\n logger = LOGGER\n while not logger.level:\n logger = logger.parent\n if logger is None:\n break\n rv['logging']['level'] = logger.level\n\n logger = LOGGER\n log_paths = [realpath(h.baseFilename) for h in logger.handlers if hasattr(h, 'baseFilename')]\n while not log_paths:\n logger = logger.parent\n if logger is None:\n break\n log_paths = [realpath(h.baseFilename) for h in logger.handlers if hasattr(h, 'baseFilename')]\n for log_path in log_paths:\n rv['logging']['paths'].append(log_path)\n\n rv['wallet']['storage_type'] = self.wallet.storage_type\n rv['wallet']['config'] = self.wallet.config\n rv['wallet']['access_creds'] = self.wallet.access_creds\n\n return json.dumps(rv)", "response": "Output json with start data to write for external revocation registry builder process pickup."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_state(wallet_name: str) -> _STATE:\n\n dir_sentinel = RevRegBuilder.dir_tails_sentinel(wallet_name)\n file_pid = join(dir_sentinel, '.pid')\n file_start = join(dir_sentinel, '.start')\n file_stop = join(dir_sentinel, '.stop')\n\n if isfile(file_stop):\n return _STATE.STOPPING\n\n if isfile(file_start) or isfile(file_pid):\n return _STATE.RUNNING\n\n return _STATE.ABSENT", "response": "Return current state of revocation registry builder process."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning top of tails tree for input revocation registry identifier.", "response": "def dir_tails_top(self, rr_id) -> str:\n \"\"\"\n Return top of tails tree for input rev reg id.\n\n :param rr_id: revocation registry identifier\n :return: top of tails tree\n \"\"\"\n\n return join(self.dir_tails_hopper, rr_id) if self.external else self.dir_tails"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef dir_tails_target(self, rr_id) -> str:\n\n return join(self.dir_tails_top(rr_id), rev_reg_id2cred_def_id(rr_id))", "response": "Return target directory for revocation registry and tails file production."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef mark_in_progress(self, rr_id: str, rr_size: int) -> None:\n try:\n makedirs(join(self._dir_tails_sentinel, rr_id), exist_ok=False)\n except FileExistsError:\n LOGGER.warning('Rev reg %s construction already in progress', rr_id)\n else:\n open(join(self._dir_tails_sentinel, rr_id, '.{}'.format(rr_size)), 'w').close()", "response": "Mark the revocation registry as in progress."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nserving the tails file.", "response": "async def serve(self) -> None:\n \"\"\"\n Write pidfile to sentinel directory if need be, and wait for sentinels\n to shut down or build revocation registry and tails file.\n \"\"\"\n\n LOGGER.debug('RevRegBuilder.serve >>>')\n\n assert self.external\n\n file_pid = join(self._dir_tails_sentinel, '.pid')\n if isfile(file_pid):\n with open(file_pid, 'r') as fh_pid:\n pid = int(fh_pid.read())\n try:\n kill(pid, 0)\n except ProcessLookupError:\n remove(file_pid)\n LOGGER.info('RevRegBuilder removed derelict .pid file')\n except PermissionError:\n LOGGER.info('RevRegBuilder process already running with pid %s: exiting', pid)\n LOGGER.debug('RevRegBuilder.serve <<<')\n return\n else:\n LOGGER.info('RevRegBuilder process already running with pid %s: exiting', pid)\n LOGGER.debug('RevRegBuilder.serve <<<')\n return\n\n pid = getpid()\n with open(file_pid, 'w') as pid_fh:\n print(str(pid), file=pid_fh)\n\n file_stop = join(self._dir_tails_sentinel, '.stop')\n\n while True:\n if isfile(file_stop): # stop now, pick up any pending tasks next invocation\n remove(file_stop)\n remove(file_pid)\n break\n\n p_pending = [join(self._dir_tails_sentinel, d) for d in listdir(self._dir_tails_sentinel)\n if isdir(join(self._dir_tails_sentinel, d))]\n p_pending = [p for p in p_pending if [s for s in listdir(p) if s.startswith('.')]] # size marker\n if p_pending:\n pdir = basename(p_pending[0])\n rr_id = pdir\n rr_size = int([s for s in listdir(p_pending[0]) if s.startswith('.')][0][1:])\n open(join(p_pending[0], '.in-progress'), 'w').close()\n await self.create_rev_reg(rr_id, rr_size or None)\n rmtree(p_pending[0])\n await asyncio.sleep(1)\n\n LOGGER.debug('RevRegBuilder.serve <<<')"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstop an external revocation registry builder.", "response": "async def stop(wallet_name: str) -> None:\n \"\"\"\n Gracefully stop an external revocation registry builder, waiting for its current.\n\n The indy-sdk toolkit uses a temporary directory for tails file mustration,\n and shutting down the toolkit removes the directory, crashing the external\n tails file write. This method allows a graceful stop to wait for completion\n of such tasks already in progress.\n\n :wallet_name: name external revocation registry builder to check\n :return: whether a task is pending.\n \"\"\"\n\n LOGGER.debug('RevRegBuilder.stop >>>')\n\n dir_sentinel = join(RevRegBuilder.dir_tails_sentinel(wallet_name))\n\n if isdir(dir_sentinel):\n open(join(dir_sentinel, '.stop'), 'w').close() # touch\n\n while any(isfile(join(dir_sentinel, d, '.in-progress')) for d in listdir(dir_sentinel)):\n await asyncio.sleep(1)\n\n LOGGER.debug('RevRegBuilder.stop <<<')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates revocation registry artifacts and new tails file.", "response": "async def create_rev_reg(self, rr_id: str, rr_size: int = None) -> None:\n \"\"\"\n Create revocation registry artifacts and new tails file (with association to\n corresponding revocation registry identifier via symbolic link name)\n for input revocation registry identifier. Symbolic link presence signals completion.\n If revocation registry builder operates in a process external to its Issuer's,\n target directory is hopper directory.\n\n Raise WalletState for closed wallet.\n\n :param rr_id: revocation registry identifier\n :param rr_size: revocation registry size (defaults to 64)\n \"\"\"\n\n LOGGER.debug('RevRegBuilder.create_rev_reg >>> rr_id: %s, rr_size: %s', rr_id, rr_size)\n\n if not self.wallet.handle:\n LOGGER.debug('RevRegBuilder.create_rev_reg str:\n\n if elide_to is not None:\n elide_to = max(elide_to, 3) # make room for ellipses '...'\n try:\n rv = json.dumps(json.loads(dumpit) if isinstance(dumpit, str) else dumpit, indent=4)\n except TypeError:\n rv = '{}'.format(pformat(dumpit, indent=4, width=120))\n return rv if elide_to is None or len(rv) <= elide_to else '{}...'.format(rv[0 : elide_to - 3])", "response": "Pretty - print a node s hierarchy in json format."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nperform a synchronous operation ; await then return the result.", "response": "def do_wait(coro: Callable) -> Any:\n \"\"\"\n Perform aynchronous operation; await then return the result.\n\n :param coro: coroutine to await\n :return: coroutine result\n \"\"\"\n\n event_loop = None\n try:\n event_loop = asyncio.get_event_loop()\n except RuntimeError:\n event_loop = asyncio.new_event_loop()\n asyncio.set_event_loop(event_loop)\n return event_loop.run_until_complete(coro)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntake one or more ini files and return a dict with configuration from all. ini files.", "response": "def inis2dict(ini_paths: Union[str, Sequence[str]]) -> dict:\n \"\"\"\n Take one or more ini files and return a dict with configuration from all,\n interpolating bash-style variables ${VAR} or ${VAR:-DEFAULT}.\n\n :param ini_paths: path or paths to .ini files\n \"\"\"\n\n var_dflt = r'\\${(.*?):-(.*?)}'\n def _interpolate(content):\n rv = expandvars(content)\n while True:\n match = re.search(var_dflt, rv)\n if match is None:\n break\n bash_var = '${{{}}}'.format(match.group(1))\n value = expandvars(bash_var)\n rv = re.sub(var_dflt, match.group(2) if value == bash_var else value, rv, count=1)\n\n return rv\n\n parser = ConfigParser()\n\n for ini in [ini_paths] if isinstance(ini_paths, str) else ini_paths:\n if not isfile(ini):\n raise FileNotFoundError('No such file: {}'.format(ini))\n with open(ini, 'r') as ini_fh:\n ini_text = _interpolate(ini_fh.read())\n parser.read_string(ini_text)\n\n return {s: dict(parser[s].items()) for s in parser.sections()}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mark(self, digits: int = None) -> float:\n\n self._mark[:] = [self._mark[1], time()]\n rv = self._mark[1] - self._mark[0]\n\n if digits is not None and digits > 0:\n rv = round(rv, digits)\n elif digits == 0 or self._digits == 0:\n rv = int(rv)\n elif self._digits is not None and self._digits > 0:\n rv = round(rv, self._digits)\n\n return rv", "response": "Return time in seconds since last mark reset or construction."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nbegin the search operation.", "response": "async def open(self) -> None:\n \"\"\"\n Begin the search operation.\n \"\"\"\n\n LOGGER.debug('StorageRecordSearch.open >>>')\n\n if self.opened:\n LOGGER.debug('StorageRecordSearch.open str:\n \"\"\"\n Return identifier for archivable caches, computing it first and retaining it if need be.\n Raise AbsentPool if ledger configuration is not yet available.\n\n :param name: pool name\n :return: archivable cache identifier\n \"\"\"\n\n if self._cache_id:\n return self._cache_id\n\n with open(join(expanduser('~'), '.indy_client', 'pool', self.name, '{}.txn'.format(self.name))) as fh_genesis:\n genesis = [json.loads(line) for line in fh_genesis.readlines() if line]\n\n hps = []\n for gen_txn in genesis:\n hps.append(self.protocol.genesis_host_port(gen_txn))\n hps.sort() # canonicalize to make order irrelevant\n self._cache_id = ':'.join('{}:{}'.format(hp[0], hp[1]) for hp in hps)\n\n return self._cache_id"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nopen pool with given configuration.", "response": "async def open(self) -> 'NodePool':\n \"\"\"\n Explicit entry. Opens pool as configured, for later closure via close().\n Creates pool if it does not yet exist, using configured genesis transaction file.\n For use when keeping pool open across multiple calls.\n\n Raise any AbsentPool if node pool ledger configuration is not available.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('NodePool.open >>>')\n\n await pool.set_protocol_version(self.protocol.indy())\n LOGGER.info('Pool ledger %s set protocol %s', self.name, self.protocol)\n\n try:\n self._handle = await pool.open_pool_ledger(self.name, json.dumps(self.config))\n except IndyError as x_indy:\n if x_indy.error_code == ErrorCode.PoolLedgerNotCreatedError:\n LOGGER.debug('NodePool.open None:\n\n LOGGER.debug('NodePool.close >>>')\n\n if not self.handle:\n LOGGER.warning('Abstaining from closing pool %s: already closed', self.name)\n else:\n await pool.close_pool_ledger(self.handle)\n self._handle = None\n\n LOGGER.debug('NodePool.close <<<')", "response": "Explicit exit. Closes pool. For use when keeping pool open across multiple calls."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrefreshes local copy of pool ledger and update node pool connections.", "response": "async def refresh(self) -> None:\n \"\"\"\n Refresh local copy of pool ledger and update node pool connections.\n \"\"\"\n\n LOGGER.debug('NodePool.refresh >>>')\n\n await pool.refresh_pool_ledger(self.handle)\n\n LOGGER.debug('NodePool.refresh <<<')"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\nasync def reseed(self, seed: str = None) -> None:\n\n LOGGER.debug('BaseAnchor.reseed >>> seed: [SEED]')\n\n verkey = await self.wallet.reseed_init(seed)\n req_json = await ledger.build_nym_request(\n self.did,\n self.did,\n verkey,\n self.name,\n (await self.get_nym_role()).token())\n await self._sign_submit(req_json)\n await self.wallet.reseed_apply()\n\n LOGGER.debug('BaseAnchor.reseed <<<')", "response": "Reseed the current key pair."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget json cryptonym for input ( anchor ) DID.", "response": "async def get_nym(self, target_did: str = None) -> str:\n \"\"\"\n Get json cryptonym (including current verification key) for input (anchor) DID from ledger.\n Return empty production {} if the ledger has no such cryptonym.\n\n Raise BadLedgerTxn on failure. Raise WalletState if target DID is default (own DID) value but\n wallet does not have it (neither created nor opened since initialization).\n\n :param target_did: DID of cryptonym to fetch (default own DID)\n :return: cryptonym json\n \"\"\"\n\n LOGGER.debug('BaseAnchor.get_nym >>> target_did: %s', target_did)\n\n if target_did and not ok_did(target_did):\n LOGGER.debug('BaseAnchor.get_nym Role:\n\n LOGGER.debug('BaseAnchor.get_nym_role >>> target_did: %s', target_did)\n\n nym = json.loads(await self.get_nym(target_did))\n if not nym:\n LOGGER.debug('BaseAnchor.get_nym_role Role:\n\n LOGGER.debug('BaseAnchor.least_role >>>')\n\n rv = Role.TRUST_ANCHOR\n\n LOGGER.debug('BaseAnchor.least_role <<< %s', rv)\n return rv", "response": "Return the indy - sdk role of the least privilege for an anchor class in building\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\nasync def set_did_endpoint(self, remote_did: str, did_endpoint: str) -> EndpointInfo:\n\n LOGGER.debug('BaseAnchor.set_did_endpoint >>> remote_did: %s, did_endpoint: %s', remote_did, did_endpoint)\n\n if not ok_did(remote_did):\n LOGGER.debug('BaseAnchor.set_did_endpoint None:\n\n LOGGER.debug('BaseAnchor.send_endpoint >>> endpoint: %s', endpoint)\n\n ledger_endpoint = await self.get_endpoint()\n if ledger_endpoint == endpoint:\n LOGGER.info('%s endpoint already set as %s', self.name, endpoint)\n LOGGER.debug('BaseAnchor.send_endpoint <<< (%s already set for %s )')\n return\n\n attr_json = json.dumps({\n 'endpoint': {\n 'endpoint': endpoint # indy-sdk likes 'ha' here but won't map 'ha' to a URL, only ip:port\n }\n })\n req_json = await ledger.build_attrib_request(self.did, self.did, None, attr_json, None)\n await self._sign_submit(req_json)\n\n for _ in range(16): # reasonable timeout\n if await self.get_endpoint(None, False) == endpoint:\n break\n await asyncio.sleep(1)\n LOGGER.info('Sent endpoint %s to ledger, waiting 1s for its confirmation', endpoint)\n else:\n LOGGER.debug('BaseAnchor.send_endpoint str:\n\n LOGGER.debug('BaseAnchor.get_endpoint >>> target_did: %s, from_cache: %s', target_did, from_cache)\n\n rv = None\n\n if not (target_did or self.did):\n LOGGER.debug('BaseAnchor.get_endpoint str:\n \"\"\"\n Given a DID, retrieve its verification key, looking in wallet, then pool.\n Given a verification key or None, return input.\n\n Raise WalletState if the wallet is closed. Given a recipient DID not in the wallet,\n raise AbsentPool if the instance has no pool or ClosedPool if its pool is closed.\n If no such verification key is on the ledger, raise AbsentNym.\n\n :param target: verification key, or DID to resolve to such\n :return: verification key\n \"\"\"\n\n LOGGER.debug('BaseAnchor._verkey_for >>> target: %s', target)\n\n rv = target\n if rv is None or not ok_did(rv): # it's None or already a verification key\n LOGGER.debug('BaseAnchor._verkey_for <<< %s', rv)\n return rv\n\n if self.wallet.handle:\n try:\n rv = await did.key_for_local_did(self.wallet.handle, target)\n LOGGER.info('Anchor %s got verkey for DID %s from wallet', self.name, target)\n LOGGER.debug('BaseAnchor._verkey_for <<< %s', rv)\n return rv\n except IndyError as x_indy:\n if x_indy.error_code != ErrorCode.WalletItemNotFound: # on not found, try the pool\n LOGGER.debug(\n 'BaseAnchor._verkey_for bytes:\n \"\"\"\n Encrypt plaintext for owner of DID or verification key, anonymously or via\n authenticated encryption scheme. If given DID, first check wallet and then pool\n for corresponding verification key.\n\n Raise WalletState if the wallet is closed. Given a recipient DID not in the wallet,\n raise AbsentPool if the instance has no pool or ClosedPool if its pool is closed.\n\n :param message: plaintext, as bytes\n :param authn: whether to use authenticated encryption scheme\n :param recip: DID or verification key of recipient, None for anchor's own\n :return: ciphertext, as bytes\n \"\"\"\n\n LOGGER.debug('BaseAnchor.encrypt >>> message: %s, authn: %s, recip: %s', message, authn, recip)\n\n if not self.wallet.handle:\n LOGGER.debug('BaseAnchor.encrypt bool:\n\n LOGGER.debug('BaseAnchor.verify >>> signer: %s, message: %s, signature: %s', signer, message, signature)\n\n if not self.wallet.handle:\n LOGGER.debug('BaseAnchor.verify str:\n \"\"\"\n Find a transaction on the distributed ledger by its sequence number.\n\n :param seq_no: transaction number\n :return: json sequence number of transaction, null for no match\n \"\"\"\n\n LOGGER.debug('BaseAnchor.get_txn >>> seq_no: %s', seq_no)\n\n rv_json = json.dumps({})\n req_json = await ledger.build_get_txn_request(self.did, None, seq_no)\n resp = json.loads(await self._submit(req_json))\n\n rv_json = self.pool.protocol.txn2data(resp)\n\n LOGGER.debug('BaseAnchor.get_txn <<< %s', rv_json)\n return rv_json"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef to_dict(self):\n\n rv = {\n 'id': self.id,\n 'type': self.type,\n 'priority': self.priority\n }\n if self.recip_keys:\n rv['routingKeys'] = [canon_ref(k.did, k.id, '#') for k in self.recip_keys]\n if self.routing_keys:\n rv['routingKeys'] = [canon_ref(k.did, k.id, '#') for k in self.routing_keys]\n rv['serviceEndpoint'] = self.endpoint\n\n return rv", "response": "Return dict representation of a resource in DID document."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsynchronize revocation registry identifier with tails file.", "response": "async def _sync_revoc_for_proof(self, rr_id: str) -> None:\n \"\"\"\n Pick up tails file reader handle for input revocation registry identifier. If no symbolic\n link is present, get the revocation registry definition to retrieve its tails file hash,\n then find the tails file and link it.\n\n Raise AbsentTails for missing corresponding tails file.\n\n :param rr_id: revocation registry identifier\n \"\"\"\n\n LOGGER.debug('HolderProver._sync_revoc_for_proof >>> rr_id: %s', rr_id)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('HolderProver._sync_revoc_for_proof str:\n\n LOGGER.debug('HolderProver.dir_tails >>>')\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('HolderProver.dir_tails 'HolderProver':\n\n LOGGER.debug('HolderProver.open >>>')\n\n await super().open()\n if self.config.get('parse-caches-on-open', False):\n ArchivableCaches.parse(self.dir_cache)\n\n for path_rr_id in Tails.links(self._dir_tails):\n await self._sync_revoc_for_proof(basename(path_rr_id))\n\n LOGGER.debug('HolderProver.open <<<')\n return self", "response": "Explicit entry. Perform ancestor opening operations,\n then parse cache from archive if so configured, and\n synchronize revocation registry to tails tree content.\n\n :return: current object"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def close(self) -> None:\n\n LOGGER.debug('HolderProver.close >>>')\n\n if self.config.get('archive-holder-prover-caches-on-close', False):\n await self.load_cache_for_proof(True)\n ArchivableCaches.purge_archives(self.dir_cache, True)\n\n await BaseAnchor.close(self)\n for path_rr_id in Tails.links(self._dir_tails):\n rr_id = basename(path_rr_id)\n try:\n await self._sync_revoc_for_proof(rr_id)\n except ClosedPool:\n LOGGER.warning('HolderProver sync-revoc on close required ledger for %s but pool was closed', rr_id)\n\n LOGGER.debug('HolderProver.close <<<')", "response": "Close the HolderProver instance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def rev_regs(self) -> list:\n\n LOGGER.debug('HolderProver.rev_regs >>>')\n\n for path_rr_id in Tails.links(self._dir_tails):\n await self._sync_revoc_for_proof(basename(path_rr_id))\n\n rv = [basename(f) for f in Tails.links(self._dir_tails)]\n LOGGER.debug('HolderProver.rev_regs <<< %s', rv)\n return rv", "response": "Return list of revocation registry identifiers for which HolderProver has associated tails files."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def offline_intervals(self, cd_ids: Union[str, Sequence[str]]) -> dict:\n\n LOGGER.debug('HolderProver.offline_intervals >>> cd_ids: %s', cd_ids)\n\n rv = {}\n for cd_id in [cd_ids] if isinstance(cd_ids, str) else cd_ids:\n if not ok_cred_def_id(cd_id):\n LOGGER.debug('HolderProver.offline_intervals None:\n \"\"\"\n Create link secret (a.k.a. master secret) used in proofs by HolderProver, if the\n current link secret does not already correspond to the input link secret label.\n\n Raise WalletState if wallet is closed, or any other IndyError causing failure\n to set link secret in wallet.\n\n :param label: label for link secret; indy-sdk uses label to generate link secret\n \"\"\"\n\n LOGGER.debug('HolderProver.create_link_secret >>> label: %s', label)\n\n await self.wallet.create_link_secret(label)\n\n LOGGER.debug('HolderProver.create_link_secret <<<')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstore credential in wallet as HolderProver.", "response": "async def store_cred(self, cred_json: str, cred_req_metadata_json: str) -> str:\n \"\"\"\n Store cred in wallet as HolderProver, return its credential identifier as created in wallet.\n\n Raise AbsentTails if tails file not available for revocation registry for input credential.\n Raise WalletState if wallet is closed.\n\n :param cred_json: credential json as HolderProver created\n :param cred_req_metadata_json: credential request metadata json as HolderProver created via create_cred_req()\n :return: credential identifier within wallet\n \"\"\"\n\n LOGGER.debug(\n 'HolderProver.store_cred >>> cred_json: %s, cred_req_metadata_json: %s',\n cred_json,\n cred_req_metadata_json)\n\n if not self.wallet.handle:\n LOGGER.debug('HolderProver.store_cred int:\n\n LOGGER.debug('HolderProver.load_cache_for_proof >>> archive: %s', archive)\n\n rv = int(time())\n box_ids = json.loads(await self.get_box_ids_held())\n for s_id in box_ids['schema_id']:\n with SCHEMA_CACHE.lock:\n await self.get_schema(s_id)\n for cd_id in box_ids['cred_def_id']:\n with CRED_DEF_CACHE.lock:\n await self.get_cred_def(cd_id)\n for rr_id in box_ids['rev_reg_id']:\n await self.get_rev_reg_def(rr_id)\n with REVO_CACHE.lock:\n revo_cache_entry = REVO_CACHE.get(rr_id, None)\n if revo_cache_entry:\n try:\n await revo_cache_entry.get_delta_json(self._build_rr_delta_json, rv, rv)\n except ClosedPool:\n LOGGER.warning(\n 'HolderProver %s is offline from pool %s, cannot update revo cache reg delta for %s to %s',\n self.name,\n self.pool.name,\n rr_id,\n rv)\n except AbsentPool:\n LOGGER.warning(\n 'HolderProver %s has no pool, cannot update revo cache reg delta for %s to %s',\n self.name,\n rr_id,\n rv)\n\n if archive:\n ArchivableCaches.archive(self.dir_cache)\n LOGGER.debug('HolderProver.load_cache_for_proof <<< %s', rv)\n return rv", "response": "Load schema cred def revocation cache and revocation cache delta for proof on all credentials in wallet."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def get_box_ids_held(self) -> str:\n\n LOGGER.debug('HolderProver.get_box_ids_held >>>')\n\n if not self.wallet.handle:\n LOGGER.debug('HolderProver.get_box_ids_held str:\n\n LOGGER.debug('HolderProver.get_cred_infos_by_q >>> query_json: %s, limit: %s', query_json, limit)\n\n if not self.wallet.handle:\n LOGGER.debug('HolderProver.get_cred_infos_by_q str:\n\n LOGGER.debug('HolderProver.get_cred_infos_by_filter >>> filt: %s', filt)\n\n if not self.wallet.handle:\n LOGGER.debug('HolderProver.get_cred_infos_by_filter str:\n\n LOGGER.debug(\n 'HolderProver.create_proof >>> proof_req: %s, briefs: %s, requested_creds: %s',\n proof_req,\n briefs,\n requested_creds)\n\n if not self.wallet.handle:\n LOGGER.debug('HolderProver.create_proof int(time()):\n LOGGER.debug(\n 'HolderProver.create_proof Wallet:\n\n LOGGER.debug('HolderProver.reset_wallet >>>')\n\n self.wallet = await WalletManager().reset(self.wallet, seed)\n\n rv = self.wallet\n LOGGER.debug('HolderProver.reset_wallet <<< %s', rv)\n return rv", "response": "Close and delete HolderProver wallet, then create and open a replacement on prior link secret.\n Note that this operation effectively destroys private keys for credential definitions. Its\n intended use is primarily for testing and demonstration.\n\n Raise AbsentLinkSecret if link secret not set. Raise WalletState if the wallet is closed.\n\n :param seed: seed to use for new wallet (default random)\n :return: replacement wallet"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef my_resolve_lookup(self, context):\n current = context\n try: # catch-all for silent variable failures\n for bit in self.lookups:\n try: # dictionary lookup\n current = current[bit]\n # ValueError/IndexError are for numpy.array lookup on\n # numpy < 1.9 and 1.9+ respectively\n except (TypeError, AttributeError, KeyError, ValueError, IndexError):\n try: # attribute lookup\n # Don\"t return class attributes if the class is the context:\n if isinstance(current, BaseContext) and getattr(type(current), bit):\n raise AttributeError\n current = getattr(current, bit)\n except (TypeError, AttributeError) as e:\n # Reraise an AttributeError raised by a @property\n if (isinstance(e, AttributeError) and\n not isinstance(current, BaseContext) and bit in dir(current)):\n raise\n try: # list-index lookup\n current = current[int(bit)]\n except (IndexError, # list index out of range\n ValueError, # invalid literal for int()\n KeyError, # current is a dict without `int(bit)` key\n TypeError): # unsubscriptable object\n raise VariableDoesNotExist(\"Failed lookup for key \"\n \"[%s] in %r\",\n (bit, current)) # missing attribute\n if callable(current):\n if getattr(current, \"do_not_call_in_templates\", False):\n pass\n elif getattr(current, \"alters_data\", False):\n try:\n current = context.template.engine.string_if_invalid\n except AttributeError:\n current = settings.TEMPLATE_STRING_IF_INVALID\n else:\n try: # method call (assuming no args required)\n current = current()\n except TypeError:\n try:\n inspect.getcallargs(current)\n except TypeError: # arguments *were* required\n current = context.template.engine.string_if_invalid # invalid method call\n else:\n raise\n elif isinstance(current, Model):\n if (\"request\" in context) and hasattr(context[\"request\"], \"_ultracache\"):\n # get_for_model itself is cached\n ct = ContentType.objects.get_for_model(current.__class__)\n context[\"request\"]._ultracache.append((ct.id, current.pk))\n\n except Exception as e:\n template_name = getattr(context, \"template_name\", None) or \"unknown\"\n if logger is not None:\n logger.debug(\n \"Exception while resolving variable \\\"%s\\\" in template \\\"%s\\\".\",\n bit,\n template_name,\n exc_info=True,\n )\n\n if getattr(e, \"silent_variable_failure\", False):\n current = context.template.engine.string_if_invalid\n else:\n raise\n\n return current", "response": "This method is called by the lookup methods to resolve a real variable against the given context."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef index(self) -> dict:\n\n LOGGER.debug('SchemaCache.index >>>')\n\n rv = self._seq_no2schema_key\n LOGGER.debug('SchemaCache.index <<< %s', rv)\n return rv", "response": "Return a dictionary mapping content sequence numbers to schema keys."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef schema_key_for(self, seq_no: int) -> SchemaKey:\n\n LOGGER.debug('SchemaCache.schema_key_for >>> seq_no: %s', seq_no)\n\n rv = self._seq_no2schema_key.get(seq_no, None)\n\n LOGGER.debug('SchemaCache.schema_key_for <<< %s', rv)\n return rv", "response": "Return schema key for given sequence number."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef schemata(self) -> list:\n\n LOGGER.debug('SchemaCache.schemata >>>')\n\n LOGGER.debug('SchemaCache.schemata <<<')\n return [self._schema_key2schema[seq_no] for seq_no in self._schema_key2schema]", "response": "Return list with schemata in cache."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def _get_update(self, rr_builder: Callable, fro: int, to: int, delta: bool) -> (str, int):\n\n LOGGER.debug(\n 'RevoCacheEntry.get_update >>> rr_builder: %s, fro: %s, to: %s, delta: %s',\n rr_builder.__name__,\n fro,\n to,\n delta)\n\n if fro > to:\n (fro, to) = (to, fro)\n\n now = int(time())\n if to > now:\n LOGGER.debug(\n 'RevoCacheEntry._get_update %s)',\n 'delta' if delta else 'state',\n to,\n now)\n raise BadRevStateTime('Cannot query a rev reg {} in the future ({} > {})'.format(\n 'delta' if delta else 'state',\n to,\n now))\n\n cache_frame = None\n rr_update_json = None\n rr_frames = self.rr_delta_frames if delta else self.rr_state_frames\n\n frames = [frame for frame in rr_frames if frame.timestamp <= to <= frame.to]\n if frames:\n cache_frame = max(frames, key=lambda f: f.timestamp) # should be unique in any case\n # do not update frame.to, it's already past asked-for 'to'\n else:\n frames = [frame for frame in rr_frames if (fro <= frame.to and frame.timestamp <= to)]\n if frames:\n cache_frame = max(frames, key=lambda f: f.timestamp)\n # do not update frame.to - another update might occur, but we don't care; fro < frame.to, good enough\n if not frames:\n frames = [frame for frame in rr_frames if frame.timestamp < to] # frame.to < to since not frames coming in\n if frames:\n latest_cached = max(frames, key=lambda frame: frame.timestamp)\n if delta:\n (rr_update_json, timestamp) = await rr_builder(\n self.rev_reg_def['id'],\n to=to,\n fro=latest_cached.timestamp,\n fro_delta=latest_cached.rr_update)\n else:\n (rr_update_json, timestamp) = await rr_builder(self.rev_reg_def['id'], to)\n if timestamp == latest_cached.timestamp:\n latest_cached.to = to # this timestamp now known good through more recent 'to'\n cache_frame = latest_cached\n else:\n (rr_update_json, timestamp) = await rr_builder(self.rev_reg_def['id'], to)\n\n if cache_frame is None:\n cache_frame = RevRegUpdateFrame(to, timestamp, json.loads(rr_update_json)) # sets qtime to now\n rr_frames.append(cache_frame)\n self.cull(delta)\n else:\n cache_frame.qtime = int(time())\n\n rv = (json.dumps(cache_frame.rr_update), cache_frame.timestamp)\n LOGGER.debug('RevoCacheEntry._get_update <<< %s', rv)\n return rv", "response": "Get the update json and timestamp for a given delta and state."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def get_delta_json(\n self,\n rr_delta_builder: Callable[['HolderProver', str, int, int, dict], Awaitable[Tuple[str, int]]],\n fro: int,\n to: int) -> (str, int):\n \"\"\"\n Get rev reg delta json, and its timestamp on the distributed ledger,\n from cached rev reg delta frames list or distributed ledger,\n updating cache as necessary.\n\n Raise BadRevStateTime if caller asks for a delta to the future.\n\n On return of any previously existing rev reg delta frame, always update its query time beforehand.\n\n :param rr_delta_builder: callback to build rev reg delta if need be (specify anchor instance's\n _build_rr_delta())\n :param fro: least time (epoch seconds) of interest; lower-bounds 'to' on frame housing return data\n :param to: greatest time (epoch seconds) of interest; upper-bounds returned revocation delta timestamp\n :return: rev reg delta json and ledger timestamp (epoch seconds)\n \"\"\"\n\n LOGGER.debug(\n 'RevoCacheEntry.get_delta_json >>> rr_delta_builder: %s, fro: %s, to: %s',\n rr_delta_builder.__name__,\n fro,\n to)\n\n rv = await self._get_update(rr_delta_builder, fro, to, True)\n LOGGER.debug('RevoCacheEntry.get_delta_json <<< %s', rv)\n return rv", "response": "Get revocation delta json and its timestamp on the distributed ledger and return it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def get_state_json(\n self,\n rr_state_builder: Callable[['Verifier', str, int], Awaitable[Tuple[str, int]]],\n fro: int,\n to: int) -> (str, int):\n \"\"\"\n Get rev reg state json, and its timestamp on the distributed ledger,\n from cached rev reg state frames list or distributed ledger,\n updating cache as necessary.\n\n Raise BadRevStateTime if caller asks for a state in the future.\n\n On return of any previously existing rev reg state frame, always update its query time beforehand.\n\n :param rr_state_builder: callback to build rev reg state if need be (specify anchor instance's\n _build_rr_state())\n :param fro: least time (epoch seconds) of interest; lower-bounds 'to' on frame housing return data\n :param to: greatest time (epoch seconds) of interest; upper-bounds returned revocation state timestamp\n :return: rev reg state json and ledger timestamp (epoch seconds)\n \"\"\"\n\n LOGGER.debug(\n 'RevoCacheEntry.get_state_json >>> rr_state_builder: %s, fro: %s, to: %s',\n rr_state_builder.__name__,\n fro,\n to)\n\n rv = await self._get_update(rr_state_builder, fro, to, False)\n LOGGER.debug('RevoCacheEntry.get_state_json <<< %s', rv)\n return rv", "response": "Get revocation state json and its timestamp on the distributed ledger."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef clear() -> None:\n\n LOGGER.debug('clear >>>')\n\n with SCHEMA_CACHE.lock:\n SCHEMA_CACHE.clear()\n with CRED_DEF_CACHE.lock:\n CRED_DEF_CACHE.clear()\n with REVO_CACHE.lock:\n REVO_CACHE.clear()\n\n LOGGER.debug('clear <<<')", "response": "Clear all archivable caches in memory."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef archive(base_dir: str) -> int:\n\n LOGGER.debug('archive >>> base_dir: %s', base_dir)\n\n rv = int(time())\n timestamp_dir = join(base_dir, str(rv))\n makedirs(timestamp_dir, exist_ok=True)\n\n with SCHEMA_CACHE.lock:\n with open(join(timestamp_dir, 'schema'), 'w') as archive:\n print(json.dumps(SCHEMA_CACHE.schemata()), file=archive)\n\n with CRED_DEF_CACHE.lock:\n with open(join(timestamp_dir, 'cred_def'), 'w') as archive:\n print(json.dumps(CRED_DEF_CACHE), file=archive)\n\n with REVO_CACHE.lock:\n with open(join(timestamp_dir, 'revocation'), 'w') as archive:\n revo_cache_dict = {}\n for rr_id in REVO_CACHE:\n revo_cache_dict[rr_id] = {\n 'rev_reg_def': REVO_CACHE[rr_id].rev_reg_def,\n 'rr_delta_frames': [vars(f) for f in REVO_CACHE[rr_id].rr_delta_frames],\n 'rr_state_frames': [vars(f) for f in REVO_CACHE[rr_id].rr_state_frames]\n }\n print(json.dumps(revo_cache_dict), file=archive)\n\n LOGGER.debug('archive <<< %s', rv)\n return rv", "response": "Archive schema cred def revocation caches to disk as json."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npurges all cache archives.", "response": "def purge_archives(base_dir: str, retain_latest: bool = False) -> None:\n \"\"\"\n Erase all (or nearly all) cache archives.\n\n :param base_dir: archive base directory\n :param retain_latest: retain latest archive if present, purge all others\n \"\"\"\n\n LOGGER.debug('purge_archives >>> base_dir: %s, retain_latest: %s', base_dir, retain_latest)\n\n if isdir(base_dir):\n timestamps = sorted([int(t) for t in listdir(base_dir) if t.isdigit()])\n if retain_latest and timestamps:\n timestamps.pop()\n for timestamp in timestamps:\n timestamp_dir = join(base_dir, str(timestamp))\n rmtree(timestamp_dir)\n LOGGER.info('Purged archive cache directory %s', timestamp_dir)\n\n LOGGER.debug('purge_archives <<<')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef pairwise_info2tags(pairwise: PairwiseInfo) -> dict:\n\n rv = {\n canon_pairwise_tag(tag): raw(pairwise.metadata[tag]) for tag in pairwise.metadata or {}\n }\n rv['~their_did'] = pairwise.their_did\n rv['~their_verkey'] = pairwise.their_verkey\n rv['~my_did'] = pairwise.my_did\n rv['~my_verkey'] = pairwise.my_verkey\n\n if not StorageRecord.ok_tags(rv):\n raise BadRecord('Pairwise metadata {} must map strings to strings'.format(rv))\n\n return rv", "response": "Given a pairwise info with metadata mapping tags to values return corresponding\n indy - sdk non_secrets record tags dict to store same in wallet"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive indy - sdk non - secrets implementation of pairwise storage record dict return corresponding PairwiseInfo.", "response": "def storage_record2pairwise_info(storec: StorageRecord) -> PairwiseInfo:\n \"\"\"\n Given indy-sdk non_secrets implementation of pairwise storage record dict, return corresponding PairwiseInfo.\n\n :param storec: (non-secret) storage record to convert to PairwiseInfo\n :return: PairwiseInfo on record DIDs, verkeys, metadata\n \"\"\"\n\n return PairwiseInfo(\n storec.id, # = their did\n storec.value, # = their verkey\n storec.tags['~my_did'],\n storec.tags['~my_verkey'],\n {\n tag[tag.startswith('~'):]: storec.tags[tag] for tag in (storec.tags or {}) # strip any leading '~'\n })"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a configuration dict with indy and possibly more configuration values return the corresponding indy wallet configuration dict from current default and input values.", "response": "def _config2indy(self, config: dict) -> dict:\n \"\"\"\n Given a configuration dict with indy and possibly more configuration values, return the\n corresponding indy wallet configuration dict from current default and input values.\n\n :param config: input configuration\n :return: configuration dict for indy wallet\n \"\"\"\n\n assert {'name', 'id'} & {k for k in config}\n return {\n 'id': config.get('name', config.get('id')),\n 'storage_type': config.get('storage_type', self.default_storage_type),\n 'freshness_time': config.get('freshness_time', self.default_freshness_time)\n }"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _config2von(self, config: dict, access: str = None) -> dict:\n\n rv = {k: config.get(k, self._defaults[k]) for k in ('auto_create', 'auto_remove')}\n rv['access'] = access or self.default_access\n for key in ('seed', 'did', 'link_secret_label'):\n if key in config:\n rv[key] = config[key]\n return rv", "response": "Given a configuration dict with indy and possibly more configuration values return the corresponding VON wallet configuration dict from current default and input values."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def create(self, config: dict = None, access: str = None, replace: bool = False) -> Wallet:\n\n LOGGER.debug('WalletManager.create >>> config %s, access %s, replace %s', config, access, replace)\n\n assert {'name', 'id'} & {k for k in config}\n wallet_name = config.get('name', config.get('id'))\n if replace:\n von_wallet = self.get(config, access)\n if not await von_wallet.remove():\n LOGGER.debug('WalletManager.create Wallet:\n\n LOGGER.debug('WalletManager.get >>> config %s, access %s', config, access)\n\n rv = Wallet(\n self._config2indy(config),\n self._config2von(config, access))\n\n LOGGER.debug('WalletManager.get <<< %s', rv)\n return rv", "response": "Instantiate and return VON anchor wallet object on given configuration data."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def reseed_local(self, local_wallet: Wallet, next_seed: str = None) -> DIDInfo:\n\n LOGGER.debug('WalletManager.reseed_local >>> local_wallet %s', local_wallet)\n\n await local_wallet.reseed_init(next_seed)\n\n rv = await local_wallet.reseed_apply()\n LOGGER.debug('WalletManager.reseed_local <<< %s', rv)\n return rv", "response": "Generate and apply new verification key for local DID based on input seed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\nasync def export_wallet(self, von_wallet: Wallet, path: str) -> None:\n\n LOGGER.debug('WalletManager.export_wallet >>> von_wallet %s, path %s', von_wallet, path)\n\n if not von_wallet.handle:\n LOGGER.debug('WalletManager.export_wallet None:\n\n LOGGER.debug('WalletManager.import_wallet >>> indy_config %s, path: %s', indy_config, path)\n\n try:\n await wallet.import_wallet(\n json.dumps(indy_config),\n json.dumps({'key': access or self.default_access}),\n json.dumps({'path': path, 'key': access or self.default_access}))\n except IndyError as x_indy:\n if x_indy.error_code == ErrorCode.CommonInvalidStructure: # indy-sdk raises on bad access\n LOGGER.debug(\n 'WalletManager.import_wallet Wallet:\n \"\"\"\n Close and delete (open) VON anchor wallet and then create, open, and return\n replacement on current link secret.\n\n Note that this operation effectively destroys private keys for keyed data\n structures such as credential offers or credential definitions.\n\n Raise WalletState if the wallet is closed.\n\n :param von_wallet: open wallet\n :param seed: seed to use for new wallet (default random)\n :return: replacement wallet\n \"\"\"\n\n LOGGER.debug('WalletManager.reset >>> von_wallet %s', von_wallet)\n\n if not von_wallet.handle:\n LOGGER.debug('WalletManager.reset None:\n\n LOGGER.debug(\n 'WalletManager.register_storage_library >>> storage_type %s, c_library %s, entry_point %s',\n storage_type,\n c_library,\n entry_point)\n\n try:\n stg_lib = CDLL(c_library)\n result = stg_lib[entry_point]()\n if result:\n LOGGER.debug(\n 'WalletManager.register_storage_library KeyInfo:\n\n LOGGER.debug('Wallet.create_signing_key >>> seed: [SEED], metadata: %s', metadata)\n\n if not self.handle:\n LOGGER.debug('Wallet.create_signing_key DIDInfo:\n \"\"\"\n Create and store a new local DID for use in pairwise DID relations.\n\n :param seed: seed from which to create (default random)\n :param loc_did: local DID value (default None to let indy-sdk generate)\n :param metadata: metadata to associate with the local DID\n (operation always sets 'since', 'modified' epoch timestamps)\n :return: DIDInfo for new local DID\n \"\"\"\n\n LOGGER.debug('Wallet.create_local_did >>> seed: [SEED] loc_did: %s metadata: %s', loc_did, metadata)\n\n cfg = {}\n if seed:\n cfg['seed'] = seed\n if loc_did:\n cfg['did'] = loc_did\n\n if not self.handle:\n LOGGER.debug('Wallet.create_local_did DIDInfo:\n \"\"\"\n Replace the metadata associated with a local DID.\n\n Raise WalletState if wallet is closed, AbsentRecord for no such local DID.\n\n :param loc_did: local DID of interest\n :param metadata: new metadata to store\n :return: DIDInfo for local DID after write\n \"\"\"\n\n LOGGER.debug('Wallet.replace_local_did_metadata >>> loc_did: %s, metadata: %s', loc_did, metadata)\n\n old = await self.get_local_did(loc_did) # raises exceptions if applicable\n now = int(time())\n loc_did_metadata = {**(metadata or {}), 'since': (old.metadata or {}).get('since', now), 'modified': now}\n try:\n await did.set_did_metadata(self.handle, loc_did, json.dumps(loc_did_metadata))\n except IndyError as x_indy:\n LOGGER.debug('Wallet.replace_local_did_metadata Sequence[DIDInfo]:\n\n LOGGER.debug('Wallet.get_local_dids >>>')\n\n dids_with_meta = json.loads(did.list_my_dids_with_meta(self.handle)) # list\n\n rv = []\n for did_with_meta in dids_with_meta:\n meta = json.loads(did_with_meta['metadata']) if did_with_meta['metadata'] else {}\n if meta.get('anchor', False):\n continue # exclude anchor DIDs past and present\n rv.append(DIDInfo(did_with_meta['did'], did_with_meta['verkey'], meta))\n\n LOGGER.debug('Wallet.get_local_dids <<< %s', rv)\n return rv", "response": "Get list of local DIDInfos for local DIDs."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting current anchor DID by metadata.", "response": "async def get_anchor_did(self) -> str:\n \"\"\"\n Get current anchor DID by metadata, None for not yet set.\n\n :return: DID\n \"\"\"\n\n LOGGER.debug('Wallet.get_anchor_did >>>')\n\n if not self.handle:\n LOGGER.debug('Wallet.get_anchor_did latest:\n rv = did_with_meta.get('did')\n except json.decoder.JSONDecodeError:\n continue # it's not an anchor DID, carry on\n\n LOGGER.debug('Wallet.get_anchor_did <<< %s', rv)\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\nasync def create_link_secret(self, label: str) -> None:\n\n LOGGER.debug('Wallet.create_link_secret >>> label: %s', label)\n\n if not self.handle:\n LOGGER.debug('Wallet.create_link_secret None:\n \"\"\"\n Update non-secret storage record with link secret label.\n\n :param label: link secret label\n \"\"\"\n\n LOGGER.debug('Wallet._write_link_secret_label <<< %s', label)\n\n if await self.get_link_secret_label() == label:\n LOGGER.info('Wallet._write_link_secret_label abstaining - already current')\n else:\n await self.write_non_secret(StorageRecord(\n TYPE_LINK_SECRET_LABEL,\n label,\n tags=None,\n ident=str(int(time())))) # indy requires str\n\n LOGGER.debug('Wallet._write_link_secret_label <<<')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets current link secret label from non - secret storage records ; return None for no match.", "response": "async def get_link_secret_label(self) -> str:\n \"\"\"\n Get current link secret label from non-secret storage records; return None for no match.\n\n :return: latest non-secret storage record for link secret label\n \"\"\"\n\n LOGGER.debug('Wallet.get_link_secret_label >>>')\n\n if not self.handle:\n LOGGER.debug('Wallet.get_link_secret 'Wallet':\n\n LOGGER.debug('Wallet.open >>>')\n\n created = False\n while True:\n try:\n self._handle = await wallet.open_wallet(\n json.dumps(self.config),\n json.dumps(self.access_creds))\n LOGGER.info('Opened wallet %s on handle %s', self.name, self.handle)\n break\n except IndyError as x_indy:\n if x_indy.error_code == ErrorCode.WalletNotFoundError:\n if created:\n LOGGER.debug('Wallet.open() None:\n\n LOGGER.debug('Wallet.create >>>')\n\n try:\n await wallet.create_wallet(\n config=json.dumps(self.config),\n credentials=json.dumps(self.access_creds))\n LOGGER.info('Created wallet %s', self.name)\n except IndyError as x_indy:\n if x_indy.error_code == ErrorCode.WalletAlreadyExistsError:\n LOGGER.debug('Wallet.create None:\n \"\"\"\n Explicit exit. Close wallet (and delete if so configured).\n \"\"\"\n\n LOGGER.debug('Wallet.close >>>')\n\n if not self.handle:\n LOGGER.warning('Abstaining from closing wallet %s: already closed', self.name)\n else:\n LOGGER.debug('Closing wallet %s', self.name)\n await wallet.close_wallet(self.handle)\n self._handle = None\n if self.auto_remove:\n LOGGER.info('Automatically removing wallet %s', self.name)\n await self.remove()\n self._handle = None\n\n LOGGER.debug('Wallet.close <<<')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nremove serialized wallet return whether wallet absent after operation", "response": "async def remove(self) -> bool:\n \"\"\"\n Remove serialized wallet, best effort, if it exists. Return whether wallet absent after operation\n (removal successful or else not present a priori).\n\n Raise WalletState if wallet is open.\n\n :return: whether wallet gone from persistent storage\n \"\"\"\n\n LOGGER.debug('Wallet.remove >>>')\n\n if self.handle:\n LOGGER.debug('Wallet.remove PairwiseInfo:\n \"\"\"\n Store a pairwise DID for a secure connection. Use verification key for local DID in wallet if\n supplied; otherwise, create one first. If local DID specified but not present, raise AbsentRecord.\n\n With supplied metadata, replace or augment and overwrite any existing metadata for the pairwise\n relation if one already exists in the wallet. Always include local and remote DIDs and keys in\n metadata to allow for WQL search.\n\n Raise AbsentRecord on call to update a non-existent record. Raise BadRecord if metadata does not\n coerce into non-secrets API tags specification {str:str}.\n\n :param their_did: remote DID\n :param their_verkey: remote verification key (default None is OK if updating an existing pairwise DID)\n :param my_did: local DID\n :param metadata: metadata for pairwise connection\n :param replace_meta: whether to (True) replace or (False) augment and overwrite existing metadata\n :return: resulting PairwiseInfo\n \"\"\"\n\n LOGGER.debug(\n 'Wallet.write_pairwise >>> their_did: %s, their_verkey: %s, my_did: %s, metadata: %s, replace_meta: %s',\n their_did,\n their_verkey,\n my_did,\n metadata,\n replace_meta)\n\n if their_verkey is None:\n match = await self.get_pairwise(their_did)\n if not match:\n LOGGER.debug(\n 'Wallet.write_pairwise dict:\n\n LOGGER.debug('Wallet.get_pairwise >>> pairwise_filt: %s', pairwise_filt)\n\n if not self.handle:\n LOGGER.debug('Wallet.get_pairwise StorageRecord:\n\n LOGGER.debug('Wallet.write_non_secret >>> storec: %s, replace_meta: %s', storec, replace_meta)\n\n if not self.handle:\n LOGGER.debug('Wallet.write_non_secret dict:\n \"\"\"\n Return dict mapping each non-secret storage record of interest by identifier or,\n for no filter specified, mapping them all. If wallet has no such item, return empty dict.\n\n :param typ: non-secret storage record type\n :param filt: non-secret storage record identifier or WQL json (default all)\n :param canon_wql: WQL canonicalization function (default von_anchor.canon.canon_non_secret_wql())\n :param limit: maximum number of results to return (default no limit)\n :return: dict mapping identifiers to non-secret storage records\n \"\"\"\n\n LOGGER.debug('Wallet.get_non_secret >>> typ: %s, filt: %s, canon_wql: %s', typ, filt, canon_wql)\n\n if not self.handle:\n LOGGER.debug('Wallet.get_non_secret bytes:\n \"\"\"\n Encrypt plaintext for owner of DID, anonymously or via authenticated encryption scheme.\n Raise AbsentMessage for missing message, or WalletState if wallet is closed.\n\n :param message: plaintext, as bytes\n :param authn: whether to use authenticated encryption scheme\n :param to_verkey: verification key of recipient, None for anchor's own\n :param from_verkey: verification key of sender for authenticated encryption, None for anchor's own\n :return: ciphertext, as bytes\n \"\"\"\n\n LOGGER.debug(\n 'Wallet.encrypt >>> message: %s, authn: %s, to_verkey: %s, from_verkey: %s',\n message,\n authn,\n to_verkey,\n from_verkey)\n\n if not message:\n LOGGER.debug('Wallet.encrypt bytes:\n \"\"\"\n Pack a message for one or more recipients (default anchor only).\n Raise AbsentMessage for missing message, or WalletState if wallet is closed.\n\n :param message: message to pack\n :param recip_verkeys: verification keys of recipients (default anchor's own, only)\n :param sender_verkey: sender verification key (default anonymous encryption)\n :return: packed message\n \"\"\"\n\n LOGGER.debug(\n 'Wallet.pack >>> message: %s, recip_verkeys: %s, sender_verkey: %s',\n message,\n recip_verkeys,\n sender_verkey)\n\n if message is None:\n LOGGER.debug('Wallet.pack str:\n \"\"\"\n Begin reseed operation: generate new key. Raise WalletState if wallet is closed.\n\n :param next_seed: incoming replacement seed (default random)\n :return: new verification key\n \"\"\"\n\n LOGGER.debug('Wallet.reseed_init >>> next_seed: [SEED]')\n\n if not self.handle:\n LOGGER.debug('Wallet.reseed_init DIDInfo:\n \"\"\"\n Replace verification key with new verification key from reseed operation.\n Raise WalletState if wallet is closed.\n\n :return: DIDInfo with new verification key and metadata for DID\n \"\"\"\n\n LOGGER.debug('Wallet.reseed_apply >>>')\n\n if not self.handle:\n LOGGER.debug('Wallet.reseed_init str:\n \"\"\"\n Send schema to ledger, then retrieve it as written to the ledger and return it.\n Raise BadLedgerTxn on failure. Raise BadAttribute for attribute name with spaces or\n reserved for indy-sdk.\n\n If schema already exists on ledger, log error and return schema.\n\n :param schema_data_json: schema data json with name, version, attribute names; e.g.,\n\n ::\n\n {\n 'name': 'my-schema',\n 'version': '1.234',\n 'attr_names': ['favourite_drink', 'height', 'last_visit_date']\n }\n\n :return: schema json as written to ledger (or existed a priori)\n \"\"\"\n\n LOGGER.debug('Origin.send_schema >>> schema_data_json: %s', schema_data_json)\n\n schema_data = json.loads(schema_data_json)\n for attr in schema_data['attr_names']:\n if not (re.match(r'(?=[^- ])[-_a-zA-Z0-9 ]+(?<=[^- ])$', attr)) or attr.strip().lower() == 'hash':\n LOGGER.debug('Origin.send_schema Role:\n \"\"\"\n Return the indy-sdk null role for a tails sync anchor, which does not need write access.\n\n :return: USER role\n \"\"\"\n\n LOGGER.debug('NominalAnchor.least_role >>>')\n\n rv = Role.USER\n\n LOGGER.debug('NominalAnchor.least_role <<< %s', rv)\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclosing the current object.", "response": "async def close(self) -> None:\n \"\"\"\n Explicit exit. If so configured, populate cache to prove for any creds on schemata,\n cred defs, and rev regs marked of interest in configuration at initialization,\n archive cache, and purge prior cache archives.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('OrgHubAnchor.close >>>')\n\n archive_caches = False\n if self.config.get('archive-holder-prover-caches-on-close', False):\n archive_caches = True\n await self.load_cache_for_proof(False)\n if self.config.get('archive-verifier-caches-on-close', {}):\n archive_caches = True\n await self.load_cache_for_verification(False)\n if archive_caches:\n ArchivableCaches.archive(self.dir_cache)\n ArchivableCaches.purge_archives(self.dir_cache, True)\n\n # Do not close wallet independently: allow for sharing open wallet over many anchor lifetimes\n # await self.wallet.close() #1.7.8\n # Do not close pool independently: let relying party decide when to go on-line and off-line\n\n for path_rr_id in Tails.links(self._dir_tails):\n rr_id = basename(path_rr_id)\n try:\n await HolderProver._sync_revoc_for_proof(self, rr_id)\n except ClosedPool:\n LOGGER.warning('OrgHubAnchor sync-revoc on close required ledger for %s but pool was closed', rr_id)\n\n LOGGER.debug('OrgHubAnchor.close <<<')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef canon_cred_wql(query: dict) -> dict:\n\n for k in [qk for qk in query]: # copy: iteration alters query keys\n attr_match = re.match('attr::([^:]+)::(marker|value)$', k)\n if isinstance(query[k], dict): # only subqueries are dicts: recurse\n query[k] = canon_cred_wql(query[k])\n if k == '$or':\n if not isinstance(query[k], list):\n raise BadWalletQuery('Bad WQL; $or value must be a list in {}'.format(json.dumps(query)))\n query[k] = [canon_cred_wql(subq) for subq in query[k]]\n if attr_match:\n qkey = 'attr::{}::{}'.format(canon(attr_match.group(1)), canon(attr_match.group(2)))\n query[qkey] = query.pop(k)\n tag_value = query[qkey]\n if isinstance(tag_value, dict) and len(tag_value) == 1:\n if '$in' in tag_value:\n tag_value['$in'] = [raw(val) for val in tag_value.pop('$in')]\n else:\n wql_op = set(tag_value.keys()).pop() # $neq, $gt, $gte, etc.\n tag_value[wql_op] = raw(tag_value[wql_op])\n else: # equality\n query[qkey] = raw(query[qkey])\n\n return query", "response": "Canonicalize WQL query to get all the keys that match the input to indy - sdk wallet credential filtration."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef canon_pairwise_wql(query: dict = None) -> dict:\n\n if not query:\n return {\n '~their_did': {\n '$neq': ''\n }\n }\n\n for k in [qk for qk in query]: # copy: iteration alters query keys\n if isinstance(query[k], dict): # only subqueries are dicts: recurse\n query[k] = canon_pairwise_wql(query[k])\n if k == '$or':\n if not isinstance(query[k], list):\n raise BadWalletQuery('Bad WQL; $or value must be a list in {}'.format(json.dumps(query)))\n query[k] = [canon_pairwise_wql(subq) for subq in query[k]]\n elif k == '$not':\n query[k] = canon_pairwise_wql(query.pop(k))\n elif k not in WQL_1_OPS:\n qkey = canon_pairwise_tag(k)\n query[qkey] = query.pop(k)\n tag_value = query[qkey]\n if isinstance(tag_value, dict) and len(tag_value) == 1:\n if '$in' in tag_value:\n tag_value['$in'] = [raw(val) for val in tag_value['$in']]\n else:\n wql_op = set(tag_value.keys()).pop() # $neq, $gt, $gt, etc.\n tag_value[wql_op] = raw(tag_value[wql_op])\n else:\n query[qkey] = raw(query.pop(qkey))\n\n return query", "response": "Canonicalize WQL tags to unencrypted storage specification."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_requirements(filename):\n\n try:\n with open(filename) as fh_req:\n return [line.strip() for line in fh_req if line.strip() and not line.startswith('#')]\n except FileNotFoundError:\n print('File not found: {}'.format(realpath(filename)), file=stderr)\n raise", "response": "Load a pip requirements file and return a list of strings"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\nasync def add_config(self, name: str, genesis: str = None) -> None:\n\n LOGGER.debug('NodePoolManager.__init__ >>> name: %s, genesis: %s', name, genesis)\n\n if name in await self.list():\n LOGGER.debug('NodePoolManager.add_config: List[str]:\n\n LOGGER.debug('NodePoolManager.list >>>')\n\n rv = [p['pool'] for p in await pool.list_pools()]\n\n LOGGER.debug('NodePoolManager.list <<< %s', rv)\n return rv", "response": "Return list of pool names configured empty list for none."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get(self, name: str, config: dict = None) -> NodePool:\n\n LOGGER.debug('NodePoolManager.node_pool >>>')\n\n rv = NodePool(name, self.protocol, config)\n\n LOGGER.debug('NodePoolManager.node_pool <<< %s', rv)\n return rv", "response": "Return node pool in input name and optional configuration."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nremove serialized pool info if it exists. Abstain from removing open node pool.", "response": "async def remove(self, name: str) -> None:\n \"\"\"\n Remove serialized pool info if it exists. Abstain from removing open node pool.\n \"\"\"\n\n LOGGER.debug('NodePoolManager.remove >>> name: %s', name)\n\n try:\n await pool.delete_pool_ledger_config(name)\n except IndyError as x_indy:\n LOGGER.info('Abstaining from node pool removal; indy-sdk error code %s', x_indy.error_code)\n\n LOGGER.debug('NodePool.remove <<<')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef authnkey(self) -> dict:\n\n return {k: self._pubkey[k] for k in self._pubkey if self._pubkey[k].authn}", "response": "Return a dict of public keys marked as authentication keys."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set(self, item: Union[Service, PublicKey]) -> 'DIDDoc':\n\n if isinstance(item, Service):\n self.service[item.id] = item\n elif isinstance(item, PublicKey):\n self.pubkey[item.id] = item\n else:\n raise BadDIDDocItem('Cannot add item {} to DIDDoc on DID {}'.format(item, self.did))", "response": "Add or replace service or public key to current DIDDoc."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef serialize(self) -> str:\n\n return {\n '@context': DIDDoc.CONTEXT,\n 'id': canon_ref(self.did, self.did),\n 'publicKey': [pubkey.to_dict() for pubkey in self.pubkey.values()],\n 'authentication': [{\n 'type': pubkey.type.authn_type,\n 'publicKey': canon_ref(self.did, pubkey.id)\n } for pubkey in self.pubkey.values() if pubkey.authn],\n 'service': [service.to_dict() for service in self.service.values()]\n }", "response": "Dump current object to a JSON - compatible dictionary."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_service_pubkeys(self, service: dict, tags: Union[Sequence[str], str]) -> List[PublicKey]:\n\n rv = []\n for tag in [tags] if isinstance(tags, str) else list(tags):\n\n for svc_key in service.get(tag, {}):\n canon_key = canon_ref(self.did, svc_key)\n pubkey = None\n\n if '#' in svc_key:\n if canon_key in self.pubkey:\n pubkey = self.pubkey[canon_key]\n else: # service key refers to another DID doc\n LOGGER.debug(\n 'DIDDoc.add_service_pubkeys 'DIDDoc':\n \"\"\"\n Construct DIDDoc object from dict representation.\n\n Raise BadIdentifier for bad DID.\n\n :param did_doc: DIDDoc dict reprentation.\n :return: DIDDoc from input json.\n \"\"\"\n\n rv = None\n if 'id' in did_doc:\n rv = DIDDoc(did_doc['id'])\n else: # get DID to serve as DID document identifier from first public key\n if 'publicKey' not in did_doc:\n LOGGER.debug('DIDDoc.deserialize 'Protocol':\n\n return Protocol.V_13 if version == Protocol.V_13.value.name else Protocol.DEFAULT", "response": "Get the class corresponding to the input version value."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the tag for the current version of node protocol.", "response": "def cd_id_tag(self, for_box_id: bool = False) -> str:\n \"\"\"\n Return (place-holder) credential definition identifier tag for current version of node protocol.\n At present, von_anchor always uses the tag of 'tag' if the protocol calls for one.\n\n :param for_box_id: whether to prefix a colon, if current protocol uses one, in constructing\n a cred def id or rev reg id.\n :return: cred def id tag\n \"\"\"\n\n if for_box_id:\n return '' if self == Protocol.V_13 else ':tag'\n return 'tag'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the identifier for input issuer DID and schema sequence number.", "response": "def cred_def_id(self, issuer_did: str, schema_seq_no: int) -> str:\n \"\"\"\n Return credential definition identifier for input issuer DID and schema sequence number.\n\n :param issuer_did: DID of credential definition issuer\n :param schema_seq_no: schema sequence number\n :return: credential definition identifier\n \"\"\"\n\n return '{}:3:CL:{}{}'.format( # 3 marks indy cred def id, CL is sig type\n issuer_did,\n schema_seq_no,\n self.cd_id_tag(True))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef txn_data2schema_key(self, txn: dict) -> SchemaKey:\n\n rv = None\n if self == Protocol.V_13:\n rv = SchemaKey(txn['identifier'], txn['data']['name'], txn['data']['version'])\n else:\n txn_txn = txn.get('txn', None) or txn # may have already run this txn through txn2data() below\n rv = SchemaKey(\n txn_txn['metadata']['from'],\n txn_txn['data']['data']['name'],\n txn_txn['data']['data']['version'])\n\n return rv", "response": "Return schema key from ledger transaction data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef txn2data(self, txn: dict) -> str:\n\n rv_json = json.dumps({})\n if self == Protocol.V_13:\n rv_json = json.dumps(txn['result'].get('data', {}))\n else:\n rv_json = json.dumps((txn['result'].get('data', {}) or {}).get('txn', {})) # \"data\": null for no such txn\n\n return rv_json", "response": "Given ledger transaction return its data json."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngives ledger transaction return its epoch time.", "response": "def txn2epoch(self, txn: dict) -> int:\n \"\"\"\n Given ledger transaction, return its epoch time.\n\n :param txn: transaction as dict\n :return: transaction time\n \"\"\"\n\n rv = None\n if self == Protocol.V_13:\n rv = txn['result']['txnTime']\n else:\n rv = txn['result']['txnMetadata']['txnTime']\n\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ngive a genesis transaction return its node host and port.", "response": "def genesis_host_port(self, genesis_txn: dict) -> tuple:\n \"\"\"\n Given a genesis transaction, return its node host and port.\n\n :param genesis_txn: genesis transaction as dict\n :return: node host and port\n \"\"\"\n\n txn_data = genesis_txn['data'] if self == Protocol.V_13 else genesis_txn['txn']['data']['data']\n return (txn_data['node_ip'], txn_data['node_port'])"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nopen reader handle and return current object.", "response": "async def open(self) -> 'Tails':\n \"\"\"\n Open reader handle and return current object.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('Tails.open >>>')\n\n self._reader_handle = await blob_storage.open_reader('default', self._tails_config_json)\n\n LOGGER.debug('Tails.open <<<')\n return self"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns whether input token looks like a valid tails hash.", "response": "def ok_hash(token: str) -> bool:\n \"\"\"\n Whether input token looks like a valid tails hash.\n\n :param token: candidate string\n :return: whether input token looks like a valid tails hash\n \"\"\"\n\n LOGGER.debug('Tails.ok_hash >>> token: %s', token)\n\n rv = re.match('[{}]{{42,44}}$'.format(B58), token) is not None\n LOGGER.debug('Tails.ok_hash <<< %s', rv)\n return rv"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nassociating tails file with a revocation registry id.", "response": "def associate(base_dir: str, rr_id: str, tails_hash: str) -> None:\n \"\"\"\n Create symbolic link to tails file named tails_hash for rev reg id rr_id.\n\n :param rr_id: rev reg id\n :param tails_hash: hash of tails file, serving as file name\n \"\"\"\n\n LOGGER.debug('Tails.associate >>> base_dir: %s, rr_id: %s, tails_hash: %s', base_dir, rr_id, tails_hash)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Tails.associate str:\n\n LOGGER.debug('Tails.dir >>> base_dir: %s, rr_id: %s', base_dir, rr_id)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Tails.dir str:\n \"\"\"\n Get, from the specified directory, the path to the tails file associated with\n the input revocation registry identifier, or None for no such file.\n\n :param base_dir: base directory for tails files, thereafter split by cred def id\n :param rr_id: rev reg id\n :return: (stringified) path to tails file of interest, or None for no such file.\n \"\"\"\n\n LOGGER.debug('Tails.linked >>> base_dir: %s, rr_id: %s', base_dir, rr_id)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Tails.linked set:\n \"\"\"\n Return set of all paths to symbolic links (rev reg ids) associating their\n respective tails files, in specified base tails directory recursively\n (omitting the .hopper subdirectory), on input issuer DID if specified.\n\n :param base_dir: base directory for tails files, thereafter split by cred def id\n :param issuer_did: issuer DID of interest\n :return: set of paths to symbolic links associating tails files\n \"\"\"\n\n LOGGER.debug('Tails.links >>> base_dir: %s, issuer_did: %s', base_dir, issuer_did)\n\n if issuer_did and not ok_did(issuer_did):\n LOGGER.debug('Tails.links set:\n \"\"\"\n Return all paths to tails files, in specified tails base directory recursively\n (omitting the .hopper subdirectory), without symbolic links associating\n revocation registry identifiers.\n\n At an Issuer, tails files should not persist long without revocation registry identifier\n association via symbolic link. At a HolderProver, a newly downloaded tails file stays\n unlinked until the anchor stores a credential or creates a proof needing it, or else the\n anchor restarts.\n\n :param base_dir: base directory for tails files, thereafter split by cred def id\n :return: set of paths to tails files with no local symbolic links to them\n \"\"\"\n\n LOGGER.debug('Tails.unlinked >>> base_dir: %s', base_dir)\n\n rv = set()\n for dir_path, dir_names, file_names in walk(base_dir, topdown=True):\n dir_names[:] = [d for d in dir_names if not d.startswith('.')]\n for file_name in file_names:\n if isfile(join(dir_path, file_name)) and Tails.ok_hash(file_name):\n rv.add(join(dir_path, file_name))\n rv -= {join(dirname(path_link), readlink(path_link)) for path_link in Tails.links(base_dir)}\n\n LOGGER.debug('Tails.unlinked <<< %s', rv)\n return rv"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn next available tag name for a new credential definition identifier.", "response": "def next_tag(base_dir: str, cd_id: str) -> (str, int):\n \"\"\"\n Return the next tag name available for a new rev reg id on input cred def id in base directory,\n and suggested size of associated rev reg.\n\n :param base_dir: base directory for tails files, thereafter split by cred def id\n :param cd_id: credential definition identifier of interest\n :return: stringified least non-negative integer not yet used in a rev reg id associated with a tails file\n in base directory, and recommendation for next size to use\n \"\"\"\n\n LOGGER.debug('Tails.next_tag >>> base_dir: %s, cd_id: %s', base_dir, cd_id)\n\n if not ok_cred_def_id(cd_id):\n LOGGER.debug('Tails.next_tag str:\n\n LOGGER.debug('Tails.current_rev_reg_id >>> base_dir: %s, cd_id: %s', base_dir, cd_id)\n\n if not ok_cred_def_id(cd_id):\n LOGGER.debug('Tails.current_rev_reg_id 9, not '9' > '10'\n LOGGER.debug('Tails.current_rev_reg_id <<< %s', rv)\n return rv", "response": "Return revocation registry identifier for input credential definition identifier."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef path(self) -> str:\n\n config = json.loads(self._tails_config_json)\n return join(config['base_dir'], config['file'])", "response": "Returns the path to the current tails file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nencoding a credential attribute value.", "response": "def encode(orig: Any) -> str:\n \"\"\"\n Encode credential attribute value, purely stringifying any int32 and leaving numeric int32 strings alone,\n but mapping any other input to a stringified 256-bit (but not 32-bit) integer. Predicates in indy-sdk operate\n on int32 values properly only when their encoded values match their raw values.\n\n :param orig: original value to encode\n :return: encoded value\n \"\"\"\n\n if isinstance(orig, int) and -I32_BOUND <= orig < I32_BOUND:\n return str(int(orig)) # python bools are ints\n\n try:\n i32orig = int(str(orig)) # don't encode floats as ints\n if -I32_BOUND <= i32orig < I32_BOUND:\n return str(i32orig)\n except (ValueError, TypeError):\n pass\n\n rv = int.from_bytes(sha256(raw(orig).encode()).digest(), 'big')\n while -I32_BOUND <= rv < I32_BOUND:\n rv = int.from_bytes(sha256(rv.encode()).digest(), 'big') # sha256 maps no 32-bit int to another: terminates\n\n return str(rv)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get(relation: str) -> 'Predicate':\n\n for pred in Predicate:\n if relation.upper() in (pred.value.fortran, pred.value.wql.upper(), pred.value.math):\n return pred\n return None", "response": "Returns enum instance corresponding to input relation string"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef to_int(value: Any) -> int:\n\n if isinstance(value, (bool, int)):\n return int(value)\n return int(str(value))", "response": "Cast a value as its equivalent int for indy predicate argument. Raise ValueError for any input but\n int or boolean. Raise ValueError for any input but\n int or boolean. Raise ValueError for any input but\n int or boolean."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get(token: Union[str, int] = None) -> 'Role':\n\n if token is None:\n return Role.USER\n\n for role in Role:\n if role == Role.ROLE_REMOVE:\n continue # ROLE_REMOVE is not a sensible role to parse from any configuration\n if isinstance(token, int) and token in role.value:\n return role\n if str(token).upper() == role.name or token in (str(v) for v in role.value): # could be numeric string\n return role\n\n return None", "response": "Return the appropriate role for the given token."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef token(self) -> str:\n\n return self.value[0] if self in (Role.USER, Role.ROLE_REMOVE) else self.name", "response": "Return token identifying role to indy - sdk."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cached_get(timeout, *params):\n\n def decorator(view_func):\n @wraps(view_func, assigned=available_attrs(view_func))\n def _wrapped_view(view_or_request, *args, **kwargs):\n\n # The type of the request gets muddled when using a function based\n # decorator. We must use a function based decorator so it can be\n # used in urls.py.\n request = getattr(view_or_request, \"request\", view_or_request)\n\n if not hasattr(_thread_locals, \"ultracache_request\"):\n setattr(_thread_locals, \"ultracache_request\", request)\n\n # If request not GET or HEAD never cache\n if request.method.lower() not in (\"get\", \"head\"):\n return view_func(view_or_request, *args, **kwargs)\n\n # If request contains messages never cache\n l = 0\n try:\n l = len(request._messages)\n except (AttributeError, TypeError):\n pass\n if l:\n return view_func(view_or_request, *args, **kwargs)\n\n # Compute a cache key\n li = [str(view_or_request.__class__), view_func.__name__]\n\n # request.get_full_path is implicitly added it no other request\n # path is provided. get_full_path includes the querystring and is\n # the more conservative approach but makes it trivially easy for a\n # request to bust through the cache.\n if not set(params).intersection(set((\n \"request.get_full_path()\", \"request.path\", \"request.path_info\"\n ))):\n li.append(request.get_full_path())\n\n if \"django.contrib.sites\" in settings.INSTALLED_APPS:\n li.append(get_current_site_pk(request))\n\n # Pre-sort kwargs\n keys = list(kwargs.keys())\n keys.sort()\n for key in keys:\n li.append(\"%s,%s\" % (key, kwargs[key]))\n\n # Extend cache key with custom variables\n for param in params:\n if not isinstance(param, str):\n param = str(param)\n li.append(eval(param))\n\n s = \":\".join([str(l) for l in li])\n hashed = hashlib.md5(s.encode(\"utf-8\")).hexdigest()\n cache_key = \"ucache-get-%s\" % hashed\n cached = cache.get(cache_key, None)\n if cached is None:\n # The get view as outermost caller may bluntly set _ultracache\n request._ultracache = []\n response = view_func(view_or_request, *args, **kwargs)\n content = None\n if isinstance(response, TemplateResponse):\n content = response.render().rendered_content\n elif isinstance(response, HttpResponse):\n content = response.content\n if content is not None:\n headers = getattr(response, \"_headers\", {})\n cache.set(\n cache_key,\n {\"content\": content, \"headers\": headers},\n timeout\n )\n cache_meta(request, cache_key)\n else:\n response = HttpResponse(cached[\"content\"])\n # Headers has a non-obvious format\n for k, v in cached[\"headers\"].items():\n response[v[0]] = v[1]\n\n return response\n\n return _wrapped_view\n return decorator", "response": "Decorator applied specifically to a view s get method."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nperforms ancestor opening operations and synchronize revocation registry to tails tree content.", "response": "async def open(self) -> 'Issuer':\n \"\"\"\n Explicit entry. Perform ancestor opening operations,\n then synchronize revocation registry to tails tree content.\n\n :return: current object\n \"\"\"\n\n LOGGER.debug('Issuer.open >>>')\n\n await super().open()\n for path_rr_id in Tails.links(self.dir_tails, self.did):\n await self._sync_revoc_for_issue(basename(path_rr_id))\n\n LOGGER.debug('Issuer.open <<<')\n return self"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\nasync def _send_rev_reg_def(self, rr_id: str) -> None:\n\n LOGGER.debug('Issuer._send_rev_reg_def >>> rr_id: %s', rr_id)\n\n dir_tails_rr_id = self.rrb.dir_tails_top(rr_id)\n dir_target = self.rrb.dir_tails_target(rr_id)\n\n if not Tails.linked(dir_tails_rr_id, rr_id):\n LOGGER.debug(\n 'Issuer._send_rev_reg_def None:\n\n LOGGER.debug('Issuer._set_rev_reg >>> rr_id: %s, rr_size: %s', rr_id, rr_size)\n assert self.rrbx\n\n dir_hopper_rr_id = join(self.rrb.dir_tails_hopper, rr_id)\n\n while Tails.linked(dir_hopper_rr_id, rr_id) is None:\n await asyncio.sleep(1)\n await self._send_rev_reg_def(rr_id)\n\n cd_id = rev_reg_id2cred_def_id(rr_id)\n (next_tag, rr_size_suggested) = Tails.next_tag(self.dir_tails, cd_id)\n rr_id = rev_reg_id(cd_id, next_tag)\n self.rrb.mark_in_progress(rr_id, rr_size or rr_size_suggested)\n\n LOGGER.debug('Issuer._set_rev_reg <<<')", "response": "Set revocation registry data from hopper into place within tails directory."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\nasync def _sync_revoc_for_issue(self, rr_id: str, rr_size: int = None) -> None:\n\n LOGGER.debug('Issuer._sync_revoc_for_issue >>> rr_id: %s, rr_size: %s', rr_id, rr_size)\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Issuer._sync_revoc_for_issue str:\n\n LOGGER.debug('Issuer.path_tails >>>')\n\n if not ok_rev_reg_id(rr_id):\n LOGGER.debug('Issuer.path_tails (str, bool):\n\n LOGGER.debug(\n 'Issuer._create_cred_def >>> schema: %s, ledger_cred_def: %s, revo: %s',\n schema,\n ledger_cred_def,\n revo)\n\n cred_def_json = '{}'\n private_key_ok = True\n try:\n (_, cred_def_json) = await anoncreds.issuer_create_and_store_credential_def(\n self.wallet.handle,\n self.did, # issuer DID\n json.dumps(schema),\n self.pool.protocol.cd_id_tag(False), # expect only one cred def per schema and issuer\n 'CL',\n json.dumps({'support_revocation': revo}))\n if ledger_cred_def:\n private_key_ok = False\n LOGGER.warning(\n 'New cred def on %s in wallet shadows existing one on ledger: private key not usable',\n cred_def_id(self.did, schema['seqNo'], self.pool.protocol))\n # carry on though, this anchor may have other capacities so public key may be good enough\n except IndyError as x_indy:\n if x_indy.error_code == ErrorCode.AnoncredsCredDefAlreadyExistsError:\n if ledger_cred_def:\n LOGGER.info(\n 'Issuer wallet %s reusing existing cred def on schema %s version %s',\n self.name,\n schema['name'],\n schema['version'])\n else:\n LOGGER.debug('Issuer._create_cred_def str:\n \"\"\"\n Create a credential definition as Issuer, store it in its wallet, and send it to the ledger.\n\n Raise CorruptWallet for wallet not pertaining to current ledger, BadLedgerTxn on failure\n to send credential definition to ledger if need be, WalletState for closed wallet,\n or IndyError for any other failure to create and store credential definition in wallet.\n\n :param s_id: schema identifier\n :param revo: whether to support revocation for cred def\n :param rr_size: size of initial revocation registry (default as per RevRegBuilder.create_rev_reg()),\n if revocation supported\n :return: json credential definition as it appears on ledger\n \"\"\"\n\n LOGGER.debug('Issuer.send_cred_def >>> s_id: %s, revo: %s, rr_size: %s', s_id, revo, rr_size)\n\n if not ok_schema_id(s_id):\n LOGGER.debug('Issuer.send_cred_def str:\n\n LOGGER.debug('Issuer.create_cred_offer >>> schema_seq_no: %s', schema_seq_no)\n\n if not self.wallet.handle:\n LOGGER.debug('Issuer.create_cred_offer (str, str):\n \"\"\"\n Create credential as Issuer out of credential request and dict of key:value (raw, unencoded)\n entries for attributes.\n\n Return credential json, and if cred def supports revocation, credential revocation identifier.\n Raise WalletState for closed wallet.\n\n If the credential definition supports revocation, and the current revocation registry is full,\n the processing creates a new revocation registry en passant. Depending on the revocation\n registry size (by default starting at 64 and doubling iteratively through a maximum of 100000)\n and the revocation registry builder posture (see RevRegBuilder.__init__()), this operation may\n delay credential creation by several seconds. The use of an external revocation registry builder\n runs a parallel process, skirting this delay, but is more costly at initialization.\n\n :param cred_offer_json: credential offer json as created by Issuer\n :param cred_req_json: credential request json as created by HolderProver\n :param cred_attrs: dict mapping each attribute to its original value (the operation encodes it); e.g.,\n\n ::\n\n {\n 'favourite_drink': 'martini',\n 'height': 180,\n 'last_visit_date': '2017-12-31',\n 'weaknesses': None\n }\n\n :param rr_size: size of new revocation registry (default as per RevRegBuilder.create_rev_reg()) if necessary\n :return: tuple with newly issued credential json, credential revocation identifier (if cred def\n supports revocation, None otherwise).\n \"\"\"\n\n LOGGER.debug(\n 'Issuer.create_cred >>> cred_offer_json: %s, cred_req_json: %s, cred_attrs: %s, rr_size: %s',\n cred_offer_json,\n cred_req_json,\n cred_attrs,\n rr_size)\n\n if not self.wallet.handle:\n LOGGER.debug('Issuer.create_cred int:\n \"\"\"\n Revoke credential that input revocation registry identifier and\n credential revocation identifier specify.\n\n Return (epoch seconds) time of revocation.\n\n Raise AbsentTails if no tails file is available for input revocation registry identifier.\n Raise WalletState for closed wallet.\n Raise BadRevocation if issuer cannot revoke specified credential for any other reason\n (e.g., did not issue it, already revoked it).\n\n :param rr_id: revocation registry identifier\n :param cr_id: credential revocation identifier\n :return: time of revocation, in epoch seconds\n \"\"\"\n\n LOGGER.debug('Issuer.revoke_cred >>> rr_id: %s, cr_id: %s', rr_id, cr_id)\n\n if not self.wallet.handle:\n LOGGER.debug('Issuer.revoke_cred str:\n\n LOGGER.debug('Issuer.get_box_ids_issued >>>')\n\n cd_ids = [\n d for d in listdir(self.dir_tails) if isdir(join(self.dir_tails, d)) and ok_cred_def_id(d, self.did)]\n s_ids = []\n for cd_id in cd_ids:\n try:\n s_ids.append(json.loads(await self.get_schema(cred_def_id2seq_no(cd_id)))['id'])\n except AbsentSchema:\n LOGGER.error(\n 'Issuer %s has issued cred def %s but no corresponding schema on ledger',\n self.name,\n cd_id)\n rr_ids = [basename(link) for link in Tails.links(self.dir_tails, self.did)]\n\n rv = json.dumps({\n 'schema_id': s_ids,\n 'cred_def_id': cd_ids,\n 'rev_reg_id': rr_ids\n })\n LOGGER.debug('Issuer.get_box_ids_issued <<< %s', rv)\n return rv", "response": "Get the set of unique box identifiers for all credential definitions and credentials issued."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive a URI reference return up to its delimiter or all of it if there is none.", "response": "def resource(ref: str, delimiter: str = None) -> str:\n \"\"\"\n Given a (URI) reference, return up to its delimiter (exclusively), or all of it if there is none.\n\n :param ref: reference\n :param delimiter: delimiter character (default None maps to '#', or ';' introduces identifiers)\n \"\"\"\n\n return ref.split(delimiter if delimiter else '#')[0]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a URI into a DID if need be left - stripping did : if present.", "response": "def canon_did(uri: str) -> str:\n \"\"\"\n Convert a URI into a DID if need be, left-stripping 'did:sov:' if present.\n Return input if already a DID. Raise BadIdentifier for invalid input.\n\n :param uri: input URI or DID\n :return: corresponding DID\n \"\"\"\n\n if ok_did(uri):\n return uri\n\n if uri.startswith('did:sov:'):\n rv = uri[8:]\n if ok_did(rv):\n return rv\n raise BadIdentifier('Bad specification {} does not correspond to a sovrin DID'.format(uri))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngive a DID and a reference in a DID document return a canonical form of a URI.", "response": "def canon_ref(did: str, ref: str, delimiter: str = None):\n \"\"\"\n Given a reference in a DID document, return it in its canonical form of a URI.\n\n :param did: DID acting as the identifier of the DID document\n :param ref: reference to canonicalize, either a DID or a fragment pointing to a location in the DID doc\n :param delimiter: delimiter character marking fragment (default '#') or\n introducing identifier (';') against DID resource\n \"\"\"\n\n if not ok_did(did):\n raise BadIdentifier('Bad DID {} cannot act as DID document identifier'.format(did))\n\n if ok_did(ref): # e.g., LjgpST2rjsoxYegQDRm7EL\n return 'did:sov:{}'.format(did)\n\n if ok_did(resource(ref, delimiter)): # e.g., LjgpST2rjsoxYegQDRm7EL#keys-1\n return 'did:sov:{}'.format(ref)\n\n if ref.startswith('did:sov:'): # e.g., did:sov:LjgpST2rjsoxYegQDRm7EL, did:sov:LjgpST2rjsoxYegQDRm7EL#3\n rv = ref[8:]\n if ok_did(resource(rv, delimiter)):\n return ref\n raise BadIdentifier('Bad URI {} does not correspond to a sovrin DID'.format(ref))\n\n if urlparse(ref).scheme: # e.g., https://example.com/messages/8377464\n return ref\n\n return 'did:sov:{}{}{}'.format(did, delimiter if delimiter else '#', ref)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ok_tags(tags: dict) -> bool:\n\n if not tags:\n return True\n depth = 0\n queue = [(i, depth+1) for i in tags.values() if isinstance(i, dict)]\n max_depth = 0\n while queue and max_depth < 2:\n sub, depth = queue.pop()\n max_depth = max(max_depth, depth)\n queue = queue + [(i, depth+1) for i in sub.values() if isinstance(i, dict)]\n\n return max_depth < 2 and all(isinstance(k, str) and isinstance(tags[k], str) for k in tags)", "response": "Check if input tags dict is OK as an indy - sdk tags structure."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef tags(self, val: str) -> None:\n\n if not StorageRecord.ok_tags(val):\n LOGGER.debug('StorageRecord.__init__ dict:\n\n return {t: self.tags[t] for t in (self.tags or {}) if t.startswith('~')} or None", "response": "Accessor for record tags stored in the clear."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef encr_tags(self) -> dict:\n\n return {t: self._tags[t] for t in self.tags or {} if not t.startswith('~')} or None", "response": "Accessor for record tags stored encrypted."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfetching a dict of all hash suffixes from Pwned Passwords for a given a SHA - 1 prefix.", "response": "def _get_pwned(prefix):\n \"\"\"\n Fetches a dict of all hash suffixes from Pwned Passwords for a\n given SHA-1 prefix.\n\n \"\"\"\n try:\n response = requests.get(\n url=API_ENDPOINT.format(prefix),\n headers={'User-Agent': USER_AGENT},\n timeout=getattr(\n settings,\n 'PWNED_PASSWORDS_API_TIMEOUT',\n REQUEST_TIMEOUT,\n ),\n )\n response.raise_for_status()\n except requests.RequestException as e:\n # Gracefully handle timeouts and HTTP error response codes.\n log.warning(\n 'Skipped Pwned Passwords check due to error: %r', e\n )\n return None\n\n results = {}\n for line in response.text.splitlines():\n line_suffix, _, times = line.partition(':')\n results[line_suffix] = int(times)\n\n return results"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pwned_password(password):\n if not isinstance(password, text_type):\n raise TypeError('Password values to check must be Unicode strings.')\n password_hash = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()\n prefix, suffix = password_hash[:5], password_hash[5:]\n results = _get_pwned(prefix)\n if results is None:\n # Gracefully handle timeouts and HTTP error response codes.\n return None\n return results.get(suffix, 0)", "response": "Checks a password against the Pwned Passwords database."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_regularizer(self, proxfun, **kwargs):\n\n # if proxfun is a string, grab the corresponding function from operators.py\n if isinstance(proxfun, str):\n try:\n proxfun_name = proxfun.split(None, 1)[0]\n # Ignore everything after white space\n op = getattr(operators, proxfun_name)\n self.objectives.append(lambda theta, rho: op(theta.copy(), float(rho), **kwargs))\n\n except AttributeError as e:\n print(str(e) + '\\n' + 'Could not find the function ' + proxfun + ' in the operators module!')\n\n # if proxfun is a function, add it as its own proximal operator\n elif hasattr(proxfun, '__call__'):\n self.objectives.append(lambda theta, rho: proxfun(theta.copy(), float(rho)))\n\n # type of proxfun must be a string or a function\n else:\n raise TypeError('The argument \"proxfun\" must be a string or a function!')", "response": "Adds a regularizer to the objectives list for the given function in the operators module."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a set of regularizers to the set of regularizers.", "response": "def set_regularizers(self, regularizers, clear=True):\n \"\"\"\n Adds a set of regularizers\n\n Parameters\n ----------\n regularizers : dict\n Each key is the name of a corresponding proximal operator, and the\n value associated with that key is a set of keyword arguments\n\n clear : boolean, optional\n Whether or not to clear the existing regularizers. (Default: True)\n\n \"\"\"\n\n # clear existing operators\n if clear:\n self.clear()\n\n # add new regularizers\n list([self.add_regularizer(proxfun, **regularizers[proxfun])\n for proxfun in regularizers.keys()])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef minimize(self, theta_init, max_iter=50, callback=None, disp=0, tau=(10., 2., 2.), tol=1e-3):\n\n # get list of objectives for this parameter\n num_obj = len(self.objectives)\n assert num_obj >= 1, \"There must be at least one objective!\"\n\n # initialize lists of primal and dual variable copies, one for each objective\n orig_shape = theta_init.shape\n primals = [theta_init.flatten() for _ in range(num_obj)]\n duals = [np.zeros(theta_init.size) for _ in range(num_obj)]\n theta_avg = np.mean(primals, axis=0).ravel()\n\n # initialize penalty parameter\n tau = namedtuple('tau', ('init', 'inc', 'dec'))(*tau)\n rho = tau.init\n\n # store cumulative runtimes of each iteration, starting now\n tstart = time.time()\n\n # clear metadata\n self.metadata = defaultdict(list)\n\n # run ADMM iterations\n self.converged = False\n for cur_iter in range(max_iter):\n\n # store the parameters from the previous iteration\n theta_prev = theta_avg\n\n # update each primal variable copy by taking a proximal step via each objective\n for varidx, dual in enumerate(duals):\n primals[varidx] = self.objectives[varidx]((theta_prev - dual).reshape(orig_shape), rho).ravel()\n\n # average primal copies\n theta_avg = np.mean(primals, axis=0)\n\n # update the dual variables (after primal update has finished)\n for varidx, primal in enumerate(primals):\n duals[varidx] += primal - theta_avg\n\n # compute primal and dual residuals\n primal_resid = float(np.sum([np.linalg.norm(primal - theta_avg) for primal in primals]))\n dual_resid = num_obj * rho ** 2 * np.linalg.norm(theta_avg - theta_prev)\n\n # update penalty parameter according to primal and dual residuals\n # (see sect. 3.4.1 of the Boyd and Parikh ADMM paper)\n if primal_resid > tau.init * dual_resid:\n rho *= float(tau.inc)\n elif dual_resid > tau.init * primal_resid:\n rho /= float(tau.dec)\n\n # update metadata for this iteration\n self.metadata['Primal resid'].append(primal_resid)\n self.metadata['Dual resid'].append(dual_resid)\n self.metadata['Time (s)'].append(time.time() - tstart)\n self.metadata['rho'].append(rho)\n\n # invoke the callback function with the current parameters and\n # history\n if callback is not None:\n\n # get the metadata from this iteration\n data = valmap(last, self.metadata)\n\n callback(theta_avg.reshape(orig_shape), data)\n\n # update the display\n self.update_display(cur_iter + 1, disp)\n\n # check for convergence\n if (primal_resid <= tol) & (dual_resid <= tol):\n self.converged = True\n break\n\n # clean up display\n self.update_display(-1, disp)\n\n # store and return final parameters\n self.theta = theta_avg.reshape(orig_shape)\n return self.theta", "response": "Minimize a list of objectives using a proximal consensus algorithm."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the display of the current iteration.", "response": "def update_display(self, iteration, disp_level, col_width=12): # pragma: no cover\n \"\"\"\n Prints information about the optimization procedure to standard output\n\n Parameters\n ----------\n iteration : int\n The current iteration. Must either a positive integer or -1, which indicates the end of the algorithm\n\n disp_level : int\n An integer which controls how much information to display, ranging from 0 (nothing) to 3 (lots of stuff)\n\n col_width : int\n The width of each column in the data table, used if disp_level > 1\n \"\"\"\n\n # exit and print nothing if disp_level is zero\n if disp_level == 0:\n return\n\n else:\n\n # simple update, no table\n if disp_level == 1 and iteration >= 0:\n print('[Iteration %i]' % iteration)\n\n # fancy table updates\n if disp_level > 1:\n\n # get the metadata from this iteration\n data = valmap(last, self.metadata)\n\n # choose what keys to use\n keys = ['Time (s)', 'Primal resid', 'Dual resid', 'rho']\n\n # initial update. print out table headers\n if iteration == 1:\n print(tableprint.header(keys, width=col_width))\n\n # print data\n print(tableprint.row([data[k] for k in keys], width=col_width, format_spec='4g'))\n\n if iteration == -1:\n print(tableprint.bottom(len(keys), width=col_width) + '\\n')\n\n # print convergence statement\n if iteration == -1 and self.converged:\n print('Converged after %i iterations!' % len(self.metadata['Primal resid']))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef susvd(x, x_obs, rho, penalties):\n\n assert type(x) == Tensor, \"Input array must be a Tensor\"\n\n while True:\n\n # proximal operator for the Fro. norm\n x = squared_error(x, rho, x_obs)\n\n # sequential singular value thresholding\n for ix, penalty in enumerate(penalties):\n x = x.unfold(ix).svt(penalty / rho).fold()\n\n yield x", "response": "Sequential unfolding SVD generator."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nbuilds the bytes from the python object.", "response": "def build(self, obj, context=None) -> bytes:\n \"\"\"\n Build bytes from the python object.\n\n :param obj: Python object to build bytes from.\n :param context: Optional context dictionary.\n \"\"\"\n stream = BytesIO()\n self.build_stream(obj, stream, context)\n return stream.getvalue()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses some python object from the data.", "response": "def parse(self, data: bytes, context=None):\n \"\"\"\n Parse some python object from the data.\n\n :param data: Data to be parsed.\n :param context: Optional context dictionary.\n \"\"\"\n stream = BytesIO(data)\n return self.parse_stream(stream, context)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_stream(self, obj, stream: BytesIO, context=None) -> None:\n if context is None:\n context = Context()\n if not isinstance(context, Context):\n context = Context(context)\n try:\n self._build_stream(obj, stream, context)\n except Error:\n raise\n except Exception as exc:\n raise BuildingError(str(exc))", "response": "Builds bytes from the python object into the stream."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse_stream(self, stream: BytesIO, context=None):\n if context is None:\n context = Context()\n if not isinstance(context, Context):\n context = Context(context)\n try:\n return self._parse_stream(stream, context)\n except Error:\n raise\n except Exception as exc:\n raise ParsingError(str(exc))", "response": "Parse some python object from the stream."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the size of the construct in bytes.", "response": "def sizeof(self, context=None) -> int:\n \"\"\"\n Return the size of the construct in bytes.\n\n :param context: Optional context dictionary.\n \"\"\"\n if context is None:\n context = Context()\n if not isinstance(context, Context):\n context = Context(context)\n try:\n return self._sizeof(context)\n except Error:\n raise\n except Exception as exc:\n raise SizeofError(str(exc))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef poissreg(x0, rho, x, y):\n\n # objective and gradient\n n = float(x.shape[0])\n f = lambda w: np.mean(np.exp(x.dot(w)) - y * x.dot(w))\n df = lambda w: (x.T.dot(np.exp(x.dot(w))) - x.T.dot(y)) / n\n\n # minimize via BFGS\n return bfgs(x0, rho, f, df)", "response": "Proximal operator for Poisson regression\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef smooth(x0, rho, gamma, axis=0):\n\n # Apply Laplacian smoothing\n n = x0.shape[axis]\n lap_op = spdiags([(2 + rho / gamma) * np.ones(n), -1 * np.ones(n), -1 * np.ones(n)], [0, -1, 1], n, n, format='csc')\n x_out = np.rollaxis(spsolve(gamma * lap_op, rho * np.rollaxis(x0, axis, 0)), axis, 0)\n\n return x_out", "response": "Proximal operator for a smoothing function enforced via the discrete laplacian operator\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef nucnorm(x0, rho, gamma):\n\n # compute SVD\n u, s, v = np.linalg.svd(x0, full_matrices=False)\n\n # soft threshold the singular values\n sthr = np.maximum(s - (gamma / float(rho)), 0)\n\n # reconstruct\n x_out = (u.dot(np.diag(sthr)).dot(v))\n\n return x_out", "response": "Proximal operator for the nuclear norm"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef tvd(x0, rho, gamma):\n try:\n from skimage.restoration import denoise_tv_bregman\n except ImportError:\n print('Error: scikit-image not found. TVD will not work.')\n return x0\n\n return denoise_tv_bregman(x0, rho / gamma)", "response": "Returns the TVD of the total variation of the TVD."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef sparse(x0, rho, gamma):\n\n lmbda = float(gamma) / rho\n\n return (x0 - lmbda) * (x0 >= lmbda) + (x0 + lmbda) * (x0 <= -lmbda)", "response": "This function computes the sparse operator for the l1 norm of the current node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef linsys(x0, rho, P, q):\n return np.linalg.solve(rho * np.eye(q.shape[0]) + P, rho * x0.copy() + q)", "response": "Proximal operator for the linear approximation of the function Ax = b."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef headerlist(self):\n if 'Content-Type' not in self.headers:\n self.headers.add_header('Content-Type', self.default_content_type)\n if self._cookies:\n for c in self._cookies.values():\n self.headers.add_header('Set-Cookie', c.OutputString())\n return self.headers.items()", "response": "WSGI conform list of ( header value ) tuples."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef assert_redirect(self, response, expected_url=None):\n self.assertIn(\n response.status_code,\n self.redirect_codes,\n self._get_redirect_assertion_message(response),\n )\n if expected_url:\n location_header = response._headers.get('location', None)\n self.assertEqual(\n location_header,\n ('Location', str(expected_url)),\n 'Response should redirect to {0}, but it redirects to {1} instead'.format(\n expected_url,\n location_header[1],\n )\n )", "response": "Assert that the response is redirected to the expected_url."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if a model instance was created in the database.", "response": "def assert_instance_created(self, model_class, **kwargs):\n \"\"\"\n Checks if a model instance was created in the database.\n\n For example::\n\n >>> with self.assert_instance_created(Article, slug='lorem-ipsum'):\n ... Article.objects.create(slug='lorem-ipsum')\n \"\"\"\n return _InstanceContext(\n self.assert_instance_does_not_exist,\n self.assert_instance_exists,\n model_class,\n **kwargs\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if the model instance was deleted from the database.", "response": "def assert_instance_deleted(self, model_class, **kwargs):\n \"\"\"\n Checks if the model instance was deleted from the database.\n\n For example::\n\n >>> with self.assert_instance_deleted(Article, slug='lorem-ipsum'):\n ... Article.objects.get(slug='lorem-ipsum').delete()\n \"\"\"\n return _InstanceContext(\n self.assert_instance_exists,\n self.assert_instance_does_not_exist,\n model_class,\n **kwargs\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _split_into_mimetype_and_priority(x):\n if ';' in x:\n content_type, priority = x.split(';')\n casted_priority = float(priority.split('=')[1])\n else:\n content_type, casted_priority = x, 1.0\n\n content_type = content_type.lstrip().rstrip() # Replace ' text/html' to 'text/html'\n return content_type, casted_priority", "response": "Split an accept header item into mimetype and priority."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses and sort the accept header items.", "response": "def _parse_and_sort_accept_header(accept_header):\n \"\"\"Parse and sort the accept header items.\n\n >>> _parse_and_sort_accept_header('application/json;q=0.5, text/*')\n [('text/*', 1.0), ('application/json', 0.5)]\n \"\"\"\n return sorted([_split_into_mimetype_and_priority(x) for x in accept_header.split(',')],\n key=lambda x: x[1], reverse=True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a mimetype best matched the accept headers.", "response": "def accept_best_match(accept_header, mimetypes):\n \"\"\"Return a mimetype best matched the accept headers.\n\n >>> accept_best_match('application/json, text/html', ['application/json', 'text/plain'])\n 'application/json'\n\n >>> accept_best_match('application/json;q=0.5, text/*', ['application/json', 'text/plain'])\n 'text/plain'\n \"\"\"\n for mimetype_pattern, _ in _parse_and_sort_accept_header(accept_header):\n matched_types = fnmatch.filter(mimetypes, mimetype_pattern)\n if matched_types:\n return matched_types[0]\n return mimetypes[0]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmatching types of url vars.", "response": "def match_url_vars_type(url_vars, type_hints):\n \"\"\" Match types of url vars.\n\n >>> match_url_vars_type({'user_id': '1'}, {'user_id': int})\n (True, {'user_id': 1})\n >>> match_url_vars_type({'user_id': 'foo'}, {'user_id': int})\n (False, {})\n \"\"\"\n typed_url_vars = {}\n try:\n for k, v in url_vars.items():\n arg_type = type_hints.get(k)\n if arg_type and arg_type != str:\n typed_url_vars[k] = arg_type(v)\n else:\n typed_url_vars[k] = v\n except ValueError:\n return False, {}\n return True, typed_url_vars"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef match_path(rule, path):\n split_rule = split_by_slash(rule)\n split_path = split_by_slash(path)\n url_vars = {}\n\n if len(split_rule) != len(split_path):\n return False, {}\n\n for r, p in zip(split_rule, split_path):\n if r.startswith('{') and r.endswith('}'):\n url_vars[r[1:-1]] = p\n continue\n if r != p:\n return False, {}\n return True, url_vars", "response": "Match a path to a rule."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef match(self, path, method):\n if path != '/':\n path = path.rstrip('/')\n method = method.upper()\n\n status = 404\n for p, n, m in self.endpoints:\n matched, url_vars = match_path(p, path)\n if not matched: # path: not matched\n continue\n\n if method not in m: # path: matched, method: not matched\n status = 405\n raise HTTPError(status=status, body=f'Method not found: {path} {method}') # it has security issue??\n\n callback, type_hints = m[method]\n type_matched, typed_url_vars = match_url_vars_type(url_vars, type_hints)\n if not type_matched:\n continue # path: not matched (types are different)\n return callback, typed_url_vars\n raise HTTPError(status=status, body=f'Not found: {path}')", "response": "Match the given path and method."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a new rule or replace an existing rule.", "response": "def add(self, rule, method, name, callback):\n \"\"\" Add a new rule or replace the target for an existing rule.\n\n >>> from kobin import Response\n >>> r = Router()\n >>> def view(user_id: int) -> Response:\n ... return Response(f'You are {user_id}')\n ...\n >>> r.add('/users/{user_id}', 'GET', 'user-detail', view)\n >>> path, name, methods = r.endpoints[0]\n >>> path\n '/users/{user_id}'\n >>> name\n 'user-detail'\n >>> callback, type_hints = methods['GET']\n >>> view == callback\n True\n >>> type_hints['user_id'] == int\n True\n \"\"\"\n if rule != '/':\n rule = rule.rstrip('/')\n method = method.upper()\n\n for i, e in enumerate(self.endpoints):\n r, n, callbacks = e\n if r == rule:\n assert name == n and n is not None, (\n \"A same path should set a same name for reverse routing.\"\n )\n callbacks[method] = (callback, get_type_hints(callback))\n self.endpoints[i] = (r, name, callbacks)\n break\n else:\n e = (rule, name, {method: (callback, get_type_hints(callback))})\n self.endpoints.append(e)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreversing routing. >>> from kobin import Response >>> r = Router() >>> def view(user_id: int) -> Response: ... return Response(f'You are {user_id}') ... >>> r.add('/users/{user_id}', 'GET', 'user-detail', view) >>> r.reverse('user-detail', user_id=1) '/users/1'", "response": "def reverse(self, name, **kwargs):\n \"\"\" Reverse routing.\n\n >>> from kobin import Response\n >>> r = Router()\n >>> def view(user_id: int) -> Response:\n ... return Response(f'You are {user_id}')\n ...\n >>> r.add('/users/{user_id}', 'GET', 'user-detail', view)\n >>> r.reverse('user-detail', user_id=1)\n '/users/1'\n \"\"\"\n for p, n, _ in self.endpoints:\n if name == n:\n return p.format(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set(self, value):\n '''\n Atomically sets the value to `value`.\n\n :param value: The value to set.\n '''\n with self._lock.exclusive:\n self._value = value\n return value", "response": "Atomically sets the value to value."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_and_set(self, value):\n '''\n Atomically sets the value to `value` and returns the old value.\n\n :param value: The value to set.\n '''\n with self._lock.exclusive:\n oldval = self._value\n self._value = value\n return oldval", "response": "Atomically sets the value to value and returns the old value."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncompares the current value of the current object with the expected value and set the value to update if the current value is equal to the expect value.", "response": "def compare_and_set(self, expect, update):\n '''\n Atomically sets the value to `update` if the current value is equal to\n `expect`.\n\n :param expect: The expected current value.\n :param update: The value to set if and only if `expect` equals the\n current value.\n '''\n with self._lock.exclusive:\n if self._value == expect:\n self._value = update\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_and_get(self, delta):\n '''\n Atomically adds `delta` to the current value.\n\n :param delta: The delta to add.\n '''\n with self._lock.exclusive:\n self._value += delta\n return self._value", "response": "Atomically adds delta to the current value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_and_add(self, delta):\n '''\n Atomically adds `delta` to the current value and returns the old value.\n\n :param delta: The delta to add.\n '''\n with self._lock.exclusive:\n oldval = self._value\n self._value += delta\n return oldval", "response": "Atomically adds delta to the current value and returns the old value."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsubtract delta from the current value and returns the old value.", "response": "def subtract_and_get(self, delta):\n '''\n Atomically subtracts `delta` from the current value.\n\n :param delta: The delta to subtract.\n '''\n with self._lock.exclusive:\n self._value -= delta\n return self._value"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nnotifies all the watches that a new value has changed.", "response": "def notify_watches(self, oldval, newval):\n '''\n Passes `oldval` and `newval` to each `fn` in the watches dictionary,\n passing along its respective key and the reference to this object.\n\n :param oldval: The old value which will be passed to the watch.\n :param newval: The new value which will be passed to the watch.\n '''\n watches = self._watches.copy()\n for k in watches:\n fn = watches[k]\n if isinstance(fn, collections.Callable):\n fn(k, self, oldval, newval)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef swap(self, fn, *args, **kwargs):\n '''\n Given a mutator `fn`, calls `fn` with the atom's current state, `args`,\n and `kwargs`. The return value of this invocation becomes the new value\n of the atom. Returns the new value.\n\n :param fn: A function which will be passed the current state. Should\n return a new state. This absolutely *MUST NOT* mutate the\n reference to the current state! If it does, this function may loop\n indefinitely.\n :param \\*args: Arguments to be passed to `fn`.\n :param \\*\\*kwargs: Keyword arguments to be passed to `fn`.\n '''\n while True:\n oldval = self.deref()\n newval = fn(oldval, *args, **kwargs)\n if self._state.compare_and_set(oldval, newval):\n self.notify_watches(oldval, newval)\n return newval", "response": "Given a mutator fn calls the atom s current state with the current state args and kwargs returns the new value."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef reset(self, newval):\n '''\n Resets the atom's value to `newval`, returning `newval`.\n\n :param newval: The new value to set.\n '''\n oldval = self._state.get()\n self._state.set(newval)\n self.notify_watches(oldval, newval)\n return newval", "response": "Resets the atom s value to newval returning the newval."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef compare_and_set(self, oldval, newval):\n '''\n Given `oldval` and `newval`, sets the atom's value to `newval` if and\n only if `oldval` is the atom's current value. Returns `True` upon\n success, otherwise `False`.\n\n :param oldval: The old expected value.\n :param newval: The new value which will be set if and only if `oldval`\n equals the current value.\n '''\n ret = self._state.compare_and_set(oldval, newval)\n if ret:\n self.notify_watches(oldval, newval)\n\n return ret", "response": "Given oldval and newval sets the atom s value to newval if and only if and\n equals the current value. Returns True upon success otherwise False."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompare the current value of the current key with the expected value and set the value to update if the current value is equal to the expect value.", "response": "def compare_and_set(self, expect, update):\n '''\n Atomically sets the value to `update` if the current value is equal to\n `expect`.\n\n :param expect: The expected current value.\n :param update: The value to set if and only if `expect` equals the\n current value.\n '''\n with self._reference.get_lock():\n if self._reference.value == expect:\n self._reference.value = update\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_and_get(self, delta):\n '''\n Atomically adds `delta` to the current value.\n\n :param delta: The delta to add.\n '''\n with self._reference.get_lock():\n self._reference.value += delta\n return self._reference.value", "response": "Atomically adds delta to the current value and returns the new value."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef subtract_and_get(self, delta):\n '''\n Atomically subtracts `delta` from the current value.\n\n :param delta: The delta to subtract.\n '''\n with self._reference.get_lock():\n self._reference.value -= delta\n return self._reference.value", "response": "Atomically subtracts delta from the current value and returns the new value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_and_subtract(self, delta):\n '''\n Atomically subtracts `delta` from the current value and returns the\n old value.\n\n :param delta: The delta to subtract.\n '''\n with self._reference.get_lock():\n oldval = self._reference.value\n self._reference.value -= delta\n return oldval", "response": "Atomically subtracts delta from the current value and returns the old value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef translate_zxcvbn_text(text):\n i18n = {\n \"Use a few words, avoid common phrases\": _(\n \"Use a few words, avoid common phrases\"\n ),\n \"No need for symbols, digits, or uppercase letters\": _(\n \"No need for symbols, digits, or uppercase letters\"\n ),\n \"Add another word or two. Uncommon words are better.\": _(\n \"Add another word or two. Uncommon words are better.\"\n ),\n \"Straight rows of keys are easy to guess\": _(\n \"Straight rows of keys are easy to guess\"\n ),\n \"Short keyboard patterns are easy to guess\": _(\n \"Short keyboard patterns are easy to guess\"\n ),\n \"Use a longer keyboard pattern with more turns\": _(\n \"Use a longer keyboard pattern with more turns\"\n ),\n 'Repeats like \"aaa\" are easy to guess': _(\n 'Repeats like \"aaa\" are easy to guess'\n ),\n 'Repeats like \"abcabcabc\" are only slightly harder to guess than \"abc\"': _(\n 'Repeats like \"abcabcabc\" are only slightly harder to guess than \"abc\"'\n ),\n \"Avoid repeated words and characters\": _(\"Avoid repeated words and characters\"),\n 'Sequences like \"abc\" or \"6543\" are easy to guess': _(\n 'Sequences like \"abc\" or \"6543\" are easy to guess'\n ),\n \"Avoid sequences\": _(\"Avoid sequences\"),\n \"Recent years are easy to guess\": _(\"Recent years are easy to guess\"),\n \"Avoid recent years\": _(\"Avoid recent years\"),\n \"Avoid years that are associated with you\": _(\n \"Avoid years that are associated with you\"\n ),\n \"Dates are often easy to guess\": _(\"Dates are often easy to guess\"),\n \"Avoid dates and years that are associated with you\": _(\n \"Avoid dates and years that are associated with you\"\n ),\n \"This is a top-10 common password\": _(\"This is a top-10 common password\"),\n \"This is a top-100 common password\": _(\"This is a top-100 common password\"),\n \"This is a very common password\": _(\"This is a very common password\"),\n \"This is similar to a commonly used password\": _(\n \"This is similar to a commonly used password\"\n ),\n \"A word by itself is easy to guess\": _(\"A word by itself is easy to guess\"),\n \"Names and surnames by themselves are easy to guess\": _(\n \"Names and surnames by themselves are easy to guess\"\n ),\n \"Common names and surnames are easy to guess\": _(\n \"Common names and surnames are easy to guess\"\n ),\n \"Capitalization doesn't help very much\": _(\n \"Capitalization doesn't help very much\"\n ),\n \"All-uppercase is almost as easy to guess as all-lowercase\": _(\n \"All-uppercase is almost as easy to guess as all-lowercase\"\n ),\n \"Reversed words aren't much harder to guess\": _(\n \"Reversed words aren't much harder to guess\"\n ),\n \"Predictable substitutions like '@' instead of 'a' don't help very much\": _(\n \"Predictable substitutions like '@' instead of 'a' don't help very much\"\n ),\n }\n translated_text = i18n.get(text)\n if translated_text is None:\n # zxcvbn is inconsistent, sometime there is a dot, sometime not\n translated_text = i18n.get(text[:-1])\n if translated_text is None:\n LOGGER.warning(\n \"No translation for '%s' or '%s', update the generatei18ndict command.\",\n text,\n text[:-1],\n )\n return text\n return translated_text", "response": "Translate the text into a zxcvbn language."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntrying to find conda on the system", "response": "def find_conda():\n \"\"\" Try to find conda on the system \"\"\"\n USER_HOME = os.path.expanduser('~')\n CONDA_HOME = os.environ.get('CONDA_HOME', '')\n PROGRAMDATA = os.environ.get('PROGRAMDATA', '')\n\n # Search common install paths and sys path\n search_paths = [\n # Windows\n join(PROGRAMDATA, 'miniconda2', 'scripts'),\n join(PROGRAMDATA, 'miniconda3', 'scripts'),\n join(USER_HOME, 'miniconda2', 'scripts'),\n join(USER_HOME, 'miniconda3', 'scripts'),\n join(CONDA_HOME, 'scripts'),\n\n # Linux\n join(USER_HOME, 'miniconda2', 'bin'),\n join(USER_HOME, 'miniconda3', 'bin'),\n join(CONDA_HOME, 'bin'),\n # TODO: OSX\n ] + os.environ.get(\"PATH\", \"\").split(\";\" if 'win' in sys.path else \":\")\n\n cmd = 'conda.exe' if IS_WIN else 'conda'\n for conda_path in search_paths:\n conda = join(conda_path, cmd)\n if exists(conda):\n return sh.Command(conda)\n\n # Try to let the system find it\n return sh.conda"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef cp(src, dst):\n print(\"[DEBUG]: -> copying {} to {}\".format(src, dst))\n if os.path.isfile(src):\n if not exists(dirname(dst)):\n os.makedirs(dirname(dst))\n shutil.copy(src, dst)\n else:\n copy_tree(src, dst)", "response": "Like cp - R src dst"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nfind all commands of the given class.", "response": "def find_commands(cls):\n \"\"\" Finds commands by finding the subclasses of Command\"\"\"\n cmds = []\n for subclass in cls.__subclasses__():\n cmds.append(subclass)\n cmds.extend(find_commands(subclass))\n return cmds"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef link(self, path, pkg):\n # Check if a custom linker exists to handle linking this package\n #for ep in pkg_resources.iter_entry_points(group=\"enaml_native_linker\"):\n # if ep.name.replace(\"-\", '_') == pkg.replace(\"-\", '_'):\n # linker = ep.load()\n # print(\"Custom linker {} found for '{}'. Linking...\".format(\n # linker, pkg))\n # if linker(self.ctx, path):\n # return\n\n #: Use the default builtin linker script\n if exists(join(path, pkg, 'build.gradle')):\n print(Colors.BLUE+\"[INFO] Linking {}/build.gradle\".format(\n pkg)+Colors.RESET)\n self.link_android(path, pkg)\n if exists(join(path, pkg, 'Podfile')):\n print(Colors.BLUE+\"[INFO] Linking {}/Podfile\".format(\n pkg)+Colors.RESET)\n self.link_ios(path, pkg)", "response": "Link the package in the current directory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_settings_linked(source, pkg):\n for line in source.split(\"\\n\"):\n if re.search(r\"include\\s*['\\\"]:{}['\\\"]\".format(pkg), line):\n return True\n return False", "response": "Returns true if the include line in the file \n is linked to the specified package."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_build_linked(source, pkg):\n for line in source.split(\"\\n\"):\n if re.search(r\"(api|compile)\\s+project\\(['\\\"]:{}['\\\"]\\)\".format(pkg),\n line):\n return True\n return False", "response": "Returns true if the build is linked to the specified package"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfinding all java files matching the \"*Package. java\" pattern within the given enaml package directory relative to the java source path.", "response": "def find_packages(path):\n \"\"\" Find all java files matching the \"*Package.java\" pattern within\n the given enaml package directory relative to the java source path.\n \"\"\"\n matches = []\n root = join(path, 'src', 'main', 'java')\n for folder, dirnames, filenames in os.walk(root):\n for filename in fnmatch.filter(filenames, '*Package.java'):\n #: Open and make sure it's an EnamlPackage somewhere\n with open(join(folder, filename)) as f:\n if \"implements EnamlPackage\" in f.read():\n package = os.path.relpath(folder, root)\n matches.append(os.path.join(package, filename))\n return matches"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_app_linked(source, pkg, java_package):\n for line in source.split(\"\\n\"):\n if java_package in line:\n return True\n return False", "response": "Returns true if the compile project line exists in the file \n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nlinking the android project to the new package", "response": "def link_android(self, path, pkg):\n \"\"\" Link's the android project to this library.\n\n 1. Includes this project's directory in the app's \n android/settings.gradle\n It adds:\n include ':'\n project(':').projectDir = new File(\n rootProject.projectDir, '../packages//android')\n\n 2. Add's this project as a dependency to the android/app/build.gradle\n It adds:\n compile project(':')\n to the dependencies.\n\n 3. If preset, adds the import and package statement\n to the android/app/src/main/java//MainApplication.java\n\n \"\"\"\n\n bundle_id = self.ctx['bundle_id']\n pkg_root = join(path, pkg)\n\n #: Check if it's already linked\n with open(join('android', 'settings.gradle')) as f:\n settings_gradle = f.read()\n with open(join('android', 'app', 'build.gradle')) as f:\n build_gradle = f.read()\n\n #: Find the MainApplication.java\n main_app_java_path = join('android', 'app', 'src', 'main', 'java',\n join(*bundle_id.split(\".\")),\n 'MainApplication.java')\n with open(main_app_java_path) as f:\n main_application_java = f.read()\n\n try:\n #: Now link all the EnamlPackages we can find in the new \"package\"\n new_packages = Link.find_packages(join(path, pkg))\n if not new_packages:\n print(\"[Android] {} No EnamlPackages found to link!\".format(\n pkg))\n return\n\n #: Link settings.gradle\n if not Link.is_settings_linked(settings_gradle, pkg):\n #: Add two statements\n new_settings = settings_gradle.split(\"\\n\")\n new_settings.append(\"\") # Blank line\n new_settings.append(\"include ':{name}'\".format(name=pkg))\n new_settings.append(\"project(':{name}').projectDir = \"\n \"new File(rootProject.projectDir, \"\n \"'../{path}/android/{name}')\"\n .format(name=pkg, path=self.package_dir))\n\n with open(join('android', 'settings.gradle'), 'w') as f:\n f.write(\"\\n\".join(new_settings))\n print(\"[Android] {} linked in settings.gradle!\".format(pkg))\n else:\n print(\"[Android] {} was already linked in \"\n \"settings.gradle!\".format(pkg))\n\n #: Link app/build.gradle\n if not Link.is_build_linked(build_gradle, pkg):\n #: Add two statements\n new_build = build_gradle.split(\"\\n\")\n\n #: Find correct line number\n found = False\n for i, line in enumerate(new_build):\n if re.match(r\"dependencies\\s*{\", line):\n found = True\n continue\n if found and \"}\" in line:\n #: Hackish way to find line of the closing bracket after\n #: the dependencies { block is found\n break\n if not found:\n raise ValueError(\"Unable to find dependencies in \"\n \"{pkg}/app/build.gradle!\".format(pkg=pkg))\n\n #: Insert before the closing bracket\n new_build.insert(i, \" api project(':{name}')\".format(\n name=pkg))\n\n with open(join('android', 'app', 'build.gradle'), 'w') as f:\n f.write(\"\\n\".join(new_build))\n print(\"[Android] {} linked in app/build.gradle!\".format(pkg))\n else:\n print(\"[Android] {} was already linked in \"\n \"app/build.gradle!\".format(pkg))\n\n new_app_java = []\n for package in new_packages:\n #: Add our import statement\n javacls = os.path.splitext(package)[0].replace(\"/\", \".\")\n\n if not Link.is_app_linked(main_application_java, pkg, javacls):\n #: Reuse previous if avialable\n new_app_java = (new_app_java or\n main_application_java.split(\"\\n\"))\n\n #: Find last import statement\n j = 0\n for i, line in enumerate(new_app_java):\n if fnmatch.fnmatch(line, \"import *;\"):\n j = i\n\n new_app_java.insert(j+1, \"import {};\".format(javacls))\n\n #: Add the package statement\n j = 0\n for i, line in enumerate(new_app_java):\n if fnmatch.fnmatch(line.strip(), \"new *Package()\"):\n j = i\n if j == 0:\n raise ValueError(\"Could not find the correct spot to \"\n \"add package {}\".format(javacls))\n else:\n #: Get indent and add to previous line\n #: Add comma to previous line\n new_app_java[j] = new_app_java[j]+ \",\"\n\n #: Insert new line\n new_app_java.insert(j+1, \" new {}()\"\n .format(javacls.split(\".\")[-1]))\n\n else:\n print(\"[Android] {} was already linked in {}!\".format(\n pkg, main_app_java_path))\n\n if new_app_java:\n with open(main_app_java_path, 'w') as f:\n f.write(\"\\n\".join(new_app_java))\n\n print(Colors.GREEN+\"[Android] {} linked successfully!\".format(\n pkg)+Colors.RESET)\n except Exception as e:\n print(Colors.GREEN+\"[Android] {} Failed to link. \"\n \"Reverting due to error: \"\n \"{}\".format(pkg, e)+Colors.RESET)\n\n #: Undo any changes\n with open(join('android', 'settings.gradle'), 'w') as f:\n f.write(settings_gradle)\n with open(join('android', 'app', 'build.gradle'), 'w') as f:\n f.write(build_gradle)\n with open(main_app_java_path, 'w') as f:\n f.write(main_application_java)\n\n #: Now blow up\n raise"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run(self, args=None):\n print(Colors.BLUE+\"[INFO] Unlinking {}...\".format(\n args.names)+Colors.RESET)\n for name in args.names:\n self.unlink(Link.package_dir, name)", "response": "Unlink the specified resource classes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef unlink(self, path, pkg):\n #: Check if a custom unlinker exists to handle unlinking this package\n for ep in pkg_resources.iter_entry_points(\n group=\"enaml_native_unlinker\"):\n if ep.name.replace(\"-\", '_') == pkg.replace(\"-\", '_'):\n unlinker = ep.load()\n print(\"Custom unlinker {} found for '{}'. \"\n \"Unlinking...\".format(unlinker, pkg))\n if unlinker(self.ctx, path):\n return\n\n if exists(join(path, 'android', pkg, 'build.gradle')):\n print(\"[Android] unlinking {}\".format(pkg))\n self.unlink_android(path, pkg)\n\n for target in ['iphoneos', 'iphonesimulator']:\n if exists(join(path, target, pkg, 'Podfile')):\n print(\"[iOS] unlinking {}\".format(pkg))\n self.unlink_ios(path, pkg)", "response": "Unlink the package in the current directory."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef run_tornado(self, args):\n server = self\n import tornado.ioloop\n import tornado.web\n import tornado.websocket\n\n ioloop = tornado.ioloop.IOLoop.current()\n\n class DevWebSocketHandler(tornado.websocket.WebSocketHandler):\n def open(self):\n super(DevWebSocketHandler, self).open()\n server.on_open(self)\n\n def on_message(self, message):\n server.on_message(self, message)\n\n def on_close(self):\n super(DevWebSocketHandler, self).on_close()\n server.on_close(self)\n\n class MainHandler(tornado.web.RequestHandler):\n def get(self):\n self.write(server.index_page)\n\n #: Set the call later method\n server.call_later = ioloop.call_later\n server.add_callback = ioloop.add_callback\n\n app = tornado.web.Application([\n (r\"/\", MainHandler),\n (r\"/dev\", DevWebSocketHandler),\n ])\n\n app.listen(self.port)\n print(\"Tornado Dev server started on {}\".format(self.port))\n ioloop.start()", "response": "Tornado dev server implementation"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run_twisted(self, args):\n server = self\n\n from twisted.internet import reactor\n from twisted.web import resource\n from twisted.web.static import File\n from twisted.web.server import Site\n from autobahn.twisted.websocket import (WebSocketServerFactory,\n WebSocketServerProtocol)\n from autobahn.twisted.resource import WebSocketResource\n\n class DevWebSocketHandler(WebSocketServerProtocol):\n def onConnect(self, request):\n super(DevWebSocketHandler, self).onConnect(request)\n server.on_open(self)\n\n def onMessage(self, payload, isBinary):\n server.on_message(self, payload)\n\n def onClose(self, wasClean, code, reason):\n super(DevWebSocketHandler,self).onClose(wasClean, code, reason)\n server.on_close(self)\n\n def write_message(self, message, binary=False):\n self.sendMessage(message, binary)\n\n #: Set the call later method\n server.call_later = reactor.callLater\n server.add_callback = reactor.callFromThread\n\n factory = WebSocketServerFactory(u\"ws://0.0.0.0:{}\".format(self.port))\n factory.protocol = DevWebSocketHandler\n\n class MainHandler(resource.Resource):\n def render_GET(self, req):\n return str(server.index_page)\n\n root = resource.Resource()\n root.putChild(\"\", MainHandler())\n root.putChild(\"dev\", WebSocketResource(factory))\n reactor.listenTCP(self.port, Site(root))\n print(\"Twisted Dev server started on {}\".format(self.port))\n reactor.run()", "response": "Twisted dev server implementation"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef on_message(self, handler, msg):\n if self.remote_debugging:\n #: Forward to other clients\n for h in self.handlers:\n if h != handler:\n h.write_message(msg, True)\n else:\n print(msg)", "response": "Called when a message is received from the remote client."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsending a message to the client.", "response": "def send_message(self, msg):\n \"\"\" Send a message to the client. This should not be used in\n remote debugging mode.\n \n \"\"\"\n if not self.handlers:\n return #: Client not connected\n for h in self.handlers:\n h.write_message(msg)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbuilding the list of CLI commands by finding subclasses of the Command class and then loading them into the appropriate list of Command subclasses.", "response": "def _default_commands(self):\n \"\"\" Build the list of CLI commands by finding subclasses of the Command \n class\n\n Also allows commands to be installed using the \"enaml_native_command\" \n entry point. This entry point should return a Command subclass\n\n \"\"\"\n commands = [c() for c in find_commands(Command)]\n\n #: Get commands installed via entry points\n for ep in pkg_resources.iter_entry_points(\n group=\"enaml_native_command\"):\n c = ep.load()\n if not issubclass(c, Command):\n print(\"Warning: entry point {} did not return a valid enaml \"\n \"cli command! This command will be ignored!\".format(\n ep.name))\n commands.append(c())\n\n return commands"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _default_ctx(self):\n if not self.in_app_directory:\n print(\"Warning: {} does not exist. Using the default.\".format(\n self.package))\n ctx = {}\n\n else:\n with open(self.package) as f:\n ctx = dict(yaml.load(f, Loader=yaml.RoundTripLoader))\n\n if self.in_app_directory:\n # Update the env for each platform\n excluded = list(ctx.get('excluded', []))\n\n for env in [ctx['ios'], ctx['android']]:\n if 'python_build_dir' not in env:\n env['python_build_dir'] = expanduser(abspath('build/python'))\n if 'conda_prefix' not in env:\n env['conda_prefix'] = os.environ.get(\n 'CONDA_PREFIX', expanduser(abspath('venv')))\n\n # Join the shared and local exclusions\n env['excluded'] = list(env.get('excluded', [])) + excluded\n\n return ctx", "response": "Return the package config or context and normalize some of the \n values \n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates a parser using the command list", "response": "def _default_parser(self):\n \"\"\" Generate a parser using the command list \"\"\"\n parser = ArgumentParser(prog='enaml-native')\n\n #: Build commands by name\n cmds = {c.title: c for c in self.commands}\n\n #: Build parser, prepare commands\n subparsers = parser.add_subparsers()\n for c in self.commands:\n p = subparsers.add_parser(c.title, help=c.help)\n c.parser = p\n for (flags, kwargs) in c.args:\n p.add_argument(*flags.split(), **kwargs)\n p.set_defaults(cmd=c)\n c.ctx = self.ctx\n c.cmds = cmds\n c.cli = self\n\n return parser"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef find_data(folder):\n for (path, directories, filenames) in os.walk(folder):\n for filename in filenames:\n yield os.path.join('..', path, filename)", "response": "Find all the data in the folder."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef generate(number=4, choice=SystemRandom().choice, words=words, joiner=\" \"):\n return joiner.join(choice(words) for each in range(number))", "response": "Generate a random passphrase from the GSL."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef redivmod(initial_value, factors):\n result = []\n value = initial_value\n for divisor, label in factors:\n if not divisor:\n remainder = value\n if not remainder:\n break\n else:\n value, remainder = divmod(value, divisor)\n if not value and not remainder:\n break\n if remainder == 1:\n # depluralize\n label = label[:-1]\n if six.PY2:\n addition = unicode(remainder) + ' ' + unicode(label)\n else:\n addition = str(remainder) + ' ' + str(label)\n result.insert(0, addition)\n if len(result) > 1:\n result[-1] = \"and \" + result[-1]\n if result:\n return ', '.join(result)\n else:\n return \"instantly\"", "response": "Redivant the value of a single element of a sequence according to the list of factors and return a string."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nzip two sequences into a new set of points", "response": "def zip(self, other):\n \"\"\"\n zips two sequences unifying the corresponding points.\n \"\"\"\n return self.__class__(p1 % p2 for p1, p2 in zip(self, other))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndisplays the sequence in a specific format.", "response": "def display(self, format=\"png\"):\n \"\"\"\n Return an object that can be used to display this sequence.\n This is used for IPython Notebook.\n\n :param format: \"png\" or \"svg\"\n \"\"\"\n from sebastian.core.transforms import lilypond\n seq = HSeq(self) | lilypond()\n\n lily_output = write_lilypond.lily_format(seq)\n if not lily_output.strip():\n #In the case of empty lily outputs, return self to get a textual display\n return self\n\n if format == \"png\":\n suffix = \".preview.png\"\n args = [\"lilypond\", \"--png\", \"-dno-print-pages\", \"-dpreview\"]\n elif format == \"svg\":\n suffix = \".preview.svg\"\n args = [\"lilypond\", \"-dbackend=svg\", \"-dno-print-pages\", \"-dpreview\"]\n\n f = tempfile.NamedTemporaryFile(suffix=suffix)\n basename = f.name[:-len(suffix)]\n args.extend([\"-o\" + basename, \"-\"])\n\n #Pass shell=True so that if your $PATH contains ~ it will\n #get expanded. This also changes the way the arguments get\n #passed in. To work correctly, pass them as a string\n p = sp.Popen(\" \".join(args), stdin=sp.PIPE, shell=True)\n stdout, stderr = p.communicate(\"{ %s }\" % lily_output)\n if p.returncode != 0:\n # there was an error\n #raise IOError(\"Lilypond execution failed: %s%s\" % (stdout, stderr))\n return None\n\n if not ipython:\n return f.read()\n if format == \"png\":\n return Image(data=f.read(), filename=f.name, format=\"png\")\n else:\n return SVG(data=f.read(), filename=f.name)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef append(self, point):\n point = Point(point)\n self._elements.append(point)", "response": "Appends a copy of the given point to this sequence\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrepeats the sequence given number of times to produce a new sequence", "response": "def repeat(self, count):\n \"\"\"\n repeat sequence given number of times to produce a new sequence\n \"\"\"\n x = HSeq()\n for i in range(count):\n x = x.concatenate(self)\n return x"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef subseq(self, start_offset=0, end_offset=None):\n from sebastian.core import DURATION_64\n\n def subseq_iter(start_offset, end_offset):\n cur_offset = 0\n for point in self._elements:\n try:\n cur_offset += point[DURATION_64]\n except KeyError:\n raise ValueError(\"HSeq.subseq requires all points to have a %s attribute\" % DURATION_64)\n #Skip until start\n if cur_offset < start_offset:\n continue\n\n #Yield points start_offset <= point < end_offset\n if end_offset is None or cur_offset < end_offset:\n yield point\n else:\n raise StopIteration\n return HSeq(subseq_iter(start_offset, end_offset))", "response": "Return a subset of the sequence."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef arpeggio(pattern, point):\n point['sequence'] = HSeq(point['sequence'][i] for i in pattern)\n return point", "response": "Takes each subsequence into an arpeggio matching the given pattern."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfilling the subsequence of the point with repetitions of its subsequence and sets the duration of each point.", "response": "def fill(duration, point):\n \"\"\"\n fills the subsequence of the point with repetitions of its subsequence and\n sets the ``duration`` of each point.\n \"\"\"\n point['sequence'] = point['sequence'] * (point[DURATION_64] / (8 * duration)) | add({DURATION_64: duration})\n return point"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexpand a tree of sequences into a single flat sequence recursively.", "response": "def expand(sequence):\n \"\"\"\n expands a tree of sequences into a single, flat sequence, recursively.\n \"\"\"\n expanse = []\n for point in sequence:\n if 'sequence' in point:\n expanse.extend(expand(point['sequence']))\n else:\n expanse.append(point)\n return sequence.__class__(expanse)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a new sequence with only the information that is available to the user.", "response": "def debug(sequence):\n \"\"\"\n adds information to the sequence for better debugging, currently only\n an index property on each point in the sequence.\n \"\"\"\n points = []\n for i, p in enumerate(sequence):\n copy = Point(p)\n copy['index'] = i\n points.append(copy)\n return sequence.__class__(points)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a portion of the input sequence", "response": "def subseq(start_offset=0, end_offset=None):\n \"\"\"\n Return a portion of the input sequence\n \"\"\"\n def _(sequence):\n return sequence.subseq(start_offset, end_offset)\n return _"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef lilypond(point):\n #If lilypond already computed, leave as is\n if \"lilypond\" in point:\n return point\n\n #Defaults:\n pitch_string = \"\"\n octave_string = \"\"\n duration_string = \"\"\n preamble = \"\"\n dynamic_string = \"\"\n if \"pitch\" in point:\n octave = point[\"octave\"]\n pitch = point[\"pitch\"]\n if octave > 4:\n octave_string = \"'\" * (octave - 4)\n elif octave < 4:\n octave_string = \",\" * (4 - octave)\n else:\n octave_string = \"\"\n m = modifiers(pitch)\n if m > 0:\n modifier_string = \"is\" * m\n elif m < 0:\n modifier_string = \"es\" * -m\n else:\n modifier_string = \"\"\n pitch_string = letter(pitch).lower() + modifier_string\n\n if DURATION_64 in point:\n duration = point[DURATION_64]\n if duration > 0:\n if duration % 3 == 0: # dotted note\n duration_string = str(192 // (2 * duration)) + \".\"\n else:\n duration_string = str(64 // duration)\n #TODO: for now, if we have a duration but no pitch, show a 'c' with an x note\n if duration_string:\n if not pitch_string:\n pitch_string = \"c\"\n octave_string = \"'\"\n preamble = r'\\xNote '\n\n if \"dynamic\" in point:\n dynamic = point[\"dynamic\"]\n if dynamic == \"crescendo\":\n dynamic_string = \"\\<\"\n elif dynamic == \"diminuendo\":\n dynamic_string = \"\\>\"\n else:\n dynamic_string = \"\\%s\" % (dynamic,)\n\n point[\"lilypond\"] = \"%s%s%s%s%s\" % (preamble, pitch_string, octave_string, duration_string, dynamic_string)\n\n return point", "response": "Generates a lilypond representation of a point."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef dynamics(start, end=None):\n def _(sequence):\n if start in _dynamic_markers_to_velocity:\n start_velocity = _dynamic_markers_to_velocity[start]\n start_marker = start\n else:\n raise ValueError(\"Unknown start dynamic: %s, must be in %s\" % (start, _dynamic_markers_to_velocity.keys()))\n\n if end is None:\n end_velocity = start_velocity\n end_marker = start_marker\n elif end in _dynamic_markers_to_velocity:\n end_velocity = _dynamic_markers_to_velocity[end]\n end_marker = end\n else:\n raise ValueError(\"Unknown end dynamic: %s, must be in %s\" % (start, _dynamic_markers_to_velocity.keys()))\n\n retval = sequence.__class__([Point(point) for point in sequence._elements])\n\n velocity_interval = (float(end_velocity) - float(start_velocity)) / (len(retval) - 1) if len(retval) > 1 else 0\n velocities = [int(start_velocity + velocity_interval * pos) for pos in range(len(retval))]\n\n # insert dynamics markers for lilypond\n if start_velocity > end_velocity:\n retval[0][\"dynamic\"] = \"diminuendo\"\n retval[-1][\"dynamic\"] = end_marker\n elif start_velocity < end_velocity:\n retval[0][\"dynamic\"] = \"crescendo\"\n retval[-1][\"dynamic\"] = end_marker\n else:\n retval[0][\"dynamic\"] = start_marker\n\n for point, velocity in zip(retval, velocities):\n point[\"velocity\"] = velocity\n\n return retval\n return _", "response": "Returns a new sequence with dynamics applied to the specified start and end notes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef user_blocks(user_text, session):\n logger.debug(\"Getting blocks for {0}\".format(user_text))\n doc = session.get(action='query', list='blocks', bkusers=user_text,\n bkprop=['id', 'timestamp'])\n return [mwtypes.Timestamp(b['timestamp']) for b in doc['query']['blocks']]", "response": "Returns a list of blocks for a single user"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a list of blocks for a single user", "response": "def get_user_blocks(session, user_text):\n \"\"\"\n Returns a list of blocks for a single user\n \"\"\"\n logger.debug(\"Getting user_blocks for {0}\".format(user_text))\n doc = session.get(action='query', list='blocks', bkusers=user_text,\n bkprop=['id'])\n return doc['query']['blocks']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting a set of revisions by their IDs by repeatedly querying in batches.", "response": "def query_revisions_by_revids(session, revids, **params):\n \"\"\"\n Gets a set of revisions by their IDs by repeatedly querying in batches.\n If an ID cannot be found, it is ignored.\n \"\"\"\n doc = session.get(action='query', prop='revisions',\n revids=revids, **params)\n\n for page_doc in doc['query'].get('pages', {}).values():\n revisions = page_doc.get('revisions', [])\n if 'revisions' in page_doc:\n del page_doc['revisions']\n\n for revision_doc in revisions:\n revision_doc['page'] = page_doc\n yield revision_doc"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating a new tree structure from the given template.", "response": "def generate(variables, templates_path, main_template):\n \"\"\"\n :Parameters:\n variables : dict\n Template parameters, passed through.\n templates_path : str\n Root directory for transclusions.\n main_template : str\n Contents of the main template.\n\n Returns the rendered output.\n \"\"\"\n\n env = jinja2.Environment(\n loader=jinja2.FileSystemLoader(templates_path),\n lstrip_blocks=True,\n trim_blocks=True\n )\n\n def norm_alg_filename(alg_name):\n if alg_name in variables['globals']['algorithm_filename_parts']:\n return variables['globals']['algorithm_filename_parts'][alg_name]\n else:\n raise KeyError(\"{0} not found in globals.algorithm_filename_parts\"\n .format(alg_name))\n\n env.globals.update(norm_alg_filename=norm_alg_filename)\n\n template = env.from_string(main_template)\n return template.render(variables) + \"\\n\""} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load_metafile(filepath):\n try:\n with open(filepath, 'r', encoding='utf-8') as file:\n return email.message_from_file(file)\n except FileNotFoundError:\n logger.warning(\"Category file %s not found\", filepath)\n orm.delete(c for c in model.Category if c.file_path == filepath)\n orm.commit()\n\n return None", "response": "Load a metadata file from the filesystem"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nscans a file and put it into the index", "response": "def scan_file(fullpath, relpath):\n \"\"\" scan a file and put it into the index \"\"\"\n\n load_metafile.cache_clear()\n\n meta = load_metafile(fullpath)\n if not meta:\n return True\n\n # update the category meta file mapping\n category = meta.get('Category', utils.get_category(relpath))\n values = {\n 'category': category,\n 'file_path': fullpath,\n 'sort_name': meta.get('Sort-Name', '')\n }\n\n logger.debug(\"setting category %s to metafile %s\", category, fullpath)\n record = model.Category.get(category=category)\n if record:\n record.set(**values)\n else:\n record = model.Category(**values)\n\n # update other relationships to the index\n path_alias.remove_aliases(record)\n for alias in meta.get_all('Path-Alias', []):\n path_alias.set_alias(alias, category=record)\n\n orm.commit()\n\n return record"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef name(self):\n if self._meta and self._meta.get('name'):\n # get it from the meta file\n return self._meta.get('name')\n # infer it from the basename\n return self.basename.replace('_', ' ').title()", "response": "Get the display name of the category"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the textual description of the category", "response": "def description(self):\n \"\"\" Get the textual description of the category \"\"\"\n if self._meta and self._meta.get_payload():\n return utils.TrueCallableProxy(self._description)\n return utils.CallableProxy(None)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the category hierarchy leading up to this category and including the current category.", "response": "def breadcrumb(self):\n \"\"\" Get the category hierarchy leading up to this category, including\n root and self.\n\n For example, path/to/long/category will return a list containing\n Category('path'), Category('path/to'), and Category('path/to/long').\n \"\"\"\n ret = []\n here = self\n while here:\n ret.append(here)\n here = here.parent\n return list(reversed(ret))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the sorting name of this category", "response": "def sort_name(self):\n \"\"\" Get the sorting name of this category \"\"\"\n if self._record and self._record.sort_name:\n return self._record.sort_name\n return self.name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the parent category", "response": "def parent(self):\n \"\"\" Get the parent category \"\"\"\n if self.path:\n return Category(os.path.dirname(self.path))\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the subcategories of this category", "response": "def _get_subcats(self, recurse=False):\n \"\"\" Get the subcategories of this category\n\n recurse -- whether to include their subcategories as well\n \"\"\"\n\n if recurse:\n # No need to filter\n return sorted([Category(e) for e in self._subcats_recursive],\n key=lambda c: c.sort_breadcrumb)\n\n # get all the subcategories, with only the first subdir added\n\n # number of path components to ingest\n parts = len(self.path.split('/')) + 1 if self.path else 1\n\n # convert the subcategories into separated pathlists with only 'parts'\n # parts\n subcats = [c.split('/')[:parts] for c in self._subcats_recursive]\n\n # join them back into a path, and make unique\n subcats = {'/'.join(c) for c in subcats}\n\n # convert to a bunch of Category objects\n return sorted([Category(c) for c in subcats], key=lambda c: c.sort_name or c.name)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _first(self, **spec):\n for record in self._entries(spec).order_by(model.Entry.local_date,\n model.Entry.id)[:1]:\n return entry.Entry(record)\n return None", "response": "Return the first entry in this category optionally including subcategories"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _last(self, **spec):\n for record in self._entries(spec).order_by(orm.desc(model.Entry.local_date),\n orm.desc(model.Entry.id))[:1]:\n return entry.Entry(record)\n return None", "response": "Get the latest entry in this category optionally including subcategories"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef parse_date(datestr):\n\n match = re.match(\n r'([0-9]{4})(-?([0-9]{1,2}))?(-?([0-9]{1,2}))?(_w)?$', datestr)\n if not match:\n return (arrow.get(datestr,\n tzinfo=config.timezone).replace(tzinfo=config.timezone),\n 'day', 'YYYY-MM-DD')\n\n year, month, day, week = match.group(1, 3, 5, 6)\n start = arrow.Arrow(year=int(year), month=int(\n month or 1), day=int(day or 1), tzinfo=config.timezone)\n\n if week:\n return start.span('week')[0], 'week', WEEK_FORMAT\n if day:\n return start, 'day', DAY_FORMAT\n if month:\n return start, 'month', MONTH_FORMAT\n if year:\n return start, 'year', YEAR_FORMAT\n\n raise ValueError(\"Could not parse date: {}\".format(datestr))", "response": "Parse a date expression into a tuple of start_date span_type span_format"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfinds a file by relative path.", "response": "def find_file(path, search_path):\n \"\"\" Find a file by relative path. Arguments:\n\n path -- the image's filename\n search_path -- a list of directories to check in\n\n Returns: the resolved file path\n \"\"\"\n\n if isinstance(search_path, str):\n search_path = [search_path]\n for relative in search_path:\n candidate = os.path.normpath(os.path.join(relative, path))\n if os.path.isfile(candidate):\n return candidate\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef find_entry(rel_path, search_path):\n\n from . import entry # pylint:disable=cyclic-import\n\n try:\n entry_id = int(rel_path)\n record = model.Entry.get(id=entry_id)\n if record:\n return entry.Entry(record)\n except ValueError:\n pass\n\n if rel_path.startswith('/'):\n search_path = [config.content_folder]\n rel_path = '.' + rel_path\n\n for where in search_path:\n abspath = os.path.normpath(os.path.join(where, rel_path))\n record = model.Entry.get(file_path=abspath)\n if record:\n return entry.Entry(record)\n return None", "response": "Find an entry by relative path."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef static_url(path, absolute=False):\n\n if os.sep != '/':\n path = '/'.join(path.split(os.sep))\n\n return flask.url_for('static', filename=path, _external=absolute)", "response": "Shortcut for returning a URL for the requested static file."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbuild an HTML tag from the given name and attributes.", "response": "def make_tag(name, attrs, start_end=False):\n \"\"\" Build an HTML tag from the given name and attributes.\n\n Arguments:\n\n name -- the name of the tag (p, div, etc.)\n attrs -- a dict of attributes to apply to the tag\n start_end -- whether this tag should be self-closing\n \"\"\"\n\n text = '<' + name\n\n if isinstance(attrs, dict):\n attr_list = attrs.items()\n elif isinstance(attrs, list):\n attr_list = attrs\n elif attrs is not None:\n raise TypeError(\"Unhandled attrs type \" + str(type(attrs)))\n\n for key, val in attr_list:\n if val is not None:\n escaped = html.escape(str(val), False).replace('\"', '"')\n text += ' {}=\"{}\"'.format(key, escaped)\n if start_end:\n text += ' /'\n text += '>'\n return flask.Markup(text)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting a fingerprint for a file", "response": "def file_fingerprint(fullpath):\n \"\"\" Get a metadata fingerprint for a file \"\"\"\n stat = os.stat(fullpath)\n return ','.join([str(value) for value in [stat.st_ino, stat.st_mtime, stat.st_size] if value])"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates a new argument list by remapping keys.", "response": "def remap_args(input_args, remap):\n \"\"\" Generate a new argument list by remapping keys. The 'remap'\n dict maps from destination key -> priority list of source keys\n \"\"\"\n out_args = input_args\n for dest_key, src_keys in remap.items():\n remap_value = None\n if isinstance(src_keys, str):\n src_keys = [src_keys]\n\n for key in src_keys:\n if key in input_args:\n remap_value = input_args[key]\n break\n\n if remap_value is not None:\n if out_args is input_args:\n out_args = {**input_args}\n out_args[dest_key] = remap_value\n\n return out_args"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remap_link_target(path, absolute=False):\n if path.startswith('@'):\n # static resource\n return static_url(path[1:], absolute=absolute)\n\n if absolute:\n # absolute-ify whatever the URL is\n return urllib.parse.urljoin(flask.request.url, path)\n\n return path", "response": "remap a link target to a static URL if it s prefixed with @"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_category(filename):\n return '/'.join(os.path.dirname(filename).split(os.sep))", "response": "Get a default category name from a filename in a cross - platform manner"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the default function return", "response": "def _default(self):\n \"\"\" Get the default function return \"\"\"\n\n if self._default_args:\n return self._func(\n *self._default_args,\n **self._default_kwargs)\n\n return self._func(**self._default_kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting up the database", "response": "def setup():\n \"\"\" Set up the database \"\"\"\n try:\n db.bind(**config.database_config)\n except OSError:\n # Attempted to connect to a file-based database where the file didn't\n # exist\n db.bind(**config.database_config, create_db=True)\n\n rebuild = True\n\n try:\n db.generate_mapping(create_tables=True)\n with orm.db_session:\n version = GlobalConfig.get(key='schema_version')\n if version and version.int_value != SCHEMA_VERSION:\n logger.info(\"Existing database has schema version %d\",\n version.int_value)\n else:\n rebuild = False\n except: # pylint:disable=bare-except\n logger.exception(\"Error mapping schema\")\n\n if rebuild:\n logger.info(\"Rebuilding schema\")\n try:\n db.drop_all_tables(with_all_data=True)\n db.create_tables()\n except:\n raise RuntimeError(\"Unable to upgrade schema automatically; please \" +\n \"delete the existing database and try again.\")\n\n with orm.db_session:\n if not GlobalConfig.get(key='schema_version'):\n logger.info(\"setting schema version to %d\", SCHEMA_VERSION)\n GlobalConfig(key='schema_version',\n int_value=SCHEMA_VERSION)\n orm.commit()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning true if the entry should be viewable", "response": "def visible(self):\n \"\"\" Returns true if the entry should be viewable \"\"\"\n return self.status not in (PublishStatus.DRAFT.value,\n PublishStatus.GONE.value)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nextracts card data based on the provided texts.", "response": "def extract_card(text, config, image_search_path):\n \"\"\" Extract card data based on the provided texts. \"\"\"\n card = CardData()\n parser = CardParser(card, config, image_search_path)\n misaka.Markdown(parser, extensions=markdown.ENABLED_EXTENSIONS)(text)\n\n return card"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nturning the first paragraph of text into the summary text", "response": "def paragraph(self, content):\n \"\"\" Turn the first paragraph of text into the summary text \"\"\"\n if not self._out.description:\n self._out.description = content\n return ' '"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nextracts the images from the raw url", "response": "def image(self, raw_url, title='', alt=''):\n ''' extract the images '''\n max_images = self._config.get('count')\n if max_images is not None and len(self._out.images) >= max_images:\n # We already have enough images, so bail out\n return ' '\n\n image_specs = raw_url\n if title:\n image_specs += ' \"{}\"'.format(title)\n\n alt, container_args = image.parse_alt_text(alt)\n\n spec_list, _ = image.get_spec_list(image_specs, container_args)\n\n for spec in spec_list:\n if not spec:\n continue\n\n self._out.images.append(self._render_image(spec, alt))\n if max_images is not None and len(self._out.images) >= max_images:\n break\n\n return ' '"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _render_image(self, spec, alt=''):\n # pylint: disable=unused-argument\n\n try:\n path, image_args, _ = image.parse_image_spec(spec)\n except Exception as err: # pylint: disable=broad-except\n # we tried\u2122\n logger.exception(\"Got error on spec %s: %s\", spec, err)\n return None\n\n img = image.get_image(path, self._image_search_path)\n if img:\n image_config = {**image_args, **self._config, 'absolute': True}\n return img.get_rendition(1, **image_config)[0]\n\n return None", "response": "Given an image spec try to turn it into a card image per the configuration"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a Publ application and configure it for use with Publ.", "response": "def publ(name, cfg):\n \"\"\" Create a Flask app and configure it for use with Publ \"\"\"\n\n config.setup(cfg)\n\n app = _PublApp(name,\n template_folder=config.template_folder,\n static_folder=config.static_folder,\n static_url_path=config.static_url_path)\n\n for route in [\n '/',\n '//',\n '/